inode.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 1997-2004 Erez Zadok
  5. * Copyright (C) 2001-2004 Stony Brook University
  6. * Copyright (C) 2004-2007 International Business Machines Corp.
  7. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
  8. * Michael C. Thompsion <mcthomps@us.ibm.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. * 02111-1307, USA.
  24. */
  25. #include <linux/file.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/dcache.h>
  29. #include <linux/namei.h>
  30. #include <linux/mount.h>
  31. #include <linux/crypto.h>
  32. #include <linux/fs_stack.h>
  33. #include "ecryptfs_kernel.h"
  34. static struct dentry *lock_parent(struct dentry *dentry)
  35. {
  36. struct dentry *dir;
  37. dir = dget(dentry->d_parent);
  38. mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
  39. return dir;
  40. }
  41. static void unlock_parent(struct dentry *dentry)
  42. {
  43. mutex_unlock(&(dentry->d_parent->d_inode->i_mutex));
  44. dput(dentry->d_parent);
  45. }
  46. static void unlock_dir(struct dentry *dir)
  47. {
  48. mutex_unlock(&dir->d_inode->i_mutex);
  49. dput(dir);
  50. }
  51. /**
  52. * ecryptfs_create_underlying_file
  53. * @lower_dir_inode: inode of the parent in the lower fs of the new file
  54. * @lower_dentry: New file's dentry in the lower fs
  55. * @ecryptfs_dentry: New file's dentry in ecryptfs
  56. * @mode: The mode of the new file
  57. * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
  58. *
  59. * Creates the file in the lower file system.
  60. *
  61. * Returns zero on success; non-zero on error condition
  62. */
  63. static int
  64. ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
  65. struct dentry *dentry, int mode,
  66. struct nameidata *nd)
  67. {
  68. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  69. struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
  70. struct dentry *dentry_save;
  71. struct vfsmount *vfsmount_save;
  72. int rc;
  73. dentry_save = nd->dentry;
  74. vfsmount_save = nd->mnt;
  75. nd->dentry = lower_dentry;
  76. nd->mnt = lower_mnt;
  77. rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
  78. nd->dentry = dentry_save;
  79. nd->mnt = vfsmount_save;
  80. return rc;
  81. }
  82. /**
  83. * ecryptfs_do_create
  84. * @directory_inode: inode of the new file's dentry's parent in ecryptfs
  85. * @ecryptfs_dentry: New file's dentry in ecryptfs
  86. * @mode: The mode of the new file
  87. * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
  88. *
  89. * Creates the underlying file and the eCryptfs inode which will link to
  90. * it. It will also update the eCryptfs directory inode to mimic the
  91. * stat of the lower directory inode.
  92. *
  93. * Returns zero on success; non-zero on error condition
  94. */
  95. static int
  96. ecryptfs_do_create(struct inode *directory_inode,
  97. struct dentry *ecryptfs_dentry, int mode,
  98. struct nameidata *nd)
  99. {
  100. int rc;
  101. struct dentry *lower_dentry;
  102. struct dentry *lower_dir_dentry;
  103. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  104. lower_dir_dentry = lock_parent(lower_dentry);
  105. if (unlikely(IS_ERR(lower_dir_dentry))) {
  106. ecryptfs_printk(KERN_ERR, "Error locking directory of "
  107. "dentry\n");
  108. rc = PTR_ERR(lower_dir_dentry);
  109. goto out;
  110. }
  111. rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
  112. ecryptfs_dentry, mode, nd);
  113. if (unlikely(rc)) {
  114. ecryptfs_printk(KERN_ERR,
  115. "Failure to create underlying file\n");
  116. goto out_lock;
  117. }
  118. rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
  119. directory_inode->i_sb, 0);
  120. if (rc) {
  121. ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
  122. goto out_lock;
  123. }
  124. fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
  125. fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
  126. out_lock:
  127. unlock_dir(lower_dir_dentry);
  128. out:
  129. return rc;
  130. }
  131. /**
  132. * grow_file
  133. * @ecryptfs_dentry: the ecryptfs dentry
  134. * @lower_file: The lower file
  135. * @inode: The ecryptfs inode
  136. * @lower_inode: The lower inode
  137. *
  138. * This is the code which will grow the file to its correct size.
  139. */
  140. static int grow_file(struct dentry *ecryptfs_dentry, struct file *lower_file,
  141. struct inode *inode, struct inode *lower_inode)
  142. {
  143. int rc = 0;
  144. struct file fake_file;
  145. struct ecryptfs_file_info tmp_file_info;
  146. memset(&fake_file, 0, sizeof(fake_file));
  147. fake_file.f_path.dentry = ecryptfs_dentry;
  148. memset(&tmp_file_info, 0, sizeof(tmp_file_info));
  149. ecryptfs_set_file_private(&fake_file, &tmp_file_info);
  150. ecryptfs_set_file_lower(&fake_file, lower_file);
  151. rc = ecryptfs_fill_zeros(&fake_file, 1);
  152. if (rc) {
  153. ecryptfs_inode_to_private(inode)->crypt_stat.flags |=
  154. ECRYPTFS_SECURITY_WARNING;
  155. ecryptfs_printk(KERN_WARNING, "Error attempting to fill zeros "
  156. "in file; rc = [%d]\n", rc);
  157. goto out;
  158. }
  159. i_size_write(inode, 0);
  160. rc = ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode,
  161. inode, ecryptfs_dentry,
  162. ECRYPTFS_LOWER_I_MUTEX_NOT_HELD);
  163. ecryptfs_inode_to_private(inode)->crypt_stat.flags |= ECRYPTFS_NEW_FILE;
  164. out:
  165. return rc;
  166. }
  167. /**
  168. * ecryptfs_initialize_file
  169. *
  170. * Cause the file to be changed from a basic empty file to an ecryptfs
  171. * file with a header and first data page.
  172. *
  173. * Returns zero on success
  174. */
  175. static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
  176. {
  177. int rc = 0;
  178. int lower_flags;
  179. struct ecryptfs_crypt_stat *crypt_stat;
  180. struct dentry *lower_dentry;
  181. struct file *lower_file;
  182. struct inode *inode, *lower_inode;
  183. struct vfsmount *lower_mnt;
  184. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  185. ecryptfs_printk(KERN_DEBUG, "lower_dentry->d_name.name = [%s]\n",
  186. lower_dentry->d_name.name);
  187. inode = ecryptfs_dentry->d_inode;
  188. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  189. lower_flags = ((O_CREAT | O_TRUNC) & O_ACCMODE) | O_RDWR;
  190. lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
  191. /* Corresponding fput() at end of this function */
  192. if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
  193. lower_flags))) {
  194. ecryptfs_printk(KERN_ERR,
  195. "Error opening dentry; rc = [%i]\n", rc);
  196. goto out;
  197. }
  198. lower_inode = lower_dentry->d_inode;
  199. if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
  200. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  201. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  202. goto out_fput;
  203. }
  204. crypt_stat->flags |= ECRYPTFS_NEW_FILE;
  205. ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
  206. rc = ecryptfs_new_file_context(ecryptfs_dentry);
  207. if (rc) {
  208. ecryptfs_printk(KERN_DEBUG, "Error creating new file "
  209. "context\n");
  210. goto out_fput;
  211. }
  212. rc = ecryptfs_write_metadata(ecryptfs_dentry, lower_file);
  213. if (rc) {
  214. ecryptfs_printk(KERN_DEBUG, "Error writing headers\n");
  215. goto out_fput;
  216. }
  217. rc = grow_file(ecryptfs_dentry, lower_file, inode, lower_inode);
  218. out_fput:
  219. if ((rc = ecryptfs_close_lower_file(lower_file)))
  220. printk(KERN_ERR "Error closing lower_file\n");
  221. out:
  222. return rc;
  223. }
  224. /**
  225. * ecryptfs_create
  226. * @dir: The inode of the directory in which to create the file.
  227. * @dentry: The eCryptfs dentry
  228. * @mode: The mode of the new file.
  229. * @nd: nameidata
  230. *
  231. * Creates a new file.
  232. *
  233. * Returns zero on success; non-zero on error condition
  234. */
  235. static int
  236. ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
  237. int mode, struct nameidata *nd)
  238. {
  239. int rc;
  240. rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
  241. if (unlikely(rc)) {
  242. ecryptfs_printk(KERN_WARNING, "Failed to create file in"
  243. "lower filesystem\n");
  244. goto out;
  245. }
  246. /* At this point, a file exists on "disk"; we need to make sure
  247. * that this on disk file is prepared to be an ecryptfs file */
  248. rc = ecryptfs_initialize_file(ecryptfs_dentry);
  249. out:
  250. return rc;
  251. }
  252. /**
  253. * ecryptfs_lookup
  254. * @dir: inode
  255. * @dentry: The dentry
  256. * @nd: nameidata, may be NULL
  257. *
  258. * Find a file on disk. If the file does not exist, then we'll add it to the
  259. * dentry cache and continue on to read it from the disk.
  260. */
  261. static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
  262. struct nameidata *nd)
  263. {
  264. int rc = 0;
  265. struct dentry *lower_dir_dentry;
  266. struct dentry *lower_dentry;
  267. struct vfsmount *lower_mnt;
  268. char *encoded_name;
  269. unsigned int encoded_namelen;
  270. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  271. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  272. char *page_virt = NULL;
  273. struct inode *lower_inode;
  274. u64 file_size;
  275. lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
  276. dentry->d_op = &ecryptfs_dops;
  277. if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, "."))
  278. || (dentry->d_name.len == 2
  279. && !strcmp(dentry->d_name.name, ".."))) {
  280. d_drop(dentry);
  281. goto out;
  282. }
  283. encoded_namelen = ecryptfs_encode_filename(crypt_stat,
  284. dentry->d_name.name,
  285. dentry->d_name.len,
  286. &encoded_name);
  287. if (encoded_namelen < 0) {
  288. rc = encoded_namelen;
  289. d_drop(dentry);
  290. goto out;
  291. }
  292. ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen "
  293. "= [%d]\n", encoded_name, encoded_namelen);
  294. lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry,
  295. encoded_namelen - 1);
  296. kfree(encoded_name);
  297. if (IS_ERR(lower_dentry)) {
  298. ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
  299. rc = PTR_ERR(lower_dentry);
  300. d_drop(dentry);
  301. goto out;
  302. }
  303. lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
  304. ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->"
  305. "d_name.name = [%s]\n", lower_dentry,
  306. lower_dentry->d_name.name);
  307. lower_inode = lower_dentry->d_inode;
  308. fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode);
  309. BUG_ON(!atomic_read(&lower_dentry->d_count));
  310. ecryptfs_set_dentry_private(dentry,
  311. kmem_cache_alloc(ecryptfs_dentry_info_cache,
  312. GFP_KERNEL));
  313. if (!ecryptfs_dentry_to_private(dentry)) {
  314. rc = -ENOMEM;
  315. ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting "
  316. "to allocate ecryptfs_dentry_info struct\n");
  317. goto out_dput;
  318. }
  319. ecryptfs_set_dentry_lower(dentry, lower_dentry);
  320. ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
  321. if (!lower_dentry->d_inode) {
  322. /* We want to add because we couldn't find in lower */
  323. d_add(dentry, NULL);
  324. goto out;
  325. }
  326. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 1);
  327. if (rc) {
  328. ecryptfs_printk(KERN_ERR, "Error interposing\n");
  329. goto out_dput;
  330. }
  331. if (S_ISDIR(lower_inode->i_mode)) {
  332. ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
  333. goto out;
  334. }
  335. if (S_ISLNK(lower_inode->i_mode)) {
  336. ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
  337. goto out;
  338. }
  339. if (!nd) {
  340. ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave"
  341. "as we *think* we are about to unlink\n");
  342. goto out;
  343. }
  344. /* Released in this function */
  345. page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2,
  346. GFP_USER);
  347. if (!page_virt) {
  348. rc = -ENOMEM;
  349. ecryptfs_printk(KERN_ERR,
  350. "Cannot ecryptfs_kmalloc a page\n");
  351. goto out_dput;
  352. }
  353. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  354. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
  355. ecryptfs_set_default_sizes(crypt_stat);
  356. rc = ecryptfs_read_and_validate_header_region(page_virt, lower_dentry,
  357. nd->mnt);
  358. if (rc) {
  359. rc = ecryptfs_read_and_validate_xattr_region(page_virt, dentry);
  360. if (rc) {
  361. printk(KERN_DEBUG "Valid metadata not found in header "
  362. "region or xattr region; treating file as "
  363. "unencrypted\n");
  364. rc = 0;
  365. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  366. goto out;
  367. }
  368. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  369. }
  370. mount_crypt_stat = &ecryptfs_superblock_to_private(
  371. dentry->d_sb)->mount_crypt_stat;
  372. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
  373. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  374. file_size = (crypt_stat->header_extent_size
  375. + i_size_read(lower_dentry->d_inode));
  376. else
  377. file_size = i_size_read(lower_dentry->d_inode);
  378. } else {
  379. memcpy(&file_size, page_virt, sizeof(file_size));
  380. file_size = be64_to_cpu(file_size);
  381. }
  382. i_size_write(dentry->d_inode, (loff_t)file_size);
  383. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  384. goto out;
  385. out_dput:
  386. dput(lower_dentry);
  387. d_drop(dentry);
  388. out:
  389. return ERR_PTR(rc);
  390. }
  391. static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
  392. struct dentry *new_dentry)
  393. {
  394. struct dentry *lower_old_dentry;
  395. struct dentry *lower_new_dentry;
  396. struct dentry *lower_dir_dentry;
  397. u64 file_size_save;
  398. int rc;
  399. file_size_save = i_size_read(old_dentry->d_inode);
  400. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  401. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  402. dget(lower_old_dentry);
  403. dget(lower_new_dentry);
  404. lower_dir_dentry = lock_parent(lower_new_dentry);
  405. rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
  406. lower_new_dentry);
  407. if (rc || !lower_new_dentry->d_inode)
  408. goto out_lock;
  409. rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
  410. if (rc)
  411. goto out_lock;
  412. fsstack_copy_attr_times(dir, lower_new_dentry->d_inode);
  413. fsstack_copy_inode_size(dir, lower_new_dentry->d_inode);
  414. old_dentry->d_inode->i_nlink =
  415. ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
  416. i_size_write(new_dentry->d_inode, file_size_save);
  417. out_lock:
  418. unlock_dir(lower_dir_dentry);
  419. dput(lower_new_dentry);
  420. dput(lower_old_dentry);
  421. d_drop(lower_old_dentry);
  422. d_drop(new_dentry);
  423. d_drop(old_dentry);
  424. return rc;
  425. }
  426. static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
  427. {
  428. int rc = 0;
  429. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  430. struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
  431. lock_parent(lower_dentry);
  432. rc = vfs_unlink(lower_dir_inode, lower_dentry);
  433. if (rc) {
  434. printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
  435. goto out_unlock;
  436. }
  437. fsstack_copy_attr_times(dir, lower_dir_inode);
  438. dentry->d_inode->i_nlink =
  439. ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
  440. dentry->d_inode->i_ctime = dir->i_ctime;
  441. out_unlock:
  442. unlock_parent(lower_dentry);
  443. return rc;
  444. }
  445. static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
  446. const char *symname)
  447. {
  448. int rc;
  449. struct dentry *lower_dentry;
  450. struct dentry *lower_dir_dentry;
  451. umode_t mode;
  452. char *encoded_symname;
  453. unsigned int encoded_symlen;
  454. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  455. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  456. dget(lower_dentry);
  457. lower_dir_dentry = lock_parent(lower_dentry);
  458. mode = S_IALLUGO;
  459. encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
  460. strlen(symname),
  461. &encoded_symname);
  462. if (encoded_symlen < 0) {
  463. rc = encoded_symlen;
  464. goto out_lock;
  465. }
  466. rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
  467. encoded_symname, mode);
  468. kfree(encoded_symname);
  469. if (rc || !lower_dentry->d_inode)
  470. goto out_lock;
  471. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  472. if (rc)
  473. goto out_lock;
  474. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  475. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  476. out_lock:
  477. unlock_dir(lower_dir_dentry);
  478. dput(lower_dentry);
  479. if (!dentry->d_inode)
  480. d_drop(dentry);
  481. return rc;
  482. }
  483. static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  484. {
  485. int rc;
  486. struct dentry *lower_dentry;
  487. struct dentry *lower_dir_dentry;
  488. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  489. lower_dir_dentry = lock_parent(lower_dentry);
  490. rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
  491. if (rc || !lower_dentry->d_inode)
  492. goto out;
  493. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  494. if (rc)
  495. goto out;
  496. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  497. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  498. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  499. out:
  500. unlock_dir(lower_dir_dentry);
  501. if (!dentry->d_inode)
  502. d_drop(dentry);
  503. return rc;
  504. }
  505. static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
  506. {
  507. struct dentry *lower_dentry;
  508. struct dentry *lower_dir_dentry;
  509. int rc;
  510. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  511. dget(dentry);
  512. lower_dir_dentry = lock_parent(lower_dentry);
  513. dget(lower_dentry);
  514. rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
  515. dput(lower_dentry);
  516. if (!rc)
  517. d_delete(lower_dentry);
  518. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  519. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  520. unlock_dir(lower_dir_dentry);
  521. if (!rc)
  522. d_drop(dentry);
  523. dput(dentry);
  524. return rc;
  525. }
  526. static int
  527. ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  528. {
  529. int rc;
  530. struct dentry *lower_dentry;
  531. struct dentry *lower_dir_dentry;
  532. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  533. lower_dir_dentry = lock_parent(lower_dentry);
  534. rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
  535. if (rc || !lower_dentry->d_inode)
  536. goto out;
  537. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  538. if (rc)
  539. goto out;
  540. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  541. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  542. out:
  543. unlock_dir(lower_dir_dentry);
  544. if (!dentry->d_inode)
  545. d_drop(dentry);
  546. return rc;
  547. }
  548. static int
  549. ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  550. struct inode *new_dir, struct dentry *new_dentry)
  551. {
  552. int rc;
  553. struct dentry *lower_old_dentry;
  554. struct dentry *lower_new_dentry;
  555. struct dentry *lower_old_dir_dentry;
  556. struct dentry *lower_new_dir_dentry;
  557. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  558. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  559. dget(lower_old_dentry);
  560. dget(lower_new_dentry);
  561. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  562. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  563. lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  564. rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
  565. lower_new_dir_dentry->d_inode, lower_new_dentry);
  566. if (rc)
  567. goto out_lock;
  568. fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
  569. if (new_dir != old_dir)
  570. fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
  571. out_lock:
  572. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  573. dput(lower_new_dentry->d_parent);
  574. dput(lower_old_dentry->d_parent);
  575. dput(lower_new_dentry);
  576. dput(lower_old_dentry);
  577. return rc;
  578. }
  579. static int
  580. ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
  581. {
  582. int rc;
  583. struct dentry *lower_dentry;
  584. char *decoded_name;
  585. char *lower_buf;
  586. mm_segment_t old_fs;
  587. struct ecryptfs_crypt_stat *crypt_stat;
  588. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  589. if (!lower_dentry->d_inode->i_op ||
  590. !lower_dentry->d_inode->i_op->readlink) {
  591. rc = -EINVAL;
  592. goto out;
  593. }
  594. /* Released in this function */
  595. lower_buf = kmalloc(bufsiz, GFP_KERNEL);
  596. if (lower_buf == NULL) {
  597. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  598. rc = -ENOMEM;
  599. goto out;
  600. }
  601. old_fs = get_fs();
  602. set_fs(get_ds());
  603. ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
  604. "lower_dentry->d_name.name = [%s]\n",
  605. lower_dentry->d_name.name);
  606. rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
  607. (char __user *)lower_buf,
  608. bufsiz);
  609. set_fs(old_fs);
  610. if (rc >= 0) {
  611. crypt_stat = NULL;
  612. rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
  613. &decoded_name);
  614. if (rc == -ENOMEM)
  615. goto out_free_lower_buf;
  616. if (rc > 0) {
  617. ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
  618. "to userspace: [%*s]\n", rc,
  619. decoded_name);
  620. if (copy_to_user(buf, decoded_name, rc))
  621. rc = -EFAULT;
  622. }
  623. kfree(decoded_name);
  624. fsstack_copy_attr_atime(dentry->d_inode,
  625. lower_dentry->d_inode);
  626. }
  627. out_free_lower_buf:
  628. kfree(lower_buf);
  629. out:
  630. return rc;
  631. }
  632. static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  633. {
  634. char *buf;
  635. int len = PAGE_SIZE, rc;
  636. mm_segment_t old_fs;
  637. /* Released in ecryptfs_put_link(); only release here on error */
  638. buf = kmalloc(len, GFP_KERNEL);
  639. if (!buf) {
  640. rc = -ENOMEM;
  641. goto out;
  642. }
  643. old_fs = get_fs();
  644. set_fs(get_ds());
  645. ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
  646. "dentry->d_name.name = [%s]\n", dentry->d_name.name);
  647. rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
  648. buf[rc] = '\0';
  649. set_fs(old_fs);
  650. if (rc < 0)
  651. goto out_free;
  652. rc = 0;
  653. nd_set_link(nd, buf);
  654. goto out;
  655. out_free:
  656. kfree(buf);
  657. out:
  658. return ERR_PTR(rc);
  659. }
  660. static void
  661. ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
  662. {
  663. /* Free the char* */
  664. kfree(nd_get_link(nd));
  665. }
  666. /**
  667. * upper_size_to_lower_size
  668. * @crypt_stat: Crypt_stat associated with file
  669. * @upper_size: Size of the upper file
  670. *
  671. * Calculate the requried size of the lower file based on the
  672. * specified size of the upper file. This calculation is based on the
  673. * number of headers in the underlying file and the extent size.
  674. *
  675. * Returns Calculated size of the lower file.
  676. */
  677. static loff_t
  678. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  679. loff_t upper_size)
  680. {
  681. loff_t lower_size;
  682. lower_size = ( crypt_stat->header_extent_size
  683. * crypt_stat->num_header_extents_at_front );
  684. if (upper_size != 0) {
  685. loff_t num_extents;
  686. num_extents = upper_size >> crypt_stat->extent_shift;
  687. if (upper_size & ~crypt_stat->extent_mask)
  688. num_extents++;
  689. lower_size += (num_extents * crypt_stat->extent_size);
  690. }
  691. return lower_size;
  692. }
  693. /**
  694. * ecryptfs_truncate
  695. * @dentry: The ecryptfs layer dentry
  696. * @new_length: The length to expand the file to
  697. *
  698. * Function to handle truncations modifying the size of the file. Note
  699. * that the file sizes are interpolated. When expanding, we are simply
  700. * writing strings of 0's out. When truncating, we need to modify the
  701. * underlying file size according to the page index interpolations.
  702. *
  703. * Returns zero on success; non-zero otherwise
  704. */
  705. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  706. {
  707. int rc = 0;
  708. struct inode *inode = dentry->d_inode;
  709. struct dentry *lower_dentry;
  710. struct vfsmount *lower_mnt;
  711. struct file fake_ecryptfs_file, *lower_file = NULL;
  712. struct ecryptfs_crypt_stat *crypt_stat;
  713. loff_t i_size = i_size_read(inode);
  714. loff_t lower_size_before_truncate;
  715. loff_t lower_size_after_truncate;
  716. if (unlikely((new_length == i_size)))
  717. goto out;
  718. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  719. /* Set up a fake ecryptfs file, this is used to interface with
  720. * the file in the underlying filesystem so that the
  721. * truncation has an effect there as well. */
  722. memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
  723. fake_ecryptfs_file.f_path.dentry = dentry;
  724. /* Released at out_free: label */
  725. ecryptfs_set_file_private(&fake_ecryptfs_file,
  726. kmem_cache_alloc(ecryptfs_file_info_cache,
  727. GFP_KERNEL));
  728. if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
  729. rc = -ENOMEM;
  730. goto out;
  731. }
  732. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  733. /* This dget & mntget is released through fput at out_fput: */
  734. lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
  735. if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
  736. O_RDWR))) {
  737. ecryptfs_printk(KERN_ERR,
  738. "Error opening dentry; rc = [%i]\n", rc);
  739. goto out_free;
  740. }
  741. ecryptfs_set_file_lower(&fake_ecryptfs_file, lower_file);
  742. /* Switch on growing or shrinking file */
  743. if (new_length > i_size) {
  744. rc = ecryptfs_fill_zeros(&fake_ecryptfs_file, new_length);
  745. if (rc) {
  746. ecryptfs_printk(KERN_ERR,
  747. "Problem with fill_zeros\n");
  748. goto out_fput;
  749. }
  750. i_size_write(inode, new_length);
  751. rc = ecryptfs_write_inode_size_to_metadata(
  752. lower_file, lower_dentry->d_inode, inode, dentry,
  753. ECRYPTFS_LOWER_I_MUTEX_NOT_HELD);
  754. if (rc) {
  755. printk(KERN_ERR "Problem with "
  756. "ecryptfs_write_inode_size_to_metadata; "
  757. "rc = [%d]\n", rc);
  758. goto out_fput;
  759. }
  760. } else { /* new_length < i_size_read(inode) */
  761. vmtruncate(inode, new_length);
  762. rc = ecryptfs_write_inode_size_to_metadata(
  763. lower_file, lower_dentry->d_inode, inode, dentry,
  764. ECRYPTFS_LOWER_I_MUTEX_NOT_HELD);
  765. if (rc) {
  766. printk(KERN_ERR "Problem with "
  767. "ecryptfs_write_inode_size_to_metadata; "
  768. "rc = [%d]\n", rc);
  769. goto out_fput;
  770. }
  771. /* We are reducing the size of the ecryptfs file, and need to
  772. * know if we need to reduce the size of the lower file. */
  773. lower_size_before_truncate =
  774. upper_size_to_lower_size(crypt_stat, i_size);
  775. lower_size_after_truncate =
  776. upper_size_to_lower_size(crypt_stat, new_length);
  777. if (lower_size_after_truncate < lower_size_before_truncate)
  778. vmtruncate(lower_dentry->d_inode,
  779. lower_size_after_truncate);
  780. }
  781. /* Update the access times */
  782. lower_dentry->d_inode->i_mtime = lower_dentry->d_inode->i_ctime
  783. = CURRENT_TIME;
  784. mark_inode_dirty_sync(inode);
  785. out_fput:
  786. if ((rc = ecryptfs_close_lower_file(lower_file)))
  787. printk(KERN_ERR "Error closing lower_file\n");
  788. out_free:
  789. if (ecryptfs_file_to_private(&fake_ecryptfs_file))
  790. kmem_cache_free(ecryptfs_file_info_cache,
  791. ecryptfs_file_to_private(&fake_ecryptfs_file));
  792. out:
  793. return rc;
  794. }
  795. static int
  796. ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  797. {
  798. int rc;
  799. if (nd) {
  800. struct vfsmount *vfsmnt_save = nd->mnt;
  801. struct dentry *dentry_save = nd->dentry;
  802. nd->mnt = ecryptfs_dentry_to_lower_mnt(nd->dentry);
  803. nd->dentry = ecryptfs_dentry_to_lower(nd->dentry);
  804. rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
  805. nd->mnt = vfsmnt_save;
  806. nd->dentry = dentry_save;
  807. } else
  808. rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
  809. return rc;
  810. }
  811. /**
  812. * ecryptfs_setattr
  813. * @dentry: dentry handle to the inode to modify
  814. * @ia: Structure with flags of what to change and values
  815. *
  816. * Updates the metadata of an inode. If the update is to the size
  817. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  818. * of both the ecryptfs inode and the lower inode.
  819. *
  820. * All other metadata changes will be passed right to the lower filesystem,
  821. * and we will just update our inode to look like the lower.
  822. */
  823. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  824. {
  825. int rc = 0;
  826. struct dentry *lower_dentry;
  827. struct inode *inode;
  828. struct inode *lower_inode;
  829. struct ecryptfs_crypt_stat *crypt_stat;
  830. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  831. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  832. inode = dentry->d_inode;
  833. lower_inode = ecryptfs_inode_to_lower(inode);
  834. if (ia->ia_valid & ATTR_SIZE) {
  835. ecryptfs_printk(KERN_DEBUG,
  836. "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
  837. ia->ia_valid, ATTR_SIZE);
  838. rc = ecryptfs_truncate(dentry, ia->ia_size);
  839. /* ecryptfs_truncate handles resizing of the lower file */
  840. ia->ia_valid &= ~ATTR_SIZE;
  841. ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
  842. ia->ia_valid);
  843. if (rc < 0)
  844. goto out;
  845. }
  846. rc = notify_change(lower_dentry, ia);
  847. out:
  848. fsstack_copy_attr_all(inode, lower_inode, NULL);
  849. return rc;
  850. }
  851. int
  852. ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  853. size_t size, int flags)
  854. {
  855. int rc = 0;
  856. struct dentry *lower_dentry;
  857. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  858. if (!lower_dentry->d_inode->i_op->setxattr) {
  859. rc = -ENOSYS;
  860. goto out;
  861. }
  862. mutex_lock(&lower_dentry->d_inode->i_mutex);
  863. rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
  864. size, flags);
  865. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  866. out:
  867. return rc;
  868. }
  869. ssize_t
  870. ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
  871. size_t size)
  872. {
  873. int rc = 0;
  874. struct dentry *lower_dentry;
  875. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  876. if (!lower_dentry->d_inode->i_op->getxattr) {
  877. rc = -ENOSYS;
  878. goto out;
  879. }
  880. mutex_lock(&lower_dentry->d_inode->i_mutex);
  881. rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
  882. size);
  883. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  884. out:
  885. return rc;
  886. }
  887. static ssize_t
  888. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  889. {
  890. int rc = 0;
  891. struct dentry *lower_dentry;
  892. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  893. if (!lower_dentry->d_inode->i_op->listxattr) {
  894. rc = -ENOSYS;
  895. goto out;
  896. }
  897. mutex_lock(&lower_dentry->d_inode->i_mutex);
  898. rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
  899. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  900. out:
  901. return rc;
  902. }
  903. static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
  904. {
  905. int rc = 0;
  906. struct dentry *lower_dentry;
  907. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  908. if (!lower_dentry->d_inode->i_op->removexattr) {
  909. rc = -ENOSYS;
  910. goto out;
  911. }
  912. mutex_lock(&lower_dentry->d_inode->i_mutex);
  913. rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
  914. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  915. out:
  916. return rc;
  917. }
  918. int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
  919. {
  920. if ((ecryptfs_inode_to_lower(inode)
  921. == (struct inode *)candidate_lower_inode))
  922. return 1;
  923. else
  924. return 0;
  925. }
  926. int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
  927. {
  928. ecryptfs_init_inode(inode, (struct inode *)lower_inode);
  929. return 0;
  930. }
  931. const struct inode_operations ecryptfs_symlink_iops = {
  932. .readlink = ecryptfs_readlink,
  933. .follow_link = ecryptfs_follow_link,
  934. .put_link = ecryptfs_put_link,
  935. .permission = ecryptfs_permission,
  936. .setattr = ecryptfs_setattr,
  937. .setxattr = ecryptfs_setxattr,
  938. .getxattr = ecryptfs_getxattr,
  939. .listxattr = ecryptfs_listxattr,
  940. .removexattr = ecryptfs_removexattr
  941. };
  942. const struct inode_operations ecryptfs_dir_iops = {
  943. .create = ecryptfs_create,
  944. .lookup = ecryptfs_lookup,
  945. .link = ecryptfs_link,
  946. .unlink = ecryptfs_unlink,
  947. .symlink = ecryptfs_symlink,
  948. .mkdir = ecryptfs_mkdir,
  949. .rmdir = ecryptfs_rmdir,
  950. .mknod = ecryptfs_mknod,
  951. .rename = ecryptfs_rename,
  952. .permission = ecryptfs_permission,
  953. .setattr = ecryptfs_setattr,
  954. .setxattr = ecryptfs_setxattr,
  955. .getxattr = ecryptfs_getxattr,
  956. .listxattr = ecryptfs_listxattr,
  957. .removexattr = ecryptfs_removexattr
  958. };
  959. const struct inode_operations ecryptfs_main_iops = {
  960. .permission = ecryptfs_permission,
  961. .setattr = ecryptfs_setattr,
  962. .setxattr = ecryptfs_setxattr,
  963. .getxattr = ecryptfs_getxattr,
  964. .listxattr = ecryptfs_listxattr,
  965. .removexattr = ecryptfs_removexattr
  966. };