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