11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/direct.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2003 by Chuck Lever <cel@netapp.com> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * High-performance uncached I/O for the Linux NFS client 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * There are important applications whose performance or correctness 91da177e4SLinus Torvalds * depends on uncached access to file data. Database clusters 101da177e4SLinus Torvalds * (multiple copies of the same instance running on separate hosts) 111da177e4SLinus Torvalds * implement their own cache coherency protocol that subsumes file 121da177e4SLinus Torvalds * system cache protocols. Applications that process datasets 131da177e4SLinus Torvalds * considerably larger than the client's memory do not always benefit 141da177e4SLinus Torvalds * from a local cache. A streaming video server, for instance, has no 151da177e4SLinus Torvalds * need to cache the contents of a file. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * When an application requests uncached I/O, all read and write requests 181da177e4SLinus Torvalds * are made directly to the server; data stored or fetched via these 191da177e4SLinus Torvalds * requests is not cached in the Linux page cache. The client does not 201da177e4SLinus Torvalds * correct unaligned requests from applications. All requested bytes are 211da177e4SLinus Torvalds * held on permanent storage before a direct write system call returns to 221da177e4SLinus Torvalds * an application. 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * Solaris implements an uncached I/O facility called directio() that 251da177e4SLinus Torvalds * is used for backups and sequential I/O to very large files. Solaris 261da177e4SLinus Torvalds * also supports uncaching whole NFS partitions with "-o forcedirectio," 271da177e4SLinus Torvalds * an undocumented mount option. 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with 301da177e4SLinus Torvalds * help from Andrew Morton. 311da177e4SLinus Torvalds * 321da177e4SLinus Torvalds * 18 Dec 2001 Initial implementation for 2.4 --cel 331da177e4SLinus Torvalds * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy 341da177e4SLinus Torvalds * 08 Jun 2003 Port to 2.5 APIs --cel 351da177e4SLinus Torvalds * 31 Mar 2004 Handle direct I/O without VFS support --cel 361da177e4SLinus Torvalds * 15 Sep 2004 Parallel async reads --cel 3788467055SChuck Lever * 04 May 2005 support O_DIRECT with aio --cel 381da177e4SLinus Torvalds * 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <linux/errno.h> 421da177e4SLinus Torvalds #include <linux/sched.h> 431da177e4SLinus Torvalds #include <linux/kernel.h> 441da177e4SLinus Torvalds #include <linux/file.h> 451da177e4SLinus Torvalds #include <linux/pagemap.h> 461da177e4SLinus Torvalds #include <linux/kref.h> 475a0e3ad6STejun Heo #include <linux/slab.h> 487ec10f26SKonstantin Khlebnikov #include <linux/task_io_accounting_ops.h> 496296556fSPeng Tao #include <linux/module.h> 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds #include <linux/nfs_fs.h> 521da177e4SLinus Torvalds #include <linux/nfs_page.h> 531da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #include <asm/uaccess.h> 5660063497SArun Sharma #include <linux/atomic.h> 571da177e4SLinus Torvalds 588d5658c9STrond Myklebust #include "internal.h" 5991d5b470SChuck Lever #include "iostat.h" 601763da12SFred Isaman #include "pnfs.h" 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_VFS 631da177e4SLinus Torvalds 64e18b890bSChristoph Lameter static struct kmem_cache *nfs_direct_cachep; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds /* 671da177e4SLinus Torvalds * This represents a set of asynchronous requests that we're waiting on 681da177e4SLinus Torvalds */ 691da177e4SLinus Torvalds struct nfs_direct_req { 701da177e4SLinus Torvalds struct kref kref; /* release manager */ 7115ce4a0cSChuck Lever 7215ce4a0cSChuck Lever /* I/O parameters */ 73a8881f5aSTrond Myklebust struct nfs_open_context *ctx; /* file open context info */ 74f11ac8dbSTrond Myklebust struct nfs_lock_context *l_ctx; /* Lock context info */ 7599514f8fSChuck Lever struct kiocb * iocb; /* controlling i/o request */ 7688467055SChuck Lever struct inode * inode; /* target file of i/o */ 7715ce4a0cSChuck Lever 7815ce4a0cSChuck Lever /* completion state */ 79607f31e8STrond Myklebust atomic_t io_count; /* i/os we're waiting for */ 8015ce4a0cSChuck Lever spinlock_t lock; /* protect completion state */ 8115ce4a0cSChuck Lever ssize_t count, /* bytes actually processed */ 8235754bc0SPeng Tao bytes_left, /* bytes left to be sent */ 831da177e4SLinus Torvalds error; /* any reported error */ 84d72b7a6bSTrond Myklebust struct completion completion; /* wait for i/o completion */ 85fad61490STrond Myklebust 86fad61490STrond Myklebust /* commit state */ 871763da12SFred Isaman struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */ 881763da12SFred Isaman struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */ 891763da12SFred Isaman struct work_struct work; 90fad61490STrond Myklebust int flags; 91fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 92fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 93fad61490STrond Myklebust struct nfs_writeverf verf; /* unstable write verifier */ 941da177e4SLinus Torvalds }; 951da177e4SLinus Torvalds 961763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops; 971763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops; 98fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); 991763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work); 100607f31e8STrond Myklebust 101607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq) 102607f31e8STrond Myklebust { 103607f31e8STrond Myklebust atomic_inc(&dreq->io_count); 104607f31e8STrond Myklebust } 105607f31e8STrond Myklebust 106607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq) 107607f31e8STrond Myklebust { 108607f31e8STrond Myklebust return atomic_dec_and_test(&dreq->io_count); 109607f31e8STrond Myklebust } 110607f31e8STrond Myklebust 1115002c586SWeston Andros Adamson /* 1125002c586SWeston Andros Adamson * nfs_direct_select_verf - select the right verifier 1135002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 1145002c586SWeston Andros Adamson * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs 1155002c586SWeston Andros Adamson * @ds_idx - index of data server in data server list, only valid if ds_clp set 1165002c586SWeston Andros Adamson * 1175002c586SWeston Andros Adamson * returns the correct verifier to use given the role of the server 1185002c586SWeston Andros Adamson */ 1195002c586SWeston Andros Adamson static struct nfs_writeverf * 1205002c586SWeston Andros Adamson nfs_direct_select_verf(struct nfs_direct_req *dreq, 1215002c586SWeston Andros Adamson struct nfs_client *ds_clp, 1225002c586SWeston Andros Adamson int ds_idx) 1235002c586SWeston Andros Adamson { 1245002c586SWeston Andros Adamson struct nfs_writeverf *verfp = &dreq->verf; 1255002c586SWeston Andros Adamson 1265002c586SWeston Andros Adamson #ifdef CONFIG_NFS_V4_1 1275002c586SWeston Andros Adamson if (ds_clp) { 1285002c586SWeston Andros Adamson /* pNFS is in use, use the DS verf */ 1295002c586SWeston Andros Adamson if (ds_idx >= 0 && ds_idx < dreq->ds_cinfo.nbuckets) 1305002c586SWeston Andros Adamson verfp = &dreq->ds_cinfo.buckets[ds_idx].direct_verf; 1315002c586SWeston Andros Adamson else 1325002c586SWeston Andros Adamson WARN_ON_ONCE(1); 1335002c586SWeston Andros Adamson } 1345002c586SWeston Andros Adamson #endif 1355002c586SWeston Andros Adamson return verfp; 1365002c586SWeston Andros Adamson } 1375002c586SWeston Andros Adamson 1385002c586SWeston Andros Adamson 1395002c586SWeston Andros Adamson /* 1405002c586SWeston Andros Adamson * nfs_direct_set_hdr_verf - set the write/commit verifier 1415002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 1425002c586SWeston Andros Adamson * @hdr - pageio header to validate against previously seen verfs 1435002c586SWeston Andros Adamson * 1445002c586SWeston Andros Adamson * Set the server's (MDS or DS) "seen" verifier 1455002c586SWeston Andros Adamson */ 1465002c586SWeston Andros Adamson static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq, 1475002c586SWeston Andros Adamson struct nfs_pgio_header *hdr) 1485002c586SWeston Andros Adamson { 1495002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 1505002c586SWeston Andros Adamson 151d45f60c6SWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, 152d45f60c6SWeston Andros Adamson hdr->ds_idx); 1535002c586SWeston Andros Adamson WARN_ON_ONCE(verfp->committed >= 0); 1545002c586SWeston Andros Adamson memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf)); 1555002c586SWeston Andros Adamson WARN_ON_ONCE(verfp->committed < 0); 1565002c586SWeston Andros Adamson } 1575002c586SWeston Andros Adamson 1585002c586SWeston Andros Adamson /* 1595002c586SWeston Andros Adamson * nfs_direct_cmp_hdr_verf - compare verifier for pgio header 1605002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 1615002c586SWeston Andros Adamson * @hdr - pageio header to validate against previously seen verf 1625002c586SWeston Andros Adamson * 1635002c586SWeston Andros Adamson * set the server's "seen" verf if not initialized. 1645002c586SWeston Andros Adamson * returns result of comparison between @hdr->verf and the "seen" 1655002c586SWeston Andros Adamson * verf of the server used by @hdr (DS or MDS) 1665002c586SWeston Andros Adamson */ 1675002c586SWeston Andros Adamson static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq, 1685002c586SWeston Andros Adamson struct nfs_pgio_header *hdr) 1695002c586SWeston Andros Adamson { 1705002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 1715002c586SWeston Andros Adamson 172d45f60c6SWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, 173d45f60c6SWeston Andros Adamson hdr->ds_idx); 1745002c586SWeston Andros Adamson if (verfp->committed < 0) { 1755002c586SWeston Andros Adamson nfs_direct_set_hdr_verf(dreq, hdr); 1765002c586SWeston Andros Adamson return 0; 1775002c586SWeston Andros Adamson } 1785002c586SWeston Andros Adamson return memcmp(verfp, &hdr->verf, sizeof(struct nfs_writeverf)); 1795002c586SWeston Andros Adamson } 1805002c586SWeston Andros Adamson 1815002c586SWeston Andros Adamson /* 1825002c586SWeston Andros Adamson * nfs_direct_cmp_commit_data_verf - compare verifier for commit data 1835002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 1845002c586SWeston Andros Adamson * @data - commit data to validate against previously seen verf 1855002c586SWeston Andros Adamson * 1865002c586SWeston Andros Adamson * returns result of comparison between @data->verf and the verf of 1875002c586SWeston Andros Adamson * the server used by @data (DS or MDS) 1885002c586SWeston Andros Adamson */ 1895002c586SWeston Andros Adamson static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq, 1905002c586SWeston Andros Adamson struct nfs_commit_data *data) 1915002c586SWeston Andros Adamson { 1925002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 1935002c586SWeston Andros Adamson 1945002c586SWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, data->ds_clp, 1955002c586SWeston Andros Adamson data->ds_commit_index); 1965002c586SWeston Andros Adamson WARN_ON_ONCE(verfp->committed < 0); 1975002c586SWeston Andros Adamson return memcmp(verfp, &data->verf, sizeof(struct nfs_writeverf)); 1985002c586SWeston Andros Adamson } 1995002c586SWeston Andros Adamson 2001da177e4SLinus Torvalds /** 201b8a32e2bSChuck Lever * nfs_direct_IO - NFS address space operation for direct I/O 202b8a32e2bSChuck Lever * @rw: direction (read or write) 203b8a32e2bSChuck Lever * @iocb: target I/O control block 204b8a32e2bSChuck Lever * @iov: array of vectors that define I/O buffer 205b8a32e2bSChuck Lever * @pos: offset in file to begin the operation 206b8a32e2bSChuck Lever * @nr_segs: size of iovec array 207b8a32e2bSChuck Lever * 208b8a32e2bSChuck Lever * The presence of this routine in the address space ops vector means 209a564b8f0SMel Gorman * the NFS client supports direct I/O. However, for most direct IO, we 210a564b8f0SMel Gorman * shunt off direct read and write requests before the VFS gets them, 211a564b8f0SMel Gorman * so this method is only ever called for swap. 2121da177e4SLinus Torvalds */ 213d8d3d94bSAl Viro ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter, loff_t pos) 214b8a32e2bSChuck Lever { 215a564b8f0SMel Gorman #ifndef CONFIG_NFS_SWAP 2166de1472fSAl Viro dprintk("NFS: nfs_direct_IO (%pD) off/no(%Ld/%lu) EINVAL\n", 217d8d3d94bSAl Viro iocb->ki_filp, (long long) pos, iter->nr_segs); 218b8a32e2bSChuck Lever 219b8a32e2bSChuck Lever return -EINVAL; 220a564b8f0SMel Gorman #else 221a564b8f0SMel Gorman VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE); 222a564b8f0SMel Gorman 223e19a8a0aSMartin K. Petersen if (rw == READ) 224e19a8a0aSMartin K. Petersen return nfs_file_direct_read(iocb, iter, pos); 225e19a8a0aSMartin K. Petersen return nfs_file_direct_write(iocb, iter, pos); 226a564b8f0SMel Gorman #endif /* CONFIG_NFS_SWAP */ 227b8a32e2bSChuck Lever } 228b8a32e2bSChuck Lever 229749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages) 2309c93ab7dSChuck Lever { 231749e146eSChuck Lever unsigned int i; 232607f31e8STrond Myklebust for (i = 0; i < npages; i++) 233607f31e8STrond Myklebust page_cache_release(pages[i]); 2346b45d858STrond Myklebust } 2356b45d858STrond Myklebust 2361763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo, 2371763da12SFred Isaman struct nfs_direct_req *dreq) 2381763da12SFred Isaman { 2391763da12SFred Isaman cinfo->lock = &dreq->lock; 2401763da12SFred Isaman cinfo->mds = &dreq->mds_cinfo; 2411763da12SFred Isaman cinfo->ds = &dreq->ds_cinfo; 2421763da12SFred Isaman cinfo->dreq = dreq; 2431763da12SFred Isaman cinfo->completion_ops = &nfs_direct_commit_completion_ops; 2441763da12SFred Isaman } 2451763da12SFred Isaman 24693619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void) 2471da177e4SLinus Torvalds { 2481da177e4SLinus Torvalds struct nfs_direct_req *dreq; 2491da177e4SLinus Torvalds 250292f3eeeSTrond Myklebust dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL); 2511da177e4SLinus Torvalds if (!dreq) 2521da177e4SLinus Torvalds return NULL; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds kref_init(&dreq->kref); 255607f31e8STrond Myklebust kref_get(&dreq->kref); 256d72b7a6bSTrond Myklebust init_completion(&dreq->completion); 2571763da12SFred Isaman INIT_LIST_HEAD(&dreq->mds_cinfo.list); 2585002c586SWeston Andros Adamson dreq->verf.committed = NFS_INVALID_STABLE_HOW; /* not set yet */ 2591763da12SFred Isaman INIT_WORK(&dreq->work, nfs_direct_write_schedule_work); 26015ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 26193619e59SChuck Lever 26293619e59SChuck Lever return dreq; 26393619e59SChuck Lever } 26493619e59SChuck Lever 265b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 268a8881f5aSTrond Myklebust 2698c393f9aSPeng Tao nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo); 270f11ac8dbSTrond Myklebust if (dreq->l_ctx != NULL) 271f11ac8dbSTrond Myklebust nfs_put_lock_context(dreq->l_ctx); 272a8881f5aSTrond Myklebust if (dreq->ctx != NULL) 273a8881f5aSTrond Myklebust put_nfs_open_context(dreq->ctx); 2741da177e4SLinus Torvalds kmem_cache_free(nfs_direct_cachep, dreq); 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 277b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq) 278b4946ffbSTrond Myklebust { 279b4946ffbSTrond Myklebust kref_put(&dreq->kref, nfs_direct_req_free); 280b4946ffbSTrond Myklebust } 281b4946ffbSTrond Myklebust 2826296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq) 2836296556fSPeng Tao { 2846296556fSPeng Tao return dreq->bytes_left; 2856296556fSPeng Tao } 2866296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left); 2876296556fSPeng Tao 288d4cc948bSChuck Lever /* 289bc0fb201SChuck Lever * Collects and returns the final error value/byte-count. 290bc0fb201SChuck Lever */ 291bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) 292bc0fb201SChuck Lever { 29315ce4a0cSChuck Lever ssize_t result = -EIOCBQUEUED; 294bc0fb201SChuck Lever 295bc0fb201SChuck Lever /* Async requests don't wait here */ 296bc0fb201SChuck Lever if (dreq->iocb) 297bc0fb201SChuck Lever goto out; 298bc0fb201SChuck Lever 299150030b7SMatthew Wilcox result = wait_for_completion_killable(&dreq->completion); 300bc0fb201SChuck Lever 301bc0fb201SChuck Lever if (!result) 30215ce4a0cSChuck Lever result = dreq->error; 303bc0fb201SChuck Lever if (!result) 30415ce4a0cSChuck Lever result = dreq->count; 305bc0fb201SChuck Lever 306bc0fb201SChuck Lever out: 307bc0fb201SChuck Lever return (ssize_t) result; 308bc0fb201SChuck Lever } 309bc0fb201SChuck Lever 310bc0fb201SChuck Lever /* 311607f31e8STrond Myklebust * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust 312607f31e8STrond Myklebust * the iocb is still valid here if this is a synchronous request. 31363ab46abSChuck Lever */ 3149811cd57SChristoph Hellwig static void nfs_direct_complete(struct nfs_direct_req *dreq, bool write) 31563ab46abSChuck Lever { 3169811cd57SChristoph Hellwig struct inode *inode = dreq->inode; 3179811cd57SChristoph Hellwig 3182a009ec9SChristoph Hellwig if (dreq->iocb && write) { 3199811cd57SChristoph Hellwig loff_t pos = dreq->iocb->ki_pos + dreq->count; 3209811cd57SChristoph Hellwig 3219811cd57SChristoph Hellwig spin_lock(&inode->i_lock); 3229811cd57SChristoph Hellwig if (i_size_read(inode) < pos) 3239811cd57SChristoph Hellwig i_size_write(inode, pos); 3249811cd57SChristoph Hellwig spin_unlock(&inode->i_lock); 3259811cd57SChristoph Hellwig } 3269811cd57SChristoph Hellwig 3271f90ee27SChristoph Hellwig if (write) 3282a009ec9SChristoph Hellwig nfs_zap_mapping(inode, inode->i_mapping); 3291f90ee27SChristoph Hellwig 3302a009ec9SChristoph Hellwig inode_dio_done(inode); 3312a009ec9SChristoph Hellwig 3322a009ec9SChristoph Hellwig if (dreq->iocb) { 3332a009ec9SChristoph Hellwig long res = (long) dreq->error; 3342a009ec9SChristoph Hellwig if (!res) 3352a009ec9SChristoph Hellwig res = (long) dreq->count; 33663ab46abSChuck Lever aio_complete(dreq->iocb, res, 0); 337d72b7a6bSTrond Myklebust } 3382a009ec9SChristoph Hellwig 339d72b7a6bSTrond Myklebust complete_all(&dreq->completion); 34063ab46abSChuck Lever 341b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 34263ab46abSChuck Lever } 34363ab46abSChuck Lever 3441385b811STrond Myklebust static void nfs_direct_readpage_release(struct nfs_page *req) 3451da177e4SLinus Torvalds { 3461e8968c5SNiels de Vos dprintk("NFS: direct read done (%s/%llu %d@%lld)\n", 347584aa810SFred Isaman req->wb_context->dentry->d_inode->i_sb->s_id, 3481e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode), 349584aa810SFred Isaman req->wb_bytes, 350584aa810SFred Isaman (long long)req_offset(req)); 351584aa810SFred Isaman nfs_release_request(req); 352fdd1e74cSTrond Myklebust } 353fdd1e74cSTrond Myklebust 354584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr) 355fdd1e74cSTrond Myklebust { 356584aa810SFred Isaman unsigned long bytes = 0; 357584aa810SFred Isaman struct nfs_direct_req *dreq = hdr->dreq; 358fdd1e74cSTrond Myklebust 359584aa810SFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 360584aa810SFred Isaman goto out_put; 3611da177e4SLinus Torvalds 36215ce4a0cSChuck Lever spin_lock(&dreq->lock); 363584aa810SFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0)) 364584aa810SFred Isaman dreq->error = hdr->error; 365584aa810SFred Isaman else 366584aa810SFred Isaman dreq->count += hdr->good_bytes; 36715ce4a0cSChuck Lever spin_unlock(&dreq->lock); 3681da177e4SLinus Torvalds 369584aa810SFred Isaman while (!list_empty(&hdr->pages)) { 370584aa810SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 371584aa810SFred Isaman struct page *page = req->wb_page; 372584aa810SFred Isaman 373be7e9858SJeff Layton if (!PageCompound(page) && bytes < hdr->good_bytes) 3744bd8b010STrond Myklebust set_page_dirty(page); 375584aa810SFred Isaman bytes += req->wb_bytes; 376584aa810SFred Isaman nfs_list_remove_request(req); 377584aa810SFred Isaman nfs_direct_readpage_release(req); 378584aa810SFred Isaman } 379584aa810SFred Isaman out_put: 380607f31e8STrond Myklebust if (put_dreq(dreq)) 3819811cd57SChristoph Hellwig nfs_direct_complete(dreq, false); 382584aa810SFred Isaman hdr->release(hdr); 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 3853e9e0ca3STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head) 386cd841605SFred Isaman { 387584aa810SFred Isaman struct nfs_page *req; 388cd841605SFred Isaman 389584aa810SFred Isaman while (!list_empty(head)) { 390584aa810SFred Isaman req = nfs_list_entry(head->next); 391584aa810SFred Isaman nfs_list_remove_request(req); 392584aa810SFred Isaman nfs_release_request(req); 393cd841605SFred Isaman } 394584aa810SFred Isaman } 395584aa810SFred Isaman 396584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr) 397584aa810SFred Isaman { 398584aa810SFred Isaman get_dreq(hdr->dreq); 399584aa810SFred Isaman } 400584aa810SFred Isaman 401584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = { 4023e9e0ca3STrond Myklebust .error_cleanup = nfs_read_sync_pgio_error, 403584aa810SFred Isaman .init_hdr = nfs_direct_pgio_init, 404584aa810SFred Isaman .completion = nfs_direct_read_completion, 405584aa810SFred Isaman }; 406cd841605SFred Isaman 407d4cc948bSChuck Lever /* 408607f31e8STrond Myklebust * For each rsize'd chunk of the user's buffer, dispatch an NFS READ 409607f31e8STrond Myklebust * operation. If nfs_readdata_alloc() or get_user_pages() fails, 410607f31e8STrond Myklebust * bail and stop sending more reads. Read length accounting is 411607f31e8STrond Myklebust * handled automatically by nfs_direct_read_result(). Otherwise, if 412607f31e8STrond Myklebust * no requests have been sent, just return an error. 4131da177e4SLinus Torvalds */ 41491f79c43SAl Viro 41591f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq, 41691f79c43SAl Viro struct iov_iter *iter, 41791f79c43SAl Viro loff_t pos) 4181da177e4SLinus Torvalds { 41991f79c43SAl Viro struct nfs_pageio_descriptor desc; 42091f79c43SAl Viro struct inode *inode = dreq->inode; 42191f79c43SAl Viro ssize_t result = -EINVAL; 42291f79c43SAl Viro size_t requested_bytes = 0; 42391f79c43SAl Viro size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE); 42482b145c5SChuck Lever 42516b90578SLinus Torvalds nfs_pageio_init_read(&desc, dreq->inode, false, 42691f79c43SAl Viro &nfs_direct_read_completion_ops); 42791f79c43SAl Viro get_dreq(dreq); 42891f79c43SAl Viro desc.pg_dreq = dreq; 42991f79c43SAl Viro atomic_inc(&inode->i_dio_count); 43091f79c43SAl Viro 43191f79c43SAl Viro while (iov_iter_count(iter)) { 43291f79c43SAl Viro struct page **pagevec; 4335dd602f2SChuck Lever size_t bytes; 43491f79c43SAl Viro size_t pgbase; 43591f79c43SAl Viro unsigned npages, i; 4361da177e4SLinus Torvalds 43791f79c43SAl Viro result = iov_iter_get_pages_alloc(iter, &pagevec, 43891f79c43SAl Viro rsize, &pgbase); 439584aa810SFred Isaman if (result < 0) 440749e146eSChuck Lever break; 441a564b8f0SMel Gorman 44291f79c43SAl Viro bytes = result; 44391f79c43SAl Viro iov_iter_advance(iter, bytes); 44491f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 445584aa810SFred Isaman for (i = 0; i < npages; i++) { 446584aa810SFred Isaman struct nfs_page *req; 447bf5fc402STrond Myklebust unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 448584aa810SFred Isaman /* XXX do we need to do the eof zeroing found in async_filler? */ 4492bfc6e56SWeston Andros Adamson req = nfs_create_request(dreq->ctx, pagevec[i], NULL, 450584aa810SFred Isaman pgbase, req_len); 451584aa810SFred Isaman if (IS_ERR(req)) { 452584aa810SFred Isaman result = PTR_ERR(req); 453dbae4c73STrond Myklebust break; 454584aa810SFred Isaman } 455584aa810SFred Isaman req->wb_index = pos >> PAGE_SHIFT; 456584aa810SFred Isaman req->wb_offset = pos & ~PAGE_MASK; 45791f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 45891f79c43SAl Viro result = desc.pg_error; 459584aa810SFred Isaman nfs_release_request(req); 460584aa810SFred Isaman break; 461584aa810SFred Isaman } 462584aa810SFred Isaman pgbase = 0; 463584aa810SFred Isaman bytes -= req_len; 46491f79c43SAl Viro requested_bytes += req_len; 465584aa810SFred Isaman pos += req_len; 46635754bc0SPeng Tao dreq->bytes_left -= req_len; 467584aa810SFred Isaman } 4686d74743bSTrond Myklebust nfs_direct_release_pages(pagevec, npages); 46991f79c43SAl Viro kvfree(pagevec); 47019f73787SChuck Lever if (result < 0) 47119f73787SChuck Lever break; 47219f73787SChuck Lever } 47319f73787SChuck Lever 474584aa810SFred Isaman nfs_pageio_complete(&desc); 475584aa810SFred Isaman 476839f7ad6SChuck Lever /* 477839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 478839f7ad6SChuck Lever * generic layer handle the completion. 479839f7ad6SChuck Lever */ 480839f7ad6SChuck Lever if (requested_bytes == 0) { 4811f90ee27SChristoph Hellwig inode_dio_done(inode); 482839f7ad6SChuck Lever nfs_direct_req_release(dreq); 483839f7ad6SChuck Lever return result < 0 ? result : -EIO; 484839f7ad6SChuck Lever } 485839f7ad6SChuck Lever 48619f73787SChuck Lever if (put_dreq(dreq)) 4879811cd57SChristoph Hellwig nfs_direct_complete(dreq, false); 48819f73787SChuck Lever return 0; 48919f73787SChuck Lever } 49019f73787SChuck Lever 49114a3ec79SChristoph Hellwig /** 49214a3ec79SChristoph Hellwig * nfs_file_direct_read - file direct read operation for NFS files 49314a3ec79SChristoph Hellwig * @iocb: target I/O control block 494619d30b4SAl Viro * @iter: vector of user buffers into which to read data 49514a3ec79SChristoph Hellwig * @pos: byte offset in file where reading starts 49614a3ec79SChristoph Hellwig * 49714a3ec79SChristoph Hellwig * We use this function for direct reads instead of calling 49814a3ec79SChristoph Hellwig * generic_file_aio_read() in order to avoid gfar's check to see if 49914a3ec79SChristoph Hellwig * the request starts before the end of the file. For that check 50014a3ec79SChristoph Hellwig * to work, we must generate a GETATTR before each direct read, and 50114a3ec79SChristoph Hellwig * even then there is a window between the GETATTR and the subsequent 50214a3ec79SChristoph Hellwig * READ where the file size could change. Our preference is simply 50314a3ec79SChristoph Hellwig * to do all reads the application wants, and the server will take 50414a3ec79SChristoph Hellwig * care of managing the end of file boundary. 50514a3ec79SChristoph Hellwig * 50614a3ec79SChristoph Hellwig * This function also eliminates unnecessarily updating the file's 50714a3ec79SChristoph Hellwig * atime locally, as the NFS server sets the file's atime, and this 50814a3ec79SChristoph Hellwig * client must read the updated atime from the server back into its 50914a3ec79SChristoph Hellwig * cache. 51014a3ec79SChristoph Hellwig */ 511619d30b4SAl Viro ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter, 512e19a8a0aSMartin K. Petersen loff_t pos) 5131da177e4SLinus Torvalds { 51414a3ec79SChristoph Hellwig struct file *file = iocb->ki_filp; 51514a3ec79SChristoph Hellwig struct address_space *mapping = file->f_mapping; 51614a3ec79SChristoph Hellwig struct inode *inode = mapping->host; 5171da177e4SLinus Torvalds struct nfs_direct_req *dreq; 518b3c54de6STrond Myklebust struct nfs_lock_context *l_ctx; 51914a3ec79SChristoph Hellwig ssize_t result = -EINVAL; 520a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 52114a3ec79SChristoph Hellwig nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); 52214a3ec79SChristoph Hellwig 52314a3ec79SChristoph Hellwig dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n", 52414a3ec79SChristoph Hellwig file, count, (long long) pos); 52514a3ec79SChristoph Hellwig 52614a3ec79SChristoph Hellwig result = 0; 52714a3ec79SChristoph Hellwig if (!count) 52814a3ec79SChristoph Hellwig goto out; 52914a3ec79SChristoph Hellwig 530d0b9875dSChristoph Hellwig mutex_lock(&inode->i_mutex); 53114a3ec79SChristoph Hellwig result = nfs_sync_mapping(mapping); 53214a3ec79SChristoph Hellwig if (result) 533d0b9875dSChristoph Hellwig goto out_unlock; 53414a3ec79SChristoph Hellwig 53514a3ec79SChristoph Hellwig task_io_account_read(count); 53614a3ec79SChristoph Hellwig 53714a3ec79SChristoph Hellwig result = -ENOMEM; 538607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 539f11ac8dbSTrond Myklebust if (dreq == NULL) 540d0b9875dSChristoph Hellwig goto out_unlock; 5411da177e4SLinus Torvalds 54291d5b470SChuck Lever dreq->inode = inode; 543619d30b4SAl Viro dreq->bytes_left = count; 544cd3758e3STrond Myklebust dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 545b3c54de6STrond Myklebust l_ctx = nfs_get_lock_context(dreq->ctx); 546b3c54de6STrond Myklebust if (IS_ERR(l_ctx)) { 547b3c54de6STrond Myklebust result = PTR_ERR(l_ctx); 548f11ac8dbSTrond Myklebust goto out_release; 549b3c54de6STrond Myklebust } 550b3c54de6STrond Myklebust dreq->l_ctx = l_ctx; 551487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 552487b8372SChuck Lever dreq->iocb = iocb; 5531da177e4SLinus Torvalds 554619d30b4SAl Viro NFS_I(inode)->read_io += count; 55591f79c43SAl Viro result = nfs_direct_read_schedule_iovec(dreq, iter, pos); 556d0b9875dSChristoph Hellwig 557d0b9875dSChristoph Hellwig mutex_unlock(&inode->i_mutex); 558d0b9875dSChristoph Hellwig 55914a3ec79SChristoph Hellwig if (!result) { 560bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 56114a3ec79SChristoph Hellwig if (result > 0) 56214a3ec79SChristoph Hellwig iocb->ki_pos = pos + result; 56314a3ec79SChristoph Hellwig } 564d0b9875dSChristoph Hellwig 565d0b9875dSChristoph Hellwig nfs_direct_req_release(dreq); 566d0b9875dSChristoph Hellwig return result; 567d0b9875dSChristoph Hellwig 568f11ac8dbSTrond Myklebust out_release: 569b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 570d0b9875dSChristoph Hellwig out_unlock: 571d0b9875dSChristoph Hellwig mutex_unlock(&inode->i_mutex); 572f11ac8dbSTrond Myklebust out: 5731da177e4SLinus Torvalds return result; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 576*085d1e33STom Haynes static void 577*085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode, 578*085d1e33STom Haynes struct list_head *list, 579*085d1e33STom Haynes struct nfs_commit_info *cinfo) 580*085d1e33STom Haynes { 581*085d1e33STom Haynes spin_lock(cinfo->lock); 582*085d1e33STom Haynes #ifdef CONFIG_NFS_V4_1 583*085d1e33STom Haynes if (cinfo->ds != NULL && cinfo->ds->nwritten != 0) 584*085d1e33STom Haynes NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo); 585*085d1e33STom Haynes #endif 586*085d1e33STom Haynes nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0); 587*085d1e33STom Haynes spin_unlock(cinfo->lock); 588*085d1e33STom Haynes } 589*085d1e33STom Haynes 590fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 5911da177e4SLinus Torvalds { 5921763da12SFred Isaman struct nfs_pageio_descriptor desc; 5931763da12SFred Isaman struct nfs_page *req, *tmp; 5941763da12SFred Isaman LIST_HEAD(reqs); 5951763da12SFred Isaman struct nfs_commit_info cinfo; 5961763da12SFred Isaman LIST_HEAD(failed); 5971763da12SFred Isaman 5981763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 599*085d1e33STom Haynes nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo); 6001da177e4SLinus Torvalds 601fad61490STrond Myklebust dreq->count = 0; 602607f31e8STrond Myklebust get_dreq(dreq); 6031da177e4SLinus Torvalds 604a20c93e3SChristoph Hellwig nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false, 6051763da12SFred Isaman &nfs_direct_write_completion_ops); 6061763da12SFred Isaman desc.pg_dreq = dreq; 607607f31e8STrond Myklebust 6081763da12SFred Isaman list_for_each_entry_safe(req, tmp, &reqs, wb_list) { 6091763da12SFred Isaman if (!nfs_pageio_add_request(&desc, req)) { 6104035c248STrond Myklebust nfs_list_remove_request(req); 6111763da12SFred Isaman nfs_list_add_request(req, &failed); 6121763da12SFred Isaman spin_lock(cinfo.lock); 6131763da12SFred Isaman dreq->flags = 0; 6141763da12SFred Isaman dreq->error = -EIO; 6151763da12SFred Isaman spin_unlock(cinfo.lock); 6161763da12SFred Isaman } 6175a695da2STrond Myklebust nfs_release_request(req); 6181763da12SFred Isaman } 6191763da12SFred Isaman nfs_pageio_complete(&desc); 620607f31e8STrond Myklebust 6214035c248STrond Myklebust while (!list_empty(&failed)) { 6224035c248STrond Myklebust req = nfs_list_entry(failed.next); 6234035c248STrond Myklebust nfs_list_remove_request(req); 6241d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 6254035c248STrond Myklebust } 626607f31e8STrond Myklebust 627607f31e8STrond Myklebust if (put_dreq(dreq)) 6281763da12SFred Isaman nfs_direct_write_complete(dreq, dreq->inode); 629fad61490STrond Myklebust } 6301da177e4SLinus Torvalds 6311763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data) 632fad61490STrond Myklebust { 6330b7c0153SFred Isaman struct nfs_direct_req *dreq = data->dreq; 6341763da12SFred Isaman struct nfs_commit_info cinfo; 6351763da12SFred Isaman struct nfs_page *req; 636c9d8f89dSTrond Myklebust int status = data->task.tk_status; 637c9d8f89dSTrond Myklebust 6381763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 639c9d8f89dSTrond Myklebust if (status < 0) { 64060fa3f76STrond Myklebust dprintk("NFS: %5u commit failed with error %d.\n", 641c9d8f89dSTrond Myklebust data->task.tk_pid, status); 642fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 6435002c586SWeston Andros Adamson } else if (nfs_direct_cmp_commit_data_verf(dreq, data)) { 644c9d8f89dSTrond Myklebust dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid); 645fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 646fad61490STrond Myklebust } 647fad61490STrond Myklebust 648c9d8f89dSTrond Myklebust dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status); 6491763da12SFred Isaman while (!list_empty(&data->pages)) { 6501763da12SFred Isaman req = nfs_list_entry(data->pages.next); 6511763da12SFred Isaman nfs_list_remove_request(req); 6521763da12SFred Isaman if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) { 6531763da12SFred Isaman /* Note the rewrite will go through mds */ 6541763da12SFred Isaman nfs_mark_request_commit(req, NULL, &cinfo); 655906369e4SFred Isaman } else 656906369e4SFred Isaman nfs_release_request(req); 6571d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 658fad61490STrond Myklebust } 659fad61490STrond Myklebust 6601763da12SFred Isaman if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) 6611763da12SFred Isaman nfs_direct_write_complete(dreq, data->inode); 6621763da12SFred Isaman } 6631763da12SFred Isaman 6641763da12SFred Isaman static void nfs_direct_error_cleanup(struct nfs_inode *nfsi) 6651763da12SFred Isaman { 6661763da12SFred Isaman /* There is no lock to clear */ 6671763da12SFred Isaman } 6681763da12SFred Isaman 6691763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = { 6701763da12SFred Isaman .completion = nfs_direct_commit_complete, 6711763da12SFred Isaman .error_cleanup = nfs_direct_error_cleanup, 672fad61490STrond Myklebust }; 673fad61490STrond Myklebust 674fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 675fad61490STrond Myklebust { 6761763da12SFred Isaman int res; 6771763da12SFred Isaman struct nfs_commit_info cinfo; 6781763da12SFred Isaman LIST_HEAD(mds_list); 679fad61490STrond Myklebust 6801763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 6811763da12SFred Isaman nfs_scan_commit(dreq->inode, &mds_list, &cinfo); 6821763da12SFred Isaman res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo); 6831763da12SFred Isaman if (res < 0) /* res == -ENOMEM */ 6841763da12SFred Isaman nfs_direct_write_reschedule(dreq); 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 6871763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work) 6881da177e4SLinus Torvalds { 6891763da12SFred Isaman struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work); 690fad61490STrond Myklebust int flags = dreq->flags; 6911da177e4SLinus Torvalds 692fad61490STrond Myklebust dreq->flags = 0; 693fad61490STrond Myklebust switch (flags) { 694fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 695fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 6961da177e4SLinus Torvalds break; 697fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 698fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 6991da177e4SLinus Torvalds break; 7001da177e4SLinus Torvalds default: 7019811cd57SChristoph Hellwig nfs_direct_complete(dreq, true); 7021da177e4SLinus Torvalds } 703fad61490STrond Myklebust } 704fad61490STrond Myklebust 7051763da12SFred Isaman static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 706fad61490STrond Myklebust { 7071763da12SFred Isaman schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */ 708fad61490STrond Myklebust } 7091763da12SFred Isaman 7101763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr) 7111763da12SFred Isaman { 7121763da12SFred Isaman struct nfs_direct_req *dreq = hdr->dreq; 7131763da12SFred Isaman struct nfs_commit_info cinfo; 714c65e6254SWeston Andros Adamson bool request_commit = false; 7151763da12SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 7161763da12SFred Isaman 7171763da12SFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 7181763da12SFred Isaman goto out_put; 7191763da12SFred Isaman 7201763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 7211763da12SFred Isaman 7221763da12SFred Isaman spin_lock(&dreq->lock); 7231763da12SFred Isaman 7241763da12SFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) { 7251763da12SFred Isaman dreq->flags = 0; 7261763da12SFred Isaman dreq->error = hdr->error; 7271763da12SFred Isaman } 728c65e6254SWeston Andros Adamson if (dreq->error == 0) { 7291763da12SFred Isaman dreq->count += hdr->good_bytes; 730c65e6254SWeston Andros Adamson if (nfs_write_need_commit(hdr)) { 7311763da12SFred Isaman if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) 732c65e6254SWeston Andros Adamson request_commit = true; 7331763da12SFred Isaman else if (dreq->flags == 0) { 7345002c586SWeston Andros Adamson nfs_direct_set_hdr_verf(dreq, hdr); 735c65e6254SWeston Andros Adamson request_commit = true; 7361763da12SFred Isaman dreq->flags = NFS_ODIRECT_DO_COMMIT; 7371763da12SFred Isaman } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) { 738c65e6254SWeston Andros Adamson request_commit = true; 739c65e6254SWeston Andros Adamson if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr)) 7405002c586SWeston Andros Adamson dreq->flags = 7415002c586SWeston Andros Adamson NFS_ODIRECT_RESCHED_WRITES; 7421763da12SFred Isaman } 7431763da12SFred Isaman } 7441763da12SFred Isaman } 7451763da12SFred Isaman spin_unlock(&dreq->lock); 7461763da12SFred Isaman 7471763da12SFred Isaman while (!list_empty(&hdr->pages)) { 7482bfc6e56SWeston Andros Adamson 7491763da12SFred Isaman req = nfs_list_entry(hdr->pages.next); 7501763da12SFred Isaman nfs_list_remove_request(req); 751c65e6254SWeston Andros Adamson if (request_commit) { 75204277086STrond Myklebust kref_get(&req->wb_kref); 7531763da12SFred Isaman nfs_mark_request_commit(req, hdr->lseg, &cinfo); 7541763da12SFred Isaman } 7551d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 7561763da12SFred Isaman } 7571763da12SFred Isaman 7581763da12SFred Isaman out_put: 7591763da12SFred Isaman if (put_dreq(dreq)) 7601763da12SFred Isaman nfs_direct_write_complete(dreq, hdr->inode); 7611763da12SFred Isaman hdr->release(hdr); 7621763da12SFred Isaman } 7631763da12SFred Isaman 7643e9e0ca3STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head) 7653e9e0ca3STrond Myklebust { 7663e9e0ca3STrond Myklebust struct nfs_page *req; 7673e9e0ca3STrond Myklebust 7683e9e0ca3STrond Myklebust while (!list_empty(head)) { 7693e9e0ca3STrond Myklebust req = nfs_list_entry(head->next); 7703e9e0ca3STrond Myklebust nfs_list_remove_request(req); 7711d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 7723e9e0ca3STrond Myklebust } 7733e9e0ca3STrond Myklebust } 7743e9e0ca3STrond Myklebust 7751763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = { 7763e9e0ca3STrond Myklebust .error_cleanup = nfs_write_sync_pgio_error, 7771763da12SFred Isaman .init_hdr = nfs_direct_pgio_init, 7781763da12SFred Isaman .completion = nfs_direct_write_completion, 7791763da12SFred Isaman }; 7801763da12SFred Isaman 78191f79c43SAl Viro 78291f79c43SAl Viro /* 78391f79c43SAl Viro * NB: Return the value of the first error return code. Subsequent 78491f79c43SAl Viro * errors after the first one are ignored. 78591f79c43SAl Viro */ 78691f79c43SAl Viro /* 78791f79c43SAl Viro * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE 78891f79c43SAl Viro * operation. If nfs_writedata_alloc() or get_user_pages() fails, 78991f79c43SAl Viro * bail and stop sending more writes. Write length accounting is 79091f79c43SAl Viro * handled automatically by nfs_direct_write_result(). Otherwise, if 79191f79c43SAl Viro * no requests have been sent, just return an error. 79291f79c43SAl Viro */ 79319f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq, 794619d30b4SAl Viro struct iov_iter *iter, 79591f79c43SAl Viro loff_t pos) 79619f73787SChuck Lever { 7971763da12SFred Isaman struct nfs_pageio_descriptor desc; 7981d59d61fSTrond Myklebust struct inode *inode = dreq->inode; 79919f73787SChuck Lever ssize_t result = 0; 80019f73787SChuck Lever size_t requested_bytes = 0; 80191f79c43SAl Viro size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE); 80219f73787SChuck Lever 803a20c93e3SChristoph Hellwig nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false, 8041763da12SFred Isaman &nfs_direct_write_completion_ops); 8051763da12SFred Isaman desc.pg_dreq = dreq; 80619f73787SChuck Lever get_dreq(dreq); 8071d59d61fSTrond Myklebust atomic_inc(&inode->i_dio_count); 80819f73787SChuck Lever 80991f79c43SAl Viro NFS_I(inode)->write_io += iov_iter_count(iter); 81091f79c43SAl Viro while (iov_iter_count(iter)) { 81191f79c43SAl Viro struct page **pagevec; 81291f79c43SAl Viro size_t bytes; 81391f79c43SAl Viro size_t pgbase; 81491f79c43SAl Viro unsigned npages, i; 81591f79c43SAl Viro 81691f79c43SAl Viro result = iov_iter_get_pages_alloc(iter, &pagevec, 81791f79c43SAl Viro wsize, &pgbase); 81819f73787SChuck Lever if (result < 0) 81919f73787SChuck Lever break; 82091f79c43SAl Viro 82191f79c43SAl Viro bytes = result; 82291f79c43SAl Viro iov_iter_advance(iter, bytes); 82391f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 82491f79c43SAl Viro for (i = 0; i < npages; i++) { 82591f79c43SAl Viro struct nfs_page *req; 82691f79c43SAl Viro unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 82791f79c43SAl Viro 82816b90578SLinus Torvalds req = nfs_create_request(dreq->ctx, pagevec[i], NULL, 82991f79c43SAl Viro pgbase, req_len); 83091f79c43SAl Viro if (IS_ERR(req)) { 83191f79c43SAl Viro result = PTR_ERR(req); 83219f73787SChuck Lever break; 83391f79c43SAl Viro } 83491f79c43SAl Viro nfs_lock_request(req); 83591f79c43SAl Viro req->wb_index = pos >> PAGE_SHIFT; 83691f79c43SAl Viro req->wb_offset = pos & ~PAGE_MASK; 83791f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 83891f79c43SAl Viro result = desc.pg_error; 83991f79c43SAl Viro nfs_unlock_and_release_request(req); 84091f79c43SAl Viro break; 84191f79c43SAl Viro } 84291f79c43SAl Viro pgbase = 0; 84391f79c43SAl Viro bytes -= req_len; 84491f79c43SAl Viro requested_bytes += req_len; 84591f79c43SAl Viro pos += req_len; 84691f79c43SAl Viro dreq->bytes_left -= req_len; 84791f79c43SAl Viro } 84891f79c43SAl Viro nfs_direct_release_pages(pagevec, npages); 84991f79c43SAl Viro kvfree(pagevec); 85091f79c43SAl Viro if (result < 0) 85191f79c43SAl Viro break; 85219f73787SChuck Lever } 8531763da12SFred Isaman nfs_pageio_complete(&desc); 85419f73787SChuck Lever 855839f7ad6SChuck Lever /* 856839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 857839f7ad6SChuck Lever * generic layer handle the completion. 858839f7ad6SChuck Lever */ 859839f7ad6SChuck Lever if (requested_bytes == 0) { 8601d59d61fSTrond Myklebust inode_dio_done(inode); 861839f7ad6SChuck Lever nfs_direct_req_release(dreq); 862839f7ad6SChuck Lever return result < 0 ? result : -EIO; 863839f7ad6SChuck Lever } 864839f7ad6SChuck Lever 86519f73787SChuck Lever if (put_dreq(dreq)) 86619f73787SChuck Lever nfs_direct_write_complete(dreq, dreq->inode); 86719f73787SChuck Lever return 0; 86819f73787SChuck Lever } 86919f73787SChuck Lever 8701da177e4SLinus Torvalds /** 8711da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 8721da177e4SLinus Torvalds * @iocb: target I/O control block 873619d30b4SAl Viro * @iter: vector of user buffers from which to write data 87488467055SChuck Lever * @pos: byte offset in file where writing starts 8751da177e4SLinus Torvalds * 8761da177e4SLinus Torvalds * We use this function for direct writes instead of calling 8771da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 8781da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 8791da177e4SLinus Torvalds * the new i_size and this client must read the updated size 8801da177e4SLinus Torvalds * back into its cache. We let the server do generic write 8811da177e4SLinus Torvalds * parameter checking and report problems. 8821da177e4SLinus Torvalds * 8831da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 8841da177e4SLinus Torvalds * 8851da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 8861da177e4SLinus Torvalds * readers of this file. 8871da177e4SLinus Torvalds * 8881da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 8891da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 8901da177e4SLinus Torvalds */ 891619d30b4SAl Viro ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter, 892e19a8a0aSMartin K. Petersen loff_t pos) 8931da177e4SLinus Torvalds { 89422cd1bf1SChristoph Hellwig ssize_t result = -EINVAL; 8951da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 8961da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 89722cd1bf1SChristoph Hellwig struct inode *inode = mapping->host; 89822cd1bf1SChristoph Hellwig struct nfs_direct_req *dreq; 89922cd1bf1SChristoph Hellwig struct nfs_lock_context *l_ctx; 900a9ab5e84SChristoph Hellwig loff_t end; 901a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 902a9ab5e84SChristoph Hellwig end = (pos + count - 1) >> PAGE_CACHE_SHIFT; 903a9ab5e84SChristoph Hellwig 904c216fd70SChuck Lever nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); 905c216fd70SChuck Lever 9066de1472fSAl Viro dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n", 9076de1472fSAl Viro file, count, (long long) pos); 908027445c3SBadari Pulavarty 90922cd1bf1SChristoph Hellwig result = generic_write_checks(file, &pos, &count, 0); 91022cd1bf1SChristoph Hellwig if (result) 9111da177e4SLinus Torvalds goto out; 912ce1a8e67SChuck Lever 91322cd1bf1SChristoph Hellwig result = -EINVAL; 914ce1a8e67SChuck Lever if ((ssize_t) count < 0) 9151da177e4SLinus Torvalds goto out; 91622cd1bf1SChristoph Hellwig result = 0; 9171da177e4SLinus Torvalds if (!count) 9181da177e4SLinus Torvalds goto out; 919ce1a8e67SChuck Lever 920a9ab5e84SChristoph Hellwig mutex_lock(&inode->i_mutex); 921a9ab5e84SChristoph Hellwig 92222cd1bf1SChristoph Hellwig result = nfs_sync_mapping(mapping); 92322cd1bf1SChristoph Hellwig if (result) 924a9ab5e84SChristoph Hellwig goto out_unlock; 925a9ab5e84SChristoph Hellwig 926a9ab5e84SChristoph Hellwig if (mapping->nrpages) { 927a9ab5e84SChristoph Hellwig result = invalidate_inode_pages2_range(mapping, 928a9ab5e84SChristoph Hellwig pos >> PAGE_CACHE_SHIFT, end); 929a9ab5e84SChristoph Hellwig if (result) 930a9ab5e84SChristoph Hellwig goto out_unlock; 931a9ab5e84SChristoph Hellwig } 9321da177e4SLinus Torvalds 9337ec10f26SKonstantin Khlebnikov task_io_account_write(count); 9347ec10f26SKonstantin Khlebnikov 93522cd1bf1SChristoph Hellwig result = -ENOMEM; 93622cd1bf1SChristoph Hellwig dreq = nfs_direct_req_alloc(); 93722cd1bf1SChristoph Hellwig if (!dreq) 938a9ab5e84SChristoph Hellwig goto out_unlock; 93922cd1bf1SChristoph Hellwig 94022cd1bf1SChristoph Hellwig dreq->inode = inode; 94122cd1bf1SChristoph Hellwig dreq->bytes_left = count; 94222cd1bf1SChristoph Hellwig dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 94322cd1bf1SChristoph Hellwig l_ctx = nfs_get_lock_context(dreq->ctx); 94422cd1bf1SChristoph Hellwig if (IS_ERR(l_ctx)) { 94522cd1bf1SChristoph Hellwig result = PTR_ERR(l_ctx); 94622cd1bf1SChristoph Hellwig goto out_release; 94722cd1bf1SChristoph Hellwig } 94822cd1bf1SChristoph Hellwig dreq->l_ctx = l_ctx; 94922cd1bf1SChristoph Hellwig if (!is_sync_kiocb(iocb)) 95022cd1bf1SChristoph Hellwig dreq->iocb = iocb; 95122cd1bf1SChristoph Hellwig 95291f79c43SAl Viro result = nfs_direct_write_schedule_iovec(dreq, iter, pos); 953a9ab5e84SChristoph Hellwig 954a9ab5e84SChristoph Hellwig if (mapping->nrpages) { 955a9ab5e84SChristoph Hellwig invalidate_inode_pages2_range(mapping, 956a9ab5e84SChristoph Hellwig pos >> PAGE_CACHE_SHIFT, end); 957a9ab5e84SChristoph Hellwig } 958a9ab5e84SChristoph Hellwig 959a9ab5e84SChristoph Hellwig mutex_unlock(&inode->i_mutex); 960a9ab5e84SChristoph Hellwig 96122cd1bf1SChristoph Hellwig if (!result) { 96222cd1bf1SChristoph Hellwig result = nfs_direct_wait(dreq); 96322cd1bf1SChristoph Hellwig if (result > 0) { 9641763da12SFred Isaman struct inode *inode = mapping->host; 9659eafa8ccSChuck Lever 96622cd1bf1SChristoph Hellwig iocb->ki_pos = pos + result; 9671763da12SFred Isaman spin_lock(&inode->i_lock); 9681763da12SFred Isaman if (i_size_read(inode) < iocb->ki_pos) 9691763da12SFred Isaman i_size_write(inode, iocb->ki_pos); 9701763da12SFred Isaman spin_unlock(&inode->i_lock); 9711763da12SFred Isaman } 97222cd1bf1SChristoph Hellwig } 973a9ab5e84SChristoph Hellwig nfs_direct_req_release(dreq); 974a9ab5e84SChristoph Hellwig return result; 975a9ab5e84SChristoph Hellwig 97622cd1bf1SChristoph Hellwig out_release: 97722cd1bf1SChristoph Hellwig nfs_direct_req_release(dreq); 978a9ab5e84SChristoph Hellwig out_unlock: 979a9ab5e84SChristoph Hellwig mutex_unlock(&inode->i_mutex); 9801da177e4SLinus Torvalds out: 98122cd1bf1SChristoph Hellwig return result; 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds 98488467055SChuck Lever /** 98588467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 98688467055SChuck Lever * 98788467055SChuck Lever */ 988f7b422b1SDavid Howells int __init nfs_init_directcache(void) 9891da177e4SLinus Torvalds { 9901da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 9911da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 992fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 993fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 99420c2df83SPaul Mundt NULL); 9951da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 9961da177e4SLinus Torvalds return -ENOMEM; 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds return 0; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 100188467055SChuck Lever /** 1002f7b422b1SDavid Howells * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures 100388467055SChuck Lever * 100488467055SChuck Lever */ 1005266bee88SDavid Brownell void nfs_destroy_directcache(void) 10061da177e4SLinus Torvalds { 10071a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_direct_cachep); 10081da177e4SLinus Torvalds } 1009