read_write.c 11 KB

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