Linux Audio

Check our new training course

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