^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) 2017 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2018 Christoph Hellwig.
^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/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/pagevec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Seek for SEEK_DATA / SEEK_HOLE within @page, starting at @lastoff.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Returns true if found and updates @lastoff to the offset in file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) page_seek_hole_data(struct inode *inode, struct page *page, loff_t *lastoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) const struct address_space_operations *ops = inode->i_mapping->a_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned int bsize = i_blocksize(inode), off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) bool seek_data = whence == SEEK_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) loff_t poff = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (WARN_ON_ONCE(*lastoff >= poff + PAGE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (*lastoff < poff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Last offset smaller than the start of the page means we found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * a hole:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (whence == SEEK_HOLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *lastoff = poff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Just check the page unless we can and should check block ranges:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (bsize == PAGE_SIZE || !ops->is_partially_uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return PageUptodate(page) == seek_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (unlikely(page->mapping != inode->i_mapping))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) goto out_unlock_not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) for (off = 0; off < PAGE_SIZE; off += bsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (offset_in_page(*lastoff) >= off + bsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (ops->is_partially_uptodate(page, off, bsize) == seek_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) *lastoff = poff + off + bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) out_unlock_not_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Seek for SEEK_DATA / SEEK_HOLE in the page cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * Within unwritten extents, the page cache determines which parts are holes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * and which are data: uptodate buffer heads count as data; everything else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * counts as a hole.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * Returns the resulting offset on successs, and -ENOENT otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static loff_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) page_cache_seek_hole_data(struct inode *inode, loff_t offset, loff_t length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) pgoff_t index = offset >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pgoff_t end = DIV_ROUND_UP(offset + length, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) loff_t lastoff = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct pagevec pvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (length <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) pagevec_init(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) unsigned nr_pages, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, &index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) end - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (nr_pages == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct page *page = pvec.pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (page_seek_hole_data(inode, page, &lastoff, whence))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto check_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) lastoff = page_offset(page) + PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) } while (index < end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /* When no page at lastoff and we are not done, we found a hole. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (whence != SEEK_HOLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) goto not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) check_range:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (lastoff < offset + length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) not_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) lastoff = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return lastoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static loff_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) iomap_seek_hole_actor(struct inode *inode, loff_t offset, loff_t length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) void *data, struct iomap *iomap, struct iomap *srcmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) switch (iomap->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) case IOMAP_UNWRITTEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) offset = page_cache_seek_hole_data(inode, offset, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) SEEK_HOLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) case IOMAP_HOLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) *(loff_t *)data = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return length;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) loff_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) loff_t size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) loff_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* Nothing to be found before or beyond the end of the file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (offset < 0 || offset >= size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) while (offset < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ret = iomap_apply(inode, offset, size - offset, IOMAP_REPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ops, &offset, iomap_seek_hole_actor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) offset += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) EXPORT_SYMBOL_GPL(iomap_seek_hole);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static loff_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) iomap_seek_data_actor(struct inode *inode, loff_t offset, loff_t length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void *data, struct iomap *iomap, struct iomap *srcmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) switch (iomap->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) case IOMAP_HOLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) case IOMAP_UNWRITTEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) offset = page_cache_seek_hole_data(inode, offset, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) SEEK_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *(loff_t *)data = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) loff_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) loff_t size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) loff_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* Nothing to be found before or beyond the end of the file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (offset < 0 || offset >= size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) while (offset < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ret = iomap_apply(inode, offset, size - offset, IOMAP_REPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ops, &offset, iomap_seek_data_actor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) offset += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /* We've reached the end of the file without finding data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) EXPORT_SYMBOL_GPL(iomap_seek_data);