inode.c 34 KB

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