blob: 7eefdef8a14c76b1d62e64fb18b36fcbba44e6d4 [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>
Ilya Dryomov59641012012-01-16 22:04:48 +020026#include <linux/kthread.h>
Chris Mason593060d2008-03-25 16:50:33 -040027#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050028#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040029#include "ctree.h"
30#include "extent_map.h"
31#include "disk-io.h"
32#include "transaction.h"
33#include "print-tree.h"
34#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040035#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010036#include "check-integrity.h"
Chris Mason0b86a832008-03-24 15:01:56 -040037
Yan Zheng2b820322008-11-17 21:11:30 -050038static int init_first_rw_device(struct btrfs_trans_handle *trans,
39 struct btrfs_root *root,
40 struct btrfs_device *device);
41static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
42
Chris Mason8a4b83c2008-03-24 15:02:07 -040043static DEFINE_MUTEX(uuid_mutex);
44static LIST_HEAD(fs_uuids);
45
Chris Mason7d9eb122008-07-08 14:19:17 -040046static void lock_chunks(struct btrfs_root *root)
47{
Chris Mason7d9eb122008-07-08 14:19:17 -040048 mutex_lock(&root->fs_info->chunk_mutex);
49}
50
51static void unlock_chunks(struct btrfs_root *root)
52{
Chris Mason7d9eb122008-07-08 14:19:17 -040053 mutex_unlock(&root->fs_info->chunk_mutex);
54}
55
Yan Zhenge4404d62008-12-12 10:03:26 -050056static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
57{
58 struct btrfs_device *device;
59 WARN_ON(fs_devices->opened);
60 while (!list_empty(&fs_devices->devices)) {
61 device = list_entry(fs_devices->devices.next,
62 struct btrfs_device, dev_list);
63 list_del(&device->dev_list);
64 kfree(device->name);
65 kfree(device);
66 }
67 kfree(fs_devices);
68}
69
Chris Mason8a4b83c2008-03-24 15:02:07 -040070int btrfs_cleanup_fs_uuids(void)
71{
72 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040073
Yan Zheng2b820322008-11-17 21:11:30 -050074 while (!list_empty(&fs_uuids)) {
75 fs_devices = list_entry(fs_uuids.next,
76 struct btrfs_fs_devices, list);
77 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050078 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040079 }
80 return 0;
81}
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 */
Chris Masond3977122009-01-05 21:25:51 -0500133static noinline int 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 return 0;
320}
321
Christoph Hellwigb2950862008-12-02 09:54:17 -0500322static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400323{
324 struct btrfs_device *device;
325
326 device = container_of(work, struct btrfs_device, work);
327 run_scheduled_bios(device);
328}
329
Chris Masona1b32a52008-09-05 16:09:51 -0400330static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400331 struct btrfs_super_block *disk_super,
332 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
333{
334 struct btrfs_device *device;
335 struct btrfs_fs_devices *fs_devices;
336 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000337 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400338
339 fs_devices = find_fsid(disk_super->fsid);
340 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400341 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400342 if (!fs_devices)
343 return -ENOMEM;
344 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400345 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400346 list_add(&fs_devices->list, &fs_uuids);
347 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
348 fs_devices->latest_devid = devid;
349 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400350 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400351 device = NULL;
352 } else {
Chris Masona4437552008-04-18 10:29:38 -0400353 device = __find_device(&fs_devices->devices, devid,
354 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400355 }
356 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500357 if (fs_devices->opened)
358 return -EBUSY;
359
Chris Mason8a4b83c2008-03-24 15:02:07 -0400360 device = kzalloc(sizeof(*device), GFP_NOFS);
361 if (!device) {
362 /* we can safely leave the fs_devices entry around */
363 return -ENOMEM;
364 }
365 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400366 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400367 memcpy(device->uuid, disk_super->dev_item.uuid,
368 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400369 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370 device->name = kstrdup(path, GFP_NOFS);
371 if (!device->name) {
372 kfree(device);
373 return -ENOMEM;
374 }
Yan Zheng2b820322008-11-17 21:11:30 -0500375 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400376
Arne Jansen90519d62011-05-23 14:30:00 +0200377 /* init readahead state */
378 spin_lock_init(&device->reada_lock);
379 device->reada_curr_zone = NULL;
380 atomic_set(&device->reada_in_flight, 0);
381 device->reada_next = 0;
382 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
383 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
384
Chris Masone5e9a522009-06-10 15:17:02 -0400385 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000386 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400387 mutex_unlock(&fs_devices->device_list_mutex);
388
Yan Zheng2b820322008-11-17 21:11:30 -0500389 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400390 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500391 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000392 name = kstrdup(path, GFP_NOFS);
393 if (!name)
394 return -ENOMEM;
395 kfree(device->name);
396 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500397 if (device->missing) {
398 fs_devices->missing_devices--;
399 device->missing = 0;
400 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400401 }
402
403 if (found_transid > fs_devices->latest_trans) {
404 fs_devices->latest_devid = devid;
405 fs_devices->latest_trans = found_transid;
406 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400407 *fs_devices_ret = fs_devices;
408 return 0;
409}
410
Yan Zhenge4404d62008-12-12 10:03:26 -0500411static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
412{
413 struct btrfs_fs_devices *fs_devices;
414 struct btrfs_device *device;
415 struct btrfs_device *orig_dev;
416
417 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
418 if (!fs_devices)
419 return ERR_PTR(-ENOMEM);
420
421 INIT_LIST_HEAD(&fs_devices->devices);
422 INIT_LIST_HEAD(&fs_devices->alloc_list);
423 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400424 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500425 fs_devices->latest_devid = orig->latest_devid;
426 fs_devices->latest_trans = orig->latest_trans;
427 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
428
Xiao Guangrong46224702011-04-20 10:08:47 +0000429 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500430 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
431 device = kzalloc(sizeof(*device), GFP_NOFS);
432 if (!device)
433 goto error;
434
435 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400436 if (!device->name) {
437 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500438 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400439 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500440
441 device->devid = orig_dev->devid;
442 device->work.func = pending_bios_fn;
443 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500444 spin_lock_init(&device->io_lock);
445 INIT_LIST_HEAD(&device->dev_list);
446 INIT_LIST_HEAD(&device->dev_alloc_list);
447
448 list_add(&device->dev_list, &fs_devices->devices);
449 device->fs_devices = fs_devices;
450 fs_devices->num_devices++;
451 }
452 return fs_devices;
453error:
454 free_fs_devices(fs_devices);
455 return ERR_PTR(-ENOMEM);
456}
457
Chris Masondfe25022008-05-13 13:46:40 -0400458int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
459{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500460 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400461
Chris Masona6b0d5c2012-02-20 20:53:43 -0500462 struct block_device *latest_bdev = NULL;
463 u64 latest_devid = 0;
464 u64 latest_transid = 0;
465
Chris Masondfe25022008-05-13 13:46:40 -0400466 mutex_lock(&uuid_mutex);
467again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000468 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500469 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500470 if (device->in_fs_metadata) {
471 if (!latest_transid ||
472 device->generation > latest_transid) {
473 latest_devid = device->devid;
474 latest_transid = device->generation;
475 latest_bdev = device->bdev;
476 }
Yan Zheng2b820322008-11-17 21:11:30 -0500477 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500478 }
Yan Zheng2b820322008-11-17 21:11:30 -0500479
480 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100481 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500482 device->bdev = NULL;
483 fs_devices->open_devices--;
484 }
485 if (device->writeable) {
486 list_del_init(&device->dev_alloc_list);
487 device->writeable = 0;
488 fs_devices->rw_devices--;
489 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500490 list_del_init(&device->dev_list);
491 fs_devices->num_devices--;
492 kfree(device->name);
493 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400494 }
Yan Zheng2b820322008-11-17 21:11:30 -0500495
496 if (fs_devices->seed) {
497 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500498 goto again;
499 }
500
Chris Masona6b0d5c2012-02-20 20:53:43 -0500501 fs_devices->latest_bdev = latest_bdev;
502 fs_devices->latest_devid = latest_devid;
503 fs_devices->latest_trans = latest_transid;
504
Chris Masondfe25022008-05-13 13:46:40 -0400505 mutex_unlock(&uuid_mutex);
506 return 0;
507}
Chris Masona0af4692008-05-13 16:03:06 -0400508
Xiao Guangrong1f781602011-04-20 10:09:16 +0000509static void __free_device(struct work_struct *work)
510{
511 struct btrfs_device *device;
512
513 device = container_of(work, struct btrfs_device, rcu_work);
514
515 if (device->bdev)
516 blkdev_put(device->bdev, device->mode);
517
518 kfree(device->name);
519 kfree(device);
520}
521
522static void free_device(struct rcu_head *head)
523{
524 struct btrfs_device *device;
525
526 device = container_of(head, struct btrfs_device, rcu);
527
528 INIT_WORK(&device->rcu_work, __free_device);
529 schedule_work(&device->rcu_work);
530}
531
Yan Zheng2b820322008-11-17 21:11:30 -0500532static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400533{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400534 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500535
Yan Zheng2b820322008-11-17 21:11:30 -0500536 if (--fs_devices->opened > 0)
537 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400538
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000539 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500540 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000541 struct btrfs_device *new_device;
542
543 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400544 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000545
Yan Zheng2b820322008-11-17 21:11:30 -0500546 if (device->writeable) {
547 list_del_init(&device->dev_alloc_list);
548 fs_devices->rw_devices--;
549 }
550
Josef Bacikd5e20032011-08-04 14:52:27 +0000551 if (device->can_discard)
552 fs_devices->num_can_discard--;
553
Xiao Guangrong1f781602011-04-20 10:09:16 +0000554 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
555 BUG_ON(!new_device);
556 memcpy(new_device, device, sizeof(*new_device));
557 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000558 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000559 new_device->bdev = NULL;
560 new_device->writeable = 0;
561 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000562 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000563 list_replace_rcu(&device->dev_list, &new_device->dev_list);
564
565 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400566 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000567 mutex_unlock(&fs_devices->device_list_mutex);
568
Yan Zhenge4404d62008-12-12 10:03:26 -0500569 WARN_ON(fs_devices->open_devices);
570 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500571 fs_devices->opened = 0;
572 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500573
Chris Mason8a4b83c2008-03-24 15:02:07 -0400574 return 0;
575}
576
Yan Zheng2b820322008-11-17 21:11:30 -0500577int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
578{
Yan Zhenge4404d62008-12-12 10:03:26 -0500579 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500580 int ret;
581
582 mutex_lock(&uuid_mutex);
583 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500584 if (!fs_devices->opened) {
585 seed_devices = fs_devices->seed;
586 fs_devices->seed = NULL;
587 }
Yan Zheng2b820322008-11-17 21:11:30 -0500588 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500589
590 while (seed_devices) {
591 fs_devices = seed_devices;
592 seed_devices = fs_devices->seed;
593 __btrfs_close_devices(fs_devices);
594 free_fs_devices(fs_devices);
595 }
Yan Zheng2b820322008-11-17 21:11:30 -0500596 return ret;
597}
598
Yan Zhenge4404d62008-12-12 10:03:26 -0500599static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
600 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400601{
Josef Bacikd5e20032011-08-04 14:52:27 +0000602 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400603 struct block_device *bdev;
604 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400605 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400606 struct block_device *latest_bdev = NULL;
607 struct buffer_head *bh;
608 struct btrfs_super_block *disk_super;
609 u64 latest_devid = 0;
610 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400611 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500612 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400613 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400614
Tejun Heod4d77622010-11-13 11:55:18 +0100615 flags |= FMODE_EXCL;
616
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500617 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400618 if (device->bdev)
619 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400620 if (!device->name)
621 continue;
622
Tejun Heod4d77622010-11-13 11:55:18 +0100623 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400624 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500625 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400626 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400627 }
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 Masonc289811c2009-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
726 mutex_lock(&uuid_mutex);
727
Tejun Heod4d77622010-11-13 11:55:18 +0100728 flags |= FMODE_EXCL;
729 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400730
731 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400732 ret = PTR_ERR(bdev);
733 goto error;
734 }
735
736 ret = set_blocksize(bdev, 4096);
737 if (ret)
738 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500739 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400740 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000741 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400742 goto error_close;
743 }
744 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000745 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400746 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400747 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500748 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200749 else
750 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500751 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500752 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400753 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
754
Chris Mason8a4b83c2008-03-24 15:02:07 -0400755 brelse(bh);
756error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100757 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758error:
759 mutex_unlock(&uuid_mutex);
760 return ret;
761}
Chris Mason0b86a832008-03-24 15:01:56 -0400762
Miao Xie6d07bce2011-01-05 10:07:31 +0000763/* helper to account the used device space in the range */
764int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
765 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400766{
767 struct btrfs_key key;
768 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000769 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500770 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000771 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400772 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000773 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400774 struct extent_buffer *l;
775
Miao Xie6d07bce2011-01-05 10:07:31 +0000776 *length = 0;
777
778 if (start >= device->total_bytes)
779 return 0;
780
Yan Zheng2b820322008-11-17 21:11:30 -0500781 path = btrfs_alloc_path();
782 if (!path)
783 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400784 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400785
Chris Mason0b86a832008-03-24 15:01:56 -0400786 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000787 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400788 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000789
790 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400791 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000792 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400793 if (ret > 0) {
794 ret = btrfs_previous_item(root, path, key.objectid, key.type);
795 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000796 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400797 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000798
Chris Mason0b86a832008-03-24 15:01:56 -0400799 while (1) {
800 l = path->nodes[0];
801 slot = path->slots[0];
802 if (slot >= btrfs_header_nritems(l)) {
803 ret = btrfs_next_leaf(root, path);
804 if (ret == 0)
805 continue;
806 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000807 goto out;
808
809 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400810 }
811 btrfs_item_key_to_cpu(l, &key, slot);
812
813 if (key.objectid < device->devid)
814 goto next;
815
816 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000817 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400818
Chris Masond3977122009-01-05 21:25:51 -0500819 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400820 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400821
Chris Mason0b86a832008-03-24 15:01:56 -0400822 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000823 extent_end = key.offset + btrfs_dev_extent_length(l,
824 dev_extent);
825 if (key.offset <= start && extent_end > end) {
826 *length = end - start + 1;
827 break;
828 } else if (key.offset <= start && extent_end > start)
829 *length += extent_end - start;
830 else if (key.offset > start && extent_end <= end)
831 *length += extent_end - key.offset;
832 else if (key.offset > start && key.offset <= end) {
833 *length += end - key.offset + 1;
834 break;
835 } else if (key.offset > end)
836 break;
837
838next:
839 path->slots[0]++;
840 }
841 ret = 0;
842out:
843 btrfs_free_path(path);
844 return ret;
845}
846
Chris Mason0b86a832008-03-24 15:01:56 -0400847/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000848 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000849 * @device: the device which we search the free space in
850 * @num_bytes: the size of the free space that we need
851 * @start: store the start of the free space.
852 * @len: the size of the free space. that we find, or the size of the max
853 * free space if we don't find suitable free space
854 *
Chris Mason0b86a832008-03-24 15:01:56 -0400855 * this uses a pretty simple search, the expectation is that it is
856 * called very infrequently and that a given device has a small number
857 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000858 *
859 * @start is used to store the start of the free space if we find. But if we
860 * don't find suitable free space, it will be used to store the start position
861 * of the max free space.
862 *
863 * @len is used to store the size of the free space that we find.
864 * But if we don't find suitable free space, it is used to store the size of
865 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400866 */
Li Zefan125ccb02011-12-08 15:07:24 +0800867int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000868 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400869{
870 struct btrfs_key key;
871 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000872 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400873 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000874 u64 hole_size;
875 u64 max_hole_start;
876 u64 max_hole_size;
877 u64 extent_end;
878 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400879 u64 search_end = device->total_bytes;
880 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000881 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400882 struct extent_buffer *l;
883
Chris Mason0b86a832008-03-24 15:01:56 -0400884 /* FIXME use last free of some kind */
885
886 /* we don't want to overwrite the superblock on the drive,
887 * so we make sure to start at an offset of at least 1MB
888 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200889 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400890
Miao Xie7bfc8372011-01-05 10:07:26 +0000891 max_hole_start = search_start;
892 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000893 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000894
895 if (search_start >= search_end) {
896 ret = -ENOSPC;
897 goto error;
898 }
899
900 path = btrfs_alloc_path();
901 if (!path) {
902 ret = -ENOMEM;
903 goto error;
904 }
905 path->reada = 2;
906
Chris Mason0b86a832008-03-24 15:01:56 -0400907 key.objectid = device->devid;
908 key.offset = search_start;
909 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000910
Li Zefan125ccb02011-12-08 15:07:24 +0800911 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400912 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000913 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400914 if (ret > 0) {
915 ret = btrfs_previous_item(root, path, key.objectid, key.type);
916 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000917 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400918 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000919
Chris Mason0b86a832008-03-24 15:01:56 -0400920 while (1) {
921 l = path->nodes[0];
922 slot = path->slots[0];
923 if (slot >= btrfs_header_nritems(l)) {
924 ret = btrfs_next_leaf(root, path);
925 if (ret == 0)
926 continue;
927 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000928 goto out;
929
930 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400931 }
932 btrfs_item_key_to_cpu(l, &key, slot);
933
934 if (key.objectid < device->devid)
935 goto next;
936
937 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000938 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400939
Chris Mason0b86a832008-03-24 15:01:56 -0400940 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
941 goto next;
942
Miao Xie7bfc8372011-01-05 10:07:26 +0000943 if (key.offset > search_start) {
944 hole_size = key.offset - search_start;
945
946 if (hole_size > max_hole_size) {
947 max_hole_start = search_start;
948 max_hole_size = hole_size;
949 }
950
951 /*
952 * If this free space is greater than which we need,
953 * it must be the max free space that we have found
954 * until now, so max_hole_start must point to the start
955 * of this free space and the length of this free space
956 * is stored in max_hole_size. Thus, we return
957 * max_hole_start and max_hole_size and go back to the
958 * caller.
959 */
960 if (hole_size >= num_bytes) {
961 ret = 0;
962 goto out;
963 }
964 }
965
Chris Mason0b86a832008-03-24 15:01:56 -0400966 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000967 extent_end = key.offset + btrfs_dev_extent_length(l,
968 dev_extent);
969 if (extent_end > search_start)
970 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400971next:
972 path->slots[0]++;
973 cond_resched();
974 }
Chris Mason0b86a832008-03-24 15:01:56 -0400975
liubo38c01b92011-08-02 02:39:03 +0000976 /*
977 * At this point, search_start should be the end of
978 * allocated dev extents, and when shrinking the device,
979 * search_end may be smaller than search_start.
980 */
981 if (search_end > search_start)
982 hole_size = search_end - search_start;
983
Miao Xie7bfc8372011-01-05 10:07:26 +0000984 if (hole_size > max_hole_size) {
985 max_hole_start = search_start;
986 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400987 }
Chris Mason0b86a832008-03-24 15:01:56 -0400988
Miao Xie7bfc8372011-01-05 10:07:26 +0000989 /* See above. */
990 if (hole_size < num_bytes)
991 ret = -ENOSPC;
992 else
993 ret = 0;
994
995out:
Yan Zheng2b820322008-11-17 21:11:30 -0500996 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000997error:
998 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000999 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001000 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001001 return ret;
1002}
1003
Christoph Hellwigb2950862008-12-02 09:54:17 -05001004static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001005 struct btrfs_device *device,
1006 u64 start)
1007{
1008 int ret;
1009 struct btrfs_path *path;
1010 struct btrfs_root *root = device->dev_root;
1011 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001012 struct btrfs_key found_key;
1013 struct extent_buffer *leaf = NULL;
1014 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001015
1016 path = btrfs_alloc_path();
1017 if (!path)
1018 return -ENOMEM;
1019
1020 key.objectid = device->devid;
1021 key.offset = start;
1022 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001023again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001024 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001025 if (ret > 0) {
1026 ret = btrfs_previous_item(root, path, key.objectid,
1027 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001028 if (ret)
1029 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001030 leaf = path->nodes[0];
1031 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1032 extent = btrfs_item_ptr(leaf, path->slots[0],
1033 struct btrfs_dev_extent);
1034 BUG_ON(found_key.offset > start || found_key.offset +
1035 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001036 key = found_key;
1037 btrfs_release_path(path);
1038 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001039 } else if (ret == 0) {
1040 leaf = path->nodes[0];
1041 extent = btrfs_item_ptr(leaf, path->slots[0],
1042 struct btrfs_dev_extent);
1043 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001044 BUG_ON(ret);
1045
Josef Bacik2bf64752011-09-26 17:12:22 -04001046 if (device->bytes_used > 0) {
1047 u64 len = btrfs_dev_extent_length(leaf, extent);
1048 device->bytes_used -= len;
1049 spin_lock(&root->fs_info->free_chunk_lock);
1050 root->fs_info->free_chunk_space += len;
1051 spin_unlock(&root->fs_info->free_chunk_lock);
1052 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001053 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001054
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001055out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001056 btrfs_free_path(path);
1057 return ret;
1058}
1059
Yan Zheng2b820322008-11-17 21:11:30 -05001060int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001061 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001062 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001063 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001064{
1065 int ret;
1066 struct btrfs_path *path;
1067 struct btrfs_root *root = device->dev_root;
1068 struct btrfs_dev_extent *extent;
1069 struct extent_buffer *leaf;
1070 struct btrfs_key key;
1071
Chris Masondfe25022008-05-13 13:46:40 -04001072 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001073 path = btrfs_alloc_path();
1074 if (!path)
1075 return -ENOMEM;
1076
Chris Mason0b86a832008-03-24 15:01:56 -04001077 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001078 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001079 key.type = BTRFS_DEV_EXTENT_KEY;
1080 ret = btrfs_insert_empty_item(trans, root, path, &key,
1081 sizeof(*extent));
1082 BUG_ON(ret);
1083
1084 leaf = path->nodes[0];
1085 extent = btrfs_item_ptr(leaf, path->slots[0],
1086 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001087 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1088 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1089 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1090
1091 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1092 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1093 BTRFS_UUID_SIZE);
1094
Chris Mason0b86a832008-03-24 15:01:56 -04001095 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1096 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001097 btrfs_free_path(path);
1098 return ret;
1099}
1100
Chris Masona1b32a52008-09-05 16:09:51 -04001101static noinline int find_next_chunk(struct btrfs_root *root,
1102 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001103{
1104 struct btrfs_path *path;
1105 int ret;
1106 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001107 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001108 struct btrfs_key found_key;
1109
1110 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001111 if (!path)
1112 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001113
Chris Masone17cade2008-04-15 15:41:47 -04001114 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001115 key.offset = (u64)-1;
1116 key.type = BTRFS_CHUNK_ITEM_KEY;
1117
1118 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1119 if (ret < 0)
1120 goto error;
1121
1122 BUG_ON(ret == 0);
1123
1124 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1125 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001126 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001127 } else {
1128 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1129 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001130 if (found_key.objectid != objectid)
1131 *offset = 0;
1132 else {
1133 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1134 struct btrfs_chunk);
1135 *offset = found_key.offset +
1136 btrfs_chunk_length(path->nodes[0], chunk);
1137 }
Chris Mason0b86a832008-03-24 15:01:56 -04001138 }
1139 ret = 0;
1140error:
1141 btrfs_free_path(path);
1142 return ret;
1143}
1144
Yan Zheng2b820322008-11-17 21:11:30 -05001145static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001146{
1147 int ret;
1148 struct btrfs_key key;
1149 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001150 struct btrfs_path *path;
1151
1152 root = root->fs_info->chunk_root;
1153
1154 path = btrfs_alloc_path();
1155 if (!path)
1156 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001157
1158 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1159 key.type = BTRFS_DEV_ITEM_KEY;
1160 key.offset = (u64)-1;
1161
1162 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1163 if (ret < 0)
1164 goto error;
1165
1166 BUG_ON(ret == 0);
1167
1168 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1169 BTRFS_DEV_ITEM_KEY);
1170 if (ret) {
1171 *objectid = 1;
1172 } else {
1173 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1174 path->slots[0]);
1175 *objectid = found_key.offset + 1;
1176 }
1177 ret = 0;
1178error:
Yan Zheng2b820322008-11-17 21:11:30 -05001179 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001180 return ret;
1181}
1182
1183/*
1184 * the device information is stored in the chunk root
1185 * the btrfs_device struct should be fully filled in
1186 */
1187int btrfs_add_device(struct btrfs_trans_handle *trans,
1188 struct btrfs_root *root,
1189 struct btrfs_device *device)
1190{
1191 int ret;
1192 struct btrfs_path *path;
1193 struct btrfs_dev_item *dev_item;
1194 struct extent_buffer *leaf;
1195 struct btrfs_key key;
1196 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001197
1198 root = root->fs_info->chunk_root;
1199
1200 path = btrfs_alloc_path();
1201 if (!path)
1202 return -ENOMEM;
1203
Chris Mason0b86a832008-03-24 15:01:56 -04001204 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1205 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001206 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001207
1208 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001209 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001210 if (ret)
1211 goto out;
1212
1213 leaf = path->nodes[0];
1214 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1215
1216 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001217 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001218 btrfs_set_device_type(leaf, dev_item, device->type);
1219 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1220 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1221 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001222 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1223 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001224 btrfs_set_device_group(leaf, dev_item, 0);
1225 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1226 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001227 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001228
Chris Mason0b86a832008-03-24 15:01:56 -04001229 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001230 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001231 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1232 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001233 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001234
Yan Zheng2b820322008-11-17 21:11:30 -05001235 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001236out:
1237 btrfs_free_path(path);
1238 return ret;
1239}
Chris Mason8f18cf12008-04-25 16:53:30 -04001240
Chris Masona061fc82008-05-07 11:43:44 -04001241static int btrfs_rm_dev_item(struct btrfs_root *root,
1242 struct btrfs_device *device)
1243{
1244 int ret;
1245 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001246 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001247 struct btrfs_trans_handle *trans;
1248
1249 root = root->fs_info->chunk_root;
1250
1251 path = btrfs_alloc_path();
1252 if (!path)
1253 return -ENOMEM;
1254
Yan, Zhenga22285a2010-05-16 10:48:46 -04001255 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001256 if (IS_ERR(trans)) {
1257 btrfs_free_path(path);
1258 return PTR_ERR(trans);
1259 }
Chris Masona061fc82008-05-07 11:43:44 -04001260 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1261 key.type = BTRFS_DEV_ITEM_KEY;
1262 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001263 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001264
1265 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1266 if (ret < 0)
1267 goto out;
1268
1269 if (ret > 0) {
1270 ret = -ENOENT;
1271 goto out;
1272 }
1273
1274 ret = btrfs_del_item(trans, root, path);
1275 if (ret)
1276 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001277out:
1278 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001279 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001280 btrfs_commit_transaction(trans, root);
1281 return ret;
1282}
1283
1284int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1285{
1286 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001287 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001288 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001289 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001290 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001291 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001292 u64 all_avail;
1293 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001294 u64 num_devices;
1295 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001296 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001297 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001298
Chris Masona061fc82008-05-07 11:43:44 -04001299 mutex_lock(&uuid_mutex);
1300
1301 all_avail = root->fs_info->avail_data_alloc_bits |
1302 root->fs_info->avail_system_alloc_bits |
1303 root->fs_info->avail_metadata_alloc_bits;
1304
1305 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001306 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001307 printk(KERN_ERR "btrfs: unable to go below four devices "
1308 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001309 ret = -EINVAL;
1310 goto out;
1311 }
1312
1313 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001314 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001315 printk(KERN_ERR "btrfs: unable to go below two "
1316 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001317 ret = -EINVAL;
1318 goto out;
1319 }
1320
Chris Masondfe25022008-05-13 13:46:40 -04001321 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001322 struct list_head *devices;
1323 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001324
Chris Masondfe25022008-05-13 13:46:40 -04001325 device = NULL;
1326 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001327 /*
1328 * It is safe to read the devices since the volume_mutex
1329 * is held.
1330 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001331 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001332 if (tmp->in_fs_metadata && !tmp->bdev) {
1333 device = tmp;
1334 break;
1335 }
1336 }
1337 bdev = NULL;
1338 bh = NULL;
1339 disk_super = NULL;
1340 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001341 printk(KERN_ERR "btrfs: no missing devices found to "
1342 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001343 goto out;
1344 }
Chris Masondfe25022008-05-13 13:46:40 -04001345 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001346 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1347 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001348 if (IS_ERR(bdev)) {
1349 ret = PTR_ERR(bdev);
1350 goto out;
1351 }
1352
Yan Zheng2b820322008-11-17 21:11:30 -05001353 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001354 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001355 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001356 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001357 goto error_close;
1358 }
1359 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001360 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001361 dev_uuid = disk_super->dev_item.uuid;
1362 device = btrfs_find_device(root, devid, dev_uuid,
1363 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001364 if (!device) {
1365 ret = -ENOENT;
1366 goto error_brelse;
1367 }
Chris Masondfe25022008-05-13 13:46:40 -04001368 }
Yan Zheng2b820322008-11-17 21:11:30 -05001369
1370 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001371 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1372 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001373 ret = -EINVAL;
1374 goto error_brelse;
1375 }
1376
1377 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001378 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001379 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001380 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001381 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001382 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001383 }
Chris Masona061fc82008-05-07 11:43:44 -04001384
1385 ret = btrfs_shrink_device(device, 0);
1386 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001387 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001388
Chris Masona061fc82008-05-07 11:43:44 -04001389 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1390 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001391 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001392
Josef Bacik2bf64752011-09-26 17:12:22 -04001393 spin_lock(&root->fs_info->free_chunk_lock);
1394 root->fs_info->free_chunk_space = device->total_bytes -
1395 device->bytes_used;
1396 spin_unlock(&root->fs_info->free_chunk_lock);
1397
Yan Zheng2b820322008-11-17 21:11:30 -05001398 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001399 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001400
1401 /*
1402 * the device list mutex makes sure that we don't change
1403 * the device list while someone else is writing out all
1404 * the device supers.
1405 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001406
1407 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001408 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001409 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001410
Yan Zhenge4404d62008-12-12 10:03:26 -05001411 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001412
Chris Masoncd02dca2010-12-13 14:56:23 -05001413 if (device->missing)
1414 root->fs_info->fs_devices->missing_devices--;
1415
Yan Zheng2b820322008-11-17 21:11:30 -05001416 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1417 struct btrfs_device, dev_list);
1418 if (device->bdev == root->fs_info->sb->s_bdev)
1419 root->fs_info->sb->s_bdev = next_device->bdev;
1420 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1421 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1422
Xiao Guangrong1f781602011-04-20 10:09:16 +00001423 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001424 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001425
1426 call_rcu(&device->rcu, free_device);
1427 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001428
David Sterba6c417612011-04-13 15:41:04 +02001429 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1430 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001431
Xiao Guangrong1f781602011-04-20 10:09:16 +00001432 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001433 struct btrfs_fs_devices *fs_devices;
1434 fs_devices = root->fs_info->fs_devices;
1435 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001436 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001437 break;
1438 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001439 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001440 fs_devices->seed = cur_devices->seed;
1441 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001442 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001443 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001444 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001445 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001446 }
1447
1448 /*
1449 * at this point, the device is zero sized. We want to
1450 * remove it from the devices list and zero out the old super
1451 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001452 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001453 /* make sure this device isn't detected as part of
1454 * the FS anymore
1455 */
1456 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1457 set_buffer_dirty(bh);
1458 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001459 }
Chris Masona061fc82008-05-07 11:43:44 -04001460
Chris Masona061fc82008-05-07 11:43:44 -04001461 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001462
1463error_brelse:
1464 brelse(bh);
1465error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001466 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001467 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001468out:
1469 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001470 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001471error_undo:
1472 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001473 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001474 list_add(&device->dev_alloc_list,
1475 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001476 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001477 root->fs_info->fs_devices->rw_devices++;
1478 }
1479 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001480}
1481
Yan Zheng2b820322008-11-17 21:11:30 -05001482/*
1483 * does all the dirty work required for changing file system's UUID.
1484 */
Li Zefan125ccb02011-12-08 15:07:24 +08001485static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001486{
1487 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1488 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001489 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001490 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001491 struct btrfs_device *device;
1492 u64 super_flags;
1493
1494 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001495 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001496 return -EINVAL;
1497
Yan Zhenge4404d62008-12-12 10:03:26 -05001498 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1499 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001500 return -ENOMEM;
1501
Yan Zhenge4404d62008-12-12 10:03:26 -05001502 old_devices = clone_fs_devices(fs_devices);
1503 if (IS_ERR(old_devices)) {
1504 kfree(seed_devices);
1505 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001506 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001507
Yan Zheng2b820322008-11-17 21:11:30 -05001508 list_add(&old_devices->list, &fs_uuids);
1509
Yan Zhenge4404d62008-12-12 10:03:26 -05001510 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1511 seed_devices->opened = 1;
1512 INIT_LIST_HEAD(&seed_devices->devices);
1513 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001514 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001515
1516 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001517 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1518 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001519 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1520
Yan Zhenge4404d62008-12-12 10:03:26 -05001521 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1522 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1523 device->fs_devices = seed_devices;
1524 }
1525
Yan Zheng2b820322008-11-17 21:11:30 -05001526 fs_devices->seeding = 0;
1527 fs_devices->num_devices = 0;
1528 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001529 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001530
1531 generate_random_uuid(fs_devices->fsid);
1532 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1533 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1534 super_flags = btrfs_super_flags(disk_super) &
1535 ~BTRFS_SUPER_FLAG_SEEDING;
1536 btrfs_set_super_flags(disk_super, super_flags);
1537
1538 return 0;
1539}
1540
1541/*
1542 * strore the expected generation for seed devices in device items.
1543 */
1544static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1545 struct btrfs_root *root)
1546{
1547 struct btrfs_path *path;
1548 struct extent_buffer *leaf;
1549 struct btrfs_dev_item *dev_item;
1550 struct btrfs_device *device;
1551 struct btrfs_key key;
1552 u8 fs_uuid[BTRFS_UUID_SIZE];
1553 u8 dev_uuid[BTRFS_UUID_SIZE];
1554 u64 devid;
1555 int ret;
1556
1557 path = btrfs_alloc_path();
1558 if (!path)
1559 return -ENOMEM;
1560
1561 root = root->fs_info->chunk_root;
1562 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1563 key.offset = 0;
1564 key.type = BTRFS_DEV_ITEM_KEY;
1565
1566 while (1) {
1567 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1568 if (ret < 0)
1569 goto error;
1570
1571 leaf = path->nodes[0];
1572next_slot:
1573 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1574 ret = btrfs_next_leaf(root, path);
1575 if (ret > 0)
1576 break;
1577 if (ret < 0)
1578 goto error;
1579 leaf = path->nodes[0];
1580 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001581 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001582 continue;
1583 }
1584
1585 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1586 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1587 key.type != BTRFS_DEV_ITEM_KEY)
1588 break;
1589
1590 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1591 struct btrfs_dev_item);
1592 devid = btrfs_device_id(leaf, dev_item);
1593 read_extent_buffer(leaf, dev_uuid,
1594 (unsigned long)btrfs_device_uuid(dev_item),
1595 BTRFS_UUID_SIZE);
1596 read_extent_buffer(leaf, fs_uuid,
1597 (unsigned long)btrfs_device_fsid(dev_item),
1598 BTRFS_UUID_SIZE);
1599 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1600 BUG_ON(!device);
1601
1602 if (device->fs_devices->seeding) {
1603 btrfs_set_device_generation(leaf, dev_item,
1604 device->generation);
1605 btrfs_mark_buffer_dirty(leaf);
1606 }
1607
1608 path->slots[0]++;
1609 goto next_slot;
1610 }
1611 ret = 0;
1612error:
1613 btrfs_free_path(path);
1614 return ret;
1615}
1616
Chris Mason788f20e2008-04-28 15:29:42 -04001617int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1618{
Josef Bacikd5e20032011-08-04 14:52:27 +00001619 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001620 struct btrfs_trans_handle *trans;
1621 struct btrfs_device *device;
1622 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001623 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001624 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001625 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001626 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001627 int ret = 0;
1628
Yan Zheng2b820322008-11-17 21:11:30 -05001629 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1630 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001631
Li Zefana5d16332011-12-07 20:08:40 -05001632 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001633 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001634 if (IS_ERR(bdev))
1635 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001636
Yan Zheng2b820322008-11-17 21:11:30 -05001637 if (root->fs_info->fs_devices->seeding) {
1638 seeding_dev = 1;
1639 down_write(&sb->s_umount);
1640 mutex_lock(&uuid_mutex);
1641 }
1642
Chris Mason8c8bee12008-09-29 11:19:10 -04001643 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001644
Chris Mason788f20e2008-04-28 15:29:42 -04001645 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001646 /*
1647 * we have the volume lock, so we don't need the extra
1648 * device list mutex while reading the list here.
1649 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001650 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001651 if (device->bdev == bdev) {
1652 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001653 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001654 }
1655 }
1656
1657 device = kzalloc(sizeof(*device), GFP_NOFS);
1658 if (!device) {
1659 /* we can safely leave the fs_devices entry around */
1660 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001661 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001662 }
1663
Chris Mason788f20e2008-04-28 15:29:42 -04001664 device->name = kstrdup(device_path, GFP_NOFS);
1665 if (!device->name) {
1666 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001667 ret = -ENOMEM;
1668 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001669 }
Yan Zheng2b820322008-11-17 21:11:30 -05001670
1671 ret = find_next_devid(root, &device->devid);
1672 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001673 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001674 kfree(device);
1675 goto error;
1676 }
1677
Yan, Zhenga22285a2010-05-16 10:48:46 -04001678 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001679 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001680 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001681 kfree(device);
1682 ret = PTR_ERR(trans);
1683 goto error;
1684 }
1685
Yan Zheng2b820322008-11-17 21:11:30 -05001686 lock_chunks(root);
1687
Josef Bacikd5e20032011-08-04 14:52:27 +00001688 q = bdev_get_queue(bdev);
1689 if (blk_queue_discard(q))
1690 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001691 device->writeable = 1;
1692 device->work.func = pending_bios_fn;
1693 generate_random_uuid(device->uuid);
1694 spin_lock_init(&device->io_lock);
1695 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001696 device->io_width = root->sectorsize;
1697 device->io_align = root->sectorsize;
1698 device->sector_size = root->sectorsize;
1699 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001700 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001701 device->dev_root = root->fs_info->dev_root;
1702 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001703 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001704 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001705 set_blocksize(device->bdev, 4096);
1706
Yan Zheng2b820322008-11-17 21:11:30 -05001707 if (seeding_dev) {
1708 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001709 ret = btrfs_prepare_sprout(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001710 BUG_ON(ret);
1711 }
1712
1713 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001714
1715 /*
1716 * we don't want write_supers to jump in here with our device
1717 * half setup
1718 */
1719 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001720 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001721 list_add(&device->dev_alloc_list,
1722 &root->fs_info->fs_devices->alloc_list);
1723 root->fs_info->fs_devices->num_devices++;
1724 root->fs_info->fs_devices->open_devices++;
1725 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001726 if (device->can_discard)
1727 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001728 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1729
Josef Bacik2bf64752011-09-26 17:12:22 -04001730 spin_lock(&root->fs_info->free_chunk_lock);
1731 root->fs_info->free_chunk_space += device->total_bytes;
1732 spin_unlock(&root->fs_info->free_chunk_lock);
1733
Chris Masonc289811c2009-06-10 09:51:32 -04001734 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1735 root->fs_info->fs_devices->rotating = 1;
1736
David Sterba6c417612011-04-13 15:41:04 +02001737 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1738 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001739 total_bytes + device->total_bytes);
1740
David Sterba6c417612011-04-13 15:41:04 +02001741 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1742 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001743 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001744 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001745
Yan Zheng2b820322008-11-17 21:11:30 -05001746 if (seeding_dev) {
1747 ret = init_first_rw_device(trans, root, device);
1748 BUG_ON(ret);
1749 ret = btrfs_finish_sprout(trans, root);
1750 BUG_ON(ret);
1751 } else {
1752 ret = btrfs_add_device(trans, root, device);
1753 }
1754
Chris Mason913d9522009-03-10 13:17:18 -04001755 /*
1756 * we've got more storage, clear any full flags on the space
1757 * infos
1758 */
1759 btrfs_clear_space_info_full(root->fs_info);
1760
Chris Mason7d9eb122008-07-08 14:19:17 -04001761 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001762 btrfs_commit_transaction(trans, root);
1763
1764 if (seeding_dev) {
1765 mutex_unlock(&uuid_mutex);
1766 up_write(&sb->s_umount);
1767
1768 ret = btrfs_relocate_sys_chunks(root);
1769 BUG_ON(ret);
1770 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001771
Chris Mason788f20e2008-04-28 15:29:42 -04001772 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001773error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001774 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001775 if (seeding_dev) {
1776 mutex_unlock(&uuid_mutex);
1777 up_write(&sb->s_umount);
1778 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001779 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001780}
1781
Chris Masond3977122009-01-05 21:25:51 -05001782static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1783 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001784{
1785 int ret;
1786 struct btrfs_path *path;
1787 struct btrfs_root *root;
1788 struct btrfs_dev_item *dev_item;
1789 struct extent_buffer *leaf;
1790 struct btrfs_key key;
1791
1792 root = device->dev_root->fs_info->chunk_root;
1793
1794 path = btrfs_alloc_path();
1795 if (!path)
1796 return -ENOMEM;
1797
1798 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1799 key.type = BTRFS_DEV_ITEM_KEY;
1800 key.offset = device->devid;
1801
1802 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1803 if (ret < 0)
1804 goto out;
1805
1806 if (ret > 0) {
1807 ret = -ENOENT;
1808 goto out;
1809 }
1810
1811 leaf = path->nodes[0];
1812 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1813
1814 btrfs_set_device_id(leaf, dev_item, device->devid);
1815 btrfs_set_device_type(leaf, dev_item, device->type);
1816 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1817 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1818 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001819 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001820 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1821 btrfs_mark_buffer_dirty(leaf);
1822
1823out:
1824 btrfs_free_path(path);
1825 return ret;
1826}
1827
Chris Mason7d9eb122008-07-08 14:19:17 -04001828static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001829 struct btrfs_device *device, u64 new_size)
1830{
1831 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001832 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001833 u64 old_total = btrfs_super_total_bytes(super_copy);
1834 u64 diff = new_size - device->total_bytes;
1835
Yan Zheng2b820322008-11-17 21:11:30 -05001836 if (!device->writeable)
1837 return -EACCES;
1838 if (new_size <= device->total_bytes)
1839 return -EINVAL;
1840
Chris Mason8f18cf12008-04-25 16:53:30 -04001841 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001842 device->fs_devices->total_rw_bytes += diff;
1843
1844 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001845 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001846 btrfs_clear_space_info_full(device->dev_root->fs_info);
1847
Chris Mason8f18cf12008-04-25 16:53:30 -04001848 return btrfs_update_device(trans, device);
1849}
1850
Chris Mason7d9eb122008-07-08 14:19:17 -04001851int btrfs_grow_device(struct btrfs_trans_handle *trans,
1852 struct btrfs_device *device, u64 new_size)
1853{
1854 int ret;
1855 lock_chunks(device->dev_root);
1856 ret = __btrfs_grow_device(trans, device, new_size);
1857 unlock_chunks(device->dev_root);
1858 return ret;
1859}
1860
Chris Mason8f18cf12008-04-25 16:53:30 -04001861static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1862 struct btrfs_root *root,
1863 u64 chunk_tree, u64 chunk_objectid,
1864 u64 chunk_offset)
1865{
1866 int ret;
1867 struct btrfs_path *path;
1868 struct btrfs_key key;
1869
1870 root = root->fs_info->chunk_root;
1871 path = btrfs_alloc_path();
1872 if (!path)
1873 return -ENOMEM;
1874
1875 key.objectid = chunk_objectid;
1876 key.offset = chunk_offset;
1877 key.type = BTRFS_CHUNK_ITEM_KEY;
1878
1879 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1880 BUG_ON(ret);
1881
1882 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001883
1884 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001885 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001886}
1887
Christoph Hellwigb2950862008-12-02 09:54:17 -05001888static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001889 chunk_offset)
1890{
David Sterba6c417612011-04-13 15:41:04 +02001891 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001892 struct btrfs_disk_key *disk_key;
1893 struct btrfs_chunk *chunk;
1894 u8 *ptr;
1895 int ret = 0;
1896 u32 num_stripes;
1897 u32 array_size;
1898 u32 len = 0;
1899 u32 cur;
1900 struct btrfs_key key;
1901
1902 array_size = btrfs_super_sys_array_size(super_copy);
1903
1904 ptr = super_copy->sys_chunk_array;
1905 cur = 0;
1906
1907 while (cur < array_size) {
1908 disk_key = (struct btrfs_disk_key *)ptr;
1909 btrfs_disk_key_to_cpu(&key, disk_key);
1910
1911 len = sizeof(*disk_key);
1912
1913 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1914 chunk = (struct btrfs_chunk *)(ptr + len);
1915 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1916 len += btrfs_chunk_item_size(num_stripes);
1917 } else {
1918 ret = -EIO;
1919 break;
1920 }
1921 if (key.objectid == chunk_objectid &&
1922 key.offset == chunk_offset) {
1923 memmove(ptr, ptr + len, array_size - (cur + len));
1924 array_size -= len;
1925 btrfs_set_super_sys_array_size(super_copy, array_size);
1926 } else {
1927 ptr += len;
1928 cur += len;
1929 }
1930 }
1931 return ret;
1932}
1933
Christoph Hellwigb2950862008-12-02 09:54:17 -05001934static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001935 u64 chunk_tree, u64 chunk_objectid,
1936 u64 chunk_offset)
1937{
1938 struct extent_map_tree *em_tree;
1939 struct btrfs_root *extent_root;
1940 struct btrfs_trans_handle *trans;
1941 struct extent_map *em;
1942 struct map_lookup *map;
1943 int ret;
1944 int i;
1945
1946 root = root->fs_info->chunk_root;
1947 extent_root = root->fs_info->extent_root;
1948 em_tree = &root->fs_info->mapping_tree.map_tree;
1949
Josef Bacikba1bf482009-09-11 16:11:19 -04001950 ret = btrfs_can_relocate(extent_root, chunk_offset);
1951 if (ret)
1952 return -ENOSPC;
1953
Chris Mason8f18cf12008-04-25 16:53:30 -04001954 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001955 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001956 if (ret)
1957 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001958
Yan, Zhenga22285a2010-05-16 10:48:46 -04001959 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001960 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001961
Chris Mason7d9eb122008-07-08 14:19:17 -04001962 lock_chunks(root);
1963
Chris Mason8f18cf12008-04-25 16:53:30 -04001964 /*
1965 * step two, delete the device extents and the
1966 * chunk tree entries
1967 */
Chris Mason890871b2009-09-02 16:24:52 -04001968 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001969 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001970 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001971
Tsutomu Itoh285190d2012-02-16 16:23:58 +09001972 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04001973 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001974 map = (struct map_lookup *)em->bdev;
1975
1976 for (i = 0; i < map->num_stripes; i++) {
1977 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1978 map->stripes[i].physical);
1979 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001980
Chris Masondfe25022008-05-13 13:46:40 -04001981 if (map->stripes[i].dev) {
1982 ret = btrfs_update_device(trans, map->stripes[i].dev);
1983 BUG_ON(ret);
1984 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001985 }
1986 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1987 chunk_offset);
1988
1989 BUG_ON(ret);
1990
liubo1abe9b82011-03-24 11:18:59 +00001991 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1992
Chris Mason8f18cf12008-04-25 16:53:30 -04001993 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1994 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1995 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001996 }
1997
Zheng Yan1a40e232008-09-26 10:09:34 -04001998 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1999 BUG_ON(ret);
2000
Chris Mason890871b2009-09-02 16:24:52 -04002001 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002002 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002003 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002004
Chris Mason8f18cf12008-04-25 16:53:30 -04002005 kfree(map);
2006 em->bdev = NULL;
2007
2008 /* once for the tree */
2009 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002010 /* once for us */
2011 free_extent_map(em);
2012
Chris Mason7d9eb122008-07-08 14:19:17 -04002013 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002014 btrfs_end_transaction(trans, root);
2015 return 0;
2016}
2017
Yan Zheng2b820322008-11-17 21:11:30 -05002018static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2019{
2020 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2021 struct btrfs_path *path;
2022 struct extent_buffer *leaf;
2023 struct btrfs_chunk *chunk;
2024 struct btrfs_key key;
2025 struct btrfs_key found_key;
2026 u64 chunk_tree = chunk_root->root_key.objectid;
2027 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002028 bool retried = false;
2029 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002030 int ret;
2031
2032 path = btrfs_alloc_path();
2033 if (!path)
2034 return -ENOMEM;
2035
Josef Bacikba1bf482009-09-11 16:11:19 -04002036again:
Yan Zheng2b820322008-11-17 21:11:30 -05002037 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2038 key.offset = (u64)-1;
2039 key.type = BTRFS_CHUNK_ITEM_KEY;
2040
2041 while (1) {
2042 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2043 if (ret < 0)
2044 goto error;
2045 BUG_ON(ret == 0);
2046
2047 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2048 key.type);
2049 if (ret < 0)
2050 goto error;
2051 if (ret > 0)
2052 break;
2053
2054 leaf = path->nodes[0];
2055 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2056
2057 chunk = btrfs_item_ptr(leaf, path->slots[0],
2058 struct btrfs_chunk);
2059 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002060 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002061
2062 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2063 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2064 found_key.objectid,
2065 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002066 if (ret == -ENOSPC)
2067 failed++;
2068 else if (ret)
2069 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002070 }
2071
2072 if (found_key.offset == 0)
2073 break;
2074 key.offset = found_key.offset - 1;
2075 }
2076 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002077 if (failed && !retried) {
2078 failed = 0;
2079 retried = true;
2080 goto again;
2081 } else if (failed && retried) {
2082 WARN_ON(1);
2083 ret = -ENOSPC;
2084 }
Yan Zheng2b820322008-11-17 21:11:30 -05002085error:
2086 btrfs_free_path(path);
2087 return ret;
2088}
2089
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002090static int insert_balance_item(struct btrfs_root *root,
2091 struct btrfs_balance_control *bctl)
2092{
2093 struct btrfs_trans_handle *trans;
2094 struct btrfs_balance_item *item;
2095 struct btrfs_disk_balance_args disk_bargs;
2096 struct btrfs_path *path;
2097 struct extent_buffer *leaf;
2098 struct btrfs_key key;
2099 int ret, err;
2100
2101 path = btrfs_alloc_path();
2102 if (!path)
2103 return -ENOMEM;
2104
2105 trans = btrfs_start_transaction(root, 0);
2106 if (IS_ERR(trans)) {
2107 btrfs_free_path(path);
2108 return PTR_ERR(trans);
2109 }
2110
2111 key.objectid = BTRFS_BALANCE_OBJECTID;
2112 key.type = BTRFS_BALANCE_ITEM_KEY;
2113 key.offset = 0;
2114
2115 ret = btrfs_insert_empty_item(trans, root, path, &key,
2116 sizeof(*item));
2117 if (ret)
2118 goto out;
2119
2120 leaf = path->nodes[0];
2121 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2122
2123 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2124
2125 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2126 btrfs_set_balance_data(leaf, item, &disk_bargs);
2127 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2128 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2129 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2130 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2131
2132 btrfs_set_balance_flags(leaf, item, bctl->flags);
2133
2134 btrfs_mark_buffer_dirty(leaf);
2135out:
2136 btrfs_free_path(path);
2137 err = btrfs_commit_transaction(trans, root);
2138 if (err && !ret)
2139 ret = err;
2140 return ret;
2141}
2142
2143static int del_balance_item(struct btrfs_root *root)
2144{
2145 struct btrfs_trans_handle *trans;
2146 struct btrfs_path *path;
2147 struct btrfs_key key;
2148 int ret, err;
2149
2150 path = btrfs_alloc_path();
2151 if (!path)
2152 return -ENOMEM;
2153
2154 trans = btrfs_start_transaction(root, 0);
2155 if (IS_ERR(trans)) {
2156 btrfs_free_path(path);
2157 return PTR_ERR(trans);
2158 }
2159
2160 key.objectid = BTRFS_BALANCE_OBJECTID;
2161 key.type = BTRFS_BALANCE_ITEM_KEY;
2162 key.offset = 0;
2163
2164 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2165 if (ret < 0)
2166 goto out;
2167 if (ret > 0) {
2168 ret = -ENOENT;
2169 goto out;
2170 }
2171
2172 ret = btrfs_del_item(trans, root, path);
2173out:
2174 btrfs_free_path(path);
2175 err = btrfs_commit_transaction(trans, root);
2176 if (err && !ret)
2177 ret = err;
2178 return ret;
2179}
2180
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002181/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002182 * This is a heuristic used to reduce the number of chunks balanced on
2183 * resume after balance was interrupted.
2184 */
2185static void update_balance_args(struct btrfs_balance_control *bctl)
2186{
2187 /*
2188 * Turn on soft mode for chunk types that were being converted.
2189 */
2190 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2191 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2192 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2193 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2194 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2195 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2196
2197 /*
2198 * Turn on usage filter if is not already used. The idea is
2199 * that chunks that we have already balanced should be
2200 * reasonably full. Don't do it for chunks that are being
2201 * converted - that will keep us from relocating unconverted
2202 * (albeit full) chunks.
2203 */
2204 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2205 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2206 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2207 bctl->data.usage = 90;
2208 }
2209 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2210 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2211 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2212 bctl->sys.usage = 90;
2213 }
2214 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2215 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2216 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2217 bctl->meta.usage = 90;
2218 }
2219}
2220
2221/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002222 * Should be called with both balance and volume mutexes held to
2223 * serialize other volume operations (add_dev/rm_dev/resize) with
2224 * restriper. Same goes for unset_balance_control.
2225 */
2226static void set_balance_control(struct btrfs_balance_control *bctl)
2227{
2228 struct btrfs_fs_info *fs_info = bctl->fs_info;
2229
2230 BUG_ON(fs_info->balance_ctl);
2231
2232 spin_lock(&fs_info->balance_lock);
2233 fs_info->balance_ctl = bctl;
2234 spin_unlock(&fs_info->balance_lock);
2235}
2236
2237static void unset_balance_control(struct btrfs_fs_info *fs_info)
2238{
2239 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2240
2241 BUG_ON(!fs_info->balance_ctl);
2242
2243 spin_lock(&fs_info->balance_lock);
2244 fs_info->balance_ctl = NULL;
2245 spin_unlock(&fs_info->balance_lock);
2246
2247 kfree(bctl);
2248}
2249
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002250/*
2251 * Balance filters. Return 1 if chunk should be filtered out
2252 * (should not be balanced).
2253 */
2254static int chunk_profiles_filter(u64 chunk_profile,
2255 struct btrfs_balance_args *bargs)
2256{
2257 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2258
2259 if (chunk_profile == 0)
2260 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2261
2262 if (bargs->profiles & chunk_profile)
2263 return 0;
2264
2265 return 1;
2266}
2267
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002268static u64 div_factor_fine(u64 num, int factor)
2269{
2270 if (factor <= 0)
2271 return 0;
2272 if (factor >= 100)
2273 return num;
2274
2275 num *= factor;
2276 do_div(num, 100);
2277 return num;
2278}
2279
2280static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2281 struct btrfs_balance_args *bargs)
2282{
2283 struct btrfs_block_group_cache *cache;
2284 u64 chunk_used, user_thresh;
2285 int ret = 1;
2286
2287 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2288 chunk_used = btrfs_block_group_used(&cache->item);
2289
2290 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2291 if (chunk_used < user_thresh)
2292 ret = 0;
2293
2294 btrfs_put_block_group(cache);
2295 return ret;
2296}
2297
Ilya Dryomov409d4042012-01-16 22:04:47 +02002298static int chunk_devid_filter(struct extent_buffer *leaf,
2299 struct btrfs_chunk *chunk,
2300 struct btrfs_balance_args *bargs)
2301{
2302 struct btrfs_stripe *stripe;
2303 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2304 int i;
2305
2306 for (i = 0; i < num_stripes; i++) {
2307 stripe = btrfs_stripe_nr(chunk, i);
2308 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2309 return 0;
2310 }
2311
2312 return 1;
2313}
2314
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002315/* [pstart, pend) */
2316static int chunk_drange_filter(struct extent_buffer *leaf,
2317 struct btrfs_chunk *chunk,
2318 u64 chunk_offset,
2319 struct btrfs_balance_args *bargs)
2320{
2321 struct btrfs_stripe *stripe;
2322 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2323 u64 stripe_offset;
2324 u64 stripe_length;
2325 int factor;
2326 int i;
2327
2328 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2329 return 0;
2330
2331 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2332 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2333 factor = 2;
2334 else
2335 factor = 1;
2336 factor = num_stripes / factor;
2337
2338 for (i = 0; i < num_stripes; i++) {
2339 stripe = btrfs_stripe_nr(chunk, i);
2340 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2341 continue;
2342
2343 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2344 stripe_length = btrfs_chunk_length(leaf, chunk);
2345 do_div(stripe_length, factor);
2346
2347 if (stripe_offset < bargs->pend &&
2348 stripe_offset + stripe_length > bargs->pstart)
2349 return 0;
2350 }
2351
2352 return 1;
2353}
2354
Ilya Dryomovea671762012-01-16 22:04:48 +02002355/* [vstart, vend) */
2356static int chunk_vrange_filter(struct extent_buffer *leaf,
2357 struct btrfs_chunk *chunk,
2358 u64 chunk_offset,
2359 struct btrfs_balance_args *bargs)
2360{
2361 if (chunk_offset < bargs->vend &&
2362 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2363 /* at least part of the chunk is inside this vrange */
2364 return 0;
2365
2366 return 1;
2367}
2368
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002369static int chunk_soft_convert_filter(u64 chunk_profile,
2370 struct btrfs_balance_args *bargs)
2371{
2372 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2373 return 0;
2374
2375 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2376
2377 if (chunk_profile == 0)
2378 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2379
2380 if (bargs->target & chunk_profile)
2381 return 1;
2382
2383 return 0;
2384}
2385
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002386static int should_balance_chunk(struct btrfs_root *root,
2387 struct extent_buffer *leaf,
2388 struct btrfs_chunk *chunk, u64 chunk_offset)
2389{
2390 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2391 struct btrfs_balance_args *bargs = NULL;
2392 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2393
2394 /* type filter */
2395 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2396 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2397 return 0;
2398 }
2399
2400 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2401 bargs = &bctl->data;
2402 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2403 bargs = &bctl->sys;
2404 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2405 bargs = &bctl->meta;
2406
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002407 /* profiles filter */
2408 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2409 chunk_profiles_filter(chunk_type, bargs)) {
2410 return 0;
2411 }
2412
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002413 /* usage filter */
2414 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2415 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2416 return 0;
2417 }
2418
Ilya Dryomov409d4042012-01-16 22:04:47 +02002419 /* devid filter */
2420 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2421 chunk_devid_filter(leaf, chunk, bargs)) {
2422 return 0;
2423 }
2424
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002425 /* drange filter, makes sense only with devid filter */
2426 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2427 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2428 return 0;
2429 }
2430
Ilya Dryomovea671762012-01-16 22:04:48 +02002431 /* vrange filter */
2432 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2433 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2434 return 0;
2435 }
2436
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002437 /* soft profile changing mode */
2438 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2439 chunk_soft_convert_filter(chunk_type, bargs)) {
2440 return 0;
2441 }
2442
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002443 return 1;
2444}
2445
Chris Masonec44a352008-04-28 15:29:52 -04002446static u64 div_factor(u64 num, int factor)
2447{
2448 if (factor == 10)
2449 return num;
2450 num *= factor;
2451 do_div(num, 10);
2452 return num;
2453}
2454
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002455static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002456{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002457 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002458 struct btrfs_root *chunk_root = fs_info->chunk_root;
2459 struct btrfs_root *dev_root = fs_info->dev_root;
2460 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002461 struct btrfs_device *device;
2462 u64 old_size;
2463 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002464 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002465 struct btrfs_path *path;
2466 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002467 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002468 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002469 struct extent_buffer *leaf;
2470 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002471 int ret;
2472 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002473 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002474
Chris Masonec44a352008-04-28 15:29:52 -04002475 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002476 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002477 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002478 old_size = device->total_bytes;
2479 size_to_free = div_factor(old_size, 1);
2480 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002481 if (!device->writeable ||
2482 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002483 continue;
2484
2485 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002486 if (ret == -ENOSPC)
2487 break;
Chris Masonec44a352008-04-28 15:29:52 -04002488 BUG_ON(ret);
2489
Yan, Zhenga22285a2010-05-16 10:48:46 -04002490 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002491 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002492
2493 ret = btrfs_grow_device(trans, device, old_size);
2494 BUG_ON(ret);
2495
2496 btrfs_end_transaction(trans, dev_root);
2497 }
2498
2499 /* step two, relocate all the chunks */
2500 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002501 if (!path) {
2502 ret = -ENOMEM;
2503 goto error;
2504 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002505
2506 /* zero out stat counters */
2507 spin_lock(&fs_info->balance_lock);
2508 memset(&bctl->stat, 0, sizeof(bctl->stat));
2509 spin_unlock(&fs_info->balance_lock);
2510again:
Chris Masonec44a352008-04-28 15:29:52 -04002511 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2512 key.offset = (u64)-1;
2513 key.type = BTRFS_CHUNK_ITEM_KEY;
2514
Chris Masond3977122009-01-05 21:25:51 -05002515 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002516 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002517 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002518 ret = -ECANCELED;
2519 goto error;
2520 }
2521
Chris Masonec44a352008-04-28 15:29:52 -04002522 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2523 if (ret < 0)
2524 goto error;
2525
2526 /*
2527 * this shouldn't happen, it means the last relocate
2528 * failed
2529 */
2530 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002531 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002532
2533 ret = btrfs_previous_item(chunk_root, path, 0,
2534 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002535 if (ret) {
2536 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002537 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002538 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002539
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002540 leaf = path->nodes[0];
2541 slot = path->slots[0];
2542 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2543
Chris Masonec44a352008-04-28 15:29:52 -04002544 if (found_key.objectid != key.objectid)
2545 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002546
Chris Masonec44a352008-04-28 15:29:52 -04002547 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002548 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002549 break;
2550
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002551 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2552
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002553 if (!counting) {
2554 spin_lock(&fs_info->balance_lock);
2555 bctl->stat.considered++;
2556 spin_unlock(&fs_info->balance_lock);
2557 }
2558
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002559 ret = should_balance_chunk(chunk_root, leaf, chunk,
2560 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002561 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002562 if (!ret)
2563 goto loop;
2564
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002565 if (counting) {
2566 spin_lock(&fs_info->balance_lock);
2567 bctl->stat.expected++;
2568 spin_unlock(&fs_info->balance_lock);
2569 goto loop;
2570 }
2571
Chris Masonec44a352008-04-28 15:29:52 -04002572 ret = btrfs_relocate_chunk(chunk_root,
2573 chunk_root->root_key.objectid,
2574 found_key.objectid,
2575 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002576 if (ret && ret != -ENOSPC)
2577 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002578 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002579 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002580 } else {
2581 spin_lock(&fs_info->balance_lock);
2582 bctl->stat.completed++;
2583 spin_unlock(&fs_info->balance_lock);
2584 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002585loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002586 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002587 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002588
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002589 if (counting) {
2590 btrfs_release_path(path);
2591 counting = false;
2592 goto again;
2593 }
Chris Masonec44a352008-04-28 15:29:52 -04002594error:
2595 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002596 if (enospc_errors) {
2597 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2598 enospc_errors);
2599 if (!ret)
2600 ret = -ENOSPC;
2601 }
2602
Chris Masonec44a352008-04-28 15:29:52 -04002603 return ret;
2604}
2605
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002606static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2607{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002608 /* cancel requested || normal exit path */
2609 return atomic_read(&fs_info->balance_cancel_req) ||
2610 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2611 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002612}
2613
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002614static void __cancel_balance(struct btrfs_fs_info *fs_info)
2615{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002616 int ret;
2617
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002618 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002619 ret = del_balance_item(fs_info->tree_root);
2620 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002621}
2622
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002623void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002624 struct btrfs_ioctl_balance_args *bargs);
2625
2626/*
2627 * Should be called with both balance and volume mutexes held
2628 */
2629int btrfs_balance(struct btrfs_balance_control *bctl,
2630 struct btrfs_ioctl_balance_args *bargs)
2631{
2632 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002633 u64 allowed;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002634 int ret;
2635
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002636 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002637 atomic_read(&fs_info->balance_pause_req) ||
2638 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002639 ret = -EINVAL;
2640 goto out;
2641 }
2642
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002643 /*
2644 * In case of mixed groups both data and meta should be picked,
2645 * and identical options should be given for both of them.
2646 */
2647 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2648 if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
2649 (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
2650 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2651 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2652 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2653 printk(KERN_ERR "btrfs: with mixed groups data and "
2654 "metadata balance options must be the same\n");
2655 ret = -EINVAL;
2656 goto out;
2657 }
2658 }
2659
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002660 /*
2661 * Profile changing sanity checks. Skip them if a simple
2662 * balance is requested.
2663 */
2664 if (!((bctl->data.flags | bctl->sys.flags | bctl->meta.flags) &
2665 BTRFS_BALANCE_ARGS_CONVERT))
2666 goto do_balance;
2667
2668 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2669 if (fs_info->fs_devices->num_devices == 1)
2670 allowed |= BTRFS_BLOCK_GROUP_DUP;
2671 else if (fs_info->fs_devices->num_devices < 4)
2672 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2673 else
2674 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2675 BTRFS_BLOCK_GROUP_RAID10);
2676
2677 if (!profile_is_valid(bctl->data.target, 1) ||
2678 bctl->data.target & ~allowed) {
2679 printk(KERN_ERR "btrfs: unable to start balance with target "
2680 "data profile %llu\n",
2681 (unsigned long long)bctl->data.target);
2682 ret = -EINVAL;
2683 goto out;
2684 }
2685 if (!profile_is_valid(bctl->meta.target, 1) ||
2686 bctl->meta.target & ~allowed) {
2687 printk(KERN_ERR "btrfs: unable to start balance with target "
2688 "metadata profile %llu\n",
2689 (unsigned long long)bctl->meta.target);
2690 ret = -EINVAL;
2691 goto out;
2692 }
2693 if (!profile_is_valid(bctl->sys.target, 1) ||
2694 bctl->sys.target & ~allowed) {
2695 printk(KERN_ERR "btrfs: unable to start balance with target "
2696 "system profile %llu\n",
2697 (unsigned long long)bctl->sys.target);
2698 ret = -EINVAL;
2699 goto out;
2700 }
2701
2702 if (bctl->data.target & BTRFS_BLOCK_GROUP_DUP) {
2703 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2704 ret = -EINVAL;
2705 goto out;
2706 }
2707
2708 /* allow to reduce meta or sys integrity only if force set */
2709 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2710 BTRFS_BLOCK_GROUP_RAID10;
2711 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2712 (fs_info->avail_system_alloc_bits & allowed) &&
2713 !(bctl->sys.target & allowed)) ||
2714 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2715 (fs_info->avail_metadata_alloc_bits & allowed) &&
2716 !(bctl->meta.target & allowed))) {
2717 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2718 printk(KERN_INFO "btrfs: force reducing metadata "
2719 "integrity\n");
2720 } else {
2721 printk(KERN_ERR "btrfs: balance will reduce metadata "
2722 "integrity, use force if you want this\n");
2723 ret = -EINVAL;
2724 goto out;
2725 }
2726 }
2727
2728do_balance:
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002729 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002730 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002731 goto out;
2732
Ilya Dryomov59641012012-01-16 22:04:48 +02002733 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2734 BUG_ON(ret == -EEXIST);
2735 set_balance_control(bctl);
2736 } else {
2737 BUG_ON(ret != -EEXIST);
2738 spin_lock(&fs_info->balance_lock);
2739 update_balance_args(bctl);
2740 spin_unlock(&fs_info->balance_lock);
2741 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002742
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002743 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002744 mutex_unlock(&fs_info->balance_mutex);
2745
2746 ret = __btrfs_balance(fs_info);
2747
2748 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002749 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002750
2751 if (bargs) {
2752 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002753 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002754 }
2755
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002756 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
2757 balance_need_close(fs_info)) {
2758 __cancel_balance(fs_info);
2759 }
2760
2761 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002762
2763 return ret;
2764out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002765 if (bctl->flags & BTRFS_BALANCE_RESUME)
2766 __cancel_balance(fs_info);
2767 else
2768 kfree(bctl);
2769 return ret;
2770}
2771
2772static int balance_kthread(void *data)
2773{
2774 struct btrfs_balance_control *bctl =
2775 (struct btrfs_balance_control *)data;
2776 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002777 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02002778
2779 mutex_lock(&fs_info->volume_mutex);
2780 mutex_lock(&fs_info->balance_mutex);
2781
2782 set_balance_control(bctl);
2783
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02002784 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
2785 printk(KERN_INFO "btrfs: force skipping balance\n");
2786 } else {
2787 printk(KERN_INFO "btrfs: continuing balance\n");
2788 ret = btrfs_balance(bctl, NULL);
2789 }
Ilya Dryomov59641012012-01-16 22:04:48 +02002790
2791 mutex_unlock(&fs_info->balance_mutex);
2792 mutex_unlock(&fs_info->volume_mutex);
2793 return ret;
2794}
2795
2796int btrfs_recover_balance(struct btrfs_root *tree_root)
2797{
2798 struct task_struct *tsk;
2799 struct btrfs_balance_control *bctl;
2800 struct btrfs_balance_item *item;
2801 struct btrfs_disk_balance_args disk_bargs;
2802 struct btrfs_path *path;
2803 struct extent_buffer *leaf;
2804 struct btrfs_key key;
2805 int ret;
2806
2807 path = btrfs_alloc_path();
2808 if (!path)
2809 return -ENOMEM;
2810
2811 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2812 if (!bctl) {
2813 ret = -ENOMEM;
2814 goto out;
2815 }
2816
2817 key.objectid = BTRFS_BALANCE_OBJECTID;
2818 key.type = BTRFS_BALANCE_ITEM_KEY;
2819 key.offset = 0;
2820
2821 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2822 if (ret < 0)
2823 goto out_bctl;
2824 if (ret > 0) { /* ret = -ENOENT; */
2825 ret = 0;
2826 goto out_bctl;
2827 }
2828
2829 leaf = path->nodes[0];
2830 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2831
2832 bctl->fs_info = tree_root->fs_info;
2833 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2834
2835 btrfs_balance_data(leaf, item, &disk_bargs);
2836 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2837 btrfs_balance_meta(leaf, item, &disk_bargs);
2838 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2839 btrfs_balance_sys(leaf, item, &disk_bargs);
2840 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2841
2842 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2843 if (IS_ERR(tsk))
2844 ret = PTR_ERR(tsk);
2845 else
2846 goto out;
2847
2848out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002849 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002850out:
2851 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002852 return ret;
2853}
2854
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002855int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
2856{
2857 int ret = 0;
2858
2859 mutex_lock(&fs_info->balance_mutex);
2860 if (!fs_info->balance_ctl) {
2861 mutex_unlock(&fs_info->balance_mutex);
2862 return -ENOTCONN;
2863 }
2864
2865 if (atomic_read(&fs_info->balance_running)) {
2866 atomic_inc(&fs_info->balance_pause_req);
2867 mutex_unlock(&fs_info->balance_mutex);
2868
2869 wait_event(fs_info->balance_wait_q,
2870 atomic_read(&fs_info->balance_running) == 0);
2871
2872 mutex_lock(&fs_info->balance_mutex);
2873 /* we are good with balance_ctl ripped off from under us */
2874 BUG_ON(atomic_read(&fs_info->balance_running));
2875 atomic_dec(&fs_info->balance_pause_req);
2876 } else {
2877 ret = -ENOTCONN;
2878 }
2879
2880 mutex_unlock(&fs_info->balance_mutex);
2881 return ret;
2882}
2883
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002884int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
2885{
2886 mutex_lock(&fs_info->balance_mutex);
2887 if (!fs_info->balance_ctl) {
2888 mutex_unlock(&fs_info->balance_mutex);
2889 return -ENOTCONN;
2890 }
2891
2892 atomic_inc(&fs_info->balance_cancel_req);
2893 /*
2894 * if we are running just wait and return, balance item is
2895 * deleted in btrfs_balance in this case
2896 */
2897 if (atomic_read(&fs_info->balance_running)) {
2898 mutex_unlock(&fs_info->balance_mutex);
2899 wait_event(fs_info->balance_wait_q,
2900 atomic_read(&fs_info->balance_running) == 0);
2901 mutex_lock(&fs_info->balance_mutex);
2902 } else {
2903 /* __cancel_balance needs volume_mutex */
2904 mutex_unlock(&fs_info->balance_mutex);
2905 mutex_lock(&fs_info->volume_mutex);
2906 mutex_lock(&fs_info->balance_mutex);
2907
2908 if (fs_info->balance_ctl)
2909 __cancel_balance(fs_info);
2910
2911 mutex_unlock(&fs_info->volume_mutex);
2912 }
2913
2914 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
2915 atomic_dec(&fs_info->balance_cancel_req);
2916 mutex_unlock(&fs_info->balance_mutex);
2917 return 0;
2918}
2919
Chris Mason8f18cf12008-04-25 16:53:30 -04002920/*
2921 * shrinking a device means finding all of the device extents past
2922 * the new size, and then following the back refs to the chunks.
2923 * The chunk relocation code actually frees the device extent
2924 */
2925int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2926{
2927 struct btrfs_trans_handle *trans;
2928 struct btrfs_root *root = device->dev_root;
2929 struct btrfs_dev_extent *dev_extent = NULL;
2930 struct btrfs_path *path;
2931 u64 length;
2932 u64 chunk_tree;
2933 u64 chunk_objectid;
2934 u64 chunk_offset;
2935 int ret;
2936 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002937 int failed = 0;
2938 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002939 struct extent_buffer *l;
2940 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002941 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002942 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002943 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002944 u64 diff = device->total_bytes - new_size;
2945
Yan Zheng2b820322008-11-17 21:11:30 -05002946 if (new_size >= device->total_bytes)
2947 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002948
2949 path = btrfs_alloc_path();
2950 if (!path)
2951 return -ENOMEM;
2952
Chris Mason8f18cf12008-04-25 16:53:30 -04002953 path->reada = 2;
2954
Chris Mason7d9eb122008-07-08 14:19:17 -04002955 lock_chunks(root);
2956
Chris Mason8f18cf12008-04-25 16:53:30 -04002957 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002958 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002959 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002960 spin_lock(&root->fs_info->free_chunk_lock);
2961 root->fs_info->free_chunk_space -= diff;
2962 spin_unlock(&root->fs_info->free_chunk_lock);
2963 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002964 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002965
Josef Bacikba1bf482009-09-11 16:11:19 -04002966again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002967 key.objectid = device->devid;
2968 key.offset = (u64)-1;
2969 key.type = BTRFS_DEV_EXTENT_KEY;
2970
2971 while (1) {
2972 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2973 if (ret < 0)
2974 goto done;
2975
2976 ret = btrfs_previous_item(root, path, 0, key.type);
2977 if (ret < 0)
2978 goto done;
2979 if (ret) {
2980 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002981 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002982 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002983 }
2984
2985 l = path->nodes[0];
2986 slot = path->slots[0];
2987 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2988
Josef Bacikba1bf482009-09-11 16:11:19 -04002989 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002990 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002991 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002992 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002993
2994 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2995 length = btrfs_dev_extent_length(l, dev_extent);
2996
Josef Bacikba1bf482009-09-11 16:11:19 -04002997 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002998 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002999 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003000 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003001
3002 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3003 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3004 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003005 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003006
3007 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3008 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003009 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003010 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003011 if (ret == -ENOSPC)
3012 failed++;
3013 key.offset -= 1;
3014 }
3015
3016 if (failed && !retried) {
3017 failed = 0;
3018 retried = true;
3019 goto again;
3020 } else if (failed && retried) {
3021 ret = -ENOSPC;
3022 lock_chunks(root);
3023
3024 device->total_bytes = old_size;
3025 if (device->writeable)
3026 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003027 spin_lock(&root->fs_info->free_chunk_lock);
3028 root->fs_info->free_chunk_space += diff;
3029 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003030 unlock_chunks(root);
3031 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003032 }
3033
Chris Balld6397ba2009-04-27 07:29:03 -04003034 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003035 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003036 if (IS_ERR(trans)) {
3037 ret = PTR_ERR(trans);
3038 goto done;
3039 }
3040
Chris Balld6397ba2009-04-27 07:29:03 -04003041 lock_chunks(root);
3042
3043 device->disk_total_bytes = new_size;
3044 /* Now btrfs_update_device() will change the on-disk size. */
3045 ret = btrfs_update_device(trans, device);
3046 if (ret) {
3047 unlock_chunks(root);
3048 btrfs_end_transaction(trans, root);
3049 goto done;
3050 }
3051 WARN_ON(diff > old_total);
3052 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3053 unlock_chunks(root);
3054 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003055done:
3056 btrfs_free_path(path);
3057 return ret;
3058}
3059
Li Zefan125ccb02011-12-08 15:07:24 +08003060static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003061 struct btrfs_key *key,
3062 struct btrfs_chunk *chunk, int item_size)
3063{
David Sterba6c417612011-04-13 15:41:04 +02003064 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003065 struct btrfs_disk_key disk_key;
3066 u32 array_size;
3067 u8 *ptr;
3068
3069 array_size = btrfs_super_sys_array_size(super_copy);
3070 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3071 return -EFBIG;
3072
3073 ptr = super_copy->sys_chunk_array + array_size;
3074 btrfs_cpu_key_to_disk(&disk_key, key);
3075 memcpy(ptr, &disk_key, sizeof(disk_key));
3076 ptr += sizeof(disk_key);
3077 memcpy(ptr, chunk, item_size);
3078 item_size += sizeof(disk_key);
3079 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3080 return 0;
3081}
3082
Miao Xieb2117a32011-01-05 10:07:28 +00003083/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003084 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003085 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003086static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003087{
Arne Jansen73c5de02011-04-12 12:07:57 +02003088 const struct btrfs_device_info *di_a = a;
3089 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003090
Arne Jansen73c5de02011-04-12 12:07:57 +02003091 if (di_a->max_avail > di_b->max_avail)
3092 return -1;
3093 if (di_a->max_avail < di_b->max_avail)
3094 return 1;
3095 if (di_a->total_avail > di_b->total_avail)
3096 return -1;
3097 if (di_a->total_avail < di_b->total_avail)
3098 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003099 return 0;
3100}
3101
3102static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3103 struct btrfs_root *extent_root,
3104 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003105 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003106 u64 start, u64 type)
3107{
3108 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003109 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3110 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003111 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003112 struct extent_map_tree *em_tree;
3113 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003114 struct btrfs_device_info *devices_info = NULL;
3115 u64 total_avail;
3116 int num_stripes; /* total number of stripes to allocate */
3117 int sub_stripes; /* sub_stripes info for map */
3118 int dev_stripes; /* stripes per dev */
3119 int devs_max; /* max devs to use */
3120 int devs_min; /* min devs needed */
3121 int devs_increment; /* ndevs has to be a multiple of this */
3122 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003123 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003124 u64 max_stripe_size;
3125 u64 max_chunk_size;
3126 u64 stripe_size;
3127 u64 num_bytes;
3128 int ndevs;
3129 int i;
3130 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00003131
3132 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
3133 (type & BTRFS_BLOCK_GROUP_DUP)) {
3134 WARN_ON(1);
3135 type &= ~BTRFS_BLOCK_GROUP_DUP;
3136 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003137
Miao Xieb2117a32011-01-05 10:07:28 +00003138 if (list_empty(&fs_devices->alloc_list))
3139 return -ENOSPC;
3140
Arne Jansen73c5de02011-04-12 12:07:57 +02003141 sub_stripes = 1;
3142 dev_stripes = 1;
3143 devs_increment = 1;
3144 ncopies = 1;
3145 devs_max = 0; /* 0 == as many as possible */
3146 devs_min = 1;
3147
3148 /*
3149 * define the properties of each RAID type.
3150 * FIXME: move this to a global table and use it in all RAID
3151 * calculation code
3152 */
3153 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3154 dev_stripes = 2;
3155 ncopies = 2;
3156 devs_max = 1;
3157 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3158 devs_min = 2;
3159 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3160 devs_increment = 2;
3161 ncopies = 2;
3162 devs_max = 2;
3163 devs_min = 2;
3164 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3165 sub_stripes = 2;
3166 devs_increment = 2;
3167 ncopies = 2;
3168 devs_min = 4;
3169 } else {
3170 devs_max = 1;
3171 }
3172
3173 if (type & BTRFS_BLOCK_GROUP_DATA) {
3174 max_stripe_size = 1024 * 1024 * 1024;
3175 max_chunk_size = 10 * max_stripe_size;
3176 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003177 /* for larger filesystems, use larger metadata chunks */
3178 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3179 max_stripe_size = 1024 * 1024 * 1024;
3180 else
3181 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003182 max_chunk_size = max_stripe_size;
3183 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003184 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003185 max_chunk_size = 2 * max_stripe_size;
3186 } else {
3187 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3188 type);
3189 BUG_ON(1);
3190 }
3191
3192 /* we don't want a chunk larger than 10% of writeable space */
3193 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3194 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003195
3196 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3197 GFP_NOFS);
3198 if (!devices_info)
3199 return -ENOMEM;
3200
Arne Jansen73c5de02011-04-12 12:07:57 +02003201 cur = fs_devices->alloc_list.next;
3202
3203 /*
3204 * in the first pass through the devices list, we gather information
3205 * about the available holes on each device.
3206 */
3207 ndevs = 0;
3208 while (cur != &fs_devices->alloc_list) {
3209 struct btrfs_device *device;
3210 u64 max_avail;
3211 u64 dev_offset;
3212
3213 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3214
3215 cur = cur->next;
3216
3217 if (!device->writeable) {
3218 printk(KERN_ERR
3219 "btrfs: read-only device in alloc_list\n");
3220 WARN_ON(1);
3221 continue;
3222 }
3223
3224 if (!device->in_fs_metadata)
3225 continue;
3226
3227 if (device->total_bytes > device->bytes_used)
3228 total_avail = device->total_bytes - device->bytes_used;
3229 else
3230 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003231
3232 /* If there is no space on this device, skip it. */
3233 if (total_avail == 0)
3234 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003235
Li Zefan125ccb02011-12-08 15:07:24 +08003236 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003237 max_stripe_size * dev_stripes,
3238 &dev_offset, &max_avail);
3239 if (ret && ret != -ENOSPC)
3240 goto error;
3241
3242 if (ret == 0)
3243 max_avail = max_stripe_size * dev_stripes;
3244
3245 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3246 continue;
3247
3248 devices_info[ndevs].dev_offset = dev_offset;
3249 devices_info[ndevs].max_avail = max_avail;
3250 devices_info[ndevs].total_avail = total_avail;
3251 devices_info[ndevs].dev = device;
3252 ++ndevs;
3253 }
3254
3255 /*
3256 * now sort the devices by hole size / available space
3257 */
3258 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3259 btrfs_cmp_device_info, NULL);
3260
3261 /* round down to number of usable stripes */
3262 ndevs -= ndevs % devs_increment;
3263
3264 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3265 ret = -ENOSPC;
3266 goto error;
3267 }
3268
3269 if (devs_max && ndevs > devs_max)
3270 ndevs = devs_max;
3271 /*
3272 * the primary goal is to maximize the number of stripes, so use as many
3273 * devices as possible, even if the stripes are not maximum sized.
3274 */
3275 stripe_size = devices_info[ndevs-1].max_avail;
3276 num_stripes = ndevs * dev_stripes;
3277
3278 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
3279 stripe_size = max_chunk_size * ncopies;
3280 do_div(stripe_size, num_stripes);
3281 }
3282
3283 do_div(stripe_size, dev_stripes);
3284 do_div(stripe_size, BTRFS_STRIPE_LEN);
3285 stripe_size *= BTRFS_STRIPE_LEN;
3286
Miao Xieb2117a32011-01-05 10:07:28 +00003287 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3288 if (!map) {
3289 ret = -ENOMEM;
3290 goto error;
3291 }
3292 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003293
Arne Jansen73c5de02011-04-12 12:07:57 +02003294 for (i = 0; i < ndevs; ++i) {
3295 for (j = 0; j < dev_stripes; ++j) {
3296 int s = i * dev_stripes + j;
3297 map->stripes[s].dev = devices_info[i].dev;
3298 map->stripes[s].physical = devices_info[i].dev_offset +
3299 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003300 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003301 }
Chris Mason593060d2008-03-25 16:50:33 -04003302 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003303 map->stripe_len = BTRFS_STRIPE_LEN;
3304 map->io_align = BTRFS_STRIPE_LEN;
3305 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003306 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003307 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003308
Yan Zheng2b820322008-11-17 21:11:30 -05003309 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003310 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003311
Arne Jansen73c5de02011-04-12 12:07:57 +02003312 *stripe_size_out = stripe_size;
3313 *num_bytes_out = num_bytes;
3314
3315 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003316
David Sterba172ddd62011-04-21 00:48:27 +02003317 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003318 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003319 ret = -ENOMEM;
3320 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003321 }
Chris Mason0b86a832008-03-24 15:01:56 -04003322 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003323 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003324 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003325 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003326 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003327
Chris Mason0b86a832008-03-24 15:01:56 -04003328 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003329 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003330 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003331 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04003332 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003333 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05003334
3335 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3336 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003337 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05003338 BUG_ON(ret);
3339
Arne Jansen73c5de02011-04-12 12:07:57 +02003340 for (i = 0; i < map->num_stripes; ++i) {
3341 struct btrfs_device *device;
3342 u64 dev_offset;
3343
3344 device = map->stripes[i].dev;
3345 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003346
3347 ret = btrfs_alloc_dev_extent(trans, device,
3348 info->chunk_root->root_key.objectid,
3349 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003350 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003351 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003352 }
3353
Miao Xieb2117a32011-01-05 10:07:28 +00003354 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003355 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003356
3357error:
3358 kfree(map);
3359 kfree(devices_info);
3360 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003361}
3362
3363static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3364 struct btrfs_root *extent_root,
3365 struct map_lookup *map, u64 chunk_offset,
3366 u64 chunk_size, u64 stripe_size)
3367{
3368 u64 dev_offset;
3369 struct btrfs_key key;
3370 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3371 struct btrfs_device *device;
3372 struct btrfs_chunk *chunk;
3373 struct btrfs_stripe *stripe;
3374 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3375 int index = 0;
3376 int ret;
3377
3378 chunk = kzalloc(item_size, GFP_NOFS);
3379 if (!chunk)
3380 return -ENOMEM;
3381
3382 index = 0;
3383 while (index < map->num_stripes) {
3384 device = map->stripes[index].dev;
3385 device->bytes_used += stripe_size;
3386 ret = btrfs_update_device(trans, device);
3387 BUG_ON(ret);
3388 index++;
3389 }
3390
Josef Bacik2bf64752011-09-26 17:12:22 -04003391 spin_lock(&extent_root->fs_info->free_chunk_lock);
3392 extent_root->fs_info->free_chunk_space -= (stripe_size *
3393 map->num_stripes);
3394 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3395
Yan Zheng2b820322008-11-17 21:11:30 -05003396 index = 0;
3397 stripe = &chunk->stripe;
3398 while (index < map->num_stripes) {
3399 device = map->stripes[index].dev;
3400 dev_offset = map->stripes[index].physical;
3401
3402 btrfs_set_stack_stripe_devid(stripe, device->devid);
3403 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3404 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3405 stripe++;
3406 index++;
3407 }
3408
3409 btrfs_set_stack_chunk_length(chunk, chunk_size);
3410 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3411 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3412 btrfs_set_stack_chunk_type(chunk, map->type);
3413 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3414 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3415 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3416 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3417 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3418
3419 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3420 key.type = BTRFS_CHUNK_ITEM_KEY;
3421 key.offset = chunk_offset;
3422
3423 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
3424 BUG_ON(ret);
3425
3426 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Li Zefan125ccb02011-12-08 15:07:24 +08003427 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003428 item_size);
3429 BUG_ON(ret);
3430 }
liubo1abe9b82011-03-24 11:18:59 +00003431
Yan Zheng2b820322008-11-17 21:11:30 -05003432 kfree(chunk);
3433 return 0;
3434}
3435
3436/*
3437 * Chunk allocation falls into two parts. The first part does works
3438 * that make the new allocated chunk useable, but not do any operation
3439 * that modifies the chunk tree. The second part does the works that
3440 * require modifying the chunk tree. This division is important for the
3441 * bootstrap process of adding storage to a seed btrfs.
3442 */
3443int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3444 struct btrfs_root *extent_root, u64 type)
3445{
3446 u64 chunk_offset;
3447 u64 chunk_size;
3448 u64 stripe_size;
3449 struct map_lookup *map;
3450 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3451 int ret;
3452
3453 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3454 &chunk_offset);
3455 if (ret)
3456 return ret;
3457
3458 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3459 &stripe_size, chunk_offset, type);
3460 if (ret)
3461 return ret;
3462
3463 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3464 chunk_size, stripe_size);
3465 BUG_ON(ret);
3466 return 0;
3467}
3468
Chris Masond3977122009-01-05 21:25:51 -05003469static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003470 struct btrfs_root *root,
3471 struct btrfs_device *device)
3472{
3473 u64 chunk_offset;
3474 u64 sys_chunk_offset;
3475 u64 chunk_size;
3476 u64 sys_chunk_size;
3477 u64 stripe_size;
3478 u64 sys_stripe_size;
3479 u64 alloc_profile;
3480 struct map_lookup *map;
3481 struct map_lookup *sys_map;
3482 struct btrfs_fs_info *fs_info = root->fs_info;
3483 struct btrfs_root *extent_root = fs_info->extent_root;
3484 int ret;
3485
3486 ret = find_next_chunk(fs_info->chunk_root,
3487 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003488 if (ret)
3489 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003490
3491 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003492 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003493 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3494
3495 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3496 &stripe_size, chunk_offset, alloc_profile);
3497 BUG_ON(ret);
3498
3499 sys_chunk_offset = chunk_offset + chunk_size;
3500
3501 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003502 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003503 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3504
3505 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3506 &sys_chunk_size, &sys_stripe_size,
3507 sys_chunk_offset, alloc_profile);
3508 BUG_ON(ret);
3509
3510 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
3511 BUG_ON(ret);
3512
3513 /*
3514 * Modifying chunk tree needs allocating new blocks from both
3515 * system block group and metadata block group. So we only can
3516 * do operations require modifying the chunk tree after both
3517 * block groups were created.
3518 */
3519 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3520 chunk_size, stripe_size);
3521 BUG_ON(ret);
3522
3523 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3524 sys_chunk_offset, sys_chunk_size,
3525 sys_stripe_size);
3526 BUG_ON(ret);
3527 return 0;
3528}
3529
3530int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3531{
3532 struct extent_map *em;
3533 struct map_lookup *map;
3534 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3535 int readonly = 0;
3536 int i;
3537
Chris Mason890871b2009-09-02 16:24:52 -04003538 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003539 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003540 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003541 if (!em)
3542 return 1;
3543
Josef Bacikf48b9072010-01-27 02:07:59 +00003544 if (btrfs_test_opt(root, DEGRADED)) {
3545 free_extent_map(em);
3546 return 0;
3547 }
3548
Yan Zheng2b820322008-11-17 21:11:30 -05003549 map = (struct map_lookup *)em->bdev;
3550 for (i = 0; i < map->num_stripes; i++) {
3551 if (!map->stripes[i].dev->writeable) {
3552 readonly = 1;
3553 break;
3554 }
3555 }
3556 free_extent_map(em);
3557 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003558}
3559
3560void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3561{
David Sterbaa8067e02011-04-21 00:34:43 +02003562 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003563}
3564
3565void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3566{
3567 struct extent_map *em;
3568
Chris Masond3977122009-01-05 21:25:51 -05003569 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003570 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003571 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3572 if (em)
3573 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003574 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003575 if (!em)
3576 break;
3577 kfree(em->bdev);
3578 /* once for us */
3579 free_extent_map(em);
3580 /* once for the tree */
3581 free_extent_map(em);
3582 }
3583}
3584
Chris Masonf1885912008-04-09 16:28:12 -04003585int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3586{
3587 struct extent_map *em;
3588 struct map_lookup *map;
3589 struct extent_map_tree *em_tree = &map_tree->map_tree;
3590 int ret;
3591
Chris Mason890871b2009-09-02 16:24:52 -04003592 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003593 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003594 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003595 BUG_ON(!em);
3596
3597 BUG_ON(em->start > logical || em->start + em->len < logical);
3598 map = (struct map_lookup *)em->bdev;
3599 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3600 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003601 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3602 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003603 else
3604 ret = 1;
3605 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003606 return ret;
3607}
3608
Chris Masondfe25022008-05-13 13:46:40 -04003609static int find_live_mirror(struct map_lookup *map, int first, int num,
3610 int optimal)
3611{
3612 int i;
3613 if (map->stripes[optimal].dev->bdev)
3614 return optimal;
3615 for (i = first; i < first + num; i++) {
3616 if (map->stripes[i].dev->bdev)
3617 return i;
3618 }
3619 /* we couldn't find one that doesn't fail. Just return something
3620 * and the io error handling code will clean up eventually
3621 */
3622 return optimal;
3623}
3624
Chris Masonf2d8d742008-04-21 10:03:05 -04003625static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3626 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003627 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003628 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003629{
3630 struct extent_map *em;
3631 struct map_lookup *map;
3632 struct extent_map_tree *em_tree = &map_tree->map_tree;
3633 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003634 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003635 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003636 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003637 u64 stripe_nr_orig;
3638 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04003639 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003640 int i;
Li Zefande11cc12011-12-01 12:55:47 +08003641 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04003642 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003643 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003644 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003645
Chris Mason890871b2009-09-02 16:24:52 -04003646 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003647 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003648 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003649
Chris Mason3b951512008-04-17 11:29:12 -04003650 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003651 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3652 (unsigned long long)logical,
3653 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003654 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003655 }
Chris Mason0b86a832008-03-24 15:01:56 -04003656
3657 BUG_ON(em->start > logical || em->start + em->len < logical);
3658 map = (struct map_lookup *)em->bdev;
3659 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003660
Chris Masonf1885912008-04-09 16:28:12 -04003661 if (mirror_num > map->num_stripes)
3662 mirror_num = 0;
3663
Chris Mason593060d2008-03-25 16:50:33 -04003664 stripe_nr = offset;
3665 /*
3666 * stripe_nr counts the total number of stripes we have to stride
3667 * to get to this block
3668 */
3669 do_div(stripe_nr, map->stripe_len);
3670
3671 stripe_offset = stripe_nr * map->stripe_len;
3672 BUG_ON(offset < stripe_offset);
3673
3674 /* stripe_offset is the offset of this block in its stripe*/
3675 stripe_offset = offset - stripe_offset;
3676
Li Dongyangfce3bb92011-03-24 10:24:26 +00003677 if (rw & REQ_DISCARD)
3678 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003679 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003680 /* we limit the length of each bio to what fits in a stripe */
3681 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003682 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003683 } else {
3684 *length = em->len - offset;
3685 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003686
Jan Schmidta1d3c472011-08-04 17:15:33 +02003687 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003688 goto out;
3689
Chris Masonf2d8d742008-04-21 10:03:05 -04003690 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003691 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003692 stripe_nr_orig = stripe_nr;
3693 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3694 (~(map->stripe_len - 1));
3695 do_div(stripe_nr_end, map->stripe_len);
3696 stripe_end_offset = stripe_nr_end * map->stripe_len -
3697 (offset + *length);
3698 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3699 if (rw & REQ_DISCARD)
3700 num_stripes = min_t(u64, map->num_stripes,
3701 stripe_nr_end - stripe_nr_orig);
3702 stripe_index = do_div(stripe_nr, map->num_stripes);
3703 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003704 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003705 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003706 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003707 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003708 else {
3709 stripe_index = find_live_mirror(map, 0,
3710 map->num_stripes,
3711 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003712 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003713 }
Chris Mason2fff7342008-04-29 14:12:09 -04003714
Chris Mason611f0e02008-04-03 16:29:03 -04003715 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003716 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003717 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003718 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003719 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003720 } else {
3721 mirror_num = 1;
3722 }
Chris Mason2fff7342008-04-29 14:12:09 -04003723
Chris Mason321aecc2008-04-16 10:49:51 -04003724 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3725 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003726
3727 stripe_index = do_div(stripe_nr, factor);
3728 stripe_index *= map->sub_stripes;
3729
Jens Axboe7eaceac2011-03-10 08:52:07 +01003730 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003731 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003732 else if (rw & REQ_DISCARD)
3733 num_stripes = min_t(u64, map->sub_stripes *
3734 (stripe_nr_end - stripe_nr_orig),
3735 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003736 else if (mirror_num)
3737 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003738 else {
3739 stripe_index = find_live_mirror(map, stripe_index,
3740 map->sub_stripes, stripe_index +
3741 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003742 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003743 }
Chris Mason8790d502008-04-03 16:29:03 -04003744 } else {
3745 /*
3746 * after this do_div call, stripe_nr is the number of stripes
3747 * on this device we have to walk to find the data, and
3748 * stripe_index is the number of our device in the stripe array
3749 */
3750 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003751 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003752 }
Chris Mason593060d2008-03-25 16:50:33 -04003753 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003754
Li Zefande11cc12011-12-01 12:55:47 +08003755 bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
3756 if (!bbio) {
3757 ret = -ENOMEM;
3758 goto out;
3759 }
3760 atomic_set(&bbio->error, 0);
3761
Li Dongyangfce3bb92011-03-24 10:24:26 +00003762 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08003763 int factor = 0;
3764 int sub_stripes = 0;
3765 u64 stripes_per_dev = 0;
3766 u32 remaining_stripes = 0;
3767
3768 if (map->type &
3769 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
3770 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3771 sub_stripes = 1;
3772 else
3773 sub_stripes = map->sub_stripes;
3774
3775 factor = map->num_stripes / sub_stripes;
3776 stripes_per_dev = div_u64_rem(stripe_nr_end -
3777 stripe_nr_orig,
3778 factor,
3779 &remaining_stripes);
3780 }
3781
Li Dongyangfce3bb92011-03-24 10:24:26 +00003782 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003783 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003784 map->stripes[stripe_index].physical +
3785 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003786 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003787
Li Zefanec9ef7a2011-12-01 14:06:42 +08003788 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3789 BTRFS_BLOCK_GROUP_RAID10)) {
3790 bbio->stripes[i].length = stripes_per_dev *
3791 map->stripe_len;
3792 if (i / sub_stripes < remaining_stripes)
3793 bbio->stripes[i].length +=
3794 map->stripe_len;
3795 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003796 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003797 stripe_offset;
Li Zefanec9ef7a2011-12-01 14:06:42 +08003798 if ((i / sub_stripes + 1) %
3799 sub_stripes == remaining_stripes)
3800 bbio->stripes[i].length -=
3801 stripe_end_offset;
3802 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003803 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003804 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003805 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003806
3807 stripe_index++;
3808 if (stripe_index == map->num_stripes) {
3809 /* This could only happen for RAID0/10 */
3810 stripe_index = 0;
3811 stripe_nr++;
3812 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003813 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003814 } else {
3815 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003816 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003817 map->stripes[stripe_index].physical +
3818 stripe_offset +
3819 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003820 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003821 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003822 stripe_index++;
3823 }
Chris Mason593060d2008-03-25 16:50:33 -04003824 }
Li Zefande11cc12011-12-01 12:55:47 +08003825
3826 if (rw & REQ_WRITE) {
3827 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3828 BTRFS_BLOCK_GROUP_RAID10 |
3829 BTRFS_BLOCK_GROUP_DUP)) {
3830 max_errors = 1;
3831 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003832 }
Li Zefande11cc12011-12-01 12:55:47 +08003833
3834 *bbio_ret = bbio;
3835 bbio->num_stripes = num_stripes;
3836 bbio->max_errors = max_errors;
3837 bbio->mirror_num = mirror_num;
Chris Masoncea9e442008-04-09 16:28:12 -04003838out:
Chris Mason0b86a832008-03-24 15:01:56 -04003839 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08003840 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003841}
3842
Chris Masonf2d8d742008-04-21 10:03:05 -04003843int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3844 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003845 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003846{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003847 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003848 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003849}
3850
Yan Zhenga512bbf2008-12-08 16:46:26 -05003851int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3852 u64 chunk_start, u64 physical, u64 devid,
3853 u64 **logical, int *naddrs, int *stripe_len)
3854{
3855 struct extent_map_tree *em_tree = &map_tree->map_tree;
3856 struct extent_map *em;
3857 struct map_lookup *map;
3858 u64 *buf;
3859 u64 bytenr;
3860 u64 length;
3861 u64 stripe_nr;
3862 int i, j, nr = 0;
3863
Chris Mason890871b2009-09-02 16:24:52 -04003864 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003865 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003866 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003867
3868 BUG_ON(!em || em->start != chunk_start);
3869 map = (struct map_lookup *)em->bdev;
3870
3871 length = em->len;
3872 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3873 do_div(length, map->num_stripes / map->sub_stripes);
3874 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3875 do_div(length, map->num_stripes);
3876
3877 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3878 BUG_ON(!buf);
3879
3880 for (i = 0; i < map->num_stripes; i++) {
3881 if (devid && map->stripes[i].dev->devid != devid)
3882 continue;
3883 if (map->stripes[i].physical > physical ||
3884 map->stripes[i].physical + length <= physical)
3885 continue;
3886
3887 stripe_nr = physical - map->stripes[i].physical;
3888 do_div(stripe_nr, map->stripe_len);
3889
3890 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3891 stripe_nr = stripe_nr * map->num_stripes + i;
3892 do_div(stripe_nr, map->sub_stripes);
3893 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3894 stripe_nr = stripe_nr * map->num_stripes + i;
3895 }
3896 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003897 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003898 for (j = 0; j < nr; j++) {
3899 if (buf[j] == bytenr)
3900 break;
3901 }
Chris Mason934d3752008-12-08 16:43:10 -05003902 if (j == nr) {
3903 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003904 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003905 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003906 }
3907
Yan Zhenga512bbf2008-12-08 16:46:26 -05003908 *logical = buf;
3909 *naddrs = nr;
3910 *stripe_len = map->stripe_len;
3911
3912 free_extent_map(em);
3913 return 0;
3914}
3915
Jan Schmidta1d3c472011-08-04 17:15:33 +02003916static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003917{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003918 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003919 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003920
Chris Mason8790d502008-04-03 16:29:03 -04003921 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003922 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003923
Jan Schmidta1d3c472011-08-04 17:15:33 +02003924 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003925 is_orig_bio = 1;
3926
Jan Schmidta1d3c472011-08-04 17:15:33 +02003927 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003928 if (!is_orig_bio) {
3929 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003930 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003931 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003932 bio->bi_private = bbio->private;
3933 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02003934 bio->bi_bdev = (struct block_device *)
3935 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04003936 /* only send an error to the higher layers if it is
3937 * beyond the tolerance of the multi-bio
3938 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02003939 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003940 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05003941 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04003942 /*
3943 * this bio is actually up to date, we didn't
3944 * go over the max number of errors
3945 */
3946 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003947 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003948 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003949 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04003950
3951 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003952 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003953 bio_put(bio);
3954 }
Chris Mason8790d502008-04-03 16:29:03 -04003955}
3956
Chris Mason8b712842008-06-11 16:50:36 -04003957struct async_sched {
3958 struct bio *bio;
3959 int rw;
3960 struct btrfs_fs_info *info;
3961 struct btrfs_work work;
3962};
3963
3964/*
3965 * see run_scheduled_bios for a description of why bios are collected for
3966 * async submit.
3967 *
3968 * This will add one bio to the pending list for a device and make sure
3969 * the work struct is scheduled.
3970 */
Chris Masond3977122009-01-05 21:25:51 -05003971static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003972 struct btrfs_device *device,
3973 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003974{
3975 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003976 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003977
3978 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003979 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003980 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01003981 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003982 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003983 return 0;
3984 }
3985
3986 /*
Chris Mason0986fe92008-08-15 15:34:15 -04003987 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003988 * higher layers. Otherwise, the async bio makes it appear we have
3989 * made progress against dirty pages when we've really just put it
3990 * on a queue for later
3991 */
Chris Mason0986fe92008-08-15 15:34:15 -04003992 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003993 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003994 bio->bi_next = NULL;
3995 bio->bi_rw |= rw;
3996
3997 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003998 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003999 pending_bios = &device->pending_sync_bios;
4000 else
4001 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004002
Chris Masonffbd5172009-04-20 15:50:09 -04004003 if (pending_bios->tail)
4004 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004005
Chris Masonffbd5172009-04-20 15:50:09 -04004006 pending_bios->tail = bio;
4007 if (!pending_bios->head)
4008 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004009 if (device->running_pending)
4010 should_queue = 0;
4011
4012 spin_unlock(&device->io_lock);
4013
4014 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004015 btrfs_queue_worker(&root->fs_info->submit_workers,
4016 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004017 return 0;
4018}
4019
Chris Masonf1885912008-04-09 16:28:12 -04004020int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004021 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004022{
4023 struct btrfs_mapping_tree *map_tree;
4024 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004025 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004026 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004027 u64 length = 0;
4028 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004029 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004030 int dev_nr = 0;
4031 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004032 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004033
Chris Masonf2d8d742008-04-21 10:03:05 -04004034 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004035 map_tree = &root->fs_info->mapping_tree;
4036 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004037
Jan Schmidta1d3c472011-08-04 17:15:33 +02004038 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004039 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04004040 BUG_ON(ret);
4041
Jan Schmidta1d3c472011-08-04 17:15:33 +02004042 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004043 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05004044 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
4045 "len %llu\n", (unsigned long long)logical,
4046 (unsigned long long)length,
4047 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004048 BUG();
4049 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004050
4051 bbio->orig_bio = first_bio;
4052 bbio->private = first_bio->bi_private;
4053 bbio->end_io = first_bio->bi_end_io;
4054 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004055
Chris Masond3977122009-01-05 21:25:51 -05004056 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004057 if (dev_nr < total_devs - 1) {
4058 bio = bio_clone(first_bio, GFP_NOFS);
4059 BUG_ON(!bio);
4060 } else {
4061 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004062 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004063 bio->bi_private = bbio;
4064 bio->bi_end_io = btrfs_end_bio;
4065 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
4066 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04004067 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004068 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
4069 "(%s id %llu), size=%u\n", rw,
4070 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4071 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04004072 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04004073 if (async_submit)
4074 schedule_bio(root, dev, rw, bio);
4075 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01004076 btrfsic_submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04004077 } else {
4078 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
4079 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04004080 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04004081 }
Chris Mason8790d502008-04-03 16:29:03 -04004082 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004083 }
Chris Mason0b86a832008-03-24 15:01:56 -04004084 return 0;
4085}
4086
Chris Masona4437552008-04-18 10:29:38 -04004087struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004088 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004089{
Yan Zheng2b820322008-11-17 21:11:30 -05004090 struct btrfs_device *device;
4091 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004092
Yan Zheng2b820322008-11-17 21:11:30 -05004093 cur_devices = root->fs_info->fs_devices;
4094 while (cur_devices) {
4095 if (!fsid ||
4096 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4097 device = __find_device(&cur_devices->devices,
4098 devid, uuid);
4099 if (device)
4100 return device;
4101 }
4102 cur_devices = cur_devices->seed;
4103 }
4104 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004105}
4106
Chris Masondfe25022008-05-13 13:46:40 -04004107static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4108 u64 devid, u8 *dev_uuid)
4109{
4110 struct btrfs_device *device;
4111 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4112
4113 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004114 if (!device)
4115 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004116 list_add(&device->dev_list,
4117 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004118 device->dev_root = root->fs_info->dev_root;
4119 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004120 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004121 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004122 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004123 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004124 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004125 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004126 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004127 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4128 return device;
4129}
4130
Chris Mason0b86a832008-03-24 15:01:56 -04004131static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4132 struct extent_buffer *leaf,
4133 struct btrfs_chunk *chunk)
4134{
4135 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4136 struct map_lookup *map;
4137 struct extent_map *em;
4138 u64 logical;
4139 u64 length;
4140 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004141 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004142 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004143 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004144 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004145
Chris Masone17cade2008-04-15 15:41:47 -04004146 logical = key->offset;
4147 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004148
Chris Mason890871b2009-09-02 16:24:52 -04004149 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004150 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004151 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004152
4153 /* already mapped? */
4154 if (em && em->start <= logical && em->start + em->len > logical) {
4155 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004156 return 0;
4157 } else if (em) {
4158 free_extent_map(em);
4159 }
Chris Mason0b86a832008-03-24 15:01:56 -04004160
David Sterba172ddd62011-04-21 00:48:27 +02004161 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004162 if (!em)
4163 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004164 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4165 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004166 if (!map) {
4167 free_extent_map(em);
4168 return -ENOMEM;
4169 }
4170
4171 em->bdev = (struct block_device *)map;
4172 em->start = logical;
4173 em->len = length;
4174 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004175 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004176
Chris Mason593060d2008-03-25 16:50:33 -04004177 map->num_stripes = num_stripes;
4178 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4179 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4180 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4181 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4182 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004183 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004184 for (i = 0; i < num_stripes; i++) {
4185 map->stripes[i].physical =
4186 btrfs_stripe_offset_nr(leaf, chunk, i);
4187 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004188 read_extent_buffer(leaf, uuid, (unsigned long)
4189 btrfs_stripe_dev_uuid_nr(chunk, i),
4190 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004191 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4192 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004193 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004194 kfree(map);
4195 free_extent_map(em);
4196 return -EIO;
4197 }
Chris Masondfe25022008-05-13 13:46:40 -04004198 if (!map->stripes[i].dev) {
4199 map->stripes[i].dev =
4200 add_missing_dev(root, devid, uuid);
4201 if (!map->stripes[i].dev) {
4202 kfree(map);
4203 free_extent_map(em);
4204 return -EIO;
4205 }
4206 }
4207 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004208 }
4209
Chris Mason890871b2009-09-02 16:24:52 -04004210 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004211 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004212 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04004213 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04004214 free_extent_map(em);
4215
4216 return 0;
4217}
4218
4219static int fill_device_from_item(struct extent_buffer *leaf,
4220 struct btrfs_dev_item *dev_item,
4221 struct btrfs_device *device)
4222{
4223 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004224
4225 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004226 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4227 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004228 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4229 device->type = btrfs_device_type(leaf, dev_item);
4230 device->io_align = btrfs_device_io_align(leaf, dev_item);
4231 device->io_width = btrfs_device_io_width(leaf, dev_item);
4232 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004233
4234 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004235 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004236
Chris Mason0b86a832008-03-24 15:01:56 -04004237 return 0;
4238}
4239
Yan Zheng2b820322008-11-17 21:11:30 -05004240static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4241{
4242 struct btrfs_fs_devices *fs_devices;
4243 int ret;
4244
Li Zefanb367e472011-12-07 11:38:24 +08004245 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05004246
4247 fs_devices = root->fs_info->fs_devices->seed;
4248 while (fs_devices) {
4249 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4250 ret = 0;
4251 goto out;
4252 }
4253 fs_devices = fs_devices->seed;
4254 }
4255
4256 fs_devices = find_fsid(fsid);
4257 if (!fs_devices) {
4258 ret = -ENOENT;
4259 goto out;
4260 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004261
4262 fs_devices = clone_fs_devices(fs_devices);
4263 if (IS_ERR(fs_devices)) {
4264 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004265 goto out;
4266 }
4267
Christoph Hellwig97288f22008-12-02 06:36:09 -05004268 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004269 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05004270 if (ret)
4271 goto out;
4272
4273 if (!fs_devices->seeding) {
4274 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004275 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004276 ret = -EINVAL;
4277 goto out;
4278 }
4279
4280 fs_devices->seed = root->fs_info->fs_devices->seed;
4281 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004282out:
Yan Zheng2b820322008-11-17 21:11:30 -05004283 return ret;
4284}
4285
Chris Mason0d81ba52008-03-24 15:02:07 -04004286static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004287 struct extent_buffer *leaf,
4288 struct btrfs_dev_item *dev_item)
4289{
4290 struct btrfs_device *device;
4291 u64 devid;
4292 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004293 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004294 u8 dev_uuid[BTRFS_UUID_SIZE];
4295
Chris Mason0b86a832008-03-24 15:01:56 -04004296 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004297 read_extent_buffer(leaf, dev_uuid,
4298 (unsigned long)btrfs_device_uuid(dev_item),
4299 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004300 read_extent_buffer(leaf, fs_uuid,
4301 (unsigned long)btrfs_device_fsid(dev_item),
4302 BTRFS_UUID_SIZE);
4303
4304 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4305 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004306 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004307 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004308 }
4309
4310 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4311 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004312 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004313 return -EIO;
4314
4315 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004316 printk(KERN_WARNING "warning devid %llu missing\n",
4317 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004318 device = add_missing_dev(root, devid, dev_uuid);
4319 if (!device)
4320 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004321 } else if (!device->missing) {
4322 /*
4323 * this happens when a device that was properly setup
4324 * in the device info lists suddenly goes bad.
4325 * device->bdev is NULL, and so we have to set
4326 * device->missing to one here
4327 */
4328 root->fs_info->fs_devices->missing_devices++;
4329 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004330 }
4331 }
4332
4333 if (device->fs_devices != root->fs_info->fs_devices) {
4334 BUG_ON(device->writeable);
4335 if (device->generation !=
4336 btrfs_device_generation(leaf, dev_item))
4337 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004338 }
Chris Mason0b86a832008-03-24 15:01:56 -04004339
4340 fill_device_from_item(leaf, dev_item, device);
4341 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004342 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004343 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004344 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004345 spin_lock(&root->fs_info->free_chunk_lock);
4346 root->fs_info->free_chunk_space += device->total_bytes -
4347 device->bytes_used;
4348 spin_unlock(&root->fs_info->free_chunk_lock);
4349 }
Chris Mason0b86a832008-03-24 15:01:56 -04004350 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004351 return ret;
4352}
4353
Yan Zhenge4404d62008-12-12 10:03:26 -05004354int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004355{
David Sterba6c417612011-04-13 15:41:04 +02004356 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004357 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004358 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004359 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004360 u8 *ptr;
4361 unsigned long sb_ptr;
4362 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004363 u32 num_stripes;
4364 u32 array_size;
4365 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004366 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004367 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004368
Yan Zhenge4404d62008-12-12 10:03:26 -05004369 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004370 BTRFS_SUPER_INFO_SIZE);
4371 if (!sb)
4372 return -ENOMEM;
4373 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004374 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02004375 /*
4376 * The sb extent buffer is artifical and just used to read the system array.
4377 * btrfs_set_buffer_uptodate() call does not properly mark all it's
4378 * pages up-to-date when the page is larger: extent does not cover the
4379 * whole page and consequently check_page_uptodate does not find all
4380 * the page's extents up-to-date (the hole beyond sb),
4381 * write_extent_buffer then triggers a WARN_ON.
4382 *
4383 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
4384 * but sb spans only this function. Add an explicit SetPageUptodate call
4385 * to silence the warning eg. on PowerPC 64.
4386 */
4387 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
4388 SetPageUptodate(sb->first_page);
Chris Mason4008c042009-02-12 14:09:45 -05004389
Chris Masona061fc82008-05-07 11:43:44 -04004390 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004391 array_size = btrfs_super_sys_array_size(super_copy);
4392
Chris Mason0b86a832008-03-24 15:01:56 -04004393 ptr = super_copy->sys_chunk_array;
4394 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4395 cur = 0;
4396
4397 while (cur < array_size) {
4398 disk_key = (struct btrfs_disk_key *)ptr;
4399 btrfs_disk_key_to_cpu(&key, disk_key);
4400
Chris Masona061fc82008-05-07 11:43:44 -04004401 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004402 sb_ptr += len;
4403 cur += len;
4404
Chris Mason0d81ba52008-03-24 15:02:07 -04004405 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004406 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004407 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004408 if (ret)
4409 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004410 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4411 len = btrfs_chunk_item_size(num_stripes);
4412 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004413 ret = -EIO;
4414 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004415 }
4416 ptr += len;
4417 sb_ptr += len;
4418 cur += len;
4419 }
Chris Masona061fc82008-05-07 11:43:44 -04004420 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004421 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004422}
4423
4424int btrfs_read_chunk_tree(struct btrfs_root *root)
4425{
4426 struct btrfs_path *path;
4427 struct extent_buffer *leaf;
4428 struct btrfs_key key;
4429 struct btrfs_key found_key;
4430 int ret;
4431 int slot;
4432
4433 root = root->fs_info->chunk_root;
4434
4435 path = btrfs_alloc_path();
4436 if (!path)
4437 return -ENOMEM;
4438
Li Zefanb367e472011-12-07 11:38:24 +08004439 mutex_lock(&uuid_mutex);
4440 lock_chunks(root);
4441
Chris Mason0b86a832008-03-24 15:01:56 -04004442 /* first we search for all of the device items, and then we
4443 * read in all of the chunk items. This way we can create chunk
4444 * mappings that reference all of the devices that are afound
4445 */
4446 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4447 key.offset = 0;
4448 key.type = 0;
4449again:
4450 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004451 if (ret < 0)
4452 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004453 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004454 leaf = path->nodes[0];
4455 slot = path->slots[0];
4456 if (slot >= btrfs_header_nritems(leaf)) {
4457 ret = btrfs_next_leaf(root, path);
4458 if (ret == 0)
4459 continue;
4460 if (ret < 0)
4461 goto error;
4462 break;
4463 }
4464 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4465 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4466 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4467 break;
4468 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4469 struct btrfs_dev_item *dev_item;
4470 dev_item = btrfs_item_ptr(leaf, slot,
4471 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004472 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004473 if (ret)
4474 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004475 }
4476 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4477 struct btrfs_chunk *chunk;
4478 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4479 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004480 if (ret)
4481 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004482 }
4483 path->slots[0]++;
4484 }
4485 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4486 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004487 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004488 goto again;
4489 }
Chris Mason0b86a832008-03-24 15:01:56 -04004490 ret = 0;
4491error:
Li Zefanb367e472011-12-07 11:38:24 +08004492 unlock_chunks(root);
4493 mutex_unlock(&uuid_mutex);
4494
Yan Zheng2b820322008-11-17 21:11:30 -05004495 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004496 return ret;
4497}