direct.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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/nfs_fs.h>
  49. #include <linux/nfs_page.h>
  50. #include <linux/sunrpc/clnt.h>
  51. #include <asm/uaccess.h>
  52. #include <linux/atomic.h>
  53. #include "internal.h"
  54. #include "iostat.h"
  55. #include "pnfs.h"
  56. #define NFSDBG_FACILITY NFSDBG_VFS
  57. static struct kmem_cache *nfs_direct_cachep;
  58. /*
  59. * This represents a set of asynchronous requests that we're waiting on
  60. */
  61. struct nfs_direct_req {
  62. struct kref kref; /* release manager */
  63. /* I/O parameters */
  64. struct nfs_open_context *ctx; /* file open context info */
  65. struct nfs_lock_context *l_ctx; /* Lock context info */
  66. struct kiocb * iocb; /* controlling i/o request */
  67. struct inode * inode; /* target file of i/o */
  68. /* completion state */
  69. atomic_t io_count; /* i/os we're waiting for */
  70. spinlock_t lock; /* protect completion state */
  71. ssize_t count, /* bytes actually processed */
  72. error; /* any reported error */
  73. struct completion completion; /* wait for i/o completion */
  74. /* commit state */
  75. struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */
  76. struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */
  77. struct work_struct work;
  78. int flags;
  79. #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
  80. #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
  81. struct nfs_writeverf verf; /* unstable write verifier */
  82. };
  83. static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
  84. static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
  85. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
  86. static void nfs_direct_write_schedule_work(struct work_struct *work);
  87. static inline void get_dreq(struct nfs_direct_req *dreq)
  88. {
  89. atomic_inc(&dreq->io_count);
  90. }
  91. static inline int put_dreq(struct nfs_direct_req *dreq)
  92. {
  93. return atomic_dec_and_test(&dreq->io_count);
  94. }
  95. /**
  96. * nfs_direct_IO - NFS address space operation for direct I/O
  97. * @rw: direction (read or write)
  98. * @iocb: target I/O control block
  99. * @iov: array of vectors that define I/O buffer
  100. * @pos: offset in file to begin the operation
  101. * @nr_segs: size of iovec array
  102. *
  103. * The presence of this routine in the address space ops vector means
  104. * the NFS client supports direct I/O. However, for most direct IO, we
  105. * shunt off direct read and write requests before the VFS gets them,
  106. * so this method is only ever called for swap.
  107. */
  108. ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
  109. {
  110. #ifndef CONFIG_NFS_SWAP
  111. dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
  112. iocb->ki_filp->f_path.dentry->d_name.name,
  113. (long long) pos, nr_segs);
  114. return -EINVAL;
  115. #else
  116. VM_BUG_ON(iocb->ki_left != PAGE_SIZE);
  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. /*
  168. * Collects and returns the final error value/byte-count.
  169. */
  170. static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
  171. {
  172. ssize_t result = -EIOCBQUEUED;
  173. /* Async requests don't wait here */
  174. if (dreq->iocb)
  175. goto out;
  176. result = wait_for_completion_killable(&dreq->completion);
  177. if (!result)
  178. result = dreq->error;
  179. if (!result)
  180. result = dreq->count;
  181. out:
  182. return (ssize_t) result;
  183. }
  184. /*
  185. * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
  186. * the iocb is still valid here if this is a synchronous request.
  187. */
  188. static void nfs_direct_complete(struct nfs_direct_req *dreq)
  189. {
  190. if (dreq->iocb) {
  191. long res = (long) dreq->error;
  192. if (!res)
  193. res = (long) dreq->count;
  194. aio_complete(dreq->iocb, res, 0);
  195. }
  196. complete_all(&dreq->completion);
  197. nfs_direct_req_release(dreq);
  198. }
  199. static void nfs_direct_readpage_release(struct nfs_page *req)
  200. {
  201. dprintk("NFS: direct read done (%s/%lld %d@%lld)\n",
  202. req->wb_context->dentry->d_inode->i_sb->s_id,
  203. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  204. req->wb_bytes,
  205. (long long)req_offset(req));
  206. nfs_release_request(req);
  207. }
  208. static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
  209. {
  210. unsigned long bytes = 0;
  211. struct nfs_direct_req *dreq = hdr->dreq;
  212. if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
  213. goto out_put;
  214. spin_lock(&dreq->lock);
  215. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
  216. dreq->error = hdr->error;
  217. else
  218. dreq->count += hdr->good_bytes;
  219. spin_unlock(&dreq->lock);
  220. while (!list_empty(&hdr->pages)) {
  221. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  222. struct page *page = req->wb_page;
  223. if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
  224. if (bytes > hdr->good_bytes)
  225. zero_user(page, 0, PAGE_SIZE);
  226. else if (hdr->good_bytes - bytes < PAGE_SIZE)
  227. zero_user_segment(page,
  228. hdr->good_bytes & ~PAGE_MASK,
  229. PAGE_SIZE);
  230. }
  231. if (!PageCompound(page)) {
  232. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
  233. if (bytes < hdr->good_bytes)
  234. set_page_dirty(page);
  235. } else
  236. set_page_dirty(page);
  237. }
  238. bytes += req->wb_bytes;
  239. nfs_list_remove_request(req);
  240. nfs_direct_readpage_release(req);
  241. }
  242. out_put:
  243. if (put_dreq(dreq))
  244. nfs_direct_complete(dreq);
  245. hdr->release(hdr);
  246. }
  247. static void nfs_read_sync_pgio_error(struct list_head *head)
  248. {
  249. struct nfs_page *req;
  250. while (!list_empty(head)) {
  251. req = nfs_list_entry(head->next);
  252. nfs_list_remove_request(req);
  253. nfs_release_request(req);
  254. }
  255. }
  256. static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
  257. {
  258. get_dreq(hdr->dreq);
  259. }
  260. static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
  261. .error_cleanup = nfs_read_sync_pgio_error,
  262. .init_hdr = nfs_direct_pgio_init,
  263. .completion = nfs_direct_read_completion,
  264. };
  265. /*
  266. * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
  267. * operation. If nfs_readdata_alloc() or get_user_pages() fails,
  268. * bail and stop sending more reads. Read length accounting is
  269. * handled automatically by nfs_direct_read_result(). Otherwise, if
  270. * no requests have been sent, just return an error.
  271. */
  272. static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
  273. const struct iovec *iov,
  274. loff_t pos, bool uio)
  275. {
  276. struct nfs_direct_req *dreq = desc->pg_dreq;
  277. struct nfs_open_context *ctx = dreq->ctx;
  278. struct inode *inode = ctx->dentry->d_inode;
  279. unsigned long user_addr = (unsigned long)iov->iov_base;
  280. size_t count = iov->iov_len;
  281. size_t rsize = NFS_SERVER(inode)->rsize;
  282. unsigned int pgbase;
  283. int result;
  284. ssize_t started = 0;
  285. struct page **pagevec = NULL;
  286. unsigned int npages;
  287. do {
  288. size_t bytes;
  289. int i;
  290. pgbase = user_addr & ~PAGE_MASK;
  291. bytes = min(max_t(size_t, rsize, PAGE_SIZE), count);
  292. result = -ENOMEM;
  293. npages = nfs_page_array_len(pgbase, bytes);
  294. if (!pagevec)
  295. pagevec = kmalloc(npages * sizeof(struct page *),
  296. GFP_KERNEL);
  297. if (!pagevec)
  298. break;
  299. if (uio) {
  300. down_read(&current->mm->mmap_sem);
  301. result = get_user_pages(current, current->mm, user_addr,
  302. npages, 1, 0, pagevec, NULL);
  303. up_read(&current->mm->mmap_sem);
  304. if (result < 0)
  305. break;
  306. } else {
  307. WARN_ON(npages != 1);
  308. result = get_kernel_page(user_addr, 1, pagevec);
  309. if (WARN_ON(result != 1))
  310. break;
  311. }
  312. if ((unsigned)result < npages) {
  313. bytes = result * PAGE_SIZE;
  314. if (bytes <= pgbase) {
  315. nfs_direct_release_pages(pagevec, result);
  316. break;
  317. }
  318. bytes -= pgbase;
  319. npages = result;
  320. }
  321. for (i = 0; i < npages; i++) {
  322. struct nfs_page *req;
  323. unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
  324. /* XXX do we need to do the eof zeroing found in async_filler? */
  325. req = nfs_create_request(dreq->ctx, dreq->inode,
  326. pagevec[i],
  327. pgbase, req_len);
  328. if (IS_ERR(req)) {
  329. result = PTR_ERR(req);
  330. break;
  331. }
  332. req->wb_index = pos >> PAGE_SHIFT;
  333. req->wb_offset = pos & ~PAGE_MASK;
  334. if (!nfs_pageio_add_request(desc, req)) {
  335. result = desc->pg_error;
  336. nfs_release_request(req);
  337. break;
  338. }
  339. pgbase = 0;
  340. bytes -= req_len;
  341. started += req_len;
  342. user_addr += req_len;
  343. pos += req_len;
  344. count -= 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. dreq = nfs_direct_req_alloc();
  397. if (dreq == NULL)
  398. goto out;
  399. dreq->inode = inode;
  400. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  401. dreq->l_ctx = nfs_get_lock_context(dreq->ctx);
  402. if (dreq->l_ctx == NULL)
  403. goto out_release;
  404. if (!is_sync_kiocb(iocb))
  405. dreq->iocb = iocb;
  406. result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos, uio);
  407. if (!result)
  408. result = nfs_direct_wait(dreq);
  409. NFS_I(inode)->read_io += result;
  410. out_release:
  411. nfs_direct_req_release(dreq);
  412. out:
  413. return result;
  414. }
  415. static void nfs_inode_dio_write_done(struct inode *inode)
  416. {
  417. nfs_zap_mapping(inode, inode->i_mapping);
  418. inode_dio_done(inode);
  419. }
  420. #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
  421. static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
  422. {
  423. struct nfs_pageio_descriptor desc;
  424. struct nfs_page *req, *tmp;
  425. LIST_HEAD(reqs);
  426. struct nfs_commit_info cinfo;
  427. LIST_HEAD(failed);
  428. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  429. pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
  430. spin_lock(cinfo.lock);
  431. nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
  432. spin_unlock(cinfo.lock);
  433. dreq->count = 0;
  434. get_dreq(dreq);
  435. NFS_PROTO(dreq->inode)->write_pageio_init(&desc, dreq->inode, FLUSH_STABLE,
  436. &nfs_direct_write_completion_ops);
  437. desc.pg_dreq = dreq;
  438. list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
  439. if (!nfs_pageio_add_request(&desc, req)) {
  440. nfs_list_remove_request(req);
  441. nfs_list_add_request(req, &failed);
  442. spin_lock(cinfo.lock);
  443. dreq->flags = 0;
  444. dreq->error = -EIO;
  445. spin_unlock(cinfo.lock);
  446. }
  447. nfs_release_request(req);
  448. }
  449. nfs_pageio_complete(&desc);
  450. while (!list_empty(&failed)) {
  451. req = nfs_list_entry(failed.next);
  452. nfs_list_remove_request(req);
  453. nfs_unlock_and_release_request(req);
  454. }
  455. if (put_dreq(dreq))
  456. nfs_direct_write_complete(dreq, dreq->inode);
  457. }
  458. static void nfs_direct_commit_complete(struct nfs_commit_data *data)
  459. {
  460. struct nfs_direct_req *dreq = data->dreq;
  461. struct nfs_commit_info cinfo;
  462. struct nfs_page *req;
  463. int status = data->task.tk_status;
  464. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  465. if (status < 0) {
  466. dprintk("NFS: %5u commit failed with error %d.\n",
  467. data->task.tk_pid, status);
  468. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  469. } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
  470. dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
  471. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  472. }
  473. dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
  474. while (!list_empty(&data->pages)) {
  475. req = nfs_list_entry(data->pages.next);
  476. nfs_list_remove_request(req);
  477. if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
  478. /* Note the rewrite will go through mds */
  479. nfs_mark_request_commit(req, NULL, &cinfo);
  480. } else
  481. nfs_release_request(req);
  482. nfs_unlock_and_release_request(req);
  483. }
  484. if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
  485. nfs_direct_write_complete(dreq, data->inode);
  486. }
  487. static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
  488. {
  489. /* There is no lock to clear */
  490. }
  491. static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
  492. .completion = nfs_direct_commit_complete,
  493. .error_cleanup = nfs_direct_error_cleanup,
  494. };
  495. static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
  496. {
  497. int res;
  498. struct nfs_commit_info cinfo;
  499. LIST_HEAD(mds_list);
  500. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  501. nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
  502. res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
  503. if (res < 0) /* res == -ENOMEM */
  504. nfs_direct_write_reschedule(dreq);
  505. }
  506. static void nfs_direct_write_schedule_work(struct work_struct *work)
  507. {
  508. struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
  509. int flags = dreq->flags;
  510. dreq->flags = 0;
  511. switch (flags) {
  512. case NFS_ODIRECT_DO_COMMIT:
  513. nfs_direct_commit_schedule(dreq);
  514. break;
  515. case NFS_ODIRECT_RESCHED_WRITES:
  516. nfs_direct_write_reschedule(dreq);
  517. break;
  518. default:
  519. nfs_inode_dio_write_done(dreq->inode);
  520. nfs_direct_complete(dreq);
  521. }
  522. }
  523. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  524. {
  525. schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
  526. }
  527. #else
  528. static void nfs_direct_write_schedule_work(struct work_struct *work)
  529. {
  530. }
  531. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  532. {
  533. nfs_inode_dio_write_done(inode);
  534. nfs_direct_complete(dreq);
  535. }
  536. #endif
  537. /*
  538. * NB: Return the value of the first error return code. Subsequent
  539. * errors after the first one are ignored.
  540. */
  541. /*
  542. * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
  543. * operation. If nfs_writedata_alloc() or get_user_pages() fails,
  544. * bail and stop sending more writes. Write length accounting is
  545. * handled automatically by nfs_direct_write_result(). Otherwise, if
  546. * no requests have been sent, just return an error.
  547. */
  548. static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
  549. const struct iovec *iov,
  550. loff_t pos, bool uio)
  551. {
  552. struct nfs_direct_req *dreq = desc->pg_dreq;
  553. struct nfs_open_context *ctx = dreq->ctx;
  554. struct inode *inode = ctx->dentry->d_inode;
  555. unsigned long user_addr = (unsigned long)iov->iov_base;
  556. size_t count = iov->iov_len;
  557. size_t wsize = NFS_SERVER(inode)->wsize;
  558. unsigned int pgbase;
  559. int result;
  560. ssize_t started = 0;
  561. struct page **pagevec = NULL;
  562. unsigned int npages;
  563. do {
  564. size_t bytes;
  565. int i;
  566. pgbase = user_addr & ~PAGE_MASK;
  567. bytes = min(max_t(size_t, wsize, PAGE_SIZE), count);
  568. result = -ENOMEM;
  569. npages = nfs_page_array_len(pgbase, bytes);
  570. if (!pagevec)
  571. pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
  572. if (!pagevec)
  573. break;
  574. if (uio) {
  575. down_read(&current->mm->mmap_sem);
  576. result = get_user_pages(current, current->mm, user_addr,
  577. npages, 0, 0, pagevec, NULL);
  578. up_read(&current->mm->mmap_sem);
  579. if (result < 0)
  580. break;
  581. } else {
  582. WARN_ON(npages != 1);
  583. result = get_kernel_page(user_addr, 0, pagevec);
  584. if (WARN_ON(result != 1))
  585. break;
  586. }
  587. if ((unsigned)result < npages) {
  588. bytes = result * PAGE_SIZE;
  589. if (bytes <= pgbase) {
  590. nfs_direct_release_pages(pagevec, result);
  591. break;
  592. }
  593. bytes -= pgbase;
  594. npages = result;
  595. }
  596. for (i = 0; i < npages; i++) {
  597. struct nfs_page *req;
  598. unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
  599. req = nfs_create_request(dreq->ctx, dreq->inode,
  600. pagevec[i],
  601. pgbase, req_len);
  602. if (IS_ERR(req)) {
  603. result = PTR_ERR(req);
  604. break;
  605. }
  606. nfs_lock_request(req);
  607. req->wb_index = pos >> PAGE_SHIFT;
  608. req->wb_offset = pos & ~PAGE_MASK;
  609. if (!nfs_pageio_add_request(desc, req)) {
  610. result = desc->pg_error;
  611. nfs_unlock_and_release_request(req);
  612. break;
  613. }
  614. pgbase = 0;
  615. bytes -= req_len;
  616. started += req_len;
  617. user_addr += req_len;
  618. pos += req_len;
  619. count -= 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. for (seg = 0; seg < nr_segs; seg++) {
  714. const struct iovec *vec = &iov[seg];
  715. result = nfs_direct_write_schedule_segment(&desc, vec, pos, uio);
  716. if (result < 0)
  717. break;
  718. requested_bytes += result;
  719. if ((size_t)result < vec->iov_len)
  720. break;
  721. pos += vec->iov_len;
  722. }
  723. nfs_pageio_complete(&desc);
  724. NFS_I(dreq->inode)->write_io += desc.pg_bytes_written;
  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. dreq = nfs_direct_req_alloc();
  746. if (!dreq)
  747. goto out;
  748. dreq->inode = inode;
  749. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  750. dreq->l_ctx = nfs_get_lock_context(dreq->ctx);
  751. if (dreq->l_ctx == NULL)
  752. goto out_release;
  753. if (!is_sync_kiocb(iocb))
  754. dreq->iocb = iocb;
  755. result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, uio);
  756. if (!result)
  757. result = nfs_direct_wait(dreq);
  758. out_release:
  759. nfs_direct_req_release(dreq);
  760. out:
  761. return result;
  762. }
  763. /**
  764. * nfs_file_direct_read - file direct read operation for NFS files
  765. * @iocb: target I/O control block
  766. * @iov: vector of user buffers into which to read data
  767. * @nr_segs: size of iov vector
  768. * @pos: byte offset in file where reading starts
  769. *
  770. * We use this function for direct reads instead of calling
  771. * generic_file_aio_read() in order to avoid gfar's check to see if
  772. * the request starts before the end of the file. For that check
  773. * to work, we must generate a GETATTR before each direct read, and
  774. * even then there is a window between the GETATTR and the subsequent
  775. * READ where the file size could change. Our preference is simply
  776. * to do all reads the application wants, and the server will take
  777. * care of managing the end of file boundary.
  778. *
  779. * This function also eliminates unnecessarily updating the file's
  780. * atime locally, as the NFS server sets the file's atime, and this
  781. * client must read the updated atime from the server back into its
  782. * cache.
  783. */
  784. ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
  785. unsigned long nr_segs, loff_t pos, bool uio)
  786. {
  787. ssize_t retval = -EINVAL;
  788. struct file *file = iocb->ki_filp;
  789. struct address_space *mapping = file->f_mapping;
  790. size_t count;
  791. count = iov_length(iov, nr_segs);
  792. nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
  793. dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
  794. file->f_path.dentry->d_parent->d_name.name,
  795. file->f_path.dentry->d_name.name,
  796. count, (long long) pos);
  797. retval = 0;
  798. if (!count)
  799. goto out;
  800. retval = nfs_sync_mapping(mapping);
  801. if (retval)
  802. goto out;
  803. task_io_account_read(count);
  804. retval = nfs_direct_read(iocb, iov, nr_segs, pos, uio);
  805. if (retval > 0)
  806. iocb->ki_pos = pos + retval;
  807. out:
  808. return retval;
  809. }
  810. /**
  811. * nfs_file_direct_write - file direct write operation for NFS files
  812. * @iocb: target I/O control block
  813. * @iov: vector of user buffers from which to write data
  814. * @nr_segs: size of iov vector
  815. * @pos: byte offset in file where writing starts
  816. *
  817. * We use this function for direct writes instead of calling
  818. * generic_file_aio_write() in order to avoid taking the inode
  819. * semaphore and updating the i_size. The NFS server will set
  820. * the new i_size and this client must read the updated size
  821. * back into its cache. We let the server do generic write
  822. * parameter checking and report problems.
  823. *
  824. * We eliminate local atime updates, see direct read above.
  825. *
  826. * We avoid unnecessary page cache invalidations for normal cached
  827. * readers of this file.
  828. *
  829. * Note that O_APPEND is not supported for NFS direct writes, as there
  830. * is no atomic O_APPEND write facility in the NFS protocol.
  831. */
  832. ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
  833. unsigned long nr_segs, loff_t pos, bool uio)
  834. {
  835. ssize_t retval = -EINVAL;
  836. struct file *file = iocb->ki_filp;
  837. struct address_space *mapping = file->f_mapping;
  838. size_t count;
  839. count = iov_length(iov, nr_segs);
  840. nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
  841. dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
  842. file->f_path.dentry->d_parent->d_name.name,
  843. file->f_path.dentry->d_name.name,
  844. count, (long long) pos);
  845. retval = generic_write_checks(file, &pos, &count, 0);
  846. if (retval)
  847. goto out;
  848. retval = -EINVAL;
  849. if ((ssize_t) count < 0)
  850. goto out;
  851. retval = 0;
  852. if (!count)
  853. goto out;
  854. retval = nfs_sync_mapping(mapping);
  855. if (retval)
  856. goto out;
  857. task_io_account_write(count);
  858. retval = nfs_direct_write(iocb, iov, nr_segs, pos, count, uio);
  859. if (retval > 0) {
  860. struct inode *inode = mapping->host;
  861. iocb->ki_pos = pos + retval;
  862. spin_lock(&inode->i_lock);
  863. if (i_size_read(inode) < iocb->ki_pos)
  864. i_size_write(inode, iocb->ki_pos);
  865. spin_unlock(&inode->i_lock);
  866. }
  867. out:
  868. return retval;
  869. }
  870. /**
  871. * nfs_init_directcache - create a slab cache for nfs_direct_req structures
  872. *
  873. */
  874. int __init nfs_init_directcache(void)
  875. {
  876. nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
  877. sizeof(struct nfs_direct_req),
  878. 0, (SLAB_RECLAIM_ACCOUNT|
  879. SLAB_MEM_SPREAD),
  880. NULL);
  881. if (nfs_direct_cachep == NULL)
  882. return -ENOMEM;
  883. return 0;
  884. }
  885. /**
  886. * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
  887. *
  888. */
  889. void nfs_destroy_directcache(void)
  890. {
  891. kmem_cache_destroy(nfs_direct_cachep);
  892. }