blob: 4cb3446220ba70c121bd650ed6984458e6e0e109 [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
Chuck Lever9c93ab72006-06-20 12:56:31 -0400129static void nfs_direct_dirty_pages(struct page **pages, int npages)
Trond Myklebust6b45d852006-03-20 13:44:43 -0500130{
131 int i;
132 for (i = 0; i < npages; i++) {
133 struct page *page = pages[i];
Chuck Lever9c93ab72006-06-20 12:56:31 -0400134 if (!PageCompound(page))
Trond Myklebust6b45d852006-03-20 13:44:43 -0500135 set_page_dirty_lock(page);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500136 }
Chuck Lever9c93ab72006-06-20 12:56:31 -0400137}
138
139static void nfs_direct_release_pages(struct page **pages, int npages)
140{
141 int i;
142 for (i = 0; i < npages; i++)
143 page_cache_release(pages[i]);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500144}
145
Chuck Leverd4cc9482006-03-20 13:44:28 -0500146static 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 -0700147{
148 int result = -ENOMEM;
149 unsigned long page_count;
150 size_t array_size;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
153 page_count -= user_addr >> PAGE_SHIFT;
154
155 array_size = (page_count * sizeof(struct page *));
156 *pages = kmalloc(array_size, GFP_KERNEL);
157 if (*pages) {
158 down_read(&current->mm->mmap_sem);
159 result = get_user_pages(current, current->mm, user_addr,
160 page_count, (rw == READ), 0,
161 *pages, NULL);
162 up_read(&current->mm->mmap_sem);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500163 if (result != page_count) {
164 /*
165 * If we got fewer pages than expected from
166 * get_user_pages(), the user buffer runs off the
167 * end of a mapping; return EFAULT.
168 */
169 if (result >= 0) {
Chuck Lever9c93ab72006-06-20 12:56:31 -0400170 nfs_direct_release_pages(*pages, result);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500171 result = -EFAULT;
172 } else
173 kfree(*pages);
Trond Myklebust143f4122006-03-13 21:20:46 -0800174 *pages = NULL;
Trond Myklebust143f4122006-03-13 21:20:46 -0800175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177 return result;
178}
179
Chuck Lever93619e52006-03-20 13:44:31 -0500180static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct nfs_direct_req *dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
185 if (!dreq)
186 return NULL;
187
188 kref_init(&dreq->kref);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500189 init_completion(&dreq->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 INIT_LIST_HEAD(&dreq->list);
Trond Myklebustfad61492006-03-20 13:44:36 -0500191 INIT_LIST_HEAD(&dreq->rewrite_list);
Chuck Lever93619e52006-03-20 13:44:31 -0500192 dreq->iocb = NULL;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500193 dreq->ctx = NULL;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500194 spin_lock_init(&dreq->lock);
Chuck Leverb1c59212006-06-20 12:55:19 -0400195 atomic_set(&dreq->io_count, 0);
Chuck Lever15ce4a02006-03-20 13:44:34 -0500196 dreq->count = 0;
197 dreq->error = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500198 dreq->flags = 0;
Chuck Lever93619e52006-03-20 13:44:31 -0500199
200 return dreq;
201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static void nfs_direct_req_release(struct kref *kref)
204{
205 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
Trond Myklebusta8881f52006-03-20 13:44:36 -0500206
207 if (dreq->ctx != NULL)
208 put_nfs_open_context(dreq->ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 kmem_cache_free(nfs_direct_cachep, dreq);
210}
211
Chuck Leverd4cc9482006-03-20 13:44:28 -0500212/*
Chuck Leverbc0fb202006-03-20 13:44:31 -0500213 * Collects and returns the final error value/byte-count.
214 */
215static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
216{
Chuck Lever15ce4a02006-03-20 13:44:34 -0500217 ssize_t result = -EIOCBQUEUED;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500218
219 /* Async requests don't wait here */
220 if (dreq->iocb)
221 goto out;
222
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500223 result = wait_for_completion_interruptible(&dreq->completion);
Chuck Leverbc0fb202006-03-20 13:44:31 -0500224
225 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500226 result = dreq->error;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500227 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500228 result = dreq->count;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500229
230out:
231 kref_put(&dreq->kref, nfs_direct_req_release);
232 return (ssize_t) result;
233}
234
235/*
Chuck Lever63ab46a2006-03-20 13:44:31 -0500236 * We must hold a reference to all the pages in this direct read request
237 * until the RPCs complete. This could be long *after* we are woken up in
238 * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
239 *
240 * In addition, synchronous I/O uses a stack-allocated iocb. Thus we
241 * can't trust the iocb is still valid here if this is a synchronous
242 * request. If the waiter is woken prematurely, the iocb is long gone.
243 */
244static void nfs_direct_complete(struct nfs_direct_req *dreq)
245{
Chuck Lever63ab46a2006-03-20 13:44:31 -0500246 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
Chuck Lever9c93ab72006-06-20 12:56:31 -0400317 if (put_dreq(dreq)) {
318 nfs_direct_dirty_pages(dreq->pages, dreq->npages);
319 nfs_direct_release_pages(dreq->pages, dreq->npages);
Chuck Leverb1c59212006-06-20 12:55:19 -0400320 nfs_direct_complete(dreq);
Chuck Lever9c93ab72006-06-20 12:56:31 -0400321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Trond Myklebustec06c092006-03-20 13:44:27 -0500324static const struct rpc_call_ops nfs_read_direct_ops = {
325 .rpc_call_done = nfs_direct_read_result,
326 .rpc_release = nfs_readdata_release,
327};
328
Chuck Leverd4cc9482006-03-20 13:44:28 -0500329/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * For each nfs_read_data struct that was allocated on the list, dispatch
331 * an NFS READ operation
332 */
Chuck Lever51a7bc62006-06-20 12:56:16 -0400333static 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 -0700334{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500335 struct nfs_open_context *ctx = dreq->ctx;
336 struct inode *inode = ctx->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct list_head *list = &dreq->list;
338 struct page **pages = dreq->pages;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500339 size_t rsize = NFS_SERVER(inode)->rsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned int curpage, pgbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 curpage = 0;
Chuck Lever51a7bc62006-06-20 12:56:16 -0400343 pgbase = user_addr & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 do {
345 struct nfs_read_data *data;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500346 size_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 bytes = rsize;
349 if (count < rsize)
350 bytes = count;
351
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500352 BUG_ON(list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 data = list_entry(list->next, struct nfs_read_data, pages);
354 list_del_init(&data->pages);
355
356 data->inode = inode;
357 data->cred = ctx->cred;
358 data->args.fh = NFS_FH(inode);
359 data->args.context = ctx;
Chuck Lever88467052006-03-20 13:44:34 -0500360 data->args.offset = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 data->args.pgbase = pgbase;
362 data->args.pages = &pages[curpage];
363 data->args.count = bytes;
364 data->res.fattr = &data->fattr;
365 data->res.eof = 0;
366 data->res.count = bytes;
367
Trond Myklebustec06c092006-03-20 13:44:27 -0500368 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
369 &nfs_read_direct_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 NFS_PROTO(inode)->read_setup(data);
371
372 data->task.tk_cookie = (unsigned long) inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 lock_kernel();
375 rpc_execute(&data->task);
376 unlock_kernel();
377
Chuck Lever606bbba2006-03-20 13:44:42 -0500378 dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 data->task.tk_pid,
380 inode->i_sb->s_id,
381 (long long)NFS_FILEID(inode),
382 bytes,
383 (unsigned long long)data->args.offset);
384
Chuck Lever88467052006-03-20 13:44:34 -0500385 pos += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 pgbase += bytes;
387 curpage += pgbase >> PAGE_SHIFT;
388 pgbase &= ~PAGE_MASK;
389
390 count -= bytes;
391 } while (count != 0);
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500392 BUG_ON(!list_empty(list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Chuck Lever88467052006-03-20 13:44:34 -0500395static 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 -0700396{
397 ssize_t result;
398 sigset_t oldset;
Chuck Lever99514f82006-03-20 13:44:30 -0500399 struct inode *inode = iocb->ki_filp->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct rpc_clnt *clnt = NFS_CLIENT(inode);
401 struct nfs_direct_req *dreq;
402
403 dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize);
404 if (!dreq)
405 return -ENOMEM;
406
407 dreq->pages = pages;
408 dreq->npages = nr_pages;
Chuck Lever91d5b472006-03-20 13:44:14 -0500409 dreq->inode = inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500410 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
Chuck Lever487b8372006-03-20 13:44:30 -0500411 if (!is_sync_kiocb(iocb))
412 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Chuck Lever91d5b472006-03-20 13:44:14 -0500414 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 rpc_clnt_sigmask(clnt, &oldset);
Chuck Lever51a7bc62006-06-20 12:56:16 -0400416 nfs_direct_read_schedule(dreq, user_addr, count, pos);
Chuck Leverbc0fb202006-03-20 13:44:31 -0500417 result = nfs_direct_wait(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 rpc_clnt_sigunmask(clnt, &oldset);
419
420 return result;
421}
422
Trond Myklebustfad61492006-03-20 13:44:36 -0500423static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Trond Myklebustfad61492006-03-20 13:44:36 -0500425 list_splice_init(&dreq->rewrite_list, &dreq->list);
426 while (!list_empty(&dreq->list)) {
427 struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages);
428 list_del(&data->pages);
429 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Chuck Lever9c93ab72006-06-20 12:56:31 -0400431 nfs_direct_release_pages(dreq->pages, dreq->npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Trond Myklebustfad61492006-03-20 13:44:36 -0500434#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
435static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Chuck Leverfedb5952006-06-20 12:55:45 -0400437 struct inode *inode = dreq->inode;
438 struct list_head *p;
439 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Trond Myklebustfad61492006-03-20 13:44:36 -0500441 dreq->count = 0;
Chuck Leverfedb5952006-06-20 12:55:45 -0400442 get_dreq(dreq);
Trond Myklebustfad61492006-03-20 13:44:36 -0500443
Chuck Leverfedb5952006-06-20 12:55:45 -0400444 list_for_each(p, &dreq->rewrite_list) {
445 data = list_entry(p, struct nfs_write_data, pages);
446
447 get_dreq(dreq);
448
449 /*
450 * Reset data->res.
451 */
452 nfs_fattr_init(&data->fattr);
453 data->res.count = data->args.count;
454 memset(&data->verf, 0, sizeof(data->verf));
455
456 /*
457 * Reuse data->task; data->args should not have changed
458 * since the original request was sent.
459 */
460 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
461 &nfs_write_direct_ops, data);
462 NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
463
464 data->task.tk_priority = RPC_PRIORITY_NORMAL;
465 data->task.tk_cookie = (unsigned long) inode;
466
467 /*
468 * We're called via an RPC callback, so BKL is already held.
469 */
470 rpc_execute(&data->task);
471
472 dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
473 data->task.tk_pid,
474 inode->i_sb->s_id,
475 (long long)NFS_FILEID(inode),
476 data->args.count,
477 (unsigned long long)data->args.offset);
478 }
479
480 if (put_dreq(dreq))
481 nfs_direct_write_complete(dreq, inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500482}
483
484static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
485{
486 struct nfs_write_data *data = calldata;
487 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
488
489 /* Call the NFS version-specific code */
490 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
491 return;
492 if (unlikely(task->tk_status < 0)) {
493 dreq->error = task->tk_status;
494 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
495 }
496 if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
497 dprintk("NFS: %5u commit verify failed\n", task->tk_pid);
498 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
499 }
500
501 dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status);
502 nfs_direct_write_complete(dreq, data->inode);
503}
504
505static const struct rpc_call_ops nfs_commit_direct_ops = {
506 .rpc_call_done = nfs_direct_commit_result,
507 .rpc_release = nfs_commit_release,
508};
509
510static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
511{
Trond Myklebustfad61492006-03-20 13:44:36 -0500512 struct nfs_write_data *data = dreq->commit_data;
Trond Myklebustfad61492006-03-20 13:44:36 -0500513
514 data->inode = dreq->inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500515 data->cred = dreq->ctx->cred;
Trond Myklebustfad61492006-03-20 13:44:36 -0500516
517 data->args.fh = NFS_FH(data->inode);
Chuck Lever51a7bc62006-06-20 12:56:16 -0400518 data->args.offset = 0;
519 data->args.count = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500520 data->res.count = 0;
521 data->res.fattr = &data->fattr;
522 data->res.verf = &data->verf;
523
524 rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC,
525 &nfs_commit_direct_ops, data);
526 NFS_PROTO(data->inode)->commit_setup(data, 0);
527
528 data->task.tk_priority = RPC_PRIORITY_NORMAL;
529 data->task.tk_cookie = (unsigned long)data->inode;
530 /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
531 dreq->commit_data = NULL;
532
Trond Myklebuste99170f2006-04-18 13:21:42 -0400533 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
Trond Myklebustfad61492006-03-20 13:44:36 -0500534
535 lock_kernel();
536 rpc_execute(&data->task);
537 unlock_kernel();
538}
539
540static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
541{
542 int flags = dreq->flags;
543
544 dreq->flags = 0;
545 switch (flags) {
546 case NFS_ODIRECT_DO_COMMIT:
547 nfs_direct_commit_schedule(dreq);
548 break;
549 case NFS_ODIRECT_RESCHED_WRITES:
550 nfs_direct_write_reschedule(dreq);
551 break;
552 default:
553 nfs_end_data_update(inode);
554 if (dreq->commit_data != NULL)
555 nfs_commit_free(dreq->commit_data);
556 nfs_direct_free_writedata(dreq);
557 nfs_direct_complete(dreq);
558 }
559}
560
561static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
562{
563 dreq->commit_data = nfs_commit_alloc(0);
564 if (dreq->commit_data != NULL)
565 dreq->commit_data->req = (struct nfs_page *) dreq;
566}
567#else
568static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
569{
570 dreq->commit_data = NULL;
571}
572
573static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
574{
575 nfs_end_data_update(inode);
576 nfs_direct_free_writedata(dreq);
577 nfs_direct_complete(dreq);
578}
579#endif
580
Chuck Lever462d5b32006-03-20 13:44:32 -0500581static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize)
582{
583 struct list_head *list;
584 struct nfs_direct_req *dreq;
Chuck Lever462d5b32006-03-20 13:44:32 -0500585 unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
586
587 dreq = nfs_direct_req_alloc();
588 if (!dreq)
589 return NULL;
590
591 list = &dreq->list;
592 for(;;) {
593 struct nfs_write_data *data = nfs_writedata_alloc(wpages);
594
595 if (unlikely(!data)) {
596 while (!list_empty(list)) {
597 data = list_entry(list->next,
598 struct nfs_write_data, pages);
599 list_del(&data->pages);
600 nfs_writedata_free(data);
601 }
602 kref_put(&dreq->kref, nfs_direct_req_release);
603 return NULL;
604 }
605
606 INIT_LIST_HEAD(&data->pages);
607 list_add(&data->pages, list);
608
609 data->req = (struct nfs_page *) dreq;
Chuck Leverb1c59212006-06-20 12:55:19 -0400610 get_dreq(dreq);
Chuck Lever462d5b32006-03-20 13:44:32 -0500611 if (nbytes <= wsize)
612 break;
613 nbytes -= wsize;
614 }
Trond Myklebustfad61492006-03-20 13:44:36 -0500615
616 nfs_alloc_commit_data(dreq);
617
Chuck Lever462d5b32006-03-20 13:44:32 -0500618 kref_get(&dreq->kref);
Chuck Lever462d5b32006-03-20 13:44:32 -0500619 return dreq;
620}
621
Chuck Lever462d5b32006-03-20 13:44:32 -0500622static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
623{
624 struct nfs_write_data *data = calldata;
625 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
626 int status = task->tk_status;
627
628 if (nfs_writeback_done(task, data) != 0)
629 return;
Chuck Lever462d5b32006-03-20 13:44:32 -0500630
Chuck Lever15ce4a02006-03-20 13:44:34 -0500631 spin_lock(&dreq->lock);
Chuck Lever462d5b32006-03-20 13:44:32 -0500632
Chuck Lever15ce4a02006-03-20 13:44:34 -0500633 if (likely(status >= 0))
634 dreq->count += data->res.count;
635 else
Trond Myklebustfad61492006-03-20 13:44:36 -0500636 dreq->error = task->tk_status;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500637
Trond Myklebustfad61492006-03-20 13:44:36 -0500638 if (data->res.verf->committed != NFS_FILE_SYNC) {
639 switch (dreq->flags) {
640 case 0:
641 memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
642 dreq->flags = NFS_ODIRECT_DO_COMMIT;
643 break;
644 case NFS_ODIRECT_DO_COMMIT:
645 if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
646 dprintk("NFS: %5u write verify failed\n", task->tk_pid);
647 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
648 }
649 }
650 }
Trond Myklebustfad61492006-03-20 13:44:36 -0500651
652 spin_unlock(&dreq->lock);
653}
654
655/*
656 * NB: Return the value of the first error return code. Subsequent
657 * errors after the first one are ignored.
658 */
659static void nfs_direct_write_release(void *calldata)
660{
661 struct nfs_write_data *data = calldata;
662 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
663
Chuck Leverb1c59212006-06-20 12:55:19 -0400664 if (put_dreq(dreq))
665 nfs_direct_write_complete(dreq, data->inode);
Chuck Lever462d5b32006-03-20 13:44:32 -0500666}
667
668static const struct rpc_call_ops nfs_write_direct_ops = {
669 .rpc_call_done = nfs_direct_write_result,
Trond Myklebustfad61492006-03-20 13:44:36 -0500670 .rpc_release = nfs_direct_write_release,
Chuck Lever462d5b32006-03-20 13:44:32 -0500671};
672
673/*
674 * For each nfs_write_data struct that was allocated on the list, dispatch
675 * an NFS WRITE operation
Chuck Lever462d5b32006-03-20 13:44:32 -0500676 */
Chuck Lever51a7bc62006-06-20 12:56:16 -0400677static 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 -0500678{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500679 struct nfs_open_context *ctx = dreq->ctx;
680 struct inode *inode = ctx->dentry->d_inode;
Chuck Lever462d5b32006-03-20 13:44:32 -0500681 struct list_head *list = &dreq->list;
682 struct page **pages = dreq->pages;
683 size_t wsize = NFS_SERVER(inode)->wsize;
684 unsigned int curpage, pgbase;
685
686 curpage = 0;
Chuck Lever51a7bc62006-06-20 12:56:16 -0400687 pgbase = user_addr & ~PAGE_MASK;
Chuck Lever462d5b32006-03-20 13:44:32 -0500688 do {
689 struct nfs_write_data *data;
690 size_t bytes;
691
692 bytes = wsize;
693 if (count < wsize)
694 bytes = count;
695
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500696 BUG_ON(list_empty(list));
Chuck Lever462d5b32006-03-20 13:44:32 -0500697 data = list_entry(list->next, struct nfs_write_data, pages);
Trond Myklebustfad61492006-03-20 13:44:36 -0500698 list_move_tail(&data->pages, &dreq->rewrite_list);
Chuck Lever462d5b32006-03-20 13:44:32 -0500699
700 data->inode = inode;
701 data->cred = ctx->cred;
702 data->args.fh = NFS_FH(inode);
703 data->args.context = ctx;
Chuck Lever88467052006-03-20 13:44:34 -0500704 data->args.offset = pos;
Chuck Lever462d5b32006-03-20 13:44:32 -0500705 data->args.pgbase = pgbase;
706 data->args.pages = &pages[curpage];
707 data->args.count = bytes;
708 data->res.fattr = &data->fattr;
709 data->res.count = bytes;
Chuck Lever47989d72006-03-20 13:44:32 -0500710 data->res.verf = &data->verf;
Chuck Lever462d5b32006-03-20 13:44:32 -0500711
712 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
713 &nfs_write_direct_ops, data);
Trond Myklebustfad61492006-03-20 13:44:36 -0500714 NFS_PROTO(inode)->write_setup(data, sync);
Chuck Lever462d5b32006-03-20 13:44:32 -0500715
716 data->task.tk_priority = RPC_PRIORITY_NORMAL;
717 data->task.tk_cookie = (unsigned long) inode;
718
719 lock_kernel();
720 rpc_execute(&data->task);
721 unlock_kernel();
722
Chuck Lever606bbba2006-03-20 13:44:42 -0500723 dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
Chuck Lever462d5b32006-03-20 13:44:32 -0500724 data->task.tk_pid,
725 inode->i_sb->s_id,
726 (long long)NFS_FILEID(inode),
727 bytes,
728 (unsigned long long)data->args.offset);
729
Chuck Lever88467052006-03-20 13:44:34 -0500730 pos += bytes;
Chuck Lever462d5b32006-03-20 13:44:32 -0500731 pgbase += bytes;
732 curpage += pgbase >> PAGE_SHIFT;
733 pgbase &= ~PAGE_MASK;
734
735 count -= bytes;
736 } while (count != 0);
Trond Myklebust5db3a7b2006-03-20 13:44:37 -0500737 BUG_ON(!list_empty(list));
Chuck Lever462d5b32006-03-20 13:44:32 -0500738}
739
Chuck Lever88467052006-03-20 13:44:34 -0500740static 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 -0700741{
Chuck Lever462d5b32006-03-20 13:44:32 -0500742 ssize_t result;
743 sigset_t oldset;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500744 struct inode *inode = iocb->ki_filp->f_mapping->host;
Chuck Lever462d5b32006-03-20 13:44:32 -0500745 struct rpc_clnt *clnt = NFS_CLIENT(inode);
746 struct nfs_direct_req *dreq;
Trond Myklebustfad61492006-03-20 13:44:36 -0500747 size_t wsize = NFS_SERVER(inode)->wsize;
748 int sync = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Trond Myklebustfad61492006-03-20 13:44:36 -0500750 dreq = nfs_direct_write_alloc(count, wsize);
Chuck Lever462d5b32006-03-20 13:44:32 -0500751 if (!dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return -ENOMEM;
Trond Myklebustfad61492006-03-20 13:44:36 -0500753 if (dreq->commit_data == NULL || count < wsize)
754 sync = FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Chuck Lever462d5b32006-03-20 13:44:32 -0500756 dreq->pages = pages;
757 dreq->npages = nr_pages;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500758 dreq->inode = inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500759 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500760 if (!is_sync_kiocb(iocb))
761 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Chuck Lever47989d72006-03-20 13:44:32 -0500763 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 nfs_begin_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Chuck Lever462d5b32006-03-20 13:44:32 -0500767 rpc_clnt_sigmask(clnt, &oldset);
Chuck Lever51a7bc62006-06-20 12:56:16 -0400768 nfs_direct_write_schedule(dreq, user_addr, count, pos, sync);
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500769 result = nfs_direct_wait(dreq);
Chuck Lever462d5b32006-03-20 13:44:32 -0500770 rpc_clnt_sigunmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return result;
773}
774
775/**
776 * nfs_file_direct_read - file direct read operation for NFS files
777 * @iocb: target I/O control block
778 * @buf: user's buffer into which to read data
Chuck Lever88467052006-03-20 13:44:34 -0500779 * @count: number of bytes to read
780 * @pos: byte offset in file where reading starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 *
782 * We use this function for direct reads instead of calling
783 * generic_file_aio_read() in order to avoid gfar's check to see if
784 * the request starts before the end of the file. For that check
785 * to work, we must generate a GETATTR before each direct read, and
786 * even then there is a window between the GETATTR and the subsequent
Chuck Lever88467052006-03-20 13:44:34 -0500787 * READ where the file size could change. Our preference is simply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 * to do all reads the application wants, and the server will take
789 * care of managing the end of file boundary.
Chuck Lever88467052006-03-20 13:44:34 -0500790 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * This function also eliminates unnecessarily updating the file's
792 * atime locally, as the NFS server sets the file's atime, and this
793 * client must read the updated atime from the server back into its
794 * cache.
795 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500796ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 ssize_t retval = -EINVAL;
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500799 int page_count;
800 struct page **pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Chuck Leverce1a8e62005-11-30 18:08:17 -0500804 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500805 file->f_dentry->d_parent->d_name.name,
806 file->f_dentry->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500807 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (count < 0)
810 goto out;
811 retval = -EFAULT;
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500812 if (!access_ok(VERIFY_WRITE, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 goto out;
814 retval = 0;
815 if (!count)
816 goto out;
817
Trond Myklebust29884df2005-12-13 16:13:54 -0500818 retval = nfs_sync_mapping(mapping);
819 if (retval)
820 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Trond Myklebust6b45d852006-03-20 13:44:43 -0500822 retval = nfs_get_user_pages(READ, (unsigned long) buf,
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500823 count, &pages);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500824 if (retval < 0)
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500825 goto out;
Trond Myklebust6b45d852006-03-20 13:44:43 -0500826 page_count = retval;
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500827
Chuck Lever99514f82006-03-20 13:44:30 -0500828 retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos,
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500829 pages, page_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (retval > 0)
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500831 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833out:
834 return retval;
835}
836
837/**
838 * nfs_file_direct_write - file direct write operation for NFS files
839 * @iocb: target I/O control block
840 * @buf: user's buffer from which to write data
Chuck Lever88467052006-03-20 13:44:34 -0500841 * @count: number of bytes to write
842 * @pos: byte offset in file where writing starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 *
844 * We use this function for direct writes instead of calling
845 * generic_file_aio_write() in order to avoid taking the inode
846 * semaphore and updating the i_size. The NFS server will set
847 * the new i_size and this client must read the updated size
848 * back into its cache. We let the server do generic write
849 * parameter checking and report problems.
850 *
851 * We also avoid an unnecessary invocation of generic_osync_inode(),
852 * as it is fairly meaningless to sync the metadata of an NFS file.
853 *
854 * We eliminate local atime updates, see direct read above.
855 *
856 * We avoid unnecessary page cache invalidations for normal cached
857 * readers of this file.
858 *
859 * Note that O_APPEND is not supported for NFS direct writes, as there
860 * is no atomic O_APPEND write facility in the NFS protocol.
861 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500862ssize_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 -0700863{
Chuck Leverce1a8e62005-11-30 18:08:17 -0500864 ssize_t retval;
Chuck Lever47989d72006-03-20 13:44:32 -0500865 int page_count;
866 struct page **pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Chuck Leverce1a8e62005-11-30 18:08:17 -0500870 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500871 file->f_dentry->d_parent->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500872 file->f_dentry->d_name.name,
873 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Chuck Leverce1a8e62005-11-30 18:08:17 -0500875 retval = generic_write_checks(file, &pos, &count, 0);
876 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500878
879 retval = -EINVAL;
880 if ((ssize_t) count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 retval = 0;
883 if (!count)
884 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500885
886 retval = -EFAULT;
Chuck Lever47989d72006-03-20 13:44:32 -0500887 if (!access_ok(VERIFY_READ, buf, count))
Chuck Leverce1a8e62005-11-30 18:08:17 -0500888 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Trond Myklebust29884df2005-12-13 16:13:54 -0500890 retval = nfs_sync_mapping(mapping);
891 if (retval)
892 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Trond Myklebust6b45d852006-03-20 13:44:43 -0500894 retval = nfs_get_user_pages(WRITE, (unsigned long) buf,
Chuck Lever47989d72006-03-20 13:44:32 -0500895 count, &pages);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500896 if (retval < 0)
Chuck Lever47989d72006-03-20 13:44:32 -0500897 goto out;
Trond Myklebust6b45d852006-03-20 13:44:43 -0500898 page_count = retval;
Chuck Lever47989d72006-03-20 13:44:32 -0500899
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500900 retval = nfs_direct_write(iocb, (unsigned long) buf, count,
Chuck Lever47989d72006-03-20 13:44:32 -0500901 pos, pages, page_count);
Chuck Lever9eafa8c2006-03-20 13:44:33 -0500902
903 /*
904 * XXX: nfs_end_data_update() already ensures this file's
905 * cached data is subsequently invalidated. Do we really
906 * need to call invalidate_inode_pages2() again here?
907 *
908 * For aio writes, this invalidation will almost certainly
909 * occur before the writes complete. Kind of racey.
910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (mapping->nrpages)
912 invalidate_inode_pages2(mapping);
Chuck Lever9eafa8c2006-03-20 13:44:33 -0500913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (retval > 0)
Chuck Leverce1a8e62005-11-30 18:08:17 -0500915 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917out:
918 return retval;
919}
920
Chuck Lever88467052006-03-20 13:44:34 -0500921/**
922 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
923 *
924 */
David Howellsf7b422b2006-06-09 09:34:33 -0400925int __init nfs_init_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
928 sizeof(struct nfs_direct_req),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800929 0, (SLAB_RECLAIM_ACCOUNT|
930 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 NULL, NULL);
932 if (nfs_direct_cachep == NULL)
933 return -ENOMEM;
934
935 return 0;
936}
937
Chuck Lever88467052006-03-20 13:44:34 -0500938/**
David Howellsf7b422b2006-06-09 09:34:33 -0400939 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
Chuck Lever88467052006-03-20 13:44:34 -0500940 *
941 */
David Howellsf7b422b2006-06-09 09:34:33 -0400942void __exit nfs_destroy_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 if (kmem_cache_destroy(nfs_direct_cachep))
945 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
946}