blob: 3b394503bd4e775d420b345539b0742ebad35586 [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 */
Josef Bacik99f59442012-08-02 10:23:59 -0400572 if (device->name) {
573 name = rcu_string_strdup(device->name->str, GFP_NOFS);
574 BUG_ON(device->name && !name); /* -ENOMEM */
575 rcu_assign_pointer(new_device->name, name);
576 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000577 new_device->bdev = NULL;
578 new_device->writeable = 0;
579 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000580 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000581 list_replace_rcu(&device->dev_list, &new_device->dev_list);
582
583 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400584 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000585 mutex_unlock(&fs_devices->device_list_mutex);
586
Yan Zhenge4404d62008-12-12 10:03:26 -0500587 WARN_ON(fs_devices->open_devices);
588 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500589 fs_devices->opened = 0;
590 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500591
Chris Mason8a4b83c2008-03-24 15:02:07 -0400592 return 0;
593}
594
Yan Zheng2b820322008-11-17 21:11:30 -0500595int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
596{
Yan Zhenge4404d62008-12-12 10:03:26 -0500597 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500598 int ret;
599
600 mutex_lock(&uuid_mutex);
601 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500602 if (!fs_devices->opened) {
603 seed_devices = fs_devices->seed;
604 fs_devices->seed = NULL;
605 }
Yan Zheng2b820322008-11-17 21:11:30 -0500606 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500607
608 while (seed_devices) {
609 fs_devices = seed_devices;
610 seed_devices = fs_devices->seed;
611 __btrfs_close_devices(fs_devices);
612 free_fs_devices(fs_devices);
613 }
Yan Zheng2b820322008-11-17 21:11:30 -0500614 return ret;
615}
616
Yan Zhenge4404d62008-12-12 10:03:26 -0500617static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
618 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400619{
Josef Bacikd5e20032011-08-04 14:52:27 +0000620 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400621 struct block_device *bdev;
622 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400623 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400624 struct block_device *latest_bdev = NULL;
625 struct buffer_head *bh;
626 struct btrfs_super_block *disk_super;
627 u64 latest_devid = 0;
628 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400629 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500630 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400631 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400632
Tejun Heod4d77622010-11-13 11:55:18 +0100633 flags |= FMODE_EXCL;
634
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500635 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400636 if (device->bdev)
637 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400638 if (!device->name)
639 continue;
640
Josef Bacik606686e2012-06-04 14:03:51 -0400641 bdev = blkdev_get_by_path(device->name->str, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400642 if (IS_ERR(bdev)) {
Josef Bacik606686e2012-06-04 14:03:51 -0400643 printk(KERN_INFO "open %s failed\n", device->name->str);
Chris Masona0af4692008-05-13 16:03:06 -0400644 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400645 }
Chris Mason3c4bb262012-03-27 18:56:56 -0400646 filemap_write_and_wait(bdev->bd_inode->i_mapping);
647 invalidate_bdev(bdev);
Chris Masona061fc82008-05-07 11:43:44 -0400648 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400649
Yan Zhenga512bbf2008-12-08 16:46:26 -0500650 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300651 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400652 goto error_close;
653
654 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000655 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400656 if (devid != device->devid)
657 goto error_brelse;
658
Yan Zheng2b820322008-11-17 21:11:30 -0500659 if (memcmp(device->uuid, disk_super->dev_item.uuid,
660 BTRFS_UUID_SIZE))
661 goto error_brelse;
662
663 device->generation = btrfs_super_generation(disk_super);
664 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400665 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500666 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400667 latest_bdev = bdev;
668 }
669
Yan Zheng2b820322008-11-17 21:11:30 -0500670 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
671 device->writeable = 0;
672 } else {
673 device->writeable = !bdev_read_only(bdev);
674 seeding = 0;
675 }
676
Josef Bacikd5e20032011-08-04 14:52:27 +0000677 q = bdev_get_queue(bdev);
678 if (blk_queue_discard(q)) {
679 device->can_discard = 1;
680 fs_devices->num_can_discard++;
681 }
682
Chris Mason8a4b83c2008-03-24 15:02:07 -0400683 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400684 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500685 device->mode = flags;
686
Chris Masonc2898112009-06-10 09:51:32 -0400687 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
688 fs_devices->rotating = 1;
689
Chris Masona0af4692008-05-13 16:03:06 -0400690 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500691 if (device->writeable) {
692 fs_devices->rw_devices++;
693 list_add(&device->dev_alloc_list,
694 &fs_devices->alloc_list);
695 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000696 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400697 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400698
Chris Masona0af4692008-05-13 16:03:06 -0400699error_brelse:
700 brelse(bh);
701error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100702 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400703error:
704 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400705 }
Chris Masona0af4692008-05-13 16:03:06 -0400706 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300707 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400708 goto out;
709 }
Yan Zheng2b820322008-11-17 21:11:30 -0500710 fs_devices->seeding = seeding;
711 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400712 fs_devices->latest_bdev = latest_bdev;
713 fs_devices->latest_devid = latest_devid;
714 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500715 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400716out:
Yan Zheng2b820322008-11-17 21:11:30 -0500717 return ret;
718}
719
720int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500721 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500722{
723 int ret;
724
725 mutex_lock(&uuid_mutex);
726 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500727 fs_devices->opened++;
728 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500729 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500730 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500731 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400732 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400733 return ret;
734}
735
Christoph Hellwig97288f22008-12-02 06:36:09 -0500736int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400737 struct btrfs_fs_devices **fs_devices_ret)
738{
739 struct btrfs_super_block *disk_super;
740 struct block_device *bdev;
741 struct buffer_head *bh;
742 int ret;
743 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400744 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400745 u64 total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400746
Tejun Heod4d77622010-11-13 11:55:18 +0100747 flags |= FMODE_EXCL;
748 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400749
750 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400751 ret = PTR_ERR(bdev);
752 goto error;
753 }
754
Al Viro10f63272011-11-17 15:05:22 -0500755 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400756 ret = set_blocksize(bdev, 4096);
757 if (ret)
758 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500759 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000761 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400762 goto error_close;
763 }
764 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000765 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400766 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400767 total_devices = btrfs_super_num_devices(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400768 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500769 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200770 else
771 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500772 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500773 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400774 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400775 if (!ret && fs_devices_ret)
776 (*fs_devices_ret)->total_devices = total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400777 brelse(bh);
778error_close:
Al Viro10f63272011-11-17 15:05:22 -0500779 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100780 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400781error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400782 return ret;
783}
Chris Mason0b86a832008-03-24 15:01:56 -0400784
Miao Xie6d07bce2011-01-05 10:07:31 +0000785/* helper to account the used device space in the range */
786int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
787 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400788{
789 struct btrfs_key key;
790 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000791 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500792 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000793 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400794 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000795 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400796 struct extent_buffer *l;
797
Miao Xie6d07bce2011-01-05 10:07:31 +0000798 *length = 0;
799
800 if (start >= device->total_bytes)
801 return 0;
802
Yan Zheng2b820322008-11-17 21:11:30 -0500803 path = btrfs_alloc_path();
804 if (!path)
805 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400806 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400807
Chris Mason0b86a832008-03-24 15:01:56 -0400808 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000809 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400810 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000811
812 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400813 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000814 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400815 if (ret > 0) {
816 ret = btrfs_previous_item(root, path, key.objectid, key.type);
817 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000818 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400819 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000820
Chris Mason0b86a832008-03-24 15:01:56 -0400821 while (1) {
822 l = path->nodes[0];
823 slot = path->slots[0];
824 if (slot >= btrfs_header_nritems(l)) {
825 ret = btrfs_next_leaf(root, path);
826 if (ret == 0)
827 continue;
828 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000829 goto out;
830
831 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400832 }
833 btrfs_item_key_to_cpu(l, &key, slot);
834
835 if (key.objectid < device->devid)
836 goto next;
837
838 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000839 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400840
Chris Masond3977122009-01-05 21:25:51 -0500841 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400842 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400843
Chris Mason0b86a832008-03-24 15:01:56 -0400844 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000845 extent_end = key.offset + btrfs_dev_extent_length(l,
846 dev_extent);
847 if (key.offset <= start && extent_end > end) {
848 *length = end - start + 1;
849 break;
850 } else if (key.offset <= start && extent_end > start)
851 *length += extent_end - start;
852 else if (key.offset > start && extent_end <= end)
853 *length += extent_end - key.offset;
854 else if (key.offset > start && key.offset <= end) {
855 *length += end - key.offset + 1;
856 break;
857 } else if (key.offset > end)
858 break;
859
860next:
861 path->slots[0]++;
862 }
863 ret = 0;
864out:
865 btrfs_free_path(path);
866 return ret;
867}
868
Chris Mason0b86a832008-03-24 15:01:56 -0400869/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000870 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000871 * @device: the device which we search the free space in
872 * @num_bytes: the size of the free space that we need
873 * @start: store the start of the free space.
874 * @len: the size of the free space. that we find, or the size of the max
875 * free space if we don't find suitable free space
876 *
Chris Mason0b86a832008-03-24 15:01:56 -0400877 * this uses a pretty simple search, the expectation is that it is
878 * called very infrequently and that a given device has a small number
879 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000880 *
881 * @start is used to store the start of the free space if we find. But if we
882 * don't find suitable free space, it will be used to store the start position
883 * of the max free space.
884 *
885 * @len is used to store the size of the free space that we find.
886 * But if we don't find suitable free space, it is used to store the size of
887 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400888 */
Li Zefan125ccb02011-12-08 15:07:24 +0800889int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000890 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400891{
892 struct btrfs_key key;
893 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000894 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400895 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000896 u64 hole_size;
897 u64 max_hole_start;
898 u64 max_hole_size;
899 u64 extent_end;
900 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400901 u64 search_end = device->total_bytes;
902 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000903 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400904 struct extent_buffer *l;
905
Chris Mason0b86a832008-03-24 15:01:56 -0400906 /* FIXME use last free of some kind */
907
908 /* we don't want to overwrite the superblock on the drive,
909 * so we make sure to start at an offset of at least 1MB
910 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200911 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400912
Miao Xie7bfc8372011-01-05 10:07:26 +0000913 max_hole_start = search_start;
914 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000915 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000916
917 if (search_start >= search_end) {
918 ret = -ENOSPC;
919 goto error;
920 }
921
922 path = btrfs_alloc_path();
923 if (!path) {
924 ret = -ENOMEM;
925 goto error;
926 }
927 path->reada = 2;
928
Chris Mason0b86a832008-03-24 15:01:56 -0400929 key.objectid = device->devid;
930 key.offset = search_start;
931 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000932
Li Zefan125ccb02011-12-08 15:07:24 +0800933 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400934 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000935 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400936 if (ret > 0) {
937 ret = btrfs_previous_item(root, path, key.objectid, key.type);
938 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000939 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400940 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000941
Chris Mason0b86a832008-03-24 15:01:56 -0400942 while (1) {
943 l = path->nodes[0];
944 slot = path->slots[0];
945 if (slot >= btrfs_header_nritems(l)) {
946 ret = btrfs_next_leaf(root, path);
947 if (ret == 0)
948 continue;
949 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000950 goto out;
951
952 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400953 }
954 btrfs_item_key_to_cpu(l, &key, slot);
955
956 if (key.objectid < device->devid)
957 goto next;
958
959 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000960 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400961
Chris Mason0b86a832008-03-24 15:01:56 -0400962 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
963 goto next;
964
Miao Xie7bfc8372011-01-05 10:07:26 +0000965 if (key.offset > search_start) {
966 hole_size = key.offset - search_start;
967
968 if (hole_size > max_hole_size) {
969 max_hole_start = search_start;
970 max_hole_size = hole_size;
971 }
972
973 /*
974 * If this free space is greater than which we need,
975 * it must be the max free space that we have found
976 * until now, so max_hole_start must point to the start
977 * of this free space and the length of this free space
978 * is stored in max_hole_size. Thus, we return
979 * max_hole_start and max_hole_size and go back to the
980 * caller.
981 */
982 if (hole_size >= num_bytes) {
983 ret = 0;
984 goto out;
985 }
986 }
987
Chris Mason0b86a832008-03-24 15:01:56 -0400988 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000989 extent_end = key.offset + btrfs_dev_extent_length(l,
990 dev_extent);
991 if (extent_end > search_start)
992 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400993next:
994 path->slots[0]++;
995 cond_resched();
996 }
Chris Mason0b86a832008-03-24 15:01:56 -0400997
liubo38c01b92011-08-02 02:39:03 +0000998 /*
999 * At this point, search_start should be the end of
1000 * allocated dev extents, and when shrinking the device,
1001 * search_end may be smaller than search_start.
1002 */
1003 if (search_end > search_start)
1004 hole_size = search_end - search_start;
1005
Miao Xie7bfc8372011-01-05 10:07:26 +00001006 if (hole_size > max_hole_size) {
1007 max_hole_start = search_start;
1008 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001009 }
Chris Mason0b86a832008-03-24 15:01:56 -04001010
Miao Xie7bfc8372011-01-05 10:07:26 +00001011 /* See above. */
1012 if (hole_size < num_bytes)
1013 ret = -ENOSPC;
1014 else
1015 ret = 0;
1016
1017out:
Yan Zheng2b820322008-11-17 21:11:30 -05001018 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001019error:
1020 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001021 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001022 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001023 return ret;
1024}
1025
Christoph Hellwigb2950862008-12-02 09:54:17 -05001026static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001027 struct btrfs_device *device,
1028 u64 start)
1029{
1030 int ret;
1031 struct btrfs_path *path;
1032 struct btrfs_root *root = device->dev_root;
1033 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001034 struct btrfs_key found_key;
1035 struct extent_buffer *leaf = NULL;
1036 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001037
1038 path = btrfs_alloc_path();
1039 if (!path)
1040 return -ENOMEM;
1041
1042 key.objectid = device->devid;
1043 key.offset = start;
1044 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001045again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001046 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001047 if (ret > 0) {
1048 ret = btrfs_previous_item(root, path, key.objectid,
1049 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001050 if (ret)
1051 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001052 leaf = path->nodes[0];
1053 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1054 extent = btrfs_item_ptr(leaf, path->slots[0],
1055 struct btrfs_dev_extent);
1056 BUG_ON(found_key.offset > start || found_key.offset +
1057 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001058 key = found_key;
1059 btrfs_release_path(path);
1060 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001061 } else if (ret == 0) {
1062 leaf = path->nodes[0];
1063 extent = btrfs_item_ptr(leaf, path->slots[0],
1064 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001065 } else {
1066 btrfs_error(root->fs_info, ret, "Slot search failed");
1067 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001068 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001069
Josef Bacik2bf64752011-09-26 17:12:22 -04001070 if (device->bytes_used > 0) {
1071 u64 len = btrfs_dev_extent_length(leaf, extent);
1072 device->bytes_used -= len;
1073 spin_lock(&root->fs_info->free_chunk_lock);
1074 root->fs_info->free_chunk_space += len;
1075 spin_unlock(&root->fs_info->free_chunk_lock);
1076 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001077 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001078 if (ret) {
1079 btrfs_error(root->fs_info, ret,
1080 "Failed to remove dev extent item");
1081 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001082out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001083 btrfs_free_path(path);
1084 return ret;
1085}
1086
Yan Zheng2b820322008-11-17 21:11:30 -05001087int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001088 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001089 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001090 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001091{
1092 int ret;
1093 struct btrfs_path *path;
1094 struct btrfs_root *root = device->dev_root;
1095 struct btrfs_dev_extent *extent;
1096 struct extent_buffer *leaf;
1097 struct btrfs_key key;
1098
Chris Masondfe25022008-05-13 13:46:40 -04001099 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001100 path = btrfs_alloc_path();
1101 if (!path)
1102 return -ENOMEM;
1103
Chris Mason0b86a832008-03-24 15:01:56 -04001104 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001105 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001106 key.type = BTRFS_DEV_EXTENT_KEY;
1107 ret = btrfs_insert_empty_item(trans, root, path, &key,
1108 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001109 if (ret)
1110 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001111
1112 leaf = path->nodes[0];
1113 extent = btrfs_item_ptr(leaf, path->slots[0],
1114 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001115 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1116 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1117 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1118
1119 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1120 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1121 BTRFS_UUID_SIZE);
1122
Chris Mason0b86a832008-03-24 15:01:56 -04001123 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1124 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001125out:
Chris Mason0b86a832008-03-24 15:01:56 -04001126 btrfs_free_path(path);
1127 return ret;
1128}
1129
Chris Masona1b32a52008-09-05 16:09:51 -04001130static noinline int find_next_chunk(struct btrfs_root *root,
1131 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001132{
1133 struct btrfs_path *path;
1134 int ret;
1135 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001136 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001137 struct btrfs_key found_key;
1138
1139 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001140 if (!path)
1141 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001142
Chris Masone17cade2008-04-15 15:41:47 -04001143 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001144 key.offset = (u64)-1;
1145 key.type = BTRFS_CHUNK_ITEM_KEY;
1146
1147 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1148 if (ret < 0)
1149 goto error;
1150
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001151 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001152
1153 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1154 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001155 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001156 } else {
1157 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1158 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001159 if (found_key.objectid != objectid)
1160 *offset = 0;
1161 else {
1162 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1163 struct btrfs_chunk);
1164 *offset = found_key.offset +
1165 btrfs_chunk_length(path->nodes[0], chunk);
1166 }
Chris Mason0b86a832008-03-24 15:01:56 -04001167 }
1168 ret = 0;
1169error:
1170 btrfs_free_path(path);
1171 return ret;
1172}
1173
Yan Zheng2b820322008-11-17 21:11:30 -05001174static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001175{
1176 int ret;
1177 struct btrfs_key key;
1178 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001179 struct btrfs_path *path;
1180
1181 root = root->fs_info->chunk_root;
1182
1183 path = btrfs_alloc_path();
1184 if (!path)
1185 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001186
1187 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1188 key.type = BTRFS_DEV_ITEM_KEY;
1189 key.offset = (u64)-1;
1190
1191 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1192 if (ret < 0)
1193 goto error;
1194
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001195 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001196
1197 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1198 BTRFS_DEV_ITEM_KEY);
1199 if (ret) {
1200 *objectid = 1;
1201 } else {
1202 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1203 path->slots[0]);
1204 *objectid = found_key.offset + 1;
1205 }
1206 ret = 0;
1207error:
Yan Zheng2b820322008-11-17 21:11:30 -05001208 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001209 return ret;
1210}
1211
1212/*
1213 * the device information is stored in the chunk root
1214 * the btrfs_device struct should be fully filled in
1215 */
1216int btrfs_add_device(struct btrfs_trans_handle *trans,
1217 struct btrfs_root *root,
1218 struct btrfs_device *device)
1219{
1220 int ret;
1221 struct btrfs_path *path;
1222 struct btrfs_dev_item *dev_item;
1223 struct extent_buffer *leaf;
1224 struct btrfs_key key;
1225 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001226
1227 root = root->fs_info->chunk_root;
1228
1229 path = btrfs_alloc_path();
1230 if (!path)
1231 return -ENOMEM;
1232
Chris Mason0b86a832008-03-24 15:01:56 -04001233 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1234 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001235 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001236
1237 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001238 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001239 if (ret)
1240 goto out;
1241
1242 leaf = path->nodes[0];
1243 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1244
1245 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001246 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001247 btrfs_set_device_type(leaf, dev_item, device->type);
1248 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1249 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1250 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001251 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1252 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001253 btrfs_set_device_group(leaf, dev_item, 0);
1254 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1255 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001256 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001257
Chris Mason0b86a832008-03-24 15:01:56 -04001258 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001259 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001260 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1261 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001262 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001263
Yan Zheng2b820322008-11-17 21:11:30 -05001264 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001265out:
1266 btrfs_free_path(path);
1267 return ret;
1268}
Chris Mason8f18cf12008-04-25 16:53:30 -04001269
Chris Masona061fc82008-05-07 11:43:44 -04001270static int btrfs_rm_dev_item(struct btrfs_root *root,
1271 struct btrfs_device *device)
1272{
1273 int ret;
1274 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001275 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001276 struct btrfs_trans_handle *trans;
1277
1278 root = root->fs_info->chunk_root;
1279
1280 path = btrfs_alloc_path();
1281 if (!path)
1282 return -ENOMEM;
1283
Yan, Zhenga22285a2010-05-16 10:48:46 -04001284 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001285 if (IS_ERR(trans)) {
1286 btrfs_free_path(path);
1287 return PTR_ERR(trans);
1288 }
Chris Masona061fc82008-05-07 11:43:44 -04001289 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1290 key.type = BTRFS_DEV_ITEM_KEY;
1291 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001292 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001293
1294 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1295 if (ret < 0)
1296 goto out;
1297
1298 if (ret > 0) {
1299 ret = -ENOENT;
1300 goto out;
1301 }
1302
1303 ret = btrfs_del_item(trans, root, path);
1304 if (ret)
1305 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001306out:
1307 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001308 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001309 btrfs_commit_transaction(trans, root);
1310 return ret;
1311}
1312
1313int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1314{
1315 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001316 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001317 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001318 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001319 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001320 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001321 u64 all_avail;
1322 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001323 u64 num_devices;
1324 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001325 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001326 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001327
Chris Masona061fc82008-05-07 11:43:44 -04001328 mutex_lock(&uuid_mutex);
1329
1330 all_avail = root->fs_info->avail_data_alloc_bits |
1331 root->fs_info->avail_system_alloc_bits |
1332 root->fs_info->avail_metadata_alloc_bits;
1333
1334 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001335 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001336 printk(KERN_ERR "btrfs: unable to go below four devices "
1337 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001338 ret = -EINVAL;
1339 goto out;
1340 }
1341
1342 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001343 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001344 printk(KERN_ERR "btrfs: unable to go below two "
1345 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001346 ret = -EINVAL;
1347 goto out;
1348 }
1349
Chris Masondfe25022008-05-13 13:46:40 -04001350 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001351 struct list_head *devices;
1352 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001353
Chris Masondfe25022008-05-13 13:46:40 -04001354 device = NULL;
1355 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001356 /*
1357 * It is safe to read the devices since the volume_mutex
1358 * is held.
1359 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001360 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001361 if (tmp->in_fs_metadata && !tmp->bdev) {
1362 device = tmp;
1363 break;
1364 }
1365 }
1366 bdev = NULL;
1367 bh = NULL;
1368 disk_super = NULL;
1369 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001370 printk(KERN_ERR "btrfs: no missing devices found to "
1371 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001372 goto out;
1373 }
Chris Masondfe25022008-05-13 13:46:40 -04001374 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001375 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1376 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001377 if (IS_ERR(bdev)) {
1378 ret = PTR_ERR(bdev);
1379 goto out;
1380 }
1381
Yan Zheng2b820322008-11-17 21:11:30 -05001382 set_blocksize(bdev, 4096);
Chris Mason3c4bb262012-03-27 18:56:56 -04001383 invalidate_bdev(bdev);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001384 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001385 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001386 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001387 goto error_close;
1388 }
1389 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001390 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001391 dev_uuid = disk_super->dev_item.uuid;
1392 device = btrfs_find_device(root, devid, dev_uuid,
1393 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001394 if (!device) {
1395 ret = -ENOENT;
1396 goto error_brelse;
1397 }
Chris Masondfe25022008-05-13 13:46:40 -04001398 }
Yan Zheng2b820322008-11-17 21:11:30 -05001399
1400 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001401 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1402 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001403 ret = -EINVAL;
1404 goto error_brelse;
1405 }
1406
1407 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001408 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001409 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001410 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001411 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001412 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001413 }
Chris Masona061fc82008-05-07 11:43:44 -04001414
1415 ret = btrfs_shrink_device(device, 0);
1416 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001417 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001418
Chris Masona061fc82008-05-07 11:43:44 -04001419 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1420 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001421 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001422
Josef Bacik2bf64752011-09-26 17:12:22 -04001423 spin_lock(&root->fs_info->free_chunk_lock);
1424 root->fs_info->free_chunk_space = device->total_bytes -
1425 device->bytes_used;
1426 spin_unlock(&root->fs_info->free_chunk_lock);
1427
Yan Zheng2b820322008-11-17 21:11:30 -05001428 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001429 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001430
1431 /*
1432 * the device list mutex makes sure that we don't change
1433 * the device list while someone else is writing out all
1434 * the device supers.
1435 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001436
1437 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001438 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001439 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001440
Yan Zhenge4404d62008-12-12 10:03:26 -05001441 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001442 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001443
Chris Masoncd02dca2010-12-13 14:56:23 -05001444 if (device->missing)
1445 root->fs_info->fs_devices->missing_devices--;
1446
Yan Zheng2b820322008-11-17 21:11:30 -05001447 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1448 struct btrfs_device, dev_list);
1449 if (device->bdev == root->fs_info->sb->s_bdev)
1450 root->fs_info->sb->s_bdev = next_device->bdev;
1451 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1452 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1453
Xiao Guangrong1f781602011-04-20 10:09:16 +00001454 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001455 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001456
1457 call_rcu(&device->rcu, free_device);
1458 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001459
David Sterba6c417612011-04-13 15:41:04 +02001460 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1461 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001462
Xiao Guangrong1f781602011-04-20 10:09:16 +00001463 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001464 struct btrfs_fs_devices *fs_devices;
1465 fs_devices = root->fs_info->fs_devices;
1466 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001467 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001468 break;
1469 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001470 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001471 fs_devices->seed = cur_devices->seed;
1472 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001473 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001474 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001475 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001476 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001477 }
1478
1479 /*
1480 * at this point, the device is zero sized. We want to
1481 * remove it from the devices list and zero out the old super
1482 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001483 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001484 /* make sure this device isn't detected as part of
1485 * the FS anymore
1486 */
1487 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1488 set_buffer_dirty(bh);
1489 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001490 }
Chris Masona061fc82008-05-07 11:43:44 -04001491
Chris Masona061fc82008-05-07 11:43:44 -04001492 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001493
1494error_brelse:
1495 brelse(bh);
1496error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001497 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001498 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001499out:
1500 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001501 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001502error_undo:
1503 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001504 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001505 list_add(&device->dev_alloc_list,
1506 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001507 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001508 root->fs_info->fs_devices->rw_devices++;
1509 }
1510 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001511}
1512
Yan Zheng2b820322008-11-17 21:11:30 -05001513/*
1514 * does all the dirty work required for changing file system's UUID.
1515 */
Li Zefan125ccb02011-12-08 15:07:24 +08001516static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001517{
1518 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1519 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001520 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001521 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001522 struct btrfs_device *device;
1523 u64 super_flags;
1524
1525 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001526 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001527 return -EINVAL;
1528
Yan Zhenge4404d62008-12-12 10:03:26 -05001529 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1530 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001531 return -ENOMEM;
1532
Yan Zhenge4404d62008-12-12 10:03:26 -05001533 old_devices = clone_fs_devices(fs_devices);
1534 if (IS_ERR(old_devices)) {
1535 kfree(seed_devices);
1536 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001537 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001538
Yan Zheng2b820322008-11-17 21:11:30 -05001539 list_add(&old_devices->list, &fs_uuids);
1540
Yan Zhenge4404d62008-12-12 10:03:26 -05001541 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1542 seed_devices->opened = 1;
1543 INIT_LIST_HEAD(&seed_devices->devices);
1544 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001545 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001546
1547 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001548 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1549 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001550 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1551
Yan Zhenge4404d62008-12-12 10:03:26 -05001552 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1553 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1554 device->fs_devices = seed_devices;
1555 }
1556
Yan Zheng2b820322008-11-17 21:11:30 -05001557 fs_devices->seeding = 0;
1558 fs_devices->num_devices = 0;
1559 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001560 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001561 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001562
1563 generate_random_uuid(fs_devices->fsid);
1564 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1565 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1566 super_flags = btrfs_super_flags(disk_super) &
1567 ~BTRFS_SUPER_FLAG_SEEDING;
1568 btrfs_set_super_flags(disk_super, super_flags);
1569
1570 return 0;
1571}
1572
1573/*
1574 * strore the expected generation for seed devices in device items.
1575 */
1576static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1577 struct btrfs_root *root)
1578{
1579 struct btrfs_path *path;
1580 struct extent_buffer *leaf;
1581 struct btrfs_dev_item *dev_item;
1582 struct btrfs_device *device;
1583 struct btrfs_key key;
1584 u8 fs_uuid[BTRFS_UUID_SIZE];
1585 u8 dev_uuid[BTRFS_UUID_SIZE];
1586 u64 devid;
1587 int ret;
1588
1589 path = btrfs_alloc_path();
1590 if (!path)
1591 return -ENOMEM;
1592
1593 root = root->fs_info->chunk_root;
1594 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1595 key.offset = 0;
1596 key.type = BTRFS_DEV_ITEM_KEY;
1597
1598 while (1) {
1599 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1600 if (ret < 0)
1601 goto error;
1602
1603 leaf = path->nodes[0];
1604next_slot:
1605 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1606 ret = btrfs_next_leaf(root, path);
1607 if (ret > 0)
1608 break;
1609 if (ret < 0)
1610 goto error;
1611 leaf = path->nodes[0];
1612 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001613 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001614 continue;
1615 }
1616
1617 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1618 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1619 key.type != BTRFS_DEV_ITEM_KEY)
1620 break;
1621
1622 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1623 struct btrfs_dev_item);
1624 devid = btrfs_device_id(leaf, dev_item);
1625 read_extent_buffer(leaf, dev_uuid,
1626 (unsigned long)btrfs_device_uuid(dev_item),
1627 BTRFS_UUID_SIZE);
1628 read_extent_buffer(leaf, fs_uuid,
1629 (unsigned long)btrfs_device_fsid(dev_item),
1630 BTRFS_UUID_SIZE);
1631 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001632 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001633
1634 if (device->fs_devices->seeding) {
1635 btrfs_set_device_generation(leaf, dev_item,
1636 device->generation);
1637 btrfs_mark_buffer_dirty(leaf);
1638 }
1639
1640 path->slots[0]++;
1641 goto next_slot;
1642 }
1643 ret = 0;
1644error:
1645 btrfs_free_path(path);
1646 return ret;
1647}
1648
Chris Mason788f20e2008-04-28 15:29:42 -04001649int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1650{
Josef Bacikd5e20032011-08-04 14:52:27 +00001651 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001652 struct btrfs_trans_handle *trans;
1653 struct btrfs_device *device;
1654 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001655 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001656 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001657 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001658 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001659 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001660 int ret = 0;
1661
Yan Zheng2b820322008-11-17 21:11:30 -05001662 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001663 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001664
Li Zefana5d16332011-12-07 20:08:40 -05001665 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001666 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001667 if (IS_ERR(bdev))
1668 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001669
Yan Zheng2b820322008-11-17 21:11:30 -05001670 if (root->fs_info->fs_devices->seeding) {
1671 seeding_dev = 1;
1672 down_write(&sb->s_umount);
1673 mutex_lock(&uuid_mutex);
1674 }
1675
Chris Mason8c8bee12008-09-29 11:19:10 -04001676 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001677
Chris Mason788f20e2008-04-28 15:29:42 -04001678 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001679 /*
1680 * we have the volume lock, so we don't need the extra
1681 * device list mutex while reading the list here.
1682 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001683 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001684 if (device->bdev == bdev) {
1685 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001686 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001687 }
1688 }
1689
1690 device = kzalloc(sizeof(*device), GFP_NOFS);
1691 if (!device) {
1692 /* we can safely leave the fs_devices entry around */
1693 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001694 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001695 }
1696
Josef Bacik606686e2012-06-04 14:03:51 -04001697 name = rcu_string_strdup(device_path, GFP_NOFS);
1698 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001699 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001700 ret = -ENOMEM;
1701 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001702 }
Josef Bacik606686e2012-06-04 14:03:51 -04001703 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001704
1705 ret = find_next_devid(root, &device->devid);
1706 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001707 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001708 kfree(device);
1709 goto error;
1710 }
1711
Yan, Zhenga22285a2010-05-16 10:48:46 -04001712 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001713 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001714 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001715 kfree(device);
1716 ret = PTR_ERR(trans);
1717 goto error;
1718 }
1719
Yan Zheng2b820322008-11-17 21:11:30 -05001720 lock_chunks(root);
1721
Josef Bacikd5e20032011-08-04 14:52:27 +00001722 q = bdev_get_queue(bdev);
1723 if (blk_queue_discard(q))
1724 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001725 device->writeable = 1;
1726 device->work.func = pending_bios_fn;
1727 generate_random_uuid(device->uuid);
1728 spin_lock_init(&device->io_lock);
1729 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001730 device->io_width = root->sectorsize;
1731 device->io_align = root->sectorsize;
1732 device->sector_size = root->sectorsize;
1733 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001734 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001735 device->dev_root = root->fs_info->dev_root;
1736 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001737 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001738 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001739 set_blocksize(device->bdev, 4096);
1740
Yan Zheng2b820322008-11-17 21:11:30 -05001741 if (seeding_dev) {
1742 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001743 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001744 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001745 }
1746
1747 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001748
1749 /*
1750 * we don't want write_supers to jump in here with our device
1751 * half setup
1752 */
1753 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001754 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001755 list_add(&device->dev_alloc_list,
1756 &root->fs_info->fs_devices->alloc_list);
1757 root->fs_info->fs_devices->num_devices++;
1758 root->fs_info->fs_devices->open_devices++;
1759 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04001760 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001761 if (device->can_discard)
1762 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001763 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1764
Josef Bacik2bf64752011-09-26 17:12:22 -04001765 spin_lock(&root->fs_info->free_chunk_lock);
1766 root->fs_info->free_chunk_space += device->total_bytes;
1767 spin_unlock(&root->fs_info->free_chunk_lock);
1768
Chris Masonc2898112009-06-10 09:51:32 -04001769 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1770 root->fs_info->fs_devices->rotating = 1;
1771
David Sterba6c417612011-04-13 15:41:04 +02001772 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1773 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001774 total_bytes + device->total_bytes);
1775
David Sterba6c417612011-04-13 15:41:04 +02001776 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1777 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001778 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001779 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001780
Yan Zheng2b820322008-11-17 21:11:30 -05001781 if (seeding_dev) {
1782 ret = init_first_rw_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001783 if (ret)
1784 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001785 ret = btrfs_finish_sprout(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001786 if (ret)
1787 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001788 } else {
1789 ret = btrfs_add_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001790 if (ret)
1791 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001792 }
1793
Chris Mason913d9522009-03-10 13:17:18 -04001794 /*
1795 * we've got more storage, clear any full flags on the space
1796 * infos
1797 */
1798 btrfs_clear_space_info_full(root->fs_info);
1799
Chris Mason7d9eb122008-07-08 14:19:17 -04001800 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001801 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001802
1803 if (seeding_dev) {
1804 mutex_unlock(&uuid_mutex);
1805 up_write(&sb->s_umount);
1806
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001807 if (ret) /* transaction commit */
1808 return ret;
1809
Yan Zheng2b820322008-11-17 21:11:30 -05001810 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001811 if (ret < 0)
1812 btrfs_error(root->fs_info, ret,
1813 "Failed to relocate sys chunks after "
1814 "device initialization. This can be fixed "
1815 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001816 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001817
Chris Mason788f20e2008-04-28 15:29:42 -04001818 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001819
1820error_trans:
1821 unlock_chunks(root);
1822 btrfs_abort_transaction(trans, root, ret);
1823 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04001824 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001825 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001826error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001827 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001828 if (seeding_dev) {
1829 mutex_unlock(&uuid_mutex);
1830 up_write(&sb->s_umount);
1831 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001832 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001833}
1834
Chris Masond3977122009-01-05 21:25:51 -05001835static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1836 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001837{
1838 int ret;
1839 struct btrfs_path *path;
1840 struct btrfs_root *root;
1841 struct btrfs_dev_item *dev_item;
1842 struct extent_buffer *leaf;
1843 struct btrfs_key key;
1844
1845 root = device->dev_root->fs_info->chunk_root;
1846
1847 path = btrfs_alloc_path();
1848 if (!path)
1849 return -ENOMEM;
1850
1851 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1852 key.type = BTRFS_DEV_ITEM_KEY;
1853 key.offset = device->devid;
1854
1855 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1856 if (ret < 0)
1857 goto out;
1858
1859 if (ret > 0) {
1860 ret = -ENOENT;
1861 goto out;
1862 }
1863
1864 leaf = path->nodes[0];
1865 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1866
1867 btrfs_set_device_id(leaf, dev_item, device->devid);
1868 btrfs_set_device_type(leaf, dev_item, device->type);
1869 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1870 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1871 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001872 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001873 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1874 btrfs_mark_buffer_dirty(leaf);
1875
1876out:
1877 btrfs_free_path(path);
1878 return ret;
1879}
1880
Chris Mason7d9eb122008-07-08 14:19:17 -04001881static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001882 struct btrfs_device *device, u64 new_size)
1883{
1884 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001885 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001886 u64 old_total = btrfs_super_total_bytes(super_copy);
1887 u64 diff = new_size - device->total_bytes;
1888
Yan Zheng2b820322008-11-17 21:11:30 -05001889 if (!device->writeable)
1890 return -EACCES;
1891 if (new_size <= device->total_bytes)
1892 return -EINVAL;
1893
Chris Mason8f18cf12008-04-25 16:53:30 -04001894 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001895 device->fs_devices->total_rw_bytes += diff;
1896
1897 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001898 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001899 btrfs_clear_space_info_full(device->dev_root->fs_info);
1900
Chris Mason8f18cf12008-04-25 16:53:30 -04001901 return btrfs_update_device(trans, device);
1902}
1903
Chris Mason7d9eb122008-07-08 14:19:17 -04001904int btrfs_grow_device(struct btrfs_trans_handle *trans,
1905 struct btrfs_device *device, u64 new_size)
1906{
1907 int ret;
1908 lock_chunks(device->dev_root);
1909 ret = __btrfs_grow_device(trans, device, new_size);
1910 unlock_chunks(device->dev_root);
1911 return ret;
1912}
1913
Chris Mason8f18cf12008-04-25 16:53:30 -04001914static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1915 struct btrfs_root *root,
1916 u64 chunk_tree, u64 chunk_objectid,
1917 u64 chunk_offset)
1918{
1919 int ret;
1920 struct btrfs_path *path;
1921 struct btrfs_key key;
1922
1923 root = root->fs_info->chunk_root;
1924 path = btrfs_alloc_path();
1925 if (!path)
1926 return -ENOMEM;
1927
1928 key.objectid = chunk_objectid;
1929 key.offset = chunk_offset;
1930 key.type = BTRFS_CHUNK_ITEM_KEY;
1931
1932 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001933 if (ret < 0)
1934 goto out;
1935 else if (ret > 0) { /* Logic error or corruption */
1936 btrfs_error(root->fs_info, -ENOENT,
1937 "Failed lookup while freeing chunk.");
1938 ret = -ENOENT;
1939 goto out;
1940 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001941
1942 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001943 if (ret < 0)
1944 btrfs_error(root->fs_info, ret,
1945 "Failed to delete chunk item.");
1946out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001947 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001948 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001949}
1950
Christoph Hellwigb2950862008-12-02 09:54:17 -05001951static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001952 chunk_offset)
1953{
David Sterba6c417612011-04-13 15:41:04 +02001954 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001955 struct btrfs_disk_key *disk_key;
1956 struct btrfs_chunk *chunk;
1957 u8 *ptr;
1958 int ret = 0;
1959 u32 num_stripes;
1960 u32 array_size;
1961 u32 len = 0;
1962 u32 cur;
1963 struct btrfs_key key;
1964
1965 array_size = btrfs_super_sys_array_size(super_copy);
1966
1967 ptr = super_copy->sys_chunk_array;
1968 cur = 0;
1969
1970 while (cur < array_size) {
1971 disk_key = (struct btrfs_disk_key *)ptr;
1972 btrfs_disk_key_to_cpu(&key, disk_key);
1973
1974 len = sizeof(*disk_key);
1975
1976 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1977 chunk = (struct btrfs_chunk *)(ptr + len);
1978 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1979 len += btrfs_chunk_item_size(num_stripes);
1980 } else {
1981 ret = -EIO;
1982 break;
1983 }
1984 if (key.objectid == chunk_objectid &&
1985 key.offset == chunk_offset) {
1986 memmove(ptr, ptr + len, array_size - (cur + len));
1987 array_size -= len;
1988 btrfs_set_super_sys_array_size(super_copy, array_size);
1989 } else {
1990 ptr += len;
1991 cur += len;
1992 }
1993 }
1994 return ret;
1995}
1996
Christoph Hellwigb2950862008-12-02 09:54:17 -05001997static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001998 u64 chunk_tree, u64 chunk_objectid,
1999 u64 chunk_offset)
2000{
2001 struct extent_map_tree *em_tree;
2002 struct btrfs_root *extent_root;
2003 struct btrfs_trans_handle *trans;
2004 struct extent_map *em;
2005 struct map_lookup *map;
2006 int ret;
2007 int i;
2008
2009 root = root->fs_info->chunk_root;
2010 extent_root = root->fs_info->extent_root;
2011 em_tree = &root->fs_info->mapping_tree.map_tree;
2012
Josef Bacikba1bf482009-09-11 16:11:19 -04002013 ret = btrfs_can_relocate(extent_root, chunk_offset);
2014 if (ret)
2015 return -ENOSPC;
2016
Chris Mason8f18cf12008-04-25 16:53:30 -04002017 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002018 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002019 if (ret)
2020 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002021
Yan, Zhenga22285a2010-05-16 10:48:46 -04002022 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002023 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002024
Chris Mason7d9eb122008-07-08 14:19:17 -04002025 lock_chunks(root);
2026
Chris Mason8f18cf12008-04-25 16:53:30 -04002027 /*
2028 * step two, delete the device extents and the
2029 * chunk tree entries
2030 */
Chris Mason890871b2009-09-02 16:24:52 -04002031 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002032 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002033 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002034
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002035 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002036 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002037 map = (struct map_lookup *)em->bdev;
2038
2039 for (i = 0; i < map->num_stripes; i++) {
2040 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2041 map->stripes[i].physical);
2042 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002043
Chris Masondfe25022008-05-13 13:46:40 -04002044 if (map->stripes[i].dev) {
2045 ret = btrfs_update_device(trans, map->stripes[i].dev);
2046 BUG_ON(ret);
2047 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002048 }
2049 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2050 chunk_offset);
2051
2052 BUG_ON(ret);
2053
liubo1abe9b82011-03-24 11:18:59 +00002054 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2055
Chris Mason8f18cf12008-04-25 16:53:30 -04002056 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2057 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2058 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002059 }
2060
Zheng Yan1a40e232008-09-26 10:09:34 -04002061 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2062 BUG_ON(ret);
2063
Chris Mason890871b2009-09-02 16:24:52 -04002064 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002065 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002066 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002067
Chris Mason8f18cf12008-04-25 16:53:30 -04002068 kfree(map);
2069 em->bdev = NULL;
2070
2071 /* once for the tree */
2072 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002073 /* once for us */
2074 free_extent_map(em);
2075
Chris Mason7d9eb122008-07-08 14:19:17 -04002076 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002077 btrfs_end_transaction(trans, root);
2078 return 0;
2079}
2080
Yan Zheng2b820322008-11-17 21:11:30 -05002081static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2082{
2083 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2084 struct btrfs_path *path;
2085 struct extent_buffer *leaf;
2086 struct btrfs_chunk *chunk;
2087 struct btrfs_key key;
2088 struct btrfs_key found_key;
2089 u64 chunk_tree = chunk_root->root_key.objectid;
2090 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002091 bool retried = false;
2092 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002093 int ret;
2094
2095 path = btrfs_alloc_path();
2096 if (!path)
2097 return -ENOMEM;
2098
Josef Bacikba1bf482009-09-11 16:11:19 -04002099again:
Yan Zheng2b820322008-11-17 21:11:30 -05002100 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2101 key.offset = (u64)-1;
2102 key.type = BTRFS_CHUNK_ITEM_KEY;
2103
2104 while (1) {
2105 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2106 if (ret < 0)
2107 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002108 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002109
2110 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2111 key.type);
2112 if (ret < 0)
2113 goto error;
2114 if (ret > 0)
2115 break;
2116
2117 leaf = path->nodes[0];
2118 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2119
2120 chunk = btrfs_item_ptr(leaf, path->slots[0],
2121 struct btrfs_chunk);
2122 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002123 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002124
2125 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2126 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2127 found_key.objectid,
2128 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002129 if (ret == -ENOSPC)
2130 failed++;
2131 else if (ret)
2132 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002133 }
2134
2135 if (found_key.offset == 0)
2136 break;
2137 key.offset = found_key.offset - 1;
2138 }
2139 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002140 if (failed && !retried) {
2141 failed = 0;
2142 retried = true;
2143 goto again;
2144 } else if (failed && retried) {
2145 WARN_ON(1);
2146 ret = -ENOSPC;
2147 }
Yan Zheng2b820322008-11-17 21:11:30 -05002148error:
2149 btrfs_free_path(path);
2150 return ret;
2151}
2152
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002153static int insert_balance_item(struct btrfs_root *root,
2154 struct btrfs_balance_control *bctl)
2155{
2156 struct btrfs_trans_handle *trans;
2157 struct btrfs_balance_item *item;
2158 struct btrfs_disk_balance_args disk_bargs;
2159 struct btrfs_path *path;
2160 struct extent_buffer *leaf;
2161 struct btrfs_key key;
2162 int ret, err;
2163
2164 path = btrfs_alloc_path();
2165 if (!path)
2166 return -ENOMEM;
2167
2168 trans = btrfs_start_transaction(root, 0);
2169 if (IS_ERR(trans)) {
2170 btrfs_free_path(path);
2171 return PTR_ERR(trans);
2172 }
2173
2174 key.objectid = BTRFS_BALANCE_OBJECTID;
2175 key.type = BTRFS_BALANCE_ITEM_KEY;
2176 key.offset = 0;
2177
2178 ret = btrfs_insert_empty_item(trans, root, path, &key,
2179 sizeof(*item));
2180 if (ret)
2181 goto out;
2182
2183 leaf = path->nodes[0];
2184 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2185
2186 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2187
2188 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2189 btrfs_set_balance_data(leaf, item, &disk_bargs);
2190 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2191 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2192 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2193 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2194
2195 btrfs_set_balance_flags(leaf, item, bctl->flags);
2196
2197 btrfs_mark_buffer_dirty(leaf);
2198out:
2199 btrfs_free_path(path);
2200 err = btrfs_commit_transaction(trans, root);
2201 if (err && !ret)
2202 ret = err;
2203 return ret;
2204}
2205
2206static int del_balance_item(struct btrfs_root *root)
2207{
2208 struct btrfs_trans_handle *trans;
2209 struct btrfs_path *path;
2210 struct btrfs_key key;
2211 int ret, err;
2212
2213 path = btrfs_alloc_path();
2214 if (!path)
2215 return -ENOMEM;
2216
2217 trans = btrfs_start_transaction(root, 0);
2218 if (IS_ERR(trans)) {
2219 btrfs_free_path(path);
2220 return PTR_ERR(trans);
2221 }
2222
2223 key.objectid = BTRFS_BALANCE_OBJECTID;
2224 key.type = BTRFS_BALANCE_ITEM_KEY;
2225 key.offset = 0;
2226
2227 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2228 if (ret < 0)
2229 goto out;
2230 if (ret > 0) {
2231 ret = -ENOENT;
2232 goto out;
2233 }
2234
2235 ret = btrfs_del_item(trans, root, path);
2236out:
2237 btrfs_free_path(path);
2238 err = btrfs_commit_transaction(trans, root);
2239 if (err && !ret)
2240 ret = err;
2241 return ret;
2242}
2243
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002244/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002245 * This is a heuristic used to reduce the number of chunks balanced on
2246 * resume after balance was interrupted.
2247 */
2248static void update_balance_args(struct btrfs_balance_control *bctl)
2249{
2250 /*
2251 * Turn on soft mode for chunk types that were being converted.
2252 */
2253 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2254 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2255 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2256 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2257 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2258 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2259
2260 /*
2261 * Turn on usage filter if is not already used. The idea is
2262 * that chunks that we have already balanced should be
2263 * reasonably full. Don't do it for chunks that are being
2264 * converted - that will keep us from relocating unconverted
2265 * (albeit full) chunks.
2266 */
2267 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2268 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2269 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2270 bctl->data.usage = 90;
2271 }
2272 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2273 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2274 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2275 bctl->sys.usage = 90;
2276 }
2277 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2278 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2279 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2280 bctl->meta.usage = 90;
2281 }
2282}
2283
2284/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002285 * Should be called with both balance and volume mutexes held to
2286 * serialize other volume operations (add_dev/rm_dev/resize) with
2287 * restriper. Same goes for unset_balance_control.
2288 */
2289static void set_balance_control(struct btrfs_balance_control *bctl)
2290{
2291 struct btrfs_fs_info *fs_info = bctl->fs_info;
2292
2293 BUG_ON(fs_info->balance_ctl);
2294
2295 spin_lock(&fs_info->balance_lock);
2296 fs_info->balance_ctl = bctl;
2297 spin_unlock(&fs_info->balance_lock);
2298}
2299
2300static void unset_balance_control(struct btrfs_fs_info *fs_info)
2301{
2302 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2303
2304 BUG_ON(!fs_info->balance_ctl);
2305
2306 spin_lock(&fs_info->balance_lock);
2307 fs_info->balance_ctl = NULL;
2308 spin_unlock(&fs_info->balance_lock);
2309
2310 kfree(bctl);
2311}
2312
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002313/*
2314 * Balance filters. Return 1 if chunk should be filtered out
2315 * (should not be balanced).
2316 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002317static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002318 struct btrfs_balance_args *bargs)
2319{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002320 chunk_type = chunk_to_extended(chunk_type) &
2321 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002322
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002323 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002324 return 0;
2325
2326 return 1;
2327}
2328
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002329static u64 div_factor_fine(u64 num, int factor)
2330{
2331 if (factor <= 0)
2332 return 0;
2333 if (factor >= 100)
2334 return num;
2335
2336 num *= factor;
2337 do_div(num, 100);
2338 return num;
2339}
2340
2341static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2342 struct btrfs_balance_args *bargs)
2343{
2344 struct btrfs_block_group_cache *cache;
2345 u64 chunk_used, user_thresh;
2346 int ret = 1;
2347
2348 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2349 chunk_used = btrfs_block_group_used(&cache->item);
2350
2351 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2352 if (chunk_used < user_thresh)
2353 ret = 0;
2354
2355 btrfs_put_block_group(cache);
2356 return ret;
2357}
2358
Ilya Dryomov409d4042012-01-16 22:04:47 +02002359static int chunk_devid_filter(struct extent_buffer *leaf,
2360 struct btrfs_chunk *chunk,
2361 struct btrfs_balance_args *bargs)
2362{
2363 struct btrfs_stripe *stripe;
2364 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2365 int i;
2366
2367 for (i = 0; i < num_stripes; i++) {
2368 stripe = btrfs_stripe_nr(chunk, i);
2369 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2370 return 0;
2371 }
2372
2373 return 1;
2374}
2375
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002376/* [pstart, pend) */
2377static int chunk_drange_filter(struct extent_buffer *leaf,
2378 struct btrfs_chunk *chunk,
2379 u64 chunk_offset,
2380 struct btrfs_balance_args *bargs)
2381{
2382 struct btrfs_stripe *stripe;
2383 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2384 u64 stripe_offset;
2385 u64 stripe_length;
2386 int factor;
2387 int i;
2388
2389 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2390 return 0;
2391
2392 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2393 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2394 factor = 2;
2395 else
2396 factor = 1;
2397 factor = num_stripes / factor;
2398
2399 for (i = 0; i < num_stripes; i++) {
2400 stripe = btrfs_stripe_nr(chunk, i);
2401 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2402 continue;
2403
2404 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2405 stripe_length = btrfs_chunk_length(leaf, chunk);
2406 do_div(stripe_length, factor);
2407
2408 if (stripe_offset < bargs->pend &&
2409 stripe_offset + stripe_length > bargs->pstart)
2410 return 0;
2411 }
2412
2413 return 1;
2414}
2415
Ilya Dryomovea671762012-01-16 22:04:48 +02002416/* [vstart, vend) */
2417static int chunk_vrange_filter(struct extent_buffer *leaf,
2418 struct btrfs_chunk *chunk,
2419 u64 chunk_offset,
2420 struct btrfs_balance_args *bargs)
2421{
2422 if (chunk_offset < bargs->vend &&
2423 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2424 /* at least part of the chunk is inside this vrange */
2425 return 0;
2426
2427 return 1;
2428}
2429
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002430static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002431 struct btrfs_balance_args *bargs)
2432{
2433 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2434 return 0;
2435
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002436 chunk_type = chunk_to_extended(chunk_type) &
2437 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002438
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002439 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002440 return 1;
2441
2442 return 0;
2443}
2444
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002445static int should_balance_chunk(struct btrfs_root *root,
2446 struct extent_buffer *leaf,
2447 struct btrfs_chunk *chunk, u64 chunk_offset)
2448{
2449 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2450 struct btrfs_balance_args *bargs = NULL;
2451 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2452
2453 /* type filter */
2454 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2455 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2456 return 0;
2457 }
2458
2459 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2460 bargs = &bctl->data;
2461 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2462 bargs = &bctl->sys;
2463 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2464 bargs = &bctl->meta;
2465
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002466 /* profiles filter */
2467 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2468 chunk_profiles_filter(chunk_type, bargs)) {
2469 return 0;
2470 }
2471
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002472 /* usage filter */
2473 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2474 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2475 return 0;
2476 }
2477
Ilya Dryomov409d4042012-01-16 22:04:47 +02002478 /* devid filter */
2479 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2480 chunk_devid_filter(leaf, chunk, bargs)) {
2481 return 0;
2482 }
2483
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002484 /* drange filter, makes sense only with devid filter */
2485 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2486 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2487 return 0;
2488 }
2489
Ilya Dryomovea671762012-01-16 22:04:48 +02002490 /* vrange filter */
2491 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2492 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2493 return 0;
2494 }
2495
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002496 /* soft profile changing mode */
2497 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2498 chunk_soft_convert_filter(chunk_type, bargs)) {
2499 return 0;
2500 }
2501
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002502 return 1;
2503}
2504
Chris Masonec44a352008-04-28 15:29:52 -04002505static u64 div_factor(u64 num, int factor)
2506{
2507 if (factor == 10)
2508 return num;
2509 num *= factor;
2510 do_div(num, 10);
2511 return num;
2512}
2513
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002514static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002515{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002516 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002517 struct btrfs_root *chunk_root = fs_info->chunk_root;
2518 struct btrfs_root *dev_root = fs_info->dev_root;
2519 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002520 struct btrfs_device *device;
2521 u64 old_size;
2522 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002523 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002524 struct btrfs_path *path;
2525 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002526 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002527 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002528 struct extent_buffer *leaf;
2529 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002530 int ret;
2531 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002532 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002533
Chris Masonec44a352008-04-28 15:29:52 -04002534 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002535 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002536 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002537 old_size = device->total_bytes;
2538 size_to_free = div_factor(old_size, 1);
2539 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002540 if (!device->writeable ||
2541 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002542 continue;
2543
2544 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002545 if (ret == -ENOSPC)
2546 break;
Chris Masonec44a352008-04-28 15:29:52 -04002547 BUG_ON(ret);
2548
Yan, Zhenga22285a2010-05-16 10:48:46 -04002549 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002550 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002551
2552 ret = btrfs_grow_device(trans, device, old_size);
2553 BUG_ON(ret);
2554
2555 btrfs_end_transaction(trans, dev_root);
2556 }
2557
2558 /* step two, relocate all the chunks */
2559 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002560 if (!path) {
2561 ret = -ENOMEM;
2562 goto error;
2563 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002564
2565 /* zero out stat counters */
2566 spin_lock(&fs_info->balance_lock);
2567 memset(&bctl->stat, 0, sizeof(bctl->stat));
2568 spin_unlock(&fs_info->balance_lock);
2569again:
Chris Masonec44a352008-04-28 15:29:52 -04002570 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2571 key.offset = (u64)-1;
2572 key.type = BTRFS_CHUNK_ITEM_KEY;
2573
Chris Masond3977122009-01-05 21:25:51 -05002574 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002575 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002576 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002577 ret = -ECANCELED;
2578 goto error;
2579 }
2580
Chris Masonec44a352008-04-28 15:29:52 -04002581 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2582 if (ret < 0)
2583 goto error;
2584
2585 /*
2586 * this shouldn't happen, it means the last relocate
2587 * failed
2588 */
2589 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002590 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002591
2592 ret = btrfs_previous_item(chunk_root, path, 0,
2593 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002594 if (ret) {
2595 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002596 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002597 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002598
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002599 leaf = path->nodes[0];
2600 slot = path->slots[0];
2601 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2602
Chris Masonec44a352008-04-28 15:29:52 -04002603 if (found_key.objectid != key.objectid)
2604 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002605
Chris Masonec44a352008-04-28 15:29:52 -04002606 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002607 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002608 break;
2609
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002610 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2611
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002612 if (!counting) {
2613 spin_lock(&fs_info->balance_lock);
2614 bctl->stat.considered++;
2615 spin_unlock(&fs_info->balance_lock);
2616 }
2617
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002618 ret = should_balance_chunk(chunk_root, leaf, chunk,
2619 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002620 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002621 if (!ret)
2622 goto loop;
2623
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002624 if (counting) {
2625 spin_lock(&fs_info->balance_lock);
2626 bctl->stat.expected++;
2627 spin_unlock(&fs_info->balance_lock);
2628 goto loop;
2629 }
2630
Chris Masonec44a352008-04-28 15:29:52 -04002631 ret = btrfs_relocate_chunk(chunk_root,
2632 chunk_root->root_key.objectid,
2633 found_key.objectid,
2634 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002635 if (ret && ret != -ENOSPC)
2636 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002637 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002638 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002639 } else {
2640 spin_lock(&fs_info->balance_lock);
2641 bctl->stat.completed++;
2642 spin_unlock(&fs_info->balance_lock);
2643 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002644loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002645 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002646 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002647
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002648 if (counting) {
2649 btrfs_release_path(path);
2650 counting = false;
2651 goto again;
2652 }
Chris Masonec44a352008-04-28 15:29:52 -04002653error:
2654 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002655 if (enospc_errors) {
2656 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2657 enospc_errors);
2658 if (!ret)
2659 ret = -ENOSPC;
2660 }
2661
Chris Masonec44a352008-04-28 15:29:52 -04002662 return ret;
2663}
2664
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002665/**
2666 * alloc_profile_is_valid - see if a given profile is valid and reduced
2667 * @flags: profile to validate
2668 * @extended: if true @flags is treated as an extended profile
2669 */
2670static int alloc_profile_is_valid(u64 flags, int extended)
2671{
2672 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2673 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2674
2675 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2676
2677 /* 1) check that all other bits are zeroed */
2678 if (flags & ~mask)
2679 return 0;
2680
2681 /* 2) see if profile is reduced */
2682 if (flags == 0)
2683 return !extended; /* "0" is valid for usual profiles */
2684
2685 /* true if exactly one bit set */
2686 return (flags & (flags - 1)) == 0;
2687}
2688
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002689static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2690{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002691 /* cancel requested || normal exit path */
2692 return atomic_read(&fs_info->balance_cancel_req) ||
2693 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2694 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002695}
2696
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002697static void __cancel_balance(struct btrfs_fs_info *fs_info)
2698{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002699 int ret;
2700
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002701 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002702 ret = del_balance_item(fs_info->tree_root);
2703 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002704}
2705
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002706void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002707 struct btrfs_ioctl_balance_args *bargs);
2708
2709/*
2710 * Should be called with both balance and volume mutexes held
2711 */
2712int btrfs_balance(struct btrfs_balance_control *bctl,
2713 struct btrfs_ioctl_balance_args *bargs)
2714{
2715 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002716 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002717 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002718 int ret;
2719
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002720 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002721 atomic_read(&fs_info->balance_pause_req) ||
2722 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002723 ret = -EINVAL;
2724 goto out;
2725 }
2726
Ilya Dryomove4837f82012-03-27 17:09:17 +03002727 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2728 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2729 mixed = 1;
2730
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002731 /*
2732 * In case of mixed groups both data and meta should be picked,
2733 * and identical options should be given for both of them.
2734 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002735 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2736 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002737 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2738 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2739 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2740 printk(KERN_ERR "btrfs: with mixed groups data and "
2741 "metadata balance options must be the same\n");
2742 ret = -EINVAL;
2743 goto out;
2744 }
2745 }
2746
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002747 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2748 if (fs_info->fs_devices->num_devices == 1)
2749 allowed |= BTRFS_BLOCK_GROUP_DUP;
2750 else if (fs_info->fs_devices->num_devices < 4)
2751 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2752 else
2753 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2754 BTRFS_BLOCK_GROUP_RAID10);
2755
Ilya Dryomov6728b192012-03-27 17:09:17 +03002756 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2757 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2758 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002759 printk(KERN_ERR "btrfs: unable to start balance with target "
2760 "data profile %llu\n",
2761 (unsigned long long)bctl->data.target);
2762 ret = -EINVAL;
2763 goto out;
2764 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002765 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2766 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2767 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002768 printk(KERN_ERR "btrfs: unable to start balance with target "
2769 "metadata profile %llu\n",
2770 (unsigned long long)bctl->meta.target);
2771 ret = -EINVAL;
2772 goto out;
2773 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002774 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2775 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2776 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002777 printk(KERN_ERR "btrfs: unable to start balance with target "
2778 "system profile %llu\n",
2779 (unsigned long long)bctl->sys.target);
2780 ret = -EINVAL;
2781 goto out;
2782 }
2783
Ilya Dryomove4837f82012-03-27 17:09:17 +03002784 /* allow dup'ed data chunks only in mixed mode */
2785 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002786 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002787 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2788 ret = -EINVAL;
2789 goto out;
2790 }
2791
2792 /* allow to reduce meta or sys integrity only if force set */
2793 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2794 BTRFS_BLOCK_GROUP_RAID10;
2795 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2796 (fs_info->avail_system_alloc_bits & allowed) &&
2797 !(bctl->sys.target & allowed)) ||
2798 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2799 (fs_info->avail_metadata_alloc_bits & allowed) &&
2800 !(bctl->meta.target & allowed))) {
2801 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2802 printk(KERN_INFO "btrfs: force reducing metadata "
2803 "integrity\n");
2804 } else {
2805 printk(KERN_ERR "btrfs: balance will reduce metadata "
2806 "integrity, use force if you want this\n");
2807 ret = -EINVAL;
2808 goto out;
2809 }
2810 }
2811
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002812 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002813 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002814 goto out;
2815
Ilya Dryomov59641012012-01-16 22:04:48 +02002816 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2817 BUG_ON(ret == -EEXIST);
2818 set_balance_control(bctl);
2819 } else {
2820 BUG_ON(ret != -EEXIST);
2821 spin_lock(&fs_info->balance_lock);
2822 update_balance_args(bctl);
2823 spin_unlock(&fs_info->balance_lock);
2824 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002825
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002826 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002827 mutex_unlock(&fs_info->balance_mutex);
2828
2829 ret = __btrfs_balance(fs_info);
2830
2831 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002832 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002833
2834 if (bargs) {
2835 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002836 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002837 }
2838
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002839 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2840 balance_need_close(fs_info)) {
2841 __cancel_balance(fs_info);
2842 }
2843
2844 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002845
2846 return ret;
2847out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002848 if (bctl->flags & BTRFS_BALANCE_RESUME)
2849 __cancel_balance(fs_info);
2850 else
2851 kfree(bctl);
2852 return ret;
2853}
2854
2855static int balance_kthread(void *data)
2856{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002857 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002858 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002859
2860 mutex_lock(&fs_info->volume_mutex);
2861 mutex_lock(&fs_info->balance_mutex);
2862
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002863 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002864 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002865 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002866 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002867
2868 mutex_unlock(&fs_info->balance_mutex);
2869 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002870
Ilya Dryomov59641012012-01-16 22:04:48 +02002871 return ret;
2872}
2873
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002874int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
2875{
2876 struct task_struct *tsk;
2877
2878 spin_lock(&fs_info->balance_lock);
2879 if (!fs_info->balance_ctl) {
2880 spin_unlock(&fs_info->balance_lock);
2881 return 0;
2882 }
2883 spin_unlock(&fs_info->balance_lock);
2884
2885 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2886 printk(KERN_INFO "btrfs: force skipping balance\n");
2887 return 0;
2888 }
2889
2890 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
2891 if (IS_ERR(tsk))
2892 return PTR_ERR(tsk);
2893
2894 return 0;
2895}
2896
Ilya Dryomov68310a52012-06-22 12:24:12 -06002897int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02002898{
Ilya Dryomov59641012012-01-16 22:04:48 +02002899 struct btrfs_balance_control *bctl;
2900 struct btrfs_balance_item *item;
2901 struct btrfs_disk_balance_args disk_bargs;
2902 struct btrfs_path *path;
2903 struct extent_buffer *leaf;
2904 struct btrfs_key key;
2905 int ret;
2906
2907 path = btrfs_alloc_path();
2908 if (!path)
2909 return -ENOMEM;
2910
Ilya Dryomov68310a52012-06-22 12:24:12 -06002911 key.objectid = BTRFS_BALANCE_OBJECTID;
2912 key.type = BTRFS_BALANCE_ITEM_KEY;
2913 key.offset = 0;
2914
2915 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2916 if (ret < 0)
2917 goto out;
2918 if (ret > 0) { /* ret = -ENOENT; */
2919 ret = 0;
2920 goto out;
2921 }
2922
Ilya Dryomov59641012012-01-16 22:04:48 +02002923 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2924 if (!bctl) {
2925 ret = -ENOMEM;
2926 goto out;
2927 }
2928
Ilya Dryomov59641012012-01-16 22:04:48 +02002929 leaf = path->nodes[0];
2930 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2931
Ilya Dryomov68310a52012-06-22 12:24:12 -06002932 bctl->fs_info = fs_info;
2933 bctl->flags = btrfs_balance_flags(leaf, item);
2934 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02002935
2936 btrfs_balance_data(leaf, item, &disk_bargs);
2937 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2938 btrfs_balance_meta(leaf, item, &disk_bargs);
2939 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2940 btrfs_balance_sys(leaf, item, &disk_bargs);
2941 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2942
Ilya Dryomov68310a52012-06-22 12:24:12 -06002943 mutex_lock(&fs_info->volume_mutex);
2944 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02002945
Ilya Dryomov68310a52012-06-22 12:24:12 -06002946 set_balance_control(bctl);
2947
2948 mutex_unlock(&fs_info->balance_mutex);
2949 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02002950out:
2951 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002952 return ret;
2953}
2954
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002955int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2956{
2957 int ret = 0;
2958
2959 mutex_lock(&fs_info->balance_mutex);
2960 if (!fs_info->balance_ctl) {
2961 mutex_unlock(&fs_info->balance_mutex);
2962 return -ENOTCONN;
2963 }
2964
2965 if (atomic_read(&fs_info->balance_running)) {
2966 atomic_inc(&fs_info->balance_pause_req);
2967 mutex_unlock(&fs_info->balance_mutex);
2968
2969 wait_event(fs_info->balance_wait_q,
2970 atomic_read(&fs_info->balance_running) == 0);
2971
2972 mutex_lock(&fs_info->balance_mutex);
2973 /* we are good with balance_ctl ripped off from under us */
2974 BUG_ON(atomic_read(&fs_info->balance_running));
2975 atomic_dec(&fs_info->balance_pause_req);
2976 } else {
2977 ret = -ENOTCONN;
2978 }
2979
2980 mutex_unlock(&fs_info->balance_mutex);
2981 return ret;
2982}
2983
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002984int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2985{
2986 mutex_lock(&fs_info->balance_mutex);
2987 if (!fs_info->balance_ctl) {
2988 mutex_unlock(&fs_info->balance_mutex);
2989 return -ENOTCONN;
2990 }
2991
2992 atomic_inc(&fs_info->balance_cancel_req);
2993 /*
2994 * if we are running just wait and return, balance item is
2995 * deleted in btrfs_balance in this case
2996 */
2997 if (atomic_read(&fs_info->balance_running)) {
2998 mutex_unlock(&fs_info->balance_mutex);
2999 wait_event(fs_info->balance_wait_q,
3000 atomic_read(&fs_info->balance_running) == 0);
3001 mutex_lock(&fs_info->balance_mutex);
3002 } else {
3003 /* __cancel_balance needs volume_mutex */
3004 mutex_unlock(&fs_info->balance_mutex);
3005 mutex_lock(&fs_info->volume_mutex);
3006 mutex_lock(&fs_info->balance_mutex);
3007
3008 if (fs_info->balance_ctl)
3009 __cancel_balance(fs_info);
3010
3011 mutex_unlock(&fs_info->volume_mutex);
3012 }
3013
3014 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3015 atomic_dec(&fs_info->balance_cancel_req);
3016 mutex_unlock(&fs_info->balance_mutex);
3017 return 0;
3018}
3019
Chris Mason8f18cf12008-04-25 16:53:30 -04003020/*
3021 * shrinking a device means finding all of the device extents past
3022 * the new size, and then following the back refs to the chunks.
3023 * The chunk relocation code actually frees the device extent
3024 */
3025int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3026{
3027 struct btrfs_trans_handle *trans;
3028 struct btrfs_root *root = device->dev_root;
3029 struct btrfs_dev_extent *dev_extent = NULL;
3030 struct btrfs_path *path;
3031 u64 length;
3032 u64 chunk_tree;
3033 u64 chunk_objectid;
3034 u64 chunk_offset;
3035 int ret;
3036 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003037 int failed = 0;
3038 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003039 struct extent_buffer *l;
3040 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003041 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003042 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003043 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003044 u64 diff = device->total_bytes - new_size;
3045
Yan Zheng2b820322008-11-17 21:11:30 -05003046 if (new_size >= device->total_bytes)
3047 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04003048
3049 path = btrfs_alloc_path();
3050 if (!path)
3051 return -ENOMEM;
3052
Chris Mason8f18cf12008-04-25 16:53:30 -04003053 path->reada = 2;
3054
Chris Mason7d9eb122008-07-08 14:19:17 -04003055 lock_chunks(root);
3056
Chris Mason8f18cf12008-04-25 16:53:30 -04003057 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003058 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003059 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003060 spin_lock(&root->fs_info->free_chunk_lock);
3061 root->fs_info->free_chunk_space -= diff;
3062 spin_unlock(&root->fs_info->free_chunk_lock);
3063 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003064 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003065
Josef Bacikba1bf482009-09-11 16:11:19 -04003066again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003067 key.objectid = device->devid;
3068 key.offset = (u64)-1;
3069 key.type = BTRFS_DEV_EXTENT_KEY;
3070
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003071 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003072 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3073 if (ret < 0)
3074 goto done;
3075
3076 ret = btrfs_previous_item(root, path, 0, key.type);
3077 if (ret < 0)
3078 goto done;
3079 if (ret) {
3080 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003081 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003082 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003083 }
3084
3085 l = path->nodes[0];
3086 slot = path->slots[0];
3087 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3088
Josef Bacikba1bf482009-09-11 16:11:19 -04003089 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003090 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003091 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003092 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003093
3094 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3095 length = btrfs_dev_extent_length(l, dev_extent);
3096
Josef Bacikba1bf482009-09-11 16:11:19 -04003097 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003098 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003099 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003100 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003101
3102 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3103 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3104 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003105 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003106
3107 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3108 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003109 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003110 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003111 if (ret == -ENOSPC)
3112 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003113 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003114
3115 if (failed && !retried) {
3116 failed = 0;
3117 retried = true;
3118 goto again;
3119 } else if (failed && retried) {
3120 ret = -ENOSPC;
3121 lock_chunks(root);
3122
3123 device->total_bytes = old_size;
3124 if (device->writeable)
3125 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003126 spin_lock(&root->fs_info->free_chunk_lock);
3127 root->fs_info->free_chunk_space += diff;
3128 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003129 unlock_chunks(root);
3130 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003131 }
3132
Chris Balld6397ba2009-04-27 07:29:03 -04003133 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003134 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003135 if (IS_ERR(trans)) {
3136 ret = PTR_ERR(trans);
3137 goto done;
3138 }
3139
Chris Balld6397ba2009-04-27 07:29:03 -04003140 lock_chunks(root);
3141
3142 device->disk_total_bytes = new_size;
3143 /* Now btrfs_update_device() will change the on-disk size. */
3144 ret = btrfs_update_device(trans, device);
3145 if (ret) {
3146 unlock_chunks(root);
3147 btrfs_end_transaction(trans, root);
3148 goto done;
3149 }
3150 WARN_ON(diff > old_total);
3151 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3152 unlock_chunks(root);
3153 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003154done:
3155 btrfs_free_path(path);
3156 return ret;
3157}
3158
Li Zefan125ccb02011-12-08 15:07:24 +08003159static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003160 struct btrfs_key *key,
3161 struct btrfs_chunk *chunk, int item_size)
3162{
David Sterba6c417612011-04-13 15:41:04 +02003163 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003164 struct btrfs_disk_key disk_key;
3165 u32 array_size;
3166 u8 *ptr;
3167
3168 array_size = btrfs_super_sys_array_size(super_copy);
3169 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3170 return -EFBIG;
3171
3172 ptr = super_copy->sys_chunk_array + array_size;
3173 btrfs_cpu_key_to_disk(&disk_key, key);
3174 memcpy(ptr, &disk_key, sizeof(disk_key));
3175 ptr += sizeof(disk_key);
3176 memcpy(ptr, chunk, item_size);
3177 item_size += sizeof(disk_key);
3178 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3179 return 0;
3180}
3181
Miao Xieb2117a32011-01-05 10:07:28 +00003182/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003183 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003184 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003185static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003186{
Arne Jansen73c5de02011-04-12 12:07:57 +02003187 const struct btrfs_device_info *di_a = a;
3188 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003189
Arne Jansen73c5de02011-04-12 12:07:57 +02003190 if (di_a->max_avail > di_b->max_avail)
3191 return -1;
3192 if (di_a->max_avail < di_b->max_avail)
3193 return 1;
3194 if (di_a->total_avail > di_b->total_avail)
3195 return -1;
3196 if (di_a->total_avail < di_b->total_avail)
3197 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003198 return 0;
3199}
3200
3201static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3202 struct btrfs_root *extent_root,
3203 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003204 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003205 u64 start, u64 type)
3206{
3207 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003208 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3209 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003210 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003211 struct extent_map_tree *em_tree;
3212 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003213 struct btrfs_device_info *devices_info = NULL;
3214 u64 total_avail;
3215 int num_stripes; /* total number of stripes to allocate */
3216 int sub_stripes; /* sub_stripes info for map */
3217 int dev_stripes; /* stripes per dev */
3218 int devs_max; /* max devs to use */
3219 int devs_min; /* min devs needed */
3220 int devs_increment; /* ndevs has to be a multiple of this */
3221 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003222 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003223 u64 max_stripe_size;
3224 u64 max_chunk_size;
3225 u64 stripe_size;
3226 u64 num_bytes;
3227 int ndevs;
3228 int i;
3229 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003230
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003231 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003232
Miao Xieb2117a32011-01-05 10:07:28 +00003233 if (list_empty(&fs_devices->alloc_list))
3234 return -ENOSPC;
3235
Arne Jansen73c5de02011-04-12 12:07:57 +02003236 sub_stripes = 1;
3237 dev_stripes = 1;
3238 devs_increment = 1;
3239 ncopies = 1;
3240 devs_max = 0; /* 0 == as many as possible */
3241 devs_min = 1;
3242
3243 /*
3244 * define the properties of each RAID type.
3245 * FIXME: move this to a global table and use it in all RAID
3246 * calculation code
3247 */
3248 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3249 dev_stripes = 2;
3250 ncopies = 2;
3251 devs_max = 1;
3252 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3253 devs_min = 2;
3254 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3255 devs_increment = 2;
3256 ncopies = 2;
3257 devs_max = 2;
3258 devs_min = 2;
3259 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3260 sub_stripes = 2;
3261 devs_increment = 2;
3262 ncopies = 2;
3263 devs_min = 4;
3264 } else {
3265 devs_max = 1;
3266 }
3267
3268 if (type & BTRFS_BLOCK_GROUP_DATA) {
3269 max_stripe_size = 1024 * 1024 * 1024;
3270 max_chunk_size = 10 * max_stripe_size;
3271 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003272 /* for larger filesystems, use larger metadata chunks */
3273 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3274 max_stripe_size = 1024 * 1024 * 1024;
3275 else
3276 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003277 max_chunk_size = max_stripe_size;
3278 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003279 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003280 max_chunk_size = 2 * max_stripe_size;
3281 } else {
3282 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3283 type);
3284 BUG_ON(1);
3285 }
3286
3287 /* we don't want a chunk larger than 10% of writeable space */
3288 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3289 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003290
3291 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3292 GFP_NOFS);
3293 if (!devices_info)
3294 return -ENOMEM;
3295
Arne Jansen73c5de02011-04-12 12:07:57 +02003296 cur = fs_devices->alloc_list.next;
3297
3298 /*
3299 * in the first pass through the devices list, we gather information
3300 * about the available holes on each device.
3301 */
3302 ndevs = 0;
3303 while (cur != &fs_devices->alloc_list) {
3304 struct btrfs_device *device;
3305 u64 max_avail;
3306 u64 dev_offset;
3307
3308 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3309
3310 cur = cur->next;
3311
3312 if (!device->writeable) {
3313 printk(KERN_ERR
3314 "btrfs: read-only device in alloc_list\n");
3315 WARN_ON(1);
3316 continue;
3317 }
3318
3319 if (!device->in_fs_metadata)
3320 continue;
3321
3322 if (device->total_bytes > device->bytes_used)
3323 total_avail = device->total_bytes - device->bytes_used;
3324 else
3325 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003326
3327 /* If there is no space on this device, skip it. */
3328 if (total_avail == 0)
3329 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003330
Li Zefan125ccb02011-12-08 15:07:24 +08003331 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003332 max_stripe_size * dev_stripes,
3333 &dev_offset, &max_avail);
3334 if (ret && ret != -ENOSPC)
3335 goto error;
3336
3337 if (ret == 0)
3338 max_avail = max_stripe_size * dev_stripes;
3339
3340 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3341 continue;
3342
3343 devices_info[ndevs].dev_offset = dev_offset;
3344 devices_info[ndevs].max_avail = max_avail;
3345 devices_info[ndevs].total_avail = total_avail;
3346 devices_info[ndevs].dev = device;
3347 ++ndevs;
3348 }
3349
3350 /*
3351 * now sort the devices by hole size / available space
3352 */
3353 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3354 btrfs_cmp_device_info, NULL);
3355
3356 /* round down to number of usable stripes */
3357 ndevs -= ndevs % devs_increment;
3358
3359 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3360 ret = -ENOSPC;
3361 goto error;
3362 }
3363
3364 if (devs_max && ndevs > devs_max)
3365 ndevs = devs_max;
3366 /*
3367 * the primary goal is to maximize the number of stripes, so use as many
3368 * devices as possible, even if the stripes are not maximum sized.
3369 */
3370 stripe_size = devices_info[ndevs-1].max_avail;
3371 num_stripes = ndevs * dev_stripes;
3372
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003373 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003374 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003375 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003376 }
3377
3378 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003379
3380 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003381 do_div(stripe_size, BTRFS_STRIPE_LEN);
3382 stripe_size *= BTRFS_STRIPE_LEN;
3383
Miao Xieb2117a32011-01-05 10:07:28 +00003384 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3385 if (!map) {
3386 ret = -ENOMEM;
3387 goto error;
3388 }
3389 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003390
Arne Jansen73c5de02011-04-12 12:07:57 +02003391 for (i = 0; i < ndevs; ++i) {
3392 for (j = 0; j < dev_stripes; ++j) {
3393 int s = i * dev_stripes + j;
3394 map->stripes[s].dev = devices_info[i].dev;
3395 map->stripes[s].physical = devices_info[i].dev_offset +
3396 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003397 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003398 }
Chris Mason593060d2008-03-25 16:50:33 -04003399 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003400 map->stripe_len = BTRFS_STRIPE_LEN;
3401 map->io_align = BTRFS_STRIPE_LEN;
3402 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003403 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003404 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003405
Yan Zheng2b820322008-11-17 21:11:30 -05003406 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003407 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003408
Arne Jansen73c5de02011-04-12 12:07:57 +02003409 *stripe_size_out = stripe_size;
3410 *num_bytes_out = num_bytes;
3411
3412 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003413
David Sterba172ddd62011-04-21 00:48:27 +02003414 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003415 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003416 ret = -ENOMEM;
3417 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003418 }
Chris Mason0b86a832008-03-24 15:01:56 -04003419 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003420 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003421 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003422 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003423 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003424
Chris Mason0b86a832008-03-24 15:01:56 -04003425 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003426 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003427 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003428 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003429 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003430 if (ret)
3431 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003432
3433 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3434 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003435 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003436 if (ret)
3437 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003438
Arne Jansen73c5de02011-04-12 12:07:57 +02003439 for (i = 0; i < map->num_stripes; ++i) {
3440 struct btrfs_device *device;
3441 u64 dev_offset;
3442
3443 device = map->stripes[i].dev;
3444 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003445
3446 ret = btrfs_alloc_dev_extent(trans, device,
3447 info->chunk_root->root_key.objectid,
3448 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003449 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003450 if (ret) {
3451 btrfs_abort_transaction(trans, extent_root, ret);
3452 goto error;
3453 }
Yan Zheng2b820322008-11-17 21:11:30 -05003454 }
3455
Miao Xieb2117a32011-01-05 10:07:28 +00003456 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003457 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003458
3459error:
3460 kfree(map);
3461 kfree(devices_info);
3462 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003463}
3464
3465static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3466 struct btrfs_root *extent_root,
3467 struct map_lookup *map, u64 chunk_offset,
3468 u64 chunk_size, u64 stripe_size)
3469{
3470 u64 dev_offset;
3471 struct btrfs_key key;
3472 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3473 struct btrfs_device *device;
3474 struct btrfs_chunk *chunk;
3475 struct btrfs_stripe *stripe;
3476 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3477 int index = 0;
3478 int ret;
3479
3480 chunk = kzalloc(item_size, GFP_NOFS);
3481 if (!chunk)
3482 return -ENOMEM;
3483
3484 index = 0;
3485 while (index < map->num_stripes) {
3486 device = map->stripes[index].dev;
3487 device->bytes_used += stripe_size;
3488 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003489 if (ret)
3490 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003491 index++;
3492 }
3493
Josef Bacik2bf64752011-09-26 17:12:22 -04003494 spin_lock(&extent_root->fs_info->free_chunk_lock);
3495 extent_root->fs_info->free_chunk_space -= (stripe_size *
3496 map->num_stripes);
3497 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3498
Yan Zheng2b820322008-11-17 21:11:30 -05003499 index = 0;
3500 stripe = &chunk->stripe;
3501 while (index < map->num_stripes) {
3502 device = map->stripes[index].dev;
3503 dev_offset = map->stripes[index].physical;
3504
3505 btrfs_set_stack_stripe_devid(stripe, device->devid);
3506 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3507 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3508 stripe++;
3509 index++;
3510 }
3511
3512 btrfs_set_stack_chunk_length(chunk, chunk_size);
3513 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3514 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3515 btrfs_set_stack_chunk_type(chunk, map->type);
3516 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3517 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3518 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3519 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3520 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3521
3522 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3523 key.type = BTRFS_CHUNK_ITEM_KEY;
3524 key.offset = chunk_offset;
3525
3526 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003527
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003528 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3529 /*
3530 * TODO: Cleanup of inserted chunk root in case of
3531 * failure.
3532 */
Li Zefan125ccb02011-12-08 15:07:24 +08003533 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003534 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003535 }
liubo1abe9b82011-03-24 11:18:59 +00003536
Mark Fasheh3acd3952011-09-08 17:40:01 -07003537out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003538 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003539 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003540}
3541
3542/*
3543 * Chunk allocation falls into two parts. The first part does works
3544 * that make the new allocated chunk useable, but not do any operation
3545 * that modifies the chunk tree. The second part does the works that
3546 * require modifying the chunk tree. This division is important for the
3547 * bootstrap process of adding storage to a seed btrfs.
3548 */
3549int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3550 struct btrfs_root *extent_root, u64 type)
3551{
3552 u64 chunk_offset;
3553 u64 chunk_size;
3554 u64 stripe_size;
3555 struct map_lookup *map;
3556 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3557 int ret;
3558
3559 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3560 &chunk_offset);
3561 if (ret)
3562 return ret;
3563
3564 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3565 &stripe_size, chunk_offset, type);
3566 if (ret)
3567 return ret;
3568
3569 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3570 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003571 if (ret)
3572 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003573 return 0;
3574}
3575
Chris Masond3977122009-01-05 21:25:51 -05003576static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003577 struct btrfs_root *root,
3578 struct btrfs_device *device)
3579{
3580 u64 chunk_offset;
3581 u64 sys_chunk_offset;
3582 u64 chunk_size;
3583 u64 sys_chunk_size;
3584 u64 stripe_size;
3585 u64 sys_stripe_size;
3586 u64 alloc_profile;
3587 struct map_lookup *map;
3588 struct map_lookup *sys_map;
3589 struct btrfs_fs_info *fs_info = root->fs_info;
3590 struct btrfs_root *extent_root = fs_info->extent_root;
3591 int ret;
3592
3593 ret = find_next_chunk(fs_info->chunk_root,
3594 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003595 if (ret)
3596 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003597
3598 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003599 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003600 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3601
3602 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3603 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003604 if (ret)
3605 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003606
3607 sys_chunk_offset = chunk_offset + chunk_size;
3608
3609 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003610 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003611 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3612
3613 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3614 &sys_chunk_size, &sys_stripe_size,
3615 sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003616 if (ret)
3617 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003618
3619 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003620 if (ret)
3621 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003622
3623 /*
3624 * Modifying chunk tree needs allocating new blocks from both
3625 * system block group and metadata block group. So we only can
3626 * do operations require modifying the chunk tree after both
3627 * block groups were created.
3628 */
3629 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3630 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003631 if (ret)
3632 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003633
3634 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3635 sys_chunk_offset, sys_chunk_size,
3636 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003637 if (ret)
3638 goto abort;
3639
Yan Zheng2b820322008-11-17 21:11:30 -05003640 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003641
3642abort:
3643 btrfs_abort_transaction(trans, root, ret);
3644 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003645}
3646
3647int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3648{
3649 struct extent_map *em;
3650 struct map_lookup *map;
3651 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3652 int readonly = 0;
3653 int i;
3654
Chris Mason890871b2009-09-02 16:24:52 -04003655 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003656 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003657 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003658 if (!em)
3659 return 1;
3660
Josef Bacikf48b9072010-01-27 02:07:59 +00003661 if (btrfs_test_opt(root, DEGRADED)) {
3662 free_extent_map(em);
3663 return 0;
3664 }
3665
Yan Zheng2b820322008-11-17 21:11:30 -05003666 map = (struct map_lookup *)em->bdev;
3667 for (i = 0; i < map->num_stripes; i++) {
3668 if (!map->stripes[i].dev->writeable) {
3669 readonly = 1;
3670 break;
3671 }
3672 }
3673 free_extent_map(em);
3674 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003675}
3676
3677void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3678{
David Sterbaa8067e02011-04-21 00:34:43 +02003679 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003680}
3681
3682void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3683{
3684 struct extent_map *em;
3685
Chris Masond3977122009-01-05 21:25:51 -05003686 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003687 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003688 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3689 if (em)
3690 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003691 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003692 if (!em)
3693 break;
3694 kfree(em->bdev);
3695 /* once for us */
3696 free_extent_map(em);
3697 /* once for the tree */
3698 free_extent_map(em);
3699 }
3700}
3701
Chris Masonf1885912008-04-09 16:28:12 -04003702int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3703{
3704 struct extent_map *em;
3705 struct map_lookup *map;
3706 struct extent_map_tree *em_tree = &map_tree->map_tree;
3707 int ret;
3708
Chris Mason890871b2009-09-02 16:24:52 -04003709 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003710 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003711 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003712 BUG_ON(!em);
3713
3714 BUG_ON(em->start > logical || em->start + em->len < logical);
3715 map = (struct map_lookup *)em->bdev;
3716 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3717 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003718 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3719 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003720 else
3721 ret = 1;
3722 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003723 return ret;
3724}
3725
Chris Masondfe25022008-05-13 13:46:40 -04003726static int find_live_mirror(struct map_lookup *map, int first, int num,
3727 int optimal)
3728{
3729 int i;
3730 if (map->stripes[optimal].dev->bdev)
3731 return optimal;
3732 for (i = first; i < first + num; i++) {
3733 if (map->stripes[i].dev->bdev)
3734 return i;
3735 }
3736 /* we couldn't find one that doesn't fail. Just return something
3737 * and the io error handling code will clean up eventually
3738 */
3739 return optimal;
3740}
3741
Chris Masonf2d8d742008-04-21 10:03:05 -04003742static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3743 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003744 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003745 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003746{
3747 struct extent_map *em;
3748 struct map_lookup *map;
3749 struct extent_map_tree *em_tree = &map_tree->map_tree;
3750 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003751 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003752 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003753 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003754 u64 stripe_nr_orig;
3755 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003756 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003757 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003758 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003759 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003760 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003761 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003762
Chris Mason890871b2009-09-02 16:24:52 -04003763 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003764 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003765 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003766
Chris Mason3b951512008-04-17 11:29:12 -04003767 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003768 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3769 (unsigned long long)logical,
3770 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003771 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003772 }
Chris Mason0b86a832008-03-24 15:01:56 -04003773
3774 BUG_ON(em->start > logical || em->start + em->len < logical);
3775 map = (struct map_lookup *)em->bdev;
3776 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003777
Chris Masonf1885912008-04-09 16:28:12 -04003778 if (mirror_num > map->num_stripes)
3779 mirror_num = 0;
3780
Chris Mason593060d2008-03-25 16:50:33 -04003781 stripe_nr = offset;
3782 /*
3783 * stripe_nr counts the total number of stripes we have to stride
3784 * to get to this block
3785 */
3786 do_div(stripe_nr, map->stripe_len);
3787
3788 stripe_offset = stripe_nr * map->stripe_len;
3789 BUG_ON(offset < stripe_offset);
3790
3791 /* stripe_offset is the offset of this block in its stripe*/
3792 stripe_offset = offset - stripe_offset;
3793
Li Dongyangfce3bb92011-03-24 10:24:26 +00003794 if (rw & REQ_DISCARD)
3795 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003796 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003797 /* we limit the length of each bio to what fits in a stripe */
3798 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003799 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003800 } else {
3801 *length = em->len - offset;
3802 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003803
Jan Schmidta1d3c472011-08-04 17:15:33 +02003804 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003805 goto out;
3806
Chris Masonf2d8d742008-04-21 10:03:05 -04003807 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003808 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003809 stripe_nr_orig = stripe_nr;
3810 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3811 (~(map->stripe_len - 1));
3812 do_div(stripe_nr_end, map->stripe_len);
3813 stripe_end_offset = stripe_nr_end * map->stripe_len -
3814 (offset + *length);
3815 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3816 if (rw & REQ_DISCARD)
3817 num_stripes = min_t(u64, map->num_stripes,
3818 stripe_nr_end - stripe_nr_orig);
3819 stripe_index = do_div(stripe_nr, map->num_stripes);
3820 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003821 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003822 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003823 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003824 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003825 else {
3826 stripe_index = find_live_mirror(map, 0,
3827 map->num_stripes,
3828 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003829 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003830 }
Chris Mason2fff7342008-04-29 14:12:09 -04003831
Chris Mason611f0e02008-04-03 16:29:03 -04003832 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003833 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003834 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003835 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003836 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003837 } else {
3838 mirror_num = 1;
3839 }
Chris Mason2fff7342008-04-29 14:12:09 -04003840
Chris Mason321aecc2008-04-16 10:49:51 -04003841 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3842 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003843
3844 stripe_index = do_div(stripe_nr, factor);
3845 stripe_index *= map->sub_stripes;
3846
Jens Axboe7eaceac2011-03-10 08:52:07 +01003847 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003848 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003849 else if (rw & REQ_DISCARD)
3850 num_stripes = min_t(u64, map->sub_stripes *
3851 (stripe_nr_end - stripe_nr_orig),
3852 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003853 else if (mirror_num)
3854 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003855 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04003856 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04003857 stripe_index = find_live_mirror(map, stripe_index,
3858 map->sub_stripes, stripe_index +
3859 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04003860 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003861 }
Chris Mason8790d502008-04-03 16:29:03 -04003862 } else {
3863 /*
3864 * after this do_div call, stripe_nr is the number of stripes
3865 * on this device we have to walk to find the data, and
3866 * stripe_index is the number of our device in the stripe array
3867 */
3868 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003869 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003870 }
Chris Mason593060d2008-03-25 16:50:33 -04003871 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003872
Li Zefande11cc12011-12-01 12:55:47 +08003873 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3874 if (!bbio) {
3875 ret = -ENOMEM;
3876 goto out;
3877 }
3878 atomic_set(&bbio->error, 0);
3879
Li Dongyangfce3bb92011-03-24 10:24:26 +00003880 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003881 int factor = 0;
3882 int sub_stripes = 0;
3883 u64 stripes_per_dev = 0;
3884 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003885 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003886
3887 if (map->type &
3888 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3889 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3890 sub_stripes = 1;
3891 else
3892 sub_stripes = map->sub_stripes;
3893
3894 factor = map->num_stripes / sub_stripes;
3895 stripes_per_dev = div_u64_rem(stripe_nr_end -
3896 stripe_nr_orig,
3897 factor,
3898 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04003899 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3900 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003901 }
3902
Li Dongyangfce3bb92011-03-24 10:24:26 +00003903 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003904 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003905 map->stripes[stripe_index].physical +
3906 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003907 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003908
Li Zefanec9ef7a2011-12-01 14:06:42 +08003909 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3910 BTRFS_BLOCK_GROUP_RAID10)) {
3911 bbio->stripes[i].length = stripes_per_dev *
3912 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003913
Li Zefanec9ef7a2011-12-01 14:06:42 +08003914 if (i / sub_stripes < remaining_stripes)
3915 bbio->stripes[i].length +=
3916 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003917
3918 /*
3919 * Special for the first stripe and
3920 * the last stripe:
3921 *
3922 * |-------|...|-------|
3923 * |----------|
3924 * off end_off
3925 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08003926 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003927 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003928 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003929
3930 if (stripe_index >= last_stripe &&
3931 stripe_index <= (last_stripe +
3932 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08003933 bbio->stripes[i].length -=
3934 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003935
Li Zefanec9ef7a2011-12-01 14:06:42 +08003936 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003937 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003938 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003939 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003940
3941 stripe_index++;
3942 if (stripe_index == map->num_stripes) {
3943 /* This could only happen for RAID0/10 */
3944 stripe_index = 0;
3945 stripe_nr++;
3946 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003947 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003948 } else {
3949 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003950 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003951 map->stripes[stripe_index].physical +
3952 stripe_offset +
3953 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003954 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003955 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003956 stripe_index++;
3957 }
Chris Mason593060d2008-03-25 16:50:33 -04003958 }
Li Zefande11cc12011-12-01 12:55:47 +08003959
3960 if (rw & REQ_WRITE) {
3961 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3962 BTRFS_BLOCK_GROUP_RAID10 |
3963 BTRFS_BLOCK_GROUP_DUP)) {
3964 max_errors = 1;
3965 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003966 }
Li Zefande11cc12011-12-01 12:55:47 +08003967
3968 *bbio_ret = bbio;
3969 bbio->num_stripes = num_stripes;
3970 bbio->max_errors = max_errors;
3971 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003972out:
Chris Mason0b86a832008-03-24 15:01:56 -04003973 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003974 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003975}
3976
Chris Masonf2d8d742008-04-21 10:03:05 -04003977int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3978 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003979 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003980{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003981 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003982 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003983}
3984
Yan Zhenga512bbf2008-12-08 16:46:26 -05003985int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3986 u64 chunk_start, u64 physical, u64 devid,
3987 u64 **logical, int *naddrs, int *stripe_len)
3988{
3989 struct extent_map_tree *em_tree = &map_tree->map_tree;
3990 struct extent_map *em;
3991 struct map_lookup *map;
3992 u64 *buf;
3993 u64 bytenr;
3994 u64 length;
3995 u64 stripe_nr;
3996 int i, j, nr = 0;
3997
Chris Mason890871b2009-09-02 16:24:52 -04003998 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003999 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004000 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004001
4002 BUG_ON(!em || em->start != chunk_start);
4003 map = (struct map_lookup *)em->bdev;
4004
4005 length = em->len;
4006 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4007 do_div(length, map->num_stripes / map->sub_stripes);
4008 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4009 do_div(length, map->num_stripes);
4010
4011 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004012 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004013
4014 for (i = 0; i < map->num_stripes; i++) {
4015 if (devid && map->stripes[i].dev->devid != devid)
4016 continue;
4017 if (map->stripes[i].physical > physical ||
4018 map->stripes[i].physical + length <= physical)
4019 continue;
4020
4021 stripe_nr = physical - map->stripes[i].physical;
4022 do_div(stripe_nr, map->stripe_len);
4023
4024 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4025 stripe_nr = stripe_nr * map->num_stripes + i;
4026 do_div(stripe_nr, map->sub_stripes);
4027 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4028 stripe_nr = stripe_nr * map->num_stripes + i;
4029 }
4030 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05004031 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004032 for (j = 0; j < nr; j++) {
4033 if (buf[j] == bytenr)
4034 break;
4035 }
Chris Mason934d3752008-12-08 16:43:10 -05004036 if (j == nr) {
4037 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004038 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004039 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004040 }
4041
Yan Zhenga512bbf2008-12-08 16:46:26 -05004042 *logical = buf;
4043 *naddrs = nr;
4044 *stripe_len = map->stripe_len;
4045
4046 free_extent_map(em);
4047 return 0;
4048}
4049
Stefan Behrens442a4f62012-05-25 16:06:08 +02004050static void *merge_stripe_index_into_bio_private(void *bi_private,
4051 unsigned int stripe_index)
4052{
4053 /*
4054 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4055 * at most 1.
4056 * The alternative solution (instead of stealing bits from the
4057 * pointer) would be to allocate an intermediate structure
4058 * that contains the old private pointer plus the stripe_index.
4059 */
4060 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4061 BUG_ON(stripe_index > 3);
4062 return (void *)(((uintptr_t)bi_private) | stripe_index);
4063}
4064
4065static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4066{
4067 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4068}
4069
4070static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4071{
4072 return (unsigned int)((uintptr_t)bi_private) & 3;
4073}
4074
Jan Schmidta1d3c472011-08-04 17:15:33 +02004075static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004076{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004077 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004078 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004079
Stefan Behrens442a4f62012-05-25 16:06:08 +02004080 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004081 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004082 if (err == -EIO || err == -EREMOTEIO) {
4083 unsigned int stripe_index =
4084 extract_stripe_index_from_bio_private(
4085 bio->bi_private);
4086 struct btrfs_device *dev;
4087
4088 BUG_ON(stripe_index >= bbio->num_stripes);
4089 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004090 if (dev->bdev) {
4091 if (bio->bi_rw & WRITE)
4092 btrfs_dev_stat_inc(dev,
4093 BTRFS_DEV_STAT_WRITE_ERRS);
4094 else
4095 btrfs_dev_stat_inc(dev,
4096 BTRFS_DEV_STAT_READ_ERRS);
4097 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4098 btrfs_dev_stat_inc(dev,
4099 BTRFS_DEV_STAT_FLUSH_ERRS);
4100 btrfs_dev_stat_print_on_error(dev);
4101 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004102 }
4103 }
Chris Mason8790d502008-04-03 16:29:03 -04004104
Jan Schmidta1d3c472011-08-04 17:15:33 +02004105 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004106 is_orig_bio = 1;
4107
Jan Schmidta1d3c472011-08-04 17:15:33 +02004108 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004109 if (!is_orig_bio) {
4110 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004111 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004112 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004113 bio->bi_private = bbio->private;
4114 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004115 bio->bi_bdev = (struct block_device *)
4116 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004117 /* only send an error to the higher layers if it is
4118 * beyond the tolerance of the multi-bio
4119 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004120 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004121 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004122 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004123 /*
4124 * this bio is actually up to date, we didn't
4125 * go over the max number of errors
4126 */
4127 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004128 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004129 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004130 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004131
4132 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004133 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004134 bio_put(bio);
4135 }
Chris Mason8790d502008-04-03 16:29:03 -04004136}
4137
Chris Mason8b712842008-06-11 16:50:36 -04004138struct async_sched {
4139 struct bio *bio;
4140 int rw;
4141 struct btrfs_fs_info *info;
4142 struct btrfs_work work;
4143};
4144
4145/*
4146 * see run_scheduled_bios for a description of why bios are collected for
4147 * async submit.
4148 *
4149 * This will add one bio to the pending list for a device and make sure
4150 * the work struct is scheduled.
4151 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004152static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004153 struct btrfs_device *device,
4154 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004155{
4156 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004157 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004158
4159 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004160 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004161 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004162 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004163 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004164 return;
Chris Mason8b712842008-06-11 16:50:36 -04004165 }
4166
4167 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004168 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004169 * higher layers. Otherwise, the async bio makes it appear we have
4170 * made progress against dirty pages when we've really just put it
4171 * on a queue for later
4172 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004173 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004174 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004175 bio->bi_next = NULL;
4176 bio->bi_rw |= rw;
4177
4178 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004179 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004180 pending_bios = &device->pending_sync_bios;
4181 else
4182 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004183
Chris Masonffbd5172009-04-20 15:50:09 -04004184 if (pending_bios->tail)
4185 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004186
Chris Masonffbd5172009-04-20 15:50:09 -04004187 pending_bios->tail = bio;
4188 if (!pending_bios->head)
4189 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004190 if (device->running_pending)
4191 should_queue = 0;
4192
4193 spin_unlock(&device->io_lock);
4194
4195 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004196 btrfs_queue_worker(&root->fs_info->submit_workers,
4197 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004198}
4199
Chris Masonf1885912008-04-09 16:28:12 -04004200int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004201 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004202{
4203 struct btrfs_mapping_tree *map_tree;
4204 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004205 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004206 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004207 u64 length = 0;
4208 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004209 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004210 int dev_nr = 0;
4211 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004212 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004213
Chris Masonf2d8d742008-04-21 10:03:05 -04004214 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004215 map_tree = &root->fs_info->mapping_tree;
4216 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004217
Jan Schmidta1d3c472011-08-04 17:15:33 +02004218 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004219 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004220 if (ret) /* -ENOMEM */
4221 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004222
Jan Schmidta1d3c472011-08-04 17:15:33 +02004223 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004224 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004225 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4226 "len %llu\n", (unsigned long long)logical,
4227 (unsigned long long)length,
4228 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004229 BUG();
4230 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004231
4232 bbio->orig_bio = first_bio;
4233 bbio->private = first_bio->bi_private;
4234 bbio->end_io = first_bio->bi_end_io;
4235 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004236
Chris Masond3977122009-01-05 21:25:51 -05004237 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004238 if (dev_nr < total_devs - 1) {
4239 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004240 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004241 } else {
4242 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004243 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004244 bio->bi_private = bbio;
Stefan Behrens442a4f62012-05-25 16:06:08 +02004245 bio->bi_private = merge_stripe_index_into_bio_private(
4246 bio->bi_private, (unsigned int)dev_nr);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004247 bio->bi_end_io = btrfs_end_bio;
4248 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4249 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004250 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Josef Bacik606686e2012-06-04 14:03:51 -04004251#ifdef DEBUG
4252 struct rcu_string *name;
4253
4254 rcu_read_lock();
4255 name = rcu_dereference(dev->name);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004256 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4257 "(%s id %llu), size=%u\n", rw,
4258 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
Josef Bacik606686e2012-06-04 14:03:51 -04004259 name->str, dev->devid, bio->bi_size);
4260 rcu_read_unlock();
4261#endif
Chris Masondfe25022008-05-13 13:46:40 -04004262 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004263 if (async_submit)
4264 schedule_bio(root, dev, rw, bio);
4265 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004266 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004267 } else {
4268 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4269 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004270 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004271 }
Chris Mason8790d502008-04-03 16:29:03 -04004272 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004273 }
Chris Mason0b86a832008-03-24 15:01:56 -04004274 return 0;
4275}
4276
Chris Masona4437552008-04-18 10:29:38 -04004277struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004278 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004279{
Yan Zheng2b820322008-11-17 21:11:30 -05004280 struct btrfs_device *device;
4281 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004282
Yan Zheng2b820322008-11-17 21:11:30 -05004283 cur_devices = root->fs_info->fs_devices;
4284 while (cur_devices) {
4285 if (!fsid ||
4286 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4287 device = __find_device(&cur_devices->devices,
4288 devid, uuid);
4289 if (device)
4290 return device;
4291 }
4292 cur_devices = cur_devices->seed;
4293 }
4294 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004295}
4296
Chris Masondfe25022008-05-13 13:46:40 -04004297static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4298 u64 devid, u8 *dev_uuid)
4299{
4300 struct btrfs_device *device;
4301 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4302
4303 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004304 if (!device)
4305 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004306 list_add(&device->dev_list,
4307 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004308 device->dev_root = root->fs_info->dev_root;
4309 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004310 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004311 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004312 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004313 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004314 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004315 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004316 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004317 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4318 return device;
4319}
4320
Chris Mason0b86a832008-03-24 15:01:56 -04004321static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4322 struct extent_buffer *leaf,
4323 struct btrfs_chunk *chunk)
4324{
4325 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4326 struct map_lookup *map;
4327 struct extent_map *em;
4328 u64 logical;
4329 u64 length;
4330 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004331 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004332 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004333 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004334 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004335
Chris Masone17cade2008-04-15 15:41:47 -04004336 logical = key->offset;
4337 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004338
Chris Mason890871b2009-09-02 16:24:52 -04004339 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004340 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004341 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004342
4343 /* already mapped? */
4344 if (em && em->start <= logical && em->start + em->len > logical) {
4345 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004346 return 0;
4347 } else if (em) {
4348 free_extent_map(em);
4349 }
Chris Mason0b86a832008-03-24 15:01:56 -04004350
David Sterba172ddd62011-04-21 00:48:27 +02004351 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004352 if (!em)
4353 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004354 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4355 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004356 if (!map) {
4357 free_extent_map(em);
4358 return -ENOMEM;
4359 }
4360
4361 em->bdev = (struct block_device *)map;
4362 em->start = logical;
4363 em->len = length;
4364 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004365 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004366
Chris Mason593060d2008-03-25 16:50:33 -04004367 map->num_stripes = num_stripes;
4368 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4369 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4370 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4371 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4372 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004373 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004374 for (i = 0; i < num_stripes; i++) {
4375 map->stripes[i].physical =
4376 btrfs_stripe_offset_nr(leaf, chunk, i);
4377 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004378 read_extent_buffer(leaf, uuid, (unsigned long)
4379 btrfs_stripe_dev_uuid_nr(chunk, i),
4380 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004381 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4382 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004383 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004384 kfree(map);
4385 free_extent_map(em);
4386 return -EIO;
4387 }
Chris Masondfe25022008-05-13 13:46:40 -04004388 if (!map->stripes[i].dev) {
4389 map->stripes[i].dev =
4390 add_missing_dev(root, devid, uuid);
4391 if (!map->stripes[i].dev) {
4392 kfree(map);
4393 free_extent_map(em);
4394 return -EIO;
4395 }
4396 }
4397 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004398 }
4399
Chris Mason890871b2009-09-02 16:24:52 -04004400 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004401 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004402 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004403 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004404 free_extent_map(em);
4405
4406 return 0;
4407}
4408
Jeff Mahoney143bede2012-03-01 14:56:26 +01004409static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004410 struct btrfs_dev_item *dev_item,
4411 struct btrfs_device *device)
4412{
4413 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004414
4415 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004416 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4417 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004418 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4419 device->type = btrfs_device_type(leaf, dev_item);
4420 device->io_align = btrfs_device_io_align(leaf, dev_item);
4421 device->io_width = btrfs_device_io_width(leaf, dev_item);
4422 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004423
4424 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004425 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004426}
4427
Yan Zheng2b820322008-11-17 21:11:30 -05004428static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4429{
4430 struct btrfs_fs_devices *fs_devices;
4431 int ret;
4432
Li Zefanb367e472011-12-07 11:38:24 +08004433 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004434
4435 fs_devices = root->fs_info->fs_devices->seed;
4436 while (fs_devices) {
4437 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4438 ret = 0;
4439 goto out;
4440 }
4441 fs_devices = fs_devices->seed;
4442 }
4443
4444 fs_devices = find_fsid(fsid);
4445 if (!fs_devices) {
4446 ret = -ENOENT;
4447 goto out;
4448 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004449
4450 fs_devices = clone_fs_devices(fs_devices);
4451 if (IS_ERR(fs_devices)) {
4452 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004453 goto out;
4454 }
4455
Christoph Hellwig97288f22008-12-02 06:36:09 -05004456 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004457 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004458 if (ret) {
4459 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004460 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004461 }
Yan Zheng2b820322008-11-17 21:11:30 -05004462
4463 if (!fs_devices->seeding) {
4464 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004465 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004466 ret = -EINVAL;
4467 goto out;
4468 }
4469
4470 fs_devices->seed = root->fs_info->fs_devices->seed;
4471 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004472out:
Yan Zheng2b820322008-11-17 21:11:30 -05004473 return ret;
4474}
4475
Chris Mason0d81ba52008-03-24 15:02:07 -04004476static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004477 struct extent_buffer *leaf,
4478 struct btrfs_dev_item *dev_item)
4479{
4480 struct btrfs_device *device;
4481 u64 devid;
4482 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004483 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004484 u8 dev_uuid[BTRFS_UUID_SIZE];
4485
Chris Mason0b86a832008-03-24 15:01:56 -04004486 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004487 read_extent_buffer(leaf, dev_uuid,
4488 (unsigned long)btrfs_device_uuid(dev_item),
4489 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004490 read_extent_buffer(leaf, fs_uuid,
4491 (unsigned long)btrfs_device_fsid(dev_item),
4492 BTRFS_UUID_SIZE);
4493
4494 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4495 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004496 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004497 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004498 }
4499
4500 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4501 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004502 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004503 return -EIO;
4504
4505 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004506 printk(KERN_WARNING "warning devid %llu missing\n",
4507 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004508 device = add_missing_dev(root, devid, dev_uuid);
4509 if (!device)
4510 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004511 } else if (!device->missing) {
4512 /*
4513 * this happens when a device that was properly setup
4514 * in the device info lists suddenly goes bad.
4515 * device->bdev is NULL, and so we have to set
4516 * device->missing to one here
4517 */
4518 root->fs_info->fs_devices->missing_devices++;
4519 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004520 }
4521 }
4522
4523 if (device->fs_devices != root->fs_info->fs_devices) {
4524 BUG_ON(device->writeable);
4525 if (device->generation !=
4526 btrfs_device_generation(leaf, dev_item))
4527 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004528 }
Chris Mason0b86a832008-03-24 15:01:56 -04004529
4530 fill_device_from_item(leaf, dev_item, device);
4531 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004532 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004533 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004534 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004535 spin_lock(&root->fs_info->free_chunk_lock);
4536 root->fs_info->free_chunk_space += device->total_bytes -
4537 device->bytes_used;
4538 spin_unlock(&root->fs_info->free_chunk_lock);
4539 }
Chris Mason0b86a832008-03-24 15:01:56 -04004540 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004541 return ret;
4542}
4543
Yan Zhenge4404d62008-12-12 10:03:26 -05004544int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004545{
David Sterba6c417612011-04-13 15:41:04 +02004546 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004547 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004548 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004549 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004550 u8 *ptr;
4551 unsigned long sb_ptr;
4552 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004553 u32 num_stripes;
4554 u32 array_size;
4555 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004556 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004557 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004558
Yan Zhenge4404d62008-12-12 10:03:26 -05004559 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004560 BTRFS_SUPER_INFO_SIZE);
4561 if (!sb)
4562 return -ENOMEM;
4563 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004564 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004565 /*
4566 * The sb extent buffer is artifical and just used to read the system array.
4567 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4568 * pages up-to-date when the page is larger: extent does not cover the
4569 * whole page and consequently check_page_uptodate does not find all
4570 * the page's extents up-to-date (the hole beyond sb),
4571 * write_extent_buffer then triggers a WARN_ON.
4572 *
4573 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4574 * but sb spans only this function. Add an explicit SetPageUptodate call
4575 * to silence the warning eg. on PowerPC 64.
4576 */
4577 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004578 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004579
Chris Masona061fc82008-05-07 11:43:44 -04004580 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004581 array_size = btrfs_super_sys_array_size(super_copy);
4582
Chris Mason0b86a832008-03-24 15:01:56 -04004583 ptr = super_copy->sys_chunk_array;
4584 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4585 cur = 0;
4586
4587 while (cur < array_size) {
4588 disk_key = (struct btrfs_disk_key *)ptr;
4589 btrfs_disk_key_to_cpu(&key, disk_key);
4590
Chris Masona061fc82008-05-07 11:43:44 -04004591 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004592 sb_ptr += len;
4593 cur += len;
4594
Chris Mason0d81ba52008-03-24 15:02:07 -04004595 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004596 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004597 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004598 if (ret)
4599 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004600 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4601 len = btrfs_chunk_item_size(num_stripes);
4602 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004603 ret = -EIO;
4604 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004605 }
4606 ptr += len;
4607 sb_ptr += len;
4608 cur += len;
4609 }
Chris Masona061fc82008-05-07 11:43:44 -04004610 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004611 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004612}
4613
Stefan Behrens442a4f62012-05-25 16:06:08 +02004614struct btrfs_device *btrfs_find_device_for_logical(struct btrfs_root *root,
4615 u64 logical, int mirror_num)
4616{
4617 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4618 int ret;
4619 u64 map_length = 0;
4620 struct btrfs_bio *bbio = NULL;
4621 struct btrfs_device *device;
4622
4623 BUG_ON(mirror_num == 0);
4624 ret = btrfs_map_block(map_tree, WRITE, logical, &map_length, &bbio,
4625 mirror_num);
4626 if (ret) {
4627 BUG_ON(bbio != NULL);
4628 return NULL;
4629 }
4630 BUG_ON(mirror_num != bbio->mirror_num);
4631 device = bbio->stripes[mirror_num - 1].dev;
4632 kfree(bbio);
4633 return device;
4634}
4635
Chris Mason0b86a832008-03-24 15:01:56 -04004636int btrfs_read_chunk_tree(struct btrfs_root *root)
4637{
4638 struct btrfs_path *path;
4639 struct extent_buffer *leaf;
4640 struct btrfs_key key;
4641 struct btrfs_key found_key;
4642 int ret;
4643 int slot;
4644
4645 root = root->fs_info->chunk_root;
4646
4647 path = btrfs_alloc_path();
4648 if (!path)
4649 return -ENOMEM;
4650
Li Zefanb367e472011-12-07 11:38:24 +08004651 mutex_lock(&uuid_mutex);
4652 lock_chunks(root);
4653
Chris Mason0b86a832008-03-24 15:01:56 -04004654 /* first we search for all of the device items, and then we
4655 * read in all of the chunk items. This way we can create chunk
4656 * mappings that reference all of the devices that are afound
4657 */
4658 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4659 key.offset = 0;
4660 key.type = 0;
4661again:
4662 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004663 if (ret < 0)
4664 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004665 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004666 leaf = path->nodes[0];
4667 slot = path->slots[0];
4668 if (slot >= btrfs_header_nritems(leaf)) {
4669 ret = btrfs_next_leaf(root, path);
4670 if (ret == 0)
4671 continue;
4672 if (ret < 0)
4673 goto error;
4674 break;
4675 }
4676 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4677 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4678 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4679 break;
4680 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4681 struct btrfs_dev_item *dev_item;
4682 dev_item = btrfs_item_ptr(leaf, slot,
4683 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004684 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004685 if (ret)
4686 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004687 }
4688 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4689 struct btrfs_chunk *chunk;
4690 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4691 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004692 if (ret)
4693 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004694 }
4695 path->slots[0]++;
4696 }
4697 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4698 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004699 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004700 goto again;
4701 }
Chris Mason0b86a832008-03-24 15:01:56 -04004702 ret = 0;
4703error:
Li Zefanb367e472011-12-07 11:38:24 +08004704 unlock_chunks(root);
4705 mutex_unlock(&uuid_mutex);
4706
Yan Zheng2b820322008-11-17 21:11:30 -05004707 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004708 return ret;
4709}
Stefan Behrens442a4f62012-05-25 16:06:08 +02004710
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004711static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
4712{
4713 int i;
4714
4715 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4716 btrfs_dev_stat_reset(dev, i);
4717}
4718
4719int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
4720{
4721 struct btrfs_key key;
4722 struct btrfs_key found_key;
4723 struct btrfs_root *dev_root = fs_info->dev_root;
4724 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4725 struct extent_buffer *eb;
4726 int slot;
4727 int ret = 0;
4728 struct btrfs_device *device;
4729 struct btrfs_path *path = NULL;
4730 int i;
4731
4732 path = btrfs_alloc_path();
4733 if (!path) {
4734 ret = -ENOMEM;
4735 goto out;
4736 }
4737
4738 mutex_lock(&fs_devices->device_list_mutex);
4739 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4740 int item_size;
4741 struct btrfs_dev_stats_item *ptr;
4742
4743 key.objectid = 0;
4744 key.type = BTRFS_DEV_STATS_KEY;
4745 key.offset = device->devid;
4746 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
4747 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004748 __btrfs_reset_dev_stats(device);
4749 device->dev_stats_valid = 1;
4750 btrfs_release_path(path);
4751 continue;
4752 }
4753 slot = path->slots[0];
4754 eb = path->nodes[0];
4755 btrfs_item_key_to_cpu(eb, &found_key, slot);
4756 item_size = btrfs_item_size_nr(eb, slot);
4757
4758 ptr = btrfs_item_ptr(eb, slot,
4759 struct btrfs_dev_stats_item);
4760
4761 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4762 if (item_size >= (1 + i) * sizeof(__le64))
4763 btrfs_dev_stat_set(device, i,
4764 btrfs_dev_stats_value(eb, ptr, i));
4765 else
4766 btrfs_dev_stat_reset(device, i);
4767 }
4768
4769 device->dev_stats_valid = 1;
4770 btrfs_dev_stat_print_on_load(device);
4771 btrfs_release_path(path);
4772 }
4773 mutex_unlock(&fs_devices->device_list_mutex);
4774
4775out:
4776 btrfs_free_path(path);
4777 return ret < 0 ? ret : 0;
4778}
4779
4780static int update_dev_stat_item(struct btrfs_trans_handle *trans,
4781 struct btrfs_root *dev_root,
4782 struct btrfs_device *device)
4783{
4784 struct btrfs_path *path;
4785 struct btrfs_key key;
4786 struct extent_buffer *eb;
4787 struct btrfs_dev_stats_item *ptr;
4788 int ret;
4789 int i;
4790
4791 key.objectid = 0;
4792 key.type = BTRFS_DEV_STATS_KEY;
4793 key.offset = device->devid;
4794
4795 path = btrfs_alloc_path();
4796 BUG_ON(!path);
4797 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
4798 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004799 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
4800 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004801 goto out;
4802 }
4803
4804 if (ret == 0 &&
4805 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
4806 /* need to delete old one and insert a new one */
4807 ret = btrfs_del_item(trans, dev_root, path);
4808 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004809 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
4810 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004811 goto out;
4812 }
4813 ret = 1;
4814 }
4815
4816 if (ret == 1) {
4817 /* need to insert a new item */
4818 btrfs_release_path(path);
4819 ret = btrfs_insert_empty_item(trans, dev_root, path,
4820 &key, sizeof(*ptr));
4821 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004822 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
4823 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004824 goto out;
4825 }
4826 }
4827
4828 eb = path->nodes[0];
4829 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
4830 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4831 btrfs_set_dev_stats_value(eb, ptr, i,
4832 btrfs_dev_stat_read(device, i));
4833 btrfs_mark_buffer_dirty(eb);
4834
4835out:
4836 btrfs_free_path(path);
4837 return ret;
4838}
4839
4840/*
4841 * called from commit_transaction. Writes all changed device stats to disk.
4842 */
4843int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
4844 struct btrfs_fs_info *fs_info)
4845{
4846 struct btrfs_root *dev_root = fs_info->dev_root;
4847 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4848 struct btrfs_device *device;
4849 int ret = 0;
4850
4851 mutex_lock(&fs_devices->device_list_mutex);
4852 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4853 if (!device->dev_stats_valid || !device->dev_stats_dirty)
4854 continue;
4855
4856 ret = update_dev_stat_item(trans, dev_root, device);
4857 if (!ret)
4858 device->dev_stats_dirty = 0;
4859 }
4860 mutex_unlock(&fs_devices->device_list_mutex);
4861
4862 return ret;
4863}
4864
Stefan Behrens442a4f62012-05-25 16:06:08 +02004865void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
4866{
4867 btrfs_dev_stat_inc(dev, index);
4868 btrfs_dev_stat_print_on_error(dev);
4869}
4870
4871void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
4872{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004873 if (!dev->dev_stats_valid)
4874 return;
Josef Bacik606686e2012-06-04 14:03:51 -04004875 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02004876 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04004877 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02004878 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4879 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4880 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4881 btrfs_dev_stat_read(dev,
4882 BTRFS_DEV_STAT_CORRUPTION_ERRS),
4883 btrfs_dev_stat_read(dev,
4884 BTRFS_DEV_STAT_GENERATION_ERRS));
4885}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004886
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004887static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
4888{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06004889 int i;
4890
4891 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4892 if (btrfs_dev_stat_read(dev, i) != 0)
4893 break;
4894 if (i == BTRFS_DEV_STAT_VALUES_MAX)
4895 return; /* all values == 0, suppress message */
4896
Josef Bacik606686e2012-06-04 14:03:51 -04004897 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
4898 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004899 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4900 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4901 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4902 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
4903 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
4904}
4905
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004906int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06004907 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004908{
4909 struct btrfs_device *dev;
4910 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4911 int i;
4912
4913 mutex_lock(&fs_devices->device_list_mutex);
4914 dev = btrfs_find_device(root, stats->devid, NULL, NULL);
4915 mutex_unlock(&fs_devices->device_list_mutex);
4916
4917 if (!dev) {
4918 printk(KERN_WARNING
4919 "btrfs: get dev_stats failed, device not found\n");
4920 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004921 } else if (!dev->dev_stats_valid) {
4922 printk(KERN_WARNING
4923 "btrfs: get dev_stats failed, not yet valid\n");
4924 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06004925 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004926 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4927 if (stats->nr_items > i)
4928 stats->values[i] =
4929 btrfs_dev_stat_read_and_reset(dev, i);
4930 else
4931 btrfs_dev_stat_reset(dev, i);
4932 }
4933 } else {
4934 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4935 if (stats->nr_items > i)
4936 stats->values[i] = btrfs_dev_stat_read(dev, i);
4937 }
4938 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
4939 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
4940 return 0;
4941}