read.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * linux/fs/nfs/read.c
  3. *
  4. * Block I/O for NFS
  5. *
  6. * Partial copy of Linus' read cache modifications to fs/nfs/file.c
  7. * modified for async RPC by okir@monad.swb.de
  8. */
  9. #include <linux/time.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/fcntl.h>
  13. #include <linux/stat.h>
  14. #include <linux/mm.h>
  15. #include <linux/slab.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/sunrpc/clnt.h>
  18. #include <linux/nfs_fs.h>
  19. #include <linux/nfs_page.h>
  20. #include <linux/smp_lock.h>
  21. #include <asm/system.h>
  22. #include "internal.h"
  23. #include "iostat.h"
  24. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  25. static int nfs_pagein_multi(struct inode *, struct list_head *, unsigned int, size_t, int);
  26. static int nfs_pagein_one(struct inode *, struct list_head *, unsigned int, size_t, int);
  27. static const struct rpc_call_ops nfs_read_partial_ops;
  28. static const struct rpc_call_ops nfs_read_full_ops;
  29. static struct kmem_cache *nfs_rdata_cachep;
  30. static mempool_t *nfs_rdata_mempool;
  31. #define MIN_POOL_READ (32)
  32. struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount)
  33. {
  34. struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, GFP_NOFS);
  35. if (p) {
  36. memset(p, 0, sizeof(*p));
  37. INIT_LIST_HEAD(&p->pages);
  38. p->npages = pagecount;
  39. if (pagecount <= ARRAY_SIZE(p->page_array))
  40. p->pagevec = p->page_array;
  41. else {
  42. p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
  43. if (!p->pagevec) {
  44. mempool_free(p, nfs_rdata_mempool);
  45. p = NULL;
  46. }
  47. }
  48. }
  49. return p;
  50. }
  51. static void nfs_readdata_rcu_free(struct rcu_head *head)
  52. {
  53. struct nfs_read_data *p = container_of(head, struct nfs_read_data, task.u.tk_rcu);
  54. if (p && (p->pagevec != &p->page_array[0]))
  55. kfree(p->pagevec);
  56. mempool_free(p, nfs_rdata_mempool);
  57. }
  58. static void nfs_readdata_free(struct nfs_read_data *rdata)
  59. {
  60. call_rcu_bh(&rdata->task.u.tk_rcu, nfs_readdata_rcu_free);
  61. }
  62. void nfs_readdata_release(void *data)
  63. {
  64. struct nfs_read_data *rdata = data;
  65. put_nfs_open_context(rdata->args.context);
  66. nfs_readdata_free(rdata);
  67. }
  68. static
  69. int nfs_return_empty_page(struct page *page)
  70. {
  71. zero_user(page, 0, PAGE_CACHE_SIZE);
  72. SetPageUptodate(page);
  73. unlock_page(page);
  74. return 0;
  75. }
  76. static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
  77. {
  78. unsigned int remainder = data->args.count - data->res.count;
  79. unsigned int base = data->args.pgbase + data->res.count;
  80. unsigned int pglen;
  81. struct page **pages;
  82. if (data->res.eof == 0 || remainder == 0)
  83. return;
  84. /*
  85. * Note: "remainder" can never be negative, since we check for
  86. * this in the XDR code.
  87. */
  88. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  89. base &= ~PAGE_CACHE_MASK;
  90. pglen = PAGE_CACHE_SIZE - base;
  91. for (;;) {
  92. if (remainder <= pglen) {
  93. zero_user(*pages, base, remainder);
  94. break;
  95. }
  96. zero_user(*pages, base, pglen);
  97. pages++;
  98. remainder -= pglen;
  99. pglen = PAGE_CACHE_SIZE;
  100. base = 0;
  101. }
  102. }
  103. static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
  104. struct page *page)
  105. {
  106. LIST_HEAD(one_request);
  107. struct nfs_page *new;
  108. unsigned int len;
  109. len = nfs_page_length(page);
  110. if (len == 0)
  111. return nfs_return_empty_page(page);
  112. new = nfs_create_request(ctx, inode, page, 0, len);
  113. if (IS_ERR(new)) {
  114. unlock_page(page);
  115. return PTR_ERR(new);
  116. }
  117. if (len < PAGE_CACHE_SIZE)
  118. zero_user_segment(page, len, PAGE_CACHE_SIZE);
  119. nfs_list_add_request(new, &one_request);
  120. if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
  121. nfs_pagein_multi(inode, &one_request, 1, len, 0);
  122. else
  123. nfs_pagein_one(inode, &one_request, 1, len, 0);
  124. return 0;
  125. }
  126. static void nfs_readpage_release(struct nfs_page *req)
  127. {
  128. unlock_page(req->wb_page);
  129. dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
  130. req->wb_context->path.dentry->d_inode->i_sb->s_id,
  131. (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
  132. req->wb_bytes,
  133. (long long)req_offset(req));
  134. nfs_clear_request(req);
  135. nfs_release_request(req);
  136. }
  137. /*
  138. * Set up the NFS read request struct
  139. */
  140. static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
  141. const struct rpc_call_ops *call_ops,
  142. unsigned int count, unsigned int offset)
  143. {
  144. struct inode *inode = req->wb_context->path.dentry->d_inode;
  145. int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
  146. struct rpc_task *task;
  147. struct rpc_message msg = {
  148. .rpc_argp = &data->args,
  149. .rpc_resp = &data->res,
  150. .rpc_cred = req->wb_context->cred,
  151. };
  152. struct rpc_task_setup task_setup_data = {
  153. .task = &data->task,
  154. .rpc_client = NFS_CLIENT(inode),
  155. .rpc_message = &msg,
  156. .callback_ops = call_ops,
  157. .callback_data = data,
  158. .flags = RPC_TASK_ASYNC | swap_flags,
  159. };
  160. data->req = req;
  161. data->inode = inode;
  162. data->cred = msg.rpc_cred;
  163. data->args.fh = NFS_FH(inode);
  164. data->args.offset = req_offset(req) + offset;
  165. data->args.pgbase = req->wb_pgbase + offset;
  166. data->args.pages = data->pagevec;
  167. data->args.count = count;
  168. data->args.context = get_nfs_open_context(req->wb_context);
  169. data->res.fattr = &data->fattr;
  170. data->res.count = count;
  171. data->res.eof = 0;
  172. nfs_fattr_init(&data->fattr);
  173. /* Set up the initial task struct. */
  174. NFS_PROTO(inode)->read_setup(data, &msg);
  175. dprintk("NFS: %5u initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  176. data->task.tk_pid,
  177. inode->i_sb->s_id,
  178. (long long)NFS_FILEID(inode),
  179. count,
  180. (unsigned long long)data->args.offset);
  181. task = rpc_run_task(&task_setup_data);
  182. if (!IS_ERR(task))
  183. rpc_put_task(task);
  184. }
  185. static void
  186. nfs_async_read_error(struct list_head *head)
  187. {
  188. struct nfs_page *req;
  189. while (!list_empty(head)) {
  190. req = nfs_list_entry(head->next);
  191. nfs_list_remove_request(req);
  192. SetPageError(req->wb_page);
  193. nfs_readpage_release(req);
  194. }
  195. }
  196. /*
  197. * Generate multiple requests to fill a single page.
  198. *
  199. * We optimize to reduce the number of read operations on the wire. If we
  200. * detect that we're reading a page, or an area of a page, that is past the
  201. * end of file, we do not generate NFS read operations but just clear the
  202. * parts of the page that would have come back zero from the server anyway.
  203. *
  204. * We rely on the cached value of i_size to make this determination; another
  205. * client can fill pages on the server past our cached end-of-file, but we
  206. * won't see the new data until our attribute cache is updated. This is more
  207. * or less conventional NFS client behavior.
  208. */
  209. static int nfs_pagein_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags)
  210. {
  211. struct nfs_page *req = nfs_list_entry(head->next);
  212. struct page *page = req->wb_page;
  213. struct nfs_read_data *data;
  214. size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
  215. unsigned int offset;
  216. int requests = 0;
  217. LIST_HEAD(list);
  218. nfs_list_remove_request(req);
  219. nbytes = count;
  220. do {
  221. size_t len = min(nbytes,rsize);
  222. data = nfs_readdata_alloc(1);
  223. if (!data)
  224. goto out_bad;
  225. INIT_LIST_HEAD(&data->pages);
  226. list_add(&data->pages, &list);
  227. requests++;
  228. nbytes -= len;
  229. } while(nbytes != 0);
  230. atomic_set(&req->wb_complete, requests);
  231. ClearPageError(page);
  232. offset = 0;
  233. nbytes = count;
  234. do {
  235. data = list_entry(list.next, struct nfs_read_data, pages);
  236. list_del_init(&data->pages);
  237. data->pagevec[0] = page;
  238. if (nbytes < rsize)
  239. rsize = nbytes;
  240. nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
  241. rsize, offset);
  242. offset += rsize;
  243. nbytes -= rsize;
  244. } while (nbytes != 0);
  245. return 0;
  246. out_bad:
  247. while (!list_empty(&list)) {
  248. data = list_entry(list.next, struct nfs_read_data, pages);
  249. list_del(&data->pages);
  250. nfs_readdata_free(data);
  251. }
  252. SetPageError(page);
  253. nfs_readpage_release(req);
  254. return -ENOMEM;
  255. }
  256. static int nfs_pagein_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags)
  257. {
  258. struct nfs_page *req;
  259. struct page **pages;
  260. struct nfs_read_data *data;
  261. data = nfs_readdata_alloc(npages);
  262. if (!data)
  263. goto out_bad;
  264. INIT_LIST_HEAD(&data->pages);
  265. pages = data->pagevec;
  266. while (!list_empty(head)) {
  267. req = nfs_list_entry(head->next);
  268. nfs_list_remove_request(req);
  269. nfs_list_add_request(req, &data->pages);
  270. ClearPageError(req->wb_page);
  271. *pages++ = req->wb_page;
  272. }
  273. req = nfs_list_entry(data->pages.next);
  274. nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
  275. return 0;
  276. out_bad:
  277. nfs_async_read_error(head);
  278. return -ENOMEM;
  279. }
  280. /*
  281. * This is the callback from RPC telling us whether a reply was
  282. * received or some error occurred (timeout or socket shutdown).
  283. */
  284. int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
  285. {
  286. int status;
  287. dprintk("NFS: %s: %5u, (status %d)\n", __FUNCTION__, task->tk_pid,
  288. task->tk_status);
  289. status = NFS_PROTO(data->inode)->read_done(task, data);
  290. if (status != 0)
  291. return status;
  292. nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
  293. if (task->tk_status == -ESTALE) {
  294. set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags);
  295. nfs_mark_for_revalidate(data->inode);
  296. }
  297. return 0;
  298. }
  299. static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
  300. {
  301. struct nfs_readargs *argp = &data->args;
  302. struct nfs_readres *resp = &data->res;
  303. if (resp->eof || resp->count == argp->count)
  304. return 0;
  305. /* This is a short read! */
  306. nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
  307. /* Has the server at least made some progress? */
  308. if (resp->count == 0)
  309. return 0;
  310. /* Yes, so retry the read at the end of the data */
  311. argp->offset += resp->count;
  312. argp->pgbase += resp->count;
  313. argp->count -= resp->count;
  314. rpc_restart_call(task);
  315. return -EAGAIN;
  316. }
  317. /*
  318. * Handle a read reply that fills part of a page.
  319. */
  320. static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
  321. {
  322. struct nfs_read_data *data = calldata;
  323. struct nfs_page *req = data->req;
  324. struct page *page = req->wb_page;
  325. if (nfs_readpage_result(task, data) != 0)
  326. return;
  327. if (likely(task->tk_status >= 0)) {
  328. nfs_readpage_truncate_uninitialised_page(data);
  329. if (nfs_readpage_retry(task, data) != 0)
  330. return;
  331. }
  332. if (unlikely(task->tk_status < 0))
  333. SetPageError(page);
  334. if (atomic_dec_and_test(&req->wb_complete)) {
  335. if (!PageError(page))
  336. SetPageUptodate(page);
  337. nfs_readpage_release(req);
  338. }
  339. }
  340. static const struct rpc_call_ops nfs_read_partial_ops = {
  341. .rpc_call_done = nfs_readpage_result_partial,
  342. .rpc_release = nfs_readdata_release,
  343. };
  344. static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
  345. {
  346. unsigned int count = data->res.count;
  347. unsigned int base = data->args.pgbase;
  348. struct page **pages;
  349. if (data->res.eof)
  350. count = data->args.count;
  351. if (unlikely(count == 0))
  352. return;
  353. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  354. base &= ~PAGE_CACHE_MASK;
  355. count += base;
  356. for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
  357. SetPageUptodate(*pages);
  358. if (count == 0)
  359. return;
  360. /* Was this a short read? */
  361. if (data->res.eof || data->res.count == data->args.count)
  362. SetPageUptodate(*pages);
  363. }
  364. /*
  365. * This is the callback from RPC telling us whether a reply was
  366. * received or some error occurred (timeout or socket shutdown).
  367. */
  368. static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
  369. {
  370. struct nfs_read_data *data = calldata;
  371. if (nfs_readpage_result(task, data) != 0)
  372. return;
  373. /*
  374. * Note: nfs_readpage_retry may change the values of
  375. * data->args. In the multi-page case, we therefore need
  376. * to ensure that we call nfs_readpage_set_pages_uptodate()
  377. * first.
  378. */
  379. if (likely(task->tk_status >= 0)) {
  380. nfs_readpage_truncate_uninitialised_page(data);
  381. nfs_readpage_set_pages_uptodate(data);
  382. if (nfs_readpage_retry(task, data) != 0)
  383. return;
  384. }
  385. while (!list_empty(&data->pages)) {
  386. struct nfs_page *req = nfs_list_entry(data->pages.next);
  387. nfs_list_remove_request(req);
  388. nfs_readpage_release(req);
  389. }
  390. }
  391. static const struct rpc_call_ops nfs_read_full_ops = {
  392. .rpc_call_done = nfs_readpage_result_full,
  393. .rpc_release = nfs_readdata_release,
  394. };
  395. /*
  396. * Read a page over NFS.
  397. * We read the page synchronously in the following case:
  398. * - The error flag is set for this page. This happens only when a
  399. * previous async read operation failed.
  400. */
  401. int nfs_readpage(struct file *file, struct page *page)
  402. {
  403. struct nfs_open_context *ctx;
  404. struct inode *inode = page->mapping->host;
  405. int error;
  406. dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
  407. page, PAGE_CACHE_SIZE, page->index);
  408. nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
  409. nfs_add_stats(inode, NFSIOS_READPAGES, 1);
  410. /*
  411. * Try to flush any pending writes to the file..
  412. *
  413. * NOTE! Because we own the page lock, there cannot
  414. * be any new pending writes generated at this point
  415. * for this page (other pages can be written to).
  416. */
  417. error = nfs_wb_page(inode, page);
  418. if (error)
  419. goto out_unlock;
  420. if (PageUptodate(page))
  421. goto out_unlock;
  422. error = -ESTALE;
  423. if (NFS_STALE(inode))
  424. goto out_unlock;
  425. if (file == NULL) {
  426. error = -EBADF;
  427. ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  428. if (ctx == NULL)
  429. goto out_unlock;
  430. } else
  431. ctx = get_nfs_open_context(nfs_file_open_context(file));
  432. error = nfs_readpage_async(ctx, inode, page);
  433. put_nfs_open_context(ctx);
  434. return error;
  435. out_unlock:
  436. unlock_page(page);
  437. return error;
  438. }
  439. struct nfs_readdesc {
  440. struct nfs_pageio_descriptor *pgio;
  441. struct nfs_open_context *ctx;
  442. };
  443. static int
  444. readpage_async_filler(void *data, struct page *page)
  445. {
  446. struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
  447. struct inode *inode = page->mapping->host;
  448. struct nfs_page *new;
  449. unsigned int len;
  450. int error;
  451. error = nfs_wb_page(inode, page);
  452. if (error)
  453. goto out_unlock;
  454. if (PageUptodate(page))
  455. goto out_unlock;
  456. len = nfs_page_length(page);
  457. if (len == 0)
  458. return nfs_return_empty_page(page);
  459. new = nfs_create_request(desc->ctx, inode, page, 0, len);
  460. if (IS_ERR(new))
  461. goto out_error;
  462. if (len < PAGE_CACHE_SIZE)
  463. zero_user_segment(page, len, PAGE_CACHE_SIZE);
  464. nfs_pageio_add_request(desc->pgio, new);
  465. return 0;
  466. out_error:
  467. error = PTR_ERR(new);
  468. SetPageError(page);
  469. out_unlock:
  470. unlock_page(page);
  471. return error;
  472. }
  473. int nfs_readpages(struct file *filp, struct address_space *mapping,
  474. struct list_head *pages, unsigned nr_pages)
  475. {
  476. struct nfs_pageio_descriptor pgio;
  477. struct nfs_readdesc desc = {
  478. .pgio = &pgio,
  479. };
  480. struct inode *inode = mapping->host;
  481. struct nfs_server *server = NFS_SERVER(inode);
  482. size_t rsize = server->rsize;
  483. unsigned long npages;
  484. int ret = -ESTALE;
  485. dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
  486. inode->i_sb->s_id,
  487. (long long)NFS_FILEID(inode),
  488. nr_pages);
  489. nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
  490. if (NFS_STALE(inode))
  491. goto out;
  492. if (filp == NULL) {
  493. desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  494. if (desc.ctx == NULL)
  495. return -EBADF;
  496. } else
  497. desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
  498. if (rsize < PAGE_CACHE_SIZE)
  499. nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0);
  500. else
  501. nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0);
  502. ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
  503. nfs_pageio_complete(&pgio);
  504. npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  505. nfs_add_stats(inode, NFSIOS_READPAGES, npages);
  506. put_nfs_open_context(desc.ctx);
  507. out:
  508. return ret;
  509. }
  510. int __init nfs_init_readpagecache(void)
  511. {
  512. nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
  513. sizeof(struct nfs_read_data),
  514. 0, SLAB_HWCACHE_ALIGN,
  515. NULL);
  516. if (nfs_rdata_cachep == NULL)
  517. return -ENOMEM;
  518. nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
  519. nfs_rdata_cachep);
  520. if (nfs_rdata_mempool == NULL)
  521. return -ENOMEM;
  522. return 0;
  523. }
  524. void nfs_destroy_readpagecache(void)
  525. {
  526. mempool_destroy(nfs_rdata_mempool);
  527. kmem_cache_destroy(nfs_rdata_cachep);
  528. }