blob: f492f7eabf29e9df47ba88f7d8c0e230066d515c [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>
Stefan Behrens803b2f52013-08-15 17:11:21 +020029#include <linux/semaphore.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050030#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050031#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "ctree.h"
33#include "extent_map.h"
34#include "disk-io.h"
35#include "transaction.h"
36#include "print-tree.h"
37#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050038#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040039#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010040#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040041#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060042#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010043#include "dev-replace.h"
Chris Mason0b86a832008-03-24 15:01:56 -040044
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020049static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +000050static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020051static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050052
Chris Mason8a4b83c2008-03-24 15:02:07 -040053static DEFINE_MUTEX(uuid_mutex);
54static LIST_HEAD(fs_uuids);
55
Chris Mason7d9eb122008-07-08 14:19:17 -040056static void lock_chunks(struct btrfs_root *root)
57{
Chris Mason7d9eb122008-07-08 14:19:17 -040058 mutex_lock(&root->fs_info->chunk_mutex);
59}
60
61static void unlock_chunks(struct btrfs_root *root)
62{
Chris Mason7d9eb122008-07-08 14:19:17 -040063 mutex_unlock(&root->fs_info->chunk_mutex);
64}
65
Ilya Dryomov2208a372013-08-12 14:33:03 +030066static struct btrfs_fs_devices *__alloc_fs_devices(void)
67{
68 struct btrfs_fs_devices *fs_devs;
69
70 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
71 if (!fs_devs)
72 return ERR_PTR(-ENOMEM);
73
74 mutex_init(&fs_devs->device_list_mutex);
75
76 INIT_LIST_HEAD(&fs_devs->devices);
77 INIT_LIST_HEAD(&fs_devs->alloc_list);
78 INIT_LIST_HEAD(&fs_devs->list);
79
80 return fs_devs;
81}
82
83/**
84 * alloc_fs_devices - allocate struct btrfs_fs_devices
85 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
86 * generated.
87 *
88 * Return: a pointer to a new &struct btrfs_fs_devices on success;
89 * ERR_PTR() on error. Returned struct is not linked onto any lists and
90 * can be destroyed with kfree() right away.
91 */
92static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
93{
94 struct btrfs_fs_devices *fs_devs;
95
96 fs_devs = __alloc_fs_devices();
97 if (IS_ERR(fs_devs))
98 return fs_devs;
99
100 if (fsid)
101 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
102 else
103 generate_random_uuid(fs_devs->fsid);
104
105 return fs_devs;
106}
107
Yan Zhenge4404d62008-12-12 10:03:26 -0500108static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
109{
110 struct btrfs_device *device;
111 WARN_ON(fs_devices->opened);
112 while (!list_empty(&fs_devices->devices)) {
113 device = list_entry(fs_devices->devices.next,
114 struct btrfs_device, dev_list);
115 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400116 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500117 kfree(device);
118 }
119 kfree(fs_devices);
120}
121
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000122static void btrfs_kobject_uevent(struct block_device *bdev,
123 enum kobject_action action)
124{
125 int ret;
126
127 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
128 if (ret)
129 pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
130 action,
131 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
132 &disk_to_dev(bdev->bd_disk)->kobj);
133}
134
Jeff Mahoney143bede2012-03-01 14:56:26 +0100135void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400136{
137 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400138
Yan Zheng2b820322008-11-17 21:11:30 -0500139 while (!list_empty(&fs_uuids)) {
140 fs_devices = list_entry(fs_uuids.next,
141 struct btrfs_fs_devices, list);
142 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500143 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400144 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400145}
146
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300147static struct btrfs_device *__alloc_device(void)
148{
149 struct btrfs_device *dev;
150
151 dev = kzalloc(sizeof(*dev), GFP_NOFS);
152 if (!dev)
153 return ERR_PTR(-ENOMEM);
154
155 INIT_LIST_HEAD(&dev->dev_list);
156 INIT_LIST_HEAD(&dev->dev_alloc_list);
157
158 spin_lock_init(&dev->io_lock);
159
160 spin_lock_init(&dev->reada_lock);
161 atomic_set(&dev->reada_in_flight, 0);
162 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
163 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
164
165 return dev;
166}
167
Chris Masona1b32a52008-09-05 16:09:51 -0400168static noinline struct btrfs_device *__find_device(struct list_head *head,
169 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400170{
171 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400172
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500173 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400174 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400175 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400176 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400177 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400178 }
179 return NULL;
180}
181
Chris Masona1b32a52008-09-05 16:09:51 -0400182static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400183{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400184 struct btrfs_fs_devices *fs_devices;
185
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500186 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400187 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
188 return fs_devices;
189 }
190 return NULL;
191}
192
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100193static int
194btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
195 int flush, struct block_device **bdev,
196 struct buffer_head **bh)
197{
198 int ret;
199
200 *bdev = blkdev_get_by_path(device_path, flags, holder);
201
202 if (IS_ERR(*bdev)) {
203 ret = PTR_ERR(*bdev);
204 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
205 goto error;
206 }
207
208 if (flush)
209 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
210 ret = set_blocksize(*bdev, 4096);
211 if (ret) {
212 blkdev_put(*bdev, flags);
213 goto error;
214 }
215 invalidate_bdev(*bdev);
216 *bh = btrfs_read_dev_super(*bdev);
217 if (!*bh) {
218 ret = -EINVAL;
219 blkdev_put(*bdev, flags);
220 goto error;
221 }
222
223 return 0;
224
225error:
226 *bdev = NULL;
227 *bh = NULL;
228 return ret;
229}
230
Chris Masonffbd5172009-04-20 15:50:09 -0400231static void requeue_list(struct btrfs_pending_bios *pending_bios,
232 struct bio *head, struct bio *tail)
233{
234
235 struct bio *old_head;
236
237 old_head = pending_bios->head;
238 pending_bios->head = head;
239 if (pending_bios->tail)
240 tail->bi_next = old_head;
241 else
242 pending_bios->tail = tail;
243}
244
Chris Mason8b712842008-06-11 16:50:36 -0400245/*
246 * we try to collect pending bios for a device so we don't get a large
247 * number of procs sending bios down to the same device. This greatly
248 * improves the schedulers ability to collect and merge the bios.
249 *
250 * But, it also turns into a long list of bios to process and that is sure
251 * to eventually make the worker thread block. The solution here is to
252 * make some progress and then put this work struct back at the end of
253 * the list if the block device is congested. This way, multiple devices
254 * can make progress from a single worker thread.
255 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100256static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400257{
258 struct bio *pending;
259 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400260 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400261 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400262 struct bio *tail;
263 struct bio *cur;
264 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400265 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400266 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400267 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400268 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400269 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000270 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400271 struct blk_plug plug;
272
273 /*
274 * this function runs all the bios we've collected for
275 * a particular device. We don't want to wander off to
276 * another device without first sending all of these down.
277 * So, setup a plug here and finish it off before we return
278 */
279 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400280
Chris Masonbedf7622009-04-03 10:32:58 -0400281 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400282 fs_info = device->dev_root->fs_info;
283 limit = btrfs_async_submit_limit(fs_info);
284 limit = limit * 2 / 3;
285
Chris Mason8b712842008-06-11 16:50:36 -0400286loop:
287 spin_lock(&device->io_lock);
288
Chris Masona6837052009-02-04 09:19:41 -0500289loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400290 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400291
Chris Mason8b712842008-06-11 16:50:36 -0400292 /* take all the bios off the list at once and process them
293 * later on (without the lock held). But, remember the
294 * tail and other pointers so the bios can be properly reinserted
295 * into the list if we hit congestion
296 */
Chris Masond84275c2009-06-09 15:39:08 -0400297 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400298 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400299 force_reg = 1;
300 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400301 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400302 force_reg = 0;
303 }
Chris Masonffbd5172009-04-20 15:50:09 -0400304
305 pending = pending_bios->head;
306 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400307 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400308
309 /*
310 * if pending was null this time around, no bios need processing
311 * at all and we can stop. Otherwise it'll loop back up again
312 * and do an additional check so no bios are missed.
313 *
314 * device->running_pending is used to synchronize with the
315 * schedule_bio code.
316 */
Chris Masonffbd5172009-04-20 15:50:09 -0400317 if (device->pending_sync_bios.head == NULL &&
318 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400319 again = 0;
320 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400321 } else {
322 again = 1;
323 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400324 }
Chris Masonffbd5172009-04-20 15:50:09 -0400325
326 pending_bios->head = NULL;
327 pending_bios->tail = NULL;
328
Chris Mason8b712842008-06-11 16:50:36 -0400329 spin_unlock(&device->io_lock);
330
Chris Masond3977122009-01-05 21:25:51 -0500331 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400332
333 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400334 /* we want to work on both lists, but do more bios on the
335 * sync list than the regular list
336 */
337 if ((num_run > 32 &&
338 pending_bios != &device->pending_sync_bios &&
339 device->pending_sync_bios.head) ||
340 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
341 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400342 spin_lock(&device->io_lock);
343 requeue_list(pending_bios, pending, tail);
344 goto loop_lock;
345 }
346
Chris Mason8b712842008-06-11 16:50:36 -0400347 cur = pending;
348 pending = pending->bi_next;
349 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400350
Josef Bacik66657b32012-08-01 15:36:24 -0400351 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400352 waitqueue_active(&fs_info->async_submit_wait))
353 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400354
355 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400356
Chris Mason2ab1ba62011-08-04 14:28:36 -0400357 /*
358 * if we're doing the sync list, record that our
359 * plug has some sync requests on it
360 *
361 * If we're doing the regular list and there are
362 * sync requests sitting around, unplug before
363 * we add more
364 */
365 if (pending_bios == &device->pending_sync_bios) {
366 sync_pending = 1;
367 } else if (sync_pending) {
368 blk_finish_plug(&plug);
369 blk_start_plug(&plug);
370 sync_pending = 0;
371 }
372
Stefan Behrens21adbd52011-11-09 13:44:05 +0100373 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400374 num_run++;
375 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100376 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400377 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400378
379 /*
380 * we made progress, there is more work to do and the bdi
381 * is now congested. Back off and let other work structs
382 * run instead
383 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400384 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500385 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400386 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400387
Chris Masonb765ead2009-04-03 10:27:10 -0400388 ioc = current->io_context;
389
390 /*
391 * the main goal here is that we don't want to
392 * block if we're going to be able to submit
393 * more requests without blocking.
394 *
395 * This code does two great things, it pokes into
396 * the elevator code from a filesystem _and_
397 * it makes assumptions about how batching works.
398 */
399 if (ioc && ioc->nr_batch_requests > 0 &&
400 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
401 (last_waited == 0 ||
402 ioc->last_waited == last_waited)) {
403 /*
404 * we want to go through our batch of
405 * requests and stop. So, we copy out
406 * the ioc->last_waited time and test
407 * against it before looping
408 */
409 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100410 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400411 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400412 continue;
413 }
Chris Mason8b712842008-06-11 16:50:36 -0400414 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400415 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500416 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400417
418 spin_unlock(&device->io_lock);
419 btrfs_requeue_work(&device->work);
420 goto done;
421 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500422 /* unplug every 64 requests just for good measure */
423 if (batch_run % 64 == 0) {
424 blk_finish_plug(&plug);
425 blk_start_plug(&plug);
426 sync_pending = 0;
427 }
Chris Mason8b712842008-06-11 16:50:36 -0400428 }
Chris Masonffbd5172009-04-20 15:50:09 -0400429
Chris Mason51684082010-03-10 15:33:32 -0500430 cond_resched();
431 if (again)
432 goto loop;
433
434 spin_lock(&device->io_lock);
435 if (device->pending_bios.head || device->pending_sync_bios.head)
436 goto loop_lock;
437 spin_unlock(&device->io_lock);
438
Chris Mason8b712842008-06-11 16:50:36 -0400439done:
Chris Mason211588a2011-04-19 20:12:40 -0400440 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400441}
442
Christoph Hellwigb2950862008-12-02 09:54:17 -0500443static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400444{
445 struct btrfs_device *device;
446
447 device = container_of(work, struct btrfs_device, work);
448 run_scheduled_bios(device);
449}
450
Chris Masona1b32a52008-09-05 16:09:51 -0400451static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400452 struct btrfs_super_block *disk_super,
453 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
454{
455 struct btrfs_device *device;
456 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400457 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400458 u64 found_transid = btrfs_super_generation(disk_super);
459
460 fs_devices = find_fsid(disk_super->fsid);
461 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300462 fs_devices = alloc_fs_devices(disk_super->fsid);
463 if (IS_ERR(fs_devices))
464 return PTR_ERR(fs_devices);
465
Chris Mason8a4b83c2008-03-24 15:02:07 -0400466 list_add(&fs_devices->list, &fs_uuids);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400467 fs_devices->latest_devid = devid;
468 fs_devices->latest_trans = found_transid;
Ilya Dryomov2208a372013-08-12 14:33:03 +0300469
Chris Mason8a4b83c2008-03-24 15:02:07 -0400470 device = NULL;
471 } else {
Chris Masona4437552008-04-18 10:29:38 -0400472 device = __find_device(&fs_devices->devices, devid,
473 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400474 }
475 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500476 if (fs_devices->opened)
477 return -EBUSY;
478
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300479 device = btrfs_alloc_device(NULL, &devid,
480 disk_super->dev_item.uuid);
481 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400482 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300483 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400484 }
Josef Bacik606686e2012-06-04 14:03:51 -0400485
486 name = rcu_string_strdup(path, GFP_NOFS);
487 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400488 kfree(device);
489 return -ENOMEM;
490 }
Josef Bacik606686e2012-06-04 14:03:51 -0400491 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200492
Chris Masone5e9a522009-06-10 15:17:02 -0400493 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000494 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100495 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400496 mutex_unlock(&fs_devices->device_list_mutex);
497
Yan Zheng2b820322008-11-17 21:11:30 -0500498 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400499 } else if (!device->name || strcmp(device->name->str, path)) {
500 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000501 if (!name)
502 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400503 rcu_string_free(device->name);
504 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500505 if (device->missing) {
506 fs_devices->missing_devices--;
507 device->missing = 0;
508 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400509 }
510
511 if (found_transid > fs_devices->latest_trans) {
512 fs_devices->latest_devid = devid;
513 fs_devices->latest_trans = found_transid;
514 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400515 *fs_devices_ret = fs_devices;
516 return 0;
517}
518
Yan Zhenge4404d62008-12-12 10:03:26 -0500519static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
520{
521 struct btrfs_fs_devices *fs_devices;
522 struct btrfs_device *device;
523 struct btrfs_device *orig_dev;
524
Ilya Dryomov2208a372013-08-12 14:33:03 +0300525 fs_devices = alloc_fs_devices(orig->fsid);
526 if (IS_ERR(fs_devices))
527 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500528
Yan Zhenge4404d62008-12-12 10:03:26 -0500529 fs_devices->latest_devid = orig->latest_devid;
530 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400531 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500532
Xiao Guangrong46224702011-04-20 10:08:47 +0000533 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500534 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400535 struct rcu_string *name;
536
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300537 device = btrfs_alloc_device(NULL, &orig_dev->devid,
538 orig_dev->uuid);
539 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500540 goto error;
541
Josef Bacik606686e2012-06-04 14:03:51 -0400542 /*
543 * This is ok to do without rcu read locked because we hold the
544 * uuid mutex so nothing we touch in here is going to disappear.
545 */
546 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
547 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400548 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500549 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400550 }
Josef Bacik606686e2012-06-04 14:03:51 -0400551 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500552
Yan Zhenge4404d62008-12-12 10:03:26 -0500553 list_add(&device->dev_list, &fs_devices->devices);
554 device->fs_devices = fs_devices;
555 fs_devices->num_devices++;
556 }
557 return fs_devices;
558error:
559 free_fs_devices(fs_devices);
560 return ERR_PTR(-ENOMEM);
561}
562
Stefan Behrens8dabb742012-11-06 13:15:27 +0100563void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
564 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400565{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500566 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400567
Chris Masona6b0d5c2012-02-20 20:53:43 -0500568 struct block_device *latest_bdev = NULL;
569 u64 latest_devid = 0;
570 u64 latest_transid = 0;
571
Chris Masondfe25022008-05-13 13:46:40 -0400572 mutex_lock(&uuid_mutex);
573again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000574 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500575 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500576 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100577 if (!device->is_tgtdev_for_dev_replace &&
578 (!latest_transid ||
579 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500580 latest_devid = device->devid;
581 latest_transid = device->generation;
582 latest_bdev = device->bdev;
583 }
Yan Zheng2b820322008-11-17 21:11:30 -0500584 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500585 }
Yan Zheng2b820322008-11-17 21:11:30 -0500586
Stefan Behrens8dabb742012-11-06 13:15:27 +0100587 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
588 /*
589 * In the first step, keep the device which has
590 * the correct fsid and the devid that is used
591 * for the dev_replace procedure.
592 * In the second step, the dev_replace state is
593 * read from the device tree and it is known
594 * whether the procedure is really active or
595 * not, which means whether this device is
596 * used or whether it should be removed.
597 */
598 if (step == 0 || device->is_tgtdev_for_dev_replace) {
599 continue;
600 }
601 }
Yan Zheng2b820322008-11-17 21:11:30 -0500602 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100603 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500604 device->bdev = NULL;
605 fs_devices->open_devices--;
606 }
607 if (device->writeable) {
608 list_del_init(&device->dev_alloc_list);
609 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100610 if (!device->is_tgtdev_for_dev_replace)
611 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500612 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500613 list_del_init(&device->dev_list);
614 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400615 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500616 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400617 }
Yan Zheng2b820322008-11-17 21:11:30 -0500618
619 if (fs_devices->seed) {
620 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500621 goto again;
622 }
623
Chris Masona6b0d5c2012-02-20 20:53:43 -0500624 fs_devices->latest_bdev = latest_bdev;
625 fs_devices->latest_devid = latest_devid;
626 fs_devices->latest_trans = latest_transid;
627
Chris Masondfe25022008-05-13 13:46:40 -0400628 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400629}
Chris Masona0af4692008-05-13 16:03:06 -0400630
Xiao Guangrong1f781602011-04-20 10:09:16 +0000631static void __free_device(struct work_struct *work)
632{
633 struct btrfs_device *device;
634
635 device = container_of(work, struct btrfs_device, rcu_work);
636
637 if (device->bdev)
638 blkdev_put(device->bdev, device->mode);
639
Josef Bacik606686e2012-06-04 14:03:51 -0400640 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000641 kfree(device);
642}
643
644static void free_device(struct rcu_head *head)
645{
646 struct btrfs_device *device;
647
648 device = container_of(head, struct btrfs_device, rcu);
649
650 INIT_WORK(&device->rcu_work, __free_device);
651 schedule_work(&device->rcu_work);
652}
653
Yan Zheng2b820322008-11-17 21:11:30 -0500654static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400655{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400656 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500657
Yan Zheng2b820322008-11-17 21:11:30 -0500658 if (--fs_devices->opened > 0)
659 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400660
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000661 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500662 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000663 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400664 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000665
666 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400667 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000668
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300669 if (device->writeable &&
670 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500671 list_del_init(&device->dev_alloc_list);
672 fs_devices->rw_devices--;
673 }
674
Josef Bacikd5e20032011-08-04 14:52:27 +0000675 if (device->can_discard)
676 fs_devices->num_can_discard--;
Josef Bacik726551e2013-08-26 13:45:53 -0400677 if (device->missing)
678 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000679
Ilya Dryomova1e87802013-08-12 14:33:04 +0300680 new_device = btrfs_alloc_device(NULL, &device->devid,
681 device->uuid);
682 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400683
684 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400685 if (device->name) {
686 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300687 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400688 rcu_assign_pointer(new_device->name, name);
689 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300690
Xiao Guangrong1f781602011-04-20 10:09:16 +0000691 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300692 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000693
694 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400695 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000696 mutex_unlock(&fs_devices->device_list_mutex);
697
Yan Zhenge4404d62008-12-12 10:03:26 -0500698 WARN_ON(fs_devices->open_devices);
699 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500700 fs_devices->opened = 0;
701 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500702
Chris Mason8a4b83c2008-03-24 15:02:07 -0400703 return 0;
704}
705
Yan Zheng2b820322008-11-17 21:11:30 -0500706int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
707{
Yan Zhenge4404d62008-12-12 10:03:26 -0500708 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500709 int ret;
710
711 mutex_lock(&uuid_mutex);
712 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500713 if (!fs_devices->opened) {
714 seed_devices = fs_devices->seed;
715 fs_devices->seed = NULL;
716 }
Yan Zheng2b820322008-11-17 21:11:30 -0500717 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500718
719 while (seed_devices) {
720 fs_devices = seed_devices;
721 seed_devices = fs_devices->seed;
722 __btrfs_close_devices(fs_devices);
723 free_fs_devices(fs_devices);
724 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000725 /*
726 * Wait for rcu kworkers under __btrfs_close_devices
727 * to finish all blkdev_puts so device is really
728 * free when umount is done.
729 */
730 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500731 return ret;
732}
733
Yan Zhenge4404d62008-12-12 10:03:26 -0500734static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
735 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400736{
Josef Bacikd5e20032011-08-04 14:52:27 +0000737 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400738 struct block_device *bdev;
739 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400740 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400741 struct block_device *latest_bdev = NULL;
742 struct buffer_head *bh;
743 struct btrfs_super_block *disk_super;
744 u64 latest_devid = 0;
745 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400746 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500747 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400748 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400749
Tejun Heod4d77622010-11-13 11:55:18 +0100750 flags |= FMODE_EXCL;
751
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500752 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400753 if (device->bdev)
754 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400755 if (!device->name)
756 continue;
757
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000758 /* Just open everything we can; ignore failures here */
759 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
760 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100761 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400762
763 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000764 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400765 if (devid != device->devid)
766 goto error_brelse;
767
Yan Zheng2b820322008-11-17 21:11:30 -0500768 if (memcmp(device->uuid, disk_super->dev_item.uuid,
769 BTRFS_UUID_SIZE))
770 goto error_brelse;
771
772 device->generation = btrfs_super_generation(disk_super);
773 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400774 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500775 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400776 latest_bdev = bdev;
777 }
778
Yan Zheng2b820322008-11-17 21:11:30 -0500779 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
780 device->writeable = 0;
781 } else {
782 device->writeable = !bdev_read_only(bdev);
783 seeding = 0;
784 }
785
Josef Bacikd5e20032011-08-04 14:52:27 +0000786 q = bdev_get_queue(bdev);
787 if (blk_queue_discard(q)) {
788 device->can_discard = 1;
789 fs_devices->num_can_discard++;
790 }
791
Chris Mason8a4b83c2008-03-24 15:02:07 -0400792 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400793 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500794 device->mode = flags;
795
Chris Masonc2898112009-06-10 09:51:32 -0400796 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
797 fs_devices->rotating = 1;
798
Chris Masona0af4692008-05-13 16:03:06 -0400799 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300800 if (device->writeable &&
801 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500802 fs_devices->rw_devices++;
803 list_add(&device->dev_alloc_list,
804 &fs_devices->alloc_list);
805 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000806 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400807 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400808
Chris Masona0af4692008-05-13 16:03:06 -0400809error_brelse:
810 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100811 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400812 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400813 }
Chris Masona0af4692008-05-13 16:03:06 -0400814 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300815 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400816 goto out;
817 }
Yan Zheng2b820322008-11-17 21:11:30 -0500818 fs_devices->seeding = seeding;
819 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400820 fs_devices->latest_bdev = latest_bdev;
821 fs_devices->latest_devid = latest_devid;
822 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500823 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400824out:
Yan Zheng2b820322008-11-17 21:11:30 -0500825 return ret;
826}
827
828int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500829 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500830{
831 int ret;
832
833 mutex_lock(&uuid_mutex);
834 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500835 fs_devices->opened++;
836 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500837 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500838 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500839 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400840 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400841 return ret;
842}
843
David Sterba6f60cbd2013-02-15 11:31:02 -0700844/*
845 * Look for a btrfs signature on a device. This may be called out of the mount path
846 * and we are not allowed to call set_blocksize during the scan. The superblock
847 * is read via pagecache
848 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500849int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400850 struct btrfs_fs_devices **fs_devices_ret)
851{
852 struct btrfs_super_block *disk_super;
853 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700854 struct page *page;
855 void *p;
856 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400857 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400858 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400859 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700860 u64 bytenr;
861 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400862
David Sterba6f60cbd2013-02-15 11:31:02 -0700863 /*
864 * we would like to check all the supers, but that would make
865 * a btrfs mount succeed after a mkfs from a different FS.
866 * So, we need to add a special mount option to scan for
867 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
868 */
869 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100870 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500871 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700872
873 bdev = blkdev_get_by_path(path, flags, holder);
874
875 if (IS_ERR(bdev)) {
876 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100877 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700878 }
879
880 /* make sure our super fits in the device */
881 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
882 goto error_bdev_put;
883
884 /* make sure our super fits in the page */
885 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
886 goto error_bdev_put;
887
888 /* make sure our super doesn't straddle pages on disk */
889 index = bytenr >> PAGE_CACHE_SHIFT;
890 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
891 goto error_bdev_put;
892
893 /* pull in the page with our super */
894 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
895 index, GFP_NOFS);
896
897 if (IS_ERR_OR_NULL(page))
898 goto error_bdev_put;
899
900 p = kmap(page);
901
902 /* align our pointer to the offset of the super block */
903 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
904
905 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800906 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700907 goto error_unmap;
908
Xiao Guangronga3438322010-01-06 11:48:18 +0000909 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400910 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400911 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700912
Stefan Behrensd03f9182012-11-05 13:10:49 +0000913 if (disk_super->label[0]) {
914 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
915 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Frank Holton5138ccc2013-09-13 11:46:50 -0400916 printk(KERN_INFO "btrfs: device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000917 } else {
Frank Holton5138ccc2013-09-13 11:46:50 -0400918 printk(KERN_INFO "btrfs: device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000919 }
David Sterba6f60cbd2013-02-15 11:31:02 -0700920
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200921 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
David Sterba6f60cbd2013-02-15 11:31:02 -0700922
Chris Mason8a4b83c2008-03-24 15:02:07 -0400923 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400924 if (!ret && fs_devices_ret)
925 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700926
927error_unmap:
928 kunmap(page);
929 page_cache_release(page);
930
931error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100932 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400933error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100934 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400935 return ret;
936}
Chris Mason0b86a832008-03-24 15:01:56 -0400937
Miao Xie6d07bce2011-01-05 10:07:31 +0000938/* helper to account the used device space in the range */
939int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
940 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400941{
942 struct btrfs_key key;
943 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000944 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500945 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000946 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400947 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000948 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400949 struct extent_buffer *l;
950
Miao Xie6d07bce2011-01-05 10:07:31 +0000951 *length = 0;
952
Stefan Behrens63a212a2012-11-05 18:29:28 +0100953 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000954 return 0;
955
Yan Zheng2b820322008-11-17 21:11:30 -0500956 path = btrfs_alloc_path();
957 if (!path)
958 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400959 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400960
Chris Mason0b86a832008-03-24 15:01:56 -0400961 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000962 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400963 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000964
965 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400966 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000967 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400968 if (ret > 0) {
969 ret = btrfs_previous_item(root, path, key.objectid, key.type);
970 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000971 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400972 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000973
Chris Mason0b86a832008-03-24 15:01:56 -0400974 while (1) {
975 l = path->nodes[0];
976 slot = path->slots[0];
977 if (slot >= btrfs_header_nritems(l)) {
978 ret = btrfs_next_leaf(root, path);
979 if (ret == 0)
980 continue;
981 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000982 goto out;
983
984 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400985 }
986 btrfs_item_key_to_cpu(l, &key, slot);
987
988 if (key.objectid < device->devid)
989 goto next;
990
991 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000992 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400993
Chris Masond3977122009-01-05 21:25:51 -0500994 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400995 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400996
Chris Mason0b86a832008-03-24 15:01:56 -0400997 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000998 extent_end = key.offset + btrfs_dev_extent_length(l,
999 dev_extent);
1000 if (key.offset <= start && extent_end > end) {
1001 *length = end - start + 1;
1002 break;
1003 } else if (key.offset <= start && extent_end > start)
1004 *length += extent_end - start;
1005 else if (key.offset > start && extent_end <= end)
1006 *length += extent_end - key.offset;
1007 else if (key.offset > start && key.offset <= end) {
1008 *length += end - key.offset + 1;
1009 break;
1010 } else if (key.offset > end)
1011 break;
1012
1013next:
1014 path->slots[0]++;
1015 }
1016 ret = 0;
1017out:
1018 btrfs_free_path(path);
1019 return ret;
1020}
1021
Josef Bacik6df9a952013-06-27 13:22:46 -04001022static int contains_pending_extent(struct btrfs_trans_handle *trans,
1023 struct btrfs_device *device,
1024 u64 *start, u64 len)
1025{
1026 struct extent_map *em;
1027 int ret = 0;
1028
1029 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1030 struct map_lookup *map;
1031 int i;
1032
1033 map = (struct map_lookup *)em->bdev;
1034 for (i = 0; i < map->num_stripes; i++) {
1035 if (map->stripes[i].dev != device)
1036 continue;
1037 if (map->stripes[i].physical >= *start + len ||
1038 map->stripes[i].physical + em->orig_block_len <=
1039 *start)
1040 continue;
1041 *start = map->stripes[i].physical +
1042 em->orig_block_len;
1043 ret = 1;
1044 }
1045 }
1046
1047 return ret;
1048}
1049
1050
Chris Mason0b86a832008-03-24 15:01:56 -04001051/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001052 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001053 * @device: the device which we search the free space in
1054 * @num_bytes: the size of the free space that we need
1055 * @start: store the start of the free space.
1056 * @len: the size of the free space. that we find, or the size of the max
1057 * free space if we don't find suitable free space
1058 *
Chris Mason0b86a832008-03-24 15:01:56 -04001059 * this uses a pretty simple search, the expectation is that it is
1060 * called very infrequently and that a given device has a small number
1061 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001062 *
1063 * @start is used to store the start of the free space if we find. But if we
1064 * don't find suitable free space, it will be used to store the start position
1065 * of the max free space.
1066 *
1067 * @len is used to store the size of the free space that we find.
1068 * But if we don't find suitable free space, it is used to store the size of
1069 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001070 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001071int find_free_dev_extent(struct btrfs_trans_handle *trans,
1072 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001073 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001074{
1075 struct btrfs_key key;
1076 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001077 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001078 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001079 u64 hole_size;
1080 u64 max_hole_start;
1081 u64 max_hole_size;
1082 u64 extent_end;
1083 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001084 u64 search_end = device->total_bytes;
1085 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001086 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001087 struct extent_buffer *l;
1088
Chris Mason0b86a832008-03-24 15:01:56 -04001089 /* FIXME use last free of some kind */
1090
1091 /* we don't want to overwrite the superblock on the drive,
1092 * so we make sure to start at an offset of at least 1MB
1093 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001094 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001095
Josef Bacik6df9a952013-06-27 13:22:46 -04001096 path = btrfs_alloc_path();
1097 if (!path)
1098 return -ENOMEM;
1099again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001100 max_hole_start = search_start;
1101 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001102 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001103
Stefan Behrens63a212a2012-11-05 18:29:28 +01001104 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001105 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001106 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001107 }
1108
Miao Xie7bfc8372011-01-05 10:07:26 +00001109 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001110 path->search_commit_root = 1;
1111 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001112
Chris Mason0b86a832008-03-24 15:01:56 -04001113 key.objectid = device->devid;
1114 key.offset = search_start;
1115 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001116
Li Zefan125ccb02011-12-08 15:07:24 +08001117 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001118 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001119 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001120 if (ret > 0) {
1121 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1122 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001123 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001124 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001125
Chris Mason0b86a832008-03-24 15:01:56 -04001126 while (1) {
1127 l = path->nodes[0];
1128 slot = path->slots[0];
1129 if (slot >= btrfs_header_nritems(l)) {
1130 ret = btrfs_next_leaf(root, path);
1131 if (ret == 0)
1132 continue;
1133 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001134 goto out;
1135
1136 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001137 }
1138 btrfs_item_key_to_cpu(l, &key, slot);
1139
1140 if (key.objectid < device->devid)
1141 goto next;
1142
1143 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001144 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001145
Chris Mason0b86a832008-03-24 15:01:56 -04001146 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1147 goto next;
1148
Miao Xie7bfc8372011-01-05 10:07:26 +00001149 if (key.offset > search_start) {
1150 hole_size = key.offset - search_start;
1151
Josef Bacik6df9a952013-06-27 13:22:46 -04001152 /*
1153 * Have to check before we set max_hole_start, otherwise
1154 * we could end up sending back this offset anyway.
1155 */
1156 if (contains_pending_extent(trans, device,
1157 &search_start,
1158 hole_size))
1159 hole_size = 0;
1160
Miao Xie7bfc8372011-01-05 10:07:26 +00001161 if (hole_size > max_hole_size) {
1162 max_hole_start = search_start;
1163 max_hole_size = hole_size;
1164 }
1165
1166 /*
1167 * If this free space is greater than which we need,
1168 * it must be the max free space that we have found
1169 * until now, so max_hole_start must point to the start
1170 * of this free space and the length of this free space
1171 * is stored in max_hole_size. Thus, we return
1172 * max_hole_start and max_hole_size and go back to the
1173 * caller.
1174 */
1175 if (hole_size >= num_bytes) {
1176 ret = 0;
1177 goto out;
1178 }
1179 }
1180
Chris Mason0b86a832008-03-24 15:01:56 -04001181 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001182 extent_end = key.offset + btrfs_dev_extent_length(l,
1183 dev_extent);
1184 if (extent_end > search_start)
1185 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001186next:
1187 path->slots[0]++;
1188 cond_resched();
1189 }
Chris Mason0b86a832008-03-24 15:01:56 -04001190
liubo38c01b92011-08-02 02:39:03 +00001191 /*
1192 * At this point, search_start should be the end of
1193 * allocated dev extents, and when shrinking the device,
1194 * search_end may be smaller than search_start.
1195 */
1196 if (search_end > search_start)
1197 hole_size = search_end - search_start;
1198
Miao Xie7bfc8372011-01-05 10:07:26 +00001199 if (hole_size > max_hole_size) {
1200 max_hole_start = search_start;
1201 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001202 }
Chris Mason0b86a832008-03-24 15:01:56 -04001203
Josef Bacik6df9a952013-06-27 13:22:46 -04001204 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1205 btrfs_release_path(path);
1206 goto again;
1207 }
1208
Miao Xie7bfc8372011-01-05 10:07:26 +00001209 /* See above. */
1210 if (hole_size < num_bytes)
1211 ret = -ENOSPC;
1212 else
1213 ret = 0;
1214
1215out:
Yan Zheng2b820322008-11-17 21:11:30 -05001216 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001217 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001218 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001219 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001220 return ret;
1221}
1222
Christoph Hellwigb2950862008-12-02 09:54:17 -05001223static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001224 struct btrfs_device *device,
1225 u64 start)
1226{
1227 int ret;
1228 struct btrfs_path *path;
1229 struct btrfs_root *root = device->dev_root;
1230 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001231 struct btrfs_key found_key;
1232 struct extent_buffer *leaf = NULL;
1233 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001234
1235 path = btrfs_alloc_path();
1236 if (!path)
1237 return -ENOMEM;
1238
1239 key.objectid = device->devid;
1240 key.offset = start;
1241 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001242again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001243 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001244 if (ret > 0) {
1245 ret = btrfs_previous_item(root, path, key.objectid,
1246 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001247 if (ret)
1248 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001249 leaf = path->nodes[0];
1250 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1251 extent = btrfs_item_ptr(leaf, path->slots[0],
1252 struct btrfs_dev_extent);
1253 BUG_ON(found_key.offset > start || found_key.offset +
1254 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001255 key = found_key;
1256 btrfs_release_path(path);
1257 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001258 } else if (ret == 0) {
1259 leaf = path->nodes[0];
1260 extent = btrfs_item_ptr(leaf, path->slots[0],
1261 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001262 } else {
1263 btrfs_error(root->fs_info, ret, "Slot search failed");
1264 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001265 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001266
Josef Bacik2bf64752011-09-26 17:12:22 -04001267 if (device->bytes_used > 0) {
1268 u64 len = btrfs_dev_extent_length(leaf, extent);
1269 device->bytes_used -= len;
1270 spin_lock(&root->fs_info->free_chunk_lock);
1271 root->fs_info->free_chunk_space += len;
1272 spin_unlock(&root->fs_info->free_chunk_lock);
1273 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001274 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001275 if (ret) {
1276 btrfs_error(root->fs_info, ret,
1277 "Failed to remove dev extent item");
1278 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001279out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001280 btrfs_free_path(path);
1281 return ret;
1282}
1283
Eric Sandeen48a3b632013-04-25 20:41:01 +00001284static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1285 struct btrfs_device *device,
1286 u64 chunk_tree, u64 chunk_objectid,
1287 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001288{
1289 int ret;
1290 struct btrfs_path *path;
1291 struct btrfs_root *root = device->dev_root;
1292 struct btrfs_dev_extent *extent;
1293 struct extent_buffer *leaf;
1294 struct btrfs_key key;
1295
Chris Masondfe25022008-05-13 13:46:40 -04001296 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001297 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001298 path = btrfs_alloc_path();
1299 if (!path)
1300 return -ENOMEM;
1301
Chris Mason0b86a832008-03-24 15:01:56 -04001302 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001303 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001304 key.type = BTRFS_DEV_EXTENT_KEY;
1305 ret = btrfs_insert_empty_item(trans, root, path, &key,
1306 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001307 if (ret)
1308 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001309
1310 leaf = path->nodes[0];
1311 extent = btrfs_item_ptr(leaf, path->slots[0],
1312 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001313 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1314 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1315 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1316
1317 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001318 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001319
Chris Mason0b86a832008-03-24 15:01:56 -04001320 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1321 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001322out:
Chris Mason0b86a832008-03-24 15:01:56 -04001323 btrfs_free_path(path);
1324 return ret;
1325}
1326
Josef Bacik6df9a952013-06-27 13:22:46 -04001327static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001328{
Josef Bacik6df9a952013-06-27 13:22:46 -04001329 struct extent_map_tree *em_tree;
1330 struct extent_map *em;
1331 struct rb_node *n;
1332 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001333
Josef Bacik6df9a952013-06-27 13:22:46 -04001334 em_tree = &fs_info->mapping_tree.map_tree;
1335 read_lock(&em_tree->lock);
1336 n = rb_last(&em_tree->map);
1337 if (n) {
1338 em = rb_entry(n, struct extent_map, rb_node);
1339 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001340 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001341 read_unlock(&em_tree->lock);
1342
Chris Mason0b86a832008-03-24 15:01:56 -04001343 return ret;
1344}
1345
Ilya Dryomov53f10652013-08-12 14:33:01 +03001346static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1347 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001348{
1349 int ret;
1350 struct btrfs_key key;
1351 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001352 struct btrfs_path *path;
1353
Yan Zheng2b820322008-11-17 21:11:30 -05001354 path = btrfs_alloc_path();
1355 if (!path)
1356 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001357
1358 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1359 key.type = BTRFS_DEV_ITEM_KEY;
1360 key.offset = (u64)-1;
1361
Ilya Dryomov53f10652013-08-12 14:33:01 +03001362 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001363 if (ret < 0)
1364 goto error;
1365
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001366 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001367
Ilya Dryomov53f10652013-08-12 14:33:01 +03001368 ret = btrfs_previous_item(fs_info->chunk_root, path,
1369 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001370 BTRFS_DEV_ITEM_KEY);
1371 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001372 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001373 } else {
1374 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1375 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001376 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001377 }
1378 ret = 0;
1379error:
Yan Zheng2b820322008-11-17 21:11:30 -05001380 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001381 return ret;
1382}
1383
1384/*
1385 * the device information is stored in the chunk root
1386 * the btrfs_device struct should be fully filled in
1387 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001388static int btrfs_add_device(struct btrfs_trans_handle *trans,
1389 struct btrfs_root *root,
1390 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001391{
1392 int ret;
1393 struct btrfs_path *path;
1394 struct btrfs_dev_item *dev_item;
1395 struct extent_buffer *leaf;
1396 struct btrfs_key key;
1397 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001398
1399 root = root->fs_info->chunk_root;
1400
1401 path = btrfs_alloc_path();
1402 if (!path)
1403 return -ENOMEM;
1404
Chris Mason0b86a832008-03-24 15:01:56 -04001405 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1406 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001407 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001408
1409 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001410 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001411 if (ret)
1412 goto out;
1413
1414 leaf = path->nodes[0];
1415 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1416
1417 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001418 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001419 btrfs_set_device_type(leaf, dev_item, device->type);
1420 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1421 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1422 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001423 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1424 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001425 btrfs_set_device_group(leaf, dev_item, 0);
1426 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1427 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001428 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001429
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001430 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001431 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001432 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001433 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001434 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001435
Yan Zheng2b820322008-11-17 21:11:30 -05001436 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001437out:
1438 btrfs_free_path(path);
1439 return ret;
1440}
Chris Mason8f18cf12008-04-25 16:53:30 -04001441
Chris Masona061fc82008-05-07 11:43:44 -04001442static int btrfs_rm_dev_item(struct btrfs_root *root,
1443 struct btrfs_device *device)
1444{
1445 int ret;
1446 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001447 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001448 struct btrfs_trans_handle *trans;
1449
1450 root = root->fs_info->chunk_root;
1451
1452 path = btrfs_alloc_path();
1453 if (!path)
1454 return -ENOMEM;
1455
Yan, Zhenga22285a2010-05-16 10:48:46 -04001456 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001457 if (IS_ERR(trans)) {
1458 btrfs_free_path(path);
1459 return PTR_ERR(trans);
1460 }
Chris Masona061fc82008-05-07 11:43:44 -04001461 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1462 key.type = BTRFS_DEV_ITEM_KEY;
1463 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001464 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001465
1466 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1467 if (ret < 0)
1468 goto out;
1469
1470 if (ret > 0) {
1471 ret = -ENOENT;
1472 goto out;
1473 }
1474
1475 ret = btrfs_del_item(trans, root, path);
1476 if (ret)
1477 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001478out:
1479 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001480 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001481 btrfs_commit_transaction(trans, root);
1482 return ret;
1483}
1484
1485int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1486{
1487 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001488 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001489 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001490 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001491 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001492 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001493 u64 all_avail;
1494 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001495 u64 num_devices;
1496 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001497 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001498 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001499 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001500
Chris Masona061fc82008-05-07 11:43:44 -04001501 mutex_lock(&uuid_mutex);
1502
Miao Xiede98ced2013-01-29 10:13:12 +00001503 do {
1504 seq = read_seqbegin(&root->fs_info->profiles_lock);
1505
1506 all_avail = root->fs_info->avail_data_alloc_bits |
1507 root->fs_info->avail_system_alloc_bits |
1508 root->fs_info->avail_metadata_alloc_bits;
1509 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001510
Stefan Behrens8dabb742012-11-06 13:15:27 +01001511 num_devices = root->fs_info->fs_devices->num_devices;
1512 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1513 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1514 WARN_ON(num_devices < 1);
1515 num_devices--;
1516 }
1517 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1518
1519 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001520 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001521 goto out;
1522 }
1523
Stefan Behrens8dabb742012-11-06 13:15:27 +01001524 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001525 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001526 goto out;
1527 }
1528
David Woodhouse53b381b2013-01-29 18:40:14 -05001529 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1530 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001531 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001532 goto out;
1533 }
1534 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1535 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001536 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001537 goto out;
1538 }
1539
Chris Masondfe25022008-05-13 13:46:40 -04001540 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001541 struct list_head *devices;
1542 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001543
Chris Masondfe25022008-05-13 13:46:40 -04001544 device = NULL;
1545 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001546 /*
1547 * It is safe to read the devices since the volume_mutex
1548 * is held.
1549 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001550 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001551 if (tmp->in_fs_metadata &&
1552 !tmp->is_tgtdev_for_dev_replace &&
1553 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001554 device = tmp;
1555 break;
1556 }
1557 }
1558 bdev = NULL;
1559 bh = NULL;
1560 disk_super = NULL;
1561 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001562 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001563 goto out;
1564 }
Chris Masondfe25022008-05-13 13:46:40 -04001565 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001566 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001567 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001568 root->fs_info->bdev_holder, 0,
1569 &bdev, &bh);
1570 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001571 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001572 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001573 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001574 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001575 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001576 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001577 if (!device) {
1578 ret = -ENOENT;
1579 goto error_brelse;
1580 }
Chris Masondfe25022008-05-13 13:46:40 -04001581 }
Yan Zheng2b820322008-11-17 21:11:30 -05001582
Stefan Behrens63a212a2012-11-05 18:29:28 +01001583 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001584 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001585 goto error_brelse;
1586 }
1587
Yan Zheng2b820322008-11-17 21:11:30 -05001588 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001589 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001590 goto error_brelse;
1591 }
1592
1593 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001594 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001595 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001596 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001597 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001598 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001599 }
Chris Masona061fc82008-05-07 11:43:44 -04001600
Carey Underwoodd7901552013-03-04 16:37:06 -06001601 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001602 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001603 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001604 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001605 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001606
Stefan Behrens63a212a2012-11-05 18:29:28 +01001607 /*
1608 * TODO: the superblock still includes this device in its num_devices
1609 * counter although write_all_supers() is not locked out. This
1610 * could give a filesystem state which requires a degraded mount.
1611 */
Chris Masona061fc82008-05-07 11:43:44 -04001612 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1613 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001614 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001615
Josef Bacik2bf64752011-09-26 17:12:22 -04001616 spin_lock(&root->fs_info->free_chunk_lock);
1617 root->fs_info->free_chunk_space = device->total_bytes -
1618 device->bytes_used;
1619 spin_unlock(&root->fs_info->free_chunk_lock);
1620
Yan Zheng2b820322008-11-17 21:11:30 -05001621 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001622 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001623
1624 /*
1625 * the device list mutex makes sure that we don't change
1626 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001627 * the device supers. Whoever is writing all supers, should
1628 * lock the device list mutex before getting the number of
1629 * devices in the super block (super_copy). Conversely,
1630 * whoever updates the number of devices in the super block
1631 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001632 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001633
1634 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001635 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001636 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001637
Yan Zhenge4404d62008-12-12 10:03:26 -05001638 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001639 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001640
Chris Masoncd02dca2010-12-13 14:56:23 -05001641 if (device->missing)
1642 root->fs_info->fs_devices->missing_devices--;
1643
Yan Zheng2b820322008-11-17 21:11:30 -05001644 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1645 struct btrfs_device, dev_list);
1646 if (device->bdev == root->fs_info->sb->s_bdev)
1647 root->fs_info->sb->s_bdev = next_device->bdev;
1648 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1649 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1650
Xiao Guangrong1f781602011-04-20 10:09:16 +00001651 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001652 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001653
1654 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001655
David Sterba6c417612011-04-13 15:41:04 +02001656 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1657 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001658 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001659
Xiao Guangrong1f781602011-04-20 10:09:16 +00001660 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001661 struct btrfs_fs_devices *fs_devices;
1662 fs_devices = root->fs_info->fs_devices;
1663 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001664 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001665 break;
1666 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001667 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001668 fs_devices->seed = cur_devices->seed;
1669 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001670 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001671 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001672 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001673 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001674 }
1675
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001676 root->fs_info->num_tolerated_disk_barrier_failures =
1677 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1678
Yan Zheng2b820322008-11-17 21:11:30 -05001679 /*
1680 * at this point, the device is zero sized. We want to
1681 * remove it from the devices list and zero out the old super
1682 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001683 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001684 /* make sure this device isn't detected as part of
1685 * the FS anymore
1686 */
1687 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1688 set_buffer_dirty(bh);
1689 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001690 }
Chris Masona061fc82008-05-07 11:43:44 -04001691
Chris Masona061fc82008-05-07 11:43:44 -04001692 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001693
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001694 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001695 if (bdev)
1696 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001697
Chris Masona061fc82008-05-07 11:43:44 -04001698error_brelse:
1699 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001700 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001701 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001702out:
1703 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001704 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001705error_undo:
1706 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001707 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001708 list_add(&device->dev_alloc_list,
1709 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001710 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001711 root->fs_info->fs_devices->rw_devices++;
1712 }
1713 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001714}
1715
Stefan Behrense93c89c2012-11-05 17:33:06 +01001716void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1717 struct btrfs_device *srcdev)
1718{
1719 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001720
Stefan Behrense93c89c2012-11-05 17:33:06 +01001721 list_del_rcu(&srcdev->dev_list);
1722 list_del_rcu(&srcdev->dev_alloc_list);
1723 fs_info->fs_devices->num_devices--;
1724 if (srcdev->missing) {
1725 fs_info->fs_devices->missing_devices--;
1726 fs_info->fs_devices->rw_devices++;
1727 }
1728 if (srcdev->can_discard)
1729 fs_info->fs_devices->num_can_discard--;
Ilya Dryomov13572722013-10-02 20:41:01 +03001730 if (srcdev->bdev) {
Stefan Behrense93c89c2012-11-05 17:33:06 +01001731 fs_info->fs_devices->open_devices--;
1732
Ilya Dryomov13572722013-10-02 20:41:01 +03001733 /* zero out the old super */
1734 btrfs_scratch_superblock(srcdev);
1735 }
1736
Stefan Behrense93c89c2012-11-05 17:33:06 +01001737 call_rcu(&srcdev->rcu, free_device);
1738}
1739
1740void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1741 struct btrfs_device *tgtdev)
1742{
1743 struct btrfs_device *next_device;
1744
1745 WARN_ON(!tgtdev);
1746 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1747 if (tgtdev->bdev) {
1748 btrfs_scratch_superblock(tgtdev);
1749 fs_info->fs_devices->open_devices--;
1750 }
1751 fs_info->fs_devices->num_devices--;
1752 if (tgtdev->can_discard)
1753 fs_info->fs_devices->num_can_discard++;
1754
1755 next_device = list_entry(fs_info->fs_devices->devices.next,
1756 struct btrfs_device, dev_list);
1757 if (tgtdev->bdev == fs_info->sb->s_bdev)
1758 fs_info->sb->s_bdev = next_device->bdev;
1759 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1760 fs_info->fs_devices->latest_bdev = next_device->bdev;
1761 list_del_rcu(&tgtdev->dev_list);
1762
1763 call_rcu(&tgtdev->rcu, free_device);
1764
1765 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1766}
1767
Eric Sandeen48a3b632013-04-25 20:41:01 +00001768static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1769 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001770{
1771 int ret = 0;
1772 struct btrfs_super_block *disk_super;
1773 u64 devid;
1774 u8 *dev_uuid;
1775 struct block_device *bdev;
1776 struct buffer_head *bh;
1777
1778 *device = NULL;
1779 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1780 root->fs_info->bdev_holder, 0, &bdev, &bh);
1781 if (ret)
1782 return ret;
1783 disk_super = (struct btrfs_super_block *)bh->b_data;
1784 devid = btrfs_stack_device_id(&disk_super->dev_item);
1785 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001786 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001787 disk_super->fsid);
1788 brelse(bh);
1789 if (!*device)
1790 ret = -ENOENT;
1791 blkdev_put(bdev, FMODE_READ);
1792 return ret;
1793}
1794
1795int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1796 char *device_path,
1797 struct btrfs_device **device)
1798{
1799 *device = NULL;
1800 if (strcmp(device_path, "missing") == 0) {
1801 struct list_head *devices;
1802 struct btrfs_device *tmp;
1803
1804 devices = &root->fs_info->fs_devices->devices;
1805 /*
1806 * It is safe to read the devices since the volume_mutex
1807 * is held by the caller.
1808 */
1809 list_for_each_entry(tmp, devices, dev_list) {
1810 if (tmp->in_fs_metadata && !tmp->bdev) {
1811 *device = tmp;
1812 break;
1813 }
1814 }
1815
1816 if (!*device) {
1817 pr_err("btrfs: no missing device found\n");
1818 return -ENOENT;
1819 }
1820
1821 return 0;
1822 } else {
1823 return btrfs_find_device_by_path(root, device_path, device);
1824 }
1825}
1826
Yan Zheng2b820322008-11-17 21:11:30 -05001827/*
1828 * does all the dirty work required for changing file system's UUID.
1829 */
Li Zefan125ccb02011-12-08 15:07:24 +08001830static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001831{
1832 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1833 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001834 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001835 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001836 struct btrfs_device *device;
1837 u64 super_flags;
1838
1839 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001840 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001841 return -EINVAL;
1842
Ilya Dryomov2208a372013-08-12 14:33:03 +03001843 seed_devices = __alloc_fs_devices();
1844 if (IS_ERR(seed_devices))
1845 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001846
Yan Zhenge4404d62008-12-12 10:03:26 -05001847 old_devices = clone_fs_devices(fs_devices);
1848 if (IS_ERR(old_devices)) {
1849 kfree(seed_devices);
1850 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001851 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001852
Yan Zheng2b820322008-11-17 21:11:30 -05001853 list_add(&old_devices->list, &fs_uuids);
1854
Yan Zhenge4404d62008-12-12 10:03:26 -05001855 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1856 seed_devices->opened = 1;
1857 INIT_LIST_HEAD(&seed_devices->devices);
1858 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001859 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001860
1861 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001862 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1863 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001864
Yan Zhenge4404d62008-12-12 10:03:26 -05001865 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1866 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1867 device->fs_devices = seed_devices;
1868 }
1869
Yan Zheng2b820322008-11-17 21:11:30 -05001870 fs_devices->seeding = 0;
1871 fs_devices->num_devices = 0;
1872 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001873 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001874 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001875
1876 generate_random_uuid(fs_devices->fsid);
1877 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1878 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001879 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1880
Yan Zheng2b820322008-11-17 21:11:30 -05001881 super_flags = btrfs_super_flags(disk_super) &
1882 ~BTRFS_SUPER_FLAG_SEEDING;
1883 btrfs_set_super_flags(disk_super, super_flags);
1884
1885 return 0;
1886}
1887
1888/*
1889 * strore the expected generation for seed devices in device items.
1890 */
1891static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1892 struct btrfs_root *root)
1893{
1894 struct btrfs_path *path;
1895 struct extent_buffer *leaf;
1896 struct btrfs_dev_item *dev_item;
1897 struct btrfs_device *device;
1898 struct btrfs_key key;
1899 u8 fs_uuid[BTRFS_UUID_SIZE];
1900 u8 dev_uuid[BTRFS_UUID_SIZE];
1901 u64 devid;
1902 int ret;
1903
1904 path = btrfs_alloc_path();
1905 if (!path)
1906 return -ENOMEM;
1907
1908 root = root->fs_info->chunk_root;
1909 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1910 key.offset = 0;
1911 key.type = BTRFS_DEV_ITEM_KEY;
1912
1913 while (1) {
1914 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1915 if (ret < 0)
1916 goto error;
1917
1918 leaf = path->nodes[0];
1919next_slot:
1920 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1921 ret = btrfs_next_leaf(root, path);
1922 if (ret > 0)
1923 break;
1924 if (ret < 0)
1925 goto error;
1926 leaf = path->nodes[0];
1927 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001928 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001929 continue;
1930 }
1931
1932 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1933 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1934 key.type != BTRFS_DEV_ITEM_KEY)
1935 break;
1936
1937 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1938 struct btrfs_dev_item);
1939 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001940 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05001941 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001942 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05001943 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001944 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1945 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001946 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001947
1948 if (device->fs_devices->seeding) {
1949 btrfs_set_device_generation(leaf, dev_item,
1950 device->generation);
1951 btrfs_mark_buffer_dirty(leaf);
1952 }
1953
1954 path->slots[0]++;
1955 goto next_slot;
1956 }
1957 ret = 0;
1958error:
1959 btrfs_free_path(path);
1960 return ret;
1961}
1962
Chris Mason788f20e2008-04-28 15:29:42 -04001963int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1964{
Josef Bacikd5e20032011-08-04 14:52:27 +00001965 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001966 struct btrfs_trans_handle *trans;
1967 struct btrfs_device *device;
1968 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001969 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001970 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001971 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001972 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001973 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001974 int ret = 0;
1975
Yan Zheng2b820322008-11-17 21:11:30 -05001976 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001977 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001978
Li Zefana5d16332011-12-07 20:08:40 -05001979 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001980 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001981 if (IS_ERR(bdev))
1982 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001983
Yan Zheng2b820322008-11-17 21:11:30 -05001984 if (root->fs_info->fs_devices->seeding) {
1985 seeding_dev = 1;
1986 down_write(&sb->s_umount);
1987 mutex_lock(&uuid_mutex);
1988 }
1989
Chris Mason8c8bee12008-09-29 11:19:10 -04001990 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001991
Chris Mason788f20e2008-04-28 15:29:42 -04001992 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001993
1994 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001995 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001996 if (device->bdev == bdev) {
1997 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001998 mutex_unlock(
1999 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002000 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002001 }
2002 }
Liu Bod25628b2012-11-14 14:35:30 +00002003 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002004
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002005 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2006 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002007 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002008 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002009 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002010 }
2011
Josef Bacik606686e2012-06-04 14:03:51 -04002012 name = rcu_string_strdup(device_path, GFP_NOFS);
2013 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002014 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002015 ret = -ENOMEM;
2016 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002017 }
Josef Bacik606686e2012-06-04 14:03:51 -04002018 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002019
Yan, Zhenga22285a2010-05-16 10:48:46 -04002020 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002021 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002022 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002023 kfree(device);
2024 ret = PTR_ERR(trans);
2025 goto error;
2026 }
2027
Yan Zheng2b820322008-11-17 21:11:30 -05002028 lock_chunks(root);
2029
Josef Bacikd5e20032011-08-04 14:52:27 +00002030 q = bdev_get_queue(bdev);
2031 if (blk_queue_discard(q))
2032 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002033 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002034 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002035 device->io_width = root->sectorsize;
2036 device->io_align = root->sectorsize;
2037 device->sector_size = root->sectorsize;
2038 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002039 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002040 device->dev_root = root->fs_info->dev_root;
2041 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002042 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002043 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002044 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002045 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002046 set_blocksize(device->bdev, 4096);
2047
Yan Zheng2b820322008-11-17 21:11:30 -05002048 if (seeding_dev) {
2049 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002050 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002051 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002052 }
2053
2054 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002055
Chris Masone5e9a522009-06-10 15:17:02 -04002056 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002057 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002058 list_add(&device->dev_alloc_list,
2059 &root->fs_info->fs_devices->alloc_list);
2060 root->fs_info->fs_devices->num_devices++;
2061 root->fs_info->fs_devices->open_devices++;
2062 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002063 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002064 if (device->can_discard)
2065 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002066 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2067
Josef Bacik2bf64752011-09-26 17:12:22 -04002068 spin_lock(&root->fs_info->free_chunk_lock);
2069 root->fs_info->free_chunk_space += device->total_bytes;
2070 spin_unlock(&root->fs_info->free_chunk_lock);
2071
Chris Masonc2898112009-06-10 09:51:32 -04002072 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2073 root->fs_info->fs_devices->rotating = 1;
2074
David Sterba6c417612011-04-13 15:41:04 +02002075 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2076 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002077 total_bytes + device->total_bytes);
2078
David Sterba6c417612011-04-13 15:41:04 +02002079 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2080 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002081 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04002082 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002083
Yan Zheng2b820322008-11-17 21:11:30 -05002084 if (seeding_dev) {
2085 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002086 if (ret) {
2087 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002088 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002089 }
Yan Zheng2b820322008-11-17 21:11:30 -05002090 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002091 if (ret) {
2092 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002093 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002094 }
Yan Zheng2b820322008-11-17 21:11:30 -05002095 } else {
2096 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002097 if (ret) {
2098 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002099 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002100 }
Yan Zheng2b820322008-11-17 21:11:30 -05002101 }
2102
Chris Mason913d9522009-03-10 13:17:18 -04002103 /*
2104 * we've got more storage, clear any full flags on the space
2105 * infos
2106 */
2107 btrfs_clear_space_info_full(root->fs_info);
2108
Chris Mason7d9eb122008-07-08 14:19:17 -04002109 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002110 root->fs_info->num_tolerated_disk_barrier_failures =
2111 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002112 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002113
2114 if (seeding_dev) {
2115 mutex_unlock(&uuid_mutex);
2116 up_write(&sb->s_umount);
2117
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002118 if (ret) /* transaction commit */
2119 return ret;
2120
Yan Zheng2b820322008-11-17 21:11:30 -05002121 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002122 if (ret < 0)
2123 btrfs_error(root->fs_info, ret,
2124 "Failed to relocate sys chunks after "
2125 "device initialization. This can be fixed "
2126 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002127 trans = btrfs_attach_transaction(root);
2128 if (IS_ERR(trans)) {
2129 if (PTR_ERR(trans) == -ENOENT)
2130 return 0;
2131 return PTR_ERR(trans);
2132 }
2133 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002134 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002135
Chris Mason788f20e2008-04-28 15:29:42 -04002136 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002137
2138error_trans:
2139 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002140 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002141 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002142 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002143error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002144 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002145 if (seeding_dev) {
2146 mutex_unlock(&uuid_mutex);
2147 up_write(&sb->s_umount);
2148 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002149 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002150}
2151
Stefan Behrense93c89c2012-11-05 17:33:06 +01002152int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2153 struct btrfs_device **device_out)
2154{
2155 struct request_queue *q;
2156 struct btrfs_device *device;
2157 struct block_device *bdev;
2158 struct btrfs_fs_info *fs_info = root->fs_info;
2159 struct list_head *devices;
2160 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002161 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002162 int ret = 0;
2163
2164 *device_out = NULL;
2165 if (fs_info->fs_devices->seeding)
2166 return -EINVAL;
2167
2168 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2169 fs_info->bdev_holder);
2170 if (IS_ERR(bdev))
2171 return PTR_ERR(bdev);
2172
2173 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2174
2175 devices = &fs_info->fs_devices->devices;
2176 list_for_each_entry(device, devices, dev_list) {
2177 if (device->bdev == bdev) {
2178 ret = -EEXIST;
2179 goto error;
2180 }
2181 }
2182
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002183 device = btrfs_alloc_device(NULL, &devid, NULL);
2184 if (IS_ERR(device)) {
2185 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002186 goto error;
2187 }
2188
2189 name = rcu_string_strdup(device_path, GFP_NOFS);
2190 if (!name) {
2191 kfree(device);
2192 ret = -ENOMEM;
2193 goto error;
2194 }
2195 rcu_assign_pointer(device->name, name);
2196
2197 q = bdev_get_queue(bdev);
2198 if (blk_queue_discard(q))
2199 device->can_discard = 1;
2200 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2201 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002202 device->generation = 0;
2203 device->io_width = root->sectorsize;
2204 device->io_align = root->sectorsize;
2205 device->sector_size = root->sectorsize;
2206 device->total_bytes = i_size_read(bdev->bd_inode);
2207 device->disk_total_bytes = device->total_bytes;
2208 device->dev_root = fs_info->dev_root;
2209 device->bdev = bdev;
2210 device->in_fs_metadata = 1;
2211 device->is_tgtdev_for_dev_replace = 1;
2212 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002213 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002214 set_blocksize(device->bdev, 4096);
2215 device->fs_devices = fs_info->fs_devices;
2216 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2217 fs_info->fs_devices->num_devices++;
2218 fs_info->fs_devices->open_devices++;
2219 if (device->can_discard)
2220 fs_info->fs_devices->num_can_discard++;
2221 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2222
2223 *device_out = device;
2224 return ret;
2225
2226error:
2227 blkdev_put(bdev, FMODE_EXCL);
2228 return ret;
2229}
2230
2231void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2232 struct btrfs_device *tgtdev)
2233{
2234 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2235 tgtdev->io_width = fs_info->dev_root->sectorsize;
2236 tgtdev->io_align = fs_info->dev_root->sectorsize;
2237 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2238 tgtdev->dev_root = fs_info->dev_root;
2239 tgtdev->in_fs_metadata = 1;
2240}
2241
Chris Masond3977122009-01-05 21:25:51 -05002242static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2243 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002244{
2245 int ret;
2246 struct btrfs_path *path;
2247 struct btrfs_root *root;
2248 struct btrfs_dev_item *dev_item;
2249 struct extent_buffer *leaf;
2250 struct btrfs_key key;
2251
2252 root = device->dev_root->fs_info->chunk_root;
2253
2254 path = btrfs_alloc_path();
2255 if (!path)
2256 return -ENOMEM;
2257
2258 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2259 key.type = BTRFS_DEV_ITEM_KEY;
2260 key.offset = device->devid;
2261
2262 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2263 if (ret < 0)
2264 goto out;
2265
2266 if (ret > 0) {
2267 ret = -ENOENT;
2268 goto out;
2269 }
2270
2271 leaf = path->nodes[0];
2272 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2273
2274 btrfs_set_device_id(leaf, dev_item, device->devid);
2275 btrfs_set_device_type(leaf, dev_item, device->type);
2276 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2277 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2278 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002279 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002280 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2281 btrfs_mark_buffer_dirty(leaf);
2282
2283out:
2284 btrfs_free_path(path);
2285 return ret;
2286}
2287
Chris Mason7d9eb122008-07-08 14:19:17 -04002288static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002289 struct btrfs_device *device, u64 new_size)
2290{
2291 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002292 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002293 u64 old_total = btrfs_super_total_bytes(super_copy);
2294 u64 diff = new_size - device->total_bytes;
2295
Yan Zheng2b820322008-11-17 21:11:30 -05002296 if (!device->writeable)
2297 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002298 if (new_size <= device->total_bytes ||
2299 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002300 return -EINVAL;
2301
Chris Mason8f18cf12008-04-25 16:53:30 -04002302 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002303 device->fs_devices->total_rw_bytes += diff;
2304
2305 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002306 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002307 btrfs_clear_space_info_full(device->dev_root->fs_info);
2308
Chris Mason8f18cf12008-04-25 16:53:30 -04002309 return btrfs_update_device(trans, device);
2310}
2311
Chris Mason7d9eb122008-07-08 14:19:17 -04002312int btrfs_grow_device(struct btrfs_trans_handle *trans,
2313 struct btrfs_device *device, u64 new_size)
2314{
2315 int ret;
2316 lock_chunks(device->dev_root);
2317 ret = __btrfs_grow_device(trans, device, new_size);
2318 unlock_chunks(device->dev_root);
2319 return ret;
2320}
2321
Chris Mason8f18cf12008-04-25 16:53:30 -04002322static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2323 struct btrfs_root *root,
2324 u64 chunk_tree, u64 chunk_objectid,
2325 u64 chunk_offset)
2326{
2327 int ret;
2328 struct btrfs_path *path;
2329 struct btrfs_key key;
2330
2331 root = root->fs_info->chunk_root;
2332 path = btrfs_alloc_path();
2333 if (!path)
2334 return -ENOMEM;
2335
2336 key.objectid = chunk_objectid;
2337 key.offset = chunk_offset;
2338 key.type = BTRFS_CHUNK_ITEM_KEY;
2339
2340 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002341 if (ret < 0)
2342 goto out;
2343 else if (ret > 0) { /* Logic error or corruption */
2344 btrfs_error(root->fs_info, -ENOENT,
2345 "Failed lookup while freeing chunk.");
2346 ret = -ENOENT;
2347 goto out;
2348 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002349
2350 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002351 if (ret < 0)
2352 btrfs_error(root->fs_info, ret,
2353 "Failed to delete chunk item.");
2354out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002355 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002356 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002357}
2358
Christoph Hellwigb2950862008-12-02 09:54:17 -05002359static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002360 chunk_offset)
2361{
David Sterba6c417612011-04-13 15:41:04 +02002362 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002363 struct btrfs_disk_key *disk_key;
2364 struct btrfs_chunk *chunk;
2365 u8 *ptr;
2366 int ret = 0;
2367 u32 num_stripes;
2368 u32 array_size;
2369 u32 len = 0;
2370 u32 cur;
2371 struct btrfs_key key;
2372
2373 array_size = btrfs_super_sys_array_size(super_copy);
2374
2375 ptr = super_copy->sys_chunk_array;
2376 cur = 0;
2377
2378 while (cur < array_size) {
2379 disk_key = (struct btrfs_disk_key *)ptr;
2380 btrfs_disk_key_to_cpu(&key, disk_key);
2381
2382 len = sizeof(*disk_key);
2383
2384 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2385 chunk = (struct btrfs_chunk *)(ptr + len);
2386 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2387 len += btrfs_chunk_item_size(num_stripes);
2388 } else {
2389 ret = -EIO;
2390 break;
2391 }
2392 if (key.objectid == chunk_objectid &&
2393 key.offset == chunk_offset) {
2394 memmove(ptr, ptr + len, array_size - (cur + len));
2395 array_size -= len;
2396 btrfs_set_super_sys_array_size(super_copy, array_size);
2397 } else {
2398 ptr += len;
2399 cur += len;
2400 }
2401 }
2402 return ret;
2403}
2404
Christoph Hellwigb2950862008-12-02 09:54:17 -05002405static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002406 u64 chunk_tree, u64 chunk_objectid,
2407 u64 chunk_offset)
2408{
2409 struct extent_map_tree *em_tree;
2410 struct btrfs_root *extent_root;
2411 struct btrfs_trans_handle *trans;
2412 struct extent_map *em;
2413 struct map_lookup *map;
2414 int ret;
2415 int i;
2416
2417 root = root->fs_info->chunk_root;
2418 extent_root = root->fs_info->extent_root;
2419 em_tree = &root->fs_info->mapping_tree.map_tree;
2420
Josef Bacikba1bf482009-09-11 16:11:19 -04002421 ret = btrfs_can_relocate(extent_root, chunk_offset);
2422 if (ret)
2423 return -ENOSPC;
2424
Chris Mason8f18cf12008-04-25 16:53:30 -04002425 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002426 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002427 if (ret)
2428 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002429
Yan, Zhenga22285a2010-05-16 10:48:46 -04002430 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002431 if (IS_ERR(trans)) {
2432 ret = PTR_ERR(trans);
2433 btrfs_std_error(root->fs_info, ret);
2434 return ret;
2435 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002436
Chris Mason7d9eb122008-07-08 14:19:17 -04002437 lock_chunks(root);
2438
Chris Mason8f18cf12008-04-25 16:53:30 -04002439 /*
2440 * step two, delete the device extents and the
2441 * chunk tree entries
2442 */
Chris Mason890871b2009-09-02 16:24:52 -04002443 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002444 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002445 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002446
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002447 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002448 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002449 map = (struct map_lookup *)em->bdev;
2450
2451 for (i = 0; i < map->num_stripes; i++) {
2452 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2453 map->stripes[i].physical);
2454 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002455
Chris Masondfe25022008-05-13 13:46:40 -04002456 if (map->stripes[i].dev) {
2457 ret = btrfs_update_device(trans, map->stripes[i].dev);
2458 BUG_ON(ret);
2459 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002460 }
2461 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2462 chunk_offset);
2463
2464 BUG_ON(ret);
2465
liubo1abe9b82011-03-24 11:18:59 +00002466 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2467
Chris Mason8f18cf12008-04-25 16:53:30 -04002468 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2469 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2470 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002471 }
2472
Zheng Yan1a40e232008-09-26 10:09:34 -04002473 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2474 BUG_ON(ret);
2475
Chris Mason890871b2009-09-02 16:24:52 -04002476 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002477 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002478 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002479
Chris Mason8f18cf12008-04-25 16:53:30 -04002480 kfree(map);
2481 em->bdev = NULL;
2482
2483 /* once for the tree */
2484 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002485 /* once for us */
2486 free_extent_map(em);
2487
Chris Mason7d9eb122008-07-08 14:19:17 -04002488 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002489 btrfs_end_transaction(trans, root);
2490 return 0;
2491}
2492
Yan Zheng2b820322008-11-17 21:11:30 -05002493static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2494{
2495 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2496 struct btrfs_path *path;
2497 struct extent_buffer *leaf;
2498 struct btrfs_chunk *chunk;
2499 struct btrfs_key key;
2500 struct btrfs_key found_key;
2501 u64 chunk_tree = chunk_root->root_key.objectid;
2502 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002503 bool retried = false;
2504 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002505 int ret;
2506
2507 path = btrfs_alloc_path();
2508 if (!path)
2509 return -ENOMEM;
2510
Josef Bacikba1bf482009-09-11 16:11:19 -04002511again:
Yan Zheng2b820322008-11-17 21:11:30 -05002512 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2513 key.offset = (u64)-1;
2514 key.type = BTRFS_CHUNK_ITEM_KEY;
2515
2516 while (1) {
2517 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2518 if (ret < 0)
2519 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002520 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002521
2522 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2523 key.type);
2524 if (ret < 0)
2525 goto error;
2526 if (ret > 0)
2527 break;
2528
2529 leaf = path->nodes[0];
2530 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2531
2532 chunk = btrfs_item_ptr(leaf, path->slots[0],
2533 struct btrfs_chunk);
2534 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002535 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002536
2537 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2538 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2539 found_key.objectid,
2540 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002541 if (ret == -ENOSPC)
2542 failed++;
2543 else if (ret)
2544 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002545 }
2546
2547 if (found_key.offset == 0)
2548 break;
2549 key.offset = found_key.offset - 1;
2550 }
2551 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002552 if (failed && !retried) {
2553 failed = 0;
2554 retried = true;
2555 goto again;
2556 } else if (failed && retried) {
2557 WARN_ON(1);
2558 ret = -ENOSPC;
2559 }
Yan Zheng2b820322008-11-17 21:11:30 -05002560error:
2561 btrfs_free_path(path);
2562 return ret;
2563}
2564
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002565static int insert_balance_item(struct btrfs_root *root,
2566 struct btrfs_balance_control *bctl)
2567{
2568 struct btrfs_trans_handle *trans;
2569 struct btrfs_balance_item *item;
2570 struct btrfs_disk_balance_args disk_bargs;
2571 struct btrfs_path *path;
2572 struct extent_buffer *leaf;
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_insert_empty_item(trans, root, path, &key,
2591 sizeof(*item));
2592 if (ret)
2593 goto out;
2594
2595 leaf = path->nodes[0];
2596 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2597
2598 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2599
2600 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2601 btrfs_set_balance_data(leaf, item, &disk_bargs);
2602 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2603 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2604 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2605 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2606
2607 btrfs_set_balance_flags(leaf, item, bctl->flags);
2608
2609 btrfs_mark_buffer_dirty(leaf);
2610out:
2611 btrfs_free_path(path);
2612 err = btrfs_commit_transaction(trans, root);
2613 if (err && !ret)
2614 ret = err;
2615 return ret;
2616}
2617
2618static int del_balance_item(struct btrfs_root *root)
2619{
2620 struct btrfs_trans_handle *trans;
2621 struct btrfs_path *path;
2622 struct btrfs_key key;
2623 int ret, err;
2624
2625 path = btrfs_alloc_path();
2626 if (!path)
2627 return -ENOMEM;
2628
2629 trans = btrfs_start_transaction(root, 0);
2630 if (IS_ERR(trans)) {
2631 btrfs_free_path(path);
2632 return PTR_ERR(trans);
2633 }
2634
2635 key.objectid = BTRFS_BALANCE_OBJECTID;
2636 key.type = BTRFS_BALANCE_ITEM_KEY;
2637 key.offset = 0;
2638
2639 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2640 if (ret < 0)
2641 goto out;
2642 if (ret > 0) {
2643 ret = -ENOENT;
2644 goto out;
2645 }
2646
2647 ret = btrfs_del_item(trans, root, path);
2648out:
2649 btrfs_free_path(path);
2650 err = btrfs_commit_transaction(trans, root);
2651 if (err && !ret)
2652 ret = err;
2653 return ret;
2654}
2655
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002656/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002657 * This is a heuristic used to reduce the number of chunks balanced on
2658 * resume after balance was interrupted.
2659 */
2660static void update_balance_args(struct btrfs_balance_control *bctl)
2661{
2662 /*
2663 * Turn on soft mode for chunk types that were being converted.
2664 */
2665 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2666 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2667 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2668 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2669 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2670 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2671
2672 /*
2673 * Turn on usage filter if is not already used. The idea is
2674 * that chunks that we have already balanced should be
2675 * reasonably full. Don't do it for chunks that are being
2676 * converted - that will keep us from relocating unconverted
2677 * (albeit full) chunks.
2678 */
2679 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2680 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2681 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2682 bctl->data.usage = 90;
2683 }
2684 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2685 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2686 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2687 bctl->sys.usage = 90;
2688 }
2689 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2690 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2691 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2692 bctl->meta.usage = 90;
2693 }
2694}
2695
2696/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002697 * Should be called with both balance and volume mutexes held to
2698 * serialize other volume operations (add_dev/rm_dev/resize) with
2699 * restriper. Same goes for unset_balance_control.
2700 */
2701static void set_balance_control(struct btrfs_balance_control *bctl)
2702{
2703 struct btrfs_fs_info *fs_info = bctl->fs_info;
2704
2705 BUG_ON(fs_info->balance_ctl);
2706
2707 spin_lock(&fs_info->balance_lock);
2708 fs_info->balance_ctl = bctl;
2709 spin_unlock(&fs_info->balance_lock);
2710}
2711
2712static void unset_balance_control(struct btrfs_fs_info *fs_info)
2713{
2714 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2715
2716 BUG_ON(!fs_info->balance_ctl);
2717
2718 spin_lock(&fs_info->balance_lock);
2719 fs_info->balance_ctl = NULL;
2720 spin_unlock(&fs_info->balance_lock);
2721
2722 kfree(bctl);
2723}
2724
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002725/*
2726 * Balance filters. Return 1 if chunk should be filtered out
2727 * (should not be balanced).
2728 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002729static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002730 struct btrfs_balance_args *bargs)
2731{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002732 chunk_type = chunk_to_extended(chunk_type) &
2733 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002734
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002735 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002736 return 0;
2737
2738 return 1;
2739}
2740
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002741static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2742 struct btrfs_balance_args *bargs)
2743{
2744 struct btrfs_block_group_cache *cache;
2745 u64 chunk_used, user_thresh;
2746 int ret = 1;
2747
2748 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2749 chunk_used = btrfs_block_group_used(&cache->item);
2750
Ilya Dryomova105bb82013-01-21 15:15:56 +02002751 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002752 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002753 else if (bargs->usage > 100)
2754 user_thresh = cache->key.offset;
2755 else
2756 user_thresh = div_factor_fine(cache->key.offset,
2757 bargs->usage);
2758
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002759 if (chunk_used < user_thresh)
2760 ret = 0;
2761
2762 btrfs_put_block_group(cache);
2763 return ret;
2764}
2765
Ilya Dryomov409d4042012-01-16 22:04:47 +02002766static int chunk_devid_filter(struct extent_buffer *leaf,
2767 struct btrfs_chunk *chunk,
2768 struct btrfs_balance_args *bargs)
2769{
2770 struct btrfs_stripe *stripe;
2771 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2772 int i;
2773
2774 for (i = 0; i < num_stripes; i++) {
2775 stripe = btrfs_stripe_nr(chunk, i);
2776 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2777 return 0;
2778 }
2779
2780 return 1;
2781}
2782
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002783/* [pstart, pend) */
2784static int chunk_drange_filter(struct extent_buffer *leaf,
2785 struct btrfs_chunk *chunk,
2786 u64 chunk_offset,
2787 struct btrfs_balance_args *bargs)
2788{
2789 struct btrfs_stripe *stripe;
2790 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2791 u64 stripe_offset;
2792 u64 stripe_length;
2793 int factor;
2794 int i;
2795
2796 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2797 return 0;
2798
2799 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002800 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2801 factor = num_stripes / 2;
2802 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2803 factor = num_stripes - 1;
2804 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2805 factor = num_stripes - 2;
2806 } else {
2807 factor = num_stripes;
2808 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002809
2810 for (i = 0; i < num_stripes; i++) {
2811 stripe = btrfs_stripe_nr(chunk, i);
2812 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2813 continue;
2814
2815 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2816 stripe_length = btrfs_chunk_length(leaf, chunk);
2817 do_div(stripe_length, factor);
2818
2819 if (stripe_offset < bargs->pend &&
2820 stripe_offset + stripe_length > bargs->pstart)
2821 return 0;
2822 }
2823
2824 return 1;
2825}
2826
Ilya Dryomovea671762012-01-16 22:04:48 +02002827/* [vstart, vend) */
2828static int chunk_vrange_filter(struct extent_buffer *leaf,
2829 struct btrfs_chunk *chunk,
2830 u64 chunk_offset,
2831 struct btrfs_balance_args *bargs)
2832{
2833 if (chunk_offset < bargs->vend &&
2834 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2835 /* at least part of the chunk is inside this vrange */
2836 return 0;
2837
2838 return 1;
2839}
2840
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002841static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002842 struct btrfs_balance_args *bargs)
2843{
2844 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2845 return 0;
2846
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002847 chunk_type = chunk_to_extended(chunk_type) &
2848 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002849
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002850 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002851 return 1;
2852
2853 return 0;
2854}
2855
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002856static int should_balance_chunk(struct btrfs_root *root,
2857 struct extent_buffer *leaf,
2858 struct btrfs_chunk *chunk, u64 chunk_offset)
2859{
2860 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2861 struct btrfs_balance_args *bargs = NULL;
2862 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2863
2864 /* type filter */
2865 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2866 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2867 return 0;
2868 }
2869
2870 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2871 bargs = &bctl->data;
2872 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2873 bargs = &bctl->sys;
2874 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2875 bargs = &bctl->meta;
2876
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002877 /* profiles filter */
2878 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2879 chunk_profiles_filter(chunk_type, bargs)) {
2880 return 0;
2881 }
2882
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002883 /* usage filter */
2884 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2885 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2886 return 0;
2887 }
2888
Ilya Dryomov409d4042012-01-16 22:04:47 +02002889 /* devid filter */
2890 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2891 chunk_devid_filter(leaf, chunk, bargs)) {
2892 return 0;
2893 }
2894
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002895 /* drange filter, makes sense only with devid filter */
2896 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2897 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2898 return 0;
2899 }
2900
Ilya Dryomovea671762012-01-16 22:04:48 +02002901 /* vrange filter */
2902 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2903 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2904 return 0;
2905 }
2906
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002907 /* soft profile changing mode */
2908 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2909 chunk_soft_convert_filter(chunk_type, bargs)) {
2910 return 0;
2911 }
2912
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002913 return 1;
2914}
2915
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002916static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002917{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002918 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002919 struct btrfs_root *chunk_root = fs_info->chunk_root;
2920 struct btrfs_root *dev_root = fs_info->dev_root;
2921 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002922 struct btrfs_device *device;
2923 u64 old_size;
2924 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002925 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002926 struct btrfs_path *path;
2927 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002928 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002929 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002930 struct extent_buffer *leaf;
2931 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002932 int ret;
2933 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002934 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002935
Chris Masonec44a352008-04-28 15:29:52 -04002936 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002937 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002938 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002939 old_size = device->total_bytes;
2940 size_to_free = div_factor(old_size, 1);
2941 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002942 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002943 device->total_bytes - device->bytes_used > size_to_free ||
2944 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002945 continue;
2946
2947 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002948 if (ret == -ENOSPC)
2949 break;
Chris Masonec44a352008-04-28 15:29:52 -04002950 BUG_ON(ret);
2951
Yan, Zhenga22285a2010-05-16 10:48:46 -04002952 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002953 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002954
2955 ret = btrfs_grow_device(trans, device, old_size);
2956 BUG_ON(ret);
2957
2958 btrfs_end_transaction(trans, dev_root);
2959 }
2960
2961 /* step two, relocate all the chunks */
2962 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002963 if (!path) {
2964 ret = -ENOMEM;
2965 goto error;
2966 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002967
2968 /* zero out stat counters */
2969 spin_lock(&fs_info->balance_lock);
2970 memset(&bctl->stat, 0, sizeof(bctl->stat));
2971 spin_unlock(&fs_info->balance_lock);
2972again:
Chris Masonec44a352008-04-28 15:29:52 -04002973 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2974 key.offset = (u64)-1;
2975 key.type = BTRFS_CHUNK_ITEM_KEY;
2976
Chris Masond3977122009-01-05 21:25:51 -05002977 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002978 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002979 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002980 ret = -ECANCELED;
2981 goto error;
2982 }
2983
Chris Masonec44a352008-04-28 15:29:52 -04002984 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2985 if (ret < 0)
2986 goto error;
2987
2988 /*
2989 * this shouldn't happen, it means the last relocate
2990 * failed
2991 */
2992 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002993 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002994
2995 ret = btrfs_previous_item(chunk_root, path, 0,
2996 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002997 if (ret) {
2998 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002999 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003000 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003001
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003002 leaf = path->nodes[0];
3003 slot = path->slots[0];
3004 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3005
Chris Masonec44a352008-04-28 15:29:52 -04003006 if (found_key.objectid != key.objectid)
3007 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003008
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003009 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3010
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003011 if (!counting) {
3012 spin_lock(&fs_info->balance_lock);
3013 bctl->stat.considered++;
3014 spin_unlock(&fs_info->balance_lock);
3015 }
3016
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003017 ret = should_balance_chunk(chunk_root, leaf, chunk,
3018 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003019 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003020 if (!ret)
3021 goto loop;
3022
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003023 if (counting) {
3024 spin_lock(&fs_info->balance_lock);
3025 bctl->stat.expected++;
3026 spin_unlock(&fs_info->balance_lock);
3027 goto loop;
3028 }
3029
Chris Masonec44a352008-04-28 15:29:52 -04003030 ret = btrfs_relocate_chunk(chunk_root,
3031 chunk_root->root_key.objectid,
3032 found_key.objectid,
3033 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003034 if (ret && ret != -ENOSPC)
3035 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003036 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003037 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003038 } else {
3039 spin_lock(&fs_info->balance_lock);
3040 bctl->stat.completed++;
3041 spin_unlock(&fs_info->balance_lock);
3042 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003043loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003044 if (found_key.offset == 0)
3045 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003046 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003047 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003048
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003049 if (counting) {
3050 btrfs_release_path(path);
3051 counting = false;
3052 goto again;
3053 }
Chris Masonec44a352008-04-28 15:29:52 -04003054error:
3055 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003056 if (enospc_errors) {
3057 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
3058 enospc_errors);
3059 if (!ret)
3060 ret = -ENOSPC;
3061 }
3062
Chris Masonec44a352008-04-28 15:29:52 -04003063 return ret;
3064}
3065
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003066/**
3067 * alloc_profile_is_valid - see if a given profile is valid and reduced
3068 * @flags: profile to validate
3069 * @extended: if true @flags is treated as an extended profile
3070 */
3071static int alloc_profile_is_valid(u64 flags, int extended)
3072{
3073 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3074 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3075
3076 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3077
3078 /* 1) check that all other bits are zeroed */
3079 if (flags & ~mask)
3080 return 0;
3081
3082 /* 2) see if profile is reduced */
3083 if (flags == 0)
3084 return !extended; /* "0" is valid for usual profiles */
3085
3086 /* true if exactly one bit set */
3087 return (flags & (flags - 1)) == 0;
3088}
3089
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003090static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3091{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003092 /* cancel requested || normal exit path */
3093 return atomic_read(&fs_info->balance_cancel_req) ||
3094 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3095 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003096}
3097
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003098static void __cancel_balance(struct btrfs_fs_info *fs_info)
3099{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003100 int ret;
3101
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003102 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003103 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003104 if (ret)
3105 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003106
3107 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003108}
3109
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003110/*
3111 * Should be called with both balance and volume mutexes held
3112 */
3113int btrfs_balance(struct btrfs_balance_control *bctl,
3114 struct btrfs_ioctl_balance_args *bargs)
3115{
3116 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003117 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003118 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003119 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003120 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003121 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003122
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003123 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003124 atomic_read(&fs_info->balance_pause_req) ||
3125 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003126 ret = -EINVAL;
3127 goto out;
3128 }
3129
Ilya Dryomove4837f82012-03-27 17:09:17 +03003130 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3131 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3132 mixed = 1;
3133
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003134 /*
3135 * In case of mixed groups both data and meta should be picked,
3136 * and identical options should be given for both of them.
3137 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003138 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3139 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003140 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3141 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3142 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3143 printk(KERN_ERR "btrfs: with mixed groups data and "
3144 "metadata balance options must be the same\n");
3145 ret = -EINVAL;
3146 goto out;
3147 }
3148 }
3149
Stefan Behrens8dabb742012-11-06 13:15:27 +01003150 num_devices = fs_info->fs_devices->num_devices;
3151 btrfs_dev_replace_lock(&fs_info->dev_replace);
3152 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3153 BUG_ON(num_devices < 1);
3154 num_devices--;
3155 }
3156 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003157 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003158 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003159 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003160 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003161 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003162 if (num_devices > 2)
3163 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3164 if (num_devices > 3)
3165 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3166 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003167 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3168 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3169 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003170 printk(KERN_ERR "btrfs: unable to start balance with target "
3171 "data profile %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003172 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003173 ret = -EINVAL;
3174 goto out;
3175 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003176 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3177 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3178 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003179 printk(KERN_ERR "btrfs: unable to start balance with target "
3180 "metadata profile %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003181 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003182 ret = -EINVAL;
3183 goto out;
3184 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003185 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3186 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3187 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003188 printk(KERN_ERR "btrfs: unable to start balance with target "
3189 "system profile %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003190 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003191 ret = -EINVAL;
3192 goto out;
3193 }
3194
Ilya Dryomove4837f82012-03-27 17:09:17 +03003195 /* allow dup'ed data chunks only in mixed mode */
3196 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003197 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003198 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3199 ret = -EINVAL;
3200 goto out;
3201 }
3202
3203 /* allow to reduce meta or sys integrity only if force set */
3204 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003205 BTRFS_BLOCK_GROUP_RAID10 |
3206 BTRFS_BLOCK_GROUP_RAID5 |
3207 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003208 do {
3209 seq = read_seqbegin(&fs_info->profiles_lock);
3210
3211 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3212 (fs_info->avail_system_alloc_bits & allowed) &&
3213 !(bctl->sys.target & allowed)) ||
3214 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3215 (fs_info->avail_metadata_alloc_bits & allowed) &&
3216 !(bctl->meta.target & allowed))) {
3217 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3218 printk(KERN_INFO "btrfs: force reducing metadata "
3219 "integrity\n");
3220 } else {
3221 printk(KERN_ERR "btrfs: balance will reduce metadata "
3222 "integrity, use force if you want this\n");
3223 ret = -EINVAL;
3224 goto out;
3225 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003226 }
Miao Xiede98ced2013-01-29 10:13:12 +00003227 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003228
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003229 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3230 int num_tolerated_disk_barrier_failures;
3231 u64 target = bctl->sys.target;
3232
3233 num_tolerated_disk_barrier_failures =
3234 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3235 if (num_tolerated_disk_barrier_failures > 0 &&
3236 (target &
3237 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3238 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3239 num_tolerated_disk_barrier_failures = 0;
3240 else if (num_tolerated_disk_barrier_failures > 1 &&
3241 (target &
3242 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3243 num_tolerated_disk_barrier_failures = 1;
3244
3245 fs_info->num_tolerated_disk_barrier_failures =
3246 num_tolerated_disk_barrier_failures;
3247 }
3248
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003249 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003250 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003251 goto out;
3252
Ilya Dryomov59641012012-01-16 22:04:48 +02003253 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3254 BUG_ON(ret == -EEXIST);
3255 set_balance_control(bctl);
3256 } else {
3257 BUG_ON(ret != -EEXIST);
3258 spin_lock(&fs_info->balance_lock);
3259 update_balance_args(bctl);
3260 spin_unlock(&fs_info->balance_lock);
3261 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003262
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003263 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003264 mutex_unlock(&fs_info->balance_mutex);
3265
3266 ret = __btrfs_balance(fs_info);
3267
3268 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003269 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003270
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003271 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3272 fs_info->num_tolerated_disk_barrier_failures =
3273 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3274 }
3275
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003276 if (bargs) {
3277 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003278 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003279 }
3280
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003281 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3282 balance_need_close(fs_info)) {
3283 __cancel_balance(fs_info);
3284 }
3285
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003286 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003287
3288 return ret;
3289out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003290 if (bctl->flags & BTRFS_BALANCE_RESUME)
3291 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003292 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003293 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003294 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3295 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003296 return ret;
3297}
3298
3299static int balance_kthread(void *data)
3300{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003301 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003302 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003303
3304 mutex_lock(&fs_info->volume_mutex);
3305 mutex_lock(&fs_info->balance_mutex);
3306
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003307 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003308 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003309 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003310 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003311
3312 mutex_unlock(&fs_info->balance_mutex);
3313 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003314
Ilya Dryomov59641012012-01-16 22:04:48 +02003315 return ret;
3316}
3317
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003318int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3319{
3320 struct task_struct *tsk;
3321
3322 spin_lock(&fs_info->balance_lock);
3323 if (!fs_info->balance_ctl) {
3324 spin_unlock(&fs_info->balance_lock);
3325 return 0;
3326 }
3327 spin_unlock(&fs_info->balance_lock);
3328
3329 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3330 printk(KERN_INFO "btrfs: force skipping balance\n");
3331 return 0;
3332 }
3333
3334 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303335 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003336}
3337
Ilya Dryomov68310a52012-06-22 12:24:12 -06003338int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003339{
Ilya Dryomov59641012012-01-16 22:04:48 +02003340 struct btrfs_balance_control *bctl;
3341 struct btrfs_balance_item *item;
3342 struct btrfs_disk_balance_args disk_bargs;
3343 struct btrfs_path *path;
3344 struct extent_buffer *leaf;
3345 struct btrfs_key key;
3346 int ret;
3347
3348 path = btrfs_alloc_path();
3349 if (!path)
3350 return -ENOMEM;
3351
Ilya Dryomov68310a52012-06-22 12:24:12 -06003352 key.objectid = BTRFS_BALANCE_OBJECTID;
3353 key.type = BTRFS_BALANCE_ITEM_KEY;
3354 key.offset = 0;
3355
3356 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3357 if (ret < 0)
3358 goto out;
3359 if (ret > 0) { /* ret = -ENOENT; */
3360 ret = 0;
3361 goto out;
3362 }
3363
Ilya Dryomov59641012012-01-16 22:04:48 +02003364 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3365 if (!bctl) {
3366 ret = -ENOMEM;
3367 goto out;
3368 }
3369
Ilya Dryomov59641012012-01-16 22:04:48 +02003370 leaf = path->nodes[0];
3371 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3372
Ilya Dryomov68310a52012-06-22 12:24:12 -06003373 bctl->fs_info = fs_info;
3374 bctl->flags = btrfs_balance_flags(leaf, item);
3375 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003376
3377 btrfs_balance_data(leaf, item, &disk_bargs);
3378 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3379 btrfs_balance_meta(leaf, item, &disk_bargs);
3380 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3381 btrfs_balance_sys(leaf, item, &disk_bargs);
3382 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3383
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003384 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3385
Ilya Dryomov68310a52012-06-22 12:24:12 -06003386 mutex_lock(&fs_info->volume_mutex);
3387 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003388
Ilya Dryomov68310a52012-06-22 12:24:12 -06003389 set_balance_control(bctl);
3390
3391 mutex_unlock(&fs_info->balance_mutex);
3392 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003393out:
3394 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003395 return ret;
3396}
3397
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003398int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3399{
3400 int ret = 0;
3401
3402 mutex_lock(&fs_info->balance_mutex);
3403 if (!fs_info->balance_ctl) {
3404 mutex_unlock(&fs_info->balance_mutex);
3405 return -ENOTCONN;
3406 }
3407
3408 if (atomic_read(&fs_info->balance_running)) {
3409 atomic_inc(&fs_info->balance_pause_req);
3410 mutex_unlock(&fs_info->balance_mutex);
3411
3412 wait_event(fs_info->balance_wait_q,
3413 atomic_read(&fs_info->balance_running) == 0);
3414
3415 mutex_lock(&fs_info->balance_mutex);
3416 /* we are good with balance_ctl ripped off from under us */
3417 BUG_ON(atomic_read(&fs_info->balance_running));
3418 atomic_dec(&fs_info->balance_pause_req);
3419 } else {
3420 ret = -ENOTCONN;
3421 }
3422
3423 mutex_unlock(&fs_info->balance_mutex);
3424 return ret;
3425}
3426
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003427int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3428{
Ilya Dryomove649e582013-10-10 20:40:21 +03003429 if (fs_info->sb->s_flags & MS_RDONLY)
3430 return -EROFS;
3431
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003432 mutex_lock(&fs_info->balance_mutex);
3433 if (!fs_info->balance_ctl) {
3434 mutex_unlock(&fs_info->balance_mutex);
3435 return -ENOTCONN;
3436 }
3437
3438 atomic_inc(&fs_info->balance_cancel_req);
3439 /*
3440 * if we are running just wait and return, balance item is
3441 * deleted in btrfs_balance in this case
3442 */
3443 if (atomic_read(&fs_info->balance_running)) {
3444 mutex_unlock(&fs_info->balance_mutex);
3445 wait_event(fs_info->balance_wait_q,
3446 atomic_read(&fs_info->balance_running) == 0);
3447 mutex_lock(&fs_info->balance_mutex);
3448 } else {
3449 /* __cancel_balance needs volume_mutex */
3450 mutex_unlock(&fs_info->balance_mutex);
3451 mutex_lock(&fs_info->volume_mutex);
3452 mutex_lock(&fs_info->balance_mutex);
3453
3454 if (fs_info->balance_ctl)
3455 __cancel_balance(fs_info);
3456
3457 mutex_unlock(&fs_info->volume_mutex);
3458 }
3459
3460 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3461 atomic_dec(&fs_info->balance_cancel_req);
3462 mutex_unlock(&fs_info->balance_mutex);
3463 return 0;
3464}
3465
Stefan Behrens803b2f52013-08-15 17:11:21 +02003466static int btrfs_uuid_scan_kthread(void *data)
3467{
3468 struct btrfs_fs_info *fs_info = data;
3469 struct btrfs_root *root = fs_info->tree_root;
3470 struct btrfs_key key;
3471 struct btrfs_key max_key;
3472 struct btrfs_path *path = NULL;
3473 int ret = 0;
3474 struct extent_buffer *eb;
3475 int slot;
3476 struct btrfs_root_item root_item;
3477 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003478 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003479
3480 path = btrfs_alloc_path();
3481 if (!path) {
3482 ret = -ENOMEM;
3483 goto out;
3484 }
3485
3486 key.objectid = 0;
3487 key.type = BTRFS_ROOT_ITEM_KEY;
3488 key.offset = 0;
3489
3490 max_key.objectid = (u64)-1;
3491 max_key.type = BTRFS_ROOT_ITEM_KEY;
3492 max_key.offset = (u64)-1;
3493
3494 path->keep_locks = 1;
3495
3496 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003497 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003498 if (ret) {
3499 if (ret > 0)
3500 ret = 0;
3501 break;
3502 }
3503
3504 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3505 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3506 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3507 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3508 goto skip;
3509
3510 eb = path->nodes[0];
3511 slot = path->slots[0];
3512 item_size = btrfs_item_size_nr(eb, slot);
3513 if (item_size < sizeof(root_item))
3514 goto skip;
3515
Stefan Behrens803b2f52013-08-15 17:11:21 +02003516 read_extent_buffer(eb, &root_item,
3517 btrfs_item_ptr_offset(eb, slot),
3518 (int)sizeof(root_item));
3519 if (btrfs_root_refs(&root_item) == 0)
3520 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003521
3522 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3523 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3524 if (trans)
3525 goto update_tree;
3526
3527 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003528 /*
3529 * 1 - subvol uuid item
3530 * 1 - received_subvol uuid item
3531 */
3532 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3533 if (IS_ERR(trans)) {
3534 ret = PTR_ERR(trans);
3535 break;
3536 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003537 continue;
3538 } else {
3539 goto skip;
3540 }
3541update_tree:
3542 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003543 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3544 root_item.uuid,
3545 BTRFS_UUID_KEY_SUBVOL,
3546 key.objectid);
3547 if (ret < 0) {
3548 pr_warn("btrfs: uuid_tree_add failed %d\n",
3549 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003550 break;
3551 }
3552 }
3553
3554 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003555 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3556 root_item.received_uuid,
3557 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3558 key.objectid);
3559 if (ret < 0) {
3560 pr_warn("btrfs: uuid_tree_add failed %d\n",
3561 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003562 break;
3563 }
3564 }
3565
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003566skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003567 if (trans) {
3568 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003569 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003570 if (ret)
3571 break;
3572 }
3573
Stefan Behrens803b2f52013-08-15 17:11:21 +02003574 btrfs_release_path(path);
3575 if (key.offset < (u64)-1) {
3576 key.offset++;
3577 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3578 key.offset = 0;
3579 key.type = BTRFS_ROOT_ITEM_KEY;
3580 } else if (key.objectid < (u64)-1) {
3581 key.offset = 0;
3582 key.type = BTRFS_ROOT_ITEM_KEY;
3583 key.objectid++;
3584 } else {
3585 break;
3586 }
3587 cond_resched();
3588 }
3589
3590out:
3591 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003592 if (trans && !IS_ERR(trans))
3593 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003594 if (ret)
3595 pr_warn("btrfs: btrfs_uuid_scan_kthread failed %d\n", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003596 else
3597 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003598 up(&fs_info->uuid_tree_rescan_sem);
3599 return 0;
3600}
3601
Stefan Behrens70f80172013-08-15 17:11:23 +02003602/*
3603 * Callback for btrfs_uuid_tree_iterate().
3604 * returns:
3605 * 0 check succeeded, the entry is not outdated.
3606 * < 0 if an error occured.
3607 * > 0 if the check failed, which means the caller shall remove the entry.
3608 */
3609static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3610 u8 *uuid, u8 type, u64 subid)
3611{
3612 struct btrfs_key key;
3613 int ret = 0;
3614 struct btrfs_root *subvol_root;
3615
3616 if (type != BTRFS_UUID_KEY_SUBVOL &&
3617 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3618 goto out;
3619
3620 key.objectid = subid;
3621 key.type = BTRFS_ROOT_ITEM_KEY;
3622 key.offset = (u64)-1;
3623 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3624 if (IS_ERR(subvol_root)) {
3625 ret = PTR_ERR(subvol_root);
3626 if (ret == -ENOENT)
3627 ret = 1;
3628 goto out;
3629 }
3630
3631 switch (type) {
3632 case BTRFS_UUID_KEY_SUBVOL:
3633 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3634 ret = 1;
3635 break;
3636 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3637 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3638 BTRFS_UUID_SIZE))
3639 ret = 1;
3640 break;
3641 }
3642
3643out:
3644 return ret;
3645}
3646
3647static int btrfs_uuid_rescan_kthread(void *data)
3648{
3649 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3650 int ret;
3651
3652 /*
3653 * 1st step is to iterate through the existing UUID tree and
3654 * to delete all entries that contain outdated data.
3655 * 2nd step is to add all missing entries to the UUID tree.
3656 */
3657 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3658 if (ret < 0) {
3659 pr_warn("btrfs: iterating uuid_tree failed %d\n", ret);
3660 up(&fs_info->uuid_tree_rescan_sem);
3661 return ret;
3662 }
3663 return btrfs_uuid_scan_kthread(data);
3664}
3665
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003666int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3667{
3668 struct btrfs_trans_handle *trans;
3669 struct btrfs_root *tree_root = fs_info->tree_root;
3670 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003671 struct task_struct *task;
3672 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003673
3674 /*
3675 * 1 - root node
3676 * 1 - root item
3677 */
3678 trans = btrfs_start_transaction(tree_root, 2);
3679 if (IS_ERR(trans))
3680 return PTR_ERR(trans);
3681
3682 uuid_root = btrfs_create_tree(trans, fs_info,
3683 BTRFS_UUID_TREE_OBJECTID);
3684 if (IS_ERR(uuid_root)) {
3685 btrfs_abort_transaction(trans, tree_root,
3686 PTR_ERR(uuid_root));
3687 return PTR_ERR(uuid_root);
3688 }
3689
3690 fs_info->uuid_root = uuid_root;
3691
Stefan Behrens803b2f52013-08-15 17:11:21 +02003692 ret = btrfs_commit_transaction(trans, tree_root);
3693 if (ret)
3694 return ret;
3695
3696 down(&fs_info->uuid_tree_rescan_sem);
3697 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3698 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003699 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Stefan Behrens803b2f52013-08-15 17:11:21 +02003700 pr_warn("btrfs: failed to start uuid_scan task\n");
3701 up(&fs_info->uuid_tree_rescan_sem);
3702 return PTR_ERR(task);
3703 }
3704
3705 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003706}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003707
Stefan Behrens70f80172013-08-15 17:11:23 +02003708int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3709{
3710 struct task_struct *task;
3711
3712 down(&fs_info->uuid_tree_rescan_sem);
3713 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3714 if (IS_ERR(task)) {
3715 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3716 pr_warn("btrfs: failed to start uuid_rescan task\n");
3717 up(&fs_info->uuid_tree_rescan_sem);
3718 return PTR_ERR(task);
3719 }
3720
3721 return 0;
3722}
3723
Chris Mason8f18cf12008-04-25 16:53:30 -04003724/*
3725 * shrinking a device means finding all of the device extents past
3726 * the new size, and then following the back refs to the chunks.
3727 * The chunk relocation code actually frees the device extent
3728 */
3729int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3730{
3731 struct btrfs_trans_handle *trans;
3732 struct btrfs_root *root = device->dev_root;
3733 struct btrfs_dev_extent *dev_extent = NULL;
3734 struct btrfs_path *path;
3735 u64 length;
3736 u64 chunk_tree;
3737 u64 chunk_objectid;
3738 u64 chunk_offset;
3739 int ret;
3740 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003741 int failed = 0;
3742 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003743 struct extent_buffer *l;
3744 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003745 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003746 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003747 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003748 u64 diff = device->total_bytes - new_size;
3749
Stefan Behrens63a212a2012-11-05 18:29:28 +01003750 if (device->is_tgtdev_for_dev_replace)
3751 return -EINVAL;
3752
Chris Mason8f18cf12008-04-25 16:53:30 -04003753 path = btrfs_alloc_path();
3754 if (!path)
3755 return -ENOMEM;
3756
Chris Mason8f18cf12008-04-25 16:53:30 -04003757 path->reada = 2;
3758
Chris Mason7d9eb122008-07-08 14:19:17 -04003759 lock_chunks(root);
3760
Chris Mason8f18cf12008-04-25 16:53:30 -04003761 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003762 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003763 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003764 spin_lock(&root->fs_info->free_chunk_lock);
3765 root->fs_info->free_chunk_space -= diff;
3766 spin_unlock(&root->fs_info->free_chunk_lock);
3767 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003768 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003769
Josef Bacikba1bf482009-09-11 16:11:19 -04003770again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003771 key.objectid = device->devid;
3772 key.offset = (u64)-1;
3773 key.type = BTRFS_DEV_EXTENT_KEY;
3774
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003775 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003776 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3777 if (ret < 0)
3778 goto done;
3779
3780 ret = btrfs_previous_item(root, path, 0, key.type);
3781 if (ret < 0)
3782 goto done;
3783 if (ret) {
3784 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003785 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003786 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003787 }
3788
3789 l = path->nodes[0];
3790 slot = path->slots[0];
3791 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3792
Josef Bacikba1bf482009-09-11 16:11:19 -04003793 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003794 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003795 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003796 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003797
3798 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3799 length = btrfs_dev_extent_length(l, dev_extent);
3800
Josef Bacikba1bf482009-09-11 16:11:19 -04003801 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003802 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003803 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003804 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003805
3806 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3807 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3808 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003809 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003810
3811 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3812 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003813 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003814 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003815 if (ret == -ENOSPC)
3816 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003817 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003818
3819 if (failed && !retried) {
3820 failed = 0;
3821 retried = true;
3822 goto again;
3823 } else if (failed && retried) {
3824 ret = -ENOSPC;
3825 lock_chunks(root);
3826
3827 device->total_bytes = old_size;
3828 if (device->writeable)
3829 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003830 spin_lock(&root->fs_info->free_chunk_lock);
3831 root->fs_info->free_chunk_space += diff;
3832 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003833 unlock_chunks(root);
3834 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003835 }
3836
Chris Balld6397ba2009-04-27 07:29:03 -04003837 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003838 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003839 if (IS_ERR(trans)) {
3840 ret = PTR_ERR(trans);
3841 goto done;
3842 }
3843
Chris Balld6397ba2009-04-27 07:29:03 -04003844 lock_chunks(root);
3845
3846 device->disk_total_bytes = new_size;
3847 /* Now btrfs_update_device() will change the on-disk size. */
3848 ret = btrfs_update_device(trans, device);
3849 if (ret) {
3850 unlock_chunks(root);
3851 btrfs_end_transaction(trans, root);
3852 goto done;
3853 }
3854 WARN_ON(diff > old_total);
3855 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3856 unlock_chunks(root);
3857 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003858done:
3859 btrfs_free_path(path);
3860 return ret;
3861}
3862
Li Zefan125ccb02011-12-08 15:07:24 +08003863static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003864 struct btrfs_key *key,
3865 struct btrfs_chunk *chunk, int item_size)
3866{
David Sterba6c417612011-04-13 15:41:04 +02003867 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003868 struct btrfs_disk_key disk_key;
3869 u32 array_size;
3870 u8 *ptr;
3871
3872 array_size = btrfs_super_sys_array_size(super_copy);
3873 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3874 return -EFBIG;
3875
3876 ptr = super_copy->sys_chunk_array + array_size;
3877 btrfs_cpu_key_to_disk(&disk_key, key);
3878 memcpy(ptr, &disk_key, sizeof(disk_key));
3879 ptr += sizeof(disk_key);
3880 memcpy(ptr, chunk, item_size);
3881 item_size += sizeof(disk_key);
3882 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3883 return 0;
3884}
3885
Miao Xieb2117a32011-01-05 10:07:28 +00003886/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003887 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003888 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003889static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003890{
Arne Jansen73c5de02011-04-12 12:07:57 +02003891 const struct btrfs_device_info *di_a = a;
3892 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003893
Arne Jansen73c5de02011-04-12 12:07:57 +02003894 if (di_a->max_avail > di_b->max_avail)
3895 return -1;
3896 if (di_a->max_avail < di_b->max_avail)
3897 return 1;
3898 if (di_a->total_avail > di_b->total_avail)
3899 return -1;
3900 if (di_a->total_avail < di_b->total_avail)
3901 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003902 return 0;
3903}
3904
Eric Sandeen48a3b632013-04-25 20:41:01 +00003905static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00003906 [BTRFS_RAID_RAID10] = {
3907 .sub_stripes = 2,
3908 .dev_stripes = 1,
3909 .devs_max = 0, /* 0 == as many as possible */
3910 .devs_min = 4,
3911 .devs_increment = 2,
3912 .ncopies = 2,
3913 },
3914 [BTRFS_RAID_RAID1] = {
3915 .sub_stripes = 1,
3916 .dev_stripes = 1,
3917 .devs_max = 2,
3918 .devs_min = 2,
3919 .devs_increment = 2,
3920 .ncopies = 2,
3921 },
3922 [BTRFS_RAID_DUP] = {
3923 .sub_stripes = 1,
3924 .dev_stripes = 2,
3925 .devs_max = 1,
3926 .devs_min = 1,
3927 .devs_increment = 1,
3928 .ncopies = 2,
3929 },
3930 [BTRFS_RAID_RAID0] = {
3931 .sub_stripes = 1,
3932 .dev_stripes = 1,
3933 .devs_max = 0,
3934 .devs_min = 2,
3935 .devs_increment = 1,
3936 .ncopies = 1,
3937 },
3938 [BTRFS_RAID_SINGLE] = {
3939 .sub_stripes = 1,
3940 .dev_stripes = 1,
3941 .devs_max = 1,
3942 .devs_min = 1,
3943 .devs_increment = 1,
3944 .ncopies = 1,
3945 },
Chris Masone942f882013-02-20 14:06:05 -05003946 [BTRFS_RAID_RAID5] = {
3947 .sub_stripes = 1,
3948 .dev_stripes = 1,
3949 .devs_max = 0,
3950 .devs_min = 2,
3951 .devs_increment = 1,
3952 .ncopies = 2,
3953 },
3954 [BTRFS_RAID_RAID6] = {
3955 .sub_stripes = 1,
3956 .dev_stripes = 1,
3957 .devs_max = 0,
3958 .devs_min = 3,
3959 .devs_increment = 1,
3960 .ncopies = 3,
3961 },
Liu Bo31e50222012-11-21 14:18:10 +00003962};
3963
David Woodhouse53b381b2013-01-29 18:40:14 -05003964static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3965{
3966 /* TODO allow them to set a preferred stripe size */
3967 return 64 * 1024;
3968}
3969
3970static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3971{
David Woodhouse53b381b2013-01-29 18:40:14 -05003972 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3973 return;
3974
Miao Xieceda0862013-04-11 10:30:16 +00003975 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05003976}
3977
Miao Xieb2117a32011-01-05 10:07:28 +00003978static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04003979 struct btrfs_root *extent_root, u64 start,
3980 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00003981{
3982 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003983 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3984 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003985 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003986 struct extent_map_tree *em_tree;
3987 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003988 struct btrfs_device_info *devices_info = NULL;
3989 u64 total_avail;
3990 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05003991 int data_stripes; /* number of stripes that count for
3992 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02003993 int sub_stripes; /* sub_stripes info for map */
3994 int dev_stripes; /* stripes per dev */
3995 int devs_max; /* max devs to use */
3996 int devs_min; /* min devs needed */
3997 int devs_increment; /* ndevs has to be a multiple of this */
3998 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003999 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004000 u64 max_stripe_size;
4001 u64 max_chunk_size;
4002 u64 stripe_size;
4003 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004004 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004005 int ndevs;
4006 int i;
4007 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004008 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004009
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004010 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004011
Miao Xieb2117a32011-01-05 10:07:28 +00004012 if (list_empty(&fs_devices->alloc_list))
4013 return -ENOSPC;
4014
Liu Bo31e50222012-11-21 14:18:10 +00004015 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004016
Liu Bo31e50222012-11-21 14:18:10 +00004017 sub_stripes = btrfs_raid_array[index].sub_stripes;
4018 dev_stripes = btrfs_raid_array[index].dev_stripes;
4019 devs_max = btrfs_raid_array[index].devs_max;
4020 devs_min = btrfs_raid_array[index].devs_min;
4021 devs_increment = btrfs_raid_array[index].devs_increment;
4022 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004023
4024 if (type & BTRFS_BLOCK_GROUP_DATA) {
4025 max_stripe_size = 1024 * 1024 * 1024;
4026 max_chunk_size = 10 * max_stripe_size;
4027 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004028 /* for larger filesystems, use larger metadata chunks */
4029 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4030 max_stripe_size = 1024 * 1024 * 1024;
4031 else
4032 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004033 max_chunk_size = max_stripe_size;
4034 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004035 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004036 max_chunk_size = 2 * max_stripe_size;
4037 } else {
4038 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
4039 type);
4040 BUG_ON(1);
4041 }
4042
4043 /* we don't want a chunk larger than 10% of writeable space */
4044 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4045 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004046
4047 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4048 GFP_NOFS);
4049 if (!devices_info)
4050 return -ENOMEM;
4051
Arne Jansen73c5de02011-04-12 12:07:57 +02004052 cur = fs_devices->alloc_list.next;
4053
4054 /*
4055 * in the first pass through the devices list, we gather information
4056 * about the available holes on each device.
4057 */
4058 ndevs = 0;
4059 while (cur != &fs_devices->alloc_list) {
4060 struct btrfs_device *device;
4061 u64 max_avail;
4062 u64 dev_offset;
4063
4064 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4065
4066 cur = cur->next;
4067
4068 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004069 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02004070 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004071 continue;
4072 }
4073
Stefan Behrens63a212a2012-11-05 18:29:28 +01004074 if (!device->in_fs_metadata ||
4075 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004076 continue;
4077
4078 if (device->total_bytes > device->bytes_used)
4079 total_avail = device->total_bytes - device->bytes_used;
4080 else
4081 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004082
4083 /* If there is no space on this device, skip it. */
4084 if (total_avail == 0)
4085 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004086
Josef Bacik6df9a952013-06-27 13:22:46 -04004087 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004088 max_stripe_size * dev_stripes,
4089 &dev_offset, &max_avail);
4090 if (ret && ret != -ENOSPC)
4091 goto error;
4092
4093 if (ret == 0)
4094 max_avail = max_stripe_size * dev_stripes;
4095
4096 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4097 continue;
4098
Eric Sandeen063d0062013-01-31 00:55:01 +00004099 if (ndevs == fs_devices->rw_devices) {
4100 WARN(1, "%s: found more than %llu devices\n",
4101 __func__, fs_devices->rw_devices);
4102 break;
4103 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004104 devices_info[ndevs].dev_offset = dev_offset;
4105 devices_info[ndevs].max_avail = max_avail;
4106 devices_info[ndevs].total_avail = total_avail;
4107 devices_info[ndevs].dev = device;
4108 ++ndevs;
4109 }
4110
4111 /*
4112 * now sort the devices by hole size / available space
4113 */
4114 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4115 btrfs_cmp_device_info, NULL);
4116
4117 /* round down to number of usable stripes */
4118 ndevs -= ndevs % devs_increment;
4119
4120 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4121 ret = -ENOSPC;
4122 goto error;
4123 }
4124
4125 if (devs_max && ndevs > devs_max)
4126 ndevs = devs_max;
4127 /*
4128 * the primary goal is to maximize the number of stripes, so use as many
4129 * devices as possible, even if the stripes are not maximum sized.
4130 */
4131 stripe_size = devices_info[ndevs-1].max_avail;
4132 num_stripes = ndevs * dev_stripes;
4133
David Woodhouse53b381b2013-01-29 18:40:14 -05004134 /*
4135 * this will have to be fixed for RAID1 and RAID10 over
4136 * more drives
4137 */
4138 data_stripes = num_stripes / ncopies;
4139
David Woodhouse53b381b2013-01-29 18:40:14 -05004140 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4141 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4142 btrfs_super_stripesize(info->super_copy));
4143 data_stripes = num_stripes - 1;
4144 }
4145 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4146 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4147 btrfs_super_stripesize(info->super_copy));
4148 data_stripes = num_stripes - 2;
4149 }
Chris Mason86db2572013-02-20 16:23:40 -05004150
4151 /*
4152 * Use the number of data stripes to figure out how big this chunk
4153 * is really going to be in terms of logical address space,
4154 * and compare that answer with the max chunk size
4155 */
4156 if (stripe_size * data_stripes > max_chunk_size) {
4157 u64 mask = (1ULL << 24) - 1;
4158 stripe_size = max_chunk_size;
4159 do_div(stripe_size, data_stripes);
4160
4161 /* bump the answer up to a 16MB boundary */
4162 stripe_size = (stripe_size + mask) & ~mask;
4163
4164 /* but don't go higher than the limits we found
4165 * while searching for free extents
4166 */
4167 if (stripe_size > devices_info[ndevs-1].max_avail)
4168 stripe_size = devices_info[ndevs-1].max_avail;
4169 }
4170
Arne Jansen73c5de02011-04-12 12:07:57 +02004171 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004172
4173 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004174 do_div(stripe_size, raid_stripe_len);
4175 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004176
Miao Xieb2117a32011-01-05 10:07:28 +00004177 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4178 if (!map) {
4179 ret = -ENOMEM;
4180 goto error;
4181 }
4182 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004183
Arne Jansen73c5de02011-04-12 12:07:57 +02004184 for (i = 0; i < ndevs; ++i) {
4185 for (j = 0; j < dev_stripes; ++j) {
4186 int s = i * dev_stripes + j;
4187 map->stripes[s].dev = devices_info[i].dev;
4188 map->stripes[s].physical = devices_info[i].dev_offset +
4189 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004190 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004191 }
Chris Mason593060d2008-03-25 16:50:33 -04004192 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004193 map->stripe_len = raid_stripe_len;
4194 map->io_align = raid_stripe_len;
4195 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004196 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004197 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004198
David Woodhouse53b381b2013-01-29 18:40:14 -05004199 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004200
Arne Jansen73c5de02011-04-12 12:07:57 +02004201 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004202
David Sterba172ddd62011-04-21 00:48:27 +02004203 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004204 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00004205 ret = -ENOMEM;
4206 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004207 }
Chris Mason0b86a832008-03-24 15:01:56 -04004208 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004209 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004210 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004211 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004212 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004213 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004214
Chris Mason0b86a832008-03-24 15:01:56 -04004215 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004216 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004217 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004218 if (!ret) {
4219 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4220 atomic_inc(&em->refs);
4221 }
Chris Mason890871b2009-09-02 16:24:52 -04004222 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004223 if (ret) {
4224 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004225 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004226 }
Yan Zheng2b820322008-11-17 21:11:30 -05004227
Josef Bacik04487482013-01-29 15:03:37 -05004228 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4229 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4230 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004231 if (ret)
4232 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004233
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004234 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004235 check_raid56_incompat_flag(extent_root->fs_info, type);
4236
Miao Xieb2117a32011-01-05 10:07:28 +00004237 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004238 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004239
Josef Bacik6df9a952013-06-27 13:22:46 -04004240error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004241 write_lock(&em_tree->lock);
4242 remove_extent_mapping(em_tree, em);
4243 write_unlock(&em_tree->lock);
4244
4245 /* One for our allocation */
4246 free_extent_map(em);
4247 /* One for the tree reference */
4248 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004249error:
4250 kfree(map);
4251 kfree(devices_info);
4252 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004253}
4254
Josef Bacik6df9a952013-06-27 13:22:46 -04004255int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004256 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004257 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004258{
Yan Zheng2b820322008-11-17 21:11:30 -05004259 struct btrfs_key key;
4260 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4261 struct btrfs_device *device;
4262 struct btrfs_chunk *chunk;
4263 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004264 struct extent_map_tree *em_tree;
4265 struct extent_map *em;
4266 struct map_lookup *map;
4267 size_t item_size;
4268 u64 dev_offset;
4269 u64 stripe_size;
4270 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004271 int ret;
4272
Josef Bacik6df9a952013-06-27 13:22:46 -04004273 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4274 read_lock(&em_tree->lock);
4275 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4276 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004277
Josef Bacik6df9a952013-06-27 13:22:46 -04004278 if (!em) {
4279 btrfs_crit(extent_root->fs_info, "unable to find logical "
4280 "%Lu len %Lu", chunk_offset, chunk_size);
4281 return -EINVAL;
4282 }
4283
4284 if (em->start != chunk_offset || em->len != chunk_size) {
4285 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
4286 " %Lu-%Lu, found %Lu-%Lu\n", chunk_offset,
4287 chunk_size, em->start, em->len);
4288 free_extent_map(em);
4289 return -EINVAL;
4290 }
4291
4292 map = (struct map_lookup *)em->bdev;
4293 item_size = btrfs_chunk_item_size(map->num_stripes);
4294 stripe_size = em->orig_block_len;
4295
4296 chunk = kzalloc(item_size, GFP_NOFS);
4297 if (!chunk) {
4298 ret = -ENOMEM;
4299 goto out;
4300 }
4301
4302 for (i = 0; i < map->num_stripes; i++) {
4303 device = map->stripes[i].dev;
4304 dev_offset = map->stripes[i].physical;
4305
Yan Zheng2b820322008-11-17 21:11:30 -05004306 device->bytes_used += stripe_size;
4307 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004308 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004309 goto out;
4310 ret = btrfs_alloc_dev_extent(trans, device,
4311 chunk_root->root_key.objectid,
4312 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4313 chunk_offset, dev_offset,
4314 stripe_size);
4315 if (ret)
4316 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004317 }
4318
Josef Bacik2bf64752011-09-26 17:12:22 -04004319 spin_lock(&extent_root->fs_info->free_chunk_lock);
4320 extent_root->fs_info->free_chunk_space -= (stripe_size *
4321 map->num_stripes);
4322 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4323
Yan Zheng2b820322008-11-17 21:11:30 -05004324 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004325 for (i = 0; i < map->num_stripes; i++) {
4326 device = map->stripes[i].dev;
4327 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004328
4329 btrfs_set_stack_stripe_devid(stripe, device->devid);
4330 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4331 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4332 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004333 }
4334
4335 btrfs_set_stack_chunk_length(chunk, chunk_size);
4336 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4337 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4338 btrfs_set_stack_chunk_type(chunk, map->type);
4339 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4340 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4341 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4342 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4343 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4344
4345 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4346 key.type = BTRFS_CHUNK_ITEM_KEY;
4347 key.offset = chunk_offset;
4348
4349 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004350 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4351 /*
4352 * TODO: Cleanup of inserted chunk root in case of
4353 * failure.
4354 */
Li Zefan125ccb02011-12-08 15:07:24 +08004355 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004356 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004357 }
liubo1abe9b82011-03-24 11:18:59 +00004358
Josef Bacik6df9a952013-06-27 13:22:46 -04004359out:
Yan Zheng2b820322008-11-17 21:11:30 -05004360 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004361 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004362 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004363}
4364
4365/*
4366 * Chunk allocation falls into two parts. The first part does works
4367 * that make the new allocated chunk useable, but not do any operation
4368 * that modifies the chunk tree. The second part does the works that
4369 * require modifying the chunk tree. This division is important for the
4370 * bootstrap process of adding storage to a seed btrfs.
4371 */
4372int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4373 struct btrfs_root *extent_root, u64 type)
4374{
4375 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004376
Josef Bacik6df9a952013-06-27 13:22:46 -04004377 chunk_offset = find_next_chunk(extent_root->fs_info);
4378 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004379}
4380
Chris Masond3977122009-01-05 21:25:51 -05004381static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004382 struct btrfs_root *root,
4383 struct btrfs_device *device)
4384{
4385 u64 chunk_offset;
4386 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004387 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004388 struct btrfs_fs_info *fs_info = root->fs_info;
4389 struct btrfs_root *extent_root = fs_info->extent_root;
4390 int ret;
4391
Josef Bacik6df9a952013-06-27 13:22:46 -04004392 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004393 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004394 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4395 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004396 if (ret)
4397 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004398
Josef Bacik6df9a952013-06-27 13:22:46 -04004399 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004400 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004401 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4402 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004403 if (ret) {
4404 btrfs_abort_transaction(trans, root, ret);
4405 goto out;
4406 }
Yan Zheng2b820322008-11-17 21:11:30 -05004407
4408 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004409 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004410 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004411out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004412 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004413}
4414
4415int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4416{
4417 struct extent_map *em;
4418 struct map_lookup *map;
4419 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4420 int readonly = 0;
4421 int i;
4422
Chris Mason890871b2009-09-02 16:24:52 -04004423 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004424 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004425 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004426 if (!em)
4427 return 1;
4428
Josef Bacikf48b9072010-01-27 02:07:59 +00004429 if (btrfs_test_opt(root, DEGRADED)) {
4430 free_extent_map(em);
4431 return 0;
4432 }
4433
Yan Zheng2b820322008-11-17 21:11:30 -05004434 map = (struct map_lookup *)em->bdev;
4435 for (i = 0; i < map->num_stripes; i++) {
4436 if (!map->stripes[i].dev->writeable) {
4437 readonly = 1;
4438 break;
4439 }
4440 }
4441 free_extent_map(em);
4442 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004443}
4444
4445void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4446{
David Sterbaa8067e02011-04-21 00:34:43 +02004447 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004448}
4449
4450void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4451{
4452 struct extent_map *em;
4453
Chris Masond3977122009-01-05 21:25:51 -05004454 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004455 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004456 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4457 if (em)
4458 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004459 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004460 if (!em)
4461 break;
4462 kfree(em->bdev);
4463 /* once for us */
4464 free_extent_map(em);
4465 /* once for the tree */
4466 free_extent_map(em);
4467 }
4468}
4469
Stefan Behrens5d964052012-11-05 14:59:07 +01004470int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004471{
Stefan Behrens5d964052012-11-05 14:59:07 +01004472 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004473 struct extent_map *em;
4474 struct map_lookup *map;
4475 struct extent_map_tree *em_tree = &map_tree->map_tree;
4476 int ret;
4477
Chris Mason890871b2009-09-02 16:24:52 -04004478 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004479 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004480 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004481
Josef Bacikfb7669b2013-04-23 10:53:18 -04004482 /*
4483 * We could return errors for these cases, but that could get ugly and
4484 * we'd probably do the same thing which is just not do anything else
4485 * and exit, so return 1 so the callers don't try to use other copies.
4486 */
4487 if (!em) {
David Sterbaccf39f92013-07-11 15:41:11 +02004488 btrfs_crit(fs_info, "No mapping for %Lu-%Lu\n", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004489 logical+len);
4490 return 1;
4491 }
4492
4493 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004494 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
Josef Bacikfb7669b2013-04-23 10:53:18 -04004495 "%Lu-%Lu\n", logical, logical+len, em->start,
4496 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004497 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004498 return 1;
4499 }
4500
Chris Masonf1885912008-04-09 16:28:12 -04004501 map = (struct map_lookup *)em->bdev;
4502 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4503 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004504 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4505 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004506 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4507 ret = 2;
4508 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4509 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004510 else
4511 ret = 1;
4512 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004513
4514 btrfs_dev_replace_lock(&fs_info->dev_replace);
4515 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4516 ret++;
4517 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4518
Chris Masonf1885912008-04-09 16:28:12 -04004519 return ret;
4520}
4521
David Woodhouse53b381b2013-01-29 18:40:14 -05004522unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4523 struct btrfs_mapping_tree *map_tree,
4524 u64 logical)
4525{
4526 struct extent_map *em;
4527 struct map_lookup *map;
4528 struct extent_map_tree *em_tree = &map_tree->map_tree;
4529 unsigned long len = root->sectorsize;
4530
4531 read_lock(&em_tree->lock);
4532 em = lookup_extent_mapping(em_tree, logical, len);
4533 read_unlock(&em_tree->lock);
4534 BUG_ON(!em);
4535
4536 BUG_ON(em->start > logical || em->start + em->len < logical);
4537 map = (struct map_lookup *)em->bdev;
4538 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4539 BTRFS_BLOCK_GROUP_RAID6)) {
4540 len = map->stripe_len * nr_data_stripes(map);
4541 }
4542 free_extent_map(em);
4543 return len;
4544}
4545
4546int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4547 u64 logical, u64 len, int mirror_num)
4548{
4549 struct extent_map *em;
4550 struct map_lookup *map;
4551 struct extent_map_tree *em_tree = &map_tree->map_tree;
4552 int ret = 0;
4553
4554 read_lock(&em_tree->lock);
4555 em = lookup_extent_mapping(em_tree, logical, len);
4556 read_unlock(&em_tree->lock);
4557 BUG_ON(!em);
4558
4559 BUG_ON(em->start > logical || em->start + em->len < logical);
4560 map = (struct map_lookup *)em->bdev;
4561 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4562 BTRFS_BLOCK_GROUP_RAID6))
4563 ret = 1;
4564 free_extent_map(em);
4565 return ret;
4566}
4567
Stefan Behrens30d98612012-11-06 14:52:18 +01004568static int find_live_mirror(struct btrfs_fs_info *fs_info,
4569 struct map_lookup *map, int first, int num,
4570 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004571{
4572 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004573 int tolerance;
4574 struct btrfs_device *srcdev;
4575
4576 if (dev_replace_is_ongoing &&
4577 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4578 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4579 srcdev = fs_info->dev_replace.srcdev;
4580 else
4581 srcdev = NULL;
4582
4583 /*
4584 * try to avoid the drive that is the source drive for a
4585 * dev-replace procedure, only choose it if no other non-missing
4586 * mirror is available
4587 */
4588 for (tolerance = 0; tolerance < 2; tolerance++) {
4589 if (map->stripes[optimal].dev->bdev &&
4590 (tolerance || map->stripes[optimal].dev != srcdev))
4591 return optimal;
4592 for (i = first; i < first + num; i++) {
4593 if (map->stripes[i].dev->bdev &&
4594 (tolerance || map->stripes[i].dev != srcdev))
4595 return i;
4596 }
Chris Masondfe25022008-05-13 13:46:40 -04004597 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004598
Chris Masondfe25022008-05-13 13:46:40 -04004599 /* we couldn't find one that doesn't fail. Just return something
4600 * and the io error handling code will clean up eventually
4601 */
4602 return optimal;
4603}
4604
David Woodhouse53b381b2013-01-29 18:40:14 -05004605static inline int parity_smaller(u64 a, u64 b)
4606{
4607 return a > b;
4608}
4609
4610/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4611static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4612{
4613 struct btrfs_bio_stripe s;
4614 int i;
4615 u64 l;
4616 int again = 1;
4617
4618 while (again) {
4619 again = 0;
4620 for (i = 0; i < bbio->num_stripes - 1; i++) {
4621 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4622 s = bbio->stripes[i];
4623 l = raid_map[i];
4624 bbio->stripes[i] = bbio->stripes[i+1];
4625 raid_map[i] = raid_map[i+1];
4626 bbio->stripes[i+1] = s;
4627 raid_map[i+1] = l;
4628 again = 1;
4629 }
4630 }
4631 }
4632}
4633
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004634static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004635 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004636 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004637 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004638{
4639 struct extent_map *em;
4640 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004641 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004642 struct extent_map_tree *em_tree = &map_tree->map_tree;
4643 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004644 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004645 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004646 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004647 u64 stripe_nr_orig;
4648 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004649 u64 stripe_len;
4650 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004651 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004652 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004653 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004654 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004655 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004656 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004657 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4658 int dev_replace_is_ongoing = 0;
4659 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004660 int patch_the_first_stripe_for_dev_replace = 0;
4661 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004662 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004663
Chris Mason890871b2009-09-02 16:24:52 -04004664 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004665 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004666 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004667
Chris Mason3b951512008-04-17 11:29:12 -04004668 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004669 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004670 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004671 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004672 }
Chris Mason0b86a832008-03-24 15:01:56 -04004673
Josef Bacik9bb91872013-04-19 23:45:33 -04004674 if (em->start > logical || em->start + em->len < logical) {
4675 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
4676 "found %Lu-%Lu\n", logical, em->start,
4677 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004678 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004679 return -EINVAL;
4680 }
4681
Chris Mason0b86a832008-03-24 15:01:56 -04004682 map = (struct map_lookup *)em->bdev;
4683 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004684
David Woodhouse53b381b2013-01-29 18:40:14 -05004685 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004686 stripe_nr = offset;
4687 /*
4688 * stripe_nr counts the total number of stripes we have to stride
4689 * to get to this block
4690 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004691 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004692
David Woodhouse53b381b2013-01-29 18:40:14 -05004693 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004694 BUG_ON(offset < stripe_offset);
4695
4696 /* stripe_offset is the offset of this block in its stripe*/
4697 stripe_offset = offset - stripe_offset;
4698
David Woodhouse53b381b2013-01-29 18:40:14 -05004699 /* if we're here for raid56, we need to know the stripe aligned start */
4700 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4701 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4702 raid56_full_stripe_start = offset;
4703
4704 /* allow a write of a full stripe, but make sure we don't
4705 * allow straddling of stripes
4706 */
4707 do_div(raid56_full_stripe_start, full_stripe_len);
4708 raid56_full_stripe_start *= full_stripe_len;
4709 }
4710
4711 if (rw & REQ_DISCARD) {
4712 /* we don't discard raid56 yet */
4713 if (map->type &
4714 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4715 ret = -EOPNOTSUPP;
4716 goto out;
4717 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004718 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004719 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4720 u64 max_len;
4721 /* For writes to RAID[56], allow a full stripeset across all disks.
4722 For other RAID types and for RAID[56] reads, just allow a single
4723 stripe (on a single disk). */
4724 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4725 (rw & REQ_WRITE)) {
4726 max_len = stripe_len * nr_data_stripes(map) -
4727 (offset - raid56_full_stripe_start);
4728 } else {
4729 /* we limit the length of each bio to what fits in a stripe */
4730 max_len = stripe_len - stripe_offset;
4731 }
4732 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004733 } else {
4734 *length = em->len - offset;
4735 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004736
David Woodhouse53b381b2013-01-29 18:40:14 -05004737 /* This is for when we're called from btrfs_merge_bio_hook() and all
4738 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004739 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004740 goto out;
4741
Stefan Behrens472262f2012-11-06 14:43:46 +01004742 btrfs_dev_replace_lock(dev_replace);
4743 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4744 if (!dev_replace_is_ongoing)
4745 btrfs_dev_replace_unlock(dev_replace);
4746
Stefan Behrensad6d6202012-11-06 15:06:47 +01004747 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4748 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4749 dev_replace->tgtdev != NULL) {
4750 /*
4751 * in dev-replace case, for repair case (that's the only
4752 * case where the mirror is selected explicitly when
4753 * calling btrfs_map_block), blocks left of the left cursor
4754 * can also be read from the target drive.
4755 * For REQ_GET_READ_MIRRORS, the target drive is added as
4756 * the last one to the array of stripes. For READ, it also
4757 * needs to be supported using the same mirror number.
4758 * If the requested block is not left of the left cursor,
4759 * EIO is returned. This can happen because btrfs_num_copies()
4760 * returns one more in the dev-replace case.
4761 */
4762 u64 tmp_length = *length;
4763 struct btrfs_bio *tmp_bbio = NULL;
4764 int tmp_num_stripes;
4765 u64 srcdev_devid = dev_replace->srcdev->devid;
4766 int index_srcdev = 0;
4767 int found = 0;
4768 u64 physical_of_found = 0;
4769
4770 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004771 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004772 if (ret) {
4773 WARN_ON(tmp_bbio != NULL);
4774 goto out;
4775 }
4776
4777 tmp_num_stripes = tmp_bbio->num_stripes;
4778 if (mirror_num > tmp_num_stripes) {
4779 /*
4780 * REQ_GET_READ_MIRRORS does not contain this
4781 * mirror, that means that the requested area
4782 * is not left of the left cursor
4783 */
4784 ret = -EIO;
4785 kfree(tmp_bbio);
4786 goto out;
4787 }
4788
4789 /*
4790 * process the rest of the function using the mirror_num
4791 * of the source drive. Therefore look it up first.
4792 * At the end, patch the device pointer to the one of the
4793 * target drive.
4794 */
4795 for (i = 0; i < tmp_num_stripes; i++) {
4796 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4797 /*
4798 * In case of DUP, in order to keep it
4799 * simple, only add the mirror with the
4800 * lowest physical address
4801 */
4802 if (found &&
4803 physical_of_found <=
4804 tmp_bbio->stripes[i].physical)
4805 continue;
4806 index_srcdev = i;
4807 found = 1;
4808 physical_of_found =
4809 tmp_bbio->stripes[i].physical;
4810 }
4811 }
4812
4813 if (found) {
4814 mirror_num = index_srcdev + 1;
4815 patch_the_first_stripe_for_dev_replace = 1;
4816 physical_to_patch_in_first_stripe = physical_of_found;
4817 } else {
4818 WARN_ON(1);
4819 ret = -EIO;
4820 kfree(tmp_bbio);
4821 goto out;
4822 }
4823
4824 kfree(tmp_bbio);
4825 } else if (mirror_num > map->num_stripes) {
4826 mirror_num = 0;
4827 }
4828
Chris Masonf2d8d742008-04-21 10:03:05 -04004829 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004830 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004831 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004832 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004833 do_div(stripe_nr_end, map->stripe_len);
4834 stripe_end_offset = stripe_nr_end * map->stripe_len -
4835 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004836
Li Dongyangfce3bb92011-03-24 10:24:26 +00004837 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4838 if (rw & REQ_DISCARD)
4839 num_stripes = min_t(u64, map->num_stripes,
4840 stripe_nr_end - stripe_nr_orig);
4841 stripe_index = do_div(stripe_nr, map->num_stripes);
4842 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004843 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004844 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004845 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004846 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004847 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004848 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004849 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004850 current->pid % map->num_stripes,
4851 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004852 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004853 }
Chris Mason2fff7342008-04-29 14:12:09 -04004854
Chris Mason611f0e02008-04-03 16:29:03 -04004855 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004856 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004857 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004858 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004859 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004860 } else {
4861 mirror_num = 1;
4862 }
Chris Mason2fff7342008-04-29 14:12:09 -04004863
Chris Mason321aecc2008-04-16 10:49:51 -04004864 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4865 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004866
4867 stripe_index = do_div(stripe_nr, factor);
4868 stripe_index *= map->sub_stripes;
4869
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004870 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004871 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004872 else if (rw & REQ_DISCARD)
4873 num_stripes = min_t(u64, map->sub_stripes *
4874 (stripe_nr_end - stripe_nr_orig),
4875 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004876 else if (mirror_num)
4877 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004878 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004879 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004880 stripe_index = find_live_mirror(fs_info, map,
4881 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004882 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004883 current->pid % map->sub_stripes,
4884 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004885 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004886 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004887
4888 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4889 BTRFS_BLOCK_GROUP_RAID6)) {
4890 u64 tmp;
4891
4892 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4893 && raid_map_ret) {
4894 int i, rot;
4895
4896 /* push stripe_nr back to the start of the full stripe */
4897 stripe_nr = raid56_full_stripe_start;
4898 do_div(stripe_nr, stripe_len);
4899
4900 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4901
4902 /* RAID[56] write or recovery. Return all stripes */
4903 num_stripes = map->num_stripes;
4904 max_errors = nr_parity_stripes(map);
4905
4906 raid_map = kmalloc(sizeof(u64) * num_stripes,
4907 GFP_NOFS);
4908 if (!raid_map) {
4909 ret = -ENOMEM;
4910 goto out;
4911 }
4912
4913 /* Work out the disk rotation on this stripe-set */
4914 tmp = stripe_nr;
4915 rot = do_div(tmp, num_stripes);
4916
4917 /* Fill in the logical address of each stripe */
4918 tmp = stripe_nr * nr_data_stripes(map);
4919 for (i = 0; i < nr_data_stripes(map); i++)
4920 raid_map[(i+rot) % num_stripes] =
4921 em->start + (tmp + i) * map->stripe_len;
4922
4923 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4924 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4925 raid_map[(i+rot+1) % num_stripes] =
4926 RAID6_Q_STRIPE;
4927
4928 *length = map->stripe_len;
4929 stripe_index = 0;
4930 stripe_offset = 0;
4931 } else {
4932 /*
4933 * Mirror #0 or #1 means the original data block.
4934 * Mirror #2 is RAID5 parity block.
4935 * Mirror #3 is RAID6 Q block.
4936 */
4937 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4938 if (mirror_num > 1)
4939 stripe_index = nr_data_stripes(map) +
4940 mirror_num - 2;
4941
4942 /* We distribute the parity blocks across stripes */
4943 tmp = stripe_nr + stripe_index;
4944 stripe_index = do_div(tmp, map->num_stripes);
4945 }
Chris Mason8790d502008-04-03 16:29:03 -04004946 } else {
4947 /*
4948 * after this do_div call, stripe_nr is the number of stripes
4949 * on this device we have to walk to find the data, and
4950 * stripe_index is the number of our device in the stripe array
4951 */
4952 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004953 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004954 }
Chris Mason593060d2008-03-25 16:50:33 -04004955 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004956
Stefan Behrens472262f2012-11-06 14:43:46 +01004957 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004958 if (dev_replace_is_ongoing) {
4959 if (rw & (REQ_WRITE | REQ_DISCARD))
4960 num_alloc_stripes <<= 1;
4961 if (rw & REQ_GET_READ_MIRRORS)
4962 num_alloc_stripes++;
4963 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004964 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08004965 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04004966 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08004967 ret = -ENOMEM;
4968 goto out;
4969 }
4970 atomic_set(&bbio->error, 0);
4971
Li Dongyangfce3bb92011-03-24 10:24:26 +00004972 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004973 int factor = 0;
4974 int sub_stripes = 0;
4975 u64 stripes_per_dev = 0;
4976 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004977 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004978
4979 if (map->type &
4980 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4981 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4982 sub_stripes = 1;
4983 else
4984 sub_stripes = map->sub_stripes;
4985
4986 factor = map->num_stripes / sub_stripes;
4987 stripes_per_dev = div_u64_rem(stripe_nr_end -
4988 stripe_nr_orig,
4989 factor,
4990 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004991 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4992 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004993 }
4994
Li Dongyangfce3bb92011-03-24 10:24:26 +00004995 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004996 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004997 map->stripes[stripe_index].physical +
4998 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004999 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005000
Li Zefanec9ef7a2011-12-01 14:06:42 +08005001 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5002 BTRFS_BLOCK_GROUP_RAID10)) {
5003 bbio->stripes[i].length = stripes_per_dev *
5004 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005005
Li Zefanec9ef7a2011-12-01 14:06:42 +08005006 if (i / sub_stripes < remaining_stripes)
5007 bbio->stripes[i].length +=
5008 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005009
5010 /*
5011 * Special for the first stripe and
5012 * the last stripe:
5013 *
5014 * |-------|...|-------|
5015 * |----------|
5016 * off end_off
5017 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005018 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005019 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005020 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005021
5022 if (stripe_index >= last_stripe &&
5023 stripe_index <= (last_stripe +
5024 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005025 bbio->stripes[i].length -=
5026 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005027
Li Zefanec9ef7a2011-12-01 14:06:42 +08005028 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005029 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005030 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005031 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005032
5033 stripe_index++;
5034 if (stripe_index == map->num_stripes) {
5035 /* This could only happen for RAID0/10 */
5036 stripe_index = 0;
5037 stripe_nr++;
5038 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005039 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005040 } else {
5041 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005042 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005043 map->stripes[stripe_index].physical +
5044 stripe_offset +
5045 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005046 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005047 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005048 stripe_index++;
5049 }
Chris Mason593060d2008-03-25 16:50:33 -04005050 }
Li Zefande11cc12011-12-01 12:55:47 +08005051
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005052 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08005053 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5054 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05005055 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08005056 BTRFS_BLOCK_GROUP_DUP)) {
5057 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005058 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5059 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08005060 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005061 }
Li Zefande11cc12011-12-01 12:55:47 +08005062
Stefan Behrens472262f2012-11-06 14:43:46 +01005063 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5064 dev_replace->tgtdev != NULL) {
5065 int index_where_to_add;
5066 u64 srcdev_devid = dev_replace->srcdev->devid;
5067
5068 /*
5069 * duplicate the write operations while the dev replace
5070 * procedure is running. Since the copying of the old disk
5071 * to the new disk takes place at run time while the
5072 * filesystem is mounted writable, the regular write
5073 * operations to the old disk have to be duplicated to go
5074 * to the new disk as well.
5075 * Note that device->missing is handled by the caller, and
5076 * that the write to the old disk is already set up in the
5077 * stripes array.
5078 */
5079 index_where_to_add = num_stripes;
5080 for (i = 0; i < num_stripes; i++) {
5081 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5082 /* write to new disk, too */
5083 struct btrfs_bio_stripe *new =
5084 bbio->stripes + index_where_to_add;
5085 struct btrfs_bio_stripe *old =
5086 bbio->stripes + i;
5087
5088 new->physical = old->physical;
5089 new->length = old->length;
5090 new->dev = dev_replace->tgtdev;
5091 index_where_to_add++;
5092 max_errors++;
5093 }
5094 }
5095 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005096 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5097 dev_replace->tgtdev != NULL) {
5098 u64 srcdev_devid = dev_replace->srcdev->devid;
5099 int index_srcdev = 0;
5100 int found = 0;
5101 u64 physical_of_found = 0;
5102
5103 /*
5104 * During the dev-replace procedure, the target drive can
5105 * also be used to read data in case it is needed to repair
5106 * a corrupt block elsewhere. This is possible if the
5107 * requested area is left of the left cursor. In this area,
5108 * the target drive is a full copy of the source drive.
5109 */
5110 for (i = 0; i < num_stripes; i++) {
5111 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5112 /*
5113 * In case of DUP, in order to keep it
5114 * simple, only add the mirror with the
5115 * lowest physical address
5116 */
5117 if (found &&
5118 physical_of_found <=
5119 bbio->stripes[i].physical)
5120 continue;
5121 index_srcdev = i;
5122 found = 1;
5123 physical_of_found = bbio->stripes[i].physical;
5124 }
5125 }
5126 if (found) {
5127 u64 length = map->stripe_len;
5128
5129 if (physical_of_found + length <=
5130 dev_replace->cursor_left) {
5131 struct btrfs_bio_stripe *tgtdev_stripe =
5132 bbio->stripes + num_stripes;
5133
5134 tgtdev_stripe->physical = physical_of_found;
5135 tgtdev_stripe->length =
5136 bbio->stripes[index_srcdev].length;
5137 tgtdev_stripe->dev = dev_replace->tgtdev;
5138
5139 num_stripes++;
5140 }
5141 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005142 }
5143
Li Zefande11cc12011-12-01 12:55:47 +08005144 *bbio_ret = bbio;
5145 bbio->num_stripes = num_stripes;
5146 bbio->max_errors = max_errors;
5147 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005148
5149 /*
5150 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5151 * mirror_num == num_stripes + 1 && dev_replace target drive is
5152 * available as a mirror
5153 */
5154 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5155 WARN_ON(num_stripes > 1);
5156 bbio->stripes[0].dev = dev_replace->tgtdev;
5157 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5158 bbio->mirror_num = map->num_stripes + 1;
5159 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005160 if (raid_map) {
5161 sort_parity_stripes(bbio, raid_map);
5162 *raid_map_ret = raid_map;
5163 }
Chris Masoncea9e442008-04-09 16:28:12 -04005164out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005165 if (dev_replace_is_ongoing)
5166 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005167 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005168 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005169}
5170
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005171int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005172 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005173 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005174{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005175 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005176 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005177}
5178
Yan Zhenga512bbf2008-12-08 16:46:26 -05005179int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5180 u64 chunk_start, u64 physical, u64 devid,
5181 u64 **logical, int *naddrs, int *stripe_len)
5182{
5183 struct extent_map_tree *em_tree = &map_tree->map_tree;
5184 struct extent_map *em;
5185 struct map_lookup *map;
5186 u64 *buf;
5187 u64 bytenr;
5188 u64 length;
5189 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005190 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005191 int i, j, nr = 0;
5192
Chris Mason890871b2009-09-02 16:24:52 -04005193 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005194 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005195 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005196
Josef Bacik835d9742013-03-19 12:13:25 -04005197 if (!em) {
5198 printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
5199 chunk_start);
5200 return -EIO;
5201 }
5202
5203 if (em->start != chunk_start) {
5204 printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
5205 em->start, chunk_start);
5206 free_extent_map(em);
5207 return -EIO;
5208 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005209 map = (struct map_lookup *)em->bdev;
5210
5211 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005212 rmap_len = map->stripe_len;
5213
Yan Zhenga512bbf2008-12-08 16:46:26 -05005214 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5215 do_div(length, map->num_stripes / map->sub_stripes);
5216 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5217 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005218 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5219 BTRFS_BLOCK_GROUP_RAID6)) {
5220 do_div(length, nr_data_stripes(map));
5221 rmap_len = map->stripe_len * nr_data_stripes(map);
5222 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005223
5224 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005225 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005226
5227 for (i = 0; i < map->num_stripes; i++) {
5228 if (devid && map->stripes[i].dev->devid != devid)
5229 continue;
5230 if (map->stripes[i].physical > physical ||
5231 map->stripes[i].physical + length <= physical)
5232 continue;
5233
5234 stripe_nr = physical - map->stripes[i].physical;
5235 do_div(stripe_nr, map->stripe_len);
5236
5237 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5238 stripe_nr = stripe_nr * map->num_stripes + i;
5239 do_div(stripe_nr, map->sub_stripes);
5240 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5241 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005242 } /* else if RAID[56], multiply by nr_data_stripes().
5243 * Alternatively, just use rmap_len below instead of
5244 * map->stripe_len */
5245
5246 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005247 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005248 for (j = 0; j < nr; j++) {
5249 if (buf[j] == bytenr)
5250 break;
5251 }
Chris Mason934d3752008-12-08 16:43:10 -05005252 if (j == nr) {
5253 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005254 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005255 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005256 }
5257
Yan Zhenga512bbf2008-12-08 16:46:26 -05005258 *logical = buf;
5259 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005260 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005261
5262 free_extent_map(em);
5263 return 0;
5264}
5265
Jan Schmidta1d3c472011-08-04 17:15:33 +02005266static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005267{
Chris Mason9be33952013-05-17 18:30:14 -04005268 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005269 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005270
Stefan Behrens442a4f62012-05-25 16:06:08 +02005271 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005272 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005273 if (err == -EIO || err == -EREMOTEIO) {
5274 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005275 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005276 struct btrfs_device *dev;
5277
5278 BUG_ON(stripe_index >= bbio->num_stripes);
5279 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005280 if (dev->bdev) {
5281 if (bio->bi_rw & WRITE)
5282 btrfs_dev_stat_inc(dev,
5283 BTRFS_DEV_STAT_WRITE_ERRS);
5284 else
5285 btrfs_dev_stat_inc(dev,
5286 BTRFS_DEV_STAT_READ_ERRS);
5287 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5288 btrfs_dev_stat_inc(dev,
5289 BTRFS_DEV_STAT_FLUSH_ERRS);
5290 btrfs_dev_stat_print_on_error(dev);
5291 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005292 }
5293 }
Chris Mason8790d502008-04-03 16:29:03 -04005294
Jan Schmidta1d3c472011-08-04 17:15:33 +02005295 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005296 is_orig_bio = 1;
5297
Jan Schmidta1d3c472011-08-04 17:15:33 +02005298 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005299 if (!is_orig_bio) {
5300 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005301 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005302 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005303 bio->bi_private = bbio->private;
5304 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005305 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005306 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005307 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005308 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005309 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005310 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005311 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005312 /*
5313 * this bio is actually up to date, we didn't
5314 * go over the max number of errors
5315 */
5316 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005317 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005318 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005319 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04005320
5321 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005322 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005323 bio_put(bio);
5324 }
Chris Mason8790d502008-04-03 16:29:03 -04005325}
5326
Chris Mason8b712842008-06-11 16:50:36 -04005327struct async_sched {
5328 struct bio *bio;
5329 int rw;
5330 struct btrfs_fs_info *info;
5331 struct btrfs_work work;
5332};
5333
5334/*
5335 * see run_scheduled_bios for a description of why bios are collected for
5336 * async submit.
5337 *
5338 * This will add one bio to the pending list for a device and make sure
5339 * the work struct is scheduled.
5340 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005341static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5342 struct btrfs_device *device,
5343 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005344{
5345 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005346 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005347
David Woodhouse53b381b2013-01-29 18:40:14 -05005348 if (device->missing || !device->bdev) {
5349 bio_endio(bio, -EIO);
5350 return;
5351 }
5352
Chris Mason8b712842008-06-11 16:50:36 -04005353 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005354 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005355 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005356 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005357 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005358 return;
Chris Mason8b712842008-06-11 16:50:36 -04005359 }
5360
5361 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005362 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005363 * higher layers. Otherwise, the async bio makes it appear we have
5364 * made progress against dirty pages when we've really just put it
5365 * on a queue for later
5366 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005367 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005368 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005369 bio->bi_next = NULL;
5370 bio->bi_rw |= rw;
5371
5372 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005373 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005374 pending_bios = &device->pending_sync_bios;
5375 else
5376 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005377
Chris Masonffbd5172009-04-20 15:50:09 -04005378 if (pending_bios->tail)
5379 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005380
Chris Masonffbd5172009-04-20 15:50:09 -04005381 pending_bios->tail = bio;
5382 if (!pending_bios->head)
5383 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005384 if (device->running_pending)
5385 should_queue = 0;
5386
5387 spin_unlock(&device->io_lock);
5388
5389 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04005390 btrfs_queue_worker(&root->fs_info->submit_workers,
5391 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005392}
5393
Josef Bacikde1ee922012-10-19 16:50:56 -04005394static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5395 sector_t sector)
5396{
5397 struct bio_vec *prev;
5398 struct request_queue *q = bdev_get_queue(bdev);
5399 unsigned short max_sectors = queue_max_sectors(q);
5400 struct bvec_merge_data bvm = {
5401 .bi_bdev = bdev,
5402 .bi_sector = sector,
5403 .bi_rw = bio->bi_rw,
5404 };
5405
5406 if (bio->bi_vcnt == 0) {
5407 WARN_ON(1);
5408 return 1;
5409 }
5410
5411 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005412 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005413 return 0;
5414
5415 if (!q->merge_bvec_fn)
5416 return 1;
5417
5418 bvm.bi_size = bio->bi_size - prev->bv_len;
5419 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5420 return 0;
5421 return 1;
5422}
5423
5424static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5425 struct bio *bio, u64 physical, int dev_nr,
5426 int rw, int async)
5427{
5428 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5429
5430 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005431 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005432 bio->bi_end_io = btrfs_end_bio;
5433 bio->bi_sector = physical >> 9;
5434#ifdef DEBUG
5435 {
5436 struct rcu_string *name;
5437
5438 rcu_read_lock();
5439 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005440 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005441 "(%s id %llu), size=%u\n", rw,
5442 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5443 name->str, dev->devid, bio->bi_size);
5444 rcu_read_unlock();
5445 }
5446#endif
5447 bio->bi_bdev = dev->bdev;
5448 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005449 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005450 else
5451 btrfsic_submit_bio(rw, bio);
5452}
5453
5454static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5455 struct bio *first_bio, struct btrfs_device *dev,
5456 int dev_nr, int rw, int async)
5457{
5458 struct bio_vec *bvec = first_bio->bi_io_vec;
5459 struct bio *bio;
5460 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5461 u64 physical = bbio->stripes[dev_nr].physical;
5462
5463again:
5464 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5465 if (!bio)
5466 return -ENOMEM;
5467
5468 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5469 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5470 bvec->bv_offset) < bvec->bv_len) {
5471 u64 len = bio->bi_size;
5472
5473 atomic_inc(&bbio->stripes_pending);
5474 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5475 rw, async);
5476 physical += len;
5477 goto again;
5478 }
5479 bvec++;
5480 }
5481
5482 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5483 return 0;
5484}
5485
5486static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5487{
5488 atomic_inc(&bbio->error);
5489 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5490 bio->bi_private = bbio->private;
5491 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005492 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Josef Bacikde1ee922012-10-19 16:50:56 -04005493 bio->bi_sector = logical >> 9;
5494 kfree(bbio);
5495 bio_endio(bio, -EIO);
5496 }
5497}
5498
Chris Masonf1885912008-04-09 16:28:12 -04005499int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005500 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005501{
Chris Mason0b86a832008-03-24 15:01:56 -04005502 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005503 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04005504 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005505 u64 length = 0;
5506 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005507 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005508 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005509 int dev_nr = 0;
5510 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005511 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005512
Chris Masonf2d8d742008-04-21 10:03:05 -04005513 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005514 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005515
David Woodhouse53b381b2013-01-29 18:40:14 -05005516 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5517 mirror_num, &raid_map);
5518 if (ret) /* -ENOMEM */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005519 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04005520
Jan Schmidta1d3c472011-08-04 17:15:33 +02005521 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005522 bbio->orig_bio = first_bio;
5523 bbio->private = first_bio->bi_private;
5524 bbio->end_io = first_bio->bi_end_io;
5525 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5526
5527 if (raid_map) {
5528 /* In this case, map_length has been set to the length of
5529 a single stripe; not the whole write */
5530 if (rw & WRITE) {
5531 return raid56_parity_write(root, bio, bbio,
5532 raid_map, map_length);
5533 } else {
5534 return raid56_parity_recover(root, bio, bbio,
5535 raid_map, map_length,
5536 mirror_num);
5537 }
5538 }
5539
Chris Masoncea9e442008-04-09 16:28:12 -04005540 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005541 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005542 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005543 BUG();
5544 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005545
Chris Masond3977122009-01-05 21:25:51 -05005546 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005547 dev = bbio->stripes[dev_nr].dev;
5548 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5549 bbio_error(bbio, first_bio, logical);
5550 dev_nr++;
5551 continue;
5552 }
5553
5554 /*
5555 * Check and see if we're ok with this bio based on it's size
5556 * and offset with the given device.
5557 */
5558 if (!bio_size_ok(dev->bdev, first_bio,
5559 bbio->stripes[dev_nr].physical >> 9)) {
5560 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5561 dev_nr, rw, async_submit);
5562 BUG_ON(ret);
5563 dev_nr++;
5564 continue;
5565 }
5566
Jan Schmidta1d3c472011-08-04 17:15:33 +02005567 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005568 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005569 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005570 } else {
5571 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04005572 }
Josef Bacik606686e2012-06-04 14:03:51 -04005573
Josef Bacikde1ee922012-10-19 16:50:56 -04005574 submit_stripe_bio(root, bbio, bio,
5575 bbio->stripes[dev_nr].physical, dev_nr, rw,
5576 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005577 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005578 }
Chris Mason0b86a832008-03-24 15:01:56 -04005579 return 0;
5580}
5581
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005582struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005583 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005584{
Yan Zheng2b820322008-11-17 21:11:30 -05005585 struct btrfs_device *device;
5586 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005587
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005588 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005589 while (cur_devices) {
5590 if (!fsid ||
5591 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5592 device = __find_device(&cur_devices->devices,
5593 devid, uuid);
5594 if (device)
5595 return device;
5596 }
5597 cur_devices = cur_devices->seed;
5598 }
5599 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005600}
5601
Chris Masondfe25022008-05-13 13:46:40 -04005602static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5603 u64 devid, u8 *dev_uuid)
5604{
5605 struct btrfs_device *device;
5606 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5607
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005608 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5609 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005610 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005611
5612 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005613 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005614 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005615
5616 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005617 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005618
Chris Masondfe25022008-05-13 13:46:40 -04005619 return device;
5620}
5621
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005622/**
5623 * btrfs_alloc_device - allocate struct btrfs_device
5624 * @fs_info: used only for generating a new devid, can be NULL if
5625 * devid is provided (i.e. @devid != NULL).
5626 * @devid: a pointer to devid for this device. If NULL a new devid
5627 * is generated.
5628 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5629 * is generated.
5630 *
5631 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5632 * on error. Returned struct is not linked onto any lists and can be
5633 * destroyed with kfree() right away.
5634 */
5635struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5636 const u64 *devid,
5637 const u8 *uuid)
5638{
5639 struct btrfs_device *dev;
5640 u64 tmp;
5641
5642 if (!devid && !fs_info) {
5643 WARN_ON(1);
5644 return ERR_PTR(-EINVAL);
5645 }
5646
5647 dev = __alloc_device();
5648 if (IS_ERR(dev))
5649 return dev;
5650
5651 if (devid)
5652 tmp = *devid;
5653 else {
5654 int ret;
5655
5656 ret = find_next_devid(fs_info, &tmp);
5657 if (ret) {
5658 kfree(dev);
5659 return ERR_PTR(ret);
5660 }
5661 }
5662 dev->devid = tmp;
5663
5664 if (uuid)
5665 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5666 else
5667 generate_random_uuid(dev->uuid);
5668
5669 dev->work.func = pending_bios_fn;
5670
5671 return dev;
5672}
5673
Chris Mason0b86a832008-03-24 15:01:56 -04005674static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5675 struct extent_buffer *leaf,
5676 struct btrfs_chunk *chunk)
5677{
5678 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5679 struct map_lookup *map;
5680 struct extent_map *em;
5681 u64 logical;
5682 u64 length;
5683 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005684 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005685 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005686 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005687 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005688
Chris Masone17cade2008-04-15 15:41:47 -04005689 logical = key->offset;
5690 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005691
Chris Mason890871b2009-09-02 16:24:52 -04005692 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005693 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005694 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005695
5696 /* already mapped? */
5697 if (em && em->start <= logical && em->start + em->len > logical) {
5698 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005699 return 0;
5700 } else if (em) {
5701 free_extent_map(em);
5702 }
Chris Mason0b86a832008-03-24 15:01:56 -04005703
David Sterba172ddd62011-04-21 00:48:27 +02005704 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005705 if (!em)
5706 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005707 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5708 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005709 if (!map) {
5710 free_extent_map(em);
5711 return -ENOMEM;
5712 }
5713
5714 em->bdev = (struct block_device *)map;
5715 em->start = logical;
5716 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005717 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005718 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005719 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005720
Chris Mason593060d2008-03-25 16:50:33 -04005721 map->num_stripes = num_stripes;
5722 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5723 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5724 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5725 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5726 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005727 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005728 for (i = 0; i < num_stripes; i++) {
5729 map->stripes[i].physical =
5730 btrfs_stripe_offset_nr(leaf, chunk, i);
5731 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005732 read_extent_buffer(leaf, uuid, (unsigned long)
5733 btrfs_stripe_dev_uuid_nr(chunk, i),
5734 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005735 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5736 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005737 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005738 kfree(map);
5739 free_extent_map(em);
5740 return -EIO;
5741 }
Chris Masondfe25022008-05-13 13:46:40 -04005742 if (!map->stripes[i].dev) {
5743 map->stripes[i].dev =
5744 add_missing_dev(root, devid, uuid);
5745 if (!map->stripes[i].dev) {
5746 kfree(map);
5747 free_extent_map(em);
5748 return -EIO;
5749 }
5750 }
5751 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005752 }
5753
Chris Mason890871b2009-09-02 16:24:52 -04005754 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005755 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005756 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005757 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005758 free_extent_map(em);
5759
5760 return 0;
5761}
5762
Jeff Mahoney143bede2012-03-01 14:56:26 +01005763static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005764 struct btrfs_dev_item *dev_item,
5765 struct btrfs_device *device)
5766{
5767 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005768
5769 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005770 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5771 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005772 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5773 device->type = btrfs_device_type(leaf, dev_item);
5774 device->io_align = btrfs_device_io_align(leaf, dev_item);
5775 device->io_width = btrfs_device_io_width(leaf, dev_item);
5776 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005777 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005778 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005779
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005780 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005781 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005782}
5783
Yan Zheng2b820322008-11-17 21:11:30 -05005784static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5785{
5786 struct btrfs_fs_devices *fs_devices;
5787 int ret;
5788
Li Zefanb367e472011-12-07 11:38:24 +08005789 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005790
5791 fs_devices = root->fs_info->fs_devices->seed;
5792 while (fs_devices) {
5793 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5794 ret = 0;
5795 goto out;
5796 }
5797 fs_devices = fs_devices->seed;
5798 }
5799
5800 fs_devices = find_fsid(fsid);
5801 if (!fs_devices) {
5802 ret = -ENOENT;
5803 goto out;
5804 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005805
5806 fs_devices = clone_fs_devices(fs_devices);
5807 if (IS_ERR(fs_devices)) {
5808 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005809 goto out;
5810 }
5811
Christoph Hellwig97288f22008-12-02 06:36:09 -05005812 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005813 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005814 if (ret) {
5815 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005816 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005817 }
Yan Zheng2b820322008-11-17 21:11:30 -05005818
5819 if (!fs_devices->seeding) {
5820 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005821 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005822 ret = -EINVAL;
5823 goto out;
5824 }
5825
5826 fs_devices->seed = root->fs_info->fs_devices->seed;
5827 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005828out:
Yan Zheng2b820322008-11-17 21:11:30 -05005829 return ret;
5830}
5831
Chris Mason0d81ba52008-03-24 15:02:07 -04005832static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005833 struct extent_buffer *leaf,
5834 struct btrfs_dev_item *dev_item)
5835{
5836 struct btrfs_device *device;
5837 u64 devid;
5838 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005839 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005840 u8 dev_uuid[BTRFS_UUID_SIZE];
5841
Chris Mason0b86a832008-03-24 15:01:56 -04005842 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02005843 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04005844 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02005845 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05005846 BTRFS_UUID_SIZE);
5847
5848 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5849 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005850 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005851 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005852 }
5853
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005854 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005855 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005856 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005857 return -EIO;
5858
5859 if (!device) {
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005860 btrfs_warn(root->fs_info, "devid %llu missing", devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005861 device = add_missing_dev(root, devid, dev_uuid);
5862 if (!device)
5863 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005864 } else if (!device->missing) {
5865 /*
5866 * this happens when a device that was properly setup
5867 * in the device info lists suddenly goes bad.
5868 * device->bdev is NULL, and so we have to set
5869 * device->missing to one here
5870 */
5871 root->fs_info->fs_devices->missing_devices++;
5872 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05005873 }
5874 }
5875
5876 if (device->fs_devices != root->fs_info->fs_devices) {
5877 BUG_ON(device->writeable);
5878 if (device->generation !=
5879 btrfs_device_generation(leaf, dev_item))
5880 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04005881 }
Chris Mason0b86a832008-03-24 15:01:56 -04005882
5883 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04005884 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01005885 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05005886 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04005887 spin_lock(&root->fs_info->free_chunk_lock);
5888 root->fs_info->free_chunk_space += device->total_bytes -
5889 device->bytes_used;
5890 spin_unlock(&root->fs_info->free_chunk_lock);
5891 }
Chris Mason0b86a832008-03-24 15:01:56 -04005892 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005893 return ret;
5894}
5895
Yan Zhenge4404d62008-12-12 10:03:26 -05005896int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04005897{
David Sterba6c417612011-04-13 15:41:04 +02005898 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04005899 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04005900 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04005901 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04005902 u8 *ptr;
5903 unsigned long sb_ptr;
5904 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005905 u32 num_stripes;
5906 u32 array_size;
5907 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005908 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04005909 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04005910
Yan Zhenge4404d62008-12-12 10:03:26 -05005911 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04005912 BTRFS_SUPER_INFO_SIZE);
5913 if (!sb)
5914 return -ENOMEM;
5915 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04005916 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02005917 /*
5918 * The sb extent buffer is artifical and just used to read the system array.
5919 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5920 * pages up-to-date when the page is larger: extent does not cover the
5921 * whole page and consequently check_page_uptodate does not find all
5922 * the page's extents up-to-date (the hole beyond sb),
5923 * write_extent_buffer then triggers a WARN_ON.
5924 *
5925 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5926 * but sb spans only this function. Add an explicit SetPageUptodate call
5927 * to silence the warning eg. on PowerPC 64.
5928 */
5929 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04005930 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05005931
Chris Masona061fc82008-05-07 11:43:44 -04005932 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005933 array_size = btrfs_super_sys_array_size(super_copy);
5934
Chris Mason0b86a832008-03-24 15:01:56 -04005935 ptr = super_copy->sys_chunk_array;
5936 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5937 cur = 0;
5938
5939 while (cur < array_size) {
5940 disk_key = (struct btrfs_disk_key *)ptr;
5941 btrfs_disk_key_to_cpu(&key, disk_key);
5942
Chris Masona061fc82008-05-07 11:43:44 -04005943 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04005944 sb_ptr += len;
5945 cur += len;
5946
Chris Mason0d81ba52008-03-24 15:02:07 -04005947 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04005948 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04005949 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04005950 if (ret)
5951 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005952 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5953 len = btrfs_chunk_item_size(num_stripes);
5954 } else {
Chris Mason84eed902008-04-25 09:04:37 -04005955 ret = -EIO;
5956 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005957 }
5958 ptr += len;
5959 sb_ptr += len;
5960 cur += len;
5961 }
Chris Masona061fc82008-05-07 11:43:44 -04005962 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04005963 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005964}
5965
5966int btrfs_read_chunk_tree(struct btrfs_root *root)
5967{
5968 struct btrfs_path *path;
5969 struct extent_buffer *leaf;
5970 struct btrfs_key key;
5971 struct btrfs_key found_key;
5972 int ret;
5973 int slot;
5974
5975 root = root->fs_info->chunk_root;
5976
5977 path = btrfs_alloc_path();
5978 if (!path)
5979 return -ENOMEM;
5980
Li Zefanb367e472011-12-07 11:38:24 +08005981 mutex_lock(&uuid_mutex);
5982 lock_chunks(root);
5983
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01005984 /*
5985 * Read all device items, and then all the chunk items. All
5986 * device items are found before any chunk item (their object id
5987 * is smaller than the lowest possible object id for a chunk
5988 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04005989 */
5990 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5991 key.offset = 0;
5992 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005993 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005994 if (ret < 0)
5995 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005996 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005997 leaf = path->nodes[0];
5998 slot = path->slots[0];
5999 if (slot >= btrfs_header_nritems(leaf)) {
6000 ret = btrfs_next_leaf(root, path);
6001 if (ret == 0)
6002 continue;
6003 if (ret < 0)
6004 goto error;
6005 break;
6006 }
6007 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006008 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6009 struct btrfs_dev_item *dev_item;
6010 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006011 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006012 ret = read_one_dev(root, leaf, dev_item);
6013 if (ret)
6014 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006015 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6016 struct btrfs_chunk *chunk;
6017 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6018 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006019 if (ret)
6020 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006021 }
6022 path->slots[0]++;
6023 }
Chris Mason0b86a832008-03-24 15:01:56 -04006024 ret = 0;
6025error:
Li Zefanb367e472011-12-07 11:38:24 +08006026 unlock_chunks(root);
6027 mutex_unlock(&uuid_mutex);
6028
Yan Zheng2b820322008-11-17 21:11:30 -05006029 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006030 return ret;
6031}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006032
Miao Xiecb517ea2013-05-15 07:48:19 +00006033void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6034{
6035 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6036 struct btrfs_device *device;
6037
6038 mutex_lock(&fs_devices->device_list_mutex);
6039 list_for_each_entry(device, &fs_devices->devices, dev_list)
6040 device->dev_root = fs_info->dev_root;
6041 mutex_unlock(&fs_devices->device_list_mutex);
6042}
6043
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006044static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6045{
6046 int i;
6047
6048 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6049 btrfs_dev_stat_reset(dev, i);
6050}
6051
6052int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6053{
6054 struct btrfs_key key;
6055 struct btrfs_key found_key;
6056 struct btrfs_root *dev_root = fs_info->dev_root;
6057 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6058 struct extent_buffer *eb;
6059 int slot;
6060 int ret = 0;
6061 struct btrfs_device *device;
6062 struct btrfs_path *path = NULL;
6063 int i;
6064
6065 path = btrfs_alloc_path();
6066 if (!path) {
6067 ret = -ENOMEM;
6068 goto out;
6069 }
6070
6071 mutex_lock(&fs_devices->device_list_mutex);
6072 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6073 int item_size;
6074 struct btrfs_dev_stats_item *ptr;
6075
6076 key.objectid = 0;
6077 key.type = BTRFS_DEV_STATS_KEY;
6078 key.offset = device->devid;
6079 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6080 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006081 __btrfs_reset_dev_stats(device);
6082 device->dev_stats_valid = 1;
6083 btrfs_release_path(path);
6084 continue;
6085 }
6086 slot = path->slots[0];
6087 eb = path->nodes[0];
6088 btrfs_item_key_to_cpu(eb, &found_key, slot);
6089 item_size = btrfs_item_size_nr(eb, slot);
6090
6091 ptr = btrfs_item_ptr(eb, slot,
6092 struct btrfs_dev_stats_item);
6093
6094 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6095 if (item_size >= (1 + i) * sizeof(__le64))
6096 btrfs_dev_stat_set(device, i,
6097 btrfs_dev_stats_value(eb, ptr, i));
6098 else
6099 btrfs_dev_stat_reset(device, i);
6100 }
6101
6102 device->dev_stats_valid = 1;
6103 btrfs_dev_stat_print_on_load(device);
6104 btrfs_release_path(path);
6105 }
6106 mutex_unlock(&fs_devices->device_list_mutex);
6107
6108out:
6109 btrfs_free_path(path);
6110 return ret < 0 ? ret : 0;
6111}
6112
6113static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6114 struct btrfs_root *dev_root,
6115 struct btrfs_device *device)
6116{
6117 struct btrfs_path *path;
6118 struct btrfs_key key;
6119 struct extent_buffer *eb;
6120 struct btrfs_dev_stats_item *ptr;
6121 int ret;
6122 int i;
6123
6124 key.objectid = 0;
6125 key.type = BTRFS_DEV_STATS_KEY;
6126 key.offset = device->devid;
6127
6128 path = btrfs_alloc_path();
6129 BUG_ON(!path);
6130 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6131 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04006132 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
6133 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006134 goto out;
6135 }
6136
6137 if (ret == 0 &&
6138 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6139 /* need to delete old one and insert a new one */
6140 ret = btrfs_del_item(trans, dev_root, path);
6141 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04006142 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
6143 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006144 goto out;
6145 }
6146 ret = 1;
6147 }
6148
6149 if (ret == 1) {
6150 /* need to insert a new item */
6151 btrfs_release_path(path);
6152 ret = btrfs_insert_empty_item(trans, dev_root, path,
6153 &key, sizeof(*ptr));
6154 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04006155 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
6156 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006157 goto out;
6158 }
6159 }
6160
6161 eb = path->nodes[0];
6162 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6163 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6164 btrfs_set_dev_stats_value(eb, ptr, i,
6165 btrfs_dev_stat_read(device, i));
6166 btrfs_mark_buffer_dirty(eb);
6167
6168out:
6169 btrfs_free_path(path);
6170 return ret;
6171}
6172
6173/*
6174 * called from commit_transaction. Writes all changed device stats to disk.
6175 */
6176int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6177 struct btrfs_fs_info *fs_info)
6178{
6179 struct btrfs_root *dev_root = fs_info->dev_root;
6180 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6181 struct btrfs_device *device;
6182 int ret = 0;
6183
6184 mutex_lock(&fs_devices->device_list_mutex);
6185 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6186 if (!device->dev_stats_valid || !device->dev_stats_dirty)
6187 continue;
6188
6189 ret = update_dev_stat_item(trans, dev_root, device);
6190 if (!ret)
6191 device->dev_stats_dirty = 0;
6192 }
6193 mutex_unlock(&fs_devices->device_list_mutex);
6194
6195 return ret;
6196}
6197
Stefan Behrens442a4f62012-05-25 16:06:08 +02006198void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6199{
6200 btrfs_dev_stat_inc(dev, index);
6201 btrfs_dev_stat_print_on_error(dev);
6202}
6203
Eric Sandeen48a3b632013-04-25 20:41:01 +00006204static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006205{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006206 if (!dev->dev_stats_valid)
6207 return;
Josef Bacik606686e2012-06-04 14:03:51 -04006208 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02006209 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006210 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006211 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6212 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6213 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6214 btrfs_dev_stat_read(dev,
6215 BTRFS_DEV_STAT_CORRUPTION_ERRS),
6216 btrfs_dev_stat_read(dev,
6217 BTRFS_DEV_STAT_GENERATION_ERRS));
6218}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006219
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006220static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6221{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006222 int i;
6223
6224 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6225 if (btrfs_dev_stat_read(dev, i) != 0)
6226 break;
6227 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6228 return; /* all values == 0, suppress message */
6229
Josef Bacik606686e2012-06-04 14:03:51 -04006230 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6231 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006232 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6233 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6234 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6235 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6236 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6237}
6238
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006239int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006240 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006241{
6242 struct btrfs_device *dev;
6243 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6244 int i;
6245
6246 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006247 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006248 mutex_unlock(&fs_devices->device_list_mutex);
6249
6250 if (!dev) {
6251 printk(KERN_WARNING
6252 "btrfs: get dev_stats failed, device not found\n");
6253 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006254 } else if (!dev->dev_stats_valid) {
6255 printk(KERN_WARNING
6256 "btrfs: get dev_stats failed, not yet valid\n");
6257 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006258 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006259 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6260 if (stats->nr_items > i)
6261 stats->values[i] =
6262 btrfs_dev_stat_read_and_reset(dev, i);
6263 else
6264 btrfs_dev_stat_reset(dev, i);
6265 }
6266 } else {
6267 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6268 if (stats->nr_items > i)
6269 stats->values[i] = btrfs_dev_stat_read(dev, i);
6270 }
6271 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6272 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6273 return 0;
6274}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006275
6276int btrfs_scratch_superblock(struct btrfs_device *device)
6277{
6278 struct buffer_head *bh;
6279 struct btrfs_super_block *disk_super;
6280
6281 bh = btrfs_read_dev_super(device->bdev);
6282 if (!bh)
6283 return -EINVAL;
6284 disk_super = (struct btrfs_super_block *)bh->b_data;
6285
6286 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6287 set_buffer_dirty(bh);
6288 sync_dirty_buffer(bh);
6289 brelse(bh);
6290
6291 return 0;
6292}