read.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. list_add(&data->pages, &list);
  222. requests++;
  223. nbytes -= len;
  224. } while(nbytes != 0);
  225. atomic_set(&req->wb_complete, requests);
  226. ClearPageError(page);
  227. offset = 0;
  228. nbytes = count;
  229. do {
  230. data = list_entry(list.next, struct nfs_read_data, pages);
  231. list_del_init(&data->pages);
  232. data->pagevec[0] = page;
  233. if (nbytes < rsize)
  234. rsize = nbytes;
  235. nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
  236. rsize, offset);
  237. offset += rsize;
  238. nbytes -= rsize;
  239. } while (nbytes != 0);
  240. return 0;
  241. out_bad:
  242. while (!list_empty(&list)) {
  243. data = list_entry(list.next, struct nfs_read_data, pages);
  244. list_del(&data->pages);
  245. nfs_readdata_free(data);
  246. }
  247. SetPageError(page);
  248. nfs_readpage_release(req);
  249. return -ENOMEM;
  250. }
  251. static int nfs_pagein_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags)
  252. {
  253. struct nfs_page *req;
  254. struct page **pages;
  255. struct nfs_read_data *data;
  256. data = nfs_readdata_alloc(npages);
  257. if (!data)
  258. goto out_bad;
  259. pages = data->pagevec;
  260. while (!list_empty(head)) {
  261. req = nfs_list_entry(head->next);
  262. nfs_list_remove_request(req);
  263. nfs_list_add_request(req, &data->pages);
  264. ClearPageError(req->wb_page);
  265. *pages++ = req->wb_page;
  266. }
  267. req = nfs_list_entry(data->pages.next);
  268. nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
  269. return 0;
  270. out_bad:
  271. nfs_async_read_error(head);
  272. return -ENOMEM;
  273. }
  274. /*
  275. * This is the callback from RPC telling us whether a reply was
  276. * received or some error occurred (timeout or socket shutdown).
  277. */
  278. int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
  279. {
  280. int status;
  281. dprintk("NFS: %s: %5u, (status %d)\n", __FUNCTION__, task->tk_pid,
  282. task->tk_status);
  283. status = NFS_PROTO(data->inode)->read_done(task, data);
  284. if (status != 0)
  285. return status;
  286. nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
  287. if (task->tk_status == -ESTALE) {
  288. set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags);
  289. nfs_mark_for_revalidate(data->inode);
  290. }
  291. return 0;
  292. }
  293. static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
  294. {
  295. struct nfs_readargs *argp = &data->args;
  296. struct nfs_readres *resp = &data->res;
  297. if (resp->eof || resp->count == argp->count)
  298. return 0;
  299. /* This is a short read! */
  300. nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
  301. /* Has the server at least made some progress? */
  302. if (resp->count == 0)
  303. return 0;
  304. /* Yes, so retry the read at the end of the data */
  305. argp->offset += resp->count;
  306. argp->pgbase += resp->count;
  307. argp->count -= resp->count;
  308. rpc_restart_call(task);
  309. return -EAGAIN;
  310. }
  311. /*
  312. * Handle a read reply that fills part of a page.
  313. */
  314. static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
  315. {
  316. struct nfs_read_data *data = calldata;
  317. struct nfs_page *req = data->req;
  318. struct page *page = req->wb_page;
  319. if (nfs_readpage_result(task, data) != 0)
  320. return;
  321. if (likely(task->tk_status >= 0)) {
  322. nfs_readpage_truncate_uninitialised_page(data);
  323. if (nfs_readpage_retry(task, data) != 0)
  324. return;
  325. }
  326. if (unlikely(task->tk_status < 0))
  327. SetPageError(page);
  328. if (atomic_dec_and_test(&req->wb_complete)) {
  329. if (!PageError(page))
  330. SetPageUptodate(page);
  331. nfs_readpage_release(req);
  332. }
  333. }
  334. static const struct rpc_call_ops nfs_read_partial_ops = {
  335. .rpc_call_done = nfs_readpage_result_partial,
  336. .rpc_release = nfs_readdata_release,
  337. };
  338. static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
  339. {
  340. unsigned int count = data->res.count;
  341. unsigned int base = data->args.pgbase;
  342. struct page **pages;
  343. if (data->res.eof)
  344. count = data->args.count;
  345. if (unlikely(count == 0))
  346. return;
  347. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  348. base &= ~PAGE_CACHE_MASK;
  349. count += base;
  350. for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
  351. SetPageUptodate(*pages);
  352. if (count == 0)
  353. return;
  354. /* Was this a short read? */
  355. if (data->res.eof || data->res.count == data->args.count)
  356. SetPageUptodate(*pages);
  357. }
  358. /*
  359. * This is the callback from RPC telling us whether a reply was
  360. * received or some error occurred (timeout or socket shutdown).
  361. */
  362. static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
  363. {
  364. struct nfs_read_data *data = calldata;
  365. if (nfs_readpage_result(task, data) != 0)
  366. return;
  367. /*
  368. * Note: nfs_readpage_retry may change the values of
  369. * data->args. In the multi-page case, we therefore need
  370. * to ensure that we call nfs_readpage_set_pages_uptodate()
  371. * first.
  372. */
  373. if (likely(task->tk_status >= 0)) {
  374. nfs_readpage_truncate_uninitialised_page(data);
  375. nfs_readpage_set_pages_uptodate(data);
  376. if (nfs_readpage_retry(task, data) != 0)
  377. return;
  378. }
  379. while (!list_empty(&data->pages)) {
  380. struct nfs_page *req = nfs_list_entry(data->pages.next);
  381. nfs_list_remove_request(req);
  382. nfs_readpage_release(req);
  383. }
  384. }
  385. static const struct rpc_call_ops nfs_read_full_ops = {
  386. .rpc_call_done = nfs_readpage_result_full,
  387. .rpc_release = nfs_readdata_release,
  388. };
  389. /*
  390. * Read a page over NFS.
  391. * We read the page synchronously in the following case:
  392. * - The error flag is set for this page. This happens only when a
  393. * previous async read operation failed.
  394. */
  395. int nfs_readpage(struct file *file, struct page *page)
  396. {
  397. struct nfs_open_context *ctx;
  398. struct inode *inode = page->mapping->host;
  399. int error;
  400. dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
  401. page, PAGE_CACHE_SIZE, page->index);
  402. nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
  403. nfs_add_stats(inode, NFSIOS_READPAGES, 1);
  404. /*
  405. * Try to flush any pending writes to the file..
  406. *
  407. * NOTE! Because we own the page lock, there cannot
  408. * be any new pending writes generated at this point
  409. * for this page (other pages can be written to).
  410. */
  411. error = nfs_wb_page(inode, page);
  412. if (error)
  413. goto out_unlock;
  414. if (PageUptodate(page))
  415. goto out_unlock;
  416. error = -ESTALE;
  417. if (NFS_STALE(inode))
  418. goto out_unlock;
  419. if (file == NULL) {
  420. error = -EBADF;
  421. ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  422. if (ctx == NULL)
  423. goto out_unlock;
  424. } else
  425. ctx = get_nfs_open_context(nfs_file_open_context(file));
  426. error = nfs_readpage_async(ctx, inode, page);
  427. put_nfs_open_context(ctx);
  428. return error;
  429. out_unlock:
  430. unlock_page(page);
  431. return error;
  432. }
  433. struct nfs_readdesc {
  434. struct nfs_pageio_descriptor *pgio;
  435. struct nfs_open_context *ctx;
  436. };
  437. static int
  438. readpage_async_filler(void *data, struct page *page)
  439. {
  440. struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
  441. struct inode *inode = page->mapping->host;
  442. struct nfs_page *new;
  443. unsigned int len;
  444. int error;
  445. error = nfs_wb_page(inode, page);
  446. if (error)
  447. goto out_unlock;
  448. if (PageUptodate(page))
  449. goto out_unlock;
  450. len = nfs_page_length(page);
  451. if (len == 0)
  452. return nfs_return_empty_page(page);
  453. new = nfs_create_request(desc->ctx, inode, page, 0, len);
  454. if (IS_ERR(new))
  455. goto out_error;
  456. if (len < PAGE_CACHE_SIZE)
  457. zero_user_segment(page, len, PAGE_CACHE_SIZE);
  458. if (!nfs_pageio_add_request(desc->pgio, new)) {
  459. error = desc->pgio->pg_error;
  460. goto out_unlock;
  461. }
  462. return 0;
  463. out_error:
  464. error = PTR_ERR(new);
  465. SetPageError(page);
  466. out_unlock:
  467. unlock_page(page);
  468. return error;
  469. }
  470. int nfs_readpages(struct file *filp, struct address_space *mapping,
  471. struct list_head *pages, unsigned nr_pages)
  472. {
  473. struct nfs_pageio_descriptor pgio;
  474. struct nfs_readdesc desc = {
  475. .pgio = &pgio,
  476. };
  477. struct inode *inode = mapping->host;
  478. struct nfs_server *server = NFS_SERVER(inode);
  479. size_t rsize = server->rsize;
  480. unsigned long npages;
  481. int ret = -ESTALE;
  482. dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
  483. inode->i_sb->s_id,
  484. (long long)NFS_FILEID(inode),
  485. nr_pages);
  486. nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
  487. if (NFS_STALE(inode))
  488. goto out;
  489. if (filp == NULL) {
  490. desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  491. if (desc.ctx == NULL)
  492. return -EBADF;
  493. } else
  494. desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
  495. if (rsize < PAGE_CACHE_SIZE)
  496. nfs_pageio_init(&pgio, inode, nfs_pagein_multi, rsize, 0);
  497. else
  498. nfs_pageio_init(&pgio, inode, nfs_pagein_one, rsize, 0);
  499. ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
  500. nfs_pageio_complete(&pgio);
  501. npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  502. nfs_add_stats(inode, NFSIOS_READPAGES, npages);
  503. put_nfs_open_context(desc.ctx);
  504. out:
  505. return ret;
  506. }
  507. int __init nfs_init_readpagecache(void)
  508. {
  509. nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
  510. sizeof(struct nfs_read_data),
  511. 0, SLAB_HWCACHE_ALIGN,
  512. NULL);
  513. if (nfs_rdata_cachep == NULL)
  514. return -ENOMEM;
  515. nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
  516. nfs_rdata_cachep);
  517. if (nfs_rdata_mempool == NULL)
  518. return -ENOMEM;
  519. return 0;
  520. }
  521. void nfs_destroy_readpagecache(void)
  522. {
  523. mempool_destroy(nfs_rdata_mempool);
  524. kmem_cache_destroy(nfs_rdata_cachep);
  525. }