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