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 */ 690a00b77bSWeston Andros Adamson struct nfs_direct_mirror { 700a00b77bSWeston Andros Adamson ssize_t count; 710a00b77bSWeston Andros Adamson }; 720a00b77bSWeston Andros Adamson 731da177e4SLinus Torvalds struct nfs_direct_req { 741da177e4SLinus Torvalds struct kref kref; /* release manager */ 7515ce4a0cSChuck Lever 7615ce4a0cSChuck Lever /* I/O parameters */ 77a8881f5aSTrond Myklebust struct nfs_open_context *ctx; /* file open context info */ 78f11ac8dbSTrond Myklebust struct nfs_lock_context *l_ctx; /* Lock context info */ 7999514f8fSChuck Lever struct kiocb * iocb; /* controlling i/o request */ 8088467055SChuck Lever struct inode * inode; /* target file of i/o */ 8115ce4a0cSChuck Lever 8215ce4a0cSChuck Lever /* completion state */ 83607f31e8STrond Myklebust atomic_t io_count; /* i/os we're waiting for */ 8415ce4a0cSChuck Lever spinlock_t lock; /* protect completion state */ 850a00b77bSWeston Andros Adamson 860a00b77bSWeston Andros Adamson struct nfs_direct_mirror mirrors[NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX]; 870a00b77bSWeston Andros Adamson int mirror_count; 880a00b77bSWeston Andros Adamson 8915ce4a0cSChuck Lever ssize_t count, /* bytes actually processed */ 9035754bc0SPeng Tao bytes_left, /* bytes left to be sent */ 915fadeb47SPeng Tao io_start, /* start of IO */ 921da177e4SLinus Torvalds error; /* any reported error */ 93d72b7a6bSTrond Myklebust struct completion completion; /* wait for i/o completion */ 94fad61490STrond Myklebust 95fad61490STrond Myklebust /* commit state */ 961763da12SFred Isaman struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */ 971763da12SFred Isaman struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */ 981763da12SFred Isaman struct work_struct work; 99fad61490STrond Myklebust int flags; 100fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 101fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 102fad61490STrond Myklebust struct nfs_writeverf verf; /* unstable write verifier */ 1031da177e4SLinus Torvalds }; 1041da177e4SLinus Torvalds 1051763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops; 1061763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops; 107fad61490STrond Myklebust static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); 1081763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work); 109607f31e8STrond Myklebust 110607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq) 111607f31e8STrond Myklebust { 112607f31e8STrond Myklebust atomic_inc(&dreq->io_count); 113607f31e8STrond Myklebust } 114607f31e8STrond Myklebust 115607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq) 116607f31e8STrond Myklebust { 117607f31e8STrond Myklebust return atomic_dec_and_test(&dreq->io_count); 118607f31e8STrond Myklebust } 119607f31e8STrond Myklebust 120012fa16dSPeng Tao void nfs_direct_set_resched_writes(struct nfs_direct_req *dreq) 121012fa16dSPeng Tao { 122012fa16dSPeng Tao dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 123012fa16dSPeng Tao } 124012fa16dSPeng Tao EXPORT_SYMBOL_GPL(nfs_direct_set_resched_writes); 125012fa16dSPeng Tao 1260a00b77bSWeston Andros Adamson static void 1270a00b77bSWeston Andros Adamson nfs_direct_good_bytes(struct nfs_direct_req *dreq, struct nfs_pgio_header *hdr) 1280a00b77bSWeston Andros Adamson { 1290a00b77bSWeston Andros Adamson int i; 1300a00b77bSWeston Andros Adamson ssize_t count; 1310a00b77bSWeston Andros Adamson 1320a00b77bSWeston Andros Adamson WARN_ON_ONCE(hdr->pgio_mirror_idx >= dreq->mirror_count); 1330a00b77bSWeston Andros Adamson 1345fadeb47SPeng Tao count = dreq->mirrors[hdr->pgio_mirror_idx].count; 1355fadeb47SPeng Tao if (count + dreq->io_start < hdr->io_start + hdr->good_bytes) { 1365fadeb47SPeng Tao count = hdr->io_start + hdr->good_bytes - dreq->io_start; 1375fadeb47SPeng Tao dreq->mirrors[hdr->pgio_mirror_idx].count = count; 1385fadeb47SPeng Tao } 1390a00b77bSWeston Andros Adamson 1400a00b77bSWeston Andros Adamson /* update the dreq->count by finding the minimum agreed count from all 1410a00b77bSWeston Andros Adamson * mirrors */ 1420a00b77bSWeston Andros Adamson count = dreq->mirrors[0].count; 1430a00b77bSWeston Andros Adamson 1440a00b77bSWeston Andros Adamson for (i = 1; i < dreq->mirror_count; i++) 1450a00b77bSWeston Andros Adamson count = min(count, dreq->mirrors[i].count); 1460a00b77bSWeston Andros Adamson 1470a00b77bSWeston Andros Adamson dreq->count = count; 1480a00b77bSWeston Andros Adamson } 1490a00b77bSWeston Andros Adamson 1505002c586SWeston Andros Adamson /* 1515002c586SWeston Andros Adamson * nfs_direct_select_verf - select the right verifier 1525002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 1535002c586SWeston Andros Adamson * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs 1546cccbb6fSWeston Andros Adamson * @commit_idx - commit bucket index for the DS 1555002c586SWeston Andros Adamson * 1565002c586SWeston Andros Adamson * returns the correct verifier to use given the role of the server 1575002c586SWeston Andros Adamson */ 1585002c586SWeston Andros Adamson static struct nfs_writeverf * 1595002c586SWeston Andros Adamson nfs_direct_select_verf(struct nfs_direct_req *dreq, 1605002c586SWeston Andros Adamson struct nfs_client *ds_clp, 1616cccbb6fSWeston Andros Adamson int commit_idx) 1625002c586SWeston Andros Adamson { 1635002c586SWeston Andros Adamson struct nfs_writeverf *verfp = &dreq->verf; 1645002c586SWeston Andros Adamson 1655002c586SWeston Andros Adamson #ifdef CONFIG_NFS_V4_1 1665002c586SWeston Andros Adamson if (ds_clp) { 1675002c586SWeston Andros Adamson /* pNFS is in use, use the DS verf */ 1686cccbb6fSWeston Andros Adamson if (commit_idx >= 0 && commit_idx < dreq->ds_cinfo.nbuckets) 1696cccbb6fSWeston Andros Adamson verfp = &dreq->ds_cinfo.buckets[commit_idx].direct_verf; 1705002c586SWeston Andros Adamson else 1715002c586SWeston Andros Adamson WARN_ON_ONCE(1); 1725002c586SWeston Andros Adamson } 1735002c586SWeston Andros Adamson #endif 1745002c586SWeston Andros Adamson return verfp; 1755002c586SWeston Andros Adamson } 1765002c586SWeston Andros Adamson 1775002c586SWeston Andros Adamson 1785002c586SWeston Andros Adamson /* 1795002c586SWeston Andros Adamson * nfs_direct_set_hdr_verf - set the write/commit verifier 1805002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 1815002c586SWeston Andros Adamson * @hdr - pageio header to validate against previously seen verfs 1825002c586SWeston Andros Adamson * 1835002c586SWeston Andros Adamson * Set the server's (MDS or DS) "seen" verifier 1845002c586SWeston Andros Adamson */ 1855002c586SWeston Andros Adamson static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq, 1865002c586SWeston Andros Adamson struct nfs_pgio_header *hdr) 1875002c586SWeston Andros Adamson { 1885002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 1895002c586SWeston Andros Adamson 1906cccbb6fSWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx); 1915002c586SWeston Andros Adamson WARN_ON_ONCE(verfp->committed >= 0); 1925002c586SWeston Andros Adamson memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf)); 1935002c586SWeston Andros Adamson WARN_ON_ONCE(verfp->committed < 0); 1945002c586SWeston Andros Adamson } 1955002c586SWeston Andros Adamson 1965002c586SWeston Andros Adamson /* 1975002c586SWeston Andros Adamson * nfs_direct_cmp_hdr_verf - compare verifier for pgio header 1985002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 1995002c586SWeston Andros Adamson * @hdr - pageio header to validate against previously seen verf 2005002c586SWeston Andros Adamson * 2015002c586SWeston Andros Adamson * set the server's "seen" verf if not initialized. 2025002c586SWeston Andros Adamson * returns result of comparison between @hdr->verf and the "seen" 2035002c586SWeston Andros Adamson * verf of the server used by @hdr (DS or MDS) 2045002c586SWeston Andros Adamson */ 2055002c586SWeston Andros Adamson static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq, 2065002c586SWeston Andros Adamson struct nfs_pgio_header *hdr) 2075002c586SWeston Andros Adamson { 2085002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 2095002c586SWeston Andros Adamson 2106cccbb6fSWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx); 2115002c586SWeston Andros Adamson if (verfp->committed < 0) { 2125002c586SWeston Andros Adamson nfs_direct_set_hdr_verf(dreq, hdr); 2135002c586SWeston Andros Adamson return 0; 2145002c586SWeston Andros Adamson } 2155002c586SWeston Andros Adamson return memcmp(verfp, &hdr->verf, sizeof(struct nfs_writeverf)); 2165002c586SWeston Andros Adamson } 2175002c586SWeston Andros Adamson 2185002c586SWeston Andros Adamson /* 2195002c586SWeston Andros Adamson * nfs_direct_cmp_commit_data_verf - compare verifier for commit data 2205002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 2215002c586SWeston Andros Adamson * @data - commit data to validate against previously seen verf 2225002c586SWeston Andros Adamson * 2235002c586SWeston Andros Adamson * returns result of comparison between @data->verf and the verf of 2245002c586SWeston Andros Adamson * the server used by @data (DS or MDS) 2255002c586SWeston Andros Adamson */ 2265002c586SWeston Andros Adamson static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq, 2275002c586SWeston Andros Adamson struct nfs_commit_data *data) 2285002c586SWeston Andros Adamson { 2295002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 2305002c586SWeston Andros Adamson 2315002c586SWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, data->ds_clp, 2325002c586SWeston Andros Adamson data->ds_commit_index); 23380c76fe3SWeston Andros Adamson 23480c76fe3SWeston Andros Adamson /* verifier not set so always fail */ 23580c76fe3SWeston Andros Adamson if (verfp->committed < 0) 23680c76fe3SWeston Andros Adamson return 1; 23780c76fe3SWeston Andros Adamson 2385002c586SWeston Andros Adamson return memcmp(verfp, &data->verf, sizeof(struct nfs_writeverf)); 2395002c586SWeston Andros Adamson } 2405002c586SWeston Andros Adamson 2411da177e4SLinus Torvalds /** 242b8a32e2bSChuck Lever * nfs_direct_IO - NFS address space operation for direct I/O 243b8a32e2bSChuck Lever * @rw: direction (read or write) 244b8a32e2bSChuck Lever * @iocb: target I/O control block 245b8a32e2bSChuck Lever * @iov: array of vectors that define I/O buffer 246b8a32e2bSChuck Lever * @pos: offset in file to begin the operation 247b8a32e2bSChuck Lever * @nr_segs: size of iovec array 248b8a32e2bSChuck Lever * 249b8a32e2bSChuck Lever * The presence of this routine in the address space ops vector means 250a564b8f0SMel Gorman * the NFS client supports direct I/O. However, for most direct IO, we 251a564b8f0SMel Gorman * shunt off direct read and write requests before the VFS gets them, 252a564b8f0SMel Gorman * so this method is only ever called for swap. 2531da177e4SLinus Torvalds */ 254d8d3d94bSAl Viro ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter, loff_t pos) 255b8a32e2bSChuck Lever { 256ee8a1a8bSPeng Tao struct inode *inode = iocb->ki_filp->f_mapping->host; 257ee8a1a8bSPeng Tao 258ee8a1a8bSPeng Tao /* we only support swap file calling nfs_direct_IO */ 259ee8a1a8bSPeng Tao if (!IS_SWAPFILE(inode)) 260ee8a1a8bSPeng Tao return 0; 261ee8a1a8bSPeng Tao 262a564b8f0SMel Gorman #ifndef CONFIG_NFS_SWAP 2636de1472fSAl Viro dprintk("NFS: nfs_direct_IO (%pD) off/no(%Ld/%lu) EINVAL\n", 264d8d3d94bSAl Viro iocb->ki_filp, (long long) pos, iter->nr_segs); 265b8a32e2bSChuck Lever 266b8a32e2bSChuck Lever return -EINVAL; 267a564b8f0SMel Gorman #else 26866ee59afSChristoph Hellwig VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE); 269a564b8f0SMel Gorman 270*6f673763SOmar Sandoval if (iov_iter_rw(iter) == READ) 271e19a8a0aSMartin K. Petersen return nfs_file_direct_read(iocb, iter, pos); 272e19a8a0aSMartin K. Petersen return nfs_file_direct_write(iocb, iter, pos); 273a564b8f0SMel Gorman #endif /* CONFIG_NFS_SWAP */ 274b8a32e2bSChuck Lever } 275b8a32e2bSChuck Lever 276749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages) 2779c93ab7dSChuck Lever { 278749e146eSChuck Lever unsigned int i; 279607f31e8STrond Myklebust for (i = 0; i < npages; i++) 280607f31e8STrond Myklebust page_cache_release(pages[i]); 2816b45d858STrond Myklebust } 2826b45d858STrond Myklebust 2831763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo, 2841763da12SFred Isaman struct nfs_direct_req *dreq) 2851763da12SFred Isaman { 286f4086a3dSTrond Myklebust cinfo->lock = &dreq->inode->i_lock; 2871763da12SFred Isaman cinfo->mds = &dreq->mds_cinfo; 2881763da12SFred Isaman cinfo->ds = &dreq->ds_cinfo; 2891763da12SFred Isaman cinfo->dreq = dreq; 2901763da12SFred Isaman cinfo->completion_ops = &nfs_direct_commit_completion_ops; 2911763da12SFred Isaman } 2921763da12SFred Isaman 2930a00b77bSWeston Andros Adamson static inline void nfs_direct_setup_mirroring(struct nfs_direct_req *dreq, 2940a00b77bSWeston Andros Adamson struct nfs_pageio_descriptor *pgio, 2950a00b77bSWeston Andros Adamson struct nfs_page *req) 2960a00b77bSWeston Andros Adamson { 2970a00b77bSWeston Andros Adamson int mirror_count = 1; 2980a00b77bSWeston Andros Adamson 2990a00b77bSWeston Andros Adamson if (pgio->pg_ops->pg_get_mirror_count) 3000a00b77bSWeston Andros Adamson mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req); 3010a00b77bSWeston Andros Adamson 3020a00b77bSWeston Andros Adamson dreq->mirror_count = mirror_count; 3030a00b77bSWeston Andros Adamson } 3040a00b77bSWeston Andros Adamson 30593619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds struct nfs_direct_req *dreq; 3081da177e4SLinus Torvalds 309292f3eeeSTrond Myklebust dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL); 3101da177e4SLinus Torvalds if (!dreq) 3111da177e4SLinus Torvalds return NULL; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds kref_init(&dreq->kref); 314607f31e8STrond Myklebust kref_get(&dreq->kref); 315d72b7a6bSTrond Myklebust init_completion(&dreq->completion); 3161763da12SFred Isaman INIT_LIST_HEAD(&dreq->mds_cinfo.list); 3175002c586SWeston Andros Adamson dreq->verf.committed = NFS_INVALID_STABLE_HOW; /* not set yet */ 3181763da12SFred Isaman INIT_WORK(&dreq->work, nfs_direct_write_schedule_work); 3190a00b77bSWeston Andros Adamson dreq->mirror_count = 1; 32015ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 32193619e59SChuck Lever 32293619e59SChuck Lever return dreq; 32393619e59SChuck Lever } 32493619e59SChuck Lever 325b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref) 3261da177e4SLinus Torvalds { 3271da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 328a8881f5aSTrond Myklebust 3298c393f9aSPeng Tao nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo); 330f11ac8dbSTrond Myklebust if (dreq->l_ctx != NULL) 331f11ac8dbSTrond Myklebust nfs_put_lock_context(dreq->l_ctx); 332a8881f5aSTrond Myklebust if (dreq->ctx != NULL) 333a8881f5aSTrond Myklebust put_nfs_open_context(dreq->ctx); 3341da177e4SLinus Torvalds kmem_cache_free(nfs_direct_cachep, dreq); 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 337b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq) 338b4946ffbSTrond Myklebust { 339b4946ffbSTrond Myklebust kref_put(&dreq->kref, nfs_direct_req_free); 340b4946ffbSTrond Myklebust } 341b4946ffbSTrond Myklebust 3426296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq) 3436296556fSPeng Tao { 3446296556fSPeng Tao return dreq->bytes_left; 3456296556fSPeng Tao } 3466296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left); 3476296556fSPeng Tao 348d4cc948bSChuck Lever /* 349bc0fb201SChuck Lever * Collects and returns the final error value/byte-count. 350bc0fb201SChuck Lever */ 351bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) 352bc0fb201SChuck Lever { 35315ce4a0cSChuck Lever ssize_t result = -EIOCBQUEUED; 354bc0fb201SChuck Lever 355bc0fb201SChuck Lever /* Async requests don't wait here */ 356bc0fb201SChuck Lever if (dreq->iocb) 357bc0fb201SChuck Lever goto out; 358bc0fb201SChuck Lever 359150030b7SMatthew Wilcox result = wait_for_completion_killable(&dreq->completion); 360bc0fb201SChuck Lever 361bc0fb201SChuck Lever if (!result) 36215ce4a0cSChuck Lever result = dreq->error; 363bc0fb201SChuck Lever if (!result) 36415ce4a0cSChuck Lever result = dreq->count; 365bc0fb201SChuck Lever 366bc0fb201SChuck Lever out: 367bc0fb201SChuck Lever return (ssize_t) result; 368bc0fb201SChuck Lever } 369bc0fb201SChuck Lever 370bc0fb201SChuck Lever /* 371607f31e8STrond Myklebust * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust 372607f31e8STrond Myklebust * the iocb is still valid here if this is a synchronous request. 37363ab46abSChuck Lever */ 3749811cd57SChristoph Hellwig static void nfs_direct_complete(struct nfs_direct_req *dreq, bool write) 37563ab46abSChuck Lever { 3769811cd57SChristoph Hellwig struct inode *inode = dreq->inode; 3779811cd57SChristoph Hellwig 3782a009ec9SChristoph Hellwig if (dreq->iocb && write) { 3799811cd57SChristoph Hellwig loff_t pos = dreq->iocb->ki_pos + dreq->count; 3809811cd57SChristoph Hellwig 3819811cd57SChristoph Hellwig spin_lock(&inode->i_lock); 3829811cd57SChristoph Hellwig if (i_size_read(inode) < pos) 3839811cd57SChristoph Hellwig i_size_write(inode, pos); 3849811cd57SChristoph Hellwig spin_unlock(&inode->i_lock); 3859811cd57SChristoph Hellwig } 3869811cd57SChristoph Hellwig 3871f90ee27SChristoph Hellwig if (write) 3882a009ec9SChristoph Hellwig nfs_zap_mapping(inode, inode->i_mapping); 3891f90ee27SChristoph Hellwig 3902a009ec9SChristoph Hellwig inode_dio_done(inode); 3912a009ec9SChristoph Hellwig 3922a009ec9SChristoph Hellwig if (dreq->iocb) { 3932a009ec9SChristoph Hellwig long res = (long) dreq->error; 3942a009ec9SChristoph Hellwig if (!res) 3952a009ec9SChristoph Hellwig res = (long) dreq->count; 39604b2fa9fSChristoph Hellwig dreq->iocb->ki_complete(dreq->iocb, res, 0); 397d72b7a6bSTrond Myklebust } 3982a009ec9SChristoph Hellwig 399d72b7a6bSTrond Myklebust complete_all(&dreq->completion); 40063ab46abSChuck Lever 401b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 40263ab46abSChuck Lever } 40363ab46abSChuck Lever 4041385b811STrond Myklebust static void nfs_direct_readpage_release(struct nfs_page *req) 4051da177e4SLinus Torvalds { 4061e8968c5SNiels de Vos dprintk("NFS: direct read done (%s/%llu %d@%lld)\n", 407584aa810SFred Isaman req->wb_context->dentry->d_inode->i_sb->s_id, 4081e8968c5SNiels de Vos (unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode), 409584aa810SFred Isaman req->wb_bytes, 410584aa810SFred Isaman (long long)req_offset(req)); 411584aa810SFred Isaman nfs_release_request(req); 412fdd1e74cSTrond Myklebust } 413fdd1e74cSTrond Myklebust 414584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr) 415fdd1e74cSTrond Myklebust { 416584aa810SFred Isaman unsigned long bytes = 0; 417584aa810SFred Isaman struct nfs_direct_req *dreq = hdr->dreq; 418fdd1e74cSTrond Myklebust 419584aa810SFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 420584aa810SFred Isaman goto out_put; 4211da177e4SLinus Torvalds 42215ce4a0cSChuck Lever spin_lock(&dreq->lock); 423584aa810SFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0)) 424584aa810SFred Isaman dreq->error = hdr->error; 425584aa810SFred Isaman else 4260a00b77bSWeston Andros Adamson nfs_direct_good_bytes(dreq, hdr); 4270a00b77bSWeston Andros Adamson 42815ce4a0cSChuck Lever spin_unlock(&dreq->lock); 4291da177e4SLinus Torvalds 430584aa810SFred Isaman while (!list_empty(&hdr->pages)) { 431584aa810SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 432584aa810SFred Isaman struct page *page = req->wb_page; 433584aa810SFred Isaman 434be7e9858SJeff Layton if (!PageCompound(page) && bytes < hdr->good_bytes) 4354bd8b010STrond Myklebust set_page_dirty(page); 436584aa810SFred Isaman bytes += req->wb_bytes; 437584aa810SFred Isaman nfs_list_remove_request(req); 438584aa810SFred Isaman nfs_direct_readpage_release(req); 439584aa810SFred Isaman } 440584aa810SFred Isaman out_put: 441607f31e8STrond Myklebust if (put_dreq(dreq)) 4429811cd57SChristoph Hellwig nfs_direct_complete(dreq, false); 443584aa810SFred Isaman hdr->release(hdr); 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4463e9e0ca3STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head) 447cd841605SFred Isaman { 448584aa810SFred Isaman struct nfs_page *req; 449cd841605SFred Isaman 450584aa810SFred Isaman while (!list_empty(head)) { 451584aa810SFred Isaman req = nfs_list_entry(head->next); 452584aa810SFred Isaman nfs_list_remove_request(req); 453584aa810SFred Isaman nfs_release_request(req); 454cd841605SFred Isaman } 455584aa810SFred Isaman } 456584aa810SFred Isaman 457584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr) 458584aa810SFred Isaman { 459584aa810SFred Isaman get_dreq(hdr->dreq); 460584aa810SFred Isaman } 461584aa810SFred Isaman 462584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = { 4633e9e0ca3STrond Myklebust .error_cleanup = nfs_read_sync_pgio_error, 464584aa810SFred Isaman .init_hdr = nfs_direct_pgio_init, 465584aa810SFred Isaman .completion = nfs_direct_read_completion, 466584aa810SFred Isaman }; 467cd841605SFred Isaman 468d4cc948bSChuck Lever /* 469607f31e8STrond Myklebust * For each rsize'd chunk of the user's buffer, dispatch an NFS READ 470607f31e8STrond Myklebust * operation. If nfs_readdata_alloc() or get_user_pages() fails, 471607f31e8STrond Myklebust * bail and stop sending more reads. Read length accounting is 472607f31e8STrond Myklebust * handled automatically by nfs_direct_read_result(). Otherwise, if 473607f31e8STrond Myklebust * no requests have been sent, just return an error. 4741da177e4SLinus Torvalds */ 47591f79c43SAl Viro 47691f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq, 47791f79c43SAl Viro struct iov_iter *iter, 47891f79c43SAl Viro loff_t pos) 4791da177e4SLinus Torvalds { 48091f79c43SAl Viro struct nfs_pageio_descriptor desc; 48191f79c43SAl Viro struct inode *inode = dreq->inode; 48291f79c43SAl Viro ssize_t result = -EINVAL; 48391f79c43SAl Viro size_t requested_bytes = 0; 48491f79c43SAl Viro size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE); 48582b145c5SChuck Lever 48616b90578SLinus Torvalds nfs_pageio_init_read(&desc, dreq->inode, false, 48791f79c43SAl Viro &nfs_direct_read_completion_ops); 48891f79c43SAl Viro get_dreq(dreq); 48991f79c43SAl Viro desc.pg_dreq = dreq; 49091f79c43SAl Viro atomic_inc(&inode->i_dio_count); 49191f79c43SAl Viro 49291f79c43SAl Viro while (iov_iter_count(iter)) { 49391f79c43SAl Viro struct page **pagevec; 4945dd602f2SChuck Lever size_t bytes; 49591f79c43SAl Viro size_t pgbase; 49691f79c43SAl Viro unsigned npages, i; 4971da177e4SLinus Torvalds 49891f79c43SAl Viro result = iov_iter_get_pages_alloc(iter, &pagevec, 49991f79c43SAl Viro rsize, &pgbase); 500584aa810SFred Isaman if (result < 0) 501749e146eSChuck Lever break; 502a564b8f0SMel Gorman 50391f79c43SAl Viro bytes = result; 50491f79c43SAl Viro iov_iter_advance(iter, bytes); 50591f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 506584aa810SFred Isaman for (i = 0; i < npages; i++) { 507584aa810SFred Isaman struct nfs_page *req; 508bf5fc402STrond Myklebust unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 509584aa810SFred Isaman /* XXX do we need to do the eof zeroing found in async_filler? */ 5102bfc6e56SWeston Andros Adamson req = nfs_create_request(dreq->ctx, pagevec[i], NULL, 511584aa810SFred Isaman pgbase, req_len); 512584aa810SFred Isaman if (IS_ERR(req)) { 513584aa810SFred Isaman result = PTR_ERR(req); 514dbae4c73STrond Myklebust break; 515584aa810SFred Isaman } 516584aa810SFred Isaman req->wb_index = pos >> PAGE_SHIFT; 517584aa810SFred Isaman req->wb_offset = pos & ~PAGE_MASK; 51891f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 51991f79c43SAl Viro result = desc.pg_error; 520584aa810SFred Isaman nfs_release_request(req); 521584aa810SFred Isaman break; 522584aa810SFred Isaman } 523584aa810SFred Isaman pgbase = 0; 524584aa810SFred Isaman bytes -= req_len; 52591f79c43SAl Viro requested_bytes += req_len; 526584aa810SFred Isaman pos += req_len; 52735754bc0SPeng Tao dreq->bytes_left -= req_len; 528584aa810SFred Isaman } 5296d74743bSTrond Myklebust nfs_direct_release_pages(pagevec, npages); 53091f79c43SAl Viro kvfree(pagevec); 53119f73787SChuck Lever if (result < 0) 53219f73787SChuck Lever break; 53319f73787SChuck Lever } 53419f73787SChuck Lever 535584aa810SFred Isaman nfs_pageio_complete(&desc); 536584aa810SFred Isaman 537839f7ad6SChuck Lever /* 538839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 539839f7ad6SChuck Lever * generic layer handle the completion. 540839f7ad6SChuck Lever */ 541839f7ad6SChuck Lever if (requested_bytes == 0) { 5421f90ee27SChristoph Hellwig inode_dio_done(inode); 543839f7ad6SChuck Lever nfs_direct_req_release(dreq); 544839f7ad6SChuck Lever return result < 0 ? result : -EIO; 545839f7ad6SChuck Lever } 546839f7ad6SChuck Lever 54719f73787SChuck Lever if (put_dreq(dreq)) 5489811cd57SChristoph Hellwig nfs_direct_complete(dreq, false); 54919f73787SChuck Lever return 0; 55019f73787SChuck Lever } 55119f73787SChuck Lever 55214a3ec79SChristoph Hellwig /** 55314a3ec79SChristoph Hellwig * nfs_file_direct_read - file direct read operation for NFS files 55414a3ec79SChristoph Hellwig * @iocb: target I/O control block 555619d30b4SAl Viro * @iter: vector of user buffers into which to read data 55614a3ec79SChristoph Hellwig * @pos: byte offset in file where reading starts 55714a3ec79SChristoph Hellwig * 55814a3ec79SChristoph Hellwig * We use this function for direct reads instead of calling 55914a3ec79SChristoph Hellwig * generic_file_aio_read() in order to avoid gfar's check to see if 56014a3ec79SChristoph Hellwig * the request starts before the end of the file. For that check 56114a3ec79SChristoph Hellwig * to work, we must generate a GETATTR before each direct read, and 56214a3ec79SChristoph Hellwig * even then there is a window between the GETATTR and the subsequent 56314a3ec79SChristoph Hellwig * READ where the file size could change. Our preference is simply 56414a3ec79SChristoph Hellwig * to do all reads the application wants, and the server will take 56514a3ec79SChristoph Hellwig * care of managing the end of file boundary. 56614a3ec79SChristoph Hellwig * 56714a3ec79SChristoph Hellwig * This function also eliminates unnecessarily updating the file's 56814a3ec79SChristoph Hellwig * atime locally, as the NFS server sets the file's atime, and this 56914a3ec79SChristoph Hellwig * client must read the updated atime from the server back into its 57014a3ec79SChristoph Hellwig * cache. 57114a3ec79SChristoph Hellwig */ 572619d30b4SAl Viro ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter, 573e19a8a0aSMartin K. Petersen loff_t pos) 5741da177e4SLinus Torvalds { 57514a3ec79SChristoph Hellwig struct file *file = iocb->ki_filp; 57614a3ec79SChristoph Hellwig struct address_space *mapping = file->f_mapping; 57714a3ec79SChristoph Hellwig struct inode *inode = mapping->host; 5781da177e4SLinus Torvalds struct nfs_direct_req *dreq; 579b3c54de6STrond Myklebust struct nfs_lock_context *l_ctx; 58014a3ec79SChristoph Hellwig ssize_t result = -EINVAL; 581a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 58214a3ec79SChristoph Hellwig nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); 58314a3ec79SChristoph Hellwig 58414a3ec79SChristoph Hellwig dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n", 58514a3ec79SChristoph Hellwig file, count, (long long) pos); 58614a3ec79SChristoph Hellwig 58714a3ec79SChristoph Hellwig result = 0; 58814a3ec79SChristoph Hellwig if (!count) 58914a3ec79SChristoph Hellwig goto out; 59014a3ec79SChristoph Hellwig 591d0b9875dSChristoph Hellwig mutex_lock(&inode->i_mutex); 59214a3ec79SChristoph Hellwig result = nfs_sync_mapping(mapping); 59314a3ec79SChristoph Hellwig if (result) 594d0b9875dSChristoph Hellwig goto out_unlock; 59514a3ec79SChristoph Hellwig 59614a3ec79SChristoph Hellwig task_io_account_read(count); 59714a3ec79SChristoph Hellwig 59814a3ec79SChristoph Hellwig result = -ENOMEM; 599607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 600f11ac8dbSTrond Myklebust if (dreq == NULL) 601d0b9875dSChristoph Hellwig goto out_unlock; 6021da177e4SLinus Torvalds 60391d5b470SChuck Lever dreq->inode = inode; 604619d30b4SAl Viro dreq->bytes_left = count; 6055fadeb47SPeng Tao dreq->io_start = pos; 606cd3758e3STrond Myklebust dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 607b3c54de6STrond Myklebust l_ctx = nfs_get_lock_context(dreq->ctx); 608b3c54de6STrond Myklebust if (IS_ERR(l_ctx)) { 609b3c54de6STrond Myklebust result = PTR_ERR(l_ctx); 610f11ac8dbSTrond Myklebust goto out_release; 611b3c54de6STrond Myklebust } 612b3c54de6STrond Myklebust dreq->l_ctx = l_ctx; 613487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 614487b8372SChuck Lever dreq->iocb = iocb; 6151da177e4SLinus Torvalds 616619d30b4SAl Viro NFS_I(inode)->read_io += count; 61791f79c43SAl Viro result = nfs_direct_read_schedule_iovec(dreq, iter, pos); 618d0b9875dSChristoph Hellwig 619d0b9875dSChristoph Hellwig mutex_unlock(&inode->i_mutex); 620d0b9875dSChristoph Hellwig 62114a3ec79SChristoph Hellwig if (!result) { 622bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 62314a3ec79SChristoph Hellwig if (result > 0) 62414a3ec79SChristoph Hellwig iocb->ki_pos = pos + result; 62514a3ec79SChristoph Hellwig } 626d0b9875dSChristoph Hellwig 627d0b9875dSChristoph Hellwig nfs_direct_req_release(dreq); 628d0b9875dSChristoph Hellwig return result; 629d0b9875dSChristoph Hellwig 630f11ac8dbSTrond Myklebust out_release: 631b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 632d0b9875dSChristoph Hellwig out_unlock: 633d0b9875dSChristoph Hellwig mutex_unlock(&inode->i_mutex); 634f11ac8dbSTrond Myklebust out: 6351da177e4SLinus Torvalds return result; 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 638085d1e33STom Haynes static void 639085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode, 640085d1e33STom Haynes struct list_head *list, 641085d1e33STom Haynes struct nfs_commit_info *cinfo) 642085d1e33STom Haynes { 643085d1e33STom Haynes spin_lock(cinfo->lock); 644085d1e33STom Haynes #ifdef CONFIG_NFS_V4_1 645085d1e33STom Haynes if (cinfo->ds != NULL && cinfo->ds->nwritten != 0) 646085d1e33STom Haynes NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo); 647085d1e33STom Haynes #endif 648085d1e33STom Haynes nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0); 649085d1e33STom Haynes spin_unlock(cinfo->lock); 650085d1e33STom Haynes } 651085d1e33STom Haynes 652fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 6531da177e4SLinus Torvalds { 6541763da12SFred Isaman struct nfs_pageio_descriptor desc; 6551763da12SFred Isaman struct nfs_page *req, *tmp; 6561763da12SFred Isaman LIST_HEAD(reqs); 6571763da12SFred Isaman struct nfs_commit_info cinfo; 6581763da12SFred Isaman LIST_HEAD(failed); 6590a00b77bSWeston Andros Adamson int i; 6601763da12SFred Isaman 6611763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 662085d1e33STom Haynes nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo); 6631da177e4SLinus Torvalds 664fad61490STrond Myklebust dreq->count = 0; 6650a00b77bSWeston Andros Adamson for (i = 0; i < dreq->mirror_count; i++) 6660a00b77bSWeston Andros Adamson dreq->mirrors[i].count = 0; 667607f31e8STrond Myklebust get_dreq(dreq); 6681da177e4SLinus Torvalds 669a20c93e3SChristoph Hellwig nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false, 6701763da12SFred Isaman &nfs_direct_write_completion_ops); 6711763da12SFred Isaman desc.pg_dreq = dreq; 672607f31e8STrond Myklebust 6730a00b77bSWeston Andros Adamson req = nfs_list_entry(reqs.next); 6740a00b77bSWeston Andros Adamson nfs_direct_setup_mirroring(dreq, &desc, req); 6750a00b77bSWeston Andros Adamson 6761763da12SFred Isaman list_for_each_entry_safe(req, tmp, &reqs, wb_list) { 6771763da12SFred Isaman if (!nfs_pageio_add_request(&desc, req)) { 6784035c248STrond Myklebust nfs_list_remove_request(req); 6791763da12SFred Isaman nfs_list_add_request(req, &failed); 6801763da12SFred Isaman spin_lock(cinfo.lock); 6811763da12SFred Isaman dreq->flags = 0; 6821763da12SFred Isaman dreq->error = -EIO; 6831763da12SFred Isaman spin_unlock(cinfo.lock); 6841763da12SFred Isaman } 6855a695da2STrond Myklebust nfs_release_request(req); 6861763da12SFred Isaman } 6871763da12SFred Isaman nfs_pageio_complete(&desc); 688607f31e8STrond Myklebust 6894035c248STrond Myklebust while (!list_empty(&failed)) { 6904035c248STrond Myklebust req = nfs_list_entry(failed.next); 6914035c248STrond Myklebust nfs_list_remove_request(req); 6921d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 6934035c248STrond Myklebust } 694607f31e8STrond Myklebust 695607f31e8STrond Myklebust if (put_dreq(dreq)) 6961763da12SFred Isaman nfs_direct_write_complete(dreq, dreq->inode); 697fad61490STrond Myklebust } 6981da177e4SLinus Torvalds 6991763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data) 700fad61490STrond Myklebust { 7010b7c0153SFred Isaman struct nfs_direct_req *dreq = data->dreq; 7021763da12SFred Isaman struct nfs_commit_info cinfo; 7031763da12SFred Isaman struct nfs_page *req; 704c9d8f89dSTrond Myklebust int status = data->task.tk_status; 705c9d8f89dSTrond Myklebust 7061763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 707c9d8f89dSTrond Myklebust if (status < 0) { 70860fa3f76STrond Myklebust dprintk("NFS: %5u commit failed with error %d.\n", 709c9d8f89dSTrond Myklebust data->task.tk_pid, status); 710fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 7115002c586SWeston Andros Adamson } else if (nfs_direct_cmp_commit_data_verf(dreq, data)) { 712c9d8f89dSTrond Myklebust dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid); 713fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 714fad61490STrond Myklebust } 715fad61490STrond Myklebust 716c9d8f89dSTrond Myklebust dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status); 7171763da12SFred Isaman while (!list_empty(&data->pages)) { 7181763da12SFred Isaman req = nfs_list_entry(data->pages.next); 7191763da12SFred Isaman nfs_list_remove_request(req); 7201763da12SFred Isaman if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) { 7211763da12SFred Isaman /* Note the rewrite will go through mds */ 722b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, NULL, &cinfo, 0); 723906369e4SFred Isaman } else 724906369e4SFred Isaman nfs_release_request(req); 7251d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 726fad61490STrond Myklebust } 727fad61490STrond Myklebust 7281763da12SFred Isaman if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) 7291763da12SFred Isaman nfs_direct_write_complete(dreq, data->inode); 7301763da12SFred Isaman } 7311763da12SFred Isaman 7321763da12SFred Isaman static void nfs_direct_error_cleanup(struct nfs_inode *nfsi) 7331763da12SFred Isaman { 7341763da12SFred Isaman /* There is no lock to clear */ 7351763da12SFred Isaman } 7361763da12SFred Isaman 7371763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = { 7381763da12SFred Isaman .completion = nfs_direct_commit_complete, 7391763da12SFred Isaman .error_cleanup = nfs_direct_error_cleanup, 740fad61490STrond Myklebust }; 741fad61490STrond Myklebust 742fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 743fad61490STrond Myklebust { 7441763da12SFred Isaman int res; 7451763da12SFred Isaman struct nfs_commit_info cinfo; 7461763da12SFred Isaman LIST_HEAD(mds_list); 747fad61490STrond Myklebust 7481763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 7491763da12SFred Isaman nfs_scan_commit(dreq->inode, &mds_list, &cinfo); 7501763da12SFred Isaman res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo); 7511763da12SFred Isaman if (res < 0) /* res == -ENOMEM */ 7521763da12SFred Isaman nfs_direct_write_reschedule(dreq); 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 7551763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work) 7561da177e4SLinus Torvalds { 7571763da12SFred Isaman struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work); 758fad61490STrond Myklebust int flags = dreq->flags; 7591da177e4SLinus Torvalds 760fad61490STrond Myklebust dreq->flags = 0; 761fad61490STrond Myklebust switch (flags) { 762fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 763fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 7641da177e4SLinus Torvalds break; 765fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 766fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 7671da177e4SLinus Torvalds break; 7681da177e4SLinus Torvalds default: 7699811cd57SChristoph Hellwig nfs_direct_complete(dreq, true); 7701da177e4SLinus Torvalds } 771fad61490STrond Myklebust } 772fad61490STrond Myklebust 7731763da12SFred Isaman static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) 774fad61490STrond Myklebust { 7751763da12SFred Isaman schedule_work(&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 7921763da12SFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) { 7931763da12SFred Isaman dreq->flags = 0; 7941763da12SFred Isaman dreq->error = hdr->error; 7951763da12SFred Isaman } 796c65e6254SWeston Andros Adamson if (dreq->error == 0) { 7970a00b77bSWeston Andros Adamson nfs_direct_good_bytes(dreq, hdr); 798c65e6254SWeston Andros Adamson if (nfs_write_need_commit(hdr)) { 7991763da12SFred Isaman if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) 800c65e6254SWeston Andros Adamson request_commit = true; 8011763da12SFred Isaman else if (dreq->flags == 0) { 8025002c586SWeston Andros Adamson nfs_direct_set_hdr_verf(dreq, hdr); 803c65e6254SWeston Andros Adamson request_commit = true; 8041763da12SFred Isaman dreq->flags = NFS_ODIRECT_DO_COMMIT; 8051763da12SFred Isaman } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) { 806c65e6254SWeston Andros Adamson request_commit = true; 807c65e6254SWeston Andros Adamson if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr)) 8085002c586SWeston Andros Adamson dreq->flags = 8095002c586SWeston Andros Adamson NFS_ODIRECT_RESCHED_WRITES; 8101763da12SFred Isaman } 8111763da12SFred Isaman } 8121763da12SFred Isaman } 8131763da12SFred Isaman spin_unlock(&dreq->lock); 8141763da12SFred Isaman 8151763da12SFred Isaman while (!list_empty(&hdr->pages)) { 8162bfc6e56SWeston Andros Adamson 8171763da12SFred Isaman req = nfs_list_entry(hdr->pages.next); 8181763da12SFred Isaman nfs_list_remove_request(req); 819c65e6254SWeston Andros Adamson if (request_commit) { 82004277086STrond Myklebust kref_get(&req->wb_kref); 821b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, hdr->lseg, &cinfo, 822b57ff130SWeston Andros Adamson hdr->ds_commit_idx); 8231763da12SFred Isaman } 8241d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 8251763da12SFred Isaman } 8261763da12SFred Isaman 8271763da12SFred Isaman out_put: 8281763da12SFred Isaman if (put_dreq(dreq)) 8291763da12SFred Isaman nfs_direct_write_complete(dreq, hdr->inode); 8301763da12SFred Isaman hdr->release(hdr); 8311763da12SFred Isaman } 8321763da12SFred Isaman 8333e9e0ca3STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head) 8343e9e0ca3STrond Myklebust { 8353e9e0ca3STrond Myklebust struct nfs_page *req; 8363e9e0ca3STrond Myklebust 8373e9e0ca3STrond Myklebust while (!list_empty(head)) { 8383e9e0ca3STrond Myklebust req = nfs_list_entry(head->next); 8393e9e0ca3STrond Myklebust nfs_list_remove_request(req); 8401d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 8413e9e0ca3STrond Myklebust } 8423e9e0ca3STrond Myklebust } 8433e9e0ca3STrond Myklebust 8441763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = { 8453e9e0ca3STrond Myklebust .error_cleanup = nfs_write_sync_pgio_error, 8461763da12SFred Isaman .init_hdr = nfs_direct_pgio_init, 8471763da12SFred Isaman .completion = nfs_direct_write_completion, 8481763da12SFred Isaman }; 8491763da12SFred Isaman 85091f79c43SAl Viro 85191f79c43SAl Viro /* 85291f79c43SAl Viro * NB: Return the value of the first error return code. Subsequent 85391f79c43SAl Viro * errors after the first one are ignored. 85491f79c43SAl Viro */ 85591f79c43SAl Viro /* 85691f79c43SAl Viro * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE 85791f79c43SAl Viro * operation. If nfs_writedata_alloc() or get_user_pages() fails, 85891f79c43SAl Viro * bail and stop sending more writes. Write length accounting is 85991f79c43SAl Viro * handled automatically by nfs_direct_write_result(). Otherwise, if 86091f79c43SAl Viro * no requests have been sent, just return an error. 86191f79c43SAl Viro */ 86219f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq, 863619d30b4SAl Viro struct iov_iter *iter, 86491f79c43SAl Viro loff_t pos) 86519f73787SChuck Lever { 8661763da12SFred Isaman struct nfs_pageio_descriptor desc; 8671d59d61fSTrond Myklebust struct inode *inode = dreq->inode; 86819f73787SChuck Lever ssize_t result = 0; 86919f73787SChuck Lever size_t requested_bytes = 0; 87091f79c43SAl Viro size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE); 87119f73787SChuck Lever 872a20c93e3SChristoph Hellwig nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false, 8731763da12SFred Isaman &nfs_direct_write_completion_ops); 8741763da12SFred Isaman desc.pg_dreq = dreq; 87519f73787SChuck Lever get_dreq(dreq); 8761d59d61fSTrond Myklebust atomic_inc(&inode->i_dio_count); 87719f73787SChuck Lever 87891f79c43SAl Viro NFS_I(inode)->write_io += iov_iter_count(iter); 87991f79c43SAl Viro while (iov_iter_count(iter)) { 88091f79c43SAl Viro struct page **pagevec; 88191f79c43SAl Viro size_t bytes; 88291f79c43SAl Viro size_t pgbase; 88391f79c43SAl Viro unsigned npages, i; 88491f79c43SAl Viro 88591f79c43SAl Viro result = iov_iter_get_pages_alloc(iter, &pagevec, 88691f79c43SAl Viro wsize, &pgbase); 88719f73787SChuck Lever if (result < 0) 88819f73787SChuck Lever break; 88991f79c43SAl Viro 89091f79c43SAl Viro bytes = result; 89191f79c43SAl Viro iov_iter_advance(iter, bytes); 89291f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 89391f79c43SAl Viro for (i = 0; i < npages; i++) { 89491f79c43SAl Viro struct nfs_page *req; 89591f79c43SAl Viro unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 89691f79c43SAl Viro 89716b90578SLinus Torvalds req = nfs_create_request(dreq->ctx, pagevec[i], NULL, 89891f79c43SAl Viro pgbase, req_len); 89991f79c43SAl Viro if (IS_ERR(req)) { 90091f79c43SAl Viro result = PTR_ERR(req); 90119f73787SChuck Lever break; 90291f79c43SAl Viro } 9030a00b77bSWeston Andros Adamson 9040a00b77bSWeston Andros Adamson nfs_direct_setup_mirroring(dreq, &desc, req); 9050a00b77bSWeston Andros Adamson 90691f79c43SAl Viro nfs_lock_request(req); 90791f79c43SAl Viro req->wb_index = pos >> PAGE_SHIFT; 90891f79c43SAl Viro req->wb_offset = pos & ~PAGE_MASK; 90991f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 91091f79c43SAl Viro result = desc.pg_error; 91191f79c43SAl Viro nfs_unlock_and_release_request(req); 91291f79c43SAl Viro break; 91391f79c43SAl Viro } 91491f79c43SAl Viro pgbase = 0; 91591f79c43SAl Viro bytes -= req_len; 91691f79c43SAl Viro requested_bytes += req_len; 91791f79c43SAl Viro pos += req_len; 91891f79c43SAl Viro dreq->bytes_left -= req_len; 91991f79c43SAl Viro } 92091f79c43SAl Viro nfs_direct_release_pages(pagevec, npages); 92191f79c43SAl Viro kvfree(pagevec); 92291f79c43SAl Viro if (result < 0) 92391f79c43SAl Viro break; 92419f73787SChuck Lever } 9251763da12SFred Isaman nfs_pageio_complete(&desc); 92619f73787SChuck Lever 927839f7ad6SChuck Lever /* 928839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 929839f7ad6SChuck Lever * generic layer handle the completion. 930839f7ad6SChuck Lever */ 931839f7ad6SChuck Lever if (requested_bytes == 0) { 9321d59d61fSTrond Myklebust inode_dio_done(inode); 933839f7ad6SChuck Lever nfs_direct_req_release(dreq); 934839f7ad6SChuck Lever return result < 0 ? result : -EIO; 935839f7ad6SChuck Lever } 936839f7ad6SChuck Lever 93719f73787SChuck Lever if (put_dreq(dreq)) 93819f73787SChuck Lever nfs_direct_write_complete(dreq, dreq->inode); 93919f73787SChuck Lever return 0; 94019f73787SChuck Lever } 94119f73787SChuck Lever 9421da177e4SLinus Torvalds /** 9431da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 9441da177e4SLinus Torvalds * @iocb: target I/O control block 945619d30b4SAl Viro * @iter: vector of user buffers from which to write data 94688467055SChuck Lever * @pos: byte offset in file where writing starts 9471da177e4SLinus Torvalds * 9481da177e4SLinus Torvalds * We use this function for direct writes instead of calling 9491da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 9501da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 9511da177e4SLinus Torvalds * the new i_size and this client must read the updated size 9521da177e4SLinus Torvalds * back into its cache. We let the server do generic write 9531da177e4SLinus Torvalds * parameter checking and report problems. 9541da177e4SLinus Torvalds * 9551da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 9561da177e4SLinus Torvalds * 9571da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 9581da177e4SLinus Torvalds * readers of this file. 9591da177e4SLinus Torvalds * 9601da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 9611da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 9621da177e4SLinus Torvalds */ 963619d30b4SAl Viro ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter, 964e19a8a0aSMartin K. Petersen loff_t pos) 9651da177e4SLinus Torvalds { 96622cd1bf1SChristoph Hellwig ssize_t result = -EINVAL; 9671da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 9681da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 96922cd1bf1SChristoph Hellwig struct inode *inode = mapping->host; 97022cd1bf1SChristoph Hellwig struct nfs_direct_req *dreq; 97122cd1bf1SChristoph Hellwig struct nfs_lock_context *l_ctx; 972a9ab5e84SChristoph Hellwig loff_t end; 973a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 974a9ab5e84SChristoph Hellwig end = (pos + count - 1) >> PAGE_CACHE_SHIFT; 975a9ab5e84SChristoph Hellwig 976c216fd70SChuck Lever nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); 977c216fd70SChuck Lever 9786de1472fSAl Viro dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n", 9796de1472fSAl Viro file, count, (long long) pos); 980027445c3SBadari Pulavarty 98122cd1bf1SChristoph Hellwig result = generic_write_checks(file, &pos, &count, 0); 98222cd1bf1SChristoph Hellwig if (result) 9831da177e4SLinus Torvalds goto out; 984ce1a8e67SChuck Lever 98522cd1bf1SChristoph Hellwig result = -EINVAL; 986ce1a8e67SChuck Lever if ((ssize_t) count < 0) 9871da177e4SLinus Torvalds goto out; 98822cd1bf1SChristoph Hellwig result = 0; 9891da177e4SLinus Torvalds if (!count) 9901da177e4SLinus Torvalds goto out; 991ce1a8e67SChuck Lever 992a9ab5e84SChristoph Hellwig mutex_lock(&inode->i_mutex); 993a9ab5e84SChristoph Hellwig 99422cd1bf1SChristoph Hellwig result = nfs_sync_mapping(mapping); 99522cd1bf1SChristoph Hellwig if (result) 996a9ab5e84SChristoph Hellwig goto out_unlock; 997a9ab5e84SChristoph Hellwig 998a9ab5e84SChristoph Hellwig if (mapping->nrpages) { 999a9ab5e84SChristoph Hellwig result = invalidate_inode_pages2_range(mapping, 1000a9ab5e84SChristoph Hellwig pos >> PAGE_CACHE_SHIFT, end); 1001a9ab5e84SChristoph Hellwig if (result) 1002a9ab5e84SChristoph Hellwig goto out_unlock; 1003a9ab5e84SChristoph Hellwig } 10041da177e4SLinus Torvalds 10057ec10f26SKonstantin Khlebnikov task_io_account_write(count); 10067ec10f26SKonstantin Khlebnikov 100722cd1bf1SChristoph Hellwig result = -ENOMEM; 100822cd1bf1SChristoph Hellwig dreq = nfs_direct_req_alloc(); 100922cd1bf1SChristoph Hellwig if (!dreq) 1010a9ab5e84SChristoph Hellwig goto out_unlock; 101122cd1bf1SChristoph Hellwig 101222cd1bf1SChristoph Hellwig dreq->inode = inode; 101322cd1bf1SChristoph Hellwig dreq->bytes_left = count; 10145fadeb47SPeng Tao dreq->io_start = pos; 101522cd1bf1SChristoph Hellwig dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 101622cd1bf1SChristoph Hellwig l_ctx = nfs_get_lock_context(dreq->ctx); 101722cd1bf1SChristoph Hellwig if (IS_ERR(l_ctx)) { 101822cd1bf1SChristoph Hellwig result = PTR_ERR(l_ctx); 101922cd1bf1SChristoph Hellwig goto out_release; 102022cd1bf1SChristoph Hellwig } 102122cd1bf1SChristoph Hellwig dreq->l_ctx = l_ctx; 102222cd1bf1SChristoph Hellwig if (!is_sync_kiocb(iocb)) 102322cd1bf1SChristoph Hellwig dreq->iocb = iocb; 102422cd1bf1SChristoph Hellwig 102591f79c43SAl Viro result = nfs_direct_write_schedule_iovec(dreq, iter, pos); 1026a9ab5e84SChristoph Hellwig 1027a9ab5e84SChristoph Hellwig if (mapping->nrpages) { 1028a9ab5e84SChristoph Hellwig invalidate_inode_pages2_range(mapping, 1029a9ab5e84SChristoph Hellwig pos >> PAGE_CACHE_SHIFT, end); 1030a9ab5e84SChristoph Hellwig } 1031a9ab5e84SChristoph Hellwig 1032a9ab5e84SChristoph Hellwig mutex_unlock(&inode->i_mutex); 1033a9ab5e84SChristoph Hellwig 103422cd1bf1SChristoph Hellwig if (!result) { 103522cd1bf1SChristoph Hellwig result = nfs_direct_wait(dreq); 103622cd1bf1SChristoph Hellwig if (result > 0) { 10371763da12SFred Isaman struct inode *inode = mapping->host; 10389eafa8ccSChuck Lever 103922cd1bf1SChristoph Hellwig iocb->ki_pos = pos + result; 10401763da12SFred Isaman spin_lock(&inode->i_lock); 10411763da12SFred Isaman if (i_size_read(inode) < iocb->ki_pos) 10421763da12SFred Isaman i_size_write(inode, iocb->ki_pos); 10431763da12SFred Isaman spin_unlock(&inode->i_lock); 10441763da12SFred Isaman } 104522cd1bf1SChristoph Hellwig } 1046a9ab5e84SChristoph Hellwig nfs_direct_req_release(dreq); 1047a9ab5e84SChristoph Hellwig return result; 1048a9ab5e84SChristoph Hellwig 104922cd1bf1SChristoph Hellwig out_release: 105022cd1bf1SChristoph Hellwig nfs_direct_req_release(dreq); 1051a9ab5e84SChristoph Hellwig out_unlock: 1052a9ab5e84SChristoph Hellwig mutex_unlock(&inode->i_mutex); 10531da177e4SLinus Torvalds out: 105422cd1bf1SChristoph Hellwig return result; 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 105788467055SChuck Lever /** 105888467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 105988467055SChuck Lever * 106088467055SChuck Lever */ 1061f7b422b1SDavid Howells int __init nfs_init_directcache(void) 10621da177e4SLinus Torvalds { 10631da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 10641da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 1065fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 1066fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 106720c2df83SPaul Mundt NULL); 10681da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 10691da177e4SLinus Torvalds return -ENOMEM; 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds return 0; 10721da177e4SLinus Torvalds } 10731da177e4SLinus Torvalds 107488467055SChuck Lever /** 1075f7b422b1SDavid Howells * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures 107688467055SChuck Lever * 107788467055SChuck Lever */ 1078266bee88SDavid Brownell void nfs_destroy_directcache(void) 10791da177e4SLinus Torvalds { 10801a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_direct_cachep); 10811da177e4SLinus Torvalds } 1082