read.c 15 KB

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