inode.c 34 KB

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