iomap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef LINUX_IOMAP_H
  3. #define LINUX_IOMAP_H 1
  4. #include <linux/atomic.h>
  5. #include <linux/bitmap.h>
  6. #include <linux/blk_types.h>
  7. #include <linux/mm.h>
  8. #include <linux/types.h>
  9. #include <linux/mm_types.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/android_kabi.h>
  12. struct address_space;
  13. struct fiemap_extent_info;
  14. struct inode;
  15. struct iomap_dio;
  16. struct iomap_writepage_ctx;
  17. struct iov_iter;
  18. struct kiocb;
  19. struct page;
  20. struct vm_area_struct;
  21. struct vm_fault;
  22. /*
  23. * Types of block ranges for iomap mappings:
  24. */
  25. #define IOMAP_HOLE 0 /* no blocks allocated, need allocation */
  26. #define IOMAP_DELALLOC 1 /* delayed allocation blocks */
  27. #define IOMAP_MAPPED 2 /* blocks allocated at @addr */
  28. #define IOMAP_UNWRITTEN 3 /* blocks allocated at @addr in unwritten state */
  29. #define IOMAP_INLINE 4 /* data inline in the inode */
  30. /*
  31. * Flags reported by the file system from iomap_begin:
  32. *
  33. * IOMAP_F_NEW indicates that the blocks have been newly allocated and need
  34. * zeroing for areas that no data is copied to.
  35. *
  36. * IOMAP_F_DIRTY indicates the inode has uncommitted metadata needed to access
  37. * written data and requires fdatasync to commit them to persistent storage.
  38. * This needs to take into account metadata changes that *may* be made at IO
  39. * completion, such as file size updates from direct IO.
  40. *
  41. * IOMAP_F_SHARED indicates that the blocks are shared, and will need to be
  42. * unshared as part a write.
  43. *
  44. * IOMAP_F_MERGED indicates that the iomap contains the merge of multiple block
  45. * mappings.
  46. *
  47. * IOMAP_F_BUFFER_HEAD indicates that the file system requires the use of
  48. * buffer heads for this mapping.
  49. */
  50. #define IOMAP_F_NEW 0x01
  51. #define IOMAP_F_DIRTY 0x02
  52. #define IOMAP_F_SHARED 0x04
  53. #define IOMAP_F_MERGED 0x08
  54. #define IOMAP_F_BUFFER_HEAD 0x10
  55. #define IOMAP_F_ZONE_APPEND 0x20
  56. /*
  57. * Flags set by the core iomap code during operations:
  58. *
  59. * IOMAP_F_SIZE_CHANGED indicates to the iomap_end method that the file size
  60. * has changed as the result of this write operation.
  61. */
  62. #define IOMAP_F_SIZE_CHANGED 0x100
  63. /*
  64. * Flags from 0x1000 up are for file system specific usage:
  65. */
  66. #define IOMAP_F_PRIVATE 0x1000
  67. /*
  68. * Magic value for addr:
  69. */
  70. #define IOMAP_NULL_ADDR -1ULL /* addr is not valid */
  71. struct iomap_page_ops;
  72. struct iomap {
  73. u64 addr; /* disk offset of mapping, bytes */
  74. loff_t offset; /* file offset of mapping, bytes */
  75. u64 length; /* length of mapping, bytes */
  76. u16 type; /* type of mapping */
  77. u16 flags; /* flags for mapping */
  78. struct block_device *bdev; /* block device for I/O */
  79. struct dax_device *dax_dev; /* dax_dev for dax operations */
  80. void *inline_data;
  81. void *private; /* filesystem private */
  82. const struct iomap_page_ops *page_ops;
  83. ANDROID_KABI_RESERVE(1);
  84. };
  85. static inline sector_t iomap_sector(const struct iomap *iomap, loff_t pos)
  86. {
  87. return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
  88. }
  89. /*
  90. * Returns the inline data pointer for logical offset @pos.
  91. */
  92. static inline void *iomap_inline_data(const struct iomap *iomap, loff_t pos)
  93. {
  94. return iomap->inline_data + pos - iomap->offset;
  95. }
  96. /*
  97. * Check if the mapping's length is within the valid range for inline data.
  98. * This is used to guard against accessing data beyond the page inline_data
  99. * points at.
  100. */
  101. static inline bool iomap_inline_data_valid(const struct iomap *iomap)
  102. {
  103. return iomap->length <= PAGE_SIZE - offset_in_page(iomap->inline_data);
  104. }
  105. /*
  106. * When a filesystem sets page_ops in an iomap mapping it returns, page_prepare
  107. * and page_done will be called for each page written to. This only applies to
  108. * buffered writes as unbuffered writes will not typically have pages
  109. * associated with them.
  110. *
  111. * When page_prepare succeeds, page_done will always be called to do any
  112. * cleanup work necessary. In that page_done call, @page will be NULL if the
  113. * associated page could not be obtained.
  114. */
  115. struct iomap_page_ops {
  116. int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len);
  117. void (*page_done)(struct inode *inode, loff_t pos, unsigned copied,
  118. struct page *page);
  119. };
  120. /*
  121. * Flags for iomap_begin / iomap_end. No flag implies a read.
  122. */
  123. #define IOMAP_WRITE (1 << 0) /* writing, must allocate blocks */
  124. #define IOMAP_ZERO (1 << 1) /* zeroing operation, may skip holes */
  125. #define IOMAP_REPORT (1 << 2) /* report extent status, e.g. FIEMAP */
  126. #define IOMAP_FAULT (1 << 3) /* mapping for page fault */
  127. #define IOMAP_DIRECT (1 << 4) /* direct I/O */
  128. #define IOMAP_NOWAIT (1 << 5) /* do not block */
  129. #define IOMAP_OVERWRITE_ONLY (1 << 6) /* only pure overwrites allowed */
  130. #define IOMAP_UNSHARE (1 << 7) /* unshare_file_range */
  131. #ifdef CONFIG_FS_DAX
  132. #define IOMAP_DAX (1 << 8) /* DAX mapping */
  133. #else
  134. #define IOMAP_DAX 0
  135. #endif /* CONFIG_FS_DAX */
  136. struct iomap_ops {
  137. /*
  138. * Return the existing mapping at pos, or reserve space starting at
  139. * pos for up to length, as long as we can do it as a single mapping.
  140. * The actual length is returned in iomap->length.
  141. */
  142. int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
  143. unsigned flags, struct iomap *iomap,
  144. struct iomap *srcmap);
  145. /*
  146. * Commit and/or unreserve space previous allocated using iomap_begin.
  147. * Written indicates the length of the successful write operation which
  148. * needs to be commited, while the rest needs to be unreserved.
  149. * Written might be zero if no data was written.
  150. */
  151. int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
  152. ssize_t written, unsigned flags, struct iomap *iomap);
  153. ANDROID_KABI_RESERVE(1);
  154. ANDROID_KABI_RESERVE(2);
  155. };
  156. /**
  157. * struct iomap_iter - Iterate through a range of a file
  158. * @inode: Set at the start of the iteration and should not change.
  159. * @pos: The current file position we are operating on. It is updated by
  160. * calls to iomap_iter(). Treat as read-only in the body.
  161. * @len: The remaining length of the file segment we're operating on.
  162. * It is updated at the same time as @pos.
  163. * @processed: The number of bytes processed by the body in the most recent
  164. * iteration, or a negative errno. 0 causes the iteration to stop.
  165. * @flags: Zero or more of the iomap_begin flags above.
  166. * @iomap: Map describing the I/O iteration
  167. * @srcmap: Source map for COW operations
  168. */
  169. struct iomap_iter {
  170. struct inode *inode;
  171. loff_t pos;
  172. u64 len;
  173. s64 processed;
  174. unsigned flags;
  175. struct iomap iomap;
  176. struct iomap srcmap;
  177. void *private;
  178. };
  179. int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops);
  180. /**
  181. * iomap_length - length of the current iomap iteration
  182. * @iter: iteration structure
  183. *
  184. * Returns the length that the operation applies to for the current iteration.
  185. */
  186. static inline u64 iomap_length(const struct iomap_iter *iter)
  187. {
  188. u64 end = iter->iomap.offset + iter->iomap.length;
  189. if (iter->srcmap.type != IOMAP_HOLE)
  190. end = min(end, iter->srcmap.offset + iter->srcmap.length);
  191. return min(iter->len, end - iter->pos);
  192. }
  193. /**
  194. * iomap_iter_srcmap - return the source map for the current iomap iteration
  195. * @i: iteration structure
  196. *
  197. * Write operations on file systems with reflink support might require a
  198. * source and a destination map. This function retourns the source map
  199. * for a given operation, which may or may no be identical to the destination
  200. * map in &i->iomap.
  201. */
  202. static inline const struct iomap *iomap_iter_srcmap(const struct iomap_iter *i)
  203. {
  204. if (i->srcmap.type != IOMAP_HOLE)
  205. return &i->srcmap;
  206. return &i->iomap;
  207. }
  208. ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
  209. const struct iomap_ops *ops);
  210. int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops);
  211. void iomap_readahead(struct readahead_control *, const struct iomap_ops *ops);
  212. bool iomap_is_partially_uptodate(struct folio *, size_t from, size_t count);
  213. bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags);
  214. void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len);
  215. int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
  216. const struct iomap_ops *ops);
  217. int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
  218. bool *did_zero, const struct iomap_ops *ops);
  219. int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
  220. const struct iomap_ops *ops);
  221. vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf,
  222. const struct iomap_ops *ops);
  223. int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  224. u64 start, u64 len, const struct iomap_ops *ops);
  225. loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
  226. const struct iomap_ops *ops);
  227. loff_t iomap_seek_data(struct inode *inode, loff_t offset,
  228. const struct iomap_ops *ops);
  229. sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
  230. const struct iomap_ops *ops);
  231. /*
  232. * Structure for writeback I/O completions.
  233. */
  234. struct iomap_ioend {
  235. struct list_head io_list; /* next ioend in chain */
  236. u16 io_type;
  237. u16 io_flags; /* IOMAP_F_* */
  238. u32 io_folios; /* folios added to ioend */
  239. struct inode *io_inode; /* file being written to */
  240. size_t io_size; /* size of the extent */
  241. loff_t io_offset; /* offset in the file */
  242. sector_t io_sector; /* start sector of ioend */
  243. struct bio *io_bio; /* bio being built */
  244. struct bio io_inline_bio; /* MUST BE LAST! */
  245. };
  246. struct iomap_writeback_ops {
  247. /*
  248. * Required, maps the blocks so that writeback can be performed on
  249. * the range starting at offset.
  250. */
  251. int (*map_blocks)(struct iomap_writepage_ctx *wpc, struct inode *inode,
  252. loff_t offset);
  253. /*
  254. * Optional, allows the file systems to perform actions just before
  255. * submitting the bio and/or override the bio end_io handler for complex
  256. * operations like copy on write extent manipulation or unwritten extent
  257. * conversions.
  258. */
  259. int (*prepare_ioend)(struct iomap_ioend *ioend, int status);
  260. /*
  261. * Optional, allows the file system to discard state on a page where
  262. * we failed to submit any I/O.
  263. */
  264. void (*discard_folio)(struct folio *folio, loff_t pos);
  265. };
  266. struct iomap_writepage_ctx {
  267. struct iomap iomap;
  268. struct iomap_ioend *ioend;
  269. const struct iomap_writeback_ops *ops;
  270. };
  271. void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
  272. void iomap_ioend_try_merge(struct iomap_ioend *ioend,
  273. struct list_head *more_ioends);
  274. void iomap_sort_ioends(struct list_head *ioend_list);
  275. int iomap_writepages(struct address_space *mapping,
  276. struct writeback_control *wbc, struct iomap_writepage_ctx *wpc,
  277. const struct iomap_writeback_ops *ops);
  278. /*
  279. * Flags for direct I/O ->end_io:
  280. */
  281. #define IOMAP_DIO_UNWRITTEN (1 << 0) /* covers unwritten extent(s) */
  282. #define IOMAP_DIO_COW (1 << 1) /* covers COW extent(s) */
  283. struct iomap_dio_ops {
  284. int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
  285. unsigned flags);
  286. void (*submit_io)(const struct iomap_iter *iter, struct bio *bio,
  287. loff_t file_offset);
  288. /*
  289. * Filesystems wishing to attach private information to a direct io bio
  290. * must provide a ->submit_io method that attaches the additional
  291. * information to the bio and changes the ->bi_end_io callback to a
  292. * custom function. This function should, at a minimum, perform any
  293. * relevant post-processing of the bio and end with a call to
  294. * iomap_dio_bio_end_io.
  295. */
  296. struct bio_set *bio_set;
  297. };
  298. /*
  299. * Wait for the I/O to complete in iomap_dio_rw even if the kiocb is not
  300. * synchronous.
  301. */
  302. #define IOMAP_DIO_FORCE_WAIT (1 << 0)
  303. /*
  304. * Do not allocate blocks or zero partial blocks, but instead fall back to
  305. * the caller by returning -EAGAIN. Used to optimize direct I/O writes that
  306. * are not aligned to the file system block size.
  307. */
  308. #define IOMAP_DIO_OVERWRITE_ONLY (1 << 1)
  309. /*
  310. * When a page fault occurs, return a partial synchronous result and allow
  311. * the caller to retry the rest of the operation after dealing with the page
  312. * fault.
  313. */
  314. #define IOMAP_DIO_PARTIAL (1 << 2)
  315. /*
  316. * The caller will sync the write if needed; do not sync it within
  317. * iomap_dio_rw. Overrides IOMAP_DIO_FORCE_WAIT.
  318. */
  319. #define IOMAP_DIO_NOSYNC (1 << 3)
  320. ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
  321. const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
  322. unsigned int dio_flags, void *private, size_t done_before);
  323. struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
  324. const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
  325. unsigned int dio_flags, void *private, size_t done_before);
  326. ssize_t iomap_dio_complete(struct iomap_dio *dio);
  327. void iomap_dio_bio_end_io(struct bio *bio);
  328. #ifdef CONFIG_SWAP
  329. struct file;
  330. struct swap_info_struct;
  331. int iomap_swapfile_activate(struct swap_info_struct *sis,
  332. struct file *swap_file, sector_t *pagespan,
  333. const struct iomap_ops *ops);
  334. #else
  335. # define iomap_swapfile_activate(sis, swapfile, pagespan, ops) (-EIO)
  336. #endif /* CONFIG_SWAP */
  337. #endif /* LINUX_IOMAP_H */