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