read_write.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. * @folio_for_lower: The folio containing the data to be written to the
  40. * lower file
  41. * @offset_in_page: The offset in the @folio_for_lower from which to
  42. * start writing the data
  43. * @size: The amount of data from @folio_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 @folio_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 folio *folio_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)folio_for_lower->index * PAGE_SIZE + offset_in_page;
  60. virt = kmap_local_folio(folio_for_lower, 0);
  61. rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
  62. if (rc > 0)
  63. rc = 0;
  64. kunmap_local(virt);
  65. return rc;
  66. }
  67. /**
  68. * ecryptfs_write
  69. * @ecryptfs_inode: The eCryptfs file into which to write
  70. * @data: Virtual address where data to write is located
  71. * @offset: Offset in the eCryptfs file at which to begin writing the
  72. * data from @data
  73. * @size: The number of bytes to write from @data
  74. *
  75. * Write an arbitrary amount of data to an arbitrary location in the
  76. * eCryptfs inode page cache. This is done on a page-by-page, and then
  77. * by an extent-by-extent, basis; individual extents are encrypted and
  78. * written to the lower page cache (via VFS writes). This function
  79. * takes care of all the address translation to locations in the lower
  80. * filesystem; it also handles truncate events, writing out zeros
  81. * where necessary.
  82. *
  83. * Returns zero on success; non-zero otherwise
  84. */
  85. int ecryptfs_write(struct inode *ecryptfs_inode, char *data, loff_t offset,
  86. size_t size)
  87. {
  88. struct ecryptfs_crypt_stat *crypt_stat;
  89. char *ecryptfs_page_virt;
  90. loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
  91. loff_t data_offset = 0;
  92. loff_t pos;
  93. int rc = 0;
  94. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  95. /*
  96. * if we are writing beyond current size, then start pos
  97. * at the current size - we'll fill in zeros from there.
  98. */
  99. if (offset > ecryptfs_file_size)
  100. pos = ecryptfs_file_size;
  101. else
  102. pos = offset;
  103. while (pos < (offset + size)) {
  104. struct folio *ecryptfs_folio;
  105. pgoff_t ecryptfs_page_idx = (pos >> PAGE_SHIFT);
  106. size_t start_offset_in_page = (pos & ~PAGE_MASK);
  107. size_t num_bytes = (PAGE_SIZE - start_offset_in_page);
  108. loff_t total_remaining_bytes = ((offset + size) - pos);
  109. if (fatal_signal_pending(current)) {
  110. rc = -EINTR;
  111. break;
  112. }
  113. if (num_bytes > total_remaining_bytes)
  114. num_bytes = total_remaining_bytes;
  115. if (pos < offset) {
  116. /* remaining zeros to write, up to destination offset */
  117. loff_t total_remaining_zeros = (offset - pos);
  118. if (num_bytes > total_remaining_zeros)
  119. num_bytes = total_remaining_zeros;
  120. }
  121. ecryptfs_folio = read_mapping_folio(ecryptfs_inode->i_mapping,
  122. ecryptfs_page_idx, NULL);
  123. if (IS_ERR(ecryptfs_folio)) {
  124. rc = PTR_ERR(ecryptfs_folio);
  125. printk(KERN_ERR "%s: Error getting page at "
  126. "index [%ld] from eCryptfs inode "
  127. "mapping; rc = [%d]\n", __func__,
  128. ecryptfs_page_idx, rc);
  129. goto out;
  130. }
  131. folio_lock(ecryptfs_folio);
  132. ecryptfs_page_virt = kmap_local_folio(ecryptfs_folio, 0);
  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_local(ecryptfs_page_virt);
  154. flush_dcache_folio(ecryptfs_folio);
  155. folio_mark_uptodate(ecryptfs_folio);
  156. folio_unlock(ecryptfs_folio);
  157. if (crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  158. rc = ecryptfs_encrypt_page(ecryptfs_folio);
  159. else
  160. rc = ecryptfs_write_lower_page_segment(ecryptfs_inode,
  161. ecryptfs_folio,
  162. start_offset_in_page,
  163. data_offset);
  164. folio_put(ecryptfs_folio);
  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. * @folio_for_ecryptfs: The folio into which data for eCryptfs will be
  216. * written
  217. * @page_index: Page index in @page_for_ecryptfs from which to start
  218. * writing
  219. * @offset_in_page: Offset in @page_for_ecryptfs from which to start
  220. * writing
  221. * @size: The number of bytes to write into @page_for_ecryptfs
  222. * @ecryptfs_inode: The eCryptfs inode
  223. *
  224. * Determines the byte offset in the file for the given page and
  225. * offset within the page, maps the page, and makes the call to read
  226. * the contents of @page_for_ecryptfs from the lower inode.
  227. *
  228. * Returns zero on success; non-zero otherwise
  229. */
  230. int ecryptfs_read_lower_page_segment(struct folio *folio_for_ecryptfs,
  231. pgoff_t page_index,
  232. size_t offset_in_page, size_t size,
  233. struct inode *ecryptfs_inode)
  234. {
  235. char *virt;
  236. loff_t offset;
  237. int rc;
  238. offset = (loff_t)page_index * PAGE_SIZE + offset_in_page;
  239. virt = kmap_local_folio(folio_for_ecryptfs, 0);
  240. rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
  241. if (rc > 0)
  242. rc = 0;
  243. kunmap_local(virt);
  244. flush_dcache_folio(folio_for_ecryptfs);
  245. return rc;
  246. }