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 557c0f6ba6SLinus Torvalds #include <linux/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 89d9ee6553STrond Myklebust loff_t io_start; /* Start offset for I/O */ 9015ce4a0cSChuck Lever ssize_t count, /* bytes actually processed */ 91ed3743a6SWeston Andros Adamson max_count, /* max expected count */ 9235754bc0SPeng Tao bytes_left, /* bytes left to be sent */ 931da177e4SLinus Torvalds error; /* any reported error */ 94d72b7a6bSTrond Myklebust struct completion completion; /* wait for i/o completion */ 95fad61490STrond Myklebust 96fad61490STrond Myklebust /* commit state */ 971763da12SFred Isaman struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */ 981763da12SFred Isaman struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */ 991763da12SFred Isaman struct work_struct work; 100fad61490STrond Myklebust int flags; 101ad3cba22SDave Kleikamp /* for write */ 102fad61490STrond Myklebust #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */ 103fad61490STrond Myklebust #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */ 104ad3cba22SDave Kleikamp /* for read */ 105ad3cba22SDave Kleikamp #define NFS_ODIRECT_SHOULD_DIRTY (3) /* dirty user-space page after read */ 106fad61490STrond Myklebust struct nfs_writeverf verf; /* unstable write verifier */ 1071da177e4SLinus Torvalds }; 1081da177e4SLinus Torvalds 1091763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops; 1101763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops; 1114d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq); 1121763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work); 113607f31e8STrond Myklebust 114607f31e8STrond Myklebust static inline void get_dreq(struct nfs_direct_req *dreq) 115607f31e8STrond Myklebust { 116607f31e8STrond Myklebust atomic_inc(&dreq->io_count); 117607f31e8STrond Myklebust } 118607f31e8STrond Myklebust 119607f31e8STrond Myklebust static inline int put_dreq(struct nfs_direct_req *dreq) 120607f31e8STrond Myklebust { 121607f31e8STrond Myklebust return atomic_dec_and_test(&dreq->io_count); 122607f31e8STrond Myklebust } 123607f31e8STrond Myklebust 1240a00b77bSWeston Andros Adamson static void 1250a00b77bSWeston Andros Adamson nfs_direct_good_bytes(struct nfs_direct_req *dreq, struct nfs_pgio_header *hdr) 1260a00b77bSWeston Andros Adamson { 1270a00b77bSWeston Andros Adamson int i; 1280a00b77bSWeston Andros Adamson ssize_t count; 1290a00b77bSWeston Andros Adamson 130ed3743a6SWeston Andros Adamson WARN_ON_ONCE(dreq->count >= dreq->max_count); 131ed3743a6SWeston Andros Adamson 1321ccbad9fSPeng Tao if (dreq->mirror_count == 1) { 1331ccbad9fSPeng Tao dreq->mirrors[hdr->pgio_mirror_idx].count += hdr->good_bytes; 1341ccbad9fSPeng Tao dreq->count += hdr->good_bytes; 1351ccbad9fSPeng Tao } else { 1361ccbad9fSPeng Tao /* mirrored writes */ 1375fadeb47SPeng Tao count = dreq->mirrors[hdr->pgio_mirror_idx].count; 1385fadeb47SPeng Tao if (count + dreq->io_start < hdr->io_start + hdr->good_bytes) { 1395fadeb47SPeng Tao count = hdr->io_start + hdr->good_bytes - dreq->io_start; 1405fadeb47SPeng Tao dreq->mirrors[hdr->pgio_mirror_idx].count = count; 1415fadeb47SPeng Tao } 1420a00b77bSWeston Andros Adamson /* update the dreq->count by finding the minimum agreed count from all 1430a00b77bSWeston Andros Adamson * mirrors */ 1440a00b77bSWeston Andros Adamson count = dreq->mirrors[0].count; 1450a00b77bSWeston Andros Adamson 1460a00b77bSWeston Andros Adamson for (i = 1; i < dreq->mirror_count; i++) 1470a00b77bSWeston Andros Adamson count = min(count, dreq->mirrors[i].count); 1480a00b77bSWeston Andros Adamson 1490a00b77bSWeston Andros Adamson dreq->count = count; 1500a00b77bSWeston Andros Adamson } 1511ccbad9fSPeng Tao } 1520a00b77bSWeston Andros Adamson 1535002c586SWeston Andros Adamson /* 1545002c586SWeston Andros Adamson * nfs_direct_select_verf - select the right verifier 1555002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 1565002c586SWeston Andros Adamson * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs 1576cccbb6fSWeston Andros Adamson * @commit_idx - commit bucket index for the DS 1585002c586SWeston Andros Adamson * 1595002c586SWeston Andros Adamson * returns the correct verifier to use given the role of the server 1605002c586SWeston Andros Adamson */ 1615002c586SWeston Andros Adamson static struct nfs_writeverf * 1625002c586SWeston Andros Adamson nfs_direct_select_verf(struct nfs_direct_req *dreq, 1635002c586SWeston Andros Adamson struct nfs_client *ds_clp, 1646cccbb6fSWeston Andros Adamson int commit_idx) 1655002c586SWeston Andros Adamson { 1665002c586SWeston Andros Adamson struct nfs_writeverf *verfp = &dreq->verf; 1675002c586SWeston Andros Adamson 1685002c586SWeston Andros Adamson #ifdef CONFIG_NFS_V4_1 169834e465bSKinglong Mee /* 170834e465bSKinglong Mee * pNFS is in use, use the DS verf except commit_through_mds is set 171834e465bSKinglong Mee * for layout segment where nbuckets is zero. 172834e465bSKinglong Mee */ 173834e465bSKinglong Mee if (ds_clp && dreq->ds_cinfo.nbuckets > 0) { 1746cccbb6fSWeston Andros Adamson if (commit_idx >= 0 && commit_idx < dreq->ds_cinfo.nbuckets) 1756cccbb6fSWeston Andros Adamson verfp = &dreq->ds_cinfo.buckets[commit_idx].direct_verf; 1765002c586SWeston Andros Adamson else 1775002c586SWeston Andros Adamson WARN_ON_ONCE(1); 1785002c586SWeston Andros Adamson } 1795002c586SWeston Andros Adamson #endif 1805002c586SWeston Andros Adamson return verfp; 1815002c586SWeston Andros Adamson } 1825002c586SWeston Andros Adamson 1835002c586SWeston Andros Adamson 1845002c586SWeston Andros Adamson /* 1855002c586SWeston Andros Adamson * nfs_direct_set_hdr_verf - set the write/commit verifier 1865002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 1875002c586SWeston Andros Adamson * @hdr - pageio header to validate against previously seen verfs 1885002c586SWeston Andros Adamson * 1895002c586SWeston Andros Adamson * Set the server's (MDS or DS) "seen" verifier 1905002c586SWeston Andros Adamson */ 1915002c586SWeston Andros Adamson static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq, 1925002c586SWeston Andros Adamson struct nfs_pgio_header *hdr) 1935002c586SWeston Andros Adamson { 1945002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 1955002c586SWeston Andros Adamson 1966cccbb6fSWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx); 1975002c586SWeston Andros Adamson WARN_ON_ONCE(verfp->committed >= 0); 1985002c586SWeston Andros Adamson memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf)); 1995002c586SWeston Andros Adamson WARN_ON_ONCE(verfp->committed < 0); 2005002c586SWeston Andros Adamson } 2015002c586SWeston Andros Adamson 2028fc3c386STrond Myklebust static int nfs_direct_cmp_verf(const struct nfs_writeverf *v1, 2038fc3c386STrond Myklebust const struct nfs_writeverf *v2) 2048fc3c386STrond Myklebust { 2058fc3c386STrond Myklebust return nfs_write_verifier_cmp(&v1->verifier, &v2->verifier); 2068fc3c386STrond Myklebust } 2078fc3c386STrond Myklebust 2085002c586SWeston Andros Adamson /* 2095002c586SWeston Andros Adamson * nfs_direct_cmp_hdr_verf - compare verifier for pgio header 2105002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 2115002c586SWeston Andros Adamson * @hdr - pageio header to validate against previously seen verf 2125002c586SWeston Andros Adamson * 2135002c586SWeston Andros Adamson * set the server's "seen" verf if not initialized. 2145002c586SWeston Andros Adamson * returns result of comparison between @hdr->verf and the "seen" 2155002c586SWeston Andros Adamson * verf of the server used by @hdr (DS or MDS) 2165002c586SWeston Andros Adamson */ 2175002c586SWeston Andros Adamson static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq, 2185002c586SWeston Andros Adamson struct nfs_pgio_header *hdr) 2195002c586SWeston Andros Adamson { 2205002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 2215002c586SWeston Andros Adamson 2226cccbb6fSWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx); 2235002c586SWeston Andros Adamson if (verfp->committed < 0) { 2245002c586SWeston Andros Adamson nfs_direct_set_hdr_verf(dreq, hdr); 2255002c586SWeston Andros Adamson return 0; 2265002c586SWeston Andros Adamson } 2278fc3c386STrond Myklebust return nfs_direct_cmp_verf(verfp, &hdr->verf); 2285002c586SWeston Andros Adamson } 2295002c586SWeston Andros Adamson 2305002c586SWeston Andros Adamson /* 2315002c586SWeston Andros Adamson * nfs_direct_cmp_commit_data_verf - compare verifier for commit data 2325002c586SWeston Andros Adamson * @dreq - direct request possibly spanning multiple servers 2335002c586SWeston Andros Adamson * @data - commit data to validate against previously seen verf 2345002c586SWeston Andros Adamson * 2355002c586SWeston Andros Adamson * returns result of comparison between @data->verf and the verf of 2365002c586SWeston Andros Adamson * the server used by @data (DS or MDS) 2375002c586SWeston Andros Adamson */ 2385002c586SWeston Andros Adamson static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq, 2395002c586SWeston Andros Adamson struct nfs_commit_data *data) 2405002c586SWeston Andros Adamson { 2415002c586SWeston Andros Adamson struct nfs_writeverf *verfp; 2425002c586SWeston Andros Adamson 2435002c586SWeston Andros Adamson verfp = nfs_direct_select_verf(dreq, data->ds_clp, 2445002c586SWeston Andros Adamson data->ds_commit_index); 24580c76fe3SWeston Andros Adamson 24680c76fe3SWeston Andros Adamson /* verifier not set so always fail */ 24780c76fe3SWeston Andros Adamson if (verfp->committed < 0) 24880c76fe3SWeston Andros Adamson return 1; 24980c76fe3SWeston Andros Adamson 2508fc3c386STrond Myklebust return nfs_direct_cmp_verf(verfp, &data->verf); 2515002c586SWeston Andros Adamson } 2525002c586SWeston Andros Adamson 2531da177e4SLinus Torvalds /** 254b8a32e2bSChuck Lever * nfs_direct_IO - NFS address space operation for direct I/O 255b8a32e2bSChuck Lever * @iocb: target I/O control block 25690090ae6SAl Viro * @iter: I/O buffer 257b8a32e2bSChuck Lever * 258b8a32e2bSChuck Lever * The presence of this routine in the address space ops vector means 259a564b8f0SMel Gorman * the NFS client supports direct I/O. However, for most direct IO, we 260a564b8f0SMel Gorman * shunt off direct read and write requests before the VFS gets them, 261a564b8f0SMel Gorman * so this method is only ever called for swap. 2621da177e4SLinus Torvalds */ 263c8b8e32dSChristoph Hellwig ssize_t nfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 264b8a32e2bSChuck Lever { 265ee8a1a8bSPeng Tao struct inode *inode = iocb->ki_filp->f_mapping->host; 266ee8a1a8bSPeng Tao 267ee8a1a8bSPeng Tao /* we only support swap file calling nfs_direct_IO */ 268ee8a1a8bSPeng Tao if (!IS_SWAPFILE(inode)) 269ee8a1a8bSPeng Tao return 0; 270ee8a1a8bSPeng Tao 27166ee59afSChristoph Hellwig VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE); 272a564b8f0SMel Gorman 2736f673763SOmar Sandoval if (iov_iter_rw(iter) == READ) 274c8b8e32dSChristoph Hellwig return nfs_file_direct_read(iocb, iter); 27565a4a1caSAl Viro return nfs_file_direct_write(iocb, iter); 276b8a32e2bSChuck Lever } 277b8a32e2bSChuck Lever 278749e146eSChuck Lever static void nfs_direct_release_pages(struct page **pages, unsigned int npages) 2799c93ab7dSChuck Lever { 280749e146eSChuck Lever unsigned int i; 281607f31e8STrond Myklebust for (i = 0; i < npages; i++) 28209cbfeafSKirill A. Shutemov put_page(pages[i]); 2836b45d858STrond Myklebust } 2846b45d858STrond Myklebust 2851763da12SFred Isaman void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo, 2861763da12SFred Isaman struct nfs_direct_req *dreq) 2871763da12SFred Isaman { 288fe238e60SDave Wysochanski cinfo->inode = dreq->inode; 2891763da12SFred Isaman cinfo->mds = &dreq->mds_cinfo; 2901763da12SFred Isaman cinfo->ds = &dreq->ds_cinfo; 2911763da12SFred Isaman cinfo->dreq = dreq; 2921763da12SFred Isaman cinfo->completion_ops = &nfs_direct_commit_completion_ops; 2931763da12SFred Isaman } 2941763da12SFred Isaman 2950a00b77bSWeston Andros Adamson static inline void nfs_direct_setup_mirroring(struct nfs_direct_req *dreq, 2960a00b77bSWeston Andros Adamson struct nfs_pageio_descriptor *pgio, 2970a00b77bSWeston Andros Adamson struct nfs_page *req) 2980a00b77bSWeston Andros Adamson { 2990a00b77bSWeston Andros Adamson int mirror_count = 1; 3000a00b77bSWeston Andros Adamson 3010a00b77bSWeston Andros Adamson if (pgio->pg_ops->pg_get_mirror_count) 3020a00b77bSWeston Andros Adamson mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req); 3030a00b77bSWeston Andros Adamson 3040a00b77bSWeston Andros Adamson dreq->mirror_count = mirror_count; 3050a00b77bSWeston Andros Adamson } 3060a00b77bSWeston Andros Adamson 30793619e59SChuck Lever static inline struct nfs_direct_req *nfs_direct_req_alloc(void) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds struct nfs_direct_req *dreq; 3101da177e4SLinus Torvalds 311292f3eeeSTrond Myklebust dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL); 3121da177e4SLinus Torvalds if (!dreq) 3131da177e4SLinus Torvalds return NULL; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds kref_init(&dreq->kref); 316607f31e8STrond Myklebust kref_get(&dreq->kref); 317d72b7a6bSTrond Myklebust init_completion(&dreq->completion); 3181763da12SFred Isaman INIT_LIST_HEAD(&dreq->mds_cinfo.list); 3195002c586SWeston Andros Adamson dreq->verf.committed = NFS_INVALID_STABLE_HOW; /* not set yet */ 3201763da12SFred Isaman INIT_WORK(&dreq->work, nfs_direct_write_schedule_work); 3210a00b77bSWeston Andros Adamson dreq->mirror_count = 1; 32215ce4a0cSChuck Lever spin_lock_init(&dreq->lock); 32393619e59SChuck Lever 32493619e59SChuck Lever return dreq; 32593619e59SChuck Lever } 32693619e59SChuck Lever 327b4946ffbSTrond Myklebust static void nfs_direct_req_free(struct kref *kref) 3281da177e4SLinus Torvalds { 3291da177e4SLinus Torvalds struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); 330a8881f5aSTrond Myklebust 3318c393f9aSPeng Tao nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo); 332f11ac8dbSTrond Myklebust if (dreq->l_ctx != NULL) 333f11ac8dbSTrond Myklebust nfs_put_lock_context(dreq->l_ctx); 334a8881f5aSTrond Myklebust if (dreq->ctx != NULL) 335a8881f5aSTrond Myklebust put_nfs_open_context(dreq->ctx); 3361da177e4SLinus Torvalds kmem_cache_free(nfs_direct_cachep, dreq); 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 339b4946ffbSTrond Myklebust static void nfs_direct_req_release(struct nfs_direct_req *dreq) 340b4946ffbSTrond Myklebust { 341b4946ffbSTrond Myklebust kref_put(&dreq->kref, nfs_direct_req_free); 342b4946ffbSTrond Myklebust } 343b4946ffbSTrond Myklebust 3446296556fSPeng Tao ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq) 3456296556fSPeng Tao { 3466296556fSPeng Tao return dreq->bytes_left; 3476296556fSPeng Tao } 3486296556fSPeng Tao EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left); 3496296556fSPeng Tao 350d4cc948bSChuck Lever /* 351bc0fb201SChuck Lever * Collects and returns the final error value/byte-count. 352bc0fb201SChuck Lever */ 353bc0fb201SChuck Lever static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) 354bc0fb201SChuck Lever { 35515ce4a0cSChuck Lever ssize_t result = -EIOCBQUEUED; 356bc0fb201SChuck Lever 357bc0fb201SChuck Lever /* Async requests don't wait here */ 358bc0fb201SChuck Lever if (dreq->iocb) 359bc0fb201SChuck Lever goto out; 360bc0fb201SChuck Lever 361150030b7SMatthew Wilcox result = wait_for_completion_killable(&dreq->completion); 362bc0fb201SChuck Lever 363d2a7de0bSTrond Myklebust if (!result) { 364d2a7de0bSTrond Myklebust result = dreq->count; 365d2a7de0bSTrond Myklebust WARN_ON_ONCE(dreq->count < 0); 366d2a7de0bSTrond Myklebust } 367bc0fb201SChuck Lever if (!result) 36815ce4a0cSChuck Lever result = dreq->error; 369bc0fb201SChuck Lever 370bc0fb201SChuck Lever out: 371bc0fb201SChuck Lever return (ssize_t) result; 372bc0fb201SChuck Lever } 373bc0fb201SChuck Lever 374bc0fb201SChuck Lever /* 375607f31e8STrond Myklebust * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust 376607f31e8STrond Myklebust * the iocb is still valid here if this is a synchronous request. 37763ab46abSChuck Lever */ 378f7b5c340STrond Myklebust static void nfs_direct_complete(struct nfs_direct_req *dreq) 37963ab46abSChuck Lever { 3809811cd57SChristoph Hellwig struct inode *inode = dreq->inode; 3819811cd57SChristoph Hellwig 382fe0f07d0SJens Axboe inode_dio_end(inode); 3832a009ec9SChristoph Hellwig 3842a009ec9SChristoph Hellwig if (dreq->iocb) { 3852a009ec9SChristoph Hellwig long res = (long) dreq->error; 386d2a7de0bSTrond Myklebust if (dreq->count != 0) { 3872a009ec9SChristoph Hellwig res = (long) dreq->count; 388d2a7de0bSTrond Myklebust WARN_ON_ONCE(dreq->count < 0); 389d2a7de0bSTrond Myklebust } 39004b2fa9fSChristoph Hellwig dreq->iocb->ki_complete(dreq->iocb, res, 0); 391d72b7a6bSTrond Myklebust } 3922a009ec9SChristoph Hellwig 393024de8f1SDaniel Wagner complete(&dreq->completion); 39463ab46abSChuck Lever 395b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 39663ab46abSChuck Lever } 39763ab46abSChuck Lever 398584aa810SFred Isaman static void nfs_direct_read_completion(struct nfs_pgio_header *hdr) 399fdd1e74cSTrond Myklebust { 400584aa810SFred Isaman unsigned long bytes = 0; 401584aa810SFred Isaman struct nfs_direct_req *dreq = hdr->dreq; 402fdd1e74cSTrond Myklebust 403584aa810SFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 404584aa810SFred Isaman goto out_put; 4051da177e4SLinus Torvalds 40615ce4a0cSChuck Lever spin_lock(&dreq->lock); 407584aa810SFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0)) 408584aa810SFred Isaman dreq->error = hdr->error; 409584aa810SFred Isaman else 4100a00b77bSWeston Andros Adamson nfs_direct_good_bytes(dreq, hdr); 4110a00b77bSWeston Andros Adamson 41215ce4a0cSChuck Lever spin_unlock(&dreq->lock); 4131da177e4SLinus Torvalds 414584aa810SFred Isaman while (!list_empty(&hdr->pages)) { 415584aa810SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 416584aa810SFred Isaman struct page *page = req->wb_page; 417584aa810SFred Isaman 418ad3cba22SDave Kleikamp if (!PageCompound(page) && bytes < hdr->good_bytes && 419ad3cba22SDave Kleikamp (dreq->flags == NFS_ODIRECT_SHOULD_DIRTY)) 4204bd8b010STrond Myklebust set_page_dirty(page); 421584aa810SFred Isaman bytes += req->wb_bytes; 422584aa810SFred Isaman nfs_list_remove_request(req); 423beeb5338SAnna Schumaker nfs_release_request(req); 424584aa810SFred Isaman } 425584aa810SFred Isaman out_put: 426607f31e8STrond Myklebust if (put_dreq(dreq)) 427f7b5c340STrond Myklebust nfs_direct_complete(dreq); 428584aa810SFred Isaman hdr->release(hdr); 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 431*df3accb8STrond Myklebust static void nfs_read_sync_pgio_error(struct list_head *head, int error) 432cd841605SFred Isaman { 433584aa810SFred Isaman struct nfs_page *req; 434cd841605SFred Isaman 435584aa810SFred Isaman while (!list_empty(head)) { 436584aa810SFred Isaman req = nfs_list_entry(head->next); 437584aa810SFred Isaman nfs_list_remove_request(req); 438584aa810SFred Isaman nfs_release_request(req); 439cd841605SFred Isaman } 440584aa810SFred Isaman } 441584aa810SFred Isaman 442584aa810SFred Isaman static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr) 443584aa810SFred Isaman { 444584aa810SFred Isaman get_dreq(hdr->dreq); 445584aa810SFred Isaman } 446584aa810SFred Isaman 447584aa810SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = { 4483e9e0ca3STrond Myklebust .error_cleanup = nfs_read_sync_pgio_error, 449584aa810SFred Isaman .init_hdr = nfs_direct_pgio_init, 450584aa810SFred Isaman .completion = nfs_direct_read_completion, 451584aa810SFred Isaman }; 452cd841605SFred Isaman 453d4cc948bSChuck Lever /* 454607f31e8STrond Myklebust * For each rsize'd chunk of the user's buffer, dispatch an NFS READ 455607f31e8STrond Myklebust * operation. If nfs_readdata_alloc() or get_user_pages() fails, 456607f31e8STrond Myklebust * bail and stop sending more reads. Read length accounting is 457607f31e8STrond Myklebust * handled automatically by nfs_direct_read_result(). Otherwise, if 458607f31e8STrond Myklebust * no requests have been sent, just return an error. 4591da177e4SLinus Torvalds */ 46091f79c43SAl Viro 46191f79c43SAl Viro static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq, 46291f79c43SAl Viro struct iov_iter *iter, 46391f79c43SAl Viro loff_t pos) 4641da177e4SLinus Torvalds { 46591f79c43SAl Viro struct nfs_pageio_descriptor desc; 46691f79c43SAl Viro struct inode *inode = dreq->inode; 46791f79c43SAl Viro ssize_t result = -EINVAL; 46891f79c43SAl Viro size_t requested_bytes = 0; 46991f79c43SAl Viro size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE); 47082b145c5SChuck Lever 47116b90578SLinus Torvalds nfs_pageio_init_read(&desc, dreq->inode, false, 47291f79c43SAl Viro &nfs_direct_read_completion_ops); 47391f79c43SAl Viro get_dreq(dreq); 47491f79c43SAl Viro desc.pg_dreq = dreq; 475fe0f07d0SJens Axboe inode_dio_begin(inode); 47691f79c43SAl Viro 47791f79c43SAl Viro while (iov_iter_count(iter)) { 47891f79c43SAl Viro struct page **pagevec; 4795dd602f2SChuck Lever size_t bytes; 48091f79c43SAl Viro size_t pgbase; 48191f79c43SAl Viro unsigned npages, i; 4821da177e4SLinus Torvalds 48391f79c43SAl Viro result = iov_iter_get_pages_alloc(iter, &pagevec, 48491f79c43SAl Viro rsize, &pgbase); 485584aa810SFred Isaman if (result < 0) 486749e146eSChuck Lever break; 487a564b8f0SMel Gorman 48891f79c43SAl Viro bytes = result; 48991f79c43SAl Viro iov_iter_advance(iter, bytes); 49091f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 491584aa810SFred Isaman for (i = 0; i < npages; i++) { 492584aa810SFred Isaman struct nfs_page *req; 493bf5fc402STrond Myklebust unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 494584aa810SFred Isaman /* XXX do we need to do the eof zeroing found in async_filler? */ 4952bfc6e56SWeston Andros Adamson req = nfs_create_request(dreq->ctx, pagevec[i], NULL, 496584aa810SFred Isaman pgbase, req_len); 497584aa810SFred Isaman if (IS_ERR(req)) { 498584aa810SFred Isaman result = PTR_ERR(req); 499dbae4c73STrond Myklebust break; 500584aa810SFred Isaman } 501584aa810SFred Isaman req->wb_index = pos >> PAGE_SHIFT; 502584aa810SFred Isaman req->wb_offset = pos & ~PAGE_MASK; 50391f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 50491f79c43SAl Viro result = desc.pg_error; 505584aa810SFred Isaman nfs_release_request(req); 506584aa810SFred Isaman break; 507584aa810SFred Isaman } 508584aa810SFred Isaman pgbase = 0; 509584aa810SFred Isaman bytes -= req_len; 51091f79c43SAl Viro requested_bytes += req_len; 511584aa810SFred Isaman pos += req_len; 51235754bc0SPeng Tao dreq->bytes_left -= req_len; 513584aa810SFred Isaman } 5146d74743bSTrond Myklebust nfs_direct_release_pages(pagevec, npages); 51591f79c43SAl Viro kvfree(pagevec); 51619f73787SChuck Lever if (result < 0) 51719f73787SChuck Lever break; 51819f73787SChuck Lever } 51919f73787SChuck Lever 520584aa810SFred Isaman nfs_pageio_complete(&desc); 521584aa810SFred Isaman 522839f7ad6SChuck Lever /* 523839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 524839f7ad6SChuck Lever * generic layer handle the completion. 525839f7ad6SChuck Lever */ 526839f7ad6SChuck Lever if (requested_bytes == 0) { 527fe0f07d0SJens Axboe inode_dio_end(inode); 528839f7ad6SChuck Lever nfs_direct_req_release(dreq); 529839f7ad6SChuck Lever return result < 0 ? result : -EIO; 530839f7ad6SChuck Lever } 531839f7ad6SChuck Lever 53219f73787SChuck Lever if (put_dreq(dreq)) 533f7b5c340STrond Myklebust nfs_direct_complete(dreq); 53485128b2bSAl Viro return requested_bytes; 53519f73787SChuck Lever } 53619f73787SChuck Lever 53714a3ec79SChristoph Hellwig /** 53814a3ec79SChristoph Hellwig * nfs_file_direct_read - file direct read operation for NFS files 53914a3ec79SChristoph Hellwig * @iocb: target I/O control block 540619d30b4SAl Viro * @iter: vector of user buffers into which to read data 54114a3ec79SChristoph Hellwig * 54214a3ec79SChristoph Hellwig * We use this function for direct reads instead of calling 54314a3ec79SChristoph Hellwig * generic_file_aio_read() in order to avoid gfar's check to see if 54414a3ec79SChristoph Hellwig * the request starts before the end of the file. For that check 54514a3ec79SChristoph Hellwig * to work, we must generate a GETATTR before each direct read, and 54614a3ec79SChristoph Hellwig * even then there is a window between the GETATTR and the subsequent 54714a3ec79SChristoph Hellwig * READ where the file size could change. Our preference is simply 54814a3ec79SChristoph Hellwig * to do all reads the application wants, and the server will take 54914a3ec79SChristoph Hellwig * care of managing the end of file boundary. 55014a3ec79SChristoph Hellwig * 55114a3ec79SChristoph Hellwig * This function also eliminates unnecessarily updating the file's 55214a3ec79SChristoph Hellwig * atime locally, as the NFS server sets the file's atime, and this 55314a3ec79SChristoph Hellwig * client must read the updated atime from the server back into its 55414a3ec79SChristoph Hellwig * cache. 55514a3ec79SChristoph Hellwig */ 556c8b8e32dSChristoph Hellwig ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter) 5571da177e4SLinus Torvalds { 55814a3ec79SChristoph Hellwig struct file *file = iocb->ki_filp; 55914a3ec79SChristoph Hellwig struct address_space *mapping = file->f_mapping; 56014a3ec79SChristoph Hellwig struct inode *inode = mapping->host; 5611da177e4SLinus Torvalds struct nfs_direct_req *dreq; 562b3c54de6STrond Myklebust struct nfs_lock_context *l_ctx; 56385128b2bSAl Viro ssize_t result = -EINVAL, requested; 564a6cbcd4aSAl Viro size_t count = iov_iter_count(iter); 56514a3ec79SChristoph Hellwig nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); 56614a3ec79SChristoph Hellwig 56714a3ec79SChristoph Hellwig dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n", 568c8b8e32dSChristoph Hellwig file, count, (long long) iocb->ki_pos); 56914a3ec79SChristoph Hellwig 57014a3ec79SChristoph Hellwig result = 0; 57114a3ec79SChristoph Hellwig if (!count) 57214a3ec79SChristoph Hellwig goto out; 57314a3ec79SChristoph Hellwig 57414a3ec79SChristoph Hellwig task_io_account_read(count); 57514a3ec79SChristoph Hellwig 57614a3ec79SChristoph Hellwig result = -ENOMEM; 577607f31e8STrond Myklebust dreq = nfs_direct_req_alloc(); 578f11ac8dbSTrond Myklebust if (dreq == NULL) 579a5864c99STrond Myklebust goto out; 5801da177e4SLinus Torvalds 58191d5b470SChuck Lever dreq->inode = inode; 582ed3743a6SWeston Andros Adamson dreq->bytes_left = dreq->max_count = count; 583c8b8e32dSChristoph Hellwig dreq->io_start = iocb->ki_pos; 584cd3758e3STrond Myklebust dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 585b3c54de6STrond Myklebust l_ctx = nfs_get_lock_context(dreq->ctx); 586b3c54de6STrond Myklebust if (IS_ERR(l_ctx)) { 587b3c54de6STrond Myklebust result = PTR_ERR(l_ctx); 588f11ac8dbSTrond Myklebust goto out_release; 589b3c54de6STrond Myklebust } 590b3c54de6STrond Myklebust dreq->l_ctx = l_ctx; 591487b8372SChuck Lever if (!is_sync_kiocb(iocb)) 592487b8372SChuck Lever dreq->iocb = iocb; 5931da177e4SLinus Torvalds 594ad3cba22SDave Kleikamp if (iter_is_iovec(iter)) 595ad3cba22SDave Kleikamp dreq->flags = NFS_ODIRECT_SHOULD_DIRTY; 596ad3cba22SDave Kleikamp 597a5864c99STrond Myklebust nfs_start_io_direct(inode); 598a5864c99STrond Myklebust 599619d30b4SAl Viro NFS_I(inode)->read_io += count; 60085128b2bSAl Viro requested = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos); 601d0b9875dSChristoph Hellwig 602a5864c99STrond Myklebust nfs_end_io_direct(inode); 603d0b9875dSChristoph Hellwig 60485128b2bSAl Viro if (requested > 0) { 605bc0fb201SChuck Lever result = nfs_direct_wait(dreq); 60685128b2bSAl Viro if (result > 0) { 60785128b2bSAl Viro requested -= result; 608c8b8e32dSChristoph Hellwig iocb->ki_pos += result; 60914a3ec79SChristoph Hellwig } 61085128b2bSAl Viro iov_iter_revert(iter, requested); 61185128b2bSAl Viro } else { 61285128b2bSAl Viro result = requested; 61385128b2bSAl Viro } 614d0b9875dSChristoph Hellwig 615f11ac8dbSTrond Myklebust out_release: 616b4946ffbSTrond Myklebust nfs_direct_req_release(dreq); 617f11ac8dbSTrond Myklebust out: 6181da177e4SLinus Torvalds return result; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 621085d1e33STom Haynes static void 622085d1e33STom Haynes nfs_direct_write_scan_commit_list(struct inode *inode, 623085d1e33STom Haynes struct list_head *list, 624085d1e33STom Haynes struct nfs_commit_info *cinfo) 625085d1e33STom Haynes { 626e824f99aSTrond Myklebust mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 627085d1e33STom Haynes #ifdef CONFIG_NFS_V4_1 628085d1e33STom Haynes if (cinfo->ds != NULL && cinfo->ds->nwritten != 0) 629085d1e33STom Haynes NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo); 630085d1e33STom Haynes #endif 631085d1e33STom Haynes nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0); 632e824f99aSTrond Myklebust mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 633085d1e33STom Haynes } 634085d1e33STom Haynes 635fad61490STrond Myklebust static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) 6361da177e4SLinus Torvalds { 6371763da12SFred Isaman struct nfs_pageio_descriptor desc; 6381763da12SFred Isaman struct nfs_page *req, *tmp; 6391763da12SFred Isaman LIST_HEAD(reqs); 6401763da12SFred Isaman struct nfs_commit_info cinfo; 6411763da12SFred Isaman LIST_HEAD(failed); 6420a00b77bSWeston Andros Adamson int i; 6431763da12SFred Isaman 6441763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 645085d1e33STom Haynes nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo); 6461da177e4SLinus Torvalds 647fad61490STrond Myklebust dreq->count = 0; 648a5314a74STrond Myklebust dreq->verf.committed = NFS_INVALID_STABLE_HOW; 649a5314a74STrond Myklebust nfs_clear_pnfs_ds_commit_verifiers(&dreq->ds_cinfo); 6500a00b77bSWeston Andros Adamson for (i = 0; i < dreq->mirror_count; i++) 6510a00b77bSWeston Andros Adamson dreq->mirrors[i].count = 0; 652607f31e8STrond Myklebust get_dreq(dreq); 6531da177e4SLinus Torvalds 654a20c93e3SChristoph Hellwig nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false, 6551763da12SFred Isaman &nfs_direct_write_completion_ops); 6561763da12SFred Isaman desc.pg_dreq = dreq; 657607f31e8STrond Myklebust 6580a00b77bSWeston Andros Adamson req = nfs_list_entry(reqs.next); 6590a00b77bSWeston Andros Adamson nfs_direct_setup_mirroring(dreq, &desc, req); 660d600ad1fSPeng Tao if (desc.pg_error < 0) { 661d600ad1fSPeng Tao list_splice_init(&reqs, &failed); 662d600ad1fSPeng Tao goto out_failed; 663d600ad1fSPeng Tao } 6640a00b77bSWeston Andros Adamson 6651763da12SFred Isaman list_for_each_entry_safe(req, tmp, &reqs, wb_list) { 6661763da12SFred Isaman if (!nfs_pageio_add_request(&desc, req)) { 667078b5fd9STrond Myklebust nfs_list_move_request(req, &failed); 668fe238e60SDave Wysochanski spin_lock(&cinfo.inode->i_lock); 6691763da12SFred Isaman dreq->flags = 0; 670d600ad1fSPeng Tao if (desc.pg_error < 0) 671d600ad1fSPeng Tao dreq->error = desc.pg_error; 672d600ad1fSPeng Tao else 6731763da12SFred Isaman dreq->error = -EIO; 674fe238e60SDave Wysochanski spin_unlock(&cinfo.inode->i_lock); 6751763da12SFred Isaman } 6765a695da2STrond Myklebust nfs_release_request(req); 6771763da12SFred Isaman } 6781763da12SFred Isaman nfs_pageio_complete(&desc); 679607f31e8STrond Myklebust 680d600ad1fSPeng Tao out_failed: 6814035c248STrond Myklebust while (!list_empty(&failed)) { 6824035c248STrond Myklebust req = nfs_list_entry(failed.next); 6834035c248STrond Myklebust nfs_list_remove_request(req); 6841d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 6854035c248STrond Myklebust } 686607f31e8STrond Myklebust 687607f31e8STrond Myklebust if (put_dreq(dreq)) 6884d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 689fad61490STrond Myklebust } 6901da177e4SLinus Torvalds 6911763da12SFred Isaman static void nfs_direct_commit_complete(struct nfs_commit_data *data) 692fad61490STrond Myklebust { 6930b7c0153SFred Isaman struct nfs_direct_req *dreq = data->dreq; 6941763da12SFred Isaman struct nfs_commit_info cinfo; 6951763da12SFred Isaman struct nfs_page *req; 696c9d8f89dSTrond Myklebust int status = data->task.tk_status; 697c9d8f89dSTrond Myklebust 6981763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 699fe4f844dSAnna Schumaker if (status < 0 || nfs_direct_cmp_commit_data_verf(dreq, data)) 700fad61490STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 701fad61490STrond Myklebust 7021763da12SFred Isaman while (!list_empty(&data->pages)) { 7031763da12SFred Isaman req = nfs_list_entry(data->pages.next); 7041763da12SFred Isaman nfs_list_remove_request(req); 7051763da12SFred Isaman if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) { 7061763da12SFred Isaman /* Note the rewrite will go through mds */ 707b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, NULL, &cinfo, 0); 708906369e4SFred Isaman } else 709906369e4SFred Isaman nfs_release_request(req); 7101d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 711fad61490STrond Myklebust } 712fad61490STrond Myklebust 7131763da12SFred Isaman if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) 7144d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 7151763da12SFred Isaman } 7161763da12SFred Isaman 717b20135d0STrond Myklebust static void nfs_direct_resched_write(struct nfs_commit_info *cinfo, 718b20135d0STrond Myklebust struct nfs_page *req) 7191763da12SFred Isaman { 720b20135d0STrond Myklebust struct nfs_direct_req *dreq = cinfo->dreq; 721b20135d0STrond Myklebust 722b20135d0STrond Myklebust spin_lock(&dreq->lock); 723b20135d0STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 724b20135d0STrond Myklebust spin_unlock(&dreq->lock); 725b20135d0STrond Myklebust nfs_mark_request_commit(req, NULL, cinfo, 0); 7261763da12SFred Isaman } 7271763da12SFred Isaman 7281763da12SFred Isaman static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = { 7291763da12SFred Isaman .completion = nfs_direct_commit_complete, 730b20135d0STrond Myklebust .resched_write = nfs_direct_resched_write, 731fad61490STrond Myklebust }; 732fad61490STrond Myklebust 733fad61490STrond Myklebust static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) 734fad61490STrond Myklebust { 7351763da12SFred Isaman int res; 7361763da12SFred Isaman struct nfs_commit_info cinfo; 7371763da12SFred Isaman LIST_HEAD(mds_list); 738fad61490STrond Myklebust 7391763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 7401763da12SFred Isaman nfs_scan_commit(dreq->inode, &mds_list, &cinfo); 7411763da12SFred Isaman res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo); 7421763da12SFred Isaman if (res < 0) /* res == -ENOMEM */ 7431763da12SFred Isaman nfs_direct_write_reschedule(dreq); 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7461763da12SFred Isaman static void nfs_direct_write_schedule_work(struct work_struct *work) 7471da177e4SLinus Torvalds { 7481763da12SFred Isaman struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work); 749fad61490STrond Myklebust int flags = dreq->flags; 7501da177e4SLinus Torvalds 751fad61490STrond Myklebust dreq->flags = 0; 752fad61490STrond Myklebust switch (flags) { 753fad61490STrond Myklebust case NFS_ODIRECT_DO_COMMIT: 754fad61490STrond Myklebust nfs_direct_commit_schedule(dreq); 7551da177e4SLinus Torvalds break; 756fad61490STrond Myklebust case NFS_ODIRECT_RESCHED_WRITES: 757fad61490STrond Myklebust nfs_direct_write_reschedule(dreq); 7581da177e4SLinus Torvalds break; 7591da177e4SLinus Torvalds default: 760f7b5c340STrond Myklebust nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping); 761f7b5c340STrond Myklebust nfs_direct_complete(dreq); 7621da177e4SLinus Torvalds } 763fad61490STrond Myklebust } 764fad61490STrond Myklebust 7654d3b55d3SAnna Schumaker static void nfs_direct_write_complete(struct nfs_direct_req *dreq) 766fad61490STrond Myklebust { 76746483c2eSNeilBrown queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */ 768fad61490STrond Myklebust } 7691763da12SFred Isaman 7701763da12SFred Isaman static void nfs_direct_write_completion(struct nfs_pgio_header *hdr) 7711763da12SFred Isaman { 7721763da12SFred Isaman struct nfs_direct_req *dreq = hdr->dreq; 7731763da12SFred Isaman struct nfs_commit_info cinfo; 774c65e6254SWeston Andros Adamson bool request_commit = false; 7751763da12SFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 7761763da12SFred Isaman 7771763da12SFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 7781763da12SFred Isaman goto out_put; 7791763da12SFred Isaman 7801763da12SFred Isaman nfs_init_cinfo_from_dreq(&cinfo, dreq); 7811763da12SFred Isaman 7821763da12SFred Isaman spin_lock(&dreq->lock); 7831763da12SFred Isaman 7841b8d97b0SJ. Bruce Fields if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) 7851763da12SFred Isaman dreq->error = hdr->error; 786c65e6254SWeston Andros Adamson if (dreq->error == 0) { 7870a00b77bSWeston Andros Adamson nfs_direct_good_bytes(dreq, hdr); 788c65e6254SWeston Andros Adamson if (nfs_write_need_commit(hdr)) { 7891763da12SFred Isaman if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) 790c65e6254SWeston Andros Adamson request_commit = true; 7911763da12SFred Isaman else if (dreq->flags == 0) { 7925002c586SWeston Andros Adamson nfs_direct_set_hdr_verf(dreq, hdr); 793c65e6254SWeston Andros Adamson request_commit = true; 7941763da12SFred Isaman dreq->flags = NFS_ODIRECT_DO_COMMIT; 7951763da12SFred Isaman } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) { 796c65e6254SWeston Andros Adamson request_commit = true; 797c65e6254SWeston Andros Adamson if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr)) 7985002c586SWeston Andros Adamson dreq->flags = 7995002c586SWeston Andros Adamson NFS_ODIRECT_RESCHED_WRITES; 8001763da12SFred Isaman } 8011763da12SFred Isaman } 8021763da12SFred Isaman } 8031763da12SFred Isaman spin_unlock(&dreq->lock); 8041763da12SFred Isaman 8051763da12SFred Isaman while (!list_empty(&hdr->pages)) { 8062bfc6e56SWeston Andros Adamson 8071763da12SFred Isaman req = nfs_list_entry(hdr->pages.next); 8081763da12SFred Isaman nfs_list_remove_request(req); 809c65e6254SWeston Andros Adamson if (request_commit) { 81004277086STrond Myklebust kref_get(&req->wb_kref); 811b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, hdr->lseg, &cinfo, 812b57ff130SWeston Andros Adamson hdr->ds_commit_idx); 8131763da12SFred Isaman } 8141d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 8151763da12SFred Isaman } 8161763da12SFred Isaman 8171763da12SFred Isaman out_put: 8181763da12SFred Isaman if (put_dreq(dreq)) 8194d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 8201763da12SFred Isaman hdr->release(hdr); 8211763da12SFred Isaman } 8221763da12SFred Isaman 823*df3accb8STrond Myklebust static void nfs_write_sync_pgio_error(struct list_head *head, int error) 8243e9e0ca3STrond Myklebust { 8253e9e0ca3STrond Myklebust struct nfs_page *req; 8263e9e0ca3STrond Myklebust 8273e9e0ca3STrond Myklebust while (!list_empty(head)) { 8283e9e0ca3STrond Myklebust req = nfs_list_entry(head->next); 8293e9e0ca3STrond Myklebust nfs_list_remove_request(req); 8301d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 8313e9e0ca3STrond Myklebust } 8323e9e0ca3STrond Myklebust } 8333e9e0ca3STrond Myklebust 834dc602dd7STrond Myklebust static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr) 835dc602dd7STrond Myklebust { 836dc602dd7STrond Myklebust struct nfs_direct_req *dreq = hdr->dreq; 837dc602dd7STrond Myklebust 838dc602dd7STrond Myklebust spin_lock(&dreq->lock); 839dc602dd7STrond Myklebust if (dreq->error == 0) { 840dc602dd7STrond Myklebust dreq->flags = NFS_ODIRECT_RESCHED_WRITES; 841dc602dd7STrond Myklebust /* fake unstable write to let common nfs resend pages */ 842dc602dd7STrond Myklebust hdr->verf.committed = NFS_UNSTABLE; 843dc602dd7STrond Myklebust hdr->good_bytes = hdr->args.count; 844dc602dd7STrond Myklebust } 845dc602dd7STrond Myklebust spin_unlock(&dreq->lock); 846dc602dd7STrond Myklebust } 847dc602dd7STrond Myklebust 8481763da12SFred Isaman static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = { 8493e9e0ca3STrond Myklebust .error_cleanup = nfs_write_sync_pgio_error, 8501763da12SFred Isaman .init_hdr = nfs_direct_pgio_init, 8511763da12SFred Isaman .completion = nfs_direct_write_completion, 852dc602dd7STrond Myklebust .reschedule_io = nfs_direct_write_reschedule_io, 8531763da12SFred Isaman }; 8541763da12SFred Isaman 85591f79c43SAl Viro 85691f79c43SAl Viro /* 85791f79c43SAl Viro * NB: Return the value of the first error return code. Subsequent 85891f79c43SAl Viro * errors after the first one are ignored. 85991f79c43SAl Viro */ 86091f79c43SAl Viro /* 86191f79c43SAl Viro * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE 86291f79c43SAl Viro * operation. If nfs_writedata_alloc() or get_user_pages() fails, 86391f79c43SAl Viro * bail and stop sending more writes. Write length accounting is 86491f79c43SAl Viro * handled automatically by nfs_direct_write_result(). Otherwise, if 86591f79c43SAl Viro * no requests have been sent, just return an error. 86691f79c43SAl Viro */ 86719f73787SChuck Lever static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq, 868619d30b4SAl Viro struct iov_iter *iter, 86991f79c43SAl Viro loff_t pos) 87019f73787SChuck Lever { 8711763da12SFred Isaman struct nfs_pageio_descriptor desc; 8721d59d61fSTrond Myklebust struct inode *inode = dreq->inode; 87319f73787SChuck Lever ssize_t result = 0; 87419f73787SChuck Lever size_t requested_bytes = 0; 87591f79c43SAl Viro size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE); 87619f73787SChuck Lever 877a20c93e3SChristoph Hellwig nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false, 8781763da12SFred Isaman &nfs_direct_write_completion_ops); 8791763da12SFred Isaman desc.pg_dreq = dreq; 88019f73787SChuck Lever get_dreq(dreq); 881fe0f07d0SJens Axboe inode_dio_begin(inode); 88219f73787SChuck Lever 88391f79c43SAl Viro NFS_I(inode)->write_io += iov_iter_count(iter); 88491f79c43SAl Viro while (iov_iter_count(iter)) { 88591f79c43SAl Viro struct page **pagevec; 88691f79c43SAl Viro size_t bytes; 88791f79c43SAl Viro size_t pgbase; 88891f79c43SAl Viro unsigned npages, i; 88991f79c43SAl Viro 89091f79c43SAl Viro result = iov_iter_get_pages_alloc(iter, &pagevec, 89191f79c43SAl Viro wsize, &pgbase); 89219f73787SChuck Lever if (result < 0) 89319f73787SChuck Lever break; 89491f79c43SAl Viro 89591f79c43SAl Viro bytes = result; 89691f79c43SAl Viro iov_iter_advance(iter, bytes); 89791f79c43SAl Viro npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE; 89891f79c43SAl Viro for (i = 0; i < npages; i++) { 89991f79c43SAl Viro struct nfs_page *req; 90091f79c43SAl Viro unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase); 90191f79c43SAl Viro 90216b90578SLinus Torvalds req = nfs_create_request(dreq->ctx, pagevec[i], NULL, 90391f79c43SAl Viro pgbase, req_len); 90491f79c43SAl Viro if (IS_ERR(req)) { 90591f79c43SAl Viro result = PTR_ERR(req); 90619f73787SChuck Lever break; 90791f79c43SAl Viro } 9080a00b77bSWeston Andros Adamson 9090a00b77bSWeston Andros Adamson nfs_direct_setup_mirroring(dreq, &desc, req); 910d600ad1fSPeng Tao if (desc.pg_error < 0) { 911d600ad1fSPeng Tao nfs_free_request(req); 912d600ad1fSPeng Tao result = desc.pg_error; 913d600ad1fSPeng Tao break; 914d600ad1fSPeng Tao } 9150a00b77bSWeston Andros Adamson 91691f79c43SAl Viro nfs_lock_request(req); 91791f79c43SAl Viro req->wb_index = pos >> PAGE_SHIFT; 91891f79c43SAl Viro req->wb_offset = pos & ~PAGE_MASK; 91991f79c43SAl Viro if (!nfs_pageio_add_request(&desc, req)) { 92091f79c43SAl Viro result = desc.pg_error; 92191f79c43SAl Viro nfs_unlock_and_release_request(req); 92291f79c43SAl Viro break; 92391f79c43SAl Viro } 92491f79c43SAl Viro pgbase = 0; 92591f79c43SAl Viro bytes -= req_len; 92691f79c43SAl Viro requested_bytes += req_len; 92791f79c43SAl Viro pos += req_len; 92891f79c43SAl Viro dreq->bytes_left -= req_len; 92991f79c43SAl Viro } 93091f79c43SAl Viro nfs_direct_release_pages(pagevec, npages); 93191f79c43SAl Viro kvfree(pagevec); 93291f79c43SAl Viro if (result < 0) 93391f79c43SAl Viro break; 93419f73787SChuck Lever } 9351763da12SFred Isaman nfs_pageio_complete(&desc); 93619f73787SChuck Lever 937839f7ad6SChuck Lever /* 938839f7ad6SChuck Lever * If no bytes were started, return the error, and let the 939839f7ad6SChuck Lever * generic layer handle the completion. 940839f7ad6SChuck Lever */ 941839f7ad6SChuck Lever if (requested_bytes == 0) { 942fe0f07d0SJens Axboe inode_dio_end(inode); 943839f7ad6SChuck Lever nfs_direct_req_release(dreq); 944839f7ad6SChuck Lever return result < 0 ? result : -EIO; 945839f7ad6SChuck Lever } 946839f7ad6SChuck Lever 94719f73787SChuck Lever if (put_dreq(dreq)) 9484d3b55d3SAnna Schumaker nfs_direct_write_complete(dreq); 94985128b2bSAl Viro return requested_bytes; 95019f73787SChuck Lever } 95119f73787SChuck Lever 9521da177e4SLinus Torvalds /** 9531da177e4SLinus Torvalds * nfs_file_direct_write - file direct write operation for NFS files 9541da177e4SLinus Torvalds * @iocb: target I/O control block 955619d30b4SAl Viro * @iter: vector of user buffers from which to write data 9561da177e4SLinus Torvalds * 9571da177e4SLinus Torvalds * We use this function for direct writes instead of calling 9581da177e4SLinus Torvalds * generic_file_aio_write() in order to avoid taking the inode 9591da177e4SLinus Torvalds * semaphore and updating the i_size. The NFS server will set 9601da177e4SLinus Torvalds * the new i_size and this client must read the updated size 9611da177e4SLinus Torvalds * back into its cache. We let the server do generic write 9621da177e4SLinus Torvalds * parameter checking and report problems. 9631da177e4SLinus Torvalds * 9641da177e4SLinus Torvalds * We eliminate local atime updates, see direct read above. 9651da177e4SLinus Torvalds * 9661da177e4SLinus Torvalds * We avoid unnecessary page cache invalidations for normal cached 9671da177e4SLinus Torvalds * readers of this file. 9681da177e4SLinus Torvalds * 9691da177e4SLinus Torvalds * Note that O_APPEND is not supported for NFS direct writes, as there 9701da177e4SLinus Torvalds * is no atomic O_APPEND write facility in the NFS protocol. 9711da177e4SLinus Torvalds */ 97265a4a1caSAl Viro ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter) 9731da177e4SLinus Torvalds { 97485128b2bSAl Viro ssize_t result = -EINVAL, requested; 97589698b24STrond Myklebust size_t count; 9761da177e4SLinus Torvalds struct file *file = iocb->ki_filp; 9771da177e4SLinus Torvalds struct address_space *mapping = file->f_mapping; 97822cd1bf1SChristoph Hellwig struct inode *inode = mapping->host; 97922cd1bf1SChristoph Hellwig struct nfs_direct_req *dreq; 98022cd1bf1SChristoph Hellwig struct nfs_lock_context *l_ctx; 98165a4a1caSAl Viro loff_t pos, end; 982c216fd70SChuck Lever 9836de1472fSAl Viro dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n", 9843309dd04SAl Viro file, iov_iter_count(iter), (long long) iocb->ki_pos); 985027445c3SBadari Pulavarty 98689698b24STrond Myklebust result = generic_write_checks(iocb, iter); 98789698b24STrond Myklebust if (result <= 0) 98889698b24STrond Myklebust return result; 98989698b24STrond Myklebust count = result; 99089698b24STrond Myklebust nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); 9913309dd04SAl Viro 9923309dd04SAl Viro pos = iocb->ki_pos; 99309cbfeafSKirill A. Shutemov end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT; 994ce1a8e67SChuck Lever 99589698b24STrond Myklebust task_io_account_write(count); 9967ec10f26SKonstantin Khlebnikov 99722cd1bf1SChristoph Hellwig result = -ENOMEM; 99822cd1bf1SChristoph Hellwig dreq = nfs_direct_req_alloc(); 99922cd1bf1SChristoph Hellwig if (!dreq) 1000a5864c99STrond Myklebust goto out; 100122cd1bf1SChristoph Hellwig 100222cd1bf1SChristoph Hellwig dreq->inode = inode; 100389698b24STrond Myklebust dreq->bytes_left = dreq->max_count = count; 10045fadeb47SPeng Tao dreq->io_start = pos; 100522cd1bf1SChristoph Hellwig dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp)); 100622cd1bf1SChristoph Hellwig l_ctx = nfs_get_lock_context(dreq->ctx); 100722cd1bf1SChristoph Hellwig if (IS_ERR(l_ctx)) { 100822cd1bf1SChristoph Hellwig result = PTR_ERR(l_ctx); 100922cd1bf1SChristoph Hellwig goto out_release; 101022cd1bf1SChristoph Hellwig } 101122cd1bf1SChristoph Hellwig dreq->l_ctx = l_ctx; 101222cd1bf1SChristoph Hellwig if (!is_sync_kiocb(iocb)) 101322cd1bf1SChristoph Hellwig dreq->iocb = iocb; 101422cd1bf1SChristoph Hellwig 1015a5864c99STrond Myklebust nfs_start_io_direct(inode); 1016a5864c99STrond Myklebust 101785128b2bSAl Viro requested = nfs_direct_write_schedule_iovec(dreq, iter, pos); 1018a9ab5e84SChristoph Hellwig 1019a9ab5e84SChristoph Hellwig if (mapping->nrpages) { 1020a9ab5e84SChristoph Hellwig invalidate_inode_pages2_range(mapping, 102109cbfeafSKirill A. Shutemov pos >> PAGE_SHIFT, end); 1022a9ab5e84SChristoph Hellwig } 1023a9ab5e84SChristoph Hellwig 1024a5864c99STrond Myklebust nfs_end_io_direct(inode); 1025a9ab5e84SChristoph Hellwig 102685128b2bSAl Viro if (requested > 0) { 102722cd1bf1SChristoph Hellwig result = nfs_direct_wait(dreq); 102822cd1bf1SChristoph Hellwig if (result > 0) { 102985128b2bSAl Viro requested -= result; 103022cd1bf1SChristoph Hellwig iocb->ki_pos = pos + result; 1031e2592217SChristoph Hellwig /* XXX: should check the generic_write_sync retval */ 1032e2592217SChristoph Hellwig generic_write_sync(iocb, result); 10331763da12SFred Isaman } 103485128b2bSAl Viro iov_iter_revert(iter, requested); 103585128b2bSAl Viro } else { 103685128b2bSAl Viro result = requested; 103722cd1bf1SChristoph Hellwig } 103822cd1bf1SChristoph Hellwig out_release: 103922cd1bf1SChristoph Hellwig nfs_direct_req_release(dreq); 1040a5864c99STrond Myklebust out: 104122cd1bf1SChristoph Hellwig return result; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 104488467055SChuck Lever /** 104588467055SChuck Lever * nfs_init_directcache - create a slab cache for nfs_direct_req structures 104688467055SChuck Lever * 104788467055SChuck Lever */ 1048f7b422b1SDavid Howells int __init nfs_init_directcache(void) 10491da177e4SLinus Torvalds { 10501da177e4SLinus Torvalds nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", 10511da177e4SLinus Torvalds sizeof(struct nfs_direct_req), 1052fffb60f9SPaul Jackson 0, (SLAB_RECLAIM_ACCOUNT| 1053fffb60f9SPaul Jackson SLAB_MEM_SPREAD), 105420c2df83SPaul Mundt NULL); 10551da177e4SLinus Torvalds if (nfs_direct_cachep == NULL) 10561da177e4SLinus Torvalds return -ENOMEM; 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds return 0; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 106188467055SChuck Lever /** 1062f7b422b1SDavid Howells * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures 106388467055SChuck Lever * 106488467055SChuck Lever */ 1065266bee88SDavid Brownell void nfs_destroy_directcache(void) 10661da177e4SLinus Torvalds { 10671a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_direct_cachep); 10681da177e4SLinus Torvalds } 1069