direct.c 22 KB

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