direct.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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_seg(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. /*
  301. * We've already pushed out any non-direct writes so that this read
  302. * will see them when we read from the server.
  303. */
  304. static ssize_t nfs_direct_read(struct inode *inode, struct nfs_open_context *ctx, const struct iovec *iov, loff_t file_offset, unsigned long nr_segs)
  305. {
  306. ssize_t tot_bytes = 0;
  307. unsigned long seg = 0;
  308. while ((seg < nr_segs) && (tot_bytes >= 0)) {
  309. ssize_t result;
  310. int page_count;
  311. struct page **pages;
  312. const struct iovec *vec = &iov[seg++];
  313. unsigned long user_addr = (unsigned long) vec->iov_base;
  314. size_t size = vec->iov_len;
  315. page_count = nfs_get_user_pages(READ, user_addr, size, &pages);
  316. if (page_count < 0) {
  317. nfs_free_user_pages(pages, 0, 0);
  318. if (tot_bytes > 0)
  319. break;
  320. return page_count;
  321. }
  322. result = nfs_direct_read_seg(inode, ctx, user_addr, size,
  323. file_offset, pages, page_count);
  324. if (result <= 0) {
  325. if (tot_bytes > 0)
  326. break;
  327. return result;
  328. }
  329. tot_bytes += result;
  330. file_offset += result;
  331. if (result < size)
  332. break;
  333. }
  334. return tot_bytes;
  335. }
  336. 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)
  337. {
  338. const unsigned int wsize = NFS_SERVER(inode)->wsize;
  339. size_t request;
  340. int curpage, need_commit;
  341. ssize_t result, tot_bytes;
  342. struct nfs_writeverf first_verf;
  343. struct nfs_write_data *wdata;
  344. wdata = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
  345. if (!wdata)
  346. return -ENOMEM;
  347. wdata->inode = inode;
  348. wdata->cred = ctx->cred;
  349. wdata->args.fh = NFS_FH(inode);
  350. wdata->args.context = ctx;
  351. wdata->args.stable = NFS_UNSTABLE;
  352. if (IS_SYNC(inode) || NFS_PROTO(inode)->version == 2 || count <= wsize)
  353. wdata->args.stable = NFS_FILE_SYNC;
  354. wdata->res.fattr = &wdata->fattr;
  355. wdata->res.verf = &wdata->verf;
  356. nfs_begin_data_update(inode);
  357. retry:
  358. need_commit = 0;
  359. tot_bytes = 0;
  360. curpage = 0;
  361. request = count;
  362. wdata->args.pgbase = user_addr & ~PAGE_MASK;
  363. wdata->args.offset = file_offset;
  364. do {
  365. wdata->args.count = request;
  366. if (wdata->args.count > wsize)
  367. wdata->args.count = wsize;
  368. wdata->args.pages = &pages[curpage];
  369. dprintk("NFS: direct write: c=%u o=%Ld ua=%lu, pb=%u, cp=%u\n",
  370. wdata->args.count, (long long) wdata->args.offset,
  371. user_addr + tot_bytes, wdata->args.pgbase, curpage);
  372. lock_kernel();
  373. result = NFS_PROTO(inode)->write(wdata);
  374. unlock_kernel();
  375. if (result <= 0) {
  376. if (tot_bytes > 0)
  377. break;
  378. goto out;
  379. }
  380. if (tot_bytes == 0)
  381. memcpy(&first_verf.verifier, &wdata->verf.verifier,
  382. sizeof(first_verf.verifier));
  383. if (wdata->verf.committed != NFS_FILE_SYNC) {
  384. need_commit = 1;
  385. if (memcmp(&first_verf.verifier, &wdata->verf.verifier,
  386. sizeof(first_verf.verifier)))
  387. goto sync_retry;
  388. }
  389. tot_bytes += result;
  390. /* in case of a short write: stop now, let the app recover */
  391. if (result < wdata->args.count)
  392. break;
  393. wdata->args.offset += result;
  394. wdata->args.pgbase += result;
  395. curpage += wdata->args.pgbase >> PAGE_SHIFT;
  396. wdata->args.pgbase &= ~PAGE_MASK;
  397. request -= result;
  398. } while (request != 0);
  399. /*
  400. * Commit data written so far, even in the event of an error
  401. */
  402. if (need_commit) {
  403. wdata->args.count = tot_bytes;
  404. wdata->args.offset = file_offset;
  405. lock_kernel();
  406. result = NFS_PROTO(inode)->commit(wdata);
  407. unlock_kernel();
  408. if (result < 0 || memcmp(&first_verf.verifier,
  409. &wdata->verf.verifier,
  410. sizeof(first_verf.verifier)) != 0)
  411. goto sync_retry;
  412. }
  413. result = tot_bytes;
  414. out:
  415. nfs_end_data_update(inode);
  416. nfs_writedata_free(wdata);
  417. return result;
  418. sync_retry:
  419. wdata->args.stable = NFS_FILE_SYNC;
  420. goto retry;
  421. }
  422. /*
  423. * Upon return, generic_file_direct_IO invalidates any cached pages
  424. * that non-direct readers might access, so they will pick up these
  425. * writes immediately.
  426. */
  427. 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)
  428. {
  429. ssize_t tot_bytes = 0;
  430. unsigned long seg = 0;
  431. while ((seg < nr_segs) && (tot_bytes >= 0)) {
  432. ssize_t result;
  433. int page_count;
  434. struct page **pages;
  435. const struct iovec *vec = &iov[seg++];
  436. unsigned long user_addr = (unsigned long) vec->iov_base;
  437. size_t size = vec->iov_len;
  438. page_count = nfs_get_user_pages(WRITE, user_addr, size, &pages);
  439. if (page_count < 0) {
  440. nfs_free_user_pages(pages, 0, 0);
  441. if (tot_bytes > 0)
  442. break;
  443. return page_count;
  444. }
  445. nfs_add_stats(inode, NFSIOS_DIRECTWRITTENBYTES, size);
  446. result = nfs_direct_write_seg(inode, ctx, user_addr, size,
  447. file_offset, pages, page_count);
  448. nfs_free_user_pages(pages, page_count, 0);
  449. if (result <= 0) {
  450. if (tot_bytes > 0)
  451. break;
  452. return result;
  453. }
  454. nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
  455. tot_bytes += result;
  456. file_offset += result;
  457. if (result < size)
  458. break;
  459. }
  460. return tot_bytes;
  461. }
  462. /**
  463. * nfs_file_direct_read - file direct read operation for NFS files
  464. * @iocb: target I/O control block
  465. * @buf: user's buffer into which to read data
  466. * count: number of bytes to read
  467. * pos: byte offset in file where reading starts
  468. *
  469. * We use this function for direct reads instead of calling
  470. * generic_file_aio_read() in order to avoid gfar's check to see if
  471. * the request starts before the end of the file. For that check
  472. * to work, we must generate a GETATTR before each direct read, and
  473. * even then there is a window between the GETATTR and the subsequent
  474. * READ where the file size could change. So our preference is simply
  475. * to do all reads the application wants, and the server will take
  476. * care of managing the end of file boundary.
  477. *
  478. * This function also eliminates unnecessarily updating the file's
  479. * atime locally, as the NFS server sets the file's atime, and this
  480. * client must read the updated atime from the server back into its
  481. * cache.
  482. */
  483. ssize_t nfs_file_direct_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
  484. {
  485. ssize_t retval = -EINVAL;
  486. loff_t *ppos = &iocb->ki_pos;
  487. struct file *file = iocb->ki_filp;
  488. struct nfs_open_context *ctx =
  489. (struct nfs_open_context *) file->private_data;
  490. struct address_space *mapping = file->f_mapping;
  491. struct inode *inode = mapping->host;
  492. struct iovec iov = {
  493. .iov_base = buf,
  494. .iov_len = count,
  495. };
  496. dprintk("nfs: direct read(%s/%s, %lu@%Ld)\n",
  497. file->f_dentry->d_parent->d_name.name,
  498. file->f_dentry->d_name.name,
  499. (unsigned long) count, (long long) pos);
  500. if (!is_sync_kiocb(iocb))
  501. goto out;
  502. if (count < 0)
  503. goto out;
  504. retval = -EFAULT;
  505. if (!access_ok(VERIFY_WRITE, iov.iov_base, iov.iov_len))
  506. goto out;
  507. retval = 0;
  508. if (!count)
  509. goto out;
  510. retval = nfs_sync_mapping(mapping);
  511. if (retval)
  512. goto out;
  513. retval = nfs_direct_read(inode, ctx, &iov, pos, 1);
  514. if (retval > 0)
  515. *ppos = pos + retval;
  516. out:
  517. return retval;
  518. }
  519. /**
  520. * nfs_file_direct_write - file direct write operation for NFS files
  521. * @iocb: target I/O control block
  522. * @buf: user's buffer from which to write data
  523. * count: number of bytes to write
  524. * pos: byte offset in file where writing starts
  525. *
  526. * We use this function for direct writes instead of calling
  527. * generic_file_aio_write() in order to avoid taking the inode
  528. * semaphore and updating the i_size. The NFS server will set
  529. * the new i_size and this client must read the updated size
  530. * back into its cache. We let the server do generic write
  531. * parameter checking and report problems.
  532. *
  533. * We also avoid an unnecessary invocation of generic_osync_inode(),
  534. * as it is fairly meaningless to sync the metadata of an NFS file.
  535. *
  536. * We eliminate local atime updates, see direct read above.
  537. *
  538. * We avoid unnecessary page cache invalidations for normal cached
  539. * readers of this file.
  540. *
  541. * Note that O_APPEND is not supported for NFS direct writes, as there
  542. * is no atomic O_APPEND write facility in the NFS protocol.
  543. */
  544. ssize_t nfs_file_direct_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos)
  545. {
  546. ssize_t retval;
  547. struct file *file = iocb->ki_filp;
  548. struct nfs_open_context *ctx =
  549. (struct nfs_open_context *) file->private_data;
  550. struct address_space *mapping = file->f_mapping;
  551. struct inode *inode = mapping->host;
  552. struct iovec iov = {
  553. .iov_base = (char __user *)buf,
  554. };
  555. dfprintk(VFS, "nfs: direct write(%s/%s, %lu@%Ld)\n",
  556. file->f_dentry->d_parent->d_name.name,
  557. file->f_dentry->d_name.name,
  558. (unsigned long) count, (long long) pos);
  559. retval = -EINVAL;
  560. if (!is_sync_kiocb(iocb))
  561. goto out;
  562. retval = generic_write_checks(file, &pos, &count, 0);
  563. if (retval)
  564. goto out;
  565. retval = -EINVAL;
  566. if ((ssize_t) count < 0)
  567. goto out;
  568. retval = 0;
  569. if (!count)
  570. goto out;
  571. iov.iov_len = count,
  572. retval = -EFAULT;
  573. if (!access_ok(VERIFY_READ, iov.iov_base, iov.iov_len))
  574. goto out;
  575. retval = nfs_sync_mapping(mapping);
  576. if (retval)
  577. goto out;
  578. retval = nfs_direct_write(inode, ctx, &iov, pos, 1);
  579. if (mapping->nrpages)
  580. invalidate_inode_pages2(mapping);
  581. if (retval > 0)
  582. iocb->ki_pos = pos + retval;
  583. out:
  584. return retval;
  585. }
  586. int nfs_init_directcache(void)
  587. {
  588. nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
  589. sizeof(struct nfs_direct_req),
  590. 0, SLAB_RECLAIM_ACCOUNT,
  591. NULL, NULL);
  592. if (nfs_direct_cachep == NULL)
  593. return -ENOMEM;
  594. return 0;
  595. }
  596. void nfs_destroy_directcache(void)
  597. {
  598. if (kmem_cache_destroy(nfs_direct_cachep))
  599. printk(KERN_INFO "nfs_direct_cache: not all structures were freed\n");
  600. }