direct.c 19 KB

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