read.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. nfs_readdata_free(data);
  65. }
  66. static
  67. int nfs_return_empty_page(struct page *page)
  68. {
  69. zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
  70. SetPageUptodate(page);
  71. unlock_page(page);
  72. return 0;
  73. }
  74. static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
  75. {
  76. unsigned int remainder = data->args.count - data->res.count;
  77. unsigned int base = data->args.pgbase + data->res.count;
  78. unsigned int pglen;
  79. struct page **pages;
  80. if (data->res.eof == 0 || remainder == 0)
  81. return;
  82. /*
  83. * Note: "remainder" can never be negative, since we check for
  84. * this in the XDR code.
  85. */
  86. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  87. base &= ~PAGE_CACHE_MASK;
  88. pglen = PAGE_CACHE_SIZE - base;
  89. for (;;) {
  90. if (remainder <= pglen) {
  91. zero_user_page(*pages, base, remainder, KM_USER0);
  92. break;
  93. }
  94. zero_user_page(*pages, base, pglen, KM_USER0);
  95. pages++;
  96. remainder -= pglen;
  97. pglen = PAGE_CACHE_SIZE;
  98. base = 0;
  99. }
  100. }
  101. static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
  102. struct page *page)
  103. {
  104. LIST_HEAD(one_request);
  105. struct nfs_page *new;
  106. unsigned int len;
  107. len = nfs_page_length(page);
  108. if (len == 0)
  109. return nfs_return_empty_page(page);
  110. new = nfs_create_request(ctx, inode, page, 0, len);
  111. if (IS_ERR(new)) {
  112. unlock_page(page);
  113. return PTR_ERR(new);
  114. }
  115. if (len < PAGE_CACHE_SIZE)
  116. zero_user_page(page, len, PAGE_CACHE_SIZE - len, KM_USER0);
  117. nfs_list_add_request(new, &one_request);
  118. if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
  119. nfs_pagein_multi(inode, &one_request, 1, len, 0);
  120. else
  121. nfs_pagein_one(inode, &one_request, 1, len, 0);
  122. return 0;
  123. }
  124. static void nfs_readpage_release(struct nfs_page *req)
  125. {
  126. unlock_page(req->wb_page);
  127. dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
  128. req->wb_context->path.dentry->d_inode->i_sb->s_id,
  129. (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
  130. req->wb_bytes,
  131. (long long)req_offset(req));
  132. nfs_clear_request(req);
  133. nfs_release_request(req);
  134. }
  135. /*
  136. * Set up the NFS read request struct
  137. */
  138. static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
  139. const struct rpc_call_ops *call_ops,
  140. unsigned int count, unsigned int offset)
  141. {
  142. struct inode *inode;
  143. int flags;
  144. data->req = req;
  145. data->inode = inode = req->wb_context->path.dentry->d_inode;
  146. data->cred = req->wb_context->cred;
  147. data->args.fh = NFS_FH(inode);
  148. data->args.offset = req_offset(req) + offset;
  149. data->args.pgbase = req->wb_pgbase + offset;
  150. data->args.pages = data->pagevec;
  151. data->args.count = count;
  152. data->args.context = req->wb_context;
  153. data->res.fattr = &data->fattr;
  154. data->res.count = count;
  155. data->res.eof = 0;
  156. nfs_fattr_init(&data->fattr);
  157. /* Set up the initial task struct. */
  158. flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
  159. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
  160. NFS_PROTO(inode)->read_setup(data);
  161. data->task.tk_cookie = (unsigned long)inode;
  162. dprintk("NFS: %5u initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  163. data->task.tk_pid,
  164. inode->i_sb->s_id,
  165. (long long)NFS_FILEID(inode),
  166. count,
  167. (unsigned long long)data->args.offset);
  168. }
  169. static void
  170. nfs_async_read_error(struct list_head *head)
  171. {
  172. struct nfs_page *req;
  173. while (!list_empty(head)) {
  174. req = nfs_list_entry(head->next);
  175. nfs_list_remove_request(req);
  176. SetPageError(req->wb_page);
  177. nfs_readpage_release(req);
  178. }
  179. }
  180. /*
  181. * Start an async read operation
  182. */
  183. static void nfs_execute_read(struct nfs_read_data *data)
  184. {
  185. struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
  186. sigset_t oldset;
  187. rpc_clnt_sigmask(clnt, &oldset);
  188. rpc_execute(&data->task);
  189. rpc_clnt_sigunmask(clnt, &oldset);
  190. }
  191. /*
  192. * Generate multiple requests to fill a single page.
  193. *
  194. * We optimize to reduce the number of read operations on the wire. If we
  195. * detect that we're reading a page, or an area of a page, that is past the
  196. * end of file, we do not generate NFS read operations but just clear the
  197. * parts of the page that would have come back zero from the server anyway.
  198. *
  199. * We rely on the cached value of i_size to make this determination; another
  200. * client can fill pages on the server past our cached end-of-file, but we
  201. * won't see the new data until our attribute cache is updated. This is more
  202. * or less conventional NFS client behavior.
  203. */
  204. static int nfs_pagein_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int flags)
  205. {
  206. struct nfs_page *req = nfs_list_entry(head->next);
  207. struct page *page = req->wb_page;
  208. struct nfs_read_data *data;
  209. size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
  210. unsigned int offset;
  211. int requests = 0;
  212. LIST_HEAD(list);
  213. nfs_list_remove_request(req);
  214. nbytes = count;
  215. do {
  216. size_t len = min(nbytes,rsize);
  217. data = nfs_readdata_alloc(1);
  218. if (!data)
  219. goto out_bad;
  220. INIT_LIST_HEAD(&data->pages);
  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. nfs_execute_read(data);
  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. nfs_execute_read(data);
  272. return 0;
  273. out_bad:
  274. nfs_async_read_error(head);
  275. return -ENOMEM;
  276. }
  277. /*
  278. * This is the callback from RPC telling us whether a reply was
  279. * received or some error occurred (timeout or socket shutdown).
  280. */
  281. int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
  282. {
  283. int status;
  284. dprintk("NFS: %s: %5u, (status %d)\n", __FUNCTION__, task->tk_pid,
  285. task->tk_status);
  286. status = NFS_PROTO(data->inode)->read_done(task, data);
  287. if (status != 0)
  288. return status;
  289. nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
  290. if (task->tk_status == -ESTALE) {
  291. set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode));
  292. nfs_mark_for_revalidate(data->inode);
  293. }
  294. return 0;
  295. }
  296. static int nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
  297. {
  298. struct nfs_readargs *argp = &data->args;
  299. struct nfs_readres *resp = &data->res;
  300. if (resp->eof || resp->count == argp->count)
  301. return 0;
  302. /* This is a short read! */
  303. nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
  304. /* Has the server at least made some progress? */
  305. if (resp->count == 0)
  306. return 0;
  307. /* Yes, so retry the read at the end of the data */
  308. argp->offset += resp->count;
  309. argp->pgbase += resp->count;
  310. argp->count -= resp->count;
  311. rpc_restart_call(task);
  312. return -EAGAIN;
  313. }
  314. /*
  315. * Handle a read reply that fills part of a page.
  316. */
  317. static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
  318. {
  319. struct nfs_read_data *data = calldata;
  320. struct nfs_page *req = data->req;
  321. struct page *page = req->wb_page;
  322. if (nfs_readpage_result(task, data) != 0)
  323. return;
  324. if (likely(task->tk_status >= 0)) {
  325. nfs_readpage_truncate_uninitialised_page(data);
  326. if (nfs_readpage_retry(task, data) != 0)
  327. return;
  328. }
  329. if (unlikely(task->tk_status < 0))
  330. SetPageError(page);
  331. if (atomic_dec_and_test(&req->wb_complete)) {
  332. if (!PageError(page))
  333. SetPageUptodate(page);
  334. nfs_readpage_release(req);
  335. }
  336. }
  337. static const struct rpc_call_ops nfs_read_partial_ops = {
  338. .rpc_call_done = nfs_readpage_result_partial,
  339. .rpc_release = nfs_readdata_release,
  340. };
  341. static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
  342. {
  343. unsigned int count = data->res.count;
  344. unsigned int base = data->args.pgbase;
  345. struct page **pages;
  346. if (data->res.eof)
  347. count = data->args.count;
  348. if (unlikely(count == 0))
  349. return;
  350. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  351. base &= ~PAGE_CACHE_MASK;
  352. count += base;
  353. for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
  354. SetPageUptodate(*pages);
  355. if (count == 0)
  356. return;
  357. /* Was this a short read? */
  358. if (data->res.eof || data->res.count == data->args.count)
  359. SetPageUptodate(*pages);
  360. }
  361. /*
  362. * This is the callback from RPC telling us whether a reply was
  363. * received or some error occurred (timeout or socket shutdown).
  364. */
  365. static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
  366. {
  367. struct nfs_read_data *data = calldata;
  368. if (nfs_readpage_result(task, data) != 0)
  369. return;
  370. /*
  371. * Note: nfs_readpage_retry may change the values of
  372. * data->args. In the multi-page case, we therefore need
  373. * to ensure that we call nfs_readpage_set_pages_uptodate()
  374. * first.
  375. */
  376. if (likely(task->tk_status >= 0)) {
  377. nfs_readpage_truncate_uninitialised_page(data);
  378. nfs_readpage_set_pages_uptodate(data);
  379. if (nfs_readpage_retry(task, data) != 0)
  380. return;
  381. }
  382. while (!list_empty(&data->pages)) {
  383. struct nfs_page *req = nfs_list_entry(data->pages.next);
  384. nfs_list_remove_request(req);
  385. nfs_readpage_release(req);
  386. }
  387. }
  388. static const struct rpc_call_ops nfs_read_full_ops = {
  389. .rpc_call_done = nfs_readpage_result_full,
  390. .rpc_release = nfs_readdata_release,
  391. };
  392. /*
  393. * Read a page over NFS.
  394. * We read the page synchronously in the following case:
  395. * - The error flag is set for this page. This happens only when a
  396. * previous async read operation failed.
  397. */
  398. int nfs_readpage(struct file *file, struct page *page)
  399. {
  400. struct nfs_open_context *ctx;
  401. struct inode *inode = page->mapping->host;
  402. int error;
  403. dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
  404. page, PAGE_CACHE_SIZE, page->index);
  405. nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
  406. nfs_add_stats(inode, NFSIOS_READPAGES, 1);
  407. /*
  408. * Try to flush any pending writes to the file..
  409. *
  410. * NOTE! Because we own the page lock, there cannot
  411. * be any new pending writes generated at this point
  412. * for this page (other pages can be written to).
  413. */
  414. error = nfs_wb_page(inode, page);
  415. if (error)
  416. goto out_unlock;
  417. if (PageUptodate(page))
  418. goto out_unlock;
  419. error = -ESTALE;
  420. if (NFS_STALE(inode))
  421. goto out_unlock;
  422. if (file == NULL) {
  423. error = -EBADF;
  424. ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  425. if (ctx == NULL)
  426. goto out_unlock;
  427. } else
  428. ctx = get_nfs_open_context(nfs_file_open_context(file));
  429. error = nfs_readpage_async(ctx, inode, page);
  430. put_nfs_open_context(ctx);
  431. return error;
  432. out_unlock:
  433. unlock_page(page);
  434. return error;
  435. }
  436. struct nfs_readdesc {
  437. struct nfs_pageio_descriptor *pgio;
  438. struct nfs_open_context *ctx;
  439. };
  440. static int
  441. readpage_async_filler(void *data, struct page *page)
  442. {
  443. struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
  444. struct inode *inode = page->mapping->host;
  445. struct nfs_page *new;
  446. unsigned int len;
  447. int error;
  448. error = nfs_wb_page(inode, page);
  449. if (error)
  450. goto out_unlock;
  451. if (PageUptodate(page))
  452. goto out_unlock;
  453. len = nfs_page_length(page);
  454. if (len == 0)
  455. return nfs_return_empty_page(page);
  456. new = nfs_create_request(desc->ctx, inode, page, 0, len);
  457. if (IS_ERR(new))
  458. goto out_error;
  459. if (len < PAGE_CACHE_SIZE)
  460. zero_user_page(page, len, PAGE_CACHE_SIZE - len, KM_USER0);
  461. nfs_pageio_add_request(desc->pgio, new);
  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. }