read_write.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. mm_segment_t fs_save;
  42. ssize_t rc;
  43. lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
  44. if (!lower_file)
  45. return -EIO;
  46. fs_save = get_fs();
  47. set_fs(get_ds());
  48. rc = vfs_write(lower_file, data, size, &offset);
  49. set_fs(fs_save);
  50. mark_inode_dirty_sync(ecryptfs_inode);
  51. return rc;
  52. }
  53. /**
  54. * ecryptfs_write_lower_page_segment
  55. * @ecryptfs_inode: The eCryptfs inode
  56. * @page_for_lower: The page containing the data to be written to the
  57. * lower file
  58. * @offset_in_page: The offset in the @page_for_lower from which to
  59. * start writing the data
  60. * @size: The amount of data from @page_for_lower to write to the
  61. * lower file
  62. *
  63. * Determines the byte offset in the file for the given page and
  64. * offset within the page, maps the page, and makes the call to write
  65. * the contents of @page_for_lower to the lower inode.
  66. *
  67. * Returns zero on success; non-zero otherwise
  68. */
  69. int ecryptfs_write_lower_page_segment(struct inode *ecryptfs_inode,
  70. struct page *page_for_lower,
  71. size_t offset_in_page, size_t size)
  72. {
  73. char *virt;
  74. loff_t offset;
  75. int rc;
  76. offset = ((((loff_t)page_for_lower->index) << PAGE_CACHE_SHIFT)
  77. + offset_in_page);
  78. virt = kmap(page_for_lower);
  79. rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size);
  80. if (rc > 0)
  81. rc = 0;
  82. kunmap(page_for_lower);
  83. return rc;
  84. }
  85. /**
  86. * ecryptfs_write
  87. * @ecryptfs_inode: The eCryptfs file into which to write
  88. * @data: Virtual address where data to write is located
  89. * @offset: Offset in the eCryptfs file at which to begin writing the
  90. * data from @data
  91. * @size: The number of bytes to write from @data
  92. *
  93. * Write an arbitrary amount of data to an arbitrary location in the
  94. * eCryptfs inode page cache. This is done on a page-by-page, and then
  95. * by an extent-by-extent, basis; individual extents are encrypted and
  96. * written to the lower page cache (via VFS writes). This function
  97. * takes care of all the address translation to locations in the lower
  98. * filesystem; it also handles truncate events, writing out zeros
  99. * where necessary.
  100. *
  101. * Returns zero on success; non-zero otherwise
  102. */
  103. int ecryptfs_write(struct inode *ecryptfs_inode, char *data, loff_t offset,
  104. size_t size)
  105. {
  106. struct page *ecryptfs_page;
  107. struct ecryptfs_crypt_stat *crypt_stat;
  108. char *ecryptfs_page_virt;
  109. loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
  110. loff_t data_offset = 0;
  111. loff_t pos;
  112. int rc = 0;
  113. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  114. /*
  115. * if we are writing beyond current size, then start pos
  116. * at the current size - we'll fill in zeros from there.
  117. */
  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. /* remaining zeros to write, up to destination offset */
  131. size_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, KM_USER0);
  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, KM_USER0);
  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 ((offset + size) > ecryptfs_file_size) {
  186. i_size_write(ecryptfs_inode, (offset + size));
  187. if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) {
  188. rc = ecryptfs_write_inode_size_to_metadata(
  189. ecryptfs_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. }
  198. out:
  199. return rc;
  200. }
  201. /**
  202. * ecryptfs_read_lower
  203. * @data: The read data is stored here by this function
  204. * @offset: Byte offset in the lower file from which to read the data
  205. * @size: Number of bytes to read from @offset of the lower file and
  206. * store into @data
  207. * @ecryptfs_inode: The eCryptfs inode
  208. *
  209. * Read @size bytes of data at byte offset @offset from the lower
  210. * inode into memory location @data.
  211. *
  212. * Returns bytes read on success; 0 on EOF; less than zero on error
  213. */
  214. int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
  215. struct inode *ecryptfs_inode)
  216. {
  217. struct file *lower_file;
  218. mm_segment_t fs_save;
  219. ssize_t rc;
  220. lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
  221. if (!lower_file)
  222. return -EIO;
  223. fs_save = get_fs();
  224. set_fs(get_ds());
  225. rc = vfs_read(lower_file, data, size, &offset);
  226. set_fs(fs_save);
  227. return rc;
  228. }
  229. /**
  230. * ecryptfs_read_lower_page_segment
  231. * @page_for_ecryptfs: The page into which data for eCryptfs will be
  232. * written
  233. * @offset_in_page: Offset in @page_for_ecryptfs from which to start
  234. * writing
  235. * @size: The number of bytes to write into @page_for_ecryptfs
  236. * @ecryptfs_inode: The eCryptfs inode
  237. *
  238. * Determines the byte offset in the file for the given page and
  239. * offset within the page, maps the page, and makes the call to read
  240. * the contents of @page_for_ecryptfs from the lower inode.
  241. *
  242. * Returns zero on success; non-zero otherwise
  243. */
  244. int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
  245. pgoff_t page_index,
  246. size_t offset_in_page, size_t size,
  247. struct inode *ecryptfs_inode)
  248. {
  249. char *virt;
  250. loff_t offset;
  251. int rc;
  252. offset = ((((loff_t)page_index) << PAGE_CACHE_SHIFT) + offset_in_page);
  253. virt = kmap(page_for_ecryptfs);
  254. rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
  255. if (rc > 0)
  256. rc = 0;
  257. kunmap(page_for_ecryptfs);
  258. flush_dcache_page(page_for_ecryptfs);
  259. return rc;
  260. }
  261. #if 0
  262. /**
  263. * ecryptfs_read
  264. * @data: The virtual address into which to write the data read (and
  265. * possibly decrypted) from the lower file
  266. * @offset: The offset in the decrypted view of the file from which to
  267. * read into @data
  268. * @size: The number of bytes to read into @data
  269. * @ecryptfs_file: The eCryptfs file from which to read
  270. *
  271. * Read an arbitrary amount of data from an arbitrary location in the
  272. * eCryptfs page cache. This is done on an extent-by-extent basis;
  273. * individual extents are decrypted and read from the lower page
  274. * cache (via VFS reads). This function takes care of all the
  275. * address translation to locations in the lower filesystem.
  276. *
  277. * Returns zero on success; non-zero otherwise
  278. */
  279. int ecryptfs_read(char *data, loff_t offset, size_t size,
  280. struct file *ecryptfs_file)
  281. {
  282. struct inode *ecryptfs_inode = ecryptfs_file->f_dentry->d_inode;
  283. struct page *ecryptfs_page;
  284. char *ecryptfs_page_virt;
  285. loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
  286. loff_t data_offset = 0;
  287. loff_t pos;
  288. int rc = 0;
  289. if ((offset + size) > ecryptfs_file_size) {
  290. rc = -EINVAL;
  291. printk(KERN_ERR "%s: Attempt to read data past the end of the "
  292. "file; offset = [%lld]; size = [%td]; "
  293. "ecryptfs_file_size = [%lld]\n",
  294. __func__, offset, size, ecryptfs_file_size);
  295. goto out;
  296. }
  297. pos = offset;
  298. while (pos < (offset + size)) {
  299. pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
  300. size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
  301. size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
  302. size_t total_remaining_bytes = ((offset + size) - pos);
  303. if (num_bytes > total_remaining_bytes)
  304. num_bytes = total_remaining_bytes;
  305. ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_inode,
  306. ecryptfs_page_idx);
  307. if (IS_ERR(ecryptfs_page)) {
  308. rc = PTR_ERR(ecryptfs_page);
  309. printk(KERN_ERR "%s: Error getting page at "
  310. "index [%ld] from eCryptfs inode "
  311. "mapping; rc = [%d]\n", __func__,
  312. ecryptfs_page_idx, rc);
  313. goto out;
  314. }
  315. ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
  316. memcpy((data + data_offset),
  317. ((char *)ecryptfs_page_virt + start_offset_in_page),
  318. num_bytes);
  319. kunmap_atomic(ecryptfs_page_virt, KM_USER0);
  320. flush_dcache_page(ecryptfs_page);
  321. SetPageUptodate(ecryptfs_page);
  322. unlock_page(ecryptfs_page);
  323. page_cache_release(ecryptfs_page);
  324. pos += num_bytes;
  325. data_offset += num_bytes;
  326. }
  327. out:
  328. return rc;
  329. }
  330. #endif /* 0 */