blob: 95324e9f928013e5943108948d654b0d9aa298d1 [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 Mason492bb6de2008-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 Masonb248a412008-04-14 09:48:18 -0400401 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400402 device->name = kstrdup(path, GFP_NOFS);
403 if (!device->name) {
404 kfree(device);
405 return -ENOMEM;
406 }
Yan Zheng2b820322008-11-17 21:11:30 -0500407 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400408
409 mutex_lock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400410 list_add(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400411 mutex_unlock(&fs_devices->device_list_mutex);
412
Yan Zheng2b820322008-11-17 21:11:30 -0500413 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400414 fs_devices->num_devices++;
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000415 } else if (strcmp(device->name, path)) {
416 name = kstrdup(path, GFP_NOFS);
417 if (!name)
418 return -ENOMEM;
419 kfree(device->name);
420 device->name = name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400421 }
422
423 if (found_transid > fs_devices->latest_trans) {
424 fs_devices->latest_devid = devid;
425 fs_devices->latest_trans = found_transid;
426 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400427 *fs_devices_ret = fs_devices;
428 return 0;
429}
430
Yan Zhenge4404d62008-12-12 10:03:26 -0500431static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
432{
433 struct btrfs_fs_devices *fs_devices;
434 struct btrfs_device *device;
435 struct btrfs_device *orig_dev;
436
437 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
438 if (!fs_devices)
439 return ERR_PTR(-ENOMEM);
440
441 INIT_LIST_HEAD(&fs_devices->devices);
442 INIT_LIST_HEAD(&fs_devices->alloc_list);
443 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400444 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500445 fs_devices->latest_devid = orig->latest_devid;
446 fs_devices->latest_trans = orig->latest_trans;
447 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
448
Chris Masone5e9a522009-06-10 15:17:02 -0400449 mutex_lock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500450 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
451 device = kzalloc(sizeof(*device), GFP_NOFS);
452 if (!device)
453 goto error;
454
455 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400456 if (!device->name) {
457 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500458 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400459 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500460
461 device->devid = orig_dev->devid;
462 device->work.func = pending_bios_fn;
463 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500464 spin_lock_init(&device->io_lock);
465 INIT_LIST_HEAD(&device->dev_list);
466 INIT_LIST_HEAD(&device->dev_alloc_list);
467
468 list_add(&device->dev_list, &fs_devices->devices);
469 device->fs_devices = fs_devices;
470 fs_devices->num_devices++;
471 }
Chris Masone5e9a522009-06-10 15:17:02 -0400472 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500473 return fs_devices;
474error:
Chris Masone5e9a522009-06-10 15:17:02 -0400475 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500476 free_fs_devices(fs_devices);
477 return ERR_PTR(-ENOMEM);
478}
479
Chris Masondfe25022008-05-13 13:46:40 -0400480int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
481{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500482 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400483
484 mutex_lock(&uuid_mutex);
485again:
Chris Masone5e9a522009-06-10 15:17:02 -0400486 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500487 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500488 if (device->in_fs_metadata)
489 continue;
490
491 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100492 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500493 device->bdev = NULL;
494 fs_devices->open_devices--;
495 }
496 if (device->writeable) {
497 list_del_init(&device->dev_alloc_list);
498 device->writeable = 0;
499 fs_devices->rw_devices--;
500 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500501 list_del_init(&device->dev_list);
502 fs_devices->num_devices--;
503 kfree(device->name);
504 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400505 }
Chris Masone5e9a522009-06-10 15:17:02 -0400506 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -0500507
508 if (fs_devices->seed) {
509 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500510 goto again;
511 }
512
Chris Masondfe25022008-05-13 13:46:40 -0400513 mutex_unlock(&uuid_mutex);
514 return 0;
515}
Chris Masona0af4692008-05-13 16:03:06 -0400516
Yan Zheng2b820322008-11-17 21:11:30 -0500517static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400518{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400519 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500520
Yan Zheng2b820322008-11-17 21:11:30 -0500521 if (--fs_devices->opened > 0)
522 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400523
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500524 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400525 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100526 blkdev_put(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400527 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400528 }
Yan Zheng2b820322008-11-17 21:11:30 -0500529 if (device->writeable) {
530 list_del_init(&device->dev_alloc_list);
531 fs_devices->rw_devices--;
532 }
533
Chris Mason8a4b83c2008-03-24 15:02:07 -0400534 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500535 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400536 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400537 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500538 WARN_ON(fs_devices->open_devices);
539 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500540 fs_devices->opened = 0;
541 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500542
Chris Mason8a4b83c2008-03-24 15:02:07 -0400543 return 0;
544}
545
Yan Zheng2b820322008-11-17 21:11:30 -0500546int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
547{
Yan Zhenge4404d62008-12-12 10:03:26 -0500548 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500549 int ret;
550
551 mutex_lock(&uuid_mutex);
552 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500553 if (!fs_devices->opened) {
554 seed_devices = fs_devices->seed;
555 fs_devices->seed = NULL;
556 }
Yan Zheng2b820322008-11-17 21:11:30 -0500557 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500558
559 while (seed_devices) {
560 fs_devices = seed_devices;
561 seed_devices = fs_devices->seed;
562 __btrfs_close_devices(fs_devices);
563 free_fs_devices(fs_devices);
564 }
Yan Zheng2b820322008-11-17 21:11:30 -0500565 return ret;
566}
567
Yan Zhenge4404d62008-12-12 10:03:26 -0500568static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
569 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400570{
571 struct block_device *bdev;
572 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400573 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400574 struct block_device *latest_bdev = NULL;
575 struct buffer_head *bh;
576 struct btrfs_super_block *disk_super;
577 u64 latest_devid = 0;
578 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400579 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500580 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400581 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400582
Tejun Heod4d77622010-11-13 11:55:18 +0100583 flags |= FMODE_EXCL;
584
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500585 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400586 if (device->bdev)
587 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400588 if (!device->name)
589 continue;
590
Tejun Heod4d77622010-11-13 11:55:18 +0100591 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400592 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500593 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400594 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400595 }
Chris Masona061fc82008-05-07 11:43:44 -0400596 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400597
Yan Zhenga512bbf2008-12-08 16:46:26 -0500598 bh = btrfs_read_dev_super(bdev);
Chris Masona0af4692008-05-13 16:03:06 -0400599 if (!bh)
600 goto error_close;
601
602 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000603 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400604 if (devid != device->devid)
605 goto error_brelse;
606
Yan Zheng2b820322008-11-17 21:11:30 -0500607 if (memcmp(device->uuid, disk_super->dev_item.uuid,
608 BTRFS_UUID_SIZE))
609 goto error_brelse;
610
611 device->generation = btrfs_super_generation(disk_super);
612 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400613 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500614 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400615 latest_bdev = bdev;
616 }
617
Yan Zheng2b820322008-11-17 21:11:30 -0500618 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
619 device->writeable = 0;
620 } else {
621 device->writeable = !bdev_read_only(bdev);
622 seeding = 0;
623 }
624
Chris Mason8a4b83c2008-03-24 15:02:07 -0400625 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400626 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500627 device->mode = flags;
628
Chris Masonc2898112009-06-10 09:51:32 -0400629 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
630 fs_devices->rotating = 1;
631
Chris Masona0af4692008-05-13 16:03:06 -0400632 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500633 if (device->writeable) {
634 fs_devices->rw_devices++;
635 list_add(&device->dev_alloc_list,
636 &fs_devices->alloc_list);
637 }
Chris Masona0af4692008-05-13 16:03:06 -0400638 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400639
Chris Masona0af4692008-05-13 16:03:06 -0400640error_brelse:
641 brelse(bh);
642error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100643 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400644error:
645 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400646 }
Chris Masona0af4692008-05-13 16:03:06 -0400647 if (fs_devices->open_devices == 0) {
648 ret = -EIO;
649 goto out;
650 }
Yan Zheng2b820322008-11-17 21:11:30 -0500651 fs_devices->seeding = seeding;
652 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400653 fs_devices->latest_bdev = latest_bdev;
654 fs_devices->latest_devid = latest_devid;
655 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500656 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400657out:
Yan Zheng2b820322008-11-17 21:11:30 -0500658 return ret;
659}
660
661int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500662 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500663{
664 int ret;
665
666 mutex_lock(&uuid_mutex);
667 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500668 fs_devices->opened++;
669 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500670 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500671 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500672 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400673 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400674 return ret;
675}
676
Christoph Hellwig97288f22008-12-02 06:36:09 -0500677int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400678 struct btrfs_fs_devices **fs_devices_ret)
679{
680 struct btrfs_super_block *disk_super;
681 struct block_device *bdev;
682 struct buffer_head *bh;
683 int ret;
684 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400685 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400686
687 mutex_lock(&uuid_mutex);
688
Tejun Heod4d77622010-11-13 11:55:18 +0100689 flags |= FMODE_EXCL;
690 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400691
692 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400693 ret = PTR_ERR(bdev);
694 goto error;
695 }
696
697 ret = set_blocksize(bdev, 4096);
698 if (ret)
699 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500700 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400701 if (!bh) {
702 ret = -EIO;
703 goto error_close;
704 }
705 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000706 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400707 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400708 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500709 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400710 else {
711 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500712 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400713 *(unsigned long long *)disk_super->fsid,
714 *(unsigned long long *)(disk_super->fsid + 8));
715 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500716 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500717 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400718 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
719
Chris Mason8a4b83c2008-03-24 15:02:07 -0400720 brelse(bh);
721error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100722 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400723error:
724 mutex_unlock(&uuid_mutex);
725 return ret;
726}
Chris Mason0b86a832008-03-24 15:01:56 -0400727
728/*
729 * this uses a pretty simple search, the expectation is that it is
730 * called very infrequently and that a given device has a small number
731 * of extents
732 */
Josef Bacikba1bf482009-09-11 16:11:19 -0400733int find_free_dev_extent(struct btrfs_trans_handle *trans,
734 struct btrfs_device *device, u64 num_bytes,
735 u64 *start, u64 *max_avail)
Chris Mason0b86a832008-03-24 15:01:56 -0400736{
737 struct btrfs_key key;
738 struct btrfs_root *root = device->dev_root;
739 struct btrfs_dev_extent *dev_extent = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500740 struct btrfs_path *path;
Chris Mason0b86a832008-03-24 15:01:56 -0400741 u64 hole_size = 0;
742 u64 last_byte = 0;
743 u64 search_start = 0;
744 u64 search_end = device->total_bytes;
745 int ret;
746 int slot = 0;
747 int start_found;
748 struct extent_buffer *l;
749
Yan Zheng2b820322008-11-17 21:11:30 -0500750 path = btrfs_alloc_path();
751 if (!path)
752 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400753 path->reada = 2;
Yan Zheng2b820322008-11-17 21:11:30 -0500754 start_found = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400755
756 /* FIXME use last free of some kind */
757
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758 /* we don't want to overwrite the superblock on the drive,
759 * so we make sure to start at an offset of at least 1MB
760 */
761 search_start = max((u64)1024 * 1024, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400762
763 if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
764 search_start = max(root->fs_info->alloc_start, search_start);
765
Chris Mason0b86a832008-03-24 15:01:56 -0400766 key.objectid = device->devid;
767 key.offset = search_start;
768 key.type = BTRFS_DEV_EXTENT_KEY;
769 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
770 if (ret < 0)
771 goto error;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400772 if (ret > 0) {
773 ret = btrfs_previous_item(root, path, key.objectid, key.type);
774 if (ret < 0)
775 goto error;
776 if (ret > 0)
777 start_found = 1;
778 }
Chris Mason0b86a832008-03-24 15:01:56 -0400779 l = path->nodes[0];
780 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
781 while (1) {
782 l = path->nodes[0];
783 slot = path->slots[0];
784 if (slot >= btrfs_header_nritems(l)) {
785 ret = btrfs_next_leaf(root, path);
786 if (ret == 0)
787 continue;
788 if (ret < 0)
789 goto error;
790no_more_items:
791 if (!start_found) {
792 if (search_start >= search_end) {
793 ret = -ENOSPC;
794 goto error;
795 }
796 *start = search_start;
797 start_found = 1;
798 goto check_pending;
799 }
800 *start = last_byte > search_start ?
801 last_byte : search_start;
802 if (search_end <= *start) {
803 ret = -ENOSPC;
804 goto error;
805 }
806 goto check_pending;
807 }
808 btrfs_item_key_to_cpu(l, &key, slot);
809
810 if (key.objectid < device->devid)
811 goto next;
812
813 if (key.objectid > device->devid)
814 goto no_more_items;
815
816 if (key.offset >= search_start && key.offset > last_byte &&
817 start_found) {
818 if (last_byte < search_start)
819 last_byte = search_start;
820 hole_size = key.offset - last_byte;
Chris Mason9779b722009-07-24 16:41:41 -0400821
822 if (hole_size > *max_avail)
823 *max_avail = hole_size;
824
Chris Mason0b86a832008-03-24 15:01:56 -0400825 if (key.offset > last_byte &&
826 hole_size >= num_bytes) {
827 *start = last_byte;
828 goto check_pending;
829 }
830 }
Chris Masond3977122009-01-05 21:25:51 -0500831 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400832 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400833
834 start_found = 1;
835 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
836 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
837next:
838 path->slots[0]++;
839 cond_resched();
840 }
841check_pending:
842 /* we have to make sure we didn't find an extent that has already
843 * been allocated by the map tree or the original allocation
844 */
Chris Mason0b86a832008-03-24 15:01:56 -0400845 BUG_ON(*start < search_start);
846
Chris Mason6324fbf2008-03-24 15:01:59 -0400847 if (*start + num_bytes > search_end) {
Chris Mason0b86a832008-03-24 15:01:56 -0400848 ret = -ENOSPC;
849 goto error;
850 }
851 /* check for pending inserts here */
Yan Zheng2b820322008-11-17 21:11:30 -0500852 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400853
854error:
Yan Zheng2b820322008-11-17 21:11:30 -0500855 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -0400856 return ret;
857}
858
Christoph Hellwigb2950862008-12-02 09:54:17 -0500859static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400860 struct btrfs_device *device,
861 u64 start)
862{
863 int ret;
864 struct btrfs_path *path;
865 struct btrfs_root *root = device->dev_root;
866 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400867 struct btrfs_key found_key;
868 struct extent_buffer *leaf = NULL;
869 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400870
871 path = btrfs_alloc_path();
872 if (!path)
873 return -ENOMEM;
874
875 key.objectid = device->devid;
876 key.offset = start;
877 key.type = BTRFS_DEV_EXTENT_KEY;
878
879 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400880 if (ret > 0) {
881 ret = btrfs_previous_item(root, path, key.objectid,
882 BTRFS_DEV_EXTENT_KEY);
883 BUG_ON(ret);
884 leaf = path->nodes[0];
885 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
886 extent = btrfs_item_ptr(leaf, path->slots[0],
887 struct btrfs_dev_extent);
888 BUG_ON(found_key.offset > start || found_key.offset +
889 btrfs_dev_extent_length(leaf, extent) < start);
890 ret = 0;
891 } else if (ret == 0) {
892 leaf = path->nodes[0];
893 extent = btrfs_item_ptr(leaf, path->slots[0],
894 struct btrfs_dev_extent);
895 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400896 BUG_ON(ret);
897
Chris Masondfe25022008-05-13 13:46:40 -0400898 if (device->bytes_used > 0)
899 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -0400900 ret = btrfs_del_item(trans, root, path);
901 BUG_ON(ret);
902
903 btrfs_free_path(path);
904 return ret;
905}
906
Yan Zheng2b820322008-11-17 21:11:30 -0500907int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -0400908 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -0400909 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -0500910 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -0400911{
912 int ret;
913 struct btrfs_path *path;
914 struct btrfs_root *root = device->dev_root;
915 struct btrfs_dev_extent *extent;
916 struct extent_buffer *leaf;
917 struct btrfs_key key;
918
Chris Masondfe25022008-05-13 13:46:40 -0400919 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -0400920 path = btrfs_alloc_path();
921 if (!path)
922 return -ENOMEM;
923
Chris Mason0b86a832008-03-24 15:01:56 -0400924 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500925 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400926 key.type = BTRFS_DEV_EXTENT_KEY;
927 ret = btrfs_insert_empty_item(trans, root, path, &key,
928 sizeof(*extent));
929 BUG_ON(ret);
930
931 leaf = path->nodes[0];
932 extent = btrfs_item_ptr(leaf, path->slots[0],
933 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -0400934 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
935 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
936 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
937
938 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
939 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
940 BTRFS_UUID_SIZE);
941
Chris Mason0b86a832008-03-24 15:01:56 -0400942 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
943 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -0400944 btrfs_free_path(path);
945 return ret;
946}
947
Chris Masona1b32a52008-09-05 16:09:51 -0400948static noinline int find_next_chunk(struct btrfs_root *root,
949 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -0400950{
951 struct btrfs_path *path;
952 int ret;
953 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -0400954 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -0400955 struct btrfs_key found_key;
956
957 path = btrfs_alloc_path();
958 BUG_ON(!path);
959
Chris Masone17cade2008-04-15 15:41:47 -0400960 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -0400961 key.offset = (u64)-1;
962 key.type = BTRFS_CHUNK_ITEM_KEY;
963
964 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
965 if (ret < 0)
966 goto error;
967
968 BUG_ON(ret == 0);
969
970 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
971 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -0400972 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -0400973 } else {
974 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
975 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -0400976 if (found_key.objectid != objectid)
977 *offset = 0;
978 else {
979 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
980 struct btrfs_chunk);
981 *offset = found_key.offset +
982 btrfs_chunk_length(path->nodes[0], chunk);
983 }
Chris Mason0b86a832008-03-24 15:01:56 -0400984 }
985 ret = 0;
986error:
987 btrfs_free_path(path);
988 return ret;
989}
990
Yan Zheng2b820322008-11-17 21:11:30 -0500991static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -0400992{
993 int ret;
994 struct btrfs_key key;
995 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -0500996 struct btrfs_path *path;
997
998 root = root->fs_info->chunk_root;
999
1000 path = btrfs_alloc_path();
1001 if (!path)
1002 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001003
1004 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1005 key.type = BTRFS_DEV_ITEM_KEY;
1006 key.offset = (u64)-1;
1007
1008 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1009 if (ret < 0)
1010 goto error;
1011
1012 BUG_ON(ret == 0);
1013
1014 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1015 BTRFS_DEV_ITEM_KEY);
1016 if (ret) {
1017 *objectid = 1;
1018 } else {
1019 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1020 path->slots[0]);
1021 *objectid = found_key.offset + 1;
1022 }
1023 ret = 0;
1024error:
Yan Zheng2b820322008-11-17 21:11:30 -05001025 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001026 return ret;
1027}
1028
1029/*
1030 * the device information is stored in the chunk root
1031 * the btrfs_device struct should be fully filled in
1032 */
1033int btrfs_add_device(struct btrfs_trans_handle *trans,
1034 struct btrfs_root *root,
1035 struct btrfs_device *device)
1036{
1037 int ret;
1038 struct btrfs_path *path;
1039 struct btrfs_dev_item *dev_item;
1040 struct extent_buffer *leaf;
1041 struct btrfs_key key;
1042 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001043
1044 root = root->fs_info->chunk_root;
1045
1046 path = btrfs_alloc_path();
1047 if (!path)
1048 return -ENOMEM;
1049
Chris Mason0b86a832008-03-24 15:01:56 -04001050 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1051 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001052 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001053
1054 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001055 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001056 if (ret)
1057 goto out;
1058
1059 leaf = path->nodes[0];
1060 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1061
1062 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001063 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001064 btrfs_set_device_type(leaf, dev_item, device->type);
1065 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1066 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1067 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001068 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1069 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001070 btrfs_set_device_group(leaf, dev_item, 0);
1071 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1072 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001073 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001074
Chris Mason0b86a832008-03-24 15:01:56 -04001075 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001076 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001077 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1078 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001079 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001080
Yan Zheng2b820322008-11-17 21:11:30 -05001081 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001082out:
1083 btrfs_free_path(path);
1084 return ret;
1085}
Chris Mason8f18cf12008-04-25 16:53:30 -04001086
Chris Masona061fc82008-05-07 11:43:44 -04001087static int btrfs_rm_dev_item(struct btrfs_root *root,
1088 struct btrfs_device *device)
1089{
1090 int ret;
1091 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001092 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001093 struct btrfs_trans_handle *trans;
1094
1095 root = root->fs_info->chunk_root;
1096
1097 path = btrfs_alloc_path();
1098 if (!path)
1099 return -ENOMEM;
1100
Yan, Zhenga22285a2010-05-16 10:48:46 -04001101 trans = btrfs_start_transaction(root, 0);
Chris Masona061fc82008-05-07 11:43:44 -04001102 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1103 key.type = BTRFS_DEV_ITEM_KEY;
1104 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001105 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001106
1107 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1108 if (ret < 0)
1109 goto out;
1110
1111 if (ret > 0) {
1112 ret = -ENOENT;
1113 goto out;
1114 }
1115
1116 ret = btrfs_del_item(trans, root, path);
1117 if (ret)
1118 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001119out:
1120 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001121 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001122 btrfs_commit_transaction(trans, root);
1123 return ret;
1124}
1125
1126int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1127{
1128 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001129 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001130 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001131 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001132 struct btrfs_super_block *disk_super;
1133 u64 all_avail;
1134 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001135 u64 num_devices;
1136 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001137 int ret = 0;
1138
Chris Masona061fc82008-05-07 11:43:44 -04001139 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001140 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001141
1142 all_avail = root->fs_info->avail_data_alloc_bits |
1143 root->fs_info->avail_system_alloc_bits |
1144 root->fs_info->avail_metadata_alloc_bits;
1145
1146 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001147 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001148 printk(KERN_ERR "btrfs: unable to go below four devices "
1149 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001150 ret = -EINVAL;
1151 goto out;
1152 }
1153
1154 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001155 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001156 printk(KERN_ERR "btrfs: unable to go below two "
1157 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001158 ret = -EINVAL;
1159 goto out;
1160 }
1161
Chris Masondfe25022008-05-13 13:46:40 -04001162 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001163 struct list_head *devices;
1164 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001165
Chris Masondfe25022008-05-13 13:46:40 -04001166 device = NULL;
1167 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001168 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001169 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001170 if (tmp->in_fs_metadata && !tmp->bdev) {
1171 device = tmp;
1172 break;
1173 }
1174 }
Chris Masone5e9a522009-06-10 15:17:02 -04001175 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001176 bdev = NULL;
1177 bh = NULL;
1178 disk_super = NULL;
1179 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001180 printk(KERN_ERR "btrfs: no missing devices found to "
1181 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001182 goto out;
1183 }
Chris Masondfe25022008-05-13 13:46:40 -04001184 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001185 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1186 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001187 if (IS_ERR(bdev)) {
1188 ret = PTR_ERR(bdev);
1189 goto out;
1190 }
1191
Yan Zheng2b820322008-11-17 21:11:30 -05001192 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001193 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001194 if (!bh) {
1195 ret = -EIO;
1196 goto error_close;
1197 }
1198 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001199 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001200 dev_uuid = disk_super->dev_item.uuid;
1201 device = btrfs_find_device(root, devid, dev_uuid,
1202 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001203 if (!device) {
1204 ret = -ENOENT;
1205 goto error_brelse;
1206 }
Chris Masondfe25022008-05-13 13:46:40 -04001207 }
Yan Zheng2b820322008-11-17 21:11:30 -05001208
1209 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001210 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1211 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001212 ret = -EINVAL;
1213 goto error_brelse;
1214 }
1215
1216 if (device->writeable) {
1217 list_del_init(&device->dev_alloc_list);
1218 root->fs_info->fs_devices->rw_devices--;
1219 }
Chris Masona061fc82008-05-07 11:43:44 -04001220
1221 ret = btrfs_shrink_device(device, 0);
1222 if (ret)
1223 goto error_brelse;
1224
Chris Masona061fc82008-05-07 11:43:44 -04001225 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1226 if (ret)
1227 goto error_brelse;
1228
Yan Zheng2b820322008-11-17 21:11:30 -05001229 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001230
1231 /*
1232 * the device list mutex makes sure that we don't change
1233 * the device list while someone else is writing out all
1234 * the device supers.
1235 */
1236 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001237 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001238 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1239
Yan Zhenge4404d62008-12-12 10:03:26 -05001240 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001241
1242 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1243 struct btrfs_device, dev_list);
1244 if (device->bdev == root->fs_info->sb->s_bdev)
1245 root->fs_info->sb->s_bdev = next_device->bdev;
1246 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1247 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1248
Yan Zhenge4404d62008-12-12 10:03:26 -05001249 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001250 blkdev_put(device->bdev, device->mode);
Yan Zhenge4404d62008-12-12 10:03:26 -05001251 device->bdev = NULL;
1252 device->fs_devices->open_devices--;
1253 }
1254
Yan Zheng2b820322008-11-17 21:11:30 -05001255 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1256 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1257
Yan Zhenge4404d62008-12-12 10:03:26 -05001258 if (device->fs_devices->open_devices == 0) {
1259 struct btrfs_fs_devices *fs_devices;
1260 fs_devices = root->fs_info->fs_devices;
1261 while (fs_devices) {
1262 if (fs_devices->seed == device->fs_devices)
1263 break;
1264 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001265 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001266 fs_devices->seed = device->fs_devices->seed;
1267 device->fs_devices->seed = NULL;
1268 __btrfs_close_devices(device->fs_devices);
1269 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001270 }
1271
1272 /*
1273 * at this point, the device is zero sized. We want to
1274 * remove it from the devices list and zero out the old super
1275 */
1276 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001277 /* make sure this device isn't detected as part of
1278 * the FS anymore
1279 */
1280 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1281 set_buffer_dirty(bh);
1282 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001283 }
Chris Masona061fc82008-05-07 11:43:44 -04001284
Chris Masona061fc82008-05-07 11:43:44 -04001285 kfree(device->name);
1286 kfree(device);
1287 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001288
1289error_brelse:
1290 brelse(bh);
1291error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001292 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001293 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001294out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001295 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001296 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001297 return ret;
1298}
1299
Yan Zheng2b820322008-11-17 21:11:30 -05001300/*
1301 * does all the dirty work required for changing file system's UUID.
1302 */
1303static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1304 struct btrfs_root *root)
1305{
1306 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1307 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001308 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001309 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1310 struct btrfs_device *device;
1311 u64 super_flags;
1312
1313 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001314 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001315 return -EINVAL;
1316
Yan Zhenge4404d62008-12-12 10:03:26 -05001317 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1318 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001319 return -ENOMEM;
1320
Yan Zhenge4404d62008-12-12 10:03:26 -05001321 old_devices = clone_fs_devices(fs_devices);
1322 if (IS_ERR(old_devices)) {
1323 kfree(seed_devices);
1324 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001325 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001326
Yan Zheng2b820322008-11-17 21:11:30 -05001327 list_add(&old_devices->list, &fs_uuids);
1328
Yan Zhenge4404d62008-12-12 10:03:26 -05001329 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1330 seed_devices->opened = 1;
1331 INIT_LIST_HEAD(&seed_devices->devices);
1332 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001333 mutex_init(&seed_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001334 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1335 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1336 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1337 device->fs_devices = seed_devices;
1338 }
1339
Yan Zheng2b820322008-11-17 21:11:30 -05001340 fs_devices->seeding = 0;
1341 fs_devices->num_devices = 0;
1342 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001343 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001344
1345 generate_random_uuid(fs_devices->fsid);
1346 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1347 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1348 super_flags = btrfs_super_flags(disk_super) &
1349 ~BTRFS_SUPER_FLAG_SEEDING;
1350 btrfs_set_super_flags(disk_super, super_flags);
1351
1352 return 0;
1353}
1354
1355/*
1356 * strore the expected generation for seed devices in device items.
1357 */
1358static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1359 struct btrfs_root *root)
1360{
1361 struct btrfs_path *path;
1362 struct extent_buffer *leaf;
1363 struct btrfs_dev_item *dev_item;
1364 struct btrfs_device *device;
1365 struct btrfs_key key;
1366 u8 fs_uuid[BTRFS_UUID_SIZE];
1367 u8 dev_uuid[BTRFS_UUID_SIZE];
1368 u64 devid;
1369 int ret;
1370
1371 path = btrfs_alloc_path();
1372 if (!path)
1373 return -ENOMEM;
1374
1375 root = root->fs_info->chunk_root;
1376 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1377 key.offset = 0;
1378 key.type = BTRFS_DEV_ITEM_KEY;
1379
1380 while (1) {
1381 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1382 if (ret < 0)
1383 goto error;
1384
1385 leaf = path->nodes[0];
1386next_slot:
1387 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1388 ret = btrfs_next_leaf(root, path);
1389 if (ret > 0)
1390 break;
1391 if (ret < 0)
1392 goto error;
1393 leaf = path->nodes[0];
1394 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1395 btrfs_release_path(root, path);
1396 continue;
1397 }
1398
1399 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1400 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1401 key.type != BTRFS_DEV_ITEM_KEY)
1402 break;
1403
1404 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1405 struct btrfs_dev_item);
1406 devid = btrfs_device_id(leaf, dev_item);
1407 read_extent_buffer(leaf, dev_uuid,
1408 (unsigned long)btrfs_device_uuid(dev_item),
1409 BTRFS_UUID_SIZE);
1410 read_extent_buffer(leaf, fs_uuid,
1411 (unsigned long)btrfs_device_fsid(dev_item),
1412 BTRFS_UUID_SIZE);
1413 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1414 BUG_ON(!device);
1415
1416 if (device->fs_devices->seeding) {
1417 btrfs_set_device_generation(leaf, dev_item,
1418 device->generation);
1419 btrfs_mark_buffer_dirty(leaf);
1420 }
1421
1422 path->slots[0]++;
1423 goto next_slot;
1424 }
1425 ret = 0;
1426error:
1427 btrfs_free_path(path);
1428 return ret;
1429}
1430
Chris Mason788f20e2008-04-28 15:29:42 -04001431int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1432{
1433 struct btrfs_trans_handle *trans;
1434 struct btrfs_device *device;
1435 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001436 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001437 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001438 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001439 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001440 int ret = 0;
1441
Yan Zheng2b820322008-11-17 21:11:30 -05001442 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1443 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001444
Tejun Heod4d77622010-11-13 11:55:18 +01001445 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1446 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001447 if (IS_ERR(bdev))
1448 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001449
Yan Zheng2b820322008-11-17 21:11:30 -05001450 if (root->fs_info->fs_devices->seeding) {
1451 seeding_dev = 1;
1452 down_write(&sb->s_umount);
1453 mutex_lock(&uuid_mutex);
1454 }
1455
Chris Mason8c8bee12008-09-29 11:19:10 -04001456 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001457 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001458
Chris Mason788f20e2008-04-28 15:29:42 -04001459 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001460 /*
1461 * we have the volume lock, so we don't need the extra
1462 * device list mutex while reading the list here.
1463 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001464 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001465 if (device->bdev == bdev) {
1466 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001467 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001468 }
1469 }
1470
1471 device = kzalloc(sizeof(*device), GFP_NOFS);
1472 if (!device) {
1473 /* we can safely leave the fs_devices entry around */
1474 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001475 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001476 }
1477
Chris Mason788f20e2008-04-28 15:29:42 -04001478 device->name = kstrdup(device_path, GFP_NOFS);
1479 if (!device->name) {
1480 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001481 ret = -ENOMEM;
1482 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001483 }
Yan Zheng2b820322008-11-17 21:11:30 -05001484
1485 ret = find_next_devid(root, &device->devid);
1486 if (ret) {
1487 kfree(device);
1488 goto error;
1489 }
1490
Yan, Zhenga22285a2010-05-16 10:48:46 -04001491 trans = btrfs_start_transaction(root, 0);
Yan Zheng2b820322008-11-17 21:11:30 -05001492 lock_chunks(root);
1493
Yan Zheng2b820322008-11-17 21:11:30 -05001494 device->writeable = 1;
1495 device->work.func = pending_bios_fn;
1496 generate_random_uuid(device->uuid);
1497 spin_lock_init(&device->io_lock);
1498 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001499 device->io_width = root->sectorsize;
1500 device->io_align = root->sectorsize;
1501 device->sector_size = root->sectorsize;
1502 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001503 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001504 device->dev_root = root->fs_info->dev_root;
1505 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001506 device->in_fs_metadata = 1;
Chris Mason15916de2008-11-19 21:17:22 -05001507 device->mode = 0;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001508 set_blocksize(device->bdev, 4096);
1509
Yan Zheng2b820322008-11-17 21:11:30 -05001510 if (seeding_dev) {
1511 sb->s_flags &= ~MS_RDONLY;
1512 ret = btrfs_prepare_sprout(trans, root);
1513 BUG_ON(ret);
1514 }
1515
1516 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001517
1518 /*
1519 * we don't want write_supers to jump in here with our device
1520 * half setup
1521 */
1522 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001523 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1524 list_add(&device->dev_alloc_list,
1525 &root->fs_info->fs_devices->alloc_list);
1526 root->fs_info->fs_devices->num_devices++;
1527 root->fs_info->fs_devices->open_devices++;
1528 root->fs_info->fs_devices->rw_devices++;
1529 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1530
Chris Masonc2898112009-06-10 09:51:32 -04001531 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1532 root->fs_info->fs_devices->rotating = 1;
1533
Chris Mason788f20e2008-04-28 15:29:42 -04001534 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1535 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1536 total_bytes + device->total_bytes);
1537
1538 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1539 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1540 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001541 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001542
Yan Zheng2b820322008-11-17 21:11:30 -05001543 if (seeding_dev) {
1544 ret = init_first_rw_device(trans, root, device);
1545 BUG_ON(ret);
1546 ret = btrfs_finish_sprout(trans, root);
1547 BUG_ON(ret);
1548 } else {
1549 ret = btrfs_add_device(trans, root, device);
1550 }
1551
Chris Mason913d9522009-03-10 13:17:18 -04001552 /*
1553 * we've got more storage, clear any full flags on the space
1554 * infos
1555 */
1556 btrfs_clear_space_info_full(root->fs_info);
1557
Chris Mason7d9eb122008-07-08 14:19:17 -04001558 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001559 btrfs_commit_transaction(trans, root);
1560
1561 if (seeding_dev) {
1562 mutex_unlock(&uuid_mutex);
1563 up_write(&sb->s_umount);
1564
1565 ret = btrfs_relocate_sys_chunks(root);
1566 BUG_ON(ret);
1567 }
1568out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001569 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001570 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001571error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001572 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001573 if (seeding_dev) {
1574 mutex_unlock(&uuid_mutex);
1575 up_write(&sb->s_umount);
1576 }
Chris Mason788f20e2008-04-28 15:29:42 -04001577 goto out;
1578}
1579
Chris Masond3977122009-01-05 21:25:51 -05001580static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1581 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001582{
1583 int ret;
1584 struct btrfs_path *path;
1585 struct btrfs_root *root;
1586 struct btrfs_dev_item *dev_item;
1587 struct extent_buffer *leaf;
1588 struct btrfs_key key;
1589
1590 root = device->dev_root->fs_info->chunk_root;
1591
1592 path = btrfs_alloc_path();
1593 if (!path)
1594 return -ENOMEM;
1595
1596 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1597 key.type = BTRFS_DEV_ITEM_KEY;
1598 key.offset = device->devid;
1599
1600 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1601 if (ret < 0)
1602 goto out;
1603
1604 if (ret > 0) {
1605 ret = -ENOENT;
1606 goto out;
1607 }
1608
1609 leaf = path->nodes[0];
1610 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1611
1612 btrfs_set_device_id(leaf, dev_item, device->devid);
1613 btrfs_set_device_type(leaf, dev_item, device->type);
1614 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1615 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1616 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001617 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001618 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1619 btrfs_mark_buffer_dirty(leaf);
1620
1621out:
1622 btrfs_free_path(path);
1623 return ret;
1624}
1625
Chris Mason7d9eb122008-07-08 14:19:17 -04001626static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001627 struct btrfs_device *device, u64 new_size)
1628{
1629 struct btrfs_super_block *super_copy =
1630 &device->dev_root->fs_info->super_copy;
1631 u64 old_total = btrfs_super_total_bytes(super_copy);
1632 u64 diff = new_size - device->total_bytes;
1633
Yan Zheng2b820322008-11-17 21:11:30 -05001634 if (!device->writeable)
1635 return -EACCES;
1636 if (new_size <= device->total_bytes)
1637 return -EINVAL;
1638
Chris Mason8f18cf12008-04-25 16:53:30 -04001639 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001640 device->fs_devices->total_rw_bytes += diff;
1641
1642 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001643 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001644 btrfs_clear_space_info_full(device->dev_root->fs_info);
1645
Chris Mason8f18cf12008-04-25 16:53:30 -04001646 return btrfs_update_device(trans, device);
1647}
1648
Chris Mason7d9eb122008-07-08 14:19:17 -04001649int btrfs_grow_device(struct btrfs_trans_handle *trans,
1650 struct btrfs_device *device, u64 new_size)
1651{
1652 int ret;
1653 lock_chunks(device->dev_root);
1654 ret = __btrfs_grow_device(trans, device, new_size);
1655 unlock_chunks(device->dev_root);
1656 return ret;
1657}
1658
Chris Mason8f18cf12008-04-25 16:53:30 -04001659static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1660 struct btrfs_root *root,
1661 u64 chunk_tree, u64 chunk_objectid,
1662 u64 chunk_offset)
1663{
1664 int ret;
1665 struct btrfs_path *path;
1666 struct btrfs_key key;
1667
1668 root = root->fs_info->chunk_root;
1669 path = btrfs_alloc_path();
1670 if (!path)
1671 return -ENOMEM;
1672
1673 key.objectid = chunk_objectid;
1674 key.offset = chunk_offset;
1675 key.type = BTRFS_CHUNK_ITEM_KEY;
1676
1677 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1678 BUG_ON(ret);
1679
1680 ret = btrfs_del_item(trans, root, path);
1681 BUG_ON(ret);
1682
1683 btrfs_free_path(path);
1684 return 0;
1685}
1686
Christoph Hellwigb2950862008-12-02 09:54:17 -05001687static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001688 chunk_offset)
1689{
1690 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1691 struct btrfs_disk_key *disk_key;
1692 struct btrfs_chunk *chunk;
1693 u8 *ptr;
1694 int ret = 0;
1695 u32 num_stripes;
1696 u32 array_size;
1697 u32 len = 0;
1698 u32 cur;
1699 struct btrfs_key key;
1700
1701 array_size = btrfs_super_sys_array_size(super_copy);
1702
1703 ptr = super_copy->sys_chunk_array;
1704 cur = 0;
1705
1706 while (cur < array_size) {
1707 disk_key = (struct btrfs_disk_key *)ptr;
1708 btrfs_disk_key_to_cpu(&key, disk_key);
1709
1710 len = sizeof(*disk_key);
1711
1712 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1713 chunk = (struct btrfs_chunk *)(ptr + len);
1714 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1715 len += btrfs_chunk_item_size(num_stripes);
1716 } else {
1717 ret = -EIO;
1718 break;
1719 }
1720 if (key.objectid == chunk_objectid &&
1721 key.offset == chunk_offset) {
1722 memmove(ptr, ptr + len, array_size - (cur + len));
1723 array_size -= len;
1724 btrfs_set_super_sys_array_size(super_copy, array_size);
1725 } else {
1726 ptr += len;
1727 cur += len;
1728 }
1729 }
1730 return ret;
1731}
1732
Christoph Hellwigb2950862008-12-02 09:54:17 -05001733static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001734 u64 chunk_tree, u64 chunk_objectid,
1735 u64 chunk_offset)
1736{
1737 struct extent_map_tree *em_tree;
1738 struct btrfs_root *extent_root;
1739 struct btrfs_trans_handle *trans;
1740 struct extent_map *em;
1741 struct map_lookup *map;
1742 int ret;
1743 int i;
1744
1745 root = root->fs_info->chunk_root;
1746 extent_root = root->fs_info->extent_root;
1747 em_tree = &root->fs_info->mapping_tree.map_tree;
1748
Josef Bacikba1bf482009-09-11 16:11:19 -04001749 ret = btrfs_can_relocate(extent_root, chunk_offset);
1750 if (ret)
1751 return -ENOSPC;
1752
Chris Mason8f18cf12008-04-25 16:53:30 -04001753 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001754 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001755 if (ret)
1756 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001757
Yan, Zhenga22285a2010-05-16 10:48:46 -04001758 trans = btrfs_start_transaction(root, 0);
Chris Mason8f18cf12008-04-25 16:53:30 -04001759 BUG_ON(!trans);
1760
Chris Mason7d9eb122008-07-08 14:19:17 -04001761 lock_chunks(root);
1762
Chris Mason8f18cf12008-04-25 16:53:30 -04001763 /*
1764 * step two, delete the device extents and the
1765 * chunk tree entries
1766 */
Chris Mason890871b2009-09-02 16:24:52 -04001767 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001768 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001769 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001770
Chris Masona061fc82008-05-07 11:43:44 -04001771 BUG_ON(em->start > chunk_offset ||
1772 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001773 map = (struct map_lookup *)em->bdev;
1774
1775 for (i = 0; i < map->num_stripes; i++) {
1776 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1777 map->stripes[i].physical);
1778 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001779
Chris Masondfe25022008-05-13 13:46:40 -04001780 if (map->stripes[i].dev) {
1781 ret = btrfs_update_device(trans, map->stripes[i].dev);
1782 BUG_ON(ret);
1783 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001784 }
1785 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1786 chunk_offset);
1787
1788 BUG_ON(ret);
1789
1790 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1791 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1792 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001793 }
1794
Zheng Yan1a40e232008-09-26 10:09:34 -04001795 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1796 BUG_ON(ret);
1797
Chris Mason890871b2009-09-02 16:24:52 -04001798 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001799 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001800 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001801
Chris Mason8f18cf12008-04-25 16:53:30 -04001802 kfree(map);
1803 em->bdev = NULL;
1804
1805 /* once for the tree */
1806 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001807 /* once for us */
1808 free_extent_map(em);
1809
Chris Mason7d9eb122008-07-08 14:19:17 -04001810 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001811 btrfs_end_transaction(trans, root);
1812 return 0;
1813}
1814
Yan Zheng2b820322008-11-17 21:11:30 -05001815static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1816{
1817 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1818 struct btrfs_path *path;
1819 struct extent_buffer *leaf;
1820 struct btrfs_chunk *chunk;
1821 struct btrfs_key key;
1822 struct btrfs_key found_key;
1823 u64 chunk_tree = chunk_root->root_key.objectid;
1824 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001825 bool retried = false;
1826 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001827 int ret;
1828
1829 path = btrfs_alloc_path();
1830 if (!path)
1831 return -ENOMEM;
1832
Josef Bacikba1bf482009-09-11 16:11:19 -04001833again:
Yan Zheng2b820322008-11-17 21:11:30 -05001834 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1835 key.offset = (u64)-1;
1836 key.type = BTRFS_CHUNK_ITEM_KEY;
1837
1838 while (1) {
1839 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1840 if (ret < 0)
1841 goto error;
1842 BUG_ON(ret == 0);
1843
1844 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1845 key.type);
1846 if (ret < 0)
1847 goto error;
1848 if (ret > 0)
1849 break;
1850
1851 leaf = path->nodes[0];
1852 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1853
1854 chunk = btrfs_item_ptr(leaf, path->slots[0],
1855 struct btrfs_chunk);
1856 chunk_type = btrfs_chunk_type(leaf, chunk);
1857 btrfs_release_path(chunk_root, path);
1858
1859 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1860 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1861 found_key.objectid,
1862 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001863 if (ret == -ENOSPC)
1864 failed++;
1865 else if (ret)
1866 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001867 }
1868
1869 if (found_key.offset == 0)
1870 break;
1871 key.offset = found_key.offset - 1;
1872 }
1873 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04001874 if (failed && !retried) {
1875 failed = 0;
1876 retried = true;
1877 goto again;
1878 } else if (failed && retried) {
1879 WARN_ON(1);
1880 ret = -ENOSPC;
1881 }
Yan Zheng2b820322008-11-17 21:11:30 -05001882error:
1883 btrfs_free_path(path);
1884 return ret;
1885}
1886
Chris Masonec44a352008-04-28 15:29:52 -04001887static u64 div_factor(u64 num, int factor)
1888{
1889 if (factor == 10)
1890 return num;
1891 num *= factor;
1892 do_div(num, 10);
1893 return num;
1894}
1895
Chris Masonec44a352008-04-28 15:29:52 -04001896int btrfs_balance(struct btrfs_root *dev_root)
1897{
1898 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04001899 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1900 struct btrfs_device *device;
1901 u64 old_size;
1902 u64 size_to_free;
1903 struct btrfs_path *path;
1904 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04001905 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1906 struct btrfs_trans_handle *trans;
1907 struct btrfs_key found_key;
1908
Yan Zheng2b820322008-11-17 21:11:30 -05001909 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1910 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04001911
Chris Mason7d9eb122008-07-08 14:19:17 -04001912 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001913 dev_root = dev_root->fs_info->dev_root;
1914
Chris Masonec44a352008-04-28 15:29:52 -04001915 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001916 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04001917 old_size = device->total_bytes;
1918 size_to_free = div_factor(old_size, 1);
1919 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05001920 if (!device->writeable ||
1921 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04001922 continue;
1923
1924 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04001925 if (ret == -ENOSPC)
1926 break;
Chris Masonec44a352008-04-28 15:29:52 -04001927 BUG_ON(ret);
1928
Yan, Zhenga22285a2010-05-16 10:48:46 -04001929 trans = btrfs_start_transaction(dev_root, 0);
Chris Masonec44a352008-04-28 15:29:52 -04001930 BUG_ON(!trans);
1931
1932 ret = btrfs_grow_device(trans, device, old_size);
1933 BUG_ON(ret);
1934
1935 btrfs_end_transaction(trans, dev_root);
1936 }
1937
1938 /* step two, relocate all the chunks */
1939 path = btrfs_alloc_path();
1940 BUG_ON(!path);
1941
1942 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1943 key.offset = (u64)-1;
1944 key.type = BTRFS_CHUNK_ITEM_KEY;
1945
Chris Masond3977122009-01-05 21:25:51 -05001946 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04001947 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1948 if (ret < 0)
1949 goto error;
1950
1951 /*
1952 * this shouldn't happen, it means the last relocate
1953 * failed
1954 */
1955 if (ret == 0)
1956 break;
1957
1958 ret = btrfs_previous_item(chunk_root, path, 0,
1959 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04001960 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04001961 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001962
Chris Masonec44a352008-04-28 15:29:52 -04001963 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1964 path->slots[0]);
1965 if (found_key.objectid != key.objectid)
1966 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04001967
Chris Masonec44a352008-04-28 15:29:52 -04001968 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04001969 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04001970 break;
1971
Chris Mason7d9eb122008-07-08 14:19:17 -04001972 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04001973 ret = btrfs_relocate_chunk(chunk_root,
1974 chunk_root->root_key.objectid,
1975 found_key.objectid,
1976 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001977 BUG_ON(ret && ret != -ENOSPC);
1978 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04001979 }
1980 ret = 0;
1981error:
1982 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001983 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04001984 return ret;
1985}
1986
Chris Mason8f18cf12008-04-25 16:53:30 -04001987/*
1988 * shrinking a device means finding all of the device extents past
1989 * the new size, and then following the back refs to the chunks.
1990 * The chunk relocation code actually frees the device extent
1991 */
1992int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1993{
1994 struct btrfs_trans_handle *trans;
1995 struct btrfs_root *root = device->dev_root;
1996 struct btrfs_dev_extent *dev_extent = NULL;
1997 struct btrfs_path *path;
1998 u64 length;
1999 u64 chunk_tree;
2000 u64 chunk_objectid;
2001 u64 chunk_offset;
2002 int ret;
2003 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002004 int failed = 0;
2005 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002006 struct extent_buffer *l;
2007 struct btrfs_key key;
2008 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2009 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002010 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002011 u64 diff = device->total_bytes - new_size;
2012
Yan Zheng2b820322008-11-17 21:11:30 -05002013 if (new_size >= device->total_bytes)
2014 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002015
2016 path = btrfs_alloc_path();
2017 if (!path)
2018 return -ENOMEM;
2019
Chris Mason8f18cf12008-04-25 16:53:30 -04002020 path->reada = 2;
2021
Chris Mason7d9eb122008-07-08 14:19:17 -04002022 lock_chunks(root);
2023
Chris Mason8f18cf12008-04-25 16:53:30 -04002024 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002025 if (device->writeable)
2026 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002027 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002028
Josef Bacikba1bf482009-09-11 16:11:19 -04002029again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002030 key.objectid = device->devid;
2031 key.offset = (u64)-1;
2032 key.type = BTRFS_DEV_EXTENT_KEY;
2033
2034 while (1) {
2035 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2036 if (ret < 0)
2037 goto done;
2038
2039 ret = btrfs_previous_item(root, path, 0, key.type);
2040 if (ret < 0)
2041 goto done;
2042 if (ret) {
2043 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002044 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002045 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002046 }
2047
2048 l = path->nodes[0];
2049 slot = path->slots[0];
2050 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2051
Josef Bacikba1bf482009-09-11 16:11:19 -04002052 if (key.objectid != device->devid) {
2053 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002054 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002055 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002056
2057 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2058 length = btrfs_dev_extent_length(l, dev_extent);
2059
Josef Bacikba1bf482009-09-11 16:11:19 -04002060 if (key.offset + length <= new_size) {
2061 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002062 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002063 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002064
2065 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2066 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2067 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2068 btrfs_release_path(root, path);
2069
2070 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2071 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002072 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002073 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002074 if (ret == -ENOSPC)
2075 failed++;
2076 key.offset -= 1;
2077 }
2078
2079 if (failed && !retried) {
2080 failed = 0;
2081 retried = true;
2082 goto again;
2083 } else if (failed && retried) {
2084 ret = -ENOSPC;
2085 lock_chunks(root);
2086
2087 device->total_bytes = old_size;
2088 if (device->writeable)
2089 device->fs_devices->total_rw_bytes += diff;
2090 unlock_chunks(root);
2091 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002092 }
2093
Chris Balld6397ba2009-04-27 07:29:03 -04002094 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002095 trans = btrfs_start_transaction(root, 0);
Chris Balld6397ba2009-04-27 07:29:03 -04002096 lock_chunks(root);
2097
2098 device->disk_total_bytes = new_size;
2099 /* Now btrfs_update_device() will change the on-disk size. */
2100 ret = btrfs_update_device(trans, device);
2101 if (ret) {
2102 unlock_chunks(root);
2103 btrfs_end_transaction(trans, root);
2104 goto done;
2105 }
2106 WARN_ON(diff > old_total);
2107 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2108 unlock_chunks(root);
2109 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002110done:
2111 btrfs_free_path(path);
2112 return ret;
2113}
2114
Christoph Hellwigb2950862008-12-02 09:54:17 -05002115static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002116 struct btrfs_root *root,
2117 struct btrfs_key *key,
2118 struct btrfs_chunk *chunk, int item_size)
2119{
2120 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2121 struct btrfs_disk_key disk_key;
2122 u32 array_size;
2123 u8 *ptr;
2124
2125 array_size = btrfs_super_sys_array_size(super_copy);
2126 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2127 return -EFBIG;
2128
2129 ptr = super_copy->sys_chunk_array + array_size;
2130 btrfs_cpu_key_to_disk(&disk_key, key);
2131 memcpy(ptr, &disk_key, sizeof(disk_key));
2132 ptr += sizeof(disk_key);
2133 memcpy(ptr, chunk, item_size);
2134 item_size += sizeof(disk_key);
2135 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2136 return 0;
2137}
2138
Chris Masond3977122009-01-05 21:25:51 -05002139static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002140 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002141{
2142 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2143 return calc_size;
2144 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2145 return calc_size * (num_stripes / sub_stripes);
2146 else
2147 return calc_size * num_stripes;
2148}
2149
Yan Zheng2b820322008-11-17 21:11:30 -05002150static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2151 struct btrfs_root *extent_root,
2152 struct map_lookup **map_ret,
2153 u64 *num_bytes, u64 *stripe_size,
2154 u64 start, u64 type)
Chris Mason0b86a832008-03-24 15:01:56 -04002155{
Chris Mason593060d2008-03-25 16:50:33 -04002156 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason0b86a832008-03-24 15:01:56 -04002157 struct btrfs_device *device = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05002158 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Chris Mason6324fbf2008-03-24 15:01:59 -04002159 struct list_head *cur;
Yan Zheng2b820322008-11-17 21:11:30 -05002160 struct map_lookup *map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002161 struct extent_map_tree *em_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04002162 struct extent_map *em;
Yan Zheng2b820322008-11-17 21:11:30 -05002163 struct list_head private_devs;
Chris Masona40a90a2008-04-18 11:55:51 -04002164 int min_stripe_size = 1 * 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04002165 u64 calc_size = 1024 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002166 u64 max_chunk_size = calc_size;
2167 u64 min_free;
Chris Mason6324fbf2008-03-24 15:01:59 -04002168 u64 avail;
2169 u64 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002170 u64 dev_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04002171 int num_stripes = 1;
Chris Masona40a90a2008-04-18 11:55:51 -04002172 int min_stripes = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002173 int sub_stripes = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002174 int looped = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002175 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04002176 int index;
Chris Mason593060d2008-03-25 16:50:33 -04002177 int stripe_len = 64 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -04002178
Chris Masonec44a352008-04-28 15:29:52 -04002179 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2180 (type & BTRFS_BLOCK_GROUP_DUP)) {
2181 WARN_ON(1);
2182 type &= ~BTRFS_BLOCK_GROUP_DUP;
2183 }
Yan Zheng2b820322008-11-17 21:11:30 -05002184 if (list_empty(&fs_devices->alloc_list))
Chris Mason6324fbf2008-03-24 15:01:59 -04002185 return -ENOSPC;
Chris Mason593060d2008-03-25 16:50:33 -04002186
Chris Masona40a90a2008-04-18 11:55:51 -04002187 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002188 num_stripes = fs_devices->rw_devices;
Chris Masona40a90a2008-04-18 11:55:51 -04002189 min_stripes = 2;
2190 }
2191 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Chris Mason611f0e02008-04-03 16:29:03 -04002192 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002193 min_stripes = 2;
2194 }
Chris Mason8790d502008-04-03 16:29:03 -04002195 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Zhao Leif3eae7e2010-03-25 12:32:59 +00002196 if (fs_devices->rw_devices < 2)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002197 return -ENOSPC;
Zhao Leif3eae7e2010-03-25 12:32:59 +00002198 num_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002199 min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002200 }
Chris Mason321aecc2008-04-16 10:49:51 -04002201 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Yan Zheng2b820322008-11-17 21:11:30 -05002202 num_stripes = fs_devices->rw_devices;
Chris Mason321aecc2008-04-16 10:49:51 -04002203 if (num_stripes < 4)
2204 return -ENOSPC;
2205 num_stripes &= ~(u32)1;
2206 sub_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002207 min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002208 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002209
2210 if (type & BTRFS_BLOCK_GROUP_DATA) {
2211 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002212 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002213 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik83d3c962009-12-07 21:45:59 +00002214 max_chunk_size = 256 * 1024 * 1024;
Chris Masona40a90a2008-04-18 11:55:51 -04002215 min_stripe_size = 32 * 1024 * 1024;
2216 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2217 calc_size = 8 * 1024 * 1024;
2218 max_chunk_size = calc_size * 2;
2219 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002220 }
2221
Yan Zheng2b820322008-11-17 21:11:30 -05002222 /* we don't want a chunk larger than 10% of writeable space */
2223 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2224 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002225
Chris Masona40a90a2008-04-18 11:55:51 -04002226again:
Chris Mason9779b722009-07-24 16:41:41 -04002227 max_avail = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002228 if (!map || map->num_stripes != num_stripes) {
2229 kfree(map);
2230 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2231 if (!map)
2232 return -ENOMEM;
2233 map->num_stripes = num_stripes;
2234 }
2235
Chris Mason9b3f68b2008-04-18 10:29:51 -04002236 if (calc_size * num_stripes > max_chunk_size) {
2237 calc_size = max_chunk_size;
2238 do_div(calc_size, num_stripes);
2239 do_div(calc_size, stripe_len);
2240 calc_size *= stripe_len;
2241 }
Josef Bacik0cad8a112010-03-17 20:45:56 +00002242
Chris Mason9b3f68b2008-04-18 10:29:51 -04002243 /* we don't want tiny stripes */
Josef Bacik0cad8a112010-03-17 20:45:56 +00002244 if (!looped)
2245 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002246
Chris Mason9f680ce2010-04-06 09:37:47 -04002247 /*
2248 * we're about to do_div by the stripe_len so lets make sure
2249 * we end up with something bigger than a stripe
2250 */
2251 calc_size = max_t(u64, calc_size, stripe_len * 4);
2252
Chris Mason9b3f68b2008-04-18 10:29:51 -04002253 do_div(calc_size, stripe_len);
2254 calc_size *= stripe_len;
2255
Yan Zheng2b820322008-11-17 21:11:30 -05002256 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002257 index = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002258
2259 if (type & BTRFS_BLOCK_GROUP_DUP)
2260 min_free = calc_size * 2;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002261 else
2262 min_free = calc_size;
Chris Mason611f0e02008-04-03 16:29:03 -04002263
Josef Bacik0f9dd462008-09-23 13:14:11 -04002264 /*
2265 * we add 1MB because we never use the first 1MB of the device, unless
2266 * we've looped, then we are likely allocating the maximum amount of
2267 * space left already
2268 */
2269 if (!looped)
2270 min_free += 1024 * 1024;
Chris Masonad5bd912008-04-21 08:28:10 -04002271
Yan Zheng2b820322008-11-17 21:11:30 -05002272 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002273 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002274 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002275 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002276 if (device->total_bytes > device->bytes_used)
2277 avail = device->total_bytes - device->bytes_used;
2278 else
2279 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002280 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002281
Chris Masondfe25022008-05-13 13:46:40 -04002282 if (device->in_fs_metadata && avail >= min_free) {
Yan Zheng2b820322008-11-17 21:11:30 -05002283 ret = find_free_dev_extent(trans, device,
Chris Mason9779b722009-07-24 16:41:41 -04002284 min_free, &dev_offset,
2285 &max_avail);
Chris Mason8f18cf12008-04-25 16:53:30 -04002286 if (ret == 0) {
2287 list_move_tail(&device->dev_alloc_list,
2288 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002289 map->stripes[index].dev = device;
2290 map->stripes[index].physical = dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002291 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002292 if (type & BTRFS_BLOCK_GROUP_DUP) {
2293 map->stripes[index].dev = device;
2294 map->stripes[index].physical =
2295 dev_offset + calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002296 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002297 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002298 }
Chris Masondfe25022008-05-13 13:46:40 -04002299 } else if (device->in_fs_metadata && avail > max_avail)
Chris Masona40a90a2008-04-18 11:55:51 -04002300 max_avail = avail;
Yan Zheng2b820322008-11-17 21:11:30 -05002301 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002302 break;
2303 }
Yan Zheng2b820322008-11-17 21:11:30 -05002304 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002305 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002306 if (index >= min_stripes) {
2307 num_stripes = index;
2308 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2309 num_stripes /= sub_stripes;
2310 num_stripes *= sub_stripes;
2311 }
2312 looped = 1;
2313 goto again;
2314 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002315 if (!looped && max_avail > 0) {
2316 looped = 1;
2317 calc_size = max_avail;
2318 goto again;
2319 }
Yan Zheng2b820322008-11-17 21:11:30 -05002320 kfree(map);
Chris Mason6324fbf2008-03-24 15:01:59 -04002321 return -ENOSPC;
2322 }
Chris Mason593060d2008-03-25 16:50:33 -04002323 map->sector_size = extent_root->sectorsize;
2324 map->stripe_len = stripe_len;
2325 map->io_align = stripe_len;
2326 map->io_width = stripe_len;
2327 map->type = type;
2328 map->num_stripes = num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002329 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002330
Yan Zheng2b820322008-11-17 21:11:30 -05002331 *map_ret = map;
2332 *stripe_size = calc_size;
2333 *num_bytes = chunk_bytes_by_type(type, calc_size,
2334 num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002335
2336 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002337 if (!em) {
2338 kfree(map);
Chris Mason0b86a832008-03-24 15:01:56 -04002339 return -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05002340 }
Chris Mason0b86a832008-03-24 15:01:56 -04002341 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002342 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002343 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002344 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002345 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002346
Chris Mason0b86a832008-03-24 15:01:56 -04002347 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002348 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002349 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002350 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002351 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002352 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002353
2354 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2355 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2356 start, *num_bytes);
2357 BUG_ON(ret);
2358
2359 index = 0;
2360 while (index < map->num_stripes) {
2361 device = map->stripes[index].dev;
2362 dev_offset = map->stripes[index].physical;
2363
2364 ret = btrfs_alloc_dev_extent(trans, device,
2365 info->chunk_root->root_key.objectid,
2366 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2367 start, dev_offset, calc_size);
2368 BUG_ON(ret);
2369 index++;
2370 }
2371
2372 return 0;
2373}
2374
2375static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2376 struct btrfs_root *extent_root,
2377 struct map_lookup *map, u64 chunk_offset,
2378 u64 chunk_size, u64 stripe_size)
2379{
2380 u64 dev_offset;
2381 struct btrfs_key key;
2382 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2383 struct btrfs_device *device;
2384 struct btrfs_chunk *chunk;
2385 struct btrfs_stripe *stripe;
2386 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2387 int index = 0;
2388 int ret;
2389
2390 chunk = kzalloc(item_size, GFP_NOFS);
2391 if (!chunk)
2392 return -ENOMEM;
2393
2394 index = 0;
2395 while (index < map->num_stripes) {
2396 device = map->stripes[index].dev;
2397 device->bytes_used += stripe_size;
2398 ret = btrfs_update_device(trans, device);
2399 BUG_ON(ret);
2400 index++;
2401 }
2402
2403 index = 0;
2404 stripe = &chunk->stripe;
2405 while (index < map->num_stripes) {
2406 device = map->stripes[index].dev;
2407 dev_offset = map->stripes[index].physical;
2408
2409 btrfs_set_stack_stripe_devid(stripe, device->devid);
2410 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2411 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2412 stripe++;
2413 index++;
2414 }
2415
2416 btrfs_set_stack_chunk_length(chunk, chunk_size);
2417 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2418 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2419 btrfs_set_stack_chunk_type(chunk, map->type);
2420 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2421 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2422 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2423 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2424 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2425
2426 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2427 key.type = BTRFS_CHUNK_ITEM_KEY;
2428 key.offset = chunk_offset;
2429
2430 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2431 BUG_ON(ret);
2432
2433 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2434 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2435 item_size);
2436 BUG_ON(ret);
2437 }
2438 kfree(chunk);
2439 return 0;
2440}
2441
2442/*
2443 * Chunk allocation falls into two parts. The first part does works
2444 * that make the new allocated chunk useable, but not do any operation
2445 * that modifies the chunk tree. The second part does the works that
2446 * require modifying the chunk tree. This division is important for the
2447 * bootstrap process of adding storage to a seed btrfs.
2448 */
2449int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2450 struct btrfs_root *extent_root, u64 type)
2451{
2452 u64 chunk_offset;
2453 u64 chunk_size;
2454 u64 stripe_size;
2455 struct map_lookup *map;
2456 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2457 int ret;
2458
2459 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2460 &chunk_offset);
2461 if (ret)
2462 return ret;
2463
2464 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2465 &stripe_size, chunk_offset, type);
2466 if (ret)
2467 return ret;
2468
2469 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2470 chunk_size, stripe_size);
2471 BUG_ON(ret);
2472 return 0;
2473}
2474
Chris Masond3977122009-01-05 21:25:51 -05002475static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002476 struct btrfs_root *root,
2477 struct btrfs_device *device)
2478{
2479 u64 chunk_offset;
2480 u64 sys_chunk_offset;
2481 u64 chunk_size;
2482 u64 sys_chunk_size;
2483 u64 stripe_size;
2484 u64 sys_stripe_size;
2485 u64 alloc_profile;
2486 struct map_lookup *map;
2487 struct map_lookup *sys_map;
2488 struct btrfs_fs_info *fs_info = root->fs_info;
2489 struct btrfs_root *extent_root = fs_info->extent_root;
2490 int ret;
2491
2492 ret = find_next_chunk(fs_info->chunk_root,
2493 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2494 BUG_ON(ret);
2495
2496 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2497 (fs_info->metadata_alloc_profile &
2498 fs_info->avail_metadata_alloc_bits);
2499 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2500
2501 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2502 &stripe_size, chunk_offset, alloc_profile);
2503 BUG_ON(ret);
2504
2505 sys_chunk_offset = chunk_offset + chunk_size;
2506
2507 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2508 (fs_info->system_alloc_profile &
2509 fs_info->avail_system_alloc_bits);
2510 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2511
2512 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2513 &sys_chunk_size, &sys_stripe_size,
2514 sys_chunk_offset, alloc_profile);
2515 BUG_ON(ret);
2516
2517 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2518 BUG_ON(ret);
2519
2520 /*
2521 * Modifying chunk tree needs allocating new blocks from both
2522 * system block group and metadata block group. So we only can
2523 * do operations require modifying the chunk tree after both
2524 * block groups were created.
2525 */
2526 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2527 chunk_size, stripe_size);
2528 BUG_ON(ret);
2529
2530 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2531 sys_chunk_offset, sys_chunk_size,
2532 sys_stripe_size);
2533 BUG_ON(ret);
2534 return 0;
2535}
2536
2537int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2538{
2539 struct extent_map *em;
2540 struct map_lookup *map;
2541 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2542 int readonly = 0;
2543 int i;
2544
Chris Mason890871b2009-09-02 16:24:52 -04002545 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002546 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002547 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002548 if (!em)
2549 return 1;
2550
Josef Bacikf48b9072010-01-27 02:07:59 +00002551 if (btrfs_test_opt(root, DEGRADED)) {
2552 free_extent_map(em);
2553 return 0;
2554 }
2555
Yan Zheng2b820322008-11-17 21:11:30 -05002556 map = (struct map_lookup *)em->bdev;
2557 for (i = 0; i < map->num_stripes; i++) {
2558 if (!map->stripes[i].dev->writeable) {
2559 readonly = 1;
2560 break;
2561 }
2562 }
2563 free_extent_map(em);
2564 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002565}
2566
2567void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2568{
2569 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2570}
2571
2572void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2573{
2574 struct extent_map *em;
2575
Chris Masond3977122009-01-05 21:25:51 -05002576 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002577 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002578 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2579 if (em)
2580 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002581 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002582 if (!em)
2583 break;
2584 kfree(em->bdev);
2585 /* once for us */
2586 free_extent_map(em);
2587 /* once for the tree */
2588 free_extent_map(em);
2589 }
2590}
2591
Chris Masonf1885912008-04-09 16:28:12 -04002592int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2593{
2594 struct extent_map *em;
2595 struct map_lookup *map;
2596 struct extent_map_tree *em_tree = &map_tree->map_tree;
2597 int ret;
2598
Chris Mason890871b2009-09-02 16:24:52 -04002599 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002600 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002601 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002602 BUG_ON(!em);
2603
2604 BUG_ON(em->start > logical || em->start + em->len < logical);
2605 map = (struct map_lookup *)em->bdev;
2606 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2607 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002608 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2609 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002610 else
2611 ret = 1;
2612 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002613 return ret;
2614}
2615
Chris Masondfe25022008-05-13 13:46:40 -04002616static int find_live_mirror(struct map_lookup *map, int first, int num,
2617 int optimal)
2618{
2619 int i;
2620 if (map->stripes[optimal].dev->bdev)
2621 return optimal;
2622 for (i = first; i < first + num; i++) {
2623 if (map->stripes[i].dev->bdev)
2624 return i;
2625 }
2626 /* we couldn't find one that doesn't fail. Just return something
2627 * and the io error handling code will clean up eventually
2628 */
2629 return optimal;
2630}
2631
Chris Masonf2d8d742008-04-21 10:03:05 -04002632static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2633 u64 logical, u64 *length,
2634 struct btrfs_multi_bio **multi_ret,
2635 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002636{
2637 struct extent_map *em;
2638 struct map_lookup *map;
2639 struct extent_map_tree *em_tree = &map_tree->map_tree;
2640 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002641 u64 stripe_offset;
2642 u64 stripe_nr;
Chris Masoncea9e442008-04-09 16:28:12 -04002643 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002644 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002645 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002646 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002647 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002648 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002649 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002650
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002651 if (multi_ret && !(rw & REQ_WRITE))
Chris Masoncea9e442008-04-09 16:28:12 -04002652 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002653again:
2654 if (multi_ret) {
2655 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2656 GFP_NOFS);
2657 if (!multi)
2658 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002659
2660 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002661 }
Chris Mason0b86a832008-03-24 15:01:56 -04002662
Chris Mason890871b2009-09-02 16:24:52 -04002663 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002664 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002665 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002666
Jiri Slaby2423fdf2010-01-06 16:57:22 +00002667 if (!em && unplug_page) {
2668 kfree(multi);
Chris Masonf2d8d742008-04-21 10:03:05 -04002669 return 0;
Jiri Slaby2423fdf2010-01-06 16:57:22 +00002670 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002671
Chris Mason3b951512008-04-17 11:29:12 -04002672 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002673 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2674 (unsigned long long)logical,
2675 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002676 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002677 }
Chris Mason0b86a832008-03-24 15:01:56 -04002678
2679 BUG_ON(em->start > logical || em->start + em->len < logical);
2680 map = (struct map_lookup *)em->bdev;
2681 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002682
Chris Masonf1885912008-04-09 16:28:12 -04002683 if (mirror_num > map->num_stripes)
2684 mirror_num = 0;
2685
Chris Masoncea9e442008-04-09 16:28:12 -04002686 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002687 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002688 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2689 BTRFS_BLOCK_GROUP_DUP)) {
2690 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002691 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002692 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2693 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002694 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002695 }
2696 }
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002697 if (multi_ret && (rw & REQ_WRITE) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002698 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002699 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002700 free_extent_map(em);
2701 kfree(multi);
2702 goto again;
2703 }
Chris Mason593060d2008-03-25 16:50:33 -04002704 stripe_nr = offset;
2705 /*
2706 * stripe_nr counts the total number of stripes we have to stride
2707 * to get to this block
2708 */
2709 do_div(stripe_nr, map->stripe_len);
2710
2711 stripe_offset = stripe_nr * map->stripe_len;
2712 BUG_ON(offset < stripe_offset);
2713
2714 /* stripe_offset is the offset of this block in its stripe*/
2715 stripe_offset = offset - stripe_offset;
2716
Chris Masoncea9e442008-04-09 16:28:12 -04002717 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04002718 BTRFS_BLOCK_GROUP_RAID10 |
Chris Masoncea9e442008-04-09 16:28:12 -04002719 BTRFS_BLOCK_GROUP_DUP)) {
2720 /* we limit the length of each bio to what fits in a stripe */
2721 *length = min_t(u64, em->len - offset,
2722 map->stripe_len - stripe_offset);
2723 } else {
2724 *length = em->len - offset;
2725 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002726
2727 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002728 goto out;
2729
Chris Masonf2d8d742008-04-21 10:03:05 -04002730 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002731 stripe_index = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002732 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002733 if (unplug_page || (rw & REQ_WRITE))
Chris Masonf2d8d742008-04-21 10:03:05 -04002734 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002735 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002736 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002737 else {
2738 stripe_index = find_live_mirror(map, 0,
2739 map->num_stripes,
2740 current->pid % map->num_stripes);
2741 }
Chris Mason2fff7342008-04-29 14:12:09 -04002742
Chris Mason611f0e02008-04-03 16:29:03 -04002743 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002744 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04002745 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002746 else if (mirror_num)
2747 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002748
Chris Mason321aecc2008-04-16 10:49:51 -04002749 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2750 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002751
2752 stripe_index = do_div(stripe_nr, factor);
2753 stripe_index *= map->sub_stripes;
2754
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002755 if (unplug_page || (rw & REQ_WRITE))
Chris Masonf2d8d742008-04-21 10:03:05 -04002756 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002757 else if (mirror_num)
2758 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002759 else {
2760 stripe_index = find_live_mirror(map, stripe_index,
2761 map->sub_stripes, stripe_index +
2762 current->pid % map->sub_stripes);
2763 }
Chris Mason8790d502008-04-03 16:29:03 -04002764 } else {
2765 /*
2766 * after this do_div call, stripe_nr is the number of stripes
2767 * on this device we have to walk to find the data, and
2768 * stripe_index is the number of our device in the stripe array
2769 */
2770 stripe_index = do_div(stripe_nr, map->num_stripes);
2771 }
Chris Mason593060d2008-03-25 16:50:33 -04002772 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002773
Chris Masonf2d8d742008-04-21 10:03:05 -04002774 for (i = 0; i < num_stripes; i++) {
2775 if (unplug_page) {
2776 struct btrfs_device *device;
2777 struct backing_dev_info *bdi;
2778
2779 device = map->stripes[stripe_index].dev;
Chris Masondfe25022008-05-13 13:46:40 -04002780 if (device->bdev) {
2781 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masond3977122009-01-05 21:25:51 -05002782 if (bdi->unplug_io_fn)
Chris Masondfe25022008-05-13 13:46:40 -04002783 bdi->unplug_io_fn(bdi, unplug_page);
Chris Masonf2d8d742008-04-21 10:03:05 -04002784 }
2785 } else {
2786 multi->stripes[i].physical =
2787 map->stripes[stripe_index].physical +
2788 stripe_offset + stripe_nr * map->stripe_len;
2789 multi->stripes[i].dev = map->stripes[stripe_index].dev;
2790 }
Chris Masoncea9e442008-04-09 16:28:12 -04002791 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04002792 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002793 if (multi_ret) {
2794 *multi_ret = multi;
2795 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002796 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04002797 }
Chris Masoncea9e442008-04-09 16:28:12 -04002798out:
Chris Mason0b86a832008-03-24 15:01:56 -04002799 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04002800 return 0;
2801}
2802
Chris Masonf2d8d742008-04-21 10:03:05 -04002803int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2804 u64 logical, u64 *length,
2805 struct btrfs_multi_bio **multi_ret, int mirror_num)
2806{
2807 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2808 mirror_num, NULL);
2809}
2810
Yan Zhenga512bbf2008-12-08 16:46:26 -05002811int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2812 u64 chunk_start, u64 physical, u64 devid,
2813 u64 **logical, int *naddrs, int *stripe_len)
2814{
2815 struct extent_map_tree *em_tree = &map_tree->map_tree;
2816 struct extent_map *em;
2817 struct map_lookup *map;
2818 u64 *buf;
2819 u64 bytenr;
2820 u64 length;
2821 u64 stripe_nr;
2822 int i, j, nr = 0;
2823
Chris Mason890871b2009-09-02 16:24:52 -04002824 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002825 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002826 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002827
2828 BUG_ON(!em || em->start != chunk_start);
2829 map = (struct map_lookup *)em->bdev;
2830
2831 length = em->len;
2832 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2833 do_div(length, map->num_stripes / map->sub_stripes);
2834 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2835 do_div(length, map->num_stripes);
2836
2837 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2838 BUG_ON(!buf);
2839
2840 for (i = 0; i < map->num_stripes; i++) {
2841 if (devid && map->stripes[i].dev->devid != devid)
2842 continue;
2843 if (map->stripes[i].physical > physical ||
2844 map->stripes[i].physical + length <= physical)
2845 continue;
2846
2847 stripe_nr = physical - map->stripes[i].physical;
2848 do_div(stripe_nr, map->stripe_len);
2849
2850 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2851 stripe_nr = stripe_nr * map->num_stripes + i;
2852 do_div(stripe_nr, map->sub_stripes);
2853 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2854 stripe_nr = stripe_nr * map->num_stripes + i;
2855 }
2856 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05002857 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002858 for (j = 0; j < nr; j++) {
2859 if (buf[j] == bytenr)
2860 break;
2861 }
Chris Mason934d3752008-12-08 16:43:10 -05002862 if (j == nr) {
2863 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05002864 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05002865 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05002866 }
2867
Yan Zhenga512bbf2008-12-08 16:46:26 -05002868 *logical = buf;
2869 *naddrs = nr;
2870 *stripe_len = map->stripe_len;
2871
2872 free_extent_map(em);
2873 return 0;
2874}
2875
Chris Masonf2d8d742008-04-21 10:03:05 -04002876int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2877 u64 logical, struct page *page)
2878{
2879 u64 length = PAGE_CACHE_SIZE;
2880 return __btrfs_map_block(map_tree, READ, logical, &length,
2881 NULL, 0, page);
2882}
2883
Chris Mason8790d502008-04-03 16:29:03 -04002884static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04002885{
Chris Masoncea9e442008-04-09 16:28:12 -04002886 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04002887 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002888
Chris Mason8790d502008-04-03 16:29:03 -04002889 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04002890 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04002891
Chris Mason7d2b4da2008-08-05 10:13:57 -04002892 if (bio == multi->orig_bio)
2893 is_orig_bio = 1;
2894
Chris Masoncea9e442008-04-09 16:28:12 -04002895 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04002896 if (!is_orig_bio) {
2897 bio_put(bio);
2898 bio = multi->orig_bio;
2899 }
Chris Mason8790d502008-04-03 16:29:03 -04002900 bio->bi_private = multi->private;
2901 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04002902 /* only send an error to the higher layers if it is
2903 * beyond the tolerance of the multi-bio
2904 */
Chris Mason1259ab72008-05-12 13:39:03 -04002905 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04002906 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04002907 } else if (err) {
2908 /*
2909 * this bio is actually up to date, we didn't
2910 * go over the max number of errors
2911 */
2912 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04002913 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04002914 }
Chris Mason8790d502008-04-03 16:29:03 -04002915 kfree(multi);
2916
2917 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04002918 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04002919 bio_put(bio);
2920 }
Chris Mason8790d502008-04-03 16:29:03 -04002921}
2922
Chris Mason8b712842008-06-11 16:50:36 -04002923struct async_sched {
2924 struct bio *bio;
2925 int rw;
2926 struct btrfs_fs_info *info;
2927 struct btrfs_work work;
2928};
2929
2930/*
2931 * see run_scheduled_bios for a description of why bios are collected for
2932 * async submit.
2933 *
2934 * This will add one bio to the pending list for a device and make sure
2935 * the work struct is scheduled.
2936 */
Chris Masond3977122009-01-05 21:25:51 -05002937static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04002938 struct btrfs_device *device,
2939 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04002940{
2941 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04002942 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04002943
2944 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002945 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04002946 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002947 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04002948 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04002949 return 0;
2950 }
2951
2952 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04002953 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04002954 * higher layers. Otherwise, the async bio makes it appear we have
2955 * made progress against dirty pages when we've really just put it
2956 * on a queue for later
2957 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04002958 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04002959 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04002960 bio->bi_next = NULL;
2961 bio->bi_rw |= rw;
2962
2963 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002964 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04002965 pending_bios = &device->pending_sync_bios;
2966 else
2967 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04002968
Chris Masonffbd5172009-04-20 15:50:09 -04002969 if (pending_bios->tail)
2970 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04002971
Chris Masonffbd5172009-04-20 15:50:09 -04002972 pending_bios->tail = bio;
2973 if (!pending_bios->head)
2974 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04002975 if (device->running_pending)
2976 should_queue = 0;
2977
2978 spin_unlock(&device->io_lock);
2979
2980 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04002981 btrfs_queue_worker(&root->fs_info->submit_workers,
2982 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04002983 return 0;
2984}
2985
Chris Masonf1885912008-04-09 16:28:12 -04002986int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04002987 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04002988{
2989 struct btrfs_mapping_tree *map_tree;
2990 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04002991 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04002992 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04002993 u64 length = 0;
2994 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04002995 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002996 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04002997 int dev_nr = 0;
2998 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04002999
Chris Masonf2d8d742008-04-21 10:03:05 -04003000 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003001 map_tree = &root->fs_info->mapping_tree;
3002 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003003
Chris Masonf1885912008-04-09 16:28:12 -04003004 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3005 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003006 BUG_ON(ret);
3007
3008 total_devs = multi->num_stripes;
3009 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003010 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3011 "len %llu\n", (unsigned long long)logical,
3012 (unsigned long long)length,
3013 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003014 BUG();
3015 }
3016 multi->end_io = first_bio->bi_end_io;
3017 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003018 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003019 atomic_set(&multi->stripes_pending, multi->num_stripes);
3020
Chris Masond3977122009-01-05 21:25:51 -05003021 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003022 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003023 if (dev_nr < total_devs - 1) {
3024 bio = bio_clone(first_bio, GFP_NOFS);
3025 BUG_ON(!bio);
3026 } else {
3027 bio = first_bio;
3028 }
3029 bio->bi_private = multi;
3030 bio->bi_end_io = end_bio_multi_stripe;
3031 }
Chris Masoncea9e442008-04-09 16:28:12 -04003032 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3033 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003034 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003035 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003036 if (async_submit)
3037 schedule_bio(root, dev, rw, bio);
3038 else
3039 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003040 } else {
3041 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3042 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003043 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003044 }
Chris Mason8790d502008-04-03 16:29:03 -04003045 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003046 }
Chris Masoncea9e442008-04-09 16:28:12 -04003047 if (total_devs == 1)
3048 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003049 return 0;
3050}
3051
Chris Masona4437552008-04-18 10:29:38 -04003052struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003053 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003054{
Yan Zheng2b820322008-11-17 21:11:30 -05003055 struct btrfs_device *device;
3056 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003057
Yan Zheng2b820322008-11-17 21:11:30 -05003058 cur_devices = root->fs_info->fs_devices;
3059 while (cur_devices) {
3060 if (!fsid ||
3061 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3062 device = __find_device(&cur_devices->devices,
3063 devid, uuid);
3064 if (device)
3065 return device;
3066 }
3067 cur_devices = cur_devices->seed;
3068 }
3069 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003070}
3071
Chris Masondfe25022008-05-13 13:46:40 -04003072static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3073 u64 devid, u8 *dev_uuid)
3074{
3075 struct btrfs_device *device;
3076 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3077
3078 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003079 if (!device)
3080 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003081 list_add(&device->dev_list,
3082 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003083 device->dev_root = root->fs_info->dev_root;
3084 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003085 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003086 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04003087 fs_devices->num_devices++;
3088 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003089 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003090 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3091 return device;
3092}
3093
Chris Mason0b86a832008-03-24 15:01:56 -04003094static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3095 struct extent_buffer *leaf,
3096 struct btrfs_chunk *chunk)
3097{
3098 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3099 struct map_lookup *map;
3100 struct extent_map *em;
3101 u64 logical;
3102 u64 length;
3103 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003104 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003105 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003106 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003107 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003108
Chris Masone17cade2008-04-15 15:41:47 -04003109 logical = key->offset;
3110 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003111
Chris Mason890871b2009-09-02 16:24:52 -04003112 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003113 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003114 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003115
3116 /* already mapped? */
3117 if (em && em->start <= logical && em->start + em->len > logical) {
3118 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003119 return 0;
3120 } else if (em) {
3121 free_extent_map(em);
3122 }
Chris Mason0b86a832008-03-24 15:01:56 -04003123
Chris Mason0b86a832008-03-24 15:01:56 -04003124 em = alloc_extent_map(GFP_NOFS);
3125 if (!em)
3126 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003127 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3128 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003129 if (!map) {
3130 free_extent_map(em);
3131 return -ENOMEM;
3132 }
3133
3134 em->bdev = (struct block_device *)map;
3135 em->start = logical;
3136 em->len = length;
3137 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003138 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003139
Chris Mason593060d2008-03-25 16:50:33 -04003140 map->num_stripes = num_stripes;
3141 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3142 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3143 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3144 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3145 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003146 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003147 for (i = 0; i < num_stripes; i++) {
3148 map->stripes[i].physical =
3149 btrfs_stripe_offset_nr(leaf, chunk, i);
3150 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003151 read_extent_buffer(leaf, uuid, (unsigned long)
3152 btrfs_stripe_dev_uuid_nr(chunk, i),
3153 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003154 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3155 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003156 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003157 kfree(map);
3158 free_extent_map(em);
3159 return -EIO;
3160 }
Chris Masondfe25022008-05-13 13:46:40 -04003161 if (!map->stripes[i].dev) {
3162 map->stripes[i].dev =
3163 add_missing_dev(root, devid, uuid);
3164 if (!map->stripes[i].dev) {
3165 kfree(map);
3166 free_extent_map(em);
3167 return -EIO;
3168 }
3169 }
3170 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003171 }
3172
Chris Mason890871b2009-09-02 16:24:52 -04003173 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003174 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003175 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003176 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003177 free_extent_map(em);
3178
3179 return 0;
3180}
3181
3182static int fill_device_from_item(struct extent_buffer *leaf,
3183 struct btrfs_dev_item *dev_item,
3184 struct btrfs_device *device)
3185{
3186 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003187
3188 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003189 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3190 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003191 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3192 device->type = btrfs_device_type(leaf, dev_item);
3193 device->io_align = btrfs_device_io_align(leaf, dev_item);
3194 device->io_width = btrfs_device_io_width(leaf, dev_item);
3195 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003196
3197 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003198 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003199
Chris Mason0b86a832008-03-24 15:01:56 -04003200 return 0;
3201}
3202
Yan Zheng2b820322008-11-17 21:11:30 -05003203static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3204{
3205 struct btrfs_fs_devices *fs_devices;
3206 int ret;
3207
3208 mutex_lock(&uuid_mutex);
3209
3210 fs_devices = root->fs_info->fs_devices->seed;
3211 while (fs_devices) {
3212 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3213 ret = 0;
3214 goto out;
3215 }
3216 fs_devices = fs_devices->seed;
3217 }
3218
3219 fs_devices = find_fsid(fsid);
3220 if (!fs_devices) {
3221 ret = -ENOENT;
3222 goto out;
3223 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003224
3225 fs_devices = clone_fs_devices(fs_devices);
3226 if (IS_ERR(fs_devices)) {
3227 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003228 goto out;
3229 }
3230
Christoph Hellwig97288f22008-12-02 06:36:09 -05003231 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003232 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003233 if (ret)
3234 goto out;
3235
3236 if (!fs_devices->seeding) {
3237 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003238 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003239 ret = -EINVAL;
3240 goto out;
3241 }
3242
3243 fs_devices->seed = root->fs_info->fs_devices->seed;
3244 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003245out:
3246 mutex_unlock(&uuid_mutex);
3247 return ret;
3248}
3249
Chris Mason0d81ba52008-03-24 15:02:07 -04003250static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003251 struct extent_buffer *leaf,
3252 struct btrfs_dev_item *dev_item)
3253{
3254 struct btrfs_device *device;
3255 u64 devid;
3256 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003257 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003258 u8 dev_uuid[BTRFS_UUID_SIZE];
3259
Chris Mason0b86a832008-03-24 15:01:56 -04003260 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003261 read_extent_buffer(leaf, dev_uuid,
3262 (unsigned long)btrfs_device_uuid(dev_item),
3263 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003264 read_extent_buffer(leaf, fs_uuid,
3265 (unsigned long)btrfs_device_fsid(dev_item),
3266 BTRFS_UUID_SIZE);
3267
3268 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3269 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003270 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003271 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003272 }
3273
3274 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3275 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003276 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003277 return -EIO;
3278
3279 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003280 printk(KERN_WARNING "warning devid %llu missing\n",
3281 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003282 device = add_missing_dev(root, devid, dev_uuid);
3283 if (!device)
3284 return -ENOMEM;
3285 }
3286 }
3287
3288 if (device->fs_devices != root->fs_info->fs_devices) {
3289 BUG_ON(device->writeable);
3290 if (device->generation !=
3291 btrfs_device_generation(leaf, dev_item))
3292 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003293 }
Chris Mason0b86a832008-03-24 15:01:56 -04003294
3295 fill_device_from_item(leaf, dev_item, device);
3296 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003297 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003298 if (device->writeable)
3299 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003300 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003301 return ret;
3302}
3303
Chris Mason0d81ba52008-03-24 15:02:07 -04003304int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3305{
3306 struct btrfs_dev_item *dev_item;
3307
3308 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3309 dev_item);
3310 return read_one_dev(root, buf, dev_item);
3311}
3312
Yan Zhenge4404d62008-12-12 10:03:26 -05003313int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003314{
3315 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003316 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003317 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003318 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003319 u8 *ptr;
3320 unsigned long sb_ptr;
3321 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003322 u32 num_stripes;
3323 u32 array_size;
3324 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003325 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003326 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003327
Yan Zhenge4404d62008-12-12 10:03:26 -05003328 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003329 BTRFS_SUPER_INFO_SIZE);
3330 if (!sb)
3331 return -ENOMEM;
3332 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003333 btrfs_set_buffer_lockdep_class(sb, 0);
3334
Chris Masona061fc82008-05-07 11:43:44 -04003335 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003336 array_size = btrfs_super_sys_array_size(super_copy);
3337
Chris Mason0b86a832008-03-24 15:01:56 -04003338 ptr = super_copy->sys_chunk_array;
3339 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3340 cur = 0;
3341
3342 while (cur < array_size) {
3343 disk_key = (struct btrfs_disk_key *)ptr;
3344 btrfs_disk_key_to_cpu(&key, disk_key);
3345
Chris Masona061fc82008-05-07 11:43:44 -04003346 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003347 sb_ptr += len;
3348 cur += len;
3349
Chris Mason0d81ba52008-03-24 15:02:07 -04003350 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003351 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003352 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003353 if (ret)
3354 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003355 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3356 len = btrfs_chunk_item_size(num_stripes);
3357 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003358 ret = -EIO;
3359 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003360 }
3361 ptr += len;
3362 sb_ptr += len;
3363 cur += len;
3364 }
Chris Masona061fc82008-05-07 11:43:44 -04003365 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003366 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003367}
3368
3369int btrfs_read_chunk_tree(struct btrfs_root *root)
3370{
3371 struct btrfs_path *path;
3372 struct extent_buffer *leaf;
3373 struct btrfs_key key;
3374 struct btrfs_key found_key;
3375 int ret;
3376 int slot;
3377
3378 root = root->fs_info->chunk_root;
3379
3380 path = btrfs_alloc_path();
3381 if (!path)
3382 return -ENOMEM;
3383
3384 /* first we search for all of the device items, and then we
3385 * read in all of the chunk items. This way we can create chunk
3386 * mappings that reference all of the devices that are afound
3387 */
3388 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3389 key.offset = 0;
3390 key.type = 0;
3391again:
3392 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003393 if (ret < 0)
3394 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003395 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003396 leaf = path->nodes[0];
3397 slot = path->slots[0];
3398 if (slot >= btrfs_header_nritems(leaf)) {
3399 ret = btrfs_next_leaf(root, path);
3400 if (ret == 0)
3401 continue;
3402 if (ret < 0)
3403 goto error;
3404 break;
3405 }
3406 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3407 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3408 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3409 break;
3410 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3411 struct btrfs_dev_item *dev_item;
3412 dev_item = btrfs_item_ptr(leaf, slot,
3413 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003414 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003415 if (ret)
3416 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003417 }
3418 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3419 struct btrfs_chunk *chunk;
3420 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3421 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003422 if (ret)
3423 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003424 }
3425 path->slots[0]++;
3426 }
3427 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3428 key.objectid = 0;
3429 btrfs_release_path(root, path);
3430 goto again;
3431 }
Chris Mason0b86a832008-03-24 15:01:56 -04003432 ret = 0;
3433error:
Yan Zheng2b820322008-11-17 21:11:30 -05003434 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003435 return ret;
3436}