mmap.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. static struct page *ecryptfs_get1page(struct file *file, int 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. struct ecryptfs_page_crypt_context ctx;
  168. int rc;
  169. ctx.page = page;
  170. ctx.mode = ECRYPTFS_WRITEPAGE_MODE;
  171. ctx.param.wbc = wbc;
  172. rc = ecryptfs_encrypt_page(&ctx);
  173. if (rc) {
  174. ecryptfs_printk(KERN_WARNING, "Error encrypting "
  175. "page (upper index [0x%.16x])\n", page->index);
  176. ClearPageUptodate(page);
  177. goto out;
  178. }
  179. SetPageUptodate(page);
  180. unlock_page(page);
  181. out:
  182. return rc;
  183. }
  184. /**
  185. * Reads the data from the lower file file at index lower_page_index
  186. * and copies that data into page.
  187. *
  188. * @param page Page to fill
  189. * @param lower_page_index Index of the page in the lower file to get
  190. */
  191. int ecryptfs_do_readpage(struct file *file, struct page *page,
  192. pgoff_t lower_page_index)
  193. {
  194. int rc;
  195. struct dentry *dentry;
  196. struct file *lower_file;
  197. struct dentry *lower_dentry;
  198. struct inode *inode;
  199. struct inode *lower_inode;
  200. char *page_data;
  201. struct page *lower_page = NULL;
  202. char *lower_page_data;
  203. const struct address_space_operations *lower_a_ops;
  204. dentry = file->f_path.dentry;
  205. lower_file = ecryptfs_file_to_lower(file);
  206. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  207. inode = dentry->d_inode;
  208. lower_inode = ecryptfs_inode_to_lower(inode);
  209. lower_a_ops = lower_inode->i_mapping->a_ops;
  210. lower_page = read_cache_page(lower_inode->i_mapping, lower_page_index,
  211. (filler_t *)lower_a_ops->readpage,
  212. (void *)lower_file);
  213. if (IS_ERR(lower_page)) {
  214. rc = PTR_ERR(lower_page);
  215. lower_page = NULL;
  216. ecryptfs_printk(KERN_ERR, "Error reading from page cache\n");
  217. goto out;
  218. }
  219. page_data = kmap_atomic(page, KM_USER0);
  220. lower_page_data = kmap_atomic(lower_page, KM_USER1);
  221. memcpy(page_data, lower_page_data, PAGE_CACHE_SIZE);
  222. kunmap_atomic(lower_page_data, KM_USER1);
  223. kunmap_atomic(page_data, KM_USER0);
  224. flush_dcache_page(page);
  225. rc = 0;
  226. out:
  227. if (likely(lower_page))
  228. page_cache_release(lower_page);
  229. if (rc == 0)
  230. SetPageUptodate(page);
  231. else
  232. ClearPageUptodate(page);
  233. return rc;
  234. }
  235. /**
  236. * Header Extent:
  237. * Octets 0-7: Unencrypted file size (big-endian)
  238. * Octets 8-15: eCryptfs special marker
  239. * Octets 16-19: Flags
  240. * Octet 16: File format version number (between 0 and 255)
  241. * Octets 17-18: Reserved
  242. * Octet 19: Bit 1 (lsb): Reserved
  243. * Bit 2: Encrypted?
  244. * Bits 3-8: Reserved
  245. * Octets 20-23: Header extent size (big-endian)
  246. * Octets 24-25: Number of header extents at front of file
  247. * (big-endian)
  248. * Octet 26: Begin RFC 2440 authentication token packet set
  249. */
  250. static void set_header_info(char *page_virt,
  251. struct ecryptfs_crypt_stat *crypt_stat)
  252. {
  253. size_t written;
  254. int save_num_header_extents_at_front =
  255. crypt_stat->num_header_extents_at_front;
  256. crypt_stat->num_header_extents_at_front = 1;
  257. ecryptfs_write_header_metadata(page_virt + 20, crypt_stat, &written);
  258. crypt_stat->num_header_extents_at_front =
  259. save_num_header_extents_at_front;
  260. }
  261. /**
  262. * ecryptfs_readpage
  263. * @file: This is an ecryptfs file
  264. * @page: ecryptfs associated page to stick the read data into
  265. *
  266. * Read in a page, decrypting if necessary.
  267. *
  268. * Returns zero on success; non-zero on error.
  269. */
  270. static int ecryptfs_readpage(struct file *file, struct page *page)
  271. {
  272. int rc = 0;
  273. struct ecryptfs_crypt_stat *crypt_stat;
  274. BUG_ON(!(file && file->f_path.dentry && file->f_path.dentry->d_inode));
  275. crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
  276. ->crypt_stat;
  277. if (!crypt_stat
  278. || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
  279. || (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
  280. ecryptfs_printk(KERN_DEBUG,
  281. "Passing through unencrypted page\n");
  282. rc = ecryptfs_do_readpage(file, page, page->index);
  283. if (rc) {
  284. ecryptfs_printk(KERN_ERR, "Error reading page; rc = "
  285. "[%d]\n", rc);
  286. goto out;
  287. }
  288. } else if (crypt_stat->flags & ECRYPTFS_VIEW_AS_ENCRYPTED) {
  289. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR) {
  290. int num_pages_in_header_region =
  291. (crypt_stat->header_extent_size
  292. / PAGE_CACHE_SIZE);
  293. if (page->index < num_pages_in_header_region) {
  294. char *page_virt;
  295. page_virt = kmap_atomic(page, KM_USER0);
  296. memset(page_virt, 0, PAGE_CACHE_SIZE);
  297. if (page->index == 0) {
  298. rc = ecryptfs_read_xattr_region(
  299. page_virt, file->f_path.dentry);
  300. set_header_info(page_virt, crypt_stat);
  301. }
  302. kunmap_atomic(page_virt, KM_USER0);
  303. flush_dcache_page(page);
  304. if (rc) {
  305. printk(KERN_ERR "Error reading xattr "
  306. "region\n");
  307. goto out;
  308. }
  309. } else {
  310. rc = ecryptfs_do_readpage(
  311. file, page,
  312. (page->index
  313. - num_pages_in_header_region));
  314. if (rc) {
  315. printk(KERN_ERR "Error reading page; "
  316. "rc = [%d]\n", rc);
  317. goto out;
  318. }
  319. }
  320. } else {
  321. rc = ecryptfs_do_readpage(file, page, page->index);
  322. if (rc) {
  323. printk(KERN_ERR "Error reading page; rc = "
  324. "[%d]\n", rc);
  325. goto out;
  326. }
  327. }
  328. } else {
  329. rc = ecryptfs_decrypt_page(file, page);
  330. if (rc) {
  331. ecryptfs_printk(KERN_ERR, "Error decrypting page; "
  332. "rc = [%d]\n", rc);
  333. goto out;
  334. }
  335. }
  336. SetPageUptodate(page);
  337. out:
  338. if (rc)
  339. ClearPageUptodate(page);
  340. ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
  341. page->index);
  342. unlock_page(page);
  343. return rc;
  344. }
  345. /**
  346. * Called with lower inode mutex held.
  347. */
  348. static int fill_zeros_to_end_of_page(struct page *page, unsigned int to)
  349. {
  350. struct inode *inode = page->mapping->host;
  351. int end_byte_in_page;
  352. if ((i_size_read(inode) / PAGE_CACHE_SIZE) != page->index)
  353. goto out;
  354. end_byte_in_page = i_size_read(inode) % PAGE_CACHE_SIZE;
  355. if (to > end_byte_in_page)
  356. end_byte_in_page = to;
  357. zero_user_page(page, end_byte_in_page,
  358. PAGE_CACHE_SIZE - end_byte_in_page, KM_USER0);
  359. out:
  360. return 0;
  361. }
  362. /**
  363. * eCryptfs does not currently support holes. When writing after a
  364. * seek past the end of the file, eCryptfs fills in 0's through to the
  365. * current location. The code to fill in the 0's to all the
  366. * intermediate pages calls ecryptfs_prepare_write_no_truncate().
  367. */
  368. static int
  369. ecryptfs_prepare_write_no_truncate(struct file *file, struct page *page,
  370. unsigned from, unsigned to)
  371. {
  372. int rc = 0;
  373. if (from == 0 && to == PAGE_CACHE_SIZE)
  374. goto out; /* If we are writing a full page, it will be
  375. up to date. */
  376. if (!PageUptodate(page))
  377. rc = ecryptfs_do_readpage(file, page, page->index);
  378. out:
  379. return rc;
  380. }
  381. static int ecryptfs_prepare_write(struct file *file, struct page *page,
  382. unsigned from, unsigned to)
  383. {
  384. int rc = 0;
  385. if (from == 0 && to == PAGE_CACHE_SIZE)
  386. goto out; /* If we are writing a full page, it will be
  387. up to date. */
  388. if (!PageUptodate(page))
  389. rc = ecryptfs_do_readpage(file, page, page->index);
  390. if (page->index != 0) {
  391. loff_t end_of_prev_pg_pos = page_offset(page) - 1;
  392. if (end_of_prev_pg_pos > i_size_read(page->mapping->host)) {
  393. rc = ecryptfs_truncate(file->f_path.dentry,
  394. end_of_prev_pg_pos);
  395. if (rc) {
  396. printk(KERN_ERR "Error on attempt to "
  397. "truncate to (higher) offset [%lld];"
  398. " rc = [%d]\n", end_of_prev_pg_pos, rc);
  399. goto out;
  400. }
  401. }
  402. if (end_of_prev_pg_pos + 1 > i_size_read(page->mapping->host))
  403. zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
  404. }
  405. out:
  406. return rc;
  407. }
  408. int ecryptfs_writepage_and_release_lower_page(struct page *lower_page,
  409. struct inode *lower_inode,
  410. struct writeback_control *wbc)
  411. {
  412. int rc = 0;
  413. rc = lower_inode->i_mapping->a_ops->writepage(lower_page, wbc);
  414. if (rc) {
  415. ecryptfs_printk(KERN_ERR, "Error calling lower writepage(); "
  416. "rc = [%d]\n", rc);
  417. goto out;
  418. }
  419. lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
  420. page_cache_release(lower_page);
  421. out:
  422. return rc;
  423. }
  424. static
  425. void ecryptfs_release_lower_page(struct page *lower_page, int page_locked)
  426. {
  427. if (page_locked)
  428. unlock_page(lower_page);
  429. page_cache_release(lower_page);
  430. }
  431. /**
  432. * ecryptfs_write_inode_size_to_header
  433. *
  434. * Writes the lower file size to the first 8 bytes of the header.
  435. *
  436. * Returns zero on success; non-zero on error.
  437. */
  438. static int ecryptfs_write_inode_size_to_header(struct file *lower_file,
  439. struct inode *lower_inode,
  440. struct inode *inode)
  441. {
  442. int rc = 0;
  443. struct page *header_page;
  444. char *header_virt;
  445. const struct address_space_operations *lower_a_ops;
  446. u64 file_size;
  447. retry:
  448. header_page = grab_cache_page(lower_inode->i_mapping, 0);
  449. if (!header_page) {
  450. ecryptfs_printk(KERN_ERR, "grab_cache_page for "
  451. "lower_page_index 0 failed\n");
  452. rc = -EINVAL;
  453. goto out;
  454. }
  455. lower_a_ops = lower_inode->i_mapping->a_ops;
  456. rc = lower_a_ops->prepare_write(lower_file, header_page, 0, 8);
  457. if (rc) {
  458. if (rc == AOP_TRUNCATED_PAGE) {
  459. ecryptfs_release_lower_page(header_page, 0);
  460. goto retry;
  461. } else
  462. ecryptfs_release_lower_page(header_page, 1);
  463. goto out;
  464. }
  465. file_size = (u64)i_size_read(inode);
  466. ecryptfs_printk(KERN_DEBUG, "Writing size: [0x%.16x]\n", file_size);
  467. file_size = cpu_to_be64(file_size);
  468. header_virt = kmap_atomic(header_page, KM_USER0);
  469. memcpy(header_virt, &file_size, sizeof(u64));
  470. kunmap_atomic(header_virt, KM_USER0);
  471. flush_dcache_page(header_page);
  472. rc = lower_a_ops->commit_write(lower_file, header_page, 0, 8);
  473. if (rc < 0)
  474. ecryptfs_printk(KERN_ERR, "Error commiting header page "
  475. "write\n");
  476. if (rc == AOP_TRUNCATED_PAGE) {
  477. ecryptfs_release_lower_page(header_page, 0);
  478. goto retry;
  479. } else
  480. ecryptfs_release_lower_page(header_page, 1);
  481. lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
  482. mark_inode_dirty_sync(inode);
  483. out:
  484. return rc;
  485. }
  486. static int ecryptfs_write_inode_size_to_xattr(struct inode *lower_inode,
  487. struct inode *inode,
  488. struct dentry *ecryptfs_dentry,
  489. int lower_i_mutex_held)
  490. {
  491. ssize_t size;
  492. void *xattr_virt;
  493. struct dentry *lower_dentry;
  494. u64 file_size;
  495. int rc;
  496. xattr_virt = kmem_cache_alloc(ecryptfs_xattr_cache, GFP_KERNEL);
  497. if (!xattr_virt) {
  498. printk(KERN_ERR "Out of memory whilst attempting to write "
  499. "inode size to xattr\n");
  500. rc = -ENOMEM;
  501. goto out;
  502. }
  503. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  504. if (!lower_dentry->d_inode->i_op->getxattr ||
  505. !lower_dentry->d_inode->i_op->setxattr) {
  506. printk(KERN_WARNING
  507. "No support for setting xattr in lower filesystem\n");
  508. rc = -ENOSYS;
  509. kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
  510. goto out;
  511. }
  512. if (!lower_i_mutex_held)
  513. mutex_lock(&lower_dentry->d_inode->i_mutex);
  514. size = lower_dentry->d_inode->i_op->getxattr(lower_dentry,
  515. ECRYPTFS_XATTR_NAME,
  516. xattr_virt,
  517. PAGE_CACHE_SIZE);
  518. if (!lower_i_mutex_held)
  519. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  520. if (size < 0)
  521. size = 8;
  522. file_size = (u64)i_size_read(inode);
  523. file_size = cpu_to_be64(file_size);
  524. memcpy(xattr_virt, &file_size, sizeof(u64));
  525. if (!lower_i_mutex_held)
  526. mutex_lock(&lower_dentry->d_inode->i_mutex);
  527. rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry,
  528. ECRYPTFS_XATTR_NAME,
  529. xattr_virt, size, 0);
  530. if (!lower_i_mutex_held)
  531. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  532. if (rc)
  533. printk(KERN_ERR "Error whilst attempting to write inode size "
  534. "to lower file xattr; rc = [%d]\n", rc);
  535. kmem_cache_free(ecryptfs_xattr_cache, xattr_virt);
  536. out:
  537. return rc;
  538. }
  539. int
  540. ecryptfs_write_inode_size_to_metadata(struct file *lower_file,
  541. struct inode *lower_inode,
  542. struct inode *inode,
  543. struct dentry *ecryptfs_dentry,
  544. int lower_i_mutex_held)
  545. {
  546. struct ecryptfs_crypt_stat *crypt_stat;
  547. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  548. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  549. return ecryptfs_write_inode_size_to_xattr(lower_inode, inode,
  550. ecryptfs_dentry,
  551. lower_i_mutex_held);
  552. else
  553. return ecryptfs_write_inode_size_to_header(lower_file,
  554. lower_inode,
  555. inode);
  556. }
  557. int ecryptfs_get_lower_page(struct page **lower_page, struct inode *lower_inode,
  558. struct file *lower_file,
  559. unsigned long lower_page_index, int byte_offset,
  560. int region_bytes)
  561. {
  562. int rc = 0;
  563. retry:
  564. *lower_page = grab_cache_page(lower_inode->i_mapping, lower_page_index);
  565. if (!(*lower_page)) {
  566. rc = -EINVAL;
  567. ecryptfs_printk(KERN_ERR, "Error attempting to grab "
  568. "lower page with index [0x%.16x]\n",
  569. lower_page_index);
  570. goto out;
  571. }
  572. rc = lower_inode->i_mapping->a_ops->prepare_write(lower_file,
  573. (*lower_page),
  574. byte_offset,
  575. region_bytes);
  576. if (rc) {
  577. if (rc == AOP_TRUNCATED_PAGE) {
  578. ecryptfs_release_lower_page(*lower_page, 0);
  579. goto retry;
  580. } else {
  581. ecryptfs_printk(KERN_ERR, "prepare_write for "
  582. "lower_page_index = [0x%.16x] failed; rc = "
  583. "[%d]\n", lower_page_index, rc);
  584. ecryptfs_release_lower_page(*lower_page, 1);
  585. (*lower_page) = NULL;
  586. }
  587. }
  588. out:
  589. return rc;
  590. }
  591. /**
  592. * ecryptfs_commit_lower_page
  593. *
  594. * Returns zero on success; non-zero on error
  595. */
  596. int
  597. ecryptfs_commit_lower_page(struct page *lower_page, struct inode *lower_inode,
  598. struct file *lower_file, int byte_offset,
  599. int region_size)
  600. {
  601. int page_locked = 1;
  602. int rc = 0;
  603. rc = lower_inode->i_mapping->a_ops->commit_write(
  604. lower_file, lower_page, byte_offset, region_size);
  605. if (rc == AOP_TRUNCATED_PAGE)
  606. page_locked = 0;
  607. if (rc < 0) {
  608. ecryptfs_printk(KERN_ERR,
  609. "Error committing write; rc = [%d]\n", rc);
  610. } else
  611. rc = 0;
  612. ecryptfs_release_lower_page(lower_page, page_locked);
  613. return rc;
  614. }
  615. /**
  616. * ecryptfs_copy_page_to_lower
  617. *
  618. * Used for plaintext pass-through; no page index interpolation
  619. * required.
  620. */
  621. int ecryptfs_copy_page_to_lower(struct page *page, struct inode *lower_inode,
  622. struct file *lower_file)
  623. {
  624. int rc = 0;
  625. struct page *lower_page;
  626. rc = ecryptfs_get_lower_page(&lower_page, lower_inode, lower_file,
  627. page->index, 0, PAGE_CACHE_SIZE);
  628. if (rc) {
  629. ecryptfs_printk(KERN_ERR, "Error attempting to get page "
  630. "at index [0x%.16x]\n", page->index);
  631. goto out;
  632. }
  633. /* TODO: aops */
  634. memcpy((char *)page_address(lower_page), page_address(page),
  635. PAGE_CACHE_SIZE);
  636. rc = ecryptfs_commit_lower_page(lower_page, lower_inode, lower_file,
  637. 0, PAGE_CACHE_SIZE);
  638. if (rc)
  639. ecryptfs_printk(KERN_ERR, "Error attempting to commit page "
  640. "at index [0x%.16x]\n", page->index);
  641. out:
  642. return rc;
  643. }
  644. struct kmem_cache *ecryptfs_xattr_cache;
  645. /**
  646. * ecryptfs_commit_write
  647. * @file: The eCryptfs file object
  648. * @page: The eCryptfs page
  649. * @from: Ignored (we rotate the page IV on each write)
  650. * @to: Ignored
  651. *
  652. * This is where we encrypt the data and pass the encrypted data to
  653. * the lower filesystem. In OpenPGP-compatible mode, we operate on
  654. * entire underlying packets.
  655. */
  656. static int ecryptfs_commit_write(struct file *file, struct page *page,
  657. unsigned from, unsigned to)
  658. {
  659. struct ecryptfs_page_crypt_context ctx;
  660. loff_t pos;
  661. struct inode *inode;
  662. struct inode *lower_inode;
  663. struct file *lower_file;
  664. struct ecryptfs_crypt_stat *crypt_stat;
  665. int rc;
  666. inode = page->mapping->host;
  667. lower_inode = ecryptfs_inode_to_lower(inode);
  668. lower_file = ecryptfs_file_to_lower(file);
  669. mutex_lock(&lower_inode->i_mutex);
  670. crypt_stat = &ecryptfs_inode_to_private(file->f_path.dentry->d_inode)
  671. ->crypt_stat;
  672. if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
  673. ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
  674. "crypt_stat at memory location [%p]\n", crypt_stat);
  675. crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
  676. } else
  677. ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
  678. ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
  679. "(page w/ index = [0x%.16x], to = [%d])\n", page->index,
  680. to);
  681. rc = fill_zeros_to_end_of_page(page, to);
  682. if (rc) {
  683. ecryptfs_printk(KERN_WARNING, "Error attempting to fill "
  684. "zeros in page with index = [0x%.16x]\n",
  685. page->index);
  686. goto out;
  687. }
  688. ctx.page = page;
  689. ctx.mode = ECRYPTFS_PREPARE_COMMIT_MODE;
  690. ctx.param.lower_file = lower_file;
  691. rc = ecryptfs_encrypt_page(&ctx);
  692. if (rc) {
  693. ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper "
  694. "index [0x%.16x])\n", page->index);
  695. goto out;
  696. }
  697. inode->i_blocks = lower_inode->i_blocks;
  698. pos = page_offset(page) + to;
  699. if (pos > i_size_read(inode)) {
  700. i_size_write(inode, pos);
  701. ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
  702. "[0x%.16x]\n", i_size_read(inode));
  703. }
  704. rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode,
  705. inode, file->f_dentry,
  706. ECRYPTFS_LOWER_I_MUTEX_HELD);
  707. if (rc)
  708. printk(KERN_ERR "Error writing inode size to metadata; "
  709. "rc = [%d]\n", rc);
  710. lower_inode->i_mtime = lower_inode->i_ctime = CURRENT_TIME;
  711. mark_inode_dirty_sync(inode);
  712. out:
  713. if (rc < 0)
  714. ClearPageUptodate(page);
  715. else
  716. SetPageUptodate(page);
  717. mutex_unlock(&lower_inode->i_mutex);
  718. return rc;
  719. }
  720. /**
  721. * ecryptfs_write_zeros
  722. * @file: The ecryptfs file
  723. * @index: The index in which we are writing
  724. * @start: The position after the last block of data
  725. * @num_zeros: The number of zeros to write
  726. *
  727. * Write a specified number of zero's to a page.
  728. *
  729. * (start + num_zeros) must be less than or equal to PAGE_CACHE_SIZE
  730. */
  731. int
  732. ecryptfs_write_zeros(struct file *file, pgoff_t index, int start, int num_zeros)
  733. {
  734. int rc = 0;
  735. struct page *tmp_page;
  736. tmp_page = ecryptfs_get1page(file, index);
  737. if (IS_ERR(tmp_page)) {
  738. ecryptfs_printk(KERN_ERR, "Error getting page at index "
  739. "[0x%.16x]\n", index);
  740. rc = PTR_ERR(tmp_page);
  741. goto out;
  742. }
  743. if ((rc = ecryptfs_prepare_write_no_truncate(file, tmp_page, start,
  744. (start + num_zeros)))) {
  745. ecryptfs_printk(KERN_ERR, "Error preparing to write zero's "
  746. "to page at index [0x%.16x]\n",
  747. index);
  748. page_cache_release(tmp_page);
  749. goto out;
  750. }
  751. zero_user_page(tmp_page, start, num_zeros, KM_USER0);
  752. rc = ecryptfs_commit_write(file, tmp_page, start, start + num_zeros);
  753. if (rc < 0) {
  754. ecryptfs_printk(KERN_ERR, "Error attempting to write zero's "
  755. "to remainder of page at index [0x%.16x]\n",
  756. index);
  757. page_cache_release(tmp_page);
  758. goto out;
  759. }
  760. rc = 0;
  761. page_cache_release(tmp_page);
  762. out:
  763. return rc;
  764. }
  765. static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block)
  766. {
  767. int rc = 0;
  768. struct inode *inode;
  769. struct inode *lower_inode;
  770. inode = (struct inode *)mapping->host;
  771. lower_inode = ecryptfs_inode_to_lower(inode);
  772. if (lower_inode->i_mapping->a_ops->bmap)
  773. rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping,
  774. block);
  775. return rc;
  776. }
  777. static void ecryptfs_sync_page(struct page *page)
  778. {
  779. struct inode *inode;
  780. struct inode *lower_inode;
  781. struct page *lower_page;
  782. inode = page->mapping->host;
  783. lower_inode = ecryptfs_inode_to_lower(inode);
  784. /* NOTE: Recently swapped with grab_cache_page(), since
  785. * sync_page() just makes sure that pending I/O gets done. */
  786. lower_page = find_lock_page(lower_inode->i_mapping, page->index);
  787. if (!lower_page) {
  788. ecryptfs_printk(KERN_DEBUG, "find_lock_page failed\n");
  789. return;
  790. }
  791. lower_page->mapping->a_ops->sync_page(lower_page);
  792. ecryptfs_printk(KERN_DEBUG, "Unlocking page with index = [0x%.16x]\n",
  793. lower_page->index);
  794. unlock_page(lower_page);
  795. page_cache_release(lower_page);
  796. }
  797. struct address_space_operations ecryptfs_aops = {
  798. .writepage = ecryptfs_writepage,
  799. .readpage = ecryptfs_readpage,
  800. .prepare_write = ecryptfs_prepare_write,
  801. .commit_write = ecryptfs_commit_write,
  802. .bmap = ecryptfs_bmap,
  803. .sync_page = ecryptfs_sync_page,
  804. };