blob: 28addea5b0f2aec4539ec0e5eae20e96f9d45d9e [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
Chris Mason8a4b83c2008-03-24 15:02:07 -040070int btrfs_cleanup_fs_uuids(void)
71{
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 }
80 return 0;
81}
82
Chris Masona1b32a52008-09-05 16:09:51 -040083static noinline struct btrfs_device *__find_device(struct list_head *head,
84 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040085{
86 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040087
Qinghuang Fengc6e30872009-01-21 10:59:08 -050088 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040089 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040090 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040091 return dev;
Chris Masona4437552008-04-18 10:29:38 -040092 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040093 }
94 return NULL;
95}
96
Chris Masona1b32a52008-09-05 16:09:51 -040097static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040098{
Chris Mason8a4b83c2008-03-24 15:02:07 -040099 struct btrfs_fs_devices *fs_devices;
100
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500101 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
103 return fs_devices;
104 }
105 return NULL;
106}
107
Chris Masonffbd5172009-04-20 15:50:09 -0400108static void requeue_list(struct btrfs_pending_bios *pending_bios,
109 struct bio *head, struct bio *tail)
110{
111
112 struct bio *old_head;
113
114 old_head = pending_bios->head;
115 pending_bios->head = head;
116 if (pending_bios->tail)
117 tail->bi_next = old_head;
118 else
119 pending_bios->tail = tail;
120}
121
Chris Mason8b712842008-06-11 16:50:36 -0400122/*
123 * we try to collect pending bios for a device so we don't get a large
124 * number of procs sending bios down to the same device. This greatly
125 * improves the schedulers ability to collect and merge the bios.
126 *
127 * But, it also turns into a long list of bios to process and that is sure
128 * to eventually make the worker thread block. The solution here is to
129 * make some progress and then put this work struct back at the end of
130 * the list if the block device is congested. This way, multiple devices
131 * can make progress from a single worker thread.
132 */
Chris Masond3977122009-01-05 21:25:51 -0500133static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400134{
135 struct bio *pending;
136 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400137 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400138 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400139 struct bio *tail;
140 struct bio *cur;
141 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400142 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400143 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400144 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400145 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400146 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000147 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400148 struct blk_plug plug;
149
150 /*
151 * this function runs all the bios we've collected for
152 * a particular device. We don't want to wander off to
153 * another device without first sending all of these down.
154 * So, setup a plug here and finish it off before we return
155 */
156 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400157
Chris Masonbedf7622009-04-03 10:32:58 -0400158 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400159 fs_info = device->dev_root->fs_info;
160 limit = btrfs_async_submit_limit(fs_info);
161 limit = limit * 2 / 3;
162
Chris Mason8b712842008-06-11 16:50:36 -0400163loop:
164 spin_lock(&device->io_lock);
165
Chris Masona6837052009-02-04 09:19:41 -0500166loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400167 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400168
Chris Mason8b712842008-06-11 16:50:36 -0400169 /* take all the bios off the list at once and process them
170 * later on (without the lock held). But, remember the
171 * tail and other pointers so the bios can be properly reinserted
172 * into the list if we hit congestion
173 */
Chris Masond84275c2009-06-09 15:39:08 -0400174 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400175 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400176 force_reg = 1;
177 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400178 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400179 force_reg = 0;
180 }
Chris Masonffbd5172009-04-20 15:50:09 -0400181
182 pending = pending_bios->head;
183 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400184 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400185
186 /*
187 * if pending was null this time around, no bios need processing
188 * at all and we can stop. Otherwise it'll loop back up again
189 * and do an additional check so no bios are missed.
190 *
191 * device->running_pending is used to synchronize with the
192 * schedule_bio code.
193 */
Chris Masonffbd5172009-04-20 15:50:09 -0400194 if (device->pending_sync_bios.head == NULL &&
195 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400196 again = 0;
197 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400198 } else {
199 again = 1;
200 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400201 }
Chris Masonffbd5172009-04-20 15:50:09 -0400202
203 pending_bios->head = NULL;
204 pending_bios->tail = NULL;
205
Chris Mason8b712842008-06-11 16:50:36 -0400206 spin_unlock(&device->io_lock);
207
Chris Masond3977122009-01-05 21:25:51 -0500208 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400209
210 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400211 /* we want to work on both lists, but do more bios on the
212 * sync list than the regular list
213 */
214 if ((num_run > 32 &&
215 pending_bios != &device->pending_sync_bios &&
216 device->pending_sync_bios.head) ||
217 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
218 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400219 spin_lock(&device->io_lock);
220 requeue_list(pending_bios, pending, tail);
221 goto loop_lock;
222 }
223
Chris Mason8b712842008-06-11 16:50:36 -0400224 cur = pending;
225 pending = pending->bi_next;
226 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400227 atomic_dec(&fs_info->nr_async_bios);
228
229 if (atomic_read(&fs_info->nr_async_bios) < limit &&
230 waitqueue_active(&fs_info->async_submit_wait))
231 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6d2008-07-31 16:29:02 -0400232
233 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400234
Chris Mason2ab1ba62011-08-04 14:28:36 -0400235 /*
236 * if we're doing the sync list, record that our
237 * plug has some sync requests on it
238 *
239 * If we're doing the regular list and there are
240 * sync requests sitting around, unplug before
241 * we add more
242 */
243 if (pending_bios == &device->pending_sync_bios) {
244 sync_pending = 1;
245 } else if (sync_pending) {
246 blk_finish_plug(&plug);
247 blk_start_plug(&plug);
248 sync_pending = 0;
249 }
250
Stefan Behrens21adbd52011-11-09 13:44:05 +0100251 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400252 num_run++;
253 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100254 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400255 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400256
257 /*
258 * we made progress, there is more work to do and the bdi
259 * is now congested. Back off and let other work structs
260 * run instead
261 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400262 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500263 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400264 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400265
Chris Masonb765ead2009-04-03 10:27:10 -0400266 ioc = current->io_context;
267
268 /*
269 * the main goal here is that we don't want to
270 * block if we're going to be able to submit
271 * more requests without blocking.
272 *
273 * This code does two great things, it pokes into
274 * the elevator code from a filesystem _and_
275 * it makes assumptions about how batching works.
276 */
277 if (ioc && ioc->nr_batch_requests > 0 &&
278 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
279 (last_waited == 0 ||
280 ioc->last_waited == last_waited)) {
281 /*
282 * we want to go through our batch of
283 * requests and stop. So, we copy out
284 * the ioc->last_waited time and test
285 * against it before looping
286 */
287 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100288 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400289 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400290 continue;
291 }
Chris Mason8b712842008-06-11 16:50:36 -0400292 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400293 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500294 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400295
296 spin_unlock(&device->io_lock);
297 btrfs_requeue_work(&device->work);
298 goto done;
299 }
Chris Masond85c8a62011-12-15 15:38:41 -0500300 /* unplug every 64 requests just for good measure */
301 if (batch_run % 64 == 0) {
302 blk_finish_plug(&plug);
303 blk_start_plug(&plug);
304 sync_pending = 0;
305 }
Chris Mason8b712842008-06-11 16:50:36 -0400306 }
Chris Masonffbd5172009-04-20 15:50:09 -0400307
Chris Mason51684082010-03-10 15:33:32 -0500308 cond_resched();
309 if (again)
310 goto loop;
311
312 spin_lock(&device->io_lock);
313 if (device->pending_bios.head || device->pending_sync_bios.head)
314 goto loop_lock;
315 spin_unlock(&device->io_lock);
316
Chris Mason8b712842008-06-11 16:50:36 -0400317done:
Chris Mason211588a2011-04-19 20:12:40 -0400318 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400319 return 0;
320}
321
Christoph Hellwigb2950862008-12-02 09:54:17 -0500322static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400323{
324 struct btrfs_device *device;
325
326 device = container_of(work, struct btrfs_device, work);
327 run_scheduled_bios(device);
328}
329
Chris Masona1b32a52008-09-05 16:09:51 -0400330static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400331 struct btrfs_super_block *disk_super,
332 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
333{
334 struct btrfs_device *device;
335 struct btrfs_fs_devices *fs_devices;
336 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000337 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400338
339 fs_devices = find_fsid(disk_super->fsid);
340 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400341 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400342 if (!fs_devices)
343 return -ENOMEM;
344 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400345 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400346 list_add(&fs_devices->list, &fs_uuids);
347 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
348 fs_devices->latest_devid = devid;
349 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400350 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400351 device = NULL;
352 } else {
Chris Masona4437552008-04-18 10:29:38 -0400353 device = __find_device(&fs_devices->devices, devid,
354 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400355 }
356 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500357 if (fs_devices->opened)
358 return -EBUSY;
359
Chris Mason8a4b83c2008-03-24 15:02:07 -0400360 device = kzalloc(sizeof(*device), GFP_NOFS);
361 if (!device) {
362 /* we can safely leave the fs_devices entry around */
363 return -ENOMEM;
364 }
365 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400366 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400367 memcpy(device->uuid, disk_super->dev_item.uuid,
368 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400369 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370 device->name = kstrdup(path, GFP_NOFS);
371 if (!device->name) {
372 kfree(device);
373 return -ENOMEM;
374 }
Yan Zheng2b820322008-11-17 21:11:30 -0500375 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400376
Arne Jansen90519d62011-05-23 14:30:00 +0200377 /* init readahead state */
378 spin_lock_init(&device->reada_lock);
379 device->reada_curr_zone = NULL;
380 atomic_set(&device->reada_in_flight, 0);
381 device->reada_next = 0;
382 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
383 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
384
Chris Masone5e9a522009-06-10 15:17:02 -0400385 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000386 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400387 mutex_unlock(&fs_devices->device_list_mutex);
388
Yan Zheng2b820322008-11-17 21:11:30 -0500389 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400390 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500391 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000392 name = kstrdup(path, GFP_NOFS);
393 if (!name)
394 return -ENOMEM;
395 kfree(device->name);
396 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500397 if (device->missing) {
398 fs_devices->missing_devices--;
399 device->missing = 0;
400 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400401 }
402
403 if (found_transid > fs_devices->latest_trans) {
404 fs_devices->latest_devid = devid;
405 fs_devices->latest_trans = found_transid;
406 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400407 *fs_devices_ret = fs_devices;
408 return 0;
409}
410
Yan Zhenge4404d62008-12-12 10:03:26 -0500411static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
412{
413 struct btrfs_fs_devices *fs_devices;
414 struct btrfs_device *device;
415 struct btrfs_device *orig_dev;
416
417 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
418 if (!fs_devices)
419 return ERR_PTR(-ENOMEM);
420
421 INIT_LIST_HEAD(&fs_devices->devices);
422 INIT_LIST_HEAD(&fs_devices->alloc_list);
423 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400424 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500425 fs_devices->latest_devid = orig->latest_devid;
426 fs_devices->latest_trans = orig->latest_trans;
427 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
428
Xiao Guangrong46224702011-04-20 10:08:47 +0000429 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500430 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
431 device = kzalloc(sizeof(*device), GFP_NOFS);
432 if (!device)
433 goto error;
434
435 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400436 if (!device->name) {
437 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500438 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400439 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500440
441 device->devid = orig_dev->devid;
442 device->work.func = pending_bios_fn;
443 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500444 spin_lock_init(&device->io_lock);
445 INIT_LIST_HEAD(&device->dev_list);
446 INIT_LIST_HEAD(&device->dev_alloc_list);
447
448 list_add(&device->dev_list, &fs_devices->devices);
449 device->fs_devices = fs_devices;
450 fs_devices->num_devices++;
451 }
452 return fs_devices;
453error:
454 free_fs_devices(fs_devices);
455 return ERR_PTR(-ENOMEM);
456}
457
Chris Masondfe25022008-05-13 13:46:40 -0400458int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
459{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500460 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400461
Chris Masona6b0d5c2012-02-20 20:53:43 -0500462 struct block_device *latest_bdev = NULL;
463 u64 latest_devid = 0;
464 u64 latest_transid = 0;
465
Chris Masondfe25022008-05-13 13:46:40 -0400466 mutex_lock(&uuid_mutex);
467again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000468 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500469 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500470 if (device->in_fs_metadata) {
471 if (!latest_transid ||
472 device->generation > latest_transid) {
473 latest_devid = device->devid;
474 latest_transid = device->generation;
475 latest_bdev = device->bdev;
476 }
Yan Zheng2b820322008-11-17 21:11:30 -0500477 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500478 }
Yan Zheng2b820322008-11-17 21:11:30 -0500479
480 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100481 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500482 device->bdev = NULL;
483 fs_devices->open_devices--;
484 }
485 if (device->writeable) {
486 list_del_init(&device->dev_alloc_list);
487 device->writeable = 0;
488 fs_devices->rw_devices--;
489 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500490 list_del_init(&device->dev_list);
491 fs_devices->num_devices--;
492 kfree(device->name);
493 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400494 }
Yan Zheng2b820322008-11-17 21:11:30 -0500495
496 if (fs_devices->seed) {
497 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500498 goto again;
499 }
500
Chris Masona6b0d5c2012-02-20 20:53:43 -0500501 fs_devices->latest_bdev = latest_bdev;
502 fs_devices->latest_devid = latest_devid;
503 fs_devices->latest_trans = latest_transid;
504
Chris Masondfe25022008-05-13 13:46:40 -0400505 mutex_unlock(&uuid_mutex);
506 return 0;
507}
Chris Masona0af4692008-05-13 16:03:06 -0400508
Xiao Guangrong1f781602011-04-20 10:09:16 +0000509static void __free_device(struct work_struct *work)
510{
511 struct btrfs_device *device;
512
513 device = container_of(work, struct btrfs_device, rcu_work);
514
515 if (device->bdev)
516 blkdev_put(device->bdev, device->mode);
517
518 kfree(device->name);
519 kfree(device);
520}
521
522static void free_device(struct rcu_head *head)
523{
524 struct btrfs_device *device;
525
526 device = container_of(head, struct btrfs_device, rcu);
527
528 INIT_WORK(&device->rcu_work, __free_device);
529 schedule_work(&device->rcu_work);
530}
531
Yan Zheng2b820322008-11-17 21:11:30 -0500532static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400533{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400534 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500535
Yan Zheng2b820322008-11-17 21:11:30 -0500536 if (--fs_devices->opened > 0)
537 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400538
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000539 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500540 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000541 struct btrfs_device *new_device;
542
543 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400544 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000545
Yan Zheng2b820322008-11-17 21:11:30 -0500546 if (device->writeable) {
547 list_del_init(&device->dev_alloc_list);
548 fs_devices->rw_devices--;
549 }
550
Josef Bacikd5e20032011-08-04 14:52:27 +0000551 if (device->can_discard)
552 fs_devices->num_can_discard--;
553
Xiao Guangrong1f781602011-04-20 10:09:16 +0000554 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
555 BUG_ON(!new_device);
556 memcpy(new_device, device, sizeof(*new_device));
557 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000558 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000559 new_device->bdev = NULL;
560 new_device->writeable = 0;
561 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000562 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000563 list_replace_rcu(&device->dev_list, &new_device->dev_list);
564
565 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400566 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000567 mutex_unlock(&fs_devices->device_list_mutex);
568
Yan Zhenge4404d62008-12-12 10:03:26 -0500569 WARN_ON(fs_devices->open_devices);
570 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500571 fs_devices->opened = 0;
572 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500573
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574 return 0;
575}
576
Yan Zheng2b820322008-11-17 21:11:30 -0500577int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
578{
Yan Zhenge4404d62008-12-12 10:03:26 -0500579 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500580 int ret;
581
582 mutex_lock(&uuid_mutex);
583 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500584 if (!fs_devices->opened) {
585 seed_devices = fs_devices->seed;
586 fs_devices->seed = NULL;
587 }
Yan Zheng2b820322008-11-17 21:11:30 -0500588 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500589
590 while (seed_devices) {
591 fs_devices = seed_devices;
592 seed_devices = fs_devices->seed;
593 __btrfs_close_devices(fs_devices);
594 free_fs_devices(fs_devices);
595 }
Yan Zheng2b820322008-11-17 21:11:30 -0500596 return ret;
597}
598
Yan Zhenge4404d62008-12-12 10:03:26 -0500599static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
600 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400601{
Josef Bacikd5e20032011-08-04 14:52:27 +0000602 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400603 struct block_device *bdev;
604 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400605 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400606 struct block_device *latest_bdev = NULL;
607 struct buffer_head *bh;
608 struct btrfs_super_block *disk_super;
609 u64 latest_devid = 0;
610 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400611 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500612 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400613 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400614
Tejun Heod4d77622010-11-13 11:55:18 +0100615 flags |= FMODE_EXCL;
616
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500617 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400618 if (device->bdev)
619 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400620 if (!device->name)
621 continue;
622
Tejun Heod4d77622010-11-13 11:55:18 +0100623 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400624 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500625 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400626 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400627 }
Chris Masona061fc82008-05-07 11:43:44 -0400628 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400629
Yan Zhenga512bbf2008-12-08 16:46:26 -0500630 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300631 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400632 goto error_close;
633
634 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000635 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400636 if (devid != device->devid)
637 goto error_brelse;
638
Yan Zheng2b820322008-11-17 21:11:30 -0500639 if (memcmp(device->uuid, disk_super->dev_item.uuid,
640 BTRFS_UUID_SIZE))
641 goto error_brelse;
642
643 device->generation = btrfs_super_generation(disk_super);
644 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400645 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500646 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400647 latest_bdev = bdev;
648 }
649
Yan Zheng2b820322008-11-17 21:11:30 -0500650 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
651 device->writeable = 0;
652 } else {
653 device->writeable = !bdev_read_only(bdev);
654 seeding = 0;
655 }
656
Josef Bacikd5e20032011-08-04 14:52:27 +0000657 q = bdev_get_queue(bdev);
658 if (blk_queue_discard(q)) {
659 device->can_discard = 1;
660 fs_devices->num_can_discard++;
661 }
662
Chris Mason8a4b83c2008-03-24 15:02:07 -0400663 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400664 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500665 device->mode = flags;
666
Chris Masonc2898112009-06-10 09:51:32 -0400667 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
668 fs_devices->rotating = 1;
669
Chris Masona0af4692008-05-13 16:03:06 -0400670 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500671 if (device->writeable) {
672 fs_devices->rw_devices++;
673 list_add(&device->dev_alloc_list,
674 &fs_devices->alloc_list);
675 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000676 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400677 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400678
Chris Masona0af4692008-05-13 16:03:06 -0400679error_brelse:
680 brelse(bh);
681error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100682 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400683error:
684 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400685 }
Chris Masona0af4692008-05-13 16:03:06 -0400686 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300687 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400688 goto out;
689 }
Yan Zheng2b820322008-11-17 21:11:30 -0500690 fs_devices->seeding = seeding;
691 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400692 fs_devices->latest_bdev = latest_bdev;
693 fs_devices->latest_devid = latest_devid;
694 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500695 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400696out:
Yan Zheng2b820322008-11-17 21:11:30 -0500697 return ret;
698}
699
700int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500701 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500702{
703 int ret;
704
705 mutex_lock(&uuid_mutex);
706 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500707 fs_devices->opened++;
708 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500709 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500710 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500711 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400712 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713 return ret;
714}
715
Christoph Hellwig97288f22008-12-02 06:36:09 -0500716int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400717 struct btrfs_fs_devices **fs_devices_ret)
718{
719 struct btrfs_super_block *disk_super;
720 struct block_device *bdev;
721 struct buffer_head *bh;
722 int ret;
723 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400724 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400725
Tejun Heod4d77622010-11-13 11:55:18 +0100726 flags |= FMODE_EXCL;
727 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400728
729 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400730 ret = PTR_ERR(bdev);
731 goto error;
732 }
733
Al Viro10f63272011-11-17 15:05:22 -0500734 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400735 ret = set_blocksize(bdev, 4096);
736 if (ret)
737 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500738 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400739 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000740 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400741 goto error_close;
742 }
743 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000744 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400745 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400746 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500747 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200748 else
749 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500750 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500751 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400752 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
753
Chris Mason8a4b83c2008-03-24 15:02:07 -0400754 brelse(bh);
755error_close:
Al Viro10f63272011-11-17 15:05:22 -0500756 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100757 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400759 return ret;
760}
Chris Mason0b86a832008-03-24 15:01:56 -0400761
Miao Xie6d07bce2011-01-05 10:07:31 +0000762/* helper to account the used device space in the range */
763int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
764 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400765{
766 struct btrfs_key key;
767 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000768 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500769 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000770 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400771 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000772 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400773 struct extent_buffer *l;
774
Miao Xie6d07bce2011-01-05 10:07:31 +0000775 *length = 0;
776
777 if (start >= device->total_bytes)
778 return 0;
779
Yan Zheng2b820322008-11-17 21:11:30 -0500780 path = btrfs_alloc_path();
781 if (!path)
782 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400783 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400784
Chris Mason0b86a832008-03-24 15:01:56 -0400785 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000786 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400787 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000788
789 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400790 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000791 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400792 if (ret > 0) {
793 ret = btrfs_previous_item(root, path, key.objectid, key.type);
794 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000795 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400796 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000797
Chris Mason0b86a832008-03-24 15:01:56 -0400798 while (1) {
799 l = path->nodes[0];
800 slot = path->slots[0];
801 if (slot >= btrfs_header_nritems(l)) {
802 ret = btrfs_next_leaf(root, path);
803 if (ret == 0)
804 continue;
805 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000806 goto out;
807
808 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400809 }
810 btrfs_item_key_to_cpu(l, &key, slot);
811
812 if (key.objectid < device->devid)
813 goto next;
814
815 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000816 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400817
Chris Masond3977122009-01-05 21:25:51 -0500818 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400819 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400820
Chris Mason0b86a832008-03-24 15:01:56 -0400821 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000822 extent_end = key.offset + btrfs_dev_extent_length(l,
823 dev_extent);
824 if (key.offset <= start && extent_end > end) {
825 *length = end - start + 1;
826 break;
827 } else if (key.offset <= start && extent_end > start)
828 *length += extent_end - start;
829 else if (key.offset > start && extent_end <= end)
830 *length += extent_end - key.offset;
831 else if (key.offset > start && key.offset <= end) {
832 *length += end - key.offset + 1;
833 break;
834 } else if (key.offset > end)
835 break;
836
837next:
838 path->slots[0]++;
839 }
840 ret = 0;
841out:
842 btrfs_free_path(path);
843 return ret;
844}
845
Chris Mason0b86a832008-03-24 15:01:56 -0400846/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000847 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000848 * @device: the device which we search the free space in
849 * @num_bytes: the size of the free space that we need
850 * @start: store the start of the free space.
851 * @len: the size of the free space. that we find, or the size of the max
852 * free space if we don't find suitable free space
853 *
Chris Mason0b86a832008-03-24 15:01:56 -0400854 * this uses a pretty simple search, the expectation is that it is
855 * called very infrequently and that a given device has a small number
856 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000857 *
858 * @start is used to store the start of the free space if we find. But if we
859 * don't find suitable free space, it will be used to store the start position
860 * of the max free space.
861 *
862 * @len is used to store the size of the free space that we find.
863 * But if we don't find suitable free space, it is used to store the size of
864 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400865 */
Li Zefan125ccb02011-12-08 15:07:24 +0800866int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000867 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400868{
869 struct btrfs_key key;
870 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000871 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400872 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000873 u64 hole_size;
874 u64 max_hole_start;
875 u64 max_hole_size;
876 u64 extent_end;
877 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400878 u64 search_end = device->total_bytes;
879 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000880 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400881 struct extent_buffer *l;
882
Chris Mason0b86a832008-03-24 15:01:56 -0400883 /* FIXME use last free of some kind */
884
885 /* we don't want to overwrite the superblock on the drive,
886 * so we make sure to start at an offset of at least 1MB
887 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200888 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400889
Miao Xie7bfc8372011-01-05 10:07:26 +0000890 max_hole_start = search_start;
891 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000892 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000893
894 if (search_start >= search_end) {
895 ret = -ENOSPC;
896 goto error;
897 }
898
899 path = btrfs_alloc_path();
900 if (!path) {
901 ret = -ENOMEM;
902 goto error;
903 }
904 path->reada = 2;
905
Chris Mason0b86a832008-03-24 15:01:56 -0400906 key.objectid = device->devid;
907 key.offset = search_start;
908 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000909
Li Zefan125ccb02011-12-08 15:07:24 +0800910 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400911 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000912 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400913 if (ret > 0) {
914 ret = btrfs_previous_item(root, path, key.objectid, key.type);
915 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000916 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400917 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000918
Chris Mason0b86a832008-03-24 15:01:56 -0400919 while (1) {
920 l = path->nodes[0];
921 slot = path->slots[0];
922 if (slot >= btrfs_header_nritems(l)) {
923 ret = btrfs_next_leaf(root, path);
924 if (ret == 0)
925 continue;
926 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000927 goto out;
928
929 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400930 }
931 btrfs_item_key_to_cpu(l, &key, slot);
932
933 if (key.objectid < device->devid)
934 goto next;
935
936 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000937 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400938
Chris Mason0b86a832008-03-24 15:01:56 -0400939 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
940 goto next;
941
Miao Xie7bfc8372011-01-05 10:07:26 +0000942 if (key.offset > search_start) {
943 hole_size = key.offset - search_start;
944
945 if (hole_size > max_hole_size) {
946 max_hole_start = search_start;
947 max_hole_size = hole_size;
948 }
949
950 /*
951 * If this free space is greater than which we need,
952 * it must be the max free space that we have found
953 * until now, so max_hole_start must point to the start
954 * of this free space and the length of this free space
955 * is stored in max_hole_size. Thus, we return
956 * max_hole_start and max_hole_size and go back to the
957 * caller.
958 */
959 if (hole_size >= num_bytes) {
960 ret = 0;
961 goto out;
962 }
963 }
964
Chris Mason0b86a832008-03-24 15:01:56 -0400965 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000966 extent_end = key.offset + btrfs_dev_extent_length(l,
967 dev_extent);
968 if (extent_end > search_start)
969 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400970next:
971 path->slots[0]++;
972 cond_resched();
973 }
Chris Mason0b86a832008-03-24 15:01:56 -0400974
liubo38c01b92011-08-02 02:39:03 +0000975 /*
976 * At this point, search_start should be the end of
977 * allocated dev extents, and when shrinking the device,
978 * search_end may be smaller than search_start.
979 */
980 if (search_end > search_start)
981 hole_size = search_end - search_start;
982
Miao Xie7bfc8372011-01-05 10:07:26 +0000983 if (hole_size > max_hole_size) {
984 max_hole_start = search_start;
985 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400986 }
Chris Mason0b86a832008-03-24 15:01:56 -0400987
Miao Xie7bfc8372011-01-05 10:07:26 +0000988 /* See above. */
989 if (hole_size < num_bytes)
990 ret = -ENOSPC;
991 else
992 ret = 0;
993
994out:
Yan Zheng2b820322008-11-17 21:11:30 -0500995 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000996error:
997 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000998 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000999 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001000 return ret;
1001}
1002
Christoph Hellwigb2950862008-12-02 09:54:17 -05001003static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001004 struct btrfs_device *device,
1005 u64 start)
1006{
1007 int ret;
1008 struct btrfs_path *path;
1009 struct btrfs_root *root = device->dev_root;
1010 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001011 struct btrfs_key found_key;
1012 struct extent_buffer *leaf = NULL;
1013 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001014
1015 path = btrfs_alloc_path();
1016 if (!path)
1017 return -ENOMEM;
1018
1019 key.objectid = device->devid;
1020 key.offset = start;
1021 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001022again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001023 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001024 if (ret > 0) {
1025 ret = btrfs_previous_item(root, path, key.objectid,
1026 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001027 if (ret)
1028 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001029 leaf = path->nodes[0];
1030 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1031 extent = btrfs_item_ptr(leaf, path->slots[0],
1032 struct btrfs_dev_extent);
1033 BUG_ON(found_key.offset > start || found_key.offset +
1034 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001035 key = found_key;
1036 btrfs_release_path(path);
1037 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001038 } else if (ret == 0) {
1039 leaf = path->nodes[0];
1040 extent = btrfs_item_ptr(leaf, path->slots[0],
1041 struct btrfs_dev_extent);
1042 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001043 BUG_ON(ret);
1044
Josef Bacik2bf64752011-09-26 17:12:22 -04001045 if (device->bytes_used > 0) {
1046 u64 len = btrfs_dev_extent_length(leaf, extent);
1047 device->bytes_used -= len;
1048 spin_lock(&root->fs_info->free_chunk_lock);
1049 root->fs_info->free_chunk_space += len;
1050 spin_unlock(&root->fs_info->free_chunk_lock);
1051 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001052 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001053
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001054out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001055 btrfs_free_path(path);
1056 return ret;
1057}
1058
Yan Zheng2b820322008-11-17 21:11:30 -05001059int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001060 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001061 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001062 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001063{
1064 int ret;
1065 struct btrfs_path *path;
1066 struct btrfs_root *root = device->dev_root;
1067 struct btrfs_dev_extent *extent;
1068 struct extent_buffer *leaf;
1069 struct btrfs_key key;
1070
Chris Masondfe25022008-05-13 13:46:40 -04001071 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001072 path = btrfs_alloc_path();
1073 if (!path)
1074 return -ENOMEM;
1075
Chris Mason0b86a832008-03-24 15:01:56 -04001076 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001077 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001078 key.type = BTRFS_DEV_EXTENT_KEY;
1079 ret = btrfs_insert_empty_item(trans, root, path, &key,
1080 sizeof(*extent));
1081 BUG_ON(ret);
1082
1083 leaf = path->nodes[0];
1084 extent = btrfs_item_ptr(leaf, path->slots[0],
1085 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001086 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1087 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1088 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1089
1090 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1091 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1092 BTRFS_UUID_SIZE);
1093
Chris Mason0b86a832008-03-24 15:01:56 -04001094 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1095 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001096 btrfs_free_path(path);
1097 return ret;
1098}
1099
Chris Masona1b32a52008-09-05 16:09:51 -04001100static noinline int find_next_chunk(struct btrfs_root *root,
1101 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001102{
1103 struct btrfs_path *path;
1104 int ret;
1105 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001106 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001107 struct btrfs_key found_key;
1108
1109 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001110 if (!path)
1111 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001112
Chris Masone17cade2008-04-15 15:41:47 -04001113 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001114 key.offset = (u64)-1;
1115 key.type = BTRFS_CHUNK_ITEM_KEY;
1116
1117 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1118 if (ret < 0)
1119 goto error;
1120
1121 BUG_ON(ret == 0);
1122
1123 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1124 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001125 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001126 } else {
1127 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1128 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001129 if (found_key.objectid != objectid)
1130 *offset = 0;
1131 else {
1132 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1133 struct btrfs_chunk);
1134 *offset = found_key.offset +
1135 btrfs_chunk_length(path->nodes[0], chunk);
1136 }
Chris Mason0b86a832008-03-24 15:01:56 -04001137 }
1138 ret = 0;
1139error:
1140 btrfs_free_path(path);
1141 return ret;
1142}
1143
Yan Zheng2b820322008-11-17 21:11:30 -05001144static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001145{
1146 int ret;
1147 struct btrfs_key key;
1148 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001149 struct btrfs_path *path;
1150
1151 root = root->fs_info->chunk_root;
1152
1153 path = btrfs_alloc_path();
1154 if (!path)
1155 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001156
1157 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1158 key.type = BTRFS_DEV_ITEM_KEY;
1159 key.offset = (u64)-1;
1160
1161 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1162 if (ret < 0)
1163 goto error;
1164
1165 BUG_ON(ret == 0);
1166
1167 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1168 BTRFS_DEV_ITEM_KEY);
1169 if (ret) {
1170 *objectid = 1;
1171 } else {
1172 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1173 path->slots[0]);
1174 *objectid = found_key.offset + 1;
1175 }
1176 ret = 0;
1177error:
Yan Zheng2b820322008-11-17 21:11:30 -05001178 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001179 return ret;
1180}
1181
1182/*
1183 * the device information is stored in the chunk root
1184 * the btrfs_device struct should be fully filled in
1185 */
1186int btrfs_add_device(struct btrfs_trans_handle *trans,
1187 struct btrfs_root *root,
1188 struct btrfs_device *device)
1189{
1190 int ret;
1191 struct btrfs_path *path;
1192 struct btrfs_dev_item *dev_item;
1193 struct extent_buffer *leaf;
1194 struct btrfs_key key;
1195 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001196
1197 root = root->fs_info->chunk_root;
1198
1199 path = btrfs_alloc_path();
1200 if (!path)
1201 return -ENOMEM;
1202
Chris Mason0b86a832008-03-24 15:01:56 -04001203 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1204 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001205 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001206
1207 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001208 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001209 if (ret)
1210 goto out;
1211
1212 leaf = path->nodes[0];
1213 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1214
1215 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001216 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001217 btrfs_set_device_type(leaf, dev_item, device->type);
1218 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1219 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1220 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001221 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1222 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001223 btrfs_set_device_group(leaf, dev_item, 0);
1224 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1225 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001226 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001227
Chris Mason0b86a832008-03-24 15:01:56 -04001228 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001229 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001230 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1231 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001232 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001233
Yan Zheng2b820322008-11-17 21:11:30 -05001234 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001235out:
1236 btrfs_free_path(path);
1237 return ret;
1238}
Chris Mason8f18cf12008-04-25 16:53:30 -04001239
Chris Masona061fc82008-05-07 11:43:44 -04001240static int btrfs_rm_dev_item(struct btrfs_root *root,
1241 struct btrfs_device *device)
1242{
1243 int ret;
1244 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001245 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001246 struct btrfs_trans_handle *trans;
1247
1248 root = root->fs_info->chunk_root;
1249
1250 path = btrfs_alloc_path();
1251 if (!path)
1252 return -ENOMEM;
1253
Yan, Zhenga22285a2010-05-16 10:48:46 -04001254 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001255 if (IS_ERR(trans)) {
1256 btrfs_free_path(path);
1257 return PTR_ERR(trans);
1258 }
Chris Masona061fc82008-05-07 11:43:44 -04001259 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1260 key.type = BTRFS_DEV_ITEM_KEY;
1261 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001262 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001263
1264 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1265 if (ret < 0)
1266 goto out;
1267
1268 if (ret > 0) {
1269 ret = -ENOENT;
1270 goto out;
1271 }
1272
1273 ret = btrfs_del_item(trans, root, path);
1274 if (ret)
1275 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001276out:
1277 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001278 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001279 btrfs_commit_transaction(trans, root);
1280 return ret;
1281}
1282
1283int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1284{
1285 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001286 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001287 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001288 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001289 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001290 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001291 u64 all_avail;
1292 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001293 u64 num_devices;
1294 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001295 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001296 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001297
Chris Masona061fc82008-05-07 11:43:44 -04001298 mutex_lock(&uuid_mutex);
1299
1300 all_avail = root->fs_info->avail_data_alloc_bits |
1301 root->fs_info->avail_system_alloc_bits |
1302 root->fs_info->avail_metadata_alloc_bits;
1303
1304 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001305 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001306 printk(KERN_ERR "btrfs: unable to go below four devices "
1307 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001308 ret = -EINVAL;
1309 goto out;
1310 }
1311
1312 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001313 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001314 printk(KERN_ERR "btrfs: unable to go below two "
1315 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001316 ret = -EINVAL;
1317 goto out;
1318 }
1319
Chris Masondfe25022008-05-13 13:46:40 -04001320 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001321 struct list_head *devices;
1322 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001323
Chris Masondfe25022008-05-13 13:46:40 -04001324 device = NULL;
1325 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001326 /*
1327 * It is safe to read the devices since the volume_mutex
1328 * is held.
1329 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001330 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001331 if (tmp->in_fs_metadata && !tmp->bdev) {
1332 device = tmp;
1333 break;
1334 }
1335 }
1336 bdev = NULL;
1337 bh = NULL;
1338 disk_super = NULL;
1339 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001340 printk(KERN_ERR "btrfs: no missing devices found to "
1341 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001342 goto out;
1343 }
Chris Masondfe25022008-05-13 13:46:40 -04001344 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001345 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1346 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001347 if (IS_ERR(bdev)) {
1348 ret = PTR_ERR(bdev);
1349 goto out;
1350 }
1351
Yan Zheng2b820322008-11-17 21:11:30 -05001352 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001353 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001354 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001355 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001356 goto error_close;
1357 }
1358 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001359 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001360 dev_uuid = disk_super->dev_item.uuid;
1361 device = btrfs_find_device(root, devid, dev_uuid,
1362 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001363 if (!device) {
1364 ret = -ENOENT;
1365 goto error_brelse;
1366 }
Chris Masondfe25022008-05-13 13:46:40 -04001367 }
Yan Zheng2b820322008-11-17 21:11:30 -05001368
1369 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001370 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1371 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001372 ret = -EINVAL;
1373 goto error_brelse;
1374 }
1375
1376 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001377 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001378 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001379 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001380 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001381 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001382 }
Chris Masona061fc82008-05-07 11:43:44 -04001383
1384 ret = btrfs_shrink_device(device, 0);
1385 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001386 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001387
Chris Masona061fc82008-05-07 11:43:44 -04001388 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1389 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001390 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001391
Josef Bacik2bf64752011-09-26 17:12:22 -04001392 spin_lock(&root->fs_info->free_chunk_lock);
1393 root->fs_info->free_chunk_space = device->total_bytes -
1394 device->bytes_used;
1395 spin_unlock(&root->fs_info->free_chunk_lock);
1396
Yan Zheng2b820322008-11-17 21:11:30 -05001397 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001398 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001399
1400 /*
1401 * the device list mutex makes sure that we don't change
1402 * the device list while someone else is writing out all
1403 * the device supers.
1404 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001405
1406 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001407 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001408 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001409
Yan Zhenge4404d62008-12-12 10:03:26 -05001410 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001411
Chris Masoncd02dca2010-12-13 14:56:23 -05001412 if (device->missing)
1413 root->fs_info->fs_devices->missing_devices--;
1414
Yan Zheng2b820322008-11-17 21:11:30 -05001415 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1416 struct btrfs_device, dev_list);
1417 if (device->bdev == root->fs_info->sb->s_bdev)
1418 root->fs_info->sb->s_bdev = next_device->bdev;
1419 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1420 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1421
Xiao Guangrong1f781602011-04-20 10:09:16 +00001422 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001423 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001424
1425 call_rcu(&device->rcu, free_device);
1426 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001427
David Sterba6c417612011-04-13 15:41:04 +02001428 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1429 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001430
Xiao Guangrong1f781602011-04-20 10:09:16 +00001431 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001432 struct btrfs_fs_devices *fs_devices;
1433 fs_devices = root->fs_info->fs_devices;
1434 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001435 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001436 break;
1437 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001438 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001439 fs_devices->seed = cur_devices->seed;
1440 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001441 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001442 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001443 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001444 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001445 }
1446
1447 /*
1448 * at this point, the device is zero sized. We want to
1449 * remove it from the devices list and zero out the old super
1450 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001451 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001452 /* make sure this device isn't detected as part of
1453 * the FS anymore
1454 */
1455 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1456 set_buffer_dirty(bh);
1457 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001458 }
Chris Masona061fc82008-05-07 11:43:44 -04001459
Chris Masona061fc82008-05-07 11:43:44 -04001460 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001461
1462error_brelse:
1463 brelse(bh);
1464error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001465 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001466 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001467out:
1468 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001469 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001470error_undo:
1471 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001472 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001473 list_add(&device->dev_alloc_list,
1474 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001475 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001476 root->fs_info->fs_devices->rw_devices++;
1477 }
1478 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001479}
1480
Yan Zheng2b820322008-11-17 21:11:30 -05001481/*
1482 * does all the dirty work required for changing file system's UUID.
1483 */
Li Zefan125ccb02011-12-08 15:07:24 +08001484static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001485{
1486 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1487 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001488 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001489 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001490 struct btrfs_device *device;
1491 u64 super_flags;
1492
1493 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001494 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001495 return -EINVAL;
1496
Yan Zhenge4404d62008-12-12 10:03:26 -05001497 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1498 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001499 return -ENOMEM;
1500
Yan Zhenge4404d62008-12-12 10:03:26 -05001501 old_devices = clone_fs_devices(fs_devices);
1502 if (IS_ERR(old_devices)) {
1503 kfree(seed_devices);
1504 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001505 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001506
Yan Zheng2b820322008-11-17 21:11:30 -05001507 list_add(&old_devices->list, &fs_uuids);
1508
Yan Zhenge4404d62008-12-12 10:03:26 -05001509 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1510 seed_devices->opened = 1;
1511 INIT_LIST_HEAD(&seed_devices->devices);
1512 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001513 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001514
1515 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001516 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1517 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001518 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1519
Yan Zhenge4404d62008-12-12 10:03:26 -05001520 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1521 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1522 device->fs_devices = seed_devices;
1523 }
1524
Yan Zheng2b820322008-11-17 21:11:30 -05001525 fs_devices->seeding = 0;
1526 fs_devices->num_devices = 0;
1527 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001528 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001529
1530 generate_random_uuid(fs_devices->fsid);
1531 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1532 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1533 super_flags = btrfs_super_flags(disk_super) &
1534 ~BTRFS_SUPER_FLAG_SEEDING;
1535 btrfs_set_super_flags(disk_super, super_flags);
1536
1537 return 0;
1538}
1539
1540/*
1541 * strore the expected generation for seed devices in device items.
1542 */
1543static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1544 struct btrfs_root *root)
1545{
1546 struct btrfs_path *path;
1547 struct extent_buffer *leaf;
1548 struct btrfs_dev_item *dev_item;
1549 struct btrfs_device *device;
1550 struct btrfs_key key;
1551 u8 fs_uuid[BTRFS_UUID_SIZE];
1552 u8 dev_uuid[BTRFS_UUID_SIZE];
1553 u64 devid;
1554 int ret;
1555
1556 path = btrfs_alloc_path();
1557 if (!path)
1558 return -ENOMEM;
1559
1560 root = root->fs_info->chunk_root;
1561 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1562 key.offset = 0;
1563 key.type = BTRFS_DEV_ITEM_KEY;
1564
1565 while (1) {
1566 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1567 if (ret < 0)
1568 goto error;
1569
1570 leaf = path->nodes[0];
1571next_slot:
1572 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1573 ret = btrfs_next_leaf(root, path);
1574 if (ret > 0)
1575 break;
1576 if (ret < 0)
1577 goto error;
1578 leaf = path->nodes[0];
1579 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001580 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001581 continue;
1582 }
1583
1584 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1585 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1586 key.type != BTRFS_DEV_ITEM_KEY)
1587 break;
1588
1589 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1590 struct btrfs_dev_item);
1591 devid = btrfs_device_id(leaf, dev_item);
1592 read_extent_buffer(leaf, dev_uuid,
1593 (unsigned long)btrfs_device_uuid(dev_item),
1594 BTRFS_UUID_SIZE);
1595 read_extent_buffer(leaf, fs_uuid,
1596 (unsigned long)btrfs_device_fsid(dev_item),
1597 BTRFS_UUID_SIZE);
1598 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1599 BUG_ON(!device);
1600
1601 if (device->fs_devices->seeding) {
1602 btrfs_set_device_generation(leaf, dev_item,
1603 device->generation);
1604 btrfs_mark_buffer_dirty(leaf);
1605 }
1606
1607 path->slots[0]++;
1608 goto next_slot;
1609 }
1610 ret = 0;
1611error:
1612 btrfs_free_path(path);
1613 return ret;
1614}
1615
Chris Mason788f20e2008-04-28 15:29:42 -04001616int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1617{
Josef Bacikd5e20032011-08-04 14:52:27 +00001618 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001619 struct btrfs_trans_handle *trans;
1620 struct btrfs_device *device;
1621 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001622 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001623 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001624 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001625 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001626 int ret = 0;
1627
Yan Zheng2b820322008-11-17 21:11:30 -05001628 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1629 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001630
Li Zefana5d16332011-12-07 20:08:40 -05001631 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001632 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001633 if (IS_ERR(bdev))
1634 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001635
Yan Zheng2b820322008-11-17 21:11:30 -05001636 if (root->fs_info->fs_devices->seeding) {
1637 seeding_dev = 1;
1638 down_write(&sb->s_umount);
1639 mutex_lock(&uuid_mutex);
1640 }
1641
Chris Mason8c8bee12008-09-29 11:19:10 -04001642 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001643
Chris Mason788f20e2008-04-28 15:29:42 -04001644 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001645 /*
1646 * we have the volume lock, so we don't need the extra
1647 * device list mutex while reading the list here.
1648 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001649 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001650 if (device->bdev == bdev) {
1651 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001652 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001653 }
1654 }
1655
1656 device = kzalloc(sizeof(*device), GFP_NOFS);
1657 if (!device) {
1658 /* we can safely leave the fs_devices entry around */
1659 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001660 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001661 }
1662
Chris Mason788f20e2008-04-28 15:29:42 -04001663 device->name = kstrdup(device_path, GFP_NOFS);
1664 if (!device->name) {
1665 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001666 ret = -ENOMEM;
1667 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001668 }
Yan Zheng2b820322008-11-17 21:11:30 -05001669
1670 ret = find_next_devid(root, &device->devid);
1671 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001672 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001673 kfree(device);
1674 goto error;
1675 }
1676
Yan, Zhenga22285a2010-05-16 10:48:46 -04001677 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001678 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001679 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001680 kfree(device);
1681 ret = PTR_ERR(trans);
1682 goto error;
1683 }
1684
Yan Zheng2b820322008-11-17 21:11:30 -05001685 lock_chunks(root);
1686
Josef Bacikd5e20032011-08-04 14:52:27 +00001687 q = bdev_get_queue(bdev);
1688 if (blk_queue_discard(q))
1689 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001690 device->writeable = 1;
1691 device->work.func = pending_bios_fn;
1692 generate_random_uuid(device->uuid);
1693 spin_lock_init(&device->io_lock);
1694 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001695 device->io_width = root->sectorsize;
1696 device->io_align = root->sectorsize;
1697 device->sector_size = root->sectorsize;
1698 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001699 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001700 device->dev_root = root->fs_info->dev_root;
1701 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001702 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001703 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001704 set_blocksize(device->bdev, 4096);
1705
Yan Zheng2b820322008-11-17 21:11:30 -05001706 if (seeding_dev) {
1707 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001708 ret = btrfs_prepare_sprout(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001709 BUG_ON(ret);
1710 }
1711
1712 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001713
1714 /*
1715 * we don't want write_supers to jump in here with our device
1716 * half setup
1717 */
1718 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001719 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001720 list_add(&device->dev_alloc_list,
1721 &root->fs_info->fs_devices->alloc_list);
1722 root->fs_info->fs_devices->num_devices++;
1723 root->fs_info->fs_devices->open_devices++;
1724 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001725 if (device->can_discard)
1726 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001727 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1728
Josef Bacik2bf64752011-09-26 17:12:22 -04001729 spin_lock(&root->fs_info->free_chunk_lock);
1730 root->fs_info->free_chunk_space += device->total_bytes;
1731 spin_unlock(&root->fs_info->free_chunk_lock);
1732
Chris Masonc2898112009-06-10 09:51:32 -04001733 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1734 root->fs_info->fs_devices->rotating = 1;
1735
David Sterba6c417612011-04-13 15:41:04 +02001736 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1737 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001738 total_bytes + device->total_bytes);
1739
David Sterba6c417612011-04-13 15:41:04 +02001740 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1741 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001742 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001743 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001744
Yan Zheng2b820322008-11-17 21:11:30 -05001745 if (seeding_dev) {
1746 ret = init_first_rw_device(trans, root, device);
1747 BUG_ON(ret);
1748 ret = btrfs_finish_sprout(trans, root);
1749 BUG_ON(ret);
1750 } else {
1751 ret = btrfs_add_device(trans, root, device);
1752 }
1753
Chris Mason913d9522009-03-10 13:17:18 -04001754 /*
1755 * we've got more storage, clear any full flags on the space
1756 * infos
1757 */
1758 btrfs_clear_space_info_full(root->fs_info);
1759
Chris Mason7d9eb122008-07-08 14:19:17 -04001760 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001761 btrfs_commit_transaction(trans, root);
1762
1763 if (seeding_dev) {
1764 mutex_unlock(&uuid_mutex);
1765 up_write(&sb->s_umount);
1766
1767 ret = btrfs_relocate_sys_chunks(root);
1768 BUG_ON(ret);
1769 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001770
Chris Mason788f20e2008-04-28 15:29:42 -04001771 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001772error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001773 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001774 if (seeding_dev) {
1775 mutex_unlock(&uuid_mutex);
1776 up_write(&sb->s_umount);
1777 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001778 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001779}
1780
Chris Masond3977122009-01-05 21:25:51 -05001781static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1782 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001783{
1784 int ret;
1785 struct btrfs_path *path;
1786 struct btrfs_root *root;
1787 struct btrfs_dev_item *dev_item;
1788 struct extent_buffer *leaf;
1789 struct btrfs_key key;
1790
1791 root = device->dev_root->fs_info->chunk_root;
1792
1793 path = btrfs_alloc_path();
1794 if (!path)
1795 return -ENOMEM;
1796
1797 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1798 key.type = BTRFS_DEV_ITEM_KEY;
1799 key.offset = device->devid;
1800
1801 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1802 if (ret < 0)
1803 goto out;
1804
1805 if (ret > 0) {
1806 ret = -ENOENT;
1807 goto out;
1808 }
1809
1810 leaf = path->nodes[0];
1811 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1812
1813 btrfs_set_device_id(leaf, dev_item, device->devid);
1814 btrfs_set_device_type(leaf, dev_item, device->type);
1815 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1816 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1817 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001818 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001819 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1820 btrfs_mark_buffer_dirty(leaf);
1821
1822out:
1823 btrfs_free_path(path);
1824 return ret;
1825}
1826
Chris Mason7d9eb122008-07-08 14:19:17 -04001827static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001828 struct btrfs_device *device, u64 new_size)
1829{
1830 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001831 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001832 u64 old_total = btrfs_super_total_bytes(super_copy);
1833 u64 diff = new_size - device->total_bytes;
1834
Yan Zheng2b820322008-11-17 21:11:30 -05001835 if (!device->writeable)
1836 return -EACCES;
1837 if (new_size <= device->total_bytes)
1838 return -EINVAL;
1839
Chris Mason8f18cf12008-04-25 16:53:30 -04001840 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001841 device->fs_devices->total_rw_bytes += diff;
1842
1843 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001844 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001845 btrfs_clear_space_info_full(device->dev_root->fs_info);
1846
Chris Mason8f18cf12008-04-25 16:53:30 -04001847 return btrfs_update_device(trans, device);
1848}
1849
Chris Mason7d9eb122008-07-08 14:19:17 -04001850int btrfs_grow_device(struct btrfs_trans_handle *trans,
1851 struct btrfs_device *device, u64 new_size)
1852{
1853 int ret;
1854 lock_chunks(device->dev_root);
1855 ret = __btrfs_grow_device(trans, device, new_size);
1856 unlock_chunks(device->dev_root);
1857 return ret;
1858}
1859
Chris Mason8f18cf12008-04-25 16:53:30 -04001860static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1861 struct btrfs_root *root,
1862 u64 chunk_tree, u64 chunk_objectid,
1863 u64 chunk_offset)
1864{
1865 int ret;
1866 struct btrfs_path *path;
1867 struct btrfs_key key;
1868
1869 root = root->fs_info->chunk_root;
1870 path = btrfs_alloc_path();
1871 if (!path)
1872 return -ENOMEM;
1873
1874 key.objectid = chunk_objectid;
1875 key.offset = chunk_offset;
1876 key.type = BTRFS_CHUNK_ITEM_KEY;
1877
1878 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1879 BUG_ON(ret);
1880
1881 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001882
1883 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001884 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001885}
1886
Christoph Hellwigb2950862008-12-02 09:54:17 -05001887static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001888 chunk_offset)
1889{
David Sterba6c417612011-04-13 15:41:04 +02001890 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001891 struct btrfs_disk_key *disk_key;
1892 struct btrfs_chunk *chunk;
1893 u8 *ptr;
1894 int ret = 0;
1895 u32 num_stripes;
1896 u32 array_size;
1897 u32 len = 0;
1898 u32 cur;
1899 struct btrfs_key key;
1900
1901 array_size = btrfs_super_sys_array_size(super_copy);
1902
1903 ptr = super_copy->sys_chunk_array;
1904 cur = 0;
1905
1906 while (cur < array_size) {
1907 disk_key = (struct btrfs_disk_key *)ptr;
1908 btrfs_disk_key_to_cpu(&key, disk_key);
1909
1910 len = sizeof(*disk_key);
1911
1912 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1913 chunk = (struct btrfs_chunk *)(ptr + len);
1914 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1915 len += btrfs_chunk_item_size(num_stripes);
1916 } else {
1917 ret = -EIO;
1918 break;
1919 }
1920 if (key.objectid == chunk_objectid &&
1921 key.offset == chunk_offset) {
1922 memmove(ptr, ptr + len, array_size - (cur + len));
1923 array_size -= len;
1924 btrfs_set_super_sys_array_size(super_copy, array_size);
1925 } else {
1926 ptr += len;
1927 cur += len;
1928 }
1929 }
1930 return ret;
1931}
1932
Christoph Hellwigb2950862008-12-02 09:54:17 -05001933static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001934 u64 chunk_tree, u64 chunk_objectid,
1935 u64 chunk_offset)
1936{
1937 struct extent_map_tree *em_tree;
1938 struct btrfs_root *extent_root;
1939 struct btrfs_trans_handle *trans;
1940 struct extent_map *em;
1941 struct map_lookup *map;
1942 int ret;
1943 int i;
1944
1945 root = root->fs_info->chunk_root;
1946 extent_root = root->fs_info->extent_root;
1947 em_tree = &root->fs_info->mapping_tree.map_tree;
1948
Josef Bacikba1bf482009-09-11 16:11:19 -04001949 ret = btrfs_can_relocate(extent_root, chunk_offset);
1950 if (ret)
1951 return -ENOSPC;
1952
Chris Mason8f18cf12008-04-25 16:53:30 -04001953 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001954 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001955 if (ret)
1956 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001957
Yan, Zhenga22285a2010-05-16 10:48:46 -04001958 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001959 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001960
Chris Mason7d9eb122008-07-08 14:19:17 -04001961 lock_chunks(root);
1962
Chris Mason8f18cf12008-04-25 16:53:30 -04001963 /*
1964 * step two, delete the device extents and the
1965 * chunk tree entries
1966 */
Chris Mason890871b2009-09-02 16:24:52 -04001967 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001968 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001969 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001970
Tsutomu Itoh285190d2012-02-16 16:23:58 +09001971 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04001972 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001973 map = (struct map_lookup *)em->bdev;
1974
1975 for (i = 0; i < map->num_stripes; i++) {
1976 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1977 map->stripes[i].physical);
1978 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001979
Chris Masondfe25022008-05-13 13:46:40 -04001980 if (map->stripes[i].dev) {
1981 ret = btrfs_update_device(trans, map->stripes[i].dev);
1982 BUG_ON(ret);
1983 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001984 }
1985 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1986 chunk_offset);
1987
1988 BUG_ON(ret);
1989
liubo1abe9b82011-03-24 11:18:59 +00001990 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1991
Chris Mason8f18cf12008-04-25 16:53:30 -04001992 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1993 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1994 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001995 }
1996
Zheng Yan1a40e232008-09-26 10:09:34 -04001997 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1998 BUG_ON(ret);
1999
Chris Mason890871b2009-09-02 16:24:52 -04002000 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002001 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002002 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002003
Chris Mason8f18cf12008-04-25 16:53:30 -04002004 kfree(map);
2005 em->bdev = NULL;
2006
2007 /* once for the tree */
2008 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002009 /* once for us */
2010 free_extent_map(em);
2011
Chris Mason7d9eb122008-07-08 14:19:17 -04002012 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002013 btrfs_end_transaction(trans, root);
2014 return 0;
2015}
2016
Yan Zheng2b820322008-11-17 21:11:30 -05002017static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2018{
2019 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2020 struct btrfs_path *path;
2021 struct extent_buffer *leaf;
2022 struct btrfs_chunk *chunk;
2023 struct btrfs_key key;
2024 struct btrfs_key found_key;
2025 u64 chunk_tree = chunk_root->root_key.objectid;
2026 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002027 bool retried = false;
2028 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002029 int ret;
2030
2031 path = btrfs_alloc_path();
2032 if (!path)
2033 return -ENOMEM;
2034
Josef Bacikba1bf482009-09-11 16:11:19 -04002035again:
Yan Zheng2b820322008-11-17 21:11:30 -05002036 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2037 key.offset = (u64)-1;
2038 key.type = BTRFS_CHUNK_ITEM_KEY;
2039
2040 while (1) {
2041 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2042 if (ret < 0)
2043 goto error;
2044 BUG_ON(ret == 0);
2045
2046 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2047 key.type);
2048 if (ret < 0)
2049 goto error;
2050 if (ret > 0)
2051 break;
2052
2053 leaf = path->nodes[0];
2054 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2055
2056 chunk = btrfs_item_ptr(leaf, path->slots[0],
2057 struct btrfs_chunk);
2058 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002059 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002060
2061 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2062 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2063 found_key.objectid,
2064 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002065 if (ret == -ENOSPC)
2066 failed++;
2067 else if (ret)
2068 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002069 }
2070
2071 if (found_key.offset == 0)
2072 break;
2073 key.offset = found_key.offset - 1;
2074 }
2075 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002076 if (failed && !retried) {
2077 failed = 0;
2078 retried = true;
2079 goto again;
2080 } else if (failed && retried) {
2081 WARN_ON(1);
2082 ret = -ENOSPC;
2083 }
Yan Zheng2b820322008-11-17 21:11:30 -05002084error:
2085 btrfs_free_path(path);
2086 return ret;
2087}
2088
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002089static int insert_balance_item(struct btrfs_root *root,
2090 struct btrfs_balance_control *bctl)
2091{
2092 struct btrfs_trans_handle *trans;
2093 struct btrfs_balance_item *item;
2094 struct btrfs_disk_balance_args disk_bargs;
2095 struct btrfs_path *path;
2096 struct extent_buffer *leaf;
2097 struct btrfs_key key;
2098 int ret, err;
2099
2100 path = btrfs_alloc_path();
2101 if (!path)
2102 return -ENOMEM;
2103
2104 trans = btrfs_start_transaction(root, 0);
2105 if (IS_ERR(trans)) {
2106 btrfs_free_path(path);
2107 return PTR_ERR(trans);
2108 }
2109
2110 key.objectid = BTRFS_BALANCE_OBJECTID;
2111 key.type = BTRFS_BALANCE_ITEM_KEY;
2112 key.offset = 0;
2113
2114 ret = btrfs_insert_empty_item(trans, root, path, &key,
2115 sizeof(*item));
2116 if (ret)
2117 goto out;
2118
2119 leaf = path->nodes[0];
2120 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2121
2122 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2123
2124 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2125 btrfs_set_balance_data(leaf, item, &disk_bargs);
2126 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2127 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2128 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2129 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2130
2131 btrfs_set_balance_flags(leaf, item, bctl->flags);
2132
2133 btrfs_mark_buffer_dirty(leaf);
2134out:
2135 btrfs_free_path(path);
2136 err = btrfs_commit_transaction(trans, root);
2137 if (err && !ret)
2138 ret = err;
2139 return ret;
2140}
2141
2142static int del_balance_item(struct btrfs_root *root)
2143{
2144 struct btrfs_trans_handle *trans;
2145 struct btrfs_path *path;
2146 struct btrfs_key key;
2147 int ret, err;
2148
2149 path = btrfs_alloc_path();
2150 if (!path)
2151 return -ENOMEM;
2152
2153 trans = btrfs_start_transaction(root, 0);
2154 if (IS_ERR(trans)) {
2155 btrfs_free_path(path);
2156 return PTR_ERR(trans);
2157 }
2158
2159 key.objectid = BTRFS_BALANCE_OBJECTID;
2160 key.type = BTRFS_BALANCE_ITEM_KEY;
2161 key.offset = 0;
2162
2163 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2164 if (ret < 0)
2165 goto out;
2166 if (ret > 0) {
2167 ret = -ENOENT;
2168 goto out;
2169 }
2170
2171 ret = btrfs_del_item(trans, root, path);
2172out:
2173 btrfs_free_path(path);
2174 err = btrfs_commit_transaction(trans, root);
2175 if (err && !ret)
2176 ret = err;
2177 return ret;
2178}
2179
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002180/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002181 * This is a heuristic used to reduce the number of chunks balanced on
2182 * resume after balance was interrupted.
2183 */
2184static void update_balance_args(struct btrfs_balance_control *bctl)
2185{
2186 /*
2187 * Turn on soft mode for chunk types that were being converted.
2188 */
2189 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2190 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2191 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2192 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2193 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2194 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2195
2196 /*
2197 * Turn on usage filter if is not already used. The idea is
2198 * that chunks that we have already balanced should be
2199 * reasonably full. Don't do it for chunks that are being
2200 * converted - that will keep us from relocating unconverted
2201 * (albeit full) chunks.
2202 */
2203 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2204 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2205 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2206 bctl->data.usage = 90;
2207 }
2208 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2209 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2210 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2211 bctl->sys.usage = 90;
2212 }
2213 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2214 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2215 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2216 bctl->meta.usage = 90;
2217 }
2218}
2219
2220/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002221 * Should be called with both balance and volume mutexes held to
2222 * serialize other volume operations (add_dev/rm_dev/resize) with
2223 * restriper. Same goes for unset_balance_control.
2224 */
2225static void set_balance_control(struct btrfs_balance_control *bctl)
2226{
2227 struct btrfs_fs_info *fs_info = bctl->fs_info;
2228
2229 BUG_ON(fs_info->balance_ctl);
2230
2231 spin_lock(&fs_info->balance_lock);
2232 fs_info->balance_ctl = bctl;
2233 spin_unlock(&fs_info->balance_lock);
2234}
2235
2236static void unset_balance_control(struct btrfs_fs_info *fs_info)
2237{
2238 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2239
2240 BUG_ON(!fs_info->balance_ctl);
2241
2242 spin_lock(&fs_info->balance_lock);
2243 fs_info->balance_ctl = NULL;
2244 spin_unlock(&fs_info->balance_lock);
2245
2246 kfree(bctl);
2247}
2248
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002249/*
2250 * Balance filters. Return 1 if chunk should be filtered out
2251 * (should not be balanced).
2252 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002253static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002254 struct btrfs_balance_args *bargs)
2255{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002256 chunk_type = chunk_to_extended(chunk_type) &
2257 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002258
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002259 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002260 return 0;
2261
2262 return 1;
2263}
2264
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002265static u64 div_factor_fine(u64 num, int factor)
2266{
2267 if (factor <= 0)
2268 return 0;
2269 if (factor >= 100)
2270 return num;
2271
2272 num *= factor;
2273 do_div(num, 100);
2274 return num;
2275}
2276
2277static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2278 struct btrfs_balance_args *bargs)
2279{
2280 struct btrfs_block_group_cache *cache;
2281 u64 chunk_used, user_thresh;
2282 int ret = 1;
2283
2284 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2285 chunk_used = btrfs_block_group_used(&cache->item);
2286
2287 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2288 if (chunk_used < user_thresh)
2289 ret = 0;
2290
2291 btrfs_put_block_group(cache);
2292 return ret;
2293}
2294
Ilya Dryomov409d4042012-01-16 22:04:47 +02002295static int chunk_devid_filter(struct extent_buffer *leaf,
2296 struct btrfs_chunk *chunk,
2297 struct btrfs_balance_args *bargs)
2298{
2299 struct btrfs_stripe *stripe;
2300 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2301 int i;
2302
2303 for (i = 0; i < num_stripes; i++) {
2304 stripe = btrfs_stripe_nr(chunk, i);
2305 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2306 return 0;
2307 }
2308
2309 return 1;
2310}
2311
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002312/* [pstart, pend) */
2313static int chunk_drange_filter(struct extent_buffer *leaf,
2314 struct btrfs_chunk *chunk,
2315 u64 chunk_offset,
2316 struct btrfs_balance_args *bargs)
2317{
2318 struct btrfs_stripe *stripe;
2319 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2320 u64 stripe_offset;
2321 u64 stripe_length;
2322 int factor;
2323 int i;
2324
2325 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2326 return 0;
2327
2328 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2329 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2330 factor = 2;
2331 else
2332 factor = 1;
2333 factor = num_stripes / factor;
2334
2335 for (i = 0; i < num_stripes; i++) {
2336 stripe = btrfs_stripe_nr(chunk, i);
2337 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2338 continue;
2339
2340 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2341 stripe_length = btrfs_chunk_length(leaf, chunk);
2342 do_div(stripe_length, factor);
2343
2344 if (stripe_offset < bargs->pend &&
2345 stripe_offset + stripe_length > bargs->pstart)
2346 return 0;
2347 }
2348
2349 return 1;
2350}
2351
Ilya Dryomovea671762012-01-16 22:04:48 +02002352/* [vstart, vend) */
2353static int chunk_vrange_filter(struct extent_buffer *leaf,
2354 struct btrfs_chunk *chunk,
2355 u64 chunk_offset,
2356 struct btrfs_balance_args *bargs)
2357{
2358 if (chunk_offset < bargs->vend &&
2359 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2360 /* at least part of the chunk is inside this vrange */
2361 return 0;
2362
2363 return 1;
2364}
2365
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002366static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002367 struct btrfs_balance_args *bargs)
2368{
2369 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2370 return 0;
2371
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002372 chunk_type = chunk_to_extended(chunk_type) &
2373 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002374
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002375 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002376 return 1;
2377
2378 return 0;
2379}
2380
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002381static int should_balance_chunk(struct btrfs_root *root,
2382 struct extent_buffer *leaf,
2383 struct btrfs_chunk *chunk, u64 chunk_offset)
2384{
2385 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2386 struct btrfs_balance_args *bargs = NULL;
2387 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2388
2389 /* type filter */
2390 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2391 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2392 return 0;
2393 }
2394
2395 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2396 bargs = &bctl->data;
2397 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2398 bargs = &bctl->sys;
2399 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2400 bargs = &bctl->meta;
2401
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002402 /* profiles filter */
2403 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2404 chunk_profiles_filter(chunk_type, bargs)) {
2405 return 0;
2406 }
2407
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002408 /* usage filter */
2409 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2410 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2411 return 0;
2412 }
2413
Ilya Dryomov409d4042012-01-16 22:04:47 +02002414 /* devid filter */
2415 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2416 chunk_devid_filter(leaf, chunk, bargs)) {
2417 return 0;
2418 }
2419
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002420 /* drange filter, makes sense only with devid filter */
2421 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2422 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2423 return 0;
2424 }
2425
Ilya Dryomovea671762012-01-16 22:04:48 +02002426 /* vrange filter */
2427 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2428 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2429 return 0;
2430 }
2431
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002432 /* soft profile changing mode */
2433 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2434 chunk_soft_convert_filter(chunk_type, bargs)) {
2435 return 0;
2436 }
2437
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002438 return 1;
2439}
2440
Chris Masonec44a352008-04-28 15:29:52 -04002441static u64 div_factor(u64 num, int factor)
2442{
2443 if (factor == 10)
2444 return num;
2445 num *= factor;
2446 do_div(num, 10);
2447 return num;
2448}
2449
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002450static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002451{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002452 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002453 struct btrfs_root *chunk_root = fs_info->chunk_root;
2454 struct btrfs_root *dev_root = fs_info->dev_root;
2455 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002456 struct btrfs_device *device;
2457 u64 old_size;
2458 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002459 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002460 struct btrfs_path *path;
2461 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002462 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002463 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002464 struct extent_buffer *leaf;
2465 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002466 int ret;
2467 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002468 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002469
Chris Masonec44a352008-04-28 15:29:52 -04002470 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002471 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002472 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002473 old_size = device->total_bytes;
2474 size_to_free = div_factor(old_size, 1);
2475 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002476 if (!device->writeable ||
2477 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002478 continue;
2479
2480 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002481 if (ret == -ENOSPC)
2482 break;
Chris Masonec44a352008-04-28 15:29:52 -04002483 BUG_ON(ret);
2484
Yan, Zhenga22285a2010-05-16 10:48:46 -04002485 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002486 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002487
2488 ret = btrfs_grow_device(trans, device, old_size);
2489 BUG_ON(ret);
2490
2491 btrfs_end_transaction(trans, dev_root);
2492 }
2493
2494 /* step two, relocate all the chunks */
2495 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002496 if (!path) {
2497 ret = -ENOMEM;
2498 goto error;
2499 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002500
2501 /* zero out stat counters */
2502 spin_lock(&fs_info->balance_lock);
2503 memset(&bctl->stat, 0, sizeof(bctl->stat));
2504 spin_unlock(&fs_info->balance_lock);
2505again:
Chris Masonec44a352008-04-28 15:29:52 -04002506 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2507 key.offset = (u64)-1;
2508 key.type = BTRFS_CHUNK_ITEM_KEY;
2509
Chris Masond3977122009-01-05 21:25:51 -05002510 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002511 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002512 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002513 ret = -ECANCELED;
2514 goto error;
2515 }
2516
Chris Masonec44a352008-04-28 15:29:52 -04002517 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2518 if (ret < 0)
2519 goto error;
2520
2521 /*
2522 * this shouldn't happen, it means the last relocate
2523 * failed
2524 */
2525 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002526 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002527
2528 ret = btrfs_previous_item(chunk_root, path, 0,
2529 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002530 if (ret) {
2531 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002532 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002533 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002534
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002535 leaf = path->nodes[0];
2536 slot = path->slots[0];
2537 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2538
Chris Masonec44a352008-04-28 15:29:52 -04002539 if (found_key.objectid != key.objectid)
2540 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002541
Chris Masonec44a352008-04-28 15:29:52 -04002542 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002543 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002544 break;
2545
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002546 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2547
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002548 if (!counting) {
2549 spin_lock(&fs_info->balance_lock);
2550 bctl->stat.considered++;
2551 spin_unlock(&fs_info->balance_lock);
2552 }
2553
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002554 ret = should_balance_chunk(chunk_root, leaf, chunk,
2555 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002556 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002557 if (!ret)
2558 goto loop;
2559
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002560 if (counting) {
2561 spin_lock(&fs_info->balance_lock);
2562 bctl->stat.expected++;
2563 spin_unlock(&fs_info->balance_lock);
2564 goto loop;
2565 }
2566
Chris Masonec44a352008-04-28 15:29:52 -04002567 ret = btrfs_relocate_chunk(chunk_root,
2568 chunk_root->root_key.objectid,
2569 found_key.objectid,
2570 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002571 if (ret && ret != -ENOSPC)
2572 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002573 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002574 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002575 } else {
2576 spin_lock(&fs_info->balance_lock);
2577 bctl->stat.completed++;
2578 spin_unlock(&fs_info->balance_lock);
2579 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002580loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002581 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002582 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002583
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002584 if (counting) {
2585 btrfs_release_path(path);
2586 counting = false;
2587 goto again;
2588 }
Chris Masonec44a352008-04-28 15:29:52 -04002589error:
2590 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002591 if (enospc_errors) {
2592 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2593 enospc_errors);
2594 if (!ret)
2595 ret = -ENOSPC;
2596 }
2597
Chris Masonec44a352008-04-28 15:29:52 -04002598 return ret;
2599}
2600
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002601/**
2602 * alloc_profile_is_valid - see if a given profile is valid and reduced
2603 * @flags: profile to validate
2604 * @extended: if true @flags is treated as an extended profile
2605 */
2606static int alloc_profile_is_valid(u64 flags, int extended)
2607{
2608 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2609 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2610
2611 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2612
2613 /* 1) check that all other bits are zeroed */
2614 if (flags & ~mask)
2615 return 0;
2616
2617 /* 2) see if profile is reduced */
2618 if (flags == 0)
2619 return !extended; /* "0" is valid for usual profiles */
2620
2621 /* true if exactly one bit set */
2622 return (flags & (flags - 1)) == 0;
2623}
2624
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002625static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2626{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002627 /* cancel requested || normal exit path */
2628 return atomic_read(&fs_info->balance_cancel_req) ||
2629 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2630 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002631}
2632
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002633static void __cancel_balance(struct btrfs_fs_info *fs_info)
2634{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002635 int ret;
2636
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002637 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002638 ret = del_balance_item(fs_info->tree_root);
2639 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002640}
2641
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002642void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002643 struct btrfs_ioctl_balance_args *bargs);
2644
2645/*
2646 * Should be called with both balance and volume mutexes held
2647 */
2648int btrfs_balance(struct btrfs_balance_control *bctl,
2649 struct btrfs_ioctl_balance_args *bargs)
2650{
2651 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002652 u64 allowed;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002653 int ret;
2654
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002655 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002656 atomic_read(&fs_info->balance_pause_req) ||
2657 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002658 ret = -EINVAL;
2659 goto out;
2660 }
2661
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002662 /*
2663 * In case of mixed groups both data and meta should be picked,
2664 * and identical options should be given for both of them.
2665 */
2666 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2667 if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
2668 (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
2669 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2670 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2671 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2672 printk(KERN_ERR "btrfs: with mixed groups data and "
2673 "metadata balance options must be the same\n");
2674 ret = -EINVAL;
2675 goto out;
2676 }
2677 }
2678
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002679 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2680 if (fs_info->fs_devices->num_devices == 1)
2681 allowed |= BTRFS_BLOCK_GROUP_DUP;
2682 else if (fs_info->fs_devices->num_devices < 4)
2683 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2684 else
2685 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2686 BTRFS_BLOCK_GROUP_RAID10);
2687
Ilya Dryomov6728b192012-03-27 17:09:17 +03002688 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2689 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2690 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002691 printk(KERN_ERR "btrfs: unable to start balance with target "
2692 "data profile %llu\n",
2693 (unsigned long long)bctl->data.target);
2694 ret = -EINVAL;
2695 goto out;
2696 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002697 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2698 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2699 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002700 printk(KERN_ERR "btrfs: unable to start balance with target "
2701 "metadata profile %llu\n",
2702 (unsigned long long)bctl->meta.target);
2703 ret = -EINVAL;
2704 goto out;
2705 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002706 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2707 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2708 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002709 printk(KERN_ERR "btrfs: unable to start balance with target "
2710 "system profile %llu\n",
2711 (unsigned long long)bctl->sys.target);
2712 ret = -EINVAL;
2713 goto out;
2714 }
2715
Ilya Dryomov6728b192012-03-27 17:09:17 +03002716 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2717 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002718 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2719 ret = -EINVAL;
2720 goto out;
2721 }
2722
2723 /* allow to reduce meta or sys integrity only if force set */
2724 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2725 BTRFS_BLOCK_GROUP_RAID10;
2726 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2727 (fs_info->avail_system_alloc_bits & allowed) &&
2728 !(bctl->sys.target & allowed)) ||
2729 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2730 (fs_info->avail_metadata_alloc_bits & allowed) &&
2731 !(bctl->meta.target & allowed))) {
2732 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2733 printk(KERN_INFO "btrfs: force reducing metadata "
2734 "integrity\n");
2735 } else {
2736 printk(KERN_ERR "btrfs: balance will reduce metadata "
2737 "integrity, use force if you want this\n");
2738 ret = -EINVAL;
2739 goto out;
2740 }
2741 }
2742
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002743 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002744 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002745 goto out;
2746
Ilya Dryomov59641012012-01-16 22:04:48 +02002747 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2748 BUG_ON(ret == -EEXIST);
2749 set_balance_control(bctl);
2750 } else {
2751 BUG_ON(ret != -EEXIST);
2752 spin_lock(&fs_info->balance_lock);
2753 update_balance_args(bctl);
2754 spin_unlock(&fs_info->balance_lock);
2755 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002756
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002757 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002758 mutex_unlock(&fs_info->balance_mutex);
2759
2760 ret = __btrfs_balance(fs_info);
2761
2762 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002763 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002764
2765 if (bargs) {
2766 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002767 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002768 }
2769
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002770 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2771 balance_need_close(fs_info)) {
2772 __cancel_balance(fs_info);
2773 }
2774
2775 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002776
2777 return ret;
2778out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002779 if (bctl->flags & BTRFS_BALANCE_RESUME)
2780 __cancel_balance(fs_info);
2781 else
2782 kfree(bctl);
2783 return ret;
2784}
2785
2786static int balance_kthread(void *data)
2787{
2788 struct btrfs_balance_control *bctl =
2789 (struct btrfs_balance_control *)data;
2790 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002791 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002792
2793 mutex_lock(&fs_info->volume_mutex);
2794 mutex_lock(&fs_info->balance_mutex);
2795
2796 set_balance_control(bctl);
2797
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002798 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2799 printk(KERN_INFO "btrfs: force skipping balance\n");
2800 } else {
2801 printk(KERN_INFO "btrfs: continuing balance\n");
2802 ret = btrfs_balance(bctl, NULL);
2803 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002804
2805 mutex_unlock(&fs_info->balance_mutex);
2806 mutex_unlock(&fs_info->volume_mutex);
2807 return ret;
2808}
2809
2810int btrfs_recover_balance(struct btrfs_root *tree_root)
2811{
2812 struct task_struct *tsk;
2813 struct btrfs_balance_control *bctl;
2814 struct btrfs_balance_item *item;
2815 struct btrfs_disk_balance_args disk_bargs;
2816 struct btrfs_path *path;
2817 struct extent_buffer *leaf;
2818 struct btrfs_key key;
2819 int ret;
2820
2821 path = btrfs_alloc_path();
2822 if (!path)
2823 return -ENOMEM;
2824
2825 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2826 if (!bctl) {
2827 ret = -ENOMEM;
2828 goto out;
2829 }
2830
2831 key.objectid = BTRFS_BALANCE_OBJECTID;
2832 key.type = BTRFS_BALANCE_ITEM_KEY;
2833 key.offset = 0;
2834
2835 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2836 if (ret < 0)
2837 goto out_bctl;
2838 if (ret > 0) { /* ret = -ENOENT; */
2839 ret = 0;
2840 goto out_bctl;
2841 }
2842
2843 leaf = path->nodes[0];
2844 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2845
2846 bctl->fs_info = tree_root->fs_info;
2847 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2848
2849 btrfs_balance_data(leaf, item, &disk_bargs);
2850 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2851 btrfs_balance_meta(leaf, item, &disk_bargs);
2852 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2853 btrfs_balance_sys(leaf, item, &disk_bargs);
2854 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2855
2856 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2857 if (IS_ERR(tsk))
2858 ret = PTR_ERR(tsk);
2859 else
2860 goto out;
2861
2862out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002863 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002864out:
2865 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002866 return ret;
2867}
2868
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002869int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2870{
2871 int ret = 0;
2872
2873 mutex_lock(&fs_info->balance_mutex);
2874 if (!fs_info->balance_ctl) {
2875 mutex_unlock(&fs_info->balance_mutex);
2876 return -ENOTCONN;
2877 }
2878
2879 if (atomic_read(&fs_info->balance_running)) {
2880 atomic_inc(&fs_info->balance_pause_req);
2881 mutex_unlock(&fs_info->balance_mutex);
2882
2883 wait_event(fs_info->balance_wait_q,
2884 atomic_read(&fs_info->balance_running) == 0);
2885
2886 mutex_lock(&fs_info->balance_mutex);
2887 /* we are good with balance_ctl ripped off from under us */
2888 BUG_ON(atomic_read(&fs_info->balance_running));
2889 atomic_dec(&fs_info->balance_pause_req);
2890 } else {
2891 ret = -ENOTCONN;
2892 }
2893
2894 mutex_unlock(&fs_info->balance_mutex);
2895 return ret;
2896}
2897
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002898int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2899{
2900 mutex_lock(&fs_info->balance_mutex);
2901 if (!fs_info->balance_ctl) {
2902 mutex_unlock(&fs_info->balance_mutex);
2903 return -ENOTCONN;
2904 }
2905
2906 atomic_inc(&fs_info->balance_cancel_req);
2907 /*
2908 * if we are running just wait and return, balance item is
2909 * deleted in btrfs_balance in this case
2910 */
2911 if (atomic_read(&fs_info->balance_running)) {
2912 mutex_unlock(&fs_info->balance_mutex);
2913 wait_event(fs_info->balance_wait_q,
2914 atomic_read(&fs_info->balance_running) == 0);
2915 mutex_lock(&fs_info->balance_mutex);
2916 } else {
2917 /* __cancel_balance needs volume_mutex */
2918 mutex_unlock(&fs_info->balance_mutex);
2919 mutex_lock(&fs_info->volume_mutex);
2920 mutex_lock(&fs_info->balance_mutex);
2921
2922 if (fs_info->balance_ctl)
2923 __cancel_balance(fs_info);
2924
2925 mutex_unlock(&fs_info->volume_mutex);
2926 }
2927
2928 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2929 atomic_dec(&fs_info->balance_cancel_req);
2930 mutex_unlock(&fs_info->balance_mutex);
2931 return 0;
2932}
2933
Chris Mason8f18cf12008-04-25 16:53:30 -04002934/*
2935 * shrinking a device means finding all of the device extents past
2936 * the new size, and then following the back refs to the chunks.
2937 * The chunk relocation code actually frees the device extent
2938 */
2939int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2940{
2941 struct btrfs_trans_handle *trans;
2942 struct btrfs_root *root = device->dev_root;
2943 struct btrfs_dev_extent *dev_extent = NULL;
2944 struct btrfs_path *path;
2945 u64 length;
2946 u64 chunk_tree;
2947 u64 chunk_objectid;
2948 u64 chunk_offset;
2949 int ret;
2950 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002951 int failed = 0;
2952 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002953 struct extent_buffer *l;
2954 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002955 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002956 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002957 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002958 u64 diff = device->total_bytes - new_size;
2959
Yan Zheng2b820322008-11-17 21:11:30 -05002960 if (new_size >= device->total_bytes)
2961 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002962
2963 path = btrfs_alloc_path();
2964 if (!path)
2965 return -ENOMEM;
2966
Chris Mason8f18cf12008-04-25 16:53:30 -04002967 path->reada = 2;
2968
Chris Mason7d9eb122008-07-08 14:19:17 -04002969 lock_chunks(root);
2970
Chris Mason8f18cf12008-04-25 16:53:30 -04002971 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002972 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002973 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002974 spin_lock(&root->fs_info->free_chunk_lock);
2975 root->fs_info->free_chunk_space -= diff;
2976 spin_unlock(&root->fs_info->free_chunk_lock);
2977 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002978 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002979
Josef Bacikba1bf482009-09-11 16:11:19 -04002980again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002981 key.objectid = device->devid;
2982 key.offset = (u64)-1;
2983 key.type = BTRFS_DEV_EXTENT_KEY;
2984
2985 while (1) {
2986 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2987 if (ret < 0)
2988 goto done;
2989
2990 ret = btrfs_previous_item(root, path, 0, key.type);
2991 if (ret < 0)
2992 goto done;
2993 if (ret) {
2994 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002995 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002996 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002997 }
2998
2999 l = path->nodes[0];
3000 slot = path->slots[0];
3001 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3002
Josef Bacikba1bf482009-09-11 16:11:19 -04003003 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003004 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003005 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003006 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003007
3008 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3009 length = btrfs_dev_extent_length(l, dev_extent);
3010
Josef Bacikba1bf482009-09-11 16:11:19 -04003011 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003012 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003013 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003014 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003015
3016 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3017 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3018 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003019 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003020
3021 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3022 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003023 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003024 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003025 if (ret == -ENOSPC)
3026 failed++;
3027 key.offset -= 1;
3028 }
3029
3030 if (failed && !retried) {
3031 failed = 0;
3032 retried = true;
3033 goto again;
3034 } else if (failed && retried) {
3035 ret = -ENOSPC;
3036 lock_chunks(root);
3037
3038 device->total_bytes = old_size;
3039 if (device->writeable)
3040 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003041 spin_lock(&root->fs_info->free_chunk_lock);
3042 root->fs_info->free_chunk_space += diff;
3043 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003044 unlock_chunks(root);
3045 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003046 }
3047
Chris Balld6397ba2009-04-27 07:29:03 -04003048 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003049 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003050 if (IS_ERR(trans)) {
3051 ret = PTR_ERR(trans);
3052 goto done;
3053 }
3054
Chris Balld6397ba2009-04-27 07:29:03 -04003055 lock_chunks(root);
3056
3057 device->disk_total_bytes = new_size;
3058 /* Now btrfs_update_device() will change the on-disk size. */
3059 ret = btrfs_update_device(trans, device);
3060 if (ret) {
3061 unlock_chunks(root);
3062 btrfs_end_transaction(trans, root);
3063 goto done;
3064 }
3065 WARN_ON(diff > old_total);
3066 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3067 unlock_chunks(root);
3068 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003069done:
3070 btrfs_free_path(path);
3071 return ret;
3072}
3073
Li Zefan125ccb02011-12-08 15:07:24 +08003074static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003075 struct btrfs_key *key,
3076 struct btrfs_chunk *chunk, int item_size)
3077{
David Sterba6c417612011-04-13 15:41:04 +02003078 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003079 struct btrfs_disk_key disk_key;
3080 u32 array_size;
3081 u8 *ptr;
3082
3083 array_size = btrfs_super_sys_array_size(super_copy);
3084 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3085 return -EFBIG;
3086
3087 ptr = super_copy->sys_chunk_array + array_size;
3088 btrfs_cpu_key_to_disk(&disk_key, key);
3089 memcpy(ptr, &disk_key, sizeof(disk_key));
3090 ptr += sizeof(disk_key);
3091 memcpy(ptr, chunk, item_size);
3092 item_size += sizeof(disk_key);
3093 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3094 return 0;
3095}
3096
Miao Xieb2117a32011-01-05 10:07:28 +00003097/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003098 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003099 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003100static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003101{
Arne Jansen73c5de02011-04-12 12:07:57 +02003102 const struct btrfs_device_info *di_a = a;
3103 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003104
Arne Jansen73c5de02011-04-12 12:07:57 +02003105 if (di_a->max_avail > di_b->max_avail)
3106 return -1;
3107 if (di_a->max_avail < di_b->max_avail)
3108 return 1;
3109 if (di_a->total_avail > di_b->total_avail)
3110 return -1;
3111 if (di_a->total_avail < di_b->total_avail)
3112 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003113 return 0;
3114}
3115
3116static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3117 struct btrfs_root *extent_root,
3118 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003119 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003120 u64 start, u64 type)
3121{
3122 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003123 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3124 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003125 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003126 struct extent_map_tree *em_tree;
3127 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003128 struct btrfs_device_info *devices_info = NULL;
3129 u64 total_avail;
3130 int num_stripes; /* total number of stripes to allocate */
3131 int sub_stripes; /* sub_stripes info for map */
3132 int dev_stripes; /* stripes per dev */
3133 int devs_max; /* max devs to use */
3134 int devs_min; /* min devs needed */
3135 int devs_increment; /* ndevs has to be a multiple of this */
3136 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003137 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003138 u64 max_stripe_size;
3139 u64 max_chunk_size;
3140 u64 stripe_size;
3141 u64 num_bytes;
3142 int ndevs;
3143 int i;
3144 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003145
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003146 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003147
Miao Xieb2117a32011-01-05 10:07:28 +00003148 if (list_empty(&fs_devices->alloc_list))
3149 return -ENOSPC;
3150
Arne Jansen73c5de02011-04-12 12:07:57 +02003151 sub_stripes = 1;
3152 dev_stripes = 1;
3153 devs_increment = 1;
3154 ncopies = 1;
3155 devs_max = 0; /* 0 == as many as possible */
3156 devs_min = 1;
3157
3158 /*
3159 * define the properties of each RAID type.
3160 * FIXME: move this to a global table and use it in all RAID
3161 * calculation code
3162 */
3163 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3164 dev_stripes = 2;
3165 ncopies = 2;
3166 devs_max = 1;
3167 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3168 devs_min = 2;
3169 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3170 devs_increment = 2;
3171 ncopies = 2;
3172 devs_max = 2;
3173 devs_min = 2;
3174 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3175 sub_stripes = 2;
3176 devs_increment = 2;
3177 ncopies = 2;
3178 devs_min = 4;
3179 } else {
3180 devs_max = 1;
3181 }
3182
3183 if (type & BTRFS_BLOCK_GROUP_DATA) {
3184 max_stripe_size = 1024 * 1024 * 1024;
3185 max_chunk_size = 10 * max_stripe_size;
3186 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003187 /* for larger filesystems, use larger metadata chunks */
3188 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3189 max_stripe_size = 1024 * 1024 * 1024;
3190 else
3191 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003192 max_chunk_size = max_stripe_size;
3193 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003194 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003195 max_chunk_size = 2 * max_stripe_size;
3196 } else {
3197 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3198 type);
3199 BUG_ON(1);
3200 }
3201
3202 /* we don't want a chunk larger than 10% of writeable space */
3203 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3204 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003205
3206 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3207 GFP_NOFS);
3208 if (!devices_info)
3209 return -ENOMEM;
3210
Arne Jansen73c5de02011-04-12 12:07:57 +02003211 cur = fs_devices->alloc_list.next;
3212
3213 /*
3214 * in the first pass through the devices list, we gather information
3215 * about the available holes on each device.
3216 */
3217 ndevs = 0;
3218 while (cur != &fs_devices->alloc_list) {
3219 struct btrfs_device *device;
3220 u64 max_avail;
3221 u64 dev_offset;
3222
3223 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3224
3225 cur = cur->next;
3226
3227 if (!device->writeable) {
3228 printk(KERN_ERR
3229 "btrfs: read-only device in alloc_list\n");
3230 WARN_ON(1);
3231 continue;
3232 }
3233
3234 if (!device->in_fs_metadata)
3235 continue;
3236
3237 if (device->total_bytes > device->bytes_used)
3238 total_avail = device->total_bytes - device->bytes_used;
3239 else
3240 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003241
3242 /* If there is no space on this device, skip it. */
3243 if (total_avail == 0)
3244 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003245
Li Zefan125ccb02011-12-08 15:07:24 +08003246 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003247 max_stripe_size * dev_stripes,
3248 &dev_offset, &max_avail);
3249 if (ret && ret != -ENOSPC)
3250 goto error;
3251
3252 if (ret == 0)
3253 max_avail = max_stripe_size * dev_stripes;
3254
3255 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3256 continue;
3257
3258 devices_info[ndevs].dev_offset = dev_offset;
3259 devices_info[ndevs].max_avail = max_avail;
3260 devices_info[ndevs].total_avail = total_avail;
3261 devices_info[ndevs].dev = device;
3262 ++ndevs;
3263 }
3264
3265 /*
3266 * now sort the devices by hole size / available space
3267 */
3268 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3269 btrfs_cmp_device_info, NULL);
3270
3271 /* round down to number of usable stripes */
3272 ndevs -= ndevs % devs_increment;
3273
3274 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3275 ret = -ENOSPC;
3276 goto error;
3277 }
3278
3279 if (devs_max && ndevs > devs_max)
3280 ndevs = devs_max;
3281 /*
3282 * the primary goal is to maximize the number of stripes, so use as many
3283 * devices as possible, even if the stripes are not maximum sized.
3284 */
3285 stripe_size = devices_info[ndevs-1].max_avail;
3286 num_stripes = ndevs * dev_stripes;
3287
3288 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
3289 stripe_size = max_chunk_size * ncopies;
3290 do_div(stripe_size, num_stripes);
3291 }
3292
3293 do_div(stripe_size, dev_stripes);
3294 do_div(stripe_size, BTRFS_STRIPE_LEN);
3295 stripe_size *= BTRFS_STRIPE_LEN;
3296
Miao Xieb2117a32011-01-05 10:07:28 +00003297 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3298 if (!map) {
3299 ret = -ENOMEM;
3300 goto error;
3301 }
3302 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003303
Arne Jansen73c5de02011-04-12 12:07:57 +02003304 for (i = 0; i < ndevs; ++i) {
3305 for (j = 0; j < dev_stripes; ++j) {
3306 int s = i * dev_stripes + j;
3307 map->stripes[s].dev = devices_info[i].dev;
3308 map->stripes[s].physical = devices_info[i].dev_offset +
3309 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003310 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003311 }
Chris Mason593060d2008-03-25 16:50:33 -04003312 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003313 map->stripe_len = BTRFS_STRIPE_LEN;
3314 map->io_align = BTRFS_STRIPE_LEN;
3315 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003316 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003317 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003318
Yan Zheng2b820322008-11-17 21:11:30 -05003319 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003320 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003321
Arne Jansen73c5de02011-04-12 12:07:57 +02003322 *stripe_size_out = stripe_size;
3323 *num_bytes_out = num_bytes;
3324
3325 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003326
David Sterba172ddd62011-04-21 00:48:27 +02003327 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003328 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003329 ret = -ENOMEM;
3330 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003331 }
Chris Mason0b86a832008-03-24 15:01:56 -04003332 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003333 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003334 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003335 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003336 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003337
Chris Mason0b86a832008-03-24 15:01:56 -04003338 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003339 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003340 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003341 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04003342 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003343 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05003344
3345 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3346 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003347 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05003348 BUG_ON(ret);
3349
Arne Jansen73c5de02011-04-12 12:07:57 +02003350 for (i = 0; i < map->num_stripes; ++i) {
3351 struct btrfs_device *device;
3352 u64 dev_offset;
3353
3354 device = map->stripes[i].dev;
3355 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003356
3357 ret = btrfs_alloc_dev_extent(trans, device,
3358 info->chunk_root->root_key.objectid,
3359 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003360 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003361 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003362 }
3363
Miao Xieb2117a32011-01-05 10:07:28 +00003364 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003365 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003366
3367error:
3368 kfree(map);
3369 kfree(devices_info);
3370 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003371}
3372
3373static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3374 struct btrfs_root *extent_root,
3375 struct map_lookup *map, u64 chunk_offset,
3376 u64 chunk_size, u64 stripe_size)
3377{
3378 u64 dev_offset;
3379 struct btrfs_key key;
3380 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3381 struct btrfs_device *device;
3382 struct btrfs_chunk *chunk;
3383 struct btrfs_stripe *stripe;
3384 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3385 int index = 0;
3386 int ret;
3387
3388 chunk = kzalloc(item_size, GFP_NOFS);
3389 if (!chunk)
3390 return -ENOMEM;
3391
3392 index = 0;
3393 while (index < map->num_stripes) {
3394 device = map->stripes[index].dev;
3395 device->bytes_used += stripe_size;
3396 ret = btrfs_update_device(trans, device);
3397 BUG_ON(ret);
3398 index++;
3399 }
3400
Josef Bacik2bf64752011-09-26 17:12:22 -04003401 spin_lock(&extent_root->fs_info->free_chunk_lock);
3402 extent_root->fs_info->free_chunk_space -= (stripe_size *
3403 map->num_stripes);
3404 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3405
Yan Zheng2b820322008-11-17 21:11:30 -05003406 index = 0;
3407 stripe = &chunk->stripe;
3408 while (index < map->num_stripes) {
3409 device = map->stripes[index].dev;
3410 dev_offset = map->stripes[index].physical;
3411
3412 btrfs_set_stack_stripe_devid(stripe, device->devid);
3413 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3414 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3415 stripe++;
3416 index++;
3417 }
3418
3419 btrfs_set_stack_chunk_length(chunk, chunk_size);
3420 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3421 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3422 btrfs_set_stack_chunk_type(chunk, map->type);
3423 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3424 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3425 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3426 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3427 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3428
3429 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3430 key.type = BTRFS_CHUNK_ITEM_KEY;
3431 key.offset = chunk_offset;
3432
3433 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
3434 BUG_ON(ret);
3435
3436 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Li Zefan125ccb02011-12-08 15:07:24 +08003437 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003438 item_size);
3439 BUG_ON(ret);
3440 }
liubo1abe9b82011-03-24 11:18:59 +00003441
Yan Zheng2b820322008-11-17 21:11:30 -05003442 kfree(chunk);
3443 return 0;
3444}
3445
3446/*
3447 * Chunk allocation falls into two parts. The first part does works
3448 * that make the new allocated chunk useable, but not do any operation
3449 * that modifies the chunk tree. The second part does the works that
3450 * require modifying the chunk tree. This division is important for the
3451 * bootstrap process of adding storage to a seed btrfs.
3452 */
3453int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3454 struct btrfs_root *extent_root, u64 type)
3455{
3456 u64 chunk_offset;
3457 u64 chunk_size;
3458 u64 stripe_size;
3459 struct map_lookup *map;
3460 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3461 int ret;
3462
3463 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3464 &chunk_offset);
3465 if (ret)
3466 return ret;
3467
3468 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3469 &stripe_size, chunk_offset, type);
3470 if (ret)
3471 return ret;
3472
3473 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3474 chunk_size, stripe_size);
3475 BUG_ON(ret);
3476 return 0;
3477}
3478
Chris Masond3977122009-01-05 21:25:51 -05003479static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003480 struct btrfs_root *root,
3481 struct btrfs_device *device)
3482{
3483 u64 chunk_offset;
3484 u64 sys_chunk_offset;
3485 u64 chunk_size;
3486 u64 sys_chunk_size;
3487 u64 stripe_size;
3488 u64 sys_stripe_size;
3489 u64 alloc_profile;
3490 struct map_lookup *map;
3491 struct map_lookup *sys_map;
3492 struct btrfs_fs_info *fs_info = root->fs_info;
3493 struct btrfs_root *extent_root = fs_info->extent_root;
3494 int ret;
3495
3496 ret = find_next_chunk(fs_info->chunk_root,
3497 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003498 if (ret)
3499 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003500
3501 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003502 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003503 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3504
3505 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3506 &stripe_size, chunk_offset, alloc_profile);
3507 BUG_ON(ret);
3508
3509 sys_chunk_offset = chunk_offset + chunk_size;
3510
3511 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003512 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003513 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3514
3515 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3516 &sys_chunk_size, &sys_stripe_size,
3517 sys_chunk_offset, alloc_profile);
3518 BUG_ON(ret);
3519
3520 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
3521 BUG_ON(ret);
3522
3523 /*
3524 * Modifying chunk tree needs allocating new blocks from both
3525 * system block group and metadata block group. So we only can
3526 * do operations require modifying the chunk tree after both
3527 * block groups were created.
3528 */
3529 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3530 chunk_size, stripe_size);
3531 BUG_ON(ret);
3532
3533 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3534 sys_chunk_offset, sys_chunk_size,
3535 sys_stripe_size);
3536 BUG_ON(ret);
3537 return 0;
3538}
3539
3540int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3541{
3542 struct extent_map *em;
3543 struct map_lookup *map;
3544 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3545 int readonly = 0;
3546 int i;
3547
Chris Mason890871b2009-09-02 16:24:52 -04003548 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003549 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003550 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003551 if (!em)
3552 return 1;
3553
Josef Bacikf48b9072010-01-27 02:07:59 +00003554 if (btrfs_test_opt(root, DEGRADED)) {
3555 free_extent_map(em);
3556 return 0;
3557 }
3558
Yan Zheng2b820322008-11-17 21:11:30 -05003559 map = (struct map_lookup *)em->bdev;
3560 for (i = 0; i < map->num_stripes; i++) {
3561 if (!map->stripes[i].dev->writeable) {
3562 readonly = 1;
3563 break;
3564 }
3565 }
3566 free_extent_map(em);
3567 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003568}
3569
3570void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3571{
David Sterbaa8067e02011-04-21 00:34:43 +02003572 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003573}
3574
3575void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3576{
3577 struct extent_map *em;
3578
Chris Masond3977122009-01-05 21:25:51 -05003579 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003580 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003581 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3582 if (em)
3583 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003584 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003585 if (!em)
3586 break;
3587 kfree(em->bdev);
3588 /* once for us */
3589 free_extent_map(em);
3590 /* once for the tree */
3591 free_extent_map(em);
3592 }
3593}
3594
Chris Masonf1885912008-04-09 16:28:12 -04003595int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3596{
3597 struct extent_map *em;
3598 struct map_lookup *map;
3599 struct extent_map_tree *em_tree = &map_tree->map_tree;
3600 int ret;
3601
Chris Mason890871b2009-09-02 16:24:52 -04003602 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003603 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003604 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003605 BUG_ON(!em);
3606
3607 BUG_ON(em->start > logical || em->start + em->len < logical);
3608 map = (struct map_lookup *)em->bdev;
3609 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3610 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003611 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3612 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003613 else
3614 ret = 1;
3615 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003616 return ret;
3617}
3618
Chris Masondfe25022008-05-13 13:46:40 -04003619static int find_live_mirror(struct map_lookup *map, int first, int num,
3620 int optimal)
3621{
3622 int i;
3623 if (map->stripes[optimal].dev->bdev)
3624 return optimal;
3625 for (i = first; i < first + num; i++) {
3626 if (map->stripes[i].dev->bdev)
3627 return i;
3628 }
3629 /* we couldn't find one that doesn't fail. Just return something
3630 * and the io error handling code will clean up eventually
3631 */
3632 return optimal;
3633}
3634
Chris Masonf2d8d742008-04-21 10:03:05 -04003635static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3636 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003637 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003638 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003639{
3640 struct extent_map *em;
3641 struct map_lookup *map;
3642 struct extent_map_tree *em_tree = &map_tree->map_tree;
3643 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003644 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003645 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003646 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003647 u64 stripe_nr_orig;
3648 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003649 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003650 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003651 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003652 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003653 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003654 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003655
Chris Mason890871b2009-09-02 16:24:52 -04003656 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003657 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003658 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003659
Chris Mason3b951512008-04-17 11:29:12 -04003660 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003661 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3662 (unsigned long long)logical,
3663 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003664 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003665 }
Chris Mason0b86a832008-03-24 15:01:56 -04003666
3667 BUG_ON(em->start > logical || em->start + em->len < logical);
3668 map = (struct map_lookup *)em->bdev;
3669 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003670
Chris Masonf1885912008-04-09 16:28:12 -04003671 if (mirror_num > map->num_stripes)
3672 mirror_num = 0;
3673
Chris Mason593060d2008-03-25 16:50:33 -04003674 stripe_nr = offset;
3675 /*
3676 * stripe_nr counts the total number of stripes we have to stride
3677 * to get to this block
3678 */
3679 do_div(stripe_nr, map->stripe_len);
3680
3681 stripe_offset = stripe_nr * map->stripe_len;
3682 BUG_ON(offset < stripe_offset);
3683
3684 /* stripe_offset is the offset of this block in its stripe*/
3685 stripe_offset = offset - stripe_offset;
3686
Li Dongyangfce3bb92011-03-24 10:24:26 +00003687 if (rw & REQ_DISCARD)
3688 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003689 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003690 /* we limit the length of each bio to what fits in a stripe */
3691 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003692 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003693 } else {
3694 *length = em->len - offset;
3695 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003696
Jan Schmidta1d3c472011-08-04 17:15:33 +02003697 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003698 goto out;
3699
Chris Masonf2d8d742008-04-21 10:03:05 -04003700 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003701 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003702 stripe_nr_orig = stripe_nr;
3703 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3704 (~(map->stripe_len - 1));
3705 do_div(stripe_nr_end, map->stripe_len);
3706 stripe_end_offset = stripe_nr_end * map->stripe_len -
3707 (offset + *length);
3708 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3709 if (rw & REQ_DISCARD)
3710 num_stripes = min_t(u64, map->num_stripes,
3711 stripe_nr_end - stripe_nr_orig);
3712 stripe_index = do_div(stripe_nr, map->num_stripes);
3713 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003714 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003715 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003716 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003717 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003718 else {
3719 stripe_index = find_live_mirror(map, 0,
3720 map->num_stripes,
3721 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003722 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003723 }
Chris Mason2fff7342008-04-29 14:12:09 -04003724
Chris Mason611f0e02008-04-03 16:29:03 -04003725 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003726 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003727 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003728 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003729 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003730 } else {
3731 mirror_num = 1;
3732 }
Chris Mason2fff7342008-04-29 14:12:09 -04003733
Chris Mason321aecc2008-04-16 10:49:51 -04003734 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3735 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003736
3737 stripe_index = do_div(stripe_nr, factor);
3738 stripe_index *= map->sub_stripes;
3739
Jens Axboe7eaceac2011-03-10 08:52:07 +01003740 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003741 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003742 else if (rw & REQ_DISCARD)
3743 num_stripes = min_t(u64, map->sub_stripes *
3744 (stripe_nr_end - stripe_nr_orig),
3745 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003746 else if (mirror_num)
3747 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003748 else {
3749 stripe_index = find_live_mirror(map, stripe_index,
3750 map->sub_stripes, stripe_index +
3751 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003752 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003753 }
Chris Mason8790d502008-04-03 16:29:03 -04003754 } else {
3755 /*
3756 * after this do_div call, stripe_nr is the number of stripes
3757 * on this device we have to walk to find the data, and
3758 * stripe_index is the number of our device in the stripe array
3759 */
3760 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003761 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003762 }
Chris Mason593060d2008-03-25 16:50:33 -04003763 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003764
Li Zefande11cc12011-12-01 12:55:47 +08003765 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3766 if (!bbio) {
3767 ret = -ENOMEM;
3768 goto out;
3769 }
3770 atomic_set(&bbio->error, 0);
3771
Li Dongyangfce3bb92011-03-24 10:24:26 +00003772 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003773 int factor = 0;
3774 int sub_stripes = 0;
3775 u64 stripes_per_dev = 0;
3776 u32 remaining_stripes = 0;
3777
3778 if (map->type &
3779 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3780 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3781 sub_stripes = 1;
3782 else
3783 sub_stripes = map->sub_stripes;
3784
3785 factor = map->num_stripes / sub_stripes;
3786 stripes_per_dev = div_u64_rem(stripe_nr_end -
3787 stripe_nr_orig,
3788 factor,
3789 &remaining_stripes);
3790 }
3791
Li Dongyangfce3bb92011-03-24 10:24:26 +00003792 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003793 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003794 map->stripes[stripe_index].physical +
3795 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003796 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003797
Li Zefanec9ef7a2011-12-01 14:06:42 +08003798 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3799 BTRFS_BLOCK_GROUP_RAID10)) {
3800 bbio->stripes[i].length = stripes_per_dev *
3801 map->stripe_len;
3802 if (i / sub_stripes < remaining_stripes)
3803 bbio->stripes[i].length +=
3804 map->stripe_len;
3805 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003806 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003807 stripe_offset;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003808 if ((i / sub_stripes + 1) %
3809 sub_stripes == remaining_stripes)
3810 bbio->stripes[i].length -=
3811 stripe_end_offset;
3812 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003813 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003814 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003815 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003816
3817 stripe_index++;
3818 if (stripe_index == map->num_stripes) {
3819 /* This could only happen for RAID0/10 */
3820 stripe_index = 0;
3821 stripe_nr++;
3822 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003823 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003824 } else {
3825 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003826 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003827 map->stripes[stripe_index].physical +
3828 stripe_offset +
3829 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003830 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003831 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003832 stripe_index++;
3833 }
Chris Mason593060d2008-03-25 16:50:33 -04003834 }
Li Zefande11cc12011-12-01 12:55:47 +08003835
3836 if (rw & REQ_WRITE) {
3837 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3838 BTRFS_BLOCK_GROUP_RAID10 |
3839 BTRFS_BLOCK_GROUP_DUP)) {
3840 max_errors = 1;
3841 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003842 }
Li Zefande11cc12011-12-01 12:55:47 +08003843
3844 *bbio_ret = bbio;
3845 bbio->num_stripes = num_stripes;
3846 bbio->max_errors = max_errors;
3847 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003848out:
Chris Mason0b86a832008-03-24 15:01:56 -04003849 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003850 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003851}
3852
Chris Masonf2d8d742008-04-21 10:03:05 -04003853int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3854 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003855 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003856{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003857 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003858 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003859}
3860
Yan Zhenga512bbf2008-12-08 16:46:26 -05003861int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3862 u64 chunk_start, u64 physical, u64 devid,
3863 u64 **logical, int *naddrs, int *stripe_len)
3864{
3865 struct extent_map_tree *em_tree = &map_tree->map_tree;
3866 struct extent_map *em;
3867 struct map_lookup *map;
3868 u64 *buf;
3869 u64 bytenr;
3870 u64 length;
3871 u64 stripe_nr;
3872 int i, j, nr = 0;
3873
Chris Mason890871b2009-09-02 16:24:52 -04003874 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003875 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003876 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003877
3878 BUG_ON(!em || em->start != chunk_start);
3879 map = (struct map_lookup *)em->bdev;
3880
3881 length = em->len;
3882 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3883 do_div(length, map->num_stripes / map->sub_stripes);
3884 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3885 do_div(length, map->num_stripes);
3886
3887 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3888 BUG_ON(!buf);
3889
3890 for (i = 0; i < map->num_stripes; i++) {
3891 if (devid && map->stripes[i].dev->devid != devid)
3892 continue;
3893 if (map->stripes[i].physical > physical ||
3894 map->stripes[i].physical + length <= physical)
3895 continue;
3896
3897 stripe_nr = physical - map->stripes[i].physical;
3898 do_div(stripe_nr, map->stripe_len);
3899
3900 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3901 stripe_nr = stripe_nr * map->num_stripes + i;
3902 do_div(stripe_nr, map->sub_stripes);
3903 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3904 stripe_nr = stripe_nr * map->num_stripes + i;
3905 }
3906 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003907 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003908 for (j = 0; j < nr; j++) {
3909 if (buf[j] == bytenr)
3910 break;
3911 }
Chris Mason934d3752008-12-08 16:43:10 -05003912 if (j == nr) {
3913 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003914 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003915 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003916 }
3917
Yan Zhenga512bbf2008-12-08 16:46:26 -05003918 *logical = buf;
3919 *naddrs = nr;
3920 *stripe_len = map->stripe_len;
3921
3922 free_extent_map(em);
3923 return 0;
3924}
3925
Jan Schmidta1d3c472011-08-04 17:15:33 +02003926static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003927{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003928 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003929 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003930
Chris Mason8790d502008-04-03 16:29:03 -04003931 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003932 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003933
Jan Schmidta1d3c472011-08-04 17:15:33 +02003934 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003935 is_orig_bio = 1;
3936
Jan Schmidta1d3c472011-08-04 17:15:33 +02003937 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003938 if (!is_orig_bio) {
3939 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003940 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003941 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003942 bio->bi_private = bbio->private;
3943 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02003944 bio->bi_bdev = (struct block_device *)
3945 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04003946 /* only send an error to the higher layers if it is
3947 * beyond the tolerance of the multi-bio
3948 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02003949 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003950 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05003951 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04003952 /*
3953 * this bio is actually up to date, we didn't
3954 * go over the max number of errors
3955 */
3956 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003957 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003958 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003959 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04003960
3961 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003962 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003963 bio_put(bio);
3964 }
Chris Mason8790d502008-04-03 16:29:03 -04003965}
3966
Chris Mason8b712842008-06-11 16:50:36 -04003967struct async_sched {
3968 struct bio *bio;
3969 int rw;
3970 struct btrfs_fs_info *info;
3971 struct btrfs_work work;
3972};
3973
3974/*
3975 * see run_scheduled_bios for a description of why bios are collected for
3976 * async submit.
3977 *
3978 * This will add one bio to the pending list for a device and make sure
3979 * the work struct is scheduled.
3980 */
Chris Masond3977122009-01-05 21:25:51 -05003981static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003982 struct btrfs_device *device,
3983 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003984{
3985 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003986 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003987
3988 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003989 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6d2008-07-31 16:29:02 -04003990 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01003991 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04003992 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003993 return 0;
3994 }
3995
3996 /*
Chris Mason0986fe92008-08-15 15:34:15 -04003997 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003998 * higher layers. Otherwise, the async bio makes it appear we have
3999 * made progress against dirty pages when we've really just put it
4000 * on a queue for later
4001 */
Chris Mason0986fe92008-08-15 15:34:15 -04004002 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04004003 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004004 bio->bi_next = NULL;
4005 bio->bi_rw |= rw;
4006
4007 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004008 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004009 pending_bios = &device->pending_sync_bios;
4010 else
4011 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004012
Chris Masonffbd5172009-04-20 15:50:09 -04004013 if (pending_bios->tail)
4014 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004015
Chris Masonffbd5172009-04-20 15:50:09 -04004016 pending_bios->tail = bio;
4017 if (!pending_bios->head)
4018 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004019 if (device->running_pending)
4020 should_queue = 0;
4021
4022 spin_unlock(&device->io_lock);
4023
4024 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004025 btrfs_queue_worker(&root->fs_info->submit_workers,
4026 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004027 return 0;
4028}
4029
Chris Masonf1885912008-04-09 16:28:12 -04004030int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004031 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004032{
4033 struct btrfs_mapping_tree *map_tree;
4034 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004035 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004036 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004037 u64 length = 0;
4038 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004039 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004040 int dev_nr = 0;
4041 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004042 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004043
Chris Masonf2d8d742008-04-21 10:03:05 -04004044 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004045 map_tree = &root->fs_info->mapping_tree;
4046 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004047
Jan Schmidta1d3c472011-08-04 17:15:33 +02004048 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004049 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04004050 BUG_ON(ret);
4051
Jan Schmidta1d3c472011-08-04 17:15:33 +02004052 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004053 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004054 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4055 "len %llu\n", (unsigned long long)logical,
4056 (unsigned long long)length,
4057 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004058 BUG();
4059 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004060
4061 bbio->orig_bio = first_bio;
4062 bbio->private = first_bio->bi_private;
4063 bbio->end_io = first_bio->bi_end_io;
4064 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004065
Chris Masond3977122009-01-05 21:25:51 -05004066 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004067 if (dev_nr < total_devs - 1) {
4068 bio = bio_clone(first_bio, GFP_NOFS);
4069 BUG_ON(!bio);
4070 } else {
4071 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004072 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004073 bio->bi_private = bbio;
4074 bio->bi_end_io = btrfs_end_bio;
4075 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4076 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004077 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004078 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4079 "(%s id %llu), size=%u\n", rw,
4080 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4081 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004082 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004083 if (async_submit)
4084 schedule_bio(root, dev, rw, bio);
4085 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004086 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004087 } else {
4088 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4089 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004090 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004091 }
Chris Mason8790d502008-04-03 16:29:03 -04004092 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004093 }
Chris Mason0b86a832008-03-24 15:01:56 -04004094 return 0;
4095}
4096
Chris Masona4437552008-04-18 10:29:38 -04004097struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004098 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004099{
Yan Zheng2b820322008-11-17 21:11:30 -05004100 struct btrfs_device *device;
4101 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004102
Yan Zheng2b820322008-11-17 21:11:30 -05004103 cur_devices = root->fs_info->fs_devices;
4104 while (cur_devices) {
4105 if (!fsid ||
4106 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4107 device = __find_device(&cur_devices->devices,
4108 devid, uuid);
4109 if (device)
4110 return device;
4111 }
4112 cur_devices = cur_devices->seed;
4113 }
4114 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004115}
4116
Chris Masondfe25022008-05-13 13:46:40 -04004117static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4118 u64 devid, u8 *dev_uuid)
4119{
4120 struct btrfs_device *device;
4121 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4122
4123 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004124 if (!device)
4125 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004126 list_add(&device->dev_list,
4127 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004128 device->dev_root = root->fs_info->dev_root;
4129 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004130 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004131 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004132 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004133 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004134 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004135 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004136 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004137 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4138 return device;
4139}
4140
Chris Mason0b86a832008-03-24 15:01:56 -04004141static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4142 struct extent_buffer *leaf,
4143 struct btrfs_chunk *chunk)
4144{
4145 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4146 struct map_lookup *map;
4147 struct extent_map *em;
4148 u64 logical;
4149 u64 length;
4150 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004151 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004152 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004153 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004154 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004155
Chris Masone17cade2008-04-15 15:41:47 -04004156 logical = key->offset;
4157 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004158
Chris Mason890871b2009-09-02 16:24:52 -04004159 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004160 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004161 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004162
4163 /* already mapped? */
4164 if (em && em->start <= logical && em->start + em->len > logical) {
4165 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004166 return 0;
4167 } else if (em) {
4168 free_extent_map(em);
4169 }
Chris Mason0b86a832008-03-24 15:01:56 -04004170
David Sterba172ddd62011-04-21 00:48:27 +02004171 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004172 if (!em)
4173 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004174 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4175 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004176 if (!map) {
4177 free_extent_map(em);
4178 return -ENOMEM;
4179 }
4180
4181 em->bdev = (struct block_device *)map;
4182 em->start = logical;
4183 em->len = length;
4184 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004185 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004186
Chris Mason593060d2008-03-25 16:50:33 -04004187 map->num_stripes = num_stripes;
4188 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4189 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4190 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4191 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4192 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004193 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004194 for (i = 0; i < num_stripes; i++) {
4195 map->stripes[i].physical =
4196 btrfs_stripe_offset_nr(leaf, chunk, i);
4197 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004198 read_extent_buffer(leaf, uuid, (unsigned long)
4199 btrfs_stripe_dev_uuid_nr(chunk, i),
4200 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004201 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4202 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004203 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004204 kfree(map);
4205 free_extent_map(em);
4206 return -EIO;
4207 }
Chris Masondfe25022008-05-13 13:46:40 -04004208 if (!map->stripes[i].dev) {
4209 map->stripes[i].dev =
4210 add_missing_dev(root, devid, uuid);
4211 if (!map->stripes[i].dev) {
4212 kfree(map);
4213 free_extent_map(em);
4214 return -EIO;
4215 }
4216 }
4217 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004218 }
4219
Chris Mason890871b2009-09-02 16:24:52 -04004220 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004221 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004222 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04004223 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04004224 free_extent_map(em);
4225
4226 return 0;
4227}
4228
4229static int fill_device_from_item(struct extent_buffer *leaf,
4230 struct btrfs_dev_item *dev_item,
4231 struct btrfs_device *device)
4232{
4233 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004234
4235 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004236 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4237 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004238 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4239 device->type = btrfs_device_type(leaf, dev_item);
4240 device->io_align = btrfs_device_io_align(leaf, dev_item);
4241 device->io_width = btrfs_device_io_width(leaf, dev_item);
4242 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004243
4244 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004245 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004246
Chris Mason0b86a832008-03-24 15:01:56 -04004247 return 0;
4248}
4249
Yan Zheng2b820322008-11-17 21:11:30 -05004250static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4251{
4252 struct btrfs_fs_devices *fs_devices;
4253 int ret;
4254
Li Zefanb367e472011-12-07 11:38:24 +08004255 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004256
4257 fs_devices = root->fs_info->fs_devices->seed;
4258 while (fs_devices) {
4259 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4260 ret = 0;
4261 goto out;
4262 }
4263 fs_devices = fs_devices->seed;
4264 }
4265
4266 fs_devices = find_fsid(fsid);
4267 if (!fs_devices) {
4268 ret = -ENOENT;
4269 goto out;
4270 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004271
4272 fs_devices = clone_fs_devices(fs_devices);
4273 if (IS_ERR(fs_devices)) {
4274 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004275 goto out;
4276 }
4277
Christoph Hellwig97288f22008-12-02 06:36:09 -05004278 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004279 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05004280 if (ret)
4281 goto out;
4282
4283 if (!fs_devices->seeding) {
4284 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004285 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004286 ret = -EINVAL;
4287 goto out;
4288 }
4289
4290 fs_devices->seed = root->fs_info->fs_devices->seed;
4291 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004292out:
Yan Zheng2b820322008-11-17 21:11:30 -05004293 return ret;
4294}
4295
Chris Mason0d81ba52008-03-24 15:02:07 -04004296static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004297 struct extent_buffer *leaf,
4298 struct btrfs_dev_item *dev_item)
4299{
4300 struct btrfs_device *device;
4301 u64 devid;
4302 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004303 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004304 u8 dev_uuid[BTRFS_UUID_SIZE];
4305
Chris Mason0b86a832008-03-24 15:01:56 -04004306 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004307 read_extent_buffer(leaf, dev_uuid,
4308 (unsigned long)btrfs_device_uuid(dev_item),
4309 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004310 read_extent_buffer(leaf, fs_uuid,
4311 (unsigned long)btrfs_device_fsid(dev_item),
4312 BTRFS_UUID_SIZE);
4313
4314 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4315 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004316 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004317 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004318 }
4319
4320 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4321 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004322 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004323 return -EIO;
4324
4325 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004326 printk(KERN_WARNING "warning devid %llu missing\n",
4327 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004328 device = add_missing_dev(root, devid, dev_uuid);
4329 if (!device)
4330 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004331 } else if (!device->missing) {
4332 /*
4333 * this happens when a device that was properly setup
4334 * in the device info lists suddenly goes bad.
4335 * device->bdev is NULL, and so we have to set
4336 * device->missing to one here
4337 */
4338 root->fs_info->fs_devices->missing_devices++;
4339 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004340 }
4341 }
4342
4343 if (device->fs_devices != root->fs_info->fs_devices) {
4344 BUG_ON(device->writeable);
4345 if (device->generation !=
4346 btrfs_device_generation(leaf, dev_item))
4347 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004348 }
Chris Mason0b86a832008-03-24 15:01:56 -04004349
4350 fill_device_from_item(leaf, dev_item, device);
4351 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004352 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004353 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004354 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004355 spin_lock(&root->fs_info->free_chunk_lock);
4356 root->fs_info->free_chunk_space += device->total_bytes -
4357 device->bytes_used;
4358 spin_unlock(&root->fs_info->free_chunk_lock);
4359 }
Chris Mason0b86a832008-03-24 15:01:56 -04004360 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004361 return ret;
4362}
4363
Yan Zhenge4404d62008-12-12 10:03:26 -05004364int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004365{
David Sterba6c417612011-04-13 15:41:04 +02004366 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004367 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004368 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004369 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004370 u8 *ptr;
4371 unsigned long sb_ptr;
4372 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004373 u32 num_stripes;
4374 u32 array_size;
4375 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004376 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004377 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004378
Yan Zhenge4404d62008-12-12 10:03:26 -05004379 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004380 BTRFS_SUPER_INFO_SIZE);
4381 if (!sb)
4382 return -ENOMEM;
4383 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004384 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004385 /*
4386 * The sb extent buffer is artifical and just used to read the system array.
4387 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4388 * pages up-to-date when the page is larger: extent does not cover the
4389 * whole page and consequently check_page_uptodate does not find all
4390 * the page's extents up-to-date (the hole beyond sb),
4391 * write_extent_buffer then triggers a WARN_ON.
4392 *
4393 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4394 * but sb spans only this function. Add an explicit SetPageUptodate call
4395 * to silence the warning eg. on PowerPC 64.
4396 */
4397 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004398 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004399
Chris Masona061fc82008-05-07 11:43:44 -04004400 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004401 array_size = btrfs_super_sys_array_size(super_copy);
4402
Chris Mason0b86a832008-03-24 15:01:56 -04004403 ptr = super_copy->sys_chunk_array;
4404 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4405 cur = 0;
4406
4407 while (cur < array_size) {
4408 disk_key = (struct btrfs_disk_key *)ptr;
4409 btrfs_disk_key_to_cpu(&key, disk_key);
4410
Chris Masona061fc82008-05-07 11:43:44 -04004411 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004412 sb_ptr += len;
4413 cur += len;
4414
Chris Mason0d81ba52008-03-24 15:02:07 -04004415 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004416 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004417 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004418 if (ret)
4419 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004420 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4421 len = btrfs_chunk_item_size(num_stripes);
4422 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004423 ret = -EIO;
4424 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004425 }
4426 ptr += len;
4427 sb_ptr += len;
4428 cur += len;
4429 }
Chris Masona061fc82008-05-07 11:43:44 -04004430 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004431 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004432}
4433
4434int btrfs_read_chunk_tree(struct btrfs_root *root)
4435{
4436 struct btrfs_path *path;
4437 struct extent_buffer *leaf;
4438 struct btrfs_key key;
4439 struct btrfs_key found_key;
4440 int ret;
4441 int slot;
4442
4443 root = root->fs_info->chunk_root;
4444
4445 path = btrfs_alloc_path();
4446 if (!path)
4447 return -ENOMEM;
4448
Li Zefanb367e472011-12-07 11:38:24 +08004449 mutex_lock(&uuid_mutex);
4450 lock_chunks(root);
4451
Chris Mason0b86a832008-03-24 15:01:56 -04004452 /* first we search for all of the device items, and then we
4453 * read in all of the chunk items. This way we can create chunk
4454 * mappings that reference all of the devices that are afound
4455 */
4456 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4457 key.offset = 0;
4458 key.type = 0;
4459again:
4460 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004461 if (ret < 0)
4462 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004463 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004464 leaf = path->nodes[0];
4465 slot = path->slots[0];
4466 if (slot >= btrfs_header_nritems(leaf)) {
4467 ret = btrfs_next_leaf(root, path);
4468 if (ret == 0)
4469 continue;
4470 if (ret < 0)
4471 goto error;
4472 break;
4473 }
4474 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4475 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4476 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4477 break;
4478 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4479 struct btrfs_dev_item *dev_item;
4480 dev_item = btrfs_item_ptr(leaf, slot,
4481 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004482 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004483 if (ret)
4484 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004485 }
4486 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4487 struct btrfs_chunk *chunk;
4488 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4489 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004490 if (ret)
4491 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004492 }
4493 path->slots[0]++;
4494 }
4495 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4496 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004497 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004498 goto again;
4499 }
Chris Mason0b86a832008-03-24 15:01:56 -04004500 ret = 0;
4501error:
Li Zefanb367e472011-12-07 11:38:24 +08004502 unlock_chunks(root);
4503 mutex_unlock(&uuid_mutex);
4504
Yan Zheng2b820322008-11-17 21:11:30 -05004505 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004506 return ret;
4507}