read_write.c 12 KB

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