1*457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/nfs/direct.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2003 by Chuck Lever <cel@netapp.com> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * High-performance uncached I/O for the Linux NFS client 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * There are important applications whose performance or correctness 101da177e4SLinus Torvalds * depends on uncached access to file data. Database clusters 111da177e4SLinus Torvalds * (multiple copies of the same instance running on separate hosts) 121da177e4SLinus Torvalds * implement their own cache coherency protocol that subsumes file 131da177e4SLinus Torvalds * system cache protocols. Applications that process datasets 141da177e4SLinus Torvalds * considerably larger than the client's memory do not always benefit 151da177e4SLinus Torvalds * from a local cache. A streaming video server, for instance, has no 161da177e4SLinus Torvalds * need to cache the contents of a file. 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * When an application requests uncached I/O, all read and write requests 191da177e4SLinus Torvalds * are made directly to the server; data stored or fetched via these 201da177e4SLinus Torvalds * requests is not cached in the Linux page cache. The client does not 211da177e4SLinus Torvalds * correct unaligned requests from applications. All requested bytes are 221da177e4SLinus Torvalds * held on permanent storage before a direct write system call returns to 231da177e4SLinus Torvalds * an application. 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * Solaris implements an uncached I/O facility called directio() that 261da177e4SLinus Torvalds * is used for backups and sequential I/O to very large files. Solaris 271da177e4SLinus Torvalds * also supports uncaching whole NFS partitions with "-o forcedirectio," 281da177e4SLinus Torvalds * an undocumented mount option. 291da177e4SLinus Torvalds * 301da177e4SLinus Torvalds * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with 311da177e4SLinus Torvalds * help from Andrew Morton. 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * 18 Dec 2001 Initial implementation for 2.4 --cel 341da177e4SLinus Torvalds * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy 351da177e4SLinus Torvalds * 08 Jun 2003 Port to 2.5 APIs --cel 361da177e4SLinus Torvalds * 31 Mar 2004 Handle direct I/O without VFS support --cel 371da177e4SLinus Torvalds * 15 Sep 2004 Parallel async reads --cel 3888467055SChuck Lever * 04 May 2005 support O_DIRECT with aio --cel 391da177e4SLinus Torvalds * 401da177e4SLinus Torvalds */ 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include <linux/errno.h> 431da177e4SLinus Torvalds #include <linux/sched.h> 441da177e4SLinus Torvalds #include <linux/kernel.h> 451da177e4SLinus Torvalds #include <linux/file.h> 461da177e4SLinus Torvalds #include <linux/pagemap.h> 471da177e4SLinus Torvalds #include <linux/kref.h> 485a0e3ad6STejun Heo #include <linux/slab.h> 497ec10f26SKonstantin Khlebnikov #include <linux/task_io_accounting_ops.h> 506296556fSPeng Tao #include <linux/module.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/nfs_fs.h> 531da177e4SLinus Torvalds #include <linux/nfs_page.h> 541da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 551da177e4SLinus Torvalds 567c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 5760063497SArun Sharma #include <linux/atomic.h> 581da177e4SLinus Torvalds 598d5658c9STrond Myklebust #include "internal.h" 6091d5b470SChuck Lever #include "iostat.h" 611763da12SFred Isaman #include "pnfs.h" 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_VFS 641da177e4SLinus Torvalds 65e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep; 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* 681da177e4SLinus Torvalds * This represents a set of asynchronous requests that we're waiting on 691da177e4SLinus Torvalds */ 700a00b77bSWeston Andros Adamson struct nfs_direct_mirror { 710a00b77bSWeston Andros Adamson ssize_t count; 720a00b77bSWeston Andros Adamson }; 730a00b77bSWeston Andros Adamson 741da177e4SLinus Torvalds struct nfs_direct_req { 751da177e4SLinus Torvalds struct kref kref; /* release manager */ 7615ce4a0cSChuck Lever 7715ce4a0cSChuck Lever /* I/O parameters */ 78a8881f5aSTrond Myklebust struct nfs_open_context *ctx; /* file open context info */ 79f11ac8dbSTrond Myklebust struct nfs_lock_context *l_ctx; /* Lock context info */ 8099514f8fSChuck Lever struct kiocb * iocb; /* controlling i/o request */ 8188467055SChuck Lever struct inode * inode; /* target file of i/o */ 8215ce4a0cSChuck Lever 8315ce4a0cSChuck Lever /* completion state */ 84607f31e8STrond Myklebust atomic_t io_count; /* i/os we're waiting for */ 8515ce4a0cSChuck Lever spinlock_t lock; /* protect completion state */ 860a00b77bSWeston Andros Adamson 870a00b77bSWeston Andros Adamson struct nfs_direct_mirror mirrors[NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX]; 880a00b77bSWeston Andros Adamson int mirror_count; 890a00b77bSWeston Andros Adamson 90d9ee6553STrond Myklebust loff_t io_start; /* Start offset for I/O */ 9115ce4a0cSChuck Lever ssize_t count, /* bytes actually processed */ 92ed3743a6SWeston Andros Adamson max_count, /* max expected count */ 9335754bc0SPeng Tao bytes_left, /* bytes left to be sent */ 941da177e4SLinus Torvalds error; /* any reported error */ 95d72b7a6bSTrond Myklebust struct completion completion; /* wait for i/o completion */ 96fad61490STrond Myklebust 97fad61490STrond Myklebust /* commit state */ 981763da12SFred Isaman struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */ 991763da12SFred Isaman struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */ 1001763da12SFred Isaman struct work_struct work; 101fad61490STrond Myklebust int flags; 102ad3cba22SDave Kleikamp /* for write */ 103fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 104fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 105ad3cba22SDave Kleikamp /* for read */ 106ad3cba22SDave Kleikamp #define NFS_ODIRECT_SHOULD_DIRTY (3) /* dirty user-space page after read */ 107fad61490STrond Myklebust struct nfs_writeverf verf; /* unstable write verifier */ 1081da177e4SLinus Torvalds }; 1091da177e4SLinus Torvalds 1101763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops; 1111763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops; 1124d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq); 1131763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work); 114607f31e8STrond Myklebust 115607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq) 116607f31e8STrond Myklebust { 117607f31e8STrond Myklebust atomic_inc(&dreq->io_count); 118607f31e8STrond Myklebust } 119607f31e8STrond Myklebust 120607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq) 121607f31e8STrond Myklebust { 122607f31e8STrond Myklebust return atomic_dec_and_test(&dreq->io_count); 123607f31e8STrond Myklebust } 124607f31e8STrond Myklebust 1250a00b77bSWeston Andros Adamson static void 1260a00b77bSWeston Andros Adamson nfs_direct_good_bytes(struct nfs_direct_req *dreq, struct nfs_pgio_header *hdr) 1270a00b77bSWeston Andros Adamson { 1280a00b77bSWeston Andros Adamson int i; 1290a00b77bSWeston Andros Adamson ssize_t count; 1300a00b77bSWeston Andros Adamson 131ed3743a6SWeston Andros Adamson WARN_ON_ONCE(dreq->count >= dreq->max_count); 132ed3743a6SWeston Andros Adamson 1331ccbad9fSPeng Tao if (dreq->mirror_count == 1) { 1341ccbad9fSPeng Tao dreq->mirrors[hdr->pgio_mirror_idx].count += hdr->good_bytes; 1351ccbad9fSPeng Tao dreq->count += hdr->good_bytes; 1361ccbad9fSPeng Tao } else { 1371ccbad9fSPeng Tao /* mirrored writes */ 1385fadeb47SPeng Tao count = dreq->mirrors[hdr->pgio_mirror_idx].count; 1395fadeb47SPeng Tao if (count + dreq->io_start < hdr->io_start + hdr->good_bytes) { 1405fadeb47SPeng Tao count = hdr->io_start + hdr->good_bytes - dreq->io_start; 1415fadeb47SPeng Tao dreq->mirrors[hdr->pgio_mirror_idx].count = count; 1425fadeb47SPeng Tao } 1430a00b77bSWeston Andros Adamson /* update the dreq->count by finding the minimum agreed count from all 1440a00b77bSWeston Andros Adamson * mirrors */ 1450a00b77bSWeston Andros Adamson count = dreq->mirrors[0].count; 1460a00b77bSWeston Andros Adamson 1470a00b77bSWeston Andros Adamson for (i = 1; i < dreq->mirror_count; i++) 1480a00b77bSWeston Andros Adamson count = min(count, dreq->mirrors[i].count); 1490a00b77bSWeston Andros Adamson 1500a00b77bSWeston Andros Adamson dreq->count = count; 1510a00b77bSWeston Andros Adamson } 1521ccbad9fSPeng Tao } 1530a00b77bSWeston Andros Adamson 1545002c586SWeston Andros Adamson /* 1555002c586SWeston Andros Adamson * nfs_direct_select_verf - select the right verifier 1565002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 1575002c586SWeston Andros Adamson * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs 1586cccbb6fSWeston Andros Adamson * @commit_idx - commit bucket index for the DS 1595002c586SWeston Andros Adamson * 1605002c586SWeston Andros Adamson * returns the correct verifier to use given the role of the server 1615002c586SWeston Andros Adamson */ 1625002c586SWeston Andros Adamson static struct nfs_writeverf * 1635002c586SWeston Andros Adamson nfs_direct_select_verf(struct nfs_direct_req *dreq, 1645002c586SWeston Andros Adamson struct nfs_client *ds_clp, 1656cccbb6fSWeston Andros Adamson int commit_idx) 1665002c586SWeston Andros Adamson { 1675002c586SWeston Andros Adamson struct nfs_writeverf *verfp = &dreq->verf; 1685002c586SWeston Andros Adamson 1695002c586SWeston Andros Adamson #ifdef CONFIG_NFS_V4_1 170834e465bSKinglong Mee /* 171834e465bSKinglong Mee * pNFS is in use, use the DS verf except commit_through_mds is set 172834e465bSKinglong Mee * for layout segment where nbuckets is zero. 173834e465bSKinglong Mee */ 174834e465bSKinglong Mee if (ds_clp && dreq->ds_cinfo.nbuckets > 0) { 1756cccbb6fSWeston Andros Adamson if (commit_idx >= 0 && commit_idx < dreq->ds_cinfo.nbuckets) 1766cccbb6fSWeston Andros Adamson verfp = &dreq->ds_cinfo.buckets[commit_idx].direct_verf; 1775002c586SWeston Andros Adamson else 1785002c586SWeston Andros Adamson WARN_ON_ONCE(1); 1795002c586SWeston Andros Adamson } 1805002c586SWeston Andros Adamson #endif 1815002c586SWeston Andros Adamson return verfp; 1825002c586SWeston Andros Adamson } 1835002c586SWeston Andros Adamson 1845002c586SWeston Andros Adamson 1855002c586SWeston Andros Adamson /* 1865002c586SWeston Andros Adamson * nfs_direct_set_hdr_verf - set the write/commit verifier 1875002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 1885002c586SWeston Andros Adamson * @hdr - pageio header to validate against previously seen verfs 1895002c586SWeston Andros Adamson * 1905002c586SWeston Andros Adamson * Set the server's (MDS or DS) "seen" verifier 1915002c586SWeston Andros Adamson */ 1925002c586SWeston Andros Adamson static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq, 1935002c586SWeston Andros Adamson struct nfs_pgio_header *hdr) 1945002c586SWeston Andros Adamson { 1955002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 1965002c586SWeston Andros Adamson 1976cccbb6fSWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx); 1985002c586SWeston Andros Adamson WARN_ON_ONCE(verfp->committed >= 0); 1995002c586SWeston Andros Adamson memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf)); 2005002c586SWeston Andros Adamson WARN_ON_ONCE(verfp->committed < 0); 2015002c586SWeston Andros Adamson } 2025002c586SWeston Andros Adamson 2038fc3c386STrond Myklebust static int nfs_direct_cmp_verf(const struct nfs_writeverf *v1, 2048fc3c386STrond Myklebust const struct nfs_writeverf *v2) 2058fc3c386STrond Myklebust { 2068fc3c386STrond Myklebust return nfs_write_verifier_cmp(&v1->verifier, &v2->verifier); 2078fc3c386STrond Myklebust } 2088fc3c386STrond Myklebust 2095002c586SWeston Andros Adamson /* 2105002c586SWeston Andros Adamson * nfs_direct_cmp_hdr_verf - compare verifier for pgio header 2115002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 2125002c586SWeston Andros Adamson * @hdr - pageio header to validate against previously seen verf 2135002c586SWeston Andros Adamson * 2145002c586SWeston Andros Adamson * set the server's "seen" verf if not initialized. 2155002c586SWeston Andros Adamson * returns result of comparison between @hdr->verf and the "seen" 2165002c586SWeston Andros Adamson * verf of the server used by @hdr (DS or MDS) 2175002c586SWeston Andros Adamson */ 2185002c586SWeston Andros Adamson static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq, 2195002c586SWeston Andros Adamson struct nfs_pgio_header *hdr) 2205002c586SWeston Andros Adamson { 2215002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 2225002c586SWeston Andros Adamson 2236cccbb6fSWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx); 2245002c586SWeston Andros Adamson if (verfp->committed < 0) { 2255002c586SWeston Andros Adamson nfs_direct_set_hdr_verf(dreq, hdr); 2265002c586SWeston Andros Adamson return 0; 2275002c586SWeston Andros Adamson } 2288fc3c386STrond Myklebust return nfs_direct_cmp_verf(verfp, &hdr->verf); 2295002c586SWeston Andros Adamson } 2305002c586SWeston Andros Adamson 2315002c586SWeston Andros Adamson /* 2325002c586SWeston Andros Adamson * nfs_direct_cmp_commit_data_verf - compare verifier for commit data 2335002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 2345002c586SWeston Andros Adamson * @data - commit data to validate against previously seen verf 2355002c586SWeston Andros Adamson * 2365002c586SWeston Andros Adamson * returns result of comparison between @data->verf and the verf of 2375002c586SWeston Andros Adamson * the server used by @data (DS or MDS) 2385002c586SWeston Andros Adamson */ 2395002c586SWeston Andros Adamson static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq, 2405002c586SWeston Andros Adamson struct nfs_commit_data *data) 2415002c586SWeston Andros Adamson { 2425002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 2435002c586SWeston Andros Adamson 2445002c586SWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, data->ds_clp, 2455002c586SWeston Andros Adamson data->ds_commit_index); 24680c76fe3SWeston Andros Adamson 24780c76fe3SWeston Andros Adamson /* verifier not set so always fail */ 24880c76fe3SWeston Andros Adamson if (verfp->committed < 0) 24980c76fe3SWeston Andros Adamson return 1; 25080c76fe3SWeston Andros Adamson 2518fc3c386STrond Myklebust return nfs_direct_cmp_verf(verfp, &data->verf); 2525002c586SWeston Andros Adamson } 2535002c586SWeston Andros Adamson 2541da177e4SLinus Torvalds /** 255b8a32e2bSChuck Lever * nfs_direct_IO - NFS address space operation for direct I/O 256b8a32e2bSChuck Lever * @iocb: target I/O control block 25790090ae6SAl Viro * @iter: I/O buffer 258b8a32e2bSChuck Lever * 259b8a32e2bSChuck Lever * The presence of this routine in the address space ops vector means 260a564b8f0SMel Gorman * the NFS client supports direct I/O. However, for most direct IO, we 261a564b8f0SMel Gorman * shunt off direct read and write requests before the VFS gets them, 262a564b8f0SMel Gorman * so this method is only ever called for swap. 2631da177e4SLinus Torvalds */ 264c8b8e32dSChristoph Hellwig ssize_t nfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 265b8a32e2bSChuck Lever { 266ee8a1a8bSPeng Tao struct inode *inode = iocb->ki_filp->f_mapping->host; 267ee8a1a8bSPeng Tao 268ee8a1a8bSPeng Tao /* we only support swap file calling nfs_direct_IO */ 269ee8a1a8bSPeng Tao if (!IS_SWAPFILE(inode)) 270ee8a1a8bSPeng Tao return 0; 271ee8a1a8bSPeng Tao 27266ee59afSChristoph Hellwig VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE); 273a564b8f0SMel Gorman 2746f673763SOmar Sandoval if (iov_iter_rw(iter) == READ) 275c8b8e32dSChristoph Hellwig return nfs_file_direct_read(iocb, iter); 27665a4a1caSAl Viro return nfs_file_direct_write(iocb, iter); 277b8a32e2bSChuck Lever } 278b8a32e2bSChuck Lever 279749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages) 2809c93ab7dSChuck Lever { 281749e146eSChuck Lever unsigned int i; 282607f31e8STrond Myklebust for (i = 0; i < npages; i++) 28309cbfeafSKirill A. Shutemov put_page(pages[i]); 2846b45d858STrond Myklebust } 2856b45d858STrond Myklebust 2861763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo, 2871763da12SFred Isaman struct nfs_direct_req *dreq) 2881763da12SFred Isaman { 289fe238e60SDave Wysochanski cinfo->inode = dreq->inode; 2901763da12SFred Isaman cinfo->mds = &dreq->mds_cinfo; 2911763da12SFred Isaman cinfo->ds = &dreq->ds_cinfo; 2921763da12SFred Isaman cinfo->dreq = dreq; 2931763da12SFred Isaman cinfo->completion_ops = &nfs_direct_commit_completion_ops; 2941763da12SFred Isaman } 2951763da12SFred Isaman 2960a00b77bSWeston Andros Adamson static inline void nfs_direct_setup_mirroring(struct nfs_direct_req *dreq, 2970a00b77bSWeston Andros Adamson struct nfs_pageio_descriptor *pgio, 2980a00b77bSWeston Andros Adamson struct nfs_page *req) 2990a00b77bSWeston Andros Adamson { 3000a00b77bSWeston Andros Adamson int mirror_count = 1; 3010a00b77bSWeston Andros Adamson 3020a00b77bSWeston Andros Adamson if (pgio->pg_ops->pg_get_mirror_count) 3030a00b77bSWeston Andros Adamson mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req); 3040a00b77bSWeston Andros Adamson 3050a00b77bSWeston Andros Adamson dreq->mirror_count = mirror_count; 3060a00b77bSWeston Andros Adamson } 3070a00b77bSWeston Andros Adamson 30893619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds struct nfs_direct_req *dreq; 3111da177e4SLinus Torvalds 312292f3eeeSTrond Myklebust dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL); 3131da177e4SLinus Torvalds if (!dreq) 3141da177e4SLinus Torvalds return NULL; 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds kref_init(&dreq->kref); 317607f31e8STrond Myklebust kref_get(&dreq->kref); 318d72b7a6bSTrond Myklebust init_completion(&dreq->completion); 3191763da12SFred Isaman INIT_LIST_HEAD(&dreq->mds_cinfo.list); 3205002c586SWeston Andros Adamson dreq->verf.committed = NFS_INVALID_STABLE_HOW; /* not set yet */ 3211763da12SFred Isaman INIT_WORK(&dreq->work, nfs_direct_write_schedule_work); 3220a00b77bSWeston Andros Adamson dreq->mirror_count = 1; 32315ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 32493619e59SChuck Lever 32593619e59SChuck Lever return dreq; 32693619e59SChuck Lever } 32793619e59SChuck Lever 328b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref) 3291da177e4SLinus Torvalds { 3301da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 331a8881f5aSTrond Myklebust 3328c393f9aSPeng Tao nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo); 333f11ac8dbSTrond Myklebust if (dreq->l_ctx != NULL) 334f11ac8dbSTrond Myklebust nfs_put_lock_context(dreq->l_ctx); 335a8881f5aSTrond Myklebust if (dreq->ctx != NULL) 336a8881f5aSTrond Myklebust put_nfs_open_context(dreq->ctx); 3371da177e4SLinus Torvalds kmem_cache_free(nfs_direct_cachep, dreq); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 340b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq) 341b4946ffbSTrond Myklebust { 342b4946ffbSTrond Myklebust kref_put(&dreq->kref, nfs_direct_req_free); 343b4946ffbSTrond Myklebust } 344b4946ffbSTrond Myklebust 3456296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq) 3466296556fSPeng Tao { 3476296556fSPeng Tao return dreq->bytes_left; 3486296556fSPeng Tao } 3496296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left); 3506296556fSPeng Tao 351d4cc948bSChuck Lever /* 352bc0fb201SChuck Lever * Collects and returns the final error value/byte-count. 353bc0fb201SChuck Lever */ 354bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) 355bc0fb201SChuck Lever { 35615ce4a0cSChuck Lever ssize_t result = -EIOCBQUEUED; 357bc0fb201SChuck Lever 358bc0fb201SChuck Lever /* Async requests don't wait here */ 359bc0fb201SChuck Lever if (dreq->iocb) 360bc0fb201SChuck Lever goto out; 361bc0fb201SChuck Lever 362150030b7SMatthew Wilcox result = wait_for_completion_killable(&dreq->completion); 363bc0fb201SChuck Lever 364d2a7de0bSTrond Myklebust if (!result) { 365d2a7de0bSTrond Myklebust result = dreq->count; 366d2a7de0bSTrond Myklebust WARN_ON_ONCE(dreq->count < 0); 367d2a7de0bSTrond Myklebust } 368bc0fb201SChuck Lever if (!result) 36915ce4a0cSChuck Lever result = dreq->error; 370bc0fb201SChuck Lever 371bc0fb201SChuck Lever out: 372bc0fb201SChuck Lever return (ssize_t) result; 373bc0fb201SChuck Lever } 374bc0fb201SChuck Lever 375bc0fb201SChuck Lever /* 376607f31e8STrond Myklebust * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust 377607f31e8STrond Myklebust * the iocb is still valid here if this is a synchronous request. 37863ab46abSChuck Lever */ 379f7b5c340STrond Myklebust static void nfs_direct_complete(struct nfs_direct_req *dreq) 38063ab46abSChuck Lever { 3819811cd57SChristoph Hellwig struct inode *inode = dreq->inode; 3829811cd57SChristoph Hellwig 383fe0f07d0SJens Axboe inode_dio_end(inode); 3842a009ec9SChristoph Hellwig 3852a009ec9SChristoph Hellwig if (dreq->iocb) { 3862a009ec9SChristoph Hellwig long res = (long) dreq->error; 387d2a7de0bSTrond Myklebust if (dreq->count != 0) { 3882a009ec9SChristoph Hellwig res = (long) dreq->count; 389d2a7de0bSTrond Myklebust WARN_ON_ONCE(dreq->count < 0); 390d2a7de0bSTrond Myklebust } 39104b2fa9fSChristoph Hellwig dreq->iocb->ki_complete(dreq->iocb, res, 0); 392d72b7a6bSTrond Myklebust } 3932a009ec9SChristoph Hellwig 394024de8f1SDaniel Wagner complete(&dreq->completion); 39563ab46abSChuck Lever 396b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 39763ab46abSChuck Lever } 39863ab46abSChuck Lever 399584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr) 400fdd1e74cSTrond Myklebust { 401584aa810SFred Isaman unsigned long bytes = 0; 402584aa810SFred Isaman struct nfs_direct_req *dreq = hdr->dreq; 403fdd1e74cSTrond Myklebust 404584aa810SFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 405584aa810SFred Isaman goto out_put; 4061da177e4SLinus Torvalds 40715ce4a0cSChuck Lever spin_lock(&dreq->lock); 408584aa810SFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0)) 409584aa810SFred Isaman dreq->error = hdr->error; 410584aa810SFred Isaman else 4110a00b77bSWeston Andros Adamson nfs_direct_good_bytes(dreq, hdr); 4120a00b77bSWeston Andros Adamson 41315ce4a0cSChuck Lever spin_unlock(&dreq->lock); 4141da177e4SLinus Torvalds 415584aa810SFred Isaman while (!list_empty(&hdr->pages)) { 416584aa810SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 417584aa810SFred Isaman struct page *page = req->wb_page; 418584aa810SFred Isaman 419ad3cba22SDave Kleikamp if (!PageCompound(page) && bytes < hdr->good_bytes && 420ad3cba22SDave Kleikamp (dreq->flags == NFS_ODIRECT_SHOULD_DIRTY)) 4214bd8b010STrond Myklebust set_page_dirty(page); 422584aa810SFred Isaman bytes += req->wb_bytes; 423584aa810SFred Isaman nfs_list_remove_request(req); 424beeb5338SAnna Schumaker nfs_release_request(req); 425584aa810SFred Isaman } 426584aa810SFred Isaman out_put: 427607f31e8STrond Myklebust if (put_dreq(dreq)) 428f7b5c340STrond Myklebust nfs_direct_complete(dreq); 429584aa810SFred Isaman hdr->release(hdr); 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 432df3accb8STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head, int error) 433cd841605SFred Isaman { 434584aa810SFred Isaman struct nfs_page *req; 435cd841605SFred Isaman 436584aa810SFred Isaman while (!list_empty(head)) { 437584aa810SFred Isaman req = nfs_list_entry(head->next); 438584aa810SFred Isaman nfs_list_remove_request(req); 439584aa810SFred Isaman nfs_release_request(req); 440cd841605SFred Isaman } 441584aa810SFred Isaman } 442584aa810SFred Isaman 443584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr) 444584aa810SFred Isaman { 445584aa810SFred Isaman get_dreq(hdr->dreq); 446584aa810SFred Isaman } 447584aa810SFred Isaman 448584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = { 4493e9e0ca3STrond Myklebust .error_cleanup = nfs_read_sync_pgio_error, 450584aa810SFred Isaman .init_hdr = nfs_direct_pgio_init, 451584aa810SFred Isaman .completion = nfs_direct_read_completion, 452584aa810SFred Isaman }; 453cd841605SFred Isaman 454d4cc948bSChuck Lever /* 455607f31e8STrond Myklebust * For each rsize'd chunk of the user's buffer, dispatch an NFS READ 456607f31e8STrond Myklebust * operation. If nfs_readdata_alloc() or get_user_pages() fails, 457607f31e8STrond Myklebust * bail and stop sending more reads. Read length accounting is 458607f31e8STrond Myklebust * handled automatically by nfs_direct_read_result(). Otherwise, if 459607f31e8STrond Myklebust * no requests have been sent, just return an error. 4601da177e4SLinus Torvalds */ 46191f79c43SAl Viro 46291f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq, 46391f79c43SAl Viro struct iov_iter *iter, 46491f79c43SAl Viro loff_t pos) 4651da177e4SLinus Torvalds { 46691f79c43SAl Viro struct nfs_pageio_descriptor desc; 46791f79c43SAl Viro struct inode *inode = dreq->inode; 46891f79c43SAl Viro ssize_t result = -EINVAL; 46991f79c43SAl Viro size_t requested_bytes = 0; 47091f79c43SAl Viro size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE); 47182b145c5SChuck Lever 47216b90578SLinus Torvalds nfs_pageio_init_read(&desc, dreq->inode, false, 47391f79c43SAl Viro &nfs_direct_read_completion_ops); 47491f79c43SAl Viro get_dreq(dreq); 47591f79c43SAl Viro desc.pg_dreq = dreq; 476fe0f07d0SJens Axboe inode_dio_begin(inode); 47791f79c43SAl Viro 47891f79c43SAl Viro while (iov_iter_count(iter)) { 47991f79c43SAl Viro struct page **pagevec; 4805dd602f2SChuck Lever size_t bytes; 48191f79c43SAl Viro size_t pgbase; 48291f79c43SAl Viro unsigned npages, i; 4831da177e4SLinus Torvalds 48491f79c43SAl Viro result = iov_iter_get_pages_alloc(iter, &pagevec, 48591f79c43SAl Viro rsize, &pgbase); 486584aa810SFred Isaman if (result < 0) 487749e146eSChuck Lever break; 488a564b8f0SMel Gorman 48991f79c43SAl Viro bytes = result; 49091f79c43SAl Viro iov_iter_advance(iter, bytes); 49191f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 492584aa810SFred Isaman for (i = 0; i < npages; i++) { 493584aa810SFred Isaman struct nfs_page *req; 494bf5fc402STrond Myklebust unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 495584aa810SFred Isaman /* XXX do we need to do the eof zeroing found in async_filler? */ 49628b1d3f5STrond Myklebust req = nfs_create_request(dreq->ctx, pagevec[i], 497584aa810SFred Isaman pgbase, req_len); 498584aa810SFred Isaman if (IS_ERR(req)) { 499584aa810SFred Isaman result = PTR_ERR(req); 500dbae4c73STrond Myklebust break; 501584aa810SFred Isaman } 502584aa810SFred Isaman req->wb_index = pos >> PAGE_SHIFT; 503584aa810SFred Isaman req->wb_offset = pos & ~PAGE_MASK; 50491f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 50591f79c43SAl Viro result = desc.pg_error; 506584aa810SFred Isaman nfs_release_request(req); 507584aa810SFred Isaman break; 508584aa810SFred Isaman } 509584aa810SFred Isaman pgbase = 0; 510584aa810SFred Isaman bytes -= req_len; 51191f79c43SAl Viro requested_bytes += req_len; 512584aa810SFred Isaman pos += req_len; 51335754bc0SPeng Tao dreq->bytes_left -= req_len; 514584aa810SFred Isaman } 5156d74743bSTrond Myklebust nfs_direct_release_pages(pagevec, npages); 51691f79c43SAl Viro kvfree(pagevec); 51719f73787SChuck Lever if (result < 0) 51819f73787SChuck Lever break; 51919f73787SChuck Lever } 52019f73787SChuck Lever 521584aa810SFred Isaman nfs_pageio_complete(&desc); 522584aa810SFred Isaman 523839f7ad6SChuck Lever /* 524839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 525839f7ad6SChuck Lever * generic layer handle the completion. 526839f7ad6SChuck Lever */ 527839f7ad6SChuck Lever if (requested_bytes == 0) { 528fe0f07d0SJens Axboe inode_dio_end(inode); 529839f7ad6SChuck Lever nfs_direct_req_release(dreq); 530839f7ad6SChuck Lever return result < 0 ? result : -EIO; 531839f7ad6SChuck Lever } 532839f7ad6SChuck Lever 53319f73787SChuck Lever if (put_dreq(dreq)) 534f7b5c340STrond Myklebust nfs_direct_complete(dreq); 53585128b2bSAl Viro return requested_bytes; 53619f73787SChuck Lever } 53719f73787SChuck Lever 53814a3ec79SChristoph Hellwig /** 53914a3ec79SChristoph Hellwig * nfs_file_direct_read - file direct read operation for NFS files 54014a3ec79SChristoph Hellwig * @iocb: target I/O control block 541619d30b4SAl Viro * @iter: vector of user buffers into which to read data 54214a3ec79SChristoph Hellwig * 54314a3ec79SChristoph Hellwig * We use this function for direct reads instead of calling 54414a3ec79SChristoph Hellwig * generic_file_aio_read() in order to avoid gfar's check to see if 54514a3ec79SChristoph Hellwig * the request starts before the end of the file. For that check 54614a3ec79SChristoph Hellwig * to work, we must generate a GETATTR before each direct read, and 54714a3ec79SChristoph Hellwig * even then there is a window between the GETATTR and the subsequent 54814a3ec79SChristoph Hellwig * READ where the file size could change. Our preference is simply 54914a3ec79SChristoph Hellwig * to do all reads the application wants, and the server will take 55014a3ec79SChristoph Hellwig * care of managing the end of file boundary. 55114a3ec79SChristoph Hellwig * 55214a3ec79SChristoph Hellwig * This function also eliminates unnecessarily updating the file's 55314a3ec79SChristoph Hellwig * atime locally, as the NFS server sets the file's atime, and this 55414a3ec79SChristoph Hellwig * client must read the updated atime from the server back into its 55514a3ec79SChristoph Hellwig * cache. 55614a3ec79SChristoph Hellwig */ 557c8b8e32dSChristoph Hellwig ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter) 5581da177e4SLinus Torvalds { 55914a3ec79SChristoph Hellwig struct file *file = iocb->ki_filp; 56014a3ec79SChristoph Hellwig struct address_space *mapping = file->f_mapping; 56114a3ec79SChristoph Hellwig struct inode *inode = mapping->host; 5621da177e4SLinus Torvalds struct nfs_direct_req *dreq; 563b3c54de6STrond Myklebust struct nfs_lock_context *l_ctx; 56485128b2bSAl Viro ssize_t result = -EINVAL, requested; 565a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 56614a3ec79SChristoph Hellwig nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); 56714a3ec79SChristoph Hellwig 56814a3ec79SChristoph Hellwig dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n", 569c8b8e32dSChristoph Hellwig file, count, (long long) iocb->ki_pos); 57014a3ec79SChristoph Hellwig 57114a3ec79SChristoph Hellwig result = 0; 57214a3ec79SChristoph Hellwig if (!count) 57314a3ec79SChristoph Hellwig goto out; 57414a3ec79SChristoph Hellwig 57514a3ec79SChristoph Hellwig task_io_account_read(count); 57614a3ec79SChristoph Hellwig 57714a3ec79SChristoph Hellwig result = -ENOMEM; 578607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 579f11ac8dbSTrond Myklebust if (dreq == NULL) 580a5864c99STrond Myklebust goto out; 5811da177e4SLinus Torvalds 58291d5b470SChuck Lever dreq->inode = inode; 583ed3743a6SWeston Andros Adamson dreq->bytes_left = dreq->max_count = count; 584c8b8e32dSChristoph Hellwig dreq->io_start = iocb->ki_pos; 585cd3758e3STrond Myklebust dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 586b3c54de6STrond Myklebust l_ctx = nfs_get_lock_context(dreq->ctx); 587b3c54de6STrond Myklebust if (IS_ERR(l_ctx)) { 588b3c54de6STrond Myklebust result = PTR_ERR(l_ctx); 589f11ac8dbSTrond Myklebust goto out_release; 590b3c54de6STrond Myklebust } 591b3c54de6STrond Myklebust dreq->l_ctx = l_ctx; 592487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 593487b8372SChuck Lever dreq->iocb = iocb; 5941da177e4SLinus Torvalds 595ad3cba22SDave Kleikamp if (iter_is_iovec(iter)) 596ad3cba22SDave Kleikamp dreq->flags = NFS_ODIRECT_SHOULD_DIRTY; 597ad3cba22SDave Kleikamp 598a5864c99STrond Myklebust nfs_start_io_direct(inode); 599a5864c99STrond Myklebust 600619d30b4SAl Viro NFS_I(inode)->read_io += count; 60185128b2bSAl Viro requested = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos); 602d0b9875dSChristoph Hellwig 603a5864c99STrond Myklebust nfs_end_io_direct(inode); 604d0b9875dSChristoph Hellwig 60585128b2bSAl Viro if (requested > 0) { 606bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 60785128b2bSAl Viro if (result > 0) { 60885128b2bSAl Viro requested -= result; 609c8b8e32dSChristoph Hellwig iocb->ki_pos += result; 61014a3ec79SChristoph Hellwig } 61185128b2bSAl Viro iov_iter_revert(iter, requested); 61285128b2bSAl Viro } else { 61385128b2bSAl Viro result = requested; 61485128b2bSAl Viro } 615d0b9875dSChristoph Hellwig 616f11ac8dbSTrond Myklebust out_release: 617b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 618f11ac8dbSTrond Myklebust out: 6191da177e4SLinus Torvalds return result; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 622085d1e33STom Haynes static void 623085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode, 624085d1e33STom Haynes struct list_head *list, 625085d1e33STom Haynes struct nfs_commit_info *cinfo) 626085d1e33STom Haynes { 627e824f99aSTrond Myklebust mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 628085d1e33STom Haynes #ifdef CONFIG_NFS_V4_1 629085d1e33STom Haynes if (cinfo->ds != NULL && cinfo->ds->nwritten != 0) 630085d1e33STom Haynes NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo); 631085d1e33STom Haynes #endif 632085d1e33STom Haynes nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0); 633e824f99aSTrond Myklebust mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 634085d1e33STom Haynes } 635085d1e33STom Haynes 636fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 6371da177e4SLinus Torvalds { 6381763da12SFred Isaman struct nfs_pageio_descriptor desc; 6391763da12SFred Isaman struct nfs_page *req, *tmp; 6401763da12SFred Isaman LIST_HEAD(reqs); 6411763da12SFred Isaman struct nfs_commit_info cinfo; 6421763da12SFred Isaman LIST_HEAD(failed); 6430a00b77bSWeston Andros Adamson int i; 6441763da12SFred Isaman 6451763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 646085d1e33STom Haynes nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo); 6471da177e4SLinus Torvalds 648fad61490STrond Myklebust dreq->count = 0; 649a5314a74STrond Myklebust dreq->verf.committed = NFS_INVALID_STABLE_HOW; 650a5314a74STrond Myklebust nfs_clear_pnfs_ds_commit_verifiers(&dreq->ds_cinfo); 6510a00b77bSWeston Andros Adamson for (i = 0; i < dreq->mirror_count; i++) 6520a00b77bSWeston Andros Adamson dreq->mirrors[i].count = 0; 653607f31e8STrond Myklebust get_dreq(dreq); 6541da177e4SLinus Torvalds 655a20c93e3SChristoph Hellwig nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false, 6561763da12SFred Isaman &nfs_direct_write_completion_ops); 6571763da12SFred Isaman desc.pg_dreq = dreq; 658607f31e8STrond Myklebust 6590a00b77bSWeston Andros Adamson req = nfs_list_entry(reqs.next); 6600a00b77bSWeston Andros Adamson nfs_direct_setup_mirroring(dreq, &desc, req); 661d600ad1fSPeng Tao if (desc.pg_error < 0) { 662d600ad1fSPeng Tao list_splice_init(&reqs, &failed); 663d600ad1fSPeng Tao goto out_failed; 664d600ad1fSPeng Tao } 6650a00b77bSWeston Andros Adamson 6661763da12SFred Isaman list_for_each_entry_safe(req, tmp, &reqs, wb_list) { 66733344e0fSTrond Myklebust /* Bump the transmission count */ 66833344e0fSTrond Myklebust req->wb_nio++; 6691763da12SFred Isaman if (!nfs_pageio_add_request(&desc, req)) { 670078b5fd9STrond Myklebust nfs_list_move_request(req, &failed); 671fe238e60SDave Wysochanski spin_lock(&cinfo.inode->i_lock); 6721763da12SFred Isaman dreq->flags = 0; 673d600ad1fSPeng Tao if (desc.pg_error < 0) 674d600ad1fSPeng Tao dreq->error = desc.pg_error; 675d600ad1fSPeng Tao else 6761763da12SFred Isaman dreq->error = -EIO; 677fe238e60SDave Wysochanski spin_unlock(&cinfo.inode->i_lock); 6781763da12SFred Isaman } 6795a695da2STrond Myklebust nfs_release_request(req); 6801763da12SFred Isaman } 6811763da12SFred Isaman nfs_pageio_complete(&desc); 682607f31e8STrond Myklebust 683d600ad1fSPeng Tao out_failed: 6844035c248STrond Myklebust while (!list_empty(&failed)) { 6854035c248STrond Myklebust req = nfs_list_entry(failed.next); 6864035c248STrond Myklebust nfs_list_remove_request(req); 6871d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 6884035c248STrond Myklebust } 689607f31e8STrond Myklebust 690607f31e8STrond Myklebust if (put_dreq(dreq)) 6914d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 692fad61490STrond Myklebust } 6931da177e4SLinus Torvalds 6941763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data) 695fad61490STrond Myklebust { 6960b7c0153SFred Isaman struct nfs_direct_req *dreq = data->dreq; 6971763da12SFred Isaman struct nfs_commit_info cinfo; 6981763da12SFred Isaman struct nfs_page *req; 699c9d8f89dSTrond Myklebust int status = data->task.tk_status; 700c9d8f89dSTrond Myklebust 7011763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 702fe4f844dSAnna Schumaker if (status < 0 || nfs_direct_cmp_commit_data_verf(dreq, data)) 703fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 704fad61490STrond Myklebust 7051763da12SFred Isaman while (!list_empty(&data->pages)) { 7061763da12SFred Isaman req = nfs_list_entry(data->pages.next); 7071763da12SFred Isaman nfs_list_remove_request(req); 7081763da12SFred Isaman if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) { 70933344e0fSTrond Myklebust /* 71033344e0fSTrond Myklebust * Despite the reboot, the write was successful, 71133344e0fSTrond Myklebust * so reset wb_nio. 71233344e0fSTrond Myklebust */ 71333344e0fSTrond Myklebust req->wb_nio = 0; 7141763da12SFred Isaman /* Note the rewrite will go through mds */ 715b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, NULL, &cinfo, 0); 716906369e4SFred Isaman } else 717906369e4SFred Isaman nfs_release_request(req); 7181d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 719fad61490STrond Myklebust } 720fad61490STrond Myklebust 7211763da12SFred Isaman if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) 7224d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 7231763da12SFred Isaman } 7241763da12SFred Isaman 725b20135d0STrond Myklebust static void nfs_direct_resched_write(struct nfs_commit_info *cinfo, 726b20135d0STrond Myklebust struct nfs_page *req) 7271763da12SFred Isaman { 728b20135d0STrond Myklebust struct nfs_direct_req *dreq = cinfo->dreq; 729b20135d0STrond Myklebust 730b20135d0STrond Myklebust spin_lock(&dreq->lock); 731b20135d0STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 732b20135d0STrond Myklebust spin_unlock(&dreq->lock); 733b20135d0STrond Myklebust nfs_mark_request_commit(req, NULL, cinfo, 0); 7341763da12SFred Isaman } 7351763da12SFred Isaman 7361763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = { 7371763da12SFred Isaman .completion = nfs_direct_commit_complete, 738b20135d0STrond Myklebust .resched_write = nfs_direct_resched_write, 739fad61490STrond Myklebust }; 740fad61490STrond Myklebust 741fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 742fad61490STrond Myklebust { 7431763da12SFred Isaman int res; 7441763da12SFred Isaman struct nfs_commit_info cinfo; 7451763da12SFred Isaman LIST_HEAD(mds_list); 746fad61490STrond Myklebust 7471763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 7481763da12SFred Isaman nfs_scan_commit(dreq->inode, &mds_list, &cinfo); 7491763da12SFred Isaman res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo); 7501763da12SFred Isaman if (res < 0) /* res == -ENOMEM */ 7511763da12SFred Isaman nfs_direct_write_reschedule(dreq); 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 7541763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work) 7551da177e4SLinus Torvalds { 7561763da12SFred Isaman struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work); 757fad61490STrond Myklebust int flags = dreq->flags; 7581da177e4SLinus Torvalds 759fad61490STrond Myklebust dreq->flags = 0; 760fad61490STrond Myklebust switch (flags) { 761fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 762fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 7631da177e4SLinus Torvalds break; 764fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 765fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 7661da177e4SLinus Torvalds break; 7671da177e4SLinus Torvalds default: 768f7b5c340STrond Myklebust nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping); 769f7b5c340STrond Myklebust nfs_direct_complete(dreq); 7701da177e4SLinus Torvalds } 771fad61490STrond Myklebust } 772fad61490STrond Myklebust 7734d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq) 774fad61490STrond Myklebust { 77546483c2eSNeilBrown queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */ 776fad61490STrond Myklebust } 7771763da12SFred Isaman 7781763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr) 7791763da12SFred Isaman { 7801763da12SFred Isaman struct nfs_direct_req *dreq = hdr->dreq; 7811763da12SFred Isaman struct nfs_commit_info cinfo; 782c65e6254SWeston Andros Adamson bool request_commit = false; 7831763da12SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 7841763da12SFred Isaman 7851763da12SFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 7861763da12SFred Isaman goto out_put; 7871763da12SFred Isaman 7881763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 7891763da12SFred Isaman 7901763da12SFred Isaman spin_lock(&dreq->lock); 7911763da12SFred Isaman 7921b8d97b0SJ. Bruce Fields if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) 7931763da12SFred Isaman dreq->error = hdr->error; 794c65e6254SWeston Andros Adamson if (dreq->error == 0) { 7950a00b77bSWeston Andros Adamson nfs_direct_good_bytes(dreq, hdr); 796c65e6254SWeston Andros Adamson if (nfs_write_need_commit(hdr)) { 7971763da12SFred Isaman if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) 798c65e6254SWeston Andros Adamson request_commit = true; 7991763da12SFred Isaman else if (dreq->flags == 0) { 8005002c586SWeston Andros Adamson nfs_direct_set_hdr_verf(dreq, hdr); 801c65e6254SWeston Andros Adamson request_commit = true; 8021763da12SFred Isaman dreq->flags = NFS_ODIRECT_DO_COMMIT; 8031763da12SFred Isaman } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) { 804c65e6254SWeston Andros Adamson request_commit = true; 805c65e6254SWeston Andros Adamson if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr)) 8065002c586SWeston Andros Adamson dreq->flags = 8075002c586SWeston Andros Adamson NFS_ODIRECT_RESCHED_WRITES; 8081763da12SFred Isaman } 8091763da12SFred Isaman } 8101763da12SFred Isaman } 8111763da12SFred Isaman spin_unlock(&dreq->lock); 8121763da12SFred Isaman 8131763da12SFred Isaman while (!list_empty(&hdr->pages)) { 8142bfc6e56SWeston Andros Adamson 8151763da12SFred Isaman req = nfs_list_entry(hdr->pages.next); 8161763da12SFred Isaman nfs_list_remove_request(req); 817c65e6254SWeston Andros Adamson if (request_commit) { 81804277086STrond Myklebust kref_get(&req->wb_kref); 819b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, hdr->lseg, &cinfo, 820b57ff130SWeston Andros Adamson hdr->ds_commit_idx); 8211763da12SFred Isaman } 8221d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 8231763da12SFred Isaman } 8241763da12SFred Isaman 8251763da12SFred Isaman out_put: 8261763da12SFred Isaman if (put_dreq(dreq)) 8274d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 8281763da12SFred Isaman hdr->release(hdr); 8291763da12SFred Isaman } 8301763da12SFred Isaman 831df3accb8STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head, int error) 8323e9e0ca3STrond Myklebust { 8333e9e0ca3STrond Myklebust struct nfs_page *req; 8343e9e0ca3STrond Myklebust 8353e9e0ca3STrond Myklebust while (!list_empty(head)) { 8363e9e0ca3STrond Myklebust req = nfs_list_entry(head->next); 8373e9e0ca3STrond Myklebust nfs_list_remove_request(req); 8381d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 8393e9e0ca3STrond Myklebust } 8403e9e0ca3STrond Myklebust } 8413e9e0ca3STrond Myklebust 842dc602dd7STrond Myklebust static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr) 843dc602dd7STrond Myklebust { 844dc602dd7STrond Myklebust struct nfs_direct_req *dreq = hdr->dreq; 845dc602dd7STrond Myklebust 846dc602dd7STrond Myklebust spin_lock(&dreq->lock); 847dc602dd7STrond Myklebust if (dreq->error == 0) { 848dc602dd7STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 849dc602dd7STrond Myklebust /* fake unstable write to let common nfs resend pages */ 850dc602dd7STrond Myklebust hdr->verf.committed = NFS_UNSTABLE; 851dc602dd7STrond Myklebust hdr->good_bytes = hdr->args.count; 852dc602dd7STrond Myklebust } 853dc602dd7STrond Myklebust spin_unlock(&dreq->lock); 854dc602dd7STrond Myklebust } 855dc602dd7STrond Myklebust 8561763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = { 8573e9e0ca3STrond Myklebust .error_cleanup = nfs_write_sync_pgio_error, 8581763da12SFred Isaman .init_hdr = nfs_direct_pgio_init, 8591763da12SFred Isaman .completion = nfs_direct_write_completion, 860dc602dd7STrond Myklebust .reschedule_io = nfs_direct_write_reschedule_io, 8611763da12SFred Isaman }; 8621763da12SFred Isaman 86391f79c43SAl Viro 86491f79c43SAl Viro /* 86591f79c43SAl Viro * NB: Return the value of the first error return code. Subsequent 86691f79c43SAl Viro * errors after the first one are ignored. 86791f79c43SAl Viro */ 86891f79c43SAl Viro /* 86991f79c43SAl Viro * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE 87091f79c43SAl Viro * operation. If nfs_writedata_alloc() or get_user_pages() fails, 87191f79c43SAl Viro * bail and stop sending more writes. Write length accounting is 87291f79c43SAl Viro * handled automatically by nfs_direct_write_result(). Otherwise, if 87391f79c43SAl Viro * no requests have been sent, just return an error. 87491f79c43SAl Viro */ 87519f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq, 876619d30b4SAl Viro struct iov_iter *iter, 87791f79c43SAl Viro loff_t pos) 87819f73787SChuck Lever { 8791763da12SFred Isaman struct nfs_pageio_descriptor desc; 8801d59d61fSTrond Myklebust struct inode *inode = dreq->inode; 88119f73787SChuck Lever ssize_t result = 0; 88219f73787SChuck Lever size_t requested_bytes = 0; 88391f79c43SAl Viro size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE); 88419f73787SChuck Lever 885a20c93e3SChristoph Hellwig nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false, 8861763da12SFred Isaman &nfs_direct_write_completion_ops); 8871763da12SFred Isaman desc.pg_dreq = dreq; 88819f73787SChuck Lever get_dreq(dreq); 889fe0f07d0SJens Axboe inode_dio_begin(inode); 89019f73787SChuck Lever 89191f79c43SAl Viro NFS_I(inode)->write_io += iov_iter_count(iter); 89291f79c43SAl Viro while (iov_iter_count(iter)) { 89391f79c43SAl Viro struct page **pagevec; 89491f79c43SAl Viro size_t bytes; 89591f79c43SAl Viro size_t pgbase; 89691f79c43SAl Viro unsigned npages, i; 89791f79c43SAl Viro 89891f79c43SAl Viro result = iov_iter_get_pages_alloc(iter, &pagevec, 89991f79c43SAl Viro wsize, &pgbase); 90019f73787SChuck Lever if (result < 0) 90119f73787SChuck Lever break; 90291f79c43SAl Viro 90391f79c43SAl Viro bytes = result; 90491f79c43SAl Viro iov_iter_advance(iter, bytes); 90591f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 90691f79c43SAl Viro for (i = 0; i < npages; i++) { 90791f79c43SAl Viro struct nfs_page *req; 90891f79c43SAl Viro unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 90991f79c43SAl Viro 91028b1d3f5STrond Myklebust req = nfs_create_request(dreq->ctx, pagevec[i], 91191f79c43SAl Viro pgbase, req_len); 91291f79c43SAl Viro if (IS_ERR(req)) { 91391f79c43SAl Viro result = PTR_ERR(req); 91419f73787SChuck Lever break; 91591f79c43SAl Viro } 9160a00b77bSWeston Andros Adamson 9170a00b77bSWeston Andros Adamson nfs_direct_setup_mirroring(dreq, &desc, req); 918d600ad1fSPeng Tao if (desc.pg_error < 0) { 919d600ad1fSPeng Tao nfs_free_request(req); 920d600ad1fSPeng Tao result = desc.pg_error; 921d600ad1fSPeng Tao break; 922d600ad1fSPeng Tao } 9230a00b77bSWeston Andros Adamson 92491f79c43SAl Viro nfs_lock_request(req); 92591f79c43SAl Viro req->wb_index = pos >> PAGE_SHIFT; 92691f79c43SAl Viro req->wb_offset = pos & ~PAGE_MASK; 92791f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 92891f79c43SAl Viro result = desc.pg_error; 92991f79c43SAl Viro nfs_unlock_and_release_request(req); 93091f79c43SAl Viro break; 93191f79c43SAl Viro } 93291f79c43SAl Viro pgbase = 0; 93391f79c43SAl Viro bytes -= req_len; 93491f79c43SAl Viro requested_bytes += req_len; 93591f79c43SAl Viro pos += req_len; 93691f79c43SAl Viro dreq->bytes_left -= req_len; 93791f79c43SAl Viro } 93891f79c43SAl Viro nfs_direct_release_pages(pagevec, npages); 93991f79c43SAl Viro kvfree(pagevec); 94091f79c43SAl Viro if (result < 0) 94191f79c43SAl Viro break; 94219f73787SChuck Lever } 9431763da12SFred Isaman nfs_pageio_complete(&desc); 94419f73787SChuck Lever 945839f7ad6SChuck Lever /* 946839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 947839f7ad6SChuck Lever * generic layer handle the completion. 948839f7ad6SChuck Lever */ 949839f7ad6SChuck Lever if (requested_bytes == 0) { 950fe0f07d0SJens Axboe inode_dio_end(inode); 951839f7ad6SChuck Lever nfs_direct_req_release(dreq); 952839f7ad6SChuck Lever return result < 0 ? result : -EIO; 953839f7ad6SChuck Lever } 954839f7ad6SChuck Lever 95519f73787SChuck Lever if (put_dreq(dreq)) 9564d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 95785128b2bSAl Viro return requested_bytes; 95819f73787SChuck Lever } 95919f73787SChuck Lever 9601da177e4SLinus Torvalds /** 9611da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 9621da177e4SLinus Torvalds * @iocb: target I/O control block 963619d30b4SAl Viro * @iter: vector of user buffers from which to write data 9641da177e4SLinus Torvalds * 9651da177e4SLinus Torvalds * We use this function for direct writes instead of calling 9661da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 9671da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 9681da177e4SLinus Torvalds * the new i_size and this client must read the updated size 9691da177e4SLinus Torvalds * back into its cache. We let the server do generic write 9701da177e4SLinus Torvalds * parameter checking and report problems. 9711da177e4SLinus Torvalds * 9721da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 9731da177e4SLinus Torvalds * 9741da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 9751da177e4SLinus Torvalds * readers of this file. 9761da177e4SLinus Torvalds * 9771da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 9781da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 9791da177e4SLinus Torvalds */ 98065a4a1caSAl Viro ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter) 9811da177e4SLinus Torvalds { 98285128b2bSAl Viro ssize_t result = -EINVAL, requested; 98389698b24STrond Myklebust size_t count; 9841da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 9851da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 98622cd1bf1SChristoph Hellwig struct inode *inode = mapping->host; 98722cd1bf1SChristoph Hellwig struct nfs_direct_req *dreq; 98822cd1bf1SChristoph Hellwig struct nfs_lock_context *l_ctx; 98965a4a1caSAl Viro loff_t pos, end; 990c216fd70SChuck Lever 9916de1472fSAl Viro dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n", 9923309dd04SAl Viro file, iov_iter_count(iter), (long long) iocb->ki_pos); 993027445c3SBadari Pulavarty 99489698b24STrond Myklebust result = generic_write_checks(iocb, iter); 99589698b24STrond Myklebust if (result <= 0) 99689698b24STrond Myklebust return result; 99789698b24STrond Myklebust count = result; 99889698b24STrond Myklebust nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); 9993309dd04SAl Viro 10003309dd04SAl Viro pos = iocb->ki_pos; 100109cbfeafSKirill A. Shutemov end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT; 1002ce1a8e67SChuck Lever 100389698b24STrond Myklebust task_io_account_write(count); 10047ec10f26SKonstantin Khlebnikov 100522cd1bf1SChristoph Hellwig result = -ENOMEM; 100622cd1bf1SChristoph Hellwig dreq = nfs_direct_req_alloc(); 100722cd1bf1SChristoph Hellwig if (!dreq) 1008a5864c99STrond Myklebust goto out; 100922cd1bf1SChristoph Hellwig 101022cd1bf1SChristoph Hellwig dreq->inode = inode; 101189698b24STrond Myklebust dreq->bytes_left = dreq->max_count = count; 10125fadeb47SPeng Tao dreq->io_start = pos; 101322cd1bf1SChristoph Hellwig dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 101422cd1bf1SChristoph Hellwig l_ctx = nfs_get_lock_context(dreq->ctx); 101522cd1bf1SChristoph Hellwig if (IS_ERR(l_ctx)) { 101622cd1bf1SChristoph Hellwig result = PTR_ERR(l_ctx); 101722cd1bf1SChristoph Hellwig goto out_release; 101822cd1bf1SChristoph Hellwig } 101922cd1bf1SChristoph Hellwig dreq->l_ctx = l_ctx; 102022cd1bf1SChristoph Hellwig if (!is_sync_kiocb(iocb)) 102122cd1bf1SChristoph Hellwig dreq->iocb = iocb; 102222cd1bf1SChristoph Hellwig 1023a5864c99STrond Myklebust nfs_start_io_direct(inode); 1024a5864c99STrond Myklebust 102585128b2bSAl Viro requested = nfs_direct_write_schedule_iovec(dreq, iter, pos); 1026a9ab5e84SChristoph Hellwig 1027a9ab5e84SChristoph Hellwig if (mapping->nrpages) { 1028a9ab5e84SChristoph Hellwig invalidate_inode_pages2_range(mapping, 102909cbfeafSKirill A. Shutemov pos >> PAGE_SHIFT, end); 1030a9ab5e84SChristoph Hellwig } 1031a9ab5e84SChristoph Hellwig 1032a5864c99STrond Myklebust nfs_end_io_direct(inode); 1033a9ab5e84SChristoph Hellwig 103485128b2bSAl Viro if (requested > 0) { 103522cd1bf1SChristoph Hellwig result = nfs_direct_wait(dreq); 103622cd1bf1SChristoph Hellwig if (result > 0) { 103785128b2bSAl Viro requested -= result; 103822cd1bf1SChristoph Hellwig iocb->ki_pos = pos + result; 1039e2592217SChristoph Hellwig /* XXX: should check the generic_write_sync retval */ 1040e2592217SChristoph Hellwig generic_write_sync(iocb, result); 10411763da12SFred Isaman } 104285128b2bSAl Viro iov_iter_revert(iter, requested); 104385128b2bSAl Viro } else { 104485128b2bSAl Viro result = requested; 104522cd1bf1SChristoph Hellwig } 104622cd1bf1SChristoph Hellwig out_release: 104722cd1bf1SChristoph Hellwig nfs_direct_req_release(dreq); 1048a5864c99STrond Myklebust out: 104922cd1bf1SChristoph Hellwig return result; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 105288467055SChuck Lever /** 105388467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 105488467055SChuck Lever * 105588467055SChuck Lever */ 1056f7b422b1SDavid Howells int __init nfs_init_directcache(void) 10571da177e4SLinus Torvalds { 10581da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 10591da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 1060fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 1061fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 106220c2df83SPaul Mundt NULL); 10631da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 10641da177e4SLinus Torvalds return -ENOMEM; 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds return 0; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 106988467055SChuck Lever /** 1070f7b422b1SDavid Howells * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures 107188467055SChuck Lever * 107288467055SChuck Lever */ 1073266bee88SDavid Brownell void nfs_destroy_directcache(void) 10741da177e4SLinus Torvalds { 10751a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_direct_cachep); 10761da177e4SLinus Torvalds } 1077