inode.c 33 KB

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