]>
Commit | Line | Data |
---|---|---|
ae259a9c CH |
1 | /* |
2 | * Copyright (C) 2010 Red Hat, Inc. | |
3 | * Copyright (c) 2016 Christoph Hellwig. | |
4 | * | |
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. | |
8 | * | |
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 | |
12 | * more details. | |
13 | */ | |
14 | #include <linux/module.h> | |
15 | #include <linux/compiler.h> | |
16 | #include <linux/fs.h> | |
17 | #include <linux/iomap.h> | |
18 | #include <linux/uaccess.h> | |
19 | #include <linux/gfp.h> | |
20 | #include <linux/mm.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> | |
9a286f0e | 27 | #include <linux/dax.h> |
ae259a9c CH |
28 | #include "internal.h" |
29 | ||
30 | typedef loff_t (*iomap_actor_t)(struct inode *inode, loff_t pos, loff_t len, | |
31 | void *data, struct iomap *iomap); | |
32 | ||
33 | /* | |
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. | |
36 | * | |
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 | |
42 | * iomap_end call. | |
43 | */ | |
44 | static loff_t | |
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) | |
47 | { | |
48 | struct iomap iomap = { 0 }; | |
49 | loff_t written = 0, ret; | |
50 | ||
51 | /* | |
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. | |
56 | * | |
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. | |
62 | */ | |
63 | ret = ops->iomap_begin(inode, pos, length, flags, &iomap); | |
64 | if (ret) | |
65 | return ret; | |
66 | if (WARN_ON(iomap.offset > pos)) | |
67 | return -EIO; | |
68 | ||
69 | /* | |
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. | |
72 | */ | |
73 | if (iomap.offset + iomap.length < pos + length) | |
74 | length = iomap.offset + iomap.length - pos; | |
75 | ||
76 | /* | |
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. | |
80 | */ | |
81 | written = actor(inode, pos, length, data, &iomap); | |
82 | ||
83 | /* | |
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. | |
86 | */ | |
f20ac7ab CH |
87 | if (ops->iomap_end) { |
88 | ret = ops->iomap_end(inode, pos, length, | |
89 | written > 0 ? written : 0, | |
90 | flags, &iomap); | |
91 | } | |
ae259a9c CH |
92 | |
93 | return written ? written : ret; | |
94 | } | |
95 | ||
96 | static void | |
97 | iomap_write_failed(struct inode *inode, loff_t pos, unsigned len) | |
98 | { | |
99 | loff_t i_size = i_size_read(inode); | |
100 | ||
101 | /* | |
102 | * Only truncate newly allocated pages beyoned EOF, even if the | |
103 | * write started inside the existing inode size. | |
104 | */ | |
105 | if (pos + len > i_size) | |
106 | truncate_pagecache_range(inode, max(pos, i_size), pos + len); | |
107 | } | |
108 | ||
109 | static int | |
110 | iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags, | |
111 | struct page **pagep, struct iomap *iomap) | |
112 | { | |
113 | pgoff_t index = pos >> PAGE_SHIFT; | |
114 | struct page *page; | |
115 | int status = 0; | |
116 | ||
117 | BUG_ON(pos + len > iomap->offset + iomap->length); | |
118 | ||
119 | page = grab_cache_page_write_begin(inode->i_mapping, index, flags); | |
120 | if (!page) | |
121 | return -ENOMEM; | |
122 | ||
123 | status = __block_write_begin_int(page, pos, len, NULL, iomap); | |
124 | if (unlikely(status)) { | |
125 | unlock_page(page); | |
126 | put_page(page); | |
127 | page = NULL; | |
128 | ||
129 | iomap_write_failed(inode, pos, len); | |
130 | } | |
131 | ||
132 | *pagep = page; | |
133 | return status; | |
134 | } | |
135 | ||
136 | static int | |
137 | iomap_write_end(struct inode *inode, loff_t pos, unsigned len, | |
138 | unsigned copied, struct page *page) | |
139 | { | |
140 | int ret; | |
141 | ||
142 | ret = generic_write_end(NULL, inode->i_mapping, pos, len, | |
143 | copied, page, NULL); | |
144 | if (ret < len) | |
145 | iomap_write_failed(inode, pos, len); | |
146 | return ret; | |
147 | } | |
148 | ||
149 | static loff_t | |
150 | iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data, | |
151 | struct iomap *iomap) | |
152 | { | |
153 | struct iov_iter *i = data; | |
154 | long status = 0; | |
155 | ssize_t written = 0; | |
156 | unsigned int flags = AOP_FLAG_NOFS; | |
157 | ||
158 | /* | |
159 | * Copies from kernel address space cannot fail (NFSD is a big user). | |
160 | */ | |
161 | if (!iter_is_iovec(i)) | |
162 | flags |= AOP_FLAG_UNINTERRUPTIBLE; | |
163 | ||
164 | do { | |
165 | struct page *page; | |
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 */ | |
169 | ||
170 | offset = (pos & (PAGE_SIZE - 1)); | |
171 | bytes = min_t(unsigned long, PAGE_SIZE - offset, | |
172 | iov_iter_count(i)); | |
173 | again: | |
174 | if (bytes > length) | |
175 | bytes = length; | |
176 | ||
177 | /* | |
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 | |
181 | * up-to-date. | |
182 | * | |
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. | |
186 | */ | |
187 | if (unlikely(iov_iter_fault_in_readable(i, bytes))) { | |
188 | status = -EFAULT; | |
189 | break; | |
190 | } | |
191 | ||
192 | status = iomap_write_begin(inode, pos, bytes, flags, &page, | |
193 | iomap); | |
194 | if (unlikely(status)) | |
195 | break; | |
196 | ||
197 | if (mapping_writably_mapped(inode->i_mapping)) | |
198 | flush_dcache_page(page); | |
199 | ||
ae259a9c | 200 | copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes); |
ae259a9c CH |
201 | |
202 | flush_dcache_page(page); | |
ae259a9c CH |
203 | |
204 | status = iomap_write_end(inode, pos, bytes, copied, page); | |
205 | if (unlikely(status < 0)) | |
206 | break; | |
207 | copied = status; | |
208 | ||
209 | cond_resched(); | |
210 | ||
211 | iov_iter_advance(i, copied); | |
212 | if (unlikely(copied == 0)) { | |
213 | /* | |
214 | * If we were unable to copy any data at all, we must | |
215 | * fall back to a single segment length write. | |
216 | * | |
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. | |
220 | */ | |
221 | bytes = min_t(unsigned long, PAGE_SIZE - offset, | |
222 | iov_iter_single_seg_count(i)); | |
223 | goto again; | |
224 | } | |
225 | pos += copied; | |
226 | written += copied; | |
227 | length -= copied; | |
228 | ||
229 | balance_dirty_pages_ratelimited(inode->i_mapping); | |
230 | } while (iov_iter_count(i) && length); | |
231 | ||
232 | return written ? written : status; | |
233 | } | |
234 | ||
235 | ssize_t | |
236 | iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter, | |
237 | struct iomap_ops *ops) | |
238 | { | |
239 | struct inode *inode = iocb->ki_filp->f_mapping->host; | |
240 | loff_t pos = iocb->ki_pos, ret = 0, written = 0; | |
241 | ||
242 | while (iov_iter_count(iter)) { | |
243 | ret = iomap_apply(inode, pos, iov_iter_count(iter), | |
244 | IOMAP_WRITE, ops, iter, iomap_write_actor); | |
245 | if (ret <= 0) | |
246 | break; | |
247 | pos += ret; | |
248 | written += ret; | |
249 | } | |
250 | ||
251 | return written ? written : ret; | |
252 | } | |
253 | EXPORT_SYMBOL_GPL(iomap_file_buffered_write); | |
254 | ||
5f4e5752 CH |
255 | static struct page * |
256 | __iomap_read_page(struct inode *inode, loff_t offset) | |
257 | { | |
258 | struct address_space *mapping = inode->i_mapping; | |
259 | struct page *page; | |
260 | ||
261 | page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL); | |
262 | if (IS_ERR(page)) | |
263 | return page; | |
264 | if (!PageUptodate(page)) { | |
265 | put_page(page); | |
266 | return ERR_PTR(-EIO); | |
267 | } | |
268 | return page; | |
269 | } | |
270 | ||
271 | static loff_t | |
272 | iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data, | |
273 | struct iomap *iomap) | |
274 | { | |
275 | long status = 0; | |
276 | ssize_t written = 0; | |
277 | ||
278 | do { | |
279 | struct page *page, *rpage; | |
280 | unsigned long offset; /* Offset into pagecache page */ | |
281 | unsigned long bytes; /* Bytes to write to page */ | |
282 | ||
283 | offset = (pos & (PAGE_SIZE - 1)); | |
284 | bytes = min_t(unsigned long, PAGE_SIZE - offset, length); | |
285 | ||
286 | rpage = __iomap_read_page(inode, pos); | |
287 | if (IS_ERR(rpage)) | |
288 | return PTR_ERR(rpage); | |
289 | ||
290 | status = iomap_write_begin(inode, pos, bytes, | |
291 | AOP_FLAG_NOFS | AOP_FLAG_UNINTERRUPTIBLE, | |
292 | &page, iomap); | |
293 | put_page(rpage); | |
294 | if (unlikely(status)) | |
295 | return status; | |
296 | ||
297 | WARN_ON_ONCE(!PageUptodate(page)); | |
298 | ||
299 | status = iomap_write_end(inode, pos, bytes, bytes, page); | |
300 | if (unlikely(status <= 0)) { | |
301 | if (WARN_ON_ONCE(status == 0)) | |
302 | return -EIO; | |
303 | return status; | |
304 | } | |
305 | ||
306 | cond_resched(); | |
307 | ||
308 | pos += status; | |
309 | written += status; | |
310 | length -= status; | |
311 | ||
312 | balance_dirty_pages_ratelimited(inode->i_mapping); | |
313 | } while (length); | |
314 | ||
315 | return written; | |
316 | } | |
317 | ||
318 | int | |
319 | iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len, | |
320 | struct iomap_ops *ops) | |
321 | { | |
322 | loff_t ret; | |
323 | ||
324 | while (len) { | |
325 | ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL, | |
326 | iomap_dirty_actor); | |
327 | if (ret <= 0) | |
328 | return ret; | |
329 | pos += ret; | |
330 | len -= ret; | |
331 | } | |
332 | ||
333 | return 0; | |
334 | } | |
335 | EXPORT_SYMBOL_GPL(iomap_file_dirty); | |
336 | ||
ae259a9c CH |
337 | static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset, |
338 | unsigned bytes, struct iomap *iomap) | |
339 | { | |
340 | struct page *page; | |
341 | int status; | |
342 | ||
343 | status = iomap_write_begin(inode, pos, bytes, | |
344 | AOP_FLAG_UNINTERRUPTIBLE | AOP_FLAG_NOFS, &page, iomap); | |
345 | if (status) | |
346 | return status; | |
347 | ||
348 | zero_user(page, offset, bytes); | |
349 | mark_page_accessed(page); | |
350 | ||
351 | return iomap_write_end(inode, pos, bytes, bytes, page); | |
352 | } | |
353 | ||
9a286f0e CH |
354 | static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes, |
355 | struct iomap *iomap) | |
356 | { | |
357 | sector_t sector = iomap->blkno + | |
358 | (((pos & ~(PAGE_SIZE - 1)) - iomap->offset) >> 9); | |
359 | ||
360 | return __dax_zero_page_range(iomap->bdev, sector, offset, bytes); | |
361 | } | |
362 | ||
ae259a9c CH |
363 | static loff_t |
364 | iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count, | |
365 | void *data, struct iomap *iomap) | |
366 | { | |
367 | bool *did_zero = data; | |
368 | loff_t written = 0; | |
369 | int status; | |
370 | ||
371 | /* already zeroed? we're done. */ | |
372 | if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN) | |
373 | return count; | |
374 | ||
375 | do { | |
376 | unsigned offset, bytes; | |
377 | ||
378 | offset = pos & (PAGE_SIZE - 1); /* Within page */ | |
379 | bytes = min_t(unsigned, PAGE_SIZE - offset, count); | |
380 | ||
9a286f0e CH |
381 | if (IS_DAX(inode)) |
382 | status = iomap_dax_zero(pos, offset, bytes, iomap); | |
383 | else | |
384 | status = iomap_zero(inode, pos, offset, bytes, iomap); | |
ae259a9c CH |
385 | if (status < 0) |
386 | return status; | |
387 | ||
388 | pos += bytes; | |
389 | count -= bytes; | |
390 | written += bytes; | |
391 | if (did_zero) | |
392 | *did_zero = true; | |
393 | } while (count > 0); | |
394 | ||
395 | return written; | |
396 | } | |
397 | ||
398 | int | |
399 | iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero, | |
400 | struct iomap_ops *ops) | |
401 | { | |
402 | loff_t ret; | |
403 | ||
404 | while (len > 0) { | |
405 | ret = iomap_apply(inode, pos, len, IOMAP_ZERO, | |
406 | ops, did_zero, iomap_zero_range_actor); | |
407 | if (ret <= 0) | |
408 | return ret; | |
409 | ||
410 | pos += ret; | |
411 | len -= ret; | |
412 | } | |
413 | ||
414 | return 0; | |
415 | } | |
416 | EXPORT_SYMBOL_GPL(iomap_zero_range); | |
417 | ||
418 | int | |
419 | iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero, | |
420 | struct iomap_ops *ops) | |
421 | { | |
422 | unsigned blocksize = (1 << inode->i_blkbits); | |
423 | unsigned off = pos & (blocksize - 1); | |
424 | ||
425 | /* Block boundary? Nothing to do */ | |
426 | if (!off) | |
427 | return 0; | |
428 | return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops); | |
429 | } | |
430 | EXPORT_SYMBOL_GPL(iomap_truncate_page); | |
431 | ||
432 | static loff_t | |
433 | iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length, | |
434 | void *data, struct iomap *iomap) | |
435 | { | |
436 | struct page *page = data; | |
437 | int ret; | |
438 | ||
439 | ret = __block_write_begin_int(page, pos & ~PAGE_MASK, length, | |
440 | NULL, iomap); | |
441 | if (ret) | |
442 | return ret; | |
443 | ||
444 | block_commit_write(page, 0, length); | |
445 | return length; | |
446 | } | |
447 | ||
448 | int iomap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf, | |
449 | struct iomap_ops *ops) | |
450 | { | |
451 | struct page *page = vmf->page; | |
452 | struct inode *inode = file_inode(vma->vm_file); | |
453 | unsigned long length; | |
454 | loff_t offset, size; | |
455 | ssize_t ret; | |
456 | ||
457 | lock_page(page); | |
458 | size = i_size_read(inode); | |
459 | if ((page->mapping != inode->i_mapping) || | |
460 | (page_offset(page) > size)) { | |
461 | /* We overload EFAULT to mean page got truncated */ | |
462 | ret = -EFAULT; | |
463 | goto out_unlock; | |
464 | } | |
465 | ||
466 | /* page is wholly or partially inside EOF */ | |
467 | if (((page->index + 1) << PAGE_SHIFT) > size) | |
468 | length = size & ~PAGE_MASK; | |
469 | else | |
470 | length = PAGE_SIZE; | |
471 | ||
472 | offset = page_offset(page); | |
473 | while (length > 0) { | |
474 | ret = iomap_apply(inode, offset, length, IOMAP_WRITE, | |
475 | ops, page, iomap_page_mkwrite_actor); | |
476 | if (unlikely(ret <= 0)) | |
477 | goto out_unlock; | |
478 | offset += ret; | |
479 | length -= ret; | |
480 | } | |
481 | ||
482 | set_page_dirty(page); | |
483 | wait_for_stable_page(page); | |
484 | return 0; | |
485 | out_unlock: | |
486 | unlock_page(page); | |
487 | return ret; | |
488 | } | |
489 | EXPORT_SYMBOL_GPL(iomap_page_mkwrite); | |
8be9f564 CH |
490 | |
491 | struct fiemap_ctx { | |
492 | struct fiemap_extent_info *fi; | |
493 | struct iomap prev; | |
494 | }; | |
495 | ||
496 | static int iomap_to_fiemap(struct fiemap_extent_info *fi, | |
497 | struct iomap *iomap, u32 flags) | |
498 | { | |
499 | switch (iomap->type) { | |
500 | case IOMAP_HOLE: | |
501 | /* skip holes */ | |
502 | return 0; | |
503 | case IOMAP_DELALLOC: | |
504 | flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN; | |
505 | break; | |
506 | case IOMAP_UNWRITTEN: | |
507 | flags |= FIEMAP_EXTENT_UNWRITTEN; | |
508 | break; | |
509 | case IOMAP_MAPPED: | |
510 | break; | |
511 | } | |
512 | ||
17de0a9f CH |
513 | if (iomap->flags & IOMAP_F_MERGED) |
514 | flags |= FIEMAP_EXTENT_MERGED; | |
515 | ||
8be9f564 CH |
516 | return fiemap_fill_next_extent(fi, iomap->offset, |
517 | iomap->blkno != IOMAP_NULL_BLOCK ? iomap->blkno << 9: 0, | |
17de0a9f | 518 | iomap->length, flags); |
8be9f564 CH |
519 | |
520 | } | |
521 | ||
522 | static loff_t | |
523 | iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, | |
524 | struct iomap *iomap) | |
525 | { | |
526 | struct fiemap_ctx *ctx = data; | |
527 | loff_t ret = length; | |
528 | ||
529 | if (iomap->type == IOMAP_HOLE) | |
530 | return length; | |
531 | ||
532 | ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0); | |
533 | ctx->prev = *iomap; | |
534 | switch (ret) { | |
535 | case 0: /* success */ | |
536 | return length; | |
537 | case 1: /* extent array full */ | |
538 | return 0; | |
539 | default: | |
540 | return ret; | |
541 | } | |
542 | } | |
543 | ||
544 | int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi, | |
545 | loff_t start, loff_t len, struct iomap_ops *ops) | |
546 | { | |
547 | struct fiemap_ctx ctx; | |
548 | loff_t ret; | |
549 | ||
550 | memset(&ctx, 0, sizeof(ctx)); | |
551 | ctx.fi = fi; | |
552 | ctx.prev.type = IOMAP_HOLE; | |
553 | ||
554 | ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC); | |
555 | if (ret) | |
556 | return ret; | |
557 | ||
8896b8f6 DC |
558 | if (fi->fi_flags & FIEMAP_FLAG_SYNC) { |
559 | ret = filemap_write_and_wait(inode->i_mapping); | |
560 | if (ret) | |
561 | return ret; | |
562 | } | |
8be9f564 CH |
563 | |
564 | while (len > 0) { | |
565 | ret = iomap_apply(inode, start, len, 0, ops, &ctx, | |
566 | iomap_fiemap_actor); | |
ac2dc058 DC |
567 | /* inode with no (attribute) mapping will give ENOENT */ |
568 | if (ret == -ENOENT) | |
569 | break; | |
8be9f564 CH |
570 | if (ret < 0) |
571 | return ret; | |
572 | if (ret == 0) | |
573 | break; | |
574 | ||
575 | start += ret; | |
576 | len -= ret; | |
577 | } | |
578 | ||
579 | if (ctx.prev.type != IOMAP_HOLE) { | |
580 | ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST); | |
581 | if (ret < 0) | |
582 | return ret; | |
583 | } | |
584 | ||
585 | return 0; | |
586 | } | |
587 | EXPORT_SYMBOL_GPL(iomap_fiemap); |