direct.c 28 KB

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