mmap.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 = ((((loff_t)page->index)
  277. * num_extents_per_page)
  278. + extent_num_in_page);
  279. if (view_extent_num < crypt_stat->num_header_extents_at_front) {
  280. /* This is a header extent */
  281. char *page_virt;
  282. page_virt = kmap_atomic(page, KM_USER0);
  283. memset(page_virt, 0, PAGE_CACHE_SIZE);
  284. /* TODO: Support more than one header extent */
  285. if (view_extent_num == 0) {
  286. rc = ecryptfs_read_xattr_region(
  287. page_virt, page->mapping->host);
  288. set_header_info(page_virt, crypt_stat);
  289. }
  290. kunmap_atomic(page_virt, KM_USER0);
  291. flush_dcache_page(page);
  292. if (rc) {
  293. ClearPageUptodate(page);
  294. printk(KERN_ERR "%s: Error reading xattr "
  295. "region; rc = [%d]\n", __FUNCTION__, rc);
  296. goto out;
  297. }
  298. SetPageUptodate(page);
  299. } else {
  300. /* This is an encrypted data extent */
  301. loff_t lower_offset =
  302. ((view_extent_num -
  303. crypt_stat->num_header_extents_at_front)
  304. * crypt_stat->extent_size);
  305. rc = ecryptfs_read_lower_page_segment(
  306. page, (lower_offset >> PAGE_CACHE_SHIFT),
  307. (lower_offset & ~PAGE_CACHE_MASK),
  308. crypt_stat->extent_size, page->mapping->host);
  309. if (rc) {
  310. printk(KERN_ERR "%s: Error attempting to read "
  311. "extent at offset [%lld] in the lower "
  312. "file; rc = [%d]\n", __FUNCTION__,
  313. lower_offset, rc);
  314. goto out;
  315. }
  316. }
  317. extent_num_in_page++;
  318. }
  319. out:
  320. return rc;
  321. }
  322. /**
  323. * ecryptfs_readpage
  324. * @file: An eCryptfs file
  325. * @page: Page from eCryptfs inode mapping into which to stick the read data
  326. *
  327. * Read in a page, decrypting if necessary.
  328. *
  329. * Returns zero on success; non-zero on error.
  330. */
  331. static int ecryptfs_readpage(struct file *file, struct page *page)
  332. {
  333. struct ecryptfs_crypt_stat *crypt_stat =
  334. &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
  335. int rc = 0;
  336. if (!crypt_stat
  337. || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  338. || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
  339. ecryptfs_printk(KERN_DEBUG,
  340. "Passing through unencrypted page\n");
  341. rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
  342. PAGE_CACHE_SIZE,
  343. page->mapping->host);
  344. } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
  345. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  346. rc = ecryptfs_copy_up_encrypted_with_header(page,
  347. crypt_stat);
  348. if (rc) {
  349. printk(KERN_ERR "%s: Error attempting to copy "
  350. "the encrypted content from the lower "
  351. "file whilst inserting the metadata "
  352. "from the xattr into the header; rc = "
  353. "[%d]\n", __FUNCTION__, rc);
  354. goto out;
  355. }
  356. } else {
  357. rc = ecryptfs_read_lower_page_segment(
  358. page, page->index, 0, PAGE_CACHE_SIZE,
  359. page->mapping->host);
  360. if (rc) {
  361. printk(KERN_ERR "Error reading page; rc = "
  362. "[%d]\n", rc);
  363. goto out;
  364. }
  365. }
  366. } else {
  367. rc = ecryptfs_decrypt_page(page);
  368. if (rc) {
  369. ecryptfs_printk(KERN_ERR, "Error decrypting page; "
  370. "rc = [%d]\n", rc);
  371. goto out;
  372. }
  373. }
  374. out:
  375. ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
  376. page->index);
  377. unlock_page(page);
  378. return rc;
  379. }
  380. /**
  381. * Called with lower inode mutex held.
  382. */
  383. static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
  384. {
  385. struct inode *inode = page->mapping->host;
  386. int end_byte_in_page;
  387. if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
  388. goto out;
  389. end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
  390. if (to > end_byte_in_page)
  391. end_byte_in_page = to;
  392. zero_user_page(page, end_byte_in_page,
  393. PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0);
  394. out:
  395. return 0;
  396. }
  397. /**
  398. * eCryptfs does not currently support holes. When writing after a
  399. * seek past the end of the file, eCryptfs fills in 0's through to the
  400. * current location. The code to fill in the 0's to all the
  401. * intermediate pages calls ecryptfs_prepare_write_no_truncate().
  402. */
  403. static int
  404. ecryptfs_prepare_write_no_truncate(struct file *file, struct page *page,
  405. unsigned from, unsigned to)
  406. {
  407. int rc = 0;
  408. if (from == 0 && to == PAGE_CACHE_SIZE)
  409. goto out; /* If we are writing a full page, it will be
  410. up to date. */
  411. if (!PageUptodate(page))
  412. rc = ecryptfs_do_readpage(file, page, page->index);
  413. out:
  414. return rc;
  415. }
  416. static int ecryptfs_prepare_write(struct file *file, struct page *page,
  417. unsigned from, unsigned to)
  418. {
  419. int rc = 0;
  420. if (from == 0 && to == PAGE_CACHE_SIZE)
  421. goto out; /* If we are writing a full page, it will be
  422. up to date. */
  423. if (!PageUptodate(page))
  424. rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
  425. PAGE_CACHE_SIZE,
  426. page->mapping->host);
  427. if (page->index != 0) {
  428. loff_t end_of_prev_pg_pos =
  429. (((loff_t)page->index << PAGE_CACHE_SHIFT) - 1);
  430. if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) {
  431. rc = ecryptfs_truncate(file->f_path.dentry,
  432. end_of_prev_pg_pos);
  433. if (rc) {
  434. printk(KERN_ERR "Error on attempt to "
  435. "truncate to (higher) offset [%lld];"
  436. " rc = [%d]\n", end_of_prev_pg_pos, rc);
  437. goto out;
  438. }
  439. }
  440. if (end_of_prev_pg_pos + 1 > i_size_read(page->mapping->host))
  441. zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
  442. }
  443. out:
  444. return rc;
  445. }
  446. int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
  447. struct inode *lower_inode,
  448. struct writeback_control *wbc)
  449. {
  450. int rc = 0;
  451. rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
  452. if (rc) {
  453. ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
  454. "rc = [%d]\n", rc);
  455. goto out;
  456. }
  457. lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
  458. page_cache_release(lower_page);
  459. out:
  460. return rc;
  461. }
  462. static void ecryptfs_release_lower_page(struct page *lower_page)
  463. {
  464. unlock_page(lower_page);
  465. page_cache_release(lower_page);
  466. }
  467. /**
  468. * ecryptfs_write_inode_size_to_header
  469. *
  470. * Writes the lower file size to the first 8 bytes of the header.
  471. *
  472. * Returns zero on success; non-zero on error.
  473. */
  474. static int ecryptfs_write_inode_size_to_header(struct inode *ecryptfs_inode)
  475. {
  476. u64 file_size;
  477. char *file_size_virt;
  478. int rc;
  479. file_size_virt = kmalloc(sizeof(u64), GFP_KERNEL);
  480. if (!file_size_virt) {
  481. rc = -ENOMEM;
  482. goto out;
  483. }
  484. file_size = (u64)i_size_read(ecryptfs_inode);
  485. file_size = cpu_to_be64(file_size);
  486. memcpy(file_size_virt, &file_size, sizeof(u64));
  487. rc = ecryptfs_write_lower(ecryptfs_inode, file_size_virt, 0,
  488. sizeof(u64));
  489. kfree(file_size_virt);
  490. if (rc)
  491. printk(KERN_ERR "%s: Error writing file size to header; "
  492. "rc = [%d]\n", __FUNCTION__, rc);
  493. out:
  494. return rc;
  495. }
  496. struct kmem_cache *ecryptfs_xattr_cache;
  497. static int ecryptfs_write_inode_size_to_xattr(struct inode *ecryptfs_inode)
  498. {
  499. ssize_t size;
  500. void *xattr_virt;
  501. struct dentry *lower_dentry =
  502. ecryptfs_inode_to_private(ecryptfs_inode)->lower_file->f_dentry;
  503. struct inode *lower_inode = lower_dentry->d_inode;
  504. u64 file_size;
  505. int rc;
  506. if (!lower_inode->i_op->getxattr || !lower_inode->i_op->setxattr) {
  507. printk(KERN_WARNING
  508. "No support for setting xattr in lower filesystem\n");
  509. rc = -ENOSYS;
  510. goto out;
  511. }
  512. xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
  513. if (!xattr_virt) {
  514. printk(KERN_ERR "Out of memory whilst attempting to write "
  515. "inode size to xattr\n");
  516. rc = -ENOMEM;
  517. goto out;
  518. }
  519. mutex_lock(&lower_inode->i_mutex);
  520. size = lower_inode->i_op->getxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  521. xattr_virt, PAGE_CACHE_SIZE);
  522. if (size < 0)
  523. size = 8;
  524. file_size = (u64)i_size_read(ecryptfs_inode);
  525. file_size = cpu_to_be64(file_size);
  526. memcpy(xattr_virt, &file_size, sizeof(u64));
  527. rc = lower_inode->i_op->setxattr(lower_dentry, ECRYPTFS_XATTR_NAME,
  528. xattr_virt, size, 0);
  529. mutex_unlock(&lower_inode->i_mutex);
  530. if (rc)
  531. printk(KERN_ERR "Error whilst attempting to write inode size "
  532. "to lower file xattr; rc = [%d]\n", rc);
  533. kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
  534. out:
  535. return rc;
  536. }
  537. int ecryptfs_write_inode_size_to_metadata(struct inode *ecryptfs_inode)
  538. {
  539. struct ecryptfs_crypt_stat *crypt_stat;
  540. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  541. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  542. return ecryptfs_write_inode_size_to_xattr(ecryptfs_inode);
  543. else
  544. return ecryptfs_write_inode_size_to_header(ecryptfs_inode);
  545. }
  546. int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
  547. struct file *lower_file,
  548. unsigned long lower_page_index, int byte_offset,
  549. int region_bytes)
  550. {
  551. int rc = 0;
  552. *lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index);
  553. if (!(*lower_page)) {
  554. rc = -EINVAL;
  555. ecryptfs_printk(KERN_ERR, "Error attempting to grab "
  556. "lower page with index [0x%.16x]\n",
  557. lower_page_index);
  558. goto out;
  559. }
  560. rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
  561. (*lower_page),
  562. byte_offset,
  563. region_bytes);
  564. if (rc) {
  565. ecryptfs_printk(KERN_ERR, "prepare_write for "
  566. "lower_page_index = [0x%.16x] failed; rc = "
  567. "[%d]\n", lower_page_index, rc);
  568. ecryptfs_release_lower_page(*lower_page);
  569. (*lower_page) = NULL;
  570. }
  571. out:
  572. return rc;
  573. }
  574. /**
  575. * ecryptfs_commit_lower_page
  576. *
  577. * Returns zero on success; non-zero on error
  578. */
  579. int
  580. ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
  581. struct file *lower_file, int byte_offset,
  582. int region_size)
  583. {
  584. int rc = 0;
  585. rc = lower_inode->i_mapping->a_ops->commit_write(
  586. lower_file, lower_page, byte_offset, region_size);
  587. if (rc < 0) {
  588. ecryptfs_printk(KERN_ERR,
  589. "Error committing write; rc = [%d]\n", rc);
  590. } else
  591. rc = 0;
  592. ecryptfs_release_lower_page(lower_page);
  593. return rc;
  594. }
  595. /**
  596. * ecryptfs_copy_page_to_lower
  597. *
  598. * Used for plaintext pass-through; no page index interpolation
  599. * required.
  600. */
  601. int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
  602. struct file *lower_file)
  603. {
  604. int rc = 0;
  605. struct page *lower_page;
  606. rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
  607. page->index, 0, PAGE_CACHE_SIZE);
  608. if (rc) {
  609. ecryptfs_printk(KERN_ERR, "Error attempting to get page "
  610. "at index [0x%.16x]\n", page->index);
  611. goto out;
  612. }
  613. /* TODO: aops */
  614. memcpy((char *)page_address(lower_page), page_address(page),
  615. PAGE_CACHE_SIZE);
  616. rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
  617. 0, PAGE_CACHE_SIZE);
  618. if (rc)
  619. ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
  620. "at index [0x%.16x]\n", page->index);
  621. out:
  622. return rc;
  623. }
  624. /**
  625. * ecryptfs_commit_write
  626. * @file: The eCryptfs file object
  627. * @page: The eCryptfs page
  628. * @from: Ignored (we rotate the page IV on each write)
  629. * @to: Ignored
  630. *
  631. * This is where we encrypt the data and pass the encrypted data to
  632. * the lower filesystem. In OpenPGP-compatible mode, we operate on
  633. * entire underlying packets.
  634. */
  635. static int ecryptfs_commit_write(struct file *file, struct page *page,
  636. unsigned from, unsigned to)
  637. {
  638. loff_t pos;
  639. struct inode *ecryptfs_inode = page->mapping->host;
  640. struct ecryptfs_crypt_stat *crypt_stat =
  641. &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)->crypt_stat;
  642. int rc;
  643. if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
  644. ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
  645. "crypt_stat at memory location [%p]\n", crypt_stat);
  646. crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
  647. } else
  648. ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
  649. ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
  650. "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
  651. to);
  652. /* Fills in zeros if 'to' goes beyond inode size */
  653. rc = fill_zeros_to_end_of_page(page, to);
  654. if (rc) {
  655. ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
  656. "zeros in page with index = [0x%.16x]\n",
  657. page->index);
  658. goto out;
  659. }
  660. rc = ecryptfs_encrypt_page(page);
  661. if (rc) {
  662. ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
  663. "index [0x%.16x])\n", page->index);
  664. goto out;
  665. }
  666. pos = (((loff_t)page->index) << PAGE_CACHE_SHIFT) + to;
  667. if (pos > i_size_read(ecryptfs_inode)) {
  668. i_size_write(ecryptfs_inode, pos);
  669. ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
  670. "[0x%.16x]\n", i_size_read(ecryptfs_inode));
  671. }
  672. rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
  673. if (rc)
  674. printk(KERN_ERR "Error writing inode size to metadata; "
  675. "rc = [%d]\n", rc);
  676. out:
  677. return rc;
  678. }
  679. /**
  680. * ecryptfs_write_zeros
  681. * @file: The ecryptfs file
  682. * @index: The index in which we are writing
  683. * @start: The position after the last block of data
  684. * @num_zeros: The number of zeros to write
  685. *
  686. * Write a specified number of zero's to a page.
  687. *
  688. * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
  689. */
  690. int
  691. ecryptfs_write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
  692. {
  693. int rc = 0;
  694. struct page *tmp_page;
  695. tmp_page = ecryptfs_get1page(file, index);
  696. if (IS_ERR(tmp_page)) {
  697. ecryptfs_printk(KERN_ERR, "Error getting page at index "
  698. "[0x%.16x]\n", index);
  699. rc = PTR_ERR(tmp_page);
  700. goto out;
  701. }
  702. rc = ecryptfs_prepare_write_no_truncate(file, tmp_page, start,
  703. (start + num_zeros));
  704. if (rc) {
  705. ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
  706. "to page at index [0x%.16x]\n",
  707. index);
  708. page_cache_release(tmp_page);
  709. goto out;
  710. }
  711. zero_user_page(tmp_page, start, num_zeros, KM_USER0);
  712. rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
  713. if (rc < 0) {
  714. ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
  715. "to remainder of page at index [0x%.16x]\n",
  716. index);
  717. page_cache_release(tmp_page);
  718. goto out;
  719. }
  720. rc = 0;
  721. page_cache_release(tmp_page);
  722. out:
  723. return rc;
  724. }
  725. static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
  726. {
  727. int rc = 0;
  728. struct inode *inode;
  729. struct inode *lower_inode;
  730. inode = (struct inode *)mapping->host;
  731. lower_inode = ecryptfs_inode_to_lower(inode);
  732. if (lower_inode->i_mapping->a_ops->bmap)
  733. rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
  734. block);
  735. return rc;
  736. }
  737. struct address_space_operations ecryptfs_aops = {
  738. .writepage = ecryptfs_writepage,
  739. .readpage = ecryptfs_readpage,
  740. .prepare_write = ecryptfs_prepare_write,
  741. .commit_write = ecryptfs_commit_write,
  742. .bmap = ecryptfs_bmap,
  743. };