direct.c 26 KB

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