]>
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 | |
10 | * (multiple copies of the same instance running on separate hosts) | |
11 | * implement their own cache coherency protocol that subsumes file | |
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 | |
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 | |
37 | * | |
38 | */ | |
39 | ||
40 | #include <linux/config.h> | |
41 | #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 | ||
91d5b470 CL |
57 | #include "iostat.h" |
58 | ||
1da177e4 LT |
59 | #define NFSDBG_FACILITY NFSDBG_VFS |
60 | #define MAX_DIRECTIO_SIZE (4096UL << PAGE_SHIFT) | |
61 | ||
143f412e | 62 | static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty); |
1da177e4 LT |
63 | static kmem_cache_t *nfs_direct_cachep; |
64 | ||
65 | /* | |
66 | * This represents a set of asynchronous requests that we're waiting on | |
67 | */ | |
68 | struct nfs_direct_req { | |
69 | struct kref kref; /* release manager */ | |
70 | struct list_head list; /* nfs_read_data structs */ | |
99514f8f CL |
71 | struct file * filp; /* file descriptor */ |
72 | struct kiocb * iocb; /* controlling i/o request */ | |
1da177e4 | 73 | wait_queue_head_t wait; /* wait for i/o completion */ |
91d5b470 | 74 | struct inode * inode; /* target file of I/O */ |
1da177e4 LT |
75 | struct page ** pages; /* pages in our buffer */ |
76 | unsigned int npages; /* count of pages */ | |
77 | atomic_t complete, /* i/os we're waiting for */ | |
78 | count, /* bytes actually processed */ | |
79 | error; /* any reported error */ | |
80 | }; | |
81 | ||
82 | ||
b8a32e2b CL |
83 | /** |
84 | * nfs_direct_IO - NFS address space operation for direct I/O | |
85 | * @rw: direction (read or write) | |
86 | * @iocb: target I/O control block | |
87 | * @iov: array of vectors that define I/O buffer | |
88 | * @pos: offset in file to begin the operation | |
89 | * @nr_segs: size of iovec array | |
90 | * | |
91 | * The presence of this routine in the address space ops vector means | |
92 | * the NFS client supports direct I/O. However, we shunt off direct | |
93 | * read and write requests before the VFS gets them, so this method | |
94 | * should never be called. | |
95 | */ | |
96 | ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs) | |
97 | { | |
98 | struct dentry *dentry = iocb->ki_filp->f_dentry; | |
99 | ||
100 | dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n", | |
101 | dentry->d_name.name, (long long) pos, nr_segs); | |
102 | ||
103 | return -EINVAL; | |
104 | } | |
105 | ||
d4cc948b | 106 | static inline int nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, struct page ***pages) |
1da177e4 LT |
107 | { |
108 | int result = -ENOMEM; | |
109 | unsigned long page_count; | |
110 | size_t array_size; | |
111 | ||
112 | /* set an arbitrary limit to prevent type overflow */ | |
113 | /* XXX: this can probably be as large as INT_MAX */ | |
114 | if (size > MAX_DIRECTIO_SIZE) { | |
115 | *pages = NULL; | |
116 | return -EFBIG; | |
117 | } | |
118 | ||
119 | page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
120 | page_count -= user_addr >> PAGE_SHIFT; | |
121 | ||
122 | array_size = (page_count * sizeof(struct page *)); | |
123 | *pages = kmalloc(array_size, GFP_KERNEL); | |
124 | if (*pages) { | |
125 | down_read(¤t->mm->mmap_sem); | |
126 | result = get_user_pages(current, current->mm, user_addr, | |
127 | page_count, (rw == READ), 0, | |
128 | *pages, NULL); | |
129 | up_read(¤t->mm->mmap_sem); | |
143f412e TM |
130 | /* |
131 | * If we got fewer pages than expected from get_user_pages(), | |
132 | * the user buffer runs off the end of a mapping; return EFAULT. | |
133 | */ | |
134 | if (result >= 0 && result < page_count) { | |
135 | nfs_free_user_pages(*pages, result, 0); | |
136 | *pages = NULL; | |
137 | result = -EFAULT; | |
138 | } | |
1da177e4 LT |
139 | } |
140 | return result; | |
141 | } | |
142 | ||
d4cc948b | 143 | static void nfs_free_user_pages(struct page **pages, int npages, int do_dirty) |
1da177e4 LT |
144 | { |
145 | int i; | |
146 | for (i = 0; i < npages; i++) { | |
566dd606 TM |
147 | struct page *page = pages[i]; |
148 | if (do_dirty && !PageCompound(page)) | |
149 | set_page_dirty_lock(page); | |
150 | page_cache_release(page); | |
1da177e4 LT |
151 | } |
152 | kfree(pages); | |
153 | } | |
154 | ||
1da177e4 LT |
155 | static void nfs_direct_req_release(struct kref *kref) |
156 | { | |
157 | struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); | |
158 | kmem_cache_free(nfs_direct_cachep, dreq); | |
159 | } | |
160 | ||
d4cc948b | 161 | /* |
1da177e4 LT |
162 | * Note we also set the number of requests we have in the dreq when we are |
163 | * done. This prevents races with I/O completion so we will always wait | |
164 | * until all requests have been dispatched and completed. | |
165 | */ | |
5dd602f2 | 166 | static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, size_t rsize) |
1da177e4 LT |
167 | { |
168 | struct list_head *list; | |
169 | struct nfs_direct_req *dreq; | |
170 | unsigned int reads = 0; | |
40859d7e | 171 | unsigned int rpages = (rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
1da177e4 LT |
172 | |
173 | dreq = kmem_cache_alloc(nfs_direct_cachep, SLAB_KERNEL); | |
174 | if (!dreq) | |
175 | return NULL; | |
176 | ||
177 | kref_init(&dreq->kref); | |
178 | init_waitqueue_head(&dreq->wait); | |
179 | INIT_LIST_HEAD(&dreq->list); | |
180 | atomic_set(&dreq->count, 0); | |
181 | atomic_set(&dreq->error, 0); | |
182 | ||
183 | list = &dreq->list; | |
184 | for(;;) { | |
40859d7e | 185 | struct nfs_read_data *data = nfs_readdata_alloc(rpages); |
1da177e4 LT |
186 | |
187 | if (unlikely(!data)) { | |
188 | while (!list_empty(list)) { | |
189 | data = list_entry(list->next, | |
190 | struct nfs_read_data, pages); | |
191 | list_del(&data->pages); | |
192 | nfs_readdata_free(data); | |
193 | } | |
194 | kref_put(&dreq->kref, nfs_direct_req_release); | |
195 | return NULL; | |
196 | } | |
197 | ||
198 | INIT_LIST_HEAD(&data->pages); | |
199 | list_add(&data->pages, list); | |
200 | ||
201 | data->req = (struct nfs_page *) dreq; | |
202 | reads++; | |
203 | if (nbytes <= rsize) | |
204 | break; | |
205 | nbytes -= rsize; | |
206 | } | |
207 | kref_get(&dreq->kref); | |
208 | atomic_set(&dreq->complete, reads); | |
209 | return dreq; | |
210 | } | |
211 | ||
d4cc948b | 212 | /* |
1da177e4 LT |
213 | * We must hold a reference to all the pages in this direct read request |
214 | * until the RPCs complete. This could be long *after* we are woken up in | |
215 | * nfs_direct_read_wait (for instance, if someone hits ^C on a slow server). | |
216 | */ | |
ec06c096 | 217 | static void nfs_direct_read_result(struct rpc_task *task, void *calldata) |
1da177e4 | 218 | { |
ec06c096 | 219 | struct nfs_read_data *data = calldata; |
1da177e4 LT |
220 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; |
221 | ||
ec06c096 TM |
222 | if (nfs_readpage_result(task, data) != 0) |
223 | return; | |
224 | if (likely(task->tk_status >= 0)) | |
1da177e4 LT |
225 | atomic_add(data->res.count, &dreq->count); |
226 | else | |
ec06c096 | 227 | atomic_set(&dreq->error, task->tk_status); |
1da177e4 LT |
228 | |
229 | if (unlikely(atomic_dec_and_test(&dreq->complete))) { | |
230 | nfs_free_user_pages(dreq->pages, dreq->npages, 1); | |
231 | wake_up(&dreq->wait); | |
232 | kref_put(&dreq->kref, nfs_direct_req_release); | |
233 | } | |
234 | } | |
235 | ||
ec06c096 TM |
236 | static const struct rpc_call_ops nfs_read_direct_ops = { |
237 | .rpc_call_done = nfs_direct_read_result, | |
238 | .rpc_release = nfs_readdata_release, | |
239 | }; | |
240 | ||
d4cc948b | 241 | /* |
1da177e4 LT |
242 | * For each nfs_read_data struct that was allocated on the list, dispatch |
243 | * an NFS READ operation | |
244 | */ | |
99514f8f | 245 | static void nfs_direct_read_schedule(struct nfs_direct_req *dreq, unsigned long user_addr, size_t count, loff_t file_offset) |
1da177e4 | 246 | { |
99514f8f CL |
247 | struct file *file = dreq->filp; |
248 | struct inode *inode = file->f_mapping->host; | |
249 | struct nfs_open_context *ctx = (struct nfs_open_context *) | |
250 | file->private_data; | |
1da177e4 LT |
251 | struct list_head *list = &dreq->list; |
252 | struct page **pages = dreq->pages; | |
5dd602f2 | 253 | size_t rsize = NFS_SERVER(inode)->rsize; |
1da177e4 | 254 | unsigned int curpage, pgbase; |
1da177e4 LT |
255 | |
256 | curpage = 0; | |
257 | pgbase = user_addr & ~PAGE_MASK; | |
258 | do { | |
259 | struct nfs_read_data *data; | |
5dd602f2 | 260 | size_t bytes; |
1da177e4 LT |
261 | |
262 | bytes = rsize; | |
263 | if (count < rsize) | |
264 | bytes = count; | |
265 | ||
266 | data = list_entry(list->next, struct nfs_read_data, pages); | |
267 | list_del_init(&data->pages); | |
268 | ||
269 | data->inode = inode; | |
270 | data->cred = ctx->cred; | |
271 | data->args.fh = NFS_FH(inode); | |
272 | data->args.context = ctx; | |
273 | data->args.offset = file_offset; | |
274 | data->args.pgbase = pgbase; | |
275 | data->args.pages = &pages[curpage]; | |
276 | data->args.count = bytes; | |
277 | data->res.fattr = &data->fattr; | |
278 | data->res.eof = 0; | |
279 | data->res.count = bytes; | |
280 | ||
ec06c096 TM |
281 | rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, |
282 | &nfs_read_direct_ops, data); | |
1da177e4 LT |
283 | NFS_PROTO(inode)->read_setup(data); |
284 | ||
285 | data->task.tk_cookie = (unsigned long) inode; | |
1da177e4 LT |
286 | |
287 | lock_kernel(); | |
288 | rpc_execute(&data->task); | |
289 | unlock_kernel(); | |
290 | ||
291 | dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n", | |
292 | data->task.tk_pid, | |
293 | inode->i_sb->s_id, | |
294 | (long long)NFS_FILEID(inode), | |
295 | bytes, | |
296 | (unsigned long long)data->args.offset); | |
297 | ||
298 | file_offset += bytes; | |
299 | pgbase += bytes; | |
300 | curpage += pgbase >> PAGE_SHIFT; | |
301 | pgbase &= ~PAGE_MASK; | |
302 | ||
303 | count -= bytes; | |
304 | } while (count != 0); | |
305 | } | |
306 | ||
d4cc948b | 307 | /* |
1da177e4 LT |
308 | * Collects and returns the final error value/byte-count. |
309 | */ | |
310 | static ssize_t nfs_direct_read_wait(struct nfs_direct_req *dreq, int intr) | |
311 | { | |
312 | int result = 0; | |
313 | ||
314 | if (intr) { | |
315 | result = wait_event_interruptible(dreq->wait, | |
316 | (atomic_read(&dreq->complete) == 0)); | |
317 | } else { | |
318 | wait_event(dreq->wait, (atomic_read(&dreq->complete) == 0)); | |
319 | } | |
320 | ||
321 | if (!result) | |
322 | result = atomic_read(&dreq->error); | |
323 | if (!result) | |
324 | result = atomic_read(&dreq->count); | |
325 | ||
326 | kref_put(&dreq->kref, nfs_direct_req_release); | |
327 | return (ssize_t) result; | |
328 | } | |
329 | ||
99514f8f | 330 | static ssize_t nfs_direct_read(struct kiocb *iocb, unsigned long user_addr, size_t count, loff_t file_offset, struct page **pages, unsigned int nr_pages) |
1da177e4 LT |
331 | { |
332 | ssize_t result; | |
333 | sigset_t oldset; | |
99514f8f | 334 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
1da177e4 LT |
335 | struct rpc_clnt *clnt = NFS_CLIENT(inode); |
336 | struct nfs_direct_req *dreq; | |
337 | ||
338 | dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize); | |
339 | if (!dreq) | |
340 | return -ENOMEM; | |
341 | ||
342 | dreq->pages = pages; | |
343 | dreq->npages = nr_pages; | |
91d5b470 | 344 | dreq->inode = inode; |
99514f8f | 345 | dreq->filp = iocb->ki_filp; |
1da177e4 | 346 | |
91d5b470 | 347 | nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count); |
1da177e4 | 348 | rpc_clnt_sigmask(clnt, &oldset); |
99514f8f | 349 | nfs_direct_read_schedule(dreq, user_addr, count, file_offset); |
1da177e4 LT |
350 | result = nfs_direct_read_wait(dreq, clnt->cl_intr); |
351 | rpc_clnt_sigunmask(clnt, &oldset); | |
352 | ||
353 | return result; | |
354 | } | |
355 | ||
d4cc948b | 356 | static ssize_t nfs_direct_write_seg(struct inode *inode, struct nfs_open_context *ctx, unsigned long user_addr, size_t count, loff_t file_offset, struct page **pages, int nr_pages) |
1da177e4 LT |
357 | { |
358 | const unsigned int wsize = NFS_SERVER(inode)->wsize; | |
359 | size_t request; | |
360 | int curpage, need_commit; | |
361 | ssize_t result, tot_bytes; | |
362 | struct nfs_writeverf first_verf; | |
363 | struct nfs_write_data *wdata; | |
364 | ||
40859d7e | 365 | wdata = nfs_writedata_alloc(NFS_SERVER(inode)->wpages); |
1da177e4 LT |
366 | if (!wdata) |
367 | return -ENOMEM; | |
368 | ||
369 | wdata->inode = inode; | |
370 | wdata->cred = ctx->cred; | |
371 | wdata->args.fh = NFS_FH(inode); | |
372 | wdata->args.context = ctx; | |
373 | wdata->args.stable = NFS_UNSTABLE; | |
374 | if (IS_SYNC(inode) || NFS_PROTO(inode)->version == 2 || count <= wsize) | |
375 | wdata->args.stable = NFS_FILE_SYNC; | |
376 | wdata->res.fattr = &wdata->fattr; | |
377 | wdata->res.verf = &wdata->verf; | |
378 | ||
379 | nfs_begin_data_update(inode); | |
380 | retry: | |
381 | need_commit = 0; | |
382 | tot_bytes = 0; | |
383 | curpage = 0; | |
384 | request = count; | |
385 | wdata->args.pgbase = user_addr & ~PAGE_MASK; | |
386 | wdata->args.offset = file_offset; | |
387 | do { | |
388 | wdata->args.count = request; | |
389 | if (wdata->args.count > wsize) | |
390 | wdata->args.count = wsize; | |
391 | wdata->args.pages = &pages[curpage]; | |
392 | ||
393 | dprintk("NFS: direct write: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n", | |
394 | wdata->args.count, (long long) wdata->args.offset, | |
395 | user_addr + tot_bytes, wdata->args.pgbase, curpage); | |
396 | ||
397 | lock_kernel(); | |
398 | result = NFS_PROTO(inode)->write(wdata); | |
399 | unlock_kernel(); | |
400 | ||
401 | if (result <= 0) { | |
402 | if (tot_bytes > 0) | |
403 | break; | |
404 | goto out; | |
405 | } | |
406 | ||
407 | if (tot_bytes == 0) | |
408 | memcpy(&first_verf.verifier, &wdata->verf.verifier, | |
409 | sizeof(first_verf.verifier)); | |
410 | if (wdata->verf.committed != NFS_FILE_SYNC) { | |
411 | need_commit = 1; | |
412 | if (memcmp(&first_verf.verifier, &wdata->verf.verifier, | |
19352456 | 413 | sizeof(first_verf.verifier))) |
1da177e4 LT |
414 | goto sync_retry; |
415 | } | |
416 | ||
417 | tot_bytes += result; | |
418 | ||
419 | /* in case of a short write: stop now, let the app recover */ | |
420 | if (result < wdata->args.count) | |
421 | break; | |
422 | ||
423 | wdata->args.offset += result; | |
424 | wdata->args.pgbase += result; | |
425 | curpage += wdata->args.pgbase >> PAGE_SHIFT; | |
426 | wdata->args.pgbase &= ~PAGE_MASK; | |
427 | request -= result; | |
428 | } while (request != 0); | |
429 | ||
430 | /* | |
431 | * Commit data written so far, even in the event of an error | |
432 | */ | |
433 | if (need_commit) { | |
434 | wdata->args.count = tot_bytes; | |
435 | wdata->args.offset = file_offset; | |
436 | ||
437 | lock_kernel(); | |
438 | result = NFS_PROTO(inode)->commit(wdata); | |
439 | unlock_kernel(); | |
440 | ||
441 | if (result < 0 || memcmp(&first_verf.verifier, | |
442 | &wdata->verf.verifier, | |
443 | sizeof(first_verf.verifier)) != 0) | |
444 | goto sync_retry; | |
445 | } | |
446 | result = tot_bytes; | |
447 | ||
448 | out: | |
951a143b | 449 | nfs_end_data_update(inode); |
1da177e4 LT |
450 | nfs_writedata_free(wdata); |
451 | return result; | |
452 | ||
453 | sync_retry: | |
454 | wdata->args.stable = NFS_FILE_SYNC; | |
455 | goto retry; | |
456 | } | |
457 | ||
d4cc948b | 458 | /* |
1da177e4 LT |
459 | * Upon return, generic_file_direct_IO invalidates any cached pages |
460 | * that non-direct readers might access, so they will pick up these | |
461 | * writes immediately. | |
462 | */ | |
d4cc948b | 463 | static ssize_t nfs_direct_write(struct inode *inode, struct nfs_open_context *ctx, const struct iovec *iov, loff_t file_offset, unsigned long nr_segs) |
1da177e4 LT |
464 | { |
465 | ssize_t tot_bytes = 0; | |
466 | unsigned long seg = 0; | |
467 | ||
468 | while ((seg < nr_segs) && (tot_bytes >= 0)) { | |
469 | ssize_t result; | |
470 | int page_count; | |
471 | struct page **pages; | |
472 | const struct iovec *vec = &iov[seg++]; | |
473 | unsigned long user_addr = (unsigned long) vec->iov_base; | |
474 | size_t size = vec->iov_len; | |
475 | ||
476 | page_count = nfs_get_user_pages(WRITE, user_addr, size, &pages); | |
477 | if (page_count < 0) { | |
478 | nfs_free_user_pages(pages, 0, 0); | |
479 | if (tot_bytes > 0) | |
480 | break; | |
481 | return page_count; | |
482 | } | |
483 | ||
91d5b470 | 484 | nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, size); |
1da177e4 LT |
485 | result = nfs_direct_write_seg(inode, ctx, user_addr, size, |
486 | file_offset, pages, page_count); | |
487 | nfs_free_user_pages(pages, page_count, 0); | |
488 | ||
489 | if (result <= 0) { | |
490 | if (tot_bytes > 0) | |
491 | break; | |
492 | return result; | |
493 | } | |
91d5b470 | 494 | nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result); |
1da177e4 LT |
495 | tot_bytes += result; |
496 | file_offset += result; | |
497 | if (result < size) | |
498 | break; | |
499 | } | |
500 | return tot_bytes; | |
501 | } | |
502 | ||
1da177e4 LT |
503 | /** |
504 | * nfs_file_direct_read - file direct read operation for NFS files | |
505 | * @iocb: target I/O control block | |
506 | * @buf: user's buffer into which to read data | |
507 | * count: number of bytes to read | |
508 | * pos: byte offset in file where reading starts | |
509 | * | |
510 | * We use this function for direct reads instead of calling | |
511 | * generic_file_aio_read() in order to avoid gfar's check to see if | |
512 | * the request starts before the end of the file. For that check | |
513 | * to work, we must generate a GETATTR before each direct read, and | |
514 | * even then there is a window between the GETATTR and the subsequent | |
515 | * READ where the file size could change. So our preference is simply | |
516 | * to do all reads the application wants, and the server will take | |
517 | * care of managing the end of file boundary. | |
518 | * | |
519 | * This function also eliminates unnecessarily updating the file's | |
520 | * atime locally, as the NFS server sets the file's atime, and this | |
521 | * client must read the updated atime from the server back into its | |
522 | * cache. | |
523 | */ | |
d4cc948b | 524 | ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos) |
1da177e4 LT |
525 | { |
526 | ssize_t retval = -EINVAL; | |
0cdd80d0 CL |
527 | int page_count; |
528 | struct page **pages; | |
1da177e4 | 529 | struct file *file = iocb->ki_filp; |
1da177e4 | 530 | struct address_space *mapping = file->f_mapping; |
1da177e4 | 531 | |
ce1a8e67 | 532 | dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n", |
0bbacc40 CL |
533 | file->f_dentry->d_parent->d_name.name, |
534 | file->f_dentry->d_name.name, | |
ce1a8e67 | 535 | (unsigned long) count, (long long) pos); |
1da177e4 LT |
536 | |
537 | if (!is_sync_kiocb(iocb)) | |
538 | goto out; | |
539 | if (count < 0) | |
540 | goto out; | |
541 | retval = -EFAULT; | |
0cdd80d0 | 542 | if (!access_ok(VERIFY_WRITE, buf, count)) |
1da177e4 LT |
543 | goto out; |
544 | retval = 0; | |
545 | if (!count) | |
546 | goto out; | |
547 | ||
29884df0 TM |
548 | retval = nfs_sync_mapping(mapping); |
549 | if (retval) | |
550 | goto out; | |
1da177e4 | 551 | |
0cdd80d0 CL |
552 | page_count = nfs_get_user_pages(READ, (unsigned long) buf, |
553 | count, &pages); | |
554 | if (page_count < 0) { | |
555 | nfs_free_user_pages(pages, 0, 0); | |
556 | retval = page_count; | |
557 | goto out; | |
558 | } | |
559 | ||
99514f8f | 560 | retval = nfs_direct_read(iocb, (unsigned long) buf, count, pos, |
0cdd80d0 | 561 | pages, page_count); |
1da177e4 | 562 | if (retval > 0) |
0cdd80d0 | 563 | iocb->ki_pos = pos + retval; |
1da177e4 LT |
564 | |
565 | out: | |
566 | return retval; | |
567 | } | |
568 | ||
569 | /** | |
570 | * nfs_file_direct_write - file direct write operation for NFS files | |
571 | * @iocb: target I/O control block | |
572 | * @buf: user's buffer from which to write data | |
573 | * count: number of bytes to write | |
574 | * pos: byte offset in file where writing starts | |
575 | * | |
576 | * We use this function for direct writes instead of calling | |
577 | * generic_file_aio_write() in order to avoid taking the inode | |
578 | * semaphore and updating the i_size. The NFS server will set | |
579 | * the new i_size and this client must read the updated size | |
580 | * back into its cache. We let the server do generic write | |
581 | * parameter checking and report problems. | |
582 | * | |
583 | * We also avoid an unnecessary invocation of generic_osync_inode(), | |
584 | * as it is fairly meaningless to sync the metadata of an NFS file. | |
585 | * | |
586 | * We eliminate local atime updates, see direct read above. | |
587 | * | |
588 | * We avoid unnecessary page cache invalidations for normal cached | |
589 | * readers of this file. | |
590 | * | |
591 | * Note that O_APPEND is not supported for NFS direct writes, as there | |
592 | * is no atomic O_APPEND write facility in the NFS protocol. | |
593 | */ | |
d4cc948b | 594 | ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos) |
1da177e4 | 595 | { |
ce1a8e67 | 596 | ssize_t retval; |
1da177e4 LT |
597 | struct file *file = iocb->ki_filp; |
598 | struct nfs_open_context *ctx = | |
599 | (struct nfs_open_context *) file->private_data; | |
1da177e4 LT |
600 | struct address_space *mapping = file->f_mapping; |
601 | struct inode *inode = mapping->host; | |
602 | struct iovec iov = { | |
603 | .iov_base = (char __user *)buf, | |
1da177e4 LT |
604 | }; |
605 | ||
ce1a8e67 | 606 | dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n", |
0bbacc40 | 607 | file->f_dentry->d_parent->d_name.name, |
ce1a8e67 CL |
608 | file->f_dentry->d_name.name, |
609 | (unsigned long) count, (long long) pos); | |
1da177e4 | 610 | |
ce1a8e67 | 611 | retval = -EINVAL; |
1da177e4 LT |
612 | if (!is_sync_kiocb(iocb)) |
613 | goto out; | |
ce1a8e67 CL |
614 | |
615 | retval = generic_write_checks(file, &pos, &count, 0); | |
616 | if (retval) | |
1da177e4 | 617 | goto out; |
ce1a8e67 CL |
618 | |
619 | retval = -EINVAL; | |
620 | if ((ssize_t) count < 0) | |
1da177e4 | 621 | goto out; |
1da177e4 LT |
622 | retval = 0; |
623 | if (!count) | |
624 | goto out; | |
ce1a8e67 CL |
625 | iov.iov_len = count, |
626 | ||
627 | retval = -EFAULT; | |
628 | if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len)) | |
629 | goto out; | |
1da177e4 | 630 | |
29884df0 TM |
631 | retval = nfs_sync_mapping(mapping); |
632 | if (retval) | |
633 | goto out; | |
1da177e4 LT |
634 | |
635 | retval = nfs_direct_write(inode, ctx, &iov, pos, 1); | |
636 | if (mapping->nrpages) | |
637 | invalidate_inode_pages2(mapping); | |
638 | if (retval > 0) | |
ce1a8e67 | 639 | iocb->ki_pos = pos + retval; |
1da177e4 LT |
640 | |
641 | out: | |
642 | return retval; | |
643 | } | |
644 | ||
645 | int nfs_init_directcache(void) | |
646 | { | |
647 | nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", | |
648 | sizeof(struct nfs_direct_req), | |
649 | 0, SLAB_RECLAIM_ACCOUNT, | |
650 | NULL, NULL); | |
651 | if (nfs_direct_cachep == NULL) | |
652 | return -ENOMEM; | |
653 | ||
654 | return 0; | |
655 | } | |
656 | ||
657 | void nfs_destroy_directcache(void) | |
658 | { | |
659 | if (kmem_cache_destroy(nfs_direct_cachep)) | |
660 | printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n"); | |
661 | } |