blob: d78c61a41ec3b53912b4d952ee4c4470150a173f [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 */
Trond Myklebustfad61492006-03-20 13:44:36 -050076 unsigned long user_addr; /* location of user's buffer */
77 size_t user_count; /* total bytes to move */
78 loff_t pos; /* starting offset in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 struct page ** pages; /* pages in our buffer */
80 unsigned int npages; /* count of pages */
Chuck Lever15ce4a02006-03-20 13:44:34 -050081
82 /* completion state */
Chuck Leverb1c59212006-06-20 12:55:19 -040083 atomic_t io_count; /* i/os we're waiting for */
Chuck Lever15ce4a02006-03-20 13:44:34 -050084 spinlock_t lock; /* protect completion state */
Chuck Lever15ce4a02006-03-20 13:44:34 -050085 ssize_t count, /* bytes actually processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 error; /* any reported error */
Trond Myklebustd72b7a62006-03-20 13:44:43 -050087 struct completion completion; /* wait for i/o completion */
Trond Myklebustfad61492006-03-20 13:44:36 -050088
89 /* commit state */
90 struct nfs_write_data * commit_data; /* special write_data for commits */
91 int flags;
92#define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
93#define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
94 struct nfs_writeverf verf; /* unstable write verifier */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
Trond Myklebustfad61492006-03-20 13:44:36 -050097static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync);
98static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Chuck Leverb1c59212006-06-20 12:55:19 -0400100static inline void get_dreq(struct nfs_direct_req *dreq)
101{
102 atomic_inc(&dreq->io_count);
103}
104
105static inline int put_dreq(struct nfs_direct_req *dreq)
106{
107 return atomic_dec_and_test(&dreq->io_count);
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/**
Chuck Leverb8a32e22006-03-20 13:44:28 -0500111 * nfs_direct_IO - NFS address space operation for direct I/O
112 * @rw: direction (read or write)
113 * @iocb: target I/O control block
114 * @iov: array of vectors that define I/O buffer
115 * @pos: offset in file to begin the operation
116 * @nr_segs: size of iovec array
117 *
118 * The presence of this routine in the address space ops vector means
119 * the NFS client supports direct I/O. However, we shunt off direct
120 * read and write requests before the VFS gets them, so this method
121 * should never be called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
Chuck Leverb8a32e22006-03-20 13:44:28 -0500123ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
124{
Chuck Leverb8a32e22006-03-20 13:44:28 -0500125 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
Trond Myklebuste99170f2006-04-18 13:21:42 -0400126 iocb->ki_filp->f_dentry->d_name.name,
127 (long long) pos, nr_segs);
Chuck Leverb8a32e22006-03-20 13:44:28 -0500128
129 return -EINVAL;
130}
131
Trond Myklebust6b45d852006-03-20 13:44:43 -0500132static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty)
133{
134 int i;
135 for (i = 0; i < npages; i++) {
136 struct page *page = pages[i];
137 if (do_dirty && !PageCompound(page))
138 set_page_dirty_lock(page);
139 page_cache_release(page);
140 }
141 kfree(pages);
142}
143
Chuck Leverd4cc9482006-03-20 13:44:28 -0500144static 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 -0700145{
146 int result = -ENOMEM;
147 unsigned long page_count;
148 size_t array_size;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
151 page_count -= user_addr >> PAGE_SHIFT;
152
153 array_size = (page_count * sizeof(struct page *));
154 *pages = kmalloc(array_size, GFP_KERNEL);
155 if (*pages) {
156 down_read(&current->mm->mmap_sem);
157 result = get_user_pages(current, current->mm, user_addr,
158 page_count, (rw == READ), 0,
159 *pages, NULL);
160 up_read(&current->mm->mmap_sem);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500161 if (result != page_count) {
162 /*
163 * If we got fewer pages than expected from
164 * get_user_pages(), the user buffer runs off the
165 * end of a mapping; return EFAULT.
166 */
167 if (result >= 0) {
168 nfs_free_user_pages(*pages, result, 0);
169 result = -EFAULT;
170 } else
171 kfree(*pages);
Trond Myklebust143f4122006-03-13 21:20:46 -0800172 *pages = NULL;
Trond Myklebust143f4122006-03-13 21:20:46 -0800173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175 return result;
176}
177
Chuck Lever93619e52006-03-20 13:44:31 -0500178static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct nfs_direct_req *dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
183 if (!dreq)
184 return NULL;
185
186 kref_init(&dreq->kref);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500187 init_completion(&dreq->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 INIT_LIST_HEAD(&dreq->list);
Trond Myklebustfad61492006-03-20 13:44:36 -0500189 INIT_LIST_HEAD(&dreq->rewrite_list);
Chuck Lever93619e52006-03-20 13:44:31 -0500190 dreq->iocb = NULL;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500191 dreq->ctx = NULL;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500192 spin_lock_init(&dreq->lock);
Chuck Leverb1c59212006-06-20 12:55:19 -0400193 atomic_set(&dreq->io_count, 0);
Chuck Lever15ce4a02006-03-20 13:44:34 -0500194 dreq->count = 0;
195 dreq->error = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500196 dreq->flags = 0;
Chuck Lever93619e52006-03-20 13:44:31 -0500197
198 return dreq;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static void nfs_direct_req_release(struct kref *kref)
202{
203 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
Trond Myklebusta8881f52006-03-20 13:44:36 -0500204
205 if (dreq->ctx != NULL)
206 put_nfs_open_context(dreq->ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 kmem_cache_free(nfs_direct_cachep, dreq);
208}
209
Chuck Leverd4cc9482006-03-20 13:44:28 -0500210/*
Chuck Leverbc0fb202006-03-20 13:44:31 -0500211 * Collects and returns the final error value/byte-count.
212 */
213static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
214{
Chuck Lever15ce4a02006-03-20 13:44:34 -0500215 ssize_t result = -EIOCBQUEUED;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500216
217 /* Async requests don't wait here */
218 if (dreq->iocb)
219 goto out;
220
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500221 result = wait_for_completion_interruptible(&dreq->completion);
Chuck Leverbc0fb202006-03-20 13:44:31 -0500222
223 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500224 result = dreq->error;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500225 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500226 result = dreq->count;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500227
228out:
229 kref_put(&dreq->kref, nfs_direct_req_release);
230 return (ssize_t) result;
231}
232
233/*
Chuck Lever63ab46a2006-03-20 13:44:31 -0500234 * We must hold a reference to all the pages in this direct read request
235 * until the RPCs complete. This could be long *after* we are woken up in
236 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
237 *
238 * In addition, synchronous I/O uses a stack-allocated iocb. Thus we
239 * can't trust the iocb is still valid here if this is a synchronous
240 * request. If the waiter is woken prematurely, the iocb is long gone.
241 */
242static void nfs_direct_complete(struct nfs_direct_req *dreq)
243{
244 nfs_free_user_pages(dreq->pages, dreq->npages, 1);
245
246 if (dreq->iocb) {
Chuck Lever15ce4a02006-03-20 13:44:34 -0500247 long res = (long) dreq->error;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500248 if (!res)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500249 res = (long) dreq->count;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500250 aio_complete(dreq->iocb, res, 0);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500251 }
252 complete_all(&dreq->completion);
Chuck Lever63ab46a2006-03-20 13:44:31 -0500253
254 kref_put(&dreq->kref, nfs_direct_req_release);
255}
256
257/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * Note we also set the number of requests we have in the dreq when we are
259 * done. This prevents races with I/O completion so we will always wait
260 * until all requests have been dispatched and completed.
261 */
Chuck Lever5dd602f2006-03-20 13:44:29 -0500262static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 struct list_head *list;
265 struct nfs_direct_req *dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
267
Chuck Lever93619e52006-03-20 13:44:31 -0500268 dreq = nfs_direct_req_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!dreq)
270 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 list = &dreq->list;
273 for(;;) {
Chuck Lever40859d72005-11-30 18:09:02 -0500274 struct nfs_read_data *data = nfs_readdata_alloc(rpages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 if (unlikely(!data)) {
277 while (!list_empty(list)) {
278 data = list_entry(list->next,
279 struct nfs_read_data, pages);
280 list_del(&data->pages);
281 nfs_readdata_free(data);
282 }
283 kref_put(&dreq->kref, nfs_direct_req_release);
284 return NULL;
285 }
286
287 INIT_LIST_HEAD(&data->pages);
288 list_add(&data->pages, list);
289
290 data->req = (struct nfs_page *) dreq;
Chuck Leverb1c59212006-06-20 12:55:19 -0400291 get_dreq(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (nbytes <= rsize)
293 break;
294 nbytes -= rsize;
295 }
296 kref_get(&dreq->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return dreq;
298}
299
Trond Myklebustec06c092006-03-20 13:44:27 -0500300static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Trond Myklebustec06c092006-03-20 13:44:27 -0500302 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
304
Trond Myklebustec06c092006-03-20 13:44:27 -0500305 if (nfs_readpage_result(task, data) != 0)
306 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Chuck Lever15ce4a02006-03-20 13:44:34 -0500308 spin_lock(&dreq->lock);
309
310 if (likely(task->tk_status >= 0))
311 dreq->count += data->res.count;
312 else
313 dreq->error = task->tk_status;
314
Chuck Lever15ce4a02006-03-20 13:44:34 -0500315 spin_unlock(&dreq->lock);
Chuck Leverb1c59212006-06-20 12:55:19 -0400316
317 if (put_dreq(dreq))
318 nfs_direct_complete(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Trond Myklebustec06c092006-03-20 13:44:27 -0500321static const struct rpc_call_ops nfs_read_direct_ops = {
322 .rpc_call_done = nfs_direct_read_result,
323 .rpc_release = nfs_readdata_release,
324};
325
Chuck Leverd4cc9482006-03-20 13:44:28 -0500326/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * For each nfs_read_data struct that was allocated on the list, dispatch
328 * an NFS READ operation
329 */
Trond Myklebustfad61492006-03-20 13:44:36 -0500330static void nfs_direct_read_schedule(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500332 struct nfs_open_context *ctx = dreq->ctx;
333 struct inode *inode = ctx->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct list_head *list = &dreq->list;
335 struct page **pages = dreq->pages;
Trond Myklebustfad61492006-03-20 13:44:36 -0500336 size_t count = dreq->user_count;
337 loff_t pos = dreq->pos;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500338 size_t rsize = NFS_SERVER(inode)->rsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 unsigned int curpage, pgbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 curpage = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500342 pgbase = dreq->user_addr & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 do {
344 struct nfs_read_data *data;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500345 size_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 bytes = rsize;
348 if (count < rsize)
349 bytes = count;
350
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500351 BUG_ON(list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 data = list_entry(list->next, struct nfs_read_data, pages);
353 list_del_init(&data->pages);
354
355 data->inode = inode;
356 data->cred = ctx->cred;
357 data->args.fh = NFS_FH(inode);
358 data->args.context = ctx;
Chuck Lever88467052006-03-20 13:44:34 -0500359 data->args.offset = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 data->args.pgbase = pgbase;
361 data->args.pages = &pages[curpage];
362 data->args.count = bytes;
363 data->res.fattr = &data->fattr;
364 data->res.eof = 0;
365 data->res.count = bytes;
366
Trond Myklebustec06c092006-03-20 13:44:27 -0500367 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
368 &nfs_read_direct_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 NFS_PROTO(inode)->read_setup(data);
370
371 data->task.tk_cookie = (unsigned long) inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 lock_kernel();
374 rpc_execute(&data->task);
375 unlock_kernel();
376
Chuck Lever606bbba2006-03-20 13:44:42 -0500377 dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 data->task.tk_pid,
379 inode->i_sb->s_id,
380 (long long)NFS_FILEID(inode),
381 bytes,
382 (unsigned long long)data->args.offset);
383
Chuck Lever88467052006-03-20 13:44:34 -0500384 pos += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 pgbase += bytes;
386 curpage += pgbase >> PAGE_SHIFT;
387 pgbase &= ~PAGE_MASK;
388
389 count -= bytes;
390 } while (count != 0);
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500391 BUG_ON(!list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Chuck Lever88467052006-03-20 13:44:34 -0500394static 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 -0700395{
396 ssize_t result;
397 sigset_t oldset;
Chuck Lever99514f82006-03-20 13:44:30 -0500398 struct inode *inode = iocb->ki_filp->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct rpc_clnt *clnt = NFS_CLIENT(inode);
400 struct nfs_direct_req *dreq;
401
402 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
403 if (!dreq)
404 return -ENOMEM;
405
Trond Myklebustfad61492006-03-20 13:44:36 -0500406 dreq->user_addr = user_addr;
407 dreq->user_count = count;
408 dreq->pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 dreq->pages = pages;
410 dreq->npages = nr_pages;
Chuck Lever91d5b472006-03-20 13:44:14 -0500411 dreq->inode = inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500412 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
Chuck Lever487b8372006-03-20 13:44:30 -0500413 if (!is_sync_kiocb(iocb))
414 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Chuck Lever91d5b472006-03-20 13:44:14 -0500416 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebustfad61492006-03-20 13:44:36 -0500418 nfs_direct_read_schedule(dreq);
Chuck Leverbc0fb202006-03-20 13:44:31 -0500419 result = nfs_direct_wait(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 rpc_clnt_sigunmask(clnt, &oldset);
421
422 return result;
423}
424
Trond Myklebustfad61492006-03-20 13:44:36 -0500425static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Trond Myklebustfad61492006-03-20 13:44:36 -0500427 list_splice_init(&dreq->rewrite_list, &dreq->list);
428 while (!list_empty(&dreq->list)) {
429 struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
430 list_del(&data->pages);
431 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Trond Myklebustfad61492006-03-20 13:44:36 -0500435#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
436static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Trond Myklebustfad61492006-03-20 13:44:36 -0500438 struct list_head *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Trond Myklebustfad61492006-03-20 13:44:36 -0500440 list_splice_init(&dreq->rewrite_list, &dreq->list);
441 list_for_each(pos, &dreq->list)
Chuck Leverb1c59212006-06-20 12:55:19 -0400442 get_dreq(dreq);
Trond Myklebustfad61492006-03-20 13:44:36 -0500443 dreq->count = 0;
444
445 nfs_direct_write_schedule(dreq, FLUSH_STABLE);
446}
447
448static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
449{
450 struct nfs_write_data *data = calldata;
451 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
452
453 /* Call the NFS version-specific code */
454 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
455 return;
456 if (unlikely(task->tk_status < 0)) {
457 dreq->error = task->tk_status;
458 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
459 }
460 if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
461 dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
462 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
463 }
464
465 dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
466 nfs_direct_write_complete(dreq, data->inode);
467}
468
469static const struct rpc_call_ops nfs_commit_direct_ops = {
470 .rpc_call_done = nfs_direct_commit_result,
471 .rpc_release = nfs_commit_release,
472};
473
474static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
475{
Trond Myklebustfad61492006-03-20 13:44:36 -0500476 struct nfs_write_data *data = dreq->commit_data;
Trond Myklebustfad61492006-03-20 13:44:36 -0500477
478 data->inode = dreq->inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500479 data->cred = dreq->ctx->cred;
Trond Myklebustfad61492006-03-20 13:44:36 -0500480
481 data->args.fh = NFS_FH(data->inode);
482 data->args.offset = dreq->pos;
483 data->args.count = dreq->user_count;
484 data->res.count = 0;
485 data->res.fattr = &data->fattr;
486 data->res.verf = &data->verf;
487
488 rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
489 &nfs_commit_direct_ops, data);
490 NFS_PROTO(data->inode)->commit_setup(data, 0);
491
492 data->task.tk_priority = RPC_PRIORITY_NORMAL;
493 data->task.tk_cookie = (unsigned long)data->inode;
494 /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
495 dreq->commit_data = NULL;
496
Trond Myklebuste99170f2006-04-18 13:21:42 -0400497 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustfad61492006-03-20 13:44:36 -0500498
499 lock_kernel();
500 rpc_execute(&data->task);
501 unlock_kernel();
502}
503
504static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
505{
506 int flags = dreq->flags;
507
508 dreq->flags = 0;
509 switch (flags) {
510 case NFS_ODIRECT_DO_COMMIT:
511 nfs_direct_commit_schedule(dreq);
512 break;
513 case NFS_ODIRECT_RESCHED_WRITES:
514 nfs_direct_write_reschedule(dreq);
515 break;
516 default:
517 nfs_end_data_update(inode);
518 if (dreq->commit_data != NULL)
519 nfs_commit_free(dreq->commit_data);
520 nfs_direct_free_writedata(dreq);
521 nfs_direct_complete(dreq);
522 }
523}
524
525static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
526{
527 dreq->commit_data = nfs_commit_alloc(0);
528 if (dreq->commit_data != NULL)
529 dreq->commit_data->req = (struct nfs_page *) dreq;
530}
531#else
532static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
533{
534 dreq->commit_data = NULL;
535}
536
537static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
538{
539 nfs_end_data_update(inode);
540 nfs_direct_free_writedata(dreq);
541 nfs_direct_complete(dreq);
542}
543#endif
544
Chuck Lever462d5b32006-03-20 13:44:32 -0500545static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
546{
547 struct list_head *list;
548 struct nfs_direct_req *dreq;
Chuck Lever462d5b32006-03-20 13:44:32 -0500549 unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
550
551 dreq = nfs_direct_req_alloc();
552 if (!dreq)
553 return NULL;
554
555 list = &dreq->list;
556 for(;;) {
557 struct nfs_write_data *data = nfs_writedata_alloc(wpages);
558
559 if (unlikely(!data)) {
560 while (!list_empty(list)) {
561 data = list_entry(list->next,
562 struct nfs_write_data, pages);
563 list_del(&data->pages);
564 nfs_writedata_free(data);
565 }
566 kref_put(&dreq->kref, nfs_direct_req_release);
567 return NULL;
568 }
569
570 INIT_LIST_HEAD(&data->pages);
571 list_add(&data->pages, list);
572
573 data->req = (struct nfs_page *) dreq;
Chuck Leverb1c59212006-06-20 12:55:19 -0400574 get_dreq(dreq);
Chuck Lever462d5b32006-03-20 13:44:32 -0500575 if (nbytes <= wsize)
576 break;
577 nbytes -= wsize;
578 }
Trond Myklebustfad61492006-03-20 13:44:36 -0500579
580 nfs_alloc_commit_data(dreq);
581
Chuck Lever462d5b32006-03-20 13:44:32 -0500582 kref_get(&dreq->kref);
Chuck Lever462d5b32006-03-20 13:44:32 -0500583 return dreq;
584}
585
Chuck Lever462d5b32006-03-20 13:44:32 -0500586static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
587{
588 struct nfs_write_data *data = calldata;
589 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
590 int status = task->tk_status;
591
592 if (nfs_writeback_done(task, data) != 0)
593 return;
Chuck Lever462d5b32006-03-20 13:44:32 -0500594
Chuck Lever15ce4a02006-03-20 13:44:34 -0500595 spin_lock(&dreq->lock);
Chuck Lever462d5b32006-03-20 13:44:32 -0500596
Chuck Lever15ce4a02006-03-20 13:44:34 -0500597 if (likely(status >= 0))
598 dreq->count += data->res.count;
599 else
Trond Myklebustfad61492006-03-20 13:44:36 -0500600 dreq->error = task->tk_status;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500601
Trond Myklebustfad61492006-03-20 13:44:36 -0500602 if (data->res.verf->committed != NFS_FILE_SYNC) {
603 switch (dreq->flags) {
604 case 0:
605 memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
606 dreq->flags = NFS_ODIRECT_DO_COMMIT;
607 break;
608 case NFS_ODIRECT_DO_COMMIT:
609 if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
610 dprintk("NFS: %5u write verify failed\n", task->tk_pid);
611 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
612 }
613 }
614 }
615 /* In case we have to resend */
616 data->args.stable = NFS_FILE_SYNC;
617
618 spin_unlock(&dreq->lock);
619}
620
621/*
622 * NB: Return the value of the first error return code. Subsequent
623 * errors after the first one are ignored.
624 */
625static void nfs_direct_write_release(void *calldata)
626{
627 struct nfs_write_data *data = calldata;
628 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
629
Chuck Leverb1c59212006-06-20 12:55:19 -0400630 if (put_dreq(dreq))
631 nfs_direct_write_complete(dreq, data->inode);
Chuck Lever462d5b32006-03-20 13:44:32 -0500632}
633
634static const struct rpc_call_ops nfs_write_direct_ops = {
635 .rpc_call_done = nfs_direct_write_result,
Trond Myklebustfad61492006-03-20 13:44:36 -0500636 .rpc_release = nfs_direct_write_release,
Chuck Lever462d5b32006-03-20 13:44:32 -0500637};
638
639/*
640 * For each nfs_write_data struct that was allocated on the list, dispatch
641 * an NFS WRITE operation
Chuck Lever462d5b32006-03-20 13:44:32 -0500642 */
Trond Myklebustfad61492006-03-20 13:44:36 -0500643static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync)
Chuck Lever462d5b32006-03-20 13:44:32 -0500644{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500645 struct nfs_open_context *ctx = dreq->ctx;
646 struct inode *inode = ctx->dentry->d_inode;
Chuck Lever462d5b32006-03-20 13:44:32 -0500647 struct list_head *list = &dreq->list;
648 struct page **pages = dreq->pages;
Trond Myklebustfad61492006-03-20 13:44:36 -0500649 size_t count = dreq->user_count;
650 loff_t pos = dreq->pos;
Chuck Lever462d5b32006-03-20 13:44:32 -0500651 size_t wsize = NFS_SERVER(inode)->wsize;
652 unsigned int curpage, pgbase;
653
654 curpage = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500655 pgbase = dreq->user_addr & ~PAGE_MASK;
Chuck Lever462d5b32006-03-20 13:44:32 -0500656 do {
657 struct nfs_write_data *data;
658 size_t bytes;
659
660 bytes = wsize;
661 if (count < wsize)
662 bytes = count;
663
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500664 BUG_ON(list_empty(list));
Chuck Lever462d5b32006-03-20 13:44:32 -0500665 data = list_entry(list->next, struct nfs_write_data, pages);
Trond Myklebustfad61492006-03-20 13:44:36 -0500666 list_move_tail(&data->pages, &dreq->rewrite_list);
Chuck Lever462d5b32006-03-20 13:44:32 -0500667
668 data->inode = inode;
669 data->cred = ctx->cred;
670 data->args.fh = NFS_FH(inode);
671 data->args.context = ctx;
Chuck Lever88467052006-03-20 13:44:34 -0500672 data->args.offset = pos;
Chuck Lever462d5b32006-03-20 13:44:32 -0500673 data->args.pgbase = pgbase;
674 data->args.pages = &pages[curpage];
675 data->args.count = bytes;
676 data->res.fattr = &data->fattr;
677 data->res.count = bytes;
Chuck Lever47989d72006-03-20 13:44:32 -0500678 data->res.verf = &data->verf;
Chuck Lever462d5b32006-03-20 13:44:32 -0500679
680 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
681 &nfs_write_direct_ops, data);
Trond Myklebustfad61492006-03-20 13:44:36 -0500682 NFS_PROTO(inode)->write_setup(data, sync);
Chuck Lever462d5b32006-03-20 13:44:32 -0500683
684 data->task.tk_priority = RPC_PRIORITY_NORMAL;
685 data->task.tk_cookie = (unsigned long) inode;
686
687 lock_kernel();
688 rpc_execute(&data->task);
689 unlock_kernel();
690
Chuck Lever606bbba2006-03-20 13:44:42 -0500691 dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
Chuck Lever462d5b32006-03-20 13:44:32 -0500692 data->task.tk_pid,
693 inode->i_sb->s_id,
694 (long long)NFS_FILEID(inode),
695 bytes,
696 (unsigned long long)data->args.offset);
697
Chuck Lever88467052006-03-20 13:44:34 -0500698 pos += bytes;
Chuck Lever462d5b32006-03-20 13:44:32 -0500699 pgbase += bytes;
700 curpage += pgbase >> PAGE_SHIFT;
701 pgbase &= ~PAGE_MASK;
702
703 count -= bytes;
704 } while (count != 0);
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500705 BUG_ON(!list_empty(list));
Chuck Lever462d5b32006-03-20 13:44:32 -0500706}
707
Chuck Lever88467052006-03-20 13:44:34 -0500708static 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 -0700709{
Chuck Lever462d5b32006-03-20 13:44:32 -0500710 ssize_t result;
711 sigset_t oldset;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500712 struct inode *inode = iocb->ki_filp->f_mapping->host;
Chuck Lever462d5b32006-03-20 13:44:32 -0500713 struct rpc_clnt *clnt = NFS_CLIENT(inode);
714 struct nfs_direct_req *dreq;
Trond Myklebustfad61492006-03-20 13:44:36 -0500715 size_t wsize = NFS_SERVER(inode)->wsize;
716 int sync = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Trond Myklebustfad61492006-03-20 13:44:36 -0500718 dreq = nfs_direct_write_alloc(count, wsize);
Chuck Lever462d5b32006-03-20 13:44:32 -0500719 if (!dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return -ENOMEM;
Trond Myklebustfad61492006-03-20 13:44:36 -0500721 if (dreq->commit_data == NULL || count < wsize)
722 sync = FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Trond Myklebustfad61492006-03-20 13:44:36 -0500724 dreq->user_addr = user_addr;
725 dreq->user_count = count;
726 dreq->pos = pos;
Chuck Lever462d5b32006-03-20 13:44:32 -0500727 dreq->pages = pages;
728 dreq->npages = nr_pages;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500729 dreq->inode = inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500730 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500731 if (!is_sync_kiocb(iocb))
732 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Chuck Lever47989d72006-03-20 13:44:32 -0500734 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 nfs_begin_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Chuck Lever462d5b32006-03-20 13:44:32 -0500738 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebustfad61492006-03-20 13:44:36 -0500739 nfs_direct_write_schedule(dreq, sync);
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500740 result = nfs_direct_wait(dreq);
Chuck Lever462d5b32006-03-20 13:44:32 -0500741 rpc_clnt_sigunmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return result;
744}
745
746/**
747 * nfs_file_direct_read - file direct read operation for NFS files
748 * @iocb: target I/O control block
749 * @buf: user's buffer into which to read data
Chuck Lever88467052006-03-20 13:44:34 -0500750 * @count: number of bytes to read
751 * @pos: byte offset in file where reading starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 *
753 * We use this function for direct reads instead of calling
754 * generic_file_aio_read() in order to avoid gfar's check to see if
755 * the request starts before the end of the file. For that check
756 * to work, we must generate a GETATTR before each direct read, and
757 * even then there is a window between the GETATTR and the subsequent
Chuck Lever88467052006-03-20 13:44:34 -0500758 * READ where the file size could change. Our preference is simply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * to do all reads the application wants, and the server will take
760 * care of managing the end of file boundary.
Chuck Lever88467052006-03-20 13:44:34 -0500761 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 * This function also eliminates unnecessarily updating the file's
763 * atime locally, as the NFS server sets the file's atime, and this
764 * client must read the updated atime from the server back into its
765 * cache.
766 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500767ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 ssize_t retval = -EINVAL;
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500770 int page_count;
771 struct page **pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Chuck Leverce1a8e62005-11-30 18:08:17 -0500775 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500776 file->f_dentry->d_parent->d_name.name,
777 file->f_dentry->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500778 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (count < 0)
781 goto out;
782 retval = -EFAULT;
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500783 if (!access_ok(VERIFY_WRITE, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto out;
785 retval = 0;
786 if (!count)
787 goto out;
788
Trond Myklebust29884df2005-12-13 16:13:54 -0500789 retval = nfs_sync_mapping(mapping);
790 if (retval)
791 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Trond Myklebust6b45d852006-03-20 13:44:43 -0500793 retval = nfs_get_user_pages(READ, (unsigned long) buf,
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500794 count, &pages);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500795 if (retval < 0)
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500796 goto out;
Trond Myklebust6b45d852006-03-20 13:44:43 -0500797 page_count = retval;
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500798
Chuck Lever99514f82006-03-20 13:44:30 -0500799 retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos,
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500800 pages, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (retval > 0)
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500802 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804out:
805 return retval;
806}
807
808/**
809 * nfs_file_direct_write - file direct write operation for NFS files
810 * @iocb: target I/O control block
811 * @buf: user's buffer from which to write data
Chuck Lever88467052006-03-20 13:44:34 -0500812 * @count: number of bytes to write
813 * @pos: byte offset in file where writing starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 *
815 * We use this function for direct writes instead of calling
816 * generic_file_aio_write() in order to avoid taking the inode
817 * semaphore and updating the i_size. The NFS server will set
818 * the new i_size and this client must read the updated size
819 * back into its cache. We let the server do generic write
820 * parameter checking and report problems.
821 *
822 * We also avoid an unnecessary invocation of generic_osync_inode(),
823 * as it is fairly meaningless to sync the metadata of an NFS file.
824 *
825 * We eliminate local atime updates, see direct read above.
826 *
827 * We avoid unnecessary page cache invalidations for normal cached
828 * readers of this file.
829 *
830 * Note that O_APPEND is not supported for NFS direct writes, as there
831 * is no atomic O_APPEND write facility in the NFS protocol.
832 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500833ssize_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 -0700834{
Chuck Leverce1a8e62005-11-30 18:08:17 -0500835 ssize_t retval;
Chuck Lever47989d72006-03-20 13:44:32 -0500836 int page_count;
837 struct page **pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Chuck Leverce1a8e62005-11-30 18:08:17 -0500841 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500842 file->f_dentry->d_parent->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500843 file->f_dentry->d_name.name,
844 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Chuck Leverce1a8e62005-11-30 18:08:17 -0500846 retval = generic_write_checks(file, &pos, &count, 0);
847 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500849
850 retval = -EINVAL;
851 if ((ssize_t) count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 retval = 0;
854 if (!count)
855 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500856
857 retval = -EFAULT;
Chuck Lever47989d72006-03-20 13:44:32 -0500858 if (!access_ok(VERIFY_READ, buf, count))
Chuck Leverce1a8e62005-11-30 18:08:17 -0500859 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Trond Myklebust29884df2005-12-13 16:13:54 -0500861 retval = nfs_sync_mapping(mapping);
862 if (retval)
863 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Trond Myklebust6b45d852006-03-20 13:44:43 -0500865 retval = nfs_get_user_pages(WRITE, (unsigned long) buf,
Chuck Lever47989d72006-03-20 13:44:32 -0500866 count, &pages);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500867 if (retval < 0)
Chuck Lever47989d72006-03-20 13:44:32 -0500868 goto out;
Trond Myklebust6b45d852006-03-20 13:44:43 -0500869 page_count = retval;
Chuck Lever47989d72006-03-20 13:44:32 -0500870
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500871 retval = nfs_direct_write(iocb, (unsigned long) buf, count,
Chuck Lever47989d72006-03-20 13:44:32 -0500872 pos, pages, page_count);
Chuck Lever9eafa8c2006-03-20 13:44:33 -0500873
874 /*
875 * XXX: nfs_end_data_update() already ensures this file's
876 * cached data is subsequently invalidated. Do we really
877 * need to call invalidate_inode_pages2() again here?
878 *
879 * For aio writes, this invalidation will almost certainly
880 * occur before the writes complete. Kind of racey.
881 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (mapping->nrpages)
883 invalidate_inode_pages2(mapping);
Chuck Lever9eafa8c2006-03-20 13:44:33 -0500884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (retval > 0)
Chuck Leverce1a8e62005-11-30 18:08:17 -0500886 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888out:
889 return retval;
890}
891
Chuck Lever88467052006-03-20 13:44:34 -0500892/**
893 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
894 *
895 */
David Howellsf7b422b2006-06-09 09:34:33 -0400896int __init nfs_init_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
899 sizeof(struct nfs_direct_req),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800900 0, (SLAB_RECLAIM_ACCOUNT|
901 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 NULL, NULL);
903 if (nfs_direct_cachep == NULL)
904 return -ENOMEM;
905
906 return 0;
907}
908
Chuck Lever88467052006-03-20 13:44:34 -0500909/**
David Howellsf7b422b2006-06-09 09:34:33 -0400910 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
Chuck Lever88467052006-03-20 13:44:34 -0500911 *
912 */
David Howellsf7b422b2006-06-09 09:34:33 -0400913void __exit nfs_destroy_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 if (kmem_cache_destroy(nfs_direct_cachep))
916 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
917}