direct.c 26 KB

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