direct.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. * 04 May 2005 support O_DIRECT with aio --cel
  38. *
  39. */
  40. #include <linux/errno.h>
  41. #include <linux/sched.h>
  42. #include <linux/kernel.h>
  43. #include <linux/file.h>
  44. #include <linux/pagemap.h>
  45. #include <linux/kref.h>
  46. #include <linux/slab.h>
  47. #include <linux/task_io_accounting_ops.h>
  48. #include <linux/nfs_fs.h>
  49. #include <linux/nfs_page.h>
  50. #include <linux/sunrpc/clnt.h>
  51. #include <asm/uaccess.h>
  52. #include <linux/atomic.h>
  53. #include "internal.h"
  54. #include "iostat.h"
  55. #define NFSDBG_FACILITY NFSDBG_VFS
  56. static struct kmem_cache *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. /* I/O parameters */
  63. struct nfs_open_context *ctx; /* file open context info */
  64. struct nfs_lock_context *l_ctx; /* Lock context info */
  65. struct kiocb * iocb; /* controlling i/o request */
  66. struct inode * inode; /* target file of i/o */
  67. /* completion state */
  68. atomic_t io_count; /* i/os we're waiting for */
  69. spinlock_t lock; /* protect completion state */
  70. ssize_t count, /* bytes actually processed */
  71. error; /* any reported error */
  72. struct completion completion; /* wait for i/o completion */
  73. /* commit state */
  74. struct list_head rewrite_list; /* saved nfs_write_data structs */
  75. struct nfs_commit_data *commit_data; /* special write_data for commits */
  76. int flags;
  77. #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
  78. #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
  79. struct nfs_writeverf verf; /* unstable write verifier */
  80. };
  81. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
  82. static const struct rpc_call_ops nfs_write_direct_ops;
  83. static inline void get_dreq(struct nfs_direct_req *dreq)
  84. {
  85. atomic_inc(&dreq->io_count);
  86. }
  87. static inline int put_dreq(struct nfs_direct_req *dreq)
  88. {
  89. return atomic_dec_and_test(&dreq->io_count);
  90. }
  91. /**
  92. * nfs_direct_IO - NFS address space operation for direct I/O
  93. * @rw: direction (read or write)
  94. * @iocb: target I/O control block
  95. * @iov: array of vectors that define I/O buffer
  96. * @pos: offset in file to begin the operation
  97. * @nr_segs: size of iovec array
  98. *
  99. * The presence of this routine in the address space ops vector means
  100. * the NFS client supports direct I/O. However, we shunt off direct
  101. * read and write requests before the VFS gets them, so this method
  102. * should never be called.
  103. */
  104. ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
  105. {
  106. dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
  107. iocb->ki_filp->f_path.dentry->d_name.name,
  108. (long long) pos, nr_segs);
  109. return -EINVAL;
  110. }
  111. static void nfs_direct_dirty_pages(struct page **pages, unsigned int pgbase, size_t count)
  112. {
  113. unsigned int npages;
  114. unsigned int i;
  115. if (count == 0)
  116. return;
  117. pages += (pgbase >> PAGE_SHIFT);
  118. npages = (count + (pgbase & ~PAGE_MASK) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  119. for (i = 0; i < npages; i++) {
  120. struct page *page = pages[i];
  121. if (!PageCompound(page))
  122. set_page_dirty(page);
  123. }
  124. }
  125. static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
  126. {
  127. unsigned int i;
  128. for (i = 0; i < npages; i++)
  129. page_cache_release(pages[i]);
  130. }
  131. static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
  132. {
  133. struct nfs_direct_req *dreq;
  134. dreq = kmem_cache_alloc(nfs_direct_cachep, GFP_KERNEL);
  135. if (!dreq)
  136. return NULL;
  137. kref_init(&dreq->kref);
  138. kref_get(&dreq->kref);
  139. init_completion(&dreq->completion);
  140. INIT_LIST_HEAD(&dreq->rewrite_list);
  141. dreq->iocb = NULL;
  142. dreq->ctx = NULL;
  143. dreq->l_ctx = NULL;
  144. spin_lock_init(&dreq->lock);
  145. atomic_set(&dreq->io_count, 0);
  146. dreq->count = 0;
  147. dreq->error = 0;
  148. dreq->flags = 0;
  149. return dreq;
  150. }
  151. static void nfs_direct_req_free(struct kref *kref)
  152. {
  153. struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
  154. if (dreq->l_ctx != NULL)
  155. nfs_put_lock_context(dreq->l_ctx);
  156. if (dreq->ctx != NULL)
  157. put_nfs_open_context(dreq->ctx);
  158. kmem_cache_free(nfs_direct_cachep, dreq);
  159. }
  160. static void nfs_direct_req_release(struct nfs_direct_req *dreq)
  161. {
  162. kref_put(&dreq->kref, nfs_direct_req_free);
  163. }
  164. /*
  165. * Collects and returns the final error value/byte-count.
  166. */
  167. static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
  168. {
  169. ssize_t result = -EIOCBQUEUED;
  170. /* Async requests don't wait here */
  171. if (dreq->iocb)
  172. goto out;
  173. result = wait_for_completion_killable(&dreq->completion);
  174. if (!result)
  175. result = dreq->error;
  176. if (!result)
  177. result = dreq->count;
  178. out:
  179. return (ssize_t) result;
  180. }
  181. /*
  182. * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
  183. * the iocb is still valid here if this is a synchronous request.
  184. */
  185. static void nfs_direct_complete(struct nfs_direct_req *dreq)
  186. {
  187. if (dreq->iocb) {
  188. long res = (long) dreq->error;
  189. if (!res)
  190. res = (long) dreq->count;
  191. aio_complete(dreq->iocb, res, 0);
  192. }
  193. complete_all(&dreq->completion);
  194. nfs_direct_req_release(dreq);
  195. }
  196. /*
  197. * We must hold a reference to all the pages in this direct read request
  198. * until the RPCs complete. This could be long *after* we are woken up in
  199. * nfs_direct_wait (for instance, if someone hits ^C on a slow server).
  200. */
  201. static void nfs_direct_read_result(struct rpc_task *task, void *calldata)
  202. {
  203. struct nfs_read_data *data = calldata;
  204. nfs_readpage_result(task, data);
  205. }
  206. static void nfs_direct_read_release(void *calldata)
  207. {
  208. struct nfs_read_data *data = calldata;
  209. struct nfs_direct_req *dreq = (struct nfs_direct_req *)data->header->req;
  210. int status = data->task.tk_status;
  211. spin_lock(&dreq->lock);
  212. if (unlikely(status < 0)) {
  213. dreq->error = status;
  214. spin_unlock(&dreq->lock);
  215. } else {
  216. dreq->count += data->res.count;
  217. spin_unlock(&dreq->lock);
  218. nfs_direct_dirty_pages(data->pagevec,
  219. data->args.pgbase,
  220. data->res.count);
  221. }
  222. nfs_direct_release_pages(data->pagevec, data->npages);
  223. if (put_dreq(dreq))
  224. nfs_direct_complete(dreq);
  225. nfs_readdata_release(data);
  226. }
  227. static const struct rpc_call_ops nfs_read_direct_ops = {
  228. .rpc_call_prepare = nfs_read_prepare,
  229. .rpc_call_done = nfs_direct_read_result,
  230. .rpc_release = nfs_direct_read_release,
  231. };
  232. static void nfs_direct_readhdr_release(struct nfs_read_header *rhdr)
  233. {
  234. struct nfs_read_data *data = &rhdr->rpc_data;
  235. if (data->pagevec != data->page_array)
  236. kfree(data->pagevec);
  237. nfs_readhdr_free(&rhdr->header);
  238. }
  239. /*
  240. * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
  241. * operation. If nfs_readdata_alloc() or get_user_pages() fails,
  242. * bail and stop sending more reads. Read length accounting is
  243. * handled automatically by nfs_direct_read_result(). Otherwise, if
  244. * no requests have been sent, just return an error.
  245. */
  246. static ssize_t nfs_direct_read_schedule_segment(struct nfs_direct_req *dreq,
  247. const struct iovec *iov,
  248. loff_t pos)
  249. {
  250. struct nfs_open_context *ctx = dreq->ctx;
  251. struct inode *inode = ctx->dentry->d_inode;
  252. unsigned long user_addr = (unsigned long)iov->iov_base;
  253. size_t count = iov->iov_len;
  254. size_t rsize = NFS_SERVER(inode)->rsize;
  255. struct rpc_task *task;
  256. struct rpc_message msg = {
  257. .rpc_cred = ctx->cred,
  258. };
  259. struct rpc_task_setup task_setup_data = {
  260. .rpc_client = NFS_CLIENT(inode),
  261. .rpc_message = &msg,
  262. .callback_ops = &nfs_read_direct_ops,
  263. .workqueue = nfsiod_workqueue,
  264. .flags = RPC_TASK_ASYNC,
  265. };
  266. unsigned int pgbase;
  267. int result;
  268. ssize_t started = 0;
  269. do {
  270. struct nfs_read_header *rhdr;
  271. struct nfs_read_data *data;
  272. size_t bytes;
  273. pgbase = user_addr & ~PAGE_MASK;
  274. bytes = min(rsize,count);
  275. result = -ENOMEM;
  276. rhdr = nfs_readhdr_alloc(nfs_page_array_len(pgbase, bytes));
  277. if (unlikely(!rhdr))
  278. break;
  279. data = &rhdr->rpc_data;
  280. down_read(&current->mm->mmap_sem);
  281. result = get_user_pages(current, current->mm, user_addr,
  282. data->npages, 1, 0, data->pagevec, NULL);
  283. up_read(&current->mm->mmap_sem);
  284. if (result < 0) {
  285. nfs_direct_readhdr_release(rhdr);
  286. break;
  287. }
  288. if ((unsigned)result < data->npages) {
  289. bytes = result * PAGE_SIZE;
  290. if (bytes <= pgbase) {
  291. nfs_direct_release_pages(data->pagevec, result);
  292. nfs_direct_readhdr_release(rhdr);
  293. break;
  294. }
  295. bytes -= pgbase;
  296. data->npages = result;
  297. }
  298. get_dreq(dreq);
  299. rhdr->header.req = (struct nfs_page *) dreq;
  300. rhdr->header.inode = inode;
  301. rhdr->header.cred = msg.rpc_cred;
  302. data->args.fh = NFS_FH(inode);
  303. data->args.context = get_nfs_open_context(ctx);
  304. data->args.lock_context = dreq->l_ctx;
  305. data->args.offset = pos;
  306. data->args.pgbase = pgbase;
  307. data->args.pages = data->pagevec;
  308. data->args.count = bytes;
  309. data->res.fattr = &data->fattr;
  310. data->res.eof = 0;
  311. data->res.count = bytes;
  312. nfs_fattr_init(&data->fattr);
  313. msg.rpc_argp = &data->args;
  314. msg.rpc_resp = &data->res;
  315. task_setup_data.task = &data->task;
  316. task_setup_data.callback_data = data;
  317. NFS_PROTO(inode)->read_setup(data, &msg);
  318. task = rpc_run_task(&task_setup_data);
  319. if (IS_ERR(task))
  320. break;
  321. dprintk("NFS: %5u initiated direct read call "
  322. "(req %s/%Ld, %zu bytes @ offset %Lu)\n",
  323. task->tk_pid,
  324. inode->i_sb->s_id,
  325. (long long)NFS_FILEID(inode),
  326. bytes,
  327. (unsigned long long)data->args.offset);
  328. rpc_put_task(task);
  329. started += bytes;
  330. user_addr += bytes;
  331. pos += bytes;
  332. /* FIXME: Remove this unnecessary math from final patch */
  333. pgbase += bytes;
  334. pgbase &= ~PAGE_MASK;
  335. BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
  336. count -= bytes;
  337. } while (count != 0);
  338. if (started)
  339. return started;
  340. return result < 0 ? (ssize_t) result : -EFAULT;
  341. }
  342. static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
  343. const struct iovec *iov,
  344. unsigned long nr_segs,
  345. loff_t pos)
  346. {
  347. ssize_t result = -EINVAL;
  348. size_t requested_bytes = 0;
  349. unsigned long seg;
  350. get_dreq(dreq);
  351. for (seg = 0; seg < nr_segs; seg++) {
  352. const struct iovec *vec = &iov[seg];
  353. result = nfs_direct_read_schedule_segment(dreq, vec, pos);
  354. if (result < 0)
  355. break;
  356. requested_bytes += result;
  357. if ((size_t)result < vec->iov_len)
  358. break;
  359. pos += vec->iov_len;
  360. }
  361. /*
  362. * If no bytes were started, return the error, and let the
  363. * generic layer handle the completion.
  364. */
  365. if (requested_bytes == 0) {
  366. nfs_direct_req_release(dreq);
  367. return result < 0 ? result : -EIO;
  368. }
  369. if (put_dreq(dreq))
  370. nfs_direct_complete(dreq);
  371. return 0;
  372. }
  373. static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
  374. unsigned long nr_segs, loff_t pos)
  375. {
  376. ssize_t result = -ENOMEM;
  377. struct inode *inode = iocb->ki_filp->f_mapping->host;
  378. struct nfs_direct_req *dreq;
  379. dreq = nfs_direct_req_alloc();
  380. if (dreq == NULL)
  381. goto out;
  382. dreq->inode = inode;
  383. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  384. dreq->l_ctx = nfs_get_lock_context(dreq->ctx);
  385. if (dreq->l_ctx == NULL)
  386. goto out_release;
  387. if (!is_sync_kiocb(iocb))
  388. dreq->iocb = iocb;
  389. result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos);
  390. if (!result)
  391. result = nfs_direct_wait(dreq);
  392. out_release:
  393. nfs_direct_req_release(dreq);
  394. out:
  395. return result;
  396. }
  397. static void nfs_direct_writehdr_release(struct nfs_write_header *whdr)
  398. {
  399. struct nfs_write_data *data = &whdr->rpc_data;
  400. if (data->pagevec != data->page_array)
  401. kfree(data->pagevec);
  402. nfs_writehdr_free(&whdr->header);
  403. }
  404. static void nfs_direct_free_writedata(struct nfs_direct_req *dreq)
  405. {
  406. while (!list_empty(&dreq->rewrite_list)) {
  407. struct nfs_pgio_header *hdr = list_entry(dreq->rewrite_list.next, struct nfs_pgio_header, pages);
  408. struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header);
  409. list_del(&hdr->pages);
  410. nfs_direct_release_pages(whdr->rpc_data.pagevec, whdr->rpc_data.npages);
  411. nfs_direct_writehdr_release(whdr);
  412. }
  413. }
  414. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  415. static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
  416. {
  417. struct inode *inode = dreq->inode;
  418. struct list_head *p;
  419. struct nfs_write_data *data;
  420. struct nfs_pgio_header *hdr;
  421. struct rpc_task *task;
  422. struct rpc_message msg = {
  423. .rpc_cred = dreq->ctx->cred,
  424. };
  425. struct rpc_task_setup task_setup_data = {
  426. .rpc_client = NFS_CLIENT(inode),
  427. .rpc_message = &msg,
  428. .callback_ops = &nfs_write_direct_ops,
  429. .workqueue = nfsiod_workqueue,
  430. .flags = RPC_TASK_ASYNC,
  431. };
  432. dreq->count = 0;
  433. get_dreq(dreq);
  434. list_for_each(p, &dreq->rewrite_list) {
  435. hdr = list_entry(p, struct nfs_pgio_header, pages);
  436. data = &(container_of(hdr, struct nfs_write_header, header))->rpc_data;
  437. get_dreq(dreq);
  438. /* Use stable writes */
  439. data->args.stable = NFS_FILE_SYNC;
  440. /*
  441. * Reset data->res.
  442. */
  443. nfs_fattr_init(&data->fattr);
  444. data->res.count = data->args.count;
  445. memset(&data->verf, 0, sizeof(data->verf));
  446. /*
  447. * Reuse data->task; data->args should not have changed
  448. * since the original request was sent.
  449. */
  450. task_setup_data.task = &data->task;
  451. task_setup_data.callback_data = data;
  452. msg.rpc_argp = &data->args;
  453. msg.rpc_resp = &data->res;
  454. NFS_PROTO(inode)->write_setup(data, &msg);
  455. /*
  456. * We're called via an RPC callback, so BKL is already held.
  457. */
  458. task = rpc_run_task(&task_setup_data);
  459. if (!IS_ERR(task))
  460. rpc_put_task(task);
  461. dprintk("NFS: %5u rescheduled direct write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  462. data->task.tk_pid,
  463. inode->i_sb->s_id,
  464. (long long)NFS_FILEID(inode),
  465. data->args.count,
  466. (unsigned long long)data->args.offset);
  467. }
  468. if (put_dreq(dreq))
  469. nfs_direct_write_complete(dreq, inode);
  470. }
  471. static void nfs_direct_commit_result(struct rpc_task *task, void *calldata)
  472. {
  473. struct nfs_commit_data *data = calldata;
  474. /* Call the NFS version-specific code */
  475. NFS_PROTO(data->inode)->commit_done(task, data);
  476. }
  477. static void nfs_direct_commit_release(void *calldata)
  478. {
  479. struct nfs_commit_data *data = calldata;
  480. struct nfs_direct_req *dreq = data->dreq;
  481. int status = data->task.tk_status;
  482. if (status < 0) {
  483. dprintk("NFS: %5u commit failed with error %d.\n",
  484. data->task.tk_pid, status);
  485. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  486. } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
  487. dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
  488. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  489. }
  490. dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
  491. nfs_direct_write_complete(dreq, data->inode);
  492. nfs_commit_free(data);
  493. }
  494. static const struct rpc_call_ops nfs_commit_direct_ops = {
  495. .rpc_call_prepare = nfs_commit_prepare,
  496. .rpc_call_done = nfs_direct_commit_result,
  497. .rpc_release = nfs_direct_commit_release,
  498. };
  499. static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
  500. {
  501. struct nfs_commit_data *data = dreq->commit_data;
  502. struct rpc_task *task;
  503. struct rpc_message msg = {
  504. .rpc_argp = &data->args,
  505. .rpc_resp = &data->res,
  506. .rpc_cred = dreq->ctx->cred,
  507. };
  508. struct rpc_task_setup task_setup_data = {
  509. .task = &data->task,
  510. .rpc_client = NFS_CLIENT(dreq->inode),
  511. .rpc_message = &msg,
  512. .callback_ops = &nfs_commit_direct_ops,
  513. .callback_data = data,
  514. .workqueue = nfsiod_workqueue,
  515. .flags = RPC_TASK_ASYNC,
  516. };
  517. data->inode = dreq->inode;
  518. data->cred = msg.rpc_cred;
  519. data->args.fh = NFS_FH(data->inode);
  520. data->args.offset = 0;
  521. data->args.count = 0;
  522. data->res.fattr = &data->fattr;
  523. data->res.verf = &data->verf;
  524. nfs_fattr_init(&data->fattr);
  525. NFS_PROTO(data->inode)->commit_setup(data, &msg);
  526. /* Note: task.tk_ops->rpc_release will free dreq->commit_data */
  527. dreq->commit_data = NULL;
  528. dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
  529. task = rpc_run_task(&task_setup_data);
  530. if (!IS_ERR(task))
  531. rpc_put_task(task);
  532. }
  533. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  534. {
  535. int flags = dreq->flags;
  536. dreq->flags = 0;
  537. switch (flags) {
  538. case NFS_ODIRECT_DO_COMMIT:
  539. nfs_direct_commit_schedule(dreq);
  540. break;
  541. case NFS_ODIRECT_RESCHED_WRITES:
  542. nfs_direct_write_reschedule(dreq);
  543. break;
  544. default:
  545. if (dreq->commit_data != NULL)
  546. nfs_commit_free(dreq->commit_data);
  547. nfs_direct_free_writedata(dreq);
  548. nfs_zap_mapping(inode, inode->i_mapping);
  549. nfs_direct_complete(dreq);
  550. }
  551. }
  552. static void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
  553. {
  554. dreq->commit_data = nfs_commitdata_alloc();
  555. if (dreq->commit_data != NULL)
  556. dreq->commit_data->dreq = dreq;
  557. }
  558. #else
  559. static inline void nfs_alloc_commit_data(struct nfs_direct_req *dreq)
  560. {
  561. dreq->commit_data = NULL;
  562. }
  563. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  564. {
  565. nfs_direct_free_writedata(dreq);
  566. nfs_zap_mapping(inode, inode->i_mapping);
  567. nfs_direct_complete(dreq);
  568. }
  569. #endif
  570. static void nfs_direct_write_result(struct rpc_task *task, void *calldata)
  571. {
  572. struct nfs_write_data *data = calldata;
  573. nfs_writeback_done(task, data);
  574. }
  575. /*
  576. * NB: Return the value of the first error return code. Subsequent
  577. * errors after the first one are ignored.
  578. */
  579. static void nfs_direct_write_release(void *calldata)
  580. {
  581. struct nfs_write_data *data = calldata;
  582. struct nfs_pgio_header *hdr = data->header;
  583. struct nfs_direct_req *dreq = (struct nfs_direct_req *) hdr->req;
  584. int status = data->task.tk_status;
  585. spin_lock(&dreq->lock);
  586. if (unlikely(status < 0)) {
  587. /* An error has occurred, so we should not commit */
  588. dreq->flags = 0;
  589. dreq->error = status;
  590. }
  591. if (unlikely(dreq->error != 0))
  592. goto out_unlock;
  593. dreq->count += data->res.count;
  594. if (data->res.verf->committed != NFS_FILE_SYNC) {
  595. switch (dreq->flags) {
  596. case 0:
  597. memcpy(&dreq->verf, &data->verf, sizeof(dreq->verf));
  598. dreq->flags = NFS_ODIRECT_DO_COMMIT;
  599. break;
  600. case NFS_ODIRECT_DO_COMMIT:
  601. if (memcmp(&dreq->verf, &data->verf, sizeof(dreq->verf))) {
  602. dprintk("NFS: %5u write verify failed\n", data->task.tk_pid);
  603. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  604. }
  605. }
  606. }
  607. out_unlock:
  608. spin_unlock(&dreq->lock);
  609. if (put_dreq(dreq))
  610. nfs_direct_write_complete(dreq, hdr->inode);
  611. }
  612. static const struct rpc_call_ops nfs_write_direct_ops = {
  613. .rpc_call_prepare = nfs_write_prepare,
  614. .rpc_call_done = nfs_direct_write_result,
  615. .rpc_release = nfs_direct_write_release,
  616. };
  617. /*
  618. * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
  619. * operation. If nfs_writedata_alloc() or get_user_pages() fails,
  620. * bail and stop sending more writes. Write length accounting is
  621. * handled automatically by nfs_direct_write_result(). Otherwise, if
  622. * no requests have been sent, just return an error.
  623. */
  624. static ssize_t nfs_direct_write_schedule_segment(struct nfs_direct_req *dreq,
  625. const struct iovec *iov,
  626. loff_t pos, int sync)
  627. {
  628. struct nfs_open_context *ctx = dreq->ctx;
  629. struct inode *inode = ctx->dentry->d_inode;
  630. unsigned long user_addr = (unsigned long)iov->iov_base;
  631. size_t count = iov->iov_len;
  632. struct rpc_task *task;
  633. struct rpc_message msg = {
  634. .rpc_cred = ctx->cred,
  635. };
  636. struct rpc_task_setup task_setup_data = {
  637. .rpc_client = NFS_CLIENT(inode),
  638. .rpc_message = &msg,
  639. .callback_ops = &nfs_write_direct_ops,
  640. .workqueue = nfsiod_workqueue,
  641. .flags = RPC_TASK_ASYNC,
  642. };
  643. size_t wsize = NFS_SERVER(inode)->wsize;
  644. unsigned int pgbase;
  645. int result;
  646. ssize_t started = 0;
  647. do {
  648. struct nfs_write_header *whdr;
  649. struct nfs_write_data *data;
  650. size_t bytes;
  651. pgbase = user_addr & ~PAGE_MASK;
  652. bytes = min(wsize,count);
  653. result = -ENOMEM;
  654. whdr = nfs_writehdr_alloc(nfs_page_array_len(pgbase, bytes));
  655. if (unlikely(!whdr))
  656. break;
  657. data = &whdr->rpc_data;
  658. down_read(&current->mm->mmap_sem);
  659. result = get_user_pages(current, current->mm, user_addr,
  660. data->npages, 0, 0, data->pagevec, NULL);
  661. up_read(&current->mm->mmap_sem);
  662. if (result < 0) {
  663. nfs_direct_writehdr_release(whdr);
  664. break;
  665. }
  666. if ((unsigned)result < data->npages) {
  667. bytes = result * PAGE_SIZE;
  668. if (bytes <= pgbase) {
  669. nfs_direct_release_pages(data->pagevec, result);
  670. nfs_direct_writehdr_release(whdr);
  671. break;
  672. }
  673. bytes -= pgbase;
  674. data->npages = result;
  675. }
  676. get_dreq(dreq);
  677. list_move_tail(&whdr->header.pages, &dreq->rewrite_list);
  678. whdr->header.req = (struct nfs_page *) dreq;
  679. whdr->header.inode = inode;
  680. whdr->header.cred = msg.rpc_cred;
  681. data->args.fh = NFS_FH(inode);
  682. data->args.context = ctx;
  683. data->args.lock_context = dreq->l_ctx;
  684. data->args.offset = pos;
  685. data->args.pgbase = pgbase;
  686. data->args.pages = data->pagevec;
  687. data->args.count = bytes;
  688. data->args.stable = sync;
  689. data->res.fattr = &data->fattr;
  690. data->res.count = bytes;
  691. data->res.verf = &data->verf;
  692. nfs_fattr_init(&data->fattr);
  693. task_setup_data.task = &data->task;
  694. task_setup_data.callback_data = data;
  695. msg.rpc_argp = &data->args;
  696. msg.rpc_resp = &data->res;
  697. NFS_PROTO(inode)->write_setup(data, &msg);
  698. task = rpc_run_task(&task_setup_data);
  699. if (IS_ERR(task))
  700. break;
  701. dprintk("NFS: %5u initiated direct write call "
  702. "(req %s/%Ld, %zu bytes @ offset %Lu)\n",
  703. task->tk_pid,
  704. inode->i_sb->s_id,
  705. (long long)NFS_FILEID(inode),
  706. bytes,
  707. (unsigned long long)data->args.offset);
  708. rpc_put_task(task);
  709. started += bytes;
  710. user_addr += bytes;
  711. pos += bytes;
  712. /* FIXME: Remove this useless math from the final patch */
  713. pgbase += bytes;
  714. pgbase &= ~PAGE_MASK;
  715. BUG_ON(pgbase != (user_addr & ~PAGE_MASK));
  716. count -= bytes;
  717. } while (count != 0);
  718. if (started)
  719. return started;
  720. return result < 0 ? (ssize_t) result : -EFAULT;
  721. }
  722. static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
  723. const struct iovec *iov,
  724. unsigned long nr_segs,
  725. loff_t pos, int sync)
  726. {
  727. ssize_t result = 0;
  728. size_t requested_bytes = 0;
  729. unsigned long seg;
  730. get_dreq(dreq);
  731. for (seg = 0; seg < nr_segs; seg++) {
  732. const struct iovec *vec = &iov[seg];
  733. result = nfs_direct_write_schedule_segment(dreq, vec,
  734. pos, sync);
  735. if (result < 0)
  736. break;
  737. requested_bytes += result;
  738. if ((size_t)result < vec->iov_len)
  739. break;
  740. pos += vec->iov_len;
  741. }
  742. /*
  743. * If no bytes were started, return the error, and let the
  744. * generic layer handle the completion.
  745. */
  746. if (requested_bytes == 0) {
  747. nfs_direct_req_release(dreq);
  748. return result < 0 ? result : -EIO;
  749. }
  750. if (put_dreq(dreq))
  751. nfs_direct_write_complete(dreq, dreq->inode);
  752. return 0;
  753. }
  754. static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
  755. unsigned long nr_segs, loff_t pos,
  756. size_t count)
  757. {
  758. ssize_t result = -ENOMEM;
  759. struct inode *inode = iocb->ki_filp->f_mapping->host;
  760. struct nfs_direct_req *dreq;
  761. size_t wsize = NFS_SERVER(inode)->wsize;
  762. int sync = NFS_UNSTABLE;
  763. dreq = nfs_direct_req_alloc();
  764. if (!dreq)
  765. goto out;
  766. nfs_alloc_commit_data(dreq);
  767. if (dreq->commit_data == NULL || count <= wsize)
  768. sync = NFS_FILE_SYNC;
  769. dreq->inode = inode;
  770. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  771. dreq->l_ctx = nfs_get_lock_context(dreq->ctx);
  772. if (dreq->l_ctx == NULL)
  773. goto out_release;
  774. if (!is_sync_kiocb(iocb))
  775. dreq->iocb = iocb;
  776. result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, sync);
  777. if (!result)
  778. result = nfs_direct_wait(dreq);
  779. out_release:
  780. nfs_direct_req_release(dreq);
  781. out:
  782. return result;
  783. }
  784. /**
  785. * nfs_file_direct_read - file direct read operation for NFS files
  786. * @iocb: target I/O control block
  787. * @iov: vector of user buffers into which to read data
  788. * @nr_segs: size of iov vector
  789. * @pos: byte offset in file where reading starts
  790. *
  791. * We use this function for direct reads instead of calling
  792. * generic_file_aio_read() in order to avoid gfar's check to see if
  793. * the request starts before the end of the file. For that check
  794. * to work, we must generate a GETATTR before each direct read, and
  795. * even then there is a window between the GETATTR and the subsequent
  796. * READ where the file size could change. Our preference is simply
  797. * to do all reads the application wants, and the server will take
  798. * care of managing the end of file boundary.
  799. *
  800. * This function also eliminates unnecessarily updating the file's
  801. * atime locally, as the NFS server sets the file's atime, and this
  802. * client must read the updated atime from the server back into its
  803. * cache.
  804. */
  805. ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
  806. unsigned long nr_segs, loff_t pos)
  807. {
  808. ssize_t retval = -EINVAL;
  809. struct file *file = iocb->ki_filp;
  810. struct address_space *mapping = file->f_mapping;
  811. size_t count;
  812. count = iov_length(iov, nr_segs);
  813. nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
  814. dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
  815. file->f_path.dentry->d_parent->d_name.name,
  816. file->f_path.dentry->d_name.name,
  817. count, (long long) pos);
  818. retval = 0;
  819. if (!count)
  820. goto out;
  821. retval = nfs_sync_mapping(mapping);
  822. if (retval)
  823. goto out;
  824. task_io_account_read(count);
  825. retval = nfs_direct_read(iocb, iov, nr_segs, pos);
  826. if (retval > 0)
  827. iocb->ki_pos = pos + retval;
  828. out:
  829. return retval;
  830. }
  831. /**
  832. * nfs_file_direct_write - file direct write operation for NFS files
  833. * @iocb: target I/O control block
  834. * @iov: vector of user buffers from which to write data
  835. * @nr_segs: size of iov vector
  836. * @pos: byte offset in file where writing starts
  837. *
  838. * We use this function for direct writes instead of calling
  839. * generic_file_aio_write() in order to avoid taking the inode
  840. * semaphore and updating the i_size. The NFS server will set
  841. * the new i_size and this client must read the updated size
  842. * back into its cache. We let the server do generic write
  843. * parameter checking and report problems.
  844. *
  845. * We eliminate local atime updates, see direct read above.
  846. *
  847. * We avoid unnecessary page cache invalidations for normal cached
  848. * readers of this file.
  849. *
  850. * Note that O_APPEND is not supported for NFS direct writes, as there
  851. * is no atomic O_APPEND write facility in the NFS protocol.
  852. */
  853. ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
  854. unsigned long nr_segs, loff_t pos)
  855. {
  856. ssize_t retval = -EINVAL;
  857. struct file *file = iocb->ki_filp;
  858. struct address_space *mapping = file->f_mapping;
  859. size_t count;
  860. count = iov_length(iov, nr_segs);
  861. nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
  862. dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
  863. file->f_path.dentry->d_parent->d_name.name,
  864. file->f_path.dentry->d_name.name,
  865. count, (long long) pos);
  866. retval = generic_write_checks(file, &pos, &count, 0);
  867. if (retval)
  868. goto out;
  869. retval = -EINVAL;
  870. if ((ssize_t) count < 0)
  871. goto out;
  872. retval = 0;
  873. if (!count)
  874. goto out;
  875. retval = nfs_sync_mapping(mapping);
  876. if (retval)
  877. goto out;
  878. task_io_account_write(count);
  879. retval = nfs_direct_write(iocb, iov, nr_segs, pos, count);
  880. if (retval > 0)
  881. iocb->ki_pos = pos + retval;
  882. out:
  883. return retval;
  884. }
  885. /**
  886. * nfs_init_directcache - create a slab cache for nfs_direct_req structures
  887. *
  888. */
  889. int __init nfs_init_directcache(void)
  890. {
  891. nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
  892. sizeof(struct nfs_direct_req),
  893. 0, (SLAB_RECLAIM_ACCOUNT|
  894. SLAB_MEM_SPREAD),
  895. NULL);
  896. if (nfs_direct_cachep == NULL)
  897. return -ENOMEM;
  898. return 0;
  899. }
  900. /**
  901. * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
  902. *
  903. */
  904. void nfs_destroy_directcache(void)
  905. {
  906. kmem_cache_destroy(nfs_direct_cachep);
  907. }