blob: 48943d0f861af5412465aebbd2c6de144ccb2af9 [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 atomic_dec(&fs_info->nr_async_bios);
231
232 if (atomic_read(&fs_info->nr_async_bios) < limit &&
233 waitqueue_active(&fs_info->async_submit_wait))
234 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400235
236 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400237
Chris Mason2ab1ba62011-08-04 14:28:36 -0400238 /*
239 * if we're doing the sync list, record that our
240 * plug has some sync requests on it
241 *
242 * If we're doing the regular list and there are
243 * sync requests sitting around, unplug before
244 * we add more
245 */
246 if (pending_bios == &device->pending_sync_bios) {
247 sync_pending = 1;
248 } else if (sync_pending) {
249 blk_finish_plug(&plug);
250 blk_start_plug(&plug);
251 sync_pending = 0;
252 }
253
Stefan Behrens21adbd52011-11-09 13:44:05 +0100254 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400255 num_run++;
256 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100257 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400258 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400259
260 /*
261 * we made progress, there is more work to do and the bdi
262 * is now congested. Back off and let other work structs
263 * run instead
264 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400265 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500266 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400267 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400268
Chris Masonb765ead2009-04-03 10:27:10 -0400269 ioc = current->io_context;
270
271 /*
272 * the main goal here is that we don't want to
273 * block if we're going to be able to submit
274 * more requests without blocking.
275 *
276 * This code does two great things, it pokes into
277 * the elevator code from a filesystem _and_
278 * it makes assumptions about how batching works.
279 */
280 if (ioc && ioc->nr_batch_requests > 0 &&
281 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
282 (last_waited == 0 ||
283 ioc->last_waited == last_waited)) {
284 /*
285 * we want to go through our batch of
286 * requests and stop. So, we copy out
287 * the ioc->last_waited time and test
288 * against it before looping
289 */
290 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100291 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400292 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400293 continue;
294 }
Chris Mason8b712842008-06-11 16:50:36 -0400295 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400296 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500297 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400298
299 spin_unlock(&device->io_lock);
300 btrfs_requeue_work(&device->work);
301 goto done;
302 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500303 /* unplug every 64 requests just for good measure */
304 if (batch_run % 64 == 0) {
305 blk_finish_plug(&plug);
306 blk_start_plug(&plug);
307 sync_pending = 0;
308 }
Chris Mason8b712842008-06-11 16:50:36 -0400309 }
Chris Masonffbd5172009-04-20 15:50:09 -0400310
Chris Mason51684082010-03-10 15:33:32 -0500311 cond_resched();
312 if (again)
313 goto loop;
314
315 spin_lock(&device->io_lock);
316 if (device->pending_bios.head || device->pending_sync_bios.head)
317 goto loop_lock;
318 spin_unlock(&device->io_lock);
319
Chris Mason8b712842008-06-11 16:50:36 -0400320done:
Chris Mason211588a2011-04-19 20:12:40 -0400321 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400322}
323
Christoph Hellwigb2950862008-12-02 09:54:17 -0500324static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400325{
326 struct btrfs_device *device;
327
328 device = container_of(work, struct btrfs_device, work);
329 run_scheduled_bios(device);
330}
331
Chris Masona1b32a52008-09-05 16:09:51 -0400332static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400333 struct btrfs_super_block *disk_super,
334 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
335{
336 struct btrfs_device *device;
337 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400338 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400339 u64 found_transid = btrfs_super_generation(disk_super);
340
341 fs_devices = find_fsid(disk_super->fsid);
342 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400343 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400344 if (!fs_devices)
345 return -ENOMEM;
346 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400347 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400348 list_add(&fs_devices->list, &fs_uuids);
349 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
350 fs_devices->latest_devid = devid;
351 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400352 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400353 device = NULL;
354 } else {
Chris Masona4437552008-04-18 10:29:38 -0400355 device = __find_device(&fs_devices->devices, devid,
356 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400357 }
358 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500359 if (fs_devices->opened)
360 return -EBUSY;
361
Chris Mason8a4b83c2008-03-24 15:02:07 -0400362 device = kzalloc(sizeof(*device), GFP_NOFS);
363 if (!device) {
364 /* we can safely leave the fs_devices entry around */
365 return -ENOMEM;
366 }
367 device->devid = devid;
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200368 device->dev_stats_valid = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400369 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400370 memcpy(device->uuid, disk_super->dev_item.uuid,
371 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400372 spin_lock_init(&device->io_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400373
374 name = rcu_string_strdup(path, GFP_NOFS);
375 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400376 kfree(device);
377 return -ENOMEM;
378 }
Josef Bacik606686e2012-06-04 14:03:51 -0400379 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -0500380 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400381
Arne Jansen90519d62011-05-23 14:30:00 +0200382 /* init readahead state */
383 spin_lock_init(&device->reada_lock);
384 device->reada_curr_zone = NULL;
385 atomic_set(&device->reada_in_flight, 0);
386 device->reada_next = 0;
387 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
388 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
389
Chris Masone5e9a522009-06-10 15:17:02 -0400390 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000391 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400392 mutex_unlock(&fs_devices->device_list_mutex);
393
Yan Zheng2b820322008-11-17 21:11:30 -0500394 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400395 fs_devices->num_devices++;
Josef Bacik606686e2012-06-04 14:03:51 -0400396 } else if (!device->name || strcmp(device->name->str, path)) {
397 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000398 if (!name)
399 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400400 rcu_string_free(device->name);
401 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500402 if (device->missing) {
403 fs_devices->missing_devices--;
404 device->missing = 0;
405 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400406 }
407
408 if (found_transid > fs_devices->latest_trans) {
409 fs_devices->latest_devid = devid;
410 fs_devices->latest_trans = found_transid;
411 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400412 *fs_devices_ret = fs_devices;
413 return 0;
414}
415
Yan Zhenge4404d62008-12-12 10:03:26 -0500416static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
417{
418 struct btrfs_fs_devices *fs_devices;
419 struct btrfs_device *device;
420 struct btrfs_device *orig_dev;
421
422 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
423 if (!fs_devices)
424 return ERR_PTR(-ENOMEM);
425
426 INIT_LIST_HEAD(&fs_devices->devices);
427 INIT_LIST_HEAD(&fs_devices->alloc_list);
428 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400429 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500430 fs_devices->latest_devid = orig->latest_devid;
431 fs_devices->latest_trans = orig->latest_trans;
432 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 */
571 name = rcu_string_strdup(device->name->str, GFP_NOFS);
572 BUG_ON(device->name && !name); /* -ENOMEM */
573 rcu_assign_pointer(new_device->name, name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000574 new_device->bdev = NULL;
575 new_device->writeable = 0;
576 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000577 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000578 list_replace_rcu(&device->dev_list, &new_device->dev_list);
579
580 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400581 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000582 mutex_unlock(&fs_devices->device_list_mutex);
583
Yan Zhenge4404d62008-12-12 10:03:26 -0500584 WARN_ON(fs_devices->open_devices);
585 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500586 fs_devices->opened = 0;
587 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500588
Chris Mason8a4b83c2008-03-24 15:02:07 -0400589 return 0;
590}
591
Yan Zheng2b820322008-11-17 21:11:30 -0500592int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
593{
Yan Zhenge4404d62008-12-12 10:03:26 -0500594 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500595 int ret;
596
597 mutex_lock(&uuid_mutex);
598 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500599 if (!fs_devices->opened) {
600 seed_devices = fs_devices->seed;
601 fs_devices->seed = NULL;
602 }
Yan Zheng2b820322008-11-17 21:11:30 -0500603 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500604
605 while (seed_devices) {
606 fs_devices = seed_devices;
607 seed_devices = fs_devices->seed;
608 __btrfs_close_devices(fs_devices);
609 free_fs_devices(fs_devices);
610 }
Yan Zheng2b820322008-11-17 21:11:30 -0500611 return ret;
612}
613
Yan Zhenge4404d62008-12-12 10:03:26 -0500614static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
615 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400616{
Josef Bacikd5e20032011-08-04 14:52:27 +0000617 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400618 struct block_device *bdev;
619 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400620 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400621 struct block_device *latest_bdev = NULL;
622 struct buffer_head *bh;
623 struct btrfs_super_block *disk_super;
624 u64 latest_devid = 0;
625 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400626 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500627 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400628 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400629
Tejun Heod4d77622010-11-13 11:55:18 +0100630 flags |= FMODE_EXCL;
631
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500632 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400633 if (device->bdev)
634 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400635 if (!device->name)
636 continue;
637
Josef Bacik606686e2012-06-04 14:03:51 -0400638 bdev = blkdev_get_by_path(device->name->str, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400639 if (IS_ERR(bdev)) {
Josef Bacik606686e2012-06-04 14:03:51 -0400640 printk(KERN_INFO "open %s failed\n", device->name->str);
Chris Masona0af4692008-05-13 16:03:06 -0400641 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400642 }
Chris Mason3c4bb262012-03-27 18:56:56 -0400643 filemap_write_and_wait(bdev->bd_inode->i_mapping);
644 invalidate_bdev(bdev);
Chris Masona061fc82008-05-07 11:43:44 -0400645 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400646
Yan Zhenga512bbf2008-12-08 16:46:26 -0500647 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300648 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400649 goto error_close;
650
651 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000652 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400653 if (devid != device->devid)
654 goto error_brelse;
655
Yan Zheng2b820322008-11-17 21:11:30 -0500656 if (memcmp(device->uuid, disk_super->dev_item.uuid,
657 BTRFS_UUID_SIZE))
658 goto error_brelse;
659
660 device->generation = btrfs_super_generation(disk_super);
661 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400662 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500663 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400664 latest_bdev = bdev;
665 }
666
Yan Zheng2b820322008-11-17 21:11:30 -0500667 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
668 device->writeable = 0;
669 } else {
670 device->writeable = !bdev_read_only(bdev);
671 seeding = 0;
672 }
673
Josef Bacikd5e20032011-08-04 14:52:27 +0000674 q = bdev_get_queue(bdev);
675 if (blk_queue_discard(q)) {
676 device->can_discard = 1;
677 fs_devices->num_can_discard++;
678 }
679
Chris Mason8a4b83c2008-03-24 15:02:07 -0400680 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400681 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500682 device->mode = flags;
683
Chris Masonc2898112009-06-10 09:51:32 -0400684 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
685 fs_devices->rotating = 1;
686
Chris Masona0af4692008-05-13 16:03:06 -0400687 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500688 if (device->writeable) {
689 fs_devices->rw_devices++;
690 list_add(&device->dev_alloc_list,
691 &fs_devices->alloc_list);
692 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000693 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400694 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400695
Chris Masona0af4692008-05-13 16:03:06 -0400696error_brelse:
697 brelse(bh);
698error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100699 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400700error:
701 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400702 }
Chris Masona0af4692008-05-13 16:03:06 -0400703 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300704 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400705 goto out;
706 }
Yan Zheng2b820322008-11-17 21:11:30 -0500707 fs_devices->seeding = seeding;
708 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400709 fs_devices->latest_bdev = latest_bdev;
710 fs_devices->latest_devid = latest_devid;
711 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500712 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400713out:
Yan Zheng2b820322008-11-17 21:11:30 -0500714 return ret;
715}
716
717int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500718 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500719{
720 int ret;
721
722 mutex_lock(&uuid_mutex);
723 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500724 fs_devices->opened++;
725 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500726 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500727 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500728 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400729 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400730 return ret;
731}
732
Christoph Hellwig97288f22008-12-02 06:36:09 -0500733int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400734 struct btrfs_fs_devices **fs_devices_ret)
735{
736 struct btrfs_super_block *disk_super;
737 struct block_device *bdev;
738 struct buffer_head *bh;
739 int ret;
740 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400741 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400742
Tejun Heod4d77622010-11-13 11:55:18 +0100743 flags |= FMODE_EXCL;
744 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400745
746 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400747 ret = PTR_ERR(bdev);
748 goto error;
749 }
750
Al Viro10f63272011-11-17 15:05:22 -0500751 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400752 ret = set_blocksize(bdev, 4096);
753 if (ret)
754 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500755 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400756 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000757 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758 goto error_close;
759 }
760 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000761 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400762 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400763 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500764 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200765 else
766 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500767 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500768 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400769 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
770
Chris Mason8a4b83c2008-03-24 15:02:07 -0400771 brelse(bh);
772error_close:
Al Viro10f63272011-11-17 15:05:22 -0500773 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100774 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400775error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400776 return ret;
777}
Chris Mason0b86a832008-03-24 15:01:56 -0400778
Miao Xie6d07bce2011-01-05 10:07:31 +0000779/* helper to account the used device space in the range */
780int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
781 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400782{
783 struct btrfs_key key;
784 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000785 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500786 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000787 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400788 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000789 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400790 struct extent_buffer *l;
791
Miao Xie6d07bce2011-01-05 10:07:31 +0000792 *length = 0;
793
794 if (start >= device->total_bytes)
795 return 0;
796
Yan Zheng2b820322008-11-17 21:11:30 -0500797 path = btrfs_alloc_path();
798 if (!path)
799 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400800 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400801
Chris Mason0b86a832008-03-24 15:01:56 -0400802 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000803 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400804 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000805
806 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400807 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000808 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400809 if (ret > 0) {
810 ret = btrfs_previous_item(root, path, key.objectid, key.type);
811 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000812 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400813 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000814
Chris Mason0b86a832008-03-24 15:01:56 -0400815 while (1) {
816 l = path->nodes[0];
817 slot = path->slots[0];
818 if (slot >= btrfs_header_nritems(l)) {
819 ret = btrfs_next_leaf(root, path);
820 if (ret == 0)
821 continue;
822 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000823 goto out;
824
825 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400826 }
827 btrfs_item_key_to_cpu(l, &key, slot);
828
829 if (key.objectid < device->devid)
830 goto next;
831
832 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000833 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400834
Chris Masond3977122009-01-05 21:25:51 -0500835 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400836 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400837
Chris Mason0b86a832008-03-24 15:01:56 -0400838 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000839 extent_end = key.offset + btrfs_dev_extent_length(l,
840 dev_extent);
841 if (key.offset <= start && extent_end > end) {
842 *length = end - start + 1;
843 break;
844 } else if (key.offset <= start && extent_end > start)
845 *length += extent_end - start;
846 else if (key.offset > start && extent_end <= end)
847 *length += extent_end - key.offset;
848 else if (key.offset > start && key.offset <= end) {
849 *length += end - key.offset + 1;
850 break;
851 } else if (key.offset > end)
852 break;
853
854next:
855 path->slots[0]++;
856 }
857 ret = 0;
858out:
859 btrfs_free_path(path);
860 return ret;
861}
862
Chris Mason0b86a832008-03-24 15:01:56 -0400863/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000864 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000865 * @device: the device which we search the free space in
866 * @num_bytes: the size of the free space that we need
867 * @start: store the start of the free space.
868 * @len: the size of the free space. that we find, or the size of the max
869 * free space if we don't find suitable free space
870 *
Chris Mason0b86a832008-03-24 15:01:56 -0400871 * this uses a pretty simple search, the expectation is that it is
872 * called very infrequently and that a given device has a small number
873 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000874 *
875 * @start is used to store the start of the free space if we find. But if we
876 * don't find suitable free space, it will be used to store the start position
877 * of the max free space.
878 *
879 * @len is used to store the size of the free space that we find.
880 * But if we don't find suitable free space, it is used to store the size of
881 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400882 */
Li Zefan125ccb02011-12-08 15:07:24 +0800883int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000884 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400885{
886 struct btrfs_key key;
887 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000888 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400889 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000890 u64 hole_size;
891 u64 max_hole_start;
892 u64 max_hole_size;
893 u64 extent_end;
894 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400895 u64 search_end = device->total_bytes;
896 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000897 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400898 struct extent_buffer *l;
899
Chris Mason0b86a832008-03-24 15:01:56 -0400900 /* FIXME use last free of some kind */
901
902 /* we don't want to overwrite the superblock on the drive,
903 * so we make sure to start at an offset of at least 1MB
904 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200905 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400906
Miao Xie7bfc8372011-01-05 10:07:26 +0000907 max_hole_start = search_start;
908 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000909 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000910
911 if (search_start >= search_end) {
912 ret = -ENOSPC;
913 goto error;
914 }
915
916 path = btrfs_alloc_path();
917 if (!path) {
918 ret = -ENOMEM;
919 goto error;
920 }
921 path->reada = 2;
922
Chris Mason0b86a832008-03-24 15:01:56 -0400923 key.objectid = device->devid;
924 key.offset = search_start;
925 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000926
Li Zefan125ccb02011-12-08 15:07:24 +0800927 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400928 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000929 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400930 if (ret > 0) {
931 ret = btrfs_previous_item(root, path, key.objectid, key.type);
932 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000933 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400934 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000935
Chris Mason0b86a832008-03-24 15:01:56 -0400936 while (1) {
937 l = path->nodes[0];
938 slot = path->slots[0];
939 if (slot >= btrfs_header_nritems(l)) {
940 ret = btrfs_next_leaf(root, path);
941 if (ret == 0)
942 continue;
943 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000944 goto out;
945
946 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400947 }
948 btrfs_item_key_to_cpu(l, &key, slot);
949
950 if (key.objectid < device->devid)
951 goto next;
952
953 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000954 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400955
Chris Mason0b86a832008-03-24 15:01:56 -0400956 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
957 goto next;
958
Miao Xie7bfc8372011-01-05 10:07:26 +0000959 if (key.offset > search_start) {
960 hole_size = key.offset - search_start;
961
962 if (hole_size > max_hole_size) {
963 max_hole_start = search_start;
964 max_hole_size = hole_size;
965 }
966
967 /*
968 * If this free space is greater than which we need,
969 * it must be the max free space that we have found
970 * until now, so max_hole_start must point to the start
971 * of this free space and the length of this free space
972 * is stored in max_hole_size. Thus, we return
973 * max_hole_start and max_hole_size and go back to the
974 * caller.
975 */
976 if (hole_size >= num_bytes) {
977 ret = 0;
978 goto out;
979 }
980 }
981
Chris Mason0b86a832008-03-24 15:01:56 -0400982 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000983 extent_end = key.offset + btrfs_dev_extent_length(l,
984 dev_extent);
985 if (extent_end > search_start)
986 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400987next:
988 path->slots[0]++;
989 cond_resched();
990 }
Chris Mason0b86a832008-03-24 15:01:56 -0400991
liubo38c01b92011-08-02 02:39:03 +0000992 /*
993 * At this point, search_start should be the end of
994 * allocated dev extents, and when shrinking the device,
995 * search_end may be smaller than search_start.
996 */
997 if (search_end > search_start)
998 hole_size = search_end - search_start;
999
Miao Xie7bfc8372011-01-05 10:07:26 +00001000 if (hole_size > max_hole_size) {
1001 max_hole_start = search_start;
1002 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001003 }
Chris Mason0b86a832008-03-24 15:01:56 -04001004
Miao Xie7bfc8372011-01-05 10:07:26 +00001005 /* See above. */
1006 if (hole_size < num_bytes)
1007 ret = -ENOSPC;
1008 else
1009 ret = 0;
1010
1011out:
Yan Zheng2b820322008-11-17 21:11:30 -05001012 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001013error:
1014 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001015 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001016 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001017 return ret;
1018}
1019
Christoph Hellwigb2950862008-12-02 09:54:17 -05001020static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001021 struct btrfs_device *device,
1022 u64 start)
1023{
1024 int ret;
1025 struct btrfs_path *path;
1026 struct btrfs_root *root = device->dev_root;
1027 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001028 struct btrfs_key found_key;
1029 struct extent_buffer *leaf = NULL;
1030 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001031
1032 path = btrfs_alloc_path();
1033 if (!path)
1034 return -ENOMEM;
1035
1036 key.objectid = device->devid;
1037 key.offset = start;
1038 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001039again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001040 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001041 if (ret > 0) {
1042 ret = btrfs_previous_item(root, path, key.objectid,
1043 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001044 if (ret)
1045 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001046 leaf = path->nodes[0];
1047 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1048 extent = btrfs_item_ptr(leaf, path->slots[0],
1049 struct btrfs_dev_extent);
1050 BUG_ON(found_key.offset > start || found_key.offset +
1051 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001052 key = found_key;
1053 btrfs_release_path(path);
1054 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001055 } else if (ret == 0) {
1056 leaf = path->nodes[0];
1057 extent = btrfs_item_ptr(leaf, path->slots[0],
1058 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001059 } else {
1060 btrfs_error(root->fs_info, ret, "Slot search failed");
1061 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001062 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001063
Josef Bacik2bf64752011-09-26 17:12:22 -04001064 if (device->bytes_used > 0) {
1065 u64 len = btrfs_dev_extent_length(leaf, extent);
1066 device->bytes_used -= len;
1067 spin_lock(&root->fs_info->free_chunk_lock);
1068 root->fs_info->free_chunk_space += len;
1069 spin_unlock(&root->fs_info->free_chunk_lock);
1070 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001071 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001072 if (ret) {
1073 btrfs_error(root->fs_info, ret,
1074 "Failed to remove dev extent item");
1075 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001076out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001077 btrfs_free_path(path);
1078 return ret;
1079}
1080
Yan Zheng2b820322008-11-17 21:11:30 -05001081int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001082 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001083 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001084 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001085{
1086 int ret;
1087 struct btrfs_path *path;
1088 struct btrfs_root *root = device->dev_root;
1089 struct btrfs_dev_extent *extent;
1090 struct extent_buffer *leaf;
1091 struct btrfs_key key;
1092
Chris Masondfe25022008-05-13 13:46:40 -04001093 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001094 path = btrfs_alloc_path();
1095 if (!path)
1096 return -ENOMEM;
1097
Chris Mason0b86a832008-03-24 15:01:56 -04001098 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001099 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001100 key.type = BTRFS_DEV_EXTENT_KEY;
1101 ret = btrfs_insert_empty_item(trans, root, path, &key,
1102 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001103 if (ret)
1104 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001105
1106 leaf = path->nodes[0];
1107 extent = btrfs_item_ptr(leaf, path->slots[0],
1108 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001109 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1110 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1111 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1112
1113 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1114 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1115 BTRFS_UUID_SIZE);
1116
Chris Mason0b86a832008-03-24 15:01:56 -04001117 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1118 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001119out:
Chris Mason0b86a832008-03-24 15:01:56 -04001120 btrfs_free_path(path);
1121 return ret;
1122}
1123
Chris Masona1b32a52008-09-05 16:09:51 -04001124static noinline int find_next_chunk(struct btrfs_root *root,
1125 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001126{
1127 struct btrfs_path *path;
1128 int ret;
1129 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001130 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001131 struct btrfs_key found_key;
1132
1133 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001134 if (!path)
1135 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001136
Chris Masone17cade2008-04-15 15:41:47 -04001137 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001138 key.offset = (u64)-1;
1139 key.type = BTRFS_CHUNK_ITEM_KEY;
1140
1141 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1142 if (ret < 0)
1143 goto error;
1144
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001145 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001146
1147 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1148 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001149 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001150 } else {
1151 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1152 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001153 if (found_key.objectid != objectid)
1154 *offset = 0;
1155 else {
1156 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1157 struct btrfs_chunk);
1158 *offset = found_key.offset +
1159 btrfs_chunk_length(path->nodes[0], chunk);
1160 }
Chris Mason0b86a832008-03-24 15:01:56 -04001161 }
1162 ret = 0;
1163error:
1164 btrfs_free_path(path);
1165 return ret;
1166}
1167
Yan Zheng2b820322008-11-17 21:11:30 -05001168static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001169{
1170 int ret;
1171 struct btrfs_key key;
1172 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001173 struct btrfs_path *path;
1174
1175 root = root->fs_info->chunk_root;
1176
1177 path = btrfs_alloc_path();
1178 if (!path)
1179 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001180
1181 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1182 key.type = BTRFS_DEV_ITEM_KEY;
1183 key.offset = (u64)-1;
1184
1185 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1186 if (ret < 0)
1187 goto error;
1188
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001189 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001190
1191 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1192 BTRFS_DEV_ITEM_KEY);
1193 if (ret) {
1194 *objectid = 1;
1195 } else {
1196 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1197 path->slots[0]);
1198 *objectid = found_key.offset + 1;
1199 }
1200 ret = 0;
1201error:
Yan Zheng2b820322008-11-17 21:11:30 -05001202 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001203 return ret;
1204}
1205
1206/*
1207 * the device information is stored in the chunk root
1208 * the btrfs_device struct should be fully filled in
1209 */
1210int btrfs_add_device(struct btrfs_trans_handle *trans,
1211 struct btrfs_root *root,
1212 struct btrfs_device *device)
1213{
1214 int ret;
1215 struct btrfs_path *path;
1216 struct btrfs_dev_item *dev_item;
1217 struct extent_buffer *leaf;
1218 struct btrfs_key key;
1219 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001220
1221 root = root->fs_info->chunk_root;
1222
1223 path = btrfs_alloc_path();
1224 if (!path)
1225 return -ENOMEM;
1226
Chris Mason0b86a832008-03-24 15:01:56 -04001227 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1228 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001229 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001230
1231 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001232 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001233 if (ret)
1234 goto out;
1235
1236 leaf = path->nodes[0];
1237 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1238
1239 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001240 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001241 btrfs_set_device_type(leaf, dev_item, device->type);
1242 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1243 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1244 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001245 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1246 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001247 btrfs_set_device_group(leaf, dev_item, 0);
1248 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1249 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001250 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001251
Chris Mason0b86a832008-03-24 15:01:56 -04001252 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001253 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001254 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1255 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001256 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001257
Yan Zheng2b820322008-11-17 21:11:30 -05001258 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001259out:
1260 btrfs_free_path(path);
1261 return ret;
1262}
Chris Mason8f18cf12008-04-25 16:53:30 -04001263
Chris Masona061fc82008-05-07 11:43:44 -04001264static int btrfs_rm_dev_item(struct btrfs_root *root,
1265 struct btrfs_device *device)
1266{
1267 int ret;
1268 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001269 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001270 struct btrfs_trans_handle *trans;
1271
1272 root = root->fs_info->chunk_root;
1273
1274 path = btrfs_alloc_path();
1275 if (!path)
1276 return -ENOMEM;
1277
Yan, Zhenga22285a2010-05-16 10:48:46 -04001278 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001279 if (IS_ERR(trans)) {
1280 btrfs_free_path(path);
1281 return PTR_ERR(trans);
1282 }
Chris Masona061fc82008-05-07 11:43:44 -04001283 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1284 key.type = BTRFS_DEV_ITEM_KEY;
1285 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001286 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001287
1288 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1289 if (ret < 0)
1290 goto out;
1291
1292 if (ret > 0) {
1293 ret = -ENOENT;
1294 goto out;
1295 }
1296
1297 ret = btrfs_del_item(trans, root, path);
1298 if (ret)
1299 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001300out:
1301 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001302 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001303 btrfs_commit_transaction(trans, root);
1304 return ret;
1305}
1306
1307int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1308{
1309 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001310 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001311 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001312 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001313 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001314 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001315 u64 all_avail;
1316 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001317 u64 num_devices;
1318 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001319 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001320 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001321
Chris Masona061fc82008-05-07 11:43:44 -04001322 mutex_lock(&uuid_mutex);
1323
1324 all_avail = root->fs_info->avail_data_alloc_bits |
1325 root->fs_info->avail_system_alloc_bits |
1326 root->fs_info->avail_metadata_alloc_bits;
1327
1328 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001329 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001330 printk(KERN_ERR "btrfs: unable to go below four devices "
1331 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001332 ret = -EINVAL;
1333 goto out;
1334 }
1335
1336 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001337 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001338 printk(KERN_ERR "btrfs: unable to go below two "
1339 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001340 ret = -EINVAL;
1341 goto out;
1342 }
1343
Chris Masondfe25022008-05-13 13:46:40 -04001344 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001345 struct list_head *devices;
1346 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001347
Chris Masondfe25022008-05-13 13:46:40 -04001348 device = NULL;
1349 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001350 /*
1351 * It is safe to read the devices since the volume_mutex
1352 * is held.
1353 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001354 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001355 if (tmp->in_fs_metadata && !tmp->bdev) {
1356 device = tmp;
1357 break;
1358 }
1359 }
1360 bdev = NULL;
1361 bh = NULL;
1362 disk_super = NULL;
1363 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001364 printk(KERN_ERR "btrfs: no missing devices found to "
1365 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001366 goto out;
1367 }
Chris Masondfe25022008-05-13 13:46:40 -04001368 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001369 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1370 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001371 if (IS_ERR(bdev)) {
1372 ret = PTR_ERR(bdev);
1373 goto out;
1374 }
1375
Yan Zheng2b820322008-11-17 21:11:30 -05001376 set_blocksize(bdev, 4096);
Chris Mason3c4bb262012-03-27 18:56:56 -04001377 invalidate_bdev(bdev);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001378 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001379 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001380 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001381 goto error_close;
1382 }
1383 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001384 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001385 dev_uuid = disk_super->dev_item.uuid;
1386 device = btrfs_find_device(root, devid, dev_uuid,
1387 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001388 if (!device) {
1389 ret = -ENOENT;
1390 goto error_brelse;
1391 }
Chris Masondfe25022008-05-13 13:46:40 -04001392 }
Yan Zheng2b820322008-11-17 21:11:30 -05001393
1394 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001395 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1396 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001397 ret = -EINVAL;
1398 goto error_brelse;
1399 }
1400
1401 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001402 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001403 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001404 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001405 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001406 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001407 }
Chris Masona061fc82008-05-07 11:43:44 -04001408
1409 ret = btrfs_shrink_device(device, 0);
1410 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001411 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001412
Chris Masona061fc82008-05-07 11:43:44 -04001413 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1414 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001415 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001416
Josef Bacik2bf64752011-09-26 17:12:22 -04001417 spin_lock(&root->fs_info->free_chunk_lock);
1418 root->fs_info->free_chunk_space = device->total_bytes -
1419 device->bytes_used;
1420 spin_unlock(&root->fs_info->free_chunk_lock);
1421
Yan Zheng2b820322008-11-17 21:11:30 -05001422 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001423 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001424
1425 /*
1426 * the device list mutex makes sure that we don't change
1427 * the device list while someone else is writing out all
1428 * the device supers.
1429 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001430
1431 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001432 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001433 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001434
Yan Zhenge4404d62008-12-12 10:03:26 -05001435 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001436
Chris Masoncd02dca2010-12-13 14:56:23 -05001437 if (device->missing)
1438 root->fs_info->fs_devices->missing_devices--;
1439
Yan Zheng2b820322008-11-17 21:11:30 -05001440 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1441 struct btrfs_device, dev_list);
1442 if (device->bdev == root->fs_info->sb->s_bdev)
1443 root->fs_info->sb->s_bdev = next_device->bdev;
1444 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1445 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1446
Xiao Guangrong1f781602011-04-20 10:09:16 +00001447 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001448 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001449
1450 call_rcu(&device->rcu, free_device);
1451 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001452
David Sterba6c417612011-04-13 15:41:04 +02001453 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1454 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001455
Xiao Guangrong1f781602011-04-20 10:09:16 +00001456 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001457 struct btrfs_fs_devices *fs_devices;
1458 fs_devices = root->fs_info->fs_devices;
1459 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001460 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001461 break;
1462 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001463 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001464 fs_devices->seed = cur_devices->seed;
1465 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001466 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001467 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001468 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001469 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001470 }
1471
1472 /*
1473 * at this point, the device is zero sized. We want to
1474 * remove it from the devices list and zero out the old super
1475 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001476 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001477 /* make sure this device isn't detected as part of
1478 * the FS anymore
1479 */
1480 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1481 set_buffer_dirty(bh);
1482 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001483 }
Chris Masona061fc82008-05-07 11:43:44 -04001484
Chris Masona061fc82008-05-07 11:43:44 -04001485 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001486
1487error_brelse:
1488 brelse(bh);
1489error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001490 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001491 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001492out:
1493 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001494 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001495error_undo:
1496 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001497 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001498 list_add(&device->dev_alloc_list,
1499 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001500 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001501 root->fs_info->fs_devices->rw_devices++;
1502 }
1503 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001504}
1505
Yan Zheng2b820322008-11-17 21:11:30 -05001506/*
1507 * does all the dirty work required for changing file system's UUID.
1508 */
Li Zefan125ccb02011-12-08 15:07:24 +08001509static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001510{
1511 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1512 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001513 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001514 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001515 struct btrfs_device *device;
1516 u64 super_flags;
1517
1518 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001519 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001520 return -EINVAL;
1521
Yan Zhenge4404d62008-12-12 10:03:26 -05001522 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1523 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001524 return -ENOMEM;
1525
Yan Zhenge4404d62008-12-12 10:03:26 -05001526 old_devices = clone_fs_devices(fs_devices);
1527 if (IS_ERR(old_devices)) {
1528 kfree(seed_devices);
1529 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001530 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001531
Yan Zheng2b820322008-11-17 21:11:30 -05001532 list_add(&old_devices->list, &fs_uuids);
1533
Yan Zhenge4404d62008-12-12 10:03:26 -05001534 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1535 seed_devices->opened = 1;
1536 INIT_LIST_HEAD(&seed_devices->devices);
1537 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001538 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001539
1540 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001541 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1542 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001543 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1544
Yan Zhenge4404d62008-12-12 10:03:26 -05001545 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1546 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1547 device->fs_devices = seed_devices;
1548 }
1549
Yan Zheng2b820322008-11-17 21:11:30 -05001550 fs_devices->seeding = 0;
1551 fs_devices->num_devices = 0;
1552 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001553 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001554
1555 generate_random_uuid(fs_devices->fsid);
1556 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1557 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1558 super_flags = btrfs_super_flags(disk_super) &
1559 ~BTRFS_SUPER_FLAG_SEEDING;
1560 btrfs_set_super_flags(disk_super, super_flags);
1561
1562 return 0;
1563}
1564
1565/*
1566 * strore the expected generation for seed devices in device items.
1567 */
1568static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1569 struct btrfs_root *root)
1570{
1571 struct btrfs_path *path;
1572 struct extent_buffer *leaf;
1573 struct btrfs_dev_item *dev_item;
1574 struct btrfs_device *device;
1575 struct btrfs_key key;
1576 u8 fs_uuid[BTRFS_UUID_SIZE];
1577 u8 dev_uuid[BTRFS_UUID_SIZE];
1578 u64 devid;
1579 int ret;
1580
1581 path = btrfs_alloc_path();
1582 if (!path)
1583 return -ENOMEM;
1584
1585 root = root->fs_info->chunk_root;
1586 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1587 key.offset = 0;
1588 key.type = BTRFS_DEV_ITEM_KEY;
1589
1590 while (1) {
1591 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1592 if (ret < 0)
1593 goto error;
1594
1595 leaf = path->nodes[0];
1596next_slot:
1597 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1598 ret = btrfs_next_leaf(root, path);
1599 if (ret > 0)
1600 break;
1601 if (ret < 0)
1602 goto error;
1603 leaf = path->nodes[0];
1604 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001605 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001606 continue;
1607 }
1608
1609 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1610 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1611 key.type != BTRFS_DEV_ITEM_KEY)
1612 break;
1613
1614 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1615 struct btrfs_dev_item);
1616 devid = btrfs_device_id(leaf, dev_item);
1617 read_extent_buffer(leaf, dev_uuid,
1618 (unsigned long)btrfs_device_uuid(dev_item),
1619 BTRFS_UUID_SIZE);
1620 read_extent_buffer(leaf, fs_uuid,
1621 (unsigned long)btrfs_device_fsid(dev_item),
1622 BTRFS_UUID_SIZE);
1623 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001624 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001625
1626 if (device->fs_devices->seeding) {
1627 btrfs_set_device_generation(leaf, dev_item,
1628 device->generation);
1629 btrfs_mark_buffer_dirty(leaf);
1630 }
1631
1632 path->slots[0]++;
1633 goto next_slot;
1634 }
1635 ret = 0;
1636error:
1637 btrfs_free_path(path);
1638 return ret;
1639}
1640
Chris Mason788f20e2008-04-28 15:29:42 -04001641int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1642{
Josef Bacikd5e20032011-08-04 14:52:27 +00001643 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001644 struct btrfs_trans_handle *trans;
1645 struct btrfs_device *device;
1646 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001647 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001648 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001649 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001650 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001651 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001652 int ret = 0;
1653
Yan Zheng2b820322008-11-17 21:11:30 -05001654 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001655 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001656
Li Zefana5d16332011-12-07 20:08:40 -05001657 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001658 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001659 if (IS_ERR(bdev))
1660 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001661
Yan Zheng2b820322008-11-17 21:11:30 -05001662 if (root->fs_info->fs_devices->seeding) {
1663 seeding_dev = 1;
1664 down_write(&sb->s_umount);
1665 mutex_lock(&uuid_mutex);
1666 }
1667
Chris Mason8c8bee12008-09-29 11:19:10 -04001668 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001669
Chris Mason788f20e2008-04-28 15:29:42 -04001670 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001671 /*
1672 * we have the volume lock, so we don't need the extra
1673 * device list mutex while reading the list here.
1674 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001675 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001676 if (device->bdev == bdev) {
1677 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001678 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001679 }
1680 }
1681
1682 device = kzalloc(sizeof(*device), GFP_NOFS);
1683 if (!device) {
1684 /* we can safely leave the fs_devices entry around */
1685 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001686 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001687 }
1688
Josef Bacik606686e2012-06-04 14:03:51 -04001689 name = rcu_string_strdup(device_path, GFP_NOFS);
1690 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001691 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001692 ret = -ENOMEM;
1693 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001694 }
Josef Bacik606686e2012-06-04 14:03:51 -04001695 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001696
1697 ret = find_next_devid(root, &device->devid);
1698 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001699 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001700 kfree(device);
1701 goto error;
1702 }
1703
Yan, Zhenga22285a2010-05-16 10:48:46 -04001704 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001705 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001706 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001707 kfree(device);
1708 ret = PTR_ERR(trans);
1709 goto error;
1710 }
1711
Yan Zheng2b820322008-11-17 21:11:30 -05001712 lock_chunks(root);
1713
Josef Bacikd5e20032011-08-04 14:52:27 +00001714 q = bdev_get_queue(bdev);
1715 if (blk_queue_discard(q))
1716 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001717 device->writeable = 1;
1718 device->work.func = pending_bios_fn;
1719 generate_random_uuid(device->uuid);
1720 spin_lock_init(&device->io_lock);
1721 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001722 device->io_width = root->sectorsize;
1723 device->io_align = root->sectorsize;
1724 device->sector_size = root->sectorsize;
1725 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001726 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001727 device->dev_root = root->fs_info->dev_root;
1728 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001729 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001730 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001731 set_blocksize(device->bdev, 4096);
1732
Yan Zheng2b820322008-11-17 21:11:30 -05001733 if (seeding_dev) {
1734 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001735 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001736 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001737 }
1738
1739 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001740
1741 /*
1742 * we don't want write_supers to jump in here with our device
1743 * half setup
1744 */
1745 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001746 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001747 list_add(&device->dev_alloc_list,
1748 &root->fs_info->fs_devices->alloc_list);
1749 root->fs_info->fs_devices->num_devices++;
1750 root->fs_info->fs_devices->open_devices++;
1751 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001752 if (device->can_discard)
1753 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001754 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1755
Josef Bacik2bf64752011-09-26 17:12:22 -04001756 spin_lock(&root->fs_info->free_chunk_lock);
1757 root->fs_info->free_chunk_space += device->total_bytes;
1758 spin_unlock(&root->fs_info->free_chunk_lock);
1759
Chris Masonc2898112009-06-10 09:51:32 -04001760 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1761 root->fs_info->fs_devices->rotating = 1;
1762
David Sterba6c417612011-04-13 15:41:04 +02001763 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1764 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001765 total_bytes + device->total_bytes);
1766
David Sterba6c417612011-04-13 15:41:04 +02001767 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1768 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001769 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001770 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001771
Yan Zheng2b820322008-11-17 21:11:30 -05001772 if (seeding_dev) {
1773 ret = init_first_rw_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001774 if (ret)
1775 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001776 ret = btrfs_finish_sprout(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001777 if (ret)
1778 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001779 } else {
1780 ret = btrfs_add_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001781 if (ret)
1782 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001783 }
1784
Chris Mason913d9522009-03-10 13:17:18 -04001785 /*
1786 * we've got more storage, clear any full flags on the space
1787 * infos
1788 */
1789 btrfs_clear_space_info_full(root->fs_info);
1790
Chris Mason7d9eb122008-07-08 14:19:17 -04001791 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001792 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001793
1794 if (seeding_dev) {
1795 mutex_unlock(&uuid_mutex);
1796 up_write(&sb->s_umount);
1797
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001798 if (ret) /* transaction commit */
1799 return ret;
1800
Yan Zheng2b820322008-11-17 21:11:30 -05001801 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001802 if (ret < 0)
1803 btrfs_error(root->fs_info, ret,
1804 "Failed to relocate sys chunks after "
1805 "device initialization. This can be fixed "
1806 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001807 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001808
Chris Mason788f20e2008-04-28 15:29:42 -04001809 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001810
1811error_trans:
1812 unlock_chunks(root);
1813 btrfs_abort_transaction(trans, root, ret);
1814 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04001815 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001816 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001817error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001818 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001819 if (seeding_dev) {
1820 mutex_unlock(&uuid_mutex);
1821 up_write(&sb->s_umount);
1822 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001823 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001824}
1825
Chris Masond3977122009-01-05 21:25:51 -05001826static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1827 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001828{
1829 int ret;
1830 struct btrfs_path *path;
1831 struct btrfs_root *root;
1832 struct btrfs_dev_item *dev_item;
1833 struct extent_buffer *leaf;
1834 struct btrfs_key key;
1835
1836 root = device->dev_root->fs_info->chunk_root;
1837
1838 path = btrfs_alloc_path();
1839 if (!path)
1840 return -ENOMEM;
1841
1842 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1843 key.type = BTRFS_DEV_ITEM_KEY;
1844 key.offset = device->devid;
1845
1846 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1847 if (ret < 0)
1848 goto out;
1849
1850 if (ret > 0) {
1851 ret = -ENOENT;
1852 goto out;
1853 }
1854
1855 leaf = path->nodes[0];
1856 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1857
1858 btrfs_set_device_id(leaf, dev_item, device->devid);
1859 btrfs_set_device_type(leaf, dev_item, device->type);
1860 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1861 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1862 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001863 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001864 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1865 btrfs_mark_buffer_dirty(leaf);
1866
1867out:
1868 btrfs_free_path(path);
1869 return ret;
1870}
1871
Chris Mason7d9eb122008-07-08 14:19:17 -04001872static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001873 struct btrfs_device *device, u64 new_size)
1874{
1875 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001876 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001877 u64 old_total = btrfs_super_total_bytes(super_copy);
1878 u64 diff = new_size - device->total_bytes;
1879
Yan Zheng2b820322008-11-17 21:11:30 -05001880 if (!device->writeable)
1881 return -EACCES;
1882 if (new_size <= device->total_bytes)
1883 return -EINVAL;
1884
Chris Mason8f18cf12008-04-25 16:53:30 -04001885 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001886 device->fs_devices->total_rw_bytes += diff;
1887
1888 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001889 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001890 btrfs_clear_space_info_full(device->dev_root->fs_info);
1891
Chris Mason8f18cf12008-04-25 16:53:30 -04001892 return btrfs_update_device(trans, device);
1893}
1894
Chris Mason7d9eb122008-07-08 14:19:17 -04001895int btrfs_grow_device(struct btrfs_trans_handle *trans,
1896 struct btrfs_device *device, u64 new_size)
1897{
1898 int ret;
1899 lock_chunks(device->dev_root);
1900 ret = __btrfs_grow_device(trans, device, new_size);
1901 unlock_chunks(device->dev_root);
1902 return ret;
1903}
1904
Chris Mason8f18cf12008-04-25 16:53:30 -04001905static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1906 struct btrfs_root *root,
1907 u64 chunk_tree, u64 chunk_objectid,
1908 u64 chunk_offset)
1909{
1910 int ret;
1911 struct btrfs_path *path;
1912 struct btrfs_key key;
1913
1914 root = root->fs_info->chunk_root;
1915 path = btrfs_alloc_path();
1916 if (!path)
1917 return -ENOMEM;
1918
1919 key.objectid = chunk_objectid;
1920 key.offset = chunk_offset;
1921 key.type = BTRFS_CHUNK_ITEM_KEY;
1922
1923 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 if (ret < 0)
1925 goto out;
1926 else if (ret > 0) { /* Logic error or corruption */
1927 btrfs_error(root->fs_info, -ENOENT,
1928 "Failed lookup while freeing chunk.");
1929 ret = -ENOENT;
1930 goto out;
1931 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001932
1933 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001934 if (ret < 0)
1935 btrfs_error(root->fs_info, ret,
1936 "Failed to delete chunk item.");
1937out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001938 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001939 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001940}
1941
Christoph Hellwigb2950862008-12-02 09:54:17 -05001942static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001943 chunk_offset)
1944{
David Sterba6c417612011-04-13 15:41:04 +02001945 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001946 struct btrfs_disk_key *disk_key;
1947 struct btrfs_chunk *chunk;
1948 u8 *ptr;
1949 int ret = 0;
1950 u32 num_stripes;
1951 u32 array_size;
1952 u32 len = 0;
1953 u32 cur;
1954 struct btrfs_key key;
1955
1956 array_size = btrfs_super_sys_array_size(super_copy);
1957
1958 ptr = super_copy->sys_chunk_array;
1959 cur = 0;
1960
1961 while (cur < array_size) {
1962 disk_key = (struct btrfs_disk_key *)ptr;
1963 btrfs_disk_key_to_cpu(&key, disk_key);
1964
1965 len = sizeof(*disk_key);
1966
1967 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1968 chunk = (struct btrfs_chunk *)(ptr + len);
1969 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1970 len += btrfs_chunk_item_size(num_stripes);
1971 } else {
1972 ret = -EIO;
1973 break;
1974 }
1975 if (key.objectid == chunk_objectid &&
1976 key.offset == chunk_offset) {
1977 memmove(ptr, ptr + len, array_size - (cur + len));
1978 array_size -= len;
1979 btrfs_set_super_sys_array_size(super_copy, array_size);
1980 } else {
1981 ptr += len;
1982 cur += len;
1983 }
1984 }
1985 return ret;
1986}
1987
Christoph Hellwigb2950862008-12-02 09:54:17 -05001988static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001989 u64 chunk_tree, u64 chunk_objectid,
1990 u64 chunk_offset)
1991{
1992 struct extent_map_tree *em_tree;
1993 struct btrfs_root *extent_root;
1994 struct btrfs_trans_handle *trans;
1995 struct extent_map *em;
1996 struct map_lookup *map;
1997 int ret;
1998 int i;
1999
2000 root = root->fs_info->chunk_root;
2001 extent_root = root->fs_info->extent_root;
2002 em_tree = &root->fs_info->mapping_tree.map_tree;
2003
Josef Bacikba1bf482009-09-11 16:11:19 -04002004 ret = btrfs_can_relocate(extent_root, chunk_offset);
2005 if (ret)
2006 return -ENOSPC;
2007
Chris Mason8f18cf12008-04-25 16:53:30 -04002008 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002009 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002010 if (ret)
2011 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002012
Yan, Zhenga22285a2010-05-16 10:48:46 -04002013 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002014 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002015
Chris Mason7d9eb122008-07-08 14:19:17 -04002016 lock_chunks(root);
2017
Chris Mason8f18cf12008-04-25 16:53:30 -04002018 /*
2019 * step two, delete the device extents and the
2020 * chunk tree entries
2021 */
Chris Mason890871b2009-09-02 16:24:52 -04002022 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002023 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002024 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002025
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002026 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002027 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002028 map = (struct map_lookup *)em->bdev;
2029
2030 for (i = 0; i < map->num_stripes; i++) {
2031 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2032 map->stripes[i].physical);
2033 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002034
Chris Masondfe25022008-05-13 13:46:40 -04002035 if (map->stripes[i].dev) {
2036 ret = btrfs_update_device(trans, map->stripes[i].dev);
2037 BUG_ON(ret);
2038 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002039 }
2040 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2041 chunk_offset);
2042
2043 BUG_ON(ret);
2044
liubo1abe9b82011-03-24 11:18:59 +00002045 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2046
Chris Mason8f18cf12008-04-25 16:53:30 -04002047 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2048 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2049 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002050 }
2051
Zheng Yan1a40e232008-09-26 10:09:34 -04002052 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2053 BUG_ON(ret);
2054
Chris Mason890871b2009-09-02 16:24:52 -04002055 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002056 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002057 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002058
Chris Mason8f18cf12008-04-25 16:53:30 -04002059 kfree(map);
2060 em->bdev = NULL;
2061
2062 /* once for the tree */
2063 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002064 /* once for us */
2065 free_extent_map(em);
2066
Chris Mason7d9eb122008-07-08 14:19:17 -04002067 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002068 btrfs_end_transaction(trans, root);
2069 return 0;
2070}
2071
Yan Zheng2b820322008-11-17 21:11:30 -05002072static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2073{
2074 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2075 struct btrfs_path *path;
2076 struct extent_buffer *leaf;
2077 struct btrfs_chunk *chunk;
2078 struct btrfs_key key;
2079 struct btrfs_key found_key;
2080 u64 chunk_tree = chunk_root->root_key.objectid;
2081 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002082 bool retried = false;
2083 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002084 int ret;
2085
2086 path = btrfs_alloc_path();
2087 if (!path)
2088 return -ENOMEM;
2089
Josef Bacikba1bf482009-09-11 16:11:19 -04002090again:
Yan Zheng2b820322008-11-17 21:11:30 -05002091 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2092 key.offset = (u64)-1;
2093 key.type = BTRFS_CHUNK_ITEM_KEY;
2094
2095 while (1) {
2096 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2097 if (ret < 0)
2098 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002099 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002100
2101 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2102 key.type);
2103 if (ret < 0)
2104 goto error;
2105 if (ret > 0)
2106 break;
2107
2108 leaf = path->nodes[0];
2109 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2110
2111 chunk = btrfs_item_ptr(leaf, path->slots[0],
2112 struct btrfs_chunk);
2113 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002114 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002115
2116 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2117 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2118 found_key.objectid,
2119 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002120 if (ret == -ENOSPC)
2121 failed++;
2122 else if (ret)
2123 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002124 }
2125
2126 if (found_key.offset == 0)
2127 break;
2128 key.offset = found_key.offset - 1;
2129 }
2130 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002131 if (failed && !retried) {
2132 failed = 0;
2133 retried = true;
2134 goto again;
2135 } else if (failed && retried) {
2136 WARN_ON(1);
2137 ret = -ENOSPC;
2138 }
Yan Zheng2b820322008-11-17 21:11:30 -05002139error:
2140 btrfs_free_path(path);
2141 return ret;
2142}
2143
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002144static int insert_balance_item(struct btrfs_root *root,
2145 struct btrfs_balance_control *bctl)
2146{
2147 struct btrfs_trans_handle *trans;
2148 struct btrfs_balance_item *item;
2149 struct btrfs_disk_balance_args disk_bargs;
2150 struct btrfs_path *path;
2151 struct extent_buffer *leaf;
2152 struct btrfs_key key;
2153 int ret, err;
2154
2155 path = btrfs_alloc_path();
2156 if (!path)
2157 return -ENOMEM;
2158
2159 trans = btrfs_start_transaction(root, 0);
2160 if (IS_ERR(trans)) {
2161 btrfs_free_path(path);
2162 return PTR_ERR(trans);
2163 }
2164
2165 key.objectid = BTRFS_BALANCE_OBJECTID;
2166 key.type = BTRFS_BALANCE_ITEM_KEY;
2167 key.offset = 0;
2168
2169 ret = btrfs_insert_empty_item(trans, root, path, &key,
2170 sizeof(*item));
2171 if (ret)
2172 goto out;
2173
2174 leaf = path->nodes[0];
2175 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2176
2177 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2178
2179 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2180 btrfs_set_balance_data(leaf, item, &disk_bargs);
2181 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2182 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2183 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2184 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2185
2186 btrfs_set_balance_flags(leaf, item, bctl->flags);
2187
2188 btrfs_mark_buffer_dirty(leaf);
2189out:
2190 btrfs_free_path(path);
2191 err = btrfs_commit_transaction(trans, root);
2192 if (err && !ret)
2193 ret = err;
2194 return ret;
2195}
2196
2197static int del_balance_item(struct btrfs_root *root)
2198{
2199 struct btrfs_trans_handle *trans;
2200 struct btrfs_path *path;
2201 struct btrfs_key key;
2202 int ret, err;
2203
2204 path = btrfs_alloc_path();
2205 if (!path)
2206 return -ENOMEM;
2207
2208 trans = btrfs_start_transaction(root, 0);
2209 if (IS_ERR(trans)) {
2210 btrfs_free_path(path);
2211 return PTR_ERR(trans);
2212 }
2213
2214 key.objectid = BTRFS_BALANCE_OBJECTID;
2215 key.type = BTRFS_BALANCE_ITEM_KEY;
2216 key.offset = 0;
2217
2218 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2219 if (ret < 0)
2220 goto out;
2221 if (ret > 0) {
2222 ret = -ENOENT;
2223 goto out;
2224 }
2225
2226 ret = btrfs_del_item(trans, root, path);
2227out:
2228 btrfs_free_path(path);
2229 err = btrfs_commit_transaction(trans, root);
2230 if (err && !ret)
2231 ret = err;
2232 return ret;
2233}
2234
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002235/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002236 * This is a heuristic used to reduce the number of chunks balanced on
2237 * resume after balance was interrupted.
2238 */
2239static void update_balance_args(struct btrfs_balance_control *bctl)
2240{
2241 /*
2242 * Turn on soft mode for chunk types that were being converted.
2243 */
2244 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2245 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2246 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2247 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2248 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2249 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2250
2251 /*
2252 * Turn on usage filter if is not already used. The idea is
2253 * that chunks that we have already balanced should be
2254 * reasonably full. Don't do it for chunks that are being
2255 * converted - that will keep us from relocating unconverted
2256 * (albeit full) chunks.
2257 */
2258 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2259 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2260 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2261 bctl->data.usage = 90;
2262 }
2263 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2264 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2265 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2266 bctl->sys.usage = 90;
2267 }
2268 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2269 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2270 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2271 bctl->meta.usage = 90;
2272 }
2273}
2274
2275/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002276 * Should be called with both balance and volume mutexes held to
2277 * serialize other volume operations (add_dev/rm_dev/resize) with
2278 * restriper. Same goes for unset_balance_control.
2279 */
2280static void set_balance_control(struct btrfs_balance_control *bctl)
2281{
2282 struct btrfs_fs_info *fs_info = bctl->fs_info;
2283
2284 BUG_ON(fs_info->balance_ctl);
2285
2286 spin_lock(&fs_info->balance_lock);
2287 fs_info->balance_ctl = bctl;
2288 spin_unlock(&fs_info->balance_lock);
2289}
2290
2291static void unset_balance_control(struct btrfs_fs_info *fs_info)
2292{
2293 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2294
2295 BUG_ON(!fs_info->balance_ctl);
2296
2297 spin_lock(&fs_info->balance_lock);
2298 fs_info->balance_ctl = NULL;
2299 spin_unlock(&fs_info->balance_lock);
2300
2301 kfree(bctl);
2302}
2303
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002304/*
2305 * Balance filters. Return 1 if chunk should be filtered out
2306 * (should not be balanced).
2307 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002308static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002309 struct btrfs_balance_args *bargs)
2310{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002311 chunk_type = chunk_to_extended(chunk_type) &
2312 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002313
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002314 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002315 return 0;
2316
2317 return 1;
2318}
2319
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002320static u64 div_factor_fine(u64 num, int factor)
2321{
2322 if (factor <= 0)
2323 return 0;
2324 if (factor >= 100)
2325 return num;
2326
2327 num *= factor;
2328 do_div(num, 100);
2329 return num;
2330}
2331
2332static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2333 struct btrfs_balance_args *bargs)
2334{
2335 struct btrfs_block_group_cache *cache;
2336 u64 chunk_used, user_thresh;
2337 int ret = 1;
2338
2339 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2340 chunk_used = btrfs_block_group_used(&cache->item);
2341
2342 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2343 if (chunk_used < user_thresh)
2344 ret = 0;
2345
2346 btrfs_put_block_group(cache);
2347 return ret;
2348}
2349
Ilya Dryomov409d4042012-01-16 22:04:47 +02002350static int chunk_devid_filter(struct extent_buffer *leaf,
2351 struct btrfs_chunk *chunk,
2352 struct btrfs_balance_args *bargs)
2353{
2354 struct btrfs_stripe *stripe;
2355 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2356 int i;
2357
2358 for (i = 0; i < num_stripes; i++) {
2359 stripe = btrfs_stripe_nr(chunk, i);
2360 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2361 return 0;
2362 }
2363
2364 return 1;
2365}
2366
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002367/* [pstart, pend) */
2368static int chunk_drange_filter(struct extent_buffer *leaf,
2369 struct btrfs_chunk *chunk,
2370 u64 chunk_offset,
2371 struct btrfs_balance_args *bargs)
2372{
2373 struct btrfs_stripe *stripe;
2374 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2375 u64 stripe_offset;
2376 u64 stripe_length;
2377 int factor;
2378 int i;
2379
2380 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2381 return 0;
2382
2383 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2384 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2385 factor = 2;
2386 else
2387 factor = 1;
2388 factor = num_stripes / factor;
2389
2390 for (i = 0; i < num_stripes; i++) {
2391 stripe = btrfs_stripe_nr(chunk, i);
2392 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2393 continue;
2394
2395 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2396 stripe_length = btrfs_chunk_length(leaf, chunk);
2397 do_div(stripe_length, factor);
2398
2399 if (stripe_offset < bargs->pend &&
2400 stripe_offset + stripe_length > bargs->pstart)
2401 return 0;
2402 }
2403
2404 return 1;
2405}
2406
Ilya Dryomovea671762012-01-16 22:04:48 +02002407/* [vstart, vend) */
2408static int chunk_vrange_filter(struct extent_buffer *leaf,
2409 struct btrfs_chunk *chunk,
2410 u64 chunk_offset,
2411 struct btrfs_balance_args *bargs)
2412{
2413 if (chunk_offset < bargs->vend &&
2414 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2415 /* at least part of the chunk is inside this vrange */
2416 return 0;
2417
2418 return 1;
2419}
2420
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002421static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002422 struct btrfs_balance_args *bargs)
2423{
2424 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2425 return 0;
2426
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002427 chunk_type = chunk_to_extended(chunk_type) &
2428 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002429
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002430 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002431 return 1;
2432
2433 return 0;
2434}
2435
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002436static int should_balance_chunk(struct btrfs_root *root,
2437 struct extent_buffer *leaf,
2438 struct btrfs_chunk *chunk, u64 chunk_offset)
2439{
2440 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2441 struct btrfs_balance_args *bargs = NULL;
2442 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2443
2444 /* type filter */
2445 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2446 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2447 return 0;
2448 }
2449
2450 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2451 bargs = &bctl->data;
2452 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2453 bargs = &bctl->sys;
2454 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2455 bargs = &bctl->meta;
2456
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002457 /* profiles filter */
2458 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2459 chunk_profiles_filter(chunk_type, bargs)) {
2460 return 0;
2461 }
2462
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002463 /* usage filter */
2464 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2465 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2466 return 0;
2467 }
2468
Ilya Dryomov409d4042012-01-16 22:04:47 +02002469 /* devid filter */
2470 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2471 chunk_devid_filter(leaf, chunk, bargs)) {
2472 return 0;
2473 }
2474
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002475 /* drange filter, makes sense only with devid filter */
2476 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2477 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2478 return 0;
2479 }
2480
Ilya Dryomovea671762012-01-16 22:04:48 +02002481 /* vrange filter */
2482 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2483 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2484 return 0;
2485 }
2486
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002487 /* soft profile changing mode */
2488 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2489 chunk_soft_convert_filter(chunk_type, bargs)) {
2490 return 0;
2491 }
2492
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002493 return 1;
2494}
2495
Chris Masonec44a352008-04-28 15:29:52 -04002496static u64 div_factor(u64 num, int factor)
2497{
2498 if (factor == 10)
2499 return num;
2500 num *= factor;
2501 do_div(num, 10);
2502 return num;
2503}
2504
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002505static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002506{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002507 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002508 struct btrfs_root *chunk_root = fs_info->chunk_root;
2509 struct btrfs_root *dev_root = fs_info->dev_root;
2510 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002511 struct btrfs_device *device;
2512 u64 old_size;
2513 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002514 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002515 struct btrfs_path *path;
2516 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002517 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002518 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002519 struct extent_buffer *leaf;
2520 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002521 int ret;
2522 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002523 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002524
Chris Masonec44a352008-04-28 15:29:52 -04002525 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002526 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002527 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002528 old_size = device->total_bytes;
2529 size_to_free = div_factor(old_size, 1);
2530 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002531 if (!device->writeable ||
2532 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002533 continue;
2534
2535 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002536 if (ret == -ENOSPC)
2537 break;
Chris Masonec44a352008-04-28 15:29:52 -04002538 BUG_ON(ret);
2539
Yan, Zhenga22285a2010-05-16 10:48:46 -04002540 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002541 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002542
2543 ret = btrfs_grow_device(trans, device, old_size);
2544 BUG_ON(ret);
2545
2546 btrfs_end_transaction(trans, dev_root);
2547 }
2548
2549 /* step two, relocate all the chunks */
2550 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002551 if (!path) {
2552 ret = -ENOMEM;
2553 goto error;
2554 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002555
2556 /* zero out stat counters */
2557 spin_lock(&fs_info->balance_lock);
2558 memset(&bctl->stat, 0, sizeof(bctl->stat));
2559 spin_unlock(&fs_info->balance_lock);
2560again:
Chris Masonec44a352008-04-28 15:29:52 -04002561 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2562 key.offset = (u64)-1;
2563 key.type = BTRFS_CHUNK_ITEM_KEY;
2564
Chris Masond3977122009-01-05 21:25:51 -05002565 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002566 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002567 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002568 ret = -ECANCELED;
2569 goto error;
2570 }
2571
Chris Masonec44a352008-04-28 15:29:52 -04002572 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2573 if (ret < 0)
2574 goto error;
2575
2576 /*
2577 * this shouldn't happen, it means the last relocate
2578 * failed
2579 */
2580 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002581 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002582
2583 ret = btrfs_previous_item(chunk_root, path, 0,
2584 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002585 if (ret) {
2586 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002587 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002588 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002589
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002590 leaf = path->nodes[0];
2591 slot = path->slots[0];
2592 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2593
Chris Masonec44a352008-04-28 15:29:52 -04002594 if (found_key.objectid != key.objectid)
2595 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002596
Chris Masonec44a352008-04-28 15:29:52 -04002597 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002598 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002599 break;
2600
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002601 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2602
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002603 if (!counting) {
2604 spin_lock(&fs_info->balance_lock);
2605 bctl->stat.considered++;
2606 spin_unlock(&fs_info->balance_lock);
2607 }
2608
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002609 ret = should_balance_chunk(chunk_root, leaf, chunk,
2610 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002611 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002612 if (!ret)
2613 goto loop;
2614
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002615 if (counting) {
2616 spin_lock(&fs_info->balance_lock);
2617 bctl->stat.expected++;
2618 spin_unlock(&fs_info->balance_lock);
2619 goto loop;
2620 }
2621
Chris Masonec44a352008-04-28 15:29:52 -04002622 ret = btrfs_relocate_chunk(chunk_root,
2623 chunk_root->root_key.objectid,
2624 found_key.objectid,
2625 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002626 if (ret && ret != -ENOSPC)
2627 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002628 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002629 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002630 } else {
2631 spin_lock(&fs_info->balance_lock);
2632 bctl->stat.completed++;
2633 spin_unlock(&fs_info->balance_lock);
2634 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002635loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002636 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002637 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002638
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002639 if (counting) {
2640 btrfs_release_path(path);
2641 counting = false;
2642 goto again;
2643 }
Chris Masonec44a352008-04-28 15:29:52 -04002644error:
2645 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002646 if (enospc_errors) {
2647 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2648 enospc_errors);
2649 if (!ret)
2650 ret = -ENOSPC;
2651 }
2652
Chris Masonec44a352008-04-28 15:29:52 -04002653 return ret;
2654}
2655
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002656/**
2657 * alloc_profile_is_valid - see if a given profile is valid and reduced
2658 * @flags: profile to validate
2659 * @extended: if true @flags is treated as an extended profile
2660 */
2661static int alloc_profile_is_valid(u64 flags, int extended)
2662{
2663 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2664 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2665
2666 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2667
2668 /* 1) check that all other bits are zeroed */
2669 if (flags & ~mask)
2670 return 0;
2671
2672 /* 2) see if profile is reduced */
2673 if (flags == 0)
2674 return !extended; /* "0" is valid for usual profiles */
2675
2676 /* true if exactly one bit set */
2677 return (flags & (flags - 1)) == 0;
2678}
2679
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002680static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2681{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002682 /* cancel requested || normal exit path */
2683 return atomic_read(&fs_info->balance_cancel_req) ||
2684 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2685 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002686}
2687
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002688static void __cancel_balance(struct btrfs_fs_info *fs_info)
2689{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002690 int ret;
2691
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002692 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002693 ret = del_balance_item(fs_info->tree_root);
2694 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002695}
2696
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002697void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002698 struct btrfs_ioctl_balance_args *bargs);
2699
2700/*
2701 * Should be called with both balance and volume mutexes held
2702 */
2703int btrfs_balance(struct btrfs_balance_control *bctl,
2704 struct btrfs_ioctl_balance_args *bargs)
2705{
2706 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002707 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002708 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002709 int ret;
2710
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002711 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002712 atomic_read(&fs_info->balance_pause_req) ||
2713 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002714 ret = -EINVAL;
2715 goto out;
2716 }
2717
Ilya Dryomove4837f82012-03-27 17:09:17 +03002718 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2719 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2720 mixed = 1;
2721
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002722 /*
2723 * In case of mixed groups both data and meta should be picked,
2724 * and identical options should be given for both of them.
2725 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002726 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2727 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002728 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2729 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2730 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2731 printk(KERN_ERR "btrfs: with mixed groups data and "
2732 "metadata balance options must be the same\n");
2733 ret = -EINVAL;
2734 goto out;
2735 }
2736 }
2737
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002738 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2739 if (fs_info->fs_devices->num_devices == 1)
2740 allowed |= BTRFS_BLOCK_GROUP_DUP;
2741 else if (fs_info->fs_devices->num_devices < 4)
2742 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2743 else
2744 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2745 BTRFS_BLOCK_GROUP_RAID10);
2746
Ilya Dryomov6728b192012-03-27 17:09:17 +03002747 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2748 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2749 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002750 printk(KERN_ERR "btrfs: unable to start balance with target "
2751 "data profile %llu\n",
2752 (unsigned long long)bctl->data.target);
2753 ret = -EINVAL;
2754 goto out;
2755 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002756 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2757 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2758 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002759 printk(KERN_ERR "btrfs: unable to start balance with target "
2760 "metadata profile %llu\n",
2761 (unsigned long long)bctl->meta.target);
2762 ret = -EINVAL;
2763 goto out;
2764 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002765 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2766 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2767 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002768 printk(KERN_ERR "btrfs: unable to start balance with target "
2769 "system profile %llu\n",
2770 (unsigned long long)bctl->sys.target);
2771 ret = -EINVAL;
2772 goto out;
2773 }
2774
Ilya Dryomove4837f82012-03-27 17:09:17 +03002775 /* allow dup'ed data chunks only in mixed mode */
2776 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002777 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002778 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2779 ret = -EINVAL;
2780 goto out;
2781 }
2782
2783 /* allow to reduce meta or sys integrity only if force set */
2784 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2785 BTRFS_BLOCK_GROUP_RAID10;
2786 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2787 (fs_info->avail_system_alloc_bits & allowed) &&
2788 !(bctl->sys.target & allowed)) ||
2789 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2790 (fs_info->avail_metadata_alloc_bits & allowed) &&
2791 !(bctl->meta.target & allowed))) {
2792 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2793 printk(KERN_INFO "btrfs: force reducing metadata "
2794 "integrity\n");
2795 } else {
2796 printk(KERN_ERR "btrfs: balance will reduce metadata "
2797 "integrity, use force if you want this\n");
2798 ret = -EINVAL;
2799 goto out;
2800 }
2801 }
2802
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002803 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002804 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002805 goto out;
2806
Ilya Dryomov59641012012-01-16 22:04:48 +02002807 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2808 BUG_ON(ret == -EEXIST);
2809 set_balance_control(bctl);
2810 } else {
2811 BUG_ON(ret != -EEXIST);
2812 spin_lock(&fs_info->balance_lock);
2813 update_balance_args(bctl);
2814 spin_unlock(&fs_info->balance_lock);
2815 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002816
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002817 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002818 mutex_unlock(&fs_info->balance_mutex);
2819
2820 ret = __btrfs_balance(fs_info);
2821
2822 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002823 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002824
2825 if (bargs) {
2826 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002827 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002828 }
2829
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002830 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2831 balance_need_close(fs_info)) {
2832 __cancel_balance(fs_info);
2833 }
2834
2835 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002836
2837 return ret;
2838out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002839 if (bctl->flags & BTRFS_BALANCE_RESUME)
2840 __cancel_balance(fs_info);
2841 else
2842 kfree(bctl);
2843 return ret;
2844}
2845
2846static int balance_kthread(void *data)
2847{
2848 struct btrfs_balance_control *bctl =
2849 (struct btrfs_balance_control *)data;
2850 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002851 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002852
2853 mutex_lock(&fs_info->volume_mutex);
2854 mutex_lock(&fs_info->balance_mutex);
2855
2856 set_balance_control(bctl);
2857
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002858 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2859 printk(KERN_INFO "btrfs: force skipping balance\n");
2860 } else {
2861 printk(KERN_INFO "btrfs: continuing balance\n");
2862 ret = btrfs_balance(bctl, NULL);
2863 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002864
2865 mutex_unlock(&fs_info->balance_mutex);
2866 mutex_unlock(&fs_info->volume_mutex);
2867 return ret;
2868}
2869
Ilya Dryomov68310a52012-06-22 12:24:12 -06002870int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02002871{
Ilya Dryomov59641012012-01-16 22:04:48 +02002872 struct btrfs_balance_control *bctl;
2873 struct btrfs_balance_item *item;
2874 struct btrfs_disk_balance_args disk_bargs;
2875 struct btrfs_path *path;
2876 struct extent_buffer *leaf;
2877 struct btrfs_key key;
2878 int ret;
2879
2880 path = btrfs_alloc_path();
2881 if (!path)
2882 return -ENOMEM;
2883
Ilya Dryomov68310a52012-06-22 12:24:12 -06002884 key.objectid = BTRFS_BALANCE_OBJECTID;
2885 key.type = BTRFS_BALANCE_ITEM_KEY;
2886 key.offset = 0;
2887
2888 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2889 if (ret < 0)
2890 goto out;
2891 if (ret > 0) { /* ret = -ENOENT; */
2892 ret = 0;
2893 goto out;
2894 }
2895
Ilya Dryomov59641012012-01-16 22:04:48 +02002896 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2897 if (!bctl) {
2898 ret = -ENOMEM;
2899 goto out;
2900 }
2901
Ilya Dryomov59641012012-01-16 22:04:48 +02002902 leaf = path->nodes[0];
2903 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2904
Ilya Dryomov68310a52012-06-22 12:24:12 -06002905 bctl->fs_info = fs_info;
2906 bctl->flags = btrfs_balance_flags(leaf, item);
2907 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02002908
2909 btrfs_balance_data(leaf, item, &disk_bargs);
2910 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2911 btrfs_balance_meta(leaf, item, &disk_bargs);
2912 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2913 btrfs_balance_sys(leaf, item, &disk_bargs);
2914 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2915
Ilya Dryomov68310a52012-06-22 12:24:12 -06002916 mutex_lock(&fs_info->volume_mutex);
2917 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02002918
Ilya Dryomov68310a52012-06-22 12:24:12 -06002919 set_balance_control(bctl);
2920
2921 mutex_unlock(&fs_info->balance_mutex);
2922 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02002923out:
2924 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002925 return ret;
2926}
2927
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002928int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2929{
2930 int ret = 0;
2931
2932 mutex_lock(&fs_info->balance_mutex);
2933 if (!fs_info->balance_ctl) {
2934 mutex_unlock(&fs_info->balance_mutex);
2935 return -ENOTCONN;
2936 }
2937
2938 if (atomic_read(&fs_info->balance_running)) {
2939 atomic_inc(&fs_info->balance_pause_req);
2940 mutex_unlock(&fs_info->balance_mutex);
2941
2942 wait_event(fs_info->balance_wait_q,
2943 atomic_read(&fs_info->balance_running) == 0);
2944
2945 mutex_lock(&fs_info->balance_mutex);
2946 /* we are good with balance_ctl ripped off from under us */
2947 BUG_ON(atomic_read(&fs_info->balance_running));
2948 atomic_dec(&fs_info->balance_pause_req);
2949 } else {
2950 ret = -ENOTCONN;
2951 }
2952
2953 mutex_unlock(&fs_info->balance_mutex);
2954 return ret;
2955}
2956
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002957int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2958{
2959 mutex_lock(&fs_info->balance_mutex);
2960 if (!fs_info->balance_ctl) {
2961 mutex_unlock(&fs_info->balance_mutex);
2962 return -ENOTCONN;
2963 }
2964
2965 atomic_inc(&fs_info->balance_cancel_req);
2966 /*
2967 * if we are running just wait and return, balance item is
2968 * deleted in btrfs_balance in this case
2969 */
2970 if (atomic_read(&fs_info->balance_running)) {
2971 mutex_unlock(&fs_info->balance_mutex);
2972 wait_event(fs_info->balance_wait_q,
2973 atomic_read(&fs_info->balance_running) == 0);
2974 mutex_lock(&fs_info->balance_mutex);
2975 } else {
2976 /* __cancel_balance needs volume_mutex */
2977 mutex_unlock(&fs_info->balance_mutex);
2978 mutex_lock(&fs_info->volume_mutex);
2979 mutex_lock(&fs_info->balance_mutex);
2980
2981 if (fs_info->balance_ctl)
2982 __cancel_balance(fs_info);
2983
2984 mutex_unlock(&fs_info->volume_mutex);
2985 }
2986
2987 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2988 atomic_dec(&fs_info->balance_cancel_req);
2989 mutex_unlock(&fs_info->balance_mutex);
2990 return 0;
2991}
2992
Chris Mason8f18cf12008-04-25 16:53:30 -04002993/*
2994 * shrinking a device means finding all of the device extents past
2995 * the new size, and then following the back refs to the chunks.
2996 * The chunk relocation code actually frees the device extent
2997 */
2998int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2999{
3000 struct btrfs_trans_handle *trans;
3001 struct btrfs_root *root = device->dev_root;
3002 struct btrfs_dev_extent *dev_extent = NULL;
3003 struct btrfs_path *path;
3004 u64 length;
3005 u64 chunk_tree;
3006 u64 chunk_objectid;
3007 u64 chunk_offset;
3008 int ret;
3009 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003010 int failed = 0;
3011 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003012 struct extent_buffer *l;
3013 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003014 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003015 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003016 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003017 u64 diff = device->total_bytes - new_size;
3018
Yan Zheng2b820322008-11-17 21:11:30 -05003019 if (new_size >= device->total_bytes)
3020 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04003021
3022 path = btrfs_alloc_path();
3023 if (!path)
3024 return -ENOMEM;
3025
Chris Mason8f18cf12008-04-25 16:53:30 -04003026 path->reada = 2;
3027
Chris Mason7d9eb122008-07-08 14:19:17 -04003028 lock_chunks(root);
3029
Chris Mason8f18cf12008-04-25 16:53:30 -04003030 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003031 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003032 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003033 spin_lock(&root->fs_info->free_chunk_lock);
3034 root->fs_info->free_chunk_space -= diff;
3035 spin_unlock(&root->fs_info->free_chunk_lock);
3036 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003037 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003038
Josef Bacikba1bf482009-09-11 16:11:19 -04003039again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003040 key.objectid = device->devid;
3041 key.offset = (u64)-1;
3042 key.type = BTRFS_DEV_EXTENT_KEY;
3043
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003044 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003045 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3046 if (ret < 0)
3047 goto done;
3048
3049 ret = btrfs_previous_item(root, path, 0, key.type);
3050 if (ret < 0)
3051 goto done;
3052 if (ret) {
3053 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003054 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003055 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003056 }
3057
3058 l = path->nodes[0];
3059 slot = path->slots[0];
3060 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3061
Josef Bacikba1bf482009-09-11 16:11:19 -04003062 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003063 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003064 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003065 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003066
3067 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3068 length = btrfs_dev_extent_length(l, dev_extent);
3069
Josef Bacikba1bf482009-09-11 16:11:19 -04003070 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003071 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003072 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003073 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003074
3075 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3076 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3077 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003078 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003079
3080 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3081 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003082 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003083 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003084 if (ret == -ENOSPC)
3085 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003086 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003087
3088 if (failed && !retried) {
3089 failed = 0;
3090 retried = true;
3091 goto again;
3092 } else if (failed && retried) {
3093 ret = -ENOSPC;
3094 lock_chunks(root);
3095
3096 device->total_bytes = old_size;
3097 if (device->writeable)
3098 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003099 spin_lock(&root->fs_info->free_chunk_lock);
3100 root->fs_info->free_chunk_space += diff;
3101 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003102 unlock_chunks(root);
3103 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003104 }
3105
Chris Balld6397ba2009-04-27 07:29:03 -04003106 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003107 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003108 if (IS_ERR(trans)) {
3109 ret = PTR_ERR(trans);
3110 goto done;
3111 }
3112
Chris Balld6397ba2009-04-27 07:29:03 -04003113 lock_chunks(root);
3114
3115 device->disk_total_bytes = new_size;
3116 /* Now btrfs_update_device() will change the on-disk size. */
3117 ret = btrfs_update_device(trans, device);
3118 if (ret) {
3119 unlock_chunks(root);
3120 btrfs_end_transaction(trans, root);
3121 goto done;
3122 }
3123 WARN_ON(diff > old_total);
3124 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3125 unlock_chunks(root);
3126 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003127done:
3128 btrfs_free_path(path);
3129 return ret;
3130}
3131
Li Zefan125ccb02011-12-08 15:07:24 +08003132static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003133 struct btrfs_key *key,
3134 struct btrfs_chunk *chunk, int item_size)
3135{
David Sterba6c417612011-04-13 15:41:04 +02003136 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003137 struct btrfs_disk_key disk_key;
3138 u32 array_size;
3139 u8 *ptr;
3140
3141 array_size = btrfs_super_sys_array_size(super_copy);
3142 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3143 return -EFBIG;
3144
3145 ptr = super_copy->sys_chunk_array + array_size;
3146 btrfs_cpu_key_to_disk(&disk_key, key);
3147 memcpy(ptr, &disk_key, sizeof(disk_key));
3148 ptr += sizeof(disk_key);
3149 memcpy(ptr, chunk, item_size);
3150 item_size += sizeof(disk_key);
3151 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3152 return 0;
3153}
3154
Miao Xieb2117a32011-01-05 10:07:28 +00003155/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003156 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003157 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003158static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003159{
Arne Jansen73c5de02011-04-12 12:07:57 +02003160 const struct btrfs_device_info *di_a = a;
3161 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003162
Arne Jansen73c5de02011-04-12 12:07:57 +02003163 if (di_a->max_avail > di_b->max_avail)
3164 return -1;
3165 if (di_a->max_avail < di_b->max_avail)
3166 return 1;
3167 if (di_a->total_avail > di_b->total_avail)
3168 return -1;
3169 if (di_a->total_avail < di_b->total_avail)
3170 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003171 return 0;
3172}
3173
3174static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3175 struct btrfs_root *extent_root,
3176 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003177 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003178 u64 start, u64 type)
3179{
3180 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003181 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3182 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003183 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003184 struct extent_map_tree *em_tree;
3185 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003186 struct btrfs_device_info *devices_info = NULL;
3187 u64 total_avail;
3188 int num_stripes; /* total number of stripes to allocate */
3189 int sub_stripes; /* sub_stripes info for map */
3190 int dev_stripes; /* stripes per dev */
3191 int devs_max; /* max devs to use */
3192 int devs_min; /* min devs needed */
3193 int devs_increment; /* ndevs has to be a multiple of this */
3194 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003195 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003196 u64 max_stripe_size;
3197 u64 max_chunk_size;
3198 u64 stripe_size;
3199 u64 num_bytes;
3200 int ndevs;
3201 int i;
3202 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003203
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003204 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003205
Miao Xieb2117a32011-01-05 10:07:28 +00003206 if (list_empty(&fs_devices->alloc_list))
3207 return -ENOSPC;
3208
Arne Jansen73c5de02011-04-12 12:07:57 +02003209 sub_stripes = 1;
3210 dev_stripes = 1;
3211 devs_increment = 1;
3212 ncopies = 1;
3213 devs_max = 0; /* 0 == as many as possible */
3214 devs_min = 1;
3215
3216 /*
3217 * define the properties of each RAID type.
3218 * FIXME: move this to a global table and use it in all RAID
3219 * calculation code
3220 */
3221 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3222 dev_stripes = 2;
3223 ncopies = 2;
3224 devs_max = 1;
3225 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3226 devs_min = 2;
3227 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3228 devs_increment = 2;
3229 ncopies = 2;
3230 devs_max = 2;
3231 devs_min = 2;
3232 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3233 sub_stripes = 2;
3234 devs_increment = 2;
3235 ncopies = 2;
3236 devs_min = 4;
3237 } else {
3238 devs_max = 1;
3239 }
3240
3241 if (type & BTRFS_BLOCK_GROUP_DATA) {
3242 max_stripe_size = 1024 * 1024 * 1024;
3243 max_chunk_size = 10 * max_stripe_size;
3244 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003245 /* for larger filesystems, use larger metadata chunks */
3246 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3247 max_stripe_size = 1024 * 1024 * 1024;
3248 else
3249 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003250 max_chunk_size = max_stripe_size;
3251 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003252 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003253 max_chunk_size = 2 * max_stripe_size;
3254 } else {
3255 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3256 type);
3257 BUG_ON(1);
3258 }
3259
3260 /* we don't want a chunk larger than 10% of writeable space */
3261 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3262 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003263
3264 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3265 GFP_NOFS);
3266 if (!devices_info)
3267 return -ENOMEM;
3268
Arne Jansen73c5de02011-04-12 12:07:57 +02003269 cur = fs_devices->alloc_list.next;
3270
3271 /*
3272 * in the first pass through the devices list, we gather information
3273 * about the available holes on each device.
3274 */
3275 ndevs = 0;
3276 while (cur != &fs_devices->alloc_list) {
3277 struct btrfs_device *device;
3278 u64 max_avail;
3279 u64 dev_offset;
3280
3281 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3282
3283 cur = cur->next;
3284
3285 if (!device->writeable) {
3286 printk(KERN_ERR
3287 "btrfs: read-only device in alloc_list\n");
3288 WARN_ON(1);
3289 continue;
3290 }
3291
3292 if (!device->in_fs_metadata)
3293 continue;
3294
3295 if (device->total_bytes > device->bytes_used)
3296 total_avail = device->total_bytes - device->bytes_used;
3297 else
3298 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003299
3300 /* If there is no space on this device, skip it. */
3301 if (total_avail == 0)
3302 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003303
Li Zefan125ccb02011-12-08 15:07:24 +08003304 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003305 max_stripe_size * dev_stripes,
3306 &dev_offset, &max_avail);
3307 if (ret && ret != -ENOSPC)
3308 goto error;
3309
3310 if (ret == 0)
3311 max_avail = max_stripe_size * dev_stripes;
3312
3313 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3314 continue;
3315
3316 devices_info[ndevs].dev_offset = dev_offset;
3317 devices_info[ndevs].max_avail = max_avail;
3318 devices_info[ndevs].total_avail = total_avail;
3319 devices_info[ndevs].dev = device;
3320 ++ndevs;
3321 }
3322
3323 /*
3324 * now sort the devices by hole size / available space
3325 */
3326 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3327 btrfs_cmp_device_info, NULL);
3328
3329 /* round down to number of usable stripes */
3330 ndevs -= ndevs % devs_increment;
3331
3332 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3333 ret = -ENOSPC;
3334 goto error;
3335 }
3336
3337 if (devs_max && ndevs > devs_max)
3338 ndevs = devs_max;
3339 /*
3340 * the primary goal is to maximize the number of stripes, so use as many
3341 * devices as possible, even if the stripes are not maximum sized.
3342 */
3343 stripe_size = devices_info[ndevs-1].max_avail;
3344 num_stripes = ndevs * dev_stripes;
3345
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003346 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003347 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003348 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003349 }
3350
3351 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003352
3353 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003354 do_div(stripe_size, BTRFS_STRIPE_LEN);
3355 stripe_size *= BTRFS_STRIPE_LEN;
3356
Miao Xieb2117a32011-01-05 10:07:28 +00003357 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3358 if (!map) {
3359 ret = -ENOMEM;
3360 goto error;
3361 }
3362 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003363
Arne Jansen73c5de02011-04-12 12:07:57 +02003364 for (i = 0; i < ndevs; ++i) {
3365 for (j = 0; j < dev_stripes; ++j) {
3366 int s = i * dev_stripes + j;
3367 map->stripes[s].dev = devices_info[i].dev;
3368 map->stripes[s].physical = devices_info[i].dev_offset +
3369 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003370 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003371 }
Chris Mason593060d2008-03-25 16:50:33 -04003372 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003373 map->stripe_len = BTRFS_STRIPE_LEN;
3374 map->io_align = BTRFS_STRIPE_LEN;
3375 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003376 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003377 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003378
Yan Zheng2b820322008-11-17 21:11:30 -05003379 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003380 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003381
Arne Jansen73c5de02011-04-12 12:07:57 +02003382 *stripe_size_out = stripe_size;
3383 *num_bytes_out = num_bytes;
3384
3385 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003386
David Sterba172ddd62011-04-21 00:48:27 +02003387 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003388 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003389 ret = -ENOMEM;
3390 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003391 }
Chris Mason0b86a832008-03-24 15:01:56 -04003392 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003393 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003394 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003395 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003396 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003397
Chris Mason0b86a832008-03-24 15:01:56 -04003398 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003399 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003400 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003401 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003402 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003403 if (ret)
3404 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003405
3406 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3407 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003408 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003409 if (ret)
3410 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003411
Arne Jansen73c5de02011-04-12 12:07:57 +02003412 for (i = 0; i < map->num_stripes; ++i) {
3413 struct btrfs_device *device;
3414 u64 dev_offset;
3415
3416 device = map->stripes[i].dev;
3417 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003418
3419 ret = btrfs_alloc_dev_extent(trans, device,
3420 info->chunk_root->root_key.objectid,
3421 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003422 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003423 if (ret) {
3424 btrfs_abort_transaction(trans, extent_root, ret);
3425 goto error;
3426 }
Yan Zheng2b820322008-11-17 21:11:30 -05003427 }
3428
Miao Xieb2117a32011-01-05 10:07:28 +00003429 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003430 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003431
3432error:
3433 kfree(map);
3434 kfree(devices_info);
3435 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003436}
3437
3438static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3439 struct btrfs_root *extent_root,
3440 struct map_lookup *map, u64 chunk_offset,
3441 u64 chunk_size, u64 stripe_size)
3442{
3443 u64 dev_offset;
3444 struct btrfs_key key;
3445 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3446 struct btrfs_device *device;
3447 struct btrfs_chunk *chunk;
3448 struct btrfs_stripe *stripe;
3449 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3450 int index = 0;
3451 int ret;
3452
3453 chunk = kzalloc(item_size, GFP_NOFS);
3454 if (!chunk)
3455 return -ENOMEM;
3456
3457 index = 0;
3458 while (index < map->num_stripes) {
3459 device = map->stripes[index].dev;
3460 device->bytes_used += stripe_size;
3461 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003462 if (ret)
3463 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003464 index++;
3465 }
3466
Josef Bacik2bf64752011-09-26 17:12:22 -04003467 spin_lock(&extent_root->fs_info->free_chunk_lock);
3468 extent_root->fs_info->free_chunk_space -= (stripe_size *
3469 map->num_stripes);
3470 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3471
Yan Zheng2b820322008-11-17 21:11:30 -05003472 index = 0;
3473 stripe = &chunk->stripe;
3474 while (index < map->num_stripes) {
3475 device = map->stripes[index].dev;
3476 dev_offset = map->stripes[index].physical;
3477
3478 btrfs_set_stack_stripe_devid(stripe, device->devid);
3479 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3480 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3481 stripe++;
3482 index++;
3483 }
3484
3485 btrfs_set_stack_chunk_length(chunk, chunk_size);
3486 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3487 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3488 btrfs_set_stack_chunk_type(chunk, map->type);
3489 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3490 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3491 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3492 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3493 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3494
3495 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3496 key.type = BTRFS_CHUNK_ITEM_KEY;
3497 key.offset = chunk_offset;
3498
3499 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003500
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003501 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3502 /*
3503 * TODO: Cleanup of inserted chunk root in case of
3504 * failure.
3505 */
Li Zefan125ccb02011-12-08 15:07:24 +08003506 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003507 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003508 }
liubo1abe9b82011-03-24 11:18:59 +00003509
Mark Fasheh3acd3952011-09-08 17:40:01 -07003510out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003511 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003512 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003513}
3514
3515/*
3516 * Chunk allocation falls into two parts. The first part does works
3517 * that make the new allocated chunk useable, but not do any operation
3518 * that modifies the chunk tree. The second part does the works that
3519 * require modifying the chunk tree. This division is important for the
3520 * bootstrap process of adding storage to a seed btrfs.
3521 */
3522int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3523 struct btrfs_root *extent_root, u64 type)
3524{
3525 u64 chunk_offset;
3526 u64 chunk_size;
3527 u64 stripe_size;
3528 struct map_lookup *map;
3529 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3530 int ret;
3531
3532 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3533 &chunk_offset);
3534 if (ret)
3535 return ret;
3536
3537 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3538 &stripe_size, chunk_offset, type);
3539 if (ret)
3540 return ret;
3541
3542 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3543 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003544 if (ret)
3545 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003546 return 0;
3547}
3548
Chris Masond3977122009-01-05 21:25:51 -05003549static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003550 struct btrfs_root *root,
3551 struct btrfs_device *device)
3552{
3553 u64 chunk_offset;
3554 u64 sys_chunk_offset;
3555 u64 chunk_size;
3556 u64 sys_chunk_size;
3557 u64 stripe_size;
3558 u64 sys_stripe_size;
3559 u64 alloc_profile;
3560 struct map_lookup *map;
3561 struct map_lookup *sys_map;
3562 struct btrfs_fs_info *fs_info = root->fs_info;
3563 struct btrfs_root *extent_root = fs_info->extent_root;
3564 int ret;
3565
3566 ret = find_next_chunk(fs_info->chunk_root,
3567 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003568 if (ret)
3569 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003570
3571 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003572 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003573 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3574
3575 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3576 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003577 if (ret)
3578 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003579
3580 sys_chunk_offset = chunk_offset + chunk_size;
3581
3582 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003583 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003584 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3585
3586 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3587 &sys_chunk_size, &sys_stripe_size,
3588 sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003589 if (ret)
3590 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003591
3592 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003593 if (ret)
3594 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003595
3596 /*
3597 * Modifying chunk tree needs allocating new blocks from both
3598 * system block group and metadata block group. So we only can
3599 * do operations require modifying the chunk tree after both
3600 * block groups were created.
3601 */
3602 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3603 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003604 if (ret)
3605 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003606
3607 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3608 sys_chunk_offset, sys_chunk_size,
3609 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003610 if (ret)
3611 goto abort;
3612
Yan Zheng2b820322008-11-17 21:11:30 -05003613 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003614
3615abort:
3616 btrfs_abort_transaction(trans, root, ret);
3617 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003618}
3619
3620int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3621{
3622 struct extent_map *em;
3623 struct map_lookup *map;
3624 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3625 int readonly = 0;
3626 int i;
3627
Chris Mason890871b2009-09-02 16:24:52 -04003628 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003629 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003630 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003631 if (!em)
3632 return 1;
3633
Josef Bacikf48b9072010-01-27 02:07:59 +00003634 if (btrfs_test_opt(root, DEGRADED)) {
3635 free_extent_map(em);
3636 return 0;
3637 }
3638
Yan Zheng2b820322008-11-17 21:11:30 -05003639 map = (struct map_lookup *)em->bdev;
3640 for (i = 0; i < map->num_stripes; i++) {
3641 if (!map->stripes[i].dev->writeable) {
3642 readonly = 1;
3643 break;
3644 }
3645 }
3646 free_extent_map(em);
3647 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003648}
3649
3650void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3651{
David Sterbaa8067e02011-04-21 00:34:43 +02003652 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003653}
3654
3655void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3656{
3657 struct extent_map *em;
3658
Chris Masond3977122009-01-05 21:25:51 -05003659 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003660 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003661 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3662 if (em)
3663 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003664 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003665 if (!em)
3666 break;
3667 kfree(em->bdev);
3668 /* once for us */
3669 free_extent_map(em);
3670 /* once for the tree */
3671 free_extent_map(em);
3672 }
3673}
3674
Chris Masonf1885912008-04-09 16:28:12 -04003675int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3676{
3677 struct extent_map *em;
3678 struct map_lookup *map;
3679 struct extent_map_tree *em_tree = &map_tree->map_tree;
3680 int ret;
3681
Chris Mason890871b2009-09-02 16:24:52 -04003682 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003683 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003684 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003685 BUG_ON(!em);
3686
3687 BUG_ON(em->start > logical || em->start + em->len < logical);
3688 map = (struct map_lookup *)em->bdev;
3689 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3690 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003691 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3692 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003693 else
3694 ret = 1;
3695 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003696 return ret;
3697}
3698
Chris Masondfe25022008-05-13 13:46:40 -04003699static int find_live_mirror(struct map_lookup *map, int first, int num,
3700 int optimal)
3701{
3702 int i;
3703 if (map->stripes[optimal].dev->bdev)
3704 return optimal;
3705 for (i = first; i < first + num; i++) {
3706 if (map->stripes[i].dev->bdev)
3707 return i;
3708 }
3709 /* we couldn't find one that doesn't fail. Just return something
3710 * and the io error handling code will clean up eventually
3711 */
3712 return optimal;
3713}
3714
Chris Masonf2d8d742008-04-21 10:03:05 -04003715static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3716 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003717 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003718 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003719{
3720 struct extent_map *em;
3721 struct map_lookup *map;
3722 struct extent_map_tree *em_tree = &map_tree->map_tree;
3723 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003724 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003725 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003726 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003727 u64 stripe_nr_orig;
3728 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003729 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003730 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003731 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003732 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003733 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003734 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003735
Chris Mason890871b2009-09-02 16:24:52 -04003736 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003737 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003738 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003739
Chris Mason3b951512008-04-17 11:29:12 -04003740 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003741 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3742 (unsigned long long)logical,
3743 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003744 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003745 }
Chris Mason0b86a832008-03-24 15:01:56 -04003746
3747 BUG_ON(em->start > logical || em->start + em->len < logical);
3748 map = (struct map_lookup *)em->bdev;
3749 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003750
Chris Masonf1885912008-04-09 16:28:12 -04003751 if (mirror_num > map->num_stripes)
3752 mirror_num = 0;
3753
Chris Mason593060d2008-03-25 16:50:33 -04003754 stripe_nr = offset;
3755 /*
3756 * stripe_nr counts the total number of stripes we have to stride
3757 * to get to this block
3758 */
3759 do_div(stripe_nr, map->stripe_len);
3760
3761 stripe_offset = stripe_nr * map->stripe_len;
3762 BUG_ON(offset < stripe_offset);
3763
3764 /* stripe_offset is the offset of this block in its stripe*/
3765 stripe_offset = offset - stripe_offset;
3766
Li Dongyangfce3bb92011-03-24 10:24:26 +00003767 if (rw & REQ_DISCARD)
3768 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003769 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003770 /* we limit the length of each bio to what fits in a stripe */
3771 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003772 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003773 } else {
3774 *length = em->len - offset;
3775 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003776
Jan Schmidta1d3c472011-08-04 17:15:33 +02003777 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003778 goto out;
3779
Chris Masonf2d8d742008-04-21 10:03:05 -04003780 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003781 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003782 stripe_nr_orig = stripe_nr;
3783 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3784 (~(map->stripe_len - 1));
3785 do_div(stripe_nr_end, map->stripe_len);
3786 stripe_end_offset = stripe_nr_end * map->stripe_len -
3787 (offset + *length);
3788 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3789 if (rw & REQ_DISCARD)
3790 num_stripes = min_t(u64, map->num_stripes,
3791 stripe_nr_end - stripe_nr_orig);
3792 stripe_index = do_div(stripe_nr, map->num_stripes);
3793 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003794 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003795 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003796 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003797 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003798 else {
3799 stripe_index = find_live_mirror(map, 0,
3800 map->num_stripes,
3801 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003802 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003803 }
Chris Mason2fff7342008-04-29 14:12:09 -04003804
Chris Mason611f0e02008-04-03 16:29:03 -04003805 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003806 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003807 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003808 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003809 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003810 } else {
3811 mirror_num = 1;
3812 }
Chris Mason2fff7342008-04-29 14:12:09 -04003813
Chris Mason321aecc2008-04-16 10:49:51 -04003814 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3815 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003816
3817 stripe_index = do_div(stripe_nr, factor);
3818 stripe_index *= map->sub_stripes;
3819
Jens Axboe7eaceac2011-03-10 08:52:07 +01003820 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003821 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003822 else if (rw & REQ_DISCARD)
3823 num_stripes = min_t(u64, map->sub_stripes *
3824 (stripe_nr_end - stripe_nr_orig),
3825 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003826 else if (mirror_num)
3827 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003828 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04003829 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04003830 stripe_index = find_live_mirror(map, stripe_index,
3831 map->sub_stripes, stripe_index +
3832 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04003833 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003834 }
Chris Mason8790d502008-04-03 16:29:03 -04003835 } else {
3836 /*
3837 * after this do_div call, stripe_nr is the number of stripes
3838 * on this device we have to walk to find the data, and
3839 * stripe_index is the number of our device in the stripe array
3840 */
3841 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003842 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003843 }
Chris Mason593060d2008-03-25 16:50:33 -04003844 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003845
Li Zefande11cc12011-12-01 12:55:47 +08003846 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3847 if (!bbio) {
3848 ret = -ENOMEM;
3849 goto out;
3850 }
3851 atomic_set(&bbio->error, 0);
3852
Li Dongyangfce3bb92011-03-24 10:24:26 +00003853 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003854 int factor = 0;
3855 int sub_stripes = 0;
3856 u64 stripes_per_dev = 0;
3857 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003858 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003859
3860 if (map->type &
3861 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3862 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3863 sub_stripes = 1;
3864 else
3865 sub_stripes = map->sub_stripes;
3866
3867 factor = map->num_stripes / sub_stripes;
3868 stripes_per_dev = div_u64_rem(stripe_nr_end -
3869 stripe_nr_orig,
3870 factor,
3871 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04003872 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3873 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003874 }
3875
Li Dongyangfce3bb92011-03-24 10:24:26 +00003876 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003877 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003878 map->stripes[stripe_index].physical +
3879 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003880 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003881
Li Zefanec9ef7a2011-12-01 14:06:42 +08003882 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3883 BTRFS_BLOCK_GROUP_RAID10)) {
3884 bbio->stripes[i].length = stripes_per_dev *
3885 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003886
Li Zefanec9ef7a2011-12-01 14:06:42 +08003887 if (i / sub_stripes < remaining_stripes)
3888 bbio->stripes[i].length +=
3889 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003890
3891 /*
3892 * Special for the first stripe and
3893 * the last stripe:
3894 *
3895 * |-------|...|-------|
3896 * |----------|
3897 * off end_off
3898 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08003899 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003900 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003901 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003902
3903 if (stripe_index >= last_stripe &&
3904 stripe_index <= (last_stripe +
3905 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08003906 bbio->stripes[i].length -=
3907 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003908
Li Zefanec9ef7a2011-12-01 14:06:42 +08003909 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003910 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003911 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003912 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003913
3914 stripe_index++;
3915 if (stripe_index == map->num_stripes) {
3916 /* This could only happen for RAID0/10 */
3917 stripe_index = 0;
3918 stripe_nr++;
3919 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003920 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003921 } else {
3922 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003923 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003924 map->stripes[stripe_index].physical +
3925 stripe_offset +
3926 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003927 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003928 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003929 stripe_index++;
3930 }
Chris Mason593060d2008-03-25 16:50:33 -04003931 }
Li Zefande11cc12011-12-01 12:55:47 +08003932
3933 if (rw & REQ_WRITE) {
3934 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3935 BTRFS_BLOCK_GROUP_RAID10 |
3936 BTRFS_BLOCK_GROUP_DUP)) {
3937 max_errors = 1;
3938 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003939 }
Li Zefande11cc12011-12-01 12:55:47 +08003940
3941 *bbio_ret = bbio;
3942 bbio->num_stripes = num_stripes;
3943 bbio->max_errors = max_errors;
3944 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003945out:
Chris Mason0b86a832008-03-24 15:01:56 -04003946 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003947 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003948}
3949
Chris Masonf2d8d742008-04-21 10:03:05 -04003950int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3951 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003952 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003953{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003954 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003955 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003956}
3957
Yan Zhenga512bbf2008-12-08 16:46:26 -05003958int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3959 u64 chunk_start, u64 physical, u64 devid,
3960 u64 **logical, int *naddrs, int *stripe_len)
3961{
3962 struct extent_map_tree *em_tree = &map_tree->map_tree;
3963 struct extent_map *em;
3964 struct map_lookup *map;
3965 u64 *buf;
3966 u64 bytenr;
3967 u64 length;
3968 u64 stripe_nr;
3969 int i, j, nr = 0;
3970
Chris Mason890871b2009-09-02 16:24:52 -04003971 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003972 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003973 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003974
3975 BUG_ON(!em || em->start != chunk_start);
3976 map = (struct map_lookup *)em->bdev;
3977
3978 length = em->len;
3979 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3980 do_div(length, map->num_stripes / map->sub_stripes);
3981 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3982 do_div(length, map->num_stripes);
3983
3984 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003985 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003986
3987 for (i = 0; i < map->num_stripes; i++) {
3988 if (devid && map->stripes[i].dev->devid != devid)
3989 continue;
3990 if (map->stripes[i].physical > physical ||
3991 map->stripes[i].physical + length <= physical)
3992 continue;
3993
3994 stripe_nr = physical - map->stripes[i].physical;
3995 do_div(stripe_nr, map->stripe_len);
3996
3997 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3998 stripe_nr = stripe_nr * map->num_stripes + i;
3999 do_div(stripe_nr, map->sub_stripes);
4000 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4001 stripe_nr = stripe_nr * map->num_stripes + i;
4002 }
4003 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05004004 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004005 for (j = 0; j < nr; j++) {
4006 if (buf[j] == bytenr)
4007 break;
4008 }
Chris Mason934d3752008-12-08 16:43:10 -05004009 if (j == nr) {
4010 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004011 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004012 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004013 }
4014
Yan Zhenga512bbf2008-12-08 16:46:26 -05004015 *logical = buf;
4016 *naddrs = nr;
4017 *stripe_len = map->stripe_len;
4018
4019 free_extent_map(em);
4020 return 0;
4021}
4022
Stefan Behrens442a4f62012-05-25 16:06:08 +02004023static void *merge_stripe_index_into_bio_private(void *bi_private,
4024 unsigned int stripe_index)
4025{
4026 /*
4027 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4028 * at most 1.
4029 * The alternative solution (instead of stealing bits from the
4030 * pointer) would be to allocate an intermediate structure
4031 * that contains the old private pointer plus the stripe_index.
4032 */
4033 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4034 BUG_ON(stripe_index > 3);
4035 return (void *)(((uintptr_t)bi_private) | stripe_index);
4036}
4037
4038static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4039{
4040 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4041}
4042
4043static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4044{
4045 return (unsigned int)((uintptr_t)bi_private) & 3;
4046}
4047
Jan Schmidta1d3c472011-08-04 17:15:33 +02004048static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004049{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004050 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004051 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004052
Stefan Behrens442a4f62012-05-25 16:06:08 +02004053 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004054 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004055 if (err == -EIO || err == -EREMOTEIO) {
4056 unsigned int stripe_index =
4057 extract_stripe_index_from_bio_private(
4058 bio->bi_private);
4059 struct btrfs_device *dev;
4060
4061 BUG_ON(stripe_index >= bbio->num_stripes);
4062 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004063 if (dev->bdev) {
4064 if (bio->bi_rw & WRITE)
4065 btrfs_dev_stat_inc(dev,
4066 BTRFS_DEV_STAT_WRITE_ERRS);
4067 else
4068 btrfs_dev_stat_inc(dev,
4069 BTRFS_DEV_STAT_READ_ERRS);
4070 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4071 btrfs_dev_stat_inc(dev,
4072 BTRFS_DEV_STAT_FLUSH_ERRS);
4073 btrfs_dev_stat_print_on_error(dev);
4074 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004075 }
4076 }
Chris Mason8790d502008-04-03 16:29:03 -04004077
Jan Schmidta1d3c472011-08-04 17:15:33 +02004078 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004079 is_orig_bio = 1;
4080
Jan Schmidta1d3c472011-08-04 17:15:33 +02004081 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004082 if (!is_orig_bio) {
4083 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004084 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004085 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004086 bio->bi_private = bbio->private;
4087 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004088 bio->bi_bdev = (struct block_device *)
4089 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004090 /* only send an error to the higher layers if it is
4091 * beyond the tolerance of the multi-bio
4092 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004093 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004094 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004095 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004096 /*
4097 * this bio is actually up to date, we didn't
4098 * go over the max number of errors
4099 */
4100 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004101 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004102 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004103 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004104
4105 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004106 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004107 bio_put(bio);
4108 }
Chris Mason8790d502008-04-03 16:29:03 -04004109}
4110
Chris Mason8b712842008-06-11 16:50:36 -04004111struct async_sched {
4112 struct bio *bio;
4113 int rw;
4114 struct btrfs_fs_info *info;
4115 struct btrfs_work work;
4116};
4117
4118/*
4119 * see run_scheduled_bios for a description of why bios are collected for
4120 * async submit.
4121 *
4122 * This will add one bio to the pending list for a device and make sure
4123 * the work struct is scheduled.
4124 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004125static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004126 struct btrfs_device *device,
4127 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004128{
4129 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004130 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004131
4132 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004133 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004134 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004135 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004136 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004137 return;
Chris Mason8b712842008-06-11 16:50:36 -04004138 }
4139
4140 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004141 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004142 * higher layers. Otherwise, the async bio makes it appear we have
4143 * made progress against dirty pages when we've really just put it
4144 * on a queue for later
4145 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004146 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004147 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004148 bio->bi_next = NULL;
4149 bio->bi_rw |= rw;
4150
4151 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004152 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004153 pending_bios = &device->pending_sync_bios;
4154 else
4155 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004156
Chris Masonffbd5172009-04-20 15:50:09 -04004157 if (pending_bios->tail)
4158 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004159
Chris Masonffbd5172009-04-20 15:50:09 -04004160 pending_bios->tail = bio;
4161 if (!pending_bios->head)
4162 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004163 if (device->running_pending)
4164 should_queue = 0;
4165
4166 spin_unlock(&device->io_lock);
4167
4168 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004169 btrfs_queue_worker(&root->fs_info->submit_workers,
4170 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004171}
4172
Chris Masonf1885912008-04-09 16:28:12 -04004173int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004174 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004175{
4176 struct btrfs_mapping_tree *map_tree;
4177 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004178 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004179 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004180 u64 length = 0;
4181 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004182 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004183 int dev_nr = 0;
4184 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004185 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004186
Chris Masonf2d8d742008-04-21 10:03:05 -04004187 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004188 map_tree = &root->fs_info->mapping_tree;
4189 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004190
Jan Schmidta1d3c472011-08-04 17:15:33 +02004191 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004192 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004193 if (ret) /* -ENOMEM */
4194 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004195
Jan Schmidta1d3c472011-08-04 17:15:33 +02004196 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004197 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004198 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4199 "len %llu\n", (unsigned long long)logical,
4200 (unsigned long long)length,
4201 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004202 BUG();
4203 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004204
4205 bbio->orig_bio = first_bio;
4206 bbio->private = first_bio->bi_private;
4207 bbio->end_io = first_bio->bi_end_io;
4208 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004209
Chris Masond3977122009-01-05 21:25:51 -05004210 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004211 if (dev_nr < total_devs - 1) {
4212 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004213 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004214 } else {
4215 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004216 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004217 bio->bi_private = bbio;
Stefan Behrens442a4f62012-05-25 16:06:08 +02004218 bio->bi_private = merge_stripe_index_into_bio_private(
4219 bio->bi_private, (unsigned int)dev_nr);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004220 bio->bi_end_io = btrfs_end_bio;
4221 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4222 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004223 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Josef Bacik606686e2012-06-04 14:03:51 -04004224#ifdef DEBUG
4225 struct rcu_string *name;
4226
4227 rcu_read_lock();
4228 name = rcu_dereference(dev->name);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004229 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4230 "(%s id %llu), size=%u\n", rw,
4231 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
Josef Bacik606686e2012-06-04 14:03:51 -04004232 name->str, dev->devid, bio->bi_size);
4233 rcu_read_unlock();
4234#endif
Chris Masondfe25022008-05-13 13:46:40 -04004235 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004236 if (async_submit)
4237 schedule_bio(root, dev, rw, bio);
4238 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004239 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004240 } else {
4241 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4242 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004243 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004244 }
Chris Mason8790d502008-04-03 16:29:03 -04004245 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004246 }
Chris Mason0b86a832008-03-24 15:01:56 -04004247 return 0;
4248}
4249
Chris Masona4437552008-04-18 10:29:38 -04004250struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004251 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004252{
Yan Zheng2b820322008-11-17 21:11:30 -05004253 struct btrfs_device *device;
4254 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004255
Yan Zheng2b820322008-11-17 21:11:30 -05004256 cur_devices = root->fs_info->fs_devices;
4257 while (cur_devices) {
4258 if (!fsid ||
4259 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4260 device = __find_device(&cur_devices->devices,
4261 devid, uuid);
4262 if (device)
4263 return device;
4264 }
4265 cur_devices = cur_devices->seed;
4266 }
4267 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004268}
4269
Chris Masondfe25022008-05-13 13:46:40 -04004270static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4271 u64 devid, u8 *dev_uuid)
4272{
4273 struct btrfs_device *device;
4274 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4275
4276 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004277 if (!device)
4278 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004279 list_add(&device->dev_list,
4280 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004281 device->dev_root = root->fs_info->dev_root;
4282 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004283 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004284 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004285 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004286 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004287 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004288 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004289 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004290 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4291 return device;
4292}
4293
Chris Mason0b86a832008-03-24 15:01:56 -04004294static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4295 struct extent_buffer *leaf,
4296 struct btrfs_chunk *chunk)
4297{
4298 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4299 struct map_lookup *map;
4300 struct extent_map *em;
4301 u64 logical;
4302 u64 length;
4303 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004304 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004305 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004306 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004307 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004308
Chris Masone17cade2008-04-15 15:41:47 -04004309 logical = key->offset;
4310 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004311
Chris Mason890871b2009-09-02 16:24:52 -04004312 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004313 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004314 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004315
4316 /* already mapped? */
4317 if (em && em->start <= logical && em->start + em->len > logical) {
4318 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004319 return 0;
4320 } else if (em) {
4321 free_extent_map(em);
4322 }
Chris Mason0b86a832008-03-24 15:01:56 -04004323
David Sterba172ddd62011-04-21 00:48:27 +02004324 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004325 if (!em)
4326 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004327 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4328 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004329 if (!map) {
4330 free_extent_map(em);
4331 return -ENOMEM;
4332 }
4333
4334 em->bdev = (struct block_device *)map;
4335 em->start = logical;
4336 em->len = length;
4337 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004338 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004339
Chris Mason593060d2008-03-25 16:50:33 -04004340 map->num_stripes = num_stripes;
4341 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4342 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4343 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4344 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4345 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004346 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004347 for (i = 0; i < num_stripes; i++) {
4348 map->stripes[i].physical =
4349 btrfs_stripe_offset_nr(leaf, chunk, i);
4350 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004351 read_extent_buffer(leaf, uuid, (unsigned long)
4352 btrfs_stripe_dev_uuid_nr(chunk, i),
4353 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004354 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4355 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004356 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004357 kfree(map);
4358 free_extent_map(em);
4359 return -EIO;
4360 }
Chris Masondfe25022008-05-13 13:46:40 -04004361 if (!map->stripes[i].dev) {
4362 map->stripes[i].dev =
4363 add_missing_dev(root, devid, uuid);
4364 if (!map->stripes[i].dev) {
4365 kfree(map);
4366 free_extent_map(em);
4367 return -EIO;
4368 }
4369 }
4370 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004371 }
4372
Chris Mason890871b2009-09-02 16:24:52 -04004373 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004374 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004375 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004376 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004377 free_extent_map(em);
4378
4379 return 0;
4380}
4381
Jeff Mahoney143bede2012-03-01 14:56:26 +01004382static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004383 struct btrfs_dev_item *dev_item,
4384 struct btrfs_device *device)
4385{
4386 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004387
4388 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004389 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4390 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004391 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4392 device->type = btrfs_device_type(leaf, dev_item);
4393 device->io_align = btrfs_device_io_align(leaf, dev_item);
4394 device->io_width = btrfs_device_io_width(leaf, dev_item);
4395 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004396
4397 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004398 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004399}
4400
Yan Zheng2b820322008-11-17 21:11:30 -05004401static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4402{
4403 struct btrfs_fs_devices *fs_devices;
4404 int ret;
4405
Li Zefanb367e472011-12-07 11:38:24 +08004406 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004407
4408 fs_devices = root->fs_info->fs_devices->seed;
4409 while (fs_devices) {
4410 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4411 ret = 0;
4412 goto out;
4413 }
4414 fs_devices = fs_devices->seed;
4415 }
4416
4417 fs_devices = find_fsid(fsid);
4418 if (!fs_devices) {
4419 ret = -ENOENT;
4420 goto out;
4421 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004422
4423 fs_devices = clone_fs_devices(fs_devices);
4424 if (IS_ERR(fs_devices)) {
4425 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004426 goto out;
4427 }
4428
Christoph Hellwig97288f22008-12-02 06:36:09 -05004429 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004430 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004431 if (ret) {
4432 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004433 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004434 }
Yan Zheng2b820322008-11-17 21:11:30 -05004435
4436 if (!fs_devices->seeding) {
4437 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004438 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004439 ret = -EINVAL;
4440 goto out;
4441 }
4442
4443 fs_devices->seed = root->fs_info->fs_devices->seed;
4444 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004445out:
Yan Zheng2b820322008-11-17 21:11:30 -05004446 return ret;
4447}
4448
Chris Mason0d81ba52008-03-24 15:02:07 -04004449static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004450 struct extent_buffer *leaf,
4451 struct btrfs_dev_item *dev_item)
4452{
4453 struct btrfs_device *device;
4454 u64 devid;
4455 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004456 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004457 u8 dev_uuid[BTRFS_UUID_SIZE];
4458
Chris Mason0b86a832008-03-24 15:01:56 -04004459 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004460 read_extent_buffer(leaf, dev_uuid,
4461 (unsigned long)btrfs_device_uuid(dev_item),
4462 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004463 read_extent_buffer(leaf, fs_uuid,
4464 (unsigned long)btrfs_device_fsid(dev_item),
4465 BTRFS_UUID_SIZE);
4466
4467 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4468 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004469 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004470 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004471 }
4472
4473 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4474 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004475 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004476 return -EIO;
4477
4478 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004479 printk(KERN_WARNING "warning devid %llu missing\n",
4480 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004481 device = add_missing_dev(root, devid, dev_uuid);
4482 if (!device)
4483 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004484 } else if (!device->missing) {
4485 /*
4486 * this happens when a device that was properly setup
4487 * in the device info lists suddenly goes bad.
4488 * device->bdev is NULL, and so we have to set
4489 * device->missing to one here
4490 */
4491 root->fs_info->fs_devices->missing_devices++;
4492 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004493 }
4494 }
4495
4496 if (device->fs_devices != root->fs_info->fs_devices) {
4497 BUG_ON(device->writeable);
4498 if (device->generation !=
4499 btrfs_device_generation(leaf, dev_item))
4500 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004501 }
Chris Mason0b86a832008-03-24 15:01:56 -04004502
4503 fill_device_from_item(leaf, dev_item, device);
4504 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004505 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004506 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004507 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004508 spin_lock(&root->fs_info->free_chunk_lock);
4509 root->fs_info->free_chunk_space += device->total_bytes -
4510 device->bytes_used;
4511 spin_unlock(&root->fs_info->free_chunk_lock);
4512 }
Chris Mason0b86a832008-03-24 15:01:56 -04004513 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004514 return ret;
4515}
4516
Yan Zhenge4404d62008-12-12 10:03:26 -05004517int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004518{
David Sterba6c417612011-04-13 15:41:04 +02004519 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004520 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004521 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004522 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004523 u8 *ptr;
4524 unsigned long sb_ptr;
4525 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004526 u32 num_stripes;
4527 u32 array_size;
4528 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004529 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004530 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004531
Yan Zhenge4404d62008-12-12 10:03:26 -05004532 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004533 BTRFS_SUPER_INFO_SIZE);
4534 if (!sb)
4535 return -ENOMEM;
4536 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004537 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004538 /*
4539 * The sb extent buffer is artifical and just used to read the system array.
4540 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4541 * pages up-to-date when the page is larger: extent does not cover the
4542 * whole page and consequently check_page_uptodate does not find all
4543 * the page's extents up-to-date (the hole beyond sb),
4544 * write_extent_buffer then triggers a WARN_ON.
4545 *
4546 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4547 * but sb spans only this function. Add an explicit SetPageUptodate call
4548 * to silence the warning eg. on PowerPC 64.
4549 */
4550 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004551 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004552
Chris Masona061fc82008-05-07 11:43:44 -04004553 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004554 array_size = btrfs_super_sys_array_size(super_copy);
4555
Chris Mason0b86a832008-03-24 15:01:56 -04004556 ptr = super_copy->sys_chunk_array;
4557 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4558 cur = 0;
4559
4560 while (cur < array_size) {
4561 disk_key = (struct btrfs_disk_key *)ptr;
4562 btrfs_disk_key_to_cpu(&key, disk_key);
4563
Chris Masona061fc82008-05-07 11:43:44 -04004564 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004565 sb_ptr += len;
4566 cur += len;
4567
Chris Mason0d81ba52008-03-24 15:02:07 -04004568 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004569 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004570 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004571 if (ret)
4572 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004573 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4574 len = btrfs_chunk_item_size(num_stripes);
4575 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004576 ret = -EIO;
4577 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004578 }
4579 ptr += len;
4580 sb_ptr += len;
4581 cur += len;
4582 }
Chris Masona061fc82008-05-07 11:43:44 -04004583 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004584 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004585}
4586
Stefan Behrens442a4f62012-05-25 16:06:08 +02004587struct btrfs_device *btrfs_find_device_for_logical(struct btrfs_root *root,
4588 u64 logical, int mirror_num)
4589{
4590 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4591 int ret;
4592 u64 map_length = 0;
4593 struct btrfs_bio *bbio = NULL;
4594 struct btrfs_device *device;
4595
4596 BUG_ON(mirror_num == 0);
4597 ret = btrfs_map_block(map_tree, WRITE, logical, &map_length, &bbio,
4598 mirror_num);
4599 if (ret) {
4600 BUG_ON(bbio != NULL);
4601 return NULL;
4602 }
4603 BUG_ON(mirror_num != bbio->mirror_num);
4604 device = bbio->stripes[mirror_num - 1].dev;
4605 kfree(bbio);
4606 return device;
4607}
4608
Chris Mason0b86a832008-03-24 15:01:56 -04004609int btrfs_read_chunk_tree(struct btrfs_root *root)
4610{
4611 struct btrfs_path *path;
4612 struct extent_buffer *leaf;
4613 struct btrfs_key key;
4614 struct btrfs_key found_key;
4615 int ret;
4616 int slot;
4617
4618 root = root->fs_info->chunk_root;
4619
4620 path = btrfs_alloc_path();
4621 if (!path)
4622 return -ENOMEM;
4623
Li Zefanb367e472011-12-07 11:38:24 +08004624 mutex_lock(&uuid_mutex);
4625 lock_chunks(root);
4626
Chris Mason0b86a832008-03-24 15:01:56 -04004627 /* first we search for all of the device items, and then we
4628 * read in all of the chunk items. This way we can create chunk
4629 * mappings that reference all of the devices that are afound
4630 */
4631 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4632 key.offset = 0;
4633 key.type = 0;
4634again:
4635 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004636 if (ret < 0)
4637 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004638 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004639 leaf = path->nodes[0];
4640 slot = path->slots[0];
4641 if (slot >= btrfs_header_nritems(leaf)) {
4642 ret = btrfs_next_leaf(root, path);
4643 if (ret == 0)
4644 continue;
4645 if (ret < 0)
4646 goto error;
4647 break;
4648 }
4649 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4650 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4651 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4652 break;
4653 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4654 struct btrfs_dev_item *dev_item;
4655 dev_item = btrfs_item_ptr(leaf, slot,
4656 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004657 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004658 if (ret)
4659 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004660 }
4661 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4662 struct btrfs_chunk *chunk;
4663 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4664 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004665 if (ret)
4666 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004667 }
4668 path->slots[0]++;
4669 }
4670 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4671 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004672 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004673 goto again;
4674 }
Chris Mason0b86a832008-03-24 15:01:56 -04004675 ret = 0;
4676error:
Li Zefanb367e472011-12-07 11:38:24 +08004677 unlock_chunks(root);
4678 mutex_unlock(&uuid_mutex);
4679
Yan Zheng2b820322008-11-17 21:11:30 -05004680 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004681 return ret;
4682}
Stefan Behrens442a4f62012-05-25 16:06:08 +02004683
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004684static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
4685{
4686 int i;
4687
4688 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4689 btrfs_dev_stat_reset(dev, i);
4690}
4691
4692int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
4693{
4694 struct btrfs_key key;
4695 struct btrfs_key found_key;
4696 struct btrfs_root *dev_root = fs_info->dev_root;
4697 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4698 struct extent_buffer *eb;
4699 int slot;
4700 int ret = 0;
4701 struct btrfs_device *device;
4702 struct btrfs_path *path = NULL;
4703 int i;
4704
4705 path = btrfs_alloc_path();
4706 if (!path) {
4707 ret = -ENOMEM;
4708 goto out;
4709 }
4710
4711 mutex_lock(&fs_devices->device_list_mutex);
4712 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4713 int item_size;
4714 struct btrfs_dev_stats_item *ptr;
4715
4716 key.objectid = 0;
4717 key.type = BTRFS_DEV_STATS_KEY;
4718 key.offset = device->devid;
4719 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
4720 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04004721 printk_in_rcu(KERN_WARNING "btrfs: no dev_stats entry found for device %s (devid %llu) (OK on first mount after mkfs)\n",
4722 rcu_str_deref(device->name),
4723 (unsigned long long)device->devid);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004724 __btrfs_reset_dev_stats(device);
4725 device->dev_stats_valid = 1;
4726 btrfs_release_path(path);
4727 continue;
4728 }
4729 slot = path->slots[0];
4730 eb = path->nodes[0];
4731 btrfs_item_key_to_cpu(eb, &found_key, slot);
4732 item_size = btrfs_item_size_nr(eb, slot);
4733
4734 ptr = btrfs_item_ptr(eb, slot,
4735 struct btrfs_dev_stats_item);
4736
4737 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4738 if (item_size >= (1 + i) * sizeof(__le64))
4739 btrfs_dev_stat_set(device, i,
4740 btrfs_dev_stats_value(eb, ptr, i));
4741 else
4742 btrfs_dev_stat_reset(device, i);
4743 }
4744
4745 device->dev_stats_valid = 1;
4746 btrfs_dev_stat_print_on_load(device);
4747 btrfs_release_path(path);
4748 }
4749 mutex_unlock(&fs_devices->device_list_mutex);
4750
4751out:
4752 btrfs_free_path(path);
4753 return ret < 0 ? ret : 0;
4754}
4755
4756static int update_dev_stat_item(struct btrfs_trans_handle *trans,
4757 struct btrfs_root *dev_root,
4758 struct btrfs_device *device)
4759{
4760 struct btrfs_path *path;
4761 struct btrfs_key key;
4762 struct extent_buffer *eb;
4763 struct btrfs_dev_stats_item *ptr;
4764 int ret;
4765 int i;
4766
4767 key.objectid = 0;
4768 key.type = BTRFS_DEV_STATS_KEY;
4769 key.offset = device->devid;
4770
4771 path = btrfs_alloc_path();
4772 BUG_ON(!path);
4773 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
4774 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004775 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
4776 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004777 goto out;
4778 }
4779
4780 if (ret == 0 &&
4781 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
4782 /* need to delete old one and insert a new one */
4783 ret = btrfs_del_item(trans, dev_root, path);
4784 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004785 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
4786 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004787 goto out;
4788 }
4789 ret = 1;
4790 }
4791
4792 if (ret == 1) {
4793 /* need to insert a new item */
4794 btrfs_release_path(path);
4795 ret = btrfs_insert_empty_item(trans, dev_root, path,
4796 &key, sizeof(*ptr));
4797 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004798 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
4799 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004800 goto out;
4801 }
4802 }
4803
4804 eb = path->nodes[0];
4805 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
4806 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4807 btrfs_set_dev_stats_value(eb, ptr, i,
4808 btrfs_dev_stat_read(device, i));
4809 btrfs_mark_buffer_dirty(eb);
4810
4811out:
4812 btrfs_free_path(path);
4813 return ret;
4814}
4815
4816/*
4817 * called from commit_transaction. Writes all changed device stats to disk.
4818 */
4819int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
4820 struct btrfs_fs_info *fs_info)
4821{
4822 struct btrfs_root *dev_root = fs_info->dev_root;
4823 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4824 struct btrfs_device *device;
4825 int ret = 0;
4826
4827 mutex_lock(&fs_devices->device_list_mutex);
4828 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4829 if (!device->dev_stats_valid || !device->dev_stats_dirty)
4830 continue;
4831
4832 ret = update_dev_stat_item(trans, dev_root, device);
4833 if (!ret)
4834 device->dev_stats_dirty = 0;
4835 }
4836 mutex_unlock(&fs_devices->device_list_mutex);
4837
4838 return ret;
4839}
4840
Stefan Behrens442a4f62012-05-25 16:06:08 +02004841void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
4842{
4843 btrfs_dev_stat_inc(dev, index);
4844 btrfs_dev_stat_print_on_error(dev);
4845}
4846
4847void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
4848{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004849 if (!dev->dev_stats_valid)
4850 return;
Josef Bacik606686e2012-06-04 14:03:51 -04004851 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02004852 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04004853 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02004854 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4855 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4856 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4857 btrfs_dev_stat_read(dev,
4858 BTRFS_DEV_STAT_CORRUPTION_ERRS),
4859 btrfs_dev_stat_read(dev,
4860 BTRFS_DEV_STAT_GENERATION_ERRS));
4861}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004862
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004863static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
4864{
Josef Bacik606686e2012-06-04 14:03:51 -04004865 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
4866 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004867 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4868 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4869 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4870 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
4871 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
4872}
4873
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004874int btrfs_get_dev_stats(struct btrfs_root *root,
4875 struct btrfs_ioctl_get_dev_stats *stats,
4876 int reset_after_read)
4877{
4878 struct btrfs_device *dev;
4879 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4880 int i;
4881
4882 mutex_lock(&fs_devices->device_list_mutex);
4883 dev = btrfs_find_device(root, stats->devid, NULL, NULL);
4884 mutex_unlock(&fs_devices->device_list_mutex);
4885
4886 if (!dev) {
4887 printk(KERN_WARNING
4888 "btrfs: get dev_stats failed, device not found\n");
4889 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004890 } else if (!dev->dev_stats_valid) {
4891 printk(KERN_WARNING
4892 "btrfs: get dev_stats failed, not yet valid\n");
4893 return -ENODEV;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004894 } else if (reset_after_read) {
4895 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4896 if (stats->nr_items > i)
4897 stats->values[i] =
4898 btrfs_dev_stat_read_and_reset(dev, i);
4899 else
4900 btrfs_dev_stat_reset(dev, i);
4901 }
4902 } else {
4903 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4904 if (stats->nr_items > i)
4905 stats->values[i] = btrfs_dev_stat_read(dev, i);
4906 }
4907 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
4908 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
4909 return 0;
4910}