blob: c372264b85bfc31d82819f68568c2a680373b976 [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;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000653 list_replace_rcu(&device->dev_list, &new_device->dev_list);
654
655 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400656 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000657 mutex_unlock(&fs_devices->device_list_mutex);
658
Yan Zhenge4404d62008-12-12 10:03:26 -0500659 WARN_ON(fs_devices->open_devices);
660 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500661 fs_devices->opened = 0;
662 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500663
Chris Mason8a4b83c2008-03-24 15:02:07 -0400664 return 0;
665}
666
Yan Zheng2b820322008-11-17 21:11:30 -0500667int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
668{
Yan Zhenge4404d62008-12-12 10:03:26 -0500669 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500670 int ret;
671
672 mutex_lock(&uuid_mutex);
673 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500674 if (!fs_devices->opened) {
675 seed_devices = fs_devices->seed;
676 fs_devices->seed = NULL;
677 }
Yan Zheng2b820322008-11-17 21:11:30 -0500678 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500679
680 while (seed_devices) {
681 fs_devices = seed_devices;
682 seed_devices = fs_devices->seed;
683 __btrfs_close_devices(fs_devices);
684 free_fs_devices(fs_devices);
685 }
Yan Zheng2b820322008-11-17 21:11:30 -0500686 return ret;
687}
688
Yan Zhenge4404d62008-12-12 10:03:26 -0500689static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
690 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400691{
Josef Bacikd5e20032011-08-04 14:52:27 +0000692 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400693 struct block_device *bdev;
694 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400695 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400696 struct block_device *latest_bdev = NULL;
697 struct buffer_head *bh;
698 struct btrfs_super_block *disk_super;
699 u64 latest_devid = 0;
700 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400701 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500702 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400703 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400704
Tejun Heod4d77622010-11-13 11:55:18 +0100705 flags |= FMODE_EXCL;
706
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500707 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400708 if (device->bdev)
709 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400710 if (!device->name)
711 continue;
712
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100713 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
714 &bdev, &bh);
715 if (ret)
716 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400717
718 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000719 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400720 if (devid != device->devid)
721 goto error_brelse;
722
Yan Zheng2b820322008-11-17 21:11:30 -0500723 if (memcmp(device->uuid, disk_super->dev_item.uuid,
724 BTRFS_UUID_SIZE))
725 goto error_brelse;
726
727 device->generation = btrfs_super_generation(disk_super);
728 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400729 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500730 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400731 latest_bdev = bdev;
732 }
733
Yan Zheng2b820322008-11-17 21:11:30 -0500734 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
735 device->writeable = 0;
736 } else {
737 device->writeable = !bdev_read_only(bdev);
738 seeding = 0;
739 }
740
Josef Bacikd5e20032011-08-04 14:52:27 +0000741 q = bdev_get_queue(bdev);
742 if (blk_queue_discard(q)) {
743 device->can_discard = 1;
744 fs_devices->num_can_discard++;
745 }
746
Chris Mason8a4b83c2008-03-24 15:02:07 -0400747 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400748 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500749 device->mode = flags;
750
Chris Masonc289811c2009-06-10 09:51:32 -0400751 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
752 fs_devices->rotating = 1;
753
Chris Masona0af4692008-05-13 16:03:06 -0400754 fs_devices->open_devices++;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100755 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500756 fs_devices->rw_devices++;
757 list_add(&device->dev_alloc_list,
758 &fs_devices->alloc_list);
759 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000760 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400761 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400762
Chris Masona0af4692008-05-13 16:03:06 -0400763error_brelse:
764 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100765 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400766 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400767 }
Chris Masona0af4692008-05-13 16:03:06 -0400768 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300769 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400770 goto out;
771 }
Yan Zheng2b820322008-11-17 21:11:30 -0500772 fs_devices->seeding = seeding;
773 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400774 fs_devices->latest_bdev = latest_bdev;
775 fs_devices->latest_devid = latest_devid;
776 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500777 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400778out:
Yan Zheng2b820322008-11-17 21:11:30 -0500779 return ret;
780}
781
782int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500783 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500784{
785 int ret;
786
787 mutex_lock(&uuid_mutex);
788 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500789 fs_devices->opened++;
790 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500791 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500792 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500793 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400794 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400795 return ret;
796}
797
Christoph Hellwig97288f22008-12-02 06:36:09 -0500798int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400799 struct btrfs_fs_devices **fs_devices_ret)
800{
801 struct btrfs_super_block *disk_super;
802 struct block_device *bdev;
803 struct buffer_head *bh;
804 int ret;
805 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400806 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400807 u64 total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400808
Tejun Heod4d77622010-11-13 11:55:18 +0100809 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500810 mutex_lock(&uuid_mutex);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100811 ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400812 if (ret)
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100813 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400814 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000815 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400816 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400817 total_devices = btrfs_super_num_devices(disk_super);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000818 if (disk_super->label[0]) {
819 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
820 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Chris Masond3977122009-01-05 21:25:51 -0500821 printk(KERN_INFO "device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000822 } else {
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200823 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000824 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500825 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500826 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400827 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400828 if (!ret && fs_devices_ret)
829 (*fs_devices_ret)->total_devices = total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400830 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100831 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400832error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100833 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400834 return ret;
835}
Chris Mason0b86a832008-03-24 15:01:56 -0400836
Miao Xie6d07bce2011-01-05 10:07:31 +0000837/* helper to account the used device space in the range */
838int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
839 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400840{
841 struct btrfs_key key;
842 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000843 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500844 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000845 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400846 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000847 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400848 struct extent_buffer *l;
849
Miao Xie6d07bce2011-01-05 10:07:31 +0000850 *length = 0;
851
Stefan Behrens63a212a2012-11-05 18:29:28 +0100852 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000853 return 0;
854
Yan Zheng2b820322008-11-17 21:11:30 -0500855 path = btrfs_alloc_path();
856 if (!path)
857 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400858 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400859
Chris Mason0b86a832008-03-24 15:01:56 -0400860 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000861 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400862 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000863
864 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400865 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000866 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400867 if (ret > 0) {
868 ret = btrfs_previous_item(root, path, key.objectid, key.type);
869 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000870 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400871 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000872
Chris Mason0b86a832008-03-24 15:01:56 -0400873 while (1) {
874 l = path->nodes[0];
875 slot = path->slots[0];
876 if (slot >= btrfs_header_nritems(l)) {
877 ret = btrfs_next_leaf(root, path);
878 if (ret == 0)
879 continue;
880 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000881 goto out;
882
883 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400884 }
885 btrfs_item_key_to_cpu(l, &key, slot);
886
887 if (key.objectid < device->devid)
888 goto next;
889
890 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000891 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400892
Chris Masond3977122009-01-05 21:25:51 -0500893 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400894 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400895
Chris Mason0b86a832008-03-24 15:01:56 -0400896 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000897 extent_end = key.offset + btrfs_dev_extent_length(l,
898 dev_extent);
899 if (key.offset <= start && extent_end > end) {
900 *length = end - start + 1;
901 break;
902 } else if (key.offset <= start && extent_end > start)
903 *length += extent_end - start;
904 else if (key.offset > start && extent_end <= end)
905 *length += extent_end - key.offset;
906 else if (key.offset > start && key.offset <= end) {
907 *length += end - key.offset + 1;
908 break;
909 } else if (key.offset > end)
910 break;
911
912next:
913 path->slots[0]++;
914 }
915 ret = 0;
916out:
917 btrfs_free_path(path);
918 return ret;
919}
920
Chris Mason0b86a832008-03-24 15:01:56 -0400921/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000922 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000923 * @device: the device which we search the free space in
924 * @num_bytes: the size of the free space that we need
925 * @start: store the start of the free space.
926 * @len: the size of the free space. that we find, or the size of the max
927 * free space if we don't find suitable free space
928 *
Chris Mason0b86a832008-03-24 15:01:56 -0400929 * this uses a pretty simple search, the expectation is that it is
930 * called very infrequently and that a given device has a small number
931 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000932 *
933 * @start is used to store the start of the free space if we find. But if we
934 * don't find suitable free space, it will be used to store the start position
935 * of the max free space.
936 *
937 * @len is used to store the size of the free space that we find.
938 * But if we don't find suitable free space, it is used to store the size of
939 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400940 */
Li Zefan125ccb02011-12-08 15:07:24 +0800941int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000942 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400943{
944 struct btrfs_key key;
945 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000946 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400947 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000948 u64 hole_size;
949 u64 max_hole_start;
950 u64 max_hole_size;
951 u64 extent_end;
952 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400953 u64 search_end = device->total_bytes;
954 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000955 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400956 struct extent_buffer *l;
957
Chris Mason0b86a832008-03-24 15:01:56 -0400958 /* FIXME use last free of some kind */
959
960 /* we don't want to overwrite the superblock on the drive,
961 * so we make sure to start at an offset of at least 1MB
962 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200963 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400964
Miao Xie7bfc8372011-01-05 10:07:26 +0000965 max_hole_start = search_start;
966 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000967 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000968
Stefan Behrens63a212a2012-11-05 18:29:28 +0100969 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +0000970 ret = -ENOSPC;
971 goto error;
972 }
973
974 path = btrfs_alloc_path();
975 if (!path) {
976 ret = -ENOMEM;
977 goto error;
978 }
979 path->reada = 2;
980
Chris Mason0b86a832008-03-24 15:01:56 -0400981 key.objectid = device->devid;
982 key.offset = search_start;
983 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000984
Li Zefan125ccb02011-12-08 15:07:24 +0800985 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400986 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000987 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400988 if (ret > 0) {
989 ret = btrfs_previous_item(root, path, key.objectid, key.type);
990 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000991 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400992 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000993
Chris Mason0b86a832008-03-24 15:01:56 -0400994 while (1) {
995 l = path->nodes[0];
996 slot = path->slots[0];
997 if (slot >= btrfs_header_nritems(l)) {
998 ret = btrfs_next_leaf(root, path);
999 if (ret == 0)
1000 continue;
1001 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001002 goto out;
1003
1004 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001005 }
1006 btrfs_item_key_to_cpu(l, &key, slot);
1007
1008 if (key.objectid < device->devid)
1009 goto next;
1010
1011 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001012 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001013
Chris Mason0b86a832008-03-24 15:01:56 -04001014 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1015 goto next;
1016
Miao Xie7bfc8372011-01-05 10:07:26 +00001017 if (key.offset > search_start) {
1018 hole_size = key.offset - search_start;
1019
1020 if (hole_size > max_hole_size) {
1021 max_hole_start = search_start;
1022 max_hole_size = hole_size;
1023 }
1024
1025 /*
1026 * If this free space is greater than which we need,
1027 * it must be the max free space that we have found
1028 * until now, so max_hole_start must point to the start
1029 * of this free space and the length of this free space
1030 * is stored in max_hole_size. Thus, we return
1031 * max_hole_start and max_hole_size and go back to the
1032 * caller.
1033 */
1034 if (hole_size >= num_bytes) {
1035 ret = 0;
1036 goto out;
1037 }
1038 }
1039
Chris Mason0b86a832008-03-24 15:01:56 -04001040 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001041 extent_end = key.offset + btrfs_dev_extent_length(l,
1042 dev_extent);
1043 if (extent_end > search_start)
1044 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001045next:
1046 path->slots[0]++;
1047 cond_resched();
1048 }
Chris Mason0b86a832008-03-24 15:01:56 -04001049
liubo38c01b92011-08-02 02:39:03 +00001050 /*
1051 * At this point, search_start should be the end of
1052 * allocated dev extents, and when shrinking the device,
1053 * search_end may be smaller than search_start.
1054 */
1055 if (search_end > search_start)
1056 hole_size = search_end - search_start;
1057
Miao Xie7bfc8372011-01-05 10:07:26 +00001058 if (hole_size > max_hole_size) {
1059 max_hole_start = search_start;
1060 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001061 }
Chris Mason0b86a832008-03-24 15:01:56 -04001062
Miao Xie7bfc8372011-01-05 10:07:26 +00001063 /* See above. */
1064 if (hole_size < num_bytes)
1065 ret = -ENOSPC;
1066 else
1067 ret = 0;
1068
1069out:
Yan Zheng2b820322008-11-17 21:11:30 -05001070 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001071error:
1072 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001073 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001074 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001075 return ret;
1076}
1077
Christoph Hellwigb2950862008-12-02 09:54:17 -05001078static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001079 struct btrfs_device *device,
1080 u64 start)
1081{
1082 int ret;
1083 struct btrfs_path *path;
1084 struct btrfs_root *root = device->dev_root;
1085 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001086 struct btrfs_key found_key;
1087 struct extent_buffer *leaf = NULL;
1088 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001089
1090 path = btrfs_alloc_path();
1091 if (!path)
1092 return -ENOMEM;
1093
1094 key.objectid = device->devid;
1095 key.offset = start;
1096 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001097again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001098 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001099 if (ret > 0) {
1100 ret = btrfs_previous_item(root, path, key.objectid,
1101 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001102 if (ret)
1103 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001104 leaf = path->nodes[0];
1105 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1106 extent = btrfs_item_ptr(leaf, path->slots[0],
1107 struct btrfs_dev_extent);
1108 BUG_ON(found_key.offset > start || found_key.offset +
1109 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001110 key = found_key;
1111 btrfs_release_path(path);
1112 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001113 } else if (ret == 0) {
1114 leaf = path->nodes[0];
1115 extent = btrfs_item_ptr(leaf, path->slots[0],
1116 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001117 } else {
1118 btrfs_error(root->fs_info, ret, "Slot search failed");
1119 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001120 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001121
Josef Bacik2bf64752011-09-26 17:12:22 -04001122 if (device->bytes_used > 0) {
1123 u64 len = btrfs_dev_extent_length(leaf, extent);
1124 device->bytes_used -= len;
1125 spin_lock(&root->fs_info->free_chunk_lock);
1126 root->fs_info->free_chunk_space += len;
1127 spin_unlock(&root->fs_info->free_chunk_lock);
1128 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001129 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001130 if (ret) {
1131 btrfs_error(root->fs_info, ret,
1132 "Failed to remove dev extent item");
1133 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001134out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001135 btrfs_free_path(path);
1136 return ret;
1137}
1138
Yan Zheng2b820322008-11-17 21:11:30 -05001139int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001140 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001141 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001142 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001143{
1144 int ret;
1145 struct btrfs_path *path;
1146 struct btrfs_root *root = device->dev_root;
1147 struct btrfs_dev_extent *extent;
1148 struct extent_buffer *leaf;
1149 struct btrfs_key key;
1150
Chris Masondfe25022008-05-13 13:46:40 -04001151 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001152 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001153 path = btrfs_alloc_path();
1154 if (!path)
1155 return -ENOMEM;
1156
Chris Mason0b86a832008-03-24 15:01:56 -04001157 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001158 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001159 key.type = BTRFS_DEV_EXTENT_KEY;
1160 ret = btrfs_insert_empty_item(trans, root, path, &key,
1161 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001162 if (ret)
1163 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001164
1165 leaf = path->nodes[0];
1166 extent = btrfs_item_ptr(leaf, path->slots[0],
1167 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001168 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1169 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1170 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1171
1172 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1173 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1174 BTRFS_UUID_SIZE);
1175
Chris Mason0b86a832008-03-24 15:01:56 -04001176 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1177 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001178out:
Chris Mason0b86a832008-03-24 15:01:56 -04001179 btrfs_free_path(path);
1180 return ret;
1181}
1182
Chris Masona1b32a52008-09-05 16:09:51 -04001183static noinline int find_next_chunk(struct btrfs_root *root,
1184 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001185{
1186 struct btrfs_path *path;
1187 int ret;
1188 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001189 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001190 struct btrfs_key found_key;
1191
1192 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001193 if (!path)
1194 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001195
Chris Masone17cade2008-04-15 15:41:47 -04001196 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001197 key.offset = (u64)-1;
1198 key.type = BTRFS_CHUNK_ITEM_KEY;
1199
1200 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1201 if (ret < 0)
1202 goto error;
1203
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001204 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001205
1206 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1207 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001208 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001209 } else {
1210 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1211 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001212 if (found_key.objectid != objectid)
1213 *offset = 0;
1214 else {
1215 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1216 struct btrfs_chunk);
1217 *offset = found_key.offset +
1218 btrfs_chunk_length(path->nodes[0], chunk);
1219 }
Chris Mason0b86a832008-03-24 15:01:56 -04001220 }
1221 ret = 0;
1222error:
1223 btrfs_free_path(path);
1224 return ret;
1225}
1226
Yan Zheng2b820322008-11-17 21:11:30 -05001227static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001228{
1229 int ret;
1230 struct btrfs_key key;
1231 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001232 struct btrfs_path *path;
1233
1234 root = root->fs_info->chunk_root;
1235
1236 path = btrfs_alloc_path();
1237 if (!path)
1238 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001239
1240 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1241 key.type = BTRFS_DEV_ITEM_KEY;
1242 key.offset = (u64)-1;
1243
1244 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1245 if (ret < 0)
1246 goto error;
1247
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001248 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001249
1250 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1251 BTRFS_DEV_ITEM_KEY);
1252 if (ret) {
1253 *objectid = 1;
1254 } else {
1255 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1256 path->slots[0]);
1257 *objectid = found_key.offset + 1;
1258 }
1259 ret = 0;
1260error:
Yan Zheng2b820322008-11-17 21:11:30 -05001261 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001262 return ret;
1263}
1264
1265/*
1266 * the device information is stored in the chunk root
1267 * the btrfs_device struct should be fully filled in
1268 */
1269int btrfs_add_device(struct btrfs_trans_handle *trans,
1270 struct btrfs_root *root,
1271 struct btrfs_device *device)
1272{
1273 int ret;
1274 struct btrfs_path *path;
1275 struct btrfs_dev_item *dev_item;
1276 struct extent_buffer *leaf;
1277 struct btrfs_key key;
1278 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001279
1280 root = root->fs_info->chunk_root;
1281
1282 path = btrfs_alloc_path();
1283 if (!path)
1284 return -ENOMEM;
1285
Chris Mason0b86a832008-03-24 15:01:56 -04001286 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1287 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001288 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001289
1290 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001291 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001292 if (ret)
1293 goto out;
1294
1295 leaf = path->nodes[0];
1296 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1297
1298 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001299 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001300 btrfs_set_device_type(leaf, dev_item, device->type);
1301 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1302 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1303 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001304 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1305 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001306 btrfs_set_device_group(leaf, dev_item, 0);
1307 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1308 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001309 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001310
Chris Mason0b86a832008-03-24 15:01:56 -04001311 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001312 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001313 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1314 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001315 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001316
Yan Zheng2b820322008-11-17 21:11:30 -05001317 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001318out:
1319 btrfs_free_path(path);
1320 return ret;
1321}
Chris Mason8f18cf12008-04-25 16:53:30 -04001322
Chris Masona061fc82008-05-07 11:43:44 -04001323static int btrfs_rm_dev_item(struct btrfs_root *root,
1324 struct btrfs_device *device)
1325{
1326 int ret;
1327 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001328 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001329 struct btrfs_trans_handle *trans;
1330
1331 root = root->fs_info->chunk_root;
1332
1333 path = btrfs_alloc_path();
1334 if (!path)
1335 return -ENOMEM;
1336
Yan, Zhenga22285a2010-05-16 10:48:46 -04001337 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001338 if (IS_ERR(trans)) {
1339 btrfs_free_path(path);
1340 return PTR_ERR(trans);
1341 }
Chris Masona061fc82008-05-07 11:43:44 -04001342 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1343 key.type = BTRFS_DEV_ITEM_KEY;
1344 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001345 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001346
1347 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1348 if (ret < 0)
1349 goto out;
1350
1351 if (ret > 0) {
1352 ret = -ENOENT;
1353 goto out;
1354 }
1355
1356 ret = btrfs_del_item(trans, root, path);
1357 if (ret)
1358 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001359out:
1360 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001361 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001362 btrfs_commit_transaction(trans, root);
1363 return ret;
1364}
1365
1366int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1367{
1368 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001369 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001370 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001371 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001372 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001373 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001374 u64 all_avail;
1375 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001376 u64 num_devices;
1377 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001378 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001379 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001380
Chris Masona061fc82008-05-07 11:43:44 -04001381 mutex_lock(&uuid_mutex);
1382
1383 all_avail = root->fs_info->avail_data_alloc_bits |
1384 root->fs_info->avail_system_alloc_bits |
1385 root->fs_info->avail_metadata_alloc_bits;
1386
Stefan Behrens8dabb742012-11-06 13:15:27 +01001387 num_devices = root->fs_info->fs_devices->num_devices;
1388 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1389 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1390 WARN_ON(num_devices < 1);
1391 num_devices--;
1392 }
1393 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1394
David Woodhouse53b381b2013-01-29 18:40:14 -05001395 if ((all_avail & (BTRFS_BLOCK_GROUP_RAID5 |
1396 BTRFS_BLOCK_GROUP_RAID6) && num_devices <= 3)) {
1397 printk(KERN_ERR "btrfs: unable to go below three devices "
1398 "on raid5 or raid6\n");
1399 ret = -EINVAL;
1400 goto out;
1401 }
1402
Stefan Behrens8dabb742012-11-06 13:15:27 +01001403 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001404 printk(KERN_ERR "btrfs: unable to go below four devices "
1405 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001406 ret = -EINVAL;
1407 goto out;
1408 }
1409
Stefan Behrens8dabb742012-11-06 13:15:27 +01001410 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001411 printk(KERN_ERR "btrfs: unable to go below two "
1412 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001413 ret = -EINVAL;
1414 goto out;
1415 }
1416
David Woodhouse53b381b2013-01-29 18:40:14 -05001417 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1418 root->fs_info->fs_devices->rw_devices <= 2) {
1419 printk(KERN_ERR "btrfs: unable to go below two "
1420 "devices on raid5\n");
1421 ret = -EINVAL;
1422 goto out;
1423 }
1424 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1425 root->fs_info->fs_devices->rw_devices <= 3) {
1426 printk(KERN_ERR "btrfs: unable to go below three "
1427 "devices on raid6\n");
1428 ret = -EINVAL;
1429 goto out;
1430 }
1431
Chris Masondfe25022008-05-13 13:46:40 -04001432 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001433 struct list_head *devices;
1434 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001435
Chris Masondfe25022008-05-13 13:46:40 -04001436 device = NULL;
1437 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001438 /*
1439 * It is safe to read the devices since the volume_mutex
1440 * is held.
1441 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001442 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001443 if (tmp->in_fs_metadata &&
1444 !tmp->is_tgtdev_for_dev_replace &&
1445 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001446 device = tmp;
1447 break;
1448 }
1449 }
1450 bdev = NULL;
1451 bh = NULL;
1452 disk_super = NULL;
1453 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001454 printk(KERN_ERR "btrfs: no missing devices found to "
1455 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001456 goto out;
1457 }
Chris Masondfe25022008-05-13 13:46:40 -04001458 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001459 ret = btrfs_get_bdev_and_sb(device_path,
1460 FMODE_READ | FMODE_EXCL,
1461 root->fs_info->bdev_holder, 0,
1462 &bdev, &bh);
1463 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001464 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001465 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001466 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001467 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001468 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001469 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001470 if (!device) {
1471 ret = -ENOENT;
1472 goto error_brelse;
1473 }
Chris Masondfe25022008-05-13 13:46:40 -04001474 }
Yan Zheng2b820322008-11-17 21:11:30 -05001475
Stefan Behrens63a212a2012-11-05 18:29:28 +01001476 if (device->is_tgtdev_for_dev_replace) {
1477 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1478 ret = -EINVAL;
1479 goto error_brelse;
1480 }
1481
Yan Zheng2b820322008-11-17 21:11:30 -05001482 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001483 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1484 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001485 ret = -EINVAL;
1486 goto error_brelse;
1487 }
1488
1489 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001490 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001491 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001492 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001493 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001494 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001495 }
Chris Masona061fc82008-05-07 11:43:44 -04001496
1497 ret = btrfs_shrink_device(device, 0);
1498 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001499 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001500
Stefan Behrens63a212a2012-11-05 18:29:28 +01001501 /*
1502 * TODO: the superblock still includes this device in its num_devices
1503 * counter although write_all_supers() is not locked out. This
1504 * could give a filesystem state which requires a degraded mount.
1505 */
Chris Masona061fc82008-05-07 11:43:44 -04001506 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1507 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001508 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001509
Josef Bacik2bf64752011-09-26 17:12:22 -04001510 spin_lock(&root->fs_info->free_chunk_lock);
1511 root->fs_info->free_chunk_space = device->total_bytes -
1512 device->bytes_used;
1513 spin_unlock(&root->fs_info->free_chunk_lock);
1514
Yan Zheng2b820322008-11-17 21:11:30 -05001515 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001516 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001517
1518 /*
1519 * the device list mutex makes sure that we don't change
1520 * the device list while someone else is writing out all
1521 * the device supers.
1522 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001523
1524 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001525 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001526 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001527
Yan Zhenge4404d62008-12-12 10:03:26 -05001528 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001529 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001530
Chris Masoncd02dca2010-12-13 14:56:23 -05001531 if (device->missing)
1532 root->fs_info->fs_devices->missing_devices--;
1533
Yan Zheng2b820322008-11-17 21:11:30 -05001534 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1535 struct btrfs_device, dev_list);
1536 if (device->bdev == root->fs_info->sb->s_bdev)
1537 root->fs_info->sb->s_bdev = next_device->bdev;
1538 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1539 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1540
Xiao Guangrong1f781602011-04-20 10:09:16 +00001541 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001542 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001543
1544 call_rcu(&device->rcu, free_device);
1545 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001546
David Sterba6c417612011-04-13 15:41:04 +02001547 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1548 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001549
Xiao Guangrong1f781602011-04-20 10:09:16 +00001550 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001551 struct btrfs_fs_devices *fs_devices;
1552 fs_devices = root->fs_info->fs_devices;
1553 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001554 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001555 break;
1556 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001557 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001558 fs_devices->seed = cur_devices->seed;
1559 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001560 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001561 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001562 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001563 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001564 }
1565
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001566 root->fs_info->num_tolerated_disk_barrier_failures =
1567 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1568
Yan Zheng2b820322008-11-17 21:11:30 -05001569 /*
1570 * at this point, the device is zero sized. We want to
1571 * remove it from the devices list and zero out the old super
1572 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001573 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001574 /* make sure this device isn't detected as part of
1575 * the FS anymore
1576 */
1577 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1578 set_buffer_dirty(bh);
1579 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001580 }
Chris Masona061fc82008-05-07 11:43:44 -04001581
Chris Masona061fc82008-05-07 11:43:44 -04001582 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001583
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001584 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001585 if (bdev)
1586 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001587
Chris Masona061fc82008-05-07 11:43:44 -04001588error_brelse:
1589 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001590 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001591 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001592out:
1593 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001594 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001595error_undo:
1596 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001597 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001598 list_add(&device->dev_alloc_list,
1599 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001600 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001601 root->fs_info->fs_devices->rw_devices++;
1602 }
1603 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001604}
1605
Stefan Behrense93c89c2012-11-05 17:33:06 +01001606void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1607 struct btrfs_device *srcdev)
1608{
1609 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1610 list_del_rcu(&srcdev->dev_list);
1611 list_del_rcu(&srcdev->dev_alloc_list);
1612 fs_info->fs_devices->num_devices--;
1613 if (srcdev->missing) {
1614 fs_info->fs_devices->missing_devices--;
1615 fs_info->fs_devices->rw_devices++;
1616 }
1617 if (srcdev->can_discard)
1618 fs_info->fs_devices->num_can_discard--;
1619 if (srcdev->bdev)
1620 fs_info->fs_devices->open_devices--;
1621
1622 call_rcu(&srcdev->rcu, free_device);
1623}
1624
1625void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1626 struct btrfs_device *tgtdev)
1627{
1628 struct btrfs_device *next_device;
1629
1630 WARN_ON(!tgtdev);
1631 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1632 if (tgtdev->bdev) {
1633 btrfs_scratch_superblock(tgtdev);
1634 fs_info->fs_devices->open_devices--;
1635 }
1636 fs_info->fs_devices->num_devices--;
1637 if (tgtdev->can_discard)
1638 fs_info->fs_devices->num_can_discard++;
1639
1640 next_device = list_entry(fs_info->fs_devices->devices.next,
1641 struct btrfs_device, dev_list);
1642 if (tgtdev->bdev == fs_info->sb->s_bdev)
1643 fs_info->sb->s_bdev = next_device->bdev;
1644 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1645 fs_info->fs_devices->latest_bdev = next_device->bdev;
1646 list_del_rcu(&tgtdev->dev_list);
1647
1648 call_rcu(&tgtdev->rcu, free_device);
1649
1650 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1651}
1652
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001653int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1654 struct btrfs_device **device)
1655{
1656 int ret = 0;
1657 struct btrfs_super_block *disk_super;
1658 u64 devid;
1659 u8 *dev_uuid;
1660 struct block_device *bdev;
1661 struct buffer_head *bh;
1662
1663 *device = NULL;
1664 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1665 root->fs_info->bdev_holder, 0, &bdev, &bh);
1666 if (ret)
1667 return ret;
1668 disk_super = (struct btrfs_super_block *)bh->b_data;
1669 devid = btrfs_stack_device_id(&disk_super->dev_item);
1670 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001671 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001672 disk_super->fsid);
1673 brelse(bh);
1674 if (!*device)
1675 ret = -ENOENT;
1676 blkdev_put(bdev, FMODE_READ);
1677 return ret;
1678}
1679
1680int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1681 char *device_path,
1682 struct btrfs_device **device)
1683{
1684 *device = NULL;
1685 if (strcmp(device_path, "missing") == 0) {
1686 struct list_head *devices;
1687 struct btrfs_device *tmp;
1688
1689 devices = &root->fs_info->fs_devices->devices;
1690 /*
1691 * It is safe to read the devices since the volume_mutex
1692 * is held by the caller.
1693 */
1694 list_for_each_entry(tmp, devices, dev_list) {
1695 if (tmp->in_fs_metadata && !tmp->bdev) {
1696 *device = tmp;
1697 break;
1698 }
1699 }
1700
1701 if (!*device) {
1702 pr_err("btrfs: no missing device found\n");
1703 return -ENOENT;
1704 }
1705
1706 return 0;
1707 } else {
1708 return btrfs_find_device_by_path(root, device_path, device);
1709 }
1710}
1711
Yan Zheng2b820322008-11-17 21:11:30 -05001712/*
1713 * does all the dirty work required for changing file system's UUID.
1714 */
Li Zefan125ccb02011-12-08 15:07:24 +08001715static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001716{
1717 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1718 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001719 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001720 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001721 struct btrfs_device *device;
1722 u64 super_flags;
1723
1724 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001725 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001726 return -EINVAL;
1727
Yan Zhenge4404d62008-12-12 10:03:26 -05001728 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1729 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001730 return -ENOMEM;
1731
Yan Zhenge4404d62008-12-12 10:03:26 -05001732 old_devices = clone_fs_devices(fs_devices);
1733 if (IS_ERR(old_devices)) {
1734 kfree(seed_devices);
1735 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001736 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001737
Yan Zheng2b820322008-11-17 21:11:30 -05001738 list_add(&old_devices->list, &fs_uuids);
1739
Yan Zhenge4404d62008-12-12 10:03:26 -05001740 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1741 seed_devices->opened = 1;
1742 INIT_LIST_HEAD(&seed_devices->devices);
1743 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001744 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001745
1746 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001747 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1748 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001749 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1750
Yan Zhenge4404d62008-12-12 10:03:26 -05001751 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1752 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1753 device->fs_devices = seed_devices;
1754 }
1755
Yan Zheng2b820322008-11-17 21:11:30 -05001756 fs_devices->seeding = 0;
1757 fs_devices->num_devices = 0;
1758 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001759 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001760 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001761
1762 generate_random_uuid(fs_devices->fsid);
1763 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1764 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1765 super_flags = btrfs_super_flags(disk_super) &
1766 ~BTRFS_SUPER_FLAG_SEEDING;
1767 btrfs_set_super_flags(disk_super, super_flags);
1768
1769 return 0;
1770}
1771
1772/*
1773 * strore the expected generation for seed devices in device items.
1774 */
1775static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1776 struct btrfs_root *root)
1777{
1778 struct btrfs_path *path;
1779 struct extent_buffer *leaf;
1780 struct btrfs_dev_item *dev_item;
1781 struct btrfs_device *device;
1782 struct btrfs_key key;
1783 u8 fs_uuid[BTRFS_UUID_SIZE];
1784 u8 dev_uuid[BTRFS_UUID_SIZE];
1785 u64 devid;
1786 int ret;
1787
1788 path = btrfs_alloc_path();
1789 if (!path)
1790 return -ENOMEM;
1791
1792 root = root->fs_info->chunk_root;
1793 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1794 key.offset = 0;
1795 key.type = BTRFS_DEV_ITEM_KEY;
1796
1797 while (1) {
1798 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1799 if (ret < 0)
1800 goto error;
1801
1802 leaf = path->nodes[0];
1803next_slot:
1804 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1805 ret = btrfs_next_leaf(root, path);
1806 if (ret > 0)
1807 break;
1808 if (ret < 0)
1809 goto error;
1810 leaf = path->nodes[0];
1811 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001812 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001813 continue;
1814 }
1815
1816 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1817 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1818 key.type != BTRFS_DEV_ITEM_KEY)
1819 break;
1820
1821 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1822 struct btrfs_dev_item);
1823 devid = btrfs_device_id(leaf, dev_item);
1824 read_extent_buffer(leaf, dev_uuid,
1825 (unsigned long)btrfs_device_uuid(dev_item),
1826 BTRFS_UUID_SIZE);
1827 read_extent_buffer(leaf, fs_uuid,
1828 (unsigned long)btrfs_device_fsid(dev_item),
1829 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001830 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1831 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001832 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001833
1834 if (device->fs_devices->seeding) {
1835 btrfs_set_device_generation(leaf, dev_item,
1836 device->generation);
1837 btrfs_mark_buffer_dirty(leaf);
1838 }
1839
1840 path->slots[0]++;
1841 goto next_slot;
1842 }
1843 ret = 0;
1844error:
1845 btrfs_free_path(path);
1846 return ret;
1847}
1848
Chris Mason788f20e2008-04-28 15:29:42 -04001849int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1850{
Josef Bacikd5e20032011-08-04 14:52:27 +00001851 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001852 struct btrfs_trans_handle *trans;
1853 struct btrfs_device *device;
1854 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001855 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001856 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001857 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001858 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001859 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001860 int ret = 0;
1861
Yan Zheng2b820322008-11-17 21:11:30 -05001862 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001863 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001864
Li Zefana5d16332011-12-07 20:08:40 -05001865 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001866 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001867 if (IS_ERR(bdev))
1868 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001869
Yan Zheng2b820322008-11-17 21:11:30 -05001870 if (root->fs_info->fs_devices->seeding) {
1871 seeding_dev = 1;
1872 down_write(&sb->s_umount);
1873 mutex_lock(&uuid_mutex);
1874 }
1875
Chris Mason8c8bee12008-09-29 11:19:10 -04001876 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001877
Chris Mason788f20e2008-04-28 15:29:42 -04001878 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001879
1880 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001881 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001882 if (device->bdev == bdev) {
1883 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001884 mutex_unlock(
1885 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001886 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001887 }
1888 }
Liu Bod25628b2012-11-14 14:35:30 +00001889 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001890
1891 device = kzalloc(sizeof(*device), GFP_NOFS);
1892 if (!device) {
1893 /* we can safely leave the fs_devices entry around */
1894 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001895 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001896 }
1897
Josef Bacik606686e2012-06-04 14:03:51 -04001898 name = rcu_string_strdup(device_path, GFP_NOFS);
1899 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001900 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001901 ret = -ENOMEM;
1902 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001903 }
Josef Bacik606686e2012-06-04 14:03:51 -04001904 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001905
1906 ret = find_next_devid(root, &device->devid);
1907 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001908 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001909 kfree(device);
1910 goto error;
1911 }
1912
Yan, Zhenga22285a2010-05-16 10:48:46 -04001913 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001914 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001915 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001916 kfree(device);
1917 ret = PTR_ERR(trans);
1918 goto error;
1919 }
1920
Yan Zheng2b820322008-11-17 21:11:30 -05001921 lock_chunks(root);
1922
Josef Bacikd5e20032011-08-04 14:52:27 +00001923 q = bdev_get_queue(bdev);
1924 if (blk_queue_discard(q))
1925 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001926 device->writeable = 1;
1927 device->work.func = pending_bios_fn;
1928 generate_random_uuid(device->uuid);
1929 spin_lock_init(&device->io_lock);
1930 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001931 device->io_width = root->sectorsize;
1932 device->io_align = root->sectorsize;
1933 device->sector_size = root->sectorsize;
1934 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001935 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001936 device->dev_root = root->fs_info->dev_root;
1937 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001938 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001939 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001940 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001941 set_blocksize(device->bdev, 4096);
1942
Yan Zheng2b820322008-11-17 21:11:30 -05001943 if (seeding_dev) {
1944 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001945 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001946 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001947 }
1948
1949 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001950
Chris Masone5e9a522009-06-10 15:17:02 -04001951 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001952 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001953 list_add(&device->dev_alloc_list,
1954 &root->fs_info->fs_devices->alloc_list);
1955 root->fs_info->fs_devices->num_devices++;
1956 root->fs_info->fs_devices->open_devices++;
1957 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04001958 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001959 if (device->can_discard)
1960 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001961 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1962
Josef Bacik2bf64752011-09-26 17:12:22 -04001963 spin_lock(&root->fs_info->free_chunk_lock);
1964 root->fs_info->free_chunk_space += device->total_bytes;
1965 spin_unlock(&root->fs_info->free_chunk_lock);
1966
Chris Masonc289811c2009-06-10 09:51:32 -04001967 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1968 root->fs_info->fs_devices->rotating = 1;
1969
David Sterba6c417612011-04-13 15:41:04 +02001970 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1971 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001972 total_bytes + device->total_bytes);
1973
David Sterba6c417612011-04-13 15:41:04 +02001974 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1975 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001976 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001977 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001978
Yan Zheng2b820322008-11-17 21:11:30 -05001979 if (seeding_dev) {
1980 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001981 if (ret) {
1982 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001983 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001984 }
Yan Zheng2b820322008-11-17 21:11:30 -05001985 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06001986 if (ret) {
1987 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001988 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001989 }
Yan Zheng2b820322008-11-17 21:11:30 -05001990 } else {
1991 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001992 if (ret) {
1993 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001994 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001995 }
Yan Zheng2b820322008-11-17 21:11:30 -05001996 }
1997
Chris Mason913d9522009-03-10 13:17:18 -04001998 /*
1999 * we've got more storage, clear any full flags on the space
2000 * infos
2001 */
2002 btrfs_clear_space_info_full(root->fs_info);
2003
Chris Mason7d9eb122008-07-08 14:19:17 -04002004 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002005 root->fs_info->num_tolerated_disk_barrier_failures =
2006 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002007 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002008
2009 if (seeding_dev) {
2010 mutex_unlock(&uuid_mutex);
2011 up_write(&sb->s_umount);
2012
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002013 if (ret) /* transaction commit */
2014 return ret;
2015
Yan Zheng2b820322008-11-17 21:11:30 -05002016 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002017 if (ret < 0)
2018 btrfs_error(root->fs_info, ret,
2019 "Failed to relocate sys chunks after "
2020 "device initialization. This can be fixed "
2021 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002022 trans = btrfs_attach_transaction(root);
2023 if (IS_ERR(trans)) {
2024 if (PTR_ERR(trans) == -ENOENT)
2025 return 0;
2026 return PTR_ERR(trans);
2027 }
2028 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002029 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002030
Chris Mason788f20e2008-04-28 15:29:42 -04002031 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002032
2033error_trans:
2034 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002035 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002036 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002037 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002038error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002039 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002040 if (seeding_dev) {
2041 mutex_unlock(&uuid_mutex);
2042 up_write(&sb->s_umount);
2043 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002044 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002045}
2046
Stefan Behrense93c89c2012-11-05 17:33:06 +01002047int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2048 struct btrfs_device **device_out)
2049{
2050 struct request_queue *q;
2051 struct btrfs_device *device;
2052 struct block_device *bdev;
2053 struct btrfs_fs_info *fs_info = root->fs_info;
2054 struct list_head *devices;
2055 struct rcu_string *name;
2056 int ret = 0;
2057
2058 *device_out = NULL;
2059 if (fs_info->fs_devices->seeding)
2060 return -EINVAL;
2061
2062 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2063 fs_info->bdev_holder);
2064 if (IS_ERR(bdev))
2065 return PTR_ERR(bdev);
2066
2067 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2068
2069 devices = &fs_info->fs_devices->devices;
2070 list_for_each_entry(device, devices, dev_list) {
2071 if (device->bdev == bdev) {
2072 ret = -EEXIST;
2073 goto error;
2074 }
2075 }
2076
2077 device = kzalloc(sizeof(*device), GFP_NOFS);
2078 if (!device) {
2079 ret = -ENOMEM;
2080 goto error;
2081 }
2082
2083 name = rcu_string_strdup(device_path, GFP_NOFS);
2084 if (!name) {
2085 kfree(device);
2086 ret = -ENOMEM;
2087 goto error;
2088 }
2089 rcu_assign_pointer(device->name, name);
2090
2091 q = bdev_get_queue(bdev);
2092 if (blk_queue_discard(q))
2093 device->can_discard = 1;
2094 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2095 device->writeable = 1;
2096 device->work.func = pending_bios_fn;
2097 generate_random_uuid(device->uuid);
2098 device->devid = BTRFS_DEV_REPLACE_DEVID;
2099 spin_lock_init(&device->io_lock);
2100 device->generation = 0;
2101 device->io_width = root->sectorsize;
2102 device->io_align = root->sectorsize;
2103 device->sector_size = root->sectorsize;
2104 device->total_bytes = i_size_read(bdev->bd_inode);
2105 device->disk_total_bytes = device->total_bytes;
2106 device->dev_root = fs_info->dev_root;
2107 device->bdev = bdev;
2108 device->in_fs_metadata = 1;
2109 device->is_tgtdev_for_dev_replace = 1;
2110 device->mode = FMODE_EXCL;
2111 set_blocksize(device->bdev, 4096);
2112 device->fs_devices = fs_info->fs_devices;
2113 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2114 fs_info->fs_devices->num_devices++;
2115 fs_info->fs_devices->open_devices++;
2116 if (device->can_discard)
2117 fs_info->fs_devices->num_can_discard++;
2118 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2119
2120 *device_out = device;
2121 return ret;
2122
2123error:
2124 blkdev_put(bdev, FMODE_EXCL);
2125 return ret;
2126}
2127
2128void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2129 struct btrfs_device *tgtdev)
2130{
2131 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2132 tgtdev->io_width = fs_info->dev_root->sectorsize;
2133 tgtdev->io_align = fs_info->dev_root->sectorsize;
2134 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2135 tgtdev->dev_root = fs_info->dev_root;
2136 tgtdev->in_fs_metadata = 1;
2137}
2138
Chris Masond3977122009-01-05 21:25:51 -05002139static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2140 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002141{
2142 int ret;
2143 struct btrfs_path *path;
2144 struct btrfs_root *root;
2145 struct btrfs_dev_item *dev_item;
2146 struct extent_buffer *leaf;
2147 struct btrfs_key key;
2148
2149 root = device->dev_root->fs_info->chunk_root;
2150
2151 path = btrfs_alloc_path();
2152 if (!path)
2153 return -ENOMEM;
2154
2155 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2156 key.type = BTRFS_DEV_ITEM_KEY;
2157 key.offset = device->devid;
2158
2159 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2160 if (ret < 0)
2161 goto out;
2162
2163 if (ret > 0) {
2164 ret = -ENOENT;
2165 goto out;
2166 }
2167
2168 leaf = path->nodes[0];
2169 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2170
2171 btrfs_set_device_id(leaf, dev_item, device->devid);
2172 btrfs_set_device_type(leaf, dev_item, device->type);
2173 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2174 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2175 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002176 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002177 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2178 btrfs_mark_buffer_dirty(leaf);
2179
2180out:
2181 btrfs_free_path(path);
2182 return ret;
2183}
2184
Chris Mason7d9eb122008-07-08 14:19:17 -04002185static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002186 struct btrfs_device *device, u64 new_size)
2187{
2188 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002189 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002190 u64 old_total = btrfs_super_total_bytes(super_copy);
2191 u64 diff = new_size - device->total_bytes;
2192
Yan Zheng2b820322008-11-17 21:11:30 -05002193 if (!device->writeable)
2194 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002195 if (new_size <= device->total_bytes ||
2196 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002197 return -EINVAL;
2198
Chris Mason8f18cf12008-04-25 16:53:30 -04002199 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002200 device->fs_devices->total_rw_bytes += diff;
2201
2202 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002203 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002204 btrfs_clear_space_info_full(device->dev_root->fs_info);
2205
Chris Mason8f18cf12008-04-25 16:53:30 -04002206 return btrfs_update_device(trans, device);
2207}
2208
Chris Mason7d9eb122008-07-08 14:19:17 -04002209int btrfs_grow_device(struct btrfs_trans_handle *trans,
2210 struct btrfs_device *device, u64 new_size)
2211{
2212 int ret;
2213 lock_chunks(device->dev_root);
2214 ret = __btrfs_grow_device(trans, device, new_size);
2215 unlock_chunks(device->dev_root);
2216 return ret;
2217}
2218
Chris Mason8f18cf12008-04-25 16:53:30 -04002219static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2220 struct btrfs_root *root,
2221 u64 chunk_tree, u64 chunk_objectid,
2222 u64 chunk_offset)
2223{
2224 int ret;
2225 struct btrfs_path *path;
2226 struct btrfs_key key;
2227
2228 root = root->fs_info->chunk_root;
2229 path = btrfs_alloc_path();
2230 if (!path)
2231 return -ENOMEM;
2232
2233 key.objectid = chunk_objectid;
2234 key.offset = chunk_offset;
2235 key.type = BTRFS_CHUNK_ITEM_KEY;
2236
2237 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002238 if (ret < 0)
2239 goto out;
2240 else if (ret > 0) { /* Logic error or corruption */
2241 btrfs_error(root->fs_info, -ENOENT,
2242 "Failed lookup while freeing chunk.");
2243 ret = -ENOENT;
2244 goto out;
2245 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002246
2247 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002248 if (ret < 0)
2249 btrfs_error(root->fs_info, ret,
2250 "Failed to delete chunk item.");
2251out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002252 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002253 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002254}
2255
Christoph Hellwigb2950862008-12-02 09:54:17 -05002256static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002257 chunk_offset)
2258{
David Sterba6c417612011-04-13 15:41:04 +02002259 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002260 struct btrfs_disk_key *disk_key;
2261 struct btrfs_chunk *chunk;
2262 u8 *ptr;
2263 int ret = 0;
2264 u32 num_stripes;
2265 u32 array_size;
2266 u32 len = 0;
2267 u32 cur;
2268 struct btrfs_key key;
2269
2270 array_size = btrfs_super_sys_array_size(super_copy);
2271
2272 ptr = super_copy->sys_chunk_array;
2273 cur = 0;
2274
2275 while (cur < array_size) {
2276 disk_key = (struct btrfs_disk_key *)ptr;
2277 btrfs_disk_key_to_cpu(&key, disk_key);
2278
2279 len = sizeof(*disk_key);
2280
2281 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2282 chunk = (struct btrfs_chunk *)(ptr + len);
2283 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2284 len += btrfs_chunk_item_size(num_stripes);
2285 } else {
2286 ret = -EIO;
2287 break;
2288 }
2289 if (key.objectid == chunk_objectid &&
2290 key.offset == chunk_offset) {
2291 memmove(ptr, ptr + len, array_size - (cur + len));
2292 array_size -= len;
2293 btrfs_set_super_sys_array_size(super_copy, array_size);
2294 } else {
2295 ptr += len;
2296 cur += len;
2297 }
2298 }
2299 return ret;
2300}
2301
Christoph Hellwigb2950862008-12-02 09:54:17 -05002302static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002303 u64 chunk_tree, u64 chunk_objectid,
2304 u64 chunk_offset)
2305{
2306 struct extent_map_tree *em_tree;
2307 struct btrfs_root *extent_root;
2308 struct btrfs_trans_handle *trans;
2309 struct extent_map *em;
2310 struct map_lookup *map;
2311 int ret;
2312 int i;
2313
2314 root = root->fs_info->chunk_root;
2315 extent_root = root->fs_info->extent_root;
2316 em_tree = &root->fs_info->mapping_tree.map_tree;
2317
Josef Bacikba1bf482009-09-11 16:11:19 -04002318 ret = btrfs_can_relocate(extent_root, chunk_offset);
2319 if (ret)
2320 return -ENOSPC;
2321
Chris Mason8f18cf12008-04-25 16:53:30 -04002322 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002323 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002324 if (ret)
2325 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002326
Yan, Zhenga22285a2010-05-16 10:48:46 -04002327 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002328 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002329
Chris Mason7d9eb122008-07-08 14:19:17 -04002330 lock_chunks(root);
2331
Chris Mason8f18cf12008-04-25 16:53:30 -04002332 /*
2333 * step two, delete the device extents and the
2334 * chunk tree entries
2335 */
Chris Mason890871b2009-09-02 16:24:52 -04002336 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002337 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002338 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002339
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002340 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002341 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002342 map = (struct map_lookup *)em->bdev;
2343
2344 for (i = 0; i < map->num_stripes; i++) {
2345 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2346 map->stripes[i].physical);
2347 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002348
Chris Masondfe25022008-05-13 13:46:40 -04002349 if (map->stripes[i].dev) {
2350 ret = btrfs_update_device(trans, map->stripes[i].dev);
2351 BUG_ON(ret);
2352 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002353 }
2354 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2355 chunk_offset);
2356
2357 BUG_ON(ret);
2358
liubo1abe9b82011-03-24 11:18:59 +00002359 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2360
Chris Mason8f18cf12008-04-25 16:53:30 -04002361 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2362 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2363 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002364 }
2365
Zheng Yan1a40e232008-09-26 10:09:34 -04002366 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2367 BUG_ON(ret);
2368
Chris Mason890871b2009-09-02 16:24:52 -04002369 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002370 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002371 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002372
Chris Mason8f18cf12008-04-25 16:53:30 -04002373 kfree(map);
2374 em->bdev = NULL;
2375
2376 /* once for the tree */
2377 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002378 /* once for us */
2379 free_extent_map(em);
2380
Chris Mason7d9eb122008-07-08 14:19:17 -04002381 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002382 btrfs_end_transaction(trans, root);
2383 return 0;
2384}
2385
Yan Zheng2b820322008-11-17 21:11:30 -05002386static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2387{
2388 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2389 struct btrfs_path *path;
2390 struct extent_buffer *leaf;
2391 struct btrfs_chunk *chunk;
2392 struct btrfs_key key;
2393 struct btrfs_key found_key;
2394 u64 chunk_tree = chunk_root->root_key.objectid;
2395 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002396 bool retried = false;
2397 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002398 int ret;
2399
2400 path = btrfs_alloc_path();
2401 if (!path)
2402 return -ENOMEM;
2403
Josef Bacikba1bf482009-09-11 16:11:19 -04002404again:
Yan Zheng2b820322008-11-17 21:11:30 -05002405 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2406 key.offset = (u64)-1;
2407 key.type = BTRFS_CHUNK_ITEM_KEY;
2408
2409 while (1) {
2410 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2411 if (ret < 0)
2412 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002413 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002414
2415 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2416 key.type);
2417 if (ret < 0)
2418 goto error;
2419 if (ret > 0)
2420 break;
2421
2422 leaf = path->nodes[0];
2423 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2424
2425 chunk = btrfs_item_ptr(leaf, path->slots[0],
2426 struct btrfs_chunk);
2427 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002428 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002429
2430 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2431 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2432 found_key.objectid,
2433 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002434 if (ret == -ENOSPC)
2435 failed++;
2436 else if (ret)
2437 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002438 }
2439
2440 if (found_key.offset == 0)
2441 break;
2442 key.offset = found_key.offset - 1;
2443 }
2444 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002445 if (failed && !retried) {
2446 failed = 0;
2447 retried = true;
2448 goto again;
2449 } else if (failed && retried) {
2450 WARN_ON(1);
2451 ret = -ENOSPC;
2452 }
Yan Zheng2b820322008-11-17 21:11:30 -05002453error:
2454 btrfs_free_path(path);
2455 return ret;
2456}
2457
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002458static int insert_balance_item(struct btrfs_root *root,
2459 struct btrfs_balance_control *bctl)
2460{
2461 struct btrfs_trans_handle *trans;
2462 struct btrfs_balance_item *item;
2463 struct btrfs_disk_balance_args disk_bargs;
2464 struct btrfs_path *path;
2465 struct extent_buffer *leaf;
2466 struct btrfs_key key;
2467 int ret, err;
2468
2469 path = btrfs_alloc_path();
2470 if (!path)
2471 return -ENOMEM;
2472
2473 trans = btrfs_start_transaction(root, 0);
2474 if (IS_ERR(trans)) {
2475 btrfs_free_path(path);
2476 return PTR_ERR(trans);
2477 }
2478
2479 key.objectid = BTRFS_BALANCE_OBJECTID;
2480 key.type = BTRFS_BALANCE_ITEM_KEY;
2481 key.offset = 0;
2482
2483 ret = btrfs_insert_empty_item(trans, root, path, &key,
2484 sizeof(*item));
2485 if (ret)
2486 goto out;
2487
2488 leaf = path->nodes[0];
2489 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2490
2491 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2492
2493 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2494 btrfs_set_balance_data(leaf, item, &disk_bargs);
2495 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2496 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2497 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2498 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2499
2500 btrfs_set_balance_flags(leaf, item, bctl->flags);
2501
2502 btrfs_mark_buffer_dirty(leaf);
2503out:
2504 btrfs_free_path(path);
2505 err = btrfs_commit_transaction(trans, root);
2506 if (err && !ret)
2507 ret = err;
2508 return ret;
2509}
2510
2511static int del_balance_item(struct btrfs_root *root)
2512{
2513 struct btrfs_trans_handle *trans;
2514 struct btrfs_path *path;
2515 struct btrfs_key key;
2516 int ret, err;
2517
2518 path = btrfs_alloc_path();
2519 if (!path)
2520 return -ENOMEM;
2521
2522 trans = btrfs_start_transaction(root, 0);
2523 if (IS_ERR(trans)) {
2524 btrfs_free_path(path);
2525 return PTR_ERR(trans);
2526 }
2527
2528 key.objectid = BTRFS_BALANCE_OBJECTID;
2529 key.type = BTRFS_BALANCE_ITEM_KEY;
2530 key.offset = 0;
2531
2532 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2533 if (ret < 0)
2534 goto out;
2535 if (ret > 0) {
2536 ret = -ENOENT;
2537 goto out;
2538 }
2539
2540 ret = btrfs_del_item(trans, root, path);
2541out:
2542 btrfs_free_path(path);
2543 err = btrfs_commit_transaction(trans, root);
2544 if (err && !ret)
2545 ret = err;
2546 return ret;
2547}
2548
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002549/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002550 * This is a heuristic used to reduce the number of chunks balanced on
2551 * resume after balance was interrupted.
2552 */
2553static void update_balance_args(struct btrfs_balance_control *bctl)
2554{
2555 /*
2556 * Turn on soft mode for chunk types that were being converted.
2557 */
2558 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2559 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2560 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2561 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2562 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2563 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2564
2565 /*
2566 * Turn on usage filter if is not already used. The idea is
2567 * that chunks that we have already balanced should be
2568 * reasonably full. Don't do it for chunks that are being
2569 * converted - that will keep us from relocating unconverted
2570 * (albeit full) chunks.
2571 */
2572 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2573 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2574 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2575 bctl->data.usage = 90;
2576 }
2577 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2578 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2579 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2580 bctl->sys.usage = 90;
2581 }
2582 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2583 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2584 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2585 bctl->meta.usage = 90;
2586 }
2587}
2588
2589/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002590 * Should be called with both balance and volume mutexes held to
2591 * serialize other volume operations (add_dev/rm_dev/resize) with
2592 * restriper. Same goes for unset_balance_control.
2593 */
2594static void set_balance_control(struct btrfs_balance_control *bctl)
2595{
2596 struct btrfs_fs_info *fs_info = bctl->fs_info;
2597
2598 BUG_ON(fs_info->balance_ctl);
2599
2600 spin_lock(&fs_info->balance_lock);
2601 fs_info->balance_ctl = bctl;
2602 spin_unlock(&fs_info->balance_lock);
2603}
2604
2605static void unset_balance_control(struct btrfs_fs_info *fs_info)
2606{
2607 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2608
2609 BUG_ON(!fs_info->balance_ctl);
2610
2611 spin_lock(&fs_info->balance_lock);
2612 fs_info->balance_ctl = NULL;
2613 spin_unlock(&fs_info->balance_lock);
2614
2615 kfree(bctl);
2616}
2617
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002618/*
2619 * Balance filters. Return 1 if chunk should be filtered out
2620 * (should not be balanced).
2621 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002622static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002623 struct btrfs_balance_args *bargs)
2624{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002625 chunk_type = chunk_to_extended(chunk_type) &
2626 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002627
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002628 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002629 return 0;
2630
2631 return 1;
2632}
2633
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002634static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2635 struct btrfs_balance_args *bargs)
2636{
2637 struct btrfs_block_group_cache *cache;
2638 u64 chunk_used, user_thresh;
2639 int ret = 1;
2640
2641 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2642 chunk_used = btrfs_block_group_used(&cache->item);
2643
2644 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2645 if (chunk_used < user_thresh)
2646 ret = 0;
2647
2648 btrfs_put_block_group(cache);
2649 return ret;
2650}
2651
Ilya Dryomov409d4042012-01-16 22:04:47 +02002652static int chunk_devid_filter(struct extent_buffer *leaf,
2653 struct btrfs_chunk *chunk,
2654 struct btrfs_balance_args *bargs)
2655{
2656 struct btrfs_stripe *stripe;
2657 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2658 int i;
2659
2660 for (i = 0; i < num_stripes; i++) {
2661 stripe = btrfs_stripe_nr(chunk, i);
2662 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2663 return 0;
2664 }
2665
2666 return 1;
2667}
2668
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002669/* [pstart, pend) */
2670static int chunk_drange_filter(struct extent_buffer *leaf,
2671 struct btrfs_chunk *chunk,
2672 u64 chunk_offset,
2673 struct btrfs_balance_args *bargs)
2674{
2675 struct btrfs_stripe *stripe;
2676 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2677 u64 stripe_offset;
2678 u64 stripe_length;
2679 int factor;
2680 int i;
2681
2682 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2683 return 0;
2684
2685 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002686 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2687 factor = num_stripes / 2;
2688 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2689 factor = num_stripes - 1;
2690 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2691 factor = num_stripes - 2;
2692 } else {
2693 factor = num_stripes;
2694 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002695
2696 for (i = 0; i < num_stripes; i++) {
2697 stripe = btrfs_stripe_nr(chunk, i);
2698 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2699 continue;
2700
2701 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2702 stripe_length = btrfs_chunk_length(leaf, chunk);
2703 do_div(stripe_length, factor);
2704
2705 if (stripe_offset < bargs->pend &&
2706 stripe_offset + stripe_length > bargs->pstart)
2707 return 0;
2708 }
2709
2710 return 1;
2711}
2712
Ilya Dryomovea671762012-01-16 22:04:48 +02002713/* [vstart, vend) */
2714static int chunk_vrange_filter(struct extent_buffer *leaf,
2715 struct btrfs_chunk *chunk,
2716 u64 chunk_offset,
2717 struct btrfs_balance_args *bargs)
2718{
2719 if (chunk_offset < bargs->vend &&
2720 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2721 /* at least part of the chunk is inside this vrange */
2722 return 0;
2723
2724 return 1;
2725}
2726
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002727static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002728 struct btrfs_balance_args *bargs)
2729{
2730 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2731 return 0;
2732
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002733 chunk_type = chunk_to_extended(chunk_type) &
2734 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002735
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002736 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002737 return 1;
2738
2739 return 0;
2740}
2741
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002742static int should_balance_chunk(struct btrfs_root *root,
2743 struct extent_buffer *leaf,
2744 struct btrfs_chunk *chunk, u64 chunk_offset)
2745{
2746 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2747 struct btrfs_balance_args *bargs = NULL;
2748 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2749
2750 /* type filter */
2751 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2752 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2753 return 0;
2754 }
2755
2756 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2757 bargs = &bctl->data;
2758 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2759 bargs = &bctl->sys;
2760 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2761 bargs = &bctl->meta;
2762
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002763 /* profiles filter */
2764 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2765 chunk_profiles_filter(chunk_type, bargs)) {
2766 return 0;
2767 }
2768
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002769 /* usage filter */
2770 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2771 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2772 return 0;
2773 }
2774
Ilya Dryomov409d4042012-01-16 22:04:47 +02002775 /* devid filter */
2776 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2777 chunk_devid_filter(leaf, chunk, bargs)) {
2778 return 0;
2779 }
2780
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002781 /* drange filter, makes sense only with devid filter */
2782 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2783 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2784 return 0;
2785 }
2786
Ilya Dryomovea671762012-01-16 22:04:48 +02002787 /* vrange filter */
2788 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2789 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2790 return 0;
2791 }
2792
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002793 /* soft profile changing mode */
2794 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2795 chunk_soft_convert_filter(chunk_type, bargs)) {
2796 return 0;
2797 }
2798
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002799 return 1;
2800}
2801
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002802static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002803{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002804 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002805 struct btrfs_root *chunk_root = fs_info->chunk_root;
2806 struct btrfs_root *dev_root = fs_info->dev_root;
2807 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002808 struct btrfs_device *device;
2809 u64 old_size;
2810 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002811 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002812 struct btrfs_path *path;
2813 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002814 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002815 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002816 struct extent_buffer *leaf;
2817 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002818 int ret;
2819 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002820 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002821
Chris Masonec44a352008-04-28 15:29:52 -04002822 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002823 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002824 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002825 old_size = device->total_bytes;
2826 size_to_free = div_factor(old_size, 1);
2827 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002828 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002829 device->total_bytes - device->bytes_used > size_to_free ||
2830 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002831 continue;
2832
2833 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002834 if (ret == -ENOSPC)
2835 break;
Chris Masonec44a352008-04-28 15:29:52 -04002836 BUG_ON(ret);
2837
Yan, Zhenga22285a2010-05-16 10:48:46 -04002838 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002839 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002840
2841 ret = btrfs_grow_device(trans, device, old_size);
2842 BUG_ON(ret);
2843
2844 btrfs_end_transaction(trans, dev_root);
2845 }
2846
2847 /* step two, relocate all the chunks */
2848 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002849 if (!path) {
2850 ret = -ENOMEM;
2851 goto error;
2852 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002853
2854 /* zero out stat counters */
2855 spin_lock(&fs_info->balance_lock);
2856 memset(&bctl->stat, 0, sizeof(bctl->stat));
2857 spin_unlock(&fs_info->balance_lock);
2858again:
Chris Masonec44a352008-04-28 15:29:52 -04002859 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2860 key.offset = (u64)-1;
2861 key.type = BTRFS_CHUNK_ITEM_KEY;
2862
Chris Masond3977122009-01-05 21:25:51 -05002863 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002864 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002865 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002866 ret = -ECANCELED;
2867 goto error;
2868 }
2869
Chris Masonec44a352008-04-28 15:29:52 -04002870 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2871 if (ret < 0)
2872 goto error;
2873
2874 /*
2875 * this shouldn't happen, it means the last relocate
2876 * failed
2877 */
2878 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002879 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002880
2881 ret = btrfs_previous_item(chunk_root, path, 0,
2882 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002883 if (ret) {
2884 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002885 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002886 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002887
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002888 leaf = path->nodes[0];
2889 slot = path->slots[0];
2890 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2891
Chris Masonec44a352008-04-28 15:29:52 -04002892 if (found_key.objectid != key.objectid)
2893 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002894
Chris Masonec44a352008-04-28 15:29:52 -04002895 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002896 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002897 break;
2898
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002899 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2900
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002901 if (!counting) {
2902 spin_lock(&fs_info->balance_lock);
2903 bctl->stat.considered++;
2904 spin_unlock(&fs_info->balance_lock);
2905 }
2906
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002907 ret = should_balance_chunk(chunk_root, leaf, chunk,
2908 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002909 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002910 if (!ret)
2911 goto loop;
2912
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002913 if (counting) {
2914 spin_lock(&fs_info->balance_lock);
2915 bctl->stat.expected++;
2916 spin_unlock(&fs_info->balance_lock);
2917 goto loop;
2918 }
2919
Chris Masonec44a352008-04-28 15:29:52 -04002920 ret = btrfs_relocate_chunk(chunk_root,
2921 chunk_root->root_key.objectid,
2922 found_key.objectid,
2923 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002924 if (ret && ret != -ENOSPC)
2925 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002926 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002927 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002928 } else {
2929 spin_lock(&fs_info->balance_lock);
2930 bctl->stat.completed++;
2931 spin_unlock(&fs_info->balance_lock);
2932 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002933loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002934 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002935 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002936
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002937 if (counting) {
2938 btrfs_release_path(path);
2939 counting = false;
2940 goto again;
2941 }
Chris Masonec44a352008-04-28 15:29:52 -04002942error:
2943 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002944 if (enospc_errors) {
2945 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2946 enospc_errors);
2947 if (!ret)
2948 ret = -ENOSPC;
2949 }
2950
Chris Masonec44a352008-04-28 15:29:52 -04002951 return ret;
2952}
2953
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002954/**
2955 * alloc_profile_is_valid - see if a given profile is valid and reduced
2956 * @flags: profile to validate
2957 * @extended: if true @flags is treated as an extended profile
2958 */
2959static int alloc_profile_is_valid(u64 flags, int extended)
2960{
2961 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2962 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2963
2964 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2965
2966 /* 1) check that all other bits are zeroed */
2967 if (flags & ~mask)
2968 return 0;
2969
2970 /* 2) see if profile is reduced */
2971 if (flags == 0)
2972 return !extended; /* "0" is valid for usual profiles */
2973
2974 /* true if exactly one bit set */
2975 return (flags & (flags - 1)) == 0;
2976}
2977
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002978static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2979{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002980 /* cancel requested || normal exit path */
2981 return atomic_read(&fs_info->balance_cancel_req) ||
2982 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2983 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002984}
2985
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002986static void __cancel_balance(struct btrfs_fs_info *fs_info)
2987{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002988 int ret;
2989
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002990 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002991 ret = del_balance_item(fs_info->tree_root);
2992 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002993}
2994
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002995void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002996 struct btrfs_ioctl_balance_args *bargs);
2997
2998/*
2999 * Should be called with both balance and volume mutexes held
3000 */
3001int btrfs_balance(struct btrfs_balance_control *bctl,
3002 struct btrfs_ioctl_balance_args *bargs)
3003{
3004 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003005 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003006 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003007 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003008 u64 num_devices;
David Woodhouse53b381b2013-01-29 18:40:14 -05003009 int cancel = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003010
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003011 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003012 atomic_read(&fs_info->balance_pause_req) ||
3013 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003014 ret = -EINVAL;
3015 goto out;
3016 }
3017
Ilya Dryomove4837f82012-03-27 17:09:17 +03003018 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3019 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3020 mixed = 1;
3021
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003022 /*
3023 * In case of mixed groups both data and meta should be picked,
3024 * and identical options should be given for both of them.
3025 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003026 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3027 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003028 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3029 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3030 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3031 printk(KERN_ERR "btrfs: with mixed groups data and "
3032 "metadata balance options must be the same\n");
3033 ret = -EINVAL;
3034 goto out;
3035 }
3036 }
3037
Stefan Behrens8dabb742012-11-06 13:15:27 +01003038 num_devices = fs_info->fs_devices->num_devices;
3039 btrfs_dev_replace_lock(&fs_info->dev_replace);
3040 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3041 BUG_ON(num_devices < 1);
3042 num_devices--;
3043 }
3044 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003045 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003046 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003047 allowed |= BTRFS_BLOCK_GROUP_DUP;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003048 else if (num_devices < 4)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003049 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3050 else
3051 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003052 BTRFS_BLOCK_GROUP_RAID10 |
3053 BTRFS_BLOCK_GROUP_RAID5 |
3054 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003055
Ilya Dryomov6728b192012-03-27 17:09:17 +03003056 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3057 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3058 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003059 printk(KERN_ERR "btrfs: unable to start balance with target "
3060 "data profile %llu\n",
3061 (unsigned long long)bctl->data.target);
3062 ret = -EINVAL;
3063 goto out;
3064 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003065 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3066 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3067 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003068 printk(KERN_ERR "btrfs: unable to start balance with target "
3069 "metadata profile %llu\n",
3070 (unsigned long long)bctl->meta.target);
3071 ret = -EINVAL;
3072 goto out;
3073 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003074 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3075 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3076 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003077 printk(KERN_ERR "btrfs: unable to start balance with target "
3078 "system profile %llu\n",
3079 (unsigned long long)bctl->sys.target);
3080 ret = -EINVAL;
3081 goto out;
3082 }
3083
Ilya Dryomove4837f82012-03-27 17:09:17 +03003084 /* allow dup'ed data chunks only in mixed mode */
3085 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003086 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003087 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3088 ret = -EINVAL;
3089 goto out;
3090 }
3091
3092 /* allow to reduce meta or sys integrity only if force set */
3093 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003094 BTRFS_BLOCK_GROUP_RAID10 |
3095 BTRFS_BLOCK_GROUP_RAID5 |
3096 BTRFS_BLOCK_GROUP_RAID6;
3097
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003098 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3099 (fs_info->avail_system_alloc_bits & allowed) &&
3100 !(bctl->sys.target & allowed)) ||
3101 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3102 (fs_info->avail_metadata_alloc_bits & allowed) &&
3103 !(bctl->meta.target & allowed))) {
3104 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3105 printk(KERN_INFO "btrfs: force reducing metadata "
3106 "integrity\n");
3107 } else {
3108 printk(KERN_ERR "btrfs: balance will reduce metadata "
3109 "integrity, use force if you want this\n");
3110 ret = -EINVAL;
3111 goto out;
3112 }
3113 }
3114
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003115 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3116 int num_tolerated_disk_barrier_failures;
3117 u64 target = bctl->sys.target;
3118
3119 num_tolerated_disk_barrier_failures =
3120 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3121 if (num_tolerated_disk_barrier_failures > 0 &&
3122 (target &
3123 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3124 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3125 num_tolerated_disk_barrier_failures = 0;
3126 else if (num_tolerated_disk_barrier_failures > 1 &&
3127 (target &
3128 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3129 num_tolerated_disk_barrier_failures = 1;
3130
3131 fs_info->num_tolerated_disk_barrier_failures =
3132 num_tolerated_disk_barrier_failures;
3133 }
3134
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003135 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003136 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003137 goto out;
3138
Ilya Dryomov59641012012-01-16 22:04:48 +02003139 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3140 BUG_ON(ret == -EEXIST);
3141 set_balance_control(bctl);
3142 } else {
3143 BUG_ON(ret != -EEXIST);
3144 spin_lock(&fs_info->balance_lock);
3145 update_balance_args(bctl);
3146 spin_unlock(&fs_info->balance_lock);
3147 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003148
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003149 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003150 mutex_unlock(&fs_info->balance_mutex);
3151
3152 ret = __btrfs_balance(fs_info);
3153
3154 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003155 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003156
3157 if (bargs) {
3158 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003159 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003160 }
3161
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003162 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
David Woodhouse53b381b2013-01-29 18:40:14 -05003163 balance_need_close(fs_info))
3164 cancel = 1;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003165
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003166 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3167 fs_info->num_tolerated_disk_barrier_failures =
3168 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3169 }
3170
David Woodhouse53b381b2013-01-29 18:40:14 -05003171 if (cancel)
3172 __cancel_balance(fs_info);
3173
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003174 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003175
3176 return ret;
3177out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003178 if (bctl->flags & BTRFS_BALANCE_RESUME)
3179 __cancel_balance(fs_info);
3180 else
3181 kfree(bctl);
3182 return ret;
3183}
3184
3185static int balance_kthread(void *data)
3186{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003187 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003188 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003189
3190 mutex_lock(&fs_info->volume_mutex);
3191 mutex_lock(&fs_info->balance_mutex);
3192
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003193 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003194 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003195 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003196 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003197
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003198 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov59641012012-01-16 22:04:48 +02003199 mutex_unlock(&fs_info->balance_mutex);
3200 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003201
Ilya Dryomov59641012012-01-16 22:04:48 +02003202 return ret;
3203}
3204
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003205int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3206{
3207 struct task_struct *tsk;
3208
3209 spin_lock(&fs_info->balance_lock);
3210 if (!fs_info->balance_ctl) {
3211 spin_unlock(&fs_info->balance_lock);
3212 return 0;
3213 }
3214 spin_unlock(&fs_info->balance_lock);
3215
3216 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3217 printk(KERN_INFO "btrfs: force skipping balance\n");
3218 return 0;
3219 }
3220
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003221 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003222 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3223 if (IS_ERR(tsk))
3224 return PTR_ERR(tsk);
3225
3226 return 0;
3227}
3228
Ilya Dryomov68310a52012-06-22 12:24:12 -06003229int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003230{
Ilya Dryomov59641012012-01-16 22:04:48 +02003231 struct btrfs_balance_control *bctl;
3232 struct btrfs_balance_item *item;
3233 struct btrfs_disk_balance_args disk_bargs;
3234 struct btrfs_path *path;
3235 struct extent_buffer *leaf;
3236 struct btrfs_key key;
3237 int ret;
3238
3239 path = btrfs_alloc_path();
3240 if (!path)
3241 return -ENOMEM;
3242
Ilya Dryomov68310a52012-06-22 12:24:12 -06003243 key.objectid = BTRFS_BALANCE_OBJECTID;
3244 key.type = BTRFS_BALANCE_ITEM_KEY;
3245 key.offset = 0;
3246
3247 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3248 if (ret < 0)
3249 goto out;
3250 if (ret > 0) { /* ret = -ENOENT; */
3251 ret = 0;
3252 goto out;
3253 }
3254
Ilya Dryomov59641012012-01-16 22:04:48 +02003255 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3256 if (!bctl) {
3257 ret = -ENOMEM;
3258 goto out;
3259 }
3260
Ilya Dryomov59641012012-01-16 22:04:48 +02003261 leaf = path->nodes[0];
3262 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3263
Ilya Dryomov68310a52012-06-22 12:24:12 -06003264 bctl->fs_info = fs_info;
3265 bctl->flags = btrfs_balance_flags(leaf, item);
3266 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003267
3268 btrfs_balance_data(leaf, item, &disk_bargs);
3269 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3270 btrfs_balance_meta(leaf, item, &disk_bargs);
3271 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3272 btrfs_balance_sys(leaf, item, &disk_bargs);
3273 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3274
Ilya Dryomov68310a52012-06-22 12:24:12 -06003275 mutex_lock(&fs_info->volume_mutex);
3276 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003277
Ilya Dryomov68310a52012-06-22 12:24:12 -06003278 set_balance_control(bctl);
3279
3280 mutex_unlock(&fs_info->balance_mutex);
3281 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003282out:
3283 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003284 return ret;
3285}
3286
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003287int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3288{
3289 int ret = 0;
3290
3291 mutex_lock(&fs_info->balance_mutex);
3292 if (!fs_info->balance_ctl) {
3293 mutex_unlock(&fs_info->balance_mutex);
3294 return -ENOTCONN;
3295 }
3296
3297 if (atomic_read(&fs_info->balance_running)) {
3298 atomic_inc(&fs_info->balance_pause_req);
3299 mutex_unlock(&fs_info->balance_mutex);
3300
3301 wait_event(fs_info->balance_wait_q,
3302 atomic_read(&fs_info->balance_running) == 0);
3303
3304 mutex_lock(&fs_info->balance_mutex);
3305 /* we are good with balance_ctl ripped off from under us */
3306 BUG_ON(atomic_read(&fs_info->balance_running));
3307 atomic_dec(&fs_info->balance_pause_req);
3308 } else {
3309 ret = -ENOTCONN;
3310 }
3311
3312 mutex_unlock(&fs_info->balance_mutex);
3313 return ret;
3314}
3315
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003316int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3317{
3318 mutex_lock(&fs_info->balance_mutex);
3319 if (!fs_info->balance_ctl) {
3320 mutex_unlock(&fs_info->balance_mutex);
3321 return -ENOTCONN;
3322 }
3323
3324 atomic_inc(&fs_info->balance_cancel_req);
3325 /*
3326 * if we are running just wait and return, balance item is
3327 * deleted in btrfs_balance in this case
3328 */
3329 if (atomic_read(&fs_info->balance_running)) {
3330 mutex_unlock(&fs_info->balance_mutex);
3331 wait_event(fs_info->balance_wait_q,
3332 atomic_read(&fs_info->balance_running) == 0);
3333 mutex_lock(&fs_info->balance_mutex);
3334 } else {
3335 /* __cancel_balance needs volume_mutex */
3336 mutex_unlock(&fs_info->balance_mutex);
3337 mutex_lock(&fs_info->volume_mutex);
3338 mutex_lock(&fs_info->balance_mutex);
3339
3340 if (fs_info->balance_ctl)
3341 __cancel_balance(fs_info);
3342
3343 mutex_unlock(&fs_info->volume_mutex);
3344 }
3345
3346 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3347 atomic_dec(&fs_info->balance_cancel_req);
3348 mutex_unlock(&fs_info->balance_mutex);
3349 return 0;
3350}
3351
Chris Mason8f18cf12008-04-25 16:53:30 -04003352/*
3353 * shrinking a device means finding all of the device extents past
3354 * the new size, and then following the back refs to the chunks.
3355 * The chunk relocation code actually frees the device extent
3356 */
3357int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3358{
3359 struct btrfs_trans_handle *trans;
3360 struct btrfs_root *root = device->dev_root;
3361 struct btrfs_dev_extent *dev_extent = NULL;
3362 struct btrfs_path *path;
3363 u64 length;
3364 u64 chunk_tree;
3365 u64 chunk_objectid;
3366 u64 chunk_offset;
3367 int ret;
3368 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003369 int failed = 0;
3370 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003371 struct extent_buffer *l;
3372 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003373 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003374 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003375 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003376 u64 diff = device->total_bytes - new_size;
3377
Stefan Behrens63a212a2012-11-05 18:29:28 +01003378 if (device->is_tgtdev_for_dev_replace)
3379 return -EINVAL;
3380
Chris Mason8f18cf12008-04-25 16:53:30 -04003381 path = btrfs_alloc_path();
3382 if (!path)
3383 return -ENOMEM;
3384
Chris Mason8f18cf12008-04-25 16:53:30 -04003385 path->reada = 2;
3386
Chris Mason7d9eb122008-07-08 14:19:17 -04003387 lock_chunks(root);
3388
Chris Mason8f18cf12008-04-25 16:53:30 -04003389 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003390 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003391 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003392 spin_lock(&root->fs_info->free_chunk_lock);
3393 root->fs_info->free_chunk_space -= diff;
3394 spin_unlock(&root->fs_info->free_chunk_lock);
3395 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003396 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003397
Josef Bacikba1bf482009-09-11 16:11:19 -04003398again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003399 key.objectid = device->devid;
3400 key.offset = (u64)-1;
3401 key.type = BTRFS_DEV_EXTENT_KEY;
3402
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003403 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003404 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3405 if (ret < 0)
3406 goto done;
3407
3408 ret = btrfs_previous_item(root, path, 0, key.type);
3409 if (ret < 0)
3410 goto done;
3411 if (ret) {
3412 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003413 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003414 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003415 }
3416
3417 l = path->nodes[0];
3418 slot = path->slots[0];
3419 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3420
Josef Bacikba1bf482009-09-11 16:11:19 -04003421 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003422 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003423 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003424 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003425
3426 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3427 length = btrfs_dev_extent_length(l, dev_extent);
3428
Josef Bacikba1bf482009-09-11 16:11:19 -04003429 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003430 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003431 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003432 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003433
3434 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3435 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3436 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003437 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003438
3439 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3440 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003441 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003442 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003443 if (ret == -ENOSPC)
3444 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003445 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003446
3447 if (failed && !retried) {
3448 failed = 0;
3449 retried = true;
3450 goto again;
3451 } else if (failed && retried) {
3452 ret = -ENOSPC;
3453 lock_chunks(root);
3454
3455 device->total_bytes = old_size;
3456 if (device->writeable)
3457 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003458 spin_lock(&root->fs_info->free_chunk_lock);
3459 root->fs_info->free_chunk_space += diff;
3460 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003461 unlock_chunks(root);
3462 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003463 }
3464
Chris Balld6397ba2009-04-27 07:29:03 -04003465 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003466 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003467 if (IS_ERR(trans)) {
3468 ret = PTR_ERR(trans);
3469 goto done;
3470 }
3471
Chris Balld6397ba2009-04-27 07:29:03 -04003472 lock_chunks(root);
3473
3474 device->disk_total_bytes = new_size;
3475 /* Now btrfs_update_device() will change the on-disk size. */
3476 ret = btrfs_update_device(trans, device);
3477 if (ret) {
3478 unlock_chunks(root);
3479 btrfs_end_transaction(trans, root);
3480 goto done;
3481 }
3482 WARN_ON(diff > old_total);
3483 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3484 unlock_chunks(root);
3485 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003486done:
3487 btrfs_free_path(path);
3488 return ret;
3489}
3490
Li Zefan125ccb02011-12-08 15:07:24 +08003491static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003492 struct btrfs_key *key,
3493 struct btrfs_chunk *chunk, int item_size)
3494{
David Sterba6c417612011-04-13 15:41:04 +02003495 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003496 struct btrfs_disk_key disk_key;
3497 u32 array_size;
3498 u8 *ptr;
3499
3500 array_size = btrfs_super_sys_array_size(super_copy);
3501 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3502 return -EFBIG;
3503
3504 ptr = super_copy->sys_chunk_array + array_size;
3505 btrfs_cpu_key_to_disk(&disk_key, key);
3506 memcpy(ptr, &disk_key, sizeof(disk_key));
3507 ptr += sizeof(disk_key);
3508 memcpy(ptr, chunk, item_size);
3509 item_size += sizeof(disk_key);
3510 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3511 return 0;
3512}
3513
Miao Xieb2117a32011-01-05 10:07:28 +00003514/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003515 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003516 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003517static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003518{
Arne Jansen73c5de02011-04-12 12:07:57 +02003519 const struct btrfs_device_info *di_a = a;
3520 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003521
Arne Jansen73c5de02011-04-12 12:07:57 +02003522 if (di_a->max_avail > di_b->max_avail)
3523 return -1;
3524 if (di_a->max_avail < di_b->max_avail)
3525 return 1;
3526 if (di_a->total_avail > di_b->total_avail)
3527 return -1;
3528 if (di_a->total_avail < di_b->total_avail)
3529 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003530 return 0;
3531}
3532
Liu Bo31e50222012-11-21 14:18:10 +00003533struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
David Woodhouse53b381b2013-01-29 18:40:14 -05003534 /*
3535 * sub_stripes info for map,
3536 * dev_stripes -- stripes per dev, 2 for DUP, 1 other wise
3537 * devs_max -- max devices per stripe, 0 for unlimited
3538 * devs_min -- min devices per stripe
3539 * devs_increment -- ndevs must be a multiple of this
3540 * ncopies -- how many copies of the data we have
3541 */
Liu Bo31e50222012-11-21 14:18:10 +00003542 { 2, 1, 0, 4, 2, 2 /* raid10 */ },
3543 { 1, 1, 2, 2, 2, 2 /* raid1 */ },
3544 { 1, 2, 1, 1, 1, 2 /* dup */ },
3545 { 1, 1, 0, 2, 1, 1 /* raid0 */ },
3546 { 1, 1, 0, 1, 1, 1 /* single */ },
David Woodhouse53b381b2013-01-29 18:40:14 -05003547 { 1, 1, 0, 2, 1, 2 /* raid5 */ },
3548 { 1, 1, 0, 3, 1, 3 /* raid6 */ },
Liu Bo31e50222012-11-21 14:18:10 +00003549};
3550
David Woodhouse53b381b2013-01-29 18:40:14 -05003551static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3552{
3553 /* TODO allow them to set a preferred stripe size */
3554 return 64 * 1024;
3555}
3556
3557static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3558{
3559 u64 features;
3560
3561 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3562 return;
3563
3564 features = btrfs_super_incompat_flags(info->super_copy);
3565 if (features & BTRFS_FEATURE_INCOMPAT_RAID56)
3566 return;
3567
3568 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
3569 btrfs_set_super_incompat_flags(info->super_copy, features);
3570 printk(KERN_INFO "btrfs: setting RAID5/6 feature flag\n");
3571}
3572
Miao Xieb2117a32011-01-05 10:07:28 +00003573static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3574 struct btrfs_root *extent_root,
3575 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003576 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003577 u64 start, u64 type)
3578{
3579 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003580 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3581 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003582 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003583 struct extent_map_tree *em_tree;
3584 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003585 struct btrfs_device_info *devices_info = NULL;
3586 u64 total_avail;
3587 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05003588 int data_stripes; /* number of stripes that count for
3589 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02003590 int sub_stripes; /* sub_stripes info for map */
3591 int dev_stripes; /* stripes per dev */
3592 int devs_max; /* max devs to use */
3593 int devs_min; /* min devs needed */
3594 int devs_increment; /* ndevs has to be a multiple of this */
3595 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003596 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003597 u64 max_stripe_size;
3598 u64 max_chunk_size;
3599 u64 stripe_size;
3600 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05003601 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02003602 int ndevs;
3603 int i;
3604 int j;
Liu Bo31e50222012-11-21 14:18:10 +00003605 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00003606
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003607 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003608
Miao Xieb2117a32011-01-05 10:07:28 +00003609 if (list_empty(&fs_devices->alloc_list))
3610 return -ENOSPC;
3611
Liu Bo31e50222012-11-21 14:18:10 +00003612 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02003613
Liu Bo31e50222012-11-21 14:18:10 +00003614 sub_stripes = btrfs_raid_array[index].sub_stripes;
3615 dev_stripes = btrfs_raid_array[index].dev_stripes;
3616 devs_max = btrfs_raid_array[index].devs_max;
3617 devs_min = btrfs_raid_array[index].devs_min;
3618 devs_increment = btrfs_raid_array[index].devs_increment;
3619 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02003620
3621 if (type & BTRFS_BLOCK_GROUP_DATA) {
3622 max_stripe_size = 1024 * 1024 * 1024;
3623 max_chunk_size = 10 * max_stripe_size;
3624 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003625 /* for larger filesystems, use larger metadata chunks */
3626 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3627 max_stripe_size = 1024 * 1024 * 1024;
3628 else
3629 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003630 max_chunk_size = max_stripe_size;
3631 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003632 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003633 max_chunk_size = 2 * max_stripe_size;
3634 } else {
3635 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3636 type);
3637 BUG_ON(1);
3638 }
3639
3640 /* we don't want a chunk larger than 10% of writeable space */
3641 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3642 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003643
3644 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3645 GFP_NOFS);
3646 if (!devices_info)
3647 return -ENOMEM;
3648
Arne Jansen73c5de02011-04-12 12:07:57 +02003649 cur = fs_devices->alloc_list.next;
3650
3651 /*
3652 * in the first pass through the devices list, we gather information
3653 * about the available holes on each device.
3654 */
3655 ndevs = 0;
3656 while (cur != &fs_devices->alloc_list) {
3657 struct btrfs_device *device;
3658 u64 max_avail;
3659 u64 dev_offset;
3660
3661 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3662
3663 cur = cur->next;
3664
3665 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003666 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003667 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003668 continue;
3669 }
3670
Stefan Behrens63a212a2012-11-05 18:29:28 +01003671 if (!device->in_fs_metadata ||
3672 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003673 continue;
3674
3675 if (device->total_bytes > device->bytes_used)
3676 total_avail = device->total_bytes - device->bytes_used;
3677 else
3678 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003679
3680 /* If there is no space on this device, skip it. */
3681 if (total_avail == 0)
3682 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003683
Li Zefan125ccb02011-12-08 15:07:24 +08003684 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003685 max_stripe_size * dev_stripes,
3686 &dev_offset, &max_avail);
3687 if (ret && ret != -ENOSPC)
3688 goto error;
3689
3690 if (ret == 0)
3691 max_avail = max_stripe_size * dev_stripes;
3692
3693 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3694 continue;
3695
3696 devices_info[ndevs].dev_offset = dev_offset;
3697 devices_info[ndevs].max_avail = max_avail;
3698 devices_info[ndevs].total_avail = total_avail;
3699 devices_info[ndevs].dev = device;
3700 ++ndevs;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003701 WARN_ON(ndevs > fs_devices->rw_devices);
Arne Jansen73c5de02011-04-12 12:07:57 +02003702 }
3703
3704 /*
3705 * now sort the devices by hole size / available space
3706 */
3707 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3708 btrfs_cmp_device_info, NULL);
3709
3710 /* round down to number of usable stripes */
3711 ndevs -= ndevs % devs_increment;
3712
3713 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3714 ret = -ENOSPC;
3715 goto error;
3716 }
3717
3718 if (devs_max && ndevs > devs_max)
3719 ndevs = devs_max;
3720 /*
3721 * the primary goal is to maximize the number of stripes, so use as many
3722 * devices as possible, even if the stripes are not maximum sized.
3723 */
3724 stripe_size = devices_info[ndevs-1].max_avail;
3725 num_stripes = ndevs * dev_stripes;
3726
David Woodhouse53b381b2013-01-29 18:40:14 -05003727 /*
3728 * this will have to be fixed for RAID1 and RAID10 over
3729 * more drives
3730 */
3731 data_stripes = num_stripes / ncopies;
3732
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003733 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003734 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003735 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003736 }
David Woodhouse53b381b2013-01-29 18:40:14 -05003737 if (type & BTRFS_BLOCK_GROUP_RAID5) {
3738 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
3739 btrfs_super_stripesize(info->super_copy));
3740 data_stripes = num_stripes - 1;
3741 }
3742 if (type & BTRFS_BLOCK_GROUP_RAID6) {
3743 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
3744 btrfs_super_stripesize(info->super_copy));
3745 data_stripes = num_stripes - 2;
3746 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003747 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003748
3749 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05003750 do_div(stripe_size, raid_stripe_len);
3751 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02003752
Miao Xieb2117a32011-01-05 10:07:28 +00003753 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3754 if (!map) {
3755 ret = -ENOMEM;
3756 goto error;
3757 }
3758 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003759
Arne Jansen73c5de02011-04-12 12:07:57 +02003760 for (i = 0; i < ndevs; ++i) {
3761 for (j = 0; j < dev_stripes; ++j) {
3762 int s = i * dev_stripes + j;
3763 map->stripes[s].dev = devices_info[i].dev;
3764 map->stripes[s].physical = devices_info[i].dev_offset +
3765 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003766 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003767 }
Chris Mason593060d2008-03-25 16:50:33 -04003768 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05003769 map->stripe_len = raid_stripe_len;
3770 map->io_align = raid_stripe_len;
3771 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04003772 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003773 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003774
Yan Zheng2b820322008-11-17 21:11:30 -05003775 *map_ret = map;
David Woodhouse53b381b2013-01-29 18:40:14 -05003776 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003777
Arne Jansen73c5de02011-04-12 12:07:57 +02003778 *stripe_size_out = stripe_size;
3779 *num_bytes_out = num_bytes;
3780
3781 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003782
David Sterba172ddd62011-04-21 00:48:27 +02003783 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003784 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003785 ret = -ENOMEM;
3786 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003787 }
Chris Mason0b86a832008-03-24 15:01:56 -04003788 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003789 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003790 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003791 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003792 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003793
Chris Mason0b86a832008-03-24 15:01:56 -04003794 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003795 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003796 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003797 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003798 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003799 if (ret)
3800 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003801
3802 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3803 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003804 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 if (ret)
3806 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003807
Arne Jansen73c5de02011-04-12 12:07:57 +02003808 for (i = 0; i < map->num_stripes; ++i) {
3809 struct btrfs_device *device;
3810 u64 dev_offset;
3811
3812 device = map->stripes[i].dev;
3813 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003814
3815 ret = btrfs_alloc_dev_extent(trans, device,
3816 info->chunk_root->root_key.objectid,
3817 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003818 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003819 if (ret) {
3820 btrfs_abort_transaction(trans, extent_root, ret);
3821 goto error;
3822 }
Yan Zheng2b820322008-11-17 21:11:30 -05003823 }
3824
David Woodhouse53b381b2013-01-29 18:40:14 -05003825 check_raid56_incompat_flag(extent_root->fs_info, type);
3826
Miao Xieb2117a32011-01-05 10:07:28 +00003827 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003828 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003829
3830error:
3831 kfree(map);
3832 kfree(devices_info);
3833 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003834}
3835
3836static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3837 struct btrfs_root *extent_root,
3838 struct map_lookup *map, u64 chunk_offset,
3839 u64 chunk_size, u64 stripe_size)
3840{
3841 u64 dev_offset;
3842 struct btrfs_key key;
3843 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3844 struct btrfs_device *device;
3845 struct btrfs_chunk *chunk;
3846 struct btrfs_stripe *stripe;
3847 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3848 int index = 0;
3849 int ret;
3850
3851 chunk = kzalloc(item_size, GFP_NOFS);
3852 if (!chunk)
3853 return -ENOMEM;
3854
3855 index = 0;
3856 while (index < map->num_stripes) {
3857 device = map->stripes[index].dev;
3858 device->bytes_used += stripe_size;
3859 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003860 if (ret)
3861 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003862 index++;
3863 }
3864
Josef Bacik2bf64752011-09-26 17:12:22 -04003865 spin_lock(&extent_root->fs_info->free_chunk_lock);
3866 extent_root->fs_info->free_chunk_space -= (stripe_size *
3867 map->num_stripes);
3868 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3869
Yan Zheng2b820322008-11-17 21:11:30 -05003870 index = 0;
3871 stripe = &chunk->stripe;
3872 while (index < map->num_stripes) {
3873 device = map->stripes[index].dev;
3874 dev_offset = map->stripes[index].physical;
3875
3876 btrfs_set_stack_stripe_devid(stripe, device->devid);
3877 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3878 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3879 stripe++;
3880 index++;
3881 }
3882
3883 btrfs_set_stack_chunk_length(chunk, chunk_size);
3884 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3885 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3886 btrfs_set_stack_chunk_type(chunk, map->type);
3887 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3888 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3889 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3890 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3891 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3892
3893 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3894 key.type = BTRFS_CHUNK_ITEM_KEY;
3895 key.offset = chunk_offset;
3896
3897 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003898
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003899 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3900 /*
3901 * TODO: Cleanup of inserted chunk root in case of
3902 * failure.
3903 */
Li Zefan125ccb02011-12-08 15:07:24 +08003904 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003905 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003906 }
liubo1abe9b82011-03-24 11:18:59 +00003907
Mark Fasheh3acd3952011-09-08 17:40:01 -07003908out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003909 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003910 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003911}
3912
3913/*
3914 * Chunk allocation falls into two parts. The first part does works
3915 * that make the new allocated chunk useable, but not do any operation
3916 * that modifies the chunk tree. The second part does the works that
3917 * require modifying the chunk tree. This division is important for the
3918 * bootstrap process of adding storage to a seed btrfs.
3919 */
3920int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3921 struct btrfs_root *extent_root, u64 type)
3922{
3923 u64 chunk_offset;
3924 u64 chunk_size;
3925 u64 stripe_size;
3926 struct map_lookup *map;
3927 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3928 int ret;
3929
3930 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3931 &chunk_offset);
3932 if (ret)
3933 return ret;
3934
3935 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3936 &stripe_size, chunk_offset, type);
3937 if (ret)
3938 return ret;
3939
3940 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3941 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003942 if (ret)
3943 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003944 return 0;
3945}
3946
Chris Masond3977122009-01-05 21:25:51 -05003947static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003948 struct btrfs_root *root,
3949 struct btrfs_device *device)
3950{
3951 u64 chunk_offset;
3952 u64 sys_chunk_offset;
3953 u64 chunk_size;
3954 u64 sys_chunk_size;
3955 u64 stripe_size;
3956 u64 sys_stripe_size;
3957 u64 alloc_profile;
3958 struct map_lookup *map;
3959 struct map_lookup *sys_map;
3960 struct btrfs_fs_info *fs_info = root->fs_info;
3961 struct btrfs_root *extent_root = fs_info->extent_root;
3962 int ret;
3963
3964 ret = find_next_chunk(fs_info->chunk_root,
3965 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003966 if (ret)
3967 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003968
3969 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003970 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003971 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3972
3973 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3974 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003975 if (ret)
3976 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003977
3978 sys_chunk_offset = chunk_offset + chunk_size;
3979
3980 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003981 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003982 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3983
3984 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3985 &sys_chunk_size, &sys_stripe_size,
3986 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06003987 if (ret) {
3988 btrfs_abort_transaction(trans, root, ret);
3989 goto out;
3990 }
Yan Zheng2b820322008-11-17 21:11:30 -05003991
3992 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06003993 if (ret) {
3994 btrfs_abort_transaction(trans, root, ret);
3995 goto out;
3996 }
Yan Zheng2b820322008-11-17 21:11:30 -05003997
3998 /*
3999 * Modifying chunk tree needs allocating new blocks from both
4000 * system block group and metadata block group. So we only can
4001 * do operations require modifying the chunk tree after both
4002 * block groups were created.
4003 */
4004 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
4005 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06004006 if (ret) {
4007 btrfs_abort_transaction(trans, root, ret);
4008 goto out;
4009 }
Yan Zheng2b820322008-11-17 21:11:30 -05004010
4011 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
4012 sys_chunk_offset, sys_chunk_size,
4013 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004014 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004015 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004016
David Sterba005d6422012-09-18 07:52:32 -06004017out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004018
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004019 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004020}
4021
4022int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4023{
4024 struct extent_map *em;
4025 struct map_lookup *map;
4026 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4027 int readonly = 0;
4028 int i;
4029
Chris Mason890871b2009-09-02 16:24:52 -04004030 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004031 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004032 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004033 if (!em)
4034 return 1;
4035
Josef Bacikf48b9072010-01-27 02:07:59 +00004036 if (btrfs_test_opt(root, DEGRADED)) {
4037 free_extent_map(em);
4038 return 0;
4039 }
4040
Yan Zheng2b820322008-11-17 21:11:30 -05004041 map = (struct map_lookup *)em->bdev;
4042 for (i = 0; i < map->num_stripes; i++) {
4043 if (!map->stripes[i].dev->writeable) {
4044 readonly = 1;
4045 break;
4046 }
4047 }
4048 free_extent_map(em);
4049 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004050}
4051
4052void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4053{
David Sterbaa8067e02011-04-21 00:34:43 +02004054 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004055}
4056
4057void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4058{
4059 struct extent_map *em;
4060
Chris Masond3977122009-01-05 21:25:51 -05004061 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004062 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004063 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4064 if (em)
4065 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004066 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004067 if (!em)
4068 break;
4069 kfree(em->bdev);
4070 /* once for us */
4071 free_extent_map(em);
4072 /* once for the tree */
4073 free_extent_map(em);
4074 }
4075}
4076
Stefan Behrens5d964052012-11-05 14:59:07 +01004077int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004078{
Stefan Behrens5d964052012-11-05 14:59:07 +01004079 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004080 struct extent_map *em;
4081 struct map_lookup *map;
4082 struct extent_map_tree *em_tree = &map_tree->map_tree;
4083 int ret;
4084
Chris Mason890871b2009-09-02 16:24:52 -04004085 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004086 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004087 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004088 BUG_ON(!em);
4089
4090 BUG_ON(em->start > logical || em->start + em->len < logical);
4091 map = (struct map_lookup *)em->bdev;
4092 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4093 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004094 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4095 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004096 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4097 ret = 2;
4098 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4099 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004100 else
4101 ret = 1;
4102 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004103
4104 btrfs_dev_replace_lock(&fs_info->dev_replace);
4105 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4106 ret++;
4107 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4108
Chris Masonf1885912008-04-09 16:28:12 -04004109 return ret;
4110}
4111
David Woodhouse53b381b2013-01-29 18:40:14 -05004112unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4113 struct btrfs_mapping_tree *map_tree,
4114 u64 logical)
4115{
4116 struct extent_map *em;
4117 struct map_lookup *map;
4118 struct extent_map_tree *em_tree = &map_tree->map_tree;
4119 unsigned long len = root->sectorsize;
4120
4121 read_lock(&em_tree->lock);
4122 em = lookup_extent_mapping(em_tree, logical, len);
4123 read_unlock(&em_tree->lock);
4124 BUG_ON(!em);
4125
4126 BUG_ON(em->start > logical || em->start + em->len < logical);
4127 map = (struct map_lookup *)em->bdev;
4128 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4129 BTRFS_BLOCK_GROUP_RAID6)) {
4130 len = map->stripe_len * nr_data_stripes(map);
4131 }
4132 free_extent_map(em);
4133 return len;
4134}
4135
4136int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4137 u64 logical, u64 len, int mirror_num)
4138{
4139 struct extent_map *em;
4140 struct map_lookup *map;
4141 struct extent_map_tree *em_tree = &map_tree->map_tree;
4142 int ret = 0;
4143
4144 read_lock(&em_tree->lock);
4145 em = lookup_extent_mapping(em_tree, logical, len);
4146 read_unlock(&em_tree->lock);
4147 BUG_ON(!em);
4148
4149 BUG_ON(em->start > logical || em->start + em->len < logical);
4150 map = (struct map_lookup *)em->bdev;
4151 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4152 BTRFS_BLOCK_GROUP_RAID6))
4153 ret = 1;
4154 free_extent_map(em);
4155 return ret;
4156}
4157
Stefan Behrens30d98612012-11-06 14:52:18 +01004158static int find_live_mirror(struct btrfs_fs_info *fs_info,
4159 struct map_lookup *map, int first, int num,
4160 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004161{
4162 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004163 int tolerance;
4164 struct btrfs_device *srcdev;
4165
4166 if (dev_replace_is_ongoing &&
4167 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4168 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4169 srcdev = fs_info->dev_replace.srcdev;
4170 else
4171 srcdev = NULL;
4172
4173 /*
4174 * try to avoid the drive that is the source drive for a
4175 * dev-replace procedure, only choose it if no other non-missing
4176 * mirror is available
4177 */
4178 for (tolerance = 0; tolerance < 2; tolerance++) {
4179 if (map->stripes[optimal].dev->bdev &&
4180 (tolerance || map->stripes[optimal].dev != srcdev))
4181 return optimal;
4182 for (i = first; i < first + num; i++) {
4183 if (map->stripes[i].dev->bdev &&
4184 (tolerance || map->stripes[i].dev != srcdev))
4185 return i;
4186 }
Chris Masondfe25022008-05-13 13:46:40 -04004187 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004188
Chris Masondfe25022008-05-13 13:46:40 -04004189 /* we couldn't find one that doesn't fail. Just return something
4190 * and the io error handling code will clean up eventually
4191 */
4192 return optimal;
4193}
4194
David Woodhouse53b381b2013-01-29 18:40:14 -05004195static inline int parity_smaller(u64 a, u64 b)
4196{
4197 return a > b;
4198}
4199
4200/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4201static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4202{
4203 struct btrfs_bio_stripe s;
4204 int i;
4205 u64 l;
4206 int again = 1;
4207
4208 while (again) {
4209 again = 0;
4210 for (i = 0; i < bbio->num_stripes - 1; i++) {
4211 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4212 s = bbio->stripes[i];
4213 l = raid_map[i];
4214 bbio->stripes[i] = bbio->stripes[i+1];
4215 raid_map[i] = raid_map[i+1];
4216 bbio->stripes[i+1] = s;
4217 raid_map[i+1] = l;
4218 again = 1;
4219 }
4220 }
4221 }
4222}
4223
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004224static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004225 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004226 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004227 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004228{
4229 struct extent_map *em;
4230 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004231 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004232 struct extent_map_tree *em_tree = &map_tree->map_tree;
4233 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004234 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004235 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004236 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004237 u64 stripe_nr_orig;
4238 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004239 u64 stripe_len;
4240 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004241 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004242 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004243 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004244 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004245 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004246 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004247 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4248 int dev_replace_is_ongoing = 0;
4249 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004250 int patch_the_first_stripe_for_dev_replace = 0;
4251 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004252 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004253
Chris Mason890871b2009-09-02 16:24:52 -04004254 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004255 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004256 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004257
Chris Mason3b951512008-04-17 11:29:12 -04004258 if (!em) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004259 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004260 (unsigned long long)logical,
4261 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04004262 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04004263 }
Chris Mason0b86a832008-03-24 15:01:56 -04004264
4265 BUG_ON(em->start > logical || em->start + em->len < logical);
4266 map = (struct map_lookup *)em->bdev;
4267 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004268
David Woodhouse53b381b2013-01-29 18:40:14 -05004269 if (mirror_num > map->num_stripes)
4270 mirror_num = 0;
4271
4272 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004273 stripe_nr = offset;
4274 /*
4275 * stripe_nr counts the total number of stripes we have to stride
4276 * to get to this block
4277 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004278 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004279
David Woodhouse53b381b2013-01-29 18:40:14 -05004280 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004281 BUG_ON(offset < stripe_offset);
4282
4283 /* stripe_offset is the offset of this block in its stripe*/
4284 stripe_offset = offset - stripe_offset;
4285
David Woodhouse53b381b2013-01-29 18:40:14 -05004286 /* if we're here for raid56, we need to know the stripe aligned start */
4287 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4288 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4289 raid56_full_stripe_start = offset;
4290
4291 /* allow a write of a full stripe, but make sure we don't
4292 * allow straddling of stripes
4293 */
4294 do_div(raid56_full_stripe_start, full_stripe_len);
4295 raid56_full_stripe_start *= full_stripe_len;
4296 }
4297
4298 if (rw & REQ_DISCARD) {
4299 /* we don't discard raid56 yet */
4300 if (map->type &
4301 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4302 ret = -EOPNOTSUPP;
4303 goto out;
4304 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004305 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004306 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4307 u64 max_len;
4308 /* For writes to RAID[56], allow a full stripeset across all disks.
4309 For other RAID types and for RAID[56] reads, just allow a single
4310 stripe (on a single disk). */
4311 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4312 (rw & REQ_WRITE)) {
4313 max_len = stripe_len * nr_data_stripes(map) -
4314 (offset - raid56_full_stripe_start);
4315 } else {
4316 /* we limit the length of each bio to what fits in a stripe */
4317 max_len = stripe_len - stripe_offset;
4318 }
4319 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004320 } else {
4321 *length = em->len - offset;
4322 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004323
David Woodhouse53b381b2013-01-29 18:40:14 -05004324 /* This is for when we're called from btrfs_merge_bio_hook() and all
4325 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004326 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004327 goto out;
4328
Stefan Behrens472262f2012-11-06 14:43:46 +01004329 btrfs_dev_replace_lock(dev_replace);
4330 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4331 if (!dev_replace_is_ongoing)
4332 btrfs_dev_replace_unlock(dev_replace);
4333
Stefan Behrensad6d6202012-11-06 15:06:47 +01004334 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4335 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4336 dev_replace->tgtdev != NULL) {
4337 /*
4338 * in dev-replace case, for repair case (that's the only
4339 * case where the mirror is selected explicitly when
4340 * calling btrfs_map_block), blocks left of the left cursor
4341 * can also be read from the target drive.
4342 * For REQ_GET_READ_MIRRORS, the target drive is added as
4343 * the last one to the array of stripes. For READ, it also
4344 * needs to be supported using the same mirror number.
4345 * If the requested block is not left of the left cursor,
4346 * EIO is returned. This can happen because btrfs_num_copies()
4347 * returns one more in the dev-replace case.
4348 */
4349 u64 tmp_length = *length;
4350 struct btrfs_bio *tmp_bbio = NULL;
4351 int tmp_num_stripes;
4352 u64 srcdev_devid = dev_replace->srcdev->devid;
4353 int index_srcdev = 0;
4354 int found = 0;
4355 u64 physical_of_found = 0;
4356
4357 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004358 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004359 if (ret) {
4360 WARN_ON(tmp_bbio != NULL);
4361 goto out;
4362 }
4363
4364 tmp_num_stripes = tmp_bbio->num_stripes;
4365 if (mirror_num > tmp_num_stripes) {
4366 /*
4367 * REQ_GET_READ_MIRRORS does not contain this
4368 * mirror, that means that the requested area
4369 * is not left of the left cursor
4370 */
4371 ret = -EIO;
4372 kfree(tmp_bbio);
4373 goto out;
4374 }
4375
4376 /*
4377 * process the rest of the function using the mirror_num
4378 * of the source drive. Therefore look it up first.
4379 * At the end, patch the device pointer to the one of the
4380 * target drive.
4381 */
4382 for (i = 0; i < tmp_num_stripes; i++) {
4383 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4384 /*
4385 * In case of DUP, in order to keep it
4386 * simple, only add the mirror with the
4387 * lowest physical address
4388 */
4389 if (found &&
4390 physical_of_found <=
4391 tmp_bbio->stripes[i].physical)
4392 continue;
4393 index_srcdev = i;
4394 found = 1;
4395 physical_of_found =
4396 tmp_bbio->stripes[i].physical;
4397 }
4398 }
4399
4400 if (found) {
4401 mirror_num = index_srcdev + 1;
4402 patch_the_first_stripe_for_dev_replace = 1;
4403 physical_to_patch_in_first_stripe = physical_of_found;
4404 } else {
4405 WARN_ON(1);
4406 ret = -EIO;
4407 kfree(tmp_bbio);
4408 goto out;
4409 }
4410
4411 kfree(tmp_bbio);
4412 } else if (mirror_num > map->num_stripes) {
4413 mirror_num = 0;
4414 }
4415
Chris Masonf2d8d742008-04-21 10:03:05 -04004416 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004417 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004418 stripe_nr_orig = stripe_nr;
4419 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
4420 (~(map->stripe_len - 1));
4421 do_div(stripe_nr_end, map->stripe_len);
4422 stripe_end_offset = stripe_nr_end * map->stripe_len -
4423 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004424
Li Dongyangfce3bb92011-03-24 10:24:26 +00004425 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4426 if (rw & REQ_DISCARD)
4427 num_stripes = min_t(u64, map->num_stripes,
4428 stripe_nr_end - stripe_nr_orig);
4429 stripe_index = do_div(stripe_nr, map->num_stripes);
4430 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004431 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004432 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004433 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004434 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004435 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004436 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004437 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004438 current->pid % map->num_stripes,
4439 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004440 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004441 }
Chris Mason2fff7342008-04-29 14:12:09 -04004442
Chris Mason611f0e02008-04-03 16:29:03 -04004443 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004444 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004445 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004446 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004447 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004448 } else {
4449 mirror_num = 1;
4450 }
Chris Mason2fff7342008-04-29 14:12:09 -04004451
Chris Mason321aecc2008-04-16 10:49:51 -04004452 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4453 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004454
4455 stripe_index = do_div(stripe_nr, factor);
4456 stripe_index *= map->sub_stripes;
4457
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004458 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004459 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004460 else if (rw & REQ_DISCARD)
4461 num_stripes = min_t(u64, map->sub_stripes *
4462 (stripe_nr_end - stripe_nr_orig),
4463 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004464 else if (mirror_num)
4465 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004466 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004467 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004468 stripe_index = find_live_mirror(fs_info, map,
4469 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004470 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004471 current->pid % map->sub_stripes,
4472 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004473 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004474 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004475
4476 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4477 BTRFS_BLOCK_GROUP_RAID6)) {
4478 u64 tmp;
4479
4480 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4481 && raid_map_ret) {
4482 int i, rot;
4483
4484 /* push stripe_nr back to the start of the full stripe */
4485 stripe_nr = raid56_full_stripe_start;
4486 do_div(stripe_nr, stripe_len);
4487
4488 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4489
4490 /* RAID[56] write or recovery. Return all stripes */
4491 num_stripes = map->num_stripes;
4492 max_errors = nr_parity_stripes(map);
4493
4494 raid_map = kmalloc(sizeof(u64) * num_stripes,
4495 GFP_NOFS);
4496 if (!raid_map) {
4497 ret = -ENOMEM;
4498 goto out;
4499 }
4500
4501 /* Work out the disk rotation on this stripe-set */
4502 tmp = stripe_nr;
4503 rot = do_div(tmp, num_stripes);
4504
4505 /* Fill in the logical address of each stripe */
4506 tmp = stripe_nr * nr_data_stripes(map);
4507 for (i = 0; i < nr_data_stripes(map); i++)
4508 raid_map[(i+rot) % num_stripes] =
4509 em->start + (tmp + i) * map->stripe_len;
4510
4511 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4512 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4513 raid_map[(i+rot+1) % num_stripes] =
4514 RAID6_Q_STRIPE;
4515
4516 *length = map->stripe_len;
4517 stripe_index = 0;
4518 stripe_offset = 0;
4519 } else {
4520 /*
4521 * Mirror #0 or #1 means the original data block.
4522 * Mirror #2 is RAID5 parity block.
4523 * Mirror #3 is RAID6 Q block.
4524 */
4525 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4526 if (mirror_num > 1)
4527 stripe_index = nr_data_stripes(map) +
4528 mirror_num - 2;
4529
4530 /* We distribute the parity blocks across stripes */
4531 tmp = stripe_nr + stripe_index;
4532 stripe_index = do_div(tmp, map->num_stripes);
4533 }
Chris Mason8790d502008-04-03 16:29:03 -04004534 } else {
4535 /*
4536 * after this do_div call, stripe_nr is the number of stripes
4537 * on this device we have to walk to find the data, and
4538 * stripe_index is the number of our device in the stripe array
4539 */
4540 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004541 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004542 }
Chris Mason593060d2008-03-25 16:50:33 -04004543 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004544
Stefan Behrens472262f2012-11-06 14:43:46 +01004545 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004546 if (dev_replace_is_ongoing) {
4547 if (rw & (REQ_WRITE | REQ_DISCARD))
4548 num_alloc_stripes <<= 1;
4549 if (rw & REQ_GET_READ_MIRRORS)
4550 num_alloc_stripes++;
4551 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004552 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08004553 if (!bbio) {
4554 ret = -ENOMEM;
4555 goto out;
4556 }
4557 atomic_set(&bbio->error, 0);
4558
Li Dongyangfce3bb92011-03-24 10:24:26 +00004559 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004560 int factor = 0;
4561 int sub_stripes = 0;
4562 u64 stripes_per_dev = 0;
4563 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004564 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004565
4566 if (map->type &
4567 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4568 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4569 sub_stripes = 1;
4570 else
4571 sub_stripes = map->sub_stripes;
4572
4573 factor = map->num_stripes / sub_stripes;
4574 stripes_per_dev = div_u64_rem(stripe_nr_end -
4575 stripe_nr_orig,
4576 factor,
4577 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004578 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4579 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004580 }
4581
Li Dongyangfce3bb92011-03-24 10:24:26 +00004582 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004583 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004584 map->stripes[stripe_index].physical +
4585 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004586 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004587
Li Zefanec9ef7a2011-12-01 14:06:42 +08004588 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4589 BTRFS_BLOCK_GROUP_RAID10)) {
4590 bbio->stripes[i].length = stripes_per_dev *
4591 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004592
Li Zefanec9ef7a2011-12-01 14:06:42 +08004593 if (i / sub_stripes < remaining_stripes)
4594 bbio->stripes[i].length +=
4595 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004596
4597 /*
4598 * Special for the first stripe and
4599 * the last stripe:
4600 *
4601 * |-------|...|-------|
4602 * |----------|
4603 * off end_off
4604 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004605 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004606 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004607 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004608
4609 if (stripe_index >= last_stripe &&
4610 stripe_index <= (last_stripe +
4611 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004612 bbio->stripes[i].length -=
4613 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004614
Li Zefanec9ef7a2011-12-01 14:06:42 +08004615 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004616 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004617 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004618 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004619
4620 stripe_index++;
4621 if (stripe_index == map->num_stripes) {
4622 /* This could only happen for RAID0/10 */
4623 stripe_index = 0;
4624 stripe_nr++;
4625 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004626 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004627 } else {
4628 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004629 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004630 map->stripes[stripe_index].physical +
4631 stripe_offset +
4632 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004633 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004634 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004635 stripe_index++;
4636 }
Chris Mason593060d2008-03-25 16:50:33 -04004637 }
Li Zefande11cc12011-12-01 12:55:47 +08004638
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004639 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08004640 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4641 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05004642 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08004643 BTRFS_BLOCK_GROUP_DUP)) {
4644 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05004645 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4646 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08004647 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004648 }
Li Zefande11cc12011-12-01 12:55:47 +08004649
Stefan Behrens472262f2012-11-06 14:43:46 +01004650 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4651 dev_replace->tgtdev != NULL) {
4652 int index_where_to_add;
4653 u64 srcdev_devid = dev_replace->srcdev->devid;
4654
4655 /*
4656 * duplicate the write operations while the dev replace
4657 * procedure is running. Since the copying of the old disk
4658 * to the new disk takes place at run time while the
4659 * filesystem is mounted writable, the regular write
4660 * operations to the old disk have to be duplicated to go
4661 * to the new disk as well.
4662 * Note that device->missing is handled by the caller, and
4663 * that the write to the old disk is already set up in the
4664 * stripes array.
4665 */
4666 index_where_to_add = num_stripes;
4667 for (i = 0; i < num_stripes; i++) {
4668 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4669 /* write to new disk, too */
4670 struct btrfs_bio_stripe *new =
4671 bbio->stripes + index_where_to_add;
4672 struct btrfs_bio_stripe *old =
4673 bbio->stripes + i;
4674
4675 new->physical = old->physical;
4676 new->length = old->length;
4677 new->dev = dev_replace->tgtdev;
4678 index_where_to_add++;
4679 max_errors++;
4680 }
4681 }
4682 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004683 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4684 dev_replace->tgtdev != NULL) {
4685 u64 srcdev_devid = dev_replace->srcdev->devid;
4686 int index_srcdev = 0;
4687 int found = 0;
4688 u64 physical_of_found = 0;
4689
4690 /*
4691 * During the dev-replace procedure, the target drive can
4692 * also be used to read data in case it is needed to repair
4693 * a corrupt block elsewhere. This is possible if the
4694 * requested area is left of the left cursor. In this area,
4695 * the target drive is a full copy of the source drive.
4696 */
4697 for (i = 0; i < num_stripes; i++) {
4698 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4699 /*
4700 * In case of DUP, in order to keep it
4701 * simple, only add the mirror with the
4702 * lowest physical address
4703 */
4704 if (found &&
4705 physical_of_found <=
4706 bbio->stripes[i].physical)
4707 continue;
4708 index_srcdev = i;
4709 found = 1;
4710 physical_of_found = bbio->stripes[i].physical;
4711 }
4712 }
4713 if (found) {
4714 u64 length = map->stripe_len;
4715
4716 if (physical_of_found + length <=
4717 dev_replace->cursor_left) {
4718 struct btrfs_bio_stripe *tgtdev_stripe =
4719 bbio->stripes + num_stripes;
4720
4721 tgtdev_stripe->physical = physical_of_found;
4722 tgtdev_stripe->length =
4723 bbio->stripes[index_srcdev].length;
4724 tgtdev_stripe->dev = dev_replace->tgtdev;
4725
4726 num_stripes++;
4727 }
4728 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004729 }
4730
Li Zefande11cc12011-12-01 12:55:47 +08004731 *bbio_ret = bbio;
4732 bbio->num_stripes = num_stripes;
4733 bbio->max_errors = max_errors;
4734 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004735
4736 /*
4737 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4738 * mirror_num == num_stripes + 1 && dev_replace target drive is
4739 * available as a mirror
4740 */
4741 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
4742 WARN_ON(num_stripes > 1);
4743 bbio->stripes[0].dev = dev_replace->tgtdev;
4744 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
4745 bbio->mirror_num = map->num_stripes + 1;
4746 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004747 if (raid_map) {
4748 sort_parity_stripes(bbio, raid_map);
4749 *raid_map_ret = raid_map;
4750 }
Chris Masoncea9e442008-04-09 16:28:12 -04004751out:
Stefan Behrens472262f2012-11-06 14:43:46 +01004752 if (dev_replace_is_ongoing)
4753 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04004754 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004755 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004756}
4757
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004758int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004759 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004760 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004761{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004762 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004763 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04004764}
4765
Yan Zhenga512bbf2008-12-08 16:46:26 -05004766int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4767 u64 chunk_start, u64 physical, u64 devid,
4768 u64 **logical, int *naddrs, int *stripe_len)
4769{
4770 struct extent_map_tree *em_tree = &map_tree->map_tree;
4771 struct extent_map *em;
4772 struct map_lookup *map;
4773 u64 *buf;
4774 u64 bytenr;
4775 u64 length;
4776 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05004777 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05004778 int i, j, nr = 0;
4779
Chris Mason890871b2009-09-02 16:24:52 -04004780 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004781 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004782 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004783
4784 BUG_ON(!em || em->start != chunk_start);
4785 map = (struct map_lookup *)em->bdev;
4786
4787 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05004788 rmap_len = map->stripe_len;
4789
Yan Zhenga512bbf2008-12-08 16:46:26 -05004790 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4791 do_div(length, map->num_stripes / map->sub_stripes);
4792 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4793 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05004794 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4795 BTRFS_BLOCK_GROUP_RAID6)) {
4796 do_div(length, nr_data_stripes(map));
4797 rmap_len = map->stripe_len * nr_data_stripes(map);
4798 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004799
4800 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004801 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004802
4803 for (i = 0; i < map->num_stripes; i++) {
4804 if (devid && map->stripes[i].dev->devid != devid)
4805 continue;
4806 if (map->stripes[i].physical > physical ||
4807 map->stripes[i].physical + length <= physical)
4808 continue;
4809
4810 stripe_nr = physical - map->stripes[i].physical;
4811 do_div(stripe_nr, map->stripe_len);
4812
4813 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4814 stripe_nr = stripe_nr * map->num_stripes + i;
4815 do_div(stripe_nr, map->sub_stripes);
4816 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4817 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05004818 } /* else if RAID[56], multiply by nr_data_stripes().
4819 * Alternatively, just use rmap_len below instead of
4820 * map->stripe_len */
4821
4822 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05004823 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004824 for (j = 0; j < nr; j++) {
4825 if (buf[j] == bytenr)
4826 break;
4827 }
Chris Mason934d3752008-12-08 16:43:10 -05004828 if (j == nr) {
4829 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004830 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004831 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004832 }
4833
Yan Zhenga512bbf2008-12-08 16:46:26 -05004834 *logical = buf;
4835 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05004836 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05004837
4838 free_extent_map(em);
4839 return 0;
4840}
4841
Stefan Behrens442a4f62012-05-25 16:06:08 +02004842static void *merge_stripe_index_into_bio_private(void *bi_private,
4843 unsigned int stripe_index)
4844{
4845 /*
4846 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4847 * at most 1.
4848 * The alternative solution (instead of stealing bits from the
4849 * pointer) would be to allocate an intermediate structure
4850 * that contains the old private pointer plus the stripe_index.
4851 */
4852 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4853 BUG_ON(stripe_index > 3);
4854 return (void *)(((uintptr_t)bi_private) | stripe_index);
4855}
4856
4857static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4858{
4859 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4860}
4861
4862static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4863{
4864 return (unsigned int)((uintptr_t)bi_private) & 3;
4865}
4866
Jan Schmidta1d3c472011-08-04 17:15:33 +02004867static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004868{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004869 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004870 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004871
Stefan Behrens442a4f62012-05-25 16:06:08 +02004872 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004873 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004874 if (err == -EIO || err == -EREMOTEIO) {
4875 unsigned int stripe_index =
4876 extract_stripe_index_from_bio_private(
4877 bio->bi_private);
4878 struct btrfs_device *dev;
4879
4880 BUG_ON(stripe_index >= bbio->num_stripes);
4881 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004882 if (dev->bdev) {
4883 if (bio->bi_rw & WRITE)
4884 btrfs_dev_stat_inc(dev,
4885 BTRFS_DEV_STAT_WRITE_ERRS);
4886 else
4887 btrfs_dev_stat_inc(dev,
4888 BTRFS_DEV_STAT_READ_ERRS);
4889 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4890 btrfs_dev_stat_inc(dev,
4891 BTRFS_DEV_STAT_FLUSH_ERRS);
4892 btrfs_dev_stat_print_on_error(dev);
4893 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004894 }
4895 }
Chris Mason8790d502008-04-03 16:29:03 -04004896
Jan Schmidta1d3c472011-08-04 17:15:33 +02004897 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004898 is_orig_bio = 1;
4899
Jan Schmidta1d3c472011-08-04 17:15:33 +02004900 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004901 if (!is_orig_bio) {
4902 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004903 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004904 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004905 bio->bi_private = bbio->private;
4906 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004907 bio->bi_bdev = (struct block_device *)
4908 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004909 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05004910 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04004911 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004912 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004913 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004914 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004915 /*
4916 * this bio is actually up to date, we didn't
4917 * go over the max number of errors
4918 */
4919 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004920 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004921 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004922 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004923
4924 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004925 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004926 bio_put(bio);
4927 }
Chris Mason8790d502008-04-03 16:29:03 -04004928}
4929
Chris Mason8b712842008-06-11 16:50:36 -04004930struct async_sched {
4931 struct bio *bio;
4932 int rw;
4933 struct btrfs_fs_info *info;
4934 struct btrfs_work work;
4935};
4936
4937/*
4938 * see run_scheduled_bios for a description of why bios are collected for
4939 * async submit.
4940 *
4941 * This will add one bio to the pending list for a device and make sure
4942 * the work struct is scheduled.
4943 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004944noinline void btrfs_schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004945 struct btrfs_device *device,
4946 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004947{
4948 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004949 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004950
David Woodhouse53b381b2013-01-29 18:40:14 -05004951 if (device->missing || !device->bdev) {
4952 bio_endio(bio, -EIO);
4953 return;
4954 }
4955
Chris Mason8b712842008-06-11 16:50:36 -04004956 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004957 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004958 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004959 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004960 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004961 return;
Chris Mason8b712842008-06-11 16:50:36 -04004962 }
4963
4964 /*
Chris Mason0986fe92008-08-15 15:34:15 -04004965 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004966 * higher layers. Otherwise, the async bio makes it appear we have
4967 * made progress against dirty pages when we've really just put it
4968 * on a queue for later
4969 */
Chris Mason0986fe92008-08-15 15:34:15 -04004970 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004971 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004972 bio->bi_next = NULL;
4973 bio->bi_rw |= rw;
4974
4975 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004976 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004977 pending_bios = &device->pending_sync_bios;
4978 else
4979 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004980
Chris Masonffbd5172009-04-20 15:50:09 -04004981 if (pending_bios->tail)
4982 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004983
Chris Masonffbd5172009-04-20 15:50:09 -04004984 pending_bios->tail = bio;
4985 if (!pending_bios->head)
4986 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004987 if (device->running_pending)
4988 should_queue = 0;
4989
4990 spin_unlock(&device->io_lock);
4991
4992 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004993 btrfs_queue_worker(&root->fs_info->submit_workers,
4994 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004995}
4996
Josef Bacikde1ee922012-10-19 16:50:56 -04004997static int bio_size_ok(struct block_device *bdev, struct bio *bio,
4998 sector_t sector)
4999{
5000 struct bio_vec *prev;
5001 struct request_queue *q = bdev_get_queue(bdev);
5002 unsigned short max_sectors = queue_max_sectors(q);
5003 struct bvec_merge_data bvm = {
5004 .bi_bdev = bdev,
5005 .bi_sector = sector,
5006 .bi_rw = bio->bi_rw,
5007 };
5008
5009 if (bio->bi_vcnt == 0) {
5010 WARN_ON(1);
5011 return 1;
5012 }
5013
5014 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
5015 if ((bio->bi_size >> 9) > max_sectors)
5016 return 0;
5017
5018 if (!q->merge_bvec_fn)
5019 return 1;
5020
5021 bvm.bi_size = bio->bi_size - prev->bv_len;
5022 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5023 return 0;
5024 return 1;
5025}
5026
5027static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5028 struct bio *bio, u64 physical, int dev_nr,
5029 int rw, int async)
5030{
5031 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5032
5033 bio->bi_private = bbio;
5034 bio->bi_private = merge_stripe_index_into_bio_private(
5035 bio->bi_private, (unsigned int)dev_nr);
5036 bio->bi_end_io = btrfs_end_bio;
5037 bio->bi_sector = physical >> 9;
5038#ifdef DEBUG
5039 {
5040 struct rcu_string *name;
5041
5042 rcu_read_lock();
5043 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005044 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005045 "(%s id %llu), size=%u\n", rw,
5046 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5047 name->str, dev->devid, bio->bi_size);
5048 rcu_read_unlock();
5049 }
5050#endif
5051 bio->bi_bdev = dev->bdev;
5052 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005053 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005054 else
5055 btrfsic_submit_bio(rw, bio);
5056}
5057
5058static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5059 struct bio *first_bio, struct btrfs_device *dev,
5060 int dev_nr, int rw, int async)
5061{
5062 struct bio_vec *bvec = first_bio->bi_io_vec;
5063 struct bio *bio;
5064 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5065 u64 physical = bbio->stripes[dev_nr].physical;
5066
5067again:
5068 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5069 if (!bio)
5070 return -ENOMEM;
5071
5072 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5073 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5074 bvec->bv_offset) < bvec->bv_len) {
5075 u64 len = bio->bi_size;
5076
5077 atomic_inc(&bbio->stripes_pending);
5078 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5079 rw, async);
5080 physical += len;
5081 goto again;
5082 }
5083 bvec++;
5084 }
5085
5086 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5087 return 0;
5088}
5089
5090static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5091{
5092 atomic_inc(&bbio->error);
5093 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5094 bio->bi_private = bbio->private;
5095 bio->bi_end_io = bbio->end_io;
5096 bio->bi_bdev = (struct block_device *)
5097 (unsigned long)bbio->mirror_num;
5098 bio->bi_sector = logical >> 9;
5099 kfree(bbio);
5100 bio_endio(bio, -EIO);
5101 }
5102}
5103
Chris Masonf1885912008-04-09 16:28:12 -04005104int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005105 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005106{
Chris Mason0b86a832008-03-24 15:01:56 -04005107 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005108 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04005109 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005110 u64 length = 0;
5111 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005112 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005113 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005114 int dev_nr = 0;
5115 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005116 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005117
Chris Masonf2d8d742008-04-21 10:03:05 -04005118 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005119 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005120
David Woodhouse53b381b2013-01-29 18:40:14 -05005121 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5122 mirror_num, &raid_map);
5123 if (ret) /* -ENOMEM */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005124 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04005125
Jan Schmidta1d3c472011-08-04 17:15:33 +02005126 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005127 bbio->orig_bio = first_bio;
5128 bbio->private = first_bio->bi_private;
5129 bbio->end_io = first_bio->bi_end_io;
5130 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5131
5132 if (raid_map) {
5133 /* In this case, map_length has been set to the length of
5134 a single stripe; not the whole write */
5135 if (rw & WRITE) {
5136 return raid56_parity_write(root, bio, bbio,
5137 raid_map, map_length);
5138 } else {
5139 return raid56_parity_recover(root, bio, bbio,
5140 raid_map, map_length,
5141 mirror_num);
5142 }
5143 }
5144
Chris Masoncea9e442008-04-09 16:28:12 -04005145 if (map_length < length) {
Daniel J Blueman48940662012-05-07 06:35:38 -06005146 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
Chris Masond3977122009-01-05 21:25:51 -05005147 "len %llu\n", (unsigned long long)logical,
5148 (unsigned long long)length,
5149 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005150 BUG();
5151 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005152
Chris Masond3977122009-01-05 21:25:51 -05005153 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005154 dev = bbio->stripes[dev_nr].dev;
5155 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5156 bbio_error(bbio, first_bio, logical);
5157 dev_nr++;
5158 continue;
5159 }
5160
5161 /*
5162 * Check and see if we're ok with this bio based on it's size
5163 * and offset with the given device.
5164 */
5165 if (!bio_size_ok(dev->bdev, first_bio,
5166 bbio->stripes[dev_nr].physical >> 9)) {
5167 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5168 dev_nr, rw, async_submit);
5169 BUG_ON(ret);
5170 dev_nr++;
5171 continue;
5172 }
5173
Jan Schmidta1d3c472011-08-04 17:15:33 +02005174 if (dev_nr < total_devs - 1) {
5175 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005176 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005177 } else {
5178 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04005179 }
Josef Bacik606686e2012-06-04 14:03:51 -04005180
Josef Bacikde1ee922012-10-19 16:50:56 -04005181 submit_stripe_bio(root, bbio, bio,
5182 bbio->stripes[dev_nr].physical, dev_nr, rw,
5183 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005184 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005185 }
Chris Mason0b86a832008-03-24 15:01:56 -04005186 return 0;
5187}
5188
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005189struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005190 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005191{
Yan Zheng2b820322008-11-17 21:11:30 -05005192 struct btrfs_device *device;
5193 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005194
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005195 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005196 while (cur_devices) {
5197 if (!fsid ||
5198 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5199 device = __find_device(&cur_devices->devices,
5200 devid, uuid);
5201 if (device)
5202 return device;
5203 }
5204 cur_devices = cur_devices->seed;
5205 }
5206 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005207}
5208
Chris Masondfe25022008-05-13 13:46:40 -04005209static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5210 u64 devid, u8 *dev_uuid)
5211{
5212 struct btrfs_device *device;
5213 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5214
5215 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005216 if (!device)
5217 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04005218 list_add(&device->dev_list,
5219 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04005220 device->dev_root = root->fs_info->dev_root;
5221 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04005222 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05005223 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05005224 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04005225 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05005226 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04005227 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05005228 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04005229 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
5230 return device;
5231}
5232
Chris Mason0b86a832008-03-24 15:01:56 -04005233static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5234 struct extent_buffer *leaf,
5235 struct btrfs_chunk *chunk)
5236{
5237 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5238 struct map_lookup *map;
5239 struct extent_map *em;
5240 u64 logical;
5241 u64 length;
5242 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005243 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005244 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005245 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005246 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005247
Chris Masone17cade2008-04-15 15:41:47 -04005248 logical = key->offset;
5249 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005250
Chris Mason890871b2009-09-02 16:24:52 -04005251 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005252 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005253 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005254
5255 /* already mapped? */
5256 if (em && em->start <= logical && em->start + em->len > logical) {
5257 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005258 return 0;
5259 } else if (em) {
5260 free_extent_map(em);
5261 }
Chris Mason0b86a832008-03-24 15:01:56 -04005262
David Sterba172ddd62011-04-21 00:48:27 +02005263 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005264 if (!em)
5265 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005266 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5267 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005268 if (!map) {
5269 free_extent_map(em);
5270 return -ENOMEM;
5271 }
5272
5273 em->bdev = (struct block_device *)map;
5274 em->start = logical;
5275 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005276 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005277 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005278 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005279
Chris Mason593060d2008-03-25 16:50:33 -04005280 map->num_stripes = num_stripes;
5281 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5282 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5283 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5284 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5285 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005286 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005287 for (i = 0; i < num_stripes; i++) {
5288 map->stripes[i].physical =
5289 btrfs_stripe_offset_nr(leaf, chunk, i);
5290 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005291 read_extent_buffer(leaf, uuid, (unsigned long)
5292 btrfs_stripe_dev_uuid_nr(chunk, i),
5293 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005294 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5295 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005296 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005297 kfree(map);
5298 free_extent_map(em);
5299 return -EIO;
5300 }
Chris Masondfe25022008-05-13 13:46:40 -04005301 if (!map->stripes[i].dev) {
5302 map->stripes[i].dev =
5303 add_missing_dev(root, devid, uuid);
5304 if (!map->stripes[i].dev) {
5305 kfree(map);
5306 free_extent_map(em);
5307 return -EIO;
5308 }
5309 }
5310 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005311 }
5312
Chris Mason890871b2009-09-02 16:24:52 -04005313 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005314 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005315 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005316 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005317 free_extent_map(em);
5318
5319 return 0;
5320}
5321
Jeff Mahoney143bede2012-03-01 14:56:26 +01005322static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005323 struct btrfs_dev_item *dev_item,
5324 struct btrfs_device *device)
5325{
5326 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005327
5328 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005329 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5330 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005331 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5332 device->type = btrfs_device_type(leaf, dev_item);
5333 device->io_align = btrfs_device_io_align(leaf, dev_item);
5334 device->io_width = btrfs_device_io_width(leaf, dev_item);
5335 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005336 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005337 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005338
5339 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005340 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005341}
5342
Yan Zheng2b820322008-11-17 21:11:30 -05005343static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5344{
5345 struct btrfs_fs_devices *fs_devices;
5346 int ret;
5347
Li Zefanb367e472011-12-07 11:38:24 +08005348 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005349
5350 fs_devices = root->fs_info->fs_devices->seed;
5351 while (fs_devices) {
5352 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5353 ret = 0;
5354 goto out;
5355 }
5356 fs_devices = fs_devices->seed;
5357 }
5358
5359 fs_devices = find_fsid(fsid);
5360 if (!fs_devices) {
5361 ret = -ENOENT;
5362 goto out;
5363 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005364
5365 fs_devices = clone_fs_devices(fs_devices);
5366 if (IS_ERR(fs_devices)) {
5367 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005368 goto out;
5369 }
5370
Christoph Hellwig97288f22008-12-02 06:36:09 -05005371 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005372 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005373 if (ret) {
5374 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005375 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005376 }
Yan Zheng2b820322008-11-17 21:11:30 -05005377
5378 if (!fs_devices->seeding) {
5379 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005380 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005381 ret = -EINVAL;
5382 goto out;
5383 }
5384
5385 fs_devices->seed = root->fs_info->fs_devices->seed;
5386 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005387out:
Yan Zheng2b820322008-11-17 21:11:30 -05005388 return ret;
5389}
5390
Chris Mason0d81ba52008-03-24 15:02:07 -04005391static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005392 struct extent_buffer *leaf,
5393 struct btrfs_dev_item *dev_item)
5394{
5395 struct btrfs_device *device;
5396 u64 devid;
5397 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005398 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005399 u8 dev_uuid[BTRFS_UUID_SIZE];
5400
Chris Mason0b86a832008-03-24 15:01:56 -04005401 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04005402 read_extent_buffer(leaf, dev_uuid,
5403 (unsigned long)btrfs_device_uuid(dev_item),
5404 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05005405 read_extent_buffer(leaf, fs_uuid,
5406 (unsigned long)btrfs_device_fsid(dev_item),
5407 BTRFS_UUID_SIZE);
5408
5409 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5410 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005411 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005412 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005413 }
5414
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005415 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005416 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005417 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005418 return -EIO;
5419
5420 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05005421 printk(KERN_WARNING "warning devid %llu missing\n",
5422 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005423 device = add_missing_dev(root, devid, dev_uuid);
5424 if (!device)
5425 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005426 } else if (!device->missing) {
5427 /*
5428 * this happens when a device that was properly setup
5429 * in the device info lists suddenly goes bad.
5430 * device->bdev is NULL, and so we have to set
5431 * device->missing to one here
5432 */
5433 root->fs_info->fs_devices->missing_devices++;
5434 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05005435 }
5436 }
5437
5438 if (device->fs_devices != root->fs_info->fs_devices) {
5439 BUG_ON(device->writeable);
5440 if (device->generation !=
5441 btrfs_device_generation(leaf, dev_item))
5442 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04005443 }
Chris Mason0b86a832008-03-24 15:01:56 -04005444
5445 fill_device_from_item(leaf, dev_item, device);
5446 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04005447 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01005448 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05005449 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04005450 spin_lock(&root->fs_info->free_chunk_lock);
5451 root->fs_info->free_chunk_space += device->total_bytes -
5452 device->bytes_used;
5453 spin_unlock(&root->fs_info->free_chunk_lock);
5454 }
Chris Mason0b86a832008-03-24 15:01:56 -04005455 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005456 return ret;
5457}
5458
Yan Zhenge4404d62008-12-12 10:03:26 -05005459int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04005460{
David Sterba6c417612011-04-13 15:41:04 +02005461 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04005462 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04005463 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04005464 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04005465 u8 *ptr;
5466 unsigned long sb_ptr;
5467 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005468 u32 num_stripes;
5469 u32 array_size;
5470 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005471 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04005472 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04005473
Yan Zhenge4404d62008-12-12 10:03:26 -05005474 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04005475 BTRFS_SUPER_INFO_SIZE);
5476 if (!sb)
5477 return -ENOMEM;
5478 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04005479 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02005480 /*
5481 * The sb extent buffer is artifical and just used to read the system array.
5482 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5483 * pages up-to-date when the page is larger: extent does not cover the
5484 * whole page and consequently check_page_uptodate does not find all
5485 * the page's extents up-to-date (the hole beyond sb),
5486 * write_extent_buffer then triggers a WARN_ON.
5487 *
5488 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5489 * but sb spans only this function. Add an explicit SetPageUptodate call
5490 * to silence the warning eg. on PowerPC 64.
5491 */
5492 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04005493 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05005494
Chris Masona061fc82008-05-07 11:43:44 -04005495 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005496 array_size = btrfs_super_sys_array_size(super_copy);
5497
Chris Mason0b86a832008-03-24 15:01:56 -04005498 ptr = super_copy->sys_chunk_array;
5499 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5500 cur = 0;
5501
5502 while (cur < array_size) {
5503 disk_key = (struct btrfs_disk_key *)ptr;
5504 btrfs_disk_key_to_cpu(&key, disk_key);
5505
Chris Masona061fc82008-05-07 11:43:44 -04005506 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04005507 sb_ptr += len;
5508 cur += len;
5509
Chris Mason0d81ba52008-03-24 15:02:07 -04005510 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04005511 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04005512 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04005513 if (ret)
5514 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005515 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5516 len = btrfs_chunk_item_size(num_stripes);
5517 } else {
Chris Mason84eed902008-04-25 09:04:37 -04005518 ret = -EIO;
5519 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005520 }
5521 ptr += len;
5522 sb_ptr += len;
5523 cur += len;
5524 }
Chris Masona061fc82008-05-07 11:43:44 -04005525 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04005526 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005527}
5528
5529int btrfs_read_chunk_tree(struct btrfs_root *root)
5530{
5531 struct btrfs_path *path;
5532 struct extent_buffer *leaf;
5533 struct btrfs_key key;
5534 struct btrfs_key found_key;
5535 int ret;
5536 int slot;
5537
5538 root = root->fs_info->chunk_root;
5539
5540 path = btrfs_alloc_path();
5541 if (!path)
5542 return -ENOMEM;
5543
Li Zefanb367e472011-12-07 11:38:24 +08005544 mutex_lock(&uuid_mutex);
5545 lock_chunks(root);
5546
Chris Mason0b86a832008-03-24 15:01:56 -04005547 /* first we search for all of the device items, and then we
5548 * read in all of the chunk items. This way we can create chunk
5549 * mappings that reference all of the devices that are afound
5550 */
5551 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5552 key.offset = 0;
5553 key.type = 0;
5554again:
5555 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005556 if (ret < 0)
5557 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005558 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005559 leaf = path->nodes[0];
5560 slot = path->slots[0];
5561 if (slot >= btrfs_header_nritems(leaf)) {
5562 ret = btrfs_next_leaf(root, path);
5563 if (ret == 0)
5564 continue;
5565 if (ret < 0)
5566 goto error;
5567 break;
5568 }
5569 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5570 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5571 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5572 break;
5573 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5574 struct btrfs_dev_item *dev_item;
5575 dev_item = btrfs_item_ptr(leaf, slot,
5576 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04005577 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05005578 if (ret)
5579 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005580 }
5581 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5582 struct btrfs_chunk *chunk;
5583 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5584 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005585 if (ret)
5586 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005587 }
5588 path->slots[0]++;
5589 }
5590 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5591 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005592 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005593 goto again;
5594 }
Chris Mason0b86a832008-03-24 15:01:56 -04005595 ret = 0;
5596error:
Li Zefanb367e472011-12-07 11:38:24 +08005597 unlock_chunks(root);
5598 mutex_unlock(&uuid_mutex);
5599
Yan Zheng2b820322008-11-17 21:11:30 -05005600 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005601 return ret;
5602}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005603
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005604static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5605{
5606 int i;
5607
5608 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5609 btrfs_dev_stat_reset(dev, i);
5610}
5611
5612int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5613{
5614 struct btrfs_key key;
5615 struct btrfs_key found_key;
5616 struct btrfs_root *dev_root = fs_info->dev_root;
5617 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5618 struct extent_buffer *eb;
5619 int slot;
5620 int ret = 0;
5621 struct btrfs_device *device;
5622 struct btrfs_path *path = NULL;
5623 int i;
5624
5625 path = btrfs_alloc_path();
5626 if (!path) {
5627 ret = -ENOMEM;
5628 goto out;
5629 }
5630
5631 mutex_lock(&fs_devices->device_list_mutex);
5632 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5633 int item_size;
5634 struct btrfs_dev_stats_item *ptr;
5635
5636 key.objectid = 0;
5637 key.type = BTRFS_DEV_STATS_KEY;
5638 key.offset = device->devid;
5639 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5640 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005641 __btrfs_reset_dev_stats(device);
5642 device->dev_stats_valid = 1;
5643 btrfs_release_path(path);
5644 continue;
5645 }
5646 slot = path->slots[0];
5647 eb = path->nodes[0];
5648 btrfs_item_key_to_cpu(eb, &found_key, slot);
5649 item_size = btrfs_item_size_nr(eb, slot);
5650
5651 ptr = btrfs_item_ptr(eb, slot,
5652 struct btrfs_dev_stats_item);
5653
5654 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5655 if (item_size >= (1 + i) * sizeof(__le64))
5656 btrfs_dev_stat_set(device, i,
5657 btrfs_dev_stats_value(eb, ptr, i));
5658 else
5659 btrfs_dev_stat_reset(device, i);
5660 }
5661
5662 device->dev_stats_valid = 1;
5663 btrfs_dev_stat_print_on_load(device);
5664 btrfs_release_path(path);
5665 }
5666 mutex_unlock(&fs_devices->device_list_mutex);
5667
5668out:
5669 btrfs_free_path(path);
5670 return ret < 0 ? ret : 0;
5671}
5672
5673static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5674 struct btrfs_root *dev_root,
5675 struct btrfs_device *device)
5676{
5677 struct btrfs_path *path;
5678 struct btrfs_key key;
5679 struct extent_buffer *eb;
5680 struct btrfs_dev_stats_item *ptr;
5681 int ret;
5682 int i;
5683
5684 key.objectid = 0;
5685 key.type = BTRFS_DEV_STATS_KEY;
5686 key.offset = device->devid;
5687
5688 path = btrfs_alloc_path();
5689 BUG_ON(!path);
5690 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5691 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005692 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5693 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005694 goto out;
5695 }
5696
5697 if (ret == 0 &&
5698 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5699 /* need to delete old one and insert a new one */
5700 ret = btrfs_del_item(trans, dev_root, path);
5701 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005702 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5703 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005704 goto out;
5705 }
5706 ret = 1;
5707 }
5708
5709 if (ret == 1) {
5710 /* need to insert a new item */
5711 btrfs_release_path(path);
5712 ret = btrfs_insert_empty_item(trans, dev_root, path,
5713 &key, sizeof(*ptr));
5714 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005715 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5716 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005717 goto out;
5718 }
5719 }
5720
5721 eb = path->nodes[0];
5722 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5723 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5724 btrfs_set_dev_stats_value(eb, ptr, i,
5725 btrfs_dev_stat_read(device, i));
5726 btrfs_mark_buffer_dirty(eb);
5727
5728out:
5729 btrfs_free_path(path);
5730 return ret;
5731}
5732
5733/*
5734 * called from commit_transaction. Writes all changed device stats to disk.
5735 */
5736int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5737 struct btrfs_fs_info *fs_info)
5738{
5739 struct btrfs_root *dev_root = fs_info->dev_root;
5740 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5741 struct btrfs_device *device;
5742 int ret = 0;
5743
5744 mutex_lock(&fs_devices->device_list_mutex);
5745 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5746 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5747 continue;
5748
5749 ret = update_dev_stat_item(trans, dev_root, device);
5750 if (!ret)
5751 device->dev_stats_dirty = 0;
5752 }
5753 mutex_unlock(&fs_devices->device_list_mutex);
5754
5755 return ret;
5756}
5757
Stefan Behrens442a4f62012-05-25 16:06:08 +02005758void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5759{
5760 btrfs_dev_stat_inc(dev, index);
5761 btrfs_dev_stat_print_on_error(dev);
5762}
5763
5764void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5765{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005766 if (!dev->dev_stats_valid)
5767 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005768 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005769 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005770 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005771 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5772 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5773 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5774 btrfs_dev_stat_read(dev,
5775 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5776 btrfs_dev_stat_read(dev,
5777 BTRFS_DEV_STAT_GENERATION_ERRS));
5778}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005779
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005780static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5781{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005782 int i;
5783
5784 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5785 if (btrfs_dev_stat_read(dev, i) != 0)
5786 break;
5787 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5788 return; /* all values == 0, suppress message */
5789
Josef Bacik606686e2012-06-04 14:03:51 -04005790 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5791 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005792 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5793 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5794 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5795 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5796 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5797}
5798
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005799int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005800 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005801{
5802 struct btrfs_device *dev;
5803 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5804 int i;
5805
5806 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005807 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005808 mutex_unlock(&fs_devices->device_list_mutex);
5809
5810 if (!dev) {
5811 printk(KERN_WARNING
5812 "btrfs: get dev_stats failed, device not found\n");
5813 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005814 } else if (!dev->dev_stats_valid) {
5815 printk(KERN_WARNING
5816 "btrfs: get dev_stats failed, not yet valid\n");
5817 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005818 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005819 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5820 if (stats->nr_items > i)
5821 stats->values[i] =
5822 btrfs_dev_stat_read_and_reset(dev, i);
5823 else
5824 btrfs_dev_stat_reset(dev, i);
5825 }
5826 } else {
5827 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5828 if (stats->nr_items > i)
5829 stats->values[i] = btrfs_dev_stat_read(dev, i);
5830 }
5831 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5832 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5833 return 0;
5834}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01005835
5836int btrfs_scratch_superblock(struct btrfs_device *device)
5837{
5838 struct buffer_head *bh;
5839 struct btrfs_super_block *disk_super;
5840
5841 bh = btrfs_read_dev_super(device->bdev);
5842 if (!bh)
5843 return -EINVAL;
5844 disk_super = (struct btrfs_super_block *)bh->b_data;
5845
5846 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5847 set_buffer_dirty(bh);
5848 sync_dirty_buffer(bh);
5849 brelse(bh);
5850
5851 return 0;
5852}