read_write.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 zero on success; non-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 ecryptfs_inode_info *inode_info;
  41. ssize_t octets_written;
  42. mm_segment_t fs_save;
  43. int rc = 0;
  44. inode_info = ecryptfs_inode_to_private(ecryptfs_inode);
  45. mutex_lock(&inode_info->lower_file_mutex);
  46. BUG_ON(!inode_info->lower_file);
  47. inode_info->lower_file->f_pos = offset;
  48. fs_save = get_fs();
  49. set_fs(get_ds());
  50. octets_written = vfs_write(inode_info->lower_file, data, size,
  51. &inode_info->lower_file->f_pos);
  52. set_fs(fs_save);
  53. if (octets_written < 0) {
  54. printk(KERN_ERR "%s: octets_written = [%td]; "
  55. "expected [%td]\n", __FUNCTION__, octets_written, size);
  56. rc = -EINVAL;
  57. }
  58. mutex_unlock(&inode_info->lower_file_mutex);
  59. mark_inode_dirty_sync(ecryptfs_inode);
  60. return rc;
  61. }
  62. /**
  63. * ecryptfs_write_lower_page_segment
  64. * @ecryptfs_inode: The eCryptfs inode
  65. * @page_for_lower: The page containing the data to be written to the
  66. * lower file
  67. * @offset_in_page: The offset in the @page_for_lower from which to
  68. * start writing the data
  69. * @size: The amount of data from @page_for_lower to write to the
  70. * lower file
  71. *
  72. * Determines the byte offset in the file for the given page and
  73. * offset within the page, maps the page, and makes the call to write
  74. * the contents of @page_for_lower to the lower inode.
  75. *
  76. * Returns zero on success; non-zero otherwise
  77. */
  78. int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
  79. struct page *page_for_lower,
  80. size_t offset_in_page, size_t size)
  81. {
  82. char *virt;
  83. loff_t offset;
  84. int rc;
  85. offset = (page_for_lower->index << PAGE_CACHE_SHIFT) + offset_in_page;
  86. virt = kmap(page_for_lower);
  87. rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
  88. kunmap(page_for_lower);
  89. return rc;
  90. }
  91. /**
  92. * ecryptfs_write
  93. * @ecryptfs_file: The eCryptfs file into which to write
  94. * @data: Virtual address where data to write is located
  95. * @offset: Offset in the eCryptfs file at which to begin writing the
  96. * data from @data
  97. * @size: The number of bytes to write from @data
  98. *
  99. * Write an arbitrary amount of data to an arbitrary location in the
  100. * eCryptfs inode page cache. This is done on a page-by-page, and then
  101. * by an extent-by-extent, basis; individual extents are encrypted and
  102. * written to the lower page cache (via VFS writes). This function
  103. * takes care of all the address translation to locations in the lower
  104. * filesystem; it also handles truncate events, writing out zeros
  105. * where necessary.
  106. *
  107. * Returns zero on success; non-zero otherwise
  108. */
  109. int ecryptfs_write(struct file *ecryptfs_file, char *data, loff_t offset,
  110. size_t size)
  111. {
  112. struct page *ecryptfs_page;
  113. char *ecryptfs_page_virt;
  114. u64 ecryptfs_file_size = i_size_read(ecryptfs_file->f_dentry->d_inode);
  115. loff_t data_offset = 0;
  116. loff_t pos;
  117. int rc = 0;
  118. if (offset > ecryptfs_file_size)
  119. pos = ecryptfs_file_size;
  120. else
  121. pos = offset;
  122. while (pos < (offset + size)) {
  123. pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
  124. size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
  125. size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
  126. size_t total_remaining_bytes = ((offset + size) - pos);
  127. if (num_bytes > total_remaining_bytes)
  128. num_bytes = total_remaining_bytes;
  129. if (pos < offset) {
  130. size_t total_remaining_zeros = (offset - pos);
  131. if (num_bytes > total_remaining_zeros)
  132. num_bytes = total_remaining_zeros;
  133. }
  134. ecryptfs_page = ecryptfs_get1page(ecryptfs_file,
  135. ecryptfs_page_idx);
  136. if (IS_ERR(ecryptfs_page)) {
  137. rc = PTR_ERR(ecryptfs_page);
  138. printk(KERN_ERR "%s: Error getting page at "
  139. "index [%ld] from eCryptfs inode "
  140. "mapping; rc = [%d]\n", __FUNCTION__,
  141. ecryptfs_page_idx, rc);
  142. goto out;
  143. }
  144. if (start_offset_in_page) {
  145. /* Read in the page from the lower
  146. * into the eCryptfs inode page cache,
  147. * decrypting */
  148. rc = ecryptfs_decrypt_page(ecryptfs_page);
  149. if (rc) {
  150. printk(KERN_ERR "%s: Error decrypting "
  151. "page; rc = [%d]\n",
  152. __FUNCTION__, rc);
  153. page_cache_release(ecryptfs_page);
  154. goto out;
  155. }
  156. }
  157. ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
  158. if (pos >= offset) {
  159. memcpy(((char *)ecryptfs_page_virt
  160. + start_offset_in_page),
  161. (data + data_offset), num_bytes);
  162. data_offset += num_bytes;
  163. } else {
  164. /* We are extending past the previous end of the file.
  165. * Fill in zero values up to the start of where we
  166. * will be writing data. */
  167. memset(((char *)ecryptfs_page_virt
  168. + start_offset_in_page), 0, num_bytes);
  169. }
  170. kunmap_atomic(ecryptfs_page_virt, KM_USER0);
  171. flush_dcache_page(ecryptfs_page);
  172. rc = ecryptfs_encrypt_page(ecryptfs_page);
  173. if (rc) {
  174. printk(KERN_ERR "%s: Error encrypting "
  175. "page; rc = [%d]\n", __FUNCTION__, rc);
  176. page_cache_release(ecryptfs_page);
  177. goto out;
  178. }
  179. page_cache_release(ecryptfs_page);
  180. pos += num_bytes;
  181. }
  182. if ((offset + size) > ecryptfs_file_size) {
  183. i_size_write(ecryptfs_file->f_dentry->d_inode, (offset + size));
  184. rc = ecryptfs_write_inode_size_to_metadata(
  185. ecryptfs_file->f_dentry->d_inode);
  186. if (rc) {
  187. printk(KERN_ERR "Problem with "
  188. "ecryptfs_write_inode_size_to_metadata; "
  189. "rc = [%d]\n", rc);
  190. goto out;
  191. }
  192. }
  193. out:
  194. return rc;
  195. }
  196. /**
  197. * ecryptfs_read_lower
  198. * @data: The read data is stored here by this function
  199. * @offset: Byte offset in the lower file from which to read the data
  200. * @size: Number of bytes to read from @offset of the lower file and
  201. * store into @data
  202. * @ecryptfs_inode: The eCryptfs inode
  203. *
  204. * Read @size bytes of data at byte offset @offset from the lower
  205. * inode into memory location @data.
  206. *
  207. * Returns zero on success; non-zero on error
  208. */
  209. int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
  210. struct inode *ecryptfs_inode)
  211. {
  212. struct ecryptfs_inode_info *inode_info =
  213. ecryptfs_inode_to_private(ecryptfs_inode);
  214. ssize_t octets_read;
  215. mm_segment_t fs_save;
  216. size_t i;
  217. int rc = 0;
  218. mutex_lock(&inode_info->lower_file_mutex);
  219. BUG_ON(!inode_info->lower_file);
  220. inode_info->lower_file->f_pos = offset;
  221. fs_save = get_fs();
  222. set_fs(get_ds());
  223. octets_read = vfs_read(inode_info->lower_file, data, size,
  224. &inode_info->lower_file->f_pos);
  225. set_fs(fs_save);
  226. if (octets_read < 0) {
  227. printk(KERN_ERR "%s: octets_read = [%td]; "
  228. "expected [%td]\n", __FUNCTION__, octets_read, size);
  229. rc = -EINVAL;
  230. }
  231. mutex_unlock(&inode_info->lower_file_mutex);
  232. for (i = 0; i < size; i += PAGE_CACHE_SIZE) {
  233. struct page *data_page;
  234. data_page = virt_to_page(data + i);
  235. flush_dcache_page(data_page);
  236. if (rc)
  237. ClearPageUptodate(data_page);
  238. else
  239. SetPageUptodate(data_page);
  240. }
  241. return rc;
  242. }
  243. /**
  244. * ecryptfs_read_lower_page_segment
  245. * @page_for_ecryptfs: The page into which data for eCryptfs will be
  246. * written
  247. * @offset_in_page: Offset in @page_for_ecryptfs from which to start
  248. * writing
  249. * @size: The number of bytes to write into @page_for_ecryptfs
  250. * @ecryptfs_inode: The eCryptfs inode
  251. *
  252. * Determines the byte offset in the file for the given page and
  253. * offset within the page, maps the page, and makes the call to read
  254. * the contents of @page_for_ecryptfs from the lower inode.
  255. *
  256. * Returns zero on success; non-zero otherwise
  257. */
  258. int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
  259. pgoff_t page_index,
  260. size_t offset_in_page, size_t size,
  261. struct inode *ecryptfs_inode)
  262. {
  263. char *virt;
  264. loff_t offset;
  265. int rc;
  266. offset = ((page_index << PAGE_CACHE_SHIFT) + offset_in_page);
  267. virt = kmap(page_for_ecryptfs);
  268. rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
  269. kunmap(page_for_ecryptfs);
  270. return rc;
  271. }
  272. /**
  273. * ecryptfs_read
  274. * @data: The virtual address into which to write the data read (and
  275. * possibly decrypted) from the lower file
  276. * @offset: The offset in the decrypted view of the file from which to
  277. * read into @data
  278. * @size: The number of bytes to read into @data
  279. * @ecryptfs_file: The eCryptfs file from which to read
  280. *
  281. * Read an arbitrary amount of data from an arbitrary location in the
  282. * eCryptfs page cache. This is done on an extent-by-extent basis;
  283. * individual extents are decrypted and read from the lower page
  284. * cache (via VFS reads). This function takes care of all the
  285. * address translation to locations in the lower filesystem.
  286. *
  287. * Returns zero on success; non-zero otherwise
  288. */
  289. int ecryptfs_read(char *data, loff_t offset, size_t size,
  290. struct file *ecryptfs_file)
  291. {
  292. struct page *ecryptfs_page;
  293. char *ecryptfs_page_virt;
  294. u64 ecryptfs_file_size = i_size_read(ecryptfs_file->f_dentry->d_inode);
  295. loff_t data_offset = 0;
  296. loff_t pos;
  297. int rc = 0;
  298. if ((offset + size) > ecryptfs_file_size) {
  299. rc = -EINVAL;
  300. printk(KERN_ERR "%s: Attempt to read data past the end of the "
  301. "file; offset = [%lld]; size = [%td]; "
  302. "ecryptfs_file_size = [%lld]\n",
  303. __FUNCTION__, offset, size, ecryptfs_file_size);
  304. goto out;
  305. }
  306. pos = offset;
  307. while (pos < (offset + size)) {
  308. pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
  309. size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
  310. size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
  311. size_t total_remaining_bytes = ((offset + size) - pos);
  312. if (num_bytes > total_remaining_bytes)
  313. num_bytes = total_remaining_bytes;
  314. ecryptfs_page = ecryptfs_get1page(ecryptfs_file,
  315. ecryptfs_page_idx);
  316. if (IS_ERR(ecryptfs_page)) {
  317. rc = PTR_ERR(ecryptfs_page);
  318. printk(KERN_ERR "%s: Error getting page at "
  319. "index [%ld] from eCryptfs inode "
  320. "mapping; rc = [%d]\n", __FUNCTION__,
  321. ecryptfs_page_idx, rc);
  322. goto out;
  323. }
  324. rc = ecryptfs_decrypt_page(ecryptfs_page);
  325. if (rc) {
  326. printk(KERN_ERR "%s: Error decrypting "
  327. "page; rc = [%d]\n", __FUNCTION__, rc);
  328. page_cache_release(ecryptfs_page);
  329. goto out;
  330. }
  331. ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
  332. memcpy((data + data_offset),
  333. ((char *)ecryptfs_page_virt + start_offset_in_page),
  334. num_bytes);
  335. kunmap_atomic(ecryptfs_page_virt, KM_USER0);
  336. page_cache_release(ecryptfs_page);
  337. pos += num_bytes;
  338. data_offset += num_bytes;
  339. }
  340. out:
  341. return rc;
  342. }