direct.c 19 KB

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