direct.c 28 KB

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