direct.c 29 KB

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