]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | |
88467055 | 10 | * (multiple copies of the same instance running on separate hosts) |
1da177e4 | 11 | * implement their own cache coherency protocol that subsumes file |
88467055 CL |
12 | * 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 | |
1da177e4 LT |
15 | * 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 | |
88467055 | 37 | * 04 May 2005 support O_DIRECT with aio --cel |
1da177e4 LT |
38 | * |
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 | ||
91d5b470 CL |
58 | #include "iostat.h" |
59 | ||
1da177e4 | 60 | #define NFSDBG_FACILITY NFSDBG_VFS |
1da177e4 LT |
61 | |
62 | static kmem_cache_t *nfs_direct_cachep; | |
63 | ||
64 | /* | |
65 | * This represents a set of asynchronous requests that we're waiting on | |
66 | */ | |
67 | struct nfs_direct_req { | |
68 | struct kref kref; /* release manager */ | |
15ce4a0c CL |
69 | |
70 | /* I/O parameters */ | |
fad61490 TM |
71 | struct list_head list, /* nfs_read/write_data structs */ |
72 | rewrite_list; /* saved nfs_write_data structs */ | |
a8881f5a | 73 | struct nfs_open_context *ctx; /* file open context info */ |
99514f8f | 74 | struct kiocb * iocb; /* controlling i/o request */ |
88467055 | 75 | struct inode * inode; /* target file of i/o */ |
fad61490 TM |
76 | 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 */ | |
1da177e4 LT |
79 | struct page ** pages; /* pages in our buffer */ |
80 | unsigned int npages; /* count of pages */ | |
15ce4a0c CL |
81 | |
82 | /* completion state */ | |
83 | spinlock_t lock; /* protect completion state */ | |
84 | int outstanding; /* i/os we're waiting for */ | |
85 | ssize_t count, /* bytes actually processed */ | |
1da177e4 | 86 | error; /* any reported error */ |
d72b7a6b | 87 | struct completion completion; /* wait for i/o completion */ |
fad61490 TM |
88 | |
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 */ | |
1da177e4 LT |
95 | }; |
96 | ||
fad61490 TM |
97 | static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync); |
98 | static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode); | |
1da177e4 LT |
99 | |
100 | /** | |
b8a32e2b CL |
101 | * nfs_direct_IO - NFS address space operation for direct I/O |
102 | * @rw: direction (read or write) | |
103 | * @iocb: target I/O control block | |
104 | * @iov: array of vectors that define I/O buffer | |
105 | * @pos: offset in file to begin the operation | |
106 | * @nr_segs: size of iovec array | |
107 | * | |
108 | * The presence of this routine in the address space ops vector means | |
109 | * the NFS client supports direct I/O. However, we shunt off direct | |
110 | * read and write requests before the VFS gets them, so this method | |
111 | * should never be called. | |
1da177e4 | 112 | */ |
b8a32e2b CL |
113 | ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs) |
114 | { | |
b8a32e2b | 115 | dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n", |
e99170ff TM |
116 | iocb->ki_filp->f_dentry->d_name.name, |
117 | (long long) pos, nr_segs); | |
b8a32e2b CL |
118 | |
119 | return -EINVAL; | |
120 | } | |
121 | ||
6b45d858 TM |
122 | static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty) |
123 | { | |
124 | int i; | |
125 | for (i = 0; i < npages; i++) { | |
126 | struct page *page = pages[i]; | |
127 | if (do_dirty && !PageCompound(page)) | |
128 | set_page_dirty_lock(page); | |
129 | page_cache_release(page); | |
130 | } | |
131 | kfree(pages); | |
132 | } | |
133 | ||
d4cc948b | 134 | static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages) |
1da177e4 LT |
135 | { |
136 | int result = -ENOMEM; | |
137 | unsigned long page_count; | |
138 | size_t array_size; | |
139 | ||
1da177e4 LT |
140 | page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
141 | page_count -= user_addr >> PAGE_SHIFT; | |
142 | ||
143 | array_size = (page_count * sizeof(struct page *)); | |
144 | *pages = kmalloc(array_size, GFP_KERNEL); | |
145 | if (*pages) { | |
146 | down_read(¤t->mm->mmap_sem); | |
147 | result = get_user_pages(current, current->mm, user_addr, | |
148 | page_count, (rw == READ), 0, | |
149 | *pages, NULL); | |
150 | up_read(¤t->mm->mmap_sem); | |
6b45d858 TM |
151 | if (result != page_count) { |
152 | /* | |
153 | * If we got fewer pages than expected from | |
154 | * get_user_pages(), the user buffer runs off the | |
155 | * end of a mapping; return EFAULT. | |
156 | */ | |
157 | if (result >= 0) { | |
158 | nfs_free_user_pages(*pages, result, 0); | |
159 | result = -EFAULT; | |
160 | } else | |
161 | kfree(*pages); | |
143f412e | 162 | *pages = NULL; |
143f412e | 163 | } |
1da177e4 LT |
164 | } |
165 | return result; | |
166 | } | |
167 | ||
93619e59 | 168 | static inline struct nfs_direct_req *nfs_direct_req_alloc(void) |
1da177e4 | 169 | { |
93619e59 CL |
170 | struct nfs_direct_req *dreq; |
171 | ||
172 | dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL); | |
173 | if (!dreq) | |
174 | return NULL; | |
175 | ||
176 | kref_init(&dreq->kref); | |
d72b7a6b | 177 | init_completion(&dreq->completion); |
93619e59 | 178 | INIT_LIST_HEAD(&dreq->list); |
fad61490 | 179 | INIT_LIST_HEAD(&dreq->rewrite_list); |
93619e59 | 180 | dreq->iocb = NULL; |
a8881f5a | 181 | dreq->ctx = NULL; |
15ce4a0c CL |
182 | spin_lock_init(&dreq->lock); |
183 | dreq->outstanding = 0; | |
184 | dreq->count = 0; | |
185 | dreq->error = 0; | |
fad61490 | 186 | dreq->flags = 0; |
93619e59 CL |
187 | |
188 | return dreq; | |
1da177e4 LT |
189 | } |
190 | ||
1da177e4 LT |
191 | static void nfs_direct_req_release(struct kref *kref) |
192 | { | |
193 | struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); | |
a8881f5a TM |
194 | |
195 | if (dreq->ctx != NULL) | |
196 | put_nfs_open_context(dreq->ctx); | |
1da177e4 LT |
197 | kmem_cache_free(nfs_direct_cachep, dreq); |
198 | } | |
199 | ||
bc0fb201 CL |
200 | /* |
201 | * Collects and returns the final error value/byte-count. | |
202 | */ | |
203 | static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq) | |
204 | { | |
15ce4a0c | 205 | ssize_t result = -EIOCBQUEUED; |
bc0fb201 CL |
206 | |
207 | /* Async requests don't wait here */ | |
208 | if (dreq->iocb) | |
209 | goto out; | |
210 | ||
d72b7a6b | 211 | result = wait_for_completion_interruptible(&dreq->completion); |
bc0fb201 CL |
212 | |
213 | if (!result) | |
15ce4a0c | 214 | result = dreq->error; |
bc0fb201 | 215 | if (!result) |
15ce4a0c | 216 | result = dreq->count; |
bc0fb201 CL |
217 | |
218 | out: | |
219 | kref_put(&dreq->kref, nfs_direct_req_release); | |
220 | return (ssize_t) result; | |
221 | } | |
222 | ||
63ab46ab CL |
223 | /* |
224 | * We must hold a reference to all the pages in this direct read request | |
225 | * until the RPCs complete. This could be long *after* we are woken up in | |
226 | * nfs_direct_wait (for instance, if someone hits ^C on a slow server). | |
1da177e4 | 227 | * |
63ab46ab CL |
228 | * In addition, synchronous I/O uses a stack-allocated iocb. Thus we |
229 | * can't trust the iocb is still valid here if this is a synchronous | |
230 | * request. If the waiter is woken prematurely, the iocb is long gone. | |
231 | */ | |
232 | static void nfs_direct_complete(struct nfs_direct_req *dreq) | |
233 | { | |
234 | nfs_free_user_pages(dreq->pages, dreq->npages, 1); | |
235 | ||
236 | if (dreq->iocb) { | |
15ce4a0c | 237 | long res = (long) dreq->error; |
63ab46ab | 238 | if (!res) |
15ce4a0c | 239 | res = (long) dreq->count; |
63ab46ab | 240 | aio_complete(dreq->iocb, res, 0); |
d72b7a6b TM |
241 | } |
242 | complete_all(&dreq->completion); | |
63ab46ab CL |
243 | |
244 | kref_put(&dreq->kref, nfs_direct_req_release); | |
245 | } | |
246 | ||
d4cc948b | 247 | /* |
1da177e4 LT |
248 | * Note we also set the number of requests we have in the dreq when we are |
249 | * done. This prevents races with I/O completion so we will always wait | |
250 | * until all requests have been dispatched and completed. | |
251 | */ | |
5dd602f2 | 252 | static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize) |
1da177e4 LT |
253 | { |
254 | struct list_head *list; | |
255 | struct nfs_direct_req *dreq; | |
40859d7e | 256 | unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
1da177e4 | 257 | |
93619e59 | 258 | dreq = nfs_direct_req_alloc(); |
1da177e4 LT |
259 | if (!dreq) |
260 | return NULL; | |
261 | ||
1da177e4 LT |
262 | list = &dreq->list; |
263 | for(;;) { | |
40859d7e | 264 | struct nfs_read_data *data = nfs_readdata_alloc(rpages); |
1da177e4 LT |
265 | |
266 | if (unlikely(!data)) { | |
267 | while (!list_empty(list)) { | |
268 | data = list_entry(list->next, | |
269 | struct nfs_read_data, pages); | |
270 | list_del(&data->pages); | |
271 | nfs_readdata_free(data); | |
272 | } | |
273 | kref_put(&dreq->kref, nfs_direct_req_release); | |
274 | return NULL; | |
275 | } | |
276 | ||
277 | INIT_LIST_HEAD(&data->pages); | |
278 | list_add(&data->pages, list); | |
279 | ||
280 | data->req = (struct nfs_page *) dreq; | |
15ce4a0c | 281 | dreq->outstanding++; |
1da177e4 LT |
282 | if (nbytes <= rsize) |
283 | break; | |
284 | nbytes -= rsize; | |
285 | } | |
286 | kref_get(&dreq->kref); | |
1da177e4 LT |
287 | return dreq; |
288 | } | |
289 | ||
ec06c096 | 290 | static void nfs_direct_read_result(struct rpc_task *task, void *calldata) |
1da177e4 | 291 | { |
ec06c096 | 292 | struct nfs_read_data *data = calldata; |
1da177e4 LT |
293 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; |
294 | ||
ec06c096 TM |
295 | if (nfs_readpage_result(task, data) != 0) |
296 | return; | |
15ce4a0c CL |
297 | |
298 | spin_lock(&dreq->lock); | |
299 | ||
ec06c096 | 300 | if (likely(task->tk_status >= 0)) |
15ce4a0c | 301 | dreq->count += data->res.count; |
1da177e4 | 302 | else |
15ce4a0c | 303 | dreq->error = task->tk_status; |
1da177e4 | 304 | |
15ce4a0c CL |
305 | if (--dreq->outstanding) { |
306 | spin_unlock(&dreq->lock); | |
307 | return; | |
1da177e4 | 308 | } |
1da177e4 | 309 | |
15ce4a0c CL |
310 | spin_unlock(&dreq->lock); |
311 | nfs_direct_complete(dreq); | |
1da177e4 LT |
312 | } |
313 | ||
ec06c096 TM |
314 | static const struct rpc_call_ops nfs_read_direct_ops = { |
315 | .rpc_call_done = nfs_direct_read_result, | |
316 | .rpc_release = nfs_readdata_release, | |
317 | }; | |
318 | ||
d4cc948b | 319 | /* |
1da177e4 LT |
320 | * For each nfs_read_data struct that was allocated on the list, dispatch |
321 | * an NFS READ operation | |
322 | */ | |
fad61490 | 323 | static void nfs_direct_read_schedule(struct nfs_direct_req *dreq) |
1da177e4 | 324 | { |
a8881f5a TM |
325 | struct nfs_open_context *ctx = dreq->ctx; |
326 | struct inode *inode = ctx->dentry->d_inode; | |
1da177e4 LT |
327 | struct list_head *list = &dreq->list; |
328 | struct page **pages = dreq->pages; | |
fad61490 TM |
329 | size_t count = dreq->user_count; |
330 | loff_t pos = dreq->pos; | |
5dd602f2 | 331 | size_t rsize = NFS_SERVER(inode)->rsize; |
1da177e4 | 332 | unsigned int curpage, pgbase; |
1da177e4 LT |
333 | |
334 | curpage = 0; | |
fad61490 | 335 | pgbase = dreq->user_addr & ~PAGE_MASK; |
1da177e4 LT |
336 | do { |
337 | struct nfs_read_data *data; | |
5dd602f2 | 338 | size_t bytes; |
1da177e4 LT |
339 | |
340 | bytes = rsize; | |
341 | if (count < rsize) | |
342 | bytes = count; | |
343 | ||
5db3a7b2 | 344 | BUG_ON(list_empty(list)); |
1da177e4 LT |
345 | data = list_entry(list->next, struct nfs_read_data, pages); |
346 | list_del_init(&data->pages); | |
347 | ||
348 | data->inode = inode; | |
349 | data->cred = ctx->cred; | |
350 | data->args.fh = NFS_FH(inode); | |
351 | data->args.context = ctx; | |
88467055 | 352 | data->args.offset = pos; |
1da177e4 LT |
353 | data->args.pgbase = pgbase; |
354 | data->args.pages = &pages[curpage]; | |
355 | data->args.count = bytes; | |
356 | data->res.fattr = &data->fattr; | |
357 | data->res.eof = 0; | |
358 | data->res.count = bytes; | |
359 | ||
ec06c096 TM |
360 | rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, |
361 | &nfs_read_direct_ops, data); | |
1da177e4 LT |
362 | NFS_PROTO(inode)->read_setup(data); |
363 | ||
364 | data->task.tk_cookie = (unsigned long) inode; | |
1da177e4 LT |
365 | |
366 | lock_kernel(); | |
367 | rpc_execute(&data->task); | |
368 | unlock_kernel(); | |
369 | ||
606bbba0 | 370 | dfprintk(VFS, "NFS: %5u initiated direct read call (req %s/%Ld, %zu bytes @ offset %Lu)\n", |
1da177e4 LT |
371 | data->task.tk_pid, |
372 | inode->i_sb->s_id, | |
373 | (long long)NFS_FILEID(inode), | |
374 | bytes, | |
375 | (unsigned long long)data->args.offset); | |
376 | ||
88467055 | 377 | pos += bytes; |
1da177e4 LT |
378 | pgbase += bytes; |
379 | curpage += pgbase >> PAGE_SHIFT; | |
380 | pgbase &= ~PAGE_MASK; | |
381 | ||
382 | count -= bytes; | |
383 | } while (count != 0); | |
5db3a7b2 | 384 | BUG_ON(!list_empty(list)); |
1da177e4 LT |
385 | } |
386 | ||
88467055 | 387 | static 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) |
1da177e4 LT |
388 | { |
389 | ssize_t result; | |
390 | sigset_t oldset; | |
99514f8f | 391 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
1da177e4 LT |
392 | struct rpc_clnt *clnt = NFS_CLIENT(inode); |
393 | struct nfs_direct_req *dreq; | |
394 | ||
395 | dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize); | |
396 | if (!dreq) | |
397 | return -ENOMEM; | |
398 | ||
fad61490 TM |
399 | dreq->user_addr = user_addr; |
400 | dreq->user_count = count; | |
401 | dreq->pos = pos; | |
1da177e4 LT |
402 | dreq->pages = pages; |
403 | dreq->npages = nr_pages; | |
91d5b470 | 404 | dreq->inode = inode; |
a8881f5a | 405 | dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data); |
487b8372 CL |
406 | if (!is_sync_kiocb(iocb)) |
407 | dreq->iocb = iocb; | |
1da177e4 | 408 | |
91d5b470 | 409 | nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count); |
1da177e4 | 410 | rpc_clnt_sigmask(clnt, &oldset); |
fad61490 | 411 | nfs_direct_read_schedule(dreq); |
bc0fb201 | 412 | result = nfs_direct_wait(dreq); |
1da177e4 LT |
413 | rpc_clnt_sigunmask(clnt, &oldset); |
414 | ||
415 | return result; | |
416 | } | |
417 | ||
fad61490 | 418 | static void nfs_direct_free_writedata(struct nfs_direct_req *dreq) |
1da177e4 | 419 | { |
fad61490 TM |
420 | list_splice_init(&dreq->rewrite_list, &dreq->list); |
421 | while (!list_empty(&dreq->list)) { | |
422 | struct nfs_write_data *data = list_entry(dreq->list.next, struct nfs_write_data, pages); | |
423 | list_del(&data->pages); | |
424 | nfs_writedata_release(data); | |
425 | } | |
426 | } | |
1da177e4 | 427 | |
fad61490 TM |
428 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
429 | static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq) | |
430 | { | |
431 | struct list_head *pos; | |
1da177e4 | 432 | |
fad61490 TM |
433 | list_splice_init(&dreq->rewrite_list, &dreq->list); |
434 | list_for_each(pos, &dreq->list) | |
435 | dreq->outstanding++; | |
436 | dreq->count = 0; | |
437 | ||
438 | nfs_direct_write_schedule(dreq, FLUSH_STABLE); | |
439 | } | |
440 | ||
441 | static void nfs_direct_commit_result(struct rpc_task *task, void *calldata) | |
442 | { | |
443 | struct nfs_write_data *data = calldata; | |
444 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; | |
445 | ||
446 | /* Call the NFS version-specific code */ | |
447 | if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) | |
448 | return; | |
449 | if (unlikely(task->tk_status < 0)) { | |
450 | dreq->error = task->tk_status; | |
451 | dreq->flags = NFS_ODIRECT_RESCHED_WRITES; | |
452 | } | |
453 | if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) { | |
454 | dprintk("NFS: %5u commit verify failed\n", task->tk_pid); | |
455 | dreq->flags = NFS_ODIRECT_RESCHED_WRITES; | |
1da177e4 LT |
456 | } |
457 | ||
fad61490 TM |
458 | dprintk("NFS: %5u commit returned %d\n", task->tk_pid, task->tk_status); |
459 | nfs_direct_write_complete(dreq, data->inode); | |
1da177e4 LT |
460 | } |
461 | ||
fad61490 TM |
462 | static const struct rpc_call_ops nfs_commit_direct_ops = { |
463 | .rpc_call_done = nfs_direct_commit_result, | |
464 | .rpc_release = nfs_commit_release, | |
465 | }; | |
466 | ||
467 | static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq) | |
1da177e4 | 468 | { |
fad61490 | 469 | struct nfs_write_data *data = dreq->commit_data; |
1da177e4 | 470 | |
fad61490 | 471 | data->inode = dreq->inode; |
a8881f5a | 472 | data->cred = dreq->ctx->cred; |
1da177e4 | 473 | |
fad61490 TM |
474 | data->args.fh = NFS_FH(data->inode); |
475 | data->args.offset = dreq->pos; | |
476 | data->args.count = dreq->user_count; | |
477 | data->res.count = 0; | |
478 | data->res.fattr = &data->fattr; | |
479 | data->res.verf = &data->verf; | |
1da177e4 | 480 | |
fad61490 TM |
481 | rpc_init_task(&data->task, NFS_CLIENT(dreq->inode), RPC_TASK_ASYNC, |
482 | &nfs_commit_direct_ops, data); | |
483 | NFS_PROTO(data->inode)->commit_setup(data, 0); | |
1da177e4 | 484 | |
fad61490 TM |
485 | data->task.tk_priority = RPC_PRIORITY_NORMAL; |
486 | data->task.tk_cookie = (unsigned long)data->inode; | |
487 | /* Note: task.tk_ops->rpc_release will free dreq->commit_data */ | |
488 | dreq->commit_data = NULL; | |
1da177e4 | 489 | |
e99170ff | 490 | dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); |
1da177e4 | 491 | |
fad61490 TM |
492 | lock_kernel(); |
493 | rpc_execute(&data->task); | |
494 | unlock_kernel(); | |
495 | } | |
1da177e4 | 496 | |
fad61490 TM |
497 | static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) |
498 | { | |
499 | int flags = dreq->flags; | |
1da177e4 | 500 | |
fad61490 TM |
501 | dreq->flags = 0; |
502 | switch (flags) { | |
503 | case NFS_ODIRECT_DO_COMMIT: | |
504 | nfs_direct_commit_schedule(dreq); | |
1da177e4 | 505 | break; |
fad61490 TM |
506 | case NFS_ODIRECT_RESCHED_WRITES: |
507 | nfs_direct_write_reschedule(dreq); | |
508 | break; | |
509 | default: | |
510 | nfs_end_data_update(inode); | |
511 | if (dreq->commit_data != NULL) | |
512 | nfs_commit_free(dreq->commit_data); | |
513 | nfs_direct_free_writedata(dreq); | |
514 | nfs_direct_complete(dreq); | |
515 | } | |
516 | } | |
1da177e4 | 517 | |
fad61490 TM |
518 | static void nfs_alloc_commit_data(struct nfs_direct_req *dreq) |
519 | { | |
520 | dreq->commit_data = nfs_commit_alloc(0); | |
521 | if (dreq->commit_data != NULL) | |
522 | dreq->commit_data->req = (struct nfs_page *) dreq; | |
523 | } | |
524 | #else | |
525 | static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq) | |
526 | { | |
527 | dreq->commit_data = NULL; | |
528 | } | |
1da177e4 | 529 | |
fad61490 TM |
530 | static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode) |
531 | { | |
532 | nfs_end_data_update(inode); | |
533 | nfs_direct_free_writedata(dreq); | |
534 | nfs_direct_complete(dreq); | |
535 | } | |
536 | #endif | |
1da177e4 | 537 | |
462d5b32 | 538 | static struct nfs_direct_req *nfs_direct_write_alloc(size_t nbytes, size_t wsize) |
1da177e4 | 539 | { |
462d5b32 CL |
540 | struct list_head *list; |
541 | struct nfs_direct_req *dreq; | |
462d5b32 | 542 | unsigned int wpages = (wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
1da177e4 | 543 | |
462d5b32 CL |
544 | dreq = nfs_direct_req_alloc(); |
545 | if (!dreq) | |
546 | return NULL; | |
547 | ||
548 | list = &dreq->list; | |
549 | for(;;) { | |
550 | struct nfs_write_data *data = nfs_writedata_alloc(wpages); | |
1da177e4 | 551 | |
462d5b32 CL |
552 | if (unlikely(!data)) { |
553 | while (!list_empty(list)) { | |
554 | data = list_entry(list->next, | |
555 | struct nfs_write_data, pages); | |
556 | list_del(&data->pages); | |
557 | nfs_writedata_free(data); | |
558 | } | |
559 | kref_put(&dreq->kref, nfs_direct_req_release); | |
560 | return NULL; | |
561 | } | |
562 | ||
563 | INIT_LIST_HEAD(&data->pages); | |
564 | list_add(&data->pages, list); | |
565 | ||
566 | data->req = (struct nfs_page *) dreq; | |
15ce4a0c | 567 | dreq->outstanding++; |
462d5b32 CL |
568 | if (nbytes <= wsize) |
569 | break; | |
570 | nbytes -= wsize; | |
1da177e4 | 571 | } |
1da177e4 | 572 | |
fad61490 | 573 | nfs_alloc_commit_data(dreq); |
1da177e4 | 574 | |
462d5b32 | 575 | kref_get(&dreq->kref); |
462d5b32 | 576 | return dreq; |
1da177e4 LT |
577 | } |
578 | ||
462d5b32 | 579 | static void nfs_direct_write_result(struct rpc_task *task, void *calldata) |
1da177e4 | 580 | { |
462d5b32 CL |
581 | struct nfs_write_data *data = calldata; |
582 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; | |
583 | int status = task->tk_status; | |
584 | ||
585 | if (nfs_writeback_done(task, data) != 0) | |
586 | return; | |
462d5b32 | 587 | |
15ce4a0c | 588 | spin_lock(&dreq->lock); |
1da177e4 | 589 | |
462d5b32 | 590 | if (likely(status >= 0)) |
15ce4a0c | 591 | dreq->count += data->res.count; |
462d5b32 | 592 | else |
fad61490 | 593 | dreq->error = task->tk_status; |
1da177e4 | 594 | |
fad61490 TM |
595 | if (data->res.verf->committed != NFS_FILE_SYNC) { |
596 | switch (dreq->flags) { | |
597 | case 0: | |
598 | memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf)); | |
599 | dreq->flags = NFS_ODIRECT_DO_COMMIT; | |
1da177e4 | 600 | break; |
fad61490 TM |
601 | case NFS_ODIRECT_DO_COMMIT: |
602 | if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) { | |
603 | dprintk("NFS: %5u write verify failed\n", task->tk_pid); | |
604 | dreq->flags = NFS_ODIRECT_RESCHED_WRITES; | |
605 | } | |
1da177e4 | 606 | } |
1da177e4 | 607 | } |
fad61490 TM |
608 | /* In case we have to resend */ |
609 | data->args.stable = NFS_FILE_SYNC; | |
610 | ||
611 | spin_unlock(&dreq->lock); | |
1da177e4 LT |
612 | } |
613 | ||
fad61490 TM |
614 | /* |
615 | * NB: Return the value of the first error return code. Subsequent | |
616 | * errors after the first one are ignored. | |
1da177e4 | 617 | */ |
fad61490 | 618 | static void nfs_direct_write_release(void *calldata) |
1da177e4 | 619 | { |
fad61490 TM |
620 | struct nfs_write_data *data = calldata; |
621 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; | |
1da177e4 | 622 | |
fad61490 | 623 | spin_lock(&dreq->lock); |
15ce4a0c CL |
624 | if (--dreq->outstanding) { |
625 | spin_unlock(&dreq->lock); | |
626 | return; | |
1da177e4 | 627 | } |
15ce4a0c CL |
628 | spin_unlock(&dreq->lock); |
629 | ||
fad61490 | 630 | nfs_direct_write_complete(dreq, data->inode); |
462d5b32 CL |
631 | } |
632 | ||
633 | static const struct rpc_call_ops nfs_write_direct_ops = { | |
634 | .rpc_call_done = nfs_direct_write_result, | |
fad61490 | 635 | .rpc_release = nfs_direct_write_release, |
462d5b32 CL |
636 | }; |
637 | ||
638 | /* | |
639 | * For each nfs_write_data struct that was allocated on the list, dispatch | |
640 | * an NFS WRITE operation | |
462d5b32 | 641 | */ |
fad61490 | 642 | static void nfs_direct_write_schedule(struct nfs_direct_req *dreq, int sync) |
462d5b32 | 643 | { |
a8881f5a TM |
644 | struct nfs_open_context *ctx = dreq->ctx; |
645 | struct inode *inode = ctx->dentry->d_inode; | |
462d5b32 CL |
646 | struct list_head *list = &dreq->list; |
647 | struct page **pages = dreq->pages; | |
fad61490 TM |
648 | size_t count = dreq->user_count; |
649 | loff_t pos = dreq->pos; | |
462d5b32 CL |
650 | size_t wsize = NFS_SERVER(inode)->wsize; |
651 | unsigned int curpage, pgbase; | |
1da177e4 | 652 | |
1da177e4 | 653 | curpage = 0; |
fad61490 | 654 | pgbase = dreq->user_addr & ~PAGE_MASK; |
1da177e4 | 655 | do { |
462d5b32 CL |
656 | struct nfs_write_data *data; |
657 | size_t bytes; | |
658 | ||
659 | bytes = wsize; | |
660 | if (count < wsize) | |
661 | bytes = count; | |
662 | ||
5db3a7b2 | 663 | BUG_ON(list_empty(list)); |
462d5b32 | 664 | data = list_entry(list->next, struct nfs_write_data, pages); |
fad61490 | 665 | list_move_tail(&data->pages, &dreq->rewrite_list); |
462d5b32 CL |
666 | |
667 | data->inode = inode; | |
668 | data->cred = ctx->cred; | |
669 | data->args.fh = NFS_FH(inode); | |
670 | data->args.context = ctx; | |
88467055 | 671 | data->args.offset = pos; |
462d5b32 CL |
672 | data->args.pgbase = pgbase; |
673 | data->args.pages = &pages[curpage]; | |
674 | data->args.count = bytes; | |
675 | data->res.fattr = &data->fattr; | |
676 | data->res.count = bytes; | |
47989d74 | 677 | data->res.verf = &data->verf; |
462d5b32 CL |
678 | |
679 | rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, | |
680 | &nfs_write_direct_ops, data); | |
fad61490 | 681 | NFS_PROTO(inode)->write_setup(data, sync); |
1da177e4 | 682 | |
462d5b32 CL |
683 | data->task.tk_priority = RPC_PRIORITY_NORMAL; |
684 | data->task.tk_cookie = (unsigned long) inode; | |
1da177e4 LT |
685 | |
686 | lock_kernel(); | |
462d5b32 | 687 | rpc_execute(&data->task); |
1da177e4 LT |
688 | unlock_kernel(); |
689 | ||
606bbba0 | 690 | dfprintk(VFS, "NFS: %5u initiated direct write call (req %s/%Ld, %zu bytes @ offset %Lu)\n", |
462d5b32 CL |
691 | data->task.tk_pid, |
692 | inode->i_sb->s_id, | |
693 | (long long)NFS_FILEID(inode), | |
694 | bytes, | |
695 | (unsigned long long)data->args.offset); | |
1da177e4 | 696 | |
88467055 | 697 | pos += bytes; |
462d5b32 CL |
698 | pgbase += bytes; |
699 | curpage += pgbase >> PAGE_SHIFT; | |
700 | pgbase &= ~PAGE_MASK; | |
1da177e4 | 701 | |
462d5b32 CL |
702 | count -= bytes; |
703 | } while (count != 0); | |
5db3a7b2 | 704 | BUG_ON(!list_empty(list)); |
462d5b32 | 705 | } |
1da177e4 | 706 | |
88467055 | 707 | static ssize_t nfs_direct_write(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t pos, struct page **pages, int nr_pages) |
462d5b32 CL |
708 | { |
709 | ssize_t result; | |
710 | sigset_t oldset; | |
c89f2ee5 | 711 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
462d5b32 CL |
712 | struct rpc_clnt *clnt = NFS_CLIENT(inode); |
713 | struct nfs_direct_req *dreq; | |
fad61490 TM |
714 | size_t wsize = NFS_SERVER(inode)->wsize; |
715 | int sync = 0; | |
1da177e4 | 716 | |
fad61490 | 717 | dreq = nfs_direct_write_alloc(count, wsize); |
462d5b32 CL |
718 | if (!dreq) |
719 | return -ENOMEM; | |
fad61490 TM |
720 | if (dreq->commit_data == NULL || count < wsize) |
721 | sync = FLUSH_STABLE; | |
1da177e4 | 722 | |
fad61490 TM |
723 | dreq->user_addr = user_addr; |
724 | dreq->user_count = count; | |
725 | dreq->pos = pos; | |
462d5b32 CL |
726 | dreq->pages = pages; |
727 | dreq->npages = nr_pages; | |
c89f2ee5 | 728 | dreq->inode = inode; |
a8881f5a | 729 | dreq->ctx = get_nfs_open_context((struct nfs_open_context *)iocb->ki_filp->private_data); |
c89f2ee5 CL |
730 | if (!is_sync_kiocb(iocb)) |
731 | dreq->iocb = iocb; | |
1da177e4 | 732 | |
47989d74 CL |
733 | nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, count); |
734 | ||
462d5b32 | 735 | nfs_begin_data_update(inode); |
1da177e4 | 736 | |
462d5b32 | 737 | rpc_clnt_sigmask(clnt, &oldset); |
fad61490 | 738 | nfs_direct_write_schedule(dreq, sync); |
c89f2ee5 | 739 | result = nfs_direct_wait(dreq); |
462d5b32 | 740 | rpc_clnt_sigunmask(clnt, &oldset); |
1da177e4 | 741 | |
1da177e4 LT |
742 | return result; |
743 | } | |
744 | ||
745 | /** | |
746 | * nfs_file_direct_read - file direct read operation for NFS files | |
747 | * @iocb: target I/O control block | |
748 | * @buf: user's buffer into which to read data | |
88467055 CL |
749 | * @count: number of bytes to read |
750 | * @pos: byte offset in file where reading starts | |
1da177e4 LT |
751 | * |
752 | * We use this function for direct reads instead of calling | |
753 | * generic_file_aio_read() in order to avoid gfar's check to see if | |
754 | * the request starts before the end of the file. For that check | |
755 | * to work, we must generate a GETATTR before each direct read, and | |
756 | * even then there is a window between the GETATTR and the subsequent | |
88467055 | 757 | * READ where the file size could change. Our preference is simply |
1da177e4 LT |
758 | * to do all reads the application wants, and the server will take |
759 | * care of managing the end of file boundary. | |
88467055 | 760 | * |
1da177e4 LT |
761 | * This function also eliminates unnecessarily updating the file's |
762 | * atime locally, as the NFS server sets the file's atime, and this | |
763 | * client must read the updated atime from the server back into its | |
764 | * cache. | |
765 | */ | |
d4cc948b | 766 | ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos) |
1da177e4 LT |
767 | { |
768 | ssize_t retval = -EINVAL; | |
0cdd80d0 CL |
769 | int page_count; |
770 | struct page **pages; | |
1da177e4 | 771 | struct file *file = iocb->ki_filp; |
1da177e4 | 772 | struct address_space *mapping = file->f_mapping; |
1da177e4 | 773 | |
ce1a8e67 | 774 | dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n", |
0bbacc40 CL |
775 | file->f_dentry->d_parent->d_name.name, |
776 | file->f_dentry->d_name.name, | |
ce1a8e67 | 777 | (unsigned long) count, (long long) pos); |
1da177e4 | 778 | |
1da177e4 LT |
779 | if (count < 0) |
780 | goto out; | |
781 | retval = -EFAULT; | |
0cdd80d0 | 782 | if (!access_ok(VERIFY_WRITE, buf, count)) |
1da177e4 LT |
783 | goto out; |
784 | retval = 0; | |
785 | if (!count) | |
786 | goto out; | |
787 | ||
29884df0 TM |
788 | retval = nfs_sync_mapping(mapping); |
789 | if (retval) | |
790 | goto out; | |
1da177e4 | 791 | |
6b45d858 | 792 | retval = nfs_get_user_pages(READ, (unsigned long) buf, |
0cdd80d0 | 793 | count, &pages); |
6b45d858 | 794 | if (retval < 0) |
0cdd80d0 | 795 | goto out; |
6b45d858 | 796 | page_count = retval; |
0cdd80d0 | 797 | |
99514f8f | 798 | retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos, |
0cdd80d0 | 799 | pages, page_count); |
1da177e4 | 800 | if (retval > 0) |
0cdd80d0 | 801 | iocb->ki_pos = pos + retval; |
1da177e4 LT |
802 | |
803 | out: | |
804 | return retval; | |
805 | } | |
806 | ||
807 | /** | |
808 | * nfs_file_direct_write - file direct write operation for NFS files | |
809 | * @iocb: target I/O control block | |
810 | * @buf: user's buffer from which to write data | |
88467055 CL |
811 | * @count: number of bytes to write |
812 | * @pos: byte offset in file where writing starts | |
1da177e4 LT |
813 | * |
814 | * We use this function for direct writes instead of calling | |
815 | * generic_file_aio_write() in order to avoid taking the inode | |
816 | * semaphore and updating the i_size. The NFS server will set | |
817 | * the new i_size and this client must read the updated size | |
818 | * back into its cache. We let the server do generic write | |
819 | * parameter checking and report problems. | |
820 | * | |
821 | * We also avoid an unnecessary invocation of generic_osync_inode(), | |
822 | * as it is fairly meaningless to sync the metadata of an NFS file. | |
823 | * | |
824 | * We eliminate local atime updates, see direct read above. | |
825 | * | |
826 | * We avoid unnecessary page cache invalidations for normal cached | |
827 | * readers of this file. | |
828 | * | |
829 | * Note that O_APPEND is not supported for NFS direct writes, as there | |
830 | * is no atomic O_APPEND write facility in the NFS protocol. | |
831 | */ | |
d4cc948b | 832 | ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos) |
1da177e4 | 833 | { |
ce1a8e67 | 834 | ssize_t retval; |
47989d74 CL |
835 | int page_count; |
836 | struct page **pages; | |
1da177e4 | 837 | struct file *file = iocb->ki_filp; |
1da177e4 | 838 | struct address_space *mapping = file->f_mapping; |
1da177e4 | 839 | |
ce1a8e67 | 840 | dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n", |
0bbacc40 | 841 | file->f_dentry->d_parent->d_name.name, |
ce1a8e67 CL |
842 | file->f_dentry->d_name.name, |
843 | (unsigned long) count, (long long) pos); | |
1da177e4 | 844 | |
ce1a8e67 CL |
845 | retval = generic_write_checks(file, &pos, &count, 0); |
846 | if (retval) | |
1da177e4 | 847 | goto out; |
ce1a8e67 CL |
848 | |
849 | retval = -EINVAL; | |
850 | if ((ssize_t) count < 0) | |
1da177e4 | 851 | goto out; |
1da177e4 LT |
852 | retval = 0; |
853 | if (!count) | |
854 | goto out; | |
ce1a8e67 CL |
855 | |
856 | retval = -EFAULT; | |
47989d74 | 857 | if (!access_ok(VERIFY_READ, buf, count)) |
ce1a8e67 | 858 | goto out; |
1da177e4 | 859 | |
29884df0 TM |
860 | retval = nfs_sync_mapping(mapping); |
861 | if (retval) | |
862 | goto out; | |
1da177e4 | 863 | |
6b45d858 | 864 | retval = nfs_get_user_pages(WRITE, (unsigned long) buf, |
47989d74 | 865 | count, &pages); |
6b45d858 | 866 | if (retval < 0) |
47989d74 | 867 | goto out; |
6b45d858 | 868 | page_count = retval; |
47989d74 | 869 | |
c89f2ee5 | 870 | retval = nfs_direct_write(iocb, (unsigned long) buf, count, |
47989d74 | 871 | pos, pages, page_count); |
9eafa8cc CL |
872 | |
873 | /* | |
874 | * XXX: nfs_end_data_update() already ensures this file's | |
875 | * cached data is subsequently invalidated. Do we really | |
876 | * need to call invalidate_inode_pages2() again here? | |
877 | * | |
878 | * For aio writes, this invalidation will almost certainly | |
879 | * occur before the writes complete. Kind of racey. | |
880 | */ | |
1da177e4 LT |
881 | if (mapping->nrpages) |
882 | invalidate_inode_pages2(mapping); | |
9eafa8cc | 883 | |
1da177e4 | 884 | if (retval > 0) |
ce1a8e67 | 885 | iocb->ki_pos = pos + retval; |
1da177e4 LT |
886 | |
887 | out: | |
888 | return retval; | |
889 | } | |
890 | ||
88467055 CL |
891 | /** |
892 | * nfs_init_directcache - create a slab cache for nfs_direct_req structures | |
893 | * | |
894 | */ | |
1da177e4 LT |
895 | int nfs_init_directcache(void) |
896 | { | |
897 | nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", | |
898 | sizeof(struct nfs_direct_req), | |
fffb60f9 PJ |
899 | 0, (SLAB_RECLAIM_ACCOUNT| |
900 | SLAB_MEM_SPREAD), | |
1da177e4 LT |
901 | NULL, NULL); |
902 | if (nfs_direct_cachep == NULL) | |
903 | return -ENOMEM; | |
904 | ||
905 | return 0; | |
906 | } | |
907 | ||
88467055 CL |
908 | /** |
909 | * nfs_init_directcache - destroy the slab cache for nfs_direct_req structures | |
910 | * | |
911 | */ | |
1da177e4 LT |
912 | void nfs_destroy_directcache(void) |
913 | { | |
914 | if (kmem_cache_destroy(nfs_direct_cachep)) | |
915 | printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n"); | |
916 | } |