1
2
3
4
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
13
14struct iomap_swapfile_info {
15 struct iomap iomap;
16 struct swap_info_struct *sis;
17 uint64_t lowest_ppage;
18 uint64_t highest_ppage;
19 unsigned long nr_pages;
20 int nr_extents;
21 struct file *file;
22};
23
24
25
26
27
28
29
30static int iomap_swapfile_add_extent(struct iomap_swapfile_info *isi)
31{
32 struct iomap *iomap = &isi->iomap;
33 unsigned long nr_pages;
34 uint64_t first_ppage;
35 uint64_t first_ppage_reported;
36 uint64_t next_ppage;
37 int error;
38
39
40
41
42
43 first_ppage = ALIGN(iomap->addr, PAGE_SIZE) >> PAGE_SHIFT;
44 next_ppage = ALIGN_DOWN(iomap->addr + iomap->length, PAGE_SIZE) >>
45 PAGE_SHIFT;
46
47
48 if (first_ppage >= next_ppage)
49 return 0;
50 nr_pages = next_ppage - first_ppage;
51
52
53
54
55
56
57 first_ppage_reported = first_ppage;
58 if (iomap->offset == 0)
59 first_ppage_reported++;
60 if (isi->lowest_ppage > first_ppage_reported)
61 isi->lowest_ppage = first_ppage_reported;
62 if (isi->highest_ppage < (next_ppage - 1))
63 isi->highest_ppage = next_ppage - 1;
64
65
66 error = add_swap_extent(isi->sis, isi->nr_pages, nr_pages, first_ppage);
67 if (error < 0)
68 return error;
69 isi->nr_extents += error;
70 isi->nr_pages += nr_pages;
71 return 0;
72}
73
74static int iomap_swapfile_fail(struct iomap_swapfile_info *isi, const char *str)
75{
76 char *buf, *p = ERR_PTR(-ENOMEM);
77
78 buf = kmalloc(PATH_MAX, GFP_KERNEL);
79 if (buf)
80 p = file_path(isi->file, buf, PATH_MAX);
81 pr_err("swapon: file %s %s\n", IS_ERR(p) ? "<unknown>" : p, str);
82 kfree(buf);
83 return -EINVAL;
84}
85
86
87
88
89
90
91static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
92 loff_t count, void *data, struct iomap *iomap,
93 struct iomap *srcmap)
94{
95 struct iomap_swapfile_info *isi = data;
96 int error;
97
98 switch (iomap->type) {
99 case IOMAP_MAPPED:
100 case IOMAP_UNWRITTEN:
101
102 break;
103 case IOMAP_INLINE:
104
105 return iomap_swapfile_fail(isi, "is inline");
106 default:
107 return iomap_swapfile_fail(isi, "has unallocated extents");
108 }
109
110
111 if (iomap->flags & IOMAP_F_DIRTY)
112 return iomap_swapfile_fail(isi, "is not committed");
113 if (iomap->flags & IOMAP_F_SHARED)
114 return iomap_swapfile_fail(isi, "has shared extents");
115
116
117 if (iomap->bdev != isi->sis->bdev)
118 return iomap_swapfile_fail(isi, "outside the main device");
119
120 if (isi->iomap.length == 0) {
121
122 memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
123 } else if (isi->iomap.addr + isi->iomap.length == iomap->addr) {
124
125 isi->iomap.length += iomap->length;
126 } else {
127
128 error = iomap_swapfile_add_extent(isi);
129 if (error)
130 return error;
131 memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
132 }
133 return count;
134}
135
136
137
138
139
140int iomap_swapfile_activate(struct swap_info_struct *sis,
141 struct file *swap_file, sector_t *pagespan,
142 const struct iomap_ops *ops)
143{
144 struct iomap_swapfile_info isi = {
145 .sis = sis,
146 .lowest_ppage = (sector_t)-1ULL,
147 .file = swap_file,
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
157
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
181
182
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}
195EXPORT_SYMBOL_GPL(iomap_swapfile_activate);
196