blob: 029b903a4ae3797322e05090790b86c9e8596c43 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
Chris Mason593060d2008-03-25 16:50:33 -040028#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050029#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040030#include "ctree.h"
31#include "extent_map.h"
32#include "disk-io.h"
33#include "transaction.h"
34#include "print-tree.h"
35#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040036#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010037#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040038#include "rcu-string.h"
Chris Mason0b86a832008-03-24 15:01:56 -040039
Yan Zheng2b820322008-11-17 21:11:30 -050040static int init_first_rw_device(struct btrfs_trans_handle *trans,
41 struct btrfs_root *root,
42 struct btrfs_device *device);
43static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020044static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
45static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050046
Chris Mason8a4b83c2008-03-24 15:02:07 -040047static DEFINE_MUTEX(uuid_mutex);
48static LIST_HEAD(fs_uuids);
49
Chris Mason7d9eb122008-07-08 14:19:17 -040050static void lock_chunks(struct btrfs_root *root)
51{
Chris Mason7d9eb122008-07-08 14:19:17 -040052 mutex_lock(&root->fs_info->chunk_mutex);
53}
54
55static void unlock_chunks(struct btrfs_root *root)
56{
Chris Mason7d9eb122008-07-08 14:19:17 -040057 mutex_unlock(&root->fs_info->chunk_mutex);
58}
59
Yan Zhenge4404d62008-12-12 10:03:26 -050060static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
61{
62 struct btrfs_device *device;
63 WARN_ON(fs_devices->opened);
64 while (!list_empty(&fs_devices->devices)) {
65 device = list_entry(fs_devices->devices.next,
66 struct btrfs_device, dev_list);
67 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -040068 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -050069 kfree(device);
70 }
71 kfree(fs_devices);
72}
73
Jeff Mahoney143bede2012-03-01 14:56:26 +010074void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040075{
76 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040077
Yan Zheng2b820322008-11-17 21:11:30 -050078 while (!list_empty(&fs_uuids)) {
79 fs_devices = list_entry(fs_uuids.next,
80 struct btrfs_fs_devices, list);
81 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050082 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040083 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040084}
85
Chris Masona1b32a52008-09-05 16:09:51 -040086static noinline struct btrfs_device *__find_device(struct list_head *head,
87 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040088{
89 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040090
Qinghuang Fengc6e30872009-01-21 10:59:08 -050091 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040092 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040093 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040094 return dev;
Chris Masona4437552008-04-18 10:29:38 -040095 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040096 }
97 return NULL;
98}
99
Chris Masona1b32a52008-09-05 16:09:51 -0400100static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102 struct btrfs_fs_devices *fs_devices;
103
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500104 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400105 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
106 return fs_devices;
107 }
108 return NULL;
109}
110
Chris Masonffbd5172009-04-20 15:50:09 -0400111static void requeue_list(struct btrfs_pending_bios *pending_bios,
112 struct bio *head, struct bio *tail)
113{
114
115 struct bio *old_head;
116
117 old_head = pending_bios->head;
118 pending_bios->head = head;
119 if (pending_bios->tail)
120 tail->bi_next = old_head;
121 else
122 pending_bios->tail = tail;
123}
124
Chris Mason8b712842008-06-11 16:50:36 -0400125/*
126 * we try to collect pending bios for a device so we don't get a large
127 * number of procs sending bios down to the same device. This greatly
128 * improves the schedulers ability to collect and merge the bios.
129 *
130 * But, it also turns into a long list of bios to process and that is sure
131 * to eventually make the worker thread block. The solution here is to
132 * make some progress and then put this work struct back at the end of
133 * the list if the block device is congested. This way, multiple devices
134 * can make progress from a single worker thread.
135 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100136static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400137{
138 struct bio *pending;
139 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400140 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400141 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400142 struct bio *tail;
143 struct bio *cur;
144 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400145 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400146 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400147 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400148 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400149 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000150 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400151 struct blk_plug plug;
152
153 /*
154 * this function runs all the bios we've collected for
155 * a particular device. We don't want to wander off to
156 * another device without first sending all of these down.
157 * So, setup a plug here and finish it off before we return
158 */
159 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400160
Chris Masonbedf7622009-04-03 10:32:58 -0400161 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400162 fs_info = device->dev_root->fs_info;
163 limit = btrfs_async_submit_limit(fs_info);
164 limit = limit * 2 / 3;
165
Chris Mason8b712842008-06-11 16:50:36 -0400166loop:
167 spin_lock(&device->io_lock);
168
Chris Masona6837052009-02-04 09:19:41 -0500169loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400170 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400171
Chris Mason8b712842008-06-11 16:50:36 -0400172 /* take all the bios off the list at once and process them
173 * later on (without the lock held). But, remember the
174 * tail and other pointers so the bios can be properly reinserted
175 * into the list if we hit congestion
176 */
Chris Masond84275c2009-06-09 15:39:08 -0400177 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400178 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400179 force_reg = 1;
180 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400181 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400182 force_reg = 0;
183 }
Chris Masonffbd5172009-04-20 15:50:09 -0400184
185 pending = pending_bios->head;
186 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400187 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400188
189 /*
190 * if pending was null this time around, no bios need processing
191 * at all and we can stop. Otherwise it'll loop back up again
192 * and do an additional check so no bios are missed.
193 *
194 * device->running_pending is used to synchronize with the
195 * schedule_bio code.
196 */
Chris Masonffbd5172009-04-20 15:50:09 -0400197 if (device->pending_sync_bios.head == NULL &&
198 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400199 again = 0;
200 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400201 } else {
202 again = 1;
203 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400204 }
Chris Masonffbd5172009-04-20 15:50:09 -0400205
206 pending_bios->head = NULL;
207 pending_bios->tail = NULL;
208
Chris Mason8b712842008-06-11 16:50:36 -0400209 spin_unlock(&device->io_lock);
210
Chris Masond3977122009-01-05 21:25:51 -0500211 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400212
213 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400214 /* we want to work on both lists, but do more bios on the
215 * sync list than the regular list
216 */
217 if ((num_run > 32 &&
218 pending_bios != &device->pending_sync_bios &&
219 device->pending_sync_bios.head) ||
220 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
221 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400222 spin_lock(&device->io_lock);
223 requeue_list(pending_bios, pending, tail);
224 goto loop_lock;
225 }
226
Chris Mason8b712842008-06-11 16:50:36 -0400227 cur = pending;
228 pending = pending->bi_next;
229 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400230
Josef Bacik66657b32012-08-01 15:36:24 -0400231 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400232 waitqueue_active(&fs_info->async_submit_wait))
233 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400234
235 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400236
Chris Mason2ab1ba62011-08-04 14:28:36 -0400237 /*
238 * if we're doing the sync list, record that our
239 * plug has some sync requests on it
240 *
241 * If we're doing the regular list and there are
242 * sync requests sitting around, unplug before
243 * we add more
244 */
245 if (pending_bios == &device->pending_sync_bios) {
246 sync_pending = 1;
247 } else if (sync_pending) {
248 blk_finish_plug(&plug);
249 blk_start_plug(&plug);
250 sync_pending = 0;
251 }
252
Stefan Behrens21adbd52011-11-09 13:44:05 +0100253 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400254 num_run++;
255 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100256 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400257 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400258
259 /*
260 * we made progress, there is more work to do and the bdi
261 * is now congested. Back off and let other work structs
262 * run instead
263 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400264 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500265 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400266 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400267
Chris Masonb765ead2009-04-03 10:27:10 -0400268 ioc = current->io_context;
269
270 /*
271 * the main goal here is that we don't want to
272 * block if we're going to be able to submit
273 * more requests without blocking.
274 *
275 * This code does two great things, it pokes into
276 * the elevator code from a filesystem _and_
277 * it makes assumptions about how batching works.
278 */
279 if (ioc && ioc->nr_batch_requests > 0 &&
280 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
281 (last_waited == 0 ||
282 ioc->last_waited == last_waited)) {
283 /*
284 * we want to go through our batch of
285 * requests and stop. So, we copy out
286 * the ioc->last_waited time and test
287 * against it before looping
288 */
289 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100290 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400291 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400292 continue;
293 }
Chris Mason8b712842008-06-11 16:50:36 -0400294 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400295 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500296 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400297
298 spin_unlock(&device->io_lock);
299 btrfs_requeue_work(&device->work);
300 goto done;
301 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500302 /* unplug every 64 requests just for good measure */
303 if (batch_run % 64 == 0) {
304 blk_finish_plug(&plug);
305 blk_start_plug(&plug);
306 sync_pending = 0;
307 }
Chris Mason8b712842008-06-11 16:50:36 -0400308 }
Chris Masonffbd5172009-04-20 15:50:09 -0400309
Chris Mason51684082010-03-10 15:33:32 -0500310 cond_resched();
311 if (again)
312 goto loop;
313
314 spin_lock(&device->io_lock);
315 if (device->pending_bios.head || device->pending_sync_bios.head)
316 goto loop_lock;
317 spin_unlock(&device->io_lock);
318
Chris Mason8b712842008-06-11 16:50:36 -0400319done:
Chris Mason211588a2011-04-19 20:12:40 -0400320 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400321}
322
Christoph Hellwigb2950862008-12-02 09:54:17 -0500323static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400324{
325 struct btrfs_device *device;
326
327 device = container_of(work, struct btrfs_device, work);
328 run_scheduled_bios(device);
329}
330
Chris Masona1b32a52008-09-05 16:09:51 -0400331static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400332 struct btrfs_super_block *disk_super,
333 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
334{
335 struct btrfs_device *device;
336 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400337 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400338 u64 found_transid = btrfs_super_generation(disk_super);
339
340 fs_devices = find_fsid(disk_super->fsid);
341 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400342 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400343 if (!fs_devices)
344 return -ENOMEM;
345 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400346 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400347 list_add(&fs_devices->list, &fs_uuids);
348 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
349 fs_devices->latest_devid = devid;
350 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400351 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400352 device = NULL;
353 } else {
Chris Masona4437552008-04-18 10:29:38 -0400354 device = __find_device(&fs_devices->devices, devid,
355 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400356 }
357 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500358 if (fs_devices->opened)
359 return -EBUSY;
360
Chris Mason8a4b83c2008-03-24 15:02:07 -0400361 device = kzalloc(sizeof(*device), GFP_NOFS);
362 if (!device) {
363 /* we can safely leave the fs_devices entry around */
364 return -ENOMEM;
365 }
366 device->devid = devid;
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200367 device->dev_stats_valid = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400368 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400369 memcpy(device->uuid, disk_super->dev_item.uuid,
370 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400371 spin_lock_init(&device->io_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400372
373 name = rcu_string_strdup(path, GFP_NOFS);
374 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400375 kfree(device);
376 return -ENOMEM;
377 }
Josef Bacik606686e2012-06-04 14:03:51 -0400378 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -0500379 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400380
Arne Jansen90519d62011-05-23 14:30:00 +0200381 /* init readahead state */
382 spin_lock_init(&device->reada_lock);
383 device->reada_curr_zone = NULL;
384 atomic_set(&device->reada_in_flight, 0);
385 device->reada_next = 0;
386 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
387 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
388
Chris Masone5e9a522009-06-10 15:17:02 -0400389 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000390 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400391 mutex_unlock(&fs_devices->device_list_mutex);
392
Yan Zheng2b820322008-11-17 21:11:30 -0500393 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400394 fs_devices->num_devices++;
Josef Bacik606686e2012-06-04 14:03:51 -0400395 } else if (!device->name || strcmp(device->name->str, path)) {
396 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000397 if (!name)
398 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400399 rcu_string_free(device->name);
400 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500401 if (device->missing) {
402 fs_devices->missing_devices--;
403 device->missing = 0;
404 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400405 }
406
407 if (found_transid > fs_devices->latest_trans) {
408 fs_devices->latest_devid = devid;
409 fs_devices->latest_trans = found_transid;
410 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400411 *fs_devices_ret = fs_devices;
412 return 0;
413}
414
Yan Zhenge4404d62008-12-12 10:03:26 -0500415static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
416{
417 struct btrfs_fs_devices *fs_devices;
418 struct btrfs_device *device;
419 struct btrfs_device *orig_dev;
420
421 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
422 if (!fs_devices)
423 return ERR_PTR(-ENOMEM);
424
425 INIT_LIST_HEAD(&fs_devices->devices);
426 INIT_LIST_HEAD(&fs_devices->alloc_list);
427 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400428 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500429 fs_devices->latest_devid = orig->latest_devid;
430 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400431 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500432 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
433
Xiao Guangrong46224702011-04-20 10:08:47 +0000434 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500435 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400436 struct rcu_string *name;
437
Yan Zhenge4404d62008-12-12 10:03:26 -0500438 device = kzalloc(sizeof(*device), GFP_NOFS);
439 if (!device)
440 goto error;
441
Josef Bacik606686e2012-06-04 14:03:51 -0400442 /*
443 * This is ok to do without rcu read locked because we hold the
444 * uuid mutex so nothing we touch in here is going to disappear.
445 */
446 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
447 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400448 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500449 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400450 }
Josef Bacik606686e2012-06-04 14:03:51 -0400451 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500452
453 device->devid = orig_dev->devid;
454 device->work.func = pending_bios_fn;
455 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500456 spin_lock_init(&device->io_lock);
457 INIT_LIST_HEAD(&device->dev_list);
458 INIT_LIST_HEAD(&device->dev_alloc_list);
459
460 list_add(&device->dev_list, &fs_devices->devices);
461 device->fs_devices = fs_devices;
462 fs_devices->num_devices++;
463 }
464 return fs_devices;
465error:
466 free_fs_devices(fs_devices);
467 return ERR_PTR(-ENOMEM);
468}
469
Jeff Mahoney143bede2012-03-01 14:56:26 +0100470void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
Chris Masondfe25022008-05-13 13:46:40 -0400471{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500472 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400473
Chris Masona6b0d5c2012-02-20 20:53:43 -0500474 struct block_device *latest_bdev = NULL;
475 u64 latest_devid = 0;
476 u64 latest_transid = 0;
477
Chris Masondfe25022008-05-13 13:46:40 -0400478 mutex_lock(&uuid_mutex);
479again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000480 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500481 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500482 if (device->in_fs_metadata) {
483 if (!latest_transid ||
484 device->generation > latest_transid) {
485 latest_devid = device->devid;
486 latest_transid = device->generation;
487 latest_bdev = device->bdev;
488 }
Yan Zheng2b820322008-11-17 21:11:30 -0500489 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500490 }
Yan Zheng2b820322008-11-17 21:11:30 -0500491
492 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100493 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500494 device->bdev = NULL;
495 fs_devices->open_devices--;
496 }
497 if (device->writeable) {
498 list_del_init(&device->dev_alloc_list);
499 device->writeable = 0;
500 fs_devices->rw_devices--;
501 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500502 list_del_init(&device->dev_list);
503 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400504 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500505 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400506 }
Yan Zheng2b820322008-11-17 21:11:30 -0500507
508 if (fs_devices->seed) {
509 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500510 goto again;
511 }
512
Chris Masona6b0d5c2012-02-20 20:53:43 -0500513 fs_devices->latest_bdev = latest_bdev;
514 fs_devices->latest_devid = latest_devid;
515 fs_devices->latest_trans = latest_transid;
516
Chris Masondfe25022008-05-13 13:46:40 -0400517 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400518}
Chris Masona0af4692008-05-13 16:03:06 -0400519
Xiao Guangrong1f781602011-04-20 10:09:16 +0000520static void __free_device(struct work_struct *work)
521{
522 struct btrfs_device *device;
523
524 device = container_of(work, struct btrfs_device, rcu_work);
525
526 if (device->bdev)
527 blkdev_put(device->bdev, device->mode);
528
Josef Bacik606686e2012-06-04 14:03:51 -0400529 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000530 kfree(device);
531}
532
533static void free_device(struct rcu_head *head)
534{
535 struct btrfs_device *device;
536
537 device = container_of(head, struct btrfs_device, rcu);
538
539 INIT_WORK(&device->rcu_work, __free_device);
540 schedule_work(&device->rcu_work);
541}
542
Yan Zheng2b820322008-11-17 21:11:30 -0500543static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400544{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400545 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500546
Yan Zheng2b820322008-11-17 21:11:30 -0500547 if (--fs_devices->opened > 0)
548 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400549
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000550 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500551 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000552 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400553 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000554
555 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400556 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000557
Yan Zheng2b820322008-11-17 21:11:30 -0500558 if (device->writeable) {
559 list_del_init(&device->dev_alloc_list);
560 fs_devices->rw_devices--;
561 }
562
Josef Bacikd5e20032011-08-04 14:52:27 +0000563 if (device->can_discard)
564 fs_devices->num_can_discard--;
565
Xiao Guangrong1f781602011-04-20 10:09:16 +0000566 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100567 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000568 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400569
570 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400571 if (device->name) {
572 name = rcu_string_strdup(device->name->str, GFP_NOFS);
573 BUG_ON(device->name && !name); /* -ENOMEM */
574 rcu_assign_pointer(new_device->name, name);
575 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000576 new_device->bdev = NULL;
577 new_device->writeable = 0;
578 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000579 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000580 list_replace_rcu(&device->dev_list, &new_device->dev_list);
581
582 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400583 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000584 mutex_unlock(&fs_devices->device_list_mutex);
585
Yan Zhenge4404d62008-12-12 10:03:26 -0500586 WARN_ON(fs_devices->open_devices);
587 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500588 fs_devices->opened = 0;
589 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500590
Chris Mason8a4b83c2008-03-24 15:02:07 -0400591 return 0;
592}
593
Yan Zheng2b820322008-11-17 21:11:30 -0500594int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
595{
Yan Zhenge4404d62008-12-12 10:03:26 -0500596 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500597 int ret;
598
599 mutex_lock(&uuid_mutex);
600 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500601 if (!fs_devices->opened) {
602 seed_devices = fs_devices->seed;
603 fs_devices->seed = NULL;
604 }
Yan Zheng2b820322008-11-17 21:11:30 -0500605 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500606
607 while (seed_devices) {
608 fs_devices = seed_devices;
609 seed_devices = fs_devices->seed;
610 __btrfs_close_devices(fs_devices);
611 free_fs_devices(fs_devices);
612 }
Yan Zheng2b820322008-11-17 21:11:30 -0500613 return ret;
614}
615
Yan Zhenge4404d62008-12-12 10:03:26 -0500616static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
617 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400618{
Josef Bacikd5e20032011-08-04 14:52:27 +0000619 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400620 struct block_device *bdev;
621 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400622 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400623 struct block_device *latest_bdev = NULL;
624 struct buffer_head *bh;
625 struct btrfs_super_block *disk_super;
626 u64 latest_devid = 0;
627 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400628 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500629 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400630 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400631
Tejun Heod4d77622010-11-13 11:55:18 +0100632 flags |= FMODE_EXCL;
633
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500634 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400635 if (device->bdev)
636 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400637 if (!device->name)
638 continue;
639
Josef Bacik606686e2012-06-04 14:03:51 -0400640 bdev = blkdev_get_by_path(device->name->str, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400641 if (IS_ERR(bdev)) {
Daniel J Blueman48940662012-05-07 06:35:38 -0600642 printk(KERN_INFO "btrfs: open %s failed\n", device->name->str);
Chris Masona0af4692008-05-13 16:03:06 -0400643 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400644 }
Chris Mason3c4bb262012-03-27 18:56:56 -0400645 filemap_write_and_wait(bdev->bd_inode->i_mapping);
646 invalidate_bdev(bdev);
Chris Masona061fc82008-05-07 11:43:44 -0400647 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400648
Yan Zhenga512bbf2008-12-08 16:46:26 -0500649 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300650 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400651 goto error_close;
652
653 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000654 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400655 if (devid != device->devid)
656 goto error_brelse;
657
Yan Zheng2b820322008-11-17 21:11:30 -0500658 if (memcmp(device->uuid, disk_super->dev_item.uuid,
659 BTRFS_UUID_SIZE))
660 goto error_brelse;
661
662 device->generation = btrfs_super_generation(disk_super);
663 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400664 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500665 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400666 latest_bdev = bdev;
667 }
668
Yan Zheng2b820322008-11-17 21:11:30 -0500669 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
670 device->writeable = 0;
671 } else {
672 device->writeable = !bdev_read_only(bdev);
673 seeding = 0;
674 }
675
Josef Bacikd5e20032011-08-04 14:52:27 +0000676 q = bdev_get_queue(bdev);
677 if (blk_queue_discard(q)) {
678 device->can_discard = 1;
679 fs_devices->num_can_discard++;
680 }
681
Chris Mason8a4b83c2008-03-24 15:02:07 -0400682 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400683 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500684 device->mode = flags;
685
Chris Masonc2898112009-06-10 09:51:32 -0400686 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
687 fs_devices->rotating = 1;
688
Chris Masona0af4692008-05-13 16:03:06 -0400689 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500690 if (device->writeable) {
691 fs_devices->rw_devices++;
692 list_add(&device->dev_alloc_list,
693 &fs_devices->alloc_list);
694 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000695 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400696 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400697
Chris Masona0af4692008-05-13 16:03:06 -0400698error_brelse:
699 brelse(bh);
700error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100701 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400702error:
703 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400704 }
Chris Masona0af4692008-05-13 16:03:06 -0400705 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300706 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400707 goto out;
708 }
Yan Zheng2b820322008-11-17 21:11:30 -0500709 fs_devices->seeding = seeding;
710 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400711 fs_devices->latest_bdev = latest_bdev;
712 fs_devices->latest_devid = latest_devid;
713 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500714 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400715out:
Yan Zheng2b820322008-11-17 21:11:30 -0500716 return ret;
717}
718
719int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500720 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500721{
722 int ret;
723
724 mutex_lock(&uuid_mutex);
725 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500726 fs_devices->opened++;
727 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500728 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500729 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500730 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400731 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400732 return ret;
733}
734
Christoph Hellwig97288f22008-12-02 06:36:09 -0500735int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400736 struct btrfs_fs_devices **fs_devices_ret)
737{
738 struct btrfs_super_block *disk_super;
739 struct block_device *bdev;
740 struct buffer_head *bh;
741 int ret;
742 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400743 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400744 u64 total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400745
Tejun Heod4d77622010-11-13 11:55:18 +0100746 flags |= FMODE_EXCL;
747 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400748
749 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400750 ret = PTR_ERR(bdev);
751 goto error;
752 }
753
Al Viro10f63272011-11-17 15:05:22 -0500754 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400755 ret = set_blocksize(bdev, 4096);
756 if (ret)
757 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500758 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400759 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000760 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400761 goto error_close;
762 }
763 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000764 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400765 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400766 total_devices = btrfs_super_num_devices(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400767 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500768 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200769 else
770 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500771 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500772 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400773 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400774 if (!ret && fs_devices_ret)
775 (*fs_devices_ret)->total_devices = total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400776 brelse(bh);
777error_close:
Al Viro10f63272011-11-17 15:05:22 -0500778 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100779 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400780error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400781 return ret;
782}
Chris Mason0b86a832008-03-24 15:01:56 -0400783
Miao Xie6d07bce2011-01-05 10:07:31 +0000784/* helper to account the used device space in the range */
785int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
786 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400787{
788 struct btrfs_key key;
789 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000790 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500791 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000792 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400793 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000794 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400795 struct extent_buffer *l;
796
Miao Xie6d07bce2011-01-05 10:07:31 +0000797 *length = 0;
798
799 if (start >= device->total_bytes)
800 return 0;
801
Yan Zheng2b820322008-11-17 21:11:30 -0500802 path = btrfs_alloc_path();
803 if (!path)
804 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400805 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400806
Chris Mason0b86a832008-03-24 15:01:56 -0400807 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000808 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400809 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000810
811 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400812 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000813 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400814 if (ret > 0) {
815 ret = btrfs_previous_item(root, path, key.objectid, key.type);
816 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000817 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400818 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000819
Chris Mason0b86a832008-03-24 15:01:56 -0400820 while (1) {
821 l = path->nodes[0];
822 slot = path->slots[0];
823 if (slot >= btrfs_header_nritems(l)) {
824 ret = btrfs_next_leaf(root, path);
825 if (ret == 0)
826 continue;
827 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000828 goto out;
829
830 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400831 }
832 btrfs_item_key_to_cpu(l, &key, slot);
833
834 if (key.objectid < device->devid)
835 goto next;
836
837 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000838 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400839
Chris Masond3977122009-01-05 21:25:51 -0500840 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400841 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400842
Chris Mason0b86a832008-03-24 15:01:56 -0400843 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000844 extent_end = key.offset + btrfs_dev_extent_length(l,
845 dev_extent);
846 if (key.offset <= start && extent_end > end) {
847 *length = end - start + 1;
848 break;
849 } else if (key.offset <= start && extent_end > start)
850 *length += extent_end - start;
851 else if (key.offset > start && extent_end <= end)
852 *length += extent_end - key.offset;
853 else if (key.offset > start && key.offset <= end) {
854 *length += end - key.offset + 1;
855 break;
856 } else if (key.offset > end)
857 break;
858
859next:
860 path->slots[0]++;
861 }
862 ret = 0;
863out:
864 btrfs_free_path(path);
865 return ret;
866}
867
Chris Mason0b86a832008-03-24 15:01:56 -0400868/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000869 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000870 * @device: the device which we search the free space in
871 * @num_bytes: the size of the free space that we need
872 * @start: store the start of the free space.
873 * @len: the size of the free space. that we find, or the size of the max
874 * free space if we don't find suitable free space
875 *
Chris Mason0b86a832008-03-24 15:01:56 -0400876 * this uses a pretty simple search, the expectation is that it is
877 * called very infrequently and that a given device has a small number
878 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000879 *
880 * @start is used to store the start of the free space if we find. But if we
881 * don't find suitable free space, it will be used to store the start position
882 * of the max free space.
883 *
884 * @len is used to store the size of the free space that we find.
885 * But if we don't find suitable free space, it is used to store the size of
886 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400887 */
Li Zefan125ccb02011-12-08 15:07:24 +0800888int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000889 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400890{
891 struct btrfs_key key;
892 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000893 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400894 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000895 u64 hole_size;
896 u64 max_hole_start;
897 u64 max_hole_size;
898 u64 extent_end;
899 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400900 u64 search_end = device->total_bytes;
901 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000902 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400903 struct extent_buffer *l;
904
Chris Mason0b86a832008-03-24 15:01:56 -0400905 /* FIXME use last free of some kind */
906
907 /* we don't want to overwrite the superblock on the drive,
908 * so we make sure to start at an offset of at least 1MB
909 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200910 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400911
Miao Xie7bfc8372011-01-05 10:07:26 +0000912 max_hole_start = search_start;
913 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000914 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000915
916 if (search_start >= search_end) {
917 ret = -ENOSPC;
918 goto error;
919 }
920
921 path = btrfs_alloc_path();
922 if (!path) {
923 ret = -ENOMEM;
924 goto error;
925 }
926 path->reada = 2;
927
Chris Mason0b86a832008-03-24 15:01:56 -0400928 key.objectid = device->devid;
929 key.offset = search_start;
930 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000931
Li Zefan125ccb02011-12-08 15:07:24 +0800932 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400933 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000934 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400935 if (ret > 0) {
936 ret = btrfs_previous_item(root, path, key.objectid, key.type);
937 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000938 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400939 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000940
Chris Mason0b86a832008-03-24 15:01:56 -0400941 while (1) {
942 l = path->nodes[0];
943 slot = path->slots[0];
944 if (slot >= btrfs_header_nritems(l)) {
945 ret = btrfs_next_leaf(root, path);
946 if (ret == 0)
947 continue;
948 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000949 goto out;
950
951 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400952 }
953 btrfs_item_key_to_cpu(l, &key, slot);
954
955 if (key.objectid < device->devid)
956 goto next;
957
958 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000959 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400960
Chris Mason0b86a832008-03-24 15:01:56 -0400961 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
962 goto next;
963
Miao Xie7bfc8372011-01-05 10:07:26 +0000964 if (key.offset > search_start) {
965 hole_size = key.offset - search_start;
966
967 if (hole_size > max_hole_size) {
968 max_hole_start = search_start;
969 max_hole_size = hole_size;
970 }
971
972 /*
973 * If this free space is greater than which we need,
974 * it must be the max free space that we have found
975 * until now, so max_hole_start must point to the start
976 * of this free space and the length of this free space
977 * is stored in max_hole_size. Thus, we return
978 * max_hole_start and max_hole_size and go back to the
979 * caller.
980 */
981 if (hole_size >= num_bytes) {
982 ret = 0;
983 goto out;
984 }
985 }
986
Chris Mason0b86a832008-03-24 15:01:56 -0400987 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000988 extent_end = key.offset + btrfs_dev_extent_length(l,
989 dev_extent);
990 if (extent_end > search_start)
991 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400992next:
993 path->slots[0]++;
994 cond_resched();
995 }
Chris Mason0b86a832008-03-24 15:01:56 -0400996
liubo38c01b92011-08-02 02:39:03 +0000997 /*
998 * At this point, search_start should be the end of
999 * allocated dev extents, and when shrinking the device,
1000 * search_end may be smaller than search_start.
1001 */
1002 if (search_end > search_start)
1003 hole_size = search_end - search_start;
1004
Miao Xie7bfc8372011-01-05 10:07:26 +00001005 if (hole_size > max_hole_size) {
1006 max_hole_start = search_start;
1007 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001008 }
Chris Mason0b86a832008-03-24 15:01:56 -04001009
Miao Xie7bfc8372011-01-05 10:07:26 +00001010 /* See above. */
1011 if (hole_size < num_bytes)
1012 ret = -ENOSPC;
1013 else
1014 ret = 0;
1015
1016out:
Yan Zheng2b820322008-11-17 21:11:30 -05001017 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001018error:
1019 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001020 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001021 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001022 return ret;
1023}
1024
Christoph Hellwigb2950862008-12-02 09:54:17 -05001025static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001026 struct btrfs_device *device,
1027 u64 start)
1028{
1029 int ret;
1030 struct btrfs_path *path;
1031 struct btrfs_root *root = device->dev_root;
1032 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001033 struct btrfs_key found_key;
1034 struct extent_buffer *leaf = NULL;
1035 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001036
1037 path = btrfs_alloc_path();
1038 if (!path)
1039 return -ENOMEM;
1040
1041 key.objectid = device->devid;
1042 key.offset = start;
1043 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001044again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001045 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001046 if (ret > 0) {
1047 ret = btrfs_previous_item(root, path, key.objectid,
1048 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001049 if (ret)
1050 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001051 leaf = path->nodes[0];
1052 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1053 extent = btrfs_item_ptr(leaf, path->slots[0],
1054 struct btrfs_dev_extent);
1055 BUG_ON(found_key.offset > start || found_key.offset +
1056 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001057 key = found_key;
1058 btrfs_release_path(path);
1059 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001060 } else if (ret == 0) {
1061 leaf = path->nodes[0];
1062 extent = btrfs_item_ptr(leaf, path->slots[0],
1063 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001064 } else {
1065 btrfs_error(root->fs_info, ret, "Slot search failed");
1066 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001067 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001068
Josef Bacik2bf64752011-09-26 17:12:22 -04001069 if (device->bytes_used > 0) {
1070 u64 len = btrfs_dev_extent_length(leaf, extent);
1071 device->bytes_used -= len;
1072 spin_lock(&root->fs_info->free_chunk_lock);
1073 root->fs_info->free_chunk_space += len;
1074 spin_unlock(&root->fs_info->free_chunk_lock);
1075 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001076 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001077 if (ret) {
1078 btrfs_error(root->fs_info, ret,
1079 "Failed to remove dev extent item");
1080 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001081out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001082 btrfs_free_path(path);
1083 return ret;
1084}
1085
Yan Zheng2b820322008-11-17 21:11:30 -05001086int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001087 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001088 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001089 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001090{
1091 int ret;
1092 struct btrfs_path *path;
1093 struct btrfs_root *root = device->dev_root;
1094 struct btrfs_dev_extent *extent;
1095 struct extent_buffer *leaf;
1096 struct btrfs_key key;
1097
Chris Masondfe25022008-05-13 13:46:40 -04001098 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001099 path = btrfs_alloc_path();
1100 if (!path)
1101 return -ENOMEM;
1102
Chris Mason0b86a832008-03-24 15:01:56 -04001103 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001104 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001105 key.type = BTRFS_DEV_EXTENT_KEY;
1106 ret = btrfs_insert_empty_item(trans, root, path, &key,
1107 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001108 if (ret)
1109 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001110
1111 leaf = path->nodes[0];
1112 extent = btrfs_item_ptr(leaf, path->slots[0],
1113 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001114 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1115 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1116 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1117
1118 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1119 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1120 BTRFS_UUID_SIZE);
1121
Chris Mason0b86a832008-03-24 15:01:56 -04001122 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1123 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001124out:
Chris Mason0b86a832008-03-24 15:01:56 -04001125 btrfs_free_path(path);
1126 return ret;
1127}
1128
Chris Masona1b32a52008-09-05 16:09:51 -04001129static noinline int find_next_chunk(struct btrfs_root *root,
1130 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001131{
1132 struct btrfs_path *path;
1133 int ret;
1134 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001135 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001136 struct btrfs_key found_key;
1137
1138 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001139 if (!path)
1140 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001141
Chris Masone17cade2008-04-15 15:41:47 -04001142 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001143 key.offset = (u64)-1;
1144 key.type = BTRFS_CHUNK_ITEM_KEY;
1145
1146 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1147 if (ret < 0)
1148 goto error;
1149
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001150 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001151
1152 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1153 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001154 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001155 } else {
1156 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1157 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001158 if (found_key.objectid != objectid)
1159 *offset = 0;
1160 else {
1161 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1162 struct btrfs_chunk);
1163 *offset = found_key.offset +
1164 btrfs_chunk_length(path->nodes[0], chunk);
1165 }
Chris Mason0b86a832008-03-24 15:01:56 -04001166 }
1167 ret = 0;
1168error:
1169 btrfs_free_path(path);
1170 return ret;
1171}
1172
Yan Zheng2b820322008-11-17 21:11:30 -05001173static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001174{
1175 int ret;
1176 struct btrfs_key key;
1177 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001178 struct btrfs_path *path;
1179
1180 root = root->fs_info->chunk_root;
1181
1182 path = btrfs_alloc_path();
1183 if (!path)
1184 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001185
1186 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1187 key.type = BTRFS_DEV_ITEM_KEY;
1188 key.offset = (u64)-1;
1189
1190 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1191 if (ret < 0)
1192 goto error;
1193
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001194 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001195
1196 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1197 BTRFS_DEV_ITEM_KEY);
1198 if (ret) {
1199 *objectid = 1;
1200 } else {
1201 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1202 path->slots[0]);
1203 *objectid = found_key.offset + 1;
1204 }
1205 ret = 0;
1206error:
Yan Zheng2b820322008-11-17 21:11:30 -05001207 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001208 return ret;
1209}
1210
1211/*
1212 * the device information is stored in the chunk root
1213 * the btrfs_device struct should be fully filled in
1214 */
1215int btrfs_add_device(struct btrfs_trans_handle *trans,
1216 struct btrfs_root *root,
1217 struct btrfs_device *device)
1218{
1219 int ret;
1220 struct btrfs_path *path;
1221 struct btrfs_dev_item *dev_item;
1222 struct extent_buffer *leaf;
1223 struct btrfs_key key;
1224 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001225
1226 root = root->fs_info->chunk_root;
1227
1228 path = btrfs_alloc_path();
1229 if (!path)
1230 return -ENOMEM;
1231
Chris Mason0b86a832008-03-24 15:01:56 -04001232 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1233 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001234 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001235
1236 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001237 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001238 if (ret)
1239 goto out;
1240
1241 leaf = path->nodes[0];
1242 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1243
1244 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001245 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001246 btrfs_set_device_type(leaf, dev_item, device->type);
1247 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1248 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1249 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001250 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1251 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001252 btrfs_set_device_group(leaf, dev_item, 0);
1253 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1254 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001255 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001256
Chris Mason0b86a832008-03-24 15:01:56 -04001257 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001258 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001259 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1260 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001261 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001262
Yan Zheng2b820322008-11-17 21:11:30 -05001263 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001264out:
1265 btrfs_free_path(path);
1266 return ret;
1267}
Chris Mason8f18cf12008-04-25 16:53:30 -04001268
Chris Masona061fc82008-05-07 11:43:44 -04001269static int btrfs_rm_dev_item(struct btrfs_root *root,
1270 struct btrfs_device *device)
1271{
1272 int ret;
1273 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001274 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001275 struct btrfs_trans_handle *trans;
1276
1277 root = root->fs_info->chunk_root;
1278
1279 path = btrfs_alloc_path();
1280 if (!path)
1281 return -ENOMEM;
1282
Yan, Zhenga22285a2010-05-16 10:48:46 -04001283 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001284 if (IS_ERR(trans)) {
1285 btrfs_free_path(path);
1286 return PTR_ERR(trans);
1287 }
Chris Masona061fc82008-05-07 11:43:44 -04001288 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1289 key.type = BTRFS_DEV_ITEM_KEY;
1290 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001291 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001292
1293 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1294 if (ret < 0)
1295 goto out;
1296
1297 if (ret > 0) {
1298 ret = -ENOENT;
1299 goto out;
1300 }
1301
1302 ret = btrfs_del_item(trans, root, path);
1303 if (ret)
1304 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001305out:
1306 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001307 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001308 btrfs_commit_transaction(trans, root);
1309 return ret;
1310}
1311
1312int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1313{
1314 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001315 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001316 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001317 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001318 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001319 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001320 u64 all_avail;
1321 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001322 u64 num_devices;
1323 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001324 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001325 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001326
Chris Masona061fc82008-05-07 11:43:44 -04001327 mutex_lock(&uuid_mutex);
1328
1329 all_avail = root->fs_info->avail_data_alloc_bits |
1330 root->fs_info->avail_system_alloc_bits |
1331 root->fs_info->avail_metadata_alloc_bits;
1332
1333 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001334 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001335 printk(KERN_ERR "btrfs: unable to go below four devices "
1336 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001337 ret = -EINVAL;
1338 goto out;
1339 }
1340
1341 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001342 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001343 printk(KERN_ERR "btrfs: unable to go below two "
1344 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001345 ret = -EINVAL;
1346 goto out;
1347 }
1348
Chris Masondfe25022008-05-13 13:46:40 -04001349 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001350 struct list_head *devices;
1351 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001352
Chris Masondfe25022008-05-13 13:46:40 -04001353 device = NULL;
1354 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001355 /*
1356 * It is safe to read the devices since the volume_mutex
1357 * is held.
1358 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001359 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001360 if (tmp->in_fs_metadata && !tmp->bdev) {
1361 device = tmp;
1362 break;
1363 }
1364 }
1365 bdev = NULL;
1366 bh = NULL;
1367 disk_super = NULL;
1368 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001369 printk(KERN_ERR "btrfs: no missing devices found to "
1370 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001371 goto out;
1372 }
Chris Masondfe25022008-05-13 13:46:40 -04001373 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001374 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1375 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001376 if (IS_ERR(bdev)) {
1377 ret = PTR_ERR(bdev);
1378 goto out;
1379 }
1380
Yan Zheng2b820322008-11-17 21:11:30 -05001381 set_blocksize(bdev, 4096);
Chris Mason3c4bb262012-03-27 18:56:56 -04001382 invalidate_bdev(bdev);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001383 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001384 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001385 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001386 goto error_close;
1387 }
1388 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001389 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001390 dev_uuid = disk_super->dev_item.uuid;
1391 device = btrfs_find_device(root, devid, dev_uuid,
1392 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001393 if (!device) {
1394 ret = -ENOENT;
1395 goto error_brelse;
1396 }
Chris Masondfe25022008-05-13 13:46:40 -04001397 }
Yan Zheng2b820322008-11-17 21:11:30 -05001398
1399 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001400 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1401 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001402 ret = -EINVAL;
1403 goto error_brelse;
1404 }
1405
1406 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001407 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001408 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001409 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001410 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001411 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001412 }
Chris Masona061fc82008-05-07 11:43:44 -04001413
1414 ret = btrfs_shrink_device(device, 0);
1415 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001416 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001417
Chris Masona061fc82008-05-07 11:43:44 -04001418 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1419 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001420 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001421
Josef Bacik2bf64752011-09-26 17:12:22 -04001422 spin_lock(&root->fs_info->free_chunk_lock);
1423 root->fs_info->free_chunk_space = device->total_bytes -
1424 device->bytes_used;
1425 spin_unlock(&root->fs_info->free_chunk_lock);
1426
Yan Zheng2b820322008-11-17 21:11:30 -05001427 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001428 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001429
1430 /*
1431 * the device list mutex makes sure that we don't change
1432 * the device list while someone else is writing out all
1433 * the device supers.
1434 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001435
1436 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001437 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001438 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001439
Yan Zhenge4404d62008-12-12 10:03:26 -05001440 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001441 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001442
Chris Masoncd02dca2010-12-13 14:56:23 -05001443 if (device->missing)
1444 root->fs_info->fs_devices->missing_devices--;
1445
Yan Zheng2b820322008-11-17 21:11:30 -05001446 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1447 struct btrfs_device, dev_list);
1448 if (device->bdev == root->fs_info->sb->s_bdev)
1449 root->fs_info->sb->s_bdev = next_device->bdev;
1450 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1451 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1452
Xiao Guangrong1f781602011-04-20 10:09:16 +00001453 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001454 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001455
1456 call_rcu(&device->rcu, free_device);
1457 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001458
David Sterba6c417612011-04-13 15:41:04 +02001459 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1460 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001461
Xiao Guangrong1f781602011-04-20 10:09:16 +00001462 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001463 struct btrfs_fs_devices *fs_devices;
1464 fs_devices = root->fs_info->fs_devices;
1465 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001466 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001467 break;
1468 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001469 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001470 fs_devices->seed = cur_devices->seed;
1471 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001472 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001473 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001474 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001475 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001476 }
1477
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001478 root->fs_info->num_tolerated_disk_barrier_failures =
1479 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1480
Yan Zheng2b820322008-11-17 21:11:30 -05001481 /*
1482 * at this point, the device is zero sized. We want to
1483 * remove it from the devices list and zero out the old super
1484 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001485 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001486 /* make sure this device isn't detected as part of
1487 * the FS anymore
1488 */
1489 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1490 set_buffer_dirty(bh);
1491 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001492 }
Chris Masona061fc82008-05-07 11:43:44 -04001493
Chris Masona061fc82008-05-07 11:43:44 -04001494 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001495
1496error_brelse:
1497 brelse(bh);
1498error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001499 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001500 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001501out:
1502 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001503 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001504error_undo:
1505 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001506 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001507 list_add(&device->dev_alloc_list,
1508 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001509 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001510 root->fs_info->fs_devices->rw_devices++;
1511 }
1512 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001513}
1514
Yan Zheng2b820322008-11-17 21:11:30 -05001515/*
1516 * does all the dirty work required for changing file system's UUID.
1517 */
Li Zefan125ccb02011-12-08 15:07:24 +08001518static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001519{
1520 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1521 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001522 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001523 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001524 struct btrfs_device *device;
1525 u64 super_flags;
1526
1527 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001528 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001529 return -EINVAL;
1530
Yan Zhenge4404d62008-12-12 10:03:26 -05001531 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1532 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001533 return -ENOMEM;
1534
Yan Zhenge4404d62008-12-12 10:03:26 -05001535 old_devices = clone_fs_devices(fs_devices);
1536 if (IS_ERR(old_devices)) {
1537 kfree(seed_devices);
1538 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001539 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001540
Yan Zheng2b820322008-11-17 21:11:30 -05001541 list_add(&old_devices->list, &fs_uuids);
1542
Yan Zhenge4404d62008-12-12 10:03:26 -05001543 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1544 seed_devices->opened = 1;
1545 INIT_LIST_HEAD(&seed_devices->devices);
1546 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001547 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001548
1549 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001550 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1551 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001552 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1553
Yan Zhenge4404d62008-12-12 10:03:26 -05001554 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1555 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1556 device->fs_devices = seed_devices;
1557 }
1558
Yan Zheng2b820322008-11-17 21:11:30 -05001559 fs_devices->seeding = 0;
1560 fs_devices->num_devices = 0;
1561 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001562 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001563 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001564
1565 generate_random_uuid(fs_devices->fsid);
1566 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1567 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1568 super_flags = btrfs_super_flags(disk_super) &
1569 ~BTRFS_SUPER_FLAG_SEEDING;
1570 btrfs_set_super_flags(disk_super, super_flags);
1571
1572 return 0;
1573}
1574
1575/*
1576 * strore the expected generation for seed devices in device items.
1577 */
1578static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1579 struct btrfs_root *root)
1580{
1581 struct btrfs_path *path;
1582 struct extent_buffer *leaf;
1583 struct btrfs_dev_item *dev_item;
1584 struct btrfs_device *device;
1585 struct btrfs_key key;
1586 u8 fs_uuid[BTRFS_UUID_SIZE];
1587 u8 dev_uuid[BTRFS_UUID_SIZE];
1588 u64 devid;
1589 int ret;
1590
1591 path = btrfs_alloc_path();
1592 if (!path)
1593 return -ENOMEM;
1594
1595 root = root->fs_info->chunk_root;
1596 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1597 key.offset = 0;
1598 key.type = BTRFS_DEV_ITEM_KEY;
1599
1600 while (1) {
1601 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1602 if (ret < 0)
1603 goto error;
1604
1605 leaf = path->nodes[0];
1606next_slot:
1607 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1608 ret = btrfs_next_leaf(root, path);
1609 if (ret > 0)
1610 break;
1611 if (ret < 0)
1612 goto error;
1613 leaf = path->nodes[0];
1614 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001615 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001616 continue;
1617 }
1618
1619 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1620 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1621 key.type != BTRFS_DEV_ITEM_KEY)
1622 break;
1623
1624 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1625 struct btrfs_dev_item);
1626 devid = btrfs_device_id(leaf, dev_item);
1627 read_extent_buffer(leaf, dev_uuid,
1628 (unsigned long)btrfs_device_uuid(dev_item),
1629 BTRFS_UUID_SIZE);
1630 read_extent_buffer(leaf, fs_uuid,
1631 (unsigned long)btrfs_device_fsid(dev_item),
1632 BTRFS_UUID_SIZE);
1633 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001634 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001635
1636 if (device->fs_devices->seeding) {
1637 btrfs_set_device_generation(leaf, dev_item,
1638 device->generation);
1639 btrfs_mark_buffer_dirty(leaf);
1640 }
1641
1642 path->slots[0]++;
1643 goto next_slot;
1644 }
1645 ret = 0;
1646error:
1647 btrfs_free_path(path);
1648 return ret;
1649}
1650
Chris Mason788f20e2008-04-28 15:29:42 -04001651int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1652{
Josef Bacikd5e20032011-08-04 14:52:27 +00001653 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001654 struct btrfs_trans_handle *trans;
1655 struct btrfs_device *device;
1656 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001657 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001658 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001659 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001660 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001661 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001662 int ret = 0;
1663
Yan Zheng2b820322008-11-17 21:11:30 -05001664 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001665 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001666
Li Zefana5d16332011-12-07 20:08:40 -05001667 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001668 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001669 if (IS_ERR(bdev))
1670 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001671
Yan Zheng2b820322008-11-17 21:11:30 -05001672 if (root->fs_info->fs_devices->seeding) {
1673 seeding_dev = 1;
1674 down_write(&sb->s_umount);
1675 mutex_lock(&uuid_mutex);
1676 }
1677
Chris Mason8c8bee12008-09-29 11:19:10 -04001678 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001679
Chris Mason788f20e2008-04-28 15:29:42 -04001680 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001681 /*
1682 * we have the volume lock, so we don't need the extra
1683 * device list mutex while reading the list here.
1684 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001685 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001686 if (device->bdev == bdev) {
1687 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001688 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001689 }
1690 }
1691
1692 device = kzalloc(sizeof(*device), GFP_NOFS);
1693 if (!device) {
1694 /* we can safely leave the fs_devices entry around */
1695 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001696 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001697 }
1698
Josef Bacik606686e2012-06-04 14:03:51 -04001699 name = rcu_string_strdup(device_path, GFP_NOFS);
1700 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001701 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001702 ret = -ENOMEM;
1703 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001704 }
Josef Bacik606686e2012-06-04 14:03:51 -04001705 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001706
1707 ret = find_next_devid(root, &device->devid);
1708 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001709 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001710 kfree(device);
1711 goto error;
1712 }
1713
Yan, Zhenga22285a2010-05-16 10:48:46 -04001714 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001715 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001716 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001717 kfree(device);
1718 ret = PTR_ERR(trans);
1719 goto error;
1720 }
1721
Yan Zheng2b820322008-11-17 21:11:30 -05001722 lock_chunks(root);
1723
Josef Bacikd5e20032011-08-04 14:52:27 +00001724 q = bdev_get_queue(bdev);
1725 if (blk_queue_discard(q))
1726 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001727 device->writeable = 1;
1728 device->work.func = pending_bios_fn;
1729 generate_random_uuid(device->uuid);
1730 spin_lock_init(&device->io_lock);
1731 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001732 device->io_width = root->sectorsize;
1733 device->io_align = root->sectorsize;
1734 device->sector_size = root->sectorsize;
1735 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001736 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001737 device->dev_root = root->fs_info->dev_root;
1738 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001739 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001740 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001741 set_blocksize(device->bdev, 4096);
1742
Yan Zheng2b820322008-11-17 21:11:30 -05001743 if (seeding_dev) {
1744 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001745 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001746 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001747 }
1748
1749 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001750
Chris Masone5e9a522009-06-10 15:17:02 -04001751 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001752 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001753 list_add(&device->dev_alloc_list,
1754 &root->fs_info->fs_devices->alloc_list);
1755 root->fs_info->fs_devices->num_devices++;
1756 root->fs_info->fs_devices->open_devices++;
1757 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04001758 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001759 if (device->can_discard)
1760 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001761 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1762
Josef Bacik2bf64752011-09-26 17:12:22 -04001763 spin_lock(&root->fs_info->free_chunk_lock);
1764 root->fs_info->free_chunk_space += device->total_bytes;
1765 spin_unlock(&root->fs_info->free_chunk_lock);
1766
Chris Masonc2898112009-06-10 09:51:32 -04001767 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1768 root->fs_info->fs_devices->rotating = 1;
1769
David Sterba6c417612011-04-13 15:41:04 +02001770 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1771 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001772 total_bytes + device->total_bytes);
1773
David Sterba6c417612011-04-13 15:41:04 +02001774 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1775 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001776 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001777 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001778
Yan Zheng2b820322008-11-17 21:11:30 -05001779 if (seeding_dev) {
1780 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001781 if (ret) {
1782 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001783 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001784 }
Yan Zheng2b820322008-11-17 21:11:30 -05001785 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06001786 if (ret) {
1787 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001788 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001789 }
Yan Zheng2b820322008-11-17 21:11:30 -05001790 } else {
1791 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001792 if (ret) {
1793 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001794 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001795 }
Yan Zheng2b820322008-11-17 21:11:30 -05001796 }
1797
Chris Mason913d9522009-03-10 13:17:18 -04001798 /*
1799 * we've got more storage, clear any full flags on the space
1800 * infos
1801 */
1802 btrfs_clear_space_info_full(root->fs_info);
1803
Chris Mason7d9eb122008-07-08 14:19:17 -04001804 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001805 root->fs_info->num_tolerated_disk_barrier_failures =
1806 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001807 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001808
1809 if (seeding_dev) {
1810 mutex_unlock(&uuid_mutex);
1811 up_write(&sb->s_umount);
1812
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001813 if (ret) /* transaction commit */
1814 return ret;
1815
Yan Zheng2b820322008-11-17 21:11:30 -05001816 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001817 if (ret < 0)
1818 btrfs_error(root->fs_info, ret,
1819 "Failed to relocate sys chunks after "
1820 "device initialization. This can be fixed "
1821 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001822 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001823
Chris Mason788f20e2008-04-28 15:29:42 -04001824 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001825
1826error_trans:
1827 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001828 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04001829 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001830 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001831error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001832 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001833 if (seeding_dev) {
1834 mutex_unlock(&uuid_mutex);
1835 up_write(&sb->s_umount);
1836 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001837 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001838}
1839
Chris Masond3977122009-01-05 21:25:51 -05001840static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1841 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001842{
1843 int ret;
1844 struct btrfs_path *path;
1845 struct btrfs_root *root;
1846 struct btrfs_dev_item *dev_item;
1847 struct extent_buffer *leaf;
1848 struct btrfs_key key;
1849
1850 root = device->dev_root->fs_info->chunk_root;
1851
1852 path = btrfs_alloc_path();
1853 if (!path)
1854 return -ENOMEM;
1855
1856 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1857 key.type = BTRFS_DEV_ITEM_KEY;
1858 key.offset = device->devid;
1859
1860 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1861 if (ret < 0)
1862 goto out;
1863
1864 if (ret > 0) {
1865 ret = -ENOENT;
1866 goto out;
1867 }
1868
1869 leaf = path->nodes[0];
1870 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1871
1872 btrfs_set_device_id(leaf, dev_item, device->devid);
1873 btrfs_set_device_type(leaf, dev_item, device->type);
1874 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1875 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1876 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001877 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001878 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1879 btrfs_mark_buffer_dirty(leaf);
1880
1881out:
1882 btrfs_free_path(path);
1883 return ret;
1884}
1885
Chris Mason7d9eb122008-07-08 14:19:17 -04001886static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001887 struct btrfs_device *device, u64 new_size)
1888{
1889 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001890 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001891 u64 old_total = btrfs_super_total_bytes(super_copy);
1892 u64 diff = new_size - device->total_bytes;
1893
Yan Zheng2b820322008-11-17 21:11:30 -05001894 if (!device->writeable)
1895 return -EACCES;
1896 if (new_size <= device->total_bytes)
1897 return -EINVAL;
1898
Chris Mason8f18cf12008-04-25 16:53:30 -04001899 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001900 device->fs_devices->total_rw_bytes += diff;
1901
1902 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001903 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001904 btrfs_clear_space_info_full(device->dev_root->fs_info);
1905
Chris Mason8f18cf12008-04-25 16:53:30 -04001906 return btrfs_update_device(trans, device);
1907}
1908
Chris Mason7d9eb122008-07-08 14:19:17 -04001909int btrfs_grow_device(struct btrfs_trans_handle *trans,
1910 struct btrfs_device *device, u64 new_size)
1911{
1912 int ret;
1913 lock_chunks(device->dev_root);
1914 ret = __btrfs_grow_device(trans, device, new_size);
1915 unlock_chunks(device->dev_root);
1916 return ret;
1917}
1918
Chris Mason8f18cf12008-04-25 16:53:30 -04001919static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1920 struct btrfs_root *root,
1921 u64 chunk_tree, u64 chunk_objectid,
1922 u64 chunk_offset)
1923{
1924 int ret;
1925 struct btrfs_path *path;
1926 struct btrfs_key key;
1927
1928 root = root->fs_info->chunk_root;
1929 path = btrfs_alloc_path();
1930 if (!path)
1931 return -ENOMEM;
1932
1933 key.objectid = chunk_objectid;
1934 key.offset = chunk_offset;
1935 key.type = BTRFS_CHUNK_ITEM_KEY;
1936
1937 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001938 if (ret < 0)
1939 goto out;
1940 else if (ret > 0) { /* Logic error or corruption */
1941 btrfs_error(root->fs_info, -ENOENT,
1942 "Failed lookup while freeing chunk.");
1943 ret = -ENOENT;
1944 goto out;
1945 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001946
1947 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001948 if (ret < 0)
1949 btrfs_error(root->fs_info, ret,
1950 "Failed to delete chunk item.");
1951out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001952 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001953 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001954}
1955
Christoph Hellwigb2950862008-12-02 09:54:17 -05001956static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001957 chunk_offset)
1958{
David Sterba6c417612011-04-13 15:41:04 +02001959 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001960 struct btrfs_disk_key *disk_key;
1961 struct btrfs_chunk *chunk;
1962 u8 *ptr;
1963 int ret = 0;
1964 u32 num_stripes;
1965 u32 array_size;
1966 u32 len = 0;
1967 u32 cur;
1968 struct btrfs_key key;
1969
1970 array_size = btrfs_super_sys_array_size(super_copy);
1971
1972 ptr = super_copy->sys_chunk_array;
1973 cur = 0;
1974
1975 while (cur < array_size) {
1976 disk_key = (struct btrfs_disk_key *)ptr;
1977 btrfs_disk_key_to_cpu(&key, disk_key);
1978
1979 len = sizeof(*disk_key);
1980
1981 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1982 chunk = (struct btrfs_chunk *)(ptr + len);
1983 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1984 len += btrfs_chunk_item_size(num_stripes);
1985 } else {
1986 ret = -EIO;
1987 break;
1988 }
1989 if (key.objectid == chunk_objectid &&
1990 key.offset == chunk_offset) {
1991 memmove(ptr, ptr + len, array_size - (cur + len));
1992 array_size -= len;
1993 btrfs_set_super_sys_array_size(super_copy, array_size);
1994 } else {
1995 ptr += len;
1996 cur += len;
1997 }
1998 }
1999 return ret;
2000}
2001
Christoph Hellwigb2950862008-12-02 09:54:17 -05002002static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002003 u64 chunk_tree, u64 chunk_objectid,
2004 u64 chunk_offset)
2005{
2006 struct extent_map_tree *em_tree;
2007 struct btrfs_root *extent_root;
2008 struct btrfs_trans_handle *trans;
2009 struct extent_map *em;
2010 struct map_lookup *map;
2011 int ret;
2012 int i;
2013
2014 root = root->fs_info->chunk_root;
2015 extent_root = root->fs_info->extent_root;
2016 em_tree = &root->fs_info->mapping_tree.map_tree;
2017
Josef Bacikba1bf482009-09-11 16:11:19 -04002018 ret = btrfs_can_relocate(extent_root, chunk_offset);
2019 if (ret)
2020 return -ENOSPC;
2021
Chris Mason8f18cf12008-04-25 16:53:30 -04002022 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002023 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002024 if (ret)
2025 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002026
Yan, Zhenga22285a2010-05-16 10:48:46 -04002027 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002028 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002029
Chris Mason7d9eb122008-07-08 14:19:17 -04002030 lock_chunks(root);
2031
Chris Mason8f18cf12008-04-25 16:53:30 -04002032 /*
2033 * step two, delete the device extents and the
2034 * chunk tree entries
2035 */
Chris Mason890871b2009-09-02 16:24:52 -04002036 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002037 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002038 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002039
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002040 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002041 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002042 map = (struct map_lookup *)em->bdev;
2043
2044 for (i = 0; i < map->num_stripes; i++) {
2045 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2046 map->stripes[i].physical);
2047 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002048
Chris Masondfe25022008-05-13 13:46:40 -04002049 if (map->stripes[i].dev) {
2050 ret = btrfs_update_device(trans, map->stripes[i].dev);
2051 BUG_ON(ret);
2052 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002053 }
2054 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2055 chunk_offset);
2056
2057 BUG_ON(ret);
2058
liubo1abe9b82011-03-24 11:18:59 +00002059 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2060
Chris Mason8f18cf12008-04-25 16:53:30 -04002061 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2062 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2063 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002064 }
2065
Zheng Yan1a40e232008-09-26 10:09:34 -04002066 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2067 BUG_ON(ret);
2068
Chris Mason890871b2009-09-02 16:24:52 -04002069 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002070 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002071 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002072
Chris Mason8f18cf12008-04-25 16:53:30 -04002073 kfree(map);
2074 em->bdev = NULL;
2075
2076 /* once for the tree */
2077 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002078 /* once for us */
2079 free_extent_map(em);
2080
Chris Mason7d9eb122008-07-08 14:19:17 -04002081 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002082 btrfs_end_transaction(trans, root);
2083 return 0;
2084}
2085
Yan Zheng2b820322008-11-17 21:11:30 -05002086static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2087{
2088 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2089 struct btrfs_path *path;
2090 struct extent_buffer *leaf;
2091 struct btrfs_chunk *chunk;
2092 struct btrfs_key key;
2093 struct btrfs_key found_key;
2094 u64 chunk_tree = chunk_root->root_key.objectid;
2095 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002096 bool retried = false;
2097 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002098 int ret;
2099
2100 path = btrfs_alloc_path();
2101 if (!path)
2102 return -ENOMEM;
2103
Josef Bacikba1bf482009-09-11 16:11:19 -04002104again:
Yan Zheng2b820322008-11-17 21:11:30 -05002105 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2106 key.offset = (u64)-1;
2107 key.type = BTRFS_CHUNK_ITEM_KEY;
2108
2109 while (1) {
2110 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2111 if (ret < 0)
2112 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002113 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002114
2115 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2116 key.type);
2117 if (ret < 0)
2118 goto error;
2119 if (ret > 0)
2120 break;
2121
2122 leaf = path->nodes[0];
2123 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2124
2125 chunk = btrfs_item_ptr(leaf, path->slots[0],
2126 struct btrfs_chunk);
2127 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002128 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002129
2130 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2131 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2132 found_key.objectid,
2133 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002134 if (ret == -ENOSPC)
2135 failed++;
2136 else if (ret)
2137 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002138 }
2139
2140 if (found_key.offset == 0)
2141 break;
2142 key.offset = found_key.offset - 1;
2143 }
2144 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002145 if (failed && !retried) {
2146 failed = 0;
2147 retried = true;
2148 goto again;
2149 } else if (failed && retried) {
2150 WARN_ON(1);
2151 ret = -ENOSPC;
2152 }
Yan Zheng2b820322008-11-17 21:11:30 -05002153error:
2154 btrfs_free_path(path);
2155 return ret;
2156}
2157
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002158static int insert_balance_item(struct btrfs_root *root,
2159 struct btrfs_balance_control *bctl)
2160{
2161 struct btrfs_trans_handle *trans;
2162 struct btrfs_balance_item *item;
2163 struct btrfs_disk_balance_args disk_bargs;
2164 struct btrfs_path *path;
2165 struct extent_buffer *leaf;
2166 struct btrfs_key key;
2167 int ret, err;
2168
2169 path = btrfs_alloc_path();
2170 if (!path)
2171 return -ENOMEM;
2172
2173 trans = btrfs_start_transaction(root, 0);
2174 if (IS_ERR(trans)) {
2175 btrfs_free_path(path);
2176 return PTR_ERR(trans);
2177 }
2178
2179 key.objectid = BTRFS_BALANCE_OBJECTID;
2180 key.type = BTRFS_BALANCE_ITEM_KEY;
2181 key.offset = 0;
2182
2183 ret = btrfs_insert_empty_item(trans, root, path, &key,
2184 sizeof(*item));
2185 if (ret)
2186 goto out;
2187
2188 leaf = path->nodes[0];
2189 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2190
2191 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2192
2193 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2194 btrfs_set_balance_data(leaf, item, &disk_bargs);
2195 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2196 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2197 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2198 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2199
2200 btrfs_set_balance_flags(leaf, item, bctl->flags);
2201
2202 btrfs_mark_buffer_dirty(leaf);
2203out:
2204 btrfs_free_path(path);
2205 err = btrfs_commit_transaction(trans, root);
2206 if (err && !ret)
2207 ret = err;
2208 return ret;
2209}
2210
2211static int del_balance_item(struct btrfs_root *root)
2212{
2213 struct btrfs_trans_handle *trans;
2214 struct btrfs_path *path;
2215 struct btrfs_key key;
2216 int ret, err;
2217
2218 path = btrfs_alloc_path();
2219 if (!path)
2220 return -ENOMEM;
2221
2222 trans = btrfs_start_transaction(root, 0);
2223 if (IS_ERR(trans)) {
2224 btrfs_free_path(path);
2225 return PTR_ERR(trans);
2226 }
2227
2228 key.objectid = BTRFS_BALANCE_OBJECTID;
2229 key.type = BTRFS_BALANCE_ITEM_KEY;
2230 key.offset = 0;
2231
2232 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2233 if (ret < 0)
2234 goto out;
2235 if (ret > 0) {
2236 ret = -ENOENT;
2237 goto out;
2238 }
2239
2240 ret = btrfs_del_item(trans, root, path);
2241out:
2242 btrfs_free_path(path);
2243 err = btrfs_commit_transaction(trans, root);
2244 if (err && !ret)
2245 ret = err;
2246 return ret;
2247}
2248
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002249/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002250 * This is a heuristic used to reduce the number of chunks balanced on
2251 * resume after balance was interrupted.
2252 */
2253static void update_balance_args(struct btrfs_balance_control *bctl)
2254{
2255 /*
2256 * Turn on soft mode for chunk types that were being converted.
2257 */
2258 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2259 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2260 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2261 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2262 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2263 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2264
2265 /*
2266 * Turn on usage filter if is not already used. The idea is
2267 * that chunks that we have already balanced should be
2268 * reasonably full. Don't do it for chunks that are being
2269 * converted - that will keep us from relocating unconverted
2270 * (albeit full) chunks.
2271 */
2272 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2273 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2274 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2275 bctl->data.usage = 90;
2276 }
2277 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2278 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2279 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2280 bctl->sys.usage = 90;
2281 }
2282 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2283 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2284 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2285 bctl->meta.usage = 90;
2286 }
2287}
2288
2289/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002290 * Should be called with both balance and volume mutexes held to
2291 * serialize other volume operations (add_dev/rm_dev/resize) with
2292 * restriper. Same goes for unset_balance_control.
2293 */
2294static void set_balance_control(struct btrfs_balance_control *bctl)
2295{
2296 struct btrfs_fs_info *fs_info = bctl->fs_info;
2297
2298 BUG_ON(fs_info->balance_ctl);
2299
2300 spin_lock(&fs_info->balance_lock);
2301 fs_info->balance_ctl = bctl;
2302 spin_unlock(&fs_info->balance_lock);
2303}
2304
2305static void unset_balance_control(struct btrfs_fs_info *fs_info)
2306{
2307 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2308
2309 BUG_ON(!fs_info->balance_ctl);
2310
2311 spin_lock(&fs_info->balance_lock);
2312 fs_info->balance_ctl = NULL;
2313 spin_unlock(&fs_info->balance_lock);
2314
2315 kfree(bctl);
2316}
2317
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002318/*
2319 * Balance filters. Return 1 if chunk should be filtered out
2320 * (should not be balanced).
2321 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002322static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002323 struct btrfs_balance_args *bargs)
2324{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002325 chunk_type = chunk_to_extended(chunk_type) &
2326 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002327
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002328 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002329 return 0;
2330
2331 return 1;
2332}
2333
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002334static u64 div_factor_fine(u64 num, int factor)
2335{
2336 if (factor <= 0)
2337 return 0;
2338 if (factor >= 100)
2339 return num;
2340
2341 num *= factor;
2342 do_div(num, 100);
2343 return num;
2344}
2345
2346static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2347 struct btrfs_balance_args *bargs)
2348{
2349 struct btrfs_block_group_cache *cache;
2350 u64 chunk_used, user_thresh;
2351 int ret = 1;
2352
2353 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2354 chunk_used = btrfs_block_group_used(&cache->item);
2355
2356 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2357 if (chunk_used < user_thresh)
2358 ret = 0;
2359
2360 btrfs_put_block_group(cache);
2361 return ret;
2362}
2363
Ilya Dryomov409d4042012-01-16 22:04:47 +02002364static int chunk_devid_filter(struct extent_buffer *leaf,
2365 struct btrfs_chunk *chunk,
2366 struct btrfs_balance_args *bargs)
2367{
2368 struct btrfs_stripe *stripe;
2369 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2370 int i;
2371
2372 for (i = 0; i < num_stripes; i++) {
2373 stripe = btrfs_stripe_nr(chunk, i);
2374 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2375 return 0;
2376 }
2377
2378 return 1;
2379}
2380
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002381/* [pstart, pend) */
2382static int chunk_drange_filter(struct extent_buffer *leaf,
2383 struct btrfs_chunk *chunk,
2384 u64 chunk_offset,
2385 struct btrfs_balance_args *bargs)
2386{
2387 struct btrfs_stripe *stripe;
2388 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2389 u64 stripe_offset;
2390 u64 stripe_length;
2391 int factor;
2392 int i;
2393
2394 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2395 return 0;
2396
2397 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2398 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2399 factor = 2;
2400 else
2401 factor = 1;
2402 factor = num_stripes / factor;
2403
2404 for (i = 0; i < num_stripes; i++) {
2405 stripe = btrfs_stripe_nr(chunk, i);
2406 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2407 continue;
2408
2409 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2410 stripe_length = btrfs_chunk_length(leaf, chunk);
2411 do_div(stripe_length, factor);
2412
2413 if (stripe_offset < bargs->pend &&
2414 stripe_offset + stripe_length > bargs->pstart)
2415 return 0;
2416 }
2417
2418 return 1;
2419}
2420
Ilya Dryomovea671762012-01-16 22:04:48 +02002421/* [vstart, vend) */
2422static int chunk_vrange_filter(struct extent_buffer *leaf,
2423 struct btrfs_chunk *chunk,
2424 u64 chunk_offset,
2425 struct btrfs_balance_args *bargs)
2426{
2427 if (chunk_offset < bargs->vend &&
2428 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2429 /* at least part of the chunk is inside this vrange */
2430 return 0;
2431
2432 return 1;
2433}
2434
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002435static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002436 struct btrfs_balance_args *bargs)
2437{
2438 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2439 return 0;
2440
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002441 chunk_type = chunk_to_extended(chunk_type) &
2442 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002443
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002444 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002445 return 1;
2446
2447 return 0;
2448}
2449
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002450static int should_balance_chunk(struct btrfs_root *root,
2451 struct extent_buffer *leaf,
2452 struct btrfs_chunk *chunk, u64 chunk_offset)
2453{
2454 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2455 struct btrfs_balance_args *bargs = NULL;
2456 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2457
2458 /* type filter */
2459 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2460 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2461 return 0;
2462 }
2463
2464 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2465 bargs = &bctl->data;
2466 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2467 bargs = &bctl->sys;
2468 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2469 bargs = &bctl->meta;
2470
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002471 /* profiles filter */
2472 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2473 chunk_profiles_filter(chunk_type, bargs)) {
2474 return 0;
2475 }
2476
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002477 /* usage filter */
2478 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2479 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2480 return 0;
2481 }
2482
Ilya Dryomov409d4042012-01-16 22:04:47 +02002483 /* devid filter */
2484 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2485 chunk_devid_filter(leaf, chunk, bargs)) {
2486 return 0;
2487 }
2488
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002489 /* drange filter, makes sense only with devid filter */
2490 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2491 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2492 return 0;
2493 }
2494
Ilya Dryomovea671762012-01-16 22:04:48 +02002495 /* vrange filter */
2496 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2497 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2498 return 0;
2499 }
2500
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002501 /* soft profile changing mode */
2502 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2503 chunk_soft_convert_filter(chunk_type, bargs)) {
2504 return 0;
2505 }
2506
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002507 return 1;
2508}
2509
Chris Masonec44a352008-04-28 15:29:52 -04002510static u64 div_factor(u64 num, int factor)
2511{
2512 if (factor == 10)
2513 return num;
2514 num *= factor;
2515 do_div(num, 10);
2516 return num;
2517}
2518
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002519static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002520{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002521 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002522 struct btrfs_root *chunk_root = fs_info->chunk_root;
2523 struct btrfs_root *dev_root = fs_info->dev_root;
2524 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002525 struct btrfs_device *device;
2526 u64 old_size;
2527 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002528 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002529 struct btrfs_path *path;
2530 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002531 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002532 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002533 struct extent_buffer *leaf;
2534 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002535 int ret;
2536 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002537 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002538
Chris Masonec44a352008-04-28 15:29:52 -04002539 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002540 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002541 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002542 old_size = device->total_bytes;
2543 size_to_free = div_factor(old_size, 1);
2544 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002545 if (!device->writeable ||
2546 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002547 continue;
2548
2549 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002550 if (ret == -ENOSPC)
2551 break;
Chris Masonec44a352008-04-28 15:29:52 -04002552 BUG_ON(ret);
2553
Yan, Zhenga22285a2010-05-16 10:48:46 -04002554 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002555 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002556
2557 ret = btrfs_grow_device(trans, device, old_size);
2558 BUG_ON(ret);
2559
2560 btrfs_end_transaction(trans, dev_root);
2561 }
2562
2563 /* step two, relocate all the chunks */
2564 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002565 if (!path) {
2566 ret = -ENOMEM;
2567 goto error;
2568 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002569
2570 /* zero out stat counters */
2571 spin_lock(&fs_info->balance_lock);
2572 memset(&bctl->stat, 0, sizeof(bctl->stat));
2573 spin_unlock(&fs_info->balance_lock);
2574again:
Chris Masonec44a352008-04-28 15:29:52 -04002575 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2576 key.offset = (u64)-1;
2577 key.type = BTRFS_CHUNK_ITEM_KEY;
2578
Chris Masond3977122009-01-05 21:25:51 -05002579 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002580 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002581 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002582 ret = -ECANCELED;
2583 goto error;
2584 }
2585
Chris Masonec44a352008-04-28 15:29:52 -04002586 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2587 if (ret < 0)
2588 goto error;
2589
2590 /*
2591 * this shouldn't happen, it means the last relocate
2592 * failed
2593 */
2594 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002595 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002596
2597 ret = btrfs_previous_item(chunk_root, path, 0,
2598 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002599 if (ret) {
2600 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002601 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002602 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002603
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002604 leaf = path->nodes[0];
2605 slot = path->slots[0];
2606 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2607
Chris Masonec44a352008-04-28 15:29:52 -04002608 if (found_key.objectid != key.objectid)
2609 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002610
Chris Masonec44a352008-04-28 15:29:52 -04002611 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002612 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002613 break;
2614
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002615 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2616
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002617 if (!counting) {
2618 spin_lock(&fs_info->balance_lock);
2619 bctl->stat.considered++;
2620 spin_unlock(&fs_info->balance_lock);
2621 }
2622
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002623 ret = should_balance_chunk(chunk_root, leaf, chunk,
2624 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002625 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002626 if (!ret)
2627 goto loop;
2628
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002629 if (counting) {
2630 spin_lock(&fs_info->balance_lock);
2631 bctl->stat.expected++;
2632 spin_unlock(&fs_info->balance_lock);
2633 goto loop;
2634 }
2635
Chris Masonec44a352008-04-28 15:29:52 -04002636 ret = btrfs_relocate_chunk(chunk_root,
2637 chunk_root->root_key.objectid,
2638 found_key.objectid,
2639 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002640 if (ret && ret != -ENOSPC)
2641 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002642 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002643 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002644 } else {
2645 spin_lock(&fs_info->balance_lock);
2646 bctl->stat.completed++;
2647 spin_unlock(&fs_info->balance_lock);
2648 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002649loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002650 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002651 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002652
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002653 if (counting) {
2654 btrfs_release_path(path);
2655 counting = false;
2656 goto again;
2657 }
Chris Masonec44a352008-04-28 15:29:52 -04002658error:
2659 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002660 if (enospc_errors) {
2661 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2662 enospc_errors);
2663 if (!ret)
2664 ret = -ENOSPC;
2665 }
2666
Chris Masonec44a352008-04-28 15:29:52 -04002667 return ret;
2668}
2669
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002670/**
2671 * alloc_profile_is_valid - see if a given profile is valid and reduced
2672 * @flags: profile to validate
2673 * @extended: if true @flags is treated as an extended profile
2674 */
2675static int alloc_profile_is_valid(u64 flags, int extended)
2676{
2677 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2678 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2679
2680 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2681
2682 /* 1) check that all other bits are zeroed */
2683 if (flags & ~mask)
2684 return 0;
2685
2686 /* 2) see if profile is reduced */
2687 if (flags == 0)
2688 return !extended; /* "0" is valid for usual profiles */
2689
2690 /* true if exactly one bit set */
2691 return (flags & (flags - 1)) == 0;
2692}
2693
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002694static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2695{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002696 /* cancel requested || normal exit path */
2697 return atomic_read(&fs_info->balance_cancel_req) ||
2698 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2699 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002700}
2701
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002702static void __cancel_balance(struct btrfs_fs_info *fs_info)
2703{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002704 int ret;
2705
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002706 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002707 ret = del_balance_item(fs_info->tree_root);
2708 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002709}
2710
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002711void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002712 struct btrfs_ioctl_balance_args *bargs);
2713
2714/*
2715 * Should be called with both balance and volume mutexes held
2716 */
2717int btrfs_balance(struct btrfs_balance_control *bctl,
2718 struct btrfs_ioctl_balance_args *bargs)
2719{
2720 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002721 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002722 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002723 int ret;
2724
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002725 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002726 atomic_read(&fs_info->balance_pause_req) ||
2727 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002728 ret = -EINVAL;
2729 goto out;
2730 }
2731
Ilya Dryomove4837f82012-03-27 17:09:17 +03002732 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2733 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2734 mixed = 1;
2735
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002736 /*
2737 * In case of mixed groups both data and meta should be picked,
2738 * and identical options should be given for both of them.
2739 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002740 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2741 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002742 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2743 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2744 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2745 printk(KERN_ERR "btrfs: with mixed groups data and "
2746 "metadata balance options must be the same\n");
2747 ret = -EINVAL;
2748 goto out;
2749 }
2750 }
2751
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002752 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2753 if (fs_info->fs_devices->num_devices == 1)
2754 allowed |= BTRFS_BLOCK_GROUP_DUP;
2755 else if (fs_info->fs_devices->num_devices < 4)
2756 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2757 else
2758 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2759 BTRFS_BLOCK_GROUP_RAID10);
2760
Ilya Dryomov6728b192012-03-27 17:09:17 +03002761 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2762 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2763 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002764 printk(KERN_ERR "btrfs: unable to start balance with target "
2765 "data profile %llu\n",
2766 (unsigned long long)bctl->data.target);
2767 ret = -EINVAL;
2768 goto out;
2769 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002770 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2771 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2772 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002773 printk(KERN_ERR "btrfs: unable to start balance with target "
2774 "metadata profile %llu\n",
2775 (unsigned long long)bctl->meta.target);
2776 ret = -EINVAL;
2777 goto out;
2778 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002779 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2780 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2781 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002782 printk(KERN_ERR "btrfs: unable to start balance with target "
2783 "system profile %llu\n",
2784 (unsigned long long)bctl->sys.target);
2785 ret = -EINVAL;
2786 goto out;
2787 }
2788
Ilya Dryomove4837f82012-03-27 17:09:17 +03002789 /* allow dup'ed data chunks only in mixed mode */
2790 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002791 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002792 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2793 ret = -EINVAL;
2794 goto out;
2795 }
2796
2797 /* allow to reduce meta or sys integrity only if force set */
2798 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2799 BTRFS_BLOCK_GROUP_RAID10;
2800 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2801 (fs_info->avail_system_alloc_bits & allowed) &&
2802 !(bctl->sys.target & allowed)) ||
2803 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2804 (fs_info->avail_metadata_alloc_bits & allowed) &&
2805 !(bctl->meta.target & allowed))) {
2806 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2807 printk(KERN_INFO "btrfs: force reducing metadata "
2808 "integrity\n");
2809 } else {
2810 printk(KERN_ERR "btrfs: balance will reduce metadata "
2811 "integrity, use force if you want this\n");
2812 ret = -EINVAL;
2813 goto out;
2814 }
2815 }
2816
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002817 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
2818 int num_tolerated_disk_barrier_failures;
2819 u64 target = bctl->sys.target;
2820
2821 num_tolerated_disk_barrier_failures =
2822 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
2823 if (num_tolerated_disk_barrier_failures > 0 &&
2824 (target &
2825 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
2826 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
2827 num_tolerated_disk_barrier_failures = 0;
2828 else if (num_tolerated_disk_barrier_failures > 1 &&
2829 (target &
2830 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
2831 num_tolerated_disk_barrier_failures = 1;
2832
2833 fs_info->num_tolerated_disk_barrier_failures =
2834 num_tolerated_disk_barrier_failures;
2835 }
2836
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002837 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002838 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002839 goto out;
2840
Ilya Dryomov59641012012-01-16 22:04:48 +02002841 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2842 BUG_ON(ret == -EEXIST);
2843 set_balance_control(bctl);
2844 } else {
2845 BUG_ON(ret != -EEXIST);
2846 spin_lock(&fs_info->balance_lock);
2847 update_balance_args(bctl);
2848 spin_unlock(&fs_info->balance_lock);
2849 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002850
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002851 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002852 mutex_unlock(&fs_info->balance_mutex);
2853
2854 ret = __btrfs_balance(fs_info);
2855
2856 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002857 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002858
2859 if (bargs) {
2860 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002861 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002862 }
2863
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002864 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2865 balance_need_close(fs_info)) {
2866 __cancel_balance(fs_info);
2867 }
2868
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002869 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
2870 fs_info->num_tolerated_disk_barrier_failures =
2871 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
2872 }
2873
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002874 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002875
2876 return ret;
2877out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002878 if (bctl->flags & BTRFS_BALANCE_RESUME)
2879 __cancel_balance(fs_info);
2880 else
2881 kfree(bctl);
2882 return ret;
2883}
2884
2885static int balance_kthread(void *data)
2886{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002887 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002888 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002889
2890 mutex_lock(&fs_info->volume_mutex);
2891 mutex_lock(&fs_info->balance_mutex);
2892
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002893 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002894 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002895 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002896 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002897
2898 mutex_unlock(&fs_info->balance_mutex);
2899 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002900
Ilya Dryomov59641012012-01-16 22:04:48 +02002901 return ret;
2902}
2903
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06002904int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
2905{
2906 struct task_struct *tsk;
2907
2908 spin_lock(&fs_info->balance_lock);
2909 if (!fs_info->balance_ctl) {
2910 spin_unlock(&fs_info->balance_lock);
2911 return 0;
2912 }
2913 spin_unlock(&fs_info->balance_lock);
2914
2915 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2916 printk(KERN_INFO "btrfs: force skipping balance\n");
2917 return 0;
2918 }
2919
2920 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
2921 if (IS_ERR(tsk))
2922 return PTR_ERR(tsk);
2923
2924 return 0;
2925}
2926
Ilya Dryomov68310a52012-06-22 12:24:12 -06002927int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02002928{
Ilya Dryomov59641012012-01-16 22:04:48 +02002929 struct btrfs_balance_control *bctl;
2930 struct btrfs_balance_item *item;
2931 struct btrfs_disk_balance_args disk_bargs;
2932 struct btrfs_path *path;
2933 struct extent_buffer *leaf;
2934 struct btrfs_key key;
2935 int ret;
2936
2937 path = btrfs_alloc_path();
2938 if (!path)
2939 return -ENOMEM;
2940
Ilya Dryomov68310a52012-06-22 12:24:12 -06002941 key.objectid = BTRFS_BALANCE_OBJECTID;
2942 key.type = BTRFS_BALANCE_ITEM_KEY;
2943 key.offset = 0;
2944
2945 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2946 if (ret < 0)
2947 goto out;
2948 if (ret > 0) { /* ret = -ENOENT; */
2949 ret = 0;
2950 goto out;
2951 }
2952
Ilya Dryomov59641012012-01-16 22:04:48 +02002953 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2954 if (!bctl) {
2955 ret = -ENOMEM;
2956 goto out;
2957 }
2958
Ilya Dryomov59641012012-01-16 22:04:48 +02002959 leaf = path->nodes[0];
2960 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2961
Ilya Dryomov68310a52012-06-22 12:24:12 -06002962 bctl->fs_info = fs_info;
2963 bctl->flags = btrfs_balance_flags(leaf, item);
2964 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02002965
2966 btrfs_balance_data(leaf, item, &disk_bargs);
2967 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2968 btrfs_balance_meta(leaf, item, &disk_bargs);
2969 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2970 btrfs_balance_sys(leaf, item, &disk_bargs);
2971 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2972
Ilya Dryomov68310a52012-06-22 12:24:12 -06002973 mutex_lock(&fs_info->volume_mutex);
2974 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02002975
Ilya Dryomov68310a52012-06-22 12:24:12 -06002976 set_balance_control(bctl);
2977
2978 mutex_unlock(&fs_info->balance_mutex);
2979 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02002980out:
2981 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002982 return ret;
2983}
2984
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002985int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2986{
2987 int ret = 0;
2988
2989 mutex_lock(&fs_info->balance_mutex);
2990 if (!fs_info->balance_ctl) {
2991 mutex_unlock(&fs_info->balance_mutex);
2992 return -ENOTCONN;
2993 }
2994
2995 if (atomic_read(&fs_info->balance_running)) {
2996 atomic_inc(&fs_info->balance_pause_req);
2997 mutex_unlock(&fs_info->balance_mutex);
2998
2999 wait_event(fs_info->balance_wait_q,
3000 atomic_read(&fs_info->balance_running) == 0);
3001
3002 mutex_lock(&fs_info->balance_mutex);
3003 /* we are good with balance_ctl ripped off from under us */
3004 BUG_ON(atomic_read(&fs_info->balance_running));
3005 atomic_dec(&fs_info->balance_pause_req);
3006 } else {
3007 ret = -ENOTCONN;
3008 }
3009
3010 mutex_unlock(&fs_info->balance_mutex);
3011 return ret;
3012}
3013
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003014int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3015{
3016 mutex_lock(&fs_info->balance_mutex);
3017 if (!fs_info->balance_ctl) {
3018 mutex_unlock(&fs_info->balance_mutex);
3019 return -ENOTCONN;
3020 }
3021
3022 atomic_inc(&fs_info->balance_cancel_req);
3023 /*
3024 * if we are running just wait and return, balance item is
3025 * deleted in btrfs_balance in this case
3026 */
3027 if (atomic_read(&fs_info->balance_running)) {
3028 mutex_unlock(&fs_info->balance_mutex);
3029 wait_event(fs_info->balance_wait_q,
3030 atomic_read(&fs_info->balance_running) == 0);
3031 mutex_lock(&fs_info->balance_mutex);
3032 } else {
3033 /* __cancel_balance needs volume_mutex */
3034 mutex_unlock(&fs_info->balance_mutex);
3035 mutex_lock(&fs_info->volume_mutex);
3036 mutex_lock(&fs_info->balance_mutex);
3037
3038 if (fs_info->balance_ctl)
3039 __cancel_balance(fs_info);
3040
3041 mutex_unlock(&fs_info->volume_mutex);
3042 }
3043
3044 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3045 atomic_dec(&fs_info->balance_cancel_req);
3046 mutex_unlock(&fs_info->balance_mutex);
3047 return 0;
3048}
3049
Chris Mason8f18cf12008-04-25 16:53:30 -04003050/*
3051 * shrinking a device means finding all of the device extents past
3052 * the new size, and then following the back refs to the chunks.
3053 * The chunk relocation code actually frees the device extent
3054 */
3055int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3056{
3057 struct btrfs_trans_handle *trans;
3058 struct btrfs_root *root = device->dev_root;
3059 struct btrfs_dev_extent *dev_extent = NULL;
3060 struct btrfs_path *path;
3061 u64 length;
3062 u64 chunk_tree;
3063 u64 chunk_objectid;
3064 u64 chunk_offset;
3065 int ret;
3066 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003067 int failed = 0;
3068 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003069 struct extent_buffer *l;
3070 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003071 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003072 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003073 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003074 u64 diff = device->total_bytes - new_size;
3075
Yan Zheng2b820322008-11-17 21:11:30 -05003076 if (new_size >= device->total_bytes)
3077 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04003078
3079 path = btrfs_alloc_path();
3080 if (!path)
3081 return -ENOMEM;
3082
Chris Mason8f18cf12008-04-25 16:53:30 -04003083 path->reada = 2;
3084
Chris Mason7d9eb122008-07-08 14:19:17 -04003085 lock_chunks(root);
3086
Chris Mason8f18cf12008-04-25 16:53:30 -04003087 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003088 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003089 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003090 spin_lock(&root->fs_info->free_chunk_lock);
3091 root->fs_info->free_chunk_space -= diff;
3092 spin_unlock(&root->fs_info->free_chunk_lock);
3093 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003094 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003095
Josef Bacikba1bf482009-09-11 16:11:19 -04003096again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003097 key.objectid = device->devid;
3098 key.offset = (u64)-1;
3099 key.type = BTRFS_DEV_EXTENT_KEY;
3100
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003101 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003102 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3103 if (ret < 0)
3104 goto done;
3105
3106 ret = btrfs_previous_item(root, path, 0, key.type);
3107 if (ret < 0)
3108 goto done;
3109 if (ret) {
3110 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003111 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003112 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003113 }
3114
3115 l = path->nodes[0];
3116 slot = path->slots[0];
3117 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3118
Josef Bacikba1bf482009-09-11 16:11:19 -04003119 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003120 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003121 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003122 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003123
3124 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3125 length = btrfs_dev_extent_length(l, dev_extent);
3126
Josef Bacikba1bf482009-09-11 16:11:19 -04003127 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003128 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003129 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003130 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003131
3132 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3133 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3134 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003135 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003136
3137 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3138 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003139 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003140 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003141 if (ret == -ENOSPC)
3142 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003143 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003144
3145 if (failed && !retried) {
3146 failed = 0;
3147 retried = true;
3148 goto again;
3149 } else if (failed && retried) {
3150 ret = -ENOSPC;
3151 lock_chunks(root);
3152
3153 device->total_bytes = old_size;
3154 if (device->writeable)
3155 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003156 spin_lock(&root->fs_info->free_chunk_lock);
3157 root->fs_info->free_chunk_space += diff;
3158 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003159 unlock_chunks(root);
3160 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003161 }
3162
Chris Balld6397ba2009-04-27 07:29:03 -04003163 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003164 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003165 if (IS_ERR(trans)) {
3166 ret = PTR_ERR(trans);
3167 goto done;
3168 }
3169
Chris Balld6397ba2009-04-27 07:29:03 -04003170 lock_chunks(root);
3171
3172 device->disk_total_bytes = new_size;
3173 /* Now btrfs_update_device() will change the on-disk size. */
3174 ret = btrfs_update_device(trans, device);
3175 if (ret) {
3176 unlock_chunks(root);
3177 btrfs_end_transaction(trans, root);
3178 goto done;
3179 }
3180 WARN_ON(diff > old_total);
3181 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3182 unlock_chunks(root);
3183 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003184done:
3185 btrfs_free_path(path);
3186 return ret;
3187}
3188
Li Zefan125ccb02011-12-08 15:07:24 +08003189static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003190 struct btrfs_key *key,
3191 struct btrfs_chunk *chunk, int item_size)
3192{
David Sterba6c417612011-04-13 15:41:04 +02003193 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003194 struct btrfs_disk_key disk_key;
3195 u32 array_size;
3196 u8 *ptr;
3197
3198 array_size = btrfs_super_sys_array_size(super_copy);
3199 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3200 return -EFBIG;
3201
3202 ptr = super_copy->sys_chunk_array + array_size;
3203 btrfs_cpu_key_to_disk(&disk_key, key);
3204 memcpy(ptr, &disk_key, sizeof(disk_key));
3205 ptr += sizeof(disk_key);
3206 memcpy(ptr, chunk, item_size);
3207 item_size += sizeof(disk_key);
3208 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3209 return 0;
3210}
3211
Miao Xieb2117a32011-01-05 10:07:28 +00003212/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003213 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003214 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003215static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003216{
Arne Jansen73c5de02011-04-12 12:07:57 +02003217 const struct btrfs_device_info *di_a = a;
3218 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003219
Arne Jansen73c5de02011-04-12 12:07:57 +02003220 if (di_a->max_avail > di_b->max_avail)
3221 return -1;
3222 if (di_a->max_avail < di_b->max_avail)
3223 return 1;
3224 if (di_a->total_avail > di_b->total_avail)
3225 return -1;
3226 if (di_a->total_avail < di_b->total_avail)
3227 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003228 return 0;
3229}
3230
3231static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3232 struct btrfs_root *extent_root,
3233 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003234 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003235 u64 start, u64 type)
3236{
3237 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003238 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3239 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003240 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003241 struct extent_map_tree *em_tree;
3242 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003243 struct btrfs_device_info *devices_info = NULL;
3244 u64 total_avail;
3245 int num_stripes; /* total number of stripes to allocate */
3246 int sub_stripes; /* sub_stripes info for map */
3247 int dev_stripes; /* stripes per dev */
3248 int devs_max; /* max devs to use */
3249 int devs_min; /* min devs needed */
3250 int devs_increment; /* ndevs has to be a multiple of this */
3251 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003252 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003253 u64 max_stripe_size;
3254 u64 max_chunk_size;
3255 u64 stripe_size;
3256 u64 num_bytes;
3257 int ndevs;
3258 int i;
3259 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003260
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003261 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003262
Miao Xieb2117a32011-01-05 10:07:28 +00003263 if (list_empty(&fs_devices->alloc_list))
3264 return -ENOSPC;
3265
Arne Jansen73c5de02011-04-12 12:07:57 +02003266 sub_stripes = 1;
3267 dev_stripes = 1;
3268 devs_increment = 1;
3269 ncopies = 1;
3270 devs_max = 0; /* 0 == as many as possible */
3271 devs_min = 1;
3272
3273 /*
3274 * define the properties of each RAID type.
3275 * FIXME: move this to a global table and use it in all RAID
3276 * calculation code
3277 */
3278 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3279 dev_stripes = 2;
3280 ncopies = 2;
3281 devs_max = 1;
3282 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3283 devs_min = 2;
3284 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3285 devs_increment = 2;
3286 ncopies = 2;
3287 devs_max = 2;
3288 devs_min = 2;
3289 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3290 sub_stripes = 2;
3291 devs_increment = 2;
3292 ncopies = 2;
3293 devs_min = 4;
3294 } else {
3295 devs_max = 1;
3296 }
3297
3298 if (type & BTRFS_BLOCK_GROUP_DATA) {
3299 max_stripe_size = 1024 * 1024 * 1024;
3300 max_chunk_size = 10 * max_stripe_size;
3301 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003302 /* for larger filesystems, use larger metadata chunks */
3303 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3304 max_stripe_size = 1024 * 1024 * 1024;
3305 else
3306 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003307 max_chunk_size = max_stripe_size;
3308 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003309 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003310 max_chunk_size = 2 * max_stripe_size;
3311 } else {
3312 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3313 type);
3314 BUG_ON(1);
3315 }
3316
3317 /* we don't want a chunk larger than 10% of writeable space */
3318 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3319 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003320
3321 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3322 GFP_NOFS);
3323 if (!devices_info)
3324 return -ENOMEM;
3325
Arne Jansen73c5de02011-04-12 12:07:57 +02003326 cur = fs_devices->alloc_list.next;
3327
3328 /*
3329 * in the first pass through the devices list, we gather information
3330 * about the available holes on each device.
3331 */
3332 ndevs = 0;
3333 while (cur != &fs_devices->alloc_list) {
3334 struct btrfs_device *device;
3335 u64 max_avail;
3336 u64 dev_offset;
3337
3338 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3339
3340 cur = cur->next;
3341
3342 if (!device->writeable) {
3343 printk(KERN_ERR
3344 "btrfs: read-only device in alloc_list\n");
3345 WARN_ON(1);
3346 continue;
3347 }
3348
3349 if (!device->in_fs_metadata)
3350 continue;
3351
3352 if (device->total_bytes > device->bytes_used)
3353 total_avail = device->total_bytes - device->bytes_used;
3354 else
3355 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003356
3357 /* If there is no space on this device, skip it. */
3358 if (total_avail == 0)
3359 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003360
Li Zefan125ccb02011-12-08 15:07:24 +08003361 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003362 max_stripe_size * dev_stripes,
3363 &dev_offset, &max_avail);
3364 if (ret && ret != -ENOSPC)
3365 goto error;
3366
3367 if (ret == 0)
3368 max_avail = max_stripe_size * dev_stripes;
3369
3370 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3371 continue;
3372
3373 devices_info[ndevs].dev_offset = dev_offset;
3374 devices_info[ndevs].max_avail = max_avail;
3375 devices_info[ndevs].total_avail = total_avail;
3376 devices_info[ndevs].dev = device;
3377 ++ndevs;
3378 }
3379
3380 /*
3381 * now sort the devices by hole size / available space
3382 */
3383 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3384 btrfs_cmp_device_info, NULL);
3385
3386 /* round down to number of usable stripes */
3387 ndevs -= ndevs % devs_increment;
3388
3389 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3390 ret = -ENOSPC;
3391 goto error;
3392 }
3393
3394 if (devs_max && ndevs > devs_max)
3395 ndevs = devs_max;
3396 /*
3397 * the primary goal is to maximize the number of stripes, so use as many
3398 * devices as possible, even if the stripes are not maximum sized.
3399 */
3400 stripe_size = devices_info[ndevs-1].max_avail;
3401 num_stripes = ndevs * dev_stripes;
3402
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003403 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003404 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003405 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003406 }
3407
3408 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003409
3410 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003411 do_div(stripe_size, BTRFS_STRIPE_LEN);
3412 stripe_size *= BTRFS_STRIPE_LEN;
3413
Miao Xieb2117a32011-01-05 10:07:28 +00003414 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3415 if (!map) {
3416 ret = -ENOMEM;
3417 goto error;
3418 }
3419 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003420
Arne Jansen73c5de02011-04-12 12:07:57 +02003421 for (i = 0; i < ndevs; ++i) {
3422 for (j = 0; j < dev_stripes; ++j) {
3423 int s = i * dev_stripes + j;
3424 map->stripes[s].dev = devices_info[i].dev;
3425 map->stripes[s].physical = devices_info[i].dev_offset +
3426 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003427 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003428 }
Chris Mason593060d2008-03-25 16:50:33 -04003429 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003430 map->stripe_len = BTRFS_STRIPE_LEN;
3431 map->io_align = BTRFS_STRIPE_LEN;
3432 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003433 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003434 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003435
Yan Zheng2b820322008-11-17 21:11:30 -05003436 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003437 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003438
Arne Jansen73c5de02011-04-12 12:07:57 +02003439 *stripe_size_out = stripe_size;
3440 *num_bytes_out = num_bytes;
3441
3442 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003443
David Sterba172ddd62011-04-21 00:48:27 +02003444 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003445 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003446 ret = -ENOMEM;
3447 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003448 }
Chris Mason0b86a832008-03-24 15:01:56 -04003449 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003450 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003451 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003452 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003453 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003454
Chris Mason0b86a832008-03-24 15:01:56 -04003455 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003456 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003457 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003458 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003459 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003460 if (ret)
3461 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003462
3463 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3464 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003465 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003466 if (ret)
3467 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003468
Arne Jansen73c5de02011-04-12 12:07:57 +02003469 for (i = 0; i < map->num_stripes; ++i) {
3470 struct btrfs_device *device;
3471 u64 dev_offset;
3472
3473 device = map->stripes[i].dev;
3474 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003475
3476 ret = btrfs_alloc_dev_extent(trans, device,
3477 info->chunk_root->root_key.objectid,
3478 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003479 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003480 if (ret) {
3481 btrfs_abort_transaction(trans, extent_root, ret);
3482 goto error;
3483 }
Yan Zheng2b820322008-11-17 21:11:30 -05003484 }
3485
Miao Xieb2117a32011-01-05 10:07:28 +00003486 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003487 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003488
3489error:
3490 kfree(map);
3491 kfree(devices_info);
3492 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003493}
3494
3495static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3496 struct btrfs_root *extent_root,
3497 struct map_lookup *map, u64 chunk_offset,
3498 u64 chunk_size, u64 stripe_size)
3499{
3500 u64 dev_offset;
3501 struct btrfs_key key;
3502 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3503 struct btrfs_device *device;
3504 struct btrfs_chunk *chunk;
3505 struct btrfs_stripe *stripe;
3506 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3507 int index = 0;
3508 int ret;
3509
3510 chunk = kzalloc(item_size, GFP_NOFS);
3511 if (!chunk)
3512 return -ENOMEM;
3513
3514 index = 0;
3515 while (index < map->num_stripes) {
3516 device = map->stripes[index].dev;
3517 device->bytes_used += stripe_size;
3518 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003519 if (ret)
3520 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003521 index++;
3522 }
3523
Josef Bacik2bf64752011-09-26 17:12:22 -04003524 spin_lock(&extent_root->fs_info->free_chunk_lock);
3525 extent_root->fs_info->free_chunk_space -= (stripe_size *
3526 map->num_stripes);
3527 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3528
Yan Zheng2b820322008-11-17 21:11:30 -05003529 index = 0;
3530 stripe = &chunk->stripe;
3531 while (index < map->num_stripes) {
3532 device = map->stripes[index].dev;
3533 dev_offset = map->stripes[index].physical;
3534
3535 btrfs_set_stack_stripe_devid(stripe, device->devid);
3536 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3537 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3538 stripe++;
3539 index++;
3540 }
3541
3542 btrfs_set_stack_chunk_length(chunk, chunk_size);
3543 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3544 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3545 btrfs_set_stack_chunk_type(chunk, map->type);
3546 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3547 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3548 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3549 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3550 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3551
3552 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3553 key.type = BTRFS_CHUNK_ITEM_KEY;
3554 key.offset = chunk_offset;
3555
3556 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003557
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003558 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3559 /*
3560 * TODO: Cleanup of inserted chunk root in case of
3561 * failure.
3562 */
Li Zefan125ccb02011-12-08 15:07:24 +08003563 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003564 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003565 }
liubo1abe9b82011-03-24 11:18:59 +00003566
Mark Fasheh3acd3952011-09-08 17:40:01 -07003567out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003568 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003569 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003570}
3571
3572/*
3573 * Chunk allocation falls into two parts. The first part does works
3574 * that make the new allocated chunk useable, but not do any operation
3575 * that modifies the chunk tree. The second part does the works that
3576 * require modifying the chunk tree. This division is important for the
3577 * bootstrap process of adding storage to a seed btrfs.
3578 */
3579int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3580 struct btrfs_root *extent_root, u64 type)
3581{
3582 u64 chunk_offset;
3583 u64 chunk_size;
3584 u64 stripe_size;
3585 struct map_lookup *map;
3586 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3587 int ret;
3588
3589 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3590 &chunk_offset);
3591 if (ret)
3592 return ret;
3593
3594 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3595 &stripe_size, chunk_offset, type);
3596 if (ret)
3597 return ret;
3598
3599 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3600 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003601 if (ret)
3602 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003603 return 0;
3604}
3605
Chris Masond3977122009-01-05 21:25:51 -05003606static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003607 struct btrfs_root *root,
3608 struct btrfs_device *device)
3609{
3610 u64 chunk_offset;
3611 u64 sys_chunk_offset;
3612 u64 chunk_size;
3613 u64 sys_chunk_size;
3614 u64 stripe_size;
3615 u64 sys_stripe_size;
3616 u64 alloc_profile;
3617 struct map_lookup *map;
3618 struct map_lookup *sys_map;
3619 struct btrfs_fs_info *fs_info = root->fs_info;
3620 struct btrfs_root *extent_root = fs_info->extent_root;
3621 int ret;
3622
3623 ret = find_next_chunk(fs_info->chunk_root,
3624 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003625 if (ret)
3626 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003627
3628 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003629 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003630 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3631
3632 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3633 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003634 if (ret)
3635 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003636
3637 sys_chunk_offset = chunk_offset + chunk_size;
3638
3639 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003640 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003641 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3642
3643 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3644 &sys_chunk_size, &sys_stripe_size,
3645 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06003646 if (ret) {
3647 btrfs_abort_transaction(trans, root, ret);
3648 goto out;
3649 }
Yan Zheng2b820322008-11-17 21:11:30 -05003650
3651 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06003652 if (ret) {
3653 btrfs_abort_transaction(trans, root, ret);
3654 goto out;
3655 }
Yan Zheng2b820322008-11-17 21:11:30 -05003656
3657 /*
3658 * Modifying chunk tree needs allocating new blocks from both
3659 * system block group and metadata block group. So we only can
3660 * do operations require modifying the chunk tree after both
3661 * block groups were created.
3662 */
3663 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3664 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06003665 if (ret) {
3666 btrfs_abort_transaction(trans, root, ret);
3667 goto out;
3668 }
Yan Zheng2b820322008-11-17 21:11:30 -05003669
3670 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3671 sys_chunk_offset, sys_chunk_size,
3672 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003673 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06003674 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003675
David Sterba005d6422012-09-18 07:52:32 -06003676out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003677
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003678 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003679}
3680
3681int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3682{
3683 struct extent_map *em;
3684 struct map_lookup *map;
3685 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3686 int readonly = 0;
3687 int i;
3688
Chris Mason890871b2009-09-02 16:24:52 -04003689 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003690 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003691 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003692 if (!em)
3693 return 1;
3694
Josef Bacikf48b9072010-01-27 02:07:59 +00003695 if (btrfs_test_opt(root, DEGRADED)) {
3696 free_extent_map(em);
3697 return 0;
3698 }
3699
Yan Zheng2b820322008-11-17 21:11:30 -05003700 map = (struct map_lookup *)em->bdev;
3701 for (i = 0; i < map->num_stripes; i++) {
3702 if (!map->stripes[i].dev->writeable) {
3703 readonly = 1;
3704 break;
3705 }
3706 }
3707 free_extent_map(em);
3708 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003709}
3710
3711void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3712{
David Sterbaa8067e02011-04-21 00:34:43 +02003713 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003714}
3715
3716void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3717{
3718 struct extent_map *em;
3719
Chris Masond3977122009-01-05 21:25:51 -05003720 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003721 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003722 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3723 if (em)
3724 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003725 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003726 if (!em)
3727 break;
3728 kfree(em->bdev);
3729 /* once for us */
3730 free_extent_map(em);
3731 /* once for the tree */
3732 free_extent_map(em);
3733 }
3734}
3735
Chris Masonf1885912008-04-09 16:28:12 -04003736int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3737{
3738 struct extent_map *em;
3739 struct map_lookup *map;
3740 struct extent_map_tree *em_tree = &map_tree->map_tree;
3741 int ret;
3742
Chris Mason890871b2009-09-02 16:24:52 -04003743 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003744 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003745 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003746 BUG_ON(!em);
3747
3748 BUG_ON(em->start > logical || em->start + em->len < logical);
3749 map = (struct map_lookup *)em->bdev;
3750 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3751 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003752 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3753 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003754 else
3755 ret = 1;
3756 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003757 return ret;
3758}
3759
Chris Masondfe25022008-05-13 13:46:40 -04003760static int find_live_mirror(struct map_lookup *map, int first, int num,
3761 int optimal)
3762{
3763 int i;
3764 if (map->stripes[optimal].dev->bdev)
3765 return optimal;
3766 for (i = first; i < first + num; i++) {
3767 if (map->stripes[i].dev->bdev)
3768 return i;
3769 }
3770 /* we couldn't find one that doesn't fail. Just return something
3771 * and the io error handling code will clean up eventually
3772 */
3773 return optimal;
3774}
3775
Chris Masonf2d8d742008-04-21 10:03:05 -04003776static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3777 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003778 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003779 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003780{
3781 struct extent_map *em;
3782 struct map_lookup *map;
3783 struct extent_map_tree *em_tree = &map_tree->map_tree;
3784 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003785 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003786 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003787 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003788 u64 stripe_nr_orig;
3789 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003790 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003791 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003792 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003793 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003794 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003795 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003796
Chris Mason890871b2009-09-02 16:24:52 -04003797 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003798 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003799 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003800
Chris Mason3b951512008-04-17 11:29:12 -04003801 if (!em) {
Daniel J Blueman48940662012-05-07 06:35:38 -06003802 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05003803 (unsigned long long)logical,
3804 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003805 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003806 }
Chris Mason0b86a832008-03-24 15:01:56 -04003807
3808 BUG_ON(em->start > logical || em->start + em->len < logical);
3809 map = (struct map_lookup *)em->bdev;
3810 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003811
Chris Masonf1885912008-04-09 16:28:12 -04003812 if (mirror_num > map->num_stripes)
3813 mirror_num = 0;
3814
Chris Mason593060d2008-03-25 16:50:33 -04003815 stripe_nr = offset;
3816 /*
3817 * stripe_nr counts the total number of stripes we have to stride
3818 * to get to this block
3819 */
3820 do_div(stripe_nr, map->stripe_len);
3821
3822 stripe_offset = stripe_nr * map->stripe_len;
3823 BUG_ON(offset < stripe_offset);
3824
3825 /* stripe_offset is the offset of this block in its stripe*/
3826 stripe_offset = offset - stripe_offset;
3827
Li Dongyangfce3bb92011-03-24 10:24:26 +00003828 if (rw & REQ_DISCARD)
3829 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003830 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003831 /* we limit the length of each bio to what fits in a stripe */
3832 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003833 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003834 } else {
3835 *length = em->len - offset;
3836 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003837
Jan Schmidta1d3c472011-08-04 17:15:33 +02003838 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003839 goto out;
3840
Chris Masonf2d8d742008-04-21 10:03:05 -04003841 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003842 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003843 stripe_nr_orig = stripe_nr;
3844 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3845 (~(map->stripe_len - 1));
3846 do_div(stripe_nr_end, map->stripe_len);
3847 stripe_end_offset = stripe_nr_end * map->stripe_len -
3848 (offset + *length);
3849 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3850 if (rw & REQ_DISCARD)
3851 num_stripes = min_t(u64, map->num_stripes,
3852 stripe_nr_end - stripe_nr_orig);
3853 stripe_index = do_div(stripe_nr, map->num_stripes);
3854 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003855 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003856 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003857 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003858 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003859 else {
3860 stripe_index = find_live_mirror(map, 0,
3861 map->num_stripes,
3862 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003863 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003864 }
Chris Mason2fff7342008-04-29 14:12:09 -04003865
Chris Mason611f0e02008-04-03 16:29:03 -04003866 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003867 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003868 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003869 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003870 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003871 } else {
3872 mirror_num = 1;
3873 }
Chris Mason2fff7342008-04-29 14:12:09 -04003874
Chris Mason321aecc2008-04-16 10:49:51 -04003875 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3876 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003877
3878 stripe_index = do_div(stripe_nr, factor);
3879 stripe_index *= map->sub_stripes;
3880
Jens Axboe7eaceac2011-03-10 08:52:07 +01003881 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003882 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003883 else if (rw & REQ_DISCARD)
3884 num_stripes = min_t(u64, map->sub_stripes *
3885 (stripe_nr_end - stripe_nr_orig),
3886 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003887 else if (mirror_num)
3888 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003889 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04003890 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04003891 stripe_index = find_live_mirror(map, stripe_index,
3892 map->sub_stripes, stripe_index +
3893 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04003894 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003895 }
Chris Mason8790d502008-04-03 16:29:03 -04003896 } else {
3897 /*
3898 * after this do_div call, stripe_nr is the number of stripes
3899 * on this device we have to walk to find the data, and
3900 * stripe_index is the number of our device in the stripe array
3901 */
3902 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003903 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003904 }
Chris Mason593060d2008-03-25 16:50:33 -04003905 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003906
Li Zefande11cc12011-12-01 12:55:47 +08003907 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3908 if (!bbio) {
3909 ret = -ENOMEM;
3910 goto out;
3911 }
3912 atomic_set(&bbio->error, 0);
3913
Li Dongyangfce3bb92011-03-24 10:24:26 +00003914 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003915 int factor = 0;
3916 int sub_stripes = 0;
3917 u64 stripes_per_dev = 0;
3918 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003919 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003920
3921 if (map->type &
3922 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3923 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3924 sub_stripes = 1;
3925 else
3926 sub_stripes = map->sub_stripes;
3927
3928 factor = map->num_stripes / sub_stripes;
3929 stripes_per_dev = div_u64_rem(stripe_nr_end -
3930 stripe_nr_orig,
3931 factor,
3932 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04003933 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3934 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003935 }
3936
Li Dongyangfce3bb92011-03-24 10:24:26 +00003937 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003938 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003939 map->stripes[stripe_index].physical +
3940 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003941 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003942
Li Zefanec9ef7a2011-12-01 14:06:42 +08003943 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3944 BTRFS_BLOCK_GROUP_RAID10)) {
3945 bbio->stripes[i].length = stripes_per_dev *
3946 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003947
Li Zefanec9ef7a2011-12-01 14:06:42 +08003948 if (i / sub_stripes < remaining_stripes)
3949 bbio->stripes[i].length +=
3950 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003951
3952 /*
3953 * Special for the first stripe and
3954 * the last stripe:
3955 *
3956 * |-------|...|-------|
3957 * |----------|
3958 * off end_off
3959 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08003960 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003961 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003962 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003963
3964 if (stripe_index >= last_stripe &&
3965 stripe_index <= (last_stripe +
3966 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08003967 bbio->stripes[i].length -=
3968 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003969
Li Zefanec9ef7a2011-12-01 14:06:42 +08003970 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003971 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003972 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003973 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003974
3975 stripe_index++;
3976 if (stripe_index == map->num_stripes) {
3977 /* This could only happen for RAID0/10 */
3978 stripe_index = 0;
3979 stripe_nr++;
3980 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003981 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003982 } else {
3983 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003984 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003985 map->stripes[stripe_index].physical +
3986 stripe_offset +
3987 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003988 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003989 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003990 stripe_index++;
3991 }
Chris Mason593060d2008-03-25 16:50:33 -04003992 }
Li Zefande11cc12011-12-01 12:55:47 +08003993
3994 if (rw & REQ_WRITE) {
3995 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3996 BTRFS_BLOCK_GROUP_RAID10 |
3997 BTRFS_BLOCK_GROUP_DUP)) {
3998 max_errors = 1;
3999 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004000 }
Li Zefande11cc12011-12-01 12:55:47 +08004001
4002 *bbio_ret = bbio;
4003 bbio->num_stripes = num_stripes;
4004 bbio->max_errors = max_errors;
4005 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04004006out:
Chris Mason0b86a832008-03-24 15:01:56 -04004007 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004008 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004009}
4010
Chris Masonf2d8d742008-04-21 10:03:05 -04004011int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
4012 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004013 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004014{
Jan Schmidta1d3c472011-08-04 17:15:33 +02004015 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01004016 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04004017}
4018
Yan Zhenga512bbf2008-12-08 16:46:26 -05004019int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4020 u64 chunk_start, u64 physical, u64 devid,
4021 u64 **logical, int *naddrs, int *stripe_len)
4022{
4023 struct extent_map_tree *em_tree = &map_tree->map_tree;
4024 struct extent_map *em;
4025 struct map_lookup *map;
4026 u64 *buf;
4027 u64 bytenr;
4028 u64 length;
4029 u64 stripe_nr;
4030 int i, j, nr = 0;
4031
Chris Mason890871b2009-09-02 16:24:52 -04004032 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004033 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004034 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004035
4036 BUG_ON(!em || em->start != chunk_start);
4037 map = (struct map_lookup *)em->bdev;
4038
4039 length = em->len;
4040 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4041 do_div(length, map->num_stripes / map->sub_stripes);
4042 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4043 do_div(length, map->num_stripes);
4044
4045 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004046 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004047
4048 for (i = 0; i < map->num_stripes; i++) {
4049 if (devid && map->stripes[i].dev->devid != devid)
4050 continue;
4051 if (map->stripes[i].physical > physical ||
4052 map->stripes[i].physical + length <= physical)
4053 continue;
4054
4055 stripe_nr = physical - map->stripes[i].physical;
4056 do_div(stripe_nr, map->stripe_len);
4057
4058 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4059 stripe_nr = stripe_nr * map->num_stripes + i;
4060 do_div(stripe_nr, map->sub_stripes);
4061 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4062 stripe_nr = stripe_nr * map->num_stripes + i;
4063 }
4064 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05004065 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004066 for (j = 0; j < nr; j++) {
4067 if (buf[j] == bytenr)
4068 break;
4069 }
Chris Mason934d3752008-12-08 16:43:10 -05004070 if (j == nr) {
4071 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004072 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004073 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004074 }
4075
Yan Zhenga512bbf2008-12-08 16:46:26 -05004076 *logical = buf;
4077 *naddrs = nr;
4078 *stripe_len = map->stripe_len;
4079
4080 free_extent_map(em);
4081 return 0;
4082}
4083
Stefan Behrens442a4f62012-05-25 16:06:08 +02004084static void *merge_stripe_index_into_bio_private(void *bi_private,
4085 unsigned int stripe_index)
4086{
4087 /*
4088 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4089 * at most 1.
4090 * The alternative solution (instead of stealing bits from the
4091 * pointer) would be to allocate an intermediate structure
4092 * that contains the old private pointer plus the stripe_index.
4093 */
4094 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4095 BUG_ON(stripe_index > 3);
4096 return (void *)(((uintptr_t)bi_private) | stripe_index);
4097}
4098
4099static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4100{
4101 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4102}
4103
4104static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4105{
4106 return (unsigned int)((uintptr_t)bi_private) & 3;
4107}
4108
Jan Schmidta1d3c472011-08-04 17:15:33 +02004109static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004110{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004111 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004112 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004113
Stefan Behrens442a4f62012-05-25 16:06:08 +02004114 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004115 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004116 if (err == -EIO || err == -EREMOTEIO) {
4117 unsigned int stripe_index =
4118 extract_stripe_index_from_bio_private(
4119 bio->bi_private);
4120 struct btrfs_device *dev;
4121
4122 BUG_ON(stripe_index >= bbio->num_stripes);
4123 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004124 if (dev->bdev) {
4125 if (bio->bi_rw & WRITE)
4126 btrfs_dev_stat_inc(dev,
4127 BTRFS_DEV_STAT_WRITE_ERRS);
4128 else
4129 btrfs_dev_stat_inc(dev,
4130 BTRFS_DEV_STAT_READ_ERRS);
4131 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4132 btrfs_dev_stat_inc(dev,
4133 BTRFS_DEV_STAT_FLUSH_ERRS);
4134 btrfs_dev_stat_print_on_error(dev);
4135 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004136 }
4137 }
Chris Mason8790d502008-04-03 16:29:03 -04004138
Jan Schmidta1d3c472011-08-04 17:15:33 +02004139 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004140 is_orig_bio = 1;
4141
Jan Schmidta1d3c472011-08-04 17:15:33 +02004142 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004143 if (!is_orig_bio) {
4144 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004145 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004146 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004147 bio->bi_private = bbio->private;
4148 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004149 bio->bi_bdev = (struct block_device *)
4150 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004151 /* only send an error to the higher layers if it is
4152 * beyond the tolerance of the multi-bio
4153 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004154 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004155 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004156 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004157 /*
4158 * this bio is actually up to date, we didn't
4159 * go over the max number of errors
4160 */
4161 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004162 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004163 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004164 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004165
4166 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004167 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004168 bio_put(bio);
4169 }
Chris Mason8790d502008-04-03 16:29:03 -04004170}
4171
Chris Mason8b712842008-06-11 16:50:36 -04004172struct async_sched {
4173 struct bio *bio;
4174 int rw;
4175 struct btrfs_fs_info *info;
4176 struct btrfs_work work;
4177};
4178
4179/*
4180 * see run_scheduled_bios for a description of why bios are collected for
4181 * async submit.
4182 *
4183 * This will add one bio to the pending list for a device and make sure
4184 * the work struct is scheduled.
4185 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004186static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004187 struct btrfs_device *device,
4188 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004189{
4190 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004191 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004192
4193 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004194 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004195 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004196 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004197 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004198 return;
Chris Mason8b712842008-06-11 16:50:36 -04004199 }
4200
4201 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004202 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004203 * higher layers. Otherwise, the async bio makes it appear we have
4204 * made progress against dirty pages when we've really just put it
4205 * on a queue for later
4206 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004207 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004208 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004209 bio->bi_next = NULL;
4210 bio->bi_rw |= rw;
4211
4212 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004213 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004214 pending_bios = &device->pending_sync_bios;
4215 else
4216 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004217
Chris Masonffbd5172009-04-20 15:50:09 -04004218 if (pending_bios->tail)
4219 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004220
Chris Masonffbd5172009-04-20 15:50:09 -04004221 pending_bios->tail = bio;
4222 if (!pending_bios->head)
4223 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004224 if (device->running_pending)
4225 should_queue = 0;
4226
4227 spin_unlock(&device->io_lock);
4228
4229 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004230 btrfs_queue_worker(&root->fs_info->submit_workers,
4231 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004232}
4233
Chris Masonf1885912008-04-09 16:28:12 -04004234int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004235 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004236{
4237 struct btrfs_mapping_tree *map_tree;
4238 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004239 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004240 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004241 u64 length = 0;
4242 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004243 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004244 int dev_nr = 0;
4245 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004246 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004247
Chris Masonf2d8d742008-04-21 10:03:05 -04004248 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004249 map_tree = &root->fs_info->mapping_tree;
4250 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004251
Jan Schmidta1d3c472011-08-04 17:15:33 +02004252 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004253 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004254 if (ret) /* -ENOMEM */
4255 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004256
Jan Schmidta1d3c472011-08-04 17:15:33 +02004257 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004258 if (map_length < length) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004259 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
Chris Masond3977122009-01-05 21:25:51 -05004260 "len %llu\n", (unsigned long long)logical,
4261 (unsigned long long)length,
4262 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004263 BUG();
4264 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004265
4266 bbio->orig_bio = first_bio;
4267 bbio->private = first_bio->bi_private;
4268 bbio->end_io = first_bio->bi_end_io;
4269 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004270
Chris Masond3977122009-01-05 21:25:51 -05004271 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004272 if (dev_nr < total_devs - 1) {
4273 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004274 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004275 } else {
4276 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004277 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004278 bio->bi_private = bbio;
Stefan Behrens442a4f62012-05-25 16:06:08 +02004279 bio->bi_private = merge_stripe_index_into_bio_private(
4280 bio->bi_private, (unsigned int)dev_nr);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004281 bio->bi_end_io = btrfs_end_bio;
4282 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4283 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004284 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Josef Bacik606686e2012-06-04 14:03:51 -04004285#ifdef DEBUG
4286 struct rcu_string *name;
4287
4288 rcu_read_lock();
4289 name = rcu_dereference(dev->name);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004290 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4291 "(%s id %llu), size=%u\n", rw,
4292 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
Josef Bacik606686e2012-06-04 14:03:51 -04004293 name->str, dev->devid, bio->bi_size);
4294 rcu_read_unlock();
4295#endif
Chris Masondfe25022008-05-13 13:46:40 -04004296 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004297 if (async_submit)
4298 schedule_bio(root, dev, rw, bio);
4299 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004300 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004301 } else {
4302 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4303 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004304 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004305 }
Chris Mason8790d502008-04-03 16:29:03 -04004306 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004307 }
Chris Mason0b86a832008-03-24 15:01:56 -04004308 return 0;
4309}
4310
Chris Masona4437552008-04-18 10:29:38 -04004311struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004312 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004313{
Yan Zheng2b820322008-11-17 21:11:30 -05004314 struct btrfs_device *device;
4315 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004316
Yan Zheng2b820322008-11-17 21:11:30 -05004317 cur_devices = root->fs_info->fs_devices;
4318 while (cur_devices) {
4319 if (!fsid ||
4320 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4321 device = __find_device(&cur_devices->devices,
4322 devid, uuid);
4323 if (device)
4324 return device;
4325 }
4326 cur_devices = cur_devices->seed;
4327 }
4328 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004329}
4330
Chris Masondfe25022008-05-13 13:46:40 -04004331static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4332 u64 devid, u8 *dev_uuid)
4333{
4334 struct btrfs_device *device;
4335 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4336
4337 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004338 if (!device)
4339 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004340 list_add(&device->dev_list,
4341 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004342 device->dev_root = root->fs_info->dev_root;
4343 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004344 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004345 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004346 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004347 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004348 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004349 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004350 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004351 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4352 return device;
4353}
4354
Chris Mason0b86a832008-03-24 15:01:56 -04004355static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4356 struct extent_buffer *leaf,
4357 struct btrfs_chunk *chunk)
4358{
4359 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4360 struct map_lookup *map;
4361 struct extent_map *em;
4362 u64 logical;
4363 u64 length;
4364 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004365 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004366 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004367 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004368 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004369
Chris Masone17cade2008-04-15 15:41:47 -04004370 logical = key->offset;
4371 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004372
Chris Mason890871b2009-09-02 16:24:52 -04004373 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004374 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004375 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004376
4377 /* already mapped? */
4378 if (em && em->start <= logical && em->start + em->len > logical) {
4379 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004380 return 0;
4381 } else if (em) {
4382 free_extent_map(em);
4383 }
Chris Mason0b86a832008-03-24 15:01:56 -04004384
David Sterba172ddd62011-04-21 00:48:27 +02004385 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004386 if (!em)
4387 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004388 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4389 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004390 if (!map) {
4391 free_extent_map(em);
4392 return -ENOMEM;
4393 }
4394
4395 em->bdev = (struct block_device *)map;
4396 em->start = logical;
4397 em->len = length;
4398 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004399 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004400
Chris Mason593060d2008-03-25 16:50:33 -04004401 map->num_stripes = num_stripes;
4402 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4403 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4404 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4405 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4406 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004407 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004408 for (i = 0; i < num_stripes; i++) {
4409 map->stripes[i].physical =
4410 btrfs_stripe_offset_nr(leaf, chunk, i);
4411 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004412 read_extent_buffer(leaf, uuid, (unsigned long)
4413 btrfs_stripe_dev_uuid_nr(chunk, i),
4414 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004415 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4416 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004417 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004418 kfree(map);
4419 free_extent_map(em);
4420 return -EIO;
4421 }
Chris Masondfe25022008-05-13 13:46:40 -04004422 if (!map->stripes[i].dev) {
4423 map->stripes[i].dev =
4424 add_missing_dev(root, devid, uuid);
4425 if (!map->stripes[i].dev) {
4426 kfree(map);
4427 free_extent_map(em);
4428 return -EIO;
4429 }
4430 }
4431 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004432 }
4433
Chris Mason890871b2009-09-02 16:24:52 -04004434 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004435 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004436 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004437 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004438 free_extent_map(em);
4439
4440 return 0;
4441}
4442
Jeff Mahoney143bede2012-03-01 14:56:26 +01004443static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004444 struct btrfs_dev_item *dev_item,
4445 struct btrfs_device *device)
4446{
4447 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004448
4449 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004450 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4451 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004452 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4453 device->type = btrfs_device_type(leaf, dev_item);
4454 device->io_align = btrfs_device_io_align(leaf, dev_item);
4455 device->io_width = btrfs_device_io_width(leaf, dev_item);
4456 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004457
4458 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004459 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004460}
4461
Yan Zheng2b820322008-11-17 21:11:30 -05004462static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4463{
4464 struct btrfs_fs_devices *fs_devices;
4465 int ret;
4466
Li Zefanb367e472011-12-07 11:38:24 +08004467 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004468
4469 fs_devices = root->fs_info->fs_devices->seed;
4470 while (fs_devices) {
4471 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4472 ret = 0;
4473 goto out;
4474 }
4475 fs_devices = fs_devices->seed;
4476 }
4477
4478 fs_devices = find_fsid(fsid);
4479 if (!fs_devices) {
4480 ret = -ENOENT;
4481 goto out;
4482 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004483
4484 fs_devices = clone_fs_devices(fs_devices);
4485 if (IS_ERR(fs_devices)) {
4486 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004487 goto out;
4488 }
4489
Christoph Hellwig97288f22008-12-02 06:36:09 -05004490 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004491 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004492 if (ret) {
4493 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004494 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004495 }
Yan Zheng2b820322008-11-17 21:11:30 -05004496
4497 if (!fs_devices->seeding) {
4498 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004499 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004500 ret = -EINVAL;
4501 goto out;
4502 }
4503
4504 fs_devices->seed = root->fs_info->fs_devices->seed;
4505 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004506out:
Yan Zheng2b820322008-11-17 21:11:30 -05004507 return ret;
4508}
4509
Chris Mason0d81ba52008-03-24 15:02:07 -04004510static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004511 struct extent_buffer *leaf,
4512 struct btrfs_dev_item *dev_item)
4513{
4514 struct btrfs_device *device;
4515 u64 devid;
4516 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004517 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004518 u8 dev_uuid[BTRFS_UUID_SIZE];
4519
Chris Mason0b86a832008-03-24 15:01:56 -04004520 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004521 read_extent_buffer(leaf, dev_uuid,
4522 (unsigned long)btrfs_device_uuid(dev_item),
4523 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004524 read_extent_buffer(leaf, fs_uuid,
4525 (unsigned long)btrfs_device_fsid(dev_item),
4526 BTRFS_UUID_SIZE);
4527
4528 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4529 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004530 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004531 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004532 }
4533
4534 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4535 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004536 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004537 return -EIO;
4538
4539 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004540 printk(KERN_WARNING "warning devid %llu missing\n",
4541 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004542 device = add_missing_dev(root, devid, dev_uuid);
4543 if (!device)
4544 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004545 } else if (!device->missing) {
4546 /*
4547 * this happens when a device that was properly setup
4548 * in the device info lists suddenly goes bad.
4549 * device->bdev is NULL, and so we have to set
4550 * device->missing to one here
4551 */
4552 root->fs_info->fs_devices->missing_devices++;
4553 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004554 }
4555 }
4556
4557 if (device->fs_devices != root->fs_info->fs_devices) {
4558 BUG_ON(device->writeable);
4559 if (device->generation !=
4560 btrfs_device_generation(leaf, dev_item))
4561 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004562 }
Chris Mason0b86a832008-03-24 15:01:56 -04004563
4564 fill_device_from_item(leaf, dev_item, device);
4565 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004566 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004567 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004568 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004569 spin_lock(&root->fs_info->free_chunk_lock);
4570 root->fs_info->free_chunk_space += device->total_bytes -
4571 device->bytes_used;
4572 spin_unlock(&root->fs_info->free_chunk_lock);
4573 }
Chris Mason0b86a832008-03-24 15:01:56 -04004574 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004575 return ret;
4576}
4577
Yan Zhenge4404d62008-12-12 10:03:26 -05004578int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004579{
David Sterba6c417612011-04-13 15:41:04 +02004580 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004581 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004582 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004583 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004584 u8 *ptr;
4585 unsigned long sb_ptr;
4586 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004587 u32 num_stripes;
4588 u32 array_size;
4589 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004590 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004591 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004592
Yan Zhenge4404d62008-12-12 10:03:26 -05004593 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004594 BTRFS_SUPER_INFO_SIZE);
4595 if (!sb)
4596 return -ENOMEM;
4597 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004598 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004599 /*
4600 * The sb extent buffer is artifical and just used to read the system array.
4601 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4602 * pages up-to-date when the page is larger: extent does not cover the
4603 * whole page and consequently check_page_uptodate does not find all
4604 * the page's extents up-to-date (the hole beyond sb),
4605 * write_extent_buffer then triggers a WARN_ON.
4606 *
4607 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4608 * but sb spans only this function. Add an explicit SetPageUptodate call
4609 * to silence the warning eg. on PowerPC 64.
4610 */
4611 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004612 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004613
Chris Masona061fc82008-05-07 11:43:44 -04004614 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004615 array_size = btrfs_super_sys_array_size(super_copy);
4616
Chris Mason0b86a832008-03-24 15:01:56 -04004617 ptr = super_copy->sys_chunk_array;
4618 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4619 cur = 0;
4620
4621 while (cur < array_size) {
4622 disk_key = (struct btrfs_disk_key *)ptr;
4623 btrfs_disk_key_to_cpu(&key, disk_key);
4624
Chris Masona061fc82008-05-07 11:43:44 -04004625 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004626 sb_ptr += len;
4627 cur += len;
4628
Chris Mason0d81ba52008-03-24 15:02:07 -04004629 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004630 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004631 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004632 if (ret)
4633 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004634 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4635 len = btrfs_chunk_item_size(num_stripes);
4636 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004637 ret = -EIO;
4638 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004639 }
4640 ptr += len;
4641 sb_ptr += len;
4642 cur += len;
4643 }
Chris Masona061fc82008-05-07 11:43:44 -04004644 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004645 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004646}
4647
4648int btrfs_read_chunk_tree(struct btrfs_root *root)
4649{
4650 struct btrfs_path *path;
4651 struct extent_buffer *leaf;
4652 struct btrfs_key key;
4653 struct btrfs_key found_key;
4654 int ret;
4655 int slot;
4656
4657 root = root->fs_info->chunk_root;
4658
4659 path = btrfs_alloc_path();
4660 if (!path)
4661 return -ENOMEM;
4662
Li Zefanb367e472011-12-07 11:38:24 +08004663 mutex_lock(&uuid_mutex);
4664 lock_chunks(root);
4665
Chris Mason0b86a832008-03-24 15:01:56 -04004666 /* first we search for all of the device items, and then we
4667 * read in all of the chunk items. This way we can create chunk
4668 * mappings that reference all of the devices that are afound
4669 */
4670 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4671 key.offset = 0;
4672 key.type = 0;
4673again:
4674 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004675 if (ret < 0)
4676 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004677 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004678 leaf = path->nodes[0];
4679 slot = path->slots[0];
4680 if (slot >= btrfs_header_nritems(leaf)) {
4681 ret = btrfs_next_leaf(root, path);
4682 if (ret == 0)
4683 continue;
4684 if (ret < 0)
4685 goto error;
4686 break;
4687 }
4688 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4689 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4690 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4691 break;
4692 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4693 struct btrfs_dev_item *dev_item;
4694 dev_item = btrfs_item_ptr(leaf, slot,
4695 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004696 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004697 if (ret)
4698 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004699 }
4700 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4701 struct btrfs_chunk *chunk;
4702 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4703 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004704 if (ret)
4705 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004706 }
4707 path->slots[0]++;
4708 }
4709 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4710 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004711 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004712 goto again;
4713 }
Chris Mason0b86a832008-03-24 15:01:56 -04004714 ret = 0;
4715error:
Li Zefanb367e472011-12-07 11:38:24 +08004716 unlock_chunks(root);
4717 mutex_unlock(&uuid_mutex);
4718
Yan Zheng2b820322008-11-17 21:11:30 -05004719 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004720 return ret;
4721}
Stefan Behrens442a4f62012-05-25 16:06:08 +02004722
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004723static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
4724{
4725 int i;
4726
4727 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4728 btrfs_dev_stat_reset(dev, i);
4729}
4730
4731int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
4732{
4733 struct btrfs_key key;
4734 struct btrfs_key found_key;
4735 struct btrfs_root *dev_root = fs_info->dev_root;
4736 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4737 struct extent_buffer *eb;
4738 int slot;
4739 int ret = 0;
4740 struct btrfs_device *device;
4741 struct btrfs_path *path = NULL;
4742 int i;
4743
4744 path = btrfs_alloc_path();
4745 if (!path) {
4746 ret = -ENOMEM;
4747 goto out;
4748 }
4749
4750 mutex_lock(&fs_devices->device_list_mutex);
4751 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4752 int item_size;
4753 struct btrfs_dev_stats_item *ptr;
4754
4755 key.objectid = 0;
4756 key.type = BTRFS_DEV_STATS_KEY;
4757 key.offset = device->devid;
4758 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
4759 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004760 __btrfs_reset_dev_stats(device);
4761 device->dev_stats_valid = 1;
4762 btrfs_release_path(path);
4763 continue;
4764 }
4765 slot = path->slots[0];
4766 eb = path->nodes[0];
4767 btrfs_item_key_to_cpu(eb, &found_key, slot);
4768 item_size = btrfs_item_size_nr(eb, slot);
4769
4770 ptr = btrfs_item_ptr(eb, slot,
4771 struct btrfs_dev_stats_item);
4772
4773 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4774 if (item_size >= (1 + i) * sizeof(__le64))
4775 btrfs_dev_stat_set(device, i,
4776 btrfs_dev_stats_value(eb, ptr, i));
4777 else
4778 btrfs_dev_stat_reset(device, i);
4779 }
4780
4781 device->dev_stats_valid = 1;
4782 btrfs_dev_stat_print_on_load(device);
4783 btrfs_release_path(path);
4784 }
4785 mutex_unlock(&fs_devices->device_list_mutex);
4786
4787out:
4788 btrfs_free_path(path);
4789 return ret < 0 ? ret : 0;
4790}
4791
4792static int update_dev_stat_item(struct btrfs_trans_handle *trans,
4793 struct btrfs_root *dev_root,
4794 struct btrfs_device *device)
4795{
4796 struct btrfs_path *path;
4797 struct btrfs_key key;
4798 struct extent_buffer *eb;
4799 struct btrfs_dev_stats_item *ptr;
4800 int ret;
4801 int i;
4802
4803 key.objectid = 0;
4804 key.type = BTRFS_DEV_STATS_KEY;
4805 key.offset = device->devid;
4806
4807 path = btrfs_alloc_path();
4808 BUG_ON(!path);
4809 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
4810 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004811 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
4812 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004813 goto out;
4814 }
4815
4816 if (ret == 0 &&
4817 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
4818 /* need to delete old one and insert a new one */
4819 ret = btrfs_del_item(trans, dev_root, path);
4820 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004821 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
4822 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004823 goto out;
4824 }
4825 ret = 1;
4826 }
4827
4828 if (ret == 1) {
4829 /* need to insert a new item */
4830 btrfs_release_path(path);
4831 ret = btrfs_insert_empty_item(trans, dev_root, path,
4832 &key, sizeof(*ptr));
4833 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04004834 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
4835 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004836 goto out;
4837 }
4838 }
4839
4840 eb = path->nodes[0];
4841 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
4842 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4843 btrfs_set_dev_stats_value(eb, ptr, i,
4844 btrfs_dev_stat_read(device, i));
4845 btrfs_mark_buffer_dirty(eb);
4846
4847out:
4848 btrfs_free_path(path);
4849 return ret;
4850}
4851
4852/*
4853 * called from commit_transaction. Writes all changed device stats to disk.
4854 */
4855int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
4856 struct btrfs_fs_info *fs_info)
4857{
4858 struct btrfs_root *dev_root = fs_info->dev_root;
4859 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
4860 struct btrfs_device *device;
4861 int ret = 0;
4862
4863 mutex_lock(&fs_devices->device_list_mutex);
4864 list_for_each_entry(device, &fs_devices->devices, dev_list) {
4865 if (!device->dev_stats_valid || !device->dev_stats_dirty)
4866 continue;
4867
4868 ret = update_dev_stat_item(trans, dev_root, device);
4869 if (!ret)
4870 device->dev_stats_dirty = 0;
4871 }
4872 mutex_unlock(&fs_devices->device_list_mutex);
4873
4874 return ret;
4875}
4876
Stefan Behrens442a4f62012-05-25 16:06:08 +02004877void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
4878{
4879 btrfs_dev_stat_inc(dev, index);
4880 btrfs_dev_stat_print_on_error(dev);
4881}
4882
4883void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
4884{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004885 if (!dev->dev_stats_valid)
4886 return;
Josef Bacik606686e2012-06-04 14:03:51 -04004887 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02004888 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04004889 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02004890 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4891 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4892 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4893 btrfs_dev_stat_read(dev,
4894 BTRFS_DEV_STAT_CORRUPTION_ERRS),
4895 btrfs_dev_stat_read(dev,
4896 BTRFS_DEV_STAT_GENERATION_ERRS));
4897}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004898
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004899static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
4900{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06004901 int i;
4902
4903 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4904 if (btrfs_dev_stat_read(dev, i) != 0)
4905 break;
4906 if (i == BTRFS_DEV_STAT_VALUES_MAX)
4907 return; /* all values == 0, suppress message */
4908
Josef Bacik606686e2012-06-04 14:03:51 -04004909 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
4910 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004911 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4912 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4913 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4914 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
4915 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
4916}
4917
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004918int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06004919 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004920{
4921 struct btrfs_device *dev;
4922 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4923 int i;
4924
4925 mutex_lock(&fs_devices->device_list_mutex);
4926 dev = btrfs_find_device(root, stats->devid, NULL, NULL);
4927 mutex_unlock(&fs_devices->device_list_mutex);
4928
4929 if (!dev) {
4930 printk(KERN_WARNING
4931 "btrfs: get dev_stats failed, device not found\n");
4932 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02004933 } else if (!dev->dev_stats_valid) {
4934 printk(KERN_WARNING
4935 "btrfs: get dev_stats failed, not yet valid\n");
4936 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06004937 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004938 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4939 if (stats->nr_items > i)
4940 stats->values[i] =
4941 btrfs_dev_stat_read_and_reset(dev, i);
4942 else
4943 btrfs_dev_stat_reset(dev, i);
4944 }
4945 } else {
4946 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4947 if (stats->nr_items > i)
4948 stats->values[i] = btrfs_dev_stat_read(dev, i);
4949 }
4950 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
4951 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
4952 return 0;
4953}