direct.c 29 KB

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