GNU Linux-libre 5.4.257-gnu1
[releases.git] / fs / iomap / swapfile.c
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  */
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  */
84 static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
85                 loff_t count, void *data, struct iomap *iomap)
86 {
87         struct iomap_swapfile_info *isi = data;
88         int error;
89
90         switch (iomap->type) {
91         case IOMAP_MAPPED:
92         case IOMAP_UNWRITTEN:
93                 /* Only real or unwritten extents. */
94                 break;
95         case IOMAP_INLINE:
96                 /* No inline data. */
97                 pr_err("swapon: file is inline\n");
98                 return -EINVAL;
99         default:
100                 pr_err("swapon: file has unallocated extents\n");
101                 return -EINVAL;
102         }
103
104         /* No uncommitted metadata or shared blocks. */
105         if (iomap->flags & IOMAP_F_DIRTY) {
106                 pr_err("swapon: file is not committed\n");
107                 return -EINVAL;
108         }
109         if (iomap->flags & IOMAP_F_SHARED) {
110                 pr_err("swapon: file has shared extents\n");
111                 return -EINVAL;
112         }
113
114         /* Only one bdev per swap file. */
115         if (iomap->bdev != isi->sis->bdev) {
116                 pr_err("swapon: file is on multiple devices\n");
117                 return -EINVAL;
118         }
119
120         if (isi->iomap.length == 0) {
121                 /* No accumulated extent, so just store it. */
122                 memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
123         } else if (isi->iomap.addr + isi->iomap.length == iomap->addr) {
124                 /* Append this to the accumulated extent. */
125                 isi->iomap.length += iomap->length;
126         } else {
127                 /* Otherwise, add the retained iomap and store this one. */
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  * Iterate a swap file's iomaps to construct physical extents that can be
138  * passed to the swapfile subsystem.
139  */
140 int 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         };
148         struct address_space *mapping = swap_file->f_mapping;
149         struct inode *inode = mapping->host;
150         loff_t pos = 0;
151         loff_t len = ALIGN_DOWN(i_size_read(inode), PAGE_SIZE);
152         loff_t ret;
153
154         /*
155          * Persist all file mapping metadata so that we won't have any
156          * IOMAP_F_DIRTY iomaps.
157          */
158         ret = vfs_fsync(swap_file, 1);
159         if (ret)
160                 return ret;
161
162         while (len > 0) {
163                 ret = iomap_apply(inode, pos, len, IOMAP_REPORT,
164                                 ops, &isi, iomap_swapfile_activate_actor);
165                 if (ret <= 0)
166                         return ret;
167
168                 pos += ret;
169                 len -= ret;
170         }
171
172         if (isi.iomap.length) {
173                 ret = iomap_swapfile_add_extent(&isi);
174                 if (ret)
175                         return ret;
176         }
177
178         /*
179          * If this swapfile doesn't contain even a single page-aligned
180          * contiguous range of blocks, reject this useless swapfile to
181          * prevent confusion later on.
182          */
183         if (isi.nr_pages == 0) {
184                 pr_warn("swapon: Cannot find a single usable page in file.\n");
185                 return -EINVAL;
186         }
187
188         *pagespan = 1 + isi.highest_ppage - isi.lowest_ppage;
189         sis->max = isi.nr_pages;
190         sis->pages = isi.nr_pages - 1;
191         sis->highest_bit = isi.nr_pages - 1;
192         return isi.nr_extents;
193 }
194 EXPORT_SYMBOL_GPL(iomap_swapfile_activate);