direct.c 28 KB

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