inode.c 34 KB

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