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