read.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. * We do an ugly hack here in order to return proper error codes to the
  10. * user program when a read request failed: since generic_file_read
  11. * only checks the return value of inode->i_op->readpage() which is always 0
  12. * for async RPC, we set the error bit of the page to 1 when an error occurs,
  13. * and make nfs_readpage transmit requests synchronously when encountering this.
  14. * This is only a small problem, though, since we now retry all operations
  15. * within the RPC code when root squashing is suspected.
  16. */
  17. #include <linux/config.h>
  18. #include <linux/time.h>
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/stat.h>
  23. #include <linux/mm.h>
  24. #include <linux/slab.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/sunrpc/clnt.h>
  27. #include <linux/nfs_fs.h>
  28. #include <linux/nfs_page.h>
  29. #include <linux/smp_lock.h>
  30. #include <asm/system.h>
  31. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  32. static int nfs_pagein_one(struct list_head *, struct inode *);
  33. static void nfs_readpage_result_partial(struct nfs_read_data *, int);
  34. static void nfs_readpage_result_full(struct nfs_read_data *, int);
  35. static kmem_cache_t *nfs_rdata_cachep;
  36. mempool_t *nfs_rdata_mempool;
  37. #define MIN_POOL_READ (32)
  38. void nfs_readdata_release(void *data)
  39. {
  40. nfs_readdata_free(data);
  41. }
  42. static
  43. unsigned int nfs_page_length(struct inode *inode, struct page *page)
  44. {
  45. loff_t i_size = i_size_read(inode);
  46. unsigned long idx;
  47. if (i_size <= 0)
  48. return 0;
  49. idx = (i_size - 1) >> PAGE_CACHE_SHIFT;
  50. if (page->index > idx)
  51. return 0;
  52. if (page->index != idx)
  53. return PAGE_CACHE_SIZE;
  54. return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1));
  55. }
  56. static
  57. int nfs_return_empty_page(struct page *page)
  58. {
  59. memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
  60. SetPageUptodate(page);
  61. unlock_page(page);
  62. return 0;
  63. }
  64. /*
  65. * Read a page synchronously.
  66. */
  67. static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode,
  68. struct page *page)
  69. {
  70. unsigned int rsize = NFS_SERVER(inode)->rsize;
  71. unsigned int count = PAGE_CACHE_SIZE;
  72. int result;
  73. struct nfs_read_data *rdata;
  74. rdata = nfs_readdata_alloc(1);
  75. if (!rdata)
  76. return -ENOMEM;
  77. memset(rdata, 0, sizeof(*rdata));
  78. rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
  79. rdata->cred = ctx->cred;
  80. rdata->inode = inode;
  81. INIT_LIST_HEAD(&rdata->pages);
  82. rdata->args.fh = NFS_FH(inode);
  83. rdata->args.context = ctx;
  84. rdata->args.pages = &page;
  85. rdata->args.pgbase = 0UL;
  86. rdata->args.count = rsize;
  87. rdata->res.fattr = &rdata->fattr;
  88. dprintk("NFS: nfs_readpage_sync(%p)\n", page);
  89. /*
  90. * This works now because the socket layer never tries to DMA
  91. * into this buffer directly.
  92. */
  93. do {
  94. if (count < rsize)
  95. rdata->args.count = count;
  96. rdata->res.count = rdata->args.count;
  97. rdata->args.offset = page_offset(page) + rdata->args.pgbase;
  98. dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
  99. NFS_SERVER(inode)->hostname,
  100. inode->i_sb->s_id,
  101. (long long)NFS_FILEID(inode),
  102. (unsigned long long)rdata->args.pgbase,
  103. rdata->args.count);
  104. lock_kernel();
  105. result = NFS_PROTO(inode)->read(rdata);
  106. unlock_kernel();
  107. /*
  108. * Even if we had a partial success we can't mark the page
  109. * cache valid.
  110. */
  111. if (result < 0) {
  112. if (result == -EISDIR)
  113. result = -EINVAL;
  114. goto io_error;
  115. }
  116. count -= result;
  117. rdata->args.pgbase += result;
  118. /* Note: result == 0 should only happen if we're caching
  119. * a write that extends the file and punches a hole.
  120. */
  121. if (rdata->res.eof != 0 || result == 0)
  122. break;
  123. } while (count);
  124. spin_lock(&inode->i_lock);
  125. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  126. spin_unlock(&inode->i_lock);
  127. if (count)
  128. memclear_highpage_flush(page, rdata->args.pgbase, count);
  129. SetPageUptodate(page);
  130. if (PageError(page))
  131. ClearPageError(page);
  132. result = 0;
  133. io_error:
  134. unlock_page(page);
  135. nfs_readdata_free(rdata);
  136. return result;
  137. }
  138. static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
  139. struct page *page)
  140. {
  141. LIST_HEAD(one_request);
  142. struct nfs_page *new;
  143. unsigned int len;
  144. len = nfs_page_length(inode, page);
  145. if (len == 0)
  146. return nfs_return_empty_page(page);
  147. new = nfs_create_request(ctx, inode, page, 0, len);
  148. if (IS_ERR(new)) {
  149. unlock_page(page);
  150. return PTR_ERR(new);
  151. }
  152. if (len < PAGE_CACHE_SIZE)
  153. memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
  154. nfs_list_add_request(new, &one_request);
  155. nfs_pagein_one(&one_request, inode);
  156. return 0;
  157. }
  158. static void nfs_readpage_release(struct nfs_page *req)
  159. {
  160. unlock_page(req->wb_page);
  161. dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
  162. req->wb_context->dentry->d_inode->i_sb->s_id,
  163. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  164. req->wb_bytes,
  165. (long long)req_offset(req));
  166. nfs_clear_request(req);
  167. nfs_release_request(req);
  168. }
  169. /*
  170. * Set up the NFS read request struct
  171. */
  172. static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
  173. unsigned int count, unsigned int offset)
  174. {
  175. struct inode *inode;
  176. data->req = req;
  177. data->inode = inode = req->wb_context->dentry->d_inode;
  178. data->cred = req->wb_context->cred;
  179. data->args.fh = NFS_FH(inode);
  180. data->args.offset = req_offset(req) + offset;
  181. data->args.pgbase = req->wb_pgbase + offset;
  182. data->args.pages = data->pagevec;
  183. data->args.count = count;
  184. data->args.context = req->wb_context;
  185. data->res.fattr = &data->fattr;
  186. data->res.count = count;
  187. data->res.eof = 0;
  188. nfs_fattr_init(&data->fattr);
  189. NFS_PROTO(inode)->read_setup(data);
  190. data->task.tk_cookie = (unsigned long)inode;
  191. dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  192. data->task.tk_pid,
  193. inode->i_sb->s_id,
  194. (long long)NFS_FILEID(inode),
  195. count,
  196. (unsigned long long)data->args.offset);
  197. }
  198. static void
  199. nfs_async_read_error(struct list_head *head)
  200. {
  201. struct nfs_page *req;
  202. while (!list_empty(head)) {
  203. req = nfs_list_entry(head->next);
  204. nfs_list_remove_request(req);
  205. SetPageError(req->wb_page);
  206. nfs_readpage_release(req);
  207. }
  208. }
  209. /*
  210. * Start an async read operation
  211. */
  212. static void nfs_execute_read(struct nfs_read_data *data)
  213. {
  214. struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
  215. sigset_t oldset;
  216. rpc_clnt_sigmask(clnt, &oldset);
  217. lock_kernel();
  218. rpc_execute(&data->task);
  219. unlock_kernel();
  220. rpc_clnt_sigunmask(clnt, &oldset);
  221. }
  222. /*
  223. * Generate multiple requests to fill a single page.
  224. *
  225. * We optimize to reduce the number of read operations on the wire. If we
  226. * detect that we're reading a page, or an area of a page, that is past the
  227. * end of file, we do not generate NFS read operations but just clear the
  228. * parts of the page that would have come back zero from the server anyway.
  229. *
  230. * We rely on the cached value of i_size to make this determination; another
  231. * client can fill pages on the server past our cached end-of-file, but we
  232. * won't see the new data until our attribute cache is updated. This is more
  233. * or less conventional NFS client behavior.
  234. */
  235. static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
  236. {
  237. struct nfs_page *req = nfs_list_entry(head->next);
  238. struct page *page = req->wb_page;
  239. struct nfs_read_data *data;
  240. unsigned int rsize = NFS_SERVER(inode)->rsize;
  241. unsigned int nbytes, offset;
  242. int requests = 0;
  243. LIST_HEAD(list);
  244. nfs_list_remove_request(req);
  245. nbytes = req->wb_bytes;
  246. for(;;) {
  247. data = nfs_readdata_alloc(1);
  248. if (!data)
  249. goto out_bad;
  250. INIT_LIST_HEAD(&data->pages);
  251. list_add(&data->pages, &list);
  252. requests++;
  253. if (nbytes <= rsize)
  254. break;
  255. nbytes -= rsize;
  256. }
  257. atomic_set(&req->wb_complete, requests);
  258. ClearPageError(page);
  259. offset = 0;
  260. nbytes = req->wb_bytes;
  261. do {
  262. data = list_entry(list.next, struct nfs_read_data, pages);
  263. list_del_init(&data->pages);
  264. data->pagevec[0] = page;
  265. data->complete = nfs_readpage_result_partial;
  266. if (nbytes > rsize) {
  267. nfs_read_rpcsetup(req, data, rsize, offset);
  268. offset += rsize;
  269. nbytes -= rsize;
  270. } else {
  271. nfs_read_rpcsetup(req, data, nbytes, offset);
  272. nbytes = 0;
  273. }
  274. nfs_execute_read(data);
  275. } while (nbytes != 0);
  276. return 0;
  277. out_bad:
  278. while (!list_empty(&list)) {
  279. data = list_entry(list.next, struct nfs_read_data, pages);
  280. list_del(&data->pages);
  281. nfs_readdata_free(data);
  282. }
  283. SetPageError(page);
  284. nfs_readpage_release(req);
  285. return -ENOMEM;
  286. }
  287. static int nfs_pagein_one(struct list_head *head, struct inode *inode)
  288. {
  289. struct nfs_page *req;
  290. struct page **pages;
  291. struct nfs_read_data *data;
  292. unsigned int count;
  293. if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
  294. return nfs_pagein_multi(head, inode);
  295. data = nfs_readdata_alloc(NFS_SERVER(inode)->rpages);
  296. if (!data)
  297. goto out_bad;
  298. INIT_LIST_HEAD(&data->pages);
  299. pages = data->pagevec;
  300. count = 0;
  301. while (!list_empty(head)) {
  302. req = nfs_list_entry(head->next);
  303. nfs_list_remove_request(req);
  304. nfs_list_add_request(req, &data->pages);
  305. ClearPageError(req->wb_page);
  306. *pages++ = req->wb_page;
  307. count += req->wb_bytes;
  308. }
  309. req = nfs_list_entry(data->pages.next);
  310. data->complete = nfs_readpage_result_full;
  311. nfs_read_rpcsetup(req, data, count, 0);
  312. nfs_execute_read(data);
  313. return 0;
  314. out_bad:
  315. nfs_async_read_error(head);
  316. return -ENOMEM;
  317. }
  318. static int
  319. nfs_pagein_list(struct list_head *head, int rpages)
  320. {
  321. LIST_HEAD(one_request);
  322. struct nfs_page *req;
  323. int error = 0;
  324. unsigned int pages = 0;
  325. while (!list_empty(head)) {
  326. pages += nfs_coalesce_requests(head, &one_request, rpages);
  327. req = nfs_list_entry(one_request.next);
  328. error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode);
  329. if (error < 0)
  330. break;
  331. }
  332. if (error >= 0)
  333. return pages;
  334. nfs_async_read_error(head);
  335. return error;
  336. }
  337. /*
  338. * Handle a read reply that fills part of a page.
  339. */
  340. static void nfs_readpage_result_partial(struct nfs_read_data *data, int status)
  341. {
  342. struct nfs_page *req = data->req;
  343. struct page *page = req->wb_page;
  344. if (status >= 0) {
  345. unsigned int request = data->args.count;
  346. unsigned int result = data->res.count;
  347. if (result < request) {
  348. memclear_highpage_flush(page,
  349. data->args.pgbase + result,
  350. request - result);
  351. }
  352. } else
  353. SetPageError(page);
  354. if (atomic_dec_and_test(&req->wb_complete)) {
  355. if (!PageError(page))
  356. SetPageUptodate(page);
  357. nfs_readpage_release(req);
  358. }
  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 nfs_read_data *data, int status)
  365. {
  366. unsigned int count = data->res.count;
  367. while (!list_empty(&data->pages)) {
  368. struct nfs_page *req = nfs_list_entry(data->pages.next);
  369. struct page *page = req->wb_page;
  370. nfs_list_remove_request(req);
  371. if (status >= 0) {
  372. if (count < PAGE_CACHE_SIZE) {
  373. if (count < req->wb_bytes)
  374. memclear_highpage_flush(page,
  375. req->wb_pgbase + count,
  376. req->wb_bytes - count);
  377. count = 0;
  378. } else
  379. count -= PAGE_CACHE_SIZE;
  380. SetPageUptodate(page);
  381. } else
  382. SetPageError(page);
  383. nfs_readpage_release(req);
  384. }
  385. }
  386. /*
  387. * This is the callback from RPC telling us whether a reply was
  388. * received or some error occurred (timeout or socket shutdown).
  389. */
  390. void nfs_readpage_result(struct rpc_task *task, void *calldata)
  391. {
  392. struct nfs_read_data *data = calldata;
  393. struct nfs_readargs *argp = &data->args;
  394. struct nfs_readres *resp = &data->res;
  395. int status = task->tk_status;
  396. dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
  397. task->tk_pid, status);
  398. /* Is this a short read? */
  399. if (task->tk_status >= 0 && resp->count < argp->count && !resp->eof) {
  400. /* Has the server at least made some progress? */
  401. if (resp->count != 0) {
  402. /* Yes, so retry the read at the end of the data */
  403. argp->offset += resp->count;
  404. argp->pgbase += resp->count;
  405. argp->count -= resp->count;
  406. rpc_restart_call(task);
  407. return;
  408. }
  409. task->tk_status = -EIO;
  410. }
  411. spin_lock(&data->inode->i_lock);
  412. NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  413. spin_unlock(&data->inode->i_lock);
  414. data->complete(data, status);
  415. }
  416. /*
  417. * Read a page over NFS.
  418. * We read the page synchronously in the following case:
  419. * - The error flag is set for this page. This happens only when a
  420. * previous async read operation failed.
  421. */
  422. int nfs_readpage(struct file *file, struct page *page)
  423. {
  424. struct nfs_open_context *ctx;
  425. struct inode *inode = page->mapping->host;
  426. int error;
  427. dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
  428. page, PAGE_CACHE_SIZE, page->index);
  429. /*
  430. * Try to flush any pending writes to the file..
  431. *
  432. * NOTE! Because we own the page lock, there cannot
  433. * be any new pending writes generated at this point
  434. * for this page (other pages can be written to).
  435. */
  436. error = nfs_wb_page(inode, page);
  437. if (error)
  438. goto out_error;
  439. if (file == NULL) {
  440. ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  441. if (ctx == NULL)
  442. return -EBADF;
  443. } else
  444. ctx = get_nfs_open_context((struct nfs_open_context *)
  445. file->private_data);
  446. if (!IS_SYNC(inode)) {
  447. error = nfs_readpage_async(ctx, inode, page);
  448. goto out;
  449. }
  450. error = nfs_readpage_sync(ctx, inode, page);
  451. if (error < 0 && IS_SWAPFILE(inode))
  452. printk("Aiee.. nfs swap-in of page failed!\n");
  453. out:
  454. put_nfs_open_context(ctx);
  455. return error;
  456. out_error:
  457. unlock_page(page);
  458. return error;
  459. }
  460. struct nfs_readdesc {
  461. struct list_head *head;
  462. struct nfs_open_context *ctx;
  463. };
  464. static int
  465. readpage_async_filler(void *data, struct page *page)
  466. {
  467. struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
  468. struct inode *inode = page->mapping->host;
  469. struct nfs_page *new;
  470. unsigned int len;
  471. nfs_wb_page(inode, page);
  472. len = nfs_page_length(inode, page);
  473. if (len == 0)
  474. return nfs_return_empty_page(page);
  475. new = nfs_create_request(desc->ctx, inode, page, 0, len);
  476. if (IS_ERR(new)) {
  477. SetPageError(page);
  478. unlock_page(page);
  479. return PTR_ERR(new);
  480. }
  481. if (len < PAGE_CACHE_SIZE)
  482. memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
  483. nfs_list_add_request(new, desc->head);
  484. return 0;
  485. }
  486. int nfs_readpages(struct file *filp, struct address_space *mapping,
  487. struct list_head *pages, unsigned nr_pages)
  488. {
  489. LIST_HEAD(head);
  490. struct nfs_readdesc desc = {
  491. .head = &head,
  492. };
  493. struct inode *inode = mapping->host;
  494. struct nfs_server *server = NFS_SERVER(inode);
  495. int ret;
  496. dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
  497. inode->i_sb->s_id,
  498. (long long)NFS_FILEID(inode),
  499. nr_pages);
  500. if (filp == NULL) {
  501. desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  502. if (desc.ctx == NULL)
  503. return -EBADF;
  504. } else
  505. desc.ctx = get_nfs_open_context((struct nfs_open_context *)
  506. filp->private_data);
  507. ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
  508. if (!list_empty(&head)) {
  509. int err = nfs_pagein_list(&head, server->rpages);
  510. if (!ret)
  511. ret = err;
  512. }
  513. put_nfs_open_context(desc.ctx);
  514. return ret;
  515. }
  516. int nfs_init_readpagecache(void)
  517. {
  518. nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
  519. sizeof(struct nfs_read_data),
  520. 0, SLAB_HWCACHE_ALIGN,
  521. NULL, NULL);
  522. if (nfs_rdata_cachep == NULL)
  523. return -ENOMEM;
  524. nfs_rdata_mempool = mempool_create(MIN_POOL_READ,
  525. mempool_alloc_slab,
  526. mempool_free_slab,
  527. nfs_rdata_cachep);
  528. if (nfs_rdata_mempool == NULL)
  529. return -ENOMEM;
  530. return 0;
  531. }
  532. void nfs_destroy_readpagecache(void)
  533. {
  534. mempool_destroy(nfs_rdata_mempool);
  535. if (kmem_cache_destroy(nfs_rdata_cachep))
  536. printk(KERN_INFO "nfs_read_data: not all structures were freed\n");
  537. }