direct.c 28 KB

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