mmap.c 24 KB

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