2 * Copyright (C) 2010 Red Hat, Inc.
3 * Copyright (c) 2016 Christoph Hellwig.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #include <linux/module.h>
15 #include <linux/compiler.h>
17 #include <linux/iomap.h>
18 #include <linux/uaccess.h>
19 #include <linux/gfp.h>
21 #include <linux/swap.h>
22 #include <linux/pagemap.h>
23 #include <linux/file.h>
24 #include <linux/uio.h>
25 #include <linux/backing-dev.h>
26 #include <linux/buffer_head.h>
27 #include <linux/dax.h>
30 typedef loff_t (*iomap_actor_t
)(struct inode
*inode
, loff_t pos
, loff_t len
,
31 void *data
, struct iomap
*iomap
);
34 * Execute a iomap write on a segment of the mapping that spans a
35 * contiguous range of pages that have identical block mapping state.
37 * This avoids the need to map pages individually, do individual allocations
38 * for each page and most importantly avoid the need for filesystem specific
39 * locking per page. Instead, all the operations are amortised over the entire
40 * range of pages. It is assumed that the filesystems will lock whatever
41 * resources they require in the iomap_begin call, and release them in the
45 iomap_apply(struct inode
*inode
, loff_t pos
, loff_t length
, unsigned flags
,
46 struct iomap_ops
*ops
, void *data
, iomap_actor_t actor
)
48 struct iomap iomap
= { 0 };
49 loff_t written
= 0, ret
;
52 * Need to map a range from start position for length bytes. This can
53 * span multiple pages - it is only guaranteed to return a range of a
54 * single type of pages (e.g. all into a hole, all mapped or all
55 * unwritten). Failure at this point has nothing to undo.
57 * If allocation is required for this range, reserve the space now so
58 * that the allocation is guaranteed to succeed later on. Once we copy
59 * the data into the page cache pages, then we cannot fail otherwise we
60 * expose transient stale data. If the reserve fails, we can safely
61 * back out at this point as there is nothing to undo.
63 ret
= ops
->iomap_begin(inode
, pos
, length
, flags
, &iomap
);
66 if (WARN_ON(iomap
.offset
> pos
))
70 * Cut down the length to the one actually provided by the filesystem,
71 * as it might not be able to give us the whole size that we requested.
73 if (iomap
.offset
+ iomap
.length
< pos
+ length
)
74 length
= iomap
.offset
+ iomap
.length
- pos
;
77 * Now that we have guaranteed that the space allocation will succeed.
78 * we can do the copy-in page by page without having to worry about
79 * failures exposing transient data.
81 written
= actor(inode
, pos
, length
, data
, &iomap
);
84 * Now the data has been copied, commit the range we've copied. This
85 * should not fail unless the filesystem has had a fatal error.
88 ret
= ops
->iomap_end(inode
, pos
, length
,
89 written
> 0 ? written
: 0,
93 return written
? written
: ret
;
97 iomap_write_failed(struct inode
*inode
, loff_t pos
, unsigned len
)
99 loff_t i_size
= i_size_read(inode
);
102 * Only truncate newly allocated pages beyoned EOF, even if the
103 * write started inside the existing inode size.
105 if (pos
+ len
> i_size
)
106 truncate_pagecache_range(inode
, max(pos
, i_size
), pos
+ len
);
110 iomap_write_begin(struct inode
*inode
, loff_t pos
, unsigned len
, unsigned flags
,
111 struct page
**pagep
, struct iomap
*iomap
)
113 pgoff_t index
= pos
>> PAGE_SHIFT
;
117 BUG_ON(pos
+ len
> iomap
->offset
+ iomap
->length
);
119 page
= grab_cache_page_write_begin(inode
->i_mapping
, index
, flags
);
123 status
= __block_write_begin_int(page
, pos
, len
, NULL
, iomap
);
124 if (unlikely(status
)) {
129 iomap_write_failed(inode
, pos
, len
);
137 iomap_write_end(struct inode
*inode
, loff_t pos
, unsigned len
,
138 unsigned copied
, struct page
*page
)
142 ret
= generic_write_end(NULL
, inode
->i_mapping
, pos
, len
,
145 iomap_write_failed(inode
, pos
, len
);
150 iomap_write_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
153 struct iov_iter
*i
= data
;
156 unsigned int flags
= AOP_FLAG_NOFS
;
159 * Copies from kernel address space cannot fail (NFSD is a big user).
161 if (!iter_is_iovec(i
))
162 flags
|= AOP_FLAG_UNINTERRUPTIBLE
;
166 unsigned long offset
; /* Offset into pagecache page */
167 unsigned long bytes
; /* Bytes to write to page */
168 size_t copied
; /* Bytes copied from user */
170 offset
= (pos
& (PAGE_SIZE
- 1));
171 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
,
178 * Bring in the user page that we will copy from _first_.
179 * Otherwise there's a nasty deadlock on copying from the
180 * same page as we're writing to, without it being marked
183 * Not only is this an optimisation, but it is also required
184 * to check that the address is actually valid, when atomic
185 * usercopies are used, below.
187 if (unlikely(iov_iter_fault_in_readable(i
, bytes
))) {
192 status
= iomap_write_begin(inode
, pos
, bytes
, flags
, &page
,
194 if (unlikely(status
))
197 if (mapping_writably_mapped(inode
->i_mapping
))
198 flush_dcache_page(page
);
200 copied
= iov_iter_copy_from_user_atomic(page
, i
, offset
, bytes
);
202 flush_dcache_page(page
);
204 status
= iomap_write_end(inode
, pos
, bytes
, copied
, page
);
205 if (unlikely(status
< 0))
211 iov_iter_advance(i
, copied
);
212 if (unlikely(copied
== 0)) {
214 * If we were unable to copy any data at all, we must
215 * fall back to a single segment length write.
217 * If we didn't fallback here, we could livelock
218 * because not all segments in the iov can be copied at
219 * once without a pagefault.
221 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
,
222 iov_iter_single_seg_count(i
));
229 balance_dirty_pages_ratelimited(inode
->i_mapping
);
230 } while (iov_iter_count(i
) && length
);
232 return written
? written
: status
;
236 iomap_file_buffered_write(struct kiocb
*iocb
, struct iov_iter
*iter
,
237 struct iomap_ops
*ops
)
239 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
240 loff_t pos
= iocb
->ki_pos
, ret
= 0, written
= 0;
242 while (iov_iter_count(iter
)) {
243 ret
= iomap_apply(inode
, pos
, iov_iter_count(iter
),
244 IOMAP_WRITE
, ops
, iter
, iomap_write_actor
);
251 return written
? written
: ret
;
253 EXPORT_SYMBOL_GPL(iomap_file_buffered_write
);
255 static int iomap_zero(struct inode
*inode
, loff_t pos
, unsigned offset
,
256 unsigned bytes
, struct iomap
*iomap
)
261 status
= iomap_write_begin(inode
, pos
, bytes
,
262 AOP_FLAG_UNINTERRUPTIBLE
| AOP_FLAG_NOFS
, &page
, iomap
);
266 zero_user(page
, offset
, bytes
);
267 mark_page_accessed(page
);
269 return iomap_write_end(inode
, pos
, bytes
, bytes
, page
);
272 static int iomap_dax_zero(loff_t pos
, unsigned offset
, unsigned bytes
,
275 sector_t sector
= iomap
->blkno
+
276 (((pos
& ~(PAGE_SIZE
- 1)) - iomap
->offset
) >> 9);
278 return __dax_zero_page_range(iomap
->bdev
, sector
, offset
, bytes
);
282 iomap_zero_range_actor(struct inode
*inode
, loff_t pos
, loff_t count
,
283 void *data
, struct iomap
*iomap
)
285 bool *did_zero
= data
;
289 /* already zeroed? we're done. */
290 if (iomap
->type
== IOMAP_HOLE
|| iomap
->type
== IOMAP_UNWRITTEN
)
294 unsigned offset
, bytes
;
296 offset
= pos
& (PAGE_SIZE
- 1); /* Within page */
297 bytes
= min_t(unsigned, PAGE_SIZE
- offset
, count
);
300 status
= iomap_dax_zero(pos
, offset
, bytes
, iomap
);
302 status
= iomap_zero(inode
, pos
, offset
, bytes
, iomap
);
317 iomap_zero_range(struct inode
*inode
, loff_t pos
, loff_t len
, bool *did_zero
,
318 struct iomap_ops
*ops
)
323 ret
= iomap_apply(inode
, pos
, len
, IOMAP_ZERO
,
324 ops
, did_zero
, iomap_zero_range_actor
);
334 EXPORT_SYMBOL_GPL(iomap_zero_range
);
337 iomap_truncate_page(struct inode
*inode
, loff_t pos
, bool *did_zero
,
338 struct iomap_ops
*ops
)
340 unsigned blocksize
= (1 << inode
->i_blkbits
);
341 unsigned off
= pos
& (blocksize
- 1);
343 /* Block boundary? Nothing to do */
346 return iomap_zero_range(inode
, pos
, blocksize
- off
, did_zero
, ops
);
348 EXPORT_SYMBOL_GPL(iomap_truncate_page
);
351 iomap_page_mkwrite_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
352 void *data
, struct iomap
*iomap
)
354 struct page
*page
= data
;
357 ret
= __block_write_begin_int(page
, pos
& ~PAGE_MASK
, length
,
362 block_commit_write(page
, 0, length
);
366 int iomap_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
,
367 struct iomap_ops
*ops
)
369 struct page
*page
= vmf
->page
;
370 struct inode
*inode
= file_inode(vma
->vm_file
);
371 unsigned long length
;
376 size
= i_size_read(inode
);
377 if ((page
->mapping
!= inode
->i_mapping
) ||
378 (page_offset(page
) > size
)) {
379 /* We overload EFAULT to mean page got truncated */
384 /* page is wholly or partially inside EOF */
385 if (((page
->index
+ 1) << PAGE_SHIFT
) > size
)
386 length
= size
& ~PAGE_MASK
;
390 offset
= page_offset(page
);
392 ret
= iomap_apply(inode
, offset
, length
, IOMAP_WRITE
,
393 ops
, page
, iomap_page_mkwrite_actor
);
394 if (unlikely(ret
<= 0))
400 set_page_dirty(page
);
401 wait_for_stable_page(page
);
407 EXPORT_SYMBOL_GPL(iomap_page_mkwrite
);
410 struct fiemap_extent_info
*fi
;
414 static int iomap_to_fiemap(struct fiemap_extent_info
*fi
,
415 struct iomap
*iomap
, u32 flags
)
417 switch (iomap
->type
) {
422 flags
|= FIEMAP_EXTENT_DELALLOC
| FIEMAP_EXTENT_UNKNOWN
;
424 case IOMAP_UNWRITTEN
:
425 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
431 if (iomap
->flags
& IOMAP_F_MERGED
)
432 flags
|= FIEMAP_EXTENT_MERGED
;
434 return fiemap_fill_next_extent(fi
, iomap
->offset
,
435 iomap
->blkno
!= IOMAP_NULL_BLOCK
? iomap
->blkno
<< 9: 0,
436 iomap
->length
, flags
);
441 iomap_fiemap_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
444 struct fiemap_ctx
*ctx
= data
;
447 if (iomap
->type
== IOMAP_HOLE
)
450 ret
= iomap_to_fiemap(ctx
->fi
, &ctx
->prev
, 0);
453 case 0: /* success */
455 case 1: /* extent array full */
462 int iomap_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fi
,
463 loff_t start
, loff_t len
, struct iomap_ops
*ops
)
465 struct fiemap_ctx ctx
;
468 memset(&ctx
, 0, sizeof(ctx
));
470 ctx
.prev
.type
= IOMAP_HOLE
;
472 ret
= fiemap_check_flags(fi
, FIEMAP_FLAG_SYNC
);
476 if (fi
->fi_flags
& FIEMAP_FLAG_SYNC
) {
477 ret
= filemap_write_and_wait(inode
->i_mapping
);
483 ret
= iomap_apply(inode
, start
, len
, 0, ops
, &ctx
,
485 /* inode with no (attribute) mapping will give ENOENT */
497 if (ctx
.prev
.type
!= IOMAP_HOLE
) {
498 ret
= iomap_to_fiemap(fi
, &ctx
.prev
, FIEMAP_EXTENT_LAST
);
505 EXPORT_SYMBOL_GPL(iomap_fiemap
);