direct.c 27 KB

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