inode.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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. /**
  283. * ecryptfs_lookup_interpose - Dentry interposition for a lookup
  284. */
  285. static int ecryptfs_lookup_interpose(struct dentry *ecryptfs_dentry,
  286. struct dentry *lower_dentry,
  287. struct inode *ecryptfs_dir_inode)
  288. {
  289. struct dentry *lower_dir_dentry;
  290. struct vfsmount *lower_mnt;
  291. struct inode *inode, *lower_inode;
  292. struct ecryptfs_crypt_stat *crypt_stat;
  293. char *page_virt = NULL;
  294. int put_lower = 0, rc = 0;
  295. lower_dir_dentry = lower_dentry->d_parent;
  296. lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(
  297. ecryptfs_dentry->d_parent));
  298. lower_inode = lower_dentry->d_inode;
  299. fsstack_copy_attr_atime(ecryptfs_dir_inode, lower_dir_dentry->d_inode);
  300. BUG_ON(!lower_dentry->d_count);
  301. ecryptfs_set_dentry_private(ecryptfs_dentry,
  302. kmem_cache_alloc(ecryptfs_dentry_info_cache,
  303. GFP_KERNEL));
  304. if (!ecryptfs_dentry_to_private(ecryptfs_dentry)) {
  305. rc = -ENOMEM;
  306. printk(KERN_ERR "%s: Out of memory whilst attempting "
  307. "to allocate ecryptfs_dentry_info struct\n",
  308. __func__);
  309. goto out_put;
  310. }
  311. ecryptfs_set_dentry_lower(ecryptfs_dentry, lower_dentry);
  312. ecryptfs_set_dentry_lower_mnt(ecryptfs_dentry, lower_mnt);
  313. if (!lower_dentry->d_inode) {
  314. /* We want to add because we couldn't find in lower */
  315. d_add(ecryptfs_dentry, NULL);
  316. goto out;
  317. }
  318. inode = __ecryptfs_get_inode(lower_inode, ecryptfs_dir_inode->i_sb);
  319. if (IS_ERR(inode)) {
  320. rc = PTR_ERR(inode);
  321. printk(KERN_ERR "%s: Error interposing; rc = [%d]\n",
  322. __func__, rc);
  323. goto out;
  324. }
  325. if (!S_ISREG(inode->i_mode)) {
  326. if (inode->i_state & I_NEW)
  327. unlock_new_inode(inode);
  328. d_add(ecryptfs_dentry, inode);
  329. goto out;
  330. }
  331. /* Released in this function */
  332. page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2, GFP_USER);
  333. if (!page_virt) {
  334. printk(KERN_ERR "%s: Cannot kmem_cache_zalloc() a page\n",
  335. __func__);
  336. rc = -ENOMEM;
  337. make_bad_inode(inode);
  338. goto out;
  339. }
  340. rc = ecryptfs_get_lower_file(ecryptfs_dentry, inode);
  341. if (rc) {
  342. printk(KERN_ERR "%s: Error attempting to initialize "
  343. "the lower file for the dentry with name "
  344. "[%s]; rc = [%d]\n", __func__,
  345. ecryptfs_dentry->d_name.name, rc);
  346. make_bad_inode(inode);
  347. goto out_free_kmem;
  348. }
  349. put_lower = 1;
  350. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  351. /* TODO: lock for crypt_stat comparison */
  352. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
  353. ecryptfs_set_default_sizes(crypt_stat);
  354. rc = ecryptfs_read_and_validate_header_region(page_virt, inode);
  355. if (rc) {
  356. memset(page_virt, 0, PAGE_CACHE_SIZE);
  357. rc = ecryptfs_read_and_validate_xattr_region(page_virt,
  358. inode);
  359. if (rc) {
  360. rc = 0;
  361. goto unlock_inode;
  362. }
  363. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  364. }
  365. ecryptfs_i_size_init(page_virt, inode);
  366. unlock_inode:
  367. if (inode->i_state & I_NEW)
  368. unlock_new_inode(inode);
  369. d_add(ecryptfs_dentry, inode);
  370. out_free_kmem:
  371. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  372. goto out;
  373. out_put:
  374. dput(lower_dentry);
  375. mntput(lower_mnt);
  376. d_drop(ecryptfs_dentry);
  377. out:
  378. if (put_lower)
  379. ecryptfs_put_lower_file(inode);
  380. return rc;
  381. }
  382. /**
  383. * ecryptfs_lookup
  384. * @ecryptfs_dir_inode: The eCryptfs directory inode
  385. * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
  386. * @ecryptfs_nd: nameidata; may be NULL
  387. *
  388. * Find a file on disk. If the file does not exist, then we'll add it to the
  389. * dentry cache and continue on to read it from the disk.
  390. */
  391. static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
  392. struct dentry *ecryptfs_dentry,
  393. struct nameidata *ecryptfs_nd)
  394. {
  395. char *encrypted_and_encoded_name = NULL;
  396. size_t encrypted_and_encoded_name_size;
  397. struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
  398. struct dentry *lower_dir_dentry, *lower_dentry;
  399. int rc = 0;
  400. if ((ecryptfs_dentry->d_name.len == 1
  401. && !strcmp(ecryptfs_dentry->d_name.name, "."))
  402. || (ecryptfs_dentry->d_name.len == 2
  403. && !strcmp(ecryptfs_dentry->d_name.name, ".."))) {
  404. goto out_d_drop;
  405. }
  406. lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
  407. mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
  408. lower_dentry = lookup_one_len(ecryptfs_dentry->d_name.name,
  409. lower_dir_dentry,
  410. ecryptfs_dentry->d_name.len);
  411. mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
  412. if (IS_ERR(lower_dentry)) {
  413. rc = PTR_ERR(lower_dentry);
  414. ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
  415. "[%d] on lower_dentry = [%s]\n", __func__, rc,
  416. encrypted_and_encoded_name);
  417. goto out_d_drop;
  418. }
  419. if (lower_dentry->d_inode)
  420. goto interpose;
  421. mount_crypt_stat = &ecryptfs_superblock_to_private(
  422. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  423. if (!(mount_crypt_stat
  424. && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)))
  425. goto interpose;
  426. dput(lower_dentry);
  427. rc = ecryptfs_encrypt_and_encode_filename(
  428. &encrypted_and_encoded_name, &encrypted_and_encoded_name_size,
  429. NULL, mount_crypt_stat, ecryptfs_dentry->d_name.name,
  430. ecryptfs_dentry->d_name.len);
  431. if (rc) {
  432. printk(KERN_ERR "%s: Error attempting to encrypt and encode "
  433. "filename; rc = [%d]\n", __func__, rc);
  434. goto out_d_drop;
  435. }
  436. mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
  437. lower_dentry = lookup_one_len(encrypted_and_encoded_name,
  438. lower_dir_dentry,
  439. encrypted_and_encoded_name_size);
  440. mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
  441. if (IS_ERR(lower_dentry)) {
  442. rc = PTR_ERR(lower_dentry);
  443. ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
  444. "[%d] on lower_dentry = [%s]\n", __func__, rc,
  445. encrypted_and_encoded_name);
  446. goto out_d_drop;
  447. }
  448. interpose:
  449. rc = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry,
  450. ecryptfs_dir_inode);
  451. goto out;
  452. out_d_drop:
  453. d_drop(ecryptfs_dentry);
  454. out:
  455. kfree(encrypted_and_encoded_name);
  456. return ERR_PTR(rc);
  457. }
  458. static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
  459. struct dentry *new_dentry)
  460. {
  461. struct dentry *lower_old_dentry;
  462. struct dentry *lower_new_dentry;
  463. struct dentry *lower_dir_dentry;
  464. u64 file_size_save;
  465. int rc;
  466. file_size_save = i_size_read(old_dentry->d_inode);
  467. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  468. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  469. dget(lower_old_dentry);
  470. dget(lower_new_dentry);
  471. lower_dir_dentry = lock_parent(lower_new_dentry);
  472. rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
  473. lower_new_dentry);
  474. if (rc || !lower_new_dentry->d_inode)
  475. goto out_lock;
  476. rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
  477. if (rc)
  478. goto out_lock;
  479. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  480. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  481. old_dentry->d_inode->i_nlink =
  482. ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
  483. i_size_write(new_dentry->d_inode, file_size_save);
  484. out_lock:
  485. unlock_dir(lower_dir_dentry);
  486. dput(lower_new_dentry);
  487. dput(lower_old_dentry);
  488. return rc;
  489. }
  490. static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
  491. {
  492. int rc = 0;
  493. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  494. struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
  495. struct dentry *lower_dir_dentry;
  496. dget(lower_dentry);
  497. lower_dir_dentry = lock_parent(lower_dentry);
  498. rc = vfs_unlink(lower_dir_inode, lower_dentry);
  499. if (rc) {
  500. printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
  501. goto out_unlock;
  502. }
  503. fsstack_copy_attr_times(dir, lower_dir_inode);
  504. dentry->d_inode->i_nlink =
  505. ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
  506. dentry->d_inode->i_ctime = dir->i_ctime;
  507. d_drop(dentry);
  508. out_unlock:
  509. unlock_dir(lower_dir_dentry);
  510. dput(lower_dentry);
  511. return rc;
  512. }
  513. static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
  514. const char *symname)
  515. {
  516. int rc;
  517. struct dentry *lower_dentry;
  518. struct dentry *lower_dir_dentry;
  519. char *encoded_symname;
  520. size_t encoded_symlen;
  521. struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
  522. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  523. dget(lower_dentry);
  524. lower_dir_dentry = lock_parent(lower_dentry);
  525. mount_crypt_stat = &ecryptfs_superblock_to_private(
  526. dir->i_sb)->mount_crypt_stat;
  527. rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
  528. &encoded_symlen,
  529. NULL,
  530. mount_crypt_stat, symname,
  531. strlen(symname));
  532. if (rc)
  533. goto out_lock;
  534. rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
  535. encoded_symname);
  536. kfree(encoded_symname);
  537. if (rc || !lower_dentry->d_inode)
  538. goto out_lock;
  539. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  540. if (rc)
  541. goto out_lock;
  542. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  543. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  544. out_lock:
  545. unlock_dir(lower_dir_dentry);
  546. dput(lower_dentry);
  547. if (!dentry->d_inode)
  548. d_drop(dentry);
  549. return rc;
  550. }
  551. static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  552. {
  553. int rc;
  554. struct dentry *lower_dentry;
  555. struct dentry *lower_dir_dentry;
  556. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  557. lower_dir_dentry = lock_parent(lower_dentry);
  558. rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
  559. if (rc || !lower_dentry->d_inode)
  560. goto out;
  561. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  562. if (rc)
  563. goto out;
  564. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  565. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  566. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  567. out:
  568. unlock_dir(lower_dir_dentry);
  569. if (!dentry->d_inode)
  570. d_drop(dentry);
  571. return rc;
  572. }
  573. static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
  574. {
  575. struct dentry *lower_dentry;
  576. struct dentry *lower_dir_dentry;
  577. int rc;
  578. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  579. dget(dentry);
  580. lower_dir_dentry = lock_parent(lower_dentry);
  581. dget(lower_dentry);
  582. rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
  583. dput(lower_dentry);
  584. if (!rc && dentry->d_inode)
  585. clear_nlink(dentry->d_inode);
  586. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  587. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  588. unlock_dir(lower_dir_dentry);
  589. if (!rc)
  590. d_drop(dentry);
  591. dput(dentry);
  592. return rc;
  593. }
  594. static int
  595. ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  596. {
  597. int rc;
  598. struct dentry *lower_dentry;
  599. struct dentry *lower_dir_dentry;
  600. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  601. lower_dir_dentry = lock_parent(lower_dentry);
  602. rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
  603. if (rc || !lower_dentry->d_inode)
  604. goto out;
  605. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  606. if (rc)
  607. goto out;
  608. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  609. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  610. out:
  611. unlock_dir(lower_dir_dentry);
  612. if (!dentry->d_inode)
  613. d_drop(dentry);
  614. return rc;
  615. }
  616. static int
  617. ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  618. struct inode *new_dir, struct dentry *new_dentry)
  619. {
  620. int rc;
  621. struct dentry *lower_old_dentry;
  622. struct dentry *lower_new_dentry;
  623. struct dentry *lower_old_dir_dentry;
  624. struct dentry *lower_new_dir_dentry;
  625. struct dentry *trap = NULL;
  626. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  627. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  628. dget(lower_old_dentry);
  629. dget(lower_new_dentry);
  630. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  631. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  632. trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  633. /* source should not be ancestor of target */
  634. if (trap == lower_old_dentry) {
  635. rc = -EINVAL;
  636. goto out_lock;
  637. }
  638. /* target should not be ancestor of source */
  639. if (trap == lower_new_dentry) {
  640. rc = -ENOTEMPTY;
  641. goto out_lock;
  642. }
  643. rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
  644. lower_new_dir_dentry->d_inode, lower_new_dentry);
  645. if (rc)
  646. goto out_lock;
  647. fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
  648. if (new_dir != old_dir)
  649. fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
  650. out_lock:
  651. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  652. dput(lower_new_dir_dentry);
  653. dput(lower_old_dir_dentry);
  654. dput(lower_new_dentry);
  655. dput(lower_old_dentry);
  656. return rc;
  657. }
  658. static int ecryptfs_readlink_lower(struct dentry *dentry, char **buf,
  659. size_t *bufsiz)
  660. {
  661. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  662. char *lower_buf;
  663. size_t lower_bufsiz = PATH_MAX;
  664. mm_segment_t old_fs;
  665. int rc;
  666. lower_buf = kmalloc(lower_bufsiz, GFP_KERNEL);
  667. if (!lower_buf) {
  668. rc = -ENOMEM;
  669. goto out;
  670. }
  671. old_fs = get_fs();
  672. set_fs(get_ds());
  673. rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
  674. (char __user *)lower_buf,
  675. lower_bufsiz);
  676. set_fs(old_fs);
  677. if (rc < 0)
  678. goto out;
  679. lower_bufsiz = rc;
  680. rc = ecryptfs_decode_and_decrypt_filename(buf, bufsiz, dentry,
  681. lower_buf, lower_bufsiz);
  682. out:
  683. kfree(lower_buf);
  684. return rc;
  685. }
  686. static int
  687. ecryptfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
  688. {
  689. char *kbuf;
  690. size_t kbufsiz, copied;
  691. int rc;
  692. rc = ecryptfs_readlink_lower(dentry, &kbuf, &kbufsiz);
  693. if (rc)
  694. goto out;
  695. copied = min_t(size_t, bufsiz, kbufsiz);
  696. rc = copy_to_user(buf, kbuf, copied) ? -EFAULT : copied;
  697. kfree(kbuf);
  698. fsstack_copy_attr_atime(dentry->d_inode,
  699. ecryptfs_dentry_to_lower(dentry)->d_inode);
  700. out:
  701. return rc;
  702. }
  703. static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  704. {
  705. char *buf;
  706. int len = PAGE_SIZE, rc;
  707. mm_segment_t old_fs;
  708. /* Released in ecryptfs_put_link(); only release here on error */
  709. buf = kmalloc(len, GFP_KERNEL);
  710. if (!buf) {
  711. buf = ERR_PTR(-ENOMEM);
  712. goto out;
  713. }
  714. old_fs = get_fs();
  715. set_fs(get_ds());
  716. rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
  717. set_fs(old_fs);
  718. if (rc < 0) {
  719. kfree(buf);
  720. buf = ERR_PTR(rc);
  721. } else
  722. buf[rc] = '\0';
  723. out:
  724. nd_set_link(nd, buf);
  725. return NULL;
  726. }
  727. static void
  728. ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
  729. {
  730. char *buf = nd_get_link(nd);
  731. if (!IS_ERR(buf)) {
  732. /* Free the char* */
  733. kfree(buf);
  734. }
  735. }
  736. /**
  737. * upper_size_to_lower_size
  738. * @crypt_stat: Crypt_stat associated with file
  739. * @upper_size: Size of the upper file
  740. *
  741. * Calculate the required size of the lower file based on the
  742. * specified size of the upper file. This calculation is based on the
  743. * number of headers in the underlying file and the extent size.
  744. *
  745. * Returns Calculated size of the lower file.
  746. */
  747. static loff_t
  748. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  749. loff_t upper_size)
  750. {
  751. loff_t lower_size;
  752. lower_size = ecryptfs_lower_header_size(crypt_stat);
  753. if (upper_size != 0) {
  754. loff_t num_extents;
  755. num_extents = upper_size >> crypt_stat->extent_shift;
  756. if (upper_size & ~crypt_stat->extent_mask)
  757. num_extents++;
  758. lower_size += (num_extents * crypt_stat->extent_size);
  759. }
  760. return lower_size;
  761. }
  762. /**
  763. * truncate_upper
  764. * @dentry: The ecryptfs layer dentry
  765. * @ia: Address of the ecryptfs inode's attributes
  766. * @lower_ia: Address of the lower inode's attributes
  767. *
  768. * Function to handle truncations modifying the size of the file. Note
  769. * that the file sizes are interpolated. When expanding, we are simply
  770. * writing strings of 0's out. When truncating, we truncate the upper
  771. * inode and update the lower_ia according to the page index
  772. * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
  773. * the caller must use lower_ia in a call to notify_change() to perform
  774. * the truncation of the lower inode.
  775. *
  776. * Returns zero on success; non-zero otherwise
  777. */
  778. static int truncate_upper(struct dentry *dentry, struct iattr *ia,
  779. struct iattr *lower_ia)
  780. {
  781. int rc = 0;
  782. struct inode *inode = dentry->d_inode;
  783. struct ecryptfs_crypt_stat *crypt_stat;
  784. loff_t i_size = i_size_read(inode);
  785. loff_t lower_size_before_truncate;
  786. loff_t lower_size_after_truncate;
  787. if (unlikely((ia->ia_size == i_size))) {
  788. lower_ia->ia_valid &= ~ATTR_SIZE;
  789. return 0;
  790. }
  791. rc = ecryptfs_get_lower_file(dentry, inode);
  792. if (rc)
  793. return rc;
  794. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  795. /* Switch on growing or shrinking file */
  796. if (ia->ia_size > i_size) {
  797. char zero[] = { 0x00 };
  798. lower_ia->ia_valid &= ~ATTR_SIZE;
  799. /* Write a single 0 at the last position of the file;
  800. * this triggers code that will fill in 0's throughout
  801. * the intermediate portion of the previous end of the
  802. * file and the new and of the file */
  803. rc = ecryptfs_write(inode, zero,
  804. (ia->ia_size - 1), 1);
  805. } else { /* ia->ia_size < i_size_read(inode) */
  806. /* We're chopping off all the pages down to the page
  807. * in which ia->ia_size is located. Fill in the end of
  808. * that page from (ia->ia_size & ~PAGE_CACHE_MASK) to
  809. * PAGE_CACHE_SIZE with zeros. */
  810. size_t num_zeros = (PAGE_CACHE_SIZE
  811. - (ia->ia_size & ~PAGE_CACHE_MASK));
  812. /*
  813. * XXX(truncate) this should really happen at the begginning
  814. * of ->setattr. But the code is too messy to that as part
  815. * of a larger patch. ecryptfs is also totally missing out
  816. * on the inode_change_ok check at the beginning of
  817. * ->setattr while would include this.
  818. */
  819. rc = inode_newsize_ok(inode, ia->ia_size);
  820. if (rc)
  821. goto out;
  822. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  823. truncate_setsize(inode, ia->ia_size);
  824. lower_ia->ia_size = ia->ia_size;
  825. lower_ia->ia_valid |= ATTR_SIZE;
  826. goto out;
  827. }
  828. if (num_zeros) {
  829. char *zeros_virt;
  830. zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
  831. if (!zeros_virt) {
  832. rc = -ENOMEM;
  833. goto out;
  834. }
  835. rc = ecryptfs_write(inode, zeros_virt,
  836. ia->ia_size, num_zeros);
  837. kfree(zeros_virt);
  838. if (rc) {
  839. printk(KERN_ERR "Error attempting to zero out "
  840. "the remainder of the end page on "
  841. "reducing truncate; rc = [%d]\n", rc);
  842. goto out;
  843. }
  844. }
  845. truncate_setsize(inode, ia->ia_size);
  846. rc = ecryptfs_write_inode_size_to_metadata(inode);
  847. if (rc) {
  848. printk(KERN_ERR "Problem with "
  849. "ecryptfs_write_inode_size_to_metadata; "
  850. "rc = [%d]\n", rc);
  851. goto out;
  852. }
  853. /* We are reducing the size of the ecryptfs file, and need to
  854. * know if we need to reduce the size of the lower file. */
  855. lower_size_before_truncate =
  856. upper_size_to_lower_size(crypt_stat, i_size);
  857. lower_size_after_truncate =
  858. upper_size_to_lower_size(crypt_stat, ia->ia_size);
  859. if (lower_size_after_truncate < lower_size_before_truncate) {
  860. lower_ia->ia_size = lower_size_after_truncate;
  861. lower_ia->ia_valid |= ATTR_SIZE;
  862. } else
  863. lower_ia->ia_valid &= ~ATTR_SIZE;
  864. }
  865. out:
  866. ecryptfs_put_lower_file(inode);
  867. return rc;
  868. }
  869. /**
  870. * ecryptfs_truncate
  871. * @dentry: The ecryptfs layer dentry
  872. * @new_length: The length to expand the file to
  873. *
  874. * Simple function that handles the truncation of an eCryptfs inode and
  875. * its corresponding lower inode.
  876. *
  877. * Returns zero on success; non-zero otherwise
  878. */
  879. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  880. {
  881. struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
  882. struct iattr lower_ia = { .ia_valid = 0 };
  883. int rc;
  884. rc = truncate_upper(dentry, &ia, &lower_ia);
  885. if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
  886. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  887. mutex_lock(&lower_dentry->d_inode->i_mutex);
  888. rc = notify_change(lower_dentry, &lower_ia);
  889. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  890. }
  891. return rc;
  892. }
  893. static int
  894. ecryptfs_permission(struct inode *inode, int mask, unsigned int flags)
  895. {
  896. if (flags & IPERM_FLAG_RCU)
  897. return -ECHILD;
  898. return inode_permission(ecryptfs_inode_to_lower(inode), mask);
  899. }
  900. /**
  901. * ecryptfs_setattr
  902. * @dentry: dentry handle to the inode to modify
  903. * @ia: Structure with flags of what to change and values
  904. *
  905. * Updates the metadata of an inode. If the update is to the size
  906. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  907. * of both the ecryptfs inode and the lower inode.
  908. *
  909. * All other metadata changes will be passed right to the lower filesystem,
  910. * and we will just update our inode to look like the lower.
  911. */
  912. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  913. {
  914. int rc = 0;
  915. struct dentry *lower_dentry;
  916. struct iattr lower_ia;
  917. struct inode *inode;
  918. struct inode *lower_inode;
  919. struct ecryptfs_crypt_stat *crypt_stat;
  920. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  921. if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
  922. ecryptfs_init_crypt_stat(crypt_stat);
  923. inode = dentry->d_inode;
  924. lower_inode = ecryptfs_inode_to_lower(inode);
  925. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  926. mutex_lock(&crypt_stat->cs_mutex);
  927. if (S_ISDIR(dentry->d_inode->i_mode))
  928. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  929. else if (S_ISREG(dentry->d_inode->i_mode)
  930. && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  931. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
  932. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  933. mount_crypt_stat = &ecryptfs_superblock_to_private(
  934. dentry->d_sb)->mount_crypt_stat;
  935. rc = ecryptfs_get_lower_file(dentry, inode);
  936. if (rc) {
  937. mutex_unlock(&crypt_stat->cs_mutex);
  938. goto out;
  939. }
  940. rc = ecryptfs_read_metadata(dentry);
  941. ecryptfs_put_lower_file(inode);
  942. if (rc) {
  943. if (!(mount_crypt_stat->flags
  944. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  945. rc = -EIO;
  946. printk(KERN_WARNING "Either the lower file "
  947. "is not in a valid eCryptfs format, "
  948. "or the key could not be retrieved. "
  949. "Plaintext passthrough mode is not "
  950. "enabled; returning -EIO\n");
  951. mutex_unlock(&crypt_stat->cs_mutex);
  952. goto out;
  953. }
  954. rc = 0;
  955. crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
  956. | ECRYPTFS_ENCRYPTED);
  957. }
  958. }
  959. mutex_unlock(&crypt_stat->cs_mutex);
  960. if (S_ISREG(inode->i_mode)) {
  961. rc = filemap_write_and_wait(inode->i_mapping);
  962. if (rc)
  963. goto out;
  964. fsstack_copy_attr_all(inode, lower_inode);
  965. }
  966. memcpy(&lower_ia, ia, sizeof(lower_ia));
  967. if (ia->ia_valid & ATTR_FILE)
  968. lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
  969. if (ia->ia_valid & ATTR_SIZE) {
  970. rc = truncate_upper(dentry, ia, &lower_ia);
  971. if (rc < 0)
  972. goto out;
  973. }
  974. /*
  975. * mode change is for clearing setuid/setgid bits. Allow lower fs
  976. * to interpret this in its own way.
  977. */
  978. if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  979. lower_ia.ia_valid &= ~ATTR_MODE;
  980. mutex_lock(&lower_dentry->d_inode->i_mutex);
  981. rc = notify_change(lower_dentry, &lower_ia);
  982. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  983. out:
  984. fsstack_copy_attr_all(inode, lower_inode);
  985. return rc;
  986. }
  987. int ecryptfs_getattr_link(struct vfsmount *mnt, struct dentry *dentry,
  988. struct kstat *stat)
  989. {
  990. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  991. int rc = 0;
  992. mount_crypt_stat = &ecryptfs_superblock_to_private(
  993. dentry->d_sb)->mount_crypt_stat;
  994. generic_fillattr(dentry->d_inode, stat);
  995. if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
  996. char *target;
  997. size_t targetsiz;
  998. rc = ecryptfs_readlink_lower(dentry, &target, &targetsiz);
  999. if (!rc) {
  1000. kfree(target);
  1001. stat->size = targetsiz;
  1002. }
  1003. }
  1004. return rc;
  1005. }
  1006. int ecryptfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1007. struct kstat *stat)
  1008. {
  1009. struct kstat lower_stat;
  1010. int rc;
  1011. rc = vfs_getattr(ecryptfs_dentry_to_lower_mnt(dentry),
  1012. ecryptfs_dentry_to_lower(dentry), &lower_stat);
  1013. if (!rc) {
  1014. fsstack_copy_attr_all(dentry->d_inode,
  1015. ecryptfs_inode_to_lower(dentry->d_inode));
  1016. generic_fillattr(dentry->d_inode, stat);
  1017. stat->blocks = lower_stat.blocks;
  1018. }
  1019. return rc;
  1020. }
  1021. int
  1022. ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  1023. size_t size, int flags)
  1024. {
  1025. int rc = 0;
  1026. struct dentry *lower_dentry;
  1027. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  1028. if (!lower_dentry->d_inode->i_op->setxattr) {
  1029. rc = -EOPNOTSUPP;
  1030. goto out;
  1031. }
  1032. rc = vfs_setxattr(lower_dentry, name, value, size, flags);
  1033. out:
  1034. return rc;
  1035. }
  1036. ssize_t
  1037. ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
  1038. void *value, size_t size)
  1039. {
  1040. int rc = 0;
  1041. if (!lower_dentry->d_inode->i_op->getxattr) {
  1042. rc = -EOPNOTSUPP;
  1043. goto out;
  1044. }
  1045. mutex_lock(&lower_dentry->d_inode->i_mutex);
  1046. rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
  1047. size);
  1048. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  1049. out:
  1050. return rc;
  1051. }
  1052. static ssize_t
  1053. ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
  1054. size_t size)
  1055. {
  1056. return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
  1057. value, size);
  1058. }
  1059. static ssize_t
  1060. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  1061. {
  1062. int rc = 0;
  1063. struct dentry *lower_dentry;
  1064. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  1065. if (!lower_dentry->d_inode->i_op->listxattr) {
  1066. rc = -EOPNOTSUPP;
  1067. goto out;
  1068. }
  1069. mutex_lock(&lower_dentry->d_inode->i_mutex);
  1070. rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
  1071. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  1072. out:
  1073. return rc;
  1074. }
  1075. static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
  1076. {
  1077. int rc = 0;
  1078. struct dentry *lower_dentry;
  1079. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  1080. if (!lower_dentry->d_inode->i_op->removexattr) {
  1081. rc = -EOPNOTSUPP;
  1082. goto out;
  1083. }
  1084. mutex_lock(&lower_dentry->d_inode->i_mutex);
  1085. rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
  1086. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  1087. out:
  1088. return rc;
  1089. }
  1090. const struct inode_operations ecryptfs_symlink_iops = {
  1091. .readlink = ecryptfs_readlink,
  1092. .follow_link = ecryptfs_follow_link,
  1093. .put_link = ecryptfs_put_link,
  1094. .permission = ecryptfs_permission,
  1095. .setattr = ecryptfs_setattr,
  1096. .getattr = ecryptfs_getattr_link,
  1097. .setxattr = ecryptfs_setxattr,
  1098. .getxattr = ecryptfs_getxattr,
  1099. .listxattr = ecryptfs_listxattr,
  1100. .removexattr = ecryptfs_removexattr
  1101. };
  1102. const struct inode_operations ecryptfs_dir_iops = {
  1103. .create = ecryptfs_create,
  1104. .lookup = ecryptfs_lookup,
  1105. .link = ecryptfs_link,
  1106. .unlink = ecryptfs_unlink,
  1107. .symlink = ecryptfs_symlink,
  1108. .mkdir = ecryptfs_mkdir,
  1109. .rmdir = ecryptfs_rmdir,
  1110. .mknod = ecryptfs_mknod,
  1111. .rename = ecryptfs_rename,
  1112. .permission = ecryptfs_permission,
  1113. .setattr = ecryptfs_setattr,
  1114. .setxattr = ecryptfs_setxattr,
  1115. .getxattr = ecryptfs_getxattr,
  1116. .listxattr = ecryptfs_listxattr,
  1117. .removexattr = ecryptfs_removexattr
  1118. };
  1119. const struct inode_operations ecryptfs_main_iops = {
  1120. .permission = ecryptfs_permission,
  1121. .setattr = ecryptfs_setattr,
  1122. .getattr = ecryptfs_getattr,
  1123. .setxattr = ecryptfs_setxattr,
  1124. .getxattr = ecryptfs_getxattr,
  1125. .listxattr = ecryptfs_listxattr,
  1126. .removexattr = ecryptfs_removexattr
  1127. };