direct.c 30 KB

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