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
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 */
iomap_swapfile_add_extent(struct iomap_swapfile_info * isi)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 unsigned long max_pages;
34 uint64_t first_ppage;
35 uint64_t first_ppage_reported;
36 uint64_t next_ppage;
37 int error;
38
39 if (unlikely(isi->nr_pages >= isi->sis->max))
40 return 0;
41 max_pages = isi->sis->max - isi->nr_pages;
42
43 /*
44 * Round the start up and the end down so that the physical
45 * extent aligns to a page boundary.
46 */
47 first_ppage = ALIGN(iomap->addr, PAGE_SIZE) >> PAGE_SHIFT;
48 next_ppage = ALIGN_DOWN(iomap->addr + iomap->length, PAGE_SIZE) >>
49 PAGE_SHIFT;
50
51 /* Skip too-short physical extents. */
52 if (first_ppage >= next_ppage)
53 return 0;
54 nr_pages = next_ppage - first_ppage;
55 nr_pages = min(nr_pages, max_pages);
56
57 /*
58 * Calculate how much swap space we're adding; the first page contains
59 * the swap header and doesn't count. The mm still wants that first
60 * page fed to add_swap_extent, however.
61 */
62 first_ppage_reported = first_ppage;
63 if (iomap->offset == 0)
64 first_ppage_reported++;
65 if (isi->lowest_ppage > first_ppage_reported)
66 isi->lowest_ppage = first_ppage_reported;
67 if (isi->highest_ppage < (next_ppage - 1))
68 isi->highest_ppage = next_ppage - 1;
69
70 /* Add extent, set up for the next call. */
71 error = add_swap_extent(isi->sis, isi->nr_pages, nr_pages, first_ppage);
72 if (error < 0)
73 return error;
74 isi->nr_extents += error;
75 isi->nr_pages += nr_pages;
76 return 0;
77 }
78
79 /*
80 * Accumulate iomaps for this swap file. We have to accumulate iomaps because
81 * swap only cares about contiguous page-aligned physical extents and makes no
82 * distinction between written and unwritten extents.
83 */
iomap_swapfile_activate_actor(struct inode * inode,loff_t pos,loff_t count,void * data,struct iomap * iomap,struct iomap * srcmap)84 static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
85 loff_t count, void *data, struct iomap *iomap,
86 struct iomap *srcmap)
87 {
88 struct iomap_swapfile_info *isi = data;
89 int error;
90
91 switch (iomap->type) {
92 case IOMAP_MAPPED:
93 case IOMAP_UNWRITTEN:
94 /* Only real or unwritten extents. */
95 break;
96 case IOMAP_INLINE:
97 /* No inline data. */
98 pr_err("swapon: file is inline\n");
99 return -EINVAL;
100 default:
101 pr_err("swapon: file has unallocated extents\n");
102 return -EINVAL;
103 }
104
105 /* No uncommitted metadata or shared blocks. */
106 if (iomap->flags & IOMAP_F_DIRTY) {
107 pr_err("swapon: file is not committed\n");
108 return -EINVAL;
109 }
110 if (iomap->flags & IOMAP_F_SHARED) {
111 pr_err("swapon: file has shared extents\n");
112 return -EINVAL;
113 }
114
115 /* Only one bdev per swap file. */
116 if (iomap->bdev != isi->sis->bdev) {
117 pr_err("swapon: file is on multiple devices\n");
118 return -EINVAL;
119 }
120
121 if (isi->iomap.length == 0) {
122 /* No accumulated extent, so just store it. */
123 memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
124 } else if (isi->iomap.addr + isi->iomap.length == iomap->addr) {
125 /* Append this to the accumulated extent. */
126 isi->iomap.length += iomap->length;
127 } else {
128 /* Otherwise, add the retained iomap and store this one. */
129 error = iomap_swapfile_add_extent(isi);
130 if (error)
131 return error;
132 memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
133 }
134 return count;
135 }
136
137 /*
138 * Iterate a swap file's iomaps to construct physical extents that can be
139 * passed to the swapfile subsystem.
140 */
iomap_swapfile_activate(struct swap_info_struct * sis,struct file * swap_file,sector_t * pagespan,const struct iomap_ops * ops)141 int iomap_swapfile_activate(struct swap_info_struct *sis,
142 struct file *swap_file, sector_t *pagespan,
143 const struct iomap_ops *ops)
144 {
145 struct iomap_swapfile_info isi = {
146 .sis = sis,
147 .lowest_ppage = (sector_t)-1ULL,
148 };
149 struct address_space *mapping = swap_file->f_mapping;
150 struct inode *inode = mapping->host;
151 loff_t pos = 0;
152 loff_t len = ALIGN_DOWN(i_size_read(inode), PAGE_SIZE);
153 loff_t ret;
154
155 /*
156 * Persist all file mapping metadata so that we won't have any
157 * IOMAP_F_DIRTY iomaps.
158 */
159 ret = vfs_fsync(swap_file, 1);
160 if (ret)
161 return ret;
162
163 while (len > 0) {
164 ret = iomap_apply(inode, pos, len, IOMAP_REPORT,
165 ops, &isi, iomap_swapfile_activate_actor);
166 if (ret <= 0)
167 return ret;
168
169 pos += ret;
170 len -= ret;
171 }
172
173 if (isi.iomap.length) {
174 ret = iomap_swapfile_add_extent(&isi);
175 if (ret)
176 return ret;
177 }
178
179 /*
180 * If this swapfile doesn't contain even a single page-aligned
181 * contiguous range of blocks, reject this useless swapfile to
182 * prevent confusion later on.
183 */
184 if (isi.nr_pages == 0) {
185 pr_warn("swapon: Cannot find a single usable page in file.\n");
186 return -EINVAL;
187 }
188
189 *pagespan = 1 + isi.highest_ppage - isi.lowest_ppage;
190 sis->max = isi.nr_pages;
191 sis->pages = isi.nr_pages - 1;
192 sis->highest_bit = isi.nr_pages - 1;
193 return isi.nr_extents;
194 }
195 EXPORT_SYMBOL_GPL(iomap_swapfile_activate);
196