media: stv06xx: add missing descriptor sanity checks
[linux/fpc-iii.git] / fs / btrfs / volumes.c
bloba8b71ded4d212dce9ce1e18fe2a5752818bab2e8
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
6 #include <linux/sched.h>
7 #include <linux/bio.h>
8 #include <linux/slab.h>
9 #include <linux/buffer_head.h>
10 #include <linux/blkdev.h>
11 #include <linux/ratelimit.h>
12 #include <linux/kthread.h>
13 #include <linux/raid/pq.h>
14 #include <linux/semaphore.h>
15 #include <linux/uuid.h>
16 #include <linux/list_sort.h>
17 #include "misc.h"
18 #include "ctree.h"
19 #include "extent_map.h"
20 #include "disk-io.h"
21 #include "transaction.h"
22 #include "print-tree.h"
23 #include "volumes.h"
24 #include "raid56.h"
25 #include "async-thread.h"
26 #include "check-integrity.h"
27 #include "rcu-string.h"
28 #include "dev-replace.h"
29 #include "sysfs.h"
30 #include "tree-checker.h"
31 #include "space-info.h"
32 #include "block-group.h"
34 const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
35 [BTRFS_RAID_RAID10] = {
36 .sub_stripes = 2,
37 .dev_stripes = 1,
38 .devs_max = 0, /* 0 == as many as possible */
39 .devs_min = 4,
40 .tolerated_failures = 1,
41 .devs_increment = 2,
42 .ncopies = 2,
43 .nparity = 0,
44 .raid_name = "raid10",
45 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
46 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
48 [BTRFS_RAID_RAID1] = {
49 .sub_stripes = 1,
50 .dev_stripes = 1,
51 .devs_max = 2,
52 .devs_min = 2,
53 .tolerated_failures = 1,
54 .devs_increment = 2,
55 .ncopies = 2,
56 .nparity = 0,
57 .raid_name = "raid1",
58 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
59 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
61 [BTRFS_RAID_RAID1C3] = {
62 .sub_stripes = 1,
63 .dev_stripes = 1,
64 .devs_max = 3,
65 .devs_min = 3,
66 .tolerated_failures = 2,
67 .devs_increment = 3,
68 .ncopies = 3,
69 .raid_name = "raid1c3",
70 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3,
71 .mindev_error = BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET,
73 [BTRFS_RAID_RAID1C4] = {
74 .sub_stripes = 1,
75 .dev_stripes = 1,
76 .devs_max = 4,
77 .devs_min = 4,
78 .tolerated_failures = 3,
79 .devs_increment = 4,
80 .ncopies = 4,
81 .raid_name = "raid1c4",
82 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4,
83 .mindev_error = BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET,
85 [BTRFS_RAID_DUP] = {
86 .sub_stripes = 1,
87 .dev_stripes = 2,
88 .devs_max = 1,
89 .devs_min = 1,
90 .tolerated_failures = 0,
91 .devs_increment = 1,
92 .ncopies = 2,
93 .nparity = 0,
94 .raid_name = "dup",
95 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
96 .mindev_error = 0,
98 [BTRFS_RAID_RAID0] = {
99 .sub_stripes = 1,
100 .dev_stripes = 1,
101 .devs_max = 0,
102 .devs_min = 2,
103 .tolerated_failures = 0,
104 .devs_increment = 1,
105 .ncopies = 1,
106 .nparity = 0,
107 .raid_name = "raid0",
108 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
109 .mindev_error = 0,
111 [BTRFS_RAID_SINGLE] = {
112 .sub_stripes = 1,
113 .dev_stripes = 1,
114 .devs_max = 1,
115 .devs_min = 1,
116 .tolerated_failures = 0,
117 .devs_increment = 1,
118 .ncopies = 1,
119 .nparity = 0,
120 .raid_name = "single",
121 .bg_flag = 0,
122 .mindev_error = 0,
124 [BTRFS_RAID_RAID5] = {
125 .sub_stripes = 1,
126 .dev_stripes = 1,
127 .devs_max = 0,
128 .devs_min = 2,
129 .tolerated_failures = 1,
130 .devs_increment = 1,
131 .ncopies = 1,
132 .nparity = 1,
133 .raid_name = "raid5",
134 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
135 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
137 [BTRFS_RAID_RAID6] = {
138 .sub_stripes = 1,
139 .dev_stripes = 1,
140 .devs_max = 0,
141 .devs_min = 3,
142 .tolerated_failures = 2,
143 .devs_increment = 1,
144 .ncopies = 1,
145 .nparity = 2,
146 .raid_name = "raid6",
147 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
148 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
152 const char *btrfs_bg_type_to_raid_name(u64 flags)
154 const int index = btrfs_bg_flags_to_raid_index(flags);
156 if (index >= BTRFS_NR_RAID_TYPES)
157 return NULL;
159 return btrfs_raid_array[index].raid_name;
163 * Fill @buf with textual description of @bg_flags, no more than @size_buf
164 * bytes including terminating null byte.
166 void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
168 int i;
169 int ret;
170 char *bp = buf;
171 u64 flags = bg_flags;
172 u32 size_bp = size_buf;
174 if (!flags) {
175 strcpy(bp, "NONE");
176 return;
179 #define DESCRIBE_FLAG(flag, desc) \
180 do { \
181 if (flags & (flag)) { \
182 ret = snprintf(bp, size_bp, "%s|", (desc)); \
183 if (ret < 0 || ret >= size_bp) \
184 goto out_overflow; \
185 size_bp -= ret; \
186 bp += ret; \
187 flags &= ~(flag); \
189 } while (0)
191 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
192 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
193 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
195 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
196 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
197 DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag,
198 btrfs_raid_array[i].raid_name);
199 #undef DESCRIBE_FLAG
201 if (flags) {
202 ret = snprintf(bp, size_bp, "0x%llx|", flags);
203 size_bp -= ret;
206 if (size_bp < size_buf)
207 buf[size_buf - size_bp - 1] = '\0'; /* remove last | */
210 * The text is trimmed, it's up to the caller to provide sufficiently
211 * large buffer
213 out_overflow:;
216 static int init_first_rw_device(struct btrfs_trans_handle *trans);
217 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
218 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
219 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
220 static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
221 enum btrfs_map_op op,
222 u64 logical, u64 *length,
223 struct btrfs_bio **bbio_ret,
224 int mirror_num, int need_raid_map);
227 * Device locking
228 * ==============
230 * There are several mutexes that protect manipulation of devices and low-level
231 * structures like chunks but not block groups, extents or files
233 * uuid_mutex (global lock)
234 * ------------------------
235 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
236 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
237 * device) or requested by the device= mount option
239 * the mutex can be very coarse and can cover long-running operations
241 * protects: updates to fs_devices counters like missing devices, rw devices,
242 * seeding, structure cloning, opening/closing devices at mount/umount time
244 * global::fs_devs - add, remove, updates to the global list
246 * does not protect: manipulation of the fs_devices::devices list!
248 * btrfs_device::name - renames (write side), read is RCU
250 * fs_devices::device_list_mutex (per-fs, with RCU)
251 * ------------------------------------------------
252 * protects updates to fs_devices::devices, ie. adding and deleting
254 * simple list traversal with read-only actions can be done with RCU protection
256 * may be used to exclude some operations from running concurrently without any
257 * modifications to the list (see write_all_supers)
259 * balance_mutex
260 * -------------
261 * protects balance structures (status, state) and context accessed from
262 * several places (internally, ioctl)
264 * chunk_mutex
265 * -----------
266 * protects chunks, adding or removing during allocation, trim or when a new
267 * device is added/removed. Additionally it also protects post_commit_list of
268 * individual devices, since they can be added to the transaction's
269 * post_commit_list only with chunk_mutex held.
271 * cleaner_mutex
272 * -------------
273 * a big lock that is held by the cleaner thread and prevents running subvolume
274 * cleaning together with relocation or delayed iputs
277 * Lock nesting
278 * ============
280 * uuid_mutex
281 * volume_mutex
282 * device_list_mutex
283 * chunk_mutex
284 * balance_mutex
287 * Exclusive operations, BTRFS_FS_EXCL_OP
288 * ======================================
290 * Maintains the exclusivity of the following operations that apply to the
291 * whole filesystem and cannot run in parallel.
293 * - Balance (*)
294 * - Device add
295 * - Device remove
296 * - Device replace (*)
297 * - Resize
299 * The device operations (as above) can be in one of the following states:
301 * - Running state
302 * - Paused state
303 * - Completed state
305 * Only device operations marked with (*) can go into the Paused state for the
306 * following reasons:
308 * - ioctl (only Balance can be Paused through ioctl)
309 * - filesystem remounted as read-only
310 * - filesystem unmounted and mounted as read-only
311 * - system power-cycle and filesystem mounted as read-only
312 * - filesystem or device errors leading to forced read-only
314 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
315 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
316 * A device operation in Paused or Running state can be canceled or resumed
317 * either by ioctl (Balance only) or when remounted as read-write.
318 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
319 * completed.
322 DEFINE_MUTEX(uuid_mutex);
323 static LIST_HEAD(fs_uuids);
324 struct list_head * __attribute_const__ btrfs_get_fs_uuids(void)
326 return &fs_uuids;
330 * alloc_fs_devices - allocate struct btrfs_fs_devices
331 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
332 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
334 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
335 * The returned struct is not linked onto any lists and can be destroyed with
336 * kfree() right away.
338 static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
339 const u8 *metadata_fsid)
341 struct btrfs_fs_devices *fs_devs;
343 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
344 if (!fs_devs)
345 return ERR_PTR(-ENOMEM);
347 mutex_init(&fs_devs->device_list_mutex);
349 INIT_LIST_HEAD(&fs_devs->devices);
350 INIT_LIST_HEAD(&fs_devs->alloc_list);
351 INIT_LIST_HEAD(&fs_devs->fs_list);
352 if (fsid)
353 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
355 if (metadata_fsid)
356 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
357 else if (fsid)
358 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
360 return fs_devs;
363 void btrfs_free_device(struct btrfs_device *device)
365 WARN_ON(!list_empty(&device->post_commit_list));
366 rcu_string_free(device->name);
367 extent_io_tree_release(&device->alloc_state);
368 bio_put(device->flush_bio);
369 kfree(device);
372 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
374 struct btrfs_device *device;
375 WARN_ON(fs_devices->opened);
376 while (!list_empty(&fs_devices->devices)) {
377 device = list_entry(fs_devices->devices.next,
378 struct btrfs_device, dev_list);
379 list_del(&device->dev_list);
380 btrfs_free_device(device);
382 kfree(fs_devices);
385 void __exit btrfs_cleanup_fs_uuids(void)
387 struct btrfs_fs_devices *fs_devices;
389 while (!list_empty(&fs_uuids)) {
390 fs_devices = list_entry(fs_uuids.next,
391 struct btrfs_fs_devices, fs_list);
392 list_del(&fs_devices->fs_list);
393 free_fs_devices(fs_devices);
398 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
399 * Returned struct is not linked onto any lists and must be destroyed using
400 * btrfs_free_device.
402 static struct btrfs_device *__alloc_device(void)
404 struct btrfs_device *dev;
406 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
407 if (!dev)
408 return ERR_PTR(-ENOMEM);
411 * Preallocate a bio that's always going to be used for flushing device
412 * barriers and matches the device lifespan
414 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
415 if (!dev->flush_bio) {
416 kfree(dev);
417 return ERR_PTR(-ENOMEM);
420 INIT_LIST_HEAD(&dev->dev_list);
421 INIT_LIST_HEAD(&dev->dev_alloc_list);
422 INIT_LIST_HEAD(&dev->post_commit_list);
424 atomic_set(&dev->reada_in_flight, 0);
425 atomic_set(&dev->dev_stats_ccnt, 0);
426 btrfs_device_data_ordered_init(dev);
427 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
428 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
429 extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL);
431 return dev;
434 static noinline struct btrfs_fs_devices *find_fsid(
435 const u8 *fsid, const u8 *metadata_fsid)
437 struct btrfs_fs_devices *fs_devices;
439 ASSERT(fsid);
441 if (metadata_fsid) {
443 * Handle scanned device having completed its fsid change but
444 * belonging to a fs_devices that was created by first scanning
445 * a device which didn't have its fsid/metadata_uuid changed
446 * at all and the CHANGING_FSID_V2 flag set.
448 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
449 if (fs_devices->fsid_change &&
450 memcmp(metadata_fsid, fs_devices->fsid,
451 BTRFS_FSID_SIZE) == 0 &&
452 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
453 BTRFS_FSID_SIZE) == 0) {
454 return fs_devices;
458 * Handle scanned device having completed its fsid change but
459 * belonging to a fs_devices that was created by a device that
460 * has an outdated pair of fsid/metadata_uuid and
461 * CHANGING_FSID_V2 flag set.
463 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
464 if (fs_devices->fsid_change &&
465 memcmp(fs_devices->metadata_uuid,
466 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
467 memcmp(metadata_fsid, fs_devices->metadata_uuid,
468 BTRFS_FSID_SIZE) == 0) {
469 return fs_devices;
474 /* Handle non-split brain cases */
475 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
476 if (metadata_fsid) {
477 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
478 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
479 BTRFS_FSID_SIZE) == 0)
480 return fs_devices;
481 } else {
482 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
483 return fs_devices;
486 return NULL;
489 static int
490 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
491 int flush, struct block_device **bdev,
492 struct buffer_head **bh)
494 int ret;
496 *bdev = blkdev_get_by_path(device_path, flags, holder);
498 if (IS_ERR(*bdev)) {
499 ret = PTR_ERR(*bdev);
500 goto error;
503 if (flush)
504 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
505 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
506 if (ret) {
507 blkdev_put(*bdev, flags);
508 goto error;
510 invalidate_bdev(*bdev);
511 *bh = btrfs_read_dev_super(*bdev);
512 if (IS_ERR(*bh)) {
513 ret = PTR_ERR(*bh);
514 blkdev_put(*bdev, flags);
515 goto error;
518 return 0;
520 error:
521 *bdev = NULL;
522 *bh = NULL;
523 return ret;
526 static bool device_path_matched(const char *path, struct btrfs_device *device)
528 int found;
530 rcu_read_lock();
531 found = strcmp(rcu_str_deref(device->name), path);
532 rcu_read_unlock();
534 return found == 0;
538 * Search and remove all stale (devices which are not mounted) devices.
539 * When both inputs are NULL, it will search and release all stale devices.
540 * path: Optional. When provided will it release all unmounted devices
541 * matching this path only.
542 * skip_dev: Optional. Will skip this device when searching for the stale
543 * devices.
544 * Return: 0 for success or if @path is NULL.
545 * -EBUSY if @path is a mounted device.
546 * -ENOENT if @path does not match any device in the list.
548 static int btrfs_free_stale_devices(const char *path,
549 struct btrfs_device *skip_device)
551 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
552 struct btrfs_device *device, *tmp_device;
553 int ret = 0;
555 if (path)
556 ret = -ENOENT;
558 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
560 mutex_lock(&fs_devices->device_list_mutex);
561 list_for_each_entry_safe(device, tmp_device,
562 &fs_devices->devices, dev_list) {
563 if (skip_device && skip_device == device)
564 continue;
565 if (path && !device->name)
566 continue;
567 if (path && !device_path_matched(path, device))
568 continue;
569 if (fs_devices->opened) {
570 /* for an already deleted device return 0 */
571 if (path && ret != 0)
572 ret = -EBUSY;
573 break;
576 /* delete the stale device */
577 fs_devices->num_devices--;
578 list_del(&device->dev_list);
579 btrfs_free_device(device);
581 ret = 0;
582 if (fs_devices->num_devices == 0)
583 break;
585 mutex_unlock(&fs_devices->device_list_mutex);
587 if (fs_devices->num_devices == 0) {
588 btrfs_sysfs_remove_fsid(fs_devices);
589 list_del(&fs_devices->fs_list);
590 free_fs_devices(fs_devices);
594 return ret;
597 static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
598 struct btrfs_device *device, fmode_t flags,
599 void *holder)
601 struct request_queue *q;
602 struct block_device *bdev;
603 struct buffer_head *bh;
604 struct btrfs_super_block *disk_super;
605 u64 devid;
606 int ret;
608 if (device->bdev)
609 return -EINVAL;
610 if (!device->name)
611 return -EINVAL;
613 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
614 &bdev, &bh);
615 if (ret)
616 return ret;
618 disk_super = (struct btrfs_super_block *)bh->b_data;
619 devid = btrfs_stack_device_id(&disk_super->dev_item);
620 if (devid != device->devid)
621 goto error_brelse;
623 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
624 goto error_brelse;
626 device->generation = btrfs_super_generation(disk_super);
628 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
629 if (btrfs_super_incompat_flags(disk_super) &
630 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
631 pr_err(
632 "BTRFS: Invalid seeding and uuid-changed device detected\n");
633 goto error_brelse;
636 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
637 fs_devices->seeding = true;
638 } else {
639 if (bdev_read_only(bdev))
640 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
641 else
642 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
645 q = bdev_get_queue(bdev);
646 if (!blk_queue_nonrot(q))
647 fs_devices->rotating = true;
649 device->bdev = bdev;
650 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
651 device->mode = flags;
653 fs_devices->open_devices++;
654 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
655 device->devid != BTRFS_DEV_REPLACE_DEVID) {
656 fs_devices->rw_devices++;
657 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
659 brelse(bh);
661 return 0;
663 error_brelse:
664 brelse(bh);
665 blkdev_put(bdev, flags);
667 return -EINVAL;
671 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
672 * being created with a disk that has already completed its fsid change.
674 static struct btrfs_fs_devices *find_fsid_inprogress(
675 struct btrfs_super_block *disk_super)
677 struct btrfs_fs_devices *fs_devices;
679 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
680 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
681 BTRFS_FSID_SIZE) != 0 &&
682 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
683 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
684 return fs_devices;
688 return NULL;
692 static struct btrfs_fs_devices *find_fsid_changed(
693 struct btrfs_super_block *disk_super)
695 struct btrfs_fs_devices *fs_devices;
698 * Handles the case where scanned device is part of an fs that had
699 * multiple successful changes of FSID but curently device didn't
700 * observe it. Meaning our fsid will be different than theirs. We need
701 * to handle two subcases :
702 * 1 - The fs still continues to have different METADATA/FSID uuids.
703 * 2 - The fs is switched back to its original FSID (METADATA/FSID
704 * are equal).
706 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
707 /* Changed UUIDs */
708 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
709 BTRFS_FSID_SIZE) != 0 &&
710 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
711 BTRFS_FSID_SIZE) == 0 &&
712 memcmp(fs_devices->fsid, disk_super->fsid,
713 BTRFS_FSID_SIZE) != 0)
714 return fs_devices;
716 /* Unchanged UUIDs */
717 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
718 BTRFS_FSID_SIZE) == 0 &&
719 memcmp(fs_devices->fsid, disk_super->metadata_uuid,
720 BTRFS_FSID_SIZE) == 0)
721 return fs_devices;
724 return NULL;
727 static struct btrfs_fs_devices *find_fsid_reverted_metadata(
728 struct btrfs_super_block *disk_super)
730 struct btrfs_fs_devices *fs_devices;
733 * Handle the case where the scanned device is part of an fs whose last
734 * metadata UUID change reverted it to the original FSID. At the same
735 * time * fs_devices was first created by another constitutent device
736 * which didn't fully observe the operation. This results in an
737 * btrfs_fs_devices created with metadata/fsid different AND
738 * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the
739 * fs_devices equal to the FSID of the disk.
741 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
742 if (memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
743 BTRFS_FSID_SIZE) != 0 &&
744 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
745 BTRFS_FSID_SIZE) == 0 &&
746 fs_devices->fsid_change)
747 return fs_devices;
750 return NULL;
753 * Add new device to list of registered devices
755 * Returns:
756 * device pointer which was just added or updated when successful
757 * error pointer when failed
759 static noinline struct btrfs_device *device_list_add(const char *path,
760 struct btrfs_super_block *disk_super,
761 bool *new_device_added)
763 struct btrfs_device *device;
764 struct btrfs_fs_devices *fs_devices = NULL;
765 struct rcu_string *name;
766 u64 found_transid = btrfs_super_generation(disk_super);
767 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
768 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
769 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
770 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
771 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
773 if (fsid_change_in_progress) {
774 if (!has_metadata_uuid) {
776 * When we have an image which has CHANGING_FSID_V2 set
777 * it might belong to either a filesystem which has
778 * disks with completed fsid change or it might belong
779 * to fs with no UUID changes in effect, handle both.
781 fs_devices = find_fsid_inprogress(disk_super);
782 if (!fs_devices)
783 fs_devices = find_fsid(disk_super->fsid, NULL);
784 } else {
785 fs_devices = find_fsid_changed(disk_super);
787 } else if (has_metadata_uuid) {
788 fs_devices = find_fsid(disk_super->fsid,
789 disk_super->metadata_uuid);
790 } else {
791 fs_devices = find_fsid_reverted_metadata(disk_super);
792 if (!fs_devices)
793 fs_devices = find_fsid(disk_super->fsid, NULL);
797 if (!fs_devices) {
798 if (has_metadata_uuid)
799 fs_devices = alloc_fs_devices(disk_super->fsid,
800 disk_super->metadata_uuid);
801 else
802 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
804 if (IS_ERR(fs_devices))
805 return ERR_CAST(fs_devices);
807 fs_devices->fsid_change = fsid_change_in_progress;
809 mutex_lock(&fs_devices->device_list_mutex);
810 list_add(&fs_devices->fs_list, &fs_uuids);
812 device = NULL;
813 } else {
814 mutex_lock(&fs_devices->device_list_mutex);
815 device = btrfs_find_device(fs_devices, devid,
816 disk_super->dev_item.uuid, NULL, false);
819 * If this disk has been pulled into an fs devices created by
820 * a device which had the CHANGING_FSID_V2 flag then replace the
821 * metadata_uuid/fsid values of the fs_devices.
823 if (fs_devices->fsid_change &&
824 found_transid > fs_devices->latest_generation) {
825 memcpy(fs_devices->fsid, disk_super->fsid,
826 BTRFS_FSID_SIZE);
828 if (has_metadata_uuid)
829 memcpy(fs_devices->metadata_uuid,
830 disk_super->metadata_uuid,
831 BTRFS_FSID_SIZE);
832 else
833 memcpy(fs_devices->metadata_uuid,
834 disk_super->fsid, BTRFS_FSID_SIZE);
836 fs_devices->fsid_change = false;
840 if (!device) {
841 if (fs_devices->opened) {
842 mutex_unlock(&fs_devices->device_list_mutex);
843 return ERR_PTR(-EBUSY);
846 device = btrfs_alloc_device(NULL, &devid,
847 disk_super->dev_item.uuid);
848 if (IS_ERR(device)) {
849 mutex_unlock(&fs_devices->device_list_mutex);
850 /* we can safely leave the fs_devices entry around */
851 return device;
854 name = rcu_string_strdup(path, GFP_NOFS);
855 if (!name) {
856 btrfs_free_device(device);
857 mutex_unlock(&fs_devices->device_list_mutex);
858 return ERR_PTR(-ENOMEM);
860 rcu_assign_pointer(device->name, name);
862 list_add_rcu(&device->dev_list, &fs_devices->devices);
863 fs_devices->num_devices++;
865 device->fs_devices = fs_devices;
866 *new_device_added = true;
868 if (disk_super->label[0])
869 pr_info(
870 "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n",
871 disk_super->label, devid, found_transid, path,
872 current->comm, task_pid_nr(current));
873 else
874 pr_info(
875 "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n",
876 disk_super->fsid, devid, found_transid, path,
877 current->comm, task_pid_nr(current));
879 } else if (!device->name || strcmp(device->name->str, path)) {
881 * When FS is already mounted.
882 * 1. If you are here and if the device->name is NULL that
883 * means this device was missing at time of FS mount.
884 * 2. If you are here and if the device->name is different
885 * from 'path' that means either
886 * a. The same device disappeared and reappeared with
887 * different name. or
888 * b. The missing-disk-which-was-replaced, has
889 * reappeared now.
891 * We must allow 1 and 2a above. But 2b would be a spurious
892 * and unintentional.
894 * Further in case of 1 and 2a above, the disk at 'path'
895 * would have missed some transaction when it was away and
896 * in case of 2a the stale bdev has to be updated as well.
897 * 2b must not be allowed at all time.
901 * For now, we do allow update to btrfs_fs_device through the
902 * btrfs dev scan cli after FS has been mounted. We're still
903 * tracking a problem where systems fail mount by subvolume id
904 * when we reject replacement on a mounted FS.
906 if (!fs_devices->opened && found_transid < device->generation) {
908 * That is if the FS is _not_ mounted and if you
909 * are here, that means there is more than one
910 * disk with same uuid and devid.We keep the one
911 * with larger generation number or the last-in if
912 * generation are equal.
914 mutex_unlock(&fs_devices->device_list_mutex);
915 return ERR_PTR(-EEXIST);
919 * We are going to replace the device path for a given devid,
920 * make sure it's the same device if the device is mounted
922 if (device->bdev) {
923 struct block_device *path_bdev;
925 path_bdev = lookup_bdev(path);
926 if (IS_ERR(path_bdev)) {
927 mutex_unlock(&fs_devices->device_list_mutex);
928 return ERR_CAST(path_bdev);
931 if (device->bdev != path_bdev) {
932 bdput(path_bdev);
933 mutex_unlock(&fs_devices->device_list_mutex);
934 btrfs_warn_in_rcu(device->fs_info,
935 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
936 disk_super->fsid, devid,
937 rcu_str_deref(device->name), path);
938 return ERR_PTR(-EEXIST);
940 bdput(path_bdev);
941 btrfs_info_in_rcu(device->fs_info,
942 "device fsid %pU devid %llu moved old:%s new:%s",
943 disk_super->fsid, devid,
944 rcu_str_deref(device->name), path);
947 name = rcu_string_strdup(path, GFP_NOFS);
948 if (!name) {
949 mutex_unlock(&fs_devices->device_list_mutex);
950 return ERR_PTR(-ENOMEM);
952 rcu_string_free(device->name);
953 rcu_assign_pointer(device->name, name);
954 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
955 fs_devices->missing_devices--;
956 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
961 * Unmount does not free the btrfs_device struct but would zero
962 * generation along with most of the other members. So just update
963 * it back. We need it to pick the disk with largest generation
964 * (as above).
966 if (!fs_devices->opened) {
967 device->generation = found_transid;
968 fs_devices->latest_generation = max_t(u64, found_transid,
969 fs_devices->latest_generation);
972 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
974 mutex_unlock(&fs_devices->device_list_mutex);
975 return device;
978 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
980 struct btrfs_fs_devices *fs_devices;
981 struct btrfs_device *device;
982 struct btrfs_device *orig_dev;
983 int ret = 0;
985 fs_devices = alloc_fs_devices(orig->fsid, NULL);
986 if (IS_ERR(fs_devices))
987 return fs_devices;
989 mutex_lock(&orig->device_list_mutex);
990 fs_devices->total_devices = orig->total_devices;
992 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
993 struct rcu_string *name;
995 device = btrfs_alloc_device(NULL, &orig_dev->devid,
996 orig_dev->uuid);
997 if (IS_ERR(device)) {
998 ret = PTR_ERR(device);
999 goto error;
1003 * This is ok to do without rcu read locked because we hold the
1004 * uuid mutex so nothing we touch in here is going to disappear.
1006 if (orig_dev->name) {
1007 name = rcu_string_strdup(orig_dev->name->str,
1008 GFP_KERNEL);
1009 if (!name) {
1010 btrfs_free_device(device);
1011 ret = -ENOMEM;
1012 goto error;
1014 rcu_assign_pointer(device->name, name);
1017 list_add(&device->dev_list, &fs_devices->devices);
1018 device->fs_devices = fs_devices;
1019 fs_devices->num_devices++;
1021 mutex_unlock(&orig->device_list_mutex);
1022 return fs_devices;
1023 error:
1024 mutex_unlock(&orig->device_list_mutex);
1025 free_fs_devices(fs_devices);
1026 return ERR_PTR(ret);
1030 * After we have read the system tree and know devids belonging to
1031 * this filesystem, remove the device which does not belong there.
1033 void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
1035 struct btrfs_device *device, *next;
1036 struct btrfs_device *latest_dev = NULL;
1038 mutex_lock(&uuid_mutex);
1039 again:
1040 /* This is the initialized path, it is safe to release the devices. */
1041 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
1042 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1043 &device->dev_state)) {
1044 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1045 &device->dev_state) &&
1046 (!latest_dev ||
1047 device->generation > latest_dev->generation)) {
1048 latest_dev = device;
1050 continue;
1053 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1055 * In the first step, keep the device which has
1056 * the correct fsid and the devid that is used
1057 * for the dev_replace procedure.
1058 * In the second step, the dev_replace state is
1059 * read from the device tree and it is known
1060 * whether the procedure is really active or
1061 * not, which means whether this device is
1062 * used or whether it should be removed.
1064 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1065 &device->dev_state)) {
1066 continue;
1069 if (device->bdev) {
1070 blkdev_put(device->bdev, device->mode);
1071 device->bdev = NULL;
1072 fs_devices->open_devices--;
1074 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1075 list_del_init(&device->dev_alloc_list);
1076 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1077 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1078 &device->dev_state))
1079 fs_devices->rw_devices--;
1081 list_del_init(&device->dev_list);
1082 fs_devices->num_devices--;
1083 btrfs_free_device(device);
1086 if (fs_devices->seed) {
1087 fs_devices = fs_devices->seed;
1088 goto again;
1091 fs_devices->latest_bdev = latest_dev->bdev;
1093 mutex_unlock(&uuid_mutex);
1096 static void btrfs_close_bdev(struct btrfs_device *device)
1098 if (!device->bdev)
1099 return;
1101 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1102 sync_blockdev(device->bdev);
1103 invalidate_bdev(device->bdev);
1106 blkdev_put(device->bdev, device->mode);
1109 static void btrfs_close_one_device(struct btrfs_device *device)
1111 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1112 struct btrfs_device *new_device;
1113 struct rcu_string *name;
1115 if (device->bdev)
1116 fs_devices->open_devices--;
1118 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1119 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1120 list_del_init(&device->dev_alloc_list);
1121 fs_devices->rw_devices--;
1124 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
1125 fs_devices->missing_devices--;
1127 btrfs_close_bdev(device);
1129 new_device = btrfs_alloc_device(NULL, &device->devid,
1130 device->uuid);
1131 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1133 /* Safe because we are under uuid_mutex */
1134 if (device->name) {
1135 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1136 BUG_ON(!name); /* -ENOMEM */
1137 rcu_assign_pointer(new_device->name, name);
1140 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1141 new_device->fs_devices = device->fs_devices;
1143 synchronize_rcu();
1144 btrfs_free_device(device);
1147 static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
1149 struct btrfs_device *device, *tmp;
1151 if (--fs_devices->opened > 0)
1152 return 0;
1154 mutex_lock(&fs_devices->device_list_mutex);
1155 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
1156 btrfs_close_one_device(device);
1158 mutex_unlock(&fs_devices->device_list_mutex);
1160 WARN_ON(fs_devices->open_devices);
1161 WARN_ON(fs_devices->rw_devices);
1162 fs_devices->opened = 0;
1163 fs_devices->seeding = false;
1165 return 0;
1168 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1170 struct btrfs_fs_devices *seed_devices = NULL;
1171 int ret;
1173 mutex_lock(&uuid_mutex);
1174 ret = close_fs_devices(fs_devices);
1175 if (!fs_devices->opened) {
1176 seed_devices = fs_devices->seed;
1177 fs_devices->seed = NULL;
1179 mutex_unlock(&uuid_mutex);
1181 while (seed_devices) {
1182 fs_devices = seed_devices;
1183 seed_devices = fs_devices->seed;
1184 close_fs_devices(fs_devices);
1185 free_fs_devices(fs_devices);
1187 return ret;
1190 static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
1191 fmode_t flags, void *holder)
1193 struct btrfs_device *device;
1194 struct btrfs_device *latest_dev = NULL;
1195 int ret = 0;
1197 flags |= FMODE_EXCL;
1199 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1200 /* Just open everything we can; ignore failures here */
1201 if (btrfs_open_one_device(fs_devices, device, flags, holder))
1202 continue;
1204 if (!latest_dev ||
1205 device->generation > latest_dev->generation)
1206 latest_dev = device;
1208 if (fs_devices->open_devices == 0) {
1209 ret = -EINVAL;
1210 goto out;
1212 fs_devices->opened = 1;
1213 fs_devices->latest_bdev = latest_dev->bdev;
1214 fs_devices->total_rw_bytes = 0;
1215 out:
1216 return ret;
1219 static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1221 struct btrfs_device *dev1, *dev2;
1223 dev1 = list_entry(a, struct btrfs_device, dev_list);
1224 dev2 = list_entry(b, struct btrfs_device, dev_list);
1226 if (dev1->devid < dev2->devid)
1227 return -1;
1228 else if (dev1->devid > dev2->devid)
1229 return 1;
1230 return 0;
1233 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1234 fmode_t flags, void *holder)
1236 int ret;
1238 lockdep_assert_held(&uuid_mutex);
1240 mutex_lock(&fs_devices->device_list_mutex);
1241 if (fs_devices->opened) {
1242 fs_devices->opened++;
1243 ret = 0;
1244 } else {
1245 list_sort(NULL, &fs_devices->devices, devid_cmp);
1246 ret = open_fs_devices(fs_devices, flags, holder);
1248 mutex_unlock(&fs_devices->device_list_mutex);
1250 return ret;
1253 static void btrfs_release_disk_super(struct page *page)
1255 kunmap(page);
1256 put_page(page);
1259 static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1260 struct page **page,
1261 struct btrfs_super_block **disk_super)
1263 void *p;
1264 pgoff_t index;
1266 /* make sure our super fits in the device */
1267 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1268 return 1;
1270 /* make sure our super fits in the page */
1271 if (sizeof(**disk_super) > PAGE_SIZE)
1272 return 1;
1274 /* make sure our super doesn't straddle pages on disk */
1275 index = bytenr >> PAGE_SHIFT;
1276 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1277 return 1;
1279 /* pull in the page with our super */
1280 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1281 index, GFP_KERNEL);
1283 if (IS_ERR_OR_NULL(*page))
1284 return 1;
1286 p = kmap(*page);
1288 /* align our pointer to the offset of the super block */
1289 *disk_super = p + offset_in_page(bytenr);
1291 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1292 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1293 btrfs_release_disk_super(*page);
1294 return 1;
1297 if ((*disk_super)->label[0] &&
1298 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1299 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1301 return 0;
1304 int btrfs_forget_devices(const char *path)
1306 int ret;
1308 mutex_lock(&uuid_mutex);
1309 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1310 mutex_unlock(&uuid_mutex);
1312 return ret;
1316 * Look for a btrfs signature on a device. This may be called out of the mount path
1317 * and we are not allowed to call set_blocksize during the scan. The superblock
1318 * is read via pagecache
1320 struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1321 void *holder)
1323 struct btrfs_super_block *disk_super;
1324 bool new_device_added = false;
1325 struct btrfs_device *device = NULL;
1326 struct block_device *bdev;
1327 struct page *page;
1328 u64 bytenr;
1330 lockdep_assert_held(&uuid_mutex);
1333 * we would like to check all the supers, but that would make
1334 * a btrfs mount succeed after a mkfs from a different FS.
1335 * So, we need to add a special mount option to scan for
1336 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1338 bytenr = btrfs_sb_offset(0);
1339 flags |= FMODE_EXCL;
1341 bdev = blkdev_get_by_path(path, flags, holder);
1342 if (IS_ERR(bdev))
1343 return ERR_CAST(bdev);
1345 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1346 device = ERR_PTR(-EINVAL);
1347 goto error_bdev_put;
1350 device = device_list_add(path, disk_super, &new_device_added);
1351 if (!IS_ERR(device)) {
1352 if (new_device_added)
1353 btrfs_free_stale_devices(path, device);
1356 btrfs_release_disk_super(page);
1358 error_bdev_put:
1359 blkdev_put(bdev, flags);
1361 return device;
1365 * Try to find a chunk that intersects [start, start + len] range and when one
1366 * such is found, record the end of it in *start
1368 static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
1369 u64 len)
1371 u64 physical_start, physical_end;
1373 lockdep_assert_held(&device->fs_info->chunk_mutex);
1375 if (!find_first_extent_bit(&device->alloc_state, *start,
1376 &physical_start, &physical_end,
1377 CHUNK_ALLOCATED, NULL)) {
1379 if (in_range(physical_start, *start, len) ||
1380 in_range(*start, physical_start,
1381 physical_end - physical_start)) {
1382 *start = physical_end + 1;
1383 return true;
1386 return false;
1391 * find_free_dev_extent_start - find free space in the specified device
1392 * @device: the device which we search the free space in
1393 * @num_bytes: the size of the free space that we need
1394 * @search_start: the position from which to begin the search
1395 * @start: store the start of the free space.
1396 * @len: the size of the free space. that we find, or the size
1397 * of the max free space if we don't find suitable free space
1399 * this uses a pretty simple search, the expectation is that it is
1400 * called very infrequently and that a given device has a small number
1401 * of extents
1403 * @start is used to store the start of the free space if we find. But if we
1404 * don't find suitable free space, it will be used to store the start position
1405 * of the max free space.
1407 * @len is used to store the size of the free space that we find.
1408 * But if we don't find suitable free space, it is used to store the size of
1409 * the max free space.
1411 * NOTE: This function will search *commit* root of device tree, and does extra
1412 * check to ensure dev extents are not double allocated.
1413 * This makes the function safe to allocate dev extents but may not report
1414 * correct usable device space, as device extent freed in current transaction
1415 * is not reported as avaiable.
1417 static int find_free_dev_extent_start(struct btrfs_device *device,
1418 u64 num_bytes, u64 search_start, u64 *start,
1419 u64 *len)
1421 struct btrfs_fs_info *fs_info = device->fs_info;
1422 struct btrfs_root *root = fs_info->dev_root;
1423 struct btrfs_key key;
1424 struct btrfs_dev_extent *dev_extent;
1425 struct btrfs_path *path;
1426 u64 hole_size;
1427 u64 max_hole_start;
1428 u64 max_hole_size;
1429 u64 extent_end;
1430 u64 search_end = device->total_bytes;
1431 int ret;
1432 int slot;
1433 struct extent_buffer *l;
1436 * We don't want to overwrite the superblock on the drive nor any area
1437 * used by the boot loader (grub for example), so we make sure to start
1438 * at an offset of at least 1MB.
1440 search_start = max_t(u64, search_start, SZ_1M);
1442 path = btrfs_alloc_path();
1443 if (!path)
1444 return -ENOMEM;
1446 max_hole_start = search_start;
1447 max_hole_size = 0;
1449 again:
1450 if (search_start >= search_end ||
1451 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1452 ret = -ENOSPC;
1453 goto out;
1456 path->reada = READA_FORWARD;
1457 path->search_commit_root = 1;
1458 path->skip_locking = 1;
1460 key.objectid = device->devid;
1461 key.offset = search_start;
1462 key.type = BTRFS_DEV_EXTENT_KEY;
1464 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1465 if (ret < 0)
1466 goto out;
1467 if (ret > 0) {
1468 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1469 if (ret < 0)
1470 goto out;
1473 while (1) {
1474 l = path->nodes[0];
1475 slot = path->slots[0];
1476 if (slot >= btrfs_header_nritems(l)) {
1477 ret = btrfs_next_leaf(root, path);
1478 if (ret == 0)
1479 continue;
1480 if (ret < 0)
1481 goto out;
1483 break;
1485 btrfs_item_key_to_cpu(l, &key, slot);
1487 if (key.objectid < device->devid)
1488 goto next;
1490 if (key.objectid > device->devid)
1491 break;
1493 if (key.type != BTRFS_DEV_EXTENT_KEY)
1494 goto next;
1496 if (key.offset > search_start) {
1497 hole_size = key.offset - search_start;
1500 * Have to check before we set max_hole_start, otherwise
1501 * we could end up sending back this offset anyway.
1503 if (contains_pending_extent(device, &search_start,
1504 hole_size)) {
1505 if (key.offset >= search_start)
1506 hole_size = key.offset - search_start;
1507 else
1508 hole_size = 0;
1511 if (hole_size > max_hole_size) {
1512 max_hole_start = search_start;
1513 max_hole_size = hole_size;
1517 * If this free space is greater than which we need,
1518 * it must be the max free space that we have found
1519 * until now, so max_hole_start must point to the start
1520 * of this free space and the length of this free space
1521 * is stored in max_hole_size. Thus, we return
1522 * max_hole_start and max_hole_size and go back to the
1523 * caller.
1525 if (hole_size >= num_bytes) {
1526 ret = 0;
1527 goto out;
1531 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1532 extent_end = key.offset + btrfs_dev_extent_length(l,
1533 dev_extent);
1534 if (extent_end > search_start)
1535 search_start = extent_end;
1536 next:
1537 path->slots[0]++;
1538 cond_resched();
1542 * At this point, search_start should be the end of
1543 * allocated dev extents, and when shrinking the device,
1544 * search_end may be smaller than search_start.
1546 if (search_end > search_start) {
1547 hole_size = search_end - search_start;
1549 if (contains_pending_extent(device, &search_start, hole_size)) {
1550 btrfs_release_path(path);
1551 goto again;
1554 if (hole_size > max_hole_size) {
1555 max_hole_start = search_start;
1556 max_hole_size = hole_size;
1560 /* See above. */
1561 if (max_hole_size < num_bytes)
1562 ret = -ENOSPC;
1563 else
1564 ret = 0;
1566 out:
1567 btrfs_free_path(path);
1568 *start = max_hole_start;
1569 if (len)
1570 *len = max_hole_size;
1571 return ret;
1574 int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
1575 u64 *start, u64 *len)
1577 /* FIXME use last free of some kind */
1578 return find_free_dev_extent_start(device, num_bytes, 0, start, len);
1581 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1582 struct btrfs_device *device,
1583 u64 start, u64 *dev_extent_len)
1585 struct btrfs_fs_info *fs_info = device->fs_info;
1586 struct btrfs_root *root = fs_info->dev_root;
1587 int ret;
1588 struct btrfs_path *path;
1589 struct btrfs_key key;
1590 struct btrfs_key found_key;
1591 struct extent_buffer *leaf = NULL;
1592 struct btrfs_dev_extent *extent = NULL;
1594 path = btrfs_alloc_path();
1595 if (!path)
1596 return -ENOMEM;
1598 key.objectid = device->devid;
1599 key.offset = start;
1600 key.type = BTRFS_DEV_EXTENT_KEY;
1601 again:
1602 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1603 if (ret > 0) {
1604 ret = btrfs_previous_item(root, path, key.objectid,
1605 BTRFS_DEV_EXTENT_KEY);
1606 if (ret)
1607 goto out;
1608 leaf = path->nodes[0];
1609 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1610 extent = btrfs_item_ptr(leaf, path->slots[0],
1611 struct btrfs_dev_extent);
1612 BUG_ON(found_key.offset > start || found_key.offset +
1613 btrfs_dev_extent_length(leaf, extent) < start);
1614 key = found_key;
1615 btrfs_release_path(path);
1616 goto again;
1617 } else if (ret == 0) {
1618 leaf = path->nodes[0];
1619 extent = btrfs_item_ptr(leaf, path->slots[0],
1620 struct btrfs_dev_extent);
1621 } else {
1622 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
1623 goto out;
1626 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1628 ret = btrfs_del_item(trans, root, path);
1629 if (ret) {
1630 btrfs_handle_fs_error(fs_info, ret,
1631 "Failed to remove dev extent item");
1632 } else {
1633 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
1635 out:
1636 btrfs_free_path(path);
1637 return ret;
1640 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1641 struct btrfs_device *device,
1642 u64 chunk_offset, u64 start, u64 num_bytes)
1644 int ret;
1645 struct btrfs_path *path;
1646 struct btrfs_fs_info *fs_info = device->fs_info;
1647 struct btrfs_root *root = fs_info->dev_root;
1648 struct btrfs_dev_extent *extent;
1649 struct extent_buffer *leaf;
1650 struct btrfs_key key;
1652 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
1653 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1654 path = btrfs_alloc_path();
1655 if (!path)
1656 return -ENOMEM;
1658 key.objectid = device->devid;
1659 key.offset = start;
1660 key.type = BTRFS_DEV_EXTENT_KEY;
1661 ret = btrfs_insert_empty_item(trans, root, path, &key,
1662 sizeof(*extent));
1663 if (ret)
1664 goto out;
1666 leaf = path->nodes[0];
1667 extent = btrfs_item_ptr(leaf, path->slots[0],
1668 struct btrfs_dev_extent);
1669 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1670 BTRFS_CHUNK_TREE_OBJECTID);
1671 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1672 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1673 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1675 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1676 btrfs_mark_buffer_dirty(leaf);
1677 out:
1678 btrfs_free_path(path);
1679 return ret;
1682 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1684 struct extent_map_tree *em_tree;
1685 struct extent_map *em;
1686 struct rb_node *n;
1687 u64 ret = 0;
1689 em_tree = &fs_info->mapping_tree;
1690 read_lock(&em_tree->lock);
1691 n = rb_last(&em_tree->map.rb_root);
1692 if (n) {
1693 em = rb_entry(n, struct extent_map, rb_node);
1694 ret = em->start + em->len;
1696 read_unlock(&em_tree->lock);
1698 return ret;
1701 static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1702 u64 *devid_ret)
1704 int ret;
1705 struct btrfs_key key;
1706 struct btrfs_key found_key;
1707 struct btrfs_path *path;
1709 path = btrfs_alloc_path();
1710 if (!path)
1711 return -ENOMEM;
1713 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1714 key.type = BTRFS_DEV_ITEM_KEY;
1715 key.offset = (u64)-1;
1717 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1718 if (ret < 0)
1719 goto error;
1721 if (ret == 0) {
1722 /* Corruption */
1723 btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");
1724 ret = -EUCLEAN;
1725 goto error;
1728 ret = btrfs_previous_item(fs_info->chunk_root, path,
1729 BTRFS_DEV_ITEMS_OBJECTID,
1730 BTRFS_DEV_ITEM_KEY);
1731 if (ret) {
1732 *devid_ret = 1;
1733 } else {
1734 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1735 path->slots[0]);
1736 *devid_ret = found_key.offset + 1;
1738 ret = 0;
1739 error:
1740 btrfs_free_path(path);
1741 return ret;
1745 * the device information is stored in the chunk root
1746 * the btrfs_device struct should be fully filled in
1748 static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
1749 struct btrfs_device *device)
1751 int ret;
1752 struct btrfs_path *path;
1753 struct btrfs_dev_item *dev_item;
1754 struct extent_buffer *leaf;
1755 struct btrfs_key key;
1756 unsigned long ptr;
1758 path = btrfs_alloc_path();
1759 if (!path)
1760 return -ENOMEM;
1762 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1763 key.type = BTRFS_DEV_ITEM_KEY;
1764 key.offset = device->devid;
1766 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1767 &key, sizeof(*dev_item));
1768 if (ret)
1769 goto out;
1771 leaf = path->nodes[0];
1772 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1774 btrfs_set_device_id(leaf, dev_item, device->devid);
1775 btrfs_set_device_generation(leaf, dev_item, 0);
1776 btrfs_set_device_type(leaf, dev_item, device->type);
1777 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1778 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1779 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1780 btrfs_set_device_total_bytes(leaf, dev_item,
1781 btrfs_device_get_disk_total_bytes(device));
1782 btrfs_set_device_bytes_used(leaf, dev_item,
1783 btrfs_device_get_bytes_used(device));
1784 btrfs_set_device_group(leaf, dev_item, 0);
1785 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1786 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1787 btrfs_set_device_start_offset(leaf, dev_item, 0);
1789 ptr = btrfs_device_uuid(dev_item);
1790 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1791 ptr = btrfs_device_fsid(dev_item);
1792 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1793 ptr, BTRFS_FSID_SIZE);
1794 btrfs_mark_buffer_dirty(leaf);
1796 ret = 0;
1797 out:
1798 btrfs_free_path(path);
1799 return ret;
1803 * Function to update ctime/mtime for a given device path.
1804 * Mainly used for ctime/mtime based probe like libblkid.
1806 static void update_dev_time(const char *path_name)
1808 struct file *filp;
1810 filp = filp_open(path_name, O_RDWR, 0);
1811 if (IS_ERR(filp))
1812 return;
1813 file_update_time(filp);
1814 filp_close(filp, NULL);
1817 static int btrfs_rm_dev_item(struct btrfs_device *device)
1819 struct btrfs_root *root = device->fs_info->chunk_root;
1820 int ret;
1821 struct btrfs_path *path;
1822 struct btrfs_key key;
1823 struct btrfs_trans_handle *trans;
1825 path = btrfs_alloc_path();
1826 if (!path)
1827 return -ENOMEM;
1829 trans = btrfs_start_transaction(root, 0);
1830 if (IS_ERR(trans)) {
1831 btrfs_free_path(path);
1832 return PTR_ERR(trans);
1834 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1835 key.type = BTRFS_DEV_ITEM_KEY;
1836 key.offset = device->devid;
1838 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1839 if (ret) {
1840 if (ret > 0)
1841 ret = -ENOENT;
1842 btrfs_abort_transaction(trans, ret);
1843 btrfs_end_transaction(trans);
1844 goto out;
1847 ret = btrfs_del_item(trans, root, path);
1848 if (ret) {
1849 btrfs_abort_transaction(trans, ret);
1850 btrfs_end_transaction(trans);
1853 out:
1854 btrfs_free_path(path);
1855 if (!ret)
1856 ret = btrfs_commit_transaction(trans);
1857 return ret;
1861 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1862 * filesystem. It's up to the caller to adjust that number regarding eg. device
1863 * replace.
1865 static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1866 u64 num_devices)
1868 u64 all_avail;
1869 unsigned seq;
1870 int i;
1872 do {
1873 seq = read_seqbegin(&fs_info->profiles_lock);
1875 all_avail = fs_info->avail_data_alloc_bits |
1876 fs_info->avail_system_alloc_bits |
1877 fs_info->avail_metadata_alloc_bits;
1878 } while (read_seqretry(&fs_info->profiles_lock, seq));
1880 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1881 if (!(all_avail & btrfs_raid_array[i].bg_flag))
1882 continue;
1884 if (num_devices < btrfs_raid_array[i].devs_min) {
1885 int ret = btrfs_raid_array[i].mindev_error;
1887 if (ret)
1888 return ret;
1892 return 0;
1895 static struct btrfs_device * btrfs_find_next_active_device(
1896 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
1898 struct btrfs_device *next_device;
1900 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1901 if (next_device != device &&
1902 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1903 && next_device->bdev)
1904 return next_device;
1907 return NULL;
1911 * Helper function to check if the given device is part of s_bdev / latest_bdev
1912 * and replace it with the provided or the next active device, in the context
1913 * where this function called, there should be always be another device (or
1914 * this_dev) which is active.
1916 void __cold btrfs_assign_next_active_device(struct btrfs_device *device,
1917 struct btrfs_device *this_dev)
1919 struct btrfs_fs_info *fs_info = device->fs_info;
1920 struct btrfs_device *next_device;
1922 if (this_dev)
1923 next_device = this_dev;
1924 else
1925 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1926 device);
1927 ASSERT(next_device);
1929 if (fs_info->sb->s_bdev &&
1930 (fs_info->sb->s_bdev == device->bdev))
1931 fs_info->sb->s_bdev = next_device->bdev;
1933 if (fs_info->fs_devices->latest_bdev == device->bdev)
1934 fs_info->fs_devices->latest_bdev = next_device->bdev;
1938 * Return btrfs_fs_devices::num_devices excluding the device that's being
1939 * currently replaced.
1941 static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
1943 u64 num_devices = fs_info->fs_devices->num_devices;
1945 down_read(&fs_info->dev_replace.rwsem);
1946 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1947 ASSERT(num_devices > 1);
1948 num_devices--;
1950 up_read(&fs_info->dev_replace.rwsem);
1952 return num_devices;
1955 int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1956 u64 devid)
1958 struct btrfs_device *device;
1959 struct btrfs_fs_devices *cur_devices;
1960 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1961 u64 num_devices;
1962 int ret = 0;
1964 mutex_lock(&uuid_mutex);
1966 num_devices = btrfs_num_devices(fs_info);
1968 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
1969 if (ret)
1970 goto out;
1972 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
1974 if (IS_ERR(device)) {
1975 if (PTR_ERR(device) == -ENOENT &&
1976 strcmp(device_path, "missing") == 0)
1977 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
1978 else
1979 ret = PTR_ERR(device);
1980 goto out;
1983 if (btrfs_pinned_by_swapfile(fs_info, device)) {
1984 btrfs_warn_in_rcu(fs_info,
1985 "cannot remove device %s (devid %llu) due to active swapfile",
1986 rcu_str_deref(device->name), device->devid);
1987 ret = -ETXTBSY;
1988 goto out;
1991 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1992 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
1993 goto out;
1996 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1997 fs_info->fs_devices->rw_devices == 1) {
1998 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1999 goto out;
2002 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
2003 mutex_lock(&fs_info->chunk_mutex);
2004 list_del_init(&device->dev_alloc_list);
2005 device->fs_devices->rw_devices--;
2006 mutex_unlock(&fs_info->chunk_mutex);
2009 mutex_unlock(&uuid_mutex);
2010 ret = btrfs_shrink_device(device, 0);
2011 mutex_lock(&uuid_mutex);
2012 if (ret)
2013 goto error_undo;
2016 * TODO: the superblock still includes this device in its num_devices
2017 * counter although write_all_supers() is not locked out. This
2018 * could give a filesystem state which requires a degraded mount.
2020 ret = btrfs_rm_dev_item(device);
2021 if (ret)
2022 goto error_undo;
2024 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2025 btrfs_scrub_cancel_dev(device);
2028 * the device list mutex makes sure that we don't change
2029 * the device list while someone else is writing out all
2030 * the device supers. Whoever is writing all supers, should
2031 * lock the device list mutex before getting the number of
2032 * devices in the super block (super_copy). Conversely,
2033 * whoever updates the number of devices in the super block
2034 * (super_copy) should hold the device list mutex.
2038 * In normal cases the cur_devices == fs_devices. But in case
2039 * of deleting a seed device, the cur_devices should point to
2040 * its own fs_devices listed under the fs_devices->seed.
2042 cur_devices = device->fs_devices;
2043 mutex_lock(&fs_devices->device_list_mutex);
2044 list_del_rcu(&device->dev_list);
2046 cur_devices->num_devices--;
2047 cur_devices->total_devices--;
2048 /* Update total_devices of the parent fs_devices if it's seed */
2049 if (cur_devices != fs_devices)
2050 fs_devices->total_devices--;
2052 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
2053 cur_devices->missing_devices--;
2055 btrfs_assign_next_active_device(device, NULL);
2057 if (device->bdev) {
2058 cur_devices->open_devices--;
2059 /* remove sysfs entry */
2060 btrfs_sysfs_rm_device_link(fs_devices, device);
2063 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2064 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
2065 mutex_unlock(&fs_devices->device_list_mutex);
2068 * at this point, the device is zero sized and detached from
2069 * the devices list. All that's left is to zero out the old
2070 * supers and free the device.
2072 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2073 btrfs_scratch_superblocks(device->bdev, device->name->str);
2075 btrfs_close_bdev(device);
2076 synchronize_rcu();
2077 btrfs_free_device(device);
2079 if (cur_devices->open_devices == 0) {
2080 while (fs_devices) {
2081 if (fs_devices->seed == cur_devices) {
2082 fs_devices->seed = cur_devices->seed;
2083 break;
2085 fs_devices = fs_devices->seed;
2087 cur_devices->seed = NULL;
2088 close_fs_devices(cur_devices);
2089 free_fs_devices(cur_devices);
2092 out:
2093 mutex_unlock(&uuid_mutex);
2094 return ret;
2096 error_undo:
2097 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
2098 mutex_lock(&fs_info->chunk_mutex);
2099 list_add(&device->dev_alloc_list,
2100 &fs_devices->alloc_list);
2101 device->fs_devices->rw_devices++;
2102 mutex_unlock(&fs_info->chunk_mutex);
2104 goto out;
2107 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
2109 struct btrfs_fs_devices *fs_devices;
2111 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
2114 * in case of fs with no seed, srcdev->fs_devices will point
2115 * to fs_devices of fs_info. However when the dev being replaced is
2116 * a seed dev it will point to the seed's local fs_devices. In short
2117 * srcdev will have its correct fs_devices in both the cases.
2119 fs_devices = srcdev->fs_devices;
2121 list_del_rcu(&srcdev->dev_list);
2122 list_del(&srcdev->dev_alloc_list);
2123 fs_devices->num_devices--;
2124 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
2125 fs_devices->missing_devices--;
2127 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
2128 fs_devices->rw_devices--;
2130 if (srcdev->bdev)
2131 fs_devices->open_devices--;
2134 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev)
2136 struct btrfs_fs_info *fs_info = srcdev->fs_info;
2137 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
2139 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
2140 /* zero out the old super if it is writable */
2141 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2144 btrfs_close_bdev(srcdev);
2145 synchronize_rcu();
2146 btrfs_free_device(srcdev);
2148 /* if this is no devs we rather delete the fs_devices */
2149 if (!fs_devices->num_devices) {
2150 struct btrfs_fs_devices *tmp_fs_devices;
2153 * On a mounted FS, num_devices can't be zero unless it's a
2154 * seed. In case of a seed device being replaced, the replace
2155 * target added to the sprout FS, so there will be no more
2156 * device left under the seed FS.
2158 ASSERT(fs_devices->seeding);
2160 tmp_fs_devices = fs_info->fs_devices;
2161 while (tmp_fs_devices) {
2162 if (tmp_fs_devices->seed == fs_devices) {
2163 tmp_fs_devices->seed = fs_devices->seed;
2164 break;
2166 tmp_fs_devices = tmp_fs_devices->seed;
2168 fs_devices->seed = NULL;
2169 close_fs_devices(fs_devices);
2170 free_fs_devices(fs_devices);
2174 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
2176 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
2178 WARN_ON(!tgtdev);
2179 mutex_lock(&fs_devices->device_list_mutex);
2181 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
2183 if (tgtdev->bdev)
2184 fs_devices->open_devices--;
2186 fs_devices->num_devices--;
2188 btrfs_assign_next_active_device(tgtdev, NULL);
2190 list_del_rcu(&tgtdev->dev_list);
2192 mutex_unlock(&fs_devices->device_list_mutex);
2195 * The update_dev_time() with in btrfs_scratch_superblocks()
2196 * may lead to a call to btrfs_show_devname() which will try
2197 * to hold device_list_mutex. And here this device
2198 * is already out of device list, so we don't have to hold
2199 * the device_list_mutex lock.
2201 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
2203 btrfs_close_bdev(tgtdev);
2204 synchronize_rcu();
2205 btrfs_free_device(tgtdev);
2208 static struct btrfs_device *btrfs_find_device_by_path(
2209 struct btrfs_fs_info *fs_info, const char *device_path)
2211 int ret = 0;
2212 struct btrfs_super_block *disk_super;
2213 u64 devid;
2214 u8 *dev_uuid;
2215 struct block_device *bdev;
2216 struct buffer_head *bh;
2217 struct btrfs_device *device;
2219 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2220 fs_info->bdev_holder, 0, &bdev, &bh);
2221 if (ret)
2222 return ERR_PTR(ret);
2223 disk_super = (struct btrfs_super_block *)bh->b_data;
2224 devid = btrfs_stack_device_id(&disk_super->dev_item);
2225 dev_uuid = disk_super->dev_item.uuid;
2226 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
2227 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2228 disk_super->metadata_uuid, true);
2229 else
2230 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2231 disk_super->fsid, true);
2233 brelse(bh);
2234 if (!device)
2235 device = ERR_PTR(-ENOENT);
2236 blkdev_put(bdev, FMODE_READ);
2237 return device;
2241 * Lookup a device given by device id, or the path if the id is 0.
2243 struct btrfs_device *btrfs_find_device_by_devspec(
2244 struct btrfs_fs_info *fs_info, u64 devid,
2245 const char *device_path)
2247 struct btrfs_device *device;
2249 if (devid) {
2250 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
2251 NULL, true);
2252 if (!device)
2253 return ERR_PTR(-ENOENT);
2254 return device;
2257 if (!device_path || !device_path[0])
2258 return ERR_PTR(-EINVAL);
2260 if (strcmp(device_path, "missing") == 0) {
2261 /* Find first missing device */
2262 list_for_each_entry(device, &fs_info->fs_devices->devices,
2263 dev_list) {
2264 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2265 &device->dev_state) && !device->bdev)
2266 return device;
2268 return ERR_PTR(-ENOENT);
2271 return btrfs_find_device_by_path(fs_info, device_path);
2275 * does all the dirty work required for changing file system's UUID.
2277 static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
2279 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2280 struct btrfs_fs_devices *old_devices;
2281 struct btrfs_fs_devices *seed_devices;
2282 struct btrfs_super_block *disk_super = fs_info->super_copy;
2283 struct btrfs_device *device;
2284 u64 super_flags;
2286 lockdep_assert_held(&uuid_mutex);
2287 if (!fs_devices->seeding)
2288 return -EINVAL;
2290 seed_devices = alloc_fs_devices(NULL, NULL);
2291 if (IS_ERR(seed_devices))
2292 return PTR_ERR(seed_devices);
2294 old_devices = clone_fs_devices(fs_devices);
2295 if (IS_ERR(old_devices)) {
2296 kfree(seed_devices);
2297 return PTR_ERR(old_devices);
2300 list_add(&old_devices->fs_list, &fs_uuids);
2302 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2303 seed_devices->opened = 1;
2304 INIT_LIST_HEAD(&seed_devices->devices);
2305 INIT_LIST_HEAD(&seed_devices->alloc_list);
2306 mutex_init(&seed_devices->device_list_mutex);
2308 mutex_lock(&fs_devices->device_list_mutex);
2309 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2310 synchronize_rcu);
2311 list_for_each_entry(device, &seed_devices->devices, dev_list)
2312 device->fs_devices = seed_devices;
2314 mutex_lock(&fs_info->chunk_mutex);
2315 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2316 mutex_unlock(&fs_info->chunk_mutex);
2318 fs_devices->seeding = false;
2319 fs_devices->num_devices = 0;
2320 fs_devices->open_devices = 0;
2321 fs_devices->missing_devices = 0;
2322 fs_devices->rotating = false;
2323 fs_devices->seed = seed_devices;
2325 generate_random_uuid(fs_devices->fsid);
2326 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
2327 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2328 mutex_unlock(&fs_devices->device_list_mutex);
2330 super_flags = btrfs_super_flags(disk_super) &
2331 ~BTRFS_SUPER_FLAG_SEEDING;
2332 btrfs_set_super_flags(disk_super, super_flags);
2334 return 0;
2338 * Store the expected generation for seed devices in device items.
2340 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
2342 struct btrfs_fs_info *fs_info = trans->fs_info;
2343 struct btrfs_root *root = fs_info->chunk_root;
2344 struct btrfs_path *path;
2345 struct extent_buffer *leaf;
2346 struct btrfs_dev_item *dev_item;
2347 struct btrfs_device *device;
2348 struct btrfs_key key;
2349 u8 fs_uuid[BTRFS_FSID_SIZE];
2350 u8 dev_uuid[BTRFS_UUID_SIZE];
2351 u64 devid;
2352 int ret;
2354 path = btrfs_alloc_path();
2355 if (!path)
2356 return -ENOMEM;
2358 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2359 key.offset = 0;
2360 key.type = BTRFS_DEV_ITEM_KEY;
2362 while (1) {
2363 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2364 if (ret < 0)
2365 goto error;
2367 leaf = path->nodes[0];
2368 next_slot:
2369 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2370 ret = btrfs_next_leaf(root, path);
2371 if (ret > 0)
2372 break;
2373 if (ret < 0)
2374 goto error;
2375 leaf = path->nodes[0];
2376 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2377 btrfs_release_path(path);
2378 continue;
2381 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2382 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2383 key.type != BTRFS_DEV_ITEM_KEY)
2384 break;
2386 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2387 struct btrfs_dev_item);
2388 devid = btrfs_device_id(leaf, dev_item);
2389 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2390 BTRFS_UUID_SIZE);
2391 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2392 BTRFS_FSID_SIZE);
2393 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2394 fs_uuid, true);
2395 BUG_ON(!device); /* Logic error */
2397 if (device->fs_devices->seeding) {
2398 btrfs_set_device_generation(leaf, dev_item,
2399 device->generation);
2400 btrfs_mark_buffer_dirty(leaf);
2403 path->slots[0]++;
2404 goto next_slot;
2406 ret = 0;
2407 error:
2408 btrfs_free_path(path);
2409 return ret;
2412 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
2414 struct btrfs_root *root = fs_info->dev_root;
2415 struct request_queue *q;
2416 struct btrfs_trans_handle *trans;
2417 struct btrfs_device *device;
2418 struct block_device *bdev;
2419 struct super_block *sb = fs_info->sb;
2420 struct rcu_string *name;
2421 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2422 u64 orig_super_total_bytes;
2423 u64 orig_super_num_devices;
2424 int seeding_dev = 0;
2425 int ret = 0;
2426 bool unlocked = false;
2428 if (sb_rdonly(sb) && !fs_devices->seeding)
2429 return -EROFS;
2431 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2432 fs_info->bdev_holder);
2433 if (IS_ERR(bdev))
2434 return PTR_ERR(bdev);
2436 if (fs_devices->seeding) {
2437 seeding_dev = 1;
2438 down_write(&sb->s_umount);
2439 mutex_lock(&uuid_mutex);
2442 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2444 mutex_lock(&fs_devices->device_list_mutex);
2445 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2446 if (device->bdev == bdev) {
2447 ret = -EEXIST;
2448 mutex_unlock(
2449 &fs_devices->device_list_mutex);
2450 goto error;
2453 mutex_unlock(&fs_devices->device_list_mutex);
2455 device = btrfs_alloc_device(fs_info, NULL, NULL);
2456 if (IS_ERR(device)) {
2457 /* we can safely leave the fs_devices entry around */
2458 ret = PTR_ERR(device);
2459 goto error;
2462 name = rcu_string_strdup(device_path, GFP_KERNEL);
2463 if (!name) {
2464 ret = -ENOMEM;
2465 goto error_free_device;
2467 rcu_assign_pointer(device->name, name);
2469 trans = btrfs_start_transaction(root, 0);
2470 if (IS_ERR(trans)) {
2471 ret = PTR_ERR(trans);
2472 goto error_free_device;
2475 q = bdev_get_queue(bdev);
2476 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2477 device->generation = trans->transid;
2478 device->io_width = fs_info->sectorsize;
2479 device->io_align = fs_info->sectorsize;
2480 device->sector_size = fs_info->sectorsize;
2481 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2482 fs_info->sectorsize);
2483 device->disk_total_bytes = device->total_bytes;
2484 device->commit_total_bytes = device->total_bytes;
2485 device->fs_info = fs_info;
2486 device->bdev = bdev;
2487 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2488 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
2489 device->mode = FMODE_EXCL;
2490 device->dev_stats_valid = 1;
2491 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2493 if (seeding_dev) {
2494 sb->s_flags &= ~SB_RDONLY;
2495 ret = btrfs_prepare_sprout(fs_info);
2496 if (ret) {
2497 btrfs_abort_transaction(trans, ret);
2498 goto error_trans;
2502 device->fs_devices = fs_devices;
2504 mutex_lock(&fs_devices->device_list_mutex);
2505 mutex_lock(&fs_info->chunk_mutex);
2506 list_add_rcu(&device->dev_list, &fs_devices->devices);
2507 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2508 fs_devices->num_devices++;
2509 fs_devices->open_devices++;
2510 fs_devices->rw_devices++;
2511 fs_devices->total_devices++;
2512 fs_devices->total_rw_bytes += device->total_bytes;
2514 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
2516 if (!blk_queue_nonrot(q))
2517 fs_devices->rotating = true;
2519 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
2520 btrfs_set_super_total_bytes(fs_info->super_copy,
2521 round_down(orig_super_total_bytes + device->total_bytes,
2522 fs_info->sectorsize));
2524 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2525 btrfs_set_super_num_devices(fs_info->super_copy,
2526 orig_super_num_devices + 1);
2528 /* add sysfs device entry */
2529 btrfs_sysfs_add_device_link(fs_devices, device);
2532 * we've got more storage, clear any full flags on the space
2533 * infos
2535 btrfs_clear_space_info_full(fs_info);
2537 mutex_unlock(&fs_info->chunk_mutex);
2538 mutex_unlock(&fs_devices->device_list_mutex);
2540 if (seeding_dev) {
2541 mutex_lock(&fs_info->chunk_mutex);
2542 ret = init_first_rw_device(trans);
2543 mutex_unlock(&fs_info->chunk_mutex);
2544 if (ret) {
2545 btrfs_abort_transaction(trans, ret);
2546 goto error_sysfs;
2550 ret = btrfs_add_dev_item(trans, device);
2551 if (ret) {
2552 btrfs_abort_transaction(trans, ret);
2553 goto error_sysfs;
2556 if (seeding_dev) {
2557 ret = btrfs_finish_sprout(trans);
2558 if (ret) {
2559 btrfs_abort_transaction(trans, ret);
2560 goto error_sysfs;
2563 btrfs_sysfs_update_sprout_fsid(fs_devices,
2564 fs_info->fs_devices->fsid);
2567 ret = btrfs_commit_transaction(trans);
2569 if (seeding_dev) {
2570 mutex_unlock(&uuid_mutex);
2571 up_write(&sb->s_umount);
2572 unlocked = true;
2574 if (ret) /* transaction commit */
2575 return ret;
2577 ret = btrfs_relocate_sys_chunks(fs_info);
2578 if (ret < 0)
2579 btrfs_handle_fs_error(fs_info, ret,
2580 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2581 trans = btrfs_attach_transaction(root);
2582 if (IS_ERR(trans)) {
2583 if (PTR_ERR(trans) == -ENOENT)
2584 return 0;
2585 ret = PTR_ERR(trans);
2586 trans = NULL;
2587 goto error_sysfs;
2589 ret = btrfs_commit_transaction(trans);
2592 /* Update ctime/mtime for libblkid */
2593 update_dev_time(device_path);
2594 return ret;
2596 error_sysfs:
2597 btrfs_sysfs_rm_device_link(fs_devices, device);
2598 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2599 mutex_lock(&fs_info->chunk_mutex);
2600 list_del_rcu(&device->dev_list);
2601 list_del(&device->dev_alloc_list);
2602 fs_info->fs_devices->num_devices--;
2603 fs_info->fs_devices->open_devices--;
2604 fs_info->fs_devices->rw_devices--;
2605 fs_info->fs_devices->total_devices--;
2606 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2607 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2608 btrfs_set_super_total_bytes(fs_info->super_copy,
2609 orig_super_total_bytes);
2610 btrfs_set_super_num_devices(fs_info->super_copy,
2611 orig_super_num_devices);
2612 mutex_unlock(&fs_info->chunk_mutex);
2613 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2614 error_trans:
2615 if (seeding_dev)
2616 sb->s_flags |= SB_RDONLY;
2617 if (trans)
2618 btrfs_end_transaction(trans);
2619 error_free_device:
2620 btrfs_free_device(device);
2621 error:
2622 blkdev_put(bdev, FMODE_EXCL);
2623 if (seeding_dev && !unlocked) {
2624 mutex_unlock(&uuid_mutex);
2625 up_write(&sb->s_umount);
2627 return ret;
2630 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2631 struct btrfs_device *device)
2633 int ret;
2634 struct btrfs_path *path;
2635 struct btrfs_root *root = device->fs_info->chunk_root;
2636 struct btrfs_dev_item *dev_item;
2637 struct extent_buffer *leaf;
2638 struct btrfs_key key;
2640 path = btrfs_alloc_path();
2641 if (!path)
2642 return -ENOMEM;
2644 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2645 key.type = BTRFS_DEV_ITEM_KEY;
2646 key.offset = device->devid;
2648 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2649 if (ret < 0)
2650 goto out;
2652 if (ret > 0) {
2653 ret = -ENOENT;
2654 goto out;
2657 leaf = path->nodes[0];
2658 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2660 btrfs_set_device_id(leaf, dev_item, device->devid);
2661 btrfs_set_device_type(leaf, dev_item, device->type);
2662 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2663 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2664 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2665 btrfs_set_device_total_bytes(leaf, dev_item,
2666 btrfs_device_get_disk_total_bytes(device));
2667 btrfs_set_device_bytes_used(leaf, dev_item,
2668 btrfs_device_get_bytes_used(device));
2669 btrfs_mark_buffer_dirty(leaf);
2671 out:
2672 btrfs_free_path(path);
2673 return ret;
2676 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2677 struct btrfs_device *device, u64 new_size)
2679 struct btrfs_fs_info *fs_info = device->fs_info;
2680 struct btrfs_super_block *super_copy = fs_info->super_copy;
2681 u64 old_total;
2682 u64 diff;
2684 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2685 return -EACCES;
2687 new_size = round_down(new_size, fs_info->sectorsize);
2689 mutex_lock(&fs_info->chunk_mutex);
2690 old_total = btrfs_super_total_bytes(super_copy);
2691 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
2693 if (new_size <= device->total_bytes ||
2694 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
2695 mutex_unlock(&fs_info->chunk_mutex);
2696 return -EINVAL;
2699 btrfs_set_super_total_bytes(super_copy,
2700 round_down(old_total + diff, fs_info->sectorsize));
2701 device->fs_devices->total_rw_bytes += diff;
2703 btrfs_device_set_total_bytes(device, new_size);
2704 btrfs_device_set_disk_total_bytes(device, new_size);
2705 btrfs_clear_space_info_full(device->fs_info);
2706 if (list_empty(&device->post_commit_list))
2707 list_add_tail(&device->post_commit_list,
2708 &trans->transaction->dev_update_list);
2709 mutex_unlock(&fs_info->chunk_mutex);
2711 return btrfs_update_device(trans, device);
2714 static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
2716 struct btrfs_fs_info *fs_info = trans->fs_info;
2717 struct btrfs_root *root = fs_info->chunk_root;
2718 int ret;
2719 struct btrfs_path *path;
2720 struct btrfs_key key;
2722 path = btrfs_alloc_path();
2723 if (!path)
2724 return -ENOMEM;
2726 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2727 key.offset = chunk_offset;
2728 key.type = BTRFS_CHUNK_ITEM_KEY;
2730 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2731 if (ret < 0)
2732 goto out;
2733 else if (ret > 0) { /* Logic error or corruption */
2734 btrfs_handle_fs_error(fs_info, -ENOENT,
2735 "Failed lookup while freeing chunk.");
2736 ret = -ENOENT;
2737 goto out;
2740 ret = btrfs_del_item(trans, root, path);
2741 if (ret < 0)
2742 btrfs_handle_fs_error(fs_info, ret,
2743 "Failed to delete chunk item.");
2744 out:
2745 btrfs_free_path(path);
2746 return ret;
2749 static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2751 struct btrfs_super_block *super_copy = fs_info->super_copy;
2752 struct btrfs_disk_key *disk_key;
2753 struct btrfs_chunk *chunk;
2754 u8 *ptr;
2755 int ret = 0;
2756 u32 num_stripes;
2757 u32 array_size;
2758 u32 len = 0;
2759 u32 cur;
2760 struct btrfs_key key;
2762 mutex_lock(&fs_info->chunk_mutex);
2763 array_size = btrfs_super_sys_array_size(super_copy);
2765 ptr = super_copy->sys_chunk_array;
2766 cur = 0;
2768 while (cur < array_size) {
2769 disk_key = (struct btrfs_disk_key *)ptr;
2770 btrfs_disk_key_to_cpu(&key, disk_key);
2772 len = sizeof(*disk_key);
2774 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2775 chunk = (struct btrfs_chunk *)(ptr + len);
2776 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2777 len += btrfs_chunk_item_size(num_stripes);
2778 } else {
2779 ret = -EIO;
2780 break;
2782 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
2783 key.offset == chunk_offset) {
2784 memmove(ptr, ptr + len, array_size - (cur + len));
2785 array_size -= len;
2786 btrfs_set_super_sys_array_size(super_copy, array_size);
2787 } else {
2788 ptr += len;
2789 cur += len;
2792 mutex_unlock(&fs_info->chunk_mutex);
2793 return ret;
2797 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2798 * @logical: Logical block offset in bytes.
2799 * @length: Length of extent in bytes.
2801 * Return: Chunk mapping or ERR_PTR.
2803 struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2804 u64 logical, u64 length)
2806 struct extent_map_tree *em_tree;
2807 struct extent_map *em;
2809 em_tree = &fs_info->mapping_tree;
2810 read_lock(&em_tree->lock);
2811 em = lookup_extent_mapping(em_tree, logical, length);
2812 read_unlock(&em_tree->lock);
2814 if (!em) {
2815 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2816 logical, length);
2817 return ERR_PTR(-EINVAL);
2820 if (em->start > logical || em->start + em->len < logical) {
2821 btrfs_crit(fs_info,
2822 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2823 logical, length, em->start, em->start + em->len);
2824 free_extent_map(em);
2825 return ERR_PTR(-EINVAL);
2828 /* callers are responsible for dropping em's ref. */
2829 return em;
2832 int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
2834 struct btrfs_fs_info *fs_info = trans->fs_info;
2835 struct extent_map *em;
2836 struct map_lookup *map;
2837 u64 dev_extent_len = 0;
2838 int i, ret = 0;
2839 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2841 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
2842 if (IS_ERR(em)) {
2844 * This is a logic error, but we don't want to just rely on the
2845 * user having built with ASSERT enabled, so if ASSERT doesn't
2846 * do anything we still error out.
2848 ASSERT(0);
2849 return PTR_ERR(em);
2851 map = em->map_lookup;
2852 mutex_lock(&fs_info->chunk_mutex);
2853 check_system_chunk(trans, map->type);
2854 mutex_unlock(&fs_info->chunk_mutex);
2857 * Take the device list mutex to prevent races with the final phase of
2858 * a device replace operation that replaces the device object associated
2859 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2861 mutex_lock(&fs_devices->device_list_mutex);
2862 for (i = 0; i < map->num_stripes; i++) {
2863 struct btrfs_device *device = map->stripes[i].dev;
2864 ret = btrfs_free_dev_extent(trans, device,
2865 map->stripes[i].physical,
2866 &dev_extent_len);
2867 if (ret) {
2868 mutex_unlock(&fs_devices->device_list_mutex);
2869 btrfs_abort_transaction(trans, ret);
2870 goto out;
2873 if (device->bytes_used > 0) {
2874 mutex_lock(&fs_info->chunk_mutex);
2875 btrfs_device_set_bytes_used(device,
2876 device->bytes_used - dev_extent_len);
2877 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
2878 btrfs_clear_space_info_full(fs_info);
2879 mutex_unlock(&fs_info->chunk_mutex);
2882 ret = btrfs_update_device(trans, device);
2883 if (ret) {
2884 mutex_unlock(&fs_devices->device_list_mutex);
2885 btrfs_abort_transaction(trans, ret);
2886 goto out;
2889 mutex_unlock(&fs_devices->device_list_mutex);
2891 ret = btrfs_free_chunk(trans, chunk_offset);
2892 if (ret) {
2893 btrfs_abort_transaction(trans, ret);
2894 goto out;
2897 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
2899 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2900 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
2901 if (ret) {
2902 btrfs_abort_transaction(trans, ret);
2903 goto out;
2907 ret = btrfs_remove_block_group(trans, chunk_offset, em);
2908 if (ret) {
2909 btrfs_abort_transaction(trans, ret);
2910 goto out;
2913 out:
2914 /* once for us */
2915 free_extent_map(em);
2916 return ret;
2919 static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2921 struct btrfs_root *root = fs_info->chunk_root;
2922 struct btrfs_trans_handle *trans;
2923 int ret;
2926 * Prevent races with automatic removal of unused block groups.
2927 * After we relocate and before we remove the chunk with offset
2928 * chunk_offset, automatic removal of the block group can kick in,
2929 * resulting in a failure when calling btrfs_remove_chunk() below.
2931 * Make sure to acquire this mutex before doing a tree search (dev
2932 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2933 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2934 * we release the path used to search the chunk/dev tree and before
2935 * the current task acquires this mutex and calls us.
2937 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
2939 /* step one, relocate all the extents inside this chunk */
2940 btrfs_scrub_pause(fs_info);
2941 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
2942 btrfs_scrub_continue(fs_info);
2943 if (ret)
2944 return ret;
2946 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2947 chunk_offset);
2948 if (IS_ERR(trans)) {
2949 ret = PTR_ERR(trans);
2950 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2951 return ret;
2955 * step two, delete the device extents and the
2956 * chunk tree entries
2958 ret = btrfs_remove_chunk(trans, chunk_offset);
2959 btrfs_end_transaction(trans);
2960 return ret;
2963 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
2965 struct btrfs_root *chunk_root = fs_info->chunk_root;
2966 struct btrfs_path *path;
2967 struct extent_buffer *leaf;
2968 struct btrfs_chunk *chunk;
2969 struct btrfs_key key;
2970 struct btrfs_key found_key;
2971 u64 chunk_type;
2972 bool retried = false;
2973 int failed = 0;
2974 int ret;
2976 path = btrfs_alloc_path();
2977 if (!path)
2978 return -ENOMEM;
2980 again:
2981 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2982 key.offset = (u64)-1;
2983 key.type = BTRFS_CHUNK_ITEM_KEY;
2985 while (1) {
2986 mutex_lock(&fs_info->delete_unused_bgs_mutex);
2987 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2988 if (ret < 0) {
2989 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2990 goto error;
2992 BUG_ON(ret == 0); /* Corruption */
2994 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2995 key.type);
2996 if (ret)
2997 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2998 if (ret < 0)
2999 goto error;
3000 if (ret > 0)
3001 break;
3003 leaf = path->nodes[0];
3004 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3006 chunk = btrfs_item_ptr(leaf, path->slots[0],
3007 struct btrfs_chunk);
3008 chunk_type = btrfs_chunk_type(leaf, chunk);
3009 btrfs_release_path(path);
3011 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
3012 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
3013 if (ret == -ENOSPC)
3014 failed++;
3015 else
3016 BUG_ON(ret);
3018 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3020 if (found_key.offset == 0)
3021 break;
3022 key.offset = found_key.offset - 1;
3024 ret = 0;
3025 if (failed && !retried) {
3026 failed = 0;
3027 retried = true;
3028 goto again;
3029 } else if (WARN_ON(failed && retried)) {
3030 ret = -ENOSPC;
3032 error:
3033 btrfs_free_path(path);
3034 return ret;
3038 * return 1 : allocate a data chunk successfully,
3039 * return <0: errors during allocating a data chunk,
3040 * return 0 : no need to allocate a data chunk.
3042 static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3043 u64 chunk_offset)
3045 struct btrfs_block_group *cache;
3046 u64 bytes_used;
3047 u64 chunk_type;
3049 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3050 ASSERT(cache);
3051 chunk_type = cache->flags;
3052 btrfs_put_block_group(cache);
3054 if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA))
3055 return 0;
3057 spin_lock(&fs_info->data_sinfo->lock);
3058 bytes_used = fs_info->data_sinfo->bytes_used;
3059 spin_unlock(&fs_info->data_sinfo->lock);
3061 if (!bytes_used) {
3062 struct btrfs_trans_handle *trans;
3063 int ret;
3065 trans = btrfs_join_transaction(fs_info->tree_root);
3066 if (IS_ERR(trans))
3067 return PTR_ERR(trans);
3069 ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA);
3070 btrfs_end_transaction(trans);
3071 if (ret < 0)
3072 return ret;
3073 return 1;
3076 return 0;
3079 static int insert_balance_item(struct btrfs_fs_info *fs_info,
3080 struct btrfs_balance_control *bctl)
3082 struct btrfs_root *root = fs_info->tree_root;
3083 struct btrfs_trans_handle *trans;
3084 struct btrfs_balance_item *item;
3085 struct btrfs_disk_balance_args disk_bargs;
3086 struct btrfs_path *path;
3087 struct extent_buffer *leaf;
3088 struct btrfs_key key;
3089 int ret, err;
3091 path = btrfs_alloc_path();
3092 if (!path)
3093 return -ENOMEM;
3095 trans = btrfs_start_transaction(root, 0);
3096 if (IS_ERR(trans)) {
3097 btrfs_free_path(path);
3098 return PTR_ERR(trans);
3101 key.objectid = BTRFS_BALANCE_OBJECTID;
3102 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3103 key.offset = 0;
3105 ret = btrfs_insert_empty_item(trans, root, path, &key,
3106 sizeof(*item));
3107 if (ret)
3108 goto out;
3110 leaf = path->nodes[0];
3111 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3113 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
3115 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3116 btrfs_set_balance_data(leaf, item, &disk_bargs);
3117 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3118 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3119 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3120 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3122 btrfs_set_balance_flags(leaf, item, bctl->flags);
3124 btrfs_mark_buffer_dirty(leaf);
3125 out:
3126 btrfs_free_path(path);
3127 err = btrfs_commit_transaction(trans);
3128 if (err && !ret)
3129 ret = err;
3130 return ret;
3133 static int del_balance_item(struct btrfs_fs_info *fs_info)
3135 struct btrfs_root *root = fs_info->tree_root;
3136 struct btrfs_trans_handle *trans;
3137 struct btrfs_path *path;
3138 struct btrfs_key key;
3139 int ret, err;
3141 path = btrfs_alloc_path();
3142 if (!path)
3143 return -ENOMEM;
3145 trans = btrfs_start_transaction(root, 0);
3146 if (IS_ERR(trans)) {
3147 btrfs_free_path(path);
3148 return PTR_ERR(trans);
3151 key.objectid = BTRFS_BALANCE_OBJECTID;
3152 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3153 key.offset = 0;
3155 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3156 if (ret < 0)
3157 goto out;
3158 if (ret > 0) {
3159 ret = -ENOENT;
3160 goto out;
3163 ret = btrfs_del_item(trans, root, path);
3164 out:
3165 btrfs_free_path(path);
3166 err = btrfs_commit_transaction(trans);
3167 if (err && !ret)
3168 ret = err;
3169 return ret;
3173 * This is a heuristic used to reduce the number of chunks balanced on
3174 * resume after balance was interrupted.
3176 static void update_balance_args(struct btrfs_balance_control *bctl)
3179 * Turn on soft mode for chunk types that were being converted.
3181 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3182 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3183 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3184 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3185 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3186 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3189 * Turn on usage filter if is not already used. The idea is
3190 * that chunks that we have already balanced should be
3191 * reasonably full. Don't do it for chunks that are being
3192 * converted - that will keep us from relocating unconverted
3193 * (albeit full) chunks.
3195 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3196 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3197 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3198 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3199 bctl->data.usage = 90;
3201 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3202 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3203 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3204 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3205 bctl->sys.usage = 90;
3207 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3208 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3209 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3210 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3211 bctl->meta.usage = 90;
3216 * Clear the balance status in fs_info and delete the balance item from disk.
3218 static void reset_balance_state(struct btrfs_fs_info *fs_info)
3220 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3221 int ret;
3223 BUG_ON(!fs_info->balance_ctl);
3225 spin_lock(&fs_info->balance_lock);
3226 fs_info->balance_ctl = NULL;
3227 spin_unlock(&fs_info->balance_lock);
3229 kfree(bctl);
3230 ret = del_balance_item(fs_info);
3231 if (ret)
3232 btrfs_handle_fs_error(fs_info, ret, NULL);
3236 * Balance filters. Return 1 if chunk should be filtered out
3237 * (should not be balanced).
3239 static int chunk_profiles_filter(u64 chunk_type,
3240 struct btrfs_balance_args *bargs)
3242 chunk_type = chunk_to_extended(chunk_type) &
3243 BTRFS_EXTENDED_PROFILE_MASK;
3245 if (bargs->profiles & chunk_type)
3246 return 0;
3248 return 1;
3251 static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3252 struct btrfs_balance_args *bargs)
3254 struct btrfs_block_group *cache;
3255 u64 chunk_used;
3256 u64 user_thresh_min;
3257 u64 user_thresh_max;
3258 int ret = 1;
3260 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3261 chunk_used = cache->used;
3263 if (bargs->usage_min == 0)
3264 user_thresh_min = 0;
3265 else
3266 user_thresh_min = div_factor_fine(cache->length,
3267 bargs->usage_min);
3269 if (bargs->usage_max == 0)
3270 user_thresh_max = 1;
3271 else if (bargs->usage_max > 100)
3272 user_thresh_max = cache->length;
3273 else
3274 user_thresh_max = div_factor_fine(cache->length,
3275 bargs->usage_max);
3277 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3278 ret = 0;
3280 btrfs_put_block_group(cache);
3281 return ret;
3284 static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
3285 u64 chunk_offset, struct btrfs_balance_args *bargs)
3287 struct btrfs_block_group *cache;
3288 u64 chunk_used, user_thresh;
3289 int ret = 1;
3291 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3292 chunk_used = cache->used;
3294 if (bargs->usage_min == 0)
3295 user_thresh = 1;
3296 else if (bargs->usage > 100)
3297 user_thresh = cache->length;
3298 else
3299 user_thresh = div_factor_fine(cache->length, bargs->usage);
3301 if (chunk_used < user_thresh)
3302 ret = 0;
3304 btrfs_put_block_group(cache);
3305 return ret;
3308 static int chunk_devid_filter(struct extent_buffer *leaf,
3309 struct btrfs_chunk *chunk,
3310 struct btrfs_balance_args *bargs)
3312 struct btrfs_stripe *stripe;
3313 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3314 int i;
3316 for (i = 0; i < num_stripes; i++) {
3317 stripe = btrfs_stripe_nr(chunk, i);
3318 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3319 return 0;
3322 return 1;
3325 static u64 calc_data_stripes(u64 type, int num_stripes)
3327 const int index = btrfs_bg_flags_to_raid_index(type);
3328 const int ncopies = btrfs_raid_array[index].ncopies;
3329 const int nparity = btrfs_raid_array[index].nparity;
3331 if (nparity)
3332 return num_stripes - nparity;
3333 else
3334 return num_stripes / ncopies;
3337 /* [pstart, pend) */
3338 static int chunk_drange_filter(struct extent_buffer *leaf,
3339 struct btrfs_chunk *chunk,
3340 struct btrfs_balance_args *bargs)
3342 struct btrfs_stripe *stripe;
3343 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3344 u64 stripe_offset;
3345 u64 stripe_length;
3346 u64 type;
3347 int factor;
3348 int i;
3350 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3351 return 0;
3353 type = btrfs_chunk_type(leaf, chunk);
3354 factor = calc_data_stripes(type, num_stripes);
3356 for (i = 0; i < num_stripes; i++) {
3357 stripe = btrfs_stripe_nr(chunk, i);
3358 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3359 continue;
3361 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3362 stripe_length = btrfs_chunk_length(leaf, chunk);
3363 stripe_length = div_u64(stripe_length, factor);
3365 if (stripe_offset < bargs->pend &&
3366 stripe_offset + stripe_length > bargs->pstart)
3367 return 0;
3370 return 1;
3373 /* [vstart, vend) */
3374 static int chunk_vrange_filter(struct extent_buffer *leaf,
3375 struct btrfs_chunk *chunk,
3376 u64 chunk_offset,
3377 struct btrfs_balance_args *bargs)
3379 if (chunk_offset < bargs->vend &&
3380 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3381 /* at least part of the chunk is inside this vrange */
3382 return 0;
3384 return 1;
3387 static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3388 struct btrfs_chunk *chunk,
3389 struct btrfs_balance_args *bargs)
3391 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3393 if (bargs->stripes_min <= num_stripes
3394 && num_stripes <= bargs->stripes_max)
3395 return 0;
3397 return 1;
3400 static int chunk_soft_convert_filter(u64 chunk_type,
3401 struct btrfs_balance_args *bargs)
3403 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3404 return 0;
3406 chunk_type = chunk_to_extended(chunk_type) &
3407 BTRFS_EXTENDED_PROFILE_MASK;
3409 if (bargs->target == chunk_type)
3410 return 1;
3412 return 0;
3415 static int should_balance_chunk(struct extent_buffer *leaf,
3416 struct btrfs_chunk *chunk, u64 chunk_offset)
3418 struct btrfs_fs_info *fs_info = leaf->fs_info;
3419 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3420 struct btrfs_balance_args *bargs = NULL;
3421 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3423 /* type filter */
3424 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3425 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3426 return 0;
3429 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3430 bargs = &bctl->data;
3431 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3432 bargs = &bctl->sys;
3433 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3434 bargs = &bctl->meta;
3436 /* profiles filter */
3437 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3438 chunk_profiles_filter(chunk_type, bargs)) {
3439 return 0;
3442 /* usage filter */
3443 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3444 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
3445 return 0;
3446 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3447 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
3448 return 0;
3451 /* devid filter */
3452 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3453 chunk_devid_filter(leaf, chunk, bargs)) {
3454 return 0;
3457 /* drange filter, makes sense only with devid filter */
3458 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3459 chunk_drange_filter(leaf, chunk, bargs)) {
3460 return 0;
3463 /* vrange filter */
3464 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3465 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3466 return 0;
3469 /* stripes filter */
3470 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3471 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3472 return 0;
3475 /* soft profile changing mode */
3476 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3477 chunk_soft_convert_filter(chunk_type, bargs)) {
3478 return 0;
3482 * limited by count, must be the last filter
3484 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3485 if (bargs->limit == 0)
3486 return 0;
3487 else
3488 bargs->limit--;
3489 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3491 * Same logic as the 'limit' filter; the minimum cannot be
3492 * determined here because we do not have the global information
3493 * about the count of all chunks that satisfy the filters.
3495 if (bargs->limit_max == 0)
3496 return 0;
3497 else
3498 bargs->limit_max--;
3501 return 1;
3504 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
3506 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3507 struct btrfs_root *chunk_root = fs_info->chunk_root;
3508 u64 chunk_type;
3509 struct btrfs_chunk *chunk;
3510 struct btrfs_path *path = NULL;
3511 struct btrfs_key key;
3512 struct btrfs_key found_key;
3513 struct extent_buffer *leaf;
3514 int slot;
3515 int ret;
3516 int enospc_errors = 0;
3517 bool counting = true;
3518 /* The single value limit and min/max limits use the same bytes in the */
3519 u64 limit_data = bctl->data.limit;
3520 u64 limit_meta = bctl->meta.limit;
3521 u64 limit_sys = bctl->sys.limit;
3522 u32 count_data = 0;
3523 u32 count_meta = 0;
3524 u32 count_sys = 0;
3525 int chunk_reserved = 0;
3527 path = btrfs_alloc_path();
3528 if (!path) {
3529 ret = -ENOMEM;
3530 goto error;
3533 /* zero out stat counters */
3534 spin_lock(&fs_info->balance_lock);
3535 memset(&bctl->stat, 0, sizeof(bctl->stat));
3536 spin_unlock(&fs_info->balance_lock);
3537 again:
3538 if (!counting) {
3540 * The single value limit and min/max limits use the same bytes
3541 * in the
3543 bctl->data.limit = limit_data;
3544 bctl->meta.limit = limit_meta;
3545 bctl->sys.limit = limit_sys;
3547 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3548 key.offset = (u64)-1;
3549 key.type = BTRFS_CHUNK_ITEM_KEY;
3551 while (1) {
3552 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
3553 atomic_read(&fs_info->balance_cancel_req)) {
3554 ret = -ECANCELED;
3555 goto error;
3558 mutex_lock(&fs_info->delete_unused_bgs_mutex);
3559 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3560 if (ret < 0) {
3561 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3562 goto error;
3566 * this shouldn't happen, it means the last relocate
3567 * failed
3569 if (ret == 0)
3570 BUG(); /* FIXME break ? */
3572 ret = btrfs_previous_item(chunk_root, path, 0,
3573 BTRFS_CHUNK_ITEM_KEY);
3574 if (ret) {
3575 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3576 ret = 0;
3577 break;
3580 leaf = path->nodes[0];
3581 slot = path->slots[0];
3582 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3584 if (found_key.objectid != key.objectid) {
3585 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3586 break;
3589 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3590 chunk_type = btrfs_chunk_type(leaf, chunk);
3592 if (!counting) {
3593 spin_lock(&fs_info->balance_lock);
3594 bctl->stat.considered++;
3595 spin_unlock(&fs_info->balance_lock);
3598 ret = should_balance_chunk(leaf, chunk, found_key.offset);
3600 btrfs_release_path(path);
3601 if (!ret) {
3602 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3603 goto loop;
3606 if (counting) {
3607 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3608 spin_lock(&fs_info->balance_lock);
3609 bctl->stat.expected++;
3610 spin_unlock(&fs_info->balance_lock);
3612 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3613 count_data++;
3614 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3615 count_sys++;
3616 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3617 count_meta++;
3619 goto loop;
3623 * Apply limit_min filter, no need to check if the LIMITS
3624 * filter is used, limit_min is 0 by default
3626 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3627 count_data < bctl->data.limit_min)
3628 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3629 count_meta < bctl->meta.limit_min)
3630 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3631 count_sys < bctl->sys.limit_min)) {
3632 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3633 goto loop;
3636 if (!chunk_reserved) {
3638 * We may be relocating the only data chunk we have,
3639 * which could potentially end up with losing data's
3640 * raid profile, so lets allocate an empty one in
3641 * advance.
3643 ret = btrfs_may_alloc_data_chunk(fs_info,
3644 found_key.offset);
3645 if (ret < 0) {
3646 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3647 goto error;
3648 } else if (ret == 1) {
3649 chunk_reserved = 1;
3653 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
3654 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3655 if (ret == -ENOSPC) {
3656 enospc_errors++;
3657 } else if (ret == -ETXTBSY) {
3658 btrfs_info(fs_info,
3659 "skipping relocation of block group %llu due to active swapfile",
3660 found_key.offset);
3661 ret = 0;
3662 } else if (ret) {
3663 goto error;
3664 } else {
3665 spin_lock(&fs_info->balance_lock);
3666 bctl->stat.completed++;
3667 spin_unlock(&fs_info->balance_lock);
3669 loop:
3670 if (found_key.offset == 0)
3671 break;
3672 key.offset = found_key.offset - 1;
3675 if (counting) {
3676 btrfs_release_path(path);
3677 counting = false;
3678 goto again;
3680 error:
3681 btrfs_free_path(path);
3682 if (enospc_errors) {
3683 btrfs_info(fs_info, "%d enospc errors during balance",
3684 enospc_errors);
3685 if (!ret)
3686 ret = -ENOSPC;
3689 return ret;
3693 * alloc_profile_is_valid - see if a given profile is valid and reduced
3694 * @flags: profile to validate
3695 * @extended: if true @flags is treated as an extended profile
3697 static int alloc_profile_is_valid(u64 flags, int extended)
3699 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3700 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3702 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3704 /* 1) check that all other bits are zeroed */
3705 if (flags & ~mask)
3706 return 0;
3708 /* 2) see if profile is reduced */
3709 if (flags == 0)
3710 return !extended; /* "0" is valid for usual profiles */
3712 return has_single_bit_set(flags);
3715 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3717 /* cancel requested || normal exit path */
3718 return atomic_read(&fs_info->balance_cancel_req) ||
3719 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3720 atomic_read(&fs_info->balance_cancel_req) == 0);
3723 /* Non-zero return value signifies invalidity */
3724 static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3725 u64 allowed)
3727 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3728 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3729 (bctl_arg->target & ~allowed)));
3733 * Fill @buf with textual description of balance filter flags @bargs, up to
3734 * @size_buf including the terminating null. The output may be trimmed if it
3735 * does not fit into the provided buffer.
3737 static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3738 u32 size_buf)
3740 int ret;
3741 u32 size_bp = size_buf;
3742 char *bp = buf;
3743 u64 flags = bargs->flags;
3744 char tmp_buf[128] = {'\0'};
3746 if (!flags)
3747 return;
3749 #define CHECK_APPEND_NOARG(a) \
3750 do { \
3751 ret = snprintf(bp, size_bp, (a)); \
3752 if (ret < 0 || ret >= size_bp) \
3753 goto out_overflow; \
3754 size_bp -= ret; \
3755 bp += ret; \
3756 } while (0)
3758 #define CHECK_APPEND_1ARG(a, v1) \
3759 do { \
3760 ret = snprintf(bp, size_bp, (a), (v1)); \
3761 if (ret < 0 || ret >= size_bp) \
3762 goto out_overflow; \
3763 size_bp -= ret; \
3764 bp += ret; \
3765 } while (0)
3767 #define CHECK_APPEND_2ARG(a, v1, v2) \
3768 do { \
3769 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3770 if (ret < 0 || ret >= size_bp) \
3771 goto out_overflow; \
3772 size_bp -= ret; \
3773 bp += ret; \
3774 } while (0)
3776 if (flags & BTRFS_BALANCE_ARGS_CONVERT)
3777 CHECK_APPEND_1ARG("convert=%s,",
3778 btrfs_bg_type_to_raid_name(bargs->target));
3780 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3781 CHECK_APPEND_NOARG("soft,");
3783 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3784 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3785 sizeof(tmp_buf));
3786 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3789 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3790 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3792 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3793 CHECK_APPEND_2ARG("usage=%u..%u,",
3794 bargs->usage_min, bargs->usage_max);
3796 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3797 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3799 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3800 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3801 bargs->pstart, bargs->pend);
3803 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3804 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3805 bargs->vstart, bargs->vend);
3807 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
3808 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
3810 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
3811 CHECK_APPEND_2ARG("limit=%u..%u,",
3812 bargs->limit_min, bargs->limit_max);
3814 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
3815 CHECK_APPEND_2ARG("stripes=%u..%u,",
3816 bargs->stripes_min, bargs->stripes_max);
3818 #undef CHECK_APPEND_2ARG
3819 #undef CHECK_APPEND_1ARG
3820 #undef CHECK_APPEND_NOARG
3822 out_overflow:
3824 if (size_bp < size_buf)
3825 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
3826 else
3827 buf[0] = '\0';
3830 static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
3832 u32 size_buf = 1024;
3833 char tmp_buf[192] = {'\0'};
3834 char *buf;
3835 char *bp;
3836 u32 size_bp = size_buf;
3837 int ret;
3838 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3840 buf = kzalloc(size_buf, GFP_KERNEL);
3841 if (!buf)
3842 return;
3844 bp = buf;
3846 #define CHECK_APPEND_1ARG(a, v1) \
3847 do { \
3848 ret = snprintf(bp, size_bp, (a), (v1)); \
3849 if (ret < 0 || ret >= size_bp) \
3850 goto out_overflow; \
3851 size_bp -= ret; \
3852 bp += ret; \
3853 } while (0)
3855 if (bctl->flags & BTRFS_BALANCE_FORCE)
3856 CHECK_APPEND_1ARG("%s", "-f ");
3858 if (bctl->flags & BTRFS_BALANCE_DATA) {
3859 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
3860 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
3863 if (bctl->flags & BTRFS_BALANCE_METADATA) {
3864 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
3865 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
3868 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
3869 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
3870 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
3873 #undef CHECK_APPEND_1ARG
3875 out_overflow:
3877 if (size_bp < size_buf)
3878 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
3879 btrfs_info(fs_info, "balance: %s %s",
3880 (bctl->flags & BTRFS_BALANCE_RESUME) ?
3881 "resume" : "start", buf);
3883 kfree(buf);
3887 * Should be called with balance mutexe held
3889 int btrfs_balance(struct btrfs_fs_info *fs_info,
3890 struct btrfs_balance_control *bctl,
3891 struct btrfs_ioctl_balance_args *bargs)
3893 u64 meta_target, data_target;
3894 u64 allowed;
3895 int mixed = 0;
3896 int ret;
3897 u64 num_devices;
3898 unsigned seq;
3899 bool reducing_redundancy;
3900 int i;
3902 if (btrfs_fs_closing(fs_info) ||
3903 atomic_read(&fs_info->balance_pause_req) ||
3904 atomic_read(&fs_info->balance_cancel_req)) {
3905 ret = -EINVAL;
3906 goto out;
3909 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3910 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3911 mixed = 1;
3914 * In case of mixed groups both data and meta should be picked,
3915 * and identical options should be given for both of them.
3917 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3918 if (mixed && (bctl->flags & allowed)) {
3919 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3920 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3921 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3922 btrfs_err(fs_info,
3923 "balance: mixed groups data and metadata options must be the same");
3924 ret = -EINVAL;
3925 goto out;
3930 * rw_devices will not change at the moment, device add/delete/replace
3931 * are excluded by EXCL_OP
3933 num_devices = fs_info->fs_devices->rw_devices;
3936 * SINGLE profile on-disk has no profile bit, but in-memory we have a
3937 * special bit for it, to make it easier to distinguish. Thus we need
3938 * to set it manually, or balance would refuse the profile.
3940 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
3941 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++)
3942 if (num_devices >= btrfs_raid_array[i].devs_min)
3943 allowed |= btrfs_raid_array[i].bg_flag;
3945 if (validate_convert_profile(&bctl->data, allowed)) {
3946 btrfs_err(fs_info,
3947 "balance: invalid convert data profile %s",
3948 btrfs_bg_type_to_raid_name(bctl->data.target));
3949 ret = -EINVAL;
3950 goto out;
3952 if (validate_convert_profile(&bctl->meta, allowed)) {
3953 btrfs_err(fs_info,
3954 "balance: invalid convert metadata profile %s",
3955 btrfs_bg_type_to_raid_name(bctl->meta.target));
3956 ret = -EINVAL;
3957 goto out;
3959 if (validate_convert_profile(&bctl->sys, allowed)) {
3960 btrfs_err(fs_info,
3961 "balance: invalid convert system profile %s",
3962 btrfs_bg_type_to_raid_name(bctl->sys.target));
3963 ret = -EINVAL;
3964 goto out;
3968 * Allow to reduce metadata or system integrity only if force set for
3969 * profiles with redundancy (copies, parity)
3971 allowed = 0;
3972 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) {
3973 if (btrfs_raid_array[i].ncopies >= 2 ||
3974 btrfs_raid_array[i].tolerated_failures >= 1)
3975 allowed |= btrfs_raid_array[i].bg_flag;
3977 do {
3978 seq = read_seqbegin(&fs_info->profiles_lock);
3980 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3981 (fs_info->avail_system_alloc_bits & allowed) &&
3982 !(bctl->sys.target & allowed)) ||
3983 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3984 (fs_info->avail_metadata_alloc_bits & allowed) &&
3985 !(bctl->meta.target & allowed)))
3986 reducing_redundancy = true;
3987 else
3988 reducing_redundancy = false;
3990 /* if we're not converting, the target field is uninitialized */
3991 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3992 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3993 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3994 bctl->data.target : fs_info->avail_data_alloc_bits;
3995 } while (read_seqretry(&fs_info->profiles_lock, seq));
3997 if (reducing_redundancy) {
3998 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3999 btrfs_info(fs_info,
4000 "balance: force reducing metadata redundancy");
4001 } else {
4002 btrfs_err(fs_info,
4003 "balance: reduces metadata redundancy, use --force if you want this");
4004 ret = -EINVAL;
4005 goto out;
4009 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4010 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
4011 btrfs_warn(fs_info,
4012 "balance: metadata profile %s has lower redundancy than data profile %s",
4013 btrfs_bg_type_to_raid_name(meta_target),
4014 btrfs_bg_type_to_raid_name(data_target));
4017 if (fs_info->send_in_progress) {
4018 btrfs_warn_rl(fs_info,
4019 "cannot run balance while send operations are in progress (%d in progress)",
4020 fs_info->send_in_progress);
4021 ret = -EAGAIN;
4022 goto out;
4025 ret = insert_balance_item(fs_info, bctl);
4026 if (ret && ret != -EEXIST)
4027 goto out;
4029 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4030 BUG_ON(ret == -EEXIST);
4031 BUG_ON(fs_info->balance_ctl);
4032 spin_lock(&fs_info->balance_lock);
4033 fs_info->balance_ctl = bctl;
4034 spin_unlock(&fs_info->balance_lock);
4035 } else {
4036 BUG_ON(ret != -EEXIST);
4037 spin_lock(&fs_info->balance_lock);
4038 update_balance_args(bctl);
4039 spin_unlock(&fs_info->balance_lock);
4042 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4043 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
4044 describe_balance_start_or_resume(fs_info);
4045 mutex_unlock(&fs_info->balance_mutex);
4047 ret = __btrfs_balance(fs_info);
4049 mutex_lock(&fs_info->balance_mutex);
4050 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4051 btrfs_info(fs_info, "balance: paused");
4052 else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req))
4053 btrfs_info(fs_info, "balance: canceled");
4054 else
4055 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4057 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
4059 if (bargs) {
4060 memset(bargs, 0, sizeof(*bargs));
4061 btrfs_update_ioctl_balance_args(fs_info, bargs);
4064 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4065 balance_need_close(fs_info)) {
4066 reset_balance_state(fs_info);
4067 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4070 wake_up(&fs_info->balance_wait_q);
4072 return ret;
4073 out:
4074 if (bctl->flags & BTRFS_BALANCE_RESUME)
4075 reset_balance_state(fs_info);
4076 else
4077 kfree(bctl);
4078 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4080 return ret;
4083 static int balance_kthread(void *data)
4085 struct btrfs_fs_info *fs_info = data;
4086 int ret = 0;
4088 mutex_lock(&fs_info->balance_mutex);
4089 if (fs_info->balance_ctl)
4090 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
4091 mutex_unlock(&fs_info->balance_mutex);
4093 return ret;
4096 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4098 struct task_struct *tsk;
4100 mutex_lock(&fs_info->balance_mutex);
4101 if (!fs_info->balance_ctl) {
4102 mutex_unlock(&fs_info->balance_mutex);
4103 return 0;
4105 mutex_unlock(&fs_info->balance_mutex);
4107 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
4108 btrfs_info(fs_info, "balance: resume skipped");
4109 return 0;
4113 * A ro->rw remount sequence should continue with the paused balance
4114 * regardless of who pauses it, system or the user as of now, so set
4115 * the resume flag.
4117 spin_lock(&fs_info->balance_lock);
4118 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4119 spin_unlock(&fs_info->balance_lock);
4121 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
4122 return PTR_ERR_OR_ZERO(tsk);
4125 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
4127 struct btrfs_balance_control *bctl;
4128 struct btrfs_balance_item *item;
4129 struct btrfs_disk_balance_args disk_bargs;
4130 struct btrfs_path *path;
4131 struct extent_buffer *leaf;
4132 struct btrfs_key key;
4133 int ret;
4135 path = btrfs_alloc_path();
4136 if (!path)
4137 return -ENOMEM;
4139 key.objectid = BTRFS_BALANCE_OBJECTID;
4140 key.type = BTRFS_TEMPORARY_ITEM_KEY;
4141 key.offset = 0;
4143 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4144 if (ret < 0)
4145 goto out;
4146 if (ret > 0) { /* ret = -ENOENT; */
4147 ret = 0;
4148 goto out;
4151 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4152 if (!bctl) {
4153 ret = -ENOMEM;
4154 goto out;
4157 leaf = path->nodes[0];
4158 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4160 bctl->flags = btrfs_balance_flags(leaf, item);
4161 bctl->flags |= BTRFS_BALANCE_RESUME;
4163 btrfs_balance_data(leaf, item, &disk_bargs);
4164 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4165 btrfs_balance_meta(leaf, item, &disk_bargs);
4166 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4167 btrfs_balance_sys(leaf, item, &disk_bargs);
4168 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4171 * This should never happen, as the paused balance state is recovered
4172 * during mount without any chance of other exclusive ops to collide.
4174 * This gives the exclusive op status to balance and keeps in paused
4175 * state until user intervention (cancel or umount). If the ownership
4176 * cannot be assigned, show a message but do not fail. The balance
4177 * is in a paused state and must have fs_info::balance_ctl properly
4178 * set up.
4180 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4181 btrfs_warn(fs_info,
4182 "balance: cannot set exclusive op status, resume manually");
4184 mutex_lock(&fs_info->balance_mutex);
4185 BUG_ON(fs_info->balance_ctl);
4186 spin_lock(&fs_info->balance_lock);
4187 fs_info->balance_ctl = bctl;
4188 spin_unlock(&fs_info->balance_lock);
4189 mutex_unlock(&fs_info->balance_mutex);
4190 out:
4191 btrfs_free_path(path);
4192 return ret;
4195 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4197 int ret = 0;
4199 mutex_lock(&fs_info->balance_mutex);
4200 if (!fs_info->balance_ctl) {
4201 mutex_unlock(&fs_info->balance_mutex);
4202 return -ENOTCONN;
4205 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4206 atomic_inc(&fs_info->balance_pause_req);
4207 mutex_unlock(&fs_info->balance_mutex);
4209 wait_event(fs_info->balance_wait_q,
4210 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4212 mutex_lock(&fs_info->balance_mutex);
4213 /* we are good with balance_ctl ripped off from under us */
4214 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4215 atomic_dec(&fs_info->balance_pause_req);
4216 } else {
4217 ret = -ENOTCONN;
4220 mutex_unlock(&fs_info->balance_mutex);
4221 return ret;
4224 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4226 mutex_lock(&fs_info->balance_mutex);
4227 if (!fs_info->balance_ctl) {
4228 mutex_unlock(&fs_info->balance_mutex);
4229 return -ENOTCONN;
4233 * A paused balance with the item stored on disk can be resumed at
4234 * mount time if the mount is read-write. Otherwise it's still paused
4235 * and we must not allow cancelling as it deletes the item.
4237 if (sb_rdonly(fs_info->sb)) {
4238 mutex_unlock(&fs_info->balance_mutex);
4239 return -EROFS;
4242 atomic_inc(&fs_info->balance_cancel_req);
4244 * if we are running just wait and return, balance item is
4245 * deleted in btrfs_balance in this case
4247 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4248 mutex_unlock(&fs_info->balance_mutex);
4249 wait_event(fs_info->balance_wait_q,
4250 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4251 mutex_lock(&fs_info->balance_mutex);
4252 } else {
4253 mutex_unlock(&fs_info->balance_mutex);
4255 * Lock released to allow other waiters to continue, we'll
4256 * reexamine the status again.
4258 mutex_lock(&fs_info->balance_mutex);
4260 if (fs_info->balance_ctl) {
4261 reset_balance_state(fs_info);
4262 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4263 btrfs_info(fs_info, "balance: canceled");
4267 BUG_ON(fs_info->balance_ctl ||
4268 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4269 atomic_dec(&fs_info->balance_cancel_req);
4270 mutex_unlock(&fs_info->balance_mutex);
4271 return 0;
4274 static int btrfs_uuid_scan_kthread(void *data)
4276 struct btrfs_fs_info *fs_info = data;
4277 struct btrfs_root *root = fs_info->tree_root;
4278 struct btrfs_key key;
4279 struct btrfs_path *path = NULL;
4280 int ret = 0;
4281 struct extent_buffer *eb;
4282 int slot;
4283 struct btrfs_root_item root_item;
4284 u32 item_size;
4285 struct btrfs_trans_handle *trans = NULL;
4287 path = btrfs_alloc_path();
4288 if (!path) {
4289 ret = -ENOMEM;
4290 goto out;
4293 key.objectid = 0;
4294 key.type = BTRFS_ROOT_ITEM_KEY;
4295 key.offset = 0;
4297 while (1) {
4298 ret = btrfs_search_forward(root, &key, path,
4299 BTRFS_OLDEST_GENERATION);
4300 if (ret) {
4301 if (ret > 0)
4302 ret = 0;
4303 break;
4306 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4307 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4308 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4309 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4310 goto skip;
4312 eb = path->nodes[0];
4313 slot = path->slots[0];
4314 item_size = btrfs_item_size_nr(eb, slot);
4315 if (item_size < sizeof(root_item))
4316 goto skip;
4318 read_extent_buffer(eb, &root_item,
4319 btrfs_item_ptr_offset(eb, slot),
4320 (int)sizeof(root_item));
4321 if (btrfs_root_refs(&root_item) == 0)
4322 goto skip;
4324 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4325 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4326 if (trans)
4327 goto update_tree;
4329 btrfs_release_path(path);
4331 * 1 - subvol uuid item
4332 * 1 - received_subvol uuid item
4334 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4335 if (IS_ERR(trans)) {
4336 ret = PTR_ERR(trans);
4337 break;
4339 continue;
4340 } else {
4341 goto skip;
4343 update_tree:
4344 if (!btrfs_is_empty_uuid(root_item.uuid)) {
4345 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
4346 BTRFS_UUID_KEY_SUBVOL,
4347 key.objectid);
4348 if (ret < 0) {
4349 btrfs_warn(fs_info, "uuid_tree_add failed %d",
4350 ret);
4351 break;
4355 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
4356 ret = btrfs_uuid_tree_add(trans,
4357 root_item.received_uuid,
4358 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4359 key.objectid);
4360 if (ret < 0) {
4361 btrfs_warn(fs_info, "uuid_tree_add failed %d",
4362 ret);
4363 break;
4367 skip:
4368 if (trans) {
4369 ret = btrfs_end_transaction(trans);
4370 trans = NULL;
4371 if (ret)
4372 break;
4375 btrfs_release_path(path);
4376 if (key.offset < (u64)-1) {
4377 key.offset++;
4378 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4379 key.offset = 0;
4380 key.type = BTRFS_ROOT_ITEM_KEY;
4381 } else if (key.objectid < (u64)-1) {
4382 key.offset = 0;
4383 key.type = BTRFS_ROOT_ITEM_KEY;
4384 key.objectid++;
4385 } else {
4386 break;
4388 cond_resched();
4391 out:
4392 btrfs_free_path(path);
4393 if (trans && !IS_ERR(trans))
4394 btrfs_end_transaction(trans);
4395 if (ret)
4396 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
4397 else
4398 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
4399 up(&fs_info->uuid_tree_rescan_sem);
4400 return 0;
4404 * Callback for btrfs_uuid_tree_iterate().
4405 * returns:
4406 * 0 check succeeded, the entry is not outdated.
4407 * < 0 if an error occurred.
4408 * > 0 if the check failed, which means the caller shall remove the entry.
4410 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4411 u8 *uuid, u8 type, u64 subid)
4413 struct btrfs_key key;
4414 int ret = 0;
4415 struct btrfs_root *subvol_root;
4417 if (type != BTRFS_UUID_KEY_SUBVOL &&
4418 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4419 goto out;
4421 key.objectid = subid;
4422 key.type = BTRFS_ROOT_ITEM_KEY;
4423 key.offset = (u64)-1;
4424 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4425 if (IS_ERR(subvol_root)) {
4426 ret = PTR_ERR(subvol_root);
4427 if (ret == -ENOENT)
4428 ret = 1;
4429 goto out;
4432 switch (type) {
4433 case BTRFS_UUID_KEY_SUBVOL:
4434 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4435 ret = 1;
4436 break;
4437 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4438 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4439 BTRFS_UUID_SIZE))
4440 ret = 1;
4441 break;
4444 out:
4445 return ret;
4448 static int btrfs_uuid_rescan_kthread(void *data)
4450 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4451 int ret;
4454 * 1st step is to iterate through the existing UUID tree and
4455 * to delete all entries that contain outdated data.
4456 * 2nd step is to add all missing entries to the UUID tree.
4458 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4459 if (ret < 0) {
4460 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
4461 up(&fs_info->uuid_tree_rescan_sem);
4462 return ret;
4464 return btrfs_uuid_scan_kthread(data);
4467 int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4469 struct btrfs_trans_handle *trans;
4470 struct btrfs_root *tree_root = fs_info->tree_root;
4471 struct btrfs_root *uuid_root;
4472 struct task_struct *task;
4473 int ret;
4476 * 1 - root node
4477 * 1 - root item
4479 trans = btrfs_start_transaction(tree_root, 2);
4480 if (IS_ERR(trans))
4481 return PTR_ERR(trans);
4483 uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID);
4484 if (IS_ERR(uuid_root)) {
4485 ret = PTR_ERR(uuid_root);
4486 btrfs_abort_transaction(trans, ret);
4487 btrfs_end_transaction(trans);
4488 return ret;
4491 fs_info->uuid_root = uuid_root;
4493 ret = btrfs_commit_transaction(trans);
4494 if (ret)
4495 return ret;
4497 down(&fs_info->uuid_tree_rescan_sem);
4498 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4499 if (IS_ERR(task)) {
4500 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4501 btrfs_warn(fs_info, "failed to start uuid_scan task");
4502 up(&fs_info->uuid_tree_rescan_sem);
4503 return PTR_ERR(task);
4506 return 0;
4509 int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4511 struct task_struct *task;
4513 down(&fs_info->uuid_tree_rescan_sem);
4514 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4515 if (IS_ERR(task)) {
4516 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4517 btrfs_warn(fs_info, "failed to start uuid_rescan task");
4518 up(&fs_info->uuid_tree_rescan_sem);
4519 return PTR_ERR(task);
4522 return 0;
4526 * shrinking a device means finding all of the device extents past
4527 * the new size, and then following the back refs to the chunks.
4528 * The chunk relocation code actually frees the device extent
4530 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4532 struct btrfs_fs_info *fs_info = device->fs_info;
4533 struct btrfs_root *root = fs_info->dev_root;
4534 struct btrfs_trans_handle *trans;
4535 struct btrfs_dev_extent *dev_extent = NULL;
4536 struct btrfs_path *path;
4537 u64 length;
4538 u64 chunk_offset;
4539 int ret;
4540 int slot;
4541 int failed = 0;
4542 bool retried = false;
4543 struct extent_buffer *l;
4544 struct btrfs_key key;
4545 struct btrfs_super_block *super_copy = fs_info->super_copy;
4546 u64 old_total = btrfs_super_total_bytes(super_copy);
4547 u64 old_size = btrfs_device_get_total_bytes(device);
4548 u64 diff;
4549 u64 start;
4551 new_size = round_down(new_size, fs_info->sectorsize);
4552 start = new_size;
4553 diff = round_down(old_size - new_size, fs_info->sectorsize);
4555 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
4556 return -EINVAL;
4558 path = btrfs_alloc_path();
4559 if (!path)
4560 return -ENOMEM;
4562 path->reada = READA_BACK;
4564 trans = btrfs_start_transaction(root, 0);
4565 if (IS_ERR(trans)) {
4566 btrfs_free_path(path);
4567 return PTR_ERR(trans);
4570 mutex_lock(&fs_info->chunk_mutex);
4572 btrfs_device_set_total_bytes(device, new_size);
4573 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
4574 device->fs_devices->total_rw_bytes -= diff;
4575 atomic64_sub(diff, &fs_info->free_chunk_space);
4579 * Once the device's size has been set to the new size, ensure all
4580 * in-memory chunks are synced to disk so that the loop below sees them
4581 * and relocates them accordingly.
4583 if (contains_pending_extent(device, &start, diff)) {
4584 mutex_unlock(&fs_info->chunk_mutex);
4585 ret = btrfs_commit_transaction(trans);
4586 if (ret)
4587 goto done;
4588 } else {
4589 mutex_unlock(&fs_info->chunk_mutex);
4590 btrfs_end_transaction(trans);
4593 again:
4594 key.objectid = device->devid;
4595 key.offset = (u64)-1;
4596 key.type = BTRFS_DEV_EXTENT_KEY;
4598 do {
4599 mutex_lock(&fs_info->delete_unused_bgs_mutex);
4600 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4601 if (ret < 0) {
4602 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4603 goto done;
4606 ret = btrfs_previous_item(root, path, 0, key.type);
4607 if (ret)
4608 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4609 if (ret < 0)
4610 goto done;
4611 if (ret) {
4612 ret = 0;
4613 btrfs_release_path(path);
4614 break;
4617 l = path->nodes[0];
4618 slot = path->slots[0];
4619 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4621 if (key.objectid != device->devid) {
4622 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4623 btrfs_release_path(path);
4624 break;
4627 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4628 length = btrfs_dev_extent_length(l, dev_extent);
4630 if (key.offset + length <= new_size) {
4631 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4632 btrfs_release_path(path);
4633 break;
4636 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
4637 btrfs_release_path(path);
4640 * We may be relocating the only data chunk we have,
4641 * which could potentially end up with losing data's
4642 * raid profile, so lets allocate an empty one in
4643 * advance.
4645 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4646 if (ret < 0) {
4647 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4648 goto done;
4651 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4652 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4653 if (ret == -ENOSPC) {
4654 failed++;
4655 } else if (ret) {
4656 if (ret == -ETXTBSY) {
4657 btrfs_warn(fs_info,
4658 "could not shrink block group %llu due to active swapfile",
4659 chunk_offset);
4661 goto done;
4663 } while (key.offset-- > 0);
4665 if (failed && !retried) {
4666 failed = 0;
4667 retried = true;
4668 goto again;
4669 } else if (failed && retried) {
4670 ret = -ENOSPC;
4671 goto done;
4674 /* Shrinking succeeded, else we would be at "done". */
4675 trans = btrfs_start_transaction(root, 0);
4676 if (IS_ERR(trans)) {
4677 ret = PTR_ERR(trans);
4678 goto done;
4681 mutex_lock(&fs_info->chunk_mutex);
4682 btrfs_device_set_disk_total_bytes(device, new_size);
4683 if (list_empty(&device->post_commit_list))
4684 list_add_tail(&device->post_commit_list,
4685 &trans->transaction->dev_update_list);
4687 WARN_ON(diff > old_total);
4688 btrfs_set_super_total_bytes(super_copy,
4689 round_down(old_total - diff, fs_info->sectorsize));
4690 mutex_unlock(&fs_info->chunk_mutex);
4692 /* Now btrfs_update_device() will change the on-disk size. */
4693 ret = btrfs_update_device(trans, device);
4694 if (ret < 0) {
4695 btrfs_abort_transaction(trans, ret);
4696 btrfs_end_transaction(trans);
4697 } else {
4698 ret = btrfs_commit_transaction(trans);
4700 done:
4701 btrfs_free_path(path);
4702 if (ret) {
4703 mutex_lock(&fs_info->chunk_mutex);
4704 btrfs_device_set_total_bytes(device, old_size);
4705 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
4706 device->fs_devices->total_rw_bytes += diff;
4707 atomic64_add(diff, &fs_info->free_chunk_space);
4708 mutex_unlock(&fs_info->chunk_mutex);
4710 return ret;
4713 static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
4714 struct btrfs_key *key,
4715 struct btrfs_chunk *chunk, int item_size)
4717 struct btrfs_super_block *super_copy = fs_info->super_copy;
4718 struct btrfs_disk_key disk_key;
4719 u32 array_size;
4720 u8 *ptr;
4722 mutex_lock(&fs_info->chunk_mutex);
4723 array_size = btrfs_super_sys_array_size(super_copy);
4724 if (array_size + item_size + sizeof(disk_key)
4725 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4726 mutex_unlock(&fs_info->chunk_mutex);
4727 return -EFBIG;
4730 ptr = super_copy->sys_chunk_array + array_size;
4731 btrfs_cpu_key_to_disk(&disk_key, key);
4732 memcpy(ptr, &disk_key, sizeof(disk_key));
4733 ptr += sizeof(disk_key);
4734 memcpy(ptr, chunk, item_size);
4735 item_size += sizeof(disk_key);
4736 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4737 mutex_unlock(&fs_info->chunk_mutex);
4739 return 0;
4743 * sort the devices in descending order by max_avail, total_avail
4745 static int btrfs_cmp_device_info(const void *a, const void *b)
4747 const struct btrfs_device_info *di_a = a;
4748 const struct btrfs_device_info *di_b = b;
4750 if (di_a->max_avail > di_b->max_avail)
4751 return -1;
4752 if (di_a->max_avail < di_b->max_avail)
4753 return 1;
4754 if (di_a->total_avail > di_b->total_avail)
4755 return -1;
4756 if (di_a->total_avail < di_b->total_avail)
4757 return 1;
4758 return 0;
4761 static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4763 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
4764 return;
4766 btrfs_set_fs_incompat(info, RAID56);
4769 static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type)
4771 if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4)))
4772 return;
4774 btrfs_set_fs_incompat(info, RAID1C34);
4777 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4778 u64 start, u64 type)
4780 struct btrfs_fs_info *info = trans->fs_info;
4781 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4782 struct btrfs_device *device;
4783 struct map_lookup *map = NULL;
4784 struct extent_map_tree *em_tree;
4785 struct extent_map *em;
4786 struct btrfs_device_info *devices_info = NULL;
4787 u64 total_avail;
4788 int num_stripes; /* total number of stripes to allocate */
4789 int data_stripes; /* number of stripes that count for
4790 block group size */
4791 int sub_stripes; /* sub_stripes info for map */
4792 int dev_stripes; /* stripes per dev */
4793 int devs_max; /* max devs to use */
4794 int devs_min; /* min devs needed */
4795 int devs_increment; /* ndevs has to be a multiple of this */
4796 int ncopies; /* how many copies to data has */
4797 int nparity; /* number of stripes worth of bytes to
4798 store parity information */
4799 int ret;
4800 u64 max_stripe_size;
4801 u64 max_chunk_size;
4802 u64 stripe_size;
4803 u64 chunk_size;
4804 int ndevs;
4805 int i;
4806 int j;
4807 int index;
4809 BUG_ON(!alloc_profile_is_valid(type, 0));
4811 if (list_empty(&fs_devices->alloc_list)) {
4812 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4813 btrfs_debug(info, "%s: no writable device", __func__);
4814 return -ENOSPC;
4817 index = btrfs_bg_flags_to_raid_index(type);
4819 sub_stripes = btrfs_raid_array[index].sub_stripes;
4820 dev_stripes = btrfs_raid_array[index].dev_stripes;
4821 devs_max = btrfs_raid_array[index].devs_max;
4822 if (!devs_max)
4823 devs_max = BTRFS_MAX_DEVS(info);
4824 devs_min = btrfs_raid_array[index].devs_min;
4825 devs_increment = btrfs_raid_array[index].devs_increment;
4826 ncopies = btrfs_raid_array[index].ncopies;
4827 nparity = btrfs_raid_array[index].nparity;
4829 if (type & BTRFS_BLOCK_GROUP_DATA) {
4830 max_stripe_size = SZ_1G;
4831 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
4832 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4833 /* for larger filesystems, use larger metadata chunks */
4834 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4835 max_stripe_size = SZ_1G;
4836 else
4837 max_stripe_size = SZ_256M;
4838 max_chunk_size = max_stripe_size;
4839 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4840 max_stripe_size = SZ_32M;
4841 max_chunk_size = 2 * max_stripe_size;
4842 devs_max = min_t(int, devs_max, BTRFS_MAX_DEVS_SYS_CHUNK);
4843 } else {
4844 btrfs_err(info, "invalid chunk type 0x%llx requested",
4845 type);
4846 BUG();
4849 /* We don't want a chunk larger than 10% of writable space */
4850 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4851 max_chunk_size);
4853 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
4854 GFP_NOFS);
4855 if (!devices_info)
4856 return -ENOMEM;
4859 * in the first pass through the devices list, we gather information
4860 * about the available holes on each device.
4862 ndevs = 0;
4863 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
4864 u64 max_avail;
4865 u64 dev_offset;
4867 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
4868 WARN(1, KERN_ERR
4869 "BTRFS: read-only device in alloc_list\n");
4870 continue;
4873 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4874 &device->dev_state) ||
4875 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
4876 continue;
4878 if (device->total_bytes > device->bytes_used)
4879 total_avail = device->total_bytes - device->bytes_used;
4880 else
4881 total_avail = 0;
4883 /* If there is no space on this device, skip it. */
4884 if (total_avail == 0)
4885 continue;
4887 ret = find_free_dev_extent(device,
4888 max_stripe_size * dev_stripes,
4889 &dev_offset, &max_avail);
4890 if (ret && ret != -ENOSPC)
4891 goto error;
4893 if (ret == 0)
4894 max_avail = max_stripe_size * dev_stripes;
4896 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
4897 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4898 btrfs_debug(info,
4899 "%s: devid %llu has no free space, have=%llu want=%u",
4900 __func__, device->devid, max_avail,
4901 BTRFS_STRIPE_LEN * dev_stripes);
4902 continue;
4905 if (ndevs == fs_devices->rw_devices) {
4906 WARN(1, "%s: found more than %llu devices\n",
4907 __func__, fs_devices->rw_devices);
4908 break;
4910 devices_info[ndevs].dev_offset = dev_offset;
4911 devices_info[ndevs].max_avail = max_avail;
4912 devices_info[ndevs].total_avail = total_avail;
4913 devices_info[ndevs].dev = device;
4914 ++ndevs;
4918 * now sort the devices by hole size / available space
4920 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4921 btrfs_cmp_device_info, NULL);
4924 * Round down to number of usable stripes, devs_increment can be any
4925 * number so we can't use round_down()
4927 ndevs -= ndevs % devs_increment;
4929 if (ndevs < devs_min) {
4930 ret = -ENOSPC;
4931 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
4932 btrfs_debug(info,
4933 "%s: not enough devices with free space: have=%d minimum required=%d",
4934 __func__, ndevs, devs_min);
4936 goto error;
4939 ndevs = min(ndevs, devs_max);
4942 * The primary goal is to maximize the number of stripes, so use as
4943 * many devices as possible, even if the stripes are not maximum sized.
4945 * The DUP profile stores more than one stripe per device, the
4946 * max_avail is the total size so we have to adjust.
4948 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
4949 num_stripes = ndevs * dev_stripes;
4952 * this will have to be fixed for RAID1 and RAID10 over
4953 * more drives
4955 data_stripes = (num_stripes - nparity) / ncopies;
4958 * Use the number of data stripes to figure out how big this chunk
4959 * is really going to be in terms of logical address space,
4960 * and compare that answer with the max chunk size. If it's higher,
4961 * we try to reduce stripe_size.
4963 if (stripe_size * data_stripes > max_chunk_size) {
4965 * Reduce stripe_size, round it up to a 16MB boundary again and
4966 * then use it, unless it ends up being even bigger than the
4967 * previous value we had already.
4969 stripe_size = min(round_up(div_u64(max_chunk_size,
4970 data_stripes), SZ_16M),
4971 stripe_size);
4974 /* align to BTRFS_STRIPE_LEN */
4975 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
4977 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4978 if (!map) {
4979 ret = -ENOMEM;
4980 goto error;
4982 map->num_stripes = num_stripes;
4984 for (i = 0; i < ndevs; ++i) {
4985 for (j = 0; j < dev_stripes; ++j) {
4986 int s = i * dev_stripes + j;
4987 map->stripes[s].dev = devices_info[i].dev;
4988 map->stripes[s].physical = devices_info[i].dev_offset +
4989 j * stripe_size;
4992 map->stripe_len = BTRFS_STRIPE_LEN;
4993 map->io_align = BTRFS_STRIPE_LEN;
4994 map->io_width = BTRFS_STRIPE_LEN;
4995 map->type = type;
4996 map->sub_stripes = sub_stripes;
4998 chunk_size = stripe_size * data_stripes;
5000 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
5002 em = alloc_extent_map();
5003 if (!em) {
5004 kfree(map);
5005 ret = -ENOMEM;
5006 goto error;
5008 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
5009 em->map_lookup = map;
5010 em->start = start;
5011 em->len = chunk_size;
5012 em->block_start = 0;
5013 em->block_len = em->len;
5014 em->orig_block_len = stripe_size;
5016 em_tree = &info->mapping_tree;
5017 write_lock(&em_tree->lock);
5018 ret = add_extent_mapping(em_tree, em, 0);
5019 if (ret) {
5020 write_unlock(&em_tree->lock);
5021 free_extent_map(em);
5022 goto error;
5024 write_unlock(&em_tree->lock);
5026 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
5027 if (ret)
5028 goto error_del_extent;
5030 for (i = 0; i < map->num_stripes; i++) {
5031 struct btrfs_device *dev = map->stripes[i].dev;
5033 btrfs_device_set_bytes_used(dev, dev->bytes_used + stripe_size);
5034 if (list_empty(&dev->post_commit_list))
5035 list_add_tail(&dev->post_commit_list,
5036 &trans->transaction->dev_update_list);
5039 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
5041 free_extent_map(em);
5042 check_raid56_incompat_flag(info, type);
5043 check_raid1c34_incompat_flag(info, type);
5045 kfree(devices_info);
5046 return 0;
5048 error_del_extent:
5049 write_lock(&em_tree->lock);
5050 remove_extent_mapping(em_tree, em);
5051 write_unlock(&em_tree->lock);
5053 /* One for our allocation */
5054 free_extent_map(em);
5055 /* One for the tree reference */
5056 free_extent_map(em);
5057 error:
5058 kfree(devices_info);
5059 return ret;
5062 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
5063 u64 chunk_offset, u64 chunk_size)
5065 struct btrfs_fs_info *fs_info = trans->fs_info;
5066 struct btrfs_root *extent_root = fs_info->extent_root;
5067 struct btrfs_root *chunk_root = fs_info->chunk_root;
5068 struct btrfs_key key;
5069 struct btrfs_device *device;
5070 struct btrfs_chunk *chunk;
5071 struct btrfs_stripe *stripe;
5072 struct extent_map *em;
5073 struct map_lookup *map;
5074 size_t item_size;
5075 u64 dev_offset;
5076 u64 stripe_size;
5077 int i = 0;
5078 int ret = 0;
5080 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
5081 if (IS_ERR(em))
5082 return PTR_ERR(em);
5084 map = em->map_lookup;
5085 item_size = btrfs_chunk_item_size(map->num_stripes);
5086 stripe_size = em->orig_block_len;
5088 chunk = kzalloc(item_size, GFP_NOFS);
5089 if (!chunk) {
5090 ret = -ENOMEM;
5091 goto out;
5095 * Take the device list mutex to prevent races with the final phase of
5096 * a device replace operation that replaces the device object associated
5097 * with the map's stripes, because the device object's id can change
5098 * at any time during that final phase of the device replace operation
5099 * (dev-replace.c:btrfs_dev_replace_finishing()).
5101 mutex_lock(&fs_info->fs_devices->device_list_mutex);
5102 for (i = 0; i < map->num_stripes; i++) {
5103 device = map->stripes[i].dev;
5104 dev_offset = map->stripes[i].physical;
5106 ret = btrfs_update_device(trans, device);
5107 if (ret)
5108 break;
5109 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5110 dev_offset, stripe_size);
5111 if (ret)
5112 break;
5114 if (ret) {
5115 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
5116 goto out;
5119 stripe = &chunk->stripe;
5120 for (i = 0; i < map->num_stripes; i++) {
5121 device = map->stripes[i].dev;
5122 dev_offset = map->stripes[i].physical;
5124 btrfs_set_stack_stripe_devid(stripe, device->devid);
5125 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5126 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5127 stripe++;
5129 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
5131 btrfs_set_stack_chunk_length(chunk, chunk_size);
5132 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5133 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5134 btrfs_set_stack_chunk_type(chunk, map->type);
5135 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5136 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5137 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
5138 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
5139 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5141 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5142 key.type = BTRFS_CHUNK_ITEM_KEY;
5143 key.offset = chunk_offset;
5145 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
5146 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5148 * TODO: Cleanup of inserted chunk root in case of
5149 * failure.
5151 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
5154 out:
5155 kfree(chunk);
5156 free_extent_map(em);
5157 return ret;
5161 * Chunk allocation falls into two parts. The first part does work
5162 * that makes the new allocated chunk usable, but does not do any operation
5163 * that modifies the chunk tree. The second part does the work that
5164 * requires modifying the chunk tree. This division is important for the
5165 * bootstrap process of adding storage to a seed btrfs.
5167 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
5169 u64 chunk_offset;
5171 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5172 chunk_offset = find_next_chunk(trans->fs_info);
5173 return __btrfs_alloc_chunk(trans, chunk_offset, type);
5176 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
5178 struct btrfs_fs_info *fs_info = trans->fs_info;
5179 u64 chunk_offset;
5180 u64 sys_chunk_offset;
5181 u64 alloc_profile;
5182 int ret;
5184 chunk_offset = find_next_chunk(fs_info);
5185 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
5186 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
5187 if (ret)
5188 return ret;
5190 sys_chunk_offset = find_next_chunk(fs_info);
5191 alloc_profile = btrfs_system_alloc_profile(fs_info);
5192 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
5193 return ret;
5196 static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5198 const int index = btrfs_bg_flags_to_raid_index(map->type);
5200 return btrfs_raid_array[index].tolerated_failures;
5203 int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
5205 struct extent_map *em;
5206 struct map_lookup *map;
5207 int readonly = 0;
5208 int miss_ndevs = 0;
5209 int i;
5211 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
5212 if (IS_ERR(em))
5213 return 1;
5215 map = em->map_lookup;
5216 for (i = 0; i < map->num_stripes; i++) {
5217 if (test_bit(BTRFS_DEV_STATE_MISSING,
5218 &map->stripes[i].dev->dev_state)) {
5219 miss_ndevs++;
5220 continue;
5222 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5223 &map->stripes[i].dev->dev_state)) {
5224 readonly = 1;
5225 goto end;
5230 * If the number of missing devices is larger than max errors,
5231 * we can not write the data into that chunk successfully, so
5232 * set it readonly.
5234 if (miss_ndevs > btrfs_chunk_max_errors(map))
5235 readonly = 1;
5236 end:
5237 free_extent_map(em);
5238 return readonly;
5241 void btrfs_mapping_tree_free(struct extent_map_tree *tree)
5243 struct extent_map *em;
5245 while (1) {
5246 write_lock(&tree->lock);
5247 em = lookup_extent_mapping(tree, 0, (u64)-1);
5248 if (em)
5249 remove_extent_mapping(tree, em);
5250 write_unlock(&tree->lock);
5251 if (!em)
5252 break;
5253 /* once for us */
5254 free_extent_map(em);
5255 /* once for the tree */
5256 free_extent_map(em);
5260 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
5262 struct extent_map *em;
5263 struct map_lookup *map;
5264 int ret;
5266 em = btrfs_get_chunk_map(fs_info, logical, len);
5267 if (IS_ERR(em))
5269 * We could return errors for these cases, but that could get
5270 * ugly and we'd probably do the same thing which is just not do
5271 * anything else and exit, so return 1 so the callers don't try
5272 * to use other copies.
5274 return 1;
5276 map = em->map_lookup;
5277 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK))
5278 ret = map->num_stripes;
5279 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5280 ret = map->sub_stripes;
5281 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5282 ret = 2;
5283 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5285 * There could be two corrupted data stripes, we need
5286 * to loop retry in order to rebuild the correct data.
5288 * Fail a stripe at a time on every retry except the
5289 * stripe under reconstruction.
5291 ret = map->num_stripes;
5292 else
5293 ret = 1;
5294 free_extent_map(em);
5296 down_read(&fs_info->dev_replace.rwsem);
5297 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5298 fs_info->dev_replace.tgtdev)
5299 ret++;
5300 up_read(&fs_info->dev_replace.rwsem);
5302 return ret;
5305 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
5306 u64 logical)
5308 struct extent_map *em;
5309 struct map_lookup *map;
5310 unsigned long len = fs_info->sectorsize;
5312 em = btrfs_get_chunk_map(fs_info, logical, len);
5314 if (!WARN_ON(IS_ERR(em))) {
5315 map = em->map_lookup;
5316 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5317 len = map->stripe_len * nr_data_stripes(map);
5318 free_extent_map(em);
5320 return len;
5323 int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
5325 struct extent_map *em;
5326 struct map_lookup *map;
5327 int ret = 0;
5329 em = btrfs_get_chunk_map(fs_info, logical, len);
5331 if(!WARN_ON(IS_ERR(em))) {
5332 map = em->map_lookup;
5333 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5334 ret = 1;
5335 free_extent_map(em);
5337 return ret;
5340 static int find_live_mirror(struct btrfs_fs_info *fs_info,
5341 struct map_lookup *map, int first,
5342 int dev_replace_is_ongoing)
5344 int i;
5345 int num_stripes;
5346 int preferred_mirror;
5347 int tolerance;
5348 struct btrfs_device *srcdev;
5350 ASSERT((map->type &
5351 (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10)));
5353 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5354 num_stripes = map->sub_stripes;
5355 else
5356 num_stripes = map->num_stripes;
5358 preferred_mirror = first + current->pid % num_stripes;
5360 if (dev_replace_is_ongoing &&
5361 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5362 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5363 srcdev = fs_info->dev_replace.srcdev;
5364 else
5365 srcdev = NULL;
5368 * try to avoid the drive that is the source drive for a
5369 * dev-replace procedure, only choose it if no other non-missing
5370 * mirror is available
5372 for (tolerance = 0; tolerance < 2; tolerance++) {
5373 if (map->stripes[preferred_mirror].dev->bdev &&
5374 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5375 return preferred_mirror;
5376 for (i = first; i < first + num_stripes; i++) {
5377 if (map->stripes[i].dev->bdev &&
5378 (tolerance || map->stripes[i].dev != srcdev))
5379 return i;
5383 /* we couldn't find one that doesn't fail. Just return something
5384 * and the io error handling code will clean up eventually
5386 return preferred_mirror;
5389 static inline int parity_smaller(u64 a, u64 b)
5391 return a > b;
5394 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
5395 static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
5397 struct btrfs_bio_stripe s;
5398 int i;
5399 u64 l;
5400 int again = 1;
5402 while (again) {
5403 again = 0;
5404 for (i = 0; i < num_stripes - 1; i++) {
5405 if (parity_smaller(bbio->raid_map[i],
5406 bbio->raid_map[i+1])) {
5407 s = bbio->stripes[i];
5408 l = bbio->raid_map[i];
5409 bbio->stripes[i] = bbio->stripes[i+1];
5410 bbio->raid_map[i] = bbio->raid_map[i+1];
5411 bbio->stripes[i+1] = s;
5412 bbio->raid_map[i+1] = l;
5414 again = 1;
5420 static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5422 struct btrfs_bio *bbio = kzalloc(
5423 /* the size of the btrfs_bio */
5424 sizeof(struct btrfs_bio) +
5425 /* plus the variable array for the stripes */
5426 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
5427 /* plus the variable array for the tgt dev */
5428 sizeof(int) * (real_stripes) +
5430 * plus the raid_map, which includes both the tgt dev
5431 * and the stripes
5433 sizeof(u64) * (total_stripes),
5434 GFP_NOFS|__GFP_NOFAIL);
5436 atomic_set(&bbio->error, 0);
5437 refcount_set(&bbio->refs, 1);
5439 return bbio;
5442 void btrfs_get_bbio(struct btrfs_bio *bbio)
5444 WARN_ON(!refcount_read(&bbio->refs));
5445 refcount_inc(&bbio->refs);
5448 void btrfs_put_bbio(struct btrfs_bio *bbio)
5450 if (!bbio)
5451 return;
5452 if (refcount_dec_and_test(&bbio->refs))
5453 kfree(bbio);
5456 /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5458 * Please note that, discard won't be sent to target device of device
5459 * replace.
5461 static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5462 u64 logical, u64 *length_ret,
5463 struct btrfs_bio **bbio_ret)
5465 struct extent_map *em;
5466 struct map_lookup *map;
5467 struct btrfs_bio *bbio;
5468 u64 length = *length_ret;
5469 u64 offset;
5470 u64 stripe_nr;
5471 u64 stripe_nr_end;
5472 u64 stripe_end_offset;
5473 u64 stripe_cnt;
5474 u64 stripe_len;
5475 u64 stripe_offset;
5476 u64 num_stripes;
5477 u32 stripe_index;
5478 u32 factor = 0;
5479 u32 sub_stripes = 0;
5480 u64 stripes_per_dev = 0;
5481 u32 remaining_stripes = 0;
5482 u32 last_stripe = 0;
5483 int ret = 0;
5484 int i;
5486 /* discard always return a bbio */
5487 ASSERT(bbio_ret);
5489 em = btrfs_get_chunk_map(fs_info, logical, length);
5490 if (IS_ERR(em))
5491 return PTR_ERR(em);
5493 map = em->map_lookup;
5494 /* we don't discard raid56 yet */
5495 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5496 ret = -EOPNOTSUPP;
5497 goto out;
5500 offset = logical - em->start;
5501 length = min_t(u64, em->start + em->len - logical, length);
5502 *length_ret = length;
5504 stripe_len = map->stripe_len;
5506 * stripe_nr counts the total number of stripes we have to stride
5507 * to get to this block
5509 stripe_nr = div64_u64(offset, stripe_len);
5511 /* stripe_offset is the offset of this block in its stripe */
5512 stripe_offset = offset - stripe_nr * stripe_len;
5514 stripe_nr_end = round_up(offset + length, map->stripe_len);
5515 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
5516 stripe_cnt = stripe_nr_end - stripe_nr;
5517 stripe_end_offset = stripe_nr_end * map->stripe_len -
5518 (offset + length);
5520 * after this, stripe_nr is the number of stripes on this
5521 * device we have to walk to find the data, and stripe_index is
5522 * the number of our device in the stripe array
5524 num_stripes = 1;
5525 stripe_index = 0;
5526 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5527 BTRFS_BLOCK_GROUP_RAID10)) {
5528 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5529 sub_stripes = 1;
5530 else
5531 sub_stripes = map->sub_stripes;
5533 factor = map->num_stripes / sub_stripes;
5534 num_stripes = min_t(u64, map->num_stripes,
5535 sub_stripes * stripe_cnt);
5536 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5537 stripe_index *= sub_stripes;
5538 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5539 &remaining_stripes);
5540 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5541 last_stripe *= sub_stripes;
5542 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK |
5543 BTRFS_BLOCK_GROUP_DUP)) {
5544 num_stripes = map->num_stripes;
5545 } else {
5546 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5547 &stripe_index);
5550 bbio = alloc_btrfs_bio(num_stripes, 0);
5551 if (!bbio) {
5552 ret = -ENOMEM;
5553 goto out;
5556 for (i = 0; i < num_stripes; i++) {
5557 bbio->stripes[i].physical =
5558 map->stripes[stripe_index].physical +
5559 stripe_offset + stripe_nr * map->stripe_len;
5560 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5562 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5563 BTRFS_BLOCK_GROUP_RAID10)) {
5564 bbio->stripes[i].length = stripes_per_dev *
5565 map->stripe_len;
5567 if (i / sub_stripes < remaining_stripes)
5568 bbio->stripes[i].length +=
5569 map->stripe_len;
5572 * Special for the first stripe and
5573 * the last stripe:
5575 * |-------|...|-------|
5576 * |----------|
5577 * off end_off
5579 if (i < sub_stripes)
5580 bbio->stripes[i].length -=
5581 stripe_offset;
5583 if (stripe_index >= last_stripe &&
5584 stripe_index <= (last_stripe +
5585 sub_stripes - 1))
5586 bbio->stripes[i].length -=
5587 stripe_end_offset;
5589 if (i == sub_stripes - 1)
5590 stripe_offset = 0;
5591 } else {
5592 bbio->stripes[i].length = length;
5595 stripe_index++;
5596 if (stripe_index == map->num_stripes) {
5597 stripe_index = 0;
5598 stripe_nr++;
5602 *bbio_ret = bbio;
5603 bbio->map_type = map->type;
5604 bbio->num_stripes = num_stripes;
5605 out:
5606 free_extent_map(em);
5607 return ret;
5611 * In dev-replace case, for repair case (that's the only case where the mirror
5612 * is selected explicitly when calling btrfs_map_block), blocks left of the
5613 * left cursor can also be read from the target drive.
5615 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5616 * array of stripes.
5617 * For READ, it also needs to be supported using the same mirror number.
5619 * If the requested block is not left of the left cursor, EIO is returned. This
5620 * can happen because btrfs_num_copies() returns one more in the dev-replace
5621 * case.
5623 static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5624 u64 logical, u64 length,
5625 u64 srcdev_devid, int *mirror_num,
5626 u64 *physical)
5628 struct btrfs_bio *bbio = NULL;
5629 int num_stripes;
5630 int index_srcdev = 0;
5631 int found = 0;
5632 u64 physical_of_found = 0;
5633 int i;
5634 int ret = 0;
5636 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5637 logical, &length, &bbio, 0, 0);
5638 if (ret) {
5639 ASSERT(bbio == NULL);
5640 return ret;
5643 num_stripes = bbio->num_stripes;
5644 if (*mirror_num > num_stripes) {
5646 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5647 * that means that the requested area is not left of the left
5648 * cursor
5650 btrfs_put_bbio(bbio);
5651 return -EIO;
5655 * process the rest of the function using the mirror_num of the source
5656 * drive. Therefore look it up first. At the end, patch the device
5657 * pointer to the one of the target drive.
5659 for (i = 0; i < num_stripes; i++) {
5660 if (bbio->stripes[i].dev->devid != srcdev_devid)
5661 continue;
5664 * In case of DUP, in order to keep it simple, only add the
5665 * mirror with the lowest physical address
5667 if (found &&
5668 physical_of_found <= bbio->stripes[i].physical)
5669 continue;
5671 index_srcdev = i;
5672 found = 1;
5673 physical_of_found = bbio->stripes[i].physical;
5676 btrfs_put_bbio(bbio);
5678 ASSERT(found);
5679 if (!found)
5680 return -EIO;
5682 *mirror_num = index_srcdev + 1;
5683 *physical = physical_of_found;
5684 return ret;
5687 static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5688 struct btrfs_bio **bbio_ret,
5689 struct btrfs_dev_replace *dev_replace,
5690 int *num_stripes_ret, int *max_errors_ret)
5692 struct btrfs_bio *bbio = *bbio_ret;
5693 u64 srcdev_devid = dev_replace->srcdev->devid;
5694 int tgtdev_indexes = 0;
5695 int num_stripes = *num_stripes_ret;
5696 int max_errors = *max_errors_ret;
5697 int i;
5699 if (op == BTRFS_MAP_WRITE) {
5700 int index_where_to_add;
5703 * duplicate the write operations while the dev replace
5704 * procedure is running. Since the copying of the old disk to
5705 * the new disk takes place at run time while the filesystem is
5706 * mounted writable, the regular write operations to the old
5707 * disk have to be duplicated to go to the new disk as well.
5709 * Note that device->missing is handled by the caller, and that
5710 * the write to the old disk is already set up in the stripes
5711 * array.
5713 index_where_to_add = num_stripes;
5714 for (i = 0; i < num_stripes; i++) {
5715 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5716 /* write to new disk, too */
5717 struct btrfs_bio_stripe *new =
5718 bbio->stripes + index_where_to_add;
5719 struct btrfs_bio_stripe *old =
5720 bbio->stripes + i;
5722 new->physical = old->physical;
5723 new->length = old->length;
5724 new->dev = dev_replace->tgtdev;
5725 bbio->tgtdev_map[i] = index_where_to_add;
5726 index_where_to_add++;
5727 max_errors++;
5728 tgtdev_indexes++;
5731 num_stripes = index_where_to_add;
5732 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5733 int index_srcdev = 0;
5734 int found = 0;
5735 u64 physical_of_found = 0;
5738 * During the dev-replace procedure, the target drive can also
5739 * be used to read data in case it is needed to repair a corrupt
5740 * block elsewhere. This is possible if the requested area is
5741 * left of the left cursor. In this area, the target drive is a
5742 * full copy of the source drive.
5744 for (i = 0; i < num_stripes; i++) {
5745 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5747 * In case of DUP, in order to keep it simple,
5748 * only add the mirror with the lowest physical
5749 * address
5751 if (found &&
5752 physical_of_found <=
5753 bbio->stripes[i].physical)
5754 continue;
5755 index_srcdev = i;
5756 found = 1;
5757 physical_of_found = bbio->stripes[i].physical;
5760 if (found) {
5761 struct btrfs_bio_stripe *tgtdev_stripe =
5762 bbio->stripes + num_stripes;
5764 tgtdev_stripe->physical = physical_of_found;
5765 tgtdev_stripe->length =
5766 bbio->stripes[index_srcdev].length;
5767 tgtdev_stripe->dev = dev_replace->tgtdev;
5768 bbio->tgtdev_map[index_srcdev] = num_stripes;
5770 tgtdev_indexes++;
5771 num_stripes++;
5775 *num_stripes_ret = num_stripes;
5776 *max_errors_ret = max_errors;
5777 bbio->num_tgtdevs = tgtdev_indexes;
5778 *bbio_ret = bbio;
5781 static bool need_full_stripe(enum btrfs_map_op op)
5783 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5787 * btrfs_get_io_geometry - calculates the geomery of a particular (address, len)
5788 * tuple. This information is used to calculate how big a
5789 * particular bio can get before it straddles a stripe.
5791 * @fs_info - the filesystem
5792 * @logical - address that we want to figure out the geometry of
5793 * @len - the length of IO we are going to perform, starting at @logical
5794 * @op - type of operation - write or read
5795 * @io_geom - pointer used to return values
5797 * Returns < 0 in case a chunk for the given logical address cannot be found,
5798 * usually shouldn't happen unless @logical is corrupted, 0 otherwise.
5800 int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
5801 u64 logical, u64 len, struct btrfs_io_geometry *io_geom)
5803 struct extent_map *em;
5804 struct map_lookup *map;
5805 u64 offset;
5806 u64 stripe_offset;
5807 u64 stripe_nr;
5808 u64 stripe_len;
5809 u64 raid56_full_stripe_start = (u64)-1;
5810 int data_stripes;
5811 int ret = 0;
5813 ASSERT(op != BTRFS_MAP_DISCARD);
5815 em = btrfs_get_chunk_map(fs_info, logical, len);
5816 if (IS_ERR(em))
5817 return PTR_ERR(em);
5819 map = em->map_lookup;
5820 /* Offset of this logical address in the chunk */
5821 offset = logical - em->start;
5822 /* Len of a stripe in a chunk */
5823 stripe_len = map->stripe_len;
5824 /* Stripe wher this block falls in */
5825 stripe_nr = div64_u64(offset, stripe_len);
5826 /* Offset of stripe in the chunk */
5827 stripe_offset = stripe_nr * stripe_len;
5828 if (offset < stripe_offset) {
5829 btrfs_crit(fs_info,
5830 "stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu",
5831 stripe_offset, offset, em->start, logical, stripe_len);
5832 ret = -EINVAL;
5833 goto out;
5836 /* stripe_offset is the offset of this block in its stripe */
5837 stripe_offset = offset - stripe_offset;
5838 data_stripes = nr_data_stripes(map);
5840 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5841 u64 max_len = stripe_len - stripe_offset;
5844 * In case of raid56, we need to know the stripe aligned start
5846 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5847 unsigned long full_stripe_len = stripe_len * data_stripes;
5848 raid56_full_stripe_start = offset;
5851 * Allow a write of a full stripe, but make sure we
5852 * don't allow straddling of stripes
5854 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5855 full_stripe_len);
5856 raid56_full_stripe_start *= full_stripe_len;
5859 * For writes to RAID[56], allow a full stripeset across
5860 * all disks. For other RAID types and for RAID[56]
5861 * reads, just allow a single stripe (on a single disk).
5863 if (op == BTRFS_MAP_WRITE) {
5864 max_len = stripe_len * data_stripes -
5865 (offset - raid56_full_stripe_start);
5868 len = min_t(u64, em->len - offset, max_len);
5869 } else {
5870 len = em->len - offset;
5873 io_geom->len = len;
5874 io_geom->offset = offset;
5875 io_geom->stripe_len = stripe_len;
5876 io_geom->stripe_nr = stripe_nr;
5877 io_geom->stripe_offset = stripe_offset;
5878 io_geom->raid56_stripe_offset = raid56_full_stripe_start;
5880 out:
5881 /* once for us */
5882 free_extent_map(em);
5883 return ret;
5886 static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5887 enum btrfs_map_op op,
5888 u64 logical, u64 *length,
5889 struct btrfs_bio **bbio_ret,
5890 int mirror_num, int need_raid_map)
5892 struct extent_map *em;
5893 struct map_lookup *map;
5894 u64 stripe_offset;
5895 u64 stripe_nr;
5896 u64 stripe_len;
5897 u32 stripe_index;
5898 int data_stripes;
5899 int i;
5900 int ret = 0;
5901 int num_stripes;
5902 int max_errors = 0;
5903 int tgtdev_indexes = 0;
5904 struct btrfs_bio *bbio = NULL;
5905 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5906 int dev_replace_is_ongoing = 0;
5907 int num_alloc_stripes;
5908 int patch_the_first_stripe_for_dev_replace = 0;
5909 u64 physical_to_patch_in_first_stripe = 0;
5910 u64 raid56_full_stripe_start = (u64)-1;
5911 struct btrfs_io_geometry geom;
5913 ASSERT(bbio_ret);
5915 if (op == BTRFS_MAP_DISCARD)
5916 return __btrfs_map_block_for_discard(fs_info, logical,
5917 length, bbio_ret);
5919 ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom);
5920 if (ret < 0)
5921 return ret;
5923 em = btrfs_get_chunk_map(fs_info, logical, *length);
5924 ASSERT(!IS_ERR(em));
5925 map = em->map_lookup;
5927 *length = geom.len;
5928 stripe_len = geom.stripe_len;
5929 stripe_nr = geom.stripe_nr;
5930 stripe_offset = geom.stripe_offset;
5931 raid56_full_stripe_start = geom.raid56_stripe_offset;
5932 data_stripes = nr_data_stripes(map);
5934 down_read(&dev_replace->rwsem);
5935 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5937 * Hold the semaphore for read during the whole operation, write is
5938 * requested at commit time but must wait.
5940 if (!dev_replace_is_ongoing)
5941 up_read(&dev_replace->rwsem);
5943 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5944 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
5945 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5946 dev_replace->srcdev->devid,
5947 &mirror_num,
5948 &physical_to_patch_in_first_stripe);
5949 if (ret)
5950 goto out;
5951 else
5952 patch_the_first_stripe_for_dev_replace = 1;
5953 } else if (mirror_num > map->num_stripes) {
5954 mirror_num = 0;
5957 num_stripes = 1;
5958 stripe_index = 0;
5959 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5960 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5961 &stripe_index);
5962 if (!need_full_stripe(op))
5963 mirror_num = 1;
5964 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
5965 if (need_full_stripe(op))
5966 num_stripes = map->num_stripes;
5967 else if (mirror_num)
5968 stripe_index = mirror_num - 1;
5969 else {
5970 stripe_index = find_live_mirror(fs_info, map, 0,
5971 dev_replace_is_ongoing);
5972 mirror_num = stripe_index + 1;
5975 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
5976 if (need_full_stripe(op)) {
5977 num_stripes = map->num_stripes;
5978 } else if (mirror_num) {
5979 stripe_index = mirror_num - 1;
5980 } else {
5981 mirror_num = 1;
5984 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5985 u32 factor = map->num_stripes / map->sub_stripes;
5987 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5988 stripe_index *= map->sub_stripes;
5990 if (need_full_stripe(op))
5991 num_stripes = map->sub_stripes;
5992 else if (mirror_num)
5993 stripe_index += mirror_num - 1;
5994 else {
5995 int old_stripe_index = stripe_index;
5996 stripe_index = find_live_mirror(fs_info, map,
5997 stripe_index,
5998 dev_replace_is_ongoing);
5999 mirror_num = stripe_index - old_stripe_index + 1;
6002 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
6003 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
6004 /* push stripe_nr back to the start of the full stripe */
6005 stripe_nr = div64_u64(raid56_full_stripe_start,
6006 stripe_len * data_stripes);
6008 /* RAID[56] write or recovery. Return all stripes */
6009 num_stripes = map->num_stripes;
6010 max_errors = nr_parity_stripes(map);
6012 *length = map->stripe_len;
6013 stripe_index = 0;
6014 stripe_offset = 0;
6015 } else {
6017 * Mirror #0 or #1 means the original data block.
6018 * Mirror #2 is RAID5 parity block.
6019 * Mirror #3 is RAID6 Q block.
6021 stripe_nr = div_u64_rem(stripe_nr,
6022 data_stripes, &stripe_index);
6023 if (mirror_num > 1)
6024 stripe_index = data_stripes + mirror_num - 2;
6026 /* We distribute the parity blocks across stripes */
6027 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6028 &stripe_index);
6029 if (!need_full_stripe(op) && mirror_num <= 1)
6030 mirror_num = 1;
6032 } else {
6034 * after this, stripe_nr is the number of stripes on this
6035 * device we have to walk to find the data, and stripe_index is
6036 * the number of our device in the stripe array
6038 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6039 &stripe_index);
6040 mirror_num = stripe_index + 1;
6042 if (stripe_index >= map->num_stripes) {
6043 btrfs_crit(fs_info,
6044 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
6045 stripe_index, map->num_stripes);
6046 ret = -EINVAL;
6047 goto out;
6050 num_alloc_stripes = num_stripes;
6051 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
6052 if (op == BTRFS_MAP_WRITE)
6053 num_alloc_stripes <<= 1;
6054 if (op == BTRFS_MAP_GET_READ_MIRRORS)
6055 num_alloc_stripes++;
6056 tgtdev_indexes = num_stripes;
6059 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
6060 if (!bbio) {
6061 ret = -ENOMEM;
6062 goto out;
6064 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
6065 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
6067 /* build raid_map */
6068 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6069 (need_full_stripe(op) || mirror_num > 1)) {
6070 u64 tmp;
6071 unsigned rot;
6073 bbio->raid_map = (u64 *)((void *)bbio->stripes +
6074 sizeof(struct btrfs_bio_stripe) *
6075 num_alloc_stripes +
6076 sizeof(int) * tgtdev_indexes);
6078 /* Work out the disk rotation on this stripe-set */
6079 div_u64_rem(stripe_nr, num_stripes, &rot);
6081 /* Fill in the logical address of each stripe */
6082 tmp = stripe_nr * data_stripes;
6083 for (i = 0; i < data_stripes; i++)
6084 bbio->raid_map[(i+rot) % num_stripes] =
6085 em->start + (tmp + i) * map->stripe_len;
6087 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6088 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6089 bbio->raid_map[(i+rot+1) % num_stripes] =
6090 RAID6_Q_STRIPE;
6094 for (i = 0; i < num_stripes; i++) {
6095 bbio->stripes[i].physical =
6096 map->stripes[stripe_index].physical +
6097 stripe_offset +
6098 stripe_nr * map->stripe_len;
6099 bbio->stripes[i].dev =
6100 map->stripes[stripe_index].dev;
6101 stripe_index++;
6104 if (need_full_stripe(op))
6105 max_errors = btrfs_chunk_max_errors(map);
6107 if (bbio->raid_map)
6108 sort_parity_stripes(bbio, num_stripes);
6110 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
6111 need_full_stripe(op)) {
6112 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6113 &max_errors);
6116 *bbio_ret = bbio;
6117 bbio->map_type = map->type;
6118 bbio->num_stripes = num_stripes;
6119 bbio->max_errors = max_errors;
6120 bbio->mirror_num = mirror_num;
6123 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6124 * mirror_num == num_stripes + 1 && dev_replace target drive is
6125 * available as a mirror
6127 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6128 WARN_ON(num_stripes > 1);
6129 bbio->stripes[0].dev = dev_replace->tgtdev;
6130 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6131 bbio->mirror_num = map->num_stripes + 1;
6133 out:
6134 if (dev_replace_is_ongoing) {
6135 lockdep_assert_held(&dev_replace->rwsem);
6136 /* Unlock and let waiting writers proceed */
6137 up_read(&dev_replace->rwsem);
6139 free_extent_map(em);
6140 return ret;
6143 int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
6144 u64 logical, u64 *length,
6145 struct btrfs_bio **bbio_ret, int mirror_num)
6147 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
6148 mirror_num, 0);
6151 /* For Scrub/replace */
6152 int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
6153 u64 logical, u64 *length,
6154 struct btrfs_bio **bbio_ret)
6156 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
6159 int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
6160 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
6162 struct extent_map *em;
6163 struct map_lookup *map;
6164 u64 *buf;
6165 u64 bytenr;
6166 u64 length;
6167 u64 stripe_nr;
6168 u64 rmap_len;
6169 int i, j, nr = 0;
6171 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
6172 if (IS_ERR(em))
6173 return -EIO;
6175 map = em->map_lookup;
6176 length = em->len;
6177 rmap_len = map->stripe_len;
6179 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
6180 length = div_u64(length, map->num_stripes / map->sub_stripes);
6181 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
6182 length = div_u64(length, map->num_stripes);
6183 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
6184 length = div_u64(length, nr_data_stripes(map));
6185 rmap_len = map->stripe_len * nr_data_stripes(map);
6188 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
6189 BUG_ON(!buf); /* -ENOMEM */
6191 for (i = 0; i < map->num_stripes; i++) {
6192 if (map->stripes[i].physical > physical ||
6193 map->stripes[i].physical + length <= physical)
6194 continue;
6196 stripe_nr = physical - map->stripes[i].physical;
6197 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
6199 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6200 stripe_nr = stripe_nr * map->num_stripes + i;
6201 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
6202 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6203 stripe_nr = stripe_nr * map->num_stripes + i;
6204 } /* else if RAID[56], multiply by nr_data_stripes().
6205 * Alternatively, just use rmap_len below instead of
6206 * map->stripe_len */
6208 bytenr = chunk_start + stripe_nr * rmap_len;
6209 WARN_ON(nr >= map->num_stripes);
6210 for (j = 0; j < nr; j++) {
6211 if (buf[j] == bytenr)
6212 break;
6214 if (j == nr) {
6215 WARN_ON(nr >= map->num_stripes);
6216 buf[nr++] = bytenr;
6220 *logical = buf;
6221 *naddrs = nr;
6222 *stripe_len = rmap_len;
6224 free_extent_map(em);
6225 return 0;
6228 static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
6230 bio->bi_private = bbio->private;
6231 bio->bi_end_io = bbio->end_io;
6232 bio_endio(bio);
6234 btrfs_put_bbio(bbio);
6237 static void btrfs_end_bio(struct bio *bio)
6239 struct btrfs_bio *bbio = bio->bi_private;
6240 int is_orig_bio = 0;
6242 if (bio->bi_status) {
6243 atomic_inc(&bbio->error);
6244 if (bio->bi_status == BLK_STS_IOERR ||
6245 bio->bi_status == BLK_STS_TARGET) {
6246 unsigned int stripe_index =
6247 btrfs_io_bio(bio)->stripe_index;
6248 struct btrfs_device *dev;
6250 BUG_ON(stripe_index >= bbio->num_stripes);
6251 dev = bbio->stripes[stripe_index].dev;
6252 if (dev->bdev) {
6253 if (bio_op(bio) == REQ_OP_WRITE)
6254 btrfs_dev_stat_inc_and_print(dev,
6255 BTRFS_DEV_STAT_WRITE_ERRS);
6256 else if (!(bio->bi_opf & REQ_RAHEAD))
6257 btrfs_dev_stat_inc_and_print(dev,
6258 BTRFS_DEV_STAT_READ_ERRS);
6259 if (bio->bi_opf & REQ_PREFLUSH)
6260 btrfs_dev_stat_inc_and_print(dev,
6261 BTRFS_DEV_STAT_FLUSH_ERRS);
6266 if (bio == bbio->orig_bio)
6267 is_orig_bio = 1;
6269 btrfs_bio_counter_dec(bbio->fs_info);
6271 if (atomic_dec_and_test(&bbio->stripes_pending)) {
6272 if (!is_orig_bio) {
6273 bio_put(bio);
6274 bio = bbio->orig_bio;
6277 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
6278 /* only send an error to the higher layers if it is
6279 * beyond the tolerance of the btrfs bio
6281 if (atomic_read(&bbio->error) > bbio->max_errors) {
6282 bio->bi_status = BLK_STS_IOERR;
6283 } else {
6285 * this bio is actually up to date, we didn't
6286 * go over the max number of errors
6288 bio->bi_status = BLK_STS_OK;
6291 btrfs_end_bbio(bbio, bio);
6292 } else if (!is_orig_bio) {
6293 bio_put(bio);
6297 static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6298 u64 physical, int dev_nr)
6300 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
6301 struct btrfs_fs_info *fs_info = bbio->fs_info;
6303 bio->bi_private = bbio;
6304 btrfs_io_bio(bio)->stripe_index = dev_nr;
6305 bio->bi_end_io = btrfs_end_bio;
6306 bio->bi_iter.bi_sector = physical >> 9;
6307 btrfs_debug_in_rcu(fs_info,
6308 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6309 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6310 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6311 bio->bi_iter.bi_size);
6312 bio_set_dev(bio, dev->bdev);
6314 btrfs_bio_counter_inc_noblocked(fs_info);
6316 btrfsic_submit_bio(bio);
6319 static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6321 atomic_inc(&bbio->error);
6322 if (atomic_dec_and_test(&bbio->stripes_pending)) {
6323 /* Should be the original bio. */
6324 WARN_ON(bio != bbio->orig_bio);
6326 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
6327 bio->bi_iter.bi_sector = logical >> 9;
6328 if (atomic_read(&bbio->error) > bbio->max_errors)
6329 bio->bi_status = BLK_STS_IOERR;
6330 else
6331 bio->bi_status = BLK_STS_OK;
6332 btrfs_end_bbio(bbio, bio);
6336 blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6337 int mirror_num)
6339 struct btrfs_device *dev;
6340 struct bio *first_bio = bio;
6341 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
6342 u64 length = 0;
6343 u64 map_length;
6344 int ret;
6345 int dev_nr;
6346 int total_devs;
6347 struct btrfs_bio *bbio = NULL;
6349 length = bio->bi_iter.bi_size;
6350 map_length = length;
6352 btrfs_bio_counter_inc_blocked(fs_info);
6353 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
6354 &map_length, &bbio, mirror_num, 1);
6355 if (ret) {
6356 btrfs_bio_counter_dec(fs_info);
6357 return errno_to_blk_status(ret);
6360 total_devs = bbio->num_stripes;
6361 bbio->orig_bio = first_bio;
6362 bbio->private = first_bio->bi_private;
6363 bbio->end_io = first_bio->bi_end_io;
6364 bbio->fs_info = fs_info;
6365 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6367 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
6368 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
6369 /* In this case, map_length has been set to the length of
6370 a single stripe; not the whole write */
6371 if (bio_op(bio) == REQ_OP_WRITE) {
6372 ret = raid56_parity_write(fs_info, bio, bbio,
6373 map_length);
6374 } else {
6375 ret = raid56_parity_recover(fs_info, bio, bbio,
6376 map_length, mirror_num, 1);
6379 btrfs_bio_counter_dec(fs_info);
6380 return errno_to_blk_status(ret);
6383 if (map_length < length) {
6384 btrfs_crit(fs_info,
6385 "mapping failed logical %llu bio len %llu len %llu",
6386 logical, length, map_length);
6387 BUG();
6390 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
6391 dev = bbio->stripes[dev_nr].dev;
6392 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6393 &dev->dev_state) ||
6394 (bio_op(first_bio) == REQ_OP_WRITE &&
6395 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
6396 bbio_error(bbio, first_bio, logical);
6397 continue;
6400 if (dev_nr < total_devs - 1)
6401 bio = btrfs_bio_clone(first_bio);
6402 else
6403 bio = first_bio;
6405 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6406 dev_nr);
6408 btrfs_bio_counter_dec(fs_info);
6409 return BLK_STS_OK;
6413 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6414 * return NULL.
6416 * If devid and uuid are both specified, the match must be exact, otherwise
6417 * only devid is used.
6419 * If @seed is true, traverse through the seed devices.
6421 struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
6422 u64 devid, u8 *uuid, u8 *fsid,
6423 bool seed)
6425 struct btrfs_device *device;
6427 while (fs_devices) {
6428 if (!fsid ||
6429 !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6430 list_for_each_entry(device, &fs_devices->devices,
6431 dev_list) {
6432 if (device->devid == devid &&
6433 (!uuid || memcmp(device->uuid, uuid,
6434 BTRFS_UUID_SIZE) == 0))
6435 return device;
6438 if (seed)
6439 fs_devices = fs_devices->seed;
6440 else
6441 return NULL;
6443 return NULL;
6446 static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
6447 u64 devid, u8 *dev_uuid)
6449 struct btrfs_device *device;
6451 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6452 if (IS_ERR(device))
6453 return device;
6455 list_add(&device->dev_list, &fs_devices->devices);
6456 device->fs_devices = fs_devices;
6457 fs_devices->num_devices++;
6459 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
6460 fs_devices->missing_devices++;
6462 return device;
6466 * btrfs_alloc_device - allocate struct btrfs_device
6467 * @fs_info: used only for generating a new devid, can be NULL if
6468 * devid is provided (i.e. @devid != NULL).
6469 * @devid: a pointer to devid for this device. If NULL a new devid
6470 * is generated.
6471 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6472 * is generated.
6474 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6475 * on error. Returned struct is not linked onto any lists and must be
6476 * destroyed with btrfs_free_device.
6478 struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6479 const u64 *devid,
6480 const u8 *uuid)
6482 struct btrfs_device *dev;
6483 u64 tmp;
6485 if (WARN_ON(!devid && !fs_info))
6486 return ERR_PTR(-EINVAL);
6488 dev = __alloc_device();
6489 if (IS_ERR(dev))
6490 return dev;
6492 if (devid)
6493 tmp = *devid;
6494 else {
6495 int ret;
6497 ret = find_next_devid(fs_info, &tmp);
6498 if (ret) {
6499 btrfs_free_device(dev);
6500 return ERR_PTR(ret);
6503 dev->devid = tmp;
6505 if (uuid)
6506 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6507 else
6508 generate_random_uuid(dev->uuid);
6510 return dev;
6513 static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
6514 u64 devid, u8 *uuid, bool error)
6516 if (error)
6517 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6518 devid, uuid);
6519 else
6520 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6521 devid, uuid);
6524 static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
6526 int index = btrfs_bg_flags_to_raid_index(type);
6527 int ncopies = btrfs_raid_array[index].ncopies;
6528 int data_stripes;
6530 switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
6531 case BTRFS_BLOCK_GROUP_RAID5:
6532 data_stripes = num_stripes - 1;
6533 break;
6534 case BTRFS_BLOCK_GROUP_RAID6:
6535 data_stripes = num_stripes - 2;
6536 break;
6537 default:
6538 data_stripes = num_stripes / ncopies;
6539 break;
6541 return div_u64(chunk_len, data_stripes);
6544 static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
6545 struct btrfs_chunk *chunk)
6547 struct btrfs_fs_info *fs_info = leaf->fs_info;
6548 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
6549 struct map_lookup *map;
6550 struct extent_map *em;
6551 u64 logical;
6552 u64 length;
6553 u64 devid;
6554 u8 uuid[BTRFS_UUID_SIZE];
6555 int num_stripes;
6556 int ret;
6557 int i;
6559 logical = key->offset;
6560 length = btrfs_chunk_length(leaf, chunk);
6561 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6564 * Only need to verify chunk item if we're reading from sys chunk array,
6565 * as chunk item in tree block is already verified by tree-checker.
6567 if (leaf->start == BTRFS_SUPER_INFO_OFFSET) {
6568 ret = btrfs_check_chunk_valid(leaf, chunk, logical);
6569 if (ret)
6570 return ret;
6573 read_lock(&map_tree->lock);
6574 em = lookup_extent_mapping(map_tree, logical, 1);
6575 read_unlock(&map_tree->lock);
6577 /* already mapped? */
6578 if (em && em->start <= logical && em->start + em->len > logical) {
6579 free_extent_map(em);
6580 return 0;
6581 } else if (em) {
6582 free_extent_map(em);
6585 em = alloc_extent_map();
6586 if (!em)
6587 return -ENOMEM;
6588 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
6589 if (!map) {
6590 free_extent_map(em);
6591 return -ENOMEM;
6594 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
6595 em->map_lookup = map;
6596 em->start = logical;
6597 em->len = length;
6598 em->orig_start = 0;
6599 em->block_start = 0;
6600 em->block_len = em->len;
6602 map->num_stripes = num_stripes;
6603 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6604 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6605 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6606 map->type = btrfs_chunk_type(leaf, chunk);
6607 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6608 map->verified_stripes = 0;
6609 em->orig_block_len = calc_stripe_length(map->type, em->len,
6610 map->num_stripes);
6611 for (i = 0; i < num_stripes; i++) {
6612 map->stripes[i].physical =
6613 btrfs_stripe_offset_nr(leaf, chunk, i);
6614 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
6615 read_extent_buffer(leaf, uuid, (unsigned long)
6616 btrfs_stripe_dev_uuid_nr(chunk, i),
6617 BTRFS_UUID_SIZE);
6618 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
6619 devid, uuid, NULL, true);
6620 if (!map->stripes[i].dev &&
6621 !btrfs_test_opt(fs_info, DEGRADED)) {
6622 free_extent_map(em);
6623 btrfs_report_missing_device(fs_info, devid, uuid, true);
6624 return -ENOENT;
6626 if (!map->stripes[i].dev) {
6627 map->stripes[i].dev =
6628 add_missing_dev(fs_info->fs_devices, devid,
6629 uuid);
6630 if (IS_ERR(map->stripes[i].dev)) {
6631 free_extent_map(em);
6632 btrfs_err(fs_info,
6633 "failed to init missing dev %llu: %ld",
6634 devid, PTR_ERR(map->stripes[i].dev));
6635 return PTR_ERR(map->stripes[i].dev);
6637 btrfs_report_missing_device(fs_info, devid, uuid, false);
6639 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6640 &(map->stripes[i].dev->dev_state));
6644 write_lock(&map_tree->lock);
6645 ret = add_extent_mapping(map_tree, em, 0);
6646 write_unlock(&map_tree->lock);
6647 if (ret < 0) {
6648 btrfs_err(fs_info,
6649 "failed to add chunk map, start=%llu len=%llu: %d",
6650 em->start, em->len, ret);
6652 free_extent_map(em);
6654 return ret;
6657 static void fill_device_from_item(struct extent_buffer *leaf,
6658 struct btrfs_dev_item *dev_item,
6659 struct btrfs_device *device)
6661 unsigned long ptr;
6663 device->devid = btrfs_device_id(leaf, dev_item);
6664 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6665 device->total_bytes = device->disk_total_bytes;
6666 device->commit_total_bytes = device->disk_total_bytes;
6667 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
6668 device->commit_bytes_used = device->bytes_used;
6669 device->type = btrfs_device_type(leaf, dev_item);
6670 device->io_align = btrfs_device_io_align(leaf, dev_item);
6671 device->io_width = btrfs_device_io_width(leaf, dev_item);
6672 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
6673 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
6674 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
6676 ptr = btrfs_device_uuid(dev_item);
6677 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
6680 static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
6681 u8 *fsid)
6683 struct btrfs_fs_devices *fs_devices;
6684 int ret;
6686 lockdep_assert_held(&uuid_mutex);
6687 ASSERT(fsid);
6689 fs_devices = fs_info->fs_devices->seed;
6690 while (fs_devices) {
6691 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
6692 return fs_devices;
6694 fs_devices = fs_devices->seed;
6697 fs_devices = find_fsid(fsid, NULL);
6698 if (!fs_devices) {
6699 if (!btrfs_test_opt(fs_info, DEGRADED))
6700 return ERR_PTR(-ENOENT);
6702 fs_devices = alloc_fs_devices(fsid, NULL);
6703 if (IS_ERR(fs_devices))
6704 return fs_devices;
6706 fs_devices->seeding = true;
6707 fs_devices->opened = 1;
6708 return fs_devices;
6711 fs_devices = clone_fs_devices(fs_devices);
6712 if (IS_ERR(fs_devices))
6713 return fs_devices;
6715 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
6716 if (ret) {
6717 free_fs_devices(fs_devices);
6718 fs_devices = ERR_PTR(ret);
6719 goto out;
6722 if (!fs_devices->seeding) {
6723 close_fs_devices(fs_devices);
6724 free_fs_devices(fs_devices);
6725 fs_devices = ERR_PTR(-EINVAL);
6726 goto out;
6729 fs_devices->seed = fs_info->fs_devices->seed;
6730 fs_info->fs_devices->seed = fs_devices;
6731 out:
6732 return fs_devices;
6735 static int read_one_dev(struct extent_buffer *leaf,
6736 struct btrfs_dev_item *dev_item)
6738 struct btrfs_fs_info *fs_info = leaf->fs_info;
6739 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6740 struct btrfs_device *device;
6741 u64 devid;
6742 int ret;
6743 u8 fs_uuid[BTRFS_FSID_SIZE];
6744 u8 dev_uuid[BTRFS_UUID_SIZE];
6746 devid = btrfs_device_id(leaf, dev_item);
6747 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
6748 BTRFS_UUID_SIZE);
6749 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
6750 BTRFS_FSID_SIZE);
6752 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
6753 fs_devices = open_seed_devices(fs_info, fs_uuid);
6754 if (IS_ERR(fs_devices))
6755 return PTR_ERR(fs_devices);
6758 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
6759 fs_uuid, true);
6760 if (!device) {
6761 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6762 btrfs_report_missing_device(fs_info, devid,
6763 dev_uuid, true);
6764 return -ENOENT;
6767 device = add_missing_dev(fs_devices, devid, dev_uuid);
6768 if (IS_ERR(device)) {
6769 btrfs_err(fs_info,
6770 "failed to add missing dev %llu: %ld",
6771 devid, PTR_ERR(device));
6772 return PTR_ERR(device);
6774 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
6775 } else {
6776 if (!device->bdev) {
6777 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6778 btrfs_report_missing_device(fs_info,
6779 devid, dev_uuid, true);
6780 return -ENOENT;
6782 btrfs_report_missing_device(fs_info, devid,
6783 dev_uuid, false);
6786 if (!device->bdev &&
6787 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
6789 * this happens when a device that was properly setup
6790 * in the device info lists suddenly goes bad.
6791 * device->bdev is NULL, and so we have to set
6792 * device->missing to one here
6794 device->fs_devices->missing_devices++;
6795 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
6798 /* Move the device to its own fs_devices */
6799 if (device->fs_devices != fs_devices) {
6800 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6801 &device->dev_state));
6803 list_move(&device->dev_list, &fs_devices->devices);
6804 device->fs_devices->num_devices--;
6805 fs_devices->num_devices++;
6807 device->fs_devices->missing_devices--;
6808 fs_devices->missing_devices++;
6810 device->fs_devices = fs_devices;
6814 if (device->fs_devices != fs_info->fs_devices) {
6815 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
6816 if (device->generation !=
6817 btrfs_device_generation(leaf, dev_item))
6818 return -EINVAL;
6821 fill_device_from_item(leaf, dev_item, device);
6822 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
6823 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
6824 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
6825 device->fs_devices->total_rw_bytes += device->total_bytes;
6826 atomic64_add(device->total_bytes - device->bytes_used,
6827 &fs_info->free_chunk_space);
6829 ret = 0;
6830 return ret;
6833 int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
6835 struct btrfs_root *root = fs_info->tree_root;
6836 struct btrfs_super_block *super_copy = fs_info->super_copy;
6837 struct extent_buffer *sb;
6838 struct btrfs_disk_key *disk_key;
6839 struct btrfs_chunk *chunk;
6840 u8 *array_ptr;
6841 unsigned long sb_array_offset;
6842 int ret = 0;
6843 u32 num_stripes;
6844 u32 array_size;
6845 u32 len = 0;
6846 u32 cur_offset;
6847 u64 type;
6848 struct btrfs_key key;
6850 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
6852 * This will create extent buffer of nodesize, superblock size is
6853 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6854 * overallocate but we can keep it as-is, only the first page is used.
6856 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
6857 if (IS_ERR(sb))
6858 return PTR_ERR(sb);
6859 set_extent_buffer_uptodate(sb);
6860 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
6862 * The sb extent buffer is artificial and just used to read the system array.
6863 * set_extent_buffer_uptodate() call does not properly mark all it's
6864 * pages up-to-date when the page is larger: extent does not cover the
6865 * whole page and consequently check_page_uptodate does not find all
6866 * the page's extents up-to-date (the hole beyond sb),
6867 * write_extent_buffer then triggers a WARN_ON.
6869 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6870 * but sb spans only this function. Add an explicit SetPageUptodate call
6871 * to silence the warning eg. on PowerPC 64.
6873 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
6874 SetPageUptodate(sb->pages[0]);
6876 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
6877 array_size = btrfs_super_sys_array_size(super_copy);
6879 array_ptr = super_copy->sys_chunk_array;
6880 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6881 cur_offset = 0;
6883 while (cur_offset < array_size) {
6884 disk_key = (struct btrfs_disk_key *)array_ptr;
6885 len = sizeof(*disk_key);
6886 if (cur_offset + len > array_size)
6887 goto out_short_read;
6889 btrfs_disk_key_to_cpu(&key, disk_key);
6891 array_ptr += len;
6892 sb_array_offset += len;
6893 cur_offset += len;
6895 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
6896 btrfs_err(fs_info,
6897 "unexpected item type %u in sys_array at offset %u",
6898 (u32)key.type, cur_offset);
6899 ret = -EIO;
6900 break;
6903 chunk = (struct btrfs_chunk *)sb_array_offset;
6905 * At least one btrfs_chunk with one stripe must be present,
6906 * exact stripe count check comes afterwards
6908 len = btrfs_chunk_item_size(1);
6909 if (cur_offset + len > array_size)
6910 goto out_short_read;
6912 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6913 if (!num_stripes) {
6914 btrfs_err(fs_info,
6915 "invalid number of stripes %u in sys_array at offset %u",
6916 num_stripes, cur_offset);
6917 ret = -EIO;
6918 break;
6921 type = btrfs_chunk_type(sb, chunk);
6922 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
6923 btrfs_err(fs_info,
6924 "invalid chunk type %llu in sys_array at offset %u",
6925 type, cur_offset);
6926 ret = -EIO;
6927 break;
6930 len = btrfs_chunk_item_size(num_stripes);
6931 if (cur_offset + len > array_size)
6932 goto out_short_read;
6934 ret = read_one_chunk(&key, sb, chunk);
6935 if (ret)
6936 break;
6938 array_ptr += len;
6939 sb_array_offset += len;
6940 cur_offset += len;
6942 clear_extent_buffer_uptodate(sb);
6943 free_extent_buffer_stale(sb);
6944 return ret;
6946 out_short_read:
6947 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
6948 len, cur_offset);
6949 clear_extent_buffer_uptodate(sb);
6950 free_extent_buffer_stale(sb);
6951 return -EIO;
6955 * Check if all chunks in the fs are OK for read-write degraded mount
6957 * If the @failing_dev is specified, it's accounted as missing.
6959 * Return true if all chunks meet the minimal RW mount requirements.
6960 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6962 bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6963 struct btrfs_device *failing_dev)
6965 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
6966 struct extent_map *em;
6967 u64 next_start = 0;
6968 bool ret = true;
6970 read_lock(&map_tree->lock);
6971 em = lookup_extent_mapping(map_tree, 0, (u64)-1);
6972 read_unlock(&map_tree->lock);
6973 /* No chunk at all? Return false anyway */
6974 if (!em) {
6975 ret = false;
6976 goto out;
6978 while (em) {
6979 struct map_lookup *map;
6980 int missing = 0;
6981 int max_tolerated;
6982 int i;
6984 map = em->map_lookup;
6985 max_tolerated =
6986 btrfs_get_num_tolerated_disk_barrier_failures(
6987 map->type);
6988 for (i = 0; i < map->num_stripes; i++) {
6989 struct btrfs_device *dev = map->stripes[i].dev;
6991 if (!dev || !dev->bdev ||
6992 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
6993 dev->last_flush_error)
6994 missing++;
6995 else if (failing_dev && failing_dev == dev)
6996 missing++;
6998 if (missing > max_tolerated) {
6999 if (!failing_dev)
7000 btrfs_warn(fs_info,
7001 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
7002 em->start, missing, max_tolerated);
7003 free_extent_map(em);
7004 ret = false;
7005 goto out;
7007 next_start = extent_map_end(em);
7008 free_extent_map(em);
7010 read_lock(&map_tree->lock);
7011 em = lookup_extent_mapping(map_tree, next_start,
7012 (u64)(-1) - next_start);
7013 read_unlock(&map_tree->lock);
7015 out:
7016 return ret;
7019 int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
7021 struct btrfs_root *root = fs_info->chunk_root;
7022 struct btrfs_path *path;
7023 struct extent_buffer *leaf;
7024 struct btrfs_key key;
7025 struct btrfs_key found_key;
7026 int ret;
7027 int slot;
7028 u64 total_dev = 0;
7030 path = btrfs_alloc_path();
7031 if (!path)
7032 return -ENOMEM;
7035 * uuid_mutex is needed only if we are mounting a sprout FS
7036 * otherwise we don't need it.
7038 mutex_lock(&uuid_mutex);
7039 mutex_lock(&fs_info->chunk_mutex);
7042 * Read all device items, and then all the chunk items. All
7043 * device items are found before any chunk item (their object id
7044 * is smaller than the lowest possible object id for a chunk
7045 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
7047 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7048 key.offset = 0;
7049 key.type = 0;
7050 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7051 if (ret < 0)
7052 goto error;
7053 while (1) {
7054 leaf = path->nodes[0];
7055 slot = path->slots[0];
7056 if (slot >= btrfs_header_nritems(leaf)) {
7057 ret = btrfs_next_leaf(root, path);
7058 if (ret == 0)
7059 continue;
7060 if (ret < 0)
7061 goto error;
7062 break;
7064 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7065 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7066 struct btrfs_dev_item *dev_item;
7067 dev_item = btrfs_item_ptr(leaf, slot,
7068 struct btrfs_dev_item);
7069 ret = read_one_dev(leaf, dev_item);
7070 if (ret)
7071 goto error;
7072 total_dev++;
7073 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7074 struct btrfs_chunk *chunk;
7075 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
7076 ret = read_one_chunk(&found_key, leaf, chunk);
7077 if (ret)
7078 goto error;
7080 path->slots[0]++;
7084 * After loading chunk tree, we've got all device information,
7085 * do another round of validation checks.
7087 if (total_dev != fs_info->fs_devices->total_devices) {
7088 btrfs_err(fs_info,
7089 "super_num_devices %llu mismatch with num_devices %llu found here",
7090 btrfs_super_num_devices(fs_info->super_copy),
7091 total_dev);
7092 ret = -EINVAL;
7093 goto error;
7095 if (btrfs_super_total_bytes(fs_info->super_copy) <
7096 fs_info->fs_devices->total_rw_bytes) {
7097 btrfs_err(fs_info,
7098 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
7099 btrfs_super_total_bytes(fs_info->super_copy),
7100 fs_info->fs_devices->total_rw_bytes);
7101 ret = -EINVAL;
7102 goto error;
7104 ret = 0;
7105 error:
7106 mutex_unlock(&fs_info->chunk_mutex);
7107 mutex_unlock(&uuid_mutex);
7109 btrfs_free_path(path);
7110 return ret;
7113 void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7115 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7116 struct btrfs_device *device;
7118 while (fs_devices) {
7119 mutex_lock(&fs_devices->device_list_mutex);
7120 list_for_each_entry(device, &fs_devices->devices, dev_list)
7121 device->fs_info = fs_info;
7122 mutex_unlock(&fs_devices->device_list_mutex);
7124 fs_devices = fs_devices->seed;
7128 static u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
7129 const struct btrfs_dev_stats_item *ptr,
7130 int index)
7132 u64 val;
7134 read_extent_buffer(eb, &val,
7135 offsetof(struct btrfs_dev_stats_item, values) +
7136 ((unsigned long)ptr) + (index * sizeof(u64)),
7137 sizeof(val));
7138 return val;
7141 static void btrfs_set_dev_stats_value(struct extent_buffer *eb,
7142 struct btrfs_dev_stats_item *ptr,
7143 int index, u64 val)
7145 write_extent_buffer(eb, &val,
7146 offsetof(struct btrfs_dev_stats_item, values) +
7147 ((unsigned long)ptr) + (index * sizeof(u64)),
7148 sizeof(val));
7151 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7153 struct btrfs_key key;
7154 struct btrfs_root *dev_root = fs_info->dev_root;
7155 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7156 struct extent_buffer *eb;
7157 int slot;
7158 int ret = 0;
7159 struct btrfs_device *device;
7160 struct btrfs_path *path = NULL;
7161 int i;
7163 path = btrfs_alloc_path();
7164 if (!path)
7165 return -ENOMEM;
7167 mutex_lock(&fs_devices->device_list_mutex);
7168 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7169 int item_size;
7170 struct btrfs_dev_stats_item *ptr;
7172 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7173 key.type = BTRFS_PERSISTENT_ITEM_KEY;
7174 key.offset = device->devid;
7175 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7176 if (ret) {
7177 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7178 btrfs_dev_stat_set(device, i, 0);
7179 device->dev_stats_valid = 1;
7180 btrfs_release_path(path);
7181 continue;
7183 slot = path->slots[0];
7184 eb = path->nodes[0];
7185 item_size = btrfs_item_size_nr(eb, slot);
7187 ptr = btrfs_item_ptr(eb, slot,
7188 struct btrfs_dev_stats_item);
7190 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7191 if (item_size >= (1 + i) * sizeof(__le64))
7192 btrfs_dev_stat_set(device, i,
7193 btrfs_dev_stats_value(eb, ptr, i));
7194 else
7195 btrfs_dev_stat_set(device, i, 0);
7198 device->dev_stats_valid = 1;
7199 btrfs_dev_stat_print_on_load(device);
7200 btrfs_release_path(path);
7202 mutex_unlock(&fs_devices->device_list_mutex);
7204 btrfs_free_path(path);
7205 return ret < 0 ? ret : 0;
7208 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
7209 struct btrfs_device *device)
7211 struct btrfs_fs_info *fs_info = trans->fs_info;
7212 struct btrfs_root *dev_root = fs_info->dev_root;
7213 struct btrfs_path *path;
7214 struct btrfs_key key;
7215 struct extent_buffer *eb;
7216 struct btrfs_dev_stats_item *ptr;
7217 int ret;
7218 int i;
7220 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7221 key.type = BTRFS_PERSISTENT_ITEM_KEY;
7222 key.offset = device->devid;
7224 path = btrfs_alloc_path();
7225 if (!path)
7226 return -ENOMEM;
7227 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7228 if (ret < 0) {
7229 btrfs_warn_in_rcu(fs_info,
7230 "error %d while searching for dev_stats item for device %s",
7231 ret, rcu_str_deref(device->name));
7232 goto out;
7235 if (ret == 0 &&
7236 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7237 /* need to delete old one and insert a new one */
7238 ret = btrfs_del_item(trans, dev_root, path);
7239 if (ret != 0) {
7240 btrfs_warn_in_rcu(fs_info,
7241 "delete too small dev_stats item for device %s failed %d",
7242 rcu_str_deref(device->name), ret);
7243 goto out;
7245 ret = 1;
7248 if (ret == 1) {
7249 /* need to insert a new item */
7250 btrfs_release_path(path);
7251 ret = btrfs_insert_empty_item(trans, dev_root, path,
7252 &key, sizeof(*ptr));
7253 if (ret < 0) {
7254 btrfs_warn_in_rcu(fs_info,
7255 "insert dev_stats item for device %s failed %d",
7256 rcu_str_deref(device->name), ret);
7257 goto out;
7261 eb = path->nodes[0];
7262 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7263 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7264 btrfs_set_dev_stats_value(eb, ptr, i,
7265 btrfs_dev_stat_read(device, i));
7266 btrfs_mark_buffer_dirty(eb);
7268 out:
7269 btrfs_free_path(path);
7270 return ret;
7274 * called from commit_transaction. Writes all changed device stats to disk.
7276 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
7278 struct btrfs_fs_info *fs_info = trans->fs_info;
7279 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7280 struct btrfs_device *device;
7281 int stats_cnt;
7282 int ret = 0;
7284 mutex_lock(&fs_devices->device_list_mutex);
7285 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7286 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7287 if (!device->dev_stats_valid || stats_cnt == 0)
7288 continue;
7292 * There is a LOAD-LOAD control dependency between the value of
7293 * dev_stats_ccnt and updating the on-disk values which requires
7294 * reading the in-memory counters. Such control dependencies
7295 * require explicit read memory barriers.
7297 * This memory barriers pairs with smp_mb__before_atomic in
7298 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7299 * barrier implied by atomic_xchg in
7300 * btrfs_dev_stats_read_and_reset
7302 smp_rmb();
7304 ret = update_dev_stat_item(trans, device);
7305 if (!ret)
7306 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
7308 mutex_unlock(&fs_devices->device_list_mutex);
7310 return ret;
7313 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7315 btrfs_dev_stat_inc(dev, index);
7316 btrfs_dev_stat_print_on_error(dev);
7319 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
7321 if (!dev->dev_stats_valid)
7322 return;
7323 btrfs_err_rl_in_rcu(dev->fs_info,
7324 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7325 rcu_str_deref(dev->name),
7326 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7327 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7328 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7329 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7330 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7333 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7335 int i;
7337 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7338 if (btrfs_dev_stat_read(dev, i) != 0)
7339 break;
7340 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7341 return; /* all values == 0, suppress message */
7343 btrfs_info_in_rcu(dev->fs_info,
7344 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7345 rcu_str_deref(dev->name),
7346 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7347 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7348 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7349 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7350 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7353 int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
7354 struct btrfs_ioctl_get_dev_stats *stats)
7356 struct btrfs_device *dev;
7357 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7358 int i;
7360 mutex_lock(&fs_devices->device_list_mutex);
7361 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7362 true);
7363 mutex_unlock(&fs_devices->device_list_mutex);
7365 if (!dev) {
7366 btrfs_warn(fs_info, "get dev_stats failed, device not found");
7367 return -ENODEV;
7368 } else if (!dev->dev_stats_valid) {
7369 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
7370 return -ENODEV;
7371 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
7372 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7373 if (stats->nr_items > i)
7374 stats->values[i] =
7375 btrfs_dev_stat_read_and_reset(dev, i);
7376 else
7377 btrfs_dev_stat_set(dev, i, 0);
7379 btrfs_info(fs_info, "device stats zeroed by %s (%d)",
7380 current->comm, task_pid_nr(current));
7381 } else {
7382 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7383 if (stats->nr_items > i)
7384 stats->values[i] = btrfs_dev_stat_read(dev, i);
7386 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7387 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7388 return 0;
7391 void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
7393 struct buffer_head *bh;
7394 struct btrfs_super_block *disk_super;
7395 int copy_num;
7397 if (!bdev)
7398 return;
7400 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7401 copy_num++) {
7403 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7404 continue;
7406 disk_super = (struct btrfs_super_block *)bh->b_data;
7408 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7409 set_buffer_dirty(bh);
7410 sync_dirty_buffer(bh);
7411 brelse(bh);
7414 /* Notify udev that device has changed */
7415 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7417 /* Update ctime/mtime for device path for libblkid */
7418 update_dev_time(device_path);
7422 * Update the size and bytes used for each device where it changed. This is
7423 * delayed since we would otherwise get errors while writing out the
7424 * superblocks.
7426 * Must be invoked during transaction commit.
7428 void btrfs_commit_device_sizes(struct btrfs_transaction *trans)
7430 struct btrfs_device *curr, *next;
7432 ASSERT(trans->state == TRANS_STATE_COMMIT_DOING);
7434 if (list_empty(&trans->dev_update_list))
7435 return;
7438 * We don't need the device_list_mutex here. This list is owned by the
7439 * transaction and the transaction must complete before the device is
7440 * released.
7442 mutex_lock(&trans->fs_info->chunk_mutex);
7443 list_for_each_entry_safe(curr, next, &trans->dev_update_list,
7444 post_commit_list) {
7445 list_del_init(&curr->post_commit_list);
7446 curr->commit_total_bytes = curr->disk_total_bytes;
7447 curr->commit_bytes_used = curr->bytes_used;
7449 mutex_unlock(&trans->fs_info->chunk_mutex);
7452 void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7454 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7455 while (fs_devices) {
7456 fs_devices->fs_info = fs_info;
7457 fs_devices = fs_devices->seed;
7461 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7463 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7464 while (fs_devices) {
7465 fs_devices->fs_info = NULL;
7466 fs_devices = fs_devices->seed;
7471 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7473 int btrfs_bg_type_to_factor(u64 flags)
7475 const int index = btrfs_bg_flags_to_raid_index(flags);
7477 return btrfs_raid_array[index].ncopies;
7482 static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7483 u64 chunk_offset, u64 devid,
7484 u64 physical_offset, u64 physical_len)
7486 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
7487 struct extent_map *em;
7488 struct map_lookup *map;
7489 struct btrfs_device *dev;
7490 u64 stripe_len;
7491 bool found = false;
7492 int ret = 0;
7493 int i;
7495 read_lock(&em_tree->lock);
7496 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7497 read_unlock(&em_tree->lock);
7499 if (!em) {
7500 btrfs_err(fs_info,
7501 "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7502 physical_offset, devid);
7503 ret = -EUCLEAN;
7504 goto out;
7507 map = em->map_lookup;
7508 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7509 if (physical_len != stripe_len) {
7510 btrfs_err(fs_info,
7511 "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7512 physical_offset, devid, em->start, physical_len,
7513 stripe_len);
7514 ret = -EUCLEAN;
7515 goto out;
7518 for (i = 0; i < map->num_stripes; i++) {
7519 if (map->stripes[i].dev->devid == devid &&
7520 map->stripes[i].physical == physical_offset) {
7521 found = true;
7522 if (map->verified_stripes >= map->num_stripes) {
7523 btrfs_err(fs_info,
7524 "too many dev extents for chunk %llu found",
7525 em->start);
7526 ret = -EUCLEAN;
7527 goto out;
7529 map->verified_stripes++;
7530 break;
7533 if (!found) {
7534 btrfs_err(fs_info,
7535 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7536 physical_offset, devid);
7537 ret = -EUCLEAN;
7540 /* Make sure no dev extent is beyond device bondary */
7541 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
7542 if (!dev) {
7543 btrfs_err(fs_info, "failed to find devid %llu", devid);
7544 ret = -EUCLEAN;
7545 goto out;
7548 /* It's possible this device is a dummy for seed device */
7549 if (dev->disk_total_bytes == 0) {
7550 dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
7551 NULL, false);
7552 if (!dev) {
7553 btrfs_err(fs_info, "failed to find seed devid %llu",
7554 devid);
7555 ret = -EUCLEAN;
7556 goto out;
7560 if (physical_offset + physical_len > dev->disk_total_bytes) {
7561 btrfs_err(fs_info,
7562 "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7563 devid, physical_offset, physical_len,
7564 dev->disk_total_bytes);
7565 ret = -EUCLEAN;
7566 goto out;
7568 out:
7569 free_extent_map(em);
7570 return ret;
7573 static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7575 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
7576 struct extent_map *em;
7577 struct rb_node *node;
7578 int ret = 0;
7580 read_lock(&em_tree->lock);
7581 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
7582 em = rb_entry(node, struct extent_map, rb_node);
7583 if (em->map_lookup->num_stripes !=
7584 em->map_lookup->verified_stripes) {
7585 btrfs_err(fs_info,
7586 "chunk %llu has missing dev extent, have %d expect %d",
7587 em->start, em->map_lookup->verified_stripes,
7588 em->map_lookup->num_stripes);
7589 ret = -EUCLEAN;
7590 goto out;
7593 out:
7594 read_unlock(&em_tree->lock);
7595 return ret;
7599 * Ensure that all dev extents are mapped to correct chunk, otherwise
7600 * later chunk allocation/free would cause unexpected behavior.
7602 * NOTE: This will iterate through the whole device tree, which should be of
7603 * the same size level as the chunk tree. This slightly increases mount time.
7605 int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7607 struct btrfs_path *path;
7608 struct btrfs_root *root = fs_info->dev_root;
7609 struct btrfs_key key;
7610 u64 prev_devid = 0;
7611 u64 prev_dev_ext_end = 0;
7612 int ret = 0;
7614 key.objectid = 1;
7615 key.type = BTRFS_DEV_EXTENT_KEY;
7616 key.offset = 0;
7618 path = btrfs_alloc_path();
7619 if (!path)
7620 return -ENOMEM;
7622 path->reada = READA_FORWARD;
7623 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7624 if (ret < 0)
7625 goto out;
7627 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7628 ret = btrfs_next_item(root, path);
7629 if (ret < 0)
7630 goto out;
7631 /* No dev extents at all? Not good */
7632 if (ret > 0) {
7633 ret = -EUCLEAN;
7634 goto out;
7637 while (1) {
7638 struct extent_buffer *leaf = path->nodes[0];
7639 struct btrfs_dev_extent *dext;
7640 int slot = path->slots[0];
7641 u64 chunk_offset;
7642 u64 physical_offset;
7643 u64 physical_len;
7644 u64 devid;
7646 btrfs_item_key_to_cpu(leaf, &key, slot);
7647 if (key.type != BTRFS_DEV_EXTENT_KEY)
7648 break;
7649 devid = key.objectid;
7650 physical_offset = key.offset;
7652 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7653 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7654 physical_len = btrfs_dev_extent_length(leaf, dext);
7656 /* Check if this dev extent overlaps with the previous one */
7657 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7658 btrfs_err(fs_info,
7659 "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7660 devid, physical_offset, prev_dev_ext_end);
7661 ret = -EUCLEAN;
7662 goto out;
7665 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7666 physical_offset, physical_len);
7667 if (ret < 0)
7668 goto out;
7669 prev_devid = devid;
7670 prev_dev_ext_end = physical_offset + physical_len;
7672 ret = btrfs_next_item(root, path);
7673 if (ret < 0)
7674 goto out;
7675 if (ret > 0) {
7676 ret = 0;
7677 break;
7681 /* Ensure all chunks have corresponding dev extents */
7682 ret = verify_chunk_dev_extent_mapping(fs_info);
7683 out:
7684 btrfs_free_path(path);
7685 return ret;
7689 * Check whether the given block group or device is pinned by any inode being
7690 * used as a swapfile.
7692 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7694 struct btrfs_swapfile_pin *sp;
7695 struct rb_node *node;
7697 spin_lock(&fs_info->swapfile_pins_lock);
7698 node = fs_info->swapfile_pins.rb_node;
7699 while (node) {
7700 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7701 if (ptr < sp->ptr)
7702 node = node->rb_left;
7703 else if (ptr > sp->ptr)
7704 node = node->rb_right;
7705 else
7706 break;
7708 spin_unlock(&fs_info->swapfile_pins_lock);
7709 return node != NULL;