mmap.c 22 KB

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