inode.c 33 KB

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