Darrick J. Wong | a45c0ecc | 2019-07-15 08:50:57 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2018 Oracle. All Rights Reserved. |
| 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
| 5 | */ |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/compiler.h> |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/iomap.h> |
| 10 | #include <linux/swap.h> |
| 11 | |
Darrick J. Wong | a45c0ecc | 2019-07-15 08:50:57 -0700 | [diff] [blame] | 12 | /* Swapfile activation */ |
| 13 | |
| 14 | struct iomap_swapfile_info { |
| 15 | struct iomap iomap; /* accumulated iomap */ |
| 16 | struct swap_info_struct *sis; |
| 17 | uint64_t lowest_ppage; /* lowest physical addr seen (pages) */ |
| 18 | uint64_t highest_ppage; /* highest physical addr seen (pages) */ |
| 19 | unsigned long nr_pages; /* number of pages collected */ |
| 20 | int nr_extents; /* extent count */ |
| 21 | }; |
| 22 | |
| 23 | /* |
| 24 | * Collect physical extents for this swap file. Physical extents reported to |
| 25 | * the swap code must be trimmed to align to a page boundary. The logical |
| 26 | * offset within the file is irrelevant since the swapfile code maps logical |
| 27 | * page numbers of the swap device to the physical page-aligned extents. |
| 28 | */ |
| 29 | static int iomap_swapfile_add_extent(struct iomap_swapfile_info *isi) |
| 30 | { |
| 31 | struct iomap *iomap = &isi->iomap; |
| 32 | unsigned long nr_pages; |
| 33 | uint64_t first_ppage; |
| 34 | uint64_t first_ppage_reported; |
| 35 | uint64_t next_ppage; |
| 36 | int error; |
| 37 | |
| 38 | /* |
| 39 | * Round the start up and the end down so that the physical |
| 40 | * extent aligns to a page boundary. |
| 41 | */ |
| 42 | first_ppage = ALIGN(iomap->addr, PAGE_SIZE) >> PAGE_SHIFT; |
| 43 | next_ppage = ALIGN_DOWN(iomap->addr + iomap->length, PAGE_SIZE) >> |
| 44 | PAGE_SHIFT; |
| 45 | |
| 46 | /* Skip too-short physical extents. */ |
| 47 | if (first_ppage >= next_ppage) |
| 48 | return 0; |
| 49 | nr_pages = next_ppage - first_ppage; |
| 50 | |
| 51 | /* |
| 52 | * Calculate how much swap space we're adding; the first page contains |
| 53 | * the swap header and doesn't count. The mm still wants that first |
| 54 | * page fed to add_swap_extent, however. |
| 55 | */ |
| 56 | first_ppage_reported = first_ppage; |
| 57 | if (iomap->offset == 0) |
| 58 | first_ppage_reported++; |
| 59 | if (isi->lowest_ppage > first_ppage_reported) |
| 60 | isi->lowest_ppage = first_ppage_reported; |
| 61 | if (isi->highest_ppage < (next_ppage - 1)) |
| 62 | isi->highest_ppage = next_ppage - 1; |
| 63 | |
| 64 | /* Add extent, set up for the next call. */ |
| 65 | error = add_swap_extent(isi->sis, isi->nr_pages, nr_pages, first_ppage); |
| 66 | if (error < 0) |
| 67 | return error; |
| 68 | isi->nr_extents += error; |
| 69 | isi->nr_pages += nr_pages; |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Accumulate iomaps for this swap file. We have to accumulate iomaps because |
| 75 | * swap only cares about contiguous page-aligned physical extents and makes no |
| 76 | * distinction between written and unwritten extents. |
| 77 | */ |
| 78 | static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos, |
Goldwyn Rodrigues | c039b99 | 2019-10-18 16:44:10 -0700 | [diff] [blame] | 79 | loff_t count, void *data, struct iomap *iomap, |
| 80 | struct iomap *srcmap) |
Darrick J. Wong | a45c0ecc | 2019-07-15 08:50:57 -0700 | [diff] [blame] | 81 | { |
| 82 | struct iomap_swapfile_info *isi = data; |
| 83 | int error; |
| 84 | |
| 85 | switch (iomap->type) { |
| 86 | case IOMAP_MAPPED: |
| 87 | case IOMAP_UNWRITTEN: |
| 88 | /* Only real or unwritten extents. */ |
| 89 | break; |
| 90 | case IOMAP_INLINE: |
| 91 | /* No inline data. */ |
| 92 | pr_err("swapon: file is inline\n"); |
| 93 | return -EINVAL; |
| 94 | default: |
| 95 | pr_err("swapon: file has unallocated extents\n"); |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | |
| 99 | /* No uncommitted metadata or shared blocks. */ |
| 100 | if (iomap->flags & IOMAP_F_DIRTY) { |
| 101 | pr_err("swapon: file is not committed\n"); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | if (iomap->flags & IOMAP_F_SHARED) { |
| 105 | pr_err("swapon: file has shared extents\n"); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | |
| 109 | /* Only one bdev per swap file. */ |
| 110 | if (iomap->bdev != isi->sis->bdev) { |
| 111 | pr_err("swapon: file is on multiple devices\n"); |
| 112 | return -EINVAL; |
| 113 | } |
| 114 | |
| 115 | if (isi->iomap.length == 0) { |
| 116 | /* No accumulated extent, so just store it. */ |
| 117 | memcpy(&isi->iomap, iomap, sizeof(isi->iomap)); |
| 118 | } else if (isi->iomap.addr + isi->iomap.length == iomap->addr) { |
| 119 | /* Append this to the accumulated extent. */ |
| 120 | isi->iomap.length += iomap->length; |
| 121 | } else { |
| 122 | /* Otherwise, add the retained iomap and store this one. */ |
| 123 | error = iomap_swapfile_add_extent(isi); |
| 124 | if (error) |
| 125 | return error; |
| 126 | memcpy(&isi->iomap, iomap, sizeof(isi->iomap)); |
| 127 | } |
| 128 | return count; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Iterate a swap file's iomaps to construct physical extents that can be |
| 133 | * passed to the swapfile subsystem. |
| 134 | */ |
| 135 | int iomap_swapfile_activate(struct swap_info_struct *sis, |
| 136 | struct file *swap_file, sector_t *pagespan, |
| 137 | const struct iomap_ops *ops) |
| 138 | { |
| 139 | struct iomap_swapfile_info isi = { |
| 140 | .sis = sis, |
| 141 | .lowest_ppage = (sector_t)-1ULL, |
| 142 | }; |
| 143 | struct address_space *mapping = swap_file->f_mapping; |
| 144 | struct inode *inode = mapping->host; |
| 145 | loff_t pos = 0; |
| 146 | loff_t len = ALIGN_DOWN(i_size_read(inode), PAGE_SIZE); |
| 147 | loff_t ret; |
| 148 | |
| 149 | /* |
| 150 | * Persist all file mapping metadata so that we won't have any |
| 151 | * IOMAP_F_DIRTY iomaps. |
| 152 | */ |
| 153 | ret = vfs_fsync(swap_file, 1); |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | while (len > 0) { |
| 158 | ret = iomap_apply(inode, pos, len, IOMAP_REPORT, |
| 159 | ops, &isi, iomap_swapfile_activate_actor); |
| 160 | if (ret <= 0) |
| 161 | return ret; |
| 162 | |
| 163 | pos += ret; |
| 164 | len -= ret; |
| 165 | } |
| 166 | |
| 167 | if (isi.iomap.length) { |
| 168 | ret = iomap_swapfile_add_extent(&isi); |
| 169 | if (ret) |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | *pagespan = 1 + isi.highest_ppage - isi.lowest_ppage; |
| 174 | sis->max = isi.nr_pages; |
| 175 | sis->pages = isi.nr_pages - 1; |
| 176 | sis->highest_bit = isi.nr_pages - 1; |
| 177 | return isi.nr_extents; |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(iomap_swapfile_activate); |