direct.c 28 KB

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