direct.c 30 KB

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