blob: 76ded9eb77a7ba7ae467ca3911ddbc9d5e1857a8 [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 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000687 /*
688 * Wait for rcu kworkers under __btrfs_close_devices
689 * to finish all blkdev_puts so device is really
690 * free when umount is done.
691 */
692 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500693 return ret;
694}
695
Yan Zhenge4404d62008-12-12 10:03:26 -0500696static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
697 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400698{
Josef Bacikd5e20032011-08-04 14:52:27 +0000699 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400700 struct block_device *bdev;
701 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400702 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400703 struct block_device *latest_bdev = NULL;
704 struct buffer_head *bh;
705 struct btrfs_super_block *disk_super;
706 u64 latest_devid = 0;
707 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400708 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500709 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400710 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400711
Tejun Heod4d77622010-11-13 11:55:18 +0100712 flags |= FMODE_EXCL;
713
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500714 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400715 if (device->bdev)
716 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400717 if (!device->name)
718 continue;
719
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000720 /* Just open everything we can; ignore failures here */
721 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
722 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100723 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400724
725 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000726 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400727 if (devid != device->devid)
728 goto error_brelse;
729
Yan Zheng2b820322008-11-17 21:11:30 -0500730 if (memcmp(device->uuid, disk_super->dev_item.uuid,
731 BTRFS_UUID_SIZE))
732 goto error_brelse;
733
734 device->generation = btrfs_super_generation(disk_super);
735 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400736 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500737 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400738 latest_bdev = bdev;
739 }
740
Yan Zheng2b820322008-11-17 21:11:30 -0500741 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
742 device->writeable = 0;
743 } else {
744 device->writeable = !bdev_read_only(bdev);
745 seeding = 0;
746 }
747
Josef Bacikd5e20032011-08-04 14:52:27 +0000748 q = bdev_get_queue(bdev);
749 if (blk_queue_discard(q)) {
750 device->can_discard = 1;
751 fs_devices->num_can_discard++;
752 }
753
Chris Mason8a4b83c2008-03-24 15:02:07 -0400754 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400755 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500756 device->mode = flags;
757
Chris Masonc289811c2009-06-10 09:51:32 -0400758 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
759 fs_devices->rotating = 1;
760
Chris Masona0af4692008-05-13 16:03:06 -0400761 fs_devices->open_devices++;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100762 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500763 fs_devices->rw_devices++;
764 list_add(&device->dev_alloc_list,
765 &fs_devices->alloc_list);
766 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000767 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400768 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400769
Chris Masona0af4692008-05-13 16:03:06 -0400770error_brelse:
771 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100772 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400773 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400774 }
Chris Masona0af4692008-05-13 16:03:06 -0400775 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300776 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400777 goto out;
778 }
Yan Zheng2b820322008-11-17 21:11:30 -0500779 fs_devices->seeding = seeding;
780 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400781 fs_devices->latest_bdev = latest_bdev;
782 fs_devices->latest_devid = latest_devid;
783 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500784 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400785out:
Yan Zheng2b820322008-11-17 21:11:30 -0500786 return ret;
787}
788
789int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500790 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500791{
792 int ret;
793
794 mutex_lock(&uuid_mutex);
795 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500796 fs_devices->opened++;
797 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500798 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500799 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500800 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400801 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400802 return ret;
803}
804
David Sterba6f60cbd2013-02-15 11:31:02 -0700805/*
806 * Look for a btrfs signature on a device. This may be called out of the mount path
807 * and we are not allowed to call set_blocksize during the scan. The superblock
808 * is read via pagecache
809 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500810int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400811 struct btrfs_fs_devices **fs_devices_ret)
812{
813 struct btrfs_super_block *disk_super;
814 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700815 struct page *page;
816 void *p;
817 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400818 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400819 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400820 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700821 u64 bytenr;
822 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400823
David Sterba6f60cbd2013-02-15 11:31:02 -0700824 /*
825 * we would like to check all the supers, but that would make
826 * a btrfs mount succeed after a mkfs from a different FS.
827 * So, we need to add a special mount option to scan for
828 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
829 */
830 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100831 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500832 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700833
834 bdev = blkdev_get_by_path(path, flags, holder);
835
836 if (IS_ERR(bdev)) {
837 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100838 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700839 }
840
841 /* make sure our super fits in the device */
842 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
843 goto error_bdev_put;
844
845 /* make sure our super fits in the page */
846 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
847 goto error_bdev_put;
848
849 /* make sure our super doesn't straddle pages on disk */
850 index = bytenr >> PAGE_CACHE_SHIFT;
851 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
852 goto error_bdev_put;
853
854 /* pull in the page with our super */
855 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
856 index, GFP_NOFS);
857
858 if (IS_ERR_OR_NULL(page))
859 goto error_bdev_put;
860
861 p = kmap(page);
862
863 /* align our pointer to the offset of the super block */
864 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
865
866 if (btrfs_super_bytenr(disk_super) != bytenr ||
Zach Browncdb4c572013-02-20 00:55:13 +0000867 disk_super->magic != cpu_to_le64(BTRFS_MAGIC))
David Sterba6f60cbd2013-02-15 11:31:02 -0700868 goto error_unmap;
869
Xiao Guangronga3438322010-01-06 11:48:18 +0000870 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400871 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400872 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700873
Stefan Behrensd03f9182012-11-05 13:10:49 +0000874 if (disk_super->label[0]) {
875 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
876 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Chris Masond3977122009-01-05 21:25:51 -0500877 printk(KERN_INFO "device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000878 } else {
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200879 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000880 }
David Sterba6f60cbd2013-02-15 11:31:02 -0700881
Roland Dreier119e10c2009-01-21 10:49:16 -0500882 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500883 (unsigned long long)devid, (unsigned long long)transid, path);
David Sterba6f60cbd2013-02-15 11:31:02 -0700884
Chris Mason8a4b83c2008-03-24 15:02:07 -0400885 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400886 if (!ret && fs_devices_ret)
887 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700888
889error_unmap:
890 kunmap(page);
891 page_cache_release(page);
892
893error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100894 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400895error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100896 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400897 return ret;
898}
Chris Mason0b86a832008-03-24 15:01:56 -0400899
Miao Xie6d07bce2011-01-05 10:07:31 +0000900/* helper to account the used device space in the range */
901int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
902 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400903{
904 struct btrfs_key key;
905 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000906 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500907 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000908 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400909 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000910 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400911 struct extent_buffer *l;
912
Miao Xie6d07bce2011-01-05 10:07:31 +0000913 *length = 0;
914
Stefan Behrens63a212a2012-11-05 18:29:28 +0100915 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000916 return 0;
917
Yan Zheng2b820322008-11-17 21:11:30 -0500918 path = btrfs_alloc_path();
919 if (!path)
920 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400921 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400922
Chris Mason0b86a832008-03-24 15:01:56 -0400923 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000924 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400925 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000926
927 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400928 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000929 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400930 if (ret > 0) {
931 ret = btrfs_previous_item(root, path, key.objectid, key.type);
932 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000933 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400934 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000935
Chris Mason0b86a832008-03-24 15:01:56 -0400936 while (1) {
937 l = path->nodes[0];
938 slot = path->slots[0];
939 if (slot >= btrfs_header_nritems(l)) {
940 ret = btrfs_next_leaf(root, path);
941 if (ret == 0)
942 continue;
943 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000944 goto out;
945
946 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400947 }
948 btrfs_item_key_to_cpu(l, &key, slot);
949
950 if (key.objectid < device->devid)
951 goto next;
952
953 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000954 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400955
Chris Masond3977122009-01-05 21:25:51 -0500956 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400957 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400958
Chris Mason0b86a832008-03-24 15:01:56 -0400959 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000960 extent_end = key.offset + btrfs_dev_extent_length(l,
961 dev_extent);
962 if (key.offset <= start && extent_end > end) {
963 *length = end - start + 1;
964 break;
965 } else if (key.offset <= start && extent_end > start)
966 *length += extent_end - start;
967 else if (key.offset > start && extent_end <= end)
968 *length += extent_end - key.offset;
969 else if (key.offset > start && key.offset <= end) {
970 *length += end - key.offset + 1;
971 break;
972 } else if (key.offset > end)
973 break;
974
975next:
976 path->slots[0]++;
977 }
978 ret = 0;
979out:
980 btrfs_free_path(path);
981 return ret;
982}
983
Chris Mason0b86a832008-03-24 15:01:56 -0400984/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000985 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000986 * @device: the device which we search the free space in
987 * @num_bytes: the size of the free space that we need
988 * @start: store the start of the free space.
989 * @len: the size of the free space. that we find, or the size of the max
990 * free space if we don't find suitable free space
991 *
Chris Mason0b86a832008-03-24 15:01:56 -0400992 * this uses a pretty simple search, the expectation is that it is
993 * called very infrequently and that a given device has a small number
994 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000995 *
996 * @start is used to store the start of the free space if we find. But if we
997 * don't find suitable free space, it will be used to store the start position
998 * of the max free space.
999 *
1000 * @len is used to store the size of the free space that we find.
1001 * But if we don't find suitable free space, it is used to store the size of
1002 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001003 */
Li Zefan125ccb02011-12-08 15:07:24 +08001004int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001005 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001006{
1007 struct btrfs_key key;
1008 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001009 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001010 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001011 u64 hole_size;
1012 u64 max_hole_start;
1013 u64 max_hole_size;
1014 u64 extent_end;
1015 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001016 u64 search_end = device->total_bytes;
1017 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001018 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001019 struct extent_buffer *l;
1020
Chris Mason0b86a832008-03-24 15:01:56 -04001021 /* FIXME use last free of some kind */
1022
1023 /* we don't want to overwrite the superblock on the drive,
1024 * so we make sure to start at an offset of at least 1MB
1025 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001026 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001027
Miao Xie7bfc8372011-01-05 10:07:26 +00001028 max_hole_start = search_start;
1029 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001030 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001031
Stefan Behrens63a212a2012-11-05 18:29:28 +01001032 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001033 ret = -ENOSPC;
1034 goto error;
1035 }
1036
1037 path = btrfs_alloc_path();
1038 if (!path) {
1039 ret = -ENOMEM;
1040 goto error;
1041 }
1042 path->reada = 2;
1043
Chris Mason0b86a832008-03-24 15:01:56 -04001044 key.objectid = device->devid;
1045 key.offset = search_start;
1046 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001047
Li Zefan125ccb02011-12-08 15:07:24 +08001048 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001049 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001050 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001051 if (ret > 0) {
1052 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1053 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001054 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001055 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001056
Chris Mason0b86a832008-03-24 15:01:56 -04001057 while (1) {
1058 l = path->nodes[0];
1059 slot = path->slots[0];
1060 if (slot >= btrfs_header_nritems(l)) {
1061 ret = btrfs_next_leaf(root, path);
1062 if (ret == 0)
1063 continue;
1064 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001065 goto out;
1066
1067 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001068 }
1069 btrfs_item_key_to_cpu(l, &key, slot);
1070
1071 if (key.objectid < device->devid)
1072 goto next;
1073
1074 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001075 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001076
Chris Mason0b86a832008-03-24 15:01:56 -04001077 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1078 goto next;
1079
Miao Xie7bfc8372011-01-05 10:07:26 +00001080 if (key.offset > search_start) {
1081 hole_size = key.offset - search_start;
1082
1083 if (hole_size > max_hole_size) {
1084 max_hole_start = search_start;
1085 max_hole_size = hole_size;
1086 }
1087
1088 /*
1089 * If this free space is greater than which we need,
1090 * it must be the max free space that we have found
1091 * until now, so max_hole_start must point to the start
1092 * of this free space and the length of this free space
1093 * is stored in max_hole_size. Thus, we return
1094 * max_hole_start and max_hole_size and go back to the
1095 * caller.
1096 */
1097 if (hole_size >= num_bytes) {
1098 ret = 0;
1099 goto out;
1100 }
1101 }
1102
Chris Mason0b86a832008-03-24 15:01:56 -04001103 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001104 extent_end = key.offset + btrfs_dev_extent_length(l,
1105 dev_extent);
1106 if (extent_end > search_start)
1107 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001108next:
1109 path->slots[0]++;
1110 cond_resched();
1111 }
Chris Mason0b86a832008-03-24 15:01:56 -04001112
liubo38c01b92011-08-02 02:39:03 +00001113 /*
1114 * At this point, search_start should be the end of
1115 * allocated dev extents, and when shrinking the device,
1116 * search_end may be smaller than search_start.
1117 */
1118 if (search_end > search_start)
1119 hole_size = search_end - search_start;
1120
Miao Xie7bfc8372011-01-05 10:07:26 +00001121 if (hole_size > max_hole_size) {
1122 max_hole_start = search_start;
1123 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001124 }
Chris Mason0b86a832008-03-24 15:01:56 -04001125
Miao Xie7bfc8372011-01-05 10:07:26 +00001126 /* See above. */
1127 if (hole_size < num_bytes)
1128 ret = -ENOSPC;
1129 else
1130 ret = 0;
1131
1132out:
Yan Zheng2b820322008-11-17 21:11:30 -05001133 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001134error:
1135 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001136 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001137 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001138 return ret;
1139}
1140
Christoph Hellwigb2950862008-12-02 09:54:17 -05001141static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001142 struct btrfs_device *device,
1143 u64 start)
1144{
1145 int ret;
1146 struct btrfs_path *path;
1147 struct btrfs_root *root = device->dev_root;
1148 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001149 struct btrfs_key found_key;
1150 struct extent_buffer *leaf = NULL;
1151 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001152
1153 path = btrfs_alloc_path();
1154 if (!path)
1155 return -ENOMEM;
1156
1157 key.objectid = device->devid;
1158 key.offset = start;
1159 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001160again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001161 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001162 if (ret > 0) {
1163 ret = btrfs_previous_item(root, path, key.objectid,
1164 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001165 if (ret)
1166 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001167 leaf = path->nodes[0];
1168 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1169 extent = btrfs_item_ptr(leaf, path->slots[0],
1170 struct btrfs_dev_extent);
1171 BUG_ON(found_key.offset > start || found_key.offset +
1172 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001173 key = found_key;
1174 btrfs_release_path(path);
1175 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001176 } else if (ret == 0) {
1177 leaf = path->nodes[0];
1178 extent = btrfs_item_ptr(leaf, path->slots[0],
1179 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001180 } else {
1181 btrfs_error(root->fs_info, ret, "Slot search failed");
1182 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001183 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001184
Josef Bacik2bf64752011-09-26 17:12:22 -04001185 if (device->bytes_used > 0) {
1186 u64 len = btrfs_dev_extent_length(leaf, extent);
1187 device->bytes_used -= len;
1188 spin_lock(&root->fs_info->free_chunk_lock);
1189 root->fs_info->free_chunk_space += len;
1190 spin_unlock(&root->fs_info->free_chunk_lock);
1191 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001192 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001193 if (ret) {
1194 btrfs_error(root->fs_info, ret,
1195 "Failed to remove dev extent item");
1196 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001197out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001198 btrfs_free_path(path);
1199 return ret;
1200}
1201
Yan Zheng2b820322008-11-17 21:11:30 -05001202int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001203 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001204 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001205 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001206{
1207 int ret;
1208 struct btrfs_path *path;
1209 struct btrfs_root *root = device->dev_root;
1210 struct btrfs_dev_extent *extent;
1211 struct extent_buffer *leaf;
1212 struct btrfs_key key;
1213
Chris Masondfe25022008-05-13 13:46:40 -04001214 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001215 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001216 path = btrfs_alloc_path();
1217 if (!path)
1218 return -ENOMEM;
1219
Chris Mason0b86a832008-03-24 15:01:56 -04001220 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001221 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001222 key.type = BTRFS_DEV_EXTENT_KEY;
1223 ret = btrfs_insert_empty_item(trans, root, path, &key,
1224 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001225 if (ret)
1226 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001227
1228 leaf = path->nodes[0];
1229 extent = btrfs_item_ptr(leaf, path->slots[0],
1230 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001231 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1232 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1233 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1234
1235 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1236 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1237 BTRFS_UUID_SIZE);
1238
Chris Mason0b86a832008-03-24 15:01:56 -04001239 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1240 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001241out:
Chris Mason0b86a832008-03-24 15:01:56 -04001242 btrfs_free_path(path);
1243 return ret;
1244}
1245
Chris Masona1b32a52008-09-05 16:09:51 -04001246static noinline int find_next_chunk(struct btrfs_root *root,
1247 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001248{
1249 struct btrfs_path *path;
1250 int ret;
1251 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001252 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001253 struct btrfs_key found_key;
1254
1255 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001256 if (!path)
1257 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001258
Chris Masone17cade2008-04-15 15:41:47 -04001259 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001260 key.offset = (u64)-1;
1261 key.type = BTRFS_CHUNK_ITEM_KEY;
1262
1263 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1264 if (ret < 0)
1265 goto error;
1266
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001267 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001268
1269 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1270 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001271 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001272 } else {
1273 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1274 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001275 if (found_key.objectid != objectid)
1276 *offset = 0;
1277 else {
1278 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1279 struct btrfs_chunk);
1280 *offset = found_key.offset +
1281 btrfs_chunk_length(path->nodes[0], chunk);
1282 }
Chris Mason0b86a832008-03-24 15:01:56 -04001283 }
1284 ret = 0;
1285error:
1286 btrfs_free_path(path);
1287 return ret;
1288}
1289
Yan Zheng2b820322008-11-17 21:11:30 -05001290static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001291{
1292 int ret;
1293 struct btrfs_key key;
1294 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001295 struct btrfs_path *path;
1296
1297 root = root->fs_info->chunk_root;
1298
1299 path = btrfs_alloc_path();
1300 if (!path)
1301 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001302
1303 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1304 key.type = BTRFS_DEV_ITEM_KEY;
1305 key.offset = (u64)-1;
1306
1307 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1308 if (ret < 0)
1309 goto error;
1310
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001311 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001312
1313 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1314 BTRFS_DEV_ITEM_KEY);
1315 if (ret) {
1316 *objectid = 1;
1317 } else {
1318 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1319 path->slots[0]);
1320 *objectid = found_key.offset + 1;
1321 }
1322 ret = 0;
1323error:
Yan Zheng2b820322008-11-17 21:11:30 -05001324 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001325 return ret;
1326}
1327
1328/*
1329 * the device information is stored in the chunk root
1330 * the btrfs_device struct should be fully filled in
1331 */
1332int btrfs_add_device(struct btrfs_trans_handle *trans,
1333 struct btrfs_root *root,
1334 struct btrfs_device *device)
1335{
1336 int ret;
1337 struct btrfs_path *path;
1338 struct btrfs_dev_item *dev_item;
1339 struct extent_buffer *leaf;
1340 struct btrfs_key key;
1341 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001342
1343 root = root->fs_info->chunk_root;
1344
1345 path = btrfs_alloc_path();
1346 if (!path)
1347 return -ENOMEM;
1348
Chris Mason0b86a832008-03-24 15:01:56 -04001349 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1350 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001351 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001352
1353 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001354 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001355 if (ret)
1356 goto out;
1357
1358 leaf = path->nodes[0];
1359 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1360
1361 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001362 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001363 btrfs_set_device_type(leaf, dev_item, device->type);
1364 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1365 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1366 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001367 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1368 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001369 btrfs_set_device_group(leaf, dev_item, 0);
1370 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1371 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001372 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001373
Chris Mason0b86a832008-03-24 15:01:56 -04001374 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001375 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001376 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1377 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001378 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001379
Yan Zheng2b820322008-11-17 21:11:30 -05001380 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001381out:
1382 btrfs_free_path(path);
1383 return ret;
1384}
Chris Mason8f18cf12008-04-25 16:53:30 -04001385
Chris Masona061fc82008-05-07 11:43:44 -04001386static int btrfs_rm_dev_item(struct btrfs_root *root,
1387 struct btrfs_device *device)
1388{
1389 int ret;
1390 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001391 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001392 struct btrfs_trans_handle *trans;
1393
1394 root = root->fs_info->chunk_root;
1395
1396 path = btrfs_alloc_path();
1397 if (!path)
1398 return -ENOMEM;
1399
Yan, Zhenga22285a2010-05-16 10:48:46 -04001400 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001401 if (IS_ERR(trans)) {
1402 btrfs_free_path(path);
1403 return PTR_ERR(trans);
1404 }
Chris Masona061fc82008-05-07 11:43:44 -04001405 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1406 key.type = BTRFS_DEV_ITEM_KEY;
1407 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001408 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001409
1410 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1411 if (ret < 0)
1412 goto out;
1413
1414 if (ret > 0) {
1415 ret = -ENOENT;
1416 goto out;
1417 }
1418
1419 ret = btrfs_del_item(trans, root, path);
1420 if (ret)
1421 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001422out:
1423 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001424 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001425 btrfs_commit_transaction(trans, root);
1426 return ret;
1427}
1428
1429int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1430{
1431 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001432 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001433 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001434 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001435 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001436 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001437 u64 all_avail;
1438 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001439 u64 num_devices;
1440 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001441 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001442 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001443 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001444
Chris Masona061fc82008-05-07 11:43:44 -04001445 mutex_lock(&uuid_mutex);
1446
Miao Xiede98ced2013-01-29 10:13:12 +00001447 do {
1448 seq = read_seqbegin(&root->fs_info->profiles_lock);
1449
1450 all_avail = root->fs_info->avail_data_alloc_bits |
1451 root->fs_info->avail_system_alloc_bits |
1452 root->fs_info->avail_metadata_alloc_bits;
1453 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001454
Stefan Behrens8dabb742012-11-06 13:15:27 +01001455 num_devices = root->fs_info->fs_devices->num_devices;
1456 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1457 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1458 WARN_ON(num_devices < 1);
1459 num_devices--;
1460 }
1461 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1462
1463 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001464 printk(KERN_ERR "btrfs: unable to go below four devices "
1465 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001466 ret = -EINVAL;
1467 goto out;
1468 }
1469
Stefan Behrens8dabb742012-11-06 13:15:27 +01001470 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001471 printk(KERN_ERR "btrfs: unable to go below two "
1472 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001473 ret = -EINVAL;
1474 goto out;
1475 }
1476
David Woodhouse53b381b2013-01-29 18:40:14 -05001477 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1478 root->fs_info->fs_devices->rw_devices <= 2) {
1479 printk(KERN_ERR "btrfs: unable to go below two "
1480 "devices on raid5\n");
1481 ret = -EINVAL;
1482 goto out;
1483 }
1484 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1485 root->fs_info->fs_devices->rw_devices <= 3) {
1486 printk(KERN_ERR "btrfs: unable to go below three "
1487 "devices on raid6\n");
1488 ret = -EINVAL;
1489 goto out;
1490 }
1491
Chris Masondfe25022008-05-13 13:46:40 -04001492 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001493 struct list_head *devices;
1494 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001495
Chris Masondfe25022008-05-13 13:46:40 -04001496 device = NULL;
1497 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001498 /*
1499 * It is safe to read the devices since the volume_mutex
1500 * is held.
1501 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001502 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001503 if (tmp->in_fs_metadata &&
1504 !tmp->is_tgtdev_for_dev_replace &&
1505 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001506 device = tmp;
1507 break;
1508 }
1509 }
1510 bdev = NULL;
1511 bh = NULL;
1512 disk_super = NULL;
1513 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001514 printk(KERN_ERR "btrfs: no missing devices found to "
1515 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001516 goto out;
1517 }
Chris Masondfe25022008-05-13 13:46:40 -04001518 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001519 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001520 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001521 root->fs_info->bdev_holder, 0,
1522 &bdev, &bh);
1523 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001524 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001525 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001526 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001527 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001528 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001529 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001530 if (!device) {
1531 ret = -ENOENT;
1532 goto error_brelse;
1533 }
Chris Masondfe25022008-05-13 13:46:40 -04001534 }
Yan Zheng2b820322008-11-17 21:11:30 -05001535
Stefan Behrens63a212a2012-11-05 18:29:28 +01001536 if (device->is_tgtdev_for_dev_replace) {
1537 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1538 ret = -EINVAL;
1539 goto error_brelse;
1540 }
1541
Yan Zheng2b820322008-11-17 21:11:30 -05001542 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001543 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1544 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001545 ret = -EINVAL;
1546 goto error_brelse;
1547 }
1548
1549 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001550 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001551 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001552 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001553 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001554 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001555 }
Chris Masona061fc82008-05-07 11:43:44 -04001556
1557 ret = btrfs_shrink_device(device, 0);
1558 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001559 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001560
Stefan Behrens63a212a2012-11-05 18:29:28 +01001561 /*
1562 * TODO: the superblock still includes this device in its num_devices
1563 * counter although write_all_supers() is not locked out. This
1564 * could give a filesystem state which requires a degraded mount.
1565 */
Chris Masona061fc82008-05-07 11:43:44 -04001566 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1567 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001568 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001569
Josef Bacik2bf64752011-09-26 17:12:22 -04001570 spin_lock(&root->fs_info->free_chunk_lock);
1571 root->fs_info->free_chunk_space = device->total_bytes -
1572 device->bytes_used;
1573 spin_unlock(&root->fs_info->free_chunk_lock);
1574
Yan Zheng2b820322008-11-17 21:11:30 -05001575 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001576 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001577
1578 /*
1579 * the device list mutex makes sure that we don't change
1580 * the device list while someone else is writing out all
1581 * the device supers.
1582 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001583
1584 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001585 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001586 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001587
Yan Zhenge4404d62008-12-12 10:03:26 -05001588 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001589 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001590
Chris Masoncd02dca2010-12-13 14:56:23 -05001591 if (device->missing)
1592 root->fs_info->fs_devices->missing_devices--;
1593
Yan Zheng2b820322008-11-17 21:11:30 -05001594 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1595 struct btrfs_device, dev_list);
1596 if (device->bdev == root->fs_info->sb->s_bdev)
1597 root->fs_info->sb->s_bdev = next_device->bdev;
1598 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1599 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1600
Xiao Guangrong1f781602011-04-20 10:09:16 +00001601 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001602 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001603
1604 call_rcu(&device->rcu, free_device);
1605 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001606
David Sterba6c417612011-04-13 15:41:04 +02001607 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1608 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001609
Xiao Guangrong1f781602011-04-20 10:09:16 +00001610 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001611 struct btrfs_fs_devices *fs_devices;
1612 fs_devices = root->fs_info->fs_devices;
1613 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001614 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001615 break;
1616 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001617 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001618 fs_devices->seed = cur_devices->seed;
1619 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001620 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001621 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001622 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001623 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001624 }
1625
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001626 root->fs_info->num_tolerated_disk_barrier_failures =
1627 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1628
Yan Zheng2b820322008-11-17 21:11:30 -05001629 /*
1630 * at this point, the device is zero sized. We want to
1631 * remove it from the devices list and zero out the old super
1632 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001633 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001634 /* make sure this device isn't detected as part of
1635 * the FS anymore
1636 */
1637 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1638 set_buffer_dirty(bh);
1639 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001640 }
Chris Masona061fc82008-05-07 11:43:44 -04001641
Chris Masona061fc82008-05-07 11:43:44 -04001642 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001643
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001644 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001645 if (bdev)
1646 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001647
Chris Masona061fc82008-05-07 11:43:44 -04001648error_brelse:
1649 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001650 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001651 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001652out:
1653 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001654 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001655error_undo:
1656 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001657 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001658 list_add(&device->dev_alloc_list,
1659 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001660 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001661 root->fs_info->fs_devices->rw_devices++;
1662 }
1663 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001664}
1665
Stefan Behrense93c89c2012-11-05 17:33:06 +01001666void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1667 struct btrfs_device *srcdev)
1668{
1669 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1670 list_del_rcu(&srcdev->dev_list);
1671 list_del_rcu(&srcdev->dev_alloc_list);
1672 fs_info->fs_devices->num_devices--;
1673 if (srcdev->missing) {
1674 fs_info->fs_devices->missing_devices--;
1675 fs_info->fs_devices->rw_devices++;
1676 }
1677 if (srcdev->can_discard)
1678 fs_info->fs_devices->num_can_discard--;
1679 if (srcdev->bdev)
1680 fs_info->fs_devices->open_devices--;
1681
1682 call_rcu(&srcdev->rcu, free_device);
1683}
1684
1685void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1686 struct btrfs_device *tgtdev)
1687{
1688 struct btrfs_device *next_device;
1689
1690 WARN_ON(!tgtdev);
1691 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1692 if (tgtdev->bdev) {
1693 btrfs_scratch_superblock(tgtdev);
1694 fs_info->fs_devices->open_devices--;
1695 }
1696 fs_info->fs_devices->num_devices--;
1697 if (tgtdev->can_discard)
1698 fs_info->fs_devices->num_can_discard++;
1699
1700 next_device = list_entry(fs_info->fs_devices->devices.next,
1701 struct btrfs_device, dev_list);
1702 if (tgtdev->bdev == fs_info->sb->s_bdev)
1703 fs_info->sb->s_bdev = next_device->bdev;
1704 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1705 fs_info->fs_devices->latest_bdev = next_device->bdev;
1706 list_del_rcu(&tgtdev->dev_list);
1707
1708 call_rcu(&tgtdev->rcu, free_device);
1709
1710 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1711}
1712
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001713int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1714 struct btrfs_device **device)
1715{
1716 int ret = 0;
1717 struct btrfs_super_block *disk_super;
1718 u64 devid;
1719 u8 *dev_uuid;
1720 struct block_device *bdev;
1721 struct buffer_head *bh;
1722
1723 *device = NULL;
1724 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1725 root->fs_info->bdev_holder, 0, &bdev, &bh);
1726 if (ret)
1727 return ret;
1728 disk_super = (struct btrfs_super_block *)bh->b_data;
1729 devid = btrfs_stack_device_id(&disk_super->dev_item);
1730 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001731 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001732 disk_super->fsid);
1733 brelse(bh);
1734 if (!*device)
1735 ret = -ENOENT;
1736 blkdev_put(bdev, FMODE_READ);
1737 return ret;
1738}
1739
1740int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1741 char *device_path,
1742 struct btrfs_device **device)
1743{
1744 *device = NULL;
1745 if (strcmp(device_path, "missing") == 0) {
1746 struct list_head *devices;
1747 struct btrfs_device *tmp;
1748
1749 devices = &root->fs_info->fs_devices->devices;
1750 /*
1751 * It is safe to read the devices since the volume_mutex
1752 * is held by the caller.
1753 */
1754 list_for_each_entry(tmp, devices, dev_list) {
1755 if (tmp->in_fs_metadata && !tmp->bdev) {
1756 *device = tmp;
1757 break;
1758 }
1759 }
1760
1761 if (!*device) {
1762 pr_err("btrfs: no missing device found\n");
1763 return -ENOENT;
1764 }
1765
1766 return 0;
1767 } else {
1768 return btrfs_find_device_by_path(root, device_path, device);
1769 }
1770}
1771
Yan Zheng2b820322008-11-17 21:11:30 -05001772/*
1773 * does all the dirty work required for changing file system's UUID.
1774 */
Li Zefan125ccb02011-12-08 15:07:24 +08001775static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001776{
1777 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1778 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001779 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001780 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001781 struct btrfs_device *device;
1782 u64 super_flags;
1783
1784 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001785 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001786 return -EINVAL;
1787
Yan Zhenge4404d62008-12-12 10:03:26 -05001788 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1789 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001790 return -ENOMEM;
1791
Yan Zhenge4404d62008-12-12 10:03:26 -05001792 old_devices = clone_fs_devices(fs_devices);
1793 if (IS_ERR(old_devices)) {
1794 kfree(seed_devices);
1795 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001796 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001797
Yan Zheng2b820322008-11-17 21:11:30 -05001798 list_add(&old_devices->list, &fs_uuids);
1799
Yan Zhenge4404d62008-12-12 10:03:26 -05001800 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1801 seed_devices->opened = 1;
1802 INIT_LIST_HEAD(&seed_devices->devices);
1803 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001804 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001805
1806 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001807 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1808 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001809 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1810
Yan Zhenge4404d62008-12-12 10:03:26 -05001811 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1812 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1813 device->fs_devices = seed_devices;
1814 }
1815
Yan Zheng2b820322008-11-17 21:11:30 -05001816 fs_devices->seeding = 0;
1817 fs_devices->num_devices = 0;
1818 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001819 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001820 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001821
1822 generate_random_uuid(fs_devices->fsid);
1823 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1824 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1825 super_flags = btrfs_super_flags(disk_super) &
1826 ~BTRFS_SUPER_FLAG_SEEDING;
1827 btrfs_set_super_flags(disk_super, super_flags);
1828
1829 return 0;
1830}
1831
1832/*
1833 * strore the expected generation for seed devices in device items.
1834 */
1835static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1836 struct btrfs_root *root)
1837{
1838 struct btrfs_path *path;
1839 struct extent_buffer *leaf;
1840 struct btrfs_dev_item *dev_item;
1841 struct btrfs_device *device;
1842 struct btrfs_key key;
1843 u8 fs_uuid[BTRFS_UUID_SIZE];
1844 u8 dev_uuid[BTRFS_UUID_SIZE];
1845 u64 devid;
1846 int ret;
1847
1848 path = btrfs_alloc_path();
1849 if (!path)
1850 return -ENOMEM;
1851
1852 root = root->fs_info->chunk_root;
1853 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1854 key.offset = 0;
1855 key.type = BTRFS_DEV_ITEM_KEY;
1856
1857 while (1) {
1858 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1859 if (ret < 0)
1860 goto error;
1861
1862 leaf = path->nodes[0];
1863next_slot:
1864 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1865 ret = btrfs_next_leaf(root, path);
1866 if (ret > 0)
1867 break;
1868 if (ret < 0)
1869 goto error;
1870 leaf = path->nodes[0];
1871 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001872 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001873 continue;
1874 }
1875
1876 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1877 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1878 key.type != BTRFS_DEV_ITEM_KEY)
1879 break;
1880
1881 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1882 struct btrfs_dev_item);
1883 devid = btrfs_device_id(leaf, dev_item);
1884 read_extent_buffer(leaf, dev_uuid,
1885 (unsigned long)btrfs_device_uuid(dev_item),
1886 BTRFS_UUID_SIZE);
1887 read_extent_buffer(leaf, fs_uuid,
1888 (unsigned long)btrfs_device_fsid(dev_item),
1889 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001890 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1891 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001892 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001893
1894 if (device->fs_devices->seeding) {
1895 btrfs_set_device_generation(leaf, dev_item,
1896 device->generation);
1897 btrfs_mark_buffer_dirty(leaf);
1898 }
1899
1900 path->slots[0]++;
1901 goto next_slot;
1902 }
1903 ret = 0;
1904error:
1905 btrfs_free_path(path);
1906 return ret;
1907}
1908
Chris Mason788f20e2008-04-28 15:29:42 -04001909int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1910{
Josef Bacikd5e20032011-08-04 14:52:27 +00001911 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001912 struct btrfs_trans_handle *trans;
1913 struct btrfs_device *device;
1914 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001915 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001916 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001917 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001918 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001919 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001920 int ret = 0;
1921
Yan Zheng2b820322008-11-17 21:11:30 -05001922 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001923 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001924
Li Zefana5d16332011-12-07 20:08:40 -05001925 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001926 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001927 if (IS_ERR(bdev))
1928 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001929
Yan Zheng2b820322008-11-17 21:11:30 -05001930 if (root->fs_info->fs_devices->seeding) {
1931 seeding_dev = 1;
1932 down_write(&sb->s_umount);
1933 mutex_lock(&uuid_mutex);
1934 }
1935
Chris Mason8c8bee12008-09-29 11:19:10 -04001936 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001937
Chris Mason788f20e2008-04-28 15:29:42 -04001938 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001939
1940 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001941 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001942 if (device->bdev == bdev) {
1943 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001944 mutex_unlock(
1945 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001946 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001947 }
1948 }
Liu Bod25628b2012-11-14 14:35:30 +00001949 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001950
1951 device = kzalloc(sizeof(*device), GFP_NOFS);
1952 if (!device) {
1953 /* we can safely leave the fs_devices entry around */
1954 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001955 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001956 }
1957
Josef Bacik606686e2012-06-04 14:03:51 -04001958 name = rcu_string_strdup(device_path, GFP_NOFS);
1959 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001960 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001961 ret = -ENOMEM;
1962 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001963 }
Josef Bacik606686e2012-06-04 14:03:51 -04001964 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001965
1966 ret = find_next_devid(root, &device->devid);
1967 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001968 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001969 kfree(device);
1970 goto error;
1971 }
1972
Yan, Zhenga22285a2010-05-16 10:48:46 -04001973 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001974 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001975 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001976 kfree(device);
1977 ret = PTR_ERR(trans);
1978 goto error;
1979 }
1980
Yan Zheng2b820322008-11-17 21:11:30 -05001981 lock_chunks(root);
1982
Josef Bacikd5e20032011-08-04 14:52:27 +00001983 q = bdev_get_queue(bdev);
1984 if (blk_queue_discard(q))
1985 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001986 device->writeable = 1;
1987 device->work.func = pending_bios_fn;
1988 generate_random_uuid(device->uuid);
1989 spin_lock_init(&device->io_lock);
1990 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001991 device->io_width = root->sectorsize;
1992 device->io_align = root->sectorsize;
1993 device->sector_size = root->sectorsize;
1994 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001995 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001996 device->dev_root = root->fs_info->dev_root;
1997 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001998 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001999 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002000 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002001 set_blocksize(device->bdev, 4096);
2002
Yan Zheng2b820322008-11-17 21:11:30 -05002003 if (seeding_dev) {
2004 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002005 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002006 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002007 }
2008
2009 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002010
Chris Masone5e9a522009-06-10 15:17:02 -04002011 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002012 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002013 list_add(&device->dev_alloc_list,
2014 &root->fs_info->fs_devices->alloc_list);
2015 root->fs_info->fs_devices->num_devices++;
2016 root->fs_info->fs_devices->open_devices++;
2017 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002018 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002019 if (device->can_discard)
2020 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002021 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2022
Josef Bacik2bf64752011-09-26 17:12:22 -04002023 spin_lock(&root->fs_info->free_chunk_lock);
2024 root->fs_info->free_chunk_space += device->total_bytes;
2025 spin_unlock(&root->fs_info->free_chunk_lock);
2026
Chris Masonc289811c2009-06-10 09:51:32 -04002027 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2028 root->fs_info->fs_devices->rotating = 1;
2029
David Sterba6c417612011-04-13 15:41:04 +02002030 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2031 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002032 total_bytes + device->total_bytes);
2033
David Sterba6c417612011-04-13 15:41:04 +02002034 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2035 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002036 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04002037 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002038
Yan Zheng2b820322008-11-17 21:11:30 -05002039 if (seeding_dev) {
2040 ret = init_first_rw_device(trans, root, device);
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 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002046 if (ret) {
2047 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002048 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002049 }
Yan Zheng2b820322008-11-17 21:11:30 -05002050 } else {
2051 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002052 if (ret) {
2053 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002054 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002055 }
Yan Zheng2b820322008-11-17 21:11:30 -05002056 }
2057
Chris Mason913d9522009-03-10 13:17:18 -04002058 /*
2059 * we've got more storage, clear any full flags on the space
2060 * infos
2061 */
2062 btrfs_clear_space_info_full(root->fs_info);
2063
Chris Mason7d9eb122008-07-08 14:19:17 -04002064 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002065 root->fs_info->num_tolerated_disk_barrier_failures =
2066 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002067 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002068
2069 if (seeding_dev) {
2070 mutex_unlock(&uuid_mutex);
2071 up_write(&sb->s_umount);
2072
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002073 if (ret) /* transaction commit */
2074 return ret;
2075
Yan Zheng2b820322008-11-17 21:11:30 -05002076 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002077 if (ret < 0)
2078 btrfs_error(root->fs_info, ret,
2079 "Failed to relocate sys chunks after "
2080 "device initialization. This can be fixed "
2081 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002082 trans = btrfs_attach_transaction(root);
2083 if (IS_ERR(trans)) {
2084 if (PTR_ERR(trans) == -ENOENT)
2085 return 0;
2086 return PTR_ERR(trans);
2087 }
2088 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002089 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002090
Chris Mason788f20e2008-04-28 15:29:42 -04002091 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002092
2093error_trans:
2094 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002095 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002096 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002097 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002098error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002099 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002100 if (seeding_dev) {
2101 mutex_unlock(&uuid_mutex);
2102 up_write(&sb->s_umount);
2103 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002104 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002105}
2106
Stefan Behrense93c89c2012-11-05 17:33:06 +01002107int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2108 struct btrfs_device **device_out)
2109{
2110 struct request_queue *q;
2111 struct btrfs_device *device;
2112 struct block_device *bdev;
2113 struct btrfs_fs_info *fs_info = root->fs_info;
2114 struct list_head *devices;
2115 struct rcu_string *name;
2116 int ret = 0;
2117
2118 *device_out = NULL;
2119 if (fs_info->fs_devices->seeding)
2120 return -EINVAL;
2121
2122 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2123 fs_info->bdev_holder);
2124 if (IS_ERR(bdev))
2125 return PTR_ERR(bdev);
2126
2127 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2128
2129 devices = &fs_info->fs_devices->devices;
2130 list_for_each_entry(device, devices, dev_list) {
2131 if (device->bdev == bdev) {
2132 ret = -EEXIST;
2133 goto error;
2134 }
2135 }
2136
2137 device = kzalloc(sizeof(*device), GFP_NOFS);
2138 if (!device) {
2139 ret = -ENOMEM;
2140 goto error;
2141 }
2142
2143 name = rcu_string_strdup(device_path, GFP_NOFS);
2144 if (!name) {
2145 kfree(device);
2146 ret = -ENOMEM;
2147 goto error;
2148 }
2149 rcu_assign_pointer(device->name, name);
2150
2151 q = bdev_get_queue(bdev);
2152 if (blk_queue_discard(q))
2153 device->can_discard = 1;
2154 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2155 device->writeable = 1;
2156 device->work.func = pending_bios_fn;
2157 generate_random_uuid(device->uuid);
2158 device->devid = BTRFS_DEV_REPLACE_DEVID;
2159 spin_lock_init(&device->io_lock);
2160 device->generation = 0;
2161 device->io_width = root->sectorsize;
2162 device->io_align = root->sectorsize;
2163 device->sector_size = root->sectorsize;
2164 device->total_bytes = i_size_read(bdev->bd_inode);
2165 device->disk_total_bytes = device->total_bytes;
2166 device->dev_root = fs_info->dev_root;
2167 device->bdev = bdev;
2168 device->in_fs_metadata = 1;
2169 device->is_tgtdev_for_dev_replace = 1;
2170 device->mode = FMODE_EXCL;
2171 set_blocksize(device->bdev, 4096);
2172 device->fs_devices = fs_info->fs_devices;
2173 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2174 fs_info->fs_devices->num_devices++;
2175 fs_info->fs_devices->open_devices++;
2176 if (device->can_discard)
2177 fs_info->fs_devices->num_can_discard++;
2178 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2179
2180 *device_out = device;
2181 return ret;
2182
2183error:
2184 blkdev_put(bdev, FMODE_EXCL);
2185 return ret;
2186}
2187
2188void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2189 struct btrfs_device *tgtdev)
2190{
2191 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2192 tgtdev->io_width = fs_info->dev_root->sectorsize;
2193 tgtdev->io_align = fs_info->dev_root->sectorsize;
2194 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2195 tgtdev->dev_root = fs_info->dev_root;
2196 tgtdev->in_fs_metadata = 1;
2197}
2198
Chris Masond3977122009-01-05 21:25:51 -05002199static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2200 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002201{
2202 int ret;
2203 struct btrfs_path *path;
2204 struct btrfs_root *root;
2205 struct btrfs_dev_item *dev_item;
2206 struct extent_buffer *leaf;
2207 struct btrfs_key key;
2208
2209 root = device->dev_root->fs_info->chunk_root;
2210
2211 path = btrfs_alloc_path();
2212 if (!path)
2213 return -ENOMEM;
2214
2215 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2216 key.type = BTRFS_DEV_ITEM_KEY;
2217 key.offset = device->devid;
2218
2219 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2220 if (ret < 0)
2221 goto out;
2222
2223 if (ret > 0) {
2224 ret = -ENOENT;
2225 goto out;
2226 }
2227
2228 leaf = path->nodes[0];
2229 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2230
2231 btrfs_set_device_id(leaf, dev_item, device->devid);
2232 btrfs_set_device_type(leaf, dev_item, device->type);
2233 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2234 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2235 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002236 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002237 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2238 btrfs_mark_buffer_dirty(leaf);
2239
2240out:
2241 btrfs_free_path(path);
2242 return ret;
2243}
2244
Chris Mason7d9eb122008-07-08 14:19:17 -04002245static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002246 struct btrfs_device *device, u64 new_size)
2247{
2248 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002249 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002250 u64 old_total = btrfs_super_total_bytes(super_copy);
2251 u64 diff = new_size - device->total_bytes;
2252
Yan Zheng2b820322008-11-17 21:11:30 -05002253 if (!device->writeable)
2254 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002255 if (new_size <= device->total_bytes ||
2256 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002257 return -EINVAL;
2258
Chris Mason8f18cf12008-04-25 16:53:30 -04002259 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002260 device->fs_devices->total_rw_bytes += diff;
2261
2262 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002263 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002264 btrfs_clear_space_info_full(device->dev_root->fs_info);
2265
Chris Mason8f18cf12008-04-25 16:53:30 -04002266 return btrfs_update_device(trans, device);
2267}
2268
Chris Mason7d9eb122008-07-08 14:19:17 -04002269int btrfs_grow_device(struct btrfs_trans_handle *trans,
2270 struct btrfs_device *device, u64 new_size)
2271{
2272 int ret;
2273 lock_chunks(device->dev_root);
2274 ret = __btrfs_grow_device(trans, device, new_size);
2275 unlock_chunks(device->dev_root);
2276 return ret;
2277}
2278
Chris Mason8f18cf12008-04-25 16:53:30 -04002279static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2280 struct btrfs_root *root,
2281 u64 chunk_tree, u64 chunk_objectid,
2282 u64 chunk_offset)
2283{
2284 int ret;
2285 struct btrfs_path *path;
2286 struct btrfs_key key;
2287
2288 root = root->fs_info->chunk_root;
2289 path = btrfs_alloc_path();
2290 if (!path)
2291 return -ENOMEM;
2292
2293 key.objectid = chunk_objectid;
2294 key.offset = chunk_offset;
2295 key.type = BTRFS_CHUNK_ITEM_KEY;
2296
2297 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002298 if (ret < 0)
2299 goto out;
2300 else if (ret > 0) { /* Logic error or corruption */
2301 btrfs_error(root->fs_info, -ENOENT,
2302 "Failed lookup while freeing chunk.");
2303 ret = -ENOENT;
2304 goto out;
2305 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002306
2307 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002308 if (ret < 0)
2309 btrfs_error(root->fs_info, ret,
2310 "Failed to delete chunk item.");
2311out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002312 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002313 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002314}
2315
Christoph Hellwigb2950862008-12-02 09:54:17 -05002316static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002317 chunk_offset)
2318{
David Sterba6c417612011-04-13 15:41:04 +02002319 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002320 struct btrfs_disk_key *disk_key;
2321 struct btrfs_chunk *chunk;
2322 u8 *ptr;
2323 int ret = 0;
2324 u32 num_stripes;
2325 u32 array_size;
2326 u32 len = 0;
2327 u32 cur;
2328 struct btrfs_key key;
2329
2330 array_size = btrfs_super_sys_array_size(super_copy);
2331
2332 ptr = super_copy->sys_chunk_array;
2333 cur = 0;
2334
2335 while (cur < array_size) {
2336 disk_key = (struct btrfs_disk_key *)ptr;
2337 btrfs_disk_key_to_cpu(&key, disk_key);
2338
2339 len = sizeof(*disk_key);
2340
2341 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2342 chunk = (struct btrfs_chunk *)(ptr + len);
2343 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2344 len += btrfs_chunk_item_size(num_stripes);
2345 } else {
2346 ret = -EIO;
2347 break;
2348 }
2349 if (key.objectid == chunk_objectid &&
2350 key.offset == chunk_offset) {
2351 memmove(ptr, ptr + len, array_size - (cur + len));
2352 array_size -= len;
2353 btrfs_set_super_sys_array_size(super_copy, array_size);
2354 } else {
2355 ptr += len;
2356 cur += len;
2357 }
2358 }
2359 return ret;
2360}
2361
Christoph Hellwigb2950862008-12-02 09:54:17 -05002362static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002363 u64 chunk_tree, u64 chunk_objectid,
2364 u64 chunk_offset)
2365{
2366 struct extent_map_tree *em_tree;
2367 struct btrfs_root *extent_root;
2368 struct btrfs_trans_handle *trans;
2369 struct extent_map *em;
2370 struct map_lookup *map;
2371 int ret;
2372 int i;
2373
2374 root = root->fs_info->chunk_root;
2375 extent_root = root->fs_info->extent_root;
2376 em_tree = &root->fs_info->mapping_tree.map_tree;
2377
Josef Bacikba1bf482009-09-11 16:11:19 -04002378 ret = btrfs_can_relocate(extent_root, chunk_offset);
2379 if (ret)
2380 return -ENOSPC;
2381
Chris Mason8f18cf12008-04-25 16:53:30 -04002382 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002383 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002384 if (ret)
2385 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002386
Yan, Zhenga22285a2010-05-16 10:48:46 -04002387 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002388 if (IS_ERR(trans)) {
2389 ret = PTR_ERR(trans);
2390 btrfs_std_error(root->fs_info, ret);
2391 return ret;
2392 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002393
Chris Mason7d9eb122008-07-08 14:19:17 -04002394 lock_chunks(root);
2395
Chris Mason8f18cf12008-04-25 16:53:30 -04002396 /*
2397 * step two, delete the device extents and the
2398 * chunk tree entries
2399 */
Chris Mason890871b2009-09-02 16:24:52 -04002400 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002401 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002402 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002403
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002404 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002405 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002406 map = (struct map_lookup *)em->bdev;
2407
2408 for (i = 0; i < map->num_stripes; i++) {
2409 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2410 map->stripes[i].physical);
2411 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002412
Chris Masondfe25022008-05-13 13:46:40 -04002413 if (map->stripes[i].dev) {
2414 ret = btrfs_update_device(trans, map->stripes[i].dev);
2415 BUG_ON(ret);
2416 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002417 }
2418 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2419 chunk_offset);
2420
2421 BUG_ON(ret);
2422
liubo1abe9b82011-03-24 11:18:59 +00002423 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2424
Chris Mason8f18cf12008-04-25 16:53:30 -04002425 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2426 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2427 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002428 }
2429
Zheng Yan1a40e232008-09-26 10:09:34 -04002430 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2431 BUG_ON(ret);
2432
Chris Mason890871b2009-09-02 16:24:52 -04002433 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002434 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002435 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002436
Chris Mason8f18cf12008-04-25 16:53:30 -04002437 kfree(map);
2438 em->bdev = NULL;
2439
2440 /* once for the tree */
2441 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002442 /* once for us */
2443 free_extent_map(em);
2444
Chris Mason7d9eb122008-07-08 14:19:17 -04002445 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002446 btrfs_end_transaction(trans, root);
2447 return 0;
2448}
2449
Yan Zheng2b820322008-11-17 21:11:30 -05002450static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2451{
2452 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2453 struct btrfs_path *path;
2454 struct extent_buffer *leaf;
2455 struct btrfs_chunk *chunk;
2456 struct btrfs_key key;
2457 struct btrfs_key found_key;
2458 u64 chunk_tree = chunk_root->root_key.objectid;
2459 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002460 bool retried = false;
2461 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002462 int ret;
2463
2464 path = btrfs_alloc_path();
2465 if (!path)
2466 return -ENOMEM;
2467
Josef Bacikba1bf482009-09-11 16:11:19 -04002468again:
Yan Zheng2b820322008-11-17 21:11:30 -05002469 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2470 key.offset = (u64)-1;
2471 key.type = BTRFS_CHUNK_ITEM_KEY;
2472
2473 while (1) {
2474 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2475 if (ret < 0)
2476 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002477 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002478
2479 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2480 key.type);
2481 if (ret < 0)
2482 goto error;
2483 if (ret > 0)
2484 break;
2485
2486 leaf = path->nodes[0];
2487 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2488
2489 chunk = btrfs_item_ptr(leaf, path->slots[0],
2490 struct btrfs_chunk);
2491 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002492 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002493
2494 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2495 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2496 found_key.objectid,
2497 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002498 if (ret == -ENOSPC)
2499 failed++;
2500 else if (ret)
2501 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002502 }
2503
2504 if (found_key.offset == 0)
2505 break;
2506 key.offset = found_key.offset - 1;
2507 }
2508 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002509 if (failed && !retried) {
2510 failed = 0;
2511 retried = true;
2512 goto again;
2513 } else if (failed && retried) {
2514 WARN_ON(1);
2515 ret = -ENOSPC;
2516 }
Yan Zheng2b820322008-11-17 21:11:30 -05002517error:
2518 btrfs_free_path(path);
2519 return ret;
2520}
2521
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002522static int insert_balance_item(struct btrfs_root *root,
2523 struct btrfs_balance_control *bctl)
2524{
2525 struct btrfs_trans_handle *trans;
2526 struct btrfs_balance_item *item;
2527 struct btrfs_disk_balance_args disk_bargs;
2528 struct btrfs_path *path;
2529 struct extent_buffer *leaf;
2530 struct btrfs_key key;
2531 int ret, err;
2532
2533 path = btrfs_alloc_path();
2534 if (!path)
2535 return -ENOMEM;
2536
2537 trans = btrfs_start_transaction(root, 0);
2538 if (IS_ERR(trans)) {
2539 btrfs_free_path(path);
2540 return PTR_ERR(trans);
2541 }
2542
2543 key.objectid = BTRFS_BALANCE_OBJECTID;
2544 key.type = BTRFS_BALANCE_ITEM_KEY;
2545 key.offset = 0;
2546
2547 ret = btrfs_insert_empty_item(trans, root, path, &key,
2548 sizeof(*item));
2549 if (ret)
2550 goto out;
2551
2552 leaf = path->nodes[0];
2553 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2554
2555 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2556
2557 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2558 btrfs_set_balance_data(leaf, item, &disk_bargs);
2559 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2560 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2561 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2562 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2563
2564 btrfs_set_balance_flags(leaf, item, bctl->flags);
2565
2566 btrfs_mark_buffer_dirty(leaf);
2567out:
2568 btrfs_free_path(path);
2569 err = btrfs_commit_transaction(trans, root);
2570 if (err && !ret)
2571 ret = err;
2572 return ret;
2573}
2574
2575static int del_balance_item(struct btrfs_root *root)
2576{
2577 struct btrfs_trans_handle *trans;
2578 struct btrfs_path *path;
2579 struct btrfs_key key;
2580 int ret, err;
2581
2582 path = btrfs_alloc_path();
2583 if (!path)
2584 return -ENOMEM;
2585
2586 trans = btrfs_start_transaction(root, 0);
2587 if (IS_ERR(trans)) {
2588 btrfs_free_path(path);
2589 return PTR_ERR(trans);
2590 }
2591
2592 key.objectid = BTRFS_BALANCE_OBJECTID;
2593 key.type = BTRFS_BALANCE_ITEM_KEY;
2594 key.offset = 0;
2595
2596 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2597 if (ret < 0)
2598 goto out;
2599 if (ret > 0) {
2600 ret = -ENOENT;
2601 goto out;
2602 }
2603
2604 ret = btrfs_del_item(trans, root, path);
2605out:
2606 btrfs_free_path(path);
2607 err = btrfs_commit_transaction(trans, root);
2608 if (err && !ret)
2609 ret = err;
2610 return ret;
2611}
2612
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002613/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002614 * This is a heuristic used to reduce the number of chunks balanced on
2615 * resume after balance was interrupted.
2616 */
2617static void update_balance_args(struct btrfs_balance_control *bctl)
2618{
2619 /*
2620 * Turn on soft mode for chunk types that were being converted.
2621 */
2622 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2623 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2624 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2625 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2626 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2627 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2628
2629 /*
2630 * Turn on usage filter if is not already used. The idea is
2631 * that chunks that we have already balanced should be
2632 * reasonably full. Don't do it for chunks that are being
2633 * converted - that will keep us from relocating unconverted
2634 * (albeit full) chunks.
2635 */
2636 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2637 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2638 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2639 bctl->data.usage = 90;
2640 }
2641 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2642 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2643 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2644 bctl->sys.usage = 90;
2645 }
2646 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2647 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2648 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2649 bctl->meta.usage = 90;
2650 }
2651}
2652
2653/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002654 * Should be called with both balance and volume mutexes held to
2655 * serialize other volume operations (add_dev/rm_dev/resize) with
2656 * restriper. Same goes for unset_balance_control.
2657 */
2658static void set_balance_control(struct btrfs_balance_control *bctl)
2659{
2660 struct btrfs_fs_info *fs_info = bctl->fs_info;
2661
2662 BUG_ON(fs_info->balance_ctl);
2663
2664 spin_lock(&fs_info->balance_lock);
2665 fs_info->balance_ctl = bctl;
2666 spin_unlock(&fs_info->balance_lock);
2667}
2668
2669static void unset_balance_control(struct btrfs_fs_info *fs_info)
2670{
2671 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2672
2673 BUG_ON(!fs_info->balance_ctl);
2674
2675 spin_lock(&fs_info->balance_lock);
2676 fs_info->balance_ctl = NULL;
2677 spin_unlock(&fs_info->balance_lock);
2678
2679 kfree(bctl);
2680}
2681
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002682/*
2683 * Balance filters. Return 1 if chunk should be filtered out
2684 * (should not be balanced).
2685 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002686static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002687 struct btrfs_balance_args *bargs)
2688{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002689 chunk_type = chunk_to_extended(chunk_type) &
2690 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002691
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002692 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002693 return 0;
2694
2695 return 1;
2696}
2697
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002698static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2699 struct btrfs_balance_args *bargs)
2700{
2701 struct btrfs_block_group_cache *cache;
2702 u64 chunk_used, user_thresh;
2703 int ret = 1;
2704
2705 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2706 chunk_used = btrfs_block_group_used(&cache->item);
2707
Ilya Dryomova105bb82013-01-21 15:15:56 +02002708 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002709 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002710 else if (bargs->usage > 100)
2711 user_thresh = cache->key.offset;
2712 else
2713 user_thresh = div_factor_fine(cache->key.offset,
2714 bargs->usage);
2715
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002716 if (chunk_used < user_thresh)
2717 ret = 0;
2718
2719 btrfs_put_block_group(cache);
2720 return ret;
2721}
2722
Ilya Dryomov409d4042012-01-16 22:04:47 +02002723static int chunk_devid_filter(struct extent_buffer *leaf,
2724 struct btrfs_chunk *chunk,
2725 struct btrfs_balance_args *bargs)
2726{
2727 struct btrfs_stripe *stripe;
2728 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2729 int i;
2730
2731 for (i = 0; i < num_stripes; i++) {
2732 stripe = btrfs_stripe_nr(chunk, i);
2733 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2734 return 0;
2735 }
2736
2737 return 1;
2738}
2739
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002740/* [pstart, pend) */
2741static int chunk_drange_filter(struct extent_buffer *leaf,
2742 struct btrfs_chunk *chunk,
2743 u64 chunk_offset,
2744 struct btrfs_balance_args *bargs)
2745{
2746 struct btrfs_stripe *stripe;
2747 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2748 u64 stripe_offset;
2749 u64 stripe_length;
2750 int factor;
2751 int i;
2752
2753 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2754 return 0;
2755
2756 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002757 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2758 factor = num_stripes / 2;
2759 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2760 factor = num_stripes - 1;
2761 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2762 factor = num_stripes - 2;
2763 } else {
2764 factor = num_stripes;
2765 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002766
2767 for (i = 0; i < num_stripes; i++) {
2768 stripe = btrfs_stripe_nr(chunk, i);
2769 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2770 continue;
2771
2772 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2773 stripe_length = btrfs_chunk_length(leaf, chunk);
2774 do_div(stripe_length, factor);
2775
2776 if (stripe_offset < bargs->pend &&
2777 stripe_offset + stripe_length > bargs->pstart)
2778 return 0;
2779 }
2780
2781 return 1;
2782}
2783
Ilya Dryomovea671762012-01-16 22:04:48 +02002784/* [vstart, vend) */
2785static int chunk_vrange_filter(struct extent_buffer *leaf,
2786 struct btrfs_chunk *chunk,
2787 u64 chunk_offset,
2788 struct btrfs_balance_args *bargs)
2789{
2790 if (chunk_offset < bargs->vend &&
2791 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2792 /* at least part of the chunk is inside this vrange */
2793 return 0;
2794
2795 return 1;
2796}
2797
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002798static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002799 struct btrfs_balance_args *bargs)
2800{
2801 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2802 return 0;
2803
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002804 chunk_type = chunk_to_extended(chunk_type) &
2805 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002806
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002807 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002808 return 1;
2809
2810 return 0;
2811}
2812
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002813static int should_balance_chunk(struct btrfs_root *root,
2814 struct extent_buffer *leaf,
2815 struct btrfs_chunk *chunk, u64 chunk_offset)
2816{
2817 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2818 struct btrfs_balance_args *bargs = NULL;
2819 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2820
2821 /* type filter */
2822 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2823 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2824 return 0;
2825 }
2826
2827 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2828 bargs = &bctl->data;
2829 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2830 bargs = &bctl->sys;
2831 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2832 bargs = &bctl->meta;
2833
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002834 /* profiles filter */
2835 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2836 chunk_profiles_filter(chunk_type, bargs)) {
2837 return 0;
2838 }
2839
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002840 /* usage filter */
2841 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2842 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2843 return 0;
2844 }
2845
Ilya Dryomov409d4042012-01-16 22:04:47 +02002846 /* devid filter */
2847 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2848 chunk_devid_filter(leaf, chunk, bargs)) {
2849 return 0;
2850 }
2851
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002852 /* drange filter, makes sense only with devid filter */
2853 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2854 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2855 return 0;
2856 }
2857
Ilya Dryomovea671762012-01-16 22:04:48 +02002858 /* vrange filter */
2859 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2860 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2861 return 0;
2862 }
2863
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002864 /* soft profile changing mode */
2865 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2866 chunk_soft_convert_filter(chunk_type, bargs)) {
2867 return 0;
2868 }
2869
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002870 return 1;
2871}
2872
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002873static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002874{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002875 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002876 struct btrfs_root *chunk_root = fs_info->chunk_root;
2877 struct btrfs_root *dev_root = fs_info->dev_root;
2878 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002879 struct btrfs_device *device;
2880 u64 old_size;
2881 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002882 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002883 struct btrfs_path *path;
2884 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002885 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002886 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002887 struct extent_buffer *leaf;
2888 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002889 int ret;
2890 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002891 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002892
Chris Masonec44a352008-04-28 15:29:52 -04002893 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002894 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002895 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002896 old_size = device->total_bytes;
2897 size_to_free = div_factor(old_size, 1);
2898 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002899 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002900 device->total_bytes - device->bytes_used > size_to_free ||
2901 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002902 continue;
2903
2904 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002905 if (ret == -ENOSPC)
2906 break;
Chris Masonec44a352008-04-28 15:29:52 -04002907 BUG_ON(ret);
2908
Yan, Zhenga22285a2010-05-16 10:48:46 -04002909 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002910 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002911
2912 ret = btrfs_grow_device(trans, device, old_size);
2913 BUG_ON(ret);
2914
2915 btrfs_end_transaction(trans, dev_root);
2916 }
2917
2918 /* step two, relocate all the chunks */
2919 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002920 if (!path) {
2921 ret = -ENOMEM;
2922 goto error;
2923 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002924
2925 /* zero out stat counters */
2926 spin_lock(&fs_info->balance_lock);
2927 memset(&bctl->stat, 0, sizeof(bctl->stat));
2928 spin_unlock(&fs_info->balance_lock);
2929again:
Chris Masonec44a352008-04-28 15:29:52 -04002930 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2931 key.offset = (u64)-1;
2932 key.type = BTRFS_CHUNK_ITEM_KEY;
2933
Chris Masond3977122009-01-05 21:25:51 -05002934 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002935 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002936 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002937 ret = -ECANCELED;
2938 goto error;
2939 }
2940
Chris Masonec44a352008-04-28 15:29:52 -04002941 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2942 if (ret < 0)
2943 goto error;
2944
2945 /*
2946 * this shouldn't happen, it means the last relocate
2947 * failed
2948 */
2949 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002950 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002951
2952 ret = btrfs_previous_item(chunk_root, path, 0,
2953 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002954 if (ret) {
2955 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002956 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002957 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002958
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002959 leaf = path->nodes[0];
2960 slot = path->slots[0];
2961 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2962
Chris Masonec44a352008-04-28 15:29:52 -04002963 if (found_key.objectid != key.objectid)
2964 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002965
Chris Masonec44a352008-04-28 15:29:52 -04002966 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002967 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002968 break;
2969
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002970 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2971
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002972 if (!counting) {
2973 spin_lock(&fs_info->balance_lock);
2974 bctl->stat.considered++;
2975 spin_unlock(&fs_info->balance_lock);
2976 }
2977
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002978 ret = should_balance_chunk(chunk_root, leaf, chunk,
2979 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002980 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002981 if (!ret)
2982 goto loop;
2983
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002984 if (counting) {
2985 spin_lock(&fs_info->balance_lock);
2986 bctl->stat.expected++;
2987 spin_unlock(&fs_info->balance_lock);
2988 goto loop;
2989 }
2990
Chris Masonec44a352008-04-28 15:29:52 -04002991 ret = btrfs_relocate_chunk(chunk_root,
2992 chunk_root->root_key.objectid,
2993 found_key.objectid,
2994 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002995 if (ret && ret != -ENOSPC)
2996 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002997 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002998 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002999 } else {
3000 spin_lock(&fs_info->balance_lock);
3001 bctl->stat.completed++;
3002 spin_unlock(&fs_info->balance_lock);
3003 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003004loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04003005 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003006 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003007
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003008 if (counting) {
3009 btrfs_release_path(path);
3010 counting = false;
3011 goto again;
3012 }
Chris Masonec44a352008-04-28 15:29:52 -04003013error:
3014 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003015 if (enospc_errors) {
3016 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
3017 enospc_errors);
3018 if (!ret)
3019 ret = -ENOSPC;
3020 }
3021
Chris Masonec44a352008-04-28 15:29:52 -04003022 return ret;
3023}
3024
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003025/**
3026 * alloc_profile_is_valid - see if a given profile is valid and reduced
3027 * @flags: profile to validate
3028 * @extended: if true @flags is treated as an extended profile
3029 */
3030static int alloc_profile_is_valid(u64 flags, int extended)
3031{
3032 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3033 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3034
3035 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3036
3037 /* 1) check that all other bits are zeroed */
3038 if (flags & ~mask)
3039 return 0;
3040
3041 /* 2) see if profile is reduced */
3042 if (flags == 0)
3043 return !extended; /* "0" is valid for usual profiles */
3044
3045 /* true if exactly one bit set */
3046 return (flags & (flags - 1)) == 0;
3047}
3048
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003049static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3050{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003051 /* cancel requested || normal exit path */
3052 return atomic_read(&fs_info->balance_cancel_req) ||
3053 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3054 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003055}
3056
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003057static void __cancel_balance(struct btrfs_fs_info *fs_info)
3058{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003059 int ret;
3060
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003061 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003062 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003063 if (ret)
3064 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003065
3066 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003067}
3068
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003069void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003070 struct btrfs_ioctl_balance_args *bargs);
3071
3072/*
3073 * Should be called with both balance and volume mutexes held
3074 */
3075int btrfs_balance(struct btrfs_balance_control *bctl,
3076 struct btrfs_ioctl_balance_args *bargs)
3077{
3078 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003079 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003080 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003081 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003082 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003083 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003084
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003085 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003086 atomic_read(&fs_info->balance_pause_req) ||
3087 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003088 ret = -EINVAL;
3089 goto out;
3090 }
3091
Ilya Dryomove4837f82012-03-27 17:09:17 +03003092 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3093 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3094 mixed = 1;
3095
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003096 /*
3097 * In case of mixed groups both data and meta should be picked,
3098 * and identical options should be given for both of them.
3099 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003100 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3101 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003102 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3103 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3104 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3105 printk(KERN_ERR "btrfs: with mixed groups data and "
3106 "metadata balance options must be the same\n");
3107 ret = -EINVAL;
3108 goto out;
3109 }
3110 }
3111
Stefan Behrens8dabb742012-11-06 13:15:27 +01003112 num_devices = fs_info->fs_devices->num_devices;
3113 btrfs_dev_replace_lock(&fs_info->dev_replace);
3114 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3115 BUG_ON(num_devices < 1);
3116 num_devices--;
3117 }
3118 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003119 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003120 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003121 allowed |= BTRFS_BLOCK_GROUP_DUP;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003122 else if (num_devices < 4)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003123 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3124 else
3125 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003126 BTRFS_BLOCK_GROUP_RAID10 |
3127 BTRFS_BLOCK_GROUP_RAID5 |
3128 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003129
Ilya Dryomov6728b192012-03-27 17:09:17 +03003130 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3131 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3132 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003133 printk(KERN_ERR "btrfs: unable to start balance with target "
3134 "data profile %llu\n",
3135 (unsigned long long)bctl->data.target);
3136 ret = -EINVAL;
3137 goto out;
3138 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003139 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3140 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3141 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003142 printk(KERN_ERR "btrfs: unable to start balance with target "
3143 "metadata profile %llu\n",
3144 (unsigned long long)bctl->meta.target);
3145 ret = -EINVAL;
3146 goto out;
3147 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003148 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3149 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3150 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003151 printk(KERN_ERR "btrfs: unable to start balance with target "
3152 "system profile %llu\n",
3153 (unsigned long long)bctl->sys.target);
3154 ret = -EINVAL;
3155 goto out;
3156 }
3157
Ilya Dryomove4837f82012-03-27 17:09:17 +03003158 /* allow dup'ed data chunks only in mixed mode */
3159 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003160 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003161 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3162 ret = -EINVAL;
3163 goto out;
3164 }
3165
3166 /* allow to reduce meta or sys integrity only if force set */
3167 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003168 BTRFS_BLOCK_GROUP_RAID10 |
3169 BTRFS_BLOCK_GROUP_RAID5 |
3170 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003171 do {
3172 seq = read_seqbegin(&fs_info->profiles_lock);
3173
3174 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3175 (fs_info->avail_system_alloc_bits & allowed) &&
3176 !(bctl->sys.target & allowed)) ||
3177 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3178 (fs_info->avail_metadata_alloc_bits & allowed) &&
3179 !(bctl->meta.target & allowed))) {
3180 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3181 printk(KERN_INFO "btrfs: force reducing metadata "
3182 "integrity\n");
3183 } else {
3184 printk(KERN_ERR "btrfs: balance will reduce metadata "
3185 "integrity, use force if you want this\n");
3186 ret = -EINVAL;
3187 goto out;
3188 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003189 }
Miao Xiede98ced2013-01-29 10:13:12 +00003190 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003191
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003192 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3193 int num_tolerated_disk_barrier_failures;
3194 u64 target = bctl->sys.target;
3195
3196 num_tolerated_disk_barrier_failures =
3197 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3198 if (num_tolerated_disk_barrier_failures > 0 &&
3199 (target &
3200 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3201 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3202 num_tolerated_disk_barrier_failures = 0;
3203 else if (num_tolerated_disk_barrier_failures > 1 &&
3204 (target &
3205 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3206 num_tolerated_disk_barrier_failures = 1;
3207
3208 fs_info->num_tolerated_disk_barrier_failures =
3209 num_tolerated_disk_barrier_failures;
3210 }
3211
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003212 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003213 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003214 goto out;
3215
Ilya Dryomov59641012012-01-16 22:04:48 +02003216 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3217 BUG_ON(ret == -EEXIST);
3218 set_balance_control(bctl);
3219 } else {
3220 BUG_ON(ret != -EEXIST);
3221 spin_lock(&fs_info->balance_lock);
3222 update_balance_args(bctl);
3223 spin_unlock(&fs_info->balance_lock);
3224 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003225
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003226 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003227 mutex_unlock(&fs_info->balance_mutex);
3228
3229 ret = __btrfs_balance(fs_info);
3230
3231 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003232 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003233
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003234 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3235 fs_info->num_tolerated_disk_barrier_failures =
3236 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3237 }
3238
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003239 if (bargs) {
3240 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003241 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003242 }
3243
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003244 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3245 balance_need_close(fs_info)) {
3246 __cancel_balance(fs_info);
3247 }
3248
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003249 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003250
3251 return ret;
3252out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003253 if (bctl->flags & BTRFS_BALANCE_RESUME)
3254 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003255 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003256 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003257 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3258 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003259 return ret;
3260}
3261
3262static int balance_kthread(void *data)
3263{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003264 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003265 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003266
3267 mutex_lock(&fs_info->volume_mutex);
3268 mutex_lock(&fs_info->balance_mutex);
3269
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003270 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003271 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003272 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003273 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003274
3275 mutex_unlock(&fs_info->balance_mutex);
3276 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003277
Ilya Dryomov59641012012-01-16 22:04:48 +02003278 return ret;
3279}
3280
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003281int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3282{
3283 struct task_struct *tsk;
3284
3285 spin_lock(&fs_info->balance_lock);
3286 if (!fs_info->balance_ctl) {
3287 spin_unlock(&fs_info->balance_lock);
3288 return 0;
3289 }
3290 spin_unlock(&fs_info->balance_lock);
3291
3292 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3293 printk(KERN_INFO "btrfs: force skipping balance\n");
3294 return 0;
3295 }
3296
3297 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3298 if (IS_ERR(tsk))
3299 return PTR_ERR(tsk);
3300
3301 return 0;
3302}
3303
Ilya Dryomov68310a52012-06-22 12:24:12 -06003304int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003305{
Ilya Dryomov59641012012-01-16 22:04:48 +02003306 struct btrfs_balance_control *bctl;
3307 struct btrfs_balance_item *item;
3308 struct btrfs_disk_balance_args disk_bargs;
3309 struct btrfs_path *path;
3310 struct extent_buffer *leaf;
3311 struct btrfs_key key;
3312 int ret;
3313
3314 path = btrfs_alloc_path();
3315 if (!path)
3316 return -ENOMEM;
3317
Ilya Dryomov68310a52012-06-22 12:24:12 -06003318 key.objectid = BTRFS_BALANCE_OBJECTID;
3319 key.type = BTRFS_BALANCE_ITEM_KEY;
3320 key.offset = 0;
3321
3322 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3323 if (ret < 0)
3324 goto out;
3325 if (ret > 0) { /* ret = -ENOENT; */
3326 ret = 0;
3327 goto out;
3328 }
3329
Ilya Dryomov59641012012-01-16 22:04:48 +02003330 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3331 if (!bctl) {
3332 ret = -ENOMEM;
3333 goto out;
3334 }
3335
Ilya Dryomov59641012012-01-16 22:04:48 +02003336 leaf = path->nodes[0];
3337 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3338
Ilya Dryomov68310a52012-06-22 12:24:12 -06003339 bctl->fs_info = fs_info;
3340 bctl->flags = btrfs_balance_flags(leaf, item);
3341 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003342
3343 btrfs_balance_data(leaf, item, &disk_bargs);
3344 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3345 btrfs_balance_meta(leaf, item, &disk_bargs);
3346 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3347 btrfs_balance_sys(leaf, item, &disk_bargs);
3348 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3349
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003350 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3351
Ilya Dryomov68310a52012-06-22 12:24:12 -06003352 mutex_lock(&fs_info->volume_mutex);
3353 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003354
Ilya Dryomov68310a52012-06-22 12:24:12 -06003355 set_balance_control(bctl);
3356
3357 mutex_unlock(&fs_info->balance_mutex);
3358 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003359out:
3360 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003361 return ret;
3362}
3363
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003364int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3365{
3366 int ret = 0;
3367
3368 mutex_lock(&fs_info->balance_mutex);
3369 if (!fs_info->balance_ctl) {
3370 mutex_unlock(&fs_info->balance_mutex);
3371 return -ENOTCONN;
3372 }
3373
3374 if (atomic_read(&fs_info->balance_running)) {
3375 atomic_inc(&fs_info->balance_pause_req);
3376 mutex_unlock(&fs_info->balance_mutex);
3377
3378 wait_event(fs_info->balance_wait_q,
3379 atomic_read(&fs_info->balance_running) == 0);
3380
3381 mutex_lock(&fs_info->balance_mutex);
3382 /* we are good with balance_ctl ripped off from under us */
3383 BUG_ON(atomic_read(&fs_info->balance_running));
3384 atomic_dec(&fs_info->balance_pause_req);
3385 } else {
3386 ret = -ENOTCONN;
3387 }
3388
3389 mutex_unlock(&fs_info->balance_mutex);
3390 return ret;
3391}
3392
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003393int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3394{
3395 mutex_lock(&fs_info->balance_mutex);
3396 if (!fs_info->balance_ctl) {
3397 mutex_unlock(&fs_info->balance_mutex);
3398 return -ENOTCONN;
3399 }
3400
3401 atomic_inc(&fs_info->balance_cancel_req);
3402 /*
3403 * if we are running just wait and return, balance item is
3404 * deleted in btrfs_balance in this case
3405 */
3406 if (atomic_read(&fs_info->balance_running)) {
3407 mutex_unlock(&fs_info->balance_mutex);
3408 wait_event(fs_info->balance_wait_q,
3409 atomic_read(&fs_info->balance_running) == 0);
3410 mutex_lock(&fs_info->balance_mutex);
3411 } else {
3412 /* __cancel_balance needs volume_mutex */
3413 mutex_unlock(&fs_info->balance_mutex);
3414 mutex_lock(&fs_info->volume_mutex);
3415 mutex_lock(&fs_info->balance_mutex);
3416
3417 if (fs_info->balance_ctl)
3418 __cancel_balance(fs_info);
3419
3420 mutex_unlock(&fs_info->volume_mutex);
3421 }
3422
3423 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3424 atomic_dec(&fs_info->balance_cancel_req);
3425 mutex_unlock(&fs_info->balance_mutex);
3426 return 0;
3427}
3428
Chris Mason8f18cf12008-04-25 16:53:30 -04003429/*
3430 * shrinking a device means finding all of the device extents past
3431 * the new size, and then following the back refs to the chunks.
3432 * The chunk relocation code actually frees the device extent
3433 */
3434int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3435{
3436 struct btrfs_trans_handle *trans;
3437 struct btrfs_root *root = device->dev_root;
3438 struct btrfs_dev_extent *dev_extent = NULL;
3439 struct btrfs_path *path;
3440 u64 length;
3441 u64 chunk_tree;
3442 u64 chunk_objectid;
3443 u64 chunk_offset;
3444 int ret;
3445 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003446 int failed = 0;
3447 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003448 struct extent_buffer *l;
3449 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003450 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003451 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003452 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003453 u64 diff = device->total_bytes - new_size;
3454
Stefan Behrens63a212a2012-11-05 18:29:28 +01003455 if (device->is_tgtdev_for_dev_replace)
3456 return -EINVAL;
3457
Chris Mason8f18cf12008-04-25 16:53:30 -04003458 path = btrfs_alloc_path();
3459 if (!path)
3460 return -ENOMEM;
3461
Chris Mason8f18cf12008-04-25 16:53:30 -04003462 path->reada = 2;
3463
Chris Mason7d9eb122008-07-08 14:19:17 -04003464 lock_chunks(root);
3465
Chris Mason8f18cf12008-04-25 16:53:30 -04003466 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003467 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003468 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003469 spin_lock(&root->fs_info->free_chunk_lock);
3470 root->fs_info->free_chunk_space -= diff;
3471 spin_unlock(&root->fs_info->free_chunk_lock);
3472 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003473 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003474
Josef Bacikba1bf482009-09-11 16:11:19 -04003475again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003476 key.objectid = device->devid;
3477 key.offset = (u64)-1;
3478 key.type = BTRFS_DEV_EXTENT_KEY;
3479
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003480 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003481 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3482 if (ret < 0)
3483 goto done;
3484
3485 ret = btrfs_previous_item(root, path, 0, key.type);
3486 if (ret < 0)
3487 goto done;
3488 if (ret) {
3489 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003490 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003491 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003492 }
3493
3494 l = path->nodes[0];
3495 slot = path->slots[0];
3496 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3497
Josef Bacikba1bf482009-09-11 16:11:19 -04003498 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003499 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003500 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003501 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003502
3503 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3504 length = btrfs_dev_extent_length(l, dev_extent);
3505
Josef Bacikba1bf482009-09-11 16:11:19 -04003506 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003507 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003508 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003509 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003510
3511 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3512 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3513 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003514 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003515
3516 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3517 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003518 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003519 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003520 if (ret == -ENOSPC)
3521 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003522 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003523
3524 if (failed && !retried) {
3525 failed = 0;
3526 retried = true;
3527 goto again;
3528 } else if (failed && retried) {
3529 ret = -ENOSPC;
3530 lock_chunks(root);
3531
3532 device->total_bytes = old_size;
3533 if (device->writeable)
3534 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003535 spin_lock(&root->fs_info->free_chunk_lock);
3536 root->fs_info->free_chunk_space += diff;
3537 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003538 unlock_chunks(root);
3539 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003540 }
3541
Chris Balld6397ba2009-04-27 07:29:03 -04003542 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003543 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003544 if (IS_ERR(trans)) {
3545 ret = PTR_ERR(trans);
3546 goto done;
3547 }
3548
Chris Balld6397ba2009-04-27 07:29:03 -04003549 lock_chunks(root);
3550
3551 device->disk_total_bytes = new_size;
3552 /* Now btrfs_update_device() will change the on-disk size. */
3553 ret = btrfs_update_device(trans, device);
3554 if (ret) {
3555 unlock_chunks(root);
3556 btrfs_end_transaction(trans, root);
3557 goto done;
3558 }
3559 WARN_ON(diff > old_total);
3560 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3561 unlock_chunks(root);
3562 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003563done:
3564 btrfs_free_path(path);
3565 return ret;
3566}
3567
Li Zefan125ccb02011-12-08 15:07:24 +08003568static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003569 struct btrfs_key *key,
3570 struct btrfs_chunk *chunk, int item_size)
3571{
David Sterba6c417612011-04-13 15:41:04 +02003572 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003573 struct btrfs_disk_key disk_key;
3574 u32 array_size;
3575 u8 *ptr;
3576
3577 array_size = btrfs_super_sys_array_size(super_copy);
3578 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3579 return -EFBIG;
3580
3581 ptr = super_copy->sys_chunk_array + array_size;
3582 btrfs_cpu_key_to_disk(&disk_key, key);
3583 memcpy(ptr, &disk_key, sizeof(disk_key));
3584 ptr += sizeof(disk_key);
3585 memcpy(ptr, chunk, item_size);
3586 item_size += sizeof(disk_key);
3587 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3588 return 0;
3589}
3590
Miao Xieb2117a32011-01-05 10:07:28 +00003591/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003592 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003593 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003594static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003595{
Arne Jansen73c5de02011-04-12 12:07:57 +02003596 const struct btrfs_device_info *di_a = a;
3597 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003598
Arne Jansen73c5de02011-04-12 12:07:57 +02003599 if (di_a->max_avail > di_b->max_avail)
3600 return -1;
3601 if (di_a->max_avail < di_b->max_avail)
3602 return 1;
3603 if (di_a->total_avail > di_b->total_avail)
3604 return -1;
3605 if (di_a->total_avail < di_b->total_avail)
3606 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003607 return 0;
3608}
3609
Liu Bo31e50222012-11-21 14:18:10 +00003610struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00003611 [BTRFS_RAID_RAID10] = {
3612 .sub_stripes = 2,
3613 .dev_stripes = 1,
3614 .devs_max = 0, /* 0 == as many as possible */
3615 .devs_min = 4,
3616 .devs_increment = 2,
3617 .ncopies = 2,
3618 },
3619 [BTRFS_RAID_RAID1] = {
3620 .sub_stripes = 1,
3621 .dev_stripes = 1,
3622 .devs_max = 2,
3623 .devs_min = 2,
3624 .devs_increment = 2,
3625 .ncopies = 2,
3626 },
3627 [BTRFS_RAID_DUP] = {
3628 .sub_stripes = 1,
3629 .dev_stripes = 2,
3630 .devs_max = 1,
3631 .devs_min = 1,
3632 .devs_increment = 1,
3633 .ncopies = 2,
3634 },
3635 [BTRFS_RAID_RAID0] = {
3636 .sub_stripes = 1,
3637 .dev_stripes = 1,
3638 .devs_max = 0,
3639 .devs_min = 2,
3640 .devs_increment = 1,
3641 .ncopies = 1,
3642 },
3643 [BTRFS_RAID_SINGLE] = {
3644 .sub_stripes = 1,
3645 .dev_stripes = 1,
3646 .devs_max = 1,
3647 .devs_min = 1,
3648 .devs_increment = 1,
3649 .ncopies = 1,
3650 },
Chris Masone942f882013-02-20 14:06:05 -05003651 [BTRFS_RAID_RAID5] = {
3652 .sub_stripes = 1,
3653 .dev_stripes = 1,
3654 .devs_max = 0,
3655 .devs_min = 2,
3656 .devs_increment = 1,
3657 .ncopies = 2,
3658 },
3659 [BTRFS_RAID_RAID6] = {
3660 .sub_stripes = 1,
3661 .dev_stripes = 1,
3662 .devs_max = 0,
3663 .devs_min = 3,
3664 .devs_increment = 1,
3665 .ncopies = 3,
3666 },
Liu Bo31e50222012-11-21 14:18:10 +00003667};
3668
David Woodhouse53b381b2013-01-29 18:40:14 -05003669static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3670{
3671 /* TODO allow them to set a preferred stripe size */
3672 return 64 * 1024;
3673}
3674
3675static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3676{
David Woodhouse53b381b2013-01-29 18:40:14 -05003677 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3678 return;
3679
Miao Xieceda0862013-04-11 10:30:16 +00003680 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05003681}
3682
Miao Xieb2117a32011-01-05 10:07:28 +00003683static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3684 struct btrfs_root *extent_root,
3685 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003686 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003687 u64 start, u64 type)
3688{
3689 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003690 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3691 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003692 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003693 struct extent_map_tree *em_tree;
3694 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003695 struct btrfs_device_info *devices_info = NULL;
3696 u64 total_avail;
3697 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05003698 int data_stripes; /* number of stripes that count for
3699 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02003700 int sub_stripes; /* sub_stripes info for map */
3701 int dev_stripes; /* stripes per dev */
3702 int devs_max; /* max devs to use */
3703 int devs_min; /* min devs needed */
3704 int devs_increment; /* ndevs has to be a multiple of this */
3705 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003706 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003707 u64 max_stripe_size;
3708 u64 max_chunk_size;
3709 u64 stripe_size;
3710 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05003711 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02003712 int ndevs;
3713 int i;
3714 int j;
Liu Bo31e50222012-11-21 14:18:10 +00003715 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00003716
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003717 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003718
Miao Xieb2117a32011-01-05 10:07:28 +00003719 if (list_empty(&fs_devices->alloc_list))
3720 return -ENOSPC;
3721
Liu Bo31e50222012-11-21 14:18:10 +00003722 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02003723
Liu Bo31e50222012-11-21 14:18:10 +00003724 sub_stripes = btrfs_raid_array[index].sub_stripes;
3725 dev_stripes = btrfs_raid_array[index].dev_stripes;
3726 devs_max = btrfs_raid_array[index].devs_max;
3727 devs_min = btrfs_raid_array[index].devs_min;
3728 devs_increment = btrfs_raid_array[index].devs_increment;
3729 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02003730
3731 if (type & BTRFS_BLOCK_GROUP_DATA) {
3732 max_stripe_size = 1024 * 1024 * 1024;
3733 max_chunk_size = 10 * max_stripe_size;
3734 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003735 /* for larger filesystems, use larger metadata chunks */
3736 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3737 max_stripe_size = 1024 * 1024 * 1024;
3738 else
3739 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003740 max_chunk_size = max_stripe_size;
3741 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003742 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003743 max_chunk_size = 2 * max_stripe_size;
3744 } else {
3745 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3746 type);
3747 BUG_ON(1);
3748 }
3749
3750 /* we don't want a chunk larger than 10% of writeable space */
3751 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3752 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003753
3754 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3755 GFP_NOFS);
3756 if (!devices_info)
3757 return -ENOMEM;
3758
Arne Jansen73c5de02011-04-12 12:07:57 +02003759 cur = fs_devices->alloc_list.next;
3760
3761 /*
3762 * in the first pass through the devices list, we gather information
3763 * about the available holes on each device.
3764 */
3765 ndevs = 0;
3766 while (cur != &fs_devices->alloc_list) {
3767 struct btrfs_device *device;
3768 u64 max_avail;
3769 u64 dev_offset;
3770
3771 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3772
3773 cur = cur->next;
3774
3775 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003776 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003777 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003778 continue;
3779 }
3780
Stefan Behrens63a212a2012-11-05 18:29:28 +01003781 if (!device->in_fs_metadata ||
3782 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003783 continue;
3784
3785 if (device->total_bytes > device->bytes_used)
3786 total_avail = device->total_bytes - device->bytes_used;
3787 else
3788 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003789
3790 /* If there is no space on this device, skip it. */
3791 if (total_avail == 0)
3792 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003793
Li Zefan125ccb02011-12-08 15:07:24 +08003794 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003795 max_stripe_size * dev_stripes,
3796 &dev_offset, &max_avail);
3797 if (ret && ret != -ENOSPC)
3798 goto error;
3799
3800 if (ret == 0)
3801 max_avail = max_stripe_size * dev_stripes;
3802
3803 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3804 continue;
3805
Eric Sandeen063d0062013-01-31 00:55:01 +00003806 if (ndevs == fs_devices->rw_devices) {
3807 WARN(1, "%s: found more than %llu devices\n",
3808 __func__, fs_devices->rw_devices);
3809 break;
3810 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003811 devices_info[ndevs].dev_offset = dev_offset;
3812 devices_info[ndevs].max_avail = max_avail;
3813 devices_info[ndevs].total_avail = total_avail;
3814 devices_info[ndevs].dev = device;
3815 ++ndevs;
3816 }
3817
3818 /*
3819 * now sort the devices by hole size / available space
3820 */
3821 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3822 btrfs_cmp_device_info, NULL);
3823
3824 /* round down to number of usable stripes */
3825 ndevs -= ndevs % devs_increment;
3826
3827 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3828 ret = -ENOSPC;
3829 goto error;
3830 }
3831
3832 if (devs_max && ndevs > devs_max)
3833 ndevs = devs_max;
3834 /*
3835 * the primary goal is to maximize the number of stripes, so use as many
3836 * devices as possible, even if the stripes are not maximum sized.
3837 */
3838 stripe_size = devices_info[ndevs-1].max_avail;
3839 num_stripes = ndevs * dev_stripes;
3840
David Woodhouse53b381b2013-01-29 18:40:14 -05003841 /*
3842 * this will have to be fixed for RAID1 and RAID10 over
3843 * more drives
3844 */
3845 data_stripes = num_stripes / ncopies;
3846
David Woodhouse53b381b2013-01-29 18:40:14 -05003847 if (type & BTRFS_BLOCK_GROUP_RAID5) {
3848 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
3849 btrfs_super_stripesize(info->super_copy));
3850 data_stripes = num_stripes - 1;
3851 }
3852 if (type & BTRFS_BLOCK_GROUP_RAID6) {
3853 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
3854 btrfs_super_stripesize(info->super_copy));
3855 data_stripes = num_stripes - 2;
3856 }
Chris Mason86db2572013-02-20 16:23:40 -05003857
3858 /*
3859 * Use the number of data stripes to figure out how big this chunk
3860 * is really going to be in terms of logical address space,
3861 * and compare that answer with the max chunk size
3862 */
3863 if (stripe_size * data_stripes > max_chunk_size) {
3864 u64 mask = (1ULL << 24) - 1;
3865 stripe_size = max_chunk_size;
3866 do_div(stripe_size, data_stripes);
3867
3868 /* bump the answer up to a 16MB boundary */
3869 stripe_size = (stripe_size + mask) & ~mask;
3870
3871 /* but don't go higher than the limits we found
3872 * while searching for free extents
3873 */
3874 if (stripe_size > devices_info[ndevs-1].max_avail)
3875 stripe_size = devices_info[ndevs-1].max_avail;
3876 }
3877
Arne Jansen73c5de02011-04-12 12:07:57 +02003878 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003879
3880 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05003881 do_div(stripe_size, raid_stripe_len);
3882 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02003883
Miao Xieb2117a32011-01-05 10:07:28 +00003884 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3885 if (!map) {
3886 ret = -ENOMEM;
3887 goto error;
3888 }
3889 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003890
Arne Jansen73c5de02011-04-12 12:07:57 +02003891 for (i = 0; i < ndevs; ++i) {
3892 for (j = 0; j < dev_stripes; ++j) {
3893 int s = i * dev_stripes + j;
3894 map->stripes[s].dev = devices_info[i].dev;
3895 map->stripes[s].physical = devices_info[i].dev_offset +
3896 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003897 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003898 }
Chris Mason593060d2008-03-25 16:50:33 -04003899 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05003900 map->stripe_len = raid_stripe_len;
3901 map->io_align = raid_stripe_len;
3902 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04003903 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003904 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003905
Yan Zheng2b820322008-11-17 21:11:30 -05003906 *map_ret = map;
David Woodhouse53b381b2013-01-29 18:40:14 -05003907 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003908
Arne Jansen73c5de02011-04-12 12:07:57 +02003909 *stripe_size_out = stripe_size;
3910 *num_bytes_out = num_bytes;
3911
3912 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003913
David Sterba172ddd62011-04-21 00:48:27 +02003914 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003915 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003916 ret = -ENOMEM;
3917 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003918 }
Chris Mason0b86a832008-03-24 15:01:56 -04003919 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003920 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003921 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003922 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003923 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003924
Chris Mason0b86a832008-03-24 15:01:56 -04003925 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003926 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003927 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04003928 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003929 if (ret) {
3930 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003931 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003932 }
Yan Zheng2b820322008-11-17 21:11:30 -05003933
Arne Jansen73c5de02011-04-12 12:07:57 +02003934 for (i = 0; i < map->num_stripes; ++i) {
3935 struct btrfs_device *device;
3936 u64 dev_offset;
3937
3938 device = map->stripes[i].dev;
3939 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003940
3941 ret = btrfs_alloc_dev_extent(trans, device,
3942 info->chunk_root->root_key.objectid,
3943 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003944 start, dev_offset, stripe_size);
Josef Bacik04487482013-01-29 15:03:37 -05003945 if (ret)
3946 goto error_dev_extent;
3947 }
3948
3949 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3950 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3951 start, num_bytes);
3952 if (ret) {
3953 i = map->num_stripes - 1;
3954 goto error_dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05003955 }
3956
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003957 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05003958 check_raid56_incompat_flag(extent_root->fs_info, type);
3959
Miao Xieb2117a32011-01-05 10:07:28 +00003960 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003961 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003962
Josef Bacik04487482013-01-29 15:03:37 -05003963error_dev_extent:
3964 for (; i >= 0; i--) {
3965 struct btrfs_device *device;
3966 int err;
3967
3968 device = map->stripes[i].dev;
3969 err = btrfs_free_dev_extent(trans, device, start);
3970 if (err) {
3971 btrfs_abort_transaction(trans, extent_root, err);
3972 break;
3973 }
3974 }
Josef Bacik0f5d42b2013-01-31 10:23:04 -05003975 write_lock(&em_tree->lock);
3976 remove_extent_mapping(em_tree, em);
3977 write_unlock(&em_tree->lock);
3978
3979 /* One for our allocation */
3980 free_extent_map(em);
3981 /* One for the tree reference */
3982 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00003983error:
3984 kfree(map);
3985 kfree(devices_info);
3986 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003987}
3988
3989static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3990 struct btrfs_root *extent_root,
3991 struct map_lookup *map, u64 chunk_offset,
3992 u64 chunk_size, u64 stripe_size)
3993{
3994 u64 dev_offset;
3995 struct btrfs_key key;
3996 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3997 struct btrfs_device *device;
3998 struct btrfs_chunk *chunk;
3999 struct btrfs_stripe *stripe;
4000 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
4001 int index = 0;
4002 int ret;
4003
4004 chunk = kzalloc(item_size, GFP_NOFS);
4005 if (!chunk)
4006 return -ENOMEM;
4007
4008 index = 0;
4009 while (index < map->num_stripes) {
4010 device = map->stripes[index].dev;
4011 device->bytes_used += stripe_size;
4012 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004013 if (ret)
4014 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05004015 index++;
4016 }
4017
Josef Bacik2bf64752011-09-26 17:12:22 -04004018 spin_lock(&extent_root->fs_info->free_chunk_lock);
4019 extent_root->fs_info->free_chunk_space -= (stripe_size *
4020 map->num_stripes);
4021 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4022
Yan Zheng2b820322008-11-17 21:11:30 -05004023 index = 0;
4024 stripe = &chunk->stripe;
4025 while (index < map->num_stripes) {
4026 device = map->stripes[index].dev;
4027 dev_offset = map->stripes[index].physical;
4028
4029 btrfs_set_stack_stripe_devid(stripe, device->devid);
4030 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4031 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4032 stripe++;
4033 index++;
4034 }
4035
4036 btrfs_set_stack_chunk_length(chunk, chunk_size);
4037 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4038 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4039 btrfs_set_stack_chunk_type(chunk, map->type);
4040 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4041 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4042 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4043 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4044 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4045
4046 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4047 key.type = BTRFS_CHUNK_ITEM_KEY;
4048 key.offset = chunk_offset;
4049
4050 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004051
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004052 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4053 /*
4054 * TODO: Cleanup of inserted chunk root in case of
4055 * failure.
4056 */
Li Zefan125ccb02011-12-08 15:07:24 +08004057 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004058 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004059 }
liubo1abe9b82011-03-24 11:18:59 +00004060
Mark Fasheh3acd3952011-09-08 17:40:01 -07004061out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05004062 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004063 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004064}
4065
4066/*
4067 * Chunk allocation falls into two parts. The first part does works
4068 * that make the new allocated chunk useable, but not do any operation
4069 * that modifies the chunk tree. The second part does the works that
4070 * require modifying the chunk tree. This division is important for the
4071 * bootstrap process of adding storage to a seed btrfs.
4072 */
4073int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4074 struct btrfs_root *extent_root, u64 type)
4075{
4076 u64 chunk_offset;
4077 u64 chunk_size;
4078 u64 stripe_size;
4079 struct map_lookup *map;
4080 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4081 int ret;
4082
4083 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4084 &chunk_offset);
4085 if (ret)
4086 return ret;
4087
4088 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4089 &stripe_size, chunk_offset, type);
4090 if (ret)
4091 return ret;
4092
4093 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4094 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004095 if (ret)
4096 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004097 return 0;
4098}
4099
Chris Masond3977122009-01-05 21:25:51 -05004100static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004101 struct btrfs_root *root,
4102 struct btrfs_device *device)
4103{
4104 u64 chunk_offset;
4105 u64 sys_chunk_offset;
4106 u64 chunk_size;
4107 u64 sys_chunk_size;
4108 u64 stripe_size;
4109 u64 sys_stripe_size;
4110 u64 alloc_profile;
4111 struct map_lookup *map;
4112 struct map_lookup *sys_map;
4113 struct btrfs_fs_info *fs_info = root->fs_info;
4114 struct btrfs_root *extent_root = fs_info->extent_root;
4115 int ret;
4116
4117 ret = find_next_chunk(fs_info->chunk_root,
4118 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07004119 if (ret)
4120 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004121
Miao Xiede98ced2013-01-29 10:13:12 +00004122 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004123 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
4124 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004125 if (ret)
4126 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004127
4128 sys_chunk_offset = chunk_offset + chunk_size;
4129
Miao Xiede98ced2013-01-29 10:13:12 +00004130 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05004131 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
4132 &sys_chunk_size, &sys_stripe_size,
4133 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004134 if (ret) {
4135 btrfs_abort_transaction(trans, root, ret);
4136 goto out;
4137 }
Yan Zheng2b820322008-11-17 21:11:30 -05004138
4139 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06004140 if (ret) {
4141 btrfs_abort_transaction(trans, root, ret);
4142 goto out;
4143 }
Yan Zheng2b820322008-11-17 21:11:30 -05004144
4145 /*
4146 * Modifying chunk tree needs allocating new blocks from both
4147 * system block group and metadata block group. So we only can
4148 * do operations require modifying the chunk tree after both
4149 * block groups were created.
4150 */
4151 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4152 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06004153 if (ret) {
4154 btrfs_abort_transaction(trans, root, ret);
4155 goto out;
4156 }
Yan Zheng2b820322008-11-17 21:11:30 -05004157
4158 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
4159 sys_chunk_offset, sys_chunk_size,
4160 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004161 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004162 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004163
David Sterba005d6422012-09-18 07:52:32 -06004164out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004165
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004166 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004167}
4168
4169int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4170{
4171 struct extent_map *em;
4172 struct map_lookup *map;
4173 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4174 int readonly = 0;
4175 int i;
4176
Chris Mason890871b2009-09-02 16:24:52 -04004177 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004178 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004179 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004180 if (!em)
4181 return 1;
4182
Josef Bacikf48b9072010-01-27 02:07:59 +00004183 if (btrfs_test_opt(root, DEGRADED)) {
4184 free_extent_map(em);
4185 return 0;
4186 }
4187
Yan Zheng2b820322008-11-17 21:11:30 -05004188 map = (struct map_lookup *)em->bdev;
4189 for (i = 0; i < map->num_stripes; i++) {
4190 if (!map->stripes[i].dev->writeable) {
4191 readonly = 1;
4192 break;
4193 }
4194 }
4195 free_extent_map(em);
4196 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004197}
4198
4199void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4200{
David Sterbaa8067e02011-04-21 00:34:43 +02004201 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004202}
4203
4204void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4205{
4206 struct extent_map *em;
4207
Chris Masond3977122009-01-05 21:25:51 -05004208 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004209 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004210 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4211 if (em)
4212 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004213 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004214 if (!em)
4215 break;
4216 kfree(em->bdev);
4217 /* once for us */
4218 free_extent_map(em);
4219 /* once for the tree */
4220 free_extent_map(em);
4221 }
4222}
4223
Stefan Behrens5d964052012-11-05 14:59:07 +01004224int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004225{
Stefan Behrens5d964052012-11-05 14:59:07 +01004226 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004227 struct extent_map *em;
4228 struct map_lookup *map;
4229 struct extent_map_tree *em_tree = &map_tree->map_tree;
4230 int ret;
4231
Chris Mason890871b2009-09-02 16:24:52 -04004232 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004233 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004234 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004235 BUG_ON(!em);
4236
4237 BUG_ON(em->start > logical || em->start + em->len < logical);
4238 map = (struct map_lookup *)em->bdev;
4239 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4240 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004241 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4242 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004243 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4244 ret = 2;
4245 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4246 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004247 else
4248 ret = 1;
4249 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004250
4251 btrfs_dev_replace_lock(&fs_info->dev_replace);
4252 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4253 ret++;
4254 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4255
Chris Masonf1885912008-04-09 16:28:12 -04004256 return ret;
4257}
4258
David Woodhouse53b381b2013-01-29 18:40:14 -05004259unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4260 struct btrfs_mapping_tree *map_tree,
4261 u64 logical)
4262{
4263 struct extent_map *em;
4264 struct map_lookup *map;
4265 struct extent_map_tree *em_tree = &map_tree->map_tree;
4266 unsigned long len = root->sectorsize;
4267
4268 read_lock(&em_tree->lock);
4269 em = lookup_extent_mapping(em_tree, logical, len);
4270 read_unlock(&em_tree->lock);
4271 BUG_ON(!em);
4272
4273 BUG_ON(em->start > logical || em->start + em->len < logical);
4274 map = (struct map_lookup *)em->bdev;
4275 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4276 BTRFS_BLOCK_GROUP_RAID6)) {
4277 len = map->stripe_len * nr_data_stripes(map);
4278 }
4279 free_extent_map(em);
4280 return len;
4281}
4282
4283int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4284 u64 logical, u64 len, int mirror_num)
4285{
4286 struct extent_map *em;
4287 struct map_lookup *map;
4288 struct extent_map_tree *em_tree = &map_tree->map_tree;
4289 int ret = 0;
4290
4291 read_lock(&em_tree->lock);
4292 em = lookup_extent_mapping(em_tree, logical, len);
4293 read_unlock(&em_tree->lock);
4294 BUG_ON(!em);
4295
4296 BUG_ON(em->start > logical || em->start + em->len < logical);
4297 map = (struct map_lookup *)em->bdev;
4298 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4299 BTRFS_BLOCK_GROUP_RAID6))
4300 ret = 1;
4301 free_extent_map(em);
4302 return ret;
4303}
4304
Stefan Behrens30d98612012-11-06 14:52:18 +01004305static int find_live_mirror(struct btrfs_fs_info *fs_info,
4306 struct map_lookup *map, int first, int num,
4307 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004308{
4309 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004310 int tolerance;
4311 struct btrfs_device *srcdev;
4312
4313 if (dev_replace_is_ongoing &&
4314 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4315 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4316 srcdev = fs_info->dev_replace.srcdev;
4317 else
4318 srcdev = NULL;
4319
4320 /*
4321 * try to avoid the drive that is the source drive for a
4322 * dev-replace procedure, only choose it if no other non-missing
4323 * mirror is available
4324 */
4325 for (tolerance = 0; tolerance < 2; tolerance++) {
4326 if (map->stripes[optimal].dev->bdev &&
4327 (tolerance || map->stripes[optimal].dev != srcdev))
4328 return optimal;
4329 for (i = first; i < first + num; i++) {
4330 if (map->stripes[i].dev->bdev &&
4331 (tolerance || map->stripes[i].dev != srcdev))
4332 return i;
4333 }
Chris Masondfe25022008-05-13 13:46:40 -04004334 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004335
Chris Masondfe25022008-05-13 13:46:40 -04004336 /* we couldn't find one that doesn't fail. Just return something
4337 * and the io error handling code will clean up eventually
4338 */
4339 return optimal;
4340}
4341
David Woodhouse53b381b2013-01-29 18:40:14 -05004342static inline int parity_smaller(u64 a, u64 b)
4343{
4344 return a > b;
4345}
4346
4347/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4348static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4349{
4350 struct btrfs_bio_stripe s;
4351 int i;
4352 u64 l;
4353 int again = 1;
4354
4355 while (again) {
4356 again = 0;
4357 for (i = 0; i < bbio->num_stripes - 1; i++) {
4358 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4359 s = bbio->stripes[i];
4360 l = raid_map[i];
4361 bbio->stripes[i] = bbio->stripes[i+1];
4362 raid_map[i] = raid_map[i+1];
4363 bbio->stripes[i+1] = s;
4364 raid_map[i+1] = l;
4365 again = 1;
4366 }
4367 }
4368 }
4369}
4370
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004371static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004372 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004373 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004374 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004375{
4376 struct extent_map *em;
4377 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004378 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004379 struct extent_map_tree *em_tree = &map_tree->map_tree;
4380 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004381 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004382 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004383 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004384 u64 stripe_nr_orig;
4385 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004386 u64 stripe_len;
4387 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004388 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004389 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004390 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004391 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004392 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004393 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004394 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4395 int dev_replace_is_ongoing = 0;
4396 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004397 int patch_the_first_stripe_for_dev_replace = 0;
4398 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004399 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004400
Chris Mason890871b2009-09-02 16:24:52 -04004401 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004402 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004403 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004404
Chris Mason3b951512008-04-17 11:29:12 -04004405 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004406 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
4407 (unsigned long long)logical,
4408 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04004409 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04004410 }
Chris Mason0b86a832008-03-24 15:01:56 -04004411
4412 BUG_ON(em->start > logical || em->start + em->len < logical);
4413 map = (struct map_lookup *)em->bdev;
4414 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004415
David Woodhouse53b381b2013-01-29 18:40:14 -05004416 if (mirror_num > map->num_stripes)
4417 mirror_num = 0;
4418
4419 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004420 stripe_nr = offset;
4421 /*
4422 * stripe_nr counts the total number of stripes we have to stride
4423 * to get to this block
4424 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004425 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004426
David Woodhouse53b381b2013-01-29 18:40:14 -05004427 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004428 BUG_ON(offset < stripe_offset);
4429
4430 /* stripe_offset is the offset of this block in its stripe*/
4431 stripe_offset = offset - stripe_offset;
4432
David Woodhouse53b381b2013-01-29 18:40:14 -05004433 /* if we're here for raid56, we need to know the stripe aligned start */
4434 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4435 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4436 raid56_full_stripe_start = offset;
4437
4438 /* allow a write of a full stripe, but make sure we don't
4439 * allow straddling of stripes
4440 */
4441 do_div(raid56_full_stripe_start, full_stripe_len);
4442 raid56_full_stripe_start *= full_stripe_len;
4443 }
4444
4445 if (rw & REQ_DISCARD) {
4446 /* we don't discard raid56 yet */
4447 if (map->type &
4448 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4449 ret = -EOPNOTSUPP;
4450 goto out;
4451 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004452 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004453 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4454 u64 max_len;
4455 /* For writes to RAID[56], allow a full stripeset across all disks.
4456 For other RAID types and for RAID[56] reads, just allow a single
4457 stripe (on a single disk). */
4458 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4459 (rw & REQ_WRITE)) {
4460 max_len = stripe_len * nr_data_stripes(map) -
4461 (offset - raid56_full_stripe_start);
4462 } else {
4463 /* we limit the length of each bio to what fits in a stripe */
4464 max_len = stripe_len - stripe_offset;
4465 }
4466 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004467 } else {
4468 *length = em->len - offset;
4469 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004470
David Woodhouse53b381b2013-01-29 18:40:14 -05004471 /* This is for when we're called from btrfs_merge_bio_hook() and all
4472 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004473 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004474 goto out;
4475
Stefan Behrens472262f2012-11-06 14:43:46 +01004476 btrfs_dev_replace_lock(dev_replace);
4477 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4478 if (!dev_replace_is_ongoing)
4479 btrfs_dev_replace_unlock(dev_replace);
4480
Stefan Behrensad6d6202012-11-06 15:06:47 +01004481 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4482 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4483 dev_replace->tgtdev != NULL) {
4484 /*
4485 * in dev-replace case, for repair case (that's the only
4486 * case where the mirror is selected explicitly when
4487 * calling btrfs_map_block), blocks left of the left cursor
4488 * can also be read from the target drive.
4489 * For REQ_GET_READ_MIRRORS, the target drive is added as
4490 * the last one to the array of stripes. For READ, it also
4491 * needs to be supported using the same mirror number.
4492 * If the requested block is not left of the left cursor,
4493 * EIO is returned. This can happen because btrfs_num_copies()
4494 * returns one more in the dev-replace case.
4495 */
4496 u64 tmp_length = *length;
4497 struct btrfs_bio *tmp_bbio = NULL;
4498 int tmp_num_stripes;
4499 u64 srcdev_devid = dev_replace->srcdev->devid;
4500 int index_srcdev = 0;
4501 int found = 0;
4502 u64 physical_of_found = 0;
4503
4504 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004505 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004506 if (ret) {
4507 WARN_ON(tmp_bbio != NULL);
4508 goto out;
4509 }
4510
4511 tmp_num_stripes = tmp_bbio->num_stripes;
4512 if (mirror_num > tmp_num_stripes) {
4513 /*
4514 * REQ_GET_READ_MIRRORS does not contain this
4515 * mirror, that means that the requested area
4516 * is not left of the left cursor
4517 */
4518 ret = -EIO;
4519 kfree(tmp_bbio);
4520 goto out;
4521 }
4522
4523 /*
4524 * process the rest of the function using the mirror_num
4525 * of the source drive. Therefore look it up first.
4526 * At the end, patch the device pointer to the one of the
4527 * target drive.
4528 */
4529 for (i = 0; i < tmp_num_stripes; i++) {
4530 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4531 /*
4532 * In case of DUP, in order to keep it
4533 * simple, only add the mirror with the
4534 * lowest physical address
4535 */
4536 if (found &&
4537 physical_of_found <=
4538 tmp_bbio->stripes[i].physical)
4539 continue;
4540 index_srcdev = i;
4541 found = 1;
4542 physical_of_found =
4543 tmp_bbio->stripes[i].physical;
4544 }
4545 }
4546
4547 if (found) {
4548 mirror_num = index_srcdev + 1;
4549 patch_the_first_stripe_for_dev_replace = 1;
4550 physical_to_patch_in_first_stripe = physical_of_found;
4551 } else {
4552 WARN_ON(1);
4553 ret = -EIO;
4554 kfree(tmp_bbio);
4555 goto out;
4556 }
4557
4558 kfree(tmp_bbio);
4559 } else if (mirror_num > map->num_stripes) {
4560 mirror_num = 0;
4561 }
4562
Chris Masonf2d8d742008-04-21 10:03:05 -04004563 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004564 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004565 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004566 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004567 do_div(stripe_nr_end, map->stripe_len);
4568 stripe_end_offset = stripe_nr_end * map->stripe_len -
4569 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004570
Li Dongyangfce3bb92011-03-24 10:24:26 +00004571 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4572 if (rw & REQ_DISCARD)
4573 num_stripes = min_t(u64, map->num_stripes,
4574 stripe_nr_end - stripe_nr_orig);
4575 stripe_index = do_div(stripe_nr, map->num_stripes);
4576 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004577 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004578 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004579 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004580 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004581 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004582 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004583 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004584 current->pid % map->num_stripes,
4585 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004586 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004587 }
Chris Mason2fff7342008-04-29 14:12:09 -04004588
Chris Mason611f0e02008-04-03 16:29:03 -04004589 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004590 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004591 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004592 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004593 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004594 } else {
4595 mirror_num = 1;
4596 }
Chris Mason2fff7342008-04-29 14:12:09 -04004597
Chris Mason321aecc2008-04-16 10:49:51 -04004598 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4599 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004600
4601 stripe_index = do_div(stripe_nr, factor);
4602 stripe_index *= map->sub_stripes;
4603
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004604 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004605 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004606 else if (rw & REQ_DISCARD)
4607 num_stripes = min_t(u64, map->sub_stripes *
4608 (stripe_nr_end - stripe_nr_orig),
4609 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004610 else if (mirror_num)
4611 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004612 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004613 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004614 stripe_index = find_live_mirror(fs_info, map,
4615 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004616 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004617 current->pid % map->sub_stripes,
4618 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004619 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004620 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004621
4622 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4623 BTRFS_BLOCK_GROUP_RAID6)) {
4624 u64 tmp;
4625
4626 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4627 && raid_map_ret) {
4628 int i, rot;
4629
4630 /* push stripe_nr back to the start of the full stripe */
4631 stripe_nr = raid56_full_stripe_start;
4632 do_div(stripe_nr, stripe_len);
4633
4634 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4635
4636 /* RAID[56] write or recovery. Return all stripes */
4637 num_stripes = map->num_stripes;
4638 max_errors = nr_parity_stripes(map);
4639
4640 raid_map = kmalloc(sizeof(u64) * num_stripes,
4641 GFP_NOFS);
4642 if (!raid_map) {
4643 ret = -ENOMEM;
4644 goto out;
4645 }
4646
4647 /* Work out the disk rotation on this stripe-set */
4648 tmp = stripe_nr;
4649 rot = do_div(tmp, num_stripes);
4650
4651 /* Fill in the logical address of each stripe */
4652 tmp = stripe_nr * nr_data_stripes(map);
4653 for (i = 0; i < nr_data_stripes(map); i++)
4654 raid_map[(i+rot) % num_stripes] =
4655 em->start + (tmp + i) * map->stripe_len;
4656
4657 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4658 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4659 raid_map[(i+rot+1) % num_stripes] =
4660 RAID6_Q_STRIPE;
4661
4662 *length = map->stripe_len;
4663 stripe_index = 0;
4664 stripe_offset = 0;
4665 } else {
4666 /*
4667 * Mirror #0 or #1 means the original data block.
4668 * Mirror #2 is RAID5 parity block.
4669 * Mirror #3 is RAID6 Q block.
4670 */
4671 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4672 if (mirror_num > 1)
4673 stripe_index = nr_data_stripes(map) +
4674 mirror_num - 2;
4675
4676 /* We distribute the parity blocks across stripes */
4677 tmp = stripe_nr + stripe_index;
4678 stripe_index = do_div(tmp, map->num_stripes);
4679 }
Chris Mason8790d502008-04-03 16:29:03 -04004680 } else {
4681 /*
4682 * after this do_div call, stripe_nr is the number of stripes
4683 * on this device we have to walk to find the data, and
4684 * stripe_index is the number of our device in the stripe array
4685 */
4686 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004687 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004688 }
Chris Mason593060d2008-03-25 16:50:33 -04004689 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004690
Stefan Behrens472262f2012-11-06 14:43:46 +01004691 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004692 if (dev_replace_is_ongoing) {
4693 if (rw & (REQ_WRITE | REQ_DISCARD))
4694 num_alloc_stripes <<= 1;
4695 if (rw & REQ_GET_READ_MIRRORS)
4696 num_alloc_stripes++;
4697 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004698 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08004699 if (!bbio) {
4700 ret = -ENOMEM;
4701 goto out;
4702 }
4703 atomic_set(&bbio->error, 0);
4704
Li Dongyangfce3bb92011-03-24 10:24:26 +00004705 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004706 int factor = 0;
4707 int sub_stripes = 0;
4708 u64 stripes_per_dev = 0;
4709 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004710 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004711
4712 if (map->type &
4713 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4714 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4715 sub_stripes = 1;
4716 else
4717 sub_stripes = map->sub_stripes;
4718
4719 factor = map->num_stripes / sub_stripes;
4720 stripes_per_dev = div_u64_rem(stripe_nr_end -
4721 stripe_nr_orig,
4722 factor,
4723 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004724 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4725 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004726 }
4727
Li Dongyangfce3bb92011-03-24 10:24:26 +00004728 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004729 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004730 map->stripes[stripe_index].physical +
4731 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004732 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004733
Li Zefanec9ef7a2011-12-01 14:06:42 +08004734 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4735 BTRFS_BLOCK_GROUP_RAID10)) {
4736 bbio->stripes[i].length = stripes_per_dev *
4737 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004738
Li Zefanec9ef7a2011-12-01 14:06:42 +08004739 if (i / sub_stripes < remaining_stripes)
4740 bbio->stripes[i].length +=
4741 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004742
4743 /*
4744 * Special for the first stripe and
4745 * the last stripe:
4746 *
4747 * |-------|...|-------|
4748 * |----------|
4749 * off end_off
4750 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004751 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004752 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004753 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004754
4755 if (stripe_index >= last_stripe &&
4756 stripe_index <= (last_stripe +
4757 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004758 bbio->stripes[i].length -=
4759 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004760
Li Zefanec9ef7a2011-12-01 14:06:42 +08004761 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004762 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004763 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004764 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004765
4766 stripe_index++;
4767 if (stripe_index == map->num_stripes) {
4768 /* This could only happen for RAID0/10 */
4769 stripe_index = 0;
4770 stripe_nr++;
4771 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004772 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004773 } else {
4774 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004775 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004776 map->stripes[stripe_index].physical +
4777 stripe_offset +
4778 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004779 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004780 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004781 stripe_index++;
4782 }
Chris Mason593060d2008-03-25 16:50:33 -04004783 }
Li Zefande11cc12011-12-01 12:55:47 +08004784
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004785 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08004786 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4787 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05004788 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08004789 BTRFS_BLOCK_GROUP_DUP)) {
4790 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05004791 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4792 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08004793 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004794 }
Li Zefande11cc12011-12-01 12:55:47 +08004795
Stefan Behrens472262f2012-11-06 14:43:46 +01004796 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4797 dev_replace->tgtdev != NULL) {
4798 int index_where_to_add;
4799 u64 srcdev_devid = dev_replace->srcdev->devid;
4800
4801 /*
4802 * duplicate the write operations while the dev replace
4803 * procedure is running. Since the copying of the old disk
4804 * to the new disk takes place at run time while the
4805 * filesystem is mounted writable, the regular write
4806 * operations to the old disk have to be duplicated to go
4807 * to the new disk as well.
4808 * Note that device->missing is handled by the caller, and
4809 * that the write to the old disk is already set up in the
4810 * stripes array.
4811 */
4812 index_where_to_add = num_stripes;
4813 for (i = 0; i < num_stripes; i++) {
4814 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4815 /* write to new disk, too */
4816 struct btrfs_bio_stripe *new =
4817 bbio->stripes + index_where_to_add;
4818 struct btrfs_bio_stripe *old =
4819 bbio->stripes + i;
4820
4821 new->physical = old->physical;
4822 new->length = old->length;
4823 new->dev = dev_replace->tgtdev;
4824 index_where_to_add++;
4825 max_errors++;
4826 }
4827 }
4828 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004829 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4830 dev_replace->tgtdev != NULL) {
4831 u64 srcdev_devid = dev_replace->srcdev->devid;
4832 int index_srcdev = 0;
4833 int found = 0;
4834 u64 physical_of_found = 0;
4835
4836 /*
4837 * During the dev-replace procedure, the target drive can
4838 * also be used to read data in case it is needed to repair
4839 * a corrupt block elsewhere. This is possible if the
4840 * requested area is left of the left cursor. In this area,
4841 * the target drive is a full copy of the source drive.
4842 */
4843 for (i = 0; i < num_stripes; i++) {
4844 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4845 /*
4846 * In case of DUP, in order to keep it
4847 * simple, only add the mirror with the
4848 * lowest physical address
4849 */
4850 if (found &&
4851 physical_of_found <=
4852 bbio->stripes[i].physical)
4853 continue;
4854 index_srcdev = i;
4855 found = 1;
4856 physical_of_found = bbio->stripes[i].physical;
4857 }
4858 }
4859 if (found) {
4860 u64 length = map->stripe_len;
4861
4862 if (physical_of_found + length <=
4863 dev_replace->cursor_left) {
4864 struct btrfs_bio_stripe *tgtdev_stripe =
4865 bbio->stripes + num_stripes;
4866
4867 tgtdev_stripe->physical = physical_of_found;
4868 tgtdev_stripe->length =
4869 bbio->stripes[index_srcdev].length;
4870 tgtdev_stripe->dev = dev_replace->tgtdev;
4871
4872 num_stripes++;
4873 }
4874 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004875 }
4876
Li Zefande11cc12011-12-01 12:55:47 +08004877 *bbio_ret = bbio;
4878 bbio->num_stripes = num_stripes;
4879 bbio->max_errors = max_errors;
4880 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004881
4882 /*
4883 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4884 * mirror_num == num_stripes + 1 && dev_replace target drive is
4885 * available as a mirror
4886 */
4887 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
4888 WARN_ON(num_stripes > 1);
4889 bbio->stripes[0].dev = dev_replace->tgtdev;
4890 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
4891 bbio->mirror_num = map->num_stripes + 1;
4892 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004893 if (raid_map) {
4894 sort_parity_stripes(bbio, raid_map);
4895 *raid_map_ret = raid_map;
4896 }
Chris Masoncea9e442008-04-09 16:28:12 -04004897out:
Stefan Behrens472262f2012-11-06 14:43:46 +01004898 if (dev_replace_is_ongoing)
4899 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04004900 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004901 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004902}
4903
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004904int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004905 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004906 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004907{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004908 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004909 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04004910}
4911
Yan Zhenga512bbf2008-12-08 16:46:26 -05004912int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4913 u64 chunk_start, u64 physical, u64 devid,
4914 u64 **logical, int *naddrs, int *stripe_len)
4915{
4916 struct extent_map_tree *em_tree = &map_tree->map_tree;
4917 struct extent_map *em;
4918 struct map_lookup *map;
4919 u64 *buf;
4920 u64 bytenr;
4921 u64 length;
4922 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05004923 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05004924 int i, j, nr = 0;
4925
Chris Mason890871b2009-09-02 16:24:52 -04004926 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004927 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004928 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004929
Josef Bacik835d9742013-03-19 12:13:25 -04004930 if (!em) {
4931 printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
4932 chunk_start);
4933 return -EIO;
4934 }
4935
4936 if (em->start != chunk_start) {
4937 printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
4938 em->start, chunk_start);
4939 free_extent_map(em);
4940 return -EIO;
4941 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004942 map = (struct map_lookup *)em->bdev;
4943
4944 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05004945 rmap_len = map->stripe_len;
4946
Yan Zhenga512bbf2008-12-08 16:46:26 -05004947 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4948 do_div(length, map->num_stripes / map->sub_stripes);
4949 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4950 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05004951 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4952 BTRFS_BLOCK_GROUP_RAID6)) {
4953 do_div(length, nr_data_stripes(map));
4954 rmap_len = map->stripe_len * nr_data_stripes(map);
4955 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004956
4957 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004958 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004959
4960 for (i = 0; i < map->num_stripes; i++) {
4961 if (devid && map->stripes[i].dev->devid != devid)
4962 continue;
4963 if (map->stripes[i].physical > physical ||
4964 map->stripes[i].physical + length <= physical)
4965 continue;
4966
4967 stripe_nr = physical - map->stripes[i].physical;
4968 do_div(stripe_nr, map->stripe_len);
4969
4970 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4971 stripe_nr = stripe_nr * map->num_stripes + i;
4972 do_div(stripe_nr, map->sub_stripes);
4973 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4974 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05004975 } /* else if RAID[56], multiply by nr_data_stripes().
4976 * Alternatively, just use rmap_len below instead of
4977 * map->stripe_len */
4978
4979 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05004980 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004981 for (j = 0; j < nr; j++) {
4982 if (buf[j] == bytenr)
4983 break;
4984 }
Chris Mason934d3752008-12-08 16:43:10 -05004985 if (j == nr) {
4986 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004987 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004988 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004989 }
4990
Yan Zhenga512bbf2008-12-08 16:46:26 -05004991 *logical = buf;
4992 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05004993 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05004994
4995 free_extent_map(em);
4996 return 0;
4997}
4998
Stefan Behrens442a4f62012-05-25 16:06:08 +02004999static void *merge_stripe_index_into_bio_private(void *bi_private,
5000 unsigned int stripe_index)
5001{
5002 /*
5003 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
5004 * at most 1.
5005 * The alternative solution (instead of stealing bits from the
5006 * pointer) would be to allocate an intermediate structure
5007 * that contains the old private pointer plus the stripe_index.
5008 */
5009 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
5010 BUG_ON(stripe_index > 3);
5011 return (void *)(((uintptr_t)bi_private) | stripe_index);
5012}
5013
5014static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
5015{
5016 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
5017}
5018
5019static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
5020{
5021 return (unsigned int)((uintptr_t)bi_private) & 3;
5022}
5023
Jan Schmidta1d3c472011-08-04 17:15:33 +02005024static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005025{
Stefan Behrens442a4f62012-05-25 16:06:08 +02005026 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005027 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005028
Stefan Behrens442a4f62012-05-25 16:06:08 +02005029 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005030 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005031 if (err == -EIO || err == -EREMOTEIO) {
5032 unsigned int stripe_index =
5033 extract_stripe_index_from_bio_private(
5034 bio->bi_private);
5035 struct btrfs_device *dev;
5036
5037 BUG_ON(stripe_index >= bbio->num_stripes);
5038 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005039 if (dev->bdev) {
5040 if (bio->bi_rw & WRITE)
5041 btrfs_dev_stat_inc(dev,
5042 BTRFS_DEV_STAT_WRITE_ERRS);
5043 else
5044 btrfs_dev_stat_inc(dev,
5045 BTRFS_DEV_STAT_READ_ERRS);
5046 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5047 btrfs_dev_stat_inc(dev,
5048 BTRFS_DEV_STAT_FLUSH_ERRS);
5049 btrfs_dev_stat_print_on_error(dev);
5050 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005051 }
5052 }
Chris Mason8790d502008-04-03 16:29:03 -04005053
Jan Schmidta1d3c472011-08-04 17:15:33 +02005054 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005055 is_orig_bio = 1;
5056
Jan Schmidta1d3c472011-08-04 17:15:33 +02005057 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005058 if (!is_orig_bio) {
5059 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005060 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005061 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005062 bio->bi_private = bbio->private;
5063 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02005064 bio->bi_bdev = (struct block_device *)
5065 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005066 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005067 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005068 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005069 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005070 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005071 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005072 /*
5073 * this bio is actually up to date, we didn't
5074 * go over the max number of errors
5075 */
5076 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005077 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005078 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005079 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04005080
5081 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005082 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005083 bio_put(bio);
5084 }
Chris Mason8790d502008-04-03 16:29:03 -04005085}
5086
Chris Mason8b712842008-06-11 16:50:36 -04005087struct async_sched {
5088 struct bio *bio;
5089 int rw;
5090 struct btrfs_fs_info *info;
5091 struct btrfs_work work;
5092};
5093
5094/*
5095 * see run_scheduled_bios for a description of why bios are collected for
5096 * async submit.
5097 *
5098 * This will add one bio to the pending list for a device and make sure
5099 * the work struct is scheduled.
5100 */
David Woodhouse53b381b2013-01-29 18:40:14 -05005101noinline void btrfs_schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04005102 struct btrfs_device *device,
5103 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005104{
5105 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005106 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005107
David Woodhouse53b381b2013-01-29 18:40:14 -05005108 if (device->missing || !device->bdev) {
5109 bio_endio(bio, -EIO);
5110 return;
5111 }
5112
Chris Mason8b712842008-06-11 16:50:36 -04005113 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005114 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005115 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005116 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005117 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005118 return;
Chris Mason8b712842008-06-11 16:50:36 -04005119 }
5120
5121 /*
Chris Mason0986fe92008-08-15 15:34:15 -04005122 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005123 * higher layers. Otherwise, the async bio makes it appear we have
5124 * made progress against dirty pages when we've really just put it
5125 * on a queue for later
5126 */
Chris Mason0986fe92008-08-15 15:34:15 -04005127 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005128 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005129 bio->bi_next = NULL;
5130 bio->bi_rw |= rw;
5131
5132 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005133 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005134 pending_bios = &device->pending_sync_bios;
5135 else
5136 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005137
Chris Masonffbd5172009-04-20 15:50:09 -04005138 if (pending_bios->tail)
5139 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005140
Chris Masonffbd5172009-04-20 15:50:09 -04005141 pending_bios->tail = bio;
5142 if (!pending_bios->head)
5143 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005144 if (device->running_pending)
5145 should_queue = 0;
5146
5147 spin_unlock(&device->io_lock);
5148
5149 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04005150 btrfs_queue_worker(&root->fs_info->submit_workers,
5151 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005152}
5153
Josef Bacikde1ee922012-10-19 16:50:56 -04005154static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5155 sector_t sector)
5156{
5157 struct bio_vec *prev;
5158 struct request_queue *q = bdev_get_queue(bdev);
5159 unsigned short max_sectors = queue_max_sectors(q);
5160 struct bvec_merge_data bvm = {
5161 .bi_bdev = bdev,
5162 .bi_sector = sector,
5163 .bi_rw = bio->bi_rw,
5164 };
5165
5166 if (bio->bi_vcnt == 0) {
5167 WARN_ON(1);
5168 return 1;
5169 }
5170
5171 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
5172 if ((bio->bi_size >> 9) > max_sectors)
5173 return 0;
5174
5175 if (!q->merge_bvec_fn)
5176 return 1;
5177
5178 bvm.bi_size = bio->bi_size - prev->bv_len;
5179 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5180 return 0;
5181 return 1;
5182}
5183
5184static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5185 struct bio *bio, u64 physical, int dev_nr,
5186 int rw, int async)
5187{
5188 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5189
5190 bio->bi_private = bbio;
5191 bio->bi_private = merge_stripe_index_into_bio_private(
5192 bio->bi_private, (unsigned int)dev_nr);
5193 bio->bi_end_io = btrfs_end_bio;
5194 bio->bi_sector = physical >> 9;
5195#ifdef DEBUG
5196 {
5197 struct rcu_string *name;
5198
5199 rcu_read_lock();
5200 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005201 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005202 "(%s id %llu), size=%u\n", rw,
5203 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5204 name->str, dev->devid, bio->bi_size);
5205 rcu_read_unlock();
5206 }
5207#endif
5208 bio->bi_bdev = dev->bdev;
5209 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005210 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005211 else
5212 btrfsic_submit_bio(rw, bio);
5213}
5214
5215static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5216 struct bio *first_bio, struct btrfs_device *dev,
5217 int dev_nr, int rw, int async)
5218{
5219 struct bio_vec *bvec = first_bio->bi_io_vec;
5220 struct bio *bio;
5221 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5222 u64 physical = bbio->stripes[dev_nr].physical;
5223
5224again:
5225 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5226 if (!bio)
5227 return -ENOMEM;
5228
5229 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5230 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5231 bvec->bv_offset) < bvec->bv_len) {
5232 u64 len = bio->bi_size;
5233
5234 atomic_inc(&bbio->stripes_pending);
5235 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5236 rw, async);
5237 physical += len;
5238 goto again;
5239 }
5240 bvec++;
5241 }
5242
5243 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5244 return 0;
5245}
5246
5247static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5248{
5249 atomic_inc(&bbio->error);
5250 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5251 bio->bi_private = bbio->private;
5252 bio->bi_end_io = bbio->end_io;
5253 bio->bi_bdev = (struct block_device *)
5254 (unsigned long)bbio->mirror_num;
5255 bio->bi_sector = logical >> 9;
5256 kfree(bbio);
5257 bio_endio(bio, -EIO);
5258 }
5259}
5260
Chris Masonf1885912008-04-09 16:28:12 -04005261int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005262 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005263{
Chris Mason0b86a832008-03-24 15:01:56 -04005264 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005265 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04005266 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005267 u64 length = 0;
5268 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005269 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005270 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005271 int dev_nr = 0;
5272 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005273 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005274
Chris Masonf2d8d742008-04-21 10:03:05 -04005275 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005276 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005277
David Woodhouse53b381b2013-01-29 18:40:14 -05005278 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5279 mirror_num, &raid_map);
5280 if (ret) /* -ENOMEM */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005281 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04005282
Jan Schmidta1d3c472011-08-04 17:15:33 +02005283 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005284 bbio->orig_bio = first_bio;
5285 bbio->private = first_bio->bi_private;
5286 bbio->end_io = first_bio->bi_end_io;
5287 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5288
5289 if (raid_map) {
5290 /* In this case, map_length has been set to the length of
5291 a single stripe; not the whole write */
5292 if (rw & WRITE) {
5293 return raid56_parity_write(root, bio, bbio,
5294 raid_map, map_length);
5295 } else {
5296 return raid56_parity_recover(root, bio, bbio,
5297 raid_map, map_length,
5298 mirror_num);
5299 }
5300 }
5301
Chris Masoncea9e442008-04-09 16:28:12 -04005302 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005303 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
5304 (unsigned long long)logical,
5305 (unsigned long long)length,
5306 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005307 BUG();
5308 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005309
Chris Masond3977122009-01-05 21:25:51 -05005310 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005311 dev = bbio->stripes[dev_nr].dev;
5312 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5313 bbio_error(bbio, first_bio, logical);
5314 dev_nr++;
5315 continue;
5316 }
5317
5318 /*
5319 * Check and see if we're ok with this bio based on it's size
5320 * and offset with the given device.
5321 */
5322 if (!bio_size_ok(dev->bdev, first_bio,
5323 bbio->stripes[dev_nr].physical >> 9)) {
5324 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5325 dev_nr, rw, async_submit);
5326 BUG_ON(ret);
5327 dev_nr++;
5328 continue;
5329 }
5330
Jan Schmidta1d3c472011-08-04 17:15:33 +02005331 if (dev_nr < total_devs - 1) {
5332 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005333 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005334 } else {
5335 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04005336 }
Josef Bacik606686e2012-06-04 14:03:51 -04005337
Josef Bacikde1ee922012-10-19 16:50:56 -04005338 submit_stripe_bio(root, bbio, bio,
5339 bbio->stripes[dev_nr].physical, dev_nr, rw,
5340 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005341 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005342 }
Chris Mason0b86a832008-03-24 15:01:56 -04005343 return 0;
5344}
5345
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005346struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005347 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005348{
Yan Zheng2b820322008-11-17 21:11:30 -05005349 struct btrfs_device *device;
5350 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005351
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005352 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005353 while (cur_devices) {
5354 if (!fsid ||
5355 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5356 device = __find_device(&cur_devices->devices,
5357 devid, uuid);
5358 if (device)
5359 return device;
5360 }
5361 cur_devices = cur_devices->seed;
5362 }
5363 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005364}
5365
Chris Masondfe25022008-05-13 13:46:40 -04005366static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5367 u64 devid, u8 *dev_uuid)
5368{
5369 struct btrfs_device *device;
5370 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5371
5372 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005373 if (!device)
5374 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04005375 list_add(&device->dev_list,
5376 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04005377 device->dev_root = root->fs_info->dev_root;
5378 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04005379 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05005380 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05005381 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04005382 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05005383 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04005384 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05005385 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04005386 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
5387 return device;
5388}
5389
Chris Mason0b86a832008-03-24 15:01:56 -04005390static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5391 struct extent_buffer *leaf,
5392 struct btrfs_chunk *chunk)
5393{
5394 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5395 struct map_lookup *map;
5396 struct extent_map *em;
5397 u64 logical;
5398 u64 length;
5399 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005400 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005401 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005402 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005403 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005404
Chris Masone17cade2008-04-15 15:41:47 -04005405 logical = key->offset;
5406 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005407
Chris Mason890871b2009-09-02 16:24:52 -04005408 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005409 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005410 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005411
5412 /* already mapped? */
5413 if (em && em->start <= logical && em->start + em->len > logical) {
5414 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005415 return 0;
5416 } else if (em) {
5417 free_extent_map(em);
5418 }
Chris Mason0b86a832008-03-24 15:01:56 -04005419
David Sterba172ddd62011-04-21 00:48:27 +02005420 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005421 if (!em)
5422 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005423 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5424 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005425 if (!map) {
5426 free_extent_map(em);
5427 return -ENOMEM;
5428 }
5429
5430 em->bdev = (struct block_device *)map;
5431 em->start = logical;
5432 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005433 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005434 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005435 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005436
Chris Mason593060d2008-03-25 16:50:33 -04005437 map->num_stripes = num_stripes;
5438 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5439 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5440 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5441 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5442 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005443 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005444 for (i = 0; i < num_stripes; i++) {
5445 map->stripes[i].physical =
5446 btrfs_stripe_offset_nr(leaf, chunk, i);
5447 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005448 read_extent_buffer(leaf, uuid, (unsigned long)
5449 btrfs_stripe_dev_uuid_nr(chunk, i),
5450 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005451 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5452 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005453 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005454 kfree(map);
5455 free_extent_map(em);
5456 return -EIO;
5457 }
Chris Masondfe25022008-05-13 13:46:40 -04005458 if (!map->stripes[i].dev) {
5459 map->stripes[i].dev =
5460 add_missing_dev(root, devid, uuid);
5461 if (!map->stripes[i].dev) {
5462 kfree(map);
5463 free_extent_map(em);
5464 return -EIO;
5465 }
5466 }
5467 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005468 }
5469
Chris Mason890871b2009-09-02 16:24:52 -04005470 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005471 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005472 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005473 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005474 free_extent_map(em);
5475
5476 return 0;
5477}
5478
Jeff Mahoney143bede2012-03-01 14:56:26 +01005479static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005480 struct btrfs_dev_item *dev_item,
5481 struct btrfs_device *device)
5482{
5483 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005484
5485 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005486 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5487 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005488 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5489 device->type = btrfs_device_type(leaf, dev_item);
5490 device->io_align = btrfs_device_io_align(leaf, dev_item);
5491 device->io_width = btrfs_device_io_width(leaf, dev_item);
5492 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005493 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005494 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005495
5496 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005497 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005498}
5499
Yan Zheng2b820322008-11-17 21:11:30 -05005500static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5501{
5502 struct btrfs_fs_devices *fs_devices;
5503 int ret;
5504
Li Zefanb367e472011-12-07 11:38:24 +08005505 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005506
5507 fs_devices = root->fs_info->fs_devices->seed;
5508 while (fs_devices) {
5509 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5510 ret = 0;
5511 goto out;
5512 }
5513 fs_devices = fs_devices->seed;
5514 }
5515
5516 fs_devices = find_fsid(fsid);
5517 if (!fs_devices) {
5518 ret = -ENOENT;
5519 goto out;
5520 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005521
5522 fs_devices = clone_fs_devices(fs_devices);
5523 if (IS_ERR(fs_devices)) {
5524 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005525 goto out;
5526 }
5527
Christoph Hellwig97288f22008-12-02 06:36:09 -05005528 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005529 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005530 if (ret) {
5531 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005532 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005533 }
Yan Zheng2b820322008-11-17 21:11:30 -05005534
5535 if (!fs_devices->seeding) {
5536 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005537 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005538 ret = -EINVAL;
5539 goto out;
5540 }
5541
5542 fs_devices->seed = root->fs_info->fs_devices->seed;
5543 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005544out:
Yan Zheng2b820322008-11-17 21:11:30 -05005545 return ret;
5546}
5547
Chris Mason0d81ba52008-03-24 15:02:07 -04005548static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005549 struct extent_buffer *leaf,
5550 struct btrfs_dev_item *dev_item)
5551{
5552 struct btrfs_device *device;
5553 u64 devid;
5554 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005555 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005556 u8 dev_uuid[BTRFS_UUID_SIZE];
5557
Chris Mason0b86a832008-03-24 15:01:56 -04005558 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04005559 read_extent_buffer(leaf, dev_uuid,
5560 (unsigned long)btrfs_device_uuid(dev_item),
5561 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05005562 read_extent_buffer(leaf, fs_uuid,
5563 (unsigned long)btrfs_device_fsid(dev_item),
5564 BTRFS_UUID_SIZE);
5565
5566 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5567 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005568 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005569 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005570 }
5571
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005572 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005573 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005574 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005575 return -EIO;
5576
5577 if (!device) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005578 btrfs_warn(root->fs_info, "devid %llu missing",
5579 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005580 device = add_missing_dev(root, devid, dev_uuid);
5581 if (!device)
5582 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005583 } else if (!device->missing) {
5584 /*
5585 * this happens when a device that was properly setup
5586 * in the device info lists suddenly goes bad.
5587 * device->bdev is NULL, and so we have to set
5588 * device->missing to one here
5589 */
5590 root->fs_info->fs_devices->missing_devices++;
5591 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05005592 }
5593 }
5594
5595 if (device->fs_devices != root->fs_info->fs_devices) {
5596 BUG_ON(device->writeable);
5597 if (device->generation !=
5598 btrfs_device_generation(leaf, dev_item))
5599 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04005600 }
Chris Mason0b86a832008-03-24 15:01:56 -04005601
5602 fill_device_from_item(leaf, dev_item, device);
5603 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04005604 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01005605 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05005606 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04005607 spin_lock(&root->fs_info->free_chunk_lock);
5608 root->fs_info->free_chunk_space += device->total_bytes -
5609 device->bytes_used;
5610 spin_unlock(&root->fs_info->free_chunk_lock);
5611 }
Chris Mason0b86a832008-03-24 15:01:56 -04005612 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005613 return ret;
5614}
5615
Yan Zhenge4404d62008-12-12 10:03:26 -05005616int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04005617{
David Sterba6c417612011-04-13 15:41:04 +02005618 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04005619 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04005620 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04005621 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04005622 u8 *ptr;
5623 unsigned long sb_ptr;
5624 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005625 u32 num_stripes;
5626 u32 array_size;
5627 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005628 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04005629 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04005630
Yan Zhenge4404d62008-12-12 10:03:26 -05005631 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04005632 BTRFS_SUPER_INFO_SIZE);
5633 if (!sb)
5634 return -ENOMEM;
5635 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04005636 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02005637 /*
5638 * The sb extent buffer is artifical and just used to read the system array.
5639 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5640 * pages up-to-date when the page is larger: extent does not cover the
5641 * whole page and consequently check_page_uptodate does not find all
5642 * the page's extents up-to-date (the hole beyond sb),
5643 * write_extent_buffer then triggers a WARN_ON.
5644 *
5645 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5646 * but sb spans only this function. Add an explicit SetPageUptodate call
5647 * to silence the warning eg. on PowerPC 64.
5648 */
5649 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04005650 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05005651
Chris Masona061fc82008-05-07 11:43:44 -04005652 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005653 array_size = btrfs_super_sys_array_size(super_copy);
5654
Chris Mason0b86a832008-03-24 15:01:56 -04005655 ptr = super_copy->sys_chunk_array;
5656 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5657 cur = 0;
5658
5659 while (cur < array_size) {
5660 disk_key = (struct btrfs_disk_key *)ptr;
5661 btrfs_disk_key_to_cpu(&key, disk_key);
5662
Chris Masona061fc82008-05-07 11:43:44 -04005663 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04005664 sb_ptr += len;
5665 cur += len;
5666
Chris Mason0d81ba52008-03-24 15:02:07 -04005667 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04005668 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04005669 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04005670 if (ret)
5671 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005672 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5673 len = btrfs_chunk_item_size(num_stripes);
5674 } else {
Chris Mason84eed902008-04-25 09:04:37 -04005675 ret = -EIO;
5676 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005677 }
5678 ptr += len;
5679 sb_ptr += len;
5680 cur += len;
5681 }
Chris Masona061fc82008-05-07 11:43:44 -04005682 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04005683 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005684}
5685
5686int btrfs_read_chunk_tree(struct btrfs_root *root)
5687{
5688 struct btrfs_path *path;
5689 struct extent_buffer *leaf;
5690 struct btrfs_key key;
5691 struct btrfs_key found_key;
5692 int ret;
5693 int slot;
5694
5695 root = root->fs_info->chunk_root;
5696
5697 path = btrfs_alloc_path();
5698 if (!path)
5699 return -ENOMEM;
5700
Li Zefanb367e472011-12-07 11:38:24 +08005701 mutex_lock(&uuid_mutex);
5702 lock_chunks(root);
5703
Chris Mason0b86a832008-03-24 15:01:56 -04005704 /* first we search for all of the device items, and then we
5705 * read in all of the chunk items. This way we can create chunk
5706 * mappings that reference all of the devices that are afound
5707 */
5708 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5709 key.offset = 0;
5710 key.type = 0;
5711again:
5712 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005713 if (ret < 0)
5714 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005715 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005716 leaf = path->nodes[0];
5717 slot = path->slots[0];
5718 if (slot >= btrfs_header_nritems(leaf)) {
5719 ret = btrfs_next_leaf(root, path);
5720 if (ret == 0)
5721 continue;
5722 if (ret < 0)
5723 goto error;
5724 break;
5725 }
5726 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5727 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5728 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5729 break;
5730 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5731 struct btrfs_dev_item *dev_item;
5732 dev_item = btrfs_item_ptr(leaf, slot,
5733 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04005734 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05005735 if (ret)
5736 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005737 }
5738 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5739 struct btrfs_chunk *chunk;
5740 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5741 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005742 if (ret)
5743 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005744 }
5745 path->slots[0]++;
5746 }
5747 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5748 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005749 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005750 goto again;
5751 }
Chris Mason0b86a832008-03-24 15:01:56 -04005752 ret = 0;
5753error:
Li Zefanb367e472011-12-07 11:38:24 +08005754 unlock_chunks(root);
5755 mutex_unlock(&uuid_mutex);
5756
Yan Zheng2b820322008-11-17 21:11:30 -05005757 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005758 return ret;
5759}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005760
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005761static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5762{
5763 int i;
5764
5765 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5766 btrfs_dev_stat_reset(dev, i);
5767}
5768
5769int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5770{
5771 struct btrfs_key key;
5772 struct btrfs_key found_key;
5773 struct btrfs_root *dev_root = fs_info->dev_root;
5774 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5775 struct extent_buffer *eb;
5776 int slot;
5777 int ret = 0;
5778 struct btrfs_device *device;
5779 struct btrfs_path *path = NULL;
5780 int i;
5781
5782 path = btrfs_alloc_path();
5783 if (!path) {
5784 ret = -ENOMEM;
5785 goto out;
5786 }
5787
5788 mutex_lock(&fs_devices->device_list_mutex);
5789 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5790 int item_size;
5791 struct btrfs_dev_stats_item *ptr;
5792
5793 key.objectid = 0;
5794 key.type = BTRFS_DEV_STATS_KEY;
5795 key.offset = device->devid;
5796 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5797 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005798 __btrfs_reset_dev_stats(device);
5799 device->dev_stats_valid = 1;
5800 btrfs_release_path(path);
5801 continue;
5802 }
5803 slot = path->slots[0];
5804 eb = path->nodes[0];
5805 btrfs_item_key_to_cpu(eb, &found_key, slot);
5806 item_size = btrfs_item_size_nr(eb, slot);
5807
5808 ptr = btrfs_item_ptr(eb, slot,
5809 struct btrfs_dev_stats_item);
5810
5811 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5812 if (item_size >= (1 + i) * sizeof(__le64))
5813 btrfs_dev_stat_set(device, i,
5814 btrfs_dev_stats_value(eb, ptr, i));
5815 else
5816 btrfs_dev_stat_reset(device, i);
5817 }
5818
5819 device->dev_stats_valid = 1;
5820 btrfs_dev_stat_print_on_load(device);
5821 btrfs_release_path(path);
5822 }
5823 mutex_unlock(&fs_devices->device_list_mutex);
5824
5825out:
5826 btrfs_free_path(path);
5827 return ret < 0 ? ret : 0;
5828}
5829
5830static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5831 struct btrfs_root *dev_root,
5832 struct btrfs_device *device)
5833{
5834 struct btrfs_path *path;
5835 struct btrfs_key key;
5836 struct extent_buffer *eb;
5837 struct btrfs_dev_stats_item *ptr;
5838 int ret;
5839 int i;
5840
5841 key.objectid = 0;
5842 key.type = BTRFS_DEV_STATS_KEY;
5843 key.offset = device->devid;
5844
5845 path = btrfs_alloc_path();
5846 BUG_ON(!path);
5847 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5848 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005849 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5850 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005851 goto out;
5852 }
5853
5854 if (ret == 0 &&
5855 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5856 /* need to delete old one and insert a new one */
5857 ret = btrfs_del_item(trans, dev_root, path);
5858 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005859 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5860 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005861 goto out;
5862 }
5863 ret = 1;
5864 }
5865
5866 if (ret == 1) {
5867 /* need to insert a new item */
5868 btrfs_release_path(path);
5869 ret = btrfs_insert_empty_item(trans, dev_root, path,
5870 &key, sizeof(*ptr));
5871 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005872 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5873 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005874 goto out;
5875 }
5876 }
5877
5878 eb = path->nodes[0];
5879 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5880 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5881 btrfs_set_dev_stats_value(eb, ptr, i,
5882 btrfs_dev_stat_read(device, i));
5883 btrfs_mark_buffer_dirty(eb);
5884
5885out:
5886 btrfs_free_path(path);
5887 return ret;
5888}
5889
5890/*
5891 * called from commit_transaction. Writes all changed device stats to disk.
5892 */
5893int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5894 struct btrfs_fs_info *fs_info)
5895{
5896 struct btrfs_root *dev_root = fs_info->dev_root;
5897 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5898 struct btrfs_device *device;
5899 int ret = 0;
5900
5901 mutex_lock(&fs_devices->device_list_mutex);
5902 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5903 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5904 continue;
5905
5906 ret = update_dev_stat_item(trans, dev_root, device);
5907 if (!ret)
5908 device->dev_stats_dirty = 0;
5909 }
5910 mutex_unlock(&fs_devices->device_list_mutex);
5911
5912 return ret;
5913}
5914
Stefan Behrens442a4f62012-05-25 16:06:08 +02005915void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5916{
5917 btrfs_dev_stat_inc(dev, index);
5918 btrfs_dev_stat_print_on_error(dev);
5919}
5920
5921void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5922{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005923 if (!dev->dev_stats_valid)
5924 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005925 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005926 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005927 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005928 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5929 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5930 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5931 btrfs_dev_stat_read(dev,
5932 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5933 btrfs_dev_stat_read(dev,
5934 BTRFS_DEV_STAT_GENERATION_ERRS));
5935}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005936
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005937static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5938{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005939 int i;
5940
5941 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5942 if (btrfs_dev_stat_read(dev, i) != 0)
5943 break;
5944 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5945 return; /* all values == 0, suppress message */
5946
Josef Bacik606686e2012-06-04 14:03:51 -04005947 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5948 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005949 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5950 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5951 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5952 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5953 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5954}
5955
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005956int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005957 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005958{
5959 struct btrfs_device *dev;
5960 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5961 int i;
5962
5963 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005964 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005965 mutex_unlock(&fs_devices->device_list_mutex);
5966
5967 if (!dev) {
5968 printk(KERN_WARNING
5969 "btrfs: get dev_stats failed, device not found\n");
5970 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005971 } else if (!dev->dev_stats_valid) {
5972 printk(KERN_WARNING
5973 "btrfs: get dev_stats failed, not yet valid\n");
5974 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005975 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005976 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5977 if (stats->nr_items > i)
5978 stats->values[i] =
5979 btrfs_dev_stat_read_and_reset(dev, i);
5980 else
5981 btrfs_dev_stat_reset(dev, i);
5982 }
5983 } else {
5984 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5985 if (stats->nr_items > i)
5986 stats->values[i] = btrfs_dev_stat_read(dev, i);
5987 }
5988 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5989 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5990 return 0;
5991}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01005992
5993int btrfs_scratch_superblock(struct btrfs_device *device)
5994{
5995 struct buffer_head *bh;
5996 struct btrfs_super_block *disk_super;
5997
5998 bh = btrfs_read_dev_super(device->bdev);
5999 if (!bh)
6000 return -EINVAL;
6001 disk_super = (struct btrfs_super_block *)bh->b_data;
6002
6003 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6004 set_buffer_dirty(bh);
6005 sync_dirty_buffer(bh);
6006 brelse(bh);
6007
6008 return 0;
6009}