blob: c50a85e0d08f10eae70ed907c5c70c764bbd3810 [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>
Chris Mason593060d2008-03-25 16:50:33 -040025#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050026#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040027#include "ctree.h"
28#include "extent_map.h"
29#include "disk-io.h"
30#include "transaction.h"
31#include "print-tree.h"
32#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040033#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040034
Chris Mason593060d2008-03-25 16:50:33 -040035struct map_lookup {
36 u64 type;
37 int io_align;
38 int io_width;
39 int stripe_len;
40 int sector_size;
41 int num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -040042 int sub_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -040043 struct btrfs_bio_stripe stripes[];
Chris Mason593060d2008-03-25 16:50:33 -040044};
45
Yan Zheng2b820322008-11-17 21:11:30 -050046static int init_first_rw_device(struct btrfs_trans_handle *trans,
47 struct btrfs_root *root,
48 struct btrfs_device *device);
49static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
50
Chris Mason593060d2008-03-25 16:50:33 -040051#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040052 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040053
Chris Mason8a4b83c2008-03-24 15:02:07 -040054static DEFINE_MUTEX(uuid_mutex);
55static LIST_HEAD(fs_uuids);
56
Chris Masona061fc82008-05-07 11:43:44 -040057void btrfs_lock_volumes(void)
58{
59 mutex_lock(&uuid_mutex);
60}
61
62void btrfs_unlock_volumes(void)
63{
64 mutex_unlock(&uuid_mutex);
65}
66
Chris Mason7d9eb122008-07-08 14:19:17 -040067static void lock_chunks(struct btrfs_root *root)
68{
Chris Mason7d9eb122008-07-08 14:19:17 -040069 mutex_lock(&root->fs_info->chunk_mutex);
70}
71
72static void unlock_chunks(struct btrfs_root *root)
73{
Chris Mason7d9eb122008-07-08 14:19:17 -040074 mutex_unlock(&root->fs_info->chunk_mutex);
75}
76
Yan Zhenge4404d62008-12-12 10:03:26 -050077static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
78{
79 struct btrfs_device *device;
80 WARN_ON(fs_devices->opened);
81 while (!list_empty(&fs_devices->devices)) {
82 device = list_entry(fs_devices->devices.next,
83 struct btrfs_device, dev_list);
84 list_del(&device->dev_list);
85 kfree(device->name);
86 kfree(device);
87 }
88 kfree(fs_devices);
89}
90
Chris Mason8a4b83c2008-03-24 15:02:07 -040091int btrfs_cleanup_fs_uuids(void)
92{
93 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040094
Yan Zheng2b820322008-11-17 21:11:30 -050095 while (!list_empty(&fs_uuids)) {
96 fs_devices = list_entry(fs_uuids.next,
97 struct btrfs_fs_devices, list);
98 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050099 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400100 }
101 return 0;
102}
103
Chris Masona1b32a52008-09-05 16:09:51 -0400104static noinline struct btrfs_device *__find_device(struct list_head *head,
105 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400106{
107 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400108
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500109 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400110 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400111 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400112 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400113 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400114 }
115 return NULL;
116}
117
Chris Masona1b32a52008-09-05 16:09:51 -0400118static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400119{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400120 struct btrfs_fs_devices *fs_devices;
121
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500122 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400123 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
124 return fs_devices;
125 }
126 return NULL;
127}
128
Chris Masonffbd5172009-04-20 15:50:09 -0400129static void requeue_list(struct btrfs_pending_bios *pending_bios,
130 struct bio *head, struct bio *tail)
131{
132
133 struct bio *old_head;
134
135 old_head = pending_bios->head;
136 pending_bios->head = head;
137 if (pending_bios->tail)
138 tail->bi_next = old_head;
139 else
140 pending_bios->tail = tail;
141}
142
Chris Mason8b712842008-06-11 16:50:36 -0400143/*
144 * we try to collect pending bios for a device so we don't get a large
145 * number of procs sending bios down to the same device. This greatly
146 * improves the schedulers ability to collect and merge the bios.
147 *
148 * But, it also turns into a long list of bios to process and that is sure
149 * to eventually make the worker thread block. The solution here is to
150 * make some progress and then put this work struct back at the end of
151 * the list if the block device is congested. This way, multiple devices
152 * can make progress from a single worker thread.
153 */
Chris Masond3977122009-01-05 21:25:51 -0500154static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400155{
156 struct bio *pending;
157 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400158 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400159 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400160 struct bio *tail;
161 struct bio *cur;
162 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400163 unsigned long num_run;
164 unsigned long num_sync_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400165 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400166 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400167 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400168 int force_reg = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400169
Chris Masonbedf7622009-04-03 10:32:58 -0400170 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400171 fs_info = device->dev_root->fs_info;
172 limit = btrfs_async_submit_limit(fs_info);
173 limit = limit * 2 / 3;
174
Chris Masonffbd5172009-04-20 15:50:09 -0400175 /* we want to make sure that every time we switch from the sync
176 * list to the normal list, we unplug
177 */
178 num_sync_run = 0;
179
Chris Mason8b712842008-06-11 16:50:36 -0400180loop:
181 spin_lock(&device->io_lock);
182
Chris Masona6837052009-02-04 09:19:41 -0500183loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400184 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400185
Chris Mason8b712842008-06-11 16:50:36 -0400186 /* take all the bios off the list at once and process them
187 * later on (without the lock held). But, remember the
188 * tail and other pointers so the bios can be properly reinserted
189 * into the list if we hit congestion
190 */
Chris Masond84275c2009-06-09 15:39:08 -0400191 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400192 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400193 force_reg = 1;
194 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400195 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400196 force_reg = 0;
197 }
Chris Masonffbd5172009-04-20 15:50:09 -0400198
199 pending = pending_bios->head;
200 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400201 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400202
203 /*
204 * if pending was null this time around, no bios need processing
205 * at all and we can stop. Otherwise it'll loop back up again
206 * and do an additional check so no bios are missed.
207 *
208 * device->running_pending is used to synchronize with the
209 * schedule_bio code.
210 */
Chris Masonffbd5172009-04-20 15:50:09 -0400211 if (device->pending_sync_bios.head == NULL &&
212 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400213 again = 0;
214 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400215 } else {
216 again = 1;
217 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400218 }
Chris Masonffbd5172009-04-20 15:50:09 -0400219
220 pending_bios->head = NULL;
221 pending_bios->tail = NULL;
222
Chris Mason8b712842008-06-11 16:50:36 -0400223 spin_unlock(&device->io_lock);
224
Chris Masonffbd5172009-04-20 15:50:09 -0400225 /*
226 * if we're doing the regular priority list, make sure we unplug
227 * for any high prio bios we've sent down
228 */
229 if (pending_bios == &device->pending_bios && num_sync_run > 0) {
230 num_sync_run = 0;
231 blk_run_backing_dev(bdi, NULL);
232 }
233
Chris Masond3977122009-01-05 21:25:51 -0500234 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400235
236 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400237 /* we want to work on both lists, but do more bios on the
238 * sync list than the regular list
239 */
240 if ((num_run > 32 &&
241 pending_bios != &device->pending_sync_bios &&
242 device->pending_sync_bios.head) ||
243 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
244 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400245 spin_lock(&device->io_lock);
246 requeue_list(pending_bios, pending, tail);
247 goto loop_lock;
248 }
249
Chris Mason8b712842008-06-11 16:50:36 -0400250 cur = pending;
251 pending = pending->bi_next;
252 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400253 atomic_dec(&fs_info->nr_async_bios);
254
255 if (atomic_read(&fs_info->nr_async_bios) < limit &&
256 waitqueue_active(&fs_info->async_submit_wait))
257 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6d2008-07-31 16:29:02 -0400258
259 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400260
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200261 if (cur->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -0400262 num_sync_run++;
263
Chris Mason5ff7ba32010-03-15 10:21:30 -0400264 submit_bio(cur->bi_rw, cur);
265 num_run++;
266 batch_run++;
Chris Masonffbd5172009-04-20 15:50:09 -0400267 if (need_resched()) {
268 if (num_sync_run) {
269 blk_run_backing_dev(bdi, NULL);
270 num_sync_run = 0;
271 }
272 cond_resched();
273 }
Chris Mason8b712842008-06-11 16:50:36 -0400274
275 /*
276 * we made progress, there is more work to do and the bdi
277 * is now congested. Back off and let other work structs
278 * run instead
279 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400280 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500281 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400282 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400283
Chris Masonb765ead2009-04-03 10:27:10 -0400284 ioc = current->io_context;
285
286 /*
287 * the main goal here is that we don't want to
288 * block if we're going to be able to submit
289 * more requests without blocking.
290 *
291 * This code does two great things, it pokes into
292 * the elevator code from a filesystem _and_
293 * it makes assumptions about how batching works.
294 */
295 if (ioc && ioc->nr_batch_requests > 0 &&
296 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
297 (last_waited == 0 ||
298 ioc->last_waited == last_waited)) {
299 /*
300 * we want to go through our batch of
301 * requests and stop. So, we copy out
302 * the ioc->last_waited time and test
303 * against it before looping
304 */
305 last_waited = ioc->last_waited;
Chris Masonffbd5172009-04-20 15:50:09 -0400306 if (need_resched()) {
307 if (num_sync_run) {
308 blk_run_backing_dev(bdi, NULL);
309 num_sync_run = 0;
310 }
311 cond_resched();
312 }
Chris Masonb765ead2009-04-03 10:27:10 -0400313 continue;
314 }
Chris Mason8b712842008-06-11 16:50:36 -0400315 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400316 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500317 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400318
319 spin_unlock(&device->io_lock);
320 btrfs_requeue_work(&device->work);
321 goto done;
322 }
323 }
Chris Masonffbd5172009-04-20 15:50:09 -0400324
325 if (num_sync_run) {
326 num_sync_run = 0;
327 blk_run_backing_dev(bdi, NULL);
328 }
Chris Masonbedf7622009-04-03 10:32:58 -0400329 /*
330 * IO has already been through a long path to get here. Checksumming,
331 * async helper threads, perhaps compression. We've done a pretty
332 * good job of collecting a batch of IO and should just unplug
333 * the device right away.
334 *
335 * This will help anyone who is waiting on the IO, they might have
336 * already unplugged, but managed to do so before the bio they
337 * cared about found its way down here.
338 */
339 blk_run_backing_dev(bdi, NULL);
Chris Mason51684082010-03-10 15:33:32 -0500340
341 cond_resched();
342 if (again)
343 goto loop;
344
345 spin_lock(&device->io_lock);
346 if (device->pending_bios.head || device->pending_sync_bios.head)
347 goto loop_lock;
348 spin_unlock(&device->io_lock);
349
Chris Mason8b712842008-06-11 16:50:36 -0400350done:
351 return 0;
352}
353
Christoph Hellwigb2950862008-12-02 09:54:17 -0500354static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400355{
356 struct btrfs_device *device;
357
358 device = container_of(work, struct btrfs_device, work);
359 run_scheduled_bios(device);
360}
361
Chris Masona1b32a52008-09-05 16:09:51 -0400362static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400363 struct btrfs_super_block *disk_super,
364 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
365{
366 struct btrfs_device *device;
367 struct btrfs_fs_devices *fs_devices;
368 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000369 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370
371 fs_devices = find_fsid(disk_super->fsid);
372 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400373 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400374 if (!fs_devices)
375 return -ENOMEM;
376 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400377 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400378 list_add(&fs_devices->list, &fs_uuids);
379 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
380 fs_devices->latest_devid = devid;
381 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400382 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400383 device = NULL;
384 } else {
Chris Masona4437552008-04-18 10:29:38 -0400385 device = __find_device(&fs_devices->devices, devid,
386 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400387 }
388 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500389 if (fs_devices->opened)
390 return -EBUSY;
391
Chris Mason8a4b83c2008-03-24 15:02:07 -0400392 device = kzalloc(sizeof(*device), GFP_NOFS);
393 if (!device) {
394 /* we can safely leave the fs_devices entry around */
395 return -ENOMEM;
396 }
397 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400398 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400399 memcpy(device->uuid, disk_super->dev_item.uuid,
400 BTRFS_UUID_SIZE);
Chris Masonf2984462008-04-10 16:19:33 -0400401 device->barriers = 1;
Chris Masonb248a412008-04-14 09:48:18 -0400402 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400403 device->name = kstrdup(path, GFP_NOFS);
404 if (!device->name) {
405 kfree(device);
406 return -ENOMEM;
407 }
Yan Zheng2b820322008-11-17 21:11:30 -0500408 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400409
410 mutex_lock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400411 list_add(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400412 mutex_unlock(&fs_devices->device_list_mutex);
413
Yan Zheng2b820322008-11-17 21:11:30 -0500414 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400415 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500416 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000417 name = kstrdup(path, GFP_NOFS);
418 if (!name)
419 return -ENOMEM;
420 kfree(device->name);
421 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500422 if (device->missing) {
423 fs_devices->missing_devices--;
424 device->missing = 0;
425 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400426 }
427
428 if (found_transid > fs_devices->latest_trans) {
429 fs_devices->latest_devid = devid;
430 fs_devices->latest_trans = found_transid;
431 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400432 *fs_devices_ret = fs_devices;
433 return 0;
434}
435
Yan Zhenge4404d62008-12-12 10:03:26 -0500436static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
437{
438 struct btrfs_fs_devices *fs_devices;
439 struct btrfs_device *device;
440 struct btrfs_device *orig_dev;
441
442 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
443 if (!fs_devices)
444 return ERR_PTR(-ENOMEM);
445
446 INIT_LIST_HEAD(&fs_devices->devices);
447 INIT_LIST_HEAD(&fs_devices->alloc_list);
448 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400449 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500450 fs_devices->latest_devid = orig->latest_devid;
451 fs_devices->latest_trans = orig->latest_trans;
452 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
453
Chris Masone5e9a522009-06-10 15:17:02 -0400454 mutex_lock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500455 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
456 device = kzalloc(sizeof(*device), GFP_NOFS);
457 if (!device)
458 goto error;
459
460 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400461 if (!device->name) {
462 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500463 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400464 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500465
466 device->devid = orig_dev->devid;
467 device->work.func = pending_bios_fn;
468 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
469 device->barriers = 1;
470 spin_lock_init(&device->io_lock);
471 INIT_LIST_HEAD(&device->dev_list);
472 INIT_LIST_HEAD(&device->dev_alloc_list);
473
474 list_add(&device->dev_list, &fs_devices->devices);
475 device->fs_devices = fs_devices;
476 fs_devices->num_devices++;
477 }
Chris Masone5e9a522009-06-10 15:17:02 -0400478 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500479 return fs_devices;
480error:
Chris Masone5e9a522009-06-10 15:17:02 -0400481 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500482 free_fs_devices(fs_devices);
483 return ERR_PTR(-ENOMEM);
484}
485
Chris Masondfe25022008-05-13 13:46:40 -0400486int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
487{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500488 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400489
490 mutex_lock(&uuid_mutex);
491again:
Chris Masone5e9a522009-06-10 15:17:02 -0400492 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500493 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500494 if (device->in_fs_metadata)
495 continue;
496
497 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500498 close_bdev_exclusive(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500499 device->bdev = NULL;
500 fs_devices->open_devices--;
501 }
502 if (device->writeable) {
503 list_del_init(&device->dev_alloc_list);
504 device->writeable = 0;
505 fs_devices->rw_devices--;
506 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500507 list_del_init(&device->dev_list);
508 fs_devices->num_devices--;
509 kfree(device->name);
510 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400511 }
Chris Masone5e9a522009-06-10 15:17:02 -0400512 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -0500513
514 if (fs_devices->seed) {
515 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500516 goto again;
517 }
518
Chris Masondfe25022008-05-13 13:46:40 -0400519 mutex_unlock(&uuid_mutex);
520 return 0;
521}
Chris Masona0af4692008-05-13 16:03:06 -0400522
Yan Zheng2b820322008-11-17 21:11:30 -0500523static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400524{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400525 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500526
Yan Zheng2b820322008-11-17 21:11:30 -0500527 if (--fs_devices->opened > 0)
528 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400529
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500530 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400531 if (device->bdev) {
Chris Mason15916de2008-11-19 21:17:22 -0500532 close_bdev_exclusive(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400533 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400534 }
Yan Zheng2b820322008-11-17 21:11:30 -0500535 if (device->writeable) {
536 list_del_init(&device->dev_alloc_list);
537 fs_devices->rw_devices--;
538 }
539
Chris Mason8a4b83c2008-03-24 15:02:07 -0400540 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500541 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400542 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400543 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500544 WARN_ON(fs_devices->open_devices);
545 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500546 fs_devices->opened = 0;
547 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500548
Chris Mason8a4b83c2008-03-24 15:02:07 -0400549 return 0;
550}
551
Yan Zheng2b820322008-11-17 21:11:30 -0500552int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
553{
Yan Zhenge4404d62008-12-12 10:03:26 -0500554 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500555 int ret;
556
557 mutex_lock(&uuid_mutex);
558 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500559 if (!fs_devices->opened) {
560 seed_devices = fs_devices->seed;
561 fs_devices->seed = NULL;
562 }
Yan Zheng2b820322008-11-17 21:11:30 -0500563 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500564
565 while (seed_devices) {
566 fs_devices = seed_devices;
567 seed_devices = fs_devices->seed;
568 __btrfs_close_devices(fs_devices);
569 free_fs_devices(fs_devices);
570 }
Yan Zheng2b820322008-11-17 21:11:30 -0500571 return ret;
572}
573
Yan Zhenge4404d62008-12-12 10:03:26 -0500574static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
575 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400576{
577 struct block_device *bdev;
578 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400579 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400580 struct block_device *latest_bdev = NULL;
581 struct buffer_head *bh;
582 struct btrfs_super_block *disk_super;
583 u64 latest_devid = 0;
584 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400585 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500586 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400587 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400588
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500589 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400590 if (device->bdev)
591 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400592 if (!device->name)
593 continue;
594
Chris Mason15916de2008-11-19 21:17:22 -0500595 bdev = open_bdev_exclusive(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400596 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500597 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400598 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400599 }
Chris Masona061fc82008-05-07 11:43:44 -0400600 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400601
Yan Zhenga512bbf2008-12-08 16:46:26 -0500602 bh = btrfs_read_dev_super(bdev);
Chris Masona0af4692008-05-13 16:03:06 -0400603 if (!bh)
604 goto error_close;
605
606 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000607 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400608 if (devid != device->devid)
609 goto error_brelse;
610
Yan Zheng2b820322008-11-17 21:11:30 -0500611 if (memcmp(device->uuid, disk_super->dev_item.uuid,
612 BTRFS_UUID_SIZE))
613 goto error_brelse;
614
615 device->generation = btrfs_super_generation(disk_super);
616 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400617 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500618 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400619 latest_bdev = bdev;
620 }
621
Yan Zheng2b820322008-11-17 21:11:30 -0500622 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
623 device->writeable = 0;
624 } else {
625 device->writeable = !bdev_read_only(bdev);
626 seeding = 0;
627 }
628
Chris Mason8a4b83c2008-03-24 15:02:07 -0400629 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400630 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500631 device->mode = flags;
632
Chris Masonc2898112009-06-10 09:51:32 -0400633 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
634 fs_devices->rotating = 1;
635
Chris Masona0af4692008-05-13 16:03:06 -0400636 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500637 if (device->writeable) {
638 fs_devices->rw_devices++;
639 list_add(&device->dev_alloc_list,
640 &fs_devices->alloc_list);
641 }
Chris Masona0af4692008-05-13 16:03:06 -0400642 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400643
Chris Masona0af4692008-05-13 16:03:06 -0400644error_brelse:
645 brelse(bh);
646error_close:
Christoph Hellwig97288f22008-12-02 06:36:09 -0500647 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona0af4692008-05-13 16:03:06 -0400648error:
649 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400650 }
Chris Masona0af4692008-05-13 16:03:06 -0400651 if (fs_devices->open_devices == 0) {
652 ret = -EIO;
653 goto out;
654 }
Yan Zheng2b820322008-11-17 21:11:30 -0500655 fs_devices->seeding = seeding;
656 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400657 fs_devices->latest_bdev = latest_bdev;
658 fs_devices->latest_devid = latest_devid;
659 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500660 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400661out:
Yan Zheng2b820322008-11-17 21:11:30 -0500662 return ret;
663}
664
665int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500666 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500667{
668 int ret;
669
670 mutex_lock(&uuid_mutex);
671 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500672 fs_devices->opened++;
673 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500674 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500675 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500676 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400677 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400678 return ret;
679}
680
Christoph Hellwig97288f22008-12-02 06:36:09 -0500681int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400682 struct btrfs_fs_devices **fs_devices_ret)
683{
684 struct btrfs_super_block *disk_super;
685 struct block_device *bdev;
686 struct buffer_head *bh;
687 int ret;
688 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400689 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400690
691 mutex_lock(&uuid_mutex);
692
Chris Mason15916de2008-11-19 21:17:22 -0500693 bdev = open_bdev_exclusive(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400694
695 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 ret = PTR_ERR(bdev);
697 goto error;
698 }
699
700 ret = set_blocksize(bdev, 4096);
701 if (ret)
702 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500703 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400704 if (!bh) {
705 ret = -EIO;
706 goto error_close;
707 }
708 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000709 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400710 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400711 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500712 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400713 else {
714 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500715 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400716 *(unsigned long long *)disk_super->fsid,
717 *(unsigned long long *)(disk_super->fsid + 8));
718 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500719 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500720 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400721 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
722
Chris Mason8a4b83c2008-03-24 15:02:07 -0400723 brelse(bh);
724error_close:
Chris Mason15916de2008-11-19 21:17:22 -0500725 close_bdev_exclusive(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400726error:
727 mutex_unlock(&uuid_mutex);
728 return ret;
729}
Chris Mason0b86a832008-03-24 15:01:56 -0400730
731/*
732 * this uses a pretty simple search, the expectation is that it is
733 * called very infrequently and that a given device has a small number
734 * of extents
735 */
Josef Bacikba1bf482009-09-11 16:11:19 -0400736int find_free_dev_extent(struct btrfs_trans_handle *trans,
737 struct btrfs_device *device, u64 num_bytes,
738 u64 *start, u64 *max_avail)
Chris Mason0b86a832008-03-24 15:01:56 -0400739{
740 struct btrfs_key key;
741 struct btrfs_root *root = device->dev_root;
742 struct btrfs_dev_extent *dev_extent = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500743 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -0400744 u64 hole_size = 0;
745 u64 last_byte = 0;
746 u64 search_start = 0;
747 u64 search_end = device->total_bytes;
748 int ret;
749 int slot = 0;
750 int start_found;
751 struct extent_buffer *l;
752
Yan Zheng2b820322008-11-17 21:11:30 -0500753 path = btrfs_alloc_path();
754 if (!path)
755 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400756 path->reada = 2;
Yan Zheng2b820322008-11-17 21:11:30 -0500757 start_found = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400758
759 /* FIXME use last free of some kind */
760
Chris Mason8a4b83c2008-03-24 15:02:07 -0400761 /* we don't want to overwrite the superblock on the drive,
762 * so we make sure to start at an offset of at least 1MB
763 */
764 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400765
766 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
767 search_start = max(root->fs_info->alloc_start, search_start);
768
Chris Mason0b86a832008-03-24 15:01:56 -0400769 key.objectid = device->devid;
770 key.offset = search_start;
771 key.type = BTRFS_DEV_EXTENT_KEY;
772 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
773 if (ret < 0)
774 goto error;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400775 if (ret > 0) {
776 ret = btrfs_previous_item(root, path, key.objectid, key.type);
777 if (ret < 0)
778 goto error;
779 if (ret > 0)
780 start_found = 1;
781 }
Chris Mason0b86a832008-03-24 15:01:56 -0400782 l = path->nodes[0];
783 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
784 while (1) {
785 l = path->nodes[0];
786 slot = path->slots[0];
787 if (slot >= btrfs_header_nritems(l)) {
788 ret = btrfs_next_leaf(root, path);
789 if (ret == 0)
790 continue;
791 if (ret < 0)
792 goto error;
793no_more_items:
794 if (!start_found) {
795 if (search_start >= search_end) {
796 ret = -ENOSPC;
797 goto error;
798 }
799 *start = search_start;
800 start_found = 1;
801 goto check_pending;
802 }
803 *start = last_byte > search_start ?
804 last_byte : search_start;
805 if (search_end <= *start) {
806 ret = -ENOSPC;
807 goto error;
808 }
809 goto check_pending;
810 }
811 btrfs_item_key_to_cpu(l, &key, slot);
812
813 if (key.objectid < device->devid)
814 goto next;
815
816 if (key.objectid > device->devid)
817 goto no_more_items;
818
819 if (key.offset >= search_start && key.offset > last_byte &&
820 start_found) {
821 if (last_byte < search_start)
822 last_byte = search_start;
823 hole_size = key.offset - last_byte;
Chris Mason9779b722009-07-24 16:41:41 -0400824
825 if (hole_size > *max_avail)
826 *max_avail = hole_size;
827
Chris Mason0b86a832008-03-24 15:01:56 -0400828 if (key.offset > last_byte &&
829 hole_size >= num_bytes) {
830 *start = last_byte;
831 goto check_pending;
832 }
833 }
Chris Masond3977122009-01-05 21:25:51 -0500834 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400835 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400836
837 start_found = 1;
838 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
839 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
840next:
841 path->slots[0]++;
842 cond_resched();
843 }
844check_pending:
845 /* we have to make sure we didn't find an extent that has already
846 * been allocated by the map tree or the original allocation
847 */
Chris Mason0b86a832008-03-24 15:01:56 -0400848 BUG_ON(*start < search_start);
849
Chris Mason6324fbf2008-03-24 15:01:59 -0400850 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400851 ret = -ENOSPC;
852 goto error;
853 }
854 /* check for pending inserts here */
Yan Zheng2b820322008-11-17 21:11:30 -0500855 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400856
857error:
Yan Zheng2b820322008-11-17 21:11:30 -0500858 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400859 return ret;
860}
861
Christoph Hellwigb2950862008-12-02 09:54:17 -0500862static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400863 struct btrfs_device *device,
864 u64 start)
865{
866 int ret;
867 struct btrfs_path *path;
868 struct btrfs_root *root = device->dev_root;
869 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400870 struct btrfs_key found_key;
871 struct extent_buffer *leaf = NULL;
872 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400873
874 path = btrfs_alloc_path();
875 if (!path)
876 return -ENOMEM;
877
878 key.objectid = device->devid;
879 key.offset = start;
880 key.type = BTRFS_DEV_EXTENT_KEY;
881
882 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400883 if (ret > 0) {
884 ret = btrfs_previous_item(root, path, key.objectid,
885 BTRFS_DEV_EXTENT_KEY);
886 BUG_ON(ret);
887 leaf = path->nodes[0];
888 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
889 extent = btrfs_item_ptr(leaf, path->slots[0],
890 struct btrfs_dev_extent);
891 BUG_ON(found_key.offset > start || found_key.offset +
892 btrfs_dev_extent_length(leaf, extent) < start);
893 ret = 0;
894 } else if (ret == 0) {
895 leaf = path->nodes[0];
896 extent = btrfs_item_ptr(leaf, path->slots[0],
897 struct btrfs_dev_extent);
898 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400899 BUG_ON(ret);
900
Chris Masondfe25022008-05-13 13:46:40 -0400901 if (device->bytes_used > 0)
902 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400903 ret = btrfs_del_item(trans, root, path);
904 BUG_ON(ret);
905
906 btrfs_free_path(path);
907 return ret;
908}
909
Yan Zheng2b820322008-11-17 21:11:30 -0500910int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400911 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400912 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500913 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400914{
915 int ret;
916 struct btrfs_path *path;
917 struct btrfs_root *root = device->dev_root;
918 struct btrfs_dev_extent *extent;
919 struct extent_buffer *leaf;
920 struct btrfs_key key;
921
Chris Masondfe25022008-05-13 13:46:40 -0400922 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400923 path = btrfs_alloc_path();
924 if (!path)
925 return -ENOMEM;
926
Chris Mason0b86a832008-03-24 15:01:56 -0400927 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500928 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400929 key.type = BTRFS_DEV_EXTENT_KEY;
930 ret = btrfs_insert_empty_item(trans, root, path, &key,
931 sizeof(*extent));
932 BUG_ON(ret);
933
934 leaf = path->nodes[0];
935 extent = btrfs_item_ptr(leaf, path->slots[0],
936 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400937 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
938 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
939 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
940
941 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
942 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
943 BTRFS_UUID_SIZE);
944
Chris Mason0b86a832008-03-24 15:01:56 -0400945 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
946 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400947 btrfs_free_path(path);
948 return ret;
949}
950
Chris Masona1b32a52008-09-05 16:09:51 -0400951static noinline int find_next_chunk(struct btrfs_root *root,
952 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400953{
954 struct btrfs_path *path;
955 int ret;
956 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400957 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400958 struct btrfs_key found_key;
959
960 path = btrfs_alloc_path();
961 BUG_ON(!path);
962
Chris Masone17cade2008-04-15 15:41:47 -0400963 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400964 key.offset = (u64)-1;
965 key.type = BTRFS_CHUNK_ITEM_KEY;
966
967 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
968 if (ret < 0)
969 goto error;
970
971 BUG_ON(ret == 0);
972
973 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
974 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400975 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400976 } else {
977 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
978 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400979 if (found_key.objectid != objectid)
980 *offset = 0;
981 else {
982 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
983 struct btrfs_chunk);
984 *offset = found_key.offset +
985 btrfs_chunk_length(path->nodes[0], chunk);
986 }
Chris Mason0b86a832008-03-24 15:01:56 -0400987 }
988 ret = 0;
989error:
990 btrfs_free_path(path);
991 return ret;
992}
993
Yan Zheng2b820322008-11-17 21:11:30 -0500994static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400995{
996 int ret;
997 struct btrfs_key key;
998 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -0500999 struct btrfs_path *path;
1000
1001 root = root->fs_info->chunk_root;
1002
1003 path = btrfs_alloc_path();
1004 if (!path)
1005 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001006
1007 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1008 key.type = BTRFS_DEV_ITEM_KEY;
1009 key.offset = (u64)-1;
1010
1011 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1012 if (ret < 0)
1013 goto error;
1014
1015 BUG_ON(ret == 0);
1016
1017 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1018 BTRFS_DEV_ITEM_KEY);
1019 if (ret) {
1020 *objectid = 1;
1021 } else {
1022 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1023 path->slots[0]);
1024 *objectid = found_key.offset + 1;
1025 }
1026 ret = 0;
1027error:
Yan Zheng2b820322008-11-17 21:11:30 -05001028 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001029 return ret;
1030}
1031
1032/*
1033 * the device information is stored in the chunk root
1034 * the btrfs_device struct should be fully filled in
1035 */
1036int btrfs_add_device(struct btrfs_trans_handle *trans,
1037 struct btrfs_root *root,
1038 struct btrfs_device *device)
1039{
1040 int ret;
1041 struct btrfs_path *path;
1042 struct btrfs_dev_item *dev_item;
1043 struct extent_buffer *leaf;
1044 struct btrfs_key key;
1045 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001046
1047 root = root->fs_info->chunk_root;
1048
1049 path = btrfs_alloc_path();
1050 if (!path)
1051 return -ENOMEM;
1052
Chris Mason0b86a832008-03-24 15:01:56 -04001053 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1054 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001055 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001056
1057 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001058 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001059 if (ret)
1060 goto out;
1061
1062 leaf = path->nodes[0];
1063 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1064
1065 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001066 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001067 btrfs_set_device_type(leaf, dev_item, device->type);
1068 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1069 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1070 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001071 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1072 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001073 btrfs_set_device_group(leaf, dev_item, 0);
1074 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1075 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001076 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001077
Chris Mason0b86a832008-03-24 15:01:56 -04001078 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001079 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001080 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1081 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001082 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001083
Yan Zheng2b820322008-11-17 21:11:30 -05001084 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001085out:
1086 btrfs_free_path(path);
1087 return ret;
1088}
Chris Mason8f18cf12008-04-25 16:53:30 -04001089
Chris Masona061fc82008-05-07 11:43:44 -04001090static int btrfs_rm_dev_item(struct btrfs_root *root,
1091 struct btrfs_device *device)
1092{
1093 int ret;
1094 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001095 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001096 struct btrfs_trans_handle *trans;
1097
1098 root = root->fs_info->chunk_root;
1099
1100 path = btrfs_alloc_path();
1101 if (!path)
1102 return -ENOMEM;
1103
Yan, Zhenga22285a2010-05-16 10:48:46 -04001104 trans = btrfs_start_transaction(root, 0);
Chris Masona061fc82008-05-07 11:43:44 -04001105 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1106 key.type = BTRFS_DEV_ITEM_KEY;
1107 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001108 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001109
1110 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1111 if (ret < 0)
1112 goto out;
1113
1114 if (ret > 0) {
1115 ret = -ENOENT;
1116 goto out;
1117 }
1118
1119 ret = btrfs_del_item(trans, root, path);
1120 if (ret)
1121 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001122out:
1123 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001124 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001125 btrfs_commit_transaction(trans, root);
1126 return ret;
1127}
1128
1129int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1130{
1131 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001132 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001133 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001134 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001135 struct btrfs_super_block *disk_super;
1136 u64 all_avail;
1137 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001138 u64 num_devices;
1139 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001140 int ret = 0;
1141
Chris Masona061fc82008-05-07 11:43:44 -04001142 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001143 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001144
1145 all_avail = root->fs_info->avail_data_alloc_bits |
1146 root->fs_info->avail_system_alloc_bits |
1147 root->fs_info->avail_metadata_alloc_bits;
1148
1149 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001150 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001151 printk(KERN_ERR "btrfs: unable to go below four devices "
1152 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001153 ret = -EINVAL;
1154 goto out;
1155 }
1156
1157 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001158 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001159 printk(KERN_ERR "btrfs: unable to go below two "
1160 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001161 ret = -EINVAL;
1162 goto out;
1163 }
1164
Chris Masondfe25022008-05-13 13:46:40 -04001165 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001166 struct list_head *devices;
1167 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001168
Chris Masondfe25022008-05-13 13:46:40 -04001169 device = NULL;
1170 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001171 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001172 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001173 if (tmp->in_fs_metadata && !tmp->bdev) {
1174 device = tmp;
1175 break;
1176 }
1177 }
Chris Masone5e9a522009-06-10 15:17:02 -04001178 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001179 bdev = NULL;
1180 bh = NULL;
1181 disk_super = NULL;
1182 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001183 printk(KERN_ERR "btrfs: no missing devices found to "
1184 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001185 goto out;
1186 }
Chris Masondfe25022008-05-13 13:46:40 -04001187 } else {
Christoph Hellwig97288f22008-12-02 06:36:09 -05001188 bdev = open_bdev_exclusive(device_path, FMODE_READ,
Chris Masondfe25022008-05-13 13:46:40 -04001189 root->fs_info->bdev_holder);
1190 if (IS_ERR(bdev)) {
1191 ret = PTR_ERR(bdev);
1192 goto out;
1193 }
1194
Yan Zheng2b820322008-11-17 21:11:30 -05001195 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001196 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001197 if (!bh) {
1198 ret = -EIO;
1199 goto error_close;
1200 }
1201 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001202 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001203 dev_uuid = disk_super->dev_item.uuid;
1204 device = btrfs_find_device(root, devid, dev_uuid,
1205 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001206 if (!device) {
1207 ret = -ENOENT;
1208 goto error_brelse;
1209 }
Chris Masondfe25022008-05-13 13:46:40 -04001210 }
Yan Zheng2b820322008-11-17 21:11:30 -05001211
1212 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001213 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1214 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001215 ret = -EINVAL;
1216 goto error_brelse;
1217 }
1218
1219 if (device->writeable) {
1220 list_del_init(&device->dev_alloc_list);
1221 root->fs_info->fs_devices->rw_devices--;
1222 }
Chris Masona061fc82008-05-07 11:43:44 -04001223
1224 ret = btrfs_shrink_device(device, 0);
1225 if (ret)
1226 goto error_brelse;
1227
Chris Masona061fc82008-05-07 11:43:44 -04001228 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1229 if (ret)
1230 goto error_brelse;
1231
Yan Zheng2b820322008-11-17 21:11:30 -05001232 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001233
1234 /*
1235 * the device list mutex makes sure that we don't change
1236 * the device list while someone else is writing out all
1237 * the device supers.
1238 */
1239 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001240 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001241 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1242
Yan Zhenge4404d62008-12-12 10:03:26 -05001243 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001244
Chris Masoncd02dca2010-12-13 14:56:23 -05001245 if (device->missing)
1246 root->fs_info->fs_devices->missing_devices--;
1247
Yan Zheng2b820322008-11-17 21:11:30 -05001248 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1249 struct btrfs_device, dev_list);
1250 if (device->bdev == root->fs_info->sb->s_bdev)
1251 root->fs_info->sb->s_bdev = next_device->bdev;
1252 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1253 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1254
Yan Zhenge4404d62008-12-12 10:03:26 -05001255 if (device->bdev) {
1256 close_bdev_exclusive(device->bdev, device->mode);
1257 device->bdev = NULL;
1258 device->fs_devices->open_devices--;
1259 }
1260
Yan Zheng2b820322008-11-17 21:11:30 -05001261 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1262 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1263
Yan Zhenge4404d62008-12-12 10:03:26 -05001264 if (device->fs_devices->open_devices == 0) {
1265 struct btrfs_fs_devices *fs_devices;
1266 fs_devices = root->fs_info->fs_devices;
1267 while (fs_devices) {
1268 if (fs_devices->seed == device->fs_devices)
1269 break;
1270 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001271 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001272 fs_devices->seed = device->fs_devices->seed;
1273 device->fs_devices->seed = NULL;
1274 __btrfs_close_devices(device->fs_devices);
1275 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001276 }
1277
1278 /*
1279 * at this point, the device is zero sized. We want to
1280 * remove it from the devices list and zero out the old super
1281 */
1282 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001283 /* make sure this device isn't detected as part of
1284 * the FS anymore
1285 */
1286 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1287 set_buffer_dirty(bh);
1288 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001289 }
Chris Masona061fc82008-05-07 11:43:44 -04001290
Chris Masona061fc82008-05-07 11:43:44 -04001291 kfree(device->name);
1292 kfree(device);
1293 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001294
1295error_brelse:
1296 brelse(bh);
1297error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001298 if (bdev)
Christoph Hellwig97288f22008-12-02 06:36:09 -05001299 close_bdev_exclusive(bdev, FMODE_READ);
Chris Masona061fc82008-05-07 11:43:44 -04001300out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001301 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001302 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001303 return ret;
1304}
1305
Yan Zheng2b820322008-11-17 21:11:30 -05001306/*
1307 * does all the dirty work required for changing file system's UUID.
1308 */
1309static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1310 struct btrfs_root *root)
1311{
1312 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1313 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001314 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001315 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1316 struct btrfs_device *device;
1317 u64 super_flags;
1318
1319 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001320 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001321 return -EINVAL;
1322
Yan Zhenge4404d62008-12-12 10:03:26 -05001323 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1324 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001325 return -ENOMEM;
1326
Yan Zhenge4404d62008-12-12 10:03:26 -05001327 old_devices = clone_fs_devices(fs_devices);
1328 if (IS_ERR(old_devices)) {
1329 kfree(seed_devices);
1330 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001331 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001332
Yan Zheng2b820322008-11-17 21:11:30 -05001333 list_add(&old_devices->list, &fs_uuids);
1334
Yan Zhenge4404d62008-12-12 10:03:26 -05001335 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1336 seed_devices->opened = 1;
1337 INIT_LIST_HEAD(&seed_devices->devices);
1338 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001339 mutex_init(&seed_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001340 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1341 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1342 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1343 device->fs_devices = seed_devices;
1344 }
1345
Yan Zheng2b820322008-11-17 21:11:30 -05001346 fs_devices->seeding = 0;
1347 fs_devices->num_devices = 0;
1348 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001349 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001350
1351 generate_random_uuid(fs_devices->fsid);
1352 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1353 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1354 super_flags = btrfs_super_flags(disk_super) &
1355 ~BTRFS_SUPER_FLAG_SEEDING;
1356 btrfs_set_super_flags(disk_super, super_flags);
1357
1358 return 0;
1359}
1360
1361/*
1362 * strore the expected generation for seed devices in device items.
1363 */
1364static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1365 struct btrfs_root *root)
1366{
1367 struct btrfs_path *path;
1368 struct extent_buffer *leaf;
1369 struct btrfs_dev_item *dev_item;
1370 struct btrfs_device *device;
1371 struct btrfs_key key;
1372 u8 fs_uuid[BTRFS_UUID_SIZE];
1373 u8 dev_uuid[BTRFS_UUID_SIZE];
1374 u64 devid;
1375 int ret;
1376
1377 path = btrfs_alloc_path();
1378 if (!path)
1379 return -ENOMEM;
1380
1381 root = root->fs_info->chunk_root;
1382 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1383 key.offset = 0;
1384 key.type = BTRFS_DEV_ITEM_KEY;
1385
1386 while (1) {
1387 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1388 if (ret < 0)
1389 goto error;
1390
1391 leaf = path->nodes[0];
1392next_slot:
1393 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1394 ret = btrfs_next_leaf(root, path);
1395 if (ret > 0)
1396 break;
1397 if (ret < 0)
1398 goto error;
1399 leaf = path->nodes[0];
1400 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1401 btrfs_release_path(root, path);
1402 continue;
1403 }
1404
1405 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1406 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1407 key.type != BTRFS_DEV_ITEM_KEY)
1408 break;
1409
1410 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1411 struct btrfs_dev_item);
1412 devid = btrfs_device_id(leaf, dev_item);
1413 read_extent_buffer(leaf, dev_uuid,
1414 (unsigned long)btrfs_device_uuid(dev_item),
1415 BTRFS_UUID_SIZE);
1416 read_extent_buffer(leaf, fs_uuid,
1417 (unsigned long)btrfs_device_fsid(dev_item),
1418 BTRFS_UUID_SIZE);
1419 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1420 BUG_ON(!device);
1421
1422 if (device->fs_devices->seeding) {
1423 btrfs_set_device_generation(leaf, dev_item,
1424 device->generation);
1425 btrfs_mark_buffer_dirty(leaf);
1426 }
1427
1428 path->slots[0]++;
1429 goto next_slot;
1430 }
1431 ret = 0;
1432error:
1433 btrfs_free_path(path);
1434 return ret;
1435}
1436
Chris Mason788f20e2008-04-28 15:29:42 -04001437int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1438{
1439 struct btrfs_trans_handle *trans;
1440 struct btrfs_device *device;
1441 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001442 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001443 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001444 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001445 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001446 int ret = 0;
1447
Yan Zheng2b820322008-11-17 21:11:30 -05001448 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1449 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001450
Chris Mason15916de2008-11-19 21:17:22 -05001451 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001452 if (IS_ERR(bdev))
1453 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001454
Yan Zheng2b820322008-11-17 21:11:30 -05001455 if (root->fs_info->fs_devices->seeding) {
1456 seeding_dev = 1;
1457 down_write(&sb->s_umount);
1458 mutex_lock(&uuid_mutex);
1459 }
1460
Chris Mason8c8bee12008-09-29 11:19:10 -04001461 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001462 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001463
Chris Mason788f20e2008-04-28 15:29:42 -04001464 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001465 /*
1466 * we have the volume lock, so we don't need the extra
1467 * device list mutex while reading the list here.
1468 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001469 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001470 if (device->bdev == bdev) {
1471 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001472 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001473 }
1474 }
1475
1476 device = kzalloc(sizeof(*device), GFP_NOFS);
1477 if (!device) {
1478 /* we can safely leave the fs_devices entry around */
1479 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001480 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001481 }
1482
Chris Mason788f20e2008-04-28 15:29:42 -04001483 device->name = kstrdup(device_path, GFP_NOFS);
1484 if (!device->name) {
1485 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001486 ret = -ENOMEM;
1487 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001488 }
Yan Zheng2b820322008-11-17 21:11:30 -05001489
1490 ret = find_next_devid(root, &device->devid);
1491 if (ret) {
1492 kfree(device);
1493 goto error;
1494 }
1495
Yan, Zhenga22285a2010-05-16 10:48:46 -04001496 trans = btrfs_start_transaction(root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001497 lock_chunks(root);
1498
1499 device->barriers = 1;
1500 device->writeable = 1;
1501 device->work.func = pending_bios_fn;
1502 generate_random_uuid(device->uuid);
1503 spin_lock_init(&device->io_lock);
1504 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001505 device->io_width = root->sectorsize;
1506 device->io_align = root->sectorsize;
1507 device->sector_size = root->sectorsize;
1508 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001509 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001510 device->dev_root = root->fs_info->dev_root;
1511 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001512 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001513 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001514 set_blocksize(device->bdev, 4096);
1515
Yan Zheng2b820322008-11-17 21:11:30 -05001516 if (seeding_dev) {
1517 sb->s_flags &= ~MS_RDONLY;
1518 ret = btrfs_prepare_sprout(trans, root);
1519 BUG_ON(ret);
1520 }
1521
1522 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001523
1524 /*
1525 * we don't want write_supers to jump in here with our device
1526 * half setup
1527 */
1528 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001529 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1530 list_add(&device->dev_alloc_list,
1531 &root->fs_info->fs_devices->alloc_list);
1532 root->fs_info->fs_devices->num_devices++;
1533 root->fs_info->fs_devices->open_devices++;
1534 root->fs_info->fs_devices->rw_devices++;
1535 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1536
Chris Masonc2898112009-06-10 09:51:32 -04001537 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1538 root->fs_info->fs_devices->rotating = 1;
1539
Chris Mason788f20e2008-04-28 15:29:42 -04001540 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1541 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1542 total_bytes + device->total_bytes);
1543
1544 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1545 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1546 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001547 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001548
Yan Zheng2b820322008-11-17 21:11:30 -05001549 if (seeding_dev) {
1550 ret = init_first_rw_device(trans, root, device);
1551 BUG_ON(ret);
1552 ret = btrfs_finish_sprout(trans, root);
1553 BUG_ON(ret);
1554 } else {
1555 ret = btrfs_add_device(trans, root, device);
1556 }
1557
Chris Mason913d9522009-03-10 13:17:18 -04001558 /*
1559 * we've got more storage, clear any full flags on the space
1560 * infos
1561 */
1562 btrfs_clear_space_info_full(root->fs_info);
1563
Chris Mason7d9eb122008-07-08 14:19:17 -04001564 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001565 btrfs_commit_transaction(trans, root);
1566
1567 if (seeding_dev) {
1568 mutex_unlock(&uuid_mutex);
1569 up_write(&sb->s_umount);
1570
1571 ret = btrfs_relocate_sys_chunks(root);
1572 BUG_ON(ret);
1573 }
1574out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001575 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001576 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001577error:
Chris Mason15916de2008-11-19 21:17:22 -05001578 close_bdev_exclusive(bdev, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001579 if (seeding_dev) {
1580 mutex_unlock(&uuid_mutex);
1581 up_write(&sb->s_umount);
1582 }
Chris Mason788f20e2008-04-28 15:29:42 -04001583 goto out;
1584}
1585
Chris Masond3977122009-01-05 21:25:51 -05001586static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1587 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001588{
1589 int ret;
1590 struct btrfs_path *path;
1591 struct btrfs_root *root;
1592 struct btrfs_dev_item *dev_item;
1593 struct extent_buffer *leaf;
1594 struct btrfs_key key;
1595
1596 root = device->dev_root->fs_info->chunk_root;
1597
1598 path = btrfs_alloc_path();
1599 if (!path)
1600 return -ENOMEM;
1601
1602 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1603 key.type = BTRFS_DEV_ITEM_KEY;
1604 key.offset = device->devid;
1605
1606 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1607 if (ret < 0)
1608 goto out;
1609
1610 if (ret > 0) {
1611 ret = -ENOENT;
1612 goto out;
1613 }
1614
1615 leaf = path->nodes[0];
1616 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1617
1618 btrfs_set_device_id(leaf, dev_item, device->devid);
1619 btrfs_set_device_type(leaf, dev_item, device->type);
1620 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1621 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1622 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001623 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001624 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1625 btrfs_mark_buffer_dirty(leaf);
1626
1627out:
1628 btrfs_free_path(path);
1629 return ret;
1630}
1631
Chris Mason7d9eb122008-07-08 14:19:17 -04001632static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001633 struct btrfs_device *device, u64 new_size)
1634{
1635 struct btrfs_super_block *super_copy =
1636 &device->dev_root->fs_info->super_copy;
1637 u64 old_total = btrfs_super_total_bytes(super_copy);
1638 u64 diff = new_size - device->total_bytes;
1639
Yan Zheng2b820322008-11-17 21:11:30 -05001640 if (!device->writeable)
1641 return -EACCES;
1642 if (new_size <= device->total_bytes)
1643 return -EINVAL;
1644
Chris Mason8f18cf12008-04-25 16:53:30 -04001645 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001646 device->fs_devices->total_rw_bytes += diff;
1647
1648 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001649 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001650 btrfs_clear_space_info_full(device->dev_root->fs_info);
1651
Chris Mason8f18cf12008-04-25 16:53:30 -04001652 return btrfs_update_device(trans, device);
1653}
1654
Chris Mason7d9eb122008-07-08 14:19:17 -04001655int btrfs_grow_device(struct btrfs_trans_handle *trans,
1656 struct btrfs_device *device, u64 new_size)
1657{
1658 int ret;
1659 lock_chunks(device->dev_root);
1660 ret = __btrfs_grow_device(trans, device, new_size);
1661 unlock_chunks(device->dev_root);
1662 return ret;
1663}
1664
Chris Mason8f18cf12008-04-25 16:53:30 -04001665static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1666 struct btrfs_root *root,
1667 u64 chunk_tree, u64 chunk_objectid,
1668 u64 chunk_offset)
1669{
1670 int ret;
1671 struct btrfs_path *path;
1672 struct btrfs_key key;
1673
1674 root = root->fs_info->chunk_root;
1675 path = btrfs_alloc_path();
1676 if (!path)
1677 return -ENOMEM;
1678
1679 key.objectid = chunk_objectid;
1680 key.offset = chunk_offset;
1681 key.type = BTRFS_CHUNK_ITEM_KEY;
1682
1683 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1684 BUG_ON(ret);
1685
1686 ret = btrfs_del_item(trans, root, path);
1687 BUG_ON(ret);
1688
1689 btrfs_free_path(path);
1690 return 0;
1691}
1692
Christoph Hellwigb2950862008-12-02 09:54:17 -05001693static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001694 chunk_offset)
1695{
1696 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1697 struct btrfs_disk_key *disk_key;
1698 struct btrfs_chunk *chunk;
1699 u8 *ptr;
1700 int ret = 0;
1701 u32 num_stripes;
1702 u32 array_size;
1703 u32 len = 0;
1704 u32 cur;
1705 struct btrfs_key key;
1706
1707 array_size = btrfs_super_sys_array_size(super_copy);
1708
1709 ptr = super_copy->sys_chunk_array;
1710 cur = 0;
1711
1712 while (cur < array_size) {
1713 disk_key = (struct btrfs_disk_key *)ptr;
1714 btrfs_disk_key_to_cpu(&key, disk_key);
1715
1716 len = sizeof(*disk_key);
1717
1718 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1719 chunk = (struct btrfs_chunk *)(ptr + len);
1720 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1721 len += btrfs_chunk_item_size(num_stripes);
1722 } else {
1723 ret = -EIO;
1724 break;
1725 }
1726 if (key.objectid == chunk_objectid &&
1727 key.offset == chunk_offset) {
1728 memmove(ptr, ptr + len, array_size - (cur + len));
1729 array_size -= len;
1730 btrfs_set_super_sys_array_size(super_copy, array_size);
1731 } else {
1732 ptr += len;
1733 cur += len;
1734 }
1735 }
1736 return ret;
1737}
1738
Christoph Hellwigb2950862008-12-02 09:54:17 -05001739static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001740 u64 chunk_tree, u64 chunk_objectid,
1741 u64 chunk_offset)
1742{
1743 struct extent_map_tree *em_tree;
1744 struct btrfs_root *extent_root;
1745 struct btrfs_trans_handle *trans;
1746 struct extent_map *em;
1747 struct map_lookup *map;
1748 int ret;
1749 int i;
1750
1751 root = root->fs_info->chunk_root;
1752 extent_root = root->fs_info->extent_root;
1753 em_tree = &root->fs_info->mapping_tree.map_tree;
1754
Josef Bacikba1bf482009-09-11 16:11:19 -04001755 ret = btrfs_can_relocate(extent_root, chunk_offset);
1756 if (ret)
1757 return -ENOSPC;
1758
Chris Mason8f18cf12008-04-25 16:53:30 -04001759 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001760 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001761 if (ret)
1762 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001763
Yan, Zhenga22285a2010-05-16 10:48:46 -04001764 trans = btrfs_start_transaction(root, 0);
Chris Mason8f18cf12008-04-25 16:53:30 -04001765 BUG_ON(!trans);
1766
Chris Mason7d9eb122008-07-08 14:19:17 -04001767 lock_chunks(root);
1768
Chris Mason8f18cf12008-04-25 16:53:30 -04001769 /*
1770 * step two, delete the device extents and the
1771 * chunk tree entries
1772 */
Chris Mason890871b2009-09-02 16:24:52 -04001773 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001774 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001775 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001776
Chris Masona061fc82008-05-07 11:43:44 -04001777 BUG_ON(em->start > chunk_offset ||
1778 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001779 map = (struct map_lookup *)em->bdev;
1780
1781 for (i = 0; i < map->num_stripes; i++) {
1782 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1783 map->stripes[i].physical);
1784 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001785
Chris Masondfe25022008-05-13 13:46:40 -04001786 if (map->stripes[i].dev) {
1787 ret = btrfs_update_device(trans, map->stripes[i].dev);
1788 BUG_ON(ret);
1789 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001790 }
1791 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1792 chunk_offset);
1793
1794 BUG_ON(ret);
1795
1796 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1797 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1798 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001799 }
1800
Zheng Yan1a40e232008-09-26 10:09:34 -04001801 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1802 BUG_ON(ret);
1803
Chris Mason890871b2009-09-02 16:24:52 -04001804 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001805 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001806 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001807
Chris Mason8f18cf12008-04-25 16:53:30 -04001808 kfree(map);
1809 em->bdev = NULL;
1810
1811 /* once for the tree */
1812 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001813 /* once for us */
1814 free_extent_map(em);
1815
Chris Mason7d9eb122008-07-08 14:19:17 -04001816 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001817 btrfs_end_transaction(trans, root);
1818 return 0;
1819}
1820
Yan Zheng2b820322008-11-17 21:11:30 -05001821static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1822{
1823 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1824 struct btrfs_path *path;
1825 struct extent_buffer *leaf;
1826 struct btrfs_chunk *chunk;
1827 struct btrfs_key key;
1828 struct btrfs_key found_key;
1829 u64 chunk_tree = chunk_root->root_key.objectid;
1830 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001831 bool retried = false;
1832 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001833 int ret;
1834
1835 path = btrfs_alloc_path();
1836 if (!path)
1837 return -ENOMEM;
1838
Josef Bacikba1bf482009-09-11 16:11:19 -04001839again:
Yan Zheng2b820322008-11-17 21:11:30 -05001840 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1841 key.offset = (u64)-1;
1842 key.type = BTRFS_CHUNK_ITEM_KEY;
1843
1844 while (1) {
1845 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1846 if (ret < 0)
1847 goto error;
1848 BUG_ON(ret == 0);
1849
1850 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1851 key.type);
1852 if (ret < 0)
1853 goto error;
1854 if (ret > 0)
1855 break;
1856
1857 leaf = path->nodes[0];
1858 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1859
1860 chunk = btrfs_item_ptr(leaf, path->slots[0],
1861 struct btrfs_chunk);
1862 chunk_type = btrfs_chunk_type(leaf, chunk);
1863 btrfs_release_path(chunk_root, path);
1864
1865 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1866 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1867 found_key.objectid,
1868 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001869 if (ret == -ENOSPC)
1870 failed++;
1871 else if (ret)
1872 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001873 }
1874
1875 if (found_key.offset == 0)
1876 break;
1877 key.offset = found_key.offset - 1;
1878 }
1879 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04001880 if (failed && !retried) {
1881 failed = 0;
1882 retried = true;
1883 goto again;
1884 } else if (failed && retried) {
1885 WARN_ON(1);
1886 ret = -ENOSPC;
1887 }
Yan Zheng2b820322008-11-17 21:11:30 -05001888error:
1889 btrfs_free_path(path);
1890 return ret;
1891}
1892
Chris Masonec44a352008-04-28 15:29:52 -04001893static u64 div_factor(u64 num, int factor)
1894{
1895 if (factor == 10)
1896 return num;
1897 num *= factor;
1898 do_div(num, 10);
1899 return num;
1900}
1901
Chris Masonec44a352008-04-28 15:29:52 -04001902int btrfs_balance(struct btrfs_root *dev_root)
1903{
1904 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04001905 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1906 struct btrfs_device *device;
1907 u64 old_size;
1908 u64 size_to_free;
1909 struct btrfs_path *path;
1910 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04001911 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1912 struct btrfs_trans_handle *trans;
1913 struct btrfs_key found_key;
1914
Yan Zheng2b820322008-11-17 21:11:30 -05001915 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1916 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001917
Chris Mason7d9eb122008-07-08 14:19:17 -04001918 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001919 dev_root = dev_root->fs_info->dev_root;
1920
Chris Masonec44a352008-04-28 15:29:52 -04001921 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001922 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04001923 old_size = device->total_bytes;
1924 size_to_free = div_factor(old_size, 1);
1925 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05001926 if (!device->writeable ||
1927 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04001928 continue;
1929
1930 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04001931 if (ret == -ENOSPC)
1932 break;
Chris Masonec44a352008-04-28 15:29:52 -04001933 BUG_ON(ret);
1934
Yan, Zhenga22285a2010-05-16 10:48:46 -04001935 trans = btrfs_start_transaction(dev_root, 0);
Chris Masonec44a352008-04-28 15:29:52 -04001936 BUG_ON(!trans);
1937
1938 ret = btrfs_grow_device(trans, device, old_size);
1939 BUG_ON(ret);
1940
1941 btrfs_end_transaction(trans, dev_root);
1942 }
1943
1944 /* step two, relocate all the chunks */
1945 path = btrfs_alloc_path();
1946 BUG_ON(!path);
1947
1948 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1949 key.offset = (u64)-1;
1950 key.type = BTRFS_CHUNK_ITEM_KEY;
1951
Chris Masond3977122009-01-05 21:25:51 -05001952 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04001953 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1954 if (ret < 0)
1955 goto error;
1956
1957 /*
1958 * this shouldn't happen, it means the last relocate
1959 * failed
1960 */
1961 if (ret == 0)
1962 break;
1963
1964 ret = btrfs_previous_item(chunk_root, path, 0,
1965 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001966 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001967 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001968
Chris Masonec44a352008-04-28 15:29:52 -04001969 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1970 path->slots[0]);
1971 if (found_key.objectid != key.objectid)
1972 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001973
Chris Masonec44a352008-04-28 15:29:52 -04001974 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04001975 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04001976 break;
1977
Chris Mason7d9eb122008-07-08 14:19:17 -04001978 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001979 ret = btrfs_relocate_chunk(chunk_root,
1980 chunk_root->root_key.objectid,
1981 found_key.objectid,
1982 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001983 BUG_ON(ret && ret != -ENOSPC);
1984 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04001985 }
1986 ret = 0;
1987error:
1988 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001989 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001990 return ret;
1991}
1992
Chris Mason8f18cf12008-04-25 16:53:30 -04001993/*
1994 * shrinking a device means finding all of the device extents past
1995 * the new size, and then following the back refs to the chunks.
1996 * The chunk relocation code actually frees the device extent
1997 */
1998int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1999{
2000 struct btrfs_trans_handle *trans;
2001 struct btrfs_root *root = device->dev_root;
2002 struct btrfs_dev_extent *dev_extent = NULL;
2003 struct btrfs_path *path;
2004 u64 length;
2005 u64 chunk_tree;
2006 u64 chunk_objectid;
2007 u64 chunk_offset;
2008 int ret;
2009 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002010 int failed = 0;
2011 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002012 struct extent_buffer *l;
2013 struct btrfs_key key;
2014 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2015 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002016 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002017 u64 diff = device->total_bytes - new_size;
2018
Yan Zheng2b820322008-11-17 21:11:30 -05002019 if (new_size >= device->total_bytes)
2020 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002021
2022 path = btrfs_alloc_path();
2023 if (!path)
2024 return -ENOMEM;
2025
Chris Mason8f18cf12008-04-25 16:53:30 -04002026 path->reada = 2;
2027
Chris Mason7d9eb122008-07-08 14:19:17 -04002028 lock_chunks(root);
2029
Chris Mason8f18cf12008-04-25 16:53:30 -04002030 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002031 if (device->writeable)
2032 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002033 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002034
Josef Bacikba1bf482009-09-11 16:11:19 -04002035again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002036 key.objectid = device->devid;
2037 key.offset = (u64)-1;
2038 key.type = BTRFS_DEV_EXTENT_KEY;
2039
2040 while (1) {
2041 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2042 if (ret < 0)
2043 goto done;
2044
2045 ret = btrfs_previous_item(root, path, 0, key.type);
2046 if (ret < 0)
2047 goto done;
2048 if (ret) {
2049 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002050 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002051 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002052 }
2053
2054 l = path->nodes[0];
2055 slot = path->slots[0];
2056 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2057
Josef Bacikba1bf482009-09-11 16:11:19 -04002058 if (key.objectid != device->devid) {
2059 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002060 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002061 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002062
2063 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2064 length = btrfs_dev_extent_length(l, dev_extent);
2065
Josef Bacikba1bf482009-09-11 16:11:19 -04002066 if (key.offset + length <= new_size) {
2067 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002068 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002069 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002070
2071 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2072 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2073 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2074 btrfs_release_path(root, path);
2075
2076 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2077 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002078 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002079 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002080 if (ret == -ENOSPC)
2081 failed++;
2082 key.offset -= 1;
2083 }
2084
2085 if (failed && !retried) {
2086 failed = 0;
2087 retried = true;
2088 goto again;
2089 } else if (failed && retried) {
2090 ret = -ENOSPC;
2091 lock_chunks(root);
2092
2093 device->total_bytes = old_size;
2094 if (device->writeable)
2095 device->fs_devices->total_rw_bytes += diff;
2096 unlock_chunks(root);
2097 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002098 }
2099
Chris Balld6397ba2009-04-27 07:29:03 -04002100 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002101 trans = btrfs_start_transaction(root, 0);
Chris Balld6397ba2009-04-27 07:29:03 -04002102 lock_chunks(root);
2103
2104 device->disk_total_bytes = new_size;
2105 /* Now btrfs_update_device() will change the on-disk size. */
2106 ret = btrfs_update_device(trans, device);
2107 if (ret) {
2108 unlock_chunks(root);
2109 btrfs_end_transaction(trans, root);
2110 goto done;
2111 }
2112 WARN_ON(diff > old_total);
2113 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2114 unlock_chunks(root);
2115 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002116done:
2117 btrfs_free_path(path);
2118 return ret;
2119}
2120
Christoph Hellwigb2950862008-12-02 09:54:17 -05002121static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002122 struct btrfs_root *root,
2123 struct btrfs_key *key,
2124 struct btrfs_chunk *chunk, int item_size)
2125{
2126 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2127 struct btrfs_disk_key disk_key;
2128 u32 array_size;
2129 u8 *ptr;
2130
2131 array_size = btrfs_super_sys_array_size(super_copy);
2132 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2133 return -EFBIG;
2134
2135 ptr = super_copy->sys_chunk_array + array_size;
2136 btrfs_cpu_key_to_disk(&disk_key, key);
2137 memcpy(ptr, &disk_key, sizeof(disk_key));
2138 ptr += sizeof(disk_key);
2139 memcpy(ptr, chunk, item_size);
2140 item_size += sizeof(disk_key);
2141 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2142 return 0;
2143}
2144
Chris Masond3977122009-01-05 21:25:51 -05002145static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002146 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002147{
2148 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2149 return calc_size;
2150 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2151 return calc_size * (num_stripes / sub_stripes);
2152 else
2153 return calc_size * num_stripes;
2154}
2155
Yan Zheng2b820322008-11-17 21:11:30 -05002156static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2157 struct btrfs_root *extent_root,
2158 struct map_lookup **map_ret,
2159 u64 *num_bytes, u64 *stripe_size,
2160 u64 start, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04002161{
Chris Mason593060d2008-03-25 16:50:33 -04002162 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04002163 struct btrfs_device *device = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05002164 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Chris Mason6324fbf2008-03-24 15:01:59 -04002165 struct list_head *cur;
Yan Zheng2b820322008-11-17 21:11:30 -05002166 struct map_lookup *map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002167 struct extent_map_tree *em_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04002168 struct extent_map *em;
Yan Zheng2b820322008-11-17 21:11:30 -05002169 struct list_head private_devs;
Chris Masona40a90a2008-04-18 11:55:51 -04002170 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04002171 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002172 u64 max_chunk_size = calc_size;
2173 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04002174 u64 avail;
2175 u64 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002176 u64 dev_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04002177 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04002178 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002179 int sub_stripes = 0;
Miao Xie1974a3b2011-01-05 10:07:24 +00002180 int ncopies = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002181 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002182 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04002183 int index;
Chris Mason593060d2008-03-25 16:50:33 -04002184 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04002185
Chris Masonec44a352008-04-28 15:29:52 -04002186 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2187 (type & BTRFS_BLOCK_GROUP_DUP)) {
2188 WARN_ON(1);
2189 type &= ~BTRFS_BLOCK_GROUP_DUP;
2190 }
Yan Zheng2b820322008-11-17 21:11:30 -05002191 if (list_empty(&fs_devices->alloc_list))
Chris Mason6324fbf2008-03-24 15:01:59 -04002192 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04002193
Chris Masona40a90a2008-04-18 11:55:51 -04002194 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002195 num_stripes = fs_devices->rw_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04002196 min_stripes = 2;
2197 }
2198 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04002199 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002200 min_stripes = 2;
Miao Xie1974a3b2011-01-05 10:07:24 +00002201 ncopies = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002202 }
Chris Mason8790d502008-04-03 16:29:03 -04002203 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Zhao Leif3eae7e2010-03-25 12:32:59 +00002204 if (fs_devices->rw_devices < 2)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002205 return -ENOSPC;
Zhao Leif3eae7e2010-03-25 12:32:59 +00002206 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002207 min_stripes = 2;
Miao Xie1974a3b2011-01-05 10:07:24 +00002208 ncopies = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002209 }
Chris Mason321aecc2008-04-16 10:49:51 -04002210 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002211 num_stripes = fs_devices->rw_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04002212 if (num_stripes < 4)
2213 return -ENOSPC;
2214 num_stripes &= ~(u32)1;
2215 sub_stripes = 2;
Miao Xie1974a3b2011-01-05 10:07:24 +00002216 ncopies = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002217 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002218 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002219
2220 if (type & BTRFS_BLOCK_GROUP_DATA) {
2221 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002222 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002223 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik83d3c962009-12-07 21:45:59 +00002224 max_chunk_size = 256 * 1024 * 1024;
Chris Masona40a90a2008-04-18 11:55:51 -04002225 min_stripe_size = 32 * 1024 * 1024;
2226 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2227 calc_size = 8 * 1024 * 1024;
2228 max_chunk_size = calc_size * 2;
2229 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002230 }
2231
Yan Zheng2b820322008-11-17 21:11:30 -05002232 /* we don't want a chunk larger than 10% of writeable space */
2233 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2234 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002235
Chris Masona40a90a2008-04-18 11:55:51 -04002236again:
Chris Mason9779b722009-07-24 16:41:41 -04002237 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002238 if (!map || map->num_stripes != num_stripes) {
2239 kfree(map);
2240 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2241 if (!map)
2242 return -ENOMEM;
2243 map->num_stripes = num_stripes;
2244 }
2245
Miao Xie1974a3b2011-01-05 10:07:24 +00002246 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2247 calc_size = max_chunk_size * ncopies;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002248 do_div(calc_size, num_stripes);
2249 do_div(calc_size, stripe_len);
2250 calc_size *= stripe_len;
2251 }
Josef Bacik0cad8a12010-03-17 20:45:56 +00002252
Chris Mason9b3f68b2008-04-18 10:29:51 -04002253 /* we don't want tiny stripes */
Josef Bacik0cad8a12010-03-17 20:45:56 +00002254 if (!looped)
2255 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002256
Chris Mason9f680ce2010-04-06 09:37:47 -04002257 /*
2258 * we're about to do_div by the stripe_len so lets make sure
2259 * we end up with something bigger than a stripe
2260 */
2261 calc_size = max_t(u64, calc_size, stripe_len * 4);
2262
Chris Mason9b3f68b2008-04-18 10:29:51 -04002263 do_div(calc_size, stripe_len);
2264 calc_size *= stripe_len;
2265
Yan Zheng2b820322008-11-17 21:11:30 -05002266 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002267 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002268
2269 if (type & BTRFS_BLOCK_GROUP_DUP)
2270 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002271 else
2272 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04002273
Josef Bacik0f9dd462008-09-23 13:14:11 -04002274 /*
2275 * we add 1MB because we never use the first 1MB of the device, unless
2276 * we've looped, then we are likely allocating the maximum amount of
2277 * space left already
2278 */
2279 if (!looped)
2280 min_free += 1024 * 1024;
Chris Masonad5bd912008-04-21 08:28:10 -04002281
Yan Zheng2b820322008-11-17 21:11:30 -05002282 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002283 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002284 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002285 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002286 if (device->total_bytes > device->bytes_used)
2287 avail = device->total_bytes - device->bytes_used;
2288 else
2289 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002290 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002291
Chris Masondfe25022008-05-13 13:46:40 -04002292 if (device->in_fs_metadata && avail >= min_free) {
Yan Zheng2b820322008-11-17 21:11:30 -05002293 ret = find_free_dev_extent(trans, device,
Chris Mason9779b722009-07-24 16:41:41 -04002294 min_free, &dev_offset,
2295 &max_avail);
Chris Mason8f18cf12008-04-25 16:53:30 -04002296 if (ret == 0) {
2297 list_move_tail(&device->dev_alloc_list,
2298 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002299 map->stripes[index].dev = device;
2300 map->stripes[index].physical = dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002301 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002302 if (type & BTRFS_BLOCK_GROUP_DUP) {
2303 map->stripes[index].dev = device;
2304 map->stripes[index].physical =
2305 dev_offset + calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002306 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002307 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002308 }
Chris Masondfe25022008-05-13 13:46:40 -04002309 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04002310 max_avail = avail;
Yan Zheng2b820322008-11-17 21:11:30 -05002311 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002312 break;
2313 }
Yan Zheng2b820322008-11-17 21:11:30 -05002314 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002315 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002316 if (index >= min_stripes) {
2317 num_stripes = index;
2318 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2319 num_stripes /= sub_stripes;
2320 num_stripes *= sub_stripes;
2321 }
2322 looped = 1;
2323 goto again;
2324 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002325 if (!looped && max_avail > 0) {
2326 looped = 1;
2327 calc_size = max_avail;
Miao Xie1974a3b2011-01-05 10:07:24 +00002328 if (type & BTRFS_BLOCK_GROUP_DUP)
2329 do_div(calc_size, 2);
Chris Mason6324fbf2008-03-24 15:01:59 -04002330 goto again;
2331 }
Yan Zheng2b820322008-11-17 21:11:30 -05002332 kfree(map);
Chris Mason6324fbf2008-03-24 15:01:59 -04002333 return -ENOSPC;
2334 }
Chris Mason593060d2008-03-25 16:50:33 -04002335 map->sector_size = extent_root->sectorsize;
2336 map->stripe_len = stripe_len;
2337 map->io_align = stripe_len;
2338 map->io_width = stripe_len;
2339 map->type = type;
2340 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002341 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002342
Yan Zheng2b820322008-11-17 21:11:30 -05002343 *map_ret = map;
2344 *stripe_size = calc_size;
2345 *num_bytes = chunk_bytes_by_type(type, calc_size,
2346 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002347
2348 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002349 if (!em) {
2350 kfree(map);
Chris Mason0b86a832008-03-24 15:01:56 -04002351 return -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05002352 }
Chris Mason0b86a832008-03-24 15:01:56 -04002353 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002354 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002355 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002356 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002357 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002358
Chris Mason0b86a832008-03-24 15:01:56 -04002359 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002360 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002361 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002362 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002363 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002364 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002365
2366 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2367 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2368 start, *num_bytes);
2369 BUG_ON(ret);
2370
2371 index = 0;
2372 while (index < map->num_stripes) {
2373 device = map->stripes[index].dev;
2374 dev_offset = map->stripes[index].physical;
2375
2376 ret = btrfs_alloc_dev_extent(trans, device,
2377 info->chunk_root->root_key.objectid,
2378 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2379 start, dev_offset, calc_size);
2380 BUG_ON(ret);
2381 index++;
2382 }
2383
2384 return 0;
2385}
2386
2387static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2388 struct btrfs_root *extent_root,
2389 struct map_lookup *map, u64 chunk_offset,
2390 u64 chunk_size, u64 stripe_size)
2391{
2392 u64 dev_offset;
2393 struct btrfs_key key;
2394 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2395 struct btrfs_device *device;
2396 struct btrfs_chunk *chunk;
2397 struct btrfs_stripe *stripe;
2398 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2399 int index = 0;
2400 int ret;
2401
2402 chunk = kzalloc(item_size, GFP_NOFS);
2403 if (!chunk)
2404 return -ENOMEM;
2405
2406 index = 0;
2407 while (index < map->num_stripes) {
2408 device = map->stripes[index].dev;
2409 device->bytes_used += stripe_size;
2410 ret = btrfs_update_device(trans, device);
2411 BUG_ON(ret);
2412 index++;
2413 }
2414
2415 index = 0;
2416 stripe = &chunk->stripe;
2417 while (index < map->num_stripes) {
2418 device = map->stripes[index].dev;
2419 dev_offset = map->stripes[index].physical;
2420
2421 btrfs_set_stack_stripe_devid(stripe, device->devid);
2422 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2423 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2424 stripe++;
2425 index++;
2426 }
2427
2428 btrfs_set_stack_chunk_length(chunk, chunk_size);
2429 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2430 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2431 btrfs_set_stack_chunk_type(chunk, map->type);
2432 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2433 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2434 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2435 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2436 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2437
2438 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2439 key.type = BTRFS_CHUNK_ITEM_KEY;
2440 key.offset = chunk_offset;
2441
2442 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2443 BUG_ON(ret);
2444
2445 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2446 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2447 item_size);
2448 BUG_ON(ret);
2449 }
2450 kfree(chunk);
2451 return 0;
2452}
2453
2454/*
2455 * Chunk allocation falls into two parts. The first part does works
2456 * that make the new allocated chunk useable, but not do any operation
2457 * that modifies the chunk tree. The second part does the works that
2458 * require modifying the chunk tree. This division is important for the
2459 * bootstrap process of adding storage to a seed btrfs.
2460 */
2461int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2462 struct btrfs_root *extent_root, u64 type)
2463{
2464 u64 chunk_offset;
2465 u64 chunk_size;
2466 u64 stripe_size;
2467 struct map_lookup *map;
2468 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2469 int ret;
2470
2471 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2472 &chunk_offset);
2473 if (ret)
2474 return ret;
2475
2476 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2477 &stripe_size, chunk_offset, type);
2478 if (ret)
2479 return ret;
2480
2481 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2482 chunk_size, stripe_size);
2483 BUG_ON(ret);
2484 return 0;
2485}
2486
Chris Masond3977122009-01-05 21:25:51 -05002487static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002488 struct btrfs_root *root,
2489 struct btrfs_device *device)
2490{
2491 u64 chunk_offset;
2492 u64 sys_chunk_offset;
2493 u64 chunk_size;
2494 u64 sys_chunk_size;
2495 u64 stripe_size;
2496 u64 sys_stripe_size;
2497 u64 alloc_profile;
2498 struct map_lookup *map;
2499 struct map_lookup *sys_map;
2500 struct btrfs_fs_info *fs_info = root->fs_info;
2501 struct btrfs_root *extent_root = fs_info->extent_root;
2502 int ret;
2503
2504 ret = find_next_chunk(fs_info->chunk_root,
2505 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2506 BUG_ON(ret);
2507
2508 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2509 (fs_info->metadata_alloc_profile &
2510 fs_info->avail_metadata_alloc_bits);
2511 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2512
2513 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2514 &stripe_size, chunk_offset, alloc_profile);
2515 BUG_ON(ret);
2516
2517 sys_chunk_offset = chunk_offset + chunk_size;
2518
2519 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2520 (fs_info->system_alloc_profile &
2521 fs_info->avail_system_alloc_bits);
2522 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2523
2524 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2525 &sys_chunk_size, &sys_stripe_size,
2526 sys_chunk_offset, alloc_profile);
2527 BUG_ON(ret);
2528
2529 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2530 BUG_ON(ret);
2531
2532 /*
2533 * Modifying chunk tree needs allocating new blocks from both
2534 * system block group and metadata block group. So we only can
2535 * do operations require modifying the chunk tree after both
2536 * block groups were created.
2537 */
2538 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2539 chunk_size, stripe_size);
2540 BUG_ON(ret);
2541
2542 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2543 sys_chunk_offset, sys_chunk_size,
2544 sys_stripe_size);
2545 BUG_ON(ret);
2546 return 0;
2547}
2548
2549int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2550{
2551 struct extent_map *em;
2552 struct map_lookup *map;
2553 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2554 int readonly = 0;
2555 int i;
2556
Chris Mason890871b2009-09-02 16:24:52 -04002557 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002558 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002559 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002560 if (!em)
2561 return 1;
2562
Josef Bacikf48b9072010-01-27 02:07:59 +00002563 if (btrfs_test_opt(root, DEGRADED)) {
2564 free_extent_map(em);
2565 return 0;
2566 }
2567
Yan Zheng2b820322008-11-17 21:11:30 -05002568 map = (struct map_lookup *)em->bdev;
2569 for (i = 0; i < map->num_stripes; i++) {
2570 if (!map->stripes[i].dev->writeable) {
2571 readonly = 1;
2572 break;
2573 }
2574 }
2575 free_extent_map(em);
2576 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002577}
2578
2579void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2580{
2581 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2582}
2583
2584void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2585{
2586 struct extent_map *em;
2587
Chris Masond3977122009-01-05 21:25:51 -05002588 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002589 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002590 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2591 if (em)
2592 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002593 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002594 if (!em)
2595 break;
2596 kfree(em->bdev);
2597 /* once for us */
2598 free_extent_map(em);
2599 /* once for the tree */
2600 free_extent_map(em);
2601 }
2602}
2603
Chris Masonf1885912008-04-09 16:28:12 -04002604int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2605{
2606 struct extent_map *em;
2607 struct map_lookup *map;
2608 struct extent_map_tree *em_tree = &map_tree->map_tree;
2609 int ret;
2610
Chris Mason890871b2009-09-02 16:24:52 -04002611 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002612 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002613 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002614 BUG_ON(!em);
2615
2616 BUG_ON(em->start > logical || em->start + em->len < logical);
2617 map = (struct map_lookup *)em->bdev;
2618 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2619 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002620 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2621 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002622 else
2623 ret = 1;
2624 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002625 return ret;
2626}
2627
Chris Masondfe25022008-05-13 13:46:40 -04002628static int find_live_mirror(struct map_lookup *map, int first, int num,
2629 int optimal)
2630{
2631 int i;
2632 if (map->stripes[optimal].dev->bdev)
2633 return optimal;
2634 for (i = first; i < first + num; i++) {
2635 if (map->stripes[i].dev->bdev)
2636 return i;
2637 }
2638 /* we couldn't find one that doesn't fail. Just return something
2639 * and the io error handling code will clean up eventually
2640 */
2641 return optimal;
2642}
2643
Chris Masonf2d8d742008-04-21 10:03:05 -04002644static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2645 u64 logical, u64 *length,
2646 struct btrfs_multi_bio **multi_ret,
2647 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002648{
2649 struct extent_map *em;
2650 struct map_lookup *map;
2651 struct extent_map_tree *em_tree = &map_tree->map_tree;
2652 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002653 u64 stripe_offset;
2654 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002655 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002656 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002657 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002658 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002659 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002660 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002661 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002662
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002663 if (multi_ret && !(rw & REQ_WRITE))
Chris Masoncea9e442008-04-09 16:28:12 -04002664 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002665again:
2666 if (multi_ret) {
2667 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2668 GFP_NOFS);
2669 if (!multi)
2670 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002671
2672 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002673 }
Chris Mason0b86a832008-03-24 15:01:56 -04002674
Chris Mason890871b2009-09-02 16:24:52 -04002675 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002676 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002677 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002678
Jiri Slaby2423fdf2010-01-06 16:57:22 +00002679 if (!em && unplug_page) {
2680 kfree(multi);
Chris Masonf2d8d742008-04-21 10:03:05 -04002681 return 0;
Jiri Slaby2423fdf2010-01-06 16:57:22 +00002682 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002683
Chris Mason3b951512008-04-17 11:29:12 -04002684 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002685 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2686 (unsigned long long)logical,
2687 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002688 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002689 }
Chris Mason0b86a832008-03-24 15:01:56 -04002690
2691 BUG_ON(em->start > logical || em->start + em->len < logical);
2692 map = (struct map_lookup *)em->bdev;
2693 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002694
Chris Masonf1885912008-04-09 16:28:12 -04002695 if (mirror_num > map->num_stripes)
2696 mirror_num = 0;
2697
Chris Masoncea9e442008-04-09 16:28:12 -04002698 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002699 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002700 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2701 BTRFS_BLOCK_GROUP_DUP)) {
2702 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002703 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002704 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2705 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002706 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002707 }
2708 }
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002709 if (multi_ret && (rw & REQ_WRITE) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002710 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002711 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002712 free_extent_map(em);
2713 kfree(multi);
2714 goto again;
2715 }
Chris Mason593060d2008-03-25 16:50:33 -04002716 stripe_nr = offset;
2717 /*
2718 * stripe_nr counts the total number of stripes we have to stride
2719 * to get to this block
2720 */
2721 do_div(stripe_nr, map->stripe_len);
2722
2723 stripe_offset = stripe_nr * map->stripe_len;
2724 BUG_ON(offset < stripe_offset);
2725
2726 /* stripe_offset is the offset of this block in its stripe*/
2727 stripe_offset = offset - stripe_offset;
2728
Chris Masoncea9e442008-04-09 16:28:12 -04002729 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002730 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002731 BTRFS_BLOCK_GROUP_DUP)) {
2732 /* we limit the length of each bio to what fits in a stripe */
2733 *length = min_t(u64, em->len - offset,
2734 map->stripe_len - stripe_offset);
2735 } else {
2736 *length = em->len - offset;
2737 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002738
2739 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002740 goto out;
2741
Chris Masonf2d8d742008-04-21 10:03:05 -04002742 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002743 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002744 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002745 if (unplug_page || (rw & REQ_WRITE))
Chris Masonf2d8d742008-04-21 10:03:05 -04002746 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002747 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002748 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002749 else {
2750 stripe_index = find_live_mirror(map, 0,
2751 map->num_stripes,
2752 current->pid % map->num_stripes);
2753 }
Chris Mason2fff7342008-04-29 14:12:09 -04002754
Chris Mason611f0e02008-04-03 16:29:03 -04002755 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002756 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04002757 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002758 else if (mirror_num)
2759 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002760
Chris Mason321aecc2008-04-16 10:49:51 -04002761 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2762 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002763
2764 stripe_index = do_div(stripe_nr, factor);
2765 stripe_index *= map->sub_stripes;
2766
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002767 if (unplug_page || (rw & REQ_WRITE))
Chris Masonf2d8d742008-04-21 10:03:05 -04002768 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002769 else if (mirror_num)
2770 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002771 else {
2772 stripe_index = find_live_mirror(map, stripe_index,
2773 map->sub_stripes, stripe_index +
2774 current->pid % map->sub_stripes);
2775 }
Chris Mason8790d502008-04-03 16:29:03 -04002776 } else {
2777 /*
2778 * after this do_div call, stripe_nr is the number of stripes
2779 * on this device we have to walk to find the data, and
2780 * stripe_index is the number of our device in the stripe array
2781 */
2782 stripe_index = do_div(stripe_nr, map->num_stripes);
2783 }
Chris Mason593060d2008-03-25 16:50:33 -04002784 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002785
Chris Masonf2d8d742008-04-21 10:03:05 -04002786 for (i = 0; i < num_stripes; i++) {
2787 if (unplug_page) {
2788 struct btrfs_device *device;
2789 struct backing_dev_info *bdi;
2790
2791 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002792 if (device->bdev) {
2793 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masond3977122009-01-05 21:25:51 -05002794 if (bdi->unplug_io_fn)
Chris Masondfe25022008-05-13 13:46:40 -04002795 bdi->unplug_io_fn(bdi, unplug_page);
Chris Masonf2d8d742008-04-21 10:03:05 -04002796 }
2797 } else {
2798 multi->stripes[i].physical =
2799 map->stripes[stripe_index].physical +
2800 stripe_offset + stripe_nr * map->stripe_len;
2801 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2802 }
Chris Masoncea9e442008-04-09 16:28:12 -04002803 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002804 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002805 if (multi_ret) {
2806 *multi_ret = multi;
2807 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002808 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002809 }
Chris Masoncea9e442008-04-09 16:28:12 -04002810out:
Chris Mason0b86a832008-03-24 15:01:56 -04002811 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002812 return 0;
2813}
2814
Chris Masonf2d8d742008-04-21 10:03:05 -04002815int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2816 u64 logical, u64 *length,
2817 struct btrfs_multi_bio **multi_ret, int mirror_num)
2818{
2819 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2820 mirror_num, NULL);
2821}
2822
Yan Zhenga512bbf2008-12-08 16:46:26 -05002823int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2824 u64 chunk_start, u64 physical, u64 devid,
2825 u64 **logical, int *naddrs, int *stripe_len)
2826{
2827 struct extent_map_tree *em_tree = &map_tree->map_tree;
2828 struct extent_map *em;
2829 struct map_lookup *map;
2830 u64 *buf;
2831 u64 bytenr;
2832 u64 length;
2833 u64 stripe_nr;
2834 int i, j, nr = 0;
2835
Chris Mason890871b2009-09-02 16:24:52 -04002836 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002837 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002838 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002839
2840 BUG_ON(!em || em->start != chunk_start);
2841 map = (struct map_lookup *)em->bdev;
2842
2843 length = em->len;
2844 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2845 do_div(length, map->num_stripes / map->sub_stripes);
2846 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2847 do_div(length, map->num_stripes);
2848
2849 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2850 BUG_ON(!buf);
2851
2852 for (i = 0; i < map->num_stripes; i++) {
2853 if (devid && map->stripes[i].dev->devid != devid)
2854 continue;
2855 if (map->stripes[i].physical > physical ||
2856 map->stripes[i].physical + length <= physical)
2857 continue;
2858
2859 stripe_nr = physical - map->stripes[i].physical;
2860 do_div(stripe_nr, map->stripe_len);
2861
2862 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2863 stripe_nr = stripe_nr * map->num_stripes + i;
2864 do_div(stripe_nr, map->sub_stripes);
2865 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2866 stripe_nr = stripe_nr * map->num_stripes + i;
2867 }
2868 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05002869 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002870 for (j = 0; j < nr; j++) {
2871 if (buf[j] == bytenr)
2872 break;
2873 }
Chris Mason934d3752008-12-08 16:43:10 -05002874 if (j == nr) {
2875 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002876 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05002877 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05002878 }
2879
Yan Zhenga512bbf2008-12-08 16:46:26 -05002880 *logical = buf;
2881 *naddrs = nr;
2882 *stripe_len = map->stripe_len;
2883
2884 free_extent_map(em);
2885 return 0;
2886}
2887
Chris Masonf2d8d742008-04-21 10:03:05 -04002888int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2889 u64 logical, struct page *page)
2890{
2891 u64 length = PAGE_CACHE_SIZE;
2892 return __btrfs_map_block(map_tree, READ, logical, &length,
2893 NULL, 0, page);
2894}
2895
Chris Mason8790d502008-04-03 16:29:03 -04002896static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04002897{
Chris Masoncea9e442008-04-09 16:28:12 -04002898 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002899 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002900
Chris Mason8790d502008-04-03 16:29:03 -04002901 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002902 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002903
Chris Mason7d2b4da2008-08-05 10:13:57 -04002904 if (bio == multi->orig_bio)
2905 is_orig_bio = 1;
2906
Chris Masoncea9e442008-04-09 16:28:12 -04002907 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002908 if (!is_orig_bio) {
2909 bio_put(bio);
2910 bio = multi->orig_bio;
2911 }
Chris Mason8790d502008-04-03 16:29:03 -04002912 bio->bi_private = multi->private;
2913 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002914 /* only send an error to the higher layers if it is
2915 * beyond the tolerance of the multi-bio
2916 */
Chris Mason1259ab72008-05-12 13:39:03 -04002917 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002918 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002919 } else if (err) {
2920 /*
2921 * this bio is actually up to date, we didn't
2922 * go over the max number of errors
2923 */
2924 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002925 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002926 }
Chris Mason8790d502008-04-03 16:29:03 -04002927 kfree(multi);
2928
2929 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04002930 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002931 bio_put(bio);
2932 }
Chris Mason8790d502008-04-03 16:29:03 -04002933}
2934
Chris Mason8b712842008-06-11 16:50:36 -04002935struct async_sched {
2936 struct bio *bio;
2937 int rw;
2938 struct btrfs_fs_info *info;
2939 struct btrfs_work work;
2940};
2941
2942/*
2943 * see run_scheduled_bios for a description of why bios are collected for
2944 * async submit.
2945 *
2946 * This will add one bio to the pending list for a device and make sure
2947 * the work struct is scheduled.
2948 */
Chris Masond3977122009-01-05 21:25:51 -05002949static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04002950 struct btrfs_device *device,
2951 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002952{
2953 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04002954 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04002955
2956 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002957 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6d2008-07-31 16:29:02 -04002958 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002959 submit_bio(rw, bio);
Chris Mason492bb6d2008-07-31 16:29:02 -04002960 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002961 return 0;
2962 }
2963
2964 /*
Chris Mason0986fe92008-08-15 15:34:15 -04002965 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002966 * higher layers. Otherwise, the async bio makes it appear we have
2967 * made progress against dirty pages when we've really just put it
2968 * on a queue for later
2969 */
Chris Mason0986fe92008-08-15 15:34:15 -04002970 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6d2008-07-31 16:29:02 -04002971 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002972 bio->bi_next = NULL;
2973 bio->bi_rw |= rw;
2974
2975 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002976 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04002977 pending_bios = &device->pending_sync_bios;
2978 else
2979 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04002980
Chris Masonffbd5172009-04-20 15:50:09 -04002981 if (pending_bios->tail)
2982 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04002983
Chris Masonffbd5172009-04-20 15:50:09 -04002984 pending_bios->tail = bio;
2985 if (!pending_bios->head)
2986 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04002987 if (device->running_pending)
2988 should_queue = 0;
2989
2990 spin_unlock(&device->io_lock);
2991
2992 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002993 btrfs_queue_worker(&root->fs_info->submit_workers,
2994 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002995 return 0;
2996}
2997
Chris Masonf1885912008-04-09 16:28:12 -04002998int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002999 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003000{
3001 struct btrfs_mapping_tree *map_tree;
3002 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003003 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003004 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003005 u64 length = 0;
3006 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003007 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003008 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003009 int dev_nr = 0;
3010 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003011
Chris Masonf2d8d742008-04-21 10:03:05 -04003012 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003013 map_tree = &root->fs_info->mapping_tree;
3014 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003015
Chris Masonf1885912008-04-09 16:28:12 -04003016 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3017 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003018 BUG_ON(ret);
3019
3020 total_devs = multi->num_stripes;
3021 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003022 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3023 "len %llu\n", (unsigned long long)logical,
3024 (unsigned long long)length,
3025 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003026 BUG();
3027 }
3028 multi->end_io = first_bio->bi_end_io;
3029 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003030 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003031 atomic_set(&multi->stripes_pending, multi->num_stripes);
3032
Chris Masond3977122009-01-05 21:25:51 -05003033 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003034 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003035 if (dev_nr < total_devs - 1) {
3036 bio = bio_clone(first_bio, GFP_NOFS);
3037 BUG_ON(!bio);
3038 } else {
3039 bio = first_bio;
3040 }
3041 bio->bi_private = multi;
3042 bio->bi_end_io = end_bio_multi_stripe;
3043 }
Chris Masoncea9e442008-04-09 16:28:12 -04003044 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3045 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003046 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003047 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003048 if (async_submit)
3049 schedule_bio(root, dev, rw, bio);
3050 else
3051 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003052 } else {
3053 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3054 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003055 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003056 }
Chris Mason8790d502008-04-03 16:29:03 -04003057 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003058 }
Chris Masoncea9e442008-04-09 16:28:12 -04003059 if (total_devs == 1)
3060 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003061 return 0;
3062}
3063
Chris Masona4437552008-04-18 10:29:38 -04003064struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003065 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003066{
Yan Zheng2b820322008-11-17 21:11:30 -05003067 struct btrfs_device *device;
3068 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003069
Yan Zheng2b820322008-11-17 21:11:30 -05003070 cur_devices = root->fs_info->fs_devices;
3071 while (cur_devices) {
3072 if (!fsid ||
3073 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3074 device = __find_device(&cur_devices->devices,
3075 devid, uuid);
3076 if (device)
3077 return device;
3078 }
3079 cur_devices = cur_devices->seed;
3080 }
3081 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003082}
3083
Chris Masondfe25022008-05-13 13:46:40 -04003084static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3085 u64 devid, u8 *dev_uuid)
3086{
3087 struct btrfs_device *device;
3088 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3089
3090 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003091 if (!device)
3092 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003093 list_add(&device->dev_list,
3094 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003095 device->barriers = 1;
3096 device->dev_root = root->fs_info->dev_root;
3097 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003098 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003099 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003100 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003101 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003102 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003103 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003104 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003105 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3106 return device;
3107}
3108
Chris Mason0b86a832008-03-24 15:01:56 -04003109static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3110 struct extent_buffer *leaf,
3111 struct btrfs_chunk *chunk)
3112{
3113 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3114 struct map_lookup *map;
3115 struct extent_map *em;
3116 u64 logical;
3117 u64 length;
3118 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003119 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003120 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003121 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003122 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003123
Chris Masone17cade2008-04-15 15:41:47 -04003124 logical = key->offset;
3125 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003126
Chris Mason890871b2009-09-02 16:24:52 -04003127 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003128 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003129 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003130
3131 /* already mapped? */
3132 if (em && em->start <= logical && em->start + em->len > logical) {
3133 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003134 return 0;
3135 } else if (em) {
3136 free_extent_map(em);
3137 }
Chris Mason0b86a832008-03-24 15:01:56 -04003138
Chris Mason0b86a832008-03-24 15:01:56 -04003139 em = alloc_extent_map(GFP_NOFS);
3140 if (!em)
3141 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003142 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3143 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003144 if (!map) {
3145 free_extent_map(em);
3146 return -ENOMEM;
3147 }
3148
3149 em->bdev = (struct block_device *)map;
3150 em->start = logical;
3151 em->len = length;
3152 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003153 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003154
Chris Mason593060d2008-03-25 16:50:33 -04003155 map->num_stripes = num_stripes;
3156 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3157 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3158 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3159 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3160 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003161 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003162 for (i = 0; i < num_stripes; i++) {
3163 map->stripes[i].physical =
3164 btrfs_stripe_offset_nr(leaf, chunk, i);
3165 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003166 read_extent_buffer(leaf, uuid, (unsigned long)
3167 btrfs_stripe_dev_uuid_nr(chunk, i),
3168 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003169 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3170 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003171 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003172 kfree(map);
3173 free_extent_map(em);
3174 return -EIO;
3175 }
Chris Masondfe25022008-05-13 13:46:40 -04003176 if (!map->stripes[i].dev) {
3177 map->stripes[i].dev =
3178 add_missing_dev(root, devid, uuid);
3179 if (!map->stripes[i].dev) {
3180 kfree(map);
3181 free_extent_map(em);
3182 return -EIO;
3183 }
3184 }
3185 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003186 }
3187
Chris Mason890871b2009-09-02 16:24:52 -04003188 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003189 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003190 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003191 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003192 free_extent_map(em);
3193
3194 return 0;
3195}
3196
3197static int fill_device_from_item(struct extent_buffer *leaf,
3198 struct btrfs_dev_item *dev_item,
3199 struct btrfs_device *device)
3200{
3201 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003202
3203 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003204 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3205 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003206 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3207 device->type = btrfs_device_type(leaf, dev_item);
3208 device->io_align = btrfs_device_io_align(leaf, dev_item);
3209 device->io_width = btrfs_device_io_width(leaf, dev_item);
3210 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003211
3212 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003213 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003214
Chris Mason0b86a832008-03-24 15:01:56 -04003215 return 0;
3216}
3217
Yan Zheng2b820322008-11-17 21:11:30 -05003218static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3219{
3220 struct btrfs_fs_devices *fs_devices;
3221 int ret;
3222
3223 mutex_lock(&uuid_mutex);
3224
3225 fs_devices = root->fs_info->fs_devices->seed;
3226 while (fs_devices) {
3227 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3228 ret = 0;
3229 goto out;
3230 }
3231 fs_devices = fs_devices->seed;
3232 }
3233
3234 fs_devices = find_fsid(fsid);
3235 if (!fs_devices) {
3236 ret = -ENOENT;
3237 goto out;
3238 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003239
3240 fs_devices = clone_fs_devices(fs_devices);
3241 if (IS_ERR(fs_devices)) {
3242 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003243 goto out;
3244 }
3245
Christoph Hellwig97288f22008-12-02 06:36:09 -05003246 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003247 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003248 if (ret)
3249 goto out;
3250
3251 if (!fs_devices->seeding) {
3252 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003253 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003254 ret = -EINVAL;
3255 goto out;
3256 }
3257
3258 fs_devices->seed = root->fs_info->fs_devices->seed;
3259 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003260out:
3261 mutex_unlock(&uuid_mutex);
3262 return ret;
3263}
3264
Chris Mason0d81ba52008-03-24 15:02:07 -04003265static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003266 struct extent_buffer *leaf,
3267 struct btrfs_dev_item *dev_item)
3268{
3269 struct btrfs_device *device;
3270 u64 devid;
3271 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003272 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003273 u8 dev_uuid[BTRFS_UUID_SIZE];
3274
Chris Mason0b86a832008-03-24 15:01:56 -04003275 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003276 read_extent_buffer(leaf, dev_uuid,
3277 (unsigned long)btrfs_device_uuid(dev_item),
3278 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003279 read_extent_buffer(leaf, fs_uuid,
3280 (unsigned long)btrfs_device_fsid(dev_item),
3281 BTRFS_UUID_SIZE);
3282
3283 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3284 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003285 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003286 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003287 }
3288
3289 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3290 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003291 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003292 return -EIO;
3293
3294 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003295 printk(KERN_WARNING "warning devid %llu missing\n",
3296 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003297 device = add_missing_dev(root, devid, dev_uuid);
3298 if (!device)
3299 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003300 } else if (!device->missing) {
3301 /*
3302 * this happens when a device that was properly setup
3303 * in the device info lists suddenly goes bad.
3304 * device->bdev is NULL, and so we have to set
3305 * device->missing to one here
3306 */
3307 root->fs_info->fs_devices->missing_devices++;
3308 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003309 }
3310 }
3311
3312 if (device->fs_devices != root->fs_info->fs_devices) {
3313 BUG_ON(device->writeable);
3314 if (device->generation !=
3315 btrfs_device_generation(leaf, dev_item))
3316 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003317 }
Chris Mason0b86a832008-03-24 15:01:56 -04003318
3319 fill_device_from_item(leaf, dev_item, device);
3320 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003321 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003322 if (device->writeable)
3323 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003324 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003325 return ret;
3326}
3327
Chris Mason0d81ba52008-03-24 15:02:07 -04003328int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3329{
3330 struct btrfs_dev_item *dev_item;
3331
3332 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3333 dev_item);
3334 return read_one_dev(root, buf, dev_item);
3335}
3336
Yan Zhenge4404d62008-12-12 10:03:26 -05003337int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003338{
3339 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003340 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003341 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003342 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003343 u8 *ptr;
3344 unsigned long sb_ptr;
3345 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003346 u32 num_stripes;
3347 u32 array_size;
3348 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003349 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003350 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003351
Yan Zhenge4404d62008-12-12 10:03:26 -05003352 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003353 BTRFS_SUPER_INFO_SIZE);
3354 if (!sb)
3355 return -ENOMEM;
3356 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003357 btrfs_set_buffer_lockdep_class(sb, 0);
3358
Chris Masona061fc82008-05-07 11:43:44 -04003359 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003360 array_size = btrfs_super_sys_array_size(super_copy);
3361
Chris Mason0b86a832008-03-24 15:01:56 -04003362 ptr = super_copy->sys_chunk_array;
3363 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3364 cur = 0;
3365
3366 while (cur < array_size) {
3367 disk_key = (struct btrfs_disk_key *)ptr;
3368 btrfs_disk_key_to_cpu(&key, disk_key);
3369
Chris Masona061fc82008-05-07 11:43:44 -04003370 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003371 sb_ptr += len;
3372 cur += len;
3373
Chris Mason0d81ba52008-03-24 15:02:07 -04003374 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003375 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003376 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003377 if (ret)
3378 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003379 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3380 len = btrfs_chunk_item_size(num_stripes);
3381 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003382 ret = -EIO;
3383 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003384 }
3385 ptr += len;
3386 sb_ptr += len;
3387 cur += len;
3388 }
Chris Masona061fc82008-05-07 11:43:44 -04003389 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003390 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003391}
3392
3393int btrfs_read_chunk_tree(struct btrfs_root *root)
3394{
3395 struct btrfs_path *path;
3396 struct extent_buffer *leaf;
3397 struct btrfs_key key;
3398 struct btrfs_key found_key;
3399 int ret;
3400 int slot;
3401
3402 root = root->fs_info->chunk_root;
3403
3404 path = btrfs_alloc_path();
3405 if (!path)
3406 return -ENOMEM;
3407
3408 /* first we search for all of the device items, and then we
3409 * read in all of the chunk items. This way we can create chunk
3410 * mappings that reference all of the devices that are afound
3411 */
3412 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3413 key.offset = 0;
3414 key.type = 0;
3415again:
3416 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003417 if (ret < 0)
3418 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003419 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003420 leaf = path->nodes[0];
3421 slot = path->slots[0];
3422 if (slot >= btrfs_header_nritems(leaf)) {
3423 ret = btrfs_next_leaf(root, path);
3424 if (ret == 0)
3425 continue;
3426 if (ret < 0)
3427 goto error;
3428 break;
3429 }
3430 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3431 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3432 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3433 break;
3434 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3435 struct btrfs_dev_item *dev_item;
3436 dev_item = btrfs_item_ptr(leaf, slot,
3437 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003438 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003439 if (ret)
3440 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003441 }
3442 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3443 struct btrfs_chunk *chunk;
3444 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3445 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003446 if (ret)
3447 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003448 }
3449 path->slots[0]++;
3450 }
3451 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3452 key.objectid = 0;
3453 btrfs_release_path(root, path);
3454 goto again;
3455 }
Chris Mason0b86a832008-03-24 15:01:56 -04003456 ret = 0;
3457error:
Yan Zheng2b820322008-11-17 21:11:30 -05003458 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003459 return ret;
3460}