direct.c 28 KB

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