blob: d5dc63c6bb75f8a0278b1f26cb23183a72966479 [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 Mason492bb6d2008-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 Masond85c8a62011-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;
Thomas Gleixnerd0e44ed2013-02-20 14:06:20 -0500560 spin_lock_init(&new_device->io_lock);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000561 list_replace_rcu(&device->dev_list, &new_device->dev_list);
562
563 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400564 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000565 mutex_unlock(&fs_devices->device_list_mutex);
566
Yan Zhenge4404d62008-12-12 10:03:26 -0500567 WARN_ON(fs_devices->open_devices);
568 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500569 fs_devices->opened = 0;
570 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500571
Chris Mason8a4b83c2008-03-24 15:02:07 -0400572 return 0;
573}
574
Yan Zheng2b820322008-11-17 21:11:30 -0500575int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
576{
Yan Zhenge4404d62008-12-12 10:03:26 -0500577 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500578 int ret;
579
580 mutex_lock(&uuid_mutex);
581 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500582 if (!fs_devices->opened) {
583 seed_devices = fs_devices->seed;
584 fs_devices->seed = NULL;
585 }
Yan Zheng2b820322008-11-17 21:11:30 -0500586 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500587
588 while (seed_devices) {
589 fs_devices = seed_devices;
590 seed_devices = fs_devices->seed;
591 __btrfs_close_devices(fs_devices);
592 free_fs_devices(fs_devices);
593 }
Eric Sandeena40a9452013-03-09 15:18:39 +0000594 /*
595 * Wait for rcu kworkers under __btrfs_close_devices
596 * to finish all blkdev_puts so device is really
597 * free when umount is done.
598 */
599 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500600 return ret;
601}
602
Yan Zhenge4404d62008-12-12 10:03:26 -0500603static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
604 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400605{
Josef Bacikd5e20032011-08-04 14:52:27 +0000606 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400607 struct block_device *bdev;
608 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400609 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400610 struct block_device *latest_bdev = NULL;
611 struct buffer_head *bh;
612 struct btrfs_super_block *disk_super;
613 u64 latest_devid = 0;
614 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400615 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500616 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400617 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400618
Tejun Heod4d77622010-11-13 11:55:18 +0100619 flags |= FMODE_EXCL;
620
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500621 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400622 if (device->bdev)
623 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400624 if (!device->name)
625 continue;
626
Tejun Heod4d77622010-11-13 11:55:18 +0100627 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400628 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500629 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400630 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400631 }
Chris Mason3c4bb262012-03-27 18:56:56 -0400632 filemap_write_and_wait(bdev->bd_inode->i_mapping);
633 invalidate_bdev(bdev);
Chris Masona061fc82008-05-07 11:43:44 -0400634 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400635
Yan Zhenga512bbf2008-12-08 16:46:26 -0500636 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300637 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400638 goto error_close;
639
640 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000641 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400642 if (devid != device->devid)
643 goto error_brelse;
644
Yan Zheng2b820322008-11-17 21:11:30 -0500645 if (memcmp(device->uuid, disk_super->dev_item.uuid,
646 BTRFS_UUID_SIZE))
647 goto error_brelse;
648
649 device->generation = btrfs_super_generation(disk_super);
650 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400651 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500652 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400653 latest_bdev = bdev;
654 }
655
Yan Zheng2b820322008-11-17 21:11:30 -0500656 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
657 device->writeable = 0;
658 } else {
659 device->writeable = !bdev_read_only(bdev);
660 seeding = 0;
661 }
662
Josef Bacikd5e20032011-08-04 14:52:27 +0000663 q = bdev_get_queue(bdev);
664 if (blk_queue_discard(q)) {
665 device->can_discard = 1;
666 fs_devices->num_can_discard++;
667 }
668
Chris Mason8a4b83c2008-03-24 15:02:07 -0400669 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400670 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500671 device->mode = flags;
672
Chris Masonc2898112009-06-10 09:51:32 -0400673 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
674 fs_devices->rotating = 1;
675
Chris Masona0af4692008-05-13 16:03:06 -0400676 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500677 if (device->writeable) {
678 fs_devices->rw_devices++;
679 list_add(&device->dev_alloc_list,
680 &fs_devices->alloc_list);
681 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000682 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400683 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400684
Chris Masona0af4692008-05-13 16:03:06 -0400685error_brelse:
686 brelse(bh);
687error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100688 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400689error:
690 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400691 }
Chris Masona0af4692008-05-13 16:03:06 -0400692 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300693 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400694 goto out;
695 }
Yan Zheng2b820322008-11-17 21:11:30 -0500696 fs_devices->seeding = seeding;
697 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400698 fs_devices->latest_bdev = latest_bdev;
699 fs_devices->latest_devid = latest_devid;
700 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500701 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400702out:
Yan Zheng2b820322008-11-17 21:11:30 -0500703 return ret;
704}
705
706int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500707 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500708{
709 int ret;
710
711 mutex_lock(&uuid_mutex);
712 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500713 fs_devices->opened++;
714 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500715 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500716 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500717 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400718 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400719 return ret;
720}
721
Christoph Hellwig97288f22008-12-02 06:36:09 -0500722int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400723 struct btrfs_fs_devices **fs_devices_ret)
724{
725 struct btrfs_super_block *disk_super;
726 struct block_device *bdev;
727 struct buffer_head *bh;
728 int ret;
729 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400730 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400731
Tejun Heod4d77622010-11-13 11:55:18 +0100732 flags |= FMODE_EXCL;
733 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400734
735 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400736 ret = PTR_ERR(bdev);
737 goto error;
738 }
739
Al Viro10f63272011-11-17 15:05:22 -0500740 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400741 ret = set_blocksize(bdev, 4096);
742 if (ret)
743 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500744 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400745 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000746 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400747 goto error_close;
748 }
749 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000750 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400751 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400752 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500753 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200754 else
755 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500756 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500757 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
759
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760 brelse(bh);
761error_close:
Al Viro10f63272011-11-17 15:05:22 -0500762 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100763 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400764error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400765 return ret;
766}
Chris Mason0b86a832008-03-24 15:01:56 -0400767
Miao Xie6d07bce2011-01-05 10:07:31 +0000768/* helper to account the used device space in the range */
769int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
770 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400771{
772 struct btrfs_key key;
773 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000774 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500775 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000776 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400777 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000778 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400779 struct extent_buffer *l;
780
Miao Xie6d07bce2011-01-05 10:07:31 +0000781 *length = 0;
782
783 if (start >= device->total_bytes)
784 return 0;
785
Yan Zheng2b820322008-11-17 21:11:30 -0500786 path = btrfs_alloc_path();
787 if (!path)
788 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400789 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400790
Chris Mason0b86a832008-03-24 15:01:56 -0400791 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000792 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400793 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000794
795 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400796 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000797 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400798 if (ret > 0) {
799 ret = btrfs_previous_item(root, path, key.objectid, key.type);
800 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000801 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400802 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000803
Chris Mason0b86a832008-03-24 15:01:56 -0400804 while (1) {
805 l = path->nodes[0];
806 slot = path->slots[0];
807 if (slot >= btrfs_header_nritems(l)) {
808 ret = btrfs_next_leaf(root, path);
809 if (ret == 0)
810 continue;
811 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000812 goto out;
813
814 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400815 }
816 btrfs_item_key_to_cpu(l, &key, slot);
817
818 if (key.objectid < device->devid)
819 goto next;
820
821 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000822 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400823
Chris Masond3977122009-01-05 21:25:51 -0500824 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400825 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400826
Chris Mason0b86a832008-03-24 15:01:56 -0400827 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000828 extent_end = key.offset + btrfs_dev_extent_length(l,
829 dev_extent);
830 if (key.offset <= start && extent_end > end) {
831 *length = end - start + 1;
832 break;
833 } else if (key.offset <= start && extent_end > start)
834 *length += extent_end - start;
835 else if (key.offset > start && extent_end <= end)
836 *length += extent_end - key.offset;
837 else if (key.offset > start && key.offset <= end) {
838 *length += end - key.offset + 1;
839 break;
840 } else if (key.offset > end)
841 break;
842
843next:
844 path->slots[0]++;
845 }
846 ret = 0;
847out:
848 btrfs_free_path(path);
849 return ret;
850}
851
Chris Mason0b86a832008-03-24 15:01:56 -0400852/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000853 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000854 * @device: the device which we search the free space in
855 * @num_bytes: the size of the free space that we need
856 * @start: store the start of the free space.
857 * @len: the size of the free space. that we find, or the size of the max
858 * free space if we don't find suitable free space
859 *
Chris Mason0b86a832008-03-24 15:01:56 -0400860 * this uses a pretty simple search, the expectation is that it is
861 * called very infrequently and that a given device has a small number
862 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000863 *
864 * @start is used to store the start of the free space if we find. But if we
865 * don't find suitable free space, it will be used to store the start position
866 * of the max free space.
867 *
868 * @len is used to store the size of the free space that we find.
869 * But if we don't find suitable free space, it is used to store the size of
870 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400871 */
Li Zefan125ccb02011-12-08 15:07:24 +0800872int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000873 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400874{
875 struct btrfs_key key;
876 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000877 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400878 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000879 u64 hole_size;
880 u64 max_hole_start;
881 u64 max_hole_size;
882 u64 extent_end;
883 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400884 u64 search_end = device->total_bytes;
885 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000886 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400887 struct extent_buffer *l;
888
Chris Mason0b86a832008-03-24 15:01:56 -0400889 /* FIXME use last free of some kind */
890
891 /* we don't want to overwrite the superblock on the drive,
892 * so we make sure to start at an offset of at least 1MB
893 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200894 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400895
Miao Xie7bfc8372011-01-05 10:07:26 +0000896 max_hole_start = search_start;
897 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000898 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000899
900 if (search_start >= search_end) {
901 ret = -ENOSPC;
902 goto error;
903 }
904
905 path = btrfs_alloc_path();
906 if (!path) {
907 ret = -ENOMEM;
908 goto error;
909 }
910 path->reada = 2;
911
Chris Mason0b86a832008-03-24 15:01:56 -0400912 key.objectid = device->devid;
913 key.offset = search_start;
914 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000915
Li Zefan125ccb02011-12-08 15:07:24 +0800916 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400917 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000918 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400919 if (ret > 0) {
920 ret = btrfs_previous_item(root, path, key.objectid, key.type);
921 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000922 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400923 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000924
Chris Mason0b86a832008-03-24 15:01:56 -0400925 while (1) {
926 l = path->nodes[0];
927 slot = path->slots[0];
928 if (slot >= btrfs_header_nritems(l)) {
929 ret = btrfs_next_leaf(root, path);
930 if (ret == 0)
931 continue;
932 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000933 goto out;
934
935 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400936 }
937 btrfs_item_key_to_cpu(l, &key, slot);
938
939 if (key.objectid < device->devid)
940 goto next;
941
942 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000943 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400944
Chris Mason0b86a832008-03-24 15:01:56 -0400945 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
946 goto next;
947
Miao Xie7bfc8372011-01-05 10:07:26 +0000948 if (key.offset > search_start) {
949 hole_size = key.offset - search_start;
950
951 if (hole_size > max_hole_size) {
952 max_hole_start = search_start;
953 max_hole_size = hole_size;
954 }
955
956 /*
957 * If this free space is greater than which we need,
958 * it must be the max free space that we have found
959 * until now, so max_hole_start must point to the start
960 * of this free space and the length of this free space
961 * is stored in max_hole_size. Thus, we return
962 * max_hole_start and max_hole_size and go back to the
963 * caller.
964 */
965 if (hole_size >= num_bytes) {
966 ret = 0;
967 goto out;
968 }
969 }
970
Chris Mason0b86a832008-03-24 15:01:56 -0400971 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000972 extent_end = key.offset + btrfs_dev_extent_length(l,
973 dev_extent);
974 if (extent_end > search_start)
975 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400976next:
977 path->slots[0]++;
978 cond_resched();
979 }
Chris Mason0b86a832008-03-24 15:01:56 -0400980
liubo38c01b92011-08-02 02:39:03 +0000981 /*
982 * At this point, search_start should be the end of
983 * allocated dev extents, and when shrinking the device,
984 * search_end may be smaller than search_start.
985 */
986 if (search_end > search_start)
987 hole_size = search_end - search_start;
988
Miao Xie7bfc8372011-01-05 10:07:26 +0000989 if (hole_size > max_hole_size) {
990 max_hole_start = search_start;
991 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400992 }
Chris Mason0b86a832008-03-24 15:01:56 -0400993
Miao Xie7bfc8372011-01-05 10:07:26 +0000994 /* See above. */
995 if (hole_size < num_bytes)
996 ret = -ENOSPC;
997 else
998 ret = 0;
999
1000out:
Yan Zheng2b820322008-11-17 21:11:30 -05001001 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001002error:
1003 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001004 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001005 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001006 return ret;
1007}
1008
Christoph Hellwigb2950862008-12-02 09:54:17 -05001009static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001010 struct btrfs_device *device,
1011 u64 start)
1012{
1013 int ret;
1014 struct btrfs_path *path;
1015 struct btrfs_root *root = device->dev_root;
1016 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001017 struct btrfs_key found_key;
1018 struct extent_buffer *leaf = NULL;
1019 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001020
1021 path = btrfs_alloc_path();
1022 if (!path)
1023 return -ENOMEM;
1024
1025 key.objectid = device->devid;
1026 key.offset = start;
1027 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001028again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001029 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001030 if (ret > 0) {
1031 ret = btrfs_previous_item(root, path, key.objectid,
1032 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001033 if (ret)
1034 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001035 leaf = path->nodes[0];
1036 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1037 extent = btrfs_item_ptr(leaf, path->slots[0],
1038 struct btrfs_dev_extent);
1039 BUG_ON(found_key.offset > start || found_key.offset +
1040 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001041 key = found_key;
1042 btrfs_release_path(path);
1043 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001044 } else if (ret == 0) {
1045 leaf = path->nodes[0];
1046 extent = btrfs_item_ptr(leaf, path->slots[0],
1047 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001048 } else {
1049 btrfs_error(root->fs_info, ret, "Slot search failed");
1050 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001051 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001052
Josef Bacik2bf64752011-09-26 17:12:22 -04001053 if (device->bytes_used > 0) {
1054 u64 len = btrfs_dev_extent_length(leaf, extent);
1055 device->bytes_used -= len;
1056 spin_lock(&root->fs_info->free_chunk_lock);
1057 root->fs_info->free_chunk_space += len;
1058 spin_unlock(&root->fs_info->free_chunk_lock);
1059 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001060 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001061 if (ret) {
1062 btrfs_error(root->fs_info, ret,
1063 "Failed to remove dev extent item");
1064 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001065out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001066 btrfs_free_path(path);
1067 return ret;
1068}
1069
Yan Zheng2b820322008-11-17 21:11:30 -05001070int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001071 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001072 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001073 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001074{
1075 int ret;
1076 struct btrfs_path *path;
1077 struct btrfs_root *root = device->dev_root;
1078 struct btrfs_dev_extent *extent;
1079 struct extent_buffer *leaf;
1080 struct btrfs_key key;
1081
Chris Masondfe25022008-05-13 13:46:40 -04001082 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001083 path = btrfs_alloc_path();
1084 if (!path)
1085 return -ENOMEM;
1086
Chris Mason0b86a832008-03-24 15:01:56 -04001087 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001088 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001089 key.type = BTRFS_DEV_EXTENT_KEY;
1090 ret = btrfs_insert_empty_item(trans, root, path, &key,
1091 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001092 if (ret)
1093 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001094
1095 leaf = path->nodes[0];
1096 extent = btrfs_item_ptr(leaf, path->slots[0],
1097 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001098 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1099 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1100 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1101
1102 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1103 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1104 BTRFS_UUID_SIZE);
1105
Chris Mason0b86a832008-03-24 15:01:56 -04001106 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1107 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001108out:
Chris Mason0b86a832008-03-24 15:01:56 -04001109 btrfs_free_path(path);
1110 return ret;
1111}
1112
Chris Masona1b32a52008-09-05 16:09:51 -04001113static noinline int find_next_chunk(struct btrfs_root *root,
1114 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001115{
1116 struct btrfs_path *path;
1117 int ret;
1118 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001119 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001120 struct btrfs_key found_key;
1121
1122 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001123 if (!path)
1124 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001125
Chris Masone17cade2008-04-15 15:41:47 -04001126 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001127 key.offset = (u64)-1;
1128 key.type = BTRFS_CHUNK_ITEM_KEY;
1129
1130 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1131 if (ret < 0)
1132 goto error;
1133
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001134 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001135
1136 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1137 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001138 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001139 } else {
1140 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1141 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001142 if (found_key.objectid != objectid)
1143 *offset = 0;
1144 else {
1145 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1146 struct btrfs_chunk);
1147 *offset = found_key.offset +
1148 btrfs_chunk_length(path->nodes[0], chunk);
1149 }
Chris Mason0b86a832008-03-24 15:01:56 -04001150 }
1151 ret = 0;
1152error:
1153 btrfs_free_path(path);
1154 return ret;
1155}
1156
Yan Zheng2b820322008-11-17 21:11:30 -05001157static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001158{
1159 int ret;
1160 struct btrfs_key key;
1161 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001162 struct btrfs_path *path;
1163
1164 root = root->fs_info->chunk_root;
1165
1166 path = btrfs_alloc_path();
1167 if (!path)
1168 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001169
1170 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1171 key.type = BTRFS_DEV_ITEM_KEY;
1172 key.offset = (u64)-1;
1173
1174 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1175 if (ret < 0)
1176 goto error;
1177
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001178 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001179
1180 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1181 BTRFS_DEV_ITEM_KEY);
1182 if (ret) {
1183 *objectid = 1;
1184 } else {
1185 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1186 path->slots[0]);
1187 *objectid = found_key.offset + 1;
1188 }
1189 ret = 0;
1190error:
Yan Zheng2b820322008-11-17 21:11:30 -05001191 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001192 return ret;
1193}
1194
1195/*
1196 * the device information is stored in the chunk root
1197 * the btrfs_device struct should be fully filled in
1198 */
1199int btrfs_add_device(struct btrfs_trans_handle *trans,
1200 struct btrfs_root *root,
1201 struct btrfs_device *device)
1202{
1203 int ret;
1204 struct btrfs_path *path;
1205 struct btrfs_dev_item *dev_item;
1206 struct extent_buffer *leaf;
1207 struct btrfs_key key;
1208 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001209
1210 root = root->fs_info->chunk_root;
1211
1212 path = btrfs_alloc_path();
1213 if (!path)
1214 return -ENOMEM;
1215
Chris Mason0b86a832008-03-24 15:01:56 -04001216 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1217 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001218 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001219
1220 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001221 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001222 if (ret)
1223 goto out;
1224
1225 leaf = path->nodes[0];
1226 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1227
1228 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001229 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001230 btrfs_set_device_type(leaf, dev_item, device->type);
1231 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1232 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1233 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001234 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1235 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001236 btrfs_set_device_group(leaf, dev_item, 0);
1237 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1238 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001239 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001240
Chris Mason0b86a832008-03-24 15:01:56 -04001241 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001242 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001243 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1244 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001245 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001246
Yan Zheng2b820322008-11-17 21:11:30 -05001247 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001248out:
1249 btrfs_free_path(path);
1250 return ret;
1251}
Chris Mason8f18cf12008-04-25 16:53:30 -04001252
Chris Masona061fc82008-05-07 11:43:44 -04001253static int btrfs_rm_dev_item(struct btrfs_root *root,
1254 struct btrfs_device *device)
1255{
1256 int ret;
1257 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001258 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001259 struct btrfs_trans_handle *trans;
1260
1261 root = root->fs_info->chunk_root;
1262
1263 path = btrfs_alloc_path();
1264 if (!path)
1265 return -ENOMEM;
1266
Yan, Zhenga22285a2010-05-16 10:48:46 -04001267 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001268 if (IS_ERR(trans)) {
1269 btrfs_free_path(path);
1270 return PTR_ERR(trans);
1271 }
Chris Masona061fc82008-05-07 11:43:44 -04001272 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1273 key.type = BTRFS_DEV_ITEM_KEY;
1274 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001275 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001276
1277 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1278 if (ret < 0)
1279 goto out;
1280
1281 if (ret > 0) {
1282 ret = -ENOENT;
1283 goto out;
1284 }
1285
1286 ret = btrfs_del_item(trans, root, path);
1287 if (ret)
1288 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001289out:
1290 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001291 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001292 btrfs_commit_transaction(trans, root);
1293 return ret;
1294}
1295
1296int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1297{
1298 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001299 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001300 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001301 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001302 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001303 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001304 u64 all_avail;
1305 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001306 u64 num_devices;
1307 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001308 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001309 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001310
Chris Masona061fc82008-05-07 11:43:44 -04001311 mutex_lock(&uuid_mutex);
1312
1313 all_avail = root->fs_info->avail_data_alloc_bits |
1314 root->fs_info->avail_system_alloc_bits |
1315 root->fs_info->avail_metadata_alloc_bits;
1316
1317 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001318 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001319 printk(KERN_ERR "btrfs: unable to go below four devices "
1320 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001321 ret = -EINVAL;
1322 goto out;
1323 }
1324
1325 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001326 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001327 printk(KERN_ERR "btrfs: unable to go below two "
1328 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001329 ret = -EINVAL;
1330 goto out;
1331 }
1332
Chris Masondfe25022008-05-13 13:46:40 -04001333 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001334 struct list_head *devices;
1335 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001336
Chris Masondfe25022008-05-13 13:46:40 -04001337 device = NULL;
1338 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001339 /*
1340 * It is safe to read the devices since the volume_mutex
1341 * is held.
1342 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001343 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001344 if (tmp->in_fs_metadata && !tmp->bdev) {
1345 device = tmp;
1346 break;
1347 }
1348 }
1349 bdev = NULL;
1350 bh = NULL;
1351 disk_super = NULL;
1352 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001353 printk(KERN_ERR "btrfs: no missing devices found to "
1354 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001355 goto out;
1356 }
Chris Masondfe25022008-05-13 13:46:40 -04001357 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001358 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1359 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001360 if (IS_ERR(bdev)) {
1361 ret = PTR_ERR(bdev);
1362 goto out;
1363 }
1364
Yan Zheng2b820322008-11-17 21:11:30 -05001365 set_blocksize(bdev, 4096);
Chris Mason3c4bb262012-03-27 18:56:56 -04001366 invalidate_bdev(bdev);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001367 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001368 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001369 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001370 goto error_close;
1371 }
1372 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001373 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001374 dev_uuid = disk_super->dev_item.uuid;
1375 device = btrfs_find_device(root, devid, dev_uuid,
1376 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001377 if (!device) {
1378 ret = -ENOENT;
1379 goto error_brelse;
1380 }
Chris Masondfe25022008-05-13 13:46:40 -04001381 }
Yan Zheng2b820322008-11-17 21:11:30 -05001382
1383 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001384 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1385 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001386 ret = -EINVAL;
1387 goto error_brelse;
1388 }
1389
1390 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001391 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001392 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001393 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001394 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001395 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001396 }
Chris Masona061fc82008-05-07 11:43:44 -04001397
1398 ret = btrfs_shrink_device(device, 0);
1399 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001400 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001401
Chris Masona061fc82008-05-07 11:43:44 -04001402 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1403 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001404 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001405
Josef Bacik2bf64752011-09-26 17:12:22 -04001406 spin_lock(&root->fs_info->free_chunk_lock);
1407 root->fs_info->free_chunk_space = device->total_bytes -
1408 device->bytes_used;
1409 spin_unlock(&root->fs_info->free_chunk_lock);
1410
Yan Zheng2b820322008-11-17 21:11:30 -05001411 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001412 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001413
1414 /*
1415 * the device list mutex makes sure that we don't change
1416 * the device list while someone else is writing out all
1417 * the device supers.
1418 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001419
1420 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001421 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001422 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001423
Yan Zhenge4404d62008-12-12 10:03:26 -05001424 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001425
Chris Masoncd02dca2010-12-13 14:56:23 -05001426 if (device->missing)
1427 root->fs_info->fs_devices->missing_devices--;
1428
Yan Zheng2b820322008-11-17 21:11:30 -05001429 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1430 struct btrfs_device, dev_list);
1431 if (device->bdev == root->fs_info->sb->s_bdev)
1432 root->fs_info->sb->s_bdev = next_device->bdev;
1433 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1434 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1435
Xiao Guangrong1f781602011-04-20 10:09:16 +00001436 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001437 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001438
1439 call_rcu(&device->rcu, free_device);
1440 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001441
David Sterba6c417612011-04-13 15:41:04 +02001442 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1443 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001444
Xiao Guangrong1f781602011-04-20 10:09:16 +00001445 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001446 struct btrfs_fs_devices *fs_devices;
1447 fs_devices = root->fs_info->fs_devices;
1448 while (fs_devices) {
Rickard Strandqvist8f1dd632014-05-22 22:43:43 +02001449 if (fs_devices->seed == cur_devices) {
1450 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001451 break;
Rickard Strandqvist8f1dd632014-05-22 22:43:43 +02001452 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001453 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001454 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001455 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001456 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001457 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001458 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001459 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001460 }
1461
1462 /*
1463 * at this point, the device is zero sized. We want to
1464 * remove it from the devices list and zero out the old super
1465 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001466 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001467 /* make sure this device isn't detected as part of
1468 * the FS anymore
1469 */
1470 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1471 set_buffer_dirty(bh);
1472 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001473 }
Chris Masona061fc82008-05-07 11:43:44 -04001474
Chris Masona061fc82008-05-07 11:43:44 -04001475 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001476
1477error_brelse:
1478 brelse(bh);
1479error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001480 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001481 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001482out:
1483 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001484 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001485error_undo:
1486 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001487 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001488 list_add(&device->dev_alloc_list,
1489 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001490 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001491 root->fs_info->fs_devices->rw_devices++;
1492 }
1493 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001494}
1495
Yan Zheng2b820322008-11-17 21:11:30 -05001496/*
1497 * does all the dirty work required for changing file system's UUID.
1498 */
Li Zefan125ccb02011-12-08 15:07:24 +08001499static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001500{
1501 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1502 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001503 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001504 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001505 struct btrfs_device *device;
1506 u64 super_flags;
1507
1508 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001509 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001510 return -EINVAL;
1511
Yan Zhenge4404d62008-12-12 10:03:26 -05001512 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1513 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001514 return -ENOMEM;
1515
Yan Zhenge4404d62008-12-12 10:03:26 -05001516 old_devices = clone_fs_devices(fs_devices);
1517 if (IS_ERR(old_devices)) {
1518 kfree(seed_devices);
1519 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001520 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001521
Yan Zheng2b820322008-11-17 21:11:30 -05001522 list_add(&old_devices->list, &fs_uuids);
1523
Yan Zhenge4404d62008-12-12 10:03:26 -05001524 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1525 seed_devices->opened = 1;
1526 INIT_LIST_HEAD(&seed_devices->devices);
1527 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001528 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001529
1530 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001531 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1532 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001533 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1534
Yan Zhenge4404d62008-12-12 10:03:26 -05001535 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1536 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1537 device->fs_devices = seed_devices;
1538 }
1539
Yan Zheng2b820322008-11-17 21:11:30 -05001540 fs_devices->seeding = 0;
1541 fs_devices->num_devices = 0;
1542 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001543 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001544
1545 generate_random_uuid(fs_devices->fsid);
1546 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1547 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1548 super_flags = btrfs_super_flags(disk_super) &
1549 ~BTRFS_SUPER_FLAG_SEEDING;
1550 btrfs_set_super_flags(disk_super, super_flags);
1551
1552 return 0;
1553}
1554
1555/*
1556 * strore the expected generation for seed devices in device items.
1557 */
1558static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1559 struct btrfs_root *root)
1560{
1561 struct btrfs_path *path;
1562 struct extent_buffer *leaf;
1563 struct btrfs_dev_item *dev_item;
1564 struct btrfs_device *device;
1565 struct btrfs_key key;
1566 u8 fs_uuid[BTRFS_UUID_SIZE];
1567 u8 dev_uuid[BTRFS_UUID_SIZE];
1568 u64 devid;
1569 int ret;
1570
1571 path = btrfs_alloc_path();
1572 if (!path)
1573 return -ENOMEM;
1574
1575 root = root->fs_info->chunk_root;
1576 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1577 key.offset = 0;
1578 key.type = BTRFS_DEV_ITEM_KEY;
1579
1580 while (1) {
1581 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1582 if (ret < 0)
1583 goto error;
1584
1585 leaf = path->nodes[0];
1586next_slot:
1587 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1588 ret = btrfs_next_leaf(root, path);
1589 if (ret > 0)
1590 break;
1591 if (ret < 0)
1592 goto error;
1593 leaf = path->nodes[0];
1594 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001595 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001596 continue;
1597 }
1598
1599 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1600 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1601 key.type != BTRFS_DEV_ITEM_KEY)
1602 break;
1603
1604 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1605 struct btrfs_dev_item);
1606 devid = btrfs_device_id(leaf, dev_item);
1607 read_extent_buffer(leaf, dev_uuid,
1608 (unsigned long)btrfs_device_uuid(dev_item),
1609 BTRFS_UUID_SIZE);
1610 read_extent_buffer(leaf, fs_uuid,
1611 (unsigned long)btrfs_device_fsid(dev_item),
1612 BTRFS_UUID_SIZE);
1613 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001614 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001615
1616 if (device->fs_devices->seeding) {
1617 btrfs_set_device_generation(leaf, dev_item,
1618 device->generation);
1619 btrfs_mark_buffer_dirty(leaf);
1620 }
1621
1622 path->slots[0]++;
1623 goto next_slot;
1624 }
1625 ret = 0;
1626error:
1627 btrfs_free_path(path);
1628 return ret;
1629}
1630
Chris Mason788f20e2008-04-28 15:29:42 -04001631int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1632{
Josef Bacikd5e20032011-08-04 14:52:27 +00001633 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001634 struct btrfs_trans_handle *trans;
1635 struct btrfs_device *device;
1636 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001637 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001638 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001639 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001640 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001641 int ret = 0;
1642
Yan Zheng2b820322008-11-17 21:11:30 -05001643 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1644 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001645
Li Zefana5d16332011-12-07 20:08:40 -05001646 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001647 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001648 if (IS_ERR(bdev))
1649 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001650
Yan Zheng2b820322008-11-17 21:11:30 -05001651 if (root->fs_info->fs_devices->seeding) {
1652 seeding_dev = 1;
1653 down_write(&sb->s_umount);
1654 mutex_lock(&uuid_mutex);
1655 }
1656
Chris Mason8c8bee12008-09-29 11:19:10 -04001657 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001658
Chris Mason788f20e2008-04-28 15:29:42 -04001659 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001660 /*
1661 * we have the volume lock, so we don't need the extra
1662 * device list mutex while reading the list here.
1663 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001664 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001665 if (device->bdev == bdev) {
1666 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001667 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001668 }
1669 }
1670
1671 device = kzalloc(sizeof(*device), GFP_NOFS);
1672 if (!device) {
1673 /* we can safely leave the fs_devices entry around */
1674 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001675 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001676 }
1677
Chris Mason788f20e2008-04-28 15:29:42 -04001678 device->name = kstrdup(device_path, GFP_NOFS);
1679 if (!device->name) {
1680 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001681 ret = -ENOMEM;
1682 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001683 }
Yan Zheng2b820322008-11-17 21:11:30 -05001684
1685 ret = find_next_devid(root, &device->devid);
1686 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001687 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001688 kfree(device);
1689 goto error;
1690 }
1691
Yan, Zhenga22285a2010-05-16 10:48:46 -04001692 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001693 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001694 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001695 kfree(device);
1696 ret = PTR_ERR(trans);
1697 goto error;
1698 }
1699
Yan Zheng2b820322008-11-17 21:11:30 -05001700 lock_chunks(root);
1701
Josef Bacikd5e20032011-08-04 14:52:27 +00001702 q = bdev_get_queue(bdev);
1703 if (blk_queue_discard(q))
1704 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001705 device->writeable = 1;
1706 device->work.func = pending_bios_fn;
1707 generate_random_uuid(device->uuid);
1708 spin_lock_init(&device->io_lock);
1709 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001710 device->io_width = root->sectorsize;
1711 device->io_align = root->sectorsize;
1712 device->sector_size = root->sectorsize;
1713 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001714 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001715 device->dev_root = root->fs_info->dev_root;
1716 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001717 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001718 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001719 set_blocksize(device->bdev, 4096);
1720
Yan Zheng2b820322008-11-17 21:11:30 -05001721 if (seeding_dev) {
1722 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001723 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001724 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001725 }
1726
1727 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001728
1729 /*
1730 * we don't want write_supers to jump in here with our device
1731 * half setup
1732 */
1733 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001734 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001735 list_add(&device->dev_alloc_list,
1736 &root->fs_info->fs_devices->alloc_list);
1737 root->fs_info->fs_devices->num_devices++;
1738 root->fs_info->fs_devices->open_devices++;
1739 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001740 if (device->can_discard)
1741 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001742 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1743
Josef Bacik2bf64752011-09-26 17:12:22 -04001744 spin_lock(&root->fs_info->free_chunk_lock);
1745 root->fs_info->free_chunk_space += device->total_bytes;
1746 spin_unlock(&root->fs_info->free_chunk_lock);
1747
Chris Masonc2898112009-06-10 09:51:32 -04001748 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1749 root->fs_info->fs_devices->rotating = 1;
1750
David Sterba6c417612011-04-13 15:41:04 +02001751 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1752 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001753 total_bytes + device->total_bytes);
1754
David Sterba6c417612011-04-13 15:41:04 +02001755 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1756 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001757 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001758 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001759
Yan Zheng2b820322008-11-17 21:11:30 -05001760 if (seeding_dev) {
1761 ret = init_first_rw_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001762 if (ret)
1763 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001764 ret = btrfs_finish_sprout(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001765 if (ret)
1766 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001767 } else {
1768 ret = btrfs_add_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001769 if (ret)
1770 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001771 }
1772
Chris Mason913d9522009-03-10 13:17:18 -04001773 /*
1774 * we've got more storage, clear any full flags on the space
1775 * infos
1776 */
1777 btrfs_clear_space_info_full(root->fs_info);
1778
Chris Mason7d9eb122008-07-08 14:19:17 -04001779 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001780 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001781
1782 if (seeding_dev) {
1783 mutex_unlock(&uuid_mutex);
1784 up_write(&sb->s_umount);
1785
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001786 if (ret) /* transaction commit */
1787 return ret;
1788
Yan Zheng2b820322008-11-17 21:11:30 -05001789 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001790 if (ret < 0)
1791 btrfs_error(root->fs_info, ret,
1792 "Failed to relocate sys chunks after "
1793 "device initialization. This can be fixed "
1794 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001795 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001796
Chris Mason788f20e2008-04-28 15:29:42 -04001797 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001798
1799error_trans:
1800 unlock_chunks(root);
1801 btrfs_abort_transaction(trans, root, ret);
1802 btrfs_end_transaction(trans, root);
1803 kfree(device->name);
1804 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001805error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001806 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001807 if (seeding_dev) {
1808 mutex_unlock(&uuid_mutex);
1809 up_write(&sb->s_umount);
1810 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001811 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001812}
1813
Chris Masond3977122009-01-05 21:25:51 -05001814static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1815 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001816{
1817 int ret;
1818 struct btrfs_path *path;
1819 struct btrfs_root *root;
1820 struct btrfs_dev_item *dev_item;
1821 struct extent_buffer *leaf;
1822 struct btrfs_key key;
1823
1824 root = device->dev_root->fs_info->chunk_root;
1825
1826 path = btrfs_alloc_path();
1827 if (!path)
1828 return -ENOMEM;
1829
1830 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1831 key.type = BTRFS_DEV_ITEM_KEY;
1832 key.offset = device->devid;
1833
1834 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1835 if (ret < 0)
1836 goto out;
1837
1838 if (ret > 0) {
1839 ret = -ENOENT;
1840 goto out;
1841 }
1842
1843 leaf = path->nodes[0];
1844 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1845
1846 btrfs_set_device_id(leaf, dev_item, device->devid);
1847 btrfs_set_device_type(leaf, dev_item, device->type);
1848 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1849 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1850 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001851 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001852 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1853 btrfs_mark_buffer_dirty(leaf);
1854
1855out:
1856 btrfs_free_path(path);
1857 return ret;
1858}
1859
Chris Mason7d9eb122008-07-08 14:19:17 -04001860static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001861 struct btrfs_device *device, u64 new_size)
1862{
1863 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001864 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001865 u64 old_total = btrfs_super_total_bytes(super_copy);
1866 u64 diff = new_size - device->total_bytes;
1867
Yan Zheng2b820322008-11-17 21:11:30 -05001868 if (!device->writeable)
1869 return -EACCES;
1870 if (new_size <= device->total_bytes)
1871 return -EINVAL;
1872
Chris Mason8f18cf12008-04-25 16:53:30 -04001873 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001874 device->fs_devices->total_rw_bytes += diff;
1875
1876 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001877 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001878 btrfs_clear_space_info_full(device->dev_root->fs_info);
1879
Chris Mason8f18cf12008-04-25 16:53:30 -04001880 return btrfs_update_device(trans, device);
1881}
1882
Chris Mason7d9eb122008-07-08 14:19:17 -04001883int btrfs_grow_device(struct btrfs_trans_handle *trans,
1884 struct btrfs_device *device, u64 new_size)
1885{
1886 int ret;
1887 lock_chunks(device->dev_root);
1888 ret = __btrfs_grow_device(trans, device, new_size);
1889 unlock_chunks(device->dev_root);
1890 return ret;
1891}
1892
Chris Mason8f18cf12008-04-25 16:53:30 -04001893static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1894 struct btrfs_root *root,
1895 u64 chunk_tree, u64 chunk_objectid,
1896 u64 chunk_offset)
1897{
1898 int ret;
1899 struct btrfs_path *path;
1900 struct btrfs_key key;
1901
1902 root = root->fs_info->chunk_root;
1903 path = btrfs_alloc_path();
1904 if (!path)
1905 return -ENOMEM;
1906
1907 key.objectid = chunk_objectid;
1908 key.offset = chunk_offset;
1909 key.type = BTRFS_CHUNK_ITEM_KEY;
1910
1911 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001912 if (ret < 0)
1913 goto out;
1914 else if (ret > 0) { /* Logic error or corruption */
1915 btrfs_error(root->fs_info, -ENOENT,
1916 "Failed lookup while freeing chunk.");
1917 ret = -ENOENT;
1918 goto out;
1919 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001920
1921 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001922 if (ret < 0)
1923 btrfs_error(root->fs_info, ret,
1924 "Failed to delete chunk item.");
1925out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001926 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001927 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001928}
1929
Christoph Hellwigb2950862008-12-02 09:54:17 -05001930static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001931 chunk_offset)
1932{
David Sterba6c417612011-04-13 15:41:04 +02001933 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001934 struct btrfs_disk_key *disk_key;
1935 struct btrfs_chunk *chunk;
1936 u8 *ptr;
1937 int ret = 0;
1938 u32 num_stripes;
1939 u32 array_size;
1940 u32 len = 0;
1941 u32 cur;
1942 struct btrfs_key key;
1943
1944 array_size = btrfs_super_sys_array_size(super_copy);
1945
1946 ptr = super_copy->sys_chunk_array;
1947 cur = 0;
1948
1949 while (cur < array_size) {
1950 disk_key = (struct btrfs_disk_key *)ptr;
1951 btrfs_disk_key_to_cpu(&key, disk_key);
1952
1953 len = sizeof(*disk_key);
1954
1955 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1956 chunk = (struct btrfs_chunk *)(ptr + len);
1957 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1958 len += btrfs_chunk_item_size(num_stripes);
1959 } else {
1960 ret = -EIO;
1961 break;
1962 }
1963 if (key.objectid == chunk_objectid &&
1964 key.offset == chunk_offset) {
1965 memmove(ptr, ptr + len, array_size - (cur + len));
1966 array_size -= len;
1967 btrfs_set_super_sys_array_size(super_copy, array_size);
1968 } else {
1969 ptr += len;
1970 cur += len;
1971 }
1972 }
1973 return ret;
1974}
1975
Christoph Hellwigb2950862008-12-02 09:54:17 -05001976static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001977 u64 chunk_tree, u64 chunk_objectid,
1978 u64 chunk_offset)
1979{
1980 struct extent_map_tree *em_tree;
1981 struct btrfs_root *extent_root;
1982 struct btrfs_trans_handle *trans;
1983 struct extent_map *em;
1984 struct map_lookup *map;
1985 int ret;
1986 int i;
1987
1988 root = root->fs_info->chunk_root;
1989 extent_root = root->fs_info->extent_root;
1990 em_tree = &root->fs_info->mapping_tree.map_tree;
1991
Josef Bacikba1bf482009-09-11 16:11:19 -04001992 ret = btrfs_can_relocate(extent_root, chunk_offset);
1993 if (ret)
1994 return -ENOSPC;
1995
Chris Mason8f18cf12008-04-25 16:53:30 -04001996 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001997 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001998 if (ret)
1999 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002000
Yan, Zhenga22285a2010-05-16 10:48:46 -04002001 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002002 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002003
Chris Mason7d9eb122008-07-08 14:19:17 -04002004 lock_chunks(root);
2005
Chris Mason8f18cf12008-04-25 16:53:30 -04002006 /*
2007 * step two, delete the device extents and the
2008 * chunk tree entries
2009 */
Chris Mason890871b2009-09-02 16:24:52 -04002010 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002011 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002012 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002013
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002014 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002015 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002016 map = (struct map_lookup *)em->bdev;
2017
2018 for (i = 0; i < map->num_stripes; i++) {
2019 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2020 map->stripes[i].physical);
2021 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002022
Chris Masondfe25022008-05-13 13:46:40 -04002023 if (map->stripes[i].dev) {
2024 ret = btrfs_update_device(trans, map->stripes[i].dev);
2025 BUG_ON(ret);
2026 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002027 }
2028 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2029 chunk_offset);
2030
2031 BUG_ON(ret);
2032
liubo1abe9b82011-03-24 11:18:59 +00002033 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2034
Chris Mason8f18cf12008-04-25 16:53:30 -04002035 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2036 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2037 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002038 }
2039
Zheng Yan1a40e232008-09-26 10:09:34 -04002040 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2041 BUG_ON(ret);
2042
Chris Mason890871b2009-09-02 16:24:52 -04002043 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002044 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002045 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002046
Chris Mason8f18cf12008-04-25 16:53:30 -04002047 kfree(map);
2048 em->bdev = NULL;
2049
2050 /* once for the tree */
2051 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002052 /* once for us */
2053 free_extent_map(em);
2054
Chris Mason7d9eb122008-07-08 14:19:17 -04002055 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002056 btrfs_end_transaction(trans, root);
2057 return 0;
2058}
2059
Yan Zheng2b820322008-11-17 21:11:30 -05002060static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2061{
2062 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2063 struct btrfs_path *path;
2064 struct extent_buffer *leaf;
2065 struct btrfs_chunk *chunk;
2066 struct btrfs_key key;
2067 struct btrfs_key found_key;
2068 u64 chunk_tree = chunk_root->root_key.objectid;
2069 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002070 bool retried = false;
2071 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002072 int ret;
2073
2074 path = btrfs_alloc_path();
2075 if (!path)
2076 return -ENOMEM;
2077
Josef Bacikba1bf482009-09-11 16:11:19 -04002078again:
Yan Zheng2b820322008-11-17 21:11:30 -05002079 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2080 key.offset = (u64)-1;
2081 key.type = BTRFS_CHUNK_ITEM_KEY;
2082
2083 while (1) {
2084 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2085 if (ret < 0)
2086 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002087 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002088
2089 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2090 key.type);
2091 if (ret < 0)
2092 goto error;
2093 if (ret > 0)
2094 break;
2095
2096 leaf = path->nodes[0];
2097 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2098
2099 chunk = btrfs_item_ptr(leaf, path->slots[0],
2100 struct btrfs_chunk);
2101 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002102 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002103
2104 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2105 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2106 found_key.objectid,
2107 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002108 if (ret == -ENOSPC)
2109 failed++;
2110 else if (ret)
2111 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002112 }
2113
2114 if (found_key.offset == 0)
2115 break;
2116 key.offset = found_key.offset - 1;
2117 }
2118 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002119 if (failed && !retried) {
2120 failed = 0;
2121 retried = true;
2122 goto again;
2123 } else if (failed && retried) {
2124 WARN_ON(1);
2125 ret = -ENOSPC;
2126 }
Yan Zheng2b820322008-11-17 21:11:30 -05002127error:
2128 btrfs_free_path(path);
2129 return ret;
2130}
2131
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002132static int insert_balance_item(struct btrfs_root *root,
2133 struct btrfs_balance_control *bctl)
2134{
2135 struct btrfs_trans_handle *trans;
2136 struct btrfs_balance_item *item;
2137 struct btrfs_disk_balance_args disk_bargs;
2138 struct btrfs_path *path;
2139 struct extent_buffer *leaf;
2140 struct btrfs_key key;
2141 int ret, err;
2142
2143 path = btrfs_alloc_path();
2144 if (!path)
2145 return -ENOMEM;
2146
2147 trans = btrfs_start_transaction(root, 0);
2148 if (IS_ERR(trans)) {
2149 btrfs_free_path(path);
2150 return PTR_ERR(trans);
2151 }
2152
2153 key.objectid = BTRFS_BALANCE_OBJECTID;
2154 key.type = BTRFS_BALANCE_ITEM_KEY;
2155 key.offset = 0;
2156
2157 ret = btrfs_insert_empty_item(trans, root, path, &key,
2158 sizeof(*item));
2159 if (ret)
2160 goto out;
2161
2162 leaf = path->nodes[0];
2163 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2164
2165 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2166
2167 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2168 btrfs_set_balance_data(leaf, item, &disk_bargs);
2169 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2170 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2171 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2172 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2173
2174 btrfs_set_balance_flags(leaf, item, bctl->flags);
2175
2176 btrfs_mark_buffer_dirty(leaf);
2177out:
2178 btrfs_free_path(path);
2179 err = btrfs_commit_transaction(trans, root);
2180 if (err && !ret)
2181 ret = err;
2182 return ret;
2183}
2184
2185static int del_balance_item(struct btrfs_root *root)
2186{
2187 struct btrfs_trans_handle *trans;
2188 struct btrfs_path *path;
2189 struct btrfs_key key;
2190 int ret, err;
2191
2192 path = btrfs_alloc_path();
2193 if (!path)
2194 return -ENOMEM;
2195
2196 trans = btrfs_start_transaction(root, 0);
2197 if (IS_ERR(trans)) {
2198 btrfs_free_path(path);
2199 return PTR_ERR(trans);
2200 }
2201
2202 key.objectid = BTRFS_BALANCE_OBJECTID;
2203 key.type = BTRFS_BALANCE_ITEM_KEY;
2204 key.offset = 0;
2205
2206 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2207 if (ret < 0)
2208 goto out;
2209 if (ret > 0) {
2210 ret = -ENOENT;
2211 goto out;
2212 }
2213
2214 ret = btrfs_del_item(trans, root, path);
2215out:
2216 btrfs_free_path(path);
2217 err = btrfs_commit_transaction(trans, root);
2218 if (err && !ret)
2219 ret = err;
2220 return ret;
2221}
2222
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002223/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002224 * This is a heuristic used to reduce the number of chunks balanced on
2225 * resume after balance was interrupted.
2226 */
2227static void update_balance_args(struct btrfs_balance_control *bctl)
2228{
2229 /*
2230 * Turn on soft mode for chunk types that were being converted.
2231 */
2232 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2233 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2234 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2235 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2236 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2237 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2238
2239 /*
2240 * Turn on usage filter if is not already used. The idea is
2241 * that chunks that we have already balanced should be
2242 * reasonably full. Don't do it for chunks that are being
2243 * converted - that will keep us from relocating unconverted
2244 * (albeit full) chunks.
2245 */
2246 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2247 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2248 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2249 bctl->data.usage = 90;
2250 }
2251 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2252 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2253 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2254 bctl->sys.usage = 90;
2255 }
2256 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2257 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2258 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2259 bctl->meta.usage = 90;
2260 }
2261}
2262
2263/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002264 * Should be called with both balance and volume mutexes held to
2265 * serialize other volume operations (add_dev/rm_dev/resize) with
2266 * restriper. Same goes for unset_balance_control.
2267 */
2268static void set_balance_control(struct btrfs_balance_control *bctl)
2269{
2270 struct btrfs_fs_info *fs_info = bctl->fs_info;
2271
2272 BUG_ON(fs_info->balance_ctl);
2273
2274 spin_lock(&fs_info->balance_lock);
2275 fs_info->balance_ctl = bctl;
2276 spin_unlock(&fs_info->balance_lock);
2277}
2278
2279static void unset_balance_control(struct btrfs_fs_info *fs_info)
2280{
2281 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2282
2283 BUG_ON(!fs_info->balance_ctl);
2284
2285 spin_lock(&fs_info->balance_lock);
2286 fs_info->balance_ctl = NULL;
2287 spin_unlock(&fs_info->balance_lock);
2288
2289 kfree(bctl);
2290}
2291
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002292/*
2293 * Balance filters. Return 1 if chunk should be filtered out
2294 * (should not be balanced).
2295 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002296static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002297 struct btrfs_balance_args *bargs)
2298{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002299 chunk_type = chunk_to_extended(chunk_type) &
2300 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002301
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002302 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002303 return 0;
2304
2305 return 1;
2306}
2307
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002308static u64 div_factor_fine(u64 num, int factor)
2309{
2310 if (factor <= 0)
2311 return 0;
2312 if (factor >= 100)
2313 return num;
2314
2315 num *= factor;
2316 do_div(num, 100);
2317 return num;
2318}
2319
2320static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2321 struct btrfs_balance_args *bargs)
2322{
2323 struct btrfs_block_group_cache *cache;
2324 u64 chunk_used, user_thresh;
2325 int ret = 1;
2326
2327 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2328 chunk_used = btrfs_block_group_used(&cache->item);
2329
2330 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2331 if (chunk_used < user_thresh)
2332 ret = 0;
2333
2334 btrfs_put_block_group(cache);
2335 return ret;
2336}
2337
Ilya Dryomov409d4042012-01-16 22:04:47 +02002338static int chunk_devid_filter(struct extent_buffer *leaf,
2339 struct btrfs_chunk *chunk,
2340 struct btrfs_balance_args *bargs)
2341{
2342 struct btrfs_stripe *stripe;
2343 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2344 int i;
2345
2346 for (i = 0; i < num_stripes; i++) {
2347 stripe = btrfs_stripe_nr(chunk, i);
2348 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2349 return 0;
2350 }
2351
2352 return 1;
2353}
2354
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002355/* [pstart, pend) */
2356static int chunk_drange_filter(struct extent_buffer *leaf,
2357 struct btrfs_chunk *chunk,
2358 u64 chunk_offset,
2359 struct btrfs_balance_args *bargs)
2360{
2361 struct btrfs_stripe *stripe;
2362 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2363 u64 stripe_offset;
2364 u64 stripe_length;
2365 int factor;
2366 int i;
2367
2368 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2369 return 0;
2370
2371 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2372 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2373 factor = 2;
2374 else
2375 factor = 1;
2376 factor = num_stripes / factor;
2377
2378 for (i = 0; i < num_stripes; i++) {
2379 stripe = btrfs_stripe_nr(chunk, i);
2380 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2381 continue;
2382
2383 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2384 stripe_length = btrfs_chunk_length(leaf, chunk);
2385 do_div(stripe_length, factor);
2386
2387 if (stripe_offset < bargs->pend &&
2388 stripe_offset + stripe_length > bargs->pstart)
2389 return 0;
2390 }
2391
2392 return 1;
2393}
2394
Ilya Dryomovea671762012-01-16 22:04:48 +02002395/* [vstart, vend) */
2396static int chunk_vrange_filter(struct extent_buffer *leaf,
2397 struct btrfs_chunk *chunk,
2398 u64 chunk_offset,
2399 struct btrfs_balance_args *bargs)
2400{
2401 if (chunk_offset < bargs->vend &&
2402 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2403 /* at least part of the chunk is inside this vrange */
2404 return 0;
2405
2406 return 1;
2407}
2408
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002409static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002410 struct btrfs_balance_args *bargs)
2411{
2412 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2413 return 0;
2414
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002415 chunk_type = chunk_to_extended(chunk_type) &
2416 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002417
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002418 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002419 return 1;
2420
2421 return 0;
2422}
2423
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002424static int should_balance_chunk(struct btrfs_root *root,
2425 struct extent_buffer *leaf,
2426 struct btrfs_chunk *chunk, u64 chunk_offset)
2427{
2428 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2429 struct btrfs_balance_args *bargs = NULL;
2430 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2431
2432 /* type filter */
2433 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2434 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2435 return 0;
2436 }
2437
2438 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2439 bargs = &bctl->data;
2440 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2441 bargs = &bctl->sys;
2442 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2443 bargs = &bctl->meta;
2444
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002445 /* profiles filter */
2446 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2447 chunk_profiles_filter(chunk_type, bargs)) {
2448 return 0;
2449 }
2450
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002451 /* usage filter */
2452 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2453 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2454 return 0;
2455 }
2456
Ilya Dryomov409d4042012-01-16 22:04:47 +02002457 /* devid filter */
2458 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2459 chunk_devid_filter(leaf, chunk, bargs)) {
2460 return 0;
2461 }
2462
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002463 /* drange filter, makes sense only with devid filter */
2464 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2465 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2466 return 0;
2467 }
2468
Ilya Dryomovea671762012-01-16 22:04:48 +02002469 /* vrange filter */
2470 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2471 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2472 return 0;
2473 }
2474
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002475 /* soft profile changing mode */
2476 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2477 chunk_soft_convert_filter(chunk_type, bargs)) {
2478 return 0;
2479 }
2480
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002481 return 1;
2482}
2483
Chris Masonec44a352008-04-28 15:29:52 -04002484static u64 div_factor(u64 num, int factor)
2485{
2486 if (factor == 10)
2487 return num;
2488 num *= factor;
2489 do_div(num, 10);
2490 return num;
2491}
2492
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002493static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002494{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002495 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002496 struct btrfs_root *chunk_root = fs_info->chunk_root;
2497 struct btrfs_root *dev_root = fs_info->dev_root;
2498 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002499 struct btrfs_device *device;
2500 u64 old_size;
2501 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002502 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002503 struct btrfs_path *path;
2504 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002505 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002506 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002507 struct extent_buffer *leaf;
2508 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002509 int ret;
2510 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002511 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002512
Chris Masonec44a352008-04-28 15:29:52 -04002513 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002514 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002515 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002516 old_size = device->total_bytes;
2517 size_to_free = div_factor(old_size, 1);
2518 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002519 if (!device->writeable ||
2520 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002521 continue;
2522
2523 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002524 if (ret == -ENOSPC)
2525 break;
Chris Masonec44a352008-04-28 15:29:52 -04002526 BUG_ON(ret);
2527
Yan, Zhenga22285a2010-05-16 10:48:46 -04002528 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002529 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002530
2531 ret = btrfs_grow_device(trans, device, old_size);
2532 BUG_ON(ret);
2533
2534 btrfs_end_transaction(trans, dev_root);
2535 }
2536
2537 /* step two, relocate all the chunks */
2538 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002539 if (!path) {
2540 ret = -ENOMEM;
2541 goto error;
2542 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002543
2544 /* zero out stat counters */
2545 spin_lock(&fs_info->balance_lock);
2546 memset(&bctl->stat, 0, sizeof(bctl->stat));
2547 spin_unlock(&fs_info->balance_lock);
2548again:
Chris Masonec44a352008-04-28 15:29:52 -04002549 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2550 key.offset = (u64)-1;
2551 key.type = BTRFS_CHUNK_ITEM_KEY;
2552
Chris Masond3977122009-01-05 21:25:51 -05002553 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002554 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002555 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002556 ret = -ECANCELED;
2557 goto error;
2558 }
2559
Chris Masonec44a352008-04-28 15:29:52 -04002560 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2561 if (ret < 0)
2562 goto error;
2563
2564 /*
2565 * this shouldn't happen, it means the last relocate
2566 * failed
2567 */
2568 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002569 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002570
2571 ret = btrfs_previous_item(chunk_root, path, 0,
2572 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002573 if (ret) {
2574 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002575 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002576 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002577
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002578 leaf = path->nodes[0];
2579 slot = path->slots[0];
2580 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2581
Chris Masonec44a352008-04-28 15:29:52 -04002582 if (found_key.objectid != key.objectid)
2583 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002584
Chris Masonec44a352008-04-28 15:29:52 -04002585 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002586 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002587 break;
2588
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002589 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2590
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002591 if (!counting) {
2592 spin_lock(&fs_info->balance_lock);
2593 bctl->stat.considered++;
2594 spin_unlock(&fs_info->balance_lock);
2595 }
2596
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002597 ret = should_balance_chunk(chunk_root, leaf, chunk,
2598 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002599 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002600 if (!ret)
2601 goto loop;
2602
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002603 if (counting) {
2604 spin_lock(&fs_info->balance_lock);
2605 bctl->stat.expected++;
2606 spin_unlock(&fs_info->balance_lock);
2607 goto loop;
2608 }
2609
Chris Masonec44a352008-04-28 15:29:52 -04002610 ret = btrfs_relocate_chunk(chunk_root,
2611 chunk_root->root_key.objectid,
2612 found_key.objectid,
2613 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002614 if (ret && ret != -ENOSPC)
2615 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002616 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002617 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002618 } else {
2619 spin_lock(&fs_info->balance_lock);
2620 bctl->stat.completed++;
2621 spin_unlock(&fs_info->balance_lock);
2622 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002623loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002624 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002625 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002626
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002627 if (counting) {
2628 btrfs_release_path(path);
2629 counting = false;
2630 goto again;
2631 }
Chris Masonec44a352008-04-28 15:29:52 -04002632error:
2633 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002634 if (enospc_errors) {
2635 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2636 enospc_errors);
2637 if (!ret)
2638 ret = -ENOSPC;
2639 }
2640
Chris Masonec44a352008-04-28 15:29:52 -04002641 return ret;
2642}
2643
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002644/**
2645 * alloc_profile_is_valid - see if a given profile is valid and reduced
2646 * @flags: profile to validate
2647 * @extended: if true @flags is treated as an extended profile
2648 */
2649static int alloc_profile_is_valid(u64 flags, int extended)
2650{
2651 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2652 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2653
2654 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2655
2656 /* 1) check that all other bits are zeroed */
2657 if (flags & ~mask)
2658 return 0;
2659
2660 /* 2) see if profile is reduced */
2661 if (flags == 0)
2662 return !extended; /* "0" is valid for usual profiles */
2663
2664 /* true if exactly one bit set */
2665 return (flags & (flags - 1)) == 0;
2666}
2667
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002668static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2669{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002670 /* cancel requested || normal exit path */
2671 return atomic_read(&fs_info->balance_cancel_req) ||
2672 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2673 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002674}
2675
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002676static void __cancel_balance(struct btrfs_fs_info *fs_info)
2677{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002678 int ret;
2679
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002680 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002681 ret = del_balance_item(fs_info->tree_root);
2682 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002683}
2684
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002685void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002686 struct btrfs_ioctl_balance_args *bargs);
2687
2688/*
2689 * Should be called with both balance and volume mutexes held
2690 */
2691int btrfs_balance(struct btrfs_balance_control *bctl,
2692 struct btrfs_ioctl_balance_args *bargs)
2693{
2694 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002695 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002696 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002697 int ret;
2698
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002699 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002700 atomic_read(&fs_info->balance_pause_req) ||
2701 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002702 ret = -EINVAL;
2703 goto out;
2704 }
2705
Ilya Dryomove4837f82012-03-27 17:09:17 +03002706 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2707 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2708 mixed = 1;
2709
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002710 /*
2711 * In case of mixed groups both data and meta should be picked,
2712 * and identical options should be given for both of them.
2713 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002714 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2715 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002716 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2717 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2718 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2719 printk(KERN_ERR "btrfs: with mixed groups data and "
2720 "metadata balance options must be the same\n");
2721 ret = -EINVAL;
2722 goto out;
2723 }
2724 }
2725
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002726 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2727 if (fs_info->fs_devices->num_devices == 1)
2728 allowed |= BTRFS_BLOCK_GROUP_DUP;
2729 else if (fs_info->fs_devices->num_devices < 4)
2730 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2731 else
2732 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2733 BTRFS_BLOCK_GROUP_RAID10);
2734
Ilya Dryomov6728b192012-03-27 17:09:17 +03002735 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2736 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2737 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002738 printk(KERN_ERR "btrfs: unable to start balance with target "
2739 "data profile %llu\n",
2740 (unsigned long long)bctl->data.target);
2741 ret = -EINVAL;
2742 goto out;
2743 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002744 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2745 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2746 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002747 printk(KERN_ERR "btrfs: unable to start balance with target "
2748 "metadata profile %llu\n",
2749 (unsigned long long)bctl->meta.target);
2750 ret = -EINVAL;
2751 goto out;
2752 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002753 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2754 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2755 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002756 printk(KERN_ERR "btrfs: unable to start balance with target "
2757 "system profile %llu\n",
2758 (unsigned long long)bctl->sys.target);
2759 ret = -EINVAL;
2760 goto out;
2761 }
2762
Ilya Dryomove4837f82012-03-27 17:09:17 +03002763 /* allow dup'ed data chunks only in mixed mode */
2764 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002765 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002766 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2767 ret = -EINVAL;
2768 goto out;
2769 }
2770
2771 /* allow to reduce meta or sys integrity only if force set */
2772 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2773 BTRFS_BLOCK_GROUP_RAID10;
2774 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2775 (fs_info->avail_system_alloc_bits & allowed) &&
2776 !(bctl->sys.target & allowed)) ||
2777 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2778 (fs_info->avail_metadata_alloc_bits & allowed) &&
2779 !(bctl->meta.target & allowed))) {
2780 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2781 printk(KERN_INFO "btrfs: force reducing metadata "
2782 "integrity\n");
2783 } else {
2784 printk(KERN_ERR "btrfs: balance will reduce metadata "
2785 "integrity, use force if you want this\n");
2786 ret = -EINVAL;
2787 goto out;
2788 }
2789 }
2790
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002791 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002792 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002793 goto out;
2794
Ilya Dryomov59641012012-01-16 22:04:48 +02002795 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2796 BUG_ON(ret == -EEXIST);
2797 set_balance_control(bctl);
2798 } else {
2799 BUG_ON(ret != -EEXIST);
2800 spin_lock(&fs_info->balance_lock);
2801 update_balance_args(bctl);
2802 spin_unlock(&fs_info->balance_lock);
2803 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002804
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002805 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002806 mutex_unlock(&fs_info->balance_mutex);
2807
2808 ret = __btrfs_balance(fs_info);
2809
2810 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002811 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002812
2813 if (bargs) {
2814 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002815 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002816 }
2817
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002818 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2819 balance_need_close(fs_info)) {
2820 __cancel_balance(fs_info);
2821 }
2822
2823 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002824
2825 return ret;
2826out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002827 if (bctl->flags & BTRFS_BALANCE_RESUME)
2828 __cancel_balance(fs_info);
2829 else
2830 kfree(bctl);
2831 return ret;
2832}
2833
2834static int balance_kthread(void *data)
2835{
2836 struct btrfs_balance_control *bctl =
2837 (struct btrfs_balance_control *)data;
2838 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002839 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002840
2841 mutex_lock(&fs_info->volume_mutex);
2842 mutex_lock(&fs_info->balance_mutex);
2843
2844 set_balance_control(bctl);
2845
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002846 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2847 printk(KERN_INFO "btrfs: force skipping balance\n");
2848 } else {
2849 printk(KERN_INFO "btrfs: continuing balance\n");
2850 ret = btrfs_balance(bctl, NULL);
2851 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002852
2853 mutex_unlock(&fs_info->balance_mutex);
2854 mutex_unlock(&fs_info->volume_mutex);
2855 return ret;
2856}
2857
2858int btrfs_recover_balance(struct btrfs_root *tree_root)
2859{
2860 struct task_struct *tsk;
2861 struct btrfs_balance_control *bctl;
2862 struct btrfs_balance_item *item;
2863 struct btrfs_disk_balance_args disk_bargs;
2864 struct btrfs_path *path;
2865 struct extent_buffer *leaf;
2866 struct btrfs_key key;
2867 int ret;
2868
2869 path = btrfs_alloc_path();
2870 if (!path)
2871 return -ENOMEM;
2872
2873 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2874 if (!bctl) {
2875 ret = -ENOMEM;
2876 goto out;
2877 }
2878
2879 key.objectid = BTRFS_BALANCE_OBJECTID;
2880 key.type = BTRFS_BALANCE_ITEM_KEY;
2881 key.offset = 0;
2882
2883 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2884 if (ret < 0)
2885 goto out_bctl;
2886 if (ret > 0) { /* ret = -ENOENT; */
2887 ret = 0;
2888 goto out_bctl;
2889 }
2890
2891 leaf = path->nodes[0];
2892 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2893
2894 bctl->fs_info = tree_root->fs_info;
2895 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2896
2897 btrfs_balance_data(leaf, item, &disk_bargs);
2898 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2899 btrfs_balance_meta(leaf, item, &disk_bargs);
2900 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2901 btrfs_balance_sys(leaf, item, &disk_bargs);
2902 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2903
2904 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2905 if (IS_ERR(tsk))
2906 ret = PTR_ERR(tsk);
2907 else
2908 goto out;
2909
2910out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002911 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002912out:
2913 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002914 return ret;
2915}
2916
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002917int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2918{
2919 int ret = 0;
2920
2921 mutex_lock(&fs_info->balance_mutex);
2922 if (!fs_info->balance_ctl) {
2923 mutex_unlock(&fs_info->balance_mutex);
2924 return -ENOTCONN;
2925 }
2926
2927 if (atomic_read(&fs_info->balance_running)) {
2928 atomic_inc(&fs_info->balance_pause_req);
2929 mutex_unlock(&fs_info->balance_mutex);
2930
2931 wait_event(fs_info->balance_wait_q,
2932 atomic_read(&fs_info->balance_running) == 0);
2933
2934 mutex_lock(&fs_info->balance_mutex);
2935 /* we are good with balance_ctl ripped off from under us */
2936 BUG_ON(atomic_read(&fs_info->balance_running));
2937 atomic_dec(&fs_info->balance_pause_req);
2938 } else {
2939 ret = -ENOTCONN;
2940 }
2941
2942 mutex_unlock(&fs_info->balance_mutex);
2943 return ret;
2944}
2945
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002946int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2947{
2948 mutex_lock(&fs_info->balance_mutex);
2949 if (!fs_info->balance_ctl) {
2950 mutex_unlock(&fs_info->balance_mutex);
2951 return -ENOTCONN;
2952 }
2953
2954 atomic_inc(&fs_info->balance_cancel_req);
2955 /*
2956 * if we are running just wait and return, balance item is
2957 * deleted in btrfs_balance in this case
2958 */
2959 if (atomic_read(&fs_info->balance_running)) {
2960 mutex_unlock(&fs_info->balance_mutex);
2961 wait_event(fs_info->balance_wait_q,
2962 atomic_read(&fs_info->balance_running) == 0);
2963 mutex_lock(&fs_info->balance_mutex);
2964 } else {
2965 /* __cancel_balance needs volume_mutex */
2966 mutex_unlock(&fs_info->balance_mutex);
2967 mutex_lock(&fs_info->volume_mutex);
2968 mutex_lock(&fs_info->balance_mutex);
2969
2970 if (fs_info->balance_ctl)
2971 __cancel_balance(fs_info);
2972
2973 mutex_unlock(&fs_info->volume_mutex);
2974 }
2975
2976 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2977 atomic_dec(&fs_info->balance_cancel_req);
2978 mutex_unlock(&fs_info->balance_mutex);
2979 return 0;
2980}
2981
Chris Mason8f18cf12008-04-25 16:53:30 -04002982/*
2983 * shrinking a device means finding all of the device extents past
2984 * the new size, and then following the back refs to the chunks.
2985 * The chunk relocation code actually frees the device extent
2986 */
2987int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2988{
2989 struct btrfs_trans_handle *trans;
2990 struct btrfs_root *root = device->dev_root;
2991 struct btrfs_dev_extent *dev_extent = NULL;
2992 struct btrfs_path *path;
2993 u64 length;
2994 u64 chunk_tree;
2995 u64 chunk_objectid;
2996 u64 chunk_offset;
2997 int ret;
2998 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002999 int failed = 0;
3000 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003001 struct extent_buffer *l;
3002 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003003 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003004 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003005 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003006 u64 diff = device->total_bytes - new_size;
3007
Yan Zheng2b820322008-11-17 21:11:30 -05003008 if (new_size >= device->total_bytes)
3009 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04003010
3011 path = btrfs_alloc_path();
3012 if (!path)
3013 return -ENOMEM;
3014
Chris Mason8f18cf12008-04-25 16:53:30 -04003015 path->reada = 2;
3016
Chris Mason7d9eb122008-07-08 14:19:17 -04003017 lock_chunks(root);
3018
Chris Mason8f18cf12008-04-25 16:53:30 -04003019 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003020 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003021 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003022 spin_lock(&root->fs_info->free_chunk_lock);
3023 root->fs_info->free_chunk_space -= diff;
3024 spin_unlock(&root->fs_info->free_chunk_lock);
3025 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003026 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003027
Josef Bacikba1bf482009-09-11 16:11:19 -04003028again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003029 key.objectid = device->devid;
3030 key.offset = (u64)-1;
3031 key.type = BTRFS_DEV_EXTENT_KEY;
3032
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003033 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003034 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3035 if (ret < 0)
3036 goto done;
3037
3038 ret = btrfs_previous_item(root, path, 0, key.type);
3039 if (ret < 0)
3040 goto done;
3041 if (ret) {
3042 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003043 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003044 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003045 }
3046
3047 l = path->nodes[0];
3048 slot = path->slots[0];
3049 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3050
Josef Bacikba1bf482009-09-11 16:11:19 -04003051 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003052 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003053 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003054 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003055
3056 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3057 length = btrfs_dev_extent_length(l, dev_extent);
3058
Josef Bacikba1bf482009-09-11 16:11:19 -04003059 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003060 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003061 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003062 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003063
3064 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3065 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3066 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003067 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003068
3069 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3070 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003071 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003072 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003073 if (ret == -ENOSPC)
3074 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003075 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003076
3077 if (failed && !retried) {
3078 failed = 0;
3079 retried = true;
3080 goto again;
3081 } else if (failed && retried) {
3082 ret = -ENOSPC;
3083 lock_chunks(root);
3084
3085 device->total_bytes = old_size;
3086 if (device->writeable)
3087 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003088 spin_lock(&root->fs_info->free_chunk_lock);
3089 root->fs_info->free_chunk_space += diff;
3090 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003091 unlock_chunks(root);
3092 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003093 }
3094
Chris Balld6397ba2009-04-27 07:29:03 -04003095 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003096 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003097 if (IS_ERR(trans)) {
3098 ret = PTR_ERR(trans);
3099 goto done;
3100 }
3101
Chris Balld6397ba2009-04-27 07:29:03 -04003102 lock_chunks(root);
3103
3104 device->disk_total_bytes = new_size;
3105 /* Now btrfs_update_device() will change the on-disk size. */
3106 ret = btrfs_update_device(trans, device);
3107 if (ret) {
3108 unlock_chunks(root);
3109 btrfs_end_transaction(trans, root);
3110 goto done;
3111 }
3112 WARN_ON(diff > old_total);
3113 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3114 unlock_chunks(root);
3115 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003116done:
3117 btrfs_free_path(path);
3118 return ret;
3119}
3120
Li Zefan125ccb02011-12-08 15:07:24 +08003121static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003122 struct btrfs_key *key,
3123 struct btrfs_chunk *chunk, int item_size)
3124{
David Sterba6c417612011-04-13 15:41:04 +02003125 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003126 struct btrfs_disk_key disk_key;
3127 u32 array_size;
3128 u8 *ptr;
3129
3130 array_size = btrfs_super_sys_array_size(super_copy);
3131 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3132 return -EFBIG;
3133
3134 ptr = super_copy->sys_chunk_array + array_size;
3135 btrfs_cpu_key_to_disk(&disk_key, key);
3136 memcpy(ptr, &disk_key, sizeof(disk_key));
3137 ptr += sizeof(disk_key);
3138 memcpy(ptr, chunk, item_size);
3139 item_size += sizeof(disk_key);
3140 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3141 return 0;
3142}
3143
Miao Xieb2117a32011-01-05 10:07:28 +00003144/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003145 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003146 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003147static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003148{
Arne Jansen73c5de02011-04-12 12:07:57 +02003149 const struct btrfs_device_info *di_a = a;
3150 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003151
Arne Jansen73c5de02011-04-12 12:07:57 +02003152 if (di_a->max_avail > di_b->max_avail)
3153 return -1;
3154 if (di_a->max_avail < di_b->max_avail)
3155 return 1;
3156 if (di_a->total_avail > di_b->total_avail)
3157 return -1;
3158 if (di_a->total_avail < di_b->total_avail)
3159 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003160 return 0;
3161}
3162
3163static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3164 struct btrfs_root *extent_root,
3165 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003166 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003167 u64 start, u64 type)
3168{
3169 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003170 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3171 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003172 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003173 struct extent_map_tree *em_tree;
3174 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003175 struct btrfs_device_info *devices_info = NULL;
3176 u64 total_avail;
3177 int num_stripes; /* total number of stripes to allocate */
3178 int sub_stripes; /* sub_stripes info for map */
3179 int dev_stripes; /* stripes per dev */
3180 int devs_max; /* max devs to use */
3181 int devs_min; /* min devs needed */
3182 int devs_increment; /* ndevs has to be a multiple of this */
3183 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003184 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003185 u64 max_stripe_size;
3186 u64 max_chunk_size;
3187 u64 stripe_size;
3188 u64 num_bytes;
3189 int ndevs;
3190 int i;
3191 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003192
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003193 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003194
Miao Xieb2117a32011-01-05 10:07:28 +00003195 if (list_empty(&fs_devices->alloc_list))
3196 return -ENOSPC;
3197
Arne Jansen73c5de02011-04-12 12:07:57 +02003198 sub_stripes = 1;
3199 dev_stripes = 1;
3200 devs_increment = 1;
3201 ncopies = 1;
3202 devs_max = 0; /* 0 == as many as possible */
3203 devs_min = 1;
3204
3205 /*
3206 * define the properties of each RAID type.
3207 * FIXME: move this to a global table and use it in all RAID
3208 * calculation code
3209 */
3210 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3211 dev_stripes = 2;
3212 ncopies = 2;
3213 devs_max = 1;
3214 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3215 devs_min = 2;
3216 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3217 devs_increment = 2;
3218 ncopies = 2;
3219 devs_max = 2;
3220 devs_min = 2;
3221 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3222 sub_stripes = 2;
3223 devs_increment = 2;
3224 ncopies = 2;
3225 devs_min = 4;
3226 } else {
3227 devs_max = 1;
3228 }
3229
3230 if (type & BTRFS_BLOCK_GROUP_DATA) {
3231 max_stripe_size = 1024 * 1024 * 1024;
3232 max_chunk_size = 10 * max_stripe_size;
3233 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003234 /* for larger filesystems, use larger metadata chunks */
3235 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3236 max_stripe_size = 1024 * 1024 * 1024;
3237 else
3238 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003239 max_chunk_size = max_stripe_size;
3240 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003241 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003242 max_chunk_size = 2 * max_stripe_size;
3243 } else {
3244 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3245 type);
3246 BUG_ON(1);
3247 }
3248
3249 /* we don't want a chunk larger than 10% of writeable space */
3250 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3251 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003252
3253 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3254 GFP_NOFS);
3255 if (!devices_info)
3256 return -ENOMEM;
3257
Arne Jansen73c5de02011-04-12 12:07:57 +02003258 cur = fs_devices->alloc_list.next;
3259
3260 /*
3261 * in the first pass through the devices list, we gather information
3262 * about the available holes on each device.
3263 */
3264 ndevs = 0;
3265 while (cur != &fs_devices->alloc_list) {
3266 struct btrfs_device *device;
3267 u64 max_avail;
3268 u64 dev_offset;
3269
3270 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3271
3272 cur = cur->next;
3273
3274 if (!device->writeable) {
3275 printk(KERN_ERR
3276 "btrfs: read-only device in alloc_list\n");
3277 WARN_ON(1);
3278 continue;
3279 }
3280
3281 if (!device->in_fs_metadata)
3282 continue;
3283
3284 if (device->total_bytes > device->bytes_used)
3285 total_avail = device->total_bytes - device->bytes_used;
3286 else
3287 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003288
3289 /* If there is no space on this device, skip it. */
3290 if (total_avail == 0)
3291 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003292
Li Zefan125ccb02011-12-08 15:07:24 +08003293 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003294 max_stripe_size * dev_stripes,
3295 &dev_offset, &max_avail);
3296 if (ret && ret != -ENOSPC)
3297 goto error;
3298
3299 if (ret == 0)
3300 max_avail = max_stripe_size * dev_stripes;
3301
3302 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3303 continue;
3304
3305 devices_info[ndevs].dev_offset = dev_offset;
3306 devices_info[ndevs].max_avail = max_avail;
3307 devices_info[ndevs].total_avail = total_avail;
3308 devices_info[ndevs].dev = device;
3309 ++ndevs;
3310 }
3311
3312 /*
3313 * now sort the devices by hole size / available space
3314 */
3315 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3316 btrfs_cmp_device_info, NULL);
3317
3318 /* round down to number of usable stripes */
3319 ndevs -= ndevs % devs_increment;
3320
3321 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3322 ret = -ENOSPC;
3323 goto error;
3324 }
3325
3326 if (devs_max && ndevs > devs_max)
3327 ndevs = devs_max;
3328 /*
3329 * the primary goal is to maximize the number of stripes, so use as many
3330 * devices as possible, even if the stripes are not maximum sized.
3331 */
3332 stripe_size = devices_info[ndevs-1].max_avail;
3333 num_stripes = ndevs * dev_stripes;
3334
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003335 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003336 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003337 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003338 }
3339
3340 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003341
3342 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003343 do_div(stripe_size, BTRFS_STRIPE_LEN);
3344 stripe_size *= BTRFS_STRIPE_LEN;
3345
Miao Xieb2117a32011-01-05 10:07:28 +00003346 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3347 if (!map) {
3348 ret = -ENOMEM;
3349 goto error;
3350 }
3351 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003352
Arne Jansen73c5de02011-04-12 12:07:57 +02003353 for (i = 0; i < ndevs; ++i) {
3354 for (j = 0; j < dev_stripes; ++j) {
3355 int s = i * dev_stripes + j;
3356 map->stripes[s].dev = devices_info[i].dev;
3357 map->stripes[s].physical = devices_info[i].dev_offset +
3358 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003359 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003360 }
Chris Mason593060d2008-03-25 16:50:33 -04003361 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003362 map->stripe_len = BTRFS_STRIPE_LEN;
3363 map->io_align = BTRFS_STRIPE_LEN;
3364 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003365 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003366 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003367
Yan Zheng2b820322008-11-17 21:11:30 -05003368 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003369 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003370
Arne Jansen73c5de02011-04-12 12:07:57 +02003371 *stripe_size_out = stripe_size;
3372 *num_bytes_out = num_bytes;
3373
3374 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003375
David Sterba172ddd62011-04-21 00:48:27 +02003376 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003377 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003378 ret = -ENOMEM;
3379 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003380 }
Chris Mason0b86a832008-03-24 15:01:56 -04003381 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003382 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003383 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003384 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003385 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003386
Chris Mason0b86a832008-03-24 15:01:56 -04003387 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003388 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003389 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003390 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003391 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003392 if (ret)
3393 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003394
3395 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3396 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003397 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003398 if (ret)
3399 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003400
Arne Jansen73c5de02011-04-12 12:07:57 +02003401 for (i = 0; i < map->num_stripes; ++i) {
3402 struct btrfs_device *device;
3403 u64 dev_offset;
3404
3405 device = map->stripes[i].dev;
3406 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003407
3408 ret = btrfs_alloc_dev_extent(trans, device,
3409 info->chunk_root->root_key.objectid,
3410 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003411 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003412 if (ret) {
3413 btrfs_abort_transaction(trans, extent_root, ret);
3414 goto error;
3415 }
Yan Zheng2b820322008-11-17 21:11:30 -05003416 }
3417
Miao Xieb2117a32011-01-05 10:07:28 +00003418 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003419 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003420
3421error:
3422 kfree(map);
3423 kfree(devices_info);
3424 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003425}
3426
3427static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3428 struct btrfs_root *extent_root,
3429 struct map_lookup *map, u64 chunk_offset,
3430 u64 chunk_size, u64 stripe_size)
3431{
3432 u64 dev_offset;
3433 struct btrfs_key key;
3434 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3435 struct btrfs_device *device;
3436 struct btrfs_chunk *chunk;
3437 struct btrfs_stripe *stripe;
3438 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3439 int index = 0;
3440 int ret;
3441
3442 chunk = kzalloc(item_size, GFP_NOFS);
3443 if (!chunk)
3444 return -ENOMEM;
3445
3446 index = 0;
3447 while (index < map->num_stripes) {
3448 device = map->stripes[index].dev;
3449 device->bytes_used += stripe_size;
3450 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003451 if (ret)
3452 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003453 index++;
3454 }
3455
Josef Bacik2bf64752011-09-26 17:12:22 -04003456 spin_lock(&extent_root->fs_info->free_chunk_lock);
3457 extent_root->fs_info->free_chunk_space -= (stripe_size *
3458 map->num_stripes);
3459 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3460
Yan Zheng2b820322008-11-17 21:11:30 -05003461 index = 0;
3462 stripe = &chunk->stripe;
3463 while (index < map->num_stripes) {
3464 device = map->stripes[index].dev;
3465 dev_offset = map->stripes[index].physical;
3466
3467 btrfs_set_stack_stripe_devid(stripe, device->devid);
3468 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3469 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3470 stripe++;
3471 index++;
3472 }
3473
3474 btrfs_set_stack_chunk_length(chunk, chunk_size);
3475 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3476 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3477 btrfs_set_stack_chunk_type(chunk, map->type);
3478 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3479 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3480 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3481 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3482 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3483
3484 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3485 key.type = BTRFS_CHUNK_ITEM_KEY;
3486 key.offset = chunk_offset;
3487
3488 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003489
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003490 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3491 /*
3492 * TODO: Cleanup of inserted chunk root in case of
3493 * failure.
3494 */
Li Zefan125ccb02011-12-08 15:07:24 +08003495 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003496 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003497 }
liubo1abe9b82011-03-24 11:18:59 +00003498
Mark Fasheh3acd3952011-09-08 17:40:01 -07003499out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003500 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003501 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003502}
3503
3504/*
3505 * Chunk allocation falls into two parts. The first part does works
3506 * that make the new allocated chunk useable, but not do any operation
3507 * that modifies the chunk tree. The second part does the works that
3508 * require modifying the chunk tree. This division is important for the
3509 * bootstrap process of adding storage to a seed btrfs.
3510 */
3511int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3512 struct btrfs_root *extent_root, u64 type)
3513{
3514 u64 chunk_offset;
3515 u64 chunk_size;
3516 u64 stripe_size;
3517 struct map_lookup *map;
3518 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3519 int ret;
3520
3521 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3522 &chunk_offset);
3523 if (ret)
3524 return ret;
3525
3526 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3527 &stripe_size, chunk_offset, type);
3528 if (ret)
3529 return ret;
3530
3531 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3532 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003533 if (ret)
3534 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003535 return 0;
3536}
3537
Chris Masond3977122009-01-05 21:25:51 -05003538static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003539 struct btrfs_root *root,
3540 struct btrfs_device *device)
3541{
3542 u64 chunk_offset;
3543 u64 sys_chunk_offset;
3544 u64 chunk_size;
3545 u64 sys_chunk_size;
3546 u64 stripe_size;
3547 u64 sys_stripe_size;
3548 u64 alloc_profile;
3549 struct map_lookup *map;
3550 struct map_lookup *sys_map;
3551 struct btrfs_fs_info *fs_info = root->fs_info;
3552 struct btrfs_root *extent_root = fs_info->extent_root;
3553 int ret;
3554
3555 ret = find_next_chunk(fs_info->chunk_root,
3556 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003557 if (ret)
3558 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003559
3560 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003561 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003562 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3563
3564 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3565 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003566 if (ret)
3567 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003568
3569 sys_chunk_offset = chunk_offset + chunk_size;
3570
3571 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003572 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003573 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3574
3575 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3576 &sys_chunk_size, &sys_stripe_size,
3577 sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003578 if (ret)
3579 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003580
3581 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003582 if (ret)
3583 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003584
3585 /*
3586 * Modifying chunk tree needs allocating new blocks from both
3587 * system block group and metadata block group. So we only can
3588 * do operations require modifying the chunk tree after both
3589 * block groups were created.
3590 */
3591 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3592 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003593 if (ret)
3594 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003595
3596 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3597 sys_chunk_offset, sys_chunk_size,
3598 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003599 if (ret)
3600 goto abort;
3601
Yan Zheng2b820322008-11-17 21:11:30 -05003602 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003603
3604abort:
3605 btrfs_abort_transaction(trans, root, ret);
3606 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003607}
3608
3609int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3610{
3611 struct extent_map *em;
3612 struct map_lookup *map;
3613 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3614 int readonly = 0;
3615 int i;
3616
Chris Mason890871b2009-09-02 16:24:52 -04003617 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003618 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003619 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003620 if (!em)
3621 return 1;
3622
Josef Bacikf48b9072010-01-27 02:07:59 +00003623 if (btrfs_test_opt(root, DEGRADED)) {
3624 free_extent_map(em);
3625 return 0;
3626 }
3627
Yan Zheng2b820322008-11-17 21:11:30 -05003628 map = (struct map_lookup *)em->bdev;
3629 for (i = 0; i < map->num_stripes; i++) {
3630 if (!map->stripes[i].dev->writeable) {
3631 readonly = 1;
3632 break;
3633 }
3634 }
3635 free_extent_map(em);
3636 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003637}
3638
3639void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3640{
David Sterbaa8067e02011-04-21 00:34:43 +02003641 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003642}
3643
3644void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3645{
3646 struct extent_map *em;
3647
Chris Masond3977122009-01-05 21:25:51 -05003648 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003649 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003650 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3651 if (em)
3652 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003653 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003654 if (!em)
3655 break;
3656 kfree(em->bdev);
3657 /* once for us */
3658 free_extent_map(em);
3659 /* once for the tree */
3660 free_extent_map(em);
3661 }
3662}
3663
Chris Masonf1885912008-04-09 16:28:12 -04003664int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3665{
3666 struct extent_map *em;
3667 struct map_lookup *map;
3668 struct extent_map_tree *em_tree = &map_tree->map_tree;
3669 int ret;
3670
Chris Mason890871b2009-09-02 16:24:52 -04003671 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003672 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003673 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003674 BUG_ON(!em);
3675
3676 BUG_ON(em->start > logical || em->start + em->len < logical);
3677 map = (struct map_lookup *)em->bdev;
3678 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3679 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003680 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3681 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003682 else
3683 ret = 1;
3684 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003685 return ret;
3686}
3687
Chris Masondfe25022008-05-13 13:46:40 -04003688static int find_live_mirror(struct map_lookup *map, int first, int num,
3689 int optimal)
3690{
3691 int i;
3692 if (map->stripes[optimal].dev->bdev)
3693 return optimal;
3694 for (i = first; i < first + num; i++) {
3695 if (map->stripes[i].dev->bdev)
3696 return i;
3697 }
3698 /* we couldn't find one that doesn't fail. Just return something
3699 * and the io error handling code will clean up eventually
3700 */
3701 return optimal;
3702}
3703
Chris Masonf2d8d742008-04-21 10:03:05 -04003704static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3705 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003706 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003707 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003708{
3709 struct extent_map *em;
3710 struct map_lookup *map;
3711 struct extent_map_tree *em_tree = &map_tree->map_tree;
3712 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003713 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003714 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003715 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003716 u64 stripe_nr_orig;
3717 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003718 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003719 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003720 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003721 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003722 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003723 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003724
Chris Mason890871b2009-09-02 16:24:52 -04003725 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003726 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003727 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003728
Chris Mason3b951512008-04-17 11:29:12 -04003729 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003730 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3731 (unsigned long long)logical,
3732 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003733 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003734 }
Chris Mason0b86a832008-03-24 15:01:56 -04003735
3736 BUG_ON(em->start > logical || em->start + em->len < logical);
3737 map = (struct map_lookup *)em->bdev;
3738 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003739
Chris Masonf1885912008-04-09 16:28:12 -04003740 if (mirror_num > map->num_stripes)
3741 mirror_num = 0;
3742
Chris Mason593060d2008-03-25 16:50:33 -04003743 stripe_nr = offset;
3744 /*
3745 * stripe_nr counts the total number of stripes we have to stride
3746 * to get to this block
3747 */
3748 do_div(stripe_nr, map->stripe_len);
3749
3750 stripe_offset = stripe_nr * map->stripe_len;
3751 BUG_ON(offset < stripe_offset);
3752
3753 /* stripe_offset is the offset of this block in its stripe*/
3754 stripe_offset = offset - stripe_offset;
3755
Li Dongyangfce3bb92011-03-24 10:24:26 +00003756 if (rw & REQ_DISCARD)
3757 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003758 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003759 /* we limit the length of each bio to what fits in a stripe */
3760 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003761 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003762 } else {
3763 *length = em->len - offset;
3764 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003765
Jan Schmidta1d3c472011-08-04 17:15:33 +02003766 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003767 goto out;
3768
Chris Masonf2d8d742008-04-21 10:03:05 -04003769 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003770 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003771 stripe_nr_orig = stripe_nr;
3772 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3773 (~(map->stripe_len - 1));
3774 do_div(stripe_nr_end, map->stripe_len);
3775 stripe_end_offset = stripe_nr_end * map->stripe_len -
3776 (offset + *length);
3777 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3778 if (rw & REQ_DISCARD)
3779 num_stripes = min_t(u64, map->num_stripes,
3780 stripe_nr_end - stripe_nr_orig);
3781 stripe_index = do_div(stripe_nr, map->num_stripes);
3782 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003783 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003784 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003785 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003786 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003787 else {
3788 stripe_index = find_live_mirror(map, 0,
3789 map->num_stripes,
3790 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003791 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003792 }
Chris Mason2fff7342008-04-29 14:12:09 -04003793
Chris Mason611f0e02008-04-03 16:29:03 -04003794 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003795 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003796 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003797 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003798 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003799 } else {
3800 mirror_num = 1;
3801 }
Chris Mason2fff7342008-04-29 14:12:09 -04003802
Chris Mason321aecc2008-04-16 10:49:51 -04003803 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3804 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003805
3806 stripe_index = do_div(stripe_nr, factor);
3807 stripe_index *= map->sub_stripes;
3808
Jens Axboe7eaceac2011-03-10 08:52:07 +01003809 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003810 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003811 else if (rw & REQ_DISCARD)
3812 num_stripes = min_t(u64, map->sub_stripes *
3813 (stripe_nr_end - stripe_nr_orig),
3814 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003815 else if (mirror_num)
3816 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003817 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04003818 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04003819 stripe_index = find_live_mirror(map, stripe_index,
3820 map->sub_stripes, stripe_index +
3821 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04003822 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003823 }
Chris Mason8790d502008-04-03 16:29:03 -04003824 } else {
3825 /*
3826 * after this do_div call, stripe_nr is the number of stripes
3827 * on this device we have to walk to find the data, and
3828 * stripe_index is the number of our device in the stripe array
3829 */
3830 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003831 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003832 }
Chris Mason593060d2008-03-25 16:50:33 -04003833 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003834
Li Zefande11cc12011-12-01 12:55:47 +08003835 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3836 if (!bbio) {
3837 ret = -ENOMEM;
3838 goto out;
3839 }
3840 atomic_set(&bbio->error, 0);
3841
Li Dongyangfce3bb92011-03-24 10:24:26 +00003842 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003843 int factor = 0;
3844 int sub_stripes = 0;
3845 u64 stripes_per_dev = 0;
3846 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003847 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003848
3849 if (map->type &
3850 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3851 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3852 sub_stripes = 1;
3853 else
3854 sub_stripes = map->sub_stripes;
3855
3856 factor = map->num_stripes / sub_stripes;
3857 stripes_per_dev = div_u64_rem(stripe_nr_end -
3858 stripe_nr_orig,
3859 factor,
3860 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04003861 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3862 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003863 }
3864
Li Dongyangfce3bb92011-03-24 10:24:26 +00003865 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003866 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003867 map->stripes[stripe_index].physical +
3868 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003869 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003870
Li Zefanec9ef7a2011-12-01 14:06:42 +08003871 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3872 BTRFS_BLOCK_GROUP_RAID10)) {
3873 bbio->stripes[i].length = stripes_per_dev *
3874 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003875
Li Zefanec9ef7a2011-12-01 14:06:42 +08003876 if (i / sub_stripes < remaining_stripes)
3877 bbio->stripes[i].length +=
3878 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003879
3880 /*
3881 * Special for the first stripe and
3882 * the last stripe:
3883 *
3884 * |-------|...|-------|
3885 * |----------|
3886 * off end_off
3887 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08003888 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003889 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003890 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003891
3892 if (stripe_index >= last_stripe &&
3893 stripe_index <= (last_stripe +
3894 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08003895 bbio->stripes[i].length -=
3896 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003897
Li Zefanec9ef7a2011-12-01 14:06:42 +08003898 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003899 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003900 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003901 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003902
3903 stripe_index++;
3904 if (stripe_index == map->num_stripes) {
3905 /* This could only happen for RAID0/10 */
3906 stripe_index = 0;
3907 stripe_nr++;
3908 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003909 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003910 } else {
3911 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003912 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003913 map->stripes[stripe_index].physical +
3914 stripe_offset +
3915 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003916 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003917 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003918 stripe_index++;
3919 }
Chris Mason593060d2008-03-25 16:50:33 -04003920 }
Li Zefande11cc12011-12-01 12:55:47 +08003921
3922 if (rw & REQ_WRITE) {
3923 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3924 BTRFS_BLOCK_GROUP_RAID10 |
3925 BTRFS_BLOCK_GROUP_DUP)) {
3926 max_errors = 1;
3927 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003928 }
Li Zefande11cc12011-12-01 12:55:47 +08003929
3930 *bbio_ret = bbio;
3931 bbio->num_stripes = num_stripes;
3932 bbio->max_errors = max_errors;
3933 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003934out:
Chris Mason0b86a832008-03-24 15:01:56 -04003935 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003936 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003937}
3938
Chris Masonf2d8d742008-04-21 10:03:05 -04003939int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3940 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003941 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003942{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003943 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003944 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003945}
3946
Yan Zhenga512bbf2008-12-08 16:46:26 -05003947int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3948 u64 chunk_start, u64 physical, u64 devid,
3949 u64 **logical, int *naddrs, int *stripe_len)
3950{
3951 struct extent_map_tree *em_tree = &map_tree->map_tree;
3952 struct extent_map *em;
3953 struct map_lookup *map;
3954 u64 *buf;
3955 u64 bytenr;
3956 u64 length;
3957 u64 stripe_nr;
3958 int i, j, nr = 0;
3959
Chris Mason890871b2009-09-02 16:24:52 -04003960 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003961 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003962 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003963
3964 BUG_ON(!em || em->start != chunk_start);
3965 map = (struct map_lookup *)em->bdev;
3966
3967 length = em->len;
3968 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3969 do_div(length, map->num_stripes / map->sub_stripes);
3970 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3971 do_div(length, map->num_stripes);
3972
3973 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003974 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003975
3976 for (i = 0; i < map->num_stripes; i++) {
3977 if (devid && map->stripes[i].dev->devid != devid)
3978 continue;
3979 if (map->stripes[i].physical > physical ||
3980 map->stripes[i].physical + length <= physical)
3981 continue;
3982
3983 stripe_nr = physical - map->stripes[i].physical;
3984 do_div(stripe_nr, map->stripe_len);
3985
3986 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3987 stripe_nr = stripe_nr * map->num_stripes + i;
3988 do_div(stripe_nr, map->sub_stripes);
3989 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3990 stripe_nr = stripe_nr * map->num_stripes + i;
3991 }
3992 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003993 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003994 for (j = 0; j < nr; j++) {
3995 if (buf[j] == bytenr)
3996 break;
3997 }
Chris Mason934d3752008-12-08 16:43:10 -05003998 if (j == nr) {
3999 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004000 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004001 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004002 }
4003
Yan Zhenga512bbf2008-12-08 16:46:26 -05004004 *logical = buf;
4005 *naddrs = nr;
4006 *stripe_len = map->stripe_len;
4007
4008 free_extent_map(em);
4009 return 0;
4010}
4011
Jan Schmidta1d3c472011-08-04 17:15:33 +02004012static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004013{
Jan Schmidta1d3c472011-08-04 17:15:33 +02004014 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004015 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004016
Chris Mason8790d502008-04-03 16:29:03 -04004017 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004018 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04004019
Jan Schmidta1d3c472011-08-04 17:15:33 +02004020 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004021 is_orig_bio = 1;
4022
Jan Schmidta1d3c472011-08-04 17:15:33 +02004023 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004024 if (!is_orig_bio) {
4025 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004026 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004027 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004028 bio->bi_private = bbio->private;
4029 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004030 bio->bi_bdev = (struct block_device *)
4031 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004032 /* only send an error to the higher layers if it is
4033 * beyond the tolerance of the multi-bio
4034 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004035 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004036 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004037 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004038 /*
4039 * this bio is actually up to date, we didn't
4040 * go over the max number of errors
4041 */
4042 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004043 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004044 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004045 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004046
4047 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004048 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004049 bio_put(bio);
4050 }
Chris Mason8790d502008-04-03 16:29:03 -04004051}
4052
Chris Mason8b712842008-06-11 16:50:36 -04004053struct async_sched {
4054 struct bio *bio;
4055 int rw;
4056 struct btrfs_fs_info *info;
4057 struct btrfs_work work;
4058};
4059
4060/*
4061 * see run_scheduled_bios for a description of why bios are collected for
4062 * async submit.
4063 *
4064 * This will add one bio to the pending list for a device and make sure
4065 * the work struct is scheduled.
4066 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004067static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004068 struct btrfs_device *device,
4069 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004070{
4071 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004072 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004073
4074 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004075 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6d2008-07-31 16:29:02 -04004076 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004077 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04004078 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004079 return;
Chris Mason8b712842008-06-11 16:50:36 -04004080 }
4081
4082 /*
Chris Mason0986fe92008-08-15 15:34:15 -04004083 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004084 * higher layers. Otherwise, the async bio makes it appear we have
4085 * made progress against dirty pages when we've really just put it
4086 * on a queue for later
4087 */
Chris Mason0986fe92008-08-15 15:34:15 -04004088 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04004089 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004090 bio->bi_next = NULL;
4091 bio->bi_rw |= rw;
4092
4093 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004094 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004095 pending_bios = &device->pending_sync_bios;
4096 else
4097 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004098
Chris Masonffbd5172009-04-20 15:50:09 -04004099 if (pending_bios->tail)
4100 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004101
Chris Masonffbd5172009-04-20 15:50:09 -04004102 pending_bios->tail = bio;
4103 if (!pending_bios->head)
4104 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004105 if (device->running_pending)
4106 should_queue = 0;
4107
4108 spin_unlock(&device->io_lock);
4109
4110 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004111 btrfs_queue_worker(&root->fs_info->submit_workers,
4112 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004113}
4114
Chris Masonf1885912008-04-09 16:28:12 -04004115int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004116 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004117{
4118 struct btrfs_mapping_tree *map_tree;
4119 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004120 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004121 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004122 u64 length = 0;
4123 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004124 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004125 int dev_nr = 0;
4126 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004127 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004128
Chris Masonf2d8d742008-04-21 10:03:05 -04004129 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004130 map_tree = &root->fs_info->mapping_tree;
4131 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004132
Jan Schmidta1d3c472011-08-04 17:15:33 +02004133 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004134 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004135 if (ret) /* -ENOMEM */
4136 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004137
Jan Schmidta1d3c472011-08-04 17:15:33 +02004138 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004139 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004140 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4141 "len %llu\n", (unsigned long long)logical,
4142 (unsigned long long)length,
4143 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004144 BUG();
4145 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004146
4147 bbio->orig_bio = first_bio;
4148 bbio->private = first_bio->bi_private;
4149 bbio->end_io = first_bio->bi_end_io;
4150 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004151
Chris Masond3977122009-01-05 21:25:51 -05004152 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004153 if (dev_nr < total_devs - 1) {
4154 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004155 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004156 } else {
4157 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004158 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004159 bio->bi_private = bbio;
4160 bio->bi_end_io = btrfs_end_bio;
4161 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4162 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004163 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004164 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4165 "(%s id %llu), size=%u\n", rw,
4166 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4167 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004168 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004169 if (async_submit)
4170 schedule_bio(root, dev, rw, bio);
4171 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004172 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004173 } else {
4174 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4175 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004176 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004177 }
Chris Mason8790d502008-04-03 16:29:03 -04004178 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004179 }
Chris Mason0b86a832008-03-24 15:01:56 -04004180 return 0;
4181}
4182
Chris Masona4437552008-04-18 10:29:38 -04004183struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004184 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004185{
Yan Zheng2b820322008-11-17 21:11:30 -05004186 struct btrfs_device *device;
4187 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004188
Yan Zheng2b820322008-11-17 21:11:30 -05004189 cur_devices = root->fs_info->fs_devices;
4190 while (cur_devices) {
4191 if (!fsid ||
4192 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4193 device = __find_device(&cur_devices->devices,
4194 devid, uuid);
4195 if (device)
4196 return device;
4197 }
4198 cur_devices = cur_devices->seed;
4199 }
4200 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004201}
4202
Chris Masondfe25022008-05-13 13:46:40 -04004203static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4204 u64 devid, u8 *dev_uuid)
4205{
4206 struct btrfs_device *device;
4207 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4208
4209 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004210 if (!device)
4211 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004212 list_add(&device->dev_list,
4213 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004214 device->dev_root = root->fs_info->dev_root;
4215 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004216 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004217 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004218 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004219 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004220 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004221 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004222 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004223 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4224 return device;
4225}
4226
Chris Mason0b86a832008-03-24 15:01:56 -04004227static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4228 struct extent_buffer *leaf,
4229 struct btrfs_chunk *chunk)
4230{
4231 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4232 struct map_lookup *map;
4233 struct extent_map *em;
4234 u64 logical;
4235 u64 length;
4236 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004237 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004238 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004239 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004240 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004241
Chris Masone17cade2008-04-15 15:41:47 -04004242 logical = key->offset;
4243 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004244
Chris Mason890871b2009-09-02 16:24:52 -04004245 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004246 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004247 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004248
4249 /* already mapped? */
4250 if (em && em->start <= logical && em->start + em->len > logical) {
4251 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004252 return 0;
4253 } else if (em) {
4254 free_extent_map(em);
4255 }
Chris Mason0b86a832008-03-24 15:01:56 -04004256
David Sterba172ddd62011-04-21 00:48:27 +02004257 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004258 if (!em)
4259 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004260 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4261 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004262 if (!map) {
4263 free_extent_map(em);
4264 return -ENOMEM;
4265 }
4266
4267 em->bdev = (struct block_device *)map;
4268 em->start = logical;
4269 em->len = length;
4270 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004271 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004272
Chris Mason593060d2008-03-25 16:50:33 -04004273 map->num_stripes = num_stripes;
4274 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4275 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4276 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4277 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4278 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004279 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004280 for (i = 0; i < num_stripes; i++) {
4281 map->stripes[i].physical =
4282 btrfs_stripe_offset_nr(leaf, chunk, i);
4283 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004284 read_extent_buffer(leaf, uuid, (unsigned long)
4285 btrfs_stripe_dev_uuid_nr(chunk, i),
4286 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004287 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4288 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004289 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004290 kfree(map);
4291 free_extent_map(em);
4292 return -EIO;
4293 }
Chris Masondfe25022008-05-13 13:46:40 -04004294 if (!map->stripes[i].dev) {
4295 map->stripes[i].dev =
4296 add_missing_dev(root, devid, uuid);
4297 if (!map->stripes[i].dev) {
4298 kfree(map);
4299 free_extent_map(em);
4300 return -EIO;
4301 }
4302 }
4303 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004304 }
4305
Chris Mason890871b2009-09-02 16:24:52 -04004306 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004307 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004308 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004309 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004310 free_extent_map(em);
4311
4312 return 0;
4313}
4314
Jeff Mahoney143bede2012-03-01 14:56:26 +01004315static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004316 struct btrfs_dev_item *dev_item,
4317 struct btrfs_device *device)
4318{
4319 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004320
4321 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004322 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4323 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004324 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4325 device->type = btrfs_device_type(leaf, dev_item);
4326 device->io_align = btrfs_device_io_align(leaf, dev_item);
4327 device->io_width = btrfs_device_io_width(leaf, dev_item);
4328 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004329
4330 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004331 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004332}
4333
Yan Zheng2b820322008-11-17 21:11:30 -05004334static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4335{
4336 struct btrfs_fs_devices *fs_devices;
4337 int ret;
4338
Li Zefanb367e472011-12-07 11:38:24 +08004339 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004340
4341 fs_devices = root->fs_info->fs_devices->seed;
4342 while (fs_devices) {
4343 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4344 ret = 0;
4345 goto out;
4346 }
4347 fs_devices = fs_devices->seed;
4348 }
4349
4350 fs_devices = find_fsid(fsid);
4351 if (!fs_devices) {
4352 ret = -ENOENT;
4353 goto out;
4354 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004355
4356 fs_devices = clone_fs_devices(fs_devices);
4357 if (IS_ERR(fs_devices)) {
4358 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004359 goto out;
4360 }
4361
Christoph Hellwig97288f22008-12-02 06:36:09 -05004362 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004363 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004364 if (ret) {
4365 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004366 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004367 }
Yan Zheng2b820322008-11-17 21:11:30 -05004368
4369 if (!fs_devices->seeding) {
4370 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004371 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004372 ret = -EINVAL;
4373 goto out;
4374 }
4375
4376 fs_devices->seed = root->fs_info->fs_devices->seed;
4377 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004378out:
Yan Zheng2b820322008-11-17 21:11:30 -05004379 return ret;
4380}
4381
Chris Mason0d81ba52008-03-24 15:02:07 -04004382static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004383 struct extent_buffer *leaf,
4384 struct btrfs_dev_item *dev_item)
4385{
4386 struct btrfs_device *device;
4387 u64 devid;
4388 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004389 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004390 u8 dev_uuid[BTRFS_UUID_SIZE];
4391
Chris Mason0b86a832008-03-24 15:01:56 -04004392 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004393 read_extent_buffer(leaf, dev_uuid,
4394 (unsigned long)btrfs_device_uuid(dev_item),
4395 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004396 read_extent_buffer(leaf, fs_uuid,
4397 (unsigned long)btrfs_device_fsid(dev_item),
4398 BTRFS_UUID_SIZE);
4399
4400 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4401 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004402 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004403 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004404 }
4405
4406 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4407 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004408 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004409 return -EIO;
4410
4411 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004412 printk(KERN_WARNING "warning devid %llu missing\n",
4413 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004414 device = add_missing_dev(root, devid, dev_uuid);
4415 if (!device)
4416 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004417 } else if (!device->missing) {
4418 /*
4419 * this happens when a device that was properly setup
4420 * in the device info lists suddenly goes bad.
4421 * device->bdev is NULL, and so we have to set
4422 * device->missing to one here
4423 */
4424 root->fs_info->fs_devices->missing_devices++;
4425 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004426 }
4427 }
4428
4429 if (device->fs_devices != root->fs_info->fs_devices) {
4430 BUG_ON(device->writeable);
4431 if (device->generation !=
4432 btrfs_device_generation(leaf, dev_item))
4433 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004434 }
Chris Mason0b86a832008-03-24 15:01:56 -04004435
4436 fill_device_from_item(leaf, dev_item, device);
4437 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004438 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004439 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004440 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004441 spin_lock(&root->fs_info->free_chunk_lock);
4442 root->fs_info->free_chunk_space += device->total_bytes -
4443 device->bytes_used;
4444 spin_unlock(&root->fs_info->free_chunk_lock);
4445 }
Chris Mason0b86a832008-03-24 15:01:56 -04004446 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004447 return ret;
4448}
4449
Yan Zhenge4404d62008-12-12 10:03:26 -05004450int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004451{
David Sterba6c417612011-04-13 15:41:04 +02004452 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004453 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004454 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004455 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004456 u8 *ptr;
4457 unsigned long sb_ptr;
4458 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004459 u32 num_stripes;
4460 u32 array_size;
4461 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004462 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004463 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004464
Yan Zhenge4404d62008-12-12 10:03:26 -05004465 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004466 BTRFS_SUPER_INFO_SIZE);
4467 if (!sb)
4468 return -ENOMEM;
4469 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004470 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004471 /*
4472 * The sb extent buffer is artifical and just used to read the system array.
4473 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4474 * pages up-to-date when the page is larger: extent does not cover the
4475 * whole page and consequently check_page_uptodate does not find all
4476 * the page's extents up-to-date (the hole beyond sb),
4477 * write_extent_buffer then triggers a WARN_ON.
4478 *
4479 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4480 * but sb spans only this function. Add an explicit SetPageUptodate call
4481 * to silence the warning eg. on PowerPC 64.
4482 */
4483 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004484 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004485
Chris Masona061fc82008-05-07 11:43:44 -04004486 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004487 array_size = btrfs_super_sys_array_size(super_copy);
4488
Chris Mason0b86a832008-03-24 15:01:56 -04004489 ptr = super_copy->sys_chunk_array;
4490 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4491 cur = 0;
4492
4493 while (cur < array_size) {
4494 disk_key = (struct btrfs_disk_key *)ptr;
4495 btrfs_disk_key_to_cpu(&key, disk_key);
4496
Chris Masona061fc82008-05-07 11:43:44 -04004497 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004498 sb_ptr += len;
4499 cur += len;
4500
Chris Mason0d81ba52008-03-24 15:02:07 -04004501 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004502 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004503 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004504 if (ret)
4505 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004506 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4507 len = btrfs_chunk_item_size(num_stripes);
4508 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004509 ret = -EIO;
4510 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004511 }
4512 ptr += len;
4513 sb_ptr += len;
4514 cur += len;
4515 }
Chris Masona061fc82008-05-07 11:43:44 -04004516 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004517 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004518}
4519
4520int btrfs_read_chunk_tree(struct btrfs_root *root)
4521{
4522 struct btrfs_path *path;
4523 struct extent_buffer *leaf;
4524 struct btrfs_key key;
4525 struct btrfs_key found_key;
4526 int ret;
4527 int slot;
4528
4529 root = root->fs_info->chunk_root;
4530
4531 path = btrfs_alloc_path();
4532 if (!path)
4533 return -ENOMEM;
4534
Li Zefanb367e472011-12-07 11:38:24 +08004535 mutex_lock(&uuid_mutex);
4536 lock_chunks(root);
4537
Chris Mason0b86a832008-03-24 15:01:56 -04004538 /* first we search for all of the device items, and then we
4539 * read in all of the chunk items. This way we can create chunk
4540 * mappings that reference all of the devices that are afound
4541 */
4542 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4543 key.offset = 0;
4544 key.type = 0;
4545again:
4546 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004547 if (ret < 0)
4548 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004549 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004550 leaf = path->nodes[0];
4551 slot = path->slots[0];
4552 if (slot >= btrfs_header_nritems(leaf)) {
4553 ret = btrfs_next_leaf(root, path);
4554 if (ret == 0)
4555 continue;
4556 if (ret < 0)
4557 goto error;
4558 break;
4559 }
4560 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4561 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4562 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4563 break;
4564 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4565 struct btrfs_dev_item *dev_item;
4566 dev_item = btrfs_item_ptr(leaf, slot,
4567 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004568 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004569 if (ret)
4570 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004571 }
4572 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4573 struct btrfs_chunk *chunk;
4574 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4575 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004576 if (ret)
4577 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004578 }
4579 path->slots[0]++;
4580 }
4581 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4582 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004583 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004584 goto again;
4585 }
Chris Mason0b86a832008-03-24 15:01:56 -04004586 ret = 0;
4587error:
Li Zefanb367e472011-12-07 11:38:24 +08004588 unlock_chunks(root);
4589 mutex_unlock(&uuid_mutex);
4590
Yan Zheng2b820322008-11-17 21:11:30 -05004591 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004592 return ret;
4593}