blob: a112b758822e2ac772248aea1a0d24f5a549d799 [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"
Chris Mason0b86a832008-03-24 15:01:56 -040038
Yan Zheng2b820322008-11-17 21:11:30 -050039static int init_first_rw_device(struct btrfs_trans_handle *trans,
40 struct btrfs_root *root,
41 struct btrfs_device *device);
42static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
43
Chris Mason8a4b83c2008-03-24 15:02:07 -040044static DEFINE_MUTEX(uuid_mutex);
45static LIST_HEAD(fs_uuids);
46
Chris Mason7d9eb122008-07-08 14:19:17 -040047static void lock_chunks(struct btrfs_root *root)
48{
Chris Mason7d9eb122008-07-08 14:19:17 -040049 mutex_lock(&root->fs_info->chunk_mutex);
50}
51
52static void unlock_chunks(struct btrfs_root *root)
53{
Chris Mason7d9eb122008-07-08 14:19:17 -040054 mutex_unlock(&root->fs_info->chunk_mutex);
55}
56
Yan Zhenge4404d62008-12-12 10:03:26 -050057static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
58{
59 struct btrfs_device *device;
60 WARN_ON(fs_devices->opened);
61 while (!list_empty(&fs_devices->devices)) {
62 device = list_entry(fs_devices->devices.next,
63 struct btrfs_device, dev_list);
64 list_del(&device->dev_list);
65 kfree(device->name);
66 kfree(device);
67 }
68 kfree(fs_devices);
69}
70
Jeff Mahoney143bede2012-03-01 14:56:26 +010071void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040072{
73 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040074
Yan Zheng2b820322008-11-17 21:11:30 -050075 while (!list_empty(&fs_uuids)) {
76 fs_devices = list_entry(fs_uuids.next,
77 struct btrfs_fs_devices, list);
78 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050079 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040080 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040081}
82
Chris Masona1b32a52008-09-05 16:09:51 -040083static noinline struct btrfs_device *__find_device(struct list_head *head,
84 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040085{
86 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040087
Qinghuang Fengc6e30872009-01-21 10:59:08 -050088 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040089 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040090 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040091 return dev;
Chris Masona4437552008-04-18 10:29:38 -040092 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040093 }
94 return NULL;
95}
96
Chris Masona1b32a52008-09-05 16:09:51 -040097static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040098{
Chris Mason8a4b83c2008-03-24 15:02:07 -040099 struct btrfs_fs_devices *fs_devices;
100
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500101 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
103 return fs_devices;
104 }
105 return NULL;
106}
107
Chris Masonffbd5172009-04-20 15:50:09 -0400108static void requeue_list(struct btrfs_pending_bios *pending_bios,
109 struct bio *head, struct bio *tail)
110{
111
112 struct bio *old_head;
113
114 old_head = pending_bios->head;
115 pending_bios->head = head;
116 if (pending_bios->tail)
117 tail->bi_next = old_head;
118 else
119 pending_bios->tail = tail;
120}
121
Chris Mason8b712842008-06-11 16:50:36 -0400122/*
123 * we try to collect pending bios for a device so we don't get a large
124 * number of procs sending bios down to the same device. This greatly
125 * improves the schedulers ability to collect and merge the bios.
126 *
127 * But, it also turns into a long list of bios to process and that is sure
128 * to eventually make the worker thread block. The solution here is to
129 * make some progress and then put this work struct back at the end of
130 * the list if the block device is congested. This way, multiple devices
131 * can make progress from a single worker thread.
132 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100133static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400134{
135 struct bio *pending;
136 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400137 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400138 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400139 struct bio *tail;
140 struct bio *cur;
141 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400142 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400143 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400144 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400145 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400146 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000147 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400148 struct blk_plug plug;
149
150 /*
151 * this function runs all the bios we've collected for
152 * a particular device. We don't want to wander off to
153 * another device without first sending all of these down.
154 * So, setup a plug here and finish it off before we return
155 */
156 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400157
Chris Masonbedf7622009-04-03 10:32:58 -0400158 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400159 fs_info = device->dev_root->fs_info;
160 limit = btrfs_async_submit_limit(fs_info);
161 limit = limit * 2 / 3;
162
Chris Mason8b712842008-06-11 16:50:36 -0400163loop:
164 spin_lock(&device->io_lock);
165
Chris Masona6837052009-02-04 09:19:41 -0500166loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400167 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400168
Chris Mason8b712842008-06-11 16:50:36 -0400169 /* take all the bios off the list at once and process them
170 * later on (without the lock held). But, remember the
171 * tail and other pointers so the bios can be properly reinserted
172 * into the list if we hit congestion
173 */
Chris Masond84275c2009-06-09 15:39:08 -0400174 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400175 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400176 force_reg = 1;
177 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400178 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400179 force_reg = 0;
180 }
Chris Masonffbd5172009-04-20 15:50:09 -0400181
182 pending = pending_bios->head;
183 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400184 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400185
186 /*
187 * if pending was null this time around, no bios need processing
188 * at all and we can stop. Otherwise it'll loop back up again
189 * and do an additional check so no bios are missed.
190 *
191 * device->running_pending is used to synchronize with the
192 * schedule_bio code.
193 */
Chris Masonffbd5172009-04-20 15:50:09 -0400194 if (device->pending_sync_bios.head == NULL &&
195 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400196 again = 0;
197 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400198 } else {
199 again = 1;
200 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400201 }
Chris Masonffbd5172009-04-20 15:50:09 -0400202
203 pending_bios->head = NULL;
204 pending_bios->tail = NULL;
205
Chris Mason8b712842008-06-11 16:50:36 -0400206 spin_unlock(&device->io_lock);
207
Chris Masond3977122009-01-05 21:25:51 -0500208 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400209
210 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400211 /* we want to work on both lists, but do more bios on the
212 * sync list than the regular list
213 */
214 if ((num_run > 32 &&
215 pending_bios != &device->pending_sync_bios &&
216 device->pending_sync_bios.head) ||
217 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
218 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400219 spin_lock(&device->io_lock);
220 requeue_list(pending_bios, pending, tail);
221 goto loop_lock;
222 }
223
Chris Mason8b712842008-06-11 16:50:36 -0400224 cur = pending;
225 pending = pending->bi_next;
226 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400227 atomic_dec(&fs_info->nr_async_bios);
228
229 if (atomic_read(&fs_info->nr_async_bios) < limit &&
230 waitqueue_active(&fs_info->async_submit_wait))
231 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400232
233 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400234
Chris Mason2ab1ba62011-08-04 14:28:36 -0400235 /*
236 * if we're doing the sync list, record that our
237 * plug has some sync requests on it
238 *
239 * If we're doing the regular list and there are
240 * sync requests sitting around, unplug before
241 * we add more
242 */
243 if (pending_bios == &device->pending_sync_bios) {
244 sync_pending = 1;
245 } else if (sync_pending) {
246 blk_finish_plug(&plug);
247 blk_start_plug(&plug);
248 sync_pending = 0;
249 }
250
Stefan Behrens21adbd52011-11-09 13:44:05 +0100251 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400252 num_run++;
253 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100254 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400255 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400256
257 /*
258 * we made progress, there is more work to do and the bdi
259 * is now congested. Back off and let other work structs
260 * run instead
261 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400262 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500263 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400264 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400265
Chris Masonb765ead2009-04-03 10:27:10 -0400266 ioc = current->io_context;
267
268 /*
269 * the main goal here is that we don't want to
270 * block if we're going to be able to submit
271 * more requests without blocking.
272 *
273 * This code does two great things, it pokes into
274 * the elevator code from a filesystem _and_
275 * it makes assumptions about how batching works.
276 */
277 if (ioc && ioc->nr_batch_requests > 0 &&
278 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
279 (last_waited == 0 ||
280 ioc->last_waited == last_waited)) {
281 /*
282 * we want to go through our batch of
283 * requests and stop. So, we copy out
284 * the ioc->last_waited time and test
285 * against it before looping
286 */
287 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100288 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400289 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400290 continue;
291 }
Chris Mason8b712842008-06-11 16:50:36 -0400292 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400293 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500294 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400295
296 spin_unlock(&device->io_lock);
297 btrfs_requeue_work(&device->work);
298 goto done;
299 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500300 /* unplug every 64 requests just for good measure */
301 if (batch_run % 64 == 0) {
302 blk_finish_plug(&plug);
303 blk_start_plug(&plug);
304 sync_pending = 0;
305 }
Chris Mason8b712842008-06-11 16:50:36 -0400306 }
Chris Masonffbd5172009-04-20 15:50:09 -0400307
Chris Mason51684082010-03-10 15:33:32 -0500308 cond_resched();
309 if (again)
310 goto loop;
311
312 spin_lock(&device->io_lock);
313 if (device->pending_bios.head || device->pending_sync_bios.head)
314 goto loop_lock;
315 spin_unlock(&device->io_lock);
316
Chris Mason8b712842008-06-11 16:50:36 -0400317done:
Chris Mason211588a2011-04-19 20:12:40 -0400318 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400319}
320
Christoph Hellwigb2950862008-12-02 09:54:17 -0500321static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400322{
323 struct btrfs_device *device;
324
325 device = container_of(work, struct btrfs_device, work);
326 run_scheduled_bios(device);
327}
328
Chris Masona1b32a52008-09-05 16:09:51 -0400329static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400330 struct btrfs_super_block *disk_super,
331 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
332{
333 struct btrfs_device *device;
334 struct btrfs_fs_devices *fs_devices;
335 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000336 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400337
338 fs_devices = find_fsid(disk_super->fsid);
339 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400340 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400341 if (!fs_devices)
342 return -ENOMEM;
343 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400344 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400345 list_add(&fs_devices->list, &fs_uuids);
346 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
347 fs_devices->latest_devid = devid;
348 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400349 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400350 device = NULL;
351 } else {
Chris Masona4437552008-04-18 10:29:38 -0400352 device = __find_device(&fs_devices->devices, devid,
353 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400354 }
355 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500356 if (fs_devices->opened)
357 return -EBUSY;
358
Chris Mason8a4b83c2008-03-24 15:02:07 -0400359 device = kzalloc(sizeof(*device), GFP_NOFS);
360 if (!device) {
361 /* we can safely leave the fs_devices entry around */
362 return -ENOMEM;
363 }
364 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400365 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400366 memcpy(device->uuid, disk_super->dev_item.uuid,
367 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400368 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400369 device->name = kstrdup(path, GFP_NOFS);
370 if (!device->name) {
371 kfree(device);
372 return -ENOMEM;
373 }
Yan Zheng2b820322008-11-17 21:11:30 -0500374 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400375
Arne Jansen90519d62011-05-23 14:30:00 +0200376 /* init readahead state */
377 spin_lock_init(&device->reada_lock);
378 device->reada_curr_zone = NULL;
379 atomic_set(&device->reada_in_flight, 0);
380 device->reada_next = 0;
381 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
382 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
383
Chris Masone5e9a522009-06-10 15:17:02 -0400384 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000385 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400386 mutex_unlock(&fs_devices->device_list_mutex);
387
Yan Zheng2b820322008-11-17 21:11:30 -0500388 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400389 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500390 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000391 name = kstrdup(path, GFP_NOFS);
392 if (!name)
393 return -ENOMEM;
394 kfree(device->name);
395 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500396 if (device->missing) {
397 fs_devices->missing_devices--;
398 device->missing = 0;
399 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400400 }
401
402 if (found_transid > fs_devices->latest_trans) {
403 fs_devices->latest_devid = devid;
404 fs_devices->latest_trans = found_transid;
405 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400406 *fs_devices_ret = fs_devices;
407 return 0;
408}
409
Yan Zhenge4404d62008-12-12 10:03:26 -0500410static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
411{
412 struct btrfs_fs_devices *fs_devices;
413 struct btrfs_device *device;
414 struct btrfs_device *orig_dev;
415
416 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
417 if (!fs_devices)
418 return ERR_PTR(-ENOMEM);
419
420 INIT_LIST_HEAD(&fs_devices->devices);
421 INIT_LIST_HEAD(&fs_devices->alloc_list);
422 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400423 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500424 fs_devices->latest_devid = orig->latest_devid;
425 fs_devices->latest_trans = orig->latest_trans;
426 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
427
Xiao Guangrong46224702011-04-20 10:08:47 +0000428 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500429 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
430 device = kzalloc(sizeof(*device), GFP_NOFS);
431 if (!device)
432 goto error;
433
434 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400435 if (!device->name) {
436 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500437 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400438 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500439
440 device->devid = orig_dev->devid;
441 device->work.func = pending_bios_fn;
442 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500443 spin_lock_init(&device->io_lock);
444 INIT_LIST_HEAD(&device->dev_list);
445 INIT_LIST_HEAD(&device->dev_alloc_list);
446
447 list_add(&device->dev_list, &fs_devices->devices);
448 device->fs_devices = fs_devices;
449 fs_devices->num_devices++;
450 }
451 return fs_devices;
452error:
453 free_fs_devices(fs_devices);
454 return ERR_PTR(-ENOMEM);
455}
456
Jeff Mahoney143bede2012-03-01 14:56:26 +0100457void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
Chris Masondfe25022008-05-13 13:46:40 -0400458{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500459 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400460
Chris Masona6b0d5c2012-02-20 20:53:43 -0500461 struct block_device *latest_bdev = NULL;
462 u64 latest_devid = 0;
463 u64 latest_transid = 0;
464
Chris Masondfe25022008-05-13 13:46:40 -0400465 mutex_lock(&uuid_mutex);
466again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000467 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500468 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500469 if (device->in_fs_metadata) {
470 if (!latest_transid ||
471 device->generation > latest_transid) {
472 latest_devid = device->devid;
473 latest_transid = device->generation;
474 latest_bdev = device->bdev;
475 }
Yan Zheng2b820322008-11-17 21:11:30 -0500476 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500477 }
Yan Zheng2b820322008-11-17 21:11:30 -0500478
479 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100480 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500481 device->bdev = NULL;
482 fs_devices->open_devices--;
483 }
484 if (device->writeable) {
485 list_del_init(&device->dev_alloc_list);
486 device->writeable = 0;
487 fs_devices->rw_devices--;
488 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500489 list_del_init(&device->dev_list);
490 fs_devices->num_devices--;
491 kfree(device->name);
492 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400493 }
Yan Zheng2b820322008-11-17 21:11:30 -0500494
495 if (fs_devices->seed) {
496 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500497 goto again;
498 }
499
Chris Masona6b0d5c2012-02-20 20:53:43 -0500500 fs_devices->latest_bdev = latest_bdev;
501 fs_devices->latest_devid = latest_devid;
502 fs_devices->latest_trans = latest_transid;
503
Chris Masondfe25022008-05-13 13:46:40 -0400504 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400505}
Chris Masona0af4692008-05-13 16:03:06 -0400506
Xiao Guangrong1f781602011-04-20 10:09:16 +0000507static void __free_device(struct work_struct *work)
508{
509 struct btrfs_device *device;
510
511 device = container_of(work, struct btrfs_device, rcu_work);
512
513 if (device->bdev)
514 blkdev_put(device->bdev, device->mode);
515
516 kfree(device->name);
517 kfree(device);
518}
519
520static void free_device(struct rcu_head *head)
521{
522 struct btrfs_device *device;
523
524 device = container_of(head, struct btrfs_device, rcu);
525
526 INIT_WORK(&device->rcu_work, __free_device);
527 schedule_work(&device->rcu_work);
528}
529
Yan Zheng2b820322008-11-17 21:11:30 -0500530static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400531{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400532 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500533
Yan Zheng2b820322008-11-17 21:11:30 -0500534 if (--fs_devices->opened > 0)
535 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400536
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000537 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500538 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000539 struct btrfs_device *new_device;
540
541 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400542 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000543
Yan Zheng2b820322008-11-17 21:11:30 -0500544 if (device->writeable) {
545 list_del_init(&device->dev_alloc_list);
546 fs_devices->rw_devices--;
547 }
548
Josef Bacikd5e20032011-08-04 14:52:27 +0000549 if (device->can_discard)
550 fs_devices->num_can_discard--;
551
Xiao Guangrong1f781602011-04-20 10:09:16 +0000552 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100553 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000554 memcpy(new_device, device, sizeof(*new_device));
555 new_device->name = kstrdup(device->name, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100556 BUG_ON(device->name && !new_device->name); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000557 new_device->bdev = NULL;
558 new_device->writeable = 0;
559 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000560 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000561 list_replace_rcu(&device->dev_list, &new_device->dev_list);
562
563 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400564 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000565 mutex_unlock(&fs_devices->device_list_mutex);
566
Yan Zhenge4404d62008-12-12 10:03:26 -0500567 WARN_ON(fs_devices->open_devices);
568 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500569 fs_devices->opened = 0;
570 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500571
Chris Mason8a4b83c2008-03-24 15:02:07 -0400572 return 0;
573}
574
Yan Zheng2b820322008-11-17 21:11:30 -0500575int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
576{
Yan Zhenge4404d62008-12-12 10:03:26 -0500577 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500578 int ret;
579
580 mutex_lock(&uuid_mutex);
581 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500582 if (!fs_devices->opened) {
583 seed_devices = fs_devices->seed;
584 fs_devices->seed = NULL;
585 }
Yan Zheng2b820322008-11-17 21:11:30 -0500586 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500587
588 while (seed_devices) {
589 fs_devices = seed_devices;
590 seed_devices = fs_devices->seed;
591 __btrfs_close_devices(fs_devices);
592 free_fs_devices(fs_devices);
593 }
Yan Zheng2b820322008-11-17 21:11:30 -0500594 return ret;
595}
596
Yan Zhenge4404d62008-12-12 10:03:26 -0500597static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
598 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400599{
Josef Bacikd5e20032011-08-04 14:52:27 +0000600 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400601 struct block_device *bdev;
602 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400603 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400604 struct block_device *latest_bdev = NULL;
605 struct buffer_head *bh;
606 struct btrfs_super_block *disk_super;
607 u64 latest_devid = 0;
608 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400609 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500610 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400611 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400612
Tejun Heod4d77622010-11-13 11:55:18 +0100613 flags |= FMODE_EXCL;
614
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500615 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400616 if (device->bdev)
617 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400618 if (!device->name)
619 continue;
620
Tejun Heod4d77622010-11-13 11:55:18 +0100621 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400622 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500623 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400624 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400625 }
Chris Mason3c4bb262012-03-27 18:56:56 -0400626 filemap_write_and_wait(bdev->bd_inode->i_mapping);
627 invalidate_bdev(bdev);
Chris Masona061fc82008-05-07 11:43:44 -0400628 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400629
Yan Zhenga512bbf2008-12-08 16:46:26 -0500630 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300631 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400632 goto error_close;
633
634 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000635 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400636 if (devid != device->devid)
637 goto error_brelse;
638
Yan Zheng2b820322008-11-17 21:11:30 -0500639 if (memcmp(device->uuid, disk_super->dev_item.uuid,
640 BTRFS_UUID_SIZE))
641 goto error_brelse;
642
643 device->generation = btrfs_super_generation(disk_super);
644 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400645 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500646 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400647 latest_bdev = bdev;
648 }
649
Yan Zheng2b820322008-11-17 21:11:30 -0500650 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
651 device->writeable = 0;
652 } else {
653 device->writeable = !bdev_read_only(bdev);
654 seeding = 0;
655 }
656
Josef Bacikd5e20032011-08-04 14:52:27 +0000657 q = bdev_get_queue(bdev);
658 if (blk_queue_discard(q)) {
659 device->can_discard = 1;
660 fs_devices->num_can_discard++;
661 }
662
Chris Mason8a4b83c2008-03-24 15:02:07 -0400663 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400664 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500665 device->mode = flags;
666
Chris Masonc2898112009-06-10 09:51:32 -0400667 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
668 fs_devices->rotating = 1;
669
Chris Masona0af4692008-05-13 16:03:06 -0400670 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500671 if (device->writeable) {
672 fs_devices->rw_devices++;
673 list_add(&device->dev_alloc_list,
674 &fs_devices->alloc_list);
675 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000676 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400677 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400678
Chris Masona0af4692008-05-13 16:03:06 -0400679error_brelse:
680 brelse(bh);
681error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100682 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400683error:
684 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400685 }
Chris Masona0af4692008-05-13 16:03:06 -0400686 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300687 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400688 goto out;
689 }
Yan Zheng2b820322008-11-17 21:11:30 -0500690 fs_devices->seeding = seeding;
691 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400692 fs_devices->latest_bdev = latest_bdev;
693 fs_devices->latest_devid = latest_devid;
694 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500695 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400696out:
Yan Zheng2b820322008-11-17 21:11:30 -0500697 return ret;
698}
699
700int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500701 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500702{
703 int ret;
704
705 mutex_lock(&uuid_mutex);
706 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500707 fs_devices->opened++;
708 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500709 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500710 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500711 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400712 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400713 return ret;
714}
715
Christoph Hellwig97288f22008-12-02 06:36:09 -0500716int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400717 struct btrfs_fs_devices **fs_devices_ret)
718{
719 struct btrfs_super_block *disk_super;
720 struct block_device *bdev;
721 struct buffer_head *bh;
722 int ret;
723 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400724 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400725
Tejun Heod4d77622010-11-13 11:55:18 +0100726 flags |= FMODE_EXCL;
727 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400728
729 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400730 ret = PTR_ERR(bdev);
731 goto error;
732 }
733
Al Viro10f63272011-11-17 15:05:22 -0500734 mutex_lock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400735 ret = set_blocksize(bdev, 4096);
736 if (ret)
737 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500738 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400739 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000740 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400741 goto error_close;
742 }
743 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000744 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400745 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400746 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500747 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200748 else
749 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500750 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500751 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400752 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
753
Chris Mason8a4b83c2008-03-24 15:02:07 -0400754 brelse(bh);
755error_close:
Al Viro10f63272011-11-17 15:05:22 -0500756 mutex_unlock(&uuid_mutex);
Tejun Heod4d77622010-11-13 11:55:18 +0100757 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758error:
Chris Mason8a4b83c2008-03-24 15:02:07 -0400759 return ret;
760}
Chris Mason0b86a832008-03-24 15:01:56 -0400761
Miao Xie6d07bce2011-01-05 10:07:31 +0000762/* helper to account the used device space in the range */
763int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
764 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400765{
766 struct btrfs_key key;
767 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000768 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500769 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000770 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400771 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000772 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400773 struct extent_buffer *l;
774
Miao Xie6d07bce2011-01-05 10:07:31 +0000775 *length = 0;
776
777 if (start >= device->total_bytes)
778 return 0;
779
Yan Zheng2b820322008-11-17 21:11:30 -0500780 path = btrfs_alloc_path();
781 if (!path)
782 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400783 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400784
Chris Mason0b86a832008-03-24 15:01:56 -0400785 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000786 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400787 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000788
789 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400790 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000791 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400792 if (ret > 0) {
793 ret = btrfs_previous_item(root, path, key.objectid, key.type);
794 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000795 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400796 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000797
Chris Mason0b86a832008-03-24 15:01:56 -0400798 while (1) {
799 l = path->nodes[0];
800 slot = path->slots[0];
801 if (slot >= btrfs_header_nritems(l)) {
802 ret = btrfs_next_leaf(root, path);
803 if (ret == 0)
804 continue;
805 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000806 goto out;
807
808 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400809 }
810 btrfs_item_key_to_cpu(l, &key, slot);
811
812 if (key.objectid < device->devid)
813 goto next;
814
815 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000816 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400817
Chris Masond3977122009-01-05 21:25:51 -0500818 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400819 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400820
Chris Mason0b86a832008-03-24 15:01:56 -0400821 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000822 extent_end = key.offset + btrfs_dev_extent_length(l,
823 dev_extent);
824 if (key.offset <= start && extent_end > end) {
825 *length = end - start + 1;
826 break;
827 } else if (key.offset <= start && extent_end > start)
828 *length += extent_end - start;
829 else if (key.offset > start && extent_end <= end)
830 *length += extent_end - key.offset;
831 else if (key.offset > start && key.offset <= end) {
832 *length += end - key.offset + 1;
833 break;
834 } else if (key.offset > end)
835 break;
836
837next:
838 path->slots[0]++;
839 }
840 ret = 0;
841out:
842 btrfs_free_path(path);
843 return ret;
844}
845
Chris Mason0b86a832008-03-24 15:01:56 -0400846/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000847 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000848 * @device: the device which we search the free space in
849 * @num_bytes: the size of the free space that we need
850 * @start: store the start of the free space.
851 * @len: the size of the free space. that we find, or the size of the max
852 * free space if we don't find suitable free space
853 *
Chris Mason0b86a832008-03-24 15:01:56 -0400854 * this uses a pretty simple search, the expectation is that it is
855 * called very infrequently and that a given device has a small number
856 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000857 *
858 * @start is used to store the start of the free space if we find. But if we
859 * don't find suitable free space, it will be used to store the start position
860 * of the max free space.
861 *
862 * @len is used to store the size of the free space that we find.
863 * But if we don't find suitable free space, it is used to store the size of
864 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400865 */
Li Zefan125ccb02011-12-08 15:07:24 +0800866int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000867 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400868{
869 struct btrfs_key key;
870 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000871 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400872 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000873 u64 hole_size;
874 u64 max_hole_start;
875 u64 max_hole_size;
876 u64 extent_end;
877 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400878 u64 search_end = device->total_bytes;
879 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000880 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400881 struct extent_buffer *l;
882
Chris Mason0b86a832008-03-24 15:01:56 -0400883 /* FIXME use last free of some kind */
884
885 /* we don't want to overwrite the superblock on the drive,
886 * so we make sure to start at an offset of at least 1MB
887 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200888 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400889
Miao Xie7bfc8372011-01-05 10:07:26 +0000890 max_hole_start = search_start;
891 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000892 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000893
894 if (search_start >= search_end) {
895 ret = -ENOSPC;
896 goto error;
897 }
898
899 path = btrfs_alloc_path();
900 if (!path) {
901 ret = -ENOMEM;
902 goto error;
903 }
904 path->reada = 2;
905
Chris Mason0b86a832008-03-24 15:01:56 -0400906 key.objectid = device->devid;
907 key.offset = search_start;
908 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000909
Li Zefan125ccb02011-12-08 15:07:24 +0800910 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400911 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000912 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400913 if (ret > 0) {
914 ret = btrfs_previous_item(root, path, key.objectid, key.type);
915 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000916 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400917 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000918
Chris Mason0b86a832008-03-24 15:01:56 -0400919 while (1) {
920 l = path->nodes[0];
921 slot = path->slots[0];
922 if (slot >= btrfs_header_nritems(l)) {
923 ret = btrfs_next_leaf(root, path);
924 if (ret == 0)
925 continue;
926 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000927 goto out;
928
929 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400930 }
931 btrfs_item_key_to_cpu(l, &key, slot);
932
933 if (key.objectid < device->devid)
934 goto next;
935
936 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000937 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400938
Chris Mason0b86a832008-03-24 15:01:56 -0400939 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
940 goto next;
941
Miao Xie7bfc8372011-01-05 10:07:26 +0000942 if (key.offset > search_start) {
943 hole_size = key.offset - search_start;
944
945 if (hole_size > max_hole_size) {
946 max_hole_start = search_start;
947 max_hole_size = hole_size;
948 }
949
950 /*
951 * If this free space is greater than which we need,
952 * it must be the max free space that we have found
953 * until now, so max_hole_start must point to the start
954 * of this free space and the length of this free space
955 * is stored in max_hole_size. Thus, we return
956 * max_hole_start and max_hole_size and go back to the
957 * caller.
958 */
959 if (hole_size >= num_bytes) {
960 ret = 0;
961 goto out;
962 }
963 }
964
Chris Mason0b86a832008-03-24 15:01:56 -0400965 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000966 extent_end = key.offset + btrfs_dev_extent_length(l,
967 dev_extent);
968 if (extent_end > search_start)
969 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400970next:
971 path->slots[0]++;
972 cond_resched();
973 }
Chris Mason0b86a832008-03-24 15:01:56 -0400974
liubo38c01b92011-08-02 02:39:03 +0000975 /*
976 * At this point, search_start should be the end of
977 * allocated dev extents, and when shrinking the device,
978 * search_end may be smaller than search_start.
979 */
980 if (search_end > search_start)
981 hole_size = search_end - search_start;
982
Miao Xie7bfc8372011-01-05 10:07:26 +0000983 if (hole_size > max_hole_size) {
984 max_hole_start = search_start;
985 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400986 }
Chris Mason0b86a832008-03-24 15:01:56 -0400987
Miao Xie7bfc8372011-01-05 10:07:26 +0000988 /* See above. */
989 if (hole_size < num_bytes)
990 ret = -ENOSPC;
991 else
992 ret = 0;
993
994out:
Yan Zheng2b820322008-11-17 21:11:30 -0500995 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000996error:
997 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000998 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000999 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001000 return ret;
1001}
1002
Christoph Hellwigb2950862008-12-02 09:54:17 -05001003static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001004 struct btrfs_device *device,
1005 u64 start)
1006{
1007 int ret;
1008 struct btrfs_path *path;
1009 struct btrfs_root *root = device->dev_root;
1010 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001011 struct btrfs_key found_key;
1012 struct extent_buffer *leaf = NULL;
1013 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001014
1015 path = btrfs_alloc_path();
1016 if (!path)
1017 return -ENOMEM;
1018
1019 key.objectid = device->devid;
1020 key.offset = start;
1021 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001022again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001023 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001024 if (ret > 0) {
1025 ret = btrfs_previous_item(root, path, key.objectid,
1026 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001027 if (ret)
1028 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001029 leaf = path->nodes[0];
1030 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1031 extent = btrfs_item_ptr(leaf, path->slots[0],
1032 struct btrfs_dev_extent);
1033 BUG_ON(found_key.offset > start || found_key.offset +
1034 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001035 key = found_key;
1036 btrfs_release_path(path);
1037 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001038 } else if (ret == 0) {
1039 leaf = path->nodes[0];
1040 extent = btrfs_item_ptr(leaf, path->slots[0],
1041 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001042 } else {
1043 btrfs_error(root->fs_info, ret, "Slot search failed");
1044 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001045 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001046
Josef Bacik2bf64752011-09-26 17:12:22 -04001047 if (device->bytes_used > 0) {
1048 u64 len = btrfs_dev_extent_length(leaf, extent);
1049 device->bytes_used -= len;
1050 spin_lock(&root->fs_info->free_chunk_lock);
1051 root->fs_info->free_chunk_space += len;
1052 spin_unlock(&root->fs_info->free_chunk_lock);
1053 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001054 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001055 if (ret) {
1056 btrfs_error(root->fs_info, ret,
1057 "Failed to remove dev extent item");
1058 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001059out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001060 btrfs_free_path(path);
1061 return ret;
1062}
1063
Yan Zheng2b820322008-11-17 21:11:30 -05001064int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001065 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001066 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001067 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001068{
1069 int ret;
1070 struct btrfs_path *path;
1071 struct btrfs_root *root = device->dev_root;
1072 struct btrfs_dev_extent *extent;
1073 struct extent_buffer *leaf;
1074 struct btrfs_key key;
1075
Chris Masondfe25022008-05-13 13:46:40 -04001076 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001077 path = btrfs_alloc_path();
1078 if (!path)
1079 return -ENOMEM;
1080
Chris Mason0b86a832008-03-24 15:01:56 -04001081 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001082 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001083 key.type = BTRFS_DEV_EXTENT_KEY;
1084 ret = btrfs_insert_empty_item(trans, root, path, &key,
1085 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001086 if (ret)
1087 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001088
1089 leaf = path->nodes[0];
1090 extent = btrfs_item_ptr(leaf, path->slots[0],
1091 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001092 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1093 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1094 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1095
1096 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1097 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1098 BTRFS_UUID_SIZE);
1099
Chris Mason0b86a832008-03-24 15:01:56 -04001100 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1101 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001102out:
Chris Mason0b86a832008-03-24 15:01:56 -04001103 btrfs_free_path(path);
1104 return ret;
1105}
1106
Chris Masona1b32a52008-09-05 16:09:51 -04001107static noinline int find_next_chunk(struct btrfs_root *root,
1108 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001109{
1110 struct btrfs_path *path;
1111 int ret;
1112 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001113 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001114 struct btrfs_key found_key;
1115
1116 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001117 if (!path)
1118 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001119
Chris Masone17cade2008-04-15 15:41:47 -04001120 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001121 key.offset = (u64)-1;
1122 key.type = BTRFS_CHUNK_ITEM_KEY;
1123
1124 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1125 if (ret < 0)
1126 goto error;
1127
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001128 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001129
1130 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1131 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001132 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001133 } else {
1134 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1135 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001136 if (found_key.objectid != objectid)
1137 *offset = 0;
1138 else {
1139 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1140 struct btrfs_chunk);
1141 *offset = found_key.offset +
1142 btrfs_chunk_length(path->nodes[0], chunk);
1143 }
Chris Mason0b86a832008-03-24 15:01:56 -04001144 }
1145 ret = 0;
1146error:
1147 btrfs_free_path(path);
1148 return ret;
1149}
1150
Yan Zheng2b820322008-11-17 21:11:30 -05001151static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001152{
1153 int ret;
1154 struct btrfs_key key;
1155 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001156 struct btrfs_path *path;
1157
1158 root = root->fs_info->chunk_root;
1159
1160 path = btrfs_alloc_path();
1161 if (!path)
1162 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001163
1164 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1165 key.type = BTRFS_DEV_ITEM_KEY;
1166 key.offset = (u64)-1;
1167
1168 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1169 if (ret < 0)
1170 goto error;
1171
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001172 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001173
1174 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1175 BTRFS_DEV_ITEM_KEY);
1176 if (ret) {
1177 *objectid = 1;
1178 } else {
1179 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1180 path->slots[0]);
1181 *objectid = found_key.offset + 1;
1182 }
1183 ret = 0;
1184error:
Yan Zheng2b820322008-11-17 21:11:30 -05001185 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001186 return ret;
1187}
1188
1189/*
1190 * the device information is stored in the chunk root
1191 * the btrfs_device struct should be fully filled in
1192 */
1193int btrfs_add_device(struct btrfs_trans_handle *trans,
1194 struct btrfs_root *root,
1195 struct btrfs_device *device)
1196{
1197 int ret;
1198 struct btrfs_path *path;
1199 struct btrfs_dev_item *dev_item;
1200 struct extent_buffer *leaf;
1201 struct btrfs_key key;
1202 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001203
1204 root = root->fs_info->chunk_root;
1205
1206 path = btrfs_alloc_path();
1207 if (!path)
1208 return -ENOMEM;
1209
Chris Mason0b86a832008-03-24 15:01:56 -04001210 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1211 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001212 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001213
1214 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001215 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001216 if (ret)
1217 goto out;
1218
1219 leaf = path->nodes[0];
1220 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1221
1222 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001223 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001224 btrfs_set_device_type(leaf, dev_item, device->type);
1225 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1226 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1227 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001228 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1229 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001230 btrfs_set_device_group(leaf, dev_item, 0);
1231 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1232 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001233 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001234
Chris Mason0b86a832008-03-24 15:01:56 -04001235 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001236 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001237 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1238 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001239 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001240
Yan Zheng2b820322008-11-17 21:11:30 -05001241 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001242out:
1243 btrfs_free_path(path);
1244 return ret;
1245}
Chris Mason8f18cf12008-04-25 16:53:30 -04001246
Chris Masona061fc82008-05-07 11:43:44 -04001247static int btrfs_rm_dev_item(struct btrfs_root *root,
1248 struct btrfs_device *device)
1249{
1250 int ret;
1251 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001252 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001253 struct btrfs_trans_handle *trans;
1254
1255 root = root->fs_info->chunk_root;
1256
1257 path = btrfs_alloc_path();
1258 if (!path)
1259 return -ENOMEM;
1260
Yan, Zhenga22285a2010-05-16 10:48:46 -04001261 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001262 if (IS_ERR(trans)) {
1263 btrfs_free_path(path);
1264 return PTR_ERR(trans);
1265 }
Chris Masona061fc82008-05-07 11:43:44 -04001266 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1267 key.type = BTRFS_DEV_ITEM_KEY;
1268 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001269 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001270
1271 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1272 if (ret < 0)
1273 goto out;
1274
1275 if (ret > 0) {
1276 ret = -ENOENT;
1277 goto out;
1278 }
1279
1280 ret = btrfs_del_item(trans, root, path);
1281 if (ret)
1282 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001283out:
1284 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001285 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001286 btrfs_commit_transaction(trans, root);
1287 return ret;
1288}
1289
1290int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1291{
1292 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001293 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001294 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001295 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001296 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001297 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001298 u64 all_avail;
1299 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001300 u64 num_devices;
1301 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001302 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001303 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001304
Chris Masona061fc82008-05-07 11:43:44 -04001305 mutex_lock(&uuid_mutex);
1306
1307 all_avail = root->fs_info->avail_data_alloc_bits |
1308 root->fs_info->avail_system_alloc_bits |
1309 root->fs_info->avail_metadata_alloc_bits;
1310
1311 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001312 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001313 printk(KERN_ERR "btrfs: unable to go below four devices "
1314 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001315 ret = -EINVAL;
1316 goto out;
1317 }
1318
1319 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001320 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001321 printk(KERN_ERR "btrfs: unable to go below two "
1322 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001323 ret = -EINVAL;
1324 goto out;
1325 }
1326
Chris Masondfe25022008-05-13 13:46:40 -04001327 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001328 struct list_head *devices;
1329 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001330
Chris Masondfe25022008-05-13 13:46:40 -04001331 device = NULL;
1332 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001333 /*
1334 * It is safe to read the devices since the volume_mutex
1335 * is held.
1336 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001337 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001338 if (tmp->in_fs_metadata && !tmp->bdev) {
1339 device = tmp;
1340 break;
1341 }
1342 }
1343 bdev = NULL;
1344 bh = NULL;
1345 disk_super = NULL;
1346 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001347 printk(KERN_ERR "btrfs: no missing devices found to "
1348 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001349 goto out;
1350 }
Chris Masondfe25022008-05-13 13:46:40 -04001351 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001352 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1353 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001354 if (IS_ERR(bdev)) {
1355 ret = PTR_ERR(bdev);
1356 goto out;
1357 }
1358
Yan Zheng2b820322008-11-17 21:11:30 -05001359 set_blocksize(bdev, 4096);
Chris Mason3c4bb262012-03-27 18:56:56 -04001360 invalidate_bdev(bdev);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001361 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001362 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001363 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001364 goto error_close;
1365 }
1366 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001367 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001368 dev_uuid = disk_super->dev_item.uuid;
1369 device = btrfs_find_device(root, devid, dev_uuid,
1370 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001371 if (!device) {
1372 ret = -ENOENT;
1373 goto error_brelse;
1374 }
Chris Masondfe25022008-05-13 13:46:40 -04001375 }
Yan Zheng2b820322008-11-17 21:11:30 -05001376
1377 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001378 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1379 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001380 ret = -EINVAL;
1381 goto error_brelse;
1382 }
1383
1384 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001385 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001386 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001387 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001388 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001389 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001390 }
Chris Masona061fc82008-05-07 11:43:44 -04001391
1392 ret = btrfs_shrink_device(device, 0);
1393 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001394 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001395
Chris Masona061fc82008-05-07 11:43:44 -04001396 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1397 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001398 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001399
Josef Bacik2bf64752011-09-26 17:12:22 -04001400 spin_lock(&root->fs_info->free_chunk_lock);
1401 root->fs_info->free_chunk_space = device->total_bytes -
1402 device->bytes_used;
1403 spin_unlock(&root->fs_info->free_chunk_lock);
1404
Yan Zheng2b820322008-11-17 21:11:30 -05001405 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001406 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001407
1408 /*
1409 * the device list mutex makes sure that we don't change
1410 * the device list while someone else is writing out all
1411 * the device supers.
1412 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001413
1414 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001415 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001416 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001417
Yan Zhenge4404d62008-12-12 10:03:26 -05001418 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001419
Chris Masoncd02dca2010-12-13 14:56:23 -05001420 if (device->missing)
1421 root->fs_info->fs_devices->missing_devices--;
1422
Yan Zheng2b820322008-11-17 21:11:30 -05001423 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1424 struct btrfs_device, dev_list);
1425 if (device->bdev == root->fs_info->sb->s_bdev)
1426 root->fs_info->sb->s_bdev = next_device->bdev;
1427 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1428 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1429
Xiao Guangrong1f781602011-04-20 10:09:16 +00001430 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001431 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001432
1433 call_rcu(&device->rcu, free_device);
1434 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001435
David Sterba6c417612011-04-13 15:41:04 +02001436 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1437 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001438
Xiao Guangrong1f781602011-04-20 10:09:16 +00001439 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001440 struct btrfs_fs_devices *fs_devices;
1441 fs_devices = root->fs_info->fs_devices;
1442 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001443 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001444 break;
1445 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001446 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001447 fs_devices->seed = cur_devices->seed;
1448 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001449 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001450 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001451 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001452 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001453 }
1454
1455 /*
1456 * at this point, the device is zero sized. We want to
1457 * remove it from the devices list and zero out the old super
1458 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001459 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001460 /* make sure this device isn't detected as part of
1461 * the FS anymore
1462 */
1463 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1464 set_buffer_dirty(bh);
1465 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001466 }
Chris Masona061fc82008-05-07 11:43:44 -04001467
Chris Masona061fc82008-05-07 11:43:44 -04001468 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001469
1470error_brelse:
1471 brelse(bh);
1472error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001473 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001474 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001475out:
1476 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001477 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001478error_undo:
1479 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001480 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001481 list_add(&device->dev_alloc_list,
1482 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001483 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001484 root->fs_info->fs_devices->rw_devices++;
1485 }
1486 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001487}
1488
Yan Zheng2b820322008-11-17 21:11:30 -05001489/*
1490 * does all the dirty work required for changing file system's UUID.
1491 */
Li Zefan125ccb02011-12-08 15:07:24 +08001492static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001493{
1494 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1495 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001496 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001497 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001498 struct btrfs_device *device;
1499 u64 super_flags;
1500
1501 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001502 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001503 return -EINVAL;
1504
Yan Zhenge4404d62008-12-12 10:03:26 -05001505 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1506 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001507 return -ENOMEM;
1508
Yan Zhenge4404d62008-12-12 10:03:26 -05001509 old_devices = clone_fs_devices(fs_devices);
1510 if (IS_ERR(old_devices)) {
1511 kfree(seed_devices);
1512 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001513 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001514
Yan Zheng2b820322008-11-17 21:11:30 -05001515 list_add(&old_devices->list, &fs_uuids);
1516
Yan Zhenge4404d62008-12-12 10:03:26 -05001517 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1518 seed_devices->opened = 1;
1519 INIT_LIST_HEAD(&seed_devices->devices);
1520 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001521 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001522
1523 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001524 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1525 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001526 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1527
Yan Zhenge4404d62008-12-12 10:03:26 -05001528 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1529 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1530 device->fs_devices = seed_devices;
1531 }
1532
Yan Zheng2b820322008-11-17 21:11:30 -05001533 fs_devices->seeding = 0;
1534 fs_devices->num_devices = 0;
1535 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001536 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001537
1538 generate_random_uuid(fs_devices->fsid);
1539 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1540 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1541 super_flags = btrfs_super_flags(disk_super) &
1542 ~BTRFS_SUPER_FLAG_SEEDING;
1543 btrfs_set_super_flags(disk_super, super_flags);
1544
1545 return 0;
1546}
1547
1548/*
1549 * strore the expected generation for seed devices in device items.
1550 */
1551static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1552 struct btrfs_root *root)
1553{
1554 struct btrfs_path *path;
1555 struct extent_buffer *leaf;
1556 struct btrfs_dev_item *dev_item;
1557 struct btrfs_device *device;
1558 struct btrfs_key key;
1559 u8 fs_uuid[BTRFS_UUID_SIZE];
1560 u8 dev_uuid[BTRFS_UUID_SIZE];
1561 u64 devid;
1562 int ret;
1563
1564 path = btrfs_alloc_path();
1565 if (!path)
1566 return -ENOMEM;
1567
1568 root = root->fs_info->chunk_root;
1569 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1570 key.offset = 0;
1571 key.type = BTRFS_DEV_ITEM_KEY;
1572
1573 while (1) {
1574 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1575 if (ret < 0)
1576 goto error;
1577
1578 leaf = path->nodes[0];
1579next_slot:
1580 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1581 ret = btrfs_next_leaf(root, path);
1582 if (ret > 0)
1583 break;
1584 if (ret < 0)
1585 goto error;
1586 leaf = path->nodes[0];
1587 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001588 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001589 continue;
1590 }
1591
1592 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1593 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1594 key.type != BTRFS_DEV_ITEM_KEY)
1595 break;
1596
1597 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1598 struct btrfs_dev_item);
1599 devid = btrfs_device_id(leaf, dev_item);
1600 read_extent_buffer(leaf, dev_uuid,
1601 (unsigned long)btrfs_device_uuid(dev_item),
1602 BTRFS_UUID_SIZE);
1603 read_extent_buffer(leaf, fs_uuid,
1604 (unsigned long)btrfs_device_fsid(dev_item),
1605 BTRFS_UUID_SIZE);
1606 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001607 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001608
1609 if (device->fs_devices->seeding) {
1610 btrfs_set_device_generation(leaf, dev_item,
1611 device->generation);
1612 btrfs_mark_buffer_dirty(leaf);
1613 }
1614
1615 path->slots[0]++;
1616 goto next_slot;
1617 }
1618 ret = 0;
1619error:
1620 btrfs_free_path(path);
1621 return ret;
1622}
1623
Chris Mason788f20e2008-04-28 15:29:42 -04001624int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1625{
Josef Bacikd5e20032011-08-04 14:52:27 +00001626 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001627 struct btrfs_trans_handle *trans;
1628 struct btrfs_device *device;
1629 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001630 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001631 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001632 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001633 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001634 int ret = 0;
1635
Yan Zheng2b820322008-11-17 21:11:30 -05001636 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001637 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001638
Li Zefana5d16332011-12-07 20:08:40 -05001639 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001640 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001641 if (IS_ERR(bdev))
1642 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001643
Yan Zheng2b820322008-11-17 21:11:30 -05001644 if (root->fs_info->fs_devices->seeding) {
1645 seeding_dev = 1;
1646 down_write(&sb->s_umount);
1647 mutex_lock(&uuid_mutex);
1648 }
1649
Chris Mason8c8bee12008-09-29 11:19:10 -04001650 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001651
Chris Mason788f20e2008-04-28 15:29:42 -04001652 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001653 /*
1654 * we have the volume lock, so we don't need the extra
1655 * device list mutex while reading the list here.
1656 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001657 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001658 if (device->bdev == bdev) {
1659 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001660 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001661 }
1662 }
1663
1664 device = kzalloc(sizeof(*device), GFP_NOFS);
1665 if (!device) {
1666 /* we can safely leave the fs_devices entry around */
1667 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001668 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001669 }
1670
Chris Mason788f20e2008-04-28 15:29:42 -04001671 device->name = kstrdup(device_path, GFP_NOFS);
1672 if (!device->name) {
1673 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001674 ret = -ENOMEM;
1675 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001676 }
Yan Zheng2b820322008-11-17 21:11:30 -05001677
1678 ret = find_next_devid(root, &device->devid);
1679 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001680 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001681 kfree(device);
1682 goto error;
1683 }
1684
Yan, Zhenga22285a2010-05-16 10:48:46 -04001685 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001686 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001687 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001688 kfree(device);
1689 ret = PTR_ERR(trans);
1690 goto error;
1691 }
1692
Yan Zheng2b820322008-11-17 21:11:30 -05001693 lock_chunks(root);
1694
Josef Bacikd5e20032011-08-04 14:52:27 +00001695 q = bdev_get_queue(bdev);
1696 if (blk_queue_discard(q))
1697 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001698 device->writeable = 1;
1699 device->work.func = pending_bios_fn;
1700 generate_random_uuid(device->uuid);
1701 spin_lock_init(&device->io_lock);
1702 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001703 device->io_width = root->sectorsize;
1704 device->io_align = root->sectorsize;
1705 device->sector_size = root->sectorsize;
1706 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001707 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001708 device->dev_root = root->fs_info->dev_root;
1709 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001710 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001711 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001712 set_blocksize(device->bdev, 4096);
1713
Yan Zheng2b820322008-11-17 21:11:30 -05001714 if (seeding_dev) {
1715 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001716 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001717 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001718 }
1719
1720 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001721
1722 /*
1723 * we don't want write_supers to jump in here with our device
1724 * half setup
1725 */
1726 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001727 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001728 list_add(&device->dev_alloc_list,
1729 &root->fs_info->fs_devices->alloc_list);
1730 root->fs_info->fs_devices->num_devices++;
1731 root->fs_info->fs_devices->open_devices++;
1732 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001733 if (device->can_discard)
1734 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001735 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1736
Josef Bacik2bf64752011-09-26 17:12:22 -04001737 spin_lock(&root->fs_info->free_chunk_lock);
1738 root->fs_info->free_chunk_space += device->total_bytes;
1739 spin_unlock(&root->fs_info->free_chunk_lock);
1740
Chris Masonc2898112009-06-10 09:51:32 -04001741 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1742 root->fs_info->fs_devices->rotating = 1;
1743
David Sterba6c417612011-04-13 15:41:04 +02001744 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1745 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001746 total_bytes + device->total_bytes);
1747
David Sterba6c417612011-04-13 15:41:04 +02001748 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1749 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001750 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001751 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001752
Yan Zheng2b820322008-11-17 21:11:30 -05001753 if (seeding_dev) {
1754 ret = init_first_rw_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001755 if (ret)
1756 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001757 ret = btrfs_finish_sprout(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001758 if (ret)
1759 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001760 } else {
1761 ret = btrfs_add_device(trans, root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001762 if (ret)
1763 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05001764 }
1765
Chris Mason913d9522009-03-10 13:17:18 -04001766 /*
1767 * we've got more storage, clear any full flags on the space
1768 * infos
1769 */
1770 btrfs_clear_space_info_full(root->fs_info);
1771
Chris Mason7d9eb122008-07-08 14:19:17 -04001772 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001773 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001774
1775 if (seeding_dev) {
1776 mutex_unlock(&uuid_mutex);
1777 up_write(&sb->s_umount);
1778
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001779 if (ret) /* transaction commit */
1780 return ret;
1781
Yan Zheng2b820322008-11-17 21:11:30 -05001782 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001783 if (ret < 0)
1784 btrfs_error(root->fs_info, ret,
1785 "Failed to relocate sys chunks after "
1786 "device initialization. This can be fixed "
1787 "using the \"btrfs balance\" command.");
Yan Zheng2b820322008-11-17 21:11:30 -05001788 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001789
Chris Mason788f20e2008-04-28 15:29:42 -04001790 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001791
1792error_trans:
1793 unlock_chunks(root);
1794 btrfs_abort_transaction(trans, root, ret);
1795 btrfs_end_transaction(trans, root);
1796 kfree(device->name);
1797 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001798error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001799 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001800 if (seeding_dev) {
1801 mutex_unlock(&uuid_mutex);
1802 up_write(&sb->s_umount);
1803 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001804 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001805}
1806
Chris Masond3977122009-01-05 21:25:51 -05001807static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1808 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001809{
1810 int ret;
1811 struct btrfs_path *path;
1812 struct btrfs_root *root;
1813 struct btrfs_dev_item *dev_item;
1814 struct extent_buffer *leaf;
1815 struct btrfs_key key;
1816
1817 root = device->dev_root->fs_info->chunk_root;
1818
1819 path = btrfs_alloc_path();
1820 if (!path)
1821 return -ENOMEM;
1822
1823 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1824 key.type = BTRFS_DEV_ITEM_KEY;
1825 key.offset = device->devid;
1826
1827 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1828 if (ret < 0)
1829 goto out;
1830
1831 if (ret > 0) {
1832 ret = -ENOENT;
1833 goto out;
1834 }
1835
1836 leaf = path->nodes[0];
1837 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1838
1839 btrfs_set_device_id(leaf, dev_item, device->devid);
1840 btrfs_set_device_type(leaf, dev_item, device->type);
1841 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1842 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1843 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001844 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001845 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1846 btrfs_mark_buffer_dirty(leaf);
1847
1848out:
1849 btrfs_free_path(path);
1850 return ret;
1851}
1852
Chris Mason7d9eb122008-07-08 14:19:17 -04001853static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001854 struct btrfs_device *device, u64 new_size)
1855{
1856 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001857 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001858 u64 old_total = btrfs_super_total_bytes(super_copy);
1859 u64 diff = new_size - device->total_bytes;
1860
Yan Zheng2b820322008-11-17 21:11:30 -05001861 if (!device->writeable)
1862 return -EACCES;
1863 if (new_size <= device->total_bytes)
1864 return -EINVAL;
1865
Chris Mason8f18cf12008-04-25 16:53:30 -04001866 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001867 device->fs_devices->total_rw_bytes += diff;
1868
1869 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001870 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001871 btrfs_clear_space_info_full(device->dev_root->fs_info);
1872
Chris Mason8f18cf12008-04-25 16:53:30 -04001873 return btrfs_update_device(trans, device);
1874}
1875
Chris Mason7d9eb122008-07-08 14:19:17 -04001876int btrfs_grow_device(struct btrfs_trans_handle *trans,
1877 struct btrfs_device *device, u64 new_size)
1878{
1879 int ret;
1880 lock_chunks(device->dev_root);
1881 ret = __btrfs_grow_device(trans, device, new_size);
1882 unlock_chunks(device->dev_root);
1883 return ret;
1884}
1885
Chris Mason8f18cf12008-04-25 16:53:30 -04001886static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1887 struct btrfs_root *root,
1888 u64 chunk_tree, u64 chunk_objectid,
1889 u64 chunk_offset)
1890{
1891 int ret;
1892 struct btrfs_path *path;
1893 struct btrfs_key key;
1894
1895 root = root->fs_info->chunk_root;
1896 path = btrfs_alloc_path();
1897 if (!path)
1898 return -ENOMEM;
1899
1900 key.objectid = chunk_objectid;
1901 key.offset = chunk_offset;
1902 key.type = BTRFS_CHUNK_ITEM_KEY;
1903
1904 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001905 if (ret < 0)
1906 goto out;
1907 else if (ret > 0) { /* Logic error or corruption */
1908 btrfs_error(root->fs_info, -ENOENT,
1909 "Failed lookup while freeing chunk.");
1910 ret = -ENOENT;
1911 goto out;
1912 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001913
1914 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001915 if (ret < 0)
1916 btrfs_error(root->fs_info, ret,
1917 "Failed to delete chunk item.");
1918out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001919 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001920 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001921}
1922
Christoph Hellwigb2950862008-12-02 09:54:17 -05001923static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001924 chunk_offset)
1925{
David Sterba6c417612011-04-13 15:41:04 +02001926 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001927 struct btrfs_disk_key *disk_key;
1928 struct btrfs_chunk *chunk;
1929 u8 *ptr;
1930 int ret = 0;
1931 u32 num_stripes;
1932 u32 array_size;
1933 u32 len = 0;
1934 u32 cur;
1935 struct btrfs_key key;
1936
1937 array_size = btrfs_super_sys_array_size(super_copy);
1938
1939 ptr = super_copy->sys_chunk_array;
1940 cur = 0;
1941
1942 while (cur < array_size) {
1943 disk_key = (struct btrfs_disk_key *)ptr;
1944 btrfs_disk_key_to_cpu(&key, disk_key);
1945
1946 len = sizeof(*disk_key);
1947
1948 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1949 chunk = (struct btrfs_chunk *)(ptr + len);
1950 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1951 len += btrfs_chunk_item_size(num_stripes);
1952 } else {
1953 ret = -EIO;
1954 break;
1955 }
1956 if (key.objectid == chunk_objectid &&
1957 key.offset == chunk_offset) {
1958 memmove(ptr, ptr + len, array_size - (cur + len));
1959 array_size -= len;
1960 btrfs_set_super_sys_array_size(super_copy, array_size);
1961 } else {
1962 ptr += len;
1963 cur += len;
1964 }
1965 }
1966 return ret;
1967}
1968
Christoph Hellwigb2950862008-12-02 09:54:17 -05001969static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001970 u64 chunk_tree, u64 chunk_objectid,
1971 u64 chunk_offset)
1972{
1973 struct extent_map_tree *em_tree;
1974 struct btrfs_root *extent_root;
1975 struct btrfs_trans_handle *trans;
1976 struct extent_map *em;
1977 struct map_lookup *map;
1978 int ret;
1979 int i;
1980
1981 root = root->fs_info->chunk_root;
1982 extent_root = root->fs_info->extent_root;
1983 em_tree = &root->fs_info->mapping_tree.map_tree;
1984
Josef Bacikba1bf482009-09-11 16:11:19 -04001985 ret = btrfs_can_relocate(extent_root, chunk_offset);
1986 if (ret)
1987 return -ENOSPC;
1988
Chris Mason8f18cf12008-04-25 16:53:30 -04001989 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001990 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001991 if (ret)
1992 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001993
Yan, Zhenga22285a2010-05-16 10:48:46 -04001994 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001995 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001996
Chris Mason7d9eb122008-07-08 14:19:17 -04001997 lock_chunks(root);
1998
Chris Mason8f18cf12008-04-25 16:53:30 -04001999 /*
2000 * step two, delete the device extents and the
2001 * chunk tree entries
2002 */
Chris Mason890871b2009-09-02 16:24:52 -04002003 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002004 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002005 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002006
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002007 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002008 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002009 map = (struct map_lookup *)em->bdev;
2010
2011 for (i = 0; i < map->num_stripes; i++) {
2012 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2013 map->stripes[i].physical);
2014 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002015
Chris Masondfe25022008-05-13 13:46:40 -04002016 if (map->stripes[i].dev) {
2017 ret = btrfs_update_device(trans, map->stripes[i].dev);
2018 BUG_ON(ret);
2019 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002020 }
2021 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2022 chunk_offset);
2023
2024 BUG_ON(ret);
2025
liubo1abe9b82011-03-24 11:18:59 +00002026 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2027
Chris Mason8f18cf12008-04-25 16:53:30 -04002028 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2029 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2030 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002031 }
2032
Zheng Yan1a40e232008-09-26 10:09:34 -04002033 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2034 BUG_ON(ret);
2035
Chris Mason890871b2009-09-02 16:24:52 -04002036 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002037 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002038 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002039
Chris Mason8f18cf12008-04-25 16:53:30 -04002040 kfree(map);
2041 em->bdev = NULL;
2042
2043 /* once for the tree */
2044 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002045 /* once for us */
2046 free_extent_map(em);
2047
Chris Mason7d9eb122008-07-08 14:19:17 -04002048 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002049 btrfs_end_transaction(trans, root);
2050 return 0;
2051}
2052
Yan Zheng2b820322008-11-17 21:11:30 -05002053static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2054{
2055 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2056 struct btrfs_path *path;
2057 struct extent_buffer *leaf;
2058 struct btrfs_chunk *chunk;
2059 struct btrfs_key key;
2060 struct btrfs_key found_key;
2061 u64 chunk_tree = chunk_root->root_key.objectid;
2062 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002063 bool retried = false;
2064 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002065 int ret;
2066
2067 path = btrfs_alloc_path();
2068 if (!path)
2069 return -ENOMEM;
2070
Josef Bacikba1bf482009-09-11 16:11:19 -04002071again:
Yan Zheng2b820322008-11-17 21:11:30 -05002072 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2073 key.offset = (u64)-1;
2074 key.type = BTRFS_CHUNK_ITEM_KEY;
2075
2076 while (1) {
2077 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2078 if (ret < 0)
2079 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002080 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002081
2082 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2083 key.type);
2084 if (ret < 0)
2085 goto error;
2086 if (ret > 0)
2087 break;
2088
2089 leaf = path->nodes[0];
2090 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2091
2092 chunk = btrfs_item_ptr(leaf, path->slots[0],
2093 struct btrfs_chunk);
2094 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002095 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002096
2097 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2098 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2099 found_key.objectid,
2100 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002101 if (ret == -ENOSPC)
2102 failed++;
2103 else if (ret)
2104 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002105 }
2106
2107 if (found_key.offset == 0)
2108 break;
2109 key.offset = found_key.offset - 1;
2110 }
2111 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002112 if (failed && !retried) {
2113 failed = 0;
2114 retried = true;
2115 goto again;
2116 } else if (failed && retried) {
2117 WARN_ON(1);
2118 ret = -ENOSPC;
2119 }
Yan Zheng2b820322008-11-17 21:11:30 -05002120error:
2121 btrfs_free_path(path);
2122 return ret;
2123}
2124
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002125static int insert_balance_item(struct btrfs_root *root,
2126 struct btrfs_balance_control *bctl)
2127{
2128 struct btrfs_trans_handle *trans;
2129 struct btrfs_balance_item *item;
2130 struct btrfs_disk_balance_args disk_bargs;
2131 struct btrfs_path *path;
2132 struct extent_buffer *leaf;
2133 struct btrfs_key key;
2134 int ret, err;
2135
2136 path = btrfs_alloc_path();
2137 if (!path)
2138 return -ENOMEM;
2139
2140 trans = btrfs_start_transaction(root, 0);
2141 if (IS_ERR(trans)) {
2142 btrfs_free_path(path);
2143 return PTR_ERR(trans);
2144 }
2145
2146 key.objectid = BTRFS_BALANCE_OBJECTID;
2147 key.type = BTRFS_BALANCE_ITEM_KEY;
2148 key.offset = 0;
2149
2150 ret = btrfs_insert_empty_item(trans, root, path, &key,
2151 sizeof(*item));
2152 if (ret)
2153 goto out;
2154
2155 leaf = path->nodes[0];
2156 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2157
2158 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2159
2160 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2161 btrfs_set_balance_data(leaf, item, &disk_bargs);
2162 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2163 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2164 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2165 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2166
2167 btrfs_set_balance_flags(leaf, item, bctl->flags);
2168
2169 btrfs_mark_buffer_dirty(leaf);
2170out:
2171 btrfs_free_path(path);
2172 err = btrfs_commit_transaction(trans, root);
2173 if (err && !ret)
2174 ret = err;
2175 return ret;
2176}
2177
2178static int del_balance_item(struct btrfs_root *root)
2179{
2180 struct btrfs_trans_handle *trans;
2181 struct btrfs_path *path;
2182 struct btrfs_key key;
2183 int ret, err;
2184
2185 path = btrfs_alloc_path();
2186 if (!path)
2187 return -ENOMEM;
2188
2189 trans = btrfs_start_transaction(root, 0);
2190 if (IS_ERR(trans)) {
2191 btrfs_free_path(path);
2192 return PTR_ERR(trans);
2193 }
2194
2195 key.objectid = BTRFS_BALANCE_OBJECTID;
2196 key.type = BTRFS_BALANCE_ITEM_KEY;
2197 key.offset = 0;
2198
2199 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2200 if (ret < 0)
2201 goto out;
2202 if (ret > 0) {
2203 ret = -ENOENT;
2204 goto out;
2205 }
2206
2207 ret = btrfs_del_item(trans, root, path);
2208out:
2209 btrfs_free_path(path);
2210 err = btrfs_commit_transaction(trans, root);
2211 if (err && !ret)
2212 ret = err;
2213 return ret;
2214}
2215
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002216/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002217 * This is a heuristic used to reduce the number of chunks balanced on
2218 * resume after balance was interrupted.
2219 */
2220static void update_balance_args(struct btrfs_balance_control *bctl)
2221{
2222 /*
2223 * Turn on soft mode for chunk types that were being converted.
2224 */
2225 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2226 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2227 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2228 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2229 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2230 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2231
2232 /*
2233 * Turn on usage filter if is not already used. The idea is
2234 * that chunks that we have already balanced should be
2235 * reasonably full. Don't do it for chunks that are being
2236 * converted - that will keep us from relocating unconverted
2237 * (albeit full) chunks.
2238 */
2239 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2240 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2241 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2242 bctl->data.usage = 90;
2243 }
2244 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2245 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2246 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2247 bctl->sys.usage = 90;
2248 }
2249 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2250 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2251 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2252 bctl->meta.usage = 90;
2253 }
2254}
2255
2256/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002257 * Should be called with both balance and volume mutexes held to
2258 * serialize other volume operations (add_dev/rm_dev/resize) with
2259 * restriper. Same goes for unset_balance_control.
2260 */
2261static void set_balance_control(struct btrfs_balance_control *bctl)
2262{
2263 struct btrfs_fs_info *fs_info = bctl->fs_info;
2264
2265 BUG_ON(fs_info->balance_ctl);
2266
2267 spin_lock(&fs_info->balance_lock);
2268 fs_info->balance_ctl = bctl;
2269 spin_unlock(&fs_info->balance_lock);
2270}
2271
2272static void unset_balance_control(struct btrfs_fs_info *fs_info)
2273{
2274 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2275
2276 BUG_ON(!fs_info->balance_ctl);
2277
2278 spin_lock(&fs_info->balance_lock);
2279 fs_info->balance_ctl = NULL;
2280 spin_unlock(&fs_info->balance_lock);
2281
2282 kfree(bctl);
2283}
2284
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002285/*
2286 * Balance filters. Return 1 if chunk should be filtered out
2287 * (should not be balanced).
2288 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002289static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002290 struct btrfs_balance_args *bargs)
2291{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002292 chunk_type = chunk_to_extended(chunk_type) &
2293 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002294
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002295 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002296 return 0;
2297
2298 return 1;
2299}
2300
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002301static u64 div_factor_fine(u64 num, int factor)
2302{
2303 if (factor <= 0)
2304 return 0;
2305 if (factor >= 100)
2306 return num;
2307
2308 num *= factor;
2309 do_div(num, 100);
2310 return num;
2311}
2312
2313static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2314 struct btrfs_balance_args *bargs)
2315{
2316 struct btrfs_block_group_cache *cache;
2317 u64 chunk_used, user_thresh;
2318 int ret = 1;
2319
2320 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2321 chunk_used = btrfs_block_group_used(&cache->item);
2322
2323 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2324 if (chunk_used < user_thresh)
2325 ret = 0;
2326
2327 btrfs_put_block_group(cache);
2328 return ret;
2329}
2330
Ilya Dryomov409d4042012-01-16 22:04:47 +02002331static int chunk_devid_filter(struct extent_buffer *leaf,
2332 struct btrfs_chunk *chunk,
2333 struct btrfs_balance_args *bargs)
2334{
2335 struct btrfs_stripe *stripe;
2336 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2337 int i;
2338
2339 for (i = 0; i < num_stripes; i++) {
2340 stripe = btrfs_stripe_nr(chunk, i);
2341 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2342 return 0;
2343 }
2344
2345 return 1;
2346}
2347
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002348/* [pstart, pend) */
2349static int chunk_drange_filter(struct extent_buffer *leaf,
2350 struct btrfs_chunk *chunk,
2351 u64 chunk_offset,
2352 struct btrfs_balance_args *bargs)
2353{
2354 struct btrfs_stripe *stripe;
2355 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2356 u64 stripe_offset;
2357 u64 stripe_length;
2358 int factor;
2359 int i;
2360
2361 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2362 return 0;
2363
2364 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2365 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2366 factor = 2;
2367 else
2368 factor = 1;
2369 factor = num_stripes / factor;
2370
2371 for (i = 0; i < num_stripes; i++) {
2372 stripe = btrfs_stripe_nr(chunk, i);
2373 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2374 continue;
2375
2376 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2377 stripe_length = btrfs_chunk_length(leaf, chunk);
2378 do_div(stripe_length, factor);
2379
2380 if (stripe_offset < bargs->pend &&
2381 stripe_offset + stripe_length > bargs->pstart)
2382 return 0;
2383 }
2384
2385 return 1;
2386}
2387
Ilya Dryomovea671762012-01-16 22:04:48 +02002388/* [vstart, vend) */
2389static int chunk_vrange_filter(struct extent_buffer *leaf,
2390 struct btrfs_chunk *chunk,
2391 u64 chunk_offset,
2392 struct btrfs_balance_args *bargs)
2393{
2394 if (chunk_offset < bargs->vend &&
2395 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2396 /* at least part of the chunk is inside this vrange */
2397 return 0;
2398
2399 return 1;
2400}
2401
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002402static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002403 struct btrfs_balance_args *bargs)
2404{
2405 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2406 return 0;
2407
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002408 chunk_type = chunk_to_extended(chunk_type) &
2409 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002410
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002411 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002412 return 1;
2413
2414 return 0;
2415}
2416
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002417static int should_balance_chunk(struct btrfs_root *root,
2418 struct extent_buffer *leaf,
2419 struct btrfs_chunk *chunk, u64 chunk_offset)
2420{
2421 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2422 struct btrfs_balance_args *bargs = NULL;
2423 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2424
2425 /* type filter */
2426 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2427 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2428 return 0;
2429 }
2430
2431 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2432 bargs = &bctl->data;
2433 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2434 bargs = &bctl->sys;
2435 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2436 bargs = &bctl->meta;
2437
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002438 /* profiles filter */
2439 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2440 chunk_profiles_filter(chunk_type, bargs)) {
2441 return 0;
2442 }
2443
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002444 /* usage filter */
2445 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2446 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2447 return 0;
2448 }
2449
Ilya Dryomov409d4042012-01-16 22:04:47 +02002450 /* devid filter */
2451 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2452 chunk_devid_filter(leaf, chunk, bargs)) {
2453 return 0;
2454 }
2455
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002456 /* drange filter, makes sense only with devid filter */
2457 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2458 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2459 return 0;
2460 }
2461
Ilya Dryomovea671762012-01-16 22:04:48 +02002462 /* vrange filter */
2463 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2464 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2465 return 0;
2466 }
2467
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002468 /* soft profile changing mode */
2469 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2470 chunk_soft_convert_filter(chunk_type, bargs)) {
2471 return 0;
2472 }
2473
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002474 return 1;
2475}
2476
Chris Masonec44a352008-04-28 15:29:52 -04002477static u64 div_factor(u64 num, int factor)
2478{
2479 if (factor == 10)
2480 return num;
2481 num *= factor;
2482 do_div(num, 10);
2483 return num;
2484}
2485
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002486static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002487{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002488 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002489 struct btrfs_root *chunk_root = fs_info->chunk_root;
2490 struct btrfs_root *dev_root = fs_info->dev_root;
2491 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002492 struct btrfs_device *device;
2493 u64 old_size;
2494 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002495 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002496 struct btrfs_path *path;
2497 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002498 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002499 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002500 struct extent_buffer *leaf;
2501 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002502 int ret;
2503 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002504 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002505
Chris Masonec44a352008-04-28 15:29:52 -04002506 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002507 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002508 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002509 old_size = device->total_bytes;
2510 size_to_free = div_factor(old_size, 1);
2511 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002512 if (!device->writeable ||
2513 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002514 continue;
2515
2516 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002517 if (ret == -ENOSPC)
2518 break;
Chris Masonec44a352008-04-28 15:29:52 -04002519 BUG_ON(ret);
2520
Yan, Zhenga22285a2010-05-16 10:48:46 -04002521 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002522 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002523
2524 ret = btrfs_grow_device(trans, device, old_size);
2525 BUG_ON(ret);
2526
2527 btrfs_end_transaction(trans, dev_root);
2528 }
2529
2530 /* step two, relocate all the chunks */
2531 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002532 if (!path) {
2533 ret = -ENOMEM;
2534 goto error;
2535 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002536
2537 /* zero out stat counters */
2538 spin_lock(&fs_info->balance_lock);
2539 memset(&bctl->stat, 0, sizeof(bctl->stat));
2540 spin_unlock(&fs_info->balance_lock);
2541again:
Chris Masonec44a352008-04-28 15:29:52 -04002542 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2543 key.offset = (u64)-1;
2544 key.type = BTRFS_CHUNK_ITEM_KEY;
2545
Chris Masond3977122009-01-05 21:25:51 -05002546 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002547 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002548 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002549 ret = -ECANCELED;
2550 goto error;
2551 }
2552
Chris Masonec44a352008-04-28 15:29:52 -04002553 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2554 if (ret < 0)
2555 goto error;
2556
2557 /*
2558 * this shouldn't happen, it means the last relocate
2559 * failed
2560 */
2561 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002562 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002563
2564 ret = btrfs_previous_item(chunk_root, path, 0,
2565 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002566 if (ret) {
2567 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002568 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002569 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002570
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002571 leaf = path->nodes[0];
2572 slot = path->slots[0];
2573 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2574
Chris Masonec44a352008-04-28 15:29:52 -04002575 if (found_key.objectid != key.objectid)
2576 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002577
Chris Masonec44a352008-04-28 15:29:52 -04002578 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002579 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002580 break;
2581
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002582 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2583
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002584 if (!counting) {
2585 spin_lock(&fs_info->balance_lock);
2586 bctl->stat.considered++;
2587 spin_unlock(&fs_info->balance_lock);
2588 }
2589
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002590 ret = should_balance_chunk(chunk_root, leaf, chunk,
2591 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002592 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002593 if (!ret)
2594 goto loop;
2595
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002596 if (counting) {
2597 spin_lock(&fs_info->balance_lock);
2598 bctl->stat.expected++;
2599 spin_unlock(&fs_info->balance_lock);
2600 goto loop;
2601 }
2602
Chris Masonec44a352008-04-28 15:29:52 -04002603 ret = btrfs_relocate_chunk(chunk_root,
2604 chunk_root->root_key.objectid,
2605 found_key.objectid,
2606 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002607 if (ret && ret != -ENOSPC)
2608 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002609 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002610 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002611 } else {
2612 spin_lock(&fs_info->balance_lock);
2613 bctl->stat.completed++;
2614 spin_unlock(&fs_info->balance_lock);
2615 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002616loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002617 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002618 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002619
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002620 if (counting) {
2621 btrfs_release_path(path);
2622 counting = false;
2623 goto again;
2624 }
Chris Masonec44a352008-04-28 15:29:52 -04002625error:
2626 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002627 if (enospc_errors) {
2628 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2629 enospc_errors);
2630 if (!ret)
2631 ret = -ENOSPC;
2632 }
2633
Chris Masonec44a352008-04-28 15:29:52 -04002634 return ret;
2635}
2636
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002637/**
2638 * alloc_profile_is_valid - see if a given profile is valid and reduced
2639 * @flags: profile to validate
2640 * @extended: if true @flags is treated as an extended profile
2641 */
2642static int alloc_profile_is_valid(u64 flags, int extended)
2643{
2644 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2645 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2646
2647 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2648
2649 /* 1) check that all other bits are zeroed */
2650 if (flags & ~mask)
2651 return 0;
2652
2653 /* 2) see if profile is reduced */
2654 if (flags == 0)
2655 return !extended; /* "0" is valid for usual profiles */
2656
2657 /* true if exactly one bit set */
2658 return (flags & (flags - 1)) == 0;
2659}
2660
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002661static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2662{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002663 /* cancel requested || normal exit path */
2664 return atomic_read(&fs_info->balance_cancel_req) ||
2665 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2666 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002667}
2668
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002669static void __cancel_balance(struct btrfs_fs_info *fs_info)
2670{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002671 int ret;
2672
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002673 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002674 ret = del_balance_item(fs_info->tree_root);
2675 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002676}
2677
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002678void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002679 struct btrfs_ioctl_balance_args *bargs);
2680
2681/*
2682 * Should be called with both balance and volume mutexes held
2683 */
2684int btrfs_balance(struct btrfs_balance_control *bctl,
2685 struct btrfs_ioctl_balance_args *bargs)
2686{
2687 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002688 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002689 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002690 int ret;
2691
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002692 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002693 atomic_read(&fs_info->balance_pause_req) ||
2694 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002695 ret = -EINVAL;
2696 goto out;
2697 }
2698
Ilya Dryomove4837f82012-03-27 17:09:17 +03002699 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2700 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2701 mixed = 1;
2702
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002703 /*
2704 * In case of mixed groups both data and meta should be picked,
2705 * and identical options should be given for both of them.
2706 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002707 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2708 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002709 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2710 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2711 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2712 printk(KERN_ERR "btrfs: with mixed groups data and "
2713 "metadata balance options must be the same\n");
2714 ret = -EINVAL;
2715 goto out;
2716 }
2717 }
2718
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002719 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2720 if (fs_info->fs_devices->num_devices == 1)
2721 allowed |= BTRFS_BLOCK_GROUP_DUP;
2722 else if (fs_info->fs_devices->num_devices < 4)
2723 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2724 else
2725 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2726 BTRFS_BLOCK_GROUP_RAID10);
2727
Ilya Dryomov6728b192012-03-27 17:09:17 +03002728 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2729 (!alloc_profile_is_valid(bctl->data.target, 1) ||
2730 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002731 printk(KERN_ERR "btrfs: unable to start balance with target "
2732 "data profile %llu\n",
2733 (unsigned long long)bctl->data.target);
2734 ret = -EINVAL;
2735 goto out;
2736 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002737 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2738 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
2739 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002740 printk(KERN_ERR "btrfs: unable to start balance with target "
2741 "metadata profile %llu\n",
2742 (unsigned long long)bctl->meta.target);
2743 ret = -EINVAL;
2744 goto out;
2745 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03002746 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2747 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
2748 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002749 printk(KERN_ERR "btrfs: unable to start balance with target "
2750 "system profile %llu\n",
2751 (unsigned long long)bctl->sys.target);
2752 ret = -EINVAL;
2753 goto out;
2754 }
2755
Ilya Dryomove4837f82012-03-27 17:09:17 +03002756 /* allow dup'ed data chunks only in mixed mode */
2757 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03002758 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002759 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2760 ret = -EINVAL;
2761 goto out;
2762 }
2763
2764 /* allow to reduce meta or sys integrity only if force set */
2765 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2766 BTRFS_BLOCK_GROUP_RAID10;
2767 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2768 (fs_info->avail_system_alloc_bits & allowed) &&
2769 !(bctl->sys.target & allowed)) ||
2770 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2771 (fs_info->avail_metadata_alloc_bits & allowed) &&
2772 !(bctl->meta.target & allowed))) {
2773 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2774 printk(KERN_INFO "btrfs: force reducing metadata "
2775 "integrity\n");
2776 } else {
2777 printk(KERN_ERR "btrfs: balance will reduce metadata "
2778 "integrity, use force if you want this\n");
2779 ret = -EINVAL;
2780 goto out;
2781 }
2782 }
2783
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002784 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002785 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002786 goto out;
2787
Ilya Dryomov59641012012-01-16 22:04:48 +02002788 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2789 BUG_ON(ret == -EEXIST);
2790 set_balance_control(bctl);
2791 } else {
2792 BUG_ON(ret != -EEXIST);
2793 spin_lock(&fs_info->balance_lock);
2794 update_balance_args(bctl);
2795 spin_unlock(&fs_info->balance_lock);
2796 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002797
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002798 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002799 mutex_unlock(&fs_info->balance_mutex);
2800
2801 ret = __btrfs_balance(fs_info);
2802
2803 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002804 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002805
2806 if (bargs) {
2807 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002808 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002809 }
2810
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002811 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2812 balance_need_close(fs_info)) {
2813 __cancel_balance(fs_info);
2814 }
2815
2816 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002817
2818 return ret;
2819out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002820 if (bctl->flags & BTRFS_BALANCE_RESUME)
2821 __cancel_balance(fs_info);
2822 else
2823 kfree(bctl);
2824 return ret;
2825}
2826
2827static int balance_kthread(void *data)
2828{
2829 struct btrfs_balance_control *bctl =
2830 (struct btrfs_balance_control *)data;
2831 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002832 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002833
2834 mutex_lock(&fs_info->volume_mutex);
2835 mutex_lock(&fs_info->balance_mutex);
2836
2837 set_balance_control(bctl);
2838
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002839 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2840 printk(KERN_INFO "btrfs: force skipping balance\n");
2841 } else {
2842 printk(KERN_INFO "btrfs: continuing balance\n");
2843 ret = btrfs_balance(bctl, NULL);
2844 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002845
2846 mutex_unlock(&fs_info->balance_mutex);
2847 mutex_unlock(&fs_info->volume_mutex);
2848 return ret;
2849}
2850
2851int btrfs_recover_balance(struct btrfs_root *tree_root)
2852{
2853 struct task_struct *tsk;
2854 struct btrfs_balance_control *bctl;
2855 struct btrfs_balance_item *item;
2856 struct btrfs_disk_balance_args disk_bargs;
2857 struct btrfs_path *path;
2858 struct extent_buffer *leaf;
2859 struct btrfs_key key;
2860 int ret;
2861
2862 path = btrfs_alloc_path();
2863 if (!path)
2864 return -ENOMEM;
2865
2866 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2867 if (!bctl) {
2868 ret = -ENOMEM;
2869 goto out;
2870 }
2871
2872 key.objectid = BTRFS_BALANCE_OBJECTID;
2873 key.type = BTRFS_BALANCE_ITEM_KEY;
2874 key.offset = 0;
2875
2876 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2877 if (ret < 0)
2878 goto out_bctl;
2879 if (ret > 0) { /* ret = -ENOENT; */
2880 ret = 0;
2881 goto out_bctl;
2882 }
2883
2884 leaf = path->nodes[0];
2885 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2886
2887 bctl->fs_info = tree_root->fs_info;
2888 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2889
2890 btrfs_balance_data(leaf, item, &disk_bargs);
2891 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2892 btrfs_balance_meta(leaf, item, &disk_bargs);
2893 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2894 btrfs_balance_sys(leaf, item, &disk_bargs);
2895 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2896
2897 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2898 if (IS_ERR(tsk))
2899 ret = PTR_ERR(tsk);
2900 else
2901 goto out;
2902
2903out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002904 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002905out:
2906 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002907 return ret;
2908}
2909
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002910int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2911{
2912 int ret = 0;
2913
2914 mutex_lock(&fs_info->balance_mutex);
2915 if (!fs_info->balance_ctl) {
2916 mutex_unlock(&fs_info->balance_mutex);
2917 return -ENOTCONN;
2918 }
2919
2920 if (atomic_read(&fs_info->balance_running)) {
2921 atomic_inc(&fs_info->balance_pause_req);
2922 mutex_unlock(&fs_info->balance_mutex);
2923
2924 wait_event(fs_info->balance_wait_q,
2925 atomic_read(&fs_info->balance_running) == 0);
2926
2927 mutex_lock(&fs_info->balance_mutex);
2928 /* we are good with balance_ctl ripped off from under us */
2929 BUG_ON(atomic_read(&fs_info->balance_running));
2930 atomic_dec(&fs_info->balance_pause_req);
2931 } else {
2932 ret = -ENOTCONN;
2933 }
2934
2935 mutex_unlock(&fs_info->balance_mutex);
2936 return ret;
2937}
2938
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002939int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2940{
2941 mutex_lock(&fs_info->balance_mutex);
2942 if (!fs_info->balance_ctl) {
2943 mutex_unlock(&fs_info->balance_mutex);
2944 return -ENOTCONN;
2945 }
2946
2947 atomic_inc(&fs_info->balance_cancel_req);
2948 /*
2949 * if we are running just wait and return, balance item is
2950 * deleted in btrfs_balance in this case
2951 */
2952 if (atomic_read(&fs_info->balance_running)) {
2953 mutex_unlock(&fs_info->balance_mutex);
2954 wait_event(fs_info->balance_wait_q,
2955 atomic_read(&fs_info->balance_running) == 0);
2956 mutex_lock(&fs_info->balance_mutex);
2957 } else {
2958 /* __cancel_balance needs volume_mutex */
2959 mutex_unlock(&fs_info->balance_mutex);
2960 mutex_lock(&fs_info->volume_mutex);
2961 mutex_lock(&fs_info->balance_mutex);
2962
2963 if (fs_info->balance_ctl)
2964 __cancel_balance(fs_info);
2965
2966 mutex_unlock(&fs_info->volume_mutex);
2967 }
2968
2969 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2970 atomic_dec(&fs_info->balance_cancel_req);
2971 mutex_unlock(&fs_info->balance_mutex);
2972 return 0;
2973}
2974
Chris Mason8f18cf12008-04-25 16:53:30 -04002975/*
2976 * shrinking a device means finding all of the device extents past
2977 * the new size, and then following the back refs to the chunks.
2978 * The chunk relocation code actually frees the device extent
2979 */
2980int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2981{
2982 struct btrfs_trans_handle *trans;
2983 struct btrfs_root *root = device->dev_root;
2984 struct btrfs_dev_extent *dev_extent = NULL;
2985 struct btrfs_path *path;
2986 u64 length;
2987 u64 chunk_tree;
2988 u64 chunk_objectid;
2989 u64 chunk_offset;
2990 int ret;
2991 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002992 int failed = 0;
2993 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002994 struct extent_buffer *l;
2995 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002996 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002997 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002998 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002999 u64 diff = device->total_bytes - new_size;
3000
Yan Zheng2b820322008-11-17 21:11:30 -05003001 if (new_size >= device->total_bytes)
3002 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04003003
3004 path = btrfs_alloc_path();
3005 if (!path)
3006 return -ENOMEM;
3007
Chris Mason8f18cf12008-04-25 16:53:30 -04003008 path->reada = 2;
3009
Chris Mason7d9eb122008-07-08 14:19:17 -04003010 lock_chunks(root);
3011
Chris Mason8f18cf12008-04-25 16:53:30 -04003012 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003013 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003014 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003015 spin_lock(&root->fs_info->free_chunk_lock);
3016 root->fs_info->free_chunk_space -= diff;
3017 spin_unlock(&root->fs_info->free_chunk_lock);
3018 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003019 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003020
Josef Bacikba1bf482009-09-11 16:11:19 -04003021again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003022 key.objectid = device->devid;
3023 key.offset = (u64)-1;
3024 key.type = BTRFS_DEV_EXTENT_KEY;
3025
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003026 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003027 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3028 if (ret < 0)
3029 goto done;
3030
3031 ret = btrfs_previous_item(root, path, 0, key.type);
3032 if (ret < 0)
3033 goto done;
3034 if (ret) {
3035 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003036 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003037 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003038 }
3039
3040 l = path->nodes[0];
3041 slot = path->slots[0];
3042 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3043
Josef Bacikba1bf482009-09-11 16:11:19 -04003044 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003045 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003046 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003047 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003048
3049 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3050 length = btrfs_dev_extent_length(l, dev_extent);
3051
Josef Bacikba1bf482009-09-11 16:11:19 -04003052 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003053 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003054 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003055 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003056
3057 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3058 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3059 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003060 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003061
3062 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3063 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003064 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003065 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003066 if (ret == -ENOSPC)
3067 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003068 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003069
3070 if (failed && !retried) {
3071 failed = 0;
3072 retried = true;
3073 goto again;
3074 } else if (failed && retried) {
3075 ret = -ENOSPC;
3076 lock_chunks(root);
3077
3078 device->total_bytes = old_size;
3079 if (device->writeable)
3080 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003081 spin_lock(&root->fs_info->free_chunk_lock);
3082 root->fs_info->free_chunk_space += diff;
3083 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003084 unlock_chunks(root);
3085 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003086 }
3087
Chris Balld6397ba2009-04-27 07:29:03 -04003088 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003089 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003090 if (IS_ERR(trans)) {
3091 ret = PTR_ERR(trans);
3092 goto done;
3093 }
3094
Chris Balld6397ba2009-04-27 07:29:03 -04003095 lock_chunks(root);
3096
3097 device->disk_total_bytes = new_size;
3098 /* Now btrfs_update_device() will change the on-disk size. */
3099 ret = btrfs_update_device(trans, device);
3100 if (ret) {
3101 unlock_chunks(root);
3102 btrfs_end_transaction(trans, root);
3103 goto done;
3104 }
3105 WARN_ON(diff > old_total);
3106 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3107 unlock_chunks(root);
3108 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003109done:
3110 btrfs_free_path(path);
3111 return ret;
3112}
3113
Li Zefan125ccb02011-12-08 15:07:24 +08003114static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003115 struct btrfs_key *key,
3116 struct btrfs_chunk *chunk, int item_size)
3117{
David Sterba6c417612011-04-13 15:41:04 +02003118 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003119 struct btrfs_disk_key disk_key;
3120 u32 array_size;
3121 u8 *ptr;
3122
3123 array_size = btrfs_super_sys_array_size(super_copy);
3124 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3125 return -EFBIG;
3126
3127 ptr = super_copy->sys_chunk_array + array_size;
3128 btrfs_cpu_key_to_disk(&disk_key, key);
3129 memcpy(ptr, &disk_key, sizeof(disk_key));
3130 ptr += sizeof(disk_key);
3131 memcpy(ptr, chunk, item_size);
3132 item_size += sizeof(disk_key);
3133 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3134 return 0;
3135}
3136
Miao Xieb2117a32011-01-05 10:07:28 +00003137/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003138 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003139 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003140static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003141{
Arne Jansen73c5de02011-04-12 12:07:57 +02003142 const struct btrfs_device_info *di_a = a;
3143 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003144
Arne Jansen73c5de02011-04-12 12:07:57 +02003145 if (di_a->max_avail > di_b->max_avail)
3146 return -1;
3147 if (di_a->max_avail < di_b->max_avail)
3148 return 1;
3149 if (di_a->total_avail > di_b->total_avail)
3150 return -1;
3151 if (di_a->total_avail < di_b->total_avail)
3152 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003153 return 0;
3154}
3155
3156static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3157 struct btrfs_root *extent_root,
3158 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003159 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003160 u64 start, u64 type)
3161{
3162 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003163 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3164 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003165 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003166 struct extent_map_tree *em_tree;
3167 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003168 struct btrfs_device_info *devices_info = NULL;
3169 u64 total_avail;
3170 int num_stripes; /* total number of stripes to allocate */
3171 int sub_stripes; /* sub_stripes info for map */
3172 int dev_stripes; /* stripes per dev */
3173 int devs_max; /* max devs to use */
3174 int devs_min; /* min devs needed */
3175 int devs_increment; /* ndevs has to be a multiple of this */
3176 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003177 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003178 u64 max_stripe_size;
3179 u64 max_chunk_size;
3180 u64 stripe_size;
3181 u64 num_bytes;
3182 int ndevs;
3183 int i;
3184 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003185
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003186 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003187
Miao Xieb2117a32011-01-05 10:07:28 +00003188 if (list_empty(&fs_devices->alloc_list))
3189 return -ENOSPC;
3190
Arne Jansen73c5de02011-04-12 12:07:57 +02003191 sub_stripes = 1;
3192 dev_stripes = 1;
3193 devs_increment = 1;
3194 ncopies = 1;
3195 devs_max = 0; /* 0 == as many as possible */
3196 devs_min = 1;
3197
3198 /*
3199 * define the properties of each RAID type.
3200 * FIXME: move this to a global table and use it in all RAID
3201 * calculation code
3202 */
3203 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3204 dev_stripes = 2;
3205 ncopies = 2;
3206 devs_max = 1;
3207 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3208 devs_min = 2;
3209 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3210 devs_increment = 2;
3211 ncopies = 2;
3212 devs_max = 2;
3213 devs_min = 2;
3214 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3215 sub_stripes = 2;
3216 devs_increment = 2;
3217 ncopies = 2;
3218 devs_min = 4;
3219 } else {
3220 devs_max = 1;
3221 }
3222
3223 if (type & BTRFS_BLOCK_GROUP_DATA) {
3224 max_stripe_size = 1024 * 1024 * 1024;
3225 max_chunk_size = 10 * max_stripe_size;
3226 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003227 /* for larger filesystems, use larger metadata chunks */
3228 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3229 max_stripe_size = 1024 * 1024 * 1024;
3230 else
3231 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003232 max_chunk_size = max_stripe_size;
3233 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003234 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003235 max_chunk_size = 2 * max_stripe_size;
3236 } else {
3237 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3238 type);
3239 BUG_ON(1);
3240 }
3241
3242 /* we don't want a chunk larger than 10% of writeable space */
3243 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3244 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003245
3246 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3247 GFP_NOFS);
3248 if (!devices_info)
3249 return -ENOMEM;
3250
Arne Jansen73c5de02011-04-12 12:07:57 +02003251 cur = fs_devices->alloc_list.next;
3252
3253 /*
3254 * in the first pass through the devices list, we gather information
3255 * about the available holes on each device.
3256 */
3257 ndevs = 0;
3258 while (cur != &fs_devices->alloc_list) {
3259 struct btrfs_device *device;
3260 u64 max_avail;
3261 u64 dev_offset;
3262
3263 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3264
3265 cur = cur->next;
3266
3267 if (!device->writeable) {
3268 printk(KERN_ERR
3269 "btrfs: read-only device in alloc_list\n");
3270 WARN_ON(1);
3271 continue;
3272 }
3273
3274 if (!device->in_fs_metadata)
3275 continue;
3276
3277 if (device->total_bytes > device->bytes_used)
3278 total_avail = device->total_bytes - device->bytes_used;
3279 else
3280 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003281
3282 /* If there is no space on this device, skip it. */
3283 if (total_avail == 0)
3284 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003285
Li Zefan125ccb02011-12-08 15:07:24 +08003286 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003287 max_stripe_size * dev_stripes,
3288 &dev_offset, &max_avail);
3289 if (ret && ret != -ENOSPC)
3290 goto error;
3291
3292 if (ret == 0)
3293 max_avail = max_stripe_size * dev_stripes;
3294
3295 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3296 continue;
3297
3298 devices_info[ndevs].dev_offset = dev_offset;
3299 devices_info[ndevs].max_avail = max_avail;
3300 devices_info[ndevs].total_avail = total_avail;
3301 devices_info[ndevs].dev = device;
3302 ++ndevs;
3303 }
3304
3305 /*
3306 * now sort the devices by hole size / available space
3307 */
3308 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3309 btrfs_cmp_device_info, NULL);
3310
3311 /* round down to number of usable stripes */
3312 ndevs -= ndevs % devs_increment;
3313
3314 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3315 ret = -ENOSPC;
3316 goto error;
3317 }
3318
3319 if (devs_max && ndevs > devs_max)
3320 ndevs = devs_max;
3321 /*
3322 * the primary goal is to maximize the number of stripes, so use as many
3323 * devices as possible, even if the stripes are not maximum sized.
3324 */
3325 stripe_size = devices_info[ndevs-1].max_avail;
3326 num_stripes = ndevs * dev_stripes;
3327
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003328 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003329 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003330 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003331 }
3332
3333 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003334
3335 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003336 do_div(stripe_size, BTRFS_STRIPE_LEN);
3337 stripe_size *= BTRFS_STRIPE_LEN;
3338
Miao Xieb2117a32011-01-05 10:07:28 +00003339 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3340 if (!map) {
3341 ret = -ENOMEM;
3342 goto error;
3343 }
3344 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003345
Arne Jansen73c5de02011-04-12 12:07:57 +02003346 for (i = 0; i < ndevs; ++i) {
3347 for (j = 0; j < dev_stripes; ++j) {
3348 int s = i * dev_stripes + j;
3349 map->stripes[s].dev = devices_info[i].dev;
3350 map->stripes[s].physical = devices_info[i].dev_offset +
3351 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003352 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003353 }
Chris Mason593060d2008-03-25 16:50:33 -04003354 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003355 map->stripe_len = BTRFS_STRIPE_LEN;
3356 map->io_align = BTRFS_STRIPE_LEN;
3357 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003358 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003359 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003360
Yan Zheng2b820322008-11-17 21:11:30 -05003361 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003362 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003363
Arne Jansen73c5de02011-04-12 12:07:57 +02003364 *stripe_size_out = stripe_size;
3365 *num_bytes_out = num_bytes;
3366
3367 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003368
David Sterba172ddd62011-04-21 00:48:27 +02003369 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003370 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003371 ret = -ENOMEM;
3372 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003373 }
Chris Mason0b86a832008-03-24 15:01:56 -04003374 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003375 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003376 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003377 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003378 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003379
Chris Mason0b86a832008-03-24 15:01:56 -04003380 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003381 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003382 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003383 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003384 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003385 if (ret)
3386 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003387
3388 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3389 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003390 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003391 if (ret)
3392 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003393
Arne Jansen73c5de02011-04-12 12:07:57 +02003394 for (i = 0; i < map->num_stripes; ++i) {
3395 struct btrfs_device *device;
3396 u64 dev_offset;
3397
3398 device = map->stripes[i].dev;
3399 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003400
3401 ret = btrfs_alloc_dev_extent(trans, device,
3402 info->chunk_root->root_key.objectid,
3403 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003404 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003405 if (ret) {
3406 btrfs_abort_transaction(trans, extent_root, ret);
3407 goto error;
3408 }
Yan Zheng2b820322008-11-17 21:11:30 -05003409 }
3410
Miao Xieb2117a32011-01-05 10:07:28 +00003411 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003412 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003413
3414error:
3415 kfree(map);
3416 kfree(devices_info);
3417 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003418}
3419
3420static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3421 struct btrfs_root *extent_root,
3422 struct map_lookup *map, u64 chunk_offset,
3423 u64 chunk_size, u64 stripe_size)
3424{
3425 u64 dev_offset;
3426 struct btrfs_key key;
3427 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3428 struct btrfs_device *device;
3429 struct btrfs_chunk *chunk;
3430 struct btrfs_stripe *stripe;
3431 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3432 int index = 0;
3433 int ret;
3434
3435 chunk = kzalloc(item_size, GFP_NOFS);
3436 if (!chunk)
3437 return -ENOMEM;
3438
3439 index = 0;
3440 while (index < map->num_stripes) {
3441 device = map->stripes[index].dev;
3442 device->bytes_used += stripe_size;
3443 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003444 if (ret)
3445 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003446 index++;
3447 }
3448
Josef Bacik2bf64752011-09-26 17:12:22 -04003449 spin_lock(&extent_root->fs_info->free_chunk_lock);
3450 extent_root->fs_info->free_chunk_space -= (stripe_size *
3451 map->num_stripes);
3452 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3453
Yan Zheng2b820322008-11-17 21:11:30 -05003454 index = 0;
3455 stripe = &chunk->stripe;
3456 while (index < map->num_stripes) {
3457 device = map->stripes[index].dev;
3458 dev_offset = map->stripes[index].physical;
3459
3460 btrfs_set_stack_stripe_devid(stripe, device->devid);
3461 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3462 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3463 stripe++;
3464 index++;
3465 }
3466
3467 btrfs_set_stack_chunk_length(chunk, chunk_size);
3468 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3469 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3470 btrfs_set_stack_chunk_type(chunk, map->type);
3471 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3472 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3473 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3474 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3475 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3476
3477 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3478 key.type = BTRFS_CHUNK_ITEM_KEY;
3479 key.offset = chunk_offset;
3480
3481 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003482
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003483 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3484 /*
3485 * TODO: Cleanup of inserted chunk root in case of
3486 * failure.
3487 */
Li Zefan125ccb02011-12-08 15:07:24 +08003488 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003489 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003490 }
liubo1abe9b82011-03-24 11:18:59 +00003491
Mark Fasheh3acd3952011-09-08 17:40:01 -07003492out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003493 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003494 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003495}
3496
3497/*
3498 * Chunk allocation falls into two parts. The first part does works
3499 * that make the new allocated chunk useable, but not do any operation
3500 * that modifies the chunk tree. The second part does the works that
3501 * require modifying the chunk tree. This division is important for the
3502 * bootstrap process of adding storage to a seed btrfs.
3503 */
3504int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3505 struct btrfs_root *extent_root, u64 type)
3506{
3507 u64 chunk_offset;
3508 u64 chunk_size;
3509 u64 stripe_size;
3510 struct map_lookup *map;
3511 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3512 int ret;
3513
3514 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3515 &chunk_offset);
3516 if (ret)
3517 return ret;
3518
3519 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3520 &stripe_size, chunk_offset, type);
3521 if (ret)
3522 return ret;
3523
3524 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3525 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003526 if (ret)
3527 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003528 return 0;
3529}
3530
Chris Masond3977122009-01-05 21:25:51 -05003531static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003532 struct btrfs_root *root,
3533 struct btrfs_device *device)
3534{
3535 u64 chunk_offset;
3536 u64 sys_chunk_offset;
3537 u64 chunk_size;
3538 u64 sys_chunk_size;
3539 u64 stripe_size;
3540 u64 sys_stripe_size;
3541 u64 alloc_profile;
3542 struct map_lookup *map;
3543 struct map_lookup *sys_map;
3544 struct btrfs_fs_info *fs_info = root->fs_info;
3545 struct btrfs_root *extent_root = fs_info->extent_root;
3546 int ret;
3547
3548 ret = find_next_chunk(fs_info->chunk_root,
3549 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003550 if (ret)
3551 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003552
3553 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003554 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003555 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3556
3557 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3558 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003559 if (ret)
3560 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003561
3562 sys_chunk_offset = chunk_offset + chunk_size;
3563
3564 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003565 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003566 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3567
3568 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3569 &sys_chunk_size, &sys_stripe_size,
3570 sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003571 if (ret)
3572 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003573
3574 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003575 if (ret)
3576 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003577
3578 /*
3579 * Modifying chunk tree needs allocating new blocks from both
3580 * system block group and metadata block group. So we only can
3581 * do operations require modifying the chunk tree after both
3582 * block groups were created.
3583 */
3584 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3585 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003586 if (ret)
3587 goto abort;
Yan Zheng2b820322008-11-17 21:11:30 -05003588
3589 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3590 sys_chunk_offset, sys_chunk_size,
3591 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003592 if (ret)
3593 goto abort;
3594
Yan Zheng2b820322008-11-17 21:11:30 -05003595 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003596
3597abort:
3598 btrfs_abort_transaction(trans, root, ret);
3599 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003600}
3601
3602int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3603{
3604 struct extent_map *em;
3605 struct map_lookup *map;
3606 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3607 int readonly = 0;
3608 int i;
3609
Chris Mason890871b2009-09-02 16:24:52 -04003610 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003611 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003612 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003613 if (!em)
3614 return 1;
3615
Josef Bacikf48b9072010-01-27 02:07:59 +00003616 if (btrfs_test_opt(root, DEGRADED)) {
3617 free_extent_map(em);
3618 return 0;
3619 }
3620
Yan Zheng2b820322008-11-17 21:11:30 -05003621 map = (struct map_lookup *)em->bdev;
3622 for (i = 0; i < map->num_stripes; i++) {
3623 if (!map->stripes[i].dev->writeable) {
3624 readonly = 1;
3625 break;
3626 }
3627 }
3628 free_extent_map(em);
3629 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003630}
3631
3632void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3633{
David Sterbaa8067e02011-04-21 00:34:43 +02003634 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003635}
3636
3637void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3638{
3639 struct extent_map *em;
3640
Chris Masond3977122009-01-05 21:25:51 -05003641 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003642 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003643 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3644 if (em)
3645 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003646 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003647 if (!em)
3648 break;
3649 kfree(em->bdev);
3650 /* once for us */
3651 free_extent_map(em);
3652 /* once for the tree */
3653 free_extent_map(em);
3654 }
3655}
3656
Chris Masonf1885912008-04-09 16:28:12 -04003657int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3658{
3659 struct extent_map *em;
3660 struct map_lookup *map;
3661 struct extent_map_tree *em_tree = &map_tree->map_tree;
3662 int ret;
3663
Chris Mason890871b2009-09-02 16:24:52 -04003664 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003665 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003666 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003667 BUG_ON(!em);
3668
3669 BUG_ON(em->start > logical || em->start + em->len < logical);
3670 map = (struct map_lookup *)em->bdev;
3671 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3672 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003673 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3674 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003675 else
3676 ret = 1;
3677 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003678 return ret;
3679}
3680
Chris Masondfe25022008-05-13 13:46:40 -04003681static int find_live_mirror(struct map_lookup *map, int first, int num,
3682 int optimal)
3683{
3684 int i;
3685 if (map->stripes[optimal].dev->bdev)
3686 return optimal;
3687 for (i = first; i < first + num; i++) {
3688 if (map->stripes[i].dev->bdev)
3689 return i;
3690 }
3691 /* we couldn't find one that doesn't fail. Just return something
3692 * and the io error handling code will clean up eventually
3693 */
3694 return optimal;
3695}
3696
Chris Masonf2d8d742008-04-21 10:03:05 -04003697static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3698 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003699 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003700 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003701{
3702 struct extent_map *em;
3703 struct map_lookup *map;
3704 struct extent_map_tree *em_tree = &map_tree->map_tree;
3705 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003706 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003707 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003708 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003709 u64 stripe_nr_orig;
3710 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003711 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003712 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003713 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003714 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003715 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003716 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003717
Chris Mason890871b2009-09-02 16:24:52 -04003718 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003719 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003720 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003721
Chris Mason3b951512008-04-17 11:29:12 -04003722 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003723 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3724 (unsigned long long)logical,
3725 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003726 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003727 }
Chris Mason0b86a832008-03-24 15:01:56 -04003728
3729 BUG_ON(em->start > logical || em->start + em->len < logical);
3730 map = (struct map_lookup *)em->bdev;
3731 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003732
Chris Masonf1885912008-04-09 16:28:12 -04003733 if (mirror_num > map->num_stripes)
3734 mirror_num = 0;
3735
Chris Mason593060d2008-03-25 16:50:33 -04003736 stripe_nr = offset;
3737 /*
3738 * stripe_nr counts the total number of stripes we have to stride
3739 * to get to this block
3740 */
3741 do_div(stripe_nr, map->stripe_len);
3742
3743 stripe_offset = stripe_nr * map->stripe_len;
3744 BUG_ON(offset < stripe_offset);
3745
3746 /* stripe_offset is the offset of this block in its stripe*/
3747 stripe_offset = offset - stripe_offset;
3748
Li Dongyangfce3bb92011-03-24 10:24:26 +00003749 if (rw & REQ_DISCARD)
3750 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003751 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003752 /* we limit the length of each bio to what fits in a stripe */
3753 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003754 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003755 } else {
3756 *length = em->len - offset;
3757 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003758
Jan Schmidta1d3c472011-08-04 17:15:33 +02003759 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003760 goto out;
3761
Chris Masonf2d8d742008-04-21 10:03:05 -04003762 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003763 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003764 stripe_nr_orig = stripe_nr;
3765 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3766 (~(map->stripe_len - 1));
3767 do_div(stripe_nr_end, map->stripe_len);
3768 stripe_end_offset = stripe_nr_end * map->stripe_len -
3769 (offset + *length);
3770 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3771 if (rw & REQ_DISCARD)
3772 num_stripes = min_t(u64, map->num_stripes,
3773 stripe_nr_end - stripe_nr_orig);
3774 stripe_index = do_div(stripe_nr, map->num_stripes);
3775 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003776 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003777 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003778 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003779 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003780 else {
3781 stripe_index = find_live_mirror(map, 0,
3782 map->num_stripes,
3783 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003784 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003785 }
Chris Mason2fff7342008-04-29 14:12:09 -04003786
Chris Mason611f0e02008-04-03 16:29:03 -04003787 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003788 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003789 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003790 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003791 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003792 } else {
3793 mirror_num = 1;
3794 }
Chris Mason2fff7342008-04-29 14:12:09 -04003795
Chris Mason321aecc2008-04-16 10:49:51 -04003796 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3797 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003798
3799 stripe_index = do_div(stripe_nr, factor);
3800 stripe_index *= map->sub_stripes;
3801
Jens Axboe7eaceac2011-03-10 08:52:07 +01003802 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003803 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003804 else if (rw & REQ_DISCARD)
3805 num_stripes = min_t(u64, map->sub_stripes *
3806 (stripe_nr_end - stripe_nr_orig),
3807 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003808 else if (mirror_num)
3809 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003810 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04003811 int old_stripe_index = stripe_index;
Chris Masondfe25022008-05-13 13:46:40 -04003812 stripe_index = find_live_mirror(map, stripe_index,
3813 map->sub_stripes, stripe_index +
3814 current->pid % map->sub_stripes);
Jan Schmidt3e743172012-04-27 12:41:45 -04003815 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003816 }
Chris Mason8790d502008-04-03 16:29:03 -04003817 } else {
3818 /*
3819 * after this do_div call, stripe_nr is the number of stripes
3820 * on this device we have to walk to find the data, and
3821 * stripe_index is the number of our device in the stripe array
3822 */
3823 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003824 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003825 }
Chris Mason593060d2008-03-25 16:50:33 -04003826 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003827
Li Zefande11cc12011-12-01 12:55:47 +08003828 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3829 if (!bbio) {
3830 ret = -ENOMEM;
3831 goto out;
3832 }
3833 atomic_set(&bbio->error, 0);
3834
Li Dongyangfce3bb92011-03-24 10:24:26 +00003835 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003836 int factor = 0;
3837 int sub_stripes = 0;
3838 u64 stripes_per_dev = 0;
3839 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04003840 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003841
3842 if (map->type &
3843 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3844 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3845 sub_stripes = 1;
3846 else
3847 sub_stripes = map->sub_stripes;
3848
3849 factor = map->num_stripes / sub_stripes;
3850 stripes_per_dev = div_u64_rem(stripe_nr_end -
3851 stripe_nr_orig,
3852 factor,
3853 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04003854 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3855 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003856 }
3857
Li Dongyangfce3bb92011-03-24 10:24:26 +00003858 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003859 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003860 map->stripes[stripe_index].physical +
3861 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003862 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003863
Li Zefanec9ef7a2011-12-01 14:06:42 +08003864 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3865 BTRFS_BLOCK_GROUP_RAID10)) {
3866 bbio->stripes[i].length = stripes_per_dev *
3867 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003868
Li Zefanec9ef7a2011-12-01 14:06:42 +08003869 if (i / sub_stripes < remaining_stripes)
3870 bbio->stripes[i].length +=
3871 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04003872
3873 /*
3874 * Special for the first stripe and
3875 * the last stripe:
3876 *
3877 * |-------|...|-------|
3878 * |----------|
3879 * off end_off
3880 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08003881 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003882 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003883 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003884
3885 if (stripe_index >= last_stripe &&
3886 stripe_index <= (last_stripe +
3887 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08003888 bbio->stripes[i].length -=
3889 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04003890
Li Zefanec9ef7a2011-12-01 14:06:42 +08003891 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003892 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003893 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003894 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003895
3896 stripe_index++;
3897 if (stripe_index == map->num_stripes) {
3898 /* This could only happen for RAID0/10 */
3899 stripe_index = 0;
3900 stripe_nr++;
3901 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003902 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003903 } else {
3904 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003905 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003906 map->stripes[stripe_index].physical +
3907 stripe_offset +
3908 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003909 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003910 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003911 stripe_index++;
3912 }
Chris Mason593060d2008-03-25 16:50:33 -04003913 }
Li Zefande11cc12011-12-01 12:55:47 +08003914
3915 if (rw & REQ_WRITE) {
3916 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3917 BTRFS_BLOCK_GROUP_RAID10 |
3918 BTRFS_BLOCK_GROUP_DUP)) {
3919 max_errors = 1;
3920 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003921 }
Li Zefande11cc12011-12-01 12:55:47 +08003922
3923 *bbio_ret = bbio;
3924 bbio->num_stripes = num_stripes;
3925 bbio->max_errors = max_errors;
3926 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003927out:
Chris Mason0b86a832008-03-24 15:01:56 -04003928 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003929 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003930}
3931
Chris Masonf2d8d742008-04-21 10:03:05 -04003932int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3933 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003934 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003935{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003936 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003937 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003938}
3939
Yan Zhenga512bbf2008-12-08 16:46:26 -05003940int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3941 u64 chunk_start, u64 physical, u64 devid,
3942 u64 **logical, int *naddrs, int *stripe_len)
3943{
3944 struct extent_map_tree *em_tree = &map_tree->map_tree;
3945 struct extent_map *em;
3946 struct map_lookup *map;
3947 u64 *buf;
3948 u64 bytenr;
3949 u64 length;
3950 u64 stripe_nr;
3951 int i, j, nr = 0;
3952
Chris Mason890871b2009-09-02 16:24:52 -04003953 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003954 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003955 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003956
3957 BUG_ON(!em || em->start != chunk_start);
3958 map = (struct map_lookup *)em->bdev;
3959
3960 length = em->len;
3961 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3962 do_div(length, map->num_stripes / map->sub_stripes);
3963 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3964 do_div(length, map->num_stripes);
3965
3966 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003967 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003968
3969 for (i = 0; i < map->num_stripes; i++) {
3970 if (devid && map->stripes[i].dev->devid != devid)
3971 continue;
3972 if (map->stripes[i].physical > physical ||
3973 map->stripes[i].physical + length <= physical)
3974 continue;
3975
3976 stripe_nr = physical - map->stripes[i].physical;
3977 do_div(stripe_nr, map->stripe_len);
3978
3979 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3980 stripe_nr = stripe_nr * map->num_stripes + i;
3981 do_div(stripe_nr, map->sub_stripes);
3982 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3983 stripe_nr = stripe_nr * map->num_stripes + i;
3984 }
3985 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003986 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003987 for (j = 0; j < nr; j++) {
3988 if (buf[j] == bytenr)
3989 break;
3990 }
Chris Mason934d3752008-12-08 16:43:10 -05003991 if (j == nr) {
3992 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003993 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003994 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003995 }
3996
Yan Zhenga512bbf2008-12-08 16:46:26 -05003997 *logical = buf;
3998 *naddrs = nr;
3999 *stripe_len = map->stripe_len;
4000
4001 free_extent_map(em);
4002 return 0;
4003}
4004
Stefan Behrens442a4f62012-05-25 16:06:08 +02004005static void *merge_stripe_index_into_bio_private(void *bi_private,
4006 unsigned int stripe_index)
4007{
4008 /*
4009 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4010 * at most 1.
4011 * The alternative solution (instead of stealing bits from the
4012 * pointer) would be to allocate an intermediate structure
4013 * that contains the old private pointer plus the stripe_index.
4014 */
4015 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4016 BUG_ON(stripe_index > 3);
4017 return (void *)(((uintptr_t)bi_private) | stripe_index);
4018}
4019
4020static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4021{
4022 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4023}
4024
4025static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4026{
4027 return (unsigned int)((uintptr_t)bi_private) & 3;
4028}
4029
Jan Schmidta1d3c472011-08-04 17:15:33 +02004030static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004031{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004032 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004033 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004034
Stefan Behrens442a4f62012-05-25 16:06:08 +02004035 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004036 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004037 if (err == -EIO || err == -EREMOTEIO) {
4038 unsigned int stripe_index =
4039 extract_stripe_index_from_bio_private(
4040 bio->bi_private);
4041 struct btrfs_device *dev;
4042
4043 BUG_ON(stripe_index >= bbio->num_stripes);
4044 dev = bbio->stripes[stripe_index].dev;
4045 if (bio->bi_rw & WRITE)
4046 btrfs_dev_stat_inc(dev,
4047 BTRFS_DEV_STAT_WRITE_ERRS);
4048 else
4049 btrfs_dev_stat_inc(dev,
4050 BTRFS_DEV_STAT_READ_ERRS);
4051 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4052 btrfs_dev_stat_inc(dev,
4053 BTRFS_DEV_STAT_FLUSH_ERRS);
4054 btrfs_dev_stat_print_on_error(dev);
4055 }
4056 }
Chris Mason8790d502008-04-03 16:29:03 -04004057
Jan Schmidta1d3c472011-08-04 17:15:33 +02004058 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004059 is_orig_bio = 1;
4060
Jan Schmidta1d3c472011-08-04 17:15:33 +02004061 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004062 if (!is_orig_bio) {
4063 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004064 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004065 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004066 bio->bi_private = bbio->private;
4067 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004068 bio->bi_bdev = (struct block_device *)
4069 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004070 /* only send an error to the higher layers if it is
4071 * beyond the tolerance of the multi-bio
4072 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004073 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004074 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004075 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004076 /*
4077 * this bio is actually up to date, we didn't
4078 * go over the max number of errors
4079 */
4080 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004081 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004082 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004083 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004084
4085 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004086 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004087 bio_put(bio);
4088 }
Chris Mason8790d502008-04-03 16:29:03 -04004089}
4090
Chris Mason8b712842008-06-11 16:50:36 -04004091struct async_sched {
4092 struct bio *bio;
4093 int rw;
4094 struct btrfs_fs_info *info;
4095 struct btrfs_work work;
4096};
4097
4098/*
4099 * see run_scheduled_bios for a description of why bios are collected for
4100 * async submit.
4101 *
4102 * This will add one bio to the pending list for a device and make sure
4103 * the work struct is scheduled.
4104 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004105static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004106 struct btrfs_device *device,
4107 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004108{
4109 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004110 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004111
4112 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004113 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004114 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004115 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004116 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004117 return;
Chris Mason8b712842008-06-11 16:50:36 -04004118 }
4119
4120 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004121 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004122 * higher layers. Otherwise, the async bio makes it appear we have
4123 * made progress against dirty pages when we've really just put it
4124 * on a queue for later
4125 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004126 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004127 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004128 bio->bi_next = NULL;
4129 bio->bi_rw |= rw;
4130
4131 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004132 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004133 pending_bios = &device->pending_sync_bios;
4134 else
4135 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004136
Chris Masonffbd5172009-04-20 15:50:09 -04004137 if (pending_bios->tail)
4138 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004139
Chris Masonffbd5172009-04-20 15:50:09 -04004140 pending_bios->tail = bio;
4141 if (!pending_bios->head)
4142 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004143 if (device->running_pending)
4144 should_queue = 0;
4145
4146 spin_unlock(&device->io_lock);
4147
4148 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004149 btrfs_queue_worker(&root->fs_info->submit_workers,
4150 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004151}
4152
Chris Masonf1885912008-04-09 16:28:12 -04004153int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004154 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004155{
4156 struct btrfs_mapping_tree *map_tree;
4157 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004158 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004159 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004160 u64 length = 0;
4161 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004162 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004163 int dev_nr = 0;
4164 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004165 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004166
Chris Masonf2d8d742008-04-21 10:03:05 -04004167 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004168 map_tree = &root->fs_info->mapping_tree;
4169 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004170
Jan Schmidta1d3c472011-08-04 17:15:33 +02004171 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004172 mirror_num);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004173 if (ret) /* -ENOMEM */
4174 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004175
Jan Schmidta1d3c472011-08-04 17:15:33 +02004176 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004177 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004178 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4179 "len %llu\n", (unsigned long long)logical,
4180 (unsigned long long)length,
4181 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004182 BUG();
4183 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004184
4185 bbio->orig_bio = first_bio;
4186 bbio->private = first_bio->bi_private;
4187 bbio->end_io = first_bio->bi_end_io;
4188 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004189
Chris Masond3977122009-01-05 21:25:51 -05004190 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004191 if (dev_nr < total_devs - 1) {
4192 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004193 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004194 } else {
4195 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004196 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004197 bio->bi_private = bbio;
Stefan Behrens442a4f62012-05-25 16:06:08 +02004198 bio->bi_private = merge_stripe_index_into_bio_private(
4199 bio->bi_private, (unsigned int)dev_nr);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004200 bio->bi_end_io = btrfs_end_bio;
4201 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4202 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004203 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004204 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4205 "(%s id %llu), size=%u\n", rw,
4206 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4207 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004208 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004209 if (async_submit)
4210 schedule_bio(root, dev, rw, bio);
4211 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004212 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004213 } else {
4214 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4215 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004216 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004217 }
Chris Mason8790d502008-04-03 16:29:03 -04004218 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004219 }
Chris Mason0b86a832008-03-24 15:01:56 -04004220 return 0;
4221}
4222
Chris Masona4437552008-04-18 10:29:38 -04004223struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004224 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004225{
Yan Zheng2b820322008-11-17 21:11:30 -05004226 struct btrfs_device *device;
4227 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004228
Yan Zheng2b820322008-11-17 21:11:30 -05004229 cur_devices = root->fs_info->fs_devices;
4230 while (cur_devices) {
4231 if (!fsid ||
4232 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4233 device = __find_device(&cur_devices->devices,
4234 devid, uuid);
4235 if (device)
4236 return device;
4237 }
4238 cur_devices = cur_devices->seed;
4239 }
4240 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004241}
4242
Chris Masondfe25022008-05-13 13:46:40 -04004243static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4244 u64 devid, u8 *dev_uuid)
4245{
4246 struct btrfs_device *device;
4247 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4248
4249 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004250 if (!device)
4251 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004252 list_add(&device->dev_list,
4253 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004254 device->dev_root = root->fs_info->dev_root;
4255 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004256 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004257 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004258 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004259 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004260 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004261 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004262 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004263 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4264 return device;
4265}
4266
Chris Mason0b86a832008-03-24 15:01:56 -04004267static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4268 struct extent_buffer *leaf,
4269 struct btrfs_chunk *chunk)
4270{
4271 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4272 struct map_lookup *map;
4273 struct extent_map *em;
4274 u64 logical;
4275 u64 length;
4276 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004277 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004278 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004279 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004280 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004281
Chris Masone17cade2008-04-15 15:41:47 -04004282 logical = key->offset;
4283 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004284
Chris Mason890871b2009-09-02 16:24:52 -04004285 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004286 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004287 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004288
4289 /* already mapped? */
4290 if (em && em->start <= logical && em->start + em->len > logical) {
4291 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004292 return 0;
4293 } else if (em) {
4294 free_extent_map(em);
4295 }
Chris Mason0b86a832008-03-24 15:01:56 -04004296
David Sterba172ddd62011-04-21 00:48:27 +02004297 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004298 if (!em)
4299 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004300 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4301 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004302 if (!map) {
4303 free_extent_map(em);
4304 return -ENOMEM;
4305 }
4306
4307 em->bdev = (struct block_device *)map;
4308 em->start = logical;
4309 em->len = length;
4310 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004311 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004312
Chris Mason593060d2008-03-25 16:50:33 -04004313 map->num_stripes = num_stripes;
4314 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4315 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4316 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4317 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4318 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004319 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004320 for (i = 0; i < num_stripes; i++) {
4321 map->stripes[i].physical =
4322 btrfs_stripe_offset_nr(leaf, chunk, i);
4323 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004324 read_extent_buffer(leaf, uuid, (unsigned long)
4325 btrfs_stripe_dev_uuid_nr(chunk, i),
4326 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004327 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4328 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004329 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004330 kfree(map);
4331 free_extent_map(em);
4332 return -EIO;
4333 }
Chris Masondfe25022008-05-13 13:46:40 -04004334 if (!map->stripes[i].dev) {
4335 map->stripes[i].dev =
4336 add_missing_dev(root, devid, uuid);
4337 if (!map->stripes[i].dev) {
4338 kfree(map);
4339 free_extent_map(em);
4340 return -EIO;
4341 }
4342 }
4343 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004344 }
4345
Chris Mason890871b2009-09-02 16:24:52 -04004346 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004347 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004348 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004349 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04004350 free_extent_map(em);
4351
4352 return 0;
4353}
4354
Jeff Mahoney143bede2012-03-01 14:56:26 +01004355static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04004356 struct btrfs_dev_item *dev_item,
4357 struct btrfs_device *device)
4358{
4359 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004360
4361 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004362 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4363 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004364 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4365 device->type = btrfs_device_type(leaf, dev_item);
4366 device->io_align = btrfs_device_io_align(leaf, dev_item);
4367 device->io_width = btrfs_device_io_width(leaf, dev_item);
4368 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004369
4370 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004371 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004372}
4373
Yan Zheng2b820322008-11-17 21:11:30 -05004374static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4375{
4376 struct btrfs_fs_devices *fs_devices;
4377 int ret;
4378
Li Zefanb367e472011-12-07 11:38:24 +08004379 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004380
4381 fs_devices = root->fs_info->fs_devices->seed;
4382 while (fs_devices) {
4383 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4384 ret = 0;
4385 goto out;
4386 }
4387 fs_devices = fs_devices->seed;
4388 }
4389
4390 fs_devices = find_fsid(fsid);
4391 if (!fs_devices) {
4392 ret = -ENOENT;
4393 goto out;
4394 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004395
4396 fs_devices = clone_fs_devices(fs_devices);
4397 if (IS_ERR(fs_devices)) {
4398 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004399 goto out;
4400 }
4401
Christoph Hellwig97288f22008-12-02 06:36:09 -05004402 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004403 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02004404 if (ret) {
4405 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004406 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02004407 }
Yan Zheng2b820322008-11-17 21:11:30 -05004408
4409 if (!fs_devices->seeding) {
4410 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004411 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004412 ret = -EINVAL;
4413 goto out;
4414 }
4415
4416 fs_devices->seed = root->fs_info->fs_devices->seed;
4417 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004418out:
Yan Zheng2b820322008-11-17 21:11:30 -05004419 return ret;
4420}
4421
Chris Mason0d81ba52008-03-24 15:02:07 -04004422static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004423 struct extent_buffer *leaf,
4424 struct btrfs_dev_item *dev_item)
4425{
4426 struct btrfs_device *device;
4427 u64 devid;
4428 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004429 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004430 u8 dev_uuid[BTRFS_UUID_SIZE];
4431
Chris Mason0b86a832008-03-24 15:01:56 -04004432 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004433 read_extent_buffer(leaf, dev_uuid,
4434 (unsigned long)btrfs_device_uuid(dev_item),
4435 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004436 read_extent_buffer(leaf, fs_uuid,
4437 (unsigned long)btrfs_device_fsid(dev_item),
4438 BTRFS_UUID_SIZE);
4439
4440 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4441 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004442 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004443 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004444 }
4445
4446 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4447 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004448 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004449 return -EIO;
4450
4451 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004452 printk(KERN_WARNING "warning devid %llu missing\n",
4453 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004454 device = add_missing_dev(root, devid, dev_uuid);
4455 if (!device)
4456 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004457 } else if (!device->missing) {
4458 /*
4459 * this happens when a device that was properly setup
4460 * in the device info lists suddenly goes bad.
4461 * device->bdev is NULL, and so we have to set
4462 * device->missing to one here
4463 */
4464 root->fs_info->fs_devices->missing_devices++;
4465 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004466 }
4467 }
4468
4469 if (device->fs_devices != root->fs_info->fs_devices) {
4470 BUG_ON(device->writeable);
4471 if (device->generation !=
4472 btrfs_device_generation(leaf, dev_item))
4473 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004474 }
Chris Mason0b86a832008-03-24 15:01:56 -04004475
4476 fill_device_from_item(leaf, dev_item, device);
4477 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004478 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004479 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004480 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004481 spin_lock(&root->fs_info->free_chunk_lock);
4482 root->fs_info->free_chunk_space += device->total_bytes -
4483 device->bytes_used;
4484 spin_unlock(&root->fs_info->free_chunk_lock);
4485 }
Chris Mason0b86a832008-03-24 15:01:56 -04004486 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004487 return ret;
4488}
4489
Yan Zhenge4404d62008-12-12 10:03:26 -05004490int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004491{
David Sterba6c417612011-04-13 15:41:04 +02004492 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004493 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004494 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004495 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004496 u8 *ptr;
4497 unsigned long sb_ptr;
4498 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004499 u32 num_stripes;
4500 u32 array_size;
4501 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004502 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004503 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004504
Yan Zhenge4404d62008-12-12 10:03:26 -05004505 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004506 BTRFS_SUPER_INFO_SIZE);
4507 if (!sb)
4508 return -ENOMEM;
4509 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004510 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004511 /*
4512 * The sb extent buffer is artifical and just used to read the system array.
4513 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4514 * pages up-to-date when the page is larger: extent does not cover the
4515 * whole page and consequently check_page_uptodate does not find all
4516 * the page's extents up-to-date (the hole beyond sb),
4517 * write_extent_buffer then triggers a WARN_ON.
4518 *
4519 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4520 * but sb spans only this function. Add an explicit SetPageUptodate call
4521 * to silence the warning eg. on PowerPC 64.
4522 */
4523 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04004524 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05004525
Chris Masona061fc82008-05-07 11:43:44 -04004526 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004527 array_size = btrfs_super_sys_array_size(super_copy);
4528
Chris Mason0b86a832008-03-24 15:01:56 -04004529 ptr = super_copy->sys_chunk_array;
4530 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4531 cur = 0;
4532
4533 while (cur < array_size) {
4534 disk_key = (struct btrfs_disk_key *)ptr;
4535 btrfs_disk_key_to_cpu(&key, disk_key);
4536
Chris Masona061fc82008-05-07 11:43:44 -04004537 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004538 sb_ptr += len;
4539 cur += len;
4540
Chris Mason0d81ba52008-03-24 15:02:07 -04004541 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004542 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004543 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004544 if (ret)
4545 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004546 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4547 len = btrfs_chunk_item_size(num_stripes);
4548 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004549 ret = -EIO;
4550 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004551 }
4552 ptr += len;
4553 sb_ptr += len;
4554 cur += len;
4555 }
Chris Masona061fc82008-05-07 11:43:44 -04004556 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004557 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004558}
4559
Stefan Behrens442a4f62012-05-25 16:06:08 +02004560struct btrfs_device *btrfs_find_device_for_logical(struct btrfs_root *root,
4561 u64 logical, int mirror_num)
4562{
4563 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4564 int ret;
4565 u64 map_length = 0;
4566 struct btrfs_bio *bbio = NULL;
4567 struct btrfs_device *device;
4568
4569 BUG_ON(mirror_num == 0);
4570 ret = btrfs_map_block(map_tree, WRITE, logical, &map_length, &bbio,
4571 mirror_num);
4572 if (ret) {
4573 BUG_ON(bbio != NULL);
4574 return NULL;
4575 }
4576 BUG_ON(mirror_num != bbio->mirror_num);
4577 device = bbio->stripes[mirror_num - 1].dev;
4578 kfree(bbio);
4579 return device;
4580}
4581
Chris Mason0b86a832008-03-24 15:01:56 -04004582int btrfs_read_chunk_tree(struct btrfs_root *root)
4583{
4584 struct btrfs_path *path;
4585 struct extent_buffer *leaf;
4586 struct btrfs_key key;
4587 struct btrfs_key found_key;
4588 int ret;
4589 int slot;
4590
4591 root = root->fs_info->chunk_root;
4592
4593 path = btrfs_alloc_path();
4594 if (!path)
4595 return -ENOMEM;
4596
Li Zefanb367e472011-12-07 11:38:24 +08004597 mutex_lock(&uuid_mutex);
4598 lock_chunks(root);
4599
Chris Mason0b86a832008-03-24 15:01:56 -04004600 /* first we search for all of the device items, and then we
4601 * read in all of the chunk items. This way we can create chunk
4602 * mappings that reference all of the devices that are afound
4603 */
4604 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4605 key.offset = 0;
4606 key.type = 0;
4607again:
4608 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004609 if (ret < 0)
4610 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004611 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004612 leaf = path->nodes[0];
4613 slot = path->slots[0];
4614 if (slot >= btrfs_header_nritems(leaf)) {
4615 ret = btrfs_next_leaf(root, path);
4616 if (ret == 0)
4617 continue;
4618 if (ret < 0)
4619 goto error;
4620 break;
4621 }
4622 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4623 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4624 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4625 break;
4626 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4627 struct btrfs_dev_item *dev_item;
4628 dev_item = btrfs_item_ptr(leaf, slot,
4629 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004630 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004631 if (ret)
4632 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004633 }
4634 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4635 struct btrfs_chunk *chunk;
4636 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4637 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004638 if (ret)
4639 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004640 }
4641 path->slots[0]++;
4642 }
4643 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4644 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004645 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004646 goto again;
4647 }
Chris Mason0b86a832008-03-24 15:01:56 -04004648 ret = 0;
4649error:
Li Zefanb367e472011-12-07 11:38:24 +08004650 unlock_chunks(root);
4651 mutex_unlock(&uuid_mutex);
4652
Yan Zheng2b820322008-11-17 21:11:30 -05004653 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004654 return ret;
4655}
Stefan Behrens442a4f62012-05-25 16:06:08 +02004656
4657void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
4658{
4659 btrfs_dev_stat_inc(dev, index);
4660 btrfs_dev_stat_print_on_error(dev);
4661}
4662
4663void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
4664{
4665 printk_ratelimited(KERN_ERR
4666 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
4667 dev->name,
4668 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
4669 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
4670 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
4671 btrfs_dev_stat_read(dev,
4672 BTRFS_DEV_STAT_CORRUPTION_ERRS),
4673 btrfs_dev_stat_read(dev,
4674 BTRFS_DEV_STAT_GENERATION_ERRS));
4675}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004676
4677int btrfs_get_dev_stats(struct btrfs_root *root,
4678 struct btrfs_ioctl_get_dev_stats *stats,
4679 int reset_after_read)
4680{
4681 struct btrfs_device *dev;
4682 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4683 int i;
4684
4685 mutex_lock(&fs_devices->device_list_mutex);
4686 dev = btrfs_find_device(root, stats->devid, NULL, NULL);
4687 mutex_unlock(&fs_devices->device_list_mutex);
4688
4689 if (!dev) {
4690 printk(KERN_WARNING
4691 "btrfs: get dev_stats failed, device not found\n");
4692 return -ENODEV;
4693 } else if (reset_after_read) {
4694 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
4695 if (stats->nr_items > i)
4696 stats->values[i] =
4697 btrfs_dev_stat_read_and_reset(dev, i);
4698 else
4699 btrfs_dev_stat_reset(dev, i);
4700 }
4701 } else {
4702 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
4703 if (stats->nr_items > i)
4704 stats->values[i] = btrfs_dev_stat_read(dev, i);
4705 }
4706 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
4707 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
4708 return 0;
4709}