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. dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
  163. req->wb_context->dentry->d_inode->i_sb->s_id,
  164. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  165. req->wb_bytes,
  166. (long long)req_offset(req));
  167. nfs_clear_request(req);
  168. nfs_release_request(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_fattr_init(&data->fattr);
  190. NFS_PROTO(inode)->read_setup(data);
  191. data->task.tk_cookie = (unsigned long)inode;
  192. data->task.tk_calldata = data;
  193. /* Release requests */
  194. data->task.tk_release = nfs_readdata_release;
  195. dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  196. data->task.tk_pid,
  197. inode->i_sb->s_id,
  198. (long long)NFS_FILEID(inode),
  199. count,
  200. (unsigned long long)data->args.offset);
  201. }
  202. static void
  203. nfs_async_read_error(struct list_head *head)
  204. {
  205. struct nfs_page *req;
  206. while (!list_empty(head)) {
  207. req = nfs_list_entry(head->next);
  208. nfs_list_remove_request(req);
  209. SetPageError(req->wb_page);
  210. nfs_readpage_release(req);
  211. }
  212. }
  213. /*
  214. * Start an async read operation
  215. */
  216. static void nfs_execute_read(struct nfs_read_data *data)
  217. {
  218. struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
  219. sigset_t oldset;
  220. rpc_clnt_sigmask(clnt, &oldset);
  221. lock_kernel();
  222. rpc_execute(&data->task);
  223. unlock_kernel();
  224. rpc_clnt_sigunmask(clnt, &oldset);
  225. }
  226. /*
  227. * Generate multiple requests to fill a single page.
  228. *
  229. * We optimize to reduce the number of read operations on the wire. If we
  230. * detect that we're reading a page, or an area of a page, that is past the
  231. * end of file, we do not generate NFS read operations but just clear the
  232. * parts of the page that would have come back zero from the server anyway.
  233. *
  234. * We rely on the cached value of i_size to make this determination; another
  235. * client can fill pages on the server past our cached end-of-file, but we
  236. * won't see the new data until our attribute cache is updated. This is more
  237. * or less conventional NFS client behavior.
  238. */
  239. static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
  240. {
  241. struct nfs_page *req = nfs_list_entry(head->next);
  242. struct page *page = req->wb_page;
  243. struct nfs_read_data *data;
  244. unsigned int rsize = NFS_SERVER(inode)->rsize;
  245. unsigned int nbytes, offset;
  246. int requests = 0;
  247. LIST_HEAD(list);
  248. nfs_list_remove_request(req);
  249. nbytes = req->wb_bytes;
  250. for(;;) {
  251. data = nfs_readdata_alloc();
  252. if (!data)
  253. goto out_bad;
  254. INIT_LIST_HEAD(&data->pages);
  255. list_add(&data->pages, &list);
  256. requests++;
  257. if (nbytes <= rsize)
  258. break;
  259. nbytes -= rsize;
  260. }
  261. atomic_set(&req->wb_complete, requests);
  262. ClearPageError(page);
  263. offset = 0;
  264. nbytes = req->wb_bytes;
  265. do {
  266. data = list_entry(list.next, struct nfs_read_data, pages);
  267. list_del_init(&data->pages);
  268. data->pagevec[0] = page;
  269. data->complete = nfs_readpage_result_partial;
  270. if (nbytes > rsize) {
  271. nfs_read_rpcsetup(req, data, rsize, offset);
  272. offset += rsize;
  273. nbytes -= rsize;
  274. } else {
  275. nfs_read_rpcsetup(req, data, nbytes, offset);
  276. nbytes = 0;
  277. }
  278. nfs_execute_read(data);
  279. } while (nbytes != 0);
  280. return 0;
  281. out_bad:
  282. while (!list_empty(&list)) {
  283. data = list_entry(list.next, struct nfs_read_data, pages);
  284. list_del(&data->pages);
  285. nfs_readdata_free(data);
  286. }
  287. SetPageError(page);
  288. nfs_readpage_release(req);
  289. return -ENOMEM;
  290. }
  291. static int nfs_pagein_one(struct list_head *head, struct inode *inode)
  292. {
  293. struct nfs_page *req;
  294. struct page **pages;
  295. struct nfs_read_data *data;
  296. unsigned int count;
  297. if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
  298. return nfs_pagein_multi(head, inode);
  299. data = nfs_readdata_alloc();
  300. if (!data)
  301. goto out_bad;
  302. INIT_LIST_HEAD(&data->pages);
  303. pages = data->pagevec;
  304. count = 0;
  305. while (!list_empty(head)) {
  306. req = nfs_list_entry(head->next);
  307. nfs_list_remove_request(req);
  308. nfs_list_add_request(req, &data->pages);
  309. ClearPageError(req->wb_page);
  310. *pages++ = req->wb_page;
  311. count += req->wb_bytes;
  312. }
  313. req = nfs_list_entry(data->pages.next);
  314. data->complete = nfs_readpage_result_full;
  315. nfs_read_rpcsetup(req, data, count, 0);
  316. nfs_execute_read(data);
  317. return 0;
  318. out_bad:
  319. nfs_async_read_error(head);
  320. return -ENOMEM;
  321. }
  322. static int
  323. nfs_pagein_list(struct list_head *head, int rpages)
  324. {
  325. LIST_HEAD(one_request);
  326. struct nfs_page *req;
  327. int error = 0;
  328. unsigned int pages = 0;
  329. while (!list_empty(head)) {
  330. pages += nfs_coalesce_requests(head, &one_request, rpages);
  331. req = nfs_list_entry(one_request.next);
  332. error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode);
  333. if (error < 0)
  334. break;
  335. }
  336. if (error >= 0)
  337. return pages;
  338. nfs_async_read_error(head);
  339. return error;
  340. }
  341. /*
  342. * Handle a read reply that fills part of a page.
  343. */
  344. static void nfs_readpage_result_partial(struct nfs_read_data *data, int status)
  345. {
  346. struct nfs_page *req = data->req;
  347. struct page *page = req->wb_page;
  348. if (status >= 0) {
  349. unsigned int request = data->args.count;
  350. unsigned int result = data->res.count;
  351. if (result < request) {
  352. memclear_highpage_flush(page,
  353. data->args.pgbase + result,
  354. request - result);
  355. }
  356. } else
  357. SetPageError(page);
  358. if (atomic_dec_and_test(&req->wb_complete)) {
  359. if (!PageError(page))
  360. SetPageUptodate(page);
  361. nfs_readpage_release(req);
  362. }
  363. }
  364. /*
  365. * This is the callback from RPC telling us whether a reply was
  366. * received or some error occurred (timeout or socket shutdown).
  367. */
  368. static void nfs_readpage_result_full(struct nfs_read_data *data, int status)
  369. {
  370. unsigned int count = data->res.count;
  371. while (!list_empty(&data->pages)) {
  372. struct nfs_page *req = nfs_list_entry(data->pages.next);
  373. struct page *page = req->wb_page;
  374. nfs_list_remove_request(req);
  375. if (status >= 0) {
  376. if (count < PAGE_CACHE_SIZE) {
  377. if (count < req->wb_bytes)
  378. memclear_highpage_flush(page,
  379. req->wb_pgbase + count,
  380. req->wb_bytes - count);
  381. count = 0;
  382. } else
  383. count -= PAGE_CACHE_SIZE;
  384. SetPageUptodate(page);
  385. } else
  386. SetPageError(page);
  387. nfs_readpage_release(req);
  388. }
  389. }
  390. /*
  391. * This is the callback from RPC telling us whether a reply was
  392. * received or some error occurred (timeout or socket shutdown).
  393. */
  394. void nfs_readpage_result(struct rpc_task *task)
  395. {
  396. struct nfs_read_data *data = (struct nfs_read_data *)task->tk_calldata;
  397. struct nfs_readargs *argp = &data->args;
  398. struct nfs_readres *resp = &data->res;
  399. int status = task->tk_status;
  400. dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
  401. task->tk_pid, status);
  402. /* Is this a short read? */
  403. if (task->tk_status >= 0 && resp->count < argp->count && !resp->eof) {
  404. /* Has the server at least made some progress? */
  405. if (resp->count != 0) {
  406. /* Yes, so retry the read at the end of the data */
  407. argp->offset += resp->count;
  408. argp->pgbase += resp->count;
  409. argp->count -= resp->count;
  410. rpc_restart_call(task);
  411. return;
  412. }
  413. task->tk_status = -EIO;
  414. }
  415. spin_lock(&data->inode->i_lock);
  416. NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  417. spin_unlock(&data->inode->i_lock);
  418. data->complete(data, status);
  419. }
  420. /*
  421. * Read a page over NFS.
  422. * We read the page synchronously in the following case:
  423. * - The error flag is set for this page. This happens only when a
  424. * previous async read operation failed.
  425. */
  426. int nfs_readpage(struct file *file, struct page *page)
  427. {
  428. struct nfs_open_context *ctx;
  429. struct inode *inode = page->mapping->host;
  430. int error;
  431. dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
  432. page, PAGE_CACHE_SIZE, page->index);
  433. /*
  434. * Try to flush any pending writes to the file..
  435. *
  436. * NOTE! Because we own the page lock, there cannot
  437. * be any new pending writes generated at this point
  438. * for this page (other pages can be written to).
  439. */
  440. error = nfs_wb_page(inode, page);
  441. if (error)
  442. goto out_error;
  443. if (file == NULL) {
  444. ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  445. if (ctx == NULL)
  446. return -EBADF;
  447. } else
  448. ctx = get_nfs_open_context((struct nfs_open_context *)
  449. file->private_data);
  450. if (!IS_SYNC(inode)) {
  451. error = nfs_readpage_async(ctx, inode, page);
  452. goto out;
  453. }
  454. error = nfs_readpage_sync(ctx, inode, page);
  455. if (error < 0 && IS_SWAPFILE(inode))
  456. printk("Aiee.. nfs swap-in of page failed!\n");
  457. out:
  458. put_nfs_open_context(ctx);
  459. return error;
  460. out_error:
  461. unlock_page(page);
  462. return error;
  463. }
  464. struct nfs_readdesc {
  465. struct list_head *head;
  466. struct nfs_open_context *ctx;
  467. };
  468. static int
  469. readpage_async_filler(void *data, struct page *page)
  470. {
  471. struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
  472. struct inode *inode = page->mapping->host;
  473. struct nfs_page *new;
  474. unsigned int len;
  475. nfs_wb_page(inode, page);
  476. len = nfs_page_length(inode, page);
  477. if (len == 0)
  478. return nfs_return_empty_page(page);
  479. new = nfs_create_request(desc->ctx, inode, page, 0, len);
  480. if (IS_ERR(new)) {
  481. SetPageError(page);
  482. unlock_page(page);
  483. return PTR_ERR(new);
  484. }
  485. if (len < PAGE_CACHE_SIZE)
  486. memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
  487. nfs_list_add_request(new, desc->head);
  488. return 0;
  489. }
  490. int nfs_readpages(struct file *filp, struct address_space *mapping,
  491. struct list_head *pages, unsigned nr_pages)
  492. {
  493. LIST_HEAD(head);
  494. struct nfs_readdesc desc = {
  495. .head = &head,
  496. };
  497. struct inode *inode = mapping->host;
  498. struct nfs_server *server = NFS_SERVER(inode);
  499. int ret;
  500. dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
  501. inode->i_sb->s_id,
  502. (long long)NFS_FILEID(inode),
  503. nr_pages);
  504. if (filp == NULL) {
  505. desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  506. if (desc.ctx == NULL)
  507. return -EBADF;
  508. } else
  509. desc.ctx = get_nfs_open_context((struct nfs_open_context *)
  510. filp->private_data);
  511. ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
  512. if (!list_empty(&head)) {
  513. int err = nfs_pagein_list(&head, server->rpages);
  514. if (!ret)
  515. ret = err;
  516. }
  517. put_nfs_open_context(desc.ctx);
  518. return ret;
  519. }
  520. int nfs_init_readpagecache(void)
  521. {
  522. nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
  523. sizeof(struct nfs_read_data),
  524. 0, SLAB_HWCACHE_ALIGN,
  525. NULL, NULL);
  526. if (nfs_rdata_cachep == NULL)
  527. return -ENOMEM;
  528. nfs_rdata_mempool = mempool_create(MIN_POOL_READ,
  529. mempool_alloc_slab,
  530. mempool_free_slab,
  531. nfs_rdata_cachep);
  532. if (nfs_rdata_mempool == NULL)
  533. return -ENOMEM;
  534. return 0;
  535. }
  536. void nfs_destroy_readpagecache(void)
  537. {
  538. mempool_destroy(nfs_rdata_mempool);
  539. if (kmem_cache_destroy(nfs_rdata_cachep))
  540. printk(KERN_INFO "nfs_read_data: not all structures were freed\n");
  541. }