inode.c 34 KB

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