blob: b8708f994e679a559af68b90b3135b2b8fa2a9ac [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;
Josef Bacik02db0842012-06-21 16:03:58 -0400432 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500433 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
434
Xiao Guangrong46224702011-04-20 10:08:47 +0000435 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500436 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400437 struct rcu_string *name;
438
Yan Zhenge4404d62008-12-12 10:03:26 -0500439 device = kzalloc(sizeof(*device), GFP_NOFS);
440 if (!device)
441 goto error;
442
Josef Bacik606686e2012-06-04 14:03:51 -0400443 /*
444 * This is ok to do without rcu read locked because we hold the
445 * uuid mutex so nothing we touch in here is going to disappear.
446 */
447 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
448 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400449 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500450 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400451 }
Josef Bacik606686e2012-06-04 14:03:51 -0400452 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500453
454 device->devid = orig_dev->devid;
455 device->work.func = pending_bios_fn;
456 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500457 spin_lock_init(&device->io_lock);
458 INIT_LIST_HEAD(&device->dev_list);
459 INIT_LIST_HEAD(&device->dev_alloc_list);
460
461 list_add(&device->dev_list, &fs_devices->devices);
462 device->fs_devices = fs_devices;
463 fs_devices->num_devices++;
464 }
465 return fs_devices;
466error:
467 free_fs_devices(fs_devices);
468 return ERR_PTR(-ENOMEM);
469}
470
Jeff Mahoney143bede2012-03-01 14:56:26 +0100471void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
Chris Masondfe25022008-05-13 13:46:40 -0400472{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500473 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400474
Chris Masona6b0d5c2012-02-20 20:53:43 -0500475 struct block_device *latest_bdev = NULL;
476 u64 latest_devid = 0;
477 u64 latest_transid = 0;
478
Chris Masondfe25022008-05-13 13:46:40 -0400479 mutex_lock(&uuid_mutex);
480again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000481 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500482 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500483 if (device->in_fs_metadata) {
484 if (!latest_transid ||
485 device->generation > latest_transid) {
486 latest_devid = device->devid;
487 latest_transid = device->generation;
488 latest_bdev = device->bdev;
489 }
Yan Zheng2b820322008-11-17 21:11:30 -0500490 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500491 }
Yan Zheng2b820322008-11-17 21:11:30 -0500492
493 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100494 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500495 device->bdev = NULL;
496 fs_devices->open_devices--;
497 }
498 if (device->writeable) {
499 list_del_init(&device->dev_alloc_list);
500 device->writeable = 0;
501 fs_devices->rw_devices--;
502 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500503 list_del_init(&device->dev_list);
504 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400505 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500506 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400507 }
Yan Zheng2b820322008-11-17 21:11:30 -0500508
509 if (fs_devices->seed) {
510 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500511 goto again;
512 }
513
Chris Masona6b0d5c2012-02-20 20:53:43 -0500514 fs_devices->latest_bdev = latest_bdev;
515 fs_devices->latest_devid = latest_devid;
516 fs_devices->latest_trans = latest_transid;
517
Chris Masondfe25022008-05-13 13:46:40 -0400518 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400519}
Chris Masona0af4692008-05-13 16:03:06 -0400520
Xiao Guangrong1f781602011-04-20 10:09:16 +0000521static void __free_device(struct work_struct *work)
522{
523 struct btrfs_device *device;
524
525 device = container_of(work, struct btrfs_device, rcu_work);
526
527 if (device->bdev)
528 blkdev_put(device->bdev, device->mode);
529
Josef Bacik606686e2012-06-04 14:03:51 -0400530 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000531 kfree(device);
532}
533
534static void free_device(struct rcu_head *head)
535{
536 struct btrfs_device *device;
537
538 device = container_of(head, struct btrfs_device, rcu);
539
540 INIT_WORK(&device->rcu_work, __free_device);
541 schedule_work(&device->rcu_work);
542}
543
Yan Zheng2b820322008-11-17 21:11:30 -0500544static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400545{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400546 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500547
Yan Zheng2b820322008-11-17 21:11:30 -0500548 if (--fs_devices->opened > 0)
549 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400550
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000551 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500552 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000553 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400554 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000555
556 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400557 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000558
Yan Zheng2b820322008-11-17 21:11:30 -0500559 if (device->writeable) {
560 list_del_init(&device->dev_alloc_list);
561 fs_devices->rw_devices--;
562 }
563
Josef Bacikd5e20032011-08-04 14:52:27 +0000564 if (device->can_discard)
565 fs_devices->num_can_discard--;
566
Xiao Guangrong1f781602011-04-20 10:09:16 +0000567 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100568 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000569 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400570
571 /* Safe because we are under uuid_mutex */
572 name = rcu_string_strdup(device->name->str, GFP_NOFS);
573 BUG_ON(device->name && !name); /* -ENOMEM */
574 rcu_assign_pointer(new_device->name, name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000575 new_device->bdev = NULL;
576 new_device->writeable = 0;
577 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000578 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000579 list_replace_rcu(&device->dev_list, &new_device->dev_list);
580
581 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400582 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000583 mutex_unlock(&fs_devices->device_list_mutex);
584
Yan Zhenge4404d62008-12-12 10:03:26 -0500585 WARN_ON(fs_devices->open_devices);
586 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500587 fs_devices->opened = 0;
588 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500589
Chris Mason8a4b83c2008-03-24 15:02:07 -0400590 return 0;
591}
592
Yan Zheng2b820322008-11-17 21:11:30 -0500593int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
594{
Yan Zhenge4404d62008-12-12 10:03:26 -0500595 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500596 int ret;
597
598 mutex_lock(&uuid_mutex);
599 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500600 if (!fs_devices->opened) {
601 seed_devices = fs_devices->seed;
602 fs_devices->seed = NULL;
603 }
Yan Zheng2b820322008-11-17 21:11:30 -0500604 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500605
606 while (seed_devices) {
607 fs_devices = seed_devices;
608 seed_devices = fs_devices->seed;
609 __btrfs_close_devices(fs_devices);
610 free_fs_devices(fs_devices);
611 }
Yan Zheng2b820322008-11-17 21:11:30 -0500612 return ret;
613}
614
Yan Zhenge4404d62008-12-12 10:03:26 -0500615static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
616 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400617{
Josef Bacikd5e20032011-08-04 14:52:27 +0000618 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400619 struct block_device *bdev;
620 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400621 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400622 struct block_device *latest_bdev = NULL;
623 struct buffer_head *bh;
624 struct btrfs_super_block *disk_super;
625 u64 latest_devid = 0;
626 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400627 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500628 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400629 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400630
Tejun Heod4d77622010-11-13 11:55:18 +0100631 flags |= FMODE_EXCL;
632
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500633 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400634 if (device->bdev)
635 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400636 if (!device->name)
637 continue;
638
Josef Bacik606686e2012-06-04 14:03:51 -0400639 bdev = blkdev_get_by_path(device->name->str, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400640 if (IS_ERR(bdev)) {
Josef Bacik606686e2012-06-04 14:03:51 -0400641 printk(KERN_INFO "open %s failed\n", device->name->str);
Chris Masona0af4692008-05-13 16:03:06 -0400642 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400643 }
Chris Mason3c4bb262012-03-27 18:56:56 -0400644 filemap_write_and_wait(bdev->bd_inode->i_mapping);
645 invalidate_bdev(bdev);
Chris Masona061fc82008-05-07 11:43:44 -0400646 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400647
Yan Zhenga512bbf2008-12-08 16:46:26 -0500648 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300649 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400650 goto error_close;
651
652 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000653 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400654 if (devid != device->devid)
655 goto error_brelse;
656
Yan Zheng2b820322008-11-17 21:11:30 -0500657 if (memcmp(device->uuid, disk_super->dev_item.uuid,
658 BTRFS_UUID_SIZE))
659 goto error_brelse;
660
661 device->generation = btrfs_super_generation(disk_super);
662 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400663 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500664 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400665 latest_bdev = bdev;
666 }
667
Yan Zheng2b820322008-11-17 21:11:30 -0500668 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
669 device->writeable = 0;
670 } else {
671 device->writeable = !bdev_read_only(bdev);
672 seeding = 0;
673 }
674
Josef Bacikd5e20032011-08-04 14:52:27 +0000675 q = bdev_get_queue(bdev);
676 if (blk_queue_discard(q)) {
677 device->can_discard = 1;
678 fs_devices->num_can_discard++;
679 }
680
Chris Mason8a4b83c2008-03-24 15:02:07 -0400681 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400682 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500683 device->mode = flags;
684
Chris Masonc2898112009-06-10 09:51:32 -0400685 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
686 fs_devices->rotating = 1;
687
Chris Masona0af4692008-05-13 16:03:06 -0400688 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500689 if (device->writeable) {
690 fs_devices->rw_devices++;
691 list_add(&device->dev_alloc_list,
692 &fs_devices->alloc_list);
693 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000694 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400695 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400696
Chris Masona0af4692008-05-13 16:03:06 -0400697error_brelse:
698 brelse(bh);
699error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100700 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400701error:
702 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400703 }
Chris Masona0af4692008-05-13 16:03:06 -0400704 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300705 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400706 goto out;
707 }
Yan Zheng2b820322008-11-17 21:11:30 -0500708 fs_devices->seeding = seeding;
709 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400710 fs_devices->latest_bdev = latest_bdev;
711 fs_devices->latest_devid = latest_devid;
712 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500713 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400714out:
Yan Zheng2b820322008-11-17 21:11:30 -0500715 return ret;
716}
717
718int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500719 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500720{
721 int ret;
722
723 mutex_lock(&uuid_mutex);
724 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500725 fs_devices->opened++;
726 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500727 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500728 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500729 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400730 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400731 return ret;
732}
733
Christoph Hellwig97288f22008-12-02 06:36:09 -0500734int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400735 struct btrfs_fs_devices **fs_devices_ret)
736{
737 struct btrfs_super_block *disk_super;
738 struct block_device *bdev;
739 struct buffer_head *bh;
740 int ret;
741 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400742 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400743 u64 total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400744
Tejun Heod4d77622010-11-13 11:55:18 +0100745 flags |= FMODE_EXCL;
746 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400747
748 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400749 ret = PTR_ERR(bdev);
750 goto error;
751 }
752
Al Viro10f63272011-11-17 15:05:22 -0500753 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400754 ret = set_blocksize(bdev, 4096);
755 if (ret)
756 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500757 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000759 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760 goto error_close;
761 }
762 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000763 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400764 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400765 total_devices = btrfs_super_num_devices(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400766 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500767 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200768 else
769 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500770 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500771 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400772 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400773 if (!ret && fs_devices_ret)
774 (*fs_devices_ret)->total_devices = total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400775 brelse(bh);
776error_close:
Al Viro10f63272011-11-17 15:05:22 -0500777 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100778 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400779error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400780 return ret;
781}
Chris Mason0b86a832008-03-24 15:01:56 -0400782
Miao Xie6d07bce2011-01-05 10:07:31 +0000783/* helper to account the used device space in the range */
784int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
785 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400786{
787 struct btrfs_key key;
788 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000789 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500790 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000791 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400792 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000793 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400794 struct extent_buffer *l;
795
Miao Xie6d07bce2011-01-05 10:07:31 +0000796 *length = 0;
797
798 if (start >= device->total_bytes)
799 return 0;
800
Yan Zheng2b820322008-11-17 21:11:30 -0500801 path = btrfs_alloc_path();
802 if (!path)
803 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400804 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400805
Chris Mason0b86a832008-03-24 15:01:56 -0400806 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000807 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400808 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000809
810 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400811 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000812 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400813 if (ret > 0) {
814 ret = btrfs_previous_item(root, path, key.objectid, key.type);
815 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000816 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400817 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000818
Chris Mason0b86a832008-03-24 15:01:56 -0400819 while (1) {
820 l = path->nodes[0];
821 slot = path->slots[0];
822 if (slot >= btrfs_header_nritems(l)) {
823 ret = btrfs_next_leaf(root, path);
824 if (ret == 0)
825 continue;
826 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000827 goto out;
828
829 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400830 }
831 btrfs_item_key_to_cpu(l, &key, slot);
832
833 if (key.objectid < device->devid)
834 goto next;
835
836 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000837 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400838
Chris Masond3977122009-01-05 21:25:51 -0500839 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400840 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400841
Chris Mason0b86a832008-03-24 15:01:56 -0400842 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000843 extent_end = key.offset + btrfs_dev_extent_length(l,
844 dev_extent);
845 if (key.offset <= start && extent_end > end) {
846 *length = end - start + 1;
847 break;
848 } else if (key.offset <= start && extent_end > start)
849 *length += extent_end - start;
850 else if (key.offset > start && extent_end <= end)
851 *length += extent_end - key.offset;
852 else if (key.offset > start && key.offset <= end) {
853 *length += end - key.offset + 1;
854 break;
855 } else if (key.offset > end)
856 break;
857
858next:
859 path->slots[0]++;
860 }
861 ret = 0;
862out:
863 btrfs_free_path(path);
864 return ret;
865}
866
Chris Mason0b86a832008-03-24 15:01:56 -0400867/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000868 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000869 * @device: the device which we search the free space in
870 * @num_bytes: the size of the free space that we need
871 * @start: store the start of the free space.
872 * @len: the size of the free space. that we find, or the size of the max
873 * free space if we don't find suitable free space
874 *
Chris Mason0b86a832008-03-24 15:01:56 -0400875 * this uses a pretty simple search, the expectation is that it is
876 * called very infrequently and that a given device has a small number
877 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000878 *
879 * @start is used to store the start of the free space if we find. But if we
880 * don't find suitable free space, it will be used to store the start position
881 * of the max free space.
882 *
883 * @len is used to store the size of the free space that we find.
884 * But if we don't find suitable free space, it is used to store the size of
885 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400886 */
Li Zefan125ccb02011-12-08 15:07:24 +0800887int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000888 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400889{
890 struct btrfs_key key;
891 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000892 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400893 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000894 u64 hole_size;
895 u64 max_hole_start;
896 u64 max_hole_size;
897 u64 extent_end;
898 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400899 u64 search_end = device->total_bytes;
900 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000901 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400902 struct extent_buffer *l;
903
Chris Mason0b86a832008-03-24 15:01:56 -0400904 /* FIXME use last free of some kind */
905
906 /* we don't want to overwrite the superblock on the drive,
907 * so we make sure to start at an offset of at least 1MB
908 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200909 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400910
Miao Xie7bfc8372011-01-05 10:07:26 +0000911 max_hole_start = search_start;
912 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000913 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000914
915 if (search_start >= search_end) {
916 ret = -ENOSPC;
917 goto error;
918 }
919
920 path = btrfs_alloc_path();
921 if (!path) {
922 ret = -ENOMEM;
923 goto error;
924 }
925 path->reada = 2;
926
Chris Mason0b86a832008-03-24 15:01:56 -0400927 key.objectid = device->devid;
928 key.offset = search_start;
929 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000930
Li Zefan125ccb02011-12-08 15:07:24 +0800931 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400932 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000933 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400934 if (ret > 0) {
935 ret = btrfs_previous_item(root, path, key.objectid, key.type);
936 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000937 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400938 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000939
Chris Mason0b86a832008-03-24 15:01:56 -0400940 while (1) {
941 l = path->nodes[0];
942 slot = path->slots[0];
943 if (slot >= btrfs_header_nritems(l)) {
944 ret = btrfs_next_leaf(root, path);
945 if (ret == 0)
946 continue;
947 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000948 goto out;
949
950 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400951 }
952 btrfs_item_key_to_cpu(l, &key, slot);
953
954 if (key.objectid < device->devid)
955 goto next;
956
957 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000958 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400959
Chris Mason0b86a832008-03-24 15:01:56 -0400960 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
961 goto next;
962
Miao Xie7bfc8372011-01-05 10:07:26 +0000963 if (key.offset > search_start) {
964 hole_size = key.offset - search_start;
965
966 if (hole_size > max_hole_size) {
967 max_hole_start = search_start;
968 max_hole_size = hole_size;
969 }
970
971 /*
972 * If this free space is greater than which we need,
973 * it must be the max free space that we have found
974 * until now, so max_hole_start must point to the start
975 * of this free space and the length of this free space
976 * is stored in max_hole_size. Thus, we return
977 * max_hole_start and max_hole_size and go back to the
978 * caller.
979 */
980 if (hole_size >= num_bytes) {
981 ret = 0;
982 goto out;
983 }
984 }
985
Chris Mason0b86a832008-03-24 15:01:56 -0400986 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000987 extent_end = key.offset + btrfs_dev_extent_length(l,
988 dev_extent);
989 if (extent_end > search_start)
990 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400991next:
992 path->slots[0]++;
993 cond_resched();
994 }
Chris Mason0b86a832008-03-24 15:01:56 -0400995
liubo38c01b92011-08-02 02:39:03 +0000996 /*
997 * At this point, search_start should be the end of
998 * allocated dev extents, and when shrinking the device,
999 * search_end may be smaller than search_start.
1000 */
1001 if (search_end > search_start)
1002 hole_size = search_end - search_start;
1003
Miao Xie7bfc8372011-01-05 10:07:26 +00001004 if (hole_size > max_hole_size) {
1005 max_hole_start = search_start;
1006 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001007 }
Chris Mason0b86a832008-03-24 15:01:56 -04001008
Miao Xie7bfc8372011-01-05 10:07:26 +00001009 /* See above. */
1010 if (hole_size < num_bytes)
1011 ret = -ENOSPC;
1012 else
1013 ret = 0;
1014
1015out:
Yan Zheng2b820322008-11-17 21:11:30 -05001016 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001017error:
1018 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001019 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001020 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001021 return ret;
1022}
1023
Christoph Hellwigb2950862008-12-02 09:54:17 -05001024static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001025 struct btrfs_device *device,
1026 u64 start)
1027{
1028 int ret;
1029 struct btrfs_path *path;
1030 struct btrfs_root *root = device->dev_root;
1031 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001032 struct btrfs_key found_key;
1033 struct extent_buffer *leaf = NULL;
1034 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001035
1036 path = btrfs_alloc_path();
1037 if (!path)
1038 return -ENOMEM;
1039
1040 key.objectid = device->devid;
1041 key.offset = start;
1042 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001043again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001044 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001045 if (ret > 0) {
1046 ret = btrfs_previous_item(root, path, key.objectid,
1047 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001048 if (ret)
1049 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001050 leaf = path->nodes[0];
1051 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1052 extent = btrfs_item_ptr(leaf, path->slots[0],
1053 struct btrfs_dev_extent);
1054 BUG_ON(found_key.offset > start || found_key.offset +
1055 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001056 key = found_key;
1057 btrfs_release_path(path);
1058 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001059 } else if (ret == 0) {
1060 leaf = path->nodes[0];
1061 extent = btrfs_item_ptr(leaf, path->slots[0],
1062 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001063 } else {
1064 btrfs_error(root->fs_info, ret, "Slot search failed");
1065 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001066 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001067
Josef Bacik2bf64752011-09-26 17:12:22 -04001068 if (device->bytes_used > 0) {
1069 u64 len = btrfs_dev_extent_length(leaf, extent);
1070 device->bytes_used -= len;
1071 spin_lock(&root->fs_info->free_chunk_lock);
1072 root->fs_info->free_chunk_space += len;
1073 spin_unlock(&root->fs_info->free_chunk_lock);
1074 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001075 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001076 if (ret) {
1077 btrfs_error(root->fs_info, ret,
1078 "Failed to remove dev extent item");
1079 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001080out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001081 btrfs_free_path(path);
1082 return ret;
1083}
1084
Yan Zheng2b820322008-11-17 21:11:30 -05001085int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001086 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001087 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001088 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001089{
1090 int ret;
1091 struct btrfs_path *path;
1092 struct btrfs_root *root = device->dev_root;
1093 struct btrfs_dev_extent *extent;
1094 struct extent_buffer *leaf;
1095 struct btrfs_key key;
1096
Chris Masondfe25022008-05-13 13:46:40 -04001097 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001098 path = btrfs_alloc_path();
1099 if (!path)
1100 return -ENOMEM;
1101
Chris Mason0b86a832008-03-24 15:01:56 -04001102 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001103 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001104 key.type = BTRFS_DEV_EXTENT_KEY;
1105 ret = btrfs_insert_empty_item(trans, root, path, &key,
1106 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001107 if (ret)
1108 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001109
1110 leaf = path->nodes[0];
1111 extent = btrfs_item_ptr(leaf, path->slots[0],
1112 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001113 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1114 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1115 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1116
1117 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1118 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1119 BTRFS_UUID_SIZE);
1120
Chris Mason0b86a832008-03-24 15:01:56 -04001121 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1122 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001123out:
Chris Mason0b86a832008-03-24 15:01:56 -04001124 btrfs_free_path(path);
1125 return ret;
1126}
1127
Chris Masona1b32a52008-09-05 16:09:51 -04001128static noinline int find_next_chunk(struct btrfs_root *root,
1129 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001130{
1131 struct btrfs_path *path;
1132 int ret;
1133 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001134 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001135 struct btrfs_key found_key;
1136
1137 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001138 if (!path)
1139 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001140
Chris Masone17cade2008-04-15 15:41:47 -04001141 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001142 key.offset = (u64)-1;
1143 key.type = BTRFS_CHUNK_ITEM_KEY;
1144
1145 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1146 if (ret < 0)
1147 goto error;
1148
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001149 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001150
1151 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1152 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001153 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001154 } else {
1155 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1156 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001157 if (found_key.objectid != objectid)
1158 *offset = 0;
1159 else {
1160 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1161 struct btrfs_chunk);
1162 *offset = found_key.offset +
1163 btrfs_chunk_length(path->nodes[0], chunk);
1164 }
Chris Mason0b86a832008-03-24 15:01:56 -04001165 }
1166 ret = 0;
1167error:
1168 btrfs_free_path(path);
1169 return ret;
1170}
1171
Yan Zheng2b820322008-11-17 21:11:30 -05001172static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001173{
1174 int ret;
1175 struct btrfs_key key;
1176 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001177 struct btrfs_path *path;
1178
1179 root = root->fs_info->chunk_root;
1180
1181 path = btrfs_alloc_path();
1182 if (!path)
1183 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001184
1185 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1186 key.type = BTRFS_DEV_ITEM_KEY;
1187 key.offset = (u64)-1;
1188
1189 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1190 if (ret < 0)
1191 goto error;
1192
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001193 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001194
1195 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1196 BTRFS_DEV_ITEM_KEY);
1197 if (ret) {
1198 *objectid = 1;
1199 } else {
1200 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1201 path->slots[0]);
1202 *objectid = found_key.offset + 1;
1203 }
1204 ret = 0;
1205error:
Yan Zheng2b820322008-11-17 21:11:30 -05001206 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001207 return ret;
1208}
1209
1210/*
1211 * the device information is stored in the chunk root
1212 * the btrfs_device struct should be fully filled in
1213 */
1214int btrfs_add_device(struct btrfs_trans_handle *trans,
1215 struct btrfs_root *root,
1216 struct btrfs_device *device)
1217{
1218 int ret;
1219 struct btrfs_path *path;
1220 struct btrfs_dev_item *dev_item;
1221 struct extent_buffer *leaf;
1222 struct btrfs_key key;
1223 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001224
1225 root = root->fs_info->chunk_root;
1226
1227 path = btrfs_alloc_path();
1228 if (!path)
1229 return -ENOMEM;
1230
Chris Mason0b86a832008-03-24 15:01:56 -04001231 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1232 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001233 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001234
1235 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001236 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001237 if (ret)
1238 goto out;
1239
1240 leaf = path->nodes[0];
1241 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1242
1243 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001244 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001245 btrfs_set_device_type(leaf, dev_item, device->type);
1246 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1247 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1248 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001249 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1250 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001251 btrfs_set_device_group(leaf, dev_item, 0);
1252 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1253 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001254 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001255
Chris Mason0b86a832008-03-24 15:01:56 -04001256 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001257 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001258 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1259 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001260 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001261
Yan Zheng2b820322008-11-17 21:11:30 -05001262 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001263out:
1264 btrfs_free_path(path);
1265 return ret;
1266}
Chris Mason8f18cf12008-04-25 16:53:30 -04001267
Chris Masona061fc82008-05-07 11:43:44 -04001268static int btrfs_rm_dev_item(struct btrfs_root *root,
1269 struct btrfs_device *device)
1270{
1271 int ret;
1272 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001273 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001274 struct btrfs_trans_handle *trans;
1275
1276 root = root->fs_info->chunk_root;
1277
1278 path = btrfs_alloc_path();
1279 if (!path)
1280 return -ENOMEM;
1281
Yan, Zhenga22285a2010-05-16 10:48:46 -04001282 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001283 if (IS_ERR(trans)) {
1284 btrfs_free_path(path);
1285 return PTR_ERR(trans);
1286 }
Chris Masona061fc82008-05-07 11:43:44 -04001287 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1288 key.type = BTRFS_DEV_ITEM_KEY;
1289 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001290 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001291
1292 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1293 if (ret < 0)
1294 goto out;
1295
1296 if (ret > 0) {
1297 ret = -ENOENT;
1298 goto out;
1299 }
1300
1301 ret = btrfs_del_item(trans, root, path);
1302 if (ret)
1303 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001304out:
1305 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001306 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001307 btrfs_commit_transaction(trans, root);
1308 return ret;
1309}
1310
1311int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1312{
1313 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001314 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001315 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001316 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001317 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001318 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001319 u64 all_avail;
1320 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001321 u64 num_devices;
1322 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001323 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001324 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001325
Chris Masona061fc82008-05-07 11:43:44 -04001326 mutex_lock(&uuid_mutex);
1327
1328 all_avail = root->fs_info->avail_data_alloc_bits |
1329 root->fs_info->avail_system_alloc_bits |
1330 root->fs_info->avail_metadata_alloc_bits;
1331
1332 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001333 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001334 printk(KERN_ERR "btrfs: unable to go below four devices "
1335 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001336 ret = -EINVAL;
1337 goto out;
1338 }
1339
1340 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001341 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001342 printk(KERN_ERR "btrfs: unable to go below two "
1343 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001344 ret = -EINVAL;
1345 goto out;
1346 }
1347
Chris Masondfe25022008-05-13 13:46:40 -04001348 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001349 struct list_head *devices;
1350 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001351
Chris Masondfe25022008-05-13 13:46:40 -04001352 device = NULL;
1353 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001354 /*
1355 * It is safe to read the devices since the volume_mutex
1356 * is held.
1357 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001358 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001359 if (tmp->in_fs_metadata && !tmp->bdev) {
1360 device = tmp;
1361 break;
1362 }
1363 }
1364 bdev = NULL;
1365 bh = NULL;
1366 disk_super = NULL;
1367 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001368 printk(KERN_ERR "btrfs: no missing devices found to "
1369 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001370 goto out;
1371 }
Chris Masondfe25022008-05-13 13:46:40 -04001372 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001373 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1374 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001375 if (IS_ERR(bdev)) {
1376 ret = PTR_ERR(bdev);
1377 goto out;
1378 }
1379
Yan Zheng2b820322008-11-17 21:11:30 -05001380 set_blocksize(bdev, 4096);
Chris Mason3c4bb262012-03-27 18:56:56 -04001381 invalidate_bdev(bdev);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001382 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001383 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001384 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001385 goto error_close;
1386 }
1387 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001388 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001389 dev_uuid = disk_super->dev_item.uuid;
1390 device = btrfs_find_device(root, devid, dev_uuid,
1391 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001392 if (!device) {
1393 ret = -ENOENT;
1394 goto error_brelse;
1395 }
Chris Masondfe25022008-05-13 13:46:40 -04001396 }
Yan Zheng2b820322008-11-17 21:11:30 -05001397
1398 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001399 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1400 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001401 ret = -EINVAL;
1402 goto error_brelse;
1403 }
1404
1405 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001406 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001407 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001408 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001409 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001410 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001411 }
Chris Masona061fc82008-05-07 11:43:44 -04001412
1413 ret = btrfs_shrink_device(device, 0);
1414 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001415 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001416
Chris Masona061fc82008-05-07 11:43:44 -04001417 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1418 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001419 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001420
Josef Bacik2bf64752011-09-26 17:12:22 -04001421 spin_lock(&root->fs_info->free_chunk_lock);
1422 root->fs_info->free_chunk_space = device->total_bytes -
1423 device->bytes_used;
1424 spin_unlock(&root->fs_info->free_chunk_lock);
1425
Yan Zheng2b820322008-11-17 21:11:30 -05001426 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001427 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001428
1429 /*
1430 * the device list mutex makes sure that we don't change
1431 * the device list while someone else is writing out all
1432 * the device supers.
1433 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001434
1435 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001436 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001437 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001438
Yan Zhenge4404d62008-12-12 10:03:26 -05001439 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001440 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001441
Chris Masoncd02dca2010-12-13 14:56:23 -05001442 if (device->missing)
1443 root->fs_info->fs_devices->missing_devices--;
1444
Yan Zheng2b820322008-11-17 21:11:30 -05001445 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1446 struct btrfs_device, dev_list);
1447 if (device->bdev == root->fs_info->sb->s_bdev)
1448 root->fs_info->sb->s_bdev = next_device->bdev;
1449 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1450 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1451
Xiao Guangrong1f781602011-04-20 10:09:16 +00001452 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001453 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001454
1455 call_rcu(&device->rcu, free_device);
1456 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001457
David Sterba6c417612011-04-13 15:41:04 +02001458 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1459 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001460
Xiao Guangrong1f781602011-04-20 10:09:16 +00001461 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001462 struct btrfs_fs_devices *fs_devices;
1463 fs_devices = root->fs_info->fs_devices;
1464 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001465 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001466 break;
1467 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001468 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001469 fs_devices->seed = cur_devices->seed;
1470 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001471 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001472 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001473 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001474 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001475 }
1476
1477 /*
1478 * at this point, the device is zero sized. We want to
1479 * remove it from the devices list and zero out the old super
1480 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001481 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001482 /* make sure this device isn't detected as part of
1483 * the FS anymore
1484 */
1485 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1486 set_buffer_dirty(bh);
1487 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001488 }
Chris Masona061fc82008-05-07 11:43:44 -04001489
Chris Masona061fc82008-05-07 11:43:44 -04001490 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001491
1492error_brelse:
1493 brelse(bh);
1494error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001495 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001496 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001497out:
1498 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001499 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001500error_undo:
1501 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001502 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001503 list_add(&device->dev_alloc_list,
1504 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001505 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001506 root->fs_info->fs_devices->rw_devices++;
1507 }
1508 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001509}
1510
Yan Zheng2b820322008-11-17 21:11:30 -05001511/*
1512 * does all the dirty work required for changing file system's UUID.
1513 */
Li Zefan125ccb02011-12-08 15:07:24 +08001514static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001515{
1516 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1517 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001518 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001519 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001520 struct btrfs_device *device;
1521 u64 super_flags;
1522
1523 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001524 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001525 return -EINVAL;
1526
Yan Zhenge4404d62008-12-12 10:03:26 -05001527 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1528 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001529 return -ENOMEM;
1530
Yan Zhenge4404d62008-12-12 10:03:26 -05001531 old_devices = clone_fs_devices(fs_devices);
1532 if (IS_ERR(old_devices)) {
1533 kfree(seed_devices);
1534 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001535 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001536
Yan Zheng2b820322008-11-17 21:11:30 -05001537 list_add(&old_devices->list, &fs_uuids);
1538
Yan Zhenge4404d62008-12-12 10:03:26 -05001539 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1540 seed_devices->opened = 1;
1541 INIT_LIST_HEAD(&seed_devices->devices);
1542 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001543 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001544
1545 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001546 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1547 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001548 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1549
Yan Zhenge4404d62008-12-12 10:03:26 -05001550 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1551 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1552 device->fs_devices = seed_devices;
1553 }
1554
Yan Zheng2b820322008-11-17 21:11:30 -05001555 fs_devices->seeding = 0;
1556 fs_devices->num_devices = 0;
1557 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001558 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001559 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001560
1561 generate_random_uuid(fs_devices->fsid);
1562 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1563 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1564 super_flags = btrfs_super_flags(disk_super) &
1565 ~BTRFS_SUPER_FLAG_SEEDING;
1566 btrfs_set_super_flags(disk_super, super_flags);
1567
1568 return 0;
1569}
1570
1571/*
1572 * strore the expected generation for seed devices in device items.
1573 */
1574static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1575 struct btrfs_root *root)
1576{
1577 struct btrfs_path *path;
1578 struct extent_buffer *leaf;
1579 struct btrfs_dev_item *dev_item;
1580 struct btrfs_device *device;
1581 struct btrfs_key key;
1582 u8 fs_uuid[BTRFS_UUID_SIZE];
1583 u8 dev_uuid[BTRFS_UUID_SIZE];
1584 u64 devid;
1585 int ret;
1586
1587 path = btrfs_alloc_path();
1588 if (!path)
1589 return -ENOMEM;
1590
1591 root = root->fs_info->chunk_root;
1592 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1593 key.offset = 0;
1594 key.type = BTRFS_DEV_ITEM_KEY;
1595
1596 while (1) {
1597 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1598 if (ret < 0)
1599 goto error;
1600
1601 leaf = path->nodes[0];
1602next_slot:
1603 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1604 ret = btrfs_next_leaf(root, path);
1605 if (ret > 0)
1606 break;
1607 if (ret < 0)
1608 goto error;
1609 leaf = path->nodes[0];
1610 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001611 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001612 continue;
1613 }
1614
1615 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1616 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1617 key.type != BTRFS_DEV_ITEM_KEY)
1618 break;
1619
1620 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1621 struct btrfs_dev_item);
1622 devid = btrfs_device_id(leaf, dev_item);
1623 read_extent_buffer(leaf, dev_uuid,
1624 (unsigned long)btrfs_device_uuid(dev_item),
1625 BTRFS_UUID_SIZE);
1626 read_extent_buffer(leaf, fs_uuid,
1627 (unsigned long)btrfs_device_fsid(dev_item),
1628 BTRFS_UUID_SIZE);
1629 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001630 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001631
1632 if (device->fs_devices->seeding) {
1633 btrfs_set_device_generation(leaf, dev_item,
1634 device->generation);
1635 btrfs_mark_buffer_dirty(leaf);
1636 }
1637
1638 path->slots[0]++;
1639 goto next_slot;
1640 }
1641 ret = 0;
1642error:
1643 btrfs_free_path(path);
1644 return ret;
1645}
1646
Chris Mason788f20e2008-04-28 15:29:42 -04001647int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1648{
Josef Bacikd5e20032011-08-04 14:52:27 +00001649 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001650 struct btrfs_trans_handle *trans;
1651 struct btrfs_device *device;
1652 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001653 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001654 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001655 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001656 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001657 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001658 int ret = 0;
1659
Yan Zheng2b820322008-11-17 21:11:30 -05001660 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001661 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001662
Li Zefana5d16332011-12-07 20:08:40 -05001663 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001664 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001665 if (IS_ERR(bdev))
1666 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001667
Yan Zheng2b820322008-11-17 21:11:30 -05001668 if (root->fs_info->fs_devices->seeding) {
1669 seeding_dev = 1;
1670 down_write(&sb->s_umount);
1671 mutex_lock(&uuid_mutex);
1672 }
1673
Chris Mason8c8bee12008-09-29 11:19:10 -04001674 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001675
Chris Mason788f20e2008-04-28 15:29:42 -04001676 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001677 /*
1678 * we have the volume lock, so we don't need the extra
1679 * device list mutex while reading the list here.
1680 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001681 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001682 if (device->bdev == bdev) {
1683 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001684 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001685 }
1686 }
1687
1688 device = kzalloc(sizeof(*device), GFP_NOFS);
1689 if (!device) {
1690 /* we can safely leave the fs_devices entry around */
1691 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001692 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001693 }
1694
Josef Bacik606686e2012-06-04 14:03:51 -04001695 name = rcu_string_strdup(device_path, GFP_NOFS);
1696 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001697 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001698 ret = -ENOMEM;
1699 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001700 }
Josef Bacik606686e2012-06-04 14:03:51 -04001701 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001702
1703 ret = find_next_devid(root, &device->devid);
1704 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001705 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001706 kfree(device);
1707 goto error;
1708 }
1709
Yan, Zhenga22285a2010-05-16 10:48:46 -04001710 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001711 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001712 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001713 kfree(device);
1714 ret = PTR_ERR(trans);
1715 goto error;
1716 }
1717
Yan Zheng2b820322008-11-17 21:11:30 -05001718 lock_chunks(root);
1719
Josef Bacikd5e20032011-08-04 14:52:27 +00001720 q = bdev_get_queue(bdev);
1721 if (blk_queue_discard(q))
1722 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001723 device->writeable = 1;
1724 device->work.func = pending_bios_fn;
1725 generate_random_uuid(device->uuid);
1726 spin_lock_init(&device->io_lock);
1727 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001728 device->io_width = root->sectorsize;
1729 device->io_align = root->sectorsize;
1730 device->sector_size = root->sectorsize;
1731 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001732 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001733 device->dev_root = root->fs_info->dev_root;
1734 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001735 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001736 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001737 set_blocksize(device->bdev, 4096);
1738
Yan Zheng2b820322008-11-17 21:11:30 -05001739 if (seeding_dev) {
1740 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001741 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001742 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001743 }
1744
1745 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001746
1747 /*
1748 * we don't want write_supers to jump in here with our device
1749 * half setup
1750 */
1751 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001752 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001753 list_add(&device->dev_alloc_list,
1754 &root->fs_info->fs_devices->alloc_list);
1755 root->fs_info->fs_devices->num_devices++;
1756 root->fs_info->fs_devices->open_devices++;
1757 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04001758 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001759 if (device->can_discard)
1760 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001761 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1762
Josef Bacik2bf64752011-09-26 17:12:22 -04001763 spin_lock(&root->fs_info->free_chunk_lock);
1764 root->fs_info->free_chunk_space += device->total_bytes;
1765 spin_unlock(&root->fs_info->free_chunk_lock);
1766
Chris Masonc2898112009-06-10 09:51:32 -04001767 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1768 root->fs_info->fs_devices->rotating = 1;
1769
David Sterba6c417612011-04-13 15:41:04 +02001770 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1771 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001772 total_bytes + device->total_bytes);
1773
David Sterba6c417612011-04-13 15:41:04 +02001774 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1775 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001776 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001777 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001778
Yan Zheng2b820322008-11-17 21:11:30 -05001779 if (seeding_dev) {
1780 ret = init_first_rw_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 ret = btrfs_finish_sprout(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001784 if (ret)
1785 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001786 } else {
1787 ret = btrfs_add_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001788 if (ret)
1789 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001790 }
1791
Chris Mason913d9522009-03-10 13:17:18 -04001792 /*
1793 * we've got more storage, clear any full flags on the space
1794 * infos
1795 */
1796 btrfs_clear_space_info_full(root->fs_info);
1797
Chris Mason7d9eb122008-07-08 14:19:17 -04001798 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001799 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001800
1801 if (seeding_dev) {
1802 mutex_unlock(&uuid_mutex);
1803 up_write(&sb->s_umount);
1804
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001805 if (ret) /* transaction commit */
1806 return ret;
1807
Yan Zheng2b820322008-11-17 21:11:30 -05001808 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001809 if (ret < 0)
1810 btrfs_error(root->fs_info, ret,
1811 "Failed to relocate sys chunks after "
1812 "device initialization. This can be fixed "
1813 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001814 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001815
Chris Mason788f20e2008-04-28 15:29:42 -04001816 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001817
1818error_trans:
1819 unlock_chunks(root);
1820 btrfs_abort_transaction(trans, root, ret);
1821 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04001822 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001823 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001824error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001825 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001826 if (seeding_dev) {
1827 mutex_unlock(&uuid_mutex);
1828 up_write(&sb->s_umount);
1829 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001830 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001831}
1832
Chris Masond3977122009-01-05 21:25:51 -05001833static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1834 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001835{
1836 int ret;
1837 struct btrfs_path *path;
1838 struct btrfs_root *root;
1839 struct btrfs_dev_item *dev_item;
1840 struct extent_buffer *leaf;
1841 struct btrfs_key key;
1842
1843 root = device->dev_root->fs_info->chunk_root;
1844
1845 path = btrfs_alloc_path();
1846 if (!path)
1847 return -ENOMEM;
1848
1849 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1850 key.type = BTRFS_DEV_ITEM_KEY;
1851 key.offset = device->devid;
1852
1853 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1854 if (ret < 0)
1855 goto out;
1856
1857 if (ret > 0) {
1858 ret = -ENOENT;
1859 goto out;
1860 }
1861
1862 leaf = path->nodes[0];
1863 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1864
1865 btrfs_set_device_id(leaf, dev_item, device->devid);
1866 btrfs_set_device_type(leaf, dev_item, device->type);
1867 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1868 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1869 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001870 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001871 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1872 btrfs_mark_buffer_dirty(leaf);
1873
1874out:
1875 btrfs_free_path(path);
1876 return ret;
1877}
1878
Chris Mason7d9eb122008-07-08 14:19:17 -04001879static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001880 struct btrfs_device *device, u64 new_size)
1881{
1882 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001883 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001884 u64 old_total = btrfs_super_total_bytes(super_copy);
1885 u64 diff = new_size - device->total_bytes;
1886
Yan Zheng2b820322008-11-17 21:11:30 -05001887 if (!device->writeable)
1888 return -EACCES;
1889 if (new_size <= device->total_bytes)
1890 return -EINVAL;
1891
Chris Mason8f18cf12008-04-25 16:53:30 -04001892 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001893 device->fs_devices->total_rw_bytes += diff;
1894
1895 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001896 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001897 btrfs_clear_space_info_full(device->dev_root->fs_info);
1898
Chris Mason8f18cf12008-04-25 16:53:30 -04001899 return btrfs_update_device(trans, device);
1900}
1901
Chris Mason7d9eb122008-07-08 14:19:17 -04001902int btrfs_grow_device(struct btrfs_trans_handle *trans,
1903 struct btrfs_device *device, u64 new_size)
1904{
1905 int ret;
1906 lock_chunks(device->dev_root);
1907 ret = __btrfs_grow_device(trans, device, new_size);
1908 unlock_chunks(device->dev_root);
1909 return ret;
1910}
1911
Chris Mason8f18cf12008-04-25 16:53:30 -04001912static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1913 struct btrfs_root *root,
1914 u64 chunk_tree, u64 chunk_objectid,
1915 u64 chunk_offset)
1916{
1917 int ret;
1918 struct btrfs_path *path;
1919 struct btrfs_key key;
1920
1921 root = root->fs_info->chunk_root;
1922 path = btrfs_alloc_path();
1923 if (!path)
1924 return -ENOMEM;
1925
1926 key.objectid = chunk_objectid;
1927 key.offset = chunk_offset;
1928 key.type = BTRFS_CHUNK_ITEM_KEY;
1929
1930 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001931 if (ret < 0)
1932 goto out;
1933 else if (ret > 0) { /* Logic error or corruption */
1934 btrfs_error(root->fs_info, -ENOENT,
1935 "Failed lookup while freeing chunk.");
1936 ret = -ENOENT;
1937 goto out;
1938 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001939
1940 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001941 if (ret < 0)
1942 btrfs_error(root->fs_info, ret,
1943 "Failed to delete chunk item.");
1944out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001945 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001946 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001947}
1948
Christoph Hellwigb2950862008-12-02 09:54:17 -05001949static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001950 chunk_offset)
1951{
David Sterba6c417612011-04-13 15:41:04 +02001952 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001953 struct btrfs_disk_key *disk_key;
1954 struct btrfs_chunk *chunk;
1955 u8 *ptr;
1956 int ret = 0;
1957 u32 num_stripes;
1958 u32 array_size;
1959 u32 len = 0;
1960 u32 cur;
1961 struct btrfs_key key;
1962
1963 array_size = btrfs_super_sys_array_size(super_copy);
1964
1965 ptr = super_copy->sys_chunk_array;
1966 cur = 0;
1967
1968 while (cur < array_size) {
1969 disk_key = (struct btrfs_disk_key *)ptr;
1970 btrfs_disk_key_to_cpu(&key, disk_key);
1971
1972 len = sizeof(*disk_key);
1973
1974 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1975 chunk = (struct btrfs_chunk *)(ptr + len);
1976 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1977 len += btrfs_chunk_item_size(num_stripes);
1978 } else {
1979 ret = -EIO;
1980 break;
1981 }
1982 if (key.objectid == chunk_objectid &&
1983 key.offset == chunk_offset) {
1984 memmove(ptr, ptr + len, array_size - (cur + len));
1985 array_size -= len;
1986 btrfs_set_super_sys_array_size(super_copy, array_size);
1987 } else {
1988 ptr += len;
1989 cur += len;
1990 }
1991 }
1992 return ret;
1993}
1994
Christoph Hellwigb2950862008-12-02 09:54:17 -05001995static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001996 u64 chunk_tree, u64 chunk_objectid,
1997 u64 chunk_offset)
1998{
1999 struct extent_map_tree *em_tree;
2000 struct btrfs_root *extent_root;
2001 struct btrfs_trans_handle *trans;
2002 struct extent_map *em;
2003 struct map_lookup *map;
2004 int ret;
2005 int i;
2006
2007 root = root->fs_info->chunk_root;
2008 extent_root = root->fs_info->extent_root;
2009 em_tree = &root->fs_info->mapping_tree.map_tree;
2010
Josef Bacikba1bf482009-09-11 16:11:19 -04002011 ret = btrfs_can_relocate(extent_root, chunk_offset);
2012 if (ret)
2013 return -ENOSPC;
2014
Chris Mason8f18cf12008-04-25 16:53:30 -04002015 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002016 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002017 if (ret)
2018 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002019
Yan, Zhenga22285a2010-05-16 10:48:46 -04002020 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002021 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002022
Chris Mason7d9eb122008-07-08 14:19:17 -04002023 lock_chunks(root);
2024
Chris Mason8f18cf12008-04-25 16:53:30 -04002025 /*
2026 * step two, delete the device extents and the
2027 * chunk tree entries
2028 */
Chris Mason890871b2009-09-02 16:24:52 -04002029 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002030 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002031 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002032
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002033 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002034 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002035 map = (struct map_lookup *)em->bdev;
2036
2037 for (i = 0; i < map->num_stripes; i++) {
2038 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2039 map->stripes[i].physical);
2040 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002041
Chris Masondfe25022008-05-13 13:46:40 -04002042 if (map->stripes[i].dev) {
2043 ret = btrfs_update_device(trans, map->stripes[i].dev);
2044 BUG_ON(ret);
2045 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002046 }
2047 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2048 chunk_offset);
2049
2050 BUG_ON(ret);
2051
liubo1abe9b82011-03-24 11:18:59 +00002052 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2053
Chris Mason8f18cf12008-04-25 16:53:30 -04002054 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2055 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2056 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002057 }
2058
Zheng Yan1a40e232008-09-26 10:09:34 -04002059 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2060 BUG_ON(ret);
2061
Chris Mason890871b2009-09-02 16:24:52 -04002062 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002063 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002064 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002065
Chris Mason8f18cf12008-04-25 16:53:30 -04002066 kfree(map);
2067 em->bdev = NULL;
2068
2069 /* once for the tree */
2070 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002071 /* once for us */
2072 free_extent_map(em);
2073
Chris Mason7d9eb122008-07-08 14:19:17 -04002074 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002075 btrfs_end_transaction(trans, root);
2076 return 0;
2077}
2078
Yan Zheng2b820322008-11-17 21:11:30 -05002079static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2080{
2081 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2082 struct btrfs_path *path;
2083 struct extent_buffer *leaf;
2084 struct btrfs_chunk *chunk;
2085 struct btrfs_key key;
2086 struct btrfs_key found_key;
2087 u64 chunk_tree = chunk_root->root_key.objectid;
2088 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002089 bool retried = false;
2090 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002091 int ret;
2092
2093 path = btrfs_alloc_path();
2094 if (!path)
2095 return -ENOMEM;
2096
Josef Bacikba1bf482009-09-11 16:11:19 -04002097again:
Yan Zheng2b820322008-11-17 21:11:30 -05002098 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2099 key.offset = (u64)-1;
2100 key.type = BTRFS_CHUNK_ITEM_KEY;
2101
2102 while (1) {
2103 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2104 if (ret < 0)
2105 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002106 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002107
2108 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2109 key.type);
2110 if (ret < 0)
2111 goto error;
2112 if (ret > 0)
2113 break;
2114
2115 leaf = path->nodes[0];
2116 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2117
2118 chunk = btrfs_item_ptr(leaf, path->slots[0],
2119 struct btrfs_chunk);
2120 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002121 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002122
2123 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2124 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2125 found_key.objectid,
2126 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002127 if (ret == -ENOSPC)
2128 failed++;
2129 else if (ret)
2130 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002131 }
2132
2133 if (found_key.offset == 0)
2134 break;
2135 key.offset = found_key.offset - 1;
2136 }
2137 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002138 if (failed && !retried) {
2139 failed = 0;
2140 retried = true;
2141 goto again;
2142 } else if (failed && retried) {
2143 WARN_ON(1);
2144 ret = -ENOSPC;
2145 }
Yan Zheng2b820322008-11-17 21:11:30 -05002146error:
2147 btrfs_free_path(path);
2148 return ret;
2149}
2150
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002151static int insert_balance_item(struct btrfs_root *root,
2152 struct btrfs_balance_control *bctl)
2153{
2154 struct btrfs_trans_handle *trans;
2155 struct btrfs_balance_item *item;
2156 struct btrfs_disk_balance_args disk_bargs;
2157 struct btrfs_path *path;
2158 struct extent_buffer *leaf;
2159 struct btrfs_key key;
2160 int ret, err;
2161
2162 path = btrfs_alloc_path();
2163 if (!path)
2164 return -ENOMEM;
2165
2166 trans = btrfs_start_transaction(root, 0);
2167 if (IS_ERR(trans)) {
2168 btrfs_free_path(path);
2169 return PTR_ERR(trans);
2170 }
2171
2172 key.objectid = BTRFS_BALANCE_OBJECTID;
2173 key.type = BTRFS_BALANCE_ITEM_KEY;
2174 key.offset = 0;
2175
2176 ret = btrfs_insert_empty_item(trans, root, path, &key,
2177 sizeof(*item));
2178 if (ret)
2179 goto out;
2180
2181 leaf = path->nodes[0];
2182 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2183
2184 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2185
2186 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2187 btrfs_set_balance_data(leaf, item, &disk_bargs);
2188 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2189 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2190 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2191 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2192
2193 btrfs_set_balance_flags(leaf, item, bctl->flags);
2194
2195 btrfs_mark_buffer_dirty(leaf);
2196out:
2197 btrfs_free_path(path);
2198 err = btrfs_commit_transaction(trans, root);
2199 if (err && !ret)
2200 ret = err;
2201 return ret;
2202}
2203
2204static int del_balance_item(struct btrfs_root *root)
2205{
2206 struct btrfs_trans_handle *trans;
2207 struct btrfs_path *path;
2208 struct btrfs_key key;
2209 int ret, err;
2210
2211 path = btrfs_alloc_path();
2212 if (!path)
2213 return -ENOMEM;
2214
2215 trans = btrfs_start_transaction(root, 0);
2216 if (IS_ERR(trans)) {
2217 btrfs_free_path(path);
2218 return PTR_ERR(trans);
2219 }
2220
2221 key.objectid = BTRFS_BALANCE_OBJECTID;
2222 key.type = BTRFS_BALANCE_ITEM_KEY;
2223 key.offset = 0;
2224
2225 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2226 if (ret < 0)
2227 goto out;
2228 if (ret > 0) {
2229 ret = -ENOENT;
2230 goto out;
2231 }
2232
2233 ret = btrfs_del_item(trans, root, path);
2234out:
2235 btrfs_free_path(path);
2236 err = btrfs_commit_transaction(trans, root);
2237 if (err && !ret)
2238 ret = err;
2239 return ret;
2240}
2241
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002242/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002243 * This is a heuristic used to reduce the number of chunks balanced on
2244 * resume after balance was interrupted.
2245 */
2246static void update_balance_args(struct btrfs_balance_control *bctl)
2247{
2248 /*
2249 * Turn on soft mode for chunk types that were being converted.
2250 */
2251 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2252 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2253 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2254 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2255 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2256 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2257
2258 /*
2259 * Turn on usage filter if is not already used. The idea is
2260 * that chunks that we have already balanced should be
2261 * reasonably full. Don't do it for chunks that are being
2262 * converted - that will keep us from relocating unconverted
2263 * (albeit full) chunks.
2264 */
2265 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2266 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2267 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2268 bctl->data.usage = 90;
2269 }
2270 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2271 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2272 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2273 bctl->sys.usage = 90;
2274 }
2275 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2276 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2277 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2278 bctl->meta.usage = 90;
2279 }
2280}
2281
2282/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002283 * Should be called with both balance and volume mutexes held to
2284 * serialize other volume operations (add_dev/rm_dev/resize) with
2285 * restriper. Same goes for unset_balance_control.
2286 */
2287static void set_balance_control(struct btrfs_balance_control *bctl)
2288{
2289 struct btrfs_fs_info *fs_info = bctl->fs_info;
2290
2291 BUG_ON(fs_info->balance_ctl);
2292
2293 spin_lock(&fs_info->balance_lock);
2294 fs_info->balance_ctl = bctl;
2295 spin_unlock(&fs_info->balance_lock);
2296}
2297
2298static void unset_balance_control(struct btrfs_fs_info *fs_info)
2299{
2300 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2301
2302 BUG_ON(!fs_info->balance_ctl);
2303
2304 spin_lock(&fs_info->balance_lock);
2305 fs_info->balance_ctl = NULL;
2306 spin_unlock(&fs_info->balance_lock);
2307
2308 kfree(bctl);
2309}
2310
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002311/*
2312 * Balance filters. Return 1 if chunk should be filtered out
2313 * (should not be balanced).
2314 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002315static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002316 struct btrfs_balance_args *bargs)
2317{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002318 chunk_type = chunk_to_extended(chunk_type) &
2319 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002320
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002321 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002322 return 0;
2323
2324 return 1;
2325}
2326
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002327static u64 div_factor_fine(u64 num, int factor)
2328{
2329 if (factor <= 0)
2330 return 0;
2331 if (factor >= 100)
2332 return num;
2333
2334 num *= factor;
2335 do_div(num, 100);
2336 return num;
2337}
2338
2339static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2340 struct btrfs_balance_args *bargs)
2341{
2342 struct btrfs_block_group_cache *cache;
2343 u64 chunk_used, user_thresh;
2344 int ret = 1;
2345
2346 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2347 chunk_used = btrfs_block_group_used(&cache->item);
2348
2349 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2350 if (chunk_used < user_thresh)
2351 ret = 0;
2352
2353 btrfs_put_block_group(cache);
2354 return ret;
2355}
2356
Ilya Dryomov409d4042012-01-16 22:04:47 +02002357static int chunk_devid_filter(struct extent_buffer *leaf,
2358 struct btrfs_chunk *chunk,
2359 struct btrfs_balance_args *bargs)
2360{
2361 struct btrfs_stripe *stripe;
2362 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2363 int i;
2364
2365 for (i = 0; i < num_stripes; i++) {
2366 stripe = btrfs_stripe_nr(chunk, i);
2367 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2368 return 0;
2369 }
2370
2371 return 1;
2372}
2373
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002374/* [pstart, pend) */
2375static int chunk_drange_filter(struct extent_buffer *leaf,
2376 struct btrfs_chunk *chunk,
2377 u64 chunk_offset,
2378 struct btrfs_balance_args *bargs)
2379{
2380 struct btrfs_stripe *stripe;
2381 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2382 u64 stripe_offset;
2383 u64 stripe_length;
2384 int factor;
2385 int i;
2386
2387 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2388 return 0;
2389
2390 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2391 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2392 factor = 2;
2393 else
2394 factor = 1;
2395 factor = num_stripes / factor;
2396
2397 for (i = 0; i < num_stripes; i++) {
2398 stripe = btrfs_stripe_nr(chunk, i);
2399 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2400 continue;
2401
2402 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2403 stripe_length = btrfs_chunk_length(leaf, chunk);
2404 do_div(stripe_length, factor);
2405
2406 if (stripe_offset < bargs->pend &&
2407 stripe_offset + stripe_length > bargs->pstart)
2408 return 0;
2409 }
2410
2411 return 1;
2412}
2413
Ilya Dryomovea671762012-01-16 22:04:48 +02002414/* [vstart, vend) */
2415static int chunk_vrange_filter(struct extent_buffer *leaf,
2416 struct btrfs_chunk *chunk,
2417 u64 chunk_offset,
2418 struct btrfs_balance_args *bargs)
2419{
2420 if (chunk_offset < bargs->vend &&
2421 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2422 /* at least part of the chunk is inside this vrange */
2423 return 0;
2424
2425 return 1;
2426}
2427
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002428static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002429 struct btrfs_balance_args *bargs)
2430{
2431 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2432 return 0;
2433
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002434 chunk_type = chunk_to_extended(chunk_type) &
2435 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002436
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002437 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002438 return 1;
2439
2440 return 0;
2441}
2442
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002443static int should_balance_chunk(struct btrfs_root *root,
2444 struct extent_buffer *leaf,
2445 struct btrfs_chunk *chunk, u64 chunk_offset)
2446{
2447 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2448 struct btrfs_balance_args *bargs = NULL;
2449 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2450
2451 /* type filter */
2452 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2453 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2454 return 0;
2455 }
2456
2457 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2458 bargs = &bctl->data;
2459 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2460 bargs = &bctl->sys;
2461 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2462 bargs = &bctl->meta;
2463
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002464 /* profiles filter */
2465 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2466 chunk_profiles_filter(chunk_type, bargs)) {
2467 return 0;
2468 }
2469
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002470 /* usage filter */
2471 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2472 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2473 return 0;
2474 }
2475
Ilya Dryomov409d4042012-01-16 22:04:47 +02002476 /* devid filter */
2477 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2478 chunk_devid_filter(leaf, chunk, bargs)) {
2479 return 0;
2480 }
2481
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002482 /* drange filter, makes sense only with devid filter */
2483 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2484 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2485 return 0;
2486 }
2487
Ilya Dryomovea671762012-01-16 22:04:48 +02002488 /* vrange filter */
2489 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2490 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2491 return 0;
2492 }
2493
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002494 /* soft profile changing mode */
2495 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2496 chunk_soft_convert_filter(chunk_type, bargs)) {
2497 return 0;
2498 }
2499
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002500 return 1;
2501}
2502
Chris Masonec44a352008-04-28 15:29:52 -04002503static u64 div_factor(u64 num, int factor)
2504{
2505 if (factor == 10)
2506 return num;
2507 num *= factor;
2508 do_div(num, 10);
2509 return num;
2510}
2511
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002512static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002513{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002514 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002515 struct btrfs_root *chunk_root = fs_info->chunk_root;
2516 struct btrfs_root *dev_root = fs_info->dev_root;
2517 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002518 struct btrfs_device *device;
2519 u64 old_size;
2520 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002521 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002522 struct btrfs_path *path;
2523 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002524 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002525 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002526 struct extent_buffer *leaf;
2527 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002528 int ret;
2529 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002530 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002531
Chris Masonec44a352008-04-28 15:29:52 -04002532 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002533 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002534 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002535 old_size = device->total_bytes;
2536 size_to_free = div_factor(old_size, 1);
2537 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002538 if (!device->writeable ||
2539 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002540 continue;
2541
2542 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002543 if (ret == -ENOSPC)
2544 break;
Chris Masonec44a352008-04-28 15:29:52 -04002545 BUG_ON(ret);
2546
Yan, Zhenga22285a2010-05-16 10:48:46 -04002547 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002548 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002549
2550 ret = btrfs_grow_device(trans, device, old_size);
2551 BUG_ON(ret);
2552
2553 btrfs_end_transaction(trans, dev_root);
2554 }
2555
2556 /* step two, relocate all the chunks */
2557 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002558 if (!path) {
2559 ret = -ENOMEM;
2560 goto error;
2561 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002562
2563 /* zero out stat counters */
2564 spin_lock(&fs_info->balance_lock);
2565 memset(&bctl->stat, 0, sizeof(bctl->stat));
2566 spin_unlock(&fs_info->balance_lock);
2567again:
Chris Masonec44a352008-04-28 15:29:52 -04002568 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2569 key.offset = (u64)-1;
2570 key.type = BTRFS_CHUNK_ITEM_KEY;
2571
Chris Masond3977122009-01-05 21:25:51 -05002572 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002573 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002574 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002575 ret = -ECANCELED;
2576 goto error;
2577 }
2578
Chris Masonec44a352008-04-28 15:29:52 -04002579 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2580 if (ret < 0)
2581 goto error;
2582
2583 /*
2584 * this shouldn't happen, it means the last relocate
2585 * failed
2586 */
2587 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002588 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002589
2590 ret = btrfs_previous_item(chunk_root, path, 0,
2591 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002592 if (ret) {
2593 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002594 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002595 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002596
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002597 leaf = path->nodes[0];
2598 slot = path->slots[0];
2599 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2600
Chris Masonec44a352008-04-28 15:29:52 -04002601 if (found_key.objectid != key.objectid)
2602 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002603
Chris Masonec44a352008-04-28 15:29:52 -04002604 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002605 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002606 break;
2607
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002608 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2609
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002610 if (!counting) {
2611 spin_lock(&fs_info->balance_lock);
2612 bctl->stat.considered++;
2613 spin_unlock(&fs_info->balance_lock);
2614 }
2615
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002616 ret = should_balance_chunk(chunk_root, leaf, chunk,
2617 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002618 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002619 if (!ret)
2620 goto loop;
2621
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002622 if (counting) {
2623 spin_lock(&fs_info->balance_lock);
2624 bctl->stat.expected++;
2625 spin_unlock(&fs_info->balance_lock);
2626 goto loop;
2627 }
2628
Chris Masonec44a352008-04-28 15:29:52 -04002629 ret = btrfs_relocate_chunk(chunk_root,
2630 chunk_root->root_key.objectid,
2631 found_key.objectid,
2632 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002633 if (ret && ret != -ENOSPC)
2634 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002635 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002636 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002637 } else {
2638 spin_lock(&fs_info->balance_lock);
2639 bctl->stat.completed++;
2640 spin_unlock(&fs_info->balance_lock);
2641 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002642loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002643 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002644 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002645
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002646 if (counting) {
2647 btrfs_release_path(path);
2648 counting = false;
2649 goto again;
2650 }
Chris Masonec44a352008-04-28 15:29:52 -04002651error:
2652 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002653 if (enospc_errors) {
2654 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2655 enospc_errors);
2656 if (!ret)
2657 ret = -ENOSPC;
2658 }
2659
Chris Masonec44a352008-04-28 15:29:52 -04002660 return ret;
2661}
2662
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002663/**
2664 * alloc_profile_is_valid - see if a given profile is valid and reduced
2665 * @flags: profile to validate
2666 * @extended: if true @flags is treated as an extended profile
2667 */
2668static int alloc_profile_is_valid(u64 flags, int extended)
2669{
2670 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2671 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2672
2673 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2674
2675 /* 1) check that all other bits are zeroed */
2676 if (flags & ~mask)
2677 return 0;
2678
2679 /* 2) see if profile is reduced */
2680 if (flags == 0)
2681 return !extended; /* "0" is valid for usual profiles */
2682
2683 /* true if exactly one bit set */
2684 return (flags & (flags - 1)) == 0;
2685}
2686
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002687static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2688{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002689 /* cancel requested || normal exit path */
2690 return atomic_read(&fs_info->balance_cancel_req) ||
2691 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2692 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002693}
2694
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002695static void __cancel_balance(struct btrfs_fs_info *fs_info)
2696{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002697 int ret;
2698
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002699 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002700 ret = del_balance_item(fs_info->tree_root);
2701 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002702}
2703
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002704void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002705 struct btrfs_ioctl_balance_args *bargs);
2706
2707/*
2708 * Should be called with both balance and volume mutexes held
2709 */
2710int btrfs_balance(struct btrfs_balance_control *bctl,
2711 struct btrfs_ioctl_balance_args *bargs)
2712{
2713 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002714 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002715 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002716 int ret;
2717
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002718 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002719 atomic_read(&fs_info->balance_pause_req) ||
2720 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002721 ret = -EINVAL;
2722 goto out;
2723 }
2724
Ilya Dryomove4837f82012-03-27 17:09:17 +03002725 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2726 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2727 mixed = 1;
2728
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002729 /*
2730 * In case of mixed groups both data and meta should be picked,
2731 * and identical options should be given for both of them.
2732 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002733 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2734 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002735 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2736 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2737 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2738 printk(KERN_ERR "btrfs: with mixed groups data and "
2739 "metadata balance options must be the same\n");
2740 ret = -EINVAL;
2741 goto out;
2742 }
2743 }
2744
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002745 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2746 if (fs_info->fs_devices->num_devices == 1)
2747 allowed |= BTRFS_BLOCK_GROUP_DUP;
2748 else if (fs_info->fs_devices->num_devices < 4)
2749 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2750 else
2751 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2752 BTRFS_BLOCK_GROUP_RAID10);
2753
Ilya Dryomov6728b192012-03-27 17:09:17 +03002754 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2755 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2756 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002757 printk(KERN_ERR "btrfs: unable to start balance with target "
2758 "data profile %llu\n",
2759 (unsigned long long)bctl->data.target);
2760 ret = -EINVAL;
2761 goto out;
2762 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002763 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2764 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2765 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002766 printk(KERN_ERR "btrfs: unable to start balance with target "
2767 "metadata profile %llu\n",
2768 (unsigned long long)bctl->meta.target);
2769 ret = -EINVAL;
2770 goto out;
2771 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002772 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2773 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2774 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002775 printk(KERN_ERR "btrfs: unable to start balance with target "
2776 "system profile %llu\n",
2777 (unsigned long long)bctl->sys.target);
2778 ret = -EINVAL;
2779 goto out;
2780 }
2781
Ilya Dryomove4837f82012-03-27 17:09:17 +03002782 /* allow dup'ed data chunks only in mixed mode */
2783 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002784 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002785 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2786 ret = -EINVAL;
2787 goto out;
2788 }
2789
2790 /* allow to reduce meta or sys integrity only if force set */
2791 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2792 BTRFS_BLOCK_GROUP_RAID10;
2793 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2794 (fs_info->avail_system_alloc_bits & allowed) &&
2795 !(bctl->sys.target & allowed)) ||
2796 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2797 (fs_info->avail_metadata_alloc_bits & allowed) &&
2798 !(bctl->meta.target & allowed))) {
2799 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2800 printk(KERN_INFO "btrfs: force reducing metadata "
2801 "integrity\n");
2802 } else {
2803 printk(KERN_ERR "btrfs: balance will reduce metadata "
2804 "integrity, use force if you want this\n");
2805 ret = -EINVAL;
2806 goto out;
2807 }
2808 }
2809
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002810 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002811 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002812 goto out;
2813
Ilya Dryomov59641012012-01-16 22:04:48 +02002814 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2815 BUG_ON(ret == -EEXIST);
2816 set_balance_control(bctl);
2817 } else {
2818 BUG_ON(ret != -EEXIST);
2819 spin_lock(&fs_info->balance_lock);
2820 update_balance_args(bctl);
2821 spin_unlock(&fs_info->balance_lock);
2822 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002823
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002824 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002825 mutex_unlock(&fs_info->balance_mutex);
2826
2827 ret = __btrfs_balance(fs_info);
2828
2829 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002830 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002831
2832 if (bargs) {
2833 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002834 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002835 }
2836
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002837 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2838 balance_need_close(fs_info)) {
2839 __cancel_balance(fs_info);
2840 }
2841
2842 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002843
2844 return ret;
2845out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002846 if (bctl->flags & BTRFS_BALANCE_RESUME)
2847 __cancel_balance(fs_info);
2848 else
2849 kfree(bctl);
2850 return ret;
2851}
2852
2853static int balance_kthread(void *data)
2854{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002855 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002856 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002857
2858 mutex_lock(&fs_info->volume_mutex);
2859 mutex_lock(&fs_info->balance_mutex);
2860
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002861 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002862 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002863 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002864 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002865
2866 mutex_unlock(&fs_info->balance_mutex);
2867 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002868
Ilya Dryomov59641012012-01-16 22:04:48 +02002869 return ret;
2870}
2871
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002872int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
2873{
2874 struct task_struct *tsk;
2875
2876 spin_lock(&fs_info->balance_lock);
2877 if (!fs_info->balance_ctl) {
2878 spin_unlock(&fs_info->balance_lock);
2879 return 0;
2880 }
2881 spin_unlock(&fs_info->balance_lock);
2882
2883 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2884 printk(KERN_INFO "btrfs: force skipping balance\n");
2885 return 0;
2886 }
2887
2888 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
2889 if (IS_ERR(tsk))
2890 return PTR_ERR(tsk);
2891
2892 return 0;
2893}
2894
Ilya Dryomov68310a52012-06-22 12:24:12 -06002895int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02002896{
Ilya Dryomov59641012012-01-16 22:04:48 +02002897 struct btrfs_balance_control *bctl;
2898 struct btrfs_balance_item *item;
2899 struct btrfs_disk_balance_args disk_bargs;
2900 struct btrfs_path *path;
2901 struct extent_buffer *leaf;
2902 struct btrfs_key key;
2903 int ret;
2904
2905 path = btrfs_alloc_path();
2906 if (!path)
2907 return -ENOMEM;
2908
Ilya Dryomov68310a52012-06-22 12:24:12 -06002909 key.objectid = BTRFS_BALANCE_OBJECTID;
2910 key.type = BTRFS_BALANCE_ITEM_KEY;
2911 key.offset = 0;
2912
2913 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2914 if (ret < 0)
2915 goto out;
2916 if (ret > 0) { /* ret = -ENOENT; */
2917 ret = 0;
2918 goto out;
2919 }
2920
Ilya Dryomov59641012012-01-16 22:04:48 +02002921 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2922 if (!bctl) {
2923 ret = -ENOMEM;
2924 goto out;
2925 }
2926
Ilya Dryomov59641012012-01-16 22:04:48 +02002927 leaf = path->nodes[0];
2928 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2929
Ilya Dryomov68310a52012-06-22 12:24:12 -06002930 bctl->fs_info = fs_info;
2931 bctl->flags = btrfs_balance_flags(leaf, item);
2932 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02002933
2934 btrfs_balance_data(leaf, item, &disk_bargs);
2935 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2936 btrfs_balance_meta(leaf, item, &disk_bargs);
2937 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2938 btrfs_balance_sys(leaf, item, &disk_bargs);
2939 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2940
Ilya Dryomov68310a52012-06-22 12:24:12 -06002941 mutex_lock(&fs_info->volume_mutex);
2942 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02002943
Ilya Dryomov68310a52012-06-22 12:24:12 -06002944 set_balance_control(bctl);
2945
2946 mutex_unlock(&fs_info->balance_mutex);
2947 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02002948out:
2949 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002950 return ret;
2951}
2952
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002953int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2954{
2955 int ret = 0;
2956
2957 mutex_lock(&fs_info->balance_mutex);
2958 if (!fs_info->balance_ctl) {
2959 mutex_unlock(&fs_info->balance_mutex);
2960 return -ENOTCONN;
2961 }
2962
2963 if (atomic_read(&fs_info->balance_running)) {
2964 atomic_inc(&fs_info->balance_pause_req);
2965 mutex_unlock(&fs_info->balance_mutex);
2966
2967 wait_event(fs_info->balance_wait_q,
2968 atomic_read(&fs_info->balance_running) == 0);
2969
2970 mutex_lock(&fs_info->balance_mutex);
2971 /* we are good with balance_ctl ripped off from under us */
2972 BUG_ON(atomic_read(&fs_info->balance_running));
2973 atomic_dec(&fs_info->balance_pause_req);
2974 } else {
2975 ret = -ENOTCONN;
2976 }
2977
2978 mutex_unlock(&fs_info->balance_mutex);
2979 return ret;
2980}
2981
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002982int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2983{
2984 mutex_lock(&fs_info->balance_mutex);
2985 if (!fs_info->balance_ctl) {
2986 mutex_unlock(&fs_info->balance_mutex);
2987 return -ENOTCONN;
2988 }
2989
2990 atomic_inc(&fs_info->balance_cancel_req);
2991 /*
2992 * if we are running just wait and return, balance item is
2993 * deleted in btrfs_balance in this case
2994 */
2995 if (atomic_read(&fs_info->balance_running)) {
2996 mutex_unlock(&fs_info->balance_mutex);
2997 wait_event(fs_info->balance_wait_q,
2998 atomic_read(&fs_info->balance_running) == 0);
2999 mutex_lock(&fs_info->balance_mutex);
3000 } else {
3001 /* __cancel_balance needs volume_mutex */
3002 mutex_unlock(&fs_info->balance_mutex);
3003 mutex_lock(&fs_info->volume_mutex);
3004 mutex_lock(&fs_info->balance_mutex);
3005
3006 if (fs_info->balance_ctl)
3007 __cancel_balance(fs_info);
3008
3009 mutex_unlock(&fs_info->volume_mutex);
3010 }
3011
3012 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3013 atomic_dec(&fs_info->balance_cancel_req);
3014 mutex_unlock(&fs_info->balance_mutex);
3015 return 0;
3016}
3017
Chris Mason8f18cf12008-04-25 16:53:30 -04003018/*
3019 * shrinking a device means finding all of the device extents past
3020 * the new size, and then following the back refs to the chunks.
3021 * The chunk relocation code actually frees the device extent
3022 */
3023int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3024{
3025 struct btrfs_trans_handle *trans;
3026 struct btrfs_root *root = device->dev_root;
3027 struct btrfs_dev_extent *dev_extent = NULL;
3028 struct btrfs_path *path;
3029 u64 length;
3030 u64 chunk_tree;
3031 u64 chunk_objectid;
3032 u64 chunk_offset;
3033 int ret;
3034 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003035 int failed = 0;
3036 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003037 struct extent_buffer *l;
3038 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003039 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003040 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003041 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003042 u64 diff = device->total_bytes - new_size;
3043
Yan Zheng2b820322008-11-17 21:11:30 -05003044 if (new_size >= device->total_bytes)
3045 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04003046
3047 path = btrfs_alloc_path();
3048 if (!path)
3049 return -ENOMEM;
3050
Chris Mason8f18cf12008-04-25 16:53:30 -04003051 path->reada = 2;
3052
Chris Mason7d9eb122008-07-08 14:19:17 -04003053 lock_chunks(root);
3054
Chris Mason8f18cf12008-04-25 16:53:30 -04003055 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003056 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003057 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003058 spin_lock(&root->fs_info->free_chunk_lock);
3059 root->fs_info->free_chunk_space -= diff;
3060 spin_unlock(&root->fs_info->free_chunk_lock);
3061 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003062 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003063
Josef Bacikba1bf482009-09-11 16:11:19 -04003064again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003065 key.objectid = device->devid;
3066 key.offset = (u64)-1;
3067 key.type = BTRFS_DEV_EXTENT_KEY;
3068
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003069 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003070 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3071 if (ret < 0)
3072 goto done;
3073
3074 ret = btrfs_previous_item(root, path, 0, key.type);
3075 if (ret < 0)
3076 goto done;
3077 if (ret) {
3078 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003079 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003080 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003081 }
3082
3083 l = path->nodes[0];
3084 slot = path->slots[0];
3085 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3086
Josef Bacikba1bf482009-09-11 16:11:19 -04003087 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003088 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003089 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003090 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003091
3092 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3093 length = btrfs_dev_extent_length(l, dev_extent);
3094
Josef Bacikba1bf482009-09-11 16:11:19 -04003095 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003096 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003097 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003098 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003099
3100 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3101 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3102 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003103 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003104
3105 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3106 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003107 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003108 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003109 if (ret == -ENOSPC)
3110 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003111 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003112
3113 if (failed && !retried) {
3114 failed = 0;
3115 retried = true;
3116 goto again;
3117 } else if (failed && retried) {
3118 ret = -ENOSPC;
3119 lock_chunks(root);
3120
3121 device->total_bytes = old_size;
3122 if (device->writeable)
3123 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003124 spin_lock(&root->fs_info->free_chunk_lock);
3125 root->fs_info->free_chunk_space += diff;
3126 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003127 unlock_chunks(root);
3128 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003129 }
3130
Chris Balld6397ba2009-04-27 07:29:03 -04003131 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003132 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003133 if (IS_ERR(trans)) {
3134 ret = PTR_ERR(trans);
3135 goto done;
3136 }
3137
Chris Balld6397ba2009-04-27 07:29:03 -04003138 lock_chunks(root);
3139
3140 device->disk_total_bytes = new_size;
3141 /* Now btrfs_update_device() will change the on-disk size. */
3142 ret = btrfs_update_device(trans, device);
3143 if (ret) {
3144 unlock_chunks(root);
3145 btrfs_end_transaction(trans, root);
3146 goto done;
3147 }
3148 WARN_ON(diff > old_total);
3149 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3150 unlock_chunks(root);
3151 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003152done:
3153 btrfs_free_path(path);
3154 return ret;
3155}
3156
Li Zefan125ccb02011-12-08 15:07:24 +08003157static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003158 struct btrfs_key *key,
3159 struct btrfs_chunk *chunk, int item_size)
3160{
David Sterba6c417612011-04-13 15:41:04 +02003161 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003162 struct btrfs_disk_key disk_key;
3163 u32 array_size;
3164 u8 *ptr;
3165
3166 array_size = btrfs_super_sys_array_size(super_copy);
3167 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3168 return -EFBIG;
3169
3170 ptr = super_copy->sys_chunk_array + array_size;
3171 btrfs_cpu_key_to_disk(&disk_key, key);
3172 memcpy(ptr, &disk_key, sizeof(disk_key));
3173 ptr += sizeof(disk_key);
3174 memcpy(ptr, chunk, item_size);
3175 item_size += sizeof(disk_key);
3176 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3177 return 0;
3178}
3179
Miao Xieb2117a32011-01-05 10:07:28 +00003180/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003181 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003182 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003183static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003184{
Arne Jansen73c5de02011-04-12 12:07:57 +02003185 const struct btrfs_device_info *di_a = a;
3186 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003187
Arne Jansen73c5de02011-04-12 12:07:57 +02003188 if (di_a->max_avail > di_b->max_avail)
3189 return -1;
3190 if (di_a->max_avail < di_b->max_avail)
3191 return 1;
3192 if (di_a->total_avail > di_b->total_avail)
3193 return -1;
3194 if (di_a->total_avail < di_b->total_avail)
3195 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003196 return 0;
3197}
3198
3199static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3200 struct btrfs_root *extent_root,
3201 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003202 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003203 u64 start, u64 type)
3204{
3205 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003206 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3207 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003208 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003209 struct extent_map_tree *em_tree;
3210 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003211 struct btrfs_device_info *devices_info = NULL;
3212 u64 total_avail;
3213 int num_stripes; /* total number of stripes to allocate */
3214 int sub_stripes; /* sub_stripes info for map */
3215 int dev_stripes; /* stripes per dev */
3216 int devs_max; /* max devs to use */
3217 int devs_min; /* min devs needed */
3218 int devs_increment; /* ndevs has to be a multiple of this */
3219 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003220 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003221 u64 max_stripe_size;
3222 u64 max_chunk_size;
3223 u64 stripe_size;
3224 u64 num_bytes;
3225 int ndevs;
3226 int i;
3227 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003228
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003229 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003230
Miao Xieb2117a32011-01-05 10:07:28 +00003231 if (list_empty(&fs_devices->alloc_list))
3232 return -ENOSPC;
3233
Arne Jansen73c5de02011-04-12 12:07:57 +02003234 sub_stripes = 1;
3235 dev_stripes = 1;
3236 devs_increment = 1;
3237 ncopies = 1;
3238 devs_max = 0; /* 0 == as many as possible */
3239 devs_min = 1;
3240
3241 /*
3242 * define the properties of each RAID type.
3243 * FIXME: move this to a global table and use it in all RAID
3244 * calculation code
3245 */
3246 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3247 dev_stripes = 2;
3248 ncopies = 2;
3249 devs_max = 1;
3250 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3251 devs_min = 2;
3252 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3253 devs_increment = 2;
3254 ncopies = 2;
3255 devs_max = 2;
3256 devs_min = 2;
3257 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3258 sub_stripes = 2;
3259 devs_increment = 2;
3260 ncopies = 2;
3261 devs_min = 4;
3262 } else {
3263 devs_max = 1;
3264 }
3265
3266 if (type & BTRFS_BLOCK_GROUP_DATA) {
3267 max_stripe_size = 1024 * 1024 * 1024;
3268 max_chunk_size = 10 * max_stripe_size;
3269 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003270 /* for larger filesystems, use larger metadata chunks */
3271 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3272 max_stripe_size = 1024 * 1024 * 1024;
3273 else
3274 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003275 max_chunk_size = max_stripe_size;
3276 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003277 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003278 max_chunk_size = 2 * max_stripe_size;
3279 } else {
3280 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3281 type);
3282 BUG_ON(1);
3283 }
3284
3285 /* we don't want a chunk larger than 10% of writeable space */
3286 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3287 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003288
3289 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3290 GFP_NOFS);
3291 if (!devices_info)
3292 return -ENOMEM;
3293
Arne Jansen73c5de02011-04-12 12:07:57 +02003294 cur = fs_devices->alloc_list.next;
3295
3296 /*
3297 * in the first pass through the devices list, we gather information
3298 * about the available holes on each device.
3299 */
3300 ndevs = 0;
3301 while (cur != &fs_devices->alloc_list) {
3302 struct btrfs_device *device;
3303 u64 max_avail;
3304 u64 dev_offset;
3305
3306 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3307
3308 cur = cur->next;
3309
3310 if (!device->writeable) {
3311 printk(KERN_ERR
3312 "btrfs: read-only device in alloc_list\n");
3313 WARN_ON(1);
3314 continue;
3315 }
3316
3317 if (!device->in_fs_metadata)
3318 continue;
3319
3320 if (device->total_bytes > device->bytes_used)
3321 total_avail = device->total_bytes - device->bytes_used;
3322 else
3323 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003324
3325 /* If there is no space on this device, skip it. */
3326 if (total_avail == 0)
3327 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003328
Li Zefan125ccb02011-12-08 15:07:24 +08003329 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003330 max_stripe_size * dev_stripes,
3331 &dev_offset, &max_avail);
3332 if (ret && ret != -ENOSPC)
3333 goto error;
3334
3335 if (ret == 0)
3336 max_avail = max_stripe_size * dev_stripes;
3337
3338 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3339 continue;
3340
3341 devices_info[ndevs].dev_offset = dev_offset;
3342 devices_info[ndevs].max_avail = max_avail;
3343 devices_info[ndevs].total_avail = total_avail;
3344 devices_info[ndevs].dev = device;
3345 ++ndevs;
3346 }
3347
3348 /*
3349 * now sort the devices by hole size / available space
3350 */
3351 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3352 btrfs_cmp_device_info, NULL);
3353
3354 /* round down to number of usable stripes */
3355 ndevs -= ndevs % devs_increment;
3356
3357 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3358 ret = -ENOSPC;
3359 goto error;
3360 }
3361
3362 if (devs_max && ndevs > devs_max)
3363 ndevs = devs_max;
3364 /*
3365 * the primary goal is to maximize the number of stripes, so use as many
3366 * devices as possible, even if the stripes are not maximum sized.
3367 */
3368 stripe_size = devices_info[ndevs-1].max_avail;
3369 num_stripes = ndevs * dev_stripes;
3370
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003371 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003372 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003373 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003374 }
3375
3376 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003377
3378 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003379 do_div(stripe_size, BTRFS_STRIPE_LEN);
3380 stripe_size *= BTRFS_STRIPE_LEN;
3381
Miao Xieb2117a32011-01-05 10:07:28 +00003382 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3383 if (!map) {
3384 ret = -ENOMEM;
3385 goto error;
3386 }
3387 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003388
Arne Jansen73c5de02011-04-12 12:07:57 +02003389 for (i = 0; i < ndevs; ++i) {
3390 for (j = 0; j < dev_stripes; ++j) {
3391 int s = i * dev_stripes + j;
3392 map->stripes[s].dev = devices_info[i].dev;
3393 map->stripes[s].physical = devices_info[i].dev_offset +
3394 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003395 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003396 }
Chris Mason593060d2008-03-25 16:50:33 -04003397 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003398 map->stripe_len = BTRFS_STRIPE_LEN;
3399 map->io_align = BTRFS_STRIPE_LEN;
3400 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003401 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003402 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003403
Yan Zheng2b820322008-11-17 21:11:30 -05003404 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003405 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003406
Arne Jansen73c5de02011-04-12 12:07:57 +02003407 *stripe_size_out = stripe_size;
3408 *num_bytes_out = num_bytes;
3409
3410 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003411
David Sterba172ddd62011-04-21 00:48:27 +02003412 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003413 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003414 ret = -ENOMEM;
3415 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003416 }
Chris Mason0b86a832008-03-24 15:01:56 -04003417 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003418 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003419 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003420 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003421 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003422
Chris Mason0b86a832008-03-24 15:01:56 -04003423 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003424 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003425 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003426 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003427 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003428 if (ret)
3429 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003430
3431 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3432 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003433 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003434 if (ret)
3435 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003436
Arne Jansen73c5de02011-04-12 12:07:57 +02003437 for (i = 0; i < map->num_stripes; ++i) {
3438 struct btrfs_device *device;
3439 u64 dev_offset;
3440
3441 device = map->stripes[i].dev;
3442 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003443
3444 ret = btrfs_alloc_dev_extent(trans, device,
3445 info->chunk_root->root_key.objectid,
3446 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003447 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003448 if (ret) {
3449 btrfs_abort_transaction(trans, extent_root, ret);
3450 goto error;
3451 }
Yan Zheng2b820322008-11-17 21:11:30 -05003452 }
3453
Miao Xieb2117a32011-01-05 10:07:28 +00003454 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003455 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003456
3457error:
3458 kfree(map);
3459 kfree(devices_info);
3460 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003461}
3462
3463static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3464 struct btrfs_root *extent_root,
3465 struct map_lookup *map, u64 chunk_offset,
3466 u64 chunk_size, u64 stripe_size)
3467{
3468 u64 dev_offset;
3469 struct btrfs_key key;
3470 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3471 struct btrfs_device *device;
3472 struct btrfs_chunk *chunk;
3473 struct btrfs_stripe *stripe;
3474 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3475 int index = 0;
3476 int ret;
3477
3478 chunk = kzalloc(item_size, GFP_NOFS);
3479 if (!chunk)
3480 return -ENOMEM;
3481
3482 index = 0;
3483 while (index < map->num_stripes) {
3484 device = map->stripes[index].dev;
3485 device->bytes_used += stripe_size;
3486 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003487 if (ret)
3488 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003489 index++;
3490 }
3491
Josef Bacik2bf64752011-09-26 17:12:22 -04003492 spin_lock(&extent_root->fs_info->free_chunk_lock);
3493 extent_root->fs_info->free_chunk_space -= (stripe_size *
3494 map->num_stripes);
3495 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3496
Yan Zheng2b820322008-11-17 21:11:30 -05003497 index = 0;
3498 stripe = &chunk->stripe;
3499 while (index < map->num_stripes) {
3500 device = map->stripes[index].dev;
3501 dev_offset = map->stripes[index].physical;
3502
3503 btrfs_set_stack_stripe_devid(stripe, device->devid);
3504 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3505 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3506 stripe++;
3507 index++;
3508 }
3509
3510 btrfs_set_stack_chunk_length(chunk, chunk_size);
3511 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3512 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3513 btrfs_set_stack_chunk_type(chunk, map->type);
3514 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3515 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3516 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3517 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3518 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3519
3520 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3521 key.type = BTRFS_CHUNK_ITEM_KEY;
3522 key.offset = chunk_offset;
3523
3524 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003525
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003526 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3527 /*
3528 * TODO: Cleanup of inserted chunk root in case of
3529 * failure.
3530 */
Li Zefan125ccb02011-12-08 15:07:24 +08003531 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003532 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003533 }
liubo1abe9b82011-03-24 11:18:59 +00003534
Mark Fasheh3acd3952011-09-08 17:40:01 -07003535out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003536 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003537 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003538}
3539
3540/*
3541 * Chunk allocation falls into two parts. The first part does works
3542 * that make the new allocated chunk useable, but not do any operation
3543 * that modifies the chunk tree. The second part does the works that
3544 * require modifying the chunk tree. This division is important for the
3545 * bootstrap process of adding storage to a seed btrfs.
3546 */
3547int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3548 struct btrfs_root *extent_root, u64 type)
3549{
3550 u64 chunk_offset;
3551 u64 chunk_size;
3552 u64 stripe_size;
3553 struct map_lookup *map;
3554 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3555 int ret;
3556
3557 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3558 &chunk_offset);
3559 if (ret)
3560 return ret;
3561
3562 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3563 &stripe_size, chunk_offset, type);
3564 if (ret)
3565 return ret;
3566
3567 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3568 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003569 if (ret)
3570 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003571 return 0;
3572}
3573
Chris Masond3977122009-01-05 21:25:51 -05003574static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003575 struct btrfs_root *root,
3576 struct btrfs_device *device)
3577{
3578 u64 chunk_offset;
3579 u64 sys_chunk_offset;
3580 u64 chunk_size;
3581 u64 sys_chunk_size;
3582 u64 stripe_size;
3583 u64 sys_stripe_size;
3584 u64 alloc_profile;
3585 struct map_lookup *map;
3586 struct map_lookup *sys_map;
3587 struct btrfs_fs_info *fs_info = root->fs_info;
3588 struct btrfs_root *extent_root = fs_info->extent_root;
3589 int ret;
3590
3591 ret = find_next_chunk(fs_info->chunk_root,
3592 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003593 if (ret)
3594 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003595
3596 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003597 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003598 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3599
3600 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3601 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003602 if (ret)
3603 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003604
3605 sys_chunk_offset = chunk_offset + chunk_size;
3606
3607 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003608 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003609 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3610
3611 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3612 &sys_chunk_size, &sys_stripe_size,
3613 sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003614 if (ret)
3615 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003616
3617 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003618 if (ret)
3619 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003620
3621 /*
3622 * Modifying chunk tree needs allocating new blocks from both
3623 * system block group and metadata block group. So we only can
3624 * do operations require modifying the chunk tree after both
3625 * block groups were created.
3626 */
3627 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3628 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003629 if (ret)
3630 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003631
3632 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3633 sys_chunk_offset, sys_chunk_size,
3634 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003635 if (ret)
3636 goto abort;
3637
Yan Zheng2b820322008-11-17 21:11:30 -05003638 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003639
3640abort:
3641 btrfs_abort_transaction(trans, root, ret);
3642 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003643}
3644
3645int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3646{
3647 struct extent_map *em;
3648 struct map_lookup *map;
3649 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3650 int readonly = 0;
3651 int i;
3652
Chris Mason890871b2009-09-02 16:24:52 -04003653 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003654 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003655 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003656 if (!em)
3657 return 1;
3658
Josef Bacikf48b9072010-01-27 02:07:59 +00003659 if (btrfs_test_opt(root, DEGRADED)) {
3660 free_extent_map(em);
3661 return 0;
3662 }
3663
Yan Zheng2b820322008-11-17 21:11:30 -05003664 map = (struct map_lookup *)em->bdev;
3665 for (i = 0; i < map->num_stripes; i++) {
3666 if (!map->stripes[i].dev->writeable) {
3667 readonly = 1;
3668 break;
3669 }
3670 }
3671 free_extent_map(em);
3672 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003673}
3674
3675void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3676{
David Sterbaa8067e02011-04-21 00:34:43 +02003677 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003678}
3679
3680void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3681{
3682 struct extent_map *em;
3683
Chris Masond3977122009-01-05 21:25:51 -05003684 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003685 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003686 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3687 if (em)
3688 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003689 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003690 if (!em)
3691 break;
3692 kfree(em->bdev);
3693 /* once for us */
3694 free_extent_map(em);
3695 /* once for the tree */
3696 free_extent_map(em);
3697 }
3698}
3699
Chris Masonf1885912008-04-09 16:28:12 -04003700int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3701{
3702 struct extent_map *em;
3703 struct map_lookup *map;
3704 struct extent_map_tree *em_tree = &map_tree->map_tree;
3705 int ret;
3706
Chris Mason890871b2009-09-02 16:24:52 -04003707 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003708 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003709 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003710 BUG_ON(!em);
3711
3712 BUG_ON(em->start > logical || em->start + em->len < logical);
3713 map = (struct map_lookup *)em->bdev;
3714 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3715 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003716 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3717 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003718 else
3719 ret = 1;
3720 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003721 return ret;
3722}
3723
Chris Masondfe25022008-05-13 13:46:40 -04003724static int find_live_mirror(struct map_lookup *map, int first, int num,
3725 int optimal)
3726{
3727 int i;
3728 if (map->stripes[optimal].dev->bdev)
3729 return optimal;
3730 for (i = first; i < first + num; i++) {
3731 if (map->stripes[i].dev->bdev)
3732 return i;
3733 }
3734 /* we couldn't find one that doesn't fail. Just return something
3735 * and the io error handling code will clean up eventually
3736 */
3737 return optimal;
3738}
3739
Chris Masonf2d8d742008-04-21 10:03:05 -04003740static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3741 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003742 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003743 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003744{
3745 struct extent_map *em;
3746 struct map_lookup *map;
3747 struct extent_map_tree *em_tree = &map_tree->map_tree;
3748 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003749 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003750 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003751 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003752 u64 stripe_nr_orig;
3753 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003754 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003755 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003756 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003757 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003758 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003759 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003760
Chris Mason890871b2009-09-02 16:24:52 -04003761 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003762 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003763 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003764
Chris Mason3b951512008-04-17 11:29:12 -04003765 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003766 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3767 (unsigned long long)logical,
3768 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003769 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003770 }
Chris Mason0b86a832008-03-24 15:01:56 -04003771
3772 BUG_ON(em->start > logical || em->start + em->len < logical);
3773 map = (struct map_lookup *)em->bdev;
3774 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003775
Chris Masonf1885912008-04-09 16:28:12 -04003776 if (mirror_num > map->num_stripes)
3777 mirror_num = 0;
3778
Chris Mason593060d2008-03-25 16:50:33 -04003779 stripe_nr = offset;
3780 /*
3781 * stripe_nr counts the total number of stripes we have to stride
3782 * to get to this block
3783 */
3784 do_div(stripe_nr, map->stripe_len);
3785
3786 stripe_offset = stripe_nr * map->stripe_len;
3787 BUG_ON(offset < stripe_offset);
3788
3789 /* stripe_offset is the offset of this block in its stripe*/
3790 stripe_offset = offset - stripe_offset;
3791
Li Dongyangfce3bb92011-03-24 10:24:26 +00003792 if (rw & REQ_DISCARD)
3793 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003794 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003795 /* we limit the length of each bio to what fits in a stripe */
3796 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003797 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003798 } else {
3799 *length = em->len - offset;
3800 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003801
Jan Schmidta1d3c472011-08-04 17:15:33 +02003802 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003803 goto out;
3804
Chris Masonf2d8d742008-04-21 10:03:05 -04003805 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003806 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003807 stripe_nr_orig = stripe_nr;
3808 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3809 (~(map->stripe_len - 1));
3810 do_div(stripe_nr_end, map->stripe_len);
3811 stripe_end_offset = stripe_nr_end * map->stripe_len -
3812 (offset + *length);
3813 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3814 if (rw & REQ_DISCARD)
3815 num_stripes = min_t(u64, map->num_stripes,
3816 stripe_nr_end - stripe_nr_orig);
3817 stripe_index = do_div(stripe_nr, map->num_stripes);
3818 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003819 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003820 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003821 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003822 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003823 else {
3824 stripe_index = find_live_mirror(map, 0,
3825 map->num_stripes,
3826 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003827 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003828 }
Chris Mason2fff7342008-04-29 14:12:09 -04003829
Chris Mason611f0e02008-04-03 16:29:03 -04003830 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003831 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003832 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003833 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003834 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003835 } else {
3836 mirror_num = 1;
3837 }
Chris Mason2fff7342008-04-29 14:12:09 -04003838
Chris Mason321aecc2008-04-16 10:49:51 -04003839 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3840 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003841
3842 stripe_index = do_div(stripe_nr, factor);
3843 stripe_index *= map->sub_stripes;
3844
Jens Axboe7eaceac2011-03-10 08:52:07 +01003845 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003846 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003847 else if (rw & REQ_DISCARD)
3848 num_stripes = min_t(u64, map->sub_stripes *
3849 (stripe_nr_end - stripe_nr_orig),
3850 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003851 else if (mirror_num)
3852 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003853 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04003854 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04003855 stripe_index = find_live_mirror(map, stripe_index,
3856 map->sub_stripes, stripe_index +
3857 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04003858 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003859 }
Chris Mason8790d502008-04-03 16:29:03 -04003860 } else {
3861 /*
3862 * after this do_div call, stripe_nr is the number of stripes
3863 * on this device we have to walk to find the data, and
3864 * stripe_index is the number of our device in the stripe array
3865 */
3866 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003867 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003868 }
Chris Mason593060d2008-03-25 16:50:33 -04003869 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003870
Li Zefande11cc12011-12-01 12:55:47 +08003871 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3872 if (!bbio) {
3873 ret = -ENOMEM;
3874 goto out;
3875 }
3876 atomic_set(&bbio->error, 0);
3877
Li Dongyangfce3bb92011-03-24 10:24:26 +00003878 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003879 int factor = 0;
3880 int sub_stripes = 0;
3881 u64 stripes_per_dev = 0;
3882 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003883 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003884
3885 if (map->type &
3886 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3887 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3888 sub_stripes = 1;
3889 else
3890 sub_stripes = map->sub_stripes;
3891
3892 factor = map->num_stripes / sub_stripes;
3893 stripes_per_dev = div_u64_rem(stripe_nr_end -
3894 stripe_nr_orig,
3895 factor,
3896 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04003897 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3898 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003899 }
3900
Li Dongyangfce3bb92011-03-24 10:24:26 +00003901 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003902 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003903 map->stripes[stripe_index].physical +
3904 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003905 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003906
Li Zefanec9ef7a2011-12-01 14:06:42 +08003907 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3908 BTRFS_BLOCK_GROUP_RAID10)) {
3909 bbio->stripes[i].length = stripes_per_dev *
3910 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003911
Li Zefanec9ef7a2011-12-01 14:06:42 +08003912 if (i / sub_stripes < remaining_stripes)
3913 bbio->stripes[i].length +=
3914 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003915
3916 /*
3917 * Special for the first stripe and
3918 * the last stripe:
3919 *
3920 * |-------|...|-------|
3921 * |----------|
3922 * off end_off
3923 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08003924 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003925 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003926 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003927
3928 if (stripe_index >= last_stripe &&
3929 stripe_index <= (last_stripe +
3930 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08003931 bbio->stripes[i].length -=
3932 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003933
Li Zefanec9ef7a2011-12-01 14:06:42 +08003934 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003935 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003936 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003937 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003938
3939 stripe_index++;
3940 if (stripe_index == map->num_stripes) {
3941 /* This could only happen for RAID0/10 */
3942 stripe_index = 0;
3943 stripe_nr++;
3944 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003945 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003946 } else {
3947 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003948 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003949 map->stripes[stripe_index].physical +
3950 stripe_offset +
3951 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003952 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003953 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003954 stripe_index++;
3955 }
Chris Mason593060d2008-03-25 16:50:33 -04003956 }
Li Zefande11cc12011-12-01 12:55:47 +08003957
3958 if (rw & REQ_WRITE) {
3959 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3960 BTRFS_BLOCK_GROUP_RAID10 |
3961 BTRFS_BLOCK_GROUP_DUP)) {
3962 max_errors = 1;
3963 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003964 }
Li Zefande11cc12011-12-01 12:55:47 +08003965
3966 *bbio_ret = bbio;
3967 bbio->num_stripes = num_stripes;
3968 bbio->max_errors = max_errors;
3969 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003970out:
Chris Mason0b86a832008-03-24 15:01:56 -04003971 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003972 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003973}
3974
Chris Masonf2d8d742008-04-21 10:03:05 -04003975int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3976 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003977 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003978{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003979 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003980 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003981}
3982
Yan Zhenga512bbf2008-12-08 16:46:26 -05003983int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3984 u64 chunk_start, u64 physical, u64 devid,
3985 u64 **logical, int *naddrs, int *stripe_len)
3986{
3987 struct extent_map_tree *em_tree = &map_tree->map_tree;
3988 struct extent_map *em;
3989 struct map_lookup *map;
3990 u64 *buf;
3991 u64 bytenr;
3992 u64 length;
3993 u64 stripe_nr;
3994 int i, j, nr = 0;
3995
Chris Mason890871b2009-09-02 16:24:52 -04003996 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003997 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003998 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003999
4000 BUG_ON(!em || em->start != chunk_start);
4001 map = (struct map_lookup *)em->bdev;
4002
4003 length = em->len;
4004 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4005 do_div(length, map->num_stripes / map->sub_stripes);
4006 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4007 do_div(length, map->num_stripes);
4008
4009 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004010 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004011
4012 for (i = 0; i < map->num_stripes; i++) {
4013 if (devid && map->stripes[i].dev->devid != devid)
4014 continue;
4015 if (map->stripes[i].physical > physical ||
4016 map->stripes[i].physical + length <= physical)
4017 continue;
4018
4019 stripe_nr = physical - map->stripes[i].physical;
4020 do_div(stripe_nr, map->stripe_len);
4021
4022 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4023 stripe_nr = stripe_nr * map->num_stripes + i;
4024 do_div(stripe_nr, map->sub_stripes);
4025 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4026 stripe_nr = stripe_nr * map->num_stripes + i;
4027 }
4028 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05004029 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004030 for (j = 0; j < nr; j++) {
4031 if (buf[j] == bytenr)
4032 break;
4033 }
Chris Mason934d3752008-12-08 16:43:10 -05004034 if (j == nr) {
4035 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004036 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004037 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004038 }
4039
Yan Zhenga512bbf2008-12-08 16:46:26 -05004040 *logical = buf;
4041 *naddrs = nr;
4042 *stripe_len = map->stripe_len;
4043
4044 free_extent_map(em);
4045 return 0;
4046}
4047
Stefan Behrens442a4f62012-05-25 16:06:08 +02004048static void *merge_stripe_index_into_bio_private(void *bi_private,
4049 unsigned int stripe_index)
4050{
4051 /*
4052 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4053 * at most 1.
4054 * The alternative solution (instead of stealing bits from the
4055 * pointer) would be to allocate an intermediate structure
4056 * that contains the old private pointer plus the stripe_index.
4057 */
4058 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4059 BUG_ON(stripe_index > 3);
4060 return (void *)(((uintptr_t)bi_private) | stripe_index);
4061}
4062
4063static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4064{
4065 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4066}
4067
4068static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4069{
4070 return (unsigned int)((uintptr_t)bi_private) & 3;
4071}
4072
Jan Schmidta1d3c472011-08-04 17:15:33 +02004073static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004074{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004075 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004076 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004077
Stefan Behrens442a4f62012-05-25 16:06:08 +02004078 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004079 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004080 if (err == -EIO || err == -EREMOTEIO) {
4081 unsigned int stripe_index =
4082 extract_stripe_index_from_bio_private(
4083 bio->bi_private);
4084 struct btrfs_device *dev;
4085
4086 BUG_ON(stripe_index >= bbio->num_stripes);
4087 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004088 if (dev->bdev) {
4089 if (bio->bi_rw & WRITE)
4090 btrfs_dev_stat_inc(dev,
4091 BTRFS_DEV_STAT_WRITE_ERRS);
4092 else
4093 btrfs_dev_stat_inc(dev,
4094 BTRFS_DEV_STAT_READ_ERRS);
4095 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4096 btrfs_dev_stat_inc(dev,
4097 BTRFS_DEV_STAT_FLUSH_ERRS);
4098 btrfs_dev_stat_print_on_error(dev);
4099 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004100 }
4101 }
Chris Mason8790d502008-04-03 16:29:03 -04004102
Jan Schmidta1d3c472011-08-04 17:15:33 +02004103 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004104 is_orig_bio = 1;
4105
Jan Schmidta1d3c472011-08-04 17:15:33 +02004106 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004107 if (!is_orig_bio) {
4108 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004109 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004110 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004111 bio->bi_private = bbio->private;
4112 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004113 bio->bi_bdev = (struct block_device *)
4114 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004115 /* only send an error to the higher layers if it is
4116 * beyond the tolerance of the multi-bio
4117 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004118 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004119 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004120 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004121 /*
4122 * this bio is actually up to date, we didn't
4123 * go over the max number of errors
4124 */
4125 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004126 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004127 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004128 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004129
4130 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004131 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004132 bio_put(bio);
4133 }
Chris Mason8790d502008-04-03 16:29:03 -04004134}
4135
Chris Mason8b712842008-06-11 16:50:36 -04004136struct async_sched {
4137 struct bio *bio;
4138 int rw;
4139 struct btrfs_fs_info *info;
4140 struct btrfs_work work;
4141};
4142
4143/*
4144 * see run_scheduled_bios for a description of why bios are collected for
4145 * async submit.
4146 *
4147 * This will add one bio to the pending list for a device and make sure
4148 * the work struct is scheduled.
4149 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004150static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004151 struct btrfs_device *device,
4152 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004153{
4154 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004155 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004156
4157 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004158 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004159 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004160 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004161 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004162 return;
Chris Mason8b712842008-06-11 16:50:36 -04004163 }
4164
4165 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004166 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004167 * higher layers. Otherwise, the async bio makes it appear we have
4168 * made progress against dirty pages when we've really just put it
4169 * on a queue for later
4170 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004171 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004172 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004173 bio->bi_next = NULL;
4174 bio->bi_rw |= rw;
4175
4176 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004177 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004178 pending_bios = &device->pending_sync_bios;
4179 else
4180 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004181
Chris Masonffbd5172009-04-20 15:50:09 -04004182 if (pending_bios->tail)
4183 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004184
Chris Masonffbd5172009-04-20 15:50:09 -04004185 pending_bios->tail = bio;
4186 if (!pending_bios->head)
4187 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004188 if (device->running_pending)
4189 should_queue = 0;
4190
4191 spin_unlock(&device->io_lock);
4192
4193 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004194 btrfs_queue_worker(&root->fs_info->submit_workers,
4195 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004196}
4197
Chris Masonf1885912008-04-09 16:28:12 -04004198int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004199 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004200{
4201 struct btrfs_mapping_tree *map_tree;
4202 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004203 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004204 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004205 u64 length = 0;
4206 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004207 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004208 int dev_nr = 0;
4209 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004210 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004211
Chris Masonf2d8d742008-04-21 10:03:05 -04004212 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004213 map_tree = &root->fs_info->mapping_tree;
4214 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004215
Jan Schmidta1d3c472011-08-04 17:15:33 +02004216 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004217 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004218 if (ret) /* -ENOMEM */
4219 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004220
Jan Schmidta1d3c472011-08-04 17:15:33 +02004221 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004222 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004223 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4224 "len %llu\n", (unsigned long long)logical,
4225 (unsigned long long)length,
4226 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004227 BUG();
4228 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004229
4230 bbio->orig_bio = first_bio;
4231 bbio->private = first_bio->bi_private;
4232 bbio->end_io = first_bio->bi_end_io;
4233 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004234
Chris Masond3977122009-01-05 21:25:51 -05004235 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004236 if (dev_nr < total_devs - 1) {
4237 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004238 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004239 } else {
4240 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004241 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004242 bio->bi_private = bbio;
Stefan Behrens442a4f62012-05-25 16:06:08 +02004243 bio->bi_private = merge_stripe_index_into_bio_private(
4244 bio->bi_private, (unsigned int)dev_nr);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004245 bio->bi_end_io = btrfs_end_bio;
4246 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4247 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004248 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Josef Bacik606686e2012-06-04 14:03:51 -04004249#ifdef DEBUG
4250 struct rcu_string *name;
4251
4252 rcu_read_lock();
4253 name = rcu_dereference(dev->name);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004254 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4255 "(%s id %llu), size=%u\n", rw,
4256 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
Josef Bacik606686e2012-06-04 14:03:51 -04004257 name->str, dev->devid, bio->bi_size);
4258 rcu_read_unlock();
4259#endif
Chris Masondfe25022008-05-13 13:46:40 -04004260 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004261 if (async_submit)
4262 schedule_bio(root, dev, rw, bio);
4263 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004264 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004265 } else {
4266 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4267 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004268 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004269 }
Chris Mason8790d502008-04-03 16:29:03 -04004270 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004271 }
Chris Mason0b86a832008-03-24 15:01:56 -04004272 return 0;
4273}
4274
Chris Masona4437552008-04-18 10:29:38 -04004275struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004276 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004277{
Yan Zheng2b820322008-11-17 21:11:30 -05004278 struct btrfs_device *device;
4279 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004280
Yan Zheng2b820322008-11-17 21:11:30 -05004281 cur_devices = root->fs_info->fs_devices;
4282 while (cur_devices) {
4283 if (!fsid ||
4284 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4285 device = __find_device(&cur_devices->devices,
4286 devid, uuid);
4287 if (device)
4288 return device;
4289 }
4290 cur_devices = cur_devices->seed;
4291 }
4292 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004293}
4294
Chris Masondfe25022008-05-13 13:46:40 -04004295static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4296 u64 devid, u8 *dev_uuid)
4297{
4298 struct btrfs_device *device;
4299 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4300
4301 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004302 if (!device)
4303 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004304 list_add(&device->dev_list,
4305 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004306 device->dev_root = root->fs_info->dev_root;
4307 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004308 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004309 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004310 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004311 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004312 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004313 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004314 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004315 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4316 return device;
4317}
4318
Chris Mason0b86a832008-03-24 15:01:56 -04004319static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4320 struct extent_buffer *leaf,
4321 struct btrfs_chunk *chunk)
4322{
4323 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4324 struct map_lookup *map;
4325 struct extent_map *em;
4326 u64 logical;
4327 u64 length;
4328 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004329 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004330 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004331 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004332 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004333
Chris Masone17cade2008-04-15 15:41:47 -04004334 logical = key->offset;
4335 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004336
Chris Mason890871b2009-09-02 16:24:52 -04004337 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004338 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004339 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004340
4341 /* already mapped? */
4342 if (em && em->start <= logical && em->start + em->len > logical) {
4343 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004344 return 0;
4345 } else if (em) {
4346 free_extent_map(em);
4347 }
Chris Mason0b86a832008-03-24 15:01:56 -04004348
David Sterba172ddd62011-04-21 00:48:27 +02004349 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004350 if (!em)
4351 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004352 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4353 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004354 if (!map) {
4355 free_extent_map(em);
4356 return -ENOMEM;
4357 }
4358
4359 em->bdev = (struct block_device *)map;
4360 em->start = logical;
4361 em->len = length;
4362 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004363 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004364
Chris Mason593060d2008-03-25 16:50:33 -04004365 map->num_stripes = num_stripes;
4366 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4367 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4368 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4369 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4370 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004371 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004372 for (i = 0; i < num_stripes; i++) {
4373 map->stripes[i].physical =
4374 btrfs_stripe_offset_nr(leaf, chunk, i);
4375 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004376 read_extent_buffer(leaf, uuid, (unsigned long)
4377 btrfs_stripe_dev_uuid_nr(chunk, i),
4378 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004379 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4380 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004381 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004382 kfree(map);
4383 free_extent_map(em);
4384 return -EIO;
4385 }
Chris Masondfe25022008-05-13 13:46:40 -04004386 if (!map->stripes[i].dev) {
4387 map->stripes[i].dev =
4388 add_missing_dev(root, devid, uuid);
4389 if (!map->stripes[i].dev) {
4390 kfree(map);
4391 free_extent_map(em);
4392 return -EIO;
4393 }
4394 }
4395 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004396 }
4397
Chris Mason890871b2009-09-02 16:24:52 -04004398 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004399 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004400 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004401 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004402 free_extent_map(em);
4403
4404 return 0;
4405}
4406
Jeff Mahoney143bede2012-03-01 14:56:26 +01004407static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004408 struct btrfs_dev_item *dev_item,
4409 struct btrfs_device *device)
4410{
4411 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004412
4413 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004414 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4415 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004416 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4417 device->type = btrfs_device_type(leaf, dev_item);
4418 device->io_align = btrfs_device_io_align(leaf, dev_item);
4419 device->io_width = btrfs_device_io_width(leaf, dev_item);
4420 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004421
4422 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004423 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004424}
4425
Yan Zheng2b820322008-11-17 21:11:30 -05004426static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4427{
4428 struct btrfs_fs_devices *fs_devices;
4429 int ret;
4430
Li Zefanb367e472011-12-07 11:38:24 +08004431 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004432
4433 fs_devices = root->fs_info->fs_devices->seed;
4434 while (fs_devices) {
4435 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4436 ret = 0;
4437 goto out;
4438 }
4439 fs_devices = fs_devices->seed;
4440 }
4441
4442 fs_devices = find_fsid(fsid);
4443 if (!fs_devices) {
4444 ret = -ENOENT;
4445 goto out;
4446 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004447
4448 fs_devices = clone_fs_devices(fs_devices);
4449 if (IS_ERR(fs_devices)) {
4450 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004451 goto out;
4452 }
4453
Christoph Hellwig97288f22008-12-02 06:36:09 -05004454 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004455 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004456 if (ret) {
4457 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004458 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004459 }
Yan Zheng2b820322008-11-17 21:11:30 -05004460
4461 if (!fs_devices->seeding) {
4462 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004463 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004464 ret = -EINVAL;
4465 goto out;
4466 }
4467
4468 fs_devices->seed = root->fs_info->fs_devices->seed;
4469 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004470out:
Yan Zheng2b820322008-11-17 21:11:30 -05004471 return ret;
4472}
4473
Chris Mason0d81ba52008-03-24 15:02:07 -04004474static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004475 struct extent_buffer *leaf,
4476 struct btrfs_dev_item *dev_item)
4477{
4478 struct btrfs_device *device;
4479 u64 devid;
4480 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004481 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004482 u8 dev_uuid[BTRFS_UUID_SIZE];
4483
Chris Mason0b86a832008-03-24 15:01:56 -04004484 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004485 read_extent_buffer(leaf, dev_uuid,
4486 (unsigned long)btrfs_device_uuid(dev_item),
4487 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004488 read_extent_buffer(leaf, fs_uuid,
4489 (unsigned long)btrfs_device_fsid(dev_item),
4490 BTRFS_UUID_SIZE);
4491
4492 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4493 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004494 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004495 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004496 }
4497
4498 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4499 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004500 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004501 return -EIO;
4502
4503 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004504 printk(KERN_WARNING "warning devid %llu missing\n",
4505 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004506 device = add_missing_dev(root, devid, dev_uuid);
4507 if (!device)
4508 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004509 } else if (!device->missing) {
4510 /*
4511 * this happens when a device that was properly setup
4512 * in the device info lists suddenly goes bad.
4513 * device->bdev is NULL, and so we have to set
4514 * device->missing to one here
4515 */
4516 root->fs_info->fs_devices->missing_devices++;
4517 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004518 }
4519 }
4520
4521 if (device->fs_devices != root->fs_info->fs_devices) {
4522 BUG_ON(device->writeable);
4523 if (device->generation !=
4524 btrfs_device_generation(leaf, dev_item))
4525 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004526 }
Chris Mason0b86a832008-03-24 15:01:56 -04004527
4528 fill_device_from_item(leaf, dev_item, device);
4529 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004530 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004531 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004532 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004533 spin_lock(&root->fs_info->free_chunk_lock);
4534 root->fs_info->free_chunk_space += device->total_bytes -
4535 device->bytes_used;
4536 spin_unlock(&root->fs_info->free_chunk_lock);
4537 }
Chris Mason0b86a832008-03-24 15:01:56 -04004538 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004539 return ret;
4540}
4541
Yan Zhenge4404d62008-12-12 10:03:26 -05004542int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004543{
David Sterba6c417612011-04-13 15:41:04 +02004544 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004545 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004546 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004547 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004548 u8 *ptr;
4549 unsigned long sb_ptr;
4550 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004551 u32 num_stripes;
4552 u32 array_size;
4553 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004554 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004555 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004556
Yan Zhenge4404d62008-12-12 10:03:26 -05004557 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004558 BTRFS_SUPER_INFO_SIZE);
4559 if (!sb)
4560 return -ENOMEM;
4561 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004562 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004563 /*
4564 * The sb extent buffer is artifical and just used to read the system array.
4565 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4566 * pages up-to-date when the page is larger: extent does not cover the
4567 * whole page and consequently check_page_uptodate does not find all
4568 * the page's extents up-to-date (the hole beyond sb),
4569 * write_extent_buffer then triggers a WARN_ON.
4570 *
4571 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4572 * but sb spans only this function. Add an explicit SetPageUptodate call
4573 * to silence the warning eg. on PowerPC 64.
4574 */
4575 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004576 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004577
Chris Masona061fc82008-05-07 11:43:44 -04004578 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004579 array_size = btrfs_super_sys_array_size(super_copy);
4580
Chris Mason0b86a832008-03-24 15:01:56 -04004581 ptr = super_copy->sys_chunk_array;
4582 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4583 cur = 0;
4584
4585 while (cur < array_size) {
4586 disk_key = (struct btrfs_disk_key *)ptr;
4587 btrfs_disk_key_to_cpu(&key, disk_key);
4588
Chris Masona061fc82008-05-07 11:43:44 -04004589 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004590 sb_ptr += len;
4591 cur += len;
4592
Chris Mason0d81ba52008-03-24 15:02:07 -04004593 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004594 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004595 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004596 if (ret)
4597 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004598 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4599 len = btrfs_chunk_item_size(num_stripes);
4600 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004601 ret = -EIO;
4602 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004603 }
4604 ptr += len;
4605 sb_ptr += len;
4606 cur += len;
4607 }
Chris Masona061fc82008-05-07 11:43:44 -04004608 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004609 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004610}
4611
Stefan Behrens442a4f62012-05-25 16:06:08 +02004612struct btrfs_device *btrfs_find_device_for_logical(struct btrfs_root *root,
4613 u64 logical, int mirror_num)
4614{
4615 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4616 int ret;
4617 u64 map_length = 0;
4618 struct btrfs_bio *bbio = NULL;
4619 struct btrfs_device *device;
4620
4621 BUG_ON(mirror_num == 0);
4622 ret = btrfs_map_block(map_tree, WRITE, logical, &map_length, &bbio,
4623 mirror_num);
4624 if (ret) {
4625 BUG_ON(bbio != NULL);
4626 return NULL;
4627 }
4628 BUG_ON(mirror_num != bbio->mirror_num);
4629 device = bbio->stripes[mirror_num - 1].dev;
4630 kfree(bbio);
4631 return device;
4632}
4633
Chris Mason0b86a832008-03-24 15:01:56 -04004634int btrfs_read_chunk_tree(struct btrfs_root *root)
4635{
4636 struct btrfs_path *path;
4637 struct extent_buffer *leaf;
4638 struct btrfs_key key;
4639 struct btrfs_key found_key;
4640 int ret;
4641 int slot;
4642
4643 root = root->fs_info->chunk_root;
4644
4645 path = btrfs_alloc_path();
4646 if (!path)
4647 return -ENOMEM;
4648
Li Zefanb367e472011-12-07 11:38:24 +08004649 mutex_lock(&uuid_mutex);
4650 lock_chunks(root);
4651
Chris Mason0b86a832008-03-24 15:01:56 -04004652 /* first we search for all of the device items, and then we
4653 * read in all of the chunk items. This way we can create chunk
4654 * mappings that reference all of the devices that are afound
4655 */
4656 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4657 key.offset = 0;
4658 key.type = 0;
4659again:
4660 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004661 if (ret < 0)
4662 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004663 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004664 leaf = path->nodes[0];
4665 slot = path->slots[0];
4666 if (slot >= btrfs_header_nritems(leaf)) {
4667 ret = btrfs_next_leaf(root, path);
4668 if (ret == 0)
4669 continue;
4670 if (ret < 0)
4671 goto error;
4672 break;
4673 }
4674 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4675 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4676 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4677 break;
4678 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4679 struct btrfs_dev_item *dev_item;
4680 dev_item = btrfs_item_ptr(leaf, slot,
4681 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004682 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004683 if (ret)
4684 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004685 }
4686 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4687 struct btrfs_chunk *chunk;
4688 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4689 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004690 if (ret)
4691 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004692 }
4693 path->slots[0]++;
4694 }
4695 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4696 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004697 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004698 goto again;
4699 }
Chris Mason0b86a832008-03-24 15:01:56 -04004700 ret = 0;
4701error:
Li Zefanb367e472011-12-07 11:38:24 +08004702 unlock_chunks(root);
4703 mutex_unlock(&uuid_mutex);
4704
Yan Zheng2b820322008-11-17 21:11:30 -05004705 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004706 return ret;
4707}
Stefan Behrens442a4f62012-05-25 16:06:08 +02004708
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004709static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
4710{
4711 int i;
4712
4713 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4714 btrfs_dev_stat_reset(dev, i);
4715}
4716
4717int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
4718{
4719 struct btrfs_key key;
4720 struct btrfs_key found_key;
4721 struct btrfs_root *dev_root = fs_info->dev_root;
4722 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4723 struct extent_buffer *eb;
4724 int slot;
4725 int ret = 0;
4726 struct btrfs_device *device;
4727 struct btrfs_path *path = NULL;
4728 int i;
4729
4730 path = btrfs_alloc_path();
4731 if (!path) {
4732 ret = -ENOMEM;
4733 goto out;
4734 }
4735
4736 mutex_lock(&fs_devices->device_list_mutex);
4737 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4738 int item_size;
4739 struct btrfs_dev_stats_item *ptr;
4740
4741 key.objectid = 0;
4742 key.type = BTRFS_DEV_STATS_KEY;
4743 key.offset = device->devid;
4744 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
4745 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004746 __btrfs_reset_dev_stats(device);
4747 device->dev_stats_valid = 1;
4748 btrfs_release_path(path);
4749 continue;
4750 }
4751 slot = path->slots[0];
4752 eb = path->nodes[0];
4753 btrfs_item_key_to_cpu(eb, &found_key, slot);
4754 item_size = btrfs_item_size_nr(eb, slot);
4755
4756 ptr = btrfs_item_ptr(eb, slot,
4757 struct btrfs_dev_stats_item);
4758
4759 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4760 if (item_size >= (1 + i) * sizeof(__le64))
4761 btrfs_dev_stat_set(device, i,
4762 btrfs_dev_stats_value(eb, ptr, i));
4763 else
4764 btrfs_dev_stat_reset(device, i);
4765 }
4766
4767 device->dev_stats_valid = 1;
4768 btrfs_dev_stat_print_on_load(device);
4769 btrfs_release_path(path);
4770 }
4771 mutex_unlock(&fs_devices->device_list_mutex);
4772
4773out:
4774 btrfs_free_path(path);
4775 return ret < 0 ? ret : 0;
4776}
4777
4778static int update_dev_stat_item(struct btrfs_trans_handle *trans,
4779 struct btrfs_root *dev_root,
4780 struct btrfs_device *device)
4781{
4782 struct btrfs_path *path;
4783 struct btrfs_key key;
4784 struct extent_buffer *eb;
4785 struct btrfs_dev_stats_item *ptr;
4786 int ret;
4787 int i;
4788
4789 key.objectid = 0;
4790 key.type = BTRFS_DEV_STATS_KEY;
4791 key.offset = device->devid;
4792
4793 path = btrfs_alloc_path();
4794 BUG_ON(!path);
4795 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
4796 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004797 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
4798 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004799 goto out;
4800 }
4801
4802 if (ret == 0 &&
4803 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
4804 /* need to delete old one and insert a new one */
4805 ret = btrfs_del_item(trans, dev_root, path);
4806 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004807 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
4808 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004809 goto out;
4810 }
4811 ret = 1;
4812 }
4813
4814 if (ret == 1) {
4815 /* need to insert a new item */
4816 btrfs_release_path(path);
4817 ret = btrfs_insert_empty_item(trans, dev_root, path,
4818 &key, sizeof(*ptr));
4819 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004820 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
4821 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004822 goto out;
4823 }
4824 }
4825
4826 eb = path->nodes[0];
4827 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
4828 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4829 btrfs_set_dev_stats_value(eb, ptr, i,
4830 btrfs_dev_stat_read(device, i));
4831 btrfs_mark_buffer_dirty(eb);
4832
4833out:
4834 btrfs_free_path(path);
4835 return ret;
4836}
4837
4838/*
4839 * called from commit_transaction. Writes all changed device stats to disk.
4840 */
4841int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
4842 struct btrfs_fs_info *fs_info)
4843{
4844 struct btrfs_root *dev_root = fs_info->dev_root;
4845 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4846 struct btrfs_device *device;
4847 int ret = 0;
4848
4849 mutex_lock(&fs_devices->device_list_mutex);
4850 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4851 if (!device->dev_stats_valid || !device->dev_stats_dirty)
4852 continue;
4853
4854 ret = update_dev_stat_item(trans, dev_root, device);
4855 if (!ret)
4856 device->dev_stats_dirty = 0;
4857 }
4858 mutex_unlock(&fs_devices->device_list_mutex);
4859
4860 return ret;
4861}
4862
Stefan Behrens442a4f62012-05-25 16:06:08 +02004863void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
4864{
4865 btrfs_dev_stat_inc(dev, index);
4866 btrfs_dev_stat_print_on_error(dev);
4867}
4868
4869void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
4870{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004871 if (!dev->dev_stats_valid)
4872 return;
Josef Bacik606686e2012-06-04 14:03:51 -04004873 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02004874 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04004875 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02004876 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4877 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4878 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4879 btrfs_dev_stat_read(dev,
4880 BTRFS_DEV_STAT_CORRUPTION_ERRS),
4881 btrfs_dev_stat_read(dev,
4882 BTRFS_DEV_STAT_GENERATION_ERRS));
4883}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004884
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004885static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
4886{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06004887 int i;
4888
4889 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4890 if (btrfs_dev_stat_read(dev, i) != 0)
4891 break;
4892 if (i == BTRFS_DEV_STAT_VALUES_MAX)
4893 return; /* all values == 0, suppress message */
4894
Josef Bacik606686e2012-06-04 14:03:51 -04004895 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
4896 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004897 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4898 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4899 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4900 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
4901 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
4902}
4903
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004904int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06004905 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004906{
4907 struct btrfs_device *dev;
4908 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4909 int i;
4910
4911 mutex_lock(&fs_devices->device_list_mutex);
4912 dev = btrfs_find_device(root, stats->devid, NULL, NULL);
4913 mutex_unlock(&fs_devices->device_list_mutex);
4914
4915 if (!dev) {
4916 printk(KERN_WARNING
4917 "btrfs: get dev_stats failed, device not found\n");
4918 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004919 } else if (!dev->dev_stats_valid) {
4920 printk(KERN_WARNING
4921 "btrfs: get dev_stats failed, not yet valid\n");
4922 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06004923 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004924 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4925 if (stats->nr_items > i)
4926 stats->values[i] =
4927 btrfs_dev_stat_read_and_reset(dev, i);
4928 else
4929 btrfs_dev_stat_reset(dev, i);
4930 }
4931 } else {
4932 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4933 if (stats->nr_items > i)
4934 stats->values[i] = btrfs_dev_stat_read(dev, i);
4935 }
4936 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
4937 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
4938 return 0;
4939}