direct.c 28 KB

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