direct.c 29 KB

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