| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * linux/fs/nfs/write.c |
| 4 | * |
| 5 | * Write file data over NFS. |
| 6 | * |
| 7 | * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/file.h> |
| 15 | #include <linux/writeback.h> |
| 16 | #include <linux/swap.h> |
| 17 | #include <linux/migrate.h> |
| 18 | |
| 19 | #include <linux/sunrpc/clnt.h> |
| 20 | #include <linux/nfs_fs.h> |
| 21 | #include <linux/nfs_mount.h> |
| 22 | #include <linux/nfs_page.h> |
| 23 | #include <linux/backing-dev.h> |
| 24 | #include <linux/export.h> |
| 25 | #include <linux/freezer.h> |
| 26 | #include <linux/wait.h> |
| 27 | #include <linux/iversion.h> |
| 28 | #include <linux/filelock.h> |
| 29 | |
| 30 | #include <linux/uaccess.h> |
| 31 | #include <linux/sched/mm.h> |
| 32 | |
| 33 | #include "delegation.h" |
| 34 | #include "internal.h" |
| 35 | #include "iostat.h" |
| 36 | #include "nfs4_fs.h" |
| 37 | #include "fscache.h" |
| 38 | #include "pnfs.h" |
| 39 | |
| 40 | #include "nfstrace.h" |
| 41 | |
| 42 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
| 43 | |
| 44 | #define MIN_POOL_WRITE (32) |
| 45 | #define MIN_POOL_COMMIT (4) |
| 46 | |
| 47 | struct nfs_io_completion { |
| 48 | void (*complete)(void *data); |
| 49 | void *data; |
| 50 | struct kref refcount; |
| 51 | }; |
| 52 | |
| 53 | /* |
| 54 | * Local function declarations |
| 55 | */ |
| 56 | static void nfs_redirty_request(struct nfs_page *req); |
| 57 | static const struct rpc_call_ops nfs_commit_ops; |
| 58 | static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops; |
| 59 | static const struct nfs_commit_completion_ops nfs_commit_completion_ops; |
| 60 | static const struct nfs_rw_ops nfs_rw_write_ops; |
| 61 | static void nfs_inode_remove_request(struct nfs_page *req); |
| 62 | static void nfs_clear_request_commit(struct nfs_commit_info *cinfo, |
| 63 | struct nfs_page *req); |
| 64 | static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, |
| 65 | struct inode *inode); |
| 66 | |
| 67 | static struct kmem_cache *nfs_wdata_cachep; |
| 68 | static mempool_t *nfs_wdata_mempool; |
| 69 | static struct kmem_cache *nfs_cdata_cachep; |
| 70 | static mempool_t *nfs_commit_mempool; |
| 71 | |
| 72 | struct nfs_commit_data *nfs_commitdata_alloc(void) |
| 73 | { |
| 74 | struct nfs_commit_data *p; |
| 75 | |
| 76 | p = kmem_cache_zalloc(nfs_cdata_cachep, nfs_io_gfp_mask()); |
| 77 | if (!p) { |
| 78 | p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT); |
| 79 | if (!p) |
| 80 | return NULL; |
| 81 | memset(p, 0, sizeof(*p)); |
| 82 | } |
| 83 | INIT_LIST_HEAD(list: &p->pages); |
| 84 | return p; |
| 85 | } |
| 86 | EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); |
| 87 | |
| 88 | void nfs_commit_free(struct nfs_commit_data *p) |
| 89 | { |
| 90 | mempool_free(element: p, pool: nfs_commit_mempool); |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(nfs_commit_free); |
| 93 | |
| 94 | static struct nfs_pgio_header *nfs_writehdr_alloc(void) |
| 95 | { |
| 96 | struct nfs_pgio_header *p; |
| 97 | |
| 98 | p = kmem_cache_zalloc(nfs_wdata_cachep, nfs_io_gfp_mask()); |
| 99 | if (!p) { |
| 100 | p = mempool_alloc(nfs_wdata_mempool, GFP_NOWAIT); |
| 101 | if (!p) |
| 102 | return NULL; |
| 103 | memset(p, 0, sizeof(*p)); |
| 104 | } |
| 105 | p->rw_mode = FMODE_WRITE; |
| 106 | return p; |
| 107 | } |
| 108 | |
| 109 | static void nfs_writehdr_free(struct nfs_pgio_header *hdr) |
| 110 | { |
| 111 | mempool_free(element: hdr, pool: nfs_wdata_mempool); |
| 112 | } |
| 113 | |
| 114 | static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags) |
| 115 | { |
| 116 | return kmalloc(sizeof(struct nfs_io_completion), gfp_flags); |
| 117 | } |
| 118 | |
| 119 | static void nfs_io_completion_init(struct nfs_io_completion *ioc, |
| 120 | void (*complete)(void *), void *data) |
| 121 | { |
| 122 | ioc->complete = complete; |
| 123 | ioc->data = data; |
| 124 | kref_init(kref: &ioc->refcount); |
| 125 | } |
| 126 | |
| 127 | static void nfs_io_completion_release(struct kref *kref) |
| 128 | { |
| 129 | struct nfs_io_completion *ioc = container_of(kref, |
| 130 | struct nfs_io_completion, refcount); |
| 131 | ioc->complete(ioc->data); |
| 132 | kfree(objp: ioc); |
| 133 | } |
| 134 | |
| 135 | static void nfs_io_completion_get(struct nfs_io_completion *ioc) |
| 136 | { |
| 137 | if (ioc != NULL) |
| 138 | kref_get(kref: &ioc->refcount); |
| 139 | } |
| 140 | |
| 141 | static void nfs_io_completion_put(struct nfs_io_completion *ioc) |
| 142 | { |
| 143 | if (ioc != NULL) |
| 144 | kref_put(kref: &ioc->refcount, release: nfs_io_completion_release); |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | nfs_page_set_inode_ref(struct nfs_page *req, struct inode *inode) |
| 149 | { |
| 150 | if (!test_and_set_bit(nr: PG_INODE_REF, addr: &req->wb_flags)) { |
| 151 | kref_get(kref: &req->wb_kref); |
| 152 | atomic_long_inc(v: &NFS_I(inode)->nrequests); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void nfs_cancel_remove_inode(struct nfs_page *req, struct inode *inode) |
| 157 | { |
| 158 | if (test_and_clear_bit(nr: PG_REMOVE, addr: &req->wb_flags)) |
| 159 | nfs_page_set_inode_ref(req, inode); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * nfs_folio_find_head_request - find head request associated with a folio |
| 164 | * @folio: pointer to folio |
| 165 | * |
| 166 | * must be called while holding the inode lock. |
| 167 | * |
| 168 | * returns matching head request with reference held, or NULL if not found. |
| 169 | */ |
| 170 | static struct nfs_page *nfs_folio_find_head_request(struct folio *folio) |
| 171 | { |
| 172 | struct address_space *mapping = folio->mapping; |
| 173 | struct nfs_page *req; |
| 174 | |
| 175 | if (!folio_test_private(folio)) |
| 176 | return NULL; |
| 177 | spin_lock(lock: &mapping->i_private_lock); |
| 178 | req = folio->private; |
| 179 | if (req) { |
| 180 | WARN_ON_ONCE(req->wb_head != req); |
| 181 | kref_get(kref: &req->wb_kref); |
| 182 | } |
| 183 | spin_unlock(lock: &mapping->i_private_lock); |
| 184 | return req; |
| 185 | } |
| 186 | |
| 187 | /* Adjust the file length if we're writing beyond the end */ |
| 188 | static void nfs_grow_file(struct folio *folio, unsigned int offset, |
| 189 | unsigned int count) |
| 190 | { |
| 191 | struct inode *inode = folio->mapping->host; |
| 192 | loff_t end, i_size; |
| 193 | pgoff_t end_index; |
| 194 | |
| 195 | spin_lock(lock: &inode->i_lock); |
| 196 | i_size = i_size_read(inode); |
| 197 | end_index = ((i_size - 1) >> folio_shift(folio)) << folio_order(folio); |
| 198 | if (i_size > 0 && folio->index < end_index) |
| 199 | goto out; |
| 200 | end = folio_pos(folio) + (loff_t)offset + (loff_t)count; |
| 201 | if (i_size >= end) |
| 202 | goto out; |
| 203 | trace_nfs_size_grow(inode, new_size: end); |
| 204 | i_size_write(inode, i_size: end); |
| 205 | NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE; |
| 206 | nfs_inc_stats(inode, stat: NFSIOS_EXTENDWRITE); |
| 207 | out: |
| 208 | /* Atomically update timestamps if they are delegated to us. */ |
| 209 | nfs_update_delegated_mtime_locked(inode); |
| 210 | spin_unlock(lock: &inode->i_lock); |
| 211 | nfs_fscache_invalidate(inode, flags: 0); |
| 212 | } |
| 213 | |
| 214 | /* A writeback failed: mark the page as bad, and invalidate the page cache */ |
| 215 | static void nfs_set_pageerror(struct address_space *mapping) |
| 216 | { |
| 217 | struct inode *inode = mapping->host; |
| 218 | |
| 219 | nfs_zap_mapping(inode: mapping->host, mapping); |
| 220 | /* Force file size revalidation */ |
| 221 | spin_lock(lock: &inode->i_lock); |
| 222 | nfs_set_cache_invalid(inode, NFS_INO_REVAL_FORCED | |
| 223 | NFS_INO_INVALID_CHANGE | |
| 224 | NFS_INO_INVALID_SIZE); |
| 225 | spin_unlock(lock: &inode->i_lock); |
| 226 | } |
| 227 | |
| 228 | static void nfs_mapping_set_error(struct folio *folio, int error) |
| 229 | { |
| 230 | struct address_space *mapping = folio->mapping; |
| 231 | |
| 232 | filemap_set_wb_err(mapping, err: error); |
| 233 | if (mapping->host) |
| 234 | errseq_set(eseq: &mapping->host->i_sb->s_wb_err, |
| 235 | err: error == -ENOSPC ? -ENOSPC : -EIO); |
| 236 | nfs_set_pageerror(mapping); |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * nfs_page_covers_folio |
| 241 | * @req: struct nfs_page |
| 242 | * |
| 243 | * Return true if the request covers the whole folio. |
| 244 | * Note that the caller should ensure all subrequests have been joined |
| 245 | */ |
| 246 | static bool nfs_page_group_covers_page(struct nfs_page *req) |
| 247 | { |
| 248 | unsigned int len = nfs_folio_length(folio: nfs_page_to_folio(req)); |
| 249 | |
| 250 | return req->wb_pgbase == 0 && req->wb_bytes == len; |
| 251 | } |
| 252 | |
| 253 | /* We can set the PG_uptodate flag if we see that a write request |
| 254 | * covers the full page. |
| 255 | */ |
| 256 | static void nfs_mark_uptodate(struct nfs_page *req) |
| 257 | { |
| 258 | struct folio *folio = nfs_page_to_folio(req); |
| 259 | |
| 260 | if (folio_test_uptodate(folio)) |
| 261 | return; |
| 262 | if (!nfs_page_group_covers_page(req)) |
| 263 | return; |
| 264 | folio_mark_uptodate(folio); |
| 265 | } |
| 266 | |
| 267 | static int wb_priority(struct writeback_control *wbc) |
| 268 | { |
| 269 | int ret = 0; |
| 270 | |
| 271 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 272 | ret = FLUSH_COND_STABLE; |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * NFS congestion control |
| 278 | */ |
| 279 | |
| 280 | int nfs_congestion_kb; |
| 281 | |
| 282 | #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) |
| 283 | #define NFS_CONGESTION_OFF_THRESH \ |
| 284 | (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) |
| 285 | |
| 286 | static void nfs_folio_set_writeback(struct folio *folio) |
| 287 | { |
| 288 | struct nfs_server *nfss = NFS_SERVER(inode: folio->mapping->host); |
| 289 | |
| 290 | folio_start_writeback(folio); |
| 291 | if (atomic_long_inc_return(v: &nfss->writeback) > NFS_CONGESTION_ON_THRESH) |
| 292 | nfss->write_congested = 1; |
| 293 | } |
| 294 | |
| 295 | static void nfs_folio_end_writeback(struct folio *folio) |
| 296 | { |
| 297 | struct nfs_server *nfss = NFS_SERVER(inode: folio->mapping->host); |
| 298 | |
| 299 | folio_end_writeback_no_dropbehind(folio); |
| 300 | if (atomic_long_dec_return(v: &nfss->writeback) < |
| 301 | NFS_CONGESTION_OFF_THRESH) { |
| 302 | nfss->write_congested = 0; |
| 303 | wake_up_all(&nfss->write_congestion_wait); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | static void nfs_page_end_writeback(struct nfs_page *req) |
| 308 | { |
| 309 | if (nfs_page_group_sync_on_bit(req, PG_WB_END)) { |
| 310 | nfs_unlock_request(req); |
| 311 | nfs_folio_end_writeback(folio: nfs_page_to_folio(req)); |
| 312 | } else |
| 313 | nfs_unlock_request(req); |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests |
| 318 | * |
| 319 | * @destroy_list - request list (using wb_this_page) terminated by @old_head |
| 320 | * @old_head - the old head of the list |
| 321 | * |
| 322 | * All subrequests must be locked and removed from all lists, so at this point |
| 323 | * they are only "active" in this function, and possibly in nfs_wait_on_request |
| 324 | * with a reference held by some other context. |
| 325 | */ |
| 326 | static void |
| 327 | nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list, |
| 328 | struct nfs_page *old_head, |
| 329 | struct inode *inode) |
| 330 | { |
| 331 | while (destroy_list) { |
| 332 | struct nfs_page *subreq = destroy_list; |
| 333 | |
| 334 | destroy_list = (subreq->wb_this_page == old_head) ? |
| 335 | NULL : subreq->wb_this_page; |
| 336 | |
| 337 | /* Note: lock subreq in order to change subreq->wb_head */ |
| 338 | nfs_page_set_headlock(req: subreq); |
| 339 | WARN_ON_ONCE(old_head != subreq->wb_head); |
| 340 | |
| 341 | /* make sure old group is not used */ |
| 342 | subreq->wb_this_page = subreq; |
| 343 | subreq->wb_head = subreq; |
| 344 | |
| 345 | clear_bit(nr: PG_REMOVE, addr: &subreq->wb_flags); |
| 346 | |
| 347 | /* Note: races with nfs_page_group_destroy() */ |
| 348 | if (!kref_read(kref: &subreq->wb_kref)) { |
| 349 | /* Check if we raced with nfs_page_group_destroy() */ |
| 350 | if (test_and_clear_bit(nr: PG_TEARDOWN, addr: &subreq->wb_flags)) { |
| 351 | nfs_page_clear_headlock(req: subreq); |
| 352 | nfs_free_request(req: subreq); |
| 353 | } else |
| 354 | nfs_page_clear_headlock(req: subreq); |
| 355 | continue; |
| 356 | } |
| 357 | nfs_page_clear_headlock(req: subreq); |
| 358 | |
| 359 | nfs_release_request(old_head); |
| 360 | |
| 361 | if (test_and_clear_bit(nr: PG_INODE_REF, addr: &subreq->wb_flags)) { |
| 362 | nfs_release_request(subreq); |
| 363 | atomic_long_dec(v: &NFS_I(inode)->nrequests); |
| 364 | } |
| 365 | |
| 366 | /* subreq is now totally disconnected from page group or any |
| 367 | * write / commit lists. last chance to wake any waiters */ |
| 368 | nfs_unlock_and_release_request(subreq); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * nfs_join_page_group - destroy subrequests of the head req |
| 374 | * @head: the page used to lookup the "page group" of nfs_page structures |
| 375 | * @inode: Inode to which the request belongs. |
| 376 | * |
| 377 | * This function joins all sub requests to the head request by first |
| 378 | * locking all requests in the group, cancelling any pending operations |
| 379 | * and finally updating the head request to cover the whole range covered by |
| 380 | * the (former) group. All subrequests are removed from any write or commit |
| 381 | * lists, unlinked from the group and destroyed. |
| 382 | */ |
| 383 | void nfs_join_page_group(struct nfs_page *head, struct nfs_commit_info *cinfo, |
| 384 | struct inode *inode) |
| 385 | { |
| 386 | struct nfs_page *subreq; |
| 387 | struct nfs_page *destroy_list = NULL; |
| 388 | unsigned int pgbase, off, bytes; |
| 389 | |
| 390 | pgbase = head->wb_pgbase; |
| 391 | bytes = head->wb_bytes; |
| 392 | off = head->wb_offset; |
| 393 | for (subreq = head->wb_this_page; subreq != head; |
| 394 | subreq = subreq->wb_this_page) { |
| 395 | /* Subrequests should always form a contiguous range */ |
| 396 | if (pgbase > subreq->wb_pgbase) { |
| 397 | off -= pgbase - subreq->wb_pgbase; |
| 398 | bytes += pgbase - subreq->wb_pgbase; |
| 399 | pgbase = subreq->wb_pgbase; |
| 400 | } |
| 401 | bytes = max(subreq->wb_pgbase + subreq->wb_bytes |
| 402 | - pgbase, bytes); |
| 403 | } |
| 404 | |
| 405 | /* Set the head request's range to cover the former page group */ |
| 406 | head->wb_pgbase = pgbase; |
| 407 | head->wb_bytes = bytes; |
| 408 | head->wb_offset = off; |
| 409 | |
| 410 | /* Now that all requests are locked, make sure they aren't on any list. |
| 411 | * Commit list removal accounting is done after locks are dropped */ |
| 412 | subreq = head; |
| 413 | do { |
| 414 | nfs_clear_request_commit(cinfo, req: subreq); |
| 415 | subreq = subreq->wb_this_page; |
| 416 | } while (subreq != head); |
| 417 | |
| 418 | /* unlink subrequests from head, destroy them later */ |
| 419 | if (head->wb_this_page != head) { |
| 420 | /* destroy list will be terminated by head */ |
| 421 | destroy_list = head->wb_this_page; |
| 422 | head->wb_this_page = head; |
| 423 | } |
| 424 | |
| 425 | nfs_destroy_unlinked_subrequests(destroy_list, old_head: head, inode); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * nfs_wait_on_request - Wait for a request to complete. |
| 430 | * @req: request to wait upon. |
| 431 | * |
| 432 | * Interruptible by fatal signals only. |
| 433 | * The user is responsible for holding a count on the request. |
| 434 | */ |
| 435 | static int nfs_wait_on_request(struct nfs_page *req) |
| 436 | { |
| 437 | if (!test_bit(PG_BUSY, &req->wb_flags)) |
| 438 | return 0; |
| 439 | set_bit(nr: PG_CONTENDED2, addr: &req->wb_flags); |
| 440 | smp_mb__after_atomic(); |
| 441 | return wait_on_bit_io(word: &req->wb_flags, bit: PG_BUSY, |
| 442 | TASK_UNINTERRUPTIBLE); |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * nfs_unroll_locks - unlock all newly locked reqs and wait on @req |
| 447 | * @head: head request of page group, must be holding head lock |
| 448 | * @req: request that couldn't lock and needs to wait on the req bit lock |
| 449 | * |
| 450 | * This is a helper function for nfs_lock_and_join_requests |
| 451 | * returns 0 on success, < 0 on error. |
| 452 | */ |
| 453 | static void |
| 454 | nfs_unroll_locks(struct nfs_page *head, struct nfs_page *req) |
| 455 | { |
| 456 | struct nfs_page *tmp; |
| 457 | |
| 458 | /* relinquish all the locks successfully grabbed this run */ |
| 459 | for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) { |
| 460 | if (!kref_read(kref: &tmp->wb_kref)) |
| 461 | continue; |
| 462 | nfs_unlock_and_release_request(tmp); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * nfs_page_group_lock_subreq - try to lock a subrequest |
| 468 | * @head: head request of page group |
| 469 | * @subreq: request to lock |
| 470 | * |
| 471 | * This is a helper function for nfs_lock_and_join_requests which |
| 472 | * must be called with the head request and page group both locked. |
| 473 | * On error, it returns with the page group unlocked. |
| 474 | */ |
| 475 | static int |
| 476 | nfs_page_group_lock_subreq(struct nfs_page *head, struct nfs_page *subreq) |
| 477 | { |
| 478 | int ret; |
| 479 | |
| 480 | if (!kref_get_unless_zero(kref: &subreq->wb_kref)) |
| 481 | return 0; |
| 482 | while (!nfs_lock_request(req: subreq)) { |
| 483 | nfs_page_group_unlock(head); |
| 484 | ret = nfs_wait_on_request(req: subreq); |
| 485 | if (!ret) |
| 486 | ret = nfs_page_group_lock(head); |
| 487 | if (ret < 0) { |
| 488 | nfs_unroll_locks(head, req: subreq); |
| 489 | nfs_release_request(subreq); |
| 490 | return ret; |
| 491 | } |
| 492 | } |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * nfs_lock_and_join_requests - join all subreqs to the head req |
| 498 | * @folio: the folio used to lookup the "page group" of nfs_page structures |
| 499 | * |
| 500 | * This function joins all sub requests to the head request by first |
| 501 | * locking all requests in the group, cancelling any pending operations |
| 502 | * and finally updating the head request to cover the whole range covered by |
| 503 | * the (former) group. All subrequests are removed from any write or commit |
| 504 | * lists, unlinked from the group and destroyed. |
| 505 | * |
| 506 | * Returns a locked, referenced pointer to the head request - which after |
| 507 | * this call is guaranteed to be the only request associated with the page. |
| 508 | * Returns NULL if no requests are found for @folio, or a ERR_PTR if an |
| 509 | * error was encountered. |
| 510 | */ |
| 511 | static struct nfs_page *nfs_lock_and_join_requests(struct folio *folio) |
| 512 | { |
| 513 | struct inode *inode = folio->mapping->host; |
| 514 | struct nfs_page *head, *subreq; |
| 515 | struct nfs_commit_info cinfo; |
| 516 | int ret; |
| 517 | |
| 518 | /* |
| 519 | * A reference is taken only on the head request which acts as a |
| 520 | * reference to the whole page group - the group will not be destroyed |
| 521 | * until the head reference is released. |
| 522 | */ |
| 523 | retry: |
| 524 | head = nfs_folio_find_head_request(folio); |
| 525 | if (!head) |
| 526 | return NULL; |
| 527 | |
| 528 | while (!nfs_lock_request(req: head)) { |
| 529 | ret = nfs_wait_on_request(req: head); |
| 530 | if (ret < 0) { |
| 531 | nfs_release_request(head); |
| 532 | return ERR_PTR(error: ret); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | ret = nfs_page_group_lock(head); |
| 537 | if (ret < 0) |
| 538 | goto out_unlock; |
| 539 | |
| 540 | /* Ensure that nobody removed the request before we locked it */ |
| 541 | if (head != folio->private) { |
| 542 | nfs_page_group_unlock(head); |
| 543 | nfs_unlock_and_release_request(head); |
| 544 | goto retry; |
| 545 | } |
| 546 | |
| 547 | nfs_cancel_remove_inode(req: head, inode); |
| 548 | |
| 549 | /* lock each request in the page group */ |
| 550 | for (subreq = head->wb_this_page; |
| 551 | subreq != head; |
| 552 | subreq = subreq->wb_this_page) { |
| 553 | ret = nfs_page_group_lock_subreq(head, subreq); |
| 554 | if (ret < 0) |
| 555 | goto out_unlock; |
| 556 | } |
| 557 | |
| 558 | nfs_page_group_unlock(head); |
| 559 | |
| 560 | nfs_init_cinfo_from_inode(cinfo: &cinfo, inode); |
| 561 | nfs_join_page_group(head, cinfo: &cinfo, inode); |
| 562 | return head; |
| 563 | |
| 564 | out_unlock: |
| 565 | nfs_unlock_and_release_request(head); |
| 566 | return ERR_PTR(error: ret); |
| 567 | } |
| 568 | |
| 569 | static void nfs_write_error(struct nfs_page *req, int error) |
| 570 | { |
| 571 | trace_nfs_write_error(inode: nfs_page_to_inode(req), req, error); |
| 572 | nfs_mapping_set_error(folio: nfs_page_to_folio(req), error); |
| 573 | nfs_inode_remove_request(req); |
| 574 | nfs_page_end_writeback(req); |
| 575 | nfs_release_request(req); |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Find an associated nfs write request, and prepare to flush it out |
| 580 | * May return an error if the user signalled nfs_wait_on_request(). |
| 581 | */ |
| 582 | static int nfs_do_writepage(struct folio *folio, struct writeback_control *wbc, |
| 583 | struct nfs_pageio_descriptor *pgio) |
| 584 | { |
| 585 | struct nfs_page *req; |
| 586 | int ret; |
| 587 | |
| 588 | nfs_pageio_cond_complete(pgio, folio->index); |
| 589 | |
| 590 | req = nfs_lock_and_join_requests(folio); |
| 591 | if (!req) |
| 592 | return 0; |
| 593 | if (IS_ERR(ptr: req)) |
| 594 | return PTR_ERR(ptr: req); |
| 595 | |
| 596 | trace_nfs_do_writepage(req); |
| 597 | nfs_folio_set_writeback(folio); |
| 598 | WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags)); |
| 599 | |
| 600 | /* If there is a fatal error that covers this write, just exit */ |
| 601 | ret = pgio->pg_error; |
| 602 | if (nfs_error_is_fatal_on_server(err: ret)) |
| 603 | goto out_launder; |
| 604 | |
| 605 | if (!nfs_pageio_add_request(pgio, req)) { |
| 606 | ret = pgio->pg_error; |
| 607 | /* |
| 608 | * Remove the problematic req upon fatal errors on the server |
| 609 | */ |
| 610 | if (nfs_error_is_fatal_on_server(err: ret)) |
| 611 | goto out_launder; |
| 612 | folio_redirty_for_writepage(wbc, folio); |
| 613 | nfs_redirty_request(req); |
| 614 | pgio->pg_error = 0; |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | nfs_add_stats(inode: folio->mapping->host, stat: NFSIOS_WRITEPAGES, addend: 1); |
| 619 | return 0; |
| 620 | |
| 621 | out_launder: |
| 622 | nfs_write_error(req, error: ret); |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Write an mmapped page to the server. |
| 628 | */ |
| 629 | static int nfs_writepage_locked(struct folio *folio, |
| 630 | struct writeback_control *wbc) |
| 631 | { |
| 632 | struct nfs_pageio_descriptor pgio; |
| 633 | struct inode *inode = folio->mapping->host; |
| 634 | int err; |
| 635 | |
| 636 | nfs_inc_stats(inode, stat: NFSIOS_VFSWRITEPAGE); |
| 637 | nfs_pageio_init_write(pgio: &pgio, inode, ioflags: 0, force_mds: false, |
| 638 | compl_ops: &nfs_async_write_completion_ops); |
| 639 | err = nfs_do_writepage(folio, wbc, pgio: &pgio); |
| 640 | pgio.pg_error = 0; |
| 641 | nfs_pageio_complete(desc: &pgio); |
| 642 | return err; |
| 643 | } |
| 644 | |
| 645 | static void nfs_io_completion_commit(void *inode) |
| 646 | { |
| 647 | nfs_commit_inode(inode, 0); |
| 648 | } |
| 649 | |
| 650 | int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) |
| 651 | { |
| 652 | struct inode *inode = mapping->host; |
| 653 | struct nfs_pageio_descriptor pgio; |
| 654 | struct nfs_io_completion *ioc = NULL; |
| 655 | unsigned int mntflags = NFS_SERVER(inode)->flags; |
| 656 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 657 | int priority = 0; |
| 658 | int err; |
| 659 | |
| 660 | trace_nfs_writepages(inode, offset: wbc->range_start, count: wbc->range_end - wbc->range_start); |
| 661 | |
| 662 | /* Wait with writeback until write congestion eases */ |
| 663 | if (wbc->sync_mode == WB_SYNC_NONE && nfss->write_congested) { |
| 664 | err = wait_event_killable(nfss->write_congestion_wait, |
| 665 | nfss->write_congested == 0); |
| 666 | if (err) |
| 667 | goto out_err; |
| 668 | } |
| 669 | |
| 670 | nfs_inc_stats(inode, stat: NFSIOS_VFSWRITEPAGES); |
| 671 | |
| 672 | if (!(mntflags & NFS_MOUNT_WRITE_EAGER) || wbc->for_kupdate || |
| 673 | wbc->for_background || wbc->for_sync) { |
| 674 | ioc = nfs_io_completion_alloc(GFP_KERNEL); |
| 675 | if (ioc) |
| 676 | nfs_io_completion_init(ioc, complete: nfs_io_completion_commit, |
| 677 | data: inode); |
| 678 | priority = wb_priority(wbc); |
| 679 | } |
| 680 | |
| 681 | do { |
| 682 | struct folio *folio = NULL; |
| 683 | |
| 684 | nfs_pageio_init_write(pgio: &pgio, inode, ioflags: priority, force_mds: false, |
| 685 | compl_ops: &nfs_async_write_completion_ops); |
| 686 | pgio.pg_io_completion = ioc; |
| 687 | while ((folio = writeback_iter(mapping, wbc, folio, error: &err))) { |
| 688 | err = nfs_do_writepage(folio, wbc, pgio: &pgio); |
| 689 | folio_unlock(folio); |
| 690 | } |
| 691 | pgio.pg_error = 0; |
| 692 | nfs_pageio_complete(desc: &pgio); |
| 693 | if (err == -EAGAIN && mntflags & NFS_MOUNT_SOFTERR) |
| 694 | break; |
| 695 | } while (err < 0 && !nfs_error_is_fatal(err)); |
| 696 | nfs_io_completion_put(ioc); |
| 697 | |
| 698 | if (err > 0) |
| 699 | err = 0; |
| 700 | out_err: |
| 701 | trace_nfs_writepages_done(inode, offset: wbc->range_start, count: wbc->range_end - wbc->range_start, ret: err); |
| 702 | return err; |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Insert a write request into an inode |
| 707 | */ |
| 708 | static void nfs_inode_add_request(struct nfs_page *req) |
| 709 | { |
| 710 | struct folio *folio = nfs_page_to_folio(req); |
| 711 | struct address_space *mapping = folio->mapping; |
| 712 | struct nfs_inode *nfsi = NFS_I(inode: mapping->host); |
| 713 | |
| 714 | WARN_ON_ONCE(req->wb_this_page != req); |
| 715 | |
| 716 | /* Lock the request! */ |
| 717 | nfs_lock_request(req); |
| 718 | spin_lock(lock: &mapping->i_private_lock); |
| 719 | set_bit(nr: PG_MAPPED, addr: &req->wb_flags); |
| 720 | folio_set_private(folio); |
| 721 | folio->private = req; |
| 722 | spin_unlock(lock: &mapping->i_private_lock); |
| 723 | atomic_long_inc(v: &nfsi->nrequests); |
| 724 | /* this a head request for a page group - mark it as having an |
| 725 | * extra reference so sub groups can follow suit. |
| 726 | * This flag also informs pgio layer when to bump nrequests when |
| 727 | * adding subrequests. */ |
| 728 | WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags)); |
| 729 | kref_get(kref: &req->wb_kref); |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Remove a write request from an inode |
| 734 | */ |
| 735 | static void nfs_inode_remove_request(struct nfs_page *req) |
| 736 | { |
| 737 | struct nfs_inode *nfsi = NFS_I(inode: nfs_page_to_inode(req)); |
| 738 | |
| 739 | nfs_page_group_lock(req); |
| 740 | if (nfs_page_group_sync_on_bit_locked(req, PG_REMOVE)) { |
| 741 | struct folio *folio = nfs_page_to_folio(req: req->wb_head); |
| 742 | struct address_space *mapping = folio->mapping; |
| 743 | |
| 744 | spin_lock(lock: &mapping->i_private_lock); |
| 745 | if (likely(folio)) { |
| 746 | folio->private = NULL; |
| 747 | folio_clear_private(folio); |
| 748 | clear_bit(nr: PG_MAPPED, addr: &req->wb_head->wb_flags); |
| 749 | } |
| 750 | spin_unlock(lock: &mapping->i_private_lock); |
| 751 | |
| 752 | folio_end_dropbehind(folio); |
| 753 | } |
| 754 | nfs_page_group_unlock(req); |
| 755 | |
| 756 | if (test_and_clear_bit(nr: PG_INODE_REF, addr: &req->wb_flags)) { |
| 757 | atomic_long_dec(v: &nfsi->nrequests); |
| 758 | nfs_release_request(req); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | static void nfs_mark_request_dirty(struct nfs_page *req) |
| 763 | { |
| 764 | struct folio *folio = nfs_page_to_folio(req); |
| 765 | if (folio) |
| 766 | filemap_dirty_folio(mapping: folio_mapping(folio), folio); |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * nfs_request_add_commit_list_locked - add request to a commit list |
| 771 | * @req: pointer to a struct nfs_page |
| 772 | * @dst: commit list head |
| 773 | * @cinfo: holds list lock and accounting info |
| 774 | * |
| 775 | * This sets the PG_CLEAN bit, updates the cinfo count of |
| 776 | * number of outstanding requests requiring a commit as well as |
| 777 | * the MM page stats. |
| 778 | * |
| 779 | * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the |
| 780 | * nfs_page lock. |
| 781 | */ |
| 782 | void |
| 783 | nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst, |
| 784 | struct nfs_commit_info *cinfo) |
| 785 | { |
| 786 | set_bit(nr: PG_CLEAN, addr: &req->wb_flags); |
| 787 | nfs_list_add_request(req, head: dst); |
| 788 | atomic_long_inc(v: &cinfo->mds->ncommit); |
| 789 | } |
| 790 | EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked); |
| 791 | |
| 792 | /** |
| 793 | * nfs_request_add_commit_list - add request to a commit list |
| 794 | * @req: pointer to a struct nfs_page |
| 795 | * @cinfo: holds list lock and accounting info |
| 796 | * |
| 797 | * This sets the PG_CLEAN bit, updates the cinfo count of |
| 798 | * number of outstanding requests requiring a commit as well as |
| 799 | * the MM page stats. |
| 800 | * |
| 801 | * The caller must _not_ hold the cinfo->lock, but must be |
| 802 | * holding the nfs_page lock. |
| 803 | */ |
| 804 | void |
| 805 | nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo) |
| 806 | { |
| 807 | mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); |
| 808 | nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo); |
| 809 | mutex_unlock(lock: &NFS_I(inode: cinfo->inode)->commit_mutex); |
| 810 | nfs_folio_mark_unstable(folio: nfs_page_to_folio(req), cinfo); |
| 811 | } |
| 812 | EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); |
| 813 | |
| 814 | /** |
| 815 | * nfs_request_remove_commit_list - Remove request from a commit list |
| 816 | * @req: pointer to a nfs_page |
| 817 | * @cinfo: holds list lock and accounting info |
| 818 | * |
| 819 | * This clears the PG_CLEAN bit, and updates the cinfo's count of |
| 820 | * number of outstanding requests requiring a commit |
| 821 | * It does not update the MM page stats. |
| 822 | * |
| 823 | * The caller _must_ hold the cinfo->lock and the nfs_page lock. |
| 824 | */ |
| 825 | void |
| 826 | nfs_request_remove_commit_list(struct nfs_page *req, |
| 827 | struct nfs_commit_info *cinfo) |
| 828 | { |
| 829 | if (!test_and_clear_bit(nr: PG_CLEAN, addr: &(req)->wb_flags)) |
| 830 | return; |
| 831 | nfs_list_remove_request(req); |
| 832 | atomic_long_dec(v: &cinfo->mds->ncommit); |
| 833 | } |
| 834 | EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); |
| 835 | |
| 836 | static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, |
| 837 | struct inode *inode) |
| 838 | { |
| 839 | cinfo->inode = inode; |
| 840 | cinfo->mds = &NFS_I(inode)->commit_info; |
| 841 | cinfo->ds = pnfs_get_ds_info(inode); |
| 842 | cinfo->dreq = NULL; |
| 843 | cinfo->completion_ops = &nfs_commit_completion_ops; |
| 844 | } |
| 845 | |
| 846 | void nfs_init_cinfo(struct nfs_commit_info *cinfo, |
| 847 | struct inode *inode, |
| 848 | struct nfs_direct_req *dreq) |
| 849 | { |
| 850 | if (dreq) |
| 851 | nfs_init_cinfo_from_dreq(cinfo, dreq); |
| 852 | else |
| 853 | nfs_init_cinfo_from_inode(cinfo, inode); |
| 854 | } |
| 855 | EXPORT_SYMBOL_GPL(nfs_init_cinfo); |
| 856 | |
| 857 | /* |
| 858 | * Add a request to the inode's commit list. |
| 859 | */ |
| 860 | void |
| 861 | nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, |
| 862 | struct nfs_commit_info *cinfo, u32 ds_commit_idx) |
| 863 | { |
| 864 | if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx)) |
| 865 | return; |
| 866 | nfs_request_add_commit_list(req, cinfo); |
| 867 | } |
| 868 | |
| 869 | static void nfs_folio_clear_commit(struct folio *folio) |
| 870 | { |
| 871 | if (folio) { |
| 872 | long nr = folio_nr_pages(folio); |
| 873 | |
| 874 | node_stat_mod_folio(folio, item: NR_WRITEBACK, nr: -nr); |
| 875 | wb_stat_mod(wb: &inode_to_bdi(inode: folio->mapping->host)->wb, |
| 876 | item: WB_WRITEBACK, amount: -nr); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | /* Called holding the request lock on @req */ |
| 881 | static void nfs_clear_request_commit(struct nfs_commit_info *cinfo, |
| 882 | struct nfs_page *req) |
| 883 | { |
| 884 | if (test_bit(PG_CLEAN, &req->wb_flags)) { |
| 885 | struct nfs_open_context *ctx = nfs_req_openctx(req); |
| 886 | struct inode *inode = d_inode(dentry: ctx->dentry); |
| 887 | |
| 888 | mutex_lock(&NFS_I(inode)->commit_mutex); |
| 889 | if (!pnfs_clear_request_commit(req, cinfo)) { |
| 890 | nfs_request_remove_commit_list(req, cinfo); |
| 891 | } |
| 892 | mutex_unlock(lock: &NFS_I(inode)->commit_mutex); |
| 893 | nfs_folio_clear_commit(folio: nfs_page_to_folio(req)); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | int nfs_write_need_commit(struct nfs_pgio_header *hdr) |
| 898 | { |
| 899 | if (hdr->verf.committed == NFS_DATA_SYNC) |
| 900 | return hdr->lseg == NULL; |
| 901 | return hdr->verf.committed != NFS_FILE_SYNC; |
| 902 | } |
| 903 | |
| 904 | static void nfs_async_write_init(struct nfs_pgio_header *hdr) |
| 905 | { |
| 906 | nfs_io_completion_get(ioc: hdr->io_completion); |
| 907 | } |
| 908 | |
| 909 | static void nfs_write_completion(struct nfs_pgio_header *hdr) |
| 910 | { |
| 911 | struct nfs_commit_info cinfo; |
| 912 | unsigned long bytes = 0; |
| 913 | |
| 914 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) |
| 915 | goto out; |
| 916 | nfs_init_cinfo_from_inode(cinfo: &cinfo, inode: hdr->inode); |
| 917 | while (!list_empty(head: &hdr->pages)) { |
| 918 | struct nfs_page *req = nfs_list_entry(head: hdr->pages.next); |
| 919 | |
| 920 | bytes += req->wb_bytes; |
| 921 | nfs_list_remove_request(req); |
| 922 | if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && |
| 923 | (hdr->good_bytes < bytes)) { |
| 924 | trace_nfs_comp_error(inode: hdr->inode, req, error: hdr->error); |
| 925 | nfs_mapping_set_error(folio: nfs_page_to_folio(req), |
| 926 | error: hdr->error); |
| 927 | goto remove_req; |
| 928 | } |
| 929 | if (nfs_write_need_commit(hdr)) { |
| 930 | /* Reset wb_nio, since the write was successful. */ |
| 931 | req->wb_nio = 0; |
| 932 | memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf)); |
| 933 | nfs_mark_request_commit(req, lseg: hdr->lseg, cinfo: &cinfo, |
| 934 | ds_commit_idx: hdr->ds_commit_idx); |
| 935 | goto next; |
| 936 | } |
| 937 | remove_req: |
| 938 | nfs_inode_remove_request(req); |
| 939 | next: |
| 940 | nfs_page_end_writeback(req); |
| 941 | nfs_release_request(req); |
| 942 | } |
| 943 | out: |
| 944 | nfs_io_completion_put(ioc: hdr->io_completion); |
| 945 | hdr->release(hdr); |
| 946 | } |
| 947 | |
| 948 | unsigned long |
| 949 | nfs_reqs_to_commit(struct nfs_commit_info *cinfo) |
| 950 | { |
| 951 | return atomic_long_read(v: &cinfo->mds->ncommit); |
| 952 | } |
| 953 | |
| 954 | /* NFS_I(cinfo->inode)->commit_mutex held by caller */ |
| 955 | int |
| 956 | nfs_scan_commit_list(struct list_head *src, struct list_head *dst, |
| 957 | struct nfs_commit_info *cinfo, int max) |
| 958 | { |
| 959 | struct nfs_page *req, *tmp; |
| 960 | int ret = 0; |
| 961 | |
| 962 | list_for_each_entry_safe(req, tmp, src, wb_list) { |
| 963 | kref_get(kref: &req->wb_kref); |
| 964 | if (!nfs_lock_request(req)) { |
| 965 | nfs_release_request(req); |
| 966 | continue; |
| 967 | } |
| 968 | nfs_request_remove_commit_list(req, cinfo); |
| 969 | clear_bit(nr: PG_COMMIT_TO_DS, addr: &req->wb_flags); |
| 970 | nfs_list_add_request(req, head: dst); |
| 971 | ret++; |
| 972 | if ((ret == max) && !cinfo->dreq) |
| 973 | break; |
| 974 | cond_resched(); |
| 975 | } |
| 976 | return ret; |
| 977 | } |
| 978 | EXPORT_SYMBOL_GPL(nfs_scan_commit_list); |
| 979 | |
| 980 | /* |
| 981 | * nfs_scan_commit - Scan an inode for commit requests |
| 982 | * @inode: NFS inode to scan |
| 983 | * @dst: mds destination list |
| 984 | * @cinfo: mds and ds lists of reqs ready to commit |
| 985 | * |
| 986 | * Moves requests from the inode's 'commit' request list. |
| 987 | * The requests are *not* checked to ensure that they form a contiguous set. |
| 988 | */ |
| 989 | int |
| 990 | nfs_scan_commit(struct inode *inode, struct list_head *dst, |
| 991 | struct nfs_commit_info *cinfo) |
| 992 | { |
| 993 | int ret = 0; |
| 994 | |
| 995 | if (!atomic_long_read(v: &cinfo->mds->ncommit)) |
| 996 | return 0; |
| 997 | mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); |
| 998 | if (atomic_long_read(v: &cinfo->mds->ncommit) > 0) { |
| 999 | const int max = INT_MAX; |
| 1000 | |
| 1001 | ret = nfs_scan_commit_list(&cinfo->mds->list, dst, |
| 1002 | cinfo, max); |
| 1003 | ret += pnfs_scan_commit_lists(inode, cinfo, max: max - ret); |
| 1004 | } |
| 1005 | mutex_unlock(lock: &NFS_I(inode: cinfo->inode)->commit_mutex); |
| 1006 | return ret; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * Search for an existing write request, and attempt to update |
| 1011 | * it to reflect a new dirty region on a given page. |
| 1012 | * |
| 1013 | * If the attempt fails, then the existing request is flushed out |
| 1014 | * to disk. |
| 1015 | */ |
| 1016 | static struct nfs_page *nfs_try_to_update_request(struct folio *folio, |
| 1017 | unsigned int offset, |
| 1018 | unsigned int bytes) |
| 1019 | { |
| 1020 | struct nfs_page *req; |
| 1021 | unsigned int rqend; |
| 1022 | unsigned int end; |
| 1023 | int error; |
| 1024 | |
| 1025 | trace_nfs_try_to_update_request(inode: folio_inode(folio), offset, count: bytes); |
| 1026 | end = offset + bytes; |
| 1027 | |
| 1028 | req = nfs_lock_and_join_requests(folio); |
| 1029 | if (IS_ERR_OR_NULL(ptr: req)) |
| 1030 | goto out; |
| 1031 | |
| 1032 | rqend = req->wb_offset + req->wb_bytes; |
| 1033 | /* |
| 1034 | * Tell the caller to flush out the request if |
| 1035 | * the offsets are non-contiguous. |
| 1036 | * Note: nfs_flush_incompatible() will already |
| 1037 | * have flushed out requests having wrong owners. |
| 1038 | */ |
| 1039 | if (offset > rqend || end < req->wb_offset) |
| 1040 | goto out_flushme; |
| 1041 | |
| 1042 | /* Okay, the request matches. Update the region */ |
| 1043 | if (offset < req->wb_offset) { |
| 1044 | req->wb_offset = offset; |
| 1045 | req->wb_pgbase = offset; |
| 1046 | } |
| 1047 | if (end > rqend) |
| 1048 | req->wb_bytes = end - req->wb_offset; |
| 1049 | else |
| 1050 | req->wb_bytes = rqend - req->wb_offset; |
| 1051 | req->wb_nio = 0; |
| 1052 | out: |
| 1053 | trace_nfs_try_to_update_request_done(inode: folio_inode(folio), offset, count: bytes, |
| 1054 | ret: PTR_ERR_OR_ZERO(ptr: req)); |
| 1055 | return req; |
| 1056 | out_flushme: |
| 1057 | /* |
| 1058 | * Note: we mark the request dirty here because |
| 1059 | * nfs_lock_and_join_requests() cannot preserve |
| 1060 | * commit flags, so we have to replay the write. |
| 1061 | */ |
| 1062 | nfs_mark_request_dirty(req); |
| 1063 | nfs_unlock_and_release_request(req); |
| 1064 | error = nfs_wb_folio(inode: folio->mapping->host, folio); |
| 1065 | trace_nfs_try_to_update_request_done(inode: folio_inode(folio), offset, count: bytes, ret: error); |
| 1066 | return (error < 0) ? ERR_PTR(error) : NULL; |
| 1067 | } |
| 1068 | |
| 1069 | /* |
| 1070 | * Try to update an existing write request, or create one if there is none. |
| 1071 | * |
| 1072 | * Note: Should always be called with the Page Lock held to prevent races |
| 1073 | * if we have to add a new request. Also assumes that the caller has |
| 1074 | * already called nfs_flush_incompatible() if necessary. |
| 1075 | */ |
| 1076 | static struct nfs_page *nfs_setup_write_request(struct nfs_open_context *ctx, |
| 1077 | struct folio *folio, |
| 1078 | unsigned int offset, |
| 1079 | unsigned int bytes) |
| 1080 | { |
| 1081 | struct nfs_page *req; |
| 1082 | |
| 1083 | req = nfs_try_to_update_request(folio, offset, bytes); |
| 1084 | if (req != NULL) |
| 1085 | goto out; |
| 1086 | req = nfs_page_create_from_folio(ctx, folio, offset, count: bytes); |
| 1087 | if (IS_ERR(ptr: req)) |
| 1088 | goto out; |
| 1089 | nfs_inode_add_request(req); |
| 1090 | out: |
| 1091 | return req; |
| 1092 | } |
| 1093 | |
| 1094 | static int nfs_writepage_setup(struct nfs_open_context *ctx, |
| 1095 | struct folio *folio, unsigned int offset, |
| 1096 | unsigned int count) |
| 1097 | { |
| 1098 | struct nfs_page *req; |
| 1099 | |
| 1100 | req = nfs_setup_write_request(ctx, folio, offset, bytes: count); |
| 1101 | if (IS_ERR(ptr: req)) |
| 1102 | return PTR_ERR(ptr: req); |
| 1103 | trace_nfs_writepage_setup(req); |
| 1104 | /* Update file length */ |
| 1105 | nfs_grow_file(folio, offset, count); |
| 1106 | nfs_mark_uptodate(req); |
| 1107 | nfs_mark_request_dirty(req); |
| 1108 | nfs_unlock_and_release_request(req); |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | int nfs_flush_incompatible(struct file *file, struct folio *folio) |
| 1113 | { |
| 1114 | struct nfs_open_context *ctx = nfs_file_open_context(filp: file); |
| 1115 | struct nfs_lock_context *l_ctx; |
| 1116 | struct file_lock_context *flctx = locks_inode_context(inode: file_inode(f: file)); |
| 1117 | struct nfs_page *req; |
| 1118 | int do_flush, status; |
| 1119 | /* |
| 1120 | * Look for a request corresponding to this page. If there |
| 1121 | * is one, and it belongs to another file, we flush it out |
| 1122 | * before we try to copy anything into the page. Do this |
| 1123 | * due to the lack of an ACCESS-type call in NFSv2. |
| 1124 | * Also do the same if we find a request from an existing |
| 1125 | * dropped page. |
| 1126 | */ |
| 1127 | do { |
| 1128 | req = nfs_folio_find_head_request(folio); |
| 1129 | if (req == NULL) |
| 1130 | return 0; |
| 1131 | l_ctx = req->wb_lock_context; |
| 1132 | do_flush = nfs_page_to_folio(req) != folio || |
| 1133 | !nfs_match_open_context(ctx1: nfs_req_openctx(req), ctx2: ctx); |
| 1134 | if (l_ctx && flctx && |
| 1135 | !(list_empty_careful(head: &flctx->flc_posix) && |
| 1136 | list_empty_careful(head: &flctx->flc_flock))) { |
| 1137 | do_flush |= l_ctx->lockowner != current->files; |
| 1138 | } |
| 1139 | nfs_release_request(req); |
| 1140 | if (!do_flush) |
| 1141 | return 0; |
| 1142 | status = nfs_wb_folio(inode: folio->mapping->host, folio); |
| 1143 | } while (status == 0); |
| 1144 | return status; |
| 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | * Avoid buffered writes when a open context credential's key would |
| 1149 | * expire soon. |
| 1150 | * |
| 1151 | * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL. |
| 1152 | * |
| 1153 | * Return 0 and set a credential flag which triggers the inode to flush |
| 1154 | * and performs NFS_FILE_SYNC writes if the key will expired within |
| 1155 | * RPC_KEY_EXPIRE_TIMEO. |
| 1156 | */ |
| 1157 | int |
| 1158 | nfs_key_timeout_notify(struct file *filp, struct inode *inode) |
| 1159 | { |
| 1160 | struct nfs_open_context *ctx = nfs_file_open_context(filp); |
| 1161 | |
| 1162 | if (nfs_ctx_key_to_expire(ctx, inode) && |
| 1163 | !rcu_access_pointer(ctx->ll_cred)) |
| 1164 | /* Already expired! */ |
| 1165 | return -EACCES; |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | * Test if the open context credential key is marked to expire soon. |
| 1171 | */ |
| 1172 | bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode) |
| 1173 | { |
| 1174 | struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; |
| 1175 | struct rpc_cred *cred, *new, *old = NULL; |
| 1176 | struct auth_cred acred = { |
| 1177 | .cred = ctx->cred, |
| 1178 | }; |
| 1179 | bool ret = false; |
| 1180 | |
| 1181 | rcu_read_lock(); |
| 1182 | cred = rcu_dereference(ctx->ll_cred); |
| 1183 | if (cred && !(cred->cr_ops->crkey_timeout && |
| 1184 | cred->cr_ops->crkey_timeout(cred))) |
| 1185 | goto out; |
| 1186 | rcu_read_unlock(); |
| 1187 | |
| 1188 | new = auth->au_ops->lookup_cred(auth, &acred, 0); |
| 1189 | if (new == cred) { |
| 1190 | put_rpccred(new); |
| 1191 | return true; |
| 1192 | } |
| 1193 | if (IS_ERR_OR_NULL(ptr: new)) { |
| 1194 | new = NULL; |
| 1195 | ret = true; |
| 1196 | } else if (new->cr_ops->crkey_timeout && |
| 1197 | new->cr_ops->crkey_timeout(new)) |
| 1198 | ret = true; |
| 1199 | |
| 1200 | rcu_read_lock(); |
| 1201 | old = rcu_dereference_protected(xchg(&ctx->ll_cred, |
| 1202 | RCU_INITIALIZER(new)), 1); |
| 1203 | out: |
| 1204 | rcu_read_unlock(); |
| 1205 | put_rpccred(old); |
| 1206 | return ret; |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * If the page cache is marked as unsafe or invalid, then we can't rely on |
| 1211 | * the PageUptodate() flag. In this case, we will need to turn off |
| 1212 | * write optimisations that depend on the page contents being correct. |
| 1213 | */ |
| 1214 | static bool nfs_folio_write_uptodate(struct folio *folio, unsigned int pagelen) |
| 1215 | { |
| 1216 | struct inode *inode = folio->mapping->host; |
| 1217 | struct nfs_inode *nfsi = NFS_I(inode); |
| 1218 | |
| 1219 | if (nfs_have_delegated_attributes(inode)) |
| 1220 | goto out; |
| 1221 | if (nfsi->cache_validity & |
| 1222 | (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE)) |
| 1223 | return false; |
| 1224 | smp_rmb(); |
| 1225 | if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags) && pagelen != 0) |
| 1226 | return false; |
| 1227 | out: |
| 1228 | if (nfsi->cache_validity & NFS_INO_INVALID_DATA && pagelen != 0) |
| 1229 | return false; |
| 1230 | return folio_test_uptodate(folio) != 0; |
| 1231 | } |
| 1232 | |
| 1233 | static bool |
| 1234 | is_whole_file_wrlock(struct file_lock *fl) |
| 1235 | { |
| 1236 | return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX && |
| 1237 | lock_is_write(fl); |
| 1238 | } |
| 1239 | |
| 1240 | /* If we know the page is up to date, and we're not using byte range locks (or |
| 1241 | * if we have the whole file locked for writing), it may be more efficient to |
| 1242 | * extend the write to cover the entire page in order to avoid fragmentation |
| 1243 | * inefficiencies. |
| 1244 | * |
| 1245 | * If the file is opened for synchronous writes then we can just skip the rest |
| 1246 | * of the checks. |
| 1247 | */ |
| 1248 | static int nfs_can_extend_write(struct file *file, struct folio *folio, |
| 1249 | unsigned int pagelen) |
| 1250 | { |
| 1251 | struct inode *inode = file_inode(f: file); |
| 1252 | struct file_lock_context *flctx = locks_inode_context(inode); |
| 1253 | struct file_lock *fl; |
| 1254 | int ret; |
| 1255 | unsigned int mntflags = NFS_SERVER(inode)->flags; |
| 1256 | |
| 1257 | if (mntflags & NFS_MOUNT_NO_ALIGNWRITE) |
| 1258 | return 0; |
| 1259 | if (file->f_flags & O_DSYNC) |
| 1260 | return 0; |
| 1261 | if (!nfs_folio_write_uptodate(folio, pagelen)) |
| 1262 | return 0; |
| 1263 | if (nfs_have_write_delegation(inode)) |
| 1264 | return 1; |
| 1265 | if (!flctx || (list_empty_careful(head: &flctx->flc_flock) && |
| 1266 | list_empty_careful(head: &flctx->flc_posix))) |
| 1267 | return 1; |
| 1268 | |
| 1269 | /* Check to see if there are whole file write locks */ |
| 1270 | ret = 0; |
| 1271 | spin_lock(lock: &flctx->flc_lock); |
| 1272 | if (!list_empty(head: &flctx->flc_posix)) { |
| 1273 | fl = list_first_entry(&flctx->flc_posix, struct file_lock, |
| 1274 | c.flc_list); |
| 1275 | if (is_whole_file_wrlock(fl)) |
| 1276 | ret = 1; |
| 1277 | } else if (!list_empty(head: &flctx->flc_flock)) { |
| 1278 | fl = list_first_entry(&flctx->flc_flock, struct file_lock, |
| 1279 | c.flc_list); |
| 1280 | if (lock_is_write(fl)) |
| 1281 | ret = 1; |
| 1282 | } |
| 1283 | spin_unlock(lock: &flctx->flc_lock); |
| 1284 | return ret; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * Update and possibly write a cached page of an NFS file. |
| 1289 | * |
| 1290 | * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad |
| 1291 | * things with a page scheduled for an RPC call (e.g. invalidate it). |
| 1292 | */ |
| 1293 | int nfs_update_folio(struct file *file, struct folio *folio, |
| 1294 | unsigned int offset, unsigned int count) |
| 1295 | { |
| 1296 | struct nfs_open_context *ctx = nfs_file_open_context(filp: file); |
| 1297 | struct address_space *mapping = folio->mapping; |
| 1298 | struct inode *inode = mapping->host; |
| 1299 | unsigned int pagelen = nfs_folio_length(folio); |
| 1300 | int status = 0; |
| 1301 | |
| 1302 | nfs_inc_stats(inode, stat: NFSIOS_VFSUPDATEPAGE); |
| 1303 | |
| 1304 | trace_nfs_update_folio(inode, offset, count); |
| 1305 | |
| 1306 | dprintk("NFS: nfs_update_folio(%pD2 %d@%lld)\n" , file, count, |
| 1307 | (long long)(folio_pos(folio) + offset)); |
| 1308 | |
| 1309 | if (!count) |
| 1310 | goto out; |
| 1311 | |
| 1312 | if (nfs_can_extend_write(file, folio, pagelen)) { |
| 1313 | unsigned int end = count + offset; |
| 1314 | |
| 1315 | offset = round_down(offset, PAGE_SIZE); |
| 1316 | if (end < pagelen) |
| 1317 | end = min(round_up(end, PAGE_SIZE), pagelen); |
| 1318 | count = end - offset; |
| 1319 | } |
| 1320 | |
| 1321 | status = nfs_writepage_setup(ctx, folio, offset, count); |
| 1322 | if (status < 0) |
| 1323 | nfs_set_pageerror(mapping); |
| 1324 | out: |
| 1325 | trace_nfs_update_folio_done(inode, offset, count, ret: status); |
| 1326 | dprintk("NFS: nfs_update_folio returns %d (isize %lld)\n" , |
| 1327 | status, (long long)i_size_read(inode)); |
| 1328 | return status; |
| 1329 | } |
| 1330 | |
| 1331 | static int flush_task_priority(int how) |
| 1332 | { |
| 1333 | switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { |
| 1334 | case FLUSH_HIGHPRI: |
| 1335 | return RPC_PRIORITY_HIGH; |
| 1336 | case FLUSH_LOWPRI: |
| 1337 | return RPC_PRIORITY_LOW; |
| 1338 | } |
| 1339 | return RPC_PRIORITY_NORMAL; |
| 1340 | } |
| 1341 | |
| 1342 | static void nfs_initiate_write(struct nfs_pgio_header *hdr, |
| 1343 | struct rpc_message *msg, |
| 1344 | const struct nfs_rpc_ops *rpc_ops, |
| 1345 | struct rpc_task_setup *task_setup_data, int how) |
| 1346 | { |
| 1347 | int priority = flush_task_priority(how); |
| 1348 | |
| 1349 | if (IS_SWAPFILE(hdr->inode)) |
| 1350 | task_setup_data->flags |= RPC_TASK_SWAPPER; |
| 1351 | task_setup_data->priority = priority; |
| 1352 | rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client); |
| 1353 | trace_nfs_initiate_write(hdr); |
| 1354 | } |
| 1355 | |
| 1356 | /* If a nfs_flush_* function fails, it should remove reqs from @head and |
| 1357 | * call this on each, which will prepare them to be retried on next |
| 1358 | * writeback using standard nfs. |
| 1359 | */ |
| 1360 | static void nfs_redirty_request(struct nfs_page *req) |
| 1361 | { |
| 1362 | struct nfs_inode *nfsi = NFS_I(inode: nfs_page_to_inode(req)); |
| 1363 | |
| 1364 | /* Bump the transmission count */ |
| 1365 | req->wb_nio++; |
| 1366 | nfs_mark_request_dirty(req); |
| 1367 | atomic_long_inc(v: &nfsi->redirtied_pages); |
| 1368 | nfs_page_end_writeback(req); |
| 1369 | nfs_release_request(req); |
| 1370 | } |
| 1371 | |
| 1372 | static void nfs_async_write_error(struct list_head *head, int error) |
| 1373 | { |
| 1374 | struct nfs_page *req; |
| 1375 | |
| 1376 | while (!list_empty(head)) { |
| 1377 | req = nfs_list_entry(head: head->next); |
| 1378 | nfs_list_remove_request(req); |
| 1379 | if (nfs_error_is_fatal_on_server(err: error)) |
| 1380 | nfs_write_error(req, error); |
| 1381 | else |
| 1382 | nfs_redirty_request(req); |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr) |
| 1387 | { |
| 1388 | nfs_async_write_error(head: &hdr->pages, error: 0); |
| 1389 | } |
| 1390 | |
| 1391 | static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { |
| 1392 | .init_hdr = nfs_async_write_init, |
| 1393 | .error_cleanup = nfs_async_write_error, |
| 1394 | .completion = nfs_write_completion, |
| 1395 | .reschedule_io = nfs_async_write_reschedule_io, |
| 1396 | }; |
| 1397 | |
| 1398 | void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, |
| 1399 | struct inode *inode, int ioflags, bool force_mds, |
| 1400 | const struct nfs_pgio_completion_ops *compl_ops) |
| 1401 | { |
| 1402 | struct nfs_server *server = NFS_SERVER(inode); |
| 1403 | const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops; |
| 1404 | |
| 1405 | #ifdef CONFIG_NFS_V4_1 |
| 1406 | if (server->pnfs_curr_ld && !force_mds) |
| 1407 | pg_ops = server->pnfs_curr_ld->pg_write_ops; |
| 1408 | #endif |
| 1409 | nfs_pageio_init(desc: pgio, inode, pg_ops, compl_ops, rw_ops: &nfs_rw_write_ops, |
| 1410 | bsize: server->wsize, how: ioflags); |
| 1411 | } |
| 1412 | EXPORT_SYMBOL_GPL(nfs_pageio_init_write); |
| 1413 | |
| 1414 | void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) |
| 1415 | { |
| 1416 | struct nfs_pgio_mirror *mirror; |
| 1417 | |
| 1418 | if (pgio->pg_ops && pgio->pg_ops->pg_cleanup) |
| 1419 | pgio->pg_ops->pg_cleanup(pgio); |
| 1420 | |
| 1421 | pgio->pg_ops = &nfs_pgio_rw_ops; |
| 1422 | |
| 1423 | nfs_pageio_stop_mirroring(pgio); |
| 1424 | |
| 1425 | mirror = &pgio->pg_mirrors[0]; |
| 1426 | mirror->pg_bsize = NFS_SERVER(inode: pgio->pg_inode)->wsize; |
| 1427 | } |
| 1428 | EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); |
| 1429 | |
| 1430 | |
| 1431 | void nfs_commit_prepare(struct rpc_task *task, void *calldata) |
| 1432 | { |
| 1433 | struct nfs_commit_data *data = calldata; |
| 1434 | |
| 1435 | NFS_PROTO(inode: data->inode)->commit_rpc_prepare(task, data); |
| 1436 | } |
| 1437 | |
| 1438 | static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr, |
| 1439 | struct nfs_fattr *fattr) |
| 1440 | { |
| 1441 | struct nfs_pgio_args *argp = &hdr->args; |
| 1442 | struct nfs_pgio_res *resp = &hdr->res; |
| 1443 | u64 size = argp->offset + resp->count; |
| 1444 | |
| 1445 | if (!(fattr->valid & NFS_ATTR_FATTR_SIZE)) |
| 1446 | fattr->size = size; |
| 1447 | if (nfs_size_to_loff_t(size: fattr->size) < i_size_read(inode: hdr->inode)) { |
| 1448 | fattr->valid &= ~NFS_ATTR_FATTR_SIZE; |
| 1449 | return; |
| 1450 | } |
| 1451 | if (size != fattr->size) |
| 1452 | return; |
| 1453 | /* Set attribute barrier */ |
| 1454 | nfs_fattr_set_barrier(fattr); |
| 1455 | /* ...and update size */ |
| 1456 | fattr->valid |= NFS_ATTR_FATTR_SIZE; |
| 1457 | } |
| 1458 | |
| 1459 | void nfs_writeback_update_inode(struct nfs_pgio_header *hdr) |
| 1460 | { |
| 1461 | struct nfs_fattr *fattr = &hdr->fattr; |
| 1462 | struct inode *inode = hdr->inode; |
| 1463 | |
| 1464 | if (nfs_have_delegated_mtime(inode)) { |
| 1465 | spin_lock(lock: &inode->i_lock); |
| 1466 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS); |
| 1467 | spin_unlock(lock: &inode->i_lock); |
| 1468 | return; |
| 1469 | } |
| 1470 | |
| 1471 | spin_lock(lock: &inode->i_lock); |
| 1472 | nfs_writeback_check_extend(hdr, fattr); |
| 1473 | nfs_post_op_update_inode_force_wcc_locked(inode, fattr); |
| 1474 | spin_unlock(lock: &inode->i_lock); |
| 1475 | } |
| 1476 | EXPORT_SYMBOL_GPL(nfs_writeback_update_inode); |
| 1477 | |
| 1478 | /* |
| 1479 | * This function is called when the WRITE call is complete. |
| 1480 | */ |
| 1481 | static int nfs_writeback_done(struct rpc_task *task, |
| 1482 | struct nfs_pgio_header *hdr, |
| 1483 | struct inode *inode) |
| 1484 | { |
| 1485 | int status; |
| 1486 | |
| 1487 | /* |
| 1488 | * ->write_done will attempt to use post-op attributes to detect |
| 1489 | * conflicting writes by other clients. A strict interpretation |
| 1490 | * of close-to-open would allow us to continue caching even if |
| 1491 | * another writer had changed the file, but some applications |
| 1492 | * depend on tighter cache coherency when writing. |
| 1493 | */ |
| 1494 | status = NFS_PROTO(inode)->write_done(task, hdr); |
| 1495 | if (status != 0) |
| 1496 | return status; |
| 1497 | |
| 1498 | nfs_add_stats(inode, stat: NFSIOS_SERVERWRITTENBYTES, addend: hdr->res.count); |
| 1499 | trace_nfs_writeback_done(task, hdr); |
| 1500 | |
| 1501 | if (task->tk_status >= 0) { |
| 1502 | enum nfs3_stable_how committed = hdr->res.verf->committed; |
| 1503 | |
| 1504 | if (committed == NFS_UNSTABLE) { |
| 1505 | /* |
| 1506 | * We have some uncommitted data on the server at |
| 1507 | * this point, so ensure that we keep track of that |
| 1508 | * fact irrespective of what later writes do. |
| 1509 | */ |
| 1510 | set_bit(nr: NFS_IOHDR_UNSTABLE_WRITES, addr: &hdr->flags); |
| 1511 | } |
| 1512 | |
| 1513 | if (committed < hdr->args.stable) { |
| 1514 | /* We tried a write call, but the server did not |
| 1515 | * commit data to stable storage even though we |
| 1516 | * requested it. |
| 1517 | * Note: There is a known bug in Tru64 < 5.0 in which |
| 1518 | * the server reports NFS_DATA_SYNC, but performs |
| 1519 | * NFS_FILE_SYNC. We therefore implement this checking |
| 1520 | * as a dprintk() in order to avoid filling syslog. |
| 1521 | */ |
| 1522 | static unsigned long complain; |
| 1523 | |
| 1524 | /* Note this will print the MDS for a DS write */ |
| 1525 | if (time_before(complain, jiffies)) { |
| 1526 | dprintk("NFS: faulty NFS server %s:" |
| 1527 | " (committed = %d) != (stable = %d)\n" , |
| 1528 | NFS_SERVER(inode)->nfs_client->cl_hostname, |
| 1529 | committed, hdr->args.stable); |
| 1530 | complain = jiffies + 300 * HZ; |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | /* Deal with the suid/sgid bit corner case */ |
| 1536 | if (nfs_should_remove_suid(inode)) { |
| 1537 | spin_lock(lock: &inode->i_lock); |
| 1538 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE |
| 1539 | | NFS_INO_REVAL_FORCED); |
| 1540 | spin_unlock(lock: &inode->i_lock); |
| 1541 | } |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | /* |
| 1546 | * This function is called when the WRITE call is complete. |
| 1547 | */ |
| 1548 | static void nfs_writeback_result(struct rpc_task *task, |
| 1549 | struct nfs_pgio_header *hdr) |
| 1550 | { |
| 1551 | struct nfs_pgio_args *argp = &hdr->args; |
| 1552 | struct nfs_pgio_res *resp = &hdr->res; |
| 1553 | |
| 1554 | if (resp->count < argp->count) { |
| 1555 | static unsigned long complain; |
| 1556 | |
| 1557 | /* This a short write! */ |
| 1558 | nfs_inc_stats(inode: hdr->inode, stat: NFSIOS_SHORTWRITE); |
| 1559 | |
| 1560 | /* Has the server at least made some progress? */ |
| 1561 | if (resp->count == 0) { |
| 1562 | if (time_before(complain, jiffies)) { |
| 1563 | printk(KERN_WARNING |
| 1564 | "NFS: Server wrote zero bytes, expected %u.\n" , |
| 1565 | argp->count); |
| 1566 | complain = jiffies + 300 * HZ; |
| 1567 | } |
| 1568 | nfs_set_pgio_error(hdr, error: -EIO, pos: argp->offset); |
| 1569 | task->tk_status = -EIO; |
| 1570 | return; |
| 1571 | } |
| 1572 | |
| 1573 | /* For non rpc-based layout drivers, retry-through-MDS */ |
| 1574 | if (!task->tk_ops) { |
| 1575 | hdr->pnfs_error = -EAGAIN; |
| 1576 | return; |
| 1577 | } |
| 1578 | |
| 1579 | /* Was this an NFSv2 write or an NFSv3 stable write? */ |
| 1580 | if (resp->verf->committed != NFS_UNSTABLE) { |
| 1581 | /* Resend from where the server left off */ |
| 1582 | hdr->mds_offset += resp->count; |
| 1583 | argp->offset += resp->count; |
| 1584 | argp->pgbase += resp->count; |
| 1585 | argp->count -= resp->count; |
| 1586 | } else { |
| 1587 | /* Resend as a stable write in order to avoid |
| 1588 | * headaches in the case of a server crash. |
| 1589 | */ |
| 1590 | argp->stable = NFS_FILE_SYNC; |
| 1591 | } |
| 1592 | resp->count = 0; |
| 1593 | resp->verf->committed = 0; |
| 1594 | rpc_restart_call_prepare(task); |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | static int wait_on_commit(struct nfs_mds_commit_info *cinfo) |
| 1599 | { |
| 1600 | return wait_var_event_killable(&cinfo->rpcs_out, |
| 1601 | !atomic_read(&cinfo->rpcs_out)); |
| 1602 | } |
| 1603 | |
| 1604 | void nfs_commit_begin(struct nfs_mds_commit_info *cinfo) |
| 1605 | { |
| 1606 | atomic_inc(v: &cinfo->rpcs_out); |
| 1607 | } |
| 1608 | |
| 1609 | bool nfs_commit_end(struct nfs_mds_commit_info *cinfo) |
| 1610 | { |
| 1611 | if (atomic_dec_and_test(v: &cinfo->rpcs_out)) { |
| 1612 | wake_up_var(var: &cinfo->rpcs_out); |
| 1613 | return true; |
| 1614 | } |
| 1615 | return false; |
| 1616 | } |
| 1617 | |
| 1618 | void nfs_commitdata_release(struct nfs_commit_data *data) |
| 1619 | { |
| 1620 | put_nfs_open_context(ctx: data->context); |
| 1621 | nfs_commit_free(data); |
| 1622 | } |
| 1623 | EXPORT_SYMBOL_GPL(nfs_commitdata_release); |
| 1624 | |
| 1625 | int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, |
| 1626 | const struct nfs_rpc_ops *nfs_ops, |
| 1627 | const struct rpc_call_ops *call_ops, |
| 1628 | int how, int flags, |
| 1629 | struct nfsd_file *localio) |
| 1630 | { |
| 1631 | struct rpc_task *task; |
| 1632 | int priority = flush_task_priority(how); |
| 1633 | struct rpc_message msg = { |
| 1634 | .rpc_argp = &data->args, |
| 1635 | .rpc_resp = &data->res, |
| 1636 | .rpc_cred = data->cred, |
| 1637 | }; |
| 1638 | struct rpc_task_setup task_setup_data = { |
| 1639 | .task = &data->task, |
| 1640 | .rpc_client = clnt, |
| 1641 | .rpc_message = &msg, |
| 1642 | .callback_ops = call_ops, |
| 1643 | .callback_data = data, |
| 1644 | .workqueue = nfsiod_workqueue, |
| 1645 | .flags = RPC_TASK_ASYNC | flags, |
| 1646 | .priority = priority, |
| 1647 | }; |
| 1648 | |
| 1649 | if (nfs_server_capable(inode: data->inode, NFS_CAP_MOVEABLE)) |
| 1650 | task_setup_data.flags |= RPC_TASK_MOVEABLE; |
| 1651 | |
| 1652 | /* Set up the initial task struct. */ |
| 1653 | nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client); |
| 1654 | trace_nfs_initiate_commit(data); |
| 1655 | |
| 1656 | dprintk("NFS: initiated commit call\n" ); |
| 1657 | |
| 1658 | if (localio) |
| 1659 | return nfs_local_commit(localio, data, call_ops, how); |
| 1660 | |
| 1661 | task = rpc_run_task(&task_setup_data); |
| 1662 | if (IS_ERR(ptr: task)) |
| 1663 | return PTR_ERR(ptr: task); |
| 1664 | if (how & FLUSH_SYNC) |
| 1665 | rpc_wait_for_completion_task(task); |
| 1666 | rpc_put_task(task); |
| 1667 | return 0; |
| 1668 | } |
| 1669 | EXPORT_SYMBOL_GPL(nfs_initiate_commit); |
| 1670 | |
| 1671 | static loff_t nfs_get_lwb(struct list_head *head) |
| 1672 | { |
| 1673 | loff_t lwb = 0; |
| 1674 | struct nfs_page *req; |
| 1675 | |
| 1676 | list_for_each_entry(req, head, wb_list) |
| 1677 | if (lwb < (req_offset(req) + req->wb_bytes)) |
| 1678 | lwb = req_offset(req) + req->wb_bytes; |
| 1679 | |
| 1680 | return lwb; |
| 1681 | } |
| 1682 | |
| 1683 | /* |
| 1684 | * Set up the argument/result storage required for the RPC call. |
| 1685 | */ |
| 1686 | void nfs_init_commit(struct nfs_commit_data *data, |
| 1687 | struct list_head *head, |
| 1688 | struct pnfs_layout_segment *lseg, |
| 1689 | struct nfs_commit_info *cinfo) |
| 1690 | { |
| 1691 | struct nfs_page *first; |
| 1692 | struct nfs_open_context *ctx; |
| 1693 | struct inode *inode; |
| 1694 | |
| 1695 | /* Set up the RPC argument and reply structs |
| 1696 | * NB: take care not to mess about with data->commit et al. */ |
| 1697 | |
| 1698 | if (head) |
| 1699 | list_splice_init(list: head, head: &data->pages); |
| 1700 | |
| 1701 | first = nfs_list_entry(head: data->pages.next); |
| 1702 | ctx = nfs_req_openctx(req: first); |
| 1703 | inode = d_inode(dentry: ctx->dentry); |
| 1704 | |
| 1705 | data->inode = inode; |
| 1706 | data->cred = ctx->cred; |
| 1707 | data->lseg = lseg; /* reference transferred */ |
| 1708 | /* only set lwb for pnfs commit */ |
| 1709 | if (lseg) |
| 1710 | data->lwb = nfs_get_lwb(head: &data->pages); |
| 1711 | data->mds_ops = &nfs_commit_ops; |
| 1712 | data->completion_ops = cinfo->completion_ops; |
| 1713 | data->dreq = cinfo->dreq; |
| 1714 | |
| 1715 | data->args.fh = NFS_FH(inode: data->inode); |
| 1716 | /* Note: we always request a commit of the entire inode */ |
| 1717 | data->args.offset = 0; |
| 1718 | data->args.count = 0; |
| 1719 | data->context = get_nfs_open_context(ctx); |
| 1720 | data->res.fattr = &data->fattr; |
| 1721 | data->res.verf = &data->verf; |
| 1722 | nfs_fattr_init(fattr: &data->fattr); |
| 1723 | nfs_commit_begin(cinfo: cinfo->mds); |
| 1724 | } |
| 1725 | EXPORT_SYMBOL_GPL(nfs_init_commit); |
| 1726 | |
| 1727 | void nfs_retry_commit(struct list_head *page_list, |
| 1728 | struct pnfs_layout_segment *lseg, |
| 1729 | struct nfs_commit_info *cinfo, |
| 1730 | u32 ds_commit_idx) |
| 1731 | { |
| 1732 | struct nfs_page *req; |
| 1733 | |
| 1734 | while (!list_empty(head: page_list)) { |
| 1735 | req = nfs_list_entry(head: page_list->next); |
| 1736 | nfs_list_remove_request(req); |
| 1737 | nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx); |
| 1738 | nfs_folio_clear_commit(folio: nfs_page_to_folio(req)); |
| 1739 | nfs_unlock_and_release_request(req); |
| 1740 | } |
| 1741 | } |
| 1742 | EXPORT_SYMBOL_GPL(nfs_retry_commit); |
| 1743 | |
| 1744 | static void nfs_commit_resched_write(struct nfs_commit_info *cinfo, |
| 1745 | struct nfs_page *req) |
| 1746 | { |
| 1747 | struct folio *folio = nfs_page_to_folio(req); |
| 1748 | |
| 1749 | filemap_dirty_folio(mapping: folio_mapping(folio), folio); |
| 1750 | } |
| 1751 | |
| 1752 | /* |
| 1753 | * Commit dirty pages |
| 1754 | */ |
| 1755 | static int |
| 1756 | nfs_commit_list(struct inode *inode, struct list_head *head, int how, |
| 1757 | struct nfs_commit_info *cinfo) |
| 1758 | { |
| 1759 | struct nfs_commit_data *data; |
| 1760 | struct nfsd_file *localio; |
| 1761 | unsigned short task_flags = 0; |
| 1762 | |
| 1763 | /* another commit raced with us */ |
| 1764 | if (list_empty(head)) |
| 1765 | return 0; |
| 1766 | |
| 1767 | data = nfs_commitdata_alloc(); |
| 1768 | if (!data) { |
| 1769 | nfs_retry_commit(head, NULL, cinfo, -1); |
| 1770 | return -ENOMEM; |
| 1771 | } |
| 1772 | |
| 1773 | /* Set up the argument struct */ |
| 1774 | nfs_init_commit(data, head, NULL, cinfo); |
| 1775 | if (NFS_SERVER(inode)->nfs_client->cl_minorversion) |
| 1776 | task_flags = RPC_TASK_MOVEABLE; |
| 1777 | |
| 1778 | localio = nfs_local_open_fh(NFS_SERVER(inode)->nfs_client, data->cred, |
| 1779 | data->args.fh, &data->context->nfl, |
| 1780 | data->context->mode); |
| 1781 | return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode), |
| 1782 | data->mds_ops, how, |
| 1783 | RPC_TASK_CRED_NOREF | task_flags, localio); |
| 1784 | } |
| 1785 | |
| 1786 | /* |
| 1787 | * COMMIT call returned |
| 1788 | */ |
| 1789 | static void nfs_commit_done(struct rpc_task *task, void *calldata) |
| 1790 | { |
| 1791 | struct nfs_commit_data *data = calldata; |
| 1792 | |
| 1793 | /* Call the NFS version-specific code */ |
| 1794 | NFS_PROTO(inode: data->inode)->commit_done(task, data); |
| 1795 | trace_nfs_commit_done(task, data); |
| 1796 | } |
| 1797 | |
| 1798 | static void nfs_commit_release_pages(struct nfs_commit_data *data) |
| 1799 | { |
| 1800 | const struct nfs_writeverf *verf = data->res.verf; |
| 1801 | struct nfs_page *req; |
| 1802 | int status = data->task.tk_status; |
| 1803 | struct nfs_commit_info cinfo; |
| 1804 | struct folio *folio; |
| 1805 | |
| 1806 | while (!list_empty(head: &data->pages)) { |
| 1807 | req = nfs_list_entry(head: data->pages.next); |
| 1808 | nfs_list_remove_request(req); |
| 1809 | folio = nfs_page_to_folio(req); |
| 1810 | nfs_folio_clear_commit(folio); |
| 1811 | |
| 1812 | dprintk("NFS: commit (%s/%llu %d@%lld)" , |
| 1813 | nfs_req_openctx(req)->dentry->d_sb->s_id, |
| 1814 | (unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req)->dentry)), |
| 1815 | req->wb_bytes, |
| 1816 | (long long)req_offset(req)); |
| 1817 | if (status < 0) { |
| 1818 | if (folio) { |
| 1819 | trace_nfs_commit_error(inode: data->inode, req, |
| 1820 | error: status); |
| 1821 | nfs_mapping_set_error(folio, error: status); |
| 1822 | nfs_inode_remove_request(req); |
| 1823 | } |
| 1824 | dprintk(", error = %d\n" , status); |
| 1825 | goto next; |
| 1826 | } |
| 1827 | |
| 1828 | /* Okay, COMMIT succeeded, apparently. Check the verifier |
| 1829 | * returned by the server against all stored verfs. */ |
| 1830 | if (nfs_write_match_verf(verf, req)) { |
| 1831 | /* We have a match */ |
| 1832 | if (folio) |
| 1833 | nfs_inode_remove_request(req); |
| 1834 | dprintk(" OK\n" ); |
| 1835 | goto next; |
| 1836 | } |
| 1837 | /* We have a mismatch. Write the page again */ |
| 1838 | dprintk(" mismatch\n" ); |
| 1839 | nfs_mark_request_dirty(req); |
| 1840 | atomic_long_inc(v: &NFS_I(inode: data->inode)->redirtied_pages); |
| 1841 | next: |
| 1842 | nfs_unlock_and_release_request(req); |
| 1843 | /* Latency breaker */ |
| 1844 | cond_resched(); |
| 1845 | } |
| 1846 | |
| 1847 | nfs_init_cinfo(&cinfo, data->inode, data->dreq); |
| 1848 | nfs_commit_end(cinfo: cinfo.mds); |
| 1849 | } |
| 1850 | |
| 1851 | static void nfs_commit_release(void *calldata) |
| 1852 | { |
| 1853 | struct nfs_commit_data *data = calldata; |
| 1854 | |
| 1855 | data->completion_ops->completion(data); |
| 1856 | nfs_commitdata_release(calldata); |
| 1857 | } |
| 1858 | |
| 1859 | static const struct rpc_call_ops nfs_commit_ops = { |
| 1860 | .rpc_call_prepare = nfs_commit_prepare, |
| 1861 | .rpc_call_done = nfs_commit_done, |
| 1862 | .rpc_release = nfs_commit_release, |
| 1863 | }; |
| 1864 | |
| 1865 | static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { |
| 1866 | .completion = nfs_commit_release_pages, |
| 1867 | .resched_write = nfs_commit_resched_write, |
| 1868 | }; |
| 1869 | |
| 1870 | int nfs_generic_commit_list(struct inode *inode, struct list_head *head, |
| 1871 | int how, struct nfs_commit_info *cinfo) |
| 1872 | { |
| 1873 | int status; |
| 1874 | |
| 1875 | status = pnfs_commit_list(inode, mds_pages: head, how, cinfo); |
| 1876 | if (status == PNFS_NOT_ATTEMPTED) |
| 1877 | status = nfs_commit_list(inode, head, how, cinfo); |
| 1878 | return status; |
| 1879 | } |
| 1880 | |
| 1881 | static int __nfs_commit_inode(struct inode *inode, int how, |
| 1882 | struct writeback_control *wbc) |
| 1883 | { |
| 1884 | LIST_HEAD(head); |
| 1885 | struct nfs_commit_info cinfo; |
| 1886 | int may_wait = how & FLUSH_SYNC; |
| 1887 | int ret, nscan; |
| 1888 | |
| 1889 | how &= ~FLUSH_SYNC; |
| 1890 | nfs_init_cinfo_from_inode(cinfo: &cinfo, inode); |
| 1891 | nfs_commit_begin(cinfo: cinfo.mds); |
| 1892 | for (;;) { |
| 1893 | ret = nscan = nfs_scan_commit(inode, dst: &head, cinfo: &cinfo); |
| 1894 | if (ret <= 0) |
| 1895 | break; |
| 1896 | ret = nfs_generic_commit_list(inode, head: &head, how, cinfo: &cinfo); |
| 1897 | if (ret < 0) |
| 1898 | break; |
| 1899 | ret = 0; |
| 1900 | if (wbc && wbc->sync_mode == WB_SYNC_NONE) { |
| 1901 | if (nscan < wbc->nr_to_write) |
| 1902 | wbc->nr_to_write -= nscan; |
| 1903 | else |
| 1904 | wbc->nr_to_write = 0; |
| 1905 | } |
| 1906 | if (nscan < INT_MAX) |
| 1907 | break; |
| 1908 | cond_resched(); |
| 1909 | } |
| 1910 | nfs_commit_end(cinfo: cinfo.mds); |
| 1911 | if (ret || !may_wait) |
| 1912 | return ret; |
| 1913 | return wait_on_commit(cinfo: cinfo.mds); |
| 1914 | } |
| 1915 | |
| 1916 | int nfs_commit_inode(struct inode *inode, int how) |
| 1917 | { |
| 1918 | return __nfs_commit_inode(inode, how, NULL); |
| 1919 | } |
| 1920 | EXPORT_SYMBOL_GPL(nfs_commit_inode); |
| 1921 | |
| 1922 | int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
| 1923 | { |
| 1924 | struct nfs_inode *nfsi = NFS_I(inode); |
| 1925 | int flags = FLUSH_SYNC; |
| 1926 | int ret = 0; |
| 1927 | |
| 1928 | if (wbc->sync_mode == WB_SYNC_NONE) { |
| 1929 | /* no commits means nothing needs to be done */ |
| 1930 | if (!atomic_long_read(v: &nfsi->commit_info.ncommit)) |
| 1931 | goto check_requests_outstanding; |
| 1932 | |
| 1933 | /* Don't commit yet if this is a non-blocking flush and there |
| 1934 | * are a lot of outstanding writes for this mapping. |
| 1935 | */ |
| 1936 | if (mapping_tagged(mapping: inode->i_mapping, PAGECACHE_TAG_WRITEBACK)) |
| 1937 | goto out_mark_dirty; |
| 1938 | |
| 1939 | /* don't wait for the COMMIT response */ |
| 1940 | flags = 0; |
| 1941 | } |
| 1942 | |
| 1943 | ret = __nfs_commit_inode(inode, how: flags, wbc); |
| 1944 | if (!ret) { |
| 1945 | if (flags & FLUSH_SYNC) |
| 1946 | return 0; |
| 1947 | } else if (atomic_long_read(v: &nfsi->commit_info.ncommit)) |
| 1948 | goto out_mark_dirty; |
| 1949 | |
| 1950 | check_requests_outstanding: |
| 1951 | if (!atomic_read(v: &nfsi->commit_info.rpcs_out)) |
| 1952 | return ret; |
| 1953 | out_mark_dirty: |
| 1954 | __mark_inode_dirty(inode, I_DIRTY_DATASYNC); |
| 1955 | return ret; |
| 1956 | } |
| 1957 | EXPORT_SYMBOL_GPL(nfs_write_inode); |
| 1958 | |
| 1959 | /* |
| 1960 | * Wrapper for filemap_write_and_wait_range() |
| 1961 | * |
| 1962 | * Needed for pNFS in order to ensure data becomes visible to the |
| 1963 | * client. |
| 1964 | */ |
| 1965 | int nfs_filemap_write_and_wait_range(struct address_space *mapping, |
| 1966 | loff_t lstart, loff_t lend) |
| 1967 | { |
| 1968 | int ret; |
| 1969 | |
| 1970 | ret = filemap_write_and_wait_range(mapping, lstart, lend); |
| 1971 | if (ret == 0) |
| 1972 | ret = pnfs_sync_inode(inode: mapping->host, datasync: true); |
| 1973 | return ret; |
| 1974 | } |
| 1975 | EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range); |
| 1976 | |
| 1977 | /* |
| 1978 | * flush the inode to disk. |
| 1979 | */ |
| 1980 | int nfs_wb_all(struct inode *inode) |
| 1981 | { |
| 1982 | int ret; |
| 1983 | |
| 1984 | trace_nfs_writeback_inode_enter(inode); |
| 1985 | |
| 1986 | ret = filemap_write_and_wait(mapping: inode->i_mapping); |
| 1987 | if (ret) |
| 1988 | goto out; |
| 1989 | ret = nfs_commit_inode(inode, FLUSH_SYNC); |
| 1990 | if (ret < 0) |
| 1991 | goto out; |
| 1992 | pnfs_sync_inode(inode, datasync: true); |
| 1993 | ret = 0; |
| 1994 | |
| 1995 | out: |
| 1996 | trace_nfs_writeback_inode_exit(inode, error: ret); |
| 1997 | return ret; |
| 1998 | } |
| 1999 | EXPORT_SYMBOL_GPL(nfs_wb_all); |
| 2000 | |
| 2001 | int nfs_wb_folio_cancel(struct inode *inode, struct folio *folio) |
| 2002 | { |
| 2003 | struct nfs_page *req; |
| 2004 | int ret = 0; |
| 2005 | |
| 2006 | folio_wait_writeback(folio); |
| 2007 | |
| 2008 | /* blocking call to cancel all requests and join to a single (head) |
| 2009 | * request */ |
| 2010 | req = nfs_lock_and_join_requests(folio); |
| 2011 | |
| 2012 | if (IS_ERR(ptr: req)) { |
| 2013 | ret = PTR_ERR(ptr: req); |
| 2014 | } else if (req) { |
| 2015 | /* all requests from this folio have been cancelled by |
| 2016 | * nfs_lock_and_join_requests, so just remove the head |
| 2017 | * request from the inode / page_private pointer and |
| 2018 | * release it */ |
| 2019 | nfs_inode_remove_request(req); |
| 2020 | nfs_unlock_and_release_request(req); |
| 2021 | folio_cancel_dirty(folio); |
| 2022 | } |
| 2023 | |
| 2024 | return ret; |
| 2025 | } |
| 2026 | |
| 2027 | /** |
| 2028 | * nfs_wb_folio_reclaim - Write back all requests on one page |
| 2029 | * @inode: pointer to page |
| 2030 | * @folio: pointer to folio |
| 2031 | * |
| 2032 | * Assumes that the folio has been locked by the caller |
| 2033 | */ |
| 2034 | int nfs_wb_folio_reclaim(struct inode *inode, struct folio *folio) |
| 2035 | { |
| 2036 | loff_t range_start = folio_pos(folio); |
| 2037 | size_t len = folio_size(folio); |
| 2038 | struct writeback_control wbc = { |
| 2039 | .sync_mode = WB_SYNC_ALL, |
| 2040 | .nr_to_write = 0, |
| 2041 | .range_start = range_start, |
| 2042 | .range_end = range_start + len - 1, |
| 2043 | .for_sync = 1, |
| 2044 | }; |
| 2045 | int ret; |
| 2046 | |
| 2047 | if (folio_test_writeback(folio)) |
| 2048 | return -EBUSY; |
| 2049 | if (folio_clear_dirty_for_io(folio)) { |
| 2050 | trace_nfs_writeback_folio_reclaim(inode, offset: range_start, count: len); |
| 2051 | ret = nfs_writepage_locked(folio, wbc: &wbc); |
| 2052 | trace_nfs_writeback_folio_reclaim_done(inode, offset: range_start, count: len, |
| 2053 | ret); |
| 2054 | return ret; |
| 2055 | } |
| 2056 | nfs_commit_inode(inode, 0); |
| 2057 | return 0; |
| 2058 | } |
| 2059 | |
| 2060 | /** |
| 2061 | * nfs_wb_folio - Write back all requests on one page |
| 2062 | * @inode: pointer to page |
| 2063 | * @folio: pointer to folio |
| 2064 | * |
| 2065 | * Assumes that the folio has been locked by the caller, and will |
| 2066 | * not unlock it. |
| 2067 | */ |
| 2068 | int nfs_wb_folio(struct inode *inode, struct folio *folio) |
| 2069 | { |
| 2070 | loff_t range_start = folio_pos(folio); |
| 2071 | size_t len = folio_size(folio); |
| 2072 | struct writeback_control wbc = { |
| 2073 | .sync_mode = WB_SYNC_ALL, |
| 2074 | .nr_to_write = 0, |
| 2075 | .range_start = range_start, |
| 2076 | .range_end = range_start + len - 1, |
| 2077 | }; |
| 2078 | int ret; |
| 2079 | |
| 2080 | trace_nfs_writeback_folio(inode, offset: range_start, count: len); |
| 2081 | |
| 2082 | for (;;) { |
| 2083 | folio_wait_writeback(folio); |
| 2084 | if (folio_clear_dirty_for_io(folio)) { |
| 2085 | ret = nfs_writepage_locked(folio, wbc: &wbc); |
| 2086 | if (ret < 0) |
| 2087 | goto out_error; |
| 2088 | continue; |
| 2089 | } |
| 2090 | ret = 0; |
| 2091 | if (!folio_test_private(folio)) |
| 2092 | break; |
| 2093 | ret = nfs_commit_inode(inode, FLUSH_SYNC); |
| 2094 | if (ret < 0) |
| 2095 | goto out_error; |
| 2096 | } |
| 2097 | out_error: |
| 2098 | trace_nfs_writeback_folio_done(inode, offset: range_start, count: len, ret); |
| 2099 | return ret; |
| 2100 | } |
| 2101 | |
| 2102 | #ifdef CONFIG_MIGRATION |
| 2103 | int nfs_migrate_folio(struct address_space *mapping, struct folio *dst, |
| 2104 | struct folio *src, enum migrate_mode mode) |
| 2105 | { |
| 2106 | /* |
| 2107 | * If the private flag is set, the folio is currently associated with |
| 2108 | * an in-progress read or write request. Don't try to migrate it. |
| 2109 | * |
| 2110 | * FIXME: we could do this in principle, but we'll need a way to ensure |
| 2111 | * that we can safely release the inode reference while holding |
| 2112 | * the folio lock. |
| 2113 | */ |
| 2114 | if (folio_test_private(folio: src)) { |
| 2115 | if (mode == MIGRATE_SYNC) |
| 2116 | nfs_wb_folio(inode: src->mapping->host, folio: src); |
| 2117 | if (folio_test_private(folio: src)) |
| 2118 | return -EBUSY; |
| 2119 | } |
| 2120 | |
| 2121 | if (folio_test_private_2(folio: src)) { /* [DEPRECATED] */ |
| 2122 | if (mode == MIGRATE_ASYNC) |
| 2123 | return -EBUSY; |
| 2124 | folio_wait_private_2(folio: src); |
| 2125 | } |
| 2126 | |
| 2127 | return migrate_folio(mapping, dst, src, mode); |
| 2128 | } |
| 2129 | #endif |
| 2130 | |
| 2131 | int __init nfs_init_writepagecache(void) |
| 2132 | { |
| 2133 | nfs_wdata_cachep = kmem_cache_create("nfs_write_data" , |
| 2134 | sizeof(struct nfs_pgio_header), |
| 2135 | 0, SLAB_HWCACHE_ALIGN, |
| 2136 | NULL); |
| 2137 | if (nfs_wdata_cachep == NULL) |
| 2138 | return -ENOMEM; |
| 2139 | |
| 2140 | nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, |
| 2141 | nfs_wdata_cachep); |
| 2142 | if (nfs_wdata_mempool == NULL) |
| 2143 | goto out_destroy_write_cache; |
| 2144 | |
| 2145 | nfs_cdata_cachep = kmem_cache_create("nfs_commit_data" , |
| 2146 | sizeof(struct nfs_commit_data), |
| 2147 | 0, SLAB_HWCACHE_ALIGN, |
| 2148 | NULL); |
| 2149 | if (nfs_cdata_cachep == NULL) |
| 2150 | goto out_destroy_write_mempool; |
| 2151 | |
| 2152 | nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, |
| 2153 | nfs_cdata_cachep); |
| 2154 | if (nfs_commit_mempool == NULL) |
| 2155 | goto out_destroy_commit_cache; |
| 2156 | |
| 2157 | /* |
| 2158 | * NFS congestion size, scale with available memory. |
| 2159 | * |
| 2160 | * 64MB: 8192k |
| 2161 | * 128MB: 11585k |
| 2162 | * 256MB: 16384k |
| 2163 | * 512MB: 23170k |
| 2164 | * 1GB: 32768k |
| 2165 | * 2GB: 46340k |
| 2166 | * 4GB: 65536k |
| 2167 | * 8GB: 92681k |
| 2168 | * 16GB: 131072k |
| 2169 | * |
| 2170 | * This allows larger machines to have larger/more transfers. |
| 2171 | * Limit the default to 256M |
| 2172 | */ |
| 2173 | nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10); |
| 2174 | if (nfs_congestion_kb > 256*1024) |
| 2175 | nfs_congestion_kb = 256*1024; |
| 2176 | |
| 2177 | return 0; |
| 2178 | |
| 2179 | out_destroy_commit_cache: |
| 2180 | kmem_cache_destroy(s: nfs_cdata_cachep); |
| 2181 | out_destroy_write_mempool: |
| 2182 | mempool_destroy(pool: nfs_wdata_mempool); |
| 2183 | out_destroy_write_cache: |
| 2184 | kmem_cache_destroy(s: nfs_wdata_cachep); |
| 2185 | return -ENOMEM; |
| 2186 | } |
| 2187 | |
| 2188 | void nfs_destroy_writepagecache(void) |
| 2189 | { |
| 2190 | mempool_destroy(pool: nfs_commit_mempool); |
| 2191 | kmem_cache_destroy(s: nfs_cdata_cachep); |
| 2192 | mempool_destroy(pool: nfs_wdata_mempool); |
| 2193 | kmem_cache_destroy(s: nfs_wdata_cachep); |
| 2194 | } |
| 2195 | |
| 2196 | static const struct nfs_rw_ops nfs_rw_write_ops = { |
| 2197 | .rw_alloc_header = nfs_writehdr_alloc, |
| 2198 | .rw_free_header = nfs_writehdr_free, |
| 2199 | .rw_done = nfs_writeback_done, |
| 2200 | .rw_result = nfs_writeback_result, |
| 2201 | .rw_initiate = nfs_initiate_write, |
| 2202 | }; |
| 2203 | |