direct.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/nfs/direct.c
  4. *
  5. * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
  6. *
  7. * High-performance uncached I/O for the Linux NFS client
  8. *
  9. * There are important applications whose performance or correctness
  10. * depends on uncached access to file data. Database clusters
  11. * (multiple copies of the same instance running on separate hosts)
  12. * implement their own cache coherency protocol that subsumes file
  13. * system cache protocols. Applications that process datasets
  14. * considerably larger than the client's memory do not always benefit
  15. * from a local cache. A streaming video server, for instance, has no
  16. * need to cache the contents of a file.
  17. *
  18. * When an application requests uncached I/O, all read and write requests
  19. * are made directly to the server; data stored or fetched via these
  20. * requests is not cached in the Linux page cache. The client does not
  21. * correct unaligned requests from applications. All requested bytes are
  22. * held on permanent storage before a direct write system call returns to
  23. * an application.
  24. *
  25. * Solaris implements an uncached I/O facility called directio() that
  26. * is used for backups and sequential I/O to very large files. Solaris
  27. * also supports uncaching whole NFS partitions with "-o forcedirectio,"
  28. * an undocumented mount option.
  29. *
  30. * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
  31. * help from Andrew Morton.
  32. *
  33. * 18 Dec 2001 Initial implementation for 2.4 --cel
  34. * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy
  35. * 08 Jun 2003 Port to 2.5 APIs --cel
  36. * 31 Mar 2004 Handle direct I/O without VFS support --cel
  37. * 15 Sep 2004 Parallel async reads --cel
  38. * 04 May 2005 support O_DIRECT with aio --cel
  39. *
  40. */
  41. #include <linux/errno.h>
  42. #include <linux/sched.h>
  43. #include <linux/kernel.h>
  44. #include <linux/file.h>
  45. #include <linux/pagemap.h>
  46. #include <linux/kref.h>
  47. #include <linux/slab.h>
  48. #include <linux/task_io_accounting_ops.h>
  49. #include <linux/module.h>
  50. #include <linux/nfs_fs.h>
  51. #include <linux/nfs_page.h>
  52. #include <linux/sunrpc/clnt.h>
  53. #include <linux/uaccess.h>
  54. #include <linux/atomic.h>
  55. #include "delegation.h"
  56. #include "internal.h"
  57. #include "iostat.h"
  58. #include "pnfs.h"
  59. #include "fscache.h"
  60. #include "nfstrace.h"
  61. #define NFSDBG_FACILITY NFSDBG_VFS
  62. static struct kmem_cache *nfs_direct_cachep;
  63. static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
  64. static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
  65. static void nfs_direct_write_complete(struct nfs_direct_req *dreq);
  66. static void nfs_direct_write_schedule_work(struct work_struct *work);
  67. static inline void get_dreq(struct nfs_direct_req *dreq)
  68. {
  69. atomic_inc(&dreq->io_count);
  70. }
  71. static inline int put_dreq(struct nfs_direct_req *dreq)
  72. {
  73. return atomic_dec_and_test(&dreq->io_count);
  74. }
  75. static void
  76. nfs_direct_handle_truncated(struct nfs_direct_req *dreq,
  77. const struct nfs_pgio_header *hdr,
  78. ssize_t dreq_len)
  79. {
  80. if (!(test_bit(NFS_IOHDR_ERROR, &hdr->flags) ||
  81. test_bit(NFS_IOHDR_EOF, &hdr->flags)))
  82. return;
  83. if (dreq->max_count >= dreq_len) {
  84. dreq->max_count = dreq_len;
  85. if (dreq->count > dreq_len)
  86. dreq->count = dreq_len;
  87. }
  88. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && !dreq->error)
  89. dreq->error = hdr->error;
  90. }
  91. static void
  92. nfs_direct_count_bytes(struct nfs_direct_req *dreq,
  93. const struct nfs_pgio_header *hdr)
  94. {
  95. loff_t hdr_end = hdr->io_start + hdr->good_bytes;
  96. ssize_t dreq_len = 0;
  97. if (hdr_end > dreq->io_start)
  98. dreq_len = hdr_end - dreq->io_start;
  99. nfs_direct_handle_truncated(dreq, hdr, dreq_len);
  100. if (dreq_len > dreq->max_count)
  101. dreq_len = dreq->max_count;
  102. if (dreq->count < dreq_len)
  103. dreq->count = dreq_len;
  104. }
  105. static void nfs_direct_truncate_request(struct nfs_direct_req *dreq,
  106. struct nfs_page *req)
  107. {
  108. loff_t offs = req_offset(req);
  109. size_t req_start = (size_t)(offs - dreq->io_start);
  110. if (req_start < dreq->max_count)
  111. dreq->max_count = req_start;
  112. if (req_start < dreq->count)
  113. dreq->count = req_start;
  114. }
  115. static void nfs_direct_file_adjust_size_locked(struct inode *inode,
  116. loff_t offset, size_t count)
  117. {
  118. loff_t newsize = offset + (loff_t)count;
  119. loff_t oldsize = i_size_read(inode);
  120. if (newsize > oldsize) {
  121. i_size_write(inode, newsize);
  122. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
  123. trace_nfs_size_grow(inode, newsize);
  124. nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
  125. }
  126. }
  127. /**
  128. * nfs_swap_rw - NFS address space operation for swap I/O
  129. * @iocb: target I/O control block
  130. * @iter: I/O buffer
  131. *
  132. * Perform IO to the swap-file. This is much like direct IO.
  133. */
  134. int nfs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
  135. {
  136. ssize_t ret;
  137. if (iov_iter_rw(iter) == READ)
  138. ret = nfs_file_direct_read(iocb, iter, true);
  139. else
  140. ret = nfs_file_direct_write(iocb, iter, true);
  141. if (ret < 0)
  142. return ret;
  143. return 0;
  144. }
  145. static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
  146. {
  147. unsigned int i;
  148. for (i = 0; i < npages; i++)
  149. put_page(pages[i]);
  150. }
  151. void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
  152. struct nfs_direct_req *dreq)
  153. {
  154. cinfo->inode = dreq->inode;
  155. cinfo->mds = &dreq->mds_cinfo;
  156. cinfo->ds = &dreq->ds_cinfo;
  157. cinfo->dreq = dreq;
  158. cinfo->completion_ops = &nfs_direct_commit_completion_ops;
  159. }
  160. static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
  161. {
  162. struct nfs_direct_req *dreq;
  163. dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
  164. if (!dreq)
  165. return NULL;
  166. kref_init(&dreq->kref);
  167. kref_get(&dreq->kref);
  168. init_completion(&dreq->completion);
  169. INIT_LIST_HEAD(&dreq->mds_cinfo.list);
  170. pnfs_init_ds_commit_info(&dreq->ds_cinfo);
  171. INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
  172. spin_lock_init(&dreq->lock);
  173. return dreq;
  174. }
  175. static void nfs_direct_req_free(struct kref *kref)
  176. {
  177. struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
  178. pnfs_release_ds_info(&dreq->ds_cinfo, dreq->inode);
  179. if (dreq->l_ctx != NULL)
  180. nfs_put_lock_context(dreq->l_ctx);
  181. if (dreq->ctx != NULL)
  182. put_nfs_open_context(dreq->ctx);
  183. kmem_cache_free(nfs_direct_cachep, dreq);
  184. }
  185. static void nfs_direct_req_release(struct nfs_direct_req *dreq)
  186. {
  187. kref_put(&dreq->kref, nfs_direct_req_free);
  188. }
  189. ssize_t nfs_dreq_bytes_left(struct nfs_direct_req *dreq, loff_t offset)
  190. {
  191. loff_t start = offset - dreq->io_start;
  192. return dreq->max_count - start;
  193. }
  194. EXPORT_SYMBOL_GPL(nfs_dreq_bytes_left);
  195. /*
  196. * Collects and returns the final error value/byte-count.
  197. */
  198. static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
  199. {
  200. ssize_t result = -EIOCBQUEUED;
  201. /* Async requests don't wait here */
  202. if (dreq->iocb)
  203. goto out;
  204. result = wait_for_completion_killable(&dreq->completion);
  205. if (!result) {
  206. result = dreq->count;
  207. WARN_ON_ONCE(dreq->count < 0);
  208. }
  209. if (!result)
  210. result = dreq->error;
  211. out:
  212. return (ssize_t) result;
  213. }
  214. /*
  215. * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
  216. * the iocb is still valid here if this is a synchronous request.
  217. */
  218. static void nfs_direct_complete(struct nfs_direct_req *dreq)
  219. {
  220. struct inode *inode = dreq->inode;
  221. inode_dio_end(inode);
  222. if (dreq->iocb) {
  223. long res = (long) dreq->error;
  224. if (dreq->count != 0) {
  225. res = (long) dreq->count;
  226. WARN_ON_ONCE(dreq->count < 0);
  227. }
  228. dreq->iocb->ki_complete(dreq->iocb, res);
  229. }
  230. complete(&dreq->completion);
  231. nfs_direct_req_release(dreq);
  232. }
  233. static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
  234. {
  235. unsigned long bytes = 0;
  236. struct nfs_direct_req *dreq = hdr->dreq;
  237. spin_lock(&dreq->lock);
  238. if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  239. spin_unlock(&dreq->lock);
  240. goto out_put;
  241. }
  242. nfs_direct_count_bytes(dreq, hdr);
  243. spin_unlock(&dreq->lock);
  244. nfs_update_delegated_atime(dreq->inode);
  245. while (!list_empty(&hdr->pages)) {
  246. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  247. struct page *page = req->wb_page;
  248. if (!PageCompound(page) && bytes < hdr->good_bytes &&
  249. (dreq->flags == NFS_ODIRECT_SHOULD_DIRTY))
  250. set_page_dirty(page);
  251. bytes += req->wb_bytes;
  252. nfs_list_remove_request(req);
  253. nfs_release_request(req);
  254. }
  255. out_put:
  256. if (put_dreq(dreq))
  257. nfs_direct_complete(dreq);
  258. hdr->release(hdr);
  259. }
  260. static void nfs_read_sync_pgio_error(struct list_head *head, int error)
  261. {
  262. struct nfs_page *req;
  263. while (!list_empty(head)) {
  264. req = nfs_list_entry(head->next);
  265. nfs_list_remove_request(req);
  266. nfs_release_request(req);
  267. }
  268. }
  269. static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
  270. {
  271. get_dreq(hdr->dreq);
  272. set_bit(NFS_IOHDR_ODIRECT, &hdr->flags);
  273. }
  274. static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
  275. .error_cleanup = nfs_read_sync_pgio_error,
  276. .init_hdr = nfs_direct_pgio_init,
  277. .completion = nfs_direct_read_completion,
  278. };
  279. /*
  280. * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
  281. * operation. If nfs_readdata_alloc() or get_user_pages() fails,
  282. * bail and stop sending more reads. Read length accounting is
  283. * handled automatically by nfs_direct_read_result(). Otherwise, if
  284. * no requests have been sent, just return an error.
  285. */
  286. static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
  287. struct iov_iter *iter,
  288. loff_t pos)
  289. {
  290. struct nfs_pageio_descriptor desc;
  291. struct inode *inode = dreq->inode;
  292. ssize_t result = -EINVAL;
  293. size_t requested_bytes = 0;
  294. size_t rsize = max_t(size_t, NFS_SERVER(inode)->rsize, PAGE_SIZE);
  295. nfs_pageio_init_read(&desc, dreq->inode, false,
  296. &nfs_direct_read_completion_ops);
  297. get_dreq(dreq);
  298. desc.pg_dreq = dreq;
  299. inode_dio_begin(inode);
  300. while (iov_iter_count(iter)) {
  301. struct page **pagevec;
  302. size_t bytes;
  303. size_t pgbase;
  304. unsigned npages, i;
  305. result = iov_iter_get_pages_alloc2(iter, &pagevec,
  306. rsize, &pgbase);
  307. if (result < 0)
  308. break;
  309. bytes = result;
  310. npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
  311. for (i = 0; i < npages; i++) {
  312. struct nfs_page *req;
  313. unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
  314. /* XXX do we need to do the eof zeroing found in async_filler? */
  315. req = nfs_page_create_from_page(dreq->ctx, pagevec[i],
  316. pgbase, pos, req_len);
  317. if (IS_ERR(req)) {
  318. result = PTR_ERR(req);
  319. break;
  320. }
  321. if (!nfs_pageio_add_request(&desc, req)) {
  322. result = desc.pg_error;
  323. nfs_release_request(req);
  324. break;
  325. }
  326. pgbase = 0;
  327. bytes -= req_len;
  328. requested_bytes += req_len;
  329. pos += req_len;
  330. }
  331. nfs_direct_release_pages(pagevec, npages);
  332. kvfree(pagevec);
  333. if (result < 0)
  334. break;
  335. }
  336. nfs_pageio_complete(&desc);
  337. /*
  338. * If no bytes were started, return the error, and let the
  339. * generic layer handle the completion.
  340. */
  341. if (requested_bytes == 0) {
  342. inode_dio_end(inode);
  343. nfs_direct_req_release(dreq);
  344. return result < 0 ? result : -EIO;
  345. }
  346. if (put_dreq(dreq))
  347. nfs_direct_complete(dreq);
  348. return requested_bytes;
  349. }
  350. /**
  351. * nfs_file_direct_read - file direct read operation for NFS files
  352. * @iocb: target I/O control block
  353. * @iter: vector of user buffers into which to read data
  354. * @swap: flag indicating this is swap IO, not O_DIRECT IO
  355. *
  356. * We use this function for direct reads instead of calling
  357. * generic_file_aio_read() in order to avoid gfar's check to see if
  358. * the request starts before the end of the file. For that check
  359. * to work, we must generate a GETATTR before each direct read, and
  360. * even then there is a window between the GETATTR and the subsequent
  361. * READ where the file size could change. Our preference is simply
  362. * to do all reads the application wants, and the server will take
  363. * care of managing the end of file boundary.
  364. *
  365. * This function also eliminates unnecessarily updating the file's
  366. * atime locally, as the NFS server sets the file's atime, and this
  367. * client must read the updated atime from the server back into its
  368. * cache.
  369. */
  370. ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter,
  371. bool swap)
  372. {
  373. struct file *file = iocb->ki_filp;
  374. struct address_space *mapping = file->f_mapping;
  375. struct inode *inode = mapping->host;
  376. struct nfs_direct_req *dreq;
  377. struct nfs_lock_context *l_ctx;
  378. ssize_t result, requested;
  379. size_t count = iov_iter_count(iter);
  380. nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
  381. dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
  382. file, count, (long long) iocb->ki_pos);
  383. result = 0;
  384. if (!count)
  385. goto out;
  386. task_io_account_read(count);
  387. result = -ENOMEM;
  388. dreq = nfs_direct_req_alloc();
  389. if (dreq == NULL)
  390. goto out;
  391. dreq->inode = inode;
  392. dreq->max_count = count;
  393. dreq->io_start = iocb->ki_pos;
  394. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  395. l_ctx = nfs_get_lock_context(dreq->ctx);
  396. if (IS_ERR(l_ctx)) {
  397. result = PTR_ERR(l_ctx);
  398. nfs_direct_req_release(dreq);
  399. goto out_release;
  400. }
  401. dreq->l_ctx = l_ctx;
  402. if (!is_sync_kiocb(iocb))
  403. dreq->iocb = iocb;
  404. if (user_backed_iter(iter))
  405. dreq->flags = NFS_ODIRECT_SHOULD_DIRTY;
  406. if (!swap) {
  407. result = nfs_start_io_direct(inode);
  408. if (result) {
  409. /* release the reference that would usually be
  410. * consumed by nfs_direct_read_schedule_iovec()
  411. */
  412. nfs_direct_req_release(dreq);
  413. goto out_release;
  414. }
  415. }
  416. NFS_I(inode)->read_io += count;
  417. requested = nfs_direct_read_schedule_iovec(dreq, iter, iocb->ki_pos);
  418. if (!swap)
  419. nfs_end_io_direct(inode);
  420. if (requested > 0) {
  421. result = nfs_direct_wait(dreq);
  422. if (result > 0) {
  423. requested -= result;
  424. iocb->ki_pos += result;
  425. }
  426. iov_iter_revert(iter, requested);
  427. } else {
  428. result = requested;
  429. }
  430. out_release:
  431. nfs_direct_req_release(dreq);
  432. out:
  433. return result;
  434. }
  435. static void nfs_direct_add_page_head(struct list_head *list,
  436. struct nfs_page *req)
  437. {
  438. struct nfs_page *head = req->wb_head;
  439. if (!list_empty(&head->wb_list) || !nfs_lock_request(head))
  440. return;
  441. if (!list_empty(&head->wb_list)) {
  442. nfs_unlock_request(head);
  443. return;
  444. }
  445. list_add(&head->wb_list, list);
  446. kref_get(&head->wb_kref);
  447. kref_get(&head->wb_kref);
  448. }
  449. static void nfs_direct_join_group(struct list_head *list,
  450. struct nfs_commit_info *cinfo,
  451. struct inode *inode)
  452. {
  453. struct nfs_page *req, *subreq;
  454. list_for_each_entry(req, list, wb_list) {
  455. if (req->wb_head != req) {
  456. nfs_direct_add_page_head(&req->wb_list, req);
  457. continue;
  458. }
  459. subreq = req->wb_this_page;
  460. if (subreq == req)
  461. continue;
  462. do {
  463. /*
  464. * Remove subrequests from this list before freeing
  465. * them in the call to nfs_join_page_group().
  466. */
  467. if (!list_empty(&subreq->wb_list)) {
  468. nfs_list_remove_request(subreq);
  469. nfs_release_request(subreq);
  470. }
  471. } while ((subreq = subreq->wb_this_page) != req);
  472. nfs_join_page_group(req, cinfo, inode);
  473. }
  474. }
  475. static void
  476. nfs_direct_write_scan_commit_list(struct inode *inode,
  477. struct list_head *list,
  478. struct nfs_commit_info *cinfo)
  479. {
  480. mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  481. pnfs_recover_commit_reqs(list, cinfo);
  482. nfs_scan_commit_list(&cinfo->mds->list, list, cinfo, 0);
  483. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  484. }
  485. static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
  486. {
  487. struct nfs_pageio_descriptor desc;
  488. struct nfs_page *req;
  489. LIST_HEAD(reqs);
  490. struct nfs_commit_info cinfo;
  491. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  492. nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
  493. nfs_direct_join_group(&reqs, &cinfo, dreq->inode);
  494. nfs_clear_pnfs_ds_commit_verifiers(&dreq->ds_cinfo);
  495. get_dreq(dreq);
  496. nfs_pageio_init_write(&desc, dreq->inode, FLUSH_STABLE, false,
  497. &nfs_direct_write_completion_ops);
  498. desc.pg_dreq = dreq;
  499. while (!list_empty(&reqs)) {
  500. req = nfs_list_entry(reqs.next);
  501. /* Bump the transmission count */
  502. req->wb_nio++;
  503. if (!nfs_pageio_add_request(&desc, req)) {
  504. spin_lock(&dreq->lock);
  505. if (dreq->error < 0) {
  506. desc.pg_error = dreq->error;
  507. } else if (desc.pg_error != -EAGAIN) {
  508. dreq->flags = 0;
  509. if (!desc.pg_error)
  510. desc.pg_error = -EIO;
  511. dreq->error = desc.pg_error;
  512. } else
  513. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  514. spin_unlock(&dreq->lock);
  515. break;
  516. }
  517. nfs_release_request(req);
  518. }
  519. nfs_pageio_complete(&desc);
  520. while (!list_empty(&reqs)) {
  521. req = nfs_list_entry(reqs.next);
  522. nfs_list_remove_request(req);
  523. nfs_unlock_and_release_request(req);
  524. if (desc.pg_error == -EAGAIN) {
  525. nfs_mark_request_commit(req, NULL, &cinfo, 0);
  526. } else {
  527. spin_lock(&dreq->lock);
  528. nfs_direct_truncate_request(dreq, req);
  529. spin_unlock(&dreq->lock);
  530. nfs_release_request(req);
  531. }
  532. }
  533. if (put_dreq(dreq))
  534. nfs_direct_write_complete(dreq);
  535. }
  536. static void nfs_direct_commit_complete(struct nfs_commit_data *data)
  537. {
  538. const struct nfs_writeverf *verf = data->res.verf;
  539. struct nfs_direct_req *dreq = data->dreq;
  540. struct nfs_commit_info cinfo;
  541. struct nfs_page *req;
  542. int status = data->task.tk_status;
  543. trace_nfs_direct_commit_complete(dreq);
  544. spin_lock(&dreq->lock);
  545. if (status < 0) {
  546. /* Errors in commit are fatal */
  547. dreq->error = status;
  548. dreq->flags = NFS_ODIRECT_DONE;
  549. } else {
  550. status = dreq->error;
  551. }
  552. spin_unlock(&dreq->lock);
  553. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  554. while (!list_empty(&data->pages)) {
  555. req = nfs_list_entry(data->pages.next);
  556. nfs_list_remove_request(req);
  557. if (status < 0) {
  558. spin_lock(&dreq->lock);
  559. nfs_direct_truncate_request(dreq, req);
  560. spin_unlock(&dreq->lock);
  561. nfs_release_request(req);
  562. } else if (!nfs_write_match_verf(verf, req)) {
  563. spin_lock(&dreq->lock);
  564. if (dreq->flags == 0)
  565. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  566. spin_unlock(&dreq->lock);
  567. /*
  568. * Despite the reboot, the write was successful,
  569. * so reset wb_nio.
  570. */
  571. req->wb_nio = 0;
  572. nfs_mark_request_commit(req, NULL, &cinfo, 0);
  573. } else
  574. nfs_release_request(req);
  575. nfs_unlock_and_release_request(req);
  576. }
  577. if (nfs_commit_end(cinfo.mds))
  578. nfs_direct_write_complete(dreq);
  579. }
  580. static void nfs_direct_resched_write(struct nfs_commit_info *cinfo,
  581. struct nfs_page *req)
  582. {
  583. struct nfs_direct_req *dreq = cinfo->dreq;
  584. trace_nfs_direct_resched_write(dreq);
  585. spin_lock(&dreq->lock);
  586. if (dreq->flags != NFS_ODIRECT_DONE)
  587. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  588. spin_unlock(&dreq->lock);
  589. nfs_mark_request_commit(req, NULL, cinfo, 0);
  590. }
  591. static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
  592. .completion = nfs_direct_commit_complete,
  593. .resched_write = nfs_direct_resched_write,
  594. };
  595. static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
  596. {
  597. int res;
  598. struct nfs_commit_info cinfo;
  599. LIST_HEAD(mds_list);
  600. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  601. nfs_commit_begin(cinfo.mds);
  602. nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
  603. res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
  604. if (res < 0) { /* res == -ENOMEM */
  605. spin_lock(&dreq->lock);
  606. if (dreq->flags == 0)
  607. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  608. spin_unlock(&dreq->lock);
  609. }
  610. if (nfs_commit_end(cinfo.mds))
  611. nfs_direct_write_complete(dreq);
  612. }
  613. static void nfs_direct_write_clear_reqs(struct nfs_direct_req *dreq)
  614. {
  615. struct nfs_commit_info cinfo;
  616. struct nfs_page *req;
  617. LIST_HEAD(reqs);
  618. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  619. nfs_direct_write_scan_commit_list(dreq->inode, &reqs, &cinfo);
  620. while (!list_empty(&reqs)) {
  621. req = nfs_list_entry(reqs.next);
  622. nfs_list_remove_request(req);
  623. nfs_direct_truncate_request(dreq, req);
  624. nfs_release_request(req);
  625. nfs_unlock_and_release_request(req);
  626. }
  627. }
  628. static void nfs_direct_write_schedule_work(struct work_struct *work)
  629. {
  630. struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
  631. int flags = dreq->flags;
  632. dreq->flags = 0;
  633. switch (flags) {
  634. case NFS_ODIRECT_DO_COMMIT:
  635. nfs_direct_commit_schedule(dreq);
  636. break;
  637. case NFS_ODIRECT_RESCHED_WRITES:
  638. nfs_direct_write_reschedule(dreq);
  639. break;
  640. default:
  641. nfs_direct_write_clear_reqs(dreq);
  642. nfs_zap_mapping(dreq->inode, dreq->inode->i_mapping);
  643. nfs_direct_complete(dreq);
  644. }
  645. }
  646. static void nfs_direct_write_complete(struct nfs_direct_req *dreq)
  647. {
  648. trace_nfs_direct_write_complete(dreq);
  649. queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */
  650. }
  651. static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
  652. {
  653. struct nfs_direct_req *dreq = hdr->dreq;
  654. struct nfs_commit_info cinfo;
  655. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  656. struct inode *inode = dreq->inode;
  657. int flags = NFS_ODIRECT_DONE;
  658. trace_nfs_direct_write_completion(dreq);
  659. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  660. spin_lock(&dreq->lock);
  661. if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) {
  662. spin_unlock(&dreq->lock);
  663. goto out_put;
  664. }
  665. nfs_direct_count_bytes(dreq, hdr);
  666. if (test_bit(NFS_IOHDR_UNSTABLE_WRITES, &hdr->flags) &&
  667. !test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
  668. if (!dreq->flags)
  669. dreq->flags = NFS_ODIRECT_DO_COMMIT;
  670. flags = dreq->flags;
  671. }
  672. spin_unlock(&dreq->lock);
  673. spin_lock(&inode->i_lock);
  674. nfs_direct_file_adjust_size_locked(inode, dreq->io_start, dreq->count);
  675. nfs_update_delegated_mtime_locked(dreq->inode);
  676. spin_unlock(&inode->i_lock);
  677. while (!list_empty(&hdr->pages)) {
  678. req = nfs_list_entry(hdr->pages.next);
  679. nfs_list_remove_request(req);
  680. if (flags == NFS_ODIRECT_DO_COMMIT) {
  681. kref_get(&req->wb_kref);
  682. memcpy(&req->wb_verf, &hdr->verf.verifier,
  683. sizeof(req->wb_verf));
  684. nfs_mark_request_commit(req, hdr->lseg, &cinfo,
  685. hdr->ds_commit_idx);
  686. } else if (flags == NFS_ODIRECT_RESCHED_WRITES) {
  687. kref_get(&req->wb_kref);
  688. nfs_mark_request_commit(req, NULL, &cinfo, 0);
  689. }
  690. nfs_unlock_and_release_request(req);
  691. }
  692. out_put:
  693. if (put_dreq(dreq))
  694. nfs_direct_write_complete(dreq);
  695. hdr->release(hdr);
  696. }
  697. static void nfs_write_sync_pgio_error(struct list_head *head, int error)
  698. {
  699. struct nfs_page *req;
  700. while (!list_empty(head)) {
  701. req = nfs_list_entry(head->next);
  702. nfs_list_remove_request(req);
  703. nfs_unlock_and_release_request(req);
  704. }
  705. }
  706. static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
  707. {
  708. struct nfs_direct_req *dreq = hdr->dreq;
  709. struct nfs_page *req;
  710. struct nfs_commit_info cinfo;
  711. trace_nfs_direct_write_reschedule_io(dreq);
  712. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  713. spin_lock(&dreq->lock);
  714. if (dreq->error == 0)
  715. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  716. set_bit(NFS_IOHDR_REDO, &hdr->flags);
  717. spin_unlock(&dreq->lock);
  718. while (!list_empty(&hdr->pages)) {
  719. req = nfs_list_entry(hdr->pages.next);
  720. nfs_list_remove_request(req);
  721. nfs_unlock_request(req);
  722. nfs_mark_request_commit(req, NULL, &cinfo, 0);
  723. }
  724. }
  725. static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
  726. .error_cleanup = nfs_write_sync_pgio_error,
  727. .init_hdr = nfs_direct_pgio_init,
  728. .completion = nfs_direct_write_completion,
  729. .reschedule_io = nfs_direct_write_reschedule_io,
  730. };
  731. /*
  732. * NB: Return the value of the first error return code. Subsequent
  733. * errors after the first one are ignored.
  734. */
  735. /*
  736. * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
  737. * operation. If nfs_writedata_alloc() or get_user_pages() fails,
  738. * bail and stop sending more writes. Write length accounting is
  739. * handled automatically by nfs_direct_write_result(). Otherwise, if
  740. * no requests have been sent, just return an error.
  741. */
  742. static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
  743. struct iov_iter *iter,
  744. loff_t pos, int ioflags)
  745. {
  746. struct nfs_pageio_descriptor desc;
  747. struct inode *inode = dreq->inode;
  748. struct nfs_commit_info cinfo;
  749. ssize_t result = 0;
  750. size_t requested_bytes = 0;
  751. size_t wsize = max_t(size_t, NFS_SERVER(inode)->wsize, PAGE_SIZE);
  752. bool defer = false;
  753. trace_nfs_direct_write_schedule_iovec(dreq);
  754. nfs_pageio_init_write(&desc, inode, ioflags, false,
  755. &nfs_direct_write_completion_ops);
  756. desc.pg_dreq = dreq;
  757. get_dreq(dreq);
  758. inode_dio_begin(inode);
  759. NFS_I(inode)->write_io += iov_iter_count(iter);
  760. while (iov_iter_count(iter)) {
  761. struct page **pagevec;
  762. size_t bytes;
  763. size_t pgbase;
  764. unsigned npages, i;
  765. result = iov_iter_get_pages_alloc2(iter, &pagevec,
  766. wsize, &pgbase);
  767. if (result < 0)
  768. break;
  769. bytes = result;
  770. npages = (result + pgbase + PAGE_SIZE - 1) / PAGE_SIZE;
  771. for (i = 0; i < npages; i++) {
  772. struct nfs_page *req;
  773. unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
  774. req = nfs_page_create_from_page(dreq->ctx, pagevec[i],
  775. pgbase, pos, req_len);
  776. if (IS_ERR(req)) {
  777. result = PTR_ERR(req);
  778. break;
  779. }
  780. if (desc.pg_error < 0) {
  781. nfs_free_request(req);
  782. result = desc.pg_error;
  783. break;
  784. }
  785. pgbase = 0;
  786. bytes -= req_len;
  787. requested_bytes += req_len;
  788. pos += req_len;
  789. if (defer) {
  790. nfs_mark_request_commit(req, NULL, &cinfo, 0);
  791. continue;
  792. }
  793. nfs_lock_request(req);
  794. if (nfs_pageio_add_request(&desc, req))
  795. continue;
  796. /* Exit on hard errors */
  797. if (desc.pg_error < 0 && desc.pg_error != -EAGAIN) {
  798. result = desc.pg_error;
  799. nfs_unlock_and_release_request(req);
  800. break;
  801. }
  802. /* If the error is soft, defer remaining requests */
  803. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  804. spin_lock(&dreq->lock);
  805. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  806. spin_unlock(&dreq->lock);
  807. nfs_unlock_request(req);
  808. nfs_mark_request_commit(req, NULL, &cinfo, 0);
  809. desc.pg_error = 0;
  810. defer = true;
  811. }
  812. nfs_direct_release_pages(pagevec, npages);
  813. kvfree(pagevec);
  814. if (result < 0)
  815. break;
  816. }
  817. nfs_pageio_complete(&desc);
  818. /*
  819. * If no bytes were started, return the error, and let the
  820. * generic layer handle the completion.
  821. */
  822. if (requested_bytes == 0) {
  823. inode_dio_end(inode);
  824. nfs_direct_req_release(dreq);
  825. return result < 0 ? result : -EIO;
  826. }
  827. if (put_dreq(dreq))
  828. nfs_direct_write_complete(dreq);
  829. return requested_bytes;
  830. }
  831. /**
  832. * nfs_file_direct_write - file direct write operation for NFS files
  833. * @iocb: target I/O control block
  834. * @iter: vector of user buffers from which to write data
  835. * @swap: flag indicating this is swap IO, not O_DIRECT IO
  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. bool swap)
  854. {
  855. ssize_t result, requested;
  856. size_t count;
  857. struct file *file = iocb->ki_filp;
  858. struct address_space *mapping = file->f_mapping;
  859. struct inode *inode = mapping->host;
  860. struct nfs_direct_req *dreq;
  861. struct nfs_lock_context *l_ctx;
  862. loff_t pos, end;
  863. dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
  864. file, iov_iter_count(iter), (long long) iocb->ki_pos);
  865. if (swap)
  866. /* bypass generic checks */
  867. result = iov_iter_count(iter);
  868. else
  869. result = generic_write_checks(iocb, iter);
  870. if (result <= 0)
  871. return result;
  872. count = result;
  873. nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
  874. pos = iocb->ki_pos;
  875. end = (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT;
  876. task_io_account_write(count);
  877. result = -ENOMEM;
  878. dreq = nfs_direct_req_alloc();
  879. if (!dreq)
  880. goto out;
  881. dreq->inode = inode;
  882. dreq->max_count = count;
  883. dreq->io_start = pos;
  884. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  885. l_ctx = nfs_get_lock_context(dreq->ctx);
  886. if (IS_ERR(l_ctx)) {
  887. result = PTR_ERR(l_ctx);
  888. nfs_direct_req_release(dreq);
  889. goto out_release;
  890. }
  891. dreq->l_ctx = l_ctx;
  892. if (!is_sync_kiocb(iocb))
  893. dreq->iocb = iocb;
  894. pnfs_init_ds_commit_info_ops(&dreq->ds_cinfo, inode);
  895. if (swap) {
  896. requested = nfs_direct_write_schedule_iovec(dreq, iter, pos,
  897. FLUSH_STABLE);
  898. } else {
  899. result = nfs_start_io_direct(inode);
  900. if (result) {
  901. /* release the reference that would usually be
  902. * consumed by nfs_direct_write_schedule_iovec()
  903. */
  904. nfs_direct_req_release(dreq);
  905. goto out_release;
  906. }
  907. requested = nfs_direct_write_schedule_iovec(dreq, iter, pos,
  908. FLUSH_COND_STABLE);
  909. if (mapping->nrpages) {
  910. invalidate_inode_pages2_range(mapping,
  911. pos >> PAGE_SHIFT, end);
  912. }
  913. nfs_end_io_direct(inode);
  914. }
  915. if (requested > 0) {
  916. result = nfs_direct_wait(dreq);
  917. if (result > 0) {
  918. requested -= result;
  919. iocb->ki_pos = pos + result;
  920. /* XXX: should check the generic_write_sync retval */
  921. generic_write_sync(iocb, result);
  922. }
  923. iov_iter_revert(iter, requested);
  924. } else {
  925. result = requested;
  926. }
  927. nfs_fscache_invalidate(inode, FSCACHE_INVAL_DIO_WRITE);
  928. out_release:
  929. nfs_direct_req_release(dreq);
  930. out:
  931. return result;
  932. }
  933. /**
  934. * nfs_init_directcache - create a slab cache for nfs_direct_req structures
  935. *
  936. */
  937. int __init nfs_init_directcache(void)
  938. {
  939. nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
  940. sizeof(struct nfs_direct_req),
  941. 0, SLAB_RECLAIM_ACCOUNT,
  942. NULL);
  943. if (nfs_direct_cachep == NULL)
  944. return -ENOMEM;
  945. return 0;
  946. }
  947. /**
  948. * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
  949. *
  950. */
  951. void nfs_destroy_directcache(void)
  952. {
  953. kmem_cache_destroy(nfs_direct_cachep);
  954. }