inode.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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 <linux/slab.h>
  34. #include <linux/xattr.h>
  35. #include <asm/unaligned.h>
  36. #include "ecryptfs_kernel.h"
  37. /* Do not directly use this function. Use ECRYPTFS_OVERRIDE_CRED() instead. */
  38. const struct cred * ecryptfs_override_fsids(uid_t fsuid, gid_t fsgid)
  39. {
  40. struct cred * cred;
  41. const struct cred * old_cred;
  42. cred = prepare_creds();
  43. if (!cred)
  44. return NULL;
  45. cred->fsuid = fsuid;
  46. cred->fsgid = fsgid;
  47. old_cred = override_creds(cred);
  48. return old_cred;
  49. }
  50. /* Do not directly use this function, use REVERT_CRED() instead. */
  51. void ecryptfs_revert_fsids(const struct cred * old_cred)
  52. {
  53. const struct cred * cur_cred;
  54. cur_cred = current->cred;
  55. revert_creds(old_cred);
  56. put_cred(cur_cred);
  57. }
  58. static struct dentry *lock_parent(struct dentry *dentry)
  59. {
  60. struct dentry *dir;
  61. dir = dget_parent(dentry);
  62. mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
  63. return dir;
  64. }
  65. static void unlock_dir(struct dentry *dir)
  66. {
  67. mutex_unlock(&dir->d_inode->i_mutex);
  68. dput(dir);
  69. }
  70. static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
  71. {
  72. if (ecryptfs_inode_to_lower(inode) == (struct inode *)lower_inode)
  73. return 1;
  74. return 0;
  75. }
  76. static int ecryptfs_inode_set(struct inode *inode, void *opaque)
  77. {
  78. struct inode *lower_inode = opaque;
  79. ecryptfs_set_inode_lower(inode, lower_inode);
  80. fsstack_copy_attr_all(inode, lower_inode);
  81. /* i_size will be overwritten for encrypted regular files */
  82. fsstack_copy_inode_size(inode, lower_inode);
  83. inode->i_ino = lower_inode->i_ino;
  84. inode->i_version++;
  85. inode->i_mapping->a_ops = &ecryptfs_aops;
  86. inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
  87. if (S_ISLNK(inode->i_mode))
  88. inode->i_op = &ecryptfs_symlink_iops;
  89. else if (S_ISDIR(inode->i_mode))
  90. inode->i_op = &ecryptfs_dir_iops;
  91. else
  92. inode->i_op = &ecryptfs_main_iops;
  93. if (S_ISDIR(inode->i_mode))
  94. inode->i_fop = &ecryptfs_dir_fops;
  95. else if (special_file(inode->i_mode))
  96. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  97. else
  98. inode->i_fop = &ecryptfs_main_fops;
  99. return 0;
  100. }
  101. static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
  102. struct super_block *sb)
  103. {
  104. struct inode *inode;
  105. if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
  106. return ERR_PTR(-EXDEV);
  107. if (!igrab(lower_inode))
  108. return ERR_PTR(-ESTALE);
  109. inode = iget5_locked(sb, (unsigned long)lower_inode,
  110. ecryptfs_inode_test, ecryptfs_inode_set,
  111. lower_inode);
  112. if (!inode) {
  113. iput(lower_inode);
  114. return ERR_PTR(-EACCES);
  115. }
  116. if (!(inode->i_state & I_NEW))
  117. iput(lower_inode);
  118. return inode;
  119. }
  120. struct inode *ecryptfs_get_inode(struct inode *lower_inode,
  121. struct super_block *sb)
  122. {
  123. struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
  124. if (!IS_ERR(inode) && (inode->i_state & I_NEW))
  125. unlock_new_inode(inode);
  126. return inode;
  127. }
  128. /**
  129. * ecryptfs_interpose
  130. * @lower_dentry: Existing dentry in the lower filesystem
  131. * @dentry: ecryptfs' dentry
  132. * @sb: ecryptfs's super_block
  133. *
  134. * Interposes upper and lower dentries.
  135. *
  136. * Returns zero on success; non-zero otherwise
  137. */
  138. static int ecryptfs_interpose(struct dentry *lower_dentry,
  139. struct dentry *dentry, struct super_block *sb)
  140. {
  141. struct inode *inode = ecryptfs_get_inode(lower_dentry->d_inode, sb);
  142. if (IS_ERR(inode))
  143. return PTR_ERR(inode);
  144. d_instantiate(dentry, inode);
  145. if(d_unhashed(dentry))
  146. d_rehash(dentry);
  147. return 0;
  148. }
  149. static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
  150. struct inode *inode)
  151. {
  152. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  153. struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
  154. struct dentry *lower_dir_dentry;
  155. int rc;
  156. dget(lower_dentry);
  157. lower_dir_dentry = lock_parent(lower_dentry);
  158. rc = vfs_unlink(lower_dir_inode, lower_dentry);
  159. if (rc) {
  160. printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
  161. goto out_unlock;
  162. }
  163. fsstack_copy_attr_times(dir, lower_dir_inode);
  164. set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
  165. inode->i_ctime = dir->i_ctime;
  166. d_drop(dentry);
  167. out_unlock:
  168. unlock_dir(lower_dir_dentry);
  169. dput(lower_dentry);
  170. return rc;
  171. }
  172. /**
  173. * ecryptfs_do_create
  174. * @directory_inode: inode of the new file's dentry's parent in ecryptfs
  175. * @ecryptfs_dentry: New file's dentry in ecryptfs
  176. * @mode: The mode of the new file
  177. * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
  178. *
  179. * Creates the underlying file and the eCryptfs inode which will link to
  180. * it. It will also update the eCryptfs directory inode to mimic the
  181. * stat of the lower directory inode.
  182. *
  183. * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
  184. */
  185. static struct inode *
  186. ecryptfs_do_create(struct inode *directory_inode,
  187. struct dentry *ecryptfs_dentry, umode_t mode)
  188. {
  189. int rc;
  190. struct dentry *lower_dentry;
  191. struct dentry *lower_dir_dentry;
  192. struct inode *inode;
  193. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  194. lower_dir_dentry = lock_parent(lower_dentry);
  195. if (IS_ERR(lower_dir_dentry)) {
  196. ecryptfs_printk(KERN_ERR, "Error locking directory of "
  197. "dentry\n");
  198. inode = ERR_CAST(lower_dir_dentry);
  199. goto out;
  200. }
  201. rc = vfs_create(lower_dir_dentry->d_inode, lower_dentry, mode, NULL);
  202. if (rc) {
  203. printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
  204. "rc = [%d]\n", __func__, rc);
  205. inode = ERR_PTR(rc);
  206. goto out_lock;
  207. }
  208. inode = __ecryptfs_get_inode(lower_dentry->d_inode,
  209. directory_inode->i_sb);
  210. if (IS_ERR(inode)) {
  211. vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
  212. goto out_lock;
  213. }
  214. fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
  215. fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
  216. out_lock:
  217. unlock_dir(lower_dir_dentry);
  218. out:
  219. return inode;
  220. }
  221. /**
  222. * ecryptfs_do_create2
  223. * @directory_inode: inode of the new file's dentry's parent in ecryptfs
  224. * @ecryptfs_dentry: New file's dentry in ecryptfs
  225. * @mode: The mode of the new file
  226. * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
  227. *
  228. * Creates the underlying file and the eCryptfs inode which will link to
  229. * it. It will also update the eCryptfs directory inode to mimic the
  230. * stat of the lower directory inode.
  231. *
  232. * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
  233. */
  234. static struct inode *
  235. ecryptfs_do_create2(struct inode *directory_inode,
  236. struct dentry *ecryptfs_dentry, umode_t mode, struct nameidata *nd)
  237. {
  238. int rc;
  239. struct dentry *lower_dentry;
  240. struct dentry *lower_dir_dentry;
  241. struct vfsmount *lower_mnt = NULL;
  242. struct inode *inode = NULL;
  243. struct dentry *dentry_save = NULL;
  244. struct vfsmount *vfsmount_save = NULL;
  245. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  246. lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
  247. if (!lower_dentry->d_op || !lower_dentry->d_op->d_revalidate)
  248. goto out;
  249. lower_dir_dentry = lock_parent(lower_dentry);
  250. if (IS_ERR(lower_dir_dentry)) {
  251. ecryptfs_printk(KERN_ERR, "Error locking directory of "
  252. "dentry\n");
  253. inode = ERR_CAST(lower_dir_dentry);
  254. goto out;
  255. }
  256. if (nd) {
  257. dentry_save = nd->path.dentry;
  258. vfsmount_save = nd->path.mnt;
  259. nd->path.dentry = lower_dentry;
  260. nd->path.mnt = lower_mnt;
  261. }
  262. rc = vfs_create(lower_dir_dentry->d_inode, lower_dentry, mode, nd);
  263. if (nd) {
  264. nd->path.dentry = dentry_save;
  265. nd->path.mnt = vfsmount_save;
  266. }
  267. if (rc) {
  268. printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
  269. "rc = [%d]\n", __func__, rc);
  270. inode = ERR_PTR(rc);
  271. goto out_lock;
  272. }
  273. inode = __ecryptfs_get_inode(lower_dentry->d_inode,
  274. directory_inode->i_sb);
  275. if (IS_ERR(inode)) {
  276. vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
  277. goto out_lock;
  278. }
  279. fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
  280. fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
  281. out_lock:
  282. unlock_dir(lower_dir_dentry);
  283. out:
  284. return inode;
  285. }
  286. /**
  287. * ecryptfs_initialize_file
  288. *
  289. * Cause the file to be changed from a basic empty file to an ecryptfs
  290. * file with a header and first data page.
  291. *
  292. * Returns zero on success
  293. */
  294. int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
  295. struct inode *ecryptfs_inode)
  296. {
  297. struct ecryptfs_crypt_stat *crypt_stat =
  298. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  299. int rc = 0;
  300. if (S_ISDIR(ecryptfs_inode->i_mode)) {
  301. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  302. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  303. goto out;
  304. }
  305. ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
  306. rc = ecryptfs_new_file_context(ecryptfs_inode);
  307. if (rc) {
  308. ecryptfs_printk(KERN_ERR, "Error creating new file "
  309. "context; rc = [%d]\n", rc);
  310. goto out;
  311. }
  312. rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
  313. if (rc) {
  314. printk(KERN_ERR "%s: Error attempting to initialize "
  315. "the lower file for the dentry with name "
  316. "[%s]; rc = [%d]\n", __func__,
  317. ecryptfs_dentry->d_name.name, rc);
  318. goto out;
  319. }
  320. #ifdef CONFIG_WTL_ENCRYPTION_FILTER
  321. mutex_lock(&crypt_stat->cs_mutex);
  322. if (crypt_stat->flags & ECRYPTFS_ENCRYPTED) {
  323. struct dentry *fp_dentry =
  324. ecryptfs_inode_to_private(ecryptfs_inode)
  325. ->lower_file->f_dentry;
  326. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  327. &ecryptfs_superblock_to_private(ecryptfs_dentry->d_sb)
  328. ->mount_crypt_stat;
  329. char filename[NAME_MAX+1] = {0};
  330. if (fp_dentry->d_name.len <= NAME_MAX)
  331. memcpy(filename, fp_dentry->d_name.name,
  332. fp_dentry->d_name.len + 1);
  333. if ((mount_crypt_stat->flags & ECRYPTFS_ENABLE_NEW_PASSTHROUGH)
  334. || ((mount_crypt_stat->flags & ECRYPTFS_ENABLE_FILTERING) &&
  335. (is_file_name_match(mount_crypt_stat, fp_dentry) ||
  336. is_file_ext_match(mount_crypt_stat, filename)))) {
  337. crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
  338. | ECRYPTFS_ENCRYPTED);
  339. ecryptfs_put_lower_file(ecryptfs_inode);
  340. } else {
  341. rc = ecryptfs_write_metadata(ecryptfs_dentry,
  342. ecryptfs_inode);
  343. if (rc)
  344. printk(
  345. KERN_ERR "Error writing headers; rc = [%d]\n"
  346. , rc);
  347. ecryptfs_put_lower_file(ecryptfs_inode);
  348. }
  349. }
  350. mutex_unlock(&crypt_stat->cs_mutex);
  351. #else
  352. rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
  353. if (rc)
  354. printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
  355. ecryptfs_put_lower_file(ecryptfs_inode);
  356. #endif
  357. out:
  358. return rc;
  359. }
  360. int ecryptfs_check_subfs(struct dentry *de, struct nameidata *nd, char *fs)
  361. {
  362. struct dentry *lower_dentry = NULL;
  363. lower_dentry = ecryptfs_dentry_to_lower(de);
  364. if (!lower_dentry->d_op || !lower_dentry->d_op->d_revalidate)
  365. {
  366. return -1;
  367. }
  368. if(!strcmp(lower_dentry->d_sb->s_type->name, fs))
  369. {
  370. return 1;
  371. }
  372. return 0;
  373. }
  374. /**
  375. * ecryptfs_create
  376. * @dir: The inode of the directory in which to create the file.
  377. * @dentry: The eCryptfs dentry
  378. * @mode: The mode of the new file.
  379. * @nd: nameidata
  380. *
  381. * Creates a new file.
  382. *
  383. * Returns zero on success; non-zero on error condition
  384. */
  385. static int
  386. ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
  387. umode_t mode, struct nameidata *nd)
  388. {
  389. struct inode *ecryptfs_inode;
  390. int rc;
  391. if(ecryptfs_check_subfs(ecryptfs_dentry, nd, "sdcardfs") == 1)
  392. ecryptfs_inode = ecryptfs_do_create2(directory_inode, ecryptfs_dentry,
  393. mode, nd);
  394. else
  395. ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode);
  396. if (unlikely(IS_ERR(ecryptfs_inode))) {
  397. ecryptfs_printk(KERN_WARNING, "Failed to create file in"
  398. "lower filesystem\n");
  399. rc = PTR_ERR(ecryptfs_inode);
  400. goto out;
  401. }
  402. /* At this point, a file exists on "disk"; we need to make sure
  403. * that this on disk file is prepared to be an ecryptfs file */
  404. rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
  405. if (rc) {
  406. ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
  407. ecryptfs_inode);
  408. make_bad_inode(ecryptfs_inode);
  409. unlock_new_inode(ecryptfs_inode);
  410. iput(ecryptfs_inode);
  411. goto out;
  412. }
  413. d_instantiate(ecryptfs_dentry, ecryptfs_inode);
  414. if(d_unhashed(ecryptfs_dentry))
  415. d_rehash(ecryptfs_dentry);
  416. unlock_new_inode(ecryptfs_inode);
  417. out:
  418. return rc;
  419. }
  420. static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
  421. {
  422. struct ecryptfs_crypt_stat *crypt_stat;
  423. int rc;
  424. rc = ecryptfs_get_lower_file(dentry, inode);
  425. if (rc) {
  426. printk(KERN_ERR "%s: Error attempting to initialize "
  427. "the lower file for the dentry with name "
  428. "[%s]; rc = [%d]\n", __func__,
  429. dentry->d_name.name, rc);
  430. return rc;
  431. }
  432. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  433. /* TODO: lock for crypt_stat comparison */
  434. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
  435. ecryptfs_set_default_sizes(crypt_stat);
  436. rc = ecryptfs_read_and_validate_header_region(inode);
  437. ecryptfs_put_lower_file(inode);
  438. if (rc) {
  439. rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
  440. if (!rc)
  441. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  442. }
  443. /* Must return 0 to allow non-eCryptfs files to be looked up, too */
  444. return 0;
  445. }
  446. /**
  447. * ecryptfs_lookup_interpose - Dentry interposition for a lookup
  448. */
  449. static int ecryptfs_lookup_interpose(struct dentry *dentry,
  450. struct dentry *lower_dentry,
  451. struct inode *dir_inode)
  452. {
  453. struct inode *inode, *lower_inode = lower_dentry->d_inode;
  454. struct ecryptfs_dentry_info *dentry_info;
  455. struct vfsmount *lower_mnt;
  456. int rc = 0;
  457. lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
  458. fsstack_copy_attr_atime(dir_inode, lower_dentry->d_parent->d_inode);
  459. BUG_ON(!lower_dentry->d_count);
  460. dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
  461. ecryptfs_set_dentry_private(dentry, dentry_info);
  462. if (!dentry_info) {
  463. printk(KERN_ERR "%s: Out of memory whilst attempting "
  464. "to allocate ecryptfs_dentry_info struct\n",
  465. __func__);
  466. dput(lower_dentry);
  467. mntput(lower_mnt);
  468. d_drop(dentry);
  469. return -ENOMEM;
  470. }
  471. ecryptfs_set_dentry_lower(dentry, lower_dentry);
  472. ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
  473. if (!lower_dentry->d_inode) {
  474. return 0;
  475. }
  476. inode = __ecryptfs_get_inode(lower_inode, dir_inode->i_sb);
  477. if (IS_ERR(inode)) {
  478. printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
  479. __func__, PTR_ERR(inode));
  480. return PTR_ERR(inode);
  481. }
  482. if (S_ISREG(inode->i_mode)) {
  483. rc = ecryptfs_i_size_read(dentry, inode);
  484. if (rc) {
  485. make_bad_inode(inode);
  486. return rc;
  487. }
  488. }
  489. if (inode->i_state & I_NEW)
  490. unlock_new_inode(inode);
  491. d_add(dentry, inode);
  492. return rc;
  493. }
  494. /**
  495. * ecryptfs_lookup
  496. * @ecryptfs_dir_inode: The eCryptfs directory inode
  497. * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
  498. * @ecryptfs_nd: nameidata; may be NULL
  499. *
  500. * Find a file on disk. If the file does not exist, then we'll add it to the
  501. * dentry cache and continue on to read it from the disk.
  502. */
  503. static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
  504. struct dentry *ecryptfs_dentry,
  505. struct nameidata *ecryptfs_nd)
  506. {
  507. char *encrypted_and_encoded_name = NULL;
  508. size_t encrypted_and_encoded_name_size;
  509. struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
  510. struct dentry *lower_dir_dentry, *lower_dentry;
  511. int rc = 0;
  512. if ((ecryptfs_dentry->d_name.len == 1
  513. && !strcmp(ecryptfs_dentry->d_name.name, "."))
  514. || (ecryptfs_dentry->d_name.len == 2
  515. && !strcmp(ecryptfs_dentry->d_name.name, ".."))) {
  516. goto out_d_drop;
  517. }
  518. lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
  519. mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
  520. lower_dentry = lookup_one_len(ecryptfs_dentry->d_name.name,
  521. lower_dir_dentry,
  522. ecryptfs_dentry->d_name.len);
  523. mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
  524. if (IS_ERR(lower_dentry)) {
  525. rc = PTR_ERR(lower_dentry);
  526. ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
  527. "[%d] on lower_dentry = [%s]\n", __func__, rc,
  528. encrypted_and_encoded_name);
  529. goto out_d_drop;
  530. }
  531. if (lower_dentry->d_inode)
  532. goto interpose;
  533. mount_crypt_stat = &ecryptfs_superblock_to_private(
  534. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  535. if (!(mount_crypt_stat
  536. && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)))
  537. goto interpose;
  538. dput(lower_dentry);
  539. rc = ecryptfs_encrypt_and_encode_filename(
  540. &encrypted_and_encoded_name, &encrypted_and_encoded_name_size,
  541. NULL, mount_crypt_stat, ecryptfs_dentry->d_name.name,
  542. ecryptfs_dentry->d_name.len);
  543. if (rc) {
  544. printk(KERN_ERR "%s: Error attempting to encrypt and encode "
  545. "filename; rc = [%d]\n", __func__, rc);
  546. goto out_d_drop;
  547. }
  548. mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
  549. lower_dentry = lookup_one_len(encrypted_and_encoded_name,
  550. lower_dir_dentry,
  551. encrypted_and_encoded_name_size);
  552. mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
  553. if (IS_ERR(lower_dentry)) {
  554. rc = PTR_ERR(lower_dentry);
  555. ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
  556. "[%d] on lower_dentry = [%s]\n", __func__, rc,
  557. encrypted_and_encoded_name);
  558. goto out_d_drop;
  559. }
  560. interpose:
  561. rc = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry,
  562. ecryptfs_dir_inode);
  563. goto out;
  564. out_d_drop:
  565. d_drop(ecryptfs_dentry);
  566. out:
  567. kfree(encrypted_and_encoded_name);
  568. return ERR_PTR(rc);
  569. }
  570. static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
  571. struct dentry *new_dentry)
  572. {
  573. struct dentry *lower_old_dentry;
  574. struct dentry *lower_new_dentry;
  575. struct dentry *lower_dir_dentry;
  576. u64 file_size_save;
  577. int rc;
  578. file_size_save = i_size_read(old_dentry->d_inode);
  579. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  580. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  581. dget(lower_old_dentry);
  582. dget(lower_new_dentry);
  583. lower_dir_dentry = lock_parent(lower_new_dentry);
  584. rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
  585. lower_new_dentry);
  586. if (rc || !lower_new_dentry->d_inode)
  587. goto out_lock;
  588. rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
  589. if (rc)
  590. goto out_lock;
  591. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  592. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  593. set_nlink(old_dentry->d_inode,
  594. ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink);
  595. i_size_write(new_dentry->d_inode, file_size_save);
  596. out_lock:
  597. unlock_dir(lower_dir_dentry);
  598. dput(lower_new_dentry);
  599. dput(lower_old_dentry);
  600. return rc;
  601. }
  602. static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
  603. {
  604. return ecryptfs_do_unlink(dir, dentry, dentry->d_inode);
  605. }
  606. static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
  607. const char *symname)
  608. {
  609. int rc;
  610. struct dentry *lower_dentry;
  611. struct dentry *lower_dir_dentry;
  612. char *encoded_symname;
  613. size_t encoded_symlen;
  614. struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
  615. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  616. dget(lower_dentry);
  617. lower_dir_dentry = lock_parent(lower_dentry);
  618. mount_crypt_stat = &ecryptfs_superblock_to_private(
  619. dir->i_sb)->mount_crypt_stat;
  620. rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
  621. &encoded_symlen,
  622. NULL,
  623. mount_crypt_stat, symname,
  624. strlen(symname));
  625. if (rc)
  626. goto out_lock;
  627. rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
  628. encoded_symname);
  629. kfree(encoded_symname);
  630. if (rc || !lower_dentry->d_inode)
  631. goto out_lock;
  632. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  633. if (rc)
  634. goto out_lock;
  635. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  636. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  637. out_lock:
  638. unlock_dir(lower_dir_dentry);
  639. dput(lower_dentry);
  640. if (!dentry->d_inode)
  641. d_drop(dentry);
  642. return rc;
  643. }
  644. static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  645. {
  646. int rc;
  647. struct dentry *lower_dentry;
  648. struct dentry *lower_dir_dentry;
  649. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  650. lower_dir_dentry = lock_parent(lower_dentry);
  651. rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
  652. if (rc || !lower_dentry->d_inode)
  653. goto out;
  654. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  655. if (rc)
  656. goto out;
  657. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  658. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  659. set_nlink(dir, lower_dir_dentry->d_inode->i_nlink);
  660. out:
  661. unlock_dir(lower_dir_dentry);
  662. if (!dentry->d_inode)
  663. d_drop(dentry);
  664. return rc;
  665. }
  666. static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
  667. {
  668. struct dentry *lower_dentry;
  669. struct dentry *lower_dir_dentry;
  670. int rc;
  671. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  672. dget(dentry);
  673. lower_dir_dentry = lock_parent(lower_dentry);
  674. dget(lower_dentry);
  675. rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
  676. dput(lower_dentry);
  677. if (!rc && dentry->d_inode)
  678. clear_nlink(dentry->d_inode);
  679. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  680. set_nlink(dir, lower_dir_dentry->d_inode->i_nlink);
  681. unlock_dir(lower_dir_dentry);
  682. if (!rc)
  683. d_drop(dentry);
  684. dput(dentry);
  685. return rc;
  686. }
  687. static int
  688. ecryptfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  689. {
  690. int rc;
  691. struct dentry *lower_dentry;
  692. struct dentry *lower_dir_dentry;
  693. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  694. lower_dir_dentry = lock_parent(lower_dentry);
  695. rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
  696. if (rc || !lower_dentry->d_inode)
  697. goto out;
  698. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  699. if (rc)
  700. goto out;
  701. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  702. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  703. out:
  704. unlock_dir(lower_dir_dentry);
  705. if (!dentry->d_inode)
  706. d_drop(dentry);
  707. return rc;
  708. }
  709. static int
  710. ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  711. struct inode *new_dir, struct dentry *new_dentry)
  712. {
  713. int rc;
  714. struct dentry *lower_old_dentry;
  715. struct dentry *lower_new_dentry;
  716. struct dentry *lower_old_dir_dentry;
  717. struct dentry *lower_new_dir_dentry;
  718. struct dentry *trap = NULL;
  719. struct inode *target_inode;
  720. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  721. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  722. dget(lower_old_dentry);
  723. dget(lower_new_dentry);
  724. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  725. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  726. target_inode = new_dentry->d_inode;
  727. trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  728. /* source should not be ancestor of target */
  729. if (trap == lower_old_dentry) {
  730. rc = -EINVAL;
  731. goto out_lock;
  732. }
  733. /* target should not be ancestor of source */
  734. if (trap == lower_new_dentry) {
  735. rc = -ENOTEMPTY;
  736. goto out_lock;
  737. }
  738. rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
  739. lower_new_dir_dentry->d_inode, lower_new_dentry);
  740. if (rc)
  741. goto out_lock;
  742. if (target_inode)
  743. fsstack_copy_attr_all(target_inode,
  744. ecryptfs_inode_to_lower(target_inode));
  745. fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
  746. if (new_dir != old_dir)
  747. fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
  748. out_lock:
  749. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  750. dput(lower_new_dir_dentry);
  751. dput(lower_old_dir_dentry);
  752. dput(lower_new_dentry);
  753. dput(lower_old_dentry);
  754. return rc;
  755. }
  756. static int ecryptfs_readlink_lower(struct dentry *dentry, char **buf,
  757. size_t *bufsiz)
  758. {
  759. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  760. char *lower_buf;
  761. mm_segment_t old_fs;
  762. int rc;
  763. lower_buf = kmalloc(PATH_MAX, GFP_KERNEL);
  764. if (!lower_buf) {
  765. rc = -ENOMEM;
  766. goto out;
  767. }
  768. old_fs = get_fs();
  769. set_fs(get_ds());
  770. rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
  771. (char __user *)lower_buf,
  772. PATH_MAX);
  773. set_fs(old_fs);
  774. if (rc < 0)
  775. goto out;
  776. rc = ecryptfs_decode_and_decrypt_filename(buf, bufsiz, dentry,
  777. lower_buf, rc);
  778. out:
  779. kfree(lower_buf);
  780. return rc;
  781. }
  782. static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  783. {
  784. char *buf;
  785. size_t len = PATH_MAX;
  786. int rc;
  787. rc = ecryptfs_readlink_lower(dentry, &buf, &len);
  788. if (rc)
  789. goto out;
  790. fsstack_copy_attr_atime(dentry->d_inode,
  791. ecryptfs_dentry_to_lower(dentry)->d_inode);
  792. buf[len] = '\0';
  793. out:
  794. nd_set_link(nd, buf);
  795. return NULL;
  796. }
  797. static void
  798. ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
  799. {
  800. char *buf = nd_get_link(nd);
  801. if (!IS_ERR(buf)) {
  802. /* Free the char* */
  803. kfree(buf);
  804. }
  805. }
  806. /**
  807. * upper_size_to_lower_size
  808. * @crypt_stat: Crypt_stat associated with file
  809. * @upper_size: Size of the upper file
  810. *
  811. * Calculate the required size of the lower file based on the
  812. * specified size of the upper file. This calculation is based on the
  813. * number of headers in the underlying file and the extent size.
  814. *
  815. * Returns Calculated size of the lower file.
  816. */
  817. static loff_t
  818. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  819. loff_t upper_size)
  820. {
  821. loff_t lower_size;
  822. lower_size = ecryptfs_lower_header_size(crypt_stat);
  823. if (upper_size != 0) {
  824. loff_t num_extents;
  825. num_extents = upper_size >> crypt_stat->extent_shift;
  826. if (upper_size & ~crypt_stat->extent_mask)
  827. num_extents++;
  828. lower_size += (num_extents * crypt_stat->extent_size);
  829. }
  830. return lower_size;
  831. }
  832. /**
  833. * truncate_upper
  834. * @dentry: The ecryptfs layer dentry
  835. * @ia: Address of the ecryptfs inode's attributes
  836. * @lower_ia: Address of the lower inode's attributes
  837. *
  838. * Function to handle truncations modifying the size of the file. Note
  839. * that the file sizes are interpolated. When expanding, we are simply
  840. * writing strings of 0's out. When truncating, we truncate the upper
  841. * inode and update the lower_ia according to the page index
  842. * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
  843. * the caller must use lower_ia in a call to notify_change() to perform
  844. * the truncation of the lower inode.
  845. *
  846. * Returns zero on success; non-zero otherwise
  847. */
  848. static int truncate_upper(struct dentry *dentry, struct iattr *ia,
  849. struct iattr *lower_ia)
  850. {
  851. int rc = 0;
  852. struct inode *inode = dentry->d_inode;
  853. struct ecryptfs_crypt_stat *crypt_stat;
  854. loff_t i_size = i_size_read(inode);
  855. loff_t lower_size_before_truncate;
  856. loff_t lower_size_after_truncate;
  857. if (unlikely((ia->ia_size == i_size))) {
  858. lower_ia->ia_valid &= ~ATTR_SIZE;
  859. return 0;
  860. }
  861. rc = ecryptfs_get_lower_file(dentry, inode);
  862. if (rc)
  863. return rc;
  864. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  865. /* Switch on growing or shrinking file */
  866. if (ia->ia_size > i_size) {
  867. char zero[] = { 0x00 };
  868. lower_ia->ia_valid &= ~ATTR_SIZE;
  869. /* Write a single 0 at the last position of the file;
  870. * this triggers code that will fill in 0's throughout
  871. * the intermediate portion of the previous end of the
  872. * file and the new and of the file */
  873. rc = ecryptfs_write(inode, zero,
  874. (ia->ia_size - 1), 1);
  875. } else { /* ia->ia_size < i_size_read(inode) */
  876. /* We're chopping off all the pages down to the page
  877. * in which ia->ia_size is located. Fill in the end of
  878. * that page from (ia->ia_size & ~PAGE_CACHE_MASK) to
  879. * PAGE_CACHE_SIZE with zeros. */
  880. size_t num_zeros = (PAGE_CACHE_SIZE
  881. - (ia->ia_size & ~PAGE_CACHE_MASK));
  882. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  883. truncate_setsize(inode, ia->ia_size);
  884. lower_ia->ia_size = ia->ia_size;
  885. lower_ia->ia_valid |= ATTR_SIZE;
  886. goto out;
  887. }
  888. if (num_zeros) {
  889. char *zeros_virt;
  890. zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
  891. if (!zeros_virt) {
  892. rc = -ENOMEM;
  893. goto out;
  894. }
  895. rc = ecryptfs_write(inode, zeros_virt,
  896. ia->ia_size, num_zeros);
  897. kfree(zeros_virt);
  898. if (rc) {
  899. printk(KERN_ERR "Error attempting to zero out "
  900. "the remainder of the end page on "
  901. "reducing truncate; rc = [%d]\n", rc);
  902. goto out;
  903. }
  904. }
  905. truncate_setsize(inode, ia->ia_size);
  906. rc = ecryptfs_write_inode_size_to_metadata(inode);
  907. if (rc) {
  908. printk(KERN_ERR "Problem with "
  909. "ecryptfs_write_inode_size_to_metadata; "
  910. "rc = [%d]\n", rc);
  911. goto out;
  912. }
  913. /* We are reducing the size of the ecryptfs file, and need to
  914. * know if we need to reduce the size of the lower file. */
  915. lower_size_before_truncate =
  916. upper_size_to_lower_size(crypt_stat, i_size);
  917. lower_size_after_truncate =
  918. upper_size_to_lower_size(crypt_stat, ia->ia_size);
  919. if (lower_size_after_truncate < lower_size_before_truncate) {
  920. lower_ia->ia_size = lower_size_after_truncate;
  921. lower_ia->ia_valid |= ATTR_SIZE;
  922. } else
  923. lower_ia->ia_valid &= ~ATTR_SIZE;
  924. }
  925. out:
  926. ecryptfs_put_lower_file(inode);
  927. return rc;
  928. }
  929. static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
  930. {
  931. struct ecryptfs_crypt_stat *crypt_stat;
  932. loff_t lower_oldsize, lower_newsize;
  933. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  934. lower_oldsize = upper_size_to_lower_size(crypt_stat,
  935. i_size_read(inode));
  936. lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
  937. if (lower_newsize > lower_oldsize) {
  938. /*
  939. * The eCryptfs inode and the new *lower* size are mixed here
  940. * because we may not have the lower i_mutex held and/or it may
  941. * not be appropriate to call inode_newsize_ok() with inodes
  942. * from other filesystems.
  943. */
  944. return inode_newsize_ok(inode, lower_newsize);
  945. }
  946. return 0;
  947. }
  948. /**
  949. * ecryptfs_truncate
  950. * @dentry: The ecryptfs layer dentry
  951. * @new_length: The length to expand the file to
  952. *
  953. * Simple function that handles the truncation of an eCryptfs inode and
  954. * its corresponding lower inode.
  955. *
  956. * Returns zero on success; non-zero otherwise
  957. */
  958. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  959. {
  960. struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
  961. struct iattr lower_ia = { .ia_valid = 0 };
  962. int rc;
  963. rc = ecryptfs_inode_newsize_ok(dentry->d_inode, new_length);
  964. if (rc)
  965. return rc;
  966. rc = truncate_upper(dentry, &ia, &lower_ia);
  967. if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
  968. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  969. mutex_lock(&lower_dentry->d_inode->i_mutex);
  970. rc = notify_change(lower_dentry, &lower_ia);
  971. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  972. }
  973. return rc;
  974. }
  975. static int
  976. ecryptfs_permission(struct inode *inode, int mask)
  977. {
  978. return inode_permission(ecryptfs_inode_to_lower(inode), mask);
  979. }
  980. /**
  981. * ecryptfs_setattr
  982. * @dentry: dentry handle to the inode to modify
  983. * @ia: Structure with flags of what to change and values
  984. *
  985. * Updates the metadata of an inode. If the update is to the size
  986. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  987. * of both the ecryptfs inode and the lower inode.
  988. *
  989. * All other metadata changes will be passed right to the lower filesystem,
  990. * and we will just update our inode to look like the lower.
  991. */
  992. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  993. {
  994. int rc = 0;
  995. struct dentry *lower_dentry;
  996. struct iattr lower_ia;
  997. struct inode *inode;
  998. struct inode *lower_inode;
  999. struct ecryptfs_crypt_stat *crypt_stat;
  1000. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  1001. if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
  1002. ecryptfs_init_crypt_stat(crypt_stat);
  1003. inode = dentry->d_inode;
  1004. lower_inode = ecryptfs_inode_to_lower(inode);
  1005. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  1006. mutex_lock(&crypt_stat->cs_mutex);
  1007. if (S_ISDIR(dentry->d_inode->i_mode))
  1008. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  1009. else if (S_ISREG(dentry->d_inode->i_mode)
  1010. && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  1011. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
  1012. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  1013. mount_crypt_stat = &ecryptfs_superblock_to_private(
  1014. dentry->d_sb)->mount_crypt_stat;
  1015. rc = ecryptfs_get_lower_file(dentry, inode);
  1016. if (rc) {
  1017. mutex_unlock(&crypt_stat->cs_mutex);
  1018. goto out;
  1019. }
  1020. rc = ecryptfs_read_metadata(dentry);
  1021. ecryptfs_put_lower_file(inode);
  1022. if (rc) {
  1023. if (!(mount_crypt_stat->flags
  1024. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  1025. rc = -EIO;
  1026. printk(KERN_WARNING "Either the lower file "
  1027. "is not in a valid eCryptfs format, "
  1028. "or the key could not be retrieved. "
  1029. "Plaintext passthrough mode is not "
  1030. "enabled; returning -EIO\n");
  1031. mutex_unlock(&crypt_stat->cs_mutex);
  1032. goto out;
  1033. }
  1034. rc = 0;
  1035. crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
  1036. | ECRYPTFS_ENCRYPTED);
  1037. }
  1038. }
  1039. mutex_unlock(&crypt_stat->cs_mutex);
  1040. rc = inode_change_ok(inode, ia);
  1041. if (rc)
  1042. goto out;
  1043. if (ia->ia_valid & ATTR_SIZE) {
  1044. rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
  1045. if (rc)
  1046. goto out;
  1047. }
  1048. memcpy(&lower_ia, ia, sizeof(lower_ia));
  1049. if (ia->ia_valid & ATTR_FILE)
  1050. lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
  1051. if (ia->ia_valid & ATTR_SIZE) {
  1052. rc = truncate_upper(dentry, ia, &lower_ia);
  1053. if (rc < 0)
  1054. goto out;
  1055. }
  1056. /*
  1057. * mode change is for clearing setuid/setgid bits. Allow lower fs
  1058. * to interpret this in its own way.
  1059. */
  1060. if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  1061. lower_ia.ia_valid &= ~ATTR_MODE;
  1062. mutex_lock(&lower_dentry->d_inode->i_mutex);
  1063. rc = notify_change(lower_dentry, &lower_ia);
  1064. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  1065. out:
  1066. fsstack_copy_attr_all(inode, lower_inode);
  1067. return rc;
  1068. }
  1069. int ecryptfs_getattr_link(struct vfsmount *mnt, struct dentry *dentry,
  1070. struct kstat *stat)
  1071. {
  1072. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  1073. int rc = 0;
  1074. mount_crypt_stat = &ecryptfs_superblock_to_private(
  1075. dentry->d_sb)->mount_crypt_stat;
  1076. generic_fillattr(dentry->d_inode, stat);
  1077. if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
  1078. char *target;
  1079. size_t targetsiz;
  1080. rc = ecryptfs_readlink_lower(dentry, &target, &targetsiz);
  1081. if (!rc) {
  1082. kfree(target);
  1083. stat->size = targetsiz;
  1084. }
  1085. }
  1086. return rc;
  1087. }
  1088. int ecryptfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1089. struct kstat *stat)
  1090. {
  1091. struct kstat lower_stat;
  1092. int rc;
  1093. rc = vfs_getattr(ecryptfs_dentry_to_lower_mnt(dentry),
  1094. ecryptfs_dentry_to_lower(dentry), &lower_stat);
  1095. if (!rc) {
  1096. fsstack_copy_attr_all(dentry->d_inode,
  1097. ecryptfs_inode_to_lower(dentry->d_inode));
  1098. generic_fillattr(dentry->d_inode, stat);
  1099. stat->blocks = lower_stat.blocks;
  1100. }
  1101. return rc;
  1102. }
  1103. int
  1104. ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  1105. size_t size, int flags)
  1106. {
  1107. int rc = 0;
  1108. struct dentry *lower_dentry;
  1109. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  1110. if (!lower_dentry->d_inode->i_op->setxattr) {
  1111. rc = -EOPNOTSUPP;
  1112. goto out;
  1113. }
  1114. rc = vfs_setxattr(lower_dentry, name, value, size, flags);
  1115. if (!rc && dentry->d_inode)
  1116. fsstack_copy_attr_all(dentry->d_inode, lower_dentry->d_inode);
  1117. out:
  1118. return rc;
  1119. }
  1120. ssize_t
  1121. ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
  1122. void *value, size_t size)
  1123. {
  1124. int rc = 0;
  1125. if (!lower_dentry->d_inode->i_op->getxattr) {
  1126. #ifndef ECRYPT_FS_VIRTUAL_FAT_XATTR
  1127. rc = -EOPNOTSUPP;
  1128. #endif
  1129. goto out;
  1130. }
  1131. mutex_lock(&lower_dentry->d_inode->i_mutex);
  1132. rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
  1133. size);
  1134. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  1135. out:
  1136. return rc;
  1137. }
  1138. static ssize_t
  1139. ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
  1140. size_t size)
  1141. {
  1142. return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
  1143. value, size);
  1144. }
  1145. static ssize_t
  1146. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  1147. {
  1148. int rc = 0;
  1149. struct dentry *lower_dentry;
  1150. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  1151. if (!lower_dentry->d_inode->i_op->listxattr) {
  1152. rc = -EOPNOTSUPP;
  1153. goto out;
  1154. }
  1155. mutex_lock(&lower_dentry->d_inode->i_mutex);
  1156. rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
  1157. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  1158. out:
  1159. return rc;
  1160. }
  1161. static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
  1162. {
  1163. int rc = 0;
  1164. struct dentry *lower_dentry;
  1165. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  1166. if (!lower_dentry->d_inode->i_op->removexattr) {
  1167. rc = -EOPNOTSUPP;
  1168. goto out;
  1169. }
  1170. mutex_lock(&lower_dentry->d_inode->i_mutex);
  1171. rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
  1172. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  1173. out:
  1174. return rc;
  1175. }
  1176. const struct inode_operations ecryptfs_symlink_iops = {
  1177. .readlink = generic_readlink,
  1178. .follow_link = ecryptfs_follow_link,
  1179. .put_link = ecryptfs_put_link,
  1180. .permission = ecryptfs_permission,
  1181. .setattr = ecryptfs_setattr,
  1182. .getattr = ecryptfs_getattr_link,
  1183. .setxattr = ecryptfs_setxattr,
  1184. .getxattr = ecryptfs_getxattr,
  1185. .listxattr = ecryptfs_listxattr,
  1186. .removexattr = ecryptfs_removexattr
  1187. };
  1188. const struct inode_operations ecryptfs_dir_iops = {
  1189. .create = ecryptfs_create,
  1190. .lookup = ecryptfs_lookup,
  1191. .link = ecryptfs_link,
  1192. .unlink = ecryptfs_unlink,
  1193. .symlink = ecryptfs_symlink,
  1194. .mkdir = ecryptfs_mkdir,
  1195. .rmdir = ecryptfs_rmdir,
  1196. .mknod = ecryptfs_mknod,
  1197. .rename = ecryptfs_rename,
  1198. .permission = ecryptfs_permission,
  1199. .setattr = ecryptfs_setattr,
  1200. .setxattr = ecryptfs_setxattr,
  1201. .getxattr = ecryptfs_getxattr,
  1202. .listxattr = ecryptfs_listxattr,
  1203. .removexattr = ecryptfs_removexattr
  1204. };
  1205. const struct inode_operations ecryptfs_main_iops = {
  1206. .permission = ecryptfs_permission,
  1207. .setattr = ecryptfs_setattr,
  1208. .getattr = ecryptfs_getattr,
  1209. .setxattr = ecryptfs_setxattr,
  1210. .getxattr = ecryptfs_getxattr,
  1211. .listxattr = ecryptfs_listxattr,
  1212. .removexattr = ecryptfs_removexattr
  1213. };