blob: ab55bfc31a068261d1c0e5fbe7b7d49c63768515 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Chris Mason593060d2008-03-25 16:50:33 -040026#include <asm/div64.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050027#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "ctree.h"
29#include "extent_map.h"
30#include "disk-io.h"
31#include "transaction.h"
32#include "print-tree.h"
33#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040034#include "async-thread.h"
Chris Mason0b86a832008-03-24 15:01:56 -040035
Yan Zheng2b820322008-11-17 21:11:30 -050036static int init_first_rw_device(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct btrfs_device *device);
39static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
40
Chris Mason593060d2008-03-25 16:50:33 -040041#define map_lookup_size(n) (sizeof(struct map_lookup) + \
Chris Masoncea9e442008-04-09 16:28:12 -040042 (sizeof(struct btrfs_bio_stripe) * (n)))
Chris Mason593060d2008-03-25 16:50:33 -040043
Chris Mason8a4b83c2008-03-24 15:02:07 -040044static DEFINE_MUTEX(uuid_mutex);
45static LIST_HEAD(fs_uuids);
46
Chris Masona061fc82008-05-07 11:43:44 -040047void btrfs_lock_volumes(void)
48{
49 mutex_lock(&uuid_mutex);
50}
51
52void btrfs_unlock_volumes(void)
53{
54 mutex_unlock(&uuid_mutex);
55}
56
Chris Mason7d9eb122008-07-08 14:19:17 -040057static void lock_chunks(struct btrfs_root *root)
58{
Chris Mason7d9eb122008-07-08 14:19:17 -040059 mutex_lock(&root->fs_info->chunk_mutex);
60}
61
62static void unlock_chunks(struct btrfs_root *root)
63{
Chris Mason7d9eb122008-07-08 14:19:17 -040064 mutex_unlock(&root->fs_info->chunk_mutex);
65}
66
Yan Zhenge4404d62008-12-12 10:03:26 -050067static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
68{
69 struct btrfs_device *device;
70 WARN_ON(fs_devices->opened);
71 while (!list_empty(&fs_devices->devices)) {
72 device = list_entry(fs_devices->devices.next,
73 struct btrfs_device, dev_list);
74 list_del(&device->dev_list);
75 kfree(device->name);
76 kfree(device);
77 }
78 kfree(fs_devices);
79}
80
Chris Mason8a4b83c2008-03-24 15:02:07 -040081int btrfs_cleanup_fs_uuids(void)
82{
83 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040084
Yan Zheng2b820322008-11-17 21:11:30 -050085 while (!list_empty(&fs_uuids)) {
86 fs_devices = list_entry(fs_uuids.next,
87 struct btrfs_fs_devices, list);
88 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050089 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040090 }
91 return 0;
92}
93
Chris Masona1b32a52008-09-05 16:09:51 -040094static noinline struct btrfs_device *__find_device(struct list_head *head,
95 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040096{
97 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040098
Qinghuang Fengc6e30872009-01-21 10:59:08 -050099 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400100 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400101 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400103 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400104 }
105 return NULL;
106}
107
Chris Masona1b32a52008-09-05 16:09:51 -0400108static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400109{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400110 struct btrfs_fs_devices *fs_devices;
111
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500112 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400113 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
114 return fs_devices;
115 }
116 return NULL;
117}
118
Chris Masonffbd5172009-04-20 15:50:09 -0400119static void requeue_list(struct btrfs_pending_bios *pending_bios,
120 struct bio *head, struct bio *tail)
121{
122
123 struct bio *old_head;
124
125 old_head = pending_bios->head;
126 pending_bios->head = head;
127 if (pending_bios->tail)
128 tail->bi_next = old_head;
129 else
130 pending_bios->tail = tail;
131}
132
Chris Mason8b712842008-06-11 16:50:36 -0400133/*
134 * we try to collect pending bios for a device so we don't get a large
135 * number of procs sending bios down to the same device. This greatly
136 * improves the schedulers ability to collect and merge the bios.
137 *
138 * But, it also turns into a long list of bios to process and that is sure
139 * to eventually make the worker thread block. The solution here is to
140 * make some progress and then put this work struct back at the end of
141 * the list if the block device is congested. This way, multiple devices
142 * can make progress from a single worker thread.
143 */
Chris Masond3977122009-01-05 21:25:51 -0500144static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400145{
146 struct bio *pending;
147 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400148 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400149 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400150 struct bio *tail;
151 struct bio *cur;
152 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400153 unsigned long num_run;
154 unsigned long num_sync_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400155 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400156 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400157 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400158 int force_reg = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400159
Chris Masonbedf7622009-04-03 10:32:58 -0400160 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400161 fs_info = device->dev_root->fs_info;
162 limit = btrfs_async_submit_limit(fs_info);
163 limit = limit * 2 / 3;
164
Chris Masonffbd5172009-04-20 15:50:09 -0400165 /* we want to make sure that every time we switch from the sync
166 * list to the normal list, we unplug
167 */
168 num_sync_run = 0;
169
Chris Mason8b712842008-06-11 16:50:36 -0400170loop:
171 spin_lock(&device->io_lock);
172
Chris Masona6837052009-02-04 09:19:41 -0500173loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400174 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400175
Chris Mason8b712842008-06-11 16:50:36 -0400176 /* take all the bios off the list at once and process them
177 * later on (without the lock held). But, remember the
178 * tail and other pointers so the bios can be properly reinserted
179 * into the list if we hit congestion
180 */
Chris Masond84275c2009-06-09 15:39:08 -0400181 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400182 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400183 force_reg = 1;
184 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400185 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400186 force_reg = 0;
187 }
Chris Masonffbd5172009-04-20 15:50:09 -0400188
189 pending = pending_bios->head;
190 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400191 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400192
193 /*
194 * if pending was null this time around, no bios need processing
195 * at all and we can stop. Otherwise it'll loop back up again
196 * and do an additional check so no bios are missed.
197 *
198 * device->running_pending is used to synchronize with the
199 * schedule_bio code.
200 */
Chris Masonffbd5172009-04-20 15:50:09 -0400201 if (device->pending_sync_bios.head == NULL &&
202 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400203 again = 0;
204 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400205 } else {
206 again = 1;
207 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400208 }
Chris Masonffbd5172009-04-20 15:50:09 -0400209
210 pending_bios->head = NULL;
211 pending_bios->tail = NULL;
212
Chris Mason8b712842008-06-11 16:50:36 -0400213 spin_unlock(&device->io_lock);
214
Chris Masonffbd5172009-04-20 15:50:09 -0400215 /*
216 * if we're doing the regular priority list, make sure we unplug
217 * for any high prio bios we've sent down
218 */
219 if (pending_bios == &device->pending_bios && num_sync_run > 0) {
220 num_sync_run = 0;
221 blk_run_backing_dev(bdi, NULL);
222 }
223
Chris Masond3977122009-01-05 21:25:51 -0500224 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400225
226 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400227 /* we want to work on both lists, but do more bios on the
228 * sync list than the regular list
229 */
230 if ((num_run > 32 &&
231 pending_bios != &device->pending_sync_bios &&
232 device->pending_sync_bios.head) ||
233 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
234 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400235 spin_lock(&device->io_lock);
236 requeue_list(pending_bios, pending, tail);
237 goto loop_lock;
238 }
239
Chris Mason8b712842008-06-11 16:50:36 -0400240 cur = pending;
241 pending = pending->bi_next;
242 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400243 atomic_dec(&fs_info->nr_async_bios);
244
245 if (atomic_read(&fs_info->nr_async_bios) < limit &&
246 waitqueue_active(&fs_info->async_submit_wait))
247 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400248
249 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400250
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200251 if (cur->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -0400252 num_sync_run++;
253
Chris Mason5ff7ba32010-03-15 10:21:30 -0400254 submit_bio(cur->bi_rw, cur);
255 num_run++;
256 batch_run++;
Chris Masonffbd5172009-04-20 15:50:09 -0400257 if (need_resched()) {
258 if (num_sync_run) {
259 blk_run_backing_dev(bdi, NULL);
260 num_sync_run = 0;
261 }
262 cond_resched();
263 }
Chris Mason8b712842008-06-11 16:50:36 -0400264
265 /*
266 * we made progress, there is more work to do and the bdi
267 * is now congested. Back off and let other work structs
268 * run instead
269 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400270 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500271 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400272 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400273
Chris Masonb765ead2009-04-03 10:27:10 -0400274 ioc = current->io_context;
275
276 /*
277 * the main goal here is that we don't want to
278 * block if we're going to be able to submit
279 * more requests without blocking.
280 *
281 * This code does two great things, it pokes into
282 * the elevator code from a filesystem _and_
283 * it makes assumptions about how batching works.
284 */
285 if (ioc && ioc->nr_batch_requests > 0 &&
286 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
287 (last_waited == 0 ||
288 ioc->last_waited == last_waited)) {
289 /*
290 * we want to go through our batch of
291 * requests and stop. So, we copy out
292 * the ioc->last_waited time and test
293 * against it before looping
294 */
295 last_waited = ioc->last_waited;
Chris Masonffbd5172009-04-20 15:50:09 -0400296 if (need_resched()) {
297 if (num_sync_run) {
298 blk_run_backing_dev(bdi, NULL);
299 num_sync_run = 0;
300 }
301 cond_resched();
302 }
Chris Masonb765ead2009-04-03 10:27:10 -0400303 continue;
304 }
Chris Mason8b712842008-06-11 16:50:36 -0400305 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400306 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500307 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400308
309 spin_unlock(&device->io_lock);
310 btrfs_requeue_work(&device->work);
311 goto done;
312 }
313 }
Chris Masonffbd5172009-04-20 15:50:09 -0400314
315 if (num_sync_run) {
316 num_sync_run = 0;
317 blk_run_backing_dev(bdi, NULL);
318 }
Chris Masonbedf7622009-04-03 10:32:58 -0400319 /*
320 * IO has already been through a long path to get here. Checksumming,
321 * async helper threads, perhaps compression. We've done a pretty
322 * good job of collecting a batch of IO and should just unplug
323 * the device right away.
324 *
325 * This will help anyone who is waiting on the IO, they might have
326 * already unplugged, but managed to do so before the bio they
327 * cared about found its way down here.
328 */
329 blk_run_backing_dev(bdi, NULL);
Chris Mason51684082010-03-10 15:33:32 -0500330
331 cond_resched();
332 if (again)
333 goto loop;
334
335 spin_lock(&device->io_lock);
336 if (device->pending_bios.head || device->pending_sync_bios.head)
337 goto loop_lock;
338 spin_unlock(&device->io_lock);
339
Chris Mason8b712842008-06-11 16:50:36 -0400340done:
341 return 0;
342}
343
Christoph Hellwigb2950862008-12-02 09:54:17 -0500344static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400345{
346 struct btrfs_device *device;
347
348 device = container_of(work, struct btrfs_device, work);
349 run_scheduled_bios(device);
350}
351
Chris Masona1b32a52008-09-05 16:09:51 -0400352static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400353 struct btrfs_super_block *disk_super,
354 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
355{
356 struct btrfs_device *device;
357 struct btrfs_fs_devices *fs_devices;
358 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000359 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400360
361 fs_devices = find_fsid(disk_super->fsid);
362 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400363 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400364 if (!fs_devices)
365 return -ENOMEM;
366 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400367 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400368 list_add(&fs_devices->list, &fs_uuids);
369 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
370 fs_devices->latest_devid = devid;
371 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400372 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400373 device = NULL;
374 } else {
Chris Masona4437552008-04-18 10:29:38 -0400375 device = __find_device(&fs_devices->devices, devid,
376 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400377 }
378 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500379 if (fs_devices->opened)
380 return -EBUSY;
381
Chris Mason8a4b83c2008-03-24 15:02:07 -0400382 device = kzalloc(sizeof(*device), GFP_NOFS);
383 if (!device) {
384 /* we can safely leave the fs_devices entry around */
385 return -ENOMEM;
386 }
387 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400388 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400389 memcpy(device->uuid, disk_super->dev_item.uuid,
390 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400391 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400392 device->name = kstrdup(path, GFP_NOFS);
393 if (!device->name) {
394 kfree(device);
395 return -ENOMEM;
396 }
Yan Zheng2b820322008-11-17 21:11:30 -0500397 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400398
399 mutex_lock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400400 list_add(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400401 mutex_unlock(&fs_devices->device_list_mutex);
402
Yan Zheng2b820322008-11-17 21:11:30 -0500403 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400404 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500405 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000406 name = kstrdup(path, GFP_NOFS);
407 if (!name)
408 return -ENOMEM;
409 kfree(device->name);
410 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500411 if (device->missing) {
412 fs_devices->missing_devices--;
413 device->missing = 0;
414 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400415 }
416
417 if (found_transid > fs_devices->latest_trans) {
418 fs_devices->latest_devid = devid;
419 fs_devices->latest_trans = found_transid;
420 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400421 *fs_devices_ret = fs_devices;
422 return 0;
423}
424
Yan Zhenge4404d62008-12-12 10:03:26 -0500425static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
426{
427 struct btrfs_fs_devices *fs_devices;
428 struct btrfs_device *device;
429 struct btrfs_device *orig_dev;
430
431 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
432 if (!fs_devices)
433 return ERR_PTR(-ENOMEM);
434
435 INIT_LIST_HEAD(&fs_devices->devices);
436 INIT_LIST_HEAD(&fs_devices->alloc_list);
437 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400438 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500439 fs_devices->latest_devid = orig->latest_devid;
440 fs_devices->latest_trans = orig->latest_trans;
441 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
442
Chris Masone5e9a522009-06-10 15:17:02 -0400443 mutex_lock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500444 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
445 device = kzalloc(sizeof(*device), GFP_NOFS);
446 if (!device)
447 goto error;
448
449 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400450 if (!device->name) {
451 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500452 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400453 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500454
455 device->devid = orig_dev->devid;
456 device->work.func = pending_bios_fn;
457 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500458 spin_lock_init(&device->io_lock);
459 INIT_LIST_HEAD(&device->dev_list);
460 INIT_LIST_HEAD(&device->dev_alloc_list);
461
462 list_add(&device->dev_list, &fs_devices->devices);
463 device->fs_devices = fs_devices;
464 fs_devices->num_devices++;
465 }
Chris Masone5e9a522009-06-10 15:17:02 -0400466 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500467 return fs_devices;
468error:
Chris Masone5e9a522009-06-10 15:17:02 -0400469 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500470 free_fs_devices(fs_devices);
471 return ERR_PTR(-ENOMEM);
472}
473
Chris Masondfe25022008-05-13 13:46:40 -0400474int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
475{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500476 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400477
478 mutex_lock(&uuid_mutex);
479again:
Chris Masone5e9a522009-06-10 15:17:02 -0400480 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500481 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500482 if (device->in_fs_metadata)
483 continue;
484
485 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100486 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500487 device->bdev = NULL;
488 fs_devices->open_devices--;
489 }
490 if (device->writeable) {
491 list_del_init(&device->dev_alloc_list);
492 device->writeable = 0;
493 fs_devices->rw_devices--;
494 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500495 list_del_init(&device->dev_list);
496 fs_devices->num_devices--;
497 kfree(device->name);
498 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400499 }
Chris Masone5e9a522009-06-10 15:17:02 -0400500 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -0500501
502 if (fs_devices->seed) {
503 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500504 goto again;
505 }
506
Chris Masondfe25022008-05-13 13:46:40 -0400507 mutex_unlock(&uuid_mutex);
508 return 0;
509}
Chris Masona0af4692008-05-13 16:03:06 -0400510
Yan Zheng2b820322008-11-17 21:11:30 -0500511static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400512{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400513 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500514
Yan Zheng2b820322008-11-17 21:11:30 -0500515 if (--fs_devices->opened > 0)
516 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400517
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500518 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400519 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100520 blkdev_put(device->bdev, device->mode);
Chris Masona0af4692008-05-13 16:03:06 -0400521 fs_devices->open_devices--;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400522 }
Yan Zheng2b820322008-11-17 21:11:30 -0500523 if (device->writeable) {
524 list_del_init(&device->dev_alloc_list);
525 fs_devices->rw_devices--;
526 }
527
Chris Mason8a4b83c2008-03-24 15:02:07 -0400528 device->bdev = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500529 device->writeable = 0;
Chris Masondfe25022008-05-13 13:46:40 -0400530 device->in_fs_metadata = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400531 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500532 WARN_ON(fs_devices->open_devices);
533 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500534 fs_devices->opened = 0;
535 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500536
Chris Mason8a4b83c2008-03-24 15:02:07 -0400537 return 0;
538}
539
Yan Zheng2b820322008-11-17 21:11:30 -0500540int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
541{
Yan Zhenge4404d62008-12-12 10:03:26 -0500542 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500543 int ret;
544
545 mutex_lock(&uuid_mutex);
546 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500547 if (!fs_devices->opened) {
548 seed_devices = fs_devices->seed;
549 fs_devices->seed = NULL;
550 }
Yan Zheng2b820322008-11-17 21:11:30 -0500551 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500552
553 while (seed_devices) {
554 fs_devices = seed_devices;
555 seed_devices = fs_devices->seed;
556 __btrfs_close_devices(fs_devices);
557 free_fs_devices(fs_devices);
558 }
Yan Zheng2b820322008-11-17 21:11:30 -0500559 return ret;
560}
561
Yan Zhenge4404d62008-12-12 10:03:26 -0500562static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
563 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400564{
565 struct block_device *bdev;
566 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400567 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400568 struct block_device *latest_bdev = NULL;
569 struct buffer_head *bh;
570 struct btrfs_super_block *disk_super;
571 u64 latest_devid = 0;
572 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400573 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500574 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400575 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400576
Tejun Heod4d77622010-11-13 11:55:18 +0100577 flags |= FMODE_EXCL;
578
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500579 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400580 if (device->bdev)
581 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400582 if (!device->name)
583 continue;
584
Tejun Heod4d77622010-11-13 11:55:18 +0100585 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400586 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500587 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400588 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400589 }
Chris Masona061fc82008-05-07 11:43:44 -0400590 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400591
Yan Zhenga512bbf2008-12-08 16:46:26 -0500592 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000593 if (!bh) {
594 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400595 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000596 }
Chris Masona0af4692008-05-13 16:03:06 -0400597
598 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000599 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400600 if (devid != device->devid)
601 goto error_brelse;
602
Yan Zheng2b820322008-11-17 21:11:30 -0500603 if (memcmp(device->uuid, disk_super->dev_item.uuid,
604 BTRFS_UUID_SIZE))
605 goto error_brelse;
606
607 device->generation = btrfs_super_generation(disk_super);
608 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400609 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500610 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400611 latest_bdev = bdev;
612 }
613
Yan Zheng2b820322008-11-17 21:11:30 -0500614 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
615 device->writeable = 0;
616 } else {
617 device->writeable = !bdev_read_only(bdev);
618 seeding = 0;
619 }
620
Chris Mason8a4b83c2008-03-24 15:02:07 -0400621 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400622 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500623 device->mode = flags;
624
Chris Masonc2898112009-06-10 09:51:32 -0400625 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
626 fs_devices->rotating = 1;
627
Chris Masona0af4692008-05-13 16:03:06 -0400628 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500629 if (device->writeable) {
630 fs_devices->rw_devices++;
631 list_add(&device->dev_alloc_list,
632 &fs_devices->alloc_list);
633 }
Chris Masona0af4692008-05-13 16:03:06 -0400634 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400635
Chris Masona0af4692008-05-13 16:03:06 -0400636error_brelse:
637 brelse(bh);
638error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100639 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400640error:
641 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400642 }
Chris Masona0af4692008-05-13 16:03:06 -0400643 if (fs_devices->open_devices == 0) {
644 ret = -EIO;
645 goto out;
646 }
Yan Zheng2b820322008-11-17 21:11:30 -0500647 fs_devices->seeding = seeding;
648 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400649 fs_devices->latest_bdev = latest_bdev;
650 fs_devices->latest_devid = latest_devid;
651 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500652 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400653out:
Yan Zheng2b820322008-11-17 21:11:30 -0500654 return ret;
655}
656
657int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500658 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500659{
660 int ret;
661
662 mutex_lock(&uuid_mutex);
663 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500664 fs_devices->opened++;
665 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500666 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500667 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500668 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400669 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400670 return ret;
671}
672
Christoph Hellwig97288f22008-12-02 06:36:09 -0500673int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400674 struct btrfs_fs_devices **fs_devices_ret)
675{
676 struct btrfs_super_block *disk_super;
677 struct block_device *bdev;
678 struct buffer_head *bh;
679 int ret;
680 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400681 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400682
683 mutex_lock(&uuid_mutex);
684
Tejun Heod4d77622010-11-13 11:55:18 +0100685 flags |= FMODE_EXCL;
686 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400687
688 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400689 ret = PTR_ERR(bdev);
690 goto error;
691 }
692
693 ret = set_blocksize(bdev, 4096);
694 if (ret)
695 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500696 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400697 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000698 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400699 goto error_close;
700 }
701 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000702 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400703 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400704 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500705 printk(KERN_INFO "device label %s ", disk_super->label);
Chris Mason7ae9c092008-04-18 10:29:49 -0400706 else {
707 /* FIXME, make a readl uuid parser */
Chris Masond3977122009-01-05 21:25:51 -0500708 printk(KERN_INFO "device fsid %llx-%llx ",
Chris Mason7ae9c092008-04-18 10:29:49 -0400709 *(unsigned long long *)disk_super->fsid,
710 *(unsigned long long *)(disk_super->fsid + 8));
711 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500712 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500713 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400714 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
715
Chris Mason8a4b83c2008-03-24 15:02:07 -0400716 brelse(bh);
717error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100718 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400719error:
720 mutex_unlock(&uuid_mutex);
721 return ret;
722}
Chris Mason0b86a832008-03-24 15:01:56 -0400723
Miao Xie6d07bce2011-01-05 10:07:31 +0000724/* helper to account the used device space in the range */
725int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
726 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400727{
728 struct btrfs_key key;
729 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000730 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500731 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000732 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400733 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000734 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400735 struct extent_buffer *l;
736
Miao Xie6d07bce2011-01-05 10:07:31 +0000737 *length = 0;
738
739 if (start >= device->total_bytes)
740 return 0;
741
Yan Zheng2b820322008-11-17 21:11:30 -0500742 path = btrfs_alloc_path();
743 if (!path)
744 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400745 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400746
Chris Mason0b86a832008-03-24 15:01:56 -0400747 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000748 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400749 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000750
751 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400752 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000753 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400754 if (ret > 0) {
755 ret = btrfs_previous_item(root, path, key.objectid, key.type);
756 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000757 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400758 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000759
Chris Mason0b86a832008-03-24 15:01:56 -0400760 while (1) {
761 l = path->nodes[0];
762 slot = path->slots[0];
763 if (slot >= btrfs_header_nritems(l)) {
764 ret = btrfs_next_leaf(root, path);
765 if (ret == 0)
766 continue;
767 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000768 goto out;
769
770 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400771 }
772 btrfs_item_key_to_cpu(l, &key, slot);
773
774 if (key.objectid < device->devid)
775 goto next;
776
777 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000778 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400779
Chris Masond3977122009-01-05 21:25:51 -0500780 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400781 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400782
Chris Mason0b86a832008-03-24 15:01:56 -0400783 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000784 extent_end = key.offset + btrfs_dev_extent_length(l,
785 dev_extent);
786 if (key.offset <= start && extent_end > end) {
787 *length = end - start + 1;
788 break;
789 } else if (key.offset <= start && extent_end > start)
790 *length += extent_end - start;
791 else if (key.offset > start && extent_end <= end)
792 *length += extent_end - key.offset;
793 else if (key.offset > start && key.offset <= end) {
794 *length += end - key.offset + 1;
795 break;
796 } else if (key.offset > end)
797 break;
798
799next:
800 path->slots[0]++;
801 }
802 ret = 0;
803out:
804 btrfs_free_path(path);
805 return ret;
806}
807
Chris Mason0b86a832008-03-24 15:01:56 -0400808/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000809 * find_free_dev_extent - find free space in the specified device
810 * @trans: transaction handler
811 * @device: the device which we search the free space in
812 * @num_bytes: the size of the free space that we need
813 * @start: store the start of the free space.
814 * @len: the size of the free space. that we find, or the size of the max
815 * free space if we don't find suitable free space
816 *
Chris Mason0b86a832008-03-24 15:01:56 -0400817 * this uses a pretty simple search, the expectation is that it is
818 * called very infrequently and that a given device has a small number
819 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000820 *
821 * @start is used to store the start of the free space if we find. But if we
822 * don't find suitable free space, it will be used to store the start position
823 * of the max free space.
824 *
825 * @len is used to store the size of the free space that we find.
826 * But if we don't find suitable free space, it is used to store the size of
827 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400828 */
829int find_free_dev_extent(struct btrfs_trans_handle *trans,
830 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000831 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400832{
833 struct btrfs_key key;
834 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000835 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400836 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000837 u64 hole_size;
838 u64 max_hole_start;
839 u64 max_hole_size;
840 u64 extent_end;
841 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400842 u64 search_end = device->total_bytes;
843 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000844 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400845 struct extent_buffer *l;
846
Chris Mason0b86a832008-03-24 15:01:56 -0400847 /* FIXME use last free of some kind */
848
849 /* we don't want to overwrite the superblock on the drive,
850 * so we make sure to start at an offset of at least 1MB
851 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200852 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400853
Miao Xie7bfc8372011-01-05 10:07:26 +0000854 max_hole_start = search_start;
855 max_hole_size = 0;
856
857 if (search_start >= search_end) {
858 ret = -ENOSPC;
859 goto error;
860 }
861
862 path = btrfs_alloc_path();
863 if (!path) {
864 ret = -ENOMEM;
865 goto error;
866 }
867 path->reada = 2;
868
Chris Mason0b86a832008-03-24 15:01:56 -0400869 key.objectid = device->devid;
870 key.offset = search_start;
871 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000872
Chris Mason0b86a832008-03-24 15:01:56 -0400873 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
874 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000875 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400876 if (ret > 0) {
877 ret = btrfs_previous_item(root, path, key.objectid, key.type);
878 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000879 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400880 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000881
Chris Mason0b86a832008-03-24 15:01:56 -0400882 while (1) {
883 l = path->nodes[0];
884 slot = path->slots[0];
885 if (slot >= btrfs_header_nritems(l)) {
886 ret = btrfs_next_leaf(root, path);
887 if (ret == 0)
888 continue;
889 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000890 goto out;
891
892 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400893 }
894 btrfs_item_key_to_cpu(l, &key, slot);
895
896 if (key.objectid < device->devid)
897 goto next;
898
899 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000900 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400901
Chris Mason0b86a832008-03-24 15:01:56 -0400902 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
903 goto next;
904
Miao Xie7bfc8372011-01-05 10:07:26 +0000905 if (key.offset > search_start) {
906 hole_size = key.offset - search_start;
907
908 if (hole_size > max_hole_size) {
909 max_hole_start = search_start;
910 max_hole_size = hole_size;
911 }
912
913 /*
914 * If this free space is greater than which we need,
915 * it must be the max free space that we have found
916 * until now, so max_hole_start must point to the start
917 * of this free space and the length of this free space
918 * is stored in max_hole_size. Thus, we return
919 * max_hole_start and max_hole_size and go back to the
920 * caller.
921 */
922 if (hole_size >= num_bytes) {
923 ret = 0;
924 goto out;
925 }
926 }
927
Chris Mason0b86a832008-03-24 15:01:56 -0400928 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000929 extent_end = key.offset + btrfs_dev_extent_length(l,
930 dev_extent);
931 if (extent_end > search_start)
932 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400933next:
934 path->slots[0]++;
935 cond_resched();
936 }
Chris Mason0b86a832008-03-24 15:01:56 -0400937
Miao Xie7bfc8372011-01-05 10:07:26 +0000938 hole_size = search_end- search_start;
939 if (hole_size > max_hole_size) {
940 max_hole_start = search_start;
941 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400942 }
Chris Mason0b86a832008-03-24 15:01:56 -0400943
Miao Xie7bfc8372011-01-05 10:07:26 +0000944 /* See above. */
945 if (hole_size < num_bytes)
946 ret = -ENOSPC;
947 else
948 ret = 0;
949
950out:
Yan Zheng2b820322008-11-17 21:11:30 -0500951 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000952error:
953 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000954 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000955 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400956 return ret;
957}
958
Christoph Hellwigb2950862008-12-02 09:54:17 -0500959static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400960 struct btrfs_device *device,
961 u64 start)
962{
963 int ret;
964 struct btrfs_path *path;
965 struct btrfs_root *root = device->dev_root;
966 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400967 struct btrfs_key found_key;
968 struct extent_buffer *leaf = NULL;
969 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400970
971 path = btrfs_alloc_path();
972 if (!path)
973 return -ENOMEM;
974
975 key.objectid = device->devid;
976 key.offset = start;
977 key.type = BTRFS_DEV_EXTENT_KEY;
978
979 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400980 if (ret > 0) {
981 ret = btrfs_previous_item(root, path, key.objectid,
982 BTRFS_DEV_EXTENT_KEY);
983 BUG_ON(ret);
984 leaf = path->nodes[0];
985 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
986 extent = btrfs_item_ptr(leaf, path->slots[0],
987 struct btrfs_dev_extent);
988 BUG_ON(found_key.offset > start || found_key.offset +
989 btrfs_dev_extent_length(leaf, extent) < start);
990 ret = 0;
991 } else if (ret == 0) {
992 leaf = path->nodes[0];
993 extent = btrfs_item_ptr(leaf, path->slots[0],
994 struct btrfs_dev_extent);
995 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400996 BUG_ON(ret);
997
Chris Masondfe25022008-05-13 13:46:40 -0400998 if (device->bytes_used > 0)
999 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -04001000 ret = btrfs_del_item(trans, root, path);
1001 BUG_ON(ret);
1002
1003 btrfs_free_path(path);
1004 return ret;
1005}
1006
Yan Zheng2b820322008-11-17 21:11:30 -05001007int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001008 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001009 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001010 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001011{
1012 int ret;
1013 struct btrfs_path *path;
1014 struct btrfs_root *root = device->dev_root;
1015 struct btrfs_dev_extent *extent;
1016 struct extent_buffer *leaf;
1017 struct btrfs_key key;
1018
Chris Masondfe25022008-05-13 13:46:40 -04001019 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001020 path = btrfs_alloc_path();
1021 if (!path)
1022 return -ENOMEM;
1023
Chris Mason0b86a832008-03-24 15:01:56 -04001024 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001025 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001026 key.type = BTRFS_DEV_EXTENT_KEY;
1027 ret = btrfs_insert_empty_item(trans, root, path, &key,
1028 sizeof(*extent));
1029 BUG_ON(ret);
1030
1031 leaf = path->nodes[0];
1032 extent = btrfs_item_ptr(leaf, path->slots[0],
1033 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001034 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1035 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1036 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1037
1038 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1039 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1040 BTRFS_UUID_SIZE);
1041
Chris Mason0b86a832008-03-24 15:01:56 -04001042 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1043 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001044 btrfs_free_path(path);
1045 return ret;
1046}
1047
Chris Masona1b32a52008-09-05 16:09:51 -04001048static noinline int find_next_chunk(struct btrfs_root *root,
1049 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001050{
1051 struct btrfs_path *path;
1052 int ret;
1053 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001054 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001055 struct btrfs_key found_key;
1056
1057 path = btrfs_alloc_path();
1058 BUG_ON(!path);
1059
Chris Masone17cade2008-04-15 15:41:47 -04001060 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001061 key.offset = (u64)-1;
1062 key.type = BTRFS_CHUNK_ITEM_KEY;
1063
1064 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1065 if (ret < 0)
1066 goto error;
1067
1068 BUG_ON(ret == 0);
1069
1070 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1071 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001072 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001073 } else {
1074 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1075 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001076 if (found_key.objectid != objectid)
1077 *offset = 0;
1078 else {
1079 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1080 struct btrfs_chunk);
1081 *offset = found_key.offset +
1082 btrfs_chunk_length(path->nodes[0], chunk);
1083 }
Chris Mason0b86a832008-03-24 15:01:56 -04001084 }
1085 ret = 0;
1086error:
1087 btrfs_free_path(path);
1088 return ret;
1089}
1090
Yan Zheng2b820322008-11-17 21:11:30 -05001091static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001092{
1093 int ret;
1094 struct btrfs_key key;
1095 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001096 struct btrfs_path *path;
1097
1098 root = root->fs_info->chunk_root;
1099
1100 path = btrfs_alloc_path();
1101 if (!path)
1102 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001103
1104 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1105 key.type = BTRFS_DEV_ITEM_KEY;
1106 key.offset = (u64)-1;
1107
1108 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1109 if (ret < 0)
1110 goto error;
1111
1112 BUG_ON(ret == 0);
1113
1114 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1115 BTRFS_DEV_ITEM_KEY);
1116 if (ret) {
1117 *objectid = 1;
1118 } else {
1119 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1120 path->slots[0]);
1121 *objectid = found_key.offset + 1;
1122 }
1123 ret = 0;
1124error:
Yan Zheng2b820322008-11-17 21:11:30 -05001125 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001126 return ret;
1127}
1128
1129/*
1130 * the device information is stored in the chunk root
1131 * the btrfs_device struct should be fully filled in
1132 */
1133int btrfs_add_device(struct btrfs_trans_handle *trans,
1134 struct btrfs_root *root,
1135 struct btrfs_device *device)
1136{
1137 int ret;
1138 struct btrfs_path *path;
1139 struct btrfs_dev_item *dev_item;
1140 struct extent_buffer *leaf;
1141 struct btrfs_key key;
1142 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001143
1144 root = root->fs_info->chunk_root;
1145
1146 path = btrfs_alloc_path();
1147 if (!path)
1148 return -ENOMEM;
1149
Chris Mason0b86a832008-03-24 15:01:56 -04001150 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1151 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001152 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001153
1154 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001155 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001156 if (ret)
1157 goto out;
1158
1159 leaf = path->nodes[0];
1160 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1161
1162 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001163 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001164 btrfs_set_device_type(leaf, dev_item, device->type);
1165 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1166 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1167 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001168 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1169 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001170 btrfs_set_device_group(leaf, dev_item, 0);
1171 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1172 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001173 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001174
Chris Mason0b86a832008-03-24 15:01:56 -04001175 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001176 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001177 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1178 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001179 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001180
Yan Zheng2b820322008-11-17 21:11:30 -05001181 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001182out:
1183 btrfs_free_path(path);
1184 return ret;
1185}
Chris Mason8f18cf12008-04-25 16:53:30 -04001186
Chris Masona061fc82008-05-07 11:43:44 -04001187static int btrfs_rm_dev_item(struct btrfs_root *root,
1188 struct btrfs_device *device)
1189{
1190 int ret;
1191 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001192 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001193 struct btrfs_trans_handle *trans;
1194
1195 root = root->fs_info->chunk_root;
1196
1197 path = btrfs_alloc_path();
1198 if (!path)
1199 return -ENOMEM;
1200
Yan, Zhenga22285a2010-05-16 10:48:46 -04001201 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001202 if (IS_ERR(trans)) {
1203 btrfs_free_path(path);
1204 return PTR_ERR(trans);
1205 }
Chris Masona061fc82008-05-07 11:43:44 -04001206 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1207 key.type = BTRFS_DEV_ITEM_KEY;
1208 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001209 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001210
1211 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1212 if (ret < 0)
1213 goto out;
1214
1215 if (ret > 0) {
1216 ret = -ENOENT;
1217 goto out;
1218 }
1219
1220 ret = btrfs_del_item(trans, root, path);
1221 if (ret)
1222 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001223out:
1224 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001225 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001226 btrfs_commit_transaction(trans, root);
1227 return ret;
1228}
1229
1230int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1231{
1232 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001233 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001234 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001235 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001236 struct btrfs_super_block *disk_super;
1237 u64 all_avail;
1238 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001239 u64 num_devices;
1240 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001241 int ret = 0;
1242
Chris Masona061fc82008-05-07 11:43:44 -04001243 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001244 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001245
1246 all_avail = root->fs_info->avail_data_alloc_bits |
1247 root->fs_info->avail_system_alloc_bits |
1248 root->fs_info->avail_metadata_alloc_bits;
1249
1250 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001251 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001252 printk(KERN_ERR "btrfs: unable to go below four devices "
1253 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001254 ret = -EINVAL;
1255 goto out;
1256 }
1257
1258 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001259 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001260 printk(KERN_ERR "btrfs: unable to go below two "
1261 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001262 ret = -EINVAL;
1263 goto out;
1264 }
1265
Chris Masondfe25022008-05-13 13:46:40 -04001266 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001267 struct list_head *devices;
1268 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001269
Chris Masondfe25022008-05-13 13:46:40 -04001270 device = NULL;
1271 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001272 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001273 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001274 if (tmp->in_fs_metadata && !tmp->bdev) {
1275 device = tmp;
1276 break;
1277 }
1278 }
Chris Masone5e9a522009-06-10 15:17:02 -04001279 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001280 bdev = NULL;
1281 bh = NULL;
1282 disk_super = NULL;
1283 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001284 printk(KERN_ERR "btrfs: no missing devices found to "
1285 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001286 goto out;
1287 }
Chris Masondfe25022008-05-13 13:46:40 -04001288 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001289 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1290 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001291 if (IS_ERR(bdev)) {
1292 ret = PTR_ERR(bdev);
1293 goto out;
1294 }
1295
Yan Zheng2b820322008-11-17 21:11:30 -05001296 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001297 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001298 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001299 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001300 goto error_close;
1301 }
1302 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001303 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001304 dev_uuid = disk_super->dev_item.uuid;
1305 device = btrfs_find_device(root, devid, dev_uuid,
1306 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001307 if (!device) {
1308 ret = -ENOENT;
1309 goto error_brelse;
1310 }
Chris Masondfe25022008-05-13 13:46:40 -04001311 }
Yan Zheng2b820322008-11-17 21:11:30 -05001312
1313 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001314 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1315 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001316 ret = -EINVAL;
1317 goto error_brelse;
1318 }
1319
1320 if (device->writeable) {
1321 list_del_init(&device->dev_alloc_list);
1322 root->fs_info->fs_devices->rw_devices--;
1323 }
Chris Masona061fc82008-05-07 11:43:44 -04001324
1325 ret = btrfs_shrink_device(device, 0);
1326 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001327 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001328
Chris Masona061fc82008-05-07 11:43:44 -04001329 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1330 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001331 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001332
Yan Zheng2b820322008-11-17 21:11:30 -05001333 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001334
1335 /*
1336 * the device list mutex makes sure that we don't change
1337 * the device list while someone else is writing out all
1338 * the device supers.
1339 */
1340 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001341 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001342 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1343
Yan Zhenge4404d62008-12-12 10:03:26 -05001344 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001345
Chris Masoncd02dca2010-12-13 14:56:23 -05001346 if (device->missing)
1347 root->fs_info->fs_devices->missing_devices--;
1348
Yan Zheng2b820322008-11-17 21:11:30 -05001349 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1350 struct btrfs_device, dev_list);
1351 if (device->bdev == root->fs_info->sb->s_bdev)
1352 root->fs_info->sb->s_bdev = next_device->bdev;
1353 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1354 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1355
Yan Zhenge4404d62008-12-12 10:03:26 -05001356 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001357 blkdev_put(device->bdev, device->mode);
Yan Zhenge4404d62008-12-12 10:03:26 -05001358 device->bdev = NULL;
1359 device->fs_devices->open_devices--;
1360 }
1361
Yan Zheng2b820322008-11-17 21:11:30 -05001362 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1363 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1364
Yan Zhenge4404d62008-12-12 10:03:26 -05001365 if (device->fs_devices->open_devices == 0) {
1366 struct btrfs_fs_devices *fs_devices;
1367 fs_devices = root->fs_info->fs_devices;
1368 while (fs_devices) {
1369 if (fs_devices->seed == device->fs_devices)
1370 break;
1371 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001372 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001373 fs_devices->seed = device->fs_devices->seed;
1374 device->fs_devices->seed = NULL;
1375 __btrfs_close_devices(device->fs_devices);
1376 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001377 }
1378
1379 /*
1380 * at this point, the device is zero sized. We want to
1381 * remove it from the devices list and zero out the old super
1382 */
1383 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001384 /* make sure this device isn't detected as part of
1385 * the FS anymore
1386 */
1387 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1388 set_buffer_dirty(bh);
1389 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001390 }
Chris Masona061fc82008-05-07 11:43:44 -04001391
Chris Masona061fc82008-05-07 11:43:44 -04001392 kfree(device->name);
1393 kfree(device);
1394 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001395
1396error_brelse:
1397 brelse(bh);
1398error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001399 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001400 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001401out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001402 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001403 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001404 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001405error_undo:
1406 if (device->writeable) {
1407 list_add(&device->dev_alloc_list,
1408 &root->fs_info->fs_devices->alloc_list);
1409 root->fs_info->fs_devices->rw_devices++;
1410 }
1411 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001412}
1413
Yan Zheng2b820322008-11-17 21:11:30 -05001414/*
1415 * does all the dirty work required for changing file system's UUID.
1416 */
1417static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1418 struct btrfs_root *root)
1419{
1420 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1421 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001422 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001423 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1424 struct btrfs_device *device;
1425 u64 super_flags;
1426
1427 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001428 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001429 return -EINVAL;
1430
Yan Zhenge4404d62008-12-12 10:03:26 -05001431 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1432 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001433 return -ENOMEM;
1434
Yan Zhenge4404d62008-12-12 10:03:26 -05001435 old_devices = clone_fs_devices(fs_devices);
1436 if (IS_ERR(old_devices)) {
1437 kfree(seed_devices);
1438 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001439 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001440
Yan Zheng2b820322008-11-17 21:11:30 -05001441 list_add(&old_devices->list, &fs_uuids);
1442
Yan Zhenge4404d62008-12-12 10:03:26 -05001443 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1444 seed_devices->opened = 1;
1445 INIT_LIST_HEAD(&seed_devices->devices);
1446 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001447 mutex_init(&seed_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001448 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1449 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1450 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1451 device->fs_devices = seed_devices;
1452 }
1453
Yan Zheng2b820322008-11-17 21:11:30 -05001454 fs_devices->seeding = 0;
1455 fs_devices->num_devices = 0;
1456 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001457 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001458
1459 generate_random_uuid(fs_devices->fsid);
1460 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1461 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1462 super_flags = btrfs_super_flags(disk_super) &
1463 ~BTRFS_SUPER_FLAG_SEEDING;
1464 btrfs_set_super_flags(disk_super, super_flags);
1465
1466 return 0;
1467}
1468
1469/*
1470 * strore the expected generation for seed devices in device items.
1471 */
1472static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1473 struct btrfs_root *root)
1474{
1475 struct btrfs_path *path;
1476 struct extent_buffer *leaf;
1477 struct btrfs_dev_item *dev_item;
1478 struct btrfs_device *device;
1479 struct btrfs_key key;
1480 u8 fs_uuid[BTRFS_UUID_SIZE];
1481 u8 dev_uuid[BTRFS_UUID_SIZE];
1482 u64 devid;
1483 int ret;
1484
1485 path = btrfs_alloc_path();
1486 if (!path)
1487 return -ENOMEM;
1488
1489 root = root->fs_info->chunk_root;
1490 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1491 key.offset = 0;
1492 key.type = BTRFS_DEV_ITEM_KEY;
1493
1494 while (1) {
1495 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1496 if (ret < 0)
1497 goto error;
1498
1499 leaf = path->nodes[0];
1500next_slot:
1501 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1502 ret = btrfs_next_leaf(root, path);
1503 if (ret > 0)
1504 break;
1505 if (ret < 0)
1506 goto error;
1507 leaf = path->nodes[0];
1508 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1509 btrfs_release_path(root, path);
1510 continue;
1511 }
1512
1513 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1514 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1515 key.type != BTRFS_DEV_ITEM_KEY)
1516 break;
1517
1518 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1519 struct btrfs_dev_item);
1520 devid = btrfs_device_id(leaf, dev_item);
1521 read_extent_buffer(leaf, dev_uuid,
1522 (unsigned long)btrfs_device_uuid(dev_item),
1523 BTRFS_UUID_SIZE);
1524 read_extent_buffer(leaf, fs_uuid,
1525 (unsigned long)btrfs_device_fsid(dev_item),
1526 BTRFS_UUID_SIZE);
1527 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1528 BUG_ON(!device);
1529
1530 if (device->fs_devices->seeding) {
1531 btrfs_set_device_generation(leaf, dev_item,
1532 device->generation);
1533 btrfs_mark_buffer_dirty(leaf);
1534 }
1535
1536 path->slots[0]++;
1537 goto next_slot;
1538 }
1539 ret = 0;
1540error:
1541 btrfs_free_path(path);
1542 return ret;
1543}
1544
Chris Mason788f20e2008-04-28 15:29:42 -04001545int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1546{
1547 struct btrfs_trans_handle *trans;
1548 struct btrfs_device *device;
1549 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001550 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001551 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001552 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001553 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001554 int ret = 0;
1555
Yan Zheng2b820322008-11-17 21:11:30 -05001556 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1557 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001558
Tejun Heod4d77622010-11-13 11:55:18 +01001559 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1560 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001561 if (IS_ERR(bdev))
1562 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001563
Yan Zheng2b820322008-11-17 21:11:30 -05001564 if (root->fs_info->fs_devices->seeding) {
1565 seeding_dev = 1;
1566 down_write(&sb->s_umount);
1567 mutex_lock(&uuid_mutex);
1568 }
1569
Chris Mason8c8bee12008-09-29 11:19:10 -04001570 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001571 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001572
Chris Mason788f20e2008-04-28 15:29:42 -04001573 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001574 /*
1575 * we have the volume lock, so we don't need the extra
1576 * device list mutex while reading the list here.
1577 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001578 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001579 if (device->bdev == bdev) {
1580 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001581 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001582 }
1583 }
1584
1585 device = kzalloc(sizeof(*device), GFP_NOFS);
1586 if (!device) {
1587 /* we can safely leave the fs_devices entry around */
1588 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001589 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001590 }
1591
Chris Mason788f20e2008-04-28 15:29:42 -04001592 device->name = kstrdup(device_path, GFP_NOFS);
1593 if (!device->name) {
1594 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001595 ret = -ENOMEM;
1596 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001597 }
Yan Zheng2b820322008-11-17 21:11:30 -05001598
1599 ret = find_next_devid(root, &device->devid);
1600 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001601 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001602 kfree(device);
1603 goto error;
1604 }
1605
Yan, Zhenga22285a2010-05-16 10:48:46 -04001606 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001607 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001608 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001609 kfree(device);
1610 ret = PTR_ERR(trans);
1611 goto error;
1612 }
1613
Yan Zheng2b820322008-11-17 21:11:30 -05001614 lock_chunks(root);
1615
Yan Zheng2b820322008-11-17 21:11:30 -05001616 device->writeable = 1;
1617 device->work.func = pending_bios_fn;
1618 generate_random_uuid(device->uuid);
1619 spin_lock_init(&device->io_lock);
1620 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001621 device->io_width = root->sectorsize;
1622 device->io_align = root->sectorsize;
1623 device->sector_size = root->sectorsize;
1624 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001625 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001626 device->dev_root = root->fs_info->dev_root;
1627 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001628 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001629 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001630 set_blocksize(device->bdev, 4096);
1631
Yan Zheng2b820322008-11-17 21:11:30 -05001632 if (seeding_dev) {
1633 sb->s_flags &= ~MS_RDONLY;
1634 ret = btrfs_prepare_sprout(trans, root);
1635 BUG_ON(ret);
1636 }
1637
1638 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001639
1640 /*
1641 * we don't want write_supers to jump in here with our device
1642 * half setup
1643 */
1644 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001645 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1646 list_add(&device->dev_alloc_list,
1647 &root->fs_info->fs_devices->alloc_list);
1648 root->fs_info->fs_devices->num_devices++;
1649 root->fs_info->fs_devices->open_devices++;
1650 root->fs_info->fs_devices->rw_devices++;
1651 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1652
Chris Masonc2898112009-06-10 09:51:32 -04001653 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1654 root->fs_info->fs_devices->rotating = 1;
1655
Chris Mason788f20e2008-04-28 15:29:42 -04001656 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1657 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1658 total_bytes + device->total_bytes);
1659
1660 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1661 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1662 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001663 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001664
Yan Zheng2b820322008-11-17 21:11:30 -05001665 if (seeding_dev) {
1666 ret = init_first_rw_device(trans, root, device);
1667 BUG_ON(ret);
1668 ret = btrfs_finish_sprout(trans, root);
1669 BUG_ON(ret);
1670 } else {
1671 ret = btrfs_add_device(trans, root, device);
1672 }
1673
Chris Mason913d9522009-03-10 13:17:18 -04001674 /*
1675 * we've got more storage, clear any full flags on the space
1676 * infos
1677 */
1678 btrfs_clear_space_info_full(root->fs_info);
1679
Chris Mason7d9eb122008-07-08 14:19:17 -04001680 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001681 btrfs_commit_transaction(trans, root);
1682
1683 if (seeding_dev) {
1684 mutex_unlock(&uuid_mutex);
1685 up_write(&sb->s_umount);
1686
1687 ret = btrfs_relocate_sys_chunks(root);
1688 BUG_ON(ret);
1689 }
1690out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001691 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001692 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001693error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001694 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001695 if (seeding_dev) {
1696 mutex_unlock(&uuid_mutex);
1697 up_write(&sb->s_umount);
1698 }
Chris Mason788f20e2008-04-28 15:29:42 -04001699 goto out;
1700}
1701
Chris Masond3977122009-01-05 21:25:51 -05001702static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1703 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001704{
1705 int ret;
1706 struct btrfs_path *path;
1707 struct btrfs_root *root;
1708 struct btrfs_dev_item *dev_item;
1709 struct extent_buffer *leaf;
1710 struct btrfs_key key;
1711
1712 root = device->dev_root->fs_info->chunk_root;
1713
1714 path = btrfs_alloc_path();
1715 if (!path)
1716 return -ENOMEM;
1717
1718 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1719 key.type = BTRFS_DEV_ITEM_KEY;
1720 key.offset = device->devid;
1721
1722 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1723 if (ret < 0)
1724 goto out;
1725
1726 if (ret > 0) {
1727 ret = -ENOENT;
1728 goto out;
1729 }
1730
1731 leaf = path->nodes[0];
1732 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1733
1734 btrfs_set_device_id(leaf, dev_item, device->devid);
1735 btrfs_set_device_type(leaf, dev_item, device->type);
1736 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1737 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1738 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001739 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001740 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1741 btrfs_mark_buffer_dirty(leaf);
1742
1743out:
1744 btrfs_free_path(path);
1745 return ret;
1746}
1747
Chris Mason7d9eb122008-07-08 14:19:17 -04001748static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001749 struct btrfs_device *device, u64 new_size)
1750{
1751 struct btrfs_super_block *super_copy =
1752 &device->dev_root->fs_info->super_copy;
1753 u64 old_total = btrfs_super_total_bytes(super_copy);
1754 u64 diff = new_size - device->total_bytes;
1755
Yan Zheng2b820322008-11-17 21:11:30 -05001756 if (!device->writeable)
1757 return -EACCES;
1758 if (new_size <= device->total_bytes)
1759 return -EINVAL;
1760
Chris Mason8f18cf12008-04-25 16:53:30 -04001761 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001762 device->fs_devices->total_rw_bytes += diff;
1763
1764 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001765 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001766 btrfs_clear_space_info_full(device->dev_root->fs_info);
1767
Chris Mason8f18cf12008-04-25 16:53:30 -04001768 return btrfs_update_device(trans, device);
1769}
1770
Chris Mason7d9eb122008-07-08 14:19:17 -04001771int btrfs_grow_device(struct btrfs_trans_handle *trans,
1772 struct btrfs_device *device, u64 new_size)
1773{
1774 int ret;
1775 lock_chunks(device->dev_root);
1776 ret = __btrfs_grow_device(trans, device, new_size);
1777 unlock_chunks(device->dev_root);
1778 return ret;
1779}
1780
Chris Mason8f18cf12008-04-25 16:53:30 -04001781static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1782 struct btrfs_root *root,
1783 u64 chunk_tree, u64 chunk_objectid,
1784 u64 chunk_offset)
1785{
1786 int ret;
1787 struct btrfs_path *path;
1788 struct btrfs_key key;
1789
1790 root = root->fs_info->chunk_root;
1791 path = btrfs_alloc_path();
1792 if (!path)
1793 return -ENOMEM;
1794
1795 key.objectid = chunk_objectid;
1796 key.offset = chunk_offset;
1797 key.type = BTRFS_CHUNK_ITEM_KEY;
1798
1799 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1800 BUG_ON(ret);
1801
1802 ret = btrfs_del_item(trans, root, path);
1803 BUG_ON(ret);
1804
1805 btrfs_free_path(path);
1806 return 0;
1807}
1808
Christoph Hellwigb2950862008-12-02 09:54:17 -05001809static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001810 chunk_offset)
1811{
1812 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1813 struct btrfs_disk_key *disk_key;
1814 struct btrfs_chunk *chunk;
1815 u8 *ptr;
1816 int ret = 0;
1817 u32 num_stripes;
1818 u32 array_size;
1819 u32 len = 0;
1820 u32 cur;
1821 struct btrfs_key key;
1822
1823 array_size = btrfs_super_sys_array_size(super_copy);
1824
1825 ptr = super_copy->sys_chunk_array;
1826 cur = 0;
1827
1828 while (cur < array_size) {
1829 disk_key = (struct btrfs_disk_key *)ptr;
1830 btrfs_disk_key_to_cpu(&key, disk_key);
1831
1832 len = sizeof(*disk_key);
1833
1834 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1835 chunk = (struct btrfs_chunk *)(ptr + len);
1836 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1837 len += btrfs_chunk_item_size(num_stripes);
1838 } else {
1839 ret = -EIO;
1840 break;
1841 }
1842 if (key.objectid == chunk_objectid &&
1843 key.offset == chunk_offset) {
1844 memmove(ptr, ptr + len, array_size - (cur + len));
1845 array_size -= len;
1846 btrfs_set_super_sys_array_size(super_copy, array_size);
1847 } else {
1848 ptr += len;
1849 cur += len;
1850 }
1851 }
1852 return ret;
1853}
1854
Christoph Hellwigb2950862008-12-02 09:54:17 -05001855static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001856 u64 chunk_tree, u64 chunk_objectid,
1857 u64 chunk_offset)
1858{
1859 struct extent_map_tree *em_tree;
1860 struct btrfs_root *extent_root;
1861 struct btrfs_trans_handle *trans;
1862 struct extent_map *em;
1863 struct map_lookup *map;
1864 int ret;
1865 int i;
1866
1867 root = root->fs_info->chunk_root;
1868 extent_root = root->fs_info->extent_root;
1869 em_tree = &root->fs_info->mapping_tree.map_tree;
1870
Josef Bacikba1bf482009-09-11 16:11:19 -04001871 ret = btrfs_can_relocate(extent_root, chunk_offset);
1872 if (ret)
1873 return -ENOSPC;
1874
Chris Mason8f18cf12008-04-25 16:53:30 -04001875 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001876 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001877 if (ret)
1878 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001879
Yan, Zhenga22285a2010-05-16 10:48:46 -04001880 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001881 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001882
Chris Mason7d9eb122008-07-08 14:19:17 -04001883 lock_chunks(root);
1884
Chris Mason8f18cf12008-04-25 16:53:30 -04001885 /*
1886 * step two, delete the device extents and the
1887 * chunk tree entries
1888 */
Chris Mason890871b2009-09-02 16:24:52 -04001889 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001890 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001891 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001892
Chris Masona061fc82008-05-07 11:43:44 -04001893 BUG_ON(em->start > chunk_offset ||
1894 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001895 map = (struct map_lookup *)em->bdev;
1896
1897 for (i = 0; i < map->num_stripes; i++) {
1898 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1899 map->stripes[i].physical);
1900 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001901
Chris Masondfe25022008-05-13 13:46:40 -04001902 if (map->stripes[i].dev) {
1903 ret = btrfs_update_device(trans, map->stripes[i].dev);
1904 BUG_ON(ret);
1905 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001906 }
1907 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1908 chunk_offset);
1909
1910 BUG_ON(ret);
1911
liubo1abe9b82011-03-24 11:18:59 +00001912 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1913
Chris Mason8f18cf12008-04-25 16:53:30 -04001914 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1915 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1916 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001917 }
1918
Zheng Yan1a40e232008-09-26 10:09:34 -04001919 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1920 BUG_ON(ret);
1921
Chris Mason890871b2009-09-02 16:24:52 -04001922 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001923 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001924 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001925
Chris Mason8f18cf12008-04-25 16:53:30 -04001926 kfree(map);
1927 em->bdev = NULL;
1928
1929 /* once for the tree */
1930 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001931 /* once for us */
1932 free_extent_map(em);
1933
Chris Mason7d9eb122008-07-08 14:19:17 -04001934 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001935 btrfs_end_transaction(trans, root);
1936 return 0;
1937}
1938
Yan Zheng2b820322008-11-17 21:11:30 -05001939static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1940{
1941 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1942 struct btrfs_path *path;
1943 struct extent_buffer *leaf;
1944 struct btrfs_chunk *chunk;
1945 struct btrfs_key key;
1946 struct btrfs_key found_key;
1947 u64 chunk_tree = chunk_root->root_key.objectid;
1948 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001949 bool retried = false;
1950 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001951 int ret;
1952
1953 path = btrfs_alloc_path();
1954 if (!path)
1955 return -ENOMEM;
1956
Josef Bacikba1bf482009-09-11 16:11:19 -04001957again:
Yan Zheng2b820322008-11-17 21:11:30 -05001958 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1959 key.offset = (u64)-1;
1960 key.type = BTRFS_CHUNK_ITEM_KEY;
1961
1962 while (1) {
1963 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1964 if (ret < 0)
1965 goto error;
1966 BUG_ON(ret == 0);
1967
1968 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1969 key.type);
1970 if (ret < 0)
1971 goto error;
1972 if (ret > 0)
1973 break;
1974
1975 leaf = path->nodes[0];
1976 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1977
1978 chunk = btrfs_item_ptr(leaf, path->slots[0],
1979 struct btrfs_chunk);
1980 chunk_type = btrfs_chunk_type(leaf, chunk);
1981 btrfs_release_path(chunk_root, path);
1982
1983 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1984 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1985 found_key.objectid,
1986 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001987 if (ret == -ENOSPC)
1988 failed++;
1989 else if (ret)
1990 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001991 }
1992
1993 if (found_key.offset == 0)
1994 break;
1995 key.offset = found_key.offset - 1;
1996 }
1997 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04001998 if (failed && !retried) {
1999 failed = 0;
2000 retried = true;
2001 goto again;
2002 } else if (failed && retried) {
2003 WARN_ON(1);
2004 ret = -ENOSPC;
2005 }
Yan Zheng2b820322008-11-17 21:11:30 -05002006error:
2007 btrfs_free_path(path);
2008 return ret;
2009}
2010
Chris Masonec44a352008-04-28 15:29:52 -04002011static u64 div_factor(u64 num, int factor)
2012{
2013 if (factor == 10)
2014 return num;
2015 num *= factor;
2016 do_div(num, 10);
2017 return num;
2018}
2019
Chris Masonec44a352008-04-28 15:29:52 -04002020int btrfs_balance(struct btrfs_root *dev_root)
2021{
2022 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002023 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2024 struct btrfs_device *device;
2025 u64 old_size;
2026 u64 size_to_free;
2027 struct btrfs_path *path;
2028 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002029 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2030 struct btrfs_trans_handle *trans;
2031 struct btrfs_key found_key;
2032
Yan Zheng2b820322008-11-17 21:11:30 -05002033 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2034 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002035
Ben Hutchings6f88a442010-12-29 14:55:03 +00002036 if (!capable(CAP_SYS_ADMIN))
2037 return -EPERM;
2038
Chris Mason7d9eb122008-07-08 14:19:17 -04002039 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002040 dev_root = dev_root->fs_info->dev_root;
2041
Chris Masonec44a352008-04-28 15:29:52 -04002042 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002043 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002044 old_size = device->total_bytes;
2045 size_to_free = div_factor(old_size, 1);
2046 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002047 if (!device->writeable ||
2048 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002049 continue;
2050
2051 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002052 if (ret == -ENOSPC)
2053 break;
Chris Masonec44a352008-04-28 15:29:52 -04002054 BUG_ON(ret);
2055
Yan, Zhenga22285a2010-05-16 10:48:46 -04002056 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002057 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002058
2059 ret = btrfs_grow_device(trans, device, old_size);
2060 BUG_ON(ret);
2061
2062 btrfs_end_transaction(trans, dev_root);
2063 }
2064
2065 /* step two, relocate all the chunks */
2066 path = btrfs_alloc_path();
2067 BUG_ON(!path);
2068
2069 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2070 key.offset = (u64)-1;
2071 key.type = BTRFS_CHUNK_ITEM_KEY;
2072
Chris Masond3977122009-01-05 21:25:51 -05002073 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002074 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2075 if (ret < 0)
2076 goto error;
2077
2078 /*
2079 * this shouldn't happen, it means the last relocate
2080 * failed
2081 */
2082 if (ret == 0)
2083 break;
2084
2085 ret = btrfs_previous_item(chunk_root, path, 0,
2086 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002087 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002088 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002089
Chris Masonec44a352008-04-28 15:29:52 -04002090 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2091 path->slots[0]);
2092 if (found_key.objectid != key.objectid)
2093 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002094
Chris Masonec44a352008-04-28 15:29:52 -04002095 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002096 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002097 break;
2098
Chris Mason7d9eb122008-07-08 14:19:17 -04002099 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04002100 ret = btrfs_relocate_chunk(chunk_root,
2101 chunk_root->root_key.objectid,
2102 found_key.objectid,
2103 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002104 BUG_ON(ret && ret != -ENOSPC);
2105 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002106 }
2107 ret = 0;
2108error:
2109 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002110 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002111 return ret;
2112}
2113
Chris Mason8f18cf12008-04-25 16:53:30 -04002114/*
2115 * shrinking a device means finding all of the device extents past
2116 * the new size, and then following the back refs to the chunks.
2117 * The chunk relocation code actually frees the device extent
2118 */
2119int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2120{
2121 struct btrfs_trans_handle *trans;
2122 struct btrfs_root *root = device->dev_root;
2123 struct btrfs_dev_extent *dev_extent = NULL;
2124 struct btrfs_path *path;
2125 u64 length;
2126 u64 chunk_tree;
2127 u64 chunk_objectid;
2128 u64 chunk_offset;
2129 int ret;
2130 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002131 int failed = 0;
2132 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002133 struct extent_buffer *l;
2134 struct btrfs_key key;
2135 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2136 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002137 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002138 u64 diff = device->total_bytes - new_size;
2139
Yan Zheng2b820322008-11-17 21:11:30 -05002140 if (new_size >= device->total_bytes)
2141 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002142
2143 path = btrfs_alloc_path();
2144 if (!path)
2145 return -ENOMEM;
2146
Chris Mason8f18cf12008-04-25 16:53:30 -04002147 path->reada = 2;
2148
Chris Mason7d9eb122008-07-08 14:19:17 -04002149 lock_chunks(root);
2150
Chris Mason8f18cf12008-04-25 16:53:30 -04002151 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002152 if (device->writeable)
2153 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002154 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002155
Josef Bacikba1bf482009-09-11 16:11:19 -04002156again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002157 key.objectid = device->devid;
2158 key.offset = (u64)-1;
2159 key.type = BTRFS_DEV_EXTENT_KEY;
2160
2161 while (1) {
2162 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2163 if (ret < 0)
2164 goto done;
2165
2166 ret = btrfs_previous_item(root, path, 0, key.type);
2167 if (ret < 0)
2168 goto done;
2169 if (ret) {
2170 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002171 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002172 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002173 }
2174
2175 l = path->nodes[0];
2176 slot = path->slots[0];
2177 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2178
Josef Bacikba1bf482009-09-11 16:11:19 -04002179 if (key.objectid != device->devid) {
2180 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002181 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002182 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002183
2184 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2185 length = btrfs_dev_extent_length(l, dev_extent);
2186
Josef Bacikba1bf482009-09-11 16:11:19 -04002187 if (key.offset + length <= new_size) {
2188 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002189 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002190 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002191
2192 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2193 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2194 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2195 btrfs_release_path(root, path);
2196
2197 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2198 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002199 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002200 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002201 if (ret == -ENOSPC)
2202 failed++;
2203 key.offset -= 1;
2204 }
2205
2206 if (failed && !retried) {
2207 failed = 0;
2208 retried = true;
2209 goto again;
2210 } else if (failed && retried) {
2211 ret = -ENOSPC;
2212 lock_chunks(root);
2213
2214 device->total_bytes = old_size;
2215 if (device->writeable)
2216 device->fs_devices->total_rw_bytes += diff;
2217 unlock_chunks(root);
2218 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002219 }
2220
Chris Balld6397ba2009-04-27 07:29:03 -04002221 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002222 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002223 if (IS_ERR(trans)) {
2224 ret = PTR_ERR(trans);
2225 goto done;
2226 }
2227
Chris Balld6397ba2009-04-27 07:29:03 -04002228 lock_chunks(root);
2229
2230 device->disk_total_bytes = new_size;
2231 /* Now btrfs_update_device() will change the on-disk size. */
2232 ret = btrfs_update_device(trans, device);
2233 if (ret) {
2234 unlock_chunks(root);
2235 btrfs_end_transaction(trans, root);
2236 goto done;
2237 }
2238 WARN_ON(diff > old_total);
2239 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2240 unlock_chunks(root);
2241 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002242done:
2243 btrfs_free_path(path);
2244 return ret;
2245}
2246
Christoph Hellwigb2950862008-12-02 09:54:17 -05002247static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002248 struct btrfs_root *root,
2249 struct btrfs_key *key,
2250 struct btrfs_chunk *chunk, int item_size)
2251{
2252 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2253 struct btrfs_disk_key disk_key;
2254 u32 array_size;
2255 u8 *ptr;
2256
2257 array_size = btrfs_super_sys_array_size(super_copy);
2258 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2259 return -EFBIG;
2260
2261 ptr = super_copy->sys_chunk_array + array_size;
2262 btrfs_cpu_key_to_disk(&disk_key, key);
2263 memcpy(ptr, &disk_key, sizeof(disk_key));
2264 ptr += sizeof(disk_key);
2265 memcpy(ptr, chunk, item_size);
2266 item_size += sizeof(disk_key);
2267 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2268 return 0;
2269}
2270
Miao Xieb2117a32011-01-05 10:07:28 +00002271/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002272 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002273 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002274static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002275{
Arne Jansen73c5de02011-04-12 12:07:57 +02002276 const struct btrfs_device_info *di_a = a;
2277 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002278
Arne Jansen73c5de02011-04-12 12:07:57 +02002279 if (di_a->max_avail > di_b->max_avail)
2280 return -1;
2281 if (di_a->max_avail < di_b->max_avail)
2282 return 1;
2283 if (di_a->total_avail > di_b->total_avail)
2284 return -1;
2285 if (di_a->total_avail < di_b->total_avail)
2286 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002287 return 0;
2288}
2289
2290static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2291 struct btrfs_root *extent_root,
2292 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002293 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002294 u64 start, u64 type)
2295{
2296 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002297 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2298 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002299 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002300 struct extent_map_tree *em_tree;
2301 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002302 struct btrfs_device_info *devices_info = NULL;
2303 u64 total_avail;
2304 int num_stripes; /* total number of stripes to allocate */
2305 int sub_stripes; /* sub_stripes info for map */
2306 int dev_stripes; /* stripes per dev */
2307 int devs_max; /* max devs to use */
2308 int devs_min; /* min devs needed */
2309 int devs_increment; /* ndevs has to be a multiple of this */
2310 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002311 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002312 u64 max_stripe_size;
2313 u64 max_chunk_size;
2314 u64 stripe_size;
2315 u64 num_bytes;
2316 int ndevs;
2317 int i;
2318 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002319
2320 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2321 (type & BTRFS_BLOCK_GROUP_DUP)) {
2322 WARN_ON(1);
2323 type &= ~BTRFS_BLOCK_GROUP_DUP;
2324 }
Arne Jansen73c5de02011-04-12 12:07:57 +02002325
Miao Xieb2117a32011-01-05 10:07:28 +00002326 if (list_empty(&fs_devices->alloc_list))
2327 return -ENOSPC;
2328
Arne Jansen73c5de02011-04-12 12:07:57 +02002329 sub_stripes = 1;
2330 dev_stripes = 1;
2331 devs_increment = 1;
2332 ncopies = 1;
2333 devs_max = 0; /* 0 == as many as possible */
2334 devs_min = 1;
2335
2336 /*
2337 * define the properties of each RAID type.
2338 * FIXME: move this to a global table and use it in all RAID
2339 * calculation code
2340 */
2341 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2342 dev_stripes = 2;
2343 ncopies = 2;
2344 devs_max = 1;
2345 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2346 devs_min = 2;
2347 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2348 devs_increment = 2;
2349 ncopies = 2;
2350 devs_max = 2;
2351 devs_min = 2;
2352 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2353 sub_stripes = 2;
2354 devs_increment = 2;
2355 ncopies = 2;
2356 devs_min = 4;
2357 } else {
2358 devs_max = 1;
2359 }
2360
2361 if (type & BTRFS_BLOCK_GROUP_DATA) {
2362 max_stripe_size = 1024 * 1024 * 1024;
2363 max_chunk_size = 10 * max_stripe_size;
2364 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2365 max_stripe_size = 256 * 1024 * 1024;
2366 max_chunk_size = max_stripe_size;
2367 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2368 max_stripe_size = 8 * 1024 * 1024;
2369 max_chunk_size = 2 * max_stripe_size;
2370 } else {
2371 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2372 type);
2373 BUG_ON(1);
2374 }
2375
2376 /* we don't want a chunk larger than 10% of writeable space */
2377 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2378 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00002379
2380 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2381 GFP_NOFS);
2382 if (!devices_info)
2383 return -ENOMEM;
2384
Arne Jansen73c5de02011-04-12 12:07:57 +02002385 cur = fs_devices->alloc_list.next;
2386
2387 /*
2388 * in the first pass through the devices list, we gather information
2389 * about the available holes on each device.
2390 */
2391 ndevs = 0;
2392 while (cur != &fs_devices->alloc_list) {
2393 struct btrfs_device *device;
2394 u64 max_avail;
2395 u64 dev_offset;
2396
2397 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2398
2399 cur = cur->next;
2400
2401 if (!device->writeable) {
2402 printk(KERN_ERR
2403 "btrfs: read-only device in alloc_list\n");
2404 WARN_ON(1);
2405 continue;
2406 }
2407
2408 if (!device->in_fs_metadata)
2409 continue;
2410
2411 if (device->total_bytes > device->bytes_used)
2412 total_avail = device->total_bytes - device->bytes_used;
2413 else
2414 total_avail = 0;
2415 /* avail is off by max(alloc_start, 1MB), but that is the same
2416 * for all devices, so it doesn't hurt the sorting later on
2417 */
2418
2419 ret = find_free_dev_extent(trans, device,
2420 max_stripe_size * dev_stripes,
2421 &dev_offset, &max_avail);
2422 if (ret && ret != -ENOSPC)
2423 goto error;
2424
2425 if (ret == 0)
2426 max_avail = max_stripe_size * dev_stripes;
2427
2428 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2429 continue;
2430
2431 devices_info[ndevs].dev_offset = dev_offset;
2432 devices_info[ndevs].max_avail = max_avail;
2433 devices_info[ndevs].total_avail = total_avail;
2434 devices_info[ndevs].dev = device;
2435 ++ndevs;
2436 }
2437
2438 /*
2439 * now sort the devices by hole size / available space
2440 */
2441 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2442 btrfs_cmp_device_info, NULL);
2443
2444 /* round down to number of usable stripes */
2445 ndevs -= ndevs % devs_increment;
2446
2447 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2448 ret = -ENOSPC;
2449 goto error;
2450 }
2451
2452 if (devs_max && ndevs > devs_max)
2453 ndevs = devs_max;
2454 /*
2455 * the primary goal is to maximize the number of stripes, so use as many
2456 * devices as possible, even if the stripes are not maximum sized.
2457 */
2458 stripe_size = devices_info[ndevs-1].max_avail;
2459 num_stripes = ndevs * dev_stripes;
2460
2461 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2462 stripe_size = max_chunk_size * ncopies;
2463 do_div(stripe_size, num_stripes);
2464 }
2465
2466 do_div(stripe_size, dev_stripes);
2467 do_div(stripe_size, BTRFS_STRIPE_LEN);
2468 stripe_size *= BTRFS_STRIPE_LEN;
2469
Miao Xieb2117a32011-01-05 10:07:28 +00002470 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2471 if (!map) {
2472 ret = -ENOMEM;
2473 goto error;
2474 }
2475 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002476
Arne Jansen73c5de02011-04-12 12:07:57 +02002477 for (i = 0; i < ndevs; ++i) {
2478 for (j = 0; j < dev_stripes; ++j) {
2479 int s = i * dev_stripes + j;
2480 map->stripes[s].dev = devices_info[i].dev;
2481 map->stripes[s].physical = devices_info[i].dev_offset +
2482 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002483 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002484 }
Chris Mason593060d2008-03-25 16:50:33 -04002485 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002486 map->stripe_len = BTRFS_STRIPE_LEN;
2487 map->io_align = BTRFS_STRIPE_LEN;
2488 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002489 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002490 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002491
Yan Zheng2b820322008-11-17 21:11:30 -05002492 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02002493 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04002494
Arne Jansen73c5de02011-04-12 12:07:57 +02002495 *stripe_size_out = stripe_size;
2496 *num_bytes_out = num_bytes;
2497
2498 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00002499
Chris Mason0b86a832008-03-24 15:01:56 -04002500 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002501 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002502 ret = -ENOMEM;
2503 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002504 }
Chris Mason0b86a832008-03-24 15:01:56 -04002505 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002506 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02002507 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002508 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002509 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002510
Chris Mason0b86a832008-03-24 15:01:56 -04002511 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002512 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002513 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002514 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002515 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002516 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002517
2518 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2519 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002520 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05002521 BUG_ON(ret);
2522
Arne Jansen73c5de02011-04-12 12:07:57 +02002523 for (i = 0; i < map->num_stripes; ++i) {
2524 struct btrfs_device *device;
2525 u64 dev_offset;
2526
2527 device = map->stripes[i].dev;
2528 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05002529
2530 ret = btrfs_alloc_dev_extent(trans, device,
2531 info->chunk_root->root_key.objectid,
2532 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002533 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05002534 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002535 }
2536
Miao Xieb2117a32011-01-05 10:07:28 +00002537 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002538 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002539
2540error:
2541 kfree(map);
2542 kfree(devices_info);
2543 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002544}
2545
2546static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2547 struct btrfs_root *extent_root,
2548 struct map_lookup *map, u64 chunk_offset,
2549 u64 chunk_size, u64 stripe_size)
2550{
2551 u64 dev_offset;
2552 struct btrfs_key key;
2553 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2554 struct btrfs_device *device;
2555 struct btrfs_chunk *chunk;
2556 struct btrfs_stripe *stripe;
2557 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2558 int index = 0;
2559 int ret;
2560
2561 chunk = kzalloc(item_size, GFP_NOFS);
2562 if (!chunk)
2563 return -ENOMEM;
2564
2565 index = 0;
2566 while (index < map->num_stripes) {
2567 device = map->stripes[index].dev;
2568 device->bytes_used += stripe_size;
2569 ret = btrfs_update_device(trans, device);
2570 BUG_ON(ret);
2571 index++;
2572 }
2573
2574 index = 0;
2575 stripe = &chunk->stripe;
2576 while (index < map->num_stripes) {
2577 device = map->stripes[index].dev;
2578 dev_offset = map->stripes[index].physical;
2579
2580 btrfs_set_stack_stripe_devid(stripe, device->devid);
2581 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2582 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2583 stripe++;
2584 index++;
2585 }
2586
2587 btrfs_set_stack_chunk_length(chunk, chunk_size);
2588 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2589 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2590 btrfs_set_stack_chunk_type(chunk, map->type);
2591 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2592 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2593 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2594 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2595 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2596
2597 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2598 key.type = BTRFS_CHUNK_ITEM_KEY;
2599 key.offset = chunk_offset;
2600
2601 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2602 BUG_ON(ret);
2603
2604 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2605 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2606 item_size);
2607 BUG_ON(ret);
2608 }
liubo1abe9b82011-03-24 11:18:59 +00002609
Yan Zheng2b820322008-11-17 21:11:30 -05002610 kfree(chunk);
2611 return 0;
2612}
2613
2614/*
2615 * Chunk allocation falls into two parts. The first part does works
2616 * that make the new allocated chunk useable, but not do any operation
2617 * that modifies the chunk tree. The second part does the works that
2618 * require modifying the chunk tree. This division is important for the
2619 * bootstrap process of adding storage to a seed btrfs.
2620 */
2621int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2622 struct btrfs_root *extent_root, u64 type)
2623{
2624 u64 chunk_offset;
2625 u64 chunk_size;
2626 u64 stripe_size;
2627 struct map_lookup *map;
2628 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2629 int ret;
2630
2631 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2632 &chunk_offset);
2633 if (ret)
2634 return ret;
2635
2636 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2637 &stripe_size, chunk_offset, type);
2638 if (ret)
2639 return ret;
2640
2641 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2642 chunk_size, stripe_size);
2643 BUG_ON(ret);
2644 return 0;
2645}
2646
Chris Masond3977122009-01-05 21:25:51 -05002647static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002648 struct btrfs_root *root,
2649 struct btrfs_device *device)
2650{
2651 u64 chunk_offset;
2652 u64 sys_chunk_offset;
2653 u64 chunk_size;
2654 u64 sys_chunk_size;
2655 u64 stripe_size;
2656 u64 sys_stripe_size;
2657 u64 alloc_profile;
2658 struct map_lookup *map;
2659 struct map_lookup *sys_map;
2660 struct btrfs_fs_info *fs_info = root->fs_info;
2661 struct btrfs_root *extent_root = fs_info->extent_root;
2662 int ret;
2663
2664 ret = find_next_chunk(fs_info->chunk_root,
2665 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2666 BUG_ON(ret);
2667
2668 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2669 (fs_info->metadata_alloc_profile &
2670 fs_info->avail_metadata_alloc_bits);
2671 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2672
2673 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2674 &stripe_size, chunk_offset, alloc_profile);
2675 BUG_ON(ret);
2676
2677 sys_chunk_offset = chunk_offset + chunk_size;
2678
2679 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2680 (fs_info->system_alloc_profile &
2681 fs_info->avail_system_alloc_bits);
2682 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2683
2684 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2685 &sys_chunk_size, &sys_stripe_size,
2686 sys_chunk_offset, alloc_profile);
2687 BUG_ON(ret);
2688
2689 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2690 BUG_ON(ret);
2691
2692 /*
2693 * Modifying chunk tree needs allocating new blocks from both
2694 * system block group and metadata block group. So we only can
2695 * do operations require modifying the chunk tree after both
2696 * block groups were created.
2697 */
2698 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2699 chunk_size, stripe_size);
2700 BUG_ON(ret);
2701
2702 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2703 sys_chunk_offset, sys_chunk_size,
2704 sys_stripe_size);
2705 BUG_ON(ret);
2706 return 0;
2707}
2708
2709int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2710{
2711 struct extent_map *em;
2712 struct map_lookup *map;
2713 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2714 int readonly = 0;
2715 int i;
2716
Chris Mason890871b2009-09-02 16:24:52 -04002717 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002718 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002719 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002720 if (!em)
2721 return 1;
2722
Josef Bacikf48b9072010-01-27 02:07:59 +00002723 if (btrfs_test_opt(root, DEGRADED)) {
2724 free_extent_map(em);
2725 return 0;
2726 }
2727
Yan Zheng2b820322008-11-17 21:11:30 -05002728 map = (struct map_lookup *)em->bdev;
2729 for (i = 0; i < map->num_stripes; i++) {
2730 if (!map->stripes[i].dev->writeable) {
2731 readonly = 1;
2732 break;
2733 }
2734 }
2735 free_extent_map(em);
2736 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002737}
2738
2739void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2740{
2741 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2742}
2743
2744void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2745{
2746 struct extent_map *em;
2747
Chris Masond3977122009-01-05 21:25:51 -05002748 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002749 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002750 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2751 if (em)
2752 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002753 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002754 if (!em)
2755 break;
2756 kfree(em->bdev);
2757 /* once for us */
2758 free_extent_map(em);
2759 /* once for the tree */
2760 free_extent_map(em);
2761 }
2762}
2763
Chris Masonf1885912008-04-09 16:28:12 -04002764int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2765{
2766 struct extent_map *em;
2767 struct map_lookup *map;
2768 struct extent_map_tree *em_tree = &map_tree->map_tree;
2769 int ret;
2770
Chris Mason890871b2009-09-02 16:24:52 -04002771 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002772 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002773 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002774 BUG_ON(!em);
2775
2776 BUG_ON(em->start > logical || em->start + em->len < logical);
2777 map = (struct map_lookup *)em->bdev;
2778 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2779 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002780 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2781 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002782 else
2783 ret = 1;
2784 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002785 return ret;
2786}
2787
Chris Masondfe25022008-05-13 13:46:40 -04002788static int find_live_mirror(struct map_lookup *map, int first, int num,
2789 int optimal)
2790{
2791 int i;
2792 if (map->stripes[optimal].dev->bdev)
2793 return optimal;
2794 for (i = first; i < first + num; i++) {
2795 if (map->stripes[i].dev->bdev)
2796 return i;
2797 }
2798 /* we couldn't find one that doesn't fail. Just return something
2799 * and the io error handling code will clean up eventually
2800 */
2801 return optimal;
2802}
2803
Chris Masonf2d8d742008-04-21 10:03:05 -04002804static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2805 u64 logical, u64 *length,
2806 struct btrfs_multi_bio **multi_ret,
2807 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002808{
2809 struct extent_map *em;
2810 struct map_lookup *map;
2811 struct extent_map_tree *em_tree = &map_tree->map_tree;
2812 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002813 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002814 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002815 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002816 u64 stripe_nr_orig;
2817 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002818 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002819 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002820 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002821 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002822 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002823 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002824 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002825
Li Dongyangfce3bb92011-03-24 10:24:26 +00002826 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002827 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002828again:
2829 if (multi_ret) {
2830 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2831 GFP_NOFS);
2832 if (!multi)
2833 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002834
2835 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002836 }
Chris Mason0b86a832008-03-24 15:01:56 -04002837
Chris Mason890871b2009-09-02 16:24:52 -04002838 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002839 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002840 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002841
Jiri Slaby2423fdfb2010-01-06 16:57:22 +00002842 if (!em && unplug_page) {
2843 kfree(multi);
Chris Masonf2d8d742008-04-21 10:03:05 -04002844 return 0;
Jiri Slaby2423fdfb2010-01-06 16:57:22 +00002845 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002846
Chris Mason3b951512008-04-17 11:29:12 -04002847 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002848 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2849 (unsigned long long)logical,
2850 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002851 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002852 }
Chris Mason0b86a832008-03-24 15:01:56 -04002853
2854 BUG_ON(em->start > logical || em->start + em->len < logical);
2855 map = (struct map_lookup *)em->bdev;
2856 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002857
Chris Masonf1885912008-04-09 16:28:12 -04002858 if (mirror_num > map->num_stripes)
2859 mirror_num = 0;
2860
Chris Masoncea9e442008-04-09 16:28:12 -04002861 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002862 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002863 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2864 BTRFS_BLOCK_GROUP_DUP)) {
2865 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002866 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002867 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2868 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002869 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002870 }
2871 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002872 if (rw & REQ_DISCARD) {
2873 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2874 BTRFS_BLOCK_GROUP_RAID1 |
2875 BTRFS_BLOCK_GROUP_DUP |
2876 BTRFS_BLOCK_GROUP_RAID10)) {
2877 stripes_required = map->num_stripes;
2878 }
2879 }
2880 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002881 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002882 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002883 free_extent_map(em);
2884 kfree(multi);
2885 goto again;
2886 }
Chris Mason593060d2008-03-25 16:50:33 -04002887 stripe_nr = offset;
2888 /*
2889 * stripe_nr counts the total number of stripes we have to stride
2890 * to get to this block
2891 */
2892 do_div(stripe_nr, map->stripe_len);
2893
2894 stripe_offset = stripe_nr * map->stripe_len;
2895 BUG_ON(offset < stripe_offset);
2896
2897 /* stripe_offset is the offset of this block in its stripe*/
2898 stripe_offset = offset - stripe_offset;
2899
Li Dongyangfce3bb92011-03-24 10:24:26 +00002900 if (rw & REQ_DISCARD)
2901 *length = min_t(u64, em->len - offset, *length);
2902 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2903 BTRFS_BLOCK_GROUP_RAID1 |
2904 BTRFS_BLOCK_GROUP_RAID10 |
2905 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04002906 /* we limit the length of each bio to what fits in a stripe */
2907 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00002908 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04002909 } else {
2910 *length = em->len - offset;
2911 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002912
2913 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04002914 goto out;
2915
Chris Masonf2d8d742008-04-21 10:03:05 -04002916 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002917 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002918 stripe_nr_orig = stripe_nr;
2919 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2920 (~(map->stripe_len - 1));
2921 do_div(stripe_nr_end, map->stripe_len);
2922 stripe_end_offset = stripe_nr_end * map->stripe_len -
2923 (offset + *length);
2924 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2925 if (rw & REQ_DISCARD)
2926 num_stripes = min_t(u64, map->num_stripes,
2927 stripe_nr_end - stripe_nr_orig);
2928 stripe_index = do_div(stripe_nr, map->num_stripes);
2929 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2930 if (unplug_page || (rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masonf2d8d742008-04-21 10:03:05 -04002931 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002932 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002933 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002934 else {
2935 stripe_index = find_live_mirror(map, 0,
2936 map->num_stripes,
2937 current->pid % map->num_stripes);
2938 }
Chris Mason2fff7342008-04-29 14:12:09 -04002939
Chris Mason611f0e02008-04-03 16:29:03 -04002940 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00002941 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002942 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002943 else if (mirror_num)
2944 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002945
Chris Mason321aecc2008-04-16 10:49:51 -04002946 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2947 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002948
2949 stripe_index = do_div(stripe_nr, factor);
2950 stripe_index *= map->sub_stripes;
2951
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002952 if (unplug_page || (rw & REQ_WRITE))
Chris Masonf2d8d742008-04-21 10:03:05 -04002953 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002954 else if (rw & REQ_DISCARD)
2955 num_stripes = min_t(u64, map->sub_stripes *
2956 (stripe_nr_end - stripe_nr_orig),
2957 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04002958 else if (mirror_num)
2959 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002960 else {
2961 stripe_index = find_live_mirror(map, stripe_index,
2962 map->sub_stripes, stripe_index +
2963 current->pid % map->sub_stripes);
2964 }
Chris Mason8790d502008-04-03 16:29:03 -04002965 } else {
2966 /*
2967 * after this do_div call, stripe_nr is the number of stripes
2968 * on this device we have to walk to find the data, and
2969 * stripe_index is the number of our device in the stripe array
2970 */
2971 stripe_index = do_div(stripe_nr, map->num_stripes);
2972 }
Chris Mason593060d2008-03-25 16:50:33 -04002973 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04002974
Li Dongyangfce3bb92011-03-24 10:24:26 +00002975 if (rw & REQ_DISCARD) {
2976 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04002977 multi->stripes[i].physical =
2978 map->stripes[stripe_index].physical +
2979 stripe_offset + stripe_nr * map->stripe_len;
2980 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002981
2982 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2983 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04002984 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002985 int j;
2986
Chris Masond9d04872011-03-27 21:23:21 -04002987 div_u64_rem(stripe_nr_end - 1,
2988 map->num_stripes,
2989 &last_stripe);
2990
Li Dongyangfce3bb92011-03-24 10:24:26 +00002991 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04002992 u32 test;
2993
2994 div_u64_rem(stripe_nr_end - 1 - j,
2995 map->num_stripes, &test);
2996 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00002997 break;
2998 }
2999 stripes = stripe_nr_end - 1 - j;
3000 do_div(stripes, map->num_stripes);
3001 multi->stripes[i].length = map->stripe_len *
3002 (stripes - stripe_nr + 1);
3003
3004 if (i == 0) {
3005 multi->stripes[i].length -=
3006 stripe_offset;
3007 stripe_offset = 0;
3008 }
3009 if (stripe_index == last_stripe)
3010 multi->stripes[i].length -=
3011 stripe_end_offset;
3012 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3013 u64 stripes;
3014 int j;
3015 int factor = map->num_stripes /
3016 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003017 u32 last_stripe = 0;
3018
3019 div_u64_rem(stripe_nr_end - 1,
3020 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003021 last_stripe *= map->sub_stripes;
3022
3023 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003024 u32 test;
3025
3026 div_u64_rem(stripe_nr_end - 1 - j,
3027 factor, &test);
3028
3029 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003030 stripe_index / map->sub_stripes)
3031 break;
3032 }
3033 stripes = stripe_nr_end - 1 - j;
3034 do_div(stripes, factor);
3035 multi->stripes[i].length = map->stripe_len *
3036 (stripes - stripe_nr + 1);
3037
3038 if (i < map->sub_stripes) {
3039 multi->stripes[i].length -=
3040 stripe_offset;
3041 if (i == map->sub_stripes - 1)
3042 stripe_offset = 0;
3043 }
3044 if (stripe_index >= last_stripe &&
3045 stripe_index <= (last_stripe +
3046 map->sub_stripes - 1)) {
3047 multi->stripes[i].length -=
3048 stripe_end_offset;
3049 }
3050 } else
3051 multi->stripes[i].length = *length;
3052
3053 stripe_index++;
3054 if (stripe_index == map->num_stripes) {
3055 /* This could only happen for RAID0/10 */
3056 stripe_index = 0;
3057 stripe_nr++;
3058 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003059 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003060 } else {
3061 for (i = 0; i < num_stripes; i++) {
3062 if (unplug_page) {
3063 struct btrfs_device *device;
3064 struct backing_dev_info *bdi;
3065
3066 device = map->stripes[stripe_index].dev;
3067 if (device->bdev) {
3068 bdi = blk_get_backing_dev_info(device->
3069 bdev);
3070 if (bdi->unplug_io_fn)
3071 bdi->unplug_io_fn(bdi,
3072 unplug_page);
3073 }
3074 } else {
3075 multi->stripes[i].physical =
3076 map->stripes[stripe_index].physical +
3077 stripe_offset +
3078 stripe_nr * map->stripe_len;
3079 multi->stripes[i].dev =
3080 map->stripes[stripe_index].dev;
3081 }
3082 stripe_index++;
3083 }
Chris Mason593060d2008-03-25 16:50:33 -04003084 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003085 if (multi_ret) {
3086 *multi_ret = multi;
3087 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003088 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003089 }
Chris Masoncea9e442008-04-09 16:28:12 -04003090out:
Chris Mason0b86a832008-03-24 15:01:56 -04003091 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003092 return 0;
3093}
3094
Chris Masonf2d8d742008-04-21 10:03:05 -04003095int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3096 u64 logical, u64 *length,
3097 struct btrfs_multi_bio **multi_ret, int mirror_num)
3098{
3099 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3100 mirror_num, NULL);
3101}
3102
Yan Zhenga512bbf2008-12-08 16:46:26 -05003103int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3104 u64 chunk_start, u64 physical, u64 devid,
3105 u64 **logical, int *naddrs, int *stripe_len)
3106{
3107 struct extent_map_tree *em_tree = &map_tree->map_tree;
3108 struct extent_map *em;
3109 struct map_lookup *map;
3110 u64 *buf;
3111 u64 bytenr;
3112 u64 length;
3113 u64 stripe_nr;
3114 int i, j, nr = 0;
3115
Chris Mason890871b2009-09-02 16:24:52 -04003116 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003117 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003118 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003119
3120 BUG_ON(!em || em->start != chunk_start);
3121 map = (struct map_lookup *)em->bdev;
3122
3123 length = em->len;
3124 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3125 do_div(length, map->num_stripes / map->sub_stripes);
3126 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3127 do_div(length, map->num_stripes);
3128
3129 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3130 BUG_ON(!buf);
3131
3132 for (i = 0; i < map->num_stripes; i++) {
3133 if (devid && map->stripes[i].dev->devid != devid)
3134 continue;
3135 if (map->stripes[i].physical > physical ||
3136 map->stripes[i].physical + length <= physical)
3137 continue;
3138
3139 stripe_nr = physical - map->stripes[i].physical;
3140 do_div(stripe_nr, map->stripe_len);
3141
3142 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3143 stripe_nr = stripe_nr * map->num_stripes + i;
3144 do_div(stripe_nr, map->sub_stripes);
3145 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3146 stripe_nr = stripe_nr * map->num_stripes + i;
3147 }
3148 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003149 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003150 for (j = 0; j < nr; j++) {
3151 if (buf[j] == bytenr)
3152 break;
3153 }
Chris Mason934d3752008-12-08 16:43:10 -05003154 if (j == nr) {
3155 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003156 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003157 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003158 }
3159
Yan Zhenga512bbf2008-12-08 16:46:26 -05003160 *logical = buf;
3161 *naddrs = nr;
3162 *stripe_len = map->stripe_len;
3163
3164 free_extent_map(em);
3165 return 0;
3166}
3167
Chris Masonf2d8d742008-04-21 10:03:05 -04003168int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
3169 u64 logical, struct page *page)
3170{
3171 u64 length = PAGE_CACHE_SIZE;
3172 return __btrfs_map_block(map_tree, READ, logical, &length,
3173 NULL, 0, page);
3174}
3175
Chris Mason8790d502008-04-03 16:29:03 -04003176static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003177{
Chris Masoncea9e442008-04-09 16:28:12 -04003178 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003179 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003180
Chris Mason8790d502008-04-03 16:29:03 -04003181 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003182 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003183
Chris Mason7d2b4da2008-08-05 10:13:57 -04003184 if (bio == multi->orig_bio)
3185 is_orig_bio = 1;
3186
Chris Masoncea9e442008-04-09 16:28:12 -04003187 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003188 if (!is_orig_bio) {
3189 bio_put(bio);
3190 bio = multi->orig_bio;
3191 }
Chris Mason8790d502008-04-03 16:29:03 -04003192 bio->bi_private = multi->private;
3193 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003194 /* only send an error to the higher layers if it is
3195 * beyond the tolerance of the multi-bio
3196 */
Chris Mason1259ab72008-05-12 13:39:03 -04003197 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003198 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003199 } else if (err) {
3200 /*
3201 * this bio is actually up to date, we didn't
3202 * go over the max number of errors
3203 */
3204 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003205 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003206 }
Chris Mason8790d502008-04-03 16:29:03 -04003207 kfree(multi);
3208
3209 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003210 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003211 bio_put(bio);
3212 }
Chris Mason8790d502008-04-03 16:29:03 -04003213}
3214
Chris Mason8b712842008-06-11 16:50:36 -04003215struct async_sched {
3216 struct bio *bio;
3217 int rw;
3218 struct btrfs_fs_info *info;
3219 struct btrfs_work work;
3220};
3221
3222/*
3223 * see run_scheduled_bios for a description of why bios are collected for
3224 * async submit.
3225 *
3226 * This will add one bio to the pending list for a device and make sure
3227 * the work struct is scheduled.
3228 */
Chris Masond3977122009-01-05 21:25:51 -05003229static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003230 struct btrfs_device *device,
3231 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003232{
3233 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003234 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003235
3236 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003237 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003238 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003239 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003240 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003241 return 0;
3242 }
3243
3244 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003245 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003246 * higher layers. Otherwise, the async bio makes it appear we have
3247 * made progress against dirty pages when we've really just put it
3248 * on a queue for later
3249 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003250 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003251 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003252 bio->bi_next = NULL;
3253 bio->bi_rw |= rw;
3254
3255 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003256 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003257 pending_bios = &device->pending_sync_bios;
3258 else
3259 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003260
Chris Masonffbd5172009-04-20 15:50:09 -04003261 if (pending_bios->tail)
3262 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003263
Chris Masonffbd5172009-04-20 15:50:09 -04003264 pending_bios->tail = bio;
3265 if (!pending_bios->head)
3266 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003267 if (device->running_pending)
3268 should_queue = 0;
3269
3270 spin_unlock(&device->io_lock);
3271
3272 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003273 btrfs_queue_worker(&root->fs_info->submit_workers,
3274 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003275 return 0;
3276}
3277
Chris Masonf1885912008-04-09 16:28:12 -04003278int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003279 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003280{
3281 struct btrfs_mapping_tree *map_tree;
3282 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003283 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003284 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003285 u64 length = 0;
3286 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003287 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003288 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003289 int dev_nr = 0;
3290 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003291
Chris Masonf2d8d742008-04-21 10:03:05 -04003292 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003293 map_tree = &root->fs_info->mapping_tree;
3294 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003295
Chris Masonf1885912008-04-09 16:28:12 -04003296 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3297 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003298 BUG_ON(ret);
3299
3300 total_devs = multi->num_stripes;
3301 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003302 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3303 "len %llu\n", (unsigned long long)logical,
3304 (unsigned long long)length,
3305 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003306 BUG();
3307 }
3308 multi->end_io = first_bio->bi_end_io;
3309 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003310 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003311 atomic_set(&multi->stripes_pending, multi->num_stripes);
3312
Chris Masond3977122009-01-05 21:25:51 -05003313 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003314 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003315 if (dev_nr < total_devs - 1) {
3316 bio = bio_clone(first_bio, GFP_NOFS);
3317 BUG_ON(!bio);
3318 } else {
3319 bio = first_bio;
3320 }
3321 bio->bi_private = multi;
3322 bio->bi_end_io = end_bio_multi_stripe;
3323 }
Chris Masoncea9e442008-04-09 16:28:12 -04003324 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3325 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003326 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003327 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003328 if (async_submit)
3329 schedule_bio(root, dev, rw, bio);
3330 else
3331 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003332 } else {
3333 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3334 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003335 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003336 }
Chris Mason8790d502008-04-03 16:29:03 -04003337 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003338 }
Chris Masoncea9e442008-04-09 16:28:12 -04003339 if (total_devs == 1)
3340 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003341 return 0;
3342}
3343
Chris Masona4437552008-04-18 10:29:38 -04003344struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003345 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003346{
Yan Zheng2b820322008-11-17 21:11:30 -05003347 struct btrfs_device *device;
3348 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003349
Yan Zheng2b820322008-11-17 21:11:30 -05003350 cur_devices = root->fs_info->fs_devices;
3351 while (cur_devices) {
3352 if (!fsid ||
3353 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3354 device = __find_device(&cur_devices->devices,
3355 devid, uuid);
3356 if (device)
3357 return device;
3358 }
3359 cur_devices = cur_devices->seed;
3360 }
3361 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003362}
3363
Chris Masondfe25022008-05-13 13:46:40 -04003364static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3365 u64 devid, u8 *dev_uuid)
3366{
3367 struct btrfs_device *device;
3368 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3369
3370 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003371 if (!device)
3372 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003373 list_add(&device->dev_list,
3374 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003375 device->dev_root = root->fs_info->dev_root;
3376 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003377 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003378 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003379 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003380 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003381 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003382 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003383 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003384 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3385 return device;
3386}
3387
Chris Mason0b86a832008-03-24 15:01:56 -04003388static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3389 struct extent_buffer *leaf,
3390 struct btrfs_chunk *chunk)
3391{
3392 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3393 struct map_lookup *map;
3394 struct extent_map *em;
3395 u64 logical;
3396 u64 length;
3397 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003398 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003399 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003400 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003401 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003402
Chris Masone17cade2008-04-15 15:41:47 -04003403 logical = key->offset;
3404 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003405
Chris Mason890871b2009-09-02 16:24:52 -04003406 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003407 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003408 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003409
3410 /* already mapped? */
3411 if (em && em->start <= logical && em->start + em->len > logical) {
3412 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003413 return 0;
3414 } else if (em) {
3415 free_extent_map(em);
3416 }
Chris Mason0b86a832008-03-24 15:01:56 -04003417
Chris Mason0b86a832008-03-24 15:01:56 -04003418 em = alloc_extent_map(GFP_NOFS);
3419 if (!em)
3420 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003421 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3422 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003423 if (!map) {
3424 free_extent_map(em);
3425 return -ENOMEM;
3426 }
3427
3428 em->bdev = (struct block_device *)map;
3429 em->start = logical;
3430 em->len = length;
3431 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003432 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003433
Chris Mason593060d2008-03-25 16:50:33 -04003434 map->num_stripes = num_stripes;
3435 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3436 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3437 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3438 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3439 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003440 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003441 for (i = 0; i < num_stripes; i++) {
3442 map->stripes[i].physical =
3443 btrfs_stripe_offset_nr(leaf, chunk, i);
3444 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003445 read_extent_buffer(leaf, uuid, (unsigned long)
3446 btrfs_stripe_dev_uuid_nr(chunk, i),
3447 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003448 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3449 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003450 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003451 kfree(map);
3452 free_extent_map(em);
3453 return -EIO;
3454 }
Chris Masondfe25022008-05-13 13:46:40 -04003455 if (!map->stripes[i].dev) {
3456 map->stripes[i].dev =
3457 add_missing_dev(root, devid, uuid);
3458 if (!map->stripes[i].dev) {
3459 kfree(map);
3460 free_extent_map(em);
3461 return -EIO;
3462 }
3463 }
3464 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003465 }
3466
Chris Mason890871b2009-09-02 16:24:52 -04003467 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003468 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003469 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003470 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003471 free_extent_map(em);
3472
3473 return 0;
3474}
3475
3476static int fill_device_from_item(struct extent_buffer *leaf,
3477 struct btrfs_dev_item *dev_item,
3478 struct btrfs_device *device)
3479{
3480 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003481
3482 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003483 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3484 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003485 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3486 device->type = btrfs_device_type(leaf, dev_item);
3487 device->io_align = btrfs_device_io_align(leaf, dev_item);
3488 device->io_width = btrfs_device_io_width(leaf, dev_item);
3489 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003490
3491 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003492 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003493
Chris Mason0b86a832008-03-24 15:01:56 -04003494 return 0;
3495}
3496
Yan Zheng2b820322008-11-17 21:11:30 -05003497static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3498{
3499 struct btrfs_fs_devices *fs_devices;
3500 int ret;
3501
3502 mutex_lock(&uuid_mutex);
3503
3504 fs_devices = root->fs_info->fs_devices->seed;
3505 while (fs_devices) {
3506 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3507 ret = 0;
3508 goto out;
3509 }
3510 fs_devices = fs_devices->seed;
3511 }
3512
3513 fs_devices = find_fsid(fsid);
3514 if (!fs_devices) {
3515 ret = -ENOENT;
3516 goto out;
3517 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003518
3519 fs_devices = clone_fs_devices(fs_devices);
3520 if (IS_ERR(fs_devices)) {
3521 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003522 goto out;
3523 }
3524
Christoph Hellwig97288f22008-12-02 06:36:09 -05003525 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003526 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003527 if (ret)
3528 goto out;
3529
3530 if (!fs_devices->seeding) {
3531 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003532 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003533 ret = -EINVAL;
3534 goto out;
3535 }
3536
3537 fs_devices->seed = root->fs_info->fs_devices->seed;
3538 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003539out:
3540 mutex_unlock(&uuid_mutex);
3541 return ret;
3542}
3543
Chris Mason0d81ba52008-03-24 15:02:07 -04003544static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003545 struct extent_buffer *leaf,
3546 struct btrfs_dev_item *dev_item)
3547{
3548 struct btrfs_device *device;
3549 u64 devid;
3550 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003551 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003552 u8 dev_uuid[BTRFS_UUID_SIZE];
3553
Chris Mason0b86a832008-03-24 15:01:56 -04003554 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003555 read_extent_buffer(leaf, dev_uuid,
3556 (unsigned long)btrfs_device_uuid(dev_item),
3557 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003558 read_extent_buffer(leaf, fs_uuid,
3559 (unsigned long)btrfs_device_fsid(dev_item),
3560 BTRFS_UUID_SIZE);
3561
3562 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3563 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003564 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003565 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003566 }
3567
3568 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3569 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003570 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003571 return -EIO;
3572
3573 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003574 printk(KERN_WARNING "warning devid %llu missing\n",
3575 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003576 device = add_missing_dev(root, devid, dev_uuid);
3577 if (!device)
3578 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003579 } else if (!device->missing) {
3580 /*
3581 * this happens when a device that was properly setup
3582 * in the device info lists suddenly goes bad.
3583 * device->bdev is NULL, and so we have to set
3584 * device->missing to one here
3585 */
3586 root->fs_info->fs_devices->missing_devices++;
3587 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003588 }
3589 }
3590
3591 if (device->fs_devices != root->fs_info->fs_devices) {
3592 BUG_ON(device->writeable);
3593 if (device->generation !=
3594 btrfs_device_generation(leaf, dev_item))
3595 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003596 }
Chris Mason0b86a832008-03-24 15:01:56 -04003597
3598 fill_device_from_item(leaf, dev_item, device);
3599 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003600 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003601 if (device->writeable)
3602 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003603 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003604 return ret;
3605}
3606
Chris Mason0d81ba52008-03-24 15:02:07 -04003607int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3608{
3609 struct btrfs_dev_item *dev_item;
3610
3611 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3612 dev_item);
3613 return read_one_dev(root, buf, dev_item);
3614}
3615
Yan Zhenge4404d62008-12-12 10:03:26 -05003616int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003617{
3618 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003619 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003620 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003621 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003622 u8 *ptr;
3623 unsigned long sb_ptr;
3624 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003625 u32 num_stripes;
3626 u32 array_size;
3627 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003628 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003629 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003630
Yan Zhenge4404d62008-12-12 10:03:26 -05003631 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003632 BTRFS_SUPER_INFO_SIZE);
3633 if (!sb)
3634 return -ENOMEM;
3635 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003636 btrfs_set_buffer_lockdep_class(sb, 0);
3637
Chris Masona061fc82008-05-07 11:43:44 -04003638 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003639 array_size = btrfs_super_sys_array_size(super_copy);
3640
Chris Mason0b86a832008-03-24 15:01:56 -04003641 ptr = super_copy->sys_chunk_array;
3642 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3643 cur = 0;
3644
3645 while (cur < array_size) {
3646 disk_key = (struct btrfs_disk_key *)ptr;
3647 btrfs_disk_key_to_cpu(&key, disk_key);
3648
Chris Masona061fc82008-05-07 11:43:44 -04003649 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003650 sb_ptr += len;
3651 cur += len;
3652
Chris Mason0d81ba52008-03-24 15:02:07 -04003653 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003654 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003655 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003656 if (ret)
3657 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003658 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3659 len = btrfs_chunk_item_size(num_stripes);
3660 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003661 ret = -EIO;
3662 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003663 }
3664 ptr += len;
3665 sb_ptr += len;
3666 cur += len;
3667 }
Chris Masona061fc82008-05-07 11:43:44 -04003668 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003669 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003670}
3671
3672int btrfs_read_chunk_tree(struct btrfs_root *root)
3673{
3674 struct btrfs_path *path;
3675 struct extent_buffer *leaf;
3676 struct btrfs_key key;
3677 struct btrfs_key found_key;
3678 int ret;
3679 int slot;
3680
3681 root = root->fs_info->chunk_root;
3682
3683 path = btrfs_alloc_path();
3684 if (!path)
3685 return -ENOMEM;
3686
3687 /* first we search for all of the device items, and then we
3688 * read in all of the chunk items. This way we can create chunk
3689 * mappings that reference all of the devices that are afound
3690 */
3691 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3692 key.offset = 0;
3693 key.type = 0;
3694again:
3695 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003696 if (ret < 0)
3697 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003698 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003699 leaf = path->nodes[0];
3700 slot = path->slots[0];
3701 if (slot >= btrfs_header_nritems(leaf)) {
3702 ret = btrfs_next_leaf(root, path);
3703 if (ret == 0)
3704 continue;
3705 if (ret < 0)
3706 goto error;
3707 break;
3708 }
3709 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3710 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3711 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3712 break;
3713 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3714 struct btrfs_dev_item *dev_item;
3715 dev_item = btrfs_item_ptr(leaf, slot,
3716 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003717 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003718 if (ret)
3719 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003720 }
3721 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3722 struct btrfs_chunk *chunk;
3723 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3724 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003725 if (ret)
3726 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003727 }
3728 path->slots[0]++;
3729 }
3730 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3731 key.objectid = 0;
3732 btrfs_release_path(root, path);
3733 goto again;
3734 }
Chris Mason0b86a832008-03-24 15:01:56 -04003735 ret = 0;
3736error:
Yan Zheng2b820322008-11-17 21:11:30 -05003737 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003738 return ret;
3739}