inode.c 34 KB

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