inode.c 29 KB

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