blob: 17e565388de0fa0e2fdfbe40942efa8e93d4357e [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"
Chris Mason0b86a832008-03-24 15:01:56 -040036
Yan Zheng2b820322008-11-17 21:11:30 -050037static int init_first_rw_device(struct btrfs_trans_handle *trans,
38 struct btrfs_root *root,
39 struct btrfs_device *device);
40static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
41
Chris Mason8a4b83c2008-03-24 15:02:07 -040042static DEFINE_MUTEX(uuid_mutex);
43static LIST_HEAD(fs_uuids);
44
Chris Mason7d9eb122008-07-08 14:19:17 -040045static void lock_chunks(struct btrfs_root *root)
46{
Chris Mason7d9eb122008-07-08 14:19:17 -040047 mutex_lock(&root->fs_info->chunk_mutex);
48}
49
50static void unlock_chunks(struct btrfs_root *root)
51{
Chris Mason7d9eb122008-07-08 14:19:17 -040052 mutex_unlock(&root->fs_info->chunk_mutex);
53}
54
Yan Zhenge4404d62008-12-12 10:03:26 -050055static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
56{
57 struct btrfs_device *device;
58 WARN_ON(fs_devices->opened);
59 while (!list_empty(&fs_devices->devices)) {
60 device = list_entry(fs_devices->devices.next,
61 struct btrfs_device, dev_list);
62 list_del(&device->dev_list);
63 kfree(device->name);
64 kfree(device);
65 }
66 kfree(fs_devices);
67}
68
Chris Mason8a4b83c2008-03-24 15:02:07 -040069int btrfs_cleanup_fs_uuids(void)
70{
71 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040072
Yan Zheng2b820322008-11-17 21:11:30 -050073 while (!list_empty(&fs_uuids)) {
74 fs_devices = list_entry(fs_uuids.next,
75 struct btrfs_fs_devices, list);
76 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050077 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040078 }
79 return 0;
80}
81
Chris Masona1b32a52008-09-05 16:09:51 -040082static noinline struct btrfs_device *__find_device(struct list_head *head,
83 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040084{
85 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040086
Qinghuang Fengc6e30872009-01-21 10:59:08 -050087 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040088 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040089 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040090 return dev;
Chris Masona4437552008-04-18 10:29:38 -040091 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040092 }
93 return NULL;
94}
95
Chris Masona1b32a52008-09-05 16:09:51 -040096static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040097{
Chris Mason8a4b83c2008-03-24 15:02:07 -040098 struct btrfs_fs_devices *fs_devices;
99
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500100 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400101 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
102 return fs_devices;
103 }
104 return NULL;
105}
106
Chris Masonffbd5172009-04-20 15:50:09 -0400107static void requeue_list(struct btrfs_pending_bios *pending_bios,
108 struct bio *head, struct bio *tail)
109{
110
111 struct bio *old_head;
112
113 old_head = pending_bios->head;
114 pending_bios->head = head;
115 if (pending_bios->tail)
116 tail->bi_next = old_head;
117 else
118 pending_bios->tail = tail;
119}
120
Chris Mason8b712842008-06-11 16:50:36 -0400121/*
122 * we try to collect pending bios for a device so we don't get a large
123 * number of procs sending bios down to the same device. This greatly
124 * improves the schedulers ability to collect and merge the bios.
125 *
126 * But, it also turns into a long list of bios to process and that is sure
127 * to eventually make the worker thread block. The solution here is to
128 * make some progress and then put this work struct back at the end of
129 * the list if the block device is congested. This way, multiple devices
130 * can make progress from a single worker thread.
131 */
Chris Masond3977122009-01-05 21:25:51 -0500132static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400133{
134 struct bio *pending;
135 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400136 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400137 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400138 struct bio *tail;
139 struct bio *cur;
140 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400141 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400142 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400143 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400144 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400145 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000146 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400147 struct blk_plug plug;
148
149 /*
150 * this function runs all the bios we've collected for
151 * a particular device. We don't want to wander off to
152 * another device without first sending all of these down.
153 * So, setup a plug here and finish it off before we return
154 */
155 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400156
Chris Masonbedf7622009-04-03 10:32:58 -0400157 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400158 fs_info = device->dev_root->fs_info;
159 limit = btrfs_async_submit_limit(fs_info);
160 limit = limit * 2 / 3;
161
Chris Mason8b712842008-06-11 16:50:36 -0400162loop:
163 spin_lock(&device->io_lock);
164
Chris Masona6837052009-02-04 09:19:41 -0500165loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400166 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400167
Chris Mason8b712842008-06-11 16:50:36 -0400168 /* take all the bios off the list at once and process them
169 * later on (without the lock held). But, remember the
170 * tail and other pointers so the bios can be properly reinserted
171 * into the list if we hit congestion
172 */
Chris Masond84275c2009-06-09 15:39:08 -0400173 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400174 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400175 force_reg = 1;
176 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400177 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400178 force_reg = 0;
179 }
Chris Masonffbd5172009-04-20 15:50:09 -0400180
181 pending = pending_bios->head;
182 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400183 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400184
185 /*
186 * if pending was null this time around, no bios need processing
187 * at all and we can stop. Otherwise it'll loop back up again
188 * and do an additional check so no bios are missed.
189 *
190 * device->running_pending is used to synchronize with the
191 * schedule_bio code.
192 */
Chris Masonffbd5172009-04-20 15:50:09 -0400193 if (device->pending_sync_bios.head == NULL &&
194 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400195 again = 0;
196 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400197 } else {
198 again = 1;
199 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400200 }
Chris Masonffbd5172009-04-20 15:50:09 -0400201
202 pending_bios->head = NULL;
203 pending_bios->tail = NULL;
204
Chris Mason8b712842008-06-11 16:50:36 -0400205 spin_unlock(&device->io_lock);
206
Chris Masond3977122009-01-05 21:25:51 -0500207 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400208
209 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400210 /* we want to work on both lists, but do more bios on the
211 * sync list than the regular list
212 */
213 if ((num_run > 32 &&
214 pending_bios != &device->pending_sync_bios &&
215 device->pending_sync_bios.head) ||
216 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
217 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400218 spin_lock(&device->io_lock);
219 requeue_list(pending_bios, pending, tail);
220 goto loop_lock;
221 }
222
Chris Mason8b712842008-06-11 16:50:36 -0400223 cur = pending;
224 pending = pending->bi_next;
225 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400226 atomic_dec(&fs_info->nr_async_bios);
227
228 if (atomic_read(&fs_info->nr_async_bios) < limit &&
229 waitqueue_active(&fs_info->async_submit_wait))
230 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400231
232 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400233
Chris Mason2ab1ba62011-08-04 14:28:36 -0400234 /*
235 * if we're doing the sync list, record that our
236 * plug has some sync requests on it
237 *
238 * If we're doing the regular list and there are
239 * sync requests sitting around, unplug before
240 * we add more
241 */
242 if (pending_bios == &device->pending_sync_bios) {
243 sync_pending = 1;
244 } else if (sync_pending) {
245 blk_finish_plug(&plug);
246 blk_start_plug(&plug);
247 sync_pending = 0;
248 }
249
Chris Mason5ff7ba32010-03-15 10:21:30 -0400250 submit_bio(cur->bi_rw, cur);
251 num_run++;
252 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100253 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400254 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400255
256 /*
257 * we made progress, there is more work to do and the bdi
258 * is now congested. Back off and let other work structs
259 * run instead
260 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400261 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500262 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400263 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400264
Chris Masonb765ead2009-04-03 10:27:10 -0400265 ioc = current->io_context;
266
267 /*
268 * the main goal here is that we don't want to
269 * block if we're going to be able to submit
270 * more requests without blocking.
271 *
272 * This code does two great things, it pokes into
273 * the elevator code from a filesystem _and_
274 * it makes assumptions about how batching works.
275 */
276 if (ioc && ioc->nr_batch_requests > 0 &&
277 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
278 (last_waited == 0 ||
279 ioc->last_waited == last_waited)) {
280 /*
281 * we want to go through our batch of
282 * requests and stop. So, we copy out
283 * the ioc->last_waited time and test
284 * against it before looping
285 */
286 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100287 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400288 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400289 continue;
290 }
Chris Mason8b712842008-06-11 16:50:36 -0400291 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400292 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500293 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400294
295 spin_unlock(&device->io_lock);
296 btrfs_requeue_work(&device->work);
297 goto done;
298 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500299 /* unplug every 64 requests just for good measure */
300 if (batch_run % 64 == 0) {
301 blk_finish_plug(&plug);
302 blk_start_plug(&plug);
303 sync_pending = 0;
304 }
Chris Mason8b712842008-06-11 16:50:36 -0400305 }
Chris Masonffbd5172009-04-20 15:50:09 -0400306
Chris Mason51684082010-03-10 15:33:32 -0500307 cond_resched();
308 if (again)
309 goto loop;
310
311 spin_lock(&device->io_lock);
312 if (device->pending_bios.head || device->pending_sync_bios.head)
313 goto loop_lock;
314 spin_unlock(&device->io_lock);
315
Chris Mason8b712842008-06-11 16:50:36 -0400316done:
Chris Mason211588a2011-04-19 20:12:40 -0400317 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400318 return 0;
319}
320
Christoph Hellwigb2950862008-12-02 09:54:17 -0500321static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400322{
323 struct btrfs_device *device;
324
325 device = container_of(work, struct btrfs_device, work);
326 run_scheduled_bios(device);
327}
328
Chris Masona1b32a52008-09-05 16:09:51 -0400329static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400330 struct btrfs_super_block *disk_super,
331 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
332{
333 struct btrfs_device *device;
334 struct btrfs_fs_devices *fs_devices;
335 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000336 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400337
338 fs_devices = find_fsid(disk_super->fsid);
339 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400340 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400341 if (!fs_devices)
342 return -ENOMEM;
343 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400344 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400345 list_add(&fs_devices->list, &fs_uuids);
346 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
347 fs_devices->latest_devid = devid;
348 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400349 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400350 device = NULL;
351 } else {
Chris Masona4437552008-04-18 10:29:38 -0400352 device = __find_device(&fs_devices->devices, devid,
353 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400354 }
355 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500356 if (fs_devices->opened)
357 return -EBUSY;
358
Chris Mason8a4b83c2008-03-24 15:02:07 -0400359 device = kzalloc(sizeof(*device), GFP_NOFS);
360 if (!device) {
361 /* we can safely leave the fs_devices entry around */
362 return -ENOMEM;
363 }
364 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400365 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400366 memcpy(device->uuid, disk_super->dev_item.uuid,
367 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400368 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400369 device->name = kstrdup(path, GFP_NOFS);
370 if (!device->name) {
371 kfree(device);
372 return -ENOMEM;
373 }
Yan Zheng2b820322008-11-17 21:11:30 -0500374 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400375
Arne Jansen90519d62011-05-23 14:30:00 +0200376 /* init readahead state */
377 spin_lock_init(&device->reada_lock);
378 device->reada_curr_zone = NULL;
379 atomic_set(&device->reada_in_flight, 0);
380 device->reada_next = 0;
381 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
382 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
383
Chris Masone5e9a522009-06-10 15:17:02 -0400384 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000385 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400386 mutex_unlock(&fs_devices->device_list_mutex);
387
Yan Zheng2b820322008-11-17 21:11:30 -0500388 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400389 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500390 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000391 name = kstrdup(path, GFP_NOFS);
392 if (!name)
393 return -ENOMEM;
394 kfree(device->name);
395 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500396 if (device->missing) {
397 fs_devices->missing_devices--;
398 device->missing = 0;
399 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400400 }
401
402 if (found_transid > fs_devices->latest_trans) {
403 fs_devices->latest_devid = devid;
404 fs_devices->latest_trans = found_transid;
405 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400406 *fs_devices_ret = fs_devices;
407 return 0;
408}
409
Yan Zhenge4404d62008-12-12 10:03:26 -0500410static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
411{
412 struct btrfs_fs_devices *fs_devices;
413 struct btrfs_device *device;
414 struct btrfs_device *orig_dev;
415
416 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
417 if (!fs_devices)
418 return ERR_PTR(-ENOMEM);
419
420 INIT_LIST_HEAD(&fs_devices->devices);
421 INIT_LIST_HEAD(&fs_devices->alloc_list);
422 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400423 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500424 fs_devices->latest_devid = orig->latest_devid;
425 fs_devices->latest_trans = orig->latest_trans;
426 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
427
Xiao Guangrong46224702011-04-20 10:08:47 +0000428 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500429 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
430 device = kzalloc(sizeof(*device), GFP_NOFS);
431 if (!device)
432 goto error;
433
434 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400435 if (!device->name) {
436 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500437 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400438 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500439
440 device->devid = orig_dev->devid;
441 device->work.func = pending_bios_fn;
442 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500443 spin_lock_init(&device->io_lock);
444 INIT_LIST_HEAD(&device->dev_list);
445 INIT_LIST_HEAD(&device->dev_alloc_list);
446
447 list_add(&device->dev_list, &fs_devices->devices);
448 device->fs_devices = fs_devices;
449 fs_devices->num_devices++;
450 }
451 return fs_devices;
452error:
453 free_fs_devices(fs_devices);
454 return ERR_PTR(-ENOMEM);
455}
456
Chris Masondfe25022008-05-13 13:46:40 -0400457int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
458{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500459 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400460
461 mutex_lock(&uuid_mutex);
462again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000463 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500464 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500465 if (device->in_fs_metadata)
466 continue;
467
468 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100469 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500470 device->bdev = NULL;
471 fs_devices->open_devices--;
472 }
473 if (device->writeable) {
474 list_del_init(&device->dev_alloc_list);
475 device->writeable = 0;
476 fs_devices->rw_devices--;
477 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500478 list_del_init(&device->dev_list);
479 fs_devices->num_devices--;
480 kfree(device->name);
481 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400482 }
Yan Zheng2b820322008-11-17 21:11:30 -0500483
484 if (fs_devices->seed) {
485 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500486 goto again;
487 }
488
Chris Masondfe25022008-05-13 13:46:40 -0400489 mutex_unlock(&uuid_mutex);
490 return 0;
491}
Chris Masona0af4692008-05-13 16:03:06 -0400492
Xiao Guangrong1f781602011-04-20 10:09:16 +0000493static void __free_device(struct work_struct *work)
494{
495 struct btrfs_device *device;
496
497 device = container_of(work, struct btrfs_device, rcu_work);
498
499 if (device->bdev)
500 blkdev_put(device->bdev, device->mode);
501
502 kfree(device->name);
503 kfree(device);
504}
505
506static void free_device(struct rcu_head *head)
507{
508 struct btrfs_device *device;
509
510 device = container_of(head, struct btrfs_device, rcu);
511
512 INIT_WORK(&device->rcu_work, __free_device);
513 schedule_work(&device->rcu_work);
514}
515
Yan Zheng2b820322008-11-17 21:11:30 -0500516static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400517{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400518 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500519
Yan Zheng2b820322008-11-17 21:11:30 -0500520 if (--fs_devices->opened > 0)
521 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400522
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000523 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500524 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000525 struct btrfs_device *new_device;
526
527 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400528 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000529
Yan Zheng2b820322008-11-17 21:11:30 -0500530 if (device->writeable) {
531 list_del_init(&device->dev_alloc_list);
532 fs_devices->rw_devices--;
533 }
534
Josef Bacikd5e20032011-08-04 14:52:27 +0000535 if (device->can_discard)
536 fs_devices->num_can_discard--;
537
Xiao Guangrong1f781602011-04-20 10:09:16 +0000538 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
539 BUG_ON(!new_device);
540 memcpy(new_device, device, sizeof(*new_device));
541 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000542 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000543 new_device->bdev = NULL;
544 new_device->writeable = 0;
545 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000546 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000547 list_replace_rcu(&device->dev_list, &new_device->dev_list);
548
549 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400550 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000551 mutex_unlock(&fs_devices->device_list_mutex);
552
Yan Zhenge4404d62008-12-12 10:03:26 -0500553 WARN_ON(fs_devices->open_devices);
554 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500555 fs_devices->opened = 0;
556 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500557
Chris Mason8a4b83c2008-03-24 15:02:07 -0400558 return 0;
559}
560
Yan Zheng2b820322008-11-17 21:11:30 -0500561int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
562{
Yan Zhenge4404d62008-12-12 10:03:26 -0500563 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500564 int ret;
565
566 mutex_lock(&uuid_mutex);
567 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500568 if (!fs_devices->opened) {
569 seed_devices = fs_devices->seed;
570 fs_devices->seed = NULL;
571 }
Yan Zheng2b820322008-11-17 21:11:30 -0500572 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500573
574 while (seed_devices) {
575 fs_devices = seed_devices;
576 seed_devices = fs_devices->seed;
577 __btrfs_close_devices(fs_devices);
578 free_fs_devices(fs_devices);
579 }
Yan Zheng2b820322008-11-17 21:11:30 -0500580 return ret;
581}
582
Yan Zhenge4404d62008-12-12 10:03:26 -0500583static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
584 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400585{
Josef Bacikd5e20032011-08-04 14:52:27 +0000586 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400587 struct block_device *bdev;
588 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400589 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400590 struct block_device *latest_bdev = NULL;
591 struct buffer_head *bh;
592 struct btrfs_super_block *disk_super;
593 u64 latest_devid = 0;
594 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400595 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500596 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400597 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400598
Tejun Heod4d77622010-11-13 11:55:18 +0100599 flags |= FMODE_EXCL;
600
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500601 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400602 if (device->bdev)
603 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400604 if (!device->name)
605 continue;
606
Tejun Heod4d77622010-11-13 11:55:18 +0100607 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400608 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500609 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400610 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400611 }
Chris Masona061fc82008-05-07 11:43:44 -0400612 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400613
Yan Zhenga512bbf2008-12-08 16:46:26 -0500614 bh = btrfs_read_dev_super(bdev);
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300615 if (!bh)
Chris Masona0af4692008-05-13 16:03:06 -0400616 goto error_close;
617
618 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000619 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400620 if (devid != device->devid)
621 goto error_brelse;
622
Yan Zheng2b820322008-11-17 21:11:30 -0500623 if (memcmp(device->uuid, disk_super->dev_item.uuid,
624 BTRFS_UUID_SIZE))
625 goto error_brelse;
626
627 device->generation = btrfs_super_generation(disk_super);
628 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400629 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500630 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400631 latest_bdev = bdev;
632 }
633
Yan Zheng2b820322008-11-17 21:11:30 -0500634 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
635 device->writeable = 0;
636 } else {
637 device->writeable = !bdev_read_only(bdev);
638 seeding = 0;
639 }
640
Josef Bacikd5e20032011-08-04 14:52:27 +0000641 q = bdev_get_queue(bdev);
642 if (blk_queue_discard(q)) {
643 device->can_discard = 1;
644 fs_devices->num_can_discard++;
645 }
646
Chris Mason8a4b83c2008-03-24 15:02:07 -0400647 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400648 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500649 device->mode = flags;
650
Chris Masonc2898112009-06-10 09:51:32 -0400651 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
652 fs_devices->rotating = 1;
653
Chris Masona0af4692008-05-13 16:03:06 -0400654 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500655 if (device->writeable) {
656 fs_devices->rw_devices++;
657 list_add(&device->dev_alloc_list,
658 &fs_devices->alloc_list);
659 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000660 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400661 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400662
Chris Masona0af4692008-05-13 16:03:06 -0400663error_brelse:
664 brelse(bh);
665error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100666 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400667error:
668 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400669 }
Chris Masona0af4692008-05-13 16:03:06 -0400670 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300671 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400672 goto out;
673 }
Yan Zheng2b820322008-11-17 21:11:30 -0500674 fs_devices->seeding = seeding;
675 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400676 fs_devices->latest_bdev = latest_bdev;
677 fs_devices->latest_devid = latest_devid;
678 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500679 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400680out:
Yan Zheng2b820322008-11-17 21:11:30 -0500681 return ret;
682}
683
684int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500685 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500686{
687 int ret;
688
689 mutex_lock(&uuid_mutex);
690 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500691 fs_devices->opened++;
692 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500693 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500694 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500695 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400697 return ret;
698}
699
Christoph Hellwig97288f22008-12-02 06:36:09 -0500700int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400701 struct btrfs_fs_devices **fs_devices_ret)
702{
703 struct btrfs_super_block *disk_super;
704 struct block_device *bdev;
705 struct buffer_head *bh;
706 int ret;
707 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400708 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400709
710 mutex_lock(&uuid_mutex);
711
Tejun Heod4d77622010-11-13 11:55:18 +0100712 flags |= FMODE_EXCL;
713 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400714
715 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400716 ret = PTR_ERR(bdev);
717 goto error;
718 }
719
720 ret = set_blocksize(bdev, 4096);
721 if (ret)
722 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500723 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400724 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000725 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400726 goto error_close;
727 }
728 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000729 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400730 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400731 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500732 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200733 else
734 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500735 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500736 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400737 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
738
Chris Mason8a4b83c2008-03-24 15:02:07 -0400739 brelse(bh);
740error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100741 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400742error:
743 mutex_unlock(&uuid_mutex);
744 return ret;
745}
Chris Mason0b86a832008-03-24 15:01:56 -0400746
Miao Xie6d07bce2011-01-05 10:07:31 +0000747/* helper to account the used device space in the range */
748int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
749 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400750{
751 struct btrfs_key key;
752 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000753 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500754 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000755 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400756 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000757 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400758 struct extent_buffer *l;
759
Miao Xie6d07bce2011-01-05 10:07:31 +0000760 *length = 0;
761
762 if (start >= device->total_bytes)
763 return 0;
764
Yan Zheng2b820322008-11-17 21:11:30 -0500765 path = btrfs_alloc_path();
766 if (!path)
767 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400768 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400769
Chris Mason0b86a832008-03-24 15:01:56 -0400770 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000771 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400772 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000773
774 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400775 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000776 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400777 if (ret > 0) {
778 ret = btrfs_previous_item(root, path, key.objectid, key.type);
779 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000780 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400781 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000782
Chris Mason0b86a832008-03-24 15:01:56 -0400783 while (1) {
784 l = path->nodes[0];
785 slot = path->slots[0];
786 if (slot >= btrfs_header_nritems(l)) {
787 ret = btrfs_next_leaf(root, path);
788 if (ret == 0)
789 continue;
790 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000791 goto out;
792
793 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400794 }
795 btrfs_item_key_to_cpu(l, &key, slot);
796
797 if (key.objectid < device->devid)
798 goto next;
799
800 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000801 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400802
Chris Masond3977122009-01-05 21:25:51 -0500803 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400804 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400805
Chris Mason0b86a832008-03-24 15:01:56 -0400806 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000807 extent_end = key.offset + btrfs_dev_extent_length(l,
808 dev_extent);
809 if (key.offset <= start && extent_end > end) {
810 *length = end - start + 1;
811 break;
812 } else if (key.offset <= start && extent_end > start)
813 *length += extent_end - start;
814 else if (key.offset > start && extent_end <= end)
815 *length += extent_end - key.offset;
816 else if (key.offset > start && key.offset <= end) {
817 *length += end - key.offset + 1;
818 break;
819 } else if (key.offset > end)
820 break;
821
822next:
823 path->slots[0]++;
824 }
825 ret = 0;
826out:
827 btrfs_free_path(path);
828 return ret;
829}
830
Chris Mason0b86a832008-03-24 15:01:56 -0400831/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000832 * find_free_dev_extent - find free space in the specified device
833 * @trans: transaction handler
834 * @device: the device which we search the free space in
835 * @num_bytes: the size of the free space that we need
836 * @start: store the start of the free space.
837 * @len: the size of the free space. that we find, or the size of the max
838 * free space if we don't find suitable free space
839 *
Chris Mason0b86a832008-03-24 15:01:56 -0400840 * this uses a pretty simple search, the expectation is that it is
841 * called very infrequently and that a given device has a small number
842 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000843 *
844 * @start is used to store the start of the free space if we find. But if we
845 * don't find suitable free space, it will be used to store the start position
846 * of the max free space.
847 *
848 * @len is used to store the size of the free space that we find.
849 * But if we don't find suitable free space, it is used to store the size of
850 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400851 */
852int find_free_dev_extent(struct btrfs_trans_handle *trans,
853 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000854 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400855{
856 struct btrfs_key key;
857 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000858 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400859 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000860 u64 hole_size;
861 u64 max_hole_start;
862 u64 max_hole_size;
863 u64 extent_end;
864 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400865 u64 search_end = device->total_bytes;
866 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000867 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400868 struct extent_buffer *l;
869
Chris Mason0b86a832008-03-24 15:01:56 -0400870 /* FIXME use last free of some kind */
871
872 /* we don't want to overwrite the superblock on the drive,
873 * so we make sure to start at an offset of at least 1MB
874 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200875 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400876
Miao Xie7bfc8372011-01-05 10:07:26 +0000877 max_hole_start = search_start;
878 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000879 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000880
881 if (search_start >= search_end) {
882 ret = -ENOSPC;
883 goto error;
884 }
885
886 path = btrfs_alloc_path();
887 if (!path) {
888 ret = -ENOMEM;
889 goto error;
890 }
891 path->reada = 2;
892
Chris Mason0b86a832008-03-24 15:01:56 -0400893 key.objectid = device->devid;
894 key.offset = search_start;
895 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000896
Chris Mason0b86a832008-03-24 15:01:56 -0400897 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
898 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000899 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400900 if (ret > 0) {
901 ret = btrfs_previous_item(root, path, key.objectid, key.type);
902 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000903 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400904 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000905
Chris Mason0b86a832008-03-24 15:01:56 -0400906 while (1) {
907 l = path->nodes[0];
908 slot = path->slots[0];
909 if (slot >= btrfs_header_nritems(l)) {
910 ret = btrfs_next_leaf(root, path);
911 if (ret == 0)
912 continue;
913 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000914 goto out;
915
916 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400917 }
918 btrfs_item_key_to_cpu(l, &key, slot);
919
920 if (key.objectid < device->devid)
921 goto next;
922
923 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000924 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400925
Chris Mason0b86a832008-03-24 15:01:56 -0400926 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
927 goto next;
928
Miao Xie7bfc8372011-01-05 10:07:26 +0000929 if (key.offset > search_start) {
930 hole_size = key.offset - search_start;
931
932 if (hole_size > max_hole_size) {
933 max_hole_start = search_start;
934 max_hole_size = hole_size;
935 }
936
937 /*
938 * If this free space is greater than which we need,
939 * it must be the max free space that we have found
940 * until now, so max_hole_start must point to the start
941 * of this free space and the length of this free space
942 * is stored in max_hole_size. Thus, we return
943 * max_hole_start and max_hole_size and go back to the
944 * caller.
945 */
946 if (hole_size >= num_bytes) {
947 ret = 0;
948 goto out;
949 }
950 }
951
Chris Mason0b86a832008-03-24 15:01:56 -0400952 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000953 extent_end = key.offset + btrfs_dev_extent_length(l,
954 dev_extent);
955 if (extent_end > search_start)
956 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400957next:
958 path->slots[0]++;
959 cond_resched();
960 }
Chris Mason0b86a832008-03-24 15:01:56 -0400961
liubo38c01b92011-08-02 02:39:03 +0000962 /*
963 * At this point, search_start should be the end of
964 * allocated dev extents, and when shrinking the device,
965 * search_end may be smaller than search_start.
966 */
967 if (search_end > search_start)
968 hole_size = search_end - search_start;
969
Miao Xie7bfc8372011-01-05 10:07:26 +0000970 if (hole_size > max_hole_size) {
971 max_hole_start = search_start;
972 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400973 }
Chris Mason0b86a832008-03-24 15:01:56 -0400974
Miao Xie7bfc8372011-01-05 10:07:26 +0000975 /* See above. */
976 if (hole_size < num_bytes)
977 ret = -ENOSPC;
978 else
979 ret = 0;
980
981out:
Yan Zheng2b820322008-11-17 21:11:30 -0500982 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000983error:
984 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000985 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000986 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400987 return ret;
988}
989
Christoph Hellwigb2950862008-12-02 09:54:17 -0500990static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400991 struct btrfs_device *device,
992 u64 start)
993{
994 int ret;
995 struct btrfs_path *path;
996 struct btrfs_root *root = device->dev_root;
997 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400998 struct btrfs_key found_key;
999 struct extent_buffer *leaf = NULL;
1000 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001001
1002 path = btrfs_alloc_path();
1003 if (!path)
1004 return -ENOMEM;
1005
1006 key.objectid = device->devid;
1007 key.offset = start;
1008 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001009again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001010 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001011 if (ret > 0) {
1012 ret = btrfs_previous_item(root, path, key.objectid,
1013 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001014 if (ret)
1015 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001016 leaf = path->nodes[0];
1017 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1018 extent = btrfs_item_ptr(leaf, path->slots[0],
1019 struct btrfs_dev_extent);
1020 BUG_ON(found_key.offset > start || found_key.offset +
1021 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001022 key = found_key;
1023 btrfs_release_path(path);
1024 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001025 } else if (ret == 0) {
1026 leaf = path->nodes[0];
1027 extent = btrfs_item_ptr(leaf, path->slots[0],
1028 struct btrfs_dev_extent);
1029 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001030 BUG_ON(ret);
1031
Josef Bacik2bf64752011-09-26 17:12:22 -04001032 if (device->bytes_used > 0) {
1033 u64 len = btrfs_dev_extent_length(leaf, extent);
1034 device->bytes_used -= len;
1035 spin_lock(&root->fs_info->free_chunk_lock);
1036 root->fs_info->free_chunk_space += len;
1037 spin_unlock(&root->fs_info->free_chunk_lock);
1038 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001039 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001040
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001041out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001042 btrfs_free_path(path);
1043 return ret;
1044}
1045
Yan Zheng2b820322008-11-17 21:11:30 -05001046int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001047 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001048 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001049 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001050{
1051 int ret;
1052 struct btrfs_path *path;
1053 struct btrfs_root *root = device->dev_root;
1054 struct btrfs_dev_extent *extent;
1055 struct extent_buffer *leaf;
1056 struct btrfs_key key;
1057
Chris Masondfe25022008-05-13 13:46:40 -04001058 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001059 path = btrfs_alloc_path();
1060 if (!path)
1061 return -ENOMEM;
1062
Chris Mason0b86a832008-03-24 15:01:56 -04001063 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001064 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001065 key.type = BTRFS_DEV_EXTENT_KEY;
1066 ret = btrfs_insert_empty_item(trans, root, path, &key,
1067 sizeof(*extent));
1068 BUG_ON(ret);
1069
1070 leaf = path->nodes[0];
1071 extent = btrfs_item_ptr(leaf, path->slots[0],
1072 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001073 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1074 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1075 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1076
1077 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1078 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1079 BTRFS_UUID_SIZE);
1080
Chris Mason0b86a832008-03-24 15:01:56 -04001081 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1082 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001083 btrfs_free_path(path);
1084 return ret;
1085}
1086
Chris Masona1b32a52008-09-05 16:09:51 -04001087static noinline int find_next_chunk(struct btrfs_root *root,
1088 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001089{
1090 struct btrfs_path *path;
1091 int ret;
1092 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001093 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001094 struct btrfs_key found_key;
1095
1096 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001097 if (!path)
1098 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001099
Chris Masone17cade2008-04-15 15:41:47 -04001100 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001101 key.offset = (u64)-1;
1102 key.type = BTRFS_CHUNK_ITEM_KEY;
1103
1104 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1105 if (ret < 0)
1106 goto error;
1107
1108 BUG_ON(ret == 0);
1109
1110 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1111 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001112 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001113 } else {
1114 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1115 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001116 if (found_key.objectid != objectid)
1117 *offset = 0;
1118 else {
1119 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1120 struct btrfs_chunk);
1121 *offset = found_key.offset +
1122 btrfs_chunk_length(path->nodes[0], chunk);
1123 }
Chris Mason0b86a832008-03-24 15:01:56 -04001124 }
1125 ret = 0;
1126error:
1127 btrfs_free_path(path);
1128 return ret;
1129}
1130
Yan Zheng2b820322008-11-17 21:11:30 -05001131static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001132{
1133 int ret;
1134 struct btrfs_key key;
1135 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001136 struct btrfs_path *path;
1137
1138 root = root->fs_info->chunk_root;
1139
1140 path = btrfs_alloc_path();
1141 if (!path)
1142 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001143
1144 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1145 key.type = BTRFS_DEV_ITEM_KEY;
1146 key.offset = (u64)-1;
1147
1148 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1149 if (ret < 0)
1150 goto error;
1151
1152 BUG_ON(ret == 0);
1153
1154 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1155 BTRFS_DEV_ITEM_KEY);
1156 if (ret) {
1157 *objectid = 1;
1158 } else {
1159 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1160 path->slots[0]);
1161 *objectid = found_key.offset + 1;
1162 }
1163 ret = 0;
1164error:
Yan Zheng2b820322008-11-17 21:11:30 -05001165 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001166 return ret;
1167}
1168
1169/*
1170 * the device information is stored in the chunk root
1171 * the btrfs_device struct should be fully filled in
1172 */
1173int btrfs_add_device(struct btrfs_trans_handle *trans,
1174 struct btrfs_root *root,
1175 struct btrfs_device *device)
1176{
1177 int ret;
1178 struct btrfs_path *path;
1179 struct btrfs_dev_item *dev_item;
1180 struct extent_buffer *leaf;
1181 struct btrfs_key key;
1182 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001183
1184 root = root->fs_info->chunk_root;
1185
1186 path = btrfs_alloc_path();
1187 if (!path)
1188 return -ENOMEM;
1189
Chris Mason0b86a832008-03-24 15:01:56 -04001190 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1191 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001192 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001193
1194 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001195 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001196 if (ret)
1197 goto out;
1198
1199 leaf = path->nodes[0];
1200 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1201
1202 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001203 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001204 btrfs_set_device_type(leaf, dev_item, device->type);
1205 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1206 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1207 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001208 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1209 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001210 btrfs_set_device_group(leaf, dev_item, 0);
1211 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1212 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001213 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001214
Chris Mason0b86a832008-03-24 15:01:56 -04001215 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001216 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001217 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1218 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001219 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001220
Yan Zheng2b820322008-11-17 21:11:30 -05001221 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001222out:
1223 btrfs_free_path(path);
1224 return ret;
1225}
Chris Mason8f18cf12008-04-25 16:53:30 -04001226
Chris Masona061fc82008-05-07 11:43:44 -04001227static int btrfs_rm_dev_item(struct btrfs_root *root,
1228 struct btrfs_device *device)
1229{
1230 int ret;
1231 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001232 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001233 struct btrfs_trans_handle *trans;
1234
1235 root = root->fs_info->chunk_root;
1236
1237 path = btrfs_alloc_path();
1238 if (!path)
1239 return -ENOMEM;
1240
Yan, Zhenga22285a2010-05-16 10:48:46 -04001241 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001242 if (IS_ERR(trans)) {
1243 btrfs_free_path(path);
1244 return PTR_ERR(trans);
1245 }
Chris Masona061fc82008-05-07 11:43:44 -04001246 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1247 key.type = BTRFS_DEV_ITEM_KEY;
1248 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001249 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001250
1251 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1252 if (ret < 0)
1253 goto out;
1254
1255 if (ret > 0) {
1256 ret = -ENOENT;
1257 goto out;
1258 }
1259
1260 ret = btrfs_del_item(trans, root, path);
1261 if (ret)
1262 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001263out:
1264 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001265 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001266 btrfs_commit_transaction(trans, root);
1267 return ret;
1268}
1269
1270int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1271{
1272 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001273 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001274 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001275 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001276 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001277 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001278 u64 all_avail;
1279 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001280 u64 num_devices;
1281 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001282 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001283 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001284
Chris Masona061fc82008-05-07 11:43:44 -04001285 mutex_lock(&uuid_mutex);
1286
1287 all_avail = root->fs_info->avail_data_alloc_bits |
1288 root->fs_info->avail_system_alloc_bits |
1289 root->fs_info->avail_metadata_alloc_bits;
1290
1291 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001292 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001293 printk(KERN_ERR "btrfs: unable to go below four devices "
1294 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001295 ret = -EINVAL;
1296 goto out;
1297 }
1298
1299 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001300 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001301 printk(KERN_ERR "btrfs: unable to go below two "
1302 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001303 ret = -EINVAL;
1304 goto out;
1305 }
1306
Chris Masondfe25022008-05-13 13:46:40 -04001307 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001308 struct list_head *devices;
1309 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001310
Chris Masondfe25022008-05-13 13:46:40 -04001311 device = NULL;
1312 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001313 /*
1314 * It is safe to read the devices since the volume_mutex
1315 * is held.
1316 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001317 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001318 if (tmp->in_fs_metadata && !tmp->bdev) {
1319 device = tmp;
1320 break;
1321 }
1322 }
1323 bdev = NULL;
1324 bh = NULL;
1325 disk_super = NULL;
1326 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001327 printk(KERN_ERR "btrfs: no missing devices found to "
1328 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001329 goto out;
1330 }
Chris Masondfe25022008-05-13 13:46:40 -04001331 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001332 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1333 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001334 if (IS_ERR(bdev)) {
1335 ret = PTR_ERR(bdev);
1336 goto out;
1337 }
1338
Yan Zheng2b820322008-11-17 21:11:30 -05001339 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001340 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001341 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001342 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001343 goto error_close;
1344 }
1345 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001346 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001347 dev_uuid = disk_super->dev_item.uuid;
1348 device = btrfs_find_device(root, devid, dev_uuid,
1349 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001350 if (!device) {
1351 ret = -ENOENT;
1352 goto error_brelse;
1353 }
Chris Masondfe25022008-05-13 13:46:40 -04001354 }
Yan Zheng2b820322008-11-17 21:11:30 -05001355
1356 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001357 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1358 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001359 ret = -EINVAL;
1360 goto error_brelse;
1361 }
1362
1363 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001364 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001365 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001366 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001367 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001368 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001369 }
Chris Masona061fc82008-05-07 11:43:44 -04001370
1371 ret = btrfs_shrink_device(device, 0);
1372 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001373 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001374
Chris Masona061fc82008-05-07 11:43:44 -04001375 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1376 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001377 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001378
Josef Bacik2bf64752011-09-26 17:12:22 -04001379 spin_lock(&root->fs_info->free_chunk_lock);
1380 root->fs_info->free_chunk_space = device->total_bytes -
1381 device->bytes_used;
1382 spin_unlock(&root->fs_info->free_chunk_lock);
1383
Yan Zheng2b820322008-11-17 21:11:30 -05001384 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001385 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001386
1387 /*
1388 * the device list mutex makes sure that we don't change
1389 * the device list while someone else is writing out all
1390 * the device supers.
1391 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001392
1393 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001394 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001395 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001396
Yan Zhenge4404d62008-12-12 10:03:26 -05001397 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001398
Chris Masoncd02dca2010-12-13 14:56:23 -05001399 if (device->missing)
1400 root->fs_info->fs_devices->missing_devices--;
1401
Yan Zheng2b820322008-11-17 21:11:30 -05001402 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1403 struct btrfs_device, dev_list);
1404 if (device->bdev == root->fs_info->sb->s_bdev)
1405 root->fs_info->sb->s_bdev = next_device->bdev;
1406 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1407 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1408
Xiao Guangrong1f781602011-04-20 10:09:16 +00001409 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001410 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001411
1412 call_rcu(&device->rcu, free_device);
1413 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001414
David Sterba6c417612011-04-13 15:41:04 +02001415 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1416 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001417
Xiao Guangrong1f781602011-04-20 10:09:16 +00001418 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001419 struct btrfs_fs_devices *fs_devices;
1420 fs_devices = root->fs_info->fs_devices;
1421 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001422 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001423 break;
1424 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001425 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001426 fs_devices->seed = cur_devices->seed;
1427 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001428 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001429 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001430 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001431 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001432 }
1433
1434 /*
1435 * at this point, the device is zero sized. We want to
1436 * remove it from the devices list and zero out the old super
1437 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001438 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001439 /* make sure this device isn't detected as part of
1440 * the FS anymore
1441 */
1442 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1443 set_buffer_dirty(bh);
1444 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001445 }
Chris Masona061fc82008-05-07 11:43:44 -04001446
Chris Masona061fc82008-05-07 11:43:44 -04001447 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001448
1449error_brelse:
1450 brelse(bh);
1451error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001452 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001453 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001454out:
1455 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001456 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001457error_undo:
1458 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001459 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001460 list_add(&device->dev_alloc_list,
1461 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001462 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001463 root->fs_info->fs_devices->rw_devices++;
1464 }
1465 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001466}
1467
Yan Zheng2b820322008-11-17 21:11:30 -05001468/*
1469 * does all the dirty work required for changing file system's UUID.
1470 */
1471static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1472 struct btrfs_root *root)
1473{
1474 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1475 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001476 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001477 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001478 struct btrfs_device *device;
1479 u64 super_flags;
1480
1481 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001482 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001483 return -EINVAL;
1484
Yan Zhenge4404d62008-12-12 10:03:26 -05001485 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1486 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001487 return -ENOMEM;
1488
Yan Zhenge4404d62008-12-12 10:03:26 -05001489 old_devices = clone_fs_devices(fs_devices);
1490 if (IS_ERR(old_devices)) {
1491 kfree(seed_devices);
1492 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001493 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001494
Yan Zheng2b820322008-11-17 21:11:30 -05001495 list_add(&old_devices->list, &fs_uuids);
1496
Yan Zhenge4404d62008-12-12 10:03:26 -05001497 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1498 seed_devices->opened = 1;
1499 INIT_LIST_HEAD(&seed_devices->devices);
1500 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001501 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001502
1503 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001504 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1505 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001506 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1507
Yan Zhenge4404d62008-12-12 10:03:26 -05001508 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1509 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1510 device->fs_devices = seed_devices;
1511 }
1512
Yan Zheng2b820322008-11-17 21:11:30 -05001513 fs_devices->seeding = 0;
1514 fs_devices->num_devices = 0;
1515 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001516 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001517
1518 generate_random_uuid(fs_devices->fsid);
1519 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1520 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1521 super_flags = btrfs_super_flags(disk_super) &
1522 ~BTRFS_SUPER_FLAG_SEEDING;
1523 btrfs_set_super_flags(disk_super, super_flags);
1524
1525 return 0;
1526}
1527
1528/*
1529 * strore the expected generation for seed devices in device items.
1530 */
1531static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1532 struct btrfs_root *root)
1533{
1534 struct btrfs_path *path;
1535 struct extent_buffer *leaf;
1536 struct btrfs_dev_item *dev_item;
1537 struct btrfs_device *device;
1538 struct btrfs_key key;
1539 u8 fs_uuid[BTRFS_UUID_SIZE];
1540 u8 dev_uuid[BTRFS_UUID_SIZE];
1541 u64 devid;
1542 int ret;
1543
1544 path = btrfs_alloc_path();
1545 if (!path)
1546 return -ENOMEM;
1547
1548 root = root->fs_info->chunk_root;
1549 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1550 key.offset = 0;
1551 key.type = BTRFS_DEV_ITEM_KEY;
1552
1553 while (1) {
1554 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1555 if (ret < 0)
1556 goto error;
1557
1558 leaf = path->nodes[0];
1559next_slot:
1560 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1561 ret = btrfs_next_leaf(root, path);
1562 if (ret > 0)
1563 break;
1564 if (ret < 0)
1565 goto error;
1566 leaf = path->nodes[0];
1567 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001568 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001569 continue;
1570 }
1571
1572 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1573 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1574 key.type != BTRFS_DEV_ITEM_KEY)
1575 break;
1576
1577 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1578 struct btrfs_dev_item);
1579 devid = btrfs_device_id(leaf, dev_item);
1580 read_extent_buffer(leaf, dev_uuid,
1581 (unsigned long)btrfs_device_uuid(dev_item),
1582 BTRFS_UUID_SIZE);
1583 read_extent_buffer(leaf, fs_uuid,
1584 (unsigned long)btrfs_device_fsid(dev_item),
1585 BTRFS_UUID_SIZE);
1586 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1587 BUG_ON(!device);
1588
1589 if (device->fs_devices->seeding) {
1590 btrfs_set_device_generation(leaf, dev_item,
1591 device->generation);
1592 btrfs_mark_buffer_dirty(leaf);
1593 }
1594
1595 path->slots[0]++;
1596 goto next_slot;
1597 }
1598 ret = 0;
1599error:
1600 btrfs_free_path(path);
1601 return ret;
1602}
1603
Chris Mason788f20e2008-04-28 15:29:42 -04001604int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1605{
Josef Bacikd5e20032011-08-04 14:52:27 +00001606 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001607 struct btrfs_trans_handle *trans;
1608 struct btrfs_device *device;
1609 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001610 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001611 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001612 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001613 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001614 int ret = 0;
1615
Yan Zheng2b820322008-11-17 21:11:30 -05001616 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1617 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001618
Li Zefana5d16332011-12-07 20:08:40 -05001619 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001620 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001621 if (IS_ERR(bdev))
1622 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001623
Yan Zheng2b820322008-11-17 21:11:30 -05001624 if (root->fs_info->fs_devices->seeding) {
1625 seeding_dev = 1;
1626 down_write(&sb->s_umount);
1627 mutex_lock(&uuid_mutex);
1628 }
1629
Chris Mason8c8bee12008-09-29 11:19:10 -04001630 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001631
Chris Mason788f20e2008-04-28 15:29:42 -04001632 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001633 /*
1634 * we have the volume lock, so we don't need the extra
1635 * device list mutex while reading the list here.
1636 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001637 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001638 if (device->bdev == bdev) {
1639 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001640 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001641 }
1642 }
1643
1644 device = kzalloc(sizeof(*device), GFP_NOFS);
1645 if (!device) {
1646 /* we can safely leave the fs_devices entry around */
1647 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001648 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001649 }
1650
Chris Mason788f20e2008-04-28 15:29:42 -04001651 device->name = kstrdup(device_path, GFP_NOFS);
1652 if (!device->name) {
1653 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001654 ret = -ENOMEM;
1655 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001656 }
Yan Zheng2b820322008-11-17 21:11:30 -05001657
1658 ret = find_next_devid(root, &device->devid);
1659 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001660 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001661 kfree(device);
1662 goto error;
1663 }
1664
Yan, Zhenga22285a2010-05-16 10:48:46 -04001665 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001666 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001667 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001668 kfree(device);
1669 ret = PTR_ERR(trans);
1670 goto error;
1671 }
1672
Yan Zheng2b820322008-11-17 21:11:30 -05001673 lock_chunks(root);
1674
Josef Bacikd5e20032011-08-04 14:52:27 +00001675 q = bdev_get_queue(bdev);
1676 if (blk_queue_discard(q))
1677 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001678 device->writeable = 1;
1679 device->work.func = pending_bios_fn;
1680 generate_random_uuid(device->uuid);
1681 spin_lock_init(&device->io_lock);
1682 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001683 device->io_width = root->sectorsize;
1684 device->io_align = root->sectorsize;
1685 device->sector_size = root->sectorsize;
1686 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001687 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001688 device->dev_root = root->fs_info->dev_root;
1689 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001690 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001691 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001692 set_blocksize(device->bdev, 4096);
1693
Yan Zheng2b820322008-11-17 21:11:30 -05001694 if (seeding_dev) {
1695 sb->s_flags &= ~MS_RDONLY;
1696 ret = btrfs_prepare_sprout(trans, root);
1697 BUG_ON(ret);
1698 }
1699
1700 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001701
1702 /*
1703 * we don't want write_supers to jump in here with our device
1704 * half setup
1705 */
1706 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001707 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001708 list_add(&device->dev_alloc_list,
1709 &root->fs_info->fs_devices->alloc_list);
1710 root->fs_info->fs_devices->num_devices++;
1711 root->fs_info->fs_devices->open_devices++;
1712 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001713 if (device->can_discard)
1714 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001715 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1716
Josef Bacik2bf64752011-09-26 17:12:22 -04001717 spin_lock(&root->fs_info->free_chunk_lock);
1718 root->fs_info->free_chunk_space += device->total_bytes;
1719 spin_unlock(&root->fs_info->free_chunk_lock);
1720
Chris Masonc2898112009-06-10 09:51:32 -04001721 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1722 root->fs_info->fs_devices->rotating = 1;
1723
David Sterba6c417612011-04-13 15:41:04 +02001724 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1725 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001726 total_bytes + device->total_bytes);
1727
David Sterba6c417612011-04-13 15:41:04 +02001728 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1729 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001730 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001731 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001732
Yan Zheng2b820322008-11-17 21:11:30 -05001733 if (seeding_dev) {
1734 ret = init_first_rw_device(trans, root, device);
1735 BUG_ON(ret);
1736 ret = btrfs_finish_sprout(trans, root);
1737 BUG_ON(ret);
1738 } else {
1739 ret = btrfs_add_device(trans, root, device);
1740 }
1741
Chris Mason913d9522009-03-10 13:17:18 -04001742 /*
1743 * we've got more storage, clear any full flags on the space
1744 * infos
1745 */
1746 btrfs_clear_space_info_full(root->fs_info);
1747
Chris Mason7d9eb122008-07-08 14:19:17 -04001748 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001749 btrfs_commit_transaction(trans, root);
1750
1751 if (seeding_dev) {
1752 mutex_unlock(&uuid_mutex);
1753 up_write(&sb->s_umount);
1754
1755 ret = btrfs_relocate_sys_chunks(root);
1756 BUG_ON(ret);
1757 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001758
Chris Mason788f20e2008-04-28 15:29:42 -04001759 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001760error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001761 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001762 if (seeding_dev) {
1763 mutex_unlock(&uuid_mutex);
1764 up_write(&sb->s_umount);
1765 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001766 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04001767}
1768
Chris Masond3977122009-01-05 21:25:51 -05001769static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1770 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001771{
1772 int ret;
1773 struct btrfs_path *path;
1774 struct btrfs_root *root;
1775 struct btrfs_dev_item *dev_item;
1776 struct extent_buffer *leaf;
1777 struct btrfs_key key;
1778
1779 root = device->dev_root->fs_info->chunk_root;
1780
1781 path = btrfs_alloc_path();
1782 if (!path)
1783 return -ENOMEM;
1784
1785 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1786 key.type = BTRFS_DEV_ITEM_KEY;
1787 key.offset = device->devid;
1788
1789 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1790 if (ret < 0)
1791 goto out;
1792
1793 if (ret > 0) {
1794 ret = -ENOENT;
1795 goto out;
1796 }
1797
1798 leaf = path->nodes[0];
1799 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1800
1801 btrfs_set_device_id(leaf, dev_item, device->devid);
1802 btrfs_set_device_type(leaf, dev_item, device->type);
1803 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1804 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1805 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001806 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001807 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1808 btrfs_mark_buffer_dirty(leaf);
1809
1810out:
1811 btrfs_free_path(path);
1812 return ret;
1813}
1814
Chris Mason7d9eb122008-07-08 14:19:17 -04001815static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001816 struct btrfs_device *device, u64 new_size)
1817{
1818 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02001819 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001820 u64 old_total = btrfs_super_total_bytes(super_copy);
1821 u64 diff = new_size - device->total_bytes;
1822
Yan Zheng2b820322008-11-17 21:11:30 -05001823 if (!device->writeable)
1824 return -EACCES;
1825 if (new_size <= device->total_bytes)
1826 return -EINVAL;
1827
Chris Mason8f18cf12008-04-25 16:53:30 -04001828 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001829 device->fs_devices->total_rw_bytes += diff;
1830
1831 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001832 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001833 btrfs_clear_space_info_full(device->dev_root->fs_info);
1834
Chris Mason8f18cf12008-04-25 16:53:30 -04001835 return btrfs_update_device(trans, device);
1836}
1837
Chris Mason7d9eb122008-07-08 14:19:17 -04001838int btrfs_grow_device(struct btrfs_trans_handle *trans,
1839 struct btrfs_device *device, u64 new_size)
1840{
1841 int ret;
1842 lock_chunks(device->dev_root);
1843 ret = __btrfs_grow_device(trans, device, new_size);
1844 unlock_chunks(device->dev_root);
1845 return ret;
1846}
1847
Chris Mason8f18cf12008-04-25 16:53:30 -04001848static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1849 struct btrfs_root *root,
1850 u64 chunk_tree, u64 chunk_objectid,
1851 u64 chunk_offset)
1852{
1853 int ret;
1854 struct btrfs_path *path;
1855 struct btrfs_key key;
1856
1857 root = root->fs_info->chunk_root;
1858 path = btrfs_alloc_path();
1859 if (!path)
1860 return -ENOMEM;
1861
1862 key.objectid = chunk_objectid;
1863 key.offset = chunk_offset;
1864 key.type = BTRFS_CHUNK_ITEM_KEY;
1865
1866 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1867 BUG_ON(ret);
1868
1869 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001870
1871 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001872 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001873}
1874
Christoph Hellwigb2950862008-12-02 09:54:17 -05001875static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001876 chunk_offset)
1877{
David Sterba6c417612011-04-13 15:41:04 +02001878 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04001879 struct btrfs_disk_key *disk_key;
1880 struct btrfs_chunk *chunk;
1881 u8 *ptr;
1882 int ret = 0;
1883 u32 num_stripes;
1884 u32 array_size;
1885 u32 len = 0;
1886 u32 cur;
1887 struct btrfs_key key;
1888
1889 array_size = btrfs_super_sys_array_size(super_copy);
1890
1891 ptr = super_copy->sys_chunk_array;
1892 cur = 0;
1893
1894 while (cur < array_size) {
1895 disk_key = (struct btrfs_disk_key *)ptr;
1896 btrfs_disk_key_to_cpu(&key, disk_key);
1897
1898 len = sizeof(*disk_key);
1899
1900 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1901 chunk = (struct btrfs_chunk *)(ptr + len);
1902 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1903 len += btrfs_chunk_item_size(num_stripes);
1904 } else {
1905 ret = -EIO;
1906 break;
1907 }
1908 if (key.objectid == chunk_objectid &&
1909 key.offset == chunk_offset) {
1910 memmove(ptr, ptr + len, array_size - (cur + len));
1911 array_size -= len;
1912 btrfs_set_super_sys_array_size(super_copy, array_size);
1913 } else {
1914 ptr += len;
1915 cur += len;
1916 }
1917 }
1918 return ret;
1919}
1920
Christoph Hellwigb2950862008-12-02 09:54:17 -05001921static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001922 u64 chunk_tree, u64 chunk_objectid,
1923 u64 chunk_offset)
1924{
1925 struct extent_map_tree *em_tree;
1926 struct btrfs_root *extent_root;
1927 struct btrfs_trans_handle *trans;
1928 struct extent_map *em;
1929 struct map_lookup *map;
1930 int ret;
1931 int i;
1932
1933 root = root->fs_info->chunk_root;
1934 extent_root = root->fs_info->extent_root;
1935 em_tree = &root->fs_info->mapping_tree.map_tree;
1936
Josef Bacikba1bf482009-09-11 16:11:19 -04001937 ret = btrfs_can_relocate(extent_root, chunk_offset);
1938 if (ret)
1939 return -ENOSPC;
1940
Chris Mason8f18cf12008-04-25 16:53:30 -04001941 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001942 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001943 if (ret)
1944 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001945
Yan, Zhenga22285a2010-05-16 10:48:46 -04001946 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001947 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001948
Chris Mason7d9eb122008-07-08 14:19:17 -04001949 lock_chunks(root);
1950
Chris Mason8f18cf12008-04-25 16:53:30 -04001951 /*
1952 * step two, delete the device extents and the
1953 * chunk tree entries
1954 */
Chris Mason890871b2009-09-02 16:24:52 -04001955 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001956 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001957 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001958
Chris Masona061fc82008-05-07 11:43:44 -04001959 BUG_ON(em->start > chunk_offset ||
1960 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001961 map = (struct map_lookup *)em->bdev;
1962
1963 for (i = 0; i < map->num_stripes; i++) {
1964 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1965 map->stripes[i].physical);
1966 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001967
Chris Masondfe25022008-05-13 13:46:40 -04001968 if (map->stripes[i].dev) {
1969 ret = btrfs_update_device(trans, map->stripes[i].dev);
1970 BUG_ON(ret);
1971 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001972 }
1973 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1974 chunk_offset);
1975
1976 BUG_ON(ret);
1977
liubo1abe9b82011-03-24 11:18:59 +00001978 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1979
Chris Mason8f18cf12008-04-25 16:53:30 -04001980 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1981 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1982 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001983 }
1984
Zheng Yan1a40e232008-09-26 10:09:34 -04001985 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1986 BUG_ON(ret);
1987
Chris Mason890871b2009-09-02 16:24:52 -04001988 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001989 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001990 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001991
Chris Mason8f18cf12008-04-25 16:53:30 -04001992 kfree(map);
1993 em->bdev = NULL;
1994
1995 /* once for the tree */
1996 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001997 /* once for us */
1998 free_extent_map(em);
1999
Chris Mason7d9eb122008-07-08 14:19:17 -04002000 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002001 btrfs_end_transaction(trans, root);
2002 return 0;
2003}
2004
Yan Zheng2b820322008-11-17 21:11:30 -05002005static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2006{
2007 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2008 struct btrfs_path *path;
2009 struct extent_buffer *leaf;
2010 struct btrfs_chunk *chunk;
2011 struct btrfs_key key;
2012 struct btrfs_key found_key;
2013 u64 chunk_tree = chunk_root->root_key.objectid;
2014 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002015 bool retried = false;
2016 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002017 int ret;
2018
2019 path = btrfs_alloc_path();
2020 if (!path)
2021 return -ENOMEM;
2022
Josef Bacikba1bf482009-09-11 16:11:19 -04002023again:
Yan Zheng2b820322008-11-17 21:11:30 -05002024 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2025 key.offset = (u64)-1;
2026 key.type = BTRFS_CHUNK_ITEM_KEY;
2027
2028 while (1) {
2029 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2030 if (ret < 0)
2031 goto error;
2032 BUG_ON(ret == 0);
2033
2034 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2035 key.type);
2036 if (ret < 0)
2037 goto error;
2038 if (ret > 0)
2039 break;
2040
2041 leaf = path->nodes[0];
2042 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2043
2044 chunk = btrfs_item_ptr(leaf, path->slots[0],
2045 struct btrfs_chunk);
2046 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002047 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002048
2049 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2050 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2051 found_key.objectid,
2052 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002053 if (ret == -ENOSPC)
2054 failed++;
2055 else if (ret)
2056 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002057 }
2058
2059 if (found_key.offset == 0)
2060 break;
2061 key.offset = found_key.offset - 1;
2062 }
2063 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002064 if (failed && !retried) {
2065 failed = 0;
2066 retried = true;
2067 goto again;
2068 } else if (failed && retried) {
2069 WARN_ON(1);
2070 ret = -ENOSPC;
2071 }
Yan Zheng2b820322008-11-17 21:11:30 -05002072error:
2073 btrfs_free_path(path);
2074 return ret;
2075}
2076
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002077static int insert_balance_item(struct btrfs_root *root,
2078 struct btrfs_balance_control *bctl)
2079{
2080 struct btrfs_trans_handle *trans;
2081 struct btrfs_balance_item *item;
2082 struct btrfs_disk_balance_args disk_bargs;
2083 struct btrfs_path *path;
2084 struct extent_buffer *leaf;
2085 struct btrfs_key key;
2086 int ret, err;
2087
2088 path = btrfs_alloc_path();
2089 if (!path)
2090 return -ENOMEM;
2091
2092 trans = btrfs_start_transaction(root, 0);
2093 if (IS_ERR(trans)) {
2094 btrfs_free_path(path);
2095 return PTR_ERR(trans);
2096 }
2097
2098 key.objectid = BTRFS_BALANCE_OBJECTID;
2099 key.type = BTRFS_BALANCE_ITEM_KEY;
2100 key.offset = 0;
2101
2102 ret = btrfs_insert_empty_item(trans, root, path, &key,
2103 sizeof(*item));
2104 if (ret)
2105 goto out;
2106
2107 leaf = path->nodes[0];
2108 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2109
2110 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2111
2112 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2113 btrfs_set_balance_data(leaf, item, &disk_bargs);
2114 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2115 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2116 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2117 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2118
2119 btrfs_set_balance_flags(leaf, item, bctl->flags);
2120
2121 btrfs_mark_buffer_dirty(leaf);
2122out:
2123 btrfs_free_path(path);
2124 err = btrfs_commit_transaction(trans, root);
2125 if (err && !ret)
2126 ret = err;
2127 return ret;
2128}
2129
2130static int del_balance_item(struct btrfs_root *root)
2131{
2132 struct btrfs_trans_handle *trans;
2133 struct btrfs_path *path;
2134 struct btrfs_key key;
2135 int ret, err;
2136
2137 path = btrfs_alloc_path();
2138 if (!path)
2139 return -ENOMEM;
2140
2141 trans = btrfs_start_transaction(root, 0);
2142 if (IS_ERR(trans)) {
2143 btrfs_free_path(path);
2144 return PTR_ERR(trans);
2145 }
2146
2147 key.objectid = BTRFS_BALANCE_OBJECTID;
2148 key.type = BTRFS_BALANCE_ITEM_KEY;
2149 key.offset = 0;
2150
2151 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2152 if (ret < 0)
2153 goto out;
2154 if (ret > 0) {
2155 ret = -ENOENT;
2156 goto out;
2157 }
2158
2159 ret = btrfs_del_item(trans, root, path);
2160out:
2161 btrfs_free_path(path);
2162 err = btrfs_commit_transaction(trans, root);
2163 if (err && !ret)
2164 ret = err;
2165 return ret;
2166}
2167
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002168/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002169 * This is a heuristic used to reduce the number of chunks balanced on
2170 * resume after balance was interrupted.
2171 */
2172static void update_balance_args(struct btrfs_balance_control *bctl)
2173{
2174 /*
2175 * Turn on soft mode for chunk types that were being converted.
2176 */
2177 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2178 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2179 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2180 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2181 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2182 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2183
2184 /*
2185 * Turn on usage filter if is not already used. The idea is
2186 * that chunks that we have already balanced should be
2187 * reasonably full. Don't do it for chunks that are being
2188 * converted - that will keep us from relocating unconverted
2189 * (albeit full) chunks.
2190 */
2191 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2192 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2193 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2194 bctl->data.usage = 90;
2195 }
2196 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2197 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2198 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2199 bctl->sys.usage = 90;
2200 }
2201 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2202 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2203 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2204 bctl->meta.usage = 90;
2205 }
2206}
2207
2208/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002209 * Should be called with both balance and volume mutexes held to
2210 * serialize other volume operations (add_dev/rm_dev/resize) with
2211 * restriper. Same goes for unset_balance_control.
2212 */
2213static void set_balance_control(struct btrfs_balance_control *bctl)
2214{
2215 struct btrfs_fs_info *fs_info = bctl->fs_info;
2216
2217 BUG_ON(fs_info->balance_ctl);
2218
2219 spin_lock(&fs_info->balance_lock);
2220 fs_info->balance_ctl = bctl;
2221 spin_unlock(&fs_info->balance_lock);
2222}
2223
2224static void unset_balance_control(struct btrfs_fs_info *fs_info)
2225{
2226 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2227
2228 BUG_ON(!fs_info->balance_ctl);
2229
2230 spin_lock(&fs_info->balance_lock);
2231 fs_info->balance_ctl = NULL;
2232 spin_unlock(&fs_info->balance_lock);
2233
2234 kfree(bctl);
2235}
2236
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002237/*
2238 * Balance filters. Return 1 if chunk should be filtered out
2239 * (should not be balanced).
2240 */
2241static int chunk_profiles_filter(u64 chunk_profile,
2242 struct btrfs_balance_args *bargs)
2243{
2244 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2245
2246 if (chunk_profile == 0)
2247 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2248
2249 if (bargs->profiles & chunk_profile)
2250 return 0;
2251
2252 return 1;
2253}
2254
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002255static u64 div_factor_fine(u64 num, int factor)
2256{
2257 if (factor <= 0)
2258 return 0;
2259 if (factor >= 100)
2260 return num;
2261
2262 num *= factor;
2263 do_div(num, 100);
2264 return num;
2265}
2266
2267static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2268 struct btrfs_balance_args *bargs)
2269{
2270 struct btrfs_block_group_cache *cache;
2271 u64 chunk_used, user_thresh;
2272 int ret = 1;
2273
2274 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2275 chunk_used = btrfs_block_group_used(&cache->item);
2276
2277 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2278 if (chunk_used < user_thresh)
2279 ret = 0;
2280
2281 btrfs_put_block_group(cache);
2282 return ret;
2283}
2284
Ilya Dryomov409d4042012-01-16 22:04:47 +02002285static int chunk_devid_filter(struct extent_buffer *leaf,
2286 struct btrfs_chunk *chunk,
2287 struct btrfs_balance_args *bargs)
2288{
2289 struct btrfs_stripe *stripe;
2290 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2291 int i;
2292
2293 for (i = 0; i < num_stripes; i++) {
2294 stripe = btrfs_stripe_nr(chunk, i);
2295 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2296 return 0;
2297 }
2298
2299 return 1;
2300}
2301
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002302/* [pstart, pend) */
2303static int chunk_drange_filter(struct extent_buffer *leaf,
2304 struct btrfs_chunk *chunk,
2305 u64 chunk_offset,
2306 struct btrfs_balance_args *bargs)
2307{
2308 struct btrfs_stripe *stripe;
2309 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2310 u64 stripe_offset;
2311 u64 stripe_length;
2312 int factor;
2313 int i;
2314
2315 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2316 return 0;
2317
2318 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2319 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2320 factor = 2;
2321 else
2322 factor = 1;
2323 factor = num_stripes / factor;
2324
2325 for (i = 0; i < num_stripes; i++) {
2326 stripe = btrfs_stripe_nr(chunk, i);
2327 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2328 continue;
2329
2330 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2331 stripe_length = btrfs_chunk_length(leaf, chunk);
2332 do_div(stripe_length, factor);
2333
2334 if (stripe_offset < bargs->pend &&
2335 stripe_offset + stripe_length > bargs->pstart)
2336 return 0;
2337 }
2338
2339 return 1;
2340}
2341
Ilya Dryomovea671762012-01-16 22:04:48 +02002342/* [vstart, vend) */
2343static int chunk_vrange_filter(struct extent_buffer *leaf,
2344 struct btrfs_chunk *chunk,
2345 u64 chunk_offset,
2346 struct btrfs_balance_args *bargs)
2347{
2348 if (chunk_offset < bargs->vend &&
2349 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2350 /* at least part of the chunk is inside this vrange */
2351 return 0;
2352
2353 return 1;
2354}
2355
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002356static int chunk_soft_convert_filter(u64 chunk_profile,
2357 struct btrfs_balance_args *bargs)
2358{
2359 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2360 return 0;
2361
2362 chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
2363
2364 if (chunk_profile == 0)
2365 chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2366
2367 if (bargs->target & chunk_profile)
2368 return 1;
2369
2370 return 0;
2371}
2372
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002373static int should_balance_chunk(struct btrfs_root *root,
2374 struct extent_buffer *leaf,
2375 struct btrfs_chunk *chunk, u64 chunk_offset)
2376{
2377 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2378 struct btrfs_balance_args *bargs = NULL;
2379 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2380
2381 /* type filter */
2382 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2383 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2384 return 0;
2385 }
2386
2387 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2388 bargs = &bctl->data;
2389 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2390 bargs = &bctl->sys;
2391 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2392 bargs = &bctl->meta;
2393
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002394 /* profiles filter */
2395 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2396 chunk_profiles_filter(chunk_type, bargs)) {
2397 return 0;
2398 }
2399
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002400 /* usage filter */
2401 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2402 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2403 return 0;
2404 }
2405
Ilya Dryomov409d4042012-01-16 22:04:47 +02002406 /* devid filter */
2407 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2408 chunk_devid_filter(leaf, chunk, bargs)) {
2409 return 0;
2410 }
2411
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002412 /* drange filter, makes sense only with devid filter */
2413 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2414 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2415 return 0;
2416 }
2417
Ilya Dryomovea671762012-01-16 22:04:48 +02002418 /* vrange filter */
2419 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2420 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2421 return 0;
2422 }
2423
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002424 /* soft profile changing mode */
2425 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2426 chunk_soft_convert_filter(chunk_type, bargs)) {
2427 return 0;
2428 }
2429
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002430 return 1;
2431}
2432
Chris Masonec44a352008-04-28 15:29:52 -04002433static u64 div_factor(u64 num, int factor)
2434{
2435 if (factor == 10)
2436 return num;
2437 num *= factor;
2438 do_div(num, 10);
2439 return num;
2440}
2441
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002442static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002443{
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002444 struct btrfs_root *chunk_root = fs_info->chunk_root;
2445 struct btrfs_root *dev_root = fs_info->dev_root;
2446 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002447 struct btrfs_device *device;
2448 u64 old_size;
2449 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002450 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002451 struct btrfs_path *path;
2452 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002453 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002454 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002455 struct extent_buffer *leaf;
2456 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002457 int ret;
2458 int enospc_errors = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002459
Chris Masonec44a352008-04-28 15:29:52 -04002460 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002461 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002462 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002463 old_size = device->total_bytes;
2464 size_to_free = div_factor(old_size, 1);
2465 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002466 if (!device->writeable ||
2467 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002468 continue;
2469
2470 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002471 if (ret == -ENOSPC)
2472 break;
Chris Masonec44a352008-04-28 15:29:52 -04002473 BUG_ON(ret);
2474
Yan, Zhenga22285a2010-05-16 10:48:46 -04002475 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002476 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002477
2478 ret = btrfs_grow_device(trans, device, old_size);
2479 BUG_ON(ret);
2480
2481 btrfs_end_transaction(trans, dev_root);
2482 }
2483
2484 /* step two, relocate all the chunks */
2485 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002486 if (!path) {
2487 ret = -ENOMEM;
2488 goto error;
2489 }
Chris Masonec44a352008-04-28 15:29:52 -04002490 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2491 key.offset = (u64)-1;
2492 key.type = BTRFS_CHUNK_ITEM_KEY;
2493
Chris Masond3977122009-01-05 21:25:51 -05002494 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002495 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2496 if (ret < 0)
2497 goto error;
2498
2499 /*
2500 * this shouldn't happen, it means the last relocate
2501 * failed
2502 */
2503 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002504 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002505
2506 ret = btrfs_previous_item(chunk_root, path, 0,
2507 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002508 if (ret) {
2509 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002510 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002511 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002512
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002513 leaf = path->nodes[0];
2514 slot = path->slots[0];
2515 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2516
Chris Masonec44a352008-04-28 15:29:52 -04002517 if (found_key.objectid != key.objectid)
2518 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002519
Chris Masonec44a352008-04-28 15:29:52 -04002520 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002521 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002522 break;
2523
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002524 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2525
2526 ret = should_balance_chunk(chunk_root, leaf, chunk,
2527 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002528 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002529 if (!ret)
2530 goto loop;
2531
Chris Masonec44a352008-04-28 15:29:52 -04002532 ret = btrfs_relocate_chunk(chunk_root,
2533 chunk_root->root_key.objectid,
2534 found_key.objectid,
2535 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002536 if (ret && ret != -ENOSPC)
2537 goto error;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002538 if (ret == -ENOSPC)
2539 enospc_errors++;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002540loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002541 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002542 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002543
Chris Masonec44a352008-04-28 15:29:52 -04002544error:
2545 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002546 if (enospc_errors) {
2547 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2548 enospc_errors);
2549 if (!ret)
2550 ret = -ENOSPC;
2551 }
2552
2553 return ret;
2554}
2555
2556static void __cancel_balance(struct btrfs_fs_info *fs_info)
2557{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002558 int ret;
2559
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002560 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002561 ret = del_balance_item(fs_info->tree_root);
2562 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002563}
2564
2565void update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
2566 struct btrfs_ioctl_balance_args *bargs);
2567
2568/*
2569 * Should be called with both balance and volume mutexes held
2570 */
2571int btrfs_balance(struct btrfs_balance_control *bctl,
2572 struct btrfs_ioctl_balance_args *bargs)
2573{
2574 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002575 u64 allowed;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002576 int ret;
2577
2578 if (btrfs_fs_closing(fs_info)) {
2579 ret = -EINVAL;
2580 goto out;
2581 }
2582
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002583 /*
2584 * In case of mixed groups both data and meta should be picked,
2585 * and identical options should be given for both of them.
2586 */
2587 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2588 if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
2589 (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
2590 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2591 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2592 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
2593 printk(KERN_ERR "btrfs: with mixed groups data and "
2594 "metadata balance options must be the same\n");
2595 ret = -EINVAL;
2596 goto out;
2597 }
2598 }
2599
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02002600 /*
2601 * Profile changing sanity checks. Skip them if a simple
2602 * balance is requested.
2603 */
2604 if (!((bctl->data.flags | bctl->sys.flags | bctl->meta.flags) &
2605 BTRFS_BALANCE_ARGS_CONVERT))
2606 goto do_balance;
2607
2608 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
2609 if (fs_info->fs_devices->num_devices == 1)
2610 allowed |= BTRFS_BLOCK_GROUP_DUP;
2611 else if (fs_info->fs_devices->num_devices < 4)
2612 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
2613 else
2614 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2615 BTRFS_BLOCK_GROUP_RAID10);
2616
2617 if (!profile_is_valid(bctl->data.target, 1) ||
2618 bctl->data.target & ~allowed) {
2619 printk(KERN_ERR "btrfs: unable to start balance with target "
2620 "data profile %llu\n",
2621 (unsigned long long)bctl->data.target);
2622 ret = -EINVAL;
2623 goto out;
2624 }
2625 if (!profile_is_valid(bctl->meta.target, 1) ||
2626 bctl->meta.target & ~allowed) {
2627 printk(KERN_ERR "btrfs: unable to start balance with target "
2628 "metadata profile %llu\n",
2629 (unsigned long long)bctl->meta.target);
2630 ret = -EINVAL;
2631 goto out;
2632 }
2633 if (!profile_is_valid(bctl->sys.target, 1) ||
2634 bctl->sys.target & ~allowed) {
2635 printk(KERN_ERR "btrfs: unable to start balance with target "
2636 "system profile %llu\n",
2637 (unsigned long long)bctl->sys.target);
2638 ret = -EINVAL;
2639 goto out;
2640 }
2641
2642 if (bctl->data.target & BTRFS_BLOCK_GROUP_DUP) {
2643 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
2644 ret = -EINVAL;
2645 goto out;
2646 }
2647
2648 /* allow to reduce meta or sys integrity only if force set */
2649 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2650 BTRFS_BLOCK_GROUP_RAID10;
2651 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2652 (fs_info->avail_system_alloc_bits & allowed) &&
2653 !(bctl->sys.target & allowed)) ||
2654 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
2655 (fs_info->avail_metadata_alloc_bits & allowed) &&
2656 !(bctl->meta.target & allowed))) {
2657 if (bctl->flags & BTRFS_BALANCE_FORCE) {
2658 printk(KERN_INFO "btrfs: force reducing metadata "
2659 "integrity\n");
2660 } else {
2661 printk(KERN_ERR "btrfs: balance will reduce metadata "
2662 "integrity, use force if you want this\n");
2663 ret = -EINVAL;
2664 goto out;
2665 }
2666 }
2667
2668do_balance:
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002669 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002670 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002671 goto out;
2672
Ilya Dryomov59641012012-01-16 22:04:48 +02002673 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
2674 BUG_ON(ret == -EEXIST);
2675 set_balance_control(bctl);
2676 } else {
2677 BUG_ON(ret != -EEXIST);
2678 spin_lock(&fs_info->balance_lock);
2679 update_balance_args(bctl);
2680 spin_unlock(&fs_info->balance_lock);
2681 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002682
2683 mutex_unlock(&fs_info->balance_mutex);
2684
2685 ret = __btrfs_balance(fs_info);
2686
2687 mutex_lock(&fs_info->balance_mutex);
2688
2689 if (bargs) {
2690 memset(bargs, 0, sizeof(*bargs));
2691 update_ioctl_balance_args(fs_info, bargs);
2692 }
2693
2694 __cancel_balance(fs_info);
2695
2696 return ret;
2697out:
Ilya Dryomov59641012012-01-16 22:04:48 +02002698 if (bctl->flags & BTRFS_BALANCE_RESUME)
2699 __cancel_balance(fs_info);
2700 else
2701 kfree(bctl);
2702 return ret;
2703}
2704
2705static int balance_kthread(void *data)
2706{
2707 struct btrfs_balance_control *bctl =
2708 (struct btrfs_balance_control *)data;
2709 struct btrfs_fs_info *fs_info = bctl->fs_info;
2710 int ret;
2711
2712 mutex_lock(&fs_info->volume_mutex);
2713 mutex_lock(&fs_info->balance_mutex);
2714
2715 set_balance_control(bctl);
2716
2717 printk(KERN_INFO "btrfs: continuing balance\n");
2718 ret = btrfs_balance(bctl, NULL);
2719
2720 mutex_unlock(&fs_info->balance_mutex);
2721 mutex_unlock(&fs_info->volume_mutex);
2722 return ret;
2723}
2724
2725int btrfs_recover_balance(struct btrfs_root *tree_root)
2726{
2727 struct task_struct *tsk;
2728 struct btrfs_balance_control *bctl;
2729 struct btrfs_balance_item *item;
2730 struct btrfs_disk_balance_args disk_bargs;
2731 struct btrfs_path *path;
2732 struct extent_buffer *leaf;
2733 struct btrfs_key key;
2734 int ret;
2735
2736 path = btrfs_alloc_path();
2737 if (!path)
2738 return -ENOMEM;
2739
2740 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
2741 if (!bctl) {
2742 ret = -ENOMEM;
2743 goto out;
2744 }
2745
2746 key.objectid = BTRFS_BALANCE_OBJECTID;
2747 key.type = BTRFS_BALANCE_ITEM_KEY;
2748 key.offset = 0;
2749
2750 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
2751 if (ret < 0)
2752 goto out_bctl;
2753 if (ret > 0) { /* ret = -ENOENT; */
2754 ret = 0;
2755 goto out_bctl;
2756 }
2757
2758 leaf = path->nodes[0];
2759 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2760
2761 bctl->fs_info = tree_root->fs_info;
2762 bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
2763
2764 btrfs_balance_data(leaf, item, &disk_bargs);
2765 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
2766 btrfs_balance_meta(leaf, item, &disk_bargs);
2767 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
2768 btrfs_balance_sys(leaf, item, &disk_bargs);
2769 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
2770
2771 tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
2772 if (IS_ERR(tsk))
2773 ret = PTR_ERR(tsk);
2774 else
2775 goto out;
2776
2777out_bctl:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002778 kfree(bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02002779out:
2780 btrfs_free_path(path);
Chris Masonec44a352008-04-28 15:29:52 -04002781 return ret;
2782}
2783
Chris Mason8f18cf12008-04-25 16:53:30 -04002784/*
2785 * shrinking a device means finding all of the device extents past
2786 * the new size, and then following the back refs to the chunks.
2787 * The chunk relocation code actually frees the device extent
2788 */
2789int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2790{
2791 struct btrfs_trans_handle *trans;
2792 struct btrfs_root *root = device->dev_root;
2793 struct btrfs_dev_extent *dev_extent = NULL;
2794 struct btrfs_path *path;
2795 u64 length;
2796 u64 chunk_tree;
2797 u64 chunk_objectid;
2798 u64 chunk_offset;
2799 int ret;
2800 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002801 int failed = 0;
2802 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002803 struct extent_buffer *l;
2804 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02002805 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002806 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002807 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002808 u64 diff = device->total_bytes - new_size;
2809
Yan Zheng2b820322008-11-17 21:11:30 -05002810 if (new_size >= device->total_bytes)
2811 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002812
2813 path = btrfs_alloc_path();
2814 if (!path)
2815 return -ENOMEM;
2816
Chris Mason8f18cf12008-04-25 16:53:30 -04002817 path->reada = 2;
2818
Chris Mason7d9eb122008-07-08 14:19:17 -04002819 lock_chunks(root);
2820
Chris Mason8f18cf12008-04-25 16:53:30 -04002821 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04002822 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05002823 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002824 spin_lock(&root->fs_info->free_chunk_lock);
2825 root->fs_info->free_chunk_space -= diff;
2826 spin_unlock(&root->fs_info->free_chunk_lock);
2827 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002828 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002829
Josef Bacikba1bf482009-09-11 16:11:19 -04002830again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002831 key.objectid = device->devid;
2832 key.offset = (u64)-1;
2833 key.type = BTRFS_DEV_EXTENT_KEY;
2834
2835 while (1) {
2836 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2837 if (ret < 0)
2838 goto done;
2839
2840 ret = btrfs_previous_item(root, path, 0, key.type);
2841 if (ret < 0)
2842 goto done;
2843 if (ret) {
2844 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002845 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002846 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002847 }
2848
2849 l = path->nodes[0];
2850 slot = path->slots[0];
2851 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2852
Josef Bacikba1bf482009-09-11 16:11:19 -04002853 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002854 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002855 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002856 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002857
2858 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2859 length = btrfs_dev_extent_length(l, dev_extent);
2860
Josef Bacikba1bf482009-09-11 16:11:19 -04002861 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002862 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002863 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002864 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002865
2866 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2867 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2868 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002869 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002870
2871 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2872 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002873 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002874 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002875 if (ret == -ENOSPC)
2876 failed++;
2877 key.offset -= 1;
2878 }
2879
2880 if (failed && !retried) {
2881 failed = 0;
2882 retried = true;
2883 goto again;
2884 } else if (failed && retried) {
2885 ret = -ENOSPC;
2886 lock_chunks(root);
2887
2888 device->total_bytes = old_size;
2889 if (device->writeable)
2890 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04002891 spin_lock(&root->fs_info->free_chunk_lock);
2892 root->fs_info->free_chunk_space += diff;
2893 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04002894 unlock_chunks(root);
2895 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002896 }
2897
Chris Balld6397ba2009-04-27 07:29:03 -04002898 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002899 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002900 if (IS_ERR(trans)) {
2901 ret = PTR_ERR(trans);
2902 goto done;
2903 }
2904
Chris Balld6397ba2009-04-27 07:29:03 -04002905 lock_chunks(root);
2906
2907 device->disk_total_bytes = new_size;
2908 /* Now btrfs_update_device() will change the on-disk size. */
2909 ret = btrfs_update_device(trans, device);
2910 if (ret) {
2911 unlock_chunks(root);
2912 btrfs_end_transaction(trans, root);
2913 goto done;
2914 }
2915 WARN_ON(diff > old_total);
2916 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2917 unlock_chunks(root);
2918 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002919done:
2920 btrfs_free_path(path);
2921 return ret;
2922}
2923
Christoph Hellwigb2950862008-12-02 09:54:17 -05002924static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002925 struct btrfs_root *root,
2926 struct btrfs_key *key,
2927 struct btrfs_chunk *chunk, int item_size)
2928{
David Sterba6c417612011-04-13 15:41:04 +02002929 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04002930 struct btrfs_disk_key disk_key;
2931 u32 array_size;
2932 u8 *ptr;
2933
2934 array_size = btrfs_super_sys_array_size(super_copy);
2935 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2936 return -EFBIG;
2937
2938 ptr = super_copy->sys_chunk_array + array_size;
2939 btrfs_cpu_key_to_disk(&disk_key, key);
2940 memcpy(ptr, &disk_key, sizeof(disk_key));
2941 ptr += sizeof(disk_key);
2942 memcpy(ptr, chunk, item_size);
2943 item_size += sizeof(disk_key);
2944 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2945 return 0;
2946}
2947
Miao Xieb2117a32011-01-05 10:07:28 +00002948/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002949 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002950 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002951static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002952{
Arne Jansen73c5de02011-04-12 12:07:57 +02002953 const struct btrfs_device_info *di_a = a;
2954 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002955
Arne Jansen73c5de02011-04-12 12:07:57 +02002956 if (di_a->max_avail > di_b->max_avail)
2957 return -1;
2958 if (di_a->max_avail < di_b->max_avail)
2959 return 1;
2960 if (di_a->total_avail > di_b->total_avail)
2961 return -1;
2962 if (di_a->total_avail < di_b->total_avail)
2963 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002964 return 0;
2965}
2966
2967static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2968 struct btrfs_root *extent_root,
2969 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002970 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002971 u64 start, u64 type)
2972{
2973 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002974 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2975 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002976 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002977 struct extent_map_tree *em_tree;
2978 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002979 struct btrfs_device_info *devices_info = NULL;
2980 u64 total_avail;
2981 int num_stripes; /* total number of stripes to allocate */
2982 int sub_stripes; /* sub_stripes info for map */
2983 int dev_stripes; /* stripes per dev */
2984 int devs_max; /* max devs to use */
2985 int devs_min; /* min devs needed */
2986 int devs_increment; /* ndevs has to be a multiple of this */
2987 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002988 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002989 u64 max_stripe_size;
2990 u64 max_chunk_size;
2991 u64 stripe_size;
2992 u64 num_bytes;
2993 int ndevs;
2994 int i;
2995 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002996
2997 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2998 (type & BTRFS_BLOCK_GROUP_DUP)) {
2999 WARN_ON(1);
3000 type &= ~BTRFS_BLOCK_GROUP_DUP;
3001 }
Arne Jansen73c5de02011-04-12 12:07:57 +02003002
Miao Xieb2117a32011-01-05 10:07:28 +00003003 if (list_empty(&fs_devices->alloc_list))
3004 return -ENOSPC;
3005
Arne Jansen73c5de02011-04-12 12:07:57 +02003006 sub_stripes = 1;
3007 dev_stripes = 1;
3008 devs_increment = 1;
3009 ncopies = 1;
3010 devs_max = 0; /* 0 == as many as possible */
3011 devs_min = 1;
3012
3013 /*
3014 * define the properties of each RAID type.
3015 * FIXME: move this to a global table and use it in all RAID
3016 * calculation code
3017 */
3018 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
3019 dev_stripes = 2;
3020 ncopies = 2;
3021 devs_max = 1;
3022 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
3023 devs_min = 2;
3024 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
3025 devs_increment = 2;
3026 ncopies = 2;
3027 devs_max = 2;
3028 devs_min = 2;
3029 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
3030 sub_stripes = 2;
3031 devs_increment = 2;
3032 ncopies = 2;
3033 devs_min = 4;
3034 } else {
3035 devs_max = 1;
3036 }
3037
3038 if (type & BTRFS_BLOCK_GROUP_DATA) {
3039 max_stripe_size = 1024 * 1024 * 1024;
3040 max_chunk_size = 10 * max_stripe_size;
3041 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
3042 max_stripe_size = 256 * 1024 * 1024;
3043 max_chunk_size = max_stripe_size;
3044 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
3045 max_stripe_size = 8 * 1024 * 1024;
3046 max_chunk_size = 2 * max_stripe_size;
3047 } else {
3048 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3049 type);
3050 BUG_ON(1);
3051 }
3052
3053 /* we don't want a chunk larger than 10% of writeable space */
3054 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3055 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003056
3057 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3058 GFP_NOFS);
3059 if (!devices_info)
3060 return -ENOMEM;
3061
Arne Jansen73c5de02011-04-12 12:07:57 +02003062 cur = fs_devices->alloc_list.next;
3063
3064 /*
3065 * in the first pass through the devices list, we gather information
3066 * about the available holes on each device.
3067 */
3068 ndevs = 0;
3069 while (cur != &fs_devices->alloc_list) {
3070 struct btrfs_device *device;
3071 u64 max_avail;
3072 u64 dev_offset;
3073
3074 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3075
3076 cur = cur->next;
3077
3078 if (!device->writeable) {
3079 printk(KERN_ERR
3080 "btrfs: read-only device in alloc_list\n");
3081 WARN_ON(1);
3082 continue;
3083 }
3084
3085 if (!device->in_fs_metadata)
3086 continue;
3087
3088 if (device->total_bytes > device->bytes_used)
3089 total_avail = device->total_bytes - device->bytes_used;
3090 else
3091 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003092
3093 /* If there is no space on this device, skip it. */
3094 if (total_avail == 0)
3095 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003096
3097 ret = find_free_dev_extent(trans, device,
3098 max_stripe_size * dev_stripes,
3099 &dev_offset, &max_avail);
3100 if (ret && ret != -ENOSPC)
3101 goto error;
3102
3103 if (ret == 0)
3104 max_avail = max_stripe_size * dev_stripes;
3105
3106 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3107 continue;
3108
3109 devices_info[ndevs].dev_offset = dev_offset;
3110 devices_info[ndevs].max_avail = max_avail;
3111 devices_info[ndevs].total_avail = total_avail;
3112 devices_info[ndevs].dev = device;
3113 ++ndevs;
3114 }
3115
3116 /*
3117 * now sort the devices by hole size / available space
3118 */
3119 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3120 btrfs_cmp_device_info, NULL);
3121
3122 /* round down to number of usable stripes */
3123 ndevs -= ndevs % devs_increment;
3124
3125 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3126 ret = -ENOSPC;
3127 goto error;
3128 }
3129
3130 if (devs_max && ndevs > devs_max)
3131 ndevs = devs_max;
3132 /*
3133 * the primary goal is to maximize the number of stripes, so use as many
3134 * devices as possible, even if the stripes are not maximum sized.
3135 */
3136 stripe_size = devices_info[ndevs-1].max_avail;
3137 num_stripes = ndevs * dev_stripes;
3138
3139 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
3140 stripe_size = max_chunk_size * ncopies;
3141 do_div(stripe_size, num_stripes);
3142 }
3143
3144 do_div(stripe_size, dev_stripes);
3145 do_div(stripe_size, BTRFS_STRIPE_LEN);
3146 stripe_size *= BTRFS_STRIPE_LEN;
3147
Miao Xieb2117a32011-01-05 10:07:28 +00003148 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3149 if (!map) {
3150 ret = -ENOMEM;
3151 goto error;
3152 }
3153 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003154
Arne Jansen73c5de02011-04-12 12:07:57 +02003155 for (i = 0; i < ndevs; ++i) {
3156 for (j = 0; j < dev_stripes; ++j) {
3157 int s = i * dev_stripes + j;
3158 map->stripes[s].dev = devices_info[i].dev;
3159 map->stripes[s].physical = devices_info[i].dev_offset +
3160 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003161 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003162 }
Chris Mason593060d2008-03-25 16:50:33 -04003163 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003164 map->stripe_len = BTRFS_STRIPE_LEN;
3165 map->io_align = BTRFS_STRIPE_LEN;
3166 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003167 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003168 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003169
Yan Zheng2b820322008-11-17 21:11:30 -05003170 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003171 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003172
Arne Jansen73c5de02011-04-12 12:07:57 +02003173 *stripe_size_out = stripe_size;
3174 *num_bytes_out = num_bytes;
3175
3176 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003177
David Sterba172ddd62011-04-21 00:48:27 +02003178 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003179 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003180 ret = -ENOMEM;
3181 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003182 }
Chris Mason0b86a832008-03-24 15:01:56 -04003183 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003184 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003185 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003186 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003187 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003188
Chris Mason0b86a832008-03-24 15:01:56 -04003189 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003190 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003191 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003192 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04003193 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003194 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05003195
3196 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3197 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003198 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05003199 BUG_ON(ret);
3200
Arne Jansen73c5de02011-04-12 12:07:57 +02003201 for (i = 0; i < map->num_stripes; ++i) {
3202 struct btrfs_device *device;
3203 u64 dev_offset;
3204
3205 device = map->stripes[i].dev;
3206 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003207
3208 ret = btrfs_alloc_dev_extent(trans, device,
3209 info->chunk_root->root_key.objectid,
3210 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003211 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003212 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003213 }
3214
Miao Xieb2117a32011-01-05 10:07:28 +00003215 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003216 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003217
3218error:
3219 kfree(map);
3220 kfree(devices_info);
3221 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003222}
3223
3224static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3225 struct btrfs_root *extent_root,
3226 struct map_lookup *map, u64 chunk_offset,
3227 u64 chunk_size, u64 stripe_size)
3228{
3229 u64 dev_offset;
3230 struct btrfs_key key;
3231 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3232 struct btrfs_device *device;
3233 struct btrfs_chunk *chunk;
3234 struct btrfs_stripe *stripe;
3235 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3236 int index = 0;
3237 int ret;
3238
3239 chunk = kzalloc(item_size, GFP_NOFS);
3240 if (!chunk)
3241 return -ENOMEM;
3242
3243 index = 0;
3244 while (index < map->num_stripes) {
3245 device = map->stripes[index].dev;
3246 device->bytes_used += stripe_size;
3247 ret = btrfs_update_device(trans, device);
3248 BUG_ON(ret);
3249 index++;
3250 }
3251
Josef Bacik2bf64752011-09-26 17:12:22 -04003252 spin_lock(&extent_root->fs_info->free_chunk_lock);
3253 extent_root->fs_info->free_chunk_space -= (stripe_size *
3254 map->num_stripes);
3255 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3256
Yan Zheng2b820322008-11-17 21:11:30 -05003257 index = 0;
3258 stripe = &chunk->stripe;
3259 while (index < map->num_stripes) {
3260 device = map->stripes[index].dev;
3261 dev_offset = map->stripes[index].physical;
3262
3263 btrfs_set_stack_stripe_devid(stripe, device->devid);
3264 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3265 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3266 stripe++;
3267 index++;
3268 }
3269
3270 btrfs_set_stack_chunk_length(chunk, chunk_size);
3271 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3272 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3273 btrfs_set_stack_chunk_type(chunk, map->type);
3274 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3275 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3276 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3277 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3278 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3279
3280 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3281 key.type = BTRFS_CHUNK_ITEM_KEY;
3282 key.offset = chunk_offset;
3283
3284 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
3285 BUG_ON(ret);
3286
3287 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3288 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
3289 item_size);
3290 BUG_ON(ret);
3291 }
liubo1abe9b82011-03-24 11:18:59 +00003292
Yan Zheng2b820322008-11-17 21:11:30 -05003293 kfree(chunk);
3294 return 0;
3295}
3296
3297/*
3298 * Chunk allocation falls into two parts. The first part does works
3299 * that make the new allocated chunk useable, but not do any operation
3300 * that modifies the chunk tree. The second part does the works that
3301 * require modifying the chunk tree. This division is important for the
3302 * bootstrap process of adding storage to a seed btrfs.
3303 */
3304int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3305 struct btrfs_root *extent_root, u64 type)
3306{
3307 u64 chunk_offset;
3308 u64 chunk_size;
3309 u64 stripe_size;
3310 struct map_lookup *map;
3311 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3312 int ret;
3313
3314 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3315 &chunk_offset);
3316 if (ret)
3317 return ret;
3318
3319 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3320 &stripe_size, chunk_offset, type);
3321 if (ret)
3322 return ret;
3323
3324 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3325 chunk_size, stripe_size);
3326 BUG_ON(ret);
3327 return 0;
3328}
3329
Chris Masond3977122009-01-05 21:25:51 -05003330static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003331 struct btrfs_root *root,
3332 struct btrfs_device *device)
3333{
3334 u64 chunk_offset;
3335 u64 sys_chunk_offset;
3336 u64 chunk_size;
3337 u64 sys_chunk_size;
3338 u64 stripe_size;
3339 u64 sys_stripe_size;
3340 u64 alloc_profile;
3341 struct map_lookup *map;
3342 struct map_lookup *sys_map;
3343 struct btrfs_fs_info *fs_info = root->fs_info;
3344 struct btrfs_root *extent_root = fs_info->extent_root;
3345 int ret;
3346
3347 ret = find_next_chunk(fs_info->chunk_root,
3348 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003349 if (ret)
3350 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003351
3352 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003353 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003354 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3355
3356 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3357 &stripe_size, chunk_offset, alloc_profile);
3358 BUG_ON(ret);
3359
3360 sys_chunk_offset = chunk_offset + chunk_size;
3361
3362 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003363 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003364 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3365
3366 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3367 &sys_chunk_size, &sys_stripe_size,
3368 sys_chunk_offset, alloc_profile);
3369 BUG_ON(ret);
3370
3371 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
3372 BUG_ON(ret);
3373
3374 /*
3375 * Modifying chunk tree needs allocating new blocks from both
3376 * system block group and metadata block group. So we only can
3377 * do operations require modifying the chunk tree after both
3378 * block groups were created.
3379 */
3380 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3381 chunk_size, stripe_size);
3382 BUG_ON(ret);
3383
3384 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3385 sys_chunk_offset, sys_chunk_size,
3386 sys_stripe_size);
3387 BUG_ON(ret);
3388 return 0;
3389}
3390
3391int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3392{
3393 struct extent_map *em;
3394 struct map_lookup *map;
3395 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3396 int readonly = 0;
3397 int i;
3398
Chris Mason890871b2009-09-02 16:24:52 -04003399 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003400 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003401 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003402 if (!em)
3403 return 1;
3404
Josef Bacikf48b9072010-01-27 02:07:59 +00003405 if (btrfs_test_opt(root, DEGRADED)) {
3406 free_extent_map(em);
3407 return 0;
3408 }
3409
Yan Zheng2b820322008-11-17 21:11:30 -05003410 map = (struct map_lookup *)em->bdev;
3411 for (i = 0; i < map->num_stripes; i++) {
3412 if (!map->stripes[i].dev->writeable) {
3413 readonly = 1;
3414 break;
3415 }
3416 }
3417 free_extent_map(em);
3418 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003419}
3420
3421void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3422{
David Sterbaa8067e02011-04-21 00:34:43 +02003423 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003424}
3425
3426void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3427{
3428 struct extent_map *em;
3429
Chris Masond3977122009-01-05 21:25:51 -05003430 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003431 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003432 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3433 if (em)
3434 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003435 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003436 if (!em)
3437 break;
3438 kfree(em->bdev);
3439 /* once for us */
3440 free_extent_map(em);
3441 /* once for the tree */
3442 free_extent_map(em);
3443 }
3444}
3445
Chris Masonf1885912008-04-09 16:28:12 -04003446int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
3447{
3448 struct extent_map *em;
3449 struct map_lookup *map;
3450 struct extent_map_tree *em_tree = &map_tree->map_tree;
3451 int ret;
3452
Chris Mason890871b2009-09-02 16:24:52 -04003453 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003454 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003455 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003456 BUG_ON(!em);
3457
3458 BUG_ON(em->start > logical || em->start + em->len < logical);
3459 map = (struct map_lookup *)em->bdev;
3460 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
3461 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003462 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3463 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003464 else
3465 ret = 1;
3466 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04003467 return ret;
3468}
3469
Chris Masondfe25022008-05-13 13:46:40 -04003470static int find_live_mirror(struct map_lookup *map, int first, int num,
3471 int optimal)
3472{
3473 int i;
3474 if (map->stripes[optimal].dev->bdev)
3475 return optimal;
3476 for (i = first; i < first + num; i++) {
3477 if (map->stripes[i].dev->bdev)
3478 return i;
3479 }
3480 /* we couldn't find one that doesn't fail. Just return something
3481 * and the io error handling code will clean up eventually
3482 */
3483 return optimal;
3484}
3485
Chris Masonf2d8d742008-04-21 10:03:05 -04003486static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3487 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003488 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003489 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04003490{
3491 struct extent_map *em;
3492 struct map_lookup *map;
3493 struct extent_map_tree *em_tree = &map_tree->map_tree;
3494 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04003495 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003496 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04003497 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003498 u64 stripe_nr_orig;
3499 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04003500 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04003501 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003502 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04003503 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04003504 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003505 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003506 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003507
Jan Schmidta1d3c472011-08-04 17:15:33 +02003508 if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04003509 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003510again:
Jan Schmidta1d3c472011-08-04 17:15:33 +02003511 if (bbio_ret) {
3512 bbio = kzalloc(btrfs_bio_size(stripes_allocated),
Chris Masoncea9e442008-04-09 16:28:12 -04003513 GFP_NOFS);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003514 if (!bbio)
Chris Masoncea9e442008-04-09 16:28:12 -04003515 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04003516
Jan Schmidta1d3c472011-08-04 17:15:33 +02003517 atomic_set(&bbio->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04003518 }
Chris Mason0b86a832008-03-24 15:01:56 -04003519
Chris Mason890871b2009-09-02 16:24:52 -04003520 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003521 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04003522 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04003523
Chris Mason3b951512008-04-17 11:29:12 -04003524 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05003525 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
3526 (unsigned long long)logical,
3527 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04003528 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04003529 }
Chris Mason0b86a832008-03-24 15:01:56 -04003530
3531 BUG_ON(em->start > logical || em->start + em->len < logical);
3532 map = (struct map_lookup *)em->bdev;
3533 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04003534
Chris Masonf1885912008-04-09 16:28:12 -04003535 if (mirror_num > map->num_stripes)
3536 mirror_num = 0;
3537
Jan Schmidta1d3c472011-08-04 17:15:33 +02003538 /* if our btrfs_bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003539 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04003540 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
3541 BTRFS_BLOCK_GROUP_DUP)) {
3542 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003543 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04003544 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3545 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003546 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04003547 }
3548 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003549 if (rw & REQ_DISCARD) {
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003550 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003551 stripes_required = map->num_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003552 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003553 if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04003554 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04003555 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04003556 free_extent_map(em);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003557 kfree(bbio);
Chris Masoncea9e442008-04-09 16:28:12 -04003558 goto again;
3559 }
Chris Mason593060d2008-03-25 16:50:33 -04003560 stripe_nr = offset;
3561 /*
3562 * stripe_nr counts the total number of stripes we have to stride
3563 * to get to this block
3564 */
3565 do_div(stripe_nr, map->stripe_len);
3566
3567 stripe_offset = stripe_nr * map->stripe_len;
3568 BUG_ON(offset < stripe_offset);
3569
3570 /* stripe_offset is the offset of this block in its stripe*/
3571 stripe_offset = offset - stripe_offset;
3572
Li Dongyangfce3bb92011-03-24 10:24:26 +00003573 if (rw & REQ_DISCARD)
3574 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003575 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04003576 /* we limit the length of each bio to what fits in a stripe */
3577 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003578 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003579 } else {
3580 *length = em->len - offset;
3581 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003582
Jan Schmidta1d3c472011-08-04 17:15:33 +02003583 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04003584 goto out;
3585
Chris Masonf2d8d742008-04-21 10:03:05 -04003586 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003587 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003588 stripe_nr_orig = stripe_nr;
3589 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3590 (~(map->stripe_len - 1));
3591 do_div(stripe_nr_end, map->stripe_len);
3592 stripe_end_offset = stripe_nr_end * map->stripe_len -
3593 (offset + *length);
3594 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3595 if (rw & REQ_DISCARD)
3596 num_stripes = min_t(u64, map->num_stripes,
3597 stripe_nr_end - stripe_nr_orig);
3598 stripe_index = do_div(stripe_nr, map->num_stripes);
3599 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003600 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003601 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003602 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003603 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003604 else {
3605 stripe_index = find_live_mirror(map, 0,
3606 map->num_stripes,
3607 current->pid % map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003608 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003609 }
Chris Mason2fff7342008-04-29 14:12:09 -04003610
Chris Mason611f0e02008-04-03 16:29:03 -04003611 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003612 if (rw & (REQ_WRITE | REQ_DISCARD)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003613 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003614 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04003615 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003616 } else {
3617 mirror_num = 1;
3618 }
Chris Mason2fff7342008-04-29 14:12:09 -04003619
Chris Mason321aecc2008-04-16 10:49:51 -04003620 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3621 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003622
3623 stripe_index = do_div(stripe_nr, factor);
3624 stripe_index *= map->sub_stripes;
3625
Jens Axboe7eaceac2011-03-10 08:52:07 +01003626 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003627 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003628 else if (rw & REQ_DISCARD)
3629 num_stripes = min_t(u64, map->sub_stripes *
3630 (stripe_nr_end - stripe_nr_orig),
3631 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003632 else if (mirror_num)
3633 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003634 else {
3635 stripe_index = find_live_mirror(map, stripe_index,
3636 map->sub_stripes, stripe_index +
3637 current->pid % map->sub_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003638 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04003639 }
Chris Mason8790d502008-04-03 16:29:03 -04003640 } else {
3641 /*
3642 * after this do_div call, stripe_nr is the number of stripes
3643 * on this device we have to walk to find the data, and
3644 * stripe_index is the number of our device in the stripe array
3645 */
3646 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003647 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04003648 }
Chris Mason593060d2008-03-25 16:50:33 -04003649 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003650
Li Dongyangfce3bb92011-03-24 10:24:26 +00003651 if (rw & REQ_DISCARD) {
3652 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003653 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04003654 map->stripes[stripe_index].physical +
3655 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003656 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003657
3658 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3659 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003660 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003661 int j;
3662
Chris Masond9d04872011-03-27 21:23:21 -04003663 div_u64_rem(stripe_nr_end - 1,
3664 map->num_stripes,
3665 &last_stripe);
3666
Li Dongyangfce3bb92011-03-24 10:24:26 +00003667 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003668 u32 test;
3669
3670 div_u64_rem(stripe_nr_end - 1 - j,
3671 map->num_stripes, &test);
3672 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003673 break;
3674 }
3675 stripes = stripe_nr_end - 1 - j;
3676 do_div(stripes, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003677 bbio->stripes[i].length = map->stripe_len *
Li Dongyangfce3bb92011-03-24 10:24:26 +00003678 (stripes - stripe_nr + 1);
3679
3680 if (i == 0) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003681 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003682 stripe_offset;
3683 stripe_offset = 0;
3684 }
3685 if (stripe_index == last_stripe)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003686 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003687 stripe_end_offset;
3688 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3689 u64 stripes;
3690 int j;
3691 int factor = map->num_stripes /
3692 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003693 u32 last_stripe = 0;
3694
3695 div_u64_rem(stripe_nr_end - 1,
3696 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003697 last_stripe *= map->sub_stripes;
3698
3699 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003700 u32 test;
3701
3702 div_u64_rem(stripe_nr_end - 1 - j,
3703 factor, &test);
3704
3705 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003706 stripe_index / map->sub_stripes)
3707 break;
3708 }
3709 stripes = stripe_nr_end - 1 - j;
3710 do_div(stripes, factor);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003711 bbio->stripes[i].length = map->stripe_len *
Li Dongyangfce3bb92011-03-24 10:24:26 +00003712 (stripes - stripe_nr + 1);
3713
3714 if (i < map->sub_stripes) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003715 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003716 stripe_offset;
3717 if (i == map->sub_stripes - 1)
3718 stripe_offset = 0;
3719 }
3720 if (stripe_index >= last_stripe &&
3721 stripe_index <= (last_stripe +
3722 map->sub_stripes - 1)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003723 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00003724 stripe_end_offset;
3725 }
3726 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02003727 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003728
3729 stripe_index++;
3730 if (stripe_index == map->num_stripes) {
3731 /* This could only happen for RAID0/10 */
3732 stripe_index = 0;
3733 stripe_nr++;
3734 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003735 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003736 } else {
3737 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003738 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003739 map->stripes[stripe_index].physical +
3740 stripe_offset +
3741 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003742 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07003743 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003744 stripe_index++;
3745 }
Chris Mason593060d2008-03-25 16:50:33 -04003746 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003747 if (bbio_ret) {
3748 *bbio_ret = bbio;
3749 bbio->num_stripes = num_stripes;
3750 bbio->max_errors = max_errors;
3751 bbio->mirror_num = mirror_num;
Chris Masonf2d8d742008-04-21 10:03:05 -04003752 }
Chris Masoncea9e442008-04-09 16:28:12 -04003753out:
Chris Mason0b86a832008-03-24 15:01:56 -04003754 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003755 return 0;
3756}
3757
Chris Masonf2d8d742008-04-21 10:03:05 -04003758int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3759 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02003760 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04003761{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003762 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003763 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003764}
3765
Yan Zhenga512bbf2008-12-08 16:46:26 -05003766int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3767 u64 chunk_start, u64 physical, u64 devid,
3768 u64 **logical, int *naddrs, int *stripe_len)
3769{
3770 struct extent_map_tree *em_tree = &map_tree->map_tree;
3771 struct extent_map *em;
3772 struct map_lookup *map;
3773 u64 *buf;
3774 u64 bytenr;
3775 u64 length;
3776 u64 stripe_nr;
3777 int i, j, nr = 0;
3778
Chris Mason890871b2009-09-02 16:24:52 -04003779 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003780 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003781 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003782
3783 BUG_ON(!em || em->start != chunk_start);
3784 map = (struct map_lookup *)em->bdev;
3785
3786 length = em->len;
3787 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3788 do_div(length, map->num_stripes / map->sub_stripes);
3789 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3790 do_div(length, map->num_stripes);
3791
3792 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3793 BUG_ON(!buf);
3794
3795 for (i = 0; i < map->num_stripes; i++) {
3796 if (devid && map->stripes[i].dev->devid != devid)
3797 continue;
3798 if (map->stripes[i].physical > physical ||
3799 map->stripes[i].physical + length <= physical)
3800 continue;
3801
3802 stripe_nr = physical - map->stripes[i].physical;
3803 do_div(stripe_nr, map->stripe_len);
3804
3805 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3806 stripe_nr = stripe_nr * map->num_stripes + i;
3807 do_div(stripe_nr, map->sub_stripes);
3808 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3809 stripe_nr = stripe_nr * map->num_stripes + i;
3810 }
3811 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003812 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003813 for (j = 0; j < nr; j++) {
3814 if (buf[j] == bytenr)
3815 break;
3816 }
Chris Mason934d3752008-12-08 16:43:10 -05003817 if (j == nr) {
3818 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003819 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003820 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003821 }
3822
Yan Zhenga512bbf2008-12-08 16:46:26 -05003823 *logical = buf;
3824 *naddrs = nr;
3825 *stripe_len = map->stripe_len;
3826
3827 free_extent_map(em);
3828 return 0;
3829}
3830
Jan Schmidta1d3c472011-08-04 17:15:33 +02003831static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003832{
Jan Schmidta1d3c472011-08-04 17:15:33 +02003833 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003834 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003835
Chris Mason8790d502008-04-03 16:29:03 -04003836 if (err)
Jan Schmidta1d3c472011-08-04 17:15:33 +02003837 atomic_inc(&bbio->error);
Chris Mason8790d502008-04-03 16:29:03 -04003838
Jan Schmidta1d3c472011-08-04 17:15:33 +02003839 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04003840 is_orig_bio = 1;
3841
Jan Schmidta1d3c472011-08-04 17:15:33 +02003842 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003843 if (!is_orig_bio) {
3844 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02003845 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003846 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003847 bio->bi_private = bbio->private;
3848 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02003849 bio->bi_bdev = (struct block_device *)
3850 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04003851 /* only send an error to the higher layers if it is
3852 * beyond the tolerance of the multi-bio
3853 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02003854 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003855 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05003856 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04003857 /*
3858 * this bio is actually up to date, we didn't
3859 * go over the max number of errors
3860 */
3861 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003862 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003863 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003864 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04003865
3866 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003867 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003868 bio_put(bio);
3869 }
Chris Mason8790d502008-04-03 16:29:03 -04003870}
3871
Chris Mason8b712842008-06-11 16:50:36 -04003872struct async_sched {
3873 struct bio *bio;
3874 int rw;
3875 struct btrfs_fs_info *info;
3876 struct btrfs_work work;
3877};
3878
3879/*
3880 * see run_scheduled_bios for a description of why bios are collected for
3881 * async submit.
3882 *
3883 * This will add one bio to the pending list for a device and make sure
3884 * the work struct is scheduled.
3885 */
Chris Masond3977122009-01-05 21:25:51 -05003886static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003887 struct btrfs_device *device,
3888 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003889{
3890 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003891 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003892
3893 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003894 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003895 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003896 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003897 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003898 return 0;
3899 }
3900
3901 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003902 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003903 * higher layers. Otherwise, the async bio makes it appear we have
3904 * made progress against dirty pages when we've really just put it
3905 * on a queue for later
3906 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003907 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003908 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003909 bio->bi_next = NULL;
3910 bio->bi_rw |= rw;
3911
3912 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003913 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003914 pending_bios = &device->pending_sync_bios;
3915 else
3916 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003917
Chris Masonffbd5172009-04-20 15:50:09 -04003918 if (pending_bios->tail)
3919 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003920
Chris Masonffbd5172009-04-20 15:50:09 -04003921 pending_bios->tail = bio;
3922 if (!pending_bios->head)
3923 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003924 if (device->running_pending)
3925 should_queue = 0;
3926
3927 spin_unlock(&device->io_lock);
3928
3929 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003930 btrfs_queue_worker(&root->fs_info->submit_workers,
3931 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003932 return 0;
3933}
3934
Chris Masonf1885912008-04-09 16:28:12 -04003935int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003936 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003937{
3938 struct btrfs_mapping_tree *map_tree;
3939 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003940 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003941 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003942 u64 length = 0;
3943 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04003944 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003945 int dev_nr = 0;
3946 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02003947 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003948
Chris Masonf2d8d742008-04-21 10:03:05 -04003949 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003950 map_tree = &root->fs_info->mapping_tree;
3951 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003952
Jan Schmidta1d3c472011-08-04 17:15:33 +02003953 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04003954 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003955 BUG_ON(ret);
3956
Jan Schmidta1d3c472011-08-04 17:15:33 +02003957 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04003958 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003959 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3960 "len %llu\n", (unsigned long long)logical,
3961 (unsigned long long)length,
3962 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003963 BUG();
3964 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003965
3966 bbio->orig_bio = first_bio;
3967 bbio->private = first_bio->bi_private;
3968 bbio->end_io = first_bio->bi_end_io;
3969 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04003970
Chris Masond3977122009-01-05 21:25:51 -05003971 while (dev_nr < total_devs) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003972 if (dev_nr < total_devs - 1) {
3973 bio = bio_clone(first_bio, GFP_NOFS);
3974 BUG_ON(!bio);
3975 } else {
3976 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04003977 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02003978 bio->bi_private = bbio;
3979 bio->bi_end_io = btrfs_end_bio;
3980 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
3981 dev = bbio->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003982 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02003983 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
3984 "(%s id %llu), size=%u\n", rw,
3985 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
3986 dev->name, dev->devid, bio->bi_size);
Chris Masondfe25022008-05-13 13:46:40 -04003987 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003988 if (async_submit)
3989 schedule_bio(root, dev, rw, bio);
3990 else
3991 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003992 } else {
3993 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3994 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003995 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003996 }
Chris Mason8790d502008-04-03 16:29:03 -04003997 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003998 }
Chris Mason0b86a832008-03-24 15:01:56 -04003999 return 0;
4000}
4001
Chris Masona4437552008-04-18 10:29:38 -04004002struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004003 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004004{
Yan Zheng2b820322008-11-17 21:11:30 -05004005 struct btrfs_device *device;
4006 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004007
Yan Zheng2b820322008-11-17 21:11:30 -05004008 cur_devices = root->fs_info->fs_devices;
4009 while (cur_devices) {
4010 if (!fsid ||
4011 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4012 device = __find_device(&cur_devices->devices,
4013 devid, uuid);
4014 if (device)
4015 return device;
4016 }
4017 cur_devices = cur_devices->seed;
4018 }
4019 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004020}
4021
Chris Masondfe25022008-05-13 13:46:40 -04004022static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4023 u64 devid, u8 *dev_uuid)
4024{
4025 struct btrfs_device *device;
4026 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4027
4028 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004029 if (!device)
4030 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004031 list_add(&device->dev_list,
4032 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004033 device->dev_root = root->fs_info->dev_root;
4034 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004035 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004036 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004037 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004038 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004039 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004040 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004041 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004042 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4043 return device;
4044}
4045
Chris Mason0b86a832008-03-24 15:01:56 -04004046static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4047 struct extent_buffer *leaf,
4048 struct btrfs_chunk *chunk)
4049{
4050 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4051 struct map_lookup *map;
4052 struct extent_map *em;
4053 u64 logical;
4054 u64 length;
4055 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004056 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004057 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004058 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004059 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004060
Chris Masone17cade2008-04-15 15:41:47 -04004061 logical = key->offset;
4062 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004063
Chris Mason890871b2009-09-02 16:24:52 -04004064 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004065 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004066 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004067
4068 /* already mapped? */
4069 if (em && em->start <= logical && em->start + em->len > logical) {
4070 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004071 return 0;
4072 } else if (em) {
4073 free_extent_map(em);
4074 }
Chris Mason0b86a832008-03-24 15:01:56 -04004075
David Sterba172ddd62011-04-21 00:48:27 +02004076 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004077 if (!em)
4078 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004079 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4080 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004081 if (!map) {
4082 free_extent_map(em);
4083 return -ENOMEM;
4084 }
4085
4086 em->bdev = (struct block_device *)map;
4087 em->start = logical;
4088 em->len = length;
4089 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004090 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004091
Chris Mason593060d2008-03-25 16:50:33 -04004092 map->num_stripes = num_stripes;
4093 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4094 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4095 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4096 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4097 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004098 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004099 for (i = 0; i < num_stripes; i++) {
4100 map->stripes[i].physical =
4101 btrfs_stripe_offset_nr(leaf, chunk, i);
4102 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004103 read_extent_buffer(leaf, uuid, (unsigned long)
4104 btrfs_stripe_dev_uuid_nr(chunk, i),
4105 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004106 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
4107 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004108 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004109 kfree(map);
4110 free_extent_map(em);
4111 return -EIO;
4112 }
Chris Masondfe25022008-05-13 13:46:40 -04004113 if (!map->stripes[i].dev) {
4114 map->stripes[i].dev =
4115 add_missing_dev(root, devid, uuid);
4116 if (!map->stripes[i].dev) {
4117 kfree(map);
4118 free_extent_map(em);
4119 return -EIO;
4120 }
4121 }
4122 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04004123 }
4124
Chris Mason890871b2009-09-02 16:24:52 -04004125 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004126 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004127 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04004128 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04004129 free_extent_map(em);
4130
4131 return 0;
4132}
4133
4134static int fill_device_from_item(struct extent_buffer *leaf,
4135 struct btrfs_dev_item *dev_item,
4136 struct btrfs_device *device)
4137{
4138 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04004139
4140 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04004141 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
4142 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004143 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
4144 device->type = btrfs_device_type(leaf, dev_item);
4145 device->io_align = btrfs_device_io_align(leaf, dev_item);
4146 device->io_width = btrfs_device_io_width(leaf, dev_item);
4147 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04004148
4149 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04004150 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004151
Chris Mason0b86a832008-03-24 15:01:56 -04004152 return 0;
4153}
4154
Yan Zheng2b820322008-11-17 21:11:30 -05004155static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
4156{
4157 struct btrfs_fs_devices *fs_devices;
4158 int ret;
4159
4160 mutex_lock(&uuid_mutex);
4161
4162 fs_devices = root->fs_info->fs_devices->seed;
4163 while (fs_devices) {
4164 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4165 ret = 0;
4166 goto out;
4167 }
4168 fs_devices = fs_devices->seed;
4169 }
4170
4171 fs_devices = find_fsid(fsid);
4172 if (!fs_devices) {
4173 ret = -ENOENT;
4174 goto out;
4175 }
Yan Zhenge4404d62008-12-12 10:03:26 -05004176
4177 fs_devices = clone_fs_devices(fs_devices);
4178 if (IS_ERR(fs_devices)) {
4179 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004180 goto out;
4181 }
4182
Christoph Hellwig97288f22008-12-02 06:36:09 -05004183 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05004184 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05004185 if (ret)
4186 goto out;
4187
4188 if (!fs_devices->seeding) {
4189 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05004190 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05004191 ret = -EINVAL;
4192 goto out;
4193 }
4194
4195 fs_devices->seed = root->fs_info->fs_devices->seed;
4196 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004197out:
4198 mutex_unlock(&uuid_mutex);
4199 return ret;
4200}
4201
Chris Mason0d81ba52008-03-24 15:02:07 -04004202static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004203 struct extent_buffer *leaf,
4204 struct btrfs_dev_item *dev_item)
4205{
4206 struct btrfs_device *device;
4207 u64 devid;
4208 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004209 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04004210 u8 dev_uuid[BTRFS_UUID_SIZE];
4211
Chris Mason0b86a832008-03-24 15:01:56 -04004212 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04004213 read_extent_buffer(leaf, dev_uuid,
4214 (unsigned long)btrfs_device_uuid(dev_item),
4215 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05004216 read_extent_buffer(leaf, fs_uuid,
4217 (unsigned long)btrfs_device_fsid(dev_item),
4218 BTRFS_UUID_SIZE);
4219
4220 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
4221 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05004222 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004223 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004224 }
4225
4226 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
4227 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05004228 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05004229 return -EIO;
4230
4231 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05004232 printk(KERN_WARNING "warning devid %llu missing\n",
4233 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05004234 device = add_missing_dev(root, devid, dev_uuid);
4235 if (!device)
4236 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05004237 } else if (!device->missing) {
4238 /*
4239 * this happens when a device that was properly setup
4240 * in the device info lists suddenly goes bad.
4241 * device->bdev is NULL, and so we have to set
4242 * device->missing to one here
4243 */
4244 root->fs_info->fs_devices->missing_devices++;
4245 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05004246 }
4247 }
4248
4249 if (device->fs_devices != root->fs_info->fs_devices) {
4250 BUG_ON(device->writeable);
4251 if (device->generation !=
4252 btrfs_device_generation(leaf, dev_item))
4253 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04004254 }
Chris Mason0b86a832008-03-24 15:01:56 -04004255
4256 fill_device_from_item(leaf, dev_item, device);
4257 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04004258 device->in_fs_metadata = 1;
Josef Bacik2bf64752011-09-26 17:12:22 -04004259 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004260 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04004261 spin_lock(&root->fs_info->free_chunk_lock);
4262 root->fs_info->free_chunk_space += device->total_bytes -
4263 device->bytes_used;
4264 spin_unlock(&root->fs_info->free_chunk_lock);
4265 }
Chris Mason0b86a832008-03-24 15:01:56 -04004266 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004267 return ret;
4268}
4269
Yan Zhenge4404d62008-12-12 10:03:26 -05004270int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04004271{
David Sterba6c417612011-04-13 15:41:04 +02004272 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04004273 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04004274 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04004275 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04004276 u8 *ptr;
4277 unsigned long sb_ptr;
4278 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004279 u32 num_stripes;
4280 u32 array_size;
4281 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004282 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04004283 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04004284
Yan Zhenge4404d62008-12-12 10:03:26 -05004285 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04004286 BTRFS_SUPER_INFO_SIZE);
4287 if (!sb)
4288 return -ENOMEM;
4289 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04004290 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
Chris Mason4008c042009-02-12 14:09:45 -05004291
Chris Masona061fc82008-05-07 11:43:44 -04004292 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04004293 array_size = btrfs_super_sys_array_size(super_copy);
4294
Chris Mason0b86a832008-03-24 15:01:56 -04004295 ptr = super_copy->sys_chunk_array;
4296 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
4297 cur = 0;
4298
4299 while (cur < array_size) {
4300 disk_key = (struct btrfs_disk_key *)ptr;
4301 btrfs_disk_key_to_cpu(&key, disk_key);
4302
Chris Masona061fc82008-05-07 11:43:44 -04004303 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04004304 sb_ptr += len;
4305 cur += len;
4306
Chris Mason0d81ba52008-03-24 15:02:07 -04004307 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04004308 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04004309 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04004310 if (ret)
4311 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004312 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
4313 len = btrfs_chunk_item_size(num_stripes);
4314 } else {
Chris Mason84eed902008-04-25 09:04:37 -04004315 ret = -EIO;
4316 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004317 }
4318 ptr += len;
4319 sb_ptr += len;
4320 cur += len;
4321 }
Chris Masona061fc82008-05-07 11:43:44 -04004322 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04004323 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004324}
4325
4326int btrfs_read_chunk_tree(struct btrfs_root *root)
4327{
4328 struct btrfs_path *path;
4329 struct extent_buffer *leaf;
4330 struct btrfs_key key;
4331 struct btrfs_key found_key;
4332 int ret;
4333 int slot;
4334
4335 root = root->fs_info->chunk_root;
4336
4337 path = btrfs_alloc_path();
4338 if (!path)
4339 return -ENOMEM;
4340
4341 /* first we search for all of the device items, and then we
4342 * read in all of the chunk items. This way we can create chunk
4343 * mappings that reference all of the devices that are afound
4344 */
4345 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
4346 key.offset = 0;
4347 key.type = 0;
4348again:
4349 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00004350 if (ret < 0)
4351 goto error;
Chris Masond3977122009-01-05 21:25:51 -05004352 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004353 leaf = path->nodes[0];
4354 slot = path->slots[0];
4355 if (slot >= btrfs_header_nritems(leaf)) {
4356 ret = btrfs_next_leaf(root, path);
4357 if (ret == 0)
4358 continue;
4359 if (ret < 0)
4360 goto error;
4361 break;
4362 }
4363 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4364 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4365 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
4366 break;
4367 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
4368 struct btrfs_dev_item *dev_item;
4369 dev_item = btrfs_item_ptr(leaf, slot,
4370 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04004371 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05004372 if (ret)
4373 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004374 }
4375 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
4376 struct btrfs_chunk *chunk;
4377 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4378 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05004379 if (ret)
4380 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04004381 }
4382 path->slots[0]++;
4383 }
4384 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
4385 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004386 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004387 goto again;
4388 }
Chris Mason0b86a832008-03-24 15:01:56 -04004389 ret = 0;
4390error:
Yan Zheng2b820322008-11-17 21:11:30 -05004391 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004392 return ret;
4393}