filemap.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * linux/mm/filemap.h
  3. *
  4. * Copyright (C) 1994-1999 Linus Torvalds
  5. */
  6. #ifndef __FILEMAP_H
  7. #define __FILEMAP_H
  8. #include <linux/types.h>
  9. #include <linux/fs.h>
  10. #include <linux/mm.h>
  11. #include <linux/highmem.h>
  12. #include <linux/uio.h>
  13. #include <linux/uaccess.h>
  14. size_t
  15. __filemap_copy_from_user_iovec_inatomic(char *vaddr,
  16. const struct iovec *iov,
  17. size_t base,
  18. size_t bytes);
  19. /*
  20. * Copy as much as we can into the page and return the number of bytes which
  21. * were sucessfully copied. If a fault is encountered then return the number of
  22. * bytes which were copied.
  23. */
  24. static inline size_t
  25. filemap_copy_from_user_atomic(struct page *page, unsigned long offset,
  26. const struct iovec *iov, unsigned long nr_segs,
  27. size_t base, size_t bytes)
  28. {
  29. char *kaddr;
  30. size_t copied;
  31. kaddr = kmap_atomic(page, KM_USER0);
  32. if (likely(nr_segs == 1)) {
  33. int left;
  34. char __user *buf = iov->iov_base + base;
  35. left = __copy_from_user_inatomic_nocache(kaddr + offset,
  36. buf, bytes);
  37. copied = bytes - left;
  38. } else {
  39. copied = __filemap_copy_from_user_iovec_inatomic(kaddr + offset,
  40. iov, base, bytes);
  41. }
  42. kunmap_atomic(kaddr, KM_USER0);
  43. return copied;
  44. }
  45. /*
  46. * This has the same sideeffects and return value as
  47. * filemap_copy_from_user_atomic().
  48. * The difference is that it attempts to resolve faults.
  49. */
  50. static inline size_t
  51. filemap_copy_from_user(struct page *page, unsigned long offset,
  52. const struct iovec *iov, unsigned long nr_segs,
  53. size_t base, size_t bytes)
  54. {
  55. char *kaddr;
  56. size_t copied;
  57. kaddr = kmap(page);
  58. if (likely(nr_segs == 1)) {
  59. int left;
  60. char __user *buf = iov->iov_base + base;
  61. left = __copy_from_user_nocache(kaddr + offset, buf, bytes);
  62. copied = bytes - left;
  63. } else {
  64. copied = __filemap_copy_from_user_iovec_inatomic(kaddr + offset,
  65. iov, base, bytes);
  66. }
  67. kunmap(page);
  68. return copied;
  69. }
  70. static inline void
  71. filemap_set_next_iovec(const struct iovec **iovp, unsigned long nr_segs,
  72. size_t *basep, size_t bytes)
  73. {
  74. if (likely(nr_segs == 1)) {
  75. *basep += bytes;
  76. } else {
  77. const struct iovec *iov = *iovp;
  78. size_t base = *basep;
  79. while (bytes) {
  80. int copy = min(bytes, iov->iov_len - base);
  81. bytes -= copy;
  82. base += copy;
  83. if (iov->iov_len == base) {
  84. iov++;
  85. base = 0;
  86. }
  87. }
  88. *iovp = iov;
  89. *basep = base;
  90. }
  91. }
  92. #endif