read_write.c 11 KB

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