direct.c 28 KB

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