blob: 3ad7ee9538e416993d5fe0d2af4ffa57b9c65366 [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 }
Yan Zheng2b820322008-11-17 21:11:30 -0500594 return ret;
595}
596
Yan Zhenge4404d62008-12-12 10:03:26 -0500597static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
598 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400599{
Josef Bacikd5e20032011-08-04 14:52:27 +0000600 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400601 struct block_device *bdev;
602 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400603 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400604 struct block_device *latest_bdev = NULL;
605 struct buffer_head *bh;
606 struct btrfs_super_block *disk_super;
607 u64 latest_devid = 0;
608 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400609 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500610 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400611 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400612
Tejun Heod4d77622010-11-13 11:55:18 +0100613 flags |= FMODE_EXCL;
614
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500615 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400616 if (device->bdev)
617 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400618 if (!device->name)
619 continue;
620
Tejun Heod4d77622010-11-13 11:55:18 +0100621 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400622 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500623 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400624 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400625 }
Chris Mason3c4bb262012-03-27 18:56:56 -0400626 filemap_write_and_wait(bdev->bd_inode->i_mapping);
627 invalidate_bdev(bdev);
Chris Masona061fc82008-05-07 11:43:44 -0400628 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400629
Yan Zhenga512bbf2008-12-08 16:46:26 -0500630 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300631 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400632 goto error_close;
633
634 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000635 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400636 if (devid != device->devid)
637 goto error_brelse;
638
Yan Zheng2b820322008-11-17 21:11:30 -0500639 if (memcmp(device->uuid, disk_super->dev_item.uuid,
640 BTRFS_UUID_SIZE))
641 goto error_brelse;
642
643 device->generation = btrfs_super_generation(disk_super);
644 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400645 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500646 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400647 latest_bdev = bdev;
648 }
649
Yan Zheng2b820322008-11-17 21:11:30 -0500650 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
651 device->writeable = 0;
652 } else {
653 device->writeable = !bdev_read_only(bdev);
654 seeding = 0;
655 }
656
Josef Bacikd5e20032011-08-04 14:52:27 +0000657 q = bdev_get_queue(bdev);
658 if (blk_queue_discard(q)) {
659 device->can_discard = 1;
660 fs_devices->num_can_discard++;
661 }
662
Chris Mason8a4b83c2008-03-24 15:02:07 -0400663 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400664 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500665 device->mode = flags;
666
Chris Masonc2898112009-06-10 09:51:32 -0400667 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
668 fs_devices->rotating = 1;
669
Chris Masona0af4692008-05-13 16:03:06 -0400670 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500671 if (device->writeable) {
672 fs_devices->rw_devices++;
673 list_add(&device->dev_alloc_list,
674 &fs_devices->alloc_list);
675 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000676 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400677 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400678
Chris Masona0af4692008-05-13 16:03:06 -0400679error_brelse:
680 brelse(bh);
681error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100682 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400683error:
684 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400685 }
Chris Masona0af4692008-05-13 16:03:06 -0400686 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300687 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400688 goto out;
689 }
Yan Zheng2b820322008-11-17 21:11:30 -0500690 fs_devices->seeding = seeding;
691 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400692 fs_devices->latest_bdev = latest_bdev;
693 fs_devices->latest_devid = latest_devid;
694 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500695 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400696out:
Yan Zheng2b820322008-11-17 21:11:30 -0500697 return ret;
698}
699
700int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500701 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500702{
703 int ret;
704
705 mutex_lock(&uuid_mutex);
706 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500707 fs_devices->opened++;
708 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500709 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500710 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500711 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400712 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713 return ret;
714}
715
Christoph Hellwig97288f22008-12-02 06:36:09 -0500716int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400717 struct btrfs_fs_devices **fs_devices_ret)
718{
719 struct btrfs_super_block *disk_super;
720 struct block_device *bdev;
721 struct buffer_head *bh;
722 int ret;
723 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400724 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400725
Tejun Heod4d77622010-11-13 11:55:18 +0100726 flags |= FMODE_EXCL;
727 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400728
729 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400730 ret = PTR_ERR(bdev);
731 goto error;
732 }
733
Al Viro10f63272011-11-17 15:05:22 -0500734 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400735 ret = set_blocksize(bdev, 4096);
736 if (ret)
737 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500738 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400739 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000740 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400741 goto error_close;
742 }
743 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000744 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400745 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400746 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500747 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200748 else
749 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500750 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500751 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400752 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
753
Chris Mason8a4b83c2008-03-24 15:02:07 -0400754 brelse(bh);
755error_close:
Al Viro10f63272011-11-17 15:05:22 -0500756 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100757 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400759 return ret;
760}
Chris Mason0b86a832008-03-24 15:01:56 -0400761
Miao Xie6d07bce2011-01-05 10:07:31 +0000762/* helper to account the used device space in the range */
763int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
764 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400765{
766 struct btrfs_key key;
767 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000768 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500769 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000770 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400771 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000772 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400773 struct extent_buffer *l;
774
Miao Xie6d07bce2011-01-05 10:07:31 +0000775 *length = 0;
776
777 if (start >= device->total_bytes)
778 return 0;
779
Yan Zheng2b820322008-11-17 21:11:30 -0500780 path = btrfs_alloc_path();
781 if (!path)
782 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400783 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400784
Chris Mason0b86a832008-03-24 15:01:56 -0400785 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000786 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400787 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000788
789 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400790 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000791 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400792 if (ret > 0) {
793 ret = btrfs_previous_item(root, path, key.objectid, key.type);
794 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000795 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400796 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000797
Chris Mason0b86a832008-03-24 15:01:56 -0400798 while (1) {
799 l = path->nodes[0];
800 slot = path->slots[0];
801 if (slot >= btrfs_header_nritems(l)) {
802 ret = btrfs_next_leaf(root, path);
803 if (ret == 0)
804 continue;
805 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000806 goto out;
807
808 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400809 }
810 btrfs_item_key_to_cpu(l, &key, slot);
811
812 if (key.objectid < device->devid)
813 goto next;
814
815 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000816 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400817
Chris Masond3977122009-01-05 21:25:51 -0500818 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400819 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400820
Chris Mason0b86a832008-03-24 15:01:56 -0400821 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000822 extent_end = key.offset + btrfs_dev_extent_length(l,
823 dev_extent);
824 if (key.offset <= start && extent_end > end) {
825 *length = end - start + 1;
826 break;
827 } else if (key.offset <= start && extent_end > start)
828 *length += extent_end - start;
829 else if (key.offset > start && extent_end <= end)
830 *length += extent_end - key.offset;
831 else if (key.offset > start && key.offset <= end) {
832 *length += end - key.offset + 1;
833 break;
834 } else if (key.offset > end)
835 break;
836
837next:
838 path->slots[0]++;
839 }
840 ret = 0;
841out:
842 btrfs_free_path(path);
843 return ret;
844}
845
Chris Mason0b86a832008-03-24 15:01:56 -0400846/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000847 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000848 * @device: the device which we search the free space in
849 * @num_bytes: the size of the free space that we need
850 * @start: store the start of the free space.
851 * @len: the size of the free space. that we find, or the size of the max
852 * free space if we don't find suitable free space
853 *
Chris Mason0b86a832008-03-24 15:01:56 -0400854 * this uses a pretty simple search, the expectation is that it is
855 * called very infrequently and that a given device has a small number
856 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000857 *
858 * @start is used to store the start of the free space if we find. But if we
859 * don't find suitable free space, it will be used to store the start position
860 * of the max free space.
861 *
862 * @len is used to store the size of the free space that we find.
863 * But if we don't find suitable free space, it is used to store the size of
864 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400865 */
Li Zefan125ccb02011-12-08 15:07:24 +0800866int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000867 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400868{
869 struct btrfs_key key;
870 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000871 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400872 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000873 u64 hole_size;
874 u64 max_hole_start;
875 u64 max_hole_size;
876 u64 extent_end;
877 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400878 u64 search_end = device->total_bytes;
879 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000880 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400881 struct extent_buffer *l;
882
Chris Mason0b86a832008-03-24 15:01:56 -0400883 /* FIXME use last free of some kind */
884
885 /* we don't want to overwrite the superblock on the drive,
886 * so we make sure to start at an offset of at least 1MB
887 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200888 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400889
Miao Xie7bfc8372011-01-05 10:07:26 +0000890 max_hole_start = search_start;
891 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000892 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000893
894 if (search_start >= search_end) {
895 ret = -ENOSPC;
896 goto error;
897 }
898
899 path = btrfs_alloc_path();
900 if (!path) {
901 ret = -ENOMEM;
902 goto error;
903 }
904 path->reada = 2;
905
Chris Mason0b86a832008-03-24 15:01:56 -0400906 key.objectid = device->devid;
907 key.offset = search_start;
908 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000909
Li Zefan125ccb02011-12-08 15:07:24 +0800910 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400911 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000912 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400913 if (ret > 0) {
914 ret = btrfs_previous_item(root, path, key.objectid, key.type);
915 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000916 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400917 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000918
Chris Mason0b86a832008-03-24 15:01:56 -0400919 while (1) {
920 l = path->nodes[0];
921 slot = path->slots[0];
922 if (slot >= btrfs_header_nritems(l)) {
923 ret = btrfs_next_leaf(root, path);
924 if (ret == 0)
925 continue;
926 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000927 goto out;
928
929 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400930 }
931 btrfs_item_key_to_cpu(l, &key, slot);
932
933 if (key.objectid < device->devid)
934 goto next;
935
936 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000937 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400938
Chris Mason0b86a832008-03-24 15:01:56 -0400939 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
940 goto next;
941
Miao Xie7bfc8372011-01-05 10:07:26 +0000942 if (key.offset > search_start) {
943 hole_size = key.offset - search_start;
944
945 if (hole_size > max_hole_size) {
946 max_hole_start = search_start;
947 max_hole_size = hole_size;
948 }
949
950 /*
951 * If this free space is greater than which we need,
952 * it must be the max free space that we have found
953 * until now, so max_hole_start must point to the start
954 * of this free space and the length of this free space
955 * is stored in max_hole_size. Thus, we return
956 * max_hole_start and max_hole_size and go back to the
957 * caller.
958 */
959 if (hole_size >= num_bytes) {
960 ret = 0;
961 goto out;
962 }
963 }
964
Chris Mason0b86a832008-03-24 15:01:56 -0400965 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000966 extent_end = key.offset + btrfs_dev_extent_length(l,
967 dev_extent);
968 if (extent_end > search_start)
969 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400970next:
971 path->slots[0]++;
972 cond_resched();
973 }
Chris Mason0b86a832008-03-24 15:01:56 -0400974
liubo38c01b92011-08-02 02:39:03 +0000975 /*
976 * At this point, search_start should be the end of
977 * allocated dev extents, and when shrinking the device,
978 * search_end may be smaller than search_start.
979 */
980 if (search_end > search_start)
981 hole_size = search_end - search_start;
982
Miao Xie7bfc8372011-01-05 10:07:26 +0000983 if (hole_size > max_hole_size) {
984 max_hole_start = search_start;
985 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400986 }
Chris Mason0b86a832008-03-24 15:01:56 -0400987
Miao Xie7bfc8372011-01-05 10:07:26 +0000988 /* See above. */
989 if (hole_size < num_bytes)
990 ret = -ENOSPC;
991 else
992 ret = 0;
993
994out:
Yan Zheng2b820322008-11-17 21:11:30 -0500995 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000996error:
997 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000998 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000999 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001000 return ret;
1001}
1002
Christoph Hellwigb2950862008-12-02 09:54:17 -05001003static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001004 struct btrfs_device *device,
1005 u64 start)
1006{
1007 int ret;
1008 struct btrfs_path *path;
1009 struct btrfs_root *root = device->dev_root;
1010 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001011 struct btrfs_key found_key;
1012 struct extent_buffer *leaf = NULL;
1013 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001014
1015 path = btrfs_alloc_path();
1016 if (!path)
1017 return -ENOMEM;
1018
1019 key.objectid = device->devid;
1020 key.offset = start;
1021 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001022again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001023 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001024 if (ret > 0) {
1025 ret = btrfs_previous_item(root, path, key.objectid,
1026 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001027 if (ret)
1028 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001029 leaf = path->nodes[0];
1030 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1031 extent = btrfs_item_ptr(leaf, path->slots[0],
1032 struct btrfs_dev_extent);
1033 BUG_ON(found_key.offset > start || found_key.offset +
1034 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001035 key = found_key;
1036 btrfs_release_path(path);
1037 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001038 } else if (ret == 0) {
1039 leaf = path->nodes[0];
1040 extent = btrfs_item_ptr(leaf, path->slots[0],
1041 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001042 } else {
1043 btrfs_error(root->fs_info, ret, "Slot search failed");
1044 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001045 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001046
Josef Bacik2bf64752011-09-26 17:12:22 -04001047 if (device->bytes_used > 0) {
1048 u64 len = btrfs_dev_extent_length(leaf, extent);
1049 device->bytes_used -= len;
1050 spin_lock(&root->fs_info->free_chunk_lock);
1051 root->fs_info->free_chunk_space += len;
1052 spin_unlock(&root->fs_info->free_chunk_lock);
1053 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001054 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001055 if (ret) {
1056 btrfs_error(root->fs_info, ret,
1057 "Failed to remove dev extent item");
1058 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001059out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001060 btrfs_free_path(path);
1061 return ret;
1062}
1063
Yan Zheng2b820322008-11-17 21:11:30 -05001064int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001065 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001066 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001067 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001068{
1069 int ret;
1070 struct btrfs_path *path;
1071 struct btrfs_root *root = device->dev_root;
1072 struct btrfs_dev_extent *extent;
1073 struct extent_buffer *leaf;
1074 struct btrfs_key key;
1075
Chris Masondfe25022008-05-13 13:46:40 -04001076 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001077 path = btrfs_alloc_path();
1078 if (!path)
1079 return -ENOMEM;
1080
Chris Mason0b86a832008-03-24 15:01:56 -04001081 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001082 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001083 key.type = BTRFS_DEV_EXTENT_KEY;
1084 ret = btrfs_insert_empty_item(trans, root, path, &key,
1085 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001086 if (ret)
1087 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001088
1089 leaf = path->nodes[0];
1090 extent = btrfs_item_ptr(leaf, path->slots[0],
1091 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001092 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1093 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1094 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1095
1096 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1097 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1098 BTRFS_UUID_SIZE);
1099
Chris Mason0b86a832008-03-24 15:01:56 -04001100 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1101 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001102out:
Chris Mason0b86a832008-03-24 15:01:56 -04001103 btrfs_free_path(path);
1104 return ret;
1105}
1106
Chris Masona1b32a52008-09-05 16:09:51 -04001107static noinline int find_next_chunk(struct btrfs_root *root,
1108 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001109{
1110 struct btrfs_path *path;
1111 int ret;
1112 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001113 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001114 struct btrfs_key found_key;
1115
1116 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001117 if (!path)
1118 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001119
Chris Masone17cade2008-04-15 15:41:47 -04001120 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001121 key.offset = (u64)-1;
1122 key.type = BTRFS_CHUNK_ITEM_KEY;
1123
1124 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1125 if (ret < 0)
1126 goto error;
1127
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001128 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001129
1130 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1131 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001132 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001133 } else {
1134 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1135 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001136 if (found_key.objectid != objectid)
1137 *offset = 0;
1138 else {
1139 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1140 struct btrfs_chunk);
1141 *offset = found_key.offset +
1142 btrfs_chunk_length(path->nodes[0], chunk);
1143 }
Chris Mason0b86a832008-03-24 15:01:56 -04001144 }
1145 ret = 0;
1146error:
1147 btrfs_free_path(path);
1148 return ret;
1149}
1150
Yan Zheng2b820322008-11-17 21:11:30 -05001151static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001152{
1153 int ret;
1154 struct btrfs_key key;
1155 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001156 struct btrfs_path *path;
1157
1158 root = root->fs_info->chunk_root;
1159
1160 path = btrfs_alloc_path();
1161 if (!path)
1162 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001163
1164 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1165 key.type = BTRFS_DEV_ITEM_KEY;
1166 key.offset = (u64)-1;
1167
1168 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1169 if (ret < 0)
1170 goto error;
1171
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001172 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001173
1174 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1175 BTRFS_DEV_ITEM_KEY);
1176 if (ret) {
1177 *objectid = 1;
1178 } else {
1179 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1180 path->slots[0]);
1181 *objectid = found_key.offset + 1;
1182 }
1183 ret = 0;
1184error:
Yan Zheng2b820322008-11-17 21:11:30 -05001185 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001186 return ret;
1187}
1188
1189/*
1190 * the device information is stored in the chunk root
1191 * the btrfs_device struct should be fully filled in
1192 */
1193int btrfs_add_device(struct btrfs_trans_handle *trans,
1194 struct btrfs_root *root,
1195 struct btrfs_device *device)
1196{
1197 int ret;
1198 struct btrfs_path *path;
1199 struct btrfs_dev_item *dev_item;
1200 struct extent_buffer *leaf;
1201 struct btrfs_key key;
1202 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001203
1204 root = root->fs_info->chunk_root;
1205
1206 path = btrfs_alloc_path();
1207 if (!path)
1208 return -ENOMEM;
1209
Chris Mason0b86a832008-03-24 15:01:56 -04001210 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1211 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001212 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001213
1214 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001215 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001216 if (ret)
1217 goto out;
1218
1219 leaf = path->nodes[0];
1220 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1221
1222 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001223 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001224 btrfs_set_device_type(leaf, dev_item, device->type);
1225 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1226 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1227 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001228 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1229 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001230 btrfs_set_device_group(leaf, dev_item, 0);
1231 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1232 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001233 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001234
Chris Mason0b86a832008-03-24 15:01:56 -04001235 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001236 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001237 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1238 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001239 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001240
Yan Zheng2b820322008-11-17 21:11:30 -05001241 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001242out:
1243 btrfs_free_path(path);
1244 return ret;
1245}
Chris Mason8f18cf12008-04-25 16:53:30 -04001246
Chris Masona061fc82008-05-07 11:43:44 -04001247static int btrfs_rm_dev_item(struct btrfs_root *root,
1248 struct btrfs_device *device)
1249{
1250 int ret;
1251 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001252 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001253 struct btrfs_trans_handle *trans;
1254
1255 root = root->fs_info->chunk_root;
1256
1257 path = btrfs_alloc_path();
1258 if (!path)
1259 return -ENOMEM;
1260
Yan, Zhenga22285a2010-05-16 10:48:46 -04001261 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001262 if (IS_ERR(trans)) {
1263 btrfs_free_path(path);
1264 return PTR_ERR(trans);
1265 }
Chris Masona061fc82008-05-07 11:43:44 -04001266 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1267 key.type = BTRFS_DEV_ITEM_KEY;
1268 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001269 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001270
1271 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1272 if (ret < 0)
1273 goto out;
1274
1275 if (ret > 0) {
1276 ret = -ENOENT;
1277 goto out;
1278 }
1279
1280 ret = btrfs_del_item(trans, root, path);
1281 if (ret)
1282 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001283out:
1284 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001285 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001286 btrfs_commit_transaction(trans, root);
1287 return ret;
1288}
1289
1290int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1291{
1292 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001293 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001294 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001295 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001296 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001297 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001298 u64 all_avail;
1299 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001300 u64 num_devices;
1301 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001302 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001303 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001304
Chris Masona061fc82008-05-07 11:43:44 -04001305 mutex_lock(&uuid_mutex);
1306
1307 all_avail = root->fs_info->avail_data_alloc_bits |
1308 root->fs_info->avail_system_alloc_bits |
1309 root->fs_info->avail_metadata_alloc_bits;
1310
1311 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001312 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001313 printk(KERN_ERR "btrfs: unable to go below four devices "
1314 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001315 ret = -EINVAL;
1316 goto out;
1317 }
1318
1319 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001320 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001321 printk(KERN_ERR "btrfs: unable to go below two "
1322 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001323 ret = -EINVAL;
1324 goto out;
1325 }
1326
Chris Masondfe25022008-05-13 13:46:40 -04001327 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001328 struct list_head *devices;
1329 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001330
Chris Masondfe25022008-05-13 13:46:40 -04001331 device = NULL;
1332 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001333 /*
1334 * It is safe to read the devices since the volume_mutex
1335 * is held.
1336 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001337 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001338 if (tmp->in_fs_metadata && !tmp->bdev) {
1339 device = tmp;
1340 break;
1341 }
1342 }
1343 bdev = NULL;
1344 bh = NULL;
1345 disk_super = NULL;
1346 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001347 printk(KERN_ERR "btrfs: no missing devices found to "
1348 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001349 goto out;
1350 }
Chris Masondfe25022008-05-13 13:46:40 -04001351 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001352 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1353 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001354 if (IS_ERR(bdev)) {
1355 ret = PTR_ERR(bdev);
1356 goto out;
1357 }
1358
Yan Zheng2b820322008-11-17 21:11:30 -05001359 set_blocksize(bdev, 4096);
Chris Mason3c4bb262012-03-27 18:56:56 -04001360 invalidate_bdev(bdev);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001361 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001362 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001363 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001364 goto error_close;
1365 }
1366 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001367 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001368 dev_uuid = disk_super->dev_item.uuid;
1369 device = btrfs_find_device(root, devid, dev_uuid,
1370 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001371 if (!device) {
1372 ret = -ENOENT;
1373 goto error_brelse;
1374 }
Chris Masondfe25022008-05-13 13:46:40 -04001375 }
Yan Zheng2b820322008-11-17 21:11:30 -05001376
1377 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001378 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1379 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001380 ret = -EINVAL;
1381 goto error_brelse;
1382 }
1383
1384 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001385 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001386 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001387 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001388 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001389 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001390 }
Chris Masona061fc82008-05-07 11:43:44 -04001391
1392 ret = btrfs_shrink_device(device, 0);
1393 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001394 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001395
Chris Masona061fc82008-05-07 11:43:44 -04001396 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1397 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001398 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001399
Josef Bacik2bf64752011-09-26 17:12:22 -04001400 spin_lock(&root->fs_info->free_chunk_lock);
1401 root->fs_info->free_chunk_space = device->total_bytes -
1402 device->bytes_used;
1403 spin_unlock(&root->fs_info->free_chunk_lock);
1404
Yan Zheng2b820322008-11-17 21:11:30 -05001405 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001406 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001407
1408 /*
1409 * the device list mutex makes sure that we don't change
1410 * the device list while someone else is writing out all
1411 * the device supers.
1412 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001413
1414 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001415 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001416 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001417
Yan Zhenge4404d62008-12-12 10:03:26 -05001418 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001419
Chris Masoncd02dca2010-12-13 14:56:23 -05001420 if (device->missing)
1421 root->fs_info->fs_devices->missing_devices--;
1422
Yan Zheng2b820322008-11-17 21:11:30 -05001423 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1424 struct btrfs_device, dev_list);
1425 if (device->bdev == root->fs_info->sb->s_bdev)
1426 root->fs_info->sb->s_bdev = next_device->bdev;
1427 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1428 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1429
Xiao Guangrong1f781602011-04-20 10:09:16 +00001430 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001431 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001432
1433 call_rcu(&device->rcu, free_device);
1434 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001435
David Sterba6c417612011-04-13 15:41:04 +02001436 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1437 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001438
Xiao Guangrong1f781602011-04-20 10:09:16 +00001439 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001440 struct btrfs_fs_devices *fs_devices;
1441 fs_devices = root->fs_info->fs_devices;
1442 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001443 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001444 break;
1445 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001446 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001447 fs_devices->seed = cur_devices->seed;
1448 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001449 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001450 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001451 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001452 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001453 }
1454
1455 /*
1456 * at this point, the device is zero sized. We want to
1457 * remove it from the devices list and zero out the old super
1458 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001459 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001460 /* make sure this device isn't detected as part of
1461 * the FS anymore
1462 */
1463 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1464 set_buffer_dirty(bh);
1465 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001466 }
Chris Masona061fc82008-05-07 11:43:44 -04001467
Chris Masona061fc82008-05-07 11:43:44 -04001468 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001469
1470error_brelse:
1471 brelse(bh);
1472error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001473 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001474 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001475out:
1476 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001477 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001478error_undo:
1479 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001480 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001481 list_add(&device->dev_alloc_list,
1482 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001483 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001484 root->fs_info->fs_devices->rw_devices++;
1485 }
1486 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001487}
1488
Yan Zheng2b820322008-11-17 21:11:30 -05001489/*
1490 * does all the dirty work required for changing file system's UUID.
1491 */
Li Zefan125ccb02011-12-08 15:07:24 +08001492static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001493{
1494 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1495 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001496 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001497 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001498 struct btrfs_device *device;
1499 u64 super_flags;
1500
1501 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001502 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001503 return -EINVAL;
1504
Yan Zhenge4404d62008-12-12 10:03:26 -05001505 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1506 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001507 return -ENOMEM;
1508
Yan Zhenge4404d62008-12-12 10:03:26 -05001509 old_devices = clone_fs_devices(fs_devices);
1510 if (IS_ERR(old_devices)) {
1511 kfree(seed_devices);
1512 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001513 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001514
Yan Zheng2b820322008-11-17 21:11:30 -05001515 list_add(&old_devices->list, &fs_uuids);
1516
Yan Zhenge4404d62008-12-12 10:03:26 -05001517 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1518 seed_devices->opened = 1;
1519 INIT_LIST_HEAD(&seed_devices->devices);
1520 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001521 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001522
1523 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001524 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1525 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001526 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1527
Yan Zhenge4404d62008-12-12 10:03:26 -05001528 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1529 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1530 device->fs_devices = seed_devices;
1531 }
1532
Yan Zheng2b820322008-11-17 21:11:30 -05001533 fs_devices->seeding = 0;
1534 fs_devices->num_devices = 0;
1535 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001536 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001537
1538 generate_random_uuid(fs_devices->fsid);
1539 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1540 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1541 super_flags = btrfs_super_flags(disk_super) &
1542 ~BTRFS_SUPER_FLAG_SEEDING;
1543 btrfs_set_super_flags(disk_super, super_flags);
1544
1545 return 0;
1546}
1547
1548/*
1549 * strore the expected generation for seed devices in device items.
1550 */
1551static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1552 struct btrfs_root *root)
1553{
1554 struct btrfs_path *path;
1555 struct extent_buffer *leaf;
1556 struct btrfs_dev_item *dev_item;
1557 struct btrfs_device *device;
1558 struct btrfs_key key;
1559 u8 fs_uuid[BTRFS_UUID_SIZE];
1560 u8 dev_uuid[BTRFS_UUID_SIZE];
1561 u64 devid;
1562 int ret;
1563
1564 path = btrfs_alloc_path();
1565 if (!path)
1566 return -ENOMEM;
1567
1568 root = root->fs_info->chunk_root;
1569 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1570 key.offset = 0;
1571 key.type = BTRFS_DEV_ITEM_KEY;
1572
1573 while (1) {
1574 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1575 if (ret < 0)
1576 goto error;
1577
1578 leaf = path->nodes[0];
1579next_slot:
1580 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1581 ret = btrfs_next_leaf(root, path);
1582 if (ret > 0)
1583 break;
1584 if (ret < 0)
1585 goto error;
1586 leaf = path->nodes[0];
1587 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001588 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001589 continue;
1590 }
1591
1592 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1593 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1594 key.type != BTRFS_DEV_ITEM_KEY)
1595 break;
1596
1597 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1598 struct btrfs_dev_item);
1599 devid = btrfs_device_id(leaf, dev_item);
1600 read_extent_buffer(leaf, dev_uuid,
1601 (unsigned long)btrfs_device_uuid(dev_item),
1602 BTRFS_UUID_SIZE);
1603 read_extent_buffer(leaf, fs_uuid,
1604 (unsigned long)btrfs_device_fsid(dev_item),
1605 BTRFS_UUID_SIZE);
1606 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001607 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001608
1609 if (device->fs_devices->seeding) {
1610 btrfs_set_device_generation(leaf, dev_item,
1611 device->generation);
1612 btrfs_mark_buffer_dirty(leaf);
1613 }
1614
1615 path->slots[0]++;
1616 goto next_slot;
1617 }
1618 ret = 0;
1619error:
1620 btrfs_free_path(path);
1621 return ret;
1622}
1623
Chris Mason788f20e2008-04-28 15:29:42 -04001624int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1625{
Josef Bacikd5e20032011-08-04 14:52:27 +00001626 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001627 struct btrfs_trans_handle *trans;
1628 struct btrfs_device *device;
1629 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001630 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001631 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001632 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001633 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001634 int ret = 0;
1635
Yan Zheng2b820322008-11-17 21:11:30 -05001636 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1637 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001638
Li Zefana5d16332011-12-07 20:08:40 -05001639 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001640 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001641 if (IS_ERR(bdev))
1642 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001643
Yan Zheng2b820322008-11-17 21:11:30 -05001644 if (root->fs_info->fs_devices->seeding) {
1645 seeding_dev = 1;
1646 down_write(&sb->s_umount);
1647 mutex_lock(&uuid_mutex);
1648 }
1649
Chris Mason8c8bee12008-09-29 11:19:10 -04001650 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001651
Chris Mason788f20e2008-04-28 15:29:42 -04001652 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001653 /*
1654 * we have the volume lock, so we don't need the extra
1655 * device list mutex while reading the list here.
1656 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001657 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001658 if (device->bdev == bdev) {
1659 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001660 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001661 }
1662 }
1663
1664 device = kzalloc(sizeof(*device), GFP_NOFS);
1665 if (!device) {
1666 /* we can safely leave the fs_devices entry around */
1667 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001668 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001669 }
1670
Chris Mason788f20e2008-04-28 15:29:42 -04001671 device->name = kstrdup(device_path, GFP_NOFS);
1672 if (!device->name) {
1673 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001674 ret = -ENOMEM;
1675 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001676 }
Yan Zheng2b820322008-11-17 21:11:30 -05001677
1678 ret = find_next_devid(root, &device->devid);
1679 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001680 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001681 kfree(device);
1682 goto error;
1683 }
1684
Yan, Zhenga22285a2010-05-16 10:48:46 -04001685 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001686 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001687 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001688 kfree(device);
1689 ret = PTR_ERR(trans);
1690 goto error;
1691 }
1692
Yan Zheng2b820322008-11-17 21:11:30 -05001693 lock_chunks(root);
1694
Josef Bacikd5e20032011-08-04 14:52:27 +00001695 q = bdev_get_queue(bdev);
1696 if (blk_queue_discard(q))
1697 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001698 device->writeable = 1;
1699 device->work.func = pending_bios_fn;
1700 generate_random_uuid(device->uuid);
1701 spin_lock_init(&device->io_lock);
1702 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001703 device->io_width = root->sectorsize;
1704 device->io_align = root->sectorsize;
1705 device->sector_size = root->sectorsize;
1706 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001707 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001708 device->dev_root = root->fs_info->dev_root;
1709 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001710 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001711 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001712 set_blocksize(device->bdev, 4096);
1713
Yan Zheng2b820322008-11-17 21:11:30 -05001714 if (seeding_dev) {
1715 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001716 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001717 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001718 }
1719
1720 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001721
1722 /*
1723 * we don't want write_supers to jump in here with our device
1724 * half setup
1725 */
1726 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001727 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001728 list_add(&device->dev_alloc_list,
1729 &root->fs_info->fs_devices->alloc_list);
1730 root->fs_info->fs_devices->num_devices++;
1731 root->fs_info->fs_devices->open_devices++;
1732 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001733 if (device->can_discard)
1734 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001735 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1736
Josef Bacik2bf64752011-09-26 17:12:22 -04001737 spin_lock(&root->fs_info->free_chunk_lock);
1738 root->fs_info->free_chunk_space += device->total_bytes;
1739 spin_unlock(&root->fs_info->free_chunk_lock);
1740
Chris Masonc2898112009-06-10 09:51:32 -04001741 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1742 root->fs_info->fs_devices->rotating = 1;
1743
David Sterba6c417612011-04-13 15:41:04 +02001744 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1745 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001746 total_bytes + device->total_bytes);
1747
David Sterba6c417612011-04-13 15:41:04 +02001748 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1749 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001750 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001751 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001752
Yan Zheng2b820322008-11-17 21:11:30 -05001753 if (seeding_dev) {
1754 ret = init_first_rw_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001755 if (ret)
1756 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001757 ret = btrfs_finish_sprout(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001758 if (ret)
1759 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001760 } else {
1761 ret = btrfs_add_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 }
1765
Chris Mason913d9522009-03-10 13:17:18 -04001766 /*
1767 * we've got more storage, clear any full flags on the space
1768 * infos
1769 */
1770 btrfs_clear_space_info_full(root->fs_info);
1771
Chris Mason7d9eb122008-07-08 14:19:17 -04001772 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001773 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001774
1775 if (seeding_dev) {
1776 mutex_unlock(&uuid_mutex);
1777 up_write(&sb->s_umount);
1778
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001779 if (ret) /* transaction commit */
1780 return ret;
1781
Yan Zheng2b820322008-11-17 21:11:30 -05001782 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001783 if (ret < 0)
1784 btrfs_error(root->fs_info, ret,
1785 "Failed to relocate sys chunks after "
1786 "device initialization. This can be fixed "
1787 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001788 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001789
Chris Mason788f20e2008-04-28 15:29:42 -04001790 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001791
1792error_trans:
1793 unlock_chunks(root);
1794 btrfs_abort_transaction(trans, root, ret);
1795 btrfs_end_transaction(trans, root);
1796 kfree(device->name);
1797 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001798error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001799 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001800 if (seeding_dev) {
1801 mutex_unlock(&uuid_mutex);
1802 up_write(&sb->s_umount);
1803 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001804 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001805}
1806
Chris Masond3977122009-01-05 21:25:51 -05001807static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1808 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001809{
1810 int ret;
1811 struct btrfs_path *path;
1812 struct btrfs_root *root;
1813 struct btrfs_dev_item *dev_item;
1814 struct extent_buffer *leaf;
1815 struct btrfs_key key;
1816
1817 root = device->dev_root->fs_info->chunk_root;
1818
1819 path = btrfs_alloc_path();
1820 if (!path)
1821 return -ENOMEM;
1822
1823 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1824 key.type = BTRFS_DEV_ITEM_KEY;
1825 key.offset = device->devid;
1826
1827 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1828 if (ret < 0)
1829 goto out;
1830
1831 if (ret > 0) {
1832 ret = -ENOENT;
1833 goto out;
1834 }
1835
1836 leaf = path->nodes[0];
1837 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1838
1839 btrfs_set_device_id(leaf, dev_item, device->devid);
1840 btrfs_set_device_type(leaf, dev_item, device->type);
1841 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1842 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1843 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001844 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001845 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1846 btrfs_mark_buffer_dirty(leaf);
1847
1848out:
1849 btrfs_free_path(path);
1850 return ret;
1851}
1852
Chris Mason7d9eb122008-07-08 14:19:17 -04001853static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001854 struct btrfs_device *device, u64 new_size)
1855{
1856 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001857 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001858 u64 old_total = btrfs_super_total_bytes(super_copy);
1859 u64 diff = new_size - device->total_bytes;
1860
Yan Zheng2b820322008-11-17 21:11:30 -05001861 if (!device->writeable)
1862 return -EACCES;
1863 if (new_size <= device->total_bytes)
1864 return -EINVAL;
1865
Chris Mason8f18cf12008-04-25 16:53:30 -04001866 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001867 device->fs_devices->total_rw_bytes += diff;
1868
1869 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001870 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001871 btrfs_clear_space_info_full(device->dev_root->fs_info);
1872
Chris Mason8f18cf12008-04-25 16:53:30 -04001873 return btrfs_update_device(trans, device);
1874}
1875
Chris Mason7d9eb122008-07-08 14:19:17 -04001876int btrfs_grow_device(struct btrfs_trans_handle *trans,
1877 struct btrfs_device *device, u64 new_size)
1878{
1879 int ret;
1880 lock_chunks(device->dev_root);
1881 ret = __btrfs_grow_device(trans, device, new_size);
1882 unlock_chunks(device->dev_root);
1883 return ret;
1884}
1885
Chris Mason8f18cf12008-04-25 16:53:30 -04001886static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1887 struct btrfs_root *root,
1888 u64 chunk_tree, u64 chunk_objectid,
1889 u64 chunk_offset)
1890{
1891 int ret;
1892 struct btrfs_path *path;
1893 struct btrfs_key key;
1894
1895 root = root->fs_info->chunk_root;
1896 path = btrfs_alloc_path();
1897 if (!path)
1898 return -ENOMEM;
1899
1900 key.objectid = chunk_objectid;
1901 key.offset = chunk_offset;
1902 key.type = BTRFS_CHUNK_ITEM_KEY;
1903
1904 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001905 if (ret < 0)
1906 goto out;
1907 else if (ret > 0) { /* Logic error or corruption */
1908 btrfs_error(root->fs_info, -ENOENT,
1909 "Failed lookup while freeing chunk.");
1910 ret = -ENOENT;
1911 goto out;
1912 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001913
1914 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001915 if (ret < 0)
1916 btrfs_error(root->fs_info, ret,
1917 "Failed to delete chunk item.");
1918out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001919 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001920 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001921}
1922
Christoph Hellwigb2950862008-12-02 09:54:17 -05001923static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001924 chunk_offset)
1925{
David Sterba6c417612011-04-13 15:41:04 +02001926 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001927 struct btrfs_disk_key *disk_key;
1928 struct btrfs_chunk *chunk;
1929 u8 *ptr;
1930 int ret = 0;
1931 u32 num_stripes;
1932 u32 array_size;
1933 u32 len = 0;
1934 u32 cur;
1935 struct btrfs_key key;
1936
1937 array_size = btrfs_super_sys_array_size(super_copy);
1938
1939 ptr = super_copy->sys_chunk_array;
1940 cur = 0;
1941
1942 while (cur < array_size) {
1943 disk_key = (struct btrfs_disk_key *)ptr;
1944 btrfs_disk_key_to_cpu(&key, disk_key);
1945
1946 len = sizeof(*disk_key);
1947
1948 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1949 chunk = (struct btrfs_chunk *)(ptr + len);
1950 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1951 len += btrfs_chunk_item_size(num_stripes);
1952 } else {
1953 ret = -EIO;
1954 break;
1955 }
1956 if (key.objectid == chunk_objectid &&
1957 key.offset == chunk_offset) {
1958 memmove(ptr, ptr + len, array_size - (cur + len));
1959 array_size -= len;
1960 btrfs_set_super_sys_array_size(super_copy, array_size);
1961 } else {
1962 ptr += len;
1963 cur += len;
1964 }
1965 }
1966 return ret;
1967}
1968
Christoph Hellwigb2950862008-12-02 09:54:17 -05001969static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001970 u64 chunk_tree, u64 chunk_objectid,
1971 u64 chunk_offset)
1972{
1973 struct extent_map_tree *em_tree;
1974 struct btrfs_root *extent_root;
1975 struct btrfs_trans_handle *trans;
1976 struct extent_map *em;
1977 struct map_lookup *map;
1978 int ret;
1979 int i;
1980
1981 root = root->fs_info->chunk_root;
1982 extent_root = root->fs_info->extent_root;
1983 em_tree = &root->fs_info->mapping_tree.map_tree;
1984
Josef Bacikba1bf482009-09-11 16:11:19 -04001985 ret = btrfs_can_relocate(extent_root, chunk_offset);
1986 if (ret)
1987 return -ENOSPC;
1988
Chris Mason8f18cf12008-04-25 16:53:30 -04001989 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001990 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001991 if (ret)
1992 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001993
Yan, Zhenga22285a2010-05-16 10:48:46 -04001994 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001995 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001996
Chris Mason7d9eb122008-07-08 14:19:17 -04001997 lock_chunks(root);
1998
Chris Mason8f18cf12008-04-25 16:53:30 -04001999 /*
2000 * step two, delete the device extents and the
2001 * chunk tree entries
2002 */
Chris Mason890871b2009-09-02 16:24:52 -04002003 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002004 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002005 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002006
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002007 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002008 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002009 map = (struct map_lookup *)em->bdev;
2010
2011 for (i = 0; i < map->num_stripes; i++) {
2012 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2013 map->stripes[i].physical);
2014 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002015
Chris Masondfe25022008-05-13 13:46:40 -04002016 if (map->stripes[i].dev) {
2017 ret = btrfs_update_device(trans, map->stripes[i].dev);
2018 BUG_ON(ret);
2019 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002020 }
2021 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2022 chunk_offset);
2023
2024 BUG_ON(ret);
2025
liubo1abe9b82011-03-24 11:18:59 +00002026 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2027
Chris Mason8f18cf12008-04-25 16:53:30 -04002028 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2029 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2030 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002031 }
2032
Zheng Yan1a40e232008-09-26 10:09:34 -04002033 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2034 BUG_ON(ret);
2035
Chris Mason890871b2009-09-02 16:24:52 -04002036 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002037 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002038 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002039
Chris Mason8f18cf12008-04-25 16:53:30 -04002040 kfree(map);
2041 em->bdev = NULL;
2042
2043 /* once for the tree */
2044 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002045 /* once for us */
2046 free_extent_map(em);
2047
Chris Mason7d9eb122008-07-08 14:19:17 -04002048 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002049 btrfs_end_transaction(trans, root);
2050 return 0;
2051}
2052
Yan Zheng2b820322008-11-17 21:11:30 -05002053static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2054{
2055 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2056 struct btrfs_path *path;
2057 struct extent_buffer *leaf;
2058 struct btrfs_chunk *chunk;
2059 struct btrfs_key key;
2060 struct btrfs_key found_key;
2061 u64 chunk_tree = chunk_root->root_key.objectid;
2062 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002063 bool retried = false;
2064 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002065 int ret;
2066
2067 path = btrfs_alloc_path();
2068 if (!path)
2069 return -ENOMEM;
2070
Josef Bacikba1bf482009-09-11 16:11:19 -04002071again:
Yan Zheng2b820322008-11-17 21:11:30 -05002072 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2073 key.offset = (u64)-1;
2074 key.type = BTRFS_CHUNK_ITEM_KEY;
2075
2076 while (1) {
2077 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2078 if (ret < 0)
2079 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002080 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002081
2082 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2083 key.type);
2084 if (ret < 0)
2085 goto error;
2086 if (ret > 0)
2087 break;
2088
2089 leaf = path->nodes[0];
2090 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2091
2092 chunk = btrfs_item_ptr(leaf, path->slots[0],
2093 struct btrfs_chunk);
2094 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002095 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002096
2097 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2098 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2099 found_key.objectid,
2100 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002101 if (ret == -ENOSPC)
2102 failed++;
2103 else if (ret)
2104 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002105 }
2106
2107 if (found_key.offset == 0)
2108 break;
2109 key.offset = found_key.offset - 1;
2110 }
2111 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002112 if (failed && !retried) {
2113 failed = 0;
2114 retried = true;
2115 goto again;
2116 } else if (failed && retried) {
2117 WARN_ON(1);
2118 ret = -ENOSPC;
2119 }
Yan Zheng2b820322008-11-17 21:11:30 -05002120error:
2121 btrfs_free_path(path);
2122 return ret;
2123}
2124
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002125static int insert_balance_item(struct btrfs_root *root,
2126 struct btrfs_balance_control *bctl)
2127{
2128 struct btrfs_trans_handle *trans;
2129 struct btrfs_balance_item *item;
2130 struct btrfs_disk_balance_args disk_bargs;
2131 struct btrfs_path *path;
2132 struct extent_buffer *leaf;
2133 struct btrfs_key key;
2134 int ret, err;
2135
2136 path = btrfs_alloc_path();
2137 if (!path)
2138 return -ENOMEM;
2139
2140 trans = btrfs_start_transaction(root, 0);
2141 if (IS_ERR(trans)) {
2142 btrfs_free_path(path);
2143 return PTR_ERR(trans);
2144 }
2145
2146 key.objectid = BTRFS_BALANCE_OBJECTID;
2147 key.type = BTRFS_BALANCE_ITEM_KEY;
2148 key.offset = 0;
2149
2150 ret = btrfs_insert_empty_item(trans, root, path, &key,
2151 sizeof(*item));
2152 if (ret)
2153 goto out;
2154
2155 leaf = path->nodes[0];
2156 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2157
2158 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2159
2160 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2161 btrfs_set_balance_data(leaf, item, &disk_bargs);
2162 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2163 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2164 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2165 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2166
2167 btrfs_set_balance_flags(leaf, item, bctl->flags);
2168
2169 btrfs_mark_buffer_dirty(leaf);
2170out:
2171 btrfs_free_path(path);
2172 err = btrfs_commit_transaction(trans, root);
2173 if (err && !ret)
2174 ret = err;
2175 return ret;
2176}
2177
2178static int del_balance_item(struct btrfs_root *root)
2179{
2180 struct btrfs_trans_handle *trans;
2181 struct btrfs_path *path;
2182 struct btrfs_key key;
2183 int ret, err;
2184
2185 path = btrfs_alloc_path();
2186 if (!path)
2187 return -ENOMEM;
2188
2189 trans = btrfs_start_transaction(root, 0);
2190 if (IS_ERR(trans)) {
2191 btrfs_free_path(path);
2192 return PTR_ERR(trans);
2193 }
2194
2195 key.objectid = BTRFS_BALANCE_OBJECTID;
2196 key.type = BTRFS_BALANCE_ITEM_KEY;
2197 key.offset = 0;
2198
2199 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2200 if (ret < 0)
2201 goto out;
2202 if (ret > 0) {
2203 ret = -ENOENT;
2204 goto out;
2205 }
2206
2207 ret = btrfs_del_item(trans, root, path);
2208out:
2209 btrfs_free_path(path);
2210 err = btrfs_commit_transaction(trans, root);
2211 if (err && !ret)
2212 ret = err;
2213 return ret;
2214}
2215
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002216/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002217 * This is a heuristic used to reduce the number of chunks balanced on
2218 * resume after balance was interrupted.
2219 */
2220static void update_balance_args(struct btrfs_balance_control *bctl)
2221{
2222 /*
2223 * Turn on soft mode for chunk types that were being converted.
2224 */
2225 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2226 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2227 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2228 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2229 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2230 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2231
2232 /*
2233 * Turn on usage filter if is not already used. The idea is
2234 * that chunks that we have already balanced should be
2235 * reasonably full. Don't do it for chunks that are being
2236 * converted - that will keep us from relocating unconverted
2237 * (albeit full) chunks.
2238 */
2239 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2240 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2241 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2242 bctl->data.usage = 90;
2243 }
2244 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2245 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2246 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2247 bctl->sys.usage = 90;
2248 }
2249 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2250 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2251 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2252 bctl->meta.usage = 90;
2253 }
2254}
2255
2256/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002257 * Should be called with both balance and volume mutexes held to
2258 * serialize other volume operations (add_dev/rm_dev/resize) with
2259 * restriper. Same goes for unset_balance_control.
2260 */
2261static void set_balance_control(struct btrfs_balance_control *bctl)
2262{
2263 struct btrfs_fs_info *fs_info = bctl->fs_info;
2264
2265 BUG_ON(fs_info->balance_ctl);
2266
2267 spin_lock(&fs_info->balance_lock);
2268 fs_info->balance_ctl = bctl;
2269 spin_unlock(&fs_info->balance_lock);
2270}
2271
2272static void unset_balance_control(struct btrfs_fs_info *fs_info)
2273{
2274 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2275
2276 BUG_ON(!fs_info->balance_ctl);
2277
2278 spin_lock(&fs_info->balance_lock);
2279 fs_info->balance_ctl = NULL;
2280 spin_unlock(&fs_info->balance_lock);
2281
2282 kfree(bctl);
2283}
2284
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002285/*
2286 * Balance filters. Return 1 if chunk should be filtered out
2287 * (should not be balanced).
2288 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002289static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002290 struct btrfs_balance_args *bargs)
2291{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002292 chunk_type = chunk_to_extended(chunk_type) &
2293 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002294
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002295 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002296 return 0;
2297
2298 return 1;
2299}
2300
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002301static u64 div_factor_fine(u64 num, int factor)
2302{
2303 if (factor <= 0)
2304 return 0;
2305 if (factor >= 100)
2306 return num;
2307
2308 num *= factor;
2309 do_div(num, 100);
2310 return num;
2311}
2312
2313static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2314 struct btrfs_balance_args *bargs)
2315{
2316 struct btrfs_block_group_cache *cache;
2317 u64 chunk_used, user_thresh;
2318 int ret = 1;
2319
2320 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2321 chunk_used = btrfs_block_group_used(&cache->item);
2322
2323 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2324 if (chunk_used < user_thresh)
2325 ret = 0;
2326
2327 btrfs_put_block_group(cache);
2328 return ret;
2329}
2330
Ilya Dryomov409d4042012-01-16 22:04:47 +02002331static int chunk_devid_filter(struct extent_buffer *leaf,
2332 struct btrfs_chunk *chunk,
2333 struct btrfs_balance_args *bargs)
2334{
2335 struct btrfs_stripe *stripe;
2336 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2337 int i;
2338
2339 for (i = 0; i < num_stripes; i++) {
2340 stripe = btrfs_stripe_nr(chunk, i);
2341 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2342 return 0;
2343 }
2344
2345 return 1;
2346}
2347
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002348/* [pstart, pend) */
2349static int chunk_drange_filter(struct extent_buffer *leaf,
2350 struct btrfs_chunk *chunk,
2351 u64 chunk_offset,
2352 struct btrfs_balance_args *bargs)
2353{
2354 struct btrfs_stripe *stripe;
2355 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2356 u64 stripe_offset;
2357 u64 stripe_length;
2358 int factor;
2359 int i;
2360
2361 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2362 return 0;
2363
2364 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2365 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2366 factor = 2;
2367 else
2368 factor = 1;
2369 factor = num_stripes / factor;
2370
2371 for (i = 0; i < num_stripes; i++) {
2372 stripe = btrfs_stripe_nr(chunk, i);
2373 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2374 continue;
2375
2376 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2377 stripe_length = btrfs_chunk_length(leaf, chunk);
2378 do_div(stripe_length, factor);
2379
2380 if (stripe_offset < bargs->pend &&
2381 stripe_offset + stripe_length > bargs->pstart)
2382 return 0;
2383 }
2384
2385 return 1;
2386}
2387
Ilya Dryomovea671762012-01-16 22:04:48 +02002388/* [vstart, vend) */
2389static int chunk_vrange_filter(struct extent_buffer *leaf,
2390 struct btrfs_chunk *chunk,
2391 u64 chunk_offset,
2392 struct btrfs_balance_args *bargs)
2393{
2394 if (chunk_offset < bargs->vend &&
2395 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2396 /* at least part of the chunk is inside this vrange */
2397 return 0;
2398
2399 return 1;
2400}
2401
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002402static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002403 struct btrfs_balance_args *bargs)
2404{
2405 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2406 return 0;
2407
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002408 chunk_type = chunk_to_extended(chunk_type) &
2409 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002410
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002411 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002412 return 1;
2413
2414 return 0;
2415}
2416
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002417static int should_balance_chunk(struct btrfs_root *root,
2418 struct extent_buffer *leaf,
2419 struct btrfs_chunk *chunk, u64 chunk_offset)
2420{
2421 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2422 struct btrfs_balance_args *bargs = NULL;
2423 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2424
2425 /* type filter */
2426 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2427 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2428 return 0;
2429 }
2430
2431 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2432 bargs = &bctl->data;
2433 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2434 bargs = &bctl->sys;
2435 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2436 bargs = &bctl->meta;
2437
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002438 /* profiles filter */
2439 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2440 chunk_profiles_filter(chunk_type, bargs)) {
2441 return 0;
2442 }
2443
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002444 /* usage filter */
2445 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2446 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2447 return 0;
2448 }
2449
Ilya Dryomov409d4042012-01-16 22:04:47 +02002450 /* devid filter */
2451 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2452 chunk_devid_filter(leaf, chunk, bargs)) {
2453 return 0;
2454 }
2455
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002456 /* drange filter, makes sense only with devid filter */
2457 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2458 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2459 return 0;
2460 }
2461
Ilya Dryomovea671762012-01-16 22:04:48 +02002462 /* vrange filter */
2463 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2464 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2465 return 0;
2466 }
2467
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002468 /* soft profile changing mode */
2469 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2470 chunk_soft_convert_filter(chunk_type, bargs)) {
2471 return 0;
2472 }
2473
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002474 return 1;
2475}
2476
Chris Masonec44a352008-04-28 15:29:52 -04002477static u64 div_factor(u64 num, int factor)
2478{
2479 if (factor == 10)
2480 return num;
2481 num *= factor;
2482 do_div(num, 10);
2483 return num;
2484}
2485
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002486static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002487{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002488 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002489 struct btrfs_root *chunk_root = fs_info->chunk_root;
2490 struct btrfs_root *dev_root = fs_info->dev_root;
2491 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002492 struct btrfs_device *device;
2493 u64 old_size;
2494 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002495 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002496 struct btrfs_path *path;
2497 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002498 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002499 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002500 struct extent_buffer *leaf;
2501 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002502 int ret;
2503 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002504 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002505
Chris Masonec44a352008-04-28 15:29:52 -04002506 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002507 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002508 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002509 old_size = device->total_bytes;
2510 size_to_free = div_factor(old_size, 1);
2511 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002512 if (!device->writeable ||
2513 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002514 continue;
2515
2516 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002517 if (ret == -ENOSPC)
2518 break;
Chris Masonec44a352008-04-28 15:29:52 -04002519 BUG_ON(ret);
2520
Yan, Zhenga22285a2010-05-16 10:48:46 -04002521 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002522 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002523
2524 ret = btrfs_grow_device(trans, device, old_size);
2525 BUG_ON(ret);
2526
2527 btrfs_end_transaction(trans, dev_root);
2528 }
2529
2530 /* step two, relocate all the chunks */
2531 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002532 if (!path) {
2533 ret = -ENOMEM;
2534 goto error;
2535 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002536
2537 /* zero out stat counters */
2538 spin_lock(&fs_info->balance_lock);
2539 memset(&bctl->stat, 0, sizeof(bctl->stat));
2540 spin_unlock(&fs_info->balance_lock);
2541again:
Chris Masonec44a352008-04-28 15:29:52 -04002542 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2543 key.offset = (u64)-1;
2544 key.type = BTRFS_CHUNK_ITEM_KEY;
2545
Chris Masond3977122009-01-05 21:25:51 -05002546 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002547 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002548 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002549 ret = -ECANCELED;
2550 goto error;
2551 }
2552
Chris Masonec44a352008-04-28 15:29:52 -04002553 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2554 if (ret < 0)
2555 goto error;
2556
2557 /*
2558 * this shouldn't happen, it means the last relocate
2559 * failed
2560 */
2561 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002562 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002563
2564 ret = btrfs_previous_item(chunk_root, path, 0,
2565 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002566 if (ret) {
2567 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002568 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002569 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002570
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002571 leaf = path->nodes[0];
2572 slot = path->slots[0];
2573 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2574
Chris Masonec44a352008-04-28 15:29:52 -04002575 if (found_key.objectid != key.objectid)
2576 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002577
Chris Masonec44a352008-04-28 15:29:52 -04002578 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002579 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002580 break;
2581
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002582 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2583
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002584 if (!counting) {
2585 spin_lock(&fs_info->balance_lock);
2586 bctl->stat.considered++;
2587 spin_unlock(&fs_info->balance_lock);
2588 }
2589
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002590 ret = should_balance_chunk(chunk_root, leaf, chunk,
2591 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002592 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002593 if (!ret)
2594 goto loop;
2595
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002596 if (counting) {
2597 spin_lock(&fs_info->balance_lock);
2598 bctl->stat.expected++;
2599 spin_unlock(&fs_info->balance_lock);
2600 goto loop;
2601 }
2602
Chris Masonec44a352008-04-28 15:29:52 -04002603 ret = btrfs_relocate_chunk(chunk_root,
2604 chunk_root->root_key.objectid,
2605 found_key.objectid,
2606 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002607 if (ret && ret != -ENOSPC)
2608 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002609 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002610 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002611 } else {
2612 spin_lock(&fs_info->balance_lock);
2613 bctl->stat.completed++;
2614 spin_unlock(&fs_info->balance_lock);
2615 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002616loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002617 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002618 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002619
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002620 if (counting) {
2621 btrfs_release_path(path);
2622 counting = false;
2623 goto again;
2624 }
Chris Masonec44a352008-04-28 15:29:52 -04002625error:
2626 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002627 if (enospc_errors) {
2628 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2629 enospc_errors);
2630 if (!ret)
2631 ret = -ENOSPC;
2632 }
2633
Chris Masonec44a352008-04-28 15:29:52 -04002634 return ret;
2635}
2636
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002637/**
2638 * alloc_profile_is_valid - see if a given profile is valid and reduced
2639 * @flags: profile to validate
2640 * @extended: if true @flags is treated as an extended profile
2641 */
2642static int alloc_profile_is_valid(u64 flags, int extended)
2643{
2644 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2645 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2646
2647 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2648
2649 /* 1) check that all other bits are zeroed */
2650 if (flags & ~mask)
2651 return 0;
2652
2653 /* 2) see if profile is reduced */
2654 if (flags == 0)
2655 return !extended; /* "0" is valid for usual profiles */
2656
2657 /* true if exactly one bit set */
2658 return (flags & (flags - 1)) == 0;
2659}
2660
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002661static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2662{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002663 /* cancel requested || normal exit path */
2664 return atomic_read(&fs_info->balance_cancel_req) ||
2665 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2666 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002667}
2668
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002669static void __cancel_balance(struct btrfs_fs_info *fs_info)
2670{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002671 int ret;
2672
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002673 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002674 ret = del_balance_item(fs_info->tree_root);
2675 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002676}
2677
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002678void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002679 struct btrfs_ioctl_balance_args *bargs);
2680
2681/*
2682 * Should be called with both balance and volume mutexes held
2683 */
2684int btrfs_balance(struct btrfs_balance_control *bctl,
2685 struct btrfs_ioctl_balance_args *bargs)
2686{
2687 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002688 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002689 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002690 int ret;
2691
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002692 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002693 atomic_read(&fs_info->balance_pause_req) ||
2694 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002695 ret = -EINVAL;
2696 goto out;
2697 }
2698
Ilya Dryomove4837f82012-03-27 17:09:17 +03002699 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2700 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2701 mixed = 1;
2702
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002703 /*
2704 * In case of mixed groups both data and meta should be picked,
2705 * and identical options should be given for both of them.
2706 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002707 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2708 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002709 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2710 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2711 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2712 printk(KERN_ERR "btrfs: with mixed groups data and "
2713 "metadata balance options must be the same\n");
2714 ret = -EINVAL;
2715 goto out;
2716 }
2717 }
2718
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002719 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2720 if (fs_info->fs_devices->num_devices == 1)
2721 allowed |= BTRFS_BLOCK_GROUP_DUP;
2722 else if (fs_info->fs_devices->num_devices < 4)
2723 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2724 else
2725 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2726 BTRFS_BLOCK_GROUP_RAID10);
2727
Ilya Dryomov6728b192012-03-27 17:09:17 +03002728 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2729 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2730 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002731 printk(KERN_ERR "btrfs: unable to start balance with target "
2732 "data profile %llu\n",
2733 (unsigned long long)bctl->data.target);
2734 ret = -EINVAL;
2735 goto out;
2736 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002737 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2738 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2739 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002740 printk(KERN_ERR "btrfs: unable to start balance with target "
2741 "metadata profile %llu\n",
2742 (unsigned long long)bctl->meta.target);
2743 ret = -EINVAL;
2744 goto out;
2745 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002746 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2747 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2748 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002749 printk(KERN_ERR "btrfs: unable to start balance with target "
2750 "system profile %llu\n",
2751 (unsigned long long)bctl->sys.target);
2752 ret = -EINVAL;
2753 goto out;
2754 }
2755
Ilya Dryomove4837f82012-03-27 17:09:17 +03002756 /* allow dup'ed data chunks only in mixed mode */
2757 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002758 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002759 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2760 ret = -EINVAL;
2761 goto out;
2762 }
2763
2764 /* allow to reduce meta or sys integrity only if force set */
2765 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2766 BTRFS_BLOCK_GROUP_RAID10;
2767 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2768 (fs_info->avail_system_alloc_bits & allowed) &&
2769 !(bctl->sys.target & allowed)) ||
2770 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2771 (fs_info->avail_metadata_alloc_bits & allowed) &&
2772 !(bctl->meta.target & allowed))) {
2773 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2774 printk(KERN_INFO "btrfs: force reducing metadata "
2775 "integrity\n");
2776 } else {
2777 printk(KERN_ERR "btrfs: balance will reduce metadata "
2778 "integrity, use force if you want this\n");
2779 ret = -EINVAL;
2780 goto out;
2781 }
2782 }
2783
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002784 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002785 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002786 goto out;
2787
Ilya Dryomov59641012012-01-16 22:04:48 +02002788 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2789 BUG_ON(ret == -EEXIST);
2790 set_balance_control(bctl);
2791 } else {
2792 BUG_ON(ret != -EEXIST);
2793 spin_lock(&fs_info->balance_lock);
2794 update_balance_args(bctl);
2795 spin_unlock(&fs_info->balance_lock);
2796 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002797
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002798 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002799 mutex_unlock(&fs_info->balance_mutex);
2800
2801 ret = __btrfs_balance(fs_info);
2802
2803 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002804 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002805
2806 if (bargs) {
2807 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002808 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002809 }
2810
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002811 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2812 balance_need_close(fs_info)) {
2813 __cancel_balance(fs_info);
2814 }
2815
2816 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002817
2818 return ret;
2819out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002820 if (bctl->flags & BTRFS_BALANCE_RESUME)
2821 __cancel_balance(fs_info);
2822 else
2823 kfree(bctl);
2824 return ret;
2825}
2826
2827static int balance_kthread(void *data)
2828{
2829 struct btrfs_balance_control *bctl =
2830 (struct btrfs_balance_control *)data;
2831 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002832 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002833
2834 mutex_lock(&fs_info->volume_mutex);
2835 mutex_lock(&fs_info->balance_mutex);
2836
2837 set_balance_control(bctl);
2838
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002839 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2840 printk(KERN_INFO "btrfs: force skipping balance\n");
2841 } else {
2842 printk(KERN_INFO "btrfs: continuing balance\n");
2843 ret = btrfs_balance(bctl, NULL);
2844 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002845
2846 mutex_unlock(&fs_info->balance_mutex);
2847 mutex_unlock(&fs_info->volume_mutex);
2848 return ret;
2849}
2850
2851int btrfs_recover_balance(struct btrfs_root *tree_root)
2852{
2853 struct task_struct *tsk;
2854 struct btrfs_balance_control *bctl;
2855 struct btrfs_balance_item *item;
2856 struct btrfs_disk_balance_args disk_bargs;
2857 struct btrfs_path *path;
2858 struct extent_buffer *leaf;
2859 struct btrfs_key key;
2860 int ret;
2861
2862 path = btrfs_alloc_path();
2863 if (!path)
2864 return -ENOMEM;
2865
2866 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2867 if (!bctl) {
2868 ret = -ENOMEM;
2869 goto out;
2870 }
2871
2872 key.objectid = BTRFS_BALANCE_OBJECTID;
2873 key.type = BTRFS_BALANCE_ITEM_KEY;
2874 key.offset = 0;
2875
2876 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2877 if (ret < 0)
2878 goto out_bctl;
2879 if (ret > 0) { /* ret = -ENOENT; */
2880 ret = 0;
2881 goto out_bctl;
2882 }
2883
2884 leaf = path->nodes[0];
2885 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2886
2887 bctl->fs_info = tree_root->fs_info;
2888 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2889
2890 btrfs_balance_data(leaf, item, &disk_bargs);
2891 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2892 btrfs_balance_meta(leaf, item, &disk_bargs);
2893 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2894 btrfs_balance_sys(leaf, item, &disk_bargs);
2895 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2896
2897 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2898 if (IS_ERR(tsk))
2899 ret = PTR_ERR(tsk);
2900 else
2901 goto out;
2902
2903out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002904 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002905out:
2906 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002907 return ret;
2908}
2909
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002910int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2911{
2912 int ret = 0;
2913
2914 mutex_lock(&fs_info->balance_mutex);
2915 if (!fs_info->balance_ctl) {
2916 mutex_unlock(&fs_info->balance_mutex);
2917 return -ENOTCONN;
2918 }
2919
2920 if (atomic_read(&fs_info->balance_running)) {
2921 atomic_inc(&fs_info->balance_pause_req);
2922 mutex_unlock(&fs_info->balance_mutex);
2923
2924 wait_event(fs_info->balance_wait_q,
2925 atomic_read(&fs_info->balance_running) == 0);
2926
2927 mutex_lock(&fs_info->balance_mutex);
2928 /* we are good with balance_ctl ripped off from under us */
2929 BUG_ON(atomic_read(&fs_info->balance_running));
2930 atomic_dec(&fs_info->balance_pause_req);
2931 } else {
2932 ret = -ENOTCONN;
2933 }
2934
2935 mutex_unlock(&fs_info->balance_mutex);
2936 return ret;
2937}
2938
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002939int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2940{
2941 mutex_lock(&fs_info->balance_mutex);
2942 if (!fs_info->balance_ctl) {
2943 mutex_unlock(&fs_info->balance_mutex);
2944 return -ENOTCONN;
2945 }
2946
2947 atomic_inc(&fs_info->balance_cancel_req);
2948 /*
2949 * if we are running just wait and return, balance item is
2950 * deleted in btrfs_balance in this case
2951 */
2952 if (atomic_read(&fs_info->balance_running)) {
2953 mutex_unlock(&fs_info->balance_mutex);
2954 wait_event(fs_info->balance_wait_q,
2955 atomic_read(&fs_info->balance_running) == 0);
2956 mutex_lock(&fs_info->balance_mutex);
2957 } else {
2958 /* __cancel_balance needs volume_mutex */
2959 mutex_unlock(&fs_info->balance_mutex);
2960 mutex_lock(&fs_info->volume_mutex);
2961 mutex_lock(&fs_info->balance_mutex);
2962
2963 if (fs_info->balance_ctl)
2964 __cancel_balance(fs_info);
2965
2966 mutex_unlock(&fs_info->volume_mutex);
2967 }
2968
2969 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2970 atomic_dec(&fs_info->balance_cancel_req);
2971 mutex_unlock(&fs_info->balance_mutex);
2972 return 0;
2973}
2974
Chris Mason8f18cf12008-04-25 16:53:30 -04002975/*
2976 * shrinking a device means finding all of the device extents past
2977 * the new size, and then following the back refs to the chunks.
2978 * The chunk relocation code actually frees the device extent
2979 */
2980int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2981{
2982 struct btrfs_trans_handle *trans;
2983 struct btrfs_root *root = device->dev_root;
2984 struct btrfs_dev_extent *dev_extent = NULL;
2985 struct btrfs_path *path;
2986 u64 length;
2987 u64 chunk_tree;
2988 u64 chunk_objectid;
2989 u64 chunk_offset;
2990 int ret;
2991 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002992 int failed = 0;
2993 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002994 struct extent_buffer *l;
2995 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002996 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002997 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002998 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002999 u64 diff = device->total_bytes - new_size;
3000
Yan Zheng2b820322008-11-17 21:11:30 -05003001 if (new_size >= device->total_bytes)
3002 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04003003
3004 path = btrfs_alloc_path();
3005 if (!path)
3006 return -ENOMEM;
3007
Chris Mason8f18cf12008-04-25 16:53:30 -04003008 path->reada = 2;
3009
Chris Mason7d9eb122008-07-08 14:19:17 -04003010 lock_chunks(root);
3011
Chris Mason8f18cf12008-04-25 16:53:30 -04003012 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003013 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003014 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003015 spin_lock(&root->fs_info->free_chunk_lock);
3016 root->fs_info->free_chunk_space -= diff;
3017 spin_unlock(&root->fs_info->free_chunk_lock);
3018 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003019 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003020
Josef Bacikba1bf482009-09-11 16:11:19 -04003021again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003022 key.objectid = device->devid;
3023 key.offset = (u64)-1;
3024 key.type = BTRFS_DEV_EXTENT_KEY;
3025
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003026 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003027 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3028 if (ret < 0)
3029 goto done;
3030
3031 ret = btrfs_previous_item(root, path, 0, key.type);
3032 if (ret < 0)
3033 goto done;
3034 if (ret) {
3035 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003036 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003037 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003038 }
3039
3040 l = path->nodes[0];
3041 slot = path->slots[0];
3042 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3043
Josef Bacikba1bf482009-09-11 16:11:19 -04003044 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003045 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003046 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003047 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003048
3049 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3050 length = btrfs_dev_extent_length(l, dev_extent);
3051
Josef Bacikba1bf482009-09-11 16:11:19 -04003052 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003053 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003054 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003055 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003056
3057 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3058 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3059 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003060 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003061
3062 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3063 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003064 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003065 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003066 if (ret == -ENOSPC)
3067 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003068 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003069
3070 if (failed && !retried) {
3071 failed = 0;
3072 retried = true;
3073 goto again;
3074 } else if (failed && retried) {
3075 ret = -ENOSPC;
3076 lock_chunks(root);
3077
3078 device->total_bytes = old_size;
3079 if (device->writeable)
3080 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003081 spin_lock(&root->fs_info->free_chunk_lock);
3082 root->fs_info->free_chunk_space += diff;
3083 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003084 unlock_chunks(root);
3085 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003086 }
3087
Chris Balld6397ba2009-04-27 07:29:03 -04003088 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003089 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003090 if (IS_ERR(trans)) {
3091 ret = PTR_ERR(trans);
3092 goto done;
3093 }
3094
Chris Balld6397ba2009-04-27 07:29:03 -04003095 lock_chunks(root);
3096
3097 device->disk_total_bytes = new_size;
3098 /* Now btrfs_update_device() will change the on-disk size. */
3099 ret = btrfs_update_device(trans, device);
3100 if (ret) {
3101 unlock_chunks(root);
3102 btrfs_end_transaction(trans, root);
3103 goto done;
3104 }
3105 WARN_ON(diff > old_total);
3106 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3107 unlock_chunks(root);
3108 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003109done:
3110 btrfs_free_path(path);
3111 return ret;
3112}
3113
Li Zefan125ccb02011-12-08 15:07:24 +08003114static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003115 struct btrfs_key *key,
3116 struct btrfs_chunk *chunk, int item_size)
3117{
David Sterba6c417612011-04-13 15:41:04 +02003118 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003119 struct btrfs_disk_key disk_key;
3120 u32 array_size;
3121 u8 *ptr;
3122
3123 array_size = btrfs_super_sys_array_size(super_copy);
3124 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3125 return -EFBIG;
3126
3127 ptr = super_copy->sys_chunk_array + array_size;
3128 btrfs_cpu_key_to_disk(&disk_key, key);
3129 memcpy(ptr, &disk_key, sizeof(disk_key));
3130 ptr += sizeof(disk_key);
3131 memcpy(ptr, chunk, item_size);
3132 item_size += sizeof(disk_key);
3133 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3134 return 0;
3135}
3136
Miao Xieb2117a32011-01-05 10:07:28 +00003137/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003138 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003139 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003140static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003141{
Arne Jansen73c5de02011-04-12 12:07:57 +02003142 const struct btrfs_device_info *di_a = a;
3143 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003144
Arne Jansen73c5de02011-04-12 12:07:57 +02003145 if (di_a->max_avail > di_b->max_avail)
3146 return -1;
3147 if (di_a->max_avail < di_b->max_avail)
3148 return 1;
3149 if (di_a->total_avail > di_b->total_avail)
3150 return -1;
3151 if (di_a->total_avail < di_b->total_avail)
3152 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003153 return 0;
3154}
3155
3156static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3157 struct btrfs_root *extent_root,
3158 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003159 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003160 u64 start, u64 type)
3161{
3162 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003163 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3164 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003165 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003166 struct extent_map_tree *em_tree;
3167 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003168 struct btrfs_device_info *devices_info = NULL;
3169 u64 total_avail;
3170 int num_stripes; /* total number of stripes to allocate */
3171 int sub_stripes; /* sub_stripes info for map */
3172 int dev_stripes; /* stripes per dev */
3173 int devs_max; /* max devs to use */
3174 int devs_min; /* min devs needed */
3175 int devs_increment; /* ndevs has to be a multiple of this */
3176 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003177 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003178 u64 max_stripe_size;
3179 u64 max_chunk_size;
3180 u64 stripe_size;
3181 u64 num_bytes;
3182 int ndevs;
3183 int i;
3184 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003185
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003186 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003187
Miao Xieb2117a32011-01-05 10:07:28 +00003188 if (list_empty(&fs_devices->alloc_list))
3189 return -ENOSPC;
3190
Arne Jansen73c5de02011-04-12 12:07:57 +02003191 sub_stripes = 1;
3192 dev_stripes = 1;
3193 devs_increment = 1;
3194 ncopies = 1;
3195 devs_max = 0; /* 0 == as many as possible */
3196 devs_min = 1;
3197
3198 /*
3199 * define the properties of each RAID type.
3200 * FIXME: move this to a global table and use it in all RAID
3201 * calculation code
3202 */
3203 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3204 dev_stripes = 2;
3205 ncopies = 2;
3206 devs_max = 1;
3207 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3208 devs_min = 2;
3209 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3210 devs_increment = 2;
3211 ncopies = 2;
3212 devs_max = 2;
3213 devs_min = 2;
3214 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3215 sub_stripes = 2;
3216 devs_increment = 2;
3217 ncopies = 2;
3218 devs_min = 4;
3219 } else {
3220 devs_max = 1;
3221 }
3222
3223 if (type & BTRFS_BLOCK_GROUP_DATA) {
3224 max_stripe_size = 1024 * 1024 * 1024;
3225 max_chunk_size = 10 * max_stripe_size;
3226 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003227 /* for larger filesystems, use larger metadata chunks */
3228 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3229 max_stripe_size = 1024 * 1024 * 1024;
3230 else
3231 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003232 max_chunk_size = max_stripe_size;
3233 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003234 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003235 max_chunk_size = 2 * max_stripe_size;
3236 } else {
3237 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3238 type);
3239 BUG_ON(1);
3240 }
3241
3242 /* we don't want a chunk larger than 10% of writeable space */
3243 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3244 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003245
3246 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3247 GFP_NOFS);
3248 if (!devices_info)
3249 return -ENOMEM;
3250
Arne Jansen73c5de02011-04-12 12:07:57 +02003251 cur = fs_devices->alloc_list.next;
3252
3253 /*
3254 * in the first pass through the devices list, we gather information
3255 * about the available holes on each device.
3256 */
3257 ndevs = 0;
3258 while (cur != &fs_devices->alloc_list) {
3259 struct btrfs_device *device;
3260 u64 max_avail;
3261 u64 dev_offset;
3262
3263 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3264
3265 cur = cur->next;
3266
3267 if (!device->writeable) {
3268 printk(KERN_ERR
3269 "btrfs: read-only device in alloc_list\n");
3270 WARN_ON(1);
3271 continue;
3272 }
3273
3274 if (!device->in_fs_metadata)
3275 continue;
3276
3277 if (device->total_bytes > device->bytes_used)
3278 total_avail = device->total_bytes - device->bytes_used;
3279 else
3280 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003281
3282 /* If there is no space on this device, skip it. */
3283 if (total_avail == 0)
3284 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003285
Li Zefan125ccb02011-12-08 15:07:24 +08003286 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003287 max_stripe_size * dev_stripes,
3288 &dev_offset, &max_avail);
3289 if (ret && ret != -ENOSPC)
3290 goto error;
3291
3292 if (ret == 0)
3293 max_avail = max_stripe_size * dev_stripes;
3294
3295 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3296 continue;
3297
3298 devices_info[ndevs].dev_offset = dev_offset;
3299 devices_info[ndevs].max_avail = max_avail;
3300 devices_info[ndevs].total_avail = total_avail;
3301 devices_info[ndevs].dev = device;
3302 ++ndevs;
3303 }
3304
3305 /*
3306 * now sort the devices by hole size / available space
3307 */
3308 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3309 btrfs_cmp_device_info, NULL);
3310
3311 /* round down to number of usable stripes */
3312 ndevs -= ndevs % devs_increment;
3313
3314 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3315 ret = -ENOSPC;
3316 goto error;
3317 }
3318
3319 if (devs_max && ndevs > devs_max)
3320 ndevs = devs_max;
3321 /*
3322 * the primary goal is to maximize the number of stripes, so use as many
3323 * devices as possible, even if the stripes are not maximum sized.
3324 */
3325 stripe_size = devices_info[ndevs-1].max_avail;
3326 num_stripes = ndevs * dev_stripes;
3327
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003328 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003329 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003330 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003331 }
3332
3333 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003334
3335 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003336 do_div(stripe_size, BTRFS_STRIPE_LEN);
3337 stripe_size *= BTRFS_STRIPE_LEN;
3338
Miao Xieb2117a32011-01-05 10:07:28 +00003339 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3340 if (!map) {
3341 ret = -ENOMEM;
3342 goto error;
3343 }
3344 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003345
Arne Jansen73c5de02011-04-12 12:07:57 +02003346 for (i = 0; i < ndevs; ++i) {
3347 for (j = 0; j < dev_stripes; ++j) {
3348 int s = i * dev_stripes + j;
3349 map->stripes[s].dev = devices_info[i].dev;
3350 map->stripes[s].physical = devices_info[i].dev_offset +
3351 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003352 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003353 }
Chris Mason593060d2008-03-25 16:50:33 -04003354 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003355 map->stripe_len = BTRFS_STRIPE_LEN;
3356 map->io_align = BTRFS_STRIPE_LEN;
3357 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003358 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003359 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003360
Yan Zheng2b820322008-11-17 21:11:30 -05003361 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003362 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003363
Arne Jansen73c5de02011-04-12 12:07:57 +02003364 *stripe_size_out = stripe_size;
3365 *num_bytes_out = num_bytes;
3366
3367 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003368
David Sterba172ddd62011-04-21 00:48:27 +02003369 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003370 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003371 ret = -ENOMEM;
3372 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003373 }
Chris Mason0b86a832008-03-24 15:01:56 -04003374 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003375 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003376 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003377 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003378 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003379
Chris Mason0b86a832008-03-24 15:01:56 -04003380 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003381 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003382 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003383 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003384 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003385 if (ret)
3386 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003387
3388 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3389 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003390 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003391 if (ret)
3392 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003393
Arne Jansen73c5de02011-04-12 12:07:57 +02003394 for (i = 0; i < map->num_stripes; ++i) {
3395 struct btrfs_device *device;
3396 u64 dev_offset;
3397
3398 device = map->stripes[i].dev;
3399 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003400
3401 ret = btrfs_alloc_dev_extent(trans, device,
3402 info->chunk_root->root_key.objectid,
3403 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003404 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003405 if (ret) {
3406 btrfs_abort_transaction(trans, extent_root, ret);
3407 goto error;
3408 }
Yan Zheng2b820322008-11-17 21:11:30 -05003409 }
3410
Miao Xieb2117a32011-01-05 10:07:28 +00003411 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003412 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003413
3414error:
3415 kfree(map);
3416 kfree(devices_info);
3417 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003418}
3419
3420static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3421 struct btrfs_root *extent_root,
3422 struct map_lookup *map, u64 chunk_offset,
3423 u64 chunk_size, u64 stripe_size)
3424{
3425 u64 dev_offset;
3426 struct btrfs_key key;
3427 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3428 struct btrfs_device *device;
3429 struct btrfs_chunk *chunk;
3430 struct btrfs_stripe *stripe;
3431 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3432 int index = 0;
3433 int ret;
3434
3435 chunk = kzalloc(item_size, GFP_NOFS);
3436 if (!chunk)
3437 return -ENOMEM;
3438
3439 index = 0;
3440 while (index < map->num_stripes) {
3441 device = map->stripes[index].dev;
3442 device->bytes_used += stripe_size;
3443 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003444 if (ret)
3445 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003446 index++;
3447 }
3448
Josef Bacik2bf64752011-09-26 17:12:22 -04003449 spin_lock(&extent_root->fs_info->free_chunk_lock);
3450 extent_root->fs_info->free_chunk_space -= (stripe_size *
3451 map->num_stripes);
3452 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3453
Yan Zheng2b820322008-11-17 21:11:30 -05003454 index = 0;
3455 stripe = &chunk->stripe;
3456 while (index < map->num_stripes) {
3457 device = map->stripes[index].dev;
3458 dev_offset = map->stripes[index].physical;
3459
3460 btrfs_set_stack_stripe_devid(stripe, device->devid);
3461 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3462 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3463 stripe++;
3464 index++;
3465 }
3466
3467 btrfs_set_stack_chunk_length(chunk, chunk_size);
3468 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3469 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3470 btrfs_set_stack_chunk_type(chunk, map->type);
3471 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3472 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3473 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3474 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3475 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3476
3477 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3478 key.type = BTRFS_CHUNK_ITEM_KEY;
3479 key.offset = chunk_offset;
3480
3481 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003482
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003483 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3484 /*
3485 * TODO: Cleanup of inserted chunk root in case of
3486 * failure.
3487 */
Li Zefan125ccb02011-12-08 15:07:24 +08003488 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003489 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003490 }
liubo1abe9b82011-03-24 11:18:59 +00003491
Mark Fasheh3acd3952011-09-08 17:40:01 -07003492out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003493 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003494 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003495}
3496
3497/*
3498 * Chunk allocation falls into two parts. The first part does works
3499 * that make the new allocated chunk useable, but not do any operation
3500 * that modifies the chunk tree. The second part does the works that
3501 * require modifying the chunk tree. This division is important for the
3502 * bootstrap process of adding storage to a seed btrfs.
3503 */
3504int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3505 struct btrfs_root *extent_root, u64 type)
3506{
3507 u64 chunk_offset;
3508 u64 chunk_size;
3509 u64 stripe_size;
3510 struct map_lookup *map;
3511 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3512 int ret;
3513
3514 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3515 &chunk_offset);
3516 if (ret)
3517 return ret;
3518
3519 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3520 &stripe_size, chunk_offset, type);
3521 if (ret)
3522 return ret;
3523
3524 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3525 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003526 if (ret)
3527 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003528 return 0;
3529}
3530
Chris Masond3977122009-01-05 21:25:51 -05003531static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003532 struct btrfs_root *root,
3533 struct btrfs_device *device)
3534{
3535 u64 chunk_offset;
3536 u64 sys_chunk_offset;
3537 u64 chunk_size;
3538 u64 sys_chunk_size;
3539 u64 stripe_size;
3540 u64 sys_stripe_size;
3541 u64 alloc_profile;
3542 struct map_lookup *map;
3543 struct map_lookup *sys_map;
3544 struct btrfs_fs_info *fs_info = root->fs_info;
3545 struct btrfs_root *extent_root = fs_info->extent_root;
3546 int ret;
3547
3548 ret = find_next_chunk(fs_info->chunk_root,
3549 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003550 if (ret)
3551 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003552
3553 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003554 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003555 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3556
3557 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3558 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003559 if (ret)
3560 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003561
3562 sys_chunk_offset = chunk_offset + chunk_size;
3563
3564 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003565 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003566 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3567
3568 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3569 &sys_chunk_size, &sys_stripe_size,
3570 sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003571 if (ret)
3572 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003573
3574 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003575 if (ret)
3576 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003577
3578 /*
3579 * Modifying chunk tree needs allocating new blocks from both
3580 * system block group and metadata block group. So we only can
3581 * do operations require modifying the chunk tree after both
3582 * block groups were created.
3583 */
3584 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3585 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003586 if (ret)
3587 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003588
3589 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3590 sys_chunk_offset, sys_chunk_size,
3591 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003592 if (ret)
3593 goto abort;
3594
Yan Zheng2b820322008-11-17 21:11:30 -05003595 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003596
3597abort:
3598 btrfs_abort_transaction(trans, root, ret);
3599 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003600}
3601
3602int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3603{
3604 struct extent_map *em;
3605 struct map_lookup *map;
3606 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3607 int readonly = 0;
3608 int i;
3609
Chris Mason890871b2009-09-02 16:24:52 -04003610 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003611 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003612 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003613 if (!em)
3614 return 1;
3615
Josef Bacikf48b9072010-01-27 02:07:59 +00003616 if (btrfs_test_opt(root, DEGRADED)) {
3617 free_extent_map(em);
3618 return 0;
3619 }
3620
Yan Zheng2b820322008-11-17 21:11:30 -05003621 map = (struct map_lookup *)em->bdev;
3622 for (i = 0; i < map->num_stripes; i++) {
3623 if (!map->stripes[i].dev->writeable) {
3624 readonly = 1;
3625 break;
3626 }
3627 }
3628 free_extent_map(em);
3629 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003630}
3631
3632void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3633{
David Sterbaa8067e02011-04-21 00:34:43 +02003634 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003635}
3636
3637void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3638{
3639 struct extent_map *em;
3640
Chris Masond3977122009-01-05 21:25:51 -05003641 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003642 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003643 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3644 if (em)
3645 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003646 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003647 if (!em)
3648 break;
3649 kfree(em->bdev);
3650 /* once for us */
3651 free_extent_map(em);
3652 /* once for the tree */
3653 free_extent_map(em);
3654 }
3655}
3656
Chris Masonf1885912008-04-09 16:28:12 -04003657int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3658{
3659 struct extent_map *em;
3660 struct map_lookup *map;
3661 struct extent_map_tree *em_tree = &map_tree->map_tree;
3662 int ret;
3663
Chris Mason890871b2009-09-02 16:24:52 -04003664 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003665 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003666 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003667 BUG_ON(!em);
3668
3669 BUG_ON(em->start > logical || em->start + em->len < logical);
3670 map = (struct map_lookup *)em->bdev;
3671 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3672 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003673 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3674 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003675 else
3676 ret = 1;
3677 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003678 return ret;
3679}
3680
Chris Masondfe25022008-05-13 13:46:40 -04003681static int find_live_mirror(struct map_lookup *map, int first, int num,
3682 int optimal)
3683{
3684 int i;
3685 if (map->stripes[optimal].dev->bdev)
3686 return optimal;
3687 for (i = first; i < first + num; i++) {
3688 if (map->stripes[i].dev->bdev)
3689 return i;
3690 }
3691 /* we couldn't find one that doesn't fail. Just return something
3692 * and the io error handling code will clean up eventually
3693 */
3694 return optimal;
3695}
3696
Chris Masonf2d8d742008-04-21 10:03:05 -04003697static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3698 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003699 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003700 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003701{
3702 struct extent_map *em;
3703 struct map_lookup *map;
3704 struct extent_map_tree *em_tree = &map_tree->map_tree;
3705 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003706 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003707 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003708 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003709 u64 stripe_nr_orig;
3710 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003711 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003712 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003713 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003714 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003715 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003716 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003717
Chris Mason890871b2009-09-02 16:24:52 -04003718 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003719 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003720 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003721
Chris Mason3b951512008-04-17 11:29:12 -04003722 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003723 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3724 (unsigned long long)logical,
3725 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003726 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003727 }
Chris Mason0b86a832008-03-24 15:01:56 -04003728
3729 BUG_ON(em->start > logical || em->start + em->len < logical);
3730 map = (struct map_lookup *)em->bdev;
3731 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003732
Chris Masonf1885912008-04-09 16:28:12 -04003733 if (mirror_num > map->num_stripes)
3734 mirror_num = 0;
3735
Chris Mason593060d2008-03-25 16:50:33 -04003736 stripe_nr = offset;
3737 /*
3738 * stripe_nr counts the total number of stripes we have to stride
3739 * to get to this block
3740 */
3741 do_div(stripe_nr, map->stripe_len);
3742
3743 stripe_offset = stripe_nr * map->stripe_len;
3744 BUG_ON(offset < stripe_offset);
3745
3746 /* stripe_offset is the offset of this block in its stripe*/
3747 stripe_offset = offset - stripe_offset;
3748
Li Dongyangfce3bb92011-03-24 10:24:26 +00003749 if (rw & REQ_DISCARD)
3750 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003751 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003752 /* we limit the length of each bio to what fits in a stripe */
3753 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003754 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003755 } else {
3756 *length = em->len - offset;
3757 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003758
Jan Schmidta1d3c472011-08-04 17:15:33 +02003759 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003760 goto out;
3761
Chris Masonf2d8d742008-04-21 10:03:05 -04003762 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003763 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003764 stripe_nr_orig = stripe_nr;
3765 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3766 (~(map->stripe_len - 1));
3767 do_div(stripe_nr_end, map->stripe_len);
3768 stripe_end_offset = stripe_nr_end * map->stripe_len -
3769 (offset + *length);
3770 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3771 if (rw & REQ_DISCARD)
3772 num_stripes = min_t(u64, map->num_stripes,
3773 stripe_nr_end - stripe_nr_orig);
3774 stripe_index = do_div(stripe_nr, map->num_stripes);
3775 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003776 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003777 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003778 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003779 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003780 else {
3781 stripe_index = find_live_mirror(map, 0,
3782 map->num_stripes,
3783 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003784 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003785 }
Chris Mason2fff7342008-04-29 14:12:09 -04003786
Chris Mason611f0e02008-04-03 16:29:03 -04003787 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003788 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003789 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003790 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003791 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003792 } else {
3793 mirror_num = 1;
3794 }
Chris Mason2fff7342008-04-29 14:12:09 -04003795
Chris Mason321aecc2008-04-16 10:49:51 -04003796 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3797 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003798
3799 stripe_index = do_div(stripe_nr, factor);
3800 stripe_index *= map->sub_stripes;
3801
Jens Axboe7eaceac2011-03-10 08:52:07 +01003802 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003803 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003804 else if (rw & REQ_DISCARD)
3805 num_stripes = min_t(u64, map->sub_stripes *
3806 (stripe_nr_end - stripe_nr_orig),
3807 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003808 else if (mirror_num)
3809 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003810 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04003811 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04003812 stripe_index = find_live_mirror(map, stripe_index,
3813 map->sub_stripes, stripe_index +
3814 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04003815 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003816 }
Chris Mason8790d502008-04-03 16:29:03 -04003817 } else {
3818 /*
3819 * after this do_div call, stripe_nr is the number of stripes
3820 * on this device we have to walk to find the data, and
3821 * stripe_index is the number of our device in the stripe array
3822 */
3823 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003824 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003825 }
Chris Mason593060d2008-03-25 16:50:33 -04003826 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003827
Li Zefande11cc12011-12-01 12:55:47 +08003828 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3829 if (!bbio) {
3830 ret = -ENOMEM;
3831 goto out;
3832 }
3833 atomic_set(&bbio->error, 0);
3834
Li Dongyangfce3bb92011-03-24 10:24:26 +00003835 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003836 int factor = 0;
3837 int sub_stripes = 0;
3838 u64 stripes_per_dev = 0;
3839 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003840 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003841
3842 if (map->type &
3843 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3844 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3845 sub_stripes = 1;
3846 else
3847 sub_stripes = map->sub_stripes;
3848
3849 factor = map->num_stripes / sub_stripes;
3850 stripes_per_dev = div_u64_rem(stripe_nr_end -
3851 stripe_nr_orig,
3852 factor,
3853 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04003854 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3855 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003856 }
3857
Li Dongyangfce3bb92011-03-24 10:24:26 +00003858 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003859 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003860 map->stripes[stripe_index].physical +
3861 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003862 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003863
Li Zefanec9ef7a2011-12-01 14:06:42 +08003864 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3865 BTRFS_BLOCK_GROUP_RAID10)) {
3866 bbio->stripes[i].length = stripes_per_dev *
3867 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003868
Li Zefanec9ef7a2011-12-01 14:06:42 +08003869 if (i / sub_stripes < remaining_stripes)
3870 bbio->stripes[i].length +=
3871 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003872
3873 /*
3874 * Special for the first stripe and
3875 * the last stripe:
3876 *
3877 * |-------|...|-------|
3878 * |----------|
3879 * off end_off
3880 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08003881 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003882 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003883 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003884
3885 if (stripe_index >= last_stripe &&
3886 stripe_index <= (last_stripe +
3887 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08003888 bbio->stripes[i].length -=
3889 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003890
Li Zefanec9ef7a2011-12-01 14:06:42 +08003891 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003892 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003893 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003894 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003895
3896 stripe_index++;
3897 if (stripe_index == map->num_stripes) {
3898 /* This could only happen for RAID0/10 */
3899 stripe_index = 0;
3900 stripe_nr++;
3901 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003902 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003903 } else {
3904 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003905 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003906 map->stripes[stripe_index].physical +
3907 stripe_offset +
3908 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003909 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003910 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003911 stripe_index++;
3912 }
Chris Mason593060d2008-03-25 16:50:33 -04003913 }
Li Zefande11cc12011-12-01 12:55:47 +08003914
3915 if (rw & REQ_WRITE) {
3916 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3917 BTRFS_BLOCK_GROUP_RAID10 |
3918 BTRFS_BLOCK_GROUP_DUP)) {
3919 max_errors = 1;
3920 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003921 }
Li Zefande11cc12011-12-01 12:55:47 +08003922
3923 *bbio_ret = bbio;
3924 bbio->num_stripes = num_stripes;
3925 bbio->max_errors = max_errors;
3926 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003927out:
Chris Mason0b86a832008-03-24 15:01:56 -04003928 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003929 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003930}
3931
Chris Masonf2d8d742008-04-21 10:03:05 -04003932int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3933 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003934 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003935{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003936 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003937 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003938}
3939
Yan Zhenga512bbf2008-12-08 16:46:26 -05003940int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3941 u64 chunk_start, u64 physical, u64 devid,
3942 u64 **logical, int *naddrs, int *stripe_len)
3943{
3944 struct extent_map_tree *em_tree = &map_tree->map_tree;
3945 struct extent_map *em;
3946 struct map_lookup *map;
3947 u64 *buf;
3948 u64 bytenr;
3949 u64 length;
3950 u64 stripe_nr;
3951 int i, j, nr = 0;
3952
Chris Mason890871b2009-09-02 16:24:52 -04003953 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003954 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003955 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003956
3957 BUG_ON(!em || em->start != chunk_start);
3958 map = (struct map_lookup *)em->bdev;
3959
3960 length = em->len;
3961 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3962 do_div(length, map->num_stripes / map->sub_stripes);
3963 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3964 do_div(length, map->num_stripes);
3965
3966 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003967 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003968
3969 for (i = 0; i < map->num_stripes; i++) {
3970 if (devid && map->stripes[i].dev->devid != devid)
3971 continue;
3972 if (map->stripes[i].physical > physical ||
3973 map->stripes[i].physical + length <= physical)
3974 continue;
3975
3976 stripe_nr = physical - map->stripes[i].physical;
3977 do_div(stripe_nr, map->stripe_len);
3978
3979 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3980 stripe_nr = stripe_nr * map->num_stripes + i;
3981 do_div(stripe_nr, map->sub_stripes);
3982 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3983 stripe_nr = stripe_nr * map->num_stripes + i;
3984 }
3985 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003986 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003987 for (j = 0; j < nr; j++) {
3988 if (buf[j] == bytenr)
3989 break;
3990 }
Chris Mason934d3752008-12-08 16:43:10 -05003991 if (j == nr) {
3992 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003993 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003994 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003995 }
3996
Yan Zhenga512bbf2008-12-08 16:46:26 -05003997 *logical = buf;
3998 *naddrs = nr;
3999 *stripe_len = map->stripe_len;
4000
4001 free_extent_map(em);
4002 return 0;
4003}
4004
Jan Schmidta1d3c472011-08-04 17:15:33 +02004005static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004006{
Jan Schmidta1d3c472011-08-04 17:15:33 +02004007 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004008 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004009
Chris Mason8790d502008-04-03 16:29:03 -04004010 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004011 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04004012
Jan Schmidta1d3c472011-08-04 17:15:33 +02004013 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004014 is_orig_bio = 1;
4015
Jan Schmidta1d3c472011-08-04 17:15:33 +02004016 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004017 if (!is_orig_bio) {
4018 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004019 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004020 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004021 bio->bi_private = bbio->private;
4022 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004023 bio->bi_bdev = (struct block_device *)
4024 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004025 /* only send an error to the higher layers if it is
4026 * beyond the tolerance of the multi-bio
4027 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004028 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004029 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004030 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004031 /*
4032 * this bio is actually up to date, we didn't
4033 * go over the max number of errors
4034 */
4035 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004036 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004037 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004038 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004039
4040 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004041 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004042 bio_put(bio);
4043 }
Chris Mason8790d502008-04-03 16:29:03 -04004044}
4045
Chris Mason8b712842008-06-11 16:50:36 -04004046struct async_sched {
4047 struct bio *bio;
4048 int rw;
4049 struct btrfs_fs_info *info;
4050 struct btrfs_work work;
4051};
4052
4053/*
4054 * see run_scheduled_bios for a description of why bios are collected for
4055 * async submit.
4056 *
4057 * This will add one bio to the pending list for a device and make sure
4058 * the work struct is scheduled.
4059 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004060static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004061 struct btrfs_device *device,
4062 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004063{
4064 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004065 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004066
4067 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004068 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6d2008-07-31 16:29:02 -04004069 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004070 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04004071 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004072 return;
Chris Mason8b712842008-06-11 16:50:36 -04004073 }
4074
4075 /*
Chris Mason0986fe92008-08-15 15:34:15 -04004076 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004077 * higher layers. Otherwise, the async bio makes it appear we have
4078 * made progress against dirty pages when we've really just put it
4079 * on a queue for later
4080 */
Chris Mason0986fe92008-08-15 15:34:15 -04004081 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04004082 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004083 bio->bi_next = NULL;
4084 bio->bi_rw |= rw;
4085
4086 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004087 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004088 pending_bios = &device->pending_sync_bios;
4089 else
4090 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004091
Chris Masonffbd5172009-04-20 15:50:09 -04004092 if (pending_bios->tail)
4093 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004094
Chris Masonffbd5172009-04-20 15:50:09 -04004095 pending_bios->tail = bio;
4096 if (!pending_bios->head)
4097 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004098 if (device->running_pending)
4099 should_queue = 0;
4100
4101 spin_unlock(&device->io_lock);
4102
4103 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004104 btrfs_queue_worker(&root->fs_info->submit_workers,
4105 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004106}
4107
Chris Masonf1885912008-04-09 16:28:12 -04004108int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004109 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004110{
4111 struct btrfs_mapping_tree *map_tree;
4112 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004113 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004114 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004115 u64 length = 0;
4116 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004117 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004118 int dev_nr = 0;
4119 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004120 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004121
Chris Masonf2d8d742008-04-21 10:03:05 -04004122 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004123 map_tree = &root->fs_info->mapping_tree;
4124 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004125
Jan Schmidta1d3c472011-08-04 17:15:33 +02004126 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004127 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004128 if (ret) /* -ENOMEM */
4129 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004130
Jan Schmidta1d3c472011-08-04 17:15:33 +02004131 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004132 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004133 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4134 "len %llu\n", (unsigned long long)logical,
4135 (unsigned long long)length,
4136 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004137 BUG();
4138 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004139
4140 bbio->orig_bio = first_bio;
4141 bbio->private = first_bio->bi_private;
4142 bbio->end_io = first_bio->bi_end_io;
4143 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004144
Chris Masond3977122009-01-05 21:25:51 -05004145 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004146 if (dev_nr < total_devs - 1) {
4147 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004148 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004149 } else {
4150 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004151 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004152 bio->bi_private = bbio;
4153 bio->bi_end_io = btrfs_end_bio;
4154 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4155 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004156 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004157 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4158 "(%s id %llu), size=%u\n", rw,
4159 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4160 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004161 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004162 if (async_submit)
4163 schedule_bio(root, dev, rw, bio);
4164 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004165 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004166 } else {
4167 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4168 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004169 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004170 }
Chris Mason8790d502008-04-03 16:29:03 -04004171 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004172 }
Chris Mason0b86a832008-03-24 15:01:56 -04004173 return 0;
4174}
4175
Chris Masona4437552008-04-18 10:29:38 -04004176struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004177 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004178{
Yan Zheng2b820322008-11-17 21:11:30 -05004179 struct btrfs_device *device;
4180 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004181
Yan Zheng2b820322008-11-17 21:11:30 -05004182 cur_devices = root->fs_info->fs_devices;
4183 while (cur_devices) {
4184 if (!fsid ||
4185 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4186 device = __find_device(&cur_devices->devices,
4187 devid, uuid);
4188 if (device)
4189 return device;
4190 }
4191 cur_devices = cur_devices->seed;
4192 }
4193 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004194}
4195
Chris Masondfe25022008-05-13 13:46:40 -04004196static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4197 u64 devid, u8 *dev_uuid)
4198{
4199 struct btrfs_device *device;
4200 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4201
4202 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004203 if (!device)
4204 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004205 list_add(&device->dev_list,
4206 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004207 device->dev_root = root->fs_info->dev_root;
4208 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004209 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004210 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004211 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004212 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004213 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004214 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004215 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004216 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4217 return device;
4218}
4219
Chris Mason0b86a832008-03-24 15:01:56 -04004220static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4221 struct extent_buffer *leaf,
4222 struct btrfs_chunk *chunk)
4223{
4224 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4225 struct map_lookup *map;
4226 struct extent_map *em;
4227 u64 logical;
4228 u64 length;
4229 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004230 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004231 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004232 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004233 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004234
Chris Masone17cade2008-04-15 15:41:47 -04004235 logical = key->offset;
4236 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004237
Chris Mason890871b2009-09-02 16:24:52 -04004238 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004239 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004240 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004241
4242 /* already mapped? */
4243 if (em && em->start <= logical && em->start + em->len > logical) {
4244 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004245 return 0;
4246 } else if (em) {
4247 free_extent_map(em);
4248 }
Chris Mason0b86a832008-03-24 15:01:56 -04004249
David Sterba172ddd62011-04-21 00:48:27 +02004250 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004251 if (!em)
4252 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004253 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4254 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004255 if (!map) {
4256 free_extent_map(em);
4257 return -ENOMEM;
4258 }
4259
4260 em->bdev = (struct block_device *)map;
4261 em->start = logical;
4262 em->len = length;
4263 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004264 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004265
Chris Mason593060d2008-03-25 16:50:33 -04004266 map->num_stripes = num_stripes;
4267 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4268 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4269 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4270 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4271 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004272 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004273 for (i = 0; i < num_stripes; i++) {
4274 map->stripes[i].physical =
4275 btrfs_stripe_offset_nr(leaf, chunk, i);
4276 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004277 read_extent_buffer(leaf, uuid, (unsigned long)
4278 btrfs_stripe_dev_uuid_nr(chunk, i),
4279 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004280 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4281 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004282 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004283 kfree(map);
4284 free_extent_map(em);
4285 return -EIO;
4286 }
Chris Masondfe25022008-05-13 13:46:40 -04004287 if (!map->stripes[i].dev) {
4288 map->stripes[i].dev =
4289 add_missing_dev(root, devid, uuid);
4290 if (!map->stripes[i].dev) {
4291 kfree(map);
4292 free_extent_map(em);
4293 return -EIO;
4294 }
4295 }
4296 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004297 }
4298
Chris Mason890871b2009-09-02 16:24:52 -04004299 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004300 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004301 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004302 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004303 free_extent_map(em);
4304
4305 return 0;
4306}
4307
Jeff Mahoney143bede2012-03-01 14:56:26 +01004308static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004309 struct btrfs_dev_item *dev_item,
4310 struct btrfs_device *device)
4311{
4312 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004313
4314 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004315 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4316 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004317 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4318 device->type = btrfs_device_type(leaf, dev_item);
4319 device->io_align = btrfs_device_io_align(leaf, dev_item);
4320 device->io_width = btrfs_device_io_width(leaf, dev_item);
4321 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004322
4323 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004324 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004325}
4326
Yan Zheng2b820322008-11-17 21:11:30 -05004327static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4328{
4329 struct btrfs_fs_devices *fs_devices;
4330 int ret;
4331
Li Zefanb367e472011-12-07 11:38:24 +08004332 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004333
4334 fs_devices = root->fs_info->fs_devices->seed;
4335 while (fs_devices) {
4336 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4337 ret = 0;
4338 goto out;
4339 }
4340 fs_devices = fs_devices->seed;
4341 }
4342
4343 fs_devices = find_fsid(fsid);
4344 if (!fs_devices) {
4345 ret = -ENOENT;
4346 goto out;
4347 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004348
4349 fs_devices = clone_fs_devices(fs_devices);
4350 if (IS_ERR(fs_devices)) {
4351 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004352 goto out;
4353 }
4354
Christoph Hellwig97288f22008-12-02 06:36:09 -05004355 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004356 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004357 if (ret) {
4358 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004359 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004360 }
Yan Zheng2b820322008-11-17 21:11:30 -05004361
4362 if (!fs_devices->seeding) {
4363 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004364 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004365 ret = -EINVAL;
4366 goto out;
4367 }
4368
4369 fs_devices->seed = root->fs_info->fs_devices->seed;
4370 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004371out:
Yan Zheng2b820322008-11-17 21:11:30 -05004372 return ret;
4373}
4374
Chris Mason0d81ba52008-03-24 15:02:07 -04004375static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004376 struct extent_buffer *leaf,
4377 struct btrfs_dev_item *dev_item)
4378{
4379 struct btrfs_device *device;
4380 u64 devid;
4381 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004382 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004383 u8 dev_uuid[BTRFS_UUID_SIZE];
4384
Chris Mason0b86a832008-03-24 15:01:56 -04004385 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004386 read_extent_buffer(leaf, dev_uuid,
4387 (unsigned long)btrfs_device_uuid(dev_item),
4388 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004389 read_extent_buffer(leaf, fs_uuid,
4390 (unsigned long)btrfs_device_fsid(dev_item),
4391 BTRFS_UUID_SIZE);
4392
4393 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4394 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004395 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004396 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004397 }
4398
4399 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4400 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004401 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004402 return -EIO;
4403
4404 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004405 printk(KERN_WARNING "warning devid %llu missing\n",
4406 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004407 device = add_missing_dev(root, devid, dev_uuid);
4408 if (!device)
4409 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004410 } else if (!device->missing) {
4411 /*
4412 * this happens when a device that was properly setup
4413 * in the device info lists suddenly goes bad.
4414 * device->bdev is NULL, and so we have to set
4415 * device->missing to one here
4416 */
4417 root->fs_info->fs_devices->missing_devices++;
4418 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004419 }
4420 }
4421
4422 if (device->fs_devices != root->fs_info->fs_devices) {
4423 BUG_ON(device->writeable);
4424 if (device->generation !=
4425 btrfs_device_generation(leaf, dev_item))
4426 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004427 }
Chris Mason0b86a832008-03-24 15:01:56 -04004428
4429 fill_device_from_item(leaf, dev_item, device);
4430 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004431 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004432 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004433 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004434 spin_lock(&root->fs_info->free_chunk_lock);
4435 root->fs_info->free_chunk_space += device->total_bytes -
4436 device->bytes_used;
4437 spin_unlock(&root->fs_info->free_chunk_lock);
4438 }
Chris Mason0b86a832008-03-24 15:01:56 -04004439 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004440 return ret;
4441}
4442
Yan Zhenge4404d62008-12-12 10:03:26 -05004443int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004444{
David Sterba6c417612011-04-13 15:41:04 +02004445 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004446 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004447 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004448 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004449 u8 *ptr;
4450 unsigned long sb_ptr;
4451 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004452 u32 num_stripes;
4453 u32 array_size;
4454 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004455 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004456 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004457
Yan Zhenge4404d62008-12-12 10:03:26 -05004458 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004459 BTRFS_SUPER_INFO_SIZE);
4460 if (!sb)
4461 return -ENOMEM;
4462 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004463 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004464 /*
4465 * The sb extent buffer is artifical and just used to read the system array.
4466 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4467 * pages up-to-date when the page is larger: extent does not cover the
4468 * whole page and consequently check_page_uptodate does not find all
4469 * the page's extents up-to-date (the hole beyond sb),
4470 * write_extent_buffer then triggers a WARN_ON.
4471 *
4472 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4473 * but sb spans only this function. Add an explicit SetPageUptodate call
4474 * to silence the warning eg. on PowerPC 64.
4475 */
4476 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004477 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004478
Chris Masona061fc82008-05-07 11:43:44 -04004479 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004480 array_size = btrfs_super_sys_array_size(super_copy);
4481
Chris Mason0b86a832008-03-24 15:01:56 -04004482 ptr = super_copy->sys_chunk_array;
4483 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4484 cur = 0;
4485
4486 while (cur < array_size) {
4487 disk_key = (struct btrfs_disk_key *)ptr;
4488 btrfs_disk_key_to_cpu(&key, disk_key);
4489
Chris Masona061fc82008-05-07 11:43:44 -04004490 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004491 sb_ptr += len;
4492 cur += len;
4493
Chris Mason0d81ba52008-03-24 15:02:07 -04004494 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004495 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004496 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004497 if (ret)
4498 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004499 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4500 len = btrfs_chunk_item_size(num_stripes);
4501 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004502 ret = -EIO;
4503 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004504 }
4505 ptr += len;
4506 sb_ptr += len;
4507 cur += len;
4508 }
Chris Masona061fc82008-05-07 11:43:44 -04004509 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004510 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004511}
4512
4513int btrfs_read_chunk_tree(struct btrfs_root *root)
4514{
4515 struct btrfs_path *path;
4516 struct extent_buffer *leaf;
4517 struct btrfs_key key;
4518 struct btrfs_key found_key;
4519 int ret;
4520 int slot;
4521
4522 root = root->fs_info->chunk_root;
4523
4524 path = btrfs_alloc_path();
4525 if (!path)
4526 return -ENOMEM;
4527
Li Zefanb367e472011-12-07 11:38:24 +08004528 mutex_lock(&uuid_mutex);
4529 lock_chunks(root);
4530
Chris Mason0b86a832008-03-24 15:01:56 -04004531 /* first we search for all of the device items, and then we
4532 * read in all of the chunk items. This way we can create chunk
4533 * mappings that reference all of the devices that are afound
4534 */
4535 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4536 key.offset = 0;
4537 key.type = 0;
4538again:
4539 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004540 if (ret < 0)
4541 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004542 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004543 leaf = path->nodes[0];
4544 slot = path->slots[0];
4545 if (slot >= btrfs_header_nritems(leaf)) {
4546 ret = btrfs_next_leaf(root, path);
4547 if (ret == 0)
4548 continue;
4549 if (ret < 0)
4550 goto error;
4551 break;
4552 }
4553 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4554 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4555 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4556 break;
4557 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4558 struct btrfs_dev_item *dev_item;
4559 dev_item = btrfs_item_ptr(leaf, slot,
4560 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004561 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004562 if (ret)
4563 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004564 }
4565 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4566 struct btrfs_chunk *chunk;
4567 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4568 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004569 if (ret)
4570 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004571 }
4572 path->slots[0]++;
4573 }
4574 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4575 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004576 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004577 goto again;
4578 }
Chris Mason0b86a832008-03-24 15:01:56 -04004579 ret = 0;
4580error:
Li Zefanb367e472011-12-07 11:38:24 +08004581 unlock_chunks(root);
4582 mutex_unlock(&uuid_mutex);
4583
Yan Zheng2b820322008-11-17 21:11:30 -05004584 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004585 return ret;
4586}