read_write.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /**
  3. * eCryptfs: Linux filesystem encryption layer
  4. *
  5. * Copyright (C) 2007 International Business Machines Corp.
  6. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/sched/signal.h>
  11. #include "ecryptfs_kernel.h"
  12. /**
  13. * ecryptfs_write_lower
  14. * @ecryptfs_inode: The eCryptfs inode
  15. * @data: Data to write
  16. * @offset: Byte offset in the lower file to which to write the data
  17. * @size: Number of bytes from @data to write at @offset in the lower
  18. * file
  19. *
  20. * Write data to the lower file.
  21. *
  22. * Returns bytes written on success; less than zero on error
  23. */
  24. int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
  25. loff_t offset, size_t size)
  26. {
  27. struct file *lower_file;
  28. ssize_t rc;
  29. lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
  30. if (!lower_file)
  31. return -EIO;
  32. rc = kernel_write(lower_file, data, size, &offset);
  33. mark_inode_dirty_sync(ecryptfs_inode);
  34. return rc;
  35. }
  36. /**
  37. * ecryptfs_write_lower_page_segment
  38. * @ecryptfs_inode: The eCryptfs inode
  39. * @page_for_lower: The page containing the data to be written to the
  40. * lower file
  41. * @offset_in_page: The offset in the @page_for_lower from which to
  42. * start writing the data
  43. * @size: The amount of data from @page_for_lower to write to the
  44. * lower file
  45. *
  46. * Determines the byte offset in the file for the given page and
  47. * offset within the page, maps the page, and makes the call to write
  48. * the contents of @page_for_lower to the lower inode.
  49. *
  50. * Returns zero on success; non-zero otherwise
  51. */
  52. int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
  53. struct page *page_for_lower,
  54. size_t offset_in_page, size_t size)
  55. {
  56. char *virt;
  57. loff_t offset;
  58. int rc;
  59. offset = ((((loff_t)page_for_lower->index) << PAGE_SHIFT)
  60. + offset_in_page);
  61. virt = kmap(page_for_lower);
  62. rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
  63. if (rc > 0)
  64. rc = 0;
  65. kunmap(page_for_lower);
  66. return rc;
  67. }
  68. /**
  69. * ecryptfs_write
  70. * @ecryptfs_inode: The eCryptfs file into which to write
  71. * @data: Virtual address where data to write is located
  72. * @offset: Offset in the eCryptfs file at which to begin writing the
  73. * data from @data
  74. * @size: The number of bytes to write from @data
  75. *
  76. * Write an arbitrary amount of data to an arbitrary location in the
  77. * eCryptfs inode page cache. This is done on a page-by-page, and then
  78. * by an extent-by-extent, basis; individual extents are encrypted and
  79. * written to the lower page cache (via VFS writes). This function
  80. * takes care of all the address translation to locations in the lower
  81. * filesystem; it also handles truncate events, writing out zeros
  82. * where necessary.
  83. *
  84. * Returns zero on success; non-zero otherwise
  85. */
  86. int ecryptfs_write(struct inode *ecryptfs_inode, char *data, loff_t offset,
  87. size_t size)
  88. {
  89. struct page *ecryptfs_page;
  90. struct ecryptfs_crypt_stat *crypt_stat;
  91. char *ecryptfs_page_virt;
  92. loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
  93. loff_t data_offset = 0;
  94. loff_t pos;
  95. int rc = 0;
  96. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  97. /*
  98. * if we are writing beyond current size, then start pos
  99. * at the current size - we'll fill in zeros from there.
  100. */
  101. if (offset > ecryptfs_file_size)
  102. pos = ecryptfs_file_size;
  103. else
  104. pos = offset;
  105. while (pos < (offset + size)) {
  106. pgoff_t ecryptfs_page_idx = (pos >> PAGE_SHIFT);
  107. size_t start_offset_in_page = (pos & ~PAGE_MASK);
  108. size_t num_bytes = (PAGE_SIZE - start_offset_in_page);
  109. loff_t total_remaining_bytes = ((offset + size) - pos);
  110. if (fatal_signal_pending(current)) {
  111. rc = -EINTR;
  112. break;
  113. }
  114. if (num_bytes > total_remaining_bytes)
  115. num_bytes = total_remaining_bytes;
  116. if (pos < offset) {
  117. /* remaining zeros to write, up to destination offset */
  118. loff_t total_remaining_zeros = (offset - pos);
  119. if (num_bytes > total_remaining_zeros)
  120. num_bytes = total_remaining_zeros;
  121. }
  122. ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_inode,
  123. ecryptfs_page_idx);
  124. if (IS_ERR(ecryptfs_page)) {
  125. rc = PTR_ERR(ecryptfs_page);
  126. printk(KERN_ERR "%s: Error getting page at "
  127. "index [%ld] from eCryptfs inode "
  128. "mapping; rc = [%d]\n", __func__,
  129. ecryptfs_page_idx, rc);
  130. goto out;
  131. }
  132. ecryptfs_page_virt = kmap_atomic(ecryptfs_page);
  133. /*
  134. * pos: where we're now writing, offset: where the request was
  135. * If current pos is before request, we are filling zeros
  136. * If we are at or beyond request, we are writing the *data*
  137. * If we're in a fresh page beyond eof, zero it in either case
  138. */
  139. if (pos < offset || !start_offset_in_page) {
  140. /* We are extending past the previous end of the file.
  141. * Fill in zero values to the end of the page */
  142. memset(((char *)ecryptfs_page_virt
  143. + start_offset_in_page), 0,
  144. PAGE_SIZE - start_offset_in_page);
  145. }
  146. /* pos >= offset, we are now writing the data request */
  147. if (pos >= offset) {
  148. memcpy(((char *)ecryptfs_page_virt
  149. + start_offset_in_page),
  150. (data + data_offset), num_bytes);
  151. data_offset += num_bytes;
  152. }
  153. kunmap_atomic(ecryptfs_page_virt);
  154. flush_dcache_page(ecryptfs_page);
  155. SetPageUptodate(ecryptfs_page);
  156. unlock_page(ecryptfs_page);
  157. if (crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  158. rc = ecryptfs_encrypt_page(ecryptfs_page);
  159. else
  160. rc = ecryptfs_write_lower_page_segment(ecryptfs_inode,
  161. ecryptfs_page,
  162. start_offset_in_page,
  163. data_offset);
  164. put_page(ecryptfs_page);
  165. if (rc) {
  166. printk(KERN_ERR "%s: Error encrypting "
  167. "page; rc = [%d]\n", __func__, rc);
  168. goto out;
  169. }
  170. pos += num_bytes;
  171. }
  172. if (pos > ecryptfs_file_size) {
  173. i_size_write(ecryptfs_inode, pos);
  174. if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) {
  175. int rc2;
  176. rc2 = ecryptfs_write_inode_size_to_metadata(
  177. ecryptfs_inode);
  178. if (rc2) {
  179. printk(KERN_ERR "Problem with "
  180. "ecryptfs_write_inode_size_to_metadata; "
  181. "rc = [%d]\n", rc2);
  182. if (!rc)
  183. rc = rc2;
  184. goto out;
  185. }
  186. }
  187. }
  188. out:
  189. return rc;
  190. }
  191. /**
  192. * ecryptfs_read_lower
  193. * @data: The read data is stored here by this function
  194. * @offset: Byte offset in the lower file from which to read the data
  195. * @size: Number of bytes to read from @offset of the lower file and
  196. * store into @data
  197. * @ecryptfs_inode: The eCryptfs inode
  198. *
  199. * Read @size bytes of data at byte offset @offset from the lower
  200. * inode into memory location @data.
  201. *
  202. * Returns bytes read on success; 0 on EOF; less than zero on error
  203. */
  204. int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
  205. struct inode *ecryptfs_inode)
  206. {
  207. struct file *lower_file;
  208. lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
  209. if (!lower_file)
  210. return -EIO;
  211. return kernel_read(lower_file, data, size, &offset);
  212. }
  213. /**
  214. * ecryptfs_read_lower_page_segment
  215. * @page_for_ecryptfs: The page into which data for eCryptfs will be
  216. * written
  217. * @offset_in_page: Offset in @page_for_ecryptfs from which to start
  218. * writing
  219. * @size: The number of bytes to write into @page_for_ecryptfs
  220. * @ecryptfs_inode: The eCryptfs inode
  221. *
  222. * Determines the byte offset in the file for the given page and
  223. * offset within the page, maps the page, and makes the call to read
  224. * the contents of @page_for_ecryptfs from the lower inode.
  225. *
  226. * Returns zero on success; non-zero otherwise
  227. */
  228. int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
  229. pgoff_t page_index,
  230. size_t offset_in_page, size_t size,
  231. struct inode *ecryptfs_inode)
  232. {
  233. char *virt;
  234. loff_t offset;
  235. int rc;
  236. offset = ((((loff_t)page_index) << PAGE_SHIFT) + offset_in_page);
  237. virt = kmap(page_for_ecryptfs);
  238. rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
  239. if (rc > 0)
  240. rc = 0;
  241. kunmap(page_for_ecryptfs);
  242. flush_dcache_page(page_for_ecryptfs);
  243. return rc;
  244. }