direct.c 28 KB

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