direct.c 22 KB

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