blob: 68a1754fe3674c933501966e50598c473d772443 [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>
Ilya Dryomov59641012012-01-16 22:04:48 +020026#include <linux/kthread.h>
Chris Mason593060d2008-03-25 16:50:33 -040027#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050028#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040029#include "ctree.h"
30#include "extent_map.h"
31#include "disk-io.h"
32#include "transaction.h"
33#include "print-tree.h"
34#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040035#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010036#include "check-integrity.h"
Chris Mason0b86a832008-03-24 15:01:56 -040037
Yan Zheng2b820322008-11-17 21:11:30 -050038static int init_first_rw_device(struct btrfs_trans_handle *trans,
39 struct btrfs_root *root,
40 struct btrfs_device *device);
41static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
42
Chris Mason8a4b83c2008-03-24 15:02:07 -040043static DEFINE_MUTEX(uuid_mutex);
44static LIST_HEAD(fs_uuids);
45
Chris Mason7d9eb122008-07-08 14:19:17 -040046static void lock_chunks(struct btrfs_root *root)
47{
Chris Mason7d9eb122008-07-08 14:19:17 -040048 mutex_lock(&root->fs_info->chunk_mutex);
49}
50
51static void unlock_chunks(struct btrfs_root *root)
52{
Chris Mason7d9eb122008-07-08 14:19:17 -040053 mutex_unlock(&root->fs_info->chunk_mutex);
54}
55
Yan Zhenge4404d62008-12-12 10:03:26 -050056static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
57{
58 struct btrfs_device *device;
59 WARN_ON(fs_devices->opened);
60 while (!list_empty(&fs_devices->devices)) {
61 device = list_entry(fs_devices->devices.next,
62 struct btrfs_device, dev_list);
63 list_del(&device->dev_list);
64 kfree(device->name);
65 kfree(device);
66 }
67 kfree(fs_devices);
68}
69
Jeff Mahoney143bede2012-03-01 14:56:26 +010070void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040071{
72 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040073
Yan Zheng2b820322008-11-17 21:11:30 -050074 while (!list_empty(&fs_uuids)) {
75 fs_devices = list_entry(fs_uuids.next,
76 struct btrfs_fs_devices, list);
77 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050078 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040079 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040080}
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 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100132static noinline void 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}
319
Christoph Hellwigb2950862008-12-02 09:54:17 -0500320static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400321{
322 struct btrfs_device *device;
323
324 device = container_of(work, struct btrfs_device, work);
325 run_scheduled_bios(device);
326}
327
Chris Masona1b32a52008-09-05 16:09:51 -0400328static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400329 struct btrfs_super_block *disk_super,
330 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
331{
332 struct btrfs_device *device;
333 struct btrfs_fs_devices *fs_devices;
334 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000335 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400336
337 fs_devices = find_fsid(disk_super->fsid);
338 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400339 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400340 if (!fs_devices)
341 return -ENOMEM;
342 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400343 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400344 list_add(&fs_devices->list, &fs_uuids);
345 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
346 fs_devices->latest_devid = devid;
347 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400348 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400349 device = NULL;
350 } else {
Chris Masona4437552008-04-18 10:29:38 -0400351 device = __find_device(&fs_devices->devices, devid,
352 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400353 }
354 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500355 if (fs_devices->opened)
356 return -EBUSY;
357
Chris Mason8a4b83c2008-03-24 15:02:07 -0400358 device = kzalloc(sizeof(*device), GFP_NOFS);
359 if (!device) {
360 /* we can safely leave the fs_devices entry around */
361 return -ENOMEM;
362 }
363 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400364 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400365 memcpy(device->uuid, disk_super->dev_item.uuid,
366 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400367 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400368 device->name = kstrdup(path, GFP_NOFS);
369 if (!device->name) {
370 kfree(device);
371 return -ENOMEM;
372 }
Yan Zheng2b820322008-11-17 21:11:30 -0500373 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400374
Arne Jansen90519d62011-05-23 14:30:00 +0200375 /* init readahead state */
376 spin_lock_init(&device->reada_lock);
377 device->reada_curr_zone = NULL;
378 atomic_set(&device->reada_in_flight, 0);
379 device->reada_next = 0;
380 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
381 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
382
Chris Masone5e9a522009-06-10 15:17:02 -0400383 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000384 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400385 mutex_unlock(&fs_devices->device_list_mutex);
386
Yan Zheng2b820322008-11-17 21:11:30 -0500387 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400388 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500389 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000390 name = kstrdup(path, GFP_NOFS);
391 if (!name)
392 return -ENOMEM;
393 kfree(device->name);
394 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500395 if (device->missing) {
396 fs_devices->missing_devices--;
397 device->missing = 0;
398 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400399 }
400
401 if (found_transid > fs_devices->latest_trans) {
402 fs_devices->latest_devid = devid;
403 fs_devices->latest_trans = found_transid;
404 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400405 *fs_devices_ret = fs_devices;
406 return 0;
407}
408
Yan Zhenge4404d62008-12-12 10:03:26 -0500409static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
410{
411 struct btrfs_fs_devices *fs_devices;
412 struct btrfs_device *device;
413 struct btrfs_device *orig_dev;
414
415 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
416 if (!fs_devices)
417 return ERR_PTR(-ENOMEM);
418
419 INIT_LIST_HEAD(&fs_devices->devices);
420 INIT_LIST_HEAD(&fs_devices->alloc_list);
421 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400422 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500423 fs_devices->latest_devid = orig->latest_devid;
424 fs_devices->latest_trans = orig->latest_trans;
425 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
426
Xiao Guangrong46224702011-04-20 10:08:47 +0000427 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500428 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
429 device = kzalloc(sizeof(*device), GFP_NOFS);
430 if (!device)
431 goto error;
432
433 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400434 if (!device->name) {
435 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500436 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400437 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500438
439 device->devid = orig_dev->devid;
440 device->work.func = pending_bios_fn;
441 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500442 spin_lock_init(&device->io_lock);
443 INIT_LIST_HEAD(&device->dev_list);
444 INIT_LIST_HEAD(&device->dev_alloc_list);
445
446 list_add(&device->dev_list, &fs_devices->devices);
447 device->fs_devices = fs_devices;
448 fs_devices->num_devices++;
449 }
450 return fs_devices;
451error:
452 free_fs_devices(fs_devices);
453 return ERR_PTR(-ENOMEM);
454}
455
Jeff Mahoney143bede2012-03-01 14:56:26 +0100456void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
Chris Masondfe25022008-05-13 13:46:40 -0400457{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500458 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400459
Chris Masona6b0d5c2012-02-20 20:53:43 -0500460 struct block_device *latest_bdev = NULL;
461 u64 latest_devid = 0;
462 u64 latest_transid = 0;
463
Chris Masondfe25022008-05-13 13:46:40 -0400464 mutex_lock(&uuid_mutex);
465again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000466 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500467 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500468 if (device->in_fs_metadata) {
469 if (!latest_transid ||
470 device->generation > latest_transid) {
471 latest_devid = device->devid;
472 latest_transid = device->generation;
473 latest_bdev = device->bdev;
474 }
Yan Zheng2b820322008-11-17 21:11:30 -0500475 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500476 }
Yan Zheng2b820322008-11-17 21:11:30 -0500477
478 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100479 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500480 device->bdev = NULL;
481 fs_devices->open_devices--;
482 }
483 if (device->writeable) {
484 list_del_init(&device->dev_alloc_list);
485 device->writeable = 0;
486 fs_devices->rw_devices--;
487 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500488 list_del_init(&device->dev_list);
489 fs_devices->num_devices--;
490 kfree(device->name);
491 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400492 }
Yan Zheng2b820322008-11-17 21:11:30 -0500493
494 if (fs_devices->seed) {
495 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500496 goto again;
497 }
498
Chris Masona6b0d5c2012-02-20 20:53:43 -0500499 fs_devices->latest_bdev = latest_bdev;
500 fs_devices->latest_devid = latest_devid;
501 fs_devices->latest_trans = latest_transid;
502
Chris Masondfe25022008-05-13 13:46:40 -0400503 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400504}
Chris Masona0af4692008-05-13 16:03:06 -0400505
Xiao Guangrong1f781602011-04-20 10:09:16 +0000506static void __free_device(struct work_struct *work)
507{
508 struct btrfs_device *device;
509
510 device = container_of(work, struct btrfs_device, rcu_work);
511
512 if (device->bdev)
513 blkdev_put(device->bdev, device->mode);
514
515 kfree(device->name);
516 kfree(device);
517}
518
519static void free_device(struct rcu_head *head)
520{
521 struct btrfs_device *device;
522
523 device = container_of(head, struct btrfs_device, rcu);
524
525 INIT_WORK(&device->rcu_work, __free_device);
526 schedule_work(&device->rcu_work);
527}
528
Yan Zheng2b820322008-11-17 21:11:30 -0500529static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400530{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400531 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500532
Yan Zheng2b820322008-11-17 21:11:30 -0500533 if (--fs_devices->opened > 0)
534 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000536 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500537 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000538 struct btrfs_device *new_device;
539
540 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400541 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000542
Yan Zheng2b820322008-11-17 21:11:30 -0500543 if (device->writeable) {
544 list_del_init(&device->dev_alloc_list);
545 fs_devices->rw_devices--;
546 }
547
Josef Bacikd5e20032011-08-04 14:52:27 +0000548 if (device->can_discard)
549 fs_devices->num_can_discard--;
550
Xiao Guangrong1f781602011-04-20 10:09:16 +0000551 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100552 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000553 memcpy(new_device, device, sizeof(*new_device));
554 new_device->name = kstrdup(device->name, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100555 BUG_ON(device->name && !new_device->name); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000556 new_device->bdev = NULL;
557 new_device->writeable = 0;
558 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000559 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000560 list_replace_rcu(&device->dev_list, &new_device->dev_list);
561
562 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400563 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000564 mutex_unlock(&fs_devices->device_list_mutex);
565
Yan Zhenge4404d62008-12-12 10:03:26 -0500566 WARN_ON(fs_devices->open_devices);
567 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500568 fs_devices->opened = 0;
569 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500570
Chris Mason8a4b83c2008-03-24 15:02:07 -0400571 return 0;
572}
573
Yan Zheng2b820322008-11-17 21:11:30 -0500574int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
575{
Yan Zhenge4404d62008-12-12 10:03:26 -0500576 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500577 int ret;
578
579 mutex_lock(&uuid_mutex);
580 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500581 if (!fs_devices->opened) {
582 seed_devices = fs_devices->seed;
583 fs_devices->seed = NULL;
584 }
Yan Zheng2b820322008-11-17 21:11:30 -0500585 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500586
587 while (seed_devices) {
588 fs_devices = seed_devices;
589 seed_devices = fs_devices->seed;
590 __btrfs_close_devices(fs_devices);
591 free_fs_devices(fs_devices);
592 }
Yan Zheng2b820322008-11-17 21:11:30 -0500593 return ret;
594}
595
Yan Zhenge4404d62008-12-12 10:03:26 -0500596static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
597 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400598{
Josef Bacikd5e20032011-08-04 14:52:27 +0000599 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400600 struct block_device *bdev;
601 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400602 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400603 struct block_device *latest_bdev = NULL;
604 struct buffer_head *bh;
605 struct btrfs_super_block *disk_super;
606 u64 latest_devid = 0;
607 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400608 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500609 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400610 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400611
Tejun Heod4d77622010-11-13 11:55:18 +0100612 flags |= FMODE_EXCL;
613
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500614 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400615 if (device->bdev)
616 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400617 if (!device->name)
618 continue;
619
Tejun Heod4d77622010-11-13 11:55:18 +0100620 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400621 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500622 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400623 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400624 }
Chris Masona061fc82008-05-07 11:43:44 -0400625 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400626
Yan Zhenga512bbf2008-12-08 16:46:26 -0500627 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300628 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400629 goto error_close;
630
631 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000632 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400633 if (devid != device->devid)
634 goto error_brelse;
635
Yan Zheng2b820322008-11-17 21:11:30 -0500636 if (memcmp(device->uuid, disk_super->dev_item.uuid,
637 BTRFS_UUID_SIZE))
638 goto error_brelse;
639
640 device->generation = btrfs_super_generation(disk_super);
641 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400642 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500643 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400644 latest_bdev = bdev;
645 }
646
Yan Zheng2b820322008-11-17 21:11:30 -0500647 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
648 device->writeable = 0;
649 } else {
650 device->writeable = !bdev_read_only(bdev);
651 seeding = 0;
652 }
653
Josef Bacikd5e20032011-08-04 14:52:27 +0000654 q = bdev_get_queue(bdev);
655 if (blk_queue_discard(q)) {
656 device->can_discard = 1;
657 fs_devices->num_can_discard++;
658 }
659
Chris Mason8a4b83c2008-03-24 15:02:07 -0400660 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400661 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500662 device->mode = flags;
663
Chris Masonc2898112009-06-10 09:51:32 -0400664 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
665 fs_devices->rotating = 1;
666
Chris Masona0af4692008-05-13 16:03:06 -0400667 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500668 if (device->writeable) {
669 fs_devices->rw_devices++;
670 list_add(&device->dev_alloc_list,
671 &fs_devices->alloc_list);
672 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000673 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400674 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400675
Chris Masona0af4692008-05-13 16:03:06 -0400676error_brelse:
677 brelse(bh);
678error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100679 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400680error:
681 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400682 }
Chris Masona0af4692008-05-13 16:03:06 -0400683 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300684 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400685 goto out;
686 }
Yan Zheng2b820322008-11-17 21:11:30 -0500687 fs_devices->seeding = seeding;
688 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400689 fs_devices->latest_bdev = latest_bdev;
690 fs_devices->latest_devid = latest_devid;
691 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500692 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400693out:
Yan Zheng2b820322008-11-17 21:11:30 -0500694 return ret;
695}
696
697int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500698 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500699{
700 int ret;
701
702 mutex_lock(&uuid_mutex);
703 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500704 fs_devices->opened++;
705 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500706 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500707 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500708 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400709 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400710 return ret;
711}
712
Christoph Hellwig97288f22008-12-02 06:36:09 -0500713int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400714 struct btrfs_fs_devices **fs_devices_ret)
715{
716 struct btrfs_super_block *disk_super;
717 struct block_device *bdev;
718 struct buffer_head *bh;
719 int ret;
720 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400721 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400722
Tejun Heod4d77622010-11-13 11:55:18 +0100723 flags |= FMODE_EXCL;
724 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400725
726 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400727 ret = PTR_ERR(bdev);
728 goto error;
729 }
730
Al Viro10f63272011-11-17 15:05:22 -0500731 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400732 ret = set_blocksize(bdev, 4096);
733 if (ret)
734 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500735 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400736 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000737 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400738 goto error_close;
739 }
740 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000741 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400742 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400743 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500744 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200745 else
746 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500747 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500748 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400749 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
750
Chris Mason8a4b83c2008-03-24 15:02:07 -0400751 brelse(bh);
752error_close:
Al Viro10f63272011-11-17 15:05:22 -0500753 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100754 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400755error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400756 return ret;
757}
Chris Mason0b86a832008-03-24 15:01:56 -0400758
Miao Xie6d07bce2011-01-05 10:07:31 +0000759/* helper to account the used device space in the range */
760int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
761 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400762{
763 struct btrfs_key key;
764 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000765 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500766 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000767 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400768 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000769 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400770 struct extent_buffer *l;
771
Miao Xie6d07bce2011-01-05 10:07:31 +0000772 *length = 0;
773
774 if (start >= device->total_bytes)
775 return 0;
776
Yan Zheng2b820322008-11-17 21:11:30 -0500777 path = btrfs_alloc_path();
778 if (!path)
779 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400780 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400781
Chris Mason0b86a832008-03-24 15:01:56 -0400782 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000783 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400784 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000785
786 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400787 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000788 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400789 if (ret > 0) {
790 ret = btrfs_previous_item(root, path, key.objectid, key.type);
791 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000792 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400793 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000794
Chris Mason0b86a832008-03-24 15:01:56 -0400795 while (1) {
796 l = path->nodes[0];
797 slot = path->slots[0];
798 if (slot >= btrfs_header_nritems(l)) {
799 ret = btrfs_next_leaf(root, path);
800 if (ret == 0)
801 continue;
802 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000803 goto out;
804
805 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400806 }
807 btrfs_item_key_to_cpu(l, &key, slot);
808
809 if (key.objectid < device->devid)
810 goto next;
811
812 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000813 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400814
Chris Masond3977122009-01-05 21:25:51 -0500815 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400816 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400817
Chris Mason0b86a832008-03-24 15:01:56 -0400818 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000819 extent_end = key.offset + btrfs_dev_extent_length(l,
820 dev_extent);
821 if (key.offset <= start && extent_end > end) {
822 *length = end - start + 1;
823 break;
824 } else if (key.offset <= start && extent_end > start)
825 *length += extent_end - start;
826 else if (key.offset > start && extent_end <= end)
827 *length += extent_end - key.offset;
828 else if (key.offset > start && key.offset <= end) {
829 *length += end - key.offset + 1;
830 break;
831 } else if (key.offset > end)
832 break;
833
834next:
835 path->slots[0]++;
836 }
837 ret = 0;
838out:
839 btrfs_free_path(path);
840 return ret;
841}
842
Chris Mason0b86a832008-03-24 15:01:56 -0400843/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000844 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000845 * @device: the device which we search the free space in
846 * @num_bytes: the size of the free space that we need
847 * @start: store the start of the free space.
848 * @len: the size of the free space. that we find, or the size of the max
849 * free space if we don't find suitable free space
850 *
Chris Mason0b86a832008-03-24 15:01:56 -0400851 * this uses a pretty simple search, the expectation is that it is
852 * called very infrequently and that a given device has a small number
853 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000854 *
855 * @start is used to store the start of the free space if we find. But if we
856 * don't find suitable free space, it will be used to store the start position
857 * of the max free space.
858 *
859 * @len is used to store the size of the free space that we find.
860 * But if we don't find suitable free space, it is used to store the size of
861 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400862 */
Li Zefan125ccb02011-12-08 15:07:24 +0800863int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000864 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400865{
866 struct btrfs_key key;
867 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000868 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400869 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000870 u64 hole_size;
871 u64 max_hole_start;
872 u64 max_hole_size;
873 u64 extent_end;
874 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400875 u64 search_end = device->total_bytes;
876 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000877 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400878 struct extent_buffer *l;
879
Chris Mason0b86a832008-03-24 15:01:56 -0400880 /* FIXME use last free of some kind */
881
882 /* we don't want to overwrite the superblock on the drive,
883 * so we make sure to start at an offset of at least 1MB
884 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200885 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400886
Miao Xie7bfc8372011-01-05 10:07:26 +0000887 max_hole_start = search_start;
888 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000889 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000890
891 if (search_start >= search_end) {
892 ret = -ENOSPC;
893 goto error;
894 }
895
896 path = btrfs_alloc_path();
897 if (!path) {
898 ret = -ENOMEM;
899 goto error;
900 }
901 path->reada = 2;
902
Chris Mason0b86a832008-03-24 15:01:56 -0400903 key.objectid = device->devid;
904 key.offset = search_start;
905 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000906
Li Zefan125ccb02011-12-08 15:07:24 +0800907 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400908 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000909 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400910 if (ret > 0) {
911 ret = btrfs_previous_item(root, path, key.objectid, key.type);
912 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000913 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400914 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000915
Chris Mason0b86a832008-03-24 15:01:56 -0400916 while (1) {
917 l = path->nodes[0];
918 slot = path->slots[0];
919 if (slot >= btrfs_header_nritems(l)) {
920 ret = btrfs_next_leaf(root, path);
921 if (ret == 0)
922 continue;
923 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000924 goto out;
925
926 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400927 }
928 btrfs_item_key_to_cpu(l, &key, slot);
929
930 if (key.objectid < device->devid)
931 goto next;
932
933 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000934 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400935
Chris Mason0b86a832008-03-24 15:01:56 -0400936 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
937 goto next;
938
Miao Xie7bfc8372011-01-05 10:07:26 +0000939 if (key.offset > search_start) {
940 hole_size = key.offset - search_start;
941
942 if (hole_size > max_hole_size) {
943 max_hole_start = search_start;
944 max_hole_size = hole_size;
945 }
946
947 /*
948 * If this free space is greater than which we need,
949 * it must be the max free space that we have found
950 * until now, so max_hole_start must point to the start
951 * of this free space and the length of this free space
952 * is stored in max_hole_size. Thus, we return
953 * max_hole_start and max_hole_size and go back to the
954 * caller.
955 */
956 if (hole_size >= num_bytes) {
957 ret = 0;
958 goto out;
959 }
960 }
961
Chris Mason0b86a832008-03-24 15:01:56 -0400962 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000963 extent_end = key.offset + btrfs_dev_extent_length(l,
964 dev_extent);
965 if (extent_end > search_start)
966 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400967next:
968 path->slots[0]++;
969 cond_resched();
970 }
Chris Mason0b86a832008-03-24 15:01:56 -0400971
liubo38c01b92011-08-02 02:39:03 +0000972 /*
973 * At this point, search_start should be the end of
974 * allocated dev extents, and when shrinking the device,
975 * search_end may be smaller than search_start.
976 */
977 if (search_end > search_start)
978 hole_size = search_end - search_start;
979
Miao Xie7bfc8372011-01-05 10:07:26 +0000980 if (hole_size > max_hole_size) {
981 max_hole_start = search_start;
982 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400983 }
Chris Mason0b86a832008-03-24 15:01:56 -0400984
Miao Xie7bfc8372011-01-05 10:07:26 +0000985 /* See above. */
986 if (hole_size < num_bytes)
987 ret = -ENOSPC;
988 else
989 ret = 0;
990
991out:
Yan Zheng2b820322008-11-17 21:11:30 -0500992 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000993error:
994 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000995 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000996 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400997 return ret;
998}
999
Christoph Hellwigb2950862008-12-02 09:54:17 -05001000static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001001 struct btrfs_device *device,
1002 u64 start)
1003{
1004 int ret;
1005 struct btrfs_path *path;
1006 struct btrfs_root *root = device->dev_root;
1007 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001008 struct btrfs_key found_key;
1009 struct extent_buffer *leaf = NULL;
1010 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001011
1012 path = btrfs_alloc_path();
1013 if (!path)
1014 return -ENOMEM;
1015
1016 key.objectid = device->devid;
1017 key.offset = start;
1018 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001019again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001020 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001021 if (ret > 0) {
1022 ret = btrfs_previous_item(root, path, key.objectid,
1023 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001024 if (ret)
1025 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001026 leaf = path->nodes[0];
1027 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1028 extent = btrfs_item_ptr(leaf, path->slots[0],
1029 struct btrfs_dev_extent);
1030 BUG_ON(found_key.offset > start || found_key.offset +
1031 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001032 key = found_key;
1033 btrfs_release_path(path);
1034 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001035 } else if (ret == 0) {
1036 leaf = path->nodes[0];
1037 extent = btrfs_item_ptr(leaf, path->slots[0],
1038 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001039 } else {
1040 btrfs_error(root->fs_info, ret, "Slot search failed");
1041 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001042 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001043
Josef Bacik2bf64752011-09-26 17:12:22 -04001044 if (device->bytes_used > 0) {
1045 u64 len = btrfs_dev_extent_length(leaf, extent);
1046 device->bytes_used -= len;
1047 spin_lock(&root->fs_info->free_chunk_lock);
1048 root->fs_info->free_chunk_space += len;
1049 spin_unlock(&root->fs_info->free_chunk_lock);
1050 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001051 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001052 if (ret) {
1053 btrfs_error(root->fs_info, ret,
1054 "Failed to remove dev extent item");
1055 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001056out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001057 btrfs_free_path(path);
1058 return ret;
1059}
1060
Yan Zheng2b820322008-11-17 21:11:30 -05001061int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001062 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001063 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001064 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001065{
1066 int ret;
1067 struct btrfs_path *path;
1068 struct btrfs_root *root = device->dev_root;
1069 struct btrfs_dev_extent *extent;
1070 struct extent_buffer *leaf;
1071 struct btrfs_key key;
1072
Chris Masondfe25022008-05-13 13:46:40 -04001073 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001074 path = btrfs_alloc_path();
1075 if (!path)
1076 return -ENOMEM;
1077
Chris Mason0b86a832008-03-24 15:01:56 -04001078 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001079 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001080 key.type = BTRFS_DEV_EXTENT_KEY;
1081 ret = btrfs_insert_empty_item(trans, root, path, &key,
1082 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001083 if (ret)
1084 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001085
1086 leaf = path->nodes[0];
1087 extent = btrfs_item_ptr(leaf, path->slots[0],
1088 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001089 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1090 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1091 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1092
1093 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1094 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1095 BTRFS_UUID_SIZE);
1096
Chris Mason0b86a832008-03-24 15:01:56 -04001097 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1098 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001099out:
Chris Mason0b86a832008-03-24 15:01:56 -04001100 btrfs_free_path(path);
1101 return ret;
1102}
1103
Chris Masona1b32a52008-09-05 16:09:51 -04001104static noinline int find_next_chunk(struct btrfs_root *root,
1105 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001106{
1107 struct btrfs_path *path;
1108 int ret;
1109 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001110 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001111 struct btrfs_key found_key;
1112
1113 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001114 if (!path)
1115 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001116
Chris Masone17cade2008-04-15 15:41:47 -04001117 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001118 key.offset = (u64)-1;
1119 key.type = BTRFS_CHUNK_ITEM_KEY;
1120
1121 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1122 if (ret < 0)
1123 goto error;
1124
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001125 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001126
1127 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1128 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001129 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001130 } else {
1131 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1132 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001133 if (found_key.objectid != objectid)
1134 *offset = 0;
1135 else {
1136 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1137 struct btrfs_chunk);
1138 *offset = found_key.offset +
1139 btrfs_chunk_length(path->nodes[0], chunk);
1140 }
Chris Mason0b86a832008-03-24 15:01:56 -04001141 }
1142 ret = 0;
1143error:
1144 btrfs_free_path(path);
1145 return ret;
1146}
1147
Yan Zheng2b820322008-11-17 21:11:30 -05001148static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001149{
1150 int ret;
1151 struct btrfs_key key;
1152 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001153 struct btrfs_path *path;
1154
1155 root = root->fs_info->chunk_root;
1156
1157 path = btrfs_alloc_path();
1158 if (!path)
1159 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001160
1161 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1162 key.type = BTRFS_DEV_ITEM_KEY;
1163 key.offset = (u64)-1;
1164
1165 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1166 if (ret < 0)
1167 goto error;
1168
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001169 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001170
1171 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1172 BTRFS_DEV_ITEM_KEY);
1173 if (ret) {
1174 *objectid = 1;
1175 } else {
1176 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1177 path->slots[0]);
1178 *objectid = found_key.offset + 1;
1179 }
1180 ret = 0;
1181error:
Yan Zheng2b820322008-11-17 21:11:30 -05001182 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001183 return ret;
1184}
1185
1186/*
1187 * the device information is stored in the chunk root
1188 * the btrfs_device struct should be fully filled in
1189 */
1190int btrfs_add_device(struct btrfs_trans_handle *trans,
1191 struct btrfs_root *root,
1192 struct btrfs_device *device)
1193{
1194 int ret;
1195 struct btrfs_path *path;
1196 struct btrfs_dev_item *dev_item;
1197 struct extent_buffer *leaf;
1198 struct btrfs_key key;
1199 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001200
1201 root = root->fs_info->chunk_root;
1202
1203 path = btrfs_alloc_path();
1204 if (!path)
1205 return -ENOMEM;
1206
Chris Mason0b86a832008-03-24 15:01:56 -04001207 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1208 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001209 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001210
1211 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001212 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001213 if (ret)
1214 goto out;
1215
1216 leaf = path->nodes[0];
1217 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1218
1219 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001220 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001221 btrfs_set_device_type(leaf, dev_item, device->type);
1222 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1223 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1224 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001225 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1226 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001227 btrfs_set_device_group(leaf, dev_item, 0);
1228 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1229 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001230 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001231
Chris Mason0b86a832008-03-24 15:01:56 -04001232 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001233 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001234 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1235 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001236 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001237
Yan Zheng2b820322008-11-17 21:11:30 -05001238 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001239out:
1240 btrfs_free_path(path);
1241 return ret;
1242}
Chris Mason8f18cf12008-04-25 16:53:30 -04001243
Chris Masona061fc82008-05-07 11:43:44 -04001244static int btrfs_rm_dev_item(struct btrfs_root *root,
1245 struct btrfs_device *device)
1246{
1247 int ret;
1248 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001249 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001250 struct btrfs_trans_handle *trans;
1251
1252 root = root->fs_info->chunk_root;
1253
1254 path = btrfs_alloc_path();
1255 if (!path)
1256 return -ENOMEM;
1257
Yan, Zhenga22285a2010-05-16 10:48:46 -04001258 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001259 if (IS_ERR(trans)) {
1260 btrfs_free_path(path);
1261 return PTR_ERR(trans);
1262 }
Chris Masona061fc82008-05-07 11:43:44 -04001263 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1264 key.type = BTRFS_DEV_ITEM_KEY;
1265 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001266 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001267
1268 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1269 if (ret < 0)
1270 goto out;
1271
1272 if (ret > 0) {
1273 ret = -ENOENT;
1274 goto out;
1275 }
1276
1277 ret = btrfs_del_item(trans, root, path);
1278 if (ret)
1279 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001280out:
1281 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001282 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001283 btrfs_commit_transaction(trans, root);
1284 return ret;
1285}
1286
1287int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1288{
1289 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001290 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001291 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001292 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001293 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001294 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001295 u64 all_avail;
1296 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001297 u64 num_devices;
1298 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001299 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001300 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001301
Chris Masona061fc82008-05-07 11:43:44 -04001302 mutex_lock(&uuid_mutex);
1303
1304 all_avail = root->fs_info->avail_data_alloc_bits |
1305 root->fs_info->avail_system_alloc_bits |
1306 root->fs_info->avail_metadata_alloc_bits;
1307
1308 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001309 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001310 printk(KERN_ERR "btrfs: unable to go below four devices "
1311 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001312 ret = -EINVAL;
1313 goto out;
1314 }
1315
1316 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001317 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001318 printk(KERN_ERR "btrfs: unable to go below two "
1319 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001320 ret = -EINVAL;
1321 goto out;
1322 }
1323
Chris Masondfe25022008-05-13 13:46:40 -04001324 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001325 struct list_head *devices;
1326 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001327
Chris Masondfe25022008-05-13 13:46:40 -04001328 device = NULL;
1329 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001330 /*
1331 * It is safe to read the devices since the volume_mutex
1332 * is held.
1333 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001334 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001335 if (tmp->in_fs_metadata && !tmp->bdev) {
1336 device = tmp;
1337 break;
1338 }
1339 }
1340 bdev = NULL;
1341 bh = NULL;
1342 disk_super = NULL;
1343 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001344 printk(KERN_ERR "btrfs: no missing devices found to "
1345 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001346 goto out;
1347 }
Chris Masondfe25022008-05-13 13:46:40 -04001348 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001349 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1350 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001351 if (IS_ERR(bdev)) {
1352 ret = PTR_ERR(bdev);
1353 goto out;
1354 }
1355
Yan Zheng2b820322008-11-17 21:11:30 -05001356 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001357 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001358 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001359 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001360 goto error_close;
1361 }
1362 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001363 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001364 dev_uuid = disk_super->dev_item.uuid;
1365 device = btrfs_find_device(root, devid, dev_uuid,
1366 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001367 if (!device) {
1368 ret = -ENOENT;
1369 goto error_brelse;
1370 }
Chris Masondfe25022008-05-13 13:46:40 -04001371 }
Yan Zheng2b820322008-11-17 21:11:30 -05001372
1373 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001374 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1375 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001376 ret = -EINVAL;
1377 goto error_brelse;
1378 }
1379
1380 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001381 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001382 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001383 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001384 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001385 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001386 }
Chris Masona061fc82008-05-07 11:43:44 -04001387
1388 ret = btrfs_shrink_device(device, 0);
1389 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001390 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001391
Chris Masona061fc82008-05-07 11:43:44 -04001392 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1393 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001394 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001395
Josef Bacik2bf64752011-09-26 17:12:22 -04001396 spin_lock(&root->fs_info->free_chunk_lock);
1397 root->fs_info->free_chunk_space = device->total_bytes -
1398 device->bytes_used;
1399 spin_unlock(&root->fs_info->free_chunk_lock);
1400
Yan Zheng2b820322008-11-17 21:11:30 -05001401 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001402 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001403
1404 /*
1405 * the device list mutex makes sure that we don't change
1406 * the device list while someone else is writing out all
1407 * the device supers.
1408 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001409
1410 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001411 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001412 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001413
Yan Zhenge4404d62008-12-12 10:03:26 -05001414 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001415
Chris Masoncd02dca2010-12-13 14:56:23 -05001416 if (device->missing)
1417 root->fs_info->fs_devices->missing_devices--;
1418
Yan Zheng2b820322008-11-17 21:11:30 -05001419 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1420 struct btrfs_device, dev_list);
1421 if (device->bdev == root->fs_info->sb->s_bdev)
1422 root->fs_info->sb->s_bdev = next_device->bdev;
1423 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1424 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1425
Xiao Guangrong1f781602011-04-20 10:09:16 +00001426 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001427 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001428
1429 call_rcu(&device->rcu, free_device);
1430 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001431
David Sterba6c417612011-04-13 15:41:04 +02001432 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1433 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001434
Xiao Guangrong1f781602011-04-20 10:09:16 +00001435 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001436 struct btrfs_fs_devices *fs_devices;
1437 fs_devices = root->fs_info->fs_devices;
1438 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001439 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001440 break;
1441 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001442 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001443 fs_devices->seed = cur_devices->seed;
1444 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001445 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001446 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001447 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001448 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001449 }
1450
1451 /*
1452 * at this point, the device is zero sized. We want to
1453 * remove it from the devices list and zero out the old super
1454 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001455 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001456 /* make sure this device isn't detected as part of
1457 * the FS anymore
1458 */
1459 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1460 set_buffer_dirty(bh);
1461 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001462 }
Chris Masona061fc82008-05-07 11:43:44 -04001463
Chris Masona061fc82008-05-07 11:43:44 -04001464 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001465
1466error_brelse:
1467 brelse(bh);
1468error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001469 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001470 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001471out:
1472 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001473 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001474error_undo:
1475 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001476 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001477 list_add(&device->dev_alloc_list,
1478 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001479 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001480 root->fs_info->fs_devices->rw_devices++;
1481 }
1482 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001483}
1484
Yan Zheng2b820322008-11-17 21:11:30 -05001485/*
1486 * does all the dirty work required for changing file system's UUID.
1487 */
Li Zefan125ccb02011-12-08 15:07:24 +08001488static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001489{
1490 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1491 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001492 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001493 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001494 struct btrfs_device *device;
1495 u64 super_flags;
1496
1497 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001498 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001499 return -EINVAL;
1500
Yan Zhenge4404d62008-12-12 10:03:26 -05001501 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1502 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001503 return -ENOMEM;
1504
Yan Zhenge4404d62008-12-12 10:03:26 -05001505 old_devices = clone_fs_devices(fs_devices);
1506 if (IS_ERR(old_devices)) {
1507 kfree(seed_devices);
1508 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001509 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001510
Yan Zheng2b820322008-11-17 21:11:30 -05001511 list_add(&old_devices->list, &fs_uuids);
1512
Yan Zhenge4404d62008-12-12 10:03:26 -05001513 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1514 seed_devices->opened = 1;
1515 INIT_LIST_HEAD(&seed_devices->devices);
1516 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001517 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001518
1519 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001520 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1521 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001522 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1523
Yan Zhenge4404d62008-12-12 10:03:26 -05001524 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1525 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1526 device->fs_devices = seed_devices;
1527 }
1528
Yan Zheng2b820322008-11-17 21:11:30 -05001529 fs_devices->seeding = 0;
1530 fs_devices->num_devices = 0;
1531 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001532 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001533
1534 generate_random_uuid(fs_devices->fsid);
1535 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1536 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1537 super_flags = btrfs_super_flags(disk_super) &
1538 ~BTRFS_SUPER_FLAG_SEEDING;
1539 btrfs_set_super_flags(disk_super, super_flags);
1540
1541 return 0;
1542}
1543
1544/*
1545 * strore the expected generation for seed devices in device items.
1546 */
1547static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1548 struct btrfs_root *root)
1549{
1550 struct btrfs_path *path;
1551 struct extent_buffer *leaf;
1552 struct btrfs_dev_item *dev_item;
1553 struct btrfs_device *device;
1554 struct btrfs_key key;
1555 u8 fs_uuid[BTRFS_UUID_SIZE];
1556 u8 dev_uuid[BTRFS_UUID_SIZE];
1557 u64 devid;
1558 int ret;
1559
1560 path = btrfs_alloc_path();
1561 if (!path)
1562 return -ENOMEM;
1563
1564 root = root->fs_info->chunk_root;
1565 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1566 key.offset = 0;
1567 key.type = BTRFS_DEV_ITEM_KEY;
1568
1569 while (1) {
1570 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1571 if (ret < 0)
1572 goto error;
1573
1574 leaf = path->nodes[0];
1575next_slot:
1576 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1577 ret = btrfs_next_leaf(root, path);
1578 if (ret > 0)
1579 break;
1580 if (ret < 0)
1581 goto error;
1582 leaf = path->nodes[0];
1583 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001584 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001585 continue;
1586 }
1587
1588 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1589 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1590 key.type != BTRFS_DEV_ITEM_KEY)
1591 break;
1592
1593 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1594 struct btrfs_dev_item);
1595 devid = btrfs_device_id(leaf, dev_item);
1596 read_extent_buffer(leaf, dev_uuid,
1597 (unsigned long)btrfs_device_uuid(dev_item),
1598 BTRFS_UUID_SIZE);
1599 read_extent_buffer(leaf, fs_uuid,
1600 (unsigned long)btrfs_device_fsid(dev_item),
1601 BTRFS_UUID_SIZE);
1602 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001603 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001604
1605 if (device->fs_devices->seeding) {
1606 btrfs_set_device_generation(leaf, dev_item,
1607 device->generation);
1608 btrfs_mark_buffer_dirty(leaf);
1609 }
1610
1611 path->slots[0]++;
1612 goto next_slot;
1613 }
1614 ret = 0;
1615error:
1616 btrfs_free_path(path);
1617 return ret;
1618}
1619
Chris Mason788f20e2008-04-28 15:29:42 -04001620int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1621{
Josef Bacikd5e20032011-08-04 14:52:27 +00001622 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001623 struct btrfs_trans_handle *trans;
1624 struct btrfs_device *device;
1625 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001626 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001627 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001628 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001629 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001630 int ret = 0;
1631
Yan Zheng2b820322008-11-17 21:11:30 -05001632 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1633 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001634
Li Zefana5d16332011-12-07 20:08:40 -05001635 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001636 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001637 if (IS_ERR(bdev))
1638 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001639
Yan Zheng2b820322008-11-17 21:11:30 -05001640 if (root->fs_info->fs_devices->seeding) {
1641 seeding_dev = 1;
1642 down_write(&sb->s_umount);
1643 mutex_lock(&uuid_mutex);
1644 }
1645
Chris Mason8c8bee12008-09-29 11:19:10 -04001646 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001647
Chris Mason788f20e2008-04-28 15:29:42 -04001648 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001649 /*
1650 * we have the volume lock, so we don't need the extra
1651 * device list mutex while reading the list here.
1652 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001653 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001654 if (device->bdev == bdev) {
1655 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001656 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001657 }
1658 }
1659
1660 device = kzalloc(sizeof(*device), GFP_NOFS);
1661 if (!device) {
1662 /* we can safely leave the fs_devices entry around */
1663 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001664 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001665 }
1666
Chris Mason788f20e2008-04-28 15:29:42 -04001667 device->name = kstrdup(device_path, GFP_NOFS);
1668 if (!device->name) {
1669 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001670 ret = -ENOMEM;
1671 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001672 }
Yan Zheng2b820322008-11-17 21:11:30 -05001673
1674 ret = find_next_devid(root, &device->devid);
1675 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001676 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001677 kfree(device);
1678 goto error;
1679 }
1680
Yan, Zhenga22285a2010-05-16 10:48:46 -04001681 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001682 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001683 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001684 kfree(device);
1685 ret = PTR_ERR(trans);
1686 goto error;
1687 }
1688
Yan Zheng2b820322008-11-17 21:11:30 -05001689 lock_chunks(root);
1690
Josef Bacikd5e20032011-08-04 14:52:27 +00001691 q = bdev_get_queue(bdev);
1692 if (blk_queue_discard(q))
1693 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001694 device->writeable = 1;
1695 device->work.func = pending_bios_fn;
1696 generate_random_uuid(device->uuid);
1697 spin_lock_init(&device->io_lock);
1698 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001699 device->io_width = root->sectorsize;
1700 device->io_align = root->sectorsize;
1701 device->sector_size = root->sectorsize;
1702 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001703 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001704 device->dev_root = root->fs_info->dev_root;
1705 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001706 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001707 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001708 set_blocksize(device->bdev, 4096);
1709
Yan Zheng2b820322008-11-17 21:11:30 -05001710 if (seeding_dev) {
1711 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001712 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001713 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001714 }
1715
1716 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001717
1718 /*
1719 * we don't want write_supers to jump in here with our device
1720 * half setup
1721 */
1722 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001723 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001724 list_add(&device->dev_alloc_list,
1725 &root->fs_info->fs_devices->alloc_list);
1726 root->fs_info->fs_devices->num_devices++;
1727 root->fs_info->fs_devices->open_devices++;
1728 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001729 if (device->can_discard)
1730 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001731 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1732
Josef Bacik2bf64752011-09-26 17:12:22 -04001733 spin_lock(&root->fs_info->free_chunk_lock);
1734 root->fs_info->free_chunk_space += device->total_bytes;
1735 spin_unlock(&root->fs_info->free_chunk_lock);
1736
Chris Masonc2898112009-06-10 09:51:32 -04001737 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1738 root->fs_info->fs_devices->rotating = 1;
1739
David Sterba6c417612011-04-13 15:41:04 +02001740 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1741 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001742 total_bytes + device->total_bytes);
1743
David Sterba6c417612011-04-13 15:41:04 +02001744 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1745 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001746 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001747 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001748
Yan Zheng2b820322008-11-17 21:11:30 -05001749 if (seeding_dev) {
1750 ret = init_first_rw_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001751 if (ret)
1752 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001753 ret = btrfs_finish_sprout(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001754 if (ret)
1755 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001756 } else {
1757 ret = btrfs_add_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001758 if (ret)
1759 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001760 }
1761
Chris Mason913d9522009-03-10 13:17:18 -04001762 /*
1763 * we've got more storage, clear any full flags on the space
1764 * infos
1765 */
1766 btrfs_clear_space_info_full(root->fs_info);
1767
Chris Mason7d9eb122008-07-08 14:19:17 -04001768 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001769 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001770
1771 if (seeding_dev) {
1772 mutex_unlock(&uuid_mutex);
1773 up_write(&sb->s_umount);
1774
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001775 if (ret) /* transaction commit */
1776 return ret;
1777
Yan Zheng2b820322008-11-17 21:11:30 -05001778 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001779 if (ret < 0)
1780 btrfs_error(root->fs_info, ret,
1781 "Failed to relocate sys chunks after "
1782 "device initialization. This can be fixed "
1783 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001784 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001785
Chris Mason788f20e2008-04-28 15:29:42 -04001786 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001787
1788error_trans:
1789 unlock_chunks(root);
1790 btrfs_abort_transaction(trans, root, ret);
1791 btrfs_end_transaction(trans, root);
1792 kfree(device->name);
1793 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001794error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001795 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001796 if (seeding_dev) {
1797 mutex_unlock(&uuid_mutex);
1798 up_write(&sb->s_umount);
1799 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001800 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001801}
1802
Chris Masond3977122009-01-05 21:25:51 -05001803static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1804 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001805{
1806 int ret;
1807 struct btrfs_path *path;
1808 struct btrfs_root *root;
1809 struct btrfs_dev_item *dev_item;
1810 struct extent_buffer *leaf;
1811 struct btrfs_key key;
1812
1813 root = device->dev_root->fs_info->chunk_root;
1814
1815 path = btrfs_alloc_path();
1816 if (!path)
1817 return -ENOMEM;
1818
1819 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1820 key.type = BTRFS_DEV_ITEM_KEY;
1821 key.offset = device->devid;
1822
1823 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1824 if (ret < 0)
1825 goto out;
1826
1827 if (ret > 0) {
1828 ret = -ENOENT;
1829 goto out;
1830 }
1831
1832 leaf = path->nodes[0];
1833 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1834
1835 btrfs_set_device_id(leaf, dev_item, device->devid);
1836 btrfs_set_device_type(leaf, dev_item, device->type);
1837 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1838 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1839 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001840 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001841 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1842 btrfs_mark_buffer_dirty(leaf);
1843
1844out:
1845 btrfs_free_path(path);
1846 return ret;
1847}
1848
Chris Mason7d9eb122008-07-08 14:19:17 -04001849static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001850 struct btrfs_device *device, u64 new_size)
1851{
1852 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001853 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001854 u64 old_total = btrfs_super_total_bytes(super_copy);
1855 u64 diff = new_size - device->total_bytes;
1856
Yan Zheng2b820322008-11-17 21:11:30 -05001857 if (!device->writeable)
1858 return -EACCES;
1859 if (new_size <= device->total_bytes)
1860 return -EINVAL;
1861
Chris Mason8f18cf12008-04-25 16:53:30 -04001862 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001863 device->fs_devices->total_rw_bytes += diff;
1864
1865 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001866 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001867 btrfs_clear_space_info_full(device->dev_root->fs_info);
1868
Chris Mason8f18cf12008-04-25 16:53:30 -04001869 return btrfs_update_device(trans, device);
1870}
1871
Chris Mason7d9eb122008-07-08 14:19:17 -04001872int btrfs_grow_device(struct btrfs_trans_handle *trans,
1873 struct btrfs_device *device, u64 new_size)
1874{
1875 int ret;
1876 lock_chunks(device->dev_root);
1877 ret = __btrfs_grow_device(trans, device, new_size);
1878 unlock_chunks(device->dev_root);
1879 return ret;
1880}
1881
Chris Mason8f18cf12008-04-25 16:53:30 -04001882static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1883 struct btrfs_root *root,
1884 u64 chunk_tree, u64 chunk_objectid,
1885 u64 chunk_offset)
1886{
1887 int ret;
1888 struct btrfs_path *path;
1889 struct btrfs_key key;
1890
1891 root = root->fs_info->chunk_root;
1892 path = btrfs_alloc_path();
1893 if (!path)
1894 return -ENOMEM;
1895
1896 key.objectid = chunk_objectid;
1897 key.offset = chunk_offset;
1898 key.type = BTRFS_CHUNK_ITEM_KEY;
1899
1900 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001901 if (ret < 0)
1902 goto out;
1903 else if (ret > 0) { /* Logic error or corruption */
1904 btrfs_error(root->fs_info, -ENOENT,
1905 "Failed lookup while freeing chunk.");
1906 ret = -ENOENT;
1907 goto out;
1908 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001909
1910 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001911 if (ret < 0)
1912 btrfs_error(root->fs_info, ret,
1913 "Failed to delete chunk item.");
1914out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001915 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001916 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001917}
1918
Christoph Hellwigb2950862008-12-02 09:54:17 -05001919static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001920 chunk_offset)
1921{
David Sterba6c417612011-04-13 15:41:04 +02001922 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001923 struct btrfs_disk_key *disk_key;
1924 struct btrfs_chunk *chunk;
1925 u8 *ptr;
1926 int ret = 0;
1927 u32 num_stripes;
1928 u32 array_size;
1929 u32 len = 0;
1930 u32 cur;
1931 struct btrfs_key key;
1932
1933 array_size = btrfs_super_sys_array_size(super_copy);
1934
1935 ptr = super_copy->sys_chunk_array;
1936 cur = 0;
1937
1938 while (cur < array_size) {
1939 disk_key = (struct btrfs_disk_key *)ptr;
1940 btrfs_disk_key_to_cpu(&key, disk_key);
1941
1942 len = sizeof(*disk_key);
1943
1944 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1945 chunk = (struct btrfs_chunk *)(ptr + len);
1946 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1947 len += btrfs_chunk_item_size(num_stripes);
1948 } else {
1949 ret = -EIO;
1950 break;
1951 }
1952 if (key.objectid == chunk_objectid &&
1953 key.offset == chunk_offset) {
1954 memmove(ptr, ptr + len, array_size - (cur + len));
1955 array_size -= len;
1956 btrfs_set_super_sys_array_size(super_copy, array_size);
1957 } else {
1958 ptr += len;
1959 cur += len;
1960 }
1961 }
1962 return ret;
1963}
1964
Christoph Hellwigb2950862008-12-02 09:54:17 -05001965static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001966 u64 chunk_tree, u64 chunk_objectid,
1967 u64 chunk_offset)
1968{
1969 struct extent_map_tree *em_tree;
1970 struct btrfs_root *extent_root;
1971 struct btrfs_trans_handle *trans;
1972 struct extent_map *em;
1973 struct map_lookup *map;
1974 int ret;
1975 int i;
1976
1977 root = root->fs_info->chunk_root;
1978 extent_root = root->fs_info->extent_root;
1979 em_tree = &root->fs_info->mapping_tree.map_tree;
1980
Josef Bacikba1bf482009-09-11 16:11:19 -04001981 ret = btrfs_can_relocate(extent_root, chunk_offset);
1982 if (ret)
1983 return -ENOSPC;
1984
Chris Mason8f18cf12008-04-25 16:53:30 -04001985 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001986 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001987 if (ret)
1988 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001989
Yan, Zhenga22285a2010-05-16 10:48:46 -04001990 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001991 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001992
Chris Mason7d9eb122008-07-08 14:19:17 -04001993 lock_chunks(root);
1994
Chris Mason8f18cf12008-04-25 16:53:30 -04001995 /*
1996 * step two, delete the device extents and the
1997 * chunk tree entries
1998 */
Chris Mason890871b2009-09-02 16:24:52 -04001999 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002000 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002001 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002002
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002003 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002004 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002005 map = (struct map_lookup *)em->bdev;
2006
2007 for (i = 0; i < map->num_stripes; i++) {
2008 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2009 map->stripes[i].physical);
2010 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002011
Chris Masondfe25022008-05-13 13:46:40 -04002012 if (map->stripes[i].dev) {
2013 ret = btrfs_update_device(trans, map->stripes[i].dev);
2014 BUG_ON(ret);
2015 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002016 }
2017 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2018 chunk_offset);
2019
2020 BUG_ON(ret);
2021
liubo1abe9b82011-03-24 11:18:59 +00002022 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2023
Chris Mason8f18cf12008-04-25 16:53:30 -04002024 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2025 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2026 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002027 }
2028
Zheng Yan1a40e232008-09-26 10:09:34 -04002029 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2030 BUG_ON(ret);
2031
Chris Mason890871b2009-09-02 16:24:52 -04002032 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002033 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002034 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002035
Chris Mason8f18cf12008-04-25 16:53:30 -04002036 kfree(map);
2037 em->bdev = NULL;
2038
2039 /* once for the tree */
2040 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002041 /* once for us */
2042 free_extent_map(em);
2043
Chris Mason7d9eb122008-07-08 14:19:17 -04002044 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002045 btrfs_end_transaction(trans, root);
2046 return 0;
2047}
2048
Yan Zheng2b820322008-11-17 21:11:30 -05002049static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2050{
2051 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2052 struct btrfs_path *path;
2053 struct extent_buffer *leaf;
2054 struct btrfs_chunk *chunk;
2055 struct btrfs_key key;
2056 struct btrfs_key found_key;
2057 u64 chunk_tree = chunk_root->root_key.objectid;
2058 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002059 bool retried = false;
2060 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002061 int ret;
2062
2063 path = btrfs_alloc_path();
2064 if (!path)
2065 return -ENOMEM;
2066
Josef Bacikba1bf482009-09-11 16:11:19 -04002067again:
Yan Zheng2b820322008-11-17 21:11:30 -05002068 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2069 key.offset = (u64)-1;
2070 key.type = BTRFS_CHUNK_ITEM_KEY;
2071
2072 while (1) {
2073 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2074 if (ret < 0)
2075 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002076 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002077
2078 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2079 key.type);
2080 if (ret < 0)
2081 goto error;
2082 if (ret > 0)
2083 break;
2084
2085 leaf = path->nodes[0];
2086 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2087
2088 chunk = btrfs_item_ptr(leaf, path->slots[0],
2089 struct btrfs_chunk);
2090 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002091 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002092
2093 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2094 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2095 found_key.objectid,
2096 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002097 if (ret == -ENOSPC)
2098 failed++;
2099 else if (ret)
2100 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002101 }
2102
2103 if (found_key.offset == 0)
2104 break;
2105 key.offset = found_key.offset - 1;
2106 }
2107 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002108 if (failed && !retried) {
2109 failed = 0;
2110 retried = true;
2111 goto again;
2112 } else if (failed && retried) {
2113 WARN_ON(1);
2114 ret = -ENOSPC;
2115 }
Yan Zheng2b820322008-11-17 21:11:30 -05002116error:
2117 btrfs_free_path(path);
2118 return ret;
2119}
2120
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002121static int insert_balance_item(struct btrfs_root *root,
2122 struct btrfs_balance_control *bctl)
2123{
2124 struct btrfs_trans_handle *trans;
2125 struct btrfs_balance_item *item;
2126 struct btrfs_disk_balance_args disk_bargs;
2127 struct btrfs_path *path;
2128 struct extent_buffer *leaf;
2129 struct btrfs_key key;
2130 int ret, err;
2131
2132 path = btrfs_alloc_path();
2133 if (!path)
2134 return -ENOMEM;
2135
2136 trans = btrfs_start_transaction(root, 0);
2137 if (IS_ERR(trans)) {
2138 btrfs_free_path(path);
2139 return PTR_ERR(trans);
2140 }
2141
2142 key.objectid = BTRFS_BALANCE_OBJECTID;
2143 key.type = BTRFS_BALANCE_ITEM_KEY;
2144 key.offset = 0;
2145
2146 ret = btrfs_insert_empty_item(trans, root, path, &key,
2147 sizeof(*item));
2148 if (ret)
2149 goto out;
2150
2151 leaf = path->nodes[0];
2152 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2153
2154 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2155
2156 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2157 btrfs_set_balance_data(leaf, item, &disk_bargs);
2158 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2159 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2160 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2161 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2162
2163 btrfs_set_balance_flags(leaf, item, bctl->flags);
2164
2165 btrfs_mark_buffer_dirty(leaf);
2166out:
2167 btrfs_free_path(path);
2168 err = btrfs_commit_transaction(trans, root);
2169 if (err && !ret)
2170 ret = err;
2171 return ret;
2172}
2173
2174static int del_balance_item(struct btrfs_root *root)
2175{
2176 struct btrfs_trans_handle *trans;
2177 struct btrfs_path *path;
2178 struct btrfs_key key;
2179 int ret, err;
2180
2181 path = btrfs_alloc_path();
2182 if (!path)
2183 return -ENOMEM;
2184
2185 trans = btrfs_start_transaction(root, 0);
2186 if (IS_ERR(trans)) {
2187 btrfs_free_path(path);
2188 return PTR_ERR(trans);
2189 }
2190
2191 key.objectid = BTRFS_BALANCE_OBJECTID;
2192 key.type = BTRFS_BALANCE_ITEM_KEY;
2193 key.offset = 0;
2194
2195 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2196 if (ret < 0)
2197 goto out;
2198 if (ret > 0) {
2199 ret = -ENOENT;
2200 goto out;
2201 }
2202
2203 ret = btrfs_del_item(trans, root, path);
2204out:
2205 btrfs_free_path(path);
2206 err = btrfs_commit_transaction(trans, root);
2207 if (err && !ret)
2208 ret = err;
2209 return ret;
2210}
2211
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002212/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002213 * This is a heuristic used to reduce the number of chunks balanced on
2214 * resume after balance was interrupted.
2215 */
2216static void update_balance_args(struct btrfs_balance_control *bctl)
2217{
2218 /*
2219 * Turn on soft mode for chunk types that were being converted.
2220 */
2221 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2222 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2223 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2224 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2225 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2226 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2227
2228 /*
2229 * Turn on usage filter if is not already used. The idea is
2230 * that chunks that we have already balanced should be
2231 * reasonably full. Don't do it for chunks that are being
2232 * converted - that will keep us from relocating unconverted
2233 * (albeit full) chunks.
2234 */
2235 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2236 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2237 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2238 bctl->data.usage = 90;
2239 }
2240 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2241 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2242 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2243 bctl->sys.usage = 90;
2244 }
2245 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2246 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2247 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2248 bctl->meta.usage = 90;
2249 }
2250}
2251
2252/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002253 * Should be called with both balance and volume mutexes held to
2254 * serialize other volume operations (add_dev/rm_dev/resize) with
2255 * restriper. Same goes for unset_balance_control.
2256 */
2257static void set_balance_control(struct btrfs_balance_control *bctl)
2258{
2259 struct btrfs_fs_info *fs_info = bctl->fs_info;
2260
2261 BUG_ON(fs_info->balance_ctl);
2262
2263 spin_lock(&fs_info->balance_lock);
2264 fs_info->balance_ctl = bctl;
2265 spin_unlock(&fs_info->balance_lock);
2266}
2267
2268static void unset_balance_control(struct btrfs_fs_info *fs_info)
2269{
2270 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2271
2272 BUG_ON(!fs_info->balance_ctl);
2273
2274 spin_lock(&fs_info->balance_lock);
2275 fs_info->balance_ctl = NULL;
2276 spin_unlock(&fs_info->balance_lock);
2277
2278 kfree(bctl);
2279}
2280
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002281/*
2282 * Balance filters. Return 1 if chunk should be filtered out
2283 * (should not be balanced).
2284 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002285static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002286 struct btrfs_balance_args *bargs)
2287{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002288 chunk_type = chunk_to_extended(chunk_type) &
2289 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002290
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002291 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002292 return 0;
2293
2294 return 1;
2295}
2296
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002297static u64 div_factor_fine(u64 num, int factor)
2298{
2299 if (factor <= 0)
2300 return 0;
2301 if (factor >= 100)
2302 return num;
2303
2304 num *= factor;
2305 do_div(num, 100);
2306 return num;
2307}
2308
2309static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2310 struct btrfs_balance_args *bargs)
2311{
2312 struct btrfs_block_group_cache *cache;
2313 u64 chunk_used, user_thresh;
2314 int ret = 1;
2315
2316 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2317 chunk_used = btrfs_block_group_used(&cache->item);
2318
2319 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2320 if (chunk_used < user_thresh)
2321 ret = 0;
2322
2323 btrfs_put_block_group(cache);
2324 return ret;
2325}
2326
Ilya Dryomov409d4042012-01-16 22:04:47 +02002327static int chunk_devid_filter(struct extent_buffer *leaf,
2328 struct btrfs_chunk *chunk,
2329 struct btrfs_balance_args *bargs)
2330{
2331 struct btrfs_stripe *stripe;
2332 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2333 int i;
2334
2335 for (i = 0; i < num_stripes; i++) {
2336 stripe = btrfs_stripe_nr(chunk, i);
2337 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2338 return 0;
2339 }
2340
2341 return 1;
2342}
2343
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002344/* [pstart, pend) */
2345static int chunk_drange_filter(struct extent_buffer *leaf,
2346 struct btrfs_chunk *chunk,
2347 u64 chunk_offset,
2348 struct btrfs_balance_args *bargs)
2349{
2350 struct btrfs_stripe *stripe;
2351 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2352 u64 stripe_offset;
2353 u64 stripe_length;
2354 int factor;
2355 int i;
2356
2357 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2358 return 0;
2359
2360 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2361 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2362 factor = 2;
2363 else
2364 factor = 1;
2365 factor = num_stripes / factor;
2366
2367 for (i = 0; i < num_stripes; i++) {
2368 stripe = btrfs_stripe_nr(chunk, i);
2369 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2370 continue;
2371
2372 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2373 stripe_length = btrfs_chunk_length(leaf, chunk);
2374 do_div(stripe_length, factor);
2375
2376 if (stripe_offset < bargs->pend &&
2377 stripe_offset + stripe_length > bargs->pstart)
2378 return 0;
2379 }
2380
2381 return 1;
2382}
2383
Ilya Dryomovea671762012-01-16 22:04:48 +02002384/* [vstart, vend) */
2385static int chunk_vrange_filter(struct extent_buffer *leaf,
2386 struct btrfs_chunk *chunk,
2387 u64 chunk_offset,
2388 struct btrfs_balance_args *bargs)
2389{
2390 if (chunk_offset < bargs->vend &&
2391 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2392 /* at least part of the chunk is inside this vrange */
2393 return 0;
2394
2395 return 1;
2396}
2397
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002398static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002399 struct btrfs_balance_args *bargs)
2400{
2401 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2402 return 0;
2403
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002404 chunk_type = chunk_to_extended(chunk_type) &
2405 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002406
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002407 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002408 return 1;
2409
2410 return 0;
2411}
2412
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002413static int should_balance_chunk(struct btrfs_root *root,
2414 struct extent_buffer *leaf,
2415 struct btrfs_chunk *chunk, u64 chunk_offset)
2416{
2417 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2418 struct btrfs_balance_args *bargs = NULL;
2419 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2420
2421 /* type filter */
2422 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2423 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2424 return 0;
2425 }
2426
2427 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2428 bargs = &bctl->data;
2429 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2430 bargs = &bctl->sys;
2431 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2432 bargs = &bctl->meta;
2433
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002434 /* profiles filter */
2435 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2436 chunk_profiles_filter(chunk_type, bargs)) {
2437 return 0;
2438 }
2439
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002440 /* usage filter */
2441 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2442 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2443 return 0;
2444 }
2445
Ilya Dryomov409d4042012-01-16 22:04:47 +02002446 /* devid filter */
2447 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2448 chunk_devid_filter(leaf, chunk, bargs)) {
2449 return 0;
2450 }
2451
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002452 /* drange filter, makes sense only with devid filter */
2453 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2454 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2455 return 0;
2456 }
2457
Ilya Dryomovea671762012-01-16 22:04:48 +02002458 /* vrange filter */
2459 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2460 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2461 return 0;
2462 }
2463
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002464 /* soft profile changing mode */
2465 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2466 chunk_soft_convert_filter(chunk_type, bargs)) {
2467 return 0;
2468 }
2469
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002470 return 1;
2471}
2472
Chris Masonec44a352008-04-28 15:29:52 -04002473static u64 div_factor(u64 num, int factor)
2474{
2475 if (factor == 10)
2476 return num;
2477 num *= factor;
2478 do_div(num, 10);
2479 return num;
2480}
2481
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002482static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002483{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002484 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002485 struct btrfs_root *chunk_root = fs_info->chunk_root;
2486 struct btrfs_root *dev_root = fs_info->dev_root;
2487 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002488 struct btrfs_device *device;
2489 u64 old_size;
2490 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002491 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002492 struct btrfs_path *path;
2493 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002494 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002495 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002496 struct extent_buffer *leaf;
2497 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002498 int ret;
2499 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002500 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002501
Chris Masonec44a352008-04-28 15:29:52 -04002502 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002503 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002504 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002505 old_size = device->total_bytes;
2506 size_to_free = div_factor(old_size, 1);
2507 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002508 if (!device->writeable ||
2509 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002510 continue;
2511
2512 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002513 if (ret == -ENOSPC)
2514 break;
Chris Masonec44a352008-04-28 15:29:52 -04002515 BUG_ON(ret);
2516
Yan, Zhenga22285a2010-05-16 10:48:46 -04002517 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002518 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002519
2520 ret = btrfs_grow_device(trans, device, old_size);
2521 BUG_ON(ret);
2522
2523 btrfs_end_transaction(trans, dev_root);
2524 }
2525
2526 /* step two, relocate all the chunks */
2527 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002528 if (!path) {
2529 ret = -ENOMEM;
2530 goto error;
2531 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002532
2533 /* zero out stat counters */
2534 spin_lock(&fs_info->balance_lock);
2535 memset(&bctl->stat, 0, sizeof(bctl->stat));
2536 spin_unlock(&fs_info->balance_lock);
2537again:
Chris Masonec44a352008-04-28 15:29:52 -04002538 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2539 key.offset = (u64)-1;
2540 key.type = BTRFS_CHUNK_ITEM_KEY;
2541
Chris Masond3977122009-01-05 21:25:51 -05002542 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002543 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002544 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002545 ret = -ECANCELED;
2546 goto error;
2547 }
2548
Chris Masonec44a352008-04-28 15:29:52 -04002549 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2550 if (ret < 0)
2551 goto error;
2552
2553 /*
2554 * this shouldn't happen, it means the last relocate
2555 * failed
2556 */
2557 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002558 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002559
2560 ret = btrfs_previous_item(chunk_root, path, 0,
2561 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002562 if (ret) {
2563 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002564 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002565 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002566
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002567 leaf = path->nodes[0];
2568 slot = path->slots[0];
2569 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2570
Chris Masonec44a352008-04-28 15:29:52 -04002571 if (found_key.objectid != key.objectid)
2572 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002573
Chris Masonec44a352008-04-28 15:29:52 -04002574 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002575 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002576 break;
2577
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002578 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2579
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002580 if (!counting) {
2581 spin_lock(&fs_info->balance_lock);
2582 bctl->stat.considered++;
2583 spin_unlock(&fs_info->balance_lock);
2584 }
2585
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002586 ret = should_balance_chunk(chunk_root, leaf, chunk,
2587 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002588 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002589 if (!ret)
2590 goto loop;
2591
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002592 if (counting) {
2593 spin_lock(&fs_info->balance_lock);
2594 bctl->stat.expected++;
2595 spin_unlock(&fs_info->balance_lock);
2596 goto loop;
2597 }
2598
Chris Masonec44a352008-04-28 15:29:52 -04002599 ret = btrfs_relocate_chunk(chunk_root,
2600 chunk_root->root_key.objectid,
2601 found_key.objectid,
2602 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002603 if (ret && ret != -ENOSPC)
2604 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002605 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002606 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002607 } else {
2608 spin_lock(&fs_info->balance_lock);
2609 bctl->stat.completed++;
2610 spin_unlock(&fs_info->balance_lock);
2611 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002612loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002613 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002614 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002615
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002616 if (counting) {
2617 btrfs_release_path(path);
2618 counting = false;
2619 goto again;
2620 }
Chris Masonec44a352008-04-28 15:29:52 -04002621error:
2622 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002623 if (enospc_errors) {
2624 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2625 enospc_errors);
2626 if (!ret)
2627 ret = -ENOSPC;
2628 }
2629
Chris Masonec44a352008-04-28 15:29:52 -04002630 return ret;
2631}
2632
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002633/**
2634 * alloc_profile_is_valid - see if a given profile is valid and reduced
2635 * @flags: profile to validate
2636 * @extended: if true @flags is treated as an extended profile
2637 */
2638static int alloc_profile_is_valid(u64 flags, int extended)
2639{
2640 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2641 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2642
2643 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2644
2645 /* 1) check that all other bits are zeroed */
2646 if (flags & ~mask)
2647 return 0;
2648
2649 /* 2) see if profile is reduced */
2650 if (flags == 0)
2651 return !extended; /* "0" is valid for usual profiles */
2652
2653 /* true if exactly one bit set */
2654 return (flags & (flags - 1)) == 0;
2655}
2656
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002657static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2658{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002659 /* cancel requested || normal exit path */
2660 return atomic_read(&fs_info->balance_cancel_req) ||
2661 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2662 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002663}
2664
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002665static void __cancel_balance(struct btrfs_fs_info *fs_info)
2666{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002667 int ret;
2668
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002669 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002670 ret = del_balance_item(fs_info->tree_root);
2671 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002672}
2673
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002674void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002675 struct btrfs_ioctl_balance_args *bargs);
2676
2677/*
2678 * Should be called with both balance and volume mutexes held
2679 */
2680int btrfs_balance(struct btrfs_balance_control *bctl,
2681 struct btrfs_ioctl_balance_args *bargs)
2682{
2683 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002684 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002685 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002686 int ret;
2687
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002688 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002689 atomic_read(&fs_info->balance_pause_req) ||
2690 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002691 ret = -EINVAL;
2692 goto out;
2693 }
2694
Ilya Dryomove4837f82012-03-27 17:09:17 +03002695 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2696 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2697 mixed = 1;
2698
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002699 /*
2700 * In case of mixed groups both data and meta should be picked,
2701 * and identical options should be given for both of them.
2702 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002703 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2704 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002705 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2706 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2707 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2708 printk(KERN_ERR "btrfs: with mixed groups data and "
2709 "metadata balance options must be the same\n");
2710 ret = -EINVAL;
2711 goto out;
2712 }
2713 }
2714
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002715 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2716 if (fs_info->fs_devices->num_devices == 1)
2717 allowed |= BTRFS_BLOCK_GROUP_DUP;
2718 else if (fs_info->fs_devices->num_devices < 4)
2719 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2720 else
2721 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2722 BTRFS_BLOCK_GROUP_RAID10);
2723
Ilya Dryomov6728b192012-03-27 17:09:17 +03002724 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2725 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2726 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002727 printk(KERN_ERR "btrfs: unable to start balance with target "
2728 "data profile %llu\n",
2729 (unsigned long long)bctl->data.target);
2730 ret = -EINVAL;
2731 goto out;
2732 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002733 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2734 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2735 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002736 printk(KERN_ERR "btrfs: unable to start balance with target "
2737 "metadata profile %llu\n",
2738 (unsigned long long)bctl->meta.target);
2739 ret = -EINVAL;
2740 goto out;
2741 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002742 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2743 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2744 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002745 printk(KERN_ERR "btrfs: unable to start balance with target "
2746 "system profile %llu\n",
2747 (unsigned long long)bctl->sys.target);
2748 ret = -EINVAL;
2749 goto out;
2750 }
2751
Ilya Dryomove4837f82012-03-27 17:09:17 +03002752 /* allow dup'ed data chunks only in mixed mode */
2753 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002754 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002755 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2756 ret = -EINVAL;
2757 goto out;
2758 }
2759
2760 /* allow to reduce meta or sys integrity only if force set */
2761 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2762 BTRFS_BLOCK_GROUP_RAID10;
2763 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2764 (fs_info->avail_system_alloc_bits & allowed) &&
2765 !(bctl->sys.target & allowed)) ||
2766 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2767 (fs_info->avail_metadata_alloc_bits & allowed) &&
2768 !(bctl->meta.target & allowed))) {
2769 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2770 printk(KERN_INFO "btrfs: force reducing metadata "
2771 "integrity\n");
2772 } else {
2773 printk(KERN_ERR "btrfs: balance will reduce metadata "
2774 "integrity, use force if you want this\n");
2775 ret = -EINVAL;
2776 goto out;
2777 }
2778 }
2779
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002780 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002781 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002782 goto out;
2783
Ilya Dryomov59641012012-01-16 22:04:48 +02002784 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2785 BUG_ON(ret == -EEXIST);
2786 set_balance_control(bctl);
2787 } else {
2788 BUG_ON(ret != -EEXIST);
2789 spin_lock(&fs_info->balance_lock);
2790 update_balance_args(bctl);
2791 spin_unlock(&fs_info->balance_lock);
2792 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002793
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002794 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002795 mutex_unlock(&fs_info->balance_mutex);
2796
2797 ret = __btrfs_balance(fs_info);
2798
2799 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002800 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002801
2802 if (bargs) {
2803 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002804 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002805 }
2806
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002807 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2808 balance_need_close(fs_info)) {
2809 __cancel_balance(fs_info);
2810 }
2811
2812 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002813
2814 return ret;
2815out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002816 if (bctl->flags & BTRFS_BALANCE_RESUME)
2817 __cancel_balance(fs_info);
2818 else
2819 kfree(bctl);
2820 return ret;
2821}
2822
2823static int balance_kthread(void *data)
2824{
2825 struct btrfs_balance_control *bctl =
2826 (struct btrfs_balance_control *)data;
2827 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002828 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002829
2830 mutex_lock(&fs_info->volume_mutex);
2831 mutex_lock(&fs_info->balance_mutex);
2832
2833 set_balance_control(bctl);
2834
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002835 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2836 printk(KERN_INFO "btrfs: force skipping balance\n");
2837 } else {
2838 printk(KERN_INFO "btrfs: continuing balance\n");
2839 ret = btrfs_balance(bctl, NULL);
2840 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002841
2842 mutex_unlock(&fs_info->balance_mutex);
2843 mutex_unlock(&fs_info->volume_mutex);
2844 return ret;
2845}
2846
2847int btrfs_recover_balance(struct btrfs_root *tree_root)
2848{
2849 struct task_struct *tsk;
2850 struct btrfs_balance_control *bctl;
2851 struct btrfs_balance_item *item;
2852 struct btrfs_disk_balance_args disk_bargs;
2853 struct btrfs_path *path;
2854 struct extent_buffer *leaf;
2855 struct btrfs_key key;
2856 int ret;
2857
2858 path = btrfs_alloc_path();
2859 if (!path)
2860 return -ENOMEM;
2861
2862 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2863 if (!bctl) {
2864 ret = -ENOMEM;
2865 goto out;
2866 }
2867
2868 key.objectid = BTRFS_BALANCE_OBJECTID;
2869 key.type = BTRFS_BALANCE_ITEM_KEY;
2870 key.offset = 0;
2871
2872 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2873 if (ret < 0)
2874 goto out_bctl;
2875 if (ret > 0) { /* ret = -ENOENT; */
2876 ret = 0;
2877 goto out_bctl;
2878 }
2879
2880 leaf = path->nodes[0];
2881 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2882
2883 bctl->fs_info = tree_root->fs_info;
2884 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2885
2886 btrfs_balance_data(leaf, item, &disk_bargs);
2887 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2888 btrfs_balance_meta(leaf, item, &disk_bargs);
2889 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2890 btrfs_balance_sys(leaf, item, &disk_bargs);
2891 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2892
2893 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2894 if (IS_ERR(tsk))
2895 ret = PTR_ERR(tsk);
2896 else
2897 goto out;
2898
2899out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002900 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002901out:
2902 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002903 return ret;
2904}
2905
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002906int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2907{
2908 int ret = 0;
2909
2910 mutex_lock(&fs_info->balance_mutex);
2911 if (!fs_info->balance_ctl) {
2912 mutex_unlock(&fs_info->balance_mutex);
2913 return -ENOTCONN;
2914 }
2915
2916 if (atomic_read(&fs_info->balance_running)) {
2917 atomic_inc(&fs_info->balance_pause_req);
2918 mutex_unlock(&fs_info->balance_mutex);
2919
2920 wait_event(fs_info->balance_wait_q,
2921 atomic_read(&fs_info->balance_running) == 0);
2922
2923 mutex_lock(&fs_info->balance_mutex);
2924 /* we are good with balance_ctl ripped off from under us */
2925 BUG_ON(atomic_read(&fs_info->balance_running));
2926 atomic_dec(&fs_info->balance_pause_req);
2927 } else {
2928 ret = -ENOTCONN;
2929 }
2930
2931 mutex_unlock(&fs_info->balance_mutex);
2932 return ret;
2933}
2934
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002935int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2936{
2937 mutex_lock(&fs_info->balance_mutex);
2938 if (!fs_info->balance_ctl) {
2939 mutex_unlock(&fs_info->balance_mutex);
2940 return -ENOTCONN;
2941 }
2942
2943 atomic_inc(&fs_info->balance_cancel_req);
2944 /*
2945 * if we are running just wait and return, balance item is
2946 * deleted in btrfs_balance in this case
2947 */
2948 if (atomic_read(&fs_info->balance_running)) {
2949 mutex_unlock(&fs_info->balance_mutex);
2950 wait_event(fs_info->balance_wait_q,
2951 atomic_read(&fs_info->balance_running) == 0);
2952 mutex_lock(&fs_info->balance_mutex);
2953 } else {
2954 /* __cancel_balance needs volume_mutex */
2955 mutex_unlock(&fs_info->balance_mutex);
2956 mutex_lock(&fs_info->volume_mutex);
2957 mutex_lock(&fs_info->balance_mutex);
2958
2959 if (fs_info->balance_ctl)
2960 __cancel_balance(fs_info);
2961
2962 mutex_unlock(&fs_info->volume_mutex);
2963 }
2964
2965 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2966 atomic_dec(&fs_info->balance_cancel_req);
2967 mutex_unlock(&fs_info->balance_mutex);
2968 return 0;
2969}
2970
Chris Mason8f18cf12008-04-25 16:53:30 -04002971/*
2972 * shrinking a device means finding all of the device extents past
2973 * the new size, and then following the back refs to the chunks.
2974 * The chunk relocation code actually frees the device extent
2975 */
2976int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2977{
2978 struct btrfs_trans_handle *trans;
2979 struct btrfs_root *root = device->dev_root;
2980 struct btrfs_dev_extent *dev_extent = NULL;
2981 struct btrfs_path *path;
2982 u64 length;
2983 u64 chunk_tree;
2984 u64 chunk_objectid;
2985 u64 chunk_offset;
2986 int ret;
2987 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002988 int failed = 0;
2989 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002990 struct extent_buffer *l;
2991 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002992 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002993 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002994 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002995 u64 diff = device->total_bytes - new_size;
2996
Yan Zheng2b820322008-11-17 21:11:30 -05002997 if (new_size >= device->total_bytes)
2998 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002999
3000 path = btrfs_alloc_path();
3001 if (!path)
3002 return -ENOMEM;
3003
Chris Mason8f18cf12008-04-25 16:53:30 -04003004 path->reada = 2;
3005
Chris Mason7d9eb122008-07-08 14:19:17 -04003006 lock_chunks(root);
3007
Chris Mason8f18cf12008-04-25 16:53:30 -04003008 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003009 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003010 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003011 spin_lock(&root->fs_info->free_chunk_lock);
3012 root->fs_info->free_chunk_space -= diff;
3013 spin_unlock(&root->fs_info->free_chunk_lock);
3014 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003015 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003016
Josef Bacikba1bf482009-09-11 16:11:19 -04003017again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003018 key.objectid = device->devid;
3019 key.offset = (u64)-1;
3020 key.type = BTRFS_DEV_EXTENT_KEY;
3021
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003022 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003023 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3024 if (ret < 0)
3025 goto done;
3026
3027 ret = btrfs_previous_item(root, path, 0, key.type);
3028 if (ret < 0)
3029 goto done;
3030 if (ret) {
3031 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003032 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003033 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003034 }
3035
3036 l = path->nodes[0];
3037 slot = path->slots[0];
3038 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3039
Josef Bacikba1bf482009-09-11 16:11:19 -04003040 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003041 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003042 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003043 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003044
3045 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3046 length = btrfs_dev_extent_length(l, dev_extent);
3047
Josef Bacikba1bf482009-09-11 16:11:19 -04003048 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003049 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003050 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003051 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003052
3053 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3054 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3055 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003056 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003057
3058 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3059 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003060 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003061 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003062 if (ret == -ENOSPC)
3063 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003064 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003065
3066 if (failed && !retried) {
3067 failed = 0;
3068 retried = true;
3069 goto again;
3070 } else if (failed && retried) {
3071 ret = -ENOSPC;
3072 lock_chunks(root);
3073
3074 device->total_bytes = old_size;
3075 if (device->writeable)
3076 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003077 spin_lock(&root->fs_info->free_chunk_lock);
3078 root->fs_info->free_chunk_space += diff;
3079 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003080 unlock_chunks(root);
3081 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003082 }
3083
Chris Balld6397ba2009-04-27 07:29:03 -04003084 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003085 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003086 if (IS_ERR(trans)) {
3087 ret = PTR_ERR(trans);
3088 goto done;
3089 }
3090
Chris Balld6397ba2009-04-27 07:29:03 -04003091 lock_chunks(root);
3092
3093 device->disk_total_bytes = new_size;
3094 /* Now btrfs_update_device() will change the on-disk size. */
3095 ret = btrfs_update_device(trans, device);
3096 if (ret) {
3097 unlock_chunks(root);
3098 btrfs_end_transaction(trans, root);
3099 goto done;
3100 }
3101 WARN_ON(diff > old_total);
3102 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3103 unlock_chunks(root);
3104 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003105done:
3106 btrfs_free_path(path);
3107 return ret;
3108}
3109
Li Zefan125ccb02011-12-08 15:07:24 +08003110static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003111 struct btrfs_key *key,
3112 struct btrfs_chunk *chunk, int item_size)
3113{
David Sterba6c417612011-04-13 15:41:04 +02003114 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003115 struct btrfs_disk_key disk_key;
3116 u32 array_size;
3117 u8 *ptr;
3118
3119 array_size = btrfs_super_sys_array_size(super_copy);
3120 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3121 return -EFBIG;
3122
3123 ptr = super_copy->sys_chunk_array + array_size;
3124 btrfs_cpu_key_to_disk(&disk_key, key);
3125 memcpy(ptr, &disk_key, sizeof(disk_key));
3126 ptr += sizeof(disk_key);
3127 memcpy(ptr, chunk, item_size);
3128 item_size += sizeof(disk_key);
3129 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3130 return 0;
3131}
3132
Miao Xieb2117a32011-01-05 10:07:28 +00003133/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003134 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003135 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003136static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003137{
Arne Jansen73c5de02011-04-12 12:07:57 +02003138 const struct btrfs_device_info *di_a = a;
3139 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003140
Arne Jansen73c5de02011-04-12 12:07:57 +02003141 if (di_a->max_avail > di_b->max_avail)
3142 return -1;
3143 if (di_a->max_avail < di_b->max_avail)
3144 return 1;
3145 if (di_a->total_avail > di_b->total_avail)
3146 return -1;
3147 if (di_a->total_avail < di_b->total_avail)
3148 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003149 return 0;
3150}
3151
3152static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3153 struct btrfs_root *extent_root,
3154 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003155 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003156 u64 start, u64 type)
3157{
3158 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003159 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3160 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003161 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003162 struct extent_map_tree *em_tree;
3163 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003164 struct btrfs_device_info *devices_info = NULL;
3165 u64 total_avail;
3166 int num_stripes; /* total number of stripes to allocate */
3167 int sub_stripes; /* sub_stripes info for map */
3168 int dev_stripes; /* stripes per dev */
3169 int devs_max; /* max devs to use */
3170 int devs_min; /* min devs needed */
3171 int devs_increment; /* ndevs has to be a multiple of this */
3172 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003173 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003174 u64 max_stripe_size;
3175 u64 max_chunk_size;
3176 u64 stripe_size;
3177 u64 num_bytes;
3178 int ndevs;
3179 int i;
3180 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003181
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003182 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003183
Miao Xieb2117a32011-01-05 10:07:28 +00003184 if (list_empty(&fs_devices->alloc_list))
3185 return -ENOSPC;
3186
Arne Jansen73c5de02011-04-12 12:07:57 +02003187 sub_stripes = 1;
3188 dev_stripes = 1;
3189 devs_increment = 1;
3190 ncopies = 1;
3191 devs_max = 0; /* 0 == as many as possible */
3192 devs_min = 1;
3193
3194 /*
3195 * define the properties of each RAID type.
3196 * FIXME: move this to a global table and use it in all RAID
3197 * calculation code
3198 */
3199 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3200 dev_stripes = 2;
3201 ncopies = 2;
3202 devs_max = 1;
3203 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3204 devs_min = 2;
3205 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3206 devs_increment = 2;
3207 ncopies = 2;
3208 devs_max = 2;
3209 devs_min = 2;
3210 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3211 sub_stripes = 2;
3212 devs_increment = 2;
3213 ncopies = 2;
3214 devs_min = 4;
3215 } else {
3216 devs_max = 1;
3217 }
3218
3219 if (type & BTRFS_BLOCK_GROUP_DATA) {
3220 max_stripe_size = 1024 * 1024 * 1024;
3221 max_chunk_size = 10 * max_stripe_size;
3222 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003223 /* for larger filesystems, use larger metadata chunks */
3224 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3225 max_stripe_size = 1024 * 1024 * 1024;
3226 else
3227 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003228 max_chunk_size = max_stripe_size;
3229 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003230 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003231 max_chunk_size = 2 * max_stripe_size;
3232 } else {
3233 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3234 type);
3235 BUG_ON(1);
3236 }
3237
3238 /* we don't want a chunk larger than 10% of writeable space */
3239 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3240 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003241
3242 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3243 GFP_NOFS);
3244 if (!devices_info)
3245 return -ENOMEM;
3246
Arne Jansen73c5de02011-04-12 12:07:57 +02003247 cur = fs_devices->alloc_list.next;
3248
3249 /*
3250 * in the first pass through the devices list, we gather information
3251 * about the available holes on each device.
3252 */
3253 ndevs = 0;
3254 while (cur != &fs_devices->alloc_list) {
3255 struct btrfs_device *device;
3256 u64 max_avail;
3257 u64 dev_offset;
3258
3259 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3260
3261 cur = cur->next;
3262
3263 if (!device->writeable) {
3264 printk(KERN_ERR
3265 "btrfs: read-only device in alloc_list\n");
3266 WARN_ON(1);
3267 continue;
3268 }
3269
3270 if (!device->in_fs_metadata)
3271 continue;
3272
3273 if (device->total_bytes > device->bytes_used)
3274 total_avail = device->total_bytes - device->bytes_used;
3275 else
3276 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003277
3278 /* If there is no space on this device, skip it. */
3279 if (total_avail == 0)
3280 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003281
Li Zefan125ccb02011-12-08 15:07:24 +08003282 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003283 max_stripe_size * dev_stripes,
3284 &dev_offset, &max_avail);
3285 if (ret && ret != -ENOSPC)
3286 goto error;
3287
3288 if (ret == 0)
3289 max_avail = max_stripe_size * dev_stripes;
3290
3291 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3292 continue;
3293
3294 devices_info[ndevs].dev_offset = dev_offset;
3295 devices_info[ndevs].max_avail = max_avail;
3296 devices_info[ndevs].total_avail = total_avail;
3297 devices_info[ndevs].dev = device;
3298 ++ndevs;
3299 }
3300
3301 /*
3302 * now sort the devices by hole size / available space
3303 */
3304 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3305 btrfs_cmp_device_info, NULL);
3306
3307 /* round down to number of usable stripes */
3308 ndevs -= ndevs % devs_increment;
3309
3310 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3311 ret = -ENOSPC;
3312 goto error;
3313 }
3314
3315 if (devs_max && ndevs > devs_max)
3316 ndevs = devs_max;
3317 /*
3318 * the primary goal is to maximize the number of stripes, so use as many
3319 * devices as possible, even if the stripes are not maximum sized.
3320 */
3321 stripe_size = devices_info[ndevs-1].max_avail;
3322 num_stripes = ndevs * dev_stripes;
3323
3324 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
3325 stripe_size = max_chunk_size * ncopies;
3326 do_div(stripe_size, num_stripes);
3327 }
3328
3329 do_div(stripe_size, dev_stripes);
3330 do_div(stripe_size, BTRFS_STRIPE_LEN);
3331 stripe_size *= BTRFS_STRIPE_LEN;
3332
Miao Xieb2117a32011-01-05 10:07:28 +00003333 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3334 if (!map) {
3335 ret = -ENOMEM;
3336 goto error;
3337 }
3338 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003339
Arne Jansen73c5de02011-04-12 12:07:57 +02003340 for (i = 0; i < ndevs; ++i) {
3341 for (j = 0; j < dev_stripes; ++j) {
3342 int s = i * dev_stripes + j;
3343 map->stripes[s].dev = devices_info[i].dev;
3344 map->stripes[s].physical = devices_info[i].dev_offset +
3345 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003346 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003347 }
Chris Mason593060d2008-03-25 16:50:33 -04003348 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003349 map->stripe_len = BTRFS_STRIPE_LEN;
3350 map->io_align = BTRFS_STRIPE_LEN;
3351 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003352 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003353 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003354
Yan Zheng2b820322008-11-17 21:11:30 -05003355 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003356 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003357
Arne Jansen73c5de02011-04-12 12:07:57 +02003358 *stripe_size_out = stripe_size;
3359 *num_bytes_out = num_bytes;
3360
3361 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003362
David Sterba172ddd62011-04-21 00:48:27 +02003363 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003364 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003365 ret = -ENOMEM;
3366 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003367 }
Chris Mason0b86a832008-03-24 15:01:56 -04003368 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003369 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003370 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003371 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003372 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003373
Chris Mason0b86a832008-03-24 15:01:56 -04003374 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003375 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003376 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003377 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003378 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003379 if (ret)
3380 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003381
3382 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3383 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003384 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003385 if (ret)
3386 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003387
Arne Jansen73c5de02011-04-12 12:07:57 +02003388 for (i = 0; i < map->num_stripes; ++i) {
3389 struct btrfs_device *device;
3390 u64 dev_offset;
3391
3392 device = map->stripes[i].dev;
3393 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003394
3395 ret = btrfs_alloc_dev_extent(trans, device,
3396 info->chunk_root->root_key.objectid,
3397 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003398 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003399 if (ret) {
3400 btrfs_abort_transaction(trans, extent_root, ret);
3401 goto error;
3402 }
Yan Zheng2b820322008-11-17 21:11:30 -05003403 }
3404
Miao Xieb2117a32011-01-05 10:07:28 +00003405 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003406 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003407
3408error:
3409 kfree(map);
3410 kfree(devices_info);
3411 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003412}
3413
3414static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3415 struct btrfs_root *extent_root,
3416 struct map_lookup *map, u64 chunk_offset,
3417 u64 chunk_size, u64 stripe_size)
3418{
3419 u64 dev_offset;
3420 struct btrfs_key key;
3421 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3422 struct btrfs_device *device;
3423 struct btrfs_chunk *chunk;
3424 struct btrfs_stripe *stripe;
3425 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3426 int index = 0;
3427 int ret;
3428
3429 chunk = kzalloc(item_size, GFP_NOFS);
3430 if (!chunk)
3431 return -ENOMEM;
3432
3433 index = 0;
3434 while (index < map->num_stripes) {
3435 device = map->stripes[index].dev;
3436 device->bytes_used += stripe_size;
3437 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003438 if (ret)
3439 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003440 index++;
3441 }
3442
Josef Bacik2bf64752011-09-26 17:12:22 -04003443 spin_lock(&extent_root->fs_info->free_chunk_lock);
3444 extent_root->fs_info->free_chunk_space -= (stripe_size *
3445 map->num_stripes);
3446 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3447
Yan Zheng2b820322008-11-17 21:11:30 -05003448 index = 0;
3449 stripe = &chunk->stripe;
3450 while (index < map->num_stripes) {
3451 device = map->stripes[index].dev;
3452 dev_offset = map->stripes[index].physical;
3453
3454 btrfs_set_stack_stripe_devid(stripe, device->devid);
3455 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3456 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3457 stripe++;
3458 index++;
3459 }
3460
3461 btrfs_set_stack_chunk_length(chunk, chunk_size);
3462 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3463 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3464 btrfs_set_stack_chunk_type(chunk, map->type);
3465 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3466 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3467 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3468 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3469 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3470
3471 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3472 key.type = BTRFS_CHUNK_ITEM_KEY;
3473 key.offset = chunk_offset;
3474
3475 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003476
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003477 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3478 /*
3479 * TODO: Cleanup of inserted chunk root in case of
3480 * failure.
3481 */
Li Zefan125ccb02011-12-08 15:07:24 +08003482 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003483 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003484 }
liubo1abe9b82011-03-24 11:18:59 +00003485
Mark Fasheh3acd3952011-09-08 17:40:01 -07003486out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003487 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003488 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003489}
3490
3491/*
3492 * Chunk allocation falls into two parts. The first part does works
3493 * that make the new allocated chunk useable, but not do any operation
3494 * that modifies the chunk tree. The second part does the works that
3495 * require modifying the chunk tree. This division is important for the
3496 * bootstrap process of adding storage to a seed btrfs.
3497 */
3498int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3499 struct btrfs_root *extent_root, u64 type)
3500{
3501 u64 chunk_offset;
3502 u64 chunk_size;
3503 u64 stripe_size;
3504 struct map_lookup *map;
3505 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3506 int ret;
3507
3508 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3509 &chunk_offset);
3510 if (ret)
3511 return ret;
3512
3513 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3514 &stripe_size, chunk_offset, type);
3515 if (ret)
3516 return ret;
3517
3518 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3519 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003520 if (ret)
3521 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003522 return 0;
3523}
3524
Chris Masond3977122009-01-05 21:25:51 -05003525static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003526 struct btrfs_root *root,
3527 struct btrfs_device *device)
3528{
3529 u64 chunk_offset;
3530 u64 sys_chunk_offset;
3531 u64 chunk_size;
3532 u64 sys_chunk_size;
3533 u64 stripe_size;
3534 u64 sys_stripe_size;
3535 u64 alloc_profile;
3536 struct map_lookup *map;
3537 struct map_lookup *sys_map;
3538 struct btrfs_fs_info *fs_info = root->fs_info;
3539 struct btrfs_root *extent_root = fs_info->extent_root;
3540 int ret;
3541
3542 ret = find_next_chunk(fs_info->chunk_root,
3543 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003544 if (ret)
3545 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003546
3547 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003548 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003549 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3550
3551 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3552 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003553 if (ret)
3554 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003555
3556 sys_chunk_offset = chunk_offset + chunk_size;
3557
3558 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003559 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003560 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3561
3562 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3563 &sys_chunk_size, &sys_stripe_size,
3564 sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003565 if (ret)
3566 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003567
3568 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003569 if (ret)
3570 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003571
3572 /*
3573 * Modifying chunk tree needs allocating new blocks from both
3574 * system block group and metadata block group. So we only can
3575 * do operations require modifying the chunk tree after both
3576 * block groups were created.
3577 */
3578 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3579 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003580 if (ret)
3581 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003582
3583 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3584 sys_chunk_offset, sys_chunk_size,
3585 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003586 if (ret)
3587 goto abort;
3588
Yan Zheng2b820322008-11-17 21:11:30 -05003589 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003590
3591abort:
3592 btrfs_abort_transaction(trans, root, ret);
3593 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003594}
3595
3596int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3597{
3598 struct extent_map *em;
3599 struct map_lookup *map;
3600 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3601 int readonly = 0;
3602 int i;
3603
Chris Mason890871b2009-09-02 16:24:52 -04003604 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003605 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003606 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003607 if (!em)
3608 return 1;
3609
Josef Bacikf48b9072010-01-27 02:07:59 +00003610 if (btrfs_test_opt(root, DEGRADED)) {
3611 free_extent_map(em);
3612 return 0;
3613 }
3614
Yan Zheng2b820322008-11-17 21:11:30 -05003615 map = (struct map_lookup *)em->bdev;
3616 for (i = 0; i < map->num_stripes; i++) {
3617 if (!map->stripes[i].dev->writeable) {
3618 readonly = 1;
3619 break;
3620 }
3621 }
3622 free_extent_map(em);
3623 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003624}
3625
3626void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3627{
David Sterbaa8067e02011-04-21 00:34:43 +02003628 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003629}
3630
3631void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3632{
3633 struct extent_map *em;
3634
Chris Masond3977122009-01-05 21:25:51 -05003635 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003636 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003637 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3638 if (em)
3639 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003640 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003641 if (!em)
3642 break;
3643 kfree(em->bdev);
3644 /* once for us */
3645 free_extent_map(em);
3646 /* once for the tree */
3647 free_extent_map(em);
3648 }
3649}
3650
Chris Masonf1885912008-04-09 16:28:12 -04003651int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3652{
3653 struct extent_map *em;
3654 struct map_lookup *map;
3655 struct extent_map_tree *em_tree = &map_tree->map_tree;
3656 int ret;
3657
Chris Mason890871b2009-09-02 16:24:52 -04003658 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003659 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003660 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003661 BUG_ON(!em);
3662
3663 BUG_ON(em->start > logical || em->start + em->len < logical);
3664 map = (struct map_lookup *)em->bdev;
3665 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3666 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003667 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3668 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003669 else
3670 ret = 1;
3671 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003672 return ret;
3673}
3674
Chris Masondfe25022008-05-13 13:46:40 -04003675static int find_live_mirror(struct map_lookup *map, int first, int num,
3676 int optimal)
3677{
3678 int i;
3679 if (map->stripes[optimal].dev->bdev)
3680 return optimal;
3681 for (i = first; i < first + num; i++) {
3682 if (map->stripes[i].dev->bdev)
3683 return i;
3684 }
3685 /* we couldn't find one that doesn't fail. Just return something
3686 * and the io error handling code will clean up eventually
3687 */
3688 return optimal;
3689}
3690
Chris Masonf2d8d742008-04-21 10:03:05 -04003691static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3692 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003693 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003694 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003695{
3696 struct extent_map *em;
3697 struct map_lookup *map;
3698 struct extent_map_tree *em_tree = &map_tree->map_tree;
3699 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003700 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003701 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003702 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003703 u64 stripe_nr_orig;
3704 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003705 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003706 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003707 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003708 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003709 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003710 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003711
Chris Mason890871b2009-09-02 16:24:52 -04003712 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003713 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003714 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003715
Chris Mason3b951512008-04-17 11:29:12 -04003716 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003717 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3718 (unsigned long long)logical,
3719 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003720 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003721 }
Chris Mason0b86a832008-03-24 15:01:56 -04003722
3723 BUG_ON(em->start > logical || em->start + em->len < logical);
3724 map = (struct map_lookup *)em->bdev;
3725 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003726
Chris Masonf1885912008-04-09 16:28:12 -04003727 if (mirror_num > map->num_stripes)
3728 mirror_num = 0;
3729
Chris Mason593060d2008-03-25 16:50:33 -04003730 stripe_nr = offset;
3731 /*
3732 * stripe_nr counts the total number of stripes we have to stride
3733 * to get to this block
3734 */
3735 do_div(stripe_nr, map->stripe_len);
3736
3737 stripe_offset = stripe_nr * map->stripe_len;
3738 BUG_ON(offset < stripe_offset);
3739
3740 /* stripe_offset is the offset of this block in its stripe*/
3741 stripe_offset = offset - stripe_offset;
3742
Li Dongyangfce3bb92011-03-24 10:24:26 +00003743 if (rw & REQ_DISCARD)
3744 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003745 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003746 /* we limit the length of each bio to what fits in a stripe */
3747 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003748 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003749 } else {
3750 *length = em->len - offset;
3751 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003752
Jan Schmidta1d3c472011-08-04 17:15:33 +02003753 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003754 goto out;
3755
Chris Masonf2d8d742008-04-21 10:03:05 -04003756 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003757 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003758 stripe_nr_orig = stripe_nr;
3759 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3760 (~(map->stripe_len - 1));
3761 do_div(stripe_nr_end, map->stripe_len);
3762 stripe_end_offset = stripe_nr_end * map->stripe_len -
3763 (offset + *length);
3764 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3765 if (rw & REQ_DISCARD)
3766 num_stripes = min_t(u64, map->num_stripes,
3767 stripe_nr_end - stripe_nr_orig);
3768 stripe_index = do_div(stripe_nr, map->num_stripes);
3769 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003770 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003771 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003772 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003773 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003774 else {
3775 stripe_index = find_live_mirror(map, 0,
3776 map->num_stripes,
3777 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003778 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003779 }
Chris Mason2fff7342008-04-29 14:12:09 -04003780
Chris Mason611f0e02008-04-03 16:29:03 -04003781 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003782 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003783 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003784 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003785 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003786 } else {
3787 mirror_num = 1;
3788 }
Chris Mason2fff7342008-04-29 14:12:09 -04003789
Chris Mason321aecc2008-04-16 10:49:51 -04003790 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3791 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003792
3793 stripe_index = do_div(stripe_nr, factor);
3794 stripe_index *= map->sub_stripes;
3795
Jens Axboe7eaceac2011-03-10 08:52:07 +01003796 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003797 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003798 else if (rw & REQ_DISCARD)
3799 num_stripes = min_t(u64, map->sub_stripes *
3800 (stripe_nr_end - stripe_nr_orig),
3801 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003802 else if (mirror_num)
3803 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003804 else {
3805 stripe_index = find_live_mirror(map, stripe_index,
3806 map->sub_stripes, stripe_index +
3807 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003808 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003809 }
Chris Mason8790d502008-04-03 16:29:03 -04003810 } else {
3811 /*
3812 * after this do_div call, stripe_nr is the number of stripes
3813 * on this device we have to walk to find the data, and
3814 * stripe_index is the number of our device in the stripe array
3815 */
3816 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003817 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003818 }
Chris Mason593060d2008-03-25 16:50:33 -04003819 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003820
Li Zefande11cc12011-12-01 12:55:47 +08003821 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3822 if (!bbio) {
3823 ret = -ENOMEM;
3824 goto out;
3825 }
3826 atomic_set(&bbio->error, 0);
3827
Li Dongyangfce3bb92011-03-24 10:24:26 +00003828 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003829 int factor = 0;
3830 int sub_stripes = 0;
3831 u64 stripes_per_dev = 0;
3832 u32 remaining_stripes = 0;
3833
3834 if (map->type &
3835 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3836 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3837 sub_stripes = 1;
3838 else
3839 sub_stripes = map->sub_stripes;
3840
3841 factor = map->num_stripes / sub_stripes;
3842 stripes_per_dev = div_u64_rem(stripe_nr_end -
3843 stripe_nr_orig,
3844 factor,
3845 &remaining_stripes);
3846 }
3847
Li Dongyangfce3bb92011-03-24 10:24:26 +00003848 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003849 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003850 map->stripes[stripe_index].physical +
3851 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003852 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003853
Li Zefanec9ef7a2011-12-01 14:06:42 +08003854 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3855 BTRFS_BLOCK_GROUP_RAID10)) {
3856 bbio->stripes[i].length = stripes_per_dev *
3857 map->stripe_len;
3858 if (i / sub_stripes < remaining_stripes)
3859 bbio->stripes[i].length +=
3860 map->stripe_len;
3861 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003862 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003863 stripe_offset;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003864 if ((i / sub_stripes + 1) %
3865 sub_stripes == remaining_stripes)
3866 bbio->stripes[i].length -=
3867 stripe_end_offset;
3868 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003869 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003870 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003871 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003872
3873 stripe_index++;
3874 if (stripe_index == map->num_stripes) {
3875 /* This could only happen for RAID0/10 */
3876 stripe_index = 0;
3877 stripe_nr++;
3878 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003879 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003880 } else {
3881 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003882 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003883 map->stripes[stripe_index].physical +
3884 stripe_offset +
3885 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003886 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003887 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003888 stripe_index++;
3889 }
Chris Mason593060d2008-03-25 16:50:33 -04003890 }
Li Zefande11cc12011-12-01 12:55:47 +08003891
3892 if (rw & REQ_WRITE) {
3893 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3894 BTRFS_BLOCK_GROUP_RAID10 |
3895 BTRFS_BLOCK_GROUP_DUP)) {
3896 max_errors = 1;
3897 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003898 }
Li Zefande11cc12011-12-01 12:55:47 +08003899
3900 *bbio_ret = bbio;
3901 bbio->num_stripes = num_stripes;
3902 bbio->max_errors = max_errors;
3903 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003904out:
Chris Mason0b86a832008-03-24 15:01:56 -04003905 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003906 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003907}
3908
Chris Masonf2d8d742008-04-21 10:03:05 -04003909int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3910 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003911 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003912{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003913 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003914 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003915}
3916
Yan Zhenga512bbf2008-12-08 16:46:26 -05003917int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3918 u64 chunk_start, u64 physical, u64 devid,
3919 u64 **logical, int *naddrs, int *stripe_len)
3920{
3921 struct extent_map_tree *em_tree = &map_tree->map_tree;
3922 struct extent_map *em;
3923 struct map_lookup *map;
3924 u64 *buf;
3925 u64 bytenr;
3926 u64 length;
3927 u64 stripe_nr;
3928 int i, j, nr = 0;
3929
Chris Mason890871b2009-09-02 16:24:52 -04003930 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003931 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003932 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003933
3934 BUG_ON(!em || em->start != chunk_start);
3935 map = (struct map_lookup *)em->bdev;
3936
3937 length = em->len;
3938 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3939 do_div(length, map->num_stripes / map->sub_stripes);
3940 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3941 do_div(length, map->num_stripes);
3942
3943 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003944 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003945
3946 for (i = 0; i < map->num_stripes; i++) {
3947 if (devid && map->stripes[i].dev->devid != devid)
3948 continue;
3949 if (map->stripes[i].physical > physical ||
3950 map->stripes[i].physical + length <= physical)
3951 continue;
3952
3953 stripe_nr = physical - map->stripes[i].physical;
3954 do_div(stripe_nr, map->stripe_len);
3955
3956 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3957 stripe_nr = stripe_nr * map->num_stripes + i;
3958 do_div(stripe_nr, map->sub_stripes);
3959 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3960 stripe_nr = stripe_nr * map->num_stripes + i;
3961 }
3962 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003963 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003964 for (j = 0; j < nr; j++) {
3965 if (buf[j] == bytenr)
3966 break;
3967 }
Chris Mason934d3752008-12-08 16:43:10 -05003968 if (j == nr) {
3969 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003970 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003971 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003972 }
3973
Yan Zhenga512bbf2008-12-08 16:46:26 -05003974 *logical = buf;
3975 *naddrs = nr;
3976 *stripe_len = map->stripe_len;
3977
3978 free_extent_map(em);
3979 return 0;
3980}
3981
Jan Schmidta1d3c472011-08-04 17:15:33 +02003982static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003983{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003984 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003985 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003986
Chris Mason8790d502008-04-03 16:29:03 -04003987 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003988 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003989
Jan Schmidta1d3c472011-08-04 17:15:33 +02003990 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003991 is_orig_bio = 1;
3992
Jan Schmidta1d3c472011-08-04 17:15:33 +02003993 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003994 if (!is_orig_bio) {
3995 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003996 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003997 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003998 bio->bi_private = bbio->private;
3999 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004000 bio->bi_bdev = (struct block_device *)
4001 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004002 /* only send an error to the higher layers if it is
4003 * beyond the tolerance of the multi-bio
4004 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004005 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004006 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004007 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004008 /*
4009 * this bio is actually up to date, we didn't
4010 * go over the max number of errors
4011 */
4012 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004013 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004014 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004015 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004016
4017 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004018 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004019 bio_put(bio);
4020 }
Chris Mason8790d502008-04-03 16:29:03 -04004021}
4022
Chris Mason8b712842008-06-11 16:50:36 -04004023struct async_sched {
4024 struct bio *bio;
4025 int rw;
4026 struct btrfs_fs_info *info;
4027 struct btrfs_work work;
4028};
4029
4030/*
4031 * see run_scheduled_bios for a description of why bios are collected for
4032 * async submit.
4033 *
4034 * This will add one bio to the pending list for a device and make sure
4035 * the work struct is scheduled.
4036 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004037static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004038 struct btrfs_device *device,
4039 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004040{
4041 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004042 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004043
4044 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004045 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004046 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004047 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004048 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004049 return;
Chris Mason8b712842008-06-11 16:50:36 -04004050 }
4051
4052 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004053 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004054 * higher layers. Otherwise, the async bio makes it appear we have
4055 * made progress against dirty pages when we've really just put it
4056 * on a queue for later
4057 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004058 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004059 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004060 bio->bi_next = NULL;
4061 bio->bi_rw |= rw;
4062
4063 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004064 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004065 pending_bios = &device->pending_sync_bios;
4066 else
4067 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004068
Chris Masonffbd5172009-04-20 15:50:09 -04004069 if (pending_bios->tail)
4070 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004071
Chris Masonffbd5172009-04-20 15:50:09 -04004072 pending_bios->tail = bio;
4073 if (!pending_bios->head)
4074 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004075 if (device->running_pending)
4076 should_queue = 0;
4077
4078 spin_unlock(&device->io_lock);
4079
4080 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004081 btrfs_queue_worker(&root->fs_info->submit_workers,
4082 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004083}
4084
Chris Masonf1885912008-04-09 16:28:12 -04004085int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004086 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004087{
4088 struct btrfs_mapping_tree *map_tree;
4089 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004090 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004091 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004092 u64 length = 0;
4093 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004094 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004095 int dev_nr = 0;
4096 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004097 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004098
Chris Masonf2d8d742008-04-21 10:03:05 -04004099 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004100 map_tree = &root->fs_info->mapping_tree;
4101 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004102
Jan Schmidta1d3c472011-08-04 17:15:33 +02004103 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004104 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004105 if (ret) /* -ENOMEM */
4106 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004107
Jan Schmidta1d3c472011-08-04 17:15:33 +02004108 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004109 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004110 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4111 "len %llu\n", (unsigned long long)logical,
4112 (unsigned long long)length,
4113 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004114 BUG();
4115 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004116
4117 bbio->orig_bio = first_bio;
4118 bbio->private = first_bio->bi_private;
4119 bbio->end_io = first_bio->bi_end_io;
4120 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004121
Chris Masond3977122009-01-05 21:25:51 -05004122 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004123 if (dev_nr < total_devs - 1) {
4124 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004125 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004126 } else {
4127 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004128 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004129 bio->bi_private = bbio;
4130 bio->bi_end_io = btrfs_end_bio;
4131 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4132 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004133 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004134 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4135 "(%s id %llu), size=%u\n", rw,
4136 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4137 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004138 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004139 if (async_submit)
4140 schedule_bio(root, dev, rw, bio);
4141 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004142 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004143 } else {
4144 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4145 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004146 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004147 }
Chris Mason8790d502008-04-03 16:29:03 -04004148 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004149 }
Chris Mason0b86a832008-03-24 15:01:56 -04004150 return 0;
4151}
4152
Chris Masona4437552008-04-18 10:29:38 -04004153struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004154 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004155{
Yan Zheng2b820322008-11-17 21:11:30 -05004156 struct btrfs_device *device;
4157 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004158
Yan Zheng2b820322008-11-17 21:11:30 -05004159 cur_devices = root->fs_info->fs_devices;
4160 while (cur_devices) {
4161 if (!fsid ||
4162 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4163 device = __find_device(&cur_devices->devices,
4164 devid, uuid);
4165 if (device)
4166 return device;
4167 }
4168 cur_devices = cur_devices->seed;
4169 }
4170 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004171}
4172
Chris Masondfe25022008-05-13 13:46:40 -04004173static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4174 u64 devid, u8 *dev_uuid)
4175{
4176 struct btrfs_device *device;
4177 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4178
4179 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004180 if (!device)
4181 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004182 list_add(&device->dev_list,
4183 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004184 device->dev_root = root->fs_info->dev_root;
4185 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004186 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004187 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004188 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004189 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004190 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004191 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004192 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004193 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4194 return device;
4195}
4196
Chris Mason0b86a832008-03-24 15:01:56 -04004197static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4198 struct extent_buffer *leaf,
4199 struct btrfs_chunk *chunk)
4200{
4201 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4202 struct map_lookup *map;
4203 struct extent_map *em;
4204 u64 logical;
4205 u64 length;
4206 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004207 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004208 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004209 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004210 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004211
Chris Masone17cade2008-04-15 15:41:47 -04004212 logical = key->offset;
4213 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004214
Chris Mason890871b2009-09-02 16:24:52 -04004215 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004216 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004217 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004218
4219 /* already mapped? */
4220 if (em && em->start <= logical && em->start + em->len > logical) {
4221 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004222 return 0;
4223 } else if (em) {
4224 free_extent_map(em);
4225 }
Chris Mason0b86a832008-03-24 15:01:56 -04004226
David Sterba172ddd62011-04-21 00:48:27 +02004227 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004228 if (!em)
4229 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004230 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4231 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004232 if (!map) {
4233 free_extent_map(em);
4234 return -ENOMEM;
4235 }
4236
4237 em->bdev = (struct block_device *)map;
4238 em->start = logical;
4239 em->len = length;
4240 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004241 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004242
Chris Mason593060d2008-03-25 16:50:33 -04004243 map->num_stripes = num_stripes;
4244 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4245 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4246 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4247 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4248 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004249 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004250 for (i = 0; i < num_stripes; i++) {
4251 map->stripes[i].physical =
4252 btrfs_stripe_offset_nr(leaf, chunk, i);
4253 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004254 read_extent_buffer(leaf, uuid, (unsigned long)
4255 btrfs_stripe_dev_uuid_nr(chunk, i),
4256 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004257 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4258 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004259 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004260 kfree(map);
4261 free_extent_map(em);
4262 return -EIO;
4263 }
Chris Masondfe25022008-05-13 13:46:40 -04004264 if (!map->stripes[i].dev) {
4265 map->stripes[i].dev =
4266 add_missing_dev(root, devid, uuid);
4267 if (!map->stripes[i].dev) {
4268 kfree(map);
4269 free_extent_map(em);
4270 return -EIO;
4271 }
4272 }
4273 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004274 }
4275
Chris Mason890871b2009-09-02 16:24:52 -04004276 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004277 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004278 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004279 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004280 free_extent_map(em);
4281
4282 return 0;
4283}
4284
Jeff Mahoney143bede2012-03-01 14:56:26 +01004285static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004286 struct btrfs_dev_item *dev_item,
4287 struct btrfs_device *device)
4288{
4289 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004290
4291 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004292 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4293 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004294 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4295 device->type = btrfs_device_type(leaf, dev_item);
4296 device->io_align = btrfs_device_io_align(leaf, dev_item);
4297 device->io_width = btrfs_device_io_width(leaf, dev_item);
4298 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004299
4300 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004301 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004302}
4303
Yan Zheng2b820322008-11-17 21:11:30 -05004304static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4305{
4306 struct btrfs_fs_devices *fs_devices;
4307 int ret;
4308
Li Zefanb367e472011-12-07 11:38:24 +08004309 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004310
4311 fs_devices = root->fs_info->fs_devices->seed;
4312 while (fs_devices) {
4313 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4314 ret = 0;
4315 goto out;
4316 }
4317 fs_devices = fs_devices->seed;
4318 }
4319
4320 fs_devices = find_fsid(fsid);
4321 if (!fs_devices) {
4322 ret = -ENOENT;
4323 goto out;
4324 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004325
4326 fs_devices = clone_fs_devices(fs_devices);
4327 if (IS_ERR(fs_devices)) {
4328 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004329 goto out;
4330 }
4331
Christoph Hellwig97288f22008-12-02 06:36:09 -05004332 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004333 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05004334 if (ret)
4335 goto out;
4336
4337 if (!fs_devices->seeding) {
4338 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004339 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004340 ret = -EINVAL;
4341 goto out;
4342 }
4343
4344 fs_devices->seed = root->fs_info->fs_devices->seed;
4345 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004346out:
Yan Zheng2b820322008-11-17 21:11:30 -05004347 return ret;
4348}
4349
Chris Mason0d81ba52008-03-24 15:02:07 -04004350static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004351 struct extent_buffer *leaf,
4352 struct btrfs_dev_item *dev_item)
4353{
4354 struct btrfs_device *device;
4355 u64 devid;
4356 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004357 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004358 u8 dev_uuid[BTRFS_UUID_SIZE];
4359
Chris Mason0b86a832008-03-24 15:01:56 -04004360 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004361 read_extent_buffer(leaf, dev_uuid,
4362 (unsigned long)btrfs_device_uuid(dev_item),
4363 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004364 read_extent_buffer(leaf, fs_uuid,
4365 (unsigned long)btrfs_device_fsid(dev_item),
4366 BTRFS_UUID_SIZE);
4367
4368 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4369 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004370 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004371 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004372 }
4373
4374 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4375 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004376 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004377 return -EIO;
4378
4379 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004380 printk(KERN_WARNING "warning devid %llu missing\n",
4381 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004382 device = add_missing_dev(root, devid, dev_uuid);
4383 if (!device)
4384 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004385 } else if (!device->missing) {
4386 /*
4387 * this happens when a device that was properly setup
4388 * in the device info lists suddenly goes bad.
4389 * device->bdev is NULL, and so we have to set
4390 * device->missing to one here
4391 */
4392 root->fs_info->fs_devices->missing_devices++;
4393 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004394 }
4395 }
4396
4397 if (device->fs_devices != root->fs_info->fs_devices) {
4398 BUG_ON(device->writeable);
4399 if (device->generation !=
4400 btrfs_device_generation(leaf, dev_item))
4401 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004402 }
Chris Mason0b86a832008-03-24 15:01:56 -04004403
4404 fill_device_from_item(leaf, dev_item, device);
4405 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004406 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004407 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004408 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004409 spin_lock(&root->fs_info->free_chunk_lock);
4410 root->fs_info->free_chunk_space += device->total_bytes -
4411 device->bytes_used;
4412 spin_unlock(&root->fs_info->free_chunk_lock);
4413 }
Chris Mason0b86a832008-03-24 15:01:56 -04004414 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004415 return ret;
4416}
4417
Yan Zhenge4404d62008-12-12 10:03:26 -05004418int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004419{
David Sterba6c417612011-04-13 15:41:04 +02004420 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004421 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004422 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004423 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004424 u8 *ptr;
4425 unsigned long sb_ptr;
4426 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004427 u32 num_stripes;
4428 u32 array_size;
4429 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004430 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004431 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004432
Yan Zhenge4404d62008-12-12 10:03:26 -05004433 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004434 BTRFS_SUPER_INFO_SIZE);
4435 if (!sb)
4436 return -ENOMEM;
4437 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004438 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004439 /*
4440 * The sb extent buffer is artifical and just used to read the system array.
4441 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4442 * pages up-to-date when the page is larger: extent does not cover the
4443 * whole page and consequently check_page_uptodate does not find all
4444 * the page's extents up-to-date (the hole beyond sb),
4445 * write_extent_buffer then triggers a WARN_ON.
4446 *
4447 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4448 * but sb spans only this function. Add an explicit SetPageUptodate call
4449 * to silence the warning eg. on PowerPC 64.
4450 */
4451 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004452 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004453
Chris Masona061fc82008-05-07 11:43:44 -04004454 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004455 array_size = btrfs_super_sys_array_size(super_copy);
4456
Chris Mason0b86a832008-03-24 15:01:56 -04004457 ptr = super_copy->sys_chunk_array;
4458 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4459 cur = 0;
4460
4461 while (cur < array_size) {
4462 disk_key = (struct btrfs_disk_key *)ptr;
4463 btrfs_disk_key_to_cpu(&key, disk_key);
4464
Chris Masona061fc82008-05-07 11:43:44 -04004465 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004466 sb_ptr += len;
4467 cur += len;
4468
Chris Mason0d81ba52008-03-24 15:02:07 -04004469 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004470 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004471 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004472 if (ret)
4473 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004474 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4475 len = btrfs_chunk_item_size(num_stripes);
4476 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004477 ret = -EIO;
4478 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004479 }
4480 ptr += len;
4481 sb_ptr += len;
4482 cur += len;
4483 }
Chris Masona061fc82008-05-07 11:43:44 -04004484 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004485 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004486}
4487
4488int btrfs_read_chunk_tree(struct btrfs_root *root)
4489{
4490 struct btrfs_path *path;
4491 struct extent_buffer *leaf;
4492 struct btrfs_key key;
4493 struct btrfs_key found_key;
4494 int ret;
4495 int slot;
4496
4497 root = root->fs_info->chunk_root;
4498
4499 path = btrfs_alloc_path();
4500 if (!path)
4501 return -ENOMEM;
4502
Li Zefanb367e472011-12-07 11:38:24 +08004503 mutex_lock(&uuid_mutex);
4504 lock_chunks(root);
4505
Chris Mason0b86a832008-03-24 15:01:56 -04004506 /* first we search for all of the device items, and then we
4507 * read in all of the chunk items. This way we can create chunk
4508 * mappings that reference all of the devices that are afound
4509 */
4510 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4511 key.offset = 0;
4512 key.type = 0;
4513again:
4514 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004515 if (ret < 0)
4516 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004517 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004518 leaf = path->nodes[0];
4519 slot = path->slots[0];
4520 if (slot >= btrfs_header_nritems(leaf)) {
4521 ret = btrfs_next_leaf(root, path);
4522 if (ret == 0)
4523 continue;
4524 if (ret < 0)
4525 goto error;
4526 break;
4527 }
4528 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4529 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4530 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4531 break;
4532 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4533 struct btrfs_dev_item *dev_item;
4534 dev_item = btrfs_item_ptr(leaf, slot,
4535 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004536 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004537 if (ret)
4538 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004539 }
4540 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4541 struct btrfs_chunk *chunk;
4542 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4543 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004544 if (ret)
4545 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004546 }
4547 path->slots[0]++;
4548 }
4549 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4550 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004551 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004552 goto again;
4553 }
Chris Mason0b86a832008-03-24 15:01:56 -04004554 ret = 0;
4555error:
Li Zefanb367e472011-12-07 11:38:24 +08004556 unlock_chunks(root);
4557 mutex_unlock(&uuid_mutex);
4558
Yan Zheng2b820322008-11-17 21:11:30 -05004559 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004560 return ret;
4561}