blob: a9f1fc23278bc7fc35d5553e735851ba587f6ece [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 */
Miao Xie7bfc8372011-01-05 10:07:26 +0000852 search_start = 1024 * 1024;
Chris Mason0b86a832008-03-24 15:01:56 -0400853
Miao Xie7bfc8372011-01-05 10:07:26 +0000854 if (root->fs_info->alloc_start + num_bytes <= search_end)
Chris Mason0b86a832008-03-24 15:01:56 -0400855 search_start = max(root->fs_info->alloc_start, search_start);
856
Miao Xie7bfc8372011-01-05 10:07:26 +0000857 max_hole_start = search_start;
858 max_hole_size = 0;
859
860 if (search_start >= search_end) {
861 ret = -ENOSPC;
862 goto error;
863 }
864
865 path = btrfs_alloc_path();
866 if (!path) {
867 ret = -ENOMEM;
868 goto error;
869 }
870 path->reada = 2;
871
Chris Mason0b86a832008-03-24 15:01:56 -0400872 key.objectid = device->devid;
873 key.offset = search_start;
874 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000875
Chris Mason0b86a832008-03-24 15:01:56 -0400876 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
877 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000878 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400879 if (ret > 0) {
880 ret = btrfs_previous_item(root, path, key.objectid, key.type);
881 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000882 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400883 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000884
Chris Mason0b86a832008-03-24 15:01:56 -0400885 while (1) {
886 l = path->nodes[0];
887 slot = path->slots[0];
888 if (slot >= btrfs_header_nritems(l)) {
889 ret = btrfs_next_leaf(root, path);
890 if (ret == 0)
891 continue;
892 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000893 goto out;
894
895 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400896 }
897 btrfs_item_key_to_cpu(l, &key, slot);
898
899 if (key.objectid < device->devid)
900 goto next;
901
902 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000903 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400904
Chris Mason0b86a832008-03-24 15:01:56 -0400905 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
906 goto next;
907
Miao Xie7bfc8372011-01-05 10:07:26 +0000908 if (key.offset > search_start) {
909 hole_size = key.offset - search_start;
910
911 if (hole_size > max_hole_size) {
912 max_hole_start = search_start;
913 max_hole_size = hole_size;
914 }
915
916 /*
917 * If this free space is greater than which we need,
918 * it must be the max free space that we have found
919 * until now, so max_hole_start must point to the start
920 * of this free space and the length of this free space
921 * is stored in max_hole_size. Thus, we return
922 * max_hole_start and max_hole_size and go back to the
923 * caller.
924 */
925 if (hole_size >= num_bytes) {
926 ret = 0;
927 goto out;
928 }
929 }
930
Chris Mason0b86a832008-03-24 15:01:56 -0400931 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000932 extent_end = key.offset + btrfs_dev_extent_length(l,
933 dev_extent);
934 if (extent_end > search_start)
935 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400936next:
937 path->slots[0]++;
938 cond_resched();
939 }
Chris Mason0b86a832008-03-24 15:01:56 -0400940
Miao Xie7bfc8372011-01-05 10:07:26 +0000941 hole_size = search_end- search_start;
942 if (hole_size > max_hole_size) {
943 max_hole_start = search_start;
944 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400945 }
Chris Mason0b86a832008-03-24 15:01:56 -0400946
Miao Xie7bfc8372011-01-05 10:07:26 +0000947 /* See above. */
948 if (hole_size < num_bytes)
949 ret = -ENOSPC;
950 else
951 ret = 0;
952
953out:
Yan Zheng2b820322008-11-17 21:11:30 -0500954 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000955error:
956 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000957 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000958 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400959 return ret;
960}
961
Christoph Hellwigb2950862008-12-02 09:54:17 -0500962static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400963 struct btrfs_device *device,
964 u64 start)
965{
966 int ret;
967 struct btrfs_path *path;
968 struct btrfs_root *root = device->dev_root;
969 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400970 struct btrfs_key found_key;
971 struct extent_buffer *leaf = NULL;
972 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400973
974 path = btrfs_alloc_path();
975 if (!path)
976 return -ENOMEM;
977
978 key.objectid = device->devid;
979 key.offset = start;
980 key.type = BTRFS_DEV_EXTENT_KEY;
981
982 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -0400983 if (ret > 0) {
984 ret = btrfs_previous_item(root, path, key.objectid,
985 BTRFS_DEV_EXTENT_KEY);
986 BUG_ON(ret);
987 leaf = path->nodes[0];
988 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
989 extent = btrfs_item_ptr(leaf, path->slots[0],
990 struct btrfs_dev_extent);
991 BUG_ON(found_key.offset > start || found_key.offset +
992 btrfs_dev_extent_length(leaf, extent) < start);
993 ret = 0;
994 } else if (ret == 0) {
995 leaf = path->nodes[0];
996 extent = btrfs_item_ptr(leaf, path->slots[0],
997 struct btrfs_dev_extent);
998 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400999 BUG_ON(ret);
1000
Chris Masondfe25022008-05-13 13:46:40 -04001001 if (device->bytes_used > 0)
1002 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -04001003 ret = btrfs_del_item(trans, root, path);
1004 BUG_ON(ret);
1005
1006 btrfs_free_path(path);
1007 return ret;
1008}
1009
Yan Zheng2b820322008-11-17 21:11:30 -05001010int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001011 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001012 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001013 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001014{
1015 int ret;
1016 struct btrfs_path *path;
1017 struct btrfs_root *root = device->dev_root;
1018 struct btrfs_dev_extent *extent;
1019 struct extent_buffer *leaf;
1020 struct btrfs_key key;
1021
Chris Masondfe25022008-05-13 13:46:40 -04001022 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001023 path = btrfs_alloc_path();
1024 if (!path)
1025 return -ENOMEM;
1026
Chris Mason0b86a832008-03-24 15:01:56 -04001027 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001028 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001029 key.type = BTRFS_DEV_EXTENT_KEY;
1030 ret = btrfs_insert_empty_item(trans, root, path, &key,
1031 sizeof(*extent));
1032 BUG_ON(ret);
1033
1034 leaf = path->nodes[0];
1035 extent = btrfs_item_ptr(leaf, path->slots[0],
1036 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001037 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1038 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1039 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1040
1041 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1042 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1043 BTRFS_UUID_SIZE);
1044
Chris Mason0b86a832008-03-24 15:01:56 -04001045 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1046 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001047 btrfs_free_path(path);
1048 return ret;
1049}
1050
Chris Masona1b32a52008-09-05 16:09:51 -04001051static noinline int find_next_chunk(struct btrfs_root *root,
1052 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001053{
1054 struct btrfs_path *path;
1055 int ret;
1056 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001057 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001058 struct btrfs_key found_key;
1059
1060 path = btrfs_alloc_path();
1061 BUG_ON(!path);
1062
Chris Masone17cade2008-04-15 15:41:47 -04001063 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001064 key.offset = (u64)-1;
1065 key.type = BTRFS_CHUNK_ITEM_KEY;
1066
1067 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1068 if (ret < 0)
1069 goto error;
1070
1071 BUG_ON(ret == 0);
1072
1073 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1074 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001075 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001076 } else {
1077 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1078 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001079 if (found_key.objectid != objectid)
1080 *offset = 0;
1081 else {
1082 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1083 struct btrfs_chunk);
1084 *offset = found_key.offset +
1085 btrfs_chunk_length(path->nodes[0], chunk);
1086 }
Chris Mason0b86a832008-03-24 15:01:56 -04001087 }
1088 ret = 0;
1089error:
1090 btrfs_free_path(path);
1091 return ret;
1092}
1093
Yan Zheng2b820322008-11-17 21:11:30 -05001094static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001095{
1096 int ret;
1097 struct btrfs_key key;
1098 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001099 struct btrfs_path *path;
1100
1101 root = root->fs_info->chunk_root;
1102
1103 path = btrfs_alloc_path();
1104 if (!path)
1105 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001106
1107 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1108 key.type = BTRFS_DEV_ITEM_KEY;
1109 key.offset = (u64)-1;
1110
1111 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1112 if (ret < 0)
1113 goto error;
1114
1115 BUG_ON(ret == 0);
1116
1117 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1118 BTRFS_DEV_ITEM_KEY);
1119 if (ret) {
1120 *objectid = 1;
1121 } else {
1122 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1123 path->slots[0]);
1124 *objectid = found_key.offset + 1;
1125 }
1126 ret = 0;
1127error:
Yan Zheng2b820322008-11-17 21:11:30 -05001128 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001129 return ret;
1130}
1131
1132/*
1133 * the device information is stored in the chunk root
1134 * the btrfs_device struct should be fully filled in
1135 */
1136int btrfs_add_device(struct btrfs_trans_handle *trans,
1137 struct btrfs_root *root,
1138 struct btrfs_device *device)
1139{
1140 int ret;
1141 struct btrfs_path *path;
1142 struct btrfs_dev_item *dev_item;
1143 struct extent_buffer *leaf;
1144 struct btrfs_key key;
1145 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001146
1147 root = root->fs_info->chunk_root;
1148
1149 path = btrfs_alloc_path();
1150 if (!path)
1151 return -ENOMEM;
1152
Chris Mason0b86a832008-03-24 15:01:56 -04001153 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1154 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001155 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001156
1157 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001158 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001159 if (ret)
1160 goto out;
1161
1162 leaf = path->nodes[0];
1163 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1164
1165 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001166 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001167 btrfs_set_device_type(leaf, dev_item, device->type);
1168 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1169 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1170 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001171 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1172 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001173 btrfs_set_device_group(leaf, dev_item, 0);
1174 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1175 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001176 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001177
Chris Mason0b86a832008-03-24 15:01:56 -04001178 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001179 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001180 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1181 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001182 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001183
Yan Zheng2b820322008-11-17 21:11:30 -05001184 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001185out:
1186 btrfs_free_path(path);
1187 return ret;
1188}
Chris Mason8f18cf12008-04-25 16:53:30 -04001189
Chris Masona061fc82008-05-07 11:43:44 -04001190static int btrfs_rm_dev_item(struct btrfs_root *root,
1191 struct btrfs_device *device)
1192{
1193 int ret;
1194 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001195 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001196 struct btrfs_trans_handle *trans;
1197
1198 root = root->fs_info->chunk_root;
1199
1200 path = btrfs_alloc_path();
1201 if (!path)
1202 return -ENOMEM;
1203
Yan, Zhenga22285a2010-05-16 10:48:46 -04001204 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001205 if (IS_ERR(trans)) {
1206 btrfs_free_path(path);
1207 return PTR_ERR(trans);
1208 }
Chris Masona061fc82008-05-07 11:43:44 -04001209 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1210 key.type = BTRFS_DEV_ITEM_KEY;
1211 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001212 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001213
1214 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1215 if (ret < 0)
1216 goto out;
1217
1218 if (ret > 0) {
1219 ret = -ENOENT;
1220 goto out;
1221 }
1222
1223 ret = btrfs_del_item(trans, root, path);
1224 if (ret)
1225 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001226out:
1227 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001228 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001229 btrfs_commit_transaction(trans, root);
1230 return ret;
1231}
1232
1233int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1234{
1235 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001236 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001237 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001238 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001239 struct btrfs_super_block *disk_super;
1240 u64 all_avail;
1241 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001242 u64 num_devices;
1243 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001244 int ret = 0;
1245
Chris Masona061fc82008-05-07 11:43:44 -04001246 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001247 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001248
1249 all_avail = root->fs_info->avail_data_alloc_bits |
1250 root->fs_info->avail_system_alloc_bits |
1251 root->fs_info->avail_metadata_alloc_bits;
1252
1253 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001254 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001255 printk(KERN_ERR "btrfs: unable to go below four devices "
1256 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001257 ret = -EINVAL;
1258 goto out;
1259 }
1260
1261 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001262 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001263 printk(KERN_ERR "btrfs: unable to go below two "
1264 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001265 ret = -EINVAL;
1266 goto out;
1267 }
1268
Chris Masondfe25022008-05-13 13:46:40 -04001269 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001270 struct list_head *devices;
1271 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001272
Chris Masondfe25022008-05-13 13:46:40 -04001273 device = NULL;
1274 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001275 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001276 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001277 if (tmp->in_fs_metadata && !tmp->bdev) {
1278 device = tmp;
1279 break;
1280 }
1281 }
Chris Masone5e9a522009-06-10 15:17:02 -04001282 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001283 bdev = NULL;
1284 bh = NULL;
1285 disk_super = NULL;
1286 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001287 printk(KERN_ERR "btrfs: no missing devices found to "
1288 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001289 goto out;
1290 }
Chris Masondfe25022008-05-13 13:46:40 -04001291 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001292 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1293 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001294 if (IS_ERR(bdev)) {
1295 ret = PTR_ERR(bdev);
1296 goto out;
1297 }
1298
Yan Zheng2b820322008-11-17 21:11:30 -05001299 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001300 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001301 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001302 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001303 goto error_close;
1304 }
1305 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001306 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001307 dev_uuid = disk_super->dev_item.uuid;
1308 device = btrfs_find_device(root, devid, dev_uuid,
1309 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001310 if (!device) {
1311 ret = -ENOENT;
1312 goto error_brelse;
1313 }
Chris Masondfe25022008-05-13 13:46:40 -04001314 }
Yan Zheng2b820322008-11-17 21:11:30 -05001315
1316 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001317 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1318 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001319 ret = -EINVAL;
1320 goto error_brelse;
1321 }
1322
1323 if (device->writeable) {
1324 list_del_init(&device->dev_alloc_list);
1325 root->fs_info->fs_devices->rw_devices--;
1326 }
Chris Masona061fc82008-05-07 11:43:44 -04001327
1328 ret = btrfs_shrink_device(device, 0);
1329 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001330 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001331
Chris Masona061fc82008-05-07 11:43:44 -04001332 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1333 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001334 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001335
Yan Zheng2b820322008-11-17 21:11:30 -05001336 device->in_fs_metadata = 0;
Chris Masone5e9a522009-06-10 15:17:02 -04001337
1338 /*
1339 * the device list mutex makes sure that we don't change
1340 * the device list while someone else is writing out all
1341 * the device supers.
1342 */
1343 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001344 list_del_init(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001345 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1346
Yan Zhenge4404d62008-12-12 10:03:26 -05001347 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001348
Chris Masoncd02dca2010-12-13 14:56:23 -05001349 if (device->missing)
1350 root->fs_info->fs_devices->missing_devices--;
1351
Yan Zheng2b820322008-11-17 21:11:30 -05001352 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1353 struct btrfs_device, dev_list);
1354 if (device->bdev == root->fs_info->sb->s_bdev)
1355 root->fs_info->sb->s_bdev = next_device->bdev;
1356 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1357 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1358
Yan Zhenge4404d62008-12-12 10:03:26 -05001359 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001360 blkdev_put(device->bdev, device->mode);
Yan Zhenge4404d62008-12-12 10:03:26 -05001361 device->bdev = NULL;
1362 device->fs_devices->open_devices--;
1363 }
1364
Yan Zheng2b820322008-11-17 21:11:30 -05001365 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1366 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1367
Yan Zhenge4404d62008-12-12 10:03:26 -05001368 if (device->fs_devices->open_devices == 0) {
1369 struct btrfs_fs_devices *fs_devices;
1370 fs_devices = root->fs_info->fs_devices;
1371 while (fs_devices) {
1372 if (fs_devices->seed == device->fs_devices)
1373 break;
1374 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001375 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001376 fs_devices->seed = device->fs_devices->seed;
1377 device->fs_devices->seed = NULL;
1378 __btrfs_close_devices(device->fs_devices);
1379 free_fs_devices(device->fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001380 }
1381
1382 /*
1383 * at this point, the device is zero sized. We want to
1384 * remove it from the devices list and zero out the old super
1385 */
1386 if (device->writeable) {
Chris Masondfe25022008-05-13 13:46:40 -04001387 /* make sure this device isn't detected as part of
1388 * the FS anymore
1389 */
1390 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1391 set_buffer_dirty(bh);
1392 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001393 }
Chris Masona061fc82008-05-07 11:43:44 -04001394
Chris Masona061fc82008-05-07 11:43:44 -04001395 kfree(device->name);
1396 kfree(device);
1397 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001398
1399error_brelse:
1400 brelse(bh);
1401error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001402 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001403 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001404out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001405 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001406 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001407 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001408error_undo:
1409 if (device->writeable) {
1410 list_add(&device->dev_alloc_list,
1411 &root->fs_info->fs_devices->alloc_list);
1412 root->fs_info->fs_devices->rw_devices++;
1413 }
1414 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001415}
1416
Yan Zheng2b820322008-11-17 21:11:30 -05001417/*
1418 * does all the dirty work required for changing file system's UUID.
1419 */
1420static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1421 struct btrfs_root *root)
1422{
1423 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1424 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001425 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001426 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1427 struct btrfs_device *device;
1428 u64 super_flags;
1429
1430 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001431 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001432 return -EINVAL;
1433
Yan Zhenge4404d62008-12-12 10:03:26 -05001434 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1435 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001436 return -ENOMEM;
1437
Yan Zhenge4404d62008-12-12 10:03:26 -05001438 old_devices = clone_fs_devices(fs_devices);
1439 if (IS_ERR(old_devices)) {
1440 kfree(seed_devices);
1441 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001442 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001443
Yan Zheng2b820322008-11-17 21:11:30 -05001444 list_add(&old_devices->list, &fs_uuids);
1445
Yan Zhenge4404d62008-12-12 10:03:26 -05001446 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1447 seed_devices->opened = 1;
1448 INIT_LIST_HEAD(&seed_devices->devices);
1449 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001450 mutex_init(&seed_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001451 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1452 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1453 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1454 device->fs_devices = seed_devices;
1455 }
1456
Yan Zheng2b820322008-11-17 21:11:30 -05001457 fs_devices->seeding = 0;
1458 fs_devices->num_devices = 0;
1459 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001460 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001461
1462 generate_random_uuid(fs_devices->fsid);
1463 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1464 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1465 super_flags = btrfs_super_flags(disk_super) &
1466 ~BTRFS_SUPER_FLAG_SEEDING;
1467 btrfs_set_super_flags(disk_super, super_flags);
1468
1469 return 0;
1470}
1471
1472/*
1473 * strore the expected generation for seed devices in device items.
1474 */
1475static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1476 struct btrfs_root *root)
1477{
1478 struct btrfs_path *path;
1479 struct extent_buffer *leaf;
1480 struct btrfs_dev_item *dev_item;
1481 struct btrfs_device *device;
1482 struct btrfs_key key;
1483 u8 fs_uuid[BTRFS_UUID_SIZE];
1484 u8 dev_uuid[BTRFS_UUID_SIZE];
1485 u64 devid;
1486 int ret;
1487
1488 path = btrfs_alloc_path();
1489 if (!path)
1490 return -ENOMEM;
1491
1492 root = root->fs_info->chunk_root;
1493 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1494 key.offset = 0;
1495 key.type = BTRFS_DEV_ITEM_KEY;
1496
1497 while (1) {
1498 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1499 if (ret < 0)
1500 goto error;
1501
1502 leaf = path->nodes[0];
1503next_slot:
1504 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1505 ret = btrfs_next_leaf(root, path);
1506 if (ret > 0)
1507 break;
1508 if (ret < 0)
1509 goto error;
1510 leaf = path->nodes[0];
1511 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1512 btrfs_release_path(root, path);
1513 continue;
1514 }
1515
1516 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1517 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1518 key.type != BTRFS_DEV_ITEM_KEY)
1519 break;
1520
1521 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1522 struct btrfs_dev_item);
1523 devid = btrfs_device_id(leaf, dev_item);
1524 read_extent_buffer(leaf, dev_uuid,
1525 (unsigned long)btrfs_device_uuid(dev_item),
1526 BTRFS_UUID_SIZE);
1527 read_extent_buffer(leaf, fs_uuid,
1528 (unsigned long)btrfs_device_fsid(dev_item),
1529 BTRFS_UUID_SIZE);
1530 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1531 BUG_ON(!device);
1532
1533 if (device->fs_devices->seeding) {
1534 btrfs_set_device_generation(leaf, dev_item,
1535 device->generation);
1536 btrfs_mark_buffer_dirty(leaf);
1537 }
1538
1539 path->slots[0]++;
1540 goto next_slot;
1541 }
1542 ret = 0;
1543error:
1544 btrfs_free_path(path);
1545 return ret;
1546}
1547
Chris Mason788f20e2008-04-28 15:29:42 -04001548int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1549{
1550 struct btrfs_trans_handle *trans;
1551 struct btrfs_device *device;
1552 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001553 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001554 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001555 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001556 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001557 int ret = 0;
1558
Yan Zheng2b820322008-11-17 21:11:30 -05001559 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1560 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001561
Tejun Heod4d77622010-11-13 11:55:18 +01001562 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1563 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001564 if (IS_ERR(bdev))
1565 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001566
Yan Zheng2b820322008-11-17 21:11:30 -05001567 if (root->fs_info->fs_devices->seeding) {
1568 seeding_dev = 1;
1569 down_write(&sb->s_umount);
1570 mutex_lock(&uuid_mutex);
1571 }
1572
Chris Mason8c8bee12008-09-29 11:19:10 -04001573 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001574 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001575
Chris Mason788f20e2008-04-28 15:29:42 -04001576 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001577 /*
1578 * we have the volume lock, so we don't need the extra
1579 * device list mutex while reading the list here.
1580 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001581 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001582 if (device->bdev == bdev) {
1583 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001584 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001585 }
1586 }
1587
1588 device = kzalloc(sizeof(*device), GFP_NOFS);
1589 if (!device) {
1590 /* we can safely leave the fs_devices entry around */
1591 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001592 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001593 }
1594
Chris Mason788f20e2008-04-28 15:29:42 -04001595 device->name = kstrdup(device_path, GFP_NOFS);
1596 if (!device->name) {
1597 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001598 ret = -ENOMEM;
1599 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001600 }
Yan Zheng2b820322008-11-17 21:11:30 -05001601
1602 ret = find_next_devid(root, &device->devid);
1603 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001604 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001605 kfree(device);
1606 goto error;
1607 }
1608
Yan, Zhenga22285a2010-05-16 10:48:46 -04001609 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001610 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001611 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001612 kfree(device);
1613 ret = PTR_ERR(trans);
1614 goto error;
1615 }
1616
Yan Zheng2b820322008-11-17 21:11:30 -05001617 lock_chunks(root);
1618
Yan Zheng2b820322008-11-17 21:11:30 -05001619 device->writeable = 1;
1620 device->work.func = pending_bios_fn;
1621 generate_random_uuid(device->uuid);
1622 spin_lock_init(&device->io_lock);
1623 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001624 device->io_width = root->sectorsize;
1625 device->io_align = root->sectorsize;
1626 device->sector_size = root->sectorsize;
1627 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001628 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001629 device->dev_root = root->fs_info->dev_root;
1630 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001631 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001632 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001633 set_blocksize(device->bdev, 4096);
1634
Yan Zheng2b820322008-11-17 21:11:30 -05001635 if (seeding_dev) {
1636 sb->s_flags &= ~MS_RDONLY;
1637 ret = btrfs_prepare_sprout(trans, root);
1638 BUG_ON(ret);
1639 }
1640
1641 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001642
1643 /*
1644 * we don't want write_supers to jump in here with our device
1645 * half setup
1646 */
1647 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001648 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1649 list_add(&device->dev_alloc_list,
1650 &root->fs_info->fs_devices->alloc_list);
1651 root->fs_info->fs_devices->num_devices++;
1652 root->fs_info->fs_devices->open_devices++;
1653 root->fs_info->fs_devices->rw_devices++;
1654 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1655
Chris Masonc2898112009-06-10 09:51:32 -04001656 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1657 root->fs_info->fs_devices->rotating = 1;
1658
Chris Mason788f20e2008-04-28 15:29:42 -04001659 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1660 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1661 total_bytes + device->total_bytes);
1662
1663 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1664 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1665 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001666 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001667
Yan Zheng2b820322008-11-17 21:11:30 -05001668 if (seeding_dev) {
1669 ret = init_first_rw_device(trans, root, device);
1670 BUG_ON(ret);
1671 ret = btrfs_finish_sprout(trans, root);
1672 BUG_ON(ret);
1673 } else {
1674 ret = btrfs_add_device(trans, root, device);
1675 }
1676
Chris Mason913d9522009-03-10 13:17:18 -04001677 /*
1678 * we've got more storage, clear any full flags on the space
1679 * infos
1680 */
1681 btrfs_clear_space_info_full(root->fs_info);
1682
Chris Mason7d9eb122008-07-08 14:19:17 -04001683 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001684 btrfs_commit_transaction(trans, root);
1685
1686 if (seeding_dev) {
1687 mutex_unlock(&uuid_mutex);
1688 up_write(&sb->s_umount);
1689
1690 ret = btrfs_relocate_sys_chunks(root);
1691 BUG_ON(ret);
1692 }
1693out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001694 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001695 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001696error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001697 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001698 if (seeding_dev) {
1699 mutex_unlock(&uuid_mutex);
1700 up_write(&sb->s_umount);
1701 }
Chris Mason788f20e2008-04-28 15:29:42 -04001702 goto out;
1703}
1704
Chris Masond3977122009-01-05 21:25:51 -05001705static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1706 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001707{
1708 int ret;
1709 struct btrfs_path *path;
1710 struct btrfs_root *root;
1711 struct btrfs_dev_item *dev_item;
1712 struct extent_buffer *leaf;
1713 struct btrfs_key key;
1714
1715 root = device->dev_root->fs_info->chunk_root;
1716
1717 path = btrfs_alloc_path();
1718 if (!path)
1719 return -ENOMEM;
1720
1721 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1722 key.type = BTRFS_DEV_ITEM_KEY;
1723 key.offset = device->devid;
1724
1725 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1726 if (ret < 0)
1727 goto out;
1728
1729 if (ret > 0) {
1730 ret = -ENOENT;
1731 goto out;
1732 }
1733
1734 leaf = path->nodes[0];
1735 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1736
1737 btrfs_set_device_id(leaf, dev_item, device->devid);
1738 btrfs_set_device_type(leaf, dev_item, device->type);
1739 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1740 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1741 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001742 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001743 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1744 btrfs_mark_buffer_dirty(leaf);
1745
1746out:
1747 btrfs_free_path(path);
1748 return ret;
1749}
1750
Chris Mason7d9eb122008-07-08 14:19:17 -04001751static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001752 struct btrfs_device *device, u64 new_size)
1753{
1754 struct btrfs_super_block *super_copy =
1755 &device->dev_root->fs_info->super_copy;
1756 u64 old_total = btrfs_super_total_bytes(super_copy);
1757 u64 diff = new_size - device->total_bytes;
1758
Yan Zheng2b820322008-11-17 21:11:30 -05001759 if (!device->writeable)
1760 return -EACCES;
1761 if (new_size <= device->total_bytes)
1762 return -EINVAL;
1763
Chris Mason8f18cf12008-04-25 16:53:30 -04001764 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001765 device->fs_devices->total_rw_bytes += diff;
1766
1767 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001768 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001769 btrfs_clear_space_info_full(device->dev_root->fs_info);
1770
Chris Mason8f18cf12008-04-25 16:53:30 -04001771 return btrfs_update_device(trans, device);
1772}
1773
Chris Mason7d9eb122008-07-08 14:19:17 -04001774int btrfs_grow_device(struct btrfs_trans_handle *trans,
1775 struct btrfs_device *device, u64 new_size)
1776{
1777 int ret;
1778 lock_chunks(device->dev_root);
1779 ret = __btrfs_grow_device(trans, device, new_size);
1780 unlock_chunks(device->dev_root);
1781 return ret;
1782}
1783
Chris Mason8f18cf12008-04-25 16:53:30 -04001784static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1785 struct btrfs_root *root,
1786 u64 chunk_tree, u64 chunk_objectid,
1787 u64 chunk_offset)
1788{
1789 int ret;
1790 struct btrfs_path *path;
1791 struct btrfs_key key;
1792
1793 root = root->fs_info->chunk_root;
1794 path = btrfs_alloc_path();
1795 if (!path)
1796 return -ENOMEM;
1797
1798 key.objectid = chunk_objectid;
1799 key.offset = chunk_offset;
1800 key.type = BTRFS_CHUNK_ITEM_KEY;
1801
1802 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1803 BUG_ON(ret);
1804
1805 ret = btrfs_del_item(trans, root, path);
1806 BUG_ON(ret);
1807
1808 btrfs_free_path(path);
1809 return 0;
1810}
1811
Christoph Hellwigb2950862008-12-02 09:54:17 -05001812static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001813 chunk_offset)
1814{
1815 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1816 struct btrfs_disk_key *disk_key;
1817 struct btrfs_chunk *chunk;
1818 u8 *ptr;
1819 int ret = 0;
1820 u32 num_stripes;
1821 u32 array_size;
1822 u32 len = 0;
1823 u32 cur;
1824 struct btrfs_key key;
1825
1826 array_size = btrfs_super_sys_array_size(super_copy);
1827
1828 ptr = super_copy->sys_chunk_array;
1829 cur = 0;
1830
1831 while (cur < array_size) {
1832 disk_key = (struct btrfs_disk_key *)ptr;
1833 btrfs_disk_key_to_cpu(&key, disk_key);
1834
1835 len = sizeof(*disk_key);
1836
1837 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1838 chunk = (struct btrfs_chunk *)(ptr + len);
1839 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1840 len += btrfs_chunk_item_size(num_stripes);
1841 } else {
1842 ret = -EIO;
1843 break;
1844 }
1845 if (key.objectid == chunk_objectid &&
1846 key.offset == chunk_offset) {
1847 memmove(ptr, ptr + len, array_size - (cur + len));
1848 array_size -= len;
1849 btrfs_set_super_sys_array_size(super_copy, array_size);
1850 } else {
1851 ptr += len;
1852 cur += len;
1853 }
1854 }
1855 return ret;
1856}
1857
Christoph Hellwigb2950862008-12-02 09:54:17 -05001858static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001859 u64 chunk_tree, u64 chunk_objectid,
1860 u64 chunk_offset)
1861{
1862 struct extent_map_tree *em_tree;
1863 struct btrfs_root *extent_root;
1864 struct btrfs_trans_handle *trans;
1865 struct extent_map *em;
1866 struct map_lookup *map;
1867 int ret;
1868 int i;
1869
1870 root = root->fs_info->chunk_root;
1871 extent_root = root->fs_info->extent_root;
1872 em_tree = &root->fs_info->mapping_tree.map_tree;
1873
Josef Bacikba1bf482009-09-11 16:11:19 -04001874 ret = btrfs_can_relocate(extent_root, chunk_offset);
1875 if (ret)
1876 return -ENOSPC;
1877
Chris Mason8f18cf12008-04-25 16:53:30 -04001878 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001879 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001880 if (ret)
1881 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001882
Yan, Zhenga22285a2010-05-16 10:48:46 -04001883 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001884 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001885
Chris Mason7d9eb122008-07-08 14:19:17 -04001886 lock_chunks(root);
1887
Chris Mason8f18cf12008-04-25 16:53:30 -04001888 /*
1889 * step two, delete the device extents and the
1890 * chunk tree entries
1891 */
Chris Mason890871b2009-09-02 16:24:52 -04001892 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001893 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001894 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001895
Chris Masona061fc82008-05-07 11:43:44 -04001896 BUG_ON(em->start > chunk_offset ||
1897 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001898 map = (struct map_lookup *)em->bdev;
1899
1900 for (i = 0; i < map->num_stripes; i++) {
1901 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1902 map->stripes[i].physical);
1903 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001904
Chris Masondfe25022008-05-13 13:46:40 -04001905 if (map->stripes[i].dev) {
1906 ret = btrfs_update_device(trans, map->stripes[i].dev);
1907 BUG_ON(ret);
1908 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001909 }
1910 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1911 chunk_offset);
1912
1913 BUG_ON(ret);
1914
liubo1abe9b82011-03-24 11:18:59 +00001915 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1916
Chris Mason8f18cf12008-04-25 16:53:30 -04001917 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1918 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1919 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001920 }
1921
Zheng Yan1a40e232008-09-26 10:09:34 -04001922 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1923 BUG_ON(ret);
1924
Chris Mason890871b2009-09-02 16:24:52 -04001925 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001926 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001927 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001928
Chris Mason8f18cf12008-04-25 16:53:30 -04001929 kfree(map);
1930 em->bdev = NULL;
1931
1932 /* once for the tree */
1933 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001934 /* once for us */
1935 free_extent_map(em);
1936
Chris Mason7d9eb122008-07-08 14:19:17 -04001937 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001938 btrfs_end_transaction(trans, root);
1939 return 0;
1940}
1941
Yan Zheng2b820322008-11-17 21:11:30 -05001942static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1943{
1944 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1945 struct btrfs_path *path;
1946 struct extent_buffer *leaf;
1947 struct btrfs_chunk *chunk;
1948 struct btrfs_key key;
1949 struct btrfs_key found_key;
1950 u64 chunk_tree = chunk_root->root_key.objectid;
1951 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001952 bool retried = false;
1953 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001954 int ret;
1955
1956 path = btrfs_alloc_path();
1957 if (!path)
1958 return -ENOMEM;
1959
Josef Bacikba1bf482009-09-11 16:11:19 -04001960again:
Yan Zheng2b820322008-11-17 21:11:30 -05001961 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1962 key.offset = (u64)-1;
1963 key.type = BTRFS_CHUNK_ITEM_KEY;
1964
1965 while (1) {
1966 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1967 if (ret < 0)
1968 goto error;
1969 BUG_ON(ret == 0);
1970
1971 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1972 key.type);
1973 if (ret < 0)
1974 goto error;
1975 if (ret > 0)
1976 break;
1977
1978 leaf = path->nodes[0];
1979 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1980
1981 chunk = btrfs_item_ptr(leaf, path->slots[0],
1982 struct btrfs_chunk);
1983 chunk_type = btrfs_chunk_type(leaf, chunk);
1984 btrfs_release_path(chunk_root, path);
1985
1986 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1987 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1988 found_key.objectid,
1989 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04001990 if (ret == -ENOSPC)
1991 failed++;
1992 else if (ret)
1993 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05001994 }
1995
1996 if (found_key.offset == 0)
1997 break;
1998 key.offset = found_key.offset - 1;
1999 }
2000 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002001 if (failed && !retried) {
2002 failed = 0;
2003 retried = true;
2004 goto again;
2005 } else if (failed && retried) {
2006 WARN_ON(1);
2007 ret = -ENOSPC;
2008 }
Yan Zheng2b820322008-11-17 21:11:30 -05002009error:
2010 btrfs_free_path(path);
2011 return ret;
2012}
2013
Chris Masonec44a352008-04-28 15:29:52 -04002014static u64 div_factor(u64 num, int factor)
2015{
2016 if (factor == 10)
2017 return num;
2018 num *= factor;
2019 do_div(num, 10);
2020 return num;
2021}
2022
Chris Masonec44a352008-04-28 15:29:52 -04002023int btrfs_balance(struct btrfs_root *dev_root)
2024{
2025 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002026 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2027 struct btrfs_device *device;
2028 u64 old_size;
2029 u64 size_to_free;
2030 struct btrfs_path *path;
2031 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002032 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2033 struct btrfs_trans_handle *trans;
2034 struct btrfs_key found_key;
2035
Yan Zheng2b820322008-11-17 21:11:30 -05002036 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2037 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002038
Ben Hutchings6f88a442010-12-29 14:55:03 +00002039 if (!capable(CAP_SYS_ADMIN))
2040 return -EPERM;
2041
Chris Mason7d9eb122008-07-08 14:19:17 -04002042 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002043 dev_root = dev_root->fs_info->dev_root;
2044
Chris Masonec44a352008-04-28 15:29:52 -04002045 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002046 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002047 old_size = device->total_bytes;
2048 size_to_free = div_factor(old_size, 1);
2049 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002050 if (!device->writeable ||
2051 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002052 continue;
2053
2054 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002055 if (ret == -ENOSPC)
2056 break;
Chris Masonec44a352008-04-28 15:29:52 -04002057 BUG_ON(ret);
2058
Yan, Zhenga22285a2010-05-16 10:48:46 -04002059 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002060 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002061
2062 ret = btrfs_grow_device(trans, device, old_size);
2063 BUG_ON(ret);
2064
2065 btrfs_end_transaction(trans, dev_root);
2066 }
2067
2068 /* step two, relocate all the chunks */
2069 path = btrfs_alloc_path();
2070 BUG_ON(!path);
2071
2072 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2073 key.offset = (u64)-1;
2074 key.type = BTRFS_CHUNK_ITEM_KEY;
2075
Chris Masond3977122009-01-05 21:25:51 -05002076 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002077 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2078 if (ret < 0)
2079 goto error;
2080
2081 /*
2082 * this shouldn't happen, it means the last relocate
2083 * failed
2084 */
2085 if (ret == 0)
2086 break;
2087
2088 ret = btrfs_previous_item(chunk_root, path, 0,
2089 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002090 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002091 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002092
Chris Masonec44a352008-04-28 15:29:52 -04002093 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2094 path->slots[0]);
2095 if (found_key.objectid != key.objectid)
2096 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002097
Chris Masonec44a352008-04-28 15:29:52 -04002098 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002099 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002100 break;
2101
Chris Mason7d9eb122008-07-08 14:19:17 -04002102 btrfs_release_path(chunk_root, path);
Chris Masonec44a352008-04-28 15:29:52 -04002103 ret = btrfs_relocate_chunk(chunk_root,
2104 chunk_root->root_key.objectid,
2105 found_key.objectid,
2106 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002107 BUG_ON(ret && ret != -ENOSPC);
2108 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002109 }
2110 ret = 0;
2111error:
2112 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002113 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002114 return ret;
2115}
2116
Chris Mason8f18cf12008-04-25 16:53:30 -04002117/*
2118 * shrinking a device means finding all of the device extents past
2119 * the new size, and then following the back refs to the chunks.
2120 * The chunk relocation code actually frees the device extent
2121 */
2122int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2123{
2124 struct btrfs_trans_handle *trans;
2125 struct btrfs_root *root = device->dev_root;
2126 struct btrfs_dev_extent *dev_extent = NULL;
2127 struct btrfs_path *path;
2128 u64 length;
2129 u64 chunk_tree;
2130 u64 chunk_objectid;
2131 u64 chunk_offset;
2132 int ret;
2133 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002134 int failed = 0;
2135 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002136 struct extent_buffer *l;
2137 struct btrfs_key key;
2138 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2139 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002140 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002141 u64 diff = device->total_bytes - new_size;
2142
Yan Zheng2b820322008-11-17 21:11:30 -05002143 if (new_size >= device->total_bytes)
2144 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002145
2146 path = btrfs_alloc_path();
2147 if (!path)
2148 return -ENOMEM;
2149
Chris Mason8f18cf12008-04-25 16:53:30 -04002150 path->reada = 2;
2151
Chris Mason7d9eb122008-07-08 14:19:17 -04002152 lock_chunks(root);
2153
Chris Mason8f18cf12008-04-25 16:53:30 -04002154 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002155 if (device->writeable)
2156 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002157 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002158
Josef Bacikba1bf482009-09-11 16:11:19 -04002159again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002160 key.objectid = device->devid;
2161 key.offset = (u64)-1;
2162 key.type = BTRFS_DEV_EXTENT_KEY;
2163
2164 while (1) {
2165 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2166 if (ret < 0)
2167 goto done;
2168
2169 ret = btrfs_previous_item(root, path, 0, key.type);
2170 if (ret < 0)
2171 goto done;
2172 if (ret) {
2173 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002174 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002175 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002176 }
2177
2178 l = path->nodes[0];
2179 slot = path->slots[0];
2180 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2181
Josef Bacikba1bf482009-09-11 16:11:19 -04002182 if (key.objectid != device->devid) {
2183 btrfs_release_path(root, path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002184 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002185 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002186
2187 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2188 length = btrfs_dev_extent_length(l, dev_extent);
2189
Josef Bacikba1bf482009-09-11 16:11:19 -04002190 if (key.offset + length <= new_size) {
2191 btrfs_release_path(root, path);
Chris Balld6397ba2009-04-27 07:29:03 -04002192 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002193 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002194
2195 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2196 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2197 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2198 btrfs_release_path(root, path);
2199
2200 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2201 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002202 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002203 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002204 if (ret == -ENOSPC)
2205 failed++;
2206 key.offset -= 1;
2207 }
2208
2209 if (failed && !retried) {
2210 failed = 0;
2211 retried = true;
2212 goto again;
2213 } else if (failed && retried) {
2214 ret = -ENOSPC;
2215 lock_chunks(root);
2216
2217 device->total_bytes = old_size;
2218 if (device->writeable)
2219 device->fs_devices->total_rw_bytes += diff;
2220 unlock_chunks(root);
2221 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002222 }
2223
Chris Balld6397ba2009-04-27 07:29:03 -04002224 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002225 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002226 if (IS_ERR(trans)) {
2227 ret = PTR_ERR(trans);
2228 goto done;
2229 }
2230
Chris Balld6397ba2009-04-27 07:29:03 -04002231 lock_chunks(root);
2232
2233 device->disk_total_bytes = new_size;
2234 /* Now btrfs_update_device() will change the on-disk size. */
2235 ret = btrfs_update_device(trans, device);
2236 if (ret) {
2237 unlock_chunks(root);
2238 btrfs_end_transaction(trans, root);
2239 goto done;
2240 }
2241 WARN_ON(diff > old_total);
2242 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2243 unlock_chunks(root);
2244 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002245done:
2246 btrfs_free_path(path);
2247 return ret;
2248}
2249
Christoph Hellwigb2950862008-12-02 09:54:17 -05002250static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002251 struct btrfs_root *root,
2252 struct btrfs_key *key,
2253 struct btrfs_chunk *chunk, int item_size)
2254{
2255 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2256 struct btrfs_disk_key disk_key;
2257 u32 array_size;
2258 u8 *ptr;
2259
2260 array_size = btrfs_super_sys_array_size(super_copy);
2261 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2262 return -EFBIG;
2263
2264 ptr = super_copy->sys_chunk_array + array_size;
2265 btrfs_cpu_key_to_disk(&disk_key, key);
2266 memcpy(ptr, &disk_key, sizeof(disk_key));
2267 ptr += sizeof(disk_key);
2268 memcpy(ptr, chunk, item_size);
2269 item_size += sizeof(disk_key);
2270 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2271 return 0;
2272}
2273
Chris Masond3977122009-01-05 21:25:51 -05002274static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
Chris Masona1b32a52008-09-05 16:09:51 -04002275 int num_stripes, int sub_stripes)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002276{
2277 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2278 return calc_size;
2279 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2280 return calc_size * (num_stripes / sub_stripes);
2281 else
2282 return calc_size * num_stripes;
2283}
2284
Miao Xieb2117a32011-01-05 10:07:28 +00002285static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2286 int *num_stripes, int *min_stripes,
2287 int *sub_stripes)
2288{
2289 *num_stripes = 1;
2290 *min_stripes = 1;
2291 *sub_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04002292
Chris Masona40a90a2008-04-18 11:55:51 -04002293 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002294 *num_stripes = fs_devices->rw_devices;
2295 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002296 }
2297 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002298 *num_stripes = 2;
2299 *min_stripes = 2;
Chris Masona40a90a2008-04-18 11:55:51 -04002300 }
Chris Mason8790d502008-04-03 16:29:03 -04002301 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
Zhao Leif3eae7e2010-03-25 12:32:59 +00002302 if (fs_devices->rw_devices < 2)
Chris Mason9b3f68b2008-04-18 10:29:51 -04002303 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002304 *num_stripes = 2;
2305 *min_stripes = 2;
Chris Mason8790d502008-04-03 16:29:03 -04002306 }
Chris Mason321aecc2008-04-16 10:49:51 -04002307 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
Miao Xieb2117a32011-01-05 10:07:28 +00002308 *num_stripes = fs_devices->rw_devices;
2309 if (*num_stripes < 4)
Chris Mason321aecc2008-04-16 10:49:51 -04002310 return -ENOSPC;
Miao Xieb2117a32011-01-05 10:07:28 +00002311 *num_stripes &= ~(u32)1;
2312 *sub_stripes = 2;
2313 *min_stripes = 4;
Chris Mason321aecc2008-04-16 10:49:51 -04002314 }
Chris Mason9b3f68b2008-04-18 10:29:51 -04002315
Miao Xieb2117a32011-01-05 10:07:28 +00002316 return 0;
2317}
2318
2319static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2320 u64 proposed_size, u64 type,
2321 int num_stripes, int small_stripe)
2322{
2323 int min_stripe_size = 1 * 1024 * 1024;
2324 u64 calc_size = proposed_size;
2325 u64 max_chunk_size = calc_size;
2326 int ncopies = 1;
2327
2328 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2329 BTRFS_BLOCK_GROUP_DUP |
2330 BTRFS_BLOCK_GROUP_RAID10))
2331 ncopies = 2;
2332
Chris Mason9b3f68b2008-04-18 10:29:51 -04002333 if (type & BTRFS_BLOCK_GROUP_DATA) {
2334 max_chunk_size = 10 * calc_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002335 min_stripe_size = 64 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002336 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Josef Bacik83d3c962009-12-07 21:45:59 +00002337 max_chunk_size = 256 * 1024 * 1024;
Chris Masona40a90a2008-04-18 11:55:51 -04002338 min_stripe_size = 32 * 1024 * 1024;
2339 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2340 calc_size = 8 * 1024 * 1024;
2341 max_chunk_size = calc_size * 2;
2342 min_stripe_size = 1 * 1024 * 1024;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002343 }
2344
Yan Zheng2b820322008-11-17 21:11:30 -05002345 /* we don't want a chunk larger than 10% of writeable space */
2346 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2347 max_chunk_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002348
Miao Xie1974a3b2011-01-05 10:07:24 +00002349 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2350 calc_size = max_chunk_size * ncopies;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002351 do_div(calc_size, num_stripes);
Miao Xieb2117a32011-01-05 10:07:28 +00002352 do_div(calc_size, BTRFS_STRIPE_LEN);
2353 calc_size *= BTRFS_STRIPE_LEN;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002354 }
Josef Bacik0cad8a112010-03-17 20:45:56 +00002355
Chris Mason9b3f68b2008-04-18 10:29:51 -04002356 /* we don't want tiny stripes */
Miao Xieb2117a32011-01-05 10:07:28 +00002357 if (!small_stripe)
Josef Bacik0cad8a112010-03-17 20:45:56 +00002358 calc_size = max_t(u64, min_stripe_size, calc_size);
Chris Mason9b3f68b2008-04-18 10:29:51 -04002359
Chris Mason9f680ce2010-04-06 09:37:47 -04002360 /*
Miao Xieb2117a32011-01-05 10:07:28 +00002361 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
Chris Mason9f680ce2010-04-06 09:37:47 -04002362 * we end up with something bigger than a stripe
2363 */
Miao Xieb2117a32011-01-05 10:07:28 +00002364 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
Chris Mason9f680ce2010-04-06 09:37:47 -04002365
Miao Xieb2117a32011-01-05 10:07:28 +00002366 do_div(calc_size, BTRFS_STRIPE_LEN);
2367 calc_size *= BTRFS_STRIPE_LEN;
2368
2369 return calc_size;
2370}
2371
2372static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2373 int num_stripes)
2374{
2375 struct map_lookup *new;
2376 size_t len = map_lookup_size(num_stripes);
2377
2378 BUG_ON(map->num_stripes < num_stripes);
2379
2380 if (map->num_stripes == num_stripes)
2381 return map;
2382
2383 new = kmalloc(len, GFP_NOFS);
2384 if (!new) {
2385 /* just change map->num_stripes */
2386 map->num_stripes = num_stripes;
2387 return map;
2388 }
2389
2390 memcpy(new, map, len);
2391 new->num_stripes = num_stripes;
2392 kfree(map);
2393 return new;
2394}
2395
2396/*
2397 * helper to allocate device space from btrfs_device_info, in which we stored
2398 * max free space information of every device. It is used when we can not
2399 * allocate chunks by default size.
2400 *
2401 * By this helper, we can allocate a new chunk as larger as possible.
2402 */
2403static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2404 struct btrfs_fs_devices *fs_devices,
2405 struct btrfs_device_info *devices,
2406 int nr_device, u64 type,
2407 struct map_lookup **map_lookup,
2408 int min_stripes, u64 *stripe_size)
2409{
2410 int i, index, sort_again = 0;
2411 int min_devices = min_stripes;
2412 u64 max_avail, min_free;
2413 struct map_lookup *map = *map_lookup;
2414 int ret;
2415
2416 if (nr_device < min_stripes)
2417 return -ENOSPC;
2418
2419 btrfs_descending_sort_devices(devices, nr_device);
2420
2421 max_avail = devices[0].max_avail;
2422 if (!max_avail)
2423 return -ENOSPC;
2424
2425 for (i = 0; i < nr_device; i++) {
2426 /*
2427 * if dev_offset = 0, it means the free space of this device
2428 * is less than what we need, and we didn't search max avail
2429 * extent on this device, so do it now.
2430 */
2431 if (!devices[i].dev_offset) {
2432 ret = find_free_dev_extent(trans, devices[i].dev,
2433 max_avail,
2434 &devices[i].dev_offset,
2435 &devices[i].max_avail);
2436 if (ret != 0 && ret != -ENOSPC)
2437 return ret;
2438 sort_again = 1;
2439 }
2440 }
2441
2442 /* we update the max avail free extent of each devices, sort again */
2443 if (sort_again)
2444 btrfs_descending_sort_devices(devices, nr_device);
2445
2446 if (type & BTRFS_BLOCK_GROUP_DUP)
2447 min_devices = 1;
2448
2449 if (!devices[min_devices - 1].max_avail)
2450 return -ENOSPC;
2451
2452 max_avail = devices[min_devices - 1].max_avail;
2453 if (type & BTRFS_BLOCK_GROUP_DUP)
2454 do_div(max_avail, 2);
2455
2456 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2457 min_stripes, 1);
2458 if (type & BTRFS_BLOCK_GROUP_DUP)
2459 min_free = max_avail * 2;
2460 else
2461 min_free = max_avail;
2462
2463 if (min_free > devices[min_devices - 1].max_avail)
2464 return -ENOSPC;
2465
2466 map = __shrink_map_lookup_stripes(map, min_stripes);
2467 *stripe_size = max_avail;
2468
2469 index = 0;
2470 for (i = 0; i < min_stripes; i++) {
2471 map->stripes[i].dev = devices[index].dev;
2472 map->stripes[i].physical = devices[index].dev_offset;
2473 if (type & BTRFS_BLOCK_GROUP_DUP) {
2474 i++;
2475 map->stripes[i].dev = devices[index].dev;
2476 map->stripes[i].physical = devices[index].dev_offset +
2477 max_avail;
2478 }
2479 index++;
2480 }
2481 *map_lookup = map;
2482
2483 return 0;
2484}
2485
2486static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2487 struct btrfs_root *extent_root,
2488 struct map_lookup **map_ret,
2489 u64 *num_bytes, u64 *stripe_size,
2490 u64 start, u64 type)
2491{
2492 struct btrfs_fs_info *info = extent_root->fs_info;
2493 struct btrfs_device *device = NULL;
2494 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2495 struct list_head *cur;
2496 struct map_lookup *map;
2497 struct extent_map_tree *em_tree;
2498 struct extent_map *em;
2499 struct btrfs_device_info *devices_info;
2500 struct list_head private_devs;
2501 u64 calc_size = 1024 * 1024 * 1024;
2502 u64 min_free;
2503 u64 avail;
2504 u64 dev_offset;
2505 int num_stripes;
2506 int min_stripes;
2507 int sub_stripes;
2508 int min_devices; /* the min number of devices we need */
2509 int i;
2510 int ret;
2511 int index;
2512
2513 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2514 (type & BTRFS_BLOCK_GROUP_DUP)) {
2515 WARN_ON(1);
2516 type &= ~BTRFS_BLOCK_GROUP_DUP;
2517 }
2518 if (list_empty(&fs_devices->alloc_list))
2519 return -ENOSPC;
2520
2521 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2522 &min_stripes, &sub_stripes);
2523 if (ret)
2524 return ret;
2525
2526 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2527 GFP_NOFS);
2528 if (!devices_info)
2529 return -ENOMEM;
2530
2531 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2532 if (!map) {
2533 ret = -ENOMEM;
2534 goto error;
2535 }
2536 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002537
Yan Zheng2b820322008-11-17 21:11:30 -05002538 cur = fs_devices->alloc_list.next;
Chris Mason6324fbf2008-03-24 15:01:59 -04002539 index = 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002540 i = 0;
Chris Mason611f0e02008-04-03 16:29:03 -04002541
Miao Xieb2117a32011-01-05 10:07:28 +00002542 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2543 num_stripes, 0);
2544
2545 if (type & BTRFS_BLOCK_GROUP_DUP) {
Chris Mason611f0e02008-04-03 16:29:03 -04002546 min_free = calc_size * 2;
Miao Xieb2117a32011-01-05 10:07:28 +00002547 min_devices = 1;
2548 } else {
Chris Mason9b3f68b2008-04-18 10:29:51 -04002549 min_free = calc_size;
Miao Xieb2117a32011-01-05 10:07:28 +00002550 min_devices = min_stripes;
2551 }
Chris Masonad5bd912008-04-21 08:28:10 -04002552
Yan Zheng2b820322008-11-17 21:11:30 -05002553 INIT_LIST_HEAD(&private_devs);
Chris Masond3977122009-01-05 21:25:51 -05002554 while (index < num_stripes) {
Chris Masonb3075712008-04-22 09:22:07 -04002555 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002556 BUG_ON(!device->writeable);
Chris Masondfe25022008-05-13 13:46:40 -04002557 if (device->total_bytes > device->bytes_used)
2558 avail = device->total_bytes - device->bytes_used;
2559 else
2560 avail = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04002561 cur = cur->next;
Chris Mason8f18cf12008-04-25 16:53:30 -04002562
Chris Masondfe25022008-05-13 13:46:40 -04002563 if (device->in_fs_metadata && avail >= min_free) {
Miao Xieb2117a32011-01-05 10:07:28 +00002564 ret = find_free_dev_extent(trans, device, min_free,
2565 &devices_info[i].dev_offset,
2566 &devices_info[i].max_avail);
Chris Mason8f18cf12008-04-25 16:53:30 -04002567 if (ret == 0) {
2568 list_move_tail(&device->dev_alloc_list,
2569 &private_devs);
Yan Zheng2b820322008-11-17 21:11:30 -05002570 map->stripes[index].dev = device;
Miao Xieb2117a32011-01-05 10:07:28 +00002571 map->stripes[index].physical =
2572 devices_info[i].dev_offset;
Chris Mason611f0e02008-04-03 16:29:03 -04002573 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002574 if (type & BTRFS_BLOCK_GROUP_DUP) {
2575 map->stripes[index].dev = device;
2576 map->stripes[index].physical =
Miao Xieb2117a32011-01-05 10:07:28 +00002577 devices_info[i].dev_offset +
2578 calc_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002579 index++;
Yan Zheng2b820322008-11-17 21:11:30 -05002580 }
Miao Xieb2117a32011-01-05 10:07:28 +00002581 } else if (ret != -ENOSPC)
2582 goto error;
2583
2584 devices_info[i].dev = device;
2585 i++;
2586 } else if (device->in_fs_metadata &&
2587 avail >= BTRFS_STRIPE_LEN) {
2588 devices_info[i].dev = device;
2589 devices_info[i].max_avail = avail;
2590 i++;
2591 }
2592
Yan Zheng2b820322008-11-17 21:11:30 -05002593 if (cur == &fs_devices->alloc_list)
Chris Mason6324fbf2008-03-24 15:01:59 -04002594 break;
2595 }
Miao Xieb2117a32011-01-05 10:07:28 +00002596
Yan Zheng2b820322008-11-17 21:11:30 -05002597 list_splice(&private_devs, &fs_devices->alloc_list);
Chris Mason6324fbf2008-03-24 15:01:59 -04002598 if (index < num_stripes) {
Chris Masona40a90a2008-04-18 11:55:51 -04002599 if (index >= min_stripes) {
2600 num_stripes = index;
2601 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2602 num_stripes /= sub_stripes;
2603 num_stripes *= sub_stripes;
2604 }
Miao Xieb2117a32011-01-05 10:07:28 +00002605
2606 map = __shrink_map_lookup_stripes(map, num_stripes);
2607 } else if (i >= min_devices) {
2608 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2609 devices_info, i, type,
2610 &map, min_stripes,
2611 &calc_size);
2612 if (ret)
2613 goto error;
2614 } else {
2615 ret = -ENOSPC;
2616 goto error;
Chris Masona40a90a2008-04-18 11:55:51 -04002617 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002618 }
Chris Mason593060d2008-03-25 16:50:33 -04002619 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002620 map->stripe_len = BTRFS_STRIPE_LEN;
2621 map->io_align = BTRFS_STRIPE_LEN;
2622 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002623 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002624 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002625
Yan Zheng2b820322008-11-17 21:11:30 -05002626 *map_ret = map;
2627 *stripe_size = calc_size;
2628 *num_bytes = chunk_bytes_by_type(type, calc_size,
Miao Xieb2117a32011-01-05 10:07:28 +00002629 map->num_stripes, sub_stripes);
Chris Mason0b86a832008-03-24 15:01:56 -04002630
liubo1abe9b82011-03-24 11:18:59 +00002631 trace_btrfs_chunk_alloc(info->chunk_root, map, start, *num_bytes);
2632
Chris Mason0b86a832008-03-24 15:01:56 -04002633 em = alloc_extent_map(GFP_NOFS);
Yan Zheng2b820322008-11-17 21:11:30 -05002634 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002635 ret = -ENOMEM;
2636 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002637 }
Chris Mason0b86a832008-03-24 15:01:56 -04002638 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002639 em->start = start;
Chris Masone17cade2008-04-15 15:41:47 -04002640 em->len = *num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002641 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002642 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002643
Chris Mason0b86a832008-03-24 15:01:56 -04002644 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002645 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002646 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002647 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002648 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002649 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002650
2651 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2652 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2653 start, *num_bytes);
2654 BUG_ON(ret);
2655
2656 index = 0;
2657 while (index < map->num_stripes) {
2658 device = map->stripes[index].dev;
2659 dev_offset = map->stripes[index].physical;
2660
2661 ret = btrfs_alloc_dev_extent(trans, device,
2662 info->chunk_root->root_key.objectid,
2663 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2664 start, dev_offset, calc_size);
2665 BUG_ON(ret);
2666 index++;
2667 }
2668
Miao Xieb2117a32011-01-05 10:07:28 +00002669 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002670 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002671
2672error:
2673 kfree(map);
2674 kfree(devices_info);
2675 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002676}
2677
2678static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2679 struct btrfs_root *extent_root,
2680 struct map_lookup *map, u64 chunk_offset,
2681 u64 chunk_size, u64 stripe_size)
2682{
2683 u64 dev_offset;
2684 struct btrfs_key key;
2685 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2686 struct btrfs_device *device;
2687 struct btrfs_chunk *chunk;
2688 struct btrfs_stripe *stripe;
2689 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2690 int index = 0;
2691 int ret;
2692
2693 chunk = kzalloc(item_size, GFP_NOFS);
2694 if (!chunk)
2695 return -ENOMEM;
2696
2697 index = 0;
2698 while (index < map->num_stripes) {
2699 device = map->stripes[index].dev;
2700 device->bytes_used += stripe_size;
2701 ret = btrfs_update_device(trans, device);
2702 BUG_ON(ret);
2703 index++;
2704 }
2705
2706 index = 0;
2707 stripe = &chunk->stripe;
2708 while (index < map->num_stripes) {
2709 device = map->stripes[index].dev;
2710 dev_offset = map->stripes[index].physical;
2711
2712 btrfs_set_stack_stripe_devid(stripe, device->devid);
2713 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2714 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2715 stripe++;
2716 index++;
2717 }
2718
2719 btrfs_set_stack_chunk_length(chunk, chunk_size);
2720 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2721 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2722 btrfs_set_stack_chunk_type(chunk, map->type);
2723 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2724 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2725 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2726 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2727 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2728
2729 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2730 key.type = BTRFS_CHUNK_ITEM_KEY;
2731 key.offset = chunk_offset;
2732
2733 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2734 BUG_ON(ret);
2735
2736 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2737 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2738 item_size);
2739 BUG_ON(ret);
2740 }
liubo1abe9b82011-03-24 11:18:59 +00002741
Yan Zheng2b820322008-11-17 21:11:30 -05002742 kfree(chunk);
2743 return 0;
2744}
2745
2746/*
2747 * Chunk allocation falls into two parts. The first part does works
2748 * that make the new allocated chunk useable, but not do any operation
2749 * that modifies the chunk tree. The second part does the works that
2750 * require modifying the chunk tree. This division is important for the
2751 * bootstrap process of adding storage to a seed btrfs.
2752 */
2753int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2754 struct btrfs_root *extent_root, u64 type)
2755{
2756 u64 chunk_offset;
2757 u64 chunk_size;
2758 u64 stripe_size;
2759 struct map_lookup *map;
2760 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2761 int ret;
2762
2763 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2764 &chunk_offset);
2765 if (ret)
2766 return ret;
2767
2768 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2769 &stripe_size, chunk_offset, type);
2770 if (ret)
2771 return ret;
2772
2773 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2774 chunk_size, stripe_size);
2775 BUG_ON(ret);
2776 return 0;
2777}
2778
Chris Masond3977122009-01-05 21:25:51 -05002779static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002780 struct btrfs_root *root,
2781 struct btrfs_device *device)
2782{
2783 u64 chunk_offset;
2784 u64 sys_chunk_offset;
2785 u64 chunk_size;
2786 u64 sys_chunk_size;
2787 u64 stripe_size;
2788 u64 sys_stripe_size;
2789 u64 alloc_profile;
2790 struct map_lookup *map;
2791 struct map_lookup *sys_map;
2792 struct btrfs_fs_info *fs_info = root->fs_info;
2793 struct btrfs_root *extent_root = fs_info->extent_root;
2794 int ret;
2795
2796 ret = find_next_chunk(fs_info->chunk_root,
2797 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2798 BUG_ON(ret);
2799
2800 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2801 (fs_info->metadata_alloc_profile &
2802 fs_info->avail_metadata_alloc_bits);
2803 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2804
2805 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2806 &stripe_size, chunk_offset, alloc_profile);
2807 BUG_ON(ret);
2808
2809 sys_chunk_offset = chunk_offset + chunk_size;
2810
2811 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2812 (fs_info->system_alloc_profile &
2813 fs_info->avail_system_alloc_bits);
2814 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2815
2816 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2817 &sys_chunk_size, &sys_stripe_size,
2818 sys_chunk_offset, alloc_profile);
2819 BUG_ON(ret);
2820
2821 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2822 BUG_ON(ret);
2823
2824 /*
2825 * Modifying chunk tree needs allocating new blocks from both
2826 * system block group and metadata block group. So we only can
2827 * do operations require modifying the chunk tree after both
2828 * block groups were created.
2829 */
2830 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2831 chunk_size, stripe_size);
2832 BUG_ON(ret);
2833
2834 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2835 sys_chunk_offset, sys_chunk_size,
2836 sys_stripe_size);
2837 BUG_ON(ret);
2838 return 0;
2839}
2840
2841int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2842{
2843 struct extent_map *em;
2844 struct map_lookup *map;
2845 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2846 int readonly = 0;
2847 int i;
2848
Chris Mason890871b2009-09-02 16:24:52 -04002849 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002850 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002851 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002852 if (!em)
2853 return 1;
2854
Josef Bacikf48b9072010-01-27 02:07:59 +00002855 if (btrfs_test_opt(root, DEGRADED)) {
2856 free_extent_map(em);
2857 return 0;
2858 }
2859
Yan Zheng2b820322008-11-17 21:11:30 -05002860 map = (struct map_lookup *)em->bdev;
2861 for (i = 0; i < map->num_stripes; i++) {
2862 if (!map->stripes[i].dev->writeable) {
2863 readonly = 1;
2864 break;
2865 }
2866 }
2867 free_extent_map(em);
2868 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002869}
2870
2871void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2872{
2873 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2874}
2875
2876void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2877{
2878 struct extent_map *em;
2879
Chris Masond3977122009-01-05 21:25:51 -05002880 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002881 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002882 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2883 if (em)
2884 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002885 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002886 if (!em)
2887 break;
2888 kfree(em->bdev);
2889 /* once for us */
2890 free_extent_map(em);
2891 /* once for the tree */
2892 free_extent_map(em);
2893 }
2894}
2895
Chris Masonf1885912008-04-09 16:28:12 -04002896int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2897{
2898 struct extent_map *em;
2899 struct map_lookup *map;
2900 struct extent_map_tree *em_tree = &map_tree->map_tree;
2901 int ret;
2902
Chris Mason890871b2009-09-02 16:24:52 -04002903 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002904 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002905 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002906 BUG_ON(!em);
2907
2908 BUG_ON(em->start > logical || em->start + em->len < logical);
2909 map = (struct map_lookup *)em->bdev;
2910 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2911 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002912 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2913 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002914 else
2915 ret = 1;
2916 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002917 return ret;
2918}
2919
Chris Masondfe25022008-05-13 13:46:40 -04002920static int find_live_mirror(struct map_lookup *map, int first, int num,
2921 int optimal)
2922{
2923 int i;
2924 if (map->stripes[optimal].dev->bdev)
2925 return optimal;
2926 for (i = first; i < first + num; i++) {
2927 if (map->stripes[i].dev->bdev)
2928 return i;
2929 }
2930 /* we couldn't find one that doesn't fail. Just return something
2931 * and the io error handling code will clean up eventually
2932 */
2933 return optimal;
2934}
2935
Chris Masonf2d8d742008-04-21 10:03:05 -04002936static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2937 u64 logical, u64 *length,
2938 struct btrfs_multi_bio **multi_ret,
2939 int mirror_num, struct page *unplug_page)
Chris Mason0b86a832008-03-24 15:01:56 -04002940{
2941 struct extent_map *em;
2942 struct map_lookup *map;
2943 struct extent_map_tree *em_tree = &map_tree->map_tree;
2944 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002945 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002946 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002947 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002948 u64 stripe_nr_orig;
2949 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002950 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002951 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002952 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002953 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002954 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002955 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002956 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002957
Li Dongyangfce3bb92011-03-24 10:24:26 +00002958 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002959 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002960again:
2961 if (multi_ret) {
2962 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2963 GFP_NOFS);
2964 if (!multi)
2965 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002966
2967 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002968 }
Chris Mason0b86a832008-03-24 15:01:56 -04002969
Chris Mason890871b2009-09-02 16:24:52 -04002970 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002971 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002972 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002973
Jiri Slaby2423fdf2010-01-06 16:57:22 +00002974 if (!em && unplug_page) {
2975 kfree(multi);
Chris Masonf2d8d742008-04-21 10:03:05 -04002976 return 0;
Jiri Slaby2423fdf2010-01-06 16:57:22 +00002977 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002978
Chris Mason3b951512008-04-17 11:29:12 -04002979 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002980 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2981 (unsigned long long)logical,
2982 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002983 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002984 }
Chris Mason0b86a832008-03-24 15:01:56 -04002985
2986 BUG_ON(em->start > logical || em->start + em->len < logical);
2987 map = (struct map_lookup *)em->bdev;
2988 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002989
Chris Masonf1885912008-04-09 16:28:12 -04002990 if (mirror_num > map->num_stripes)
2991 mirror_num = 0;
2992
Chris Masoncea9e442008-04-09 16:28:12 -04002993 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002994 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002995 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2996 BTRFS_BLOCK_GROUP_DUP)) {
2997 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002998 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002999 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3000 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003001 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04003002 }
3003 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003004 if (rw & REQ_DISCARD) {
3005 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3006 BTRFS_BLOCK_GROUP_RAID1 |
3007 BTRFS_BLOCK_GROUP_DUP |
3008 BTRFS_BLOCK_GROUP_RAID10)) {
3009 stripes_required = map->num_stripes;
3010 }
3011 }
3012 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04003013 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04003014 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04003015 free_extent_map(em);
3016 kfree(multi);
3017 goto again;
3018 }
Chris Mason593060d2008-03-25 16:50:33 -04003019 stripe_nr = offset;
3020 /*
3021 * stripe_nr counts the total number of stripes we have to stride
3022 * to get to this block
3023 */
3024 do_div(stripe_nr, map->stripe_len);
3025
3026 stripe_offset = stripe_nr * map->stripe_len;
3027 BUG_ON(offset < stripe_offset);
3028
3029 /* stripe_offset is the offset of this block in its stripe*/
3030 stripe_offset = offset - stripe_offset;
3031
Li Dongyangfce3bb92011-03-24 10:24:26 +00003032 if (rw & REQ_DISCARD)
3033 *length = min_t(u64, em->len - offset, *length);
3034 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
3035 BTRFS_BLOCK_GROUP_RAID1 |
3036 BTRFS_BLOCK_GROUP_RAID10 |
3037 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04003038 /* we limit the length of each bio to what fits in a stripe */
3039 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00003040 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04003041 } else {
3042 *length = em->len - offset;
3043 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003044
3045 if (!multi_ret && !unplug_page)
Chris Masoncea9e442008-04-09 16:28:12 -04003046 goto out;
3047
Chris Masonf2d8d742008-04-21 10:03:05 -04003048 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04003049 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003050 stripe_nr_orig = stripe_nr;
3051 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
3052 (~(map->stripe_len - 1));
3053 do_div(stripe_nr_end, map->stripe_len);
3054 stripe_end_offset = stripe_nr_end * map->stripe_len -
3055 (offset + *length);
3056 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3057 if (rw & REQ_DISCARD)
3058 num_stripes = min_t(u64, map->num_stripes,
3059 stripe_nr_end - stripe_nr_orig);
3060 stripe_index = do_div(stripe_nr, map->num_stripes);
3061 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3062 if (unplug_page || (rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masonf2d8d742008-04-21 10:03:05 -04003063 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04003064 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04003065 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003066 else {
3067 stripe_index = find_live_mirror(map, 0,
3068 map->num_stripes,
3069 current->pid % map->num_stripes);
3070 }
Chris Mason2fff7342008-04-29 14:12:09 -04003071
Chris Mason611f0e02008-04-03 16:29:03 -04003072 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00003073 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04003074 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04003075 else if (mirror_num)
3076 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04003077
Chris Mason321aecc2008-04-16 10:49:51 -04003078 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3079 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04003080
3081 stripe_index = do_div(stripe_nr, factor);
3082 stripe_index *= map->sub_stripes;
3083
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003084 if (unplug_page || (rw & REQ_WRITE))
Chris Masonf2d8d742008-04-21 10:03:05 -04003085 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003086 else if (rw & REQ_DISCARD)
3087 num_stripes = min_t(u64, map->sub_stripes *
3088 (stripe_nr_end - stripe_nr_orig),
3089 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003090 else if (mirror_num)
3091 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003092 else {
3093 stripe_index = find_live_mirror(map, stripe_index,
3094 map->sub_stripes, stripe_index +
3095 current->pid % map->sub_stripes);
3096 }
Chris Mason8790d502008-04-03 16:29:03 -04003097 } else {
3098 /*
3099 * after this do_div call, stripe_nr is the number of stripes
3100 * on this device we have to walk to find the data, and
3101 * stripe_index is the number of our device in the stripe array
3102 */
3103 stripe_index = do_div(stripe_nr, map->num_stripes);
3104 }
Chris Mason593060d2008-03-25 16:50:33 -04003105 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003106
Li Dongyangfce3bb92011-03-24 10:24:26 +00003107 if (rw & REQ_DISCARD) {
3108 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003109 multi->stripes[i].physical =
3110 map->stripes[stripe_index].physical +
3111 stripe_offset + stripe_nr * map->stripe_len;
3112 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003113
3114 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3115 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003116 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003117 int j;
3118
Chris Masond9d04872011-03-27 21:23:21 -04003119 div_u64_rem(stripe_nr_end - 1,
3120 map->num_stripes,
3121 &last_stripe);
3122
Li Dongyangfce3bb92011-03-24 10:24:26 +00003123 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003124 u32 test;
3125
3126 div_u64_rem(stripe_nr_end - 1 - j,
3127 map->num_stripes, &test);
3128 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003129 break;
3130 }
3131 stripes = stripe_nr_end - 1 - j;
3132 do_div(stripes, map->num_stripes);
3133 multi->stripes[i].length = map->stripe_len *
3134 (stripes - stripe_nr + 1);
3135
3136 if (i == 0) {
3137 multi->stripes[i].length -=
3138 stripe_offset;
3139 stripe_offset = 0;
3140 }
3141 if (stripe_index == last_stripe)
3142 multi->stripes[i].length -=
3143 stripe_end_offset;
3144 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3145 u64 stripes;
3146 int j;
3147 int factor = map->num_stripes /
3148 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003149 u32 last_stripe = 0;
3150
3151 div_u64_rem(stripe_nr_end - 1,
3152 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003153 last_stripe *= map->sub_stripes;
3154
3155 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003156 u32 test;
3157
3158 div_u64_rem(stripe_nr_end - 1 - j,
3159 factor, &test);
3160
3161 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003162 stripe_index / map->sub_stripes)
3163 break;
3164 }
3165 stripes = stripe_nr_end - 1 - j;
3166 do_div(stripes, factor);
3167 multi->stripes[i].length = map->stripe_len *
3168 (stripes - stripe_nr + 1);
3169
3170 if (i < map->sub_stripes) {
3171 multi->stripes[i].length -=
3172 stripe_offset;
3173 if (i == map->sub_stripes - 1)
3174 stripe_offset = 0;
3175 }
3176 if (stripe_index >= last_stripe &&
3177 stripe_index <= (last_stripe +
3178 map->sub_stripes - 1)) {
3179 multi->stripes[i].length -=
3180 stripe_end_offset;
3181 }
3182 } else
3183 multi->stripes[i].length = *length;
3184
3185 stripe_index++;
3186 if (stripe_index == map->num_stripes) {
3187 /* This could only happen for RAID0/10 */
3188 stripe_index = 0;
3189 stripe_nr++;
3190 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003191 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003192 } else {
3193 for (i = 0; i < num_stripes; i++) {
3194 if (unplug_page) {
3195 struct btrfs_device *device;
3196 struct backing_dev_info *bdi;
3197
3198 device = map->stripes[stripe_index].dev;
3199 if (device->bdev) {
3200 bdi = blk_get_backing_dev_info(device->
3201 bdev);
3202 if (bdi->unplug_io_fn)
3203 bdi->unplug_io_fn(bdi,
3204 unplug_page);
3205 }
3206 } else {
3207 multi->stripes[i].physical =
3208 map->stripes[stripe_index].physical +
3209 stripe_offset +
3210 stripe_nr * map->stripe_len;
3211 multi->stripes[i].dev =
3212 map->stripes[stripe_index].dev;
3213 }
3214 stripe_index++;
3215 }
Chris Mason593060d2008-03-25 16:50:33 -04003216 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003217 if (multi_ret) {
3218 *multi_ret = multi;
3219 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003220 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003221 }
Chris Masoncea9e442008-04-09 16:28:12 -04003222out:
Chris Mason0b86a832008-03-24 15:01:56 -04003223 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003224 return 0;
3225}
3226
Chris Masonf2d8d742008-04-21 10:03:05 -04003227int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3228 u64 logical, u64 *length,
3229 struct btrfs_multi_bio **multi_ret, int mirror_num)
3230{
3231 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3232 mirror_num, NULL);
3233}
3234
Yan Zhenga512bbf2008-12-08 16:46:26 -05003235int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3236 u64 chunk_start, u64 physical, u64 devid,
3237 u64 **logical, int *naddrs, int *stripe_len)
3238{
3239 struct extent_map_tree *em_tree = &map_tree->map_tree;
3240 struct extent_map *em;
3241 struct map_lookup *map;
3242 u64 *buf;
3243 u64 bytenr;
3244 u64 length;
3245 u64 stripe_nr;
3246 int i, j, nr = 0;
3247
Chris Mason890871b2009-09-02 16:24:52 -04003248 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003249 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003250 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003251
3252 BUG_ON(!em || em->start != chunk_start);
3253 map = (struct map_lookup *)em->bdev;
3254
3255 length = em->len;
3256 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3257 do_div(length, map->num_stripes / map->sub_stripes);
3258 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3259 do_div(length, map->num_stripes);
3260
3261 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3262 BUG_ON(!buf);
3263
3264 for (i = 0; i < map->num_stripes; i++) {
3265 if (devid && map->stripes[i].dev->devid != devid)
3266 continue;
3267 if (map->stripes[i].physical > physical ||
3268 map->stripes[i].physical + length <= physical)
3269 continue;
3270
3271 stripe_nr = physical - map->stripes[i].physical;
3272 do_div(stripe_nr, map->stripe_len);
3273
3274 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3275 stripe_nr = stripe_nr * map->num_stripes + i;
3276 do_div(stripe_nr, map->sub_stripes);
3277 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3278 stripe_nr = stripe_nr * map->num_stripes + i;
3279 }
3280 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003281 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003282 for (j = 0; j < nr; j++) {
3283 if (buf[j] == bytenr)
3284 break;
3285 }
Chris Mason934d3752008-12-08 16:43:10 -05003286 if (j == nr) {
3287 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003288 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003289 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003290 }
3291
Yan Zhenga512bbf2008-12-08 16:46:26 -05003292 *logical = buf;
3293 *naddrs = nr;
3294 *stripe_len = map->stripe_len;
3295
3296 free_extent_map(em);
3297 return 0;
3298}
3299
Chris Masonf2d8d742008-04-21 10:03:05 -04003300int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
3301 u64 logical, struct page *page)
3302{
3303 u64 length = PAGE_CACHE_SIZE;
3304 return __btrfs_map_block(map_tree, READ, logical, &length,
3305 NULL, 0, page);
3306}
3307
Chris Mason8790d502008-04-03 16:29:03 -04003308static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003309{
Chris Masoncea9e442008-04-09 16:28:12 -04003310 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003311 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003312
Chris Mason8790d502008-04-03 16:29:03 -04003313 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003314 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003315
Chris Mason7d2b4da2008-08-05 10:13:57 -04003316 if (bio == multi->orig_bio)
3317 is_orig_bio = 1;
3318
Chris Masoncea9e442008-04-09 16:28:12 -04003319 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003320 if (!is_orig_bio) {
3321 bio_put(bio);
3322 bio = multi->orig_bio;
3323 }
Chris Mason8790d502008-04-03 16:29:03 -04003324 bio->bi_private = multi->private;
3325 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003326 /* only send an error to the higher layers if it is
3327 * beyond the tolerance of the multi-bio
3328 */
Chris Mason1259ab72008-05-12 13:39:03 -04003329 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003330 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003331 } else if (err) {
3332 /*
3333 * this bio is actually up to date, we didn't
3334 * go over the max number of errors
3335 */
3336 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003337 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003338 }
Chris Mason8790d502008-04-03 16:29:03 -04003339 kfree(multi);
3340
3341 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003342 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003343 bio_put(bio);
3344 }
Chris Mason8790d502008-04-03 16:29:03 -04003345}
3346
Chris Mason8b712842008-06-11 16:50:36 -04003347struct async_sched {
3348 struct bio *bio;
3349 int rw;
3350 struct btrfs_fs_info *info;
3351 struct btrfs_work work;
3352};
3353
3354/*
3355 * see run_scheduled_bios for a description of why bios are collected for
3356 * async submit.
3357 *
3358 * This will add one bio to the pending list for a device and make sure
3359 * the work struct is scheduled.
3360 */
Chris Masond3977122009-01-05 21:25:51 -05003361static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003362 struct btrfs_device *device,
3363 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003364{
3365 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003366 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003367
3368 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003369 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003370 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003371 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003372 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003373 return 0;
3374 }
3375
3376 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003377 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003378 * higher layers. Otherwise, the async bio makes it appear we have
3379 * made progress against dirty pages when we've really just put it
3380 * on a queue for later
3381 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003382 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003383 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003384 bio->bi_next = NULL;
3385 bio->bi_rw |= rw;
3386
3387 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003388 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003389 pending_bios = &device->pending_sync_bios;
3390 else
3391 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003392
Chris Masonffbd5172009-04-20 15:50:09 -04003393 if (pending_bios->tail)
3394 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003395
Chris Masonffbd5172009-04-20 15:50:09 -04003396 pending_bios->tail = bio;
3397 if (!pending_bios->head)
3398 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003399 if (device->running_pending)
3400 should_queue = 0;
3401
3402 spin_unlock(&device->io_lock);
3403
3404 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003405 btrfs_queue_worker(&root->fs_info->submit_workers,
3406 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003407 return 0;
3408}
3409
Chris Masonf1885912008-04-09 16:28:12 -04003410int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003411 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003412{
3413 struct btrfs_mapping_tree *map_tree;
3414 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003415 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003416 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003417 u64 length = 0;
3418 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003419 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003420 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003421 int dev_nr = 0;
3422 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003423
Chris Masonf2d8d742008-04-21 10:03:05 -04003424 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003425 map_tree = &root->fs_info->mapping_tree;
3426 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003427
Chris Masonf1885912008-04-09 16:28:12 -04003428 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3429 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003430 BUG_ON(ret);
3431
3432 total_devs = multi->num_stripes;
3433 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003434 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3435 "len %llu\n", (unsigned long long)logical,
3436 (unsigned long long)length,
3437 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003438 BUG();
3439 }
3440 multi->end_io = first_bio->bi_end_io;
3441 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003442 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003443 atomic_set(&multi->stripes_pending, multi->num_stripes);
3444
Chris Masond3977122009-01-05 21:25:51 -05003445 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003446 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003447 if (dev_nr < total_devs - 1) {
3448 bio = bio_clone(first_bio, GFP_NOFS);
3449 BUG_ON(!bio);
3450 } else {
3451 bio = first_bio;
3452 }
3453 bio->bi_private = multi;
3454 bio->bi_end_io = end_bio_multi_stripe;
3455 }
Chris Masoncea9e442008-04-09 16:28:12 -04003456 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3457 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003458 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003459 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003460 if (async_submit)
3461 schedule_bio(root, dev, rw, bio);
3462 else
3463 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003464 } else {
3465 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3466 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003467 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003468 }
Chris Mason8790d502008-04-03 16:29:03 -04003469 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003470 }
Chris Masoncea9e442008-04-09 16:28:12 -04003471 if (total_devs == 1)
3472 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003473 return 0;
3474}
3475
Chris Masona4437552008-04-18 10:29:38 -04003476struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003477 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003478{
Yan Zheng2b820322008-11-17 21:11:30 -05003479 struct btrfs_device *device;
3480 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003481
Yan Zheng2b820322008-11-17 21:11:30 -05003482 cur_devices = root->fs_info->fs_devices;
3483 while (cur_devices) {
3484 if (!fsid ||
3485 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3486 device = __find_device(&cur_devices->devices,
3487 devid, uuid);
3488 if (device)
3489 return device;
3490 }
3491 cur_devices = cur_devices->seed;
3492 }
3493 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003494}
3495
Chris Masondfe25022008-05-13 13:46:40 -04003496static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3497 u64 devid, u8 *dev_uuid)
3498{
3499 struct btrfs_device *device;
3500 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3501
3502 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003503 if (!device)
3504 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003505 list_add(&device->dev_list,
3506 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003507 device->dev_root = root->fs_info->dev_root;
3508 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003509 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003510 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003511 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003512 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003513 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003514 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003515 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003516 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3517 return device;
3518}
3519
Chris Mason0b86a832008-03-24 15:01:56 -04003520static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3521 struct extent_buffer *leaf,
3522 struct btrfs_chunk *chunk)
3523{
3524 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3525 struct map_lookup *map;
3526 struct extent_map *em;
3527 u64 logical;
3528 u64 length;
3529 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003530 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003531 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003532 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003533 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003534
Chris Masone17cade2008-04-15 15:41:47 -04003535 logical = key->offset;
3536 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003537
Chris Mason890871b2009-09-02 16:24:52 -04003538 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003539 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003540 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003541
3542 /* already mapped? */
3543 if (em && em->start <= logical && em->start + em->len > logical) {
3544 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003545 return 0;
3546 } else if (em) {
3547 free_extent_map(em);
3548 }
Chris Mason0b86a832008-03-24 15:01:56 -04003549
Chris Mason0b86a832008-03-24 15:01:56 -04003550 em = alloc_extent_map(GFP_NOFS);
3551 if (!em)
3552 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003553 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3554 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003555 if (!map) {
3556 free_extent_map(em);
3557 return -ENOMEM;
3558 }
3559
3560 em->bdev = (struct block_device *)map;
3561 em->start = logical;
3562 em->len = length;
3563 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003564 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003565
Chris Mason593060d2008-03-25 16:50:33 -04003566 map->num_stripes = num_stripes;
3567 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3568 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3569 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3570 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3571 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003572 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003573 for (i = 0; i < num_stripes; i++) {
3574 map->stripes[i].physical =
3575 btrfs_stripe_offset_nr(leaf, chunk, i);
3576 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003577 read_extent_buffer(leaf, uuid, (unsigned long)
3578 btrfs_stripe_dev_uuid_nr(chunk, i),
3579 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003580 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3581 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003582 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003583 kfree(map);
3584 free_extent_map(em);
3585 return -EIO;
3586 }
Chris Masondfe25022008-05-13 13:46:40 -04003587 if (!map->stripes[i].dev) {
3588 map->stripes[i].dev =
3589 add_missing_dev(root, devid, uuid);
3590 if (!map->stripes[i].dev) {
3591 kfree(map);
3592 free_extent_map(em);
3593 return -EIO;
3594 }
3595 }
3596 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003597 }
3598
Chris Mason890871b2009-09-02 16:24:52 -04003599 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003600 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003601 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003602 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003603 free_extent_map(em);
3604
3605 return 0;
3606}
3607
3608static int fill_device_from_item(struct extent_buffer *leaf,
3609 struct btrfs_dev_item *dev_item,
3610 struct btrfs_device *device)
3611{
3612 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003613
3614 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003615 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3616 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003617 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3618 device->type = btrfs_device_type(leaf, dev_item);
3619 device->io_align = btrfs_device_io_align(leaf, dev_item);
3620 device->io_width = btrfs_device_io_width(leaf, dev_item);
3621 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003622
3623 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003624 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003625
Chris Mason0b86a832008-03-24 15:01:56 -04003626 return 0;
3627}
3628
Yan Zheng2b820322008-11-17 21:11:30 -05003629static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3630{
3631 struct btrfs_fs_devices *fs_devices;
3632 int ret;
3633
3634 mutex_lock(&uuid_mutex);
3635
3636 fs_devices = root->fs_info->fs_devices->seed;
3637 while (fs_devices) {
3638 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3639 ret = 0;
3640 goto out;
3641 }
3642 fs_devices = fs_devices->seed;
3643 }
3644
3645 fs_devices = find_fsid(fsid);
3646 if (!fs_devices) {
3647 ret = -ENOENT;
3648 goto out;
3649 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003650
3651 fs_devices = clone_fs_devices(fs_devices);
3652 if (IS_ERR(fs_devices)) {
3653 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003654 goto out;
3655 }
3656
Christoph Hellwig97288f22008-12-02 06:36:09 -05003657 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003658 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003659 if (ret)
3660 goto out;
3661
3662 if (!fs_devices->seeding) {
3663 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003664 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003665 ret = -EINVAL;
3666 goto out;
3667 }
3668
3669 fs_devices->seed = root->fs_info->fs_devices->seed;
3670 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003671out:
3672 mutex_unlock(&uuid_mutex);
3673 return ret;
3674}
3675
Chris Mason0d81ba52008-03-24 15:02:07 -04003676static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003677 struct extent_buffer *leaf,
3678 struct btrfs_dev_item *dev_item)
3679{
3680 struct btrfs_device *device;
3681 u64 devid;
3682 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003683 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003684 u8 dev_uuid[BTRFS_UUID_SIZE];
3685
Chris Mason0b86a832008-03-24 15:01:56 -04003686 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003687 read_extent_buffer(leaf, dev_uuid,
3688 (unsigned long)btrfs_device_uuid(dev_item),
3689 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003690 read_extent_buffer(leaf, fs_uuid,
3691 (unsigned long)btrfs_device_fsid(dev_item),
3692 BTRFS_UUID_SIZE);
3693
3694 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3695 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003696 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003697 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003698 }
3699
3700 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3701 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003702 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003703 return -EIO;
3704
3705 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003706 printk(KERN_WARNING "warning devid %llu missing\n",
3707 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003708 device = add_missing_dev(root, devid, dev_uuid);
3709 if (!device)
3710 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003711 } else if (!device->missing) {
3712 /*
3713 * this happens when a device that was properly setup
3714 * in the device info lists suddenly goes bad.
3715 * device->bdev is NULL, and so we have to set
3716 * device->missing to one here
3717 */
3718 root->fs_info->fs_devices->missing_devices++;
3719 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003720 }
3721 }
3722
3723 if (device->fs_devices != root->fs_info->fs_devices) {
3724 BUG_ON(device->writeable);
3725 if (device->generation !=
3726 btrfs_device_generation(leaf, dev_item))
3727 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003728 }
Chris Mason0b86a832008-03-24 15:01:56 -04003729
3730 fill_device_from_item(leaf, dev_item, device);
3731 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003732 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003733 if (device->writeable)
3734 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003735 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003736 return ret;
3737}
3738
Chris Mason0d81ba52008-03-24 15:02:07 -04003739int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3740{
3741 struct btrfs_dev_item *dev_item;
3742
3743 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3744 dev_item);
3745 return read_one_dev(root, buf, dev_item);
3746}
3747
Yan Zhenge4404d62008-12-12 10:03:26 -05003748int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003749{
3750 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003751 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003752 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003753 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003754 u8 *ptr;
3755 unsigned long sb_ptr;
3756 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003757 u32 num_stripes;
3758 u32 array_size;
3759 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003760 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003761 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003762
Yan Zhenge4404d62008-12-12 10:03:26 -05003763 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003764 BTRFS_SUPER_INFO_SIZE);
3765 if (!sb)
3766 return -ENOMEM;
3767 btrfs_set_buffer_uptodate(sb);
Chris Mason4008c042009-02-12 14:09:45 -05003768 btrfs_set_buffer_lockdep_class(sb, 0);
3769
Chris Masona061fc82008-05-07 11:43:44 -04003770 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003771 array_size = btrfs_super_sys_array_size(super_copy);
3772
Chris Mason0b86a832008-03-24 15:01:56 -04003773 ptr = super_copy->sys_chunk_array;
3774 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3775 cur = 0;
3776
3777 while (cur < array_size) {
3778 disk_key = (struct btrfs_disk_key *)ptr;
3779 btrfs_disk_key_to_cpu(&key, disk_key);
3780
Chris Masona061fc82008-05-07 11:43:44 -04003781 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003782 sb_ptr += len;
3783 cur += len;
3784
Chris Mason0d81ba52008-03-24 15:02:07 -04003785 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003786 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003787 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003788 if (ret)
3789 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003790 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3791 len = btrfs_chunk_item_size(num_stripes);
3792 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003793 ret = -EIO;
3794 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003795 }
3796 ptr += len;
3797 sb_ptr += len;
3798 cur += len;
3799 }
Chris Masona061fc82008-05-07 11:43:44 -04003800 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003801 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003802}
3803
3804int btrfs_read_chunk_tree(struct btrfs_root *root)
3805{
3806 struct btrfs_path *path;
3807 struct extent_buffer *leaf;
3808 struct btrfs_key key;
3809 struct btrfs_key found_key;
3810 int ret;
3811 int slot;
3812
3813 root = root->fs_info->chunk_root;
3814
3815 path = btrfs_alloc_path();
3816 if (!path)
3817 return -ENOMEM;
3818
3819 /* first we search for all of the device items, and then we
3820 * read in all of the chunk items. This way we can create chunk
3821 * mappings that reference all of the devices that are afound
3822 */
3823 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3824 key.offset = 0;
3825 key.type = 0;
3826again:
3827 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003828 if (ret < 0)
3829 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003830 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003831 leaf = path->nodes[0];
3832 slot = path->slots[0];
3833 if (slot >= btrfs_header_nritems(leaf)) {
3834 ret = btrfs_next_leaf(root, path);
3835 if (ret == 0)
3836 continue;
3837 if (ret < 0)
3838 goto error;
3839 break;
3840 }
3841 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3842 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3843 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3844 break;
3845 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3846 struct btrfs_dev_item *dev_item;
3847 dev_item = btrfs_item_ptr(leaf, slot,
3848 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003849 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003850 if (ret)
3851 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003852 }
3853 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3854 struct btrfs_chunk *chunk;
3855 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3856 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003857 if (ret)
3858 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003859 }
3860 path->slots[0]++;
3861 }
3862 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3863 key.objectid = 0;
3864 btrfs_release_path(root, path);
3865 goto again;
3866 }
Chris Mason0b86a832008-03-24 15:01:56 -04003867 ret = 0;
3868error:
Yan Zheng2b820322008-11-17 21:11:30 -05003869 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003870 return ret;
3871}