blob: db72e0cc6f87920817bfabb38a88257bc501451e [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050028#include <linux/raid/pq.h>
29#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050030#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "ctree.h"
32#include "extent_map.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "print-tree.h"
36#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040038#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010039#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060041#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Chris Mason0b86a832008-03-24 15:01:56 -040043
Yan Zheng2b820322008-11-17 21:11:30 -050044static int init_first_rw_device(struct btrfs_trans_handle *trans,
45 struct btrfs_root *root,
46 struct btrfs_device *device);
47static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020048static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
49static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050050
Chris Mason8a4b83c2008-03-24 15:02:07 -040051static DEFINE_MUTEX(uuid_mutex);
52static LIST_HEAD(fs_uuids);
53
Chris Mason7d9eb122008-07-08 14:19:17 -040054static void lock_chunks(struct btrfs_root *root)
55{
Chris Mason7d9eb122008-07-08 14:19:17 -040056 mutex_lock(&root->fs_info->chunk_mutex);
57}
58
59static void unlock_chunks(struct btrfs_root *root)
60{
Chris Mason7d9eb122008-07-08 14:19:17 -040061 mutex_unlock(&root->fs_info->chunk_mutex);
62}
63
Yan Zhenge4404d62008-12-12 10:03:26 -050064static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
65{
66 struct btrfs_device *device;
67 WARN_ON(fs_devices->opened);
68 while (!list_empty(&fs_devices->devices)) {
69 device = list_entry(fs_devices->devices.next,
70 struct btrfs_device, dev_list);
71 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -040072 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -050073 kfree(device);
74 }
75 kfree(fs_devices);
76}
77
Lukas Czernerb8b8ff52012-12-06 19:25:48 +000078static void btrfs_kobject_uevent(struct block_device *bdev,
79 enum kobject_action action)
80{
81 int ret;
82
83 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
84 if (ret)
85 pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
86 action,
87 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
88 &disk_to_dev(bdev->bd_disk)->kobj);
89}
90
Jeff Mahoney143bede2012-03-01 14:56:26 +010091void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040092{
93 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040094
Yan Zheng2b820322008-11-17 21:11:30 -050095 while (!list_empty(&fs_uuids)) {
96 fs_devices = list_entry(fs_uuids.next,
97 struct btrfs_fs_devices, list);
98 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050099 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400100 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101}
102
Chris Masona1b32a52008-09-05 16:09:51 -0400103static noinline struct btrfs_device *__find_device(struct list_head *head,
104 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400105{
106 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400107
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500108 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400109 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400110 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400111 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400112 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400113 }
114 return NULL;
115}
116
Chris Masona1b32a52008-09-05 16:09:51 -0400117static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400118{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400119 struct btrfs_fs_devices *fs_devices;
120
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500121 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400122 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
123 return fs_devices;
124 }
125 return NULL;
126}
127
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100128static int
129btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
130 int flush, struct block_device **bdev,
131 struct buffer_head **bh)
132{
133 int ret;
134
135 *bdev = blkdev_get_by_path(device_path, flags, holder);
136
137 if (IS_ERR(*bdev)) {
138 ret = PTR_ERR(*bdev);
139 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
140 goto error;
141 }
142
143 if (flush)
144 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
145 ret = set_blocksize(*bdev, 4096);
146 if (ret) {
147 blkdev_put(*bdev, flags);
148 goto error;
149 }
150 invalidate_bdev(*bdev);
151 *bh = btrfs_read_dev_super(*bdev);
152 if (!*bh) {
153 ret = -EINVAL;
154 blkdev_put(*bdev, flags);
155 goto error;
156 }
157
158 return 0;
159
160error:
161 *bdev = NULL;
162 *bh = NULL;
163 return ret;
164}
165
Chris Masonffbd5172009-04-20 15:50:09 -0400166static void requeue_list(struct btrfs_pending_bios *pending_bios,
167 struct bio *head, struct bio *tail)
168{
169
170 struct bio *old_head;
171
172 old_head = pending_bios->head;
173 pending_bios->head = head;
174 if (pending_bios->tail)
175 tail->bi_next = old_head;
176 else
177 pending_bios->tail = tail;
178}
179
Chris Mason8b712842008-06-11 16:50:36 -0400180/*
181 * we try to collect pending bios for a device so we don't get a large
182 * number of procs sending bios down to the same device. This greatly
183 * improves the schedulers ability to collect and merge the bios.
184 *
185 * But, it also turns into a long list of bios to process and that is sure
186 * to eventually make the worker thread block. The solution here is to
187 * make some progress and then put this work struct back at the end of
188 * the list if the block device is congested. This way, multiple devices
189 * can make progress from a single worker thread.
190 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100191static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400192{
193 struct bio *pending;
194 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400195 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400196 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400197 struct bio *tail;
198 struct bio *cur;
199 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400200 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400201 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400202 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400203 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400204 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000205 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400206 struct blk_plug plug;
207
208 /*
209 * this function runs all the bios we've collected for
210 * a particular device. We don't want to wander off to
211 * another device without first sending all of these down.
212 * So, setup a plug here and finish it off before we return
213 */
214 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400215
Chris Masonbedf7622009-04-03 10:32:58 -0400216 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400217 fs_info = device->dev_root->fs_info;
218 limit = btrfs_async_submit_limit(fs_info);
219 limit = limit * 2 / 3;
220
Chris Mason8b712842008-06-11 16:50:36 -0400221loop:
222 spin_lock(&device->io_lock);
223
Chris Masona6837052009-02-04 09:19:41 -0500224loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400225 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400226
Chris Mason8b712842008-06-11 16:50:36 -0400227 /* take all the bios off the list at once and process them
228 * later on (without the lock held). But, remember the
229 * tail and other pointers so the bios can be properly reinserted
230 * into the list if we hit congestion
231 */
Chris Masond84275c2009-06-09 15:39:08 -0400232 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400233 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400234 force_reg = 1;
235 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400236 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400237 force_reg = 0;
238 }
Chris Masonffbd5172009-04-20 15:50:09 -0400239
240 pending = pending_bios->head;
241 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400242 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400243
244 /*
245 * if pending was null this time around, no bios need processing
246 * at all and we can stop. Otherwise it'll loop back up again
247 * and do an additional check so no bios are missed.
248 *
249 * device->running_pending is used to synchronize with the
250 * schedule_bio code.
251 */
Chris Masonffbd5172009-04-20 15:50:09 -0400252 if (device->pending_sync_bios.head == NULL &&
253 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400254 again = 0;
255 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400256 } else {
257 again = 1;
258 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400259 }
Chris Masonffbd5172009-04-20 15:50:09 -0400260
261 pending_bios->head = NULL;
262 pending_bios->tail = NULL;
263
Chris Mason8b712842008-06-11 16:50:36 -0400264 spin_unlock(&device->io_lock);
265
Chris Masond3977122009-01-05 21:25:51 -0500266 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400267
268 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400269 /* we want to work on both lists, but do more bios on the
270 * sync list than the regular list
271 */
272 if ((num_run > 32 &&
273 pending_bios != &device->pending_sync_bios &&
274 device->pending_sync_bios.head) ||
275 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
276 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400277 spin_lock(&device->io_lock);
278 requeue_list(pending_bios, pending, tail);
279 goto loop_lock;
280 }
281
Chris Mason8b712842008-06-11 16:50:36 -0400282 cur = pending;
283 pending = pending->bi_next;
284 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400285
Josef Bacik66657b32012-08-01 15:36:24 -0400286 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400287 waitqueue_active(&fs_info->async_submit_wait))
288 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400289
290 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400291
Chris Mason2ab1ba62011-08-04 14:28:36 -0400292 /*
293 * if we're doing the sync list, record that our
294 * plug has some sync requests on it
295 *
296 * If we're doing the regular list and there are
297 * sync requests sitting around, unplug before
298 * we add more
299 */
300 if (pending_bios == &device->pending_sync_bios) {
301 sync_pending = 1;
302 } else if (sync_pending) {
303 blk_finish_plug(&plug);
304 blk_start_plug(&plug);
305 sync_pending = 0;
306 }
307
Stefan Behrens21adbd52011-11-09 13:44:05 +0100308 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400309 num_run++;
310 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100311 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400312 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400313
314 /*
315 * we made progress, there is more work to do and the bdi
316 * is now congested. Back off and let other work structs
317 * run instead
318 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400319 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500320 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400321 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400322
Chris Masonb765ead2009-04-03 10:27:10 -0400323 ioc = current->io_context;
324
325 /*
326 * the main goal here is that we don't want to
327 * block if we're going to be able to submit
328 * more requests without blocking.
329 *
330 * This code does two great things, it pokes into
331 * the elevator code from a filesystem _and_
332 * it makes assumptions about how batching works.
333 */
334 if (ioc && ioc->nr_batch_requests > 0 &&
335 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
336 (last_waited == 0 ||
337 ioc->last_waited == last_waited)) {
338 /*
339 * we want to go through our batch of
340 * requests and stop. So, we copy out
341 * the ioc->last_waited time and test
342 * against it before looping
343 */
344 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100345 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400346 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400347 continue;
348 }
Chris Mason8b712842008-06-11 16:50:36 -0400349 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400350 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500351 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400352
353 spin_unlock(&device->io_lock);
354 btrfs_requeue_work(&device->work);
355 goto done;
356 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500357 /* unplug every 64 requests just for good measure */
358 if (batch_run % 64 == 0) {
359 blk_finish_plug(&plug);
360 blk_start_plug(&plug);
361 sync_pending = 0;
362 }
Chris Mason8b712842008-06-11 16:50:36 -0400363 }
Chris Masonffbd5172009-04-20 15:50:09 -0400364
Chris Mason51684082010-03-10 15:33:32 -0500365 cond_resched();
366 if (again)
367 goto loop;
368
369 spin_lock(&device->io_lock);
370 if (device->pending_bios.head || device->pending_sync_bios.head)
371 goto loop_lock;
372 spin_unlock(&device->io_lock);
373
Chris Mason8b712842008-06-11 16:50:36 -0400374done:
Chris Mason211588a2011-04-19 20:12:40 -0400375 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400376}
377
Christoph Hellwigb2950862008-12-02 09:54:17 -0500378static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400379{
380 struct btrfs_device *device;
381
382 device = container_of(work, struct btrfs_device, work);
383 run_scheduled_bios(device);
384}
385
Chris Masona1b32a52008-09-05 16:09:51 -0400386static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400387 struct btrfs_super_block *disk_super,
388 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
389{
390 struct btrfs_device *device;
391 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400392 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400393 u64 found_transid = btrfs_super_generation(disk_super);
394
395 fs_devices = find_fsid(disk_super->fsid);
396 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400397 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400398 if (!fs_devices)
399 return -ENOMEM;
400 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400401 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400402 list_add(&fs_devices->list, &fs_uuids);
403 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
404 fs_devices->latest_devid = devid;
405 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400406 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400407 device = NULL;
408 } else {
Chris Masona4437552008-04-18 10:29:38 -0400409 device = __find_device(&fs_devices->devices, devid,
410 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400411 }
412 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500413 if (fs_devices->opened)
414 return -EBUSY;
415
Chris Mason8a4b83c2008-03-24 15:02:07 -0400416 device = kzalloc(sizeof(*device), GFP_NOFS);
417 if (!device) {
418 /* we can safely leave the fs_devices entry around */
419 return -ENOMEM;
420 }
421 device->devid = devid;
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200422 device->dev_stats_valid = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400423 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400424 memcpy(device->uuid, disk_super->dev_item.uuid,
425 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400426 spin_lock_init(&device->io_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400427
428 name = rcu_string_strdup(path, GFP_NOFS);
429 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400430 kfree(device);
431 return -ENOMEM;
432 }
Josef Bacik606686e2012-06-04 14:03:51 -0400433 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -0500434 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400435
Arne Jansen90519d62011-05-23 14:30:00 +0200436 /* init readahead state */
437 spin_lock_init(&device->reada_lock);
438 device->reada_curr_zone = NULL;
439 atomic_set(&device->reada_in_flight, 0);
440 device->reada_next = 0;
441 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
442 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
443
Chris Masone5e9a522009-06-10 15:17:02 -0400444 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000445 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400446 mutex_unlock(&fs_devices->device_list_mutex);
447
Yan Zheng2b820322008-11-17 21:11:30 -0500448 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400449 fs_devices->num_devices++;
Josef Bacik606686e2012-06-04 14:03:51 -0400450 } else if (!device->name || strcmp(device->name->str, path)) {
451 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000452 if (!name)
453 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400454 rcu_string_free(device->name);
455 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500456 if (device->missing) {
457 fs_devices->missing_devices--;
458 device->missing = 0;
459 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400460 }
461
462 if (found_transid > fs_devices->latest_trans) {
463 fs_devices->latest_devid = devid;
464 fs_devices->latest_trans = found_transid;
465 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400466 *fs_devices_ret = fs_devices;
467 return 0;
468}
469
Yan Zhenge4404d62008-12-12 10:03:26 -0500470static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
471{
472 struct btrfs_fs_devices *fs_devices;
473 struct btrfs_device *device;
474 struct btrfs_device *orig_dev;
475
476 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
477 if (!fs_devices)
478 return ERR_PTR(-ENOMEM);
479
480 INIT_LIST_HEAD(&fs_devices->devices);
481 INIT_LIST_HEAD(&fs_devices->alloc_list);
482 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400483 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500484 fs_devices->latest_devid = orig->latest_devid;
485 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400486 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500487 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
488
Xiao Guangrong46224702011-04-20 10:08:47 +0000489 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500490 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400491 struct rcu_string *name;
492
Yan Zhenge4404d62008-12-12 10:03:26 -0500493 device = kzalloc(sizeof(*device), GFP_NOFS);
494 if (!device)
495 goto error;
496
Josef Bacik606686e2012-06-04 14:03:51 -0400497 /*
498 * This is ok to do without rcu read locked because we hold the
499 * uuid mutex so nothing we touch in here is going to disappear.
500 */
501 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
502 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400503 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500504 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400505 }
Josef Bacik606686e2012-06-04 14:03:51 -0400506 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500507
508 device->devid = orig_dev->devid;
509 device->work.func = pending_bios_fn;
510 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500511 spin_lock_init(&device->io_lock);
512 INIT_LIST_HEAD(&device->dev_list);
513 INIT_LIST_HEAD(&device->dev_alloc_list);
514
515 list_add(&device->dev_list, &fs_devices->devices);
516 device->fs_devices = fs_devices;
517 fs_devices->num_devices++;
518 }
519 return fs_devices;
520error:
521 free_fs_devices(fs_devices);
522 return ERR_PTR(-ENOMEM);
523}
524
Stefan Behrens8dabb742012-11-06 13:15:27 +0100525void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
526 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400527{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500528 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400529
Chris Masona6b0d5c2012-02-20 20:53:43 -0500530 struct block_device *latest_bdev = NULL;
531 u64 latest_devid = 0;
532 u64 latest_transid = 0;
533
Chris Masondfe25022008-05-13 13:46:40 -0400534 mutex_lock(&uuid_mutex);
535again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000536 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500537 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500538 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100539 if (!device->is_tgtdev_for_dev_replace &&
540 (!latest_transid ||
541 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500542 latest_devid = device->devid;
543 latest_transid = device->generation;
544 latest_bdev = device->bdev;
545 }
Yan Zheng2b820322008-11-17 21:11:30 -0500546 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500547 }
Yan Zheng2b820322008-11-17 21:11:30 -0500548
Stefan Behrens8dabb742012-11-06 13:15:27 +0100549 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
550 /*
551 * In the first step, keep the device which has
552 * the correct fsid and the devid that is used
553 * for the dev_replace procedure.
554 * In the second step, the dev_replace state is
555 * read from the device tree and it is known
556 * whether the procedure is really active or
557 * not, which means whether this device is
558 * used or whether it should be removed.
559 */
560 if (step == 0 || device->is_tgtdev_for_dev_replace) {
561 continue;
562 }
563 }
Yan Zheng2b820322008-11-17 21:11:30 -0500564 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100565 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500566 device->bdev = NULL;
567 fs_devices->open_devices--;
568 }
569 if (device->writeable) {
570 list_del_init(&device->dev_alloc_list);
571 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100572 if (!device->is_tgtdev_for_dev_replace)
573 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500574 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500575 list_del_init(&device->dev_list);
576 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400577 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500578 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400579 }
Yan Zheng2b820322008-11-17 21:11:30 -0500580
581 if (fs_devices->seed) {
582 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500583 goto again;
584 }
585
Chris Masona6b0d5c2012-02-20 20:53:43 -0500586 fs_devices->latest_bdev = latest_bdev;
587 fs_devices->latest_devid = latest_devid;
588 fs_devices->latest_trans = latest_transid;
589
Chris Masondfe25022008-05-13 13:46:40 -0400590 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400591}
Chris Masona0af4692008-05-13 16:03:06 -0400592
Xiao Guangrong1f781602011-04-20 10:09:16 +0000593static void __free_device(struct work_struct *work)
594{
595 struct btrfs_device *device;
596
597 device = container_of(work, struct btrfs_device, rcu_work);
598
599 if (device->bdev)
600 blkdev_put(device->bdev, device->mode);
601
Josef Bacik606686e2012-06-04 14:03:51 -0400602 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000603 kfree(device);
604}
605
606static void free_device(struct rcu_head *head)
607{
608 struct btrfs_device *device;
609
610 device = container_of(head, struct btrfs_device, rcu);
611
612 INIT_WORK(&device->rcu_work, __free_device);
613 schedule_work(&device->rcu_work);
614}
615
Yan Zheng2b820322008-11-17 21:11:30 -0500616static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400617{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400618 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500619
Yan Zheng2b820322008-11-17 21:11:30 -0500620 if (--fs_devices->opened > 0)
621 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400622
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000623 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500624 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000625 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400626 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000627
628 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400629 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000630
Stefan Behrens8dabb742012-11-06 13:15:27 +0100631 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500632 list_del_init(&device->dev_alloc_list);
633 fs_devices->rw_devices--;
634 }
635
Josef Bacikd5e20032011-08-04 14:52:27 +0000636 if (device->can_discard)
637 fs_devices->num_can_discard--;
638
Xiao Guangrong1f781602011-04-20 10:09:16 +0000639 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100640 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000641 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400642
643 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400644 if (device->name) {
645 name = rcu_string_strdup(device->name->str, GFP_NOFS);
646 BUG_ON(device->name && !name); /* -ENOMEM */
647 rcu_assign_pointer(new_device->name, name);
648 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000649 new_device->bdev = NULL;
650 new_device->writeable = 0;
651 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000652 new_device->can_discard = 0;
Thomas Gleixner1cba0cd2013-02-20 14:06:20 -0500653 spin_lock_init(&new_device->io_lock);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000654 list_replace_rcu(&device->dev_list, &new_device->dev_list);
655
656 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400657 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000658 mutex_unlock(&fs_devices->device_list_mutex);
659
Yan Zhenge4404d62008-12-12 10:03:26 -0500660 WARN_ON(fs_devices->open_devices);
661 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500662 fs_devices->opened = 0;
663 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500664
Chris Mason8a4b83c2008-03-24 15:02:07 -0400665 return 0;
666}
667
Yan Zheng2b820322008-11-17 21:11:30 -0500668int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
669{
Yan Zhenge4404d62008-12-12 10:03:26 -0500670 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500671 int ret;
672
673 mutex_lock(&uuid_mutex);
674 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500675 if (!fs_devices->opened) {
676 seed_devices = fs_devices->seed;
677 fs_devices->seed = NULL;
678 }
Yan Zheng2b820322008-11-17 21:11:30 -0500679 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500680
681 while (seed_devices) {
682 fs_devices = seed_devices;
683 seed_devices = fs_devices->seed;
684 __btrfs_close_devices(fs_devices);
685 free_fs_devices(fs_devices);
686 }
Yan Zheng2b820322008-11-17 21:11:30 -0500687 return ret;
688}
689
Yan Zhenge4404d62008-12-12 10:03:26 -0500690static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
691 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400692{
Josef Bacikd5e20032011-08-04 14:52:27 +0000693 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400694 struct block_device *bdev;
695 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400697 struct block_device *latest_bdev = NULL;
698 struct buffer_head *bh;
699 struct btrfs_super_block *disk_super;
700 u64 latest_devid = 0;
701 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400702 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500703 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400704 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400705
Tejun Heod4d77622010-11-13 11:55:18 +0100706 flags |= FMODE_EXCL;
707
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500708 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400709 if (device->bdev)
710 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400711 if (!device->name)
712 continue;
713
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100714 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
715 &bdev, &bh);
716 if (ret)
717 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400718
719 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000720 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400721 if (devid != device->devid)
722 goto error_brelse;
723
Yan Zheng2b820322008-11-17 21:11:30 -0500724 if (memcmp(device->uuid, disk_super->dev_item.uuid,
725 BTRFS_UUID_SIZE))
726 goto error_brelse;
727
728 device->generation = btrfs_super_generation(disk_super);
729 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400730 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500731 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400732 latest_bdev = bdev;
733 }
734
Yan Zheng2b820322008-11-17 21:11:30 -0500735 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
736 device->writeable = 0;
737 } else {
738 device->writeable = !bdev_read_only(bdev);
739 seeding = 0;
740 }
741
Josef Bacikd5e20032011-08-04 14:52:27 +0000742 q = bdev_get_queue(bdev);
743 if (blk_queue_discard(q)) {
744 device->can_discard = 1;
745 fs_devices->num_can_discard++;
746 }
747
Chris Mason8a4b83c2008-03-24 15:02:07 -0400748 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400749 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500750 device->mode = flags;
751
Chris Masonc2898112009-06-10 09:51:32 -0400752 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
753 fs_devices->rotating = 1;
754
Chris Masona0af4692008-05-13 16:03:06 -0400755 fs_devices->open_devices++;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100756 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500757 fs_devices->rw_devices++;
758 list_add(&device->dev_alloc_list,
759 &fs_devices->alloc_list);
760 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000761 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400762 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400763
Chris Masona0af4692008-05-13 16:03:06 -0400764error_brelse:
765 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100766 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400767 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400768 }
Chris Masona0af4692008-05-13 16:03:06 -0400769 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300770 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400771 goto out;
772 }
Yan Zheng2b820322008-11-17 21:11:30 -0500773 fs_devices->seeding = seeding;
774 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400775 fs_devices->latest_bdev = latest_bdev;
776 fs_devices->latest_devid = latest_devid;
777 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500778 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400779out:
Yan Zheng2b820322008-11-17 21:11:30 -0500780 return ret;
781}
782
783int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500784 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500785{
786 int ret;
787
788 mutex_lock(&uuid_mutex);
789 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500790 fs_devices->opened++;
791 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500792 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500793 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500794 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400795 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400796 return ret;
797}
798
David Sterba6f60cbd2013-02-15 11:31:02 -0700799/*
800 * Look for a btrfs signature on a device. This may be called out of the mount path
801 * and we are not allowed to call set_blocksize during the scan. The superblock
802 * is read via pagecache
803 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500804int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400805 struct btrfs_fs_devices **fs_devices_ret)
806{
807 struct btrfs_super_block *disk_super;
808 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700809 struct page *page;
810 void *p;
811 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400812 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400813 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400814 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700815 u64 bytenr;
816 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400817
David Sterba6f60cbd2013-02-15 11:31:02 -0700818 /*
819 * we would like to check all the supers, but that would make
820 * a btrfs mount succeed after a mkfs from a different FS.
821 * So, we need to add a special mount option to scan for
822 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
823 */
824 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100825 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500826 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700827
828 bdev = blkdev_get_by_path(path, flags, holder);
829
830 if (IS_ERR(bdev)) {
831 ret = PTR_ERR(bdev);
832 printk(KERN_INFO "btrfs: open %s failed\n", path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100833 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700834 }
835
836 /* make sure our super fits in the device */
837 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
838 goto error_bdev_put;
839
840 /* make sure our super fits in the page */
841 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
842 goto error_bdev_put;
843
844 /* make sure our super doesn't straddle pages on disk */
845 index = bytenr >> PAGE_CACHE_SHIFT;
846 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
847 goto error_bdev_put;
848
849 /* pull in the page with our super */
850 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
851 index, GFP_NOFS);
852
853 if (IS_ERR_OR_NULL(page))
854 goto error_bdev_put;
855
856 p = kmap(page);
857
858 /* align our pointer to the offset of the super block */
859 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
860
861 if (btrfs_super_bytenr(disk_super) != bytenr ||
Zach Browncdb4c572013-02-20 00:55:13 +0000862 disk_super->magic != cpu_to_le64(BTRFS_MAGIC))
David Sterba6f60cbd2013-02-15 11:31:02 -0700863 goto error_unmap;
864
Xiao Guangronga3438322010-01-06 11:48:18 +0000865 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400866 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400867 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700868
Stefan Behrensd03f9182012-11-05 13:10:49 +0000869 if (disk_super->label[0]) {
870 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
871 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Chris Masond3977122009-01-05 21:25:51 -0500872 printk(KERN_INFO "device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000873 } else {
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200874 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000875 }
David Sterba6f60cbd2013-02-15 11:31:02 -0700876
Roland Dreier119e10c2009-01-21 10:49:16 -0500877 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500878 (unsigned long long)devid, (unsigned long long)transid, path);
David Sterba6f60cbd2013-02-15 11:31:02 -0700879
Chris Mason8a4b83c2008-03-24 15:02:07 -0400880 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400881 if (!ret && fs_devices_ret)
882 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700883
884error_unmap:
885 kunmap(page);
886 page_cache_release(page);
887
888error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100889 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400890error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100891 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400892 return ret;
893}
Chris Mason0b86a832008-03-24 15:01:56 -0400894
Miao Xie6d07bce2011-01-05 10:07:31 +0000895/* helper to account the used device space in the range */
896int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
897 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400898{
899 struct btrfs_key key;
900 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000901 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500902 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000903 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400904 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000905 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400906 struct extent_buffer *l;
907
Miao Xie6d07bce2011-01-05 10:07:31 +0000908 *length = 0;
909
Stefan Behrens63a212a2012-11-05 18:29:28 +0100910 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000911 return 0;
912
Yan Zheng2b820322008-11-17 21:11:30 -0500913 path = btrfs_alloc_path();
914 if (!path)
915 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400916 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400917
Chris Mason0b86a832008-03-24 15:01:56 -0400918 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000919 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400920 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000921
922 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400923 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000924 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400925 if (ret > 0) {
926 ret = btrfs_previous_item(root, path, key.objectid, key.type);
927 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000928 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400929 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000930
Chris Mason0b86a832008-03-24 15:01:56 -0400931 while (1) {
932 l = path->nodes[0];
933 slot = path->slots[0];
934 if (slot >= btrfs_header_nritems(l)) {
935 ret = btrfs_next_leaf(root, path);
936 if (ret == 0)
937 continue;
938 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000939 goto out;
940
941 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400942 }
943 btrfs_item_key_to_cpu(l, &key, slot);
944
945 if (key.objectid < device->devid)
946 goto next;
947
948 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000949 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400950
Chris Masond3977122009-01-05 21:25:51 -0500951 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400952 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400953
Chris Mason0b86a832008-03-24 15:01:56 -0400954 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000955 extent_end = key.offset + btrfs_dev_extent_length(l,
956 dev_extent);
957 if (key.offset <= start && extent_end > end) {
958 *length = end - start + 1;
959 break;
960 } else if (key.offset <= start && extent_end > start)
961 *length += extent_end - start;
962 else if (key.offset > start && extent_end <= end)
963 *length += extent_end - key.offset;
964 else if (key.offset > start && key.offset <= end) {
965 *length += end - key.offset + 1;
966 break;
967 } else if (key.offset > end)
968 break;
969
970next:
971 path->slots[0]++;
972 }
973 ret = 0;
974out:
975 btrfs_free_path(path);
976 return ret;
977}
978
Chris Mason0b86a832008-03-24 15:01:56 -0400979/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000980 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000981 * @device: the device which we search the free space in
982 * @num_bytes: the size of the free space that we need
983 * @start: store the start of the free space.
984 * @len: the size of the free space. that we find, or the size of the max
985 * free space if we don't find suitable free space
986 *
Chris Mason0b86a832008-03-24 15:01:56 -0400987 * this uses a pretty simple search, the expectation is that it is
988 * called very infrequently and that a given device has a small number
989 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000990 *
991 * @start is used to store the start of the free space if we find. But if we
992 * don't find suitable free space, it will be used to store the start position
993 * of the max free space.
994 *
995 * @len is used to store the size of the free space that we find.
996 * But if we don't find suitable free space, it is used to store the size of
997 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400998 */
Li Zefan125ccb02011-12-08 15:07:24 +0800999int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001000 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001001{
1002 struct btrfs_key key;
1003 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001004 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001005 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001006 u64 hole_size;
1007 u64 max_hole_start;
1008 u64 max_hole_size;
1009 u64 extent_end;
1010 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001011 u64 search_end = device->total_bytes;
1012 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001013 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001014 struct extent_buffer *l;
1015
Chris Mason0b86a832008-03-24 15:01:56 -04001016 /* FIXME use last free of some kind */
1017
1018 /* we don't want to overwrite the superblock on the drive,
1019 * so we make sure to start at an offset of at least 1MB
1020 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001021 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001022
Miao Xie7bfc8372011-01-05 10:07:26 +00001023 max_hole_start = search_start;
1024 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001025 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001026
Stefan Behrens63a212a2012-11-05 18:29:28 +01001027 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001028 ret = -ENOSPC;
1029 goto error;
1030 }
1031
1032 path = btrfs_alloc_path();
1033 if (!path) {
1034 ret = -ENOMEM;
1035 goto error;
1036 }
1037 path->reada = 2;
1038
Chris Mason0b86a832008-03-24 15:01:56 -04001039 key.objectid = device->devid;
1040 key.offset = search_start;
1041 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001042
Li Zefan125ccb02011-12-08 15:07:24 +08001043 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001044 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001045 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001046 if (ret > 0) {
1047 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1048 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001049 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001050 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001051
Chris Mason0b86a832008-03-24 15:01:56 -04001052 while (1) {
1053 l = path->nodes[0];
1054 slot = path->slots[0];
1055 if (slot >= btrfs_header_nritems(l)) {
1056 ret = btrfs_next_leaf(root, path);
1057 if (ret == 0)
1058 continue;
1059 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001060 goto out;
1061
1062 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001063 }
1064 btrfs_item_key_to_cpu(l, &key, slot);
1065
1066 if (key.objectid < device->devid)
1067 goto next;
1068
1069 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001070 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001071
Chris Mason0b86a832008-03-24 15:01:56 -04001072 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1073 goto next;
1074
Miao Xie7bfc8372011-01-05 10:07:26 +00001075 if (key.offset > search_start) {
1076 hole_size = key.offset - search_start;
1077
1078 if (hole_size > max_hole_size) {
1079 max_hole_start = search_start;
1080 max_hole_size = hole_size;
1081 }
1082
1083 /*
1084 * If this free space is greater than which we need,
1085 * it must be the max free space that we have found
1086 * until now, so max_hole_start must point to the start
1087 * of this free space and the length of this free space
1088 * is stored in max_hole_size. Thus, we return
1089 * max_hole_start and max_hole_size and go back to the
1090 * caller.
1091 */
1092 if (hole_size >= num_bytes) {
1093 ret = 0;
1094 goto out;
1095 }
1096 }
1097
Chris Mason0b86a832008-03-24 15:01:56 -04001098 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001099 extent_end = key.offset + btrfs_dev_extent_length(l,
1100 dev_extent);
1101 if (extent_end > search_start)
1102 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001103next:
1104 path->slots[0]++;
1105 cond_resched();
1106 }
Chris Mason0b86a832008-03-24 15:01:56 -04001107
liubo38c01b92011-08-02 02:39:03 +00001108 /*
1109 * At this point, search_start should be the end of
1110 * allocated dev extents, and when shrinking the device,
1111 * search_end may be smaller than search_start.
1112 */
1113 if (search_end > search_start)
1114 hole_size = search_end - search_start;
1115
Miao Xie7bfc8372011-01-05 10:07:26 +00001116 if (hole_size > max_hole_size) {
1117 max_hole_start = search_start;
1118 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001119 }
Chris Mason0b86a832008-03-24 15:01:56 -04001120
Miao Xie7bfc8372011-01-05 10:07:26 +00001121 /* See above. */
1122 if (hole_size < num_bytes)
1123 ret = -ENOSPC;
1124 else
1125 ret = 0;
1126
1127out:
Yan Zheng2b820322008-11-17 21:11:30 -05001128 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001129error:
1130 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001131 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001132 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001133 return ret;
1134}
1135
Christoph Hellwigb2950862008-12-02 09:54:17 -05001136static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001137 struct btrfs_device *device,
1138 u64 start)
1139{
1140 int ret;
1141 struct btrfs_path *path;
1142 struct btrfs_root *root = device->dev_root;
1143 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001144 struct btrfs_key found_key;
1145 struct extent_buffer *leaf = NULL;
1146 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001147
1148 path = btrfs_alloc_path();
1149 if (!path)
1150 return -ENOMEM;
1151
1152 key.objectid = device->devid;
1153 key.offset = start;
1154 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001155again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001156 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001157 if (ret > 0) {
1158 ret = btrfs_previous_item(root, path, key.objectid,
1159 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001160 if (ret)
1161 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001162 leaf = path->nodes[0];
1163 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1164 extent = btrfs_item_ptr(leaf, path->slots[0],
1165 struct btrfs_dev_extent);
1166 BUG_ON(found_key.offset > start || found_key.offset +
1167 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001168 key = found_key;
1169 btrfs_release_path(path);
1170 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001171 } else if (ret == 0) {
1172 leaf = path->nodes[0];
1173 extent = btrfs_item_ptr(leaf, path->slots[0],
1174 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001175 } else {
1176 btrfs_error(root->fs_info, ret, "Slot search failed");
1177 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001178 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001179
Josef Bacik2bf64752011-09-26 17:12:22 -04001180 if (device->bytes_used > 0) {
1181 u64 len = btrfs_dev_extent_length(leaf, extent);
1182 device->bytes_used -= len;
1183 spin_lock(&root->fs_info->free_chunk_lock);
1184 root->fs_info->free_chunk_space += len;
1185 spin_unlock(&root->fs_info->free_chunk_lock);
1186 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001187 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001188 if (ret) {
1189 btrfs_error(root->fs_info, ret,
1190 "Failed to remove dev extent item");
1191 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001192out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001193 btrfs_free_path(path);
1194 return ret;
1195}
1196
Yan Zheng2b820322008-11-17 21:11:30 -05001197int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001198 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001199 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001200 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001201{
1202 int ret;
1203 struct btrfs_path *path;
1204 struct btrfs_root *root = device->dev_root;
1205 struct btrfs_dev_extent *extent;
1206 struct extent_buffer *leaf;
1207 struct btrfs_key key;
1208
Chris Masondfe25022008-05-13 13:46:40 -04001209 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001210 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001211 path = btrfs_alloc_path();
1212 if (!path)
1213 return -ENOMEM;
1214
Chris Mason0b86a832008-03-24 15:01:56 -04001215 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001216 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001217 key.type = BTRFS_DEV_EXTENT_KEY;
1218 ret = btrfs_insert_empty_item(trans, root, path, &key,
1219 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001220 if (ret)
1221 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001222
1223 leaf = path->nodes[0];
1224 extent = btrfs_item_ptr(leaf, path->slots[0],
1225 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001226 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1227 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1228 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1229
1230 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1231 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1232 BTRFS_UUID_SIZE);
1233
Chris Mason0b86a832008-03-24 15:01:56 -04001234 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1235 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001236out:
Chris Mason0b86a832008-03-24 15:01:56 -04001237 btrfs_free_path(path);
1238 return ret;
1239}
1240
Chris Masona1b32a52008-09-05 16:09:51 -04001241static noinline int find_next_chunk(struct btrfs_root *root,
1242 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001243{
1244 struct btrfs_path *path;
1245 int ret;
1246 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001247 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001248 struct btrfs_key found_key;
1249
1250 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001251 if (!path)
1252 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001253
Chris Masone17cade2008-04-15 15:41:47 -04001254 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001255 key.offset = (u64)-1;
1256 key.type = BTRFS_CHUNK_ITEM_KEY;
1257
1258 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1259 if (ret < 0)
1260 goto error;
1261
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001262 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001263
1264 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1265 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001266 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001267 } else {
1268 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1269 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001270 if (found_key.objectid != objectid)
1271 *offset = 0;
1272 else {
1273 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1274 struct btrfs_chunk);
1275 *offset = found_key.offset +
1276 btrfs_chunk_length(path->nodes[0], chunk);
1277 }
Chris Mason0b86a832008-03-24 15:01:56 -04001278 }
1279 ret = 0;
1280error:
1281 btrfs_free_path(path);
1282 return ret;
1283}
1284
Yan Zheng2b820322008-11-17 21:11:30 -05001285static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001286{
1287 int ret;
1288 struct btrfs_key key;
1289 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001290 struct btrfs_path *path;
1291
1292 root = root->fs_info->chunk_root;
1293
1294 path = btrfs_alloc_path();
1295 if (!path)
1296 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001297
1298 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1299 key.type = BTRFS_DEV_ITEM_KEY;
1300 key.offset = (u64)-1;
1301
1302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1303 if (ret < 0)
1304 goto error;
1305
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001306 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001307
1308 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1309 BTRFS_DEV_ITEM_KEY);
1310 if (ret) {
1311 *objectid = 1;
1312 } else {
1313 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1314 path->slots[0]);
1315 *objectid = found_key.offset + 1;
1316 }
1317 ret = 0;
1318error:
Yan Zheng2b820322008-11-17 21:11:30 -05001319 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001320 return ret;
1321}
1322
1323/*
1324 * the device information is stored in the chunk root
1325 * the btrfs_device struct should be fully filled in
1326 */
1327int btrfs_add_device(struct btrfs_trans_handle *trans,
1328 struct btrfs_root *root,
1329 struct btrfs_device *device)
1330{
1331 int ret;
1332 struct btrfs_path *path;
1333 struct btrfs_dev_item *dev_item;
1334 struct extent_buffer *leaf;
1335 struct btrfs_key key;
1336 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001337
1338 root = root->fs_info->chunk_root;
1339
1340 path = btrfs_alloc_path();
1341 if (!path)
1342 return -ENOMEM;
1343
Chris Mason0b86a832008-03-24 15:01:56 -04001344 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1345 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001346 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001347
1348 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001349 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001350 if (ret)
1351 goto out;
1352
1353 leaf = path->nodes[0];
1354 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1355
1356 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001357 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001358 btrfs_set_device_type(leaf, dev_item, device->type);
1359 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1360 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1361 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001362 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1363 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001364 btrfs_set_device_group(leaf, dev_item, 0);
1365 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1366 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001367 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001368
Chris Mason0b86a832008-03-24 15:01:56 -04001369 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001370 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001371 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1372 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001373 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001374
Yan Zheng2b820322008-11-17 21:11:30 -05001375 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001376out:
1377 btrfs_free_path(path);
1378 return ret;
1379}
Chris Mason8f18cf12008-04-25 16:53:30 -04001380
Chris Masona061fc82008-05-07 11:43:44 -04001381static int btrfs_rm_dev_item(struct btrfs_root *root,
1382 struct btrfs_device *device)
1383{
1384 int ret;
1385 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001386 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001387 struct btrfs_trans_handle *trans;
1388
1389 root = root->fs_info->chunk_root;
1390
1391 path = btrfs_alloc_path();
1392 if (!path)
1393 return -ENOMEM;
1394
Yan, Zhenga22285a2010-05-16 10:48:46 -04001395 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001396 if (IS_ERR(trans)) {
1397 btrfs_free_path(path);
1398 return PTR_ERR(trans);
1399 }
Chris Masona061fc82008-05-07 11:43:44 -04001400 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1401 key.type = BTRFS_DEV_ITEM_KEY;
1402 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001403 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001404
1405 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1406 if (ret < 0)
1407 goto out;
1408
1409 if (ret > 0) {
1410 ret = -ENOENT;
1411 goto out;
1412 }
1413
1414 ret = btrfs_del_item(trans, root, path);
1415 if (ret)
1416 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001417out:
1418 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001419 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001420 btrfs_commit_transaction(trans, root);
1421 return ret;
1422}
1423
1424int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1425{
1426 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001427 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001428 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001429 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001430 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001431 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001432 u64 all_avail;
1433 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001434 u64 num_devices;
1435 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001436 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001437 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001438 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001439
Chris Masona061fc82008-05-07 11:43:44 -04001440 mutex_lock(&uuid_mutex);
1441
Miao Xiede98ced2013-01-29 10:13:12 +00001442 do {
1443 seq = read_seqbegin(&root->fs_info->profiles_lock);
1444
1445 all_avail = root->fs_info->avail_data_alloc_bits |
1446 root->fs_info->avail_system_alloc_bits |
1447 root->fs_info->avail_metadata_alloc_bits;
1448 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001449
Stefan Behrens8dabb742012-11-06 13:15:27 +01001450 num_devices = root->fs_info->fs_devices->num_devices;
1451 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1452 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1453 WARN_ON(num_devices < 1);
1454 num_devices--;
1455 }
1456 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1457
1458 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001459 printk(KERN_ERR "btrfs: unable to go below four devices "
1460 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001461 ret = -EINVAL;
1462 goto out;
1463 }
1464
Stefan Behrens8dabb742012-11-06 13:15:27 +01001465 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001466 printk(KERN_ERR "btrfs: unable to go below two "
1467 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001468 ret = -EINVAL;
1469 goto out;
1470 }
1471
David Woodhouse53b381b2013-01-29 18:40:14 -05001472 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1473 root->fs_info->fs_devices->rw_devices <= 2) {
1474 printk(KERN_ERR "btrfs: unable to go below two "
1475 "devices on raid5\n");
1476 ret = -EINVAL;
1477 goto out;
1478 }
1479 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1480 root->fs_info->fs_devices->rw_devices <= 3) {
1481 printk(KERN_ERR "btrfs: unable to go below three "
1482 "devices on raid6\n");
1483 ret = -EINVAL;
1484 goto out;
1485 }
1486
Chris Masondfe25022008-05-13 13:46:40 -04001487 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001488 struct list_head *devices;
1489 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001490
Chris Masondfe25022008-05-13 13:46:40 -04001491 device = NULL;
1492 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001493 /*
1494 * It is safe to read the devices since the volume_mutex
1495 * is held.
1496 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001497 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001498 if (tmp->in_fs_metadata &&
1499 !tmp->is_tgtdev_for_dev_replace &&
1500 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001501 device = tmp;
1502 break;
1503 }
1504 }
1505 bdev = NULL;
1506 bh = NULL;
1507 disk_super = NULL;
1508 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001509 printk(KERN_ERR "btrfs: no missing devices found to "
1510 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001511 goto out;
1512 }
Chris Masondfe25022008-05-13 13:46:40 -04001513 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001514 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001515 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001516 root->fs_info->bdev_holder, 0,
1517 &bdev, &bh);
1518 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001519 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001520 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001521 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001522 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001523 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001524 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001525 if (!device) {
1526 ret = -ENOENT;
1527 goto error_brelse;
1528 }
Chris Masondfe25022008-05-13 13:46:40 -04001529 }
Yan Zheng2b820322008-11-17 21:11:30 -05001530
Stefan Behrens63a212a2012-11-05 18:29:28 +01001531 if (device->is_tgtdev_for_dev_replace) {
1532 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1533 ret = -EINVAL;
1534 goto error_brelse;
1535 }
1536
Yan Zheng2b820322008-11-17 21:11:30 -05001537 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001538 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1539 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001540 ret = -EINVAL;
1541 goto error_brelse;
1542 }
1543
1544 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001545 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001546 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001547 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001548 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001549 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001550 }
Chris Masona061fc82008-05-07 11:43:44 -04001551
1552 ret = btrfs_shrink_device(device, 0);
1553 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001554 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001555
Stefan Behrens63a212a2012-11-05 18:29:28 +01001556 /*
1557 * TODO: the superblock still includes this device in its num_devices
1558 * counter although write_all_supers() is not locked out. This
1559 * could give a filesystem state which requires a degraded mount.
1560 */
Chris Masona061fc82008-05-07 11:43:44 -04001561 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1562 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001563 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001564
Josef Bacik2bf64752011-09-26 17:12:22 -04001565 spin_lock(&root->fs_info->free_chunk_lock);
1566 root->fs_info->free_chunk_space = device->total_bytes -
1567 device->bytes_used;
1568 spin_unlock(&root->fs_info->free_chunk_lock);
1569
Yan Zheng2b820322008-11-17 21:11:30 -05001570 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001571 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001572
1573 /*
1574 * the device list mutex makes sure that we don't change
1575 * the device list while someone else is writing out all
1576 * the device supers.
1577 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001578
1579 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001580 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001581 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001582
Yan Zhenge4404d62008-12-12 10:03:26 -05001583 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001584 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001585
Chris Masoncd02dca2010-12-13 14:56:23 -05001586 if (device->missing)
1587 root->fs_info->fs_devices->missing_devices--;
1588
Yan Zheng2b820322008-11-17 21:11:30 -05001589 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1590 struct btrfs_device, dev_list);
1591 if (device->bdev == root->fs_info->sb->s_bdev)
1592 root->fs_info->sb->s_bdev = next_device->bdev;
1593 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1594 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1595
Xiao Guangrong1f781602011-04-20 10:09:16 +00001596 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001597 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001598
1599 call_rcu(&device->rcu, free_device);
1600 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001601
David Sterba6c417612011-04-13 15:41:04 +02001602 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1603 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001604
Xiao Guangrong1f781602011-04-20 10:09:16 +00001605 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001606 struct btrfs_fs_devices *fs_devices;
1607 fs_devices = root->fs_info->fs_devices;
1608 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001609 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001610 break;
1611 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001612 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001613 fs_devices->seed = cur_devices->seed;
1614 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001615 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001616 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001617 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001618 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001619 }
1620
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001621 root->fs_info->num_tolerated_disk_barrier_failures =
1622 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1623
Yan Zheng2b820322008-11-17 21:11:30 -05001624 /*
1625 * at this point, the device is zero sized. We want to
1626 * remove it from the devices list and zero out the old super
1627 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001628 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001629 /* make sure this device isn't detected as part of
1630 * the FS anymore
1631 */
1632 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1633 set_buffer_dirty(bh);
1634 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001635 }
Chris Masona061fc82008-05-07 11:43:44 -04001636
Chris Masona061fc82008-05-07 11:43:44 -04001637 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001638
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001639 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001640 if (bdev)
1641 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001642
Chris Masona061fc82008-05-07 11:43:44 -04001643error_brelse:
1644 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001645 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001646 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001647out:
1648 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001649 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001650error_undo:
1651 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001652 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001653 list_add(&device->dev_alloc_list,
1654 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001655 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001656 root->fs_info->fs_devices->rw_devices++;
1657 }
1658 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001659}
1660
Stefan Behrense93c89c2012-11-05 17:33:06 +01001661void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1662 struct btrfs_device *srcdev)
1663{
1664 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1665 list_del_rcu(&srcdev->dev_list);
1666 list_del_rcu(&srcdev->dev_alloc_list);
1667 fs_info->fs_devices->num_devices--;
1668 if (srcdev->missing) {
1669 fs_info->fs_devices->missing_devices--;
1670 fs_info->fs_devices->rw_devices++;
1671 }
1672 if (srcdev->can_discard)
1673 fs_info->fs_devices->num_can_discard--;
1674 if (srcdev->bdev)
1675 fs_info->fs_devices->open_devices--;
1676
1677 call_rcu(&srcdev->rcu, free_device);
1678}
1679
1680void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1681 struct btrfs_device *tgtdev)
1682{
1683 struct btrfs_device *next_device;
1684
1685 WARN_ON(!tgtdev);
1686 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1687 if (tgtdev->bdev) {
1688 btrfs_scratch_superblock(tgtdev);
1689 fs_info->fs_devices->open_devices--;
1690 }
1691 fs_info->fs_devices->num_devices--;
1692 if (tgtdev->can_discard)
1693 fs_info->fs_devices->num_can_discard++;
1694
1695 next_device = list_entry(fs_info->fs_devices->devices.next,
1696 struct btrfs_device, dev_list);
1697 if (tgtdev->bdev == fs_info->sb->s_bdev)
1698 fs_info->sb->s_bdev = next_device->bdev;
1699 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1700 fs_info->fs_devices->latest_bdev = next_device->bdev;
1701 list_del_rcu(&tgtdev->dev_list);
1702
1703 call_rcu(&tgtdev->rcu, free_device);
1704
1705 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1706}
1707
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001708int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1709 struct btrfs_device **device)
1710{
1711 int ret = 0;
1712 struct btrfs_super_block *disk_super;
1713 u64 devid;
1714 u8 *dev_uuid;
1715 struct block_device *bdev;
1716 struct buffer_head *bh;
1717
1718 *device = NULL;
1719 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1720 root->fs_info->bdev_holder, 0, &bdev, &bh);
1721 if (ret)
1722 return ret;
1723 disk_super = (struct btrfs_super_block *)bh->b_data;
1724 devid = btrfs_stack_device_id(&disk_super->dev_item);
1725 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001726 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001727 disk_super->fsid);
1728 brelse(bh);
1729 if (!*device)
1730 ret = -ENOENT;
1731 blkdev_put(bdev, FMODE_READ);
1732 return ret;
1733}
1734
1735int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1736 char *device_path,
1737 struct btrfs_device **device)
1738{
1739 *device = NULL;
1740 if (strcmp(device_path, "missing") == 0) {
1741 struct list_head *devices;
1742 struct btrfs_device *tmp;
1743
1744 devices = &root->fs_info->fs_devices->devices;
1745 /*
1746 * It is safe to read the devices since the volume_mutex
1747 * is held by the caller.
1748 */
1749 list_for_each_entry(tmp, devices, dev_list) {
1750 if (tmp->in_fs_metadata && !tmp->bdev) {
1751 *device = tmp;
1752 break;
1753 }
1754 }
1755
1756 if (!*device) {
1757 pr_err("btrfs: no missing device found\n");
1758 return -ENOENT;
1759 }
1760
1761 return 0;
1762 } else {
1763 return btrfs_find_device_by_path(root, device_path, device);
1764 }
1765}
1766
Yan Zheng2b820322008-11-17 21:11:30 -05001767/*
1768 * does all the dirty work required for changing file system's UUID.
1769 */
Li Zefan125ccb02011-12-08 15:07:24 +08001770static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001771{
1772 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1773 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001774 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001775 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001776 struct btrfs_device *device;
1777 u64 super_flags;
1778
1779 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001780 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001781 return -EINVAL;
1782
Yan Zhenge4404d62008-12-12 10:03:26 -05001783 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1784 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001785 return -ENOMEM;
1786
Yan Zhenge4404d62008-12-12 10:03:26 -05001787 old_devices = clone_fs_devices(fs_devices);
1788 if (IS_ERR(old_devices)) {
1789 kfree(seed_devices);
1790 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001791 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001792
Yan Zheng2b820322008-11-17 21:11:30 -05001793 list_add(&old_devices->list, &fs_uuids);
1794
Yan Zhenge4404d62008-12-12 10:03:26 -05001795 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1796 seed_devices->opened = 1;
1797 INIT_LIST_HEAD(&seed_devices->devices);
1798 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001799 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001800
1801 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001802 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1803 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001804 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1805
Yan Zhenge4404d62008-12-12 10:03:26 -05001806 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1807 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1808 device->fs_devices = seed_devices;
1809 }
1810
Yan Zheng2b820322008-11-17 21:11:30 -05001811 fs_devices->seeding = 0;
1812 fs_devices->num_devices = 0;
1813 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001814 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001815 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001816
1817 generate_random_uuid(fs_devices->fsid);
1818 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1819 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1820 super_flags = btrfs_super_flags(disk_super) &
1821 ~BTRFS_SUPER_FLAG_SEEDING;
1822 btrfs_set_super_flags(disk_super, super_flags);
1823
1824 return 0;
1825}
1826
1827/*
1828 * strore the expected generation for seed devices in device items.
1829 */
1830static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1831 struct btrfs_root *root)
1832{
1833 struct btrfs_path *path;
1834 struct extent_buffer *leaf;
1835 struct btrfs_dev_item *dev_item;
1836 struct btrfs_device *device;
1837 struct btrfs_key key;
1838 u8 fs_uuid[BTRFS_UUID_SIZE];
1839 u8 dev_uuid[BTRFS_UUID_SIZE];
1840 u64 devid;
1841 int ret;
1842
1843 path = btrfs_alloc_path();
1844 if (!path)
1845 return -ENOMEM;
1846
1847 root = root->fs_info->chunk_root;
1848 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1849 key.offset = 0;
1850 key.type = BTRFS_DEV_ITEM_KEY;
1851
1852 while (1) {
1853 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1854 if (ret < 0)
1855 goto error;
1856
1857 leaf = path->nodes[0];
1858next_slot:
1859 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1860 ret = btrfs_next_leaf(root, path);
1861 if (ret > 0)
1862 break;
1863 if (ret < 0)
1864 goto error;
1865 leaf = path->nodes[0];
1866 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001867 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001868 continue;
1869 }
1870
1871 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1872 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1873 key.type != BTRFS_DEV_ITEM_KEY)
1874 break;
1875
1876 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1877 struct btrfs_dev_item);
1878 devid = btrfs_device_id(leaf, dev_item);
1879 read_extent_buffer(leaf, dev_uuid,
1880 (unsigned long)btrfs_device_uuid(dev_item),
1881 BTRFS_UUID_SIZE);
1882 read_extent_buffer(leaf, fs_uuid,
1883 (unsigned long)btrfs_device_fsid(dev_item),
1884 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001885 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1886 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001887 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001888
1889 if (device->fs_devices->seeding) {
1890 btrfs_set_device_generation(leaf, dev_item,
1891 device->generation);
1892 btrfs_mark_buffer_dirty(leaf);
1893 }
1894
1895 path->slots[0]++;
1896 goto next_slot;
1897 }
1898 ret = 0;
1899error:
1900 btrfs_free_path(path);
1901 return ret;
1902}
1903
Chris Mason788f20e2008-04-28 15:29:42 -04001904int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1905{
Josef Bacikd5e20032011-08-04 14:52:27 +00001906 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001907 struct btrfs_trans_handle *trans;
1908 struct btrfs_device *device;
1909 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001910 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001911 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001912 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001913 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001914 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001915 int ret = 0;
1916
Yan Zheng2b820322008-11-17 21:11:30 -05001917 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001918 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001919
Li Zefana5d16332011-12-07 20:08:40 -05001920 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001921 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001922 if (IS_ERR(bdev))
1923 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001924
Yan Zheng2b820322008-11-17 21:11:30 -05001925 if (root->fs_info->fs_devices->seeding) {
1926 seeding_dev = 1;
1927 down_write(&sb->s_umount);
1928 mutex_lock(&uuid_mutex);
1929 }
1930
Chris Mason8c8bee12008-09-29 11:19:10 -04001931 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001932
Chris Mason788f20e2008-04-28 15:29:42 -04001933 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001934
1935 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001936 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001937 if (device->bdev == bdev) {
1938 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001939 mutex_unlock(
1940 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001941 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001942 }
1943 }
Liu Bod25628b2012-11-14 14:35:30 +00001944 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001945
1946 device = kzalloc(sizeof(*device), GFP_NOFS);
1947 if (!device) {
1948 /* we can safely leave the fs_devices entry around */
1949 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001950 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001951 }
1952
Josef Bacik606686e2012-06-04 14:03:51 -04001953 name = rcu_string_strdup(device_path, GFP_NOFS);
1954 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001955 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001956 ret = -ENOMEM;
1957 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001958 }
Josef Bacik606686e2012-06-04 14:03:51 -04001959 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001960
1961 ret = find_next_devid(root, &device->devid);
1962 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001963 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001964 kfree(device);
1965 goto error;
1966 }
1967
Yan, Zhenga22285a2010-05-16 10:48:46 -04001968 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001969 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001970 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001971 kfree(device);
1972 ret = PTR_ERR(trans);
1973 goto error;
1974 }
1975
Yan Zheng2b820322008-11-17 21:11:30 -05001976 lock_chunks(root);
1977
Josef Bacikd5e20032011-08-04 14:52:27 +00001978 q = bdev_get_queue(bdev);
1979 if (blk_queue_discard(q))
1980 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001981 device->writeable = 1;
1982 device->work.func = pending_bios_fn;
1983 generate_random_uuid(device->uuid);
1984 spin_lock_init(&device->io_lock);
1985 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001986 device->io_width = root->sectorsize;
1987 device->io_align = root->sectorsize;
1988 device->sector_size = root->sectorsize;
1989 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001990 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001991 device->dev_root = root->fs_info->dev_root;
1992 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001993 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001994 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001995 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001996 set_blocksize(device->bdev, 4096);
1997
Yan Zheng2b820322008-11-17 21:11:30 -05001998 if (seeding_dev) {
1999 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002000 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002001 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002002 }
2003
2004 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002005
Chris Masone5e9a522009-06-10 15:17:02 -04002006 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002007 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002008 list_add(&device->dev_alloc_list,
2009 &root->fs_info->fs_devices->alloc_list);
2010 root->fs_info->fs_devices->num_devices++;
2011 root->fs_info->fs_devices->open_devices++;
2012 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002013 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002014 if (device->can_discard)
2015 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002016 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2017
Josef Bacik2bf64752011-09-26 17:12:22 -04002018 spin_lock(&root->fs_info->free_chunk_lock);
2019 root->fs_info->free_chunk_space += device->total_bytes;
2020 spin_unlock(&root->fs_info->free_chunk_lock);
2021
Chris Masonc2898112009-06-10 09:51:32 -04002022 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2023 root->fs_info->fs_devices->rotating = 1;
2024
David Sterba6c417612011-04-13 15:41:04 +02002025 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2026 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002027 total_bytes + device->total_bytes);
2028
David Sterba6c417612011-04-13 15:41:04 +02002029 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2030 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002031 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04002032 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002033
Yan Zheng2b820322008-11-17 21:11:30 -05002034 if (seeding_dev) {
2035 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002036 if (ret) {
2037 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002038 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002039 }
Yan Zheng2b820322008-11-17 21:11:30 -05002040 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002041 if (ret) {
2042 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002043 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002044 }
Yan Zheng2b820322008-11-17 21:11:30 -05002045 } else {
2046 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002047 if (ret) {
2048 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002049 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002050 }
Yan Zheng2b820322008-11-17 21:11:30 -05002051 }
2052
Chris Mason913d9522009-03-10 13:17:18 -04002053 /*
2054 * we've got more storage, clear any full flags on the space
2055 * infos
2056 */
2057 btrfs_clear_space_info_full(root->fs_info);
2058
Chris Mason7d9eb122008-07-08 14:19:17 -04002059 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002060 root->fs_info->num_tolerated_disk_barrier_failures =
2061 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002062 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002063
2064 if (seeding_dev) {
2065 mutex_unlock(&uuid_mutex);
2066 up_write(&sb->s_umount);
2067
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002068 if (ret) /* transaction commit */
2069 return ret;
2070
Yan Zheng2b820322008-11-17 21:11:30 -05002071 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002072 if (ret < 0)
2073 btrfs_error(root->fs_info, ret,
2074 "Failed to relocate sys chunks after "
2075 "device initialization. This can be fixed "
2076 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002077 trans = btrfs_attach_transaction(root);
2078 if (IS_ERR(trans)) {
2079 if (PTR_ERR(trans) == -ENOENT)
2080 return 0;
2081 return PTR_ERR(trans);
2082 }
2083 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002084 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002085
Chris Mason788f20e2008-04-28 15:29:42 -04002086 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002087
2088error_trans:
2089 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002090 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002091 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002092 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002093error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002094 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002095 if (seeding_dev) {
2096 mutex_unlock(&uuid_mutex);
2097 up_write(&sb->s_umount);
2098 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002099 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002100}
2101
Stefan Behrense93c89c2012-11-05 17:33:06 +01002102int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2103 struct btrfs_device **device_out)
2104{
2105 struct request_queue *q;
2106 struct btrfs_device *device;
2107 struct block_device *bdev;
2108 struct btrfs_fs_info *fs_info = root->fs_info;
2109 struct list_head *devices;
2110 struct rcu_string *name;
2111 int ret = 0;
2112
2113 *device_out = NULL;
2114 if (fs_info->fs_devices->seeding)
2115 return -EINVAL;
2116
2117 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2118 fs_info->bdev_holder);
2119 if (IS_ERR(bdev))
2120 return PTR_ERR(bdev);
2121
2122 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2123
2124 devices = &fs_info->fs_devices->devices;
2125 list_for_each_entry(device, devices, dev_list) {
2126 if (device->bdev == bdev) {
2127 ret = -EEXIST;
2128 goto error;
2129 }
2130 }
2131
2132 device = kzalloc(sizeof(*device), GFP_NOFS);
2133 if (!device) {
2134 ret = -ENOMEM;
2135 goto error;
2136 }
2137
2138 name = rcu_string_strdup(device_path, GFP_NOFS);
2139 if (!name) {
2140 kfree(device);
2141 ret = -ENOMEM;
2142 goto error;
2143 }
2144 rcu_assign_pointer(device->name, name);
2145
2146 q = bdev_get_queue(bdev);
2147 if (blk_queue_discard(q))
2148 device->can_discard = 1;
2149 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2150 device->writeable = 1;
2151 device->work.func = pending_bios_fn;
2152 generate_random_uuid(device->uuid);
2153 device->devid = BTRFS_DEV_REPLACE_DEVID;
2154 spin_lock_init(&device->io_lock);
2155 device->generation = 0;
2156 device->io_width = root->sectorsize;
2157 device->io_align = root->sectorsize;
2158 device->sector_size = root->sectorsize;
2159 device->total_bytes = i_size_read(bdev->bd_inode);
2160 device->disk_total_bytes = device->total_bytes;
2161 device->dev_root = fs_info->dev_root;
2162 device->bdev = bdev;
2163 device->in_fs_metadata = 1;
2164 device->is_tgtdev_for_dev_replace = 1;
2165 device->mode = FMODE_EXCL;
2166 set_blocksize(device->bdev, 4096);
2167 device->fs_devices = fs_info->fs_devices;
2168 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2169 fs_info->fs_devices->num_devices++;
2170 fs_info->fs_devices->open_devices++;
2171 if (device->can_discard)
2172 fs_info->fs_devices->num_can_discard++;
2173 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2174
2175 *device_out = device;
2176 return ret;
2177
2178error:
2179 blkdev_put(bdev, FMODE_EXCL);
2180 return ret;
2181}
2182
2183void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2184 struct btrfs_device *tgtdev)
2185{
2186 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2187 tgtdev->io_width = fs_info->dev_root->sectorsize;
2188 tgtdev->io_align = fs_info->dev_root->sectorsize;
2189 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2190 tgtdev->dev_root = fs_info->dev_root;
2191 tgtdev->in_fs_metadata = 1;
2192}
2193
Chris Masond3977122009-01-05 21:25:51 -05002194static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2195 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002196{
2197 int ret;
2198 struct btrfs_path *path;
2199 struct btrfs_root *root;
2200 struct btrfs_dev_item *dev_item;
2201 struct extent_buffer *leaf;
2202 struct btrfs_key key;
2203
2204 root = device->dev_root->fs_info->chunk_root;
2205
2206 path = btrfs_alloc_path();
2207 if (!path)
2208 return -ENOMEM;
2209
2210 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2211 key.type = BTRFS_DEV_ITEM_KEY;
2212 key.offset = device->devid;
2213
2214 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2215 if (ret < 0)
2216 goto out;
2217
2218 if (ret > 0) {
2219 ret = -ENOENT;
2220 goto out;
2221 }
2222
2223 leaf = path->nodes[0];
2224 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2225
2226 btrfs_set_device_id(leaf, dev_item, device->devid);
2227 btrfs_set_device_type(leaf, dev_item, device->type);
2228 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2229 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2230 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002231 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002232 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2233 btrfs_mark_buffer_dirty(leaf);
2234
2235out:
2236 btrfs_free_path(path);
2237 return ret;
2238}
2239
Chris Mason7d9eb122008-07-08 14:19:17 -04002240static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002241 struct btrfs_device *device, u64 new_size)
2242{
2243 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002244 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002245 u64 old_total = btrfs_super_total_bytes(super_copy);
2246 u64 diff = new_size - device->total_bytes;
2247
Yan Zheng2b820322008-11-17 21:11:30 -05002248 if (!device->writeable)
2249 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002250 if (new_size <= device->total_bytes ||
2251 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002252 return -EINVAL;
2253
Chris Mason8f18cf12008-04-25 16:53:30 -04002254 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002255 device->fs_devices->total_rw_bytes += diff;
2256
2257 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002258 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002259 btrfs_clear_space_info_full(device->dev_root->fs_info);
2260
Chris Mason8f18cf12008-04-25 16:53:30 -04002261 return btrfs_update_device(trans, device);
2262}
2263
Chris Mason7d9eb122008-07-08 14:19:17 -04002264int btrfs_grow_device(struct btrfs_trans_handle *trans,
2265 struct btrfs_device *device, u64 new_size)
2266{
2267 int ret;
2268 lock_chunks(device->dev_root);
2269 ret = __btrfs_grow_device(trans, device, new_size);
2270 unlock_chunks(device->dev_root);
2271 return ret;
2272}
2273
Chris Mason8f18cf12008-04-25 16:53:30 -04002274static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2275 struct btrfs_root *root,
2276 u64 chunk_tree, u64 chunk_objectid,
2277 u64 chunk_offset)
2278{
2279 int ret;
2280 struct btrfs_path *path;
2281 struct btrfs_key key;
2282
2283 root = root->fs_info->chunk_root;
2284 path = btrfs_alloc_path();
2285 if (!path)
2286 return -ENOMEM;
2287
2288 key.objectid = chunk_objectid;
2289 key.offset = chunk_offset;
2290 key.type = BTRFS_CHUNK_ITEM_KEY;
2291
2292 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002293 if (ret < 0)
2294 goto out;
2295 else if (ret > 0) { /* Logic error or corruption */
2296 btrfs_error(root->fs_info, -ENOENT,
2297 "Failed lookup while freeing chunk.");
2298 ret = -ENOENT;
2299 goto out;
2300 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002301
2302 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002303 if (ret < 0)
2304 btrfs_error(root->fs_info, ret,
2305 "Failed to delete chunk item.");
2306out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002307 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002308 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002309}
2310
Christoph Hellwigb2950862008-12-02 09:54:17 -05002311static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002312 chunk_offset)
2313{
David Sterba6c417612011-04-13 15:41:04 +02002314 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002315 struct btrfs_disk_key *disk_key;
2316 struct btrfs_chunk *chunk;
2317 u8 *ptr;
2318 int ret = 0;
2319 u32 num_stripes;
2320 u32 array_size;
2321 u32 len = 0;
2322 u32 cur;
2323 struct btrfs_key key;
2324
2325 array_size = btrfs_super_sys_array_size(super_copy);
2326
2327 ptr = super_copy->sys_chunk_array;
2328 cur = 0;
2329
2330 while (cur < array_size) {
2331 disk_key = (struct btrfs_disk_key *)ptr;
2332 btrfs_disk_key_to_cpu(&key, disk_key);
2333
2334 len = sizeof(*disk_key);
2335
2336 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2337 chunk = (struct btrfs_chunk *)(ptr + len);
2338 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2339 len += btrfs_chunk_item_size(num_stripes);
2340 } else {
2341 ret = -EIO;
2342 break;
2343 }
2344 if (key.objectid == chunk_objectid &&
2345 key.offset == chunk_offset) {
2346 memmove(ptr, ptr + len, array_size - (cur + len));
2347 array_size -= len;
2348 btrfs_set_super_sys_array_size(super_copy, array_size);
2349 } else {
2350 ptr += len;
2351 cur += len;
2352 }
2353 }
2354 return ret;
2355}
2356
Christoph Hellwigb2950862008-12-02 09:54:17 -05002357static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002358 u64 chunk_tree, u64 chunk_objectid,
2359 u64 chunk_offset)
2360{
2361 struct extent_map_tree *em_tree;
2362 struct btrfs_root *extent_root;
2363 struct btrfs_trans_handle *trans;
2364 struct extent_map *em;
2365 struct map_lookup *map;
2366 int ret;
2367 int i;
2368
2369 root = root->fs_info->chunk_root;
2370 extent_root = root->fs_info->extent_root;
2371 em_tree = &root->fs_info->mapping_tree.map_tree;
2372
Josef Bacikba1bf482009-09-11 16:11:19 -04002373 ret = btrfs_can_relocate(extent_root, chunk_offset);
2374 if (ret)
2375 return -ENOSPC;
2376
Chris Mason8f18cf12008-04-25 16:53:30 -04002377 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002378 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002379 if (ret)
2380 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002381
Yan, Zhenga22285a2010-05-16 10:48:46 -04002382 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002383 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002384
Chris Mason7d9eb122008-07-08 14:19:17 -04002385 lock_chunks(root);
2386
Chris Mason8f18cf12008-04-25 16:53:30 -04002387 /*
2388 * step two, delete the device extents and the
2389 * chunk tree entries
2390 */
Chris Mason890871b2009-09-02 16:24:52 -04002391 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002392 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002393 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002394
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002395 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002396 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002397 map = (struct map_lookup *)em->bdev;
2398
2399 for (i = 0; i < map->num_stripes; i++) {
2400 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2401 map->stripes[i].physical);
2402 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002403
Chris Masondfe25022008-05-13 13:46:40 -04002404 if (map->stripes[i].dev) {
2405 ret = btrfs_update_device(trans, map->stripes[i].dev);
2406 BUG_ON(ret);
2407 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002408 }
2409 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2410 chunk_offset);
2411
2412 BUG_ON(ret);
2413
liubo1abe9b82011-03-24 11:18:59 +00002414 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2415
Chris Mason8f18cf12008-04-25 16:53:30 -04002416 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2417 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2418 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002419 }
2420
Zheng Yan1a40e232008-09-26 10:09:34 -04002421 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2422 BUG_ON(ret);
2423
Chris Mason890871b2009-09-02 16:24:52 -04002424 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002425 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002426 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002427
Chris Mason8f18cf12008-04-25 16:53:30 -04002428 kfree(map);
2429 em->bdev = NULL;
2430
2431 /* once for the tree */
2432 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002433 /* once for us */
2434 free_extent_map(em);
2435
Chris Mason7d9eb122008-07-08 14:19:17 -04002436 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002437 btrfs_end_transaction(trans, root);
2438 return 0;
2439}
2440
Yan Zheng2b820322008-11-17 21:11:30 -05002441static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2442{
2443 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2444 struct btrfs_path *path;
2445 struct extent_buffer *leaf;
2446 struct btrfs_chunk *chunk;
2447 struct btrfs_key key;
2448 struct btrfs_key found_key;
2449 u64 chunk_tree = chunk_root->root_key.objectid;
2450 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002451 bool retried = false;
2452 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002453 int ret;
2454
2455 path = btrfs_alloc_path();
2456 if (!path)
2457 return -ENOMEM;
2458
Josef Bacikba1bf482009-09-11 16:11:19 -04002459again:
Yan Zheng2b820322008-11-17 21:11:30 -05002460 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2461 key.offset = (u64)-1;
2462 key.type = BTRFS_CHUNK_ITEM_KEY;
2463
2464 while (1) {
2465 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2466 if (ret < 0)
2467 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002468 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002469
2470 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2471 key.type);
2472 if (ret < 0)
2473 goto error;
2474 if (ret > 0)
2475 break;
2476
2477 leaf = path->nodes[0];
2478 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2479
2480 chunk = btrfs_item_ptr(leaf, path->slots[0],
2481 struct btrfs_chunk);
2482 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002483 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002484
2485 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2486 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2487 found_key.objectid,
2488 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002489 if (ret == -ENOSPC)
2490 failed++;
2491 else if (ret)
2492 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002493 }
2494
2495 if (found_key.offset == 0)
2496 break;
2497 key.offset = found_key.offset - 1;
2498 }
2499 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002500 if (failed && !retried) {
2501 failed = 0;
2502 retried = true;
2503 goto again;
2504 } else if (failed && retried) {
2505 WARN_ON(1);
2506 ret = -ENOSPC;
2507 }
Yan Zheng2b820322008-11-17 21:11:30 -05002508error:
2509 btrfs_free_path(path);
2510 return ret;
2511}
2512
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002513static int insert_balance_item(struct btrfs_root *root,
2514 struct btrfs_balance_control *bctl)
2515{
2516 struct btrfs_trans_handle *trans;
2517 struct btrfs_balance_item *item;
2518 struct btrfs_disk_balance_args disk_bargs;
2519 struct btrfs_path *path;
2520 struct extent_buffer *leaf;
2521 struct btrfs_key key;
2522 int ret, err;
2523
2524 path = btrfs_alloc_path();
2525 if (!path)
2526 return -ENOMEM;
2527
2528 trans = btrfs_start_transaction(root, 0);
2529 if (IS_ERR(trans)) {
2530 btrfs_free_path(path);
2531 return PTR_ERR(trans);
2532 }
2533
2534 key.objectid = BTRFS_BALANCE_OBJECTID;
2535 key.type = BTRFS_BALANCE_ITEM_KEY;
2536 key.offset = 0;
2537
2538 ret = btrfs_insert_empty_item(trans, root, path, &key,
2539 sizeof(*item));
2540 if (ret)
2541 goto out;
2542
2543 leaf = path->nodes[0];
2544 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2545
2546 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2547
2548 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2549 btrfs_set_balance_data(leaf, item, &disk_bargs);
2550 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2551 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2552 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2553 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2554
2555 btrfs_set_balance_flags(leaf, item, bctl->flags);
2556
2557 btrfs_mark_buffer_dirty(leaf);
2558out:
2559 btrfs_free_path(path);
2560 err = btrfs_commit_transaction(trans, root);
2561 if (err && !ret)
2562 ret = err;
2563 return ret;
2564}
2565
2566static int del_balance_item(struct btrfs_root *root)
2567{
2568 struct btrfs_trans_handle *trans;
2569 struct btrfs_path *path;
2570 struct btrfs_key key;
2571 int ret, err;
2572
2573 path = btrfs_alloc_path();
2574 if (!path)
2575 return -ENOMEM;
2576
2577 trans = btrfs_start_transaction(root, 0);
2578 if (IS_ERR(trans)) {
2579 btrfs_free_path(path);
2580 return PTR_ERR(trans);
2581 }
2582
2583 key.objectid = BTRFS_BALANCE_OBJECTID;
2584 key.type = BTRFS_BALANCE_ITEM_KEY;
2585 key.offset = 0;
2586
2587 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2588 if (ret < 0)
2589 goto out;
2590 if (ret > 0) {
2591 ret = -ENOENT;
2592 goto out;
2593 }
2594
2595 ret = btrfs_del_item(trans, root, path);
2596out:
2597 btrfs_free_path(path);
2598 err = btrfs_commit_transaction(trans, root);
2599 if (err && !ret)
2600 ret = err;
2601 return ret;
2602}
2603
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002604/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002605 * This is a heuristic used to reduce the number of chunks balanced on
2606 * resume after balance was interrupted.
2607 */
2608static void update_balance_args(struct btrfs_balance_control *bctl)
2609{
2610 /*
2611 * Turn on soft mode for chunk types that were being converted.
2612 */
2613 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2614 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2615 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2616 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2617 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2618 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2619
2620 /*
2621 * Turn on usage filter if is not already used. The idea is
2622 * that chunks that we have already balanced should be
2623 * reasonably full. Don't do it for chunks that are being
2624 * converted - that will keep us from relocating unconverted
2625 * (albeit full) chunks.
2626 */
2627 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2628 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2629 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2630 bctl->data.usage = 90;
2631 }
2632 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2633 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2634 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2635 bctl->sys.usage = 90;
2636 }
2637 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2638 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2639 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2640 bctl->meta.usage = 90;
2641 }
2642}
2643
2644/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002645 * Should be called with both balance and volume mutexes held to
2646 * serialize other volume operations (add_dev/rm_dev/resize) with
2647 * restriper. Same goes for unset_balance_control.
2648 */
2649static void set_balance_control(struct btrfs_balance_control *bctl)
2650{
2651 struct btrfs_fs_info *fs_info = bctl->fs_info;
2652
2653 BUG_ON(fs_info->balance_ctl);
2654
2655 spin_lock(&fs_info->balance_lock);
2656 fs_info->balance_ctl = bctl;
2657 spin_unlock(&fs_info->balance_lock);
2658}
2659
2660static void unset_balance_control(struct btrfs_fs_info *fs_info)
2661{
2662 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2663
2664 BUG_ON(!fs_info->balance_ctl);
2665
2666 spin_lock(&fs_info->balance_lock);
2667 fs_info->balance_ctl = NULL;
2668 spin_unlock(&fs_info->balance_lock);
2669
2670 kfree(bctl);
2671}
2672
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002673/*
2674 * Balance filters. Return 1 if chunk should be filtered out
2675 * (should not be balanced).
2676 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002677static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002678 struct btrfs_balance_args *bargs)
2679{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002680 chunk_type = chunk_to_extended(chunk_type) &
2681 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002682
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002683 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002684 return 0;
2685
2686 return 1;
2687}
2688
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002689static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2690 struct btrfs_balance_args *bargs)
2691{
2692 struct btrfs_block_group_cache *cache;
2693 u64 chunk_used, user_thresh;
2694 int ret = 1;
2695
2696 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2697 chunk_used = btrfs_block_group_used(&cache->item);
2698
Ilya Dryomova105bb82013-01-21 15:15:56 +02002699 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002700 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002701 else if (bargs->usage > 100)
2702 user_thresh = cache->key.offset;
2703 else
2704 user_thresh = div_factor_fine(cache->key.offset,
2705 bargs->usage);
2706
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002707 if (chunk_used < user_thresh)
2708 ret = 0;
2709
2710 btrfs_put_block_group(cache);
2711 return ret;
2712}
2713
Ilya Dryomov409d4042012-01-16 22:04:47 +02002714static int chunk_devid_filter(struct extent_buffer *leaf,
2715 struct btrfs_chunk *chunk,
2716 struct btrfs_balance_args *bargs)
2717{
2718 struct btrfs_stripe *stripe;
2719 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2720 int i;
2721
2722 for (i = 0; i < num_stripes; i++) {
2723 stripe = btrfs_stripe_nr(chunk, i);
2724 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2725 return 0;
2726 }
2727
2728 return 1;
2729}
2730
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002731/* [pstart, pend) */
2732static int chunk_drange_filter(struct extent_buffer *leaf,
2733 struct btrfs_chunk *chunk,
2734 u64 chunk_offset,
2735 struct btrfs_balance_args *bargs)
2736{
2737 struct btrfs_stripe *stripe;
2738 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2739 u64 stripe_offset;
2740 u64 stripe_length;
2741 int factor;
2742 int i;
2743
2744 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2745 return 0;
2746
2747 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002748 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2749 factor = num_stripes / 2;
2750 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2751 factor = num_stripes - 1;
2752 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2753 factor = num_stripes - 2;
2754 } else {
2755 factor = num_stripes;
2756 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002757
2758 for (i = 0; i < num_stripes; i++) {
2759 stripe = btrfs_stripe_nr(chunk, i);
2760 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2761 continue;
2762
2763 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2764 stripe_length = btrfs_chunk_length(leaf, chunk);
2765 do_div(stripe_length, factor);
2766
2767 if (stripe_offset < bargs->pend &&
2768 stripe_offset + stripe_length > bargs->pstart)
2769 return 0;
2770 }
2771
2772 return 1;
2773}
2774
Ilya Dryomovea671762012-01-16 22:04:48 +02002775/* [vstart, vend) */
2776static int chunk_vrange_filter(struct extent_buffer *leaf,
2777 struct btrfs_chunk *chunk,
2778 u64 chunk_offset,
2779 struct btrfs_balance_args *bargs)
2780{
2781 if (chunk_offset < bargs->vend &&
2782 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2783 /* at least part of the chunk is inside this vrange */
2784 return 0;
2785
2786 return 1;
2787}
2788
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002789static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002790 struct btrfs_balance_args *bargs)
2791{
2792 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2793 return 0;
2794
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002795 chunk_type = chunk_to_extended(chunk_type) &
2796 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002797
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002798 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002799 return 1;
2800
2801 return 0;
2802}
2803
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002804static int should_balance_chunk(struct btrfs_root *root,
2805 struct extent_buffer *leaf,
2806 struct btrfs_chunk *chunk, u64 chunk_offset)
2807{
2808 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2809 struct btrfs_balance_args *bargs = NULL;
2810 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2811
2812 /* type filter */
2813 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2814 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2815 return 0;
2816 }
2817
2818 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2819 bargs = &bctl->data;
2820 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2821 bargs = &bctl->sys;
2822 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2823 bargs = &bctl->meta;
2824
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002825 /* profiles filter */
2826 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2827 chunk_profiles_filter(chunk_type, bargs)) {
2828 return 0;
2829 }
2830
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002831 /* usage filter */
2832 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2833 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2834 return 0;
2835 }
2836
Ilya Dryomov409d4042012-01-16 22:04:47 +02002837 /* devid filter */
2838 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2839 chunk_devid_filter(leaf, chunk, bargs)) {
2840 return 0;
2841 }
2842
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002843 /* drange filter, makes sense only with devid filter */
2844 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2845 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2846 return 0;
2847 }
2848
Ilya Dryomovea671762012-01-16 22:04:48 +02002849 /* vrange filter */
2850 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2851 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2852 return 0;
2853 }
2854
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002855 /* soft profile changing mode */
2856 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2857 chunk_soft_convert_filter(chunk_type, bargs)) {
2858 return 0;
2859 }
2860
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002861 return 1;
2862}
2863
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002864static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002865{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002866 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002867 struct btrfs_root *chunk_root = fs_info->chunk_root;
2868 struct btrfs_root *dev_root = fs_info->dev_root;
2869 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002870 struct btrfs_device *device;
2871 u64 old_size;
2872 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002873 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002874 struct btrfs_path *path;
2875 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002876 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002877 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002878 struct extent_buffer *leaf;
2879 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002880 int ret;
2881 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002882 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002883
Chris Masonec44a352008-04-28 15:29:52 -04002884 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002885 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002886 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002887 old_size = device->total_bytes;
2888 size_to_free = div_factor(old_size, 1);
2889 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002890 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002891 device->total_bytes - device->bytes_used > size_to_free ||
2892 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002893 continue;
2894
2895 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002896 if (ret == -ENOSPC)
2897 break;
Chris Masonec44a352008-04-28 15:29:52 -04002898 BUG_ON(ret);
2899
Yan, Zhenga22285a2010-05-16 10:48:46 -04002900 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002901 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002902
2903 ret = btrfs_grow_device(trans, device, old_size);
2904 BUG_ON(ret);
2905
2906 btrfs_end_transaction(trans, dev_root);
2907 }
2908
2909 /* step two, relocate all the chunks */
2910 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002911 if (!path) {
2912 ret = -ENOMEM;
2913 goto error;
2914 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002915
2916 /* zero out stat counters */
2917 spin_lock(&fs_info->balance_lock);
2918 memset(&bctl->stat, 0, sizeof(bctl->stat));
2919 spin_unlock(&fs_info->balance_lock);
2920again:
Chris Masonec44a352008-04-28 15:29:52 -04002921 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2922 key.offset = (u64)-1;
2923 key.type = BTRFS_CHUNK_ITEM_KEY;
2924
Chris Masond3977122009-01-05 21:25:51 -05002925 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002926 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002927 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002928 ret = -ECANCELED;
2929 goto error;
2930 }
2931
Chris Masonec44a352008-04-28 15:29:52 -04002932 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2933 if (ret < 0)
2934 goto error;
2935
2936 /*
2937 * this shouldn't happen, it means the last relocate
2938 * failed
2939 */
2940 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002941 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002942
2943 ret = btrfs_previous_item(chunk_root, path, 0,
2944 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002945 if (ret) {
2946 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002947 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002948 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002949
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002950 leaf = path->nodes[0];
2951 slot = path->slots[0];
2952 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2953
Chris Masonec44a352008-04-28 15:29:52 -04002954 if (found_key.objectid != key.objectid)
2955 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002956
Chris Masonec44a352008-04-28 15:29:52 -04002957 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002958 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002959 break;
2960
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002961 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2962
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002963 if (!counting) {
2964 spin_lock(&fs_info->balance_lock);
2965 bctl->stat.considered++;
2966 spin_unlock(&fs_info->balance_lock);
2967 }
2968
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002969 ret = should_balance_chunk(chunk_root, leaf, chunk,
2970 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002971 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002972 if (!ret)
2973 goto loop;
2974
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002975 if (counting) {
2976 spin_lock(&fs_info->balance_lock);
2977 bctl->stat.expected++;
2978 spin_unlock(&fs_info->balance_lock);
2979 goto loop;
2980 }
2981
Chris Masonec44a352008-04-28 15:29:52 -04002982 ret = btrfs_relocate_chunk(chunk_root,
2983 chunk_root->root_key.objectid,
2984 found_key.objectid,
2985 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002986 if (ret && ret != -ENOSPC)
2987 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002988 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002989 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002990 } else {
2991 spin_lock(&fs_info->balance_lock);
2992 bctl->stat.completed++;
2993 spin_unlock(&fs_info->balance_lock);
2994 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002995loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002996 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002997 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002998
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002999 if (counting) {
3000 btrfs_release_path(path);
3001 counting = false;
3002 goto again;
3003 }
Chris Masonec44a352008-04-28 15:29:52 -04003004error:
3005 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003006 if (enospc_errors) {
3007 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
3008 enospc_errors);
3009 if (!ret)
3010 ret = -ENOSPC;
3011 }
3012
Chris Masonec44a352008-04-28 15:29:52 -04003013 return ret;
3014}
3015
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003016/**
3017 * alloc_profile_is_valid - see if a given profile is valid and reduced
3018 * @flags: profile to validate
3019 * @extended: if true @flags is treated as an extended profile
3020 */
3021static int alloc_profile_is_valid(u64 flags, int extended)
3022{
3023 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3024 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3025
3026 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3027
3028 /* 1) check that all other bits are zeroed */
3029 if (flags & ~mask)
3030 return 0;
3031
3032 /* 2) see if profile is reduced */
3033 if (flags == 0)
3034 return !extended; /* "0" is valid for usual profiles */
3035
3036 /* true if exactly one bit set */
3037 return (flags & (flags - 1)) == 0;
3038}
3039
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003040static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3041{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003042 /* cancel requested || normal exit path */
3043 return atomic_read(&fs_info->balance_cancel_req) ||
3044 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3045 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003046}
3047
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003048static void __cancel_balance(struct btrfs_fs_info *fs_info)
3049{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003050 int ret;
3051
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003052 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003053 ret = del_balance_item(fs_info->tree_root);
3054 BUG_ON(ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003055
3056 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003057}
3058
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003059void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003060 struct btrfs_ioctl_balance_args *bargs);
3061
3062/*
3063 * Should be called with both balance and volume mutexes held
3064 */
3065int btrfs_balance(struct btrfs_balance_control *bctl,
3066 struct btrfs_ioctl_balance_args *bargs)
3067{
3068 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003069 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003070 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003071 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003072 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003073 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003074
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003075 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003076 atomic_read(&fs_info->balance_pause_req) ||
3077 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003078 ret = -EINVAL;
3079 goto out;
3080 }
3081
Ilya Dryomove4837f82012-03-27 17:09:17 +03003082 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3083 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3084 mixed = 1;
3085
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003086 /*
3087 * In case of mixed groups both data and meta should be picked,
3088 * and identical options should be given for both of them.
3089 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003090 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3091 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003092 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3093 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3094 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3095 printk(KERN_ERR "btrfs: with mixed groups data and "
3096 "metadata balance options must be the same\n");
3097 ret = -EINVAL;
3098 goto out;
3099 }
3100 }
3101
Stefan Behrens8dabb742012-11-06 13:15:27 +01003102 num_devices = fs_info->fs_devices->num_devices;
3103 btrfs_dev_replace_lock(&fs_info->dev_replace);
3104 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3105 BUG_ON(num_devices < 1);
3106 num_devices--;
3107 }
3108 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003109 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003110 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003111 allowed |= BTRFS_BLOCK_GROUP_DUP;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003112 else if (num_devices < 4)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003113 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3114 else
3115 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003116 BTRFS_BLOCK_GROUP_RAID10 |
3117 BTRFS_BLOCK_GROUP_RAID5 |
3118 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003119
Ilya Dryomov6728b192012-03-27 17:09:17 +03003120 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3121 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3122 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003123 printk(KERN_ERR "btrfs: unable to start balance with target "
3124 "data profile %llu\n",
3125 (unsigned long long)bctl->data.target);
3126 ret = -EINVAL;
3127 goto out;
3128 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003129 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3130 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3131 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003132 printk(KERN_ERR "btrfs: unable to start balance with target "
3133 "metadata profile %llu\n",
3134 (unsigned long long)bctl->meta.target);
3135 ret = -EINVAL;
3136 goto out;
3137 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003138 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3139 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3140 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003141 printk(KERN_ERR "btrfs: unable to start balance with target "
3142 "system profile %llu\n",
3143 (unsigned long long)bctl->sys.target);
3144 ret = -EINVAL;
3145 goto out;
3146 }
3147
Ilya Dryomove4837f82012-03-27 17:09:17 +03003148 /* allow dup'ed data chunks only in mixed mode */
3149 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003150 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003151 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3152 ret = -EINVAL;
3153 goto out;
3154 }
3155
3156 /* allow to reduce meta or sys integrity only if force set */
3157 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003158 BTRFS_BLOCK_GROUP_RAID10 |
3159 BTRFS_BLOCK_GROUP_RAID5 |
3160 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003161 do {
3162 seq = read_seqbegin(&fs_info->profiles_lock);
3163
3164 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3165 (fs_info->avail_system_alloc_bits & allowed) &&
3166 !(bctl->sys.target & allowed)) ||
3167 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3168 (fs_info->avail_metadata_alloc_bits & allowed) &&
3169 !(bctl->meta.target & allowed))) {
3170 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3171 printk(KERN_INFO "btrfs: force reducing metadata "
3172 "integrity\n");
3173 } else {
3174 printk(KERN_ERR "btrfs: balance will reduce metadata "
3175 "integrity, use force if you want this\n");
3176 ret = -EINVAL;
3177 goto out;
3178 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003179 }
Miao Xiede98ced2013-01-29 10:13:12 +00003180 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003181
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003182 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3183 int num_tolerated_disk_barrier_failures;
3184 u64 target = bctl->sys.target;
3185
3186 num_tolerated_disk_barrier_failures =
3187 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3188 if (num_tolerated_disk_barrier_failures > 0 &&
3189 (target &
3190 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3191 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3192 num_tolerated_disk_barrier_failures = 0;
3193 else if (num_tolerated_disk_barrier_failures > 1 &&
3194 (target &
3195 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3196 num_tolerated_disk_barrier_failures = 1;
3197
3198 fs_info->num_tolerated_disk_barrier_failures =
3199 num_tolerated_disk_barrier_failures;
3200 }
3201
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003202 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003203 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003204 goto out;
3205
Ilya Dryomov59641012012-01-16 22:04:48 +02003206 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3207 BUG_ON(ret == -EEXIST);
3208 set_balance_control(bctl);
3209 } else {
3210 BUG_ON(ret != -EEXIST);
3211 spin_lock(&fs_info->balance_lock);
3212 update_balance_args(bctl);
3213 spin_unlock(&fs_info->balance_lock);
3214 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003215
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003216 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003217 mutex_unlock(&fs_info->balance_mutex);
3218
3219 ret = __btrfs_balance(fs_info);
3220
3221 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003222 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003223
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003224 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3225 fs_info->num_tolerated_disk_barrier_failures =
3226 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3227 }
3228
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003229 if (bargs) {
3230 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003231 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003232 }
3233
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003234 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003235
3236 return ret;
3237out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003238 if (bctl->flags & BTRFS_BALANCE_RESUME)
3239 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003240 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003241 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003242 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3243 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003244 return ret;
3245}
3246
3247static int balance_kthread(void *data)
3248{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003249 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003250 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003251
3252 mutex_lock(&fs_info->volume_mutex);
3253 mutex_lock(&fs_info->balance_mutex);
3254
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003255 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003256 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003257 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003258 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003259
3260 mutex_unlock(&fs_info->balance_mutex);
3261 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003262
Ilya Dryomov59641012012-01-16 22:04:48 +02003263 return ret;
3264}
3265
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003266int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3267{
3268 struct task_struct *tsk;
3269
3270 spin_lock(&fs_info->balance_lock);
3271 if (!fs_info->balance_ctl) {
3272 spin_unlock(&fs_info->balance_lock);
3273 return 0;
3274 }
3275 spin_unlock(&fs_info->balance_lock);
3276
3277 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3278 printk(KERN_INFO "btrfs: force skipping balance\n");
3279 return 0;
3280 }
3281
3282 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3283 if (IS_ERR(tsk))
3284 return PTR_ERR(tsk);
3285
3286 return 0;
3287}
3288
Ilya Dryomov68310a52012-06-22 12:24:12 -06003289int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003290{
Ilya Dryomov59641012012-01-16 22:04:48 +02003291 struct btrfs_balance_control *bctl;
3292 struct btrfs_balance_item *item;
3293 struct btrfs_disk_balance_args disk_bargs;
3294 struct btrfs_path *path;
3295 struct extent_buffer *leaf;
3296 struct btrfs_key key;
3297 int ret;
3298
3299 path = btrfs_alloc_path();
3300 if (!path)
3301 return -ENOMEM;
3302
Ilya Dryomov68310a52012-06-22 12:24:12 -06003303 key.objectid = BTRFS_BALANCE_OBJECTID;
3304 key.type = BTRFS_BALANCE_ITEM_KEY;
3305 key.offset = 0;
3306
3307 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3308 if (ret < 0)
3309 goto out;
3310 if (ret > 0) { /* ret = -ENOENT; */
3311 ret = 0;
3312 goto out;
3313 }
3314
Ilya Dryomov59641012012-01-16 22:04:48 +02003315 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3316 if (!bctl) {
3317 ret = -ENOMEM;
3318 goto out;
3319 }
3320
Ilya Dryomov59641012012-01-16 22:04:48 +02003321 leaf = path->nodes[0];
3322 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3323
Ilya Dryomov68310a52012-06-22 12:24:12 -06003324 bctl->fs_info = fs_info;
3325 bctl->flags = btrfs_balance_flags(leaf, item);
3326 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003327
3328 btrfs_balance_data(leaf, item, &disk_bargs);
3329 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3330 btrfs_balance_meta(leaf, item, &disk_bargs);
3331 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3332 btrfs_balance_sys(leaf, item, &disk_bargs);
3333 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3334
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003335 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3336
Ilya Dryomov68310a52012-06-22 12:24:12 -06003337 mutex_lock(&fs_info->volume_mutex);
3338 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003339
Ilya Dryomov68310a52012-06-22 12:24:12 -06003340 set_balance_control(bctl);
3341
3342 mutex_unlock(&fs_info->balance_mutex);
3343 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003344out:
3345 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003346 return ret;
3347}
3348
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003349int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3350{
3351 int ret = 0;
3352
3353 mutex_lock(&fs_info->balance_mutex);
3354 if (!fs_info->balance_ctl) {
3355 mutex_unlock(&fs_info->balance_mutex);
3356 return -ENOTCONN;
3357 }
3358
3359 if (atomic_read(&fs_info->balance_running)) {
3360 atomic_inc(&fs_info->balance_pause_req);
3361 mutex_unlock(&fs_info->balance_mutex);
3362
3363 wait_event(fs_info->balance_wait_q,
3364 atomic_read(&fs_info->balance_running) == 0);
3365
3366 mutex_lock(&fs_info->balance_mutex);
3367 /* we are good with balance_ctl ripped off from under us */
3368 BUG_ON(atomic_read(&fs_info->balance_running));
3369 atomic_dec(&fs_info->balance_pause_req);
3370 } else {
3371 ret = -ENOTCONN;
3372 }
3373
3374 mutex_unlock(&fs_info->balance_mutex);
3375 return ret;
3376}
3377
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003378int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3379{
3380 mutex_lock(&fs_info->balance_mutex);
3381 if (!fs_info->balance_ctl) {
3382 mutex_unlock(&fs_info->balance_mutex);
3383 return -ENOTCONN;
3384 }
3385
3386 atomic_inc(&fs_info->balance_cancel_req);
3387 /*
3388 * if we are running just wait and return, balance item is
3389 * deleted in btrfs_balance in this case
3390 */
3391 if (atomic_read(&fs_info->balance_running)) {
3392 mutex_unlock(&fs_info->balance_mutex);
3393 wait_event(fs_info->balance_wait_q,
3394 atomic_read(&fs_info->balance_running) == 0);
3395 mutex_lock(&fs_info->balance_mutex);
3396 } else {
3397 /* __cancel_balance needs volume_mutex */
3398 mutex_unlock(&fs_info->balance_mutex);
3399 mutex_lock(&fs_info->volume_mutex);
3400 mutex_lock(&fs_info->balance_mutex);
3401
3402 if (fs_info->balance_ctl)
3403 __cancel_balance(fs_info);
3404
3405 mutex_unlock(&fs_info->volume_mutex);
3406 }
3407
3408 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3409 atomic_dec(&fs_info->balance_cancel_req);
3410 mutex_unlock(&fs_info->balance_mutex);
3411 return 0;
3412}
3413
Chris Mason8f18cf12008-04-25 16:53:30 -04003414/*
3415 * shrinking a device means finding all of the device extents past
3416 * the new size, and then following the back refs to the chunks.
3417 * The chunk relocation code actually frees the device extent
3418 */
3419int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3420{
3421 struct btrfs_trans_handle *trans;
3422 struct btrfs_root *root = device->dev_root;
3423 struct btrfs_dev_extent *dev_extent = NULL;
3424 struct btrfs_path *path;
3425 u64 length;
3426 u64 chunk_tree;
3427 u64 chunk_objectid;
3428 u64 chunk_offset;
3429 int ret;
3430 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003431 int failed = 0;
3432 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003433 struct extent_buffer *l;
3434 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003435 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003436 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003437 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003438 u64 diff = device->total_bytes - new_size;
3439
Stefan Behrens63a212a2012-11-05 18:29:28 +01003440 if (device->is_tgtdev_for_dev_replace)
3441 return -EINVAL;
3442
Chris Mason8f18cf12008-04-25 16:53:30 -04003443 path = btrfs_alloc_path();
3444 if (!path)
3445 return -ENOMEM;
3446
Chris Mason8f18cf12008-04-25 16:53:30 -04003447 path->reada = 2;
3448
Chris Mason7d9eb122008-07-08 14:19:17 -04003449 lock_chunks(root);
3450
Chris Mason8f18cf12008-04-25 16:53:30 -04003451 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003452 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003453 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003454 spin_lock(&root->fs_info->free_chunk_lock);
3455 root->fs_info->free_chunk_space -= diff;
3456 spin_unlock(&root->fs_info->free_chunk_lock);
3457 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003458 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003459
Josef Bacikba1bf482009-09-11 16:11:19 -04003460again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003461 key.objectid = device->devid;
3462 key.offset = (u64)-1;
3463 key.type = BTRFS_DEV_EXTENT_KEY;
3464
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003465 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003466 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3467 if (ret < 0)
3468 goto done;
3469
3470 ret = btrfs_previous_item(root, path, 0, key.type);
3471 if (ret < 0)
3472 goto done;
3473 if (ret) {
3474 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003475 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003476 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003477 }
3478
3479 l = path->nodes[0];
3480 slot = path->slots[0];
3481 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3482
Josef Bacikba1bf482009-09-11 16:11:19 -04003483 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003484 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003485 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003486 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003487
3488 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3489 length = btrfs_dev_extent_length(l, dev_extent);
3490
Josef Bacikba1bf482009-09-11 16:11:19 -04003491 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003492 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003493 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003494 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003495
3496 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3497 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3498 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003499 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003500
3501 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3502 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003503 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003504 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003505 if (ret == -ENOSPC)
3506 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003507 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003508
3509 if (failed && !retried) {
3510 failed = 0;
3511 retried = true;
3512 goto again;
3513 } else if (failed && retried) {
3514 ret = -ENOSPC;
3515 lock_chunks(root);
3516
3517 device->total_bytes = old_size;
3518 if (device->writeable)
3519 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003520 spin_lock(&root->fs_info->free_chunk_lock);
3521 root->fs_info->free_chunk_space += diff;
3522 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003523 unlock_chunks(root);
3524 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003525 }
3526
Chris Balld6397ba2009-04-27 07:29:03 -04003527 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003528 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003529 if (IS_ERR(trans)) {
3530 ret = PTR_ERR(trans);
3531 goto done;
3532 }
3533
Chris Balld6397ba2009-04-27 07:29:03 -04003534 lock_chunks(root);
3535
3536 device->disk_total_bytes = new_size;
3537 /* Now btrfs_update_device() will change the on-disk size. */
3538 ret = btrfs_update_device(trans, device);
3539 if (ret) {
3540 unlock_chunks(root);
3541 btrfs_end_transaction(trans, root);
3542 goto done;
3543 }
3544 WARN_ON(diff > old_total);
3545 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3546 unlock_chunks(root);
3547 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003548done:
3549 btrfs_free_path(path);
3550 return ret;
3551}
3552
Li Zefan125ccb02011-12-08 15:07:24 +08003553static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003554 struct btrfs_key *key,
3555 struct btrfs_chunk *chunk, int item_size)
3556{
David Sterba6c417612011-04-13 15:41:04 +02003557 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003558 struct btrfs_disk_key disk_key;
3559 u32 array_size;
3560 u8 *ptr;
3561
3562 array_size = btrfs_super_sys_array_size(super_copy);
3563 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3564 return -EFBIG;
3565
3566 ptr = super_copy->sys_chunk_array + array_size;
3567 btrfs_cpu_key_to_disk(&disk_key, key);
3568 memcpy(ptr, &disk_key, sizeof(disk_key));
3569 ptr += sizeof(disk_key);
3570 memcpy(ptr, chunk, item_size);
3571 item_size += sizeof(disk_key);
3572 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3573 return 0;
3574}
3575
Miao Xieb2117a32011-01-05 10:07:28 +00003576/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003577 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003578 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003579static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003580{
Arne Jansen73c5de02011-04-12 12:07:57 +02003581 const struct btrfs_device_info *di_a = a;
3582 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003583
Arne Jansen73c5de02011-04-12 12:07:57 +02003584 if (di_a->max_avail > di_b->max_avail)
3585 return -1;
3586 if (di_a->max_avail < di_b->max_avail)
3587 return 1;
3588 if (di_a->total_avail > di_b->total_avail)
3589 return -1;
3590 if (di_a->total_avail < di_b->total_avail)
3591 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003592 return 0;
3593}
3594
Liu Bo31e50222012-11-21 14:18:10 +00003595struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00003596 [BTRFS_RAID_RAID10] = {
3597 .sub_stripes = 2,
3598 .dev_stripes = 1,
3599 .devs_max = 0, /* 0 == as many as possible */
3600 .devs_min = 4,
3601 .devs_increment = 2,
3602 .ncopies = 2,
3603 },
3604 [BTRFS_RAID_RAID1] = {
3605 .sub_stripes = 1,
3606 .dev_stripes = 1,
3607 .devs_max = 2,
3608 .devs_min = 2,
3609 .devs_increment = 2,
3610 .ncopies = 2,
3611 },
3612 [BTRFS_RAID_DUP] = {
3613 .sub_stripes = 1,
3614 .dev_stripes = 2,
3615 .devs_max = 1,
3616 .devs_min = 1,
3617 .devs_increment = 1,
3618 .ncopies = 2,
3619 },
3620 [BTRFS_RAID_RAID0] = {
3621 .sub_stripes = 1,
3622 .dev_stripes = 1,
3623 .devs_max = 0,
3624 .devs_min = 2,
3625 .devs_increment = 1,
3626 .ncopies = 1,
3627 },
3628 [BTRFS_RAID_SINGLE] = {
3629 .sub_stripes = 1,
3630 .dev_stripes = 1,
3631 .devs_max = 1,
3632 .devs_min = 1,
3633 .devs_increment = 1,
3634 .ncopies = 1,
3635 },
Chris Masone942f882013-02-20 14:06:05 -05003636 [BTRFS_RAID_RAID5] = {
3637 .sub_stripes = 1,
3638 .dev_stripes = 1,
3639 .devs_max = 0,
3640 .devs_min = 2,
3641 .devs_increment = 1,
3642 .ncopies = 2,
3643 },
3644 [BTRFS_RAID_RAID6] = {
3645 .sub_stripes = 1,
3646 .dev_stripes = 1,
3647 .devs_max = 0,
3648 .devs_min = 3,
3649 .devs_increment = 1,
3650 .ncopies = 3,
3651 },
Liu Bo31e50222012-11-21 14:18:10 +00003652};
3653
David Woodhouse53b381b2013-01-29 18:40:14 -05003654static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3655{
3656 /* TODO allow them to set a preferred stripe size */
3657 return 64 * 1024;
3658}
3659
3660static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3661{
3662 u64 features;
3663
3664 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3665 return;
3666
3667 features = btrfs_super_incompat_flags(info->super_copy);
3668 if (features & BTRFS_FEATURE_INCOMPAT_RAID56)
3669 return;
3670
3671 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
3672 btrfs_set_super_incompat_flags(info->super_copy, features);
3673 printk(KERN_INFO "btrfs: setting RAID5/6 feature flag\n");
3674}
3675
Miao Xieb2117a32011-01-05 10:07:28 +00003676static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3677 struct btrfs_root *extent_root,
3678 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003679 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003680 u64 start, u64 type)
3681{
3682 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003683 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3684 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003685 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003686 struct extent_map_tree *em_tree;
3687 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003688 struct btrfs_device_info *devices_info = NULL;
3689 u64 total_avail;
3690 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05003691 int data_stripes; /* number of stripes that count for
3692 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02003693 int sub_stripes; /* sub_stripes info for map */
3694 int dev_stripes; /* stripes per dev */
3695 int devs_max; /* max devs to use */
3696 int devs_min; /* min devs needed */
3697 int devs_increment; /* ndevs has to be a multiple of this */
3698 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003699 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003700 u64 max_stripe_size;
3701 u64 max_chunk_size;
3702 u64 stripe_size;
3703 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05003704 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02003705 int ndevs;
3706 int i;
3707 int j;
Liu Bo31e50222012-11-21 14:18:10 +00003708 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00003709
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003710 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003711
Miao Xieb2117a32011-01-05 10:07:28 +00003712 if (list_empty(&fs_devices->alloc_list))
3713 return -ENOSPC;
3714
Liu Bo31e50222012-11-21 14:18:10 +00003715 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02003716
Liu Bo31e50222012-11-21 14:18:10 +00003717 sub_stripes = btrfs_raid_array[index].sub_stripes;
3718 dev_stripes = btrfs_raid_array[index].dev_stripes;
3719 devs_max = btrfs_raid_array[index].devs_max;
3720 devs_min = btrfs_raid_array[index].devs_min;
3721 devs_increment = btrfs_raid_array[index].devs_increment;
3722 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02003723
3724 if (type & BTRFS_BLOCK_GROUP_DATA) {
3725 max_stripe_size = 1024 * 1024 * 1024;
3726 max_chunk_size = 10 * max_stripe_size;
3727 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003728 /* for larger filesystems, use larger metadata chunks */
3729 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3730 max_stripe_size = 1024 * 1024 * 1024;
3731 else
3732 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003733 max_chunk_size = max_stripe_size;
3734 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003735 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003736 max_chunk_size = 2 * max_stripe_size;
3737 } else {
3738 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3739 type);
3740 BUG_ON(1);
3741 }
3742
3743 /* we don't want a chunk larger than 10% of writeable space */
3744 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3745 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003746
3747 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3748 GFP_NOFS);
3749 if (!devices_info)
3750 return -ENOMEM;
3751
Arne Jansen73c5de02011-04-12 12:07:57 +02003752 cur = fs_devices->alloc_list.next;
3753
3754 /*
3755 * in the first pass through the devices list, we gather information
3756 * about the available holes on each device.
3757 */
3758 ndevs = 0;
3759 while (cur != &fs_devices->alloc_list) {
3760 struct btrfs_device *device;
3761 u64 max_avail;
3762 u64 dev_offset;
3763
3764 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3765
3766 cur = cur->next;
3767
3768 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003769 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003770 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003771 continue;
3772 }
3773
Stefan Behrens63a212a2012-11-05 18:29:28 +01003774 if (!device->in_fs_metadata ||
3775 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003776 continue;
3777
3778 if (device->total_bytes > device->bytes_used)
3779 total_avail = device->total_bytes - device->bytes_used;
3780 else
3781 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003782
3783 /* If there is no space on this device, skip it. */
3784 if (total_avail == 0)
3785 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003786
Li Zefan125ccb02011-12-08 15:07:24 +08003787 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003788 max_stripe_size * dev_stripes,
3789 &dev_offset, &max_avail);
3790 if (ret && ret != -ENOSPC)
3791 goto error;
3792
3793 if (ret == 0)
3794 max_avail = max_stripe_size * dev_stripes;
3795
3796 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3797 continue;
3798
Eric Sandeen063d0062013-01-31 00:55:01 +00003799 if (ndevs == fs_devices->rw_devices) {
3800 WARN(1, "%s: found more than %llu devices\n",
3801 __func__, fs_devices->rw_devices);
3802 break;
3803 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003804 devices_info[ndevs].dev_offset = dev_offset;
3805 devices_info[ndevs].max_avail = max_avail;
3806 devices_info[ndevs].total_avail = total_avail;
3807 devices_info[ndevs].dev = device;
3808 ++ndevs;
3809 }
3810
3811 /*
3812 * now sort the devices by hole size / available space
3813 */
3814 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3815 btrfs_cmp_device_info, NULL);
3816
3817 /* round down to number of usable stripes */
3818 ndevs -= ndevs % devs_increment;
3819
3820 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3821 ret = -ENOSPC;
3822 goto error;
3823 }
3824
3825 if (devs_max && ndevs > devs_max)
3826 ndevs = devs_max;
3827 /*
3828 * the primary goal is to maximize the number of stripes, so use as many
3829 * devices as possible, even if the stripes are not maximum sized.
3830 */
3831 stripe_size = devices_info[ndevs-1].max_avail;
3832 num_stripes = ndevs * dev_stripes;
3833
David Woodhouse53b381b2013-01-29 18:40:14 -05003834 /*
3835 * this will have to be fixed for RAID1 and RAID10 over
3836 * more drives
3837 */
3838 data_stripes = num_stripes / ncopies;
3839
David Woodhouse53b381b2013-01-29 18:40:14 -05003840 if (type & BTRFS_BLOCK_GROUP_RAID5) {
3841 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
3842 btrfs_super_stripesize(info->super_copy));
3843 data_stripes = num_stripes - 1;
3844 }
3845 if (type & BTRFS_BLOCK_GROUP_RAID6) {
3846 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
3847 btrfs_super_stripesize(info->super_copy));
3848 data_stripes = num_stripes - 2;
3849 }
Chris Mason86db2572013-02-20 16:23:40 -05003850
3851 /*
3852 * Use the number of data stripes to figure out how big this chunk
3853 * is really going to be in terms of logical address space,
3854 * and compare that answer with the max chunk size
3855 */
3856 if (stripe_size * data_stripes > max_chunk_size) {
3857 u64 mask = (1ULL << 24) - 1;
3858 stripe_size = max_chunk_size;
3859 do_div(stripe_size, data_stripes);
3860
3861 /* bump the answer up to a 16MB boundary */
3862 stripe_size = (stripe_size + mask) & ~mask;
3863
3864 /* but don't go higher than the limits we found
3865 * while searching for free extents
3866 */
3867 if (stripe_size > devices_info[ndevs-1].max_avail)
3868 stripe_size = devices_info[ndevs-1].max_avail;
3869 }
3870
Arne Jansen73c5de02011-04-12 12:07:57 +02003871 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003872
3873 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05003874 do_div(stripe_size, raid_stripe_len);
3875 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02003876
Miao Xieb2117a32011-01-05 10:07:28 +00003877 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3878 if (!map) {
3879 ret = -ENOMEM;
3880 goto error;
3881 }
3882 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003883
Arne Jansen73c5de02011-04-12 12:07:57 +02003884 for (i = 0; i < ndevs; ++i) {
3885 for (j = 0; j < dev_stripes; ++j) {
3886 int s = i * dev_stripes + j;
3887 map->stripes[s].dev = devices_info[i].dev;
3888 map->stripes[s].physical = devices_info[i].dev_offset +
3889 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003890 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003891 }
Chris Mason593060d2008-03-25 16:50:33 -04003892 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05003893 map->stripe_len = raid_stripe_len;
3894 map->io_align = raid_stripe_len;
3895 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04003896 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003897 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003898
Yan Zheng2b820322008-11-17 21:11:30 -05003899 *map_ret = map;
David Woodhouse53b381b2013-01-29 18:40:14 -05003900 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003901
Arne Jansen73c5de02011-04-12 12:07:57 +02003902 *stripe_size_out = stripe_size;
3903 *num_bytes_out = num_bytes;
3904
3905 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003906
David Sterba172ddd62011-04-21 00:48:27 +02003907 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003908 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003909 ret = -ENOMEM;
3910 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003911 }
Chris Mason0b86a832008-03-24 15:01:56 -04003912 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003913 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003914 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003915 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003916 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003917
Chris Mason0b86a832008-03-24 15:01:56 -04003918 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003919 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003920 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003921 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003922 if (ret) {
3923 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003924 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003925 }
Yan Zheng2b820322008-11-17 21:11:30 -05003926
Arne Jansen73c5de02011-04-12 12:07:57 +02003927 for (i = 0; i < map->num_stripes; ++i) {
3928 struct btrfs_device *device;
3929 u64 dev_offset;
3930
3931 device = map->stripes[i].dev;
3932 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003933
3934 ret = btrfs_alloc_dev_extent(trans, device,
3935 info->chunk_root->root_key.objectid,
3936 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003937 start, dev_offset, stripe_size);
Josef Bacik04487482013-01-29 15:03:37 -05003938 if (ret)
3939 goto error_dev_extent;
3940 }
3941
3942 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3943 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3944 start, num_bytes);
3945 if (ret) {
3946 i = map->num_stripes - 1;
3947 goto error_dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05003948 }
3949
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003950 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05003951 check_raid56_incompat_flag(extent_root->fs_info, type);
3952
Miao Xieb2117a32011-01-05 10:07:28 +00003953 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003954 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003955
Josef Bacik04487482013-01-29 15:03:37 -05003956error_dev_extent:
3957 for (; i >= 0; i--) {
3958 struct btrfs_device *device;
3959 int err;
3960
3961 device = map->stripes[i].dev;
3962 err = btrfs_free_dev_extent(trans, device, start);
3963 if (err) {
3964 btrfs_abort_transaction(trans, extent_root, err);
3965 break;
3966 }
3967 }
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003968 write_lock(&em_tree->lock);
3969 remove_extent_mapping(em_tree, em);
3970 write_unlock(&em_tree->lock);
3971
3972 /* One for our allocation */
3973 free_extent_map(em);
3974 /* One for the tree reference */
3975 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00003976error:
3977 kfree(map);
3978 kfree(devices_info);
3979 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003980}
3981
3982static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3983 struct btrfs_root *extent_root,
3984 struct map_lookup *map, u64 chunk_offset,
3985 u64 chunk_size, u64 stripe_size)
3986{
3987 u64 dev_offset;
3988 struct btrfs_key key;
3989 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3990 struct btrfs_device *device;
3991 struct btrfs_chunk *chunk;
3992 struct btrfs_stripe *stripe;
3993 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3994 int index = 0;
3995 int ret;
3996
3997 chunk = kzalloc(item_size, GFP_NOFS);
3998 if (!chunk)
3999 return -ENOMEM;
4000
4001 index = 0;
4002 while (index < map->num_stripes) {
4003 device = map->stripes[index].dev;
4004 device->bytes_used += stripe_size;
4005 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004006 if (ret)
4007 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05004008 index++;
4009 }
4010
Josef Bacik2bf64752011-09-26 17:12:22 -04004011 spin_lock(&extent_root->fs_info->free_chunk_lock);
4012 extent_root->fs_info->free_chunk_space -= (stripe_size *
4013 map->num_stripes);
4014 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4015
Yan Zheng2b820322008-11-17 21:11:30 -05004016 index = 0;
4017 stripe = &chunk->stripe;
4018 while (index < map->num_stripes) {
4019 device = map->stripes[index].dev;
4020 dev_offset = map->stripes[index].physical;
4021
4022 btrfs_set_stack_stripe_devid(stripe, device->devid);
4023 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4024 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4025 stripe++;
4026 index++;
4027 }
4028
4029 btrfs_set_stack_chunk_length(chunk, chunk_size);
4030 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4031 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4032 btrfs_set_stack_chunk_type(chunk, map->type);
4033 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4034 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4035 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4036 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4037 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4038
4039 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4040 key.type = BTRFS_CHUNK_ITEM_KEY;
4041 key.offset = chunk_offset;
4042
4043 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004044
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004045 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4046 /*
4047 * TODO: Cleanup of inserted chunk root in case of
4048 * failure.
4049 */
Li Zefan125ccb02011-12-08 15:07:24 +08004050 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004051 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004052 }
liubo1abe9b82011-03-24 11:18:59 +00004053
Mark Fasheh3acd3952011-09-08 17:40:01 -07004054out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05004055 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004056 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004057}
4058
4059/*
4060 * Chunk allocation falls into two parts. The first part does works
4061 * that make the new allocated chunk useable, but not do any operation
4062 * that modifies the chunk tree. The second part does the works that
4063 * require modifying the chunk tree. This division is important for the
4064 * bootstrap process of adding storage to a seed btrfs.
4065 */
4066int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4067 struct btrfs_root *extent_root, u64 type)
4068{
4069 u64 chunk_offset;
4070 u64 chunk_size;
4071 u64 stripe_size;
4072 struct map_lookup *map;
4073 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4074 int ret;
4075
4076 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4077 &chunk_offset);
4078 if (ret)
4079 return ret;
4080
4081 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4082 &stripe_size, chunk_offset, type);
4083 if (ret)
4084 return ret;
4085
4086 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4087 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004088 if (ret)
4089 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004090 return 0;
4091}
4092
Chris Masond3977122009-01-05 21:25:51 -05004093static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004094 struct btrfs_root *root,
4095 struct btrfs_device *device)
4096{
4097 u64 chunk_offset;
4098 u64 sys_chunk_offset;
4099 u64 chunk_size;
4100 u64 sys_chunk_size;
4101 u64 stripe_size;
4102 u64 sys_stripe_size;
4103 u64 alloc_profile;
4104 struct map_lookup *map;
4105 struct map_lookup *sys_map;
4106 struct btrfs_fs_info *fs_info = root->fs_info;
4107 struct btrfs_root *extent_root = fs_info->extent_root;
4108 int ret;
4109
4110 ret = find_next_chunk(fs_info->chunk_root,
4111 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07004112 if (ret)
4113 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004114
Miao Xiede98ced2013-01-29 10:13:12 +00004115 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004116 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4117 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004118 if (ret)
4119 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004120
4121 sys_chunk_offset = chunk_offset + chunk_size;
4122
Miao Xiede98ced2013-01-29 10:13:12 +00004123 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004124 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
4125 &sys_chunk_size, &sys_stripe_size,
4126 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004127 if (ret) {
4128 btrfs_abort_transaction(trans, root, ret);
4129 goto out;
4130 }
Yan Zheng2b820322008-11-17 21:11:30 -05004131
4132 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06004133 if (ret) {
4134 btrfs_abort_transaction(trans, root, ret);
4135 goto out;
4136 }
Yan Zheng2b820322008-11-17 21:11:30 -05004137
4138 /*
4139 * Modifying chunk tree needs allocating new blocks from both
4140 * system block group and metadata block group. So we only can
4141 * do operations require modifying the chunk tree after both
4142 * block groups were created.
4143 */
4144 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4145 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06004146 if (ret) {
4147 btrfs_abort_transaction(trans, root, ret);
4148 goto out;
4149 }
Yan Zheng2b820322008-11-17 21:11:30 -05004150
4151 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
4152 sys_chunk_offset, sys_chunk_size,
4153 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004155 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004156
David Sterba005d6422012-09-18 07:52:32 -06004157out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004158
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004159 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004160}
4161
4162int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4163{
4164 struct extent_map *em;
4165 struct map_lookup *map;
4166 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4167 int readonly = 0;
4168 int i;
4169
Chris Mason890871b2009-09-02 16:24:52 -04004170 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004171 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004172 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004173 if (!em)
4174 return 1;
4175
Josef Bacikf48b9072010-01-27 02:07:59 +00004176 if (btrfs_test_opt(root, DEGRADED)) {
4177 free_extent_map(em);
4178 return 0;
4179 }
4180
Yan Zheng2b820322008-11-17 21:11:30 -05004181 map = (struct map_lookup *)em->bdev;
4182 for (i = 0; i < map->num_stripes; i++) {
4183 if (!map->stripes[i].dev->writeable) {
4184 readonly = 1;
4185 break;
4186 }
4187 }
4188 free_extent_map(em);
4189 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004190}
4191
4192void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4193{
David Sterbaa8067e02011-04-21 00:34:43 +02004194 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004195}
4196
4197void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4198{
4199 struct extent_map *em;
4200
Chris Masond3977122009-01-05 21:25:51 -05004201 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004202 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004203 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4204 if (em)
4205 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004206 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004207 if (!em)
4208 break;
4209 kfree(em->bdev);
4210 /* once for us */
4211 free_extent_map(em);
4212 /* once for the tree */
4213 free_extent_map(em);
4214 }
4215}
4216
Stefan Behrens5d964052012-11-05 14:59:07 +01004217int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004218{
Stefan Behrens5d964052012-11-05 14:59:07 +01004219 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004220 struct extent_map *em;
4221 struct map_lookup *map;
4222 struct extent_map_tree *em_tree = &map_tree->map_tree;
4223 int ret;
4224
Chris Mason890871b2009-09-02 16:24:52 -04004225 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004226 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004227 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004228 BUG_ON(!em);
4229
4230 BUG_ON(em->start > logical || em->start + em->len < logical);
4231 map = (struct map_lookup *)em->bdev;
4232 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4233 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004234 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4235 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004236 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4237 ret = 2;
4238 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4239 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004240 else
4241 ret = 1;
4242 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004243
4244 btrfs_dev_replace_lock(&fs_info->dev_replace);
4245 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4246 ret++;
4247 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4248
Chris Masonf1885912008-04-09 16:28:12 -04004249 return ret;
4250}
4251
David Woodhouse53b381b2013-01-29 18:40:14 -05004252unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4253 struct btrfs_mapping_tree *map_tree,
4254 u64 logical)
4255{
4256 struct extent_map *em;
4257 struct map_lookup *map;
4258 struct extent_map_tree *em_tree = &map_tree->map_tree;
4259 unsigned long len = root->sectorsize;
4260
4261 read_lock(&em_tree->lock);
4262 em = lookup_extent_mapping(em_tree, logical, len);
4263 read_unlock(&em_tree->lock);
4264 BUG_ON(!em);
4265
4266 BUG_ON(em->start > logical || em->start + em->len < logical);
4267 map = (struct map_lookup *)em->bdev;
4268 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4269 BTRFS_BLOCK_GROUP_RAID6)) {
4270 len = map->stripe_len * nr_data_stripes(map);
4271 }
4272 free_extent_map(em);
4273 return len;
4274}
4275
4276int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4277 u64 logical, u64 len, int mirror_num)
4278{
4279 struct extent_map *em;
4280 struct map_lookup *map;
4281 struct extent_map_tree *em_tree = &map_tree->map_tree;
4282 int ret = 0;
4283
4284 read_lock(&em_tree->lock);
4285 em = lookup_extent_mapping(em_tree, logical, len);
4286 read_unlock(&em_tree->lock);
4287 BUG_ON(!em);
4288
4289 BUG_ON(em->start > logical || em->start + em->len < logical);
4290 map = (struct map_lookup *)em->bdev;
4291 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4292 BTRFS_BLOCK_GROUP_RAID6))
4293 ret = 1;
4294 free_extent_map(em);
4295 return ret;
4296}
4297
Stefan Behrens30d98612012-11-06 14:52:18 +01004298static int find_live_mirror(struct btrfs_fs_info *fs_info,
4299 struct map_lookup *map, int first, int num,
4300 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004301{
4302 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004303 int tolerance;
4304 struct btrfs_device *srcdev;
4305
4306 if (dev_replace_is_ongoing &&
4307 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4308 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4309 srcdev = fs_info->dev_replace.srcdev;
4310 else
4311 srcdev = NULL;
4312
4313 /*
4314 * try to avoid the drive that is the source drive for a
4315 * dev-replace procedure, only choose it if no other non-missing
4316 * mirror is available
4317 */
4318 for (tolerance = 0; tolerance < 2; tolerance++) {
4319 if (map->stripes[optimal].dev->bdev &&
4320 (tolerance || map->stripes[optimal].dev != srcdev))
4321 return optimal;
4322 for (i = first; i < first + num; i++) {
4323 if (map->stripes[i].dev->bdev &&
4324 (tolerance || map->stripes[i].dev != srcdev))
4325 return i;
4326 }
Chris Masondfe25022008-05-13 13:46:40 -04004327 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004328
Chris Masondfe25022008-05-13 13:46:40 -04004329 /* we couldn't find one that doesn't fail. Just return something
4330 * and the io error handling code will clean up eventually
4331 */
4332 return optimal;
4333}
4334
David Woodhouse53b381b2013-01-29 18:40:14 -05004335static inline int parity_smaller(u64 a, u64 b)
4336{
4337 return a > b;
4338}
4339
4340/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4341static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4342{
4343 struct btrfs_bio_stripe s;
4344 int i;
4345 u64 l;
4346 int again = 1;
4347
4348 while (again) {
4349 again = 0;
4350 for (i = 0; i < bbio->num_stripes - 1; i++) {
4351 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4352 s = bbio->stripes[i];
4353 l = raid_map[i];
4354 bbio->stripes[i] = bbio->stripes[i+1];
4355 raid_map[i] = raid_map[i+1];
4356 bbio->stripes[i+1] = s;
4357 raid_map[i+1] = l;
4358 again = 1;
4359 }
4360 }
4361 }
4362}
4363
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004364static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004365 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004366 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004367 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004368{
4369 struct extent_map *em;
4370 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004371 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004372 struct extent_map_tree *em_tree = &map_tree->map_tree;
4373 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004374 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004375 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004376 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004377 u64 stripe_nr_orig;
4378 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004379 u64 stripe_len;
4380 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004381 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004382 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004383 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004384 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004385 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004386 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004387 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4388 int dev_replace_is_ongoing = 0;
4389 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004390 int patch_the_first_stripe_for_dev_replace = 0;
4391 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004392 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004393
Chris Mason890871b2009-09-02 16:24:52 -04004394 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004395 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004396 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004397
Chris Mason3b951512008-04-17 11:29:12 -04004398 if (!em) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004399 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004400 (unsigned long long)logical,
4401 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04004402 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04004403 }
Chris Mason0b86a832008-03-24 15:01:56 -04004404
4405 BUG_ON(em->start > logical || em->start + em->len < logical);
4406 map = (struct map_lookup *)em->bdev;
4407 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004408
David Woodhouse53b381b2013-01-29 18:40:14 -05004409 if (mirror_num > map->num_stripes)
4410 mirror_num = 0;
4411
4412 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004413 stripe_nr = offset;
4414 /*
4415 * stripe_nr counts the total number of stripes we have to stride
4416 * to get to this block
4417 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004418 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004419
David Woodhouse53b381b2013-01-29 18:40:14 -05004420 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004421 BUG_ON(offset < stripe_offset);
4422
4423 /* stripe_offset is the offset of this block in its stripe*/
4424 stripe_offset = offset - stripe_offset;
4425
David Woodhouse53b381b2013-01-29 18:40:14 -05004426 /* if we're here for raid56, we need to know the stripe aligned start */
4427 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4428 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4429 raid56_full_stripe_start = offset;
4430
4431 /* allow a write of a full stripe, but make sure we don't
4432 * allow straddling of stripes
4433 */
4434 do_div(raid56_full_stripe_start, full_stripe_len);
4435 raid56_full_stripe_start *= full_stripe_len;
4436 }
4437
4438 if (rw & REQ_DISCARD) {
4439 /* we don't discard raid56 yet */
4440 if (map->type &
4441 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4442 ret = -EOPNOTSUPP;
4443 goto out;
4444 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004445 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004446 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4447 u64 max_len;
4448 /* For writes to RAID[56], allow a full stripeset across all disks.
4449 For other RAID types and for RAID[56] reads, just allow a single
4450 stripe (on a single disk). */
4451 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4452 (rw & REQ_WRITE)) {
4453 max_len = stripe_len * nr_data_stripes(map) -
4454 (offset - raid56_full_stripe_start);
4455 } else {
4456 /* we limit the length of each bio to what fits in a stripe */
4457 max_len = stripe_len - stripe_offset;
4458 }
4459 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004460 } else {
4461 *length = em->len - offset;
4462 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004463
David Woodhouse53b381b2013-01-29 18:40:14 -05004464 /* This is for when we're called from btrfs_merge_bio_hook() and all
4465 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004466 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004467 goto out;
4468
Stefan Behrens472262f2012-11-06 14:43:46 +01004469 btrfs_dev_replace_lock(dev_replace);
4470 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4471 if (!dev_replace_is_ongoing)
4472 btrfs_dev_replace_unlock(dev_replace);
4473
Stefan Behrensad6d6202012-11-06 15:06:47 +01004474 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4475 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4476 dev_replace->tgtdev != NULL) {
4477 /*
4478 * in dev-replace case, for repair case (that's the only
4479 * case where the mirror is selected explicitly when
4480 * calling btrfs_map_block), blocks left of the left cursor
4481 * can also be read from the target drive.
4482 * For REQ_GET_READ_MIRRORS, the target drive is added as
4483 * the last one to the array of stripes. For READ, it also
4484 * needs to be supported using the same mirror number.
4485 * If the requested block is not left of the left cursor,
4486 * EIO is returned. This can happen because btrfs_num_copies()
4487 * returns one more in the dev-replace case.
4488 */
4489 u64 tmp_length = *length;
4490 struct btrfs_bio *tmp_bbio = NULL;
4491 int tmp_num_stripes;
4492 u64 srcdev_devid = dev_replace->srcdev->devid;
4493 int index_srcdev = 0;
4494 int found = 0;
4495 u64 physical_of_found = 0;
4496
4497 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004498 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004499 if (ret) {
4500 WARN_ON(tmp_bbio != NULL);
4501 goto out;
4502 }
4503
4504 tmp_num_stripes = tmp_bbio->num_stripes;
4505 if (mirror_num > tmp_num_stripes) {
4506 /*
4507 * REQ_GET_READ_MIRRORS does not contain this
4508 * mirror, that means that the requested area
4509 * is not left of the left cursor
4510 */
4511 ret = -EIO;
4512 kfree(tmp_bbio);
4513 goto out;
4514 }
4515
4516 /*
4517 * process the rest of the function using the mirror_num
4518 * of the source drive. Therefore look it up first.
4519 * At the end, patch the device pointer to the one of the
4520 * target drive.
4521 */
4522 for (i = 0; i < tmp_num_stripes; i++) {
4523 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4524 /*
4525 * In case of DUP, in order to keep it
4526 * simple, only add the mirror with the
4527 * lowest physical address
4528 */
4529 if (found &&
4530 physical_of_found <=
4531 tmp_bbio->stripes[i].physical)
4532 continue;
4533 index_srcdev = i;
4534 found = 1;
4535 physical_of_found =
4536 tmp_bbio->stripes[i].physical;
4537 }
4538 }
4539
4540 if (found) {
4541 mirror_num = index_srcdev + 1;
4542 patch_the_first_stripe_for_dev_replace = 1;
4543 physical_to_patch_in_first_stripe = physical_of_found;
4544 } else {
4545 WARN_ON(1);
4546 ret = -EIO;
4547 kfree(tmp_bbio);
4548 goto out;
4549 }
4550
4551 kfree(tmp_bbio);
4552 } else if (mirror_num > map->num_stripes) {
4553 mirror_num = 0;
4554 }
4555
Chris Masonf2d8d742008-04-21 10:03:05 -04004556 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004557 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004558 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004559 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004560 do_div(stripe_nr_end, map->stripe_len);
4561 stripe_end_offset = stripe_nr_end * map->stripe_len -
4562 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004563
Li Dongyangfce3bb92011-03-24 10:24:26 +00004564 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4565 if (rw & REQ_DISCARD)
4566 num_stripes = min_t(u64, map->num_stripes,
4567 stripe_nr_end - stripe_nr_orig);
4568 stripe_index = do_div(stripe_nr, map->num_stripes);
4569 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004570 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004571 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004572 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004573 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004574 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004575 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004576 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004577 current->pid % map->num_stripes,
4578 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004579 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004580 }
Chris Mason2fff7342008-04-29 14:12:09 -04004581
Chris Mason611f0e02008-04-03 16:29:03 -04004582 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004583 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004584 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004585 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004586 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004587 } else {
4588 mirror_num = 1;
4589 }
Chris Mason2fff7342008-04-29 14:12:09 -04004590
Chris Mason321aecc2008-04-16 10:49:51 -04004591 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4592 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004593
4594 stripe_index = do_div(stripe_nr, factor);
4595 stripe_index *= map->sub_stripes;
4596
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004597 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004598 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004599 else if (rw & REQ_DISCARD)
4600 num_stripes = min_t(u64, map->sub_stripes *
4601 (stripe_nr_end - stripe_nr_orig),
4602 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004603 else if (mirror_num)
4604 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004605 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004606 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004607 stripe_index = find_live_mirror(fs_info, map,
4608 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004609 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004610 current->pid % map->sub_stripes,
4611 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004612 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004613 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004614
4615 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4616 BTRFS_BLOCK_GROUP_RAID6)) {
4617 u64 tmp;
4618
4619 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4620 && raid_map_ret) {
4621 int i, rot;
4622
4623 /* push stripe_nr back to the start of the full stripe */
4624 stripe_nr = raid56_full_stripe_start;
4625 do_div(stripe_nr, stripe_len);
4626
4627 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4628
4629 /* RAID[56] write or recovery. Return all stripes */
4630 num_stripes = map->num_stripes;
4631 max_errors = nr_parity_stripes(map);
4632
4633 raid_map = kmalloc(sizeof(u64) * num_stripes,
4634 GFP_NOFS);
4635 if (!raid_map) {
4636 ret = -ENOMEM;
4637 goto out;
4638 }
4639
4640 /* Work out the disk rotation on this stripe-set */
4641 tmp = stripe_nr;
4642 rot = do_div(tmp, num_stripes);
4643
4644 /* Fill in the logical address of each stripe */
4645 tmp = stripe_nr * nr_data_stripes(map);
4646 for (i = 0; i < nr_data_stripes(map); i++)
4647 raid_map[(i+rot) % num_stripes] =
4648 em->start + (tmp + i) * map->stripe_len;
4649
4650 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4651 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4652 raid_map[(i+rot+1) % num_stripes] =
4653 RAID6_Q_STRIPE;
4654
4655 *length = map->stripe_len;
4656 stripe_index = 0;
4657 stripe_offset = 0;
4658 } else {
4659 /*
4660 * Mirror #0 or #1 means the original data block.
4661 * Mirror #2 is RAID5 parity block.
4662 * Mirror #3 is RAID6 Q block.
4663 */
4664 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4665 if (mirror_num > 1)
4666 stripe_index = nr_data_stripes(map) +
4667 mirror_num - 2;
4668
4669 /* We distribute the parity blocks across stripes */
4670 tmp = stripe_nr + stripe_index;
4671 stripe_index = do_div(tmp, map->num_stripes);
4672 }
Chris Mason8790d502008-04-03 16:29:03 -04004673 } else {
4674 /*
4675 * after this do_div call, stripe_nr is the number of stripes
4676 * on this device we have to walk to find the data, and
4677 * stripe_index is the number of our device in the stripe array
4678 */
4679 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004680 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004681 }
Chris Mason593060d2008-03-25 16:50:33 -04004682 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004683
Stefan Behrens472262f2012-11-06 14:43:46 +01004684 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004685 if (dev_replace_is_ongoing) {
4686 if (rw & (REQ_WRITE | REQ_DISCARD))
4687 num_alloc_stripes <<= 1;
4688 if (rw & REQ_GET_READ_MIRRORS)
4689 num_alloc_stripes++;
4690 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004691 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08004692 if (!bbio) {
4693 ret = -ENOMEM;
4694 goto out;
4695 }
4696 atomic_set(&bbio->error, 0);
4697
Li Dongyangfce3bb92011-03-24 10:24:26 +00004698 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004699 int factor = 0;
4700 int sub_stripes = 0;
4701 u64 stripes_per_dev = 0;
4702 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004703 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004704
4705 if (map->type &
4706 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4707 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4708 sub_stripes = 1;
4709 else
4710 sub_stripes = map->sub_stripes;
4711
4712 factor = map->num_stripes / sub_stripes;
4713 stripes_per_dev = div_u64_rem(stripe_nr_end -
4714 stripe_nr_orig,
4715 factor,
4716 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004717 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4718 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004719 }
4720
Li Dongyangfce3bb92011-03-24 10:24:26 +00004721 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004722 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004723 map->stripes[stripe_index].physical +
4724 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004725 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004726
Li Zefanec9ef7a2011-12-01 14:06:42 +08004727 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4728 BTRFS_BLOCK_GROUP_RAID10)) {
4729 bbio->stripes[i].length = stripes_per_dev *
4730 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004731
Li Zefanec9ef7a2011-12-01 14:06:42 +08004732 if (i / sub_stripes < remaining_stripes)
4733 bbio->stripes[i].length +=
4734 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004735
4736 /*
4737 * Special for the first stripe and
4738 * the last stripe:
4739 *
4740 * |-------|...|-------|
4741 * |----------|
4742 * off end_off
4743 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004744 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004745 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004746 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004747
4748 if (stripe_index >= last_stripe &&
4749 stripe_index <= (last_stripe +
4750 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004751 bbio->stripes[i].length -=
4752 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004753
Li Zefanec9ef7a2011-12-01 14:06:42 +08004754 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004755 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004756 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004757 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004758
4759 stripe_index++;
4760 if (stripe_index == map->num_stripes) {
4761 /* This could only happen for RAID0/10 */
4762 stripe_index = 0;
4763 stripe_nr++;
4764 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004765 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004766 } else {
4767 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004768 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004769 map->stripes[stripe_index].physical +
4770 stripe_offset +
4771 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004772 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004773 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004774 stripe_index++;
4775 }
Chris Mason593060d2008-03-25 16:50:33 -04004776 }
Li Zefande11cc12011-12-01 12:55:47 +08004777
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004778 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08004779 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4780 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05004781 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08004782 BTRFS_BLOCK_GROUP_DUP)) {
4783 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05004784 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4785 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08004786 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004787 }
Li Zefande11cc12011-12-01 12:55:47 +08004788
Stefan Behrens472262f2012-11-06 14:43:46 +01004789 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4790 dev_replace->tgtdev != NULL) {
4791 int index_where_to_add;
4792 u64 srcdev_devid = dev_replace->srcdev->devid;
4793
4794 /*
4795 * duplicate the write operations while the dev replace
4796 * procedure is running. Since the copying of the old disk
4797 * to the new disk takes place at run time while the
4798 * filesystem is mounted writable, the regular write
4799 * operations to the old disk have to be duplicated to go
4800 * to the new disk as well.
4801 * Note that device->missing is handled by the caller, and
4802 * that the write to the old disk is already set up in the
4803 * stripes array.
4804 */
4805 index_where_to_add = num_stripes;
4806 for (i = 0; i < num_stripes; i++) {
4807 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4808 /* write to new disk, too */
4809 struct btrfs_bio_stripe *new =
4810 bbio->stripes + index_where_to_add;
4811 struct btrfs_bio_stripe *old =
4812 bbio->stripes + i;
4813
4814 new->physical = old->physical;
4815 new->length = old->length;
4816 new->dev = dev_replace->tgtdev;
4817 index_where_to_add++;
4818 max_errors++;
4819 }
4820 }
4821 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004822 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4823 dev_replace->tgtdev != NULL) {
4824 u64 srcdev_devid = dev_replace->srcdev->devid;
4825 int index_srcdev = 0;
4826 int found = 0;
4827 u64 physical_of_found = 0;
4828
4829 /*
4830 * During the dev-replace procedure, the target drive can
4831 * also be used to read data in case it is needed to repair
4832 * a corrupt block elsewhere. This is possible if the
4833 * requested area is left of the left cursor. In this area,
4834 * the target drive is a full copy of the source drive.
4835 */
4836 for (i = 0; i < num_stripes; i++) {
4837 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4838 /*
4839 * In case of DUP, in order to keep it
4840 * simple, only add the mirror with the
4841 * lowest physical address
4842 */
4843 if (found &&
4844 physical_of_found <=
4845 bbio->stripes[i].physical)
4846 continue;
4847 index_srcdev = i;
4848 found = 1;
4849 physical_of_found = bbio->stripes[i].physical;
4850 }
4851 }
4852 if (found) {
4853 u64 length = map->stripe_len;
4854
4855 if (physical_of_found + length <=
4856 dev_replace->cursor_left) {
4857 struct btrfs_bio_stripe *tgtdev_stripe =
4858 bbio->stripes + num_stripes;
4859
4860 tgtdev_stripe->physical = physical_of_found;
4861 tgtdev_stripe->length =
4862 bbio->stripes[index_srcdev].length;
4863 tgtdev_stripe->dev = dev_replace->tgtdev;
4864
4865 num_stripes++;
4866 }
4867 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004868 }
4869
Li Zefande11cc12011-12-01 12:55:47 +08004870 *bbio_ret = bbio;
4871 bbio->num_stripes = num_stripes;
4872 bbio->max_errors = max_errors;
4873 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004874
4875 /*
4876 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4877 * mirror_num == num_stripes + 1 && dev_replace target drive is
4878 * available as a mirror
4879 */
4880 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
4881 WARN_ON(num_stripes > 1);
4882 bbio->stripes[0].dev = dev_replace->tgtdev;
4883 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
4884 bbio->mirror_num = map->num_stripes + 1;
4885 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004886 if (raid_map) {
4887 sort_parity_stripes(bbio, raid_map);
4888 *raid_map_ret = raid_map;
4889 }
Chris Masoncea9e442008-04-09 16:28:12 -04004890out:
Stefan Behrens472262f2012-11-06 14:43:46 +01004891 if (dev_replace_is_ongoing)
4892 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04004893 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004894 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004895}
4896
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004897int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004898 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004899 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004900{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004901 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004902 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04004903}
4904
Yan Zhenga512bbf2008-12-08 16:46:26 -05004905int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4906 u64 chunk_start, u64 physical, u64 devid,
4907 u64 **logical, int *naddrs, int *stripe_len)
4908{
4909 struct extent_map_tree *em_tree = &map_tree->map_tree;
4910 struct extent_map *em;
4911 struct map_lookup *map;
4912 u64 *buf;
4913 u64 bytenr;
4914 u64 length;
4915 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05004916 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05004917 int i, j, nr = 0;
4918
Chris Mason890871b2009-09-02 16:24:52 -04004919 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004920 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004921 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004922
4923 BUG_ON(!em || em->start != chunk_start);
4924 map = (struct map_lookup *)em->bdev;
4925
4926 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05004927 rmap_len = map->stripe_len;
4928
Yan Zhenga512bbf2008-12-08 16:46:26 -05004929 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4930 do_div(length, map->num_stripes / map->sub_stripes);
4931 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4932 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05004933 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4934 BTRFS_BLOCK_GROUP_RAID6)) {
4935 do_div(length, nr_data_stripes(map));
4936 rmap_len = map->stripe_len * nr_data_stripes(map);
4937 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004938
4939 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004940 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004941
4942 for (i = 0; i < map->num_stripes; i++) {
4943 if (devid && map->stripes[i].dev->devid != devid)
4944 continue;
4945 if (map->stripes[i].physical > physical ||
4946 map->stripes[i].physical + length <= physical)
4947 continue;
4948
4949 stripe_nr = physical - map->stripes[i].physical;
4950 do_div(stripe_nr, map->stripe_len);
4951
4952 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4953 stripe_nr = stripe_nr * map->num_stripes + i;
4954 do_div(stripe_nr, map->sub_stripes);
4955 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4956 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05004957 } /* else if RAID[56], multiply by nr_data_stripes().
4958 * Alternatively, just use rmap_len below instead of
4959 * map->stripe_len */
4960
4961 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05004962 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004963 for (j = 0; j < nr; j++) {
4964 if (buf[j] == bytenr)
4965 break;
4966 }
Chris Mason934d3752008-12-08 16:43:10 -05004967 if (j == nr) {
4968 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004969 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004970 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004971 }
4972
Yan Zhenga512bbf2008-12-08 16:46:26 -05004973 *logical = buf;
4974 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05004975 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05004976
4977 free_extent_map(em);
4978 return 0;
4979}
4980
Stefan Behrens442a4f62012-05-25 16:06:08 +02004981static void *merge_stripe_index_into_bio_private(void *bi_private,
4982 unsigned int stripe_index)
4983{
4984 /*
4985 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4986 * at most 1.
4987 * The alternative solution (instead of stealing bits from the
4988 * pointer) would be to allocate an intermediate structure
4989 * that contains the old private pointer plus the stripe_index.
4990 */
4991 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4992 BUG_ON(stripe_index > 3);
4993 return (void *)(((uintptr_t)bi_private) | stripe_index);
4994}
4995
4996static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4997{
4998 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4999}
5000
5001static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
5002{
5003 return (unsigned int)((uintptr_t)bi_private) & 3;
5004}
5005
Jan Schmidta1d3c472011-08-04 17:15:33 +02005006static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005007{
Stefan Behrens442a4f62012-05-25 16:06:08 +02005008 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005009 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005010
Stefan Behrens442a4f62012-05-25 16:06:08 +02005011 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005012 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005013 if (err == -EIO || err == -EREMOTEIO) {
5014 unsigned int stripe_index =
5015 extract_stripe_index_from_bio_private(
5016 bio->bi_private);
5017 struct btrfs_device *dev;
5018
5019 BUG_ON(stripe_index >= bbio->num_stripes);
5020 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005021 if (dev->bdev) {
5022 if (bio->bi_rw & WRITE)
5023 btrfs_dev_stat_inc(dev,
5024 BTRFS_DEV_STAT_WRITE_ERRS);
5025 else
5026 btrfs_dev_stat_inc(dev,
5027 BTRFS_DEV_STAT_READ_ERRS);
5028 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5029 btrfs_dev_stat_inc(dev,
5030 BTRFS_DEV_STAT_FLUSH_ERRS);
5031 btrfs_dev_stat_print_on_error(dev);
5032 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005033 }
5034 }
Chris Mason8790d502008-04-03 16:29:03 -04005035
Jan Schmidta1d3c472011-08-04 17:15:33 +02005036 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005037 is_orig_bio = 1;
5038
Jan Schmidta1d3c472011-08-04 17:15:33 +02005039 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005040 if (!is_orig_bio) {
5041 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005042 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005043 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005044 bio->bi_private = bbio->private;
5045 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02005046 bio->bi_bdev = (struct block_device *)
5047 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005048 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005049 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005050 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005051 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005052 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005053 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005054 /*
5055 * this bio is actually up to date, we didn't
5056 * go over the max number of errors
5057 */
5058 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005059 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005060 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005061 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04005062
5063 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005064 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005065 bio_put(bio);
5066 }
Chris Mason8790d502008-04-03 16:29:03 -04005067}
5068
Chris Mason8b712842008-06-11 16:50:36 -04005069struct async_sched {
5070 struct bio *bio;
5071 int rw;
5072 struct btrfs_fs_info *info;
5073 struct btrfs_work work;
5074};
5075
5076/*
5077 * see run_scheduled_bios for a description of why bios are collected for
5078 * async submit.
5079 *
5080 * This will add one bio to the pending list for a device and make sure
5081 * the work struct is scheduled.
5082 */
David Woodhouse53b381b2013-01-29 18:40:14 -05005083noinline void btrfs_schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04005084 struct btrfs_device *device,
5085 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005086{
5087 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005088 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005089
David Woodhouse53b381b2013-01-29 18:40:14 -05005090 if (device->missing || !device->bdev) {
5091 bio_endio(bio, -EIO);
5092 return;
5093 }
5094
Chris Mason8b712842008-06-11 16:50:36 -04005095 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005096 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005097 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005098 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005099 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005100 return;
Chris Mason8b712842008-06-11 16:50:36 -04005101 }
5102
5103 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005104 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005105 * higher layers. Otherwise, the async bio makes it appear we have
5106 * made progress against dirty pages when we've really just put it
5107 * on a queue for later
5108 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005109 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005110 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005111 bio->bi_next = NULL;
5112 bio->bi_rw |= rw;
5113
5114 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005115 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005116 pending_bios = &device->pending_sync_bios;
5117 else
5118 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005119
Chris Masonffbd5172009-04-20 15:50:09 -04005120 if (pending_bios->tail)
5121 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005122
Chris Masonffbd5172009-04-20 15:50:09 -04005123 pending_bios->tail = bio;
5124 if (!pending_bios->head)
5125 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005126 if (device->running_pending)
5127 should_queue = 0;
5128
5129 spin_unlock(&device->io_lock);
5130
5131 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04005132 btrfs_queue_worker(&root->fs_info->submit_workers,
5133 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005134}
5135
Josef Bacikde1ee922012-10-19 16:50:56 -04005136static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5137 sector_t sector)
5138{
5139 struct bio_vec *prev;
5140 struct request_queue *q = bdev_get_queue(bdev);
5141 unsigned short max_sectors = queue_max_sectors(q);
5142 struct bvec_merge_data bvm = {
5143 .bi_bdev = bdev,
5144 .bi_sector = sector,
5145 .bi_rw = bio->bi_rw,
5146 };
5147
5148 if (bio->bi_vcnt == 0) {
5149 WARN_ON(1);
5150 return 1;
5151 }
5152
5153 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
5154 if ((bio->bi_size >> 9) > max_sectors)
5155 return 0;
5156
5157 if (!q->merge_bvec_fn)
5158 return 1;
5159
5160 bvm.bi_size = bio->bi_size - prev->bv_len;
5161 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5162 return 0;
5163 return 1;
5164}
5165
5166static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5167 struct bio *bio, u64 physical, int dev_nr,
5168 int rw, int async)
5169{
5170 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5171
5172 bio->bi_private = bbio;
5173 bio->bi_private = merge_stripe_index_into_bio_private(
5174 bio->bi_private, (unsigned int)dev_nr);
5175 bio->bi_end_io = btrfs_end_bio;
5176 bio->bi_sector = physical >> 9;
5177#ifdef DEBUG
5178 {
5179 struct rcu_string *name;
5180
5181 rcu_read_lock();
5182 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005183 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005184 "(%s id %llu), size=%u\n", rw,
5185 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5186 name->str, dev->devid, bio->bi_size);
5187 rcu_read_unlock();
5188 }
5189#endif
5190 bio->bi_bdev = dev->bdev;
5191 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005192 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005193 else
5194 btrfsic_submit_bio(rw, bio);
5195}
5196
5197static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5198 struct bio *first_bio, struct btrfs_device *dev,
5199 int dev_nr, int rw, int async)
5200{
5201 struct bio_vec *bvec = first_bio->bi_io_vec;
5202 struct bio *bio;
5203 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5204 u64 physical = bbio->stripes[dev_nr].physical;
5205
5206again:
5207 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5208 if (!bio)
5209 return -ENOMEM;
5210
5211 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5212 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5213 bvec->bv_offset) < bvec->bv_len) {
5214 u64 len = bio->bi_size;
5215
5216 atomic_inc(&bbio->stripes_pending);
5217 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5218 rw, async);
5219 physical += len;
5220 goto again;
5221 }
5222 bvec++;
5223 }
5224
5225 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5226 return 0;
5227}
5228
5229static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5230{
5231 atomic_inc(&bbio->error);
5232 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5233 bio->bi_private = bbio->private;
5234 bio->bi_end_io = bbio->end_io;
5235 bio->bi_bdev = (struct block_device *)
5236 (unsigned long)bbio->mirror_num;
5237 bio->bi_sector = logical >> 9;
5238 kfree(bbio);
5239 bio_endio(bio, -EIO);
5240 }
5241}
5242
Chris Masonf1885912008-04-09 16:28:12 -04005243int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005244 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005245{
Chris Mason0b86a832008-03-24 15:01:56 -04005246 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005247 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04005248 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005249 u64 length = 0;
5250 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005251 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005252 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005253 int dev_nr = 0;
5254 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005255 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005256
Chris Masonf2d8d742008-04-21 10:03:05 -04005257 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005258 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005259
David Woodhouse53b381b2013-01-29 18:40:14 -05005260 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5261 mirror_num, &raid_map);
5262 if (ret) /* -ENOMEM */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005263 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04005264
Jan Schmidta1d3c472011-08-04 17:15:33 +02005265 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005266 bbio->orig_bio = first_bio;
5267 bbio->private = first_bio->bi_private;
5268 bbio->end_io = first_bio->bi_end_io;
5269 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5270
5271 if (raid_map) {
5272 /* In this case, map_length has been set to the length of
5273 a single stripe; not the whole write */
5274 if (rw & WRITE) {
5275 return raid56_parity_write(root, bio, bbio,
5276 raid_map, map_length);
5277 } else {
5278 return raid56_parity_recover(root, bio, bbio,
5279 raid_map, map_length,
5280 mirror_num);
5281 }
5282 }
5283
Chris Masoncea9e442008-04-09 16:28:12 -04005284 if (map_length < length) {
Daniel J Blueman48940662012-05-07 06:35:38 -06005285 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
Chris Masond3977122009-01-05 21:25:51 -05005286 "len %llu\n", (unsigned long long)logical,
5287 (unsigned long long)length,
5288 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005289 BUG();
5290 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005291
Chris Masond3977122009-01-05 21:25:51 -05005292 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005293 dev = bbio->stripes[dev_nr].dev;
5294 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5295 bbio_error(bbio, first_bio, logical);
5296 dev_nr++;
5297 continue;
5298 }
5299
5300 /*
5301 * Check and see if we're ok with this bio based on it's size
5302 * and offset with the given device.
5303 */
5304 if (!bio_size_ok(dev->bdev, first_bio,
5305 bbio->stripes[dev_nr].physical >> 9)) {
5306 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5307 dev_nr, rw, async_submit);
5308 BUG_ON(ret);
5309 dev_nr++;
5310 continue;
5311 }
5312
Jan Schmidta1d3c472011-08-04 17:15:33 +02005313 if (dev_nr < total_devs - 1) {
5314 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005315 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005316 } else {
5317 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04005318 }
Josef Bacik606686e2012-06-04 14:03:51 -04005319
Josef Bacikde1ee922012-10-19 16:50:56 -04005320 submit_stripe_bio(root, bbio, bio,
5321 bbio->stripes[dev_nr].physical, dev_nr, rw,
5322 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005323 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005324 }
Chris Mason0b86a832008-03-24 15:01:56 -04005325 return 0;
5326}
5327
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005328struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005329 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005330{
Yan Zheng2b820322008-11-17 21:11:30 -05005331 struct btrfs_device *device;
5332 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005333
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005334 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005335 while (cur_devices) {
5336 if (!fsid ||
5337 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5338 device = __find_device(&cur_devices->devices,
5339 devid, uuid);
5340 if (device)
5341 return device;
5342 }
5343 cur_devices = cur_devices->seed;
5344 }
5345 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005346}
5347
Chris Masondfe25022008-05-13 13:46:40 -04005348static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5349 u64 devid, u8 *dev_uuid)
5350{
5351 struct btrfs_device *device;
5352 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5353
5354 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005355 if (!device)
5356 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04005357 list_add(&device->dev_list,
5358 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04005359 device->dev_root = root->fs_info->dev_root;
5360 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04005361 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05005362 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05005363 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04005364 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05005365 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04005366 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05005367 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04005368 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
5369 return device;
5370}
5371
Chris Mason0b86a832008-03-24 15:01:56 -04005372static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5373 struct extent_buffer *leaf,
5374 struct btrfs_chunk *chunk)
5375{
5376 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5377 struct map_lookup *map;
5378 struct extent_map *em;
5379 u64 logical;
5380 u64 length;
5381 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005382 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005383 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005384 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005385 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005386
Chris Masone17cade2008-04-15 15:41:47 -04005387 logical = key->offset;
5388 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005389
Chris Mason890871b2009-09-02 16:24:52 -04005390 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005391 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005392 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005393
5394 /* already mapped? */
5395 if (em && em->start <= logical && em->start + em->len > logical) {
5396 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005397 return 0;
5398 } else if (em) {
5399 free_extent_map(em);
5400 }
Chris Mason0b86a832008-03-24 15:01:56 -04005401
David Sterba172ddd62011-04-21 00:48:27 +02005402 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005403 if (!em)
5404 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005405 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5406 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005407 if (!map) {
5408 free_extent_map(em);
5409 return -ENOMEM;
5410 }
5411
5412 em->bdev = (struct block_device *)map;
5413 em->start = logical;
5414 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005415 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005416 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005417 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005418
Chris Mason593060d2008-03-25 16:50:33 -04005419 map->num_stripes = num_stripes;
5420 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5421 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5422 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5423 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5424 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005425 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005426 for (i = 0; i < num_stripes; i++) {
5427 map->stripes[i].physical =
5428 btrfs_stripe_offset_nr(leaf, chunk, i);
5429 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005430 read_extent_buffer(leaf, uuid, (unsigned long)
5431 btrfs_stripe_dev_uuid_nr(chunk, i),
5432 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005433 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5434 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005435 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005436 kfree(map);
5437 free_extent_map(em);
5438 return -EIO;
5439 }
Chris Masondfe25022008-05-13 13:46:40 -04005440 if (!map->stripes[i].dev) {
5441 map->stripes[i].dev =
5442 add_missing_dev(root, devid, uuid);
5443 if (!map->stripes[i].dev) {
5444 kfree(map);
5445 free_extent_map(em);
5446 return -EIO;
5447 }
5448 }
5449 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005450 }
5451
Chris Mason890871b2009-09-02 16:24:52 -04005452 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005453 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005454 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005455 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005456 free_extent_map(em);
5457
5458 return 0;
5459}
5460
Jeff Mahoney143bede2012-03-01 14:56:26 +01005461static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005462 struct btrfs_dev_item *dev_item,
5463 struct btrfs_device *device)
5464{
5465 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005466
5467 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005468 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5469 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005470 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5471 device->type = btrfs_device_type(leaf, dev_item);
5472 device->io_align = btrfs_device_io_align(leaf, dev_item);
5473 device->io_width = btrfs_device_io_width(leaf, dev_item);
5474 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005475 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005476 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005477
5478 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005479 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005480}
5481
Yan Zheng2b820322008-11-17 21:11:30 -05005482static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5483{
5484 struct btrfs_fs_devices *fs_devices;
5485 int ret;
5486
Li Zefanb367e472011-12-07 11:38:24 +08005487 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005488
5489 fs_devices = root->fs_info->fs_devices->seed;
5490 while (fs_devices) {
5491 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5492 ret = 0;
5493 goto out;
5494 }
5495 fs_devices = fs_devices->seed;
5496 }
5497
5498 fs_devices = find_fsid(fsid);
5499 if (!fs_devices) {
5500 ret = -ENOENT;
5501 goto out;
5502 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005503
5504 fs_devices = clone_fs_devices(fs_devices);
5505 if (IS_ERR(fs_devices)) {
5506 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005507 goto out;
5508 }
5509
Christoph Hellwig97288f22008-12-02 06:36:09 -05005510 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005511 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005512 if (ret) {
5513 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005514 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005515 }
Yan Zheng2b820322008-11-17 21:11:30 -05005516
5517 if (!fs_devices->seeding) {
5518 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005519 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005520 ret = -EINVAL;
5521 goto out;
5522 }
5523
5524 fs_devices->seed = root->fs_info->fs_devices->seed;
5525 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005526out:
Yan Zheng2b820322008-11-17 21:11:30 -05005527 return ret;
5528}
5529
Chris Mason0d81ba52008-03-24 15:02:07 -04005530static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005531 struct extent_buffer *leaf,
5532 struct btrfs_dev_item *dev_item)
5533{
5534 struct btrfs_device *device;
5535 u64 devid;
5536 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005537 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005538 u8 dev_uuid[BTRFS_UUID_SIZE];
5539
Chris Mason0b86a832008-03-24 15:01:56 -04005540 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04005541 read_extent_buffer(leaf, dev_uuid,
5542 (unsigned long)btrfs_device_uuid(dev_item),
5543 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05005544 read_extent_buffer(leaf, fs_uuid,
5545 (unsigned long)btrfs_device_fsid(dev_item),
5546 BTRFS_UUID_SIZE);
5547
5548 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5549 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005550 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005551 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005552 }
5553
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005554 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005555 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005556 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005557 return -EIO;
5558
5559 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05005560 printk(KERN_WARNING "warning devid %llu missing\n",
5561 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005562 device = add_missing_dev(root, devid, dev_uuid);
5563 if (!device)
5564 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005565 } else if (!device->missing) {
5566 /*
5567 * this happens when a device that was properly setup
5568 * in the device info lists suddenly goes bad.
5569 * device->bdev is NULL, and so we have to set
5570 * device->missing to one here
5571 */
5572 root->fs_info->fs_devices->missing_devices++;
5573 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05005574 }
5575 }
5576
5577 if (device->fs_devices != root->fs_info->fs_devices) {
5578 BUG_ON(device->writeable);
5579 if (device->generation !=
5580 btrfs_device_generation(leaf, dev_item))
5581 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04005582 }
Chris Mason0b86a832008-03-24 15:01:56 -04005583
5584 fill_device_from_item(leaf, dev_item, device);
5585 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04005586 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01005587 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05005588 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04005589 spin_lock(&root->fs_info->free_chunk_lock);
5590 root->fs_info->free_chunk_space += device->total_bytes -
5591 device->bytes_used;
5592 spin_unlock(&root->fs_info->free_chunk_lock);
5593 }
Chris Mason0b86a832008-03-24 15:01:56 -04005594 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005595 return ret;
5596}
5597
Yan Zhenge4404d62008-12-12 10:03:26 -05005598int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04005599{
David Sterba6c417612011-04-13 15:41:04 +02005600 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04005601 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04005602 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04005603 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04005604 u8 *ptr;
5605 unsigned long sb_ptr;
5606 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005607 u32 num_stripes;
5608 u32 array_size;
5609 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005610 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04005611 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04005612
Yan Zhenge4404d62008-12-12 10:03:26 -05005613 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04005614 BTRFS_SUPER_INFO_SIZE);
5615 if (!sb)
5616 return -ENOMEM;
5617 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04005618 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02005619 /*
5620 * The sb extent buffer is artifical and just used to read the system array.
5621 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5622 * pages up-to-date when the page is larger: extent does not cover the
5623 * whole page and consequently check_page_uptodate does not find all
5624 * the page's extents up-to-date (the hole beyond sb),
5625 * write_extent_buffer then triggers a WARN_ON.
5626 *
5627 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5628 * but sb spans only this function. Add an explicit SetPageUptodate call
5629 * to silence the warning eg. on PowerPC 64.
5630 */
5631 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04005632 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05005633
Chris Masona061fc82008-05-07 11:43:44 -04005634 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005635 array_size = btrfs_super_sys_array_size(super_copy);
5636
Chris Mason0b86a832008-03-24 15:01:56 -04005637 ptr = super_copy->sys_chunk_array;
5638 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5639 cur = 0;
5640
5641 while (cur < array_size) {
5642 disk_key = (struct btrfs_disk_key *)ptr;
5643 btrfs_disk_key_to_cpu(&key, disk_key);
5644
Chris Masona061fc82008-05-07 11:43:44 -04005645 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04005646 sb_ptr += len;
5647 cur += len;
5648
Chris Mason0d81ba52008-03-24 15:02:07 -04005649 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04005650 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04005651 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04005652 if (ret)
5653 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005654 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5655 len = btrfs_chunk_item_size(num_stripes);
5656 } else {
Chris Mason84eed902008-04-25 09:04:37 -04005657 ret = -EIO;
5658 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005659 }
5660 ptr += len;
5661 sb_ptr += len;
5662 cur += len;
5663 }
Chris Masona061fc82008-05-07 11:43:44 -04005664 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04005665 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005666}
5667
5668int btrfs_read_chunk_tree(struct btrfs_root *root)
5669{
5670 struct btrfs_path *path;
5671 struct extent_buffer *leaf;
5672 struct btrfs_key key;
5673 struct btrfs_key found_key;
5674 int ret;
5675 int slot;
5676
5677 root = root->fs_info->chunk_root;
5678
5679 path = btrfs_alloc_path();
5680 if (!path)
5681 return -ENOMEM;
5682
Li Zefanb367e472011-12-07 11:38:24 +08005683 mutex_lock(&uuid_mutex);
5684 lock_chunks(root);
5685
Chris Mason0b86a832008-03-24 15:01:56 -04005686 /* first we search for all of the device items, and then we
5687 * read in all of the chunk items. This way we can create chunk
5688 * mappings that reference all of the devices that are afound
5689 */
5690 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5691 key.offset = 0;
5692 key.type = 0;
5693again:
5694 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005695 if (ret < 0)
5696 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005697 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005698 leaf = path->nodes[0];
5699 slot = path->slots[0];
5700 if (slot >= btrfs_header_nritems(leaf)) {
5701 ret = btrfs_next_leaf(root, path);
5702 if (ret == 0)
5703 continue;
5704 if (ret < 0)
5705 goto error;
5706 break;
5707 }
5708 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5709 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5710 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5711 break;
5712 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5713 struct btrfs_dev_item *dev_item;
5714 dev_item = btrfs_item_ptr(leaf, slot,
5715 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04005716 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05005717 if (ret)
5718 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005719 }
5720 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5721 struct btrfs_chunk *chunk;
5722 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5723 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005724 if (ret)
5725 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005726 }
5727 path->slots[0]++;
5728 }
5729 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5730 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005731 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005732 goto again;
5733 }
Chris Mason0b86a832008-03-24 15:01:56 -04005734 ret = 0;
5735error:
Li Zefanb367e472011-12-07 11:38:24 +08005736 unlock_chunks(root);
5737 mutex_unlock(&uuid_mutex);
5738
Yan Zheng2b820322008-11-17 21:11:30 -05005739 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005740 return ret;
5741}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005742
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005743static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5744{
5745 int i;
5746
5747 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5748 btrfs_dev_stat_reset(dev, i);
5749}
5750
5751int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5752{
5753 struct btrfs_key key;
5754 struct btrfs_key found_key;
5755 struct btrfs_root *dev_root = fs_info->dev_root;
5756 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5757 struct extent_buffer *eb;
5758 int slot;
5759 int ret = 0;
5760 struct btrfs_device *device;
5761 struct btrfs_path *path = NULL;
5762 int i;
5763
5764 path = btrfs_alloc_path();
5765 if (!path) {
5766 ret = -ENOMEM;
5767 goto out;
5768 }
5769
5770 mutex_lock(&fs_devices->device_list_mutex);
5771 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5772 int item_size;
5773 struct btrfs_dev_stats_item *ptr;
5774
5775 key.objectid = 0;
5776 key.type = BTRFS_DEV_STATS_KEY;
5777 key.offset = device->devid;
5778 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5779 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005780 __btrfs_reset_dev_stats(device);
5781 device->dev_stats_valid = 1;
5782 btrfs_release_path(path);
5783 continue;
5784 }
5785 slot = path->slots[0];
5786 eb = path->nodes[0];
5787 btrfs_item_key_to_cpu(eb, &found_key, slot);
5788 item_size = btrfs_item_size_nr(eb, slot);
5789
5790 ptr = btrfs_item_ptr(eb, slot,
5791 struct btrfs_dev_stats_item);
5792
5793 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5794 if (item_size >= (1 + i) * sizeof(__le64))
5795 btrfs_dev_stat_set(device, i,
5796 btrfs_dev_stats_value(eb, ptr, i));
5797 else
5798 btrfs_dev_stat_reset(device, i);
5799 }
5800
5801 device->dev_stats_valid = 1;
5802 btrfs_dev_stat_print_on_load(device);
5803 btrfs_release_path(path);
5804 }
5805 mutex_unlock(&fs_devices->device_list_mutex);
5806
5807out:
5808 btrfs_free_path(path);
5809 return ret < 0 ? ret : 0;
5810}
5811
5812static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5813 struct btrfs_root *dev_root,
5814 struct btrfs_device *device)
5815{
5816 struct btrfs_path *path;
5817 struct btrfs_key key;
5818 struct extent_buffer *eb;
5819 struct btrfs_dev_stats_item *ptr;
5820 int ret;
5821 int i;
5822
5823 key.objectid = 0;
5824 key.type = BTRFS_DEV_STATS_KEY;
5825 key.offset = device->devid;
5826
5827 path = btrfs_alloc_path();
5828 BUG_ON(!path);
5829 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5830 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005831 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5832 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005833 goto out;
5834 }
5835
5836 if (ret == 0 &&
5837 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5838 /* need to delete old one and insert a new one */
5839 ret = btrfs_del_item(trans, dev_root, path);
5840 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005841 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5842 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005843 goto out;
5844 }
5845 ret = 1;
5846 }
5847
5848 if (ret == 1) {
5849 /* need to insert a new item */
5850 btrfs_release_path(path);
5851 ret = btrfs_insert_empty_item(trans, dev_root, path,
5852 &key, sizeof(*ptr));
5853 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005854 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5855 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005856 goto out;
5857 }
5858 }
5859
5860 eb = path->nodes[0];
5861 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5862 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5863 btrfs_set_dev_stats_value(eb, ptr, i,
5864 btrfs_dev_stat_read(device, i));
5865 btrfs_mark_buffer_dirty(eb);
5866
5867out:
5868 btrfs_free_path(path);
5869 return ret;
5870}
5871
5872/*
5873 * called from commit_transaction. Writes all changed device stats to disk.
5874 */
5875int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5876 struct btrfs_fs_info *fs_info)
5877{
5878 struct btrfs_root *dev_root = fs_info->dev_root;
5879 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5880 struct btrfs_device *device;
5881 int ret = 0;
5882
5883 mutex_lock(&fs_devices->device_list_mutex);
5884 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5885 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5886 continue;
5887
5888 ret = update_dev_stat_item(trans, dev_root, device);
5889 if (!ret)
5890 device->dev_stats_dirty = 0;
5891 }
5892 mutex_unlock(&fs_devices->device_list_mutex);
5893
5894 return ret;
5895}
5896
Stefan Behrens442a4f62012-05-25 16:06:08 +02005897void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5898{
5899 btrfs_dev_stat_inc(dev, index);
5900 btrfs_dev_stat_print_on_error(dev);
5901}
5902
5903void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5904{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005905 if (!dev->dev_stats_valid)
5906 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005907 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005908 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005909 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005910 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5911 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5912 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5913 btrfs_dev_stat_read(dev,
5914 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5915 btrfs_dev_stat_read(dev,
5916 BTRFS_DEV_STAT_GENERATION_ERRS));
5917}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005918
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005919static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5920{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005921 int i;
5922
5923 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5924 if (btrfs_dev_stat_read(dev, i) != 0)
5925 break;
5926 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5927 return; /* all values == 0, suppress message */
5928
Josef Bacik606686e2012-06-04 14:03:51 -04005929 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5930 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005931 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5932 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5933 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5934 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5935 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5936}
5937
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005938int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005939 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005940{
5941 struct btrfs_device *dev;
5942 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5943 int i;
5944
5945 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005946 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005947 mutex_unlock(&fs_devices->device_list_mutex);
5948
5949 if (!dev) {
5950 printk(KERN_WARNING
5951 "btrfs: get dev_stats failed, device not found\n");
5952 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005953 } else if (!dev->dev_stats_valid) {
5954 printk(KERN_WARNING
5955 "btrfs: get dev_stats failed, not yet valid\n");
5956 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005957 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005958 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5959 if (stats->nr_items > i)
5960 stats->values[i] =
5961 btrfs_dev_stat_read_and_reset(dev, i);
5962 else
5963 btrfs_dev_stat_reset(dev, i);
5964 }
5965 } else {
5966 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5967 if (stats->nr_items > i)
5968 stats->values[i] = btrfs_dev_stat_read(dev, i);
5969 }
5970 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5971 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5972 return 0;
5973}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01005974
5975int btrfs_scratch_superblock(struct btrfs_device *device)
5976{
5977 struct buffer_head *bh;
5978 struct btrfs_super_block *disk_super;
5979
5980 bh = btrfs_read_dev_super(device->bdev);
5981 if (!bh)
5982 return -EINVAL;
5983 disk_super = (struct btrfs_super_block *)bh->b_data;
5984
5985 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5986 set_buffer_dirty(bh);
5987 sync_dirty_buffer(bh);
5988 brelse(bh);
5989
5990 return 0;
5991}