read.c 15 KB

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