direct.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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/module.h>
  49. #include <linux/nfs_fs.h>
  50. #include <linux/nfs_page.h>
  51. #include <linux/sunrpc/clnt.h>
  52. #include <linux/uaccess.h>
  53. #include <linux/atomic.h>
  54. #include "internal.h"
  55. #include "iostat.h"
  56. #include "pnfs.h"
  57. #define NFSDBG_FACILITY NFSDBG_VFS
  58. static struct kmem_cache *nfs_direct_cachep;
  59. /*
  60. * This represents a set of asynchronous requests that we're waiting on
  61. */
  62. struct nfs_direct_mirror {
  63. ssize_t count;
  64. };
  65. struct nfs_direct_req {
  66. struct kref kref; /* release manager */
  67. /* I/O parameters */
  68. struct nfs_open_context *ctx; /* file open context info */
  69. struct nfs_lock_context *l_ctx; /* Lock context info */
  70. struct kiocb * iocb; /* controlling i/o request */
  71. struct inode * inode; /* target file of i/o */
  72. /* completion state */
  73. atomic_t io_count; /* i/os we're waiting for */
  74. spinlock_t lock; /* protect completion state */
  75. struct nfs_direct_mirror mirrors[NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX];
  76. int mirror_count;
  77. loff_t io_start; /* Start offset for I/O */
  78. ssize_t count, /* bytes actually processed */
  79. max_count, /* max expected count */
  80. bytes_left, /* bytes left to be sent */
  81. error; /* any reported error */
  82. struct completion completion; /* wait for i/o completion */
  83. /* commit state */
  84. struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */
  85. struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */
  86. struct work_struct work;
  87. int flags;
  88. /* for write */
  89. #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
  90. #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
  91. /* for read */
  92. #define NFS_ODIRECT_SHOULD_DIRTY (3) /* dirty user-space page after read */
  93. struct nfs_writeverf verf; /* unstable write verifier */
  94. };
  95. static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
  96. static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
  97. static void nfs_direct_write_complete(struct nfs_direct_req *dreq);
  98. static void nfs_direct_write_schedule_work(struct work_struct *work);
  99. static inline void get_dreq(struct nfs_direct_req *dreq)
  100. {
  101. atomic_inc(&dreq->io_count);
  102. }
  103. static inline int put_dreq(struct nfs_direct_req *dreq)
  104. {
  105. return atomic_dec_and_test(&dreq->io_count);
  106. }
  107. static void
  108. nfs_direct_handle_truncated(struct nfs_direct_req *dreq,
  109. const struct nfs_pgio_header *hdr,
  110. ssize_t dreq_len)
  111. {
  112. struct nfs_direct_mirror *mirror = &dreq->mirrors[hdr->pgio_mirror_idx];
  113. if (!(test_bit(NFS_IOHDR_ERROR, &hdr->flags) ||
  114. test_bit(NFS_IOHDR_EOF, &hdr->flags)))
  115. return;
  116. if (dreq->max_count >= dreq_len) {
  117. dreq->max_count = dreq_len;
  118. if (dreq->count > dreq_len)
  119. dreq->count = dreq_len;
  120. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags))
  121. dreq->error = hdr->error;
  122. else /* Clear outstanding error if this is EOF */
  123. dreq->error = 0;
  124. }
  125. if (mirror->count > dreq_len)
  126. mirror->count = dreq_len;
  127. }
  128. static void
  129. nfs_direct_count_bytes(struct nfs_direct_req *dreq,
  130. const struct nfs_pgio_header *hdr)
  131. {
  132. struct nfs_direct_mirror *mirror = &dreq->mirrors[hdr->pgio_mirror_idx];
  133. loff_t hdr_end = hdr->io_start + hdr->good_bytes;
  134. ssize_t dreq_len = 0;
  135. if (hdr_end > dreq->io_start)
  136. dreq_len = hdr_end - dreq->io_start;
  137. nfs_direct_handle_truncated(dreq, hdr, dreq_len);
  138. if (dreq_len > dreq->max_count)
  139. dreq_len = dreq->max_count;
  140. if (mirror->count < dreq_len)
  141. mirror->count = dreq_len;
  142. if (dreq->count < dreq_len)
  143. dreq->count = dreq_len;
  144. }
  145. /*
  146. * nfs_direct_select_verf - select the right verifier
  147. * @dreq - direct request possibly spanning multiple servers
  148. * @ds_clp - nfs_client of data server or NULL if MDS / non-pnfs
  149. * @commit_idx - commit bucket index for the DS
  150. *
  151. * returns the correct verifier to use given the role of the server
  152. */
  153. static struct nfs_writeverf *
  154. nfs_direct_select_verf(struct nfs_direct_req *dreq,
  155. struct nfs_client *ds_clp,
  156. int commit_idx)
  157. {
  158. struct nfs_writeverf *verfp = &dreq->verf;
  159. #ifdef CONFIG_NFS_V4_1
  160. /*
  161. * pNFS is in use, use the DS verf except commit_through_mds is set
  162. * for layout segment where nbuckets is zero.
  163. */
  164. if (ds_clp && dreq->ds_cinfo.nbuckets > 0) {
  165. if (commit_idx >= 0 && commit_idx < dreq->ds_cinfo.nbuckets)
  166. verfp = &dreq->ds_cinfo.buckets[commit_idx].direct_verf;
  167. else
  168. WARN_ON_ONCE(1);
  169. }
  170. #endif
  171. return verfp;
  172. }
  173. /*
  174. * nfs_direct_set_hdr_verf - set the write/commit verifier
  175. * @dreq - direct request possibly spanning multiple servers
  176. * @hdr - pageio header to validate against previously seen verfs
  177. *
  178. * Set the server's (MDS or DS) "seen" verifier
  179. */
  180. static void nfs_direct_set_hdr_verf(struct nfs_direct_req *dreq,
  181. struct nfs_pgio_header *hdr)
  182. {
  183. struct nfs_writeverf *verfp;
  184. verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
  185. WARN_ON_ONCE(verfp->committed >= 0);
  186. memcpy(verfp, &hdr->verf, sizeof(struct nfs_writeverf));
  187. WARN_ON_ONCE(verfp->committed < 0);
  188. }
  189. static int nfs_direct_cmp_verf(const struct nfs_writeverf *v1,
  190. const struct nfs_writeverf *v2)
  191. {
  192. return nfs_write_verifier_cmp(&v1->verifier, &v2->verifier);
  193. }
  194. /*
  195. * nfs_direct_cmp_hdr_verf - compare verifier for pgio header
  196. * @dreq - direct request possibly spanning multiple servers
  197. * @hdr - pageio header to validate against previously seen verf
  198. *
  199. * set the server's "seen" verf if not initialized.
  200. * returns result of comparison between @hdr->verf and the "seen"
  201. * verf of the server used by @hdr (DS or MDS)
  202. */
  203. static int nfs_direct_set_or_cmp_hdr_verf(struct nfs_direct_req *dreq,
  204. struct nfs_pgio_header *hdr)
  205. {
  206. struct nfs_writeverf *verfp;
  207. verfp = nfs_direct_select_verf(dreq, hdr->ds_clp, hdr->ds_commit_idx);
  208. if (verfp->committed < 0) {
  209. nfs_direct_set_hdr_verf(dreq, hdr);
  210. return 0;
  211. }
  212. return nfs_direct_cmp_verf(verfp, &hdr->verf);
  213. }
  214. /*
  215. * nfs_direct_cmp_commit_data_verf - compare verifier for commit data
  216. * @dreq - direct request possibly spanning multiple servers
  217. * @data - commit data to validate against previously seen verf
  218. *
  219. * returns result of comparison between @data->verf and the verf of
  220. * the server used by @data (DS or MDS)
  221. */
  222. static int nfs_direct_cmp_commit_data_verf(struct nfs_direct_req *dreq,
  223. struct nfs_commit_data *data)
  224. {
  225. struct nfs_writeverf *verfp;
  226. verfp = nfs_direct_select_verf(dreq, data->ds_clp,
  227. data->ds_commit_index);
  228. /* verifier not set so always fail */
  229. if (verfp->committed < 0 || data->res.verf->committed <= NFS_UNSTABLE)
  230. return 1;
  231. return nfs_direct_cmp_verf(verfp, data->res.verf);
  232. }
  233. /**
  234. * nfs_direct_IO - NFS address space operation for direct I/O
  235. * @iocb: target I/O control block
  236. * @iter: I/O buffer
  237. *
  238. * The presence of this routine in the address space ops vector means
  239. * the NFS client supports direct I/O. However, for most direct IO, we
  240. * shunt off direct read and write requests before the VFS gets them,
  241. * so this method is only ever called for swap.
  242. */
  243. ssize_t nfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  244. {
  245. struct inode *inode = iocb->ki_filp->f_mapping->host;
  246. /* we only support swap file calling nfs_direct_IO */
  247. if (!IS_SWAPFILE(inode))
  248. return 0;
  249. VM_BUG_ON(iov_iter_count(iter) != PAGE_SIZE);
  250. if (iov_iter_rw(iter) == READ)
  251. return nfs_file_direct_read(iocb, iter);
  252. return nfs_file_direct_write(iocb, iter);
  253. }
  254. static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
  255. {
  256. unsigned int i;
  257. for (i = 0; i < npages; i++)
  258. put_page(pages[i]);
  259. }
  260. void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
  261. struct nfs_direct_req *dreq)
  262. {
  263. cinfo->inode = dreq->inode;
  264. cinfo->mds = &dreq->mds_cinfo;
  265. cinfo->ds = &dreq->ds_cinfo;
  266. cinfo->dreq = dreq;
  267. cinfo->completion_ops = &nfs_direct_commit_completion_ops;
  268. }
  269. static inline void nfs_direct_setup_mirroring(struct nfs_direct_req *dreq,
  270. struct nfs_pageio_descriptor *pgio,
  271. struct nfs_page *req)
  272. {
  273. int mirror_count = 1;
  274. if (pgio->pg_ops->pg_get_mirror_count)
  275. mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
  276. dreq->mirror_count = mirror_count;
  277. }
  278. static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
  279. {
  280. struct nfs_direct_req *dreq;
  281. dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
  282. if (!dreq)
  283. return NULL;
  284. kref_init(&dreq->kref);
  285. kref_get(&dreq->kref);
  286. init_completion(&dreq->completion);
  287. INIT_LIST_HEAD(&dreq->mds_cinfo.list);
  288. dreq->verf.committed = NFS_INVALID_STABLE_HOW; /* not set yet */
  289. INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
  290. dreq->mirror_count = 1;
  291. spin_lock_init(&dreq->lock);
  292. return dreq;
  293. }
  294. static void nfs_direct_req_free(struct kref *kref)
  295. {
  296. struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
  297. nfs_free_pnfs_ds_cinfo(&dreq->ds_cinfo);
  298. if (dreq->l_ctx != NULL)
  299. nfs_put_lock_context(dreq->l_ctx);
  300. if (dreq->ctx != NULL)
  301. put_nfs_open_context(dreq->ctx);
  302. kmem_cache_free(nfs_direct_cachep, dreq);
  303. }
  304. static void nfs_direct_req_release(struct nfs_direct_req *dreq)
  305. {
  306. kref_put(&dreq->kref, nfs_direct_req_free);
  307. }
  308. ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq)
  309. {
  310. return dreq->bytes_left;
  311. }
  312. EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
  313. /*
  314. * Collects and returns the final error value/byte-count.
  315. */
  316. static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
  317. {
  318. ssize_t result = -EIOCBQUEUED;
  319. /* Async requests don't wait here */
  320. if (dreq->iocb)
  321. goto out;
  322. result = wait_for_completion_killable(&dreq->completion);
  323. if (!result) {
  324. result = dreq->count;
  325. WARN_ON_ONCE(dreq->count < 0);
  326. }
  327. if (!result)
  328. result = dreq->error;
  329. out:
  330. return (ssize_t) result;
  331. }
  332. /*
  333. * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
  334. * the iocb is still valid here if this is a synchronous request.
  335. */
  336. static void nfs_direct_complete(struct nfs_direct_req *dreq)
  337. {
  338. struct inode *inode = dreq->inode;
  339. inode_dio_end(inode);
  340. if (dreq->iocb) {
  341. long res = (long) dreq->error;
  342. if (dreq->count != 0) {
  343. res = (long) dreq->count;
  344. WARN_ON_ONCE(dreq->count < 0);
  345. }
  346. dreq->iocb->ki_complete(dreq->iocb, res, 0);
  347. }
  348. complete(&dreq->completion);
  349. nfs_direct_req_release(dreq);
  350. }
  351. static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
  352. {
  353. unsigned long bytes = 0;
  354. struct nfs_direct_req *dreq = hdr->dreq;
  355. spin_lock(&dreq->lock);
  356. if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  357. spin_unlock(&dreq->lock);
  358. goto out_put;
  359. }
  360. nfs_direct_count_bytes(dreq, hdr);
  361. spin_unlock(&dreq->lock);
  362. while (!list_empty(&hdr->pages)) {
  363. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  364. struct page *page = req->wb_page;
  365. if (!PageCompound(page) && bytes < hdr->good_bytes &&
  366. (dreq->flags == NFS_ODIRECT_SHOULD_DIRTY))
  367. set_page_dirty(page);
  368. bytes += req->wb_bytes;
  369. nfs_list_remove_request(req);
  370. nfs_release_request(req);
  371. }
  372. out_put:
  373. if (put_dreq(dreq))
  374. nfs_direct_complete(dreq);
  375. hdr->release(hdr);
  376. }
  377. static void nfs_read_sync_pgio_error(struct list_head *head, int error)
  378. {
  379. struct nfs_page *req;
  380. while (!list_empty(head)) {
  381. req = nfs_list_entry(head->next);
  382. nfs_list_remove_request(req);
  383. nfs_release_request(req);
  384. }
  385. }
  386. static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
  387. {
  388. get_dreq(hdr->dreq);
  389. }
  390. static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
  391. .error_cleanup = nfs_read_sync_pgio_error,
  392. .init_hdr = nfs_direct_pgio_init,
  393. .completion = nfs_direct_read_completion,
  394. };
  395. /*
  396. * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
  397. * operation. If nfs_readdata_alloc() or get_user_pages() fails,
  398. * bail and stop sending more reads. Read length accounting is
  399. * handled automatically by nfs_direct_read_result(). Otherwise, if
  400. * no requests have been sent, just return an error.
  401. */
  402. static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
  403. struct iov_iter *iter,
  404. loff_t pos)
  405. {
  406. struct nfs_pageio_descriptor desc;
  407. struct inode *inode = dreq->inode;
  408. ssize_t result = -EINVAL;
  409. size_t requested_bytes = 0;
  410. size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE);
  411. nfs_pageio_init_read(&desc, dreq->inode, false,
  412. &nfs_direct_read_completion_ops);
  413. get_dreq(dreq);
  414. desc.pg_dreq = dreq;
  415. inode_dio_begin(inode);
  416. while (iov_iter_count(iter)) {
  417. struct page **pagevec;
  418. size_t bytes;
  419. size_t pgbase;
  420. unsigned npages, i;
  421. result = iov_iter_get_pages_alloc(iter, &pagevec,
  422. rsize, &pgbase);
  423. if (result < 0)
  424. break;
  425. bytes = result;
  426. iov_iter_advance(iter, bytes);
  427. npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
  428. for (i = 0; i < npages; i++) {
  429. struct nfs_page *req;
  430. unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
  431. /* XXX do we need to do the eof zeroing found in async_filler? */
  432. req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
  433. pgbase, req_len);
  434. if (IS_ERR(req)) {
  435. result = PTR_ERR(req);
  436. break;
  437. }
  438. req->wb_index = pos >> PAGE_SHIFT;
  439. req->wb_offset = pos & ~PAGE_MASK;
  440. if (!nfs_pageio_add_request(&desc, req)) {
  441. result = desc.pg_error;
  442. nfs_release_request(req);
  443. break;
  444. }
  445. pgbase = 0;
  446. bytes -= req_len;
  447. requested_bytes += req_len;
  448. pos += req_len;
  449. dreq->bytes_left -= req_len;
  450. }
  451. nfs_direct_release_pages(pagevec, npages);
  452. kvfree(pagevec);
  453. if (result < 0)
  454. break;
  455. }
  456. nfs_pageio_complete(&desc);
  457. /*
  458. * If no bytes were started, return the error, and let the
  459. * generic layer handle the completion.
  460. */
  461. if (requested_bytes == 0) {
  462. inode_dio_end(inode);
  463. nfs_direct_req_release(dreq);
  464. return result < 0 ? result : -EIO;
  465. }
  466. if (put_dreq(dreq))
  467. nfs_direct_complete(dreq);
  468. return requested_bytes;
  469. }
  470. /**
  471. * nfs_file_direct_read - file direct read operation for NFS files
  472. * @iocb: target I/O control block
  473. * @iter: vector of user buffers into which to read data
  474. *
  475. * We use this function for direct reads instead of calling
  476. * generic_file_aio_read() in order to avoid gfar's check to see if
  477. * the request starts before the end of the file. For that check
  478. * to work, we must generate a GETATTR before each direct read, and
  479. * even then there is a window between the GETATTR and the subsequent
  480. * READ where the file size could change. Our preference is simply
  481. * to do all reads the application wants, and the server will take
  482. * care of managing the end of file boundary.
  483. *
  484. * This function also eliminates unnecessarily updating the file's
  485. * atime locally, as the NFS server sets the file's atime, and this
  486. * client must read the updated atime from the server back into its
  487. * cache.
  488. */
  489. ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter)
  490. {
  491. struct file *file = iocb->ki_filp;
  492. struct address_space *mapping = file->f_mapping;
  493. struct inode *inode = mapping->host;
  494. struct nfs_direct_req *dreq;
  495. struct nfs_lock_context *l_ctx;
  496. ssize_t result = -EINVAL, requested;
  497. size_t count = iov_iter_count(iter);
  498. nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
  499. dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
  500. file, count, (long long) iocb->ki_pos);
  501. result = 0;
  502. if (!count)
  503. goto out;
  504. task_io_account_read(count);
  505. result = -ENOMEM;
  506. dreq = nfs_direct_req_alloc();
  507. if (dreq == NULL)
  508. goto out;
  509. dreq->inode = inode;
  510. dreq->bytes_left = dreq->max_count = count;
  511. dreq->io_start = iocb->ki_pos;
  512. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  513. l_ctx = nfs_get_lock_context(dreq->ctx);
  514. if (IS_ERR(l_ctx)) {
  515. result = PTR_ERR(l_ctx);
  516. nfs_direct_req_release(dreq);
  517. goto out_release;
  518. }
  519. dreq->l_ctx = l_ctx;
  520. if (!is_sync_kiocb(iocb))
  521. dreq->iocb = iocb;
  522. if (iter_is_iovec(iter))
  523. dreq->flags = NFS_ODIRECT_SHOULD_DIRTY;
  524. nfs_start_io_direct(inode);
  525. NFS_I(inode)->read_io += count;
  526. requested = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos);
  527. nfs_end_io_direct(inode);
  528. if (requested > 0) {
  529. result = nfs_direct_wait(dreq);
  530. if (result > 0) {
  531. requested -= result;
  532. iocb->ki_pos += result;
  533. }
  534. iov_iter_revert(iter, requested);
  535. } else {
  536. result = requested;
  537. }
  538. out_release:
  539. nfs_direct_req_release(dreq);
  540. out:
  541. return result;
  542. }
  543. static void
  544. nfs_direct_write_scan_commit_list(struct inode *inode,
  545. struct list_head *list,
  546. struct nfs_commit_info *cinfo)
  547. {
  548. mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  549. #ifdef CONFIG_NFS_V4_1
  550. if (cinfo->ds != NULL && cinfo->ds->nwritten != 0)
  551. NFS_SERVER(inode)->pnfs_curr_ld->recover_commit_reqs(list, cinfo);
  552. #endif
  553. nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0);
  554. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  555. }
  556. static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
  557. {
  558. struct nfs_pageio_descriptor desc;
  559. struct nfs_page *req, *tmp;
  560. LIST_HEAD(reqs);
  561. struct nfs_commit_info cinfo;
  562. LIST_HEAD(failed);
  563. int i;
  564. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  565. nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
  566. dreq->count = 0;
  567. dreq->max_count = 0;
  568. list_for_each_entry(req, &reqs, wb_list)
  569. dreq->max_count += req->wb_bytes;
  570. dreq->verf.committed = NFS_INVALID_STABLE_HOW;
  571. nfs_clear_pnfs_ds_commit_verifiers(&dreq->ds_cinfo);
  572. for (i = 0; i < dreq->mirror_count; i++)
  573. dreq->mirrors[i].count = 0;
  574. get_dreq(dreq);
  575. nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
  576. &nfs_direct_write_completion_ops);
  577. desc.pg_dreq = dreq;
  578. req = nfs_list_entry(reqs.next);
  579. nfs_direct_setup_mirroring(dreq, &desc, req);
  580. if (desc.pg_error < 0) {
  581. list_splice_init(&reqs, &failed);
  582. goto out_failed;
  583. }
  584. list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
  585. if (!nfs_pageio_add_request(&desc, req)) {
  586. nfs_list_move_request(req, &failed);
  587. spin_lock(&cinfo.inode->i_lock);
  588. dreq->flags = 0;
  589. if (desc.pg_error < 0)
  590. dreq->error = desc.pg_error;
  591. else
  592. dreq->error = -EIO;
  593. spin_unlock(&cinfo.inode->i_lock);
  594. }
  595. nfs_release_request(req);
  596. }
  597. nfs_pageio_complete(&desc);
  598. out_failed:
  599. while (!list_empty(&failed)) {
  600. req = nfs_list_entry(failed.next);
  601. nfs_list_remove_request(req);
  602. nfs_unlock_and_release_request(req);
  603. }
  604. if (put_dreq(dreq))
  605. nfs_direct_write_complete(dreq);
  606. }
  607. static void nfs_direct_commit_complete(struct nfs_commit_data *data)
  608. {
  609. struct nfs_direct_req *dreq = data->dreq;
  610. struct nfs_commit_info cinfo;
  611. struct nfs_page *req;
  612. int status = data->task.tk_status;
  613. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  614. if (status < 0 || nfs_direct_cmp_commit_data_verf(dreq, data))
  615. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  616. while (!list_empty(&data->pages)) {
  617. req = nfs_list_entry(data->pages.next);
  618. nfs_list_remove_request(req);
  619. if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
  620. /* Note the rewrite will go through mds */
  621. nfs_mark_request_commit(req, NULL, &cinfo, 0);
  622. } else
  623. nfs_release_request(req);
  624. nfs_unlock_and_release_request(req);
  625. }
  626. if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
  627. nfs_direct_write_complete(dreq);
  628. }
  629. static void nfs_direct_resched_write(struct nfs_commit_info *cinfo,
  630. struct nfs_page *req)
  631. {
  632. struct nfs_direct_req *dreq = cinfo->dreq;
  633. spin_lock(&dreq->lock);
  634. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  635. spin_unlock(&dreq->lock);
  636. nfs_mark_request_commit(req, NULL, cinfo, 0);
  637. }
  638. static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
  639. .completion = nfs_direct_commit_complete,
  640. .resched_write = nfs_direct_resched_write,
  641. };
  642. static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
  643. {
  644. int res;
  645. struct nfs_commit_info cinfo;
  646. LIST_HEAD(mds_list);
  647. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  648. nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
  649. res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
  650. if (res < 0) /* res == -ENOMEM */
  651. nfs_direct_write_reschedule(dreq);
  652. }
  653. static void nfs_direct_write_schedule_work(struct work_struct *work)
  654. {
  655. struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
  656. int flags = dreq->flags;
  657. dreq->flags = 0;
  658. switch (flags) {
  659. case NFS_ODIRECT_DO_COMMIT:
  660. nfs_direct_commit_schedule(dreq);
  661. break;
  662. case NFS_ODIRECT_RESCHED_WRITES:
  663. nfs_direct_write_reschedule(dreq);
  664. break;
  665. default:
  666. nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping);
  667. nfs_direct_complete(dreq);
  668. }
  669. }
  670. static void nfs_direct_write_complete(struct nfs_direct_req *dreq)
  671. {
  672. queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */
  673. }
  674. static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
  675. {
  676. struct nfs_direct_req *dreq = hdr->dreq;
  677. struct nfs_commit_info cinfo;
  678. bool request_commit = false;
  679. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  680. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  681. spin_lock(&dreq->lock);
  682. if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  683. spin_unlock(&dreq->lock);
  684. goto out_put;
  685. }
  686. nfs_direct_count_bytes(dreq, hdr);
  687. if (hdr->good_bytes != 0) {
  688. if (nfs_write_need_commit(hdr)) {
  689. if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
  690. request_commit = true;
  691. else if (dreq->flags == 0) {
  692. nfs_direct_set_hdr_verf(dreq, hdr);
  693. request_commit = true;
  694. dreq->flags = NFS_ODIRECT_DO_COMMIT;
  695. } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
  696. request_commit = true;
  697. if (nfs_direct_set_or_cmp_hdr_verf(dreq, hdr))
  698. dreq->flags =
  699. NFS_ODIRECT_RESCHED_WRITES;
  700. }
  701. }
  702. }
  703. spin_unlock(&dreq->lock);
  704. while (!list_empty(&hdr->pages)) {
  705. req = nfs_list_entry(hdr->pages.next);
  706. nfs_list_remove_request(req);
  707. if (request_commit) {
  708. kref_get(&req->wb_kref);
  709. nfs_mark_request_commit(req, hdr->lseg, &cinfo,
  710. hdr->ds_commit_idx);
  711. }
  712. nfs_unlock_and_release_request(req);
  713. }
  714. out_put:
  715. if (put_dreq(dreq))
  716. nfs_direct_write_complete(dreq);
  717. hdr->release(hdr);
  718. }
  719. static void nfs_write_sync_pgio_error(struct list_head *head, int error)
  720. {
  721. struct nfs_page *req;
  722. while (!list_empty(head)) {
  723. req = nfs_list_entry(head->next);
  724. nfs_list_remove_request(req);
  725. nfs_unlock_and_release_request(req);
  726. }
  727. }
  728. static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
  729. {
  730. struct nfs_direct_req *dreq = hdr->dreq;
  731. spin_lock(&dreq->lock);
  732. if (dreq->error == 0) {
  733. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  734. /* fake unstable write to let common nfs resend pages */
  735. hdr->verf.committed = NFS_UNSTABLE;
  736. hdr->good_bytes = hdr->args.count;
  737. }
  738. spin_unlock(&dreq->lock);
  739. }
  740. static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
  741. .error_cleanup = nfs_write_sync_pgio_error,
  742. .init_hdr = nfs_direct_pgio_init,
  743. .completion = nfs_direct_write_completion,
  744. .reschedule_io = nfs_direct_write_reschedule_io,
  745. };
  746. /*
  747. * NB: Return the value of the first error return code. Subsequent
  748. * errors after the first one are ignored.
  749. */
  750. /*
  751. * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
  752. * operation. If nfs_writedata_alloc() or get_user_pages() fails,
  753. * bail and stop sending more writes. Write length accounting is
  754. * handled automatically by nfs_direct_write_result(). Otherwise, if
  755. * no requests have been sent, just return an error.
  756. */
  757. static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
  758. struct iov_iter *iter,
  759. loff_t pos)
  760. {
  761. struct nfs_pageio_descriptor desc;
  762. struct inode *inode = dreq->inode;
  763. ssize_t result = 0;
  764. size_t requested_bytes = 0;
  765. size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE);
  766. nfs_pageio_init_write(&desc, inode, FLUSH_COND_STABLE, false,
  767. &nfs_direct_write_completion_ops);
  768. desc.pg_dreq = dreq;
  769. get_dreq(dreq);
  770. inode_dio_begin(inode);
  771. NFS_I(inode)->write_io += iov_iter_count(iter);
  772. while (iov_iter_count(iter)) {
  773. struct page **pagevec;
  774. size_t bytes;
  775. size_t pgbase;
  776. unsigned npages, i;
  777. result = iov_iter_get_pages_alloc(iter, &pagevec,
  778. wsize, &pgbase);
  779. if (result < 0)
  780. break;
  781. bytes = result;
  782. iov_iter_advance(iter, bytes);
  783. npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
  784. for (i = 0; i < npages; i++) {
  785. struct nfs_page *req;
  786. unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
  787. req = nfs_create_request(dreq->ctx, pagevec[i], NULL,
  788. pgbase, req_len);
  789. if (IS_ERR(req)) {
  790. result = PTR_ERR(req);
  791. break;
  792. }
  793. nfs_direct_setup_mirroring(dreq, &desc, req);
  794. if (desc.pg_error < 0) {
  795. nfs_free_request(req);
  796. result = desc.pg_error;
  797. break;
  798. }
  799. nfs_lock_request(req);
  800. req->wb_index = pos >> PAGE_SHIFT;
  801. req->wb_offset = pos & ~PAGE_MASK;
  802. if (!nfs_pageio_add_request(&desc, req)) {
  803. result = desc.pg_error;
  804. nfs_unlock_and_release_request(req);
  805. break;
  806. }
  807. pgbase = 0;
  808. bytes -= req_len;
  809. requested_bytes += req_len;
  810. pos += req_len;
  811. dreq->bytes_left -= req_len;
  812. }
  813. nfs_direct_release_pages(pagevec, npages);
  814. kvfree(pagevec);
  815. if (result < 0)
  816. break;
  817. }
  818. nfs_pageio_complete(&desc);
  819. /*
  820. * If no bytes were started, return the error, and let the
  821. * generic layer handle the completion.
  822. */
  823. if (requested_bytes == 0) {
  824. inode_dio_end(inode);
  825. nfs_direct_req_release(dreq);
  826. return result < 0 ? result : -EIO;
  827. }
  828. if (put_dreq(dreq))
  829. nfs_direct_write_complete(dreq);
  830. return requested_bytes;
  831. }
  832. /**
  833. * nfs_file_direct_write - file direct write operation for NFS files
  834. * @iocb: target I/O control block
  835. * @iter: vector of user buffers from which to write data
  836. *
  837. * We use this function for direct writes instead of calling
  838. * generic_file_aio_write() in order to avoid taking the inode
  839. * semaphore and updating the i_size. The NFS server will set
  840. * the new i_size and this client must read the updated size
  841. * back into its cache. We let the server do generic write
  842. * parameter checking and report problems.
  843. *
  844. * We eliminate local atime updates, see direct read above.
  845. *
  846. * We avoid unnecessary page cache invalidations for normal cached
  847. * readers of this file.
  848. *
  849. * Note that O_APPEND is not supported for NFS direct writes, as there
  850. * is no atomic O_APPEND write facility in the NFS protocol.
  851. */
  852. ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter)
  853. {
  854. ssize_t result = -EINVAL, requested;
  855. size_t count;
  856. struct file *file = iocb->ki_filp;
  857. struct address_space *mapping = file->f_mapping;
  858. struct inode *inode = mapping->host;
  859. struct nfs_direct_req *dreq;
  860. struct nfs_lock_context *l_ctx;
  861. loff_t pos, end;
  862. dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
  863. file, iov_iter_count(iter), (long long) iocb->ki_pos);
  864. result = generic_write_checks(iocb, iter);
  865. if (result <= 0)
  866. return result;
  867. count = result;
  868. nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
  869. pos = iocb->ki_pos;
  870. end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT;
  871. task_io_account_write(count);
  872. result = -ENOMEM;
  873. dreq = nfs_direct_req_alloc();
  874. if (!dreq)
  875. goto out;
  876. dreq->inode = inode;
  877. dreq->bytes_left = dreq->max_count = count;
  878. dreq->io_start = pos;
  879. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  880. l_ctx = nfs_get_lock_context(dreq->ctx);
  881. if (IS_ERR(l_ctx)) {
  882. result = PTR_ERR(l_ctx);
  883. nfs_direct_req_release(dreq);
  884. goto out_release;
  885. }
  886. dreq->l_ctx = l_ctx;
  887. if (!is_sync_kiocb(iocb))
  888. dreq->iocb = iocb;
  889. nfs_start_io_direct(inode);
  890. requested = nfs_direct_write_schedule_iovec(dreq, iter, pos);
  891. if (mapping->nrpages) {
  892. invalidate_inode_pages2_range(mapping,
  893. pos >> PAGE_SHIFT, end);
  894. }
  895. nfs_end_io_direct(inode);
  896. if (requested > 0) {
  897. result = nfs_direct_wait(dreq);
  898. if (result > 0) {
  899. requested -= result;
  900. iocb->ki_pos = pos + result;
  901. /* XXX: should check the generic_write_sync retval */
  902. generic_write_sync(iocb, result);
  903. }
  904. iov_iter_revert(iter, requested);
  905. } else {
  906. result = requested;
  907. }
  908. out_release:
  909. nfs_direct_req_release(dreq);
  910. out:
  911. return result;
  912. }
  913. /**
  914. * nfs_init_directcache - create a slab cache for nfs_direct_req structures
  915. *
  916. */
  917. int __init nfs_init_directcache(void)
  918. {
  919. nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
  920. sizeof(struct nfs_direct_req),
  921. 0, (SLAB_RECLAIM_ACCOUNT|
  922. SLAB_MEM_SPREAD),
  923. NULL);
  924. if (nfs_direct_cachep == NULL)
  925. return -ENOMEM;
  926. return 0;
  927. }
  928. /**
  929. * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
  930. *
  931. */
  932. void nfs_destroy_directcache(void)
  933. {
  934. kmem_cache_destroy(nfs_direct_cachep);
  935. }