Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 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> |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 27 | #include <linux/task_io_accounting_ops.h> |
Christoph Hellwig | 9a286f0 | 2016-06-21 09:31:39 +1000 | [diff] [blame] | 28 | #include <linux/dax.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 29 | #include <linux/sched/signal.h> |
| 30 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 31 | #include "internal.h" |
| 32 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 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 | */ |
Christoph Hellwig | befb503 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 44 | loff_t |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 45 | iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 46 | const struct iomap_ops *ops, void *data, iomap_actor_t actor) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 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 | */ |
Christoph Hellwig | f20ac7a | 2016-08-17 08:42:34 +1000 | [diff] [blame] | 87 | if (ops->iomap_end) { |
| 88 | ret = ops->iomap_end(inode, pos, length, |
| 89 | written > 0 ? written : 0, |
| 90 | flags, &iomap); |
| 91 | } |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 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 | |
Michal Hocko | d1908f5 | 2017-02-03 13:13:26 -0800 | [diff] [blame] | 119 | if (fatal_signal_pending(current)) |
| 120 | return -EINTR; |
| 121 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 122 | page = grab_cache_page_write_begin(inode->i_mapping, index, flags); |
| 123 | if (!page) |
| 124 | return -ENOMEM; |
| 125 | |
| 126 | status = __block_write_begin_int(page, pos, len, NULL, iomap); |
| 127 | if (unlikely(status)) { |
| 128 | unlock_page(page); |
| 129 | put_page(page); |
| 130 | page = NULL; |
| 131 | |
| 132 | iomap_write_failed(inode, pos, len); |
| 133 | } |
| 134 | |
| 135 | *pagep = page; |
| 136 | return status; |
| 137 | } |
| 138 | |
| 139 | static int |
| 140 | iomap_write_end(struct inode *inode, loff_t pos, unsigned len, |
| 141 | unsigned copied, struct page *page) |
| 142 | { |
| 143 | int ret; |
| 144 | |
| 145 | ret = generic_write_end(NULL, inode->i_mapping, pos, len, |
| 146 | copied, page, NULL); |
| 147 | if (ret < len) |
| 148 | iomap_write_failed(inode, pos, len); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | static loff_t |
| 153 | iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data, |
| 154 | struct iomap *iomap) |
| 155 | { |
| 156 | struct iov_iter *i = data; |
| 157 | long status = 0; |
| 158 | ssize_t written = 0; |
| 159 | unsigned int flags = AOP_FLAG_NOFS; |
| 160 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 161 | do { |
| 162 | struct page *page; |
| 163 | unsigned long offset; /* Offset into pagecache page */ |
| 164 | unsigned long bytes; /* Bytes to write to page */ |
| 165 | size_t copied; /* Bytes copied from user */ |
| 166 | |
| 167 | offset = (pos & (PAGE_SIZE - 1)); |
| 168 | bytes = min_t(unsigned long, PAGE_SIZE - offset, |
| 169 | iov_iter_count(i)); |
| 170 | again: |
| 171 | if (bytes > length) |
| 172 | bytes = length; |
| 173 | |
| 174 | /* |
| 175 | * Bring in the user page that we will copy from _first_. |
| 176 | * Otherwise there's a nasty deadlock on copying from the |
| 177 | * same page as we're writing to, without it being marked |
| 178 | * up-to-date. |
| 179 | * |
| 180 | * Not only is this an optimisation, but it is also required |
| 181 | * to check that the address is actually valid, when atomic |
| 182 | * usercopies are used, below. |
| 183 | */ |
| 184 | if (unlikely(iov_iter_fault_in_readable(i, bytes))) { |
| 185 | status = -EFAULT; |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | status = iomap_write_begin(inode, pos, bytes, flags, &page, |
| 190 | iomap); |
| 191 | if (unlikely(status)) |
| 192 | break; |
| 193 | |
| 194 | if (mapping_writably_mapped(inode->i_mapping)) |
| 195 | flush_dcache_page(page); |
| 196 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 197 | copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 198 | |
| 199 | flush_dcache_page(page); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 200 | |
| 201 | status = iomap_write_end(inode, pos, bytes, copied, page); |
| 202 | if (unlikely(status < 0)) |
| 203 | break; |
| 204 | copied = status; |
| 205 | |
| 206 | cond_resched(); |
| 207 | |
| 208 | iov_iter_advance(i, copied); |
| 209 | if (unlikely(copied == 0)) { |
| 210 | /* |
| 211 | * If we were unable to copy any data at all, we must |
| 212 | * fall back to a single segment length write. |
| 213 | * |
| 214 | * If we didn't fallback here, we could livelock |
| 215 | * because not all segments in the iov can be copied at |
| 216 | * once without a pagefault. |
| 217 | */ |
| 218 | bytes = min_t(unsigned long, PAGE_SIZE - offset, |
| 219 | iov_iter_single_seg_count(i)); |
| 220 | goto again; |
| 221 | } |
| 222 | pos += copied; |
| 223 | written += copied; |
| 224 | length -= copied; |
| 225 | |
| 226 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 227 | } while (iov_iter_count(i) && length); |
| 228 | |
| 229 | return written ? written : status; |
| 230 | } |
| 231 | |
| 232 | ssize_t |
| 233 | iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 234 | const struct iomap_ops *ops) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 235 | { |
| 236 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
| 237 | loff_t pos = iocb->ki_pos, ret = 0, written = 0; |
| 238 | |
| 239 | while (iov_iter_count(iter)) { |
| 240 | ret = iomap_apply(inode, pos, iov_iter_count(iter), |
| 241 | IOMAP_WRITE, ops, iter, iomap_write_actor); |
| 242 | if (ret <= 0) |
| 243 | break; |
| 244 | pos += ret; |
| 245 | written += ret; |
| 246 | } |
| 247 | |
| 248 | return written ? written : ret; |
| 249 | } |
| 250 | EXPORT_SYMBOL_GPL(iomap_file_buffered_write); |
| 251 | |
Christoph Hellwig | 5f4e575 | 2016-09-19 10:12:45 +1000 | [diff] [blame] | 252 | static struct page * |
| 253 | __iomap_read_page(struct inode *inode, loff_t offset) |
| 254 | { |
| 255 | struct address_space *mapping = inode->i_mapping; |
| 256 | struct page *page; |
| 257 | |
| 258 | page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL); |
| 259 | if (IS_ERR(page)) |
| 260 | return page; |
| 261 | if (!PageUptodate(page)) { |
| 262 | put_page(page); |
| 263 | return ERR_PTR(-EIO); |
| 264 | } |
| 265 | return page; |
| 266 | } |
| 267 | |
| 268 | static loff_t |
| 269 | iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data, |
| 270 | struct iomap *iomap) |
| 271 | { |
| 272 | long status = 0; |
| 273 | ssize_t written = 0; |
| 274 | |
| 275 | do { |
| 276 | struct page *page, *rpage; |
| 277 | unsigned long offset; /* Offset into pagecache page */ |
| 278 | unsigned long bytes; /* Bytes to write to page */ |
| 279 | |
| 280 | offset = (pos & (PAGE_SIZE - 1)); |
Christoph Hellwig | e28ae8e | 2017-08-11 12:45:35 -0700 | [diff] [blame] | 281 | bytes = min_t(loff_t, PAGE_SIZE - offset, length); |
Christoph Hellwig | 5f4e575 | 2016-09-19 10:12:45 +1000 | [diff] [blame] | 282 | |
| 283 | rpage = __iomap_read_page(inode, pos); |
| 284 | if (IS_ERR(rpage)) |
| 285 | return PTR_ERR(rpage); |
| 286 | |
| 287 | status = iomap_write_begin(inode, pos, bytes, |
Tetsuo Handa | c718a97 | 2017-05-08 15:58:59 -0700 | [diff] [blame] | 288 | AOP_FLAG_NOFS, &page, iomap); |
Christoph Hellwig | 5f4e575 | 2016-09-19 10:12:45 +1000 | [diff] [blame] | 289 | put_page(rpage); |
| 290 | if (unlikely(status)) |
| 291 | return status; |
| 292 | |
| 293 | WARN_ON_ONCE(!PageUptodate(page)); |
| 294 | |
| 295 | status = iomap_write_end(inode, pos, bytes, bytes, page); |
| 296 | if (unlikely(status <= 0)) { |
| 297 | if (WARN_ON_ONCE(status == 0)) |
| 298 | return -EIO; |
| 299 | return status; |
| 300 | } |
| 301 | |
| 302 | cond_resched(); |
| 303 | |
| 304 | pos += status; |
| 305 | written += status; |
| 306 | length -= status; |
| 307 | |
| 308 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 309 | } while (length); |
| 310 | |
| 311 | return written; |
| 312 | } |
| 313 | |
| 314 | int |
| 315 | iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 316 | const struct iomap_ops *ops) |
Christoph Hellwig | 5f4e575 | 2016-09-19 10:12:45 +1000 | [diff] [blame] | 317 | { |
| 318 | loff_t ret; |
| 319 | |
| 320 | while (len) { |
| 321 | ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL, |
| 322 | iomap_dirty_actor); |
| 323 | if (ret <= 0) |
| 324 | return ret; |
| 325 | pos += ret; |
| 326 | len -= ret; |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | EXPORT_SYMBOL_GPL(iomap_file_dirty); |
| 332 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 333 | static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset, |
| 334 | unsigned bytes, struct iomap *iomap) |
| 335 | { |
| 336 | struct page *page; |
| 337 | int status; |
| 338 | |
Tetsuo Handa | c718a97 | 2017-05-08 15:58:59 -0700 | [diff] [blame] | 339 | status = iomap_write_begin(inode, pos, bytes, AOP_FLAG_NOFS, &page, |
| 340 | iomap); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 341 | if (status) |
| 342 | return status; |
| 343 | |
| 344 | zero_user(page, offset, bytes); |
| 345 | mark_page_accessed(page); |
| 346 | |
| 347 | return iomap_write_end(inode, pos, bytes, bytes, page); |
| 348 | } |
| 349 | |
Christoph Hellwig | 9a286f0 | 2016-06-21 09:31:39 +1000 | [diff] [blame] | 350 | static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes, |
| 351 | struct iomap *iomap) |
| 352 | { |
Andreas Gruenbacher | 19fe5f6 | 2017-10-01 17:55:54 -0400 | [diff] [blame] | 353 | sector_t sector = (iomap->addr + |
| 354 | (pos & PAGE_MASK) - iomap->offset) >> 9; |
Christoph Hellwig | 9a286f0 | 2016-06-21 09:31:39 +1000 | [diff] [blame] | 355 | |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 356 | return __dax_zero_page_range(iomap->bdev, iomap->dax_dev, sector, |
| 357 | offset, bytes); |
Christoph Hellwig | 9a286f0 | 2016-06-21 09:31:39 +1000 | [diff] [blame] | 358 | } |
| 359 | |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 360 | static loff_t |
| 361 | iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count, |
| 362 | void *data, struct iomap *iomap) |
| 363 | { |
| 364 | bool *did_zero = data; |
| 365 | loff_t written = 0; |
| 366 | int status; |
| 367 | |
| 368 | /* already zeroed? we're done. */ |
| 369 | if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN) |
| 370 | return count; |
| 371 | |
| 372 | do { |
| 373 | unsigned offset, bytes; |
| 374 | |
| 375 | offset = pos & (PAGE_SIZE - 1); /* Within page */ |
Christoph Hellwig | e28ae8e | 2017-08-11 12:45:35 -0700 | [diff] [blame] | 376 | bytes = min_t(loff_t, PAGE_SIZE - offset, count); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 377 | |
Christoph Hellwig | 9a286f0 | 2016-06-21 09:31:39 +1000 | [diff] [blame] | 378 | if (IS_DAX(inode)) |
| 379 | status = iomap_dax_zero(pos, offset, bytes, iomap); |
| 380 | else |
| 381 | status = iomap_zero(inode, pos, offset, bytes, iomap); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 382 | if (status < 0) |
| 383 | return status; |
| 384 | |
| 385 | pos += bytes; |
| 386 | count -= bytes; |
| 387 | written += bytes; |
| 388 | if (did_zero) |
| 389 | *did_zero = true; |
| 390 | } while (count > 0); |
| 391 | |
| 392 | return written; |
| 393 | } |
| 394 | |
| 395 | int |
| 396 | iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 397 | const struct iomap_ops *ops) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 398 | { |
| 399 | loff_t ret; |
| 400 | |
| 401 | while (len > 0) { |
| 402 | ret = iomap_apply(inode, pos, len, IOMAP_ZERO, |
| 403 | ops, did_zero, iomap_zero_range_actor); |
| 404 | if (ret <= 0) |
| 405 | return ret; |
| 406 | |
| 407 | pos += ret; |
| 408 | len -= ret; |
| 409 | } |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | EXPORT_SYMBOL_GPL(iomap_zero_range); |
| 414 | |
| 415 | int |
| 416 | iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 417 | const struct iomap_ops *ops) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 418 | { |
Fabian Frederick | 9340747 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 419 | unsigned int blocksize = i_blocksize(inode); |
| 420 | unsigned int off = pos & (blocksize - 1); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 421 | |
| 422 | /* Block boundary? Nothing to do */ |
| 423 | if (!off) |
| 424 | return 0; |
| 425 | return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops); |
| 426 | } |
| 427 | EXPORT_SYMBOL_GPL(iomap_truncate_page); |
| 428 | |
| 429 | static loff_t |
| 430 | iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length, |
| 431 | void *data, struct iomap *iomap) |
| 432 | { |
| 433 | struct page *page = data; |
| 434 | int ret; |
| 435 | |
Jan Kara | c663e29 | 2016-10-24 14:20:25 +1100 | [diff] [blame] | 436 | ret = __block_write_begin_int(page, pos, length, NULL, iomap); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 437 | if (ret) |
| 438 | return ret; |
| 439 | |
| 440 | block_commit_write(page, 0, length); |
| 441 | return length; |
| 442 | } |
| 443 | |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 444 | int iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops) |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 445 | { |
| 446 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 447 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 448 | unsigned long length; |
| 449 | loff_t offset, size; |
| 450 | ssize_t ret; |
| 451 | |
| 452 | lock_page(page); |
| 453 | size = i_size_read(inode); |
| 454 | if ((page->mapping != inode->i_mapping) || |
| 455 | (page_offset(page) > size)) { |
| 456 | /* We overload EFAULT to mean page got truncated */ |
| 457 | ret = -EFAULT; |
| 458 | goto out_unlock; |
| 459 | } |
| 460 | |
| 461 | /* page is wholly or partially inside EOF */ |
| 462 | if (((page->index + 1) << PAGE_SHIFT) > size) |
| 463 | length = size & ~PAGE_MASK; |
| 464 | else |
| 465 | length = PAGE_SIZE; |
| 466 | |
| 467 | offset = page_offset(page); |
| 468 | while (length > 0) { |
Jan Kara | 9484ab1 | 2016-11-10 10:26:50 +1100 | [diff] [blame] | 469 | ret = iomap_apply(inode, offset, length, |
| 470 | IOMAP_WRITE | IOMAP_FAULT, ops, page, |
| 471 | iomap_page_mkwrite_actor); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 472 | if (unlikely(ret <= 0)) |
| 473 | goto out_unlock; |
| 474 | offset += ret; |
| 475 | length -= ret; |
| 476 | } |
| 477 | |
| 478 | set_page_dirty(page); |
| 479 | wait_for_stable_page(page); |
Christoph Hellwig | e7647fb | 2017-08-29 10:08:41 -0700 | [diff] [blame] | 480 | return VM_FAULT_LOCKED; |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 481 | out_unlock: |
| 482 | unlock_page(page); |
Christoph Hellwig | e7647fb | 2017-08-29 10:08:41 -0700 | [diff] [blame] | 483 | return block_page_mkwrite_return(ret); |
Christoph Hellwig | ae259a9 | 2016-06-21 09:23:11 +1000 | [diff] [blame] | 484 | } |
| 485 | EXPORT_SYMBOL_GPL(iomap_page_mkwrite); |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 486 | |
| 487 | struct fiemap_ctx { |
| 488 | struct fiemap_extent_info *fi; |
| 489 | struct iomap prev; |
| 490 | }; |
| 491 | |
| 492 | static int iomap_to_fiemap(struct fiemap_extent_info *fi, |
| 493 | struct iomap *iomap, u32 flags) |
| 494 | { |
| 495 | switch (iomap->type) { |
| 496 | case IOMAP_HOLE: |
| 497 | /* skip holes */ |
| 498 | return 0; |
| 499 | case IOMAP_DELALLOC: |
| 500 | flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN; |
| 501 | break; |
| 502 | case IOMAP_UNWRITTEN: |
| 503 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
| 504 | break; |
| 505 | case IOMAP_MAPPED: |
| 506 | break; |
| 507 | } |
| 508 | |
Christoph Hellwig | 17de0a9 | 2016-08-29 11:33:58 +1000 | [diff] [blame] | 509 | if (iomap->flags & IOMAP_F_MERGED) |
| 510 | flags |= FIEMAP_EXTENT_MERGED; |
Darrick J. Wong | e43c460 | 2016-09-19 10:13:02 +1000 | [diff] [blame] | 511 | if (iomap->flags & IOMAP_F_SHARED) |
| 512 | flags |= FIEMAP_EXTENT_SHARED; |
Andreas Gruenbacher | 9ca250a | 2017-10-01 17:56:54 -0400 | [diff] [blame^] | 513 | if (iomap->flags & IOMAP_F_DATA_INLINE) |
| 514 | flags |= FIEMAP_EXTENT_DATA_INLINE; |
Christoph Hellwig | 17de0a9 | 2016-08-29 11:33:58 +1000 | [diff] [blame] | 515 | |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 516 | return fiemap_fill_next_extent(fi, iomap->offset, |
Andreas Gruenbacher | 19fe5f6 | 2017-10-01 17:55:54 -0400 | [diff] [blame] | 517 | iomap->addr != IOMAP_NULL_ADDR ? iomap->addr : 0, |
Christoph Hellwig | 17de0a9 | 2016-08-29 11:33:58 +1000 | [diff] [blame] | 518 | iomap->length, flags); |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static loff_t |
| 522 | iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, |
| 523 | struct iomap *iomap) |
| 524 | { |
| 525 | struct fiemap_ctx *ctx = data; |
| 526 | loff_t ret = length; |
| 527 | |
| 528 | if (iomap->type == IOMAP_HOLE) |
| 529 | return length; |
| 530 | |
| 531 | ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0); |
| 532 | ctx->prev = *iomap; |
| 533 | switch (ret) { |
| 534 | case 0: /* success */ |
| 535 | return length; |
| 536 | case 1: /* extent array full */ |
| 537 | return 0; |
| 538 | default: |
| 539 | return ret; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 544 | loff_t start, loff_t len, const struct iomap_ops *ops) |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 545 | { |
| 546 | struct fiemap_ctx ctx; |
| 547 | loff_t ret; |
| 548 | |
| 549 | memset(&ctx, 0, sizeof(ctx)); |
| 550 | ctx.fi = fi; |
| 551 | ctx.prev.type = IOMAP_HOLE; |
| 552 | |
| 553 | ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC); |
| 554 | if (ret) |
| 555 | return ret; |
| 556 | |
Dave Chinner | 8896b8f | 2016-08-17 08:41:10 +1000 | [diff] [blame] | 557 | if (fi->fi_flags & FIEMAP_FLAG_SYNC) { |
| 558 | ret = filemap_write_and_wait(inode->i_mapping); |
| 559 | if (ret) |
| 560 | return ret; |
| 561 | } |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 562 | |
| 563 | while (len > 0) { |
Christoph Hellwig | d33fd77 | 2016-10-20 15:51:28 +1100 | [diff] [blame] | 564 | ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx, |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 565 | iomap_fiemap_actor); |
Dave Chinner | ac2dc05 | 2016-08-17 08:41:34 +1000 | [diff] [blame] | 566 | /* inode with no (attribute) mapping will give ENOENT */ |
| 567 | if (ret == -ENOENT) |
| 568 | break; |
Christoph Hellwig | 8be9f56 | 2016-06-21 09:38:45 +1000 | [diff] [blame] | 569 | if (ret < 0) |
| 570 | return ret; |
| 571 | if (ret == 0) |
| 572 | break; |
| 573 | |
| 574 | start += ret; |
| 575 | len -= ret; |
| 576 | } |
| 577 | |
| 578 | if (ctx.prev.type != IOMAP_HOLE) { |
| 579 | ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST); |
| 580 | if (ret < 0) |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | EXPORT_SYMBOL_GPL(iomap_fiemap); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 587 | |
Andreas Gruenbacher | 0ed3b0d | 2017-06-29 11:43:21 -0700 | [diff] [blame] | 588 | static loff_t |
| 589 | iomap_seek_hole_actor(struct inode *inode, loff_t offset, loff_t length, |
| 590 | void *data, struct iomap *iomap) |
| 591 | { |
| 592 | switch (iomap->type) { |
| 593 | case IOMAP_UNWRITTEN: |
| 594 | offset = page_cache_seek_hole_data(inode, offset, length, |
| 595 | SEEK_HOLE); |
| 596 | if (offset < 0) |
| 597 | return length; |
| 598 | /* fall through */ |
| 599 | case IOMAP_HOLE: |
| 600 | *(loff_t *)data = offset; |
| 601 | return 0; |
| 602 | default: |
| 603 | return length; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | loff_t |
| 608 | iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops) |
| 609 | { |
| 610 | loff_t size = i_size_read(inode); |
| 611 | loff_t length = size - offset; |
| 612 | loff_t ret; |
| 613 | |
Darrick J. Wong | d6ab17f | 2017-07-12 10:26:47 -0700 | [diff] [blame] | 614 | /* Nothing to be found before or beyond the end of the file. */ |
| 615 | if (offset < 0 || offset >= size) |
Andreas Gruenbacher | 0ed3b0d | 2017-06-29 11:43:21 -0700 | [diff] [blame] | 616 | return -ENXIO; |
| 617 | |
| 618 | while (length > 0) { |
| 619 | ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops, |
| 620 | &offset, iomap_seek_hole_actor); |
| 621 | if (ret < 0) |
| 622 | return ret; |
| 623 | if (ret == 0) |
| 624 | break; |
| 625 | |
| 626 | offset += ret; |
| 627 | length -= ret; |
| 628 | } |
| 629 | |
| 630 | return offset; |
| 631 | } |
| 632 | EXPORT_SYMBOL_GPL(iomap_seek_hole); |
| 633 | |
| 634 | static loff_t |
| 635 | iomap_seek_data_actor(struct inode *inode, loff_t offset, loff_t length, |
| 636 | void *data, struct iomap *iomap) |
| 637 | { |
| 638 | switch (iomap->type) { |
| 639 | case IOMAP_HOLE: |
| 640 | return length; |
| 641 | case IOMAP_UNWRITTEN: |
| 642 | offset = page_cache_seek_hole_data(inode, offset, length, |
| 643 | SEEK_DATA); |
| 644 | if (offset < 0) |
| 645 | return length; |
| 646 | /*FALLTHRU*/ |
| 647 | default: |
| 648 | *(loff_t *)data = offset; |
| 649 | return 0; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | loff_t |
| 654 | iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops) |
| 655 | { |
| 656 | loff_t size = i_size_read(inode); |
| 657 | loff_t length = size - offset; |
| 658 | loff_t ret; |
| 659 | |
Darrick J. Wong | d6ab17f | 2017-07-12 10:26:47 -0700 | [diff] [blame] | 660 | /* Nothing to be found before or beyond the end of the file. */ |
| 661 | if (offset < 0 || offset >= size) |
Andreas Gruenbacher | 0ed3b0d | 2017-06-29 11:43:21 -0700 | [diff] [blame] | 662 | return -ENXIO; |
| 663 | |
| 664 | while (length > 0) { |
| 665 | ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops, |
| 666 | &offset, iomap_seek_data_actor); |
| 667 | if (ret < 0) |
| 668 | return ret; |
| 669 | if (ret == 0) |
| 670 | break; |
| 671 | |
| 672 | offset += ret; |
| 673 | length -= ret; |
| 674 | } |
| 675 | |
| 676 | if (length <= 0) |
| 677 | return -ENXIO; |
| 678 | return offset; |
| 679 | } |
| 680 | EXPORT_SYMBOL_GPL(iomap_seek_data); |
| 681 | |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 682 | /* |
| 683 | * Private flags for iomap_dio, must not overlap with the public ones in |
| 684 | * iomap.h: |
| 685 | */ |
| 686 | #define IOMAP_DIO_WRITE (1 << 30) |
| 687 | #define IOMAP_DIO_DIRTY (1 << 31) |
| 688 | |
| 689 | struct iomap_dio { |
| 690 | struct kiocb *iocb; |
| 691 | iomap_dio_end_io_t *end_io; |
| 692 | loff_t i_size; |
| 693 | loff_t size; |
| 694 | atomic_t ref; |
| 695 | unsigned flags; |
| 696 | int error; |
| 697 | |
| 698 | union { |
| 699 | /* used during submission and for synchronous completion: */ |
| 700 | struct { |
| 701 | struct iov_iter *iter; |
| 702 | struct task_struct *waiter; |
| 703 | struct request_queue *last_queue; |
| 704 | blk_qc_t cookie; |
| 705 | } submit; |
| 706 | |
| 707 | /* used for aio completion: */ |
| 708 | struct { |
| 709 | struct work_struct work; |
| 710 | } aio; |
| 711 | }; |
| 712 | }; |
| 713 | |
| 714 | static ssize_t iomap_dio_complete(struct iomap_dio *dio) |
| 715 | { |
| 716 | struct kiocb *iocb = dio->iocb; |
Lukas Czerner | 332391a | 2017-09-21 08:16:29 -0600 | [diff] [blame] | 717 | struct inode *inode = file_inode(iocb->ki_filp); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 718 | ssize_t ret; |
| 719 | |
Lukas Czerner | 332391a | 2017-09-21 08:16:29 -0600 | [diff] [blame] | 720 | /* |
| 721 | * Try again to invalidate clean pages which might have been cached by |
| 722 | * non-direct readahead, or faulted in by get_user_pages() if the source |
| 723 | * of the write was an mmap'ed region of the file we're writing. Either |
| 724 | * one is a pretty crazy thing to do, so we don't support it 100%. If |
| 725 | * this invalidation fails, tough, the write still worked... |
| 726 | */ |
| 727 | if (!dio->error && |
| 728 | (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) { |
| 729 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 730 | iocb->ki_pos >> PAGE_SHIFT, |
| 731 | (iocb->ki_pos + dio->size - 1) >> PAGE_SHIFT); |
| 732 | WARN_ON_ONCE(ret); |
| 733 | } |
| 734 | |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 735 | if (dio->end_io) { |
| 736 | ret = dio->end_io(iocb, |
| 737 | dio->error ? dio->error : dio->size, |
| 738 | dio->flags); |
| 739 | } else { |
| 740 | ret = dio->error; |
| 741 | } |
| 742 | |
| 743 | if (likely(!ret)) { |
| 744 | ret = dio->size; |
| 745 | /* check for short read */ |
| 746 | if (iocb->ki_pos + ret > dio->i_size && |
| 747 | !(dio->flags & IOMAP_DIO_WRITE)) |
| 748 | ret = dio->i_size - iocb->ki_pos; |
| 749 | iocb->ki_pos += ret; |
| 750 | } |
| 751 | |
| 752 | inode_dio_end(file_inode(iocb->ki_filp)); |
| 753 | kfree(dio); |
| 754 | |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | static void iomap_dio_complete_work(struct work_struct *work) |
| 759 | { |
| 760 | struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work); |
| 761 | struct kiocb *iocb = dio->iocb; |
| 762 | bool is_write = (dio->flags & IOMAP_DIO_WRITE); |
| 763 | ssize_t ret; |
| 764 | |
| 765 | ret = iomap_dio_complete(dio); |
| 766 | if (is_write && ret > 0) |
| 767 | ret = generic_write_sync(iocb, ret); |
| 768 | iocb->ki_complete(iocb, ret, 0); |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * Set an error in the dio if none is set yet. We have to use cmpxchg |
| 773 | * as the submission context and the completion context(s) can race to |
| 774 | * update the error. |
| 775 | */ |
| 776 | static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret) |
| 777 | { |
| 778 | cmpxchg(&dio->error, 0, ret); |
| 779 | } |
| 780 | |
| 781 | static void iomap_dio_bio_end_io(struct bio *bio) |
| 782 | { |
| 783 | struct iomap_dio *dio = bio->bi_private; |
| 784 | bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY); |
| 785 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 786 | if (bio->bi_status) |
| 787 | iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status)); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 788 | |
| 789 | if (atomic_dec_and_test(&dio->ref)) { |
| 790 | if (is_sync_kiocb(dio->iocb)) { |
| 791 | struct task_struct *waiter = dio->submit.waiter; |
| 792 | |
| 793 | WRITE_ONCE(dio->submit.waiter, NULL); |
| 794 | wake_up_process(waiter); |
| 795 | } else if (dio->flags & IOMAP_DIO_WRITE) { |
| 796 | struct inode *inode = file_inode(dio->iocb->ki_filp); |
| 797 | |
| 798 | INIT_WORK(&dio->aio.work, iomap_dio_complete_work); |
| 799 | queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work); |
| 800 | } else { |
| 801 | iomap_dio_complete_work(&dio->aio.work); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | if (should_dirty) { |
| 806 | bio_check_pages_dirty(bio); |
| 807 | } else { |
| 808 | struct bio_vec *bvec; |
| 809 | int i; |
| 810 | |
| 811 | bio_for_each_segment_all(bvec, bio, i) |
| 812 | put_page(bvec->bv_page); |
| 813 | bio_put(bio); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | static blk_qc_t |
| 818 | iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos, |
| 819 | unsigned len) |
| 820 | { |
| 821 | struct page *page = ZERO_PAGE(0); |
| 822 | struct bio *bio; |
| 823 | |
| 824 | bio = bio_alloc(GFP_KERNEL, 1); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 825 | bio_set_dev(bio, iomap->bdev); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 826 | bio->bi_iter.bi_sector = |
Andreas Gruenbacher | 19fe5f6 | 2017-10-01 17:55:54 -0400 | [diff] [blame] | 827 | (iomap->addr + pos - iomap->offset) >> 9; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 828 | bio->bi_private = dio; |
| 829 | bio->bi_end_io = iomap_dio_bio_end_io; |
| 830 | |
| 831 | get_page(page); |
| 832 | if (bio_add_page(bio, page, len, 0) != len) |
| 833 | BUG(); |
Linus Torvalds | 5cc60ae | 2016-12-14 21:35:31 -0800 | [diff] [blame] | 834 | bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC | REQ_IDLE); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 835 | |
| 836 | atomic_inc(&dio->ref); |
| 837 | return submit_bio(bio); |
| 838 | } |
| 839 | |
| 840 | static loff_t |
| 841 | iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length, |
| 842 | void *data, struct iomap *iomap) |
| 843 | { |
| 844 | struct iomap_dio *dio = data; |
Fabian Frederick | 9340747 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 845 | unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev)); |
| 846 | unsigned int fs_block_size = i_blocksize(inode), pad; |
| 847 | unsigned int align = iov_iter_alignment(dio->submit.iter); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 848 | struct iov_iter iter; |
| 849 | struct bio *bio; |
| 850 | bool need_zeroout = false; |
| 851 | int nr_pages, ret; |
| 852 | |
| 853 | if ((pos | length | align) & ((1 << blkbits) - 1)) |
| 854 | return -EINVAL; |
| 855 | |
| 856 | switch (iomap->type) { |
| 857 | case IOMAP_HOLE: |
| 858 | if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE)) |
| 859 | return -EIO; |
| 860 | /*FALLTHRU*/ |
| 861 | case IOMAP_UNWRITTEN: |
| 862 | if (!(dio->flags & IOMAP_DIO_WRITE)) { |
| 863 | iov_iter_zero(length, dio->submit.iter); |
| 864 | dio->size += length; |
| 865 | return length; |
| 866 | } |
| 867 | dio->flags |= IOMAP_DIO_UNWRITTEN; |
| 868 | need_zeroout = true; |
| 869 | break; |
| 870 | case IOMAP_MAPPED: |
| 871 | if (iomap->flags & IOMAP_F_SHARED) |
| 872 | dio->flags |= IOMAP_DIO_COW; |
| 873 | if (iomap->flags & IOMAP_F_NEW) |
| 874 | need_zeroout = true; |
| 875 | break; |
| 876 | default: |
| 877 | WARN_ON_ONCE(1); |
| 878 | return -EIO; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Operate on a partial iter trimmed to the extent we were called for. |
| 883 | * We'll update the iter in the dio once we're done with this extent. |
| 884 | */ |
| 885 | iter = *dio->submit.iter; |
| 886 | iov_iter_truncate(&iter, length); |
| 887 | |
| 888 | nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES); |
| 889 | if (nr_pages <= 0) |
| 890 | return nr_pages; |
| 891 | |
| 892 | if (need_zeroout) { |
| 893 | /* zero out from the start of the block to the write offset */ |
| 894 | pad = pos & (fs_block_size - 1); |
| 895 | if (pad) |
| 896 | iomap_dio_zero(dio, iomap, pos - pad, pad); |
| 897 | } |
| 898 | |
| 899 | do { |
| 900 | if (dio->error) |
| 901 | return 0; |
| 902 | |
| 903 | bio = bio_alloc(GFP_KERNEL, nr_pages); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 904 | bio_set_dev(bio, iomap->bdev); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 905 | bio->bi_iter.bi_sector = |
Andreas Gruenbacher | 19fe5f6 | 2017-10-01 17:55:54 -0400 | [diff] [blame] | 906 | (iomap->addr + pos - iomap->offset) >> 9; |
Jens Axboe | 45d06cf | 2017-06-27 11:01:22 -0600 | [diff] [blame] | 907 | bio->bi_write_hint = dio->iocb->ki_hint; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 908 | bio->bi_private = dio; |
| 909 | bio->bi_end_io = iomap_dio_bio_end_io; |
| 910 | |
| 911 | ret = bio_iov_iter_get_pages(bio, &iter); |
| 912 | if (unlikely(ret)) { |
| 913 | bio_put(bio); |
| 914 | return ret; |
| 915 | } |
| 916 | |
| 917 | if (dio->flags & IOMAP_DIO_WRITE) { |
Linus Torvalds | 5cc60ae | 2016-12-14 21:35:31 -0800 | [diff] [blame] | 918 | bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC | REQ_IDLE); |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 919 | task_io_account_write(bio->bi_iter.bi_size); |
| 920 | } else { |
| 921 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
| 922 | if (dio->flags & IOMAP_DIO_DIRTY) |
| 923 | bio_set_pages_dirty(bio); |
| 924 | } |
| 925 | |
| 926 | dio->size += bio->bi_iter.bi_size; |
| 927 | pos += bio->bi_iter.bi_size; |
| 928 | |
| 929 | nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES); |
| 930 | |
| 931 | atomic_inc(&dio->ref); |
| 932 | |
| 933 | dio->submit.last_queue = bdev_get_queue(iomap->bdev); |
| 934 | dio->submit.cookie = submit_bio(bio); |
| 935 | } while (nr_pages); |
| 936 | |
| 937 | if (need_zeroout) { |
| 938 | /* zero out from the end of the write to the end of the block */ |
| 939 | pad = pos & (fs_block_size - 1); |
| 940 | if (pad) |
| 941 | iomap_dio_zero(dio, iomap, pos, fs_block_size - pad); |
| 942 | } |
| 943 | |
| 944 | iov_iter_advance(dio->submit.iter, length); |
| 945 | return length; |
| 946 | } |
| 947 | |
| 948 | ssize_t |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 949 | iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, |
| 950 | const struct iomap_ops *ops, iomap_dio_end_io_t end_io) |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 951 | { |
| 952 | struct address_space *mapping = iocb->ki_filp->f_mapping; |
| 953 | struct inode *inode = file_inode(iocb->ki_filp); |
| 954 | size_t count = iov_iter_count(iter); |
Eryu Guan | c771c14 | 2017-03-02 15:02:06 -0800 | [diff] [blame] | 955 | loff_t pos = iocb->ki_pos, start = pos; |
| 956 | loff_t end = iocb->ki_pos + count - 1, ret = 0; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 957 | unsigned int flags = IOMAP_DIRECT; |
| 958 | struct blk_plug plug; |
| 959 | struct iomap_dio *dio; |
| 960 | |
| 961 | lockdep_assert_held(&inode->i_rwsem); |
| 962 | |
| 963 | if (!count) |
| 964 | return 0; |
| 965 | |
| 966 | dio = kmalloc(sizeof(*dio), GFP_KERNEL); |
| 967 | if (!dio) |
| 968 | return -ENOMEM; |
| 969 | |
| 970 | dio->iocb = iocb; |
| 971 | atomic_set(&dio->ref, 1); |
| 972 | dio->size = 0; |
| 973 | dio->i_size = i_size_read(inode); |
| 974 | dio->end_io = end_io; |
| 975 | dio->error = 0; |
| 976 | dio->flags = 0; |
| 977 | |
| 978 | dio->submit.iter = iter; |
| 979 | if (is_sync_kiocb(iocb)) { |
| 980 | dio->submit.waiter = current; |
| 981 | dio->submit.cookie = BLK_QC_T_NONE; |
| 982 | dio->submit.last_queue = NULL; |
| 983 | } |
| 984 | |
| 985 | if (iov_iter_rw(iter) == READ) { |
| 986 | if (pos >= dio->i_size) |
| 987 | goto out_free_dio; |
| 988 | |
| 989 | if (iter->type == ITER_IOVEC) |
| 990 | dio->flags |= IOMAP_DIO_DIRTY; |
| 991 | } else { |
| 992 | dio->flags |= IOMAP_DIO_WRITE; |
| 993 | flags |= IOMAP_WRITE; |
| 994 | } |
| 995 | |
Goldwyn Rodrigues | a38d124 | 2017-06-20 07:05:45 -0500 | [diff] [blame] | 996 | if (iocb->ki_flags & IOCB_NOWAIT) { |
| 997 | if (filemap_range_has_page(mapping, start, end)) { |
| 998 | ret = -EAGAIN; |
| 999 | goto out_free_dio; |
| 1000 | } |
| 1001 | flags |= IOMAP_NOWAIT; |
| 1002 | } |
| 1003 | |
Andrey Ryabinin | 55635ba | 2017-05-03 14:55:59 -0700 | [diff] [blame] | 1004 | ret = filemap_write_and_wait_range(mapping, start, end); |
| 1005 | if (ret) |
| 1006 | goto out_free_dio; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1007 | |
Andrey Ryabinin | 55635ba | 2017-05-03 14:55:59 -0700 | [diff] [blame] | 1008 | ret = invalidate_inode_pages2_range(mapping, |
| 1009 | start >> PAGE_SHIFT, end >> PAGE_SHIFT); |
| 1010 | WARN_ON_ONCE(ret); |
| 1011 | ret = 0; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1012 | |
Chandan Rajendra | 546e7be | 2017-09-22 11:47:33 -0700 | [diff] [blame] | 1013 | if (iov_iter_rw(iter) == WRITE && !is_sync_kiocb(iocb) && |
| 1014 | !inode->i_sb->s_dio_done_wq) { |
| 1015 | ret = sb_init_dio_done_wq(inode->i_sb); |
| 1016 | if (ret < 0) |
| 1017 | goto out_free_dio; |
| 1018 | } |
| 1019 | |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1020 | inode_dio_begin(inode); |
| 1021 | |
| 1022 | blk_start_plug(&plug); |
| 1023 | do { |
| 1024 | ret = iomap_apply(inode, pos, count, flags, ops, dio, |
| 1025 | iomap_dio_actor); |
| 1026 | if (ret <= 0) { |
| 1027 | /* magic error code to fall back to buffered I/O */ |
| 1028 | if (ret == -ENOTBLK) |
| 1029 | ret = 0; |
| 1030 | break; |
| 1031 | } |
| 1032 | pos += ret; |
Chandan Rajendra | a008c31 | 2017-04-12 11:03:20 -0700 | [diff] [blame] | 1033 | |
| 1034 | if (iov_iter_rw(iter) == READ && pos >= dio->i_size) |
| 1035 | break; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1036 | } while ((count = iov_iter_count(iter)) > 0); |
| 1037 | blk_finish_plug(&plug); |
| 1038 | |
| 1039 | if (ret < 0) |
| 1040 | iomap_dio_set_error(dio, ret); |
| 1041 | |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1042 | if (!atomic_dec_and_test(&dio->ref)) { |
| 1043 | if (!is_sync_kiocb(iocb)) |
| 1044 | return -EIOCBQUEUED; |
| 1045 | |
| 1046 | for (;;) { |
| 1047 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1048 | if (!READ_ONCE(dio->submit.waiter)) |
| 1049 | break; |
| 1050 | |
| 1051 | if (!(iocb->ki_flags & IOCB_HIPRI) || |
| 1052 | !dio->submit.last_queue || |
Linus Torvalds | 5cc60ae | 2016-12-14 21:35:31 -0800 | [diff] [blame] | 1053 | !blk_mq_poll(dio->submit.last_queue, |
| 1054 | dio->submit.cookie)) |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1055 | io_schedule(); |
| 1056 | } |
| 1057 | __set_current_state(TASK_RUNNING); |
| 1058 | } |
| 1059 | |
Eryu Guan | c771c14 | 2017-03-02 15:02:06 -0800 | [diff] [blame] | 1060 | ret = iomap_dio_complete(dio); |
| 1061 | |
Eryu Guan | c771c14 | 2017-03-02 15:02:06 -0800 | [diff] [blame] | 1062 | return ret; |
Christoph Hellwig | ff6a929 | 2016-11-30 14:36:01 +1100 | [diff] [blame] | 1063 | |
| 1064 | out_free_dio: |
| 1065 | kfree(dio); |
| 1066 | return ret; |
| 1067 | } |
| 1068 | EXPORT_SYMBOL_GPL(iomap_dio_rw); |