blob: 1dccce5bc93d0548c90a77f358dfb6842f95725e [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 Mason8a4b83c2008-03-24 15:02:07 -040041static DEFINE_MUTEX(uuid_mutex);
42static LIST_HEAD(fs_uuids);
43
Chris Mason7d9eb122008-07-08 14:19:17 -040044static void lock_chunks(struct btrfs_root *root)
45{
Chris Mason7d9eb122008-07-08 14:19:17 -040046 mutex_lock(&root->fs_info->chunk_mutex);
47}
48
49static void unlock_chunks(struct btrfs_root *root)
50{
Chris Mason7d9eb122008-07-08 14:19:17 -040051 mutex_unlock(&root->fs_info->chunk_mutex);
52}
53
Yan Zhenge4404d62008-12-12 10:03:26 -050054static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
55{
56 struct btrfs_device *device;
57 WARN_ON(fs_devices->opened);
58 while (!list_empty(&fs_devices->devices)) {
59 device = list_entry(fs_devices->devices.next,
60 struct btrfs_device, dev_list);
61 list_del(&device->dev_list);
62 kfree(device->name);
63 kfree(device);
64 }
65 kfree(fs_devices);
66}
67
Chris Mason8a4b83c2008-03-24 15:02:07 -040068int btrfs_cleanup_fs_uuids(void)
69{
70 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040071
Yan Zheng2b820322008-11-17 21:11:30 -050072 while (!list_empty(&fs_uuids)) {
73 fs_devices = list_entry(fs_uuids.next,
74 struct btrfs_fs_devices, list);
75 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050076 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040077 }
78 return 0;
79}
80
Chris Masona1b32a52008-09-05 16:09:51 -040081static noinline struct btrfs_device *__find_device(struct list_head *head,
82 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040083{
84 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -040085
Qinghuang Fengc6e30872009-01-21 10:59:08 -050086 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -040087 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -040088 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -040089 return dev;
Chris Masona4437552008-04-18 10:29:38 -040090 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040091 }
92 return NULL;
93}
94
Chris Masona1b32a52008-09-05 16:09:51 -040095static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -040096{
Chris Mason8a4b83c2008-03-24 15:02:07 -040097 struct btrfs_fs_devices *fs_devices;
98
Qinghuang Fengc6e30872009-01-21 10:59:08 -050099 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400100 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
101 return fs_devices;
102 }
103 return NULL;
104}
105
Chris Masonffbd5172009-04-20 15:50:09 -0400106static void requeue_list(struct btrfs_pending_bios *pending_bios,
107 struct bio *head, struct bio *tail)
108{
109
110 struct bio *old_head;
111
112 old_head = pending_bios->head;
113 pending_bios->head = head;
114 if (pending_bios->tail)
115 tail->bi_next = old_head;
116 else
117 pending_bios->tail = tail;
118}
119
Chris Mason8b712842008-06-11 16:50:36 -0400120/*
121 * we try to collect pending bios for a device so we don't get a large
122 * number of procs sending bios down to the same device. This greatly
123 * improves the schedulers ability to collect and merge the bios.
124 *
125 * But, it also turns into a long list of bios to process and that is sure
126 * to eventually make the worker thread block. The solution here is to
127 * make some progress and then put this work struct back at the end of
128 * the list if the block device is congested. This way, multiple devices
129 * can make progress from a single worker thread.
130 */
Chris Masond3977122009-01-05 21:25:51 -0500131static noinline int run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400132{
133 struct bio *pending;
134 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400135 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400136 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400137 struct bio *tail;
138 struct bio *cur;
139 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400140 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400141 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400142 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400143 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400144 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000145 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400146 struct blk_plug plug;
147
148 /*
149 * this function runs all the bios we've collected for
150 * a particular device. We don't want to wander off to
151 * another device without first sending all of these down.
152 * So, setup a plug here and finish it off before we return
153 */
154 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400155
Chris Masonbedf7622009-04-03 10:32:58 -0400156 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400157 fs_info = device->dev_root->fs_info;
158 limit = btrfs_async_submit_limit(fs_info);
159 limit = limit * 2 / 3;
160
Chris Mason8b712842008-06-11 16:50:36 -0400161loop:
162 spin_lock(&device->io_lock);
163
Chris Masona6837052009-02-04 09:19:41 -0500164loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400165 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400166
Chris Mason8b712842008-06-11 16:50:36 -0400167 /* take all the bios off the list at once and process them
168 * later on (without the lock held). But, remember the
169 * tail and other pointers so the bios can be properly reinserted
170 * into the list if we hit congestion
171 */
Chris Masond84275c2009-06-09 15:39:08 -0400172 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400173 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400174 force_reg = 1;
175 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400176 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400177 force_reg = 0;
178 }
Chris Masonffbd5172009-04-20 15:50:09 -0400179
180 pending = pending_bios->head;
181 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400182 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400183
184 /*
185 * if pending was null this time around, no bios need processing
186 * at all and we can stop. Otherwise it'll loop back up again
187 * and do an additional check so no bios are missed.
188 *
189 * device->running_pending is used to synchronize with the
190 * schedule_bio code.
191 */
Chris Masonffbd5172009-04-20 15:50:09 -0400192 if (device->pending_sync_bios.head == NULL &&
193 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400194 again = 0;
195 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400196 } else {
197 again = 1;
198 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400199 }
Chris Masonffbd5172009-04-20 15:50:09 -0400200
201 pending_bios->head = NULL;
202 pending_bios->tail = NULL;
203
Chris Mason8b712842008-06-11 16:50:36 -0400204 spin_unlock(&device->io_lock);
205
Chris Masond3977122009-01-05 21:25:51 -0500206 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400207
208 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400209 /* we want to work on both lists, but do more bios on the
210 * sync list than the regular list
211 */
212 if ((num_run > 32 &&
213 pending_bios != &device->pending_sync_bios &&
214 device->pending_sync_bios.head) ||
215 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
216 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400217 spin_lock(&device->io_lock);
218 requeue_list(pending_bios, pending, tail);
219 goto loop_lock;
220 }
221
Chris Mason8b712842008-06-11 16:50:36 -0400222 cur = pending;
223 pending = pending->bi_next;
224 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400225 atomic_dec(&fs_info->nr_async_bios);
226
227 if (atomic_read(&fs_info->nr_async_bios) < limit &&
228 waitqueue_active(&fs_info->async_submit_wait))
229 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400230
231 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400232
Chris Mason2ab1ba62011-08-04 14:28:36 -0400233 /*
234 * if we're doing the sync list, record that our
235 * plug has some sync requests on it
236 *
237 * If we're doing the regular list and there are
238 * sync requests sitting around, unplug before
239 * we add more
240 */
241 if (pending_bios == &device->pending_sync_bios) {
242 sync_pending = 1;
243 } else if (sync_pending) {
244 blk_finish_plug(&plug);
245 blk_start_plug(&plug);
246 sync_pending = 0;
247 }
248
Chris Mason5ff7ba32010-03-15 10:21:30 -0400249 submit_bio(cur->bi_rw, cur);
250 num_run++;
251 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100252 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400253 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400254
255 /*
256 * we made progress, there is more work to do and the bdi
257 * is now congested. Back off and let other work structs
258 * run instead
259 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400260 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500261 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400262 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400263
Chris Masonb765ead2009-04-03 10:27:10 -0400264 ioc = current->io_context;
265
266 /*
267 * the main goal here is that we don't want to
268 * block if we're going to be able to submit
269 * more requests without blocking.
270 *
271 * This code does two great things, it pokes into
272 * the elevator code from a filesystem _and_
273 * it makes assumptions about how batching works.
274 */
275 if (ioc && ioc->nr_batch_requests > 0 &&
276 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
277 (last_waited == 0 ||
278 ioc->last_waited == last_waited)) {
279 /*
280 * we want to go through our batch of
281 * requests and stop. So, we copy out
282 * the ioc->last_waited time and test
283 * against it before looping
284 */
285 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100286 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400287 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400288 continue;
289 }
Chris Mason8b712842008-06-11 16:50:36 -0400290 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400291 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500292 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400293
294 spin_unlock(&device->io_lock);
295 btrfs_requeue_work(&device->work);
296 goto done;
297 }
298 }
Chris Masonffbd5172009-04-20 15:50:09 -0400299
Chris Mason51684082010-03-10 15:33:32 -0500300 cond_resched();
301 if (again)
302 goto loop;
303
304 spin_lock(&device->io_lock);
305 if (device->pending_bios.head || device->pending_sync_bios.head)
306 goto loop_lock;
307 spin_unlock(&device->io_lock);
308
Chris Mason8b712842008-06-11 16:50:36 -0400309done:
Chris Mason211588a2011-04-19 20:12:40 -0400310 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400311 return 0;
312}
313
Christoph Hellwigb2950862008-12-02 09:54:17 -0500314static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400315{
316 struct btrfs_device *device;
317
318 device = container_of(work, struct btrfs_device, work);
319 run_scheduled_bios(device);
320}
321
Chris Masona1b32a52008-09-05 16:09:51 -0400322static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400323 struct btrfs_super_block *disk_super,
324 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
325{
326 struct btrfs_device *device;
327 struct btrfs_fs_devices *fs_devices;
328 u64 found_transid = btrfs_super_generation(disk_super);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000329 char *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400330
331 fs_devices = find_fsid(disk_super->fsid);
332 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400333 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400334 if (!fs_devices)
335 return -ENOMEM;
336 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400337 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400338 list_add(&fs_devices->list, &fs_uuids);
339 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
340 fs_devices->latest_devid = devid;
341 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400342 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400343 device = NULL;
344 } else {
Chris Masona4437552008-04-18 10:29:38 -0400345 device = __find_device(&fs_devices->devices, devid,
346 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400347 }
348 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500349 if (fs_devices->opened)
350 return -EBUSY;
351
Chris Mason8a4b83c2008-03-24 15:02:07 -0400352 device = kzalloc(sizeof(*device), GFP_NOFS);
353 if (!device) {
354 /* we can safely leave the fs_devices entry around */
355 return -ENOMEM;
356 }
357 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -0400358 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400359 memcpy(device->uuid, disk_super->dev_item.uuid,
360 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400361 spin_lock_init(&device->io_lock);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400362 device->name = kstrdup(path, GFP_NOFS);
363 if (!device->name) {
364 kfree(device);
365 return -ENOMEM;
366 }
Yan Zheng2b820322008-11-17 21:11:30 -0500367 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400368
Arne Jansen90519d62011-05-23 14:30:00 +0200369 /* init readahead state */
370 spin_lock_init(&device->reada_lock);
371 device->reada_curr_zone = NULL;
372 atomic_set(&device->reada_in_flight, 0);
373 device->reada_next = 0;
374 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
375 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
376
Chris Masone5e9a522009-06-10 15:17:02 -0400377 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000378 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400379 mutex_unlock(&fs_devices->device_list_mutex);
380
Yan Zheng2b820322008-11-17 21:11:30 -0500381 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400382 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -0500383 } else if (!device->name || strcmp(device->name, path)) {
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000384 name = kstrdup(path, GFP_NOFS);
385 if (!name)
386 return -ENOMEM;
387 kfree(device->name);
388 device->name = name;
Chris Masoncd02dca2010-12-13 14:56:23 -0500389 if (device->missing) {
390 fs_devices->missing_devices--;
391 device->missing = 0;
392 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400393 }
394
395 if (found_transid > fs_devices->latest_trans) {
396 fs_devices->latest_devid = devid;
397 fs_devices->latest_trans = found_transid;
398 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400399 *fs_devices_ret = fs_devices;
400 return 0;
401}
402
Yan Zhenge4404d62008-12-12 10:03:26 -0500403static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
404{
405 struct btrfs_fs_devices *fs_devices;
406 struct btrfs_device *device;
407 struct btrfs_device *orig_dev;
408
409 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
410 if (!fs_devices)
411 return ERR_PTR(-ENOMEM);
412
413 INIT_LIST_HEAD(&fs_devices->devices);
414 INIT_LIST_HEAD(&fs_devices->alloc_list);
415 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400416 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500417 fs_devices->latest_devid = orig->latest_devid;
418 fs_devices->latest_trans = orig->latest_trans;
419 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
420
Xiao Guangrong46224702011-04-20 10:08:47 +0000421 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500422 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
423 device = kzalloc(sizeof(*device), GFP_NOFS);
424 if (!device)
425 goto error;
426
427 device->name = kstrdup(orig_dev->name, GFP_NOFS);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400428 if (!device->name) {
429 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500430 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400431 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500432
433 device->devid = orig_dev->devid;
434 device->work.func = pending_bios_fn;
435 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500436 spin_lock_init(&device->io_lock);
437 INIT_LIST_HEAD(&device->dev_list);
438 INIT_LIST_HEAD(&device->dev_alloc_list);
439
440 list_add(&device->dev_list, &fs_devices->devices);
441 device->fs_devices = fs_devices;
442 fs_devices->num_devices++;
443 }
444 return fs_devices;
445error:
446 free_fs_devices(fs_devices);
447 return ERR_PTR(-ENOMEM);
448}
449
Chris Masondfe25022008-05-13 13:46:40 -0400450int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
451{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500452 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400453
454 mutex_lock(&uuid_mutex);
455again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000456 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500457 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Yan Zheng2b820322008-11-17 21:11:30 -0500458 if (device->in_fs_metadata)
459 continue;
460
461 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100462 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500463 device->bdev = NULL;
464 fs_devices->open_devices--;
465 }
466 if (device->writeable) {
467 list_del_init(&device->dev_alloc_list);
468 device->writeable = 0;
469 fs_devices->rw_devices--;
470 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500471 list_del_init(&device->dev_list);
472 fs_devices->num_devices--;
473 kfree(device->name);
474 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400475 }
Yan Zheng2b820322008-11-17 21:11:30 -0500476
477 if (fs_devices->seed) {
478 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500479 goto again;
480 }
481
Chris Masondfe25022008-05-13 13:46:40 -0400482 mutex_unlock(&uuid_mutex);
483 return 0;
484}
Chris Masona0af4692008-05-13 16:03:06 -0400485
Xiao Guangrong1f781602011-04-20 10:09:16 +0000486static void __free_device(struct work_struct *work)
487{
488 struct btrfs_device *device;
489
490 device = container_of(work, struct btrfs_device, rcu_work);
491
492 if (device->bdev)
493 blkdev_put(device->bdev, device->mode);
494
495 kfree(device->name);
496 kfree(device);
497}
498
499static void free_device(struct rcu_head *head)
500{
501 struct btrfs_device *device;
502
503 device = container_of(head, struct btrfs_device, rcu);
504
505 INIT_WORK(&device->rcu_work, __free_device);
506 schedule_work(&device->rcu_work);
507}
508
Yan Zheng2b820322008-11-17 21:11:30 -0500509static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400510{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400511 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500512
Yan Zheng2b820322008-11-17 21:11:30 -0500513 if (--fs_devices->opened > 0)
514 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400515
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000516 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500517 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000518 struct btrfs_device *new_device;
519
520 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400521 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000522
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
Josef Bacikd5e20032011-08-04 14:52:27 +0000528 if (device->can_discard)
529 fs_devices->num_can_discard--;
530
Xiao Guangrong1f781602011-04-20 10:09:16 +0000531 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
532 BUG_ON(!new_device);
533 memcpy(new_device, device, sizeof(*new_device));
534 new_device->name = kstrdup(device->name, GFP_NOFS);
Arne Jansen5f3f3022011-05-30 08:36:16 +0000535 BUG_ON(device->name && !new_device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000536 new_device->bdev = NULL;
537 new_device->writeable = 0;
538 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000539 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000540 list_replace_rcu(&device->dev_list, &new_device->dev_list);
541
542 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400543 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000544 mutex_unlock(&fs_devices->device_list_mutex);
545
Yan Zhenge4404d62008-12-12 10:03:26 -0500546 WARN_ON(fs_devices->open_devices);
547 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500548 fs_devices->opened = 0;
549 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500550
Chris Mason8a4b83c2008-03-24 15:02:07 -0400551 return 0;
552}
553
Yan Zheng2b820322008-11-17 21:11:30 -0500554int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
555{
Yan Zhenge4404d62008-12-12 10:03:26 -0500556 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500557 int ret;
558
559 mutex_lock(&uuid_mutex);
560 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500561 if (!fs_devices->opened) {
562 seed_devices = fs_devices->seed;
563 fs_devices->seed = NULL;
564 }
Yan Zheng2b820322008-11-17 21:11:30 -0500565 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500566
567 while (seed_devices) {
568 fs_devices = seed_devices;
569 seed_devices = fs_devices->seed;
570 __btrfs_close_devices(fs_devices);
571 free_fs_devices(fs_devices);
572 }
Yan Zheng2b820322008-11-17 21:11:30 -0500573 return ret;
574}
575
Yan Zhenge4404d62008-12-12 10:03:26 -0500576static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
577 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400578{
Josef Bacikd5e20032011-08-04 14:52:27 +0000579 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400580 struct block_device *bdev;
581 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400582 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400583 struct block_device *latest_bdev = NULL;
584 struct buffer_head *bh;
585 struct btrfs_super_block *disk_super;
586 u64 latest_devid = 0;
587 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400588 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500589 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400590 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400591
Tejun Heod4d77622010-11-13 11:55:18 +0100592 flags |= FMODE_EXCL;
593
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500594 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400595 if (device->bdev)
596 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400597 if (!device->name)
598 continue;
599
Tejun Heod4d77622010-11-13 11:55:18 +0100600 bdev = blkdev_get_by_path(device->name, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400601 if (IS_ERR(bdev)) {
Chris Masond3977122009-01-05 21:25:51 -0500602 printk(KERN_INFO "open %s failed\n", device->name);
Chris Masona0af4692008-05-13 16:03:06 -0400603 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400604 }
Chris Masona061fc82008-05-07 11:43:44 -0400605 set_blocksize(bdev, 4096);
Chris Masona0af4692008-05-13 16:03:06 -0400606
Yan Zhenga512bbf2008-12-08 16:46:26 -0500607 bh = btrfs_read_dev_super(bdev);
Dave Young20b45072011-01-08 10:09:13 +0000608 if (!bh) {
609 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400610 goto error_close;
Dave Young20b45072011-01-08 10:09:13 +0000611 }
Chris Masona0af4692008-05-13 16:03:06 -0400612
613 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000614 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400615 if (devid != device->devid)
616 goto error_brelse;
617
Yan Zheng2b820322008-11-17 21:11:30 -0500618 if (memcmp(device->uuid, disk_super->dev_item.uuid,
619 BTRFS_UUID_SIZE))
620 goto error_brelse;
621
622 device->generation = btrfs_super_generation(disk_super);
623 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400624 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500625 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400626 latest_bdev = bdev;
627 }
628
Yan Zheng2b820322008-11-17 21:11:30 -0500629 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
630 device->writeable = 0;
631 } else {
632 device->writeable = !bdev_read_only(bdev);
633 seeding = 0;
634 }
635
Josef Bacikd5e20032011-08-04 14:52:27 +0000636 q = bdev_get_queue(bdev);
637 if (blk_queue_discard(q)) {
638 device->can_discard = 1;
639 fs_devices->num_can_discard++;
640 }
641
Chris Mason8a4b83c2008-03-24 15:02:07 -0400642 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400643 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500644 device->mode = flags;
645
Chris Masonc2898112009-06-10 09:51:32 -0400646 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
647 fs_devices->rotating = 1;
648
Chris Masona0af4692008-05-13 16:03:06 -0400649 fs_devices->open_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -0500650 if (device->writeable) {
651 fs_devices->rw_devices++;
652 list_add(&device->dev_alloc_list,
653 &fs_devices->alloc_list);
654 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000655 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400656 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400657
Chris Masona0af4692008-05-13 16:03:06 -0400658error_brelse:
659 brelse(bh);
660error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100661 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400662error:
663 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400664 }
Chris Masona0af4692008-05-13 16:03:06 -0400665 if (fs_devices->open_devices == 0) {
666 ret = -EIO;
667 goto out;
668 }
Yan Zheng2b820322008-11-17 21:11:30 -0500669 fs_devices->seeding = seeding;
670 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400671 fs_devices->latest_bdev = latest_bdev;
672 fs_devices->latest_devid = latest_devid;
673 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500674 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400675out:
Yan Zheng2b820322008-11-17 21:11:30 -0500676 return ret;
677}
678
679int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500680 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500681{
682 int ret;
683
684 mutex_lock(&uuid_mutex);
685 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500686 fs_devices->opened++;
687 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500688 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500689 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500690 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400691 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400692 return ret;
693}
694
Christoph Hellwig97288f22008-12-02 06:36:09 -0500695int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 struct btrfs_fs_devices **fs_devices_ret)
697{
698 struct btrfs_super_block *disk_super;
699 struct block_device *bdev;
700 struct buffer_head *bh;
701 int ret;
702 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400703 u64 transid;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400704
705 mutex_lock(&uuid_mutex);
706
Tejun Heod4d77622010-11-13 11:55:18 +0100707 flags |= FMODE_EXCL;
708 bdev = blkdev_get_by_path(path, flags, holder);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400709
710 if (IS_ERR(bdev)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400711 ret = PTR_ERR(bdev);
712 goto error;
713 }
714
715 ret = set_blocksize(bdev, 4096);
716 if (ret)
717 goto error_close;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500718 bh = btrfs_read_dev_super(bdev);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400719 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +0000720 ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400721 goto error_close;
722 }
723 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000724 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400725 transid = btrfs_super_generation(disk_super);
Chris Mason7ae9c092008-04-18 10:29:49 -0400726 if (disk_super->label[0])
Chris Masond3977122009-01-05 21:25:51 -0500727 printk(KERN_INFO "device label %s ", disk_super->label);
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200728 else
729 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Roland Dreier119e10c2009-01-21 10:49:16 -0500730 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500731 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400732 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
733
Chris Mason8a4b83c2008-03-24 15:02:07 -0400734 brelse(bh);
735error_close:
Tejun Heod4d77622010-11-13 11:55:18 +0100736 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400737error:
738 mutex_unlock(&uuid_mutex);
739 return ret;
740}
Chris Mason0b86a832008-03-24 15:01:56 -0400741
Miao Xie6d07bce2011-01-05 10:07:31 +0000742/* helper to account the used device space in the range */
743int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
744 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400745{
746 struct btrfs_key key;
747 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000748 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500749 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000750 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400751 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000752 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400753 struct extent_buffer *l;
754
Miao Xie6d07bce2011-01-05 10:07:31 +0000755 *length = 0;
756
757 if (start >= device->total_bytes)
758 return 0;
759
Yan Zheng2b820322008-11-17 21:11:30 -0500760 path = btrfs_alloc_path();
761 if (!path)
762 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400763 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400764
Chris Mason0b86a832008-03-24 15:01:56 -0400765 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000766 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400767 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000768
769 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400770 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000771 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400772 if (ret > 0) {
773 ret = btrfs_previous_item(root, path, key.objectid, key.type);
774 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000775 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400776 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000777
Chris Mason0b86a832008-03-24 15:01:56 -0400778 while (1) {
779 l = path->nodes[0];
780 slot = path->slots[0];
781 if (slot >= btrfs_header_nritems(l)) {
782 ret = btrfs_next_leaf(root, path);
783 if (ret == 0)
784 continue;
785 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000786 goto out;
787
788 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400789 }
790 btrfs_item_key_to_cpu(l, &key, slot);
791
792 if (key.objectid < device->devid)
793 goto next;
794
795 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000796 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400797
Chris Masond3977122009-01-05 21:25:51 -0500798 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400799 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400800
Chris Mason0b86a832008-03-24 15:01:56 -0400801 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000802 extent_end = key.offset + btrfs_dev_extent_length(l,
803 dev_extent);
804 if (key.offset <= start && extent_end > end) {
805 *length = end - start + 1;
806 break;
807 } else if (key.offset <= start && extent_end > start)
808 *length += extent_end - start;
809 else if (key.offset > start && extent_end <= end)
810 *length += extent_end - key.offset;
811 else if (key.offset > start && key.offset <= end) {
812 *length += end - key.offset + 1;
813 break;
814 } else if (key.offset > end)
815 break;
816
817next:
818 path->slots[0]++;
819 }
820 ret = 0;
821out:
822 btrfs_free_path(path);
823 return ret;
824}
825
Chris Mason0b86a832008-03-24 15:01:56 -0400826/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000827 * find_free_dev_extent - find free space in the specified device
828 * @trans: transaction handler
829 * @device: the device which we search the free space in
830 * @num_bytes: the size of the free space that we need
831 * @start: store the start of the free space.
832 * @len: the size of the free space. that we find, or the size of the max
833 * free space if we don't find suitable free space
834 *
Chris Mason0b86a832008-03-24 15:01:56 -0400835 * this uses a pretty simple search, the expectation is that it is
836 * called very infrequently and that a given device has a small number
837 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000838 *
839 * @start is used to store the start of the free space if we find. But if we
840 * don't find suitable free space, it will be used to store the start position
841 * of the max free space.
842 *
843 * @len is used to store the size of the free space that we find.
844 * But if we don't find suitable free space, it is used to store the size of
845 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400846 */
847int find_free_dev_extent(struct btrfs_trans_handle *trans,
848 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000849 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400850{
851 struct btrfs_key key;
852 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000853 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400854 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000855 u64 hole_size;
856 u64 max_hole_start;
857 u64 max_hole_size;
858 u64 extent_end;
859 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400860 u64 search_end = device->total_bytes;
861 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000862 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400863 struct extent_buffer *l;
864
Chris Mason0b86a832008-03-24 15:01:56 -0400865 /* FIXME use last free of some kind */
866
867 /* we don't want to overwrite the superblock on the drive,
868 * so we make sure to start at an offset of at least 1MB
869 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200870 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400871
Miao Xie7bfc8372011-01-05 10:07:26 +0000872 max_hole_start = search_start;
873 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000874 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000875
876 if (search_start >= search_end) {
877 ret = -ENOSPC;
878 goto error;
879 }
880
881 path = btrfs_alloc_path();
882 if (!path) {
883 ret = -ENOMEM;
884 goto error;
885 }
886 path->reada = 2;
887
Chris Mason0b86a832008-03-24 15:01:56 -0400888 key.objectid = device->devid;
889 key.offset = search_start;
890 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000891
Chris Mason0b86a832008-03-24 15:01:56 -0400892 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
893 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000894 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400895 if (ret > 0) {
896 ret = btrfs_previous_item(root, path, key.objectid, key.type);
897 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000898 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400899 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000900
Chris Mason0b86a832008-03-24 15:01:56 -0400901 while (1) {
902 l = path->nodes[0];
903 slot = path->slots[0];
904 if (slot >= btrfs_header_nritems(l)) {
905 ret = btrfs_next_leaf(root, path);
906 if (ret == 0)
907 continue;
908 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000909 goto out;
910
911 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400912 }
913 btrfs_item_key_to_cpu(l, &key, slot);
914
915 if (key.objectid < device->devid)
916 goto next;
917
918 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +0000919 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400920
Chris Mason0b86a832008-03-24 15:01:56 -0400921 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
922 goto next;
923
Miao Xie7bfc8372011-01-05 10:07:26 +0000924 if (key.offset > search_start) {
925 hole_size = key.offset - search_start;
926
927 if (hole_size > max_hole_size) {
928 max_hole_start = search_start;
929 max_hole_size = hole_size;
930 }
931
932 /*
933 * If this free space is greater than which we need,
934 * it must be the max free space that we have found
935 * until now, so max_hole_start must point to the start
936 * of this free space and the length of this free space
937 * is stored in max_hole_size. Thus, we return
938 * max_hole_start and max_hole_size and go back to the
939 * caller.
940 */
941 if (hole_size >= num_bytes) {
942 ret = 0;
943 goto out;
944 }
945 }
946
Chris Mason0b86a832008-03-24 15:01:56 -0400947 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +0000948 extent_end = key.offset + btrfs_dev_extent_length(l,
949 dev_extent);
950 if (extent_end > search_start)
951 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400952next:
953 path->slots[0]++;
954 cond_resched();
955 }
Chris Mason0b86a832008-03-24 15:01:56 -0400956
liubo38c01b92011-08-02 02:39:03 +0000957 /*
958 * At this point, search_start should be the end of
959 * allocated dev extents, and when shrinking the device,
960 * search_end may be smaller than search_start.
961 */
962 if (search_end > search_start)
963 hole_size = search_end - search_start;
964
Miao Xie7bfc8372011-01-05 10:07:26 +0000965 if (hole_size > max_hole_size) {
966 max_hole_start = search_start;
967 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400968 }
Chris Mason0b86a832008-03-24 15:01:56 -0400969
Miao Xie7bfc8372011-01-05 10:07:26 +0000970 /* See above. */
971 if (hole_size < num_bytes)
972 ret = -ENOSPC;
973 else
974 ret = 0;
975
976out:
Yan Zheng2b820322008-11-17 21:11:30 -0500977 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +0000978error:
979 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +0000980 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +0000981 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -0400982 return ret;
983}
984
Christoph Hellwigb2950862008-12-02 09:54:17 -0500985static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -0400986 struct btrfs_device *device,
987 u64 start)
988{
989 int ret;
990 struct btrfs_path *path;
991 struct btrfs_root *root = device->dev_root;
992 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -0400993 struct btrfs_key found_key;
994 struct extent_buffer *leaf = NULL;
995 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -0400996
997 path = btrfs_alloc_path();
998 if (!path)
999 return -ENOMEM;
1000
1001 key.objectid = device->devid;
1002 key.offset = start;
1003 key.type = BTRFS_DEV_EXTENT_KEY;
1004
1005 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001006 if (ret > 0) {
1007 ret = btrfs_previous_item(root, path, key.objectid,
1008 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001009 if (ret)
1010 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001011 leaf = path->nodes[0];
1012 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1013 extent = btrfs_item_ptr(leaf, path->slots[0],
1014 struct btrfs_dev_extent);
1015 BUG_ON(found_key.offset > start || found_key.offset +
1016 btrfs_dev_extent_length(leaf, extent) < start);
Chris Masona061fc82008-05-07 11:43:44 -04001017 } else if (ret == 0) {
1018 leaf = path->nodes[0];
1019 extent = btrfs_item_ptr(leaf, path->slots[0],
1020 struct btrfs_dev_extent);
1021 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001022 BUG_ON(ret);
1023
Chris Masondfe25022008-05-13 13:46:40 -04001024 if (device->bytes_used > 0)
1025 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
Chris Mason8f18cf12008-04-25 16:53:30 -04001026 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001027
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001028out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001029 btrfs_free_path(path);
1030 return ret;
1031}
1032
Yan Zheng2b820322008-11-17 21:11:30 -05001033int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001034 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001035 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001036 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001037{
1038 int ret;
1039 struct btrfs_path *path;
1040 struct btrfs_root *root = device->dev_root;
1041 struct btrfs_dev_extent *extent;
1042 struct extent_buffer *leaf;
1043 struct btrfs_key key;
1044
Chris Masondfe25022008-05-13 13:46:40 -04001045 WARN_ON(!device->in_fs_metadata);
Chris Mason0b86a832008-03-24 15:01:56 -04001046 path = btrfs_alloc_path();
1047 if (!path)
1048 return -ENOMEM;
1049
Chris Mason0b86a832008-03-24 15:01:56 -04001050 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001051 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001052 key.type = BTRFS_DEV_EXTENT_KEY;
1053 ret = btrfs_insert_empty_item(trans, root, path, &key,
1054 sizeof(*extent));
1055 BUG_ON(ret);
1056
1057 leaf = path->nodes[0];
1058 extent = btrfs_item_ptr(leaf, path->slots[0],
1059 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001060 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1061 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1062 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1063
1064 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1065 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1066 BTRFS_UUID_SIZE);
1067
Chris Mason0b86a832008-03-24 15:01:56 -04001068 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1069 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001070 btrfs_free_path(path);
1071 return ret;
1072}
1073
Chris Masona1b32a52008-09-05 16:09:51 -04001074static noinline int find_next_chunk(struct btrfs_root *root,
1075 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001076{
1077 struct btrfs_path *path;
1078 int ret;
1079 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001080 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001081 struct btrfs_key found_key;
1082
1083 path = btrfs_alloc_path();
Mark Fasheh92b8e8972011-07-12 10:57:59 -07001084 if (!path)
1085 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001086
Chris Masone17cade2008-04-15 15:41:47 -04001087 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001088 key.offset = (u64)-1;
1089 key.type = BTRFS_CHUNK_ITEM_KEY;
1090
1091 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1092 if (ret < 0)
1093 goto error;
1094
1095 BUG_ON(ret == 0);
1096
1097 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1098 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001099 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001100 } else {
1101 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1102 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001103 if (found_key.objectid != objectid)
1104 *offset = 0;
1105 else {
1106 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1107 struct btrfs_chunk);
1108 *offset = found_key.offset +
1109 btrfs_chunk_length(path->nodes[0], chunk);
1110 }
Chris Mason0b86a832008-03-24 15:01:56 -04001111 }
1112 ret = 0;
1113error:
1114 btrfs_free_path(path);
1115 return ret;
1116}
1117
Yan Zheng2b820322008-11-17 21:11:30 -05001118static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001119{
1120 int ret;
1121 struct btrfs_key key;
1122 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001123 struct btrfs_path *path;
1124
1125 root = root->fs_info->chunk_root;
1126
1127 path = btrfs_alloc_path();
1128 if (!path)
1129 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001130
1131 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1132 key.type = BTRFS_DEV_ITEM_KEY;
1133 key.offset = (u64)-1;
1134
1135 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1136 if (ret < 0)
1137 goto error;
1138
1139 BUG_ON(ret == 0);
1140
1141 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1142 BTRFS_DEV_ITEM_KEY);
1143 if (ret) {
1144 *objectid = 1;
1145 } else {
1146 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1147 path->slots[0]);
1148 *objectid = found_key.offset + 1;
1149 }
1150 ret = 0;
1151error:
Yan Zheng2b820322008-11-17 21:11:30 -05001152 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001153 return ret;
1154}
1155
1156/*
1157 * the device information is stored in the chunk root
1158 * the btrfs_device struct should be fully filled in
1159 */
1160int btrfs_add_device(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root,
1162 struct btrfs_device *device)
1163{
1164 int ret;
1165 struct btrfs_path *path;
1166 struct btrfs_dev_item *dev_item;
1167 struct extent_buffer *leaf;
1168 struct btrfs_key key;
1169 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001170
1171 root = root->fs_info->chunk_root;
1172
1173 path = btrfs_alloc_path();
1174 if (!path)
1175 return -ENOMEM;
1176
Chris Mason0b86a832008-03-24 15:01:56 -04001177 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1178 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001179 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001180
1181 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001182 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001183 if (ret)
1184 goto out;
1185
1186 leaf = path->nodes[0];
1187 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1188
1189 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001190 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001191 btrfs_set_device_type(leaf, dev_item, device->type);
1192 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1193 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1194 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001195 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1196 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001197 btrfs_set_device_group(leaf, dev_item, 0);
1198 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1199 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001200 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001201
Chris Mason0b86a832008-03-24 15:01:56 -04001202 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001203 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001204 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1205 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001206 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001207
Yan Zheng2b820322008-11-17 21:11:30 -05001208 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001209out:
1210 btrfs_free_path(path);
1211 return ret;
1212}
Chris Mason8f18cf12008-04-25 16:53:30 -04001213
Chris Masona061fc82008-05-07 11:43:44 -04001214static int btrfs_rm_dev_item(struct btrfs_root *root,
1215 struct btrfs_device *device)
1216{
1217 int ret;
1218 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001219 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001220 struct btrfs_trans_handle *trans;
1221
1222 root = root->fs_info->chunk_root;
1223
1224 path = btrfs_alloc_path();
1225 if (!path)
1226 return -ENOMEM;
1227
Yan, Zhenga22285a2010-05-16 10:48:46 -04001228 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001229 if (IS_ERR(trans)) {
1230 btrfs_free_path(path);
1231 return PTR_ERR(trans);
1232 }
Chris Masona061fc82008-05-07 11:43:44 -04001233 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1234 key.type = BTRFS_DEV_ITEM_KEY;
1235 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001236 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001237
1238 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1239 if (ret < 0)
1240 goto out;
1241
1242 if (ret > 0) {
1243 ret = -ENOENT;
1244 goto out;
1245 }
1246
1247 ret = btrfs_del_item(trans, root, path);
1248 if (ret)
1249 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001250out:
1251 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001252 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001253 btrfs_commit_transaction(trans, root);
1254 return ret;
1255}
1256
1257int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1258{
1259 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001260 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001261 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001262 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001263 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001264 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001265 u64 all_avail;
1266 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001267 u64 num_devices;
1268 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001269 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001270 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001271
Chris Masona061fc82008-05-07 11:43:44 -04001272 mutex_lock(&uuid_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04001273 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001274
1275 all_avail = root->fs_info->avail_data_alloc_bits |
1276 root->fs_info->avail_system_alloc_bits |
1277 root->fs_info->avail_metadata_alloc_bits;
1278
1279 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001280 root->fs_info->fs_devices->num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001281 printk(KERN_ERR "btrfs: unable to go below four devices "
1282 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001283 ret = -EINVAL;
1284 goto out;
1285 }
1286
1287 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
Josef Bacik035fe032010-01-27 02:09:38 +00001288 root->fs_info->fs_devices->num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001289 printk(KERN_ERR "btrfs: unable to go below two "
1290 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001291 ret = -EINVAL;
1292 goto out;
1293 }
1294
Chris Masondfe25022008-05-13 13:46:40 -04001295 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001296 struct list_head *devices;
1297 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001298
Chris Masondfe25022008-05-13 13:46:40 -04001299 device = NULL;
1300 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001301 /*
1302 * It is safe to read the devices since the volume_mutex
1303 * is held.
1304 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001305 list_for_each_entry(tmp, devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001306 if (tmp->in_fs_metadata && !tmp->bdev) {
1307 device = tmp;
1308 break;
1309 }
1310 }
1311 bdev = NULL;
1312 bh = NULL;
1313 disk_super = NULL;
1314 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001315 printk(KERN_ERR "btrfs: no missing devices found to "
1316 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001317 goto out;
1318 }
Chris Masondfe25022008-05-13 13:46:40 -04001319 } else {
Tejun Heod4d77622010-11-13 11:55:18 +01001320 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1321 root->fs_info->bdev_holder);
Chris Masondfe25022008-05-13 13:46:40 -04001322 if (IS_ERR(bdev)) {
1323 ret = PTR_ERR(bdev);
1324 goto out;
1325 }
1326
Yan Zheng2b820322008-11-17 21:11:30 -05001327 set_blocksize(bdev, 4096);
Yan Zhenga512bbf2008-12-08 16:46:26 -05001328 bh = btrfs_read_dev_super(bdev);
Chris Masondfe25022008-05-13 13:46:40 -04001329 if (!bh) {
Dave Young20b45072011-01-08 10:09:13 +00001330 ret = -EINVAL;
Chris Masondfe25022008-05-13 13:46:40 -04001331 goto error_close;
1332 }
1333 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001334 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001335 dev_uuid = disk_super->dev_item.uuid;
1336 device = btrfs_find_device(root, devid, dev_uuid,
1337 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001338 if (!device) {
1339 ret = -ENOENT;
1340 goto error_brelse;
1341 }
Chris Masondfe25022008-05-13 13:46:40 -04001342 }
Yan Zheng2b820322008-11-17 21:11:30 -05001343
1344 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001345 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1346 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001347 ret = -EINVAL;
1348 goto error_brelse;
1349 }
1350
1351 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001352 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001353 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001354 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001355 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001356 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001357 }
Chris Masona061fc82008-05-07 11:43:44 -04001358
1359 ret = btrfs_shrink_device(device, 0);
1360 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001361 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001362
Chris Masona061fc82008-05-07 11:43:44 -04001363 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1364 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001365 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001366
Yan Zheng2b820322008-11-17 21:11:30 -05001367 device->in_fs_metadata = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001368 btrfs_scrub_cancel_dev(root, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001369
1370 /*
1371 * the device list mutex makes sure that we don't change
1372 * the device list while someone else is writing out all
1373 * the device supers.
1374 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001375
1376 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001377 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001378 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001379
Yan Zhenge4404d62008-12-12 10:03:26 -05001380 device->fs_devices->num_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001381
Chris Masoncd02dca2010-12-13 14:56:23 -05001382 if (device->missing)
1383 root->fs_info->fs_devices->missing_devices--;
1384
Yan Zheng2b820322008-11-17 21:11:30 -05001385 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1386 struct btrfs_device, dev_list);
1387 if (device->bdev == root->fs_info->sb->s_bdev)
1388 root->fs_info->sb->s_bdev = next_device->bdev;
1389 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1390 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1391
Xiao Guangrong1f781602011-04-20 10:09:16 +00001392 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001393 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001394
1395 call_rcu(&device->rcu, free_device);
1396 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001397
Yan Zheng2b820322008-11-17 21:11:30 -05001398 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1399 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1400
Xiao Guangrong1f781602011-04-20 10:09:16 +00001401 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001402 struct btrfs_fs_devices *fs_devices;
1403 fs_devices = root->fs_info->fs_devices;
1404 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001405 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001406 break;
1407 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001408 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001409 fs_devices->seed = cur_devices->seed;
1410 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001411 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001412 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001413 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001414 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001415 }
1416
1417 /*
1418 * at this point, the device is zero sized. We want to
1419 * remove it from the devices list and zero out the old super
1420 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001421 if (clear_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001422 /* make sure this device isn't detected as part of
1423 * the FS anymore
1424 */
1425 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1426 set_buffer_dirty(bh);
1427 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001428 }
Chris Masona061fc82008-05-07 11:43:44 -04001429
Chris Masona061fc82008-05-07 11:43:44 -04001430 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001431
1432error_brelse:
1433 brelse(bh);
1434error_close:
Chris Masondfe25022008-05-13 13:46:40 -04001435 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001436 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001437out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001438 mutex_unlock(&root->fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001439 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001440 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001441error_undo:
1442 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001443 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001444 list_add(&device->dev_alloc_list,
1445 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001446 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001447 root->fs_info->fs_devices->rw_devices++;
1448 }
1449 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001450}
1451
Yan Zheng2b820322008-11-17 21:11:30 -05001452/*
1453 * does all the dirty work required for changing file system's UUID.
1454 */
1455static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1456 struct btrfs_root *root)
1457{
1458 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1459 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001460 struct btrfs_fs_devices *seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001461 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1462 struct btrfs_device *device;
1463 u64 super_flags;
1464
1465 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001466 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001467 return -EINVAL;
1468
Yan Zhenge4404d62008-12-12 10:03:26 -05001469 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1470 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001471 return -ENOMEM;
1472
Yan Zhenge4404d62008-12-12 10:03:26 -05001473 old_devices = clone_fs_devices(fs_devices);
1474 if (IS_ERR(old_devices)) {
1475 kfree(seed_devices);
1476 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001477 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001478
Yan Zheng2b820322008-11-17 21:11:30 -05001479 list_add(&old_devices->list, &fs_uuids);
1480
Yan Zhenge4404d62008-12-12 10:03:26 -05001481 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1482 seed_devices->opened = 1;
1483 INIT_LIST_HEAD(&seed_devices->devices);
1484 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001485 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001486
1487 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001488 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1489 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001490 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1491
Yan Zhenge4404d62008-12-12 10:03:26 -05001492 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1493 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1494 device->fs_devices = seed_devices;
1495 }
1496
Yan Zheng2b820322008-11-17 21:11:30 -05001497 fs_devices->seeding = 0;
1498 fs_devices->num_devices = 0;
1499 fs_devices->open_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001500 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001501
1502 generate_random_uuid(fs_devices->fsid);
1503 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1504 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1505 super_flags = btrfs_super_flags(disk_super) &
1506 ~BTRFS_SUPER_FLAG_SEEDING;
1507 btrfs_set_super_flags(disk_super, super_flags);
1508
1509 return 0;
1510}
1511
1512/*
1513 * strore the expected generation for seed devices in device items.
1514 */
1515static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1516 struct btrfs_root *root)
1517{
1518 struct btrfs_path *path;
1519 struct extent_buffer *leaf;
1520 struct btrfs_dev_item *dev_item;
1521 struct btrfs_device *device;
1522 struct btrfs_key key;
1523 u8 fs_uuid[BTRFS_UUID_SIZE];
1524 u8 dev_uuid[BTRFS_UUID_SIZE];
1525 u64 devid;
1526 int ret;
1527
1528 path = btrfs_alloc_path();
1529 if (!path)
1530 return -ENOMEM;
1531
1532 root = root->fs_info->chunk_root;
1533 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1534 key.offset = 0;
1535 key.type = BTRFS_DEV_ITEM_KEY;
1536
1537 while (1) {
1538 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1539 if (ret < 0)
1540 goto error;
1541
1542 leaf = path->nodes[0];
1543next_slot:
1544 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1545 ret = btrfs_next_leaf(root, path);
1546 if (ret > 0)
1547 break;
1548 if (ret < 0)
1549 goto error;
1550 leaf = path->nodes[0];
1551 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001552 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001553 continue;
1554 }
1555
1556 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1557 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1558 key.type != BTRFS_DEV_ITEM_KEY)
1559 break;
1560
1561 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1562 struct btrfs_dev_item);
1563 devid = btrfs_device_id(leaf, dev_item);
1564 read_extent_buffer(leaf, dev_uuid,
1565 (unsigned long)btrfs_device_uuid(dev_item),
1566 BTRFS_UUID_SIZE);
1567 read_extent_buffer(leaf, fs_uuid,
1568 (unsigned long)btrfs_device_fsid(dev_item),
1569 BTRFS_UUID_SIZE);
1570 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1571 BUG_ON(!device);
1572
1573 if (device->fs_devices->seeding) {
1574 btrfs_set_device_generation(leaf, dev_item,
1575 device->generation);
1576 btrfs_mark_buffer_dirty(leaf);
1577 }
1578
1579 path->slots[0]++;
1580 goto next_slot;
1581 }
1582 ret = 0;
1583error:
1584 btrfs_free_path(path);
1585 return ret;
1586}
1587
Chris Mason788f20e2008-04-28 15:29:42 -04001588int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1589{
Josef Bacikd5e20032011-08-04 14:52:27 +00001590 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001591 struct btrfs_trans_handle *trans;
1592 struct btrfs_device *device;
1593 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001594 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001595 struct super_block *sb = root->fs_info->sb;
Chris Mason788f20e2008-04-28 15:29:42 -04001596 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001597 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001598 int ret = 0;
1599
Yan Zheng2b820322008-11-17 21:11:30 -05001600 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1601 return -EINVAL;
Chris Mason788f20e2008-04-28 15:29:42 -04001602
Tejun Heod4d77622010-11-13 11:55:18 +01001603 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1604 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001605 if (IS_ERR(bdev))
1606 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001607
Yan Zheng2b820322008-11-17 21:11:30 -05001608 if (root->fs_info->fs_devices->seeding) {
1609 seeding_dev = 1;
1610 down_write(&sb->s_umount);
1611 mutex_lock(&uuid_mutex);
1612 }
1613
Chris Mason8c8bee12008-09-29 11:19:10 -04001614 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Mason7d9eb122008-07-08 14:19:17 -04001615 mutex_lock(&root->fs_info->volume_mutex);
Chris Masona2135012008-06-25 16:01:30 -04001616
Chris Mason788f20e2008-04-28 15:29:42 -04001617 devices = &root->fs_info->fs_devices->devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001618 /*
1619 * we have the volume lock, so we don't need the extra
1620 * device list mutex while reading the list here.
1621 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001622 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001623 if (device->bdev == bdev) {
1624 ret = -EEXIST;
Yan Zheng2b820322008-11-17 21:11:30 -05001625 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001626 }
1627 }
1628
1629 device = kzalloc(sizeof(*device), GFP_NOFS);
1630 if (!device) {
1631 /* we can safely leave the fs_devices entry around */
1632 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001633 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001634 }
1635
Chris Mason788f20e2008-04-28 15:29:42 -04001636 device->name = kstrdup(device_path, GFP_NOFS);
1637 if (!device->name) {
1638 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001639 ret = -ENOMEM;
1640 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001641 }
Yan Zheng2b820322008-11-17 21:11:30 -05001642
1643 ret = find_next_devid(root, &device->devid);
1644 if (ret) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001645 kfree(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001646 kfree(device);
1647 goto error;
1648 }
1649
Yan, Zhenga22285a2010-05-16 10:48:46 -04001650 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001651 if (IS_ERR(trans)) {
Ilya Dryomov67100f22011-02-06 19:58:21 +00001652 kfree(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001653 kfree(device);
1654 ret = PTR_ERR(trans);
1655 goto error;
1656 }
1657
Yan Zheng2b820322008-11-17 21:11:30 -05001658 lock_chunks(root);
1659
Josef Bacikd5e20032011-08-04 14:52:27 +00001660 q = bdev_get_queue(bdev);
1661 if (blk_queue_discard(q))
1662 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001663 device->writeable = 1;
1664 device->work.func = pending_bios_fn;
1665 generate_random_uuid(device->uuid);
1666 spin_lock_init(&device->io_lock);
1667 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001668 device->io_width = root->sectorsize;
1669 device->io_align = root->sectorsize;
1670 device->sector_size = root->sectorsize;
1671 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001672 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001673 device->dev_root = root->fs_info->dev_root;
1674 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001675 device->in_fs_metadata = 1;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001676 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001677 set_blocksize(device->bdev, 4096);
1678
Yan Zheng2b820322008-11-17 21:11:30 -05001679 if (seeding_dev) {
1680 sb->s_flags &= ~MS_RDONLY;
1681 ret = btrfs_prepare_sprout(trans, root);
1682 BUG_ON(ret);
1683 }
1684
1685 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001686
1687 /*
1688 * we don't want write_supers to jump in here with our device
1689 * half setup
1690 */
1691 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001692 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001693 list_add(&device->dev_alloc_list,
1694 &root->fs_info->fs_devices->alloc_list);
1695 root->fs_info->fs_devices->num_devices++;
1696 root->fs_info->fs_devices->open_devices++;
1697 root->fs_info->fs_devices->rw_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001698 if (device->can_discard)
1699 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001700 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1701
Chris Masonc2898112009-06-10 09:51:32 -04001702 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1703 root->fs_info->fs_devices->rotating = 1;
1704
Chris Mason788f20e2008-04-28 15:29:42 -04001705 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1706 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1707 total_bytes + device->total_bytes);
1708
1709 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1710 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1711 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001712 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001713
Yan Zheng2b820322008-11-17 21:11:30 -05001714 if (seeding_dev) {
1715 ret = init_first_rw_device(trans, root, device);
1716 BUG_ON(ret);
1717 ret = btrfs_finish_sprout(trans, root);
1718 BUG_ON(ret);
1719 } else {
1720 ret = btrfs_add_device(trans, root, device);
1721 }
1722
Chris Mason913d9522009-03-10 13:17:18 -04001723 /*
1724 * we've got more storage, clear any full flags on the space
1725 * infos
1726 */
1727 btrfs_clear_space_info_full(root->fs_info);
1728
Chris Mason7d9eb122008-07-08 14:19:17 -04001729 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001730 btrfs_commit_transaction(trans, root);
1731
1732 if (seeding_dev) {
1733 mutex_unlock(&uuid_mutex);
1734 up_write(&sb->s_umount);
1735
1736 ret = btrfs_relocate_sys_chunks(root);
1737 BUG_ON(ret);
1738 }
1739out:
Chris Mason7d9eb122008-07-08 14:19:17 -04001740 mutex_unlock(&root->fs_info->volume_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001741 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05001742error:
Tejun Heoe525fd82010-11-13 11:55:17 +01001743 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05001744 if (seeding_dev) {
1745 mutex_unlock(&uuid_mutex);
1746 up_write(&sb->s_umount);
1747 }
Chris Mason788f20e2008-04-28 15:29:42 -04001748 goto out;
1749}
1750
Chris Masond3977122009-01-05 21:25:51 -05001751static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1752 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001753{
1754 int ret;
1755 struct btrfs_path *path;
1756 struct btrfs_root *root;
1757 struct btrfs_dev_item *dev_item;
1758 struct extent_buffer *leaf;
1759 struct btrfs_key key;
1760
1761 root = device->dev_root->fs_info->chunk_root;
1762
1763 path = btrfs_alloc_path();
1764 if (!path)
1765 return -ENOMEM;
1766
1767 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1768 key.type = BTRFS_DEV_ITEM_KEY;
1769 key.offset = device->devid;
1770
1771 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1772 if (ret < 0)
1773 goto out;
1774
1775 if (ret > 0) {
1776 ret = -ENOENT;
1777 goto out;
1778 }
1779
1780 leaf = path->nodes[0];
1781 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1782
1783 btrfs_set_device_id(leaf, dev_item, device->devid);
1784 btrfs_set_device_type(leaf, dev_item, device->type);
1785 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1786 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1787 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04001788 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04001789 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1790 btrfs_mark_buffer_dirty(leaf);
1791
1792out:
1793 btrfs_free_path(path);
1794 return ret;
1795}
1796
Chris Mason7d9eb122008-07-08 14:19:17 -04001797static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001798 struct btrfs_device *device, u64 new_size)
1799{
1800 struct btrfs_super_block *super_copy =
1801 &device->dev_root->fs_info->super_copy;
1802 u64 old_total = btrfs_super_total_bytes(super_copy);
1803 u64 diff = new_size - device->total_bytes;
1804
Yan Zheng2b820322008-11-17 21:11:30 -05001805 if (!device->writeable)
1806 return -EACCES;
1807 if (new_size <= device->total_bytes)
1808 return -EINVAL;
1809
Chris Mason8f18cf12008-04-25 16:53:30 -04001810 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05001811 device->fs_devices->total_rw_bytes += diff;
1812
1813 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04001814 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04001815 btrfs_clear_space_info_full(device->dev_root->fs_info);
1816
Chris Mason8f18cf12008-04-25 16:53:30 -04001817 return btrfs_update_device(trans, device);
1818}
1819
Chris Mason7d9eb122008-07-08 14:19:17 -04001820int btrfs_grow_device(struct btrfs_trans_handle *trans,
1821 struct btrfs_device *device, u64 new_size)
1822{
1823 int ret;
1824 lock_chunks(device->dev_root);
1825 ret = __btrfs_grow_device(trans, device, new_size);
1826 unlock_chunks(device->dev_root);
1827 return ret;
1828}
1829
Chris Mason8f18cf12008-04-25 16:53:30 -04001830static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1831 struct btrfs_root *root,
1832 u64 chunk_tree, u64 chunk_objectid,
1833 u64 chunk_offset)
1834{
1835 int ret;
1836 struct btrfs_path *path;
1837 struct btrfs_key key;
1838
1839 root = root->fs_info->chunk_root;
1840 path = btrfs_alloc_path();
1841 if (!path)
1842 return -ENOMEM;
1843
1844 key.objectid = chunk_objectid;
1845 key.offset = chunk_offset;
1846 key.type = BTRFS_CHUNK_ITEM_KEY;
1847
1848 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1849 BUG_ON(ret);
1850
1851 ret = btrfs_del_item(trans, root, path);
Chris Mason8f18cf12008-04-25 16:53:30 -04001852
1853 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001854 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001855}
1856
Christoph Hellwigb2950862008-12-02 09:54:17 -05001857static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04001858 chunk_offset)
1859{
1860 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1861 struct btrfs_disk_key *disk_key;
1862 struct btrfs_chunk *chunk;
1863 u8 *ptr;
1864 int ret = 0;
1865 u32 num_stripes;
1866 u32 array_size;
1867 u32 len = 0;
1868 u32 cur;
1869 struct btrfs_key key;
1870
1871 array_size = btrfs_super_sys_array_size(super_copy);
1872
1873 ptr = super_copy->sys_chunk_array;
1874 cur = 0;
1875
1876 while (cur < array_size) {
1877 disk_key = (struct btrfs_disk_key *)ptr;
1878 btrfs_disk_key_to_cpu(&key, disk_key);
1879
1880 len = sizeof(*disk_key);
1881
1882 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1883 chunk = (struct btrfs_chunk *)(ptr + len);
1884 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1885 len += btrfs_chunk_item_size(num_stripes);
1886 } else {
1887 ret = -EIO;
1888 break;
1889 }
1890 if (key.objectid == chunk_objectid &&
1891 key.offset == chunk_offset) {
1892 memmove(ptr, ptr + len, array_size - (cur + len));
1893 array_size -= len;
1894 btrfs_set_super_sys_array_size(super_copy, array_size);
1895 } else {
1896 ptr += len;
1897 cur += len;
1898 }
1899 }
1900 return ret;
1901}
1902
Christoph Hellwigb2950862008-12-02 09:54:17 -05001903static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04001904 u64 chunk_tree, u64 chunk_objectid,
1905 u64 chunk_offset)
1906{
1907 struct extent_map_tree *em_tree;
1908 struct btrfs_root *extent_root;
1909 struct btrfs_trans_handle *trans;
1910 struct extent_map *em;
1911 struct map_lookup *map;
1912 int ret;
1913 int i;
1914
1915 root = root->fs_info->chunk_root;
1916 extent_root = root->fs_info->extent_root;
1917 em_tree = &root->fs_info->mapping_tree.map_tree;
1918
Josef Bacikba1bf482009-09-11 16:11:19 -04001919 ret = btrfs_can_relocate(extent_root, chunk_offset);
1920 if (ret)
1921 return -ENOSPC;
1922
Chris Mason8f18cf12008-04-25 16:53:30 -04001923 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04001924 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001925 if (ret)
1926 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04001927
Yan, Zhenga22285a2010-05-16 10:48:46 -04001928 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001929 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04001930
Chris Mason7d9eb122008-07-08 14:19:17 -04001931 lock_chunks(root);
1932
Chris Mason8f18cf12008-04-25 16:53:30 -04001933 /*
1934 * step two, delete the device extents and the
1935 * chunk tree entries
1936 */
Chris Mason890871b2009-09-02 16:24:52 -04001937 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001938 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001939 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001940
Chris Masona061fc82008-05-07 11:43:44 -04001941 BUG_ON(em->start > chunk_offset ||
1942 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04001943 map = (struct map_lookup *)em->bdev;
1944
1945 for (i = 0; i < map->num_stripes; i++) {
1946 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1947 map->stripes[i].physical);
1948 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04001949
Chris Masondfe25022008-05-13 13:46:40 -04001950 if (map->stripes[i].dev) {
1951 ret = btrfs_update_device(trans, map->stripes[i].dev);
1952 BUG_ON(ret);
1953 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001954 }
1955 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1956 chunk_offset);
1957
1958 BUG_ON(ret);
1959
liubo1abe9b82011-03-24 11:18:59 +00001960 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1961
Chris Mason8f18cf12008-04-25 16:53:30 -04001962 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1963 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1964 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04001965 }
1966
Zheng Yan1a40e232008-09-26 10:09:34 -04001967 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1968 BUG_ON(ret);
1969
Chris Mason890871b2009-09-02 16:24:52 -04001970 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04001971 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001972 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04001973
Chris Mason8f18cf12008-04-25 16:53:30 -04001974 kfree(map);
1975 em->bdev = NULL;
1976
1977 /* once for the tree */
1978 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04001979 /* once for us */
1980 free_extent_map(em);
1981
Chris Mason7d9eb122008-07-08 14:19:17 -04001982 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04001983 btrfs_end_transaction(trans, root);
1984 return 0;
1985}
1986
Yan Zheng2b820322008-11-17 21:11:30 -05001987static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1988{
1989 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1990 struct btrfs_path *path;
1991 struct extent_buffer *leaf;
1992 struct btrfs_chunk *chunk;
1993 struct btrfs_key key;
1994 struct btrfs_key found_key;
1995 u64 chunk_tree = chunk_root->root_key.objectid;
1996 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04001997 bool retried = false;
1998 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001999 int ret;
2000
2001 path = btrfs_alloc_path();
2002 if (!path)
2003 return -ENOMEM;
2004
Josef Bacikba1bf482009-09-11 16:11:19 -04002005again:
Yan Zheng2b820322008-11-17 21:11:30 -05002006 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2007 key.offset = (u64)-1;
2008 key.type = BTRFS_CHUNK_ITEM_KEY;
2009
2010 while (1) {
2011 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2012 if (ret < 0)
2013 goto error;
2014 BUG_ON(ret == 0);
2015
2016 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2017 key.type);
2018 if (ret < 0)
2019 goto error;
2020 if (ret > 0)
2021 break;
2022
2023 leaf = path->nodes[0];
2024 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2025
2026 chunk = btrfs_item_ptr(leaf, path->slots[0],
2027 struct btrfs_chunk);
2028 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002029 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002030
2031 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2032 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2033 found_key.objectid,
2034 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002035 if (ret == -ENOSPC)
2036 failed++;
2037 else if (ret)
2038 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002039 }
2040
2041 if (found_key.offset == 0)
2042 break;
2043 key.offset = found_key.offset - 1;
2044 }
2045 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002046 if (failed && !retried) {
2047 failed = 0;
2048 retried = true;
2049 goto again;
2050 } else if (failed && retried) {
2051 WARN_ON(1);
2052 ret = -ENOSPC;
2053 }
Yan Zheng2b820322008-11-17 21:11:30 -05002054error:
2055 btrfs_free_path(path);
2056 return ret;
2057}
2058
Chris Masonec44a352008-04-28 15:29:52 -04002059static u64 div_factor(u64 num, int factor)
2060{
2061 if (factor == 10)
2062 return num;
2063 num *= factor;
2064 do_div(num, 10);
2065 return num;
2066}
2067
Chris Masonec44a352008-04-28 15:29:52 -04002068int btrfs_balance(struct btrfs_root *dev_root)
2069{
2070 int ret;
Chris Masonec44a352008-04-28 15:29:52 -04002071 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2072 struct btrfs_device *device;
2073 u64 old_size;
2074 u64 size_to_free;
2075 struct btrfs_path *path;
2076 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002077 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2078 struct btrfs_trans_handle *trans;
2079 struct btrfs_key found_key;
2080
Yan Zheng2b820322008-11-17 21:11:30 -05002081 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2082 return -EROFS;
Chris Masonec44a352008-04-28 15:29:52 -04002083
Ben Hutchings6f88a442010-12-29 14:55:03 +00002084 if (!capable(CAP_SYS_ADMIN))
2085 return -EPERM;
2086
Chris Mason7d9eb122008-07-08 14:19:17 -04002087 mutex_lock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002088 dev_root = dev_root->fs_info->dev_root;
2089
Chris Masonec44a352008-04-28 15:29:52 -04002090 /* step one make some room on all the devices */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002091 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002092 old_size = device->total_bytes;
2093 size_to_free = div_factor(old_size, 1);
2094 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002095 if (!device->writeable ||
2096 device->total_bytes - device->bytes_used > size_to_free)
Chris Masonec44a352008-04-28 15:29:52 -04002097 continue;
2098
2099 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002100 if (ret == -ENOSPC)
2101 break;
Chris Masonec44a352008-04-28 15:29:52 -04002102 BUG_ON(ret);
2103
Yan, Zhenga22285a2010-05-16 10:48:46 -04002104 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002105 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002106
2107 ret = btrfs_grow_device(trans, device, old_size);
2108 BUG_ON(ret);
2109
2110 btrfs_end_transaction(trans, dev_root);
2111 }
2112
2113 /* step two, relocate all the chunks */
2114 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002115 if (!path) {
2116 ret = -ENOMEM;
2117 goto error;
2118 }
Chris Masonec44a352008-04-28 15:29:52 -04002119 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2120 key.offset = (u64)-1;
2121 key.type = BTRFS_CHUNK_ITEM_KEY;
2122
Chris Masond3977122009-01-05 21:25:51 -05002123 while (1) {
Chris Masonec44a352008-04-28 15:29:52 -04002124 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2125 if (ret < 0)
2126 goto error;
2127
2128 /*
2129 * this shouldn't happen, it means the last relocate
2130 * failed
2131 */
2132 if (ret == 0)
2133 break;
2134
2135 ret = btrfs_previous_item(chunk_root, path, 0,
2136 BTRFS_CHUNK_ITEM_KEY);
Chris Mason7d9eb122008-07-08 14:19:17 -04002137 if (ret)
Chris Masonec44a352008-04-28 15:29:52 -04002138 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002139
Chris Masonec44a352008-04-28 15:29:52 -04002140 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2141 path->slots[0]);
2142 if (found_key.objectid != key.objectid)
2143 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002144
Chris Masonec44a352008-04-28 15:29:52 -04002145 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002146 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002147 break;
2148
David Sterbab3b4aa72011-04-21 01:20:15 +02002149 btrfs_release_path(path);
Chris Masonec44a352008-04-28 15:29:52 -04002150 ret = btrfs_relocate_chunk(chunk_root,
2151 chunk_root->root_key.objectid,
2152 found_key.objectid,
2153 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002154 if (ret && ret != -ENOSPC)
2155 goto error;
Josef Bacikba1bf482009-09-11 16:11:19 -04002156 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002157 }
2158 ret = 0;
2159error:
2160 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04002161 mutex_unlock(&dev_root->fs_info->volume_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04002162 return ret;
2163}
2164
Chris Mason8f18cf12008-04-25 16:53:30 -04002165/*
2166 * shrinking a device means finding all of the device extents past
2167 * the new size, and then following the back refs to the chunks.
2168 * The chunk relocation code actually frees the device extent
2169 */
2170int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2171{
2172 struct btrfs_trans_handle *trans;
2173 struct btrfs_root *root = device->dev_root;
2174 struct btrfs_dev_extent *dev_extent = NULL;
2175 struct btrfs_path *path;
2176 u64 length;
2177 u64 chunk_tree;
2178 u64 chunk_objectid;
2179 u64 chunk_offset;
2180 int ret;
2181 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04002182 int failed = 0;
2183 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04002184 struct extent_buffer *l;
2185 struct btrfs_key key;
2186 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2187 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04002188 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04002189 u64 diff = device->total_bytes - new_size;
2190
Yan Zheng2b820322008-11-17 21:11:30 -05002191 if (new_size >= device->total_bytes)
2192 return -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002193
2194 path = btrfs_alloc_path();
2195 if (!path)
2196 return -ENOMEM;
2197
Chris Mason8f18cf12008-04-25 16:53:30 -04002198 path->reada = 2;
2199
Chris Mason7d9eb122008-07-08 14:19:17 -04002200 lock_chunks(root);
2201
Chris Mason8f18cf12008-04-25 16:53:30 -04002202 device->total_bytes = new_size;
Yan Zheng2b820322008-11-17 21:11:30 -05002203 if (device->writeable)
2204 device->fs_devices->total_rw_bytes -= diff;
Chris Mason7d9eb122008-07-08 14:19:17 -04002205 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002206
Josef Bacikba1bf482009-09-11 16:11:19 -04002207again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002208 key.objectid = device->devid;
2209 key.offset = (u64)-1;
2210 key.type = BTRFS_DEV_EXTENT_KEY;
2211
2212 while (1) {
2213 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2214 if (ret < 0)
2215 goto done;
2216
2217 ret = btrfs_previous_item(root, path, 0, key.type);
2218 if (ret < 0)
2219 goto done;
2220 if (ret) {
2221 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002222 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002223 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04002224 }
2225
2226 l = path->nodes[0];
2227 slot = path->slots[0];
2228 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2229
Josef Bacikba1bf482009-09-11 16:11:19 -04002230 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002231 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04002232 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002233 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002234
2235 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2236 length = btrfs_dev_extent_length(l, dev_extent);
2237
Josef Bacikba1bf482009-09-11 16:11:19 -04002238 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002239 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04002240 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04002241 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002242
2243 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2244 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2245 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02002246 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04002247
2248 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2249 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002250 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04002251 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04002252 if (ret == -ENOSPC)
2253 failed++;
2254 key.offset -= 1;
2255 }
2256
2257 if (failed && !retried) {
2258 failed = 0;
2259 retried = true;
2260 goto again;
2261 } else if (failed && retried) {
2262 ret = -ENOSPC;
2263 lock_chunks(root);
2264
2265 device->total_bytes = old_size;
2266 if (device->writeable)
2267 device->fs_devices->total_rw_bytes += diff;
2268 unlock_chunks(root);
2269 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04002270 }
2271
Chris Balld6397ba2009-04-27 07:29:03 -04002272 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002273 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002274 if (IS_ERR(trans)) {
2275 ret = PTR_ERR(trans);
2276 goto done;
2277 }
2278
Chris Balld6397ba2009-04-27 07:29:03 -04002279 lock_chunks(root);
2280
2281 device->disk_total_bytes = new_size;
2282 /* Now btrfs_update_device() will change the on-disk size. */
2283 ret = btrfs_update_device(trans, device);
2284 if (ret) {
2285 unlock_chunks(root);
2286 btrfs_end_transaction(trans, root);
2287 goto done;
2288 }
2289 WARN_ON(diff > old_total);
2290 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2291 unlock_chunks(root);
2292 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002293done:
2294 btrfs_free_path(path);
2295 return ret;
2296}
2297
Christoph Hellwigb2950862008-12-02 09:54:17 -05002298static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04002299 struct btrfs_root *root,
2300 struct btrfs_key *key,
2301 struct btrfs_chunk *chunk, int item_size)
2302{
2303 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2304 struct btrfs_disk_key disk_key;
2305 u32 array_size;
2306 u8 *ptr;
2307
2308 array_size = btrfs_super_sys_array_size(super_copy);
2309 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2310 return -EFBIG;
2311
2312 ptr = super_copy->sys_chunk_array + array_size;
2313 btrfs_cpu_key_to_disk(&disk_key, key);
2314 memcpy(ptr, &disk_key, sizeof(disk_key));
2315 ptr += sizeof(disk_key);
2316 memcpy(ptr, chunk, item_size);
2317 item_size += sizeof(disk_key);
2318 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2319 return 0;
2320}
2321
Miao Xieb2117a32011-01-05 10:07:28 +00002322/*
Arne Jansen73c5de02011-04-12 12:07:57 +02002323 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00002324 */
Arne Jansen73c5de02011-04-12 12:07:57 +02002325static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00002326{
Arne Jansen73c5de02011-04-12 12:07:57 +02002327 const struct btrfs_device_info *di_a = a;
2328 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00002329
Arne Jansen73c5de02011-04-12 12:07:57 +02002330 if (di_a->max_avail > di_b->max_avail)
2331 return -1;
2332 if (di_a->max_avail < di_b->max_avail)
2333 return 1;
2334 if (di_a->total_avail > di_b->total_avail)
2335 return -1;
2336 if (di_a->total_avail < di_b->total_avail)
2337 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00002338 return 0;
2339}
2340
2341static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2342 struct btrfs_root *extent_root,
2343 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02002344 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00002345 u64 start, u64 type)
2346{
2347 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00002348 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2349 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02002350 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00002351 struct extent_map_tree *em_tree;
2352 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02002353 struct btrfs_device_info *devices_info = NULL;
2354 u64 total_avail;
2355 int num_stripes; /* total number of stripes to allocate */
2356 int sub_stripes; /* sub_stripes info for map */
2357 int dev_stripes; /* stripes per dev */
2358 int devs_max; /* max devs to use */
2359 int devs_min; /* min devs needed */
2360 int devs_increment; /* ndevs has to be a multiple of this */
2361 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00002362 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02002363 u64 max_stripe_size;
2364 u64 max_chunk_size;
2365 u64 stripe_size;
2366 u64 num_bytes;
2367 int ndevs;
2368 int i;
2369 int j;
Miao Xieb2117a32011-01-05 10:07:28 +00002370
2371 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2372 (type & BTRFS_BLOCK_GROUP_DUP)) {
2373 WARN_ON(1);
2374 type &= ~BTRFS_BLOCK_GROUP_DUP;
2375 }
Arne Jansen73c5de02011-04-12 12:07:57 +02002376
Miao Xieb2117a32011-01-05 10:07:28 +00002377 if (list_empty(&fs_devices->alloc_list))
2378 return -ENOSPC;
2379
Arne Jansen73c5de02011-04-12 12:07:57 +02002380 sub_stripes = 1;
2381 dev_stripes = 1;
2382 devs_increment = 1;
2383 ncopies = 1;
2384 devs_max = 0; /* 0 == as many as possible */
2385 devs_min = 1;
2386
2387 /*
2388 * define the properties of each RAID type.
2389 * FIXME: move this to a global table and use it in all RAID
2390 * calculation code
2391 */
2392 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2393 dev_stripes = 2;
2394 ncopies = 2;
2395 devs_max = 1;
2396 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2397 devs_min = 2;
2398 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2399 devs_increment = 2;
2400 ncopies = 2;
2401 devs_max = 2;
2402 devs_min = 2;
2403 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2404 sub_stripes = 2;
2405 devs_increment = 2;
2406 ncopies = 2;
2407 devs_min = 4;
2408 } else {
2409 devs_max = 1;
2410 }
2411
2412 if (type & BTRFS_BLOCK_GROUP_DATA) {
2413 max_stripe_size = 1024 * 1024 * 1024;
2414 max_chunk_size = 10 * max_stripe_size;
2415 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2416 max_stripe_size = 256 * 1024 * 1024;
2417 max_chunk_size = max_stripe_size;
2418 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2419 max_stripe_size = 8 * 1024 * 1024;
2420 max_chunk_size = 2 * max_stripe_size;
2421 } else {
2422 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2423 type);
2424 BUG_ON(1);
2425 }
2426
2427 /* we don't want a chunk larger than 10% of writeable space */
2428 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2429 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00002430
2431 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2432 GFP_NOFS);
2433 if (!devices_info)
2434 return -ENOMEM;
2435
Arne Jansen73c5de02011-04-12 12:07:57 +02002436 cur = fs_devices->alloc_list.next;
2437
2438 /*
2439 * in the first pass through the devices list, we gather information
2440 * about the available holes on each device.
2441 */
2442 ndevs = 0;
2443 while (cur != &fs_devices->alloc_list) {
2444 struct btrfs_device *device;
2445 u64 max_avail;
2446 u64 dev_offset;
2447
2448 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2449
2450 cur = cur->next;
2451
2452 if (!device->writeable) {
2453 printk(KERN_ERR
2454 "btrfs: read-only device in alloc_list\n");
2455 WARN_ON(1);
2456 continue;
2457 }
2458
2459 if (!device->in_fs_metadata)
2460 continue;
2461
2462 if (device->total_bytes > device->bytes_used)
2463 total_avail = device->total_bytes - device->bytes_used;
2464 else
2465 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00002466
2467 /* If there is no space on this device, skip it. */
2468 if (total_avail == 0)
2469 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02002470
2471 ret = find_free_dev_extent(trans, device,
2472 max_stripe_size * dev_stripes,
2473 &dev_offset, &max_avail);
2474 if (ret && ret != -ENOSPC)
2475 goto error;
2476
2477 if (ret == 0)
2478 max_avail = max_stripe_size * dev_stripes;
2479
2480 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2481 continue;
2482
2483 devices_info[ndevs].dev_offset = dev_offset;
2484 devices_info[ndevs].max_avail = max_avail;
2485 devices_info[ndevs].total_avail = total_avail;
2486 devices_info[ndevs].dev = device;
2487 ++ndevs;
2488 }
2489
2490 /*
2491 * now sort the devices by hole size / available space
2492 */
2493 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2494 btrfs_cmp_device_info, NULL);
2495
2496 /* round down to number of usable stripes */
2497 ndevs -= ndevs % devs_increment;
2498
2499 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2500 ret = -ENOSPC;
2501 goto error;
2502 }
2503
2504 if (devs_max && ndevs > devs_max)
2505 ndevs = devs_max;
2506 /*
2507 * the primary goal is to maximize the number of stripes, so use as many
2508 * devices as possible, even if the stripes are not maximum sized.
2509 */
2510 stripe_size = devices_info[ndevs-1].max_avail;
2511 num_stripes = ndevs * dev_stripes;
2512
2513 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2514 stripe_size = max_chunk_size * ncopies;
2515 do_div(stripe_size, num_stripes);
2516 }
2517
2518 do_div(stripe_size, dev_stripes);
2519 do_div(stripe_size, BTRFS_STRIPE_LEN);
2520 stripe_size *= BTRFS_STRIPE_LEN;
2521
Miao Xieb2117a32011-01-05 10:07:28 +00002522 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2523 if (!map) {
2524 ret = -ENOMEM;
2525 goto error;
2526 }
2527 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04002528
Arne Jansen73c5de02011-04-12 12:07:57 +02002529 for (i = 0; i < ndevs; ++i) {
2530 for (j = 0; j < dev_stripes; ++j) {
2531 int s = i * dev_stripes + j;
2532 map->stripes[s].dev = devices_info[i].dev;
2533 map->stripes[s].physical = devices_info[i].dev_offset +
2534 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04002535 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002536 }
Chris Mason593060d2008-03-25 16:50:33 -04002537 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00002538 map->stripe_len = BTRFS_STRIPE_LEN;
2539 map->io_align = BTRFS_STRIPE_LEN;
2540 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04002541 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04002542 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04002543
Yan Zheng2b820322008-11-17 21:11:30 -05002544 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02002545 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04002546
Arne Jansen73c5de02011-04-12 12:07:57 +02002547 *stripe_size_out = stripe_size;
2548 *num_bytes_out = num_bytes;
2549
2550 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00002551
David Sterba172ddd62011-04-21 00:48:27 +02002552 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05002553 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00002554 ret = -ENOMEM;
2555 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05002556 }
Chris Mason0b86a832008-03-24 15:01:56 -04002557 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05002558 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02002559 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04002560 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002561 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04002562
Chris Mason0b86a832008-03-24 15:01:56 -04002563 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04002564 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002565 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002566 write_unlock(&em_tree->lock);
Chris Masonb248a412008-04-14 09:48:18 -04002567 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04002568 free_extent_map(em);
Yan Zheng2b820322008-11-17 21:11:30 -05002569
2570 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2571 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002572 start, num_bytes);
Yan Zheng2b820322008-11-17 21:11:30 -05002573 BUG_ON(ret);
2574
Arne Jansen73c5de02011-04-12 12:07:57 +02002575 for (i = 0; i < map->num_stripes; ++i) {
2576 struct btrfs_device *device;
2577 u64 dev_offset;
2578
2579 device = map->stripes[i].dev;
2580 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05002581
2582 ret = btrfs_alloc_dev_extent(trans, device,
2583 info->chunk_root->root_key.objectid,
2584 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02002585 start, dev_offset, stripe_size);
Yan Zheng2b820322008-11-17 21:11:30 -05002586 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002587 }
2588
Miao Xieb2117a32011-01-05 10:07:28 +00002589 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05002590 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00002591
2592error:
2593 kfree(map);
2594 kfree(devices_info);
2595 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002596}
2597
2598static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2599 struct btrfs_root *extent_root,
2600 struct map_lookup *map, u64 chunk_offset,
2601 u64 chunk_size, u64 stripe_size)
2602{
2603 u64 dev_offset;
2604 struct btrfs_key key;
2605 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2606 struct btrfs_device *device;
2607 struct btrfs_chunk *chunk;
2608 struct btrfs_stripe *stripe;
2609 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2610 int index = 0;
2611 int ret;
2612
2613 chunk = kzalloc(item_size, GFP_NOFS);
2614 if (!chunk)
2615 return -ENOMEM;
2616
2617 index = 0;
2618 while (index < map->num_stripes) {
2619 device = map->stripes[index].dev;
2620 device->bytes_used += stripe_size;
2621 ret = btrfs_update_device(trans, device);
2622 BUG_ON(ret);
2623 index++;
2624 }
2625
2626 index = 0;
2627 stripe = &chunk->stripe;
2628 while (index < map->num_stripes) {
2629 device = map->stripes[index].dev;
2630 dev_offset = map->stripes[index].physical;
2631
2632 btrfs_set_stack_stripe_devid(stripe, device->devid);
2633 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2634 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2635 stripe++;
2636 index++;
2637 }
2638
2639 btrfs_set_stack_chunk_length(chunk, chunk_size);
2640 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2641 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2642 btrfs_set_stack_chunk_type(chunk, map->type);
2643 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2644 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2645 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2646 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2647 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2648
2649 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2650 key.type = BTRFS_CHUNK_ITEM_KEY;
2651 key.offset = chunk_offset;
2652
2653 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2654 BUG_ON(ret);
2655
2656 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2657 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2658 item_size);
2659 BUG_ON(ret);
2660 }
liubo1abe9b82011-03-24 11:18:59 +00002661
Yan Zheng2b820322008-11-17 21:11:30 -05002662 kfree(chunk);
2663 return 0;
2664}
2665
2666/*
2667 * Chunk allocation falls into two parts. The first part does works
2668 * that make the new allocated chunk useable, but not do any operation
2669 * that modifies the chunk tree. The second part does the works that
2670 * require modifying the chunk tree. This division is important for the
2671 * bootstrap process of adding storage to a seed btrfs.
2672 */
2673int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2674 struct btrfs_root *extent_root, u64 type)
2675{
2676 u64 chunk_offset;
2677 u64 chunk_size;
2678 u64 stripe_size;
2679 struct map_lookup *map;
2680 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2681 int ret;
2682
2683 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2684 &chunk_offset);
2685 if (ret)
2686 return ret;
2687
2688 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2689 &stripe_size, chunk_offset, type);
2690 if (ret)
2691 return ret;
2692
2693 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2694 chunk_size, stripe_size);
2695 BUG_ON(ret);
2696 return 0;
2697}
2698
Chris Masond3977122009-01-05 21:25:51 -05002699static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05002700 struct btrfs_root *root,
2701 struct btrfs_device *device)
2702{
2703 u64 chunk_offset;
2704 u64 sys_chunk_offset;
2705 u64 chunk_size;
2706 u64 sys_chunk_size;
2707 u64 stripe_size;
2708 u64 sys_stripe_size;
2709 u64 alloc_profile;
2710 struct map_lookup *map;
2711 struct map_lookup *sys_map;
2712 struct btrfs_fs_info *fs_info = root->fs_info;
2713 struct btrfs_root *extent_root = fs_info->extent_root;
2714 int ret;
2715
2716 ret = find_next_chunk(fs_info->chunk_root,
2717 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07002718 if (ret)
2719 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05002720
2721 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2722 (fs_info->metadata_alloc_profile &
2723 fs_info->avail_metadata_alloc_bits);
2724 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2725
2726 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2727 &stripe_size, chunk_offset, alloc_profile);
2728 BUG_ON(ret);
2729
2730 sys_chunk_offset = chunk_offset + chunk_size;
2731
2732 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2733 (fs_info->system_alloc_profile &
2734 fs_info->avail_system_alloc_bits);
2735 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2736
2737 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2738 &sys_chunk_size, &sys_stripe_size,
2739 sys_chunk_offset, alloc_profile);
2740 BUG_ON(ret);
2741
2742 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2743 BUG_ON(ret);
2744
2745 /*
2746 * Modifying chunk tree needs allocating new blocks from both
2747 * system block group and metadata block group. So we only can
2748 * do operations require modifying the chunk tree after both
2749 * block groups were created.
2750 */
2751 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2752 chunk_size, stripe_size);
2753 BUG_ON(ret);
2754
2755 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2756 sys_chunk_offset, sys_chunk_size,
2757 sys_stripe_size);
2758 BUG_ON(ret);
2759 return 0;
2760}
2761
2762int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2763{
2764 struct extent_map *em;
2765 struct map_lookup *map;
2766 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2767 int readonly = 0;
2768 int i;
2769
Chris Mason890871b2009-09-02 16:24:52 -04002770 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002771 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002772 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05002773 if (!em)
2774 return 1;
2775
Josef Bacikf48b9072010-01-27 02:07:59 +00002776 if (btrfs_test_opt(root, DEGRADED)) {
2777 free_extent_map(em);
2778 return 0;
2779 }
2780
Yan Zheng2b820322008-11-17 21:11:30 -05002781 map = (struct map_lookup *)em->bdev;
2782 for (i = 0; i < map->num_stripes; i++) {
2783 if (!map->stripes[i].dev->writeable) {
2784 readonly = 1;
2785 break;
2786 }
2787 }
2788 free_extent_map(em);
2789 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04002790}
2791
2792void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2793{
David Sterbaa8067e02011-04-21 00:34:43 +02002794 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04002795}
2796
2797void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2798{
2799 struct extent_map *em;
2800
Chris Masond3977122009-01-05 21:25:51 -05002801 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04002802 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002803 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2804 if (em)
2805 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002806 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002807 if (!em)
2808 break;
2809 kfree(em->bdev);
2810 /* once for us */
2811 free_extent_map(em);
2812 /* once for the tree */
2813 free_extent_map(em);
2814 }
2815}
2816
Chris Masonf1885912008-04-09 16:28:12 -04002817int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2818{
2819 struct extent_map *em;
2820 struct map_lookup *map;
2821 struct extent_map_tree *em_tree = &map_tree->map_tree;
2822 int ret;
2823
Chris Mason890871b2009-09-02 16:24:52 -04002824 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002825 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04002826 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04002827 BUG_ON(!em);
2828
2829 BUG_ON(em->start > logical || em->start + em->len < logical);
2830 map = (struct map_lookup *)em->bdev;
2831 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2832 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002833 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2834 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002835 else
2836 ret = 1;
2837 free_extent_map(em);
Chris Masonf1885912008-04-09 16:28:12 -04002838 return ret;
2839}
2840
Chris Masondfe25022008-05-13 13:46:40 -04002841static int find_live_mirror(struct map_lookup *map, int first, int num,
2842 int optimal)
2843{
2844 int i;
2845 if (map->stripes[optimal].dev->bdev)
2846 return optimal;
2847 for (i = first; i < first + num; i++) {
2848 if (map->stripes[i].dev->bdev)
2849 return i;
2850 }
2851 /* we couldn't find one that doesn't fail. Just return something
2852 * and the io error handling code will clean up eventually
2853 */
2854 return optimal;
2855}
2856
Chris Masonf2d8d742008-04-21 10:03:05 -04002857static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2858 u64 logical, u64 *length,
2859 struct btrfs_multi_bio **multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01002860 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04002861{
2862 struct extent_map *em;
2863 struct map_lookup *map;
2864 struct extent_map_tree *em_tree = &map_tree->map_tree;
2865 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04002866 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002867 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04002868 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002869 u64 stripe_nr_orig;
2870 u64 stripe_nr_end;
Chris Masoncea9e442008-04-09 16:28:12 -04002871 int stripes_allocated = 8;
Chris Mason321aecc2008-04-16 10:49:51 -04002872 int stripes_required = 1;
Chris Mason593060d2008-03-25 16:50:33 -04002873 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04002874 int i;
Chris Masonf2d8d742008-04-21 10:03:05 -04002875 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002876 int max_errors = 0;
Chris Masoncea9e442008-04-09 16:28:12 -04002877 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04002878
Li Dongyangfce3bb92011-03-24 10:24:26 +00002879 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
Chris Masoncea9e442008-04-09 16:28:12 -04002880 stripes_allocated = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002881again:
2882 if (multi_ret) {
2883 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2884 GFP_NOFS);
2885 if (!multi)
2886 return -ENOMEM;
Chris Masona236aed2008-04-29 09:38:00 -04002887
2888 atomic_set(&multi->error, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002889 }
Chris Mason0b86a832008-03-24 15:01:56 -04002890
Chris Mason890871b2009-09-02 16:24:52 -04002891 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04002892 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04002893 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04002894
Chris Mason3b951512008-04-17 11:29:12 -04002895 if (!em) {
Chris Masond3977122009-01-05 21:25:51 -05002896 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2897 (unsigned long long)logical,
2898 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04002899 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04002900 }
Chris Mason0b86a832008-03-24 15:01:56 -04002901
2902 BUG_ON(em->start > logical || em->start + em->len < logical);
2903 map = (struct map_lookup *)em->bdev;
2904 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04002905
Chris Masonf1885912008-04-09 16:28:12 -04002906 if (mirror_num > map->num_stripes)
2907 mirror_num = 0;
2908
Chris Masoncea9e442008-04-09 16:28:12 -04002909 /* if our multi bio struct is too small, back off and try again */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002910 if (rw & REQ_WRITE) {
Chris Mason321aecc2008-04-16 10:49:51 -04002911 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2912 BTRFS_BLOCK_GROUP_DUP)) {
2913 stripes_required = map->num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002914 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002915 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2916 stripes_required = map->sub_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04002917 max_errors = 1;
Chris Mason321aecc2008-04-16 10:49:51 -04002918 }
2919 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00002920 if (rw & REQ_DISCARD) {
2921 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2922 BTRFS_BLOCK_GROUP_RAID1 |
2923 BTRFS_BLOCK_GROUP_DUP |
2924 BTRFS_BLOCK_GROUP_RAID10)) {
2925 stripes_required = map->num_stripes;
2926 }
2927 }
2928 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
Chris Mason321aecc2008-04-16 10:49:51 -04002929 stripes_allocated < stripes_required) {
Chris Masoncea9e442008-04-09 16:28:12 -04002930 stripes_allocated = map->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04002931 free_extent_map(em);
2932 kfree(multi);
2933 goto again;
2934 }
Chris Mason593060d2008-03-25 16:50:33 -04002935 stripe_nr = offset;
2936 /*
2937 * stripe_nr counts the total number of stripes we have to stride
2938 * to get to this block
2939 */
2940 do_div(stripe_nr, map->stripe_len);
2941
2942 stripe_offset = stripe_nr * map->stripe_len;
2943 BUG_ON(offset < stripe_offset);
2944
2945 /* stripe_offset is the offset of this block in its stripe*/
2946 stripe_offset = offset - stripe_offset;
2947
Li Dongyangfce3bb92011-03-24 10:24:26 +00002948 if (rw & REQ_DISCARD)
2949 *length = min_t(u64, em->len - offset, *length);
2950 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2951 BTRFS_BLOCK_GROUP_RAID1 |
2952 BTRFS_BLOCK_GROUP_RAID10 |
2953 BTRFS_BLOCK_GROUP_DUP)) {
Chris Masoncea9e442008-04-09 16:28:12 -04002954 /* we limit the length of each bio to what fits in a stripe */
2955 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00002956 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04002957 } else {
2958 *length = em->len - offset;
2959 }
Chris Masonf2d8d742008-04-21 10:03:05 -04002960
Jens Axboe7eaceac2011-03-10 08:52:07 +01002961 if (!multi_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04002962 goto out;
2963
Chris Masonf2d8d742008-04-21 10:03:05 -04002964 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04002965 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00002966 stripe_nr_orig = stripe_nr;
2967 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2968 (~(map->stripe_len - 1));
2969 do_div(stripe_nr_end, map->stripe_len);
2970 stripe_end_offset = stripe_nr_end * map->stripe_len -
2971 (offset + *length);
2972 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2973 if (rw & REQ_DISCARD)
2974 num_stripes = min_t(u64, map->num_stripes,
2975 stripe_nr_end - stripe_nr_orig);
2976 stripe_index = do_div(stripe_nr, map->num_stripes);
2977 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07002978 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002979 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04002980 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04002981 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04002982 else {
2983 stripe_index = find_live_mirror(map, 0,
2984 map->num_stripes,
2985 current->pid % map->num_stripes);
2986 }
Chris Mason2fff7342008-04-29 14:12:09 -04002987
Chris Mason611f0e02008-04-03 16:29:03 -04002988 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Li Dongyangfce3bb92011-03-24 10:24:26 +00002989 if (rw & (REQ_WRITE | REQ_DISCARD))
Chris Masonf2d8d742008-04-21 10:03:05 -04002990 num_stripes = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04002991 else if (mirror_num)
2992 stripe_index = mirror_num - 1;
Chris Mason2fff7342008-04-29 14:12:09 -04002993
Chris Mason321aecc2008-04-16 10:49:51 -04002994 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2995 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04002996
2997 stripe_index = do_div(stripe_nr, factor);
2998 stripe_index *= map->sub_stripes;
2999
Jens Axboe7eaceac2011-03-10 08:52:07 +01003000 if (rw & REQ_WRITE)
Chris Masonf2d8d742008-04-21 10:03:05 -04003001 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003002 else if (rw & REQ_DISCARD)
3003 num_stripes = min_t(u64, map->sub_stripes *
3004 (stripe_nr_end - stripe_nr_orig),
3005 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04003006 else if (mirror_num)
3007 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04003008 else {
3009 stripe_index = find_live_mirror(map, stripe_index,
3010 map->sub_stripes, stripe_index +
3011 current->pid % map->sub_stripes);
3012 }
Chris Mason8790d502008-04-03 16:29:03 -04003013 } else {
3014 /*
3015 * after this do_div call, stripe_nr is the number of stripes
3016 * on this device we have to walk to find the data, and
3017 * stripe_index is the number of our device in the stripe array
3018 */
3019 stripe_index = do_div(stripe_nr, map->num_stripes);
3020 }
Chris Mason593060d2008-03-25 16:50:33 -04003021 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04003022
Li Dongyangfce3bb92011-03-24 10:24:26 +00003023 if (rw & REQ_DISCARD) {
3024 for (i = 0; i < num_stripes; i++) {
Chris Masonf2d8d742008-04-21 10:03:05 -04003025 multi->stripes[i].physical =
3026 map->stripes[stripe_index].physical +
3027 stripe_offset + stripe_nr * map->stripe_len;
3028 multi->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003029
3030 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3031 u64 stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003032 u32 last_stripe = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003033 int j;
3034
Chris Masond9d04872011-03-27 21:23:21 -04003035 div_u64_rem(stripe_nr_end - 1,
3036 map->num_stripes,
3037 &last_stripe);
3038
Li Dongyangfce3bb92011-03-24 10:24:26 +00003039 for (j = 0; j < map->num_stripes; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003040 u32 test;
3041
3042 div_u64_rem(stripe_nr_end - 1 - j,
3043 map->num_stripes, &test);
3044 if (test == stripe_index)
Li Dongyangfce3bb92011-03-24 10:24:26 +00003045 break;
3046 }
3047 stripes = stripe_nr_end - 1 - j;
3048 do_div(stripes, map->num_stripes);
3049 multi->stripes[i].length = map->stripe_len *
3050 (stripes - stripe_nr + 1);
3051
3052 if (i == 0) {
3053 multi->stripes[i].length -=
3054 stripe_offset;
3055 stripe_offset = 0;
3056 }
3057 if (stripe_index == last_stripe)
3058 multi->stripes[i].length -=
3059 stripe_end_offset;
3060 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3061 u64 stripes;
3062 int j;
3063 int factor = map->num_stripes /
3064 map->sub_stripes;
Chris Masond9d04872011-03-27 21:23:21 -04003065 u32 last_stripe = 0;
3066
3067 div_u64_rem(stripe_nr_end - 1,
3068 factor, &last_stripe);
Li Dongyangfce3bb92011-03-24 10:24:26 +00003069 last_stripe *= map->sub_stripes;
3070
3071 for (j = 0; j < factor; j++) {
Chris Masond9d04872011-03-27 21:23:21 -04003072 u32 test;
3073
3074 div_u64_rem(stripe_nr_end - 1 - j,
3075 factor, &test);
3076
3077 if (test ==
Li Dongyangfce3bb92011-03-24 10:24:26 +00003078 stripe_index / map->sub_stripes)
3079 break;
3080 }
3081 stripes = stripe_nr_end - 1 - j;
3082 do_div(stripes, factor);
3083 multi->stripes[i].length = map->stripe_len *
3084 (stripes - stripe_nr + 1);
3085
3086 if (i < map->sub_stripes) {
3087 multi->stripes[i].length -=
3088 stripe_offset;
3089 if (i == map->sub_stripes - 1)
3090 stripe_offset = 0;
3091 }
3092 if (stripe_index >= last_stripe &&
3093 stripe_index <= (last_stripe +
3094 map->sub_stripes - 1)) {
3095 multi->stripes[i].length -=
3096 stripe_end_offset;
3097 }
3098 } else
3099 multi->stripes[i].length = *length;
3100
3101 stripe_index++;
3102 if (stripe_index == map->num_stripes) {
3103 /* This could only happen for RAID0/10 */
3104 stripe_index = 0;
3105 stripe_nr++;
3106 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003107 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00003108 } else {
3109 for (i = 0; i < num_stripes; i++) {
Linus Torvalds212a17a2011-03-28 15:31:05 -07003110 multi->stripes[i].physical =
3111 map->stripes[stripe_index].physical +
3112 stripe_offset +
3113 stripe_nr * map->stripe_len;
3114 multi->stripes[i].dev =
3115 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00003116 stripe_index++;
3117 }
Chris Mason593060d2008-03-25 16:50:33 -04003118 }
Chris Masonf2d8d742008-04-21 10:03:05 -04003119 if (multi_ret) {
3120 *multi_ret = multi;
3121 multi->num_stripes = num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04003122 multi->max_errors = max_errors;
Chris Masonf2d8d742008-04-21 10:03:05 -04003123 }
Chris Masoncea9e442008-04-09 16:28:12 -04003124out:
Chris Mason0b86a832008-03-24 15:01:56 -04003125 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003126 return 0;
3127}
3128
Chris Masonf2d8d742008-04-21 10:03:05 -04003129int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3130 u64 logical, u64 *length,
3131 struct btrfs_multi_bio **multi_ret, int mirror_num)
3132{
3133 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01003134 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04003135}
3136
Yan Zhenga512bbf2008-12-08 16:46:26 -05003137int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3138 u64 chunk_start, u64 physical, u64 devid,
3139 u64 **logical, int *naddrs, int *stripe_len)
3140{
3141 struct extent_map_tree *em_tree = &map_tree->map_tree;
3142 struct extent_map *em;
3143 struct map_lookup *map;
3144 u64 *buf;
3145 u64 bytenr;
3146 u64 length;
3147 u64 stripe_nr;
3148 int i, j, nr = 0;
3149
Chris Mason890871b2009-09-02 16:24:52 -04003150 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003151 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003152 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003153
3154 BUG_ON(!em || em->start != chunk_start);
3155 map = (struct map_lookup *)em->bdev;
3156
3157 length = em->len;
3158 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3159 do_div(length, map->num_stripes / map->sub_stripes);
3160 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3161 do_div(length, map->num_stripes);
3162
3163 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3164 BUG_ON(!buf);
3165
3166 for (i = 0; i < map->num_stripes; i++) {
3167 if (devid && map->stripes[i].dev->devid != devid)
3168 continue;
3169 if (map->stripes[i].physical > physical ||
3170 map->stripes[i].physical + length <= physical)
3171 continue;
3172
3173 stripe_nr = physical - map->stripes[i].physical;
3174 do_div(stripe_nr, map->stripe_len);
3175
3176 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3177 stripe_nr = stripe_nr * map->num_stripes + i;
3178 do_div(stripe_nr, map->sub_stripes);
3179 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3180 stripe_nr = stripe_nr * map->num_stripes + i;
3181 }
3182 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05003183 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003184 for (j = 0; j < nr; j++) {
3185 if (buf[j] == bytenr)
3186 break;
3187 }
Chris Mason934d3752008-12-08 16:43:10 -05003188 if (j == nr) {
3189 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003190 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05003191 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05003192 }
3193
Yan Zhenga512bbf2008-12-08 16:46:26 -05003194 *logical = buf;
3195 *naddrs = nr;
3196 *stripe_len = map->stripe_len;
3197
3198 free_extent_map(em);
3199 return 0;
3200}
3201
Chris Mason8790d502008-04-03 16:29:03 -04003202static void end_bio_multi_stripe(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04003203{
Chris Masoncea9e442008-04-09 16:28:12 -04003204 struct btrfs_multi_bio *multi = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003205 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003206
Chris Mason8790d502008-04-03 16:29:03 -04003207 if (err)
Chris Masona236aed2008-04-29 09:38:00 -04003208 atomic_inc(&multi->error);
Chris Mason8790d502008-04-03 16:29:03 -04003209
Chris Mason7d2b4da2008-08-05 10:13:57 -04003210 if (bio == multi->orig_bio)
3211 is_orig_bio = 1;
3212
Chris Masoncea9e442008-04-09 16:28:12 -04003213 if (atomic_dec_and_test(&multi->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04003214 if (!is_orig_bio) {
3215 bio_put(bio);
3216 bio = multi->orig_bio;
3217 }
Chris Mason8790d502008-04-03 16:29:03 -04003218 bio->bi_private = multi->private;
3219 bio->bi_end_io = multi->end_io;
Chris Masona236aed2008-04-29 09:38:00 -04003220 /* only send an error to the higher layers if it is
3221 * beyond the tolerance of the multi-bio
3222 */
Chris Mason1259ab72008-05-12 13:39:03 -04003223 if (atomic_read(&multi->error) > multi->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04003224 err = -EIO;
Chris Mason1259ab72008-05-12 13:39:03 -04003225 } else if (err) {
3226 /*
3227 * this bio is actually up to date, we didn't
3228 * go over the max number of errors
3229 */
3230 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04003231 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04003232 }
Chris Mason8790d502008-04-03 16:29:03 -04003233 kfree(multi);
3234
3235 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04003236 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04003237 bio_put(bio);
3238 }
Chris Mason8790d502008-04-03 16:29:03 -04003239}
3240
Chris Mason8b712842008-06-11 16:50:36 -04003241struct async_sched {
3242 struct bio *bio;
3243 int rw;
3244 struct btrfs_fs_info *info;
3245 struct btrfs_work work;
3246};
3247
3248/*
3249 * see run_scheduled_bios for a description of why bios are collected for
3250 * async submit.
3251 *
3252 * This will add one bio to the pending list for a device and make sure
3253 * the work struct is scheduled.
3254 */
Chris Masond3977122009-01-05 21:25:51 -05003255static noinline int schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04003256 struct btrfs_device *device,
3257 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04003258{
3259 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04003260 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003261
3262 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003263 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04003264 bio_get(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003265 submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04003266 bio_put(bio);
Chris Mason8b712842008-06-11 16:50:36 -04003267 return 0;
3268 }
3269
3270 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04003271 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04003272 * higher layers. Otherwise, the async bio makes it appear we have
3273 * made progress against dirty pages when we've really just put it
3274 * on a queue for later
3275 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04003276 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04003277 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04003278 bio->bi_next = NULL;
3279 bio->bi_rw |= rw;
3280
3281 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003282 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04003283 pending_bios = &device->pending_sync_bios;
3284 else
3285 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04003286
Chris Masonffbd5172009-04-20 15:50:09 -04003287 if (pending_bios->tail)
3288 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003289
Chris Masonffbd5172009-04-20 15:50:09 -04003290 pending_bios->tail = bio;
3291 if (!pending_bios->head)
3292 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04003293 if (device->running_pending)
3294 should_queue = 0;
3295
3296 spin_unlock(&device->io_lock);
3297
3298 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04003299 btrfs_queue_worker(&root->fs_info->submit_workers,
3300 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04003301 return 0;
3302}
3303
Chris Masonf1885912008-04-09 16:28:12 -04003304int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04003305 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04003306{
3307 struct btrfs_mapping_tree *map_tree;
3308 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04003309 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04003310 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04003311 u64 length = 0;
3312 u64 map_length;
Chris Masoncea9e442008-04-09 16:28:12 -04003313 struct btrfs_multi_bio *multi = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003314 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04003315 int dev_nr = 0;
3316 int total_devs = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003317
Chris Masonf2d8d742008-04-21 10:03:05 -04003318 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04003319 map_tree = &root->fs_info->mapping_tree;
3320 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04003321
Chris Masonf1885912008-04-09 16:28:12 -04003322 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3323 mirror_num);
Chris Masoncea9e442008-04-09 16:28:12 -04003324 BUG_ON(ret);
3325
3326 total_devs = multi->num_stripes;
3327 if (map_length < length) {
Chris Masond3977122009-01-05 21:25:51 -05003328 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3329 "len %llu\n", (unsigned long long)logical,
3330 (unsigned long long)length,
3331 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04003332 BUG();
3333 }
3334 multi->end_io = first_bio->bi_end_io;
3335 multi->private = first_bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04003336 multi->orig_bio = first_bio;
Chris Masoncea9e442008-04-09 16:28:12 -04003337 atomic_set(&multi->stripes_pending, multi->num_stripes);
3338
Chris Masond3977122009-01-05 21:25:51 -05003339 while (dev_nr < total_devs) {
Chris Mason8790d502008-04-03 16:29:03 -04003340 if (total_devs > 1) {
Chris Mason8790d502008-04-03 16:29:03 -04003341 if (dev_nr < total_devs - 1) {
3342 bio = bio_clone(first_bio, GFP_NOFS);
3343 BUG_ON(!bio);
3344 } else {
3345 bio = first_bio;
3346 }
3347 bio->bi_private = multi;
3348 bio->bi_end_io = end_bio_multi_stripe;
3349 }
Chris Masoncea9e442008-04-09 16:28:12 -04003350 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3351 dev = multi->stripes[dev_nr].dev;
Chris Mason18e503d2010-10-28 15:30:42 -04003352 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
Chris Masondfe25022008-05-13 13:46:40 -04003353 bio->bi_bdev = dev->bdev;
Chris Mason8b712842008-06-11 16:50:36 -04003354 if (async_submit)
3355 schedule_bio(root, dev, rw, bio);
3356 else
3357 submit_bio(rw, bio);
Chris Masondfe25022008-05-13 13:46:40 -04003358 } else {
3359 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3360 bio->bi_sector = logical >> 9;
Chris Masondfe25022008-05-13 13:46:40 -04003361 bio_endio(bio, -EIO);
Chris Masondfe25022008-05-13 13:46:40 -04003362 }
Chris Mason8790d502008-04-03 16:29:03 -04003363 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04003364 }
Chris Masoncea9e442008-04-09 16:28:12 -04003365 if (total_devs == 1)
3366 kfree(multi);
Chris Mason0b86a832008-03-24 15:01:56 -04003367 return 0;
3368}
3369
Chris Masona4437552008-04-18 10:29:38 -04003370struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05003371 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04003372{
Yan Zheng2b820322008-11-17 21:11:30 -05003373 struct btrfs_device *device;
3374 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04003375
Yan Zheng2b820322008-11-17 21:11:30 -05003376 cur_devices = root->fs_info->fs_devices;
3377 while (cur_devices) {
3378 if (!fsid ||
3379 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3380 device = __find_device(&cur_devices->devices,
3381 devid, uuid);
3382 if (device)
3383 return device;
3384 }
3385 cur_devices = cur_devices->seed;
3386 }
3387 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04003388}
3389
Chris Masondfe25022008-05-13 13:46:40 -04003390static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3391 u64 devid, u8 *dev_uuid)
3392{
3393 struct btrfs_device *device;
3394 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3395
3396 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05003397 if (!device)
3398 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04003399 list_add(&device->dev_list,
3400 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04003401 device->dev_root = root->fs_info->dev_root;
3402 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04003403 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05003404 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05003405 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04003406 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05003407 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04003408 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05003409 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04003410 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3411 return device;
3412}
3413
Chris Mason0b86a832008-03-24 15:01:56 -04003414static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3415 struct extent_buffer *leaf,
3416 struct btrfs_chunk *chunk)
3417{
3418 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3419 struct map_lookup *map;
3420 struct extent_map *em;
3421 u64 logical;
3422 u64 length;
3423 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04003424 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04003425 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003426 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04003427 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04003428
Chris Masone17cade2008-04-15 15:41:47 -04003429 logical = key->offset;
3430 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04003431
Chris Mason890871b2009-09-02 16:24:52 -04003432 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003433 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003434 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003435
3436 /* already mapped? */
3437 if (em && em->start <= logical && em->start + em->len > logical) {
3438 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04003439 return 0;
3440 } else if (em) {
3441 free_extent_map(em);
3442 }
Chris Mason0b86a832008-03-24 15:01:56 -04003443
David Sterba172ddd62011-04-21 00:48:27 +02003444 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04003445 if (!em)
3446 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04003447 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3448 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04003449 if (!map) {
3450 free_extent_map(em);
3451 return -ENOMEM;
3452 }
3453
3454 em->bdev = (struct block_device *)map;
3455 em->start = logical;
3456 em->len = length;
3457 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003458 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003459
Chris Mason593060d2008-03-25 16:50:33 -04003460 map->num_stripes = num_stripes;
3461 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3462 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3463 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3464 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3465 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04003466 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04003467 for (i = 0; i < num_stripes; i++) {
3468 map->stripes[i].physical =
3469 btrfs_stripe_offset_nr(leaf, chunk, i);
3470 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04003471 read_extent_buffer(leaf, uuid, (unsigned long)
3472 btrfs_stripe_dev_uuid_nr(chunk, i),
3473 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003474 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3475 NULL);
Chris Masondfe25022008-05-13 13:46:40 -04003476 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04003477 kfree(map);
3478 free_extent_map(em);
3479 return -EIO;
3480 }
Chris Masondfe25022008-05-13 13:46:40 -04003481 if (!map->stripes[i].dev) {
3482 map->stripes[i].dev =
3483 add_missing_dev(root, devid, uuid);
3484 if (!map->stripes[i].dev) {
3485 kfree(map);
3486 free_extent_map(em);
3487 return -EIO;
3488 }
3489 }
3490 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04003491 }
3492
Chris Mason890871b2009-09-02 16:24:52 -04003493 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003494 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003495 write_unlock(&map_tree->map_tree.lock);
Chris Masonb248a412008-04-14 09:48:18 -04003496 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -04003497 free_extent_map(em);
3498
3499 return 0;
3500}
3501
3502static int fill_device_from_item(struct extent_buffer *leaf,
3503 struct btrfs_dev_item *dev_item,
3504 struct btrfs_device *device)
3505{
3506 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04003507
3508 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04003509 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3510 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003511 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3512 device->type = btrfs_device_type(leaf, dev_item);
3513 device->io_align = btrfs_device_io_align(leaf, dev_item);
3514 device->io_width = btrfs_device_io_width(leaf, dev_item);
3515 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Chris Mason0b86a832008-03-24 15:01:56 -04003516
3517 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04003518 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003519
Chris Mason0b86a832008-03-24 15:01:56 -04003520 return 0;
3521}
3522
Yan Zheng2b820322008-11-17 21:11:30 -05003523static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3524{
3525 struct btrfs_fs_devices *fs_devices;
3526 int ret;
3527
3528 mutex_lock(&uuid_mutex);
3529
3530 fs_devices = root->fs_info->fs_devices->seed;
3531 while (fs_devices) {
3532 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3533 ret = 0;
3534 goto out;
3535 }
3536 fs_devices = fs_devices->seed;
3537 }
3538
3539 fs_devices = find_fsid(fsid);
3540 if (!fs_devices) {
3541 ret = -ENOENT;
3542 goto out;
3543 }
Yan Zhenge4404d62008-12-12 10:03:26 -05003544
3545 fs_devices = clone_fs_devices(fs_devices);
3546 if (IS_ERR(fs_devices)) {
3547 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003548 goto out;
3549 }
3550
Christoph Hellwig97288f22008-12-02 06:36:09 -05003551 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05003552 root->fs_info->bdev_holder);
Yan Zheng2b820322008-11-17 21:11:30 -05003553 if (ret)
3554 goto out;
3555
3556 if (!fs_devices->seeding) {
3557 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05003558 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05003559 ret = -EINVAL;
3560 goto out;
3561 }
3562
3563 fs_devices->seed = root->fs_info->fs_devices->seed;
3564 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05003565out:
3566 mutex_unlock(&uuid_mutex);
3567 return ret;
3568}
3569
Chris Mason0d81ba52008-03-24 15:02:07 -04003570static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003571 struct extent_buffer *leaf,
3572 struct btrfs_dev_item *dev_item)
3573{
3574 struct btrfs_device *device;
3575 u64 devid;
3576 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003577 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04003578 u8 dev_uuid[BTRFS_UUID_SIZE];
3579
Chris Mason0b86a832008-03-24 15:01:56 -04003580 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04003581 read_extent_buffer(leaf, dev_uuid,
3582 (unsigned long)btrfs_device_uuid(dev_item),
3583 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05003584 read_extent_buffer(leaf, fs_uuid,
3585 (unsigned long)btrfs_device_fsid(dev_item),
3586 BTRFS_UUID_SIZE);
3587
3588 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3589 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05003590 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003591 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003592 }
3593
3594 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3595 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05003596 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05003597 return -EIO;
3598
3599 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05003600 printk(KERN_WARNING "warning devid %llu missing\n",
3601 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05003602 device = add_missing_dev(root, devid, dev_uuid);
3603 if (!device)
3604 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05003605 } else if (!device->missing) {
3606 /*
3607 * this happens when a device that was properly setup
3608 * in the device info lists suddenly goes bad.
3609 * device->bdev is NULL, and so we have to set
3610 * device->missing to one here
3611 */
3612 root->fs_info->fs_devices->missing_devices++;
3613 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003614 }
3615 }
3616
3617 if (device->fs_devices != root->fs_info->fs_devices) {
3618 BUG_ON(device->writeable);
3619 if (device->generation !=
3620 btrfs_device_generation(leaf, dev_item))
3621 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04003622 }
Chris Mason0b86a832008-03-24 15:01:56 -04003623
3624 fill_device_from_item(leaf, dev_item, device);
3625 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04003626 device->in_fs_metadata = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05003627 if (device->writeable)
3628 device->fs_devices->total_rw_bytes += device->total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003629 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003630 return ret;
3631}
3632
Yan Zhenge4404d62008-12-12 10:03:26 -05003633int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04003634{
3635 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04003636 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04003637 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04003638 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04003639 u8 *ptr;
3640 unsigned long sb_ptr;
3641 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003642 u32 num_stripes;
3643 u32 array_size;
3644 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003645 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04003646 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04003647
Yan Zhenge4404d62008-12-12 10:03:26 -05003648 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04003649 BTRFS_SUPER_INFO_SIZE);
3650 if (!sb)
3651 return -ENOMEM;
3652 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04003653 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
Chris Mason4008c042009-02-12 14:09:45 -05003654
Chris Masona061fc82008-05-07 11:43:44 -04003655 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04003656 array_size = btrfs_super_sys_array_size(super_copy);
3657
Chris Mason0b86a832008-03-24 15:01:56 -04003658 ptr = super_copy->sys_chunk_array;
3659 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3660 cur = 0;
3661
3662 while (cur < array_size) {
3663 disk_key = (struct btrfs_disk_key *)ptr;
3664 btrfs_disk_key_to_cpu(&key, disk_key);
3665
Chris Masona061fc82008-05-07 11:43:44 -04003666 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04003667 sb_ptr += len;
3668 cur += len;
3669
Chris Mason0d81ba52008-03-24 15:02:07 -04003670 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04003671 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04003672 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04003673 if (ret)
3674 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003675 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3676 len = btrfs_chunk_item_size(num_stripes);
3677 } else {
Chris Mason84eed902008-04-25 09:04:37 -04003678 ret = -EIO;
3679 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003680 }
3681 ptr += len;
3682 sb_ptr += len;
3683 cur += len;
3684 }
Chris Masona061fc82008-05-07 11:43:44 -04003685 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04003686 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04003687}
3688
3689int btrfs_read_chunk_tree(struct btrfs_root *root)
3690{
3691 struct btrfs_path *path;
3692 struct extent_buffer *leaf;
3693 struct btrfs_key key;
3694 struct btrfs_key found_key;
3695 int ret;
3696 int slot;
3697
3698 root = root->fs_info->chunk_root;
3699
3700 path = btrfs_alloc_path();
3701 if (!path)
3702 return -ENOMEM;
3703
3704 /* first we search for all of the device items, and then we
3705 * read in all of the chunk items. This way we can create chunk
3706 * mappings that reference all of the devices that are afound
3707 */
3708 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3709 key.offset = 0;
3710 key.type = 0;
3711again:
3712 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00003713 if (ret < 0)
3714 goto error;
Chris Masond3977122009-01-05 21:25:51 -05003715 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003716 leaf = path->nodes[0];
3717 slot = path->slots[0];
3718 if (slot >= btrfs_header_nritems(leaf)) {
3719 ret = btrfs_next_leaf(root, path);
3720 if (ret == 0)
3721 continue;
3722 if (ret < 0)
3723 goto error;
3724 break;
3725 }
3726 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3727 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3728 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3729 break;
3730 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3731 struct btrfs_dev_item *dev_item;
3732 dev_item = btrfs_item_ptr(leaf, slot,
3733 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04003734 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05003735 if (ret)
3736 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003737 }
3738 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3739 struct btrfs_chunk *chunk;
3740 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3741 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05003742 if (ret)
3743 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04003744 }
3745 path->slots[0]++;
3746 }
3747 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3748 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003749 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003750 goto again;
3751 }
Chris Mason0b86a832008-03-24 15:01:56 -04003752 ret = 0;
3753error:
Yan Zheng2b820322008-11-17 21:11:30 -05003754 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04003755 return ret;
3756}