direct.c 19 KB

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