blob: 3f4e70e171ed440513a58204492d90074175c3d8 [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>
Chris Mason593060d2008-03-25 16:50:33 -040028#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050029#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040030#include "ctree.h"
31#include "extent_map.h"
32#include "disk-io.h"
33#include "transaction.h"
34#include "print-tree.h"
35#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040036#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010037#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040038#include "rcu-string.h"
Chris Mason0b86a832008-03-24 15:01:56 -040039
Yan Zheng2b820322008-11-17 21:11:30 -050040static int init_first_rw_device(struct btrfs_trans_handle *trans,
41 struct btrfs_root *root,
42 struct btrfs_device *device);
43static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020044static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
45static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050046
Chris Mason8a4b83c2008-03-24 15:02:07 -040047static DEFINE_MUTEX(uuid_mutex);
48static LIST_HEAD(fs_uuids);
49
Chris Mason7d9eb122008-07-08 14:19:17 -040050static void lock_chunks(struct btrfs_root *root)
51{
Chris Mason7d9eb122008-07-08 14:19:17 -040052 mutex_lock(&root->fs_info->chunk_mutex);
53}
54
55static void unlock_chunks(struct btrfs_root *root)
56{
Chris Mason7d9eb122008-07-08 14:19:17 -040057 mutex_unlock(&root->fs_info->chunk_mutex);
58}
59
Yan Zhenge4404d62008-12-12 10:03:26 -050060static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
61{
62 struct btrfs_device *device;
63 WARN_ON(fs_devices->opened);
64 while (!list_empty(&fs_devices->devices)) {
65 device = list_entry(fs_devices->devices.next,
66 struct btrfs_device, dev_list);
67 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -040068 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -050069 kfree(device);
70 }
71 kfree(fs_devices);
72}
73
Jeff Mahoney143bede2012-03-01 14:56:26 +010074void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040075{
76 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040077
Yan Zheng2b820322008-11-17 21:11:30 -050078 while (!list_empty(&fs_uuids)) {
79 fs_devices = list_entry(fs_uuids.next,
80 struct btrfs_fs_devices, list);
81 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050082 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040083 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040084}
85
Chris Masona1b32a52008-09-05 16:09:51 -040086static noinline struct btrfs_device *__find_device(struct list_head *head,
87 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040088{
89 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040090
Qinghuang Fengc6e30872009-01-21 10:59:08 -050091 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040092 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040093 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040094 return dev;
Chris Masona4437552008-04-18 10:29:38 -040095 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040096 }
97 return NULL;
98}
99
Chris Masona1b32a52008-09-05 16:09:51 -0400100static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102 struct btrfs_fs_devices *fs_devices;
103
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500104 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400105 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
106 return fs_devices;
107 }
108 return NULL;
109}
110
Chris Masonffbd5172009-04-20 15:50:09 -0400111static void requeue_list(struct btrfs_pending_bios *pending_bios,
112 struct bio *head, struct bio *tail)
113{
114
115 struct bio *old_head;
116
117 old_head = pending_bios->head;
118 pending_bios->head = head;
119 if (pending_bios->tail)
120 tail->bi_next = old_head;
121 else
122 pending_bios->tail = tail;
123}
124
Chris Mason8b712842008-06-11 16:50:36 -0400125/*
126 * we try to collect pending bios for a device so we don't get a large
127 * number of procs sending bios down to the same device. This greatly
128 * improves the schedulers ability to collect and merge the bios.
129 *
130 * But, it also turns into a long list of bios to process and that is sure
131 * to eventually make the worker thread block. The solution here is to
132 * make some progress and then put this work struct back at the end of
133 * the list if the block device is congested. This way, multiple devices
134 * can make progress from a single worker thread.
135 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100136static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400137{
138 struct bio *pending;
139 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400140 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400141 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400142 struct bio *tail;
143 struct bio *cur;
144 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400145 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400146 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400147 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400148 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400149 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000150 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400151 struct blk_plug plug;
152
153 /*
154 * this function runs all the bios we've collected for
155 * a particular device. We don't want to wander off to
156 * another device without first sending all of these down.
157 * So, setup a plug here and finish it off before we return
158 */
159 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400160
Chris Masonbedf7622009-04-03 10:32:58 -0400161 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400162 fs_info = device->dev_root->fs_info;
163 limit = btrfs_async_submit_limit(fs_info);
164 limit = limit * 2 / 3;
165
Chris Mason8b712842008-06-11 16:50:36 -0400166loop:
167 spin_lock(&device->io_lock);
168
Chris Masona6837052009-02-04 09:19:41 -0500169loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400170 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400171
Chris Mason8b712842008-06-11 16:50:36 -0400172 /* take all the bios off the list at once and process them
173 * later on (without the lock held). But, remember the
174 * tail and other pointers so the bios can be properly reinserted
175 * into the list if we hit congestion
176 */
Chris Masond84275c2009-06-09 15:39:08 -0400177 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400178 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400179 force_reg = 1;
180 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400181 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400182 force_reg = 0;
183 }
Chris Masonffbd5172009-04-20 15:50:09 -0400184
185 pending = pending_bios->head;
186 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400187 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400188
189 /*
190 * if pending was null this time around, no bios need processing
191 * at all and we can stop. Otherwise it'll loop back up again
192 * and do an additional check so no bios are missed.
193 *
194 * device->running_pending is used to synchronize with the
195 * schedule_bio code.
196 */
Chris Masonffbd5172009-04-20 15:50:09 -0400197 if (device->pending_sync_bios.head == NULL &&
198 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400199 again = 0;
200 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400201 } else {
202 again = 1;
203 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400204 }
Chris Masonffbd5172009-04-20 15:50:09 -0400205
206 pending_bios->head = NULL;
207 pending_bios->tail = NULL;
208
Chris Mason8b712842008-06-11 16:50:36 -0400209 spin_unlock(&device->io_lock);
210
Chris Masond3977122009-01-05 21:25:51 -0500211 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400212
213 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400214 /* we want to work on both lists, but do more bios on the
215 * sync list than the regular list
216 */
217 if ((num_run > 32 &&
218 pending_bios != &device->pending_sync_bios &&
219 device->pending_sync_bios.head) ||
220 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
221 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400222 spin_lock(&device->io_lock);
223 requeue_list(pending_bios, pending, tail);
224 goto loop_lock;
225 }
226
Chris Mason8b712842008-06-11 16:50:36 -0400227 cur = pending;
228 pending = pending->bi_next;
229 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400230
Josef Bacik66657b32012-08-01 15:36:24 -0400231 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400232 waitqueue_active(&fs_info->async_submit_wait))
233 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400234
235 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400236
Chris Mason2ab1ba62011-08-04 14:28:36 -0400237 /*
238 * if we're doing the sync list, record that our
239 * plug has some sync requests on it
240 *
241 * If we're doing the regular list and there are
242 * sync requests sitting around, unplug before
243 * we add more
244 */
245 if (pending_bios == &device->pending_sync_bios) {
246 sync_pending = 1;
247 } else if (sync_pending) {
248 blk_finish_plug(&plug);
249 blk_start_plug(&plug);
250 sync_pending = 0;
251 }
252
Stefan Behrens21adbd52011-11-09 13:44:05 +0100253 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400254 num_run++;
255 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100256 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400257 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400258
259 /*
260 * we made progress, there is more work to do and the bdi
261 * is now congested. Back off and let other work structs
262 * run instead
263 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400264 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500265 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400266 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400267
Chris Masonb765ead2009-04-03 10:27:10 -0400268 ioc = current->io_context;
269
270 /*
271 * the main goal here is that we don't want to
272 * block if we're going to be able to submit
273 * more requests without blocking.
274 *
275 * This code does two great things, it pokes into
276 * the elevator code from a filesystem _and_
277 * it makes assumptions about how batching works.
278 */
279 if (ioc && ioc->nr_batch_requests > 0 &&
280 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
281 (last_waited == 0 ||
282 ioc->last_waited == last_waited)) {
283 /*
284 * we want to go through our batch of
285 * requests and stop. So, we copy out
286 * the ioc->last_waited time and test
287 * against it before looping
288 */
289 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100290 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400291 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400292 continue;
293 }
Chris Mason8b712842008-06-11 16:50:36 -0400294 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400295 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500296 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400297
298 spin_unlock(&device->io_lock);
299 btrfs_requeue_work(&device->work);
300 goto done;
301 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500302 /* unplug every 64 requests just for good measure */
303 if (batch_run % 64 == 0) {
304 blk_finish_plug(&plug);
305 blk_start_plug(&plug);
306 sync_pending = 0;
307 }
Chris Mason8b712842008-06-11 16:50:36 -0400308 }
Chris Masonffbd5172009-04-20 15:50:09 -0400309
Chris Mason51684082010-03-10 15:33:32 -0500310 cond_resched();
311 if (again)
312 goto loop;
313
314 spin_lock(&device->io_lock);
315 if (device->pending_bios.head || device->pending_sync_bios.head)
316 goto loop_lock;
317 spin_unlock(&device->io_lock);
318
Chris Mason8b712842008-06-11 16:50:36 -0400319done:
Chris Mason211588a2011-04-19 20:12:40 -0400320 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400321}
322
Christoph Hellwigb2950862008-12-02 09:54:17 -0500323static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400324{
325 struct btrfs_device *device;
326
327 device = container_of(work, struct btrfs_device, work);
328 run_scheduled_bios(device);
329}
330
Chris Masona1b32a52008-09-05 16:09:51 -0400331static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400332 struct btrfs_super_block *disk_super,
333 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
334{
335 struct btrfs_device *device;
336 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400337 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400338 u64 found_transid = btrfs_super_generation(disk_super);
339
340 fs_devices = find_fsid(disk_super->fsid);
341 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400342 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400343 if (!fs_devices)
344 return -ENOMEM;
345 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400346 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400347 list_add(&fs_devices->list, &fs_uuids);
348 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
349 fs_devices->latest_devid = devid;
350 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400351 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400352 device = NULL;
353 } else {
Chris Masona4437552008-04-18 10:29:38 -0400354 device = __find_device(&fs_devices->devices, devid,
355 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400356 }
357 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500358 if (fs_devices->opened)
359 return -EBUSY;
360
Chris Mason8a4b83c2008-03-24 15:02:07 -0400361 device = kzalloc(sizeof(*device), GFP_NOFS);
362 if (!device) {
363 /* we can safely leave the fs_devices entry around */
364 return -ENOMEM;
365 }
366 device->devid = devid;
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200367 device->dev_stats_valid = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400368 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400369 memcpy(device->uuid, disk_super->dev_item.uuid,
370 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400371 spin_lock_init(&device->io_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400372
373 name = rcu_string_strdup(path, GFP_NOFS);
374 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400375 kfree(device);
376 return -ENOMEM;
377 }
Josef Bacik606686e2012-06-04 14:03:51 -0400378 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -0500379 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400380
Arne Jansen90519d62011-05-23 14:30:00 +0200381 /* init readahead state */
382 spin_lock_init(&device->reada_lock);
383 device->reada_curr_zone = NULL;
384 atomic_set(&device->reada_in_flight, 0);
385 device->reada_next = 0;
386 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
387 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
388
Chris Masone5e9a522009-06-10 15:17:02 -0400389 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000390 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400391 mutex_unlock(&fs_devices->device_list_mutex);
392
Yan Zheng2b820322008-11-17 21:11:30 -0500393 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400394 fs_devices->num_devices++;
Josef Bacik606686e2012-06-04 14:03:51 -0400395 } else if (!device->name || strcmp(device->name->str, path)) {
396 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000397 if (!name)
398 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400399 rcu_string_free(device->name);
400 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500401 if (device->missing) {
402 fs_devices->missing_devices--;
403 device->missing = 0;
404 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400405 }
406
407 if (found_transid > fs_devices->latest_trans) {
408 fs_devices->latest_devid = devid;
409 fs_devices->latest_trans = found_transid;
410 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400411 *fs_devices_ret = fs_devices;
412 return 0;
413}
414
Yan Zhenge4404d62008-12-12 10:03:26 -0500415static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
416{
417 struct btrfs_fs_devices *fs_devices;
418 struct btrfs_device *device;
419 struct btrfs_device *orig_dev;
420
421 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
422 if (!fs_devices)
423 return ERR_PTR(-ENOMEM);
424
425 INIT_LIST_HEAD(&fs_devices->devices);
426 INIT_LIST_HEAD(&fs_devices->alloc_list);
427 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400428 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500429 fs_devices->latest_devid = orig->latest_devid;
430 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400431 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500432 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
433
Xiao Guangrong46224702011-04-20 10:08:47 +0000434 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500435 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400436 struct rcu_string *name;
437
Yan Zhenge4404d62008-12-12 10:03:26 -0500438 device = kzalloc(sizeof(*device), GFP_NOFS);
439 if (!device)
440 goto error;
441
Josef Bacik606686e2012-06-04 14:03:51 -0400442 /*
443 * This is ok to do without rcu read locked because we hold the
444 * uuid mutex so nothing we touch in here is going to disappear.
445 */
446 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
447 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400448 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500449 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400450 }
Josef Bacik606686e2012-06-04 14:03:51 -0400451 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500452
453 device->devid = orig_dev->devid;
454 device->work.func = pending_bios_fn;
455 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500456 spin_lock_init(&device->io_lock);
457 INIT_LIST_HEAD(&device->dev_list);
458 INIT_LIST_HEAD(&device->dev_alloc_list);
459
460 list_add(&device->dev_list, &fs_devices->devices);
461 device->fs_devices = fs_devices;
462 fs_devices->num_devices++;
463 }
464 return fs_devices;
465error:
466 free_fs_devices(fs_devices);
467 return ERR_PTR(-ENOMEM);
468}
469
Jeff Mahoney143bede2012-03-01 14:56:26 +0100470void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
Chris Masondfe25022008-05-13 13:46:40 -0400471{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500472 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400473
Chris Masona6b0d5c2012-02-20 20:53:43 -0500474 struct block_device *latest_bdev = NULL;
475 u64 latest_devid = 0;
476 u64 latest_transid = 0;
477
Chris Masondfe25022008-05-13 13:46:40 -0400478 mutex_lock(&uuid_mutex);
479again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000480 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500481 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500482 if (device->in_fs_metadata) {
483 if (!latest_transid ||
484 device->generation > latest_transid) {
485 latest_devid = device->devid;
486 latest_transid = device->generation;
487 latest_bdev = device->bdev;
488 }
Yan Zheng2b820322008-11-17 21:11:30 -0500489 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500490 }
Yan Zheng2b820322008-11-17 21:11:30 -0500491
492 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100493 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500494 device->bdev = NULL;
495 fs_devices->open_devices--;
496 }
497 if (device->writeable) {
498 list_del_init(&device->dev_alloc_list);
499 device->writeable = 0;
500 fs_devices->rw_devices--;
501 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500502 list_del_init(&device->dev_list);
503 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400504 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500505 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400506 }
Yan Zheng2b820322008-11-17 21:11:30 -0500507
508 if (fs_devices->seed) {
509 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500510 goto again;
511 }
512
Chris Masona6b0d5c2012-02-20 20:53:43 -0500513 fs_devices->latest_bdev = latest_bdev;
514 fs_devices->latest_devid = latest_devid;
515 fs_devices->latest_trans = latest_transid;
516
Chris Masondfe25022008-05-13 13:46:40 -0400517 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400518}
Chris Masona0af4692008-05-13 16:03:06 -0400519
Xiao Guangrong1f781602011-04-20 10:09:16 +0000520static void __free_device(struct work_struct *work)
521{
522 struct btrfs_device *device;
523
524 device = container_of(work, struct btrfs_device, rcu_work);
525
526 if (device->bdev)
527 blkdev_put(device->bdev, device->mode);
528
Josef Bacik606686e2012-06-04 14:03:51 -0400529 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000530 kfree(device);
531}
532
533static void free_device(struct rcu_head *head)
534{
535 struct btrfs_device *device;
536
537 device = container_of(head, struct btrfs_device, rcu);
538
539 INIT_WORK(&device->rcu_work, __free_device);
540 schedule_work(&device->rcu_work);
541}
542
Yan Zheng2b820322008-11-17 21:11:30 -0500543static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400544{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400545 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500546
Yan Zheng2b820322008-11-17 21:11:30 -0500547 if (--fs_devices->opened > 0)
548 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400549
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000550 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500551 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000552 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400553 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000554
555 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400556 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000557
Yan Zheng2b820322008-11-17 21:11:30 -0500558 if (device->writeable) {
559 list_del_init(&device->dev_alloc_list);
560 fs_devices->rw_devices--;
561 }
562
Josef Bacikd5e20032011-08-04 14:52:27 +0000563 if (device->can_discard)
564 fs_devices->num_can_discard--;
565
Xiao Guangrong1f781602011-04-20 10:09:16 +0000566 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100567 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000568 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400569
570 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400571 if (device->name) {
572 name = rcu_string_strdup(device->name->str, GFP_NOFS);
573 BUG_ON(device->name && !name); /* -ENOMEM */
574 rcu_assign_pointer(new_device->name, name);
575 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000576 new_device->bdev = NULL;
577 new_device->writeable = 0;
578 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000579 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000580 list_replace_rcu(&device->dev_list, &new_device->dev_list);
581
582 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400583 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000584 mutex_unlock(&fs_devices->device_list_mutex);
585
Yan Zhenge4404d62008-12-12 10:03:26 -0500586 WARN_ON(fs_devices->open_devices);
587 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500588 fs_devices->opened = 0;
589 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500590
Chris Mason8a4b83c2008-03-24 15:02:07 -0400591 return 0;
592}
593
Yan Zheng2b820322008-11-17 21:11:30 -0500594int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
595{
Yan Zhenge4404d62008-12-12 10:03:26 -0500596 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500597 int ret;
598
599 mutex_lock(&uuid_mutex);
600 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500601 if (!fs_devices->opened) {
602 seed_devices = fs_devices->seed;
603 fs_devices->seed = NULL;
604 }
Yan Zheng2b820322008-11-17 21:11:30 -0500605 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500606
607 while (seed_devices) {
608 fs_devices = seed_devices;
609 seed_devices = fs_devices->seed;
610 __btrfs_close_devices(fs_devices);
611 free_fs_devices(fs_devices);
612 }
Yan Zheng2b820322008-11-17 21:11:30 -0500613 return ret;
614}
615
Yan Zhenge4404d62008-12-12 10:03:26 -0500616static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
617 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400618{
Josef Bacikd5e20032011-08-04 14:52:27 +0000619 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400620 struct block_device *bdev;
621 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400622 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400623 struct block_device *latest_bdev = NULL;
624 struct buffer_head *bh;
625 struct btrfs_super_block *disk_super;
626 u64 latest_devid = 0;
627 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400628 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500629 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400630 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400631
Tejun Heod4d77622010-11-13 11:55:18 +0100632 flags |= FMODE_EXCL;
633
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500634 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400635 if (device->bdev)
636 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400637 if (!device->name)
638 continue;
639
Josef Bacik606686e2012-06-04 14:03:51 -0400640 bdev = blkdev_get_by_path(device->name->str, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400641 if (IS_ERR(bdev)) {
Josef Bacik606686e2012-06-04 14:03:51 -0400642 printk(KERN_INFO "open %s failed\n", device->name->str);
Chris Masona0af4692008-05-13 16:03:06 -0400643 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400644 }
Chris Mason3c4bb262012-03-27 18:56:56 -0400645 filemap_write_and_wait(bdev->bd_inode->i_mapping);
646 invalidate_bdev(bdev);
Chris Masona061fc82008-05-07 11:43:44 -0400647 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400648
Yan Zhenga512bbf2008-12-08 16:46:26 -0500649 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300650 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400651 goto error_close;
652
653 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000654 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400655 if (devid != device->devid)
656 goto error_brelse;
657
Yan Zheng2b820322008-11-17 21:11:30 -0500658 if (memcmp(device->uuid, disk_super->dev_item.uuid,
659 BTRFS_UUID_SIZE))
660 goto error_brelse;
661
662 device->generation = btrfs_super_generation(disk_super);
663 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400664 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500665 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400666 latest_bdev = bdev;
667 }
668
Yan Zheng2b820322008-11-17 21:11:30 -0500669 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
670 device->writeable = 0;
671 } else {
672 device->writeable = !bdev_read_only(bdev);
673 seeding = 0;
674 }
675
Josef Bacikd5e20032011-08-04 14:52:27 +0000676 q = bdev_get_queue(bdev);
677 if (blk_queue_discard(q)) {
678 device->can_discard = 1;
679 fs_devices->num_can_discard++;
680 }
681
Chris Mason8a4b83c2008-03-24 15:02:07 -0400682 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400683 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500684 device->mode = flags;
685
Chris Masonc2898112009-06-10 09:51:32 -0400686 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
687 fs_devices->rotating = 1;
688
Chris Masona0af4692008-05-13 16:03:06 -0400689 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500690 if (device->writeable) {
691 fs_devices->rw_devices++;
692 list_add(&device->dev_alloc_list,
693 &fs_devices->alloc_list);
694 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000695 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400696 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400697
Chris Masona0af4692008-05-13 16:03:06 -0400698error_brelse:
699 brelse(bh);
700error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100701 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400702error:
703 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400704 }
Chris Masona0af4692008-05-13 16:03:06 -0400705 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300706 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400707 goto out;
708 }
Yan Zheng2b820322008-11-17 21:11:30 -0500709 fs_devices->seeding = seeding;
710 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400711 fs_devices->latest_bdev = latest_bdev;
712 fs_devices->latest_devid = latest_devid;
713 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500714 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400715out:
Yan Zheng2b820322008-11-17 21:11:30 -0500716 return ret;
717}
718
719int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500720 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500721{
722 int ret;
723
724 mutex_lock(&uuid_mutex);
725 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500726 fs_devices->opened++;
727 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500728 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500729 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500730 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400731 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400732 return ret;
733}
734
Christoph Hellwig97288f22008-12-02 06:36:09 -0500735int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400736 struct btrfs_fs_devices **fs_devices_ret)
737{
738 struct btrfs_super_block *disk_super;
739 struct block_device *bdev;
740 struct buffer_head *bh;
741 int ret;
742 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400743 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400744 u64 total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400745
Tejun Heod4d77622010-11-13 11:55:18 +0100746 flags |= FMODE_EXCL;
747 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400748
749 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400750 ret = PTR_ERR(bdev);
751 goto error;
752 }
753
Al Viro10f63272011-11-17 15:05:22 -0500754 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400755 ret = set_blocksize(bdev, 4096);
756 if (ret)
757 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500758 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400759 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000760 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400761 goto error_close;
762 }
763 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000764 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400765 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400766 total_devices = btrfs_super_num_devices(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400767 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500768 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200769 else
770 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500771 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500772 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400773 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400774 if (!ret && fs_devices_ret)
775 (*fs_devices_ret)->total_devices = total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400776 brelse(bh);
777error_close:
Al Viro10f63272011-11-17 15:05:22 -0500778 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100779 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400780error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400781 return ret;
782}
Chris Mason0b86a832008-03-24 15:01:56 -0400783
Miao Xie6d07bce2011-01-05 10:07:31 +0000784/* helper to account the used device space in the range */
785int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
786 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400787{
788 struct btrfs_key key;
789 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000790 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500791 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000792 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400793 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000794 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400795 struct extent_buffer *l;
796
Miao Xie6d07bce2011-01-05 10:07:31 +0000797 *length = 0;
798
799 if (start >= device->total_bytes)
800 return 0;
801
Yan Zheng2b820322008-11-17 21:11:30 -0500802 path = btrfs_alloc_path();
803 if (!path)
804 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400805 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400806
Chris Mason0b86a832008-03-24 15:01:56 -0400807 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000808 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400809 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000810
811 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400812 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000813 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400814 if (ret > 0) {
815 ret = btrfs_previous_item(root, path, key.objectid, key.type);
816 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000817 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400818 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000819
Chris Mason0b86a832008-03-24 15:01:56 -0400820 while (1) {
821 l = path->nodes[0];
822 slot = path->slots[0];
823 if (slot >= btrfs_header_nritems(l)) {
824 ret = btrfs_next_leaf(root, path);
825 if (ret == 0)
826 continue;
827 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000828 goto out;
829
830 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400831 }
832 btrfs_item_key_to_cpu(l, &key, slot);
833
834 if (key.objectid < device->devid)
835 goto next;
836
837 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000838 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400839
Chris Masond3977122009-01-05 21:25:51 -0500840 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400841 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400842
Chris Mason0b86a832008-03-24 15:01:56 -0400843 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000844 extent_end = key.offset + btrfs_dev_extent_length(l,
845 dev_extent);
846 if (key.offset <= start && extent_end > end) {
847 *length = end - start + 1;
848 break;
849 } else if (key.offset <= start && extent_end > start)
850 *length += extent_end - start;
851 else if (key.offset > start && extent_end <= end)
852 *length += extent_end - key.offset;
853 else if (key.offset > start && key.offset <= end) {
854 *length += end - key.offset + 1;
855 break;
856 } else if (key.offset > end)
857 break;
858
859next:
860 path->slots[0]++;
861 }
862 ret = 0;
863out:
864 btrfs_free_path(path);
865 return ret;
866}
867
Chris Mason0b86a832008-03-24 15:01:56 -0400868/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000869 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000870 * @device: the device which we search the free space in
871 * @num_bytes: the size of the free space that we need
872 * @start: store the start of the free space.
873 * @len: the size of the free space. that we find, or the size of the max
874 * free space if we don't find suitable free space
875 *
Chris Mason0b86a832008-03-24 15:01:56 -0400876 * this uses a pretty simple search, the expectation is that it is
877 * called very infrequently and that a given device has a small number
878 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000879 *
880 * @start is used to store the start of the free space if we find. But if we
881 * don't find suitable free space, it will be used to store the start position
882 * of the max free space.
883 *
884 * @len is used to store the size of the free space that we find.
885 * But if we don't find suitable free space, it is used to store the size of
886 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400887 */
Li Zefan125ccb02011-12-08 15:07:24 +0800888int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000889 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400890{
891 struct btrfs_key key;
892 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000893 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400894 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000895 u64 hole_size;
896 u64 max_hole_start;
897 u64 max_hole_size;
898 u64 extent_end;
899 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400900 u64 search_end = device->total_bytes;
901 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000902 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400903 struct extent_buffer *l;
904
Chris Mason0b86a832008-03-24 15:01:56 -0400905 /* FIXME use last free of some kind */
906
907 /* we don't want to overwrite the superblock on the drive,
908 * so we make sure to start at an offset of at least 1MB
909 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200910 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400911
Miao Xie7bfc8372011-01-05 10:07:26 +0000912 max_hole_start = search_start;
913 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000914 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000915
916 if (search_start >= search_end) {
917 ret = -ENOSPC;
918 goto error;
919 }
920
921 path = btrfs_alloc_path();
922 if (!path) {
923 ret = -ENOMEM;
924 goto error;
925 }
926 path->reada = 2;
927
Chris Mason0b86a832008-03-24 15:01:56 -0400928 key.objectid = device->devid;
929 key.offset = search_start;
930 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000931
Li Zefan125ccb02011-12-08 15:07:24 +0800932 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400933 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000934 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400935 if (ret > 0) {
936 ret = btrfs_previous_item(root, path, key.objectid, key.type);
937 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000938 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400939 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000940
Chris Mason0b86a832008-03-24 15:01:56 -0400941 while (1) {
942 l = path->nodes[0];
943 slot = path->slots[0];
944 if (slot >= btrfs_header_nritems(l)) {
945 ret = btrfs_next_leaf(root, path);
946 if (ret == 0)
947 continue;
948 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000949 goto out;
950
951 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400952 }
953 btrfs_item_key_to_cpu(l, &key, slot);
954
955 if (key.objectid < device->devid)
956 goto next;
957
958 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000959 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400960
Chris Mason0b86a832008-03-24 15:01:56 -0400961 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
962 goto next;
963
Miao Xie7bfc8372011-01-05 10:07:26 +0000964 if (key.offset > search_start) {
965 hole_size = key.offset - search_start;
966
967 if (hole_size > max_hole_size) {
968 max_hole_start = search_start;
969 max_hole_size = hole_size;
970 }
971
972 /*
973 * If this free space is greater than which we need,
974 * it must be the max free space that we have found
975 * until now, so max_hole_start must point to the start
976 * of this free space and the length of this free space
977 * is stored in max_hole_size. Thus, we return
978 * max_hole_start and max_hole_size and go back to the
979 * caller.
980 */
981 if (hole_size >= num_bytes) {
982 ret = 0;
983 goto out;
984 }
985 }
986
Chris Mason0b86a832008-03-24 15:01:56 -0400987 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000988 extent_end = key.offset + btrfs_dev_extent_length(l,
989 dev_extent);
990 if (extent_end > search_start)
991 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400992next:
993 path->slots[0]++;
994 cond_resched();
995 }
Chris Mason0b86a832008-03-24 15:01:56 -0400996
liubo38c01b92011-08-02 02:39:03 +0000997 /*
998 * At this point, search_start should be the end of
999 * allocated dev extents, and when shrinking the device,
1000 * search_end may be smaller than search_start.
1001 */
1002 if (search_end > search_start)
1003 hole_size = search_end - search_start;
1004
Miao Xie7bfc8372011-01-05 10:07:26 +00001005 if (hole_size > max_hole_size) {
1006 max_hole_start = search_start;
1007 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001008 }
Chris Mason0b86a832008-03-24 15:01:56 -04001009
Miao Xie7bfc8372011-01-05 10:07:26 +00001010 /* See above. */
1011 if (hole_size < num_bytes)
1012 ret = -ENOSPC;
1013 else
1014 ret = 0;
1015
1016out:
Yan Zheng2b820322008-11-17 21:11:30 -05001017 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001018error:
1019 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001020 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001021 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001022 return ret;
1023}
1024
Christoph Hellwigb2950862008-12-02 09:54:17 -05001025static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001026 struct btrfs_device *device,
1027 u64 start)
1028{
1029 int ret;
1030 struct btrfs_path *path;
1031 struct btrfs_root *root = device->dev_root;
1032 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001033 struct btrfs_key found_key;
1034 struct extent_buffer *leaf = NULL;
1035 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001036
1037 path = btrfs_alloc_path();
1038 if (!path)
1039 return -ENOMEM;
1040
1041 key.objectid = device->devid;
1042 key.offset = start;
1043 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001044again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001045 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001046 if (ret > 0) {
1047 ret = btrfs_previous_item(root, path, key.objectid,
1048 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001049 if (ret)
1050 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001051 leaf = path->nodes[0];
1052 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1053 extent = btrfs_item_ptr(leaf, path->slots[0],
1054 struct btrfs_dev_extent);
1055 BUG_ON(found_key.offset > start || found_key.offset +
1056 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001057 key = found_key;
1058 btrfs_release_path(path);
1059 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001060 } else if (ret == 0) {
1061 leaf = path->nodes[0];
1062 extent = btrfs_item_ptr(leaf, path->slots[0],
1063 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001064 } else {
1065 btrfs_error(root->fs_info, ret, "Slot search failed");
1066 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001067 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001068
Josef Bacik2bf64752011-09-26 17:12:22 -04001069 if (device->bytes_used > 0) {
1070 u64 len = btrfs_dev_extent_length(leaf, extent);
1071 device->bytes_used -= len;
1072 spin_lock(&root->fs_info->free_chunk_lock);
1073 root->fs_info->free_chunk_space += len;
1074 spin_unlock(&root->fs_info->free_chunk_lock);
1075 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001076 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001077 if (ret) {
1078 btrfs_error(root->fs_info, ret,
1079 "Failed to remove dev extent item");
1080 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001081out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001082 btrfs_free_path(path);
1083 return ret;
1084}
1085
Yan Zheng2b820322008-11-17 21:11:30 -05001086int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001087 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001088 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001089 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001090{
1091 int ret;
1092 struct btrfs_path *path;
1093 struct btrfs_root *root = device->dev_root;
1094 struct btrfs_dev_extent *extent;
1095 struct extent_buffer *leaf;
1096 struct btrfs_key key;
1097
Chris Masondfe25022008-05-13 13:46:40 -04001098 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001099 path = btrfs_alloc_path();
1100 if (!path)
1101 return -ENOMEM;
1102
Chris Mason0b86a832008-03-24 15:01:56 -04001103 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001104 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001105 key.type = BTRFS_DEV_EXTENT_KEY;
1106 ret = btrfs_insert_empty_item(trans, root, path, &key,
1107 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001108 if (ret)
1109 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001110
1111 leaf = path->nodes[0];
1112 extent = btrfs_item_ptr(leaf, path->slots[0],
1113 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001114 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1115 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1116 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1117
1118 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1119 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1120 BTRFS_UUID_SIZE);
1121
Chris Mason0b86a832008-03-24 15:01:56 -04001122 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1123 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001124out:
Chris Mason0b86a832008-03-24 15:01:56 -04001125 btrfs_free_path(path);
1126 return ret;
1127}
1128
Chris Masona1b32a52008-09-05 16:09:51 -04001129static noinline int find_next_chunk(struct btrfs_root *root,
1130 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001131{
1132 struct btrfs_path *path;
1133 int ret;
1134 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001135 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001136 struct btrfs_key found_key;
1137
1138 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001139 if (!path)
1140 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001141
Chris Masone17cade2008-04-15 15:41:47 -04001142 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001143 key.offset = (u64)-1;
1144 key.type = BTRFS_CHUNK_ITEM_KEY;
1145
1146 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1147 if (ret < 0)
1148 goto error;
1149
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001150 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001151
1152 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1153 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001154 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001155 } else {
1156 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1157 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001158 if (found_key.objectid != objectid)
1159 *offset = 0;
1160 else {
1161 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1162 struct btrfs_chunk);
1163 *offset = found_key.offset +
1164 btrfs_chunk_length(path->nodes[0], chunk);
1165 }
Chris Mason0b86a832008-03-24 15:01:56 -04001166 }
1167 ret = 0;
1168error:
1169 btrfs_free_path(path);
1170 return ret;
1171}
1172
Yan Zheng2b820322008-11-17 21:11:30 -05001173static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001174{
1175 int ret;
1176 struct btrfs_key key;
1177 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001178 struct btrfs_path *path;
1179
1180 root = root->fs_info->chunk_root;
1181
1182 path = btrfs_alloc_path();
1183 if (!path)
1184 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001185
1186 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1187 key.type = BTRFS_DEV_ITEM_KEY;
1188 key.offset = (u64)-1;
1189
1190 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1191 if (ret < 0)
1192 goto error;
1193
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001194 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001195
1196 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1197 BTRFS_DEV_ITEM_KEY);
1198 if (ret) {
1199 *objectid = 1;
1200 } else {
1201 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1202 path->slots[0]);
1203 *objectid = found_key.offset + 1;
1204 }
1205 ret = 0;
1206error:
Yan Zheng2b820322008-11-17 21:11:30 -05001207 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001208 return ret;
1209}
1210
1211/*
1212 * the device information is stored in the chunk root
1213 * the btrfs_device struct should be fully filled in
1214 */
1215int btrfs_add_device(struct btrfs_trans_handle *trans,
1216 struct btrfs_root *root,
1217 struct btrfs_device *device)
1218{
1219 int ret;
1220 struct btrfs_path *path;
1221 struct btrfs_dev_item *dev_item;
1222 struct extent_buffer *leaf;
1223 struct btrfs_key key;
1224 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001225
1226 root = root->fs_info->chunk_root;
1227
1228 path = btrfs_alloc_path();
1229 if (!path)
1230 return -ENOMEM;
1231
Chris Mason0b86a832008-03-24 15:01:56 -04001232 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1233 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001234 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001235
1236 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001237 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001238 if (ret)
1239 goto out;
1240
1241 leaf = path->nodes[0];
1242 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1243
1244 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001245 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001246 btrfs_set_device_type(leaf, dev_item, device->type);
1247 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1248 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1249 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001250 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1251 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001252 btrfs_set_device_group(leaf, dev_item, 0);
1253 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1254 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001255 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001256
Chris Mason0b86a832008-03-24 15:01:56 -04001257 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001258 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001259 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1260 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001261 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001262
Yan Zheng2b820322008-11-17 21:11:30 -05001263 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001264out:
1265 btrfs_free_path(path);
1266 return ret;
1267}
Chris Mason8f18cf12008-04-25 16:53:30 -04001268
Chris Masona061fc82008-05-07 11:43:44 -04001269static int btrfs_rm_dev_item(struct btrfs_root *root,
1270 struct btrfs_device *device)
1271{
1272 int ret;
1273 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001274 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001275 struct btrfs_trans_handle *trans;
1276
1277 root = root->fs_info->chunk_root;
1278
1279 path = btrfs_alloc_path();
1280 if (!path)
1281 return -ENOMEM;
1282
Yan, Zhenga22285a2010-05-16 10:48:46 -04001283 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001284 if (IS_ERR(trans)) {
1285 btrfs_free_path(path);
1286 return PTR_ERR(trans);
1287 }
Chris Masona061fc82008-05-07 11:43:44 -04001288 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1289 key.type = BTRFS_DEV_ITEM_KEY;
1290 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001291 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001292
1293 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1294 if (ret < 0)
1295 goto out;
1296
1297 if (ret > 0) {
1298 ret = -ENOENT;
1299 goto out;
1300 }
1301
1302 ret = btrfs_del_item(trans, root, path);
1303 if (ret)
1304 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001305out:
1306 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001307 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001308 btrfs_commit_transaction(trans, root);
1309 return ret;
1310}
1311
1312int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1313{
1314 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001315 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001316 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001317 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001318 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001319 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001320 u64 all_avail;
1321 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001322 u64 num_devices;
1323 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001324 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001325 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001326
Chris Masona061fc82008-05-07 11:43:44 -04001327 mutex_lock(&uuid_mutex);
1328
1329 all_avail = root->fs_info->avail_data_alloc_bits |
1330 root->fs_info->avail_system_alloc_bits |
1331 root->fs_info->avail_metadata_alloc_bits;
1332
1333 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001334 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001335 printk(KERN_ERR "btrfs: unable to go below four devices "
1336 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001337 ret = -EINVAL;
1338 goto out;
1339 }
1340
1341 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001342 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001343 printk(KERN_ERR "btrfs: unable to go below two "
1344 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001345 ret = -EINVAL;
1346 goto out;
1347 }
1348
Chris Masondfe25022008-05-13 13:46:40 -04001349 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001350 struct list_head *devices;
1351 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001352
Chris Masondfe25022008-05-13 13:46:40 -04001353 device = NULL;
1354 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001355 /*
1356 * It is safe to read the devices since the volume_mutex
1357 * is held.
1358 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001359 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001360 if (tmp->in_fs_metadata && !tmp->bdev) {
1361 device = tmp;
1362 break;
1363 }
1364 }
1365 bdev = NULL;
1366 bh = NULL;
1367 disk_super = NULL;
1368 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001369 printk(KERN_ERR "btrfs: no missing devices found to "
1370 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001371 goto out;
1372 }
Chris Masondfe25022008-05-13 13:46:40 -04001373 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001374 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1375 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001376 if (IS_ERR(bdev)) {
1377 ret = PTR_ERR(bdev);
1378 goto out;
1379 }
1380
Yan Zheng2b820322008-11-17 21:11:30 -05001381 set_blocksize(bdev, 4096);
Chris Mason3c4bb262012-03-27 18:56:56 -04001382 invalidate_bdev(bdev);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001383 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001384 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001385 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001386 goto error_close;
1387 }
1388 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001389 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001390 dev_uuid = disk_super->dev_item.uuid;
1391 device = btrfs_find_device(root, devid, dev_uuid,
1392 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001393 if (!device) {
1394 ret = -ENOENT;
1395 goto error_brelse;
1396 }
Chris Masondfe25022008-05-13 13:46:40 -04001397 }
Yan Zheng2b820322008-11-17 21:11:30 -05001398
1399 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001400 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1401 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001402 ret = -EINVAL;
1403 goto error_brelse;
1404 }
1405
1406 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001407 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001408 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001409 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001410 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001411 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001412 }
Chris Masona061fc82008-05-07 11:43:44 -04001413
1414 ret = btrfs_shrink_device(device, 0);
1415 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001416 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001417
Chris Masona061fc82008-05-07 11:43:44 -04001418 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1419 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001420 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001421
Josef Bacik2bf64752011-09-26 17:12:22 -04001422 spin_lock(&root->fs_info->free_chunk_lock);
1423 root->fs_info->free_chunk_space = device->total_bytes -
1424 device->bytes_used;
1425 spin_unlock(&root->fs_info->free_chunk_lock);
1426
Yan Zheng2b820322008-11-17 21:11:30 -05001427 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001428 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001429
1430 /*
1431 * the device list mutex makes sure that we don't change
1432 * the device list while someone else is writing out all
1433 * the device supers.
1434 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001435
1436 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001437 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001438 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001439
Yan Zhenge4404d62008-12-12 10:03:26 -05001440 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001441 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001442
Chris Masoncd02dca2010-12-13 14:56:23 -05001443 if (device->missing)
1444 root->fs_info->fs_devices->missing_devices--;
1445
Yan Zheng2b820322008-11-17 21:11:30 -05001446 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1447 struct btrfs_device, dev_list);
1448 if (device->bdev == root->fs_info->sb->s_bdev)
1449 root->fs_info->sb->s_bdev = next_device->bdev;
1450 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1451 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1452
Xiao Guangrong1f781602011-04-20 10:09:16 +00001453 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001454 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001455
1456 call_rcu(&device->rcu, free_device);
1457 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001458
David Sterba6c417612011-04-13 15:41:04 +02001459 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1460 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001461
Xiao Guangrong1f781602011-04-20 10:09:16 +00001462 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001463 struct btrfs_fs_devices *fs_devices;
1464 fs_devices = root->fs_info->fs_devices;
1465 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001466 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001467 break;
1468 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001469 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001470 fs_devices->seed = cur_devices->seed;
1471 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001472 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001473 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001474 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001475 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001476 }
1477
1478 /*
1479 * at this point, the device is zero sized. We want to
1480 * remove it from the devices list and zero out the old super
1481 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001482 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001483 /* make sure this device isn't detected as part of
1484 * the FS anymore
1485 */
1486 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1487 set_buffer_dirty(bh);
1488 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001489 }
Chris Masona061fc82008-05-07 11:43:44 -04001490
Chris Masona061fc82008-05-07 11:43:44 -04001491 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001492
1493error_brelse:
1494 brelse(bh);
1495error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001496 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001497 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001498out:
1499 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001500 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001501error_undo:
1502 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001503 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001504 list_add(&device->dev_alloc_list,
1505 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001506 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001507 root->fs_info->fs_devices->rw_devices++;
1508 }
1509 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001510}
1511
Yan Zheng2b820322008-11-17 21:11:30 -05001512/*
1513 * does all the dirty work required for changing file system's UUID.
1514 */
Li Zefan125ccb02011-12-08 15:07:24 +08001515static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001516{
1517 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1518 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001519 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001520 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001521 struct btrfs_device *device;
1522 u64 super_flags;
1523
1524 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001525 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001526 return -EINVAL;
1527
Yan Zhenge4404d62008-12-12 10:03:26 -05001528 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1529 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001530 return -ENOMEM;
1531
Yan Zhenge4404d62008-12-12 10:03:26 -05001532 old_devices = clone_fs_devices(fs_devices);
1533 if (IS_ERR(old_devices)) {
1534 kfree(seed_devices);
1535 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001536 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001537
Yan Zheng2b820322008-11-17 21:11:30 -05001538 list_add(&old_devices->list, &fs_uuids);
1539
Yan Zhenge4404d62008-12-12 10:03:26 -05001540 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1541 seed_devices->opened = 1;
1542 INIT_LIST_HEAD(&seed_devices->devices);
1543 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001544 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001545
1546 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001547 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1548 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001549 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1550
Yan Zhenge4404d62008-12-12 10:03:26 -05001551 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1552 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1553 device->fs_devices = seed_devices;
1554 }
1555
Yan Zheng2b820322008-11-17 21:11:30 -05001556 fs_devices->seeding = 0;
1557 fs_devices->num_devices = 0;
1558 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001559 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001560 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001561
1562 generate_random_uuid(fs_devices->fsid);
1563 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1564 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1565 super_flags = btrfs_super_flags(disk_super) &
1566 ~BTRFS_SUPER_FLAG_SEEDING;
1567 btrfs_set_super_flags(disk_super, super_flags);
1568
1569 return 0;
1570}
1571
1572/*
1573 * strore the expected generation for seed devices in device items.
1574 */
1575static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1576 struct btrfs_root *root)
1577{
1578 struct btrfs_path *path;
1579 struct extent_buffer *leaf;
1580 struct btrfs_dev_item *dev_item;
1581 struct btrfs_device *device;
1582 struct btrfs_key key;
1583 u8 fs_uuid[BTRFS_UUID_SIZE];
1584 u8 dev_uuid[BTRFS_UUID_SIZE];
1585 u64 devid;
1586 int ret;
1587
1588 path = btrfs_alloc_path();
1589 if (!path)
1590 return -ENOMEM;
1591
1592 root = root->fs_info->chunk_root;
1593 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1594 key.offset = 0;
1595 key.type = BTRFS_DEV_ITEM_KEY;
1596
1597 while (1) {
1598 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1599 if (ret < 0)
1600 goto error;
1601
1602 leaf = path->nodes[0];
1603next_slot:
1604 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1605 ret = btrfs_next_leaf(root, path);
1606 if (ret > 0)
1607 break;
1608 if (ret < 0)
1609 goto error;
1610 leaf = path->nodes[0];
1611 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001612 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001613 continue;
1614 }
1615
1616 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1617 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1618 key.type != BTRFS_DEV_ITEM_KEY)
1619 break;
1620
1621 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1622 struct btrfs_dev_item);
1623 devid = btrfs_device_id(leaf, dev_item);
1624 read_extent_buffer(leaf, dev_uuid,
1625 (unsigned long)btrfs_device_uuid(dev_item),
1626 BTRFS_UUID_SIZE);
1627 read_extent_buffer(leaf, fs_uuid,
1628 (unsigned long)btrfs_device_fsid(dev_item),
1629 BTRFS_UUID_SIZE);
1630 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001631 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001632
1633 if (device->fs_devices->seeding) {
1634 btrfs_set_device_generation(leaf, dev_item,
1635 device->generation);
1636 btrfs_mark_buffer_dirty(leaf);
1637 }
1638
1639 path->slots[0]++;
1640 goto next_slot;
1641 }
1642 ret = 0;
1643error:
1644 btrfs_free_path(path);
1645 return ret;
1646}
1647
Chris Mason788f20e2008-04-28 15:29:42 -04001648int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1649{
Josef Bacikd5e20032011-08-04 14:52:27 +00001650 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001651 struct btrfs_trans_handle *trans;
1652 struct btrfs_device *device;
1653 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001654 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001655 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001656 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001657 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001658 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001659 int ret = 0;
1660
Yan Zheng2b820322008-11-17 21:11:30 -05001661 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001662 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001663
Li Zefana5d16332011-12-07 20:08:40 -05001664 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001665 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001666 if (IS_ERR(bdev))
1667 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001668
Yan Zheng2b820322008-11-17 21:11:30 -05001669 if (root->fs_info->fs_devices->seeding) {
1670 seeding_dev = 1;
1671 down_write(&sb->s_umount);
1672 mutex_lock(&uuid_mutex);
1673 }
1674
Chris Mason8c8bee12008-09-29 11:19:10 -04001675 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001676
Chris Mason788f20e2008-04-28 15:29:42 -04001677 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001678 /*
1679 * we have the volume lock, so we don't need the extra
1680 * device list mutex while reading the list here.
1681 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001682 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001683 if (device->bdev == bdev) {
1684 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001685 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001686 }
1687 }
1688
1689 device = kzalloc(sizeof(*device), GFP_NOFS);
1690 if (!device) {
1691 /* we can safely leave the fs_devices entry around */
1692 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001693 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001694 }
1695
Josef Bacik606686e2012-06-04 14:03:51 -04001696 name = rcu_string_strdup(device_path, GFP_NOFS);
1697 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001698 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001699 ret = -ENOMEM;
1700 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001701 }
Josef Bacik606686e2012-06-04 14:03:51 -04001702 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001703
1704 ret = find_next_devid(root, &device->devid);
1705 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001706 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001707 kfree(device);
1708 goto error;
1709 }
1710
Yan, Zhenga22285a2010-05-16 10:48:46 -04001711 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001712 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001713 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001714 kfree(device);
1715 ret = PTR_ERR(trans);
1716 goto error;
1717 }
1718
Yan Zheng2b820322008-11-17 21:11:30 -05001719 lock_chunks(root);
1720
Josef Bacikd5e20032011-08-04 14:52:27 +00001721 q = bdev_get_queue(bdev);
1722 if (blk_queue_discard(q))
1723 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001724 device->writeable = 1;
1725 device->work.func = pending_bios_fn;
1726 generate_random_uuid(device->uuid);
1727 spin_lock_init(&device->io_lock);
1728 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001729 device->io_width = root->sectorsize;
1730 device->io_align = root->sectorsize;
1731 device->sector_size = root->sectorsize;
1732 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001733 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001734 device->dev_root = root->fs_info->dev_root;
1735 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001736 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001737 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001738 set_blocksize(device->bdev, 4096);
1739
Yan Zheng2b820322008-11-17 21:11:30 -05001740 if (seeding_dev) {
1741 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001742 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001743 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001744 }
1745
1746 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001747
1748 /*
1749 * we don't want write_supers to jump in here with our device
1750 * half setup
1751 */
1752 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001753 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001754 list_add(&device->dev_alloc_list,
1755 &root->fs_info->fs_devices->alloc_list);
1756 root->fs_info->fs_devices->num_devices++;
1757 root->fs_info->fs_devices->open_devices++;
1758 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04001759 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001760 if (device->can_discard)
1761 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001762 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1763
Josef Bacik2bf64752011-09-26 17:12:22 -04001764 spin_lock(&root->fs_info->free_chunk_lock);
1765 root->fs_info->free_chunk_space += device->total_bytes;
1766 spin_unlock(&root->fs_info->free_chunk_lock);
1767
Chris Masonc2898112009-06-10 09:51:32 -04001768 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1769 root->fs_info->fs_devices->rotating = 1;
1770
David Sterba6c417612011-04-13 15:41:04 +02001771 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1772 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001773 total_bytes + device->total_bytes);
1774
David Sterba6c417612011-04-13 15:41:04 +02001775 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1776 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001777 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001778 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001779
Yan Zheng2b820322008-11-17 21:11:30 -05001780 if (seeding_dev) {
1781 ret = init_first_rw_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001782 if (ret)
1783 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001784 ret = btrfs_finish_sprout(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001785 if (ret)
1786 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001787 } else {
1788 ret = btrfs_add_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001789 if (ret)
1790 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001791 }
1792
Chris Mason913d9522009-03-10 13:17:18 -04001793 /*
1794 * we've got more storage, clear any full flags on the space
1795 * infos
1796 */
1797 btrfs_clear_space_info_full(root->fs_info);
1798
Chris Mason7d9eb122008-07-08 14:19:17 -04001799 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001800 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001801
1802 if (seeding_dev) {
1803 mutex_unlock(&uuid_mutex);
1804 up_write(&sb->s_umount);
1805
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001806 if (ret) /* transaction commit */
1807 return ret;
1808
Yan Zheng2b820322008-11-17 21:11:30 -05001809 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001810 if (ret < 0)
1811 btrfs_error(root->fs_info, ret,
1812 "Failed to relocate sys chunks after "
1813 "device initialization. This can be fixed "
1814 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001815 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001816
Chris Mason788f20e2008-04-28 15:29:42 -04001817 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001818
1819error_trans:
1820 unlock_chunks(root);
1821 btrfs_abort_transaction(trans, root, ret);
1822 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04001823 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001824 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001825error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001826 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001827 if (seeding_dev) {
1828 mutex_unlock(&uuid_mutex);
1829 up_write(&sb->s_umount);
1830 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001831 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001832}
1833
Chris Masond3977122009-01-05 21:25:51 -05001834static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1835 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001836{
1837 int ret;
1838 struct btrfs_path *path;
1839 struct btrfs_root *root;
1840 struct btrfs_dev_item *dev_item;
1841 struct extent_buffer *leaf;
1842 struct btrfs_key key;
1843
1844 root = device->dev_root->fs_info->chunk_root;
1845
1846 path = btrfs_alloc_path();
1847 if (!path)
1848 return -ENOMEM;
1849
1850 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1851 key.type = BTRFS_DEV_ITEM_KEY;
1852 key.offset = device->devid;
1853
1854 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1855 if (ret < 0)
1856 goto out;
1857
1858 if (ret > 0) {
1859 ret = -ENOENT;
1860 goto out;
1861 }
1862
1863 leaf = path->nodes[0];
1864 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1865
1866 btrfs_set_device_id(leaf, dev_item, device->devid);
1867 btrfs_set_device_type(leaf, dev_item, device->type);
1868 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1869 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1870 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001871 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001872 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1873 btrfs_mark_buffer_dirty(leaf);
1874
1875out:
1876 btrfs_free_path(path);
1877 return ret;
1878}
1879
Chris Mason7d9eb122008-07-08 14:19:17 -04001880static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001881 struct btrfs_device *device, u64 new_size)
1882{
1883 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001884 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001885 u64 old_total = btrfs_super_total_bytes(super_copy);
1886 u64 diff = new_size - device->total_bytes;
1887
Yan Zheng2b820322008-11-17 21:11:30 -05001888 if (!device->writeable)
1889 return -EACCES;
1890 if (new_size <= device->total_bytes)
1891 return -EINVAL;
1892
Chris Mason8f18cf12008-04-25 16:53:30 -04001893 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001894 device->fs_devices->total_rw_bytes += diff;
1895
1896 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001897 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001898 btrfs_clear_space_info_full(device->dev_root->fs_info);
1899
Chris Mason8f18cf12008-04-25 16:53:30 -04001900 return btrfs_update_device(trans, device);
1901}
1902
Chris Mason7d9eb122008-07-08 14:19:17 -04001903int btrfs_grow_device(struct btrfs_trans_handle *trans,
1904 struct btrfs_device *device, u64 new_size)
1905{
1906 int ret;
1907 lock_chunks(device->dev_root);
1908 ret = __btrfs_grow_device(trans, device, new_size);
1909 unlock_chunks(device->dev_root);
1910 return ret;
1911}
1912
Chris Mason8f18cf12008-04-25 16:53:30 -04001913static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1914 struct btrfs_root *root,
1915 u64 chunk_tree, u64 chunk_objectid,
1916 u64 chunk_offset)
1917{
1918 int ret;
1919 struct btrfs_path *path;
1920 struct btrfs_key key;
1921
1922 root = root->fs_info->chunk_root;
1923 path = btrfs_alloc_path();
1924 if (!path)
1925 return -ENOMEM;
1926
1927 key.objectid = chunk_objectid;
1928 key.offset = chunk_offset;
1929 key.type = BTRFS_CHUNK_ITEM_KEY;
1930
1931 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001932 if (ret < 0)
1933 goto out;
1934 else if (ret > 0) { /* Logic error or corruption */
1935 btrfs_error(root->fs_info, -ENOENT,
1936 "Failed lookup while freeing chunk.");
1937 ret = -ENOENT;
1938 goto out;
1939 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001940
1941 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001942 if (ret < 0)
1943 btrfs_error(root->fs_info, ret,
1944 "Failed to delete chunk item.");
1945out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001946 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001947 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001948}
1949
Christoph Hellwigb2950862008-12-02 09:54:17 -05001950static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001951 chunk_offset)
1952{
David Sterba6c417612011-04-13 15:41:04 +02001953 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001954 struct btrfs_disk_key *disk_key;
1955 struct btrfs_chunk *chunk;
1956 u8 *ptr;
1957 int ret = 0;
1958 u32 num_stripes;
1959 u32 array_size;
1960 u32 len = 0;
1961 u32 cur;
1962 struct btrfs_key key;
1963
1964 array_size = btrfs_super_sys_array_size(super_copy);
1965
1966 ptr = super_copy->sys_chunk_array;
1967 cur = 0;
1968
1969 while (cur < array_size) {
1970 disk_key = (struct btrfs_disk_key *)ptr;
1971 btrfs_disk_key_to_cpu(&key, disk_key);
1972
1973 len = sizeof(*disk_key);
1974
1975 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1976 chunk = (struct btrfs_chunk *)(ptr + len);
1977 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1978 len += btrfs_chunk_item_size(num_stripes);
1979 } else {
1980 ret = -EIO;
1981 break;
1982 }
1983 if (key.objectid == chunk_objectid &&
1984 key.offset == chunk_offset) {
1985 memmove(ptr, ptr + len, array_size - (cur + len));
1986 array_size -= len;
1987 btrfs_set_super_sys_array_size(super_copy, array_size);
1988 } else {
1989 ptr += len;
1990 cur += len;
1991 }
1992 }
1993 return ret;
1994}
1995
Christoph Hellwigb2950862008-12-02 09:54:17 -05001996static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001997 u64 chunk_tree, u64 chunk_objectid,
1998 u64 chunk_offset)
1999{
2000 struct extent_map_tree *em_tree;
2001 struct btrfs_root *extent_root;
2002 struct btrfs_trans_handle *trans;
2003 struct extent_map *em;
2004 struct map_lookup *map;
2005 int ret;
2006 int i;
2007
2008 root = root->fs_info->chunk_root;
2009 extent_root = root->fs_info->extent_root;
2010 em_tree = &root->fs_info->mapping_tree.map_tree;
2011
Josef Bacikba1bf482009-09-11 16:11:19 -04002012 ret = btrfs_can_relocate(extent_root, chunk_offset);
2013 if (ret)
2014 return -ENOSPC;
2015
Chris Mason8f18cf12008-04-25 16:53:30 -04002016 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002017 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002018 if (ret)
2019 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002020
Yan, Zhenga22285a2010-05-16 10:48:46 -04002021 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002022 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002023
Chris Mason7d9eb122008-07-08 14:19:17 -04002024 lock_chunks(root);
2025
Chris Mason8f18cf12008-04-25 16:53:30 -04002026 /*
2027 * step two, delete the device extents and the
2028 * chunk tree entries
2029 */
Chris Mason890871b2009-09-02 16:24:52 -04002030 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002031 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002032 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002033
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002034 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002035 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002036 map = (struct map_lookup *)em->bdev;
2037
2038 for (i = 0; i < map->num_stripes; i++) {
2039 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2040 map->stripes[i].physical);
2041 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002042
Chris Masondfe25022008-05-13 13:46:40 -04002043 if (map->stripes[i].dev) {
2044 ret = btrfs_update_device(trans, map->stripes[i].dev);
2045 BUG_ON(ret);
2046 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002047 }
2048 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2049 chunk_offset);
2050
2051 BUG_ON(ret);
2052
liubo1abe9b82011-03-24 11:18:59 +00002053 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2054
Chris Mason8f18cf12008-04-25 16:53:30 -04002055 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2056 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2057 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002058 }
2059
Zheng Yan1a40e232008-09-26 10:09:34 -04002060 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2061 BUG_ON(ret);
2062
Chris Mason890871b2009-09-02 16:24:52 -04002063 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002064 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002065 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002066
Chris Mason8f18cf12008-04-25 16:53:30 -04002067 kfree(map);
2068 em->bdev = NULL;
2069
2070 /* once for the tree */
2071 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002072 /* once for us */
2073 free_extent_map(em);
2074
Chris Mason7d9eb122008-07-08 14:19:17 -04002075 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002076 btrfs_end_transaction(trans, root);
2077 return 0;
2078}
2079
Yan Zheng2b820322008-11-17 21:11:30 -05002080static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2081{
2082 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2083 struct btrfs_path *path;
2084 struct extent_buffer *leaf;
2085 struct btrfs_chunk *chunk;
2086 struct btrfs_key key;
2087 struct btrfs_key found_key;
2088 u64 chunk_tree = chunk_root->root_key.objectid;
2089 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002090 bool retried = false;
2091 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002092 int ret;
2093
2094 path = btrfs_alloc_path();
2095 if (!path)
2096 return -ENOMEM;
2097
Josef Bacikba1bf482009-09-11 16:11:19 -04002098again:
Yan Zheng2b820322008-11-17 21:11:30 -05002099 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2100 key.offset = (u64)-1;
2101 key.type = BTRFS_CHUNK_ITEM_KEY;
2102
2103 while (1) {
2104 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2105 if (ret < 0)
2106 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002107 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002108
2109 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2110 key.type);
2111 if (ret < 0)
2112 goto error;
2113 if (ret > 0)
2114 break;
2115
2116 leaf = path->nodes[0];
2117 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2118
2119 chunk = btrfs_item_ptr(leaf, path->slots[0],
2120 struct btrfs_chunk);
2121 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002122 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002123
2124 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2125 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2126 found_key.objectid,
2127 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002128 if (ret == -ENOSPC)
2129 failed++;
2130 else if (ret)
2131 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002132 }
2133
2134 if (found_key.offset == 0)
2135 break;
2136 key.offset = found_key.offset - 1;
2137 }
2138 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002139 if (failed && !retried) {
2140 failed = 0;
2141 retried = true;
2142 goto again;
2143 } else if (failed && retried) {
2144 WARN_ON(1);
2145 ret = -ENOSPC;
2146 }
Yan Zheng2b820322008-11-17 21:11:30 -05002147error:
2148 btrfs_free_path(path);
2149 return ret;
2150}
2151
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002152static int insert_balance_item(struct btrfs_root *root,
2153 struct btrfs_balance_control *bctl)
2154{
2155 struct btrfs_trans_handle *trans;
2156 struct btrfs_balance_item *item;
2157 struct btrfs_disk_balance_args disk_bargs;
2158 struct btrfs_path *path;
2159 struct extent_buffer *leaf;
2160 struct btrfs_key key;
2161 int ret, err;
2162
2163 path = btrfs_alloc_path();
2164 if (!path)
2165 return -ENOMEM;
2166
2167 trans = btrfs_start_transaction(root, 0);
2168 if (IS_ERR(trans)) {
2169 btrfs_free_path(path);
2170 return PTR_ERR(trans);
2171 }
2172
2173 key.objectid = BTRFS_BALANCE_OBJECTID;
2174 key.type = BTRFS_BALANCE_ITEM_KEY;
2175 key.offset = 0;
2176
2177 ret = btrfs_insert_empty_item(trans, root, path, &key,
2178 sizeof(*item));
2179 if (ret)
2180 goto out;
2181
2182 leaf = path->nodes[0];
2183 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2184
2185 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2186
2187 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2188 btrfs_set_balance_data(leaf, item, &disk_bargs);
2189 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2190 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2191 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2192 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2193
2194 btrfs_set_balance_flags(leaf, item, bctl->flags);
2195
2196 btrfs_mark_buffer_dirty(leaf);
2197out:
2198 btrfs_free_path(path);
2199 err = btrfs_commit_transaction(trans, root);
2200 if (err && !ret)
2201 ret = err;
2202 return ret;
2203}
2204
2205static int del_balance_item(struct btrfs_root *root)
2206{
2207 struct btrfs_trans_handle *trans;
2208 struct btrfs_path *path;
2209 struct btrfs_key key;
2210 int ret, err;
2211
2212 path = btrfs_alloc_path();
2213 if (!path)
2214 return -ENOMEM;
2215
2216 trans = btrfs_start_transaction(root, 0);
2217 if (IS_ERR(trans)) {
2218 btrfs_free_path(path);
2219 return PTR_ERR(trans);
2220 }
2221
2222 key.objectid = BTRFS_BALANCE_OBJECTID;
2223 key.type = BTRFS_BALANCE_ITEM_KEY;
2224 key.offset = 0;
2225
2226 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2227 if (ret < 0)
2228 goto out;
2229 if (ret > 0) {
2230 ret = -ENOENT;
2231 goto out;
2232 }
2233
2234 ret = btrfs_del_item(trans, root, path);
2235out:
2236 btrfs_free_path(path);
2237 err = btrfs_commit_transaction(trans, root);
2238 if (err && !ret)
2239 ret = err;
2240 return ret;
2241}
2242
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002243/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002244 * This is a heuristic used to reduce the number of chunks balanced on
2245 * resume after balance was interrupted.
2246 */
2247static void update_balance_args(struct btrfs_balance_control *bctl)
2248{
2249 /*
2250 * Turn on soft mode for chunk types that were being converted.
2251 */
2252 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2253 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2254 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2255 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2256 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2257 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2258
2259 /*
2260 * Turn on usage filter if is not already used. The idea is
2261 * that chunks that we have already balanced should be
2262 * reasonably full. Don't do it for chunks that are being
2263 * converted - that will keep us from relocating unconverted
2264 * (albeit full) chunks.
2265 */
2266 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2267 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2268 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2269 bctl->data.usage = 90;
2270 }
2271 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2272 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2273 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2274 bctl->sys.usage = 90;
2275 }
2276 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2277 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2278 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2279 bctl->meta.usage = 90;
2280 }
2281}
2282
2283/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002284 * Should be called with both balance and volume mutexes held to
2285 * serialize other volume operations (add_dev/rm_dev/resize) with
2286 * restriper. Same goes for unset_balance_control.
2287 */
2288static void set_balance_control(struct btrfs_balance_control *bctl)
2289{
2290 struct btrfs_fs_info *fs_info = bctl->fs_info;
2291
2292 BUG_ON(fs_info->balance_ctl);
2293
2294 spin_lock(&fs_info->balance_lock);
2295 fs_info->balance_ctl = bctl;
2296 spin_unlock(&fs_info->balance_lock);
2297}
2298
2299static void unset_balance_control(struct btrfs_fs_info *fs_info)
2300{
2301 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2302
2303 BUG_ON(!fs_info->balance_ctl);
2304
2305 spin_lock(&fs_info->balance_lock);
2306 fs_info->balance_ctl = NULL;
2307 spin_unlock(&fs_info->balance_lock);
2308
2309 kfree(bctl);
2310}
2311
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002312/*
2313 * Balance filters. Return 1 if chunk should be filtered out
2314 * (should not be balanced).
2315 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002316static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002317 struct btrfs_balance_args *bargs)
2318{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002319 chunk_type = chunk_to_extended(chunk_type) &
2320 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002321
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002322 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002323 return 0;
2324
2325 return 1;
2326}
2327
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002328static u64 div_factor_fine(u64 num, int factor)
2329{
2330 if (factor <= 0)
2331 return 0;
2332 if (factor >= 100)
2333 return num;
2334
2335 num *= factor;
2336 do_div(num, 100);
2337 return num;
2338}
2339
2340static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2341 struct btrfs_balance_args *bargs)
2342{
2343 struct btrfs_block_group_cache *cache;
2344 u64 chunk_used, user_thresh;
2345 int ret = 1;
2346
2347 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2348 chunk_used = btrfs_block_group_used(&cache->item);
2349
2350 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2351 if (chunk_used < user_thresh)
2352 ret = 0;
2353
2354 btrfs_put_block_group(cache);
2355 return ret;
2356}
2357
Ilya Dryomov409d4042012-01-16 22:04:47 +02002358static int chunk_devid_filter(struct extent_buffer *leaf,
2359 struct btrfs_chunk *chunk,
2360 struct btrfs_balance_args *bargs)
2361{
2362 struct btrfs_stripe *stripe;
2363 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2364 int i;
2365
2366 for (i = 0; i < num_stripes; i++) {
2367 stripe = btrfs_stripe_nr(chunk, i);
2368 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2369 return 0;
2370 }
2371
2372 return 1;
2373}
2374
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002375/* [pstart, pend) */
2376static int chunk_drange_filter(struct extent_buffer *leaf,
2377 struct btrfs_chunk *chunk,
2378 u64 chunk_offset,
2379 struct btrfs_balance_args *bargs)
2380{
2381 struct btrfs_stripe *stripe;
2382 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2383 u64 stripe_offset;
2384 u64 stripe_length;
2385 int factor;
2386 int i;
2387
2388 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2389 return 0;
2390
2391 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2392 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2393 factor = 2;
2394 else
2395 factor = 1;
2396 factor = num_stripes / factor;
2397
2398 for (i = 0; i < num_stripes; i++) {
2399 stripe = btrfs_stripe_nr(chunk, i);
2400 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2401 continue;
2402
2403 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2404 stripe_length = btrfs_chunk_length(leaf, chunk);
2405 do_div(stripe_length, factor);
2406
2407 if (stripe_offset < bargs->pend &&
2408 stripe_offset + stripe_length > bargs->pstart)
2409 return 0;
2410 }
2411
2412 return 1;
2413}
2414
Ilya Dryomovea671762012-01-16 22:04:48 +02002415/* [vstart, vend) */
2416static int chunk_vrange_filter(struct extent_buffer *leaf,
2417 struct btrfs_chunk *chunk,
2418 u64 chunk_offset,
2419 struct btrfs_balance_args *bargs)
2420{
2421 if (chunk_offset < bargs->vend &&
2422 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2423 /* at least part of the chunk is inside this vrange */
2424 return 0;
2425
2426 return 1;
2427}
2428
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002429static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002430 struct btrfs_balance_args *bargs)
2431{
2432 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2433 return 0;
2434
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002435 chunk_type = chunk_to_extended(chunk_type) &
2436 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002437
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002438 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002439 return 1;
2440
2441 return 0;
2442}
2443
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002444static int should_balance_chunk(struct btrfs_root *root,
2445 struct extent_buffer *leaf,
2446 struct btrfs_chunk *chunk, u64 chunk_offset)
2447{
2448 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2449 struct btrfs_balance_args *bargs = NULL;
2450 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2451
2452 /* type filter */
2453 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2454 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2455 return 0;
2456 }
2457
2458 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2459 bargs = &bctl->data;
2460 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2461 bargs = &bctl->sys;
2462 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2463 bargs = &bctl->meta;
2464
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002465 /* profiles filter */
2466 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2467 chunk_profiles_filter(chunk_type, bargs)) {
2468 return 0;
2469 }
2470
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002471 /* usage filter */
2472 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2473 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2474 return 0;
2475 }
2476
Ilya Dryomov409d4042012-01-16 22:04:47 +02002477 /* devid filter */
2478 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2479 chunk_devid_filter(leaf, chunk, bargs)) {
2480 return 0;
2481 }
2482
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002483 /* drange filter, makes sense only with devid filter */
2484 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2485 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2486 return 0;
2487 }
2488
Ilya Dryomovea671762012-01-16 22:04:48 +02002489 /* vrange filter */
2490 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2491 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2492 return 0;
2493 }
2494
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002495 /* soft profile changing mode */
2496 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2497 chunk_soft_convert_filter(chunk_type, bargs)) {
2498 return 0;
2499 }
2500
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002501 return 1;
2502}
2503
Chris Masonec44a352008-04-28 15:29:52 -04002504static u64 div_factor(u64 num, int factor)
2505{
2506 if (factor == 10)
2507 return num;
2508 num *= factor;
2509 do_div(num, 10);
2510 return num;
2511}
2512
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002513static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002514{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002515 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002516 struct btrfs_root *chunk_root = fs_info->chunk_root;
2517 struct btrfs_root *dev_root = fs_info->dev_root;
2518 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002519 struct btrfs_device *device;
2520 u64 old_size;
2521 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002522 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002523 struct btrfs_path *path;
2524 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002525 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002526 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002527 struct extent_buffer *leaf;
2528 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002529 int ret;
2530 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002531 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002532
Chris Masonec44a352008-04-28 15:29:52 -04002533 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002534 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002535 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002536 old_size = device->total_bytes;
2537 size_to_free = div_factor(old_size, 1);
2538 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002539 if (!device->writeable ||
2540 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002541 continue;
2542
2543 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002544 if (ret == -ENOSPC)
2545 break;
Chris Masonec44a352008-04-28 15:29:52 -04002546 BUG_ON(ret);
2547
Yan, Zhenga22285a2010-05-16 10:48:46 -04002548 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002549 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002550
2551 ret = btrfs_grow_device(trans, device, old_size);
2552 BUG_ON(ret);
2553
2554 btrfs_end_transaction(trans, dev_root);
2555 }
2556
2557 /* step two, relocate all the chunks */
2558 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002559 if (!path) {
2560 ret = -ENOMEM;
2561 goto error;
2562 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002563
2564 /* zero out stat counters */
2565 spin_lock(&fs_info->balance_lock);
2566 memset(&bctl->stat, 0, sizeof(bctl->stat));
2567 spin_unlock(&fs_info->balance_lock);
2568again:
Chris Masonec44a352008-04-28 15:29:52 -04002569 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2570 key.offset = (u64)-1;
2571 key.type = BTRFS_CHUNK_ITEM_KEY;
2572
Chris Masond3977122009-01-05 21:25:51 -05002573 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002574 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002575 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002576 ret = -ECANCELED;
2577 goto error;
2578 }
2579
Chris Masonec44a352008-04-28 15:29:52 -04002580 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2581 if (ret < 0)
2582 goto error;
2583
2584 /*
2585 * this shouldn't happen, it means the last relocate
2586 * failed
2587 */
2588 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002589 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002590
2591 ret = btrfs_previous_item(chunk_root, path, 0,
2592 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002593 if (ret) {
2594 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002595 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002596 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002597
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002598 leaf = path->nodes[0];
2599 slot = path->slots[0];
2600 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2601
Chris Masonec44a352008-04-28 15:29:52 -04002602 if (found_key.objectid != key.objectid)
2603 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002604
Chris Masonec44a352008-04-28 15:29:52 -04002605 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002606 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002607 break;
2608
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002609 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2610
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002611 if (!counting) {
2612 spin_lock(&fs_info->balance_lock);
2613 bctl->stat.considered++;
2614 spin_unlock(&fs_info->balance_lock);
2615 }
2616
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002617 ret = should_balance_chunk(chunk_root, leaf, chunk,
2618 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002619 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002620 if (!ret)
2621 goto loop;
2622
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002623 if (counting) {
2624 spin_lock(&fs_info->balance_lock);
2625 bctl->stat.expected++;
2626 spin_unlock(&fs_info->balance_lock);
2627 goto loop;
2628 }
2629
Chris Masonec44a352008-04-28 15:29:52 -04002630 ret = btrfs_relocate_chunk(chunk_root,
2631 chunk_root->root_key.objectid,
2632 found_key.objectid,
2633 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002634 if (ret && ret != -ENOSPC)
2635 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002636 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002637 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002638 } else {
2639 spin_lock(&fs_info->balance_lock);
2640 bctl->stat.completed++;
2641 spin_unlock(&fs_info->balance_lock);
2642 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002643loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002644 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002645 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002646
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002647 if (counting) {
2648 btrfs_release_path(path);
2649 counting = false;
2650 goto again;
2651 }
Chris Masonec44a352008-04-28 15:29:52 -04002652error:
2653 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002654 if (enospc_errors) {
2655 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2656 enospc_errors);
2657 if (!ret)
2658 ret = -ENOSPC;
2659 }
2660
Chris Masonec44a352008-04-28 15:29:52 -04002661 return ret;
2662}
2663
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002664/**
2665 * alloc_profile_is_valid - see if a given profile is valid and reduced
2666 * @flags: profile to validate
2667 * @extended: if true @flags is treated as an extended profile
2668 */
2669static int alloc_profile_is_valid(u64 flags, int extended)
2670{
2671 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2672 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2673
2674 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2675
2676 /* 1) check that all other bits are zeroed */
2677 if (flags & ~mask)
2678 return 0;
2679
2680 /* 2) see if profile is reduced */
2681 if (flags == 0)
2682 return !extended; /* "0" is valid for usual profiles */
2683
2684 /* true if exactly one bit set */
2685 return (flags & (flags - 1)) == 0;
2686}
2687
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002688static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2689{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002690 /* cancel requested || normal exit path */
2691 return atomic_read(&fs_info->balance_cancel_req) ||
2692 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2693 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002694}
2695
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002696static void __cancel_balance(struct btrfs_fs_info *fs_info)
2697{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002698 int ret;
2699
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002700 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002701 ret = del_balance_item(fs_info->tree_root);
2702 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002703}
2704
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002705void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002706 struct btrfs_ioctl_balance_args *bargs);
2707
2708/*
2709 * Should be called with both balance and volume mutexes held
2710 */
2711int btrfs_balance(struct btrfs_balance_control *bctl,
2712 struct btrfs_ioctl_balance_args *bargs)
2713{
2714 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002715 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002716 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002717 int ret;
2718
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002719 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002720 atomic_read(&fs_info->balance_pause_req) ||
2721 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002722 ret = -EINVAL;
2723 goto out;
2724 }
2725
Ilya Dryomove4837f82012-03-27 17:09:17 +03002726 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2727 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2728 mixed = 1;
2729
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002730 /*
2731 * In case of mixed groups both data and meta should be picked,
2732 * and identical options should be given for both of them.
2733 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002734 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2735 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002736 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2737 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2738 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2739 printk(KERN_ERR "btrfs: with mixed groups data and "
2740 "metadata balance options must be the same\n");
2741 ret = -EINVAL;
2742 goto out;
2743 }
2744 }
2745
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002746 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2747 if (fs_info->fs_devices->num_devices == 1)
2748 allowed |= BTRFS_BLOCK_GROUP_DUP;
2749 else if (fs_info->fs_devices->num_devices < 4)
2750 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2751 else
2752 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2753 BTRFS_BLOCK_GROUP_RAID10);
2754
Ilya Dryomov6728b192012-03-27 17:09:17 +03002755 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2756 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2757 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002758 printk(KERN_ERR "btrfs: unable to start balance with target "
2759 "data profile %llu\n",
2760 (unsigned long long)bctl->data.target);
2761 ret = -EINVAL;
2762 goto out;
2763 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002764 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2765 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2766 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002767 printk(KERN_ERR "btrfs: unable to start balance with target "
2768 "metadata profile %llu\n",
2769 (unsigned long long)bctl->meta.target);
2770 ret = -EINVAL;
2771 goto out;
2772 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002773 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2774 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2775 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002776 printk(KERN_ERR "btrfs: unable to start balance with target "
2777 "system profile %llu\n",
2778 (unsigned long long)bctl->sys.target);
2779 ret = -EINVAL;
2780 goto out;
2781 }
2782
Ilya Dryomove4837f82012-03-27 17:09:17 +03002783 /* allow dup'ed data chunks only in mixed mode */
2784 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002785 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002786 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2787 ret = -EINVAL;
2788 goto out;
2789 }
2790
2791 /* allow to reduce meta or sys integrity only if force set */
2792 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2793 BTRFS_BLOCK_GROUP_RAID10;
2794 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2795 (fs_info->avail_system_alloc_bits & allowed) &&
2796 !(bctl->sys.target & allowed)) ||
2797 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2798 (fs_info->avail_metadata_alloc_bits & allowed) &&
2799 !(bctl->meta.target & allowed))) {
2800 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2801 printk(KERN_INFO "btrfs: force reducing metadata "
2802 "integrity\n");
2803 } else {
2804 printk(KERN_ERR "btrfs: balance will reduce metadata "
2805 "integrity, use force if you want this\n");
2806 ret = -EINVAL;
2807 goto out;
2808 }
2809 }
2810
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002811 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002812 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002813 goto out;
2814
Ilya Dryomov59641012012-01-16 22:04:48 +02002815 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2816 BUG_ON(ret == -EEXIST);
2817 set_balance_control(bctl);
2818 } else {
2819 BUG_ON(ret != -EEXIST);
2820 spin_lock(&fs_info->balance_lock);
2821 update_balance_args(bctl);
2822 spin_unlock(&fs_info->balance_lock);
2823 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002824
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002825 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002826 mutex_unlock(&fs_info->balance_mutex);
2827
2828 ret = __btrfs_balance(fs_info);
2829
2830 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002831 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002832
2833 if (bargs) {
2834 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002835 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002836 }
2837
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002838 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2839 balance_need_close(fs_info)) {
2840 __cancel_balance(fs_info);
2841 }
2842
2843 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002844
2845 return ret;
2846out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002847 if (bctl->flags & BTRFS_BALANCE_RESUME)
2848 __cancel_balance(fs_info);
2849 else
2850 kfree(bctl);
2851 return ret;
2852}
2853
2854static int balance_kthread(void *data)
2855{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002856 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002857 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002858
2859 mutex_lock(&fs_info->volume_mutex);
2860 mutex_lock(&fs_info->balance_mutex);
2861
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002862 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002863 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002864 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002865 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002866
2867 mutex_unlock(&fs_info->balance_mutex);
2868 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002869
Ilya Dryomov59641012012-01-16 22:04:48 +02002870 return ret;
2871}
2872
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002873int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
2874{
2875 struct task_struct *tsk;
2876
2877 spin_lock(&fs_info->balance_lock);
2878 if (!fs_info->balance_ctl) {
2879 spin_unlock(&fs_info->balance_lock);
2880 return 0;
2881 }
2882 spin_unlock(&fs_info->balance_lock);
2883
2884 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2885 printk(KERN_INFO "btrfs: force skipping balance\n");
2886 return 0;
2887 }
2888
2889 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
2890 if (IS_ERR(tsk))
2891 return PTR_ERR(tsk);
2892
2893 return 0;
2894}
2895
Ilya Dryomov68310a52012-06-22 12:24:12 -06002896int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02002897{
Ilya Dryomov59641012012-01-16 22:04:48 +02002898 struct btrfs_balance_control *bctl;
2899 struct btrfs_balance_item *item;
2900 struct btrfs_disk_balance_args disk_bargs;
2901 struct btrfs_path *path;
2902 struct extent_buffer *leaf;
2903 struct btrfs_key key;
2904 int ret;
2905
2906 path = btrfs_alloc_path();
2907 if (!path)
2908 return -ENOMEM;
2909
Ilya Dryomov68310a52012-06-22 12:24:12 -06002910 key.objectid = BTRFS_BALANCE_OBJECTID;
2911 key.type = BTRFS_BALANCE_ITEM_KEY;
2912 key.offset = 0;
2913
2914 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2915 if (ret < 0)
2916 goto out;
2917 if (ret > 0) { /* ret = -ENOENT; */
2918 ret = 0;
2919 goto out;
2920 }
2921
Ilya Dryomov59641012012-01-16 22:04:48 +02002922 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2923 if (!bctl) {
2924 ret = -ENOMEM;
2925 goto out;
2926 }
2927
Ilya Dryomov59641012012-01-16 22:04:48 +02002928 leaf = path->nodes[0];
2929 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2930
Ilya Dryomov68310a52012-06-22 12:24:12 -06002931 bctl->fs_info = fs_info;
2932 bctl->flags = btrfs_balance_flags(leaf, item);
2933 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02002934
2935 btrfs_balance_data(leaf, item, &disk_bargs);
2936 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2937 btrfs_balance_meta(leaf, item, &disk_bargs);
2938 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2939 btrfs_balance_sys(leaf, item, &disk_bargs);
2940 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2941
Ilya Dryomov68310a52012-06-22 12:24:12 -06002942 mutex_lock(&fs_info->volume_mutex);
2943 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02002944
Ilya Dryomov68310a52012-06-22 12:24:12 -06002945 set_balance_control(bctl);
2946
2947 mutex_unlock(&fs_info->balance_mutex);
2948 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02002949out:
2950 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002951 return ret;
2952}
2953
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002954int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2955{
2956 int ret = 0;
2957
2958 mutex_lock(&fs_info->balance_mutex);
2959 if (!fs_info->balance_ctl) {
2960 mutex_unlock(&fs_info->balance_mutex);
2961 return -ENOTCONN;
2962 }
2963
2964 if (atomic_read(&fs_info->balance_running)) {
2965 atomic_inc(&fs_info->balance_pause_req);
2966 mutex_unlock(&fs_info->balance_mutex);
2967
2968 wait_event(fs_info->balance_wait_q,
2969 atomic_read(&fs_info->balance_running) == 0);
2970
2971 mutex_lock(&fs_info->balance_mutex);
2972 /* we are good with balance_ctl ripped off from under us */
2973 BUG_ON(atomic_read(&fs_info->balance_running));
2974 atomic_dec(&fs_info->balance_pause_req);
2975 } else {
2976 ret = -ENOTCONN;
2977 }
2978
2979 mutex_unlock(&fs_info->balance_mutex);
2980 return ret;
2981}
2982
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002983int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2984{
2985 mutex_lock(&fs_info->balance_mutex);
2986 if (!fs_info->balance_ctl) {
2987 mutex_unlock(&fs_info->balance_mutex);
2988 return -ENOTCONN;
2989 }
2990
2991 atomic_inc(&fs_info->balance_cancel_req);
2992 /*
2993 * if we are running just wait and return, balance item is
2994 * deleted in btrfs_balance in this case
2995 */
2996 if (atomic_read(&fs_info->balance_running)) {
2997 mutex_unlock(&fs_info->balance_mutex);
2998 wait_event(fs_info->balance_wait_q,
2999 atomic_read(&fs_info->balance_running) == 0);
3000 mutex_lock(&fs_info->balance_mutex);
3001 } else {
3002 /* __cancel_balance needs volume_mutex */
3003 mutex_unlock(&fs_info->balance_mutex);
3004 mutex_lock(&fs_info->volume_mutex);
3005 mutex_lock(&fs_info->balance_mutex);
3006
3007 if (fs_info->balance_ctl)
3008 __cancel_balance(fs_info);
3009
3010 mutex_unlock(&fs_info->volume_mutex);
3011 }
3012
3013 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3014 atomic_dec(&fs_info->balance_cancel_req);
3015 mutex_unlock(&fs_info->balance_mutex);
3016 return 0;
3017}
3018
Chris Mason8f18cf12008-04-25 16:53:30 -04003019/*
3020 * shrinking a device means finding all of the device extents past
3021 * the new size, and then following the back refs to the chunks.
3022 * The chunk relocation code actually frees the device extent
3023 */
3024int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3025{
3026 struct btrfs_trans_handle *trans;
3027 struct btrfs_root *root = device->dev_root;
3028 struct btrfs_dev_extent *dev_extent = NULL;
3029 struct btrfs_path *path;
3030 u64 length;
3031 u64 chunk_tree;
3032 u64 chunk_objectid;
3033 u64 chunk_offset;
3034 int ret;
3035 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003036 int failed = 0;
3037 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003038 struct extent_buffer *l;
3039 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003040 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003041 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003042 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003043 u64 diff = device->total_bytes - new_size;
3044
Yan Zheng2b820322008-11-17 21:11:30 -05003045 if (new_size >= device->total_bytes)
3046 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04003047
3048 path = btrfs_alloc_path();
3049 if (!path)
3050 return -ENOMEM;
3051
Chris Mason8f18cf12008-04-25 16:53:30 -04003052 path->reada = 2;
3053
Chris Mason7d9eb122008-07-08 14:19:17 -04003054 lock_chunks(root);
3055
Chris Mason8f18cf12008-04-25 16:53:30 -04003056 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003057 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003058 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003059 spin_lock(&root->fs_info->free_chunk_lock);
3060 root->fs_info->free_chunk_space -= diff;
3061 spin_unlock(&root->fs_info->free_chunk_lock);
3062 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003063 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003064
Josef Bacikba1bf482009-09-11 16:11:19 -04003065again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003066 key.objectid = device->devid;
3067 key.offset = (u64)-1;
3068 key.type = BTRFS_DEV_EXTENT_KEY;
3069
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003070 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003071 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3072 if (ret < 0)
3073 goto done;
3074
3075 ret = btrfs_previous_item(root, path, 0, key.type);
3076 if (ret < 0)
3077 goto done;
3078 if (ret) {
3079 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003080 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003081 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003082 }
3083
3084 l = path->nodes[0];
3085 slot = path->slots[0];
3086 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3087
Josef Bacikba1bf482009-09-11 16:11:19 -04003088 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003089 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003090 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003091 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003092
3093 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3094 length = btrfs_dev_extent_length(l, dev_extent);
3095
Josef Bacikba1bf482009-09-11 16:11:19 -04003096 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003097 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003098 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003099 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003100
3101 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3102 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3103 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003104 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003105
3106 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3107 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003108 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003109 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003110 if (ret == -ENOSPC)
3111 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003112 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003113
3114 if (failed && !retried) {
3115 failed = 0;
3116 retried = true;
3117 goto again;
3118 } else if (failed && retried) {
3119 ret = -ENOSPC;
3120 lock_chunks(root);
3121
3122 device->total_bytes = old_size;
3123 if (device->writeable)
3124 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003125 spin_lock(&root->fs_info->free_chunk_lock);
3126 root->fs_info->free_chunk_space += diff;
3127 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003128 unlock_chunks(root);
3129 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003130 }
3131
Chris Balld6397ba2009-04-27 07:29:03 -04003132 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003133 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003134 if (IS_ERR(trans)) {
3135 ret = PTR_ERR(trans);
3136 goto done;
3137 }
3138
Chris Balld6397ba2009-04-27 07:29:03 -04003139 lock_chunks(root);
3140
3141 device->disk_total_bytes = new_size;
3142 /* Now btrfs_update_device() will change the on-disk size. */
3143 ret = btrfs_update_device(trans, device);
3144 if (ret) {
3145 unlock_chunks(root);
3146 btrfs_end_transaction(trans, root);
3147 goto done;
3148 }
3149 WARN_ON(diff > old_total);
3150 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3151 unlock_chunks(root);
3152 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003153done:
3154 btrfs_free_path(path);
3155 return ret;
3156}
3157
Li Zefan125ccb02011-12-08 15:07:24 +08003158static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003159 struct btrfs_key *key,
3160 struct btrfs_chunk *chunk, int item_size)
3161{
David Sterba6c417612011-04-13 15:41:04 +02003162 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003163 struct btrfs_disk_key disk_key;
3164 u32 array_size;
3165 u8 *ptr;
3166
3167 array_size = btrfs_super_sys_array_size(super_copy);
3168 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3169 return -EFBIG;
3170
3171 ptr = super_copy->sys_chunk_array + array_size;
3172 btrfs_cpu_key_to_disk(&disk_key, key);
3173 memcpy(ptr, &disk_key, sizeof(disk_key));
3174 ptr += sizeof(disk_key);
3175 memcpy(ptr, chunk, item_size);
3176 item_size += sizeof(disk_key);
3177 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3178 return 0;
3179}
3180
Miao Xieb2117a32011-01-05 10:07:28 +00003181/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003182 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003183 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003184static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003185{
Arne Jansen73c5de02011-04-12 12:07:57 +02003186 const struct btrfs_device_info *di_a = a;
3187 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003188
Arne Jansen73c5de02011-04-12 12:07:57 +02003189 if (di_a->max_avail > di_b->max_avail)
3190 return -1;
3191 if (di_a->max_avail < di_b->max_avail)
3192 return 1;
3193 if (di_a->total_avail > di_b->total_avail)
3194 return -1;
3195 if (di_a->total_avail < di_b->total_avail)
3196 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003197 return 0;
3198}
3199
3200static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3201 struct btrfs_root *extent_root,
3202 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003203 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003204 u64 start, u64 type)
3205{
3206 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003207 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3208 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003209 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003210 struct extent_map_tree *em_tree;
3211 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003212 struct btrfs_device_info *devices_info = NULL;
3213 u64 total_avail;
3214 int num_stripes; /* total number of stripes to allocate */
3215 int sub_stripes; /* sub_stripes info for map */
3216 int dev_stripes; /* stripes per dev */
3217 int devs_max; /* max devs to use */
3218 int devs_min; /* min devs needed */
3219 int devs_increment; /* ndevs has to be a multiple of this */
3220 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003221 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003222 u64 max_stripe_size;
3223 u64 max_chunk_size;
3224 u64 stripe_size;
3225 u64 num_bytes;
3226 int ndevs;
3227 int i;
3228 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003229
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003230 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003231
Miao Xieb2117a32011-01-05 10:07:28 +00003232 if (list_empty(&fs_devices->alloc_list))
3233 return -ENOSPC;
3234
Arne Jansen73c5de02011-04-12 12:07:57 +02003235 sub_stripes = 1;
3236 dev_stripes = 1;
3237 devs_increment = 1;
3238 ncopies = 1;
3239 devs_max = 0; /* 0 == as many as possible */
3240 devs_min = 1;
3241
3242 /*
3243 * define the properties of each RAID type.
3244 * FIXME: move this to a global table and use it in all RAID
3245 * calculation code
3246 */
3247 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3248 dev_stripes = 2;
3249 ncopies = 2;
3250 devs_max = 1;
3251 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3252 devs_min = 2;
3253 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3254 devs_increment = 2;
3255 ncopies = 2;
3256 devs_max = 2;
3257 devs_min = 2;
3258 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3259 sub_stripes = 2;
3260 devs_increment = 2;
3261 ncopies = 2;
3262 devs_min = 4;
3263 } else {
3264 devs_max = 1;
3265 }
3266
3267 if (type & BTRFS_BLOCK_GROUP_DATA) {
3268 max_stripe_size = 1024 * 1024 * 1024;
3269 max_chunk_size = 10 * max_stripe_size;
3270 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003271 /* for larger filesystems, use larger metadata chunks */
3272 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3273 max_stripe_size = 1024 * 1024 * 1024;
3274 else
3275 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003276 max_chunk_size = max_stripe_size;
3277 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003278 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003279 max_chunk_size = 2 * max_stripe_size;
3280 } else {
3281 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3282 type);
3283 BUG_ON(1);
3284 }
3285
3286 /* we don't want a chunk larger than 10% of writeable space */
3287 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3288 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003289
3290 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3291 GFP_NOFS);
3292 if (!devices_info)
3293 return -ENOMEM;
3294
Arne Jansen73c5de02011-04-12 12:07:57 +02003295 cur = fs_devices->alloc_list.next;
3296
3297 /*
3298 * in the first pass through the devices list, we gather information
3299 * about the available holes on each device.
3300 */
3301 ndevs = 0;
3302 while (cur != &fs_devices->alloc_list) {
3303 struct btrfs_device *device;
3304 u64 max_avail;
3305 u64 dev_offset;
3306
3307 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3308
3309 cur = cur->next;
3310
3311 if (!device->writeable) {
3312 printk(KERN_ERR
3313 "btrfs: read-only device in alloc_list\n");
3314 WARN_ON(1);
3315 continue;
3316 }
3317
3318 if (!device->in_fs_metadata)
3319 continue;
3320
3321 if (device->total_bytes > device->bytes_used)
3322 total_avail = device->total_bytes - device->bytes_used;
3323 else
3324 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003325
3326 /* If there is no space on this device, skip it. */
3327 if (total_avail == 0)
3328 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003329
Li Zefan125ccb02011-12-08 15:07:24 +08003330 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003331 max_stripe_size * dev_stripes,
3332 &dev_offset, &max_avail);
3333 if (ret && ret != -ENOSPC)
3334 goto error;
3335
3336 if (ret == 0)
3337 max_avail = max_stripe_size * dev_stripes;
3338
3339 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3340 continue;
3341
3342 devices_info[ndevs].dev_offset = dev_offset;
3343 devices_info[ndevs].max_avail = max_avail;
3344 devices_info[ndevs].total_avail = total_avail;
3345 devices_info[ndevs].dev = device;
3346 ++ndevs;
3347 }
3348
3349 /*
3350 * now sort the devices by hole size / available space
3351 */
3352 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3353 btrfs_cmp_device_info, NULL);
3354
3355 /* round down to number of usable stripes */
3356 ndevs -= ndevs % devs_increment;
3357
3358 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3359 ret = -ENOSPC;
3360 goto error;
3361 }
3362
3363 if (devs_max && ndevs > devs_max)
3364 ndevs = devs_max;
3365 /*
3366 * the primary goal is to maximize the number of stripes, so use as many
3367 * devices as possible, even if the stripes are not maximum sized.
3368 */
3369 stripe_size = devices_info[ndevs-1].max_avail;
3370 num_stripes = ndevs * dev_stripes;
3371
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003372 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003373 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003374 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003375 }
3376
3377 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003378
3379 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003380 do_div(stripe_size, BTRFS_STRIPE_LEN);
3381 stripe_size *= BTRFS_STRIPE_LEN;
3382
Miao Xieb2117a32011-01-05 10:07:28 +00003383 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3384 if (!map) {
3385 ret = -ENOMEM;
3386 goto error;
3387 }
3388 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003389
Arne Jansen73c5de02011-04-12 12:07:57 +02003390 for (i = 0; i < ndevs; ++i) {
3391 for (j = 0; j < dev_stripes; ++j) {
3392 int s = i * dev_stripes + j;
3393 map->stripes[s].dev = devices_info[i].dev;
3394 map->stripes[s].physical = devices_info[i].dev_offset +
3395 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003396 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003397 }
Chris Mason593060d2008-03-25 16:50:33 -04003398 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003399 map->stripe_len = BTRFS_STRIPE_LEN;
3400 map->io_align = BTRFS_STRIPE_LEN;
3401 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003402 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003403 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003404
Yan Zheng2b820322008-11-17 21:11:30 -05003405 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003406 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003407
Arne Jansen73c5de02011-04-12 12:07:57 +02003408 *stripe_size_out = stripe_size;
3409 *num_bytes_out = num_bytes;
3410
3411 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003412
David Sterba172ddd62011-04-21 00:48:27 +02003413 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003414 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003415 ret = -ENOMEM;
3416 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003417 }
Chris Mason0b86a832008-03-24 15:01:56 -04003418 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003419 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003420 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003421 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003422 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003423
Chris Mason0b86a832008-03-24 15:01:56 -04003424 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003425 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003426 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003427 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003428 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003429 if (ret)
3430 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003431
3432 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3433 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003434 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003435 if (ret)
3436 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003437
Arne Jansen73c5de02011-04-12 12:07:57 +02003438 for (i = 0; i < map->num_stripes; ++i) {
3439 struct btrfs_device *device;
3440 u64 dev_offset;
3441
3442 device = map->stripes[i].dev;
3443 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003444
3445 ret = btrfs_alloc_dev_extent(trans, device,
3446 info->chunk_root->root_key.objectid,
3447 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003448 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003449 if (ret) {
3450 btrfs_abort_transaction(trans, extent_root, ret);
3451 goto error;
3452 }
Yan Zheng2b820322008-11-17 21:11:30 -05003453 }
3454
Miao Xieb2117a32011-01-05 10:07:28 +00003455 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003456 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003457
3458error:
3459 kfree(map);
3460 kfree(devices_info);
3461 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003462}
3463
3464static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3465 struct btrfs_root *extent_root,
3466 struct map_lookup *map, u64 chunk_offset,
3467 u64 chunk_size, u64 stripe_size)
3468{
3469 u64 dev_offset;
3470 struct btrfs_key key;
3471 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3472 struct btrfs_device *device;
3473 struct btrfs_chunk *chunk;
3474 struct btrfs_stripe *stripe;
3475 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3476 int index = 0;
3477 int ret;
3478
3479 chunk = kzalloc(item_size, GFP_NOFS);
3480 if (!chunk)
3481 return -ENOMEM;
3482
3483 index = 0;
3484 while (index < map->num_stripes) {
3485 device = map->stripes[index].dev;
3486 device->bytes_used += stripe_size;
3487 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003488 if (ret)
3489 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003490 index++;
3491 }
3492
Josef Bacik2bf64752011-09-26 17:12:22 -04003493 spin_lock(&extent_root->fs_info->free_chunk_lock);
3494 extent_root->fs_info->free_chunk_space -= (stripe_size *
3495 map->num_stripes);
3496 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3497
Yan Zheng2b820322008-11-17 21:11:30 -05003498 index = 0;
3499 stripe = &chunk->stripe;
3500 while (index < map->num_stripes) {
3501 device = map->stripes[index].dev;
3502 dev_offset = map->stripes[index].physical;
3503
3504 btrfs_set_stack_stripe_devid(stripe, device->devid);
3505 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3506 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3507 stripe++;
3508 index++;
3509 }
3510
3511 btrfs_set_stack_chunk_length(chunk, chunk_size);
3512 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3513 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3514 btrfs_set_stack_chunk_type(chunk, map->type);
3515 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3516 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3517 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3518 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3519 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3520
3521 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3522 key.type = BTRFS_CHUNK_ITEM_KEY;
3523 key.offset = chunk_offset;
3524
3525 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003526
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003527 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3528 /*
3529 * TODO: Cleanup of inserted chunk root in case of
3530 * failure.
3531 */
Li Zefan125ccb02011-12-08 15:07:24 +08003532 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003533 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003534 }
liubo1abe9b82011-03-24 11:18:59 +00003535
Mark Fasheh3acd3952011-09-08 17:40:01 -07003536out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003537 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003538 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003539}
3540
3541/*
3542 * Chunk allocation falls into two parts. The first part does works
3543 * that make the new allocated chunk useable, but not do any operation
3544 * that modifies the chunk tree. The second part does the works that
3545 * require modifying the chunk tree. This division is important for the
3546 * bootstrap process of adding storage to a seed btrfs.
3547 */
3548int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3549 struct btrfs_root *extent_root, u64 type)
3550{
3551 u64 chunk_offset;
3552 u64 chunk_size;
3553 u64 stripe_size;
3554 struct map_lookup *map;
3555 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3556 int ret;
3557
3558 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3559 &chunk_offset);
3560 if (ret)
3561 return ret;
3562
3563 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3564 &stripe_size, chunk_offset, type);
3565 if (ret)
3566 return ret;
3567
3568 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3569 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003570 if (ret)
3571 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003572 return 0;
3573}
3574
Chris Masond3977122009-01-05 21:25:51 -05003575static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003576 struct btrfs_root *root,
3577 struct btrfs_device *device)
3578{
3579 u64 chunk_offset;
3580 u64 sys_chunk_offset;
3581 u64 chunk_size;
3582 u64 sys_chunk_size;
3583 u64 stripe_size;
3584 u64 sys_stripe_size;
3585 u64 alloc_profile;
3586 struct map_lookup *map;
3587 struct map_lookup *sys_map;
3588 struct btrfs_fs_info *fs_info = root->fs_info;
3589 struct btrfs_root *extent_root = fs_info->extent_root;
3590 int ret;
3591
3592 ret = find_next_chunk(fs_info->chunk_root,
3593 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003594 if (ret)
3595 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003596
3597 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003598 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003599 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3600
3601 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3602 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003603 if (ret)
3604 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003605
3606 sys_chunk_offset = chunk_offset + chunk_size;
3607
3608 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003609 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003610 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3611
3612 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3613 &sys_chunk_size, &sys_stripe_size,
3614 sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003615 if (ret)
3616 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003617
3618 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003619 if (ret)
3620 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003621
3622 /*
3623 * Modifying chunk tree needs allocating new blocks from both
3624 * system block group and metadata block group. So we only can
3625 * do operations require modifying the chunk tree after both
3626 * block groups were created.
3627 */
3628 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3629 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003630 if (ret)
3631 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003632
3633 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3634 sys_chunk_offset, sys_chunk_size,
3635 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003636 if (ret)
3637 goto abort;
3638
Yan Zheng2b820322008-11-17 21:11:30 -05003639 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003640
3641abort:
3642 btrfs_abort_transaction(trans, root, ret);
3643 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003644}
3645
3646int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3647{
3648 struct extent_map *em;
3649 struct map_lookup *map;
3650 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3651 int readonly = 0;
3652 int i;
3653
Chris Mason890871b2009-09-02 16:24:52 -04003654 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003655 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003656 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003657 if (!em)
3658 return 1;
3659
Josef Bacikf48b9072010-01-27 02:07:59 +00003660 if (btrfs_test_opt(root, DEGRADED)) {
3661 free_extent_map(em);
3662 return 0;
3663 }
3664
Yan Zheng2b820322008-11-17 21:11:30 -05003665 map = (struct map_lookup *)em->bdev;
3666 for (i = 0; i < map->num_stripes; i++) {
3667 if (!map->stripes[i].dev->writeable) {
3668 readonly = 1;
3669 break;
3670 }
3671 }
3672 free_extent_map(em);
3673 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003674}
3675
3676void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3677{
David Sterbaa8067e02011-04-21 00:34:43 +02003678 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003679}
3680
3681void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3682{
3683 struct extent_map *em;
3684
Chris Masond3977122009-01-05 21:25:51 -05003685 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003686 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003687 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3688 if (em)
3689 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003690 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003691 if (!em)
3692 break;
3693 kfree(em->bdev);
3694 /* once for us */
3695 free_extent_map(em);
3696 /* once for the tree */
3697 free_extent_map(em);
3698 }
3699}
3700
Chris Masonf1885912008-04-09 16:28:12 -04003701int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3702{
3703 struct extent_map *em;
3704 struct map_lookup *map;
3705 struct extent_map_tree *em_tree = &map_tree->map_tree;
3706 int ret;
3707
Chris Mason890871b2009-09-02 16:24:52 -04003708 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003709 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003710 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003711 BUG_ON(!em);
3712
3713 BUG_ON(em->start > logical || em->start + em->len < logical);
3714 map = (struct map_lookup *)em->bdev;
3715 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3716 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003717 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3718 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003719 else
3720 ret = 1;
3721 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003722 return ret;
3723}
3724
Chris Masondfe25022008-05-13 13:46:40 -04003725static int find_live_mirror(struct map_lookup *map, int first, int num,
3726 int optimal)
3727{
3728 int i;
3729 if (map->stripes[optimal].dev->bdev)
3730 return optimal;
3731 for (i = first; i < first + num; i++) {
3732 if (map->stripes[i].dev->bdev)
3733 return i;
3734 }
3735 /* we couldn't find one that doesn't fail. Just return something
3736 * and the io error handling code will clean up eventually
3737 */
3738 return optimal;
3739}
3740
Chris Masonf2d8d742008-04-21 10:03:05 -04003741static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3742 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003743 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003744 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003745{
3746 struct extent_map *em;
3747 struct map_lookup *map;
3748 struct extent_map_tree *em_tree = &map_tree->map_tree;
3749 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003750 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003751 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003752 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003753 u64 stripe_nr_orig;
3754 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003755 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003756 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003757 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003758 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003759 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003760 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003761
Chris Mason890871b2009-09-02 16:24:52 -04003762 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003763 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003764 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003765
Chris Mason3b951512008-04-17 11:29:12 -04003766 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003767 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3768 (unsigned long long)logical,
3769 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003770 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003771 }
Chris Mason0b86a832008-03-24 15:01:56 -04003772
3773 BUG_ON(em->start > logical || em->start + em->len < logical);
3774 map = (struct map_lookup *)em->bdev;
3775 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003776
Chris Masonf1885912008-04-09 16:28:12 -04003777 if (mirror_num > map->num_stripes)
3778 mirror_num = 0;
3779
Chris Mason593060d2008-03-25 16:50:33 -04003780 stripe_nr = offset;
3781 /*
3782 * stripe_nr counts the total number of stripes we have to stride
3783 * to get to this block
3784 */
3785 do_div(stripe_nr, map->stripe_len);
3786
3787 stripe_offset = stripe_nr * map->stripe_len;
3788 BUG_ON(offset < stripe_offset);
3789
3790 /* stripe_offset is the offset of this block in its stripe*/
3791 stripe_offset = offset - stripe_offset;
3792
Li Dongyangfce3bb92011-03-24 10:24:26 +00003793 if (rw & REQ_DISCARD)
3794 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003795 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003796 /* we limit the length of each bio to what fits in a stripe */
3797 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003798 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003799 } else {
3800 *length = em->len - offset;
3801 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003802
Jan Schmidta1d3c472011-08-04 17:15:33 +02003803 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003804 goto out;
3805
Chris Masonf2d8d742008-04-21 10:03:05 -04003806 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003807 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003808 stripe_nr_orig = stripe_nr;
3809 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3810 (~(map->stripe_len - 1));
3811 do_div(stripe_nr_end, map->stripe_len);
3812 stripe_end_offset = stripe_nr_end * map->stripe_len -
3813 (offset + *length);
3814 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3815 if (rw & REQ_DISCARD)
3816 num_stripes = min_t(u64, map->num_stripes,
3817 stripe_nr_end - stripe_nr_orig);
3818 stripe_index = do_div(stripe_nr, map->num_stripes);
3819 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003820 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003821 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003822 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003823 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003824 else {
3825 stripe_index = find_live_mirror(map, 0,
3826 map->num_stripes,
3827 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003828 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003829 }
Chris Mason2fff7342008-04-29 14:12:09 -04003830
Chris Mason611f0e02008-04-03 16:29:03 -04003831 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003832 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003833 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003834 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003835 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003836 } else {
3837 mirror_num = 1;
3838 }
Chris Mason2fff7342008-04-29 14:12:09 -04003839
Chris Mason321aecc2008-04-16 10:49:51 -04003840 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3841 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003842
3843 stripe_index = do_div(stripe_nr, factor);
3844 stripe_index *= map->sub_stripes;
3845
Jens Axboe7eaceac2011-03-10 08:52:07 +01003846 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003847 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003848 else if (rw & REQ_DISCARD)
3849 num_stripes = min_t(u64, map->sub_stripes *
3850 (stripe_nr_end - stripe_nr_orig),
3851 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003852 else if (mirror_num)
3853 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003854 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04003855 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04003856 stripe_index = find_live_mirror(map, stripe_index,
3857 map->sub_stripes, stripe_index +
3858 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04003859 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003860 }
Chris Mason8790d502008-04-03 16:29:03 -04003861 } else {
3862 /*
3863 * after this do_div call, stripe_nr is the number of stripes
3864 * on this device we have to walk to find the data, and
3865 * stripe_index is the number of our device in the stripe array
3866 */
3867 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003868 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003869 }
Chris Mason593060d2008-03-25 16:50:33 -04003870 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003871
Li Zefande11cc12011-12-01 12:55:47 +08003872 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3873 if (!bbio) {
3874 ret = -ENOMEM;
3875 goto out;
3876 }
3877 atomic_set(&bbio->error, 0);
3878
Li Dongyangfce3bb92011-03-24 10:24:26 +00003879 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003880 int factor = 0;
3881 int sub_stripes = 0;
3882 u64 stripes_per_dev = 0;
3883 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003884 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003885
3886 if (map->type &
3887 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3888 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3889 sub_stripes = 1;
3890 else
3891 sub_stripes = map->sub_stripes;
3892
3893 factor = map->num_stripes / sub_stripes;
3894 stripes_per_dev = div_u64_rem(stripe_nr_end -
3895 stripe_nr_orig,
3896 factor,
3897 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04003898 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3899 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003900 }
3901
Li Dongyangfce3bb92011-03-24 10:24:26 +00003902 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003903 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003904 map->stripes[stripe_index].physical +
3905 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003906 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003907
Li Zefanec9ef7a2011-12-01 14:06:42 +08003908 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3909 BTRFS_BLOCK_GROUP_RAID10)) {
3910 bbio->stripes[i].length = stripes_per_dev *
3911 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003912
Li Zefanec9ef7a2011-12-01 14:06:42 +08003913 if (i / sub_stripes < remaining_stripes)
3914 bbio->stripes[i].length +=
3915 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003916
3917 /*
3918 * Special for the first stripe and
3919 * the last stripe:
3920 *
3921 * |-------|...|-------|
3922 * |----------|
3923 * off end_off
3924 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08003925 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003926 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003927 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003928
3929 if (stripe_index >= last_stripe &&
3930 stripe_index <= (last_stripe +
3931 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08003932 bbio->stripes[i].length -=
3933 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003934
Li Zefanec9ef7a2011-12-01 14:06:42 +08003935 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003936 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003937 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003938 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003939
3940 stripe_index++;
3941 if (stripe_index == map->num_stripes) {
3942 /* This could only happen for RAID0/10 */
3943 stripe_index = 0;
3944 stripe_nr++;
3945 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003946 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003947 } else {
3948 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003949 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003950 map->stripes[stripe_index].physical +
3951 stripe_offset +
3952 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003953 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003954 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003955 stripe_index++;
3956 }
Chris Mason593060d2008-03-25 16:50:33 -04003957 }
Li Zefande11cc12011-12-01 12:55:47 +08003958
3959 if (rw & REQ_WRITE) {
3960 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3961 BTRFS_BLOCK_GROUP_RAID10 |
3962 BTRFS_BLOCK_GROUP_DUP)) {
3963 max_errors = 1;
3964 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003965 }
Li Zefande11cc12011-12-01 12:55:47 +08003966
3967 *bbio_ret = bbio;
3968 bbio->num_stripes = num_stripes;
3969 bbio->max_errors = max_errors;
3970 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003971out:
Chris Mason0b86a832008-03-24 15:01:56 -04003972 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003973 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003974}
3975
Chris Masonf2d8d742008-04-21 10:03:05 -04003976int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3977 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003978 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003979{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003980 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003981 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003982}
3983
Yan Zhenga512bbf2008-12-08 16:46:26 -05003984int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3985 u64 chunk_start, u64 physical, u64 devid,
3986 u64 **logical, int *naddrs, int *stripe_len)
3987{
3988 struct extent_map_tree *em_tree = &map_tree->map_tree;
3989 struct extent_map *em;
3990 struct map_lookup *map;
3991 u64 *buf;
3992 u64 bytenr;
3993 u64 length;
3994 u64 stripe_nr;
3995 int i, j, nr = 0;
3996
Chris Mason890871b2009-09-02 16:24:52 -04003997 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003998 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003999 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004000
4001 BUG_ON(!em || em->start != chunk_start);
4002 map = (struct map_lookup *)em->bdev;
4003
4004 length = em->len;
4005 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4006 do_div(length, map->num_stripes / map->sub_stripes);
4007 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4008 do_div(length, map->num_stripes);
4009
4010 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004011 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004012
4013 for (i = 0; i < map->num_stripes; i++) {
4014 if (devid && map->stripes[i].dev->devid != devid)
4015 continue;
4016 if (map->stripes[i].physical > physical ||
4017 map->stripes[i].physical + length <= physical)
4018 continue;
4019
4020 stripe_nr = physical - map->stripes[i].physical;
4021 do_div(stripe_nr, map->stripe_len);
4022
4023 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4024 stripe_nr = stripe_nr * map->num_stripes + i;
4025 do_div(stripe_nr, map->sub_stripes);
4026 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4027 stripe_nr = stripe_nr * map->num_stripes + i;
4028 }
4029 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05004030 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004031 for (j = 0; j < nr; j++) {
4032 if (buf[j] == bytenr)
4033 break;
4034 }
Chris Mason934d3752008-12-08 16:43:10 -05004035 if (j == nr) {
4036 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004037 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004038 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004039 }
4040
Yan Zhenga512bbf2008-12-08 16:46:26 -05004041 *logical = buf;
4042 *naddrs = nr;
4043 *stripe_len = map->stripe_len;
4044
4045 free_extent_map(em);
4046 return 0;
4047}
4048
Stefan Behrens442a4f62012-05-25 16:06:08 +02004049static void *merge_stripe_index_into_bio_private(void *bi_private,
4050 unsigned int stripe_index)
4051{
4052 /*
4053 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4054 * at most 1.
4055 * The alternative solution (instead of stealing bits from the
4056 * pointer) would be to allocate an intermediate structure
4057 * that contains the old private pointer plus the stripe_index.
4058 */
4059 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4060 BUG_ON(stripe_index > 3);
4061 return (void *)(((uintptr_t)bi_private) | stripe_index);
4062}
4063
4064static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4065{
4066 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4067}
4068
4069static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4070{
4071 return (unsigned int)((uintptr_t)bi_private) & 3;
4072}
4073
Jan Schmidta1d3c472011-08-04 17:15:33 +02004074static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004075{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004076 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004077 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004078
Stefan Behrens442a4f62012-05-25 16:06:08 +02004079 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004080 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004081 if (err == -EIO || err == -EREMOTEIO) {
4082 unsigned int stripe_index =
4083 extract_stripe_index_from_bio_private(
4084 bio->bi_private);
4085 struct btrfs_device *dev;
4086
4087 BUG_ON(stripe_index >= bbio->num_stripes);
4088 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004089 if (dev->bdev) {
4090 if (bio->bi_rw & WRITE)
4091 btrfs_dev_stat_inc(dev,
4092 BTRFS_DEV_STAT_WRITE_ERRS);
4093 else
4094 btrfs_dev_stat_inc(dev,
4095 BTRFS_DEV_STAT_READ_ERRS);
4096 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4097 btrfs_dev_stat_inc(dev,
4098 BTRFS_DEV_STAT_FLUSH_ERRS);
4099 btrfs_dev_stat_print_on_error(dev);
4100 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004101 }
4102 }
Chris Mason8790d502008-04-03 16:29:03 -04004103
Jan Schmidta1d3c472011-08-04 17:15:33 +02004104 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004105 is_orig_bio = 1;
4106
Jan Schmidta1d3c472011-08-04 17:15:33 +02004107 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004108 if (!is_orig_bio) {
4109 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004110 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004111 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004112 bio->bi_private = bbio->private;
4113 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004114 bio->bi_bdev = (struct block_device *)
4115 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004116 /* only send an error to the higher layers if it is
4117 * beyond the tolerance of the multi-bio
4118 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004119 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004120 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004121 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004122 /*
4123 * this bio is actually up to date, we didn't
4124 * go over the max number of errors
4125 */
4126 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004127 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004128 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004129 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004130
4131 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004132 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004133 bio_put(bio);
4134 }
Chris Mason8790d502008-04-03 16:29:03 -04004135}
4136
Chris Mason8b712842008-06-11 16:50:36 -04004137struct async_sched {
4138 struct bio *bio;
4139 int rw;
4140 struct btrfs_fs_info *info;
4141 struct btrfs_work work;
4142};
4143
4144/*
4145 * see run_scheduled_bios for a description of why bios are collected for
4146 * async submit.
4147 *
4148 * This will add one bio to the pending list for a device and make sure
4149 * the work struct is scheduled.
4150 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004151static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004152 struct btrfs_device *device,
4153 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004154{
4155 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004156 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004157
4158 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004159 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004160 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004161 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004162 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004163 return;
Chris Mason8b712842008-06-11 16:50:36 -04004164 }
4165
4166 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004167 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004168 * higher layers. Otherwise, the async bio makes it appear we have
4169 * made progress against dirty pages when we've really just put it
4170 * on a queue for later
4171 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004172 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004173 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004174 bio->bi_next = NULL;
4175 bio->bi_rw |= rw;
4176
4177 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004178 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004179 pending_bios = &device->pending_sync_bios;
4180 else
4181 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004182
Chris Masonffbd5172009-04-20 15:50:09 -04004183 if (pending_bios->tail)
4184 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004185
Chris Masonffbd5172009-04-20 15:50:09 -04004186 pending_bios->tail = bio;
4187 if (!pending_bios->head)
4188 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004189 if (device->running_pending)
4190 should_queue = 0;
4191
4192 spin_unlock(&device->io_lock);
4193
4194 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004195 btrfs_queue_worker(&root->fs_info->submit_workers,
4196 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004197}
4198
Chris Masonf1885912008-04-09 16:28:12 -04004199int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004200 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004201{
4202 struct btrfs_mapping_tree *map_tree;
4203 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004204 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004205 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004206 u64 length = 0;
4207 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004208 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004209 int dev_nr = 0;
4210 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004211 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004212
Chris Masonf2d8d742008-04-21 10:03:05 -04004213 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004214 map_tree = &root->fs_info->mapping_tree;
4215 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004216
Jan Schmidta1d3c472011-08-04 17:15:33 +02004217 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004218 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004219 if (ret) /* -ENOMEM */
4220 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004221
Jan Schmidta1d3c472011-08-04 17:15:33 +02004222 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004223 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004224 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4225 "len %llu\n", (unsigned long long)logical,
4226 (unsigned long long)length,
4227 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004228 BUG();
4229 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004230
4231 bbio->orig_bio = first_bio;
4232 bbio->private = first_bio->bi_private;
4233 bbio->end_io = first_bio->bi_end_io;
4234 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004235
Chris Masond3977122009-01-05 21:25:51 -05004236 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004237 if (dev_nr < total_devs - 1) {
4238 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004239 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004240 } else {
4241 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004242 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004243 bio->bi_private = bbio;
Stefan Behrens442a4f62012-05-25 16:06:08 +02004244 bio->bi_private = merge_stripe_index_into_bio_private(
4245 bio->bi_private, (unsigned int)dev_nr);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004246 bio->bi_end_io = btrfs_end_bio;
4247 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4248 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004249 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Josef Bacik606686e2012-06-04 14:03:51 -04004250#ifdef DEBUG
4251 struct rcu_string *name;
4252
4253 rcu_read_lock();
4254 name = rcu_dereference(dev->name);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004255 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4256 "(%s id %llu), size=%u\n", rw,
4257 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
Josef Bacik606686e2012-06-04 14:03:51 -04004258 name->str, dev->devid, bio->bi_size);
4259 rcu_read_unlock();
4260#endif
Chris Masondfe25022008-05-13 13:46:40 -04004261 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004262 if (async_submit)
4263 schedule_bio(root, dev, rw, bio);
4264 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004265 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004266 } else {
4267 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4268 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004269 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004270 }
Chris Mason8790d502008-04-03 16:29:03 -04004271 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004272 }
Chris Mason0b86a832008-03-24 15:01:56 -04004273 return 0;
4274}
4275
Chris Masona4437552008-04-18 10:29:38 -04004276struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004277 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004278{
Yan Zheng2b820322008-11-17 21:11:30 -05004279 struct btrfs_device *device;
4280 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004281
Yan Zheng2b820322008-11-17 21:11:30 -05004282 cur_devices = root->fs_info->fs_devices;
4283 while (cur_devices) {
4284 if (!fsid ||
4285 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4286 device = __find_device(&cur_devices->devices,
4287 devid, uuid);
4288 if (device)
4289 return device;
4290 }
4291 cur_devices = cur_devices->seed;
4292 }
4293 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004294}
4295
Chris Masondfe25022008-05-13 13:46:40 -04004296static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4297 u64 devid, u8 *dev_uuid)
4298{
4299 struct btrfs_device *device;
4300 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4301
4302 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004303 if (!device)
4304 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004305 list_add(&device->dev_list,
4306 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004307 device->dev_root = root->fs_info->dev_root;
4308 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004309 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004310 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004311 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004312 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004313 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004314 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004315 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004316 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4317 return device;
4318}
4319
Chris Mason0b86a832008-03-24 15:01:56 -04004320static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4321 struct extent_buffer *leaf,
4322 struct btrfs_chunk *chunk)
4323{
4324 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4325 struct map_lookup *map;
4326 struct extent_map *em;
4327 u64 logical;
4328 u64 length;
4329 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004330 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004331 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004332 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004333 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004334
Chris Masone17cade2008-04-15 15:41:47 -04004335 logical = key->offset;
4336 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004337
Chris Mason890871b2009-09-02 16:24:52 -04004338 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004339 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004340 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004341
4342 /* already mapped? */
4343 if (em && em->start <= logical && em->start + em->len > logical) {
4344 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004345 return 0;
4346 } else if (em) {
4347 free_extent_map(em);
4348 }
Chris Mason0b86a832008-03-24 15:01:56 -04004349
David Sterba172ddd62011-04-21 00:48:27 +02004350 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004351 if (!em)
4352 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004353 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4354 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004355 if (!map) {
4356 free_extent_map(em);
4357 return -ENOMEM;
4358 }
4359
4360 em->bdev = (struct block_device *)map;
4361 em->start = logical;
4362 em->len = length;
4363 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004364 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004365
Chris Mason593060d2008-03-25 16:50:33 -04004366 map->num_stripes = num_stripes;
4367 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4368 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4369 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4370 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4371 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004372 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004373 for (i = 0; i < num_stripes; i++) {
4374 map->stripes[i].physical =
4375 btrfs_stripe_offset_nr(leaf, chunk, i);
4376 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004377 read_extent_buffer(leaf, uuid, (unsigned long)
4378 btrfs_stripe_dev_uuid_nr(chunk, i),
4379 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004380 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4381 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004382 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004383 kfree(map);
4384 free_extent_map(em);
4385 return -EIO;
4386 }
Chris Masondfe25022008-05-13 13:46:40 -04004387 if (!map->stripes[i].dev) {
4388 map->stripes[i].dev =
4389 add_missing_dev(root, devid, uuid);
4390 if (!map->stripes[i].dev) {
4391 kfree(map);
4392 free_extent_map(em);
4393 return -EIO;
4394 }
4395 }
4396 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004397 }
4398
Chris Mason890871b2009-09-02 16:24:52 -04004399 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004400 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004401 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004402 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004403 free_extent_map(em);
4404
4405 return 0;
4406}
4407
Jeff Mahoney143bede2012-03-01 14:56:26 +01004408static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004409 struct btrfs_dev_item *dev_item,
4410 struct btrfs_device *device)
4411{
4412 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004413
4414 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004415 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4416 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004417 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4418 device->type = btrfs_device_type(leaf, dev_item);
4419 device->io_align = btrfs_device_io_align(leaf, dev_item);
4420 device->io_width = btrfs_device_io_width(leaf, dev_item);
4421 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004422
4423 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004424 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004425}
4426
Yan Zheng2b820322008-11-17 21:11:30 -05004427static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4428{
4429 struct btrfs_fs_devices *fs_devices;
4430 int ret;
4431
Li Zefanb367e472011-12-07 11:38:24 +08004432 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004433
4434 fs_devices = root->fs_info->fs_devices->seed;
4435 while (fs_devices) {
4436 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4437 ret = 0;
4438 goto out;
4439 }
4440 fs_devices = fs_devices->seed;
4441 }
4442
4443 fs_devices = find_fsid(fsid);
4444 if (!fs_devices) {
4445 ret = -ENOENT;
4446 goto out;
4447 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004448
4449 fs_devices = clone_fs_devices(fs_devices);
4450 if (IS_ERR(fs_devices)) {
4451 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004452 goto out;
4453 }
4454
Christoph Hellwig97288f22008-12-02 06:36:09 -05004455 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004456 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004457 if (ret) {
4458 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004459 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004460 }
Yan Zheng2b820322008-11-17 21:11:30 -05004461
4462 if (!fs_devices->seeding) {
4463 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004464 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004465 ret = -EINVAL;
4466 goto out;
4467 }
4468
4469 fs_devices->seed = root->fs_info->fs_devices->seed;
4470 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004471out:
Yan Zheng2b820322008-11-17 21:11:30 -05004472 return ret;
4473}
4474
Chris Mason0d81ba52008-03-24 15:02:07 -04004475static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004476 struct extent_buffer *leaf,
4477 struct btrfs_dev_item *dev_item)
4478{
4479 struct btrfs_device *device;
4480 u64 devid;
4481 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004482 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004483 u8 dev_uuid[BTRFS_UUID_SIZE];
4484
Chris Mason0b86a832008-03-24 15:01:56 -04004485 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004486 read_extent_buffer(leaf, dev_uuid,
4487 (unsigned long)btrfs_device_uuid(dev_item),
4488 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004489 read_extent_buffer(leaf, fs_uuid,
4490 (unsigned long)btrfs_device_fsid(dev_item),
4491 BTRFS_UUID_SIZE);
4492
4493 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4494 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004495 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004496 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004497 }
4498
4499 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4500 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004501 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004502 return -EIO;
4503
4504 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004505 printk(KERN_WARNING "warning devid %llu missing\n",
4506 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004507 device = add_missing_dev(root, devid, dev_uuid);
4508 if (!device)
4509 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004510 } else if (!device->missing) {
4511 /*
4512 * this happens when a device that was properly setup
4513 * in the device info lists suddenly goes bad.
4514 * device->bdev is NULL, and so we have to set
4515 * device->missing to one here
4516 */
4517 root->fs_info->fs_devices->missing_devices++;
4518 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004519 }
4520 }
4521
4522 if (device->fs_devices != root->fs_info->fs_devices) {
4523 BUG_ON(device->writeable);
4524 if (device->generation !=
4525 btrfs_device_generation(leaf, dev_item))
4526 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004527 }
Chris Mason0b86a832008-03-24 15:01:56 -04004528
4529 fill_device_from_item(leaf, dev_item, device);
4530 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004531 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004532 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004533 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004534 spin_lock(&root->fs_info->free_chunk_lock);
4535 root->fs_info->free_chunk_space += device->total_bytes -
4536 device->bytes_used;
4537 spin_unlock(&root->fs_info->free_chunk_lock);
4538 }
Chris Mason0b86a832008-03-24 15:01:56 -04004539 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004540 return ret;
4541}
4542
Yan Zhenge4404d62008-12-12 10:03:26 -05004543int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004544{
David Sterba6c417612011-04-13 15:41:04 +02004545 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004546 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004547 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004548 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004549 u8 *ptr;
4550 unsigned long sb_ptr;
4551 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004552 u32 num_stripes;
4553 u32 array_size;
4554 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004555 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004556 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004557
Yan Zhenge4404d62008-12-12 10:03:26 -05004558 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004559 BTRFS_SUPER_INFO_SIZE);
4560 if (!sb)
4561 return -ENOMEM;
4562 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004563 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004564 /*
4565 * The sb extent buffer is artifical and just used to read the system array.
4566 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4567 * pages up-to-date when the page is larger: extent does not cover the
4568 * whole page and consequently check_page_uptodate does not find all
4569 * the page's extents up-to-date (the hole beyond sb),
4570 * write_extent_buffer then triggers a WARN_ON.
4571 *
4572 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4573 * but sb spans only this function. Add an explicit SetPageUptodate call
4574 * to silence the warning eg. on PowerPC 64.
4575 */
4576 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004577 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004578
Chris Masona061fc82008-05-07 11:43:44 -04004579 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004580 array_size = btrfs_super_sys_array_size(super_copy);
4581
Chris Mason0b86a832008-03-24 15:01:56 -04004582 ptr = super_copy->sys_chunk_array;
4583 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4584 cur = 0;
4585
4586 while (cur < array_size) {
4587 disk_key = (struct btrfs_disk_key *)ptr;
4588 btrfs_disk_key_to_cpu(&key, disk_key);
4589
Chris Masona061fc82008-05-07 11:43:44 -04004590 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004591 sb_ptr += len;
4592 cur += len;
4593
Chris Mason0d81ba52008-03-24 15:02:07 -04004594 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004595 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004596 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004597 if (ret)
4598 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004599 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4600 len = btrfs_chunk_item_size(num_stripes);
4601 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004602 ret = -EIO;
4603 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004604 }
4605 ptr += len;
4606 sb_ptr += len;
4607 cur += len;
4608 }
Chris Masona061fc82008-05-07 11:43:44 -04004609 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004610 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004611}
4612
4613int btrfs_read_chunk_tree(struct btrfs_root *root)
4614{
4615 struct btrfs_path *path;
4616 struct extent_buffer *leaf;
4617 struct btrfs_key key;
4618 struct btrfs_key found_key;
4619 int ret;
4620 int slot;
4621
4622 root = root->fs_info->chunk_root;
4623
4624 path = btrfs_alloc_path();
4625 if (!path)
4626 return -ENOMEM;
4627
Li Zefanb367e472011-12-07 11:38:24 +08004628 mutex_lock(&uuid_mutex);
4629 lock_chunks(root);
4630
Chris Mason0b86a832008-03-24 15:01:56 -04004631 /* first we search for all of the device items, and then we
4632 * read in all of the chunk items. This way we can create chunk
4633 * mappings that reference all of the devices that are afound
4634 */
4635 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4636 key.offset = 0;
4637 key.type = 0;
4638again:
4639 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004640 if (ret < 0)
4641 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004642 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004643 leaf = path->nodes[0];
4644 slot = path->slots[0];
4645 if (slot >= btrfs_header_nritems(leaf)) {
4646 ret = btrfs_next_leaf(root, path);
4647 if (ret == 0)
4648 continue;
4649 if (ret < 0)
4650 goto error;
4651 break;
4652 }
4653 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4654 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4655 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4656 break;
4657 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4658 struct btrfs_dev_item *dev_item;
4659 dev_item = btrfs_item_ptr(leaf, slot,
4660 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004661 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004662 if (ret)
4663 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004664 }
4665 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4666 struct btrfs_chunk *chunk;
4667 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4668 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004669 if (ret)
4670 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004671 }
4672 path->slots[0]++;
4673 }
4674 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4675 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004676 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004677 goto again;
4678 }
Chris Mason0b86a832008-03-24 15:01:56 -04004679 ret = 0;
4680error:
Li Zefanb367e472011-12-07 11:38:24 +08004681 unlock_chunks(root);
4682 mutex_unlock(&uuid_mutex);
4683
Yan Zheng2b820322008-11-17 21:11:30 -05004684 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004685 return ret;
4686}
Stefan Behrens442a4f62012-05-25 16:06:08 +02004687
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004688static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
4689{
4690 int i;
4691
4692 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4693 btrfs_dev_stat_reset(dev, i);
4694}
4695
4696int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
4697{
4698 struct btrfs_key key;
4699 struct btrfs_key found_key;
4700 struct btrfs_root *dev_root = fs_info->dev_root;
4701 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4702 struct extent_buffer *eb;
4703 int slot;
4704 int ret = 0;
4705 struct btrfs_device *device;
4706 struct btrfs_path *path = NULL;
4707 int i;
4708
4709 path = btrfs_alloc_path();
4710 if (!path) {
4711 ret = -ENOMEM;
4712 goto out;
4713 }
4714
4715 mutex_lock(&fs_devices->device_list_mutex);
4716 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4717 int item_size;
4718 struct btrfs_dev_stats_item *ptr;
4719
4720 key.objectid = 0;
4721 key.type = BTRFS_DEV_STATS_KEY;
4722 key.offset = device->devid;
4723 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
4724 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004725 __btrfs_reset_dev_stats(device);
4726 device->dev_stats_valid = 1;
4727 btrfs_release_path(path);
4728 continue;
4729 }
4730 slot = path->slots[0];
4731 eb = path->nodes[0];
4732 btrfs_item_key_to_cpu(eb, &found_key, slot);
4733 item_size = btrfs_item_size_nr(eb, slot);
4734
4735 ptr = btrfs_item_ptr(eb, slot,
4736 struct btrfs_dev_stats_item);
4737
4738 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4739 if (item_size >= (1 + i) * sizeof(__le64))
4740 btrfs_dev_stat_set(device, i,
4741 btrfs_dev_stats_value(eb, ptr, i));
4742 else
4743 btrfs_dev_stat_reset(device, i);
4744 }
4745
4746 device->dev_stats_valid = 1;
4747 btrfs_dev_stat_print_on_load(device);
4748 btrfs_release_path(path);
4749 }
4750 mutex_unlock(&fs_devices->device_list_mutex);
4751
4752out:
4753 btrfs_free_path(path);
4754 return ret < 0 ? ret : 0;
4755}
4756
4757static int update_dev_stat_item(struct btrfs_trans_handle *trans,
4758 struct btrfs_root *dev_root,
4759 struct btrfs_device *device)
4760{
4761 struct btrfs_path *path;
4762 struct btrfs_key key;
4763 struct extent_buffer *eb;
4764 struct btrfs_dev_stats_item *ptr;
4765 int ret;
4766 int i;
4767
4768 key.objectid = 0;
4769 key.type = BTRFS_DEV_STATS_KEY;
4770 key.offset = device->devid;
4771
4772 path = btrfs_alloc_path();
4773 BUG_ON(!path);
4774 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
4775 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004776 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
4777 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004778 goto out;
4779 }
4780
4781 if (ret == 0 &&
4782 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
4783 /* need to delete old one and insert a new one */
4784 ret = btrfs_del_item(trans, dev_root, path);
4785 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004786 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
4787 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004788 goto out;
4789 }
4790 ret = 1;
4791 }
4792
4793 if (ret == 1) {
4794 /* need to insert a new item */
4795 btrfs_release_path(path);
4796 ret = btrfs_insert_empty_item(trans, dev_root, path,
4797 &key, sizeof(*ptr));
4798 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004799 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
4800 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004801 goto out;
4802 }
4803 }
4804
4805 eb = path->nodes[0];
4806 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
4807 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4808 btrfs_set_dev_stats_value(eb, ptr, i,
4809 btrfs_dev_stat_read(device, i));
4810 btrfs_mark_buffer_dirty(eb);
4811
4812out:
4813 btrfs_free_path(path);
4814 return ret;
4815}
4816
4817/*
4818 * called from commit_transaction. Writes all changed device stats to disk.
4819 */
4820int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
4821 struct btrfs_fs_info *fs_info)
4822{
4823 struct btrfs_root *dev_root = fs_info->dev_root;
4824 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4825 struct btrfs_device *device;
4826 int ret = 0;
4827
4828 mutex_lock(&fs_devices->device_list_mutex);
4829 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4830 if (!device->dev_stats_valid || !device->dev_stats_dirty)
4831 continue;
4832
4833 ret = update_dev_stat_item(trans, dev_root, device);
4834 if (!ret)
4835 device->dev_stats_dirty = 0;
4836 }
4837 mutex_unlock(&fs_devices->device_list_mutex);
4838
4839 return ret;
4840}
4841
Stefan Behrens442a4f62012-05-25 16:06:08 +02004842void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
4843{
4844 btrfs_dev_stat_inc(dev, index);
4845 btrfs_dev_stat_print_on_error(dev);
4846}
4847
4848void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
4849{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004850 if (!dev->dev_stats_valid)
4851 return;
Josef Bacik606686e2012-06-04 14:03:51 -04004852 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02004853 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04004854 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02004855 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4856 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4857 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4858 btrfs_dev_stat_read(dev,
4859 BTRFS_DEV_STAT_CORRUPTION_ERRS),
4860 btrfs_dev_stat_read(dev,
4861 BTRFS_DEV_STAT_GENERATION_ERRS));
4862}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004863
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004864static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
4865{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06004866 int i;
4867
4868 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4869 if (btrfs_dev_stat_read(dev, i) != 0)
4870 break;
4871 if (i == BTRFS_DEV_STAT_VALUES_MAX)
4872 return; /* all values == 0, suppress message */
4873
Josef Bacik606686e2012-06-04 14:03:51 -04004874 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
4875 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004876 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4877 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4878 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4879 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
4880 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
4881}
4882
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004883int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06004884 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004885{
4886 struct btrfs_device *dev;
4887 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4888 int i;
4889
4890 mutex_lock(&fs_devices->device_list_mutex);
4891 dev = btrfs_find_device(root, stats->devid, NULL, NULL);
4892 mutex_unlock(&fs_devices->device_list_mutex);
4893
4894 if (!dev) {
4895 printk(KERN_WARNING
4896 "btrfs: get dev_stats failed, device not found\n");
4897 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004898 } else if (!dev->dev_stats_valid) {
4899 printk(KERN_WARNING
4900 "btrfs: get dev_stats failed, not yet valid\n");
4901 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06004902 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004903 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4904 if (stats->nr_items > i)
4905 stats->values[i] =
4906 btrfs_dev_stat_read_and_reset(dev, i);
4907 else
4908 btrfs_dev_stat_reset(dev, i);
4909 }
4910 } else {
4911 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4912 if (stats->nr_items > i)
4913 stats->values[i] = btrfs_dev_stat_read(dev, i);
4914 }
4915 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
4916 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
4917 return 0;
4918}