read_write.c 12 KB

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