inode.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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(&(dir->d_inode->i_mutex));
  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. ecryptfs_write_inode_size_to_metadata(lower_file, lower_inode, inode,
  161. 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. #if BITS_PER_LONG != 32
  191. lower_flags |= O_LARGEFILE;
  192. #endif
  193. lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
  194. /* Corresponding fput() at end of this function */
  195. if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
  196. lower_flags))) {
  197. ecryptfs_printk(KERN_ERR,
  198. "Error opening dentry; rc = [%i]\n", rc);
  199. goto out;
  200. }
  201. lower_inode = lower_dentry->d_inode;
  202. if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
  203. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  204. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  205. goto out_fput;
  206. }
  207. crypt_stat->flags |= ECRYPTFS_NEW_FILE;
  208. ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
  209. rc = ecryptfs_new_file_context(ecryptfs_dentry);
  210. if (rc) {
  211. ecryptfs_printk(KERN_DEBUG, "Error creating new file "
  212. "context\n");
  213. goto out_fput;
  214. }
  215. rc = ecryptfs_write_metadata(ecryptfs_dentry, lower_file);
  216. if (rc) {
  217. ecryptfs_printk(KERN_DEBUG, "Error writing headers\n");
  218. goto out_fput;
  219. }
  220. rc = grow_file(ecryptfs_dentry, lower_file, inode, lower_inode);
  221. out_fput:
  222. if ((rc = ecryptfs_close_lower_file(lower_file)))
  223. printk(KERN_ERR "Error closing lower_file\n");
  224. out:
  225. return rc;
  226. }
  227. /**
  228. * ecryptfs_create
  229. * @dir: The inode of the directory in which to create the file.
  230. * @dentry: The eCryptfs dentry
  231. * @mode: The mode of the new file.
  232. * @nd: nameidata
  233. *
  234. * Creates a new file.
  235. *
  236. * Returns zero on success; non-zero on error condition
  237. */
  238. static int
  239. ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
  240. int mode, struct nameidata *nd)
  241. {
  242. int rc;
  243. rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
  244. if (unlikely(rc)) {
  245. ecryptfs_printk(KERN_WARNING, "Failed to create file in"
  246. "lower filesystem\n");
  247. goto out;
  248. }
  249. /* At this point, a file exists on "disk"; we need to make sure
  250. * that this on disk file is prepared to be an ecryptfs file */
  251. rc = ecryptfs_initialize_file(ecryptfs_dentry);
  252. out:
  253. return rc;
  254. }
  255. /**
  256. * ecryptfs_lookup
  257. * @dir: inode
  258. * @dentry: The dentry
  259. * @nd: nameidata, may be NULL
  260. *
  261. * Find a file on disk. If the file does not exist, then we'll add it to the
  262. * dentry cache and continue on to read it from the disk.
  263. */
  264. static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
  265. struct nameidata *nd)
  266. {
  267. int rc = 0;
  268. struct dentry *lower_dir_dentry;
  269. struct dentry *lower_dentry;
  270. struct vfsmount *lower_mnt;
  271. char *encoded_name;
  272. unsigned int encoded_namelen;
  273. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  274. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  275. char *page_virt = NULL;
  276. struct inode *lower_inode;
  277. u64 file_size;
  278. lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
  279. dentry->d_op = &ecryptfs_dops;
  280. if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, "."))
  281. || (dentry->d_name.len == 2
  282. && !strcmp(dentry->d_name.name, ".."))) {
  283. d_drop(dentry);
  284. goto out;
  285. }
  286. encoded_namelen = ecryptfs_encode_filename(crypt_stat,
  287. dentry->d_name.name,
  288. dentry->d_name.len,
  289. &encoded_name);
  290. if (encoded_namelen < 0) {
  291. rc = encoded_namelen;
  292. d_drop(dentry);
  293. goto out;
  294. }
  295. ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen "
  296. "= [%d]\n", encoded_name, encoded_namelen);
  297. lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry,
  298. encoded_namelen - 1);
  299. kfree(encoded_name);
  300. if (IS_ERR(lower_dentry)) {
  301. ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
  302. rc = PTR_ERR(lower_dentry);
  303. d_drop(dentry);
  304. goto out;
  305. }
  306. lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
  307. ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->"
  308. "d_name.name = [%s]\n", lower_dentry,
  309. lower_dentry->d_name.name);
  310. lower_inode = lower_dentry->d_inode;
  311. fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode);
  312. BUG_ON(!atomic_read(&lower_dentry->d_count));
  313. ecryptfs_set_dentry_private(dentry,
  314. kmem_cache_alloc(ecryptfs_dentry_info_cache,
  315. GFP_KERNEL));
  316. if (!ecryptfs_dentry_to_private(dentry)) {
  317. rc = -ENOMEM;
  318. ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting "
  319. "to allocate ecryptfs_dentry_info struct\n");
  320. goto out_dput;
  321. }
  322. ecryptfs_set_dentry_lower(dentry, lower_dentry);
  323. ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
  324. if (!lower_dentry->d_inode) {
  325. /* We want to add because we couldn't find in lower */
  326. d_add(dentry, NULL);
  327. goto out;
  328. }
  329. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 1);
  330. if (rc) {
  331. ecryptfs_printk(KERN_ERR, "Error interposing\n");
  332. goto out_dput;
  333. }
  334. if (S_ISDIR(lower_inode->i_mode)) {
  335. ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
  336. goto out;
  337. }
  338. if (S_ISLNK(lower_inode->i_mode)) {
  339. ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
  340. goto out;
  341. }
  342. if (!nd) {
  343. ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave"
  344. "as we *think* we are about to unlink\n");
  345. goto out;
  346. }
  347. /* Released in this function */
  348. page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2,
  349. GFP_USER);
  350. if (!page_virt) {
  351. rc = -ENOMEM;
  352. ecryptfs_printk(KERN_ERR,
  353. "Cannot ecryptfs_kmalloc a page\n");
  354. goto out_dput;
  355. }
  356. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  357. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
  358. ecryptfs_set_default_sizes(crypt_stat);
  359. rc = ecryptfs_read_and_validate_header_region(page_virt, lower_dentry,
  360. nd->mnt);
  361. if (rc) {
  362. rc = ecryptfs_read_and_validate_xattr_region(page_virt, dentry);
  363. if (rc) {
  364. printk(KERN_DEBUG "Valid metadata not found in header "
  365. "region or xattr region; treating file as "
  366. "unencrypted\n");
  367. rc = 0;
  368. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  369. goto out;
  370. }
  371. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  372. }
  373. mount_crypt_stat = &ecryptfs_superblock_to_private(
  374. dentry->d_sb)->mount_crypt_stat;
  375. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
  376. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  377. file_size = (crypt_stat->header_extent_size
  378. + i_size_read(lower_dentry->d_inode));
  379. else
  380. file_size = i_size_read(lower_dentry->d_inode);
  381. } else {
  382. memcpy(&file_size, page_virt, sizeof(file_size));
  383. file_size = be64_to_cpu(file_size);
  384. }
  385. i_size_write(dentry->d_inode, (loff_t)file_size);
  386. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  387. goto out;
  388. out_dput:
  389. dput(lower_dentry);
  390. d_drop(dentry);
  391. out:
  392. return ERR_PTR(rc);
  393. }
  394. static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
  395. struct dentry *new_dentry)
  396. {
  397. struct dentry *lower_old_dentry;
  398. struct dentry *lower_new_dentry;
  399. struct dentry *lower_dir_dentry;
  400. u64 file_size_save;
  401. int rc;
  402. file_size_save = i_size_read(old_dentry->d_inode);
  403. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  404. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  405. dget(lower_old_dentry);
  406. dget(lower_new_dentry);
  407. lower_dir_dentry = lock_parent(lower_new_dentry);
  408. rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
  409. lower_new_dentry);
  410. if (rc || !lower_new_dentry->d_inode)
  411. goto out_lock;
  412. rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
  413. if (rc)
  414. goto out_lock;
  415. fsstack_copy_attr_times(dir, lower_new_dentry->d_inode);
  416. fsstack_copy_inode_size(dir, lower_new_dentry->d_inode);
  417. old_dentry->d_inode->i_nlink =
  418. ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
  419. i_size_write(new_dentry->d_inode, file_size_save);
  420. out_lock:
  421. unlock_dir(lower_dir_dentry);
  422. dput(lower_new_dentry);
  423. dput(lower_old_dentry);
  424. d_drop(lower_old_dentry);
  425. d_drop(new_dentry);
  426. d_drop(old_dentry);
  427. return rc;
  428. }
  429. static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
  430. {
  431. int rc = 0;
  432. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  433. struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
  434. lock_parent(lower_dentry);
  435. rc = vfs_unlink(lower_dir_inode, lower_dentry);
  436. if (rc) {
  437. printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
  438. goto out_unlock;
  439. }
  440. fsstack_copy_attr_times(dir, lower_dir_inode);
  441. dentry->d_inode->i_nlink =
  442. ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
  443. dentry->d_inode->i_ctime = dir->i_ctime;
  444. out_unlock:
  445. unlock_parent(lower_dentry);
  446. return rc;
  447. }
  448. static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
  449. const char *symname)
  450. {
  451. int rc;
  452. struct dentry *lower_dentry;
  453. struct dentry *lower_dir_dentry;
  454. umode_t mode;
  455. char *encoded_symname;
  456. unsigned int encoded_symlen;
  457. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  458. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  459. dget(lower_dentry);
  460. lower_dir_dentry = lock_parent(lower_dentry);
  461. mode = S_IALLUGO;
  462. encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
  463. strlen(symname),
  464. &encoded_symname);
  465. if (encoded_symlen < 0) {
  466. rc = encoded_symlen;
  467. goto out_lock;
  468. }
  469. rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
  470. encoded_symname, mode);
  471. kfree(encoded_symname);
  472. if (rc || !lower_dentry->d_inode)
  473. goto out_lock;
  474. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  475. if (rc)
  476. goto out_lock;
  477. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  478. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  479. out_lock:
  480. unlock_dir(lower_dir_dentry);
  481. dput(lower_dentry);
  482. if (!dentry->d_inode)
  483. d_drop(dentry);
  484. return rc;
  485. }
  486. static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  487. {
  488. int rc;
  489. struct dentry *lower_dentry;
  490. struct dentry *lower_dir_dentry;
  491. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  492. lower_dir_dentry = lock_parent(lower_dentry);
  493. rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
  494. if (rc || !lower_dentry->d_inode)
  495. goto out;
  496. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  497. if (rc)
  498. goto out;
  499. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  500. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  501. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  502. out:
  503. unlock_dir(lower_dir_dentry);
  504. if (!dentry->d_inode)
  505. d_drop(dentry);
  506. return rc;
  507. }
  508. static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
  509. {
  510. struct dentry *lower_dentry;
  511. struct dentry *lower_dir_dentry;
  512. int rc;
  513. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  514. dget(dentry);
  515. lower_dir_dentry = lock_parent(lower_dentry);
  516. dget(lower_dentry);
  517. rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
  518. dput(lower_dentry);
  519. if (!rc)
  520. d_delete(lower_dentry);
  521. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  522. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  523. unlock_dir(lower_dir_dentry);
  524. if (!rc)
  525. d_drop(dentry);
  526. dput(dentry);
  527. return rc;
  528. }
  529. static int
  530. ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  531. {
  532. int rc;
  533. struct dentry *lower_dentry;
  534. struct dentry *lower_dir_dentry;
  535. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  536. lower_dir_dentry = lock_parent(lower_dentry);
  537. rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
  538. if (rc || !lower_dentry->d_inode)
  539. goto out;
  540. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  541. if (rc)
  542. goto out;
  543. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  544. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  545. out:
  546. unlock_dir(lower_dir_dentry);
  547. if (!dentry->d_inode)
  548. d_drop(dentry);
  549. return rc;
  550. }
  551. static int
  552. ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  553. struct inode *new_dir, struct dentry *new_dentry)
  554. {
  555. int rc;
  556. struct dentry *lower_old_dentry;
  557. struct dentry *lower_new_dentry;
  558. struct dentry *lower_old_dir_dentry;
  559. struct dentry *lower_new_dir_dentry;
  560. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  561. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  562. dget(lower_old_dentry);
  563. dget(lower_new_dentry);
  564. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  565. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  566. lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  567. rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
  568. lower_new_dir_dentry->d_inode, lower_new_dentry);
  569. if (rc)
  570. goto out_lock;
  571. fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
  572. if (new_dir != old_dir)
  573. fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
  574. out_lock:
  575. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  576. dput(lower_new_dentry->d_parent);
  577. dput(lower_old_dentry->d_parent);
  578. dput(lower_new_dentry);
  579. dput(lower_old_dentry);
  580. return rc;
  581. }
  582. static int
  583. ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
  584. {
  585. int rc;
  586. struct dentry *lower_dentry;
  587. char *decoded_name;
  588. char *lower_buf;
  589. mm_segment_t old_fs;
  590. struct ecryptfs_crypt_stat *crypt_stat;
  591. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  592. if (!lower_dentry->d_inode->i_op ||
  593. !lower_dentry->d_inode->i_op->readlink) {
  594. rc = -EINVAL;
  595. goto out;
  596. }
  597. /* Released in this function */
  598. lower_buf = kmalloc(bufsiz, GFP_KERNEL);
  599. if (lower_buf == NULL) {
  600. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  601. rc = -ENOMEM;
  602. goto out;
  603. }
  604. old_fs = get_fs();
  605. set_fs(get_ds());
  606. ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
  607. "lower_dentry->d_name.name = [%s]\n",
  608. lower_dentry->d_name.name);
  609. rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
  610. (char __user *)lower_buf,
  611. bufsiz);
  612. set_fs(old_fs);
  613. if (rc >= 0) {
  614. crypt_stat = NULL;
  615. rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
  616. &decoded_name);
  617. if (rc == -ENOMEM)
  618. goto out_free_lower_buf;
  619. if (rc > 0) {
  620. ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
  621. "to userspace: [%*s]\n", rc,
  622. decoded_name);
  623. if (copy_to_user(buf, decoded_name, rc))
  624. rc = -EFAULT;
  625. }
  626. kfree(decoded_name);
  627. fsstack_copy_attr_atime(dentry->d_inode,
  628. lower_dentry->d_inode);
  629. }
  630. out_free_lower_buf:
  631. kfree(lower_buf);
  632. out:
  633. return rc;
  634. }
  635. static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  636. {
  637. char *buf;
  638. int len = PAGE_SIZE, rc;
  639. mm_segment_t old_fs;
  640. /* Released in ecryptfs_put_link(); only release here on error */
  641. buf = kmalloc(len, GFP_KERNEL);
  642. if (!buf) {
  643. rc = -ENOMEM;
  644. goto out;
  645. }
  646. old_fs = get_fs();
  647. set_fs(get_ds());
  648. ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
  649. "dentry->d_name.name = [%s]\n", dentry->d_name.name);
  650. rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
  651. buf[rc] = '\0';
  652. set_fs(old_fs);
  653. if (rc < 0)
  654. goto out_free;
  655. rc = 0;
  656. nd_set_link(nd, buf);
  657. goto out;
  658. out_free:
  659. kfree(buf);
  660. out:
  661. return ERR_PTR(rc);
  662. }
  663. static void
  664. ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
  665. {
  666. /* Free the char* */
  667. kfree(nd_get_link(nd));
  668. }
  669. /**
  670. * upper_size_to_lower_size
  671. * @crypt_stat: Crypt_stat associated with file
  672. * @upper_size: Size of the upper file
  673. *
  674. * Calculate the requried size of the lower file based on the
  675. * specified size of the upper file. This calculation is based on the
  676. * number of headers in the underlying file and the extent size.
  677. *
  678. * Returns Calculated size of the lower file.
  679. */
  680. static loff_t
  681. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  682. loff_t upper_size)
  683. {
  684. loff_t lower_size;
  685. lower_size = ( crypt_stat->header_extent_size
  686. * crypt_stat->num_header_extents_at_front );
  687. if (upper_size != 0) {
  688. loff_t num_extents;
  689. num_extents = upper_size >> crypt_stat->extent_shift;
  690. if (upper_size & ~crypt_stat->extent_mask)
  691. num_extents++;
  692. lower_size += (num_extents * crypt_stat->extent_size);
  693. }
  694. return lower_size;
  695. }
  696. /**
  697. * ecryptfs_truncate
  698. * @dentry: The ecryptfs layer dentry
  699. * @new_length: The length to expand the file to
  700. *
  701. * Function to handle truncations modifying the size of the file. Note
  702. * that the file sizes are interpolated. When expanding, we are simply
  703. * writing strings of 0's out. When truncating, we need to modify the
  704. * underlying file size according to the page index interpolations.
  705. *
  706. * Returns zero on success; non-zero otherwise
  707. */
  708. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  709. {
  710. int rc = 0;
  711. struct inode *inode = dentry->d_inode;
  712. struct dentry *lower_dentry;
  713. struct vfsmount *lower_mnt;
  714. struct file fake_ecryptfs_file, *lower_file = NULL;
  715. struct ecryptfs_crypt_stat *crypt_stat;
  716. loff_t i_size = i_size_read(inode);
  717. loff_t lower_size_before_truncate;
  718. loff_t lower_size_after_truncate;
  719. if (unlikely((new_length == i_size)))
  720. goto out;
  721. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  722. /* Set up a fake ecryptfs file, this is used to interface with
  723. * the file in the underlying filesystem so that the
  724. * truncation has an effect there as well. */
  725. memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
  726. fake_ecryptfs_file.f_path.dentry = dentry;
  727. /* Released at out_free: label */
  728. ecryptfs_set_file_private(&fake_ecryptfs_file,
  729. kmem_cache_alloc(ecryptfs_file_info_cache,
  730. GFP_KERNEL));
  731. if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
  732. rc = -ENOMEM;
  733. goto out;
  734. }
  735. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  736. /* This dget & mntget is released through fput at out_fput: */
  737. lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
  738. if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
  739. O_RDWR))) {
  740. ecryptfs_printk(KERN_ERR,
  741. "Error opening dentry; rc = [%i]\n", rc);
  742. goto out_free;
  743. }
  744. ecryptfs_set_file_lower(&fake_ecryptfs_file, lower_file);
  745. /* Switch on growing or shrinking file */
  746. if (new_length > i_size) {
  747. rc = ecryptfs_fill_zeros(&fake_ecryptfs_file, new_length);
  748. if (rc) {
  749. ecryptfs_printk(KERN_ERR,
  750. "Problem with fill_zeros\n");
  751. goto out_fput;
  752. }
  753. i_size_write(inode, new_length);
  754. rc = ecryptfs_write_inode_size_to_metadata(
  755. lower_file, lower_dentry->d_inode, inode, dentry,
  756. ECRYPTFS_LOWER_I_MUTEX_NOT_HELD);
  757. if (rc) {
  758. printk(KERN_ERR "Problem with "
  759. "ecryptfs_write_inode_size_to_metadata; "
  760. "rc = [%d]\n", rc);
  761. goto out_fput;
  762. }
  763. } else { /* new_length < i_size_read(inode) */
  764. vmtruncate(inode, new_length);
  765. rc = ecryptfs_write_inode_size_to_metadata(
  766. lower_file, lower_dentry->d_inode, inode, dentry,
  767. ECRYPTFS_LOWER_I_MUTEX_NOT_HELD);
  768. if (rc) {
  769. printk(KERN_ERR "Problem with "
  770. "ecryptfs_write_inode_size_to_metadata; "
  771. "rc = [%d]\n", rc);
  772. goto out_fput;
  773. }
  774. /* We are reducing the size of the ecryptfs file, and need to
  775. * know if we need to reduce the size of the lower file. */
  776. lower_size_before_truncate =
  777. upper_size_to_lower_size(crypt_stat, i_size);
  778. lower_size_after_truncate =
  779. upper_size_to_lower_size(crypt_stat, new_length);
  780. if (lower_size_after_truncate < lower_size_before_truncate)
  781. vmtruncate(lower_dentry->d_inode,
  782. lower_size_after_truncate);
  783. }
  784. /* Update the access times */
  785. lower_dentry->d_inode->i_mtime = lower_dentry->d_inode->i_ctime
  786. = CURRENT_TIME;
  787. mark_inode_dirty_sync(inode);
  788. out_fput:
  789. if ((rc = ecryptfs_close_lower_file(lower_file)))
  790. printk(KERN_ERR "Error closing lower_file\n");
  791. out_free:
  792. if (ecryptfs_file_to_private(&fake_ecryptfs_file))
  793. kmem_cache_free(ecryptfs_file_info_cache,
  794. ecryptfs_file_to_private(&fake_ecryptfs_file));
  795. out:
  796. return rc;
  797. }
  798. static int
  799. ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  800. {
  801. int rc;
  802. if (nd) {
  803. struct vfsmount *vfsmnt_save = nd->mnt;
  804. struct dentry *dentry_save = nd->dentry;
  805. nd->mnt = ecryptfs_dentry_to_lower_mnt(nd->dentry);
  806. nd->dentry = ecryptfs_dentry_to_lower(nd->dentry);
  807. rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
  808. nd->mnt = vfsmnt_save;
  809. nd->dentry = dentry_save;
  810. } else
  811. rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
  812. return rc;
  813. }
  814. /**
  815. * ecryptfs_setattr
  816. * @dentry: dentry handle to the inode to modify
  817. * @ia: Structure with flags of what to change and values
  818. *
  819. * Updates the metadata of an inode. If the update is to the size
  820. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  821. * of both the ecryptfs inode and the lower inode.
  822. *
  823. * All other metadata changes will be passed right to the lower filesystem,
  824. * and we will just update our inode to look like the lower.
  825. */
  826. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  827. {
  828. int rc = 0;
  829. struct dentry *lower_dentry;
  830. struct inode *inode;
  831. struct inode *lower_inode;
  832. struct ecryptfs_crypt_stat *crypt_stat;
  833. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  834. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  835. inode = dentry->d_inode;
  836. lower_inode = ecryptfs_inode_to_lower(inode);
  837. if (ia->ia_valid & ATTR_SIZE) {
  838. ecryptfs_printk(KERN_DEBUG,
  839. "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
  840. ia->ia_valid, ATTR_SIZE);
  841. rc = ecryptfs_truncate(dentry, ia->ia_size);
  842. /* ecryptfs_truncate handles resizing of the lower file */
  843. ia->ia_valid &= ~ATTR_SIZE;
  844. ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
  845. ia->ia_valid);
  846. if (rc < 0)
  847. goto out;
  848. }
  849. rc = notify_change(lower_dentry, ia);
  850. out:
  851. fsstack_copy_attr_all(inode, lower_inode, NULL);
  852. return rc;
  853. }
  854. int
  855. ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  856. size_t size, int flags)
  857. {
  858. int rc = 0;
  859. struct dentry *lower_dentry;
  860. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  861. if (!lower_dentry->d_inode->i_op->setxattr) {
  862. rc = -ENOSYS;
  863. goto out;
  864. }
  865. mutex_lock(&lower_dentry->d_inode->i_mutex);
  866. rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
  867. size, flags);
  868. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  869. out:
  870. return rc;
  871. }
  872. ssize_t
  873. ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
  874. size_t size)
  875. {
  876. int rc = 0;
  877. struct dentry *lower_dentry;
  878. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  879. if (!lower_dentry->d_inode->i_op->getxattr) {
  880. rc = -ENOSYS;
  881. goto out;
  882. }
  883. mutex_lock(&lower_dentry->d_inode->i_mutex);
  884. rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
  885. size);
  886. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  887. out:
  888. return rc;
  889. }
  890. static ssize_t
  891. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  892. {
  893. int rc = 0;
  894. struct dentry *lower_dentry;
  895. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  896. if (!lower_dentry->d_inode->i_op->listxattr) {
  897. rc = -ENOSYS;
  898. goto out;
  899. }
  900. mutex_lock(&lower_dentry->d_inode->i_mutex);
  901. rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
  902. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  903. out:
  904. return rc;
  905. }
  906. static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
  907. {
  908. int rc = 0;
  909. struct dentry *lower_dentry;
  910. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  911. if (!lower_dentry->d_inode->i_op->removexattr) {
  912. rc = -ENOSYS;
  913. goto out;
  914. }
  915. mutex_lock(&lower_dentry->d_inode->i_mutex);
  916. rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
  917. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  918. out:
  919. return rc;
  920. }
  921. int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
  922. {
  923. if ((ecryptfs_inode_to_lower(inode)
  924. == (struct inode *)candidate_lower_inode))
  925. return 1;
  926. else
  927. return 0;
  928. }
  929. int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
  930. {
  931. ecryptfs_init_inode(inode, (struct inode *)lower_inode);
  932. return 0;
  933. }
  934. const struct inode_operations ecryptfs_symlink_iops = {
  935. .readlink = ecryptfs_readlink,
  936. .follow_link = ecryptfs_follow_link,
  937. .put_link = ecryptfs_put_link,
  938. .permission = ecryptfs_permission,
  939. .setattr = ecryptfs_setattr,
  940. .setxattr = ecryptfs_setxattr,
  941. .getxattr = ecryptfs_getxattr,
  942. .listxattr = ecryptfs_listxattr,
  943. .removexattr = ecryptfs_removexattr
  944. };
  945. const struct inode_operations ecryptfs_dir_iops = {
  946. .create = ecryptfs_create,
  947. .lookup = ecryptfs_lookup,
  948. .link = ecryptfs_link,
  949. .unlink = ecryptfs_unlink,
  950. .symlink = ecryptfs_symlink,
  951. .mkdir = ecryptfs_mkdir,
  952. .rmdir = ecryptfs_rmdir,
  953. .mknod = ecryptfs_mknod,
  954. .rename = ecryptfs_rename,
  955. .permission = ecryptfs_permission,
  956. .setattr = ecryptfs_setattr,
  957. .setxattr = ecryptfs_setxattr,
  958. .getxattr = ecryptfs_getxattr,
  959. .listxattr = ecryptfs_listxattr,
  960. .removexattr = ecryptfs_removexattr
  961. };
  962. const struct inode_operations ecryptfs_main_iops = {
  963. .permission = ecryptfs_permission,
  964. .setattr = ecryptfs_setattr,
  965. .setxattr = ecryptfs_setxattr,
  966. .getxattr = ecryptfs_getxattr,
  967. .listxattr = ecryptfs_listxattr,
  968. .removexattr = ecryptfs_removexattr
  969. };