filemap.h 2.8 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 clear the page
  22. * out to (offset+bytes) and return the number of bytes which were copied.
  23. *
  24. * NOTE: For this to work reliably we really want copy_from_user_inatomic_nocache
  25. * to *NOT* zero any tail of the buffer that it failed to copy. If it does,
  26. * and if the following non-atomic copy succeeds, then there is a small window
  27. * where the target page contains neither the data before the write, nor the
  28. * data after the write (it contains zero). A read at this time will see
  29. * data that is inconsistent with any ordering of the read and the write.
  30. * (This has been detected in practice).
  31. */
  32. static inline size_t
  33. filemap_copy_from_user(struct page *page, unsigned long offset,
  34. const char __user *buf, unsigned bytes)
  35. {
  36. char *kaddr;
  37. int left;
  38. kaddr = kmap_atomic(page, KM_USER0);
  39. left = __copy_from_user_inatomic_nocache(kaddr + offset, buf, bytes);
  40. kunmap_atomic(kaddr, KM_USER0);
  41. if (left != 0) {
  42. /* Do it the slow way */
  43. kaddr = kmap(page);
  44. left = __copy_from_user_nocache(kaddr + offset, buf, bytes);
  45. kunmap(page);
  46. }
  47. return bytes - left;
  48. }
  49. /*
  50. * This has the same sideeffects and return value as filemap_copy_from_user().
  51. * The difference is that on a fault we need to memset the remainder of the
  52. * page (out to offset+bytes), to emulate filemap_copy_from_user()'s
  53. * single-segment behaviour.
  54. */
  55. static inline size_t
  56. filemap_copy_from_user_iovec(struct page *page, unsigned long offset,
  57. const struct iovec *iov, size_t base, size_t bytes)
  58. {
  59. char *kaddr;
  60. size_t copied;
  61. kaddr = kmap_atomic(page, KM_USER0);
  62. copied = __filemap_copy_from_user_iovec_inatomic(kaddr + offset, iov,
  63. base, bytes);
  64. kunmap_atomic(kaddr, KM_USER0);
  65. if (copied != bytes) {
  66. kaddr = kmap(page);
  67. copied = __filemap_copy_from_user_iovec_inatomic(kaddr + offset, iov,
  68. base, bytes);
  69. if (bytes - copied)
  70. memset(kaddr + offset + copied, 0, bytes - copied);
  71. kunmap(page);
  72. }
  73. return copied;
  74. }
  75. static inline void
  76. filemap_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
  77. {
  78. const struct iovec *iov = *iovp;
  79. size_t base = *basep;
  80. do {
  81. int copy = min(bytes, iov->iov_len - base);
  82. bytes -= copy;
  83. base += copy;
  84. if (iov->iov_len == base) {
  85. iov++;
  86. base = 0;
  87. }
  88. } while (bytes);
  89. *iovp = iov;
  90. *basep = base;
  91. }
  92. #endif