blob: 821334f6e3a1cab35bd7853c2c83f275efb30840 [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"
Stefan Behrens21adbd52011-11-09 13:44:05 +010035#include "check-integrity.h"
Chris Mason0b86a832008-03-24 15:01:56 -040036
Yan Zheng2b820322008-11-17 21:11:30 -050037static int init_first_rw_device(struct btrfs_trans_handle *trans,
38 struct btrfs_root *root,
39 struct btrfs_device *device);
40static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
41
Chris Mason8a4b83c2008-03-24 15:02:07 -040042static DEFINE_MUTEX(uuid_mutex);
43static LIST_HEAD(fs_uuids);
44
Chris Mason7d9eb122008-07-08 14:19:17 -040045static void lock_chunks(struct btrfs_root *root)
46{
Chris Mason7d9eb122008-07-08 14:19:17 -040047 mutex_lock(&root->fs_info->chunk_mutex);
48}
49
50static void unlock_chunks(struct btrfs_root *root)
51{
Chris Mason7d9eb122008-07-08 14:19:17 -040052 mutex_unlock(&root->fs_info->chunk_mutex);
53}
54
Yan Zhenge4404d62008-12-12 10:03:26 -050055static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
56{
57 struct btrfs_device *device;
58 WARN_ON(fs_devices->opened);
59 while (!list_empty(&fs_devices->devices)) {
60 device = list_entry(fs_devices->devices.next,
61 struct btrfs_device, dev_list);
62 list_del(&device->dev_list);
63 kfree(device->name);
64 kfree(device);
65 }
66 kfree(fs_devices);
67}
68
Chris Mason8a4b83c2008-03-24 15:02:07 -040069int btrfs_cleanup_fs_uuids(void)
70{
71 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040072
Yan Zheng2b820322008-11-17 21:11:30 -050073 while (!list_empty(&fs_uuids)) {
74 fs_devices = list_entry(fs_uuids.next,
75 struct btrfs_fs_devices, list);
76 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050077 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040078 }
79 return 0;
80}
81
Chris Masona1b32a52008-09-05 16:09:51 -040082static noinline struct btrfs_device *__find_device(struct list_head *head,
83 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040084{
85 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040086
Qinghuang Fengc6e30872009-01-21 10:59:08 -050087 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040088 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040089 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040090 return dev;
Chris Masona4437552008-04-18 10:29:38 -040091 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040092 }
93 return NULL;
94}
95
Chris Masona1b32a52008-09-05 16:09:51 -040096static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040097{
Chris Mason8a4b83c2008-03-24 15:02:07 -040098 struct btrfs_fs_devices *fs_devices;
99
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500100 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
102 return fs_devices;
103 }
104 return NULL;
105}
106
Chris Masonffbd5172009-04-20 15:50:09 -0400107static void requeue_list(struct btrfs_pending_bios *pending_bios,
108 struct bio *head, struct bio *tail)
109{
110
111 struct bio *old_head;
112
113 old_head = pending_bios->head;
114 pending_bios->head = head;
115 if (pending_bios->tail)
116 tail->bi_next = old_head;
117 else
118 pending_bios->tail = tail;
119}
120
Chris Mason8b712842008-06-11 16:50:36 -0400121/*
122 * we try to collect pending bios for a device so we don't get a large
123 * number of procs sending bios down to the same device. This greatly
124 * improves the schedulers ability to collect and merge the bios.
125 *
126 * But, it also turns into a long list of bios to process and that is sure
127 * to eventually make the worker thread block. The solution here is to
128 * make some progress and then put this work struct back at the end of
129 * the list if the block device is congested. This way, multiple devices
130 * can make progress from a single worker thread.
131 */
Chris Masond3977122009-01-05 21:25:51 -0500132static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400133{
134 struct bio *pending;
135 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400136 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400137 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400138 struct bio *tail;
139 struct bio *cur;
140 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400141 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400142 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400143 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400144 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400145 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000146 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400147 struct blk_plug plug;
148
149 /*
150 * this function runs all the bios we've collected for
151 * a particular device. We don't want to wander off to
152 * another device without first sending all of these down.
153 * So, setup a plug here and finish it off before we return
154 */
155 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400156
Chris Masonbedf7622009-04-03 10:32:58 -0400157 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400158 fs_info = device->dev_root->fs_info;
159 limit = btrfs_async_submit_limit(fs_info);
160 limit = limit * 2 / 3;
161
Chris Mason8b712842008-06-11 16:50:36 -0400162loop:
163 spin_lock(&device->io_lock);
164
Chris Masona6837052009-02-04 09:19:41 -0500165loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400166 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400167
Chris Mason8b712842008-06-11 16:50:36 -0400168 /* take all the bios off the list at once and process them
169 * later on (without the lock held). But, remember the
170 * tail and other pointers so the bios can be properly reinserted
171 * into the list if we hit congestion
172 */
Chris Masond84275c2009-06-09 15:39:08 -0400173 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400174 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400175 force_reg = 1;
176 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400177 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400178 force_reg = 0;
179 }
Chris Masonffbd5172009-04-20 15:50:09 -0400180
181 pending = pending_bios->head;
182 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400183 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400184
185 /*
186 * if pending was null this time around, no bios need processing
187 * at all and we can stop. Otherwise it'll loop back up again
188 * and do an additional check so no bios are missed.
189 *
190 * device->running_pending is used to synchronize with the
191 * schedule_bio code.
192 */
Chris Masonffbd5172009-04-20 15:50:09 -0400193 if (device->pending_sync_bios.head == NULL &&
194 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400195 again = 0;
196 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400197 } else {
198 again = 1;
199 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400200 }
Chris Masonffbd5172009-04-20 15:50:09 -0400201
202 pending_bios->head = NULL;
203 pending_bios->tail = NULL;
204
Chris Mason8b712842008-06-11 16:50:36 -0400205 spin_unlock(&device->io_lock);
206
Chris Masond3977122009-01-05 21:25:51 -0500207 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400208
209 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400210 /* we want to work on both lists, but do more bios on the
211 * sync list than the regular list
212 */
213 if ((num_run > 32 &&
214 pending_bios != &device->pending_sync_bios &&
215 device->pending_sync_bios.head) ||
216 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
217 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400218 spin_lock(&device->io_lock);
219 requeue_list(pending_bios, pending, tail);
220 goto loop_lock;
221 }
222
Chris Mason8b712842008-06-11 16:50:36 -0400223 cur = pending;
224 pending = pending->bi_next;
225 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400226 atomic_dec(&fs_info->nr_async_bios);
227
228 if (atomic_read(&fs_info->nr_async_bios) < limit &&
229 waitqueue_active(&fs_info->async_submit_wait))
230 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400231
232 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400233
Chris Mason2ab1ba62011-08-04 14:28:36 -0400234 /*
235 * if we're doing the sync list, record that our
236 * plug has some sync requests on it
237 *
238 * If we're doing the regular list and there are
239 * sync requests sitting around, unplug before
240 * we add more
241 */
242 if (pending_bios == &device->pending_sync_bios) {
243 sync_pending = 1;
244 } else if (sync_pending) {
245 blk_finish_plug(&plug);
246 blk_start_plug(&plug);
247 sync_pending = 0;
248 }
249
Stefan Behrens21adbd52011-11-09 13:44:05 +0100250 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400251 num_run++;
252 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100253 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400254 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400255
256 /*
257 * we made progress, there is more work to do and the bdi
258 * is now congested. Back off and let other work structs
259 * run instead
260 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400261 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500262 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400263 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400264
Chris Masonb765ead2009-04-03 10:27:10 -0400265 ioc = current->io_context;
266
267 /*
268 * the main goal here is that we don't want to
269 * block if we're going to be able to submit
270 * more requests without blocking.
271 *
272 * This code does two great things, it pokes into
273 * the elevator code from a filesystem _and_
274 * it makes assumptions about how batching works.
275 */
276 if (ioc && ioc->nr_batch_requests > 0 &&
277 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
278 (last_waited == 0 ||
279 ioc->last_waited == last_waited)) {
280 /*
281 * we want to go through our batch of
282 * requests and stop. So, we copy out
283 * the ioc->last_waited time and test
284 * against it before looping
285 */
286 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100287 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400288 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400289 continue;
290 }
Chris Mason8b712842008-06-11 16:50:36 -0400291 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400292 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500293 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400294
295 spin_unlock(&device->io_lock);
296 btrfs_requeue_work(&device->work);
297 goto done;
298 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500299 /* unplug every 64 requests just for good measure */
300 if (batch_run % 64 == 0) {
301 blk_finish_plug(&plug);
302 blk_start_plug(&plug);
303 sync_pending = 0;
304 }
Chris Mason8b712842008-06-11 16:50:36 -0400305 }
Chris Masonffbd5172009-04-20 15:50:09 -0400306
Chris Mason51684082010-03-10 15:33:32 -0500307 cond_resched();
308 if (again)
309 goto loop;
310
311 spin_lock(&device->io_lock);
312 if (device->pending_bios.head || device->pending_sync_bios.head)
313 goto loop_lock;
314 spin_unlock(&device->io_lock);
315
Chris Mason8b712842008-06-11 16:50:36 -0400316done:
Chris Mason211588a2011-04-19 20:12:40 -0400317 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400318 return 0;
319}
320
Christoph Hellwigb2950862008-12-02 09:54:17 -0500321static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400322{
323 struct btrfs_device *device;
324
325 device = container_of(work, struct btrfs_device, work);
326 run_scheduled_bios(device);
327}
328
Chris Masona1b32a52008-09-05 16:09:51 -0400329static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400330 struct btrfs_super_block *disk_super,
331 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
332{
333 struct btrfs_device *device;
334 struct btrfs_fs_devices *fs_devices;
335 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000336 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400337
338 fs_devices = find_fsid(disk_super->fsid);
339 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400340 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400341 if (!fs_devices)
342 return -ENOMEM;
343 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400344 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400345 list_add(&fs_devices->list, &fs_uuids);
346 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
347 fs_devices->latest_devid = devid;
348 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400349 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400350 device = NULL;
351 } else {
Chris Masona4437552008-04-18 10:29:38 -0400352 device = __find_device(&fs_devices->devices, devid,
353 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400354 }
355 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500356 if (fs_devices->opened)
357 return -EBUSY;
358
Chris Mason8a4b83c2008-03-24 15:02:07 -0400359 device = kzalloc(sizeof(*device), GFP_NOFS);
360 if (!device) {
361 /* we can safely leave the fs_devices entry around */
362 return -ENOMEM;
363 }
364 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400365 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400366 memcpy(device->uuid, disk_super->dev_item.uuid,
367 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400368 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400369 device->name = kstrdup(path, GFP_NOFS);
370 if (!device->name) {
371 kfree(device);
372 return -ENOMEM;
373 }
Yan Zheng2b820322008-11-17 21:11:30 -0500374 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400375
Arne Jansen90519d62011-05-23 14:30:00 +0200376 /* init readahead state */
377 spin_lock_init(&device->reada_lock);
378 device->reada_curr_zone = NULL;
379 atomic_set(&device->reada_in_flight, 0);
380 device->reada_next = 0;
381 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
382 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
383
Chris Masone5e9a522009-06-10 15:17:02 -0400384 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000385 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400386 mutex_unlock(&fs_devices->device_list_mutex);
387
Yan Zheng2b820322008-11-17 21:11:30 -0500388 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400389 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500390 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000391 name = kstrdup(path, GFP_NOFS);
392 if (!name)
393 return -ENOMEM;
394 kfree(device->name);
395 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500396 if (device->missing) {
397 fs_devices->missing_devices--;
398 device->missing = 0;
399 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400400 }
401
402 if (found_transid > fs_devices->latest_trans) {
403 fs_devices->latest_devid = devid;
404 fs_devices->latest_trans = found_transid;
405 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400406 *fs_devices_ret = fs_devices;
407 return 0;
408}
409
Yan Zhenge4404d62008-12-12 10:03:26 -0500410static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
411{
412 struct btrfs_fs_devices *fs_devices;
413 struct btrfs_device *device;
414 struct btrfs_device *orig_dev;
415
416 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
417 if (!fs_devices)
418 return ERR_PTR(-ENOMEM);
419
420 INIT_LIST_HEAD(&fs_devices->devices);
421 INIT_LIST_HEAD(&fs_devices->alloc_list);
422 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400423 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500424 fs_devices->latest_devid = orig->latest_devid;
425 fs_devices->latest_trans = orig->latest_trans;
426 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
427
Xiao Guangrong46224702011-04-20 10:08:47 +0000428 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500429 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
430 device = kzalloc(sizeof(*device), GFP_NOFS);
431 if (!device)
432 goto error;
433
434 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400435 if (!device->name) {
436 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500437 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400438 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500439
440 device->devid = orig_dev->devid;
441 device->work.func = pending_bios_fn;
442 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500443 spin_lock_init(&device->io_lock);
444 INIT_LIST_HEAD(&device->dev_list);
445 INIT_LIST_HEAD(&device->dev_alloc_list);
446
447 list_add(&device->dev_list, &fs_devices->devices);
448 device->fs_devices = fs_devices;
449 fs_devices->num_devices++;
450 }
451 return fs_devices;
452error:
453 free_fs_devices(fs_devices);
454 return ERR_PTR(-ENOMEM);
455}
456
Chris Masondfe25022008-05-13 13:46:40 -0400457int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
458{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500459 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400460
461 mutex_lock(&uuid_mutex);
462again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000463 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500464 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500465 if (device->in_fs_metadata)
466 continue;
467
468 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100469 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500470 device->bdev = NULL;
471 fs_devices->open_devices--;
472 }
473 if (device->writeable) {
474 list_del_init(&device->dev_alloc_list);
475 device->writeable = 0;
476 fs_devices->rw_devices--;
477 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500478 list_del_init(&device->dev_list);
479 fs_devices->num_devices--;
480 kfree(device->name);
481 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400482 }
Yan Zheng2b820322008-11-17 21:11:30 -0500483
484 if (fs_devices->seed) {
485 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500486 goto again;
487 }
488
Chris Masondfe25022008-05-13 13:46:40 -0400489 mutex_unlock(&uuid_mutex);
490 return 0;
491}
Chris Masona0af4692008-05-13 16:03:06 -0400492
Xiao Guangrong1f781602011-04-20 10:09:16 +0000493static void __free_device(struct work_struct *work)
494{
495 struct btrfs_device *device;
496
497 device = container_of(work, struct btrfs_device, rcu_work);
498
499 if (device->bdev)
500 blkdev_put(device->bdev, device->mode);
501
502 kfree(device->name);
503 kfree(device);
504}
505
506static void free_device(struct rcu_head *head)
507{
508 struct btrfs_device *device;
509
510 device = container_of(head, struct btrfs_device, rcu);
511
512 INIT_WORK(&device->rcu_work, __free_device);
513 schedule_work(&device->rcu_work);
514}
515
Yan Zheng2b820322008-11-17 21:11:30 -0500516static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400517{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400518 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500519
Yan Zheng2b820322008-11-17 21:11:30 -0500520 if (--fs_devices->opened > 0)
521 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400522
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000523 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500524 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000525 struct btrfs_device *new_device;
526
527 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400528 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000529
Yan Zheng2b820322008-11-17 21:11:30 -0500530 if (device->writeable) {
531 list_del_init(&device->dev_alloc_list);
532 fs_devices->rw_devices--;
533 }
534
Josef Bacikd5e20032011-08-04 14:52:27 +0000535 if (device->can_discard)
536 fs_devices->num_can_discard--;
537
Xiao Guangrong1f781602011-04-20 10:09:16 +0000538 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
539 BUG_ON(!new_device);
540 memcpy(new_device, device, sizeof(*new_device));
541 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000542 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000543 new_device->bdev = NULL;
544 new_device->writeable = 0;
545 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000546 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000547 list_replace_rcu(&device->dev_list, &new_device->dev_list);
548
549 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400550 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000551 mutex_unlock(&fs_devices->device_list_mutex);
552
Yan Zhenge4404d62008-12-12 10:03:26 -0500553 WARN_ON(fs_devices->open_devices);
554 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500555 fs_devices->opened = 0;
556 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500557
Chris Mason8a4b83c2008-03-24 15:02:07 -0400558 return 0;
559}
560
Yan Zheng2b820322008-11-17 21:11:30 -0500561int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
562{
Yan Zhenge4404d62008-12-12 10:03:26 -0500563 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500564 int ret;
565
566 mutex_lock(&uuid_mutex);
567 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500568 if (!fs_devices->opened) {
569 seed_devices = fs_devices->seed;
570 fs_devices->seed = NULL;
571 }
Yan Zheng2b820322008-11-17 21:11:30 -0500572 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500573
574 while (seed_devices) {
575 fs_devices = seed_devices;
576 seed_devices = fs_devices->seed;
577 __btrfs_close_devices(fs_devices);
578 free_fs_devices(fs_devices);
579 }
Yan Zheng2b820322008-11-17 21:11:30 -0500580 return ret;
581}
582
Yan Zhenge4404d62008-12-12 10:03:26 -0500583static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
584 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400585{
Josef Bacikd5e20032011-08-04 14:52:27 +0000586 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400587 struct block_device *bdev;
588 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400589 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400590 struct block_device *latest_bdev = NULL;
591 struct buffer_head *bh;
592 struct btrfs_super_block *disk_super;
593 u64 latest_devid = 0;
594 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400595 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500596 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400597 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400598
Tejun Heod4d77622010-11-13 11:55:18 +0100599 flags |= FMODE_EXCL;
600
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500601 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400602 if (device->bdev)
603 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400604 if (!device->name)
605 continue;
606
Tejun Heod4d77622010-11-13 11:55:18 +0100607 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400608 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500609 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400610 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400611 }
Chris Masona061fc82008-05-07 11:43:44 -0400612 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400613
Yan Zhenga512bbf2008-12-08 16:46:26 -0500614 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300615 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400616 goto error_close;
617
618 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000619 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400620 if (devid != device->devid)
621 goto error_brelse;
622
Yan Zheng2b820322008-11-17 21:11:30 -0500623 if (memcmp(device->uuid, disk_super->dev_item.uuid,
624 BTRFS_UUID_SIZE))
625 goto error_brelse;
626
627 device->generation = btrfs_super_generation(disk_super);
628 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400629 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500630 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400631 latest_bdev = bdev;
632 }
633
Yan Zheng2b820322008-11-17 21:11:30 -0500634 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
635 device->writeable = 0;
636 } else {
637 device->writeable = !bdev_read_only(bdev);
638 seeding = 0;
639 }
640
Josef Bacikd5e20032011-08-04 14:52:27 +0000641 q = bdev_get_queue(bdev);
642 if (blk_queue_discard(q)) {
643 device->can_discard = 1;
644 fs_devices->num_can_discard++;
645 }
646
Chris Mason8a4b83c2008-03-24 15:02:07 -0400647 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400648 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500649 device->mode = flags;
650
Chris Masonc289811c2009-06-10 09:51:32 -0400651 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
652 fs_devices->rotating = 1;
653
Chris Masona0af4692008-05-13 16:03:06 -0400654 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500655 if (device->writeable) {
656 fs_devices->rw_devices++;
657 list_add(&device->dev_alloc_list,
658 &fs_devices->alloc_list);
659 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000660 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400661 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400662
Chris Masona0af4692008-05-13 16:03:06 -0400663error_brelse:
664 brelse(bh);
665error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100666 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400667error:
668 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400669 }
Chris Masona0af4692008-05-13 16:03:06 -0400670 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300671 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400672 goto out;
673 }
Yan Zheng2b820322008-11-17 21:11:30 -0500674 fs_devices->seeding = seeding;
675 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400676 fs_devices->latest_bdev = latest_bdev;
677 fs_devices->latest_devid = latest_devid;
678 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500679 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400680out:
Yan Zheng2b820322008-11-17 21:11:30 -0500681 return ret;
682}
683
684int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500685 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500686{
687 int ret;
688
689 mutex_lock(&uuid_mutex);
690 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500691 fs_devices->opened++;
692 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500693 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500694 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500695 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400697 return ret;
698}
699
Christoph Hellwig97288f22008-12-02 06:36:09 -0500700int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400701 struct btrfs_fs_devices **fs_devices_ret)
702{
703 struct btrfs_super_block *disk_super;
704 struct block_device *bdev;
705 struct buffer_head *bh;
706 int ret;
707 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400708 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400709
710 mutex_lock(&uuid_mutex);
711
Tejun Heod4d77622010-11-13 11:55:18 +0100712 flags |= FMODE_EXCL;
713 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400714
715 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400716 ret = PTR_ERR(bdev);
717 goto error;
718 }
719
720 ret = set_blocksize(bdev, 4096);
721 if (ret)
722 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500723 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400724 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000725 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400726 goto error_close;
727 }
728 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000729 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400730 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400731 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500732 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200733 else
734 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500735 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500736 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400737 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
738
Chris Mason8a4b83c2008-03-24 15:02:07 -0400739 brelse(bh);
740error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100741 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400742error:
743 mutex_unlock(&uuid_mutex);
744 return ret;
745}
Chris Mason0b86a832008-03-24 15:01:56 -0400746
Miao Xie6d07bce2011-01-05 10:07:31 +0000747/* helper to account the used device space in the range */
748int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
749 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400750{
751 struct btrfs_key key;
752 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000753 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500754 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000755 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400756 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000757 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400758 struct extent_buffer *l;
759
Miao Xie6d07bce2011-01-05 10:07:31 +0000760 *length = 0;
761
762 if (start >= device->total_bytes)
763 return 0;
764
Yan Zheng2b820322008-11-17 21:11:30 -0500765 path = btrfs_alloc_path();
766 if (!path)
767 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400768 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400769
Chris Mason0b86a832008-03-24 15:01:56 -0400770 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000771 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400772 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000773
774 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400775 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000776 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400777 if (ret > 0) {
778 ret = btrfs_previous_item(root, path, key.objectid, key.type);
779 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000780 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400781 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000782
Chris Mason0b86a832008-03-24 15:01:56 -0400783 while (1) {
784 l = path->nodes[0];
785 slot = path->slots[0];
786 if (slot >= btrfs_header_nritems(l)) {
787 ret = btrfs_next_leaf(root, path);
788 if (ret == 0)
789 continue;
790 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000791 goto out;
792
793 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400794 }
795 btrfs_item_key_to_cpu(l, &key, slot);
796
797 if (key.objectid < device->devid)
798 goto next;
799
800 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000801 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400802
Chris Masond3977122009-01-05 21:25:51 -0500803 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400804 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400805
Chris Mason0b86a832008-03-24 15:01:56 -0400806 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000807 extent_end = key.offset + btrfs_dev_extent_length(l,
808 dev_extent);
809 if (key.offset <= start && extent_end > end) {
810 *length = end - start + 1;
811 break;
812 } else if (key.offset <= start && extent_end > start)
813 *length += extent_end - start;
814 else if (key.offset > start && extent_end <= end)
815 *length += extent_end - key.offset;
816 else if (key.offset > start && key.offset <= end) {
817 *length += end - key.offset + 1;
818 break;
819 } else if (key.offset > end)
820 break;
821
822next:
823 path->slots[0]++;
824 }
825 ret = 0;
826out:
827 btrfs_free_path(path);
828 return ret;
829}
830
Chris Mason0b86a832008-03-24 15:01:56 -0400831/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000832 * find_free_dev_extent - find free space in the specified device
833 * @trans: transaction handler
834 * @device: the device which we search the free space in
835 * @num_bytes: the size of the free space that we need
836 * @start: store the start of the free space.
837 * @len: the size of the free space. that we find, or the size of the max
838 * free space if we don't find suitable free space
839 *
Chris Mason0b86a832008-03-24 15:01:56 -0400840 * this uses a pretty simple search, the expectation is that it is
841 * called very infrequently and that a given device has a small number
842 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000843 *
844 * @start is used to store the start of the free space if we find. But if we
845 * don't find suitable free space, it will be used to store the start position
846 * of the max free space.
847 *
848 * @len is used to store the size of the free space that we find.
849 * But if we don't find suitable free space, it is used to store the size of
850 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400851 */
852int find_free_dev_extent(struct btrfs_trans_handle *trans,
853 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000854 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400855{
856 struct btrfs_key key;
857 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000858 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400859 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000860 u64 hole_size;
861 u64 max_hole_start;
862 u64 max_hole_size;
863 u64 extent_end;
864 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400865 u64 search_end = device->total_bytes;
866 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000867 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400868 struct extent_buffer *l;
869
Chris Mason0b86a832008-03-24 15:01:56 -0400870 /* FIXME use last free of some kind */
871
872 /* we don't want to overwrite the superblock on the drive,
873 * so we make sure to start at an offset of at least 1MB
874 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200875 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400876
Miao Xie7bfc8372011-01-05 10:07:26 +0000877 max_hole_start = search_start;
878 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000879 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000880
881 if (search_start >= search_end) {
882 ret = -ENOSPC;
883 goto error;
884 }
885
886 path = btrfs_alloc_path();
887 if (!path) {
888 ret = -ENOMEM;
889 goto error;
890 }
891 path->reada = 2;
892
Chris Mason0b86a832008-03-24 15:01:56 -0400893 key.objectid = device->devid;
894 key.offset = search_start;
895 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000896
Chris Mason0b86a832008-03-24 15:01:56 -0400897 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
898 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000899 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400900 if (ret > 0) {
901 ret = btrfs_previous_item(root, path, key.objectid, key.type);
902 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000903 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400904 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000905
Chris Mason0b86a832008-03-24 15:01:56 -0400906 while (1) {
907 l = path->nodes[0];
908 slot = path->slots[0];
909 if (slot >= btrfs_header_nritems(l)) {
910 ret = btrfs_next_leaf(root, path);
911 if (ret == 0)
912 continue;
913 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000914 goto out;
915
916 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400917 }
918 btrfs_item_key_to_cpu(l, &key, slot);
919
920 if (key.objectid < device->devid)
921 goto next;
922
923 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000924 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400925
Chris Mason0b86a832008-03-24 15:01:56 -0400926 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
927 goto next;
928
Miao Xie7bfc8372011-01-05 10:07:26 +0000929 if (key.offset > search_start) {
930 hole_size = key.offset - search_start;
931
932 if (hole_size > max_hole_size) {
933 max_hole_start = search_start;
934 max_hole_size = hole_size;
935 }
936
937 /*
938 * If this free space is greater than which we need,
939 * it must be the max free space that we have found
940 * until now, so max_hole_start must point to the start
941 * of this free space and the length of this free space
942 * is stored in max_hole_size. Thus, we return
943 * max_hole_start and max_hole_size and go back to the
944 * caller.
945 */
946 if (hole_size >= num_bytes) {
947 ret = 0;
948 goto out;
949 }
950 }
951
Chris Mason0b86a832008-03-24 15:01:56 -0400952 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000953 extent_end = key.offset + btrfs_dev_extent_length(l,
954 dev_extent);
955 if (extent_end > search_start)
956 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400957next:
958 path->slots[0]++;
959 cond_resched();
960 }
Chris Mason0b86a832008-03-24 15:01:56 -0400961
liubo38c01b92011-08-02 02:39:03 +0000962 /*
963 * At this point, search_start should be the end of
964 * allocated dev extents, and when shrinking the device,
965 * search_end may be smaller than search_start.
966 */
967 if (search_end > search_start)
968 hole_size = search_end - search_start;
969
Miao Xie7bfc8372011-01-05 10:07:26 +0000970 if (hole_size > max_hole_size) {
971 max_hole_start = search_start;
972 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400973 }
Chris Mason0b86a832008-03-24 15:01:56 -0400974
Miao Xie7bfc8372011-01-05 10:07:26 +0000975 /* See above. */
976 if (hole_size < num_bytes)
977 ret = -ENOSPC;
978 else
979 ret = 0;
980
981out:
Yan Zheng2b820322008-11-17 21:11:30 -0500982 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000983error:
984 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000985 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000986 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400987 return ret;
988}
989
Christoph Hellwigb2950862008-12-02 09:54:17 -0500990static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400991 struct btrfs_device *device,
992 u64 start)
993{
994 int ret;
995 struct btrfs_path *path;
996 struct btrfs_root *root = device->dev_root;
997 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400998 struct btrfs_key found_key;
999 struct extent_buffer *leaf = NULL;
1000 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001001
1002 path = btrfs_alloc_path();
1003 if (!path)
1004 return -ENOMEM;
1005
1006 key.objectid = device->devid;
1007 key.offset = start;
1008 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001009again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001010 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001011 if (ret > 0) {
1012 ret = btrfs_previous_item(root, path, key.objectid,
1013 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001014 if (ret)
1015 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001016 leaf = path->nodes[0];
1017 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1018 extent = btrfs_item_ptr(leaf, path->slots[0],
1019 struct btrfs_dev_extent);
1020 BUG_ON(found_key.offset > start || found_key.offset +
1021 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001022 key = found_key;
1023 btrfs_release_path(path);
1024 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001025 } else if (ret == 0) {
1026 leaf = path->nodes[0];
1027 extent = btrfs_item_ptr(leaf, path->slots[0],
1028 struct btrfs_dev_extent);
1029 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001030 BUG_ON(ret);
1031
Josef Bacik2bf64752011-09-26 17:12:22 -04001032 if (device->bytes_used > 0) {
1033 u64 len = btrfs_dev_extent_length(leaf, extent);
1034 device->bytes_used -= len;
1035 spin_lock(&root->fs_info->free_chunk_lock);
1036 root->fs_info->free_chunk_space += len;
1037 spin_unlock(&root->fs_info->free_chunk_lock);
1038 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001039 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001040
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001041out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001042 btrfs_free_path(path);
1043 return ret;
1044}
1045
Yan Zheng2b820322008-11-17 21:11:30 -05001046int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001047 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001048 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001049 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001050{
1051 int ret;
1052 struct btrfs_path *path;
1053 struct btrfs_root *root = device->dev_root;
1054 struct btrfs_dev_extent *extent;
1055 struct extent_buffer *leaf;
1056 struct btrfs_key key;
1057
Chris Masondfe25022008-05-13 13:46:40 -04001058 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001059 path = btrfs_alloc_path();
1060 if (!path)
1061 return -ENOMEM;
1062
Chris Mason0b86a832008-03-24 15:01:56 -04001063 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001064 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001065 key.type = BTRFS_DEV_EXTENT_KEY;
1066 ret = btrfs_insert_empty_item(trans, root, path, &key,
1067 sizeof(*extent));
1068 BUG_ON(ret);
1069
1070 leaf = path->nodes[0];
1071 extent = btrfs_item_ptr(leaf, path->slots[0],
1072 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001073 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1074 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1075 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1076
1077 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1078 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1079 BTRFS_UUID_SIZE);
1080
Chris Mason0b86a832008-03-24 15:01:56 -04001081 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1082 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001083 btrfs_free_path(path);
1084 return ret;
1085}
1086
Chris Masona1b32a52008-09-05 16:09:51 -04001087static noinline int find_next_chunk(struct btrfs_root *root,
1088 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001089{
1090 struct btrfs_path *path;
1091 int ret;
1092 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001093 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001094 struct btrfs_key found_key;
1095
1096 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001097 if (!path)
1098 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001099
Chris Masone17cade2008-04-15 15:41:47 -04001100 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001101 key.offset = (u64)-1;
1102 key.type = BTRFS_CHUNK_ITEM_KEY;
1103
1104 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1105 if (ret < 0)
1106 goto error;
1107
1108 BUG_ON(ret == 0);
1109
1110 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1111 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001112 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001113 } else {
1114 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1115 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001116 if (found_key.objectid != objectid)
1117 *offset = 0;
1118 else {
1119 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1120 struct btrfs_chunk);
1121 *offset = found_key.offset +
1122 btrfs_chunk_length(path->nodes[0], chunk);
1123 }
Chris Mason0b86a832008-03-24 15:01:56 -04001124 }
1125 ret = 0;
1126error:
1127 btrfs_free_path(path);
1128 return ret;
1129}
1130
Yan Zheng2b820322008-11-17 21:11:30 -05001131static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001132{
1133 int ret;
1134 struct btrfs_key key;
1135 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001136 struct btrfs_path *path;
1137
1138 root = root->fs_info->chunk_root;
1139
1140 path = btrfs_alloc_path();
1141 if (!path)
1142 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001143
1144 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1145 key.type = BTRFS_DEV_ITEM_KEY;
1146 key.offset = (u64)-1;
1147
1148 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1149 if (ret < 0)
1150 goto error;
1151
1152 BUG_ON(ret == 0);
1153
1154 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1155 BTRFS_DEV_ITEM_KEY);
1156 if (ret) {
1157 *objectid = 1;
1158 } else {
1159 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1160 path->slots[0]);
1161 *objectid = found_key.offset + 1;
1162 }
1163 ret = 0;
1164error:
Yan Zheng2b820322008-11-17 21:11:30 -05001165 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001166 return ret;
1167}
1168
1169/*
1170 * the device information is stored in the chunk root
1171 * the btrfs_device struct should be fully filled in
1172 */
1173int btrfs_add_device(struct btrfs_trans_handle *trans,
1174 struct btrfs_root *root,
1175 struct btrfs_device *device)
1176{
1177 int ret;
1178 struct btrfs_path *path;
1179 struct btrfs_dev_item *dev_item;
1180 struct extent_buffer *leaf;
1181 struct btrfs_key key;
1182 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001183
1184 root = root->fs_info->chunk_root;
1185
1186 path = btrfs_alloc_path();
1187 if (!path)
1188 return -ENOMEM;
1189
Chris Mason0b86a832008-03-24 15:01:56 -04001190 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1191 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001192 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001193
1194 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001195 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001196 if (ret)
1197 goto out;
1198
1199 leaf = path->nodes[0];
1200 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1201
1202 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001203 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001204 btrfs_set_device_type(leaf, dev_item, device->type);
1205 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1206 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1207 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001208 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1209 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001210 btrfs_set_device_group(leaf, dev_item, 0);
1211 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1212 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001213 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001214
Chris Mason0b86a832008-03-24 15:01:56 -04001215 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001216 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001217 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1218 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001219 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001220
Yan Zheng2b820322008-11-17 21:11:30 -05001221 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001222out:
1223 btrfs_free_path(path);
1224 return ret;
1225}
Chris Mason8f18cf12008-04-25 16:53:30 -04001226
Chris Masona061fc82008-05-07 11:43:44 -04001227static int btrfs_rm_dev_item(struct btrfs_root *root,
1228 struct btrfs_device *device)
1229{
1230 int ret;
1231 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001232 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001233 struct btrfs_trans_handle *trans;
1234
1235 root = root->fs_info->chunk_root;
1236
1237 path = btrfs_alloc_path();
1238 if (!path)
1239 return -ENOMEM;
1240
Yan, Zhenga22285a2010-05-16 10:48:46 -04001241 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001242 if (IS_ERR(trans)) {
1243 btrfs_free_path(path);
1244 return PTR_ERR(trans);
1245 }
Chris Masona061fc82008-05-07 11:43:44 -04001246 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1247 key.type = BTRFS_DEV_ITEM_KEY;
1248 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001249 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001250
1251 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1252 if (ret < 0)
1253 goto out;
1254
1255 if (ret > 0) {
1256 ret = -ENOENT;
1257 goto out;
1258 }
1259
1260 ret = btrfs_del_item(trans, root, path);
1261 if (ret)
1262 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001263out:
1264 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001265 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001266 btrfs_commit_transaction(trans, root);
1267 return ret;
1268}
1269
1270int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1271{
1272 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001273 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001274 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001275 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001276 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001277 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001278 u64 all_avail;
1279 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001280 u64 num_devices;
1281 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001282 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001283 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001284
Chris Masona061fc82008-05-07 11:43:44 -04001285 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001286 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001287
1288 all_avail = root->fs_info->avail_data_alloc_bits |
1289 root->fs_info->avail_system_alloc_bits |
1290 root->fs_info->avail_metadata_alloc_bits;
1291
1292 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001293 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001294 printk(KERN_ERR "btrfs: unable to go below four devices "
1295 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001296 ret = -EINVAL;
1297 goto out;
1298 }
1299
1300 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001301 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001302 printk(KERN_ERR "btrfs: unable to go below two "
1303 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001304 ret = -EINVAL;
1305 goto out;
1306 }
1307
Chris Masondfe25022008-05-13 13:46:40 -04001308 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001309 struct list_head *devices;
1310 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001311
Chris Masondfe25022008-05-13 13:46:40 -04001312 device = NULL;
1313 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001314 /*
1315 * It is safe to read the devices since the volume_mutex
1316 * is held.
1317 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001318 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001319 if (tmp->in_fs_metadata && !tmp->bdev) {
1320 device = tmp;
1321 break;
1322 }
1323 }
1324 bdev = NULL;
1325 bh = NULL;
1326 disk_super = NULL;
1327 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001328 printk(KERN_ERR "btrfs: no missing devices found to "
1329 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001330 goto out;
1331 }
Chris Masondfe25022008-05-13 13:46:40 -04001332 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001333 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1334 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001335 if (IS_ERR(bdev)) {
1336 ret = PTR_ERR(bdev);
1337 goto out;
1338 }
1339
Yan Zheng2b820322008-11-17 21:11:30 -05001340 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001341 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001342 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001343 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001344 goto error_close;
1345 }
1346 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001347 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001348 dev_uuid = disk_super->dev_item.uuid;
1349 device = btrfs_find_device(root, devid, dev_uuid,
1350 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001351 if (!device) {
1352 ret = -ENOENT;
1353 goto error_brelse;
1354 }
Chris Masondfe25022008-05-13 13:46:40 -04001355 }
Yan Zheng2b820322008-11-17 21:11:30 -05001356
1357 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001358 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1359 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001360 ret = -EINVAL;
1361 goto error_brelse;
1362 }
1363
1364 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001365 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001366 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001367 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001368 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001369 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001370 }
Chris Masona061fc82008-05-07 11:43:44 -04001371
1372 ret = btrfs_shrink_device(device, 0);
1373 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001374 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001375
Chris Masona061fc82008-05-07 11:43:44 -04001376 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1377 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001378 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001379
Josef Bacik2bf64752011-09-26 17:12:22 -04001380 spin_lock(&root->fs_info->free_chunk_lock);
1381 root->fs_info->free_chunk_space = device->total_bytes -
1382 device->bytes_used;
1383 spin_unlock(&root->fs_info->free_chunk_lock);
1384
Yan Zheng2b820322008-11-17 21:11:30 -05001385 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001386 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001387
1388 /*
1389 * the device list mutex makes sure that we don't change
1390 * the device list while someone else is writing out all
1391 * the device supers.
1392 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001393
1394 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001395 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001396 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001397
Yan Zhenge4404d62008-12-12 10:03:26 -05001398 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001399
Chris Masoncd02dca2010-12-13 14:56:23 -05001400 if (device->missing)
1401 root->fs_info->fs_devices->missing_devices--;
1402
Yan Zheng2b820322008-11-17 21:11:30 -05001403 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1404 struct btrfs_device, dev_list);
1405 if (device->bdev == root->fs_info->sb->s_bdev)
1406 root->fs_info->sb->s_bdev = next_device->bdev;
1407 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1408 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1409
Xiao Guangrong1f781602011-04-20 10:09:16 +00001410 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001411 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001412
1413 call_rcu(&device->rcu, free_device);
1414 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001415
David Sterba6c417612011-04-13 15:41:04 +02001416 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1417 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001418
Xiao Guangrong1f781602011-04-20 10:09:16 +00001419 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001420 struct btrfs_fs_devices *fs_devices;
1421 fs_devices = root->fs_info->fs_devices;
1422 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001423 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001424 break;
1425 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001426 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001427 fs_devices->seed = cur_devices->seed;
1428 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001429 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001430 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001431 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001432 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001433 }
1434
1435 /*
1436 * at this point, the device is zero sized. We want to
1437 * remove it from the devices list and zero out the old super
1438 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001439 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001440 /* make sure this device isn't detected as part of
1441 * the FS anymore
1442 */
1443 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1444 set_buffer_dirty(bh);
1445 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001446 }
Chris Masona061fc82008-05-07 11:43:44 -04001447
Chris Masona061fc82008-05-07 11:43:44 -04001448 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001449
1450error_brelse:
1451 brelse(bh);
1452error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001453 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001454 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001455out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001456 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001457 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001458 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001459error_undo:
1460 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001461 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001462 list_add(&device->dev_alloc_list,
1463 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001464 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001465 root->fs_info->fs_devices->rw_devices++;
1466 }
1467 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001468}
1469
Yan Zheng2b820322008-11-17 21:11:30 -05001470/*
1471 * does all the dirty work required for changing file system's UUID.
1472 */
1473static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1474 struct btrfs_root *root)
1475{
1476 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1477 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001478 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001479 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001480 struct btrfs_device *device;
1481 u64 super_flags;
1482
1483 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001484 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001485 return -EINVAL;
1486
Yan Zhenge4404d62008-12-12 10:03:26 -05001487 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1488 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001489 return -ENOMEM;
1490
Yan Zhenge4404d62008-12-12 10:03:26 -05001491 old_devices = clone_fs_devices(fs_devices);
1492 if (IS_ERR(old_devices)) {
1493 kfree(seed_devices);
1494 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001495 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001496
Yan Zheng2b820322008-11-17 21:11:30 -05001497 list_add(&old_devices->list, &fs_uuids);
1498
Yan Zhenge4404d62008-12-12 10:03:26 -05001499 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1500 seed_devices->opened = 1;
1501 INIT_LIST_HEAD(&seed_devices->devices);
1502 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001503 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001504
1505 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001506 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1507 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001508 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1509
Yan Zhenge4404d62008-12-12 10:03:26 -05001510 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1511 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1512 device->fs_devices = seed_devices;
1513 }
1514
Yan Zheng2b820322008-11-17 21:11:30 -05001515 fs_devices->seeding = 0;
1516 fs_devices->num_devices = 0;
1517 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001518 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001519
1520 generate_random_uuid(fs_devices->fsid);
1521 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1522 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1523 super_flags = btrfs_super_flags(disk_super) &
1524 ~BTRFS_SUPER_FLAG_SEEDING;
1525 btrfs_set_super_flags(disk_super, super_flags);
1526
1527 return 0;
1528}
1529
1530/*
1531 * strore the expected generation for seed devices in device items.
1532 */
1533static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1534 struct btrfs_root *root)
1535{
1536 struct btrfs_path *path;
1537 struct extent_buffer *leaf;
1538 struct btrfs_dev_item *dev_item;
1539 struct btrfs_device *device;
1540 struct btrfs_key key;
1541 u8 fs_uuid[BTRFS_UUID_SIZE];
1542 u8 dev_uuid[BTRFS_UUID_SIZE];
1543 u64 devid;
1544 int ret;
1545
1546 path = btrfs_alloc_path();
1547 if (!path)
1548 return -ENOMEM;
1549
1550 root = root->fs_info->chunk_root;
1551 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1552 key.offset = 0;
1553 key.type = BTRFS_DEV_ITEM_KEY;
1554
1555 while (1) {
1556 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1557 if (ret < 0)
1558 goto error;
1559
1560 leaf = path->nodes[0];
1561next_slot:
1562 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1563 ret = btrfs_next_leaf(root, path);
1564 if (ret > 0)
1565 break;
1566 if (ret < 0)
1567 goto error;
1568 leaf = path->nodes[0];
1569 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001570 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001571 continue;
1572 }
1573
1574 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1575 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1576 key.type != BTRFS_DEV_ITEM_KEY)
1577 break;
1578
1579 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1580 struct btrfs_dev_item);
1581 devid = btrfs_device_id(leaf, dev_item);
1582 read_extent_buffer(leaf, dev_uuid,
1583 (unsigned long)btrfs_device_uuid(dev_item),
1584 BTRFS_UUID_SIZE);
1585 read_extent_buffer(leaf, fs_uuid,
1586 (unsigned long)btrfs_device_fsid(dev_item),
1587 BTRFS_UUID_SIZE);
1588 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1589 BUG_ON(!device);
1590
1591 if (device->fs_devices->seeding) {
1592 btrfs_set_device_generation(leaf, dev_item,
1593 device->generation);
1594 btrfs_mark_buffer_dirty(leaf);
1595 }
1596
1597 path->slots[0]++;
1598 goto next_slot;
1599 }
1600 ret = 0;
1601error:
1602 btrfs_free_path(path);
1603 return ret;
1604}
1605
Chris Mason788f20e2008-04-28 15:29:42 -04001606int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1607{
Josef Bacikd5e20032011-08-04 14:52:27 +00001608 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001609 struct btrfs_trans_handle *trans;
1610 struct btrfs_device *device;
1611 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001612 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001613 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001614 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001615 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001616 int ret = 0;
1617
Yan Zheng2b820322008-11-17 21:11:30 -05001618 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1619 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001620
Li Zefana5d16332011-12-07 20:08:40 -05001621 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001622 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001623 if (IS_ERR(bdev))
1624 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001625
Yan Zheng2b820322008-11-17 21:11:30 -05001626 if (root->fs_info->fs_devices->seeding) {
1627 seeding_dev = 1;
1628 down_write(&sb->s_umount);
1629 mutex_lock(&uuid_mutex);
1630 }
1631
Chris Mason8c8bee12008-09-29 11:19:10 -04001632 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001633 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001634
Chris Mason788f20e2008-04-28 15:29:42 -04001635 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001636 /*
1637 * we have the volume lock, so we don't need the extra
1638 * device list mutex while reading the list here.
1639 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001640 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001641 if (device->bdev == bdev) {
1642 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001643 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001644 }
1645 }
1646
1647 device = kzalloc(sizeof(*device), GFP_NOFS);
1648 if (!device) {
1649 /* we can safely leave the fs_devices entry around */
1650 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001651 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001652 }
1653
Chris Mason788f20e2008-04-28 15:29:42 -04001654 device->name = kstrdup(device_path, GFP_NOFS);
1655 if (!device->name) {
1656 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001657 ret = -ENOMEM;
1658 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001659 }
Yan Zheng2b820322008-11-17 21:11:30 -05001660
1661 ret = find_next_devid(root, &device->devid);
1662 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001663 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001664 kfree(device);
1665 goto error;
1666 }
1667
Yan, Zhenga22285a2010-05-16 10:48:46 -04001668 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001669 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001670 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001671 kfree(device);
1672 ret = PTR_ERR(trans);
1673 goto error;
1674 }
1675
Yan Zheng2b820322008-11-17 21:11:30 -05001676 lock_chunks(root);
1677
Josef Bacikd5e20032011-08-04 14:52:27 +00001678 q = bdev_get_queue(bdev);
1679 if (blk_queue_discard(q))
1680 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001681 device->writeable = 1;
1682 device->work.func = pending_bios_fn;
1683 generate_random_uuid(device->uuid);
1684 spin_lock_init(&device->io_lock);
1685 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001686 device->io_width = root->sectorsize;
1687 device->io_align = root->sectorsize;
1688 device->sector_size = root->sectorsize;
1689 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001690 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001691 device->dev_root = root->fs_info->dev_root;
1692 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001693 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001694 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001695 set_blocksize(device->bdev, 4096);
1696
Yan Zheng2b820322008-11-17 21:11:30 -05001697 if (seeding_dev) {
1698 sb->s_flags &= ~MS_RDONLY;
1699 ret = btrfs_prepare_sprout(trans, root);
1700 BUG_ON(ret);
1701 }
1702
1703 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001704
1705 /*
1706 * we don't want write_supers to jump in here with our device
1707 * half setup
1708 */
1709 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001710 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001711 list_add(&device->dev_alloc_list,
1712 &root->fs_info->fs_devices->alloc_list);
1713 root->fs_info->fs_devices->num_devices++;
1714 root->fs_info->fs_devices->open_devices++;
1715 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001716 if (device->can_discard)
1717 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001718 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1719
Josef Bacik2bf64752011-09-26 17:12:22 -04001720 spin_lock(&root->fs_info->free_chunk_lock);
1721 root->fs_info->free_chunk_space += device->total_bytes;
1722 spin_unlock(&root->fs_info->free_chunk_lock);
1723
Chris Masonc289811c2009-06-10 09:51:32 -04001724 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1725 root->fs_info->fs_devices->rotating = 1;
1726
David Sterba6c417612011-04-13 15:41:04 +02001727 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1728 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001729 total_bytes + device->total_bytes);
1730
David Sterba6c417612011-04-13 15:41:04 +02001731 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1732 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001733 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001734 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001735
Yan Zheng2b820322008-11-17 21:11:30 -05001736 if (seeding_dev) {
1737 ret = init_first_rw_device(trans, root, device);
1738 BUG_ON(ret);
1739 ret = btrfs_finish_sprout(trans, root);
1740 BUG_ON(ret);
1741 } else {
1742 ret = btrfs_add_device(trans, root, device);
1743 }
1744
Chris Mason913d9522009-03-10 13:17:18 -04001745 /*
1746 * we've got more storage, clear any full flags on the space
1747 * infos
1748 */
1749 btrfs_clear_space_info_full(root->fs_info);
1750
Chris Mason7d9eb122008-07-08 14:19:17 -04001751 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001752 btrfs_commit_transaction(trans, root);
1753
1754 if (seeding_dev) {
1755 mutex_unlock(&uuid_mutex);
1756 up_write(&sb->s_umount);
1757
1758 ret = btrfs_relocate_sys_chunks(root);
1759 BUG_ON(ret);
1760 }
1761out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001762 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001763 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001764error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001765 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001766 if (seeding_dev) {
1767 mutex_unlock(&uuid_mutex);
1768 up_write(&sb->s_umount);
1769 }
Chris Mason788f20e2008-04-28 15:29:42 -04001770 goto out;
1771}
1772
Chris Masond3977122009-01-05 21:25:51 -05001773static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1774 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001775{
1776 int ret;
1777 struct btrfs_path *path;
1778 struct btrfs_root *root;
1779 struct btrfs_dev_item *dev_item;
1780 struct extent_buffer *leaf;
1781 struct btrfs_key key;
1782
1783 root = device->dev_root->fs_info->chunk_root;
1784
1785 path = btrfs_alloc_path();
1786 if (!path)
1787 return -ENOMEM;
1788
1789 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1790 key.type = BTRFS_DEV_ITEM_KEY;
1791 key.offset = device->devid;
1792
1793 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1794 if (ret < 0)
1795 goto out;
1796
1797 if (ret > 0) {
1798 ret = -ENOENT;
1799 goto out;
1800 }
1801
1802 leaf = path->nodes[0];
1803 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1804
1805 btrfs_set_device_id(leaf, dev_item, device->devid);
1806 btrfs_set_device_type(leaf, dev_item, device->type);
1807 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1808 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1809 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001810 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001811 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1812 btrfs_mark_buffer_dirty(leaf);
1813
1814out:
1815 btrfs_free_path(path);
1816 return ret;
1817}
1818
Chris Mason7d9eb122008-07-08 14:19:17 -04001819static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001820 struct btrfs_device *device, u64 new_size)
1821{
1822 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001823 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001824 u64 old_total = btrfs_super_total_bytes(super_copy);
1825 u64 diff = new_size - device->total_bytes;
1826
Yan Zheng2b820322008-11-17 21:11:30 -05001827 if (!device->writeable)
1828 return -EACCES;
1829 if (new_size <= device->total_bytes)
1830 return -EINVAL;
1831
Chris Mason8f18cf12008-04-25 16:53:30 -04001832 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001833 device->fs_devices->total_rw_bytes += diff;
1834
1835 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001836 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001837 btrfs_clear_space_info_full(device->dev_root->fs_info);
1838
Chris Mason8f18cf12008-04-25 16:53:30 -04001839 return btrfs_update_device(trans, device);
1840}
1841
Chris Mason7d9eb122008-07-08 14:19:17 -04001842int btrfs_grow_device(struct btrfs_trans_handle *trans,
1843 struct btrfs_device *device, u64 new_size)
1844{
1845 int ret;
1846 lock_chunks(device->dev_root);
1847 ret = __btrfs_grow_device(trans, device, new_size);
1848 unlock_chunks(device->dev_root);
1849 return ret;
1850}
1851
Chris Mason8f18cf12008-04-25 16:53:30 -04001852static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1853 struct btrfs_root *root,
1854 u64 chunk_tree, u64 chunk_objectid,
1855 u64 chunk_offset)
1856{
1857 int ret;
1858 struct btrfs_path *path;
1859 struct btrfs_key key;
1860
1861 root = root->fs_info->chunk_root;
1862 path = btrfs_alloc_path();
1863 if (!path)
1864 return -ENOMEM;
1865
1866 key.objectid = chunk_objectid;
1867 key.offset = chunk_offset;
1868 key.type = BTRFS_CHUNK_ITEM_KEY;
1869
1870 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1871 BUG_ON(ret);
1872
1873 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001874
1875 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001876 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001877}
1878
Christoph Hellwigb2950862008-12-02 09:54:17 -05001879static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001880 chunk_offset)
1881{
David Sterba6c417612011-04-13 15:41:04 +02001882 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001883 struct btrfs_disk_key *disk_key;
1884 struct btrfs_chunk *chunk;
1885 u8 *ptr;
1886 int ret = 0;
1887 u32 num_stripes;
1888 u32 array_size;
1889 u32 len = 0;
1890 u32 cur;
1891 struct btrfs_key key;
1892
1893 array_size = btrfs_super_sys_array_size(super_copy);
1894
1895 ptr = super_copy->sys_chunk_array;
1896 cur = 0;
1897
1898 while (cur < array_size) {
1899 disk_key = (struct btrfs_disk_key *)ptr;
1900 btrfs_disk_key_to_cpu(&key, disk_key);
1901
1902 len = sizeof(*disk_key);
1903
1904 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1905 chunk = (struct btrfs_chunk *)(ptr + len);
1906 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1907 len += btrfs_chunk_item_size(num_stripes);
1908 } else {
1909 ret = -EIO;
1910 break;
1911 }
1912 if (key.objectid == chunk_objectid &&
1913 key.offset == chunk_offset) {
1914 memmove(ptr, ptr + len, array_size - (cur + len));
1915 array_size -= len;
1916 btrfs_set_super_sys_array_size(super_copy, array_size);
1917 } else {
1918 ptr += len;
1919 cur += len;
1920 }
1921 }
1922 return ret;
1923}
1924
Christoph Hellwigb2950862008-12-02 09:54:17 -05001925static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001926 u64 chunk_tree, u64 chunk_objectid,
1927 u64 chunk_offset)
1928{
1929 struct extent_map_tree *em_tree;
1930 struct btrfs_root *extent_root;
1931 struct btrfs_trans_handle *trans;
1932 struct extent_map *em;
1933 struct map_lookup *map;
1934 int ret;
1935 int i;
1936
1937 root = root->fs_info->chunk_root;
1938 extent_root = root->fs_info->extent_root;
1939 em_tree = &root->fs_info->mapping_tree.map_tree;
1940
Josef Bacikba1bf482009-09-11 16:11:19 -04001941 ret = btrfs_can_relocate(extent_root, chunk_offset);
1942 if (ret)
1943 return -ENOSPC;
1944
Chris Mason8f18cf12008-04-25 16:53:30 -04001945 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001946 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001947 if (ret)
1948 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001949
Yan, Zhenga22285a2010-05-16 10:48:46 -04001950 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001951 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001952
Chris Mason7d9eb122008-07-08 14:19:17 -04001953 lock_chunks(root);
1954
Chris Mason8f18cf12008-04-25 16:53:30 -04001955 /*
1956 * step two, delete the device extents and the
1957 * chunk tree entries
1958 */
Chris Mason890871b2009-09-02 16:24:52 -04001959 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001960 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001961 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001962
Chris Masona061fc82008-05-07 11:43:44 -04001963 BUG_ON(em->start > chunk_offset ||
1964 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001965 map = (struct map_lookup *)em->bdev;
1966
1967 for (i = 0; i < map->num_stripes; i++) {
1968 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1969 map->stripes[i].physical);
1970 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001971
Chris Masondfe25022008-05-13 13:46:40 -04001972 if (map->stripes[i].dev) {
1973 ret = btrfs_update_device(trans, map->stripes[i].dev);
1974 BUG_ON(ret);
1975 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001976 }
1977 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1978 chunk_offset);
1979
1980 BUG_ON(ret);
1981
liubo1abe9b82011-03-24 11:18:59 +00001982 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1983
Chris Mason8f18cf12008-04-25 16:53:30 -04001984 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1985 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1986 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001987 }
1988
Zheng Yan1a40e232008-09-26 10:09:34 -04001989 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1990 BUG_ON(ret);
1991
Chris Mason890871b2009-09-02 16:24:52 -04001992 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001993 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001994 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001995
Chris Mason8f18cf12008-04-25 16:53:30 -04001996 kfree(map);
1997 em->bdev = NULL;
1998
1999 /* once for the tree */
2000 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002001 /* once for us */
2002 free_extent_map(em);
2003
Chris Mason7d9eb122008-07-08 14:19:17 -04002004 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002005 btrfs_end_transaction(trans, root);
2006 return 0;
2007}
2008
Yan Zheng2b820322008-11-17 21:11:30 -05002009static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2010{
2011 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2012 struct btrfs_path *path;
2013 struct extent_buffer *leaf;
2014 struct btrfs_chunk *chunk;
2015 struct btrfs_key key;
2016 struct btrfs_key found_key;
2017 u64 chunk_tree = chunk_root->root_key.objectid;
2018 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002019 bool retried = false;
2020 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002021 int ret;
2022
2023 path = btrfs_alloc_path();
2024 if (!path)
2025 return -ENOMEM;
2026
Josef Bacikba1bf482009-09-11 16:11:19 -04002027again:
Yan Zheng2b820322008-11-17 21:11:30 -05002028 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2029 key.offset = (u64)-1;
2030 key.type = BTRFS_CHUNK_ITEM_KEY;
2031
2032 while (1) {
2033 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2034 if (ret < 0)
2035 goto error;
2036 BUG_ON(ret == 0);
2037
2038 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2039 key.type);
2040 if (ret < 0)
2041 goto error;
2042 if (ret > 0)
2043 break;
2044
2045 leaf = path->nodes[0];
2046 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2047
2048 chunk = btrfs_item_ptr(leaf, path->slots[0],
2049 struct btrfs_chunk);
2050 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002051 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002052
2053 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2054 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2055 found_key.objectid,
2056 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002057 if (ret == -ENOSPC)
2058 failed++;
2059 else if (ret)
2060 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002061 }
2062
2063 if (found_key.offset == 0)
2064 break;
2065 key.offset = found_key.offset - 1;
2066 }
2067 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002068 if (failed && !retried) {
2069 failed = 0;
2070 retried = true;
2071 goto again;
2072 } else if (failed && retried) {
2073 WARN_ON(1);
2074 ret = -ENOSPC;
2075 }
Yan Zheng2b820322008-11-17 21:11:30 -05002076error:
2077 btrfs_free_path(path);
2078 return ret;
2079}
2080
Chris Masonec44a352008-04-28 15:29:52 -04002081static u64 div_factor(u64 num, int factor)
2082{
2083 if (factor == 10)
2084 return num;
2085 num *= factor;
2086 do_div(num, 10);
2087 return num;
2088}
2089
Chris Masonec44a352008-04-28 15:29:52 -04002090int btrfs_balance(struct btrfs_root *dev_root)
2091{
2092 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002093 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2094 struct btrfs_device *device;
2095 u64 old_size;
2096 u64 size_to_free;
2097 struct btrfs_path *path;
2098 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002099 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2100 struct btrfs_trans_handle *trans;
2101 struct btrfs_key found_key;
2102
Yan Zheng2b820322008-11-17 21:11:30 -05002103 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2104 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002105
Ben Hutchings6f88a442010-12-29 14:55:03 +00002106 if (!capable(CAP_SYS_ADMIN))
2107 return -EPERM;
2108
Chris Mason7d9eb122008-07-08 14:19:17 -04002109 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002110 dev_root = dev_root->fs_info->dev_root;
2111
Chris Masonec44a352008-04-28 15:29:52 -04002112 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002113 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002114 old_size = device->total_bytes;
2115 size_to_free = div_factor(old_size, 1);
2116 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002117 if (!device->writeable ||
2118 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002119 continue;
2120
2121 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002122 if (ret == -ENOSPC)
2123 break;
Chris Masonec44a352008-04-28 15:29:52 -04002124 BUG_ON(ret);
2125
Yan, Zhenga22285a2010-05-16 10:48:46 -04002126 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002127 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002128
2129 ret = btrfs_grow_device(trans, device, old_size);
2130 BUG_ON(ret);
2131
2132 btrfs_end_transaction(trans, dev_root);
2133 }
2134
2135 /* step two, relocate all the chunks */
2136 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002137 if (!path) {
2138 ret = -ENOMEM;
2139 goto error;
2140 }
Chris Masonec44a352008-04-28 15:29:52 -04002141 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2142 key.offset = (u64)-1;
2143 key.type = BTRFS_CHUNK_ITEM_KEY;
2144
Chris Masond3977122009-01-05 21:25:51 -05002145 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002146 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2147 if (ret < 0)
2148 goto error;
2149
2150 /*
2151 * this shouldn't happen, it means the last relocate
2152 * failed
2153 */
2154 if (ret == 0)
2155 break;
2156
2157 ret = btrfs_previous_item(chunk_root, path, 0,
2158 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002159 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002160 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002161
Chris Masonec44a352008-04-28 15:29:52 -04002162 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2163 path->slots[0]);
2164 if (found_key.objectid != key.objectid)
2165 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002166
Chris Masonec44a352008-04-28 15:29:52 -04002167 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002168 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002169 break;
2170
David Sterbab3b4aa72011-04-21 01:20:15 +02002171 btrfs_release_path(path);
Chris Masonec44a352008-04-28 15:29:52 -04002172 ret = btrfs_relocate_chunk(chunk_root,
2173 chunk_root->root_key.objectid,
2174 found_key.objectid,
2175 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002176 if (ret && ret != -ENOSPC)
2177 goto error;
Josef Bacikba1bf482009-09-11 16:11:19 -04002178 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002179 }
2180 ret = 0;
2181error:
2182 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002183 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002184 return ret;
2185}
2186
Chris Mason8f18cf12008-04-25 16:53:30 -04002187/*
2188 * shrinking a device means finding all of the device extents past
2189 * the new size, and then following the back refs to the chunks.
2190 * The chunk relocation code actually frees the device extent
2191 */
2192int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2193{
2194 struct btrfs_trans_handle *trans;
2195 struct btrfs_root *root = device->dev_root;
2196 struct btrfs_dev_extent *dev_extent = NULL;
2197 struct btrfs_path *path;
2198 u64 length;
2199 u64 chunk_tree;
2200 u64 chunk_objectid;
2201 u64 chunk_offset;
2202 int ret;
2203 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002204 int failed = 0;
2205 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002206 struct extent_buffer *l;
2207 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002208 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002209 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002210 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002211 u64 diff = device->total_bytes - new_size;
2212
Yan Zheng2b820322008-11-17 21:11:30 -05002213 if (new_size >= device->total_bytes)
2214 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002215
2216 path = btrfs_alloc_path();
2217 if (!path)
2218 return -ENOMEM;
2219
Chris Mason8f18cf12008-04-25 16:53:30 -04002220 path->reada = 2;
2221
Chris Mason7d9eb122008-07-08 14:19:17 -04002222 lock_chunks(root);
2223
Chris Mason8f18cf12008-04-25 16:53:30 -04002224 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002225 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002226 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002227 spin_lock(&root->fs_info->free_chunk_lock);
2228 root->fs_info->free_chunk_space -= diff;
2229 spin_unlock(&root->fs_info->free_chunk_lock);
2230 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002231 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002232
Josef Bacikba1bf482009-09-11 16:11:19 -04002233again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002234 key.objectid = device->devid;
2235 key.offset = (u64)-1;
2236 key.type = BTRFS_DEV_EXTENT_KEY;
2237
2238 while (1) {
2239 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2240 if (ret < 0)
2241 goto done;
2242
2243 ret = btrfs_previous_item(root, path, 0, key.type);
2244 if (ret < 0)
2245 goto done;
2246 if (ret) {
2247 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002248 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002249 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002250 }
2251
2252 l = path->nodes[0];
2253 slot = path->slots[0];
2254 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2255
Josef Bacikba1bf482009-09-11 16:11:19 -04002256 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002257 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002258 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002259 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002260
2261 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2262 length = btrfs_dev_extent_length(l, dev_extent);
2263
Josef Bacikba1bf482009-09-11 16:11:19 -04002264 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002265 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002266 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002267 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002268
2269 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2270 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2271 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002272 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002273
2274 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2275 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002276 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002277 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002278 if (ret == -ENOSPC)
2279 failed++;
2280 key.offset -= 1;
2281 }
2282
2283 if (failed && !retried) {
2284 failed = 0;
2285 retried = true;
2286 goto again;
2287 } else if (failed && retried) {
2288 ret = -ENOSPC;
2289 lock_chunks(root);
2290
2291 device->total_bytes = old_size;
2292 if (device->writeable)
2293 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002294 spin_lock(&root->fs_info->free_chunk_lock);
2295 root->fs_info->free_chunk_space += diff;
2296 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04002297 unlock_chunks(root);
2298 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002299 }
2300
Chris Balld6397ba2009-04-27 07:29:03 -04002301 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002302 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002303 if (IS_ERR(trans)) {
2304 ret = PTR_ERR(trans);
2305 goto done;
2306 }
2307
Chris Balld6397ba2009-04-27 07:29:03 -04002308 lock_chunks(root);
2309
2310 device->disk_total_bytes = new_size;
2311 /* Now btrfs_update_device() will change the on-disk size. */
2312 ret = btrfs_update_device(trans, device);
2313 if (ret) {
2314 unlock_chunks(root);
2315 btrfs_end_transaction(trans, root);
2316 goto done;
2317 }
2318 WARN_ON(diff > old_total);
2319 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2320 unlock_chunks(root);
2321 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002322done:
2323 btrfs_free_path(path);
2324 return ret;
2325}
2326
Christoph Hellwigb2950862008-12-02 09:54:17 -05002327static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002328 struct btrfs_root *root,
2329 struct btrfs_key *key,
2330 struct btrfs_chunk *chunk, int item_size)
2331{
David Sterba6c417612011-04-13 15:41:04 +02002332 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04002333 struct btrfs_disk_key disk_key;
2334 u32 array_size;
2335 u8 *ptr;
2336
2337 array_size = btrfs_super_sys_array_size(super_copy);
2338 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2339 return -EFBIG;
2340
2341 ptr = super_copy->sys_chunk_array + array_size;
2342 btrfs_cpu_key_to_disk(&disk_key, key);
2343 memcpy(ptr, &disk_key, sizeof(disk_key));
2344 ptr += sizeof(disk_key);
2345 memcpy(ptr, chunk, item_size);
2346 item_size += sizeof(disk_key);
2347 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2348 return 0;
2349}
2350
Miao Xieb2117a32011-01-05 10:07:28 +00002351/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002352 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002353 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002354static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002355{
Arne Jansen73c5de02011-04-12 12:07:57 +02002356 const struct btrfs_device_info *di_a = a;
2357 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002358
Arne Jansen73c5de02011-04-12 12:07:57 +02002359 if (di_a->max_avail > di_b->max_avail)
2360 return -1;
2361 if (di_a->max_avail < di_b->max_avail)
2362 return 1;
2363 if (di_a->total_avail > di_b->total_avail)
2364 return -1;
2365 if (di_a->total_avail < di_b->total_avail)
2366 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002367 return 0;
2368}
2369
2370static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2371 struct btrfs_root *extent_root,
2372 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002373 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002374 u64 start, u64 type)
2375{
2376 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002377 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2378 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002379 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002380 struct extent_map_tree *em_tree;
2381 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002382 struct btrfs_device_info *devices_info = NULL;
2383 u64 total_avail;
2384 int num_stripes; /* total number of stripes to allocate */
2385 int sub_stripes; /* sub_stripes info for map */
2386 int dev_stripes; /* stripes per dev */
2387 int devs_max; /* max devs to use */
2388 int devs_min; /* min devs needed */
2389 int devs_increment; /* ndevs has to be a multiple of this */
2390 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002391 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002392 u64 max_stripe_size;
2393 u64 max_chunk_size;
2394 u64 stripe_size;
2395 u64 num_bytes;
2396 int ndevs;
2397 int i;
2398 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002399
2400 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2401 (type & BTRFS_BLOCK_GROUP_DUP)) {
2402 WARN_ON(1);
2403 type &= ~BTRFS_BLOCK_GROUP_DUP;
2404 }
Arne Jansen73c5de02011-04-12 12:07:57 +02002405
Miao Xieb2117a32011-01-05 10:07:28 +00002406 if (list_empty(&fs_devices->alloc_list))
2407 return -ENOSPC;
2408
Arne Jansen73c5de02011-04-12 12:07:57 +02002409 sub_stripes = 1;
2410 dev_stripes = 1;
2411 devs_increment = 1;
2412 ncopies = 1;
2413 devs_max = 0; /* 0 == as many as possible */
2414 devs_min = 1;
2415
2416 /*
2417 * define the properties of each RAID type.
2418 * FIXME: move this to a global table and use it in all RAID
2419 * calculation code
2420 */
2421 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2422 dev_stripes = 2;
2423 ncopies = 2;
2424 devs_max = 1;
2425 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2426 devs_min = 2;
2427 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2428 devs_increment = 2;
2429 ncopies = 2;
2430 devs_max = 2;
2431 devs_min = 2;
2432 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2433 sub_stripes = 2;
2434 devs_increment = 2;
2435 ncopies = 2;
2436 devs_min = 4;
2437 } else {
2438 devs_max = 1;
2439 }
2440
2441 if (type & BTRFS_BLOCK_GROUP_DATA) {
2442 max_stripe_size = 1024 * 1024 * 1024;
2443 max_chunk_size = 10 * max_stripe_size;
2444 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2445 max_stripe_size = 256 * 1024 * 1024;
2446 max_chunk_size = max_stripe_size;
2447 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2448 max_stripe_size = 8 * 1024 * 1024;
2449 max_chunk_size = 2 * max_stripe_size;
2450 } else {
2451 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2452 type);
2453 BUG_ON(1);
2454 }
2455
2456 /* we don't want a chunk larger than 10% of writeable space */
2457 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2458 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00002459
2460 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2461 GFP_NOFS);
2462 if (!devices_info)
2463 return -ENOMEM;
2464
Arne Jansen73c5de02011-04-12 12:07:57 +02002465 cur = fs_devices->alloc_list.next;
2466
2467 /*
2468 * in the first pass through the devices list, we gather information
2469 * about the available holes on each device.
2470 */
2471 ndevs = 0;
2472 while (cur != &fs_devices->alloc_list) {
2473 struct btrfs_device *device;
2474 u64 max_avail;
2475 u64 dev_offset;
2476
2477 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2478
2479 cur = cur->next;
2480
2481 if (!device->writeable) {
2482 printk(KERN_ERR
2483 "btrfs: read-only device in alloc_list\n");
2484 WARN_ON(1);
2485 continue;
2486 }
2487
2488 if (!device->in_fs_metadata)
2489 continue;
2490
2491 if (device->total_bytes > device->bytes_used)
2492 total_avail = device->total_bytes - device->bytes_used;
2493 else
2494 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00002495
2496 /* If there is no space on this device, skip it. */
2497 if (total_avail == 0)
2498 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02002499
2500 ret = find_free_dev_extent(trans, device,
2501 max_stripe_size * dev_stripes,
2502 &dev_offset, &max_avail);
2503 if (ret && ret != -ENOSPC)
2504 goto error;
2505
2506 if (ret == 0)
2507 max_avail = max_stripe_size * dev_stripes;
2508
2509 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2510 continue;
2511
2512 devices_info[ndevs].dev_offset = dev_offset;
2513 devices_info[ndevs].max_avail = max_avail;
2514 devices_info[ndevs].total_avail = total_avail;
2515 devices_info[ndevs].dev = device;
2516 ++ndevs;
2517 }
2518
2519 /*
2520 * now sort the devices by hole size / available space
2521 */
2522 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2523 btrfs_cmp_device_info, NULL);
2524
2525 /* round down to number of usable stripes */
2526 ndevs -= ndevs % devs_increment;
2527
2528 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2529 ret = -ENOSPC;
2530 goto error;
2531 }
2532
2533 if (devs_max && ndevs > devs_max)
2534 ndevs = devs_max;
2535 /*
2536 * the primary goal is to maximize the number of stripes, so use as many
2537 * devices as possible, even if the stripes are not maximum sized.
2538 */
2539 stripe_size = devices_info[ndevs-1].max_avail;
2540 num_stripes = ndevs * dev_stripes;
2541
2542 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2543 stripe_size = max_chunk_size * ncopies;
2544 do_div(stripe_size, num_stripes);
2545 }
2546
2547 do_div(stripe_size, dev_stripes);
2548 do_div(stripe_size, BTRFS_STRIPE_LEN);
2549 stripe_size *= BTRFS_STRIPE_LEN;
2550
Miao Xieb2117a32011-01-05 10:07:28 +00002551 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2552 if (!map) {
2553 ret = -ENOMEM;
2554 goto error;
2555 }
2556 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002557
Arne Jansen73c5de02011-04-12 12:07:57 +02002558 for (i = 0; i < ndevs; ++i) {
2559 for (j = 0; j < dev_stripes; ++j) {
2560 int s = i * dev_stripes + j;
2561 map->stripes[s].dev = devices_info[i].dev;
2562 map->stripes[s].physical = devices_info[i].dev_offset +
2563 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002564 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002565 }
Chris Mason593060d2008-03-25 16:50:33 -04002566 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002567 map->stripe_len = BTRFS_STRIPE_LEN;
2568 map->io_align = BTRFS_STRIPE_LEN;
2569 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002570 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002571 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002572
Yan Zheng2b820322008-11-17 21:11:30 -05002573 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02002574 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04002575
Arne Jansen73c5de02011-04-12 12:07:57 +02002576 *stripe_size_out = stripe_size;
2577 *num_bytes_out = num_bytes;
2578
2579 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00002580
David Sterba172ddd62011-04-21 00:48:27 +02002581 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05002582 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002583 ret = -ENOMEM;
2584 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002585 }
Chris Mason0b86a832008-03-24 15:01:56 -04002586 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002587 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02002588 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002589 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002590 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002591
Chris Mason0b86a832008-03-24 15:01:56 -04002592 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002593 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002594 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002595 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002596 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002597 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002598
2599 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2600 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002601 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05002602 BUG_ON(ret);
2603
Arne Jansen73c5de02011-04-12 12:07:57 +02002604 for (i = 0; i < map->num_stripes; ++i) {
2605 struct btrfs_device *device;
2606 u64 dev_offset;
2607
2608 device = map->stripes[i].dev;
2609 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05002610
2611 ret = btrfs_alloc_dev_extent(trans, device,
2612 info->chunk_root->root_key.objectid,
2613 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002614 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05002615 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002616 }
2617
Miao Xieb2117a32011-01-05 10:07:28 +00002618 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002619 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002620
2621error:
2622 kfree(map);
2623 kfree(devices_info);
2624 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002625}
2626
2627static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2628 struct btrfs_root *extent_root,
2629 struct map_lookup *map, u64 chunk_offset,
2630 u64 chunk_size, u64 stripe_size)
2631{
2632 u64 dev_offset;
2633 struct btrfs_key key;
2634 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2635 struct btrfs_device *device;
2636 struct btrfs_chunk *chunk;
2637 struct btrfs_stripe *stripe;
2638 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2639 int index = 0;
2640 int ret;
2641
2642 chunk = kzalloc(item_size, GFP_NOFS);
2643 if (!chunk)
2644 return -ENOMEM;
2645
2646 index = 0;
2647 while (index < map->num_stripes) {
2648 device = map->stripes[index].dev;
2649 device->bytes_used += stripe_size;
2650 ret = btrfs_update_device(trans, device);
2651 BUG_ON(ret);
2652 index++;
2653 }
2654
Josef Bacik2bf64752011-09-26 17:12:22 -04002655 spin_lock(&extent_root->fs_info->free_chunk_lock);
2656 extent_root->fs_info->free_chunk_space -= (stripe_size *
2657 map->num_stripes);
2658 spin_unlock(&extent_root->fs_info->free_chunk_lock);
2659
Yan Zheng2b820322008-11-17 21:11:30 -05002660 index = 0;
2661 stripe = &chunk->stripe;
2662 while (index < map->num_stripes) {
2663 device = map->stripes[index].dev;
2664 dev_offset = map->stripes[index].physical;
2665
2666 btrfs_set_stack_stripe_devid(stripe, device->devid);
2667 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2668 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2669 stripe++;
2670 index++;
2671 }
2672
2673 btrfs_set_stack_chunk_length(chunk, chunk_size);
2674 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2675 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2676 btrfs_set_stack_chunk_type(chunk, map->type);
2677 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2678 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2679 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2680 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2681 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2682
2683 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2684 key.type = BTRFS_CHUNK_ITEM_KEY;
2685 key.offset = chunk_offset;
2686
2687 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2688 BUG_ON(ret);
2689
2690 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2691 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2692 item_size);
2693 BUG_ON(ret);
2694 }
liubo1abe9b82011-03-24 11:18:59 +00002695
Yan Zheng2b820322008-11-17 21:11:30 -05002696 kfree(chunk);
2697 return 0;
2698}
2699
2700/*
2701 * Chunk allocation falls into two parts. The first part does works
2702 * that make the new allocated chunk useable, but not do any operation
2703 * that modifies the chunk tree. The second part does the works that
2704 * require modifying the chunk tree. This division is important for the
2705 * bootstrap process of adding storage to a seed btrfs.
2706 */
2707int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2708 struct btrfs_root *extent_root, u64 type)
2709{
2710 u64 chunk_offset;
2711 u64 chunk_size;
2712 u64 stripe_size;
2713 struct map_lookup *map;
2714 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2715 int ret;
2716
2717 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2718 &chunk_offset);
2719 if (ret)
2720 return ret;
2721
2722 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2723 &stripe_size, chunk_offset, type);
2724 if (ret)
2725 return ret;
2726
2727 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2728 chunk_size, stripe_size);
2729 BUG_ON(ret);
2730 return 0;
2731}
2732
Chris Masond3977122009-01-05 21:25:51 -05002733static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002734 struct btrfs_root *root,
2735 struct btrfs_device *device)
2736{
2737 u64 chunk_offset;
2738 u64 sys_chunk_offset;
2739 u64 chunk_size;
2740 u64 sys_chunk_size;
2741 u64 stripe_size;
2742 u64 sys_stripe_size;
2743 u64 alloc_profile;
2744 struct map_lookup *map;
2745 struct map_lookup *sys_map;
2746 struct btrfs_fs_info *fs_info = root->fs_info;
2747 struct btrfs_root *extent_root = fs_info->extent_root;
2748 int ret;
2749
2750 ret = find_next_chunk(fs_info->chunk_root,
2751 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07002752 if (ret)
2753 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002754
2755 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2756 (fs_info->metadata_alloc_profile &
2757 fs_info->avail_metadata_alloc_bits);
2758 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2759
2760 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2761 &stripe_size, chunk_offset, alloc_profile);
2762 BUG_ON(ret);
2763
2764 sys_chunk_offset = chunk_offset + chunk_size;
2765
2766 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2767 (fs_info->system_alloc_profile &
2768 fs_info->avail_system_alloc_bits);
2769 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2770
2771 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2772 &sys_chunk_size, &sys_stripe_size,
2773 sys_chunk_offset, alloc_profile);
2774 BUG_ON(ret);
2775
2776 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2777 BUG_ON(ret);
2778
2779 /*
2780 * Modifying chunk tree needs allocating new blocks from both
2781 * system block group and metadata block group. So we only can
2782 * do operations require modifying the chunk tree after both
2783 * block groups were created.
2784 */
2785 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2786 chunk_size, stripe_size);
2787 BUG_ON(ret);
2788
2789 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2790 sys_chunk_offset, sys_chunk_size,
2791 sys_stripe_size);
2792 BUG_ON(ret);
2793 return 0;
2794}
2795
2796int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2797{
2798 struct extent_map *em;
2799 struct map_lookup *map;
2800 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2801 int readonly = 0;
2802 int i;
2803
Chris Mason890871b2009-09-02 16:24:52 -04002804 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002805 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002806 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002807 if (!em)
2808 return 1;
2809
Josef Bacikf48b9072010-01-27 02:07:59 +00002810 if (btrfs_test_opt(root, DEGRADED)) {
2811 free_extent_map(em);
2812 return 0;
2813 }
2814
Yan Zheng2b820322008-11-17 21:11:30 -05002815 map = (struct map_lookup *)em->bdev;
2816 for (i = 0; i < map->num_stripes; i++) {
2817 if (!map->stripes[i].dev->writeable) {
2818 readonly = 1;
2819 break;
2820 }
2821 }
2822 free_extent_map(em);
2823 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002824}
2825
2826void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2827{
David Sterbaa8067e02011-04-21 00:34:43 +02002828 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04002829}
2830
2831void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2832{
2833 struct extent_map *em;
2834
Chris Masond3977122009-01-05 21:25:51 -05002835 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002836 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002837 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2838 if (em)
2839 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002840 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002841 if (!em)
2842 break;
2843 kfree(em->bdev);
2844 /* once for us */
2845 free_extent_map(em);
2846 /* once for the tree */
2847 free_extent_map(em);
2848 }
2849}
2850
Chris Masonf1885912008-04-09 16:28:12 -04002851int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2852{
2853 struct extent_map *em;
2854 struct map_lookup *map;
2855 struct extent_map_tree *em_tree = &map_tree->map_tree;
2856 int ret;
2857
Chris Mason890871b2009-09-02 16:24:52 -04002858 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002859 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002860 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002861 BUG_ON(!em);
2862
2863 BUG_ON(em->start > logical || em->start + em->len < logical);
2864 map = (struct map_lookup *)em->bdev;
2865 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2866 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002867 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2868 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002869 else
2870 ret = 1;
2871 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002872 return ret;
2873}
2874
Chris Masondfe25022008-05-13 13:46:40 -04002875static int find_live_mirror(struct map_lookup *map, int first, int num,
2876 int optimal)
2877{
2878 int i;
2879 if (map->stripes[optimal].dev->bdev)
2880 return optimal;
2881 for (i = first; i < first + num; i++) {
2882 if (map->stripes[i].dev->bdev)
2883 return i;
2884 }
2885 /* we couldn't find one that doesn't fail. Just return something
2886 * and the io error handling code will clean up eventually
2887 */
2888 return optimal;
2889}
2890
Chris Masonf2d8d742008-04-21 10:03:05 -04002891static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2892 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02002893 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002894 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002895{
2896 struct extent_map *em;
2897 struct map_lookup *map;
2898 struct extent_map_tree *em_tree = &map_tree->map_tree;
2899 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002900 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002901 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002902 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002903 u64 stripe_nr_orig;
2904 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002905 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002906 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002907 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002908 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002909 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002910 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02002911 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002912
Jan Schmidta1d3c472011-08-04 17:15:33 +02002913 if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002914 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002915again:
Jan Schmidta1d3c472011-08-04 17:15:33 +02002916 if (bbio_ret) {
2917 bbio = kzalloc(btrfs_bio_size(stripes_allocated),
Chris Masoncea9e442008-04-09 16:28:12 -04002918 GFP_NOFS);
Jan Schmidta1d3c472011-08-04 17:15:33 +02002919 if (!bbio)
Chris Masoncea9e442008-04-09 16:28:12 -04002920 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002921
Jan Schmidta1d3c472011-08-04 17:15:33 +02002922 atomic_set(&bbio->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002923 }
Chris Mason0b86a832008-03-24 15:01:56 -04002924
Chris Mason890871b2009-09-02 16:24:52 -04002925 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002926 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002927 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002928
Chris Mason3b951512008-04-17 11:29:12 -04002929 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002930 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2931 (unsigned long long)logical,
2932 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002933 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002934 }
Chris Mason0b86a832008-03-24 15:01:56 -04002935
2936 BUG_ON(em->start > logical || em->start + em->len < logical);
2937 map = (struct map_lookup *)em->bdev;
2938 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002939
Chris Masonf1885912008-04-09 16:28:12 -04002940 if (mirror_num > map->num_stripes)
2941 mirror_num = 0;
2942
Jan Schmidta1d3c472011-08-04 17:15:33 +02002943 /* if our btrfs_bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002944 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002945 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2946 BTRFS_BLOCK_GROUP_DUP)) {
2947 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002948 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002949 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2950 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002951 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002952 }
2953 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002954 if (rw & REQ_DISCARD) {
2955 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2956 BTRFS_BLOCK_GROUP_RAID1 |
2957 BTRFS_BLOCK_GROUP_DUP |
2958 BTRFS_BLOCK_GROUP_RAID10)) {
2959 stripes_required = map->num_stripes;
2960 }
2961 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02002962 if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002963 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002964 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002965 free_extent_map(em);
Jan Schmidta1d3c472011-08-04 17:15:33 +02002966 kfree(bbio);
Chris Masoncea9e442008-04-09 16:28:12 -04002967 goto again;
2968 }
Chris Mason593060d2008-03-25 16:50:33 -04002969 stripe_nr = offset;
2970 /*
2971 * stripe_nr counts the total number of stripes we have to stride
2972 * to get to this block
2973 */
2974 do_div(stripe_nr, map->stripe_len);
2975
2976 stripe_offset = stripe_nr * map->stripe_len;
2977 BUG_ON(offset < stripe_offset);
2978
2979 /* stripe_offset is the offset of this block in its stripe*/
2980 stripe_offset = offset - stripe_offset;
2981
Li Dongyangfce3bb92011-03-24 10:24:26 +00002982 if (rw & REQ_DISCARD)
2983 *length = min_t(u64, em->len - offset, *length);
2984 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2985 BTRFS_BLOCK_GROUP_RAID1 |
2986 BTRFS_BLOCK_GROUP_RAID10 |
2987 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04002988 /* we limit the length of each bio to what fits in a stripe */
2989 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00002990 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04002991 } else {
2992 *length = em->len - offset;
2993 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002994
Jan Schmidta1d3c472011-08-04 17:15:33 +02002995 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04002996 goto out;
2997
Chris Masonf2d8d742008-04-21 10:03:05 -04002998 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002999 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003000 stripe_nr_orig = stripe_nr;
3001 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3002 (~(map->stripe_len - 1));
3003 do_div(stripe_nr_end, map->stripe_len);
3004 stripe_end_offset = stripe_nr_end * map->stripe_len -
3005 (offset + *length);
3006 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3007 if (rw & REQ_DISCARD)
3008 num_stripes = min_t(u64, map->num_stripes,
3009 stripe_nr_end - stripe_nr_orig);
3010 stripe_index = do_div(stripe_nr, map->num_stripes);
3011 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003012 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003013 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003014 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003015 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003016 else {
3017 stripe_index = find_live_mirror(map, 0,
3018 map->num_stripes,
3019 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003020 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003021 }
Chris Mason2fff7342008-04-29 14:12:09 -04003022
Chris Mason611f0e02008-04-03 16:29:03 -04003023 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003024 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003025 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003026 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003027 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003028 } else {
3029 mirror_num = 1;
3030 }
Chris Mason2fff7342008-04-29 14:12:09 -04003031
Chris Mason321aecc2008-04-16 10:49:51 -04003032 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3033 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003034
3035 stripe_index = do_div(stripe_nr, factor);
3036 stripe_index *= map->sub_stripes;
3037
Jens Axboe7eaceac2011-03-10 08:52:07 +01003038 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003039 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003040 else if (rw & REQ_DISCARD)
3041 num_stripes = min_t(u64, map->sub_stripes *
3042 (stripe_nr_end - stripe_nr_orig),
3043 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003044 else if (mirror_num)
3045 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003046 else {
3047 stripe_index = find_live_mirror(map, stripe_index,
3048 map->sub_stripes, stripe_index +
3049 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003050 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003051 }
Chris Mason8790d502008-04-03 16:29:03 -04003052 } else {
3053 /*
3054 * after this do_div call, stripe_nr is the number of stripes
3055 * on this device we have to walk to find the data, and
3056 * stripe_index is the number of our device in the stripe array
3057 */
3058 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003059 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003060 }
Chris Mason593060d2008-03-25 16:50:33 -04003061 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003062
Li Dongyangfce3bb92011-03-24 10:24:26 +00003063 if (rw & REQ_DISCARD) {
3064 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003065 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003066 map->stripes[stripe_index].physical +
3067 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003068 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003069
3070 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3071 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003072 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003073 int j;
3074
Chris Masond9d04872011-03-27 21:23:21 -04003075 div_u64_rem(stripe_nr_end - 1,
3076 map->num_stripes,
3077 &last_stripe);
3078
Li Dongyangfce3bb92011-03-24 10:24:26 +00003079 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003080 u32 test;
3081
3082 div_u64_rem(stripe_nr_end - 1 - j,
3083 map->num_stripes, &test);
3084 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003085 break;
3086 }
3087 stripes = stripe_nr_end - 1 - j;
3088 do_div(stripes, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003089 bbio->stripes[i].length = map->stripe_len *
Li Dongyangfce3bb92011-03-24 10:24:26 +00003090 (stripes - stripe_nr + 1);
3091
3092 if (i == 0) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003093 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003094 stripe_offset;
3095 stripe_offset = 0;
3096 }
3097 if (stripe_index == last_stripe)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003098 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003099 stripe_end_offset;
3100 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3101 u64 stripes;
3102 int j;
3103 int factor = map->num_stripes /
3104 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003105 u32 last_stripe = 0;
3106
3107 div_u64_rem(stripe_nr_end - 1,
3108 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003109 last_stripe *= map->sub_stripes;
3110
3111 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003112 u32 test;
3113
3114 div_u64_rem(stripe_nr_end - 1 - j,
3115 factor, &test);
3116
3117 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003118 stripe_index / map->sub_stripes)
3119 break;
3120 }
3121 stripes = stripe_nr_end - 1 - j;
3122 do_div(stripes, factor);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003123 bbio->stripes[i].length = map->stripe_len *
Li Dongyangfce3bb92011-03-24 10:24:26 +00003124 (stripes - stripe_nr + 1);
3125
3126 if (i < map->sub_stripes) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003127 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003128 stripe_offset;
3129 if (i == map->sub_stripes - 1)
3130 stripe_offset = 0;
3131 }
3132 if (stripe_index >= last_stripe &&
3133 stripe_index <= (last_stripe +
3134 map->sub_stripes - 1)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003135 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003136 stripe_end_offset;
3137 }
3138 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003139 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003140
3141 stripe_index++;
3142 if (stripe_index == map->num_stripes) {
3143 /* This could only happen for RAID0/10 */
3144 stripe_index = 0;
3145 stripe_nr++;
3146 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003147 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003148 } else {
3149 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003150 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003151 map->stripes[stripe_index].physical +
3152 stripe_offset +
3153 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003154 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003155 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003156 stripe_index++;
3157 }
Chris Mason593060d2008-03-25 16:50:33 -04003158 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003159 if (bbio_ret) {
3160 *bbio_ret = bbio;
3161 bbio->num_stripes = num_stripes;
3162 bbio->max_errors = max_errors;
3163 bbio->mirror_num = mirror_num;
Chris Masonf2d8d742008-04-21 10:03:05 -04003164 }
Chris Masoncea9e442008-04-09 16:28:12 -04003165out:
Chris Mason0b86a832008-03-24 15:01:56 -04003166 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003167 return 0;
3168}
3169
Chris Masonf2d8d742008-04-21 10:03:05 -04003170int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3171 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003172 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003173{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003174 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003175 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003176}
3177
Yan Zhenga512bbf2008-12-08 16:46:26 -05003178int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3179 u64 chunk_start, u64 physical, u64 devid,
3180 u64 **logical, int *naddrs, int *stripe_len)
3181{
3182 struct extent_map_tree *em_tree = &map_tree->map_tree;
3183 struct extent_map *em;
3184 struct map_lookup *map;
3185 u64 *buf;
3186 u64 bytenr;
3187 u64 length;
3188 u64 stripe_nr;
3189 int i, j, nr = 0;
3190
Chris Mason890871b2009-09-02 16:24:52 -04003191 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003192 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003193 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003194
3195 BUG_ON(!em || em->start != chunk_start);
3196 map = (struct map_lookup *)em->bdev;
3197
3198 length = em->len;
3199 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3200 do_div(length, map->num_stripes / map->sub_stripes);
3201 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3202 do_div(length, map->num_stripes);
3203
3204 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3205 BUG_ON(!buf);
3206
3207 for (i = 0; i < map->num_stripes; i++) {
3208 if (devid && map->stripes[i].dev->devid != devid)
3209 continue;
3210 if (map->stripes[i].physical > physical ||
3211 map->stripes[i].physical + length <= physical)
3212 continue;
3213
3214 stripe_nr = physical - map->stripes[i].physical;
3215 do_div(stripe_nr, map->stripe_len);
3216
3217 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3218 stripe_nr = stripe_nr * map->num_stripes + i;
3219 do_div(stripe_nr, map->sub_stripes);
3220 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3221 stripe_nr = stripe_nr * map->num_stripes + i;
3222 }
3223 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003224 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003225 for (j = 0; j < nr; j++) {
3226 if (buf[j] == bytenr)
3227 break;
3228 }
Chris Mason934d3752008-12-08 16:43:10 -05003229 if (j == nr) {
3230 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003231 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003232 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003233 }
3234
Yan Zhenga512bbf2008-12-08 16:46:26 -05003235 *logical = buf;
3236 *naddrs = nr;
3237 *stripe_len = map->stripe_len;
3238
3239 free_extent_map(em);
3240 return 0;
3241}
3242
Jan Schmidta1d3c472011-08-04 17:15:33 +02003243static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003244{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003245 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003246 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003247
Chris Mason8790d502008-04-03 16:29:03 -04003248 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003249 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003250
Jan Schmidta1d3c472011-08-04 17:15:33 +02003251 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003252 is_orig_bio = 1;
3253
Jan Schmidta1d3c472011-08-04 17:15:33 +02003254 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003255 if (!is_orig_bio) {
3256 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003257 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003258 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003259 bio->bi_private = bbio->private;
3260 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02003261 bio->bi_bdev = (struct block_device *)
3262 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04003263 /* only send an error to the higher layers if it is
3264 * beyond the tolerance of the multi-bio
3265 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02003266 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003267 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05003268 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04003269 /*
3270 * this bio is actually up to date, we didn't
3271 * go over the max number of errors
3272 */
3273 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003274 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003275 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003276 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04003277
3278 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003279 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003280 bio_put(bio);
3281 }
Chris Mason8790d502008-04-03 16:29:03 -04003282}
3283
Chris Mason8b712842008-06-11 16:50:36 -04003284struct async_sched {
3285 struct bio *bio;
3286 int rw;
3287 struct btrfs_fs_info *info;
3288 struct btrfs_work work;
3289};
3290
3291/*
3292 * see run_scheduled_bios for a description of why bios are collected for
3293 * async submit.
3294 *
3295 * This will add one bio to the pending list for a device and make sure
3296 * the work struct is scheduled.
3297 */
Chris Masond3977122009-01-05 21:25:51 -05003298static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003299 struct btrfs_device *device,
3300 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003301{
3302 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003303 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003304
3305 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003306 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003307 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01003308 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003309 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003310 return 0;
3311 }
3312
3313 /*
Chris Mason0986fe92008-08-15 15:34:15 -04003314 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003315 * higher layers. Otherwise, the async bio makes it appear we have
3316 * made progress against dirty pages when we've really just put it
3317 * on a queue for later
3318 */
Chris Mason0986fe92008-08-15 15:34:15 -04003319 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003320 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003321 bio->bi_next = NULL;
3322 bio->bi_rw |= rw;
3323
3324 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003325 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003326 pending_bios = &device->pending_sync_bios;
3327 else
3328 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003329
Chris Masonffbd5172009-04-20 15:50:09 -04003330 if (pending_bios->tail)
3331 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003332
Chris Masonffbd5172009-04-20 15:50:09 -04003333 pending_bios->tail = bio;
3334 if (!pending_bios->head)
3335 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003336 if (device->running_pending)
3337 should_queue = 0;
3338
3339 spin_unlock(&device->io_lock);
3340
3341 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003342 btrfs_queue_worker(&root->fs_info->submit_workers,
3343 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003344 return 0;
3345}
3346
Chris Masonf1885912008-04-09 16:28:12 -04003347int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003348 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003349{
3350 struct btrfs_mapping_tree *map_tree;
3351 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003352 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003353 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003354 u64 length = 0;
3355 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04003356 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003357 int dev_nr = 0;
3358 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003359 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003360
Chris Masonf2d8d742008-04-21 10:03:05 -04003361 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003362 map_tree = &root->fs_info->mapping_tree;
3363 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003364
Jan Schmidta1d3c472011-08-04 17:15:33 +02003365 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04003366 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003367 BUG_ON(ret);
3368
Jan Schmidta1d3c472011-08-04 17:15:33 +02003369 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04003370 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003371 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3372 "len %llu\n", (unsigned long long)logical,
3373 (unsigned long long)length,
3374 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003375 BUG();
3376 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003377
3378 bbio->orig_bio = first_bio;
3379 bbio->private = first_bio->bi_private;
3380 bbio->end_io = first_bio->bi_end_io;
3381 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04003382
Chris Masond3977122009-01-05 21:25:51 -05003383 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003384 if (dev_nr < total_devs - 1) {
3385 bio = bio_clone(first_bio, GFP_NOFS);
3386 BUG_ON(!bio);
3387 } else {
3388 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04003389 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003390 bio->bi_private = bbio;
3391 bio->bi_end_io = btrfs_end_bio;
3392 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
3393 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003394 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003395 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
3396 "(%s id %llu), size=%u\n", rw,
3397 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
3398 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04003399 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003400 if (async_submit)
3401 schedule_bio(root, dev, rw, bio);
3402 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01003403 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003404 } else {
3405 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3406 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003407 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003408 }
Chris Mason8790d502008-04-03 16:29:03 -04003409 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003410 }
Chris Mason0b86a832008-03-24 15:01:56 -04003411 return 0;
3412}
3413
Chris Masona4437552008-04-18 10:29:38 -04003414struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003415 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003416{
Yan Zheng2b820322008-11-17 21:11:30 -05003417 struct btrfs_device *device;
3418 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003419
Yan Zheng2b820322008-11-17 21:11:30 -05003420 cur_devices = root->fs_info->fs_devices;
3421 while (cur_devices) {
3422 if (!fsid ||
3423 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3424 device = __find_device(&cur_devices->devices,
3425 devid, uuid);
3426 if (device)
3427 return device;
3428 }
3429 cur_devices = cur_devices->seed;
3430 }
3431 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003432}
3433
Chris Masondfe25022008-05-13 13:46:40 -04003434static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3435 u64 devid, u8 *dev_uuid)
3436{
3437 struct btrfs_device *device;
3438 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3439
3440 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003441 if (!device)
3442 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003443 list_add(&device->dev_list,
3444 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003445 device->dev_root = root->fs_info->dev_root;
3446 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003447 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003448 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003449 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003450 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003451 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003452 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003453 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003454 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3455 return device;
3456}
3457
Chris Mason0b86a832008-03-24 15:01:56 -04003458static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3459 struct extent_buffer *leaf,
3460 struct btrfs_chunk *chunk)
3461{
3462 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3463 struct map_lookup *map;
3464 struct extent_map *em;
3465 u64 logical;
3466 u64 length;
3467 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003468 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003469 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003470 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003471 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003472
Chris Masone17cade2008-04-15 15:41:47 -04003473 logical = key->offset;
3474 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003475
Chris Mason890871b2009-09-02 16:24:52 -04003476 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003477 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003478 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003479
3480 /* already mapped? */
3481 if (em && em->start <= logical && em->start + em->len > logical) {
3482 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003483 return 0;
3484 } else if (em) {
3485 free_extent_map(em);
3486 }
Chris Mason0b86a832008-03-24 15:01:56 -04003487
David Sterba172ddd62011-04-21 00:48:27 +02003488 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04003489 if (!em)
3490 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003491 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3492 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003493 if (!map) {
3494 free_extent_map(em);
3495 return -ENOMEM;
3496 }
3497
3498 em->bdev = (struct block_device *)map;
3499 em->start = logical;
3500 em->len = length;
3501 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003502 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003503
Chris Mason593060d2008-03-25 16:50:33 -04003504 map->num_stripes = num_stripes;
3505 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3506 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3507 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3508 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3509 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003510 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003511 for (i = 0; i < num_stripes; i++) {
3512 map->stripes[i].physical =
3513 btrfs_stripe_offset_nr(leaf, chunk, i);
3514 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003515 read_extent_buffer(leaf, uuid, (unsigned long)
3516 btrfs_stripe_dev_uuid_nr(chunk, i),
3517 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003518 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3519 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003520 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003521 kfree(map);
3522 free_extent_map(em);
3523 return -EIO;
3524 }
Chris Masondfe25022008-05-13 13:46:40 -04003525 if (!map->stripes[i].dev) {
3526 map->stripes[i].dev =
3527 add_missing_dev(root, devid, uuid);
3528 if (!map->stripes[i].dev) {
3529 kfree(map);
3530 free_extent_map(em);
3531 return -EIO;
3532 }
3533 }
3534 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003535 }
3536
Chris Mason890871b2009-09-02 16:24:52 -04003537 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003538 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003539 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003540 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003541 free_extent_map(em);
3542
3543 return 0;
3544}
3545
3546static int fill_device_from_item(struct extent_buffer *leaf,
3547 struct btrfs_dev_item *dev_item,
3548 struct btrfs_device *device)
3549{
3550 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003551
3552 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003553 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3554 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003555 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3556 device->type = btrfs_device_type(leaf, dev_item);
3557 device->io_align = btrfs_device_io_align(leaf, dev_item);
3558 device->io_width = btrfs_device_io_width(leaf, dev_item);
3559 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003560
3561 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003562 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003563
Chris Mason0b86a832008-03-24 15:01:56 -04003564 return 0;
3565}
3566
Yan Zheng2b820322008-11-17 21:11:30 -05003567static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3568{
3569 struct btrfs_fs_devices *fs_devices;
3570 int ret;
3571
3572 mutex_lock(&uuid_mutex);
3573
3574 fs_devices = root->fs_info->fs_devices->seed;
3575 while (fs_devices) {
3576 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3577 ret = 0;
3578 goto out;
3579 }
3580 fs_devices = fs_devices->seed;
3581 }
3582
3583 fs_devices = find_fsid(fsid);
3584 if (!fs_devices) {
3585 ret = -ENOENT;
3586 goto out;
3587 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003588
3589 fs_devices = clone_fs_devices(fs_devices);
3590 if (IS_ERR(fs_devices)) {
3591 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003592 goto out;
3593 }
3594
Christoph Hellwig97288f22008-12-02 06:36:09 -05003595 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003596 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003597 if (ret)
3598 goto out;
3599
3600 if (!fs_devices->seeding) {
3601 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003602 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003603 ret = -EINVAL;
3604 goto out;
3605 }
3606
3607 fs_devices->seed = root->fs_info->fs_devices->seed;
3608 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003609out:
3610 mutex_unlock(&uuid_mutex);
3611 return ret;
3612}
3613
Chris Mason0d81ba52008-03-24 15:02:07 -04003614static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003615 struct extent_buffer *leaf,
3616 struct btrfs_dev_item *dev_item)
3617{
3618 struct btrfs_device *device;
3619 u64 devid;
3620 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003621 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003622 u8 dev_uuid[BTRFS_UUID_SIZE];
3623
Chris Mason0b86a832008-03-24 15:01:56 -04003624 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003625 read_extent_buffer(leaf, dev_uuid,
3626 (unsigned long)btrfs_device_uuid(dev_item),
3627 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003628 read_extent_buffer(leaf, fs_uuid,
3629 (unsigned long)btrfs_device_fsid(dev_item),
3630 BTRFS_UUID_SIZE);
3631
3632 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3633 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003634 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003635 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003636 }
3637
3638 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3639 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003640 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003641 return -EIO;
3642
3643 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003644 printk(KERN_WARNING "warning devid %llu missing\n",
3645 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003646 device = add_missing_dev(root, devid, dev_uuid);
3647 if (!device)
3648 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003649 } else if (!device->missing) {
3650 /*
3651 * this happens when a device that was properly setup
3652 * in the device info lists suddenly goes bad.
3653 * device->bdev is NULL, and so we have to set
3654 * device->missing to one here
3655 */
3656 root->fs_info->fs_devices->missing_devices++;
3657 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003658 }
3659 }
3660
3661 if (device->fs_devices != root->fs_info->fs_devices) {
3662 BUG_ON(device->writeable);
3663 if (device->generation !=
3664 btrfs_device_generation(leaf, dev_item))
3665 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003666 }
Chris Mason0b86a832008-03-24 15:01:56 -04003667
3668 fill_device_from_item(leaf, dev_item, device);
3669 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003670 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04003671 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003672 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04003673 spin_lock(&root->fs_info->free_chunk_lock);
3674 root->fs_info->free_chunk_space += device->total_bytes -
3675 device->bytes_used;
3676 spin_unlock(&root->fs_info->free_chunk_lock);
3677 }
Chris Mason0b86a832008-03-24 15:01:56 -04003678 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003679 return ret;
3680}
3681
Yan Zhenge4404d62008-12-12 10:03:26 -05003682int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003683{
David Sterba6c417612011-04-13 15:41:04 +02003684 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003685 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003686 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003687 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003688 u8 *ptr;
3689 unsigned long sb_ptr;
3690 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003691 u32 num_stripes;
3692 u32 array_size;
3693 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003694 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003695 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003696
Yan Zhenge4404d62008-12-12 10:03:26 -05003697 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003698 BTRFS_SUPER_INFO_SIZE);
3699 if (!sb)
3700 return -ENOMEM;
3701 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04003702 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
Chris Mason4008c042009-02-12 14:09:45 -05003703
Chris Masona061fc82008-05-07 11:43:44 -04003704 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003705 array_size = btrfs_super_sys_array_size(super_copy);
3706
Chris Mason0b86a832008-03-24 15:01:56 -04003707 ptr = super_copy->sys_chunk_array;
3708 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3709 cur = 0;
3710
3711 while (cur < array_size) {
3712 disk_key = (struct btrfs_disk_key *)ptr;
3713 btrfs_disk_key_to_cpu(&key, disk_key);
3714
Chris Masona061fc82008-05-07 11:43:44 -04003715 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003716 sb_ptr += len;
3717 cur += len;
3718
Chris Mason0d81ba52008-03-24 15:02:07 -04003719 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003720 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003721 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003722 if (ret)
3723 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003724 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3725 len = btrfs_chunk_item_size(num_stripes);
3726 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003727 ret = -EIO;
3728 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003729 }
3730 ptr += len;
3731 sb_ptr += len;
3732 cur += len;
3733 }
Chris Masona061fc82008-05-07 11:43:44 -04003734 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003735 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003736}
3737
3738int btrfs_read_chunk_tree(struct btrfs_root *root)
3739{
3740 struct btrfs_path *path;
3741 struct extent_buffer *leaf;
3742 struct btrfs_key key;
3743 struct btrfs_key found_key;
3744 int ret;
3745 int slot;
3746
3747 root = root->fs_info->chunk_root;
3748
3749 path = btrfs_alloc_path();
3750 if (!path)
3751 return -ENOMEM;
3752
3753 /* first we search for all of the device items, and then we
3754 * read in all of the chunk items. This way we can create chunk
3755 * mappings that reference all of the devices that are afound
3756 */
3757 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3758 key.offset = 0;
3759 key.type = 0;
3760again:
3761 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003762 if (ret < 0)
3763 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003764 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003765 leaf = path->nodes[0];
3766 slot = path->slots[0];
3767 if (slot >= btrfs_header_nritems(leaf)) {
3768 ret = btrfs_next_leaf(root, path);
3769 if (ret == 0)
3770 continue;
3771 if (ret < 0)
3772 goto error;
3773 break;
3774 }
3775 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3776 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3777 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3778 break;
3779 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3780 struct btrfs_dev_item *dev_item;
3781 dev_item = btrfs_item_ptr(leaf, slot,
3782 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003783 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003784 if (ret)
3785 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003786 }
3787 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3788 struct btrfs_chunk *chunk;
3789 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3790 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003791 if (ret)
3792 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003793 }
3794 path->slots[0]++;
3795 }
3796 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3797 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003798 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003799 goto again;
3800 }
Chris Mason0b86a832008-03-24 15:01:56 -04003801 ret = 0;
3802error:
Yan Zheng2b820322008-11-17 21:11:30 -05003803 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003804 return ret;
3805}