direct.c 18 KB

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