mmap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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-2006 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. static struct page *ecryptfs_get1page(struct file *file, int index)
  45. {
  46. struct page *page;
  47. struct dentry *dentry;
  48. struct inode *inode;
  49. struct address_space *mapping;
  50. dentry = file->f_dentry;
  51. inode = dentry->d_inode;
  52. mapping = inode->i_mapping;
  53. page = read_cache_page(mapping, index,
  54. (filler_t *)mapping->a_ops->readpage,
  55. (void *)file);
  56. if (IS_ERR(page))
  57. goto out;
  58. wait_on_page_locked(page);
  59. out:
  60. return page;
  61. }
  62. static
  63. int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros);
  64. /**
  65. * ecryptfs_fill_zeros
  66. * @file: The ecryptfs file
  67. * @new_length: The new length of the data in the underlying file;
  68. * everything between the prior end of the file and the
  69. * new end of the file will be filled with zero's.
  70. * new_length must be greater than current length
  71. *
  72. * Function for handling lseek-ing past the end of the file.
  73. *
  74. * This function does not support shrinking, only growing a file.
  75. *
  76. * Returns zero on success; non-zero otherwise.
  77. */
  78. int ecryptfs_fill_zeros(struct file *file, loff_t new_length)
  79. {
  80. int rc = 0;
  81. struct dentry *dentry = file->f_dentry;
  82. struct inode *inode = dentry->d_inode;
  83. pgoff_t old_end_page_index = 0;
  84. pgoff_t index = old_end_page_index;
  85. int old_end_pos_in_page = -1;
  86. pgoff_t new_end_page_index;
  87. int new_end_pos_in_page;
  88. loff_t cur_length = i_size_read(inode);
  89. if (cur_length != 0) {
  90. index = old_end_page_index =
  91. ((cur_length - 1) >> PAGE_CACHE_SHIFT);
  92. old_end_pos_in_page = ((cur_length - 1) & ~PAGE_CACHE_MASK);
  93. }
  94. new_end_page_index = ((new_length - 1) >> PAGE_CACHE_SHIFT);
  95. new_end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK);
  96. ecryptfs_printk(KERN_DEBUG, "old_end_page_index = [0x%.16x]; "
  97. "old_end_pos_in_page = [%d]; "
  98. "new_end_page_index = [0x%.16x]; "
  99. "new_end_pos_in_page = [%d]\n",
  100. old_end_page_index, old_end_pos_in_page,
  101. new_end_page_index, new_end_pos_in_page);
  102. if (old_end_page_index == new_end_page_index) {
  103. /* Start and end are in the same page; we just need to
  104. * set a portion of the existing page to zero's */
  105. rc = write_zeros(file, index, (old_end_pos_in_page + 1),
  106. (new_end_pos_in_page - old_end_pos_in_page));
  107. if (rc)
  108. ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
  109. "index=[0x%.16x], "
  110. "old_end_pos_in_page=[d], "
  111. "(PAGE_CACHE_SIZE - new_end_pos_in_page"
  112. "=[%d]"
  113. ")=[d]) returned [%d]\n", file, index,
  114. old_end_pos_in_page,
  115. new_end_pos_in_page,
  116. (PAGE_CACHE_SIZE - new_end_pos_in_page),
  117. rc);
  118. goto out;
  119. }
  120. /* Fill the remainder of the previous last page with zeros */
  121. rc = write_zeros(file, index, (old_end_pos_in_page + 1),
  122. ((PAGE_CACHE_SIZE - 1) - old_end_pos_in_page));
  123. if (rc) {
  124. ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
  125. "index=[0x%.16x], old_end_pos_in_page=[d], "
  126. "(PAGE_CACHE_SIZE - old_end_pos_in_page)=[d]) "
  127. "returned [%d]\n", file, index,
  128. old_end_pos_in_page,
  129. (PAGE_CACHE_SIZE - old_end_pos_in_page), rc);
  130. goto out;
  131. }
  132. index++;
  133. while (index < new_end_page_index) {
  134. /* Fill all intermediate pages with zeros */
  135. rc = write_zeros(file, index, 0, PAGE_CACHE_SIZE);
  136. if (rc) {
  137. ecryptfs_printk(KERN_ERR, "write_zeros(file=[%p], "
  138. "index=[0x%.16x], "
  139. "old_end_pos_in_page=[d], "
  140. "(PAGE_CACHE_SIZE - new_end_pos_in_page"
  141. "=[%d]"
  142. ")=[d]) returned [%d]\n", file, index,
  143. old_end_pos_in_page,
  144. new_end_pos_in_page,
  145. (PAGE_CACHE_SIZE - new_end_pos_in_page),
  146. rc);
  147. goto out;
  148. }
  149. index++;
  150. }
  151. /* Fill the portion at the beginning of the last new page with
  152. * zero's */
  153. rc = write_zeros(file, index, 0, (new_end_pos_in_page + 1));
  154. if (rc) {
  155. ecryptfs_printk(KERN_ERR, "write_zeros(file="
  156. "[%p], index=[0x%.16x], 0, "
  157. "new_end_pos_in_page=[%d]"
  158. "returned [%d]\n", file, index,
  159. new_end_pos_in_page, rc);
  160. goto out;
  161. }
  162. out:
  163. return rc;
  164. }
  165. /**
  166. * ecryptfs_writepage
  167. * @page: Page that is locked before this call is made
  168. *
  169. * Returns zero on success; non-zero otherwise
  170. */
  171. static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
  172. {
  173. struct ecryptfs_page_crypt_context ctx;
  174. int rc;
  175. ctx.page = page;
  176. ctx.mode = ECRYPTFS_WRITEPAGE_MODE;
  177. ctx.param.wbc = wbc;
  178. rc = ecryptfs_encrypt_page(&ctx);
  179. if (rc) {
  180. ecryptfs_printk(KERN_WARNING, "Error encrypting "
  181. "page (upper index [0x%.16x])\n", page->index);
  182. ClearPageUptodate(page);
  183. goto out;
  184. }
  185. SetPageUptodate(page);
  186. unlock_page(page);
  187. out:
  188. return rc;
  189. }
  190. /**
  191. * Reads the data from the lower file file at index lower_page_index
  192. * and copies that data into page.
  193. *
  194. * @param page Page to fill
  195. * @param lower_page_index Index of the page in the lower file to get
  196. */
  197. int ecryptfs_do_readpage(struct file *file, struct page *page,
  198. pgoff_t lower_page_index)
  199. {
  200. int rc;
  201. struct dentry *dentry;
  202. struct file *lower_file;
  203. struct dentry *lower_dentry;
  204. struct inode *inode;
  205. struct inode *lower_inode;
  206. char *page_data;
  207. struct page *lower_page = NULL;
  208. char *lower_page_data;
  209. const struct address_space_operations *lower_a_ops;
  210. dentry = file->f_dentry;
  211. lower_file = ecryptfs_file_to_lower(file);
  212. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  213. inode = dentry->d_inode;
  214. lower_inode = ecryptfs_inode_to_lower(inode);
  215. lower_a_ops = lower_inode->i_mapping->a_ops;
  216. lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index,
  217. (filler_t *)lower_a_ops->readpage,
  218. (void *)lower_file);
  219. if (IS_ERR(lower_page)) {
  220. rc = PTR_ERR(lower_page);
  221. lower_page = NULL;
  222. ecryptfs_printk(KERN_ERR, "Error reading from page cache\n");
  223. goto out;
  224. }
  225. wait_on_page_locked(lower_page);
  226. page_data = (char *)kmap(page);
  227. if (!page_data) {
  228. rc = -ENOMEM;
  229. ecryptfs_printk(KERN_ERR, "Error mapping page\n");
  230. goto out;
  231. }
  232. lower_page_data = (char *)kmap(lower_page);
  233. if (!lower_page_data) {
  234. rc = -ENOMEM;
  235. ecryptfs_printk(KERN_ERR, "Error mapping page\n");
  236. kunmap(page);
  237. goto out;
  238. }
  239. memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
  240. kunmap(lower_page);
  241. kunmap(page);
  242. rc = 0;
  243. out:
  244. if (likely(lower_page))
  245. page_cache_release(lower_page);
  246. if (rc == 0)
  247. SetPageUptodate(page);
  248. else
  249. ClearPageUptodate(page);
  250. return rc;
  251. }
  252. /**
  253. * ecryptfs_readpage
  254. * @file: This is an ecryptfs file
  255. * @page: ecryptfs associated page to stick the read data into
  256. *
  257. * Read in a page, decrypting if necessary.
  258. *
  259. * Returns zero on success; non-zero on error.
  260. */
  261. static int ecryptfs_readpage(struct file *file, struct page *page)
  262. {
  263. int rc = 0;
  264. struct ecryptfs_crypt_stat *crypt_stat;
  265. BUG_ON(!(file && file->f_dentry && file->f_dentry->d_inode));
  266. crypt_stat =
  267. &ecryptfs_inode_to_private(file->f_dentry->d_inode)->crypt_stat;
  268. if (!crypt_stat
  269. || !ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED)
  270. || ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) {
  271. ecryptfs_printk(KERN_DEBUG,
  272. "Passing through unencrypted page\n");
  273. rc = ecryptfs_do_readpage(file, page, page->index);
  274. if (rc) {
  275. ecryptfs_printk(KERN_ERR, "Error reading page; rc = "
  276. "[%d]\n", rc);
  277. goto out;
  278. }
  279. } else {
  280. rc = ecryptfs_decrypt_page(file, page);
  281. if (rc) {
  282. ecryptfs_printk(KERN_ERR, "Error decrypting page; "
  283. "rc = [%d]\n", rc);
  284. goto out;
  285. }
  286. }
  287. SetPageUptodate(page);
  288. out:
  289. if (rc)
  290. ClearPageUptodate(page);
  291. ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
  292. page->index);
  293. unlock_page(page);
  294. return rc;
  295. }
  296. static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
  297. {
  298. struct inode *inode = page->mapping->host;
  299. int end_byte_in_page;
  300. int rc = 0;
  301. char *page_virt;
  302. if ((i_size_read(inode) / PAGE_CACHE_SIZE) == page->index) {
  303. end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
  304. if (to > end_byte_in_page)
  305. end_byte_in_page = to;
  306. page_virt = kmap(page);
  307. if (!page_virt) {
  308. rc = -ENOMEM;
  309. ecryptfs_printk(KERN_WARNING,
  310. "Could not map page\n");
  311. goto out;
  312. }
  313. memset((page_virt + end_byte_in_page), 0,
  314. (PAGE_CACHE_SIZE - end_byte_in_page));
  315. kunmap(page);
  316. }
  317. out:
  318. return rc;
  319. }
  320. static int ecryptfs_prepare_write(struct file *file, struct page *page,
  321. unsigned from, unsigned to)
  322. {
  323. int rc = 0;
  324. kmap(page);
  325. if (from == 0 && to == PAGE_CACHE_SIZE)
  326. goto out; /* If we are writing a full page, it will be
  327. up to date. */
  328. if (!PageUptodate(page))
  329. rc = ecryptfs_do_readpage(file, page, page->index);
  330. out:
  331. return rc;
  332. }
  333. int ecryptfs_grab_and_map_lower_page(struct page **lower_page,
  334. char **lower_virt,
  335. struct inode *lower_inode,
  336. unsigned long lower_page_index)
  337. {
  338. int rc = 0;
  339. (*lower_page) = grab_cache_page(lower_inode->i_mapping,
  340. lower_page_index);
  341. if (!(*lower_page)) {
  342. ecryptfs_printk(KERN_ERR, "grab_cache_page for "
  343. "lower_page_index = [0x%.16x] failed\n",
  344. lower_page_index);
  345. rc = -EINVAL;
  346. goto out;
  347. }
  348. if (lower_virt)
  349. (*lower_virt) = kmap((*lower_page));
  350. else
  351. kmap((*lower_page));
  352. out:
  353. return rc;
  354. }
  355. int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
  356. struct inode *lower_inode,
  357. struct writeback_control *wbc)
  358. {
  359. int rc = 0;
  360. rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
  361. if (rc) {
  362. ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
  363. "rc = [%d]\n", rc);
  364. goto out;
  365. }
  366. lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
  367. page_cache_release(lower_page);
  368. out:
  369. return rc;
  370. }
  371. static void ecryptfs_unmap_and_release_lower_page(struct page *lower_page)
  372. {
  373. kunmap(lower_page);
  374. ecryptfs_printk(KERN_DEBUG, "Unlocking lower page with index = "
  375. "[0x%.16x]\n", lower_page->index);
  376. unlock_page(lower_page);
  377. page_cache_release(lower_page);
  378. }
  379. /**
  380. * ecryptfs_write_inode_size_to_header
  381. *
  382. * Writes the lower file size to the first 8 bytes of the header.
  383. *
  384. * Returns zero on success; non-zero on error.
  385. */
  386. int
  387. ecryptfs_write_inode_size_to_header(struct file *lower_file,
  388. struct inode *lower_inode,
  389. struct inode *inode)
  390. {
  391. int rc = 0;
  392. struct page *header_page;
  393. char *header_virt;
  394. const struct address_space_operations *lower_a_ops;
  395. u64 file_size;
  396. rc = ecryptfs_grab_and_map_lower_page(&header_page, &header_virt,
  397. lower_inode, 0);
  398. if (rc) {
  399. ecryptfs_printk(KERN_ERR, "grab_cache_page for header page "
  400. "failed\n");
  401. goto out;
  402. }
  403. lower_a_ops = lower_inode->i_mapping->a_ops;
  404. rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8);
  405. file_size = (u64)i_size_read(inode);
  406. ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size);
  407. file_size = cpu_to_be64(file_size);
  408. memcpy(header_virt, &file_size, sizeof(u64));
  409. rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8);
  410. if (rc < 0)
  411. ecryptfs_printk(KERN_ERR, "Error commiting header page "
  412. "write\n");
  413. ecryptfs_unmap_and_release_lower_page(header_page);
  414. lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
  415. mark_inode_dirty_sync(inode);
  416. out:
  417. return rc;
  418. }
  419. int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
  420. struct file *lower_file,
  421. unsigned long lower_page_index, int byte_offset,
  422. int region_bytes)
  423. {
  424. int rc = 0;
  425. rc = ecryptfs_grab_and_map_lower_page(lower_page, NULL, lower_inode,
  426. lower_page_index);
  427. if (rc) {
  428. ecryptfs_printk(KERN_ERR, "Error attempting to grab and map "
  429. "lower page with index [0x%.16x]\n",
  430. lower_page_index);
  431. goto out;
  432. }
  433. rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
  434. (*lower_page),
  435. byte_offset,
  436. region_bytes);
  437. if (rc) {
  438. ecryptfs_printk(KERN_ERR, "prepare_write for "
  439. "lower_page_index = [0x%.16x] failed; rc = "
  440. "[%d]\n", lower_page_index, rc);
  441. }
  442. out:
  443. if (rc && (*lower_page)) {
  444. ecryptfs_unmap_and_release_lower_page(*lower_page);
  445. (*lower_page) = NULL;
  446. }
  447. return rc;
  448. }
  449. /**
  450. * ecryptfs_commit_lower_page
  451. *
  452. * Returns zero on success; non-zero on error
  453. */
  454. int
  455. ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
  456. struct file *lower_file, int byte_offset,
  457. int region_size)
  458. {
  459. int rc = 0;
  460. rc = lower_inode->i_mapping->a_ops->commit_write(
  461. lower_file, lower_page, byte_offset, region_size);
  462. if (rc < 0) {
  463. ecryptfs_printk(KERN_ERR,
  464. "Error committing write; rc = [%d]\n", rc);
  465. } else
  466. rc = 0;
  467. ecryptfs_unmap_and_release_lower_page(lower_page);
  468. return rc;
  469. }
  470. /**
  471. * ecryptfs_copy_page_to_lower
  472. *
  473. * Used for plaintext pass-through; no page index interpolation
  474. * required.
  475. */
  476. int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
  477. struct file *lower_file)
  478. {
  479. int rc = 0;
  480. struct page *lower_page;
  481. rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
  482. page->index, 0, PAGE_CACHE_SIZE);
  483. if (rc) {
  484. ecryptfs_printk(KERN_ERR, "Error attempting to get page "
  485. "at index [0x%.16x]\n", page->index);
  486. goto out;
  487. }
  488. /* TODO: aops */
  489. memcpy((char *)page_address(lower_page), page_address(page),
  490. PAGE_CACHE_SIZE);
  491. rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
  492. 0, PAGE_CACHE_SIZE);
  493. if (rc)
  494. ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
  495. "at index [0x%.16x]\n", page->index);
  496. out:
  497. return rc;
  498. }
  499. static int
  500. process_new_file(struct ecryptfs_crypt_stat *crypt_stat,
  501. struct file *file, struct inode *inode)
  502. {
  503. struct page *header_page;
  504. const struct address_space_operations *lower_a_ops;
  505. struct inode *lower_inode;
  506. struct file *lower_file;
  507. char *header_virt;
  508. int rc = 0;
  509. int current_header_page = 0;
  510. int header_pages;
  511. int more_header_data_to_be_written = 1;
  512. lower_inode = ecryptfs_inode_to_lower(inode);
  513. lower_file = ecryptfs_file_to_lower(file);
  514. lower_a_ops = lower_inode->i_mapping->a_ops;
  515. header_pages = ((crypt_stat->header_extent_size
  516. * crypt_stat->num_header_extents_at_front)
  517. / PAGE_CACHE_SIZE);
  518. BUG_ON(header_pages < 1);
  519. while (current_header_page < header_pages) {
  520. rc = ecryptfs_grab_and_map_lower_page(&header_page,
  521. &header_virt,
  522. lower_inode,
  523. current_header_page);
  524. if (rc) {
  525. ecryptfs_printk(KERN_ERR, "grab_cache_page for "
  526. "header page [%d] failed; rc = [%d]\n",
  527. current_header_page, rc);
  528. goto out;
  529. }
  530. rc = lower_a_ops->prepare_write(lower_file, header_page, 0,
  531. PAGE_CACHE_SIZE);
  532. if (rc) {
  533. ecryptfs_printk(KERN_ERR, "Error preparing to write "
  534. "header page out; rc = [%d]\n", rc);
  535. goto out;
  536. }
  537. memset(header_virt, 0, PAGE_CACHE_SIZE);
  538. if (more_header_data_to_be_written) {
  539. rc = ecryptfs_write_headers_virt(header_virt,
  540. crypt_stat,
  541. file->f_dentry);
  542. if (rc) {
  543. ecryptfs_printk(KERN_WARNING, "Error "
  544. "generating header; rc = "
  545. "[%d]\n", rc);
  546. rc = -EIO;
  547. memset(header_virt, 0, PAGE_CACHE_SIZE);
  548. ecryptfs_unmap_and_release_lower_page(
  549. header_page);
  550. goto out;
  551. }
  552. if (current_header_page == 0)
  553. memset(header_virt, 0, 8);
  554. more_header_data_to_be_written = 0;
  555. }
  556. rc = lower_a_ops->commit_write(lower_file, header_page, 0,
  557. PAGE_CACHE_SIZE);
  558. ecryptfs_unmap_and_release_lower_page(header_page);
  559. if (rc < 0) {
  560. ecryptfs_printk(KERN_ERR,
  561. "Error commiting header page write; "
  562. "rc = [%d]\n", rc);
  563. break;
  564. }
  565. current_header_page++;
  566. }
  567. if (rc >= 0) {
  568. rc = 0;
  569. ecryptfs_printk(KERN_DEBUG, "lower_inode->i_blocks = "
  570. "[0x%.16x]\n", lower_inode->i_blocks);
  571. i_size_write(inode, 0);
  572. lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
  573. mark_inode_dirty_sync(inode);
  574. }
  575. ecryptfs_printk(KERN_DEBUG, "Clearing ECRYPTFS_NEW_FILE flag in "
  576. "crypt_stat at memory location [%p]\n", crypt_stat);
  577. ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE);
  578. out:
  579. return rc;
  580. }
  581. /**
  582. * ecryptfs_commit_write
  583. * @file: The eCryptfs file object
  584. * @page: The eCryptfs page
  585. * @from: Ignored (we rotate the page IV on each write)
  586. * @to: Ignored
  587. *
  588. * This is where we encrypt the data and pass the encrypted data to
  589. * the lower filesystem. In OpenPGP-compatible mode, we operate on
  590. * entire underlying packets.
  591. */
  592. static int ecryptfs_commit_write(struct file *file, struct page *page,
  593. unsigned from, unsigned to)
  594. {
  595. struct ecryptfs_page_crypt_context ctx;
  596. loff_t pos;
  597. struct inode *inode;
  598. struct inode *lower_inode;
  599. struct file *lower_file;
  600. struct ecryptfs_crypt_stat *crypt_stat;
  601. int rc;
  602. inode = page->mapping->host;
  603. lower_inode = ecryptfs_inode_to_lower(inode);
  604. lower_file = ecryptfs_file_to_lower(file);
  605. mutex_lock(&lower_inode->i_mutex);
  606. crypt_stat =
  607. &ecryptfs_inode_to_private(file->f_dentry->d_inode)->crypt_stat;
  608. if (ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE)) {
  609. ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
  610. "crypt_stat at memory location [%p]\n", crypt_stat);
  611. rc = process_new_file(crypt_stat, file, inode);
  612. if (rc) {
  613. ecryptfs_printk(KERN_ERR, "Error processing new "
  614. "file; rc = [%d]\n", rc);
  615. goto out;
  616. }
  617. } else
  618. ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
  619. ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
  620. "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
  621. to);
  622. rc = fill_zeros_to_end_of_page(page, to);
  623. if (rc) {
  624. ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
  625. "zeros in page with index = [0x%.16x]\n",
  626. page->index);
  627. goto out;
  628. }
  629. ctx.page = page;
  630. ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
  631. ctx.param.lower_file = lower_file;
  632. rc = ecryptfs_encrypt_page(&ctx);
  633. if (rc) {
  634. ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
  635. "index [0x%.16x])\n", page->index);
  636. goto out;
  637. }
  638. rc = 0;
  639. inode->i_blocks = lower_inode->i_blocks;
  640. pos = (page->index << PAGE_CACHE_SHIFT) + to;
  641. if (pos > i_size_read(inode)) {
  642. i_size_write(inode, pos);
  643. ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
  644. "[0x%.16x]\n", i_size_read(inode));
  645. }
  646. ecryptfs_write_inode_size_to_header(lower_file, lower_inode, inode);
  647. lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
  648. mark_inode_dirty_sync(inode);
  649. out:
  650. kunmap(page); /* mapped in prior call (prepare_write) */
  651. if (rc < 0)
  652. ClearPageUptodate(page);
  653. else
  654. SetPageUptodate(page);
  655. mutex_unlock(&lower_inode->i_mutex);
  656. return rc;
  657. }
  658. /**
  659. * write_zeros
  660. * @file: The ecryptfs file
  661. * @index: The index in which we are writing
  662. * @start: The position after the last block of data
  663. * @num_zeros: The number of zeros to write
  664. *
  665. * Write a specified number of zero's to a page.
  666. *
  667. * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
  668. */
  669. static
  670. int write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
  671. {
  672. int rc = 0;
  673. struct page *tmp_page;
  674. tmp_page = ecryptfs_get1page(file, index);
  675. if (IS_ERR(tmp_page)) {
  676. ecryptfs_printk(KERN_ERR, "Error getting page at index "
  677. "[0x%.16x]\n", index);
  678. rc = PTR_ERR(tmp_page);
  679. goto out;
  680. }
  681. kmap(tmp_page);
  682. rc = ecryptfs_prepare_write(file, tmp_page, start, start + num_zeros);
  683. if (rc) {
  684. ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
  685. "to remainder of page at index [0x%.16x]\n",
  686. index);
  687. kunmap(tmp_page);
  688. page_cache_release(tmp_page);
  689. goto out;
  690. }
  691. memset(((char *)page_address(tmp_page) + start), 0, num_zeros);
  692. rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
  693. if (rc < 0) {
  694. ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
  695. "to remainder of page at index [0x%.16x]\n",
  696. index);
  697. kunmap(tmp_page);
  698. page_cache_release(tmp_page);
  699. goto out;
  700. }
  701. rc = 0;
  702. kunmap(tmp_page);
  703. page_cache_release(tmp_page);
  704. out:
  705. return rc;
  706. }
  707. static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
  708. {
  709. int rc = 0;
  710. struct inode *inode;
  711. struct inode *lower_inode;
  712. inode = (struct inode *)mapping->host;
  713. lower_inode = ecryptfs_inode_to_lower(inode);
  714. if (lower_inode->i_mapping->a_ops->bmap)
  715. rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
  716. block);
  717. return rc;
  718. }
  719. static void ecryptfs_sync_page(struct page *page)
  720. {
  721. struct inode *inode;
  722. struct inode *lower_inode;
  723. struct page *lower_page;
  724. inode = page->mapping->host;
  725. lower_inode = ecryptfs_inode_to_lower(inode);
  726. /* NOTE: Recently swapped with grab_cache_page(), since
  727. * sync_page() just makes sure that pending I/O gets done. */
  728. lower_page = find_lock_page(lower_inode->i_mapping, page->index);
  729. if (!lower_page) {
  730. ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n");
  731. return;
  732. }
  733. lower_page->mapping->a_ops->sync_page(lower_page);
  734. ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
  735. lower_page->index);
  736. unlock_page(lower_page);
  737. page_cache_release(lower_page);
  738. }
  739. struct address_space_operations ecryptfs_aops = {
  740. .writepage = ecryptfs_writepage,
  741. .readpage = ecryptfs_readpage,
  742. .prepare_write = ecryptfs_prepare_write,
  743. .commit_write = ecryptfs_commit_write,
  744. .bmap = ecryptfs_bmap,
  745. .sync_page = ecryptfs_sync_page,
  746. };