inode.c 29 KB

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