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 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. loff_t total_remaining_bytes = ((offset + size) - pos);
  127. if (fatal_signal_pending(current)) {
  128. rc = -EINTR;
  129. break;
  130. }
  131. if (num_bytes > total_remaining_bytes)
  132. num_bytes = total_remaining_bytes;
  133. if (pos < offset) {
  134. /* remaining zeros to write, up to destination offset */
  135. loff_t total_remaining_zeros = (offset - pos);
  136. if (num_bytes > total_remaining_zeros)
  137. num_bytes = total_remaining_zeros;
  138. }
  139. ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_inode,
  140. ecryptfs_page_idx);
  141. if (IS_ERR(ecryptfs_page)) {
  142. rc = PTR_ERR(ecryptfs_page);
  143. printk(KERN_ERR "%s: Error getting page at "
  144. "index [%ld] from eCryptfs inode "
  145. "mapping; rc = [%d]\n", __func__,
  146. ecryptfs_page_idx, rc);
  147. goto out;
  148. }
  149. ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
  150. /*
  151. * pos: where we're now writing, offset: where the request was
  152. * If current pos is before request, we are filling zeros
  153. * If we are at or beyond request, we are writing the *data*
  154. * If we're in a fresh page beyond eof, zero it in either case
  155. */
  156. if (pos < offset || !start_offset_in_page) {
  157. /* We are extending past the previous end of the file.
  158. * Fill in zero values to the end of the page */
  159. memset(((char *)ecryptfs_page_virt
  160. + start_offset_in_page), 0,
  161. PAGE_CACHE_SIZE - start_offset_in_page);
  162. }
  163. /* pos >= offset, we are now writing the data request */
  164. if (pos >= offset) {
  165. memcpy(((char *)ecryptfs_page_virt
  166. + start_offset_in_page),
  167. (data + data_offset), num_bytes);
  168. data_offset += num_bytes;
  169. }
  170. kunmap_atomic(ecryptfs_page_virt, KM_USER0);
  171. flush_dcache_page(ecryptfs_page);
  172. SetPageUptodate(ecryptfs_page);
  173. unlock_page(ecryptfs_page);
  174. if (crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  175. rc = ecryptfs_encrypt_page(ecryptfs_page);
  176. else
  177. rc = ecryptfs_write_lower_page_segment(ecryptfs_inode,
  178. ecryptfs_page,
  179. start_offset_in_page,
  180. data_offset);
  181. page_cache_release(ecryptfs_page);
  182. if (rc) {
  183. printk(KERN_ERR "%s: Error encrypting "
  184. "page; rc = [%d]\n", __func__, rc);
  185. goto out;
  186. }
  187. pos += num_bytes;
  188. }
  189. if (pos > ecryptfs_file_size) {
  190. i_size_write(ecryptfs_inode, pos);
  191. if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) {
  192. int rc2;
  193. rc2 = ecryptfs_write_inode_size_to_metadata(
  194. ecryptfs_inode);
  195. if (rc2) {
  196. printk(KERN_ERR "Problem with "
  197. "ecryptfs_write_inode_size_to_metadata; "
  198. "rc = [%d]\n", rc2);
  199. if (!rc)
  200. rc = rc2;
  201. goto out;
  202. }
  203. }
  204. }
  205. out:
  206. return rc;
  207. }
  208. /**
  209. * ecryptfs_read_lower
  210. * @data: The read data is stored here by this function
  211. * @offset: Byte offset in the lower file from which to read the data
  212. * @size: Number of bytes to read from @offset of the lower file and
  213. * store into @data
  214. * @ecryptfs_inode: The eCryptfs inode
  215. *
  216. * Read @size bytes of data at byte offset @offset from the lower
  217. * inode into memory location @data.
  218. *
  219. * Returns bytes read on success; 0 on EOF; less than zero on error
  220. */
  221. int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
  222. struct inode *ecryptfs_inode)
  223. {
  224. struct file *lower_file;
  225. mm_segment_t fs_save;
  226. ssize_t rc;
  227. lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
  228. if (!lower_file)
  229. return -EIO;
  230. fs_save = get_fs();
  231. set_fs(get_ds());
  232. rc = vfs_read(lower_file, data, size, &offset);
  233. set_fs(fs_save);
  234. return rc;
  235. }
  236. /**
  237. * ecryptfs_read_lower_page_segment
  238. * @page_for_ecryptfs: The page into which data for eCryptfs will be
  239. * written
  240. * @offset_in_page: Offset in @page_for_ecryptfs from which to start
  241. * writing
  242. * @size: The number of bytes to write into @page_for_ecryptfs
  243. * @ecryptfs_inode: The eCryptfs inode
  244. *
  245. * Determines the byte offset in the file for the given page and
  246. * offset within the page, maps the page, and makes the call to read
  247. * the contents of @page_for_ecryptfs from the lower inode.
  248. *
  249. * Returns zero on success; non-zero otherwise
  250. */
  251. int ecryptfs_read_lower_page_segment(struct page *page_for_ecryptfs,
  252. pgoff_t page_index,
  253. size_t offset_in_page, size_t size,
  254. struct inode *ecryptfs_inode)
  255. {
  256. char *virt;
  257. loff_t offset;
  258. int rc;
  259. offset = ((((loff_t)page_index) << PAGE_CACHE_SHIFT) + offset_in_page);
  260. virt = kmap(page_for_ecryptfs);
  261. rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode);
  262. if (rc > 0)
  263. rc = 0;
  264. kunmap(page_for_ecryptfs);
  265. flush_dcache_page(page_for_ecryptfs);
  266. return rc;
  267. }
  268. #if 0
  269. /**
  270. * ecryptfs_read
  271. * @data: The virtual address into which to write the data read (and
  272. * possibly decrypted) from the lower file
  273. * @offset: The offset in the decrypted view of the file from which to
  274. * read into @data
  275. * @size: The number of bytes to read into @data
  276. * @ecryptfs_file: The eCryptfs file from which to read
  277. *
  278. * Read an arbitrary amount of data from an arbitrary location in the
  279. * eCryptfs page cache. This is done on an extent-by-extent basis;
  280. * individual extents are decrypted and read from the lower page
  281. * cache (via VFS reads). This function takes care of all the
  282. * address translation to locations in the lower filesystem.
  283. *
  284. * Returns zero on success; non-zero otherwise
  285. */
  286. int ecryptfs_read(char *data, loff_t offset, size_t size,
  287. struct file *ecryptfs_file)
  288. {
  289. struct inode *ecryptfs_inode = ecryptfs_file->f_dentry->d_inode;
  290. struct page *ecryptfs_page;
  291. char *ecryptfs_page_virt;
  292. loff_t ecryptfs_file_size = i_size_read(ecryptfs_inode);
  293. loff_t data_offset = 0;
  294. loff_t pos;
  295. int rc = 0;
  296. if ((offset + size) > ecryptfs_file_size) {
  297. rc = -EINVAL;
  298. printk(KERN_ERR "%s: Attempt to read data past the end of the "
  299. "file; offset = [%lld]; size = [%td]; "
  300. "ecryptfs_file_size = [%lld]\n",
  301. __func__, offset, size, ecryptfs_file_size);
  302. goto out;
  303. }
  304. pos = offset;
  305. while (pos < (offset + size)) {
  306. pgoff_t ecryptfs_page_idx = (pos >> PAGE_CACHE_SHIFT);
  307. size_t start_offset_in_page = (pos & ~PAGE_CACHE_MASK);
  308. size_t num_bytes = (PAGE_CACHE_SIZE - start_offset_in_page);
  309. size_t total_remaining_bytes = ((offset + size) - pos);
  310. if (num_bytes > total_remaining_bytes)
  311. num_bytes = total_remaining_bytes;
  312. ecryptfs_page = ecryptfs_get_locked_page(ecryptfs_inode,
  313. ecryptfs_page_idx);
  314. if (IS_ERR(ecryptfs_page)) {
  315. rc = PTR_ERR(ecryptfs_page);
  316. printk(KERN_ERR "%s: Error getting page at "
  317. "index [%ld] from eCryptfs inode "
  318. "mapping; rc = [%d]\n", __func__,
  319. ecryptfs_page_idx, rc);
  320. goto out;
  321. }
  322. ecryptfs_page_virt = kmap_atomic(ecryptfs_page, KM_USER0);
  323. memcpy((data + data_offset),
  324. ((char *)ecryptfs_page_virt + start_offset_in_page),
  325. num_bytes);
  326. kunmap_atomic(ecryptfs_page_virt, KM_USER0);
  327. flush_dcache_page(ecryptfs_page);
  328. SetPageUptodate(ecryptfs_page);
  329. unlock_page(ecryptfs_page);
  330. page_cache_release(ecryptfs_page);
  331. pos += num_bytes;
  332. data_offset += num_bytes;
  333. }
  334. out:
  335. return rc;
  336. }
  337. #endif /* 0 */