read_write.c 11 KB

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