trans_common.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. */
  23. void p9_release_pages(struct page **pages, int nr_pages)
  24. {
  25. int i = 0;
  26. while (pages[i] && nr_pages--) {
  27. put_page(pages[i]);
  28. i++;
  29. }
  30. }
  31. EXPORT_SYMBOL(p9_release_pages);
  32. /**
  33. * p9_nr_pages - Return number of pages needed to accommodate the payload.
  34. */
  35. int p9_nr_pages(char *data, int len)
  36. {
  37. unsigned long start_page, end_page;
  38. start_page = (unsigned long)data >> PAGE_SHIFT;
  39. end_page = ((unsigned long)data + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  40. return end_page - start_page;
  41. }
  42. EXPORT_SYMBOL(p9_nr_pages);
  43. /**
  44. * payload_gup - Translates user buffer into kernel pages and
  45. * pins them either for read/write through get_user_pages_fast().
  46. * @req: Request to be sent to server.
  47. * @pdata_off: data offset into the first page after translation (gup).
  48. * @pdata_len: Total length of the IO. gup may not return requested # of pages.
  49. * @nr_pages: number of pages to accommodate the payload
  50. * @rw: Indicates if the pages are for read or write.
  51. */
  52. int p9_payload_gup(char *data, int *nr_pages, struct page **pages, int write)
  53. {
  54. int nr_mapped_pages;
  55. nr_mapped_pages = get_user_pages_fast((unsigned long)data,
  56. *nr_pages, write, pages);
  57. if (nr_mapped_pages <= 0)
  58. return nr_mapped_pages;
  59. *nr_pages = nr_mapped_pages;
  60. return 0;
  61. }
  62. EXPORT_SYMBOL(p9_payload_gup);