blob: 2103b94cb1bff008e2b3fb47504f068181ae694a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Christoph Hellwig199a31c2016-06-21 09:22:39 +10002#ifndef LINUX_IOMAP_H
3#define LINUX_IOMAP_H 1
4
Christoph Hellwig9dc55f12018-07-11 22:26:05 -07005#include <linux/atomic.h>
6#include <linux/bitmap.h>
7#include <linux/mm.h>
Christoph Hellwig199a31c2016-06-21 09:22:39 +10008#include <linux/types.h>
Souptick Joarder5780a022018-10-26 15:02:59 -07009#include <linux/mm_types.h>
Christoph Hellwig199a31c2016-06-21 09:22:39 +100010
Christoph Hellwig89eb1902018-06-01 09:03:08 -070011struct address_space;
Christoph Hellwig8be9f562016-06-21 09:38:45 +100012struct fiemap_extent_info;
Christoph Hellwigae259a92016-06-21 09:23:11 +100013struct inode;
14struct iov_iter;
15struct kiocb;
Christoph Hellwig63899c62018-06-19 15:10:56 -070016struct page;
Christoph Hellwigae259a92016-06-21 09:23:11 +100017struct vm_area_struct;
18struct vm_fault;
19
20/*
21 * Types of block ranges for iomap mappings:
22 */
Christoph Hellwig199a31c2016-06-21 09:22:39 +100023#define IOMAP_HOLE 0x01 /* no blocks allocated, need allocation */
24#define IOMAP_DELALLOC 0x02 /* delayed allocation blocks */
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040025#define IOMAP_MAPPED 0x03 /* blocks allocated at @addr */
26#define IOMAP_UNWRITTEN 0x04 /* blocks allocated at @addr in unwritten state */
Christoph Hellwig19319b52018-06-01 09:03:06 -070027#define IOMAP_INLINE 0x05 /* data inline in the inode */
Christoph Hellwig199a31c2016-06-21 09:22:39 +100028
Christoph Hellwigae259a92016-06-21 09:23:11 +100029/*
Christoph Hellwigd33fd772016-10-20 15:51:28 +110030 * Flags for all iomap mappings:
Linus Torvaldsa3841f92017-11-17 09:51:57 -080031 *
Jan Karacaa51d22017-11-01 16:36:42 +010032 * IOMAP_F_DIRTY indicates the inode has uncommitted metadata needed to access
33 * written data and requires fdatasync to commit them to persistent storage.
Christoph Hellwig17de0a92016-08-29 11:33:58 +100034 */
Bob Peterson39743202017-02-16 10:27:16 -050035#define IOMAP_F_NEW 0x01 /* blocks have been newly allocated */
Christoph Hellwig7ee66c02018-06-01 09:03:07 -070036#define IOMAP_F_DIRTY 0x02 /* uncommitted metadata */
Christoph Hellwigc03cea42018-06-19 15:10:58 -070037#define IOMAP_F_BUFFER_HEAD 0x04 /* file system requires buffer heads */
Christoph Hellwigd33fd772016-10-20 15:51:28 +110038
39/*
40 * Flags that only need to be reported for IOMAP_REPORT requests:
41 */
Andreas Gruenbacher9ca250a2017-10-01 17:56:54 -040042#define IOMAP_F_MERGED 0x10 /* contains multiple blocks/extents */
43#define IOMAP_F_SHARED 0x20 /* block shared with another file */
Christoph Hellwig17de0a92016-08-29 11:33:58 +100044
45/*
Christoph Hellwig7ee66c02018-06-01 09:03:07 -070046 * Flags from 0x1000 up are for file system specific usage:
47 */
48#define IOMAP_F_PRIVATE 0x1000
49
50
51/*
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040052 * Magic value for addr:
Christoph Hellwigae259a92016-06-21 09:23:11 +100053 */
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040054#define IOMAP_NULL_ADDR -1ULL /* addr is not valid */
Christoph Hellwig199a31c2016-06-21 09:22:39 +100055
Andreas Gruenbacherdf0db3e2019-04-30 08:45:34 -070056struct iomap_page_ops;
57
Christoph Hellwig199a31c2016-06-21 09:22:39 +100058struct iomap {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040059 u64 addr; /* disk offset of mapping, bytes */
Christoph Hellwigae259a92016-06-21 09:23:11 +100060 loff_t offset; /* file offset of mapping, bytes */
61 u64 length; /* length of mapping, bytes */
Christoph Hellwig17de0a92016-08-29 11:33:58 +100062 u16 type; /* type of mapping */
63 u16 flags; /* flags for mapping */
Christoph Hellwigae259a92016-06-21 09:23:11 +100064 struct block_device *bdev; /* block device for I/O */
Dan Williamsfa5d9322017-01-27 12:04:59 -080065 struct dax_device *dax_dev; /* dax_dev for dax operations */
Andreas Gruenbacher19e0c582018-06-19 15:10:56 -070066 void *inline_data;
Andreas Gruenbachere184fde2018-06-19 15:10:57 -070067 void *private; /* filesystem private */
Andreas Gruenbacherdf0db3e2019-04-30 08:45:34 -070068 const struct iomap_page_ops *page_ops;
69};
Christoph Hellwig63899c62018-06-19 15:10:56 -070070
Andreas Gruenbacherdf0db3e2019-04-30 08:45:34 -070071/*
72 * When a filesystem sets page_ops in an iomap mapping it returns, page_prepare
73 * and page_done will be called for each page written to. This only applies to
74 * buffered writes as unbuffered writes will not typically have pages
75 * associated with them.
76 *
77 * When page_prepare succeeds, page_done will always be called to do any
78 * cleanup work necessary. In that page_done call, @page will be NULL if the
79 * associated page could not be obtained.
80 */
81struct iomap_page_ops {
82 int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len,
83 struct iomap *iomap);
Christoph Hellwig63899c62018-06-19 15:10:56 -070084 void (*page_done)(struct inode *inode, loff_t pos, unsigned copied,
85 struct page *page, struct iomap *iomap);
Christoph Hellwig199a31c2016-06-21 09:22:39 +100086};
87
Christoph Hellwigae259a92016-06-21 09:23:11 +100088/*
89 * Flags for iomap_begin / iomap_end. No flag implies a read.
90 */
Christoph Hellwigd33fd772016-10-20 15:51:28 +110091#define IOMAP_WRITE (1 << 0) /* writing, must allocate blocks */
92#define IOMAP_ZERO (1 << 1) /* zeroing operation, may skip holes */
93#define IOMAP_REPORT (1 << 2) /* report extent status, e.g. FIEMAP */
Jan Kara9484ab12016-11-10 10:26:50 +110094#define IOMAP_FAULT (1 << 3) /* mapping for page fault */
Christoph Hellwigff6a9292016-11-30 14:36:01 +110095#define IOMAP_DIRECT (1 << 4) /* direct I/O */
Christoph Hellwig9ecac0e2018-06-01 09:03:07 -070096#define IOMAP_NOWAIT (1 << 5) /* do not block */
Christoph Hellwigae259a92016-06-21 09:23:11 +100097
98struct iomap_ops {
99 /*
100 * Return the existing mapping at pos, or reserve space starting at
101 * pos for up to length, as long as we can do it as a single mapping.
102 * The actual length is returned in iomap->length.
103 */
104 int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
105 unsigned flags, struct iomap *iomap);
106
107 /*
108 * Commit and/or unreserve space previous allocated using iomap_begin.
109 * Written indicates the length of the successful write operation which
110 * needs to be commited, while the rest needs to be unreserved.
111 * Written might be zero if no data was written.
112 */
113 int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
114 ssize_t written, unsigned flags, struct iomap *iomap);
115};
116
Christoph Hellwig9dc55f12018-07-11 22:26:05 -0700117/*
118 * Structure allocate for each page when block size < PAGE_SIZE to track
119 * sub-page uptodate status and I/O completions.
120 */
121struct iomap_page {
122 atomic_t read_count;
123 atomic_t write_count;
124 DECLARE_BITMAP(uptodate, PAGE_SIZE / 512);
125};
126
127static inline struct iomap_page *to_iomap_page(struct page *page)
128{
129 if (page_has_private(page))
130 return (struct iomap_page *)page_private(page);
131 return NULL;
132}
133
Christoph Hellwigae259a92016-06-21 09:23:11 +1000134ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -0800135 const struct iomap_ops *ops);
Christoph Hellwig72b4daa2018-06-19 15:10:57 -0700136int iomap_readpage(struct page *page, const struct iomap_ops *ops);
137int iomap_readpages(struct address_space *mapping, struct list_head *pages,
138 unsigned nr_pages, const struct iomap_ops *ops);
Christoph Hellwigc03cea42018-06-19 15:10:58 -0700139int iomap_set_page_dirty(struct page *page);
Christoph Hellwig9dc55f12018-07-11 22:26:05 -0700140int iomap_is_partially_uptodate(struct page *page, unsigned long from,
141 unsigned long count);
142int iomap_releasepage(struct page *page, gfp_t gfp_mask);
143void iomap_invalidatepage(struct page *page, unsigned int offset,
144 unsigned int len);
145#ifdef CONFIG_MIGRATION
146int iomap_migrate_page(struct address_space *mapping, struct page *newpage,
147 struct page *page, enum migrate_mode mode);
148#else
149#define iomap_migrate_page NULL
150#endif
Christoph Hellwig5f4e5752016-09-19 10:12:45 +1000151int iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -0800152 const struct iomap_ops *ops);
Christoph Hellwigae259a92016-06-21 09:23:11 +1000153int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -0800154 bool *did_zero, const struct iomap_ops *ops);
Christoph Hellwigae259a92016-06-21 09:23:11 +1000155int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -0800156 const struct iomap_ops *ops);
Souptick Joarder5780a022018-10-26 15:02:59 -0700157vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf,
158 const struct iomap_ops *ops);
Christoph Hellwig8be9f562016-06-21 09:38:45 +1000159int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -0800160 loff_t start, loff_t len, const struct iomap_ops *ops);
Andreas Gruenbacher0ed3b0d2017-06-29 11:43:21 -0700161loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
162 const struct iomap_ops *ops);
163loff_t iomap_seek_data(struct inode *inode, loff_t offset,
164 const struct iomap_ops *ops);
Christoph Hellwig89eb1902018-06-01 09:03:08 -0700165sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
166 const struct iomap_ops *ops);
Christoph Hellwigae259a92016-06-21 09:23:11 +1000167
Christoph Hellwigff6a9292016-11-30 14:36:01 +1100168/*
169 * Flags for direct I/O ->end_io:
170 */
171#define IOMAP_DIO_UNWRITTEN (1 << 0) /* covers unwritten extent(s) */
172#define IOMAP_DIO_COW (1 << 1) /* covers COW extent(s) */
173typedef int (iomap_dio_end_io_t)(struct kiocb *iocb, ssize_t ret,
174 unsigned flags);
175ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -0800176 const struct iomap_ops *ops, iomap_dio_end_io_t end_io);
Christoph Hellwig81214ba2018-12-04 11:12:08 -0700177int iomap_dio_iopoll(struct kiocb *kiocb, bool spin);
Christoph Hellwigff6a9292016-11-30 14:36:01 +1100178
Darrick J. Wong674821292018-05-10 08:38:15 -0700179#ifdef CONFIG_SWAP
180struct file;
181struct swap_info_struct;
182
183int iomap_swapfile_activate(struct swap_info_struct *sis,
184 struct file *swap_file, sector_t *pagespan,
185 const struct iomap_ops *ops);
186#else
187# define iomap_swapfile_activate(sis, swapfile, pagespan, ops) (-EIO)
188#endif /* CONFIG_SWAP */
189
Christoph Hellwig199a31c2016-06-21 09:22:39 +1000190#endif /* LINUX_IOMAP_H */