blob: b1630d53fbb1fa4484ddd16c160c4cdadb210631 [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
41#include <linux/config.h>
42#include <linux/errno.h>
43#include <linux/sched.h>
44#include <linux/kernel.h>
45#include <linux/smp_lock.h>
46#include <linux/file.h>
47#include <linux/pagemap.h>
48#include <linux/kref.h>
49
50#include <linux/nfs_fs.h>
51#include <linux/nfs_page.h>
52#include <linux/sunrpc/clnt.h>
53
54#include <asm/system.h>
55#include <asm/uaccess.h>
56#include <asm/atomic.h>
57
Chuck Lever91d5b472006-03-20 13:44:14 -050058#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define NFSDBG_FACILITY NFSDBG_VFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62static kmem_cache_t *nfs_direct_cachep;
63
64/*
65 * This represents a set of asynchronous requests that we're waiting on
66 */
67struct nfs_direct_req {
68 struct kref kref; /* release manager */
Chuck Lever15ce4a02006-03-20 13:44:34 -050069
70 /* I/O parameters */
Trond Myklebustfad61492006-03-20 13:44:36 -050071 struct list_head list, /* nfs_read/write_data structs */
72 rewrite_list; /* saved nfs_write_data structs */
Trond Myklebusta8881f52006-03-20 13:44:36 -050073 struct nfs_open_context *ctx; /* file open context info */
Chuck Lever99514f82006-03-20 13:44:30 -050074 struct kiocb * iocb; /* controlling i/o request */
Chuck Lever88467052006-03-20 13:44:34 -050075 struct inode * inode; /* target file of i/o */
Chuck Lever15ce4a02006-03-20 13:44:34 -050076
77 /* completion state */
Chuck Leverb1c59212006-06-20 12:55:19 -040078 atomic_t io_count; /* i/os we're waiting for */
Chuck Lever15ce4a02006-03-20 13:44:34 -050079 spinlock_t lock; /* protect completion state */
Chuck Lever15ce4a02006-03-20 13:44:34 -050080 ssize_t count, /* bytes actually processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 error; /* any reported error */
Trond Myklebustd72b7a62006-03-20 13:44:43 -050082 struct completion completion; /* wait for i/o completion */
Trond Myklebustfad61492006-03-20 13:44:36 -050083
84 /* commit state */
85 struct nfs_write_data * commit_data; /* special write_data for commits */
86 int flags;
87#define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
88#define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
89 struct nfs_writeverf verf; /* unstable write verifier */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Trond Myklebustfad61492006-03-20 13:44:36 -050092static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
Chuck Leverfedb5952006-06-20 12:55:45 -040093static const struct rpc_call_ops nfs_write_direct_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Chuck Leverb1c59212006-06-20 12:55:19 -040095static inline void get_dreq(struct nfs_direct_req *dreq)
96{
97 atomic_inc(&dreq->io_count);
98}
99
100static inline int put_dreq(struct nfs_direct_req *dreq)
101{
102 return atomic_dec_and_test(&dreq->io_count);
103}
104
Chuck Lever06cf6f22006-06-20 12:56:49 -0400105/*
106 * "size" is never larger than rsize or wsize.
107 */
108static inline int nfs_direct_count_pages(unsigned long user_addr, size_t size)
109{
110 int page_count;
111
112 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
113 page_count -= user_addr >> PAGE_SHIFT;
114 BUG_ON(page_count < 0);
115
116 return page_count;
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/**
Chuck Leverb8a32e22006-03-20 13:44:28 -0500120 * nfs_direct_IO - NFS address space operation for direct I/O
121 * @rw: direction (read or write)
122 * @iocb: target I/O control block
123 * @iov: array of vectors that define I/O buffer
124 * @pos: offset in file to begin the operation
125 * @nr_segs: size of iovec array
126 *
127 * The presence of this routine in the address space ops vector means
128 * the NFS client supports direct I/O. However, we shunt off direct
129 * read and write requests before the VFS gets them, so this method
130 * should never be called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
Chuck Leverb8a32e22006-03-20 13:44:28 -0500132ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
133{
Chuck Leverb8a32e22006-03-20 13:44:28 -0500134 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
Trond Myklebuste99170f2006-04-18 13:21:42 -0400135 iocb->ki_filp->f_dentry->d_name.name,
136 (long long) pos, nr_segs);
Chuck Leverb8a32e22006-03-20 13:44:28 -0500137
138 return -EINVAL;
139}
140
Chuck Lever9c93ab72006-06-20 12:56:31 -0400141static void nfs_direct_dirty_pages(struct page **pages, int npages)
Trond Myklebust6b45d852006-03-20 13:44:43 -0500142{
143 int i;
144 for (i = 0; i < npages; i++) {
145 struct page *page = pages[i];
Chuck Lever9c93ab72006-06-20 12:56:31 -0400146 if (!PageCompound(page))
Trond Myklebust6b45d852006-03-20 13:44:43 -0500147 set_page_dirty_lock(page);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500148 }
Chuck Lever9c93ab72006-06-20 12:56:31 -0400149}
150
151static void nfs_direct_release_pages(struct page **pages, int npages)
152{
153 int i;
154 for (i = 0; i < npages; i++)
155 page_cache_release(pages[i]);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500156}
157
Chuck Lever93619e52006-03-20 13:44:31 -0500158static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct nfs_direct_req *dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
163 if (!dreq)
164 return NULL;
165
166 kref_init(&dreq->kref);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500167 init_completion(&dreq->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 INIT_LIST_HEAD(&dreq->list);
Trond Myklebustfad61492006-03-20 13:44:36 -0500169 INIT_LIST_HEAD(&dreq->rewrite_list);
Chuck Lever93619e52006-03-20 13:44:31 -0500170 dreq->iocb = NULL;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500171 dreq->ctx = NULL;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500172 spin_lock_init(&dreq->lock);
Chuck Leverb1c59212006-06-20 12:55:19 -0400173 atomic_set(&dreq->io_count, 0);
Chuck Lever15ce4a02006-03-20 13:44:34 -0500174 dreq->count = 0;
175 dreq->error = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500176 dreq->flags = 0;
Chuck Lever93619e52006-03-20 13:44:31 -0500177
178 return dreq;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static void nfs_direct_req_release(struct kref *kref)
182{
183 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
Trond Myklebusta8881f52006-03-20 13:44:36 -0500184
185 if (dreq->ctx != NULL)
186 put_nfs_open_context(dreq->ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kmem_cache_free(nfs_direct_cachep, dreq);
188}
189
Chuck Leverd4cc9482006-03-20 13:44:28 -0500190/*
Chuck Leverbc0fb202006-03-20 13:44:31 -0500191 * Collects and returns the final error value/byte-count.
192 */
193static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
194{
Chuck Lever15ce4a02006-03-20 13:44:34 -0500195 ssize_t result = -EIOCBQUEUED;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500196
197 /* Async requests don't wait here */
198 if (dreq->iocb)
199 goto out;
200
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500201 result = wait_for_completion_interruptible(&dreq->completion);
Chuck Leverbc0fb202006-03-20 13:44:31 -0500202
203 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500204 result = dreq->error;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500205 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500206 result = dreq->count;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500207
208out:
209 kref_put(&dreq->kref, nfs_direct_req_release);
210 return (ssize_t) result;
211}
212
213/*
Chuck Lever06cf6f22006-06-20 12:56:49 -0400214 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
215 * the iocb is still valid here if this is a synchronous request.
Chuck Lever63ab46a2006-03-20 13:44:31 -0500216 */
217static void nfs_direct_complete(struct nfs_direct_req *dreq)
218{
Chuck Lever63ab46a2006-03-20 13:44:31 -0500219 if (dreq->iocb) {
Chuck Lever15ce4a02006-03-20 13:44:34 -0500220 long res = (long) dreq->error;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500221 if (!res)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500222 res = (long) dreq->count;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500223 aio_complete(dreq->iocb, res, 0);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500224 }
225 complete_all(&dreq->completion);
Chuck Lever63ab46a2006-03-20 13:44:31 -0500226
227 kref_put(&dreq->kref, nfs_direct_req_release);
228}
229
230/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * Note we also set the number of requests we have in the dreq when we are
232 * done. This prevents races with I/O completion so we will always wait
233 * until all requests have been dispatched and completed.
234 */
Chuck Lever5dd602f2006-03-20 13:44:29 -0500235static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct list_head *list;
238 struct nfs_direct_req *dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
240
Chuck Lever93619e52006-03-20 13:44:31 -0500241 dreq = nfs_direct_req_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (!dreq)
243 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 list = &dreq->list;
246 for(;;) {
Chuck Lever40859d72005-11-30 18:09:02 -0500247 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if (unlikely(!data)) {
250 while (!list_empty(list)) {
251 data = list_entry(list->next,
252 struct nfs_read_data, pages);
253 list_del(&data->pages);
254 nfs_readdata_free(data);
255 }
256 kref_put(&dreq->kref, nfs_direct_req_release);
257 return NULL;
258 }
259
260 INIT_LIST_HEAD(&data->pages);
261 list_add(&data->pages, list);
262
263 data->req = (struct nfs_page *) dreq;
Chuck Leverb1c59212006-06-20 12:55:19 -0400264 get_dreq(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (nbytes <= rsize)
266 break;
267 nbytes -= rsize;
268 }
269 kref_get(&dreq->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return dreq;
271}
272
Chuck Lever06cf6f22006-06-20 12:56:49 -0400273/*
274 * We must hold a reference to all the pages in this direct read request
275 * until the RPCs complete. This could be long *after* we are woken up in
276 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
277 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500278static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Trond Myklebustec06c092006-03-20 13:44:27 -0500280 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
282
Trond Myklebustec06c092006-03-20 13:44:27 -0500283 if (nfs_readpage_result(task, data) != 0)
284 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Chuck Lever06cf6f22006-06-20 12:56:49 -0400286 nfs_direct_dirty_pages(data->pagevec, data->npages);
287 nfs_direct_release_pages(data->pagevec, data->npages);
288
Chuck Lever15ce4a02006-03-20 13:44:34 -0500289 spin_lock(&dreq->lock);
290
291 if (likely(task->tk_status >= 0))
292 dreq->count += data->res.count;
293 else
294 dreq->error = task->tk_status;
295
Chuck Lever15ce4a02006-03-20 13:44:34 -0500296 spin_unlock(&dreq->lock);
Chuck Leverb1c59212006-06-20 12:55:19 -0400297
Chuck Lever06cf6f22006-06-20 12:56:49 -0400298 if (put_dreq(dreq))
Chuck Leverb1c59212006-06-20 12:55:19 -0400299 nfs_direct_complete(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Trond Myklebustec06c092006-03-20 13:44:27 -0500302static const struct rpc_call_ops nfs_read_direct_ops = {
303 .rpc_call_done = nfs_direct_read_result,
304 .rpc_release = nfs_readdata_release,
305};
306
Chuck Leverd4cc9482006-03-20 13:44:28 -0500307/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * For each nfs_read_data struct that was allocated on the list, dispatch
Chuck Lever06cf6f22006-06-20 12:56:49 -0400309 * an NFS READ operation. If get_user_pages() fails, we stop sending reads.
310 * Read length accounting is handled by nfs_direct_read_result().
311 * Otherwise, if no requests have been sent, just return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Chuck Lever06cf6f22006-06-20 12:56:49 -0400313static ssize_t nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500315 struct nfs_open_context *ctx = dreq->ctx;
316 struct inode *inode = ctx->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct list_head *list = &dreq->list;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500318 size_t rsize = NFS_SERVER(inode)->rsize;
Chuck Lever06cf6f22006-06-20 12:56:49 -0400319 unsigned int pgbase;
320 int result;
321 ssize_t started = 0;
322 struct nfs_read_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Chuck Lever51a7bc62006-06-20 12:56:16 -0400324 pgbase = user_addr & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 do {
Chuck Lever5dd602f2006-03-20 13:44:29 -0500326 size_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 bytes = rsize;
329 if (count < rsize)
330 bytes = count;
331
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500332 BUG_ON(list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 data = list_entry(list->next, struct nfs_read_data, pages);
334 list_del_init(&data->pages);
335
Chuck Lever06cf6f22006-06-20 12:56:49 -0400336 data->npages = nfs_direct_count_pages(user_addr, bytes);
337 down_read(&current->mm->mmap_sem);
338 result = get_user_pages(current, current->mm, user_addr,
339 data->npages, 1, 0, data->pagevec, NULL);
340 up_read(&current->mm->mmap_sem);
341 if (unlikely(result < data->npages))
342 goto out_err;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 data->inode = inode;
345 data->cred = ctx->cred;
346 data->args.fh = NFS_FH(inode);
347 data->args.context = ctx;
Chuck Lever88467052006-03-20 13:44:34 -0500348 data->args.offset = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 data->args.pgbase = pgbase;
Chuck Lever06cf6f22006-06-20 12:56:49 -0400350 data->args.pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 data->args.count = bytes;
352 data->res.fattr = &data->fattr;
353 data->res.eof = 0;
354 data->res.count = bytes;
355
Trond Myklebustec06c092006-03-20 13:44:27 -0500356 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
357 &nfs_read_direct_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 NFS_PROTO(inode)->read_setup(data);
359
360 data->task.tk_cookie = (unsigned long) inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 lock_kernel();
363 rpc_execute(&data->task);
364 unlock_kernel();
365
Chuck Lever606bbba2006-03-20 13:44:42 -0500366 dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 data->task.tk_pid,
368 inode->i_sb->s_id,
369 (long long)NFS_FILEID(inode),
370 bytes,
371 (unsigned long long)data->args.offset);
372
Chuck Lever06cf6f22006-06-20 12:56:49 -0400373 started += bytes;
374 user_addr += bytes;
Chuck Lever88467052006-03-20 13:44:34 -0500375 pos += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 pgbase += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 pgbase &= ~PAGE_MASK;
378
379 count -= bytes;
380 } while (count != 0);
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500381 BUG_ON(!list_empty(list));
Chuck Lever06cf6f22006-06-20 12:56:49 -0400382 return 0;
383
384out_err:
385 if (result > 0)
386 nfs_direct_release_pages(data->pagevec, result);
387
388 list_add(&data->pages, list);
389 while (!list_empty(list)) {
390 data = list_entry(list->next, struct nfs_read_data, pages);
391 list_del(&data->pages);
392 nfs_readdata_free(data);
393 if (put_dreq(dreq))
394 nfs_direct_complete(dreq);
395 }
396
397 if (started)
398 return 0;
399 return result < 0 ? (ssize_t) result : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Chuck Lever06cf6f22006-06-20 12:56:49 -0400402static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 ssize_t result;
405 sigset_t oldset;
Chuck Lever99514f82006-03-20 13:44:30 -0500406 struct inode *inode = iocb->ki_filp->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct rpc_clnt *clnt = NFS_CLIENT(inode);
408 struct nfs_direct_req *dreq;
409
410 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
411 if (!dreq)
412 return -ENOMEM;
413
Chuck Lever91d5b472006-03-20 13:44:14 -0500414 dreq->inode = inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500415 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
Chuck Lever487b8372006-03-20 13:44:30 -0500416 if (!is_sync_kiocb(iocb))
417 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Chuck Lever91d5b472006-03-20 13:44:14 -0500419 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 rpc_clnt_sigmask(clnt, &oldset);
Chuck Lever06cf6f22006-06-20 12:56:49 -0400421 result = nfs_direct_read_schedule(dreq, user_addr, count, pos);
422 if (!result)
423 result = nfs_direct_wait(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 rpc_clnt_sigunmask(clnt, &oldset);
425
426 return result;
427}
428
Trond Myklebustfad61492006-03-20 13:44:36 -0500429static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Trond Myklebustfad61492006-03-20 13:44:36 -0500431 list_splice_init(&dreq->rewrite_list, &dreq->list);
432 while (!list_empty(&dreq->list)) {
433 struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
434 list_del(&data->pages);
Chuck Lever06cf6f22006-06-20 12:56:49 -0400435 nfs_direct_release_pages(data->pagevec, data->npages);
Trond Myklebustfad61492006-03-20 13:44:36 -0500436 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Trond Myklebustfad61492006-03-20 13:44:36 -0500440#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
441static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Chuck Leverfedb5952006-06-20 12:55:45 -0400443 struct inode *inode = dreq->inode;
444 struct list_head *p;
445 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Trond Myklebustfad61492006-03-20 13:44:36 -0500447 dreq->count = 0;
Chuck Leverfedb5952006-06-20 12:55:45 -0400448 get_dreq(dreq);
Trond Myklebustfad61492006-03-20 13:44:36 -0500449
Chuck Leverfedb5952006-06-20 12:55:45 -0400450 list_for_each(p, &dreq->rewrite_list) {
451 data = list_entry(p, struct nfs_write_data, pages);
452
453 get_dreq(dreq);
454
455 /*
456 * Reset data->res.
457 */
458 nfs_fattr_init(&data->fattr);
459 data->res.count = data->args.count;
460 memset(&data->verf, 0, sizeof(data->verf));
461
462 /*
463 * Reuse data->task; data->args should not have changed
464 * since the original request was sent.
465 */
466 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
467 &nfs_write_direct_ops, data);
468 NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
469
470 data->task.tk_priority = RPC_PRIORITY_NORMAL;
471 data->task.tk_cookie = (unsigned long) inode;
472
473 /*
474 * We're called via an RPC callback, so BKL is already held.
475 */
476 rpc_execute(&data->task);
477
478 dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
479 data->task.tk_pid,
480 inode->i_sb->s_id,
481 (long long)NFS_FILEID(inode),
482 data->args.count,
483 (unsigned long long)data->args.offset);
484 }
485
486 if (put_dreq(dreq))
487 nfs_direct_write_complete(dreq, inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500488}
489
490static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
491{
492 struct nfs_write_data *data = calldata;
493 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
494
495 /* Call the NFS version-specific code */
496 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
497 return;
498 if (unlikely(task->tk_status < 0)) {
499 dreq->error = task->tk_status;
500 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
501 }
502 if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
503 dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
504 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
505 }
506
507 dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
508 nfs_direct_write_complete(dreq, data->inode);
509}
510
511static const struct rpc_call_ops nfs_commit_direct_ops = {
512 .rpc_call_done = nfs_direct_commit_result,
513 .rpc_release = nfs_commit_release,
514};
515
516static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
517{
Trond Myklebustfad61492006-03-20 13:44:36 -0500518 struct nfs_write_data *data = dreq->commit_data;
Trond Myklebustfad61492006-03-20 13:44:36 -0500519
520 data->inode = dreq->inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500521 data->cred = dreq->ctx->cred;
Trond Myklebustfad61492006-03-20 13:44:36 -0500522
523 data->args.fh = NFS_FH(data->inode);
Chuck Lever51a7bc62006-06-20 12:56:16 -0400524 data->args.offset = 0;
525 data->args.count = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500526 data->res.count = 0;
527 data->res.fattr = &data->fattr;
528 data->res.verf = &data->verf;
529
530 rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
531 &nfs_commit_direct_ops, data);
532 NFS_PROTO(data->inode)->commit_setup(data, 0);
533
534 data->task.tk_priority = RPC_PRIORITY_NORMAL;
535 data->task.tk_cookie = (unsigned long)data->inode;
536 /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
537 dreq->commit_data = NULL;
538
Trond Myklebuste99170f2006-04-18 13:21:42 -0400539 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustfad61492006-03-20 13:44:36 -0500540
541 lock_kernel();
542 rpc_execute(&data->task);
543 unlock_kernel();
544}
545
546static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
547{
548 int flags = dreq->flags;
549
550 dreq->flags = 0;
551 switch (flags) {
552 case NFS_ODIRECT_DO_COMMIT:
553 nfs_direct_commit_schedule(dreq);
554 break;
555 case NFS_ODIRECT_RESCHED_WRITES:
556 nfs_direct_write_reschedule(dreq);
557 break;
558 default:
559 nfs_end_data_update(inode);
560 if (dreq->commit_data != NULL)
561 nfs_commit_free(dreq->commit_data);
562 nfs_direct_free_writedata(dreq);
563 nfs_direct_complete(dreq);
564 }
565}
566
567static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
568{
569 dreq->commit_data = nfs_commit_alloc(0);
570 if (dreq->commit_data != NULL)
571 dreq->commit_data->req = (struct nfs_page *) dreq;
572}
573#else
574static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
575{
576 dreq->commit_data = NULL;
577}
578
579static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
580{
581 nfs_end_data_update(inode);
582 nfs_direct_free_writedata(dreq);
583 nfs_direct_complete(dreq);
584}
585#endif
586
Chuck Lever462d5b32006-03-20 13:44:32 -0500587static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
588{
589 struct list_head *list;
590 struct nfs_direct_req *dreq;
Chuck Lever462d5b32006-03-20 13:44:32 -0500591 unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
592
593 dreq = nfs_direct_req_alloc();
594 if (!dreq)
595 return NULL;
596
597 list = &dreq->list;
598 for(;;) {
599 struct nfs_write_data *data = nfs_writedata_alloc(wpages);
600
601 if (unlikely(!data)) {
602 while (!list_empty(list)) {
603 data = list_entry(list->next,
604 struct nfs_write_data, pages);
605 list_del(&data->pages);
606 nfs_writedata_free(data);
607 }
608 kref_put(&dreq->kref, nfs_direct_req_release);
609 return NULL;
610 }
611
612 INIT_LIST_HEAD(&data->pages);
613 list_add(&data->pages, list);
614
615 data->req = (struct nfs_page *) dreq;
Chuck Leverb1c59212006-06-20 12:55:19 -0400616 get_dreq(dreq);
Chuck Lever462d5b32006-03-20 13:44:32 -0500617 if (nbytes <= wsize)
618 break;
619 nbytes -= wsize;
620 }
Trond Myklebustfad61492006-03-20 13:44:36 -0500621
622 nfs_alloc_commit_data(dreq);
623
Chuck Lever462d5b32006-03-20 13:44:32 -0500624 kref_get(&dreq->kref);
Chuck Lever462d5b32006-03-20 13:44:32 -0500625 return dreq;
626}
627
Chuck Lever462d5b32006-03-20 13:44:32 -0500628static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
629{
630 struct nfs_write_data *data = calldata;
631 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
632 int status = task->tk_status;
633
634 if (nfs_writeback_done(task, data) != 0)
635 return;
Chuck Lever462d5b32006-03-20 13:44:32 -0500636
Chuck Lever15ce4a02006-03-20 13:44:34 -0500637 spin_lock(&dreq->lock);
Chuck Lever462d5b32006-03-20 13:44:32 -0500638
Chuck Lever15ce4a02006-03-20 13:44:34 -0500639 if (likely(status >= 0))
640 dreq->count += data->res.count;
641 else
Trond Myklebustfad61492006-03-20 13:44:36 -0500642 dreq->error = task->tk_status;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500643
Trond Myklebustfad61492006-03-20 13:44:36 -0500644 if (data->res.verf->committed != NFS_FILE_SYNC) {
645 switch (dreq->flags) {
646 case 0:
647 memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
648 dreq->flags = NFS_ODIRECT_DO_COMMIT;
649 break;
650 case NFS_ODIRECT_DO_COMMIT:
651 if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
652 dprintk("NFS: %5u write verify failed\n", task->tk_pid);
653 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
654 }
655 }
656 }
Trond Myklebustfad61492006-03-20 13:44:36 -0500657
658 spin_unlock(&dreq->lock);
659}
660
661/*
662 * NB: Return the value of the first error return code. Subsequent
663 * errors after the first one are ignored.
664 */
665static void nfs_direct_write_release(void *calldata)
666{
667 struct nfs_write_data *data = calldata;
668 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
669
Chuck Leverb1c59212006-06-20 12:55:19 -0400670 if (put_dreq(dreq))
671 nfs_direct_write_complete(dreq, data->inode);
Chuck Lever462d5b32006-03-20 13:44:32 -0500672}
673
674static const struct rpc_call_ops nfs_write_direct_ops = {
675 .rpc_call_done = nfs_direct_write_result,
Trond Myklebustfad61492006-03-20 13:44:36 -0500676 .rpc_release = nfs_direct_write_release,
Chuck Lever462d5b32006-03-20 13:44:32 -0500677};
678
679/*
680 * For each nfs_write_data struct that was allocated on the list, dispatch
Chuck Lever06cf6f22006-06-20 12:56:49 -0400681 * an NFS WRITE operation. If get_user_pages() fails, we stop sending writes.
682 * Write length accounting is handled by nfs_direct_write_result().
683 * Otherwise, if no requests have been sent, just return an error.
Chuck Lever462d5b32006-03-20 13:44:32 -0500684 */
Chuck Lever06cf6f22006-06-20 12:56:49 -0400685static ssize_t nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos, int sync)
Chuck Lever462d5b32006-03-20 13:44:32 -0500686{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500687 struct nfs_open_context *ctx = dreq->ctx;
688 struct inode *inode = ctx->dentry->d_inode;
Chuck Lever462d5b32006-03-20 13:44:32 -0500689 struct list_head *list = &dreq->list;
Chuck Lever462d5b32006-03-20 13:44:32 -0500690 size_t wsize = NFS_SERVER(inode)->wsize;
Chuck Lever06cf6f22006-06-20 12:56:49 -0400691 unsigned int pgbase;
692 int result;
693 ssize_t started = 0;
694 struct nfs_write_data *data;
Chuck Lever462d5b32006-03-20 13:44:32 -0500695
Chuck Lever51a7bc62006-06-20 12:56:16 -0400696 pgbase = user_addr & ~PAGE_MASK;
Chuck Lever462d5b32006-03-20 13:44:32 -0500697 do {
Chuck Lever462d5b32006-03-20 13:44:32 -0500698 size_t bytes;
699
700 bytes = wsize;
701 if (count < wsize)
702 bytes = count;
703
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500704 BUG_ON(list_empty(list));
Chuck Lever462d5b32006-03-20 13:44:32 -0500705 data = list_entry(list->next, struct nfs_write_data, pages);
Chuck Lever06cf6f22006-06-20 12:56:49 -0400706
707 data->npages = nfs_direct_count_pages(user_addr, bytes);
708 down_read(&current->mm->mmap_sem);
709 result = get_user_pages(current, current->mm, user_addr,
710 data->npages, 0, 0, data->pagevec, NULL);
711 up_read(&current->mm->mmap_sem);
712 if (unlikely(result < data->npages))
713 goto out_err;
714
Trond Myklebustfad61492006-03-20 13:44:36 -0500715 list_move_tail(&data->pages, &dreq->rewrite_list);
Chuck Lever462d5b32006-03-20 13:44:32 -0500716
717 data->inode = inode;
718 data->cred = ctx->cred;
719 data->args.fh = NFS_FH(inode);
720 data->args.context = ctx;
Chuck Lever88467052006-03-20 13:44:34 -0500721 data->args.offset = pos;
Chuck Lever462d5b32006-03-20 13:44:32 -0500722 data->args.pgbase = pgbase;
Chuck Lever06cf6f22006-06-20 12:56:49 -0400723 data->args.pages = data->pagevec;
Chuck Lever462d5b32006-03-20 13:44:32 -0500724 data->args.count = bytes;
725 data->res.fattr = &data->fattr;
726 data->res.count = bytes;
Chuck Lever47989d72006-03-20 13:44:32 -0500727 data->res.verf = &data->verf;
Chuck Lever462d5b32006-03-20 13:44:32 -0500728
729 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
730 &nfs_write_direct_ops, data);
Trond Myklebustfad61492006-03-20 13:44:36 -0500731 NFS_PROTO(inode)->write_setup(data, sync);
Chuck Lever462d5b32006-03-20 13:44:32 -0500732
733 data->task.tk_priority = RPC_PRIORITY_NORMAL;
734 data->task.tk_cookie = (unsigned long) inode;
735
736 lock_kernel();
737 rpc_execute(&data->task);
738 unlock_kernel();
739
Chuck Lever606bbba2006-03-20 13:44:42 -0500740 dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
Chuck Lever462d5b32006-03-20 13:44:32 -0500741 data->task.tk_pid,
742 inode->i_sb->s_id,
743 (long long)NFS_FILEID(inode),
744 bytes,
745 (unsigned long long)data->args.offset);
746
Chuck Lever06cf6f22006-06-20 12:56:49 -0400747 started += bytes;
748 user_addr += bytes;
Chuck Lever88467052006-03-20 13:44:34 -0500749 pos += bytes;
Chuck Lever462d5b32006-03-20 13:44:32 -0500750 pgbase += bytes;
Chuck Lever462d5b32006-03-20 13:44:32 -0500751 pgbase &= ~PAGE_MASK;
752
753 count -= bytes;
754 } while (count != 0);
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500755 BUG_ON(!list_empty(list));
Chuck Lever06cf6f22006-06-20 12:56:49 -0400756 return 0;
757
758out_err:
759 if (result > 0)
760 nfs_direct_release_pages(data->pagevec, result);
761
762 list_add(&data->pages, list);
763 while (!list_empty(list)) {
764 data = list_entry(list->next, struct nfs_write_data, pages);
765 list_del(&data->pages);
766 nfs_writedata_free(data);
767 if (put_dreq(dreq))
768 nfs_direct_write_complete(dreq, inode);
769 }
770
771 if (started)
772 return 0;
773 return result < 0 ? (ssize_t) result : -EFAULT;
Chuck Lever462d5b32006-03-20 13:44:32 -0500774}
775
Chuck Lever06cf6f22006-06-20 12:56:49 -0400776static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Chuck Lever462d5b32006-03-20 13:44:32 -0500778 ssize_t result;
779 sigset_t oldset;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500780 struct inode *inode = iocb->ki_filp->f_mapping->host;
Chuck Lever462d5b32006-03-20 13:44:32 -0500781 struct rpc_clnt *clnt = NFS_CLIENT(inode);
782 struct nfs_direct_req *dreq;
Trond Myklebustfad61492006-03-20 13:44:36 -0500783 size_t wsize = NFS_SERVER(inode)->wsize;
784 int sync = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Trond Myklebustfad61492006-03-20 13:44:36 -0500786 dreq = nfs_direct_write_alloc(count, wsize);
Chuck Lever462d5b32006-03-20 13:44:32 -0500787 if (!dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return -ENOMEM;
Trond Myklebustfad61492006-03-20 13:44:36 -0500789 if (dreq->commit_data == NULL || count < wsize)
790 sync = FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500792 dreq->inode = inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500793 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500794 if (!is_sync_kiocb(iocb))
795 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Chuck Lever47989d72006-03-20 13:44:32 -0500797 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 nfs_begin_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Chuck Lever462d5b32006-03-20 13:44:32 -0500801 rpc_clnt_sigmask(clnt, &oldset);
Chuck Lever06cf6f22006-06-20 12:56:49 -0400802 result = nfs_direct_write_schedule(dreq, user_addr, count, pos, sync);
803 if (!result)
804 result = nfs_direct_wait(dreq);
Chuck Lever462d5b32006-03-20 13:44:32 -0500805 rpc_clnt_sigunmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return result;
808}
809
810/**
811 * nfs_file_direct_read - file direct read operation for NFS files
812 * @iocb: target I/O control block
813 * @buf: user's buffer into which to read data
Chuck Lever88467052006-03-20 13:44:34 -0500814 * @count: number of bytes to read
815 * @pos: byte offset in file where reading starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *
817 * We use this function for direct reads instead of calling
818 * generic_file_aio_read() in order to avoid gfar's check to see if
819 * the request starts before the end of the file. For that check
820 * to work, we must generate a GETATTR before each direct read, and
821 * even then there is a window between the GETATTR and the subsequent
Chuck Lever88467052006-03-20 13:44:34 -0500822 * READ where the file size could change. Our preference is simply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 * to do all reads the application wants, and the server will take
824 * care of managing the end of file boundary.
Chuck Lever88467052006-03-20 13:44:34 -0500825 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * This function also eliminates unnecessarily updating the file's
827 * atime locally, as the NFS server sets the file's atime, and this
828 * client must read the updated atime from the server back into its
829 * cache.
830 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500831ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 ssize_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Chuck Leverce1a8e62005-11-30 18:08:17 -0500837 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500838 file->f_dentry->d_parent->d_name.name,
839 file->f_dentry->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500840 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (count < 0)
843 goto out;
844 retval = -EFAULT;
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500845 if (!access_ok(VERIFY_WRITE, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto out;
847 retval = 0;
848 if (!count)
849 goto out;
850
Trond Myklebust29884df2005-12-13 16:13:54 -0500851 retval = nfs_sync_mapping(mapping);
852 if (retval)
853 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Chuck Lever06cf6f22006-06-20 12:56:49 -0400855 retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (retval > 0)
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500857 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859out:
860 return retval;
861}
862
863/**
864 * nfs_file_direct_write - file direct write operation for NFS files
865 * @iocb: target I/O control block
866 * @buf: user's buffer from which to write data
Chuck Lever88467052006-03-20 13:44:34 -0500867 * @count: number of bytes to write
868 * @pos: byte offset in file where writing starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 *
870 * We use this function for direct writes instead of calling
871 * generic_file_aio_write() in order to avoid taking the inode
872 * semaphore and updating the i_size. The NFS server will set
873 * the new i_size and this client must read the updated size
874 * back into its cache. We let the server do generic write
875 * parameter checking and report problems.
876 *
877 * We also avoid an unnecessary invocation of generic_osync_inode(),
878 * as it is fairly meaningless to sync the metadata of an NFS file.
879 *
880 * We eliminate local atime updates, see direct read above.
881 *
882 * We avoid unnecessary page cache invalidations for normal cached
883 * readers of this file.
884 *
885 * Note that O_APPEND is not supported for NFS direct writes, as there
886 * is no atomic O_APPEND write facility in the NFS protocol.
887 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500888ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Chuck Leverce1a8e62005-11-30 18:08:17 -0500890 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Chuck Leverce1a8e62005-11-30 18:08:17 -0500894 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500895 file->f_dentry->d_parent->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500896 file->f_dentry->d_name.name,
897 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Chuck Leverce1a8e62005-11-30 18:08:17 -0500899 retval = generic_write_checks(file, &pos, &count, 0);
900 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500902
903 retval = -EINVAL;
904 if ((ssize_t) count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 retval = 0;
907 if (!count)
908 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500909
910 retval = -EFAULT;
Chuck Lever47989d72006-03-20 13:44:32 -0500911 if (!access_ok(VERIFY_READ, buf, count))
Chuck Leverce1a8e62005-11-30 18:08:17 -0500912 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Trond Myklebust29884df2005-12-13 16:13:54 -0500914 retval = nfs_sync_mapping(mapping);
915 if (retval)
916 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Chuck Lever06cf6f22006-06-20 12:56:49 -0400918 retval = nfs_direct_write(iocb, (unsigned long) buf, count, pos);
Chuck Lever9eafa8c2006-03-20 13:44:33 -0500919
920 /*
921 * XXX: nfs_end_data_update() already ensures this file's
922 * cached data is subsequently invalidated. Do we really
923 * need to call invalidate_inode_pages2() again here?
924 *
925 * For aio writes, this invalidation will almost certainly
926 * occur before the writes complete. Kind of racey.
927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (mapping->nrpages)
929 invalidate_inode_pages2(mapping);
Chuck Lever9eafa8c2006-03-20 13:44:33 -0500930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (retval > 0)
Chuck Leverce1a8e62005-11-30 18:08:17 -0500932 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934out:
935 return retval;
936}
937
Chuck Lever88467052006-03-20 13:44:34 -0500938/**
939 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
940 *
941 */
David Howellsf7b422b2006-06-09 09:34:33 -0400942int __init nfs_init_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
945 sizeof(struct nfs_direct_req),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800946 0, (SLAB_RECLAIM_ACCOUNT|
947 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 NULL, NULL);
949 if (nfs_direct_cachep == NULL)
950 return -ENOMEM;
951
952 return 0;
953}
954
Chuck Lever88467052006-03-20 13:44:34 -0500955/**
David Howellsf7b422b2006-06-09 09:34:33 -0400956 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
Chuck Lever88467052006-03-20 13:44:34 -0500957 *
958 */
David Howellsf7b422b2006-06-09 09:34:33 -0400959void __exit nfs_destroy_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 if (kmem_cache_destroy(nfs_direct_cachep))
962 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
963}