blob: 759d02486d7ccb2dcb2ff1b1334396cc219880ca [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020026#include <linux/kthread.h>
Chris Mason593060d2008-03-25 16:50:33 -040027#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050028#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040029#include "ctree.h"
30#include "extent_map.h"
31#include "disk-io.h"
32#include "transaction.h"
33#include "print-tree.h"
34#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040035#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010036#include "check-integrity.h"
Chris Mason0b86a832008-03-24 15:01:56 -040037
Yan Zheng2b820322008-11-17 21:11:30 -050038static int init_first_rw_device(struct btrfs_trans_handle *trans,
39 struct btrfs_root *root,
40 struct btrfs_device *device);
41static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
42
Chris Mason8a4b83c2008-03-24 15:02:07 -040043static DEFINE_MUTEX(uuid_mutex);
44static LIST_HEAD(fs_uuids);
45
Chris Mason7d9eb122008-07-08 14:19:17 -040046static void lock_chunks(struct btrfs_root *root)
47{
Chris Mason7d9eb122008-07-08 14:19:17 -040048 mutex_lock(&root->fs_info->chunk_mutex);
49}
50
51static void unlock_chunks(struct btrfs_root *root)
52{
Chris Mason7d9eb122008-07-08 14:19:17 -040053 mutex_unlock(&root->fs_info->chunk_mutex);
54}
55
Yan Zhenge4404d62008-12-12 10:03:26 -050056static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
57{
58 struct btrfs_device *device;
59 WARN_ON(fs_devices->opened);
60 while (!list_empty(&fs_devices->devices)) {
61 device = list_entry(fs_devices->devices.next,
62 struct btrfs_device, dev_list);
63 list_del(&device->dev_list);
64 kfree(device->name);
65 kfree(device);
66 }
67 kfree(fs_devices);
68}
69
Jeff Mahoney143bede2012-03-01 14:56:26 +010070void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040071{
72 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040073
Yan Zheng2b820322008-11-17 21:11:30 -050074 while (!list_empty(&fs_uuids)) {
75 fs_devices = list_entry(fs_uuids.next,
76 struct btrfs_fs_devices, list);
77 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050078 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040079 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040080}
81
Chris Masona1b32a52008-09-05 16:09:51 -040082static noinline struct btrfs_device *__find_device(struct list_head *head,
83 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040084{
85 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040086
Qinghuang Fengc6e30872009-01-21 10:59:08 -050087 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040088 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040089 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040090 return dev;
Chris Masona4437552008-04-18 10:29:38 -040091 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040092 }
93 return NULL;
94}
95
Chris Masona1b32a52008-09-05 16:09:51 -040096static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040097{
Chris Mason8a4b83c2008-03-24 15:02:07 -040098 struct btrfs_fs_devices *fs_devices;
99
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500100 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
102 return fs_devices;
103 }
104 return NULL;
105}
106
Chris Masonffbd5172009-04-20 15:50:09 -0400107static void requeue_list(struct btrfs_pending_bios *pending_bios,
108 struct bio *head, struct bio *tail)
109{
110
111 struct bio *old_head;
112
113 old_head = pending_bios->head;
114 pending_bios->head = head;
115 if (pending_bios->tail)
116 tail->bi_next = old_head;
117 else
118 pending_bios->tail = tail;
119}
120
Chris Mason8b712842008-06-11 16:50:36 -0400121/*
122 * we try to collect pending bios for a device so we don't get a large
123 * number of procs sending bios down to the same device. This greatly
124 * improves the schedulers ability to collect and merge the bios.
125 *
126 * But, it also turns into a long list of bios to process and that is sure
127 * to eventually make the worker thread block. The solution here is to
128 * make some progress and then put this work struct back at the end of
129 * the list if the block device is congested. This way, multiple devices
130 * can make progress from a single worker thread.
131 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100132static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400133{
134 struct bio *pending;
135 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400136 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400137 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400138 struct bio *tail;
139 struct bio *cur;
140 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400141 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400142 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400143 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400144 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400145 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000146 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400147 struct blk_plug plug;
148
149 /*
150 * this function runs all the bios we've collected for
151 * a particular device. We don't want to wander off to
152 * another device without first sending all of these down.
153 * So, setup a plug here and finish it off before we return
154 */
155 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400156
Chris Masonbedf7622009-04-03 10:32:58 -0400157 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400158 fs_info = device->dev_root->fs_info;
159 limit = btrfs_async_submit_limit(fs_info);
160 limit = limit * 2 / 3;
161
Chris Mason8b712842008-06-11 16:50:36 -0400162loop:
163 spin_lock(&device->io_lock);
164
Chris Masona6837052009-02-04 09:19:41 -0500165loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400166 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400167
Chris Mason8b712842008-06-11 16:50:36 -0400168 /* take all the bios off the list at once and process them
169 * later on (without the lock held). But, remember the
170 * tail and other pointers so the bios can be properly reinserted
171 * into the list if we hit congestion
172 */
Chris Masond84275c2009-06-09 15:39:08 -0400173 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400174 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400175 force_reg = 1;
176 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400177 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400178 force_reg = 0;
179 }
Chris Masonffbd5172009-04-20 15:50:09 -0400180
181 pending = pending_bios->head;
182 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400183 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400184
185 /*
186 * if pending was null this time around, no bios need processing
187 * at all and we can stop. Otherwise it'll loop back up again
188 * and do an additional check so no bios are missed.
189 *
190 * device->running_pending is used to synchronize with the
191 * schedule_bio code.
192 */
Chris Masonffbd5172009-04-20 15:50:09 -0400193 if (device->pending_sync_bios.head == NULL &&
194 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400195 again = 0;
196 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400197 } else {
198 again = 1;
199 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400200 }
Chris Masonffbd5172009-04-20 15:50:09 -0400201
202 pending_bios->head = NULL;
203 pending_bios->tail = NULL;
204
Chris Mason8b712842008-06-11 16:50:36 -0400205 spin_unlock(&device->io_lock);
206
Chris Masond3977122009-01-05 21:25:51 -0500207 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400208
209 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400210 /* we want to work on both lists, but do more bios on the
211 * sync list than the regular list
212 */
213 if ((num_run > 32 &&
214 pending_bios != &device->pending_sync_bios &&
215 device->pending_sync_bios.head) ||
216 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
217 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400218 spin_lock(&device->io_lock);
219 requeue_list(pending_bios, pending, tail);
220 goto loop_lock;
221 }
222
Chris Mason8b712842008-06-11 16:50:36 -0400223 cur = pending;
224 pending = pending->bi_next;
225 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400226 atomic_dec(&fs_info->nr_async_bios);
227
228 if (atomic_read(&fs_info->nr_async_bios) < limit &&
229 waitqueue_active(&fs_info->async_submit_wait))
230 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400231
232 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400233
Chris Mason2ab1ba62011-08-04 14:28:36 -0400234 /*
235 * if we're doing the sync list, record that our
236 * plug has some sync requests on it
237 *
238 * If we're doing the regular list and there are
239 * sync requests sitting around, unplug before
240 * we add more
241 */
242 if (pending_bios == &device->pending_sync_bios) {
243 sync_pending = 1;
244 } else if (sync_pending) {
245 blk_finish_plug(&plug);
246 blk_start_plug(&plug);
247 sync_pending = 0;
248 }
249
Stefan Behrens21adbd52011-11-09 13:44:05 +0100250 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400251 num_run++;
252 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100253 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400254 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400255
256 /*
257 * we made progress, there is more work to do and the bdi
258 * is now congested. Back off and let other work structs
259 * run instead
260 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400261 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500262 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400263 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400264
Chris Masonb765ead2009-04-03 10:27:10 -0400265 ioc = current->io_context;
266
267 /*
268 * the main goal here is that we don't want to
269 * block if we're going to be able to submit
270 * more requests without blocking.
271 *
272 * This code does two great things, it pokes into
273 * the elevator code from a filesystem _and_
274 * it makes assumptions about how batching works.
275 */
276 if (ioc && ioc->nr_batch_requests > 0 &&
277 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
278 (last_waited == 0 ||
279 ioc->last_waited == last_waited)) {
280 /*
281 * we want to go through our batch of
282 * requests and stop. So, we copy out
283 * the ioc->last_waited time and test
284 * against it before looping
285 */
286 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100287 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400288 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400289 continue;
290 }
Chris Mason8b712842008-06-11 16:50:36 -0400291 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400292 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500293 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400294
295 spin_unlock(&device->io_lock);
296 btrfs_requeue_work(&device->work);
297 goto done;
298 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500299 /* unplug every 64 requests just for good measure */
300 if (batch_run % 64 == 0) {
301 blk_finish_plug(&plug);
302 blk_start_plug(&plug);
303 sync_pending = 0;
304 }
Chris Mason8b712842008-06-11 16:50:36 -0400305 }
Chris Masonffbd5172009-04-20 15:50:09 -0400306
Chris Mason51684082010-03-10 15:33:32 -0500307 cond_resched();
308 if (again)
309 goto loop;
310
311 spin_lock(&device->io_lock);
312 if (device->pending_bios.head || device->pending_sync_bios.head)
313 goto loop_lock;
314 spin_unlock(&device->io_lock);
315
Chris Mason8b712842008-06-11 16:50:36 -0400316done:
Chris Mason211588a2011-04-19 20:12:40 -0400317 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400318}
319
Christoph Hellwigb2950862008-12-02 09:54:17 -0500320static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400321{
322 struct btrfs_device *device;
323
324 device = container_of(work, struct btrfs_device, work);
325 run_scheduled_bios(device);
326}
327
Chris Masona1b32a52008-09-05 16:09:51 -0400328static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400329 struct btrfs_super_block *disk_super,
330 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
331{
332 struct btrfs_device *device;
333 struct btrfs_fs_devices *fs_devices;
334 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000335 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400336
337 fs_devices = find_fsid(disk_super->fsid);
338 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400339 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400340 if (!fs_devices)
341 return -ENOMEM;
342 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400343 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400344 list_add(&fs_devices->list, &fs_uuids);
345 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
346 fs_devices->latest_devid = devid;
347 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400348 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400349 device = NULL;
350 } else {
Chris Masona4437552008-04-18 10:29:38 -0400351 device = __find_device(&fs_devices->devices, devid,
352 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400353 }
354 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500355 if (fs_devices->opened)
356 return -EBUSY;
357
Chris Mason8a4b83c2008-03-24 15:02:07 -0400358 device = kzalloc(sizeof(*device), GFP_NOFS);
359 if (!device) {
360 /* we can safely leave the fs_devices entry around */
361 return -ENOMEM;
362 }
363 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400364 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400365 memcpy(device->uuid, disk_super->dev_item.uuid,
366 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400367 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400368 device->name = kstrdup(path, GFP_NOFS);
369 if (!device->name) {
370 kfree(device);
371 return -ENOMEM;
372 }
Yan Zheng2b820322008-11-17 21:11:30 -0500373 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400374
Arne Jansen90519d62011-05-23 14:30:00 +0200375 /* init readahead state */
376 spin_lock_init(&device->reada_lock);
377 device->reada_curr_zone = NULL;
378 atomic_set(&device->reada_in_flight, 0);
379 device->reada_next = 0;
380 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
381 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
382
Chris Masone5e9a522009-06-10 15:17:02 -0400383 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000384 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400385 mutex_unlock(&fs_devices->device_list_mutex);
386
Yan Zheng2b820322008-11-17 21:11:30 -0500387 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400388 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500389 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000390 name = kstrdup(path, GFP_NOFS);
391 if (!name)
392 return -ENOMEM;
393 kfree(device->name);
394 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500395 if (device->missing) {
396 fs_devices->missing_devices--;
397 device->missing = 0;
398 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400399 }
400
401 if (found_transid > fs_devices->latest_trans) {
402 fs_devices->latest_devid = devid;
403 fs_devices->latest_trans = found_transid;
404 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400405 *fs_devices_ret = fs_devices;
406 return 0;
407}
408
Yan Zhenge4404d62008-12-12 10:03:26 -0500409static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
410{
411 struct btrfs_fs_devices *fs_devices;
412 struct btrfs_device *device;
413 struct btrfs_device *orig_dev;
414
415 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
416 if (!fs_devices)
417 return ERR_PTR(-ENOMEM);
418
419 INIT_LIST_HEAD(&fs_devices->devices);
420 INIT_LIST_HEAD(&fs_devices->alloc_list);
421 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400422 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500423 fs_devices->latest_devid = orig->latest_devid;
424 fs_devices->latest_trans = orig->latest_trans;
425 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
426
Xiao Guangrong46224702011-04-20 10:08:47 +0000427 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500428 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
429 device = kzalloc(sizeof(*device), GFP_NOFS);
430 if (!device)
431 goto error;
432
433 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400434 if (!device->name) {
435 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500436 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400437 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500438
439 device->devid = orig_dev->devid;
440 device->work.func = pending_bios_fn;
441 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500442 spin_lock_init(&device->io_lock);
443 INIT_LIST_HEAD(&device->dev_list);
444 INIT_LIST_HEAD(&device->dev_alloc_list);
445
446 list_add(&device->dev_list, &fs_devices->devices);
447 device->fs_devices = fs_devices;
448 fs_devices->num_devices++;
449 }
450 return fs_devices;
451error:
452 free_fs_devices(fs_devices);
453 return ERR_PTR(-ENOMEM);
454}
455
Jeff Mahoney143bede2012-03-01 14:56:26 +0100456void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
Chris Masondfe25022008-05-13 13:46:40 -0400457{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500458 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400459
Chris Masona6b0d5c2012-02-20 20:53:43 -0500460 struct block_device *latest_bdev = NULL;
461 u64 latest_devid = 0;
462 u64 latest_transid = 0;
463
Chris Masondfe25022008-05-13 13:46:40 -0400464 mutex_lock(&uuid_mutex);
465again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000466 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500467 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500468 if (device->in_fs_metadata) {
469 if (!latest_transid ||
470 device->generation > latest_transid) {
471 latest_devid = device->devid;
472 latest_transid = device->generation;
473 latest_bdev = device->bdev;
474 }
Yan Zheng2b820322008-11-17 21:11:30 -0500475 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500476 }
Yan Zheng2b820322008-11-17 21:11:30 -0500477
478 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100479 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500480 device->bdev = NULL;
481 fs_devices->open_devices--;
482 }
483 if (device->writeable) {
484 list_del_init(&device->dev_alloc_list);
485 device->writeable = 0;
486 fs_devices->rw_devices--;
487 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500488 list_del_init(&device->dev_list);
489 fs_devices->num_devices--;
490 kfree(device->name);
491 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400492 }
Yan Zheng2b820322008-11-17 21:11:30 -0500493
494 if (fs_devices->seed) {
495 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500496 goto again;
497 }
498
Chris Masona6b0d5c2012-02-20 20:53:43 -0500499 fs_devices->latest_bdev = latest_bdev;
500 fs_devices->latest_devid = latest_devid;
501 fs_devices->latest_trans = latest_transid;
502
Chris Masondfe25022008-05-13 13:46:40 -0400503 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400504}
Chris Masona0af4692008-05-13 16:03:06 -0400505
Xiao Guangrong1f781602011-04-20 10:09:16 +0000506static void __free_device(struct work_struct *work)
507{
508 struct btrfs_device *device;
509
510 device = container_of(work, struct btrfs_device, rcu_work);
511
512 if (device->bdev)
513 blkdev_put(device->bdev, device->mode);
514
515 kfree(device->name);
516 kfree(device);
517}
518
519static void free_device(struct rcu_head *head)
520{
521 struct btrfs_device *device;
522
523 device = container_of(head, struct btrfs_device, rcu);
524
525 INIT_WORK(&device->rcu_work, __free_device);
526 schedule_work(&device->rcu_work);
527}
528
Yan Zheng2b820322008-11-17 21:11:30 -0500529static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400530{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400531 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500532
Yan Zheng2b820322008-11-17 21:11:30 -0500533 if (--fs_devices->opened > 0)
534 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400535
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000536 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500537 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000538 struct btrfs_device *new_device;
539
540 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400541 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000542
Yan Zheng2b820322008-11-17 21:11:30 -0500543 if (device->writeable) {
544 list_del_init(&device->dev_alloc_list);
545 fs_devices->rw_devices--;
546 }
547
Josef Bacikd5e20032011-08-04 14:52:27 +0000548 if (device->can_discard)
549 fs_devices->num_can_discard--;
550
Xiao Guangrong1f781602011-04-20 10:09:16 +0000551 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100552 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000553 memcpy(new_device, device, sizeof(*new_device));
554 new_device->name = kstrdup(device->name, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100555 BUG_ON(device->name && !new_device->name); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000556 new_device->bdev = NULL;
557 new_device->writeable = 0;
558 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000559 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000560 list_replace_rcu(&device->dev_list, &new_device->dev_list);
561
562 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400563 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000564 mutex_unlock(&fs_devices->device_list_mutex);
565
Yan Zhenge4404d62008-12-12 10:03:26 -0500566 WARN_ON(fs_devices->open_devices);
567 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500568 fs_devices->opened = 0;
569 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500570
Chris Mason8a4b83c2008-03-24 15:02:07 -0400571 return 0;
572}
573
Yan Zheng2b820322008-11-17 21:11:30 -0500574int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
575{
Yan Zhenge4404d62008-12-12 10:03:26 -0500576 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500577 int ret;
578
579 mutex_lock(&uuid_mutex);
580 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500581 if (!fs_devices->opened) {
582 seed_devices = fs_devices->seed;
583 fs_devices->seed = NULL;
584 }
Yan Zheng2b820322008-11-17 21:11:30 -0500585 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500586
587 while (seed_devices) {
588 fs_devices = seed_devices;
589 seed_devices = fs_devices->seed;
590 __btrfs_close_devices(fs_devices);
591 free_fs_devices(fs_devices);
592 }
Yan Zheng2b820322008-11-17 21:11:30 -0500593 return ret;
594}
595
Yan Zhenge4404d62008-12-12 10:03:26 -0500596static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
597 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400598{
Josef Bacikd5e20032011-08-04 14:52:27 +0000599 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400600 struct block_device *bdev;
601 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400602 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400603 struct block_device *latest_bdev = NULL;
604 struct buffer_head *bh;
605 struct btrfs_super_block *disk_super;
606 u64 latest_devid = 0;
607 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400608 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500609 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400610 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400611
Tejun Heod4d77622010-11-13 11:55:18 +0100612 flags |= FMODE_EXCL;
613
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500614 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400615 if (device->bdev)
616 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400617 if (!device->name)
618 continue;
619
Tejun Heod4d77622010-11-13 11:55:18 +0100620 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400621 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500622 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400623 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400624 }
Chris Mason3c4bb262012-03-27 18:56:56 -0400625 filemap_write_and_wait(bdev->bd_inode->i_mapping);
626 invalidate_bdev(bdev);
Chris Masona061fc82008-05-07 11:43:44 -0400627 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400628
Yan Zhenga512bbf2008-12-08 16:46:26 -0500629 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300630 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400631 goto error_close;
632
633 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000634 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400635 if (devid != device->devid)
636 goto error_brelse;
637
Yan Zheng2b820322008-11-17 21:11:30 -0500638 if (memcmp(device->uuid, disk_super->dev_item.uuid,
639 BTRFS_UUID_SIZE))
640 goto error_brelse;
641
642 device->generation = btrfs_super_generation(disk_super);
643 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400644 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500645 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400646 latest_bdev = bdev;
647 }
648
Yan Zheng2b820322008-11-17 21:11:30 -0500649 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
650 device->writeable = 0;
651 } else {
652 device->writeable = !bdev_read_only(bdev);
653 seeding = 0;
654 }
655
Josef Bacikd5e20032011-08-04 14:52:27 +0000656 q = bdev_get_queue(bdev);
657 if (blk_queue_discard(q)) {
658 device->can_discard = 1;
659 fs_devices->num_can_discard++;
660 }
661
Chris Mason8a4b83c2008-03-24 15:02:07 -0400662 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400663 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500664 device->mode = flags;
665
Chris Masonc2898112009-06-10 09:51:32 -0400666 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
667 fs_devices->rotating = 1;
668
Chris Masona0af4692008-05-13 16:03:06 -0400669 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500670 if (device->writeable) {
671 fs_devices->rw_devices++;
672 list_add(&device->dev_alloc_list,
673 &fs_devices->alloc_list);
674 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000675 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400676 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400677
Chris Masona0af4692008-05-13 16:03:06 -0400678error_brelse:
679 brelse(bh);
680error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100681 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400682error:
683 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400684 }
Chris Masona0af4692008-05-13 16:03:06 -0400685 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300686 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400687 goto out;
688 }
Yan Zheng2b820322008-11-17 21:11:30 -0500689 fs_devices->seeding = seeding;
690 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400691 fs_devices->latest_bdev = latest_bdev;
692 fs_devices->latest_devid = latest_devid;
693 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500694 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400695out:
Yan Zheng2b820322008-11-17 21:11:30 -0500696 return ret;
697}
698
699int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500700 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500701{
702 int ret;
703
704 mutex_lock(&uuid_mutex);
705 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500706 fs_devices->opened++;
707 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500708 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500709 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500710 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400711 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400712 return ret;
713}
714
Christoph Hellwig97288f22008-12-02 06:36:09 -0500715int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400716 struct btrfs_fs_devices **fs_devices_ret)
717{
718 struct btrfs_super_block *disk_super;
719 struct block_device *bdev;
720 struct buffer_head *bh;
721 int ret;
722 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400723 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400724
Tejun Heod4d77622010-11-13 11:55:18 +0100725 flags |= FMODE_EXCL;
726 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400727
728 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400729 ret = PTR_ERR(bdev);
730 goto error;
731 }
732
Al Viro10f63272011-11-17 15:05:22 -0500733 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400734 ret = set_blocksize(bdev, 4096);
735 if (ret)
736 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500737 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400738 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000739 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400740 goto error_close;
741 }
742 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000743 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400744 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400745 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500746 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200747 else
748 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500749 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500750 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400751 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
752
Chris Mason8a4b83c2008-03-24 15:02:07 -0400753 brelse(bh);
754error_close:
Al Viro10f63272011-11-17 15:05:22 -0500755 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100756 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400757error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758 return ret;
759}
Chris Mason0b86a832008-03-24 15:01:56 -0400760
Miao Xie6d07bce2011-01-05 10:07:31 +0000761/* helper to account the used device space in the range */
762int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
763 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400764{
765 struct btrfs_key key;
766 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000767 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500768 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000769 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400770 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000771 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400772 struct extent_buffer *l;
773
Miao Xie6d07bce2011-01-05 10:07:31 +0000774 *length = 0;
775
776 if (start >= device->total_bytes)
777 return 0;
778
Yan Zheng2b820322008-11-17 21:11:30 -0500779 path = btrfs_alloc_path();
780 if (!path)
781 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400782 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400783
Chris Mason0b86a832008-03-24 15:01:56 -0400784 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000785 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400786 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000787
788 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400789 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000790 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400791 if (ret > 0) {
792 ret = btrfs_previous_item(root, path, key.objectid, key.type);
793 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000794 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400795 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000796
Chris Mason0b86a832008-03-24 15:01:56 -0400797 while (1) {
798 l = path->nodes[0];
799 slot = path->slots[0];
800 if (slot >= btrfs_header_nritems(l)) {
801 ret = btrfs_next_leaf(root, path);
802 if (ret == 0)
803 continue;
804 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000805 goto out;
806
807 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400808 }
809 btrfs_item_key_to_cpu(l, &key, slot);
810
811 if (key.objectid < device->devid)
812 goto next;
813
814 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000815 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400816
Chris Masond3977122009-01-05 21:25:51 -0500817 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400818 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400819
Chris Mason0b86a832008-03-24 15:01:56 -0400820 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000821 extent_end = key.offset + btrfs_dev_extent_length(l,
822 dev_extent);
823 if (key.offset <= start && extent_end > end) {
824 *length = end - start + 1;
825 break;
826 } else if (key.offset <= start && extent_end > start)
827 *length += extent_end - start;
828 else if (key.offset > start && extent_end <= end)
829 *length += extent_end - key.offset;
830 else if (key.offset > start && key.offset <= end) {
831 *length += end - key.offset + 1;
832 break;
833 } else if (key.offset > end)
834 break;
835
836next:
837 path->slots[0]++;
838 }
839 ret = 0;
840out:
841 btrfs_free_path(path);
842 return ret;
843}
844
Chris Mason0b86a832008-03-24 15:01:56 -0400845/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000846 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000847 * @device: the device which we search the free space in
848 * @num_bytes: the size of the free space that we need
849 * @start: store the start of the free space.
850 * @len: the size of the free space. that we find, or the size of the max
851 * free space if we don't find suitable free space
852 *
Chris Mason0b86a832008-03-24 15:01:56 -0400853 * this uses a pretty simple search, the expectation is that it is
854 * called very infrequently and that a given device has a small number
855 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000856 *
857 * @start is used to store the start of the free space if we find. But if we
858 * don't find suitable free space, it will be used to store the start position
859 * of the max free space.
860 *
861 * @len is used to store the size of the free space that we find.
862 * But if we don't find suitable free space, it is used to store the size of
863 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400864 */
Li Zefan125ccb02011-12-08 15:07:24 +0800865int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000866 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400867{
868 struct btrfs_key key;
869 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000870 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400871 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000872 u64 hole_size;
873 u64 max_hole_start;
874 u64 max_hole_size;
875 u64 extent_end;
876 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400877 u64 search_end = device->total_bytes;
878 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000879 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400880 struct extent_buffer *l;
881
Chris Mason0b86a832008-03-24 15:01:56 -0400882 /* FIXME use last free of some kind */
883
884 /* we don't want to overwrite the superblock on the drive,
885 * so we make sure to start at an offset of at least 1MB
886 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200887 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400888
Miao Xie7bfc8372011-01-05 10:07:26 +0000889 max_hole_start = search_start;
890 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000891 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000892
893 if (search_start >= search_end) {
894 ret = -ENOSPC;
895 goto error;
896 }
897
898 path = btrfs_alloc_path();
899 if (!path) {
900 ret = -ENOMEM;
901 goto error;
902 }
903 path->reada = 2;
904
Chris Mason0b86a832008-03-24 15:01:56 -0400905 key.objectid = device->devid;
906 key.offset = search_start;
907 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000908
Li Zefan125ccb02011-12-08 15:07:24 +0800909 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400910 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000911 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400912 if (ret > 0) {
913 ret = btrfs_previous_item(root, path, key.objectid, key.type);
914 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000915 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400916 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000917
Chris Mason0b86a832008-03-24 15:01:56 -0400918 while (1) {
919 l = path->nodes[0];
920 slot = path->slots[0];
921 if (slot >= btrfs_header_nritems(l)) {
922 ret = btrfs_next_leaf(root, path);
923 if (ret == 0)
924 continue;
925 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000926 goto out;
927
928 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400929 }
930 btrfs_item_key_to_cpu(l, &key, slot);
931
932 if (key.objectid < device->devid)
933 goto next;
934
935 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000936 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400937
Chris Mason0b86a832008-03-24 15:01:56 -0400938 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
939 goto next;
940
Miao Xie7bfc8372011-01-05 10:07:26 +0000941 if (key.offset > search_start) {
942 hole_size = key.offset - search_start;
943
944 if (hole_size > max_hole_size) {
945 max_hole_start = search_start;
946 max_hole_size = hole_size;
947 }
948
949 /*
950 * If this free space is greater than which we need,
951 * it must be the max free space that we have found
952 * until now, so max_hole_start must point to the start
953 * of this free space and the length of this free space
954 * is stored in max_hole_size. Thus, we return
955 * max_hole_start and max_hole_size and go back to the
956 * caller.
957 */
958 if (hole_size >= num_bytes) {
959 ret = 0;
960 goto out;
961 }
962 }
963
Chris Mason0b86a832008-03-24 15:01:56 -0400964 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000965 extent_end = key.offset + btrfs_dev_extent_length(l,
966 dev_extent);
967 if (extent_end > search_start)
968 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400969next:
970 path->slots[0]++;
971 cond_resched();
972 }
Chris Mason0b86a832008-03-24 15:01:56 -0400973
liubo38c01b92011-08-02 02:39:03 +0000974 /*
975 * At this point, search_start should be the end of
976 * allocated dev extents, and when shrinking the device,
977 * search_end may be smaller than search_start.
978 */
979 if (search_end > search_start)
980 hole_size = search_end - search_start;
981
Miao Xie7bfc8372011-01-05 10:07:26 +0000982 if (hole_size > max_hole_size) {
983 max_hole_start = search_start;
984 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400985 }
Chris Mason0b86a832008-03-24 15:01:56 -0400986
Miao Xie7bfc8372011-01-05 10:07:26 +0000987 /* See above. */
988 if (hole_size < num_bytes)
989 ret = -ENOSPC;
990 else
991 ret = 0;
992
993out:
Yan Zheng2b820322008-11-17 21:11:30 -0500994 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000995error:
996 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000997 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000998 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400999 return ret;
1000}
1001
Christoph Hellwigb2950862008-12-02 09:54:17 -05001002static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001003 struct btrfs_device *device,
1004 u64 start)
1005{
1006 int ret;
1007 struct btrfs_path *path;
1008 struct btrfs_root *root = device->dev_root;
1009 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001010 struct btrfs_key found_key;
1011 struct extent_buffer *leaf = NULL;
1012 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001013
1014 path = btrfs_alloc_path();
1015 if (!path)
1016 return -ENOMEM;
1017
1018 key.objectid = device->devid;
1019 key.offset = start;
1020 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001021again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001022 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001023 if (ret > 0) {
1024 ret = btrfs_previous_item(root, path, key.objectid,
1025 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001026 if (ret)
1027 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001028 leaf = path->nodes[0];
1029 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1030 extent = btrfs_item_ptr(leaf, path->slots[0],
1031 struct btrfs_dev_extent);
1032 BUG_ON(found_key.offset > start || found_key.offset +
1033 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001034 key = found_key;
1035 btrfs_release_path(path);
1036 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001037 } else if (ret == 0) {
1038 leaf = path->nodes[0];
1039 extent = btrfs_item_ptr(leaf, path->slots[0],
1040 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001041 } else {
1042 btrfs_error(root->fs_info, ret, "Slot search failed");
1043 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001044 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001045
Josef Bacik2bf64752011-09-26 17:12:22 -04001046 if (device->bytes_used > 0) {
1047 u64 len = btrfs_dev_extent_length(leaf, extent);
1048 device->bytes_used -= len;
1049 spin_lock(&root->fs_info->free_chunk_lock);
1050 root->fs_info->free_chunk_space += len;
1051 spin_unlock(&root->fs_info->free_chunk_lock);
1052 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001053 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001054 if (ret) {
1055 btrfs_error(root->fs_info, ret,
1056 "Failed to remove dev extent item");
1057 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001058out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001059 btrfs_free_path(path);
1060 return ret;
1061}
1062
Yan Zheng2b820322008-11-17 21:11:30 -05001063int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001064 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001065 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001066 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001067{
1068 int ret;
1069 struct btrfs_path *path;
1070 struct btrfs_root *root = device->dev_root;
1071 struct btrfs_dev_extent *extent;
1072 struct extent_buffer *leaf;
1073 struct btrfs_key key;
1074
Chris Masondfe25022008-05-13 13:46:40 -04001075 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001076 path = btrfs_alloc_path();
1077 if (!path)
1078 return -ENOMEM;
1079
Chris Mason0b86a832008-03-24 15:01:56 -04001080 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001081 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001082 key.type = BTRFS_DEV_EXTENT_KEY;
1083 ret = btrfs_insert_empty_item(trans, root, path, &key,
1084 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001085 if (ret)
1086 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001087
1088 leaf = path->nodes[0];
1089 extent = btrfs_item_ptr(leaf, path->slots[0],
1090 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001091 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1092 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1093 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1094
1095 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1096 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1097 BTRFS_UUID_SIZE);
1098
Chris Mason0b86a832008-03-24 15:01:56 -04001099 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1100 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001101out:
Chris Mason0b86a832008-03-24 15:01:56 -04001102 btrfs_free_path(path);
1103 return ret;
1104}
1105
Chris Masona1b32a52008-09-05 16:09:51 -04001106static noinline int find_next_chunk(struct btrfs_root *root,
1107 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001108{
1109 struct btrfs_path *path;
1110 int ret;
1111 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001112 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001113 struct btrfs_key found_key;
1114
1115 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001116 if (!path)
1117 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001118
Chris Masone17cade2008-04-15 15:41:47 -04001119 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001120 key.offset = (u64)-1;
1121 key.type = BTRFS_CHUNK_ITEM_KEY;
1122
1123 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1124 if (ret < 0)
1125 goto error;
1126
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001127 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001128
1129 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1130 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001131 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001132 } else {
1133 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1134 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001135 if (found_key.objectid != objectid)
1136 *offset = 0;
1137 else {
1138 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1139 struct btrfs_chunk);
1140 *offset = found_key.offset +
1141 btrfs_chunk_length(path->nodes[0], chunk);
1142 }
Chris Mason0b86a832008-03-24 15:01:56 -04001143 }
1144 ret = 0;
1145error:
1146 btrfs_free_path(path);
1147 return ret;
1148}
1149
Yan Zheng2b820322008-11-17 21:11:30 -05001150static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001151{
1152 int ret;
1153 struct btrfs_key key;
1154 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001155 struct btrfs_path *path;
1156
1157 root = root->fs_info->chunk_root;
1158
1159 path = btrfs_alloc_path();
1160 if (!path)
1161 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001162
1163 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1164 key.type = BTRFS_DEV_ITEM_KEY;
1165 key.offset = (u64)-1;
1166
1167 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1168 if (ret < 0)
1169 goto error;
1170
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001171 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001172
1173 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1174 BTRFS_DEV_ITEM_KEY);
1175 if (ret) {
1176 *objectid = 1;
1177 } else {
1178 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1179 path->slots[0]);
1180 *objectid = found_key.offset + 1;
1181 }
1182 ret = 0;
1183error:
Yan Zheng2b820322008-11-17 21:11:30 -05001184 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001185 return ret;
1186}
1187
1188/*
1189 * the device information is stored in the chunk root
1190 * the btrfs_device struct should be fully filled in
1191 */
1192int btrfs_add_device(struct btrfs_trans_handle *trans,
1193 struct btrfs_root *root,
1194 struct btrfs_device *device)
1195{
1196 int ret;
1197 struct btrfs_path *path;
1198 struct btrfs_dev_item *dev_item;
1199 struct extent_buffer *leaf;
1200 struct btrfs_key key;
1201 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001202
1203 root = root->fs_info->chunk_root;
1204
1205 path = btrfs_alloc_path();
1206 if (!path)
1207 return -ENOMEM;
1208
Chris Mason0b86a832008-03-24 15:01:56 -04001209 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1210 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001211 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001212
1213 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001214 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001215 if (ret)
1216 goto out;
1217
1218 leaf = path->nodes[0];
1219 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1220
1221 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001222 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001223 btrfs_set_device_type(leaf, dev_item, device->type);
1224 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1225 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1226 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001227 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1228 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001229 btrfs_set_device_group(leaf, dev_item, 0);
1230 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1231 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001232 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001233
Chris Mason0b86a832008-03-24 15:01:56 -04001234 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001235 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001236 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1237 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001238 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001239
Yan Zheng2b820322008-11-17 21:11:30 -05001240 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001241out:
1242 btrfs_free_path(path);
1243 return ret;
1244}
Chris Mason8f18cf12008-04-25 16:53:30 -04001245
Chris Masona061fc82008-05-07 11:43:44 -04001246static int btrfs_rm_dev_item(struct btrfs_root *root,
1247 struct btrfs_device *device)
1248{
1249 int ret;
1250 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001251 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001252 struct btrfs_trans_handle *trans;
1253
1254 root = root->fs_info->chunk_root;
1255
1256 path = btrfs_alloc_path();
1257 if (!path)
1258 return -ENOMEM;
1259
Yan, Zhenga22285a2010-05-16 10:48:46 -04001260 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001261 if (IS_ERR(trans)) {
1262 btrfs_free_path(path);
1263 return PTR_ERR(trans);
1264 }
Chris Masona061fc82008-05-07 11:43:44 -04001265 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1266 key.type = BTRFS_DEV_ITEM_KEY;
1267 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001268 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001269
1270 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1271 if (ret < 0)
1272 goto out;
1273
1274 if (ret > 0) {
1275 ret = -ENOENT;
1276 goto out;
1277 }
1278
1279 ret = btrfs_del_item(trans, root, path);
1280 if (ret)
1281 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001282out:
1283 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001284 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001285 btrfs_commit_transaction(trans, root);
1286 return ret;
1287}
1288
1289int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1290{
1291 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001292 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001293 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001294 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001295 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001296 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001297 u64 all_avail;
1298 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001299 u64 num_devices;
1300 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001301 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001302 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001303
Chris Masona061fc82008-05-07 11:43:44 -04001304 mutex_lock(&uuid_mutex);
1305
1306 all_avail = root->fs_info->avail_data_alloc_bits |
1307 root->fs_info->avail_system_alloc_bits |
1308 root->fs_info->avail_metadata_alloc_bits;
1309
1310 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001311 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001312 printk(KERN_ERR "btrfs: unable to go below four devices "
1313 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001314 ret = -EINVAL;
1315 goto out;
1316 }
1317
1318 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001319 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001320 printk(KERN_ERR "btrfs: unable to go below two "
1321 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001322 ret = -EINVAL;
1323 goto out;
1324 }
1325
Chris Masondfe25022008-05-13 13:46:40 -04001326 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001327 struct list_head *devices;
1328 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001329
Chris Masondfe25022008-05-13 13:46:40 -04001330 device = NULL;
1331 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001332 /*
1333 * It is safe to read the devices since the volume_mutex
1334 * is held.
1335 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001336 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001337 if (tmp->in_fs_metadata && !tmp->bdev) {
1338 device = tmp;
1339 break;
1340 }
1341 }
1342 bdev = NULL;
1343 bh = NULL;
1344 disk_super = NULL;
1345 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001346 printk(KERN_ERR "btrfs: no missing devices found to "
1347 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001348 goto out;
1349 }
Chris Masondfe25022008-05-13 13:46:40 -04001350 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001351 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1352 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001353 if (IS_ERR(bdev)) {
1354 ret = PTR_ERR(bdev);
1355 goto out;
1356 }
1357
Yan Zheng2b820322008-11-17 21:11:30 -05001358 set_blocksize(bdev, 4096);
Chris Mason3c4bb262012-03-27 18:56:56 -04001359 invalidate_bdev(bdev);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001360 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001361 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001362 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001363 goto error_close;
1364 }
1365 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001366 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001367 dev_uuid = disk_super->dev_item.uuid;
1368 device = btrfs_find_device(root, devid, dev_uuid,
1369 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001370 if (!device) {
1371 ret = -ENOENT;
1372 goto error_brelse;
1373 }
Chris Masondfe25022008-05-13 13:46:40 -04001374 }
Yan Zheng2b820322008-11-17 21:11:30 -05001375
1376 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001377 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1378 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001379 ret = -EINVAL;
1380 goto error_brelse;
1381 }
1382
1383 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001384 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001385 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001386 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001387 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001388 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001389 }
Chris Masona061fc82008-05-07 11:43:44 -04001390
1391 ret = btrfs_shrink_device(device, 0);
1392 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001393 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001394
Chris Masona061fc82008-05-07 11:43:44 -04001395 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1396 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001397 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001398
Josef Bacik2bf64752011-09-26 17:12:22 -04001399 spin_lock(&root->fs_info->free_chunk_lock);
1400 root->fs_info->free_chunk_space = device->total_bytes -
1401 device->bytes_used;
1402 spin_unlock(&root->fs_info->free_chunk_lock);
1403
Yan Zheng2b820322008-11-17 21:11:30 -05001404 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001405 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001406
1407 /*
1408 * the device list mutex makes sure that we don't change
1409 * the device list while someone else is writing out all
1410 * the device supers.
1411 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001412
1413 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001414 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001415 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001416
Yan Zhenge4404d62008-12-12 10:03:26 -05001417 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001418
Chris Masoncd02dca2010-12-13 14:56:23 -05001419 if (device->missing)
1420 root->fs_info->fs_devices->missing_devices--;
1421
Yan Zheng2b820322008-11-17 21:11:30 -05001422 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1423 struct btrfs_device, dev_list);
1424 if (device->bdev == root->fs_info->sb->s_bdev)
1425 root->fs_info->sb->s_bdev = next_device->bdev;
1426 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1427 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1428
Xiao Guangrong1f781602011-04-20 10:09:16 +00001429 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001430 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001431
1432 call_rcu(&device->rcu, free_device);
1433 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001434
David Sterba6c417612011-04-13 15:41:04 +02001435 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1436 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001437
Xiao Guangrong1f781602011-04-20 10:09:16 +00001438 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001439 struct btrfs_fs_devices *fs_devices;
1440 fs_devices = root->fs_info->fs_devices;
1441 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001442 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001443 break;
1444 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001445 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001446 fs_devices->seed = cur_devices->seed;
1447 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001448 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001449 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001450 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001451 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001452 }
1453
1454 /*
1455 * at this point, the device is zero sized. We want to
1456 * remove it from the devices list and zero out the old super
1457 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001458 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001459 /* make sure this device isn't detected as part of
1460 * the FS anymore
1461 */
1462 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1463 set_buffer_dirty(bh);
1464 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001465 }
Chris Masona061fc82008-05-07 11:43:44 -04001466
Chris Masona061fc82008-05-07 11:43:44 -04001467 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001468
1469error_brelse:
1470 brelse(bh);
1471error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001472 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001473 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001474out:
1475 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001476 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001477error_undo:
1478 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001479 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001480 list_add(&device->dev_alloc_list,
1481 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001482 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001483 root->fs_info->fs_devices->rw_devices++;
1484 }
1485 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001486}
1487
Yan Zheng2b820322008-11-17 21:11:30 -05001488/*
1489 * does all the dirty work required for changing file system's UUID.
1490 */
Li Zefan125ccb02011-12-08 15:07:24 +08001491static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001492{
1493 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1494 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001495 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001496 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001497 struct btrfs_device *device;
1498 u64 super_flags;
1499
1500 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001501 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001502 return -EINVAL;
1503
Yan Zhenge4404d62008-12-12 10:03:26 -05001504 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1505 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001506 return -ENOMEM;
1507
Yan Zhenge4404d62008-12-12 10:03:26 -05001508 old_devices = clone_fs_devices(fs_devices);
1509 if (IS_ERR(old_devices)) {
1510 kfree(seed_devices);
1511 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001512 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001513
Yan Zheng2b820322008-11-17 21:11:30 -05001514 list_add(&old_devices->list, &fs_uuids);
1515
Yan Zhenge4404d62008-12-12 10:03:26 -05001516 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1517 seed_devices->opened = 1;
1518 INIT_LIST_HEAD(&seed_devices->devices);
1519 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001520 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001521
1522 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001523 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1524 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001525 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1526
Yan Zhenge4404d62008-12-12 10:03:26 -05001527 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1528 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1529 device->fs_devices = seed_devices;
1530 }
1531
Yan Zheng2b820322008-11-17 21:11:30 -05001532 fs_devices->seeding = 0;
1533 fs_devices->num_devices = 0;
1534 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001535 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001536
1537 generate_random_uuid(fs_devices->fsid);
1538 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1539 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1540 super_flags = btrfs_super_flags(disk_super) &
1541 ~BTRFS_SUPER_FLAG_SEEDING;
1542 btrfs_set_super_flags(disk_super, super_flags);
1543
1544 return 0;
1545}
1546
1547/*
1548 * strore the expected generation for seed devices in device items.
1549 */
1550static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1551 struct btrfs_root *root)
1552{
1553 struct btrfs_path *path;
1554 struct extent_buffer *leaf;
1555 struct btrfs_dev_item *dev_item;
1556 struct btrfs_device *device;
1557 struct btrfs_key key;
1558 u8 fs_uuid[BTRFS_UUID_SIZE];
1559 u8 dev_uuid[BTRFS_UUID_SIZE];
1560 u64 devid;
1561 int ret;
1562
1563 path = btrfs_alloc_path();
1564 if (!path)
1565 return -ENOMEM;
1566
1567 root = root->fs_info->chunk_root;
1568 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1569 key.offset = 0;
1570 key.type = BTRFS_DEV_ITEM_KEY;
1571
1572 while (1) {
1573 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1574 if (ret < 0)
1575 goto error;
1576
1577 leaf = path->nodes[0];
1578next_slot:
1579 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1580 ret = btrfs_next_leaf(root, path);
1581 if (ret > 0)
1582 break;
1583 if (ret < 0)
1584 goto error;
1585 leaf = path->nodes[0];
1586 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001587 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001588 continue;
1589 }
1590
1591 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1592 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1593 key.type != BTRFS_DEV_ITEM_KEY)
1594 break;
1595
1596 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1597 struct btrfs_dev_item);
1598 devid = btrfs_device_id(leaf, dev_item);
1599 read_extent_buffer(leaf, dev_uuid,
1600 (unsigned long)btrfs_device_uuid(dev_item),
1601 BTRFS_UUID_SIZE);
1602 read_extent_buffer(leaf, fs_uuid,
1603 (unsigned long)btrfs_device_fsid(dev_item),
1604 BTRFS_UUID_SIZE);
1605 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001606 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001607
1608 if (device->fs_devices->seeding) {
1609 btrfs_set_device_generation(leaf, dev_item,
1610 device->generation);
1611 btrfs_mark_buffer_dirty(leaf);
1612 }
1613
1614 path->slots[0]++;
1615 goto next_slot;
1616 }
1617 ret = 0;
1618error:
1619 btrfs_free_path(path);
1620 return ret;
1621}
1622
Chris Mason788f20e2008-04-28 15:29:42 -04001623int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1624{
Josef Bacikd5e20032011-08-04 14:52:27 +00001625 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001626 struct btrfs_trans_handle *trans;
1627 struct btrfs_device *device;
1628 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001629 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001630 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001631 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001632 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001633 int ret = 0;
1634
Yan Zheng2b820322008-11-17 21:11:30 -05001635 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1636 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001637
Li Zefana5d16332011-12-07 20:08:40 -05001638 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001639 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001640 if (IS_ERR(bdev))
1641 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001642
Yan Zheng2b820322008-11-17 21:11:30 -05001643 if (root->fs_info->fs_devices->seeding) {
1644 seeding_dev = 1;
1645 down_write(&sb->s_umount);
1646 mutex_lock(&uuid_mutex);
1647 }
1648
Chris Mason8c8bee12008-09-29 11:19:10 -04001649 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001650
Chris Mason788f20e2008-04-28 15:29:42 -04001651 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001652 /*
1653 * we have the volume lock, so we don't need the extra
1654 * device list mutex while reading the list here.
1655 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001656 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001657 if (device->bdev == bdev) {
1658 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001659 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001660 }
1661 }
1662
1663 device = kzalloc(sizeof(*device), GFP_NOFS);
1664 if (!device) {
1665 /* we can safely leave the fs_devices entry around */
1666 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001667 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001668 }
1669
Chris Mason788f20e2008-04-28 15:29:42 -04001670 device->name = kstrdup(device_path, GFP_NOFS);
1671 if (!device->name) {
1672 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001673 ret = -ENOMEM;
1674 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001675 }
Yan Zheng2b820322008-11-17 21:11:30 -05001676
1677 ret = find_next_devid(root, &device->devid);
1678 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001679 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001680 kfree(device);
1681 goto error;
1682 }
1683
Yan, Zhenga22285a2010-05-16 10:48:46 -04001684 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001685 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001686 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001687 kfree(device);
1688 ret = PTR_ERR(trans);
1689 goto error;
1690 }
1691
Yan Zheng2b820322008-11-17 21:11:30 -05001692 lock_chunks(root);
1693
Josef Bacikd5e20032011-08-04 14:52:27 +00001694 q = bdev_get_queue(bdev);
1695 if (blk_queue_discard(q))
1696 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001697 device->writeable = 1;
1698 device->work.func = pending_bios_fn;
1699 generate_random_uuid(device->uuid);
1700 spin_lock_init(&device->io_lock);
1701 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001702 device->io_width = root->sectorsize;
1703 device->io_align = root->sectorsize;
1704 device->sector_size = root->sectorsize;
1705 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001706 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001707 device->dev_root = root->fs_info->dev_root;
1708 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001709 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001710 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001711 set_blocksize(device->bdev, 4096);
1712
Yan Zheng2b820322008-11-17 21:11:30 -05001713 if (seeding_dev) {
1714 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001715 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001716 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001717 }
1718
1719 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001720
1721 /*
1722 * we don't want write_supers to jump in here with our device
1723 * half setup
1724 */
1725 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001726 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001727 list_add(&device->dev_alloc_list,
1728 &root->fs_info->fs_devices->alloc_list);
1729 root->fs_info->fs_devices->num_devices++;
1730 root->fs_info->fs_devices->open_devices++;
1731 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001732 if (device->can_discard)
1733 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001734 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1735
Josef Bacik2bf64752011-09-26 17:12:22 -04001736 spin_lock(&root->fs_info->free_chunk_lock);
1737 root->fs_info->free_chunk_space += device->total_bytes;
1738 spin_unlock(&root->fs_info->free_chunk_lock);
1739
Chris Masonc2898112009-06-10 09:51:32 -04001740 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1741 root->fs_info->fs_devices->rotating = 1;
1742
David Sterba6c417612011-04-13 15:41:04 +02001743 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1744 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001745 total_bytes + device->total_bytes);
1746
David Sterba6c417612011-04-13 15:41:04 +02001747 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1748 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001749 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001750 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001751
Yan Zheng2b820322008-11-17 21:11:30 -05001752 if (seeding_dev) {
1753 ret = init_first_rw_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001754 if (ret)
1755 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001756 ret = btrfs_finish_sprout(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001757 if (ret)
1758 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001759 } else {
1760 ret = btrfs_add_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 }
1764
Chris Mason913d9522009-03-10 13:17:18 -04001765 /*
1766 * we've got more storage, clear any full flags on the space
1767 * infos
1768 */
1769 btrfs_clear_space_info_full(root->fs_info);
1770
Chris Mason7d9eb122008-07-08 14:19:17 -04001771 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001772 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001773
1774 if (seeding_dev) {
1775 mutex_unlock(&uuid_mutex);
1776 up_write(&sb->s_umount);
1777
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001778 if (ret) /* transaction commit */
1779 return ret;
1780
Yan Zheng2b820322008-11-17 21:11:30 -05001781 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001782 if (ret < 0)
1783 btrfs_error(root->fs_info, ret,
1784 "Failed to relocate sys chunks after "
1785 "device initialization. This can be fixed "
1786 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001787 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001788
Chris Mason788f20e2008-04-28 15:29:42 -04001789 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001790
1791error_trans:
1792 unlock_chunks(root);
1793 btrfs_abort_transaction(trans, root, ret);
1794 btrfs_end_transaction(trans, root);
1795 kfree(device->name);
1796 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001797error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001798 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001799 if (seeding_dev) {
1800 mutex_unlock(&uuid_mutex);
1801 up_write(&sb->s_umount);
1802 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001803 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001804}
1805
Chris Masond3977122009-01-05 21:25:51 -05001806static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1807 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001808{
1809 int ret;
1810 struct btrfs_path *path;
1811 struct btrfs_root *root;
1812 struct btrfs_dev_item *dev_item;
1813 struct extent_buffer *leaf;
1814 struct btrfs_key key;
1815
1816 root = device->dev_root->fs_info->chunk_root;
1817
1818 path = btrfs_alloc_path();
1819 if (!path)
1820 return -ENOMEM;
1821
1822 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1823 key.type = BTRFS_DEV_ITEM_KEY;
1824 key.offset = device->devid;
1825
1826 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1827 if (ret < 0)
1828 goto out;
1829
1830 if (ret > 0) {
1831 ret = -ENOENT;
1832 goto out;
1833 }
1834
1835 leaf = path->nodes[0];
1836 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1837
1838 btrfs_set_device_id(leaf, dev_item, device->devid);
1839 btrfs_set_device_type(leaf, dev_item, device->type);
1840 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1841 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1842 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001843 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001844 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1845 btrfs_mark_buffer_dirty(leaf);
1846
1847out:
1848 btrfs_free_path(path);
1849 return ret;
1850}
1851
Chris Mason7d9eb122008-07-08 14:19:17 -04001852static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001853 struct btrfs_device *device, u64 new_size)
1854{
1855 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001856 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001857 u64 old_total = btrfs_super_total_bytes(super_copy);
1858 u64 diff = new_size - device->total_bytes;
1859
Yan Zheng2b820322008-11-17 21:11:30 -05001860 if (!device->writeable)
1861 return -EACCES;
1862 if (new_size <= device->total_bytes)
1863 return -EINVAL;
1864
Chris Mason8f18cf12008-04-25 16:53:30 -04001865 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001866 device->fs_devices->total_rw_bytes += diff;
1867
1868 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001869 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001870 btrfs_clear_space_info_full(device->dev_root->fs_info);
1871
Chris Mason8f18cf12008-04-25 16:53:30 -04001872 return btrfs_update_device(trans, device);
1873}
1874
Chris Mason7d9eb122008-07-08 14:19:17 -04001875int btrfs_grow_device(struct btrfs_trans_handle *trans,
1876 struct btrfs_device *device, u64 new_size)
1877{
1878 int ret;
1879 lock_chunks(device->dev_root);
1880 ret = __btrfs_grow_device(trans, device, new_size);
1881 unlock_chunks(device->dev_root);
1882 return ret;
1883}
1884
Chris Mason8f18cf12008-04-25 16:53:30 -04001885static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *root,
1887 u64 chunk_tree, u64 chunk_objectid,
1888 u64 chunk_offset)
1889{
1890 int ret;
1891 struct btrfs_path *path;
1892 struct btrfs_key key;
1893
1894 root = root->fs_info->chunk_root;
1895 path = btrfs_alloc_path();
1896 if (!path)
1897 return -ENOMEM;
1898
1899 key.objectid = chunk_objectid;
1900 key.offset = chunk_offset;
1901 key.type = BTRFS_CHUNK_ITEM_KEY;
1902
1903 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001904 if (ret < 0)
1905 goto out;
1906 else if (ret > 0) { /* Logic error or corruption */
1907 btrfs_error(root->fs_info, -ENOENT,
1908 "Failed lookup while freeing chunk.");
1909 ret = -ENOENT;
1910 goto out;
1911 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001912
1913 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001914 if (ret < 0)
1915 btrfs_error(root->fs_info, ret,
1916 "Failed to delete chunk item.");
1917out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001918 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001919 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001920}
1921
Christoph Hellwigb2950862008-12-02 09:54:17 -05001922static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001923 chunk_offset)
1924{
David Sterba6c417612011-04-13 15:41:04 +02001925 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001926 struct btrfs_disk_key *disk_key;
1927 struct btrfs_chunk *chunk;
1928 u8 *ptr;
1929 int ret = 0;
1930 u32 num_stripes;
1931 u32 array_size;
1932 u32 len = 0;
1933 u32 cur;
1934 struct btrfs_key key;
1935
1936 array_size = btrfs_super_sys_array_size(super_copy);
1937
1938 ptr = super_copy->sys_chunk_array;
1939 cur = 0;
1940
1941 while (cur < array_size) {
1942 disk_key = (struct btrfs_disk_key *)ptr;
1943 btrfs_disk_key_to_cpu(&key, disk_key);
1944
1945 len = sizeof(*disk_key);
1946
1947 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1948 chunk = (struct btrfs_chunk *)(ptr + len);
1949 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1950 len += btrfs_chunk_item_size(num_stripes);
1951 } else {
1952 ret = -EIO;
1953 break;
1954 }
1955 if (key.objectid == chunk_objectid &&
1956 key.offset == chunk_offset) {
1957 memmove(ptr, ptr + len, array_size - (cur + len));
1958 array_size -= len;
1959 btrfs_set_super_sys_array_size(super_copy, array_size);
1960 } else {
1961 ptr += len;
1962 cur += len;
1963 }
1964 }
1965 return ret;
1966}
1967
Christoph Hellwigb2950862008-12-02 09:54:17 -05001968static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001969 u64 chunk_tree, u64 chunk_objectid,
1970 u64 chunk_offset)
1971{
1972 struct extent_map_tree *em_tree;
1973 struct btrfs_root *extent_root;
1974 struct btrfs_trans_handle *trans;
1975 struct extent_map *em;
1976 struct map_lookup *map;
1977 int ret;
1978 int i;
1979
1980 root = root->fs_info->chunk_root;
1981 extent_root = root->fs_info->extent_root;
1982 em_tree = &root->fs_info->mapping_tree.map_tree;
1983
Josef Bacikba1bf482009-09-11 16:11:19 -04001984 ret = btrfs_can_relocate(extent_root, chunk_offset);
1985 if (ret)
1986 return -ENOSPC;
1987
Chris Mason8f18cf12008-04-25 16:53:30 -04001988 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001989 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001990 if (ret)
1991 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001992
Yan, Zhenga22285a2010-05-16 10:48:46 -04001993 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001994 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001995
Chris Mason7d9eb122008-07-08 14:19:17 -04001996 lock_chunks(root);
1997
Chris Mason8f18cf12008-04-25 16:53:30 -04001998 /*
1999 * step two, delete the device extents and the
2000 * chunk tree entries
2001 */
Chris Mason890871b2009-09-02 16:24:52 -04002002 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002003 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002004 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002005
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002006 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002007 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002008 map = (struct map_lookup *)em->bdev;
2009
2010 for (i = 0; i < map->num_stripes; i++) {
2011 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2012 map->stripes[i].physical);
2013 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002014
Chris Masondfe25022008-05-13 13:46:40 -04002015 if (map->stripes[i].dev) {
2016 ret = btrfs_update_device(trans, map->stripes[i].dev);
2017 BUG_ON(ret);
2018 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002019 }
2020 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2021 chunk_offset);
2022
2023 BUG_ON(ret);
2024
liubo1abe9b82011-03-24 11:18:59 +00002025 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2026
Chris Mason8f18cf12008-04-25 16:53:30 -04002027 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2028 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2029 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002030 }
2031
Zheng Yan1a40e232008-09-26 10:09:34 -04002032 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2033 BUG_ON(ret);
2034
Chris Mason890871b2009-09-02 16:24:52 -04002035 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002036 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002037 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002038
Chris Mason8f18cf12008-04-25 16:53:30 -04002039 kfree(map);
2040 em->bdev = NULL;
2041
2042 /* once for the tree */
2043 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002044 /* once for us */
2045 free_extent_map(em);
2046
Chris Mason7d9eb122008-07-08 14:19:17 -04002047 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002048 btrfs_end_transaction(trans, root);
2049 return 0;
2050}
2051
Yan Zheng2b820322008-11-17 21:11:30 -05002052static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2053{
2054 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2055 struct btrfs_path *path;
2056 struct extent_buffer *leaf;
2057 struct btrfs_chunk *chunk;
2058 struct btrfs_key key;
2059 struct btrfs_key found_key;
2060 u64 chunk_tree = chunk_root->root_key.objectid;
2061 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002062 bool retried = false;
2063 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002064 int ret;
2065
2066 path = btrfs_alloc_path();
2067 if (!path)
2068 return -ENOMEM;
2069
Josef Bacikba1bf482009-09-11 16:11:19 -04002070again:
Yan Zheng2b820322008-11-17 21:11:30 -05002071 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2072 key.offset = (u64)-1;
2073 key.type = BTRFS_CHUNK_ITEM_KEY;
2074
2075 while (1) {
2076 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2077 if (ret < 0)
2078 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002079 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002080
2081 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2082 key.type);
2083 if (ret < 0)
2084 goto error;
2085 if (ret > 0)
2086 break;
2087
2088 leaf = path->nodes[0];
2089 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2090
2091 chunk = btrfs_item_ptr(leaf, path->slots[0],
2092 struct btrfs_chunk);
2093 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002094 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002095
2096 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2097 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2098 found_key.objectid,
2099 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002100 if (ret == -ENOSPC)
2101 failed++;
2102 else if (ret)
2103 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002104 }
2105
2106 if (found_key.offset == 0)
2107 break;
2108 key.offset = found_key.offset - 1;
2109 }
2110 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002111 if (failed && !retried) {
2112 failed = 0;
2113 retried = true;
2114 goto again;
2115 } else if (failed && retried) {
2116 WARN_ON(1);
2117 ret = -ENOSPC;
2118 }
Yan Zheng2b820322008-11-17 21:11:30 -05002119error:
2120 btrfs_free_path(path);
2121 return ret;
2122}
2123
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002124static int insert_balance_item(struct btrfs_root *root,
2125 struct btrfs_balance_control *bctl)
2126{
2127 struct btrfs_trans_handle *trans;
2128 struct btrfs_balance_item *item;
2129 struct btrfs_disk_balance_args disk_bargs;
2130 struct btrfs_path *path;
2131 struct extent_buffer *leaf;
2132 struct btrfs_key key;
2133 int ret, err;
2134
2135 path = btrfs_alloc_path();
2136 if (!path)
2137 return -ENOMEM;
2138
2139 trans = btrfs_start_transaction(root, 0);
2140 if (IS_ERR(trans)) {
2141 btrfs_free_path(path);
2142 return PTR_ERR(trans);
2143 }
2144
2145 key.objectid = BTRFS_BALANCE_OBJECTID;
2146 key.type = BTRFS_BALANCE_ITEM_KEY;
2147 key.offset = 0;
2148
2149 ret = btrfs_insert_empty_item(trans, root, path, &key,
2150 sizeof(*item));
2151 if (ret)
2152 goto out;
2153
2154 leaf = path->nodes[0];
2155 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2156
2157 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2158
2159 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2160 btrfs_set_balance_data(leaf, item, &disk_bargs);
2161 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2162 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2163 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2164 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2165
2166 btrfs_set_balance_flags(leaf, item, bctl->flags);
2167
2168 btrfs_mark_buffer_dirty(leaf);
2169out:
2170 btrfs_free_path(path);
2171 err = btrfs_commit_transaction(trans, root);
2172 if (err && !ret)
2173 ret = err;
2174 return ret;
2175}
2176
2177static int del_balance_item(struct btrfs_root *root)
2178{
2179 struct btrfs_trans_handle *trans;
2180 struct btrfs_path *path;
2181 struct btrfs_key key;
2182 int ret, err;
2183
2184 path = btrfs_alloc_path();
2185 if (!path)
2186 return -ENOMEM;
2187
2188 trans = btrfs_start_transaction(root, 0);
2189 if (IS_ERR(trans)) {
2190 btrfs_free_path(path);
2191 return PTR_ERR(trans);
2192 }
2193
2194 key.objectid = BTRFS_BALANCE_OBJECTID;
2195 key.type = BTRFS_BALANCE_ITEM_KEY;
2196 key.offset = 0;
2197
2198 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2199 if (ret < 0)
2200 goto out;
2201 if (ret > 0) {
2202 ret = -ENOENT;
2203 goto out;
2204 }
2205
2206 ret = btrfs_del_item(trans, root, path);
2207out:
2208 btrfs_free_path(path);
2209 err = btrfs_commit_transaction(trans, root);
2210 if (err && !ret)
2211 ret = err;
2212 return ret;
2213}
2214
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002215/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002216 * This is a heuristic used to reduce the number of chunks balanced on
2217 * resume after balance was interrupted.
2218 */
2219static void update_balance_args(struct btrfs_balance_control *bctl)
2220{
2221 /*
2222 * Turn on soft mode for chunk types that were being converted.
2223 */
2224 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2225 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2226 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2227 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2228 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2229 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2230
2231 /*
2232 * Turn on usage filter if is not already used. The idea is
2233 * that chunks that we have already balanced should be
2234 * reasonably full. Don't do it for chunks that are being
2235 * converted - that will keep us from relocating unconverted
2236 * (albeit full) chunks.
2237 */
2238 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2239 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2240 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2241 bctl->data.usage = 90;
2242 }
2243 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2244 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2245 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2246 bctl->sys.usage = 90;
2247 }
2248 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2249 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2250 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2251 bctl->meta.usage = 90;
2252 }
2253}
2254
2255/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002256 * Should be called with both balance and volume mutexes held to
2257 * serialize other volume operations (add_dev/rm_dev/resize) with
2258 * restriper. Same goes for unset_balance_control.
2259 */
2260static void set_balance_control(struct btrfs_balance_control *bctl)
2261{
2262 struct btrfs_fs_info *fs_info = bctl->fs_info;
2263
2264 BUG_ON(fs_info->balance_ctl);
2265
2266 spin_lock(&fs_info->balance_lock);
2267 fs_info->balance_ctl = bctl;
2268 spin_unlock(&fs_info->balance_lock);
2269}
2270
2271static void unset_balance_control(struct btrfs_fs_info *fs_info)
2272{
2273 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2274
2275 BUG_ON(!fs_info->balance_ctl);
2276
2277 spin_lock(&fs_info->balance_lock);
2278 fs_info->balance_ctl = NULL;
2279 spin_unlock(&fs_info->balance_lock);
2280
2281 kfree(bctl);
2282}
2283
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002284/*
2285 * Balance filters. Return 1 if chunk should be filtered out
2286 * (should not be balanced).
2287 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002288static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002289 struct btrfs_balance_args *bargs)
2290{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002291 chunk_type = chunk_to_extended(chunk_type) &
2292 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002293
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002294 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002295 return 0;
2296
2297 return 1;
2298}
2299
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002300static u64 div_factor_fine(u64 num, int factor)
2301{
2302 if (factor <= 0)
2303 return 0;
2304 if (factor >= 100)
2305 return num;
2306
2307 num *= factor;
2308 do_div(num, 100);
2309 return num;
2310}
2311
2312static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2313 struct btrfs_balance_args *bargs)
2314{
2315 struct btrfs_block_group_cache *cache;
2316 u64 chunk_used, user_thresh;
2317 int ret = 1;
2318
2319 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2320 chunk_used = btrfs_block_group_used(&cache->item);
2321
2322 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2323 if (chunk_used < user_thresh)
2324 ret = 0;
2325
2326 btrfs_put_block_group(cache);
2327 return ret;
2328}
2329
Ilya Dryomov409d4042012-01-16 22:04:47 +02002330static int chunk_devid_filter(struct extent_buffer *leaf,
2331 struct btrfs_chunk *chunk,
2332 struct btrfs_balance_args *bargs)
2333{
2334 struct btrfs_stripe *stripe;
2335 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2336 int i;
2337
2338 for (i = 0; i < num_stripes; i++) {
2339 stripe = btrfs_stripe_nr(chunk, i);
2340 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2341 return 0;
2342 }
2343
2344 return 1;
2345}
2346
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002347/* [pstart, pend) */
2348static int chunk_drange_filter(struct extent_buffer *leaf,
2349 struct btrfs_chunk *chunk,
2350 u64 chunk_offset,
2351 struct btrfs_balance_args *bargs)
2352{
2353 struct btrfs_stripe *stripe;
2354 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2355 u64 stripe_offset;
2356 u64 stripe_length;
2357 int factor;
2358 int i;
2359
2360 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2361 return 0;
2362
2363 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2364 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2365 factor = 2;
2366 else
2367 factor = 1;
2368 factor = num_stripes / factor;
2369
2370 for (i = 0; i < num_stripes; i++) {
2371 stripe = btrfs_stripe_nr(chunk, i);
2372 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2373 continue;
2374
2375 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2376 stripe_length = btrfs_chunk_length(leaf, chunk);
2377 do_div(stripe_length, factor);
2378
2379 if (stripe_offset < bargs->pend &&
2380 stripe_offset + stripe_length > bargs->pstart)
2381 return 0;
2382 }
2383
2384 return 1;
2385}
2386
Ilya Dryomovea671762012-01-16 22:04:48 +02002387/* [vstart, vend) */
2388static int chunk_vrange_filter(struct extent_buffer *leaf,
2389 struct btrfs_chunk *chunk,
2390 u64 chunk_offset,
2391 struct btrfs_balance_args *bargs)
2392{
2393 if (chunk_offset < bargs->vend &&
2394 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2395 /* at least part of the chunk is inside this vrange */
2396 return 0;
2397
2398 return 1;
2399}
2400
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002401static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002402 struct btrfs_balance_args *bargs)
2403{
2404 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2405 return 0;
2406
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002407 chunk_type = chunk_to_extended(chunk_type) &
2408 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002409
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002410 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002411 return 1;
2412
2413 return 0;
2414}
2415
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002416static int should_balance_chunk(struct btrfs_root *root,
2417 struct extent_buffer *leaf,
2418 struct btrfs_chunk *chunk, u64 chunk_offset)
2419{
2420 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2421 struct btrfs_balance_args *bargs = NULL;
2422 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2423
2424 /* type filter */
2425 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2426 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2427 return 0;
2428 }
2429
2430 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2431 bargs = &bctl->data;
2432 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2433 bargs = &bctl->sys;
2434 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2435 bargs = &bctl->meta;
2436
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002437 /* profiles filter */
2438 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2439 chunk_profiles_filter(chunk_type, bargs)) {
2440 return 0;
2441 }
2442
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002443 /* usage filter */
2444 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2445 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2446 return 0;
2447 }
2448
Ilya Dryomov409d4042012-01-16 22:04:47 +02002449 /* devid filter */
2450 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2451 chunk_devid_filter(leaf, chunk, bargs)) {
2452 return 0;
2453 }
2454
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002455 /* drange filter, makes sense only with devid filter */
2456 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2457 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2458 return 0;
2459 }
2460
Ilya Dryomovea671762012-01-16 22:04:48 +02002461 /* vrange filter */
2462 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2463 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2464 return 0;
2465 }
2466
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002467 /* soft profile changing mode */
2468 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2469 chunk_soft_convert_filter(chunk_type, bargs)) {
2470 return 0;
2471 }
2472
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002473 return 1;
2474}
2475
Chris Masonec44a352008-04-28 15:29:52 -04002476static u64 div_factor(u64 num, int factor)
2477{
2478 if (factor == 10)
2479 return num;
2480 num *= factor;
2481 do_div(num, 10);
2482 return num;
2483}
2484
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002485static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002486{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002487 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002488 struct btrfs_root *chunk_root = fs_info->chunk_root;
2489 struct btrfs_root *dev_root = fs_info->dev_root;
2490 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002491 struct btrfs_device *device;
2492 u64 old_size;
2493 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002494 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002495 struct btrfs_path *path;
2496 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002497 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002498 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002499 struct extent_buffer *leaf;
2500 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002501 int ret;
2502 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002503 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002504
Chris Masonec44a352008-04-28 15:29:52 -04002505 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002506 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002507 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002508 old_size = device->total_bytes;
2509 size_to_free = div_factor(old_size, 1);
2510 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002511 if (!device->writeable ||
2512 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002513 continue;
2514
2515 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002516 if (ret == -ENOSPC)
2517 break;
Chris Masonec44a352008-04-28 15:29:52 -04002518 BUG_ON(ret);
2519
Yan, Zhenga22285a2010-05-16 10:48:46 -04002520 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002521 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002522
2523 ret = btrfs_grow_device(trans, device, old_size);
2524 BUG_ON(ret);
2525
2526 btrfs_end_transaction(trans, dev_root);
2527 }
2528
2529 /* step two, relocate all the chunks */
2530 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002531 if (!path) {
2532 ret = -ENOMEM;
2533 goto error;
2534 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002535
2536 /* zero out stat counters */
2537 spin_lock(&fs_info->balance_lock);
2538 memset(&bctl->stat, 0, sizeof(bctl->stat));
2539 spin_unlock(&fs_info->balance_lock);
2540again:
Chris Masonec44a352008-04-28 15:29:52 -04002541 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2542 key.offset = (u64)-1;
2543 key.type = BTRFS_CHUNK_ITEM_KEY;
2544
Chris Masond3977122009-01-05 21:25:51 -05002545 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002546 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002547 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002548 ret = -ECANCELED;
2549 goto error;
2550 }
2551
Chris Masonec44a352008-04-28 15:29:52 -04002552 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2553 if (ret < 0)
2554 goto error;
2555
2556 /*
2557 * this shouldn't happen, it means the last relocate
2558 * failed
2559 */
2560 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002561 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002562
2563 ret = btrfs_previous_item(chunk_root, path, 0,
2564 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002565 if (ret) {
2566 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002567 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002568 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002569
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002570 leaf = path->nodes[0];
2571 slot = path->slots[0];
2572 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2573
Chris Masonec44a352008-04-28 15:29:52 -04002574 if (found_key.objectid != key.objectid)
2575 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002576
Chris Masonec44a352008-04-28 15:29:52 -04002577 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002578 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002579 break;
2580
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002581 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2582
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002583 if (!counting) {
2584 spin_lock(&fs_info->balance_lock);
2585 bctl->stat.considered++;
2586 spin_unlock(&fs_info->balance_lock);
2587 }
2588
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002589 ret = should_balance_chunk(chunk_root, leaf, chunk,
2590 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002591 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002592 if (!ret)
2593 goto loop;
2594
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002595 if (counting) {
2596 spin_lock(&fs_info->balance_lock);
2597 bctl->stat.expected++;
2598 spin_unlock(&fs_info->balance_lock);
2599 goto loop;
2600 }
2601
Chris Masonec44a352008-04-28 15:29:52 -04002602 ret = btrfs_relocate_chunk(chunk_root,
2603 chunk_root->root_key.objectid,
2604 found_key.objectid,
2605 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002606 if (ret && ret != -ENOSPC)
2607 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002608 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002609 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002610 } else {
2611 spin_lock(&fs_info->balance_lock);
2612 bctl->stat.completed++;
2613 spin_unlock(&fs_info->balance_lock);
2614 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002615loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002616 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002617 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002618
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002619 if (counting) {
2620 btrfs_release_path(path);
2621 counting = false;
2622 goto again;
2623 }
Chris Masonec44a352008-04-28 15:29:52 -04002624error:
2625 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002626 if (enospc_errors) {
2627 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2628 enospc_errors);
2629 if (!ret)
2630 ret = -ENOSPC;
2631 }
2632
Chris Masonec44a352008-04-28 15:29:52 -04002633 return ret;
2634}
2635
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002636/**
2637 * alloc_profile_is_valid - see if a given profile is valid and reduced
2638 * @flags: profile to validate
2639 * @extended: if true @flags is treated as an extended profile
2640 */
2641static int alloc_profile_is_valid(u64 flags, int extended)
2642{
2643 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2644 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2645
2646 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2647
2648 /* 1) check that all other bits are zeroed */
2649 if (flags & ~mask)
2650 return 0;
2651
2652 /* 2) see if profile is reduced */
2653 if (flags == 0)
2654 return !extended; /* "0" is valid for usual profiles */
2655
2656 /* true if exactly one bit set */
2657 return (flags & (flags - 1)) == 0;
2658}
2659
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002660static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2661{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002662 /* cancel requested || normal exit path */
2663 return atomic_read(&fs_info->balance_cancel_req) ||
2664 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2665 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002666}
2667
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002668static void __cancel_balance(struct btrfs_fs_info *fs_info)
2669{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002670 int ret;
2671
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002672 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002673 ret = del_balance_item(fs_info->tree_root);
2674 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002675}
2676
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002677void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002678 struct btrfs_ioctl_balance_args *bargs);
2679
2680/*
2681 * Should be called with both balance and volume mutexes held
2682 */
2683int btrfs_balance(struct btrfs_balance_control *bctl,
2684 struct btrfs_ioctl_balance_args *bargs)
2685{
2686 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002687 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002688 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002689 int ret;
2690
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002691 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002692 atomic_read(&fs_info->balance_pause_req) ||
2693 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002694 ret = -EINVAL;
2695 goto out;
2696 }
2697
Ilya Dryomove4837f82012-03-27 17:09:17 +03002698 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2699 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2700 mixed = 1;
2701
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002702 /*
2703 * In case of mixed groups both data and meta should be picked,
2704 * and identical options should be given for both of them.
2705 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002706 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2707 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002708 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2709 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2710 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2711 printk(KERN_ERR "btrfs: with mixed groups data and "
2712 "metadata balance options must be the same\n");
2713 ret = -EINVAL;
2714 goto out;
2715 }
2716 }
2717
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002718 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2719 if (fs_info->fs_devices->num_devices == 1)
2720 allowed |= BTRFS_BLOCK_GROUP_DUP;
2721 else if (fs_info->fs_devices->num_devices < 4)
2722 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2723 else
2724 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2725 BTRFS_BLOCK_GROUP_RAID10);
2726
Ilya Dryomov6728b192012-03-27 17:09:17 +03002727 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2728 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2729 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002730 printk(KERN_ERR "btrfs: unable to start balance with target "
2731 "data profile %llu\n",
2732 (unsigned long long)bctl->data.target);
2733 ret = -EINVAL;
2734 goto out;
2735 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002736 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2737 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2738 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002739 printk(KERN_ERR "btrfs: unable to start balance with target "
2740 "metadata profile %llu\n",
2741 (unsigned long long)bctl->meta.target);
2742 ret = -EINVAL;
2743 goto out;
2744 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002745 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2746 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2747 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002748 printk(KERN_ERR "btrfs: unable to start balance with target "
2749 "system profile %llu\n",
2750 (unsigned long long)bctl->sys.target);
2751 ret = -EINVAL;
2752 goto out;
2753 }
2754
Ilya Dryomove4837f82012-03-27 17:09:17 +03002755 /* allow dup'ed data chunks only in mixed mode */
2756 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002757 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002758 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2759 ret = -EINVAL;
2760 goto out;
2761 }
2762
2763 /* allow to reduce meta or sys integrity only if force set */
2764 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2765 BTRFS_BLOCK_GROUP_RAID10;
2766 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2767 (fs_info->avail_system_alloc_bits & allowed) &&
2768 !(bctl->sys.target & allowed)) ||
2769 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2770 (fs_info->avail_metadata_alloc_bits & allowed) &&
2771 !(bctl->meta.target & allowed))) {
2772 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2773 printk(KERN_INFO "btrfs: force reducing metadata "
2774 "integrity\n");
2775 } else {
2776 printk(KERN_ERR "btrfs: balance will reduce metadata "
2777 "integrity, use force if you want this\n");
2778 ret = -EINVAL;
2779 goto out;
2780 }
2781 }
2782
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002783 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002784 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002785 goto out;
2786
Ilya Dryomov59641012012-01-16 22:04:48 +02002787 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2788 BUG_ON(ret == -EEXIST);
2789 set_balance_control(bctl);
2790 } else {
2791 BUG_ON(ret != -EEXIST);
2792 spin_lock(&fs_info->balance_lock);
2793 update_balance_args(bctl);
2794 spin_unlock(&fs_info->balance_lock);
2795 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002796
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002797 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002798 mutex_unlock(&fs_info->balance_mutex);
2799
2800 ret = __btrfs_balance(fs_info);
2801
2802 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002803 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002804
2805 if (bargs) {
2806 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002807 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002808 }
2809
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002810 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2811 balance_need_close(fs_info)) {
2812 __cancel_balance(fs_info);
2813 }
2814
2815 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002816
2817 return ret;
2818out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002819 if (bctl->flags & BTRFS_BALANCE_RESUME)
2820 __cancel_balance(fs_info);
2821 else
2822 kfree(bctl);
2823 return ret;
2824}
2825
2826static int balance_kthread(void *data)
2827{
2828 struct btrfs_balance_control *bctl =
2829 (struct btrfs_balance_control *)data;
2830 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002831 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002832
2833 mutex_lock(&fs_info->volume_mutex);
2834 mutex_lock(&fs_info->balance_mutex);
2835
2836 set_balance_control(bctl);
2837
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002838 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2839 printk(KERN_INFO "btrfs: force skipping balance\n");
2840 } else {
2841 printk(KERN_INFO "btrfs: continuing balance\n");
2842 ret = btrfs_balance(bctl, NULL);
2843 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002844
2845 mutex_unlock(&fs_info->balance_mutex);
2846 mutex_unlock(&fs_info->volume_mutex);
2847 return ret;
2848}
2849
2850int btrfs_recover_balance(struct btrfs_root *tree_root)
2851{
2852 struct task_struct *tsk;
2853 struct btrfs_balance_control *bctl;
2854 struct btrfs_balance_item *item;
2855 struct btrfs_disk_balance_args disk_bargs;
2856 struct btrfs_path *path;
2857 struct extent_buffer *leaf;
2858 struct btrfs_key key;
2859 int ret;
2860
2861 path = btrfs_alloc_path();
2862 if (!path)
2863 return -ENOMEM;
2864
2865 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2866 if (!bctl) {
2867 ret = -ENOMEM;
2868 goto out;
2869 }
2870
2871 key.objectid = BTRFS_BALANCE_OBJECTID;
2872 key.type = BTRFS_BALANCE_ITEM_KEY;
2873 key.offset = 0;
2874
2875 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2876 if (ret < 0)
2877 goto out_bctl;
2878 if (ret > 0) { /* ret = -ENOENT; */
2879 ret = 0;
2880 goto out_bctl;
2881 }
2882
2883 leaf = path->nodes[0];
2884 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2885
2886 bctl->fs_info = tree_root->fs_info;
2887 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2888
2889 btrfs_balance_data(leaf, item, &disk_bargs);
2890 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2891 btrfs_balance_meta(leaf, item, &disk_bargs);
2892 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2893 btrfs_balance_sys(leaf, item, &disk_bargs);
2894 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2895
2896 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2897 if (IS_ERR(tsk))
2898 ret = PTR_ERR(tsk);
2899 else
2900 goto out;
2901
2902out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002903 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002904out:
2905 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002906 return ret;
2907}
2908
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002909int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2910{
2911 int ret = 0;
2912
2913 mutex_lock(&fs_info->balance_mutex);
2914 if (!fs_info->balance_ctl) {
2915 mutex_unlock(&fs_info->balance_mutex);
2916 return -ENOTCONN;
2917 }
2918
2919 if (atomic_read(&fs_info->balance_running)) {
2920 atomic_inc(&fs_info->balance_pause_req);
2921 mutex_unlock(&fs_info->balance_mutex);
2922
2923 wait_event(fs_info->balance_wait_q,
2924 atomic_read(&fs_info->balance_running) == 0);
2925
2926 mutex_lock(&fs_info->balance_mutex);
2927 /* we are good with balance_ctl ripped off from under us */
2928 BUG_ON(atomic_read(&fs_info->balance_running));
2929 atomic_dec(&fs_info->balance_pause_req);
2930 } else {
2931 ret = -ENOTCONN;
2932 }
2933
2934 mutex_unlock(&fs_info->balance_mutex);
2935 return ret;
2936}
2937
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002938int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2939{
2940 mutex_lock(&fs_info->balance_mutex);
2941 if (!fs_info->balance_ctl) {
2942 mutex_unlock(&fs_info->balance_mutex);
2943 return -ENOTCONN;
2944 }
2945
2946 atomic_inc(&fs_info->balance_cancel_req);
2947 /*
2948 * if we are running just wait and return, balance item is
2949 * deleted in btrfs_balance in this case
2950 */
2951 if (atomic_read(&fs_info->balance_running)) {
2952 mutex_unlock(&fs_info->balance_mutex);
2953 wait_event(fs_info->balance_wait_q,
2954 atomic_read(&fs_info->balance_running) == 0);
2955 mutex_lock(&fs_info->balance_mutex);
2956 } else {
2957 /* __cancel_balance needs volume_mutex */
2958 mutex_unlock(&fs_info->balance_mutex);
2959 mutex_lock(&fs_info->volume_mutex);
2960 mutex_lock(&fs_info->balance_mutex);
2961
2962 if (fs_info->balance_ctl)
2963 __cancel_balance(fs_info);
2964
2965 mutex_unlock(&fs_info->volume_mutex);
2966 }
2967
2968 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2969 atomic_dec(&fs_info->balance_cancel_req);
2970 mutex_unlock(&fs_info->balance_mutex);
2971 return 0;
2972}
2973
Chris Mason8f18cf12008-04-25 16:53:30 -04002974/*
2975 * shrinking a device means finding all of the device extents past
2976 * the new size, and then following the back refs to the chunks.
2977 * The chunk relocation code actually frees the device extent
2978 */
2979int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2980{
2981 struct btrfs_trans_handle *trans;
2982 struct btrfs_root *root = device->dev_root;
2983 struct btrfs_dev_extent *dev_extent = NULL;
2984 struct btrfs_path *path;
2985 u64 length;
2986 u64 chunk_tree;
2987 u64 chunk_objectid;
2988 u64 chunk_offset;
2989 int ret;
2990 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002991 int failed = 0;
2992 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002993 struct extent_buffer *l;
2994 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002995 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002996 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002997 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002998 u64 diff = device->total_bytes - new_size;
2999
Yan Zheng2b820322008-11-17 21:11:30 -05003000 if (new_size >= device->total_bytes)
3001 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04003002
3003 path = btrfs_alloc_path();
3004 if (!path)
3005 return -ENOMEM;
3006
Chris Mason8f18cf12008-04-25 16:53:30 -04003007 path->reada = 2;
3008
Chris Mason7d9eb122008-07-08 14:19:17 -04003009 lock_chunks(root);
3010
Chris Mason8f18cf12008-04-25 16:53:30 -04003011 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003012 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003013 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003014 spin_lock(&root->fs_info->free_chunk_lock);
3015 root->fs_info->free_chunk_space -= diff;
3016 spin_unlock(&root->fs_info->free_chunk_lock);
3017 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003018 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003019
Josef Bacikba1bf482009-09-11 16:11:19 -04003020again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003021 key.objectid = device->devid;
3022 key.offset = (u64)-1;
3023 key.type = BTRFS_DEV_EXTENT_KEY;
3024
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003025 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003026 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3027 if (ret < 0)
3028 goto done;
3029
3030 ret = btrfs_previous_item(root, path, 0, key.type);
3031 if (ret < 0)
3032 goto done;
3033 if (ret) {
3034 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003035 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003036 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003037 }
3038
3039 l = path->nodes[0];
3040 slot = path->slots[0];
3041 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3042
Josef Bacikba1bf482009-09-11 16:11:19 -04003043 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003044 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003045 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003046 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003047
3048 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3049 length = btrfs_dev_extent_length(l, dev_extent);
3050
Josef Bacikba1bf482009-09-11 16:11:19 -04003051 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003052 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003053 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003054 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003055
3056 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3057 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3058 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003059 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003060
3061 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3062 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003063 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003064 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003065 if (ret == -ENOSPC)
3066 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003067 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003068
3069 if (failed && !retried) {
3070 failed = 0;
3071 retried = true;
3072 goto again;
3073 } else if (failed && retried) {
3074 ret = -ENOSPC;
3075 lock_chunks(root);
3076
3077 device->total_bytes = old_size;
3078 if (device->writeable)
3079 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003080 spin_lock(&root->fs_info->free_chunk_lock);
3081 root->fs_info->free_chunk_space += diff;
3082 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003083 unlock_chunks(root);
3084 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003085 }
3086
Chris Balld6397ba2009-04-27 07:29:03 -04003087 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003088 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003089 if (IS_ERR(trans)) {
3090 ret = PTR_ERR(trans);
3091 goto done;
3092 }
3093
Chris Balld6397ba2009-04-27 07:29:03 -04003094 lock_chunks(root);
3095
3096 device->disk_total_bytes = new_size;
3097 /* Now btrfs_update_device() will change the on-disk size. */
3098 ret = btrfs_update_device(trans, device);
3099 if (ret) {
3100 unlock_chunks(root);
3101 btrfs_end_transaction(trans, root);
3102 goto done;
3103 }
3104 WARN_ON(diff > old_total);
3105 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3106 unlock_chunks(root);
3107 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003108done:
3109 btrfs_free_path(path);
3110 return ret;
3111}
3112
Li Zefan125ccb02011-12-08 15:07:24 +08003113static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003114 struct btrfs_key *key,
3115 struct btrfs_chunk *chunk, int item_size)
3116{
David Sterba6c417612011-04-13 15:41:04 +02003117 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003118 struct btrfs_disk_key disk_key;
3119 u32 array_size;
3120 u8 *ptr;
3121
3122 array_size = btrfs_super_sys_array_size(super_copy);
3123 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3124 return -EFBIG;
3125
3126 ptr = super_copy->sys_chunk_array + array_size;
3127 btrfs_cpu_key_to_disk(&disk_key, key);
3128 memcpy(ptr, &disk_key, sizeof(disk_key));
3129 ptr += sizeof(disk_key);
3130 memcpy(ptr, chunk, item_size);
3131 item_size += sizeof(disk_key);
3132 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3133 return 0;
3134}
3135
Miao Xieb2117a32011-01-05 10:07:28 +00003136/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003137 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003138 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003139static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003140{
Arne Jansen73c5de02011-04-12 12:07:57 +02003141 const struct btrfs_device_info *di_a = a;
3142 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003143
Arne Jansen73c5de02011-04-12 12:07:57 +02003144 if (di_a->max_avail > di_b->max_avail)
3145 return -1;
3146 if (di_a->max_avail < di_b->max_avail)
3147 return 1;
3148 if (di_a->total_avail > di_b->total_avail)
3149 return -1;
3150 if (di_a->total_avail < di_b->total_avail)
3151 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003152 return 0;
3153}
3154
3155static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3156 struct btrfs_root *extent_root,
3157 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003158 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003159 u64 start, u64 type)
3160{
3161 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003162 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3163 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003164 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003165 struct extent_map_tree *em_tree;
3166 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003167 struct btrfs_device_info *devices_info = NULL;
3168 u64 total_avail;
3169 int num_stripes; /* total number of stripes to allocate */
3170 int sub_stripes; /* sub_stripes info for map */
3171 int dev_stripes; /* stripes per dev */
3172 int devs_max; /* max devs to use */
3173 int devs_min; /* min devs needed */
3174 int devs_increment; /* ndevs has to be a multiple of this */
3175 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003176 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003177 u64 max_stripe_size;
3178 u64 max_chunk_size;
3179 u64 stripe_size;
3180 u64 num_bytes;
3181 int ndevs;
3182 int i;
3183 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003184
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003185 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003186
Miao Xieb2117a32011-01-05 10:07:28 +00003187 if (list_empty(&fs_devices->alloc_list))
3188 return -ENOSPC;
3189
Arne Jansen73c5de02011-04-12 12:07:57 +02003190 sub_stripes = 1;
3191 dev_stripes = 1;
3192 devs_increment = 1;
3193 ncopies = 1;
3194 devs_max = 0; /* 0 == as many as possible */
3195 devs_min = 1;
3196
3197 /*
3198 * define the properties of each RAID type.
3199 * FIXME: move this to a global table and use it in all RAID
3200 * calculation code
3201 */
3202 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3203 dev_stripes = 2;
3204 ncopies = 2;
3205 devs_max = 1;
3206 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3207 devs_min = 2;
3208 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3209 devs_increment = 2;
3210 ncopies = 2;
3211 devs_max = 2;
3212 devs_min = 2;
3213 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3214 sub_stripes = 2;
3215 devs_increment = 2;
3216 ncopies = 2;
3217 devs_min = 4;
3218 } else {
3219 devs_max = 1;
3220 }
3221
3222 if (type & BTRFS_BLOCK_GROUP_DATA) {
3223 max_stripe_size = 1024 * 1024 * 1024;
3224 max_chunk_size = 10 * max_stripe_size;
3225 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003226 /* for larger filesystems, use larger metadata chunks */
3227 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3228 max_stripe_size = 1024 * 1024 * 1024;
3229 else
3230 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003231 max_chunk_size = max_stripe_size;
3232 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003233 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003234 max_chunk_size = 2 * max_stripe_size;
3235 } else {
3236 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3237 type);
3238 BUG_ON(1);
3239 }
3240
3241 /* we don't want a chunk larger than 10% of writeable space */
3242 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3243 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003244
3245 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3246 GFP_NOFS);
3247 if (!devices_info)
3248 return -ENOMEM;
3249
Arne Jansen73c5de02011-04-12 12:07:57 +02003250 cur = fs_devices->alloc_list.next;
3251
3252 /*
3253 * in the first pass through the devices list, we gather information
3254 * about the available holes on each device.
3255 */
3256 ndevs = 0;
3257 while (cur != &fs_devices->alloc_list) {
3258 struct btrfs_device *device;
3259 u64 max_avail;
3260 u64 dev_offset;
3261
3262 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3263
3264 cur = cur->next;
3265
3266 if (!device->writeable) {
3267 printk(KERN_ERR
3268 "btrfs: read-only device in alloc_list\n");
3269 WARN_ON(1);
3270 continue;
3271 }
3272
3273 if (!device->in_fs_metadata)
3274 continue;
3275
3276 if (device->total_bytes > device->bytes_used)
3277 total_avail = device->total_bytes - device->bytes_used;
3278 else
3279 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003280
3281 /* If there is no space on this device, skip it. */
3282 if (total_avail == 0)
3283 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003284
Li Zefan125ccb02011-12-08 15:07:24 +08003285 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003286 max_stripe_size * dev_stripes,
3287 &dev_offset, &max_avail);
3288 if (ret && ret != -ENOSPC)
3289 goto error;
3290
3291 if (ret == 0)
3292 max_avail = max_stripe_size * dev_stripes;
3293
3294 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3295 continue;
3296
3297 devices_info[ndevs].dev_offset = dev_offset;
3298 devices_info[ndevs].max_avail = max_avail;
3299 devices_info[ndevs].total_avail = total_avail;
3300 devices_info[ndevs].dev = device;
3301 ++ndevs;
3302 }
3303
3304 /*
3305 * now sort the devices by hole size / available space
3306 */
3307 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3308 btrfs_cmp_device_info, NULL);
3309
3310 /* round down to number of usable stripes */
3311 ndevs -= ndevs % devs_increment;
3312
3313 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3314 ret = -ENOSPC;
3315 goto error;
3316 }
3317
3318 if (devs_max && ndevs > devs_max)
3319 ndevs = devs_max;
3320 /*
3321 * the primary goal is to maximize the number of stripes, so use as many
3322 * devices as possible, even if the stripes are not maximum sized.
3323 */
3324 stripe_size = devices_info[ndevs-1].max_avail;
3325 num_stripes = ndevs * dev_stripes;
3326
3327 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
3328 stripe_size = max_chunk_size * ncopies;
3329 do_div(stripe_size, num_stripes);
3330 }
3331
3332 do_div(stripe_size, dev_stripes);
3333 do_div(stripe_size, BTRFS_STRIPE_LEN);
3334 stripe_size *= BTRFS_STRIPE_LEN;
3335
Miao Xieb2117a32011-01-05 10:07:28 +00003336 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3337 if (!map) {
3338 ret = -ENOMEM;
3339 goto error;
3340 }
3341 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003342
Arne Jansen73c5de02011-04-12 12:07:57 +02003343 for (i = 0; i < ndevs; ++i) {
3344 for (j = 0; j < dev_stripes; ++j) {
3345 int s = i * dev_stripes + j;
3346 map->stripes[s].dev = devices_info[i].dev;
3347 map->stripes[s].physical = devices_info[i].dev_offset +
3348 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003349 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003350 }
Chris Mason593060d2008-03-25 16:50:33 -04003351 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003352 map->stripe_len = BTRFS_STRIPE_LEN;
3353 map->io_align = BTRFS_STRIPE_LEN;
3354 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003355 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003356 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003357
Yan Zheng2b820322008-11-17 21:11:30 -05003358 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003359 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003360
Arne Jansen73c5de02011-04-12 12:07:57 +02003361 *stripe_size_out = stripe_size;
3362 *num_bytes_out = num_bytes;
3363
3364 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003365
David Sterba172ddd62011-04-21 00:48:27 +02003366 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003367 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003368 ret = -ENOMEM;
3369 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003370 }
Chris Mason0b86a832008-03-24 15:01:56 -04003371 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003372 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003373 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003374 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003375 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003376
Chris Mason0b86a832008-03-24 15:01:56 -04003377 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003378 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003379 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003380 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003381 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003382 if (ret)
3383 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003384
3385 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3386 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003387 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003388 if (ret)
3389 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003390
Arne Jansen73c5de02011-04-12 12:07:57 +02003391 for (i = 0; i < map->num_stripes; ++i) {
3392 struct btrfs_device *device;
3393 u64 dev_offset;
3394
3395 device = map->stripes[i].dev;
3396 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003397
3398 ret = btrfs_alloc_dev_extent(trans, device,
3399 info->chunk_root->root_key.objectid,
3400 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003401 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003402 if (ret) {
3403 btrfs_abort_transaction(trans, extent_root, ret);
3404 goto error;
3405 }
Yan Zheng2b820322008-11-17 21:11:30 -05003406 }
3407
Miao Xieb2117a32011-01-05 10:07:28 +00003408 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003409 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003410
3411error:
3412 kfree(map);
3413 kfree(devices_info);
3414 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003415}
3416
3417static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3418 struct btrfs_root *extent_root,
3419 struct map_lookup *map, u64 chunk_offset,
3420 u64 chunk_size, u64 stripe_size)
3421{
3422 u64 dev_offset;
3423 struct btrfs_key key;
3424 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3425 struct btrfs_device *device;
3426 struct btrfs_chunk *chunk;
3427 struct btrfs_stripe *stripe;
3428 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3429 int index = 0;
3430 int ret;
3431
3432 chunk = kzalloc(item_size, GFP_NOFS);
3433 if (!chunk)
3434 return -ENOMEM;
3435
3436 index = 0;
3437 while (index < map->num_stripes) {
3438 device = map->stripes[index].dev;
3439 device->bytes_used += stripe_size;
3440 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003441 if (ret)
3442 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003443 index++;
3444 }
3445
Josef Bacik2bf64752011-09-26 17:12:22 -04003446 spin_lock(&extent_root->fs_info->free_chunk_lock);
3447 extent_root->fs_info->free_chunk_space -= (stripe_size *
3448 map->num_stripes);
3449 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3450
Yan Zheng2b820322008-11-17 21:11:30 -05003451 index = 0;
3452 stripe = &chunk->stripe;
3453 while (index < map->num_stripes) {
3454 device = map->stripes[index].dev;
3455 dev_offset = map->stripes[index].physical;
3456
3457 btrfs_set_stack_stripe_devid(stripe, device->devid);
3458 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3459 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3460 stripe++;
3461 index++;
3462 }
3463
3464 btrfs_set_stack_chunk_length(chunk, chunk_size);
3465 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3466 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3467 btrfs_set_stack_chunk_type(chunk, map->type);
3468 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3469 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3470 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3471 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3472 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3473
3474 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3475 key.type = BTRFS_CHUNK_ITEM_KEY;
3476 key.offset = chunk_offset;
3477
3478 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003479
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003480 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3481 /*
3482 * TODO: Cleanup of inserted chunk root in case of
3483 * failure.
3484 */
Li Zefan125ccb02011-12-08 15:07:24 +08003485 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003486 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003487 }
liubo1abe9b82011-03-24 11:18:59 +00003488
Mark Fasheh3acd3952011-09-08 17:40:01 -07003489out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003490 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003491 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003492}
3493
3494/*
3495 * Chunk allocation falls into two parts. The first part does works
3496 * that make the new allocated chunk useable, but not do any operation
3497 * that modifies the chunk tree. The second part does the works that
3498 * require modifying the chunk tree. This division is important for the
3499 * bootstrap process of adding storage to a seed btrfs.
3500 */
3501int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3502 struct btrfs_root *extent_root, u64 type)
3503{
3504 u64 chunk_offset;
3505 u64 chunk_size;
3506 u64 stripe_size;
3507 struct map_lookup *map;
3508 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3509 int ret;
3510
3511 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3512 &chunk_offset);
3513 if (ret)
3514 return ret;
3515
3516 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3517 &stripe_size, chunk_offset, type);
3518 if (ret)
3519 return ret;
3520
3521 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3522 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003523 if (ret)
3524 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003525 return 0;
3526}
3527
Chris Masond3977122009-01-05 21:25:51 -05003528static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003529 struct btrfs_root *root,
3530 struct btrfs_device *device)
3531{
3532 u64 chunk_offset;
3533 u64 sys_chunk_offset;
3534 u64 chunk_size;
3535 u64 sys_chunk_size;
3536 u64 stripe_size;
3537 u64 sys_stripe_size;
3538 u64 alloc_profile;
3539 struct map_lookup *map;
3540 struct map_lookup *sys_map;
3541 struct btrfs_fs_info *fs_info = root->fs_info;
3542 struct btrfs_root *extent_root = fs_info->extent_root;
3543 int ret;
3544
3545 ret = find_next_chunk(fs_info->chunk_root,
3546 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003547 if (ret)
3548 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003549
3550 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003551 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003552 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3553
3554 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3555 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003556 if (ret)
3557 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003558
3559 sys_chunk_offset = chunk_offset + chunk_size;
3560
3561 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003562 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003563 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3564
3565 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3566 &sys_chunk_size, &sys_stripe_size,
3567 sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003568 if (ret)
3569 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003570
3571 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003572 if (ret)
3573 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003574
3575 /*
3576 * Modifying chunk tree needs allocating new blocks from both
3577 * system block group and metadata block group. So we only can
3578 * do operations require modifying the chunk tree after both
3579 * block groups were created.
3580 */
3581 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3582 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003583 if (ret)
3584 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003585
3586 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3587 sys_chunk_offset, sys_chunk_size,
3588 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003589 if (ret)
3590 goto abort;
3591
Yan Zheng2b820322008-11-17 21:11:30 -05003592 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003593
3594abort:
3595 btrfs_abort_transaction(trans, root, ret);
3596 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003597}
3598
3599int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3600{
3601 struct extent_map *em;
3602 struct map_lookup *map;
3603 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3604 int readonly = 0;
3605 int i;
3606
Chris Mason890871b2009-09-02 16:24:52 -04003607 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003608 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003609 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003610 if (!em)
3611 return 1;
3612
Josef Bacikf48b9072010-01-27 02:07:59 +00003613 if (btrfs_test_opt(root, DEGRADED)) {
3614 free_extent_map(em);
3615 return 0;
3616 }
3617
Yan Zheng2b820322008-11-17 21:11:30 -05003618 map = (struct map_lookup *)em->bdev;
3619 for (i = 0; i < map->num_stripes; i++) {
3620 if (!map->stripes[i].dev->writeable) {
3621 readonly = 1;
3622 break;
3623 }
3624 }
3625 free_extent_map(em);
3626 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003627}
3628
3629void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3630{
David Sterbaa8067e02011-04-21 00:34:43 +02003631 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003632}
3633
3634void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3635{
3636 struct extent_map *em;
3637
Chris Masond3977122009-01-05 21:25:51 -05003638 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003639 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003640 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3641 if (em)
3642 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003643 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003644 if (!em)
3645 break;
3646 kfree(em->bdev);
3647 /* once for us */
3648 free_extent_map(em);
3649 /* once for the tree */
3650 free_extent_map(em);
3651 }
3652}
3653
Chris Masonf1885912008-04-09 16:28:12 -04003654int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3655{
3656 struct extent_map *em;
3657 struct map_lookup *map;
3658 struct extent_map_tree *em_tree = &map_tree->map_tree;
3659 int ret;
3660
Chris Mason890871b2009-09-02 16:24:52 -04003661 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003662 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003663 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003664 BUG_ON(!em);
3665
3666 BUG_ON(em->start > logical || em->start + em->len < logical);
3667 map = (struct map_lookup *)em->bdev;
3668 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3669 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003670 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3671 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003672 else
3673 ret = 1;
3674 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003675 return ret;
3676}
3677
Chris Masondfe25022008-05-13 13:46:40 -04003678static int find_live_mirror(struct map_lookup *map, int first, int num,
3679 int optimal)
3680{
3681 int i;
3682 if (map->stripes[optimal].dev->bdev)
3683 return optimal;
3684 for (i = first; i < first + num; i++) {
3685 if (map->stripes[i].dev->bdev)
3686 return i;
3687 }
3688 /* we couldn't find one that doesn't fail. Just return something
3689 * and the io error handling code will clean up eventually
3690 */
3691 return optimal;
3692}
3693
Chris Masonf2d8d742008-04-21 10:03:05 -04003694static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3695 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003696 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003697 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003698{
3699 struct extent_map *em;
3700 struct map_lookup *map;
3701 struct extent_map_tree *em_tree = &map_tree->map_tree;
3702 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003703 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003704 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003705 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003706 u64 stripe_nr_orig;
3707 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003708 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003709 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003710 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003711 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003712 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003713 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003714
Chris Mason890871b2009-09-02 16:24:52 -04003715 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003716 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003717 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003718
Chris Mason3b951512008-04-17 11:29:12 -04003719 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003720 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3721 (unsigned long long)logical,
3722 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003723 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003724 }
Chris Mason0b86a832008-03-24 15:01:56 -04003725
3726 BUG_ON(em->start > logical || em->start + em->len < logical);
3727 map = (struct map_lookup *)em->bdev;
3728 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003729
Chris Masonf1885912008-04-09 16:28:12 -04003730 if (mirror_num > map->num_stripes)
3731 mirror_num = 0;
3732
Chris Mason593060d2008-03-25 16:50:33 -04003733 stripe_nr = offset;
3734 /*
3735 * stripe_nr counts the total number of stripes we have to stride
3736 * to get to this block
3737 */
3738 do_div(stripe_nr, map->stripe_len);
3739
3740 stripe_offset = stripe_nr * map->stripe_len;
3741 BUG_ON(offset < stripe_offset);
3742
3743 /* stripe_offset is the offset of this block in its stripe*/
3744 stripe_offset = offset - stripe_offset;
3745
Li Dongyangfce3bb92011-03-24 10:24:26 +00003746 if (rw & REQ_DISCARD)
3747 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003748 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003749 /* we limit the length of each bio to what fits in a stripe */
3750 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003751 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003752 } else {
3753 *length = em->len - offset;
3754 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003755
Jan Schmidta1d3c472011-08-04 17:15:33 +02003756 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003757 goto out;
3758
Chris Masonf2d8d742008-04-21 10:03:05 -04003759 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003760 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003761 stripe_nr_orig = stripe_nr;
3762 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3763 (~(map->stripe_len - 1));
3764 do_div(stripe_nr_end, map->stripe_len);
3765 stripe_end_offset = stripe_nr_end * map->stripe_len -
3766 (offset + *length);
3767 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3768 if (rw & REQ_DISCARD)
3769 num_stripes = min_t(u64, map->num_stripes,
3770 stripe_nr_end - stripe_nr_orig);
3771 stripe_index = do_div(stripe_nr, map->num_stripes);
3772 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003773 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003774 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003775 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003776 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003777 else {
3778 stripe_index = find_live_mirror(map, 0,
3779 map->num_stripes,
3780 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003781 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003782 }
Chris Mason2fff7342008-04-29 14:12:09 -04003783
Chris Mason611f0e02008-04-03 16:29:03 -04003784 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003785 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003786 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003787 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003788 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003789 } else {
3790 mirror_num = 1;
3791 }
Chris Mason2fff7342008-04-29 14:12:09 -04003792
Chris Mason321aecc2008-04-16 10:49:51 -04003793 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3794 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003795
3796 stripe_index = do_div(stripe_nr, factor);
3797 stripe_index *= map->sub_stripes;
3798
Jens Axboe7eaceac2011-03-10 08:52:07 +01003799 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003800 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003801 else if (rw & REQ_DISCARD)
3802 num_stripes = min_t(u64, map->sub_stripes *
3803 (stripe_nr_end - stripe_nr_orig),
3804 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003805 else if (mirror_num)
3806 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003807 else {
3808 stripe_index = find_live_mirror(map, stripe_index,
3809 map->sub_stripes, stripe_index +
3810 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003811 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003812 }
Chris Mason8790d502008-04-03 16:29:03 -04003813 } else {
3814 /*
3815 * after this do_div call, stripe_nr is the number of stripes
3816 * on this device we have to walk to find the data, and
3817 * stripe_index is the number of our device in the stripe array
3818 */
3819 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003820 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003821 }
Chris Mason593060d2008-03-25 16:50:33 -04003822 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003823
Li Zefande11cc12011-12-01 12:55:47 +08003824 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3825 if (!bbio) {
3826 ret = -ENOMEM;
3827 goto out;
3828 }
3829 atomic_set(&bbio->error, 0);
3830
Li Dongyangfce3bb92011-03-24 10:24:26 +00003831 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003832 int factor = 0;
3833 int sub_stripes = 0;
3834 u64 stripes_per_dev = 0;
3835 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003836 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003837
3838 if (map->type &
3839 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3840 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3841 sub_stripes = 1;
3842 else
3843 sub_stripes = map->sub_stripes;
3844
3845 factor = map->num_stripes / sub_stripes;
3846 stripes_per_dev = div_u64_rem(stripe_nr_end -
3847 stripe_nr_orig,
3848 factor,
3849 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04003850 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3851 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003852 }
3853
Li Dongyangfce3bb92011-03-24 10:24:26 +00003854 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003855 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003856 map->stripes[stripe_index].physical +
3857 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003858 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003859
Li Zefanec9ef7a2011-12-01 14:06:42 +08003860 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3861 BTRFS_BLOCK_GROUP_RAID10)) {
3862 bbio->stripes[i].length = stripes_per_dev *
3863 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003864
Li Zefanec9ef7a2011-12-01 14:06:42 +08003865 if (i / sub_stripes < remaining_stripes)
3866 bbio->stripes[i].length +=
3867 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003868
3869 /*
3870 * Special for the first stripe and
3871 * the last stripe:
3872 *
3873 * |-------|...|-------|
3874 * |----------|
3875 * off end_off
3876 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08003877 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003878 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003879 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003880
3881 if (stripe_index >= last_stripe &&
3882 stripe_index <= (last_stripe +
3883 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08003884 bbio->stripes[i].length -=
3885 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003886
Li Zefanec9ef7a2011-12-01 14:06:42 +08003887 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003888 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003889 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003890 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003891
3892 stripe_index++;
3893 if (stripe_index == map->num_stripes) {
3894 /* This could only happen for RAID0/10 */
3895 stripe_index = 0;
3896 stripe_nr++;
3897 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003898 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003899 } else {
3900 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003901 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003902 map->stripes[stripe_index].physical +
3903 stripe_offset +
3904 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003905 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003906 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003907 stripe_index++;
3908 }
Chris Mason593060d2008-03-25 16:50:33 -04003909 }
Li Zefande11cc12011-12-01 12:55:47 +08003910
3911 if (rw & REQ_WRITE) {
3912 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3913 BTRFS_BLOCK_GROUP_RAID10 |
3914 BTRFS_BLOCK_GROUP_DUP)) {
3915 max_errors = 1;
3916 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003917 }
Li Zefande11cc12011-12-01 12:55:47 +08003918
3919 *bbio_ret = bbio;
3920 bbio->num_stripes = num_stripes;
3921 bbio->max_errors = max_errors;
3922 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003923out:
Chris Mason0b86a832008-03-24 15:01:56 -04003924 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003925 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003926}
3927
Chris Masonf2d8d742008-04-21 10:03:05 -04003928int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3929 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003930 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003931{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003932 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003933 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003934}
3935
Yan Zhenga512bbf2008-12-08 16:46:26 -05003936int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3937 u64 chunk_start, u64 physical, u64 devid,
3938 u64 **logical, int *naddrs, int *stripe_len)
3939{
3940 struct extent_map_tree *em_tree = &map_tree->map_tree;
3941 struct extent_map *em;
3942 struct map_lookup *map;
3943 u64 *buf;
3944 u64 bytenr;
3945 u64 length;
3946 u64 stripe_nr;
3947 int i, j, nr = 0;
3948
Chris Mason890871b2009-09-02 16:24:52 -04003949 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003950 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003951 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003952
3953 BUG_ON(!em || em->start != chunk_start);
3954 map = (struct map_lookup *)em->bdev;
3955
3956 length = em->len;
3957 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3958 do_div(length, map->num_stripes / map->sub_stripes);
3959 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3960 do_div(length, map->num_stripes);
3961
3962 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003963 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003964
3965 for (i = 0; i < map->num_stripes; i++) {
3966 if (devid && map->stripes[i].dev->devid != devid)
3967 continue;
3968 if (map->stripes[i].physical > physical ||
3969 map->stripes[i].physical + length <= physical)
3970 continue;
3971
3972 stripe_nr = physical - map->stripes[i].physical;
3973 do_div(stripe_nr, map->stripe_len);
3974
3975 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3976 stripe_nr = stripe_nr * map->num_stripes + i;
3977 do_div(stripe_nr, map->sub_stripes);
3978 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3979 stripe_nr = stripe_nr * map->num_stripes + i;
3980 }
3981 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003982 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003983 for (j = 0; j < nr; j++) {
3984 if (buf[j] == bytenr)
3985 break;
3986 }
Chris Mason934d3752008-12-08 16:43:10 -05003987 if (j == nr) {
3988 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003989 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003990 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003991 }
3992
Yan Zhenga512bbf2008-12-08 16:46:26 -05003993 *logical = buf;
3994 *naddrs = nr;
3995 *stripe_len = map->stripe_len;
3996
3997 free_extent_map(em);
3998 return 0;
3999}
4000
Jan Schmidta1d3c472011-08-04 17:15:33 +02004001static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004002{
Jan Schmidta1d3c472011-08-04 17:15:33 +02004003 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004004 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004005
Chris Mason8790d502008-04-03 16:29:03 -04004006 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004007 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04004008
Jan Schmidta1d3c472011-08-04 17:15:33 +02004009 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004010 is_orig_bio = 1;
4011
Jan Schmidta1d3c472011-08-04 17:15:33 +02004012 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004013 if (!is_orig_bio) {
4014 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004015 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004016 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004017 bio->bi_private = bbio->private;
4018 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004019 bio->bi_bdev = (struct block_device *)
4020 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004021 /* only send an error to the higher layers if it is
4022 * beyond the tolerance of the multi-bio
4023 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004024 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004025 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004026 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004027 /*
4028 * this bio is actually up to date, we didn't
4029 * go over the max number of errors
4030 */
4031 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004032 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004033 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004034 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004035
4036 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004037 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004038 bio_put(bio);
4039 }
Chris Mason8790d502008-04-03 16:29:03 -04004040}
4041
Chris Mason8b712842008-06-11 16:50:36 -04004042struct async_sched {
4043 struct bio *bio;
4044 int rw;
4045 struct btrfs_fs_info *info;
4046 struct btrfs_work work;
4047};
4048
4049/*
4050 * see run_scheduled_bios for a description of why bios are collected for
4051 * async submit.
4052 *
4053 * This will add one bio to the pending list for a device and make sure
4054 * the work struct is scheduled.
4055 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004056static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004057 struct btrfs_device *device,
4058 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004059{
4060 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004061 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004062
4063 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004064 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004065 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004066 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004067 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004068 return;
Chris Mason8b712842008-06-11 16:50:36 -04004069 }
4070
4071 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004072 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004073 * higher layers. Otherwise, the async bio makes it appear we have
4074 * made progress against dirty pages when we've really just put it
4075 * on a queue for later
4076 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004077 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004078 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004079 bio->bi_next = NULL;
4080 bio->bi_rw |= rw;
4081
4082 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004083 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004084 pending_bios = &device->pending_sync_bios;
4085 else
4086 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004087
Chris Masonffbd5172009-04-20 15:50:09 -04004088 if (pending_bios->tail)
4089 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004090
Chris Masonffbd5172009-04-20 15:50:09 -04004091 pending_bios->tail = bio;
4092 if (!pending_bios->head)
4093 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004094 if (device->running_pending)
4095 should_queue = 0;
4096
4097 spin_unlock(&device->io_lock);
4098
4099 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004100 btrfs_queue_worker(&root->fs_info->submit_workers,
4101 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004102}
4103
Chris Masonf1885912008-04-09 16:28:12 -04004104int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004105 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004106{
4107 struct btrfs_mapping_tree *map_tree;
4108 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004109 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004110 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004111 u64 length = 0;
4112 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004113 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004114 int dev_nr = 0;
4115 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004116 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004117
Chris Masonf2d8d742008-04-21 10:03:05 -04004118 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004119 map_tree = &root->fs_info->mapping_tree;
4120 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004121
Jan Schmidta1d3c472011-08-04 17:15:33 +02004122 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004123 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004124 if (ret) /* -ENOMEM */
4125 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004126
Jan Schmidta1d3c472011-08-04 17:15:33 +02004127 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004128 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004129 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4130 "len %llu\n", (unsigned long long)logical,
4131 (unsigned long long)length,
4132 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004133 BUG();
4134 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004135
4136 bbio->orig_bio = first_bio;
4137 bbio->private = first_bio->bi_private;
4138 bbio->end_io = first_bio->bi_end_io;
4139 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004140
Chris Masond3977122009-01-05 21:25:51 -05004141 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004142 if (dev_nr < total_devs - 1) {
4143 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004144 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004145 } else {
4146 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004147 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004148 bio->bi_private = bbio;
4149 bio->bi_end_io = btrfs_end_bio;
4150 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4151 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004152 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004153 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4154 "(%s id %llu), size=%u\n", rw,
4155 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4156 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004157 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004158 if (async_submit)
4159 schedule_bio(root, dev, rw, bio);
4160 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004161 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004162 } else {
4163 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4164 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004165 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004166 }
Chris Mason8790d502008-04-03 16:29:03 -04004167 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004168 }
Chris Mason0b86a832008-03-24 15:01:56 -04004169 return 0;
4170}
4171
Chris Masona4437552008-04-18 10:29:38 -04004172struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004173 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004174{
Yan Zheng2b820322008-11-17 21:11:30 -05004175 struct btrfs_device *device;
4176 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004177
Yan Zheng2b820322008-11-17 21:11:30 -05004178 cur_devices = root->fs_info->fs_devices;
4179 while (cur_devices) {
4180 if (!fsid ||
4181 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4182 device = __find_device(&cur_devices->devices,
4183 devid, uuid);
4184 if (device)
4185 return device;
4186 }
4187 cur_devices = cur_devices->seed;
4188 }
4189 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004190}
4191
Chris Masondfe25022008-05-13 13:46:40 -04004192static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4193 u64 devid, u8 *dev_uuid)
4194{
4195 struct btrfs_device *device;
4196 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4197
4198 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004199 if (!device)
4200 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004201 list_add(&device->dev_list,
4202 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004203 device->dev_root = root->fs_info->dev_root;
4204 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004205 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004206 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004207 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004208 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004209 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004210 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004211 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004212 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4213 return device;
4214}
4215
Chris Mason0b86a832008-03-24 15:01:56 -04004216static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4217 struct extent_buffer *leaf,
4218 struct btrfs_chunk *chunk)
4219{
4220 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4221 struct map_lookup *map;
4222 struct extent_map *em;
4223 u64 logical;
4224 u64 length;
4225 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004226 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004227 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004228 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004229 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004230
Chris Masone17cade2008-04-15 15:41:47 -04004231 logical = key->offset;
4232 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004233
Chris Mason890871b2009-09-02 16:24:52 -04004234 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004235 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004236 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004237
4238 /* already mapped? */
4239 if (em && em->start <= logical && em->start + em->len > logical) {
4240 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004241 return 0;
4242 } else if (em) {
4243 free_extent_map(em);
4244 }
Chris Mason0b86a832008-03-24 15:01:56 -04004245
David Sterba172ddd62011-04-21 00:48:27 +02004246 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004247 if (!em)
4248 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004249 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4250 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004251 if (!map) {
4252 free_extent_map(em);
4253 return -ENOMEM;
4254 }
4255
4256 em->bdev = (struct block_device *)map;
4257 em->start = logical;
4258 em->len = length;
4259 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004260 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004261
Chris Mason593060d2008-03-25 16:50:33 -04004262 map->num_stripes = num_stripes;
4263 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4264 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4265 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4266 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4267 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004268 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004269 for (i = 0; i < num_stripes; i++) {
4270 map->stripes[i].physical =
4271 btrfs_stripe_offset_nr(leaf, chunk, i);
4272 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004273 read_extent_buffer(leaf, uuid, (unsigned long)
4274 btrfs_stripe_dev_uuid_nr(chunk, i),
4275 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004276 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4277 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004278 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004279 kfree(map);
4280 free_extent_map(em);
4281 return -EIO;
4282 }
Chris Masondfe25022008-05-13 13:46:40 -04004283 if (!map->stripes[i].dev) {
4284 map->stripes[i].dev =
4285 add_missing_dev(root, devid, uuid);
4286 if (!map->stripes[i].dev) {
4287 kfree(map);
4288 free_extent_map(em);
4289 return -EIO;
4290 }
4291 }
4292 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004293 }
4294
Chris Mason890871b2009-09-02 16:24:52 -04004295 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004296 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004297 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004298 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004299 free_extent_map(em);
4300
4301 return 0;
4302}
4303
Jeff Mahoney143bede2012-03-01 14:56:26 +01004304static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004305 struct btrfs_dev_item *dev_item,
4306 struct btrfs_device *device)
4307{
4308 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004309
4310 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004311 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4312 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004313 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4314 device->type = btrfs_device_type(leaf, dev_item);
4315 device->io_align = btrfs_device_io_align(leaf, dev_item);
4316 device->io_width = btrfs_device_io_width(leaf, dev_item);
4317 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004318
4319 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004320 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004321}
4322
Yan Zheng2b820322008-11-17 21:11:30 -05004323static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4324{
4325 struct btrfs_fs_devices *fs_devices;
4326 int ret;
4327
Li Zefanb367e472011-12-07 11:38:24 +08004328 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004329
4330 fs_devices = root->fs_info->fs_devices->seed;
4331 while (fs_devices) {
4332 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4333 ret = 0;
4334 goto out;
4335 }
4336 fs_devices = fs_devices->seed;
4337 }
4338
4339 fs_devices = find_fsid(fsid);
4340 if (!fs_devices) {
4341 ret = -ENOENT;
4342 goto out;
4343 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004344
4345 fs_devices = clone_fs_devices(fs_devices);
4346 if (IS_ERR(fs_devices)) {
4347 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004348 goto out;
4349 }
4350
Christoph Hellwig97288f22008-12-02 06:36:09 -05004351 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004352 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05004353 if (ret)
4354 goto out;
4355
4356 if (!fs_devices->seeding) {
4357 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004358 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004359 ret = -EINVAL;
4360 goto out;
4361 }
4362
4363 fs_devices->seed = root->fs_info->fs_devices->seed;
4364 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004365out:
Yan Zheng2b820322008-11-17 21:11:30 -05004366 return ret;
4367}
4368
Chris Mason0d81ba52008-03-24 15:02:07 -04004369static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004370 struct extent_buffer *leaf,
4371 struct btrfs_dev_item *dev_item)
4372{
4373 struct btrfs_device *device;
4374 u64 devid;
4375 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004376 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004377 u8 dev_uuid[BTRFS_UUID_SIZE];
4378
Chris Mason0b86a832008-03-24 15:01:56 -04004379 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004380 read_extent_buffer(leaf, dev_uuid,
4381 (unsigned long)btrfs_device_uuid(dev_item),
4382 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004383 read_extent_buffer(leaf, fs_uuid,
4384 (unsigned long)btrfs_device_fsid(dev_item),
4385 BTRFS_UUID_SIZE);
4386
4387 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4388 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004389 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004390 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004391 }
4392
4393 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4394 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004395 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004396 return -EIO;
4397
4398 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004399 printk(KERN_WARNING "warning devid %llu missing\n",
4400 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004401 device = add_missing_dev(root, devid, dev_uuid);
4402 if (!device)
4403 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004404 } else if (!device->missing) {
4405 /*
4406 * this happens when a device that was properly setup
4407 * in the device info lists suddenly goes bad.
4408 * device->bdev is NULL, and so we have to set
4409 * device->missing to one here
4410 */
4411 root->fs_info->fs_devices->missing_devices++;
4412 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004413 }
4414 }
4415
4416 if (device->fs_devices != root->fs_info->fs_devices) {
4417 BUG_ON(device->writeable);
4418 if (device->generation !=
4419 btrfs_device_generation(leaf, dev_item))
4420 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004421 }
Chris Mason0b86a832008-03-24 15:01:56 -04004422
4423 fill_device_from_item(leaf, dev_item, device);
4424 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004425 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004426 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004427 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004428 spin_lock(&root->fs_info->free_chunk_lock);
4429 root->fs_info->free_chunk_space += device->total_bytes -
4430 device->bytes_used;
4431 spin_unlock(&root->fs_info->free_chunk_lock);
4432 }
Chris Mason0b86a832008-03-24 15:01:56 -04004433 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004434 return ret;
4435}
4436
Yan Zhenge4404d62008-12-12 10:03:26 -05004437int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004438{
David Sterba6c417612011-04-13 15:41:04 +02004439 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004440 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004441 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004442 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004443 u8 *ptr;
4444 unsigned long sb_ptr;
4445 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004446 u32 num_stripes;
4447 u32 array_size;
4448 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004449 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004450 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004451
Yan Zhenge4404d62008-12-12 10:03:26 -05004452 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004453 BTRFS_SUPER_INFO_SIZE);
4454 if (!sb)
4455 return -ENOMEM;
4456 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004457 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004458 /*
4459 * The sb extent buffer is artifical and just used to read the system array.
4460 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4461 * pages up-to-date when the page is larger: extent does not cover the
4462 * whole page and consequently check_page_uptodate does not find all
4463 * the page's extents up-to-date (the hole beyond sb),
4464 * write_extent_buffer then triggers a WARN_ON.
4465 *
4466 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4467 * but sb spans only this function. Add an explicit SetPageUptodate call
4468 * to silence the warning eg. on PowerPC 64.
4469 */
4470 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004471 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004472
Chris Masona061fc82008-05-07 11:43:44 -04004473 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004474 array_size = btrfs_super_sys_array_size(super_copy);
4475
Chris Mason0b86a832008-03-24 15:01:56 -04004476 ptr = super_copy->sys_chunk_array;
4477 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4478 cur = 0;
4479
4480 while (cur < array_size) {
4481 disk_key = (struct btrfs_disk_key *)ptr;
4482 btrfs_disk_key_to_cpu(&key, disk_key);
4483
Chris Masona061fc82008-05-07 11:43:44 -04004484 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004485 sb_ptr += len;
4486 cur += len;
4487
Chris Mason0d81ba52008-03-24 15:02:07 -04004488 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004489 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004490 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004491 if (ret)
4492 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004493 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4494 len = btrfs_chunk_item_size(num_stripes);
4495 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004496 ret = -EIO;
4497 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004498 }
4499 ptr += len;
4500 sb_ptr += len;
4501 cur += len;
4502 }
Chris Masona061fc82008-05-07 11:43:44 -04004503 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004504 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004505}
4506
4507int btrfs_read_chunk_tree(struct btrfs_root *root)
4508{
4509 struct btrfs_path *path;
4510 struct extent_buffer *leaf;
4511 struct btrfs_key key;
4512 struct btrfs_key found_key;
4513 int ret;
4514 int slot;
4515
4516 root = root->fs_info->chunk_root;
4517
4518 path = btrfs_alloc_path();
4519 if (!path)
4520 return -ENOMEM;
4521
Li Zefanb367e472011-12-07 11:38:24 +08004522 mutex_lock(&uuid_mutex);
4523 lock_chunks(root);
4524
Chris Mason0b86a832008-03-24 15:01:56 -04004525 /* first we search for all of the device items, and then we
4526 * read in all of the chunk items. This way we can create chunk
4527 * mappings that reference all of the devices that are afound
4528 */
4529 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4530 key.offset = 0;
4531 key.type = 0;
4532again:
4533 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004534 if (ret < 0)
4535 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004536 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004537 leaf = path->nodes[0];
4538 slot = path->slots[0];
4539 if (slot >= btrfs_header_nritems(leaf)) {
4540 ret = btrfs_next_leaf(root, path);
4541 if (ret == 0)
4542 continue;
4543 if (ret < 0)
4544 goto error;
4545 break;
4546 }
4547 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4548 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4549 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4550 break;
4551 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4552 struct btrfs_dev_item *dev_item;
4553 dev_item = btrfs_item_ptr(leaf, slot,
4554 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004555 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004556 if (ret)
4557 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004558 }
4559 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4560 struct btrfs_chunk *chunk;
4561 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4562 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004563 if (ret)
4564 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004565 }
4566 path->slots[0]++;
4567 }
4568 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4569 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004570 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004571 goto again;
4572 }
Chris Mason0b86a832008-03-24 15:01:56 -04004573 ret = 0;
4574error:
Li Zefanb367e472011-12-07 11:38:24 +08004575 unlock_chunks(root);
4576 mutex_unlock(&uuid_mutex);
4577
Yan Zheng2b820322008-11-17 21:11:30 -05004578 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004579 return ret;
4580}