inode.c 33 KB

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