blob: e4c9e03aff121fcc60621594ed6e57f5178f5750 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct page ** pages; /* pages in our buffer */
77 unsigned int npages; /* count of pages */
Chuck Lever15ce4a02006-03-20 13:44:34 -050078
79 /* completion state */
Chuck Leverb1c59212006-06-20 12:55:19 -040080 atomic_t io_count; /* i/os we're waiting for */
Chuck Lever15ce4a02006-03-20 13:44:34 -050081 spinlock_t lock; /* protect completion state */
Chuck Lever15ce4a02006-03-20 13:44:34 -050082 ssize_t count, /* bytes actually processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 error; /* any reported error */
Trond Myklebustd72b7a62006-03-20 13:44:43 -050084 struct completion completion; /* wait for i/o completion */
Trond Myklebustfad61492006-03-20 13:44:36 -050085
86 /* commit state */
87 struct nfs_write_data * commit_data; /* special write_data for commits */
88 int flags;
89#define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
90#define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
91 struct nfs_writeverf verf; /* unstable write verifier */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
Trond Myklebustfad61492006-03-20 13:44:36 -050094static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
Chuck Leverfedb5952006-06-20 12:55:45 -040095static const struct rpc_call_ops nfs_write_direct_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Chuck Leverb1c59212006-06-20 12:55:19 -040097static inline void get_dreq(struct nfs_direct_req *dreq)
98{
99 atomic_inc(&dreq->io_count);
100}
101
102static inline int put_dreq(struct nfs_direct_req *dreq)
103{
104 return atomic_dec_and_test(&dreq->io_count);
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/**
Chuck Leverb8a32e22006-03-20 13:44:28 -0500108 * nfs_direct_IO - NFS address space operation for direct I/O
109 * @rw: direction (read or write)
110 * @iocb: target I/O control block
111 * @iov: array of vectors that define I/O buffer
112 * @pos: offset in file to begin the operation
113 * @nr_segs: size of iovec array
114 *
115 * The presence of this routine in the address space ops vector means
116 * the NFS client supports direct I/O. However, we shunt off direct
117 * read and write requests before the VFS gets them, so this method
118 * should never be called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 */
Chuck Leverb8a32e22006-03-20 13:44:28 -0500120ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
121{
Chuck Leverb8a32e22006-03-20 13:44:28 -0500122 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
Trond Myklebuste99170f2006-04-18 13:21:42 -0400123 iocb->ki_filp->f_dentry->d_name.name,
124 (long long) pos, nr_segs);
Chuck Leverb8a32e22006-03-20 13:44:28 -0500125
126 return -EINVAL;
127}
128
Trond Myklebust6b45d852006-03-20 13:44:43 -0500129static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
130{
131 int i;
132 for (i = 0; i < npages; i++) {
133 struct page *page = pages[i];
134 if (do_dirty && !PageCompound(page))
135 set_page_dirty_lock(page);
136 page_cache_release(page);
137 }
138 kfree(pages);
139}
140
Chuck Leverd4cc9482006-03-20 13:44:28 -0500141static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 int result = -ENOMEM;
144 unsigned long page_count;
145 size_t array_size;
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
148 page_count -= user_addr >> PAGE_SHIFT;
149
150 array_size = (page_count * sizeof(struct page *));
151 *pages = kmalloc(array_size, GFP_KERNEL);
152 if (*pages) {
153 down_read(&current->mm->mmap_sem);
154 result = get_user_pages(current, current->mm, user_addr,
155 page_count, (rw == READ), 0,
156 *pages, NULL);
157 up_read(&current->mm->mmap_sem);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500158 if (result != page_count) {
159 /*
160 * If we got fewer pages than expected from
161 * get_user_pages(), the user buffer runs off the
162 * end of a mapping; return EFAULT.
163 */
164 if (result >= 0) {
165 nfs_free_user_pages(*pages, result, 0);
166 result = -EFAULT;
167 } else
168 kfree(*pages);
Trond Myklebust143f4122006-03-13 21:20:46 -0800169 *pages = NULL;
Trond Myklebust143f4122006-03-13 21:20:46 -0800170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 return result;
173}
174
Chuck Lever93619e52006-03-20 13:44:31 -0500175static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct nfs_direct_req *dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
180 if (!dreq)
181 return NULL;
182
183 kref_init(&dreq->kref);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500184 init_completion(&dreq->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 INIT_LIST_HEAD(&dreq->list);
Trond Myklebustfad61492006-03-20 13:44:36 -0500186 INIT_LIST_HEAD(&dreq->rewrite_list);
Chuck Lever93619e52006-03-20 13:44:31 -0500187 dreq->iocb = NULL;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500188 dreq->ctx = NULL;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500189 spin_lock_init(&dreq->lock);
Chuck Leverb1c59212006-06-20 12:55:19 -0400190 atomic_set(&dreq->io_count, 0);
Chuck Lever15ce4a02006-03-20 13:44:34 -0500191 dreq->count = 0;
192 dreq->error = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500193 dreq->flags = 0;
Chuck Lever93619e52006-03-20 13:44:31 -0500194
195 return dreq;
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static void nfs_direct_req_release(struct kref *kref)
199{
200 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
Trond Myklebusta8881f52006-03-20 13:44:36 -0500201
202 if (dreq->ctx != NULL)
203 put_nfs_open_context(dreq->ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 kmem_cache_free(nfs_direct_cachep, dreq);
205}
206
Chuck Leverd4cc9482006-03-20 13:44:28 -0500207/*
Chuck Leverbc0fb202006-03-20 13:44:31 -0500208 * Collects and returns the final error value/byte-count.
209 */
210static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
211{
Chuck Lever15ce4a02006-03-20 13:44:34 -0500212 ssize_t result = -EIOCBQUEUED;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500213
214 /* Async requests don't wait here */
215 if (dreq->iocb)
216 goto out;
217
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500218 result = wait_for_completion_interruptible(&dreq->completion);
Chuck Leverbc0fb202006-03-20 13:44:31 -0500219
220 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500221 result = dreq->error;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500222 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500223 result = dreq->count;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500224
225out:
226 kref_put(&dreq->kref, nfs_direct_req_release);
227 return (ssize_t) result;
228}
229
230/*
Chuck Lever63ab46a2006-03-20 13:44:31 -0500231 * We must hold a reference to all the pages in this direct read request
232 * until the RPCs complete. This could be long *after* we are woken up in
233 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
234 *
235 * In addition, synchronous I/O uses a stack-allocated iocb. Thus we
236 * can't trust the iocb is still valid here if this is a synchronous
237 * request. If the waiter is woken prematurely, the iocb is long gone.
238 */
239static void nfs_direct_complete(struct nfs_direct_req *dreq)
240{
241 nfs_free_user_pages(dreq->pages, dreq->npages, 1);
242
243 if (dreq->iocb) {
Chuck Lever15ce4a02006-03-20 13:44:34 -0500244 long res = (long) dreq->error;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500245 if (!res)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500246 res = (long) dreq->count;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500247 aio_complete(dreq->iocb, res, 0);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500248 }
249 complete_all(&dreq->completion);
Chuck Lever63ab46a2006-03-20 13:44:31 -0500250
251 kref_put(&dreq->kref, nfs_direct_req_release);
252}
253
254/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * Note we also set the number of requests we have in the dreq when we are
256 * done. This prevents races with I/O completion so we will always wait
257 * until all requests have been dispatched and completed.
258 */
Chuck Lever5dd602f2006-03-20 13:44:29 -0500259static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct list_head *list;
262 struct nfs_direct_req *dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
264
Chuck Lever93619e52006-03-20 13:44:31 -0500265 dreq = nfs_direct_req_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!dreq)
267 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 list = &dreq->list;
270 for(;;) {
Chuck Lever40859d72005-11-30 18:09:02 -0500271 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (unlikely(!data)) {
274 while (!list_empty(list)) {
275 data = list_entry(list->next,
276 struct nfs_read_data, pages);
277 list_del(&data->pages);
278 nfs_readdata_free(data);
279 }
280 kref_put(&dreq->kref, nfs_direct_req_release);
281 return NULL;
282 }
283
284 INIT_LIST_HEAD(&data->pages);
285 list_add(&data->pages, list);
286
287 data->req = (struct nfs_page *) dreq;
Chuck Leverb1c59212006-06-20 12:55:19 -0400288 get_dreq(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (nbytes <= rsize)
290 break;
291 nbytes -= rsize;
292 }
293 kref_get(&dreq->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return dreq;
295}
296
Trond Myklebustec06c092006-03-20 13:44:27 -0500297static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Trond Myklebustec06c092006-03-20 13:44:27 -0500299 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
301
Trond Myklebustec06c092006-03-20 13:44:27 -0500302 if (nfs_readpage_result(task, data) != 0)
303 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Chuck Lever15ce4a02006-03-20 13:44:34 -0500305 spin_lock(&dreq->lock);
306
307 if (likely(task->tk_status >= 0))
308 dreq->count += data->res.count;
309 else
310 dreq->error = task->tk_status;
311
Chuck Lever15ce4a02006-03-20 13:44:34 -0500312 spin_unlock(&dreq->lock);
Chuck Leverb1c59212006-06-20 12:55:19 -0400313
314 if (put_dreq(dreq))
315 nfs_direct_complete(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Trond Myklebustec06c092006-03-20 13:44:27 -0500318static const struct rpc_call_ops nfs_read_direct_ops = {
319 .rpc_call_done = nfs_direct_read_result,
320 .rpc_release = nfs_readdata_release,
321};
322
Chuck Leverd4cc9482006-03-20 13:44:28 -0500323/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * For each nfs_read_data struct that was allocated on the list, dispatch
325 * an NFS READ operation
326 */
Chuck Lever51a7bc62006-06-20 12:56:16 -0400327static void 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 -0700328{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500329 struct nfs_open_context *ctx = dreq->ctx;
330 struct inode *inode = ctx->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct list_head *list = &dreq->list;
332 struct page **pages = dreq->pages;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500333 size_t rsize = NFS_SERVER(inode)->rsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 unsigned int curpage, pgbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 curpage = 0;
Chuck Lever51a7bc62006-06-20 12:56:16 -0400337 pgbase = user_addr & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 do {
339 struct nfs_read_data *data;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500340 size_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 bytes = rsize;
343 if (count < rsize)
344 bytes = count;
345
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500346 BUG_ON(list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 data = list_entry(list->next, struct nfs_read_data, pages);
348 list_del_init(&data->pages);
349
350 data->inode = inode;
351 data->cred = ctx->cred;
352 data->args.fh = NFS_FH(inode);
353 data->args.context = ctx;
Chuck Lever88467052006-03-20 13:44:34 -0500354 data->args.offset = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 data->args.pgbase = pgbase;
356 data->args.pages = &pages[curpage];
357 data->args.count = bytes;
358 data->res.fattr = &data->fattr;
359 data->res.eof = 0;
360 data->res.count = bytes;
361
Trond Myklebustec06c092006-03-20 13:44:27 -0500362 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
363 &nfs_read_direct_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 NFS_PROTO(inode)->read_setup(data);
365
366 data->task.tk_cookie = (unsigned long) inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 lock_kernel();
369 rpc_execute(&data->task);
370 unlock_kernel();
371
Chuck Lever606bbba2006-03-20 13:44:42 -0500372 dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 data->task.tk_pid,
374 inode->i_sb->s_id,
375 (long long)NFS_FILEID(inode),
376 bytes,
377 (unsigned long long)data->args.offset);
378
Chuck Lever88467052006-03-20 13:44:34 -0500379 pos += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 pgbase += bytes;
381 curpage += pgbase >> PAGE_SHIFT;
382 pgbase &= ~PAGE_MASK;
383
384 count -= bytes;
385 } while (count != 0);
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500386 BUG_ON(!list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Chuck Lever88467052006-03-20 13:44:34 -0500389static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, unsigned int nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 ssize_t result;
392 sigset_t oldset;
Chuck Lever99514f82006-03-20 13:44:30 -0500393 struct inode *inode = iocb->ki_filp->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct rpc_clnt *clnt = NFS_CLIENT(inode);
395 struct nfs_direct_req *dreq;
396
397 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
398 if (!dreq)
399 return -ENOMEM;
400
401 dreq->pages = pages;
402 dreq->npages = nr_pages;
Chuck Lever91d5b472006-03-20 13:44:14 -0500403 dreq->inode = inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500404 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
Chuck Lever487b8372006-03-20 13:44:30 -0500405 if (!is_sync_kiocb(iocb))
406 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Chuck Lever91d5b472006-03-20 13:44:14 -0500408 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 rpc_clnt_sigmask(clnt, &oldset);
Chuck Lever51a7bc62006-06-20 12:56:16 -0400410 nfs_direct_read_schedule(dreq, user_addr, count, pos);
Chuck Leverbc0fb202006-03-20 13:44:31 -0500411 result = nfs_direct_wait(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 rpc_clnt_sigunmask(clnt, &oldset);
413
414 return result;
415}
416
Trond Myklebustfad61492006-03-20 13:44:36 -0500417static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Trond Myklebustfad61492006-03-20 13:44:36 -0500419 list_splice_init(&dreq->rewrite_list, &dreq->list);
420 while (!list_empty(&dreq->list)) {
421 struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
422 list_del(&data->pages);
423 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Trond Myklebustfad61492006-03-20 13:44:36 -0500427#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
428static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Chuck Leverfedb5952006-06-20 12:55:45 -0400430 struct inode *inode = dreq->inode;
431 struct list_head *p;
432 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Trond Myklebustfad61492006-03-20 13:44:36 -0500434 dreq->count = 0;
Chuck Leverfedb5952006-06-20 12:55:45 -0400435 get_dreq(dreq);
Trond Myklebustfad61492006-03-20 13:44:36 -0500436
Chuck Leverfedb5952006-06-20 12:55:45 -0400437 list_for_each(p, &dreq->rewrite_list) {
438 data = list_entry(p, struct nfs_write_data, pages);
439
440 get_dreq(dreq);
441
442 /*
443 * Reset data->res.
444 */
445 nfs_fattr_init(&data->fattr);
446 data->res.count = data->args.count;
447 memset(&data->verf, 0, sizeof(data->verf));
448
449 /*
450 * Reuse data->task; data->args should not have changed
451 * since the original request was sent.
452 */
453 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
454 &nfs_write_direct_ops, data);
455 NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
456
457 data->task.tk_priority = RPC_PRIORITY_NORMAL;
458 data->task.tk_cookie = (unsigned long) inode;
459
460 /*
461 * We're called via an RPC callback, so BKL is already held.
462 */
463 rpc_execute(&data->task);
464
465 dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
466 data->task.tk_pid,
467 inode->i_sb->s_id,
468 (long long)NFS_FILEID(inode),
469 data->args.count,
470 (unsigned long long)data->args.offset);
471 }
472
473 if (put_dreq(dreq))
474 nfs_direct_write_complete(dreq, inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500475}
476
477static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
478{
479 struct nfs_write_data *data = calldata;
480 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
481
482 /* Call the NFS version-specific code */
483 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
484 return;
485 if (unlikely(task->tk_status < 0)) {
486 dreq->error = task->tk_status;
487 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
488 }
489 if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
490 dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
491 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
492 }
493
494 dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
495 nfs_direct_write_complete(dreq, data->inode);
496}
497
498static const struct rpc_call_ops nfs_commit_direct_ops = {
499 .rpc_call_done = nfs_direct_commit_result,
500 .rpc_release = nfs_commit_release,
501};
502
503static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
504{
Trond Myklebustfad61492006-03-20 13:44:36 -0500505 struct nfs_write_data *data = dreq->commit_data;
Trond Myklebustfad61492006-03-20 13:44:36 -0500506
507 data->inode = dreq->inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500508 data->cred = dreq->ctx->cred;
Trond Myklebustfad61492006-03-20 13:44:36 -0500509
510 data->args.fh = NFS_FH(data->inode);
Chuck Lever51a7bc62006-06-20 12:56:16 -0400511 data->args.offset = 0;
512 data->args.count = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500513 data->res.count = 0;
514 data->res.fattr = &data->fattr;
515 data->res.verf = &data->verf;
516
517 rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
518 &nfs_commit_direct_ops, data);
519 NFS_PROTO(data->inode)->commit_setup(data, 0);
520
521 data->task.tk_priority = RPC_PRIORITY_NORMAL;
522 data->task.tk_cookie = (unsigned long)data->inode;
523 /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
524 dreq->commit_data = NULL;
525
Trond Myklebuste99170f2006-04-18 13:21:42 -0400526 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustfad61492006-03-20 13:44:36 -0500527
528 lock_kernel();
529 rpc_execute(&data->task);
530 unlock_kernel();
531}
532
533static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
534{
535 int flags = dreq->flags;
536
537 dreq->flags = 0;
538 switch (flags) {
539 case NFS_ODIRECT_DO_COMMIT:
540 nfs_direct_commit_schedule(dreq);
541 break;
542 case NFS_ODIRECT_RESCHED_WRITES:
543 nfs_direct_write_reschedule(dreq);
544 break;
545 default:
546 nfs_end_data_update(inode);
547 if (dreq->commit_data != NULL)
548 nfs_commit_free(dreq->commit_data);
549 nfs_direct_free_writedata(dreq);
550 nfs_direct_complete(dreq);
551 }
552}
553
554static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
555{
556 dreq->commit_data = nfs_commit_alloc(0);
557 if (dreq->commit_data != NULL)
558 dreq->commit_data->req = (struct nfs_page *) dreq;
559}
560#else
561static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
562{
563 dreq->commit_data = NULL;
564}
565
566static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
567{
568 nfs_end_data_update(inode);
569 nfs_direct_free_writedata(dreq);
570 nfs_direct_complete(dreq);
571}
572#endif
573
Chuck Lever462d5b32006-03-20 13:44:32 -0500574static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
575{
576 struct list_head *list;
577 struct nfs_direct_req *dreq;
Chuck Lever462d5b32006-03-20 13:44:32 -0500578 unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
579
580 dreq = nfs_direct_req_alloc();
581 if (!dreq)
582 return NULL;
583
584 list = &dreq->list;
585 for(;;) {
586 struct nfs_write_data *data = nfs_writedata_alloc(wpages);
587
588 if (unlikely(!data)) {
589 while (!list_empty(list)) {
590 data = list_entry(list->next,
591 struct nfs_write_data, pages);
592 list_del(&data->pages);
593 nfs_writedata_free(data);
594 }
595 kref_put(&dreq->kref, nfs_direct_req_release);
596 return NULL;
597 }
598
599 INIT_LIST_HEAD(&data->pages);
600 list_add(&data->pages, list);
601
602 data->req = (struct nfs_page *) dreq;
Chuck Leverb1c59212006-06-20 12:55:19 -0400603 get_dreq(dreq);
Chuck Lever462d5b32006-03-20 13:44:32 -0500604 if (nbytes <= wsize)
605 break;
606 nbytes -= wsize;
607 }
Trond Myklebustfad61492006-03-20 13:44:36 -0500608
609 nfs_alloc_commit_data(dreq);
610
Chuck Lever462d5b32006-03-20 13:44:32 -0500611 kref_get(&dreq->kref);
Chuck Lever462d5b32006-03-20 13:44:32 -0500612 return dreq;
613}
614
Chuck Lever462d5b32006-03-20 13:44:32 -0500615static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
616{
617 struct nfs_write_data *data = calldata;
618 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
619 int status = task->tk_status;
620
621 if (nfs_writeback_done(task, data) != 0)
622 return;
Chuck Lever462d5b32006-03-20 13:44:32 -0500623
Chuck Lever15ce4a02006-03-20 13:44:34 -0500624 spin_lock(&dreq->lock);
Chuck Lever462d5b32006-03-20 13:44:32 -0500625
Chuck Lever15ce4a02006-03-20 13:44:34 -0500626 if (likely(status >= 0))
627 dreq->count += data->res.count;
628 else
Trond Myklebustfad61492006-03-20 13:44:36 -0500629 dreq->error = task->tk_status;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500630
Trond Myklebustfad61492006-03-20 13:44:36 -0500631 if (data->res.verf->committed != NFS_FILE_SYNC) {
632 switch (dreq->flags) {
633 case 0:
634 memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
635 dreq->flags = NFS_ODIRECT_DO_COMMIT;
636 break;
637 case NFS_ODIRECT_DO_COMMIT:
638 if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
639 dprintk("NFS: %5u write verify failed\n", task->tk_pid);
640 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
641 }
642 }
643 }
Trond Myklebustfad61492006-03-20 13:44:36 -0500644
645 spin_unlock(&dreq->lock);
646}
647
648/*
649 * NB: Return the value of the first error return code. Subsequent
650 * errors after the first one are ignored.
651 */
652static void nfs_direct_write_release(void *calldata)
653{
654 struct nfs_write_data *data = calldata;
655 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
656
Chuck Leverb1c59212006-06-20 12:55:19 -0400657 if (put_dreq(dreq))
658 nfs_direct_write_complete(dreq, data->inode);
Chuck Lever462d5b32006-03-20 13:44:32 -0500659}
660
661static const struct rpc_call_ops nfs_write_direct_ops = {
662 .rpc_call_done = nfs_direct_write_result,
Trond Myklebustfad61492006-03-20 13:44:36 -0500663 .rpc_release = nfs_direct_write_release,
Chuck Lever462d5b32006-03-20 13:44:32 -0500664};
665
666/*
667 * For each nfs_write_data struct that was allocated on the list, dispatch
668 * an NFS WRITE operation
Chuck Lever462d5b32006-03-20 13:44:32 -0500669 */
Chuck Lever51a7bc62006-06-20 12:56:16 -0400670static void 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 -0500671{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500672 struct nfs_open_context *ctx = dreq->ctx;
673 struct inode *inode = ctx->dentry->d_inode;
Chuck Lever462d5b32006-03-20 13:44:32 -0500674 struct list_head *list = &dreq->list;
675 struct page **pages = dreq->pages;
676 size_t wsize = NFS_SERVER(inode)->wsize;
677 unsigned int curpage, pgbase;
678
679 curpage = 0;
Chuck Lever51a7bc62006-06-20 12:56:16 -0400680 pgbase = user_addr & ~PAGE_MASK;
Chuck Lever462d5b32006-03-20 13:44:32 -0500681 do {
682 struct nfs_write_data *data;
683 size_t bytes;
684
685 bytes = wsize;
686 if (count < wsize)
687 bytes = count;
688
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500689 BUG_ON(list_empty(list));
Chuck Lever462d5b32006-03-20 13:44:32 -0500690 data = list_entry(list->next, struct nfs_write_data, pages);
Trond Myklebustfad61492006-03-20 13:44:36 -0500691 list_move_tail(&data->pages, &dreq->rewrite_list);
Chuck Lever462d5b32006-03-20 13:44:32 -0500692
693 data->inode = inode;
694 data->cred = ctx->cred;
695 data->args.fh = NFS_FH(inode);
696 data->args.context = ctx;
Chuck Lever88467052006-03-20 13:44:34 -0500697 data->args.offset = pos;
Chuck Lever462d5b32006-03-20 13:44:32 -0500698 data->args.pgbase = pgbase;
699 data->args.pages = &pages[curpage];
700 data->args.count = bytes;
701 data->res.fattr = &data->fattr;
702 data->res.count = bytes;
Chuck Lever47989d72006-03-20 13:44:32 -0500703 data->res.verf = &data->verf;
Chuck Lever462d5b32006-03-20 13:44:32 -0500704
705 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
706 &nfs_write_direct_ops, data);
Trond Myklebustfad61492006-03-20 13:44:36 -0500707 NFS_PROTO(inode)->write_setup(data, sync);
Chuck Lever462d5b32006-03-20 13:44:32 -0500708
709 data->task.tk_priority = RPC_PRIORITY_NORMAL;
710 data->task.tk_cookie = (unsigned long) inode;
711
712 lock_kernel();
713 rpc_execute(&data->task);
714 unlock_kernel();
715
Chuck Lever606bbba2006-03-20 13:44:42 -0500716 dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
Chuck Lever462d5b32006-03-20 13:44:32 -0500717 data->task.tk_pid,
718 inode->i_sb->s_id,
719 (long long)NFS_FILEID(inode),
720 bytes,
721 (unsigned long long)data->args.offset);
722
Chuck Lever88467052006-03-20 13:44:34 -0500723 pos += bytes;
Chuck Lever462d5b32006-03-20 13:44:32 -0500724 pgbase += bytes;
725 curpage += pgbase >> PAGE_SHIFT;
726 pgbase &= ~PAGE_MASK;
727
728 count -= bytes;
729 } while (count != 0);
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500730 BUG_ON(!list_empty(list));
Chuck Lever462d5b32006-03-20 13:44:32 -0500731}
732
Chuck Lever88467052006-03-20 13:44:34 -0500733static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, int nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Chuck Lever462d5b32006-03-20 13:44:32 -0500735 ssize_t result;
736 sigset_t oldset;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500737 struct inode *inode = iocb->ki_filp->f_mapping->host;
Chuck Lever462d5b32006-03-20 13:44:32 -0500738 struct rpc_clnt *clnt = NFS_CLIENT(inode);
739 struct nfs_direct_req *dreq;
Trond Myklebustfad61492006-03-20 13:44:36 -0500740 size_t wsize = NFS_SERVER(inode)->wsize;
741 int sync = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Trond Myklebustfad61492006-03-20 13:44:36 -0500743 dreq = nfs_direct_write_alloc(count, wsize);
Chuck Lever462d5b32006-03-20 13:44:32 -0500744 if (!dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -ENOMEM;
Trond Myklebustfad61492006-03-20 13:44:36 -0500746 if (dreq->commit_data == NULL || count < wsize)
747 sync = FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Chuck Lever462d5b32006-03-20 13:44:32 -0500749 dreq->pages = pages;
750 dreq->npages = nr_pages;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500751 dreq->inode = inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500752 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500753 if (!is_sync_kiocb(iocb))
754 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Chuck Lever47989d72006-03-20 13:44:32 -0500756 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 nfs_begin_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Chuck Lever462d5b32006-03-20 13:44:32 -0500760 rpc_clnt_sigmask(clnt, &oldset);
Chuck Lever51a7bc62006-06-20 12:56:16 -0400761 nfs_direct_write_schedule(dreq, user_addr, count, pos, sync);
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500762 result = nfs_direct_wait(dreq);
Chuck Lever462d5b32006-03-20 13:44:32 -0500763 rpc_clnt_sigunmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return result;
766}
767
768/**
769 * nfs_file_direct_read - file direct read operation for NFS files
770 * @iocb: target I/O control block
771 * @buf: user's buffer into which to read data
Chuck Lever88467052006-03-20 13:44:34 -0500772 * @count: number of bytes to read
773 * @pos: byte offset in file where reading starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
775 * We use this function for direct reads instead of calling
776 * generic_file_aio_read() in order to avoid gfar's check to see if
777 * the request starts before the end of the file. For that check
778 * to work, we must generate a GETATTR before each direct read, and
779 * even then there is a window between the GETATTR and the subsequent
Chuck Lever88467052006-03-20 13:44:34 -0500780 * READ where the file size could change. Our preference is simply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * to do all reads the application wants, and the server will take
782 * care of managing the end of file boundary.
Chuck Lever88467052006-03-20 13:44:34 -0500783 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * This function also eliminates unnecessarily updating the file's
785 * atime locally, as the NFS server sets the file's atime, and this
786 * client must read the updated atime from the server back into its
787 * cache.
788 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500789ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 ssize_t retval = -EINVAL;
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500792 int page_count;
793 struct page **pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Chuck Leverce1a8e62005-11-30 18:08:17 -0500797 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500798 file->f_dentry->d_parent->d_name.name,
799 file->f_dentry->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500800 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (count < 0)
803 goto out;
804 retval = -EFAULT;
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500805 if (!access_ok(VERIFY_WRITE, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 goto out;
807 retval = 0;
808 if (!count)
809 goto out;
810
Trond Myklebust29884df2005-12-13 16:13:54 -0500811 retval = nfs_sync_mapping(mapping);
812 if (retval)
813 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Trond Myklebust6b45d852006-03-20 13:44:43 -0500815 retval = nfs_get_user_pages(READ, (unsigned long) buf,
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500816 count, &pages);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500817 if (retval < 0)
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500818 goto out;
Trond Myklebust6b45d852006-03-20 13:44:43 -0500819 page_count = retval;
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500820
Chuck Lever99514f82006-03-20 13:44:30 -0500821 retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos,
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500822 pages, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (retval > 0)
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500824 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826out:
827 return retval;
828}
829
830/**
831 * nfs_file_direct_write - file direct write operation for NFS files
832 * @iocb: target I/O control block
833 * @buf: user's buffer from which to write data
Chuck Lever88467052006-03-20 13:44:34 -0500834 * @count: number of bytes to write
835 * @pos: byte offset in file where writing starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 *
837 * We use this function for direct writes instead of calling
838 * generic_file_aio_write() in order to avoid taking the inode
839 * semaphore and updating the i_size. The NFS server will set
840 * the new i_size and this client must read the updated size
841 * back into its cache. We let the server do generic write
842 * parameter checking and report problems.
843 *
844 * We also avoid an unnecessary invocation of generic_osync_inode(),
845 * as it is fairly meaningless to sync the metadata of an NFS file.
846 *
847 * We eliminate local atime updates, see direct read above.
848 *
849 * We avoid unnecessary page cache invalidations for normal cached
850 * readers of this file.
851 *
852 * Note that O_APPEND is not supported for NFS direct writes, as there
853 * is no atomic O_APPEND write facility in the NFS protocol.
854 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500855ssize_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 -0700856{
Chuck Leverce1a8e62005-11-30 18:08:17 -0500857 ssize_t retval;
Chuck Lever47989d72006-03-20 13:44:32 -0500858 int page_count;
859 struct page **pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Chuck Leverce1a8e62005-11-30 18:08:17 -0500863 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500864 file->f_dentry->d_parent->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500865 file->f_dentry->d_name.name,
866 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Chuck Leverce1a8e62005-11-30 18:08:17 -0500868 retval = generic_write_checks(file, &pos, &count, 0);
869 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500871
872 retval = -EINVAL;
873 if ((ssize_t) count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 retval = 0;
876 if (!count)
877 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500878
879 retval = -EFAULT;
Chuck Lever47989d72006-03-20 13:44:32 -0500880 if (!access_ok(VERIFY_READ, buf, count))
Chuck Leverce1a8e62005-11-30 18:08:17 -0500881 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Trond Myklebust29884df2005-12-13 16:13:54 -0500883 retval = nfs_sync_mapping(mapping);
884 if (retval)
885 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Trond Myklebust6b45d852006-03-20 13:44:43 -0500887 retval = nfs_get_user_pages(WRITE, (unsigned long) buf,
Chuck Lever47989d72006-03-20 13:44:32 -0500888 count, &pages);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500889 if (retval < 0)
Chuck Lever47989d72006-03-20 13:44:32 -0500890 goto out;
Trond Myklebust6b45d852006-03-20 13:44:43 -0500891 page_count = retval;
Chuck Lever47989d72006-03-20 13:44:32 -0500892
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500893 retval = nfs_direct_write(iocb, (unsigned long) buf, count,
Chuck Lever47989d72006-03-20 13:44:32 -0500894 pos, pages, page_count);
Chuck Lever9eafa8c2006-03-20 13:44:33 -0500895
896 /*
897 * XXX: nfs_end_data_update() already ensures this file's
898 * cached data is subsequently invalidated. Do we really
899 * need to call invalidate_inode_pages2() again here?
900 *
901 * For aio writes, this invalidation will almost certainly
902 * occur before the writes complete. Kind of racey.
903 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (mapping->nrpages)
905 invalidate_inode_pages2(mapping);
Chuck Lever9eafa8c2006-03-20 13:44:33 -0500906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (retval > 0)
Chuck Leverce1a8e62005-11-30 18:08:17 -0500908 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910out:
911 return retval;
912}
913
Chuck Lever88467052006-03-20 13:44:34 -0500914/**
915 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
916 *
917 */
David Howellsf7b422b2006-06-09 09:34:33 -0400918int __init nfs_init_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
921 sizeof(struct nfs_direct_req),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800922 0, (SLAB_RECLAIM_ACCOUNT|
923 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 NULL, NULL);
925 if (nfs_direct_cachep == NULL)
926 return -ENOMEM;
927
928 return 0;
929}
930
Chuck Lever88467052006-03-20 13:44:34 -0500931/**
David Howellsf7b422b2006-06-09 09:34:33 -0400932 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
Chuck Lever88467052006-03-20 13:44:34 -0500933 *
934 */
David Howellsf7b422b2006-06-09 09:34:33 -0400935void __exit nfs_destroy_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 if (kmem_cache_destroy(nfs_direct_cachep))
938 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
939}