blob: 9d9085b93a322249a3c823453129fb8a016f46aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/direct.c
3 *
4 * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
5 *
6 * High-performance uncached I/O for the Linux NFS client
7 *
8 * There are important applications whose performance or correctness
9 * depends on uncached access to file data. Database clusters
Chuck Lever88467052006-03-20 13:44:34 -050010 * (multiple copies of the same instance running on separate hosts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * implement their own cache coherency protocol that subsumes file
Chuck Lever88467052006-03-20 13:44:34 -050012 * system cache protocols. Applications that process datasets
13 * considerably larger than the client's memory do not always benefit
14 * from a local cache. A streaming video server, for instance, has no
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * need to cache the contents of a file.
16 *
17 * When an application requests uncached I/O, all read and write requests
18 * are made directly to the server; data stored or fetched via these
19 * requests is not cached in the Linux page cache. The client does not
20 * correct unaligned requests from applications. All requested bytes are
21 * held on permanent storage before a direct write system call returns to
22 * an application.
23 *
24 * Solaris implements an uncached I/O facility called directio() that
25 * is used for backups and sequential I/O to very large files. Solaris
26 * also supports uncaching whole NFS partitions with "-o forcedirectio,"
27 * an undocumented mount option.
28 *
29 * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
30 * help from Andrew Morton.
31 *
32 * 18 Dec 2001 Initial implementation for 2.4 --cel
33 * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy
34 * 08 Jun 2003 Port to 2.5 APIs --cel
35 * 31 Mar 2004 Handle direct I/O without VFS support --cel
36 * 15 Sep 2004 Parallel async reads --cel
Chuck Lever88467052006-03-20 13:44:34 -050037 * 04 May 2005 support O_DIRECT with aio --cel
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 *
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/errno.h>
42#include <linux/sched.h>
43#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/file.h>
45#include <linux/pagemap.h>
46#include <linux/kref.h>
47
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
50#include <linux/sunrpc/clnt.h>
51
52#include <asm/system.h>
53#include <asm/uaccess.h>
54#include <asm/atomic.h>
55
Trond Myklebust8d5658c2007-04-10 09:26:35 -040056#include "internal.h"
Chuck Lever91d5b472006-03-20 13:44:14 -050057#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define NFSDBG_FACILITY NFSDBG_VFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Christoph Lametere18b8902006-12-06 20:33:20 -080061static struct kmem_cache *nfs_direct_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
64 * This represents a set of asynchronous requests that we're waiting on
65 */
66struct nfs_direct_req {
67 struct kref kref; /* release manager */
Chuck Lever15ce4a02006-03-20 13:44:34 -050068
69 /* I/O parameters */
Trond Myklebusta8881f52006-03-20 13:44:36 -050070 struct nfs_open_context *ctx; /* file open context info */
Chuck Lever99514f82006-03-20 13:44:30 -050071 struct kiocb * iocb; /* controlling i/o request */
Chuck Lever88467052006-03-20 13:44:34 -050072 struct inode * inode; /* target file of i/o */
Chuck Lever15ce4a02006-03-20 13:44:34 -050073
74 /* completion state */
Trond Myklebust607f31e2006-06-28 16:52:45 -040075 atomic_t io_count; /* i/os we're waiting for */
Chuck Lever15ce4a02006-03-20 13:44:34 -050076 spinlock_t lock; /* protect completion state */
Chuck Lever15ce4a02006-03-20 13:44:34 -050077 ssize_t count, /* bytes actually processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 error; /* any reported error */
Trond Myklebustd72b7a62006-03-20 13:44:43 -050079 struct completion completion; /* wait for i/o completion */
Trond Myklebustfad61492006-03-20 13:44:36 -050080
81 /* commit state */
Trond Myklebust607f31e2006-06-28 16:52:45 -040082 struct list_head rewrite_list; /* saved nfs_write_data structs */
Trond Myklebustfad61492006-03-20 13:44:36 -050083 struct nfs_write_data * commit_data; /* special write_data for commits */
84 int flags;
85#define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
86#define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
87 struct nfs_writeverf verf; /* unstable write verifier */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Trond Myklebustfad61492006-03-20 13:44:36 -050090static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
Trond Myklebust607f31e2006-06-28 16:52:45 -040091static const struct rpc_call_ops nfs_write_direct_ops;
92
93static inline void get_dreq(struct nfs_direct_req *dreq)
94{
95 atomic_inc(&dreq->io_count);
96}
97
98static inline int put_dreq(struct nfs_direct_req *dreq)
99{
100 return atomic_dec_and_test(&dreq->io_count);
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/**
Chuck Leverb8a32e22006-03-20 13:44:28 -0500104 * nfs_direct_IO - NFS address space operation for direct I/O
105 * @rw: direction (read or write)
106 * @iocb: target I/O control block
107 * @iov: array of vectors that define I/O buffer
108 * @pos: offset in file to begin the operation
109 * @nr_segs: size of iovec array
110 *
111 * The presence of this routine in the address space ops vector means
112 * the NFS client supports direct I/O. However, we shunt off direct
113 * read and write requests before the VFS gets them, so this method
114 * should never be called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
Chuck Leverb8a32e22006-03-20 13:44:28 -0500116ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
117{
Chuck Leverb8a32e22006-03-20 13:44:28 -0500118 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800119 iocb->ki_filp->f_path.dentry->d_name.name,
Trond Myklebuste99170f2006-04-18 13:21:42 -0400120 (long long) pos, nr_segs);
Chuck Leverb8a32e22006-03-20 13:44:28 -0500121
122 return -EINVAL;
123}
124
Trond Myklebustd4a8f362007-05-22 10:22:27 -0400125static void nfs_direct_dirty_pages(struct page **pages, unsigned int pgbase, size_t count)
Trond Myklebust6b45d852006-03-20 13:44:43 -0500126{
Trond Myklebustd4a8f362007-05-22 10:22:27 -0400127 unsigned int npages;
Chuck Lever749e1462007-05-19 17:22:46 -0400128 unsigned int i;
Trond Myklebustd4a8f362007-05-22 10:22:27 -0400129
130 if (count == 0)
131 return;
132 pages += (pgbase >> PAGE_SHIFT);
133 npages = (count + (pgbase & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
Trond Myklebust6b45d852006-03-20 13:44:43 -0500134 for (i = 0; i < npages; i++) {
135 struct page *page = pages[i];
Trond Myklebust607f31e2006-06-28 16:52:45 -0400136 if (!PageCompound(page))
Trond Myklebustd4a8f362007-05-22 10:22:27 -0400137 set_page_dirty(page);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500138 }
Chuck Lever9c93ab72006-06-20 12:56:31 -0400139}
140
Chuck Lever749e1462007-05-19 17:22:46 -0400141static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
Chuck Lever9c93ab72006-06-20 12:56:31 -0400142{
Chuck Lever749e1462007-05-19 17:22:46 -0400143 unsigned int i;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400144 for (i = 0; i < npages; i++)
145 page_cache_release(pages[i]);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500146}
147
Chuck Lever93619e52006-03-20 13:44:31 -0500148static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct nfs_direct_req *dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Christoph Lametere94b1762006-12-06 20:33:17 -0800152 dreq = kmem_cache_alloc(nfs_direct_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (!dreq)
154 return NULL;
155
156 kref_init(&dreq->kref);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400157 kref_get(&dreq->kref);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500158 init_completion(&dreq->completion);
Trond Myklebustfad61492006-03-20 13:44:36 -0500159 INIT_LIST_HEAD(&dreq->rewrite_list);
Chuck Lever93619e52006-03-20 13:44:31 -0500160 dreq->iocb = NULL;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500161 dreq->ctx = NULL;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500162 spin_lock_init(&dreq->lock);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400163 atomic_set(&dreq->io_count, 0);
Chuck Lever15ce4a02006-03-20 13:44:34 -0500164 dreq->count = 0;
165 dreq->error = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500166 dreq->flags = 0;
Chuck Lever93619e52006-03-20 13:44:31 -0500167
168 return dreq;
169}
170
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400171static void nfs_direct_req_free(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
Trond Myklebusta8881f52006-03-20 13:44:36 -0500174
175 if (dreq->ctx != NULL)
176 put_nfs_open_context(dreq->ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 kmem_cache_free(nfs_direct_cachep, dreq);
178}
179
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400180static void nfs_direct_req_release(struct nfs_direct_req *dreq)
181{
182 kref_put(&dreq->kref, nfs_direct_req_free);
183}
184
Chuck Leverd4cc9482006-03-20 13:44:28 -0500185/*
Chuck Leverbc0fb202006-03-20 13:44:31 -0500186 * Collects and returns the final error value/byte-count.
187 */
188static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
189{
Chuck Lever15ce4a02006-03-20 13:44:34 -0500190 ssize_t result = -EIOCBQUEUED;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500191
192 /* Async requests don't wait here */
193 if (dreq->iocb)
194 goto out;
195
Matthew Wilcox150030b2007-12-06 16:24:39 -0500196 result = wait_for_completion_killable(&dreq->completion);
Chuck Leverbc0fb202006-03-20 13:44:31 -0500197
198 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500199 result = dreq->error;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500200 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500201 result = dreq->count;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500202
203out:
Chuck Leverbc0fb202006-03-20 13:44:31 -0500204 return (ssize_t) result;
205}
206
207/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400208 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
209 * the iocb is still valid here if this is a synchronous request.
Chuck Lever63ab46a2006-03-20 13:44:31 -0500210 */
211static void nfs_direct_complete(struct nfs_direct_req *dreq)
212{
Chuck Lever63ab46a2006-03-20 13:44:31 -0500213 if (dreq->iocb) {
Chuck Lever15ce4a02006-03-20 13:44:34 -0500214 long res = (long) dreq->error;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500215 if (!res)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500216 res = (long) dreq->count;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500217 aio_complete(dreq->iocb, res, 0);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500218 }
219 complete_all(&dreq->completion);
Chuck Lever63ab46a2006-03-20 13:44:31 -0500220
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400221 nfs_direct_req_release(dreq);
Chuck Lever63ab46a2006-03-20 13:44:31 -0500222}
223
224/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400225 * We must hold a reference to all the pages in this direct read request
226 * until the RPCs complete. This could be long *after* we are woken up in
227 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
Chuck Lever06cf6f22006-06-20 12:56:49 -0400228 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500229static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Trond Myklebustec06c092006-03-20 13:44:27 -0500231 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400233 nfs_readpage_result(task, data);
234}
235
236static void nfs_direct_read_release(void *calldata)
237{
238
239 struct nfs_read_data *data = calldata;
240 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
241 int status = data->task.tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Chuck Lever15ce4a02006-03-20 13:44:34 -0500243 spin_lock(&dreq->lock);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400244 if (unlikely(status < 0)) {
245 dreq->error = status;
Trond Myklebustd4a8f362007-05-22 10:22:27 -0400246 spin_unlock(&dreq->lock);
247 } else {
248 dreq->count += data->res.count;
249 spin_unlock(&dreq->lock);
250 nfs_direct_dirty_pages(data->pagevec,
251 data->args.pgbase,
252 data->res.count);
253 }
254 nfs_direct_release_pages(data->pagevec, data->npages);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400255
256 if (put_dreq(dreq))
257 nfs_direct_complete(dreq);
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400258 nfs_readdata_release(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Trond Myklebustec06c092006-03-20 13:44:27 -0500261static const struct rpc_call_ops nfs_read_direct_ops = {
262 .rpc_call_done = nfs_direct_read_result,
Trond Myklebustfdd1e742008-04-15 16:33:58 -0400263 .rpc_release = nfs_direct_read_release,
Trond Myklebustec06c092006-03-20 13:44:27 -0500264};
265
Chuck Leverd4cc9482006-03-20 13:44:28 -0500266/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400267 * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
268 * operation. If nfs_readdata_alloc() or get_user_pages() fails,
269 * bail and stop sending more reads. Read length accounting is
270 * handled automatically by nfs_direct_read_result(). Otherwise, if
271 * no requests have been sent, just return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Chuck Lever02fe4942007-11-12 12:17:03 -0500273static ssize_t nfs_direct_read_schedule_segment(struct nfs_direct_req *dreq,
274 const struct iovec *iov,
275 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500277 struct nfs_open_context *ctx = dreq->ctx;
Trond Myklebust88be9f92007-06-05 10:42:27 -0400278 struct inode *inode = ctx->path.dentry->d_inode;
Chuck Lever02fe4942007-11-12 12:17:03 -0500279 unsigned long user_addr = (unsigned long)iov->iov_base;
280 size_t count = iov->iov_len;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500281 size_t rsize = NFS_SERVER(inode)->rsize;
Trond Myklebust07737692007-10-25 18:42:54 -0400282 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400283 struct rpc_message msg = {
284 .rpc_cred = ctx->cred,
285 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400286 struct rpc_task_setup task_setup_data = {
287 .rpc_client = NFS_CLIENT(inode),
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400288 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -0400289 .callback_ops = &nfs_read_direct_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -0500290 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -0400291 .flags = RPC_TASK_ASYNC,
292 };
Trond Myklebust607f31e2006-06-28 16:52:45 -0400293 unsigned int pgbase;
294 int result;
295 ssize_t started = 0;
Chuck Lever82b145c2006-06-20 12:57:03 -0400296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 do {
Chuck Lever82b145c2006-06-20 12:57:03 -0400298 struct nfs_read_data *data;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500299 size_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700301 pgbase = user_addr & ~PAGE_MASK;
302 bytes = min(rsize,count);
303
Trond Myklebust607f31e2006-06-28 16:52:45 -0400304 result = -ENOMEM;
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400305 data = nfs_readdata_alloc(nfs_page_array_len(pgbase, bytes));
Trond Myklebust607f31e2006-06-28 16:52:45 -0400306 if (unlikely(!data))
307 break;
308
Trond Myklebust607f31e2006-06-28 16:52:45 -0400309 down_read(&current->mm->mmap_sem);
310 result = get_user_pages(current, current->mm, user_addr,
311 data->npages, 1, 0, data->pagevec, NULL);
312 up_read(&current->mm->mmap_sem);
Chuck Lever749e1462007-05-19 17:22:46 -0400313 if (result < 0) {
314 nfs_readdata_release(data);
315 break;
316 }
317 if ((unsigned)result < data->npages) {
Trond Myklebustd9df8d62007-05-22 10:22:20 -0400318 bytes = result * PAGE_SIZE;
319 if (bytes <= pgbase) {
320 nfs_direct_release_pages(data->pagevec, result);
321 nfs_readdata_release(data);
322 break;
323 }
324 bytes -= pgbase;
325 data->npages = result;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400326 }
Chuck Lever06cf6f22006-06-20 12:56:49 -0400327
Trond Myklebust607f31e2006-06-28 16:52:45 -0400328 get_dreq(dreq);
329
330 data->req = (struct nfs_page *) dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 data->inode = inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400332 data->cred = msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 data->args.fh = NFS_FH(inode);
Trond Myklebust383ba712008-02-19 20:04:20 -0500334 data->args.context = get_nfs_open_context(ctx);
Chuck Lever88467052006-03-20 13:44:34 -0500335 data->args.offset = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 data->args.pgbase = pgbase;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400337 data->args.pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 data->args.count = bytes;
339 data->res.fattr = &data->fattr;
340 data->res.eof = 0;
341 data->res.count = bytes;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400342 msg.rpc_argp = &data->args;
343 msg.rpc_resp = &data->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Trond Myklebust07737692007-10-25 18:42:54 -0400345 task_setup_data.task = &data->task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400346 task_setup_data.callback_data = data;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400347 NFS_PROTO(inode)->read_setup(data, &msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Trond Myklebust07737692007-10-25 18:42:54 -0400349 task = rpc_run_task(&task_setup_data);
350 if (!IS_ERR(task))
351 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Chuck Levera3f565b2007-01-31 12:14:01 -0500353 dprintk("NFS: %5u initiated direct read call "
354 "(req %s/%Ld, %zu bytes @ offset %Lu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 data->task.tk_pid,
356 inode->i_sb->s_id,
357 (long long)NFS_FILEID(inode),
358 bytes,
359 (unsigned long long)data->args.offset);
360
Trond Myklebust607f31e2006-06-28 16:52:45 -0400361 started += bytes;
362 user_addr += bytes;
Chuck Lever88467052006-03-20 13:44:34 -0500363 pos += bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700364 /* FIXME: Remove this unnecessary math from final patch */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 pgbase += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 pgbase &= ~PAGE_MASK;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700367 BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 count -= bytes;
370 } while (count != 0);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400371
Trond Myklebust607f31e2006-06-28 16:52:45 -0400372 if (started)
Chuck Leverc216fd72007-11-12 12:16:52 -0500373 return started;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400374 return result < 0 ? (ssize_t) result : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Chuck Lever19f73782007-11-12 12:16:47 -0500377static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
378 const struct iovec *iov,
379 unsigned long nr_segs,
380 loff_t pos)
381{
382 ssize_t result = -EINVAL;
383 size_t requested_bytes = 0;
384 unsigned long seg;
385
386 get_dreq(dreq);
387
388 for (seg = 0; seg < nr_segs; seg++) {
389 const struct iovec *vec = &iov[seg];
Chuck Lever02fe4942007-11-12 12:17:03 -0500390 result = nfs_direct_read_schedule_segment(dreq, vec, pos);
Chuck Lever19f73782007-11-12 12:16:47 -0500391 if (result < 0)
392 break;
393 requested_bytes += result;
394 if ((size_t)result < vec->iov_len)
395 break;
396 pos += vec->iov_len;
397 }
398
399 if (put_dreq(dreq))
400 nfs_direct_complete(dreq);
401
402 if (requested_bytes != 0)
403 return 0;
404
405 if (result < 0)
406 return result;
407 return -EIO;
408}
409
Chuck Leverc216fd72007-11-12 12:16:52 -0500410static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
411 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Trond Myklebust607f31e2006-06-28 16:52:45 -0400413 ssize_t result = 0;
Chuck Lever99514f82006-03-20 13:44:30 -0500414 struct inode *inode = iocb->ki_filp->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct nfs_direct_req *dreq;
416
Trond Myklebust607f31e2006-06-28 16:52:45 -0400417 dreq = nfs_direct_req_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (!dreq)
419 return -ENOMEM;
420
Chuck Lever91d5b472006-03-20 13:44:14 -0500421 dreq->inode = inode;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400422 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
Chuck Lever487b8372006-03-20 13:44:30 -0500423 if (!is_sync_kiocb(iocb))
424 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Chuck Leverc216fd72007-11-12 12:16:52 -0500426 result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400427 if (!result)
428 result = nfs_direct_wait(dreq);
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400429 nfs_direct_req_release(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 return result;
432}
433
Trond Myklebustfad61492006-03-20 13:44:36 -0500434static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Trond Myklebust607f31e2006-06-28 16:52:45 -0400436 while (!list_empty(&dreq->rewrite_list)) {
437 struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages);
Trond Myklebustfad61492006-03-20 13:44:36 -0500438 list_del(&data->pages);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400439 nfs_direct_release_pages(data->pagevec, data->npages);
Trond Myklebustfad61492006-03-20 13:44:36 -0500440 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Trond Myklebustfad61492006-03-20 13:44:36 -0500444#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
445static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Trond Myklebust607f31e2006-06-28 16:52:45 -0400447 struct inode *inode = dreq->inode;
448 struct list_head *p;
449 struct nfs_write_data *data;
Trond Myklebust07737692007-10-25 18:42:54 -0400450 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400451 struct rpc_message msg = {
452 .rpc_cred = dreq->ctx->cred,
453 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400454 struct rpc_task_setup task_setup_data = {
455 .rpc_client = NFS_CLIENT(inode),
456 .callback_ops = &nfs_write_direct_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -0500457 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -0400458 .flags = RPC_TASK_ASYNC,
459 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Trond Myklebustfad61492006-03-20 13:44:36 -0500461 dreq->count = 0;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400462 get_dreq(dreq);
Trond Myklebustfad61492006-03-20 13:44:36 -0500463
Trond Myklebust607f31e2006-06-28 16:52:45 -0400464 list_for_each(p, &dreq->rewrite_list) {
465 data = list_entry(p, struct nfs_write_data, pages);
466
467 get_dreq(dreq);
468
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400469 /* Use stable writes */
470 data->args.stable = NFS_FILE_SYNC;
471
Trond Myklebust607f31e2006-06-28 16:52:45 -0400472 /*
473 * Reset data->res.
474 */
475 nfs_fattr_init(&data->fattr);
476 data->res.count = data->args.count;
477 memset(&data->verf, 0, sizeof(data->verf));
478
479 /*
480 * Reuse data->task; data->args should not have changed
481 * since the original request was sent.
482 */
Trond Myklebust07737692007-10-25 18:42:54 -0400483 task_setup_data.task = &data->task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400484 task_setup_data.callback_data = data;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400485 msg.rpc_argp = &data->args;
486 msg.rpc_resp = &data->res;
487 NFS_PROTO(inode)->write_setup(data, &msg);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400488
Trond Myklebust607f31e2006-06-28 16:52:45 -0400489 /*
490 * We're called via an RPC callback, so BKL is already held.
491 */
Trond Myklebust07737692007-10-25 18:42:54 -0400492 task = rpc_run_task(&task_setup_data);
493 if (!IS_ERR(task))
494 rpc_put_task(task);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400495
496 dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
497 data->task.tk_pid,
498 inode->i_sb->s_id,
499 (long long)NFS_FILEID(inode),
500 data->args.count,
501 (unsigned long long)data->args.offset);
502 }
503
504 if (put_dreq(dreq))
505 nfs_direct_write_complete(dreq, inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500506}
507
508static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
509{
510 struct nfs_write_data *data = calldata;
511 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
512
513 /* Call the NFS version-specific code */
514 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
515 return;
516 if (unlikely(task->tk_status < 0)) {
Trond Myklebust60fa3f72007-04-14 19:11:52 -0400517 dprintk("NFS: %5u commit failed with error %d.\n",
518 task->tk_pid, task->tk_status);
Trond Myklebustfad61492006-03-20 13:44:36 -0500519 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
Trond Myklebust60fa3f72007-04-14 19:11:52 -0400520 } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
Trond Myklebustfad61492006-03-20 13:44:36 -0500521 dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
522 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
523 }
524
525 dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
526 nfs_direct_write_complete(dreq, data->inode);
527}
528
529static const struct rpc_call_ops nfs_commit_direct_ops = {
530 .rpc_call_done = nfs_direct_commit_result,
531 .rpc_release = nfs_commit_release,
532};
533
534static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
535{
Trond Myklebustfad61492006-03-20 13:44:36 -0500536 struct nfs_write_data *data = dreq->commit_data;
Trond Myklebust07737692007-10-25 18:42:54 -0400537 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400538 struct rpc_message msg = {
539 .rpc_argp = &data->args,
540 .rpc_resp = &data->res,
541 .rpc_cred = dreq->ctx->cred,
542 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400543 struct rpc_task_setup task_setup_data = {
Trond Myklebust07737692007-10-25 18:42:54 -0400544 .task = &data->task,
Trond Myklebust84115e12007-07-14 15:39:59 -0400545 .rpc_client = NFS_CLIENT(dreq->inode),
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400546 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -0400547 .callback_ops = &nfs_commit_direct_ops,
548 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -0500549 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -0400550 .flags = RPC_TASK_ASYNC,
551 };
Trond Myklebustfad61492006-03-20 13:44:36 -0500552
553 data->inode = dreq->inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400554 data->cred = msg.rpc_cred;
Trond Myklebustfad61492006-03-20 13:44:36 -0500555
556 data->args.fh = NFS_FH(data->inode);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400557 data->args.offset = 0;
558 data->args.count = 0;
Trond Myklebust383ba712008-02-19 20:04:20 -0500559 data->args.context = get_nfs_open_context(dreq->ctx);
Trond Myklebustfad61492006-03-20 13:44:36 -0500560 data->res.count = 0;
561 data->res.fattr = &data->fattr;
562 data->res.verf = &data->verf;
563
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400564 NFS_PROTO(data->inode)->commit_setup(data, &msg);
Trond Myklebustfad61492006-03-20 13:44:36 -0500565
Trond Myklebustfad61492006-03-20 13:44:36 -0500566 /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
567 dreq->commit_data = NULL;
568
Trond Myklebuste99170f2006-04-18 13:21:42 -0400569 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustfad61492006-03-20 13:44:36 -0500570
Trond Myklebust07737692007-10-25 18:42:54 -0400571 task = rpc_run_task(&task_setup_data);
572 if (!IS_ERR(task))
573 rpc_put_task(task);
Trond Myklebustfad61492006-03-20 13:44:36 -0500574}
575
576static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
577{
578 int flags = dreq->flags;
579
580 dreq->flags = 0;
581 switch (flags) {
582 case NFS_ODIRECT_DO_COMMIT:
583 nfs_direct_commit_schedule(dreq);
584 break;
585 case NFS_ODIRECT_RESCHED_WRITES:
586 nfs_direct_write_reschedule(dreq);
587 break;
588 default:
Trond Myklebustfad61492006-03-20 13:44:36 -0500589 if (dreq->commit_data != NULL)
590 nfs_commit_free(dreq->commit_data);
591 nfs_direct_free_writedata(dreq);
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700592 nfs_zap_mapping(inode, inode->i_mapping);
Trond Myklebustfad61492006-03-20 13:44:36 -0500593 nfs_direct_complete(dreq);
594 }
595}
596
597static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
598{
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700599 dreq->commit_data = nfs_commit_alloc();
Trond Myklebustfad61492006-03-20 13:44:36 -0500600 if (dreq->commit_data != NULL)
601 dreq->commit_data->req = (struct nfs_page *) dreq;
602}
603#else
604static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
605{
606 dreq->commit_data = NULL;
607}
608
609static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
610{
Trond Myklebustfad61492006-03-20 13:44:36 -0500611 nfs_direct_free_writedata(dreq);
Trond Myklebustcd9ae2b2006-10-19 23:28:40 -0700612 nfs_zap_mapping(inode, inode->i_mapping);
Trond Myklebustfad61492006-03-20 13:44:36 -0500613 nfs_direct_complete(dreq);
614}
615#endif
616
Chuck Lever462d5b32006-03-20 13:44:32 -0500617static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
618{
619 struct nfs_write_data *data = calldata;
620 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
621 int status = task->tk_status;
622
623 if (nfs_writeback_done(task, data) != 0)
624 return;
Chuck Lever462d5b32006-03-20 13:44:32 -0500625
Chuck Lever15ce4a02006-03-20 13:44:34 -0500626 spin_lock(&dreq->lock);
Chuck Lever462d5b32006-03-20 13:44:32 -0500627
Trond Myklebust60fa3f72007-04-14 19:11:52 -0400628 if (unlikely(status < 0)) {
Neil Brown432409e2007-10-23 17:09:13 -0400629 /* An error has occurred, so we should not commit */
Trond Myklebust60fa3f72007-04-14 19:11:52 -0400630 dreq->flags = 0;
631 dreq->error = status;
Trond Myklebusteda3cef2006-10-19 23:28:38 -0700632 }
Neil Brown432409e2007-10-23 17:09:13 -0400633 if (unlikely(dreq->error != 0))
634 goto out_unlock;
Trond Myklebusteda3cef2006-10-19 23:28:38 -0700635
636 dreq->count += data->res.count;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500637
Trond Myklebustfad61492006-03-20 13:44:36 -0500638 if (data->res.verf->committed != NFS_FILE_SYNC) {
639 switch (dreq->flags) {
640 case 0:
641 memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
642 dreq->flags = NFS_ODIRECT_DO_COMMIT;
643 break;
644 case NFS_ODIRECT_DO_COMMIT:
645 if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
646 dprintk("NFS: %5u write verify failed\n", task->tk_pid);
647 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
648 }
649 }
650 }
Trond Myklebusteda3cef2006-10-19 23:28:38 -0700651out_unlock:
Trond Myklebustfad61492006-03-20 13:44:36 -0500652 spin_unlock(&dreq->lock);
653}
654
655/*
656 * NB: Return the value of the first error return code. Subsequent
657 * errors after the first one are ignored.
658 */
659static void nfs_direct_write_release(void *calldata)
660{
661 struct nfs_write_data *data = calldata;
662 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
663
Trond Myklebust607f31e2006-06-28 16:52:45 -0400664 if (put_dreq(dreq))
665 nfs_direct_write_complete(dreq, data->inode);
Chuck Lever462d5b32006-03-20 13:44:32 -0500666}
667
668static const struct rpc_call_ops nfs_write_direct_ops = {
669 .rpc_call_done = nfs_direct_write_result,
Trond Myklebustfad61492006-03-20 13:44:36 -0500670 .rpc_release = nfs_direct_write_release,
Chuck Lever462d5b32006-03-20 13:44:32 -0500671};
672
673/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400674 * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
675 * operation. If nfs_writedata_alloc() or get_user_pages() fails,
676 * bail and stop sending more writes. Write length accounting is
677 * handled automatically by nfs_direct_write_result(). Otherwise, if
678 * no requests have been sent, just return an error.
Chuck Lever462d5b32006-03-20 13:44:32 -0500679 */
Chuck Lever02fe4942007-11-12 12:17:03 -0500680static ssize_t nfs_direct_write_schedule_segment(struct nfs_direct_req *dreq,
681 const struct iovec *iov,
682 loff_t pos, int sync)
Chuck Lever462d5b32006-03-20 13:44:32 -0500683{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500684 struct nfs_open_context *ctx = dreq->ctx;
Trond Myklebust88be9f92007-06-05 10:42:27 -0400685 struct inode *inode = ctx->path.dentry->d_inode;
Chuck Lever02fe4942007-11-12 12:17:03 -0500686 unsigned long user_addr = (unsigned long)iov->iov_base;
687 size_t count = iov->iov_len;
Trond Myklebust07737692007-10-25 18:42:54 -0400688 struct rpc_task *task;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400689 struct rpc_message msg = {
690 .rpc_cred = ctx->cred,
691 };
Trond Myklebust84115e12007-07-14 15:39:59 -0400692 struct rpc_task_setup task_setup_data = {
693 .rpc_client = NFS_CLIENT(inode),
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400694 .rpc_message = &msg,
Trond Myklebust84115e12007-07-14 15:39:59 -0400695 .callback_ops = &nfs_write_direct_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -0500696 .workqueue = nfsiod_workqueue,
Trond Myklebust84115e12007-07-14 15:39:59 -0400697 .flags = RPC_TASK_ASYNC,
698 };
Chuck Lever462d5b32006-03-20 13:44:32 -0500699 size_t wsize = NFS_SERVER(inode)->wsize;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400700 unsigned int pgbase;
701 int result;
702 ssize_t started = 0;
Chuck Lever82b145c2006-06-20 12:57:03 -0400703
Chuck Lever462d5b32006-03-20 13:44:32 -0500704 do {
Chuck Lever82b145c2006-06-20 12:57:03 -0400705 struct nfs_write_data *data;
Chuck Lever462d5b32006-03-20 13:44:32 -0500706 size_t bytes;
707
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700708 pgbase = user_addr & ~PAGE_MASK;
709 bytes = min(wsize,count);
710
Trond Myklebust607f31e2006-06-28 16:52:45 -0400711 result = -ENOMEM;
Trond Myklebust8d5658c2007-04-10 09:26:35 -0400712 data = nfs_writedata_alloc(nfs_page_array_len(pgbase, bytes));
Trond Myklebust607f31e2006-06-28 16:52:45 -0400713 if (unlikely(!data))
714 break;
715
Trond Myklebust607f31e2006-06-28 16:52:45 -0400716 down_read(&current->mm->mmap_sem);
717 result = get_user_pages(current, current->mm, user_addr,
718 data->npages, 0, 0, data->pagevec, NULL);
719 up_read(&current->mm->mmap_sem);
Chuck Lever749e1462007-05-19 17:22:46 -0400720 if (result < 0) {
721 nfs_writedata_release(data);
722 break;
723 }
724 if ((unsigned)result < data->npages) {
Trond Myklebustd9df8d62007-05-22 10:22:20 -0400725 bytes = result * PAGE_SIZE;
726 if (bytes <= pgbase) {
727 nfs_direct_release_pages(data->pagevec, result);
728 nfs_writedata_release(data);
729 break;
730 }
731 bytes -= pgbase;
732 data->npages = result;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400733 }
734
735 get_dreq(dreq);
736
Trond Myklebustfad61492006-03-20 13:44:36 -0500737 list_move_tail(&data->pages, &dreq->rewrite_list);
Chuck Lever462d5b32006-03-20 13:44:32 -0500738
Trond Myklebust607f31e2006-06-28 16:52:45 -0400739 data->req = (struct nfs_page *) dreq;
Chuck Lever462d5b32006-03-20 13:44:32 -0500740 data->inode = inode;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400741 data->cred = msg.rpc_cred;
Chuck Lever462d5b32006-03-20 13:44:32 -0500742 data->args.fh = NFS_FH(inode);
Trond Myklebust383ba712008-02-19 20:04:20 -0500743 data->args.context = get_nfs_open_context(ctx);
Chuck Lever88467052006-03-20 13:44:34 -0500744 data->args.offset = pos;
Chuck Lever462d5b32006-03-20 13:44:32 -0500745 data->args.pgbase = pgbase;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400746 data->args.pages = data->pagevec;
Chuck Lever462d5b32006-03-20 13:44:32 -0500747 data->args.count = bytes;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400748 data->args.stable = sync;
Chuck Lever462d5b32006-03-20 13:44:32 -0500749 data->res.fattr = &data->fattr;
750 data->res.count = bytes;
Chuck Lever47989d72006-03-20 13:44:32 -0500751 data->res.verf = &data->verf;
Chuck Lever462d5b32006-03-20 13:44:32 -0500752
Trond Myklebust07737692007-10-25 18:42:54 -0400753 task_setup_data.task = &data->task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400754 task_setup_data.callback_data = data;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400755 msg.rpc_argp = &data->args;
756 msg.rpc_resp = &data->res;
757 NFS_PROTO(inode)->write_setup(data, &msg);
Chuck Lever462d5b32006-03-20 13:44:32 -0500758
Trond Myklebust07737692007-10-25 18:42:54 -0400759 task = rpc_run_task(&task_setup_data);
760 if (!IS_ERR(task))
761 rpc_put_task(task);
Chuck Lever462d5b32006-03-20 13:44:32 -0500762
Chuck Levera3f565b2007-01-31 12:14:01 -0500763 dprintk("NFS: %5u initiated direct write call "
764 "(req %s/%Ld, %zu bytes @ offset %Lu)\n",
Chuck Lever462d5b32006-03-20 13:44:32 -0500765 data->task.tk_pid,
766 inode->i_sb->s_id,
767 (long long)NFS_FILEID(inode),
768 bytes,
769 (unsigned long long)data->args.offset);
770
Trond Myklebust607f31e2006-06-28 16:52:45 -0400771 started += bytes;
772 user_addr += bytes;
Chuck Lever88467052006-03-20 13:44:34 -0500773 pos += bytes;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700774
775 /* FIXME: Remove this useless math from the final patch */
Chuck Lever462d5b32006-03-20 13:44:32 -0500776 pgbase += bytes;
Chuck Lever462d5b32006-03-20 13:44:32 -0500777 pgbase &= ~PAGE_MASK;
Trond Myklebuste9f7bee2006-09-08 09:48:54 -0700778 BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
Chuck Lever462d5b32006-03-20 13:44:32 -0500779
780 count -= bytes;
781 } while (count != 0);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400782
Trond Myklebust607f31e2006-06-28 16:52:45 -0400783 if (started)
Chuck Leverc216fd72007-11-12 12:16:52 -0500784 return started;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400785 return result < 0 ? (ssize_t) result : -EFAULT;
Chuck Lever462d5b32006-03-20 13:44:32 -0500786}
787
Chuck Lever19f73782007-11-12 12:16:47 -0500788static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
789 const struct iovec *iov,
790 unsigned long nr_segs,
791 loff_t pos, int sync)
792{
793 ssize_t result = 0;
794 size_t requested_bytes = 0;
795 unsigned long seg;
796
797 get_dreq(dreq);
798
799 for (seg = 0; seg < nr_segs; seg++) {
800 const struct iovec *vec = &iov[seg];
Chuck Lever02fe4942007-11-12 12:17:03 -0500801 result = nfs_direct_write_schedule_segment(dreq, vec,
802 pos, sync);
Chuck Lever19f73782007-11-12 12:16:47 -0500803 if (result < 0)
804 break;
805 requested_bytes += result;
806 if ((size_t)result < vec->iov_len)
807 break;
808 pos += vec->iov_len;
809 }
810
811 if (put_dreq(dreq))
812 nfs_direct_write_complete(dreq, dreq->inode);
813
814 if (requested_bytes != 0)
815 return 0;
816
817 if (result < 0)
818 return result;
819 return -EIO;
820}
821
Chuck Leverc216fd72007-11-12 12:16:52 -0500822static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
823 unsigned long nr_segs, loff_t pos,
824 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Trond Myklebust607f31e2006-06-28 16:52:45 -0400826 ssize_t result = 0;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500827 struct inode *inode = iocb->ki_filp->f_mapping->host;
Chuck Lever462d5b32006-03-20 13:44:32 -0500828 struct nfs_direct_req *dreq;
Trond Myklebustfad61492006-03-20 13:44:36 -0500829 size_t wsize = NFS_SERVER(inode)->wsize;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400830 int sync = NFS_UNSTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Trond Myklebust607f31e2006-06-28 16:52:45 -0400832 dreq = nfs_direct_req_alloc();
Chuck Lever462d5b32006-03-20 13:44:32 -0500833 if (!dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return -ENOMEM;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400835 nfs_alloc_commit_data(dreq);
836
Trond Myklebustfad61492006-03-20 13:44:36 -0500837 if (dreq->commit_data == NULL || count < wsize)
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400838 sync = NFS_FILE_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500840 dreq->inode = inode;
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400841 dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500842 if (!is_sync_kiocb(iocb))
843 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Chuck Leverc216fd72007-11-12 12:16:52 -0500845 result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, sync);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400846 if (!result)
847 result = nfs_direct_wait(dreq);
Trond Myklebustb4946ff2007-05-30 12:58:00 -0400848 nfs_direct_req_release(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return result;
851}
852
853/**
854 * nfs_file_direct_read - file direct read operation for NFS files
855 * @iocb: target I/O control block
Badari Pulavarty027445c2006-09-30 23:28:46 -0700856 * @iov: vector of user buffers into which to read data
857 * @nr_segs: size of iov vector
Chuck Lever88467052006-03-20 13:44:34 -0500858 * @pos: byte offset in file where reading starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 *
860 * We use this function for direct reads instead of calling
861 * generic_file_aio_read() in order to avoid gfar's check to see if
862 * the request starts before the end of the file. For that check
863 * to work, we must generate a GETATTR before each direct read, and
864 * even then there is a window between the GETATTR and the subsequent
Chuck Lever88467052006-03-20 13:44:34 -0500865 * READ where the file size could change. Our preference is simply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 * to do all reads the application wants, and the server will take
867 * care of managing the end of file boundary.
Chuck Lever88467052006-03-20 13:44:34 -0500868 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 * This function also eliminates unnecessarily updating the file's
870 * atime locally, as the NFS server sets the file's atime, and this
871 * client must read the updated atime from the server back into its
872 * cache.
873 */
Badari Pulavarty027445c2006-09-30 23:28:46 -0700874ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
875 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 ssize_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct address_space *mapping = file->f_mapping;
Chuck Leverc216fd72007-11-12 12:16:52 -0500880 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Chuck Leverc216fd72007-11-12 12:16:52 -0500882 count = iov_length(iov, nr_segs);
883 nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
884
885 dprintk("nfs: direct read(%s/%s, %zd@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800886 file->f_path.dentry->d_parent->d_name.name,
887 file->f_path.dentry->d_name.name,
Chuck Leverc216fd72007-11-12 12:16:52 -0500888 count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 retval = 0;
891 if (!count)
892 goto out;
893
Trond Myklebust29884df2005-12-13 16:13:54 -0500894 retval = nfs_sync_mapping(mapping);
895 if (retval)
896 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Chuck Leverc216fd72007-11-12 12:16:52 -0500898 retval = nfs_direct_read(iocb, iov, nr_segs, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (retval > 0)
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500900 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902out:
903 return retval;
904}
905
906/**
907 * nfs_file_direct_write - file direct write operation for NFS files
908 * @iocb: target I/O control block
Badari Pulavarty027445c2006-09-30 23:28:46 -0700909 * @iov: vector of user buffers from which to write data
910 * @nr_segs: size of iov vector
Chuck Lever88467052006-03-20 13:44:34 -0500911 * @pos: byte offset in file where writing starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 *
913 * We use this function for direct writes instead of calling
914 * generic_file_aio_write() in order to avoid taking the inode
915 * semaphore and updating the i_size. The NFS server will set
916 * the new i_size and this client must read the updated size
917 * back into its cache. We let the server do generic write
918 * parameter checking and report problems.
919 *
920 * We also avoid an unnecessary invocation of generic_osync_inode(),
921 * as it is fairly meaningless to sync the metadata of an NFS file.
922 *
923 * We eliminate local atime updates, see direct read above.
924 *
925 * We avoid unnecessary page cache invalidations for normal cached
926 * readers of this file.
927 *
928 * Note that O_APPEND is not supported for NFS direct writes, as there
929 * is no atomic O_APPEND write facility in the NFS protocol.
930 */
Badari Pulavarty027445c2006-09-30 23:28:46 -0700931ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
932 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Chuck Lever070ea602007-05-19 17:22:52 -0400934 ssize_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct address_space *mapping = file->f_mapping;
Chuck Leverc216fd72007-11-12 12:16:52 -0500937 size_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Chuck Leverc216fd72007-11-12 12:16:52 -0500939 count = iov_length(iov, nr_segs);
940 nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
941
942 dfprintk(VFS, "nfs: direct write(%s/%s, %zd@%Ld)\n",
Josef "Jeff" Sipek01cce932006-12-08 02:36:40 -0800943 file->f_path.dentry->d_parent->d_name.name,
944 file->f_path.dentry->d_name.name,
Chuck Leverc216fd72007-11-12 12:16:52 -0500945 count, (long long) pos);
Badari Pulavarty027445c2006-09-30 23:28:46 -0700946
Chuck Leverce1a8e62005-11-30 18:08:17 -0500947 retval = generic_write_checks(file, &pos, &count, 0);
948 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500950
951 retval = -EINVAL;
952 if ((ssize_t) count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 retval = 0;
955 if (!count)
956 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500957
Trond Myklebust29884df2005-12-13 16:13:54 -0500958 retval = nfs_sync_mapping(mapping);
959 if (retval)
960 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Chuck Leverc216fd72007-11-12 12:16:52 -0500962 retval = nfs_direct_write(iocb, iov, nr_segs, pos, count);
Chuck Lever9eafa8c2006-03-20 13:44:33 -0500963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (retval > 0)
Chuck Leverce1a8e62005-11-30 18:08:17 -0500965 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967out:
968 return retval;
969}
970
Chuck Lever88467052006-03-20 13:44:34 -0500971/**
972 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
973 *
974 */
David Howellsf7b422b2006-06-09 09:34:33 -0400975int __init nfs_init_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
978 sizeof(struct nfs_direct_req),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800979 0, (SLAB_RECLAIM_ACCOUNT|
980 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900981 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (nfs_direct_cachep == NULL)
983 return -ENOMEM;
984
985 return 0;
986}
987
Chuck Lever88467052006-03-20 13:44:34 -0500988/**
David Howellsf7b422b2006-06-09 09:34:33 -0400989 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
Chuck Lever88467052006-03-20 13:44:34 -0500990 *
991 */
David Brownell266bee82006-06-27 12:59:15 -0700992void nfs_destroy_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700994 kmem_cache_destroy(nfs_direct_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}