trans_common.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright IBM Corporation, 2010
  3. * Author Venkateswararao Jujjuri <jvrao@linux.vnet.ibm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2.1 of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. *
  13. */
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <net/9p/9p.h>
  17. #include <net/9p/client.h>
  18. #include <linux/scatterlist.h>
  19. #include "trans_common.h"
  20. /**
  21. * p9_release_req_pages - Release pages after the transaction.
  22. * @*private: PDU's private page of struct trans_rpage_info
  23. */
  24. void
  25. p9_release_req_pages(struct trans_rpage_info *rpinfo)
  26. {
  27. int i = 0;
  28. while (rpinfo->rp_data[i] && rpinfo->rp_nr_pages--) {
  29. put_page(rpinfo->rp_data[i]);
  30. i++;
  31. }
  32. }
  33. EXPORT_SYMBOL(p9_release_req_pages);
  34. /**
  35. * p9_nr_pages - Return number of pages needed to accommodate the payload.
  36. */
  37. int
  38. p9_nr_pages(struct p9_req_t *req)
  39. {
  40. unsigned long start_page, end_page;
  41. start_page = (unsigned long)req->tc->pubuf >> PAGE_SHIFT;
  42. end_page = ((unsigned long)req->tc->pubuf + req->tc->pbuf_size +
  43. PAGE_SIZE - 1) >> PAGE_SHIFT;
  44. return end_page - start_page;
  45. }
  46. EXPORT_SYMBOL(p9_nr_pages);
  47. /**
  48. * payload_gup - Translates user buffer into kernel pages and
  49. * pins them either for read/write through get_user_pages_fast().
  50. * @req: Request to be sent to server.
  51. * @pdata_off: data offset into the first page after translation (gup).
  52. * @pdata_len: Total length of the IO. gup may not return requested # of pages.
  53. * @nr_pages: number of pages to accommodate the payload
  54. * @rw: Indicates if the pages are for read or write.
  55. */
  56. int
  57. p9_payload_gup(struct p9_req_t *req, size_t *pdata_off, int *pdata_len,
  58. int nr_pages, u8 rw)
  59. {
  60. uint32_t first_page_bytes = 0;
  61. int32_t pdata_mapped_pages;
  62. struct trans_rpage_info *rpinfo;
  63. *pdata_off = (__force size_t)req->tc->pubuf & (PAGE_SIZE-1);
  64. if (*pdata_off)
  65. first_page_bytes = min(((size_t)PAGE_SIZE - *pdata_off),
  66. req->tc->pbuf_size);
  67. rpinfo = req->tc->private;
  68. pdata_mapped_pages = get_user_pages_fast((unsigned long)req->tc->pubuf,
  69. nr_pages, rw, &rpinfo->rp_data[0]);
  70. if (pdata_mapped_pages <= 0)
  71. return pdata_mapped_pages;
  72. rpinfo->rp_nr_pages = pdata_mapped_pages;
  73. if (*pdata_off) {
  74. *pdata_len = first_page_bytes;
  75. *pdata_len += min((req->tc->pbuf_size - *pdata_len),
  76. ((size_t)pdata_mapped_pages - 1) << PAGE_SHIFT);
  77. } else {
  78. *pdata_len = min(req->tc->pbuf_size,
  79. (size_t)pdata_mapped_pages << PAGE_SHIFT);
  80. }
  81. return 0;
  82. }
  83. EXPORT_SYMBOL(p9_payload_gup);