blob: 06744f1e91f492547a7110e7feac262484adc688 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020026#include <linux/kthread.h>
Chris Mason593060d2008-03-25 16:50:33 -040027#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050028#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040029#include "ctree.h"
30#include "extent_map.h"
31#include "disk-io.h"
32#include "transaction.h"
33#include "print-tree.h"
34#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040035#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010036#include "check-integrity.h"
Chris Mason0b86a832008-03-24 15:01:56 -040037
Yan Zheng2b820322008-11-17 21:11:30 -050038static int init_first_rw_device(struct btrfs_trans_handle *trans,
39 struct btrfs_root *root,
40 struct btrfs_device *device);
41static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
42
Chris Mason8a4b83c2008-03-24 15:02:07 -040043static DEFINE_MUTEX(uuid_mutex);
44static LIST_HEAD(fs_uuids);
45
Chris Mason7d9eb122008-07-08 14:19:17 -040046static void lock_chunks(struct btrfs_root *root)
47{
Chris Mason7d9eb122008-07-08 14:19:17 -040048 mutex_lock(&root->fs_info->chunk_mutex);
49}
50
51static void unlock_chunks(struct btrfs_root *root)
52{
Chris Mason7d9eb122008-07-08 14:19:17 -040053 mutex_unlock(&root->fs_info->chunk_mutex);
54}
55
Yan Zhenge4404d62008-12-12 10:03:26 -050056static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
57{
58 struct btrfs_device *device;
59 WARN_ON(fs_devices->opened);
60 while (!list_empty(&fs_devices->devices)) {
61 device = list_entry(fs_devices->devices.next,
62 struct btrfs_device, dev_list);
63 list_del(&device->dev_list);
64 kfree(device->name);
65 kfree(device);
66 }
67 kfree(fs_devices);
68}
69
Jeff Mahoney143bede2012-03-01 14:56:26 +010070void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040071{
72 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040073
Yan Zheng2b820322008-11-17 21:11:30 -050074 while (!list_empty(&fs_uuids)) {
75 fs_devices = list_entry(fs_uuids.next,
76 struct btrfs_fs_devices, list);
77 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050078 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040079 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040080}
81
Chris Masona1b32a52008-09-05 16:09:51 -040082static noinline struct btrfs_device *__find_device(struct list_head *head,
83 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040084{
85 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040086
Qinghuang Fengc6e30872009-01-21 10:59:08 -050087 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040088 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040089 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040090 return dev;
Chris Masona4437552008-04-18 10:29:38 -040091 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040092 }
93 return NULL;
94}
95
Chris Masona1b32a52008-09-05 16:09:51 -040096static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040097{
Chris Mason8a4b83c2008-03-24 15:02:07 -040098 struct btrfs_fs_devices *fs_devices;
99
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500100 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
102 return fs_devices;
103 }
104 return NULL;
105}
106
Chris Masonffbd5172009-04-20 15:50:09 -0400107static void requeue_list(struct btrfs_pending_bios *pending_bios,
108 struct bio *head, struct bio *tail)
109{
110
111 struct bio *old_head;
112
113 old_head = pending_bios->head;
114 pending_bios->head = head;
115 if (pending_bios->tail)
116 tail->bi_next = old_head;
117 else
118 pending_bios->tail = tail;
119}
120
Chris Mason8b712842008-06-11 16:50:36 -0400121/*
122 * we try to collect pending bios for a device so we don't get a large
123 * number of procs sending bios down to the same device. This greatly
124 * improves the schedulers ability to collect and merge the bios.
125 *
126 * But, it also turns into a long list of bios to process and that is sure
127 * to eventually make the worker thread block. The solution here is to
128 * make some progress and then put this work struct back at the end of
129 * the list if the block device is congested. This way, multiple devices
130 * can make progress from a single worker thread.
131 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100132static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400133{
134 struct bio *pending;
135 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400136 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400137 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400138 struct bio *tail;
139 struct bio *cur;
140 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400141 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400142 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400143 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400144 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400145 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000146 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400147 struct blk_plug plug;
148
149 /*
150 * this function runs all the bios we've collected for
151 * a particular device. We don't want to wander off to
152 * another device without first sending all of these down.
153 * So, setup a plug here and finish it off before we return
154 */
155 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400156
Chris Masonbedf7622009-04-03 10:32:58 -0400157 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400158 fs_info = device->dev_root->fs_info;
159 limit = btrfs_async_submit_limit(fs_info);
160 limit = limit * 2 / 3;
161
Chris Mason8b712842008-06-11 16:50:36 -0400162loop:
163 spin_lock(&device->io_lock);
164
Chris Masona6837052009-02-04 09:19:41 -0500165loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400166 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400167
Chris Mason8b712842008-06-11 16:50:36 -0400168 /* take all the bios off the list at once and process them
169 * later on (without the lock held). But, remember the
170 * tail and other pointers so the bios can be properly reinserted
171 * into the list if we hit congestion
172 */
Chris Masond84275c2009-06-09 15:39:08 -0400173 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400174 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400175 force_reg = 1;
176 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400177 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400178 force_reg = 0;
179 }
Chris Masonffbd5172009-04-20 15:50:09 -0400180
181 pending = pending_bios->head;
182 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400183 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400184
185 /*
186 * if pending was null this time around, no bios need processing
187 * at all and we can stop. Otherwise it'll loop back up again
188 * and do an additional check so no bios are missed.
189 *
190 * device->running_pending is used to synchronize with the
191 * schedule_bio code.
192 */
Chris Masonffbd5172009-04-20 15:50:09 -0400193 if (device->pending_sync_bios.head == NULL &&
194 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400195 again = 0;
196 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400197 } else {
198 again = 1;
199 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400200 }
Chris Masonffbd5172009-04-20 15:50:09 -0400201
202 pending_bios->head = NULL;
203 pending_bios->tail = NULL;
204
Chris Mason8b712842008-06-11 16:50:36 -0400205 spin_unlock(&device->io_lock);
206
Chris Masond3977122009-01-05 21:25:51 -0500207 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400208
209 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400210 /* we want to work on both lists, but do more bios on the
211 * sync list than the regular list
212 */
213 if ((num_run > 32 &&
214 pending_bios != &device->pending_sync_bios &&
215 device->pending_sync_bios.head) ||
216 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
217 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400218 spin_lock(&device->io_lock);
219 requeue_list(pending_bios, pending, tail);
220 goto loop_lock;
221 }
222
Chris Mason8b712842008-06-11 16:50:36 -0400223 cur = pending;
224 pending = pending->bi_next;
225 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400226 atomic_dec(&fs_info->nr_async_bios);
227
228 if (atomic_read(&fs_info->nr_async_bios) < limit &&
229 waitqueue_active(&fs_info->async_submit_wait))
230 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6d2008-07-31 16:29:02 -0400231
232 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400233
Chris Mason2ab1ba62011-08-04 14:28:36 -0400234 /*
235 * if we're doing the sync list, record that our
236 * plug has some sync requests on it
237 *
238 * If we're doing the regular list and there are
239 * sync requests sitting around, unplug before
240 * we add more
241 */
242 if (pending_bios == &device->pending_sync_bios) {
243 sync_pending = 1;
244 } else if (sync_pending) {
245 blk_finish_plug(&plug);
246 blk_start_plug(&plug);
247 sync_pending = 0;
248 }
249
Stefan Behrens21adbd52011-11-09 13:44:05 +0100250 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400251 num_run++;
252 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100253 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400254 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400255
256 /*
257 * we made progress, there is more work to do and the bdi
258 * is now congested. Back off and let other work structs
259 * run instead
260 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400261 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500262 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400263 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400264
Chris Masonb765ead2009-04-03 10:27:10 -0400265 ioc = current->io_context;
266
267 /*
268 * the main goal here is that we don't want to
269 * block if we're going to be able to submit
270 * more requests without blocking.
271 *
272 * This code does two great things, it pokes into
273 * the elevator code from a filesystem _and_
274 * it makes assumptions about how batching works.
275 */
276 if (ioc && ioc->nr_batch_requests > 0 &&
277 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
278 (last_waited == 0 ||
279 ioc->last_waited == last_waited)) {
280 /*
281 * we want to go through our batch of
282 * requests and stop. So, we copy out
283 * the ioc->last_waited time and test
284 * against it before looping
285 */
286 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100287 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400288 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400289 continue;
290 }
Chris Mason8b712842008-06-11 16:50:36 -0400291 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400292 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500293 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400294
295 spin_unlock(&device->io_lock);
296 btrfs_requeue_work(&device->work);
297 goto done;
298 }
Chris Masond85c8a62011-12-15 15:38:41 -0500299 /* unplug every 64 requests just for good measure */
300 if (batch_run % 64 == 0) {
301 blk_finish_plug(&plug);
302 blk_start_plug(&plug);
303 sync_pending = 0;
304 }
Chris Mason8b712842008-06-11 16:50:36 -0400305 }
Chris Masonffbd5172009-04-20 15:50:09 -0400306
Chris Mason51684082010-03-10 15:33:32 -0500307 cond_resched();
308 if (again)
309 goto loop;
310
311 spin_lock(&device->io_lock);
312 if (device->pending_bios.head || device->pending_sync_bios.head)
313 goto loop_lock;
314 spin_unlock(&device->io_lock);
315
Chris Mason8b712842008-06-11 16:50:36 -0400316done:
Chris Mason211588a2011-04-19 20:12:40 -0400317 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400318}
319
Christoph Hellwigb2950862008-12-02 09:54:17 -0500320static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400321{
322 struct btrfs_device *device;
323
324 device = container_of(work, struct btrfs_device, work);
325 run_scheduled_bios(device);
326}
327
Chris Masona1b32a52008-09-05 16:09:51 -0400328static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400329 struct btrfs_super_block *disk_super,
330 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
331{
332 struct btrfs_device *device;
333 struct btrfs_fs_devices *fs_devices;
334 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000335 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400336
337 fs_devices = find_fsid(disk_super->fsid);
338 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400339 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400340 if (!fs_devices)
341 return -ENOMEM;
342 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400343 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400344 list_add(&fs_devices->list, &fs_uuids);
345 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
346 fs_devices->latest_devid = devid;
347 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400348 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400349 device = NULL;
350 } else {
Chris Masona4437552008-04-18 10:29:38 -0400351 device = __find_device(&fs_devices->devices, devid,
352 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400353 }
354 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500355 if (fs_devices->opened)
356 return -EBUSY;
357
Chris Mason8a4b83c2008-03-24 15:02:07 -0400358 device = kzalloc(sizeof(*device), GFP_NOFS);
359 if (!device) {
360 /* we can safely leave the fs_devices entry around */
361 return -ENOMEM;
362 }
363 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400364 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400365 memcpy(device->uuid, disk_super->dev_item.uuid,
366 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400367 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400368 device->name = kstrdup(path, GFP_NOFS);
369 if (!device->name) {
370 kfree(device);
371 return -ENOMEM;
372 }
Yan Zheng2b820322008-11-17 21:11:30 -0500373 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400374
Arne Jansen90519d62011-05-23 14:30:00 +0200375 /* init readahead state */
376 spin_lock_init(&device->reada_lock);
377 device->reada_curr_zone = NULL;
378 atomic_set(&device->reada_in_flight, 0);
379 device->reada_next = 0;
380 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
381 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
382
Chris Masone5e9a522009-06-10 15:17:02 -0400383 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000384 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400385 mutex_unlock(&fs_devices->device_list_mutex);
386
Yan Zheng2b820322008-11-17 21:11:30 -0500387 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400388 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500389 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000390 name = kstrdup(path, GFP_NOFS);
391 if (!name)
392 return -ENOMEM;
393 kfree(device->name);
394 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500395 if (device->missing) {
396 fs_devices->missing_devices--;
397 device->missing = 0;
398 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400399 }
400
401 if (found_transid > fs_devices->latest_trans) {
402 fs_devices->latest_devid = devid;
403 fs_devices->latest_trans = found_transid;
404 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400405 *fs_devices_ret = fs_devices;
406 return 0;
407}
408
Yan Zhenge4404d62008-12-12 10:03:26 -0500409static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
410{
411 struct btrfs_fs_devices *fs_devices;
412 struct btrfs_device *device;
413 struct btrfs_device *orig_dev;
414
415 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
416 if (!fs_devices)
417 return ERR_PTR(-ENOMEM);
418
419 INIT_LIST_HEAD(&fs_devices->devices);
420 INIT_LIST_HEAD(&fs_devices->alloc_list);
421 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400422 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500423 fs_devices->latest_devid = orig->latest_devid;
424 fs_devices->latest_trans = orig->latest_trans;
425 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
426
Xiao Guangrong46224702011-04-20 10:08:47 +0000427 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500428 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
429 device = kzalloc(sizeof(*device), GFP_NOFS);
430 if (!device)
431 goto error;
432
433 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400434 if (!device->name) {
435 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500436 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400437 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500438
439 device->devid = orig_dev->devid;
440 device->work.func = pending_bios_fn;
441 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500442 spin_lock_init(&device->io_lock);
443 INIT_LIST_HEAD(&device->dev_list);
444 INIT_LIST_HEAD(&device->dev_alloc_list);
445
446 list_add(&device->dev_list, &fs_devices->devices);
447 device->fs_devices = fs_devices;
448 fs_devices->num_devices++;
449 }
450 return fs_devices;
451error:
452 free_fs_devices(fs_devices);
453 return ERR_PTR(-ENOMEM);
454}
455
Jeff Mahoney143bede2012-03-01 14:56:26 +0100456void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
Chris Masondfe25022008-05-13 13:46:40 -0400457{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500458 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400459
Chris Masona6b0d5c2012-02-20 20:53:43 -0500460 struct block_device *latest_bdev = NULL;
461 u64 latest_devid = 0;
462 u64 latest_transid = 0;
463
Chris Masondfe25022008-05-13 13:46:40 -0400464 mutex_lock(&uuid_mutex);
465again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000466 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500467 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500468 if (device->in_fs_metadata) {
469 if (!latest_transid ||
470 device->generation > latest_transid) {
471 latest_devid = device->devid;
472 latest_transid = device->generation;
473 latest_bdev = device->bdev;
474 }
Yan Zheng2b820322008-11-17 21:11:30 -0500475 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500476 }
Yan Zheng2b820322008-11-17 21:11:30 -0500477
478 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100479 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500480 device->bdev = NULL;
481 fs_devices->open_devices--;
482 }
483 if (device->writeable) {
484 list_del_init(&device->dev_alloc_list);
485 device->writeable = 0;
486 fs_devices->rw_devices--;
487 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500488 list_del_init(&device->dev_list);
489 fs_devices->num_devices--;
490 kfree(device->name);
491 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400492 }
Yan Zheng2b820322008-11-17 21:11:30 -0500493
494 if (fs_devices->seed) {
495 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500496 goto again;
497 }
498
Chris Masona6b0d5c2012-02-20 20:53:43 -0500499 fs_devices->latest_bdev = latest_bdev;
500 fs_devices->latest_devid = latest_devid;
501 fs_devices->latest_trans = latest_transid;
502
Chris Masondfe25022008-05-13 13:46:40 -0400503 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400504}
Chris Masona0af4692008-05-13 16:03:06 -0400505
Xiao Guangrong1f781602011-04-20 10:09:16 +0000506static void __free_device(struct work_struct *work)
507{
508 struct btrfs_device *device;
509
510 device = container_of(work, struct btrfs_device, rcu_work);
511
512 if (device->bdev)
513 blkdev_put(device->bdev, device->mode);
514
515 kfree(device->name);
516 kfree(device);
517}
518
519static void free_device(struct rcu_head *head)
520{
521 struct btrfs_device *device;
522
523 device = container_of(head, struct btrfs_device, rcu);
524
525 INIT_WORK(&device->rcu_work, __free_device);
526 schedule_work(&device->rcu_work);
527}
528
Yan Zheng2b820322008-11-17 21:11:30 -0500529static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400530{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400531 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500532
Yan Zheng2b820322008-11-17 21:11:30 -0500533 if (--fs_devices->opened > 0)
534 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000536 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500537 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000538 struct btrfs_device *new_device;
539
540 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400541 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000542
Yan Zheng2b820322008-11-17 21:11:30 -0500543 if (device->writeable) {
544 list_del_init(&device->dev_alloc_list);
545 fs_devices->rw_devices--;
546 }
547
Josef Bacikd5e20032011-08-04 14:52:27 +0000548 if (device->can_discard)
549 fs_devices->num_can_discard--;
550
Xiao Guangrong1f781602011-04-20 10:09:16 +0000551 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100552 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000553 memcpy(new_device, device, sizeof(*new_device));
554 new_device->name = kstrdup(device->name, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100555 BUG_ON(device->name && !new_device->name); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000556 new_device->bdev = NULL;
557 new_device->writeable = 0;
558 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000559 new_device->can_discard = 0;
Thomas Gleixnerd0e44ed2013-02-20 14:06:20 -0500560 spin_lock_init(&new_device->io_lock);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000561 list_replace_rcu(&device->dev_list, &new_device->dev_list);
562
563 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400564 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000565 mutex_unlock(&fs_devices->device_list_mutex);
566
Yan Zhenge4404d62008-12-12 10:03:26 -0500567 WARN_ON(fs_devices->open_devices);
568 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500569 fs_devices->opened = 0;
570 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500571
Chris Mason8a4b83c2008-03-24 15:02:07 -0400572 return 0;
573}
574
Yan Zheng2b820322008-11-17 21:11:30 -0500575int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
576{
Yan Zhenge4404d62008-12-12 10:03:26 -0500577 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500578 int ret;
579
580 mutex_lock(&uuid_mutex);
581 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500582 if (!fs_devices->opened) {
583 seed_devices = fs_devices->seed;
584 fs_devices->seed = NULL;
585 }
Yan Zheng2b820322008-11-17 21:11:30 -0500586 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500587
588 while (seed_devices) {
589 fs_devices = seed_devices;
590 seed_devices = fs_devices->seed;
591 __btrfs_close_devices(fs_devices);
592 free_fs_devices(fs_devices);
593 }
Eric Sandeena40a9452013-03-09 15:18:39 +0000594 /*
595 * Wait for rcu kworkers under __btrfs_close_devices
596 * to finish all blkdev_puts so device is really
597 * free when umount is done.
598 */
599 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500600 return ret;
601}
602
Yan Zhenge4404d62008-12-12 10:03:26 -0500603static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
604 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400605{
Josef Bacikd5e20032011-08-04 14:52:27 +0000606 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400607 struct block_device *bdev;
608 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400609 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400610 struct block_device *latest_bdev = NULL;
611 struct buffer_head *bh;
612 struct btrfs_super_block *disk_super;
613 u64 latest_devid = 0;
614 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400615 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500616 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400617 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400618
Tejun Heod4d77622010-11-13 11:55:18 +0100619 flags |= FMODE_EXCL;
620
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500621 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400622 if (device->bdev)
623 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400624 if (!device->name)
625 continue;
626
Tejun Heod4d77622010-11-13 11:55:18 +0100627 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400628 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500629 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400630 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400631 }
Chris Mason3c4bb262012-03-27 18:56:56 -0400632 filemap_write_and_wait(bdev->bd_inode->i_mapping);
633 invalidate_bdev(bdev);
Chris Masona061fc82008-05-07 11:43:44 -0400634 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400635
Yan Zhenga512bbf2008-12-08 16:46:26 -0500636 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300637 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400638 goto error_close;
639
640 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000641 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400642 if (devid != device->devid)
643 goto error_brelse;
644
Yan Zheng2b820322008-11-17 21:11:30 -0500645 if (memcmp(device->uuid, disk_super->dev_item.uuid,
646 BTRFS_UUID_SIZE))
647 goto error_brelse;
648
649 device->generation = btrfs_super_generation(disk_super);
650 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400651 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500652 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400653 latest_bdev = bdev;
654 }
655
Yan Zheng2b820322008-11-17 21:11:30 -0500656 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
657 device->writeable = 0;
658 } else {
659 device->writeable = !bdev_read_only(bdev);
660 seeding = 0;
661 }
662
Josef Bacikd5e20032011-08-04 14:52:27 +0000663 q = bdev_get_queue(bdev);
664 if (blk_queue_discard(q)) {
665 device->can_discard = 1;
666 fs_devices->num_can_discard++;
667 }
668
Chris Mason8a4b83c2008-03-24 15:02:07 -0400669 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400670 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500671 device->mode = flags;
672
Chris Masonc2898112009-06-10 09:51:32 -0400673 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
674 fs_devices->rotating = 1;
675
Chris Masona0af4692008-05-13 16:03:06 -0400676 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500677 if (device->writeable) {
678 fs_devices->rw_devices++;
679 list_add(&device->dev_alloc_list,
680 &fs_devices->alloc_list);
681 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000682 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400683 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400684
Chris Masona0af4692008-05-13 16:03:06 -0400685error_brelse:
686 brelse(bh);
687error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100688 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400689error:
690 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400691 }
Chris Masona0af4692008-05-13 16:03:06 -0400692 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300693 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400694 goto out;
695 }
Yan Zheng2b820322008-11-17 21:11:30 -0500696 fs_devices->seeding = seeding;
697 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400698 fs_devices->latest_bdev = latest_bdev;
699 fs_devices->latest_devid = latest_devid;
700 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500701 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400702out:
Yan Zheng2b820322008-11-17 21:11:30 -0500703 return ret;
704}
705
706int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500707 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500708{
709 int ret;
710
711 mutex_lock(&uuid_mutex);
712 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500713 fs_devices->opened++;
714 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500715 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500716 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500717 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400718 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400719 return ret;
720}
721
Christoph Hellwig97288f22008-12-02 06:36:09 -0500722int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400723 struct btrfs_fs_devices **fs_devices_ret)
724{
725 struct btrfs_super_block *disk_super;
726 struct block_device *bdev;
727 struct buffer_head *bh;
728 int ret;
729 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400730 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400731
Tejun Heod4d77622010-11-13 11:55:18 +0100732 flags |= FMODE_EXCL;
733 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400734
735 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400736 ret = PTR_ERR(bdev);
737 goto error;
738 }
739
Al Viro10f63272011-11-17 15:05:22 -0500740 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400741 ret = set_blocksize(bdev, 4096);
742 if (ret)
743 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500744 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400745 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000746 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400747 goto error_close;
748 }
749 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000750 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400751 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400752 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500753 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200754 else
755 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500756 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500757 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
759
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760 brelse(bh);
761error_close:
Al Viro10f63272011-11-17 15:05:22 -0500762 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100763 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400764error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400765 return ret;
766}
Chris Mason0b86a832008-03-24 15:01:56 -0400767
Miao Xie6d07bce2011-01-05 10:07:31 +0000768/* helper to account the used device space in the range */
769int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
770 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400771{
772 struct btrfs_key key;
773 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000774 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500775 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000776 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400777 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000778 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400779 struct extent_buffer *l;
780
Miao Xie6d07bce2011-01-05 10:07:31 +0000781 *length = 0;
782
783 if (start >= device->total_bytes)
784 return 0;
785
Yan Zheng2b820322008-11-17 21:11:30 -0500786 path = btrfs_alloc_path();
787 if (!path)
788 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400789 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400790
Chris Mason0b86a832008-03-24 15:01:56 -0400791 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000792 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400793 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000794
795 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400796 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000797 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400798 if (ret > 0) {
799 ret = btrfs_previous_item(root, path, key.objectid, key.type);
800 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000801 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400802 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000803
Chris Mason0b86a832008-03-24 15:01:56 -0400804 while (1) {
805 l = path->nodes[0];
806 slot = path->slots[0];
807 if (slot >= btrfs_header_nritems(l)) {
808 ret = btrfs_next_leaf(root, path);
809 if (ret == 0)
810 continue;
811 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000812 goto out;
813
814 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400815 }
816 btrfs_item_key_to_cpu(l, &key, slot);
817
818 if (key.objectid < device->devid)
819 goto next;
820
821 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000822 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400823
Chris Masond3977122009-01-05 21:25:51 -0500824 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400825 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400826
Chris Mason0b86a832008-03-24 15:01:56 -0400827 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000828 extent_end = key.offset + btrfs_dev_extent_length(l,
829 dev_extent);
830 if (key.offset <= start && extent_end > end) {
831 *length = end - start + 1;
832 break;
833 } else if (key.offset <= start && extent_end > start)
834 *length += extent_end - start;
835 else if (key.offset > start && extent_end <= end)
836 *length += extent_end - key.offset;
837 else if (key.offset > start && key.offset <= end) {
838 *length += end - key.offset + 1;
839 break;
840 } else if (key.offset > end)
841 break;
842
843next:
844 path->slots[0]++;
845 }
846 ret = 0;
847out:
848 btrfs_free_path(path);
849 return ret;
850}
851
Chris Mason0b86a832008-03-24 15:01:56 -0400852/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000853 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000854 * @device: the device which we search the free space in
855 * @num_bytes: the size of the free space that we need
856 * @start: store the start of the free space.
857 * @len: the size of the free space. that we find, or the size of the max
858 * free space if we don't find suitable free space
859 *
Chris Mason0b86a832008-03-24 15:01:56 -0400860 * this uses a pretty simple search, the expectation is that it is
861 * called very infrequently and that a given device has a small number
862 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000863 *
864 * @start is used to store the start of the free space if we find. But if we
865 * don't find suitable free space, it will be used to store the start position
866 * of the max free space.
867 *
868 * @len is used to store the size of the free space that we find.
869 * But if we don't find suitable free space, it is used to store the size of
870 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400871 */
Li Zefan125ccb02011-12-08 15:07:24 +0800872int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000873 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400874{
875 struct btrfs_key key;
876 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000877 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400878 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000879 u64 hole_size;
880 u64 max_hole_start;
881 u64 max_hole_size;
882 u64 extent_end;
883 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400884 u64 search_end = device->total_bytes;
885 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000886 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400887 struct extent_buffer *l;
888
Chris Mason0b86a832008-03-24 15:01:56 -0400889 /* FIXME use last free of some kind */
890
891 /* we don't want to overwrite the superblock on the drive,
892 * so we make sure to start at an offset of at least 1MB
893 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200894 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400895
Miao Xie7bfc8372011-01-05 10:07:26 +0000896 max_hole_start = search_start;
897 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000898 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000899
900 if (search_start >= search_end) {
901 ret = -ENOSPC;
902 goto error;
903 }
904
905 path = btrfs_alloc_path();
906 if (!path) {
907 ret = -ENOMEM;
908 goto error;
909 }
910 path->reada = 2;
911
Chris Mason0b86a832008-03-24 15:01:56 -0400912 key.objectid = device->devid;
913 key.offset = search_start;
914 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000915
Li Zefan125ccb02011-12-08 15:07:24 +0800916 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400917 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000918 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400919 if (ret > 0) {
920 ret = btrfs_previous_item(root, path, key.objectid, key.type);
921 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000922 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400923 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000924
Chris Mason0b86a832008-03-24 15:01:56 -0400925 while (1) {
926 l = path->nodes[0];
927 slot = path->slots[0];
928 if (slot >= btrfs_header_nritems(l)) {
929 ret = btrfs_next_leaf(root, path);
930 if (ret == 0)
931 continue;
932 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000933 goto out;
934
935 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400936 }
937 btrfs_item_key_to_cpu(l, &key, slot);
938
939 if (key.objectid < device->devid)
940 goto next;
941
942 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000943 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400944
Chris Mason0b86a832008-03-24 15:01:56 -0400945 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
946 goto next;
947
Miao Xie7bfc8372011-01-05 10:07:26 +0000948 if (key.offset > search_start) {
949 hole_size = key.offset - search_start;
950
951 if (hole_size > max_hole_size) {
952 max_hole_start = search_start;
953 max_hole_size = hole_size;
954 }
955
956 /*
957 * If this free space is greater than which we need,
958 * it must be the max free space that we have found
959 * until now, so max_hole_start must point to the start
960 * of this free space and the length of this free space
961 * is stored in max_hole_size. Thus, we return
962 * max_hole_start and max_hole_size and go back to the
963 * caller.
964 */
965 if (hole_size >= num_bytes) {
966 ret = 0;
967 goto out;
968 }
969 }
970
Chris Mason0b86a832008-03-24 15:01:56 -0400971 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000972 extent_end = key.offset + btrfs_dev_extent_length(l,
973 dev_extent);
974 if (extent_end > search_start)
975 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400976next:
977 path->slots[0]++;
978 cond_resched();
979 }
Chris Mason0b86a832008-03-24 15:01:56 -0400980
liubo38c01b92011-08-02 02:39:03 +0000981 /*
982 * At this point, search_start should be the end of
983 * allocated dev extents, and when shrinking the device,
984 * search_end may be smaller than search_start.
985 */
986 if (search_end > search_start)
987 hole_size = search_end - search_start;
988
Miao Xie7bfc8372011-01-05 10:07:26 +0000989 if (hole_size > max_hole_size) {
990 max_hole_start = search_start;
991 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400992 }
Chris Mason0b86a832008-03-24 15:01:56 -0400993
Miao Xie7bfc8372011-01-05 10:07:26 +0000994 /* See above. */
995 if (hole_size < num_bytes)
996 ret = -ENOSPC;
997 else
998 ret = 0;
999
1000out:
Yan Zheng2b820322008-11-17 21:11:30 -05001001 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001002error:
1003 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001004 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001005 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001006 return ret;
1007}
1008
Christoph Hellwigb2950862008-12-02 09:54:17 -05001009static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001010 struct btrfs_device *device,
1011 u64 start)
1012{
1013 int ret;
1014 struct btrfs_path *path;
1015 struct btrfs_root *root = device->dev_root;
1016 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001017 struct btrfs_key found_key;
1018 struct extent_buffer *leaf = NULL;
1019 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001020
1021 path = btrfs_alloc_path();
1022 if (!path)
1023 return -ENOMEM;
1024
1025 key.objectid = device->devid;
1026 key.offset = start;
1027 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001028again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001029 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001030 if (ret > 0) {
1031 ret = btrfs_previous_item(root, path, key.objectid,
1032 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001033 if (ret)
1034 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001035 leaf = path->nodes[0];
1036 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1037 extent = btrfs_item_ptr(leaf, path->slots[0],
1038 struct btrfs_dev_extent);
1039 BUG_ON(found_key.offset > start || found_key.offset +
1040 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001041 key = found_key;
1042 btrfs_release_path(path);
1043 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001044 } else if (ret == 0) {
1045 leaf = path->nodes[0];
1046 extent = btrfs_item_ptr(leaf, path->slots[0],
1047 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001048 } else {
1049 btrfs_error(root->fs_info, ret, "Slot search failed");
1050 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001051 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001052
Josef Bacik2bf64752011-09-26 17:12:22 -04001053 if (device->bytes_used > 0) {
1054 u64 len = btrfs_dev_extent_length(leaf, extent);
1055 device->bytes_used -= len;
1056 spin_lock(&root->fs_info->free_chunk_lock);
1057 root->fs_info->free_chunk_space += len;
1058 spin_unlock(&root->fs_info->free_chunk_lock);
1059 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001060 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001061 if (ret) {
1062 btrfs_error(root->fs_info, ret,
1063 "Failed to remove dev extent item");
1064 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001065out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001066 btrfs_free_path(path);
1067 return ret;
1068}
1069
Yan Zheng2b820322008-11-17 21:11:30 -05001070int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001071 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001072 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001073 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001074{
1075 int ret;
1076 struct btrfs_path *path;
1077 struct btrfs_root *root = device->dev_root;
1078 struct btrfs_dev_extent *extent;
1079 struct extent_buffer *leaf;
1080 struct btrfs_key key;
1081
Chris Masondfe25022008-05-13 13:46:40 -04001082 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001083 path = btrfs_alloc_path();
1084 if (!path)
1085 return -ENOMEM;
1086
Chris Mason0b86a832008-03-24 15:01:56 -04001087 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001088 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001089 key.type = BTRFS_DEV_EXTENT_KEY;
1090 ret = btrfs_insert_empty_item(trans, root, path, &key,
1091 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001092 if (ret)
1093 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001094
1095 leaf = path->nodes[0];
1096 extent = btrfs_item_ptr(leaf, path->slots[0],
1097 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001098 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1099 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1100 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1101
1102 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1103 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1104 BTRFS_UUID_SIZE);
1105
Chris Mason0b86a832008-03-24 15:01:56 -04001106 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1107 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001108out:
Chris Mason0b86a832008-03-24 15:01:56 -04001109 btrfs_free_path(path);
1110 return ret;
1111}
1112
Chris Masona1b32a52008-09-05 16:09:51 -04001113static noinline int find_next_chunk(struct btrfs_root *root,
1114 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001115{
1116 struct btrfs_path *path;
1117 int ret;
1118 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001119 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001120 struct btrfs_key found_key;
1121
1122 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001123 if (!path)
1124 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001125
Chris Masone17cade2008-04-15 15:41:47 -04001126 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001127 key.offset = (u64)-1;
1128 key.type = BTRFS_CHUNK_ITEM_KEY;
1129
1130 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1131 if (ret < 0)
1132 goto error;
1133
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001134 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001135
1136 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1137 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001138 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001139 } else {
1140 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1141 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001142 if (found_key.objectid != objectid)
1143 *offset = 0;
1144 else {
1145 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1146 struct btrfs_chunk);
1147 *offset = found_key.offset +
1148 btrfs_chunk_length(path->nodes[0], chunk);
1149 }
Chris Mason0b86a832008-03-24 15:01:56 -04001150 }
1151 ret = 0;
1152error:
1153 btrfs_free_path(path);
1154 return ret;
1155}
1156
Yan Zheng2b820322008-11-17 21:11:30 -05001157static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001158{
1159 int ret;
1160 struct btrfs_key key;
1161 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001162 struct btrfs_path *path;
1163
1164 root = root->fs_info->chunk_root;
1165
1166 path = btrfs_alloc_path();
1167 if (!path)
1168 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001169
1170 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1171 key.type = BTRFS_DEV_ITEM_KEY;
1172 key.offset = (u64)-1;
1173
1174 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1175 if (ret < 0)
1176 goto error;
1177
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001178 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001179
1180 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1181 BTRFS_DEV_ITEM_KEY);
1182 if (ret) {
1183 *objectid = 1;
1184 } else {
1185 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1186 path->slots[0]);
1187 *objectid = found_key.offset + 1;
1188 }
1189 ret = 0;
1190error:
Yan Zheng2b820322008-11-17 21:11:30 -05001191 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001192 return ret;
1193}
1194
1195/*
1196 * the device information is stored in the chunk root
1197 * the btrfs_device struct should be fully filled in
1198 */
1199int btrfs_add_device(struct btrfs_trans_handle *trans,
1200 struct btrfs_root *root,
1201 struct btrfs_device *device)
1202{
1203 int ret;
1204 struct btrfs_path *path;
1205 struct btrfs_dev_item *dev_item;
1206 struct extent_buffer *leaf;
1207 struct btrfs_key key;
1208 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001209
1210 root = root->fs_info->chunk_root;
1211
1212 path = btrfs_alloc_path();
1213 if (!path)
1214 return -ENOMEM;
1215
Chris Mason0b86a832008-03-24 15:01:56 -04001216 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1217 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001218 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001219
1220 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001221 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001222 if (ret)
1223 goto out;
1224
1225 leaf = path->nodes[0];
1226 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1227
1228 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001229 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001230 btrfs_set_device_type(leaf, dev_item, device->type);
1231 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1232 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1233 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001234 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1235 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001236 btrfs_set_device_group(leaf, dev_item, 0);
1237 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1238 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001239 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001240
Chris Mason0b86a832008-03-24 15:01:56 -04001241 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001242 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001243 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1244 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001245 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001246
Yan Zheng2b820322008-11-17 21:11:30 -05001247 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001248out:
1249 btrfs_free_path(path);
1250 return ret;
1251}
Chris Mason8f18cf12008-04-25 16:53:30 -04001252
Chris Masona061fc82008-05-07 11:43:44 -04001253static int btrfs_rm_dev_item(struct btrfs_root *root,
1254 struct btrfs_device *device)
1255{
1256 int ret;
1257 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001258 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001259 struct btrfs_trans_handle *trans;
1260
1261 root = root->fs_info->chunk_root;
1262
1263 path = btrfs_alloc_path();
1264 if (!path)
1265 return -ENOMEM;
1266
Yan, Zhenga22285a2010-05-16 10:48:46 -04001267 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001268 if (IS_ERR(trans)) {
1269 btrfs_free_path(path);
1270 return PTR_ERR(trans);
1271 }
Chris Masona061fc82008-05-07 11:43:44 -04001272 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1273 key.type = BTRFS_DEV_ITEM_KEY;
1274 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001275 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001276
1277 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1278 if (ret < 0)
1279 goto out;
1280
1281 if (ret > 0) {
1282 ret = -ENOENT;
1283 goto out;
1284 }
1285
1286 ret = btrfs_del_item(trans, root, path);
1287 if (ret)
1288 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001289out:
1290 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001291 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001292 btrfs_commit_transaction(trans, root);
1293 return ret;
1294}
1295
1296int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1297{
1298 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001299 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001300 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001301 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001302 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001303 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001304 u64 all_avail;
1305 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001306 u64 num_devices;
1307 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001308 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001309 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001310
Chris Masona061fc82008-05-07 11:43:44 -04001311 mutex_lock(&uuid_mutex);
1312
1313 all_avail = root->fs_info->avail_data_alloc_bits |
1314 root->fs_info->avail_system_alloc_bits |
1315 root->fs_info->avail_metadata_alloc_bits;
1316
1317 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001318 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001319 printk(KERN_ERR "btrfs: unable to go below four devices "
1320 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001321 ret = -EINVAL;
1322 goto out;
1323 }
1324
1325 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001326 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001327 printk(KERN_ERR "btrfs: unable to go below two "
1328 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001329 ret = -EINVAL;
1330 goto out;
1331 }
1332
Chris Masondfe25022008-05-13 13:46:40 -04001333 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001334 struct list_head *devices;
1335 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001336
Chris Masondfe25022008-05-13 13:46:40 -04001337 device = NULL;
1338 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001339 /*
1340 * It is safe to read the devices since the volume_mutex
1341 * is held.
1342 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001343 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001344 if (tmp->in_fs_metadata && !tmp->bdev) {
1345 device = tmp;
1346 break;
1347 }
1348 }
1349 bdev = NULL;
1350 bh = NULL;
1351 disk_super = NULL;
1352 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001353 printk(KERN_ERR "btrfs: no missing devices found to "
1354 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001355 goto out;
1356 }
Chris Masondfe25022008-05-13 13:46:40 -04001357 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001358 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1359 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001360 if (IS_ERR(bdev)) {
1361 ret = PTR_ERR(bdev);
1362 goto out;
1363 }
1364
Yan Zheng2b820322008-11-17 21:11:30 -05001365 set_blocksize(bdev, 4096);
Chris Mason3c4bb262012-03-27 18:56:56 -04001366 invalidate_bdev(bdev);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001367 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001368 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001369 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001370 goto error_close;
1371 }
1372 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001373 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001374 dev_uuid = disk_super->dev_item.uuid;
1375 device = btrfs_find_device(root, devid, dev_uuid,
1376 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001377 if (!device) {
1378 ret = -ENOENT;
1379 goto error_brelse;
1380 }
Chris Masondfe25022008-05-13 13:46:40 -04001381 }
Yan Zheng2b820322008-11-17 21:11:30 -05001382
1383 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001384 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1385 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001386 ret = -EINVAL;
1387 goto error_brelse;
1388 }
1389
1390 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001391 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001392 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001393 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001394 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001395 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001396 }
Chris Masona061fc82008-05-07 11:43:44 -04001397
1398 ret = btrfs_shrink_device(device, 0);
1399 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001400 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001401
Chris Masona061fc82008-05-07 11:43:44 -04001402 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1403 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001404 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001405
Josef Bacik2bf64752011-09-26 17:12:22 -04001406 spin_lock(&root->fs_info->free_chunk_lock);
1407 root->fs_info->free_chunk_space = device->total_bytes -
1408 device->bytes_used;
1409 spin_unlock(&root->fs_info->free_chunk_lock);
1410
Yan Zheng2b820322008-11-17 21:11:30 -05001411 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001412 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001413
1414 /*
1415 * the device list mutex makes sure that we don't change
1416 * the device list while someone else is writing out all
1417 * the device supers.
1418 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001419
1420 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001421 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001422 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001423
Yan Zhenge4404d62008-12-12 10:03:26 -05001424 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001425
Chris Masoncd02dca2010-12-13 14:56:23 -05001426 if (device->missing)
1427 root->fs_info->fs_devices->missing_devices--;
1428
Yan Zheng2b820322008-11-17 21:11:30 -05001429 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1430 struct btrfs_device, dev_list);
1431 if (device->bdev == root->fs_info->sb->s_bdev)
1432 root->fs_info->sb->s_bdev = next_device->bdev;
1433 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1434 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1435
Xiao Guangrong1f781602011-04-20 10:09:16 +00001436 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001437 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001438
1439 call_rcu(&device->rcu, free_device);
1440 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001441
David Sterba6c417612011-04-13 15:41:04 +02001442 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1443 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001444
Xiao Guangrong1f781602011-04-20 10:09:16 +00001445 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001446 struct btrfs_fs_devices *fs_devices;
1447 fs_devices = root->fs_info->fs_devices;
1448 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001449 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001450 break;
1451 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001452 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001453 fs_devices->seed = cur_devices->seed;
1454 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001455 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001456 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001457 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001458 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001459 }
1460
1461 /*
1462 * at this point, the device is zero sized. We want to
1463 * remove it from the devices list and zero out the old super
1464 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001465 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001466 /* make sure this device isn't detected as part of
1467 * the FS anymore
1468 */
1469 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1470 set_buffer_dirty(bh);
1471 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001472 }
Chris Masona061fc82008-05-07 11:43:44 -04001473
Chris Masona061fc82008-05-07 11:43:44 -04001474 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001475
1476error_brelse:
1477 brelse(bh);
1478error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001479 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001480 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001481out:
1482 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001483 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001484error_undo:
1485 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001486 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001487 list_add(&device->dev_alloc_list,
1488 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001489 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001490 root->fs_info->fs_devices->rw_devices++;
1491 }
1492 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001493}
1494
Yan Zheng2b820322008-11-17 21:11:30 -05001495/*
1496 * does all the dirty work required for changing file system's UUID.
1497 */
Li Zefan125ccb02011-12-08 15:07:24 +08001498static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001499{
1500 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1501 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001502 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001503 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001504 struct btrfs_device *device;
1505 u64 super_flags;
1506
1507 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001508 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001509 return -EINVAL;
1510
Yan Zhenge4404d62008-12-12 10:03:26 -05001511 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1512 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001513 return -ENOMEM;
1514
Yan Zhenge4404d62008-12-12 10:03:26 -05001515 old_devices = clone_fs_devices(fs_devices);
1516 if (IS_ERR(old_devices)) {
1517 kfree(seed_devices);
1518 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001519 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001520
Yan Zheng2b820322008-11-17 21:11:30 -05001521 list_add(&old_devices->list, &fs_uuids);
1522
Yan Zhenge4404d62008-12-12 10:03:26 -05001523 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1524 seed_devices->opened = 1;
1525 INIT_LIST_HEAD(&seed_devices->devices);
1526 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001527 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001528
1529 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001530 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1531 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001532 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1533
Yan Zhenge4404d62008-12-12 10:03:26 -05001534 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1535 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1536 device->fs_devices = seed_devices;
1537 }
1538
Yan Zheng2b820322008-11-17 21:11:30 -05001539 fs_devices->seeding = 0;
1540 fs_devices->num_devices = 0;
1541 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001542 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001543
1544 generate_random_uuid(fs_devices->fsid);
1545 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1546 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1547 super_flags = btrfs_super_flags(disk_super) &
1548 ~BTRFS_SUPER_FLAG_SEEDING;
1549 btrfs_set_super_flags(disk_super, super_flags);
1550
1551 return 0;
1552}
1553
1554/*
1555 * strore the expected generation for seed devices in device items.
1556 */
1557static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1558 struct btrfs_root *root)
1559{
1560 struct btrfs_path *path;
1561 struct extent_buffer *leaf;
1562 struct btrfs_dev_item *dev_item;
1563 struct btrfs_device *device;
1564 struct btrfs_key key;
1565 u8 fs_uuid[BTRFS_UUID_SIZE];
1566 u8 dev_uuid[BTRFS_UUID_SIZE];
1567 u64 devid;
1568 int ret;
1569
1570 path = btrfs_alloc_path();
1571 if (!path)
1572 return -ENOMEM;
1573
1574 root = root->fs_info->chunk_root;
1575 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1576 key.offset = 0;
1577 key.type = BTRFS_DEV_ITEM_KEY;
1578
1579 while (1) {
1580 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1581 if (ret < 0)
1582 goto error;
1583
1584 leaf = path->nodes[0];
1585next_slot:
1586 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1587 ret = btrfs_next_leaf(root, path);
1588 if (ret > 0)
1589 break;
1590 if (ret < 0)
1591 goto error;
1592 leaf = path->nodes[0];
1593 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001594 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001595 continue;
1596 }
1597
1598 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1599 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1600 key.type != BTRFS_DEV_ITEM_KEY)
1601 break;
1602
1603 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1604 struct btrfs_dev_item);
1605 devid = btrfs_device_id(leaf, dev_item);
1606 read_extent_buffer(leaf, dev_uuid,
1607 (unsigned long)btrfs_device_uuid(dev_item),
1608 BTRFS_UUID_SIZE);
1609 read_extent_buffer(leaf, fs_uuid,
1610 (unsigned long)btrfs_device_fsid(dev_item),
1611 BTRFS_UUID_SIZE);
1612 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001613 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001614
1615 if (device->fs_devices->seeding) {
1616 btrfs_set_device_generation(leaf, dev_item,
1617 device->generation);
1618 btrfs_mark_buffer_dirty(leaf);
1619 }
1620
1621 path->slots[0]++;
1622 goto next_slot;
1623 }
1624 ret = 0;
1625error:
1626 btrfs_free_path(path);
1627 return ret;
1628}
1629
Chris Mason788f20e2008-04-28 15:29:42 -04001630int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1631{
Josef Bacikd5e20032011-08-04 14:52:27 +00001632 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001633 struct btrfs_trans_handle *trans;
1634 struct btrfs_device *device;
1635 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001636 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001637 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001638 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001639 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001640 int ret = 0;
1641
Yan Zheng2b820322008-11-17 21:11:30 -05001642 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1643 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001644
Li Zefana5d16332011-12-07 20:08:40 -05001645 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001646 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001647 if (IS_ERR(bdev))
1648 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001649
Yan Zheng2b820322008-11-17 21:11:30 -05001650 if (root->fs_info->fs_devices->seeding) {
1651 seeding_dev = 1;
1652 down_write(&sb->s_umount);
1653 mutex_lock(&uuid_mutex);
1654 }
1655
Chris Mason8c8bee12008-09-29 11:19:10 -04001656 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001657
Chris Mason788f20e2008-04-28 15:29:42 -04001658 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001659 /*
1660 * we have the volume lock, so we don't need the extra
1661 * device list mutex while reading the list here.
1662 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001663 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001664 if (device->bdev == bdev) {
1665 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001666 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001667 }
1668 }
1669
1670 device = kzalloc(sizeof(*device), GFP_NOFS);
1671 if (!device) {
1672 /* we can safely leave the fs_devices entry around */
1673 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001674 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001675 }
1676
Chris Mason788f20e2008-04-28 15:29:42 -04001677 device->name = kstrdup(device_path, GFP_NOFS);
1678 if (!device->name) {
1679 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001680 ret = -ENOMEM;
1681 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001682 }
Yan Zheng2b820322008-11-17 21:11:30 -05001683
1684 ret = find_next_devid(root, &device->devid);
1685 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001686 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001687 kfree(device);
1688 goto error;
1689 }
1690
Yan, Zhenga22285a2010-05-16 10:48:46 -04001691 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001692 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001693 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001694 kfree(device);
1695 ret = PTR_ERR(trans);
1696 goto error;
1697 }
1698
Yan Zheng2b820322008-11-17 21:11:30 -05001699 lock_chunks(root);
1700
Josef Bacikd5e20032011-08-04 14:52:27 +00001701 q = bdev_get_queue(bdev);
1702 if (blk_queue_discard(q))
1703 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001704 device->writeable = 1;
1705 device->work.func = pending_bios_fn;
1706 generate_random_uuid(device->uuid);
1707 spin_lock_init(&device->io_lock);
1708 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001709 device->io_width = root->sectorsize;
1710 device->io_align = root->sectorsize;
1711 device->sector_size = root->sectorsize;
1712 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001713 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001714 device->dev_root = root->fs_info->dev_root;
1715 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001716 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001717 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001718 set_blocksize(device->bdev, 4096);
1719
Yan Zheng2b820322008-11-17 21:11:30 -05001720 if (seeding_dev) {
1721 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001722 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001723 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001724 }
1725
1726 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001727
1728 /*
1729 * we don't want write_supers to jump in here with our device
1730 * half setup
1731 */
1732 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001733 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001734 list_add(&device->dev_alloc_list,
1735 &root->fs_info->fs_devices->alloc_list);
1736 root->fs_info->fs_devices->num_devices++;
1737 root->fs_info->fs_devices->open_devices++;
1738 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001739 if (device->can_discard)
1740 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001741 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1742
Josef Bacik2bf64752011-09-26 17:12:22 -04001743 spin_lock(&root->fs_info->free_chunk_lock);
1744 root->fs_info->free_chunk_space += device->total_bytes;
1745 spin_unlock(&root->fs_info->free_chunk_lock);
1746
Chris Masonc2898112009-06-10 09:51:32 -04001747 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1748 root->fs_info->fs_devices->rotating = 1;
1749
David Sterba6c417612011-04-13 15:41:04 +02001750 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1751 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001752 total_bytes + device->total_bytes);
1753
David Sterba6c417612011-04-13 15:41:04 +02001754 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1755 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001756 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001757 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001758
Yan Zheng2b820322008-11-17 21:11:30 -05001759 if (seeding_dev) {
1760 ret = init_first_rw_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001761 if (ret)
1762 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001763 ret = btrfs_finish_sprout(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001764 if (ret)
1765 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001766 } else {
1767 ret = btrfs_add_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001768 if (ret)
1769 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001770 }
1771
Chris Mason913d9522009-03-10 13:17:18 -04001772 /*
1773 * we've got more storage, clear any full flags on the space
1774 * infos
1775 */
1776 btrfs_clear_space_info_full(root->fs_info);
1777
Chris Mason7d9eb122008-07-08 14:19:17 -04001778 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001779 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001780
1781 if (seeding_dev) {
1782 mutex_unlock(&uuid_mutex);
1783 up_write(&sb->s_umount);
1784
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001785 if (ret) /* transaction commit */
1786 return ret;
1787
Yan Zheng2b820322008-11-17 21:11:30 -05001788 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001789 if (ret < 0)
1790 btrfs_error(root->fs_info, ret,
1791 "Failed to relocate sys chunks after "
1792 "device initialization. This can be fixed "
1793 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001794 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001795
Chris Mason788f20e2008-04-28 15:29:42 -04001796 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001797
1798error_trans:
1799 unlock_chunks(root);
1800 btrfs_abort_transaction(trans, root, ret);
1801 btrfs_end_transaction(trans, root);
1802 kfree(device->name);
1803 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001804error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001805 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001806 if (seeding_dev) {
1807 mutex_unlock(&uuid_mutex);
1808 up_write(&sb->s_umount);
1809 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001810 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001811}
1812
Chris Masond3977122009-01-05 21:25:51 -05001813static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1814 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001815{
1816 int ret;
1817 struct btrfs_path *path;
1818 struct btrfs_root *root;
1819 struct btrfs_dev_item *dev_item;
1820 struct extent_buffer *leaf;
1821 struct btrfs_key key;
1822
1823 root = device->dev_root->fs_info->chunk_root;
1824
1825 path = btrfs_alloc_path();
1826 if (!path)
1827 return -ENOMEM;
1828
1829 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1830 key.type = BTRFS_DEV_ITEM_KEY;
1831 key.offset = device->devid;
1832
1833 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1834 if (ret < 0)
1835 goto out;
1836
1837 if (ret > 0) {
1838 ret = -ENOENT;
1839 goto out;
1840 }
1841
1842 leaf = path->nodes[0];
1843 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1844
1845 btrfs_set_device_id(leaf, dev_item, device->devid);
1846 btrfs_set_device_type(leaf, dev_item, device->type);
1847 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1848 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1849 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001850 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001851 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1852 btrfs_mark_buffer_dirty(leaf);
1853
1854out:
1855 btrfs_free_path(path);
1856 return ret;
1857}
1858
Chris Mason7d9eb122008-07-08 14:19:17 -04001859static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001860 struct btrfs_device *device, u64 new_size)
1861{
1862 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001863 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001864 u64 old_total = btrfs_super_total_bytes(super_copy);
1865 u64 diff = new_size - device->total_bytes;
1866
Yan Zheng2b820322008-11-17 21:11:30 -05001867 if (!device->writeable)
1868 return -EACCES;
1869 if (new_size <= device->total_bytes)
1870 return -EINVAL;
1871
Chris Mason8f18cf12008-04-25 16:53:30 -04001872 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001873 device->fs_devices->total_rw_bytes += diff;
1874
1875 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001876 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001877 btrfs_clear_space_info_full(device->dev_root->fs_info);
1878
Chris Mason8f18cf12008-04-25 16:53:30 -04001879 return btrfs_update_device(trans, device);
1880}
1881
Chris Mason7d9eb122008-07-08 14:19:17 -04001882int btrfs_grow_device(struct btrfs_trans_handle *trans,
1883 struct btrfs_device *device, u64 new_size)
1884{
1885 int ret;
1886 lock_chunks(device->dev_root);
1887 ret = __btrfs_grow_device(trans, device, new_size);
1888 unlock_chunks(device->dev_root);
1889 return ret;
1890}
1891
Chris Mason8f18cf12008-04-25 16:53:30 -04001892static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1893 struct btrfs_root *root,
1894 u64 chunk_tree, u64 chunk_objectid,
1895 u64 chunk_offset)
1896{
1897 int ret;
1898 struct btrfs_path *path;
1899 struct btrfs_key key;
1900
1901 root = root->fs_info->chunk_root;
1902 path = btrfs_alloc_path();
1903 if (!path)
1904 return -ENOMEM;
1905
1906 key.objectid = chunk_objectid;
1907 key.offset = chunk_offset;
1908 key.type = BTRFS_CHUNK_ITEM_KEY;
1909
1910 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001911 if (ret < 0)
1912 goto out;
1913 else if (ret > 0) { /* Logic error or corruption */
1914 btrfs_error(root->fs_info, -ENOENT,
1915 "Failed lookup while freeing chunk.");
1916 ret = -ENOENT;
1917 goto out;
1918 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001919
1920 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001921 if (ret < 0)
1922 btrfs_error(root->fs_info, ret,
1923 "Failed to delete chunk item.");
1924out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001925 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001926 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001927}
1928
Christoph Hellwigb2950862008-12-02 09:54:17 -05001929static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001930 chunk_offset)
1931{
David Sterba6c417612011-04-13 15:41:04 +02001932 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001933 struct btrfs_disk_key *disk_key;
1934 struct btrfs_chunk *chunk;
1935 u8 *ptr;
1936 int ret = 0;
1937 u32 num_stripes;
1938 u32 array_size;
1939 u32 len = 0;
1940 u32 cur;
1941 struct btrfs_key key;
1942
1943 array_size = btrfs_super_sys_array_size(super_copy);
1944
1945 ptr = super_copy->sys_chunk_array;
1946 cur = 0;
1947
1948 while (cur < array_size) {
1949 disk_key = (struct btrfs_disk_key *)ptr;
1950 btrfs_disk_key_to_cpu(&key, disk_key);
1951
1952 len = sizeof(*disk_key);
1953
1954 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1955 chunk = (struct btrfs_chunk *)(ptr + len);
1956 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1957 len += btrfs_chunk_item_size(num_stripes);
1958 } else {
1959 ret = -EIO;
1960 break;
1961 }
1962 if (key.objectid == chunk_objectid &&
1963 key.offset == chunk_offset) {
1964 memmove(ptr, ptr + len, array_size - (cur + len));
1965 array_size -= len;
1966 btrfs_set_super_sys_array_size(super_copy, array_size);
1967 } else {
1968 ptr += len;
1969 cur += len;
1970 }
1971 }
1972 return ret;
1973}
1974
Christoph Hellwigb2950862008-12-02 09:54:17 -05001975static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001976 u64 chunk_tree, u64 chunk_objectid,
1977 u64 chunk_offset)
1978{
1979 struct extent_map_tree *em_tree;
1980 struct btrfs_root *extent_root;
1981 struct btrfs_trans_handle *trans;
1982 struct extent_map *em;
1983 struct map_lookup *map;
1984 int ret;
1985 int i;
1986
1987 root = root->fs_info->chunk_root;
1988 extent_root = root->fs_info->extent_root;
1989 em_tree = &root->fs_info->mapping_tree.map_tree;
1990
Josef Bacikba1bf482009-09-11 16:11:19 -04001991 ret = btrfs_can_relocate(extent_root, chunk_offset);
1992 if (ret)
1993 return -ENOSPC;
1994
Chris Mason8f18cf12008-04-25 16:53:30 -04001995 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001996 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001997 if (ret)
1998 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001999
Yan, Zhenga22285a2010-05-16 10:48:46 -04002000 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002001 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002002
Chris Mason7d9eb122008-07-08 14:19:17 -04002003 lock_chunks(root);
2004
Chris Mason8f18cf12008-04-25 16:53:30 -04002005 /*
2006 * step two, delete the device extents and the
2007 * chunk tree entries
2008 */
Chris Mason890871b2009-09-02 16:24:52 -04002009 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002010 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002011 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002012
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002013 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002014 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002015 map = (struct map_lookup *)em->bdev;
2016
2017 for (i = 0; i < map->num_stripes; i++) {
2018 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2019 map->stripes[i].physical);
2020 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002021
Chris Masondfe25022008-05-13 13:46:40 -04002022 if (map->stripes[i].dev) {
2023 ret = btrfs_update_device(trans, map->stripes[i].dev);
2024 BUG_ON(ret);
2025 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002026 }
2027 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2028 chunk_offset);
2029
2030 BUG_ON(ret);
2031
liubo1abe9b82011-03-24 11:18:59 +00002032 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2033
Chris Mason8f18cf12008-04-25 16:53:30 -04002034 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2035 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2036 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002037 }
2038
Zheng Yan1a40e232008-09-26 10:09:34 -04002039 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2040 BUG_ON(ret);
2041
Chris Mason890871b2009-09-02 16:24:52 -04002042 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002043 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002044 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002045
Chris Mason8f18cf12008-04-25 16:53:30 -04002046 kfree(map);
2047 em->bdev = NULL;
2048
2049 /* once for the tree */
2050 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002051 /* once for us */
2052 free_extent_map(em);
2053
Chris Mason7d9eb122008-07-08 14:19:17 -04002054 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002055 btrfs_end_transaction(trans, root);
2056 return 0;
2057}
2058
Yan Zheng2b820322008-11-17 21:11:30 -05002059static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2060{
2061 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2062 struct btrfs_path *path;
2063 struct extent_buffer *leaf;
2064 struct btrfs_chunk *chunk;
2065 struct btrfs_key key;
2066 struct btrfs_key found_key;
2067 u64 chunk_tree = chunk_root->root_key.objectid;
2068 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002069 bool retried = false;
2070 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002071 int ret;
2072
2073 path = btrfs_alloc_path();
2074 if (!path)
2075 return -ENOMEM;
2076
Josef Bacikba1bf482009-09-11 16:11:19 -04002077again:
Yan Zheng2b820322008-11-17 21:11:30 -05002078 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2079 key.offset = (u64)-1;
2080 key.type = BTRFS_CHUNK_ITEM_KEY;
2081
2082 while (1) {
2083 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2084 if (ret < 0)
2085 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002086 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002087
2088 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2089 key.type);
2090 if (ret < 0)
2091 goto error;
2092 if (ret > 0)
2093 break;
2094
2095 leaf = path->nodes[0];
2096 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2097
2098 chunk = btrfs_item_ptr(leaf, path->slots[0],
2099 struct btrfs_chunk);
2100 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002101 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002102
2103 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2104 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2105 found_key.objectid,
2106 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002107 if (ret == -ENOSPC)
2108 failed++;
2109 else if (ret)
2110 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002111 }
2112
2113 if (found_key.offset == 0)
2114 break;
2115 key.offset = found_key.offset - 1;
2116 }
2117 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002118 if (failed && !retried) {
2119 failed = 0;
2120 retried = true;
2121 goto again;
2122 } else if (failed && retried) {
2123 WARN_ON(1);
2124 ret = -ENOSPC;
2125 }
Yan Zheng2b820322008-11-17 21:11:30 -05002126error:
2127 btrfs_free_path(path);
2128 return ret;
2129}
2130
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002131static int insert_balance_item(struct btrfs_root *root,
2132 struct btrfs_balance_control *bctl)
2133{
2134 struct btrfs_trans_handle *trans;
2135 struct btrfs_balance_item *item;
2136 struct btrfs_disk_balance_args disk_bargs;
2137 struct btrfs_path *path;
2138 struct extent_buffer *leaf;
2139 struct btrfs_key key;
2140 int ret, err;
2141
2142 path = btrfs_alloc_path();
2143 if (!path)
2144 return -ENOMEM;
2145
2146 trans = btrfs_start_transaction(root, 0);
2147 if (IS_ERR(trans)) {
2148 btrfs_free_path(path);
2149 return PTR_ERR(trans);
2150 }
2151
2152 key.objectid = BTRFS_BALANCE_OBJECTID;
2153 key.type = BTRFS_BALANCE_ITEM_KEY;
2154 key.offset = 0;
2155
2156 ret = btrfs_insert_empty_item(trans, root, path, &key,
2157 sizeof(*item));
2158 if (ret)
2159 goto out;
2160
2161 leaf = path->nodes[0];
2162 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2163
2164 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2165
2166 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2167 btrfs_set_balance_data(leaf, item, &disk_bargs);
2168 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2169 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2170 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2171 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2172
2173 btrfs_set_balance_flags(leaf, item, bctl->flags);
2174
2175 btrfs_mark_buffer_dirty(leaf);
2176out:
2177 btrfs_free_path(path);
2178 err = btrfs_commit_transaction(trans, root);
2179 if (err && !ret)
2180 ret = err;
2181 return ret;
2182}
2183
2184static int del_balance_item(struct btrfs_root *root)
2185{
2186 struct btrfs_trans_handle *trans;
2187 struct btrfs_path *path;
2188 struct btrfs_key key;
2189 int ret, err;
2190
2191 path = btrfs_alloc_path();
2192 if (!path)
2193 return -ENOMEM;
2194
2195 trans = btrfs_start_transaction(root, 0);
2196 if (IS_ERR(trans)) {
2197 btrfs_free_path(path);
2198 return PTR_ERR(trans);
2199 }
2200
2201 key.objectid = BTRFS_BALANCE_OBJECTID;
2202 key.type = BTRFS_BALANCE_ITEM_KEY;
2203 key.offset = 0;
2204
2205 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2206 if (ret < 0)
2207 goto out;
2208 if (ret > 0) {
2209 ret = -ENOENT;
2210 goto out;
2211 }
2212
2213 ret = btrfs_del_item(trans, root, path);
2214out:
2215 btrfs_free_path(path);
2216 err = btrfs_commit_transaction(trans, root);
2217 if (err && !ret)
2218 ret = err;
2219 return ret;
2220}
2221
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002222/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002223 * This is a heuristic used to reduce the number of chunks balanced on
2224 * resume after balance was interrupted.
2225 */
2226static void update_balance_args(struct btrfs_balance_control *bctl)
2227{
2228 /*
2229 * Turn on soft mode for chunk types that were being converted.
2230 */
2231 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2232 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2233 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2234 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2235 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2236 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2237
2238 /*
2239 * Turn on usage filter if is not already used. The idea is
2240 * that chunks that we have already balanced should be
2241 * reasonably full. Don't do it for chunks that are being
2242 * converted - that will keep us from relocating unconverted
2243 * (albeit full) chunks.
2244 */
2245 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2246 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2247 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2248 bctl->data.usage = 90;
2249 }
2250 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2251 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2252 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2253 bctl->sys.usage = 90;
2254 }
2255 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2256 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2257 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2258 bctl->meta.usage = 90;
2259 }
2260}
2261
2262/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002263 * Should be called with both balance and volume mutexes held to
2264 * serialize other volume operations (add_dev/rm_dev/resize) with
2265 * restriper. Same goes for unset_balance_control.
2266 */
2267static void set_balance_control(struct btrfs_balance_control *bctl)
2268{
2269 struct btrfs_fs_info *fs_info = bctl->fs_info;
2270
2271 BUG_ON(fs_info->balance_ctl);
2272
2273 spin_lock(&fs_info->balance_lock);
2274 fs_info->balance_ctl = bctl;
2275 spin_unlock(&fs_info->balance_lock);
2276}
2277
2278static void unset_balance_control(struct btrfs_fs_info *fs_info)
2279{
2280 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2281
2282 BUG_ON(!fs_info->balance_ctl);
2283
2284 spin_lock(&fs_info->balance_lock);
2285 fs_info->balance_ctl = NULL;
2286 spin_unlock(&fs_info->balance_lock);
2287
2288 kfree(bctl);
2289}
2290
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002291/*
2292 * Balance filters. Return 1 if chunk should be filtered out
2293 * (should not be balanced).
2294 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002295static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002296 struct btrfs_balance_args *bargs)
2297{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002298 chunk_type = chunk_to_extended(chunk_type) &
2299 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002300
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002301 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002302 return 0;
2303
2304 return 1;
2305}
2306
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002307static u64 div_factor_fine(u64 num, int factor)
2308{
2309 if (factor <= 0)
2310 return 0;
2311 if (factor >= 100)
2312 return num;
2313
2314 num *= factor;
2315 do_div(num, 100);
2316 return num;
2317}
2318
2319static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2320 struct btrfs_balance_args *bargs)
2321{
2322 struct btrfs_block_group_cache *cache;
2323 u64 chunk_used, user_thresh;
2324 int ret = 1;
2325
2326 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2327 chunk_used = btrfs_block_group_used(&cache->item);
2328
2329 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2330 if (chunk_used < user_thresh)
2331 ret = 0;
2332
2333 btrfs_put_block_group(cache);
2334 return ret;
2335}
2336
Ilya Dryomov409d4042012-01-16 22:04:47 +02002337static int chunk_devid_filter(struct extent_buffer *leaf,
2338 struct btrfs_chunk *chunk,
2339 struct btrfs_balance_args *bargs)
2340{
2341 struct btrfs_stripe *stripe;
2342 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2343 int i;
2344
2345 for (i = 0; i < num_stripes; i++) {
2346 stripe = btrfs_stripe_nr(chunk, i);
2347 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2348 return 0;
2349 }
2350
2351 return 1;
2352}
2353
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002354/* [pstart, pend) */
2355static int chunk_drange_filter(struct extent_buffer *leaf,
2356 struct btrfs_chunk *chunk,
2357 u64 chunk_offset,
2358 struct btrfs_balance_args *bargs)
2359{
2360 struct btrfs_stripe *stripe;
2361 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2362 u64 stripe_offset;
2363 u64 stripe_length;
2364 int factor;
2365 int i;
2366
2367 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2368 return 0;
2369
2370 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2371 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2372 factor = 2;
2373 else
2374 factor = 1;
2375 factor = num_stripes / factor;
2376
2377 for (i = 0; i < num_stripes; i++) {
2378 stripe = btrfs_stripe_nr(chunk, i);
2379 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2380 continue;
2381
2382 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2383 stripe_length = btrfs_chunk_length(leaf, chunk);
2384 do_div(stripe_length, factor);
2385
2386 if (stripe_offset < bargs->pend &&
2387 stripe_offset + stripe_length > bargs->pstart)
2388 return 0;
2389 }
2390
2391 return 1;
2392}
2393
Ilya Dryomovea671762012-01-16 22:04:48 +02002394/* [vstart, vend) */
2395static int chunk_vrange_filter(struct extent_buffer *leaf,
2396 struct btrfs_chunk *chunk,
2397 u64 chunk_offset,
2398 struct btrfs_balance_args *bargs)
2399{
2400 if (chunk_offset < bargs->vend &&
2401 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2402 /* at least part of the chunk is inside this vrange */
2403 return 0;
2404
2405 return 1;
2406}
2407
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002408static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002409 struct btrfs_balance_args *bargs)
2410{
2411 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2412 return 0;
2413
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002414 chunk_type = chunk_to_extended(chunk_type) &
2415 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002416
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002417 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002418 return 1;
2419
2420 return 0;
2421}
2422
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002423static int should_balance_chunk(struct btrfs_root *root,
2424 struct extent_buffer *leaf,
2425 struct btrfs_chunk *chunk, u64 chunk_offset)
2426{
2427 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2428 struct btrfs_balance_args *bargs = NULL;
2429 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2430
2431 /* type filter */
2432 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2433 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2434 return 0;
2435 }
2436
2437 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2438 bargs = &bctl->data;
2439 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2440 bargs = &bctl->sys;
2441 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2442 bargs = &bctl->meta;
2443
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002444 /* profiles filter */
2445 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2446 chunk_profiles_filter(chunk_type, bargs)) {
2447 return 0;
2448 }
2449
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002450 /* usage filter */
2451 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2452 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2453 return 0;
2454 }
2455
Ilya Dryomov409d4042012-01-16 22:04:47 +02002456 /* devid filter */
2457 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2458 chunk_devid_filter(leaf, chunk, bargs)) {
2459 return 0;
2460 }
2461
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002462 /* drange filter, makes sense only with devid filter */
2463 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2464 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2465 return 0;
2466 }
2467
Ilya Dryomovea671762012-01-16 22:04:48 +02002468 /* vrange filter */
2469 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2470 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2471 return 0;
2472 }
2473
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002474 /* soft profile changing mode */
2475 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2476 chunk_soft_convert_filter(chunk_type, bargs)) {
2477 return 0;
2478 }
2479
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002480 return 1;
2481}
2482
Chris Masonec44a352008-04-28 15:29:52 -04002483static u64 div_factor(u64 num, int factor)
2484{
2485 if (factor == 10)
2486 return num;
2487 num *= factor;
2488 do_div(num, 10);
2489 return num;
2490}
2491
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002492static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002493{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002494 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002495 struct btrfs_root *chunk_root = fs_info->chunk_root;
2496 struct btrfs_root *dev_root = fs_info->dev_root;
2497 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002498 struct btrfs_device *device;
2499 u64 old_size;
2500 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002501 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002502 struct btrfs_path *path;
2503 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002504 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002505 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002506 struct extent_buffer *leaf;
2507 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002508 int ret;
2509 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002510 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002511
Chris Masonec44a352008-04-28 15:29:52 -04002512 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002513 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002514 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002515 old_size = device->total_bytes;
2516 size_to_free = div_factor(old_size, 1);
2517 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002518 if (!device->writeable ||
2519 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002520 continue;
2521
2522 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002523 if (ret == -ENOSPC)
2524 break;
Chris Masonec44a352008-04-28 15:29:52 -04002525 BUG_ON(ret);
2526
Yan, Zhenga22285a2010-05-16 10:48:46 -04002527 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002528 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002529
2530 ret = btrfs_grow_device(trans, device, old_size);
2531 BUG_ON(ret);
2532
2533 btrfs_end_transaction(trans, dev_root);
2534 }
2535
2536 /* step two, relocate all the chunks */
2537 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002538 if (!path) {
2539 ret = -ENOMEM;
2540 goto error;
2541 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002542
2543 /* zero out stat counters */
2544 spin_lock(&fs_info->balance_lock);
2545 memset(&bctl->stat, 0, sizeof(bctl->stat));
2546 spin_unlock(&fs_info->balance_lock);
2547again:
Chris Masonec44a352008-04-28 15:29:52 -04002548 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2549 key.offset = (u64)-1;
2550 key.type = BTRFS_CHUNK_ITEM_KEY;
2551
Chris Masond3977122009-01-05 21:25:51 -05002552 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002553 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002554 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002555 ret = -ECANCELED;
2556 goto error;
2557 }
2558
Chris Masonec44a352008-04-28 15:29:52 -04002559 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2560 if (ret < 0)
2561 goto error;
2562
2563 /*
2564 * this shouldn't happen, it means the last relocate
2565 * failed
2566 */
2567 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002568 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002569
2570 ret = btrfs_previous_item(chunk_root, path, 0,
2571 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002572 if (ret) {
2573 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002574 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002575 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002576
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002577 leaf = path->nodes[0];
2578 slot = path->slots[0];
2579 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2580
Chris Masonec44a352008-04-28 15:29:52 -04002581 if (found_key.objectid != key.objectid)
2582 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002583
Chris Masonec44a352008-04-28 15:29:52 -04002584 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002585 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002586 break;
2587
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002588 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2589
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002590 if (!counting) {
2591 spin_lock(&fs_info->balance_lock);
2592 bctl->stat.considered++;
2593 spin_unlock(&fs_info->balance_lock);
2594 }
2595
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002596 ret = should_balance_chunk(chunk_root, leaf, chunk,
2597 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002598 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002599 if (!ret)
2600 goto loop;
2601
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002602 if (counting) {
2603 spin_lock(&fs_info->balance_lock);
2604 bctl->stat.expected++;
2605 spin_unlock(&fs_info->balance_lock);
2606 goto loop;
2607 }
2608
Chris Masonec44a352008-04-28 15:29:52 -04002609 ret = btrfs_relocate_chunk(chunk_root,
2610 chunk_root->root_key.objectid,
2611 found_key.objectid,
2612 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002613 if (ret && ret != -ENOSPC)
2614 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002615 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002616 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002617 } else {
2618 spin_lock(&fs_info->balance_lock);
2619 bctl->stat.completed++;
2620 spin_unlock(&fs_info->balance_lock);
2621 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002622loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002623 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002624 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002625
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002626 if (counting) {
2627 btrfs_release_path(path);
2628 counting = false;
2629 goto again;
2630 }
Chris Masonec44a352008-04-28 15:29:52 -04002631error:
2632 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002633 if (enospc_errors) {
2634 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2635 enospc_errors);
2636 if (!ret)
2637 ret = -ENOSPC;
2638 }
2639
Chris Masonec44a352008-04-28 15:29:52 -04002640 return ret;
2641}
2642
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002643/**
2644 * alloc_profile_is_valid - see if a given profile is valid and reduced
2645 * @flags: profile to validate
2646 * @extended: if true @flags is treated as an extended profile
2647 */
2648static int alloc_profile_is_valid(u64 flags, int extended)
2649{
2650 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2651 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2652
2653 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2654
2655 /* 1) check that all other bits are zeroed */
2656 if (flags & ~mask)
2657 return 0;
2658
2659 /* 2) see if profile is reduced */
2660 if (flags == 0)
2661 return !extended; /* "0" is valid for usual profiles */
2662
2663 /* true if exactly one bit set */
2664 return (flags & (flags - 1)) == 0;
2665}
2666
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002667static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2668{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002669 /* cancel requested || normal exit path */
2670 return atomic_read(&fs_info->balance_cancel_req) ||
2671 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2672 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002673}
2674
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002675static void __cancel_balance(struct btrfs_fs_info *fs_info)
2676{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002677 int ret;
2678
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002679 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002680 ret = del_balance_item(fs_info->tree_root);
2681 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002682}
2683
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002684void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002685 struct btrfs_ioctl_balance_args *bargs);
2686
2687/*
2688 * Should be called with both balance and volume mutexes held
2689 */
2690int btrfs_balance(struct btrfs_balance_control *bctl,
2691 struct btrfs_ioctl_balance_args *bargs)
2692{
2693 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002694 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002695 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002696 int ret;
2697
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002698 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002699 atomic_read(&fs_info->balance_pause_req) ||
2700 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002701 ret = -EINVAL;
2702 goto out;
2703 }
2704
Ilya Dryomove4837f82012-03-27 17:09:17 +03002705 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2706 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2707 mixed = 1;
2708
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002709 /*
2710 * In case of mixed groups both data and meta should be picked,
2711 * and identical options should be given for both of them.
2712 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002713 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2714 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002715 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2716 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2717 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2718 printk(KERN_ERR "btrfs: with mixed groups data and "
2719 "metadata balance options must be the same\n");
2720 ret = -EINVAL;
2721 goto out;
2722 }
2723 }
2724
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002725 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2726 if (fs_info->fs_devices->num_devices == 1)
2727 allowed |= BTRFS_BLOCK_GROUP_DUP;
2728 else if (fs_info->fs_devices->num_devices < 4)
2729 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2730 else
2731 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2732 BTRFS_BLOCK_GROUP_RAID10);
2733
Ilya Dryomov6728b192012-03-27 17:09:17 +03002734 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2735 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2736 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002737 printk(KERN_ERR "btrfs: unable to start balance with target "
2738 "data profile %llu\n",
2739 (unsigned long long)bctl->data.target);
2740 ret = -EINVAL;
2741 goto out;
2742 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002743 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2744 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2745 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002746 printk(KERN_ERR "btrfs: unable to start balance with target "
2747 "metadata profile %llu\n",
2748 (unsigned long long)bctl->meta.target);
2749 ret = -EINVAL;
2750 goto out;
2751 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002752 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2753 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2754 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002755 printk(KERN_ERR "btrfs: unable to start balance with target "
2756 "system profile %llu\n",
2757 (unsigned long long)bctl->sys.target);
2758 ret = -EINVAL;
2759 goto out;
2760 }
2761
Ilya Dryomove4837f82012-03-27 17:09:17 +03002762 /* allow dup'ed data chunks only in mixed mode */
2763 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002764 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002765 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2766 ret = -EINVAL;
2767 goto out;
2768 }
2769
2770 /* allow to reduce meta or sys integrity only if force set */
2771 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2772 BTRFS_BLOCK_GROUP_RAID10;
2773 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2774 (fs_info->avail_system_alloc_bits & allowed) &&
2775 !(bctl->sys.target & allowed)) ||
2776 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2777 (fs_info->avail_metadata_alloc_bits & allowed) &&
2778 !(bctl->meta.target & allowed))) {
2779 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2780 printk(KERN_INFO "btrfs: force reducing metadata "
2781 "integrity\n");
2782 } else {
2783 printk(KERN_ERR "btrfs: balance will reduce metadata "
2784 "integrity, use force if you want this\n");
2785 ret = -EINVAL;
2786 goto out;
2787 }
2788 }
2789
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002790 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002791 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002792 goto out;
2793
Ilya Dryomov59641012012-01-16 22:04:48 +02002794 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2795 BUG_ON(ret == -EEXIST);
2796 set_balance_control(bctl);
2797 } else {
2798 BUG_ON(ret != -EEXIST);
2799 spin_lock(&fs_info->balance_lock);
2800 update_balance_args(bctl);
2801 spin_unlock(&fs_info->balance_lock);
2802 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002803
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002804 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002805 mutex_unlock(&fs_info->balance_mutex);
2806
2807 ret = __btrfs_balance(fs_info);
2808
2809 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002810 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002811
2812 if (bargs) {
2813 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002814 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002815 }
2816
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002817 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2818 balance_need_close(fs_info)) {
2819 __cancel_balance(fs_info);
2820 }
2821
2822 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002823
2824 return ret;
2825out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002826 if (bctl->flags & BTRFS_BALANCE_RESUME)
2827 __cancel_balance(fs_info);
2828 else
2829 kfree(bctl);
2830 return ret;
2831}
2832
2833static int balance_kthread(void *data)
2834{
2835 struct btrfs_balance_control *bctl =
2836 (struct btrfs_balance_control *)data;
2837 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002838 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002839
2840 mutex_lock(&fs_info->volume_mutex);
2841 mutex_lock(&fs_info->balance_mutex);
2842
2843 set_balance_control(bctl);
2844
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002845 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2846 printk(KERN_INFO "btrfs: force skipping balance\n");
2847 } else {
2848 printk(KERN_INFO "btrfs: continuing balance\n");
2849 ret = btrfs_balance(bctl, NULL);
2850 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002851
2852 mutex_unlock(&fs_info->balance_mutex);
2853 mutex_unlock(&fs_info->volume_mutex);
2854 return ret;
2855}
2856
2857int btrfs_recover_balance(struct btrfs_root *tree_root)
2858{
2859 struct task_struct *tsk;
2860 struct btrfs_balance_control *bctl;
2861 struct btrfs_balance_item *item;
2862 struct btrfs_disk_balance_args disk_bargs;
2863 struct btrfs_path *path;
2864 struct extent_buffer *leaf;
2865 struct btrfs_key key;
2866 int ret;
2867
2868 path = btrfs_alloc_path();
2869 if (!path)
2870 return -ENOMEM;
2871
2872 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2873 if (!bctl) {
2874 ret = -ENOMEM;
2875 goto out;
2876 }
2877
2878 key.objectid = BTRFS_BALANCE_OBJECTID;
2879 key.type = BTRFS_BALANCE_ITEM_KEY;
2880 key.offset = 0;
2881
2882 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2883 if (ret < 0)
2884 goto out_bctl;
2885 if (ret > 0) { /* ret = -ENOENT; */
2886 ret = 0;
2887 goto out_bctl;
2888 }
2889
2890 leaf = path->nodes[0];
2891 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2892
2893 bctl->fs_info = tree_root->fs_info;
2894 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2895
2896 btrfs_balance_data(leaf, item, &disk_bargs);
2897 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2898 btrfs_balance_meta(leaf, item, &disk_bargs);
2899 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2900 btrfs_balance_sys(leaf, item, &disk_bargs);
2901 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2902
2903 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2904 if (IS_ERR(tsk))
2905 ret = PTR_ERR(tsk);
2906 else
2907 goto out;
2908
2909out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002910 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002911out:
2912 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002913 return ret;
2914}
2915
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002916int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2917{
2918 int ret = 0;
2919
2920 mutex_lock(&fs_info->balance_mutex);
2921 if (!fs_info->balance_ctl) {
2922 mutex_unlock(&fs_info->balance_mutex);
2923 return -ENOTCONN;
2924 }
2925
2926 if (atomic_read(&fs_info->balance_running)) {
2927 atomic_inc(&fs_info->balance_pause_req);
2928 mutex_unlock(&fs_info->balance_mutex);
2929
2930 wait_event(fs_info->balance_wait_q,
2931 atomic_read(&fs_info->balance_running) == 0);
2932
2933 mutex_lock(&fs_info->balance_mutex);
2934 /* we are good with balance_ctl ripped off from under us */
2935 BUG_ON(atomic_read(&fs_info->balance_running));
2936 atomic_dec(&fs_info->balance_pause_req);
2937 } else {
2938 ret = -ENOTCONN;
2939 }
2940
2941 mutex_unlock(&fs_info->balance_mutex);
2942 return ret;
2943}
2944
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002945int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2946{
2947 mutex_lock(&fs_info->balance_mutex);
2948 if (!fs_info->balance_ctl) {
2949 mutex_unlock(&fs_info->balance_mutex);
2950 return -ENOTCONN;
2951 }
2952
2953 atomic_inc(&fs_info->balance_cancel_req);
2954 /*
2955 * if we are running just wait and return, balance item is
2956 * deleted in btrfs_balance in this case
2957 */
2958 if (atomic_read(&fs_info->balance_running)) {
2959 mutex_unlock(&fs_info->balance_mutex);
2960 wait_event(fs_info->balance_wait_q,
2961 atomic_read(&fs_info->balance_running) == 0);
2962 mutex_lock(&fs_info->balance_mutex);
2963 } else {
2964 /* __cancel_balance needs volume_mutex */
2965 mutex_unlock(&fs_info->balance_mutex);
2966 mutex_lock(&fs_info->volume_mutex);
2967 mutex_lock(&fs_info->balance_mutex);
2968
2969 if (fs_info->balance_ctl)
2970 __cancel_balance(fs_info);
2971
2972 mutex_unlock(&fs_info->volume_mutex);
2973 }
2974
2975 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2976 atomic_dec(&fs_info->balance_cancel_req);
2977 mutex_unlock(&fs_info->balance_mutex);
2978 return 0;
2979}
2980
Chris Mason8f18cf12008-04-25 16:53:30 -04002981/*
2982 * shrinking a device means finding all of the device extents past
2983 * the new size, and then following the back refs to the chunks.
2984 * The chunk relocation code actually frees the device extent
2985 */
2986int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2987{
2988 struct btrfs_trans_handle *trans;
2989 struct btrfs_root *root = device->dev_root;
2990 struct btrfs_dev_extent *dev_extent = NULL;
2991 struct btrfs_path *path;
2992 u64 length;
2993 u64 chunk_tree;
2994 u64 chunk_objectid;
2995 u64 chunk_offset;
2996 int ret;
2997 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002998 int failed = 0;
2999 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003000 struct extent_buffer *l;
3001 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003002 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003003 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003004 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003005 u64 diff = device->total_bytes - new_size;
3006
Yan Zheng2b820322008-11-17 21:11:30 -05003007 if (new_size >= device->total_bytes)
3008 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04003009
3010 path = btrfs_alloc_path();
3011 if (!path)
3012 return -ENOMEM;
3013
Chris Mason8f18cf12008-04-25 16:53:30 -04003014 path->reada = 2;
3015
Chris Mason7d9eb122008-07-08 14:19:17 -04003016 lock_chunks(root);
3017
Chris Mason8f18cf12008-04-25 16:53:30 -04003018 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003019 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003020 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003021 spin_lock(&root->fs_info->free_chunk_lock);
3022 root->fs_info->free_chunk_space -= diff;
3023 spin_unlock(&root->fs_info->free_chunk_lock);
3024 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003025 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003026
Josef Bacikba1bf482009-09-11 16:11:19 -04003027again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003028 key.objectid = device->devid;
3029 key.offset = (u64)-1;
3030 key.type = BTRFS_DEV_EXTENT_KEY;
3031
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003032 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003033 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3034 if (ret < 0)
3035 goto done;
3036
3037 ret = btrfs_previous_item(root, path, 0, key.type);
3038 if (ret < 0)
3039 goto done;
3040 if (ret) {
3041 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003042 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003043 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003044 }
3045
3046 l = path->nodes[0];
3047 slot = path->slots[0];
3048 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3049
Josef Bacikba1bf482009-09-11 16:11:19 -04003050 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003051 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003052 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003053 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003054
3055 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3056 length = btrfs_dev_extent_length(l, dev_extent);
3057
Josef Bacikba1bf482009-09-11 16:11:19 -04003058 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003059 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003060 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003061 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003062
3063 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3064 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3065 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003066 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003067
3068 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3069 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003070 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003071 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003072 if (ret == -ENOSPC)
3073 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003074 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003075
3076 if (failed && !retried) {
3077 failed = 0;
3078 retried = true;
3079 goto again;
3080 } else if (failed && retried) {
3081 ret = -ENOSPC;
3082 lock_chunks(root);
3083
3084 device->total_bytes = old_size;
3085 if (device->writeable)
3086 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003087 spin_lock(&root->fs_info->free_chunk_lock);
3088 root->fs_info->free_chunk_space += diff;
3089 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003090 unlock_chunks(root);
3091 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003092 }
3093
Chris Balld6397ba2009-04-27 07:29:03 -04003094 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003095 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003096 if (IS_ERR(trans)) {
3097 ret = PTR_ERR(trans);
3098 goto done;
3099 }
3100
Chris Balld6397ba2009-04-27 07:29:03 -04003101 lock_chunks(root);
3102
3103 device->disk_total_bytes = new_size;
3104 /* Now btrfs_update_device() will change the on-disk size. */
3105 ret = btrfs_update_device(trans, device);
3106 if (ret) {
3107 unlock_chunks(root);
3108 btrfs_end_transaction(trans, root);
3109 goto done;
3110 }
3111 WARN_ON(diff > old_total);
3112 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3113 unlock_chunks(root);
3114 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003115done:
3116 btrfs_free_path(path);
3117 return ret;
3118}
3119
Li Zefan125ccb02011-12-08 15:07:24 +08003120static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003121 struct btrfs_key *key,
3122 struct btrfs_chunk *chunk, int item_size)
3123{
David Sterba6c417612011-04-13 15:41:04 +02003124 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003125 struct btrfs_disk_key disk_key;
3126 u32 array_size;
3127 u8 *ptr;
3128
3129 array_size = btrfs_super_sys_array_size(super_copy);
3130 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3131 return -EFBIG;
3132
3133 ptr = super_copy->sys_chunk_array + array_size;
3134 btrfs_cpu_key_to_disk(&disk_key, key);
3135 memcpy(ptr, &disk_key, sizeof(disk_key));
3136 ptr += sizeof(disk_key);
3137 memcpy(ptr, chunk, item_size);
3138 item_size += sizeof(disk_key);
3139 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3140 return 0;
3141}
3142
Miao Xieb2117a32011-01-05 10:07:28 +00003143/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003144 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003145 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003146static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003147{
Arne Jansen73c5de02011-04-12 12:07:57 +02003148 const struct btrfs_device_info *di_a = a;
3149 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003150
Arne Jansen73c5de02011-04-12 12:07:57 +02003151 if (di_a->max_avail > di_b->max_avail)
3152 return -1;
3153 if (di_a->max_avail < di_b->max_avail)
3154 return 1;
3155 if (di_a->total_avail > di_b->total_avail)
3156 return -1;
3157 if (di_a->total_avail < di_b->total_avail)
3158 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003159 return 0;
3160}
3161
3162static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3163 struct btrfs_root *extent_root,
3164 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003165 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003166 u64 start, u64 type)
3167{
3168 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003169 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3170 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003171 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003172 struct extent_map_tree *em_tree;
3173 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003174 struct btrfs_device_info *devices_info = NULL;
3175 u64 total_avail;
3176 int num_stripes; /* total number of stripes to allocate */
3177 int sub_stripes; /* sub_stripes info for map */
3178 int dev_stripes; /* stripes per dev */
3179 int devs_max; /* max devs to use */
3180 int devs_min; /* min devs needed */
3181 int devs_increment; /* ndevs has to be a multiple of this */
3182 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003183 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003184 u64 max_stripe_size;
3185 u64 max_chunk_size;
3186 u64 stripe_size;
3187 u64 num_bytes;
3188 int ndevs;
3189 int i;
3190 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003191
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003192 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003193
Miao Xieb2117a32011-01-05 10:07:28 +00003194 if (list_empty(&fs_devices->alloc_list))
3195 return -ENOSPC;
3196
Arne Jansen73c5de02011-04-12 12:07:57 +02003197 sub_stripes = 1;
3198 dev_stripes = 1;
3199 devs_increment = 1;
3200 ncopies = 1;
3201 devs_max = 0; /* 0 == as many as possible */
3202 devs_min = 1;
3203
3204 /*
3205 * define the properties of each RAID type.
3206 * FIXME: move this to a global table and use it in all RAID
3207 * calculation code
3208 */
3209 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3210 dev_stripes = 2;
3211 ncopies = 2;
3212 devs_max = 1;
3213 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3214 devs_min = 2;
3215 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3216 devs_increment = 2;
3217 ncopies = 2;
3218 devs_max = 2;
3219 devs_min = 2;
3220 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3221 sub_stripes = 2;
3222 devs_increment = 2;
3223 ncopies = 2;
3224 devs_min = 4;
3225 } else {
3226 devs_max = 1;
3227 }
3228
3229 if (type & BTRFS_BLOCK_GROUP_DATA) {
3230 max_stripe_size = 1024 * 1024 * 1024;
3231 max_chunk_size = 10 * max_stripe_size;
3232 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003233 /* for larger filesystems, use larger metadata chunks */
3234 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3235 max_stripe_size = 1024 * 1024 * 1024;
3236 else
3237 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003238 max_chunk_size = max_stripe_size;
3239 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003240 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003241 max_chunk_size = 2 * max_stripe_size;
3242 } else {
3243 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3244 type);
3245 BUG_ON(1);
3246 }
3247
3248 /* we don't want a chunk larger than 10% of writeable space */
3249 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3250 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003251
3252 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3253 GFP_NOFS);
3254 if (!devices_info)
3255 return -ENOMEM;
3256
Arne Jansen73c5de02011-04-12 12:07:57 +02003257 cur = fs_devices->alloc_list.next;
3258
3259 /*
3260 * in the first pass through the devices list, we gather information
3261 * about the available holes on each device.
3262 */
3263 ndevs = 0;
3264 while (cur != &fs_devices->alloc_list) {
3265 struct btrfs_device *device;
3266 u64 max_avail;
3267 u64 dev_offset;
3268
3269 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3270
3271 cur = cur->next;
3272
3273 if (!device->writeable) {
3274 printk(KERN_ERR
3275 "btrfs: read-only device in alloc_list\n");
3276 WARN_ON(1);
3277 continue;
3278 }
3279
3280 if (!device->in_fs_metadata)
3281 continue;
3282
3283 if (device->total_bytes > device->bytes_used)
3284 total_avail = device->total_bytes - device->bytes_used;
3285 else
3286 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003287
3288 /* If there is no space on this device, skip it. */
3289 if (total_avail == 0)
3290 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003291
Li Zefan125ccb02011-12-08 15:07:24 +08003292 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003293 max_stripe_size * dev_stripes,
3294 &dev_offset, &max_avail);
3295 if (ret && ret != -ENOSPC)
3296 goto error;
3297
3298 if (ret == 0)
3299 max_avail = max_stripe_size * dev_stripes;
3300
3301 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3302 continue;
3303
3304 devices_info[ndevs].dev_offset = dev_offset;
3305 devices_info[ndevs].max_avail = max_avail;
3306 devices_info[ndevs].total_avail = total_avail;
3307 devices_info[ndevs].dev = device;
3308 ++ndevs;
3309 }
3310
3311 /*
3312 * now sort the devices by hole size / available space
3313 */
3314 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3315 btrfs_cmp_device_info, NULL);
3316
3317 /* round down to number of usable stripes */
3318 ndevs -= ndevs % devs_increment;
3319
3320 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3321 ret = -ENOSPC;
3322 goto error;
3323 }
3324
3325 if (devs_max && ndevs > devs_max)
3326 ndevs = devs_max;
3327 /*
3328 * the primary goal is to maximize the number of stripes, so use as many
3329 * devices as possible, even if the stripes are not maximum sized.
3330 */
3331 stripe_size = devices_info[ndevs-1].max_avail;
3332 num_stripes = ndevs * dev_stripes;
3333
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003334 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003335 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003336 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003337 }
3338
3339 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003340
3341 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003342 do_div(stripe_size, BTRFS_STRIPE_LEN);
3343 stripe_size *= BTRFS_STRIPE_LEN;
3344
Miao Xieb2117a32011-01-05 10:07:28 +00003345 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3346 if (!map) {
3347 ret = -ENOMEM;
3348 goto error;
3349 }
3350 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003351
Arne Jansen73c5de02011-04-12 12:07:57 +02003352 for (i = 0; i < ndevs; ++i) {
3353 for (j = 0; j < dev_stripes; ++j) {
3354 int s = i * dev_stripes + j;
3355 map->stripes[s].dev = devices_info[i].dev;
3356 map->stripes[s].physical = devices_info[i].dev_offset +
3357 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003358 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003359 }
Chris Mason593060d2008-03-25 16:50:33 -04003360 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003361 map->stripe_len = BTRFS_STRIPE_LEN;
3362 map->io_align = BTRFS_STRIPE_LEN;
3363 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003364 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003365 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003366
Yan Zheng2b820322008-11-17 21:11:30 -05003367 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003368 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003369
Arne Jansen73c5de02011-04-12 12:07:57 +02003370 *stripe_size_out = stripe_size;
3371 *num_bytes_out = num_bytes;
3372
3373 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003374
David Sterba172ddd62011-04-21 00:48:27 +02003375 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003376 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003377 ret = -ENOMEM;
3378 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003379 }
Chris Mason0b86a832008-03-24 15:01:56 -04003380 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003381 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003382 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003383 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003384 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003385
Chris Mason0b86a832008-03-24 15:01:56 -04003386 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003387 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003388 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003389 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003390 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003391 if (ret)
3392 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003393
3394 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3395 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003396 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003397 if (ret)
3398 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003399
Arne Jansen73c5de02011-04-12 12:07:57 +02003400 for (i = 0; i < map->num_stripes; ++i) {
3401 struct btrfs_device *device;
3402 u64 dev_offset;
3403
3404 device = map->stripes[i].dev;
3405 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003406
3407 ret = btrfs_alloc_dev_extent(trans, device,
3408 info->chunk_root->root_key.objectid,
3409 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003410 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003411 if (ret) {
3412 btrfs_abort_transaction(trans, extent_root, ret);
3413 goto error;
3414 }
Yan Zheng2b820322008-11-17 21:11:30 -05003415 }
3416
Miao Xieb2117a32011-01-05 10:07:28 +00003417 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003418 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003419
3420error:
3421 kfree(map);
3422 kfree(devices_info);
3423 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003424}
3425
3426static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3427 struct btrfs_root *extent_root,
3428 struct map_lookup *map, u64 chunk_offset,
3429 u64 chunk_size, u64 stripe_size)
3430{
3431 u64 dev_offset;
3432 struct btrfs_key key;
3433 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3434 struct btrfs_device *device;
3435 struct btrfs_chunk *chunk;
3436 struct btrfs_stripe *stripe;
3437 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3438 int index = 0;
3439 int ret;
3440
3441 chunk = kzalloc(item_size, GFP_NOFS);
3442 if (!chunk)
3443 return -ENOMEM;
3444
3445 index = 0;
3446 while (index < map->num_stripes) {
3447 device = map->stripes[index].dev;
3448 device->bytes_used += stripe_size;
3449 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003450 if (ret)
3451 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003452 index++;
3453 }
3454
Josef Bacik2bf64752011-09-26 17:12:22 -04003455 spin_lock(&extent_root->fs_info->free_chunk_lock);
3456 extent_root->fs_info->free_chunk_space -= (stripe_size *
3457 map->num_stripes);
3458 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3459
Yan Zheng2b820322008-11-17 21:11:30 -05003460 index = 0;
3461 stripe = &chunk->stripe;
3462 while (index < map->num_stripes) {
3463 device = map->stripes[index].dev;
3464 dev_offset = map->stripes[index].physical;
3465
3466 btrfs_set_stack_stripe_devid(stripe, device->devid);
3467 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3468 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3469 stripe++;
3470 index++;
3471 }
3472
3473 btrfs_set_stack_chunk_length(chunk, chunk_size);
3474 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3475 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3476 btrfs_set_stack_chunk_type(chunk, map->type);
3477 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3478 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3479 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3480 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3481 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3482
3483 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3484 key.type = BTRFS_CHUNK_ITEM_KEY;
3485 key.offset = chunk_offset;
3486
3487 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003488
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003489 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3490 /*
3491 * TODO: Cleanup of inserted chunk root in case of
3492 * failure.
3493 */
Li Zefan125ccb02011-12-08 15:07:24 +08003494 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003495 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003496 }
liubo1abe9b82011-03-24 11:18:59 +00003497
Mark Fasheh3acd3952011-09-08 17:40:01 -07003498out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003499 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003500 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003501}
3502
3503/*
3504 * Chunk allocation falls into two parts. The first part does works
3505 * that make the new allocated chunk useable, but not do any operation
3506 * that modifies the chunk tree. The second part does the works that
3507 * require modifying the chunk tree. This division is important for the
3508 * bootstrap process of adding storage to a seed btrfs.
3509 */
3510int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3511 struct btrfs_root *extent_root, u64 type)
3512{
3513 u64 chunk_offset;
3514 u64 chunk_size;
3515 u64 stripe_size;
3516 struct map_lookup *map;
3517 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3518 int ret;
3519
3520 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3521 &chunk_offset);
3522 if (ret)
3523 return ret;
3524
3525 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3526 &stripe_size, chunk_offset, type);
3527 if (ret)
3528 return ret;
3529
3530 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3531 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003532 if (ret)
3533 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003534 return 0;
3535}
3536
Chris Masond3977122009-01-05 21:25:51 -05003537static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003538 struct btrfs_root *root,
3539 struct btrfs_device *device)
3540{
3541 u64 chunk_offset;
3542 u64 sys_chunk_offset;
3543 u64 chunk_size;
3544 u64 sys_chunk_size;
3545 u64 stripe_size;
3546 u64 sys_stripe_size;
3547 u64 alloc_profile;
3548 struct map_lookup *map;
3549 struct map_lookup *sys_map;
3550 struct btrfs_fs_info *fs_info = root->fs_info;
3551 struct btrfs_root *extent_root = fs_info->extent_root;
3552 int ret;
3553
3554 ret = find_next_chunk(fs_info->chunk_root,
3555 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003556 if (ret)
3557 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003558
3559 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003560 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003561 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3562
3563 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3564 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003565 if (ret)
3566 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003567
3568 sys_chunk_offset = chunk_offset + chunk_size;
3569
3570 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003571 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003572 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3573
3574 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3575 &sys_chunk_size, &sys_stripe_size,
3576 sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003577 if (ret)
3578 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003579
3580 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003581 if (ret)
3582 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003583
3584 /*
3585 * Modifying chunk tree needs allocating new blocks from both
3586 * system block group and metadata block group. So we only can
3587 * do operations require modifying the chunk tree after both
3588 * block groups were created.
3589 */
3590 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3591 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003592 if (ret)
3593 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003594
3595 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3596 sys_chunk_offset, sys_chunk_size,
3597 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003598 if (ret)
3599 goto abort;
3600
Yan Zheng2b820322008-11-17 21:11:30 -05003601 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003602
3603abort:
3604 btrfs_abort_transaction(trans, root, ret);
3605 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003606}
3607
3608int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3609{
3610 struct extent_map *em;
3611 struct map_lookup *map;
3612 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3613 int readonly = 0;
3614 int i;
3615
Chris Mason890871b2009-09-02 16:24:52 -04003616 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003617 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003618 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003619 if (!em)
3620 return 1;
3621
Josef Bacikf48b9072010-01-27 02:07:59 +00003622 if (btrfs_test_opt(root, DEGRADED)) {
3623 free_extent_map(em);
3624 return 0;
3625 }
3626
Yan Zheng2b820322008-11-17 21:11:30 -05003627 map = (struct map_lookup *)em->bdev;
3628 for (i = 0; i < map->num_stripes; i++) {
3629 if (!map->stripes[i].dev->writeable) {
3630 readonly = 1;
3631 break;
3632 }
3633 }
3634 free_extent_map(em);
3635 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003636}
3637
3638void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3639{
David Sterbaa8067e02011-04-21 00:34:43 +02003640 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003641}
3642
3643void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3644{
3645 struct extent_map *em;
3646
Chris Masond3977122009-01-05 21:25:51 -05003647 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003648 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003649 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3650 if (em)
3651 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003652 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003653 if (!em)
3654 break;
3655 kfree(em->bdev);
3656 /* once for us */
3657 free_extent_map(em);
3658 /* once for the tree */
3659 free_extent_map(em);
3660 }
3661}
3662
Chris Masonf1885912008-04-09 16:28:12 -04003663int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3664{
3665 struct extent_map *em;
3666 struct map_lookup *map;
3667 struct extent_map_tree *em_tree = &map_tree->map_tree;
3668 int ret;
3669
Chris Mason890871b2009-09-02 16:24:52 -04003670 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003671 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003672 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003673 BUG_ON(!em);
3674
3675 BUG_ON(em->start > logical || em->start + em->len < logical);
3676 map = (struct map_lookup *)em->bdev;
3677 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3678 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003679 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3680 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003681 else
3682 ret = 1;
3683 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003684 return ret;
3685}
3686
Chris Masondfe25022008-05-13 13:46:40 -04003687static int find_live_mirror(struct map_lookup *map, int first, int num,
3688 int optimal)
3689{
3690 int i;
3691 if (map->stripes[optimal].dev->bdev)
3692 return optimal;
3693 for (i = first; i < first + num; i++) {
3694 if (map->stripes[i].dev->bdev)
3695 return i;
3696 }
3697 /* we couldn't find one that doesn't fail. Just return something
3698 * and the io error handling code will clean up eventually
3699 */
3700 return optimal;
3701}
3702
Chris Masonf2d8d742008-04-21 10:03:05 -04003703static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3704 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003705 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003706 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003707{
3708 struct extent_map *em;
3709 struct map_lookup *map;
3710 struct extent_map_tree *em_tree = &map_tree->map_tree;
3711 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003712 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003713 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003714 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003715 u64 stripe_nr_orig;
3716 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003717 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003718 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003719 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003720 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003721 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003722 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003723
Chris Mason890871b2009-09-02 16:24:52 -04003724 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003725 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003726 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003727
Chris Mason3b951512008-04-17 11:29:12 -04003728 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003729 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3730 (unsigned long long)logical,
3731 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003732 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003733 }
Chris Mason0b86a832008-03-24 15:01:56 -04003734
3735 BUG_ON(em->start > logical || em->start + em->len < logical);
3736 map = (struct map_lookup *)em->bdev;
3737 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003738
Chris Masonf1885912008-04-09 16:28:12 -04003739 if (mirror_num > map->num_stripes)
3740 mirror_num = 0;
3741
Chris Mason593060d2008-03-25 16:50:33 -04003742 stripe_nr = offset;
3743 /*
3744 * stripe_nr counts the total number of stripes we have to stride
3745 * to get to this block
3746 */
3747 do_div(stripe_nr, map->stripe_len);
3748
3749 stripe_offset = stripe_nr * map->stripe_len;
3750 BUG_ON(offset < stripe_offset);
3751
3752 /* stripe_offset is the offset of this block in its stripe*/
3753 stripe_offset = offset - stripe_offset;
3754
Li Dongyangfce3bb92011-03-24 10:24:26 +00003755 if (rw & REQ_DISCARD)
3756 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003757 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003758 /* we limit the length of each bio to what fits in a stripe */
3759 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003760 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003761 } else {
3762 *length = em->len - offset;
3763 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003764
Jan Schmidta1d3c472011-08-04 17:15:33 +02003765 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003766 goto out;
3767
Chris Masonf2d8d742008-04-21 10:03:05 -04003768 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003769 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003770 stripe_nr_orig = stripe_nr;
3771 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3772 (~(map->stripe_len - 1));
3773 do_div(stripe_nr_end, map->stripe_len);
3774 stripe_end_offset = stripe_nr_end * map->stripe_len -
3775 (offset + *length);
3776 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3777 if (rw & REQ_DISCARD)
3778 num_stripes = min_t(u64, map->num_stripes,
3779 stripe_nr_end - stripe_nr_orig);
3780 stripe_index = do_div(stripe_nr, map->num_stripes);
3781 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003782 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003783 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003784 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003785 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003786 else {
3787 stripe_index = find_live_mirror(map, 0,
3788 map->num_stripes,
3789 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003790 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003791 }
Chris Mason2fff7342008-04-29 14:12:09 -04003792
Chris Mason611f0e02008-04-03 16:29:03 -04003793 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003794 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003795 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003796 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003797 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003798 } else {
3799 mirror_num = 1;
3800 }
Chris Mason2fff7342008-04-29 14:12:09 -04003801
Chris Mason321aecc2008-04-16 10:49:51 -04003802 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3803 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003804
3805 stripe_index = do_div(stripe_nr, factor);
3806 stripe_index *= map->sub_stripes;
3807
Jens Axboe7eaceac2011-03-10 08:52:07 +01003808 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003809 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003810 else if (rw & REQ_DISCARD)
3811 num_stripes = min_t(u64, map->sub_stripes *
3812 (stripe_nr_end - stripe_nr_orig),
3813 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003814 else if (mirror_num)
3815 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003816 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04003817 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04003818 stripe_index = find_live_mirror(map, stripe_index,
3819 map->sub_stripes, stripe_index +
3820 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04003821 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003822 }
Chris Mason8790d502008-04-03 16:29:03 -04003823 } else {
3824 /*
3825 * after this do_div call, stripe_nr is the number of stripes
3826 * on this device we have to walk to find the data, and
3827 * stripe_index is the number of our device in the stripe array
3828 */
3829 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003830 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003831 }
Chris Mason593060d2008-03-25 16:50:33 -04003832 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003833
Li Zefande11cc12011-12-01 12:55:47 +08003834 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3835 if (!bbio) {
3836 ret = -ENOMEM;
3837 goto out;
3838 }
3839 atomic_set(&bbio->error, 0);
3840
Li Dongyangfce3bb92011-03-24 10:24:26 +00003841 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003842 int factor = 0;
3843 int sub_stripes = 0;
3844 u64 stripes_per_dev = 0;
3845 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003846 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003847
3848 if (map->type &
3849 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3850 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3851 sub_stripes = 1;
3852 else
3853 sub_stripes = map->sub_stripes;
3854
3855 factor = map->num_stripes / sub_stripes;
3856 stripes_per_dev = div_u64_rem(stripe_nr_end -
3857 stripe_nr_orig,
3858 factor,
3859 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04003860 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3861 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003862 }
3863
Li Dongyangfce3bb92011-03-24 10:24:26 +00003864 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003865 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003866 map->stripes[stripe_index].physical +
3867 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003868 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003869
Li Zefanec9ef7a2011-12-01 14:06:42 +08003870 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3871 BTRFS_BLOCK_GROUP_RAID10)) {
3872 bbio->stripes[i].length = stripes_per_dev *
3873 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003874
Li Zefanec9ef7a2011-12-01 14:06:42 +08003875 if (i / sub_stripes < remaining_stripes)
3876 bbio->stripes[i].length +=
3877 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003878
3879 /*
3880 * Special for the first stripe and
3881 * the last stripe:
3882 *
3883 * |-------|...|-------|
3884 * |----------|
3885 * off end_off
3886 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08003887 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003888 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003889 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003890
3891 if (stripe_index >= last_stripe &&
3892 stripe_index <= (last_stripe +
3893 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08003894 bbio->stripes[i].length -=
3895 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003896
Li Zefanec9ef7a2011-12-01 14:06:42 +08003897 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003898 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003899 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003900 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003901
3902 stripe_index++;
3903 if (stripe_index == map->num_stripes) {
3904 /* This could only happen for RAID0/10 */
3905 stripe_index = 0;
3906 stripe_nr++;
3907 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003908 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003909 } else {
3910 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003911 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003912 map->stripes[stripe_index].physical +
3913 stripe_offset +
3914 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003915 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003916 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003917 stripe_index++;
3918 }
Chris Mason593060d2008-03-25 16:50:33 -04003919 }
Li Zefande11cc12011-12-01 12:55:47 +08003920
3921 if (rw & REQ_WRITE) {
3922 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3923 BTRFS_BLOCK_GROUP_RAID10 |
3924 BTRFS_BLOCK_GROUP_DUP)) {
3925 max_errors = 1;
3926 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003927 }
Li Zefande11cc12011-12-01 12:55:47 +08003928
3929 *bbio_ret = bbio;
3930 bbio->num_stripes = num_stripes;
3931 bbio->max_errors = max_errors;
3932 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003933out:
Chris Mason0b86a832008-03-24 15:01:56 -04003934 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003935 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003936}
3937
Chris Masonf2d8d742008-04-21 10:03:05 -04003938int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3939 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003940 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003941{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003942 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003943 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003944}
3945
Yan Zhenga512bbf2008-12-08 16:46:26 -05003946int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3947 u64 chunk_start, u64 physical, u64 devid,
3948 u64 **logical, int *naddrs, int *stripe_len)
3949{
3950 struct extent_map_tree *em_tree = &map_tree->map_tree;
3951 struct extent_map *em;
3952 struct map_lookup *map;
3953 u64 *buf;
3954 u64 bytenr;
3955 u64 length;
3956 u64 stripe_nr;
3957 int i, j, nr = 0;
3958
Chris Mason890871b2009-09-02 16:24:52 -04003959 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003960 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003961 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003962
3963 BUG_ON(!em || em->start != chunk_start);
3964 map = (struct map_lookup *)em->bdev;
3965
3966 length = em->len;
3967 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3968 do_div(length, map->num_stripes / map->sub_stripes);
3969 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3970 do_div(length, map->num_stripes);
3971
3972 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003973 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003974
3975 for (i = 0; i < map->num_stripes; i++) {
3976 if (devid && map->stripes[i].dev->devid != devid)
3977 continue;
3978 if (map->stripes[i].physical > physical ||
3979 map->stripes[i].physical + length <= physical)
3980 continue;
3981
3982 stripe_nr = physical - map->stripes[i].physical;
3983 do_div(stripe_nr, map->stripe_len);
3984
3985 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3986 stripe_nr = stripe_nr * map->num_stripes + i;
3987 do_div(stripe_nr, map->sub_stripes);
3988 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3989 stripe_nr = stripe_nr * map->num_stripes + i;
3990 }
3991 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003992 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003993 for (j = 0; j < nr; j++) {
3994 if (buf[j] == bytenr)
3995 break;
3996 }
Chris Mason934d3752008-12-08 16:43:10 -05003997 if (j == nr) {
3998 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003999 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004000 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004001 }
4002
Yan Zhenga512bbf2008-12-08 16:46:26 -05004003 *logical = buf;
4004 *naddrs = nr;
4005 *stripe_len = map->stripe_len;
4006
4007 free_extent_map(em);
4008 return 0;
4009}
4010
Jan Schmidta1d3c472011-08-04 17:15:33 +02004011static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004012{
Jan Schmidta1d3c472011-08-04 17:15:33 +02004013 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004014 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004015
Chris Mason8790d502008-04-03 16:29:03 -04004016 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004017 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04004018
Jan Schmidta1d3c472011-08-04 17:15:33 +02004019 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004020 is_orig_bio = 1;
4021
Jan Schmidta1d3c472011-08-04 17:15:33 +02004022 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004023 if (!is_orig_bio) {
4024 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004025 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004026 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004027 bio->bi_private = bbio->private;
4028 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004029 bio->bi_bdev = (struct block_device *)
4030 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004031 /* only send an error to the higher layers if it is
4032 * beyond the tolerance of the multi-bio
4033 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004034 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004035 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004036 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004037 /*
4038 * this bio is actually up to date, we didn't
4039 * go over the max number of errors
4040 */
4041 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004042 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004043 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004044 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004045
4046 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004047 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004048 bio_put(bio);
4049 }
Chris Mason8790d502008-04-03 16:29:03 -04004050}
4051
Chris Mason8b712842008-06-11 16:50:36 -04004052struct async_sched {
4053 struct bio *bio;
4054 int rw;
4055 struct btrfs_fs_info *info;
4056 struct btrfs_work work;
4057};
4058
4059/*
4060 * see run_scheduled_bios for a description of why bios are collected for
4061 * async submit.
4062 *
4063 * This will add one bio to the pending list for a device and make sure
4064 * the work struct is scheduled.
4065 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004066static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004067 struct btrfs_device *device,
4068 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004069{
4070 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004071 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004072
4073 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004074 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6d2008-07-31 16:29:02 -04004075 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004076 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04004077 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004078 return;
Chris Mason8b712842008-06-11 16:50:36 -04004079 }
4080
4081 /*
Chris Mason0986fe92008-08-15 15:34:15 -04004082 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004083 * higher layers. Otherwise, the async bio makes it appear we have
4084 * made progress against dirty pages when we've really just put it
4085 * on a queue for later
4086 */
Chris Mason0986fe92008-08-15 15:34:15 -04004087 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04004088 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004089 bio->bi_next = NULL;
4090 bio->bi_rw |= rw;
4091
4092 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004093 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004094 pending_bios = &device->pending_sync_bios;
4095 else
4096 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004097
Chris Masonffbd5172009-04-20 15:50:09 -04004098 if (pending_bios->tail)
4099 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004100
Chris Masonffbd5172009-04-20 15:50:09 -04004101 pending_bios->tail = bio;
4102 if (!pending_bios->head)
4103 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004104 if (device->running_pending)
4105 should_queue = 0;
4106
4107 spin_unlock(&device->io_lock);
4108
4109 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004110 btrfs_queue_worker(&root->fs_info->submit_workers,
4111 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004112}
4113
Chris Masonf1885912008-04-09 16:28:12 -04004114int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004115 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004116{
4117 struct btrfs_mapping_tree *map_tree;
4118 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004119 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004120 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004121 u64 length = 0;
4122 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004123 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004124 int dev_nr = 0;
4125 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004126 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004127
Chris Masonf2d8d742008-04-21 10:03:05 -04004128 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004129 map_tree = &root->fs_info->mapping_tree;
4130 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004131
Jan Schmidta1d3c472011-08-04 17:15:33 +02004132 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004133 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004134 if (ret) /* -ENOMEM */
4135 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004136
Jan Schmidta1d3c472011-08-04 17:15:33 +02004137 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004138 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004139 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4140 "len %llu\n", (unsigned long long)logical,
4141 (unsigned long long)length,
4142 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004143 BUG();
4144 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004145
4146 bbio->orig_bio = first_bio;
4147 bbio->private = first_bio->bi_private;
4148 bbio->end_io = first_bio->bi_end_io;
4149 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004150
Chris Masond3977122009-01-05 21:25:51 -05004151 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004152 if (dev_nr < total_devs - 1) {
4153 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004155 } else {
4156 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004157 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004158 bio->bi_private = bbio;
4159 bio->bi_end_io = btrfs_end_bio;
4160 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4161 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004162 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004163 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4164 "(%s id %llu), size=%u\n", rw,
4165 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4166 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004167 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004168 if (async_submit)
4169 schedule_bio(root, dev, rw, bio);
4170 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004171 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004172 } else {
4173 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4174 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004175 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004176 }
Chris Mason8790d502008-04-03 16:29:03 -04004177 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004178 }
Chris Mason0b86a832008-03-24 15:01:56 -04004179 return 0;
4180}
4181
Chris Masona4437552008-04-18 10:29:38 -04004182struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004183 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004184{
Yan Zheng2b820322008-11-17 21:11:30 -05004185 struct btrfs_device *device;
4186 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004187
Yan Zheng2b820322008-11-17 21:11:30 -05004188 cur_devices = root->fs_info->fs_devices;
4189 while (cur_devices) {
4190 if (!fsid ||
4191 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4192 device = __find_device(&cur_devices->devices,
4193 devid, uuid);
4194 if (device)
4195 return device;
4196 }
4197 cur_devices = cur_devices->seed;
4198 }
4199 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004200}
4201
Chris Masondfe25022008-05-13 13:46:40 -04004202static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4203 u64 devid, u8 *dev_uuid)
4204{
4205 struct btrfs_device *device;
4206 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4207
4208 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004209 if (!device)
4210 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004211 list_add(&device->dev_list,
4212 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004213 device->dev_root = root->fs_info->dev_root;
4214 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004215 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004216 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004217 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004218 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004219 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004220 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004221 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004222 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4223 return device;
4224}
4225
Chris Mason0b86a832008-03-24 15:01:56 -04004226static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4227 struct extent_buffer *leaf,
4228 struct btrfs_chunk *chunk)
4229{
4230 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4231 struct map_lookup *map;
4232 struct extent_map *em;
4233 u64 logical;
4234 u64 length;
4235 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004236 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004237 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004238 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004239 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004240
Chris Masone17cade2008-04-15 15:41:47 -04004241 logical = key->offset;
4242 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004243
Chris Mason890871b2009-09-02 16:24:52 -04004244 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004245 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004246 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004247
4248 /* already mapped? */
4249 if (em && em->start <= logical && em->start + em->len > logical) {
4250 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004251 return 0;
4252 } else if (em) {
4253 free_extent_map(em);
4254 }
Chris Mason0b86a832008-03-24 15:01:56 -04004255
David Sterba172ddd62011-04-21 00:48:27 +02004256 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004257 if (!em)
4258 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004259 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4260 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004261 if (!map) {
4262 free_extent_map(em);
4263 return -ENOMEM;
4264 }
4265
4266 em->bdev = (struct block_device *)map;
4267 em->start = logical;
4268 em->len = length;
4269 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004270 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004271
Chris Mason593060d2008-03-25 16:50:33 -04004272 map->num_stripes = num_stripes;
4273 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4274 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4275 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4276 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4277 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004278 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004279 for (i = 0; i < num_stripes; i++) {
4280 map->stripes[i].physical =
4281 btrfs_stripe_offset_nr(leaf, chunk, i);
4282 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004283 read_extent_buffer(leaf, uuid, (unsigned long)
4284 btrfs_stripe_dev_uuid_nr(chunk, i),
4285 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004286 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4287 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004288 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004289 kfree(map);
4290 free_extent_map(em);
4291 return -EIO;
4292 }
Chris Masondfe25022008-05-13 13:46:40 -04004293 if (!map->stripes[i].dev) {
4294 map->stripes[i].dev =
4295 add_missing_dev(root, devid, uuid);
4296 if (!map->stripes[i].dev) {
4297 kfree(map);
4298 free_extent_map(em);
4299 return -EIO;
4300 }
4301 }
4302 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004303 }
4304
Chris Mason890871b2009-09-02 16:24:52 -04004305 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004306 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004307 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004308 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004309 free_extent_map(em);
4310
4311 return 0;
4312}
4313
Jeff Mahoney143bede2012-03-01 14:56:26 +01004314static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004315 struct btrfs_dev_item *dev_item,
4316 struct btrfs_device *device)
4317{
4318 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004319
4320 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004321 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4322 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004323 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4324 device->type = btrfs_device_type(leaf, dev_item);
4325 device->io_align = btrfs_device_io_align(leaf, dev_item);
4326 device->io_width = btrfs_device_io_width(leaf, dev_item);
4327 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004328
4329 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004330 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004331}
4332
Yan Zheng2b820322008-11-17 21:11:30 -05004333static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4334{
4335 struct btrfs_fs_devices *fs_devices;
4336 int ret;
4337
Li Zefanb367e472011-12-07 11:38:24 +08004338 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004339
4340 fs_devices = root->fs_info->fs_devices->seed;
4341 while (fs_devices) {
4342 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4343 ret = 0;
4344 goto out;
4345 }
4346 fs_devices = fs_devices->seed;
4347 }
4348
4349 fs_devices = find_fsid(fsid);
4350 if (!fs_devices) {
4351 ret = -ENOENT;
4352 goto out;
4353 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004354
4355 fs_devices = clone_fs_devices(fs_devices);
4356 if (IS_ERR(fs_devices)) {
4357 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004358 goto out;
4359 }
4360
Christoph Hellwig97288f22008-12-02 06:36:09 -05004361 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004362 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004363 if (ret) {
4364 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004365 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004366 }
Yan Zheng2b820322008-11-17 21:11:30 -05004367
4368 if (!fs_devices->seeding) {
4369 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004370 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004371 ret = -EINVAL;
4372 goto out;
4373 }
4374
4375 fs_devices->seed = root->fs_info->fs_devices->seed;
4376 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004377out:
Yan Zheng2b820322008-11-17 21:11:30 -05004378 return ret;
4379}
4380
Chris Mason0d81ba52008-03-24 15:02:07 -04004381static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004382 struct extent_buffer *leaf,
4383 struct btrfs_dev_item *dev_item)
4384{
4385 struct btrfs_device *device;
4386 u64 devid;
4387 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004388 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004389 u8 dev_uuid[BTRFS_UUID_SIZE];
4390
Chris Mason0b86a832008-03-24 15:01:56 -04004391 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004392 read_extent_buffer(leaf, dev_uuid,
4393 (unsigned long)btrfs_device_uuid(dev_item),
4394 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004395 read_extent_buffer(leaf, fs_uuid,
4396 (unsigned long)btrfs_device_fsid(dev_item),
4397 BTRFS_UUID_SIZE);
4398
4399 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4400 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004401 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004402 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004403 }
4404
4405 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4406 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004407 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004408 return -EIO;
4409
4410 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004411 printk(KERN_WARNING "warning devid %llu missing\n",
4412 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004413 device = add_missing_dev(root, devid, dev_uuid);
4414 if (!device)
4415 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004416 } else if (!device->missing) {
4417 /*
4418 * this happens when a device that was properly setup
4419 * in the device info lists suddenly goes bad.
4420 * device->bdev is NULL, and so we have to set
4421 * device->missing to one here
4422 */
4423 root->fs_info->fs_devices->missing_devices++;
4424 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004425 }
4426 }
4427
4428 if (device->fs_devices != root->fs_info->fs_devices) {
4429 BUG_ON(device->writeable);
4430 if (device->generation !=
4431 btrfs_device_generation(leaf, dev_item))
4432 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004433 }
Chris Mason0b86a832008-03-24 15:01:56 -04004434
4435 fill_device_from_item(leaf, dev_item, device);
4436 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004437 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004438 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004439 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004440 spin_lock(&root->fs_info->free_chunk_lock);
4441 root->fs_info->free_chunk_space += device->total_bytes -
4442 device->bytes_used;
4443 spin_unlock(&root->fs_info->free_chunk_lock);
4444 }
Chris Mason0b86a832008-03-24 15:01:56 -04004445 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004446 return ret;
4447}
4448
Yan Zhenge4404d62008-12-12 10:03:26 -05004449int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004450{
David Sterba6c417612011-04-13 15:41:04 +02004451 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004452 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004453 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004454 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004455 u8 *ptr;
4456 unsigned long sb_ptr;
4457 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004458 u32 num_stripes;
4459 u32 array_size;
4460 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004461 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004462 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004463
Yan Zhenge4404d62008-12-12 10:03:26 -05004464 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004465 BTRFS_SUPER_INFO_SIZE);
4466 if (!sb)
4467 return -ENOMEM;
4468 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004469 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004470 /*
4471 * The sb extent buffer is artifical and just used to read the system array.
4472 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4473 * pages up-to-date when the page is larger: extent does not cover the
4474 * whole page and consequently check_page_uptodate does not find all
4475 * the page's extents up-to-date (the hole beyond sb),
4476 * write_extent_buffer then triggers a WARN_ON.
4477 *
4478 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4479 * but sb spans only this function. Add an explicit SetPageUptodate call
4480 * to silence the warning eg. on PowerPC 64.
4481 */
4482 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004483 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004484
Chris Masona061fc82008-05-07 11:43:44 -04004485 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004486 array_size = btrfs_super_sys_array_size(super_copy);
4487
Chris Mason0b86a832008-03-24 15:01:56 -04004488 ptr = super_copy->sys_chunk_array;
4489 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4490 cur = 0;
4491
4492 while (cur < array_size) {
4493 disk_key = (struct btrfs_disk_key *)ptr;
4494 btrfs_disk_key_to_cpu(&key, disk_key);
4495
Chris Masona061fc82008-05-07 11:43:44 -04004496 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004497 sb_ptr += len;
4498 cur += len;
4499
Chris Mason0d81ba52008-03-24 15:02:07 -04004500 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004501 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004502 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004503 if (ret)
4504 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004505 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4506 len = btrfs_chunk_item_size(num_stripes);
4507 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004508 ret = -EIO;
4509 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004510 }
4511 ptr += len;
4512 sb_ptr += len;
4513 cur += len;
4514 }
Chris Masona061fc82008-05-07 11:43:44 -04004515 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004516 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004517}
4518
4519int btrfs_read_chunk_tree(struct btrfs_root *root)
4520{
4521 struct btrfs_path *path;
4522 struct extent_buffer *leaf;
4523 struct btrfs_key key;
4524 struct btrfs_key found_key;
4525 int ret;
4526 int slot;
4527
4528 root = root->fs_info->chunk_root;
4529
4530 path = btrfs_alloc_path();
4531 if (!path)
4532 return -ENOMEM;
4533
Li Zefanb367e472011-12-07 11:38:24 +08004534 mutex_lock(&uuid_mutex);
4535 lock_chunks(root);
4536
Chris Mason0b86a832008-03-24 15:01:56 -04004537 /* first we search for all of the device items, and then we
4538 * read in all of the chunk items. This way we can create chunk
4539 * mappings that reference all of the devices that are afound
4540 */
4541 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4542 key.offset = 0;
4543 key.type = 0;
4544again:
4545 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004546 if (ret < 0)
4547 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004548 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004549 leaf = path->nodes[0];
4550 slot = path->slots[0];
4551 if (slot >= btrfs_header_nritems(leaf)) {
4552 ret = btrfs_next_leaf(root, path);
4553 if (ret == 0)
4554 continue;
4555 if (ret < 0)
4556 goto error;
4557 break;
4558 }
4559 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4560 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4561 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4562 break;
4563 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4564 struct btrfs_dev_item *dev_item;
4565 dev_item = btrfs_item_ptr(leaf, slot,
4566 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004567 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004568 if (ret)
4569 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004570 }
4571 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4572 struct btrfs_chunk *chunk;
4573 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4574 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004575 if (ret)
4576 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004577 }
4578 path->slots[0]++;
4579 }
4580 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4581 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004582 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004583 goto again;
4584 }
Chris Mason0b86a832008-03-24 15:01:56 -04004585 ret = 0;
4586error:
Li Zefanb367e472011-12-07 11:38:24 +08004587 unlock_chunks(root);
4588 mutex_unlock(&uuid_mutex);
4589
Yan Zheng2b820322008-11-17 21:11:30 -05004590 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004591 return ret;
4592}