direct.c 27 KB

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