read_write.c 8.5 KB

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