direct.c 27 KB

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