blob: 6b9cff42265d06e5b26a7ed53684a5551675a930 [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>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050028#include <linux/raid/pq.h>
29#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050030#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "ctree.h"
32#include "extent_map.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "print-tree.h"
36#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040038#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010039#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060041#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Chris Mason0b86a832008-03-24 15:01:56 -040043
Yan Zheng2b820322008-11-17 21:11:30 -050044static int init_first_rw_device(struct btrfs_trans_handle *trans,
45 struct btrfs_root *root,
46 struct btrfs_device *device);
47static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020048static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
49static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050050
Chris Mason8a4b83c2008-03-24 15:02:07 -040051static DEFINE_MUTEX(uuid_mutex);
52static LIST_HEAD(fs_uuids);
53
Chris Mason7d9eb122008-07-08 14:19:17 -040054static void lock_chunks(struct btrfs_root *root)
55{
Chris Mason7d9eb122008-07-08 14:19:17 -040056 mutex_lock(&root->fs_info->chunk_mutex);
57}
58
59static void unlock_chunks(struct btrfs_root *root)
60{
Chris Mason7d9eb122008-07-08 14:19:17 -040061 mutex_unlock(&root->fs_info->chunk_mutex);
62}
63
Yan Zhenge4404d62008-12-12 10:03:26 -050064static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
65{
66 struct btrfs_device *device;
67 WARN_ON(fs_devices->opened);
68 while (!list_empty(&fs_devices->devices)) {
69 device = list_entry(fs_devices->devices.next,
70 struct btrfs_device, dev_list);
71 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -040072 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -050073 kfree(device);
74 }
75 kfree(fs_devices);
76}
77
Lukas Czernerb8b8ff52012-12-06 19:25:48 +000078static void btrfs_kobject_uevent(struct block_device *bdev,
79 enum kobject_action action)
80{
81 int ret;
82
83 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
84 if (ret)
85 pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
86 action,
87 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
88 &disk_to_dev(bdev->bd_disk)->kobj);
89}
90
Jeff Mahoney143bede2012-03-01 14:56:26 +010091void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040092{
93 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040094
Yan Zheng2b820322008-11-17 21:11:30 -050095 while (!list_empty(&fs_uuids)) {
96 fs_devices = list_entry(fs_uuids.next,
97 struct btrfs_fs_devices, list);
98 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050099 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400100 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101}
102
Chris Masona1b32a52008-09-05 16:09:51 -0400103static noinline struct btrfs_device *__find_device(struct list_head *head,
104 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400105{
106 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400107
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500108 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400109 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400110 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400111 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400112 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400113 }
114 return NULL;
115}
116
Chris Masona1b32a52008-09-05 16:09:51 -0400117static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400118{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400119 struct btrfs_fs_devices *fs_devices;
120
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500121 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400122 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
123 return fs_devices;
124 }
125 return NULL;
126}
127
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100128static int
129btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
130 int flush, struct block_device **bdev,
131 struct buffer_head **bh)
132{
133 int ret;
134
135 *bdev = blkdev_get_by_path(device_path, flags, holder);
136
137 if (IS_ERR(*bdev)) {
138 ret = PTR_ERR(*bdev);
139 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
140 goto error;
141 }
142
143 if (flush)
144 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
145 ret = set_blocksize(*bdev, 4096);
146 if (ret) {
147 blkdev_put(*bdev, flags);
148 goto error;
149 }
150 invalidate_bdev(*bdev);
151 *bh = btrfs_read_dev_super(*bdev);
152 if (!*bh) {
153 ret = -EINVAL;
154 blkdev_put(*bdev, flags);
155 goto error;
156 }
157
158 return 0;
159
160error:
161 *bdev = NULL;
162 *bh = NULL;
163 return ret;
164}
165
Chris Masonffbd5172009-04-20 15:50:09 -0400166static void requeue_list(struct btrfs_pending_bios *pending_bios,
167 struct bio *head, struct bio *tail)
168{
169
170 struct bio *old_head;
171
172 old_head = pending_bios->head;
173 pending_bios->head = head;
174 if (pending_bios->tail)
175 tail->bi_next = old_head;
176 else
177 pending_bios->tail = tail;
178}
179
Chris Mason8b712842008-06-11 16:50:36 -0400180/*
181 * we try to collect pending bios for a device so we don't get a large
182 * number of procs sending bios down to the same device. This greatly
183 * improves the schedulers ability to collect and merge the bios.
184 *
185 * But, it also turns into a long list of bios to process and that is sure
186 * to eventually make the worker thread block. The solution here is to
187 * make some progress and then put this work struct back at the end of
188 * the list if the block device is congested. This way, multiple devices
189 * can make progress from a single worker thread.
190 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100191static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400192{
193 struct bio *pending;
194 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400195 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400196 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400197 struct bio *tail;
198 struct bio *cur;
199 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400200 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400201 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400202 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400203 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400204 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000205 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400206 struct blk_plug plug;
207
208 /*
209 * this function runs all the bios we've collected for
210 * a particular device. We don't want to wander off to
211 * another device without first sending all of these down.
212 * So, setup a plug here and finish it off before we return
213 */
214 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400215
Chris Masonbedf7622009-04-03 10:32:58 -0400216 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400217 fs_info = device->dev_root->fs_info;
218 limit = btrfs_async_submit_limit(fs_info);
219 limit = limit * 2 / 3;
220
Chris Mason8b712842008-06-11 16:50:36 -0400221loop:
222 spin_lock(&device->io_lock);
223
Chris Masona6837052009-02-04 09:19:41 -0500224loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400225 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400226
Chris Mason8b712842008-06-11 16:50:36 -0400227 /* take all the bios off the list at once and process them
228 * later on (without the lock held). But, remember the
229 * tail and other pointers so the bios can be properly reinserted
230 * into the list if we hit congestion
231 */
Chris Masond84275c2009-06-09 15:39:08 -0400232 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400233 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400234 force_reg = 1;
235 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400236 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400237 force_reg = 0;
238 }
Chris Masonffbd5172009-04-20 15:50:09 -0400239
240 pending = pending_bios->head;
241 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400242 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400243
244 /*
245 * if pending was null this time around, no bios need processing
246 * at all and we can stop. Otherwise it'll loop back up again
247 * and do an additional check so no bios are missed.
248 *
249 * device->running_pending is used to synchronize with the
250 * schedule_bio code.
251 */
Chris Masonffbd5172009-04-20 15:50:09 -0400252 if (device->pending_sync_bios.head == NULL &&
253 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400254 again = 0;
255 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400256 } else {
257 again = 1;
258 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400259 }
Chris Masonffbd5172009-04-20 15:50:09 -0400260
261 pending_bios->head = NULL;
262 pending_bios->tail = NULL;
263
Chris Mason8b712842008-06-11 16:50:36 -0400264 spin_unlock(&device->io_lock);
265
Chris Masond3977122009-01-05 21:25:51 -0500266 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400267
268 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400269 /* we want to work on both lists, but do more bios on the
270 * sync list than the regular list
271 */
272 if ((num_run > 32 &&
273 pending_bios != &device->pending_sync_bios &&
274 device->pending_sync_bios.head) ||
275 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
276 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400277 spin_lock(&device->io_lock);
278 requeue_list(pending_bios, pending, tail);
279 goto loop_lock;
280 }
281
Chris Mason8b712842008-06-11 16:50:36 -0400282 cur = pending;
283 pending = pending->bi_next;
284 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400285
Josef Bacik66657b32012-08-01 15:36:24 -0400286 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400287 waitqueue_active(&fs_info->async_submit_wait))
288 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400289
290 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400291
Chris Mason2ab1ba62011-08-04 14:28:36 -0400292 /*
293 * if we're doing the sync list, record that our
294 * plug has some sync requests on it
295 *
296 * If we're doing the regular list and there are
297 * sync requests sitting around, unplug before
298 * we add more
299 */
300 if (pending_bios == &device->pending_sync_bios) {
301 sync_pending = 1;
302 } else if (sync_pending) {
303 blk_finish_plug(&plug);
304 blk_start_plug(&plug);
305 sync_pending = 0;
306 }
307
Stefan Behrens21adbd52011-11-09 13:44:05 +0100308 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400309 num_run++;
310 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100311 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400312 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400313
314 /*
315 * we made progress, there is more work to do and the bdi
316 * is now congested. Back off and let other work structs
317 * run instead
318 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400319 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500320 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400321 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400322
Chris Masonb765ead2009-04-03 10:27:10 -0400323 ioc = current->io_context;
324
325 /*
326 * the main goal here is that we don't want to
327 * block if we're going to be able to submit
328 * more requests without blocking.
329 *
330 * This code does two great things, it pokes into
331 * the elevator code from a filesystem _and_
332 * it makes assumptions about how batching works.
333 */
334 if (ioc && ioc->nr_batch_requests > 0 &&
335 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
336 (last_waited == 0 ||
337 ioc->last_waited == last_waited)) {
338 /*
339 * we want to go through our batch of
340 * requests and stop. So, we copy out
341 * the ioc->last_waited time and test
342 * against it before looping
343 */
344 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100345 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400346 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400347 continue;
348 }
Chris Mason8b712842008-06-11 16:50:36 -0400349 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400350 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500351 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400352
353 spin_unlock(&device->io_lock);
354 btrfs_requeue_work(&device->work);
355 goto done;
356 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500357 /* unplug every 64 requests just for good measure */
358 if (batch_run % 64 == 0) {
359 blk_finish_plug(&plug);
360 blk_start_plug(&plug);
361 sync_pending = 0;
362 }
Chris Mason8b712842008-06-11 16:50:36 -0400363 }
Chris Masonffbd5172009-04-20 15:50:09 -0400364
Chris Mason51684082010-03-10 15:33:32 -0500365 cond_resched();
366 if (again)
367 goto loop;
368
369 spin_lock(&device->io_lock);
370 if (device->pending_bios.head || device->pending_sync_bios.head)
371 goto loop_lock;
372 spin_unlock(&device->io_lock);
373
Chris Mason8b712842008-06-11 16:50:36 -0400374done:
Chris Mason211588a2011-04-19 20:12:40 -0400375 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400376}
377
Christoph Hellwigb2950862008-12-02 09:54:17 -0500378static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400379{
380 struct btrfs_device *device;
381
382 device = container_of(work, struct btrfs_device, work);
383 run_scheduled_bios(device);
384}
385
Chris Masona1b32a52008-09-05 16:09:51 -0400386static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400387 struct btrfs_super_block *disk_super,
388 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
389{
390 struct btrfs_device *device;
391 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400392 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400393 u64 found_transid = btrfs_super_generation(disk_super);
394
395 fs_devices = find_fsid(disk_super->fsid);
396 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400397 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400398 if (!fs_devices)
399 return -ENOMEM;
400 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400401 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400402 list_add(&fs_devices->list, &fs_uuids);
403 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
404 fs_devices->latest_devid = devid;
405 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400406 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400407 device = NULL;
408 } else {
Chris Masona4437552008-04-18 10:29:38 -0400409 device = __find_device(&fs_devices->devices, devid,
410 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400411 }
412 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500413 if (fs_devices->opened)
414 return -EBUSY;
415
Chris Mason8a4b83c2008-03-24 15:02:07 -0400416 device = kzalloc(sizeof(*device), GFP_NOFS);
417 if (!device) {
418 /* we can safely leave the fs_devices entry around */
419 return -ENOMEM;
420 }
421 device->devid = devid;
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200422 device->dev_stats_valid = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400423 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400424 memcpy(device->uuid, disk_super->dev_item.uuid,
425 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400426 spin_lock_init(&device->io_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400427
428 name = rcu_string_strdup(path, GFP_NOFS);
429 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400430 kfree(device);
431 return -ENOMEM;
432 }
Josef Bacik606686e2012-06-04 14:03:51 -0400433 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -0500434 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400435
Arne Jansen90519d62011-05-23 14:30:00 +0200436 /* init readahead state */
437 spin_lock_init(&device->reada_lock);
438 device->reada_curr_zone = NULL;
439 atomic_set(&device->reada_in_flight, 0);
440 device->reada_next = 0;
441 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
442 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
443
Chris Masone5e9a522009-06-10 15:17:02 -0400444 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000445 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400446 mutex_unlock(&fs_devices->device_list_mutex);
447
Yan Zheng2b820322008-11-17 21:11:30 -0500448 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400449 fs_devices->num_devices++;
Josef Bacik606686e2012-06-04 14:03:51 -0400450 } else if (!device->name || strcmp(device->name->str, path)) {
451 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000452 if (!name)
453 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400454 rcu_string_free(device->name);
455 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500456 if (device->missing) {
457 fs_devices->missing_devices--;
458 device->missing = 0;
459 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400460 }
461
462 if (found_transid > fs_devices->latest_trans) {
463 fs_devices->latest_devid = devid;
464 fs_devices->latest_trans = found_transid;
465 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400466 *fs_devices_ret = fs_devices;
467 return 0;
468}
469
Yan Zhenge4404d62008-12-12 10:03:26 -0500470static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
471{
472 struct btrfs_fs_devices *fs_devices;
473 struct btrfs_device *device;
474 struct btrfs_device *orig_dev;
475
476 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
477 if (!fs_devices)
478 return ERR_PTR(-ENOMEM);
479
480 INIT_LIST_HEAD(&fs_devices->devices);
481 INIT_LIST_HEAD(&fs_devices->alloc_list);
482 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400483 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500484 fs_devices->latest_devid = orig->latest_devid;
485 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400486 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500487 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
488
Xiao Guangrong46224702011-04-20 10:08:47 +0000489 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500490 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400491 struct rcu_string *name;
492
Yan Zhenge4404d62008-12-12 10:03:26 -0500493 device = kzalloc(sizeof(*device), GFP_NOFS);
494 if (!device)
495 goto error;
496
Josef Bacik606686e2012-06-04 14:03:51 -0400497 /*
498 * This is ok to do without rcu read locked because we hold the
499 * uuid mutex so nothing we touch in here is going to disappear.
500 */
501 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
502 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400503 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500504 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400505 }
Josef Bacik606686e2012-06-04 14:03:51 -0400506 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500507
508 device->devid = orig_dev->devid;
509 device->work.func = pending_bios_fn;
510 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500511 spin_lock_init(&device->io_lock);
512 INIT_LIST_HEAD(&device->dev_list);
513 INIT_LIST_HEAD(&device->dev_alloc_list);
514
515 list_add(&device->dev_list, &fs_devices->devices);
516 device->fs_devices = fs_devices;
517 fs_devices->num_devices++;
518 }
519 return fs_devices;
520error:
521 free_fs_devices(fs_devices);
522 return ERR_PTR(-ENOMEM);
523}
524
Stefan Behrens8dabb742012-11-06 13:15:27 +0100525void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
526 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400527{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500528 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400529
Chris Masona6b0d5c2012-02-20 20:53:43 -0500530 struct block_device *latest_bdev = NULL;
531 u64 latest_devid = 0;
532 u64 latest_transid = 0;
533
Chris Masondfe25022008-05-13 13:46:40 -0400534 mutex_lock(&uuid_mutex);
535again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000536 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500537 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500538 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100539 if (!device->is_tgtdev_for_dev_replace &&
540 (!latest_transid ||
541 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500542 latest_devid = device->devid;
543 latest_transid = device->generation;
544 latest_bdev = device->bdev;
545 }
Yan Zheng2b820322008-11-17 21:11:30 -0500546 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500547 }
Yan Zheng2b820322008-11-17 21:11:30 -0500548
Stefan Behrens8dabb742012-11-06 13:15:27 +0100549 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
550 /*
551 * In the first step, keep the device which has
552 * the correct fsid and the devid that is used
553 * for the dev_replace procedure.
554 * In the second step, the dev_replace state is
555 * read from the device tree and it is known
556 * whether the procedure is really active or
557 * not, which means whether this device is
558 * used or whether it should be removed.
559 */
560 if (step == 0 || device->is_tgtdev_for_dev_replace) {
561 continue;
562 }
563 }
Yan Zheng2b820322008-11-17 21:11:30 -0500564 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100565 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500566 device->bdev = NULL;
567 fs_devices->open_devices--;
568 }
569 if (device->writeable) {
570 list_del_init(&device->dev_alloc_list);
571 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100572 if (!device->is_tgtdev_for_dev_replace)
573 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500574 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500575 list_del_init(&device->dev_list);
576 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400577 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500578 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400579 }
Yan Zheng2b820322008-11-17 21:11:30 -0500580
581 if (fs_devices->seed) {
582 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500583 goto again;
584 }
585
Chris Masona6b0d5c2012-02-20 20:53:43 -0500586 fs_devices->latest_bdev = latest_bdev;
587 fs_devices->latest_devid = latest_devid;
588 fs_devices->latest_trans = latest_transid;
589
Chris Masondfe25022008-05-13 13:46:40 -0400590 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400591}
Chris Masona0af4692008-05-13 16:03:06 -0400592
Xiao Guangrong1f781602011-04-20 10:09:16 +0000593static void __free_device(struct work_struct *work)
594{
595 struct btrfs_device *device;
596
597 device = container_of(work, struct btrfs_device, rcu_work);
598
599 if (device->bdev)
600 blkdev_put(device->bdev, device->mode);
601
Josef Bacik606686e2012-06-04 14:03:51 -0400602 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000603 kfree(device);
604}
605
606static void free_device(struct rcu_head *head)
607{
608 struct btrfs_device *device;
609
610 device = container_of(head, struct btrfs_device, rcu);
611
612 INIT_WORK(&device->rcu_work, __free_device);
613 schedule_work(&device->rcu_work);
614}
615
Yan Zheng2b820322008-11-17 21:11:30 -0500616static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400617{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400618 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500619
Yan Zheng2b820322008-11-17 21:11:30 -0500620 if (--fs_devices->opened > 0)
621 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400622
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000623 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500624 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000625 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400626 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000627
628 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400629 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000630
Stefan Behrens8dabb742012-11-06 13:15:27 +0100631 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500632 list_del_init(&device->dev_alloc_list);
633 fs_devices->rw_devices--;
634 }
635
Josef Bacikd5e20032011-08-04 14:52:27 +0000636 if (device->can_discard)
637 fs_devices->num_can_discard--;
638
Xiao Guangrong1f781602011-04-20 10:09:16 +0000639 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100640 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000641 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400642
643 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400644 if (device->name) {
645 name = rcu_string_strdup(device->name->str, GFP_NOFS);
646 BUG_ON(device->name && !name); /* -ENOMEM */
647 rcu_assign_pointer(new_device->name, name);
648 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000649 new_device->bdev = NULL;
650 new_device->writeable = 0;
651 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000652 new_device->can_discard = 0;
Thomas Gleixner1cba0cd2013-02-20 14:06:20 -0500653 spin_lock_init(&new_device->io_lock);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000654 list_replace_rcu(&device->dev_list, &new_device->dev_list);
655
656 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400657 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000658 mutex_unlock(&fs_devices->device_list_mutex);
659
Yan Zhenge4404d62008-12-12 10:03:26 -0500660 WARN_ON(fs_devices->open_devices);
661 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500662 fs_devices->opened = 0;
663 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500664
Chris Mason8a4b83c2008-03-24 15:02:07 -0400665 return 0;
666}
667
Yan Zheng2b820322008-11-17 21:11:30 -0500668int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
669{
Yan Zhenge4404d62008-12-12 10:03:26 -0500670 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500671 int ret;
672
673 mutex_lock(&uuid_mutex);
674 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500675 if (!fs_devices->opened) {
676 seed_devices = fs_devices->seed;
677 fs_devices->seed = NULL;
678 }
Yan Zheng2b820322008-11-17 21:11:30 -0500679 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500680
681 while (seed_devices) {
682 fs_devices = seed_devices;
683 seed_devices = fs_devices->seed;
684 __btrfs_close_devices(fs_devices);
685 free_fs_devices(fs_devices);
686 }
Yan Zheng2b820322008-11-17 21:11:30 -0500687 return ret;
688}
689
Yan Zhenge4404d62008-12-12 10:03:26 -0500690static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
691 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400692{
Josef Bacikd5e20032011-08-04 14:52:27 +0000693 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400694 struct block_device *bdev;
695 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400697 struct block_device *latest_bdev = NULL;
698 struct buffer_head *bh;
699 struct btrfs_super_block *disk_super;
700 u64 latest_devid = 0;
701 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400702 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500703 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400704 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400705
Tejun Heod4d77622010-11-13 11:55:18 +0100706 flags |= FMODE_EXCL;
707
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500708 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400709 if (device->bdev)
710 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400711 if (!device->name)
712 continue;
713
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100714 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
715 &bdev, &bh);
716 if (ret)
717 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400718
719 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000720 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400721 if (devid != device->devid)
722 goto error_brelse;
723
Yan Zheng2b820322008-11-17 21:11:30 -0500724 if (memcmp(device->uuid, disk_super->dev_item.uuid,
725 BTRFS_UUID_SIZE))
726 goto error_brelse;
727
728 device->generation = btrfs_super_generation(disk_super);
729 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400730 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500731 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400732 latest_bdev = bdev;
733 }
734
Yan Zheng2b820322008-11-17 21:11:30 -0500735 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
736 device->writeable = 0;
737 } else {
738 device->writeable = !bdev_read_only(bdev);
739 seeding = 0;
740 }
741
Josef Bacikd5e20032011-08-04 14:52:27 +0000742 q = bdev_get_queue(bdev);
743 if (blk_queue_discard(q)) {
744 device->can_discard = 1;
745 fs_devices->num_can_discard++;
746 }
747
Chris Mason8a4b83c2008-03-24 15:02:07 -0400748 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400749 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500750 device->mode = flags;
751
Chris Masonc2898112009-06-10 09:51:32 -0400752 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
753 fs_devices->rotating = 1;
754
Chris Masona0af4692008-05-13 16:03:06 -0400755 fs_devices->open_devices++;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100756 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500757 fs_devices->rw_devices++;
758 list_add(&device->dev_alloc_list,
759 &fs_devices->alloc_list);
760 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000761 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400762 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400763
Chris Masona0af4692008-05-13 16:03:06 -0400764error_brelse:
765 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100766 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400767 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400768 }
Chris Masona0af4692008-05-13 16:03:06 -0400769 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300770 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400771 goto out;
772 }
Yan Zheng2b820322008-11-17 21:11:30 -0500773 fs_devices->seeding = seeding;
774 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400775 fs_devices->latest_bdev = latest_bdev;
776 fs_devices->latest_devid = latest_devid;
777 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500778 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400779out:
Yan Zheng2b820322008-11-17 21:11:30 -0500780 return ret;
781}
782
783int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500784 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500785{
786 int ret;
787
788 mutex_lock(&uuid_mutex);
789 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500790 fs_devices->opened++;
791 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500792 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500793 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500794 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400795 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400796 return ret;
797}
798
David Sterba6f60cbd2013-02-15 11:31:02 -0700799/*
800 * Look for a btrfs signature on a device. This may be called out of the mount path
801 * and we are not allowed to call set_blocksize during the scan. The superblock
802 * is read via pagecache
803 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500804int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400805 struct btrfs_fs_devices **fs_devices_ret)
806{
807 struct btrfs_super_block *disk_super;
808 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700809 struct page *page;
810 void *p;
811 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400812 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400813 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400814 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700815 u64 bytenr;
816 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400817
David Sterba6f60cbd2013-02-15 11:31:02 -0700818 /*
819 * we would like to check all the supers, but that would make
820 * a btrfs mount succeed after a mkfs from a different FS.
821 * So, we need to add a special mount option to scan for
822 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
823 */
824 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100825 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500826 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700827
828 bdev = blkdev_get_by_path(path, flags, holder);
829
830 if (IS_ERR(bdev)) {
831 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100832 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700833 }
834
835 /* make sure our super fits in the device */
836 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
837 goto error_bdev_put;
838
839 /* make sure our super fits in the page */
840 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
841 goto error_bdev_put;
842
843 /* make sure our super doesn't straddle pages on disk */
844 index = bytenr >> PAGE_CACHE_SHIFT;
845 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
846 goto error_bdev_put;
847
848 /* pull in the page with our super */
849 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
850 index, GFP_NOFS);
851
852 if (IS_ERR_OR_NULL(page))
853 goto error_bdev_put;
854
855 p = kmap(page);
856
857 /* align our pointer to the offset of the super block */
858 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
859
860 if (btrfs_super_bytenr(disk_super) != bytenr ||
Zach Browncdb4c572013-02-20 00:55:13 +0000861 disk_super->magic != cpu_to_le64(BTRFS_MAGIC))
David Sterba6f60cbd2013-02-15 11:31:02 -0700862 goto error_unmap;
863
Xiao Guangronga3438322010-01-06 11:48:18 +0000864 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400865 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400866 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700867
Stefan Behrensd03f9182012-11-05 13:10:49 +0000868 if (disk_super->label[0]) {
869 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
870 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Chris Masond3977122009-01-05 21:25:51 -0500871 printk(KERN_INFO "device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000872 } else {
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200873 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000874 }
David Sterba6f60cbd2013-02-15 11:31:02 -0700875
Roland Dreier119e10c2009-01-21 10:49:16 -0500876 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500877 (unsigned long long)devid, (unsigned long long)transid, path);
David Sterba6f60cbd2013-02-15 11:31:02 -0700878
Chris Mason8a4b83c2008-03-24 15:02:07 -0400879 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400880 if (!ret && fs_devices_ret)
881 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700882
883error_unmap:
884 kunmap(page);
885 page_cache_release(page);
886
887error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100888 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400889error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100890 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400891 return ret;
892}
Chris Mason0b86a832008-03-24 15:01:56 -0400893
Miao Xie6d07bce2011-01-05 10:07:31 +0000894/* helper to account the used device space in the range */
895int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
896 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400897{
898 struct btrfs_key key;
899 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000900 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500901 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000902 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400903 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000904 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400905 struct extent_buffer *l;
906
Miao Xie6d07bce2011-01-05 10:07:31 +0000907 *length = 0;
908
Stefan Behrens63a212a2012-11-05 18:29:28 +0100909 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000910 return 0;
911
Yan Zheng2b820322008-11-17 21:11:30 -0500912 path = btrfs_alloc_path();
913 if (!path)
914 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400915 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400916
Chris Mason0b86a832008-03-24 15:01:56 -0400917 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000918 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400919 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000920
921 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400922 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000923 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400924 if (ret > 0) {
925 ret = btrfs_previous_item(root, path, key.objectid, key.type);
926 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000927 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400928 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000929
Chris Mason0b86a832008-03-24 15:01:56 -0400930 while (1) {
931 l = path->nodes[0];
932 slot = path->slots[0];
933 if (slot >= btrfs_header_nritems(l)) {
934 ret = btrfs_next_leaf(root, path);
935 if (ret == 0)
936 continue;
937 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000938 goto out;
939
940 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400941 }
942 btrfs_item_key_to_cpu(l, &key, slot);
943
944 if (key.objectid < device->devid)
945 goto next;
946
947 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000948 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400949
Chris Masond3977122009-01-05 21:25:51 -0500950 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400951 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400952
Chris Mason0b86a832008-03-24 15:01:56 -0400953 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000954 extent_end = key.offset + btrfs_dev_extent_length(l,
955 dev_extent);
956 if (key.offset <= start && extent_end > end) {
957 *length = end - start + 1;
958 break;
959 } else if (key.offset <= start && extent_end > start)
960 *length += extent_end - start;
961 else if (key.offset > start && extent_end <= end)
962 *length += extent_end - key.offset;
963 else if (key.offset > start && key.offset <= end) {
964 *length += end - key.offset + 1;
965 break;
966 } else if (key.offset > end)
967 break;
968
969next:
970 path->slots[0]++;
971 }
972 ret = 0;
973out:
974 btrfs_free_path(path);
975 return ret;
976}
977
Chris Mason0b86a832008-03-24 15:01:56 -0400978/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000979 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000980 * @device: the device which we search the free space in
981 * @num_bytes: the size of the free space that we need
982 * @start: store the start of the free space.
983 * @len: the size of the free space. that we find, or the size of the max
984 * free space if we don't find suitable free space
985 *
Chris Mason0b86a832008-03-24 15:01:56 -0400986 * this uses a pretty simple search, the expectation is that it is
987 * called very infrequently and that a given device has a small number
988 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000989 *
990 * @start is used to store the start of the free space if we find. But if we
991 * don't find suitable free space, it will be used to store the start position
992 * of the max free space.
993 *
994 * @len is used to store the size of the free space that we find.
995 * But if we don't find suitable free space, it is used to store the size of
996 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400997 */
Li Zefan125ccb02011-12-08 15:07:24 +0800998int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000999 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001000{
1001 struct btrfs_key key;
1002 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001003 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001004 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001005 u64 hole_size;
1006 u64 max_hole_start;
1007 u64 max_hole_size;
1008 u64 extent_end;
1009 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001010 u64 search_end = device->total_bytes;
1011 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001012 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001013 struct extent_buffer *l;
1014
Chris Mason0b86a832008-03-24 15:01:56 -04001015 /* FIXME use last free of some kind */
1016
1017 /* we don't want to overwrite the superblock on the drive,
1018 * so we make sure to start at an offset of at least 1MB
1019 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001020 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001021
Miao Xie7bfc8372011-01-05 10:07:26 +00001022 max_hole_start = search_start;
1023 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001024 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001025
Stefan Behrens63a212a2012-11-05 18:29:28 +01001026 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001027 ret = -ENOSPC;
1028 goto error;
1029 }
1030
1031 path = btrfs_alloc_path();
1032 if (!path) {
1033 ret = -ENOMEM;
1034 goto error;
1035 }
1036 path->reada = 2;
1037
Chris Mason0b86a832008-03-24 15:01:56 -04001038 key.objectid = device->devid;
1039 key.offset = search_start;
1040 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001041
Li Zefan125ccb02011-12-08 15:07:24 +08001042 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001043 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001044 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001045 if (ret > 0) {
1046 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1047 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001048 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001049 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001050
Chris Mason0b86a832008-03-24 15:01:56 -04001051 while (1) {
1052 l = path->nodes[0];
1053 slot = path->slots[0];
1054 if (slot >= btrfs_header_nritems(l)) {
1055 ret = btrfs_next_leaf(root, path);
1056 if (ret == 0)
1057 continue;
1058 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001059 goto out;
1060
1061 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001062 }
1063 btrfs_item_key_to_cpu(l, &key, slot);
1064
1065 if (key.objectid < device->devid)
1066 goto next;
1067
1068 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001069 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001070
Chris Mason0b86a832008-03-24 15:01:56 -04001071 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1072 goto next;
1073
Miao Xie7bfc8372011-01-05 10:07:26 +00001074 if (key.offset > search_start) {
1075 hole_size = key.offset - search_start;
1076
1077 if (hole_size > max_hole_size) {
1078 max_hole_start = search_start;
1079 max_hole_size = hole_size;
1080 }
1081
1082 /*
1083 * If this free space is greater than which we need,
1084 * it must be the max free space that we have found
1085 * until now, so max_hole_start must point to the start
1086 * of this free space and the length of this free space
1087 * is stored in max_hole_size. Thus, we return
1088 * max_hole_start and max_hole_size and go back to the
1089 * caller.
1090 */
1091 if (hole_size >= num_bytes) {
1092 ret = 0;
1093 goto out;
1094 }
1095 }
1096
Chris Mason0b86a832008-03-24 15:01:56 -04001097 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001098 extent_end = key.offset + btrfs_dev_extent_length(l,
1099 dev_extent);
1100 if (extent_end > search_start)
1101 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001102next:
1103 path->slots[0]++;
1104 cond_resched();
1105 }
Chris Mason0b86a832008-03-24 15:01:56 -04001106
liubo38c01b92011-08-02 02:39:03 +00001107 /*
1108 * At this point, search_start should be the end of
1109 * allocated dev extents, and when shrinking the device,
1110 * search_end may be smaller than search_start.
1111 */
1112 if (search_end > search_start)
1113 hole_size = search_end - search_start;
1114
Miao Xie7bfc8372011-01-05 10:07:26 +00001115 if (hole_size > max_hole_size) {
1116 max_hole_start = search_start;
1117 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001118 }
Chris Mason0b86a832008-03-24 15:01:56 -04001119
Miao Xie7bfc8372011-01-05 10:07:26 +00001120 /* See above. */
1121 if (hole_size < num_bytes)
1122 ret = -ENOSPC;
1123 else
1124 ret = 0;
1125
1126out:
Yan Zheng2b820322008-11-17 21:11:30 -05001127 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001128error:
1129 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001130 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001131 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001132 return ret;
1133}
1134
Christoph Hellwigb2950862008-12-02 09:54:17 -05001135static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001136 struct btrfs_device *device,
1137 u64 start)
1138{
1139 int ret;
1140 struct btrfs_path *path;
1141 struct btrfs_root *root = device->dev_root;
1142 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001143 struct btrfs_key found_key;
1144 struct extent_buffer *leaf = NULL;
1145 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001146
1147 path = btrfs_alloc_path();
1148 if (!path)
1149 return -ENOMEM;
1150
1151 key.objectid = device->devid;
1152 key.offset = start;
1153 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001154again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001155 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001156 if (ret > 0) {
1157 ret = btrfs_previous_item(root, path, key.objectid,
1158 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001159 if (ret)
1160 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001161 leaf = path->nodes[0];
1162 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1163 extent = btrfs_item_ptr(leaf, path->slots[0],
1164 struct btrfs_dev_extent);
1165 BUG_ON(found_key.offset > start || found_key.offset +
1166 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001167 key = found_key;
1168 btrfs_release_path(path);
1169 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001170 } else if (ret == 0) {
1171 leaf = path->nodes[0];
1172 extent = btrfs_item_ptr(leaf, path->slots[0],
1173 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001174 } else {
1175 btrfs_error(root->fs_info, ret, "Slot search failed");
1176 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001177 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001178
Josef Bacik2bf64752011-09-26 17:12:22 -04001179 if (device->bytes_used > 0) {
1180 u64 len = btrfs_dev_extent_length(leaf, extent);
1181 device->bytes_used -= len;
1182 spin_lock(&root->fs_info->free_chunk_lock);
1183 root->fs_info->free_chunk_space += len;
1184 spin_unlock(&root->fs_info->free_chunk_lock);
1185 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001186 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001187 if (ret) {
1188 btrfs_error(root->fs_info, ret,
1189 "Failed to remove dev extent item");
1190 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001191out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001192 btrfs_free_path(path);
1193 return ret;
1194}
1195
Yan Zheng2b820322008-11-17 21:11:30 -05001196int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001197 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001198 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001199 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001200{
1201 int ret;
1202 struct btrfs_path *path;
1203 struct btrfs_root *root = device->dev_root;
1204 struct btrfs_dev_extent *extent;
1205 struct extent_buffer *leaf;
1206 struct btrfs_key key;
1207
Chris Masondfe25022008-05-13 13:46:40 -04001208 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001209 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001210 path = btrfs_alloc_path();
1211 if (!path)
1212 return -ENOMEM;
1213
Chris Mason0b86a832008-03-24 15:01:56 -04001214 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001215 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001216 key.type = BTRFS_DEV_EXTENT_KEY;
1217 ret = btrfs_insert_empty_item(trans, root, path, &key,
1218 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001219 if (ret)
1220 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001221
1222 leaf = path->nodes[0];
1223 extent = btrfs_item_ptr(leaf, path->slots[0],
1224 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001225 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1226 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1227 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1228
1229 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1230 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1231 BTRFS_UUID_SIZE);
1232
Chris Mason0b86a832008-03-24 15:01:56 -04001233 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1234 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001235out:
Chris Mason0b86a832008-03-24 15:01:56 -04001236 btrfs_free_path(path);
1237 return ret;
1238}
1239
Chris Masona1b32a52008-09-05 16:09:51 -04001240static noinline int find_next_chunk(struct btrfs_root *root,
1241 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001242{
1243 struct btrfs_path *path;
1244 int ret;
1245 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001246 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001247 struct btrfs_key found_key;
1248
1249 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001250 if (!path)
1251 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001252
Chris Masone17cade2008-04-15 15:41:47 -04001253 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001254 key.offset = (u64)-1;
1255 key.type = BTRFS_CHUNK_ITEM_KEY;
1256
1257 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1258 if (ret < 0)
1259 goto error;
1260
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001261 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001262
1263 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1264 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001265 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001266 } else {
1267 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1268 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001269 if (found_key.objectid != objectid)
1270 *offset = 0;
1271 else {
1272 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1273 struct btrfs_chunk);
1274 *offset = found_key.offset +
1275 btrfs_chunk_length(path->nodes[0], chunk);
1276 }
Chris Mason0b86a832008-03-24 15:01:56 -04001277 }
1278 ret = 0;
1279error:
1280 btrfs_free_path(path);
1281 return ret;
1282}
1283
Yan Zheng2b820322008-11-17 21:11:30 -05001284static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001285{
1286 int ret;
1287 struct btrfs_key key;
1288 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001289 struct btrfs_path *path;
1290
1291 root = root->fs_info->chunk_root;
1292
1293 path = btrfs_alloc_path();
1294 if (!path)
1295 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001296
1297 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1298 key.type = BTRFS_DEV_ITEM_KEY;
1299 key.offset = (u64)-1;
1300
1301 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1302 if (ret < 0)
1303 goto error;
1304
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001305 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001306
1307 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1308 BTRFS_DEV_ITEM_KEY);
1309 if (ret) {
1310 *objectid = 1;
1311 } else {
1312 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1313 path->slots[0]);
1314 *objectid = found_key.offset + 1;
1315 }
1316 ret = 0;
1317error:
Yan Zheng2b820322008-11-17 21:11:30 -05001318 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001319 return ret;
1320}
1321
1322/*
1323 * the device information is stored in the chunk root
1324 * the btrfs_device struct should be fully filled in
1325 */
1326int btrfs_add_device(struct btrfs_trans_handle *trans,
1327 struct btrfs_root *root,
1328 struct btrfs_device *device)
1329{
1330 int ret;
1331 struct btrfs_path *path;
1332 struct btrfs_dev_item *dev_item;
1333 struct extent_buffer *leaf;
1334 struct btrfs_key key;
1335 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001336
1337 root = root->fs_info->chunk_root;
1338
1339 path = btrfs_alloc_path();
1340 if (!path)
1341 return -ENOMEM;
1342
Chris Mason0b86a832008-03-24 15:01:56 -04001343 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1344 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001345 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001346
1347 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001348 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001349 if (ret)
1350 goto out;
1351
1352 leaf = path->nodes[0];
1353 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1354
1355 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001356 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001357 btrfs_set_device_type(leaf, dev_item, device->type);
1358 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1359 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1360 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001361 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1362 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001363 btrfs_set_device_group(leaf, dev_item, 0);
1364 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1365 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001366 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001367
Chris Mason0b86a832008-03-24 15:01:56 -04001368 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001369 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001370 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1371 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001372 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001373
Yan Zheng2b820322008-11-17 21:11:30 -05001374 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001375out:
1376 btrfs_free_path(path);
1377 return ret;
1378}
Chris Mason8f18cf12008-04-25 16:53:30 -04001379
Chris Masona061fc82008-05-07 11:43:44 -04001380static int btrfs_rm_dev_item(struct btrfs_root *root,
1381 struct btrfs_device *device)
1382{
1383 int ret;
1384 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001385 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001386 struct btrfs_trans_handle *trans;
1387
1388 root = root->fs_info->chunk_root;
1389
1390 path = btrfs_alloc_path();
1391 if (!path)
1392 return -ENOMEM;
1393
Yan, Zhenga22285a2010-05-16 10:48:46 -04001394 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001395 if (IS_ERR(trans)) {
1396 btrfs_free_path(path);
1397 return PTR_ERR(trans);
1398 }
Chris Masona061fc82008-05-07 11:43:44 -04001399 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1400 key.type = BTRFS_DEV_ITEM_KEY;
1401 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001402 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001403
1404 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1405 if (ret < 0)
1406 goto out;
1407
1408 if (ret > 0) {
1409 ret = -ENOENT;
1410 goto out;
1411 }
1412
1413 ret = btrfs_del_item(trans, root, path);
1414 if (ret)
1415 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001416out:
1417 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001418 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001419 btrfs_commit_transaction(trans, root);
1420 return ret;
1421}
1422
1423int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1424{
1425 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001426 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001427 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001428 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001429 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001430 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001431 u64 all_avail;
1432 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001433 u64 num_devices;
1434 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001435 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001436 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001437 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001438
Chris Masona061fc82008-05-07 11:43:44 -04001439 mutex_lock(&uuid_mutex);
1440
Miao Xiede98ced2013-01-29 10:13:12 +00001441 do {
1442 seq = read_seqbegin(&root->fs_info->profiles_lock);
1443
1444 all_avail = root->fs_info->avail_data_alloc_bits |
1445 root->fs_info->avail_system_alloc_bits |
1446 root->fs_info->avail_metadata_alloc_bits;
1447 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001448
Stefan Behrens8dabb742012-11-06 13:15:27 +01001449 num_devices = root->fs_info->fs_devices->num_devices;
1450 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1451 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1452 WARN_ON(num_devices < 1);
1453 num_devices--;
1454 }
1455 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1456
1457 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001458 printk(KERN_ERR "btrfs: unable to go below four devices "
1459 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001460 ret = -EINVAL;
1461 goto out;
1462 }
1463
Stefan Behrens8dabb742012-11-06 13:15:27 +01001464 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001465 printk(KERN_ERR "btrfs: unable to go below two "
1466 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001467 ret = -EINVAL;
1468 goto out;
1469 }
1470
David Woodhouse53b381b2013-01-29 18:40:14 -05001471 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1472 root->fs_info->fs_devices->rw_devices <= 2) {
1473 printk(KERN_ERR "btrfs: unable to go below two "
1474 "devices on raid5\n");
1475 ret = -EINVAL;
1476 goto out;
1477 }
1478 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1479 root->fs_info->fs_devices->rw_devices <= 3) {
1480 printk(KERN_ERR "btrfs: unable to go below three "
1481 "devices on raid6\n");
1482 ret = -EINVAL;
1483 goto out;
1484 }
1485
Chris Masondfe25022008-05-13 13:46:40 -04001486 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001487 struct list_head *devices;
1488 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001489
Chris Masondfe25022008-05-13 13:46:40 -04001490 device = NULL;
1491 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001492 /*
1493 * It is safe to read the devices since the volume_mutex
1494 * is held.
1495 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001496 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001497 if (tmp->in_fs_metadata &&
1498 !tmp->is_tgtdev_for_dev_replace &&
1499 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001500 device = tmp;
1501 break;
1502 }
1503 }
1504 bdev = NULL;
1505 bh = NULL;
1506 disk_super = NULL;
1507 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001508 printk(KERN_ERR "btrfs: no missing devices found to "
1509 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001510 goto out;
1511 }
Chris Masondfe25022008-05-13 13:46:40 -04001512 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001513 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001514 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001515 root->fs_info->bdev_holder, 0,
1516 &bdev, &bh);
1517 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001518 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001519 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001520 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001521 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001522 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001523 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001524 if (!device) {
1525 ret = -ENOENT;
1526 goto error_brelse;
1527 }
Chris Masondfe25022008-05-13 13:46:40 -04001528 }
Yan Zheng2b820322008-11-17 21:11:30 -05001529
Stefan Behrens63a212a2012-11-05 18:29:28 +01001530 if (device->is_tgtdev_for_dev_replace) {
1531 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1532 ret = -EINVAL;
1533 goto error_brelse;
1534 }
1535
Yan Zheng2b820322008-11-17 21:11:30 -05001536 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001537 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1538 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001539 ret = -EINVAL;
1540 goto error_brelse;
1541 }
1542
1543 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001544 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001545 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001546 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001547 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001548 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001549 }
Chris Masona061fc82008-05-07 11:43:44 -04001550
1551 ret = btrfs_shrink_device(device, 0);
1552 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001553 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001554
Stefan Behrens63a212a2012-11-05 18:29:28 +01001555 /*
1556 * TODO: the superblock still includes this device in its num_devices
1557 * counter although write_all_supers() is not locked out. This
1558 * could give a filesystem state which requires a degraded mount.
1559 */
Chris Masona061fc82008-05-07 11:43:44 -04001560 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1561 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001562 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001563
Josef Bacik2bf64752011-09-26 17:12:22 -04001564 spin_lock(&root->fs_info->free_chunk_lock);
1565 root->fs_info->free_chunk_space = device->total_bytes -
1566 device->bytes_used;
1567 spin_unlock(&root->fs_info->free_chunk_lock);
1568
Yan Zheng2b820322008-11-17 21:11:30 -05001569 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001570 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001571
1572 /*
1573 * the device list mutex makes sure that we don't change
1574 * the device list while someone else is writing out all
1575 * the device supers.
1576 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001577
1578 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001579 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001580 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001581
Yan Zhenge4404d62008-12-12 10:03:26 -05001582 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001583 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001584
Chris Masoncd02dca2010-12-13 14:56:23 -05001585 if (device->missing)
1586 root->fs_info->fs_devices->missing_devices--;
1587
Yan Zheng2b820322008-11-17 21:11:30 -05001588 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1589 struct btrfs_device, dev_list);
1590 if (device->bdev == root->fs_info->sb->s_bdev)
1591 root->fs_info->sb->s_bdev = next_device->bdev;
1592 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1593 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1594
Xiao Guangrong1f781602011-04-20 10:09:16 +00001595 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001596 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001597
1598 call_rcu(&device->rcu, free_device);
1599 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001600
David Sterba6c417612011-04-13 15:41:04 +02001601 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1602 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001603
Xiao Guangrong1f781602011-04-20 10:09:16 +00001604 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001605 struct btrfs_fs_devices *fs_devices;
1606 fs_devices = root->fs_info->fs_devices;
1607 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001608 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001609 break;
1610 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001611 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001612 fs_devices->seed = cur_devices->seed;
1613 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001614 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001615 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001616 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001617 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001618 }
1619
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001620 root->fs_info->num_tolerated_disk_barrier_failures =
1621 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1622
Yan Zheng2b820322008-11-17 21:11:30 -05001623 /*
1624 * at this point, the device is zero sized. We want to
1625 * remove it from the devices list and zero out the old super
1626 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001627 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001628 /* make sure this device isn't detected as part of
1629 * the FS anymore
1630 */
1631 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1632 set_buffer_dirty(bh);
1633 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001634 }
Chris Masona061fc82008-05-07 11:43:44 -04001635
Chris Masona061fc82008-05-07 11:43:44 -04001636 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001637
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001638 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001639 if (bdev)
1640 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001641
Chris Masona061fc82008-05-07 11:43:44 -04001642error_brelse:
1643 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001644 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001645 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001646out:
1647 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001648 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001649error_undo:
1650 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001651 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001652 list_add(&device->dev_alloc_list,
1653 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001654 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001655 root->fs_info->fs_devices->rw_devices++;
1656 }
1657 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001658}
1659
Stefan Behrense93c89c2012-11-05 17:33:06 +01001660void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1661 struct btrfs_device *srcdev)
1662{
1663 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1664 list_del_rcu(&srcdev->dev_list);
1665 list_del_rcu(&srcdev->dev_alloc_list);
1666 fs_info->fs_devices->num_devices--;
1667 if (srcdev->missing) {
1668 fs_info->fs_devices->missing_devices--;
1669 fs_info->fs_devices->rw_devices++;
1670 }
1671 if (srcdev->can_discard)
1672 fs_info->fs_devices->num_can_discard--;
1673 if (srcdev->bdev)
1674 fs_info->fs_devices->open_devices--;
1675
1676 call_rcu(&srcdev->rcu, free_device);
1677}
1678
1679void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1680 struct btrfs_device *tgtdev)
1681{
1682 struct btrfs_device *next_device;
1683
1684 WARN_ON(!tgtdev);
1685 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1686 if (tgtdev->bdev) {
1687 btrfs_scratch_superblock(tgtdev);
1688 fs_info->fs_devices->open_devices--;
1689 }
1690 fs_info->fs_devices->num_devices--;
1691 if (tgtdev->can_discard)
1692 fs_info->fs_devices->num_can_discard++;
1693
1694 next_device = list_entry(fs_info->fs_devices->devices.next,
1695 struct btrfs_device, dev_list);
1696 if (tgtdev->bdev == fs_info->sb->s_bdev)
1697 fs_info->sb->s_bdev = next_device->bdev;
1698 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1699 fs_info->fs_devices->latest_bdev = next_device->bdev;
1700 list_del_rcu(&tgtdev->dev_list);
1701
1702 call_rcu(&tgtdev->rcu, free_device);
1703
1704 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1705}
1706
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001707int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1708 struct btrfs_device **device)
1709{
1710 int ret = 0;
1711 struct btrfs_super_block *disk_super;
1712 u64 devid;
1713 u8 *dev_uuid;
1714 struct block_device *bdev;
1715 struct buffer_head *bh;
1716
1717 *device = NULL;
1718 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1719 root->fs_info->bdev_holder, 0, &bdev, &bh);
1720 if (ret)
1721 return ret;
1722 disk_super = (struct btrfs_super_block *)bh->b_data;
1723 devid = btrfs_stack_device_id(&disk_super->dev_item);
1724 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001725 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001726 disk_super->fsid);
1727 brelse(bh);
1728 if (!*device)
1729 ret = -ENOENT;
1730 blkdev_put(bdev, FMODE_READ);
1731 return ret;
1732}
1733
1734int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1735 char *device_path,
1736 struct btrfs_device **device)
1737{
1738 *device = NULL;
1739 if (strcmp(device_path, "missing") == 0) {
1740 struct list_head *devices;
1741 struct btrfs_device *tmp;
1742
1743 devices = &root->fs_info->fs_devices->devices;
1744 /*
1745 * It is safe to read the devices since the volume_mutex
1746 * is held by the caller.
1747 */
1748 list_for_each_entry(tmp, devices, dev_list) {
1749 if (tmp->in_fs_metadata && !tmp->bdev) {
1750 *device = tmp;
1751 break;
1752 }
1753 }
1754
1755 if (!*device) {
1756 pr_err("btrfs: no missing device found\n");
1757 return -ENOENT;
1758 }
1759
1760 return 0;
1761 } else {
1762 return btrfs_find_device_by_path(root, device_path, device);
1763 }
1764}
1765
Yan Zheng2b820322008-11-17 21:11:30 -05001766/*
1767 * does all the dirty work required for changing file system's UUID.
1768 */
Li Zefan125ccb02011-12-08 15:07:24 +08001769static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001770{
1771 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1772 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001773 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001774 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001775 struct btrfs_device *device;
1776 u64 super_flags;
1777
1778 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001779 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001780 return -EINVAL;
1781
Yan Zhenge4404d62008-12-12 10:03:26 -05001782 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1783 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001784 return -ENOMEM;
1785
Yan Zhenge4404d62008-12-12 10:03:26 -05001786 old_devices = clone_fs_devices(fs_devices);
1787 if (IS_ERR(old_devices)) {
1788 kfree(seed_devices);
1789 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001790 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001791
Yan Zheng2b820322008-11-17 21:11:30 -05001792 list_add(&old_devices->list, &fs_uuids);
1793
Yan Zhenge4404d62008-12-12 10:03:26 -05001794 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1795 seed_devices->opened = 1;
1796 INIT_LIST_HEAD(&seed_devices->devices);
1797 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001798 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001799
1800 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001801 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1802 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001803 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1804
Yan Zhenge4404d62008-12-12 10:03:26 -05001805 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1806 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1807 device->fs_devices = seed_devices;
1808 }
1809
Yan Zheng2b820322008-11-17 21:11:30 -05001810 fs_devices->seeding = 0;
1811 fs_devices->num_devices = 0;
1812 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001813 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001814 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001815
1816 generate_random_uuid(fs_devices->fsid);
1817 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1818 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1819 super_flags = btrfs_super_flags(disk_super) &
1820 ~BTRFS_SUPER_FLAG_SEEDING;
1821 btrfs_set_super_flags(disk_super, super_flags);
1822
1823 return 0;
1824}
1825
1826/*
1827 * strore the expected generation for seed devices in device items.
1828 */
1829static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1830 struct btrfs_root *root)
1831{
1832 struct btrfs_path *path;
1833 struct extent_buffer *leaf;
1834 struct btrfs_dev_item *dev_item;
1835 struct btrfs_device *device;
1836 struct btrfs_key key;
1837 u8 fs_uuid[BTRFS_UUID_SIZE];
1838 u8 dev_uuid[BTRFS_UUID_SIZE];
1839 u64 devid;
1840 int ret;
1841
1842 path = btrfs_alloc_path();
1843 if (!path)
1844 return -ENOMEM;
1845
1846 root = root->fs_info->chunk_root;
1847 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1848 key.offset = 0;
1849 key.type = BTRFS_DEV_ITEM_KEY;
1850
1851 while (1) {
1852 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1853 if (ret < 0)
1854 goto error;
1855
1856 leaf = path->nodes[0];
1857next_slot:
1858 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1859 ret = btrfs_next_leaf(root, path);
1860 if (ret > 0)
1861 break;
1862 if (ret < 0)
1863 goto error;
1864 leaf = path->nodes[0];
1865 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001866 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001867 continue;
1868 }
1869
1870 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1871 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1872 key.type != BTRFS_DEV_ITEM_KEY)
1873 break;
1874
1875 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1876 struct btrfs_dev_item);
1877 devid = btrfs_device_id(leaf, dev_item);
1878 read_extent_buffer(leaf, dev_uuid,
1879 (unsigned long)btrfs_device_uuid(dev_item),
1880 BTRFS_UUID_SIZE);
1881 read_extent_buffer(leaf, fs_uuid,
1882 (unsigned long)btrfs_device_fsid(dev_item),
1883 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001884 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1885 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001886 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001887
1888 if (device->fs_devices->seeding) {
1889 btrfs_set_device_generation(leaf, dev_item,
1890 device->generation);
1891 btrfs_mark_buffer_dirty(leaf);
1892 }
1893
1894 path->slots[0]++;
1895 goto next_slot;
1896 }
1897 ret = 0;
1898error:
1899 btrfs_free_path(path);
1900 return ret;
1901}
1902
Chris Mason788f20e2008-04-28 15:29:42 -04001903int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1904{
Josef Bacikd5e20032011-08-04 14:52:27 +00001905 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001906 struct btrfs_trans_handle *trans;
1907 struct btrfs_device *device;
1908 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001909 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001910 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001911 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001912 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001913 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001914 int ret = 0;
1915
Yan Zheng2b820322008-11-17 21:11:30 -05001916 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001917 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001918
Li Zefana5d16332011-12-07 20:08:40 -05001919 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001920 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001921 if (IS_ERR(bdev))
1922 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001923
Yan Zheng2b820322008-11-17 21:11:30 -05001924 if (root->fs_info->fs_devices->seeding) {
1925 seeding_dev = 1;
1926 down_write(&sb->s_umount);
1927 mutex_lock(&uuid_mutex);
1928 }
1929
Chris Mason8c8bee12008-09-29 11:19:10 -04001930 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001931
Chris Mason788f20e2008-04-28 15:29:42 -04001932 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001933
1934 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001935 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001936 if (device->bdev == bdev) {
1937 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001938 mutex_unlock(
1939 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001940 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001941 }
1942 }
Liu Bod25628b2012-11-14 14:35:30 +00001943 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001944
1945 device = kzalloc(sizeof(*device), GFP_NOFS);
1946 if (!device) {
1947 /* we can safely leave the fs_devices entry around */
1948 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001949 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001950 }
1951
Josef Bacik606686e2012-06-04 14:03:51 -04001952 name = rcu_string_strdup(device_path, GFP_NOFS);
1953 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001954 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001955 ret = -ENOMEM;
1956 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001957 }
Josef Bacik606686e2012-06-04 14:03:51 -04001958 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001959
1960 ret = find_next_devid(root, &device->devid);
1961 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001962 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001963 kfree(device);
1964 goto error;
1965 }
1966
Yan, Zhenga22285a2010-05-16 10:48:46 -04001967 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001968 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001969 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001970 kfree(device);
1971 ret = PTR_ERR(trans);
1972 goto error;
1973 }
1974
Yan Zheng2b820322008-11-17 21:11:30 -05001975 lock_chunks(root);
1976
Josef Bacikd5e20032011-08-04 14:52:27 +00001977 q = bdev_get_queue(bdev);
1978 if (blk_queue_discard(q))
1979 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001980 device->writeable = 1;
1981 device->work.func = pending_bios_fn;
1982 generate_random_uuid(device->uuid);
1983 spin_lock_init(&device->io_lock);
1984 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001985 device->io_width = root->sectorsize;
1986 device->io_align = root->sectorsize;
1987 device->sector_size = root->sectorsize;
1988 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001989 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001990 device->dev_root = root->fs_info->dev_root;
1991 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001992 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001993 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001994 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001995 set_blocksize(device->bdev, 4096);
1996
Yan Zheng2b820322008-11-17 21:11:30 -05001997 if (seeding_dev) {
1998 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001999 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002000 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002001 }
2002
2003 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002004
Chris Masone5e9a522009-06-10 15:17:02 -04002005 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002006 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002007 list_add(&device->dev_alloc_list,
2008 &root->fs_info->fs_devices->alloc_list);
2009 root->fs_info->fs_devices->num_devices++;
2010 root->fs_info->fs_devices->open_devices++;
2011 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002012 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002013 if (device->can_discard)
2014 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002015 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2016
Josef Bacik2bf64752011-09-26 17:12:22 -04002017 spin_lock(&root->fs_info->free_chunk_lock);
2018 root->fs_info->free_chunk_space += device->total_bytes;
2019 spin_unlock(&root->fs_info->free_chunk_lock);
2020
Chris Masonc2898112009-06-10 09:51:32 -04002021 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2022 root->fs_info->fs_devices->rotating = 1;
2023
David Sterba6c417612011-04-13 15:41:04 +02002024 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2025 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002026 total_bytes + device->total_bytes);
2027
David Sterba6c417612011-04-13 15:41:04 +02002028 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2029 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002030 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04002031 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002032
Yan Zheng2b820322008-11-17 21:11:30 -05002033 if (seeding_dev) {
2034 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002035 if (ret) {
2036 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002037 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002038 }
Yan Zheng2b820322008-11-17 21:11:30 -05002039 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002040 if (ret) {
2041 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002042 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002043 }
Yan Zheng2b820322008-11-17 21:11:30 -05002044 } else {
2045 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002046 if (ret) {
2047 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002048 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002049 }
Yan Zheng2b820322008-11-17 21:11:30 -05002050 }
2051
Chris Mason913d9522009-03-10 13:17:18 -04002052 /*
2053 * we've got more storage, clear any full flags on the space
2054 * infos
2055 */
2056 btrfs_clear_space_info_full(root->fs_info);
2057
Chris Mason7d9eb122008-07-08 14:19:17 -04002058 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002059 root->fs_info->num_tolerated_disk_barrier_failures =
2060 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002061 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002062
2063 if (seeding_dev) {
2064 mutex_unlock(&uuid_mutex);
2065 up_write(&sb->s_umount);
2066
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002067 if (ret) /* transaction commit */
2068 return ret;
2069
Yan Zheng2b820322008-11-17 21:11:30 -05002070 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002071 if (ret < 0)
2072 btrfs_error(root->fs_info, ret,
2073 "Failed to relocate sys chunks after "
2074 "device initialization. This can be fixed "
2075 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002076 trans = btrfs_attach_transaction(root);
2077 if (IS_ERR(trans)) {
2078 if (PTR_ERR(trans) == -ENOENT)
2079 return 0;
2080 return PTR_ERR(trans);
2081 }
2082 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002083 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002084
Chris Mason788f20e2008-04-28 15:29:42 -04002085 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002086
2087error_trans:
2088 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002089 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002090 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002091 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002092error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002093 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002094 if (seeding_dev) {
2095 mutex_unlock(&uuid_mutex);
2096 up_write(&sb->s_umount);
2097 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002098 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002099}
2100
Stefan Behrense93c89c2012-11-05 17:33:06 +01002101int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2102 struct btrfs_device **device_out)
2103{
2104 struct request_queue *q;
2105 struct btrfs_device *device;
2106 struct block_device *bdev;
2107 struct btrfs_fs_info *fs_info = root->fs_info;
2108 struct list_head *devices;
2109 struct rcu_string *name;
2110 int ret = 0;
2111
2112 *device_out = NULL;
2113 if (fs_info->fs_devices->seeding)
2114 return -EINVAL;
2115
2116 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2117 fs_info->bdev_holder);
2118 if (IS_ERR(bdev))
2119 return PTR_ERR(bdev);
2120
2121 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2122
2123 devices = &fs_info->fs_devices->devices;
2124 list_for_each_entry(device, devices, dev_list) {
2125 if (device->bdev == bdev) {
2126 ret = -EEXIST;
2127 goto error;
2128 }
2129 }
2130
2131 device = kzalloc(sizeof(*device), GFP_NOFS);
2132 if (!device) {
2133 ret = -ENOMEM;
2134 goto error;
2135 }
2136
2137 name = rcu_string_strdup(device_path, GFP_NOFS);
2138 if (!name) {
2139 kfree(device);
2140 ret = -ENOMEM;
2141 goto error;
2142 }
2143 rcu_assign_pointer(device->name, name);
2144
2145 q = bdev_get_queue(bdev);
2146 if (blk_queue_discard(q))
2147 device->can_discard = 1;
2148 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2149 device->writeable = 1;
2150 device->work.func = pending_bios_fn;
2151 generate_random_uuid(device->uuid);
2152 device->devid = BTRFS_DEV_REPLACE_DEVID;
2153 spin_lock_init(&device->io_lock);
2154 device->generation = 0;
2155 device->io_width = root->sectorsize;
2156 device->io_align = root->sectorsize;
2157 device->sector_size = root->sectorsize;
2158 device->total_bytes = i_size_read(bdev->bd_inode);
2159 device->disk_total_bytes = device->total_bytes;
2160 device->dev_root = fs_info->dev_root;
2161 device->bdev = bdev;
2162 device->in_fs_metadata = 1;
2163 device->is_tgtdev_for_dev_replace = 1;
2164 device->mode = FMODE_EXCL;
2165 set_blocksize(device->bdev, 4096);
2166 device->fs_devices = fs_info->fs_devices;
2167 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2168 fs_info->fs_devices->num_devices++;
2169 fs_info->fs_devices->open_devices++;
2170 if (device->can_discard)
2171 fs_info->fs_devices->num_can_discard++;
2172 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2173
2174 *device_out = device;
2175 return ret;
2176
2177error:
2178 blkdev_put(bdev, FMODE_EXCL);
2179 return ret;
2180}
2181
2182void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2183 struct btrfs_device *tgtdev)
2184{
2185 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2186 tgtdev->io_width = fs_info->dev_root->sectorsize;
2187 tgtdev->io_align = fs_info->dev_root->sectorsize;
2188 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2189 tgtdev->dev_root = fs_info->dev_root;
2190 tgtdev->in_fs_metadata = 1;
2191}
2192
Chris Masond3977122009-01-05 21:25:51 -05002193static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2194 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002195{
2196 int ret;
2197 struct btrfs_path *path;
2198 struct btrfs_root *root;
2199 struct btrfs_dev_item *dev_item;
2200 struct extent_buffer *leaf;
2201 struct btrfs_key key;
2202
2203 root = device->dev_root->fs_info->chunk_root;
2204
2205 path = btrfs_alloc_path();
2206 if (!path)
2207 return -ENOMEM;
2208
2209 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2210 key.type = BTRFS_DEV_ITEM_KEY;
2211 key.offset = device->devid;
2212
2213 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2214 if (ret < 0)
2215 goto out;
2216
2217 if (ret > 0) {
2218 ret = -ENOENT;
2219 goto out;
2220 }
2221
2222 leaf = path->nodes[0];
2223 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2224
2225 btrfs_set_device_id(leaf, dev_item, device->devid);
2226 btrfs_set_device_type(leaf, dev_item, device->type);
2227 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2228 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2229 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002230 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002231 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2232 btrfs_mark_buffer_dirty(leaf);
2233
2234out:
2235 btrfs_free_path(path);
2236 return ret;
2237}
2238
Chris Mason7d9eb122008-07-08 14:19:17 -04002239static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002240 struct btrfs_device *device, u64 new_size)
2241{
2242 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002243 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002244 u64 old_total = btrfs_super_total_bytes(super_copy);
2245 u64 diff = new_size - device->total_bytes;
2246
Yan Zheng2b820322008-11-17 21:11:30 -05002247 if (!device->writeable)
2248 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002249 if (new_size <= device->total_bytes ||
2250 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002251 return -EINVAL;
2252
Chris Mason8f18cf12008-04-25 16:53:30 -04002253 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002254 device->fs_devices->total_rw_bytes += diff;
2255
2256 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002257 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002258 btrfs_clear_space_info_full(device->dev_root->fs_info);
2259
Chris Mason8f18cf12008-04-25 16:53:30 -04002260 return btrfs_update_device(trans, device);
2261}
2262
Chris Mason7d9eb122008-07-08 14:19:17 -04002263int btrfs_grow_device(struct btrfs_trans_handle *trans,
2264 struct btrfs_device *device, u64 new_size)
2265{
2266 int ret;
2267 lock_chunks(device->dev_root);
2268 ret = __btrfs_grow_device(trans, device, new_size);
2269 unlock_chunks(device->dev_root);
2270 return ret;
2271}
2272
Chris Mason8f18cf12008-04-25 16:53:30 -04002273static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2274 struct btrfs_root *root,
2275 u64 chunk_tree, u64 chunk_objectid,
2276 u64 chunk_offset)
2277{
2278 int ret;
2279 struct btrfs_path *path;
2280 struct btrfs_key key;
2281
2282 root = root->fs_info->chunk_root;
2283 path = btrfs_alloc_path();
2284 if (!path)
2285 return -ENOMEM;
2286
2287 key.objectid = chunk_objectid;
2288 key.offset = chunk_offset;
2289 key.type = BTRFS_CHUNK_ITEM_KEY;
2290
2291 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002292 if (ret < 0)
2293 goto out;
2294 else if (ret > 0) { /* Logic error or corruption */
2295 btrfs_error(root->fs_info, -ENOENT,
2296 "Failed lookup while freeing chunk.");
2297 ret = -ENOENT;
2298 goto out;
2299 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002300
2301 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002302 if (ret < 0)
2303 btrfs_error(root->fs_info, ret,
2304 "Failed to delete chunk item.");
2305out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002306 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002307 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002308}
2309
Christoph Hellwigb2950862008-12-02 09:54:17 -05002310static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002311 chunk_offset)
2312{
David Sterba6c417612011-04-13 15:41:04 +02002313 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002314 struct btrfs_disk_key *disk_key;
2315 struct btrfs_chunk *chunk;
2316 u8 *ptr;
2317 int ret = 0;
2318 u32 num_stripes;
2319 u32 array_size;
2320 u32 len = 0;
2321 u32 cur;
2322 struct btrfs_key key;
2323
2324 array_size = btrfs_super_sys_array_size(super_copy);
2325
2326 ptr = super_copy->sys_chunk_array;
2327 cur = 0;
2328
2329 while (cur < array_size) {
2330 disk_key = (struct btrfs_disk_key *)ptr;
2331 btrfs_disk_key_to_cpu(&key, disk_key);
2332
2333 len = sizeof(*disk_key);
2334
2335 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2336 chunk = (struct btrfs_chunk *)(ptr + len);
2337 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2338 len += btrfs_chunk_item_size(num_stripes);
2339 } else {
2340 ret = -EIO;
2341 break;
2342 }
2343 if (key.objectid == chunk_objectid &&
2344 key.offset == chunk_offset) {
2345 memmove(ptr, ptr + len, array_size - (cur + len));
2346 array_size -= len;
2347 btrfs_set_super_sys_array_size(super_copy, array_size);
2348 } else {
2349 ptr += len;
2350 cur += len;
2351 }
2352 }
2353 return ret;
2354}
2355
Christoph Hellwigb2950862008-12-02 09:54:17 -05002356static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002357 u64 chunk_tree, u64 chunk_objectid,
2358 u64 chunk_offset)
2359{
2360 struct extent_map_tree *em_tree;
2361 struct btrfs_root *extent_root;
2362 struct btrfs_trans_handle *trans;
2363 struct extent_map *em;
2364 struct map_lookup *map;
2365 int ret;
2366 int i;
2367
2368 root = root->fs_info->chunk_root;
2369 extent_root = root->fs_info->extent_root;
2370 em_tree = &root->fs_info->mapping_tree.map_tree;
2371
Josef Bacikba1bf482009-09-11 16:11:19 -04002372 ret = btrfs_can_relocate(extent_root, chunk_offset);
2373 if (ret)
2374 return -ENOSPC;
2375
Chris Mason8f18cf12008-04-25 16:53:30 -04002376 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002377 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002378 if (ret)
2379 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002380
Yan, Zhenga22285a2010-05-16 10:48:46 -04002381 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002382 if (IS_ERR(trans)) {
2383 ret = PTR_ERR(trans);
2384 btrfs_std_error(root->fs_info, ret);
2385 return ret;
2386 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002387
Chris Mason7d9eb122008-07-08 14:19:17 -04002388 lock_chunks(root);
2389
Chris Mason8f18cf12008-04-25 16:53:30 -04002390 /*
2391 * step two, delete the device extents and the
2392 * chunk tree entries
2393 */
Chris Mason890871b2009-09-02 16:24:52 -04002394 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002395 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002396 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002397
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002398 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002399 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002400 map = (struct map_lookup *)em->bdev;
2401
2402 for (i = 0; i < map->num_stripes; i++) {
2403 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2404 map->stripes[i].physical);
2405 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002406
Chris Masondfe25022008-05-13 13:46:40 -04002407 if (map->stripes[i].dev) {
2408 ret = btrfs_update_device(trans, map->stripes[i].dev);
2409 BUG_ON(ret);
2410 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002411 }
2412 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2413 chunk_offset);
2414
2415 BUG_ON(ret);
2416
liubo1abe9b82011-03-24 11:18:59 +00002417 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2418
Chris Mason8f18cf12008-04-25 16:53:30 -04002419 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2420 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2421 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002422 }
2423
Zheng Yan1a40e232008-09-26 10:09:34 -04002424 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2425 BUG_ON(ret);
2426
Chris Mason890871b2009-09-02 16:24:52 -04002427 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002428 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002429 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002430
Chris Mason8f18cf12008-04-25 16:53:30 -04002431 kfree(map);
2432 em->bdev = NULL;
2433
2434 /* once for the tree */
2435 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002436 /* once for us */
2437 free_extent_map(em);
2438
Chris Mason7d9eb122008-07-08 14:19:17 -04002439 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002440 btrfs_end_transaction(trans, root);
2441 return 0;
2442}
2443
Yan Zheng2b820322008-11-17 21:11:30 -05002444static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2445{
2446 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2447 struct btrfs_path *path;
2448 struct extent_buffer *leaf;
2449 struct btrfs_chunk *chunk;
2450 struct btrfs_key key;
2451 struct btrfs_key found_key;
2452 u64 chunk_tree = chunk_root->root_key.objectid;
2453 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002454 bool retried = false;
2455 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002456 int ret;
2457
2458 path = btrfs_alloc_path();
2459 if (!path)
2460 return -ENOMEM;
2461
Josef Bacikba1bf482009-09-11 16:11:19 -04002462again:
Yan Zheng2b820322008-11-17 21:11:30 -05002463 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2464 key.offset = (u64)-1;
2465 key.type = BTRFS_CHUNK_ITEM_KEY;
2466
2467 while (1) {
2468 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2469 if (ret < 0)
2470 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002471 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002472
2473 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2474 key.type);
2475 if (ret < 0)
2476 goto error;
2477 if (ret > 0)
2478 break;
2479
2480 leaf = path->nodes[0];
2481 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2482
2483 chunk = btrfs_item_ptr(leaf, path->slots[0],
2484 struct btrfs_chunk);
2485 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002486 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002487
2488 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2489 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2490 found_key.objectid,
2491 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002492 if (ret == -ENOSPC)
2493 failed++;
2494 else if (ret)
2495 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002496 }
2497
2498 if (found_key.offset == 0)
2499 break;
2500 key.offset = found_key.offset - 1;
2501 }
2502 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002503 if (failed && !retried) {
2504 failed = 0;
2505 retried = true;
2506 goto again;
2507 } else if (failed && retried) {
2508 WARN_ON(1);
2509 ret = -ENOSPC;
2510 }
Yan Zheng2b820322008-11-17 21:11:30 -05002511error:
2512 btrfs_free_path(path);
2513 return ret;
2514}
2515
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002516static int insert_balance_item(struct btrfs_root *root,
2517 struct btrfs_balance_control *bctl)
2518{
2519 struct btrfs_trans_handle *trans;
2520 struct btrfs_balance_item *item;
2521 struct btrfs_disk_balance_args disk_bargs;
2522 struct btrfs_path *path;
2523 struct extent_buffer *leaf;
2524 struct btrfs_key key;
2525 int ret, err;
2526
2527 path = btrfs_alloc_path();
2528 if (!path)
2529 return -ENOMEM;
2530
2531 trans = btrfs_start_transaction(root, 0);
2532 if (IS_ERR(trans)) {
2533 btrfs_free_path(path);
2534 return PTR_ERR(trans);
2535 }
2536
2537 key.objectid = BTRFS_BALANCE_OBJECTID;
2538 key.type = BTRFS_BALANCE_ITEM_KEY;
2539 key.offset = 0;
2540
2541 ret = btrfs_insert_empty_item(trans, root, path, &key,
2542 sizeof(*item));
2543 if (ret)
2544 goto out;
2545
2546 leaf = path->nodes[0];
2547 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2548
2549 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2550
2551 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2552 btrfs_set_balance_data(leaf, item, &disk_bargs);
2553 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2554 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2555 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2556 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2557
2558 btrfs_set_balance_flags(leaf, item, bctl->flags);
2559
2560 btrfs_mark_buffer_dirty(leaf);
2561out:
2562 btrfs_free_path(path);
2563 err = btrfs_commit_transaction(trans, root);
2564 if (err && !ret)
2565 ret = err;
2566 return ret;
2567}
2568
2569static int del_balance_item(struct btrfs_root *root)
2570{
2571 struct btrfs_trans_handle *trans;
2572 struct btrfs_path *path;
2573 struct btrfs_key key;
2574 int ret, err;
2575
2576 path = btrfs_alloc_path();
2577 if (!path)
2578 return -ENOMEM;
2579
2580 trans = btrfs_start_transaction(root, 0);
2581 if (IS_ERR(trans)) {
2582 btrfs_free_path(path);
2583 return PTR_ERR(trans);
2584 }
2585
2586 key.objectid = BTRFS_BALANCE_OBJECTID;
2587 key.type = BTRFS_BALANCE_ITEM_KEY;
2588 key.offset = 0;
2589
2590 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2591 if (ret < 0)
2592 goto out;
2593 if (ret > 0) {
2594 ret = -ENOENT;
2595 goto out;
2596 }
2597
2598 ret = btrfs_del_item(trans, root, path);
2599out:
2600 btrfs_free_path(path);
2601 err = btrfs_commit_transaction(trans, root);
2602 if (err && !ret)
2603 ret = err;
2604 return ret;
2605}
2606
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002607/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002608 * This is a heuristic used to reduce the number of chunks balanced on
2609 * resume after balance was interrupted.
2610 */
2611static void update_balance_args(struct btrfs_balance_control *bctl)
2612{
2613 /*
2614 * Turn on soft mode for chunk types that were being converted.
2615 */
2616 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2617 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2618 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2619 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2620 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2621 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2622
2623 /*
2624 * Turn on usage filter if is not already used. The idea is
2625 * that chunks that we have already balanced should be
2626 * reasonably full. Don't do it for chunks that are being
2627 * converted - that will keep us from relocating unconverted
2628 * (albeit full) chunks.
2629 */
2630 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2631 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2632 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2633 bctl->data.usage = 90;
2634 }
2635 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2636 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2637 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2638 bctl->sys.usage = 90;
2639 }
2640 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2641 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2642 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2643 bctl->meta.usage = 90;
2644 }
2645}
2646
2647/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002648 * Should be called with both balance and volume mutexes held to
2649 * serialize other volume operations (add_dev/rm_dev/resize) with
2650 * restriper. Same goes for unset_balance_control.
2651 */
2652static void set_balance_control(struct btrfs_balance_control *bctl)
2653{
2654 struct btrfs_fs_info *fs_info = bctl->fs_info;
2655
2656 BUG_ON(fs_info->balance_ctl);
2657
2658 spin_lock(&fs_info->balance_lock);
2659 fs_info->balance_ctl = bctl;
2660 spin_unlock(&fs_info->balance_lock);
2661}
2662
2663static void unset_balance_control(struct btrfs_fs_info *fs_info)
2664{
2665 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2666
2667 BUG_ON(!fs_info->balance_ctl);
2668
2669 spin_lock(&fs_info->balance_lock);
2670 fs_info->balance_ctl = NULL;
2671 spin_unlock(&fs_info->balance_lock);
2672
2673 kfree(bctl);
2674}
2675
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002676/*
2677 * Balance filters. Return 1 if chunk should be filtered out
2678 * (should not be balanced).
2679 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002680static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002681 struct btrfs_balance_args *bargs)
2682{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002683 chunk_type = chunk_to_extended(chunk_type) &
2684 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002685
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002686 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002687 return 0;
2688
2689 return 1;
2690}
2691
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002692static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2693 struct btrfs_balance_args *bargs)
2694{
2695 struct btrfs_block_group_cache *cache;
2696 u64 chunk_used, user_thresh;
2697 int ret = 1;
2698
2699 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2700 chunk_used = btrfs_block_group_used(&cache->item);
2701
Ilya Dryomova105bb82013-01-21 15:15:56 +02002702 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002703 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002704 else if (bargs->usage > 100)
2705 user_thresh = cache->key.offset;
2706 else
2707 user_thresh = div_factor_fine(cache->key.offset,
2708 bargs->usage);
2709
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002710 if (chunk_used < user_thresh)
2711 ret = 0;
2712
2713 btrfs_put_block_group(cache);
2714 return ret;
2715}
2716
Ilya Dryomov409d4042012-01-16 22:04:47 +02002717static int chunk_devid_filter(struct extent_buffer *leaf,
2718 struct btrfs_chunk *chunk,
2719 struct btrfs_balance_args *bargs)
2720{
2721 struct btrfs_stripe *stripe;
2722 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2723 int i;
2724
2725 for (i = 0; i < num_stripes; i++) {
2726 stripe = btrfs_stripe_nr(chunk, i);
2727 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2728 return 0;
2729 }
2730
2731 return 1;
2732}
2733
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002734/* [pstart, pend) */
2735static int chunk_drange_filter(struct extent_buffer *leaf,
2736 struct btrfs_chunk *chunk,
2737 u64 chunk_offset,
2738 struct btrfs_balance_args *bargs)
2739{
2740 struct btrfs_stripe *stripe;
2741 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2742 u64 stripe_offset;
2743 u64 stripe_length;
2744 int factor;
2745 int i;
2746
2747 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2748 return 0;
2749
2750 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002751 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2752 factor = num_stripes / 2;
2753 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2754 factor = num_stripes - 1;
2755 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2756 factor = num_stripes - 2;
2757 } else {
2758 factor = num_stripes;
2759 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002760
2761 for (i = 0; i < num_stripes; i++) {
2762 stripe = btrfs_stripe_nr(chunk, i);
2763 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2764 continue;
2765
2766 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2767 stripe_length = btrfs_chunk_length(leaf, chunk);
2768 do_div(stripe_length, factor);
2769
2770 if (stripe_offset < bargs->pend &&
2771 stripe_offset + stripe_length > bargs->pstart)
2772 return 0;
2773 }
2774
2775 return 1;
2776}
2777
Ilya Dryomovea671762012-01-16 22:04:48 +02002778/* [vstart, vend) */
2779static int chunk_vrange_filter(struct extent_buffer *leaf,
2780 struct btrfs_chunk *chunk,
2781 u64 chunk_offset,
2782 struct btrfs_balance_args *bargs)
2783{
2784 if (chunk_offset < bargs->vend &&
2785 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2786 /* at least part of the chunk is inside this vrange */
2787 return 0;
2788
2789 return 1;
2790}
2791
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002792static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002793 struct btrfs_balance_args *bargs)
2794{
2795 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2796 return 0;
2797
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002798 chunk_type = chunk_to_extended(chunk_type) &
2799 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002800
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002801 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002802 return 1;
2803
2804 return 0;
2805}
2806
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002807static int should_balance_chunk(struct btrfs_root *root,
2808 struct extent_buffer *leaf,
2809 struct btrfs_chunk *chunk, u64 chunk_offset)
2810{
2811 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2812 struct btrfs_balance_args *bargs = NULL;
2813 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2814
2815 /* type filter */
2816 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2817 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2818 return 0;
2819 }
2820
2821 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2822 bargs = &bctl->data;
2823 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2824 bargs = &bctl->sys;
2825 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2826 bargs = &bctl->meta;
2827
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002828 /* profiles filter */
2829 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2830 chunk_profiles_filter(chunk_type, bargs)) {
2831 return 0;
2832 }
2833
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002834 /* usage filter */
2835 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2836 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2837 return 0;
2838 }
2839
Ilya Dryomov409d4042012-01-16 22:04:47 +02002840 /* devid filter */
2841 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2842 chunk_devid_filter(leaf, chunk, bargs)) {
2843 return 0;
2844 }
2845
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002846 /* drange filter, makes sense only with devid filter */
2847 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2848 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2849 return 0;
2850 }
2851
Ilya Dryomovea671762012-01-16 22:04:48 +02002852 /* vrange filter */
2853 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2854 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2855 return 0;
2856 }
2857
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002858 /* soft profile changing mode */
2859 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2860 chunk_soft_convert_filter(chunk_type, bargs)) {
2861 return 0;
2862 }
2863
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002864 return 1;
2865}
2866
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002867static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002868{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002869 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002870 struct btrfs_root *chunk_root = fs_info->chunk_root;
2871 struct btrfs_root *dev_root = fs_info->dev_root;
2872 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002873 struct btrfs_device *device;
2874 u64 old_size;
2875 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002876 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002877 struct btrfs_path *path;
2878 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002879 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002880 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002881 struct extent_buffer *leaf;
2882 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002883 int ret;
2884 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002885 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002886
Chris Masonec44a352008-04-28 15:29:52 -04002887 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002888 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002889 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002890 old_size = device->total_bytes;
2891 size_to_free = div_factor(old_size, 1);
2892 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002893 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002894 device->total_bytes - device->bytes_used > size_to_free ||
2895 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002896 continue;
2897
2898 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002899 if (ret == -ENOSPC)
2900 break;
Chris Masonec44a352008-04-28 15:29:52 -04002901 BUG_ON(ret);
2902
Yan, Zhenga22285a2010-05-16 10:48:46 -04002903 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002904 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002905
2906 ret = btrfs_grow_device(trans, device, old_size);
2907 BUG_ON(ret);
2908
2909 btrfs_end_transaction(trans, dev_root);
2910 }
2911
2912 /* step two, relocate all the chunks */
2913 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002914 if (!path) {
2915 ret = -ENOMEM;
2916 goto error;
2917 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002918
2919 /* zero out stat counters */
2920 spin_lock(&fs_info->balance_lock);
2921 memset(&bctl->stat, 0, sizeof(bctl->stat));
2922 spin_unlock(&fs_info->balance_lock);
2923again:
Chris Masonec44a352008-04-28 15:29:52 -04002924 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2925 key.offset = (u64)-1;
2926 key.type = BTRFS_CHUNK_ITEM_KEY;
2927
Chris Masond3977122009-01-05 21:25:51 -05002928 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002929 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002930 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002931 ret = -ECANCELED;
2932 goto error;
2933 }
2934
Chris Masonec44a352008-04-28 15:29:52 -04002935 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2936 if (ret < 0)
2937 goto error;
2938
2939 /*
2940 * this shouldn't happen, it means the last relocate
2941 * failed
2942 */
2943 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002944 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002945
2946 ret = btrfs_previous_item(chunk_root, path, 0,
2947 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002948 if (ret) {
2949 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002950 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002951 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002952
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002953 leaf = path->nodes[0];
2954 slot = path->slots[0];
2955 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2956
Chris Masonec44a352008-04-28 15:29:52 -04002957 if (found_key.objectid != key.objectid)
2958 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002959
Chris Masonec44a352008-04-28 15:29:52 -04002960 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002961 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002962 break;
2963
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002964 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2965
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002966 if (!counting) {
2967 spin_lock(&fs_info->balance_lock);
2968 bctl->stat.considered++;
2969 spin_unlock(&fs_info->balance_lock);
2970 }
2971
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002972 ret = should_balance_chunk(chunk_root, leaf, chunk,
2973 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002974 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002975 if (!ret)
2976 goto loop;
2977
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002978 if (counting) {
2979 spin_lock(&fs_info->balance_lock);
2980 bctl->stat.expected++;
2981 spin_unlock(&fs_info->balance_lock);
2982 goto loop;
2983 }
2984
Chris Masonec44a352008-04-28 15:29:52 -04002985 ret = btrfs_relocate_chunk(chunk_root,
2986 chunk_root->root_key.objectid,
2987 found_key.objectid,
2988 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002989 if (ret && ret != -ENOSPC)
2990 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002991 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002992 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002993 } else {
2994 spin_lock(&fs_info->balance_lock);
2995 bctl->stat.completed++;
2996 spin_unlock(&fs_info->balance_lock);
2997 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002998loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002999 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003000 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003001
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003002 if (counting) {
3003 btrfs_release_path(path);
3004 counting = false;
3005 goto again;
3006 }
Chris Masonec44a352008-04-28 15:29:52 -04003007error:
3008 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003009 if (enospc_errors) {
3010 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
3011 enospc_errors);
3012 if (!ret)
3013 ret = -ENOSPC;
3014 }
3015
Chris Masonec44a352008-04-28 15:29:52 -04003016 return ret;
3017}
3018
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003019/**
3020 * alloc_profile_is_valid - see if a given profile is valid and reduced
3021 * @flags: profile to validate
3022 * @extended: if true @flags is treated as an extended profile
3023 */
3024static int alloc_profile_is_valid(u64 flags, int extended)
3025{
3026 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3027 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3028
3029 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3030
3031 /* 1) check that all other bits are zeroed */
3032 if (flags & ~mask)
3033 return 0;
3034
3035 /* 2) see if profile is reduced */
3036 if (flags == 0)
3037 return !extended; /* "0" is valid for usual profiles */
3038
3039 /* true if exactly one bit set */
3040 return (flags & (flags - 1)) == 0;
3041}
3042
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003043static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3044{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003045 /* cancel requested || normal exit path */
3046 return atomic_read(&fs_info->balance_cancel_req) ||
3047 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3048 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003049}
3050
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003051static void __cancel_balance(struct btrfs_fs_info *fs_info)
3052{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003053 int ret;
3054
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003055 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003056 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003057 if (ret)
3058 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003059
3060 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003061}
3062
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003063void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003064 struct btrfs_ioctl_balance_args *bargs);
3065
3066/*
3067 * Should be called with both balance and volume mutexes held
3068 */
3069int btrfs_balance(struct btrfs_balance_control *bctl,
3070 struct btrfs_ioctl_balance_args *bargs)
3071{
3072 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003073 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003074 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003075 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003076 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003077 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003078
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003079 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003080 atomic_read(&fs_info->balance_pause_req) ||
3081 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003082 ret = -EINVAL;
3083 goto out;
3084 }
3085
Ilya Dryomove4837f82012-03-27 17:09:17 +03003086 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3087 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3088 mixed = 1;
3089
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003090 /*
3091 * In case of mixed groups both data and meta should be picked,
3092 * and identical options should be given for both of them.
3093 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003094 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3095 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003096 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3097 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3098 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3099 printk(KERN_ERR "btrfs: with mixed groups data and "
3100 "metadata balance options must be the same\n");
3101 ret = -EINVAL;
3102 goto out;
3103 }
3104 }
3105
Stefan Behrens8dabb742012-11-06 13:15:27 +01003106 num_devices = fs_info->fs_devices->num_devices;
3107 btrfs_dev_replace_lock(&fs_info->dev_replace);
3108 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3109 BUG_ON(num_devices < 1);
3110 num_devices--;
3111 }
3112 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003113 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003114 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003115 allowed |= BTRFS_BLOCK_GROUP_DUP;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003116 else if (num_devices < 4)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003117 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3118 else
3119 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003120 BTRFS_BLOCK_GROUP_RAID10 |
3121 BTRFS_BLOCK_GROUP_RAID5 |
3122 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003123
Ilya Dryomov6728b192012-03-27 17:09:17 +03003124 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3125 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3126 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003127 printk(KERN_ERR "btrfs: unable to start balance with target "
3128 "data profile %llu\n",
3129 (unsigned long long)bctl->data.target);
3130 ret = -EINVAL;
3131 goto out;
3132 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003133 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3134 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3135 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003136 printk(KERN_ERR "btrfs: unable to start balance with target "
3137 "metadata profile %llu\n",
3138 (unsigned long long)bctl->meta.target);
3139 ret = -EINVAL;
3140 goto out;
3141 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003142 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3143 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3144 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003145 printk(KERN_ERR "btrfs: unable to start balance with target "
3146 "system profile %llu\n",
3147 (unsigned long long)bctl->sys.target);
3148 ret = -EINVAL;
3149 goto out;
3150 }
3151
Ilya Dryomove4837f82012-03-27 17:09:17 +03003152 /* allow dup'ed data chunks only in mixed mode */
3153 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003154 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003155 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3156 ret = -EINVAL;
3157 goto out;
3158 }
3159
3160 /* allow to reduce meta or sys integrity only if force set */
3161 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003162 BTRFS_BLOCK_GROUP_RAID10 |
3163 BTRFS_BLOCK_GROUP_RAID5 |
3164 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003165 do {
3166 seq = read_seqbegin(&fs_info->profiles_lock);
3167
3168 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3169 (fs_info->avail_system_alloc_bits & allowed) &&
3170 !(bctl->sys.target & allowed)) ||
3171 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3172 (fs_info->avail_metadata_alloc_bits & allowed) &&
3173 !(bctl->meta.target & allowed))) {
3174 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3175 printk(KERN_INFO "btrfs: force reducing metadata "
3176 "integrity\n");
3177 } else {
3178 printk(KERN_ERR "btrfs: balance will reduce metadata "
3179 "integrity, use force if you want this\n");
3180 ret = -EINVAL;
3181 goto out;
3182 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003183 }
Miao Xiede98ced2013-01-29 10:13:12 +00003184 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003185
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003186 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3187 int num_tolerated_disk_barrier_failures;
3188 u64 target = bctl->sys.target;
3189
3190 num_tolerated_disk_barrier_failures =
3191 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3192 if (num_tolerated_disk_barrier_failures > 0 &&
3193 (target &
3194 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3195 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3196 num_tolerated_disk_barrier_failures = 0;
3197 else if (num_tolerated_disk_barrier_failures > 1 &&
3198 (target &
3199 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3200 num_tolerated_disk_barrier_failures = 1;
3201
3202 fs_info->num_tolerated_disk_barrier_failures =
3203 num_tolerated_disk_barrier_failures;
3204 }
3205
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003206 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003207 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003208 goto out;
3209
Ilya Dryomov59641012012-01-16 22:04:48 +02003210 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3211 BUG_ON(ret == -EEXIST);
3212 set_balance_control(bctl);
3213 } else {
3214 BUG_ON(ret != -EEXIST);
3215 spin_lock(&fs_info->balance_lock);
3216 update_balance_args(bctl);
3217 spin_unlock(&fs_info->balance_lock);
3218 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003219
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003220 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003221 mutex_unlock(&fs_info->balance_mutex);
3222
3223 ret = __btrfs_balance(fs_info);
3224
3225 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003226 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003227
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003228 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3229 fs_info->num_tolerated_disk_barrier_failures =
3230 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3231 }
3232
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003233 if (bargs) {
3234 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003235 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003236 }
3237
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003238 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3239 balance_need_close(fs_info)) {
3240 __cancel_balance(fs_info);
3241 }
3242
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003243 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003244
3245 return ret;
3246out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003247 if (bctl->flags & BTRFS_BALANCE_RESUME)
3248 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003249 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003250 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003251 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3252 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003253 return ret;
3254}
3255
3256static int balance_kthread(void *data)
3257{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003258 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003259 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003260
3261 mutex_lock(&fs_info->volume_mutex);
3262 mutex_lock(&fs_info->balance_mutex);
3263
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003264 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003265 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003266 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003267 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003268
3269 mutex_unlock(&fs_info->balance_mutex);
3270 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003271
Ilya Dryomov59641012012-01-16 22:04:48 +02003272 return ret;
3273}
3274
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003275int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3276{
3277 struct task_struct *tsk;
3278
3279 spin_lock(&fs_info->balance_lock);
3280 if (!fs_info->balance_ctl) {
3281 spin_unlock(&fs_info->balance_lock);
3282 return 0;
3283 }
3284 spin_unlock(&fs_info->balance_lock);
3285
3286 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3287 printk(KERN_INFO "btrfs: force skipping balance\n");
3288 return 0;
3289 }
3290
3291 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3292 if (IS_ERR(tsk))
3293 return PTR_ERR(tsk);
3294
3295 return 0;
3296}
3297
Ilya Dryomov68310a52012-06-22 12:24:12 -06003298int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003299{
Ilya Dryomov59641012012-01-16 22:04:48 +02003300 struct btrfs_balance_control *bctl;
3301 struct btrfs_balance_item *item;
3302 struct btrfs_disk_balance_args disk_bargs;
3303 struct btrfs_path *path;
3304 struct extent_buffer *leaf;
3305 struct btrfs_key key;
3306 int ret;
3307
3308 path = btrfs_alloc_path();
3309 if (!path)
3310 return -ENOMEM;
3311
Ilya Dryomov68310a52012-06-22 12:24:12 -06003312 key.objectid = BTRFS_BALANCE_OBJECTID;
3313 key.type = BTRFS_BALANCE_ITEM_KEY;
3314 key.offset = 0;
3315
3316 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3317 if (ret < 0)
3318 goto out;
3319 if (ret > 0) { /* ret = -ENOENT; */
3320 ret = 0;
3321 goto out;
3322 }
3323
Ilya Dryomov59641012012-01-16 22:04:48 +02003324 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3325 if (!bctl) {
3326 ret = -ENOMEM;
3327 goto out;
3328 }
3329
Ilya Dryomov59641012012-01-16 22:04:48 +02003330 leaf = path->nodes[0];
3331 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3332
Ilya Dryomov68310a52012-06-22 12:24:12 -06003333 bctl->fs_info = fs_info;
3334 bctl->flags = btrfs_balance_flags(leaf, item);
3335 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003336
3337 btrfs_balance_data(leaf, item, &disk_bargs);
3338 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3339 btrfs_balance_meta(leaf, item, &disk_bargs);
3340 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3341 btrfs_balance_sys(leaf, item, &disk_bargs);
3342 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3343
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003344 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3345
Ilya Dryomov68310a52012-06-22 12:24:12 -06003346 mutex_lock(&fs_info->volume_mutex);
3347 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003348
Ilya Dryomov68310a52012-06-22 12:24:12 -06003349 set_balance_control(bctl);
3350
3351 mutex_unlock(&fs_info->balance_mutex);
3352 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003353out:
3354 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003355 return ret;
3356}
3357
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003358int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3359{
3360 int ret = 0;
3361
3362 mutex_lock(&fs_info->balance_mutex);
3363 if (!fs_info->balance_ctl) {
3364 mutex_unlock(&fs_info->balance_mutex);
3365 return -ENOTCONN;
3366 }
3367
3368 if (atomic_read(&fs_info->balance_running)) {
3369 atomic_inc(&fs_info->balance_pause_req);
3370 mutex_unlock(&fs_info->balance_mutex);
3371
3372 wait_event(fs_info->balance_wait_q,
3373 atomic_read(&fs_info->balance_running) == 0);
3374
3375 mutex_lock(&fs_info->balance_mutex);
3376 /* we are good with balance_ctl ripped off from under us */
3377 BUG_ON(atomic_read(&fs_info->balance_running));
3378 atomic_dec(&fs_info->balance_pause_req);
3379 } else {
3380 ret = -ENOTCONN;
3381 }
3382
3383 mutex_unlock(&fs_info->balance_mutex);
3384 return ret;
3385}
3386
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003387int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3388{
3389 mutex_lock(&fs_info->balance_mutex);
3390 if (!fs_info->balance_ctl) {
3391 mutex_unlock(&fs_info->balance_mutex);
3392 return -ENOTCONN;
3393 }
3394
3395 atomic_inc(&fs_info->balance_cancel_req);
3396 /*
3397 * if we are running just wait and return, balance item is
3398 * deleted in btrfs_balance in this case
3399 */
3400 if (atomic_read(&fs_info->balance_running)) {
3401 mutex_unlock(&fs_info->balance_mutex);
3402 wait_event(fs_info->balance_wait_q,
3403 atomic_read(&fs_info->balance_running) == 0);
3404 mutex_lock(&fs_info->balance_mutex);
3405 } else {
3406 /* __cancel_balance needs volume_mutex */
3407 mutex_unlock(&fs_info->balance_mutex);
3408 mutex_lock(&fs_info->volume_mutex);
3409 mutex_lock(&fs_info->balance_mutex);
3410
3411 if (fs_info->balance_ctl)
3412 __cancel_balance(fs_info);
3413
3414 mutex_unlock(&fs_info->volume_mutex);
3415 }
3416
3417 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3418 atomic_dec(&fs_info->balance_cancel_req);
3419 mutex_unlock(&fs_info->balance_mutex);
3420 return 0;
3421}
3422
Chris Mason8f18cf12008-04-25 16:53:30 -04003423/*
3424 * shrinking a device means finding all of the device extents past
3425 * the new size, and then following the back refs to the chunks.
3426 * The chunk relocation code actually frees the device extent
3427 */
3428int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3429{
3430 struct btrfs_trans_handle *trans;
3431 struct btrfs_root *root = device->dev_root;
3432 struct btrfs_dev_extent *dev_extent = NULL;
3433 struct btrfs_path *path;
3434 u64 length;
3435 u64 chunk_tree;
3436 u64 chunk_objectid;
3437 u64 chunk_offset;
3438 int ret;
3439 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003440 int failed = 0;
3441 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003442 struct extent_buffer *l;
3443 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003444 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003445 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003446 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003447 u64 diff = device->total_bytes - new_size;
3448
Stefan Behrens63a212a2012-11-05 18:29:28 +01003449 if (device->is_tgtdev_for_dev_replace)
3450 return -EINVAL;
3451
Chris Mason8f18cf12008-04-25 16:53:30 -04003452 path = btrfs_alloc_path();
3453 if (!path)
3454 return -ENOMEM;
3455
Chris Mason8f18cf12008-04-25 16:53:30 -04003456 path->reada = 2;
3457
Chris Mason7d9eb122008-07-08 14:19:17 -04003458 lock_chunks(root);
3459
Chris Mason8f18cf12008-04-25 16:53:30 -04003460 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003461 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003462 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003463 spin_lock(&root->fs_info->free_chunk_lock);
3464 root->fs_info->free_chunk_space -= diff;
3465 spin_unlock(&root->fs_info->free_chunk_lock);
3466 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003467 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003468
Josef Bacikba1bf482009-09-11 16:11:19 -04003469again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003470 key.objectid = device->devid;
3471 key.offset = (u64)-1;
3472 key.type = BTRFS_DEV_EXTENT_KEY;
3473
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003474 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003475 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3476 if (ret < 0)
3477 goto done;
3478
3479 ret = btrfs_previous_item(root, path, 0, key.type);
3480 if (ret < 0)
3481 goto done;
3482 if (ret) {
3483 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003484 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003485 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003486 }
3487
3488 l = path->nodes[0];
3489 slot = path->slots[0];
3490 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3491
Josef Bacikba1bf482009-09-11 16:11:19 -04003492 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003493 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003494 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003495 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003496
3497 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3498 length = btrfs_dev_extent_length(l, dev_extent);
3499
Josef Bacikba1bf482009-09-11 16:11:19 -04003500 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003501 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003502 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003503 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003504
3505 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3506 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3507 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003508 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003509
3510 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3511 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003512 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003513 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003514 if (ret == -ENOSPC)
3515 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003516 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003517
3518 if (failed && !retried) {
3519 failed = 0;
3520 retried = true;
3521 goto again;
3522 } else if (failed && retried) {
3523 ret = -ENOSPC;
3524 lock_chunks(root);
3525
3526 device->total_bytes = old_size;
3527 if (device->writeable)
3528 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003529 spin_lock(&root->fs_info->free_chunk_lock);
3530 root->fs_info->free_chunk_space += diff;
3531 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003532 unlock_chunks(root);
3533 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003534 }
3535
Chris Balld6397ba2009-04-27 07:29:03 -04003536 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003537 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003538 if (IS_ERR(trans)) {
3539 ret = PTR_ERR(trans);
3540 goto done;
3541 }
3542
Chris Balld6397ba2009-04-27 07:29:03 -04003543 lock_chunks(root);
3544
3545 device->disk_total_bytes = new_size;
3546 /* Now btrfs_update_device() will change the on-disk size. */
3547 ret = btrfs_update_device(trans, device);
3548 if (ret) {
3549 unlock_chunks(root);
3550 btrfs_end_transaction(trans, root);
3551 goto done;
3552 }
3553 WARN_ON(diff > old_total);
3554 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3555 unlock_chunks(root);
3556 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003557done:
3558 btrfs_free_path(path);
3559 return ret;
3560}
3561
Li Zefan125ccb02011-12-08 15:07:24 +08003562static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003563 struct btrfs_key *key,
3564 struct btrfs_chunk *chunk, int item_size)
3565{
David Sterba6c417612011-04-13 15:41:04 +02003566 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003567 struct btrfs_disk_key disk_key;
3568 u32 array_size;
3569 u8 *ptr;
3570
3571 array_size = btrfs_super_sys_array_size(super_copy);
3572 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3573 return -EFBIG;
3574
3575 ptr = super_copy->sys_chunk_array + array_size;
3576 btrfs_cpu_key_to_disk(&disk_key, key);
3577 memcpy(ptr, &disk_key, sizeof(disk_key));
3578 ptr += sizeof(disk_key);
3579 memcpy(ptr, chunk, item_size);
3580 item_size += sizeof(disk_key);
3581 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3582 return 0;
3583}
3584
Miao Xieb2117a32011-01-05 10:07:28 +00003585/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003586 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003587 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003588static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003589{
Arne Jansen73c5de02011-04-12 12:07:57 +02003590 const struct btrfs_device_info *di_a = a;
3591 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003592
Arne Jansen73c5de02011-04-12 12:07:57 +02003593 if (di_a->max_avail > di_b->max_avail)
3594 return -1;
3595 if (di_a->max_avail < di_b->max_avail)
3596 return 1;
3597 if (di_a->total_avail > di_b->total_avail)
3598 return -1;
3599 if (di_a->total_avail < di_b->total_avail)
3600 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003601 return 0;
3602}
3603
Liu Bo31e50222012-11-21 14:18:10 +00003604struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00003605 [BTRFS_RAID_RAID10] = {
3606 .sub_stripes = 2,
3607 .dev_stripes = 1,
3608 .devs_max = 0, /* 0 == as many as possible */
3609 .devs_min = 4,
3610 .devs_increment = 2,
3611 .ncopies = 2,
3612 },
3613 [BTRFS_RAID_RAID1] = {
3614 .sub_stripes = 1,
3615 .dev_stripes = 1,
3616 .devs_max = 2,
3617 .devs_min = 2,
3618 .devs_increment = 2,
3619 .ncopies = 2,
3620 },
3621 [BTRFS_RAID_DUP] = {
3622 .sub_stripes = 1,
3623 .dev_stripes = 2,
3624 .devs_max = 1,
3625 .devs_min = 1,
3626 .devs_increment = 1,
3627 .ncopies = 2,
3628 },
3629 [BTRFS_RAID_RAID0] = {
3630 .sub_stripes = 1,
3631 .dev_stripes = 1,
3632 .devs_max = 0,
3633 .devs_min = 2,
3634 .devs_increment = 1,
3635 .ncopies = 1,
3636 },
3637 [BTRFS_RAID_SINGLE] = {
3638 .sub_stripes = 1,
3639 .dev_stripes = 1,
3640 .devs_max = 1,
3641 .devs_min = 1,
3642 .devs_increment = 1,
3643 .ncopies = 1,
3644 },
Chris Masone942f882013-02-20 14:06:05 -05003645 [BTRFS_RAID_RAID5] = {
3646 .sub_stripes = 1,
3647 .dev_stripes = 1,
3648 .devs_max = 0,
3649 .devs_min = 2,
3650 .devs_increment = 1,
3651 .ncopies = 2,
3652 },
3653 [BTRFS_RAID_RAID6] = {
3654 .sub_stripes = 1,
3655 .dev_stripes = 1,
3656 .devs_max = 0,
3657 .devs_min = 3,
3658 .devs_increment = 1,
3659 .ncopies = 3,
3660 },
Liu Bo31e50222012-11-21 14:18:10 +00003661};
3662
David Woodhouse53b381b2013-01-29 18:40:14 -05003663static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3664{
3665 /* TODO allow them to set a preferred stripe size */
3666 return 64 * 1024;
3667}
3668
3669static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3670{
3671 u64 features;
3672
3673 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3674 return;
3675
3676 features = btrfs_super_incompat_flags(info->super_copy);
3677 if (features & BTRFS_FEATURE_INCOMPAT_RAID56)
3678 return;
3679
3680 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
3681 btrfs_set_super_incompat_flags(info->super_copy, features);
3682 printk(KERN_INFO "btrfs: setting RAID5/6 feature flag\n");
3683}
3684
Miao Xieb2117a32011-01-05 10:07:28 +00003685static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3686 struct btrfs_root *extent_root,
3687 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003688 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003689 u64 start, u64 type)
3690{
3691 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003692 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3693 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003694 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003695 struct extent_map_tree *em_tree;
3696 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003697 struct btrfs_device_info *devices_info = NULL;
3698 u64 total_avail;
3699 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05003700 int data_stripes; /* number of stripes that count for
3701 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02003702 int sub_stripes; /* sub_stripes info for map */
3703 int dev_stripes; /* stripes per dev */
3704 int devs_max; /* max devs to use */
3705 int devs_min; /* min devs needed */
3706 int devs_increment; /* ndevs has to be a multiple of this */
3707 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003708 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003709 u64 max_stripe_size;
3710 u64 max_chunk_size;
3711 u64 stripe_size;
3712 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05003713 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02003714 int ndevs;
3715 int i;
3716 int j;
Liu Bo31e50222012-11-21 14:18:10 +00003717 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00003718
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003719 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003720
Miao Xieb2117a32011-01-05 10:07:28 +00003721 if (list_empty(&fs_devices->alloc_list))
3722 return -ENOSPC;
3723
Liu Bo31e50222012-11-21 14:18:10 +00003724 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02003725
Liu Bo31e50222012-11-21 14:18:10 +00003726 sub_stripes = btrfs_raid_array[index].sub_stripes;
3727 dev_stripes = btrfs_raid_array[index].dev_stripes;
3728 devs_max = btrfs_raid_array[index].devs_max;
3729 devs_min = btrfs_raid_array[index].devs_min;
3730 devs_increment = btrfs_raid_array[index].devs_increment;
3731 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02003732
3733 if (type & BTRFS_BLOCK_GROUP_DATA) {
3734 max_stripe_size = 1024 * 1024 * 1024;
3735 max_chunk_size = 10 * max_stripe_size;
3736 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003737 /* for larger filesystems, use larger metadata chunks */
3738 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3739 max_stripe_size = 1024 * 1024 * 1024;
3740 else
3741 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003742 max_chunk_size = max_stripe_size;
3743 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003744 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003745 max_chunk_size = 2 * max_stripe_size;
3746 } else {
3747 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3748 type);
3749 BUG_ON(1);
3750 }
3751
3752 /* we don't want a chunk larger than 10% of writeable space */
3753 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3754 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003755
3756 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3757 GFP_NOFS);
3758 if (!devices_info)
3759 return -ENOMEM;
3760
Arne Jansen73c5de02011-04-12 12:07:57 +02003761 cur = fs_devices->alloc_list.next;
3762
3763 /*
3764 * in the first pass through the devices list, we gather information
3765 * about the available holes on each device.
3766 */
3767 ndevs = 0;
3768 while (cur != &fs_devices->alloc_list) {
3769 struct btrfs_device *device;
3770 u64 max_avail;
3771 u64 dev_offset;
3772
3773 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3774
3775 cur = cur->next;
3776
3777 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003778 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003779 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003780 continue;
3781 }
3782
Stefan Behrens63a212a2012-11-05 18:29:28 +01003783 if (!device->in_fs_metadata ||
3784 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003785 continue;
3786
3787 if (device->total_bytes > device->bytes_used)
3788 total_avail = device->total_bytes - device->bytes_used;
3789 else
3790 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003791
3792 /* If there is no space on this device, skip it. */
3793 if (total_avail == 0)
3794 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003795
Li Zefan125ccb02011-12-08 15:07:24 +08003796 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003797 max_stripe_size * dev_stripes,
3798 &dev_offset, &max_avail);
3799 if (ret && ret != -ENOSPC)
3800 goto error;
3801
3802 if (ret == 0)
3803 max_avail = max_stripe_size * dev_stripes;
3804
3805 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3806 continue;
3807
Eric Sandeen063d0062013-01-31 00:55:01 +00003808 if (ndevs == fs_devices->rw_devices) {
3809 WARN(1, "%s: found more than %llu devices\n",
3810 __func__, fs_devices->rw_devices);
3811 break;
3812 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003813 devices_info[ndevs].dev_offset = dev_offset;
3814 devices_info[ndevs].max_avail = max_avail;
3815 devices_info[ndevs].total_avail = total_avail;
3816 devices_info[ndevs].dev = device;
3817 ++ndevs;
3818 }
3819
3820 /*
3821 * now sort the devices by hole size / available space
3822 */
3823 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3824 btrfs_cmp_device_info, NULL);
3825
3826 /* round down to number of usable stripes */
3827 ndevs -= ndevs % devs_increment;
3828
3829 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3830 ret = -ENOSPC;
3831 goto error;
3832 }
3833
3834 if (devs_max && ndevs > devs_max)
3835 ndevs = devs_max;
3836 /*
3837 * the primary goal is to maximize the number of stripes, so use as many
3838 * devices as possible, even if the stripes are not maximum sized.
3839 */
3840 stripe_size = devices_info[ndevs-1].max_avail;
3841 num_stripes = ndevs * dev_stripes;
3842
David Woodhouse53b381b2013-01-29 18:40:14 -05003843 /*
3844 * this will have to be fixed for RAID1 and RAID10 over
3845 * more drives
3846 */
3847 data_stripes = num_stripes / ncopies;
3848
David Woodhouse53b381b2013-01-29 18:40:14 -05003849 if (type & BTRFS_BLOCK_GROUP_RAID5) {
3850 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
3851 btrfs_super_stripesize(info->super_copy));
3852 data_stripes = num_stripes - 1;
3853 }
3854 if (type & BTRFS_BLOCK_GROUP_RAID6) {
3855 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
3856 btrfs_super_stripesize(info->super_copy));
3857 data_stripes = num_stripes - 2;
3858 }
Chris Mason86db2572013-02-20 16:23:40 -05003859
3860 /*
3861 * Use the number of data stripes to figure out how big this chunk
3862 * is really going to be in terms of logical address space,
3863 * and compare that answer with the max chunk size
3864 */
3865 if (stripe_size * data_stripes > max_chunk_size) {
3866 u64 mask = (1ULL << 24) - 1;
3867 stripe_size = max_chunk_size;
3868 do_div(stripe_size, data_stripes);
3869
3870 /* bump the answer up to a 16MB boundary */
3871 stripe_size = (stripe_size + mask) & ~mask;
3872
3873 /* but don't go higher than the limits we found
3874 * while searching for free extents
3875 */
3876 if (stripe_size > devices_info[ndevs-1].max_avail)
3877 stripe_size = devices_info[ndevs-1].max_avail;
3878 }
3879
Arne Jansen73c5de02011-04-12 12:07:57 +02003880 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003881
3882 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05003883 do_div(stripe_size, raid_stripe_len);
3884 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02003885
Miao Xieb2117a32011-01-05 10:07:28 +00003886 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3887 if (!map) {
3888 ret = -ENOMEM;
3889 goto error;
3890 }
3891 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003892
Arne Jansen73c5de02011-04-12 12:07:57 +02003893 for (i = 0; i < ndevs; ++i) {
3894 for (j = 0; j < dev_stripes; ++j) {
3895 int s = i * dev_stripes + j;
3896 map->stripes[s].dev = devices_info[i].dev;
3897 map->stripes[s].physical = devices_info[i].dev_offset +
3898 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003899 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003900 }
Chris Mason593060d2008-03-25 16:50:33 -04003901 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05003902 map->stripe_len = raid_stripe_len;
3903 map->io_align = raid_stripe_len;
3904 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04003905 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003906 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003907
Yan Zheng2b820322008-11-17 21:11:30 -05003908 *map_ret = map;
David Woodhouse53b381b2013-01-29 18:40:14 -05003909 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003910
Arne Jansen73c5de02011-04-12 12:07:57 +02003911 *stripe_size_out = stripe_size;
3912 *num_bytes_out = num_bytes;
3913
3914 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003915
David Sterba172ddd62011-04-21 00:48:27 +02003916 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003917 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003918 ret = -ENOMEM;
3919 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003920 }
Chris Mason0b86a832008-03-24 15:01:56 -04003921 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003922 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003923 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003924 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003925 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003926
Chris Mason0b86a832008-03-24 15:01:56 -04003927 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003928 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003929 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003930 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003931 if (ret) {
3932 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003933 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003934 }
Yan Zheng2b820322008-11-17 21:11:30 -05003935
Arne Jansen73c5de02011-04-12 12:07:57 +02003936 for (i = 0; i < map->num_stripes; ++i) {
3937 struct btrfs_device *device;
3938 u64 dev_offset;
3939
3940 device = map->stripes[i].dev;
3941 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003942
3943 ret = btrfs_alloc_dev_extent(trans, device,
3944 info->chunk_root->root_key.objectid,
3945 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003946 start, dev_offset, stripe_size);
Josef Bacik04487482013-01-29 15:03:37 -05003947 if (ret)
3948 goto error_dev_extent;
3949 }
3950
3951 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3952 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3953 start, num_bytes);
3954 if (ret) {
3955 i = map->num_stripes - 1;
3956 goto error_dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05003957 }
3958
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003959 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05003960 check_raid56_incompat_flag(extent_root->fs_info, type);
3961
Miao Xieb2117a32011-01-05 10:07:28 +00003962 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003963 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003964
Josef Bacik04487482013-01-29 15:03:37 -05003965error_dev_extent:
3966 for (; i >= 0; i--) {
3967 struct btrfs_device *device;
3968 int err;
3969
3970 device = map->stripes[i].dev;
3971 err = btrfs_free_dev_extent(trans, device, start);
3972 if (err) {
3973 btrfs_abort_transaction(trans, extent_root, err);
3974 break;
3975 }
3976 }
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003977 write_lock(&em_tree->lock);
3978 remove_extent_mapping(em_tree, em);
3979 write_unlock(&em_tree->lock);
3980
3981 /* One for our allocation */
3982 free_extent_map(em);
3983 /* One for the tree reference */
3984 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00003985error:
3986 kfree(map);
3987 kfree(devices_info);
3988 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003989}
3990
3991static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3992 struct btrfs_root *extent_root,
3993 struct map_lookup *map, u64 chunk_offset,
3994 u64 chunk_size, u64 stripe_size)
3995{
3996 u64 dev_offset;
3997 struct btrfs_key key;
3998 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3999 struct btrfs_device *device;
4000 struct btrfs_chunk *chunk;
4001 struct btrfs_stripe *stripe;
4002 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
4003 int index = 0;
4004 int ret;
4005
4006 chunk = kzalloc(item_size, GFP_NOFS);
4007 if (!chunk)
4008 return -ENOMEM;
4009
4010 index = 0;
4011 while (index < map->num_stripes) {
4012 device = map->stripes[index].dev;
4013 device->bytes_used += stripe_size;
4014 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004015 if (ret)
4016 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05004017 index++;
4018 }
4019
Josef Bacik2bf64752011-09-26 17:12:22 -04004020 spin_lock(&extent_root->fs_info->free_chunk_lock);
4021 extent_root->fs_info->free_chunk_space -= (stripe_size *
4022 map->num_stripes);
4023 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4024
Yan Zheng2b820322008-11-17 21:11:30 -05004025 index = 0;
4026 stripe = &chunk->stripe;
4027 while (index < map->num_stripes) {
4028 device = map->stripes[index].dev;
4029 dev_offset = map->stripes[index].physical;
4030
4031 btrfs_set_stack_stripe_devid(stripe, device->devid);
4032 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4033 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4034 stripe++;
4035 index++;
4036 }
4037
4038 btrfs_set_stack_chunk_length(chunk, chunk_size);
4039 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4040 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4041 btrfs_set_stack_chunk_type(chunk, map->type);
4042 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4043 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4044 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4045 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4046 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4047
4048 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4049 key.type = BTRFS_CHUNK_ITEM_KEY;
4050 key.offset = chunk_offset;
4051
4052 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004053
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004054 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4055 /*
4056 * TODO: Cleanup of inserted chunk root in case of
4057 * failure.
4058 */
Li Zefan125ccb02011-12-08 15:07:24 +08004059 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004060 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004061 }
liubo1abe9b82011-03-24 11:18:59 +00004062
Mark Fasheh3acd3952011-09-08 17:40:01 -07004063out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05004064 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004065 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004066}
4067
4068/*
4069 * Chunk allocation falls into two parts. The first part does works
4070 * that make the new allocated chunk useable, but not do any operation
4071 * that modifies the chunk tree. The second part does the works that
4072 * require modifying the chunk tree. This division is important for the
4073 * bootstrap process of adding storage to a seed btrfs.
4074 */
4075int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4076 struct btrfs_root *extent_root, u64 type)
4077{
4078 u64 chunk_offset;
4079 u64 chunk_size;
4080 u64 stripe_size;
4081 struct map_lookup *map;
4082 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4083 int ret;
4084
4085 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4086 &chunk_offset);
4087 if (ret)
4088 return ret;
4089
4090 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4091 &stripe_size, chunk_offset, type);
4092 if (ret)
4093 return ret;
4094
4095 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4096 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004097 if (ret)
4098 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004099 return 0;
4100}
4101
Chris Masond3977122009-01-05 21:25:51 -05004102static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004103 struct btrfs_root *root,
4104 struct btrfs_device *device)
4105{
4106 u64 chunk_offset;
4107 u64 sys_chunk_offset;
4108 u64 chunk_size;
4109 u64 sys_chunk_size;
4110 u64 stripe_size;
4111 u64 sys_stripe_size;
4112 u64 alloc_profile;
4113 struct map_lookup *map;
4114 struct map_lookup *sys_map;
4115 struct btrfs_fs_info *fs_info = root->fs_info;
4116 struct btrfs_root *extent_root = fs_info->extent_root;
4117 int ret;
4118
4119 ret = find_next_chunk(fs_info->chunk_root,
4120 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004121 if (ret)
4122 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004123
Miao Xiede98ced2013-01-29 10:13:12 +00004124 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004125 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4126 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004127 if (ret)
4128 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004129
4130 sys_chunk_offset = chunk_offset + chunk_size;
4131
Miao Xiede98ced2013-01-29 10:13:12 +00004132 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004133 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
4134 &sys_chunk_size, &sys_stripe_size,
4135 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004136 if (ret) {
4137 btrfs_abort_transaction(trans, root, ret);
4138 goto out;
4139 }
Yan Zheng2b820322008-11-17 21:11:30 -05004140
4141 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06004142 if (ret) {
4143 btrfs_abort_transaction(trans, root, ret);
4144 goto out;
4145 }
Yan Zheng2b820322008-11-17 21:11:30 -05004146
4147 /*
4148 * Modifying chunk tree needs allocating new blocks from both
4149 * system block group and metadata block group. So we only can
4150 * do operations require modifying the chunk tree after both
4151 * block groups were created.
4152 */
4153 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4154 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06004155 if (ret) {
4156 btrfs_abort_transaction(trans, root, ret);
4157 goto out;
4158 }
Yan Zheng2b820322008-11-17 21:11:30 -05004159
4160 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
4161 sys_chunk_offset, sys_chunk_size,
4162 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004163 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004164 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004165
David Sterba005d6422012-09-18 07:52:32 -06004166out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004167
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004168 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004169}
4170
4171int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4172{
4173 struct extent_map *em;
4174 struct map_lookup *map;
4175 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4176 int readonly = 0;
4177 int i;
4178
Chris Mason890871b2009-09-02 16:24:52 -04004179 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004180 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004181 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004182 if (!em)
4183 return 1;
4184
Josef Bacikf48b9072010-01-27 02:07:59 +00004185 if (btrfs_test_opt(root, DEGRADED)) {
4186 free_extent_map(em);
4187 return 0;
4188 }
4189
Yan Zheng2b820322008-11-17 21:11:30 -05004190 map = (struct map_lookup *)em->bdev;
4191 for (i = 0; i < map->num_stripes; i++) {
4192 if (!map->stripes[i].dev->writeable) {
4193 readonly = 1;
4194 break;
4195 }
4196 }
4197 free_extent_map(em);
4198 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004199}
4200
4201void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4202{
David Sterbaa8067e02011-04-21 00:34:43 +02004203 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004204}
4205
4206void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4207{
4208 struct extent_map *em;
4209
Chris Masond3977122009-01-05 21:25:51 -05004210 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004211 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004212 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4213 if (em)
4214 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004215 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004216 if (!em)
4217 break;
4218 kfree(em->bdev);
4219 /* once for us */
4220 free_extent_map(em);
4221 /* once for the tree */
4222 free_extent_map(em);
4223 }
4224}
4225
Stefan Behrens5d964052012-11-05 14:59:07 +01004226int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004227{
Stefan Behrens5d964052012-11-05 14:59:07 +01004228 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004229 struct extent_map *em;
4230 struct map_lookup *map;
4231 struct extent_map_tree *em_tree = &map_tree->map_tree;
4232 int ret;
4233
Chris Mason890871b2009-09-02 16:24:52 -04004234 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004235 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004236 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004237 BUG_ON(!em);
4238
4239 BUG_ON(em->start > logical || em->start + em->len < logical);
4240 map = (struct map_lookup *)em->bdev;
4241 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4242 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004243 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4244 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004245 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4246 ret = 2;
4247 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4248 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004249 else
4250 ret = 1;
4251 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004252
4253 btrfs_dev_replace_lock(&fs_info->dev_replace);
4254 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4255 ret++;
4256 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4257
Chris Masonf1885912008-04-09 16:28:12 -04004258 return ret;
4259}
4260
David Woodhouse53b381b2013-01-29 18:40:14 -05004261unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4262 struct btrfs_mapping_tree *map_tree,
4263 u64 logical)
4264{
4265 struct extent_map *em;
4266 struct map_lookup *map;
4267 struct extent_map_tree *em_tree = &map_tree->map_tree;
4268 unsigned long len = root->sectorsize;
4269
4270 read_lock(&em_tree->lock);
4271 em = lookup_extent_mapping(em_tree, logical, len);
4272 read_unlock(&em_tree->lock);
4273 BUG_ON(!em);
4274
4275 BUG_ON(em->start > logical || em->start + em->len < logical);
4276 map = (struct map_lookup *)em->bdev;
4277 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4278 BTRFS_BLOCK_GROUP_RAID6)) {
4279 len = map->stripe_len * nr_data_stripes(map);
4280 }
4281 free_extent_map(em);
4282 return len;
4283}
4284
4285int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4286 u64 logical, u64 len, int mirror_num)
4287{
4288 struct extent_map *em;
4289 struct map_lookup *map;
4290 struct extent_map_tree *em_tree = &map_tree->map_tree;
4291 int ret = 0;
4292
4293 read_lock(&em_tree->lock);
4294 em = lookup_extent_mapping(em_tree, logical, len);
4295 read_unlock(&em_tree->lock);
4296 BUG_ON(!em);
4297
4298 BUG_ON(em->start > logical || em->start + em->len < logical);
4299 map = (struct map_lookup *)em->bdev;
4300 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4301 BTRFS_BLOCK_GROUP_RAID6))
4302 ret = 1;
4303 free_extent_map(em);
4304 return ret;
4305}
4306
Stefan Behrens30d98612012-11-06 14:52:18 +01004307static int find_live_mirror(struct btrfs_fs_info *fs_info,
4308 struct map_lookup *map, int first, int num,
4309 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004310{
4311 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004312 int tolerance;
4313 struct btrfs_device *srcdev;
4314
4315 if (dev_replace_is_ongoing &&
4316 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4317 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4318 srcdev = fs_info->dev_replace.srcdev;
4319 else
4320 srcdev = NULL;
4321
4322 /*
4323 * try to avoid the drive that is the source drive for a
4324 * dev-replace procedure, only choose it if no other non-missing
4325 * mirror is available
4326 */
4327 for (tolerance = 0; tolerance < 2; tolerance++) {
4328 if (map->stripes[optimal].dev->bdev &&
4329 (tolerance || map->stripes[optimal].dev != srcdev))
4330 return optimal;
4331 for (i = first; i < first + num; i++) {
4332 if (map->stripes[i].dev->bdev &&
4333 (tolerance || map->stripes[i].dev != srcdev))
4334 return i;
4335 }
Chris Masondfe25022008-05-13 13:46:40 -04004336 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004337
Chris Masondfe25022008-05-13 13:46:40 -04004338 /* we couldn't find one that doesn't fail. Just return something
4339 * and the io error handling code will clean up eventually
4340 */
4341 return optimal;
4342}
4343
David Woodhouse53b381b2013-01-29 18:40:14 -05004344static inline int parity_smaller(u64 a, u64 b)
4345{
4346 return a > b;
4347}
4348
4349/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4350static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4351{
4352 struct btrfs_bio_stripe s;
4353 int i;
4354 u64 l;
4355 int again = 1;
4356
4357 while (again) {
4358 again = 0;
4359 for (i = 0; i < bbio->num_stripes - 1; i++) {
4360 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4361 s = bbio->stripes[i];
4362 l = raid_map[i];
4363 bbio->stripes[i] = bbio->stripes[i+1];
4364 raid_map[i] = raid_map[i+1];
4365 bbio->stripes[i+1] = s;
4366 raid_map[i+1] = l;
4367 again = 1;
4368 }
4369 }
4370 }
4371}
4372
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004373static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004374 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004375 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004376 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004377{
4378 struct extent_map *em;
4379 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004380 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004381 struct extent_map_tree *em_tree = &map_tree->map_tree;
4382 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004383 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004384 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004385 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004386 u64 stripe_nr_orig;
4387 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004388 u64 stripe_len;
4389 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004390 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004391 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004392 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004393 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004394 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004395 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004396 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4397 int dev_replace_is_ongoing = 0;
4398 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004399 int patch_the_first_stripe_for_dev_replace = 0;
4400 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004401 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004402
Chris Mason890871b2009-09-02 16:24:52 -04004403 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004404 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004405 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004406
Chris Mason3b951512008-04-17 11:29:12 -04004407 if (!em) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004408 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004409 (unsigned long long)logical,
4410 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04004411 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04004412 }
Chris Mason0b86a832008-03-24 15:01:56 -04004413
4414 BUG_ON(em->start > logical || em->start + em->len < logical);
4415 map = (struct map_lookup *)em->bdev;
4416 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004417
David Woodhouse53b381b2013-01-29 18:40:14 -05004418 if (mirror_num > map->num_stripes)
4419 mirror_num = 0;
4420
4421 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004422 stripe_nr = offset;
4423 /*
4424 * stripe_nr counts the total number of stripes we have to stride
4425 * to get to this block
4426 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004427 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004428
David Woodhouse53b381b2013-01-29 18:40:14 -05004429 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004430 BUG_ON(offset < stripe_offset);
4431
4432 /* stripe_offset is the offset of this block in its stripe*/
4433 stripe_offset = offset - stripe_offset;
4434
David Woodhouse53b381b2013-01-29 18:40:14 -05004435 /* if we're here for raid56, we need to know the stripe aligned start */
4436 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4437 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4438 raid56_full_stripe_start = offset;
4439
4440 /* allow a write of a full stripe, but make sure we don't
4441 * allow straddling of stripes
4442 */
4443 do_div(raid56_full_stripe_start, full_stripe_len);
4444 raid56_full_stripe_start *= full_stripe_len;
4445 }
4446
4447 if (rw & REQ_DISCARD) {
4448 /* we don't discard raid56 yet */
4449 if (map->type &
4450 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4451 ret = -EOPNOTSUPP;
4452 goto out;
4453 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004454 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004455 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4456 u64 max_len;
4457 /* For writes to RAID[56], allow a full stripeset across all disks.
4458 For other RAID types and for RAID[56] reads, just allow a single
4459 stripe (on a single disk). */
4460 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4461 (rw & REQ_WRITE)) {
4462 max_len = stripe_len * nr_data_stripes(map) -
4463 (offset - raid56_full_stripe_start);
4464 } else {
4465 /* we limit the length of each bio to what fits in a stripe */
4466 max_len = stripe_len - stripe_offset;
4467 }
4468 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004469 } else {
4470 *length = em->len - offset;
4471 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004472
David Woodhouse53b381b2013-01-29 18:40:14 -05004473 /* This is for when we're called from btrfs_merge_bio_hook() and all
4474 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004475 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004476 goto out;
4477
Stefan Behrens472262f2012-11-06 14:43:46 +01004478 btrfs_dev_replace_lock(dev_replace);
4479 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4480 if (!dev_replace_is_ongoing)
4481 btrfs_dev_replace_unlock(dev_replace);
4482
Stefan Behrensad6d6202012-11-06 15:06:47 +01004483 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4484 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4485 dev_replace->tgtdev != NULL) {
4486 /*
4487 * in dev-replace case, for repair case (that's the only
4488 * case where the mirror is selected explicitly when
4489 * calling btrfs_map_block), blocks left of the left cursor
4490 * can also be read from the target drive.
4491 * For REQ_GET_READ_MIRRORS, the target drive is added as
4492 * the last one to the array of stripes. For READ, it also
4493 * needs to be supported using the same mirror number.
4494 * If the requested block is not left of the left cursor,
4495 * EIO is returned. This can happen because btrfs_num_copies()
4496 * returns one more in the dev-replace case.
4497 */
4498 u64 tmp_length = *length;
4499 struct btrfs_bio *tmp_bbio = NULL;
4500 int tmp_num_stripes;
4501 u64 srcdev_devid = dev_replace->srcdev->devid;
4502 int index_srcdev = 0;
4503 int found = 0;
4504 u64 physical_of_found = 0;
4505
4506 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004507 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004508 if (ret) {
4509 WARN_ON(tmp_bbio != NULL);
4510 goto out;
4511 }
4512
4513 tmp_num_stripes = tmp_bbio->num_stripes;
4514 if (mirror_num > tmp_num_stripes) {
4515 /*
4516 * REQ_GET_READ_MIRRORS does not contain this
4517 * mirror, that means that the requested area
4518 * is not left of the left cursor
4519 */
4520 ret = -EIO;
4521 kfree(tmp_bbio);
4522 goto out;
4523 }
4524
4525 /*
4526 * process the rest of the function using the mirror_num
4527 * of the source drive. Therefore look it up first.
4528 * At the end, patch the device pointer to the one of the
4529 * target drive.
4530 */
4531 for (i = 0; i < tmp_num_stripes; i++) {
4532 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4533 /*
4534 * In case of DUP, in order to keep it
4535 * simple, only add the mirror with the
4536 * lowest physical address
4537 */
4538 if (found &&
4539 physical_of_found <=
4540 tmp_bbio->stripes[i].physical)
4541 continue;
4542 index_srcdev = i;
4543 found = 1;
4544 physical_of_found =
4545 tmp_bbio->stripes[i].physical;
4546 }
4547 }
4548
4549 if (found) {
4550 mirror_num = index_srcdev + 1;
4551 patch_the_first_stripe_for_dev_replace = 1;
4552 physical_to_patch_in_first_stripe = physical_of_found;
4553 } else {
4554 WARN_ON(1);
4555 ret = -EIO;
4556 kfree(tmp_bbio);
4557 goto out;
4558 }
4559
4560 kfree(tmp_bbio);
4561 } else if (mirror_num > map->num_stripes) {
4562 mirror_num = 0;
4563 }
4564
Chris Masonf2d8d742008-04-21 10:03:05 -04004565 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004566 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004567 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004568 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004569 do_div(stripe_nr_end, map->stripe_len);
4570 stripe_end_offset = stripe_nr_end * map->stripe_len -
4571 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004572
Li Dongyangfce3bb92011-03-24 10:24:26 +00004573 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4574 if (rw & REQ_DISCARD)
4575 num_stripes = min_t(u64, map->num_stripes,
4576 stripe_nr_end - stripe_nr_orig);
4577 stripe_index = do_div(stripe_nr, map->num_stripes);
4578 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004579 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004580 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004581 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004582 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004583 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004584 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004585 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004586 current->pid % map->num_stripes,
4587 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004588 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004589 }
Chris Mason2fff7342008-04-29 14:12:09 -04004590
Chris Mason611f0e02008-04-03 16:29:03 -04004591 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004592 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004593 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004594 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004595 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004596 } else {
4597 mirror_num = 1;
4598 }
Chris Mason2fff7342008-04-29 14:12:09 -04004599
Chris Mason321aecc2008-04-16 10:49:51 -04004600 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4601 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004602
4603 stripe_index = do_div(stripe_nr, factor);
4604 stripe_index *= map->sub_stripes;
4605
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004606 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004607 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004608 else if (rw & REQ_DISCARD)
4609 num_stripes = min_t(u64, map->sub_stripes *
4610 (stripe_nr_end - stripe_nr_orig),
4611 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004612 else if (mirror_num)
4613 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004614 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004615 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004616 stripe_index = find_live_mirror(fs_info, map,
4617 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004618 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004619 current->pid % map->sub_stripes,
4620 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004621 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004622 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004623
4624 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4625 BTRFS_BLOCK_GROUP_RAID6)) {
4626 u64 tmp;
4627
4628 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4629 && raid_map_ret) {
4630 int i, rot;
4631
4632 /* push stripe_nr back to the start of the full stripe */
4633 stripe_nr = raid56_full_stripe_start;
4634 do_div(stripe_nr, stripe_len);
4635
4636 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4637
4638 /* RAID[56] write or recovery. Return all stripes */
4639 num_stripes = map->num_stripes;
4640 max_errors = nr_parity_stripes(map);
4641
4642 raid_map = kmalloc(sizeof(u64) * num_stripes,
4643 GFP_NOFS);
4644 if (!raid_map) {
4645 ret = -ENOMEM;
4646 goto out;
4647 }
4648
4649 /* Work out the disk rotation on this stripe-set */
4650 tmp = stripe_nr;
4651 rot = do_div(tmp, num_stripes);
4652
4653 /* Fill in the logical address of each stripe */
4654 tmp = stripe_nr * nr_data_stripes(map);
4655 for (i = 0; i < nr_data_stripes(map); i++)
4656 raid_map[(i+rot) % num_stripes] =
4657 em->start + (tmp + i) * map->stripe_len;
4658
4659 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4660 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4661 raid_map[(i+rot+1) % num_stripes] =
4662 RAID6_Q_STRIPE;
4663
4664 *length = map->stripe_len;
4665 stripe_index = 0;
4666 stripe_offset = 0;
4667 } else {
4668 /*
4669 * Mirror #0 or #1 means the original data block.
4670 * Mirror #2 is RAID5 parity block.
4671 * Mirror #3 is RAID6 Q block.
4672 */
4673 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4674 if (mirror_num > 1)
4675 stripe_index = nr_data_stripes(map) +
4676 mirror_num - 2;
4677
4678 /* We distribute the parity blocks across stripes */
4679 tmp = stripe_nr + stripe_index;
4680 stripe_index = do_div(tmp, map->num_stripes);
4681 }
Chris Mason8790d502008-04-03 16:29:03 -04004682 } else {
4683 /*
4684 * after this do_div call, stripe_nr is the number of stripes
4685 * on this device we have to walk to find the data, and
4686 * stripe_index is the number of our device in the stripe array
4687 */
4688 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004689 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004690 }
Chris Mason593060d2008-03-25 16:50:33 -04004691 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004692
Stefan Behrens472262f2012-11-06 14:43:46 +01004693 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004694 if (dev_replace_is_ongoing) {
4695 if (rw & (REQ_WRITE | REQ_DISCARD))
4696 num_alloc_stripes <<= 1;
4697 if (rw & REQ_GET_READ_MIRRORS)
4698 num_alloc_stripes++;
4699 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004700 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08004701 if (!bbio) {
4702 ret = -ENOMEM;
4703 goto out;
4704 }
4705 atomic_set(&bbio->error, 0);
4706
Li Dongyangfce3bb92011-03-24 10:24:26 +00004707 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004708 int factor = 0;
4709 int sub_stripes = 0;
4710 u64 stripes_per_dev = 0;
4711 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004712 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004713
4714 if (map->type &
4715 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4716 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4717 sub_stripes = 1;
4718 else
4719 sub_stripes = map->sub_stripes;
4720
4721 factor = map->num_stripes / sub_stripes;
4722 stripes_per_dev = div_u64_rem(stripe_nr_end -
4723 stripe_nr_orig,
4724 factor,
4725 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004726 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4727 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004728 }
4729
Li Dongyangfce3bb92011-03-24 10:24:26 +00004730 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004731 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004732 map->stripes[stripe_index].physical +
4733 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004734 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004735
Li Zefanec9ef7a2011-12-01 14:06:42 +08004736 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4737 BTRFS_BLOCK_GROUP_RAID10)) {
4738 bbio->stripes[i].length = stripes_per_dev *
4739 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004740
Li Zefanec9ef7a2011-12-01 14:06:42 +08004741 if (i / sub_stripes < remaining_stripes)
4742 bbio->stripes[i].length +=
4743 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004744
4745 /*
4746 * Special for the first stripe and
4747 * the last stripe:
4748 *
4749 * |-------|...|-------|
4750 * |----------|
4751 * off end_off
4752 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004753 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004754 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004755 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004756
4757 if (stripe_index >= last_stripe &&
4758 stripe_index <= (last_stripe +
4759 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004760 bbio->stripes[i].length -=
4761 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004762
Li Zefanec9ef7a2011-12-01 14:06:42 +08004763 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004764 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004765 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004766 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004767
4768 stripe_index++;
4769 if (stripe_index == map->num_stripes) {
4770 /* This could only happen for RAID0/10 */
4771 stripe_index = 0;
4772 stripe_nr++;
4773 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004774 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004775 } else {
4776 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004777 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004778 map->stripes[stripe_index].physical +
4779 stripe_offset +
4780 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004781 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004782 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004783 stripe_index++;
4784 }
Chris Mason593060d2008-03-25 16:50:33 -04004785 }
Li Zefande11cc12011-12-01 12:55:47 +08004786
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004787 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08004788 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4789 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05004790 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08004791 BTRFS_BLOCK_GROUP_DUP)) {
4792 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05004793 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4794 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08004795 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004796 }
Li Zefande11cc12011-12-01 12:55:47 +08004797
Stefan Behrens472262f2012-11-06 14:43:46 +01004798 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4799 dev_replace->tgtdev != NULL) {
4800 int index_where_to_add;
4801 u64 srcdev_devid = dev_replace->srcdev->devid;
4802
4803 /*
4804 * duplicate the write operations while the dev replace
4805 * procedure is running. Since the copying of the old disk
4806 * to the new disk takes place at run time while the
4807 * filesystem is mounted writable, the regular write
4808 * operations to the old disk have to be duplicated to go
4809 * to the new disk as well.
4810 * Note that device->missing is handled by the caller, and
4811 * that the write to the old disk is already set up in the
4812 * stripes array.
4813 */
4814 index_where_to_add = num_stripes;
4815 for (i = 0; i < num_stripes; i++) {
4816 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4817 /* write to new disk, too */
4818 struct btrfs_bio_stripe *new =
4819 bbio->stripes + index_where_to_add;
4820 struct btrfs_bio_stripe *old =
4821 bbio->stripes + i;
4822
4823 new->physical = old->physical;
4824 new->length = old->length;
4825 new->dev = dev_replace->tgtdev;
4826 index_where_to_add++;
4827 max_errors++;
4828 }
4829 }
4830 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004831 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4832 dev_replace->tgtdev != NULL) {
4833 u64 srcdev_devid = dev_replace->srcdev->devid;
4834 int index_srcdev = 0;
4835 int found = 0;
4836 u64 physical_of_found = 0;
4837
4838 /*
4839 * During the dev-replace procedure, the target drive can
4840 * also be used to read data in case it is needed to repair
4841 * a corrupt block elsewhere. This is possible if the
4842 * requested area is left of the left cursor. In this area,
4843 * the target drive is a full copy of the source drive.
4844 */
4845 for (i = 0; i < num_stripes; i++) {
4846 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4847 /*
4848 * In case of DUP, in order to keep it
4849 * simple, only add the mirror with the
4850 * lowest physical address
4851 */
4852 if (found &&
4853 physical_of_found <=
4854 bbio->stripes[i].physical)
4855 continue;
4856 index_srcdev = i;
4857 found = 1;
4858 physical_of_found = bbio->stripes[i].physical;
4859 }
4860 }
4861 if (found) {
4862 u64 length = map->stripe_len;
4863
4864 if (physical_of_found + length <=
4865 dev_replace->cursor_left) {
4866 struct btrfs_bio_stripe *tgtdev_stripe =
4867 bbio->stripes + num_stripes;
4868
4869 tgtdev_stripe->physical = physical_of_found;
4870 tgtdev_stripe->length =
4871 bbio->stripes[index_srcdev].length;
4872 tgtdev_stripe->dev = dev_replace->tgtdev;
4873
4874 num_stripes++;
4875 }
4876 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004877 }
4878
Li Zefande11cc12011-12-01 12:55:47 +08004879 *bbio_ret = bbio;
4880 bbio->num_stripes = num_stripes;
4881 bbio->max_errors = max_errors;
4882 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004883
4884 /*
4885 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4886 * mirror_num == num_stripes + 1 && dev_replace target drive is
4887 * available as a mirror
4888 */
4889 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
4890 WARN_ON(num_stripes > 1);
4891 bbio->stripes[0].dev = dev_replace->tgtdev;
4892 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
4893 bbio->mirror_num = map->num_stripes + 1;
4894 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004895 if (raid_map) {
4896 sort_parity_stripes(bbio, raid_map);
4897 *raid_map_ret = raid_map;
4898 }
Chris Masoncea9e442008-04-09 16:28:12 -04004899out:
Stefan Behrens472262f2012-11-06 14:43:46 +01004900 if (dev_replace_is_ongoing)
4901 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04004902 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004903 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004904}
4905
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004906int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004907 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004908 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004909{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004910 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004911 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04004912}
4913
Yan Zhenga512bbf2008-12-08 16:46:26 -05004914int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4915 u64 chunk_start, u64 physical, u64 devid,
4916 u64 **logical, int *naddrs, int *stripe_len)
4917{
4918 struct extent_map_tree *em_tree = &map_tree->map_tree;
4919 struct extent_map *em;
4920 struct map_lookup *map;
4921 u64 *buf;
4922 u64 bytenr;
4923 u64 length;
4924 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05004925 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05004926 int i, j, nr = 0;
4927
Chris Mason890871b2009-09-02 16:24:52 -04004928 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004929 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004930 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004931
4932 BUG_ON(!em || em->start != chunk_start);
4933 map = (struct map_lookup *)em->bdev;
4934
4935 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05004936 rmap_len = map->stripe_len;
4937
Yan Zhenga512bbf2008-12-08 16:46:26 -05004938 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4939 do_div(length, map->num_stripes / map->sub_stripes);
4940 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4941 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05004942 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4943 BTRFS_BLOCK_GROUP_RAID6)) {
4944 do_div(length, nr_data_stripes(map));
4945 rmap_len = map->stripe_len * nr_data_stripes(map);
4946 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004947
4948 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004949 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004950
4951 for (i = 0; i < map->num_stripes; i++) {
4952 if (devid && map->stripes[i].dev->devid != devid)
4953 continue;
4954 if (map->stripes[i].physical > physical ||
4955 map->stripes[i].physical + length <= physical)
4956 continue;
4957
4958 stripe_nr = physical - map->stripes[i].physical;
4959 do_div(stripe_nr, map->stripe_len);
4960
4961 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4962 stripe_nr = stripe_nr * map->num_stripes + i;
4963 do_div(stripe_nr, map->sub_stripes);
4964 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4965 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05004966 } /* else if RAID[56], multiply by nr_data_stripes().
4967 * Alternatively, just use rmap_len below instead of
4968 * map->stripe_len */
4969
4970 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05004971 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004972 for (j = 0; j < nr; j++) {
4973 if (buf[j] == bytenr)
4974 break;
4975 }
Chris Mason934d3752008-12-08 16:43:10 -05004976 if (j == nr) {
4977 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004978 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004979 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004980 }
4981
Yan Zhenga512bbf2008-12-08 16:46:26 -05004982 *logical = buf;
4983 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05004984 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05004985
4986 free_extent_map(em);
4987 return 0;
4988}
4989
Stefan Behrens442a4f62012-05-25 16:06:08 +02004990static void *merge_stripe_index_into_bio_private(void *bi_private,
4991 unsigned int stripe_index)
4992{
4993 /*
4994 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4995 * at most 1.
4996 * The alternative solution (instead of stealing bits from the
4997 * pointer) would be to allocate an intermediate structure
4998 * that contains the old private pointer plus the stripe_index.
4999 */
5000 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
5001 BUG_ON(stripe_index > 3);
5002 return (void *)(((uintptr_t)bi_private) | stripe_index);
5003}
5004
5005static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
5006{
5007 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
5008}
5009
5010static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
5011{
5012 return (unsigned int)((uintptr_t)bi_private) & 3;
5013}
5014
Jan Schmidta1d3c472011-08-04 17:15:33 +02005015static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005016{
Stefan Behrens442a4f62012-05-25 16:06:08 +02005017 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005018 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005019
Stefan Behrens442a4f62012-05-25 16:06:08 +02005020 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005021 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005022 if (err == -EIO || err == -EREMOTEIO) {
5023 unsigned int stripe_index =
5024 extract_stripe_index_from_bio_private(
5025 bio->bi_private);
5026 struct btrfs_device *dev;
5027
5028 BUG_ON(stripe_index >= bbio->num_stripes);
5029 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005030 if (dev->bdev) {
5031 if (bio->bi_rw & WRITE)
5032 btrfs_dev_stat_inc(dev,
5033 BTRFS_DEV_STAT_WRITE_ERRS);
5034 else
5035 btrfs_dev_stat_inc(dev,
5036 BTRFS_DEV_STAT_READ_ERRS);
5037 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5038 btrfs_dev_stat_inc(dev,
5039 BTRFS_DEV_STAT_FLUSH_ERRS);
5040 btrfs_dev_stat_print_on_error(dev);
5041 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005042 }
5043 }
Chris Mason8790d502008-04-03 16:29:03 -04005044
Jan Schmidta1d3c472011-08-04 17:15:33 +02005045 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005046 is_orig_bio = 1;
5047
Jan Schmidta1d3c472011-08-04 17:15:33 +02005048 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005049 if (!is_orig_bio) {
5050 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005051 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005052 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005053 bio->bi_private = bbio->private;
5054 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02005055 bio->bi_bdev = (struct block_device *)
5056 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005057 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005058 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005059 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005060 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005061 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005062 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005063 /*
5064 * this bio is actually up to date, we didn't
5065 * go over the max number of errors
5066 */
5067 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005068 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005069 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005070 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04005071
5072 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005073 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005074 bio_put(bio);
5075 }
Chris Mason8790d502008-04-03 16:29:03 -04005076}
5077
Chris Mason8b712842008-06-11 16:50:36 -04005078struct async_sched {
5079 struct bio *bio;
5080 int rw;
5081 struct btrfs_fs_info *info;
5082 struct btrfs_work work;
5083};
5084
5085/*
5086 * see run_scheduled_bios for a description of why bios are collected for
5087 * async submit.
5088 *
5089 * This will add one bio to the pending list for a device and make sure
5090 * the work struct is scheduled.
5091 */
David Woodhouse53b381b2013-01-29 18:40:14 -05005092noinline void btrfs_schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04005093 struct btrfs_device *device,
5094 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005095{
5096 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005097 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005098
David Woodhouse53b381b2013-01-29 18:40:14 -05005099 if (device->missing || !device->bdev) {
5100 bio_endio(bio, -EIO);
5101 return;
5102 }
5103
Chris Mason8b712842008-06-11 16:50:36 -04005104 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005105 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005106 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005107 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005108 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005109 return;
Chris Mason8b712842008-06-11 16:50:36 -04005110 }
5111
5112 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005113 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005114 * higher layers. Otherwise, the async bio makes it appear we have
5115 * made progress against dirty pages when we've really just put it
5116 * on a queue for later
5117 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005118 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005119 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005120 bio->bi_next = NULL;
5121 bio->bi_rw |= rw;
5122
5123 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005124 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005125 pending_bios = &device->pending_sync_bios;
5126 else
5127 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005128
Chris Masonffbd5172009-04-20 15:50:09 -04005129 if (pending_bios->tail)
5130 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005131
Chris Masonffbd5172009-04-20 15:50:09 -04005132 pending_bios->tail = bio;
5133 if (!pending_bios->head)
5134 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005135 if (device->running_pending)
5136 should_queue = 0;
5137
5138 spin_unlock(&device->io_lock);
5139
5140 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04005141 btrfs_queue_worker(&root->fs_info->submit_workers,
5142 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005143}
5144
Josef Bacikde1ee922012-10-19 16:50:56 -04005145static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5146 sector_t sector)
5147{
5148 struct bio_vec *prev;
5149 struct request_queue *q = bdev_get_queue(bdev);
5150 unsigned short max_sectors = queue_max_sectors(q);
5151 struct bvec_merge_data bvm = {
5152 .bi_bdev = bdev,
5153 .bi_sector = sector,
5154 .bi_rw = bio->bi_rw,
5155 };
5156
5157 if (bio->bi_vcnt == 0) {
5158 WARN_ON(1);
5159 return 1;
5160 }
5161
5162 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
5163 if ((bio->bi_size >> 9) > max_sectors)
5164 return 0;
5165
5166 if (!q->merge_bvec_fn)
5167 return 1;
5168
5169 bvm.bi_size = bio->bi_size - prev->bv_len;
5170 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5171 return 0;
5172 return 1;
5173}
5174
5175static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5176 struct bio *bio, u64 physical, int dev_nr,
5177 int rw, int async)
5178{
5179 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5180
5181 bio->bi_private = bbio;
5182 bio->bi_private = merge_stripe_index_into_bio_private(
5183 bio->bi_private, (unsigned int)dev_nr);
5184 bio->bi_end_io = btrfs_end_bio;
5185 bio->bi_sector = physical >> 9;
5186#ifdef DEBUG
5187 {
5188 struct rcu_string *name;
5189
5190 rcu_read_lock();
5191 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005192 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005193 "(%s id %llu), size=%u\n", rw,
5194 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5195 name->str, dev->devid, bio->bi_size);
5196 rcu_read_unlock();
5197 }
5198#endif
5199 bio->bi_bdev = dev->bdev;
5200 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005201 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005202 else
5203 btrfsic_submit_bio(rw, bio);
5204}
5205
5206static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5207 struct bio *first_bio, struct btrfs_device *dev,
5208 int dev_nr, int rw, int async)
5209{
5210 struct bio_vec *bvec = first_bio->bi_io_vec;
5211 struct bio *bio;
5212 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5213 u64 physical = bbio->stripes[dev_nr].physical;
5214
5215again:
5216 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5217 if (!bio)
5218 return -ENOMEM;
5219
5220 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5221 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5222 bvec->bv_offset) < bvec->bv_len) {
5223 u64 len = bio->bi_size;
5224
5225 atomic_inc(&bbio->stripes_pending);
5226 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5227 rw, async);
5228 physical += len;
5229 goto again;
5230 }
5231 bvec++;
5232 }
5233
5234 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5235 return 0;
5236}
5237
5238static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5239{
5240 atomic_inc(&bbio->error);
5241 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5242 bio->bi_private = bbio->private;
5243 bio->bi_end_io = bbio->end_io;
5244 bio->bi_bdev = (struct block_device *)
5245 (unsigned long)bbio->mirror_num;
5246 bio->bi_sector = logical >> 9;
5247 kfree(bbio);
5248 bio_endio(bio, -EIO);
5249 }
5250}
5251
Chris Masonf1885912008-04-09 16:28:12 -04005252int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005253 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005254{
Chris Mason0b86a832008-03-24 15:01:56 -04005255 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005256 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04005257 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005258 u64 length = 0;
5259 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005260 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005261 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005262 int dev_nr = 0;
5263 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005264 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005265
Chris Masonf2d8d742008-04-21 10:03:05 -04005266 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005267 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005268
David Woodhouse53b381b2013-01-29 18:40:14 -05005269 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5270 mirror_num, &raid_map);
5271 if (ret) /* -ENOMEM */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005272 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04005273
Jan Schmidta1d3c472011-08-04 17:15:33 +02005274 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005275 bbio->orig_bio = first_bio;
5276 bbio->private = first_bio->bi_private;
5277 bbio->end_io = first_bio->bi_end_io;
5278 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5279
5280 if (raid_map) {
5281 /* In this case, map_length has been set to the length of
5282 a single stripe; not the whole write */
5283 if (rw & WRITE) {
5284 return raid56_parity_write(root, bio, bbio,
5285 raid_map, map_length);
5286 } else {
5287 return raid56_parity_recover(root, bio, bbio,
5288 raid_map, map_length,
5289 mirror_num);
5290 }
5291 }
5292
Chris Masoncea9e442008-04-09 16:28:12 -04005293 if (map_length < length) {
Daniel J Blueman48940662012-05-07 06:35:38 -06005294 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
Chris Masond3977122009-01-05 21:25:51 -05005295 "len %llu\n", (unsigned long long)logical,
5296 (unsigned long long)length,
5297 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005298 BUG();
5299 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005300
Chris Masond3977122009-01-05 21:25:51 -05005301 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005302 dev = bbio->stripes[dev_nr].dev;
5303 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5304 bbio_error(bbio, first_bio, logical);
5305 dev_nr++;
5306 continue;
5307 }
5308
5309 /*
5310 * Check and see if we're ok with this bio based on it's size
5311 * and offset with the given device.
5312 */
5313 if (!bio_size_ok(dev->bdev, first_bio,
5314 bbio->stripes[dev_nr].physical >> 9)) {
5315 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5316 dev_nr, rw, async_submit);
5317 BUG_ON(ret);
5318 dev_nr++;
5319 continue;
5320 }
5321
Jan Schmidta1d3c472011-08-04 17:15:33 +02005322 if (dev_nr < total_devs - 1) {
5323 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005324 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005325 } else {
5326 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04005327 }
Josef Bacik606686e2012-06-04 14:03:51 -04005328
Josef Bacikde1ee922012-10-19 16:50:56 -04005329 submit_stripe_bio(root, bbio, bio,
5330 bbio->stripes[dev_nr].physical, dev_nr, rw,
5331 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005332 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005333 }
Chris Mason0b86a832008-03-24 15:01:56 -04005334 return 0;
5335}
5336
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005337struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005338 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005339{
Yan Zheng2b820322008-11-17 21:11:30 -05005340 struct btrfs_device *device;
5341 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005342
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005343 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005344 while (cur_devices) {
5345 if (!fsid ||
5346 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5347 device = __find_device(&cur_devices->devices,
5348 devid, uuid);
5349 if (device)
5350 return device;
5351 }
5352 cur_devices = cur_devices->seed;
5353 }
5354 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005355}
5356
Chris Masondfe25022008-05-13 13:46:40 -04005357static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5358 u64 devid, u8 *dev_uuid)
5359{
5360 struct btrfs_device *device;
5361 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5362
5363 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005364 if (!device)
5365 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04005366 list_add(&device->dev_list,
5367 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04005368 device->dev_root = root->fs_info->dev_root;
5369 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04005370 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05005371 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05005372 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04005373 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05005374 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04005375 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05005376 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04005377 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
5378 return device;
5379}
5380
Chris Mason0b86a832008-03-24 15:01:56 -04005381static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5382 struct extent_buffer *leaf,
5383 struct btrfs_chunk *chunk)
5384{
5385 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5386 struct map_lookup *map;
5387 struct extent_map *em;
5388 u64 logical;
5389 u64 length;
5390 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005391 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005392 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005393 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005394 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005395
Chris Masone17cade2008-04-15 15:41:47 -04005396 logical = key->offset;
5397 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005398
Chris Mason890871b2009-09-02 16:24:52 -04005399 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005400 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005401 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005402
5403 /* already mapped? */
5404 if (em && em->start <= logical && em->start + em->len > logical) {
5405 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005406 return 0;
5407 } else if (em) {
5408 free_extent_map(em);
5409 }
Chris Mason0b86a832008-03-24 15:01:56 -04005410
David Sterba172ddd62011-04-21 00:48:27 +02005411 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005412 if (!em)
5413 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005414 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5415 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005416 if (!map) {
5417 free_extent_map(em);
5418 return -ENOMEM;
5419 }
5420
5421 em->bdev = (struct block_device *)map;
5422 em->start = logical;
5423 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005424 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005425 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005426 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005427
Chris Mason593060d2008-03-25 16:50:33 -04005428 map->num_stripes = num_stripes;
5429 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5430 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5431 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5432 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5433 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005434 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005435 for (i = 0; i < num_stripes; i++) {
5436 map->stripes[i].physical =
5437 btrfs_stripe_offset_nr(leaf, chunk, i);
5438 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005439 read_extent_buffer(leaf, uuid, (unsigned long)
5440 btrfs_stripe_dev_uuid_nr(chunk, i),
5441 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005442 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5443 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005444 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005445 kfree(map);
5446 free_extent_map(em);
5447 return -EIO;
5448 }
Chris Masondfe25022008-05-13 13:46:40 -04005449 if (!map->stripes[i].dev) {
5450 map->stripes[i].dev =
5451 add_missing_dev(root, devid, uuid);
5452 if (!map->stripes[i].dev) {
5453 kfree(map);
5454 free_extent_map(em);
5455 return -EIO;
5456 }
5457 }
5458 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005459 }
5460
Chris Mason890871b2009-09-02 16:24:52 -04005461 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005462 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005463 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005464 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005465 free_extent_map(em);
5466
5467 return 0;
5468}
5469
Jeff Mahoney143bede2012-03-01 14:56:26 +01005470static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005471 struct btrfs_dev_item *dev_item,
5472 struct btrfs_device *device)
5473{
5474 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005475
5476 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005477 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5478 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005479 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5480 device->type = btrfs_device_type(leaf, dev_item);
5481 device->io_align = btrfs_device_io_align(leaf, dev_item);
5482 device->io_width = btrfs_device_io_width(leaf, dev_item);
5483 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005484 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005485 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005486
5487 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005488 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005489}
5490
Yan Zheng2b820322008-11-17 21:11:30 -05005491static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5492{
5493 struct btrfs_fs_devices *fs_devices;
5494 int ret;
5495
Li Zefanb367e472011-12-07 11:38:24 +08005496 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005497
5498 fs_devices = root->fs_info->fs_devices->seed;
5499 while (fs_devices) {
5500 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5501 ret = 0;
5502 goto out;
5503 }
5504 fs_devices = fs_devices->seed;
5505 }
5506
5507 fs_devices = find_fsid(fsid);
5508 if (!fs_devices) {
5509 ret = -ENOENT;
5510 goto out;
5511 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005512
5513 fs_devices = clone_fs_devices(fs_devices);
5514 if (IS_ERR(fs_devices)) {
5515 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005516 goto out;
5517 }
5518
Christoph Hellwig97288f22008-12-02 06:36:09 -05005519 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005520 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005521 if (ret) {
5522 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005523 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005524 }
Yan Zheng2b820322008-11-17 21:11:30 -05005525
5526 if (!fs_devices->seeding) {
5527 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005528 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005529 ret = -EINVAL;
5530 goto out;
5531 }
5532
5533 fs_devices->seed = root->fs_info->fs_devices->seed;
5534 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005535out:
Yan Zheng2b820322008-11-17 21:11:30 -05005536 return ret;
5537}
5538
Chris Mason0d81ba52008-03-24 15:02:07 -04005539static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005540 struct extent_buffer *leaf,
5541 struct btrfs_dev_item *dev_item)
5542{
5543 struct btrfs_device *device;
5544 u64 devid;
5545 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005546 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005547 u8 dev_uuid[BTRFS_UUID_SIZE];
5548
Chris Mason0b86a832008-03-24 15:01:56 -04005549 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04005550 read_extent_buffer(leaf, dev_uuid,
5551 (unsigned long)btrfs_device_uuid(dev_item),
5552 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05005553 read_extent_buffer(leaf, fs_uuid,
5554 (unsigned long)btrfs_device_fsid(dev_item),
5555 BTRFS_UUID_SIZE);
5556
5557 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5558 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005559 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005560 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005561 }
5562
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005563 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005564 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005565 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005566 return -EIO;
5567
5568 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05005569 printk(KERN_WARNING "warning devid %llu missing\n",
5570 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005571 device = add_missing_dev(root, devid, dev_uuid);
5572 if (!device)
5573 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005574 } else if (!device->missing) {
5575 /*
5576 * this happens when a device that was properly setup
5577 * in the device info lists suddenly goes bad.
5578 * device->bdev is NULL, and so we have to set
5579 * device->missing to one here
5580 */
5581 root->fs_info->fs_devices->missing_devices++;
5582 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05005583 }
5584 }
5585
5586 if (device->fs_devices != root->fs_info->fs_devices) {
5587 BUG_ON(device->writeable);
5588 if (device->generation !=
5589 btrfs_device_generation(leaf, dev_item))
5590 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04005591 }
Chris Mason0b86a832008-03-24 15:01:56 -04005592
5593 fill_device_from_item(leaf, dev_item, device);
5594 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04005595 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01005596 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05005597 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04005598 spin_lock(&root->fs_info->free_chunk_lock);
5599 root->fs_info->free_chunk_space += device->total_bytes -
5600 device->bytes_used;
5601 spin_unlock(&root->fs_info->free_chunk_lock);
5602 }
Chris Mason0b86a832008-03-24 15:01:56 -04005603 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005604 return ret;
5605}
5606
Yan Zhenge4404d62008-12-12 10:03:26 -05005607int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04005608{
David Sterba6c417612011-04-13 15:41:04 +02005609 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04005610 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04005611 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04005612 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04005613 u8 *ptr;
5614 unsigned long sb_ptr;
5615 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005616 u32 num_stripes;
5617 u32 array_size;
5618 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005619 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04005620 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04005621
Yan Zhenge4404d62008-12-12 10:03:26 -05005622 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04005623 BTRFS_SUPER_INFO_SIZE);
5624 if (!sb)
5625 return -ENOMEM;
5626 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04005627 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02005628 /*
5629 * The sb extent buffer is artifical and just used to read the system array.
5630 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5631 * pages up-to-date when the page is larger: extent does not cover the
5632 * whole page and consequently check_page_uptodate does not find all
5633 * the page's extents up-to-date (the hole beyond sb),
5634 * write_extent_buffer then triggers a WARN_ON.
5635 *
5636 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5637 * but sb spans only this function. Add an explicit SetPageUptodate call
5638 * to silence the warning eg. on PowerPC 64.
5639 */
5640 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04005641 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05005642
Chris Masona061fc82008-05-07 11:43:44 -04005643 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005644 array_size = btrfs_super_sys_array_size(super_copy);
5645
Chris Mason0b86a832008-03-24 15:01:56 -04005646 ptr = super_copy->sys_chunk_array;
5647 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5648 cur = 0;
5649
5650 while (cur < array_size) {
5651 disk_key = (struct btrfs_disk_key *)ptr;
5652 btrfs_disk_key_to_cpu(&key, disk_key);
5653
Chris Masona061fc82008-05-07 11:43:44 -04005654 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04005655 sb_ptr += len;
5656 cur += len;
5657
Chris Mason0d81ba52008-03-24 15:02:07 -04005658 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04005659 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04005660 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04005661 if (ret)
5662 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005663 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5664 len = btrfs_chunk_item_size(num_stripes);
5665 } else {
Chris Mason84eed902008-04-25 09:04:37 -04005666 ret = -EIO;
5667 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005668 }
5669 ptr += len;
5670 sb_ptr += len;
5671 cur += len;
5672 }
Chris Masona061fc82008-05-07 11:43:44 -04005673 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04005674 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005675}
5676
5677int btrfs_read_chunk_tree(struct btrfs_root *root)
5678{
5679 struct btrfs_path *path;
5680 struct extent_buffer *leaf;
5681 struct btrfs_key key;
5682 struct btrfs_key found_key;
5683 int ret;
5684 int slot;
5685
5686 root = root->fs_info->chunk_root;
5687
5688 path = btrfs_alloc_path();
5689 if (!path)
5690 return -ENOMEM;
5691
Li Zefanb367e472011-12-07 11:38:24 +08005692 mutex_lock(&uuid_mutex);
5693 lock_chunks(root);
5694
Chris Mason0b86a832008-03-24 15:01:56 -04005695 /* first we search for all of the device items, and then we
5696 * read in all of the chunk items. This way we can create chunk
5697 * mappings that reference all of the devices that are afound
5698 */
5699 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5700 key.offset = 0;
5701 key.type = 0;
5702again:
5703 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005704 if (ret < 0)
5705 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005706 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005707 leaf = path->nodes[0];
5708 slot = path->slots[0];
5709 if (slot >= btrfs_header_nritems(leaf)) {
5710 ret = btrfs_next_leaf(root, path);
5711 if (ret == 0)
5712 continue;
5713 if (ret < 0)
5714 goto error;
5715 break;
5716 }
5717 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5718 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5719 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5720 break;
5721 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5722 struct btrfs_dev_item *dev_item;
5723 dev_item = btrfs_item_ptr(leaf, slot,
5724 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04005725 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05005726 if (ret)
5727 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005728 }
5729 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5730 struct btrfs_chunk *chunk;
5731 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5732 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005733 if (ret)
5734 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005735 }
5736 path->slots[0]++;
5737 }
5738 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5739 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005740 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005741 goto again;
5742 }
Chris Mason0b86a832008-03-24 15:01:56 -04005743 ret = 0;
5744error:
Li Zefanb367e472011-12-07 11:38:24 +08005745 unlock_chunks(root);
5746 mutex_unlock(&uuid_mutex);
5747
Yan Zheng2b820322008-11-17 21:11:30 -05005748 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005749 return ret;
5750}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005751
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005752static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5753{
5754 int i;
5755
5756 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5757 btrfs_dev_stat_reset(dev, i);
5758}
5759
5760int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5761{
5762 struct btrfs_key key;
5763 struct btrfs_key found_key;
5764 struct btrfs_root *dev_root = fs_info->dev_root;
5765 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5766 struct extent_buffer *eb;
5767 int slot;
5768 int ret = 0;
5769 struct btrfs_device *device;
5770 struct btrfs_path *path = NULL;
5771 int i;
5772
5773 path = btrfs_alloc_path();
5774 if (!path) {
5775 ret = -ENOMEM;
5776 goto out;
5777 }
5778
5779 mutex_lock(&fs_devices->device_list_mutex);
5780 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5781 int item_size;
5782 struct btrfs_dev_stats_item *ptr;
5783
5784 key.objectid = 0;
5785 key.type = BTRFS_DEV_STATS_KEY;
5786 key.offset = device->devid;
5787 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5788 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005789 __btrfs_reset_dev_stats(device);
5790 device->dev_stats_valid = 1;
5791 btrfs_release_path(path);
5792 continue;
5793 }
5794 slot = path->slots[0];
5795 eb = path->nodes[0];
5796 btrfs_item_key_to_cpu(eb, &found_key, slot);
5797 item_size = btrfs_item_size_nr(eb, slot);
5798
5799 ptr = btrfs_item_ptr(eb, slot,
5800 struct btrfs_dev_stats_item);
5801
5802 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5803 if (item_size >= (1 + i) * sizeof(__le64))
5804 btrfs_dev_stat_set(device, i,
5805 btrfs_dev_stats_value(eb, ptr, i));
5806 else
5807 btrfs_dev_stat_reset(device, i);
5808 }
5809
5810 device->dev_stats_valid = 1;
5811 btrfs_dev_stat_print_on_load(device);
5812 btrfs_release_path(path);
5813 }
5814 mutex_unlock(&fs_devices->device_list_mutex);
5815
5816out:
5817 btrfs_free_path(path);
5818 return ret < 0 ? ret : 0;
5819}
5820
5821static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5822 struct btrfs_root *dev_root,
5823 struct btrfs_device *device)
5824{
5825 struct btrfs_path *path;
5826 struct btrfs_key key;
5827 struct extent_buffer *eb;
5828 struct btrfs_dev_stats_item *ptr;
5829 int ret;
5830 int i;
5831
5832 key.objectid = 0;
5833 key.type = BTRFS_DEV_STATS_KEY;
5834 key.offset = device->devid;
5835
5836 path = btrfs_alloc_path();
5837 BUG_ON(!path);
5838 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5839 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005840 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5841 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005842 goto out;
5843 }
5844
5845 if (ret == 0 &&
5846 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5847 /* need to delete old one and insert a new one */
5848 ret = btrfs_del_item(trans, dev_root, path);
5849 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005850 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5851 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005852 goto out;
5853 }
5854 ret = 1;
5855 }
5856
5857 if (ret == 1) {
5858 /* need to insert a new item */
5859 btrfs_release_path(path);
5860 ret = btrfs_insert_empty_item(trans, dev_root, path,
5861 &key, sizeof(*ptr));
5862 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005863 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5864 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005865 goto out;
5866 }
5867 }
5868
5869 eb = path->nodes[0];
5870 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5871 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5872 btrfs_set_dev_stats_value(eb, ptr, i,
5873 btrfs_dev_stat_read(device, i));
5874 btrfs_mark_buffer_dirty(eb);
5875
5876out:
5877 btrfs_free_path(path);
5878 return ret;
5879}
5880
5881/*
5882 * called from commit_transaction. Writes all changed device stats to disk.
5883 */
5884int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5885 struct btrfs_fs_info *fs_info)
5886{
5887 struct btrfs_root *dev_root = fs_info->dev_root;
5888 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5889 struct btrfs_device *device;
5890 int ret = 0;
5891
5892 mutex_lock(&fs_devices->device_list_mutex);
5893 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5894 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5895 continue;
5896
5897 ret = update_dev_stat_item(trans, dev_root, device);
5898 if (!ret)
5899 device->dev_stats_dirty = 0;
5900 }
5901 mutex_unlock(&fs_devices->device_list_mutex);
5902
5903 return ret;
5904}
5905
Stefan Behrens442a4f62012-05-25 16:06:08 +02005906void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5907{
5908 btrfs_dev_stat_inc(dev, index);
5909 btrfs_dev_stat_print_on_error(dev);
5910}
5911
5912void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5913{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005914 if (!dev->dev_stats_valid)
5915 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005916 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005917 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005918 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005919 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5920 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5921 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5922 btrfs_dev_stat_read(dev,
5923 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5924 btrfs_dev_stat_read(dev,
5925 BTRFS_DEV_STAT_GENERATION_ERRS));
5926}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005927
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005928static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5929{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005930 int i;
5931
5932 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5933 if (btrfs_dev_stat_read(dev, i) != 0)
5934 break;
5935 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5936 return; /* all values == 0, suppress message */
5937
Josef Bacik606686e2012-06-04 14:03:51 -04005938 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5939 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005940 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5941 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5942 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5943 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5944 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5945}
5946
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005947int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005948 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005949{
5950 struct btrfs_device *dev;
5951 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5952 int i;
5953
5954 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005955 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005956 mutex_unlock(&fs_devices->device_list_mutex);
5957
5958 if (!dev) {
5959 printk(KERN_WARNING
5960 "btrfs: get dev_stats failed, device not found\n");
5961 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005962 } else if (!dev->dev_stats_valid) {
5963 printk(KERN_WARNING
5964 "btrfs: get dev_stats failed, not yet valid\n");
5965 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005966 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005967 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5968 if (stats->nr_items > i)
5969 stats->values[i] =
5970 btrfs_dev_stat_read_and_reset(dev, i);
5971 else
5972 btrfs_dev_stat_reset(dev, i);
5973 }
5974 } else {
5975 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5976 if (stats->nr_items > i)
5977 stats->values[i] = btrfs_dev_stat_read(dev, i);
5978 }
5979 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5980 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5981 return 0;
5982}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01005983
5984int btrfs_scratch_superblock(struct btrfs_device *device)
5985{
5986 struct buffer_head *bh;
5987 struct btrfs_super_block *disk_super;
5988
5989 bh = btrfs_read_dev_super(device->bdev);
5990 if (!bh)
5991 return -EINVAL;
5992 disk_super = (struct btrfs_super_block *)bh->b_data;
5993
5994 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5995 set_buffer_dirty(bh);
5996 sync_dirty_buffer(bh);
5997 brelse(bh);
5998
5999 return 0;
6000}