mmap.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. * This is where eCryptfs coordinates the symmetric encryption and
  4. * decryption of the file data as it passes between the lower
  5. * encrypted file and the upper decrypted file.
  6. *
  7. * Copyright (C) 1997-2003 Erez Zadok
  8. * Copyright (C) 2001-2003 Stony Brook University
  9. * Copyright (C) 2004-2007 International Business Machines Corp.
  10. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  25. * 02111-1307, USA.
  26. */
  27. #include <linux/pagemap.h>
  28. #include <linux/writeback.h>
  29. #include <linux/page-flags.h>
  30. #include <linux/mount.h>
  31. #include <linux/file.h>
  32. #include <linux/crypto.h>
  33. #include <linux/scatterlist.h>
  34. #include "ecryptfs_kernel.h"
  35. struct kmem_cache *ecryptfs_lower_page_cache;
  36. /**
  37. * ecryptfs_get1page
  38. *
  39. * Get one page from cache or lower f/s, return error otherwise.
  40. *
  41. * Returns unlocked and up-to-date page (if ok), with increased
  42. * refcnt.
  43. */
  44. struct page *ecryptfs_get1page(struct file *file, loff_t index)
  45. {
  46. struct dentry *dentry;
  47. struct inode *inode;
  48. struct address_space *mapping;
  49. dentry = file->f_path.dentry;
  50. inode = dentry->d_inode;
  51. mapping = inode->i_mapping;
  52. return read_mapping_page(mapping, index, (void *)file);
  53. }
  54. /**
  55. * ecryptfs_fill_zeros
  56. * @file: The ecryptfs file
  57. * @new_length: The new length of the data in the underlying file;
  58. * everything between the prior end of the file and the
  59. * new end of the file will be filled with zero's.
  60. * new_length must be greater than current length
  61. *
  62. * Function for handling lseek-ing past the end of the file.
  63. *
  64. * This function does not support shrinking, only growing a file.
  65. *
  66. * Returns zero on success; non-zero otherwise.
  67. */
  68. int ecryptfs_fill_zeros(struct file *file, loff_t new_length)
  69. {
  70. int rc = 0;
  71. struct dentry *dentry = file->f_path.dentry;
  72. struct inode *inode = dentry->d_inode;
  73. pgoff_t old_end_page_index = 0;
  74. pgoff_t index = old_end_page_index;
  75. int old_end_pos_in_page = -1;
  76. pgoff_t new_end_page_index;
  77. int new_end_pos_in_page;
  78. loff_t cur_length = i_size_read(inode);
  79. if (cur_length != 0) {
  80. index = old_end_page_index =
  81. ((cur_length - 1) >> PAGE_CACHE_SHIFT);
  82. old_end_pos_in_page = ((cur_length - 1) & ~PAGE_CACHE_MASK);
  83. }
  84. new_end_page_index = ((new_length - 1) >> PAGE_CACHE_SHIFT);
  85. new_end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK);
  86. ecryptfs_printk(KERN_DEBUG, "old_end_page_index = [0x%.16x]; "
  87. "old_end_pos_in_page = [%d]; "
  88. "new_end_page_index = [0x%.16x]; "
  89. "new_end_pos_in_page = [%d]\n",
  90. old_end_page_index, old_end_pos_in_page,
  91. new_end_page_index, new_end_pos_in_page);
  92. if (old_end_page_index == new_end_page_index) {
  93. /* Start and end are in the same page; we just need to
  94. * set a portion of the existing page to zero's */
  95. rc = ecryptfs_write_zeros(file, index,
  96. (old_end_pos_in_page + 1),
  97. (new_end_pos_in_page
  98. - old_end_pos_in_page));
  99. if (rc)
  100. ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros("
  101. "file=[%p], "
  102. "index=[0x%.16x], "
  103. "old_end_pos_in_page=[d], "
  104. "(PAGE_CACHE_SIZE - new_end_pos_in_page"
  105. "=[%d]"
  106. ")=[d]) returned [%d]\n", file, index,
  107. old_end_pos_in_page,
  108. new_end_pos_in_page,
  109. (PAGE_CACHE_SIZE - new_end_pos_in_page),
  110. rc);
  111. goto out;
  112. }
  113. /* Fill the remainder of the previous last page with zeros */
  114. rc = ecryptfs_write_zeros(file, index, (old_end_pos_in_page + 1),
  115. ((PAGE_CACHE_SIZE - 1) - old_end_pos_in_page));
  116. if (rc) {
  117. ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(file=[%p], "
  118. "index=[0x%.16x], old_end_pos_in_page=[d], "
  119. "(PAGE_CACHE_SIZE - old_end_pos_in_page)=[d]) "
  120. "returned [%d]\n", file, index,
  121. old_end_pos_in_page,
  122. (PAGE_CACHE_SIZE - old_end_pos_in_page), rc);
  123. goto out;
  124. }
  125. index++;
  126. while (index < new_end_page_index) {
  127. /* Fill all intermediate pages with zeros */
  128. rc = ecryptfs_write_zeros(file, index, 0, PAGE_CACHE_SIZE);
  129. if (rc) {
  130. ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros("
  131. "file=[%p], "
  132. "index=[0x%.16x], "
  133. "old_end_pos_in_page=[d], "
  134. "(PAGE_CACHE_SIZE - new_end_pos_in_page"
  135. "=[%d]"
  136. ")=[d]) returned [%d]\n", file, index,
  137. old_end_pos_in_page,
  138. new_end_pos_in_page,
  139. (PAGE_CACHE_SIZE - new_end_pos_in_page),
  140. rc);
  141. goto out;
  142. }
  143. index++;
  144. }
  145. /* Fill the portion at the beginning of the last new page with
  146. * zero's */
  147. rc = ecryptfs_write_zeros(file, index, 0, (new_end_pos_in_page + 1));
  148. if (rc) {
  149. ecryptfs_printk(KERN_ERR, "ecryptfs_write_zeros(file="
  150. "[%p], index=[0x%.16x], 0, "
  151. "new_end_pos_in_page=[%d]"
  152. "returned [%d]\n", file, index,
  153. new_end_pos_in_page, rc);
  154. goto out;
  155. }
  156. out:
  157. return rc;
  158. }
  159. /**
  160. * ecryptfs_writepage
  161. * @page: Page that is locked before this call is made
  162. *
  163. * Returns zero on success; non-zero otherwise
  164. */
  165. static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
  166. {
  167. int rc;
  168. rc = ecryptfs_encrypt_page(page);
  169. if (rc) {
  170. ecryptfs_printk(KERN_WARNING, "Error encrypting "
  171. "page (upper index [0x%.16x])\n", page->index);
  172. ClearPageUptodate(page);
  173. goto out;
  174. }
  175. SetPageUptodate(page);
  176. unlock_page(page);
  177. out:
  178. return rc;
  179. }
  180. /**
  181. * Reads the data from the lower file file at index lower_page_index
  182. * and copies that data into page.
  183. *
  184. * @param page Page to fill
  185. * @param lower_page_index Index of the page in the lower file to get
  186. */
  187. int ecryptfs_do_readpage(struct file *file, struct page *page,
  188. pgoff_t lower_page_index)
  189. {
  190. int rc;
  191. struct dentry *dentry;
  192. struct file *lower_file;
  193. struct dentry *lower_dentry;
  194. struct inode *inode;
  195. struct inode *lower_inode;
  196. char *page_data;
  197. struct page *lower_page = NULL;
  198. char *lower_page_data;
  199. const struct address_space_operations *lower_a_ops;
  200. dentry = file->f_path.dentry;
  201. lower_file = ecryptfs_file_to_lower(file);
  202. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  203. inode = dentry->d_inode;
  204. lower_inode = ecryptfs_inode_to_lower(inode);
  205. lower_a_ops = lower_inode->i_mapping->a_ops;
  206. lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index,
  207. (filler_t *)lower_a_ops->readpage,
  208. (void *)lower_file);
  209. if (IS_ERR(lower_page)) {
  210. rc = PTR_ERR(lower_page);
  211. lower_page = NULL;
  212. ecryptfs_printk(KERN_ERR, "Error reading from page cache\n");
  213. goto out;
  214. }
  215. page_data = kmap_atomic(page, KM_USER0);
  216. lower_page_data = kmap_atomic(lower_page, KM_USER1);
  217. memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
  218. kunmap_atomic(lower_page_data, KM_USER1);
  219. kunmap_atomic(page_data, KM_USER0);
  220. flush_dcache_page(page);
  221. rc = 0;
  222. out:
  223. if (likely(lower_page))
  224. page_cache_release(lower_page);
  225. if (rc == 0)
  226. SetPageUptodate(page);
  227. else
  228. ClearPageUptodate(page);
  229. return rc;
  230. }
  231. /**
  232. * Header Extent:
  233. * Octets 0-7: Unencrypted file size (big-endian)
  234. * Octets 8-15: eCryptfs special marker
  235. * Octets 16-19: Flags
  236. * Octet 16: File format version number (between 0 and 255)
  237. * Octets 17-18: Reserved
  238. * Octet 19: Bit 1 (lsb): Reserved
  239. * Bit 2: Encrypted?
  240. * Bits 3-8: Reserved
  241. * Octets 20-23: Header extent size (big-endian)
  242. * Octets 24-25: Number of header extents at front of file
  243. * (big-endian)
  244. * Octet 26: Begin RFC 2440 authentication token packet set
  245. */
  246. static void set_header_info(char *page_virt,
  247. struct ecryptfs_crypt_stat *crypt_stat)
  248. {
  249. size_t written;
  250. int save_num_header_extents_at_front =
  251. crypt_stat->num_header_extents_at_front;
  252. crypt_stat->num_header_extents_at_front = 1;
  253. ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
  254. crypt_stat->num_header_extents_at_front =
  255. save_num_header_extents_at_front;
  256. }
  257. /**
  258. * ecryptfs_copy_up_encrypted_with_header
  259. * @page: Sort of a ``virtual'' representation of the encrypted lower
  260. * file. The actual lower file does not have the metadata in
  261. * the header. This is locked.
  262. * @crypt_stat: The eCryptfs inode's cryptographic context
  263. *
  264. * The ``view'' is the version of the file that userspace winds up
  265. * seeing, with the header information inserted.
  266. */
  267. static int
  268. ecryptfs_copy_up_encrypted_with_header(struct page *page,
  269. struct ecryptfs_crypt_stat *crypt_stat)
  270. {
  271. loff_t extent_num_in_page = 0;
  272. loff_t num_extents_per_page = (PAGE_CACHE_SIZE
  273. / crypt_stat->extent_size);
  274. int rc = 0;
  275. while (extent_num_in_page < num_extents_per_page) {
  276. loff_t view_extent_num = ((page->index * num_extents_per_page)
  277. + extent_num_in_page);
  278. if (view_extent_num < crypt_stat->num_header_extents_at_front) {
  279. /* This is a header extent */
  280. char *page_virt;
  281. page_virt = kmap_atomic(page, KM_USER0);
  282. memset(page_virt, 0, PAGE_CACHE_SIZE);
  283. /* TODO: Support more than one header extent */
  284. if (view_extent_num == 0) {
  285. rc = ecryptfs_read_xattr_region(
  286. page_virt, page->mapping->host);
  287. set_header_info(page_virt, crypt_stat);
  288. }
  289. kunmap_atomic(page_virt, KM_USER0);
  290. flush_dcache_page(page);
  291. if (rc) {
  292. ClearPageUptodate(page);
  293. printk(KERN_ERR "%s: Error reading xattr "
  294. "region; rc = [%d]\n", __FUNCTION__, rc);
  295. goto out;
  296. }
  297. SetPageUptodate(page);
  298. } else {
  299. /* This is an encrypted data extent */
  300. loff_t lower_offset =
  301. ((view_extent_num -
  302. crypt_stat->num_header_extents_at_front)
  303. * crypt_stat->extent_size);
  304. rc = ecryptfs_read_lower_page_segment(
  305. page, (lower_offset >> PAGE_CACHE_SHIFT),
  306. (lower_offset & ~PAGE_CACHE_MASK),
  307. crypt_stat->extent_size, page->mapping->host);
  308. if (rc) {
  309. printk(KERN_ERR "%s: Error attempting to read "
  310. "extent at offset [%lld] in the lower "
  311. "file; rc = [%d]\n", __FUNCTION__,
  312. lower_offset, rc);
  313. goto out;
  314. }
  315. }
  316. extent_num_in_page++;
  317. }
  318. out:
  319. return rc;
  320. }
  321. /**
  322. * ecryptfs_readpage
  323. * @file: An eCryptfs file
  324. * @page: Page from eCryptfs inode mapping into which to stick the read data
  325. *
  326. * Read in a page, decrypting if necessary.
  327. *
  328. * Returns zero on success; non-zero on error.
  329. */
  330. static int ecryptfs_readpage(struct file *file, struct page *page)
  331. {
  332. struct ecryptfs_crypt_stat *crypt_stat =
  333. &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
  334. int rc = 0;
  335. if (!crypt_stat
  336. || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  337. || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
  338. ecryptfs_printk(KERN_DEBUG,
  339. "Passing through unencrypted page\n");
  340. rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
  341. PAGE_CACHE_SIZE,
  342. page->mapping->host);
  343. } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
  344. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  345. rc = ecryptfs_copy_up_encrypted_with_header(page,
  346. crypt_stat);
  347. if (rc) {
  348. printk(KERN_ERR "%s: Error attempting to copy "
  349. "the encrypted content from the lower "
  350. "file whilst inserting the metadata "
  351. "from the xattr into the header; rc = "
  352. "[%d]\n", __FUNCTION__, rc);
  353. goto out;
  354. }
  355. } else {
  356. rc = ecryptfs_read_lower_page_segment(
  357. page, page->index, 0, PAGE_CACHE_SIZE,
  358. page->mapping->host);
  359. if (rc) {
  360. printk(KERN_ERR "Error reading page; rc = "
  361. "[%d]\n", rc);
  362. goto out;
  363. }
  364. }
  365. } else {
  366. rc = ecryptfs_decrypt_page(page);
  367. if (rc) {
  368. ecryptfs_printk(KERN_ERR, "Error decrypting page; "
  369. "rc = [%d]\n", rc);
  370. goto out;
  371. }
  372. }
  373. out:
  374. ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
  375. page->index);
  376. unlock_page(page);
  377. return rc;
  378. }
  379. /**
  380. * Called with lower inode mutex held.
  381. */
  382. static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
  383. {
  384. struct inode *inode = page->mapping->host;
  385. int end_byte_in_page;
  386. if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
  387. goto out;
  388. end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
  389. if (to > end_byte_in_page)
  390. end_byte_in_page = to;
  391. zero_user_page(page, end_byte_in_page,
  392. PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0);
  393. out:
  394. return 0;
  395. }
  396. /**
  397. * eCryptfs does not currently support holes. When writing after a
  398. * seek past the end of the file, eCryptfs fills in 0's through to the
  399. * current location. The code to fill in the 0's to all the
  400. * intermediate pages calls ecryptfs_prepare_write_no_truncate().
  401. */
  402. static int
  403. ecryptfs_prepare_write_no_truncate(struct file *file, struct page *page,
  404. unsigned from, unsigned to)
  405. {
  406. int rc = 0;
  407. if (from == 0 && to == PAGE_CACHE_SIZE)
  408. goto out; /* If we are writing a full page, it will be
  409. up to date. */
  410. if (!PageUptodate(page))
  411. rc = ecryptfs_do_readpage(file, page, page->index);
  412. out:
  413. return rc;
  414. }
  415. static int ecryptfs_prepare_write(struct file *file, struct page *page,
  416. unsigned from, unsigned to)
  417. {
  418. int rc = 0;
  419. if (from == 0 && to == PAGE_CACHE_SIZE)
  420. goto out; /* If we are writing a full page, it will be
  421. up to date. */
  422. if (!PageUptodate(page))
  423. rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
  424. PAGE_CACHE_SIZE,
  425. page->mapping->host);
  426. if (page->index != 0) {
  427. loff_t end_of_prev_pg_pos =
  428. (((loff_t)page->index << PAGE_CACHE_SHIFT) - 1);
  429. if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) {
  430. rc = ecryptfs_truncate(file->f_path.dentry,
  431. end_of_prev_pg_pos);
  432. if (rc) {
  433. printk(KERN_ERR "Error on attempt to "
  434. "truncate to (higher) offset [%lld];"
  435. " rc = [%d]\n", end_of_prev_pg_pos, rc);
  436. goto out;
  437. }
  438. }
  439. if (end_of_prev_pg_pos + 1 > i_size_read(page->mapping->host))
  440. zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
  441. }
  442. out:
  443. return rc;
  444. }
  445. int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
  446. struct inode *lower_inode,
  447. struct writeback_control *wbc)
  448. {
  449. int rc = 0;
  450. rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
  451. if (rc) {
  452. ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
  453. "rc = [%d]\n", rc);
  454. goto out;
  455. }
  456. lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
  457. page_cache_release(lower_page);
  458. out:
  459. return rc;
  460. }
  461. static void ecryptfs_release_lower_page(struct page *lower_page)
  462. {
  463. unlock_page(lower_page);
  464. page_cache_release(lower_page);
  465. }
  466. /**
  467. * ecryptfs_write_inode_size_to_header
  468. *
  469. * Writes the lower file size to the first 8 bytes of the header.
  470. *
  471. * Returns zero on success; non-zero on error.
  472. */
  473. static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
  474. {
  475. u64 file_size;
  476. char *file_size_virt;
  477. int rc;
  478. file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
  479. if (!file_size_virt) {
  480. rc = -ENOMEM;
  481. goto out;
  482. }
  483. file_size = (u64)i_size_read(ecryptfs_inode);
  484. file_size = cpu_to_be64(file_size);
  485. memcpy(file_size_virt, &file_size, sizeof(u64));
  486. rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
  487. sizeof(u64));
  488. kfree(file_size_virt);
  489. if (rc)
  490. printk(KERN_ERR "%s: Error writing file size to header; "
  491. "rc = [%d]\n", __FUNCTION__, rc);
  492. out:
  493. return rc;
  494. }
  495. struct kmem_cache *ecryptfs_xattr_cache;
  496. static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
  497. {
  498. ssize_t size;
  499. void *xattr_virt;
  500. struct dentry *lower_dentry =
  501. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  502. struct inode *lower_inode = lower_dentry->d_inode;
  503. u64 file_size;
  504. int rc;
  505. if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
  506. printk(KERN_WARNING
  507. "No support for setting xattr in lower filesystem\n");
  508. rc = -ENOSYS;
  509. goto out;
  510. }
  511. xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
  512. if (!xattr_virt) {
  513. printk(KERN_ERR "Out of memory whilst attempting to write "
  514. "inode size to xattr\n");
  515. rc = -ENOMEM;
  516. goto out;
  517. }
  518. mutex_lock(&lower_inode->i_mutex);
  519. size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  520. xattr_virt, PAGE_CACHE_SIZE);
  521. if (size < 0)
  522. size = 8;
  523. file_size = (u64)i_size_read(ecryptfs_inode);
  524. file_size = cpu_to_be64(file_size);
  525. memcpy(xattr_virt, &file_size, sizeof(u64));
  526. rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  527. xattr_virt, size, 0);
  528. mutex_unlock(&lower_inode->i_mutex);
  529. if (rc)
  530. printk(KERN_ERR "Error whilst attempting to write inode size "
  531. "to lower file xattr; rc = [%d]\n", rc);
  532. kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
  533. out:
  534. return rc;
  535. }
  536. int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
  537. {
  538. struct ecryptfs_crypt_stat *crypt_stat;
  539. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  540. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  541. return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
  542. else
  543. return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
  544. }
  545. int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
  546. struct file *lower_file,
  547. unsigned long lower_page_index, int byte_offset,
  548. int region_bytes)
  549. {
  550. int rc = 0;
  551. *lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index);
  552. if (!(*lower_page)) {
  553. rc = -EINVAL;
  554. ecryptfs_printk(KERN_ERR, "Error attempting to grab "
  555. "lower page with index [0x%.16x]\n",
  556. lower_page_index);
  557. goto out;
  558. }
  559. rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
  560. (*lower_page),
  561. byte_offset,
  562. region_bytes);
  563. if (rc) {
  564. ecryptfs_printk(KERN_ERR, "prepare_write for "
  565. "lower_page_index = [0x%.16x] failed; rc = "
  566. "[%d]\n", lower_page_index, rc);
  567. ecryptfs_release_lower_page(*lower_page);
  568. (*lower_page) = NULL;
  569. }
  570. out:
  571. return rc;
  572. }
  573. /**
  574. * ecryptfs_commit_lower_page
  575. *
  576. * Returns zero on success; non-zero on error
  577. */
  578. int
  579. ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
  580. struct file *lower_file, int byte_offset,
  581. int region_size)
  582. {
  583. int rc = 0;
  584. rc = lower_inode->i_mapping->a_ops->commit_write(
  585. lower_file, lower_page, byte_offset, region_size);
  586. if (rc < 0) {
  587. ecryptfs_printk(KERN_ERR,
  588. "Error committing write; rc = [%d]\n", rc);
  589. } else
  590. rc = 0;
  591. ecryptfs_release_lower_page(lower_page);
  592. return rc;
  593. }
  594. /**
  595. * ecryptfs_copy_page_to_lower
  596. *
  597. * Used for plaintext pass-through; no page index interpolation
  598. * required.
  599. */
  600. int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
  601. struct file *lower_file)
  602. {
  603. int rc = 0;
  604. struct page *lower_page;
  605. rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
  606. page->index, 0, PAGE_CACHE_SIZE);
  607. if (rc) {
  608. ecryptfs_printk(KERN_ERR, "Error attempting to get page "
  609. "at index [0x%.16x]\n", page->index);
  610. goto out;
  611. }
  612. /* TODO: aops */
  613. memcpy((char *)page_address(lower_page), page_address(page),
  614. PAGE_CACHE_SIZE);
  615. rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
  616. 0, PAGE_CACHE_SIZE);
  617. if (rc)
  618. ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
  619. "at index [0x%.16x]\n", page->index);
  620. out:
  621. return rc;
  622. }
  623. /**
  624. * ecryptfs_commit_write
  625. * @file: The eCryptfs file object
  626. * @page: The eCryptfs page
  627. * @from: Ignored (we rotate the page IV on each write)
  628. * @to: Ignored
  629. *
  630. * This is where we encrypt the data and pass the encrypted data to
  631. * the lower filesystem. In OpenPGP-compatible mode, we operate on
  632. * entire underlying packets.
  633. */
  634. static int ecryptfs_commit_write(struct file *file, struct page *page,
  635. unsigned from, unsigned to)
  636. {
  637. loff_t pos;
  638. struct inode *ecryptfs_inode = page->mapping->host;
  639. struct ecryptfs_crypt_stat *crypt_stat =
  640. &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
  641. int rc;
  642. if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
  643. ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
  644. "crypt_stat at memory location [%p]\n", crypt_stat);
  645. crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
  646. } else
  647. ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
  648. ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
  649. "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
  650. to);
  651. /* Fills in zeros if 'to' goes beyond inode size */
  652. rc = fill_zeros_to_end_of_page(page, to);
  653. if (rc) {
  654. ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
  655. "zeros in page with index = [0x%.16x]\n",
  656. page->index);
  657. goto out;
  658. }
  659. rc = ecryptfs_encrypt_page(page);
  660. if (rc) {
  661. ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
  662. "index [0x%.16x])\n", page->index);
  663. goto out;
  664. }
  665. pos = (page->index << PAGE_CACHE_SHIFT) + to;
  666. if (pos > i_size_read(ecryptfs_inode)) {
  667. i_size_write(ecryptfs_inode, pos);
  668. ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
  669. "[0x%.16x]\n", i_size_read(ecryptfs_inode));
  670. }
  671. rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
  672. if (rc)
  673. printk(KERN_ERR "Error writing inode size to metadata; "
  674. "rc = [%d]\n", rc);
  675. out:
  676. return rc;
  677. }
  678. /**
  679. * ecryptfs_write_zeros
  680. * @file: The ecryptfs file
  681. * @index: The index in which we are writing
  682. * @start: The position after the last block of data
  683. * @num_zeros: The number of zeros to write
  684. *
  685. * Write a specified number of zero's to a page.
  686. *
  687. * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
  688. */
  689. int
  690. ecryptfs_write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
  691. {
  692. int rc = 0;
  693. struct page *tmp_page;
  694. tmp_page = ecryptfs_get1page(file, index);
  695. if (IS_ERR(tmp_page)) {
  696. ecryptfs_printk(KERN_ERR, "Error getting page at index "
  697. "[0x%.16x]\n", index);
  698. rc = PTR_ERR(tmp_page);
  699. goto out;
  700. }
  701. rc = ecryptfs_prepare_write_no_truncate(file, tmp_page, start,
  702. (start + num_zeros));
  703. if (rc) {
  704. ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
  705. "to page at index [0x%.16x]\n",
  706. index);
  707. page_cache_release(tmp_page);
  708. goto out;
  709. }
  710. zero_user_page(tmp_page, start, num_zeros, KM_USER0);
  711. rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
  712. if (rc < 0) {
  713. ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
  714. "to remainder of page at index [0x%.16x]\n",
  715. index);
  716. page_cache_release(tmp_page);
  717. goto out;
  718. }
  719. rc = 0;
  720. page_cache_release(tmp_page);
  721. out:
  722. return rc;
  723. }
  724. static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
  725. {
  726. int rc = 0;
  727. struct inode *inode;
  728. struct inode *lower_inode;
  729. inode = (struct inode *)mapping->host;
  730. lower_inode = ecryptfs_inode_to_lower(inode);
  731. if (lower_inode->i_mapping->a_ops->bmap)
  732. rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
  733. block);
  734. return rc;
  735. }
  736. struct address_space_operations ecryptfs_aops = {
  737. .writepage = ecryptfs_writepage,
  738. .readpage = ecryptfs_readpage,
  739. .prepare_write = ecryptfs_prepare_write,
  740. .commit_write = ecryptfs_commit_write,
  741. .bmap = ecryptfs_bmap,
  742. };