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 */ | |
71 | wait_queue_head_t wait; /* wait for i/o completion */ | |
91d5b470 | 72 | struct inode * inode; /* target file of I/O */ |
1da177e4 LT |
73 | struct page ** pages; /* pages in our buffer */ |
74 | unsigned int npages; /* count of pages */ | |
75 | atomic_t complete, /* i/os we're waiting for */ | |
76 | count, /* bytes actually processed */ | |
77 | error; /* any reported error */ | |
78 | }; | |
79 | ||
80 | ||
81 | /** | |
82 | * nfs_get_user_pages - find and set up pages underlying user's buffer | |
83 | * rw: direction (read or write) | |
84 | * user_addr: starting address of this segment of user's buffer | |
85 | * count: size of this segment | |
86 | * @pages: returned array of page struct pointers underlying user's buffer | |
87 | */ | |
88 | static inline int | |
89 | nfs_get_user_pages(int rw, unsigned long user_addr, size_t size, | |
90 | struct page ***pages) | |
91 | { | |
92 | int result = -ENOMEM; | |
93 | unsigned long page_count; | |
94 | size_t array_size; | |
95 | ||
96 | /* set an arbitrary limit to prevent type overflow */ | |
97 | /* XXX: this can probably be as large as INT_MAX */ | |
98 | if (size > MAX_DIRECTIO_SIZE) { | |
99 | *pages = NULL; | |
100 | return -EFBIG; | |
101 | } | |
102 | ||
103 | page_count = (user_addr + size + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
104 | page_count -= user_addr >> PAGE_SHIFT; | |
105 | ||
106 | array_size = (page_count * sizeof(struct page *)); | |
107 | *pages = kmalloc(array_size, GFP_KERNEL); | |
108 | if (*pages) { | |
109 | down_read(¤t->mm->mmap_sem); | |
110 | result = get_user_pages(current, current->mm, user_addr, | |
111 | page_count, (rw == READ), 0, | |
112 | *pages, NULL); | |
113 | up_read(¤t->mm->mmap_sem); | |
143f412e TM |
114 | /* |
115 | * If we got fewer pages than expected from get_user_pages(), | |
116 | * the user buffer runs off the end of a mapping; return EFAULT. | |
117 | */ | |
118 | if (result >= 0 && result < page_count) { | |
119 | nfs_free_user_pages(*pages, result, 0); | |
120 | *pages = NULL; | |
121 | result = -EFAULT; | |
122 | } | |
1da177e4 LT |
123 | } |
124 | return result; | |
125 | } | |
126 | ||
127 | /** | |
128 | * nfs_free_user_pages - tear down page struct array | |
129 | * @pages: array of page struct pointers underlying target buffer | |
130 | * @npages: number of pages in the array | |
131 | * @do_dirty: dirty the pages as we release them | |
132 | */ | |
133 | static void | |
134 | nfs_free_user_pages(struct page **pages, int npages, int do_dirty) | |
135 | { | |
136 | int i; | |
137 | for (i = 0; i < npages; i++) { | |
566dd606 TM |
138 | struct page *page = pages[i]; |
139 | if (do_dirty && !PageCompound(page)) | |
140 | set_page_dirty_lock(page); | |
141 | page_cache_release(page); | |
1da177e4 LT |
142 | } |
143 | kfree(pages); | |
144 | } | |
145 | ||
146 | /** | |
147 | * nfs_direct_req_release - release nfs_direct_req structure for direct read | |
148 | * @kref: kref object embedded in an nfs_direct_req structure | |
149 | * | |
150 | */ | |
151 | static void nfs_direct_req_release(struct kref *kref) | |
152 | { | |
153 | struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref); | |
154 | kmem_cache_free(nfs_direct_cachep, dreq); | |
155 | } | |
156 | ||
157 | /** | |
158 | * nfs_direct_read_alloc - allocate nfs_read_data structures for direct read | |
159 | * @count: count of bytes for the read request | |
160 | * @rsize: local rsize setting | |
161 | * | |
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 | */ | |
166 | static struct nfs_direct_req *nfs_direct_read_alloc(size_t nbytes, unsigned int rsize) | |
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 | ||
212 | /** | |
213 | * nfs_direct_read_result - handle a read reply for a direct read request | |
214 | * @data: address of NFS READ operation control block | |
215 | * @status: status of this NFS READ operation | |
216 | * | |
217 | * We must hold a reference to all the pages in this direct read request | |
218 | * until the RPCs complete. This could be long *after* we are woken up in | |
219 | * nfs_direct_read_wait (for instance, if someone hits ^C on a slow server). | |
220 | */ | |
ec06c096 | 221 | static void nfs_direct_read_result(struct rpc_task *task, void *calldata) |
1da177e4 | 222 | { |
ec06c096 | 223 | struct nfs_read_data *data = calldata; |
1da177e4 LT |
224 | struct nfs_direct_req *dreq = (struct nfs_direct_req *) data->req; |
225 | ||
ec06c096 TM |
226 | if (nfs_readpage_result(task, data) != 0) |
227 | return; | |
228 | if (likely(task->tk_status >= 0)) | |
1da177e4 LT |
229 | atomic_add(data->res.count, &dreq->count); |
230 | else | |
ec06c096 | 231 | atomic_set(&dreq->error, task->tk_status); |
1da177e4 LT |
232 | |
233 | if (unlikely(atomic_dec_and_test(&dreq->complete))) { | |
234 | nfs_free_user_pages(dreq->pages, dreq->npages, 1); | |
235 | wake_up(&dreq->wait); | |
236 | kref_put(&dreq->kref, nfs_direct_req_release); | |
237 | } | |
238 | } | |
239 | ||
ec06c096 TM |
240 | static const struct rpc_call_ops nfs_read_direct_ops = { |
241 | .rpc_call_done = nfs_direct_read_result, | |
242 | .rpc_release = nfs_readdata_release, | |
243 | }; | |
244 | ||
1da177e4 LT |
245 | /** |
246 | * nfs_direct_read_schedule - dispatch NFS READ operations for a direct read | |
247 | * @dreq: address of nfs_direct_req struct for this request | |
248 | * @inode: target inode | |
249 | * @ctx: target file open context | |
250 | * @user_addr: starting address of this segment of user's buffer | |
251 | * @count: size of this segment | |
252 | * @file_offset: offset in file to begin the operation | |
253 | * | |
254 | * For each nfs_read_data struct that was allocated on the list, dispatch | |
255 | * an NFS READ operation | |
256 | */ | |
257 | static void nfs_direct_read_schedule(struct nfs_direct_req *dreq, | |
258 | struct inode *inode, struct nfs_open_context *ctx, | |
259 | unsigned long user_addr, size_t count, loff_t file_offset) | |
260 | { | |
261 | struct list_head *list = &dreq->list; | |
262 | struct page **pages = dreq->pages; | |
263 | unsigned int curpage, pgbase; | |
264 | unsigned int rsize = NFS_SERVER(inode)->rsize; | |
265 | ||
266 | curpage = 0; | |
267 | pgbase = user_addr & ~PAGE_MASK; | |
268 | do { | |
269 | struct nfs_read_data *data; | |
270 | unsigned int bytes; | |
271 | ||
272 | bytes = rsize; | |
273 | if (count < rsize) | |
274 | bytes = count; | |
275 | ||
276 | data = list_entry(list->next, struct nfs_read_data, pages); | |
277 | list_del_init(&data->pages); | |
278 | ||
279 | data->inode = inode; | |
280 | data->cred = ctx->cred; | |
281 | data->args.fh = NFS_FH(inode); | |
282 | data->args.context = ctx; | |
283 | data->args.offset = file_offset; | |
284 | data->args.pgbase = pgbase; | |
285 | data->args.pages = &pages[curpage]; | |
286 | data->args.count = bytes; | |
287 | data->res.fattr = &data->fattr; | |
288 | data->res.eof = 0; | |
289 | data->res.count = bytes; | |
290 | ||
ec06c096 TM |
291 | rpc_init_task(&data->task, NFS_CLIENT(inode), RPC_TASK_ASYNC, |
292 | &nfs_read_direct_ops, data); | |
1da177e4 LT |
293 | NFS_PROTO(inode)->read_setup(data); |
294 | ||
295 | data->task.tk_cookie = (unsigned long) inode; | |
1da177e4 LT |
296 | |
297 | lock_kernel(); | |
298 | rpc_execute(&data->task); | |
299 | unlock_kernel(); | |
300 | ||
301 | dfprintk(VFS, "NFS: %4d initiated direct read call (req %s/%Ld, %u bytes @ offset %Lu)\n", | |
302 | data->task.tk_pid, | |
303 | inode->i_sb->s_id, | |
304 | (long long)NFS_FILEID(inode), | |
305 | bytes, | |
306 | (unsigned long long)data->args.offset); | |
307 | ||
308 | file_offset += bytes; | |
309 | pgbase += bytes; | |
310 | curpage += pgbase >> PAGE_SHIFT; | |
311 | pgbase &= ~PAGE_MASK; | |
312 | ||
313 | count -= bytes; | |
314 | } while (count != 0); | |
315 | } | |
316 | ||
317 | /** | |
318 | * nfs_direct_read_wait - wait for I/O completion for direct reads | |
319 | * @dreq: request on which we are to wait | |
320 | * @intr: whether or not this wait can be interrupted | |
321 | * | |
322 | * Collects and returns the final error value/byte-count. | |
323 | */ | |
324 | static ssize_t nfs_direct_read_wait(struct nfs_direct_req *dreq, int intr) | |
325 | { | |
326 | int result = 0; | |
327 | ||
328 | if (intr) { | |
329 | result = wait_event_interruptible(dreq->wait, | |
330 | (atomic_read(&dreq->complete) == 0)); | |
331 | } else { | |
332 | wait_event(dreq->wait, (atomic_read(&dreq->complete) == 0)); | |
333 | } | |
334 | ||
335 | if (!result) | |
336 | result = atomic_read(&dreq->error); | |
337 | if (!result) | |
338 | result = atomic_read(&dreq->count); | |
339 | ||
340 | kref_put(&dreq->kref, nfs_direct_req_release); | |
341 | return (ssize_t) result; | |
342 | } | |
343 | ||
344 | /** | |
345 | * nfs_direct_read_seg - Read in one iov segment. Generate separate | |
346 | * read RPCs for each "rsize" bytes. | |
347 | * @inode: target inode | |
348 | * @ctx: target file open context | |
349 | * @user_addr: starting address of this segment of user's buffer | |
350 | * @count: size of this segment | |
351 | * @file_offset: offset in file to begin the operation | |
352 | * @pages: array of addresses of page structs defining user's buffer | |
353 | * @nr_pages: number of pages in the array | |
354 | * | |
355 | */ | |
356 | static ssize_t nfs_direct_read_seg(struct inode *inode, | |
357 | struct nfs_open_context *ctx, unsigned long user_addr, | |
358 | size_t count, loff_t file_offset, struct page **pages, | |
359 | unsigned int nr_pages) | |
360 | { | |
361 | ssize_t result; | |
362 | sigset_t oldset; | |
363 | struct rpc_clnt *clnt = NFS_CLIENT(inode); | |
364 | struct nfs_direct_req *dreq; | |
365 | ||
366 | dreq = nfs_direct_read_alloc(count, NFS_SERVER(inode)->rsize); | |
367 | if (!dreq) | |
368 | return -ENOMEM; | |
369 | ||
370 | dreq->pages = pages; | |
371 | dreq->npages = nr_pages; | |
91d5b470 | 372 | dreq->inode = inode; |
1da177e4 | 373 | |
91d5b470 | 374 | nfs_add_stats(inode, NFSIOS_DIRECTREADBYTES, count); |
1da177e4 LT |
375 | rpc_clnt_sigmask(clnt, &oldset); |
376 | nfs_direct_read_schedule(dreq, inode, ctx, user_addr, count, | |
377 | file_offset); | |
378 | result = nfs_direct_read_wait(dreq, clnt->cl_intr); | |
379 | rpc_clnt_sigunmask(clnt, &oldset); | |
380 | ||
381 | return result; | |
382 | } | |
383 | ||
384 | /** | |
385 | * nfs_direct_read - For each iov segment, map the user's buffer | |
386 | * then generate read RPCs. | |
387 | * @inode: target inode | |
388 | * @ctx: target file open context | |
389 | * @iov: array of vectors that define I/O buffer | |
390 | * file_offset: offset in file to begin the operation | |
391 | * nr_segs: size of iovec array | |
392 | * | |
393 | * We've already pushed out any non-direct writes so that this read | |
394 | * will see them when we read from the server. | |
395 | */ | |
396 | static ssize_t | |
397 | nfs_direct_read(struct inode *inode, struct nfs_open_context *ctx, | |
398 | const struct iovec *iov, loff_t file_offset, | |
399 | unsigned long nr_segs) | |
400 | { | |
401 | ssize_t tot_bytes = 0; | |
402 | unsigned long seg = 0; | |
403 | ||
404 | while ((seg < nr_segs) && (tot_bytes >= 0)) { | |
405 | ssize_t result; | |
406 | int page_count; | |
407 | struct page **pages; | |
408 | const struct iovec *vec = &iov[seg++]; | |
409 | unsigned long user_addr = (unsigned long) vec->iov_base; | |
410 | size_t size = vec->iov_len; | |
411 | ||
412 | page_count = nfs_get_user_pages(READ, user_addr, size, &pages); | |
413 | if (page_count < 0) { | |
414 | nfs_free_user_pages(pages, 0, 0); | |
415 | if (tot_bytes > 0) | |
416 | break; | |
417 | return page_count; | |
418 | } | |
419 | ||
420 | result = nfs_direct_read_seg(inode, ctx, user_addr, size, | |
421 | file_offset, pages, page_count); | |
422 | ||
423 | if (result <= 0) { | |
424 | if (tot_bytes > 0) | |
425 | break; | |
426 | return result; | |
427 | } | |
428 | tot_bytes += result; | |
429 | file_offset += result; | |
430 | if (result < size) | |
431 | break; | |
432 | } | |
433 | ||
434 | return tot_bytes; | |
435 | } | |
436 | ||
437 | /** | |
438 | * nfs_direct_write_seg - Write out one iov segment. Generate separate | |
439 | * write RPCs for each "wsize" bytes, then commit. | |
440 | * @inode: target inode | |
441 | * @ctx: target file open context | |
442 | * user_addr: starting address of this segment of user's buffer | |
443 | * count: size of this segment | |
444 | * file_offset: offset in file to begin the operation | |
445 | * @pages: array of addresses of page structs defining user's buffer | |
446 | * nr_pages: size of pages array | |
447 | */ | |
448 | static ssize_t nfs_direct_write_seg(struct inode *inode, | |
449 | struct nfs_open_context *ctx, unsigned long user_addr, | |
450 | size_t count, loff_t file_offset, struct page **pages, | |
451 | int nr_pages) | |
452 | { | |
453 | const unsigned int wsize = NFS_SERVER(inode)->wsize; | |
454 | size_t request; | |
455 | int curpage, need_commit; | |
456 | ssize_t result, tot_bytes; | |
457 | struct nfs_writeverf first_verf; | |
458 | struct nfs_write_data *wdata; | |
459 | ||
40859d7e | 460 | wdata = nfs_writedata_alloc(NFS_SERVER(inode)->wpages); |
1da177e4 LT |
461 | if (!wdata) |
462 | return -ENOMEM; | |
463 | ||
464 | wdata->inode = inode; | |
465 | wdata->cred = ctx->cred; | |
466 | wdata->args.fh = NFS_FH(inode); | |
467 | wdata->args.context = ctx; | |
468 | wdata->args.stable = NFS_UNSTABLE; | |
469 | if (IS_SYNC(inode) || NFS_PROTO(inode)->version == 2 || count <= wsize) | |
470 | wdata->args.stable = NFS_FILE_SYNC; | |
471 | wdata->res.fattr = &wdata->fattr; | |
472 | wdata->res.verf = &wdata->verf; | |
473 | ||
474 | nfs_begin_data_update(inode); | |
475 | retry: | |
476 | need_commit = 0; | |
477 | tot_bytes = 0; | |
478 | curpage = 0; | |
479 | request = count; | |
480 | wdata->args.pgbase = user_addr & ~PAGE_MASK; | |
481 | wdata->args.offset = file_offset; | |
482 | do { | |
483 | wdata->args.count = request; | |
484 | if (wdata->args.count > wsize) | |
485 | wdata->args.count = wsize; | |
486 | wdata->args.pages = &pages[curpage]; | |
487 | ||
488 | dprintk("NFS: direct write: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n", | |
489 | wdata->args.count, (long long) wdata->args.offset, | |
490 | user_addr + tot_bytes, wdata->args.pgbase, curpage); | |
491 | ||
492 | lock_kernel(); | |
493 | result = NFS_PROTO(inode)->write(wdata); | |
494 | unlock_kernel(); | |
495 | ||
496 | if (result <= 0) { | |
497 | if (tot_bytes > 0) | |
498 | break; | |
499 | goto out; | |
500 | } | |
501 | ||
502 | if (tot_bytes == 0) | |
503 | memcpy(&first_verf.verifier, &wdata->verf.verifier, | |
504 | sizeof(first_verf.verifier)); | |
505 | if (wdata->verf.committed != NFS_FILE_SYNC) { | |
506 | need_commit = 1; | |
507 | if (memcmp(&first_verf.verifier, &wdata->verf.verifier, | |
19352456 | 508 | sizeof(first_verf.verifier))) |
1da177e4 LT |
509 | goto sync_retry; |
510 | } | |
511 | ||
512 | tot_bytes += result; | |
513 | ||
514 | /* in case of a short write: stop now, let the app recover */ | |
515 | if (result < wdata->args.count) | |
516 | break; | |
517 | ||
518 | wdata->args.offset += result; | |
519 | wdata->args.pgbase += result; | |
520 | curpage += wdata->args.pgbase >> PAGE_SHIFT; | |
521 | wdata->args.pgbase &= ~PAGE_MASK; | |
522 | request -= result; | |
523 | } while (request != 0); | |
524 | ||
525 | /* | |
526 | * Commit data written so far, even in the event of an error | |
527 | */ | |
528 | if (need_commit) { | |
529 | wdata->args.count = tot_bytes; | |
530 | wdata->args.offset = file_offset; | |
531 | ||
532 | lock_kernel(); | |
533 | result = NFS_PROTO(inode)->commit(wdata); | |
534 | unlock_kernel(); | |
535 | ||
536 | if (result < 0 || memcmp(&first_verf.verifier, | |
537 | &wdata->verf.verifier, | |
538 | sizeof(first_verf.verifier)) != 0) | |
539 | goto sync_retry; | |
540 | } | |
541 | result = tot_bytes; | |
542 | ||
543 | out: | |
951a143b | 544 | nfs_end_data_update(inode); |
1da177e4 LT |
545 | nfs_writedata_free(wdata); |
546 | return result; | |
547 | ||
548 | sync_retry: | |
549 | wdata->args.stable = NFS_FILE_SYNC; | |
550 | goto retry; | |
551 | } | |
552 | ||
553 | /** | |
554 | * nfs_direct_write - For each iov segment, map the user's buffer | |
555 | * then generate write and commit RPCs. | |
556 | * @inode: target inode | |
557 | * @ctx: target file open context | |
558 | * @iov: array of vectors that define I/O buffer | |
559 | * file_offset: offset in file to begin the operation | |
560 | * nr_segs: size of iovec array | |
561 | * | |
562 | * Upon return, generic_file_direct_IO invalidates any cached pages | |
563 | * that non-direct readers might access, so they will pick up these | |
564 | * writes immediately. | |
565 | */ | |
566 | static ssize_t nfs_direct_write(struct inode *inode, | |
567 | struct nfs_open_context *ctx, const struct iovec *iov, | |
568 | loff_t file_offset, unsigned long nr_segs) | |
569 | { | |
570 | ssize_t tot_bytes = 0; | |
571 | unsigned long seg = 0; | |
572 | ||
573 | while ((seg < nr_segs) && (tot_bytes >= 0)) { | |
574 | ssize_t result; | |
575 | int page_count; | |
576 | struct page **pages; | |
577 | const struct iovec *vec = &iov[seg++]; | |
578 | unsigned long user_addr = (unsigned long) vec->iov_base; | |
579 | size_t size = vec->iov_len; | |
580 | ||
581 | page_count = nfs_get_user_pages(WRITE, user_addr, size, &pages); | |
582 | if (page_count < 0) { | |
583 | nfs_free_user_pages(pages, 0, 0); | |
584 | if (tot_bytes > 0) | |
585 | break; | |
586 | return page_count; | |
587 | } | |
588 | ||
91d5b470 | 589 | nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, size); |
1da177e4 LT |
590 | result = nfs_direct_write_seg(inode, ctx, user_addr, size, |
591 | file_offset, pages, page_count); | |
592 | nfs_free_user_pages(pages, page_count, 0); | |
593 | ||
594 | if (result <= 0) { | |
595 | if (tot_bytes > 0) | |
596 | break; | |
597 | return result; | |
598 | } | |
91d5b470 | 599 | nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result); |
1da177e4 LT |
600 | tot_bytes += result; |
601 | file_offset += result; | |
602 | if (result < size) | |
603 | break; | |
604 | } | |
605 | return tot_bytes; | |
606 | } | |
607 | ||
608 | /** | |
609 | * nfs_direct_IO - NFS address space operation for direct I/O | |
610 | * rw: direction (read or write) | |
611 | * @iocb: target I/O control block | |
612 | * @iov: array of vectors that define I/O buffer | |
613 | * file_offset: offset in file to begin the operation | |
614 | * nr_segs: size of iovec array | |
615 | * | |
616 | */ | |
617 | ssize_t | |
618 | nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, | |
619 | loff_t file_offset, unsigned long nr_segs) | |
620 | { | |
621 | ssize_t result = -EINVAL; | |
622 | struct file *file = iocb->ki_filp; | |
623 | struct nfs_open_context *ctx; | |
624 | struct dentry *dentry = file->f_dentry; | |
625 | struct inode *inode = dentry->d_inode; | |
626 | ||
627 | /* | |
628 | * No support for async yet | |
629 | */ | |
630 | if (!is_sync_kiocb(iocb)) | |
631 | return result; | |
632 | ||
633 | ctx = (struct nfs_open_context *)file->private_data; | |
634 | switch (rw) { | |
635 | case READ: | |
636 | dprintk("NFS: direct_IO(read) (%s) off/no(%Lu/%lu)\n", | |
637 | dentry->d_name.name, file_offset, nr_segs); | |
638 | ||
639 | result = nfs_direct_read(inode, ctx, iov, | |
640 | file_offset, nr_segs); | |
641 | break; | |
642 | case WRITE: | |
643 | dprintk("NFS: direct_IO(write) (%s) off/no(%Lu/%lu)\n", | |
644 | dentry->d_name.name, file_offset, nr_segs); | |
645 | ||
646 | result = nfs_direct_write(inode, ctx, iov, | |
647 | file_offset, nr_segs); | |
648 | break; | |
649 | default: | |
650 | break; | |
651 | } | |
652 | return result; | |
653 | } | |
654 | ||
655 | /** | |
656 | * nfs_file_direct_read - file direct read operation for NFS files | |
657 | * @iocb: target I/O control block | |
658 | * @buf: user's buffer into which to read data | |
659 | * count: number of bytes to read | |
660 | * pos: byte offset in file where reading starts | |
661 | * | |
662 | * We use this function for direct reads instead of calling | |
663 | * generic_file_aio_read() in order to avoid gfar's check to see if | |
664 | * the request starts before the end of the file. For that check | |
665 | * to work, we must generate a GETATTR before each direct read, and | |
666 | * even then there is a window between the GETATTR and the subsequent | |
667 | * READ where the file size could change. So our preference is simply | |
668 | * to do all reads the application wants, and the server will take | |
669 | * care of managing the end of file boundary. | |
670 | * | |
671 | * This function also eliminates unnecessarily updating the file's | |
672 | * atime locally, as the NFS server sets the file's atime, and this | |
673 | * client must read the updated atime from the server back into its | |
674 | * cache. | |
675 | */ | |
676 | ssize_t | |
677 | nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos) | |
678 | { | |
679 | ssize_t retval = -EINVAL; | |
680 | loff_t *ppos = &iocb->ki_pos; | |
681 | struct file *file = iocb->ki_filp; | |
682 | struct nfs_open_context *ctx = | |
683 | (struct nfs_open_context *) file->private_data; | |
1da177e4 LT |
684 | struct address_space *mapping = file->f_mapping; |
685 | struct inode *inode = mapping->host; | |
686 | struct iovec iov = { | |
687 | .iov_base = buf, | |
688 | .iov_len = count, | |
689 | }; | |
690 | ||
ce1a8e67 | 691 | dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n", |
0bbacc40 CL |
692 | file->f_dentry->d_parent->d_name.name, |
693 | file->f_dentry->d_name.name, | |
ce1a8e67 | 694 | (unsigned long) count, (long long) pos); |
1da177e4 LT |
695 | |
696 | if (!is_sync_kiocb(iocb)) | |
697 | goto out; | |
698 | if (count < 0) | |
699 | goto out; | |
700 | retval = -EFAULT; | |
701 | if (!access_ok(VERIFY_WRITE, iov.iov_base, iov.iov_len)) | |
702 | goto out; | |
703 | retval = 0; | |
704 | if (!count) | |
705 | goto out; | |
706 | ||
29884df0 TM |
707 | retval = nfs_sync_mapping(mapping); |
708 | if (retval) | |
709 | goto out; | |
1da177e4 LT |
710 | |
711 | retval = nfs_direct_read(inode, ctx, &iov, pos, 1); | |
712 | if (retval > 0) | |
713 | *ppos = pos + retval; | |
714 | ||
715 | out: | |
716 | return retval; | |
717 | } | |
718 | ||
719 | /** | |
720 | * nfs_file_direct_write - file direct write operation for NFS files | |
721 | * @iocb: target I/O control block | |
722 | * @buf: user's buffer from which to write data | |
723 | * count: number of bytes to write | |
724 | * pos: byte offset in file where writing starts | |
725 | * | |
726 | * We use this function for direct writes instead of calling | |
727 | * generic_file_aio_write() in order to avoid taking the inode | |
728 | * semaphore and updating the i_size. The NFS server will set | |
729 | * the new i_size and this client must read the updated size | |
730 | * back into its cache. We let the server do generic write | |
731 | * parameter checking and report problems. | |
732 | * | |
733 | * We also avoid an unnecessary invocation of generic_osync_inode(), | |
734 | * as it is fairly meaningless to sync the metadata of an NFS file. | |
735 | * | |
736 | * We eliminate local atime updates, see direct read above. | |
737 | * | |
738 | * We avoid unnecessary page cache invalidations for normal cached | |
739 | * readers of this file. | |
740 | * | |
741 | * Note that O_APPEND is not supported for NFS direct writes, as there | |
742 | * is no atomic O_APPEND write facility in the NFS protocol. | |
743 | */ | |
744 | ssize_t | |
745 | nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos) | |
746 | { | |
ce1a8e67 | 747 | ssize_t retval; |
1da177e4 LT |
748 | struct file *file = iocb->ki_filp; |
749 | struct nfs_open_context *ctx = | |
750 | (struct nfs_open_context *) file->private_data; | |
1da177e4 LT |
751 | struct address_space *mapping = file->f_mapping; |
752 | struct inode *inode = mapping->host; | |
753 | struct iovec iov = { | |
754 | .iov_base = (char __user *)buf, | |
1da177e4 LT |
755 | }; |
756 | ||
ce1a8e67 | 757 | dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n", |
0bbacc40 | 758 | file->f_dentry->d_parent->d_name.name, |
ce1a8e67 CL |
759 | file->f_dentry->d_name.name, |
760 | (unsigned long) count, (long long) pos); | |
1da177e4 | 761 | |
ce1a8e67 | 762 | retval = -EINVAL; |
1da177e4 LT |
763 | if (!is_sync_kiocb(iocb)) |
764 | goto out; | |
ce1a8e67 CL |
765 | |
766 | retval = generic_write_checks(file, &pos, &count, 0); | |
767 | if (retval) | |
1da177e4 | 768 | goto out; |
ce1a8e67 CL |
769 | |
770 | retval = -EINVAL; | |
771 | if ((ssize_t) count < 0) | |
1da177e4 | 772 | goto out; |
1da177e4 LT |
773 | retval = 0; |
774 | if (!count) | |
775 | goto out; | |
ce1a8e67 CL |
776 | iov.iov_len = count, |
777 | ||
778 | retval = -EFAULT; | |
779 | if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len)) | |
780 | goto out; | |
1da177e4 | 781 | |
29884df0 TM |
782 | retval = nfs_sync_mapping(mapping); |
783 | if (retval) | |
784 | goto out; | |
1da177e4 LT |
785 | |
786 | retval = nfs_direct_write(inode, ctx, &iov, pos, 1); | |
787 | if (mapping->nrpages) | |
788 | invalidate_inode_pages2(mapping); | |
789 | if (retval > 0) | |
ce1a8e67 | 790 | iocb->ki_pos = pos + retval; |
1da177e4 LT |
791 | |
792 | out: | |
793 | return retval; | |
794 | } | |
795 | ||
796 | int nfs_init_directcache(void) | |
797 | { | |
798 | nfs_direct_cachep = kmem_cache_create("nfs_direct_cache", | |
799 | sizeof(struct nfs_direct_req), | |
800 | 0, SLAB_RECLAIM_ACCOUNT, | |
801 | NULL, NULL); | |
802 | if (nfs_direct_cachep == NULL) | |
803 | return -ENOMEM; | |
804 | ||
805 | return 0; | |
806 | } | |
807 | ||
808 | void nfs_destroy_directcache(void) | |
809 | { | |
810 | if (kmem_cache_destroy(nfs_direct_cachep)) | |
811 | printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n"); | |
812 | } |