inode.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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, 1);
  270. if (rc) {
  271. printk(KERN_ERR "%s: Error interposing; rc = [%d]\n",
  272. __func__, rc);
  273. goto out;
  274. }
  275. if (S_ISDIR(lower_inode->i_mode))
  276. goto out;
  277. if (S_ISLNK(lower_inode->i_mode))
  278. goto out;
  279. if (special_file(lower_inode->i_mode))
  280. goto out;
  281. if (!ecryptfs_nd)
  282. goto out;
  283. /* Released in this function */
  284. page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2, GFP_USER);
  285. if (!page_virt) {
  286. printk(KERN_ERR "%s: Cannot kmem_cache_zalloc() a page\n",
  287. __func__);
  288. rc = -ENOMEM;
  289. goto out;
  290. }
  291. if (!ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->lower_file) {
  292. rc = ecryptfs_init_persistent_file(ecryptfs_dentry);
  293. if (rc) {
  294. printk(KERN_ERR "%s: Error attempting to initialize "
  295. "the persistent file for the dentry with name "
  296. "[%s]; rc = [%d]\n", __func__,
  297. ecryptfs_dentry->d_name.name, rc);
  298. goto out_free_kmem;
  299. }
  300. }
  301. crypt_stat = &ecryptfs_inode_to_private(
  302. ecryptfs_dentry->d_inode)->crypt_stat;
  303. /* TODO: lock for crypt_stat comparison */
  304. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
  305. ecryptfs_set_default_sizes(crypt_stat);
  306. rc = ecryptfs_read_and_validate_header_region(page_virt,
  307. ecryptfs_dentry->d_inode);
  308. if (rc) {
  309. rc = ecryptfs_read_and_validate_xattr_region(page_virt,
  310. ecryptfs_dentry);
  311. if (rc) {
  312. rc = 0;
  313. goto out_free_kmem;
  314. }
  315. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  316. }
  317. mount_crypt_stat = &ecryptfs_superblock_to_private(
  318. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  319. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
  320. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  321. file_size = (crypt_stat->num_header_bytes_at_front
  322. + i_size_read(lower_dentry->d_inode));
  323. else
  324. file_size = i_size_read(lower_dentry->d_inode);
  325. } else {
  326. file_size = get_unaligned_be64(page_virt);
  327. }
  328. i_size_write(ecryptfs_dentry->d_inode, (loff_t)file_size);
  329. out_free_kmem:
  330. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  331. goto out;
  332. out_dput:
  333. dput(lower_dentry);
  334. d_drop(ecryptfs_dentry);
  335. out:
  336. return rc;
  337. }
  338. /**
  339. * ecryptfs_lookup
  340. * @ecryptfs_dir_inode: The eCryptfs directory inode
  341. * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
  342. * @ecryptfs_nd: nameidata; may be NULL
  343. *
  344. * Find a file on disk. If the file does not exist, then we'll add it to the
  345. * dentry cache and continue on to read it from the disk.
  346. */
  347. static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
  348. struct dentry *ecryptfs_dentry,
  349. struct nameidata *ecryptfs_nd)
  350. {
  351. char *encrypted_and_encoded_name = NULL;
  352. size_t encrypted_and_encoded_name_size;
  353. struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
  354. struct dentry *lower_dir_dentry, *lower_dentry;
  355. int rc = 0;
  356. ecryptfs_dentry->d_op = &ecryptfs_dops;
  357. if ((ecryptfs_dentry->d_name.len == 1
  358. && !strcmp(ecryptfs_dentry->d_name.name, "."))
  359. || (ecryptfs_dentry->d_name.len == 2
  360. && !strcmp(ecryptfs_dentry->d_name.name, ".."))) {
  361. goto out_d_drop;
  362. }
  363. lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
  364. mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
  365. lower_dentry = lookup_one_len(ecryptfs_dentry->d_name.name,
  366. lower_dir_dentry,
  367. ecryptfs_dentry->d_name.len);
  368. mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
  369. if (IS_ERR(lower_dentry)) {
  370. rc = PTR_ERR(lower_dentry);
  371. printk(KERN_ERR "%s: lookup_one_len() returned [%d] on "
  372. "lower_dentry = [%s]\n", __func__, rc,
  373. ecryptfs_dentry->d_name.name);
  374. goto out_d_drop;
  375. }
  376. if (lower_dentry->d_inode)
  377. goto lookup_and_interpose;
  378. mount_crypt_stat = &ecryptfs_superblock_to_private(
  379. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  380. if (!(mount_crypt_stat
  381. && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)))
  382. goto lookup_and_interpose;
  383. dput(lower_dentry);
  384. rc = ecryptfs_encrypt_and_encode_filename(
  385. &encrypted_and_encoded_name, &encrypted_and_encoded_name_size,
  386. NULL, mount_crypt_stat, ecryptfs_dentry->d_name.name,
  387. ecryptfs_dentry->d_name.len);
  388. if (rc) {
  389. printk(KERN_ERR "%s: Error attempting to encrypt and encode "
  390. "filename; rc = [%d]\n", __func__, rc);
  391. goto out_d_drop;
  392. }
  393. mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
  394. lower_dentry = lookup_one_len(encrypted_and_encoded_name,
  395. lower_dir_dentry,
  396. encrypted_and_encoded_name_size - 1);
  397. mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
  398. if (IS_ERR(lower_dentry)) {
  399. rc = PTR_ERR(lower_dentry);
  400. printk(KERN_ERR "%s: lookup_one_len() returned [%d] on "
  401. "lower_dentry = [%s]\n", __func__, rc,
  402. encrypted_and_encoded_name);
  403. goto out_d_drop;
  404. }
  405. lookup_and_interpose:
  406. rc = ecryptfs_lookup_and_interpose_lower(ecryptfs_dentry, lower_dentry,
  407. ecryptfs_dir_inode,
  408. ecryptfs_nd);
  409. goto out;
  410. out_d_drop:
  411. d_drop(ecryptfs_dentry);
  412. out:
  413. kfree(encrypted_and_encoded_name);
  414. return ERR_PTR(rc);
  415. }
  416. static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
  417. struct dentry *new_dentry)
  418. {
  419. struct dentry *lower_old_dentry;
  420. struct dentry *lower_new_dentry;
  421. struct dentry *lower_dir_dentry;
  422. u64 file_size_save;
  423. int rc;
  424. file_size_save = i_size_read(old_dentry->d_inode);
  425. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  426. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  427. dget(lower_old_dentry);
  428. dget(lower_new_dentry);
  429. lower_dir_dentry = lock_parent(lower_new_dentry);
  430. rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
  431. lower_new_dentry);
  432. if (rc || !lower_new_dentry->d_inode)
  433. goto out_lock;
  434. rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
  435. if (rc)
  436. goto out_lock;
  437. fsstack_copy_attr_times(dir, lower_new_dentry->d_inode);
  438. fsstack_copy_inode_size(dir, lower_new_dentry->d_inode);
  439. old_dentry->d_inode->i_nlink =
  440. ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
  441. i_size_write(new_dentry->d_inode, file_size_save);
  442. out_lock:
  443. unlock_dir(lower_dir_dentry);
  444. dput(lower_new_dentry);
  445. dput(lower_old_dentry);
  446. d_drop(lower_old_dentry);
  447. d_drop(new_dentry);
  448. d_drop(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. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  587. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  588. dget(lower_old_dentry);
  589. dget(lower_new_dentry);
  590. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  591. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  592. lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  593. rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
  594. lower_new_dir_dentry->d_inode, lower_new_dentry);
  595. if (rc)
  596. goto out_lock;
  597. fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
  598. if (new_dir != old_dir)
  599. fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
  600. out_lock:
  601. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  602. dput(lower_new_dentry->d_parent);
  603. dput(lower_old_dentry->d_parent);
  604. dput(lower_new_dentry);
  605. dput(lower_old_dentry);
  606. return rc;
  607. }
  608. static int
  609. ecryptfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
  610. {
  611. char *lower_buf;
  612. size_t lower_bufsiz;
  613. struct dentry *lower_dentry;
  614. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  615. char *plaintext_name;
  616. size_t plaintext_name_size;
  617. mm_segment_t old_fs;
  618. int rc;
  619. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  620. if (!lower_dentry->d_inode->i_op->readlink) {
  621. rc = -EINVAL;
  622. goto out;
  623. }
  624. mount_crypt_stat = &ecryptfs_superblock_to_private(
  625. dentry->d_sb)->mount_crypt_stat;
  626. /*
  627. * If the lower filename is encrypted, it will result in a significantly
  628. * longer name. If needed, truncate the name after decode and decrypt.
  629. */
  630. if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)
  631. lower_bufsiz = PATH_MAX;
  632. else
  633. lower_bufsiz = bufsiz;
  634. /* Released in this function */
  635. lower_buf = kmalloc(lower_bufsiz, GFP_KERNEL);
  636. if (lower_buf == NULL) {
  637. printk(KERN_ERR "%s: Out of memory whilst attempting to "
  638. "kmalloc [%zd] bytes\n", __func__, lower_bufsiz);
  639. rc = -ENOMEM;
  640. goto out;
  641. }
  642. old_fs = get_fs();
  643. set_fs(get_ds());
  644. rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
  645. (char __user *)lower_buf,
  646. lower_bufsiz);
  647. set_fs(old_fs);
  648. if (rc >= 0) {
  649. rc = ecryptfs_decode_and_decrypt_filename(&plaintext_name,
  650. &plaintext_name_size,
  651. dentry, lower_buf,
  652. rc);
  653. if (rc) {
  654. printk(KERN_ERR "%s: Error attempting to decode and "
  655. "decrypt filename; rc = [%d]\n", __func__,
  656. rc);
  657. goto out_free_lower_buf;
  658. }
  659. /* Check for bufsiz <= 0 done in sys_readlinkat() */
  660. rc = copy_to_user(buf, plaintext_name,
  661. min((size_t) bufsiz, plaintext_name_size));
  662. if (rc)
  663. rc = -EFAULT;
  664. else
  665. rc = plaintext_name_size;
  666. kfree(plaintext_name);
  667. fsstack_copy_attr_atime(dentry->d_inode, lower_dentry->d_inode);
  668. }
  669. out_free_lower_buf:
  670. kfree(lower_buf);
  671. out:
  672. return rc;
  673. }
  674. static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  675. {
  676. char *buf;
  677. int len = PAGE_SIZE, rc;
  678. mm_segment_t old_fs;
  679. /* Released in ecryptfs_put_link(); only release here on error */
  680. buf = kmalloc(len, GFP_KERNEL);
  681. if (!buf) {
  682. buf = ERR_PTR(-ENOMEM);
  683. goto out;
  684. }
  685. old_fs = get_fs();
  686. set_fs(get_ds());
  687. rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
  688. set_fs(old_fs);
  689. if (rc < 0) {
  690. kfree(buf);
  691. buf = ERR_PTR(rc);
  692. } else
  693. buf[rc] = '\0';
  694. out:
  695. nd_set_link(nd, buf);
  696. return NULL;
  697. }
  698. static void
  699. ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
  700. {
  701. char *buf = nd_get_link(nd);
  702. if (!IS_ERR(buf)) {
  703. /* Free the char* */
  704. kfree(buf);
  705. }
  706. }
  707. /**
  708. * upper_size_to_lower_size
  709. * @crypt_stat: Crypt_stat associated with file
  710. * @upper_size: Size of the upper file
  711. *
  712. * Calculate the required size of the lower file based on the
  713. * specified size of the upper file. This calculation is based on the
  714. * number of headers in the underlying file and the extent size.
  715. *
  716. * Returns Calculated size of the lower file.
  717. */
  718. static loff_t
  719. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  720. loff_t upper_size)
  721. {
  722. loff_t lower_size;
  723. lower_size = crypt_stat->num_header_bytes_at_front;
  724. if (upper_size != 0) {
  725. loff_t num_extents;
  726. num_extents = upper_size >> crypt_stat->extent_shift;
  727. if (upper_size & ~crypt_stat->extent_mask)
  728. num_extents++;
  729. lower_size += (num_extents * crypt_stat->extent_size);
  730. }
  731. return lower_size;
  732. }
  733. /**
  734. * truncate_upper
  735. * @dentry: The ecryptfs layer dentry
  736. * @ia: Address of the ecryptfs inode's attributes
  737. * @lower_ia: Address of the lower inode's attributes
  738. *
  739. * Function to handle truncations modifying the size of the file. Note
  740. * that the file sizes are interpolated. When expanding, we are simply
  741. * writing strings of 0's out. When truncating, we truncate the upper
  742. * inode and update the lower_ia according to the page index
  743. * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
  744. * the caller must use lower_ia in a call to notify_change() to perform
  745. * the truncation of the lower inode.
  746. *
  747. * Returns zero on success; non-zero otherwise
  748. */
  749. static int truncate_upper(struct dentry *dentry, struct iattr *ia,
  750. struct iattr *lower_ia)
  751. {
  752. int rc = 0;
  753. struct inode *inode = dentry->d_inode;
  754. struct dentry *lower_dentry;
  755. struct file fake_ecryptfs_file;
  756. struct ecryptfs_crypt_stat *crypt_stat;
  757. loff_t i_size = i_size_read(inode);
  758. loff_t lower_size_before_truncate;
  759. loff_t lower_size_after_truncate;
  760. if (unlikely((ia->ia_size == i_size))) {
  761. lower_ia->ia_valid &= ~ATTR_SIZE;
  762. goto out;
  763. }
  764. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  765. /* Set up a fake ecryptfs file, this is used to interface with
  766. * the file in the underlying filesystem so that the
  767. * truncation has an effect there as well. */
  768. memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
  769. fake_ecryptfs_file.f_path.dentry = dentry;
  770. /* Released at out_free: label */
  771. ecryptfs_set_file_private(&fake_ecryptfs_file,
  772. kmem_cache_alloc(ecryptfs_file_info_cache,
  773. GFP_KERNEL));
  774. if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
  775. rc = -ENOMEM;
  776. goto out;
  777. }
  778. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  779. ecryptfs_set_file_lower(
  780. &fake_ecryptfs_file,
  781. ecryptfs_inode_to_private(dentry->d_inode)->lower_file);
  782. /* Switch on growing or shrinking file */
  783. if (ia->ia_size > i_size) {
  784. char zero[] = { 0x00 };
  785. lower_ia->ia_valid &= ~ATTR_SIZE;
  786. /* Write a single 0 at the last position of the file;
  787. * this triggers code that will fill in 0's throughout
  788. * the intermediate portion of the previous end of the
  789. * file and the new and of the file */
  790. rc = ecryptfs_write(&fake_ecryptfs_file, zero,
  791. (ia->ia_size - 1), 1);
  792. } else { /* ia->ia_size < i_size_read(inode) */
  793. /* We're chopping off all the pages down to the page
  794. * in which ia->ia_size is located. Fill in the end of
  795. * that page from (ia->ia_size & ~PAGE_CACHE_MASK) to
  796. * PAGE_CACHE_SIZE with zeros. */
  797. size_t num_zeros = (PAGE_CACHE_SIZE
  798. - (ia->ia_size & ~PAGE_CACHE_MASK));
  799. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  800. rc = vmtruncate(inode, ia->ia_size);
  801. if (rc)
  802. goto out_free;
  803. lower_ia->ia_size = ia->ia_size;
  804. lower_ia->ia_valid |= ATTR_SIZE;
  805. goto out_free;
  806. }
  807. if (num_zeros) {
  808. char *zeros_virt;
  809. zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
  810. if (!zeros_virt) {
  811. rc = -ENOMEM;
  812. goto out_free;
  813. }
  814. rc = ecryptfs_write(&fake_ecryptfs_file, zeros_virt,
  815. ia->ia_size, num_zeros);
  816. kfree(zeros_virt);
  817. if (rc) {
  818. printk(KERN_ERR "Error attempting to zero out "
  819. "the remainder of the end page on "
  820. "reducing truncate; rc = [%d]\n", rc);
  821. goto out_free;
  822. }
  823. }
  824. vmtruncate(inode, ia->ia_size);
  825. rc = ecryptfs_write_inode_size_to_metadata(inode);
  826. if (rc) {
  827. printk(KERN_ERR "Problem with "
  828. "ecryptfs_write_inode_size_to_metadata; "
  829. "rc = [%d]\n", rc);
  830. goto out_free;
  831. }
  832. /* We are reducing the size of the ecryptfs file, and need to
  833. * know if we need to reduce the size of the lower file. */
  834. lower_size_before_truncate =
  835. upper_size_to_lower_size(crypt_stat, i_size);
  836. lower_size_after_truncate =
  837. upper_size_to_lower_size(crypt_stat, ia->ia_size);
  838. if (lower_size_after_truncate < lower_size_before_truncate) {
  839. lower_ia->ia_size = lower_size_after_truncate;
  840. lower_ia->ia_valid |= ATTR_SIZE;
  841. } else
  842. lower_ia->ia_valid &= ~ATTR_SIZE;
  843. }
  844. out_free:
  845. if (ecryptfs_file_to_private(&fake_ecryptfs_file))
  846. kmem_cache_free(ecryptfs_file_info_cache,
  847. ecryptfs_file_to_private(&fake_ecryptfs_file));
  848. out:
  849. return rc;
  850. }
  851. /**
  852. * ecryptfs_truncate
  853. * @dentry: The ecryptfs layer dentry
  854. * @new_length: The length to expand the file to
  855. *
  856. * Simple function that handles the truncation of an eCryptfs inode and
  857. * its corresponding lower inode.
  858. *
  859. * Returns zero on success; non-zero otherwise
  860. */
  861. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  862. {
  863. struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
  864. struct iattr lower_ia = { .ia_valid = 0 };
  865. int rc;
  866. rc = truncate_upper(dentry, &ia, &lower_ia);
  867. if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
  868. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  869. mutex_lock(&lower_dentry->d_inode->i_mutex);
  870. rc = notify_change(lower_dentry, &lower_ia);
  871. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  872. }
  873. return rc;
  874. }
  875. static int
  876. ecryptfs_permission(struct inode *inode, int mask)
  877. {
  878. return inode_permission(ecryptfs_inode_to_lower(inode), mask);
  879. }
  880. /**
  881. * ecryptfs_setattr
  882. * @dentry: dentry handle to the inode to modify
  883. * @ia: Structure with flags of what to change and values
  884. *
  885. * Updates the metadata of an inode. If the update is to the size
  886. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  887. * of both the ecryptfs inode and the lower inode.
  888. *
  889. * All other metadata changes will be passed right to the lower filesystem,
  890. * and we will just update our inode to look like the lower.
  891. */
  892. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  893. {
  894. int rc = 0;
  895. struct dentry *lower_dentry;
  896. struct iattr lower_ia;
  897. struct inode *inode;
  898. struct inode *lower_inode;
  899. struct ecryptfs_crypt_stat *crypt_stat;
  900. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  901. if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
  902. ecryptfs_init_crypt_stat(crypt_stat);
  903. inode = dentry->d_inode;
  904. lower_inode = ecryptfs_inode_to_lower(inode);
  905. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  906. mutex_lock(&crypt_stat->cs_mutex);
  907. if (S_ISDIR(dentry->d_inode->i_mode))
  908. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  909. else if (S_ISREG(dentry->d_inode->i_mode)
  910. && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  911. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
  912. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  913. mount_crypt_stat = &ecryptfs_superblock_to_private(
  914. dentry->d_sb)->mount_crypt_stat;
  915. rc = ecryptfs_read_metadata(dentry);
  916. if (rc) {
  917. if (!(mount_crypt_stat->flags
  918. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  919. rc = -EIO;
  920. printk(KERN_WARNING "Either the lower file "
  921. "is not in a valid eCryptfs format, "
  922. "or the key could not be retrieved. "
  923. "Plaintext passthrough mode is not "
  924. "enabled; returning -EIO\n");
  925. mutex_unlock(&crypt_stat->cs_mutex);
  926. goto out;
  927. }
  928. rc = 0;
  929. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  930. }
  931. }
  932. mutex_unlock(&crypt_stat->cs_mutex);
  933. memcpy(&lower_ia, ia, sizeof(lower_ia));
  934. if (ia->ia_valid & ATTR_FILE)
  935. lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
  936. if (ia->ia_valid & ATTR_SIZE) {
  937. rc = truncate_upper(dentry, ia, &lower_ia);
  938. if (rc < 0)
  939. goto out;
  940. }
  941. /*
  942. * mode change is for clearing setuid/setgid bits. Allow lower fs
  943. * to interpret this in its own way.
  944. */
  945. if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  946. lower_ia.ia_valid &= ~ATTR_MODE;
  947. mutex_lock(&lower_dentry->d_inode->i_mutex);
  948. rc = notify_change(lower_dentry, &lower_ia);
  949. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  950. out:
  951. fsstack_copy_attr_all(inode, lower_inode);
  952. return rc;
  953. }
  954. int
  955. ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  956. size_t size, int flags)
  957. {
  958. int rc = 0;
  959. struct dentry *lower_dentry;
  960. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  961. if (!lower_dentry->d_inode->i_op->setxattr) {
  962. rc = -ENOSYS;
  963. goto out;
  964. }
  965. mutex_lock(&lower_dentry->d_inode->i_mutex);
  966. rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
  967. size, flags);
  968. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  969. out:
  970. return rc;
  971. }
  972. ssize_t
  973. ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
  974. void *value, size_t size)
  975. {
  976. int rc = 0;
  977. if (!lower_dentry->d_inode->i_op->getxattr) {
  978. rc = -ENOSYS;
  979. goto out;
  980. }
  981. mutex_lock(&lower_dentry->d_inode->i_mutex);
  982. rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
  983. size);
  984. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  985. out:
  986. return rc;
  987. }
  988. static ssize_t
  989. ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
  990. size_t size)
  991. {
  992. return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
  993. value, size);
  994. }
  995. static ssize_t
  996. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  997. {
  998. int rc = 0;
  999. struct dentry *lower_dentry;
  1000. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  1001. if (!lower_dentry->d_inode->i_op->listxattr) {
  1002. rc = -ENOSYS;
  1003. goto out;
  1004. }
  1005. mutex_lock(&lower_dentry->d_inode->i_mutex);
  1006. rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
  1007. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  1008. out:
  1009. return rc;
  1010. }
  1011. static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
  1012. {
  1013. int rc = 0;
  1014. struct dentry *lower_dentry;
  1015. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  1016. if (!lower_dentry->d_inode->i_op->removexattr) {
  1017. rc = -ENOSYS;
  1018. goto out;
  1019. }
  1020. mutex_lock(&lower_dentry->d_inode->i_mutex);
  1021. rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
  1022. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  1023. out:
  1024. return rc;
  1025. }
  1026. int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
  1027. {
  1028. if ((ecryptfs_inode_to_lower(inode)
  1029. == (struct inode *)candidate_lower_inode))
  1030. return 1;
  1031. else
  1032. return 0;
  1033. }
  1034. int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
  1035. {
  1036. ecryptfs_init_inode(inode, (struct inode *)lower_inode);
  1037. return 0;
  1038. }
  1039. const struct inode_operations ecryptfs_symlink_iops = {
  1040. .readlink = ecryptfs_readlink,
  1041. .follow_link = ecryptfs_follow_link,
  1042. .put_link = ecryptfs_put_link,
  1043. .permission = ecryptfs_permission,
  1044. .setattr = ecryptfs_setattr,
  1045. .setxattr = ecryptfs_setxattr,
  1046. .getxattr = ecryptfs_getxattr,
  1047. .listxattr = ecryptfs_listxattr,
  1048. .removexattr = ecryptfs_removexattr
  1049. };
  1050. const struct inode_operations ecryptfs_dir_iops = {
  1051. .create = ecryptfs_create,
  1052. .lookup = ecryptfs_lookup,
  1053. .link = ecryptfs_link,
  1054. .unlink = ecryptfs_unlink,
  1055. .symlink = ecryptfs_symlink,
  1056. .mkdir = ecryptfs_mkdir,
  1057. .rmdir = ecryptfs_rmdir,
  1058. .mknod = ecryptfs_mknod,
  1059. .rename = ecryptfs_rename,
  1060. .permission = ecryptfs_permission,
  1061. .setattr = ecryptfs_setattr,
  1062. .setxattr = ecryptfs_setxattr,
  1063. .getxattr = ecryptfs_getxattr,
  1064. .listxattr = ecryptfs_listxattr,
  1065. .removexattr = ecryptfs_removexattr
  1066. };
  1067. const struct inode_operations ecryptfs_main_iops = {
  1068. .permission = ecryptfs_permission,
  1069. .setattr = ecryptfs_setattr,
  1070. .setxattr = ecryptfs_setxattr,
  1071. .getxattr = ecryptfs_getxattr,
  1072. .listxattr = ecryptfs_listxattr,
  1073. .removexattr = ecryptfs_removexattr
  1074. };