direct.c 28 KB

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