Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef LINUX_IOMAP_H
0003 #define LINUX_IOMAP_H 1
0004 
0005 #include <linux/atomic.h>
0006 #include <linux/bitmap.h>
0007 #include <linux/blk_types.h>
0008 #include <linux/mm.h>
0009 #include <linux/types.h>
0010 #include <linux/mm_types.h>
0011 #include <linux/blkdev.h>
0012 
0013 struct address_space;
0014 struct fiemap_extent_info;
0015 struct inode;
0016 struct iomap_dio;
0017 struct iomap_writepage_ctx;
0018 struct iov_iter;
0019 struct kiocb;
0020 struct page;
0021 struct vm_area_struct;
0022 struct vm_fault;
0023 
0024 /*
0025  * Types of block ranges for iomap mappings:
0026  */
0027 #define IOMAP_HOLE  0   /* no blocks allocated, need allocation */
0028 #define IOMAP_DELALLOC  1   /* delayed allocation blocks */
0029 #define IOMAP_MAPPED    2   /* blocks allocated at @addr */
0030 #define IOMAP_UNWRITTEN 3   /* blocks allocated at @addr in unwritten state */
0031 #define IOMAP_INLINE    4   /* data inline in the inode */
0032 
0033 /*
0034  * Flags reported by the file system from iomap_begin:
0035  *
0036  * IOMAP_F_NEW indicates that the blocks have been newly allocated and need
0037  * zeroing for areas that no data is copied to.
0038  *
0039  * IOMAP_F_DIRTY indicates the inode has uncommitted metadata needed to access
0040  * written data and requires fdatasync to commit them to persistent storage.
0041  * This needs to take into account metadata changes that *may* be made at IO
0042  * completion, such as file size updates from direct IO.
0043  *
0044  * IOMAP_F_SHARED indicates that the blocks are shared, and will need to be
0045  * unshared as part a write.
0046  *
0047  * IOMAP_F_MERGED indicates that the iomap contains the merge of multiple block
0048  * mappings.
0049  *
0050  * IOMAP_F_BUFFER_HEAD indicates that the file system requires the use of
0051  * buffer heads for this mapping.
0052  */
0053 #define IOMAP_F_NEW     0x01
0054 #define IOMAP_F_DIRTY       0x02
0055 #define IOMAP_F_SHARED      0x04
0056 #define IOMAP_F_MERGED      0x08
0057 #define IOMAP_F_BUFFER_HEAD 0x10
0058 #define IOMAP_F_ZONE_APPEND 0x20
0059 
0060 /*
0061  * Flags set by the core iomap code during operations:
0062  *
0063  * IOMAP_F_SIZE_CHANGED indicates to the iomap_end method that the file size
0064  * has changed as the result of this write operation.
0065  */
0066 #define IOMAP_F_SIZE_CHANGED    0x100
0067 
0068 /*
0069  * Flags from 0x1000 up are for file system specific usage:
0070  */
0071 #define IOMAP_F_PRIVATE     0x1000
0072 
0073 
0074 /*
0075  * Magic value for addr:
0076  */
0077 #define IOMAP_NULL_ADDR -1ULL   /* addr is not valid */
0078 
0079 struct iomap_page_ops;
0080 
0081 struct iomap {
0082     u64         addr; /* disk offset of mapping, bytes */
0083     loff_t          offset; /* file offset of mapping, bytes */
0084     u64         length; /* length of mapping, bytes */
0085     u16         type;   /* type of mapping */
0086     u16         flags;  /* flags for mapping */
0087     struct block_device *bdev;  /* block device for I/O */
0088     struct dax_device   *dax_dev; /* dax_dev for dax operations */
0089     void            *inline_data;
0090     void            *private; /* filesystem private */
0091     const struct iomap_page_ops *page_ops;
0092 };
0093 
0094 static inline sector_t iomap_sector(const struct iomap *iomap, loff_t pos)
0095 {
0096     return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
0097 }
0098 
0099 /*
0100  * Returns the inline data pointer for logical offset @pos.
0101  */
0102 static inline void *iomap_inline_data(const struct iomap *iomap, loff_t pos)
0103 {
0104     return iomap->inline_data + pos - iomap->offset;
0105 }
0106 
0107 /*
0108  * Check if the mapping's length is within the valid range for inline data.
0109  * This is used to guard against accessing data beyond the page inline_data
0110  * points at.
0111  */
0112 static inline bool iomap_inline_data_valid(const struct iomap *iomap)
0113 {
0114     return iomap->length <= PAGE_SIZE - offset_in_page(iomap->inline_data);
0115 }
0116 
0117 /*
0118  * When a filesystem sets page_ops in an iomap mapping it returns, page_prepare
0119  * and page_done will be called for each page written to.  This only applies to
0120  * buffered writes as unbuffered writes will not typically have pages
0121  * associated with them.
0122  *
0123  * When page_prepare succeeds, page_done will always be called to do any
0124  * cleanup work necessary.  In that page_done call, @page will be NULL if the
0125  * associated page could not be obtained.
0126  */
0127 struct iomap_page_ops {
0128     int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len);
0129     void (*page_done)(struct inode *inode, loff_t pos, unsigned copied,
0130             struct page *page);
0131 };
0132 
0133 /*
0134  * Flags for iomap_begin / iomap_end.  No flag implies a read.
0135  */
0136 #define IOMAP_WRITE     (1 << 0) /* writing, must allocate blocks */
0137 #define IOMAP_ZERO      (1 << 1) /* zeroing operation, may skip holes */
0138 #define IOMAP_REPORT        (1 << 2) /* report extent status, e.g. FIEMAP */
0139 #define IOMAP_FAULT     (1 << 3) /* mapping for page fault */
0140 #define IOMAP_DIRECT        (1 << 4) /* direct I/O */
0141 #define IOMAP_NOWAIT        (1 << 5) /* do not block */
0142 #define IOMAP_OVERWRITE_ONLY    (1 << 6) /* only pure overwrites allowed */
0143 #define IOMAP_UNSHARE       (1 << 7) /* unshare_file_range */
0144 #ifdef CONFIG_FS_DAX
0145 #define IOMAP_DAX       (1 << 8) /* DAX mapping */
0146 #else
0147 #define IOMAP_DAX       0
0148 #endif /* CONFIG_FS_DAX */
0149 
0150 struct iomap_ops {
0151     /*
0152      * Return the existing mapping at pos, or reserve space starting at
0153      * pos for up to length, as long as we can do it as a single mapping.
0154      * The actual length is returned in iomap->length.
0155      */
0156     int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
0157             unsigned flags, struct iomap *iomap,
0158             struct iomap *srcmap);
0159 
0160     /*
0161      * Commit and/or unreserve space previous allocated using iomap_begin.
0162      * Written indicates the length of the successful write operation which
0163      * needs to be commited, while the rest needs to be unreserved.
0164      * Written might be zero if no data was written.
0165      */
0166     int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
0167             ssize_t written, unsigned flags, struct iomap *iomap);
0168 };
0169 
0170 /**
0171  * struct iomap_iter - Iterate through a range of a file
0172  * @inode: Set at the start of the iteration and should not change.
0173  * @pos: The current file position we are operating on.  It is updated by
0174  *  calls to iomap_iter().  Treat as read-only in the body.
0175  * @len: The remaining length of the file segment we're operating on.
0176  *  It is updated at the same time as @pos.
0177  * @processed: The number of bytes processed by the body in the most recent
0178  *  iteration, or a negative errno. 0 causes the iteration to stop.
0179  * @flags: Zero or more of the iomap_begin flags above.
0180  * @iomap: Map describing the I/O iteration
0181  * @srcmap: Source map for COW operations
0182  */
0183 struct iomap_iter {
0184     struct inode *inode;
0185     loff_t pos;
0186     u64 len;
0187     s64 processed;
0188     unsigned flags;
0189     struct iomap iomap;
0190     struct iomap srcmap;
0191     void *private;
0192 };
0193 
0194 int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops);
0195 
0196 /**
0197  * iomap_length - length of the current iomap iteration
0198  * @iter: iteration structure
0199  *
0200  * Returns the length that the operation applies to for the current iteration.
0201  */
0202 static inline u64 iomap_length(const struct iomap_iter *iter)
0203 {
0204     u64 end = iter->iomap.offset + iter->iomap.length;
0205 
0206     if (iter->srcmap.type != IOMAP_HOLE)
0207         end = min(end, iter->srcmap.offset + iter->srcmap.length);
0208     return min(iter->len, end - iter->pos);
0209 }
0210 
0211 /**
0212  * iomap_iter_srcmap - return the source map for the current iomap iteration
0213  * @i: iteration structure
0214  *
0215  * Write operations on file systems with reflink support might require a
0216  * source and a destination map.  This function retourns the source map
0217  * for a given operation, which may or may no be identical to the destination
0218  * map in &i->iomap.
0219  */
0220 static inline const struct iomap *iomap_iter_srcmap(const struct iomap_iter *i)
0221 {
0222     if (i->srcmap.type != IOMAP_HOLE)
0223         return &i->srcmap;
0224     return &i->iomap;
0225 }
0226 
0227 ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
0228         const struct iomap_ops *ops);
0229 int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops);
0230 void iomap_readahead(struct readahead_control *, const struct iomap_ops *ops);
0231 bool iomap_is_partially_uptodate(struct folio *, size_t from, size_t count);
0232 bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags);
0233 void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len);
0234 int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
0235         const struct iomap_ops *ops);
0236 int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
0237         bool *did_zero, const struct iomap_ops *ops);
0238 int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
0239         const struct iomap_ops *ops);
0240 vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf,
0241             const struct iomap_ops *ops);
0242 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
0243         u64 start, u64 len, const struct iomap_ops *ops);
0244 loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
0245         const struct iomap_ops *ops);
0246 loff_t iomap_seek_data(struct inode *inode, loff_t offset,
0247         const struct iomap_ops *ops);
0248 sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
0249         const struct iomap_ops *ops);
0250 
0251 /*
0252  * Structure for writeback I/O completions.
0253  */
0254 struct iomap_ioend {
0255     struct list_head    io_list;    /* next ioend in chain */
0256     u16         io_type;
0257     u16         io_flags;   /* IOMAP_F_* */
0258     u32         io_folios;  /* folios added to ioend */
0259     struct inode        *io_inode;  /* file being written to */
0260     size_t          io_size;    /* size of the extent */
0261     loff_t          io_offset;  /* offset in the file */
0262     sector_t        io_sector;  /* start sector of ioend */
0263     struct bio      *io_bio;    /* bio being built */
0264     struct bio      io_inline_bio;  /* MUST BE LAST! */
0265 };
0266 
0267 struct iomap_writeback_ops {
0268     /*
0269      * Required, maps the blocks so that writeback can be performed on
0270      * the range starting at offset.
0271      */
0272     int (*map_blocks)(struct iomap_writepage_ctx *wpc, struct inode *inode,
0273                 loff_t offset);
0274 
0275     /*
0276      * Optional, allows the file systems to perform actions just before
0277      * submitting the bio and/or override the bio end_io handler for complex
0278      * operations like copy on write extent manipulation or unwritten extent
0279      * conversions.
0280      */
0281     int (*prepare_ioend)(struct iomap_ioend *ioend, int status);
0282 
0283     /*
0284      * Optional, allows the file system to discard state on a page where
0285      * we failed to submit any I/O.
0286      */
0287     void (*discard_folio)(struct folio *folio, loff_t pos);
0288 };
0289 
0290 struct iomap_writepage_ctx {
0291     struct iomap        iomap;
0292     struct iomap_ioend  *ioend;
0293     const struct iomap_writeback_ops *ops;
0294 };
0295 
0296 void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
0297 void iomap_ioend_try_merge(struct iomap_ioend *ioend,
0298         struct list_head *more_ioends);
0299 void iomap_sort_ioends(struct list_head *ioend_list);
0300 int iomap_writepages(struct address_space *mapping,
0301         struct writeback_control *wbc, struct iomap_writepage_ctx *wpc,
0302         const struct iomap_writeback_ops *ops);
0303 
0304 /*
0305  * Flags for direct I/O ->end_io:
0306  */
0307 #define IOMAP_DIO_UNWRITTEN (1 << 0)    /* covers unwritten extent(s) */
0308 #define IOMAP_DIO_COW       (1 << 1)    /* covers COW extent(s) */
0309 
0310 struct iomap_dio_ops {
0311     int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
0312               unsigned flags);
0313     void (*submit_io)(const struct iomap_iter *iter, struct bio *bio,
0314                   loff_t file_offset);
0315 
0316     /*
0317      * Filesystems wishing to attach private information to a direct io bio
0318      * must provide a ->submit_io method that attaches the additional
0319      * information to the bio and changes the ->bi_end_io callback to a
0320      * custom function.  This function should, at a minimum, perform any
0321      * relevant post-processing of the bio and end with a call to
0322      * iomap_dio_bio_end_io.
0323      */
0324     struct bio_set *bio_set;
0325 };
0326 
0327 /*
0328  * Wait for the I/O to complete in iomap_dio_rw even if the kiocb is not
0329  * synchronous.
0330  */
0331 #define IOMAP_DIO_FORCE_WAIT    (1 << 0)
0332 
0333 /*
0334  * Do not allocate blocks or zero partial blocks, but instead fall back to
0335  * the caller by returning -EAGAIN.  Used to optimize direct I/O writes that
0336  * are not aligned to the file system block size.
0337   */
0338 #define IOMAP_DIO_OVERWRITE_ONLY    (1 << 1)
0339 
0340 /*
0341  * When a page fault occurs, return a partial synchronous result and allow
0342  * the caller to retry the rest of the operation after dealing with the page
0343  * fault.
0344  */
0345 #define IOMAP_DIO_PARTIAL       (1 << 2)
0346 
0347 /*
0348  * The caller will sync the write if needed; do not sync it within
0349  * iomap_dio_rw.  Overrides IOMAP_DIO_FORCE_WAIT.
0350  */
0351 #define IOMAP_DIO_NOSYNC        (1 << 3)
0352 
0353 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
0354         const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
0355         unsigned int dio_flags, void *private, size_t done_before);
0356 struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
0357         const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
0358         unsigned int dio_flags, void *private, size_t done_before);
0359 ssize_t iomap_dio_complete(struct iomap_dio *dio);
0360 void iomap_dio_bio_end_io(struct bio *bio);
0361 
0362 #ifdef CONFIG_SWAP
0363 struct file;
0364 struct swap_info_struct;
0365 
0366 int iomap_swapfile_activate(struct swap_info_struct *sis,
0367         struct file *swap_file, sector_t *pagespan,
0368         const struct iomap_ops *ops);
0369 #else
0370 # define iomap_swapfile_activate(sis, swapfile, pagespan, ops)  (-EIO)
0371 #endif /* CONFIG_SWAP */
0372 
0373 #endif /* LINUX_IOMAP_H */