blob: fecd3b095debe9825879c52a8840f4878b1b576d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/direct.c
3 *
4 * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
5 *
6 * High-performance uncached I/O for the Linux NFS client
7 *
8 * There are important applications whose performance or correctness
9 * depends on uncached access to file data. Database clusters
Chuck Lever88467052006-03-20 13:44:34 -050010 * (multiple copies of the same instance running on separate hosts)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * implement their own cache coherency protocol that subsumes file
Chuck Lever88467052006-03-20 13:44:34 -050012 * system cache protocols. Applications that process datasets
13 * considerably larger than the client's memory do not always benefit
14 * from a local cache. A streaming video server, for instance, has no
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * need to cache the contents of a file.
16 *
17 * When an application requests uncached I/O, all read and write requests
18 * are made directly to the server; data stored or fetched via these
19 * requests is not cached in the Linux page cache. The client does not
20 * correct unaligned requests from applications. All requested bytes are
21 * held on permanent storage before a direct write system call returns to
22 * an application.
23 *
24 * Solaris implements an uncached I/O facility called directio() that
25 * is used for backups and sequential I/O to very large files. Solaris
26 * also supports uncaching whole NFS partitions with "-o forcedirectio,"
27 * an undocumented mount option.
28 *
29 * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
30 * help from Andrew Morton.
31 *
32 * 18 Dec 2001 Initial implementation for 2.4 --cel
33 * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy
34 * 08 Jun 2003 Port to 2.5 APIs --cel
35 * 31 Mar 2004 Handle direct I/O without VFS support --cel
36 * 15 Sep 2004 Parallel async reads --cel
Chuck Lever88467052006-03-20 13:44:34 -050037 * 04 May 2005 support O_DIRECT with aio --cel
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 *
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/errno.h>
42#include <linux/sched.h>
43#include <linux/kernel.h>
44#include <linux/smp_lock.h>
45#include <linux/file.h>
46#include <linux/pagemap.h>
47#include <linux/kref.h>
48
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
51#include <linux/sunrpc/clnt.h>
52
53#include <asm/system.h>
54#include <asm/uaccess.h>
55#include <asm/atomic.h>
56
Chuck Lever91d5b472006-03-20 13:44:14 -050057#include "iostat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define NFSDBG_FACILITY NFSDBG_VFS
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static kmem_cache_t *nfs_direct_cachep;
62
63/*
64 * This represents a set of asynchronous requests that we're waiting on
65 */
66struct nfs_direct_req {
67 struct kref kref; /* release manager */
Chuck Lever15ce4a02006-03-20 13:44:34 -050068
69 /* I/O parameters */
Trond Myklebusta8881f52006-03-20 13:44:36 -050070 struct nfs_open_context *ctx; /* file open context info */
Chuck Lever99514f82006-03-20 13:44:30 -050071 struct kiocb * iocb; /* controlling i/o request */
Chuck Lever88467052006-03-20 13:44:34 -050072 struct inode * inode; /* target file of i/o */
Chuck Lever15ce4a02006-03-20 13:44:34 -050073
74 /* completion state */
Trond Myklebust607f31e2006-06-28 16:52:45 -040075 atomic_t io_count; /* i/os we're waiting for */
Chuck Lever15ce4a02006-03-20 13:44:34 -050076 spinlock_t lock; /* protect completion state */
Chuck Lever15ce4a02006-03-20 13:44:34 -050077 ssize_t count, /* bytes actually processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 error; /* any reported error */
Trond Myklebustd72b7a62006-03-20 13:44:43 -050079 struct completion completion; /* wait for i/o completion */
Trond Myklebustfad61492006-03-20 13:44:36 -050080
81 /* commit state */
Trond Myklebust607f31e2006-06-28 16:52:45 -040082 struct list_head rewrite_list; /* saved nfs_write_data structs */
Trond Myklebustfad61492006-03-20 13:44:36 -050083 struct nfs_write_data * commit_data; /* special write_data for commits */
84 int flags;
85#define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
86#define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
87 struct nfs_writeverf verf; /* unstable write verifier */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Trond Myklebustfad61492006-03-20 13:44:36 -050090static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
Trond Myklebust607f31e2006-06-28 16:52:45 -040091static const struct rpc_call_ops nfs_write_direct_ops;
92
93static inline void get_dreq(struct nfs_direct_req *dreq)
94{
95 atomic_inc(&dreq->io_count);
96}
97
98static inline int put_dreq(struct nfs_direct_req *dreq)
99{
100 return atomic_dec_and_test(&dreq->io_count);
101}
102
103/*
104 * "size" is never larger than rsize or wsize.
105 */
106static inline int nfs_direct_count_pages(unsigned long user_addr, size_t size)
107{
108 int page_count;
109
110 page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT;
111 page_count -= user_addr >> PAGE_SHIFT;
112 BUG_ON(page_count < 0);
113
114 return page_count;
115}
116
117static inline unsigned int nfs_max_pages(unsigned int size)
118{
119 return (size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
120}
Chuck Lever82b145c2006-06-20 12:57:03 -0400121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/**
Chuck Leverb8a32e22006-03-20 13:44:28 -0500123 * nfs_direct_IO - NFS address space operation for direct I/O
124 * @rw: direction (read or write)
125 * @iocb: target I/O control block
126 * @iov: array of vectors that define I/O buffer
127 * @pos: offset in file to begin the operation
128 * @nr_segs: size of iovec array
129 *
130 * The presence of this routine in the address space ops vector means
131 * the NFS client supports direct I/O. However, we shunt off direct
132 * read and write requests before the VFS gets them, so this method
133 * should never be called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
Chuck Leverb8a32e22006-03-20 13:44:28 -0500135ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
136{
Chuck Leverb8a32e22006-03-20 13:44:28 -0500137 dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
Trond Myklebuste99170f2006-04-18 13:21:42 -0400138 iocb->ki_filp->f_dentry->d_name.name,
139 (long long) pos, nr_segs);
Chuck Leverb8a32e22006-03-20 13:44:28 -0500140
141 return -EINVAL;
142}
143
Trond Myklebust607f31e2006-06-28 16:52:45 -0400144static void nfs_direct_dirty_pages(struct page **pages, int npages)
Trond Myklebust6b45d852006-03-20 13:44:43 -0500145{
146 int i;
147 for (i = 0; i < npages; i++) {
148 struct page *page = pages[i];
Trond Myklebust607f31e2006-06-28 16:52:45 -0400149 if (!PageCompound(page))
Trond Myklebust6b45d852006-03-20 13:44:43 -0500150 set_page_dirty_lock(page);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500151 }
Chuck Lever9c93ab72006-06-20 12:56:31 -0400152}
153
Trond Myklebust607f31e2006-06-28 16:52:45 -0400154static void nfs_direct_release_pages(struct page **pages, int npages)
Chuck Lever9c93ab72006-06-20 12:56:31 -0400155{
Trond Myklebust607f31e2006-06-28 16:52:45 -0400156 int i;
157 for (i = 0; i < npages; i++)
158 page_cache_release(pages[i]);
Trond Myklebust6b45d852006-03-20 13:44:43 -0500159}
160
Chuck Lever93619e52006-03-20 13:44:31 -0500161static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct nfs_direct_req *dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL);
166 if (!dreq)
167 return NULL;
168
169 kref_init(&dreq->kref);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400170 kref_get(&dreq->kref);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500171 init_completion(&dreq->completion);
Trond Myklebustfad61492006-03-20 13:44:36 -0500172 INIT_LIST_HEAD(&dreq->rewrite_list);
Chuck Lever93619e52006-03-20 13:44:31 -0500173 dreq->iocb = NULL;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500174 dreq->ctx = NULL;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500175 spin_lock_init(&dreq->lock);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400176 atomic_set(&dreq->io_count, 0);
Chuck Lever15ce4a02006-03-20 13:44:34 -0500177 dreq->count = 0;
178 dreq->error = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500179 dreq->flags = 0;
Chuck Lever93619e52006-03-20 13:44:31 -0500180
181 return dreq;
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static void nfs_direct_req_release(struct kref *kref)
185{
186 struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
Trond Myklebusta8881f52006-03-20 13:44:36 -0500187
188 if (dreq->ctx != NULL)
189 put_nfs_open_context(dreq->ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 kmem_cache_free(nfs_direct_cachep, dreq);
191}
192
Chuck Leverd4cc9482006-03-20 13:44:28 -0500193/*
Chuck Leverbc0fb202006-03-20 13:44:31 -0500194 * Collects and returns the final error value/byte-count.
195 */
196static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
197{
Chuck Lever15ce4a02006-03-20 13:44:34 -0500198 ssize_t result = -EIOCBQUEUED;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500199
200 /* Async requests don't wait here */
201 if (dreq->iocb)
202 goto out;
203
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500204 result = wait_for_completion_interruptible(&dreq->completion);
Chuck Leverbc0fb202006-03-20 13:44:31 -0500205
206 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500207 result = dreq->error;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500208 if (!result)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500209 result = dreq->count;
Chuck Leverbc0fb202006-03-20 13:44:31 -0500210
211out:
212 kref_put(&dreq->kref, nfs_direct_req_release);
213 return (ssize_t) result;
214}
215
216/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400217 * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
218 * the iocb is still valid here if this is a synchronous request.
Chuck Lever63ab46a2006-03-20 13:44:31 -0500219 */
220static void nfs_direct_complete(struct nfs_direct_req *dreq)
221{
Chuck Lever63ab46a2006-03-20 13:44:31 -0500222 if (dreq->iocb) {
Chuck Lever15ce4a02006-03-20 13:44:34 -0500223 long res = (long) dreq->error;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500224 if (!res)
Chuck Lever15ce4a02006-03-20 13:44:34 -0500225 res = (long) dreq->count;
Chuck Lever63ab46a2006-03-20 13:44:31 -0500226 aio_complete(dreq->iocb, res, 0);
Trond Myklebustd72b7a62006-03-20 13:44:43 -0500227 }
228 complete_all(&dreq->completion);
Chuck Lever63ab46a2006-03-20 13:44:31 -0500229
230 kref_put(&dreq->kref, nfs_direct_req_release);
231}
232
233/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400234 * 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).
Chuck Lever06cf6f22006-06-20 12:56:49 -0400237 */
Trond Myklebustec06c092006-03-20 13:44:27 -0500238static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Trond Myklebustec06c092006-03-20 13:44:27 -0500240 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
242
Trond Myklebustec06c092006-03-20 13:44:27 -0500243 if (nfs_readpage_result(task, data) != 0)
244 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Trond Myklebust607f31e2006-06-28 16:52:45 -0400246 nfs_direct_dirty_pages(data->pagevec, data->npages);
247 nfs_direct_release_pages(data->pagevec, data->npages);
248
Chuck Lever15ce4a02006-03-20 13:44:34 -0500249 spin_lock(&dreq->lock);
250
251 if (likely(task->tk_status >= 0))
252 dreq->count += data->res.count;
253 else
254 dreq->error = task->tk_status;
255
Trond Myklebustccf01ef2006-06-25 06:27:31 -0400256 spin_unlock(&dreq->lock);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400257
258 if (put_dreq(dreq))
259 nfs_direct_complete(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Trond Myklebustec06c092006-03-20 13:44:27 -0500262static const struct rpc_call_ops nfs_read_direct_ops = {
263 .rpc_call_done = nfs_direct_read_result,
264 .rpc_release = nfs_readdata_release,
265};
266
Chuck Leverd4cc9482006-03-20 13:44:28 -0500267/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400268 * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
269 * operation. If nfs_readdata_alloc() or get_user_pages() fails,
270 * bail and stop sending more reads. Read length accounting is
271 * handled automatically by nfs_direct_read_result(). Otherwise, if
272 * no requests have been sent, just return an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Trond Myklebust607f31e2006-06-28 16:52:45 -0400274static ssize_t nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500276 struct nfs_open_context *ctx = dreq->ctx;
277 struct inode *inode = ctx->dentry->d_inode;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500278 size_t rsize = NFS_SERVER(inode)->rsize;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400279 unsigned int rpages = nfs_max_pages(rsize);
280 unsigned int pgbase;
281 int result;
282 ssize_t started = 0;
Chuck Lever82b145c2006-06-20 12:57:03 -0400283
Trond Myklebust607f31e2006-06-28 16:52:45 -0400284 get_dreq(dreq);
285
286 pgbase = user_addr & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 do {
Chuck Lever82b145c2006-06-20 12:57:03 -0400288 struct nfs_read_data *data;
Chuck Lever5dd602f2006-03-20 13:44:29 -0500289 size_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Trond Myklebust607f31e2006-06-28 16:52:45 -0400291 result = -ENOMEM;
292 data = nfs_readdata_alloc(rpages);
293 if (unlikely(!data))
294 break;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 bytes = rsize;
297 if (count < rsize)
298 bytes = count;
299
Trond Myklebust607f31e2006-06-28 16:52:45 -0400300 data->npages = nfs_direct_count_pages(user_addr, bytes);
301 down_read(&current->mm->mmap_sem);
302 result = get_user_pages(current, current->mm, user_addr,
303 data->npages, 1, 0, data->pagevec, NULL);
304 up_read(&current->mm->mmap_sem);
305 if (unlikely(result < data->npages)) {
306 if (result > 0)
307 nfs_direct_release_pages(data->pagevec, result);
308 nfs_readdata_release(data);
309 break;
310 }
Chuck Lever06cf6f22006-06-20 12:56:49 -0400311
Trond Myklebust607f31e2006-06-28 16:52:45 -0400312 get_dreq(dreq);
313
314 data->req = (struct nfs_page *) dreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 data->inode = inode;
316 data->cred = ctx->cred;
317 data->args.fh = NFS_FH(inode);
318 data->args.context = ctx;
Chuck Lever88467052006-03-20 13:44:34 -0500319 data->args.offset = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 data->args.pgbase = pgbase;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400321 data->args.pages = data->pagevec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 data->args.count = bytes;
323 data->res.fattr = &data->fattr;
324 data->res.eof = 0;
325 data->res.count = bytes;
326
Trond Myklebustec06c092006-03-20 13:44:27 -0500327 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
328 &nfs_read_direct_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 NFS_PROTO(inode)->read_setup(data);
330
331 data->task.tk_cookie = (unsigned long) inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 lock_kernel();
334 rpc_execute(&data->task);
335 unlock_kernel();
336
Chuck Lever606bbba2006-03-20 13:44:42 -0500337 dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 data->task.tk_pid,
339 inode->i_sb->s_id,
340 (long long)NFS_FILEID(inode),
341 bytes,
342 (unsigned long long)data->args.offset);
343
Trond Myklebust607f31e2006-06-28 16:52:45 -0400344 started += bytes;
345 user_addr += bytes;
Chuck Lever88467052006-03-20 13:44:34 -0500346 pos += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 pgbase += bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 pgbase &= ~PAGE_MASK;
349
350 count -= bytes;
351 } while (count != 0);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400352
353 if (put_dreq(dreq))
354 nfs_direct_complete(dreq);
355
356 if (started)
357 return 0;
358 return result < 0 ? (ssize_t) result : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Trond Myklebust607f31e2006-06-28 16:52:45 -0400361static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Trond Myklebust607f31e2006-06-28 16:52:45 -0400363 ssize_t result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sigset_t oldset;
Chuck Lever99514f82006-03-20 13:44:30 -0500365 struct inode *inode = iocb->ki_filp->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct rpc_clnt *clnt = NFS_CLIENT(inode);
367 struct nfs_direct_req *dreq;
368
Trond Myklebust607f31e2006-06-28 16:52:45 -0400369 dreq = nfs_direct_req_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (!dreq)
371 return -ENOMEM;
372
Chuck Lever91d5b472006-03-20 13:44:14 -0500373 dreq->inode = inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500374 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
Chuck Lever487b8372006-03-20 13:44:30 -0500375 if (!is_sync_kiocb(iocb))
376 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Chuck Lever91d5b472006-03-20 13:44:14 -0500378 nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400380 result = nfs_direct_read_schedule(dreq, user_addr, count, pos);
381 if (!result)
382 result = nfs_direct_wait(dreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 rpc_clnt_sigunmask(clnt, &oldset);
384
385 return result;
386}
387
Trond Myklebustfad61492006-03-20 13:44:36 -0500388static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Trond Myklebust607f31e2006-06-28 16:52:45 -0400390 while (!list_empty(&dreq->rewrite_list)) {
391 struct nfs_write_data *data = list_entry(dreq->rewrite_list.next, struct nfs_write_data, pages);
Trond Myklebustfad61492006-03-20 13:44:36 -0500392 list_del(&data->pages);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400393 nfs_direct_release_pages(data->pagevec, data->npages);
Trond Myklebustfad61492006-03-20 13:44:36 -0500394 nfs_writedata_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Trond Myklebustfad61492006-03-20 13:44:36 -0500398#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
399static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Trond Myklebust607f31e2006-06-28 16:52:45 -0400401 struct inode *inode = dreq->inode;
402 struct list_head *p;
403 struct nfs_write_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Trond Myklebustfad61492006-03-20 13:44:36 -0500405 dreq->count = 0;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400406 get_dreq(dreq);
Trond Myklebustfad61492006-03-20 13:44:36 -0500407
Trond Myklebust607f31e2006-06-28 16:52:45 -0400408 list_for_each(p, &dreq->rewrite_list) {
409 data = list_entry(p, struct nfs_write_data, pages);
410
411 get_dreq(dreq);
412
413 /*
414 * Reset data->res.
415 */
416 nfs_fattr_init(&data->fattr);
417 data->res.count = data->args.count;
418 memset(&data->verf, 0, sizeof(data->verf));
419
420 /*
421 * Reuse data->task; data->args should not have changed
422 * since the original request was sent.
423 */
424 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
425 &nfs_write_direct_ops, data);
426 NFS_PROTO(inode)->write_setup(data, FLUSH_STABLE);
427
428 data->task.tk_priority = RPC_PRIORITY_NORMAL;
429 data->task.tk_cookie = (unsigned long) inode;
430
431 /*
432 * We're called via an RPC callback, so BKL is already held.
433 */
434 rpc_execute(&data->task);
435
436 dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
437 data->task.tk_pid,
438 inode->i_sb->s_id,
439 (long long)NFS_FILEID(inode),
440 data->args.count,
441 (unsigned long long)data->args.offset);
442 }
443
444 if (put_dreq(dreq))
445 nfs_direct_write_complete(dreq, inode);
Trond Myklebustfad61492006-03-20 13:44:36 -0500446}
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);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400482 data->args.offset = 0;
483 data->args.count = 0;
Trond Myklebustfad61492006-03-20 13:44:36 -0500484 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 void nfs_direct_write_result(struct rpc_task *task, void *calldata)
546{
547 struct nfs_write_data *data = calldata;
548 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
549 int status = task->tk_status;
550
551 if (nfs_writeback_done(task, data) != 0)
552 return;
Chuck Lever462d5b32006-03-20 13:44:32 -0500553
Chuck Lever15ce4a02006-03-20 13:44:34 -0500554 spin_lock(&dreq->lock);
Chuck Lever462d5b32006-03-20 13:44:32 -0500555
Chuck Lever15ce4a02006-03-20 13:44:34 -0500556 if (likely(status >= 0))
557 dreq->count += data->res.count;
558 else
Trond Myklebustfad61492006-03-20 13:44:36 -0500559 dreq->error = task->tk_status;
Chuck Lever15ce4a02006-03-20 13:44:34 -0500560
Trond Myklebustfad61492006-03-20 13:44:36 -0500561 if (data->res.verf->committed != NFS_FILE_SYNC) {
562 switch (dreq->flags) {
563 case 0:
564 memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
565 dreq->flags = NFS_ODIRECT_DO_COMMIT;
566 break;
567 case NFS_ODIRECT_DO_COMMIT:
568 if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
569 dprintk("NFS: %5u write verify failed\n", task->tk_pid);
570 dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
571 }
572 }
573 }
Trond Myklebustfad61492006-03-20 13:44:36 -0500574
575 spin_unlock(&dreq->lock);
576}
577
578/*
579 * NB: Return the value of the first error return code. Subsequent
580 * errors after the first one are ignored.
581 */
582static void nfs_direct_write_release(void *calldata)
583{
584 struct nfs_write_data *data = calldata;
585 struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req;
586
Trond Myklebust607f31e2006-06-28 16:52:45 -0400587 if (put_dreq(dreq))
588 nfs_direct_write_complete(dreq, data->inode);
Chuck Lever462d5b32006-03-20 13:44:32 -0500589}
590
591static const struct rpc_call_ops nfs_write_direct_ops = {
592 .rpc_call_done = nfs_direct_write_result,
Trond Myklebustfad61492006-03-20 13:44:36 -0500593 .rpc_release = nfs_direct_write_release,
Chuck Lever462d5b32006-03-20 13:44:32 -0500594};
595
596/*
Trond Myklebust607f31e2006-06-28 16:52:45 -0400597 * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
598 * operation. If nfs_writedata_alloc() or get_user_pages() fails,
599 * bail and stop sending more writes. Write length accounting is
600 * handled automatically by nfs_direct_write_result(). Otherwise, if
601 * no requests have been sent, just return an error.
Chuck Lever462d5b32006-03-20 13:44:32 -0500602 */
Trond Myklebust607f31e2006-06-28 16:52:45 -0400603static ssize_t nfs_direct_write_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t pos, int sync)
Chuck Lever462d5b32006-03-20 13:44:32 -0500604{
Trond Myklebusta8881f52006-03-20 13:44:36 -0500605 struct nfs_open_context *ctx = dreq->ctx;
606 struct inode *inode = ctx->dentry->d_inode;
Chuck Lever462d5b32006-03-20 13:44:32 -0500607 size_t wsize = NFS_SERVER(inode)->wsize;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400608 unsigned int wpages = nfs_max_pages(wsize);
609 unsigned int pgbase;
610 int result;
611 ssize_t started = 0;
Chuck Lever82b145c2006-06-20 12:57:03 -0400612
Trond Myklebust607f31e2006-06-28 16:52:45 -0400613 get_dreq(dreq);
614
615 pgbase = user_addr & ~PAGE_MASK;
Chuck Lever462d5b32006-03-20 13:44:32 -0500616 do {
Chuck Lever82b145c2006-06-20 12:57:03 -0400617 struct nfs_write_data *data;
Chuck Lever462d5b32006-03-20 13:44:32 -0500618 size_t bytes;
619
Trond Myklebust607f31e2006-06-28 16:52:45 -0400620 result = -ENOMEM;
621 data = nfs_writedata_alloc(wpages);
622 if (unlikely(!data))
623 break;
624
Chuck Lever462d5b32006-03-20 13:44:32 -0500625 bytes = wsize;
626 if (count < wsize)
627 bytes = count;
628
Trond Myklebust607f31e2006-06-28 16:52:45 -0400629 data->npages = nfs_direct_count_pages(user_addr, bytes);
630 down_read(&current->mm->mmap_sem);
631 result = get_user_pages(current, current->mm, user_addr,
632 data->npages, 0, 0, data->pagevec, NULL);
633 up_read(&current->mm->mmap_sem);
634 if (unlikely(result < data->npages)) {
635 if (result > 0)
636 nfs_direct_release_pages(data->pagevec, result);
637 nfs_writedata_release(data);
638 break;
639 }
640
641 get_dreq(dreq);
642
Trond Myklebustfad61492006-03-20 13:44:36 -0500643 list_move_tail(&data->pages, &dreq->rewrite_list);
Chuck Lever462d5b32006-03-20 13:44:32 -0500644
Trond Myklebust607f31e2006-06-28 16:52:45 -0400645 data->req = (struct nfs_page *) dreq;
Chuck Lever462d5b32006-03-20 13:44:32 -0500646 data->inode = inode;
647 data->cred = ctx->cred;
648 data->args.fh = NFS_FH(inode);
649 data->args.context = ctx;
Chuck Lever88467052006-03-20 13:44:34 -0500650 data->args.offset = pos;
Chuck Lever462d5b32006-03-20 13:44:32 -0500651 data->args.pgbase = pgbase;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400652 data->args.pages = data->pagevec;
Chuck Lever462d5b32006-03-20 13:44:32 -0500653 data->args.count = bytes;
654 data->res.fattr = &data->fattr;
655 data->res.count = bytes;
Chuck Lever47989d72006-03-20 13:44:32 -0500656 data->res.verf = &data->verf;
Chuck Lever462d5b32006-03-20 13:44:32 -0500657
658 rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC,
659 &nfs_write_direct_ops, data);
Trond Myklebustfad61492006-03-20 13:44:36 -0500660 NFS_PROTO(inode)->write_setup(data, sync);
Chuck Lever462d5b32006-03-20 13:44:32 -0500661
662 data->task.tk_priority = RPC_PRIORITY_NORMAL;
663 data->task.tk_cookie = (unsigned long) inode;
664
665 lock_kernel();
666 rpc_execute(&data->task);
667 unlock_kernel();
668
Chuck Lever606bbba2006-03-20 13:44:42 -0500669 dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n",
Chuck Lever462d5b32006-03-20 13:44:32 -0500670 data->task.tk_pid,
671 inode->i_sb->s_id,
672 (long long)NFS_FILEID(inode),
673 bytes,
674 (unsigned long long)data->args.offset);
675
Trond Myklebust607f31e2006-06-28 16:52:45 -0400676 started += bytes;
677 user_addr += bytes;
Chuck Lever88467052006-03-20 13:44:34 -0500678 pos += bytes;
Chuck Lever462d5b32006-03-20 13:44:32 -0500679 pgbase += bytes;
Chuck Lever462d5b32006-03-20 13:44:32 -0500680 pgbase &= ~PAGE_MASK;
681
682 count -= bytes;
683 } while (count != 0);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400684
685 if (put_dreq(dreq))
686 nfs_direct_write_complete(dreq, inode);
687
688 if (started)
689 return 0;
690 return result < 0 ? (ssize_t) result : -EFAULT;
Chuck Lever462d5b32006-03-20 13:44:32 -0500691}
692
Trond Myklebust607f31e2006-06-28 16:52:45 -0400693static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Trond Myklebust607f31e2006-06-28 16:52:45 -0400695 ssize_t result = 0;
Chuck Lever462d5b32006-03-20 13:44:32 -0500696 sigset_t oldset;
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500697 struct inode *inode = iocb->ki_filp->f_mapping->host;
Chuck Lever462d5b32006-03-20 13:44:32 -0500698 struct rpc_clnt *clnt = NFS_CLIENT(inode);
699 struct nfs_direct_req *dreq;
Trond Myklebustfad61492006-03-20 13:44:36 -0500700 size_t wsize = NFS_SERVER(inode)->wsize;
701 int sync = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Trond Myklebust607f31e2006-06-28 16:52:45 -0400703 dreq = nfs_direct_req_alloc();
Chuck Lever462d5b32006-03-20 13:44:32 -0500704 if (!dreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -ENOMEM;
Trond Myklebust607f31e2006-06-28 16:52:45 -0400706 nfs_alloc_commit_data(dreq);
707
Trond Myklebustfad61492006-03-20 13:44:36 -0500708 if (dreq->commit_data == NULL || count < wsize)
709 sync = FLUSH_STABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500711 dreq->inode = inode;
Trond Myklebusta8881f52006-03-20 13:44:36 -0500712 dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data);
Chuck Leverc89f2ee2006-03-20 13:44:33 -0500713 if (!is_sync_kiocb(iocb))
714 dreq->iocb = iocb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Chuck Lever47989d72006-03-20 13:44:32 -0500716 nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 nfs_begin_data_update(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Chuck Lever462d5b32006-03-20 13:44:32 -0500720 rpc_clnt_sigmask(clnt, &oldset);
Trond Myklebust607f31e2006-06-28 16:52:45 -0400721 result = nfs_direct_write_schedule(dreq, user_addr, count, pos, sync);
722 if (!result)
723 result = nfs_direct_wait(dreq);
Chuck Lever462d5b32006-03-20 13:44:32 -0500724 rpc_clnt_sigunmask(clnt, &oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return result;
727}
728
729/**
730 * nfs_file_direct_read - file direct read operation for NFS files
731 * @iocb: target I/O control block
732 * @buf: user's buffer into which to read data
Chuck Lever88467052006-03-20 13:44:34 -0500733 * @count: number of bytes to read
734 * @pos: byte offset in file where reading starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 *
736 * We use this function for direct reads instead of calling
737 * generic_file_aio_read() in order to avoid gfar's check to see if
738 * the request starts before the end of the file. For that check
739 * to work, we must generate a GETATTR before each direct read, and
740 * even then there is a window between the GETATTR and the subsequent
Chuck Lever88467052006-03-20 13:44:34 -0500741 * READ where the file size could change. Our preference is simply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * to do all reads the application wants, and the server will take
743 * care of managing the end of file boundary.
Chuck Lever88467052006-03-20 13:44:34 -0500744 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 * This function also eliminates unnecessarily updating the file's
746 * atime locally, as the NFS server sets the file's atime, and this
747 * client must read the updated atime from the server back into its
748 * cache.
749 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500750ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 ssize_t retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Chuck Leverce1a8e62005-11-30 18:08:17 -0500756 dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500757 file->f_dentry->d_parent->d_name.name,
758 file->f_dentry->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500759 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (count < 0)
762 goto out;
763 retval = -EFAULT;
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500764 if (!access_ok(VERIFY_WRITE, buf, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto out;
766 retval = 0;
767 if (!count)
768 goto out;
769
Trond Myklebust29884df2005-12-13 16:13:54 -0500770 retval = nfs_sync_mapping(mapping);
771 if (retval)
772 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Trond Myklebust607f31e2006-06-28 16:52:45 -0400774 retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (retval > 0)
Chuck Lever0cdd80d2006-03-20 13:44:29 -0500776 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778out:
779 return retval;
780}
781
782/**
783 * nfs_file_direct_write - file direct write operation for NFS files
784 * @iocb: target I/O control block
785 * @buf: user's buffer from which to write data
Chuck Lever88467052006-03-20 13:44:34 -0500786 * @count: number of bytes to write
787 * @pos: byte offset in file where writing starts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 *
789 * We use this function for direct writes instead of calling
790 * generic_file_aio_write() in order to avoid taking the inode
791 * semaphore and updating the i_size. The NFS server will set
792 * the new i_size and this client must read the updated size
793 * back into its cache. We let the server do generic write
794 * parameter checking and report problems.
795 *
796 * We also avoid an unnecessary invocation of generic_osync_inode(),
797 * as it is fairly meaningless to sync the metadata of an NFS file.
798 *
799 * We eliminate local atime updates, see direct read above.
800 *
801 * We avoid unnecessary page cache invalidations for normal cached
802 * readers of this file.
803 *
804 * Note that O_APPEND is not supported for NFS direct writes, as there
805 * is no atomic O_APPEND write facility in the NFS protocol.
806 */
Chuck Leverd4cc9482006-03-20 13:44:28 -0500807ssize_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 -0700808{
Chuck Leverce1a8e62005-11-30 18:08:17 -0500809 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 struct file *file = iocb->ki_filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct address_space *mapping = file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Chuck Leverce1a8e62005-11-30 18:08:17 -0500813 dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
Chuck Lever0bbacc42005-11-01 16:53:32 -0500814 file->f_dentry->d_parent->d_name.name,
Chuck Leverce1a8e62005-11-30 18:08:17 -0500815 file->f_dentry->d_name.name,
816 (unsigned long) count, (long long) pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Chuck Leverce1a8e62005-11-30 18:08:17 -0500818 retval = generic_write_checks(file, &pos, &count, 0);
819 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500821
822 retval = -EINVAL;
823 if ((ssize_t) count < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 retval = 0;
826 if (!count)
827 goto out;
Chuck Leverce1a8e62005-11-30 18:08:17 -0500828
829 retval = -EFAULT;
Chuck Lever47989d72006-03-20 13:44:32 -0500830 if (!access_ok(VERIFY_READ, buf, count))
Chuck Leverce1a8e62005-11-30 18:08:17 -0500831 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Trond Myklebust29884df2005-12-13 16:13:54 -0500833 retval = nfs_sync_mapping(mapping);
834 if (retval)
835 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Trond Myklebust607f31e2006-06-28 16:52:45 -0400837 retval = nfs_direct_write(iocb, (unsigned long) buf, count, pos);
Chuck Lever9eafa8c2006-03-20 13:44:33 -0500838
839 /*
840 * XXX: nfs_end_data_update() already ensures this file's
841 * cached data is subsequently invalidated. Do we really
842 * need to call invalidate_inode_pages2() again here?
843 *
844 * For aio writes, this invalidation will almost certainly
845 * occur before the writes complete. Kind of racey.
846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (mapping->nrpages)
848 invalidate_inode_pages2(mapping);
Chuck Lever9eafa8c2006-03-20 13:44:33 -0500849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (retval > 0)
Chuck Leverce1a8e62005-11-30 18:08:17 -0500851 iocb->ki_pos = pos + retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853out:
854 return retval;
855}
856
Chuck Lever88467052006-03-20 13:44:34 -0500857/**
858 * nfs_init_directcache - create a slab cache for nfs_direct_req structures
859 *
860 */
David Howellsf7b422b2006-06-09 09:34:33 -0400861int __init nfs_init_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
864 sizeof(struct nfs_direct_req),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800865 0, (SLAB_RECLAIM_ACCOUNT|
866 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 NULL, NULL);
868 if (nfs_direct_cachep == NULL)
869 return -ENOMEM;
870
871 return 0;
872}
873
Chuck Lever88467052006-03-20 13:44:34 -0500874/**
David Howellsf7b422b2006-06-09 09:34:33 -0400875 * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
Chuck Lever88467052006-03-20 13:44:34 -0500876 *
877 */
David Brownell266bee82006-06-27 12:59:15 -0700878void nfs_destroy_directcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 if (kmem_cache_destroy(nfs_direct_cachep))
881 printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
882}