direct.c 22 KB

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