]>
Commit | Line | Data |
---|---|---|
0b86a832 CM |
1 | /* |
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> | |
5a0e3ad6 | 20 | #include <linux/slab.h> |
8a4b83cc | 21 | #include <linux/buffer_head.h> |
f2d8d74d | 22 | #include <linux/blkdev.h> |
788f20eb | 23 | #include <linux/random.h> |
b765ead5 | 24 | #include <linux/iocontext.h> |
6f88a440 | 25 | #include <linux/capability.h> |
593060d7 | 26 | #include <asm/div64.h> |
4b4e25f2 | 27 | #include "compat.h" |
0b86a832 CM |
28 | #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" | |
8b712842 | 34 | #include "async-thread.h" |
0b86a832 | 35 | |
593060d7 CM |
36 | struct map_lookup { |
37 | u64 type; | |
38 | int io_align; | |
39 | int io_width; | |
40 | int stripe_len; | |
41 | int sector_size; | |
42 | int num_stripes; | |
321aecc6 | 43 | int sub_stripes; |
cea9e445 | 44 | struct btrfs_bio_stripe stripes[]; |
593060d7 CM |
45 | }; |
46 | ||
2b82032c YZ |
47 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
48 | struct btrfs_root *root, | |
49 | struct btrfs_device *device); | |
50 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); | |
51 | ||
593060d7 | 52 | #define map_lookup_size(n) (sizeof(struct map_lookup) + \ |
cea9e445 | 53 | (sizeof(struct btrfs_bio_stripe) * (n))) |
593060d7 | 54 | |
8a4b83cc CM |
55 | static DEFINE_MUTEX(uuid_mutex); |
56 | static LIST_HEAD(fs_uuids); | |
57 | ||
a061fc8d CM |
58 | void btrfs_lock_volumes(void) |
59 | { | |
60 | mutex_lock(&uuid_mutex); | |
61 | } | |
62 | ||
63 | void btrfs_unlock_volumes(void) | |
64 | { | |
65 | mutex_unlock(&uuid_mutex); | |
66 | } | |
67 | ||
7d9eb12c CM |
68 | static void lock_chunks(struct btrfs_root *root) |
69 | { | |
7d9eb12c CM |
70 | mutex_lock(&root->fs_info->chunk_mutex); |
71 | } | |
72 | ||
73 | static void unlock_chunks(struct btrfs_root *root) | |
74 | { | |
7d9eb12c CM |
75 | mutex_unlock(&root->fs_info->chunk_mutex); |
76 | } | |
77 | ||
e4404d6e YZ |
78 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
79 | { | |
80 | struct btrfs_device *device; | |
81 | WARN_ON(fs_devices->opened); | |
82 | while (!list_empty(&fs_devices->devices)) { | |
83 | device = list_entry(fs_devices->devices.next, | |
84 | struct btrfs_device, dev_list); | |
85 | list_del(&device->dev_list); | |
86 | kfree(device->name); | |
87 | kfree(device); | |
88 | } | |
89 | kfree(fs_devices); | |
90 | } | |
91 | ||
8a4b83cc CM |
92 | int btrfs_cleanup_fs_uuids(void) |
93 | { | |
94 | struct btrfs_fs_devices *fs_devices; | |
8a4b83cc | 95 | |
2b82032c YZ |
96 | while (!list_empty(&fs_uuids)) { |
97 | fs_devices = list_entry(fs_uuids.next, | |
98 | struct btrfs_fs_devices, list); | |
99 | list_del(&fs_devices->list); | |
e4404d6e | 100 | free_fs_devices(fs_devices); |
8a4b83cc CM |
101 | } |
102 | return 0; | |
103 | } | |
104 | ||
a1b32a59 CM |
105 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
106 | u64 devid, u8 *uuid) | |
8a4b83cc CM |
107 | { |
108 | struct btrfs_device *dev; | |
8a4b83cc | 109 | |
c6e30871 | 110 | list_for_each_entry(dev, head, dev_list) { |
a443755f | 111 | if (dev->devid == devid && |
8f18cf13 | 112 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
8a4b83cc | 113 | return dev; |
a443755f | 114 | } |
8a4b83cc CM |
115 | } |
116 | return NULL; | |
117 | } | |
118 | ||
a1b32a59 | 119 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
8a4b83cc | 120 | { |
8a4b83cc CM |
121 | struct btrfs_fs_devices *fs_devices; |
122 | ||
c6e30871 | 123 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
8a4b83cc CM |
124 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
125 | return fs_devices; | |
126 | } | |
127 | return NULL; | |
128 | } | |
129 | ||
ffbd517d CM |
130 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
131 | struct bio *head, struct bio *tail) | |
132 | { | |
133 | ||
134 | struct bio *old_head; | |
135 | ||
136 | old_head = pending_bios->head; | |
137 | pending_bios->head = head; | |
138 | if (pending_bios->tail) | |
139 | tail->bi_next = old_head; | |
140 | else | |
141 | pending_bios->tail = tail; | |
142 | } | |
143 | ||
8b712842 CM |
144 | /* |
145 | * we try to collect pending bios for a device so we don't get a large | |
146 | * number of procs sending bios down to the same device. This greatly | |
147 | * improves the schedulers ability to collect and merge the bios. | |
148 | * | |
149 | * But, it also turns into a long list of bios to process and that is sure | |
150 | * to eventually make the worker thread block. The solution here is to | |
151 | * make some progress and then put this work struct back at the end of | |
152 | * the list if the block device is congested. This way, multiple devices | |
153 | * can make progress from a single worker thread. | |
154 | */ | |
d397712b | 155 | static noinline int run_scheduled_bios(struct btrfs_device *device) |
8b712842 CM |
156 | { |
157 | struct bio *pending; | |
158 | struct backing_dev_info *bdi; | |
b64a2851 | 159 | struct btrfs_fs_info *fs_info; |
ffbd517d | 160 | struct btrfs_pending_bios *pending_bios; |
8b712842 CM |
161 | struct bio *tail; |
162 | struct bio *cur; | |
163 | int again = 0; | |
ffbd517d CM |
164 | unsigned long num_run; |
165 | unsigned long num_sync_run; | |
d644d8a1 | 166 | unsigned long batch_run = 0; |
b64a2851 | 167 | unsigned long limit; |
b765ead5 | 168 | unsigned long last_waited = 0; |
d84275c9 | 169 | int force_reg = 0; |
8b712842 | 170 | |
bedf762b | 171 | bdi = blk_get_backing_dev_info(device->bdev); |
b64a2851 CM |
172 | fs_info = device->dev_root->fs_info; |
173 | limit = btrfs_async_submit_limit(fs_info); | |
174 | limit = limit * 2 / 3; | |
175 | ||
ffbd517d CM |
176 | /* we want to make sure that every time we switch from the sync |
177 | * list to the normal list, we unplug | |
178 | */ | |
179 | num_sync_run = 0; | |
180 | ||
8b712842 CM |
181 | loop: |
182 | spin_lock(&device->io_lock); | |
183 | ||
a6837051 | 184 | loop_lock: |
d84275c9 | 185 | num_run = 0; |
ffbd517d | 186 | |
8b712842 CM |
187 | /* take all the bios off the list at once and process them |
188 | * later on (without the lock held). But, remember the | |
189 | * tail and other pointers so the bios can be properly reinserted | |
190 | * into the list if we hit congestion | |
191 | */ | |
d84275c9 | 192 | if (!force_reg && device->pending_sync_bios.head) { |
ffbd517d | 193 | pending_bios = &device->pending_sync_bios; |
d84275c9 CM |
194 | force_reg = 1; |
195 | } else { | |
ffbd517d | 196 | pending_bios = &device->pending_bios; |
d84275c9 CM |
197 | force_reg = 0; |
198 | } | |
ffbd517d CM |
199 | |
200 | pending = pending_bios->head; | |
201 | tail = pending_bios->tail; | |
8b712842 | 202 | WARN_ON(pending && !tail); |
8b712842 CM |
203 | |
204 | /* | |
205 | * if pending was null this time around, no bios need processing | |
206 | * at all and we can stop. Otherwise it'll loop back up again | |
207 | * and do an additional check so no bios are missed. | |
208 | * | |
209 | * device->running_pending is used to synchronize with the | |
210 | * schedule_bio code. | |
211 | */ | |
ffbd517d CM |
212 | if (device->pending_sync_bios.head == NULL && |
213 | device->pending_bios.head == NULL) { | |
8b712842 CM |
214 | again = 0; |
215 | device->running_pending = 0; | |
ffbd517d CM |
216 | } else { |
217 | again = 1; | |
218 | device->running_pending = 1; | |
8b712842 | 219 | } |
ffbd517d CM |
220 | |
221 | pending_bios->head = NULL; | |
222 | pending_bios->tail = NULL; | |
223 | ||
8b712842 CM |
224 | spin_unlock(&device->io_lock); |
225 | ||
ffbd517d CM |
226 | /* |
227 | * if we're doing the regular priority list, make sure we unplug | |
228 | * for any high prio bios we've sent down | |
229 | */ | |
230 | if (pending_bios == &device->pending_bios && num_sync_run > 0) { | |
231 | num_sync_run = 0; | |
232 | blk_run_backing_dev(bdi, NULL); | |
233 | } | |
234 | ||
d397712b | 235 | while (pending) { |
ffbd517d CM |
236 | |
237 | rmb(); | |
d84275c9 CM |
238 | /* we want to work on both lists, but do more bios on the |
239 | * sync list than the regular list | |
240 | */ | |
241 | if ((num_run > 32 && | |
242 | pending_bios != &device->pending_sync_bios && | |
243 | device->pending_sync_bios.head) || | |
244 | (num_run > 64 && pending_bios == &device->pending_sync_bios && | |
245 | device->pending_bios.head)) { | |
ffbd517d CM |
246 | spin_lock(&device->io_lock); |
247 | requeue_list(pending_bios, pending, tail); | |
248 | goto loop_lock; | |
249 | } | |
250 | ||
8b712842 CM |
251 | cur = pending; |
252 | pending = pending->bi_next; | |
253 | cur->bi_next = NULL; | |
b64a2851 CM |
254 | atomic_dec(&fs_info->nr_async_bios); |
255 | ||
256 | if (atomic_read(&fs_info->nr_async_bios) < limit && | |
257 | waitqueue_active(&fs_info->async_submit_wait)) | |
258 | wake_up(&fs_info->async_submit_wait); | |
492bb6de CM |
259 | |
260 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); | |
d644d8a1 | 261 | |
7b6d91da | 262 | if (cur->bi_rw & REQ_SYNC) |
ffbd517d CM |
263 | num_sync_run++; |
264 | ||
5ff7ba3a CM |
265 | submit_bio(cur->bi_rw, cur); |
266 | num_run++; | |
267 | batch_run++; | |
ffbd517d CM |
268 | if (need_resched()) { |
269 | if (num_sync_run) { | |
270 | blk_run_backing_dev(bdi, NULL); | |
271 | num_sync_run = 0; | |
272 | } | |
273 | cond_resched(); | |
274 | } | |
8b712842 CM |
275 | |
276 | /* | |
277 | * we made progress, there is more work to do and the bdi | |
278 | * is now congested. Back off and let other work structs | |
279 | * run instead | |
280 | */ | |
57fd5a5f | 281 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
5f2cc086 | 282 | fs_info->fs_devices->open_devices > 1) { |
b765ead5 | 283 | struct io_context *ioc; |
8b712842 | 284 | |
b765ead5 CM |
285 | ioc = current->io_context; |
286 | ||
287 | /* | |
288 | * the main goal here is that we don't want to | |
289 | * block if we're going to be able to submit | |
290 | * more requests without blocking. | |
291 | * | |
292 | * This code does two great things, it pokes into | |
293 | * the elevator code from a filesystem _and_ | |
294 | * it makes assumptions about how batching works. | |
295 | */ | |
296 | if (ioc && ioc->nr_batch_requests > 0 && | |
297 | time_before(jiffies, ioc->last_waited + HZ/50UL) && | |
298 | (last_waited == 0 || | |
299 | ioc->last_waited == last_waited)) { | |
300 | /* | |
301 | * we want to go through our batch of | |
302 | * requests and stop. So, we copy out | |
303 | * the ioc->last_waited time and test | |
304 | * against it before looping | |
305 | */ | |
306 | last_waited = ioc->last_waited; | |
ffbd517d CM |
307 | if (need_resched()) { |
308 | if (num_sync_run) { | |
309 | blk_run_backing_dev(bdi, NULL); | |
310 | num_sync_run = 0; | |
311 | } | |
312 | cond_resched(); | |
313 | } | |
b765ead5 CM |
314 | continue; |
315 | } | |
8b712842 | 316 | spin_lock(&device->io_lock); |
ffbd517d | 317 | requeue_list(pending_bios, pending, tail); |
a6837051 | 318 | device->running_pending = 1; |
8b712842 CM |
319 | |
320 | spin_unlock(&device->io_lock); | |
321 | btrfs_requeue_work(&device->work); | |
322 | goto done; | |
323 | } | |
324 | } | |
ffbd517d CM |
325 | |
326 | if (num_sync_run) { | |
327 | num_sync_run = 0; | |
328 | blk_run_backing_dev(bdi, NULL); | |
329 | } | |
bedf762b CM |
330 | /* |
331 | * IO has already been through a long path to get here. Checksumming, | |
332 | * async helper threads, perhaps compression. We've done a pretty | |
333 | * good job of collecting a batch of IO and should just unplug | |
334 | * the device right away. | |
335 | * | |
336 | * This will help anyone who is waiting on the IO, they might have | |
337 | * already unplugged, but managed to do so before the bio they | |
338 | * cared about found its way down here. | |
339 | */ | |
340 | blk_run_backing_dev(bdi, NULL); | |
51684082 CM |
341 | |
342 | cond_resched(); | |
343 | if (again) | |
344 | goto loop; | |
345 | ||
346 | spin_lock(&device->io_lock); | |
347 | if (device->pending_bios.head || device->pending_sync_bios.head) | |
348 | goto loop_lock; | |
349 | spin_unlock(&device->io_lock); | |
350 | ||
8b712842 CM |
351 | done: |
352 | return 0; | |
353 | } | |
354 | ||
b2950863 | 355 | static void pending_bios_fn(struct btrfs_work *work) |
8b712842 CM |
356 | { |
357 | struct btrfs_device *device; | |
358 | ||
359 | device = container_of(work, struct btrfs_device, work); | |
360 | run_scheduled_bios(device); | |
361 | } | |
362 | ||
a1b32a59 | 363 | static noinline int device_list_add(const char *path, |
8a4b83cc CM |
364 | struct btrfs_super_block *disk_super, |
365 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) | |
366 | { | |
367 | struct btrfs_device *device; | |
368 | struct btrfs_fs_devices *fs_devices; | |
369 | u64 found_transid = btrfs_super_generation(disk_super); | |
3a0524dc | 370 | char *name; |
8a4b83cc CM |
371 | |
372 | fs_devices = find_fsid(disk_super->fsid); | |
373 | if (!fs_devices) { | |
515dc322 | 374 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
8a4b83cc CM |
375 | if (!fs_devices) |
376 | return -ENOMEM; | |
377 | INIT_LIST_HEAD(&fs_devices->devices); | |
b3075717 | 378 | INIT_LIST_HEAD(&fs_devices->alloc_list); |
8a4b83cc CM |
379 | list_add(&fs_devices->list, &fs_uuids); |
380 | memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE); | |
381 | fs_devices->latest_devid = devid; | |
382 | fs_devices->latest_trans = found_transid; | |
e5e9a520 | 383 | mutex_init(&fs_devices->device_list_mutex); |
8a4b83cc CM |
384 | device = NULL; |
385 | } else { | |
a443755f CM |
386 | device = __find_device(&fs_devices->devices, devid, |
387 | disk_super->dev_item.uuid); | |
8a4b83cc CM |
388 | } |
389 | if (!device) { | |
2b82032c YZ |
390 | if (fs_devices->opened) |
391 | return -EBUSY; | |
392 | ||
8a4b83cc CM |
393 | device = kzalloc(sizeof(*device), GFP_NOFS); |
394 | if (!device) { | |
395 | /* we can safely leave the fs_devices entry around */ | |
396 | return -ENOMEM; | |
397 | } | |
398 | device->devid = devid; | |
8b712842 | 399 | device->work.func = pending_bios_fn; |
a443755f CM |
400 | memcpy(device->uuid, disk_super->dev_item.uuid, |
401 | BTRFS_UUID_SIZE); | |
f2984462 | 402 | device->barriers = 1; |
b248a415 | 403 | spin_lock_init(&device->io_lock); |
8a4b83cc CM |
404 | device->name = kstrdup(path, GFP_NOFS); |
405 | if (!device->name) { | |
406 | kfree(device); | |
407 | return -ENOMEM; | |
408 | } | |
2b82032c | 409 | INIT_LIST_HEAD(&device->dev_alloc_list); |
e5e9a520 CM |
410 | |
411 | mutex_lock(&fs_devices->device_list_mutex); | |
8a4b83cc | 412 | list_add(&device->dev_list, &fs_devices->devices); |
e5e9a520 CM |
413 | mutex_unlock(&fs_devices->device_list_mutex); |
414 | ||
2b82032c | 415 | device->fs_devices = fs_devices; |
8a4b83cc | 416 | fs_devices->num_devices++; |
cd02dca5 | 417 | } else if (!device->name || strcmp(device->name, path)) { |
3a0524dc TH |
418 | name = kstrdup(path, GFP_NOFS); |
419 | if (!name) | |
420 | return -ENOMEM; | |
421 | kfree(device->name); | |
422 | device->name = name; | |
cd02dca5 CM |
423 | if (device->missing) { |
424 | fs_devices->missing_devices--; | |
425 | device->missing = 0; | |
426 | } | |
8a4b83cc CM |
427 | } |
428 | ||
429 | if (found_transid > fs_devices->latest_trans) { | |
430 | fs_devices->latest_devid = devid; | |
431 | fs_devices->latest_trans = found_transid; | |
432 | } | |
8a4b83cc CM |
433 | *fs_devices_ret = fs_devices; |
434 | return 0; | |
435 | } | |
436 | ||
e4404d6e YZ |
437 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
438 | { | |
439 | struct btrfs_fs_devices *fs_devices; | |
440 | struct btrfs_device *device; | |
441 | struct btrfs_device *orig_dev; | |
442 | ||
443 | fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); | |
444 | if (!fs_devices) | |
445 | return ERR_PTR(-ENOMEM); | |
446 | ||
447 | INIT_LIST_HEAD(&fs_devices->devices); | |
448 | INIT_LIST_HEAD(&fs_devices->alloc_list); | |
449 | INIT_LIST_HEAD(&fs_devices->list); | |
e5e9a520 | 450 | mutex_init(&fs_devices->device_list_mutex); |
e4404d6e YZ |
451 | fs_devices->latest_devid = orig->latest_devid; |
452 | fs_devices->latest_trans = orig->latest_trans; | |
453 | memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid)); | |
454 | ||
e5e9a520 | 455 | mutex_lock(&orig->device_list_mutex); |
e4404d6e YZ |
456 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
457 | device = kzalloc(sizeof(*device), GFP_NOFS); | |
458 | if (!device) | |
459 | goto error; | |
460 | ||
461 | device->name = kstrdup(orig_dev->name, GFP_NOFS); | |
fd2696f3 JL |
462 | if (!device->name) { |
463 | kfree(device); | |
e4404d6e | 464 | goto error; |
fd2696f3 | 465 | } |
e4404d6e YZ |
466 | |
467 | device->devid = orig_dev->devid; | |
468 | device->work.func = pending_bios_fn; | |
469 | memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid)); | |
470 | device->barriers = 1; | |
471 | spin_lock_init(&device->io_lock); | |
472 | INIT_LIST_HEAD(&device->dev_list); | |
473 | INIT_LIST_HEAD(&device->dev_alloc_list); | |
474 | ||
475 | list_add(&device->dev_list, &fs_devices->devices); | |
476 | device->fs_devices = fs_devices; | |
477 | fs_devices->num_devices++; | |
478 | } | |
e5e9a520 | 479 | mutex_unlock(&orig->device_list_mutex); |
e4404d6e YZ |
480 | return fs_devices; |
481 | error: | |
e5e9a520 | 482 | mutex_unlock(&orig->device_list_mutex); |
e4404d6e YZ |
483 | free_fs_devices(fs_devices); |
484 | return ERR_PTR(-ENOMEM); | |
485 | } | |
486 | ||
dfe25020 CM |
487 | int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices) |
488 | { | |
c6e30871 | 489 | struct btrfs_device *device, *next; |
dfe25020 CM |
490 | |
491 | mutex_lock(&uuid_mutex); | |
492 | again: | |
e5e9a520 | 493 | mutex_lock(&fs_devices->device_list_mutex); |
c6e30871 | 494 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
2b82032c YZ |
495 | if (device->in_fs_metadata) |
496 | continue; | |
497 | ||
498 | if (device->bdev) { | |
15916de8 | 499 | close_bdev_exclusive(device->bdev, device->mode); |
2b82032c YZ |
500 | device->bdev = NULL; |
501 | fs_devices->open_devices--; | |
502 | } | |
503 | if (device->writeable) { | |
504 | list_del_init(&device->dev_alloc_list); | |
505 | device->writeable = 0; | |
506 | fs_devices->rw_devices--; | |
507 | } | |
e4404d6e YZ |
508 | list_del_init(&device->dev_list); |
509 | fs_devices->num_devices--; | |
510 | kfree(device->name); | |
511 | kfree(device); | |
dfe25020 | 512 | } |
e5e9a520 | 513 | mutex_unlock(&fs_devices->device_list_mutex); |
2b82032c YZ |
514 | |
515 | if (fs_devices->seed) { | |
516 | fs_devices = fs_devices->seed; | |
2b82032c YZ |
517 | goto again; |
518 | } | |
519 | ||
dfe25020 CM |
520 | mutex_unlock(&uuid_mutex); |
521 | return 0; | |
522 | } | |
a0af469b | 523 | |
2b82032c | 524 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
8a4b83cc | 525 | { |
8a4b83cc | 526 | struct btrfs_device *device; |
e4404d6e | 527 | |
2b82032c YZ |
528 | if (--fs_devices->opened > 0) |
529 | return 0; | |
8a4b83cc | 530 | |
c6e30871 | 531 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
8a4b83cc | 532 | if (device->bdev) { |
15916de8 | 533 | close_bdev_exclusive(device->bdev, device->mode); |
a0af469b | 534 | fs_devices->open_devices--; |
8a4b83cc | 535 | } |
2b82032c YZ |
536 | if (device->writeable) { |
537 | list_del_init(&device->dev_alloc_list); | |
538 | fs_devices->rw_devices--; | |
539 | } | |
540 | ||
8a4b83cc | 541 | device->bdev = NULL; |
2b82032c | 542 | device->writeable = 0; |
dfe25020 | 543 | device->in_fs_metadata = 0; |
8a4b83cc | 544 | } |
e4404d6e YZ |
545 | WARN_ON(fs_devices->open_devices); |
546 | WARN_ON(fs_devices->rw_devices); | |
2b82032c YZ |
547 | fs_devices->opened = 0; |
548 | fs_devices->seeding = 0; | |
2b82032c | 549 | |
8a4b83cc CM |
550 | return 0; |
551 | } | |
552 | ||
2b82032c YZ |
553 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
554 | { | |
e4404d6e | 555 | struct btrfs_fs_devices *seed_devices = NULL; |
2b82032c YZ |
556 | int ret; |
557 | ||
558 | mutex_lock(&uuid_mutex); | |
559 | ret = __btrfs_close_devices(fs_devices); | |
e4404d6e YZ |
560 | if (!fs_devices->opened) { |
561 | seed_devices = fs_devices->seed; | |
562 | fs_devices->seed = NULL; | |
563 | } | |
2b82032c | 564 | mutex_unlock(&uuid_mutex); |
e4404d6e YZ |
565 | |
566 | while (seed_devices) { | |
567 | fs_devices = seed_devices; | |
568 | seed_devices = fs_devices->seed; | |
569 | __btrfs_close_devices(fs_devices); | |
570 | free_fs_devices(fs_devices); | |
571 | } | |
2b82032c YZ |
572 | return ret; |
573 | } | |
574 | ||
e4404d6e YZ |
575 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
576 | fmode_t flags, void *holder) | |
8a4b83cc CM |
577 | { |
578 | struct block_device *bdev; | |
579 | struct list_head *head = &fs_devices->devices; | |
8a4b83cc | 580 | struct btrfs_device *device; |
a0af469b CM |
581 | struct block_device *latest_bdev = NULL; |
582 | struct buffer_head *bh; | |
583 | struct btrfs_super_block *disk_super; | |
584 | u64 latest_devid = 0; | |
585 | u64 latest_transid = 0; | |
a0af469b | 586 | u64 devid; |
2b82032c | 587 | int seeding = 1; |
a0af469b | 588 | int ret = 0; |
8a4b83cc | 589 | |
c6e30871 | 590 | list_for_each_entry(device, head, dev_list) { |
c1c4d91c CM |
591 | if (device->bdev) |
592 | continue; | |
dfe25020 CM |
593 | if (!device->name) |
594 | continue; | |
595 | ||
15916de8 | 596 | bdev = open_bdev_exclusive(device->name, flags, holder); |
8a4b83cc | 597 | if (IS_ERR(bdev)) { |
d397712b | 598 | printk(KERN_INFO "open %s failed\n", device->name); |
a0af469b | 599 | goto error; |
8a4b83cc | 600 | } |
a061fc8d | 601 | set_blocksize(bdev, 4096); |
a0af469b | 602 | |
a512bbf8 | 603 | bh = btrfs_read_dev_super(bdev); |
20b45077 DY |
604 | if (!bh) { |
605 | ret = -EINVAL; | |
a0af469b | 606 | goto error_close; |
20b45077 | 607 | } |
a0af469b CM |
608 | |
609 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
a343832f | 610 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
a0af469b CM |
611 | if (devid != device->devid) |
612 | goto error_brelse; | |
613 | ||
2b82032c YZ |
614 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
615 | BTRFS_UUID_SIZE)) | |
616 | goto error_brelse; | |
617 | ||
618 | device->generation = btrfs_super_generation(disk_super); | |
619 | if (!latest_transid || device->generation > latest_transid) { | |
a0af469b | 620 | latest_devid = devid; |
2b82032c | 621 | latest_transid = device->generation; |
a0af469b CM |
622 | latest_bdev = bdev; |
623 | } | |
624 | ||
2b82032c YZ |
625 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
626 | device->writeable = 0; | |
627 | } else { | |
628 | device->writeable = !bdev_read_only(bdev); | |
629 | seeding = 0; | |
630 | } | |
631 | ||
8a4b83cc | 632 | device->bdev = bdev; |
dfe25020 | 633 | device->in_fs_metadata = 0; |
15916de8 CM |
634 | device->mode = flags; |
635 | ||
c289811c CM |
636 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
637 | fs_devices->rotating = 1; | |
638 | ||
a0af469b | 639 | fs_devices->open_devices++; |
2b82032c YZ |
640 | if (device->writeable) { |
641 | fs_devices->rw_devices++; | |
642 | list_add(&device->dev_alloc_list, | |
643 | &fs_devices->alloc_list); | |
644 | } | |
a0af469b | 645 | continue; |
a061fc8d | 646 | |
a0af469b CM |
647 | error_brelse: |
648 | brelse(bh); | |
649 | error_close: | |
97288f2c | 650 | close_bdev_exclusive(bdev, FMODE_READ); |
a0af469b CM |
651 | error: |
652 | continue; | |
8a4b83cc | 653 | } |
a0af469b CM |
654 | if (fs_devices->open_devices == 0) { |
655 | ret = -EIO; | |
656 | goto out; | |
657 | } | |
2b82032c YZ |
658 | fs_devices->seeding = seeding; |
659 | fs_devices->opened = 1; | |
a0af469b CM |
660 | fs_devices->latest_bdev = latest_bdev; |
661 | fs_devices->latest_devid = latest_devid; | |
662 | fs_devices->latest_trans = latest_transid; | |
2b82032c | 663 | fs_devices->total_rw_bytes = 0; |
a0af469b | 664 | out: |
2b82032c YZ |
665 | return ret; |
666 | } | |
667 | ||
668 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, | |
97288f2c | 669 | fmode_t flags, void *holder) |
2b82032c YZ |
670 | { |
671 | int ret; | |
672 | ||
673 | mutex_lock(&uuid_mutex); | |
674 | if (fs_devices->opened) { | |
e4404d6e YZ |
675 | fs_devices->opened++; |
676 | ret = 0; | |
2b82032c | 677 | } else { |
15916de8 | 678 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
2b82032c | 679 | } |
8a4b83cc | 680 | mutex_unlock(&uuid_mutex); |
8a4b83cc CM |
681 | return ret; |
682 | } | |
683 | ||
97288f2c | 684 | int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder, |
8a4b83cc CM |
685 | struct btrfs_fs_devices **fs_devices_ret) |
686 | { | |
687 | struct btrfs_super_block *disk_super; | |
688 | struct block_device *bdev; | |
689 | struct buffer_head *bh; | |
690 | int ret; | |
691 | u64 devid; | |
f2984462 | 692 | u64 transid; |
8a4b83cc CM |
693 | |
694 | mutex_lock(&uuid_mutex); | |
695 | ||
15916de8 | 696 | bdev = open_bdev_exclusive(path, flags, holder); |
8a4b83cc CM |
697 | |
698 | if (IS_ERR(bdev)) { | |
8a4b83cc CM |
699 | ret = PTR_ERR(bdev); |
700 | goto error; | |
701 | } | |
702 | ||
703 | ret = set_blocksize(bdev, 4096); | |
704 | if (ret) | |
705 | goto error_close; | |
a512bbf8 | 706 | bh = btrfs_read_dev_super(bdev); |
8a4b83cc | 707 | if (!bh) { |
20b45077 | 708 | ret = -EINVAL; |
8a4b83cc CM |
709 | goto error_close; |
710 | } | |
711 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
a343832f | 712 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
f2984462 | 713 | transid = btrfs_super_generation(disk_super); |
7ae9c09d | 714 | if (disk_super->label[0]) |
d397712b | 715 | printk(KERN_INFO "device label %s ", disk_super->label); |
7ae9c09d CM |
716 | else { |
717 | /* FIXME, make a readl uuid parser */ | |
d397712b | 718 | printk(KERN_INFO "device fsid %llx-%llx ", |
7ae9c09d CM |
719 | *(unsigned long long *)disk_super->fsid, |
720 | *(unsigned long long *)(disk_super->fsid + 8)); | |
721 | } | |
119e10cf | 722 | printk(KERN_CONT "devid %llu transid %llu %s\n", |
d397712b | 723 | (unsigned long long)devid, (unsigned long long)transid, path); |
8a4b83cc CM |
724 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
725 | ||
8a4b83cc CM |
726 | brelse(bh); |
727 | error_close: | |
15916de8 | 728 | close_bdev_exclusive(bdev, flags); |
8a4b83cc CM |
729 | error: |
730 | mutex_unlock(&uuid_mutex); | |
731 | return ret; | |
732 | } | |
0b86a832 | 733 | |
6d07bcec MX |
734 | /* helper to account the used device space in the range */ |
735 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, | |
736 | u64 end, u64 *length) | |
737 | { | |
738 | struct btrfs_key key; | |
739 | struct btrfs_root *root = device->dev_root; | |
740 | struct btrfs_dev_extent *dev_extent; | |
741 | struct btrfs_path *path; | |
742 | u64 extent_end; | |
743 | int ret; | |
744 | int slot; | |
745 | struct extent_buffer *l; | |
746 | ||
747 | *length = 0; | |
748 | ||
749 | if (start >= device->total_bytes) | |
750 | return 0; | |
751 | ||
752 | path = btrfs_alloc_path(); | |
753 | if (!path) | |
754 | return -ENOMEM; | |
755 | path->reada = 2; | |
756 | ||
757 | key.objectid = device->devid; | |
758 | key.offset = start; | |
759 | key.type = BTRFS_DEV_EXTENT_KEY; | |
760 | ||
761 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
762 | if (ret < 0) | |
763 | goto out; | |
764 | if (ret > 0) { | |
765 | ret = btrfs_previous_item(root, path, key.objectid, key.type); | |
766 | if (ret < 0) | |
767 | goto out; | |
768 | } | |
769 | ||
770 | while (1) { | |
771 | l = path->nodes[0]; | |
772 | slot = path->slots[0]; | |
773 | if (slot >= btrfs_header_nritems(l)) { | |
774 | ret = btrfs_next_leaf(root, path); | |
775 | if (ret == 0) | |
776 | continue; | |
777 | if (ret < 0) | |
778 | goto out; | |
779 | ||
780 | break; | |
781 | } | |
782 | btrfs_item_key_to_cpu(l, &key, slot); | |
783 | ||
784 | if (key.objectid < device->devid) | |
785 | goto next; | |
786 | ||
787 | if (key.objectid > device->devid) | |
788 | break; | |
789 | ||
790 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) | |
791 | goto next; | |
792 | ||
793 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
794 | extent_end = key.offset + btrfs_dev_extent_length(l, | |
795 | dev_extent); | |
796 | if (key.offset <= start && extent_end > end) { | |
797 | *length = end - start + 1; | |
798 | break; | |
799 | } else if (key.offset <= start && extent_end > start) | |
800 | *length += extent_end - start; | |
801 | else if (key.offset > start && extent_end <= end) | |
802 | *length += extent_end - key.offset; | |
803 | else if (key.offset > start && key.offset <= end) { | |
804 | *length += end - key.offset + 1; | |
805 | break; | |
806 | } else if (key.offset > end) | |
807 | break; | |
808 | ||
809 | next: | |
810 | path->slots[0]++; | |
811 | } | |
812 | ret = 0; | |
813 | out: | |
814 | btrfs_free_path(path); | |
815 | return ret; | |
816 | } | |
817 | ||
0b86a832 | 818 | /* |
7bfc837d MX |
819 | * find_free_dev_extent - find free space in the specified device |
820 | * @trans: transaction handler | |
821 | * @device: the device which we search the free space in | |
822 | * @num_bytes: the size of the free space that we need | |
823 | * @start: store the start of the free space. | |
824 | * @len: the size of the free space. that we find, or the size of the max | |
825 | * free space if we don't find suitable free space | |
826 | * | |
0b86a832 CM |
827 | * this uses a pretty simple search, the expectation is that it is |
828 | * called very infrequently and that a given device has a small number | |
829 | * of extents | |
7bfc837d MX |
830 | * |
831 | * @start is used to store the start of the free space if we find. But if we | |
832 | * don't find suitable free space, it will be used to store the start position | |
833 | * of the max free space. | |
834 | * | |
835 | * @len is used to store the size of the free space that we find. | |
836 | * But if we don't find suitable free space, it is used to store the size of | |
837 | * the max free space. | |
0b86a832 | 838 | */ |
ba1bf481 JB |
839 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
840 | struct btrfs_device *device, u64 num_bytes, | |
7bfc837d | 841 | u64 *start, u64 *len) |
0b86a832 CM |
842 | { |
843 | struct btrfs_key key; | |
844 | struct btrfs_root *root = device->dev_root; | |
7bfc837d | 845 | struct btrfs_dev_extent *dev_extent; |
2b82032c | 846 | struct btrfs_path *path; |
7bfc837d MX |
847 | u64 hole_size; |
848 | u64 max_hole_start; | |
849 | u64 max_hole_size; | |
850 | u64 extent_end; | |
851 | u64 search_start; | |
0b86a832 CM |
852 | u64 search_end = device->total_bytes; |
853 | int ret; | |
7bfc837d | 854 | int slot; |
0b86a832 CM |
855 | struct extent_buffer *l; |
856 | ||
0b86a832 CM |
857 | /* FIXME use last free of some kind */ |
858 | ||
8a4b83cc CM |
859 | /* we don't want to overwrite the superblock on the drive, |
860 | * so we make sure to start at an offset of at least 1MB | |
861 | */ | |
7bfc837d | 862 | search_start = 1024 * 1024; |
8f18cf13 | 863 | |
7bfc837d | 864 | if (root->fs_info->alloc_start + num_bytes <= search_end) |
8f18cf13 CM |
865 | search_start = max(root->fs_info->alloc_start, search_start); |
866 | ||
7bfc837d MX |
867 | max_hole_start = search_start; |
868 | max_hole_size = 0; | |
869 | ||
870 | if (search_start >= search_end) { | |
871 | ret = -ENOSPC; | |
872 | goto error; | |
873 | } | |
874 | ||
875 | path = btrfs_alloc_path(); | |
876 | if (!path) { | |
877 | ret = -ENOMEM; | |
878 | goto error; | |
879 | } | |
880 | path->reada = 2; | |
881 | ||
0b86a832 CM |
882 | key.objectid = device->devid; |
883 | key.offset = search_start; | |
884 | key.type = BTRFS_DEV_EXTENT_KEY; | |
7bfc837d | 885 | |
0b86a832 CM |
886 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); |
887 | if (ret < 0) | |
7bfc837d | 888 | goto out; |
1fcbac58 YZ |
889 | if (ret > 0) { |
890 | ret = btrfs_previous_item(root, path, key.objectid, key.type); | |
891 | if (ret < 0) | |
7bfc837d | 892 | goto out; |
1fcbac58 | 893 | } |
7bfc837d | 894 | |
0b86a832 CM |
895 | while (1) { |
896 | l = path->nodes[0]; | |
897 | slot = path->slots[0]; | |
898 | if (slot >= btrfs_header_nritems(l)) { | |
899 | ret = btrfs_next_leaf(root, path); | |
900 | if (ret == 0) | |
901 | continue; | |
902 | if (ret < 0) | |
7bfc837d MX |
903 | goto out; |
904 | ||
905 | break; | |
0b86a832 CM |
906 | } |
907 | btrfs_item_key_to_cpu(l, &key, slot); | |
908 | ||
909 | if (key.objectid < device->devid) | |
910 | goto next; | |
911 | ||
912 | if (key.objectid > device->devid) | |
7bfc837d | 913 | break; |
0b86a832 | 914 | |
7bfc837d MX |
915 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
916 | goto next; | |
9779b72f | 917 | |
7bfc837d MX |
918 | if (key.offset > search_start) { |
919 | hole_size = key.offset - search_start; | |
9779b72f | 920 | |
7bfc837d MX |
921 | if (hole_size > max_hole_size) { |
922 | max_hole_start = search_start; | |
923 | max_hole_size = hole_size; | |
924 | } | |
925 | ||
926 | /* | |
927 | * If this free space is greater than which we need, | |
928 | * it must be the max free space that we have found | |
929 | * until now, so max_hole_start must point to the start | |
930 | * of this free space and the length of this free space | |
931 | * is stored in max_hole_size. Thus, we return | |
932 | * max_hole_start and max_hole_size and go back to the | |
933 | * caller. | |
934 | */ | |
935 | if (hole_size >= num_bytes) { | |
936 | ret = 0; | |
937 | goto out; | |
0b86a832 CM |
938 | } |
939 | } | |
0b86a832 | 940 | |
0b86a832 | 941 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
7bfc837d MX |
942 | extent_end = key.offset + btrfs_dev_extent_length(l, |
943 | dev_extent); | |
944 | if (extent_end > search_start) | |
945 | search_start = extent_end; | |
0b86a832 CM |
946 | next: |
947 | path->slots[0]++; | |
948 | cond_resched(); | |
949 | } | |
0b86a832 | 950 | |
7bfc837d MX |
951 | hole_size = search_end- search_start; |
952 | if (hole_size > max_hole_size) { | |
953 | max_hole_start = search_start; | |
954 | max_hole_size = hole_size; | |
0b86a832 | 955 | } |
0b86a832 | 956 | |
7bfc837d MX |
957 | /* See above. */ |
958 | if (hole_size < num_bytes) | |
959 | ret = -ENOSPC; | |
960 | else | |
961 | ret = 0; | |
962 | ||
963 | out: | |
2b82032c | 964 | btrfs_free_path(path); |
7bfc837d MX |
965 | error: |
966 | *start = max_hole_start; | |
b2117a39 | 967 | if (len) |
7bfc837d | 968 | *len = max_hole_size; |
0b86a832 CM |
969 | return ret; |
970 | } | |
971 | ||
b2950863 | 972 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
8f18cf13 CM |
973 | struct btrfs_device *device, |
974 | u64 start) | |
975 | { | |
976 | int ret; | |
977 | struct btrfs_path *path; | |
978 | struct btrfs_root *root = device->dev_root; | |
979 | struct btrfs_key key; | |
a061fc8d CM |
980 | struct btrfs_key found_key; |
981 | struct extent_buffer *leaf = NULL; | |
982 | struct btrfs_dev_extent *extent = NULL; | |
8f18cf13 CM |
983 | |
984 | path = btrfs_alloc_path(); | |
985 | if (!path) | |
986 | return -ENOMEM; | |
987 | ||
988 | key.objectid = device->devid; | |
989 | key.offset = start; | |
990 | key.type = BTRFS_DEV_EXTENT_KEY; | |
991 | ||
992 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
a061fc8d CM |
993 | if (ret > 0) { |
994 | ret = btrfs_previous_item(root, path, key.objectid, | |
995 | BTRFS_DEV_EXTENT_KEY); | |
996 | BUG_ON(ret); | |
997 | leaf = path->nodes[0]; | |
998 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
999 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1000 | struct btrfs_dev_extent); | |
1001 | BUG_ON(found_key.offset > start || found_key.offset + | |
1002 | btrfs_dev_extent_length(leaf, extent) < start); | |
1003 | ret = 0; | |
1004 | } else if (ret == 0) { | |
1005 | leaf = path->nodes[0]; | |
1006 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1007 | struct btrfs_dev_extent); | |
1008 | } | |
8f18cf13 CM |
1009 | BUG_ON(ret); |
1010 | ||
dfe25020 CM |
1011 | if (device->bytes_used > 0) |
1012 | device->bytes_used -= btrfs_dev_extent_length(leaf, extent); | |
8f18cf13 CM |
1013 | ret = btrfs_del_item(trans, root, path); |
1014 | BUG_ON(ret); | |
1015 | ||
1016 | btrfs_free_path(path); | |
1017 | return ret; | |
1018 | } | |
1019 | ||
2b82032c | 1020 | int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
0b86a832 | 1021 | struct btrfs_device *device, |
e17cade2 | 1022 | u64 chunk_tree, u64 chunk_objectid, |
2b82032c | 1023 | u64 chunk_offset, u64 start, u64 num_bytes) |
0b86a832 CM |
1024 | { |
1025 | int ret; | |
1026 | struct btrfs_path *path; | |
1027 | struct btrfs_root *root = device->dev_root; | |
1028 | struct btrfs_dev_extent *extent; | |
1029 | struct extent_buffer *leaf; | |
1030 | struct btrfs_key key; | |
1031 | ||
dfe25020 | 1032 | WARN_ON(!device->in_fs_metadata); |
0b86a832 CM |
1033 | path = btrfs_alloc_path(); |
1034 | if (!path) | |
1035 | return -ENOMEM; | |
1036 | ||
0b86a832 | 1037 | key.objectid = device->devid; |
2b82032c | 1038 | key.offset = start; |
0b86a832 CM |
1039 | key.type = BTRFS_DEV_EXTENT_KEY; |
1040 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
1041 | sizeof(*extent)); | |
1042 | BUG_ON(ret); | |
1043 | ||
1044 | leaf = path->nodes[0]; | |
1045 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1046 | struct btrfs_dev_extent); | |
e17cade2 CM |
1047 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
1048 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); | |
1049 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); | |
1050 | ||
1051 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, | |
1052 | (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent), | |
1053 | BTRFS_UUID_SIZE); | |
1054 | ||
0b86a832 CM |
1055 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
1056 | btrfs_mark_buffer_dirty(leaf); | |
0b86a832 CM |
1057 | btrfs_free_path(path); |
1058 | return ret; | |
1059 | } | |
1060 | ||
a1b32a59 CM |
1061 | static noinline int find_next_chunk(struct btrfs_root *root, |
1062 | u64 objectid, u64 *offset) | |
0b86a832 CM |
1063 | { |
1064 | struct btrfs_path *path; | |
1065 | int ret; | |
1066 | struct btrfs_key key; | |
e17cade2 | 1067 | struct btrfs_chunk *chunk; |
0b86a832 CM |
1068 | struct btrfs_key found_key; |
1069 | ||
1070 | path = btrfs_alloc_path(); | |
1071 | BUG_ON(!path); | |
1072 | ||
e17cade2 | 1073 | key.objectid = objectid; |
0b86a832 CM |
1074 | key.offset = (u64)-1; |
1075 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
1076 | ||
1077 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1078 | if (ret < 0) | |
1079 | goto error; | |
1080 | ||
1081 | BUG_ON(ret == 0); | |
1082 | ||
1083 | ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY); | |
1084 | if (ret) { | |
e17cade2 | 1085 | *offset = 0; |
0b86a832 CM |
1086 | } else { |
1087 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1088 | path->slots[0]); | |
e17cade2 CM |
1089 | if (found_key.objectid != objectid) |
1090 | *offset = 0; | |
1091 | else { | |
1092 | chunk = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1093 | struct btrfs_chunk); | |
1094 | *offset = found_key.offset + | |
1095 | btrfs_chunk_length(path->nodes[0], chunk); | |
1096 | } | |
0b86a832 CM |
1097 | } |
1098 | ret = 0; | |
1099 | error: | |
1100 | btrfs_free_path(path); | |
1101 | return ret; | |
1102 | } | |
1103 | ||
2b82032c | 1104 | static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid) |
0b86a832 CM |
1105 | { |
1106 | int ret; | |
1107 | struct btrfs_key key; | |
1108 | struct btrfs_key found_key; | |
2b82032c YZ |
1109 | struct btrfs_path *path; |
1110 | ||
1111 | root = root->fs_info->chunk_root; | |
1112 | ||
1113 | path = btrfs_alloc_path(); | |
1114 | if (!path) | |
1115 | return -ENOMEM; | |
0b86a832 CM |
1116 | |
1117 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1118 | key.type = BTRFS_DEV_ITEM_KEY; | |
1119 | key.offset = (u64)-1; | |
1120 | ||
1121 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1122 | if (ret < 0) | |
1123 | goto error; | |
1124 | ||
1125 | BUG_ON(ret == 0); | |
1126 | ||
1127 | ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID, | |
1128 | BTRFS_DEV_ITEM_KEY); | |
1129 | if (ret) { | |
1130 | *objectid = 1; | |
1131 | } else { | |
1132 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1133 | path->slots[0]); | |
1134 | *objectid = found_key.offset + 1; | |
1135 | } | |
1136 | ret = 0; | |
1137 | error: | |
2b82032c | 1138 | btrfs_free_path(path); |
0b86a832 CM |
1139 | return ret; |
1140 | } | |
1141 | ||
1142 | /* | |
1143 | * the device information is stored in the chunk root | |
1144 | * the btrfs_device struct should be fully filled in | |
1145 | */ | |
1146 | int btrfs_add_device(struct btrfs_trans_handle *trans, | |
1147 | struct btrfs_root *root, | |
1148 | struct btrfs_device *device) | |
1149 | { | |
1150 | int ret; | |
1151 | struct btrfs_path *path; | |
1152 | struct btrfs_dev_item *dev_item; | |
1153 | struct extent_buffer *leaf; | |
1154 | struct btrfs_key key; | |
1155 | unsigned long ptr; | |
0b86a832 CM |
1156 | |
1157 | root = root->fs_info->chunk_root; | |
1158 | ||
1159 | path = btrfs_alloc_path(); | |
1160 | if (!path) | |
1161 | return -ENOMEM; | |
1162 | ||
0b86a832 CM |
1163 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
1164 | key.type = BTRFS_DEV_ITEM_KEY; | |
2b82032c | 1165 | key.offset = device->devid; |
0b86a832 CM |
1166 | |
1167 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
0d81ba5d | 1168 | sizeof(*dev_item)); |
0b86a832 CM |
1169 | if (ret) |
1170 | goto out; | |
1171 | ||
1172 | leaf = path->nodes[0]; | |
1173 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
1174 | ||
1175 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
2b82032c | 1176 | btrfs_set_device_generation(leaf, dev_item, 0); |
0b86a832 CM |
1177 | btrfs_set_device_type(leaf, dev_item, device->type); |
1178 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
1179 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
1180 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
0b86a832 CM |
1181 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
1182 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); | |
e17cade2 CM |
1183 | btrfs_set_device_group(leaf, dev_item, 0); |
1184 | btrfs_set_device_seek_speed(leaf, dev_item, 0); | |
1185 | btrfs_set_device_bandwidth(leaf, dev_item, 0); | |
c3027eb5 | 1186 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
0b86a832 | 1187 | |
0b86a832 | 1188 | ptr = (unsigned long)btrfs_device_uuid(dev_item); |
e17cade2 | 1189 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
2b82032c YZ |
1190 | ptr = (unsigned long)btrfs_device_fsid(dev_item); |
1191 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); | |
0b86a832 | 1192 | btrfs_mark_buffer_dirty(leaf); |
0b86a832 | 1193 | |
2b82032c | 1194 | ret = 0; |
0b86a832 CM |
1195 | out: |
1196 | btrfs_free_path(path); | |
1197 | return ret; | |
1198 | } | |
8f18cf13 | 1199 | |
a061fc8d CM |
1200 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
1201 | struct btrfs_device *device) | |
1202 | { | |
1203 | int ret; | |
1204 | struct btrfs_path *path; | |
a061fc8d | 1205 | struct btrfs_key key; |
a061fc8d CM |
1206 | struct btrfs_trans_handle *trans; |
1207 | ||
1208 | root = root->fs_info->chunk_root; | |
1209 | ||
1210 | path = btrfs_alloc_path(); | |
1211 | if (!path) | |
1212 | return -ENOMEM; | |
1213 | ||
a22285a6 | 1214 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
1215 | if (IS_ERR(trans)) { |
1216 | btrfs_free_path(path); | |
1217 | return PTR_ERR(trans); | |
1218 | } | |
a061fc8d CM |
1219 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
1220 | key.type = BTRFS_DEV_ITEM_KEY; | |
1221 | key.offset = device->devid; | |
7d9eb12c | 1222 | lock_chunks(root); |
a061fc8d CM |
1223 | |
1224 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1225 | if (ret < 0) | |
1226 | goto out; | |
1227 | ||
1228 | if (ret > 0) { | |
1229 | ret = -ENOENT; | |
1230 | goto out; | |
1231 | } | |
1232 | ||
1233 | ret = btrfs_del_item(trans, root, path); | |
1234 | if (ret) | |
1235 | goto out; | |
a061fc8d CM |
1236 | out: |
1237 | btrfs_free_path(path); | |
7d9eb12c | 1238 | unlock_chunks(root); |
a061fc8d CM |
1239 | btrfs_commit_transaction(trans, root); |
1240 | return ret; | |
1241 | } | |
1242 | ||
1243 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) | |
1244 | { | |
1245 | struct btrfs_device *device; | |
2b82032c | 1246 | struct btrfs_device *next_device; |
a061fc8d | 1247 | struct block_device *bdev; |
dfe25020 | 1248 | struct buffer_head *bh = NULL; |
a061fc8d CM |
1249 | struct btrfs_super_block *disk_super; |
1250 | u64 all_avail; | |
1251 | u64 devid; | |
2b82032c YZ |
1252 | u64 num_devices; |
1253 | u8 *dev_uuid; | |
a061fc8d CM |
1254 | int ret = 0; |
1255 | ||
a061fc8d | 1256 | mutex_lock(&uuid_mutex); |
7d9eb12c | 1257 | mutex_lock(&root->fs_info->volume_mutex); |
a061fc8d CM |
1258 | |
1259 | all_avail = root->fs_info->avail_data_alloc_bits | | |
1260 | root->fs_info->avail_system_alloc_bits | | |
1261 | root->fs_info->avail_metadata_alloc_bits; | |
1262 | ||
1263 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && | |
035fe03a | 1264 | root->fs_info->fs_devices->num_devices <= 4) { |
d397712b CM |
1265 | printk(KERN_ERR "btrfs: unable to go below four devices " |
1266 | "on raid10\n"); | |
a061fc8d CM |
1267 | ret = -EINVAL; |
1268 | goto out; | |
1269 | } | |
1270 | ||
1271 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && | |
035fe03a | 1272 | root->fs_info->fs_devices->num_devices <= 2) { |
d397712b CM |
1273 | printk(KERN_ERR "btrfs: unable to go below two " |
1274 | "devices on raid1\n"); | |
a061fc8d CM |
1275 | ret = -EINVAL; |
1276 | goto out; | |
1277 | } | |
1278 | ||
dfe25020 | 1279 | if (strcmp(device_path, "missing") == 0) { |
dfe25020 CM |
1280 | struct list_head *devices; |
1281 | struct btrfs_device *tmp; | |
a061fc8d | 1282 | |
dfe25020 CM |
1283 | device = NULL; |
1284 | devices = &root->fs_info->fs_devices->devices; | |
e5e9a520 | 1285 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
c6e30871 | 1286 | list_for_each_entry(tmp, devices, dev_list) { |
dfe25020 CM |
1287 | if (tmp->in_fs_metadata && !tmp->bdev) { |
1288 | device = tmp; | |
1289 | break; | |
1290 | } | |
1291 | } | |
e5e9a520 | 1292 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
dfe25020 CM |
1293 | bdev = NULL; |
1294 | bh = NULL; | |
1295 | disk_super = NULL; | |
1296 | if (!device) { | |
d397712b CM |
1297 | printk(KERN_ERR "btrfs: no missing devices found to " |
1298 | "remove\n"); | |
dfe25020 CM |
1299 | goto out; |
1300 | } | |
dfe25020 | 1301 | } else { |
97288f2c | 1302 | bdev = open_bdev_exclusive(device_path, FMODE_READ, |
dfe25020 CM |
1303 | root->fs_info->bdev_holder); |
1304 | if (IS_ERR(bdev)) { | |
1305 | ret = PTR_ERR(bdev); | |
1306 | goto out; | |
1307 | } | |
a061fc8d | 1308 | |
2b82032c | 1309 | set_blocksize(bdev, 4096); |
a512bbf8 | 1310 | bh = btrfs_read_dev_super(bdev); |
dfe25020 | 1311 | if (!bh) { |
20b45077 | 1312 | ret = -EINVAL; |
dfe25020 CM |
1313 | goto error_close; |
1314 | } | |
1315 | disk_super = (struct btrfs_super_block *)bh->b_data; | |
a343832f | 1316 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
2b82032c YZ |
1317 | dev_uuid = disk_super->dev_item.uuid; |
1318 | device = btrfs_find_device(root, devid, dev_uuid, | |
1319 | disk_super->fsid); | |
dfe25020 CM |
1320 | if (!device) { |
1321 | ret = -ENOENT; | |
1322 | goto error_brelse; | |
1323 | } | |
2b82032c | 1324 | } |
dfe25020 | 1325 | |
2b82032c | 1326 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
d397712b CM |
1327 | printk(KERN_ERR "btrfs: unable to remove the only writeable " |
1328 | "device\n"); | |
2b82032c YZ |
1329 | ret = -EINVAL; |
1330 | goto error_brelse; | |
1331 | } | |
1332 | ||
1333 | if (device->writeable) { | |
1334 | list_del_init(&device->dev_alloc_list); | |
1335 | root->fs_info->fs_devices->rw_devices--; | |
dfe25020 | 1336 | } |
a061fc8d CM |
1337 | |
1338 | ret = btrfs_shrink_device(device, 0); | |
1339 | if (ret) | |
9b3517e9 | 1340 | goto error_undo; |
a061fc8d | 1341 | |
a061fc8d CM |
1342 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
1343 | if (ret) | |
9b3517e9 | 1344 | goto error_undo; |
a061fc8d | 1345 | |
2b82032c | 1346 | device->in_fs_metadata = 0; |
e5e9a520 CM |
1347 | |
1348 | /* | |
1349 | * the device list mutex makes sure that we don't change | |
1350 | * the device list while someone else is writing out all | |
1351 | * the device supers. | |
1352 | */ | |
1353 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); | |
e4404d6e | 1354 | list_del_init(&device->dev_list); |
e5e9a520 CM |
1355 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
1356 | ||
e4404d6e | 1357 | device->fs_devices->num_devices--; |
2b82032c | 1358 | |
cd02dca5 CM |
1359 | if (device->missing) |
1360 | root->fs_info->fs_devices->missing_devices--; | |
1361 | ||
2b82032c YZ |
1362 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
1363 | struct btrfs_device, dev_list); | |
1364 | if (device->bdev == root->fs_info->sb->s_bdev) | |
1365 | root->fs_info->sb->s_bdev = next_device->bdev; | |
1366 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) | |
1367 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; | |
1368 | ||
e4404d6e YZ |
1369 | if (device->bdev) { |
1370 | close_bdev_exclusive(device->bdev, device->mode); | |
1371 | device->bdev = NULL; | |
1372 | device->fs_devices->open_devices--; | |
1373 | } | |
1374 | ||
2b82032c YZ |
1375 | num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1; |
1376 | btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices); | |
1377 | ||
e4404d6e YZ |
1378 | if (device->fs_devices->open_devices == 0) { |
1379 | struct btrfs_fs_devices *fs_devices; | |
1380 | fs_devices = root->fs_info->fs_devices; | |
1381 | while (fs_devices) { | |
1382 | if (fs_devices->seed == device->fs_devices) | |
1383 | break; | |
1384 | fs_devices = fs_devices->seed; | |
2b82032c | 1385 | } |
e4404d6e YZ |
1386 | fs_devices->seed = device->fs_devices->seed; |
1387 | device->fs_devices->seed = NULL; | |
1388 | __btrfs_close_devices(device->fs_devices); | |
1389 | free_fs_devices(device->fs_devices); | |
2b82032c YZ |
1390 | } |
1391 | ||
1392 | /* | |
1393 | * at this point, the device is zero sized. We want to | |
1394 | * remove it from the devices list and zero out the old super | |
1395 | */ | |
1396 | if (device->writeable) { | |
dfe25020 CM |
1397 | /* make sure this device isn't detected as part of |
1398 | * the FS anymore | |
1399 | */ | |
1400 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); | |
1401 | set_buffer_dirty(bh); | |
1402 | sync_dirty_buffer(bh); | |
dfe25020 | 1403 | } |
a061fc8d CM |
1404 | |
1405 | kfree(device->name); | |
1406 | kfree(device); | |
1407 | ret = 0; | |
a061fc8d CM |
1408 | |
1409 | error_brelse: | |
1410 | brelse(bh); | |
1411 | error_close: | |
dfe25020 | 1412 | if (bdev) |
97288f2c | 1413 | close_bdev_exclusive(bdev, FMODE_READ); |
a061fc8d | 1414 | out: |
7d9eb12c | 1415 | mutex_unlock(&root->fs_info->volume_mutex); |
a061fc8d | 1416 | mutex_unlock(&uuid_mutex); |
a061fc8d | 1417 | return ret; |
9b3517e9 ID |
1418 | error_undo: |
1419 | if (device->writeable) { | |
1420 | list_add(&device->dev_alloc_list, | |
1421 | &root->fs_info->fs_devices->alloc_list); | |
1422 | root->fs_info->fs_devices->rw_devices++; | |
1423 | } | |
1424 | goto error_brelse; | |
a061fc8d CM |
1425 | } |
1426 | ||
2b82032c YZ |
1427 | /* |
1428 | * does all the dirty work required for changing file system's UUID. | |
1429 | */ | |
1430 | static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans, | |
1431 | struct btrfs_root *root) | |
1432 | { | |
1433 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | |
1434 | struct btrfs_fs_devices *old_devices; | |
e4404d6e | 1435 | struct btrfs_fs_devices *seed_devices; |
2b82032c YZ |
1436 | struct btrfs_super_block *disk_super = &root->fs_info->super_copy; |
1437 | struct btrfs_device *device; | |
1438 | u64 super_flags; | |
1439 | ||
1440 | BUG_ON(!mutex_is_locked(&uuid_mutex)); | |
e4404d6e | 1441 | if (!fs_devices->seeding) |
2b82032c YZ |
1442 | return -EINVAL; |
1443 | ||
e4404d6e YZ |
1444 | seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS); |
1445 | if (!seed_devices) | |
2b82032c YZ |
1446 | return -ENOMEM; |
1447 | ||
e4404d6e YZ |
1448 | old_devices = clone_fs_devices(fs_devices); |
1449 | if (IS_ERR(old_devices)) { | |
1450 | kfree(seed_devices); | |
1451 | return PTR_ERR(old_devices); | |
2b82032c | 1452 | } |
e4404d6e | 1453 | |
2b82032c YZ |
1454 | list_add(&old_devices->list, &fs_uuids); |
1455 | ||
e4404d6e YZ |
1456 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
1457 | seed_devices->opened = 1; | |
1458 | INIT_LIST_HEAD(&seed_devices->devices); | |
1459 | INIT_LIST_HEAD(&seed_devices->alloc_list); | |
e5e9a520 | 1460 | mutex_init(&seed_devices->device_list_mutex); |
e4404d6e YZ |
1461 | list_splice_init(&fs_devices->devices, &seed_devices->devices); |
1462 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); | |
1463 | list_for_each_entry(device, &seed_devices->devices, dev_list) { | |
1464 | device->fs_devices = seed_devices; | |
1465 | } | |
1466 | ||
2b82032c YZ |
1467 | fs_devices->seeding = 0; |
1468 | fs_devices->num_devices = 0; | |
1469 | fs_devices->open_devices = 0; | |
e4404d6e | 1470 | fs_devices->seed = seed_devices; |
2b82032c YZ |
1471 | |
1472 | generate_random_uuid(fs_devices->fsid); | |
1473 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); | |
1474 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); | |
1475 | super_flags = btrfs_super_flags(disk_super) & | |
1476 | ~BTRFS_SUPER_FLAG_SEEDING; | |
1477 | btrfs_set_super_flags(disk_super, super_flags); | |
1478 | ||
1479 | return 0; | |
1480 | } | |
1481 | ||
1482 | /* | |
1483 | * strore the expected generation for seed devices in device items. | |
1484 | */ | |
1485 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, | |
1486 | struct btrfs_root *root) | |
1487 | { | |
1488 | struct btrfs_path *path; | |
1489 | struct extent_buffer *leaf; | |
1490 | struct btrfs_dev_item *dev_item; | |
1491 | struct btrfs_device *device; | |
1492 | struct btrfs_key key; | |
1493 | u8 fs_uuid[BTRFS_UUID_SIZE]; | |
1494 | u8 dev_uuid[BTRFS_UUID_SIZE]; | |
1495 | u64 devid; | |
1496 | int ret; | |
1497 | ||
1498 | path = btrfs_alloc_path(); | |
1499 | if (!path) | |
1500 | return -ENOMEM; | |
1501 | ||
1502 | root = root->fs_info->chunk_root; | |
1503 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1504 | key.offset = 0; | |
1505 | key.type = BTRFS_DEV_ITEM_KEY; | |
1506 | ||
1507 | while (1) { | |
1508 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
1509 | if (ret < 0) | |
1510 | goto error; | |
1511 | ||
1512 | leaf = path->nodes[0]; | |
1513 | next_slot: | |
1514 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1515 | ret = btrfs_next_leaf(root, path); | |
1516 | if (ret > 0) | |
1517 | break; | |
1518 | if (ret < 0) | |
1519 | goto error; | |
1520 | leaf = path->nodes[0]; | |
1521 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
1522 | btrfs_release_path(root, path); | |
1523 | continue; | |
1524 | } | |
1525 | ||
1526 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
1527 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || | |
1528 | key.type != BTRFS_DEV_ITEM_KEY) | |
1529 | break; | |
1530 | ||
1531 | dev_item = btrfs_item_ptr(leaf, path->slots[0], | |
1532 | struct btrfs_dev_item); | |
1533 | devid = btrfs_device_id(leaf, dev_item); | |
1534 | read_extent_buffer(leaf, dev_uuid, | |
1535 | (unsigned long)btrfs_device_uuid(dev_item), | |
1536 | BTRFS_UUID_SIZE); | |
1537 | read_extent_buffer(leaf, fs_uuid, | |
1538 | (unsigned long)btrfs_device_fsid(dev_item), | |
1539 | BTRFS_UUID_SIZE); | |
1540 | device = btrfs_find_device(root, devid, dev_uuid, fs_uuid); | |
1541 | BUG_ON(!device); | |
1542 | ||
1543 | if (device->fs_devices->seeding) { | |
1544 | btrfs_set_device_generation(leaf, dev_item, | |
1545 | device->generation); | |
1546 | btrfs_mark_buffer_dirty(leaf); | |
1547 | } | |
1548 | ||
1549 | path->slots[0]++; | |
1550 | goto next_slot; | |
1551 | } | |
1552 | ret = 0; | |
1553 | error: | |
1554 | btrfs_free_path(path); | |
1555 | return ret; | |
1556 | } | |
1557 | ||
788f20eb CM |
1558 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
1559 | { | |
1560 | struct btrfs_trans_handle *trans; | |
1561 | struct btrfs_device *device; | |
1562 | struct block_device *bdev; | |
788f20eb | 1563 | struct list_head *devices; |
2b82032c | 1564 | struct super_block *sb = root->fs_info->sb; |
788f20eb | 1565 | u64 total_bytes; |
2b82032c | 1566 | int seeding_dev = 0; |
788f20eb CM |
1567 | int ret = 0; |
1568 | ||
2b82032c YZ |
1569 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
1570 | return -EINVAL; | |
788f20eb | 1571 | |
15916de8 | 1572 | bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder); |
7f59203a JB |
1573 | if (IS_ERR(bdev)) |
1574 | return PTR_ERR(bdev); | |
a2135011 | 1575 | |
2b82032c YZ |
1576 | if (root->fs_info->fs_devices->seeding) { |
1577 | seeding_dev = 1; | |
1578 | down_write(&sb->s_umount); | |
1579 | mutex_lock(&uuid_mutex); | |
1580 | } | |
1581 | ||
8c8bee1d | 1582 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
7d9eb12c | 1583 | mutex_lock(&root->fs_info->volume_mutex); |
a2135011 | 1584 | |
788f20eb | 1585 | devices = &root->fs_info->fs_devices->devices; |
e5e9a520 CM |
1586 | /* |
1587 | * we have the volume lock, so we don't need the extra | |
1588 | * device list mutex while reading the list here. | |
1589 | */ | |
c6e30871 | 1590 | list_for_each_entry(device, devices, dev_list) { |
788f20eb CM |
1591 | if (device->bdev == bdev) { |
1592 | ret = -EEXIST; | |
2b82032c | 1593 | goto error; |
788f20eb CM |
1594 | } |
1595 | } | |
1596 | ||
1597 | device = kzalloc(sizeof(*device), GFP_NOFS); | |
1598 | if (!device) { | |
1599 | /* we can safely leave the fs_devices entry around */ | |
1600 | ret = -ENOMEM; | |
2b82032c | 1601 | goto error; |
788f20eb CM |
1602 | } |
1603 | ||
788f20eb CM |
1604 | device->name = kstrdup(device_path, GFP_NOFS); |
1605 | if (!device->name) { | |
1606 | kfree(device); | |
2b82032c YZ |
1607 | ret = -ENOMEM; |
1608 | goto error; | |
788f20eb | 1609 | } |
2b82032c YZ |
1610 | |
1611 | ret = find_next_devid(root, &device->devid); | |
1612 | if (ret) { | |
67100f25 | 1613 | kfree(device->name); |
2b82032c YZ |
1614 | kfree(device); |
1615 | goto error; | |
1616 | } | |
1617 | ||
a22285a6 | 1618 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 | 1619 | if (IS_ERR(trans)) { |
67100f25 | 1620 | kfree(device->name); |
98d5dc13 TI |
1621 | kfree(device); |
1622 | ret = PTR_ERR(trans); | |
1623 | goto error; | |
1624 | } | |
1625 | ||
2b82032c YZ |
1626 | lock_chunks(root); |
1627 | ||
1628 | device->barriers = 1; | |
1629 | device->writeable = 1; | |
1630 | device->work.func = pending_bios_fn; | |
1631 | generate_random_uuid(device->uuid); | |
1632 | spin_lock_init(&device->io_lock); | |
1633 | device->generation = trans->transid; | |
788f20eb CM |
1634 | device->io_width = root->sectorsize; |
1635 | device->io_align = root->sectorsize; | |
1636 | device->sector_size = root->sectorsize; | |
1637 | device->total_bytes = i_size_read(bdev->bd_inode); | |
2cc3c559 | 1638 | device->disk_total_bytes = device->total_bytes; |
788f20eb CM |
1639 | device->dev_root = root->fs_info->dev_root; |
1640 | device->bdev = bdev; | |
dfe25020 | 1641 | device->in_fs_metadata = 1; |
15916de8 | 1642 | device->mode = 0; |
2b82032c | 1643 | set_blocksize(device->bdev, 4096); |
788f20eb | 1644 | |
2b82032c YZ |
1645 | if (seeding_dev) { |
1646 | sb->s_flags &= ~MS_RDONLY; | |
1647 | ret = btrfs_prepare_sprout(trans, root); | |
1648 | BUG_ON(ret); | |
1649 | } | |
788f20eb | 1650 | |
2b82032c | 1651 | device->fs_devices = root->fs_info->fs_devices; |
e5e9a520 CM |
1652 | |
1653 | /* | |
1654 | * we don't want write_supers to jump in here with our device | |
1655 | * half setup | |
1656 | */ | |
1657 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); | |
2b82032c YZ |
1658 | list_add(&device->dev_list, &root->fs_info->fs_devices->devices); |
1659 | list_add(&device->dev_alloc_list, | |
1660 | &root->fs_info->fs_devices->alloc_list); | |
1661 | root->fs_info->fs_devices->num_devices++; | |
1662 | root->fs_info->fs_devices->open_devices++; | |
1663 | root->fs_info->fs_devices->rw_devices++; | |
1664 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; | |
325cd4ba | 1665 | |
c289811c CM |
1666 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
1667 | root->fs_info->fs_devices->rotating = 1; | |
1668 | ||
788f20eb CM |
1669 | total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
1670 | btrfs_set_super_total_bytes(&root->fs_info->super_copy, | |
1671 | total_bytes + device->total_bytes); | |
1672 | ||
1673 | total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy); | |
1674 | btrfs_set_super_num_devices(&root->fs_info->super_copy, | |
1675 | total_bytes + 1); | |
e5e9a520 | 1676 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
788f20eb | 1677 | |
2b82032c YZ |
1678 | if (seeding_dev) { |
1679 | ret = init_first_rw_device(trans, root, device); | |
1680 | BUG_ON(ret); | |
1681 | ret = btrfs_finish_sprout(trans, root); | |
1682 | BUG_ON(ret); | |
1683 | } else { | |
1684 | ret = btrfs_add_device(trans, root, device); | |
1685 | } | |
1686 | ||
913d952e CM |
1687 | /* |
1688 | * we've got more storage, clear any full flags on the space | |
1689 | * infos | |
1690 | */ | |
1691 | btrfs_clear_space_info_full(root->fs_info); | |
1692 | ||
7d9eb12c | 1693 | unlock_chunks(root); |
2b82032c | 1694 | btrfs_commit_transaction(trans, root); |
a2135011 | 1695 | |
2b82032c YZ |
1696 | if (seeding_dev) { |
1697 | mutex_unlock(&uuid_mutex); | |
1698 | up_write(&sb->s_umount); | |
788f20eb | 1699 | |
2b82032c YZ |
1700 | ret = btrfs_relocate_sys_chunks(root); |
1701 | BUG_ON(ret); | |
1702 | } | |
1703 | out: | |
1704 | mutex_unlock(&root->fs_info->volume_mutex); | |
1705 | return ret; | |
1706 | error: | |
15916de8 | 1707 | close_bdev_exclusive(bdev, 0); |
2b82032c YZ |
1708 | if (seeding_dev) { |
1709 | mutex_unlock(&uuid_mutex); | |
1710 | up_write(&sb->s_umount); | |
1711 | } | |
788f20eb CM |
1712 | goto out; |
1713 | } | |
1714 | ||
d397712b CM |
1715 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
1716 | struct btrfs_device *device) | |
0b86a832 CM |
1717 | { |
1718 | int ret; | |
1719 | struct btrfs_path *path; | |
1720 | struct btrfs_root *root; | |
1721 | struct btrfs_dev_item *dev_item; | |
1722 | struct extent_buffer *leaf; | |
1723 | struct btrfs_key key; | |
1724 | ||
1725 | root = device->dev_root->fs_info->chunk_root; | |
1726 | ||
1727 | path = btrfs_alloc_path(); | |
1728 | if (!path) | |
1729 | return -ENOMEM; | |
1730 | ||
1731 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
1732 | key.type = BTRFS_DEV_ITEM_KEY; | |
1733 | key.offset = device->devid; | |
1734 | ||
1735 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
1736 | if (ret < 0) | |
1737 | goto out; | |
1738 | ||
1739 | if (ret > 0) { | |
1740 | ret = -ENOENT; | |
1741 | goto out; | |
1742 | } | |
1743 | ||
1744 | leaf = path->nodes[0]; | |
1745 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); | |
1746 | ||
1747 | btrfs_set_device_id(leaf, dev_item, device->devid); | |
1748 | btrfs_set_device_type(leaf, dev_item, device->type); | |
1749 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); | |
1750 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); | |
1751 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); | |
d6397bae | 1752 | btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes); |
0b86a832 CM |
1753 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
1754 | btrfs_mark_buffer_dirty(leaf); | |
1755 | ||
1756 | out: | |
1757 | btrfs_free_path(path); | |
1758 | return ret; | |
1759 | } | |
1760 | ||
7d9eb12c | 1761 | static int __btrfs_grow_device(struct btrfs_trans_handle *trans, |
8f18cf13 CM |
1762 | struct btrfs_device *device, u64 new_size) |
1763 | { | |
1764 | struct btrfs_super_block *super_copy = | |
1765 | &device->dev_root->fs_info->super_copy; | |
1766 | u64 old_total = btrfs_super_total_bytes(super_copy); | |
1767 | u64 diff = new_size - device->total_bytes; | |
1768 | ||
2b82032c YZ |
1769 | if (!device->writeable) |
1770 | return -EACCES; | |
1771 | if (new_size <= device->total_bytes) | |
1772 | return -EINVAL; | |
1773 | ||
8f18cf13 | 1774 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
2b82032c YZ |
1775 | device->fs_devices->total_rw_bytes += diff; |
1776 | ||
1777 | device->total_bytes = new_size; | |
9779b72f | 1778 | device->disk_total_bytes = new_size; |
4184ea7f CM |
1779 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
1780 | ||
8f18cf13 CM |
1781 | return btrfs_update_device(trans, device); |
1782 | } | |
1783 | ||
7d9eb12c CM |
1784 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
1785 | struct btrfs_device *device, u64 new_size) | |
1786 | { | |
1787 | int ret; | |
1788 | lock_chunks(device->dev_root); | |
1789 | ret = __btrfs_grow_device(trans, device, new_size); | |
1790 | unlock_chunks(device->dev_root); | |
1791 | return ret; | |
1792 | } | |
1793 | ||
8f18cf13 CM |
1794 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
1795 | struct btrfs_root *root, | |
1796 | u64 chunk_tree, u64 chunk_objectid, | |
1797 | u64 chunk_offset) | |
1798 | { | |
1799 | int ret; | |
1800 | struct btrfs_path *path; | |
1801 | struct btrfs_key key; | |
1802 | ||
1803 | root = root->fs_info->chunk_root; | |
1804 | path = btrfs_alloc_path(); | |
1805 | if (!path) | |
1806 | return -ENOMEM; | |
1807 | ||
1808 | key.objectid = chunk_objectid; | |
1809 | key.offset = chunk_offset; | |
1810 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
1811 | ||
1812 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1813 | BUG_ON(ret); | |
1814 | ||
1815 | ret = btrfs_del_item(trans, root, path); | |
1816 | BUG_ON(ret); | |
1817 | ||
1818 | btrfs_free_path(path); | |
1819 | return 0; | |
1820 | } | |
1821 | ||
b2950863 | 1822 | static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64 |
8f18cf13 CM |
1823 | chunk_offset) |
1824 | { | |
1825 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
1826 | struct btrfs_disk_key *disk_key; | |
1827 | struct btrfs_chunk *chunk; | |
1828 | u8 *ptr; | |
1829 | int ret = 0; | |
1830 | u32 num_stripes; | |
1831 | u32 array_size; | |
1832 | u32 len = 0; | |
1833 | u32 cur; | |
1834 | struct btrfs_key key; | |
1835 | ||
1836 | array_size = btrfs_super_sys_array_size(super_copy); | |
1837 | ||
1838 | ptr = super_copy->sys_chunk_array; | |
1839 | cur = 0; | |
1840 | ||
1841 | while (cur < array_size) { | |
1842 | disk_key = (struct btrfs_disk_key *)ptr; | |
1843 | btrfs_disk_key_to_cpu(&key, disk_key); | |
1844 | ||
1845 | len = sizeof(*disk_key); | |
1846 | ||
1847 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { | |
1848 | chunk = (struct btrfs_chunk *)(ptr + len); | |
1849 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); | |
1850 | len += btrfs_chunk_item_size(num_stripes); | |
1851 | } else { | |
1852 | ret = -EIO; | |
1853 | break; | |
1854 | } | |
1855 | if (key.objectid == chunk_objectid && | |
1856 | key.offset == chunk_offset) { | |
1857 | memmove(ptr, ptr + len, array_size - (cur + len)); | |
1858 | array_size -= len; | |
1859 | btrfs_set_super_sys_array_size(super_copy, array_size); | |
1860 | } else { | |
1861 | ptr += len; | |
1862 | cur += len; | |
1863 | } | |
1864 | } | |
1865 | return ret; | |
1866 | } | |
1867 | ||
b2950863 | 1868 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
8f18cf13 CM |
1869 | u64 chunk_tree, u64 chunk_objectid, |
1870 | u64 chunk_offset) | |
1871 | { | |
1872 | struct extent_map_tree *em_tree; | |
1873 | struct btrfs_root *extent_root; | |
1874 | struct btrfs_trans_handle *trans; | |
1875 | struct extent_map *em; | |
1876 | struct map_lookup *map; | |
1877 | int ret; | |
1878 | int i; | |
1879 | ||
1880 | root = root->fs_info->chunk_root; | |
1881 | extent_root = root->fs_info->extent_root; | |
1882 | em_tree = &root->fs_info->mapping_tree.map_tree; | |
1883 | ||
ba1bf481 JB |
1884 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
1885 | if (ret) | |
1886 | return -ENOSPC; | |
1887 | ||
8f18cf13 | 1888 | /* step one, relocate all the extents inside this chunk */ |
1a40e23b | 1889 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
a22285a6 YZ |
1890 | if (ret) |
1891 | return ret; | |
8f18cf13 | 1892 | |
a22285a6 | 1893 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 | 1894 | BUG_ON(IS_ERR(trans)); |
8f18cf13 | 1895 | |
7d9eb12c CM |
1896 | lock_chunks(root); |
1897 | ||
8f18cf13 CM |
1898 | /* |
1899 | * step two, delete the device extents and the | |
1900 | * chunk tree entries | |
1901 | */ | |
890871be | 1902 | read_lock(&em_tree->lock); |
8f18cf13 | 1903 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
890871be | 1904 | read_unlock(&em_tree->lock); |
8f18cf13 | 1905 | |
a061fc8d CM |
1906 | BUG_ON(em->start > chunk_offset || |
1907 | em->start + em->len < chunk_offset); | |
8f18cf13 CM |
1908 | map = (struct map_lookup *)em->bdev; |
1909 | ||
1910 | for (i = 0; i < map->num_stripes; i++) { | |
1911 | ret = btrfs_free_dev_extent(trans, map->stripes[i].dev, | |
1912 | map->stripes[i].physical); | |
1913 | BUG_ON(ret); | |
a061fc8d | 1914 | |
dfe25020 CM |
1915 | if (map->stripes[i].dev) { |
1916 | ret = btrfs_update_device(trans, map->stripes[i].dev); | |
1917 | BUG_ON(ret); | |
1918 | } | |
8f18cf13 CM |
1919 | } |
1920 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, | |
1921 | chunk_offset); | |
1922 | ||
1923 | BUG_ON(ret); | |
1924 | ||
1925 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { | |
1926 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); | |
1927 | BUG_ON(ret); | |
8f18cf13 CM |
1928 | } |
1929 | ||
2b82032c YZ |
1930 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset); |
1931 | BUG_ON(ret); | |
1932 | ||
890871be | 1933 | write_lock(&em_tree->lock); |
2b82032c | 1934 | remove_extent_mapping(em_tree, em); |
890871be | 1935 | write_unlock(&em_tree->lock); |
2b82032c YZ |
1936 | |
1937 | kfree(map); | |
1938 | em->bdev = NULL; | |
1939 | ||
1940 | /* once for the tree */ | |
1941 | free_extent_map(em); | |
1942 | /* once for us */ | |
1943 | free_extent_map(em); | |
1944 | ||
1945 | unlock_chunks(root); | |
1946 | btrfs_end_transaction(trans, root); | |
1947 | return 0; | |
1948 | } | |
1949 | ||
1950 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) | |
1951 | { | |
1952 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; | |
1953 | struct btrfs_path *path; | |
1954 | struct extent_buffer *leaf; | |
1955 | struct btrfs_chunk *chunk; | |
1956 | struct btrfs_key key; | |
1957 | struct btrfs_key found_key; | |
1958 | u64 chunk_tree = chunk_root->root_key.objectid; | |
1959 | u64 chunk_type; | |
ba1bf481 JB |
1960 | bool retried = false; |
1961 | int failed = 0; | |
2b82032c YZ |
1962 | int ret; |
1963 | ||
1964 | path = btrfs_alloc_path(); | |
1965 | if (!path) | |
1966 | return -ENOMEM; | |
1967 | ||
ba1bf481 | 1968 | again: |
2b82032c YZ |
1969 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
1970 | key.offset = (u64)-1; | |
1971 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
1972 | ||
1973 | while (1) { | |
1974 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); | |
1975 | if (ret < 0) | |
1976 | goto error; | |
1977 | BUG_ON(ret == 0); | |
1978 | ||
1979 | ret = btrfs_previous_item(chunk_root, path, key.objectid, | |
1980 | key.type); | |
1981 | if (ret < 0) | |
1982 | goto error; | |
1983 | if (ret > 0) | |
1984 | break; | |
1a40e23b | 1985 | |
2b82032c YZ |
1986 | leaf = path->nodes[0]; |
1987 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1a40e23b | 1988 | |
2b82032c YZ |
1989 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
1990 | struct btrfs_chunk); | |
1991 | chunk_type = btrfs_chunk_type(leaf, chunk); | |
1992 | btrfs_release_path(chunk_root, path); | |
8f18cf13 | 1993 | |
2b82032c YZ |
1994 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
1995 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, | |
1996 | found_key.objectid, | |
1997 | found_key.offset); | |
ba1bf481 JB |
1998 | if (ret == -ENOSPC) |
1999 | failed++; | |
2000 | else if (ret) | |
2001 | BUG(); | |
2b82032c | 2002 | } |
8f18cf13 | 2003 | |
2b82032c YZ |
2004 | if (found_key.offset == 0) |
2005 | break; | |
2006 | key.offset = found_key.offset - 1; | |
2007 | } | |
2008 | ret = 0; | |
ba1bf481 JB |
2009 | if (failed && !retried) { |
2010 | failed = 0; | |
2011 | retried = true; | |
2012 | goto again; | |
2013 | } else if (failed && retried) { | |
2014 | WARN_ON(1); | |
2015 | ret = -ENOSPC; | |
2016 | } | |
2b82032c YZ |
2017 | error: |
2018 | btrfs_free_path(path); | |
2019 | return ret; | |
8f18cf13 CM |
2020 | } |
2021 | ||
ec44a35c CM |
2022 | static u64 div_factor(u64 num, int factor) |
2023 | { | |
2024 | if (factor == 10) | |
2025 | return num; | |
2026 | num *= factor; | |
2027 | do_div(num, 10); | |
2028 | return num; | |
2029 | } | |
2030 | ||
ec44a35c CM |
2031 | int btrfs_balance(struct btrfs_root *dev_root) |
2032 | { | |
2033 | int ret; | |
ec44a35c CM |
2034 | struct list_head *devices = &dev_root->fs_info->fs_devices->devices; |
2035 | struct btrfs_device *device; | |
2036 | u64 old_size; | |
2037 | u64 size_to_free; | |
2038 | struct btrfs_path *path; | |
2039 | struct btrfs_key key; | |
ec44a35c CM |
2040 | struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root; |
2041 | struct btrfs_trans_handle *trans; | |
2042 | struct btrfs_key found_key; | |
2043 | ||
2b82032c YZ |
2044 | if (dev_root->fs_info->sb->s_flags & MS_RDONLY) |
2045 | return -EROFS; | |
ec44a35c | 2046 | |
6f88a440 BH |
2047 | if (!capable(CAP_SYS_ADMIN)) |
2048 | return -EPERM; | |
2049 | ||
7d9eb12c | 2050 | mutex_lock(&dev_root->fs_info->volume_mutex); |
ec44a35c CM |
2051 | dev_root = dev_root->fs_info->dev_root; |
2052 | ||
ec44a35c | 2053 | /* step one make some room on all the devices */ |
c6e30871 | 2054 | list_for_each_entry(device, devices, dev_list) { |
ec44a35c CM |
2055 | old_size = device->total_bytes; |
2056 | size_to_free = div_factor(old_size, 1); | |
2057 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); | |
2b82032c YZ |
2058 | if (!device->writeable || |
2059 | device->total_bytes - device->bytes_used > size_to_free) | |
ec44a35c CM |
2060 | continue; |
2061 | ||
2062 | ret = btrfs_shrink_device(device, old_size - size_to_free); | |
ba1bf481 JB |
2063 | if (ret == -ENOSPC) |
2064 | break; | |
ec44a35c CM |
2065 | BUG_ON(ret); |
2066 | ||
a22285a6 | 2067 | trans = btrfs_start_transaction(dev_root, 0); |
98d5dc13 | 2068 | BUG_ON(IS_ERR(trans)); |
ec44a35c CM |
2069 | |
2070 | ret = btrfs_grow_device(trans, device, old_size); | |
2071 | BUG_ON(ret); | |
2072 | ||
2073 | btrfs_end_transaction(trans, dev_root); | |
2074 | } | |
2075 | ||
2076 | /* step two, relocate all the chunks */ | |
2077 | path = btrfs_alloc_path(); | |
2078 | BUG_ON(!path); | |
2079 | ||
2080 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; | |
2081 | key.offset = (u64)-1; | |
2082 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
2083 | ||
d397712b | 2084 | while (1) { |
ec44a35c CM |
2085 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
2086 | if (ret < 0) | |
2087 | goto error; | |
2088 | ||
2089 | /* | |
2090 | * this shouldn't happen, it means the last relocate | |
2091 | * failed | |
2092 | */ | |
2093 | if (ret == 0) | |
2094 | break; | |
2095 | ||
2096 | ret = btrfs_previous_item(chunk_root, path, 0, | |
2097 | BTRFS_CHUNK_ITEM_KEY); | |
7d9eb12c | 2098 | if (ret) |
ec44a35c | 2099 | break; |
7d9eb12c | 2100 | |
ec44a35c CM |
2101 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
2102 | path->slots[0]); | |
2103 | if (found_key.objectid != key.objectid) | |
2104 | break; | |
7d9eb12c | 2105 | |
ec44a35c | 2106 | /* chunk zero is special */ |
ba1bf481 | 2107 | if (found_key.offset == 0) |
ec44a35c CM |
2108 | break; |
2109 | ||
7d9eb12c | 2110 | btrfs_release_path(chunk_root, path); |
ec44a35c CM |
2111 | ret = btrfs_relocate_chunk(chunk_root, |
2112 | chunk_root->root_key.objectid, | |
2113 | found_key.objectid, | |
2114 | found_key.offset); | |
ba1bf481 JB |
2115 | BUG_ON(ret && ret != -ENOSPC); |
2116 | key.offset = found_key.offset - 1; | |
ec44a35c CM |
2117 | } |
2118 | ret = 0; | |
2119 | error: | |
2120 | btrfs_free_path(path); | |
7d9eb12c | 2121 | mutex_unlock(&dev_root->fs_info->volume_mutex); |
ec44a35c CM |
2122 | return ret; |
2123 | } | |
2124 | ||
8f18cf13 CM |
2125 | /* |
2126 | * shrinking a device means finding all of the device extents past | |
2127 | * the new size, and then following the back refs to the chunks. | |
2128 | * The chunk relocation code actually frees the device extent | |
2129 | */ | |
2130 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) | |
2131 | { | |
2132 | struct btrfs_trans_handle *trans; | |
2133 | struct btrfs_root *root = device->dev_root; | |
2134 | struct btrfs_dev_extent *dev_extent = NULL; | |
2135 | struct btrfs_path *path; | |
2136 | u64 length; | |
2137 | u64 chunk_tree; | |
2138 | u64 chunk_objectid; | |
2139 | u64 chunk_offset; | |
2140 | int ret; | |
2141 | int slot; | |
ba1bf481 JB |
2142 | int failed = 0; |
2143 | bool retried = false; | |
8f18cf13 CM |
2144 | struct extent_buffer *l; |
2145 | struct btrfs_key key; | |
2146 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
2147 | u64 old_total = btrfs_super_total_bytes(super_copy); | |
ba1bf481 | 2148 | u64 old_size = device->total_bytes; |
8f18cf13 CM |
2149 | u64 diff = device->total_bytes - new_size; |
2150 | ||
2b82032c YZ |
2151 | if (new_size >= device->total_bytes) |
2152 | return -EINVAL; | |
8f18cf13 CM |
2153 | |
2154 | path = btrfs_alloc_path(); | |
2155 | if (!path) | |
2156 | return -ENOMEM; | |
2157 | ||
8f18cf13 CM |
2158 | path->reada = 2; |
2159 | ||
7d9eb12c CM |
2160 | lock_chunks(root); |
2161 | ||
8f18cf13 | 2162 | device->total_bytes = new_size; |
2b82032c YZ |
2163 | if (device->writeable) |
2164 | device->fs_devices->total_rw_bytes -= diff; | |
7d9eb12c | 2165 | unlock_chunks(root); |
8f18cf13 | 2166 | |
ba1bf481 | 2167 | again: |
8f18cf13 CM |
2168 | key.objectid = device->devid; |
2169 | key.offset = (u64)-1; | |
2170 | key.type = BTRFS_DEV_EXTENT_KEY; | |
2171 | ||
2172 | while (1) { | |
2173 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2174 | if (ret < 0) | |
2175 | goto done; | |
2176 | ||
2177 | ret = btrfs_previous_item(root, path, 0, key.type); | |
2178 | if (ret < 0) | |
2179 | goto done; | |
2180 | if (ret) { | |
2181 | ret = 0; | |
ba1bf481 | 2182 | btrfs_release_path(root, path); |
bf1fb512 | 2183 | break; |
8f18cf13 CM |
2184 | } |
2185 | ||
2186 | l = path->nodes[0]; | |
2187 | slot = path->slots[0]; | |
2188 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | |
2189 | ||
ba1bf481 JB |
2190 | if (key.objectid != device->devid) { |
2191 | btrfs_release_path(root, path); | |
bf1fb512 | 2192 | break; |
ba1bf481 | 2193 | } |
8f18cf13 CM |
2194 | |
2195 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); | |
2196 | length = btrfs_dev_extent_length(l, dev_extent); | |
2197 | ||
ba1bf481 JB |
2198 | if (key.offset + length <= new_size) { |
2199 | btrfs_release_path(root, path); | |
d6397bae | 2200 | break; |
ba1bf481 | 2201 | } |
8f18cf13 CM |
2202 | |
2203 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); | |
2204 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); | |
2205 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); | |
2206 | btrfs_release_path(root, path); | |
2207 | ||
2208 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, | |
2209 | chunk_offset); | |
ba1bf481 | 2210 | if (ret && ret != -ENOSPC) |
8f18cf13 | 2211 | goto done; |
ba1bf481 JB |
2212 | if (ret == -ENOSPC) |
2213 | failed++; | |
2214 | key.offset -= 1; | |
2215 | } | |
2216 | ||
2217 | if (failed && !retried) { | |
2218 | failed = 0; | |
2219 | retried = true; | |
2220 | goto again; | |
2221 | } else if (failed && retried) { | |
2222 | ret = -ENOSPC; | |
2223 | lock_chunks(root); | |
2224 | ||
2225 | device->total_bytes = old_size; | |
2226 | if (device->writeable) | |
2227 | device->fs_devices->total_rw_bytes += diff; | |
2228 | unlock_chunks(root); | |
2229 | goto done; | |
8f18cf13 CM |
2230 | } |
2231 | ||
d6397bae | 2232 | /* Shrinking succeeded, else we would be at "done". */ |
a22285a6 | 2233 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
2234 | if (IS_ERR(trans)) { |
2235 | ret = PTR_ERR(trans); | |
2236 | goto done; | |
2237 | } | |
2238 | ||
d6397bae CB |
2239 | lock_chunks(root); |
2240 | ||
2241 | device->disk_total_bytes = new_size; | |
2242 | /* Now btrfs_update_device() will change the on-disk size. */ | |
2243 | ret = btrfs_update_device(trans, device); | |
2244 | if (ret) { | |
2245 | unlock_chunks(root); | |
2246 | btrfs_end_transaction(trans, root); | |
2247 | goto done; | |
2248 | } | |
2249 | WARN_ON(diff > old_total); | |
2250 | btrfs_set_super_total_bytes(super_copy, old_total - diff); | |
2251 | unlock_chunks(root); | |
2252 | btrfs_end_transaction(trans, root); | |
8f18cf13 CM |
2253 | done: |
2254 | btrfs_free_path(path); | |
2255 | return ret; | |
2256 | } | |
2257 | ||
b2950863 | 2258 | static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans, |
0b86a832 CM |
2259 | struct btrfs_root *root, |
2260 | struct btrfs_key *key, | |
2261 | struct btrfs_chunk *chunk, int item_size) | |
2262 | { | |
2263 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
2264 | struct btrfs_disk_key disk_key; | |
2265 | u32 array_size; | |
2266 | u8 *ptr; | |
2267 | ||
2268 | array_size = btrfs_super_sys_array_size(super_copy); | |
2269 | if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) | |
2270 | return -EFBIG; | |
2271 | ||
2272 | ptr = super_copy->sys_chunk_array + array_size; | |
2273 | btrfs_cpu_key_to_disk(&disk_key, key); | |
2274 | memcpy(ptr, &disk_key, sizeof(disk_key)); | |
2275 | ptr += sizeof(disk_key); | |
2276 | memcpy(ptr, chunk, item_size); | |
2277 | item_size += sizeof(disk_key); | |
2278 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); | |
2279 | return 0; | |
2280 | } | |
2281 | ||
d397712b | 2282 | static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size, |
a1b32a59 | 2283 | int num_stripes, int sub_stripes) |
9b3f68b9 CM |
2284 | { |
2285 | if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP)) | |
2286 | return calc_size; | |
2287 | else if (type & BTRFS_BLOCK_GROUP_RAID10) | |
2288 | return calc_size * (num_stripes / sub_stripes); | |
2289 | else | |
2290 | return calc_size * num_stripes; | |
2291 | } | |
2292 | ||
b2117a39 MX |
2293 | /* Used to sort the devices by max_avail(descending sort) */ |
2294 | int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2) | |
0b86a832 | 2295 | { |
b2117a39 MX |
2296 | if (((struct btrfs_device_info *)dev_info1)->max_avail > |
2297 | ((struct btrfs_device_info *)dev_info2)->max_avail) | |
2298 | return -1; | |
2299 | else if (((struct btrfs_device_info *)dev_info1)->max_avail < | |
2300 | ((struct btrfs_device_info *)dev_info2)->max_avail) | |
2301 | return 1; | |
2302 | else | |
2303 | return 0; | |
2304 | } | |
0b86a832 | 2305 | |
b2117a39 MX |
2306 | static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type, |
2307 | int *num_stripes, int *min_stripes, | |
2308 | int *sub_stripes) | |
2309 | { | |
2310 | *num_stripes = 1; | |
2311 | *min_stripes = 1; | |
2312 | *sub_stripes = 0; | |
593060d7 | 2313 | |
a40a90a0 | 2314 | if (type & (BTRFS_BLOCK_GROUP_RAID0)) { |
b2117a39 MX |
2315 | *num_stripes = fs_devices->rw_devices; |
2316 | *min_stripes = 2; | |
a40a90a0 CM |
2317 | } |
2318 | if (type & (BTRFS_BLOCK_GROUP_DUP)) { | |
b2117a39 MX |
2319 | *num_stripes = 2; |
2320 | *min_stripes = 2; | |
a40a90a0 | 2321 | } |
8790d502 | 2322 | if (type & (BTRFS_BLOCK_GROUP_RAID1)) { |
f3eae7e8 | 2323 | if (fs_devices->rw_devices < 2) |
9b3f68b9 | 2324 | return -ENOSPC; |
b2117a39 MX |
2325 | *num_stripes = 2; |
2326 | *min_stripes = 2; | |
8790d502 | 2327 | } |
321aecc6 | 2328 | if (type & (BTRFS_BLOCK_GROUP_RAID10)) { |
b2117a39 MX |
2329 | *num_stripes = fs_devices->rw_devices; |
2330 | if (*num_stripes < 4) | |
321aecc6 | 2331 | return -ENOSPC; |
b2117a39 MX |
2332 | *num_stripes &= ~(u32)1; |
2333 | *sub_stripes = 2; | |
2334 | *min_stripes = 4; | |
321aecc6 | 2335 | } |
9b3f68b9 | 2336 | |
b2117a39 MX |
2337 | return 0; |
2338 | } | |
2339 | ||
2340 | static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices, | |
2341 | u64 proposed_size, u64 type, | |
2342 | int num_stripes, int small_stripe) | |
2343 | { | |
2344 | int min_stripe_size = 1 * 1024 * 1024; | |
2345 | u64 calc_size = proposed_size; | |
2346 | u64 max_chunk_size = calc_size; | |
2347 | int ncopies = 1; | |
2348 | ||
2349 | if (type & (BTRFS_BLOCK_GROUP_RAID1 | | |
2350 | BTRFS_BLOCK_GROUP_DUP | | |
2351 | BTRFS_BLOCK_GROUP_RAID10)) | |
2352 | ncopies = 2; | |
2353 | ||
9b3f68b9 CM |
2354 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
2355 | max_chunk_size = 10 * calc_size; | |
a40a90a0 | 2356 | min_stripe_size = 64 * 1024 * 1024; |
9b3f68b9 | 2357 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
83d3c969 | 2358 | max_chunk_size = 256 * 1024 * 1024; |
a40a90a0 CM |
2359 | min_stripe_size = 32 * 1024 * 1024; |
2360 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { | |
2361 | calc_size = 8 * 1024 * 1024; | |
2362 | max_chunk_size = calc_size * 2; | |
2363 | min_stripe_size = 1 * 1024 * 1024; | |
9b3f68b9 CM |
2364 | } |
2365 | ||
2b82032c YZ |
2366 | /* we don't want a chunk larger than 10% of writeable space */ |
2367 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), | |
2368 | max_chunk_size); | |
9b3f68b9 | 2369 | |
1974a3b4 MX |
2370 | if (calc_size * num_stripes > max_chunk_size * ncopies) { |
2371 | calc_size = max_chunk_size * ncopies; | |
9b3f68b9 | 2372 | do_div(calc_size, num_stripes); |
b2117a39 MX |
2373 | do_div(calc_size, BTRFS_STRIPE_LEN); |
2374 | calc_size *= BTRFS_STRIPE_LEN; | |
9b3f68b9 | 2375 | } |
0cad8a11 | 2376 | |
9b3f68b9 | 2377 | /* we don't want tiny stripes */ |
b2117a39 | 2378 | if (!small_stripe) |
0cad8a11 | 2379 | calc_size = max_t(u64, min_stripe_size, calc_size); |
9b3f68b9 | 2380 | |
9f680ce0 | 2381 | /* |
b2117a39 | 2382 | * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure |
9f680ce0 CM |
2383 | * we end up with something bigger than a stripe |
2384 | */ | |
b2117a39 MX |
2385 | calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN); |
2386 | ||
2387 | do_div(calc_size, BTRFS_STRIPE_LEN); | |
2388 | calc_size *= BTRFS_STRIPE_LEN; | |
2389 | ||
2390 | return calc_size; | |
2391 | } | |
2392 | ||
2393 | static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map, | |
2394 | int num_stripes) | |
2395 | { | |
2396 | struct map_lookup *new; | |
2397 | size_t len = map_lookup_size(num_stripes); | |
2398 | ||
2399 | BUG_ON(map->num_stripes < num_stripes); | |
2400 | ||
2401 | if (map->num_stripes == num_stripes) | |
2402 | return map; | |
2403 | ||
2404 | new = kmalloc(len, GFP_NOFS); | |
2405 | if (!new) { | |
2406 | /* just change map->num_stripes */ | |
2407 | map->num_stripes = num_stripes; | |
2408 | return map; | |
2409 | } | |
2410 | ||
2411 | memcpy(new, map, len); | |
2412 | new->num_stripes = num_stripes; | |
2413 | kfree(map); | |
2414 | return new; | |
2415 | } | |
2416 | ||
2417 | /* | |
2418 | * helper to allocate device space from btrfs_device_info, in which we stored | |
2419 | * max free space information of every device. It is used when we can not | |
2420 | * allocate chunks by default size. | |
2421 | * | |
2422 | * By this helper, we can allocate a new chunk as larger as possible. | |
2423 | */ | |
2424 | static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans, | |
2425 | struct btrfs_fs_devices *fs_devices, | |
2426 | struct btrfs_device_info *devices, | |
2427 | int nr_device, u64 type, | |
2428 | struct map_lookup **map_lookup, | |
2429 | int min_stripes, u64 *stripe_size) | |
2430 | { | |
2431 | int i, index, sort_again = 0; | |
2432 | int min_devices = min_stripes; | |
2433 | u64 max_avail, min_free; | |
2434 | struct map_lookup *map = *map_lookup; | |
2435 | int ret; | |
9f680ce0 | 2436 | |
b2117a39 MX |
2437 | if (nr_device < min_stripes) |
2438 | return -ENOSPC; | |
2439 | ||
2440 | btrfs_descending_sort_devices(devices, nr_device); | |
2441 | ||
2442 | max_avail = devices[0].max_avail; | |
2443 | if (!max_avail) | |
2444 | return -ENOSPC; | |
2445 | ||
2446 | for (i = 0; i < nr_device; i++) { | |
2447 | /* | |
2448 | * if dev_offset = 0, it means the free space of this device | |
2449 | * is less than what we need, and we didn't search max avail | |
2450 | * extent on this device, so do it now. | |
2451 | */ | |
2452 | if (!devices[i].dev_offset) { | |
2453 | ret = find_free_dev_extent(trans, devices[i].dev, | |
2454 | max_avail, | |
2455 | &devices[i].dev_offset, | |
2456 | &devices[i].max_avail); | |
2457 | if (ret != 0 && ret != -ENOSPC) | |
2458 | return ret; | |
2459 | sort_again = 1; | |
2460 | } | |
2461 | } | |
2462 | ||
2463 | /* we update the max avail free extent of each devices, sort again */ | |
2464 | if (sort_again) | |
2465 | btrfs_descending_sort_devices(devices, nr_device); | |
2466 | ||
2467 | if (type & BTRFS_BLOCK_GROUP_DUP) | |
2468 | min_devices = 1; | |
2469 | ||
2470 | if (!devices[min_devices - 1].max_avail) | |
2471 | return -ENOSPC; | |
2472 | ||
2473 | max_avail = devices[min_devices - 1].max_avail; | |
2474 | if (type & BTRFS_BLOCK_GROUP_DUP) | |
2475 | do_div(max_avail, 2); | |
2476 | ||
2477 | max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type, | |
2478 | min_stripes, 1); | |
2479 | if (type & BTRFS_BLOCK_GROUP_DUP) | |
2480 | min_free = max_avail * 2; | |
2481 | else | |
2482 | min_free = max_avail; | |
2483 | ||
2484 | if (min_free > devices[min_devices - 1].max_avail) | |
2485 | return -ENOSPC; | |
2486 | ||
2487 | map = __shrink_map_lookup_stripes(map, min_stripes); | |
2488 | *stripe_size = max_avail; | |
2489 | ||
2490 | index = 0; | |
2491 | for (i = 0; i < min_stripes; i++) { | |
2492 | map->stripes[i].dev = devices[index].dev; | |
2493 | map->stripes[i].physical = devices[index].dev_offset; | |
2494 | if (type & BTRFS_BLOCK_GROUP_DUP) { | |
2495 | i++; | |
2496 | map->stripes[i].dev = devices[index].dev; | |
2497 | map->stripes[i].physical = devices[index].dev_offset + | |
2498 | max_avail; | |
2499 | } | |
2500 | index++; | |
2501 | } | |
2502 | *map_lookup = map; | |
2503 | ||
2504 | return 0; | |
2505 | } | |
2506 | ||
2507 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, | |
2508 | struct btrfs_root *extent_root, | |
2509 | struct map_lookup **map_ret, | |
2510 | u64 *num_bytes, u64 *stripe_size, | |
2511 | u64 start, u64 type) | |
2512 | { | |
2513 | struct btrfs_fs_info *info = extent_root->fs_info; | |
2514 | struct btrfs_device *device = NULL; | |
2515 | struct btrfs_fs_devices *fs_devices = info->fs_devices; | |
2516 | struct list_head *cur; | |
2517 | struct map_lookup *map; | |
2518 | struct extent_map_tree *em_tree; | |
2519 | struct extent_map *em; | |
2520 | struct btrfs_device_info *devices_info; | |
2521 | struct list_head private_devs; | |
2522 | u64 calc_size = 1024 * 1024 * 1024; | |
2523 | u64 min_free; | |
2524 | u64 avail; | |
2525 | u64 dev_offset; | |
2526 | int num_stripes; | |
2527 | int min_stripes; | |
2528 | int sub_stripes; | |
2529 | int min_devices; /* the min number of devices we need */ | |
2530 | int i; | |
2531 | int ret; | |
2532 | int index; | |
2533 | ||
2534 | if ((type & BTRFS_BLOCK_GROUP_RAID1) && | |
2535 | (type & BTRFS_BLOCK_GROUP_DUP)) { | |
2536 | WARN_ON(1); | |
2537 | type &= ~BTRFS_BLOCK_GROUP_DUP; | |
2538 | } | |
2539 | if (list_empty(&fs_devices->alloc_list)) | |
2540 | return -ENOSPC; | |
2541 | ||
2542 | ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes, | |
2543 | &min_stripes, &sub_stripes); | |
2544 | if (ret) | |
2545 | return ret; | |
2546 | ||
2547 | devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices, | |
2548 | GFP_NOFS); | |
2549 | if (!devices_info) | |
2550 | return -ENOMEM; | |
2551 | ||
2552 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); | |
2553 | if (!map) { | |
2554 | ret = -ENOMEM; | |
2555 | goto error; | |
2556 | } | |
2557 | map->num_stripes = num_stripes; | |
9b3f68b9 | 2558 | |
2b82032c | 2559 | cur = fs_devices->alloc_list.next; |
6324fbf3 | 2560 | index = 0; |
b2117a39 | 2561 | i = 0; |
611f0e00 | 2562 | |
b2117a39 MX |
2563 | calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type, |
2564 | num_stripes, 0); | |
2565 | ||
2566 | if (type & BTRFS_BLOCK_GROUP_DUP) { | |
611f0e00 | 2567 | min_free = calc_size * 2; |
b2117a39 MX |
2568 | min_devices = 1; |
2569 | } else { | |
9b3f68b9 | 2570 | min_free = calc_size; |
b2117a39 MX |
2571 | min_devices = min_stripes; |
2572 | } | |
ad5bd91e | 2573 | |
2b82032c | 2574 | INIT_LIST_HEAD(&private_devs); |
d397712b | 2575 | while (index < num_stripes) { |
b3075717 | 2576 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
2b82032c | 2577 | BUG_ON(!device->writeable); |
dfe25020 CM |
2578 | if (device->total_bytes > device->bytes_used) |
2579 | avail = device->total_bytes - device->bytes_used; | |
2580 | else | |
2581 | avail = 0; | |
6324fbf3 | 2582 | cur = cur->next; |
8f18cf13 | 2583 | |
dfe25020 | 2584 | if (device->in_fs_metadata && avail >= min_free) { |
b2117a39 MX |
2585 | ret = find_free_dev_extent(trans, device, min_free, |
2586 | &devices_info[i].dev_offset, | |
2587 | &devices_info[i].max_avail); | |
8f18cf13 CM |
2588 | if (ret == 0) { |
2589 | list_move_tail(&device->dev_alloc_list, | |
2590 | &private_devs); | |
2b82032c | 2591 | map->stripes[index].dev = device; |
b2117a39 MX |
2592 | map->stripes[index].physical = |
2593 | devices_info[i].dev_offset; | |
611f0e00 | 2594 | index++; |
2b82032c YZ |
2595 | if (type & BTRFS_BLOCK_GROUP_DUP) { |
2596 | map->stripes[index].dev = device; | |
2597 | map->stripes[index].physical = | |
b2117a39 MX |
2598 | devices_info[i].dev_offset + |
2599 | calc_size; | |
8f18cf13 | 2600 | index++; |
2b82032c | 2601 | } |
b2117a39 MX |
2602 | } else if (ret != -ENOSPC) |
2603 | goto error; | |
2604 | ||
2605 | devices_info[i].dev = device; | |
2606 | i++; | |
2607 | } else if (device->in_fs_metadata && | |
2608 | avail >= BTRFS_STRIPE_LEN) { | |
2609 | devices_info[i].dev = device; | |
2610 | devices_info[i].max_avail = avail; | |
2611 | i++; | |
2612 | } | |
2613 | ||
2b82032c | 2614 | if (cur == &fs_devices->alloc_list) |
6324fbf3 CM |
2615 | break; |
2616 | } | |
b2117a39 | 2617 | |
2b82032c | 2618 | list_splice(&private_devs, &fs_devices->alloc_list); |
6324fbf3 | 2619 | if (index < num_stripes) { |
a40a90a0 CM |
2620 | if (index >= min_stripes) { |
2621 | num_stripes = index; | |
2622 | if (type & (BTRFS_BLOCK_GROUP_RAID10)) { | |
2623 | num_stripes /= sub_stripes; | |
2624 | num_stripes *= sub_stripes; | |
2625 | } | |
b2117a39 MX |
2626 | |
2627 | map = __shrink_map_lookup_stripes(map, num_stripes); | |
2628 | } else if (i >= min_devices) { | |
2629 | ret = __btrfs_alloc_tiny_space(trans, fs_devices, | |
2630 | devices_info, i, type, | |
2631 | &map, min_stripes, | |
2632 | &calc_size); | |
2633 | if (ret) | |
2634 | goto error; | |
2635 | } else { | |
2636 | ret = -ENOSPC; | |
2637 | goto error; | |
6324fbf3 | 2638 | } |
6324fbf3 | 2639 | } |
2b82032c | 2640 | map->sector_size = extent_root->sectorsize; |
b2117a39 MX |
2641 | map->stripe_len = BTRFS_STRIPE_LEN; |
2642 | map->io_align = BTRFS_STRIPE_LEN; | |
2643 | map->io_width = BTRFS_STRIPE_LEN; | |
2b82032c | 2644 | map->type = type; |
2b82032c | 2645 | map->sub_stripes = sub_stripes; |
0b86a832 | 2646 | |
2b82032c YZ |
2647 | *map_ret = map; |
2648 | *stripe_size = calc_size; | |
2649 | *num_bytes = chunk_bytes_by_type(type, calc_size, | |
b2117a39 | 2650 | map->num_stripes, sub_stripes); |
0b86a832 | 2651 | |
2b82032c YZ |
2652 | em = alloc_extent_map(GFP_NOFS); |
2653 | if (!em) { | |
b2117a39 MX |
2654 | ret = -ENOMEM; |
2655 | goto error; | |
593060d7 | 2656 | } |
2b82032c YZ |
2657 | em->bdev = (struct block_device *)map; |
2658 | em->start = start; | |
2659 | em->len = *num_bytes; | |
2660 | em->block_start = 0; | |
2661 | em->block_len = em->len; | |
593060d7 | 2662 | |
2b82032c | 2663 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
890871be | 2664 | write_lock(&em_tree->lock); |
2b82032c | 2665 | ret = add_extent_mapping(em_tree, em); |
890871be | 2666 | write_unlock(&em_tree->lock); |
2b82032c YZ |
2667 | BUG_ON(ret); |
2668 | free_extent_map(em); | |
0b86a832 | 2669 | |
2b82032c YZ |
2670 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
2671 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, | |
2672 | start, *num_bytes); | |
2673 | BUG_ON(ret); | |
611f0e00 | 2674 | |
2b82032c YZ |
2675 | index = 0; |
2676 | while (index < map->num_stripes) { | |
2677 | device = map->stripes[index].dev; | |
2678 | dev_offset = map->stripes[index].physical; | |
0b86a832 CM |
2679 | |
2680 | ret = btrfs_alloc_dev_extent(trans, device, | |
2b82032c YZ |
2681 | info->chunk_root->root_key.objectid, |
2682 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, | |
2683 | start, dev_offset, calc_size); | |
0b86a832 | 2684 | BUG_ON(ret); |
2b82032c YZ |
2685 | index++; |
2686 | } | |
2687 | ||
b2117a39 | 2688 | kfree(devices_info); |
2b82032c | 2689 | return 0; |
b2117a39 MX |
2690 | |
2691 | error: | |
2692 | kfree(map); | |
2693 | kfree(devices_info); | |
2694 | return ret; | |
2b82032c YZ |
2695 | } |
2696 | ||
2697 | static int __finish_chunk_alloc(struct btrfs_trans_handle *trans, | |
2698 | struct btrfs_root *extent_root, | |
2699 | struct map_lookup *map, u64 chunk_offset, | |
2700 | u64 chunk_size, u64 stripe_size) | |
2701 | { | |
2702 | u64 dev_offset; | |
2703 | struct btrfs_key key; | |
2704 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; | |
2705 | struct btrfs_device *device; | |
2706 | struct btrfs_chunk *chunk; | |
2707 | struct btrfs_stripe *stripe; | |
2708 | size_t item_size = btrfs_chunk_item_size(map->num_stripes); | |
2709 | int index = 0; | |
2710 | int ret; | |
2711 | ||
2712 | chunk = kzalloc(item_size, GFP_NOFS); | |
2713 | if (!chunk) | |
2714 | return -ENOMEM; | |
2715 | ||
2716 | index = 0; | |
2717 | while (index < map->num_stripes) { | |
2718 | device = map->stripes[index].dev; | |
2719 | device->bytes_used += stripe_size; | |
0b86a832 CM |
2720 | ret = btrfs_update_device(trans, device); |
2721 | BUG_ON(ret); | |
2b82032c YZ |
2722 | index++; |
2723 | } | |
2724 | ||
2725 | index = 0; | |
2726 | stripe = &chunk->stripe; | |
2727 | while (index < map->num_stripes) { | |
2728 | device = map->stripes[index].dev; | |
2729 | dev_offset = map->stripes[index].physical; | |
0b86a832 | 2730 | |
e17cade2 CM |
2731 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
2732 | btrfs_set_stack_stripe_offset(stripe, dev_offset); | |
2733 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); | |
2b82032c | 2734 | stripe++; |
0b86a832 CM |
2735 | index++; |
2736 | } | |
2737 | ||
2b82032c | 2738 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
0b86a832 | 2739 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
2b82032c YZ |
2740 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
2741 | btrfs_set_stack_chunk_type(chunk, map->type); | |
2742 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); | |
2743 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); | |
2744 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); | |
0b86a832 | 2745 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
2b82032c | 2746 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
0b86a832 | 2747 | |
2b82032c YZ |
2748 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
2749 | key.type = BTRFS_CHUNK_ITEM_KEY; | |
2750 | key.offset = chunk_offset; | |
0b86a832 | 2751 | |
2b82032c YZ |
2752 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
2753 | BUG_ON(ret); | |
0b86a832 | 2754 | |
2b82032c YZ |
2755 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
2756 | ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk, | |
2757 | item_size); | |
8f18cf13 CM |
2758 | BUG_ON(ret); |
2759 | } | |
0b86a832 | 2760 | kfree(chunk); |
2b82032c YZ |
2761 | return 0; |
2762 | } | |
0b86a832 | 2763 | |
2b82032c YZ |
2764 | /* |
2765 | * Chunk allocation falls into two parts. The first part does works | |
2766 | * that make the new allocated chunk useable, but not do any operation | |
2767 | * that modifies the chunk tree. The second part does the works that | |
2768 | * require modifying the chunk tree. This division is important for the | |
2769 | * bootstrap process of adding storage to a seed btrfs. | |
2770 | */ | |
2771 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, | |
2772 | struct btrfs_root *extent_root, u64 type) | |
2773 | { | |
2774 | u64 chunk_offset; | |
2775 | u64 chunk_size; | |
2776 | u64 stripe_size; | |
2777 | struct map_lookup *map; | |
2778 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; | |
2779 | int ret; | |
2780 | ||
2781 | ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID, | |
2782 | &chunk_offset); | |
2783 | if (ret) | |
2784 | return ret; | |
2785 | ||
2786 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, | |
2787 | &stripe_size, chunk_offset, type); | |
2788 | if (ret) | |
2789 | return ret; | |
2790 | ||
2791 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, | |
2792 | chunk_size, stripe_size); | |
2793 | BUG_ON(ret); | |
2794 | return 0; | |
2795 | } | |
2796 | ||
d397712b | 2797 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
2b82032c YZ |
2798 | struct btrfs_root *root, |
2799 | struct btrfs_device *device) | |
2800 | { | |
2801 | u64 chunk_offset; | |
2802 | u64 sys_chunk_offset; | |
2803 | u64 chunk_size; | |
2804 | u64 sys_chunk_size; | |
2805 | u64 stripe_size; | |
2806 | u64 sys_stripe_size; | |
2807 | u64 alloc_profile; | |
2808 | struct map_lookup *map; | |
2809 | struct map_lookup *sys_map; | |
2810 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2811 | struct btrfs_root *extent_root = fs_info->extent_root; | |
2812 | int ret; | |
2813 | ||
2814 | ret = find_next_chunk(fs_info->chunk_root, | |
2815 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset); | |
2816 | BUG_ON(ret); | |
2817 | ||
2818 | alloc_profile = BTRFS_BLOCK_GROUP_METADATA | | |
2819 | (fs_info->metadata_alloc_profile & | |
2820 | fs_info->avail_metadata_alloc_bits); | |
2821 | alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile); | |
2822 | ||
2823 | ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size, | |
2824 | &stripe_size, chunk_offset, alloc_profile); | |
2825 | BUG_ON(ret); | |
2826 | ||
2827 | sys_chunk_offset = chunk_offset + chunk_size; | |
2828 | ||
2829 | alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM | | |
2830 | (fs_info->system_alloc_profile & | |
2831 | fs_info->avail_system_alloc_bits); | |
2832 | alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile); | |
2833 | ||
2834 | ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map, | |
2835 | &sys_chunk_size, &sys_stripe_size, | |
2836 | sys_chunk_offset, alloc_profile); | |
2837 | BUG_ON(ret); | |
2838 | ||
2839 | ret = btrfs_add_device(trans, fs_info->chunk_root, device); | |
2840 | BUG_ON(ret); | |
2841 | ||
2842 | /* | |
2843 | * Modifying chunk tree needs allocating new blocks from both | |
2844 | * system block group and metadata block group. So we only can | |
2845 | * do operations require modifying the chunk tree after both | |
2846 | * block groups were created. | |
2847 | */ | |
2848 | ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset, | |
2849 | chunk_size, stripe_size); | |
2850 | BUG_ON(ret); | |
2851 | ||
2852 | ret = __finish_chunk_alloc(trans, extent_root, sys_map, | |
2853 | sys_chunk_offset, sys_chunk_size, | |
2854 | sys_stripe_size); | |
b248a415 | 2855 | BUG_ON(ret); |
2b82032c YZ |
2856 | return 0; |
2857 | } | |
2858 | ||
2859 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) | |
2860 | { | |
2861 | struct extent_map *em; | |
2862 | struct map_lookup *map; | |
2863 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; | |
2864 | int readonly = 0; | |
2865 | int i; | |
2866 | ||
890871be | 2867 | read_lock(&map_tree->map_tree.lock); |
2b82032c | 2868 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
890871be | 2869 | read_unlock(&map_tree->map_tree.lock); |
2b82032c YZ |
2870 | if (!em) |
2871 | return 1; | |
2872 | ||
f48b9075 JB |
2873 | if (btrfs_test_opt(root, DEGRADED)) { |
2874 | free_extent_map(em); | |
2875 | return 0; | |
2876 | } | |
2877 | ||
2b82032c YZ |
2878 | map = (struct map_lookup *)em->bdev; |
2879 | for (i = 0; i < map->num_stripes; i++) { | |
2880 | if (!map->stripes[i].dev->writeable) { | |
2881 | readonly = 1; | |
2882 | break; | |
2883 | } | |
2884 | } | |
0b86a832 | 2885 | free_extent_map(em); |
2b82032c | 2886 | return readonly; |
0b86a832 CM |
2887 | } |
2888 | ||
2889 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) | |
2890 | { | |
2891 | extent_map_tree_init(&tree->map_tree, GFP_NOFS); | |
2892 | } | |
2893 | ||
2894 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) | |
2895 | { | |
2896 | struct extent_map *em; | |
2897 | ||
d397712b | 2898 | while (1) { |
890871be | 2899 | write_lock(&tree->map_tree.lock); |
0b86a832 CM |
2900 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
2901 | if (em) | |
2902 | remove_extent_mapping(&tree->map_tree, em); | |
890871be | 2903 | write_unlock(&tree->map_tree.lock); |
0b86a832 CM |
2904 | if (!em) |
2905 | break; | |
2906 | kfree(em->bdev); | |
2907 | /* once for us */ | |
2908 | free_extent_map(em); | |
2909 | /* once for the tree */ | |
2910 | free_extent_map(em); | |
2911 | } | |
2912 | } | |
2913 | ||
f188591e CM |
2914 | int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len) |
2915 | { | |
2916 | struct extent_map *em; | |
2917 | struct map_lookup *map; | |
2918 | struct extent_map_tree *em_tree = &map_tree->map_tree; | |
2919 | int ret; | |
2920 | ||
890871be | 2921 | read_lock(&em_tree->lock); |
f188591e | 2922 | em = lookup_extent_mapping(em_tree, logical, len); |
890871be | 2923 | read_unlock(&em_tree->lock); |
f188591e CM |
2924 | BUG_ON(!em); |
2925 | ||
2926 | BUG_ON(em->start > logical || em->start + em->len < logical); | |
2927 | map = (struct map_lookup *)em->bdev; | |
2928 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) | |
2929 | ret = map->num_stripes; | |
321aecc6 CM |
2930 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
2931 | ret = map->sub_stripes; | |
f188591e CM |
2932 | else |
2933 | ret = 1; | |
2934 | free_extent_map(em); | |
f188591e CM |
2935 | return ret; |
2936 | } | |
2937 | ||
dfe25020 CM |
2938 | static int find_live_mirror(struct map_lookup *map, int first, int num, |
2939 | int optimal) | |
2940 | { | |
2941 | int i; | |
2942 | if (map->stripes[optimal].dev->bdev) | |
2943 | return optimal; | |
2944 | for (i = first; i < first + num; i++) { | |
2945 | if (map->stripes[i].dev->bdev) | |
2946 | return i; | |
2947 | } | |
2948 | /* we couldn't find one that doesn't fail. Just return something | |
2949 | * and the io error handling code will clean up eventually | |
2950 | */ | |
2951 | return optimal; | |
2952 | } | |
2953 | ||
f2d8d74d CM |
2954 | static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
2955 | u64 logical, u64 *length, | |
2956 | struct btrfs_multi_bio **multi_ret, | |
2957 | int mirror_num, struct page *unplug_page) | |
0b86a832 CM |
2958 | { |
2959 | struct extent_map *em; | |
2960 | struct map_lookup *map; | |
2961 | struct extent_map_tree *em_tree = &map_tree->map_tree; | |
2962 | u64 offset; | |
593060d7 CM |
2963 | u64 stripe_offset; |
2964 | u64 stripe_nr; | |
cea9e445 | 2965 | int stripes_allocated = 8; |
321aecc6 | 2966 | int stripes_required = 1; |
593060d7 | 2967 | int stripe_index; |
cea9e445 | 2968 | int i; |
f2d8d74d | 2969 | int num_stripes; |
a236aed1 | 2970 | int max_errors = 0; |
cea9e445 | 2971 | struct btrfs_multi_bio *multi = NULL; |
0b86a832 | 2972 | |
7b6d91da | 2973 | if (multi_ret && !(rw & REQ_WRITE)) |
cea9e445 | 2974 | stripes_allocated = 1; |
cea9e445 CM |
2975 | again: |
2976 | if (multi_ret) { | |
2977 | multi = kzalloc(btrfs_multi_bio_size(stripes_allocated), | |
2978 | GFP_NOFS); | |
2979 | if (!multi) | |
2980 | return -ENOMEM; | |
a236aed1 CM |
2981 | |
2982 | atomic_set(&multi->error, 0); | |
cea9e445 | 2983 | } |
0b86a832 | 2984 | |
890871be | 2985 | read_lock(&em_tree->lock); |
0b86a832 | 2986 | em = lookup_extent_mapping(em_tree, logical, *length); |
890871be | 2987 | read_unlock(&em_tree->lock); |
f2d8d74d | 2988 | |
2423fdfb JS |
2989 | if (!em && unplug_page) { |
2990 | kfree(multi); | |
f2d8d74d | 2991 | return 0; |
2423fdfb | 2992 | } |
f2d8d74d | 2993 | |
3b951516 | 2994 | if (!em) { |
d397712b CM |
2995 | printk(KERN_CRIT "unable to find logical %llu len %llu\n", |
2996 | (unsigned long long)logical, | |
2997 | (unsigned long long)*length); | |
f2d8d74d | 2998 | BUG(); |
3b951516 | 2999 | } |
0b86a832 CM |
3000 | |
3001 | BUG_ON(em->start > logical || em->start + em->len < logical); | |
3002 | map = (struct map_lookup *)em->bdev; | |
3003 | offset = logical - em->start; | |
593060d7 | 3004 | |
f188591e CM |
3005 | if (mirror_num > map->num_stripes) |
3006 | mirror_num = 0; | |
3007 | ||
cea9e445 | 3008 | /* if our multi bio struct is too small, back off and try again */ |
7b6d91da | 3009 | if (rw & REQ_WRITE) { |
321aecc6 CM |
3010 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
3011 | BTRFS_BLOCK_GROUP_DUP)) { | |
3012 | stripes_required = map->num_stripes; | |
a236aed1 | 3013 | max_errors = 1; |
321aecc6 CM |
3014 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
3015 | stripes_required = map->sub_stripes; | |
a236aed1 | 3016 | max_errors = 1; |
321aecc6 CM |
3017 | } |
3018 | } | |
7b6d91da | 3019 | if (multi_ret && (rw & REQ_WRITE) && |
321aecc6 | 3020 | stripes_allocated < stripes_required) { |
cea9e445 | 3021 | stripes_allocated = map->num_stripes; |
cea9e445 CM |
3022 | free_extent_map(em); |
3023 | kfree(multi); | |
3024 | goto again; | |
3025 | } | |
593060d7 CM |
3026 | stripe_nr = offset; |
3027 | /* | |
3028 | * stripe_nr counts the total number of stripes we have to stride | |
3029 | * to get to this block | |
3030 | */ | |
3031 | do_div(stripe_nr, map->stripe_len); | |
3032 | ||
3033 | stripe_offset = stripe_nr * map->stripe_len; | |
3034 | BUG_ON(offset < stripe_offset); | |
3035 | ||
3036 | /* stripe_offset is the offset of this block in its stripe*/ | |
3037 | stripe_offset = offset - stripe_offset; | |
3038 | ||
cea9e445 | 3039 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 | |
321aecc6 | 3040 | BTRFS_BLOCK_GROUP_RAID10 | |
cea9e445 CM |
3041 | BTRFS_BLOCK_GROUP_DUP)) { |
3042 | /* we limit the length of each bio to what fits in a stripe */ | |
3043 | *length = min_t(u64, em->len - offset, | |
3044 | map->stripe_len - stripe_offset); | |
3045 | } else { | |
3046 | *length = em->len - offset; | |
3047 | } | |
f2d8d74d CM |
3048 | |
3049 | if (!multi_ret && !unplug_page) | |
cea9e445 CM |
3050 | goto out; |
3051 | ||
f2d8d74d | 3052 | num_stripes = 1; |
cea9e445 | 3053 | stripe_index = 0; |
8790d502 | 3054 | if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
7b6d91da | 3055 | if (unplug_page || (rw & REQ_WRITE)) |
f2d8d74d | 3056 | num_stripes = map->num_stripes; |
2fff734f | 3057 | else if (mirror_num) |
f188591e | 3058 | stripe_index = mirror_num - 1; |
dfe25020 CM |
3059 | else { |
3060 | stripe_index = find_live_mirror(map, 0, | |
3061 | map->num_stripes, | |
3062 | current->pid % map->num_stripes); | |
3063 | } | |
2fff734f | 3064 | |
611f0e00 | 3065 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
7b6d91da | 3066 | if (rw & REQ_WRITE) |
f2d8d74d | 3067 | num_stripes = map->num_stripes; |
f188591e CM |
3068 | else if (mirror_num) |
3069 | stripe_index = mirror_num - 1; | |
2fff734f | 3070 | |
321aecc6 CM |
3071 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
3072 | int factor = map->num_stripes / map->sub_stripes; | |
321aecc6 CM |
3073 | |
3074 | stripe_index = do_div(stripe_nr, factor); | |
3075 | stripe_index *= map->sub_stripes; | |
3076 | ||
7b6d91da | 3077 | if (unplug_page || (rw & REQ_WRITE)) |
f2d8d74d | 3078 | num_stripes = map->sub_stripes; |
321aecc6 CM |
3079 | else if (mirror_num) |
3080 | stripe_index += mirror_num - 1; | |
dfe25020 CM |
3081 | else { |
3082 | stripe_index = find_live_mirror(map, stripe_index, | |
3083 | map->sub_stripes, stripe_index + | |
3084 | current->pid % map->sub_stripes); | |
3085 | } | |
8790d502 CM |
3086 | } else { |
3087 | /* | |
3088 | * after this do_div call, stripe_nr is the number of stripes | |
3089 | * on this device we have to walk to find the data, and | |
3090 | * stripe_index is the number of our device in the stripe array | |
3091 | */ | |
3092 | stripe_index = do_div(stripe_nr, map->num_stripes); | |
3093 | } | |
593060d7 | 3094 | BUG_ON(stripe_index >= map->num_stripes); |
cea9e445 | 3095 | |
f2d8d74d CM |
3096 | for (i = 0; i < num_stripes; i++) { |
3097 | if (unplug_page) { | |
3098 | struct btrfs_device *device; | |
3099 | struct backing_dev_info *bdi; | |
3100 | ||
3101 | device = map->stripes[stripe_index].dev; | |
dfe25020 CM |
3102 | if (device->bdev) { |
3103 | bdi = blk_get_backing_dev_info(device->bdev); | |
d397712b | 3104 | if (bdi->unplug_io_fn) |
dfe25020 | 3105 | bdi->unplug_io_fn(bdi, unplug_page); |
f2d8d74d CM |
3106 | } |
3107 | } else { | |
3108 | multi->stripes[i].physical = | |
3109 | map->stripes[stripe_index].physical + | |
3110 | stripe_offset + stripe_nr * map->stripe_len; | |
3111 | multi->stripes[i].dev = map->stripes[stripe_index].dev; | |
3112 | } | |
cea9e445 | 3113 | stripe_index++; |
593060d7 | 3114 | } |
f2d8d74d CM |
3115 | if (multi_ret) { |
3116 | *multi_ret = multi; | |
3117 | multi->num_stripes = num_stripes; | |
a236aed1 | 3118 | multi->max_errors = max_errors; |
f2d8d74d | 3119 | } |
cea9e445 | 3120 | out: |
0b86a832 | 3121 | free_extent_map(em); |
0b86a832 CM |
3122 | return 0; |
3123 | } | |
3124 | ||
f2d8d74d CM |
3125 | int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw, |
3126 | u64 logical, u64 *length, | |
3127 | struct btrfs_multi_bio **multi_ret, int mirror_num) | |
3128 | { | |
3129 | return __btrfs_map_block(map_tree, rw, logical, length, multi_ret, | |
3130 | mirror_num, NULL); | |
3131 | } | |
3132 | ||
a512bbf8 YZ |
3133 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
3134 | u64 chunk_start, u64 physical, u64 devid, | |
3135 | u64 **logical, int *naddrs, int *stripe_len) | |
3136 | { | |
3137 | struct extent_map_tree *em_tree = &map_tree->map_tree; | |
3138 | struct extent_map *em; | |
3139 | struct map_lookup *map; | |
3140 | u64 *buf; | |
3141 | u64 bytenr; | |
3142 | u64 length; | |
3143 | u64 stripe_nr; | |
3144 | int i, j, nr = 0; | |
3145 | ||
890871be | 3146 | read_lock(&em_tree->lock); |
a512bbf8 | 3147 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
890871be | 3148 | read_unlock(&em_tree->lock); |
a512bbf8 YZ |
3149 | |
3150 | BUG_ON(!em || em->start != chunk_start); | |
3151 | map = (struct map_lookup *)em->bdev; | |
3152 | ||
3153 | length = em->len; | |
3154 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) | |
3155 | do_div(length, map->num_stripes / map->sub_stripes); | |
3156 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) | |
3157 | do_div(length, map->num_stripes); | |
3158 | ||
3159 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); | |
3160 | BUG_ON(!buf); | |
3161 | ||
3162 | for (i = 0; i < map->num_stripes; i++) { | |
3163 | if (devid && map->stripes[i].dev->devid != devid) | |
3164 | continue; | |
3165 | if (map->stripes[i].physical > physical || | |
3166 | map->stripes[i].physical + length <= physical) | |
3167 | continue; | |
3168 | ||
3169 | stripe_nr = physical - map->stripes[i].physical; | |
3170 | do_div(stripe_nr, map->stripe_len); | |
3171 | ||
3172 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { | |
3173 | stripe_nr = stripe_nr * map->num_stripes + i; | |
3174 | do_div(stripe_nr, map->sub_stripes); | |
3175 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { | |
3176 | stripe_nr = stripe_nr * map->num_stripes + i; | |
3177 | } | |
3178 | bytenr = chunk_start + stripe_nr * map->stripe_len; | |
934d375b | 3179 | WARN_ON(nr >= map->num_stripes); |
a512bbf8 YZ |
3180 | for (j = 0; j < nr; j++) { |
3181 | if (buf[j] == bytenr) | |
3182 | break; | |
3183 | } | |
934d375b CM |
3184 | if (j == nr) { |
3185 | WARN_ON(nr >= map->num_stripes); | |
a512bbf8 | 3186 | buf[nr++] = bytenr; |
934d375b | 3187 | } |
a512bbf8 YZ |
3188 | } |
3189 | ||
a512bbf8 YZ |
3190 | *logical = buf; |
3191 | *naddrs = nr; | |
3192 | *stripe_len = map->stripe_len; | |
3193 | ||
3194 | free_extent_map(em); | |
3195 | return 0; | |
3196 | } | |
3197 | ||
f2d8d74d CM |
3198 | int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree, |
3199 | u64 logical, struct page *page) | |
3200 | { | |
3201 | u64 length = PAGE_CACHE_SIZE; | |
3202 | return __btrfs_map_block(map_tree, READ, logical, &length, | |
3203 | NULL, 0, page); | |
3204 | } | |
3205 | ||
8790d502 | 3206 | static void end_bio_multi_stripe(struct bio *bio, int err) |
8790d502 | 3207 | { |
cea9e445 | 3208 | struct btrfs_multi_bio *multi = bio->bi_private; |
7d2b4daa | 3209 | int is_orig_bio = 0; |
8790d502 | 3210 | |
8790d502 | 3211 | if (err) |
a236aed1 | 3212 | atomic_inc(&multi->error); |
8790d502 | 3213 | |
7d2b4daa CM |
3214 | if (bio == multi->orig_bio) |
3215 | is_orig_bio = 1; | |
3216 | ||
cea9e445 | 3217 | if (atomic_dec_and_test(&multi->stripes_pending)) { |
7d2b4daa CM |
3218 | if (!is_orig_bio) { |
3219 | bio_put(bio); | |
3220 | bio = multi->orig_bio; | |
3221 | } | |
8790d502 CM |
3222 | bio->bi_private = multi->private; |
3223 | bio->bi_end_io = multi->end_io; | |
a236aed1 CM |
3224 | /* only send an error to the higher layers if it is |
3225 | * beyond the tolerance of the multi-bio | |
3226 | */ | |
1259ab75 | 3227 | if (atomic_read(&multi->error) > multi->max_errors) { |
a236aed1 | 3228 | err = -EIO; |
1259ab75 CM |
3229 | } else if (err) { |
3230 | /* | |
3231 | * this bio is actually up to date, we didn't | |
3232 | * go over the max number of errors | |
3233 | */ | |
3234 | set_bit(BIO_UPTODATE, &bio->bi_flags); | |
a236aed1 | 3235 | err = 0; |
1259ab75 | 3236 | } |
8790d502 CM |
3237 | kfree(multi); |
3238 | ||
3239 | bio_endio(bio, err); | |
7d2b4daa | 3240 | } else if (!is_orig_bio) { |
8790d502 CM |
3241 | bio_put(bio); |
3242 | } | |
8790d502 CM |
3243 | } |
3244 | ||
8b712842 CM |
3245 | struct async_sched { |
3246 | struct bio *bio; | |
3247 | int rw; | |
3248 | struct btrfs_fs_info *info; | |
3249 | struct btrfs_work work; | |
3250 | }; | |
3251 | ||
3252 | /* | |
3253 | * see run_scheduled_bios for a description of why bios are collected for | |
3254 | * async submit. | |
3255 | * | |
3256 | * This will add one bio to the pending list for a device and make sure | |
3257 | * the work struct is scheduled. | |
3258 | */ | |
d397712b | 3259 | static noinline int schedule_bio(struct btrfs_root *root, |
a1b32a59 CM |
3260 | struct btrfs_device *device, |
3261 | int rw, struct bio *bio) | |
8b712842 CM |
3262 | { |
3263 | int should_queue = 1; | |
ffbd517d | 3264 | struct btrfs_pending_bios *pending_bios; |
8b712842 CM |
3265 | |
3266 | /* don't bother with additional async steps for reads, right now */ | |
7b6d91da | 3267 | if (!(rw & REQ_WRITE)) { |
492bb6de | 3268 | bio_get(bio); |
8b712842 | 3269 | submit_bio(rw, bio); |
492bb6de | 3270 | bio_put(bio); |
8b712842 CM |
3271 | return 0; |
3272 | } | |
3273 | ||
3274 | /* | |
0986fe9e | 3275 | * nr_async_bios allows us to reliably return congestion to the |
8b712842 CM |
3276 | * higher layers. Otherwise, the async bio makes it appear we have |
3277 | * made progress against dirty pages when we've really just put it | |
3278 | * on a queue for later | |
3279 | */ | |
0986fe9e | 3280 | atomic_inc(&root->fs_info->nr_async_bios); |
492bb6de | 3281 | WARN_ON(bio->bi_next); |
8b712842 CM |
3282 | bio->bi_next = NULL; |
3283 | bio->bi_rw |= rw; | |
3284 | ||
3285 | spin_lock(&device->io_lock); | |
7b6d91da | 3286 | if (bio->bi_rw & REQ_SYNC) |
ffbd517d CM |
3287 | pending_bios = &device->pending_sync_bios; |
3288 | else | |
3289 | pending_bios = &device->pending_bios; | |
8b712842 | 3290 | |
ffbd517d CM |
3291 | if (pending_bios->tail) |
3292 | pending_bios->tail->bi_next = bio; | |
8b712842 | 3293 | |
ffbd517d CM |
3294 | pending_bios->tail = bio; |
3295 | if (!pending_bios->head) | |
3296 | pending_bios->head = bio; | |
8b712842 CM |
3297 | if (device->running_pending) |
3298 | should_queue = 0; | |
3299 | ||
3300 | spin_unlock(&device->io_lock); | |
3301 | ||
3302 | if (should_queue) | |
1cc127b5 CM |
3303 | btrfs_queue_worker(&root->fs_info->submit_workers, |
3304 | &device->work); | |
8b712842 CM |
3305 | return 0; |
3306 | } | |
3307 | ||
f188591e | 3308 | int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio, |
8b712842 | 3309 | int mirror_num, int async_submit) |
0b86a832 CM |
3310 | { |
3311 | struct btrfs_mapping_tree *map_tree; | |
3312 | struct btrfs_device *dev; | |
8790d502 | 3313 | struct bio *first_bio = bio; |
a62b9401 | 3314 | u64 logical = (u64)bio->bi_sector << 9; |
0b86a832 CM |
3315 | u64 length = 0; |
3316 | u64 map_length; | |
cea9e445 | 3317 | struct btrfs_multi_bio *multi = NULL; |
0b86a832 | 3318 | int ret; |
8790d502 CM |
3319 | int dev_nr = 0; |
3320 | int total_devs = 1; | |
0b86a832 | 3321 | |
f2d8d74d | 3322 | length = bio->bi_size; |
0b86a832 CM |
3323 | map_tree = &root->fs_info->mapping_tree; |
3324 | map_length = length; | |
cea9e445 | 3325 | |
f188591e CM |
3326 | ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi, |
3327 | mirror_num); | |
cea9e445 CM |
3328 | BUG_ON(ret); |
3329 | ||
3330 | total_devs = multi->num_stripes; | |
3331 | if (map_length < length) { | |
d397712b CM |
3332 | printk(KERN_CRIT "mapping failed logical %llu bio len %llu " |
3333 | "len %llu\n", (unsigned long long)logical, | |
3334 | (unsigned long long)length, | |
3335 | (unsigned long long)map_length); | |
cea9e445 CM |
3336 | BUG(); |
3337 | } | |
3338 | multi->end_io = first_bio->bi_end_io; | |
3339 | multi->private = first_bio->bi_private; | |
7d2b4daa | 3340 | multi->orig_bio = first_bio; |
cea9e445 CM |
3341 | atomic_set(&multi->stripes_pending, multi->num_stripes); |
3342 | ||
d397712b | 3343 | while (dev_nr < total_devs) { |
8790d502 | 3344 | if (total_devs > 1) { |
8790d502 CM |
3345 | if (dev_nr < total_devs - 1) { |
3346 | bio = bio_clone(first_bio, GFP_NOFS); | |
3347 | BUG_ON(!bio); | |
3348 | } else { | |
3349 | bio = first_bio; | |
3350 | } | |
3351 | bio->bi_private = multi; | |
3352 | bio->bi_end_io = end_bio_multi_stripe; | |
3353 | } | |
cea9e445 CM |
3354 | bio->bi_sector = multi->stripes[dev_nr].physical >> 9; |
3355 | dev = multi->stripes[dev_nr].dev; | |
18e503d6 | 3356 | if (dev && dev->bdev && (rw != WRITE || dev->writeable)) { |
dfe25020 | 3357 | bio->bi_bdev = dev->bdev; |
8b712842 CM |
3358 | if (async_submit) |
3359 | schedule_bio(root, dev, rw, bio); | |
3360 | else | |
3361 | submit_bio(rw, bio); | |
dfe25020 CM |
3362 | } else { |
3363 | bio->bi_bdev = root->fs_info->fs_devices->latest_bdev; | |
3364 | bio->bi_sector = logical >> 9; | |
dfe25020 | 3365 | bio_endio(bio, -EIO); |
dfe25020 | 3366 | } |
8790d502 CM |
3367 | dev_nr++; |
3368 | } | |
cea9e445 CM |
3369 | if (total_devs == 1) |
3370 | kfree(multi); | |
0b86a832 CM |
3371 | return 0; |
3372 | } | |
3373 | ||
a443755f | 3374 | struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid, |
2b82032c | 3375 | u8 *uuid, u8 *fsid) |
0b86a832 | 3376 | { |
2b82032c YZ |
3377 | struct btrfs_device *device; |
3378 | struct btrfs_fs_devices *cur_devices; | |
3379 | ||
3380 | cur_devices = root->fs_info->fs_devices; | |
3381 | while (cur_devices) { | |
3382 | if (!fsid || | |
3383 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { | |
3384 | device = __find_device(&cur_devices->devices, | |
3385 | devid, uuid); | |
3386 | if (device) | |
3387 | return device; | |
3388 | } | |
3389 | cur_devices = cur_devices->seed; | |
3390 | } | |
3391 | return NULL; | |
0b86a832 CM |
3392 | } |
3393 | ||
dfe25020 CM |
3394 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
3395 | u64 devid, u8 *dev_uuid) | |
3396 | { | |
3397 | struct btrfs_device *device; | |
3398 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | |
3399 | ||
3400 | device = kzalloc(sizeof(*device), GFP_NOFS); | |
7cbd8a83 | 3401 | if (!device) |
3402 | return NULL; | |
dfe25020 CM |
3403 | list_add(&device->dev_list, |
3404 | &fs_devices->devices); | |
dfe25020 CM |
3405 | device->barriers = 1; |
3406 | device->dev_root = root->fs_info->dev_root; | |
3407 | device->devid = devid; | |
8b712842 | 3408 | device->work.func = pending_bios_fn; |
e4404d6e | 3409 | device->fs_devices = fs_devices; |
cd02dca5 | 3410 | device->missing = 1; |
dfe25020 | 3411 | fs_devices->num_devices++; |
cd02dca5 | 3412 | fs_devices->missing_devices++; |
dfe25020 | 3413 | spin_lock_init(&device->io_lock); |
d20f7043 | 3414 | INIT_LIST_HEAD(&device->dev_alloc_list); |
dfe25020 CM |
3415 | memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE); |
3416 | return device; | |
3417 | } | |
3418 | ||
0b86a832 CM |
3419 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
3420 | struct extent_buffer *leaf, | |
3421 | struct btrfs_chunk *chunk) | |
3422 | { | |
3423 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; | |
3424 | struct map_lookup *map; | |
3425 | struct extent_map *em; | |
3426 | u64 logical; | |
3427 | u64 length; | |
3428 | u64 devid; | |
a443755f | 3429 | u8 uuid[BTRFS_UUID_SIZE]; |
593060d7 | 3430 | int num_stripes; |
0b86a832 | 3431 | int ret; |
593060d7 | 3432 | int i; |
0b86a832 | 3433 | |
e17cade2 CM |
3434 | logical = key->offset; |
3435 | length = btrfs_chunk_length(leaf, chunk); | |
a061fc8d | 3436 | |
890871be | 3437 | read_lock(&map_tree->map_tree.lock); |
0b86a832 | 3438 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
890871be | 3439 | read_unlock(&map_tree->map_tree.lock); |
0b86a832 CM |
3440 | |
3441 | /* already mapped? */ | |
3442 | if (em && em->start <= logical && em->start + em->len > logical) { | |
3443 | free_extent_map(em); | |
0b86a832 CM |
3444 | return 0; |
3445 | } else if (em) { | |
3446 | free_extent_map(em); | |
3447 | } | |
0b86a832 | 3448 | |
0b86a832 CM |
3449 | em = alloc_extent_map(GFP_NOFS); |
3450 | if (!em) | |
3451 | return -ENOMEM; | |
593060d7 CM |
3452 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
3453 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); | |
0b86a832 CM |
3454 | if (!map) { |
3455 | free_extent_map(em); | |
3456 | return -ENOMEM; | |
3457 | } | |
3458 | ||
3459 | em->bdev = (struct block_device *)map; | |
3460 | em->start = logical; | |
3461 | em->len = length; | |
3462 | em->block_start = 0; | |
c8b97818 | 3463 | em->block_len = em->len; |
0b86a832 | 3464 | |
593060d7 CM |
3465 | map->num_stripes = num_stripes; |
3466 | map->io_width = btrfs_chunk_io_width(leaf, chunk); | |
3467 | map->io_align = btrfs_chunk_io_align(leaf, chunk); | |
3468 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); | |
3469 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); | |
3470 | map->type = btrfs_chunk_type(leaf, chunk); | |
321aecc6 | 3471 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
593060d7 CM |
3472 | for (i = 0; i < num_stripes; i++) { |
3473 | map->stripes[i].physical = | |
3474 | btrfs_stripe_offset_nr(leaf, chunk, i); | |
3475 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); | |
a443755f CM |
3476 | read_extent_buffer(leaf, uuid, (unsigned long) |
3477 | btrfs_stripe_dev_uuid_nr(chunk, i), | |
3478 | BTRFS_UUID_SIZE); | |
2b82032c YZ |
3479 | map->stripes[i].dev = btrfs_find_device(root, devid, uuid, |
3480 | NULL); | |
dfe25020 | 3481 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
593060d7 CM |
3482 | kfree(map); |
3483 | free_extent_map(em); | |
3484 | return -EIO; | |
3485 | } | |
dfe25020 CM |
3486 | if (!map->stripes[i].dev) { |
3487 | map->stripes[i].dev = | |
3488 | add_missing_dev(root, devid, uuid); | |
3489 | if (!map->stripes[i].dev) { | |
3490 | kfree(map); | |
3491 | free_extent_map(em); | |
3492 | return -EIO; | |
3493 | } | |
3494 | } | |
3495 | map->stripes[i].dev->in_fs_metadata = 1; | |
0b86a832 CM |
3496 | } |
3497 | ||
890871be | 3498 | write_lock(&map_tree->map_tree.lock); |
0b86a832 | 3499 | ret = add_extent_mapping(&map_tree->map_tree, em); |
890871be | 3500 | write_unlock(&map_tree->map_tree.lock); |
b248a415 | 3501 | BUG_ON(ret); |
0b86a832 CM |
3502 | free_extent_map(em); |
3503 | ||
3504 | return 0; | |
3505 | } | |
3506 | ||
3507 | static int fill_device_from_item(struct extent_buffer *leaf, | |
3508 | struct btrfs_dev_item *dev_item, | |
3509 | struct btrfs_device *device) | |
3510 | { | |
3511 | unsigned long ptr; | |
0b86a832 CM |
3512 | |
3513 | device->devid = btrfs_device_id(leaf, dev_item); | |
d6397bae CB |
3514 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
3515 | device->total_bytes = device->disk_total_bytes; | |
0b86a832 CM |
3516 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
3517 | device->type = btrfs_device_type(leaf, dev_item); | |
3518 | device->io_align = btrfs_device_io_align(leaf, dev_item); | |
3519 | device->io_width = btrfs_device_io_width(leaf, dev_item); | |
3520 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); | |
0b86a832 CM |
3521 | |
3522 | ptr = (unsigned long)btrfs_device_uuid(dev_item); | |
e17cade2 | 3523 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
0b86a832 | 3524 | |
0b86a832 CM |
3525 | return 0; |
3526 | } | |
3527 | ||
2b82032c YZ |
3528 | static int open_seed_devices(struct btrfs_root *root, u8 *fsid) |
3529 | { | |
3530 | struct btrfs_fs_devices *fs_devices; | |
3531 | int ret; | |
3532 | ||
3533 | mutex_lock(&uuid_mutex); | |
3534 | ||
3535 | fs_devices = root->fs_info->fs_devices->seed; | |
3536 | while (fs_devices) { | |
3537 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) { | |
3538 | ret = 0; | |
3539 | goto out; | |
3540 | } | |
3541 | fs_devices = fs_devices->seed; | |
3542 | } | |
3543 | ||
3544 | fs_devices = find_fsid(fsid); | |
3545 | if (!fs_devices) { | |
3546 | ret = -ENOENT; | |
3547 | goto out; | |
3548 | } | |
e4404d6e YZ |
3549 | |
3550 | fs_devices = clone_fs_devices(fs_devices); | |
3551 | if (IS_ERR(fs_devices)) { | |
3552 | ret = PTR_ERR(fs_devices); | |
2b82032c YZ |
3553 | goto out; |
3554 | } | |
3555 | ||
97288f2c | 3556 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
15916de8 | 3557 | root->fs_info->bdev_holder); |
2b82032c YZ |
3558 | if (ret) |
3559 | goto out; | |
3560 | ||
3561 | if (!fs_devices->seeding) { | |
3562 | __btrfs_close_devices(fs_devices); | |
e4404d6e | 3563 | free_fs_devices(fs_devices); |
2b82032c YZ |
3564 | ret = -EINVAL; |
3565 | goto out; | |
3566 | } | |
3567 | ||
3568 | fs_devices->seed = root->fs_info->fs_devices->seed; | |
3569 | root->fs_info->fs_devices->seed = fs_devices; | |
2b82032c YZ |
3570 | out: |
3571 | mutex_unlock(&uuid_mutex); | |
3572 | return ret; | |
3573 | } | |
3574 | ||
0d81ba5d | 3575 | static int read_one_dev(struct btrfs_root *root, |
0b86a832 CM |
3576 | struct extent_buffer *leaf, |
3577 | struct btrfs_dev_item *dev_item) | |
3578 | { | |
3579 | struct btrfs_device *device; | |
3580 | u64 devid; | |
3581 | int ret; | |
2b82032c | 3582 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
a443755f CM |
3583 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
3584 | ||
0b86a832 | 3585 | devid = btrfs_device_id(leaf, dev_item); |
a443755f CM |
3586 | read_extent_buffer(leaf, dev_uuid, |
3587 | (unsigned long)btrfs_device_uuid(dev_item), | |
3588 | BTRFS_UUID_SIZE); | |
2b82032c YZ |
3589 | read_extent_buffer(leaf, fs_uuid, |
3590 | (unsigned long)btrfs_device_fsid(dev_item), | |
3591 | BTRFS_UUID_SIZE); | |
3592 | ||
3593 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { | |
3594 | ret = open_seed_devices(root, fs_uuid); | |
e4404d6e | 3595 | if (ret && !btrfs_test_opt(root, DEGRADED)) |
2b82032c | 3596 | return ret; |
2b82032c YZ |
3597 | } |
3598 | ||
3599 | device = btrfs_find_device(root, devid, dev_uuid, fs_uuid); | |
3600 | if (!device || !device->bdev) { | |
e4404d6e | 3601 | if (!btrfs_test_opt(root, DEGRADED)) |
2b82032c YZ |
3602 | return -EIO; |
3603 | ||
3604 | if (!device) { | |
d397712b CM |
3605 | printk(KERN_WARNING "warning devid %llu missing\n", |
3606 | (unsigned long long)devid); | |
2b82032c YZ |
3607 | device = add_missing_dev(root, devid, dev_uuid); |
3608 | if (!device) | |
3609 | return -ENOMEM; | |
cd02dca5 CM |
3610 | } else if (!device->missing) { |
3611 | /* | |
3612 | * this happens when a device that was properly setup | |
3613 | * in the device info lists suddenly goes bad. | |
3614 | * device->bdev is NULL, and so we have to set | |
3615 | * device->missing to one here | |
3616 | */ | |
3617 | root->fs_info->fs_devices->missing_devices++; | |
3618 | device->missing = 1; | |
2b82032c YZ |
3619 | } |
3620 | } | |
3621 | ||
3622 | if (device->fs_devices != root->fs_info->fs_devices) { | |
3623 | BUG_ON(device->writeable); | |
3624 | if (device->generation != | |
3625 | btrfs_device_generation(leaf, dev_item)) | |
3626 | return -EINVAL; | |
6324fbf3 | 3627 | } |
0b86a832 CM |
3628 | |
3629 | fill_device_from_item(leaf, dev_item, device); | |
3630 | device->dev_root = root->fs_info->dev_root; | |
dfe25020 | 3631 | device->in_fs_metadata = 1; |
2b82032c YZ |
3632 | if (device->writeable) |
3633 | device->fs_devices->total_rw_bytes += device->total_bytes; | |
0b86a832 | 3634 | ret = 0; |
0b86a832 CM |
3635 | return ret; |
3636 | } | |
3637 | ||
0d81ba5d CM |
3638 | int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf) |
3639 | { | |
3640 | struct btrfs_dev_item *dev_item; | |
3641 | ||
3642 | dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block, | |
3643 | dev_item); | |
3644 | return read_one_dev(root, buf, dev_item); | |
3645 | } | |
3646 | ||
e4404d6e | 3647 | int btrfs_read_sys_array(struct btrfs_root *root) |
0b86a832 CM |
3648 | { |
3649 | struct btrfs_super_block *super_copy = &root->fs_info->super_copy; | |
a061fc8d | 3650 | struct extent_buffer *sb; |
0b86a832 | 3651 | struct btrfs_disk_key *disk_key; |
0b86a832 | 3652 | struct btrfs_chunk *chunk; |
84eed90f CM |
3653 | u8 *ptr; |
3654 | unsigned long sb_ptr; | |
3655 | int ret = 0; | |
0b86a832 CM |
3656 | u32 num_stripes; |
3657 | u32 array_size; | |
3658 | u32 len = 0; | |
0b86a832 | 3659 | u32 cur; |
84eed90f | 3660 | struct btrfs_key key; |
0b86a832 | 3661 | |
e4404d6e | 3662 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
a061fc8d CM |
3663 | BTRFS_SUPER_INFO_SIZE); |
3664 | if (!sb) | |
3665 | return -ENOMEM; | |
3666 | btrfs_set_buffer_uptodate(sb); | |
4008c04a CM |
3667 | btrfs_set_buffer_lockdep_class(sb, 0); |
3668 | ||
a061fc8d | 3669 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
0b86a832 CM |
3670 | array_size = btrfs_super_sys_array_size(super_copy); |
3671 | ||
0b86a832 CM |
3672 | ptr = super_copy->sys_chunk_array; |
3673 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); | |
3674 | cur = 0; | |
3675 | ||
3676 | while (cur < array_size) { | |
3677 | disk_key = (struct btrfs_disk_key *)ptr; | |
3678 | btrfs_disk_key_to_cpu(&key, disk_key); | |
3679 | ||
a061fc8d | 3680 | len = sizeof(*disk_key); ptr += len; |
0b86a832 CM |
3681 | sb_ptr += len; |
3682 | cur += len; | |
3683 | ||
0d81ba5d | 3684 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
0b86a832 | 3685 | chunk = (struct btrfs_chunk *)sb_ptr; |
0d81ba5d | 3686 | ret = read_one_chunk(root, &key, sb, chunk); |
84eed90f CM |
3687 | if (ret) |
3688 | break; | |
0b86a832 CM |
3689 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
3690 | len = btrfs_chunk_item_size(num_stripes); | |
3691 | } else { | |
84eed90f CM |
3692 | ret = -EIO; |
3693 | break; | |
0b86a832 CM |
3694 | } |
3695 | ptr += len; | |
3696 | sb_ptr += len; | |
3697 | cur += len; | |
3698 | } | |
a061fc8d | 3699 | free_extent_buffer(sb); |
84eed90f | 3700 | return ret; |
0b86a832 CM |
3701 | } |
3702 | ||
3703 | int btrfs_read_chunk_tree(struct btrfs_root *root) | |
3704 | { | |
3705 | struct btrfs_path *path; | |
3706 | struct extent_buffer *leaf; | |
3707 | struct btrfs_key key; | |
3708 | struct btrfs_key found_key; | |
3709 | int ret; | |
3710 | int slot; | |
3711 | ||
3712 | root = root->fs_info->chunk_root; | |
3713 | ||
3714 | path = btrfs_alloc_path(); | |
3715 | if (!path) | |
3716 | return -ENOMEM; | |
3717 | ||
3718 | /* first we search for all of the device items, and then we | |
3719 | * read in all of the chunk items. This way we can create chunk | |
3720 | * mappings that reference all of the devices that are afound | |
3721 | */ | |
3722 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; | |
3723 | key.offset = 0; | |
3724 | key.type = 0; | |
3725 | again: | |
3726 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
ab59381e ZL |
3727 | if (ret < 0) |
3728 | goto error; | |
d397712b | 3729 | while (1) { |
0b86a832 CM |
3730 | leaf = path->nodes[0]; |
3731 | slot = path->slots[0]; | |
3732 | if (slot >= btrfs_header_nritems(leaf)) { | |
3733 | ret = btrfs_next_leaf(root, path); | |
3734 | if (ret == 0) | |
3735 | continue; | |
3736 | if (ret < 0) | |
3737 | goto error; | |
3738 | break; | |
3739 | } | |
3740 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3741 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { | |
3742 | if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID) | |
3743 | break; | |
3744 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { | |
3745 | struct btrfs_dev_item *dev_item; | |
3746 | dev_item = btrfs_item_ptr(leaf, slot, | |
3747 | struct btrfs_dev_item); | |
0d81ba5d | 3748 | ret = read_one_dev(root, leaf, dev_item); |
2b82032c YZ |
3749 | if (ret) |
3750 | goto error; | |
0b86a832 CM |
3751 | } |
3752 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { | |
3753 | struct btrfs_chunk *chunk; | |
3754 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); | |
3755 | ret = read_one_chunk(root, &found_key, leaf, chunk); | |
2b82032c YZ |
3756 | if (ret) |
3757 | goto error; | |
0b86a832 CM |
3758 | } |
3759 | path->slots[0]++; | |
3760 | } | |
3761 | if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) { | |
3762 | key.objectid = 0; | |
3763 | btrfs_release_path(root, path); | |
3764 | goto again; | |
3765 | } | |
0b86a832 CM |
3766 | ret = 0; |
3767 | error: | |
2b82032c | 3768 | btrfs_free_path(path); |
0b86a832 CM |
3769 | return ret; |
3770 | } |