read_write.c 8.5 KB

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