direct.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /*
  2. * linux/fs/nfs/direct.c
  3. *
  4. * Copyright (C) 2003 by Chuck Lever <cel@netapp.com>
  5. *
  6. * High-performance uncached I/O for the Linux NFS client
  7. *
  8. * There are important applications whose performance or correctness
  9. * depends on uncached access to file data. Database clusters
  10. * (multiple copies of the same instance running on separate hosts)
  11. * implement their own cache coherency protocol that subsumes file
  12. * system cache protocols. Applications that process datasets
  13. * considerably larger than the client's memory do not always benefit
  14. * from a local cache. A streaming video server, for instance, has no
  15. * need to cache the contents of a file.
  16. *
  17. * When an application requests uncached I/O, all read and write requests
  18. * are made directly to the server; data stored or fetched via these
  19. * requests is not cached in the Linux page cache. The client does not
  20. * correct unaligned requests from applications. All requested bytes are
  21. * held on permanent storage before a direct write system call returns to
  22. * an application.
  23. *
  24. * Solaris implements an uncached I/O facility called directio() that
  25. * is used for backups and sequential I/O to very large files. Solaris
  26. * also supports uncaching whole NFS partitions with "-o forcedirectio,"
  27. * an undocumented mount option.
  28. *
  29. * Designed by Jeff Kimmel, Chuck Lever, and Trond Myklebust, with
  30. * help from Andrew Morton.
  31. *
  32. * 18 Dec 2001 Initial implementation for 2.4 --cel
  33. * 08 Jul 2002 Version for 2.4.19, with bug fixes --trondmy
  34. * 08 Jun 2003 Port to 2.5 APIs --cel
  35. * 31 Mar 2004 Handle direct I/O without VFS support --cel
  36. * 15 Sep 2004 Parallel async reads --cel
  37. * 04 May 2005 support O_DIRECT with aio --cel
  38. *
  39. */
  40. #include <linux/errno.h>
  41. #include <linux/sched.h>
  42. #include <linux/kernel.h>
  43. #include <linux/file.h>
  44. #include <linux/pagemap.h>
  45. #include <linux/kref.h>
  46. #include <linux/slab.h>
  47. #include <linux/task_io_accounting_ops.h>
  48. #include <linux/nfs_fs.h>
  49. #include <linux/nfs_page.h>
  50. #include <linux/sunrpc/clnt.h>
  51. #include <asm/uaccess.h>
  52. #include <linux/atomic.h>
  53. #include "internal.h"
  54. #include "iostat.h"
  55. #include "pnfs.h"
  56. #define NFSDBG_FACILITY NFSDBG_VFS
  57. static struct kmem_cache *nfs_direct_cachep;
  58. /*
  59. * This represents a set of asynchronous requests that we're waiting on
  60. */
  61. struct nfs_direct_req {
  62. struct kref kref; /* release manager */
  63. /* I/O parameters */
  64. struct nfs_open_context *ctx; /* file open context info */
  65. struct nfs_lock_context *l_ctx; /* Lock context info */
  66. struct kiocb * iocb; /* controlling i/o request */
  67. struct inode * inode; /* target file of i/o */
  68. /* completion state */
  69. atomic_t io_count; /* i/os we're waiting for */
  70. spinlock_t lock; /* protect completion state */
  71. ssize_t count, /* bytes actually processed */
  72. bytes_left, /* bytes left to be sent */
  73. error; /* any reported error */
  74. struct completion completion; /* wait for i/o completion */
  75. /* commit state */
  76. struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */
  77. struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */
  78. struct work_struct work;
  79. int flags;
  80. #define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
  81. #define NFS_ODIRECT_RESCHED_WRITES (2) /* write verification failed */
  82. struct nfs_writeverf verf; /* unstable write verifier */
  83. };
  84. static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops;
  85. static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops;
  86. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode);
  87. static void nfs_direct_write_schedule_work(struct work_struct *work);
  88. static inline void get_dreq(struct nfs_direct_req *dreq)
  89. {
  90. atomic_inc(&dreq->io_count);
  91. }
  92. static inline int put_dreq(struct nfs_direct_req *dreq)
  93. {
  94. return atomic_dec_and_test(&dreq->io_count);
  95. }
  96. /**
  97. * nfs_direct_IO - NFS address space operation for direct I/O
  98. * @rw: direction (read or write)
  99. * @iocb: target I/O control block
  100. * @iov: array of vectors that define I/O buffer
  101. * @pos: offset in file to begin the operation
  102. * @nr_segs: size of iovec array
  103. *
  104. * The presence of this routine in the address space ops vector means
  105. * the NFS client supports direct I/O. However, for most direct IO, we
  106. * shunt off direct read and write requests before the VFS gets them,
  107. * so this method is only ever called for swap.
  108. */
  109. ssize_t nfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, loff_t pos, unsigned long nr_segs)
  110. {
  111. #ifndef CONFIG_NFS_SWAP
  112. dprintk("NFS: nfs_direct_IO (%s) off/no(%Ld/%lu) EINVAL\n",
  113. iocb->ki_filp->f_path.dentry->d_name.name,
  114. (long long) pos, nr_segs);
  115. return -EINVAL;
  116. #else
  117. VM_BUG_ON(iocb->ki_left != PAGE_SIZE);
  118. VM_BUG_ON(iocb->ki_nbytes != PAGE_SIZE);
  119. if (rw == READ || rw == KERNEL_READ)
  120. return nfs_file_direct_read(iocb, iov, nr_segs, pos,
  121. rw == READ ? true : false);
  122. return nfs_file_direct_write(iocb, iov, nr_segs, pos,
  123. rw == WRITE ? true : false);
  124. #endif /* CONFIG_NFS_SWAP */
  125. }
  126. static void nfs_direct_release_pages(struct page **pages, unsigned int npages)
  127. {
  128. unsigned int i;
  129. for (i = 0; i < npages; i++)
  130. page_cache_release(pages[i]);
  131. }
  132. void nfs_init_cinfo_from_dreq(struct nfs_commit_info *cinfo,
  133. struct nfs_direct_req *dreq)
  134. {
  135. cinfo->lock = &dreq->lock;
  136. cinfo->mds = &dreq->mds_cinfo;
  137. cinfo->ds = &dreq->ds_cinfo;
  138. cinfo->dreq = dreq;
  139. cinfo->completion_ops = &nfs_direct_commit_completion_ops;
  140. }
  141. static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
  142. {
  143. struct nfs_direct_req *dreq;
  144. dreq = kmem_cache_zalloc(nfs_direct_cachep, GFP_KERNEL);
  145. if (!dreq)
  146. return NULL;
  147. kref_init(&dreq->kref);
  148. kref_get(&dreq->kref);
  149. init_completion(&dreq->completion);
  150. INIT_LIST_HEAD(&dreq->mds_cinfo.list);
  151. INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
  152. spin_lock_init(&dreq->lock);
  153. return dreq;
  154. }
  155. static void nfs_direct_req_free(struct kref *kref)
  156. {
  157. struct nfs_direct_req *dreq = container_of(kref, struct nfs_direct_req, kref);
  158. if (dreq->l_ctx != NULL)
  159. nfs_put_lock_context(dreq->l_ctx);
  160. if (dreq->ctx != NULL)
  161. put_nfs_open_context(dreq->ctx);
  162. kmem_cache_free(nfs_direct_cachep, dreq);
  163. }
  164. static void nfs_direct_req_release(struct nfs_direct_req *dreq)
  165. {
  166. kref_put(&dreq->kref, nfs_direct_req_free);
  167. }
  168. /*
  169. * Collects and returns the final error value/byte-count.
  170. */
  171. static ssize_t nfs_direct_wait(struct nfs_direct_req *dreq)
  172. {
  173. ssize_t result = -EIOCBQUEUED;
  174. /* Async requests don't wait here */
  175. if (dreq->iocb)
  176. goto out;
  177. result = wait_for_completion_killable(&dreq->completion);
  178. if (!result)
  179. result = dreq->error;
  180. if (!result)
  181. result = dreq->count;
  182. out:
  183. return (ssize_t) result;
  184. }
  185. /*
  186. * Synchronous I/O uses a stack-allocated iocb. Thus we can't trust
  187. * the iocb is still valid here if this is a synchronous request.
  188. */
  189. static void nfs_direct_complete(struct nfs_direct_req *dreq)
  190. {
  191. if (dreq->iocb) {
  192. long res = (long) dreq->error;
  193. if (!res)
  194. res = (long) dreq->count;
  195. aio_complete(dreq->iocb, res, 0);
  196. }
  197. complete_all(&dreq->completion);
  198. nfs_direct_req_release(dreq);
  199. }
  200. static void nfs_direct_readpage_release(struct nfs_page *req)
  201. {
  202. dprintk("NFS: direct read done (%s/%lld %d@%lld)\n",
  203. req->wb_context->dentry->d_inode->i_sb->s_id,
  204. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  205. req->wb_bytes,
  206. (long long)req_offset(req));
  207. nfs_release_request(req);
  208. }
  209. static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
  210. {
  211. unsigned long bytes = 0;
  212. struct nfs_direct_req *dreq = hdr->dreq;
  213. if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
  214. goto out_put;
  215. spin_lock(&dreq->lock);
  216. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && (hdr->good_bytes == 0))
  217. dreq->error = hdr->error;
  218. else
  219. dreq->count += hdr->good_bytes;
  220. spin_unlock(&dreq->lock);
  221. while (!list_empty(&hdr->pages)) {
  222. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  223. struct page *page = req->wb_page;
  224. if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
  225. if (bytes > hdr->good_bytes)
  226. zero_user(page, 0, PAGE_SIZE);
  227. else if (hdr->good_bytes - bytes < PAGE_SIZE)
  228. zero_user_segment(page,
  229. hdr->good_bytes & ~PAGE_MASK,
  230. PAGE_SIZE);
  231. }
  232. if (!PageCompound(page)) {
  233. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
  234. if (bytes < hdr->good_bytes)
  235. set_page_dirty(page);
  236. } else
  237. set_page_dirty(page);
  238. }
  239. bytes += req->wb_bytes;
  240. nfs_list_remove_request(req);
  241. nfs_direct_readpage_release(req);
  242. }
  243. out_put:
  244. if (put_dreq(dreq))
  245. nfs_direct_complete(dreq);
  246. hdr->release(hdr);
  247. }
  248. static void nfs_read_sync_pgio_error(struct list_head *head)
  249. {
  250. struct nfs_page *req;
  251. while (!list_empty(head)) {
  252. req = nfs_list_entry(head->next);
  253. nfs_list_remove_request(req);
  254. nfs_release_request(req);
  255. }
  256. }
  257. static void nfs_direct_pgio_init(struct nfs_pgio_header *hdr)
  258. {
  259. get_dreq(hdr->dreq);
  260. }
  261. static const struct nfs_pgio_completion_ops nfs_direct_read_completion_ops = {
  262. .error_cleanup = nfs_read_sync_pgio_error,
  263. .init_hdr = nfs_direct_pgio_init,
  264. .completion = nfs_direct_read_completion,
  265. };
  266. /*
  267. * For each rsize'd chunk of the user's buffer, dispatch an NFS READ
  268. * operation. If nfs_readdata_alloc() or get_user_pages() fails,
  269. * bail and stop sending more reads. Read length accounting is
  270. * handled automatically by nfs_direct_read_result(). Otherwise, if
  271. * no requests have been sent, just return an error.
  272. */
  273. static ssize_t nfs_direct_read_schedule_segment(struct nfs_pageio_descriptor *desc,
  274. const struct iovec *iov,
  275. loff_t pos, bool uio)
  276. {
  277. struct nfs_direct_req *dreq = desc->pg_dreq;
  278. struct nfs_open_context *ctx = dreq->ctx;
  279. struct inode *inode = ctx->dentry->d_inode;
  280. unsigned long user_addr = (unsigned long)iov->iov_base;
  281. size_t count = iov->iov_len;
  282. size_t rsize = NFS_SERVER(inode)->rsize;
  283. unsigned int pgbase;
  284. int result;
  285. ssize_t started = 0;
  286. struct page **pagevec = NULL;
  287. unsigned int npages;
  288. do {
  289. size_t bytes;
  290. int i;
  291. pgbase = user_addr & ~PAGE_MASK;
  292. bytes = min(max_t(size_t, rsize, PAGE_SIZE), count);
  293. result = -ENOMEM;
  294. npages = nfs_page_array_len(pgbase, bytes);
  295. if (!pagevec)
  296. pagevec = kmalloc(npages * sizeof(struct page *),
  297. GFP_KERNEL);
  298. if (!pagevec)
  299. break;
  300. if (uio) {
  301. down_read(&current->mm->mmap_sem);
  302. result = get_user_pages(current, current->mm, user_addr,
  303. npages, 1, 0, pagevec, NULL);
  304. up_read(&current->mm->mmap_sem);
  305. if (result < 0)
  306. break;
  307. } else {
  308. WARN_ON(npages != 1);
  309. result = get_kernel_page(user_addr, 1, pagevec);
  310. if (WARN_ON(result != 1))
  311. break;
  312. }
  313. if ((unsigned)result < npages) {
  314. bytes = result * PAGE_SIZE;
  315. if (bytes <= pgbase) {
  316. nfs_direct_release_pages(pagevec, result);
  317. break;
  318. }
  319. bytes -= pgbase;
  320. npages = result;
  321. }
  322. for (i = 0; i < npages; i++) {
  323. struct nfs_page *req;
  324. unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
  325. /* XXX do we need to do the eof zeroing found in async_filler? */
  326. req = nfs_create_request(dreq->ctx, dreq->inode,
  327. pagevec[i],
  328. pgbase, req_len);
  329. if (IS_ERR(req)) {
  330. result = PTR_ERR(req);
  331. break;
  332. }
  333. req->wb_index = pos >> PAGE_SHIFT;
  334. req->wb_offset = pos & ~PAGE_MASK;
  335. if (!nfs_pageio_add_request(desc, req)) {
  336. result = desc->pg_error;
  337. nfs_release_request(req);
  338. break;
  339. }
  340. pgbase = 0;
  341. bytes -= req_len;
  342. started += req_len;
  343. user_addr += req_len;
  344. pos += req_len;
  345. count -= req_len;
  346. dreq->bytes_left -= req_len;
  347. }
  348. /* The nfs_page now hold references to these pages */
  349. nfs_direct_release_pages(pagevec, npages);
  350. } while (count != 0 && result >= 0);
  351. kfree(pagevec);
  352. if (started)
  353. return started;
  354. return result < 0 ? (ssize_t) result : -EFAULT;
  355. }
  356. static ssize_t nfs_direct_read_schedule_iovec(struct nfs_direct_req *dreq,
  357. const struct iovec *iov,
  358. unsigned long nr_segs,
  359. loff_t pos, bool uio)
  360. {
  361. struct nfs_pageio_descriptor desc;
  362. ssize_t result = -EINVAL;
  363. size_t requested_bytes = 0;
  364. unsigned long seg;
  365. NFS_PROTO(dreq->inode)->read_pageio_init(&desc, dreq->inode,
  366. &nfs_direct_read_completion_ops);
  367. get_dreq(dreq);
  368. desc.pg_dreq = dreq;
  369. for (seg = 0; seg < nr_segs; seg++) {
  370. const struct iovec *vec = &iov[seg];
  371. result = nfs_direct_read_schedule_segment(&desc, vec, pos, uio);
  372. if (result < 0)
  373. break;
  374. requested_bytes += result;
  375. if ((size_t)result < vec->iov_len)
  376. break;
  377. pos += vec->iov_len;
  378. }
  379. nfs_pageio_complete(&desc);
  380. /*
  381. * If no bytes were started, return the error, and let the
  382. * generic layer handle the completion.
  383. */
  384. if (requested_bytes == 0) {
  385. nfs_direct_req_release(dreq);
  386. return result < 0 ? result : -EIO;
  387. }
  388. if (put_dreq(dreq))
  389. nfs_direct_complete(dreq);
  390. return 0;
  391. }
  392. static ssize_t nfs_direct_read(struct kiocb *iocb, const struct iovec *iov,
  393. unsigned long nr_segs, loff_t pos, bool uio)
  394. {
  395. ssize_t result = -ENOMEM;
  396. struct inode *inode = iocb->ki_filp->f_mapping->host;
  397. struct nfs_direct_req *dreq;
  398. struct nfs_lock_context *l_ctx;
  399. dreq = nfs_direct_req_alloc();
  400. if (dreq == NULL)
  401. goto out;
  402. dreq->inode = inode;
  403. dreq->bytes_left = iov_length(iov, nr_segs);
  404. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  405. l_ctx = nfs_get_lock_context(dreq->ctx);
  406. if (IS_ERR(l_ctx)) {
  407. result = PTR_ERR(l_ctx);
  408. goto out_release;
  409. }
  410. dreq->l_ctx = l_ctx;
  411. if (!is_sync_kiocb(iocb))
  412. dreq->iocb = iocb;
  413. NFS_I(inode)->read_io += iov_length(iov, nr_segs);
  414. result = nfs_direct_read_schedule_iovec(dreq, iov, nr_segs, pos, uio);
  415. if (!result)
  416. result = nfs_direct_wait(dreq);
  417. out_release:
  418. nfs_direct_req_release(dreq);
  419. out:
  420. return result;
  421. }
  422. static void nfs_inode_dio_write_done(struct inode *inode)
  423. {
  424. nfs_zap_mapping(inode, inode->i_mapping);
  425. inode_dio_done(inode);
  426. }
  427. #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
  428. static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
  429. {
  430. struct nfs_pageio_descriptor desc;
  431. struct nfs_page *req, *tmp;
  432. LIST_HEAD(reqs);
  433. struct nfs_commit_info cinfo;
  434. LIST_HEAD(failed);
  435. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  436. pnfs_recover_commit_reqs(dreq->inode, &reqs, &cinfo);
  437. spin_lock(cinfo.lock);
  438. nfs_scan_commit_list(&cinfo.mds->list, &reqs, &cinfo, 0);
  439. spin_unlock(cinfo.lock);
  440. dreq->count = 0;
  441. get_dreq(dreq);
  442. NFS_PROTO(dreq->inode)->write_pageio_init(&desc, dreq->inode, FLUSH_STABLE,
  443. &nfs_direct_write_completion_ops);
  444. desc.pg_dreq = dreq;
  445. list_for_each_entry_safe(req, tmp, &reqs, wb_list) {
  446. if (!nfs_pageio_add_request(&desc, req)) {
  447. nfs_list_remove_request(req);
  448. nfs_list_add_request(req, &failed);
  449. spin_lock(cinfo.lock);
  450. dreq->flags = 0;
  451. dreq->error = -EIO;
  452. spin_unlock(cinfo.lock);
  453. }
  454. nfs_release_request(req);
  455. }
  456. nfs_pageio_complete(&desc);
  457. while (!list_empty(&failed)) {
  458. req = nfs_list_entry(failed.next);
  459. nfs_list_remove_request(req);
  460. nfs_unlock_and_release_request(req);
  461. }
  462. if (put_dreq(dreq))
  463. nfs_direct_write_complete(dreq, dreq->inode);
  464. }
  465. static void nfs_direct_commit_complete(struct nfs_commit_data *data)
  466. {
  467. struct nfs_direct_req *dreq = data->dreq;
  468. struct nfs_commit_info cinfo;
  469. struct nfs_page *req;
  470. int status = data->task.tk_status;
  471. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  472. if (status < 0) {
  473. dprintk("NFS: %5u commit failed with error %d.\n",
  474. data->task.tk_pid, status);
  475. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  476. } else if (memcmp(&dreq->verf, &data->verf, sizeof(data->verf))) {
  477. dprintk("NFS: %5u commit verify failed\n", data->task.tk_pid);
  478. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  479. }
  480. dprintk("NFS: %5u commit returned %d\n", data->task.tk_pid, status);
  481. while (!list_empty(&data->pages)) {
  482. req = nfs_list_entry(data->pages.next);
  483. nfs_list_remove_request(req);
  484. if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES) {
  485. /* Note the rewrite will go through mds */
  486. nfs_mark_request_commit(req, NULL, &cinfo);
  487. } else
  488. nfs_release_request(req);
  489. nfs_unlock_and_release_request(req);
  490. }
  491. if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
  492. nfs_direct_write_complete(dreq, data->inode);
  493. }
  494. static void nfs_direct_error_cleanup(struct nfs_inode *nfsi)
  495. {
  496. /* There is no lock to clear */
  497. }
  498. static const struct nfs_commit_completion_ops nfs_direct_commit_completion_ops = {
  499. .completion = nfs_direct_commit_complete,
  500. .error_cleanup = nfs_direct_error_cleanup,
  501. };
  502. static void nfs_direct_commit_schedule(struct nfs_direct_req *dreq)
  503. {
  504. int res;
  505. struct nfs_commit_info cinfo;
  506. LIST_HEAD(mds_list);
  507. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  508. nfs_scan_commit(dreq->inode, &mds_list, &cinfo);
  509. res = nfs_generic_commit_list(dreq->inode, &mds_list, 0, &cinfo);
  510. if (res < 0) /* res == -ENOMEM */
  511. nfs_direct_write_reschedule(dreq);
  512. }
  513. static void nfs_direct_write_schedule_work(struct work_struct *work)
  514. {
  515. struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
  516. int flags = dreq->flags;
  517. dreq->flags = 0;
  518. switch (flags) {
  519. case NFS_ODIRECT_DO_COMMIT:
  520. nfs_direct_commit_schedule(dreq);
  521. break;
  522. case NFS_ODIRECT_RESCHED_WRITES:
  523. nfs_direct_write_reschedule(dreq);
  524. break;
  525. default:
  526. nfs_inode_dio_write_done(dreq->inode);
  527. nfs_direct_complete(dreq);
  528. }
  529. }
  530. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  531. {
  532. schedule_work(&dreq->work); /* Calls nfs_direct_write_schedule_work */
  533. }
  534. #else
  535. static void nfs_direct_write_schedule_work(struct work_struct *work)
  536. {
  537. }
  538. static void nfs_direct_write_complete(struct nfs_direct_req *dreq, struct inode *inode)
  539. {
  540. nfs_inode_dio_write_done(inode);
  541. nfs_direct_complete(dreq);
  542. }
  543. #endif
  544. /*
  545. * NB: Return the value of the first error return code. Subsequent
  546. * errors after the first one are ignored.
  547. */
  548. /*
  549. * For each wsize'd chunk of the user's buffer, dispatch an NFS WRITE
  550. * operation. If nfs_writedata_alloc() or get_user_pages() fails,
  551. * bail and stop sending more writes. Write length accounting is
  552. * handled automatically by nfs_direct_write_result(). Otherwise, if
  553. * no requests have been sent, just return an error.
  554. */
  555. static ssize_t nfs_direct_write_schedule_segment(struct nfs_pageio_descriptor *desc,
  556. const struct iovec *iov,
  557. loff_t pos, bool uio)
  558. {
  559. struct nfs_direct_req *dreq = desc->pg_dreq;
  560. struct nfs_open_context *ctx = dreq->ctx;
  561. struct inode *inode = ctx->dentry->d_inode;
  562. unsigned long user_addr = (unsigned long)iov->iov_base;
  563. size_t count = iov->iov_len;
  564. size_t wsize = NFS_SERVER(inode)->wsize;
  565. unsigned int pgbase;
  566. int result;
  567. ssize_t started = 0;
  568. struct page **pagevec = NULL;
  569. unsigned int npages;
  570. do {
  571. size_t bytes;
  572. int i;
  573. pgbase = user_addr & ~PAGE_MASK;
  574. bytes = min(max_t(size_t, wsize, PAGE_SIZE), count);
  575. result = -ENOMEM;
  576. npages = nfs_page_array_len(pgbase, bytes);
  577. if (!pagevec)
  578. pagevec = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
  579. if (!pagevec)
  580. break;
  581. if (uio) {
  582. down_read(&current->mm->mmap_sem);
  583. result = get_user_pages(current, current->mm, user_addr,
  584. npages, 0, 0, pagevec, NULL);
  585. up_read(&current->mm->mmap_sem);
  586. if (result < 0)
  587. break;
  588. } else {
  589. WARN_ON(npages != 1);
  590. result = get_kernel_page(user_addr, 0, pagevec);
  591. if (WARN_ON(result != 1))
  592. break;
  593. }
  594. if ((unsigned)result < npages) {
  595. bytes = result * PAGE_SIZE;
  596. if (bytes <= pgbase) {
  597. nfs_direct_release_pages(pagevec, result);
  598. break;
  599. }
  600. bytes -= pgbase;
  601. npages = result;
  602. }
  603. for (i = 0; i < npages; i++) {
  604. struct nfs_page *req;
  605. unsigned int req_len = min_t(size_t, bytes, PAGE_SIZE - pgbase);
  606. req = nfs_create_request(dreq->ctx, dreq->inode,
  607. pagevec[i],
  608. pgbase, req_len);
  609. if (IS_ERR(req)) {
  610. result = PTR_ERR(req);
  611. break;
  612. }
  613. nfs_lock_request(req);
  614. req->wb_index = pos >> PAGE_SHIFT;
  615. req->wb_offset = pos & ~PAGE_MASK;
  616. if (!nfs_pageio_add_request(desc, req)) {
  617. result = desc->pg_error;
  618. nfs_unlock_and_release_request(req);
  619. break;
  620. }
  621. pgbase = 0;
  622. bytes -= req_len;
  623. started += req_len;
  624. user_addr += req_len;
  625. pos += req_len;
  626. count -= req_len;
  627. dreq->bytes_left -= req_len;
  628. }
  629. /* The nfs_page now hold references to these pages */
  630. nfs_direct_release_pages(pagevec, npages);
  631. } while (count != 0 && result >= 0);
  632. kfree(pagevec);
  633. if (started)
  634. return started;
  635. return result < 0 ? (ssize_t) result : -EFAULT;
  636. }
  637. static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
  638. {
  639. struct nfs_direct_req *dreq = hdr->dreq;
  640. struct nfs_commit_info cinfo;
  641. int bit = -1;
  642. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  643. if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
  644. goto out_put;
  645. nfs_init_cinfo_from_dreq(&cinfo, dreq);
  646. spin_lock(&dreq->lock);
  647. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
  648. dreq->flags = 0;
  649. dreq->error = hdr->error;
  650. }
  651. if (dreq->error != 0)
  652. bit = NFS_IOHDR_ERROR;
  653. else {
  654. dreq->count += hdr->good_bytes;
  655. if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
  656. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  657. bit = NFS_IOHDR_NEED_RESCHED;
  658. } else if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
  659. if (dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
  660. bit = NFS_IOHDR_NEED_RESCHED;
  661. else if (dreq->flags == 0) {
  662. memcpy(&dreq->verf, hdr->verf,
  663. sizeof(dreq->verf));
  664. bit = NFS_IOHDR_NEED_COMMIT;
  665. dreq->flags = NFS_ODIRECT_DO_COMMIT;
  666. } else if (dreq->flags == NFS_ODIRECT_DO_COMMIT) {
  667. if (memcmp(&dreq->verf, hdr->verf, sizeof(dreq->verf))) {
  668. dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
  669. bit = NFS_IOHDR_NEED_RESCHED;
  670. } else
  671. bit = NFS_IOHDR_NEED_COMMIT;
  672. }
  673. }
  674. }
  675. spin_unlock(&dreq->lock);
  676. while (!list_empty(&hdr->pages)) {
  677. req = nfs_list_entry(hdr->pages.next);
  678. nfs_list_remove_request(req);
  679. switch (bit) {
  680. case NFS_IOHDR_NEED_RESCHED:
  681. case NFS_IOHDR_NEED_COMMIT:
  682. kref_get(&req->wb_kref);
  683. nfs_mark_request_commit(req, hdr->lseg, &cinfo);
  684. }
  685. nfs_unlock_and_release_request(req);
  686. }
  687. out_put:
  688. if (put_dreq(dreq))
  689. nfs_direct_write_complete(dreq, hdr->inode);
  690. hdr->release(hdr);
  691. }
  692. static void nfs_write_sync_pgio_error(struct list_head *head)
  693. {
  694. struct nfs_page *req;
  695. while (!list_empty(head)) {
  696. req = nfs_list_entry(head->next);
  697. nfs_list_remove_request(req);
  698. nfs_unlock_and_release_request(req);
  699. }
  700. }
  701. static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
  702. .error_cleanup = nfs_write_sync_pgio_error,
  703. .init_hdr = nfs_direct_pgio_init,
  704. .completion = nfs_direct_write_completion,
  705. };
  706. static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
  707. const struct iovec *iov,
  708. unsigned long nr_segs,
  709. loff_t pos, bool uio)
  710. {
  711. struct nfs_pageio_descriptor desc;
  712. struct inode *inode = dreq->inode;
  713. ssize_t result = 0;
  714. size_t requested_bytes = 0;
  715. unsigned long seg;
  716. NFS_PROTO(inode)->write_pageio_init(&desc, inode, FLUSH_COND_STABLE,
  717. &nfs_direct_write_completion_ops);
  718. desc.pg_dreq = dreq;
  719. get_dreq(dreq);
  720. atomic_inc(&inode->i_dio_count);
  721. NFS_I(dreq->inode)->write_io += iov_length(iov, nr_segs);
  722. for (seg = 0; seg < nr_segs; seg++) {
  723. const struct iovec *vec = &iov[seg];
  724. result = nfs_direct_write_schedule_segment(&desc, vec, pos, uio);
  725. if (result < 0)
  726. break;
  727. requested_bytes += result;
  728. if ((size_t)result < vec->iov_len)
  729. break;
  730. pos += vec->iov_len;
  731. }
  732. nfs_pageio_complete(&desc);
  733. /*
  734. * If no bytes were started, return the error, and let the
  735. * generic layer handle the completion.
  736. */
  737. if (requested_bytes == 0) {
  738. inode_dio_done(inode);
  739. nfs_direct_req_release(dreq);
  740. return result < 0 ? result : -EIO;
  741. }
  742. if (put_dreq(dreq))
  743. nfs_direct_write_complete(dreq, dreq->inode);
  744. return 0;
  745. }
  746. static ssize_t nfs_direct_write(struct kiocb *iocb, const struct iovec *iov,
  747. unsigned long nr_segs, loff_t pos,
  748. size_t count, bool uio)
  749. {
  750. ssize_t result = -ENOMEM;
  751. struct inode *inode = iocb->ki_filp->f_mapping->host;
  752. struct nfs_direct_req *dreq;
  753. struct nfs_lock_context *l_ctx;
  754. dreq = nfs_direct_req_alloc();
  755. if (!dreq)
  756. goto out;
  757. dreq->inode = inode;
  758. dreq->bytes_left = count;
  759. dreq->ctx = get_nfs_open_context(nfs_file_open_context(iocb->ki_filp));
  760. l_ctx = nfs_get_lock_context(dreq->ctx);
  761. if (IS_ERR(l_ctx)) {
  762. result = PTR_ERR(l_ctx);
  763. goto out_release;
  764. }
  765. dreq->l_ctx = l_ctx;
  766. if (!is_sync_kiocb(iocb))
  767. dreq->iocb = iocb;
  768. result = nfs_direct_write_schedule_iovec(dreq, iov, nr_segs, pos, uio);
  769. if (!result)
  770. result = nfs_direct_wait(dreq);
  771. out_release:
  772. nfs_direct_req_release(dreq);
  773. out:
  774. return result;
  775. }
  776. /**
  777. * nfs_file_direct_read - file direct read operation for NFS files
  778. * @iocb: target I/O control block
  779. * @iov: vector of user buffers into which to read data
  780. * @nr_segs: size of iov vector
  781. * @pos: byte offset in file where reading starts
  782. *
  783. * We use this function for direct reads instead of calling
  784. * generic_file_aio_read() in order to avoid gfar's check to see if
  785. * the request starts before the end of the file. For that check
  786. * to work, we must generate a GETATTR before each direct read, and
  787. * even then there is a window between the GETATTR and the subsequent
  788. * READ where the file size could change. Our preference is simply
  789. * to do all reads the application wants, and the server will take
  790. * care of managing the end of file boundary.
  791. *
  792. * This function also eliminates unnecessarily updating the file's
  793. * atime locally, as the NFS server sets the file's atime, and this
  794. * client must read the updated atime from the server back into its
  795. * cache.
  796. */
  797. ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
  798. unsigned long nr_segs, loff_t pos, bool uio)
  799. {
  800. ssize_t retval = -EINVAL;
  801. struct file *file = iocb->ki_filp;
  802. struct address_space *mapping = file->f_mapping;
  803. size_t count;
  804. count = iov_length(iov, nr_segs);
  805. nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
  806. dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
  807. file->f_path.dentry->d_parent->d_name.name,
  808. file->f_path.dentry->d_name.name,
  809. count, (long long) pos);
  810. retval = 0;
  811. if (!count)
  812. goto out;
  813. retval = nfs_sync_mapping(mapping);
  814. if (retval)
  815. goto out;
  816. task_io_account_read(count);
  817. retval = nfs_direct_read(iocb, iov, nr_segs, pos, uio);
  818. if (retval > 0)
  819. iocb->ki_pos = pos + retval;
  820. out:
  821. return retval;
  822. }
  823. /**
  824. * nfs_file_direct_write - file direct write operation for NFS files
  825. * @iocb: target I/O control block
  826. * @iov: vector of user buffers from which to write data
  827. * @nr_segs: size of iov vector
  828. * @pos: byte offset in file where writing starts
  829. *
  830. * We use this function for direct writes instead of calling
  831. * generic_file_aio_write() in order to avoid taking the inode
  832. * semaphore and updating the i_size. The NFS server will set
  833. * the new i_size and this client must read the updated size
  834. * back into its cache. We let the server do generic write
  835. * parameter checking and report problems.
  836. *
  837. * We eliminate local atime updates, see direct read above.
  838. *
  839. * We avoid unnecessary page cache invalidations for normal cached
  840. * readers of this file.
  841. *
  842. * Note that O_APPEND is not supported for NFS direct writes, as there
  843. * is no atomic O_APPEND write facility in the NFS protocol.
  844. */
  845. ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
  846. unsigned long nr_segs, loff_t pos, bool uio)
  847. {
  848. ssize_t retval = -EINVAL;
  849. struct file *file = iocb->ki_filp;
  850. struct address_space *mapping = file->f_mapping;
  851. size_t count;
  852. count = iov_length(iov, nr_segs);
  853. nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
  854. dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
  855. file->f_path.dentry->d_parent->d_name.name,
  856. file->f_path.dentry->d_name.name,
  857. count, (long long) pos);
  858. retval = generic_write_checks(file, &pos, &count, 0);
  859. if (retval)
  860. goto out;
  861. retval = -EINVAL;
  862. if ((ssize_t) count < 0)
  863. goto out;
  864. retval = 0;
  865. if (!count)
  866. goto out;
  867. retval = nfs_sync_mapping(mapping);
  868. if (retval)
  869. goto out;
  870. task_io_account_write(count);
  871. retval = nfs_direct_write(iocb, iov, nr_segs, pos, count, uio);
  872. if (retval > 0) {
  873. struct inode *inode = mapping->host;
  874. iocb->ki_pos = pos + retval;
  875. spin_lock(&inode->i_lock);
  876. if (i_size_read(inode) < iocb->ki_pos)
  877. i_size_write(inode, iocb->ki_pos);
  878. spin_unlock(&inode->i_lock);
  879. }
  880. out:
  881. return retval;
  882. }
  883. /**
  884. * nfs_init_directcache - create a slab cache for nfs_direct_req structures
  885. *
  886. */
  887. int __init nfs_init_directcache(void)
  888. {
  889. nfs_direct_cachep = kmem_cache_create("nfs_direct_cache",
  890. sizeof(struct nfs_direct_req),
  891. 0, (SLAB_RECLAIM_ACCOUNT|
  892. SLAB_MEM_SPREAD),
  893. NULL);
  894. if (nfs_direct_cachep == NULL)
  895. return -ENOMEM;
  896. return 0;
  897. }
  898. /**
  899. * nfs_destroy_directcache - destroy the slab cache for nfs_direct_req structures
  900. *
  901. */
  902. void nfs_destroy_directcache(void)
  903. {
  904. kmem_cache_destroy(nfs_direct_cachep);
  905. }