inode.c 33 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 <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. char zero_virt[] = { 0x00 };
  137. int rc = 0;
  138. rc = ecryptfs_write(ecryptfs_inode, zero_virt, 0, 1);
  139. i_size_write(ecryptfs_inode, 0);
  140. rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
  141. ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat.flags |=
  142. ECRYPTFS_NEW_FILE;
  143. return rc;
  144. }
  145. /**
  146. * ecryptfs_initialize_file
  147. *
  148. * Cause the file to be changed from a basic empty file to an ecryptfs
  149. * file with a header and first data page.
  150. *
  151. * Returns zero on success
  152. */
  153. static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
  154. {
  155. struct ecryptfs_crypt_stat *crypt_stat =
  156. &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
  157. int rc = 0;
  158. if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
  159. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  160. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  161. goto out;
  162. }
  163. crypt_stat->flags |= ECRYPTFS_NEW_FILE;
  164. ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
  165. rc = ecryptfs_new_file_context(ecryptfs_dentry);
  166. if (rc) {
  167. ecryptfs_printk(KERN_ERR, "Error creating new file "
  168. "context; rc = [%d]\n", rc);
  169. goto out;
  170. }
  171. if (!ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->lower_file) {
  172. rc = ecryptfs_init_persistent_file(ecryptfs_dentry);
  173. if (rc) {
  174. printk(KERN_ERR "%s: Error attempting to initialize "
  175. "the persistent file for the dentry with name "
  176. "[%s]; rc = [%d]\n", __func__,
  177. ecryptfs_dentry->d_name.name, rc);
  178. goto out;
  179. }
  180. }
  181. rc = ecryptfs_write_metadata(ecryptfs_dentry);
  182. if (rc) {
  183. printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
  184. goto out;
  185. }
  186. rc = grow_file(ecryptfs_dentry);
  187. if (rc)
  188. printk(KERN_ERR "Error growing file; rc = [%d]\n", rc);
  189. out:
  190. return rc;
  191. }
  192. /**
  193. * ecryptfs_create
  194. * @dir: The inode of the directory in which to create the file.
  195. * @dentry: The eCryptfs dentry
  196. * @mode: The mode of the new file.
  197. * @nd: nameidata
  198. *
  199. * Creates a new file.
  200. *
  201. * Returns zero on success; non-zero on error condition
  202. */
  203. static int
  204. ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
  205. int mode, struct nameidata *nd)
  206. {
  207. int rc;
  208. /* ecryptfs_do_create() calls ecryptfs_interpose() */
  209. rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
  210. if (unlikely(rc)) {
  211. ecryptfs_printk(KERN_WARNING, "Failed to create file in"
  212. "lower filesystem\n");
  213. goto out;
  214. }
  215. /* At this point, a file exists on "disk"; we need to make sure
  216. * that this on disk file is prepared to be an ecryptfs file */
  217. rc = ecryptfs_initialize_file(ecryptfs_dentry);
  218. out:
  219. return rc;
  220. }
  221. /**
  222. * ecryptfs_lookup_and_interpose_lower - Perform a lookup
  223. */
  224. int ecryptfs_lookup_and_interpose_lower(struct dentry *ecryptfs_dentry,
  225. struct dentry *lower_dentry,
  226. struct inode *ecryptfs_dir_inode,
  227. struct nameidata *ecryptfs_nd)
  228. {
  229. struct dentry *lower_dir_dentry;
  230. struct vfsmount *lower_mnt;
  231. struct inode *lower_inode;
  232. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  233. struct ecryptfs_crypt_stat *crypt_stat;
  234. char *page_virt = NULL;
  235. u64 file_size;
  236. int rc = 0;
  237. lower_dir_dentry = lower_dentry->d_parent;
  238. lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(
  239. ecryptfs_dentry->d_parent));
  240. lower_inode = lower_dentry->d_inode;
  241. fsstack_copy_attr_atime(ecryptfs_dir_inode, lower_dir_dentry->d_inode);
  242. BUG_ON(!atomic_read(&lower_dentry->d_count));
  243. ecryptfs_set_dentry_private(ecryptfs_dentry,
  244. kmem_cache_alloc(ecryptfs_dentry_info_cache,
  245. GFP_KERNEL));
  246. if (!ecryptfs_dentry_to_private(ecryptfs_dentry)) {
  247. rc = -ENOMEM;
  248. printk(KERN_ERR "%s: Out of memory whilst attempting "
  249. "to allocate ecryptfs_dentry_info struct\n",
  250. __func__);
  251. goto out_put;
  252. }
  253. ecryptfs_set_dentry_lower(ecryptfs_dentry, lower_dentry);
  254. ecryptfs_set_dentry_lower_mnt(ecryptfs_dentry, lower_mnt);
  255. if (!lower_dentry->d_inode) {
  256. /* We want to add because we couldn't find in lower */
  257. d_add(ecryptfs_dentry, NULL);
  258. goto out;
  259. }
  260. rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
  261. ecryptfs_dir_inode->i_sb,
  262. ECRYPTFS_INTERPOSE_FLAG_D_ADD);
  263. if (rc) {
  264. printk(KERN_ERR "%s: Error interposing; rc = [%d]\n",
  265. __func__, rc);
  266. goto out;
  267. }
  268. if (S_ISDIR(lower_inode->i_mode))
  269. goto out;
  270. if (S_ISLNK(lower_inode->i_mode))
  271. goto out;
  272. if (special_file(lower_inode->i_mode))
  273. goto out;
  274. if (!ecryptfs_nd)
  275. goto out;
  276. /* Released in this function */
  277. page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2, GFP_USER);
  278. if (!page_virt) {
  279. printk(KERN_ERR "%s: Cannot kmem_cache_zalloc() a page\n",
  280. __func__);
  281. rc = -ENOMEM;
  282. goto out;
  283. }
  284. if (!ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->lower_file) {
  285. rc = ecryptfs_init_persistent_file(ecryptfs_dentry);
  286. if (rc) {
  287. printk(KERN_ERR "%s: Error attempting to initialize "
  288. "the persistent file for the dentry with name "
  289. "[%s]; rc = [%d]\n", __func__,
  290. ecryptfs_dentry->d_name.name, rc);
  291. goto out_free_kmem;
  292. }
  293. }
  294. crypt_stat = &ecryptfs_inode_to_private(
  295. ecryptfs_dentry->d_inode)->crypt_stat;
  296. /* TODO: lock for crypt_stat comparison */
  297. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
  298. ecryptfs_set_default_sizes(crypt_stat);
  299. rc = ecryptfs_read_and_validate_header_region(page_virt,
  300. ecryptfs_dentry->d_inode);
  301. if (rc) {
  302. memset(page_virt, 0, PAGE_CACHE_SIZE);
  303. rc = ecryptfs_read_and_validate_xattr_region(page_virt,
  304. ecryptfs_dentry);
  305. if (rc) {
  306. rc = 0;
  307. goto out_free_kmem;
  308. }
  309. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  310. }
  311. mount_crypt_stat = &ecryptfs_superblock_to_private(
  312. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  313. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
  314. if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
  315. file_size = (crypt_stat->metadata_size
  316. + i_size_read(lower_dentry->d_inode));
  317. else
  318. file_size = i_size_read(lower_dentry->d_inode);
  319. } else {
  320. file_size = get_unaligned_be64(page_virt);
  321. }
  322. i_size_write(ecryptfs_dentry->d_inode, (loff_t)file_size);
  323. out_free_kmem:
  324. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  325. goto out;
  326. out_put:
  327. dput(lower_dentry);
  328. mntput(lower_mnt);
  329. d_drop(ecryptfs_dentry);
  330. out:
  331. return rc;
  332. }
  333. /**
  334. * ecryptfs_lookup
  335. * @ecryptfs_dir_inode: The eCryptfs directory inode
  336. * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
  337. * @ecryptfs_nd: nameidata; may be NULL
  338. *
  339. * Find a file on disk. If the file does not exist, then we'll add it to the
  340. * dentry cache and continue on to read it from the disk.
  341. */
  342. static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
  343. struct dentry *ecryptfs_dentry,
  344. struct nameidata *ecryptfs_nd)
  345. {
  346. char *encrypted_and_encoded_name = NULL;
  347. size_t encrypted_and_encoded_name_size;
  348. struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
  349. struct dentry *lower_dir_dentry, *lower_dentry;
  350. int rc = 0;
  351. ecryptfs_dentry->d_op = &ecryptfs_dops;
  352. if ((ecryptfs_dentry->d_name.len == 1
  353. && !strcmp(ecryptfs_dentry->d_name.name, "."))
  354. || (ecryptfs_dentry->d_name.len == 2
  355. && !strcmp(ecryptfs_dentry->d_name.name, ".."))) {
  356. goto out_d_drop;
  357. }
  358. lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
  359. mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
  360. lower_dentry = lookup_one_len(ecryptfs_dentry->d_name.name,
  361. lower_dir_dentry,
  362. ecryptfs_dentry->d_name.len);
  363. mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
  364. if (IS_ERR(lower_dentry)) {
  365. rc = PTR_ERR(lower_dentry);
  366. ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
  367. "[%d] on lower_dentry = [%s]\n", __func__, rc,
  368. encrypted_and_encoded_name);
  369. goto out_d_drop;
  370. }
  371. if (lower_dentry->d_inode)
  372. goto lookup_and_interpose;
  373. mount_crypt_stat = &ecryptfs_superblock_to_private(
  374. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  375. if (!(mount_crypt_stat
  376. && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)))
  377. goto lookup_and_interpose;
  378. dput(lower_dentry);
  379. rc = ecryptfs_encrypt_and_encode_filename(
  380. &encrypted_and_encoded_name, &encrypted_and_encoded_name_size,
  381. NULL, mount_crypt_stat, ecryptfs_dentry->d_name.name,
  382. ecryptfs_dentry->d_name.len);
  383. if (rc) {
  384. printk(KERN_ERR "%s: Error attempting to encrypt and encode "
  385. "filename; rc = [%d]\n", __func__, rc);
  386. goto out_d_drop;
  387. }
  388. mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
  389. lower_dentry = lookup_one_len(encrypted_and_encoded_name,
  390. lower_dir_dentry,
  391. encrypted_and_encoded_name_size - 1);
  392. mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
  393. if (IS_ERR(lower_dentry)) {
  394. rc = PTR_ERR(lower_dentry);
  395. ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
  396. "[%d] on lower_dentry = [%s]\n", __func__, rc,
  397. encrypted_and_encoded_name);
  398. goto out_d_drop;
  399. }
  400. lookup_and_interpose:
  401. rc = ecryptfs_lookup_and_interpose_lower(ecryptfs_dentry, lower_dentry,
  402. ecryptfs_dir_inode,
  403. ecryptfs_nd);
  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. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  763. rc = simple_setsize(inode, ia->ia_size);
  764. if (rc)
  765. goto out;
  766. lower_ia->ia_size = ia->ia_size;
  767. lower_ia->ia_valid |= ATTR_SIZE;
  768. goto out;
  769. }
  770. if (num_zeros) {
  771. char *zeros_virt;
  772. zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
  773. if (!zeros_virt) {
  774. rc = -ENOMEM;
  775. goto out;
  776. }
  777. rc = ecryptfs_write(inode, zeros_virt,
  778. ia->ia_size, num_zeros);
  779. kfree(zeros_virt);
  780. if (rc) {
  781. printk(KERN_ERR "Error attempting to zero out "
  782. "the remainder of the end page on "
  783. "reducing truncate; rc = [%d]\n", rc);
  784. goto out;
  785. }
  786. }
  787. simple_setsize(inode, ia->ia_size);
  788. rc = ecryptfs_write_inode_size_to_metadata(inode);
  789. if (rc) {
  790. printk(KERN_ERR "Problem with "
  791. "ecryptfs_write_inode_size_to_metadata; "
  792. "rc = [%d]\n", rc);
  793. goto out;
  794. }
  795. /* We are reducing the size of the ecryptfs file, and need to
  796. * know if we need to reduce the size of the lower file. */
  797. lower_size_before_truncate =
  798. upper_size_to_lower_size(crypt_stat, i_size);
  799. lower_size_after_truncate =
  800. upper_size_to_lower_size(crypt_stat, ia->ia_size);
  801. if (lower_size_after_truncate < lower_size_before_truncate) {
  802. lower_ia->ia_size = lower_size_after_truncate;
  803. lower_ia->ia_valid |= ATTR_SIZE;
  804. } else
  805. lower_ia->ia_valid &= ~ATTR_SIZE;
  806. }
  807. out:
  808. return rc;
  809. }
  810. /**
  811. * ecryptfs_truncate
  812. * @dentry: The ecryptfs layer dentry
  813. * @new_length: The length to expand the file to
  814. *
  815. * Simple function that handles the truncation of an eCryptfs inode and
  816. * its corresponding lower inode.
  817. *
  818. * Returns zero on success; non-zero otherwise
  819. */
  820. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  821. {
  822. struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
  823. struct iattr lower_ia = { .ia_valid = 0 };
  824. int rc;
  825. rc = truncate_upper(dentry, &ia, &lower_ia);
  826. if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
  827. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  828. mutex_lock(&lower_dentry->d_inode->i_mutex);
  829. rc = notify_change(lower_dentry, &lower_ia);
  830. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  831. }
  832. return rc;
  833. }
  834. static int
  835. ecryptfs_permission(struct inode *inode, int mask)
  836. {
  837. return inode_permission(ecryptfs_inode_to_lower(inode), mask);
  838. }
  839. /**
  840. * ecryptfs_setattr
  841. * @dentry: dentry handle to the inode to modify
  842. * @ia: Structure with flags of what to change and values
  843. *
  844. * Updates the metadata of an inode. If the update is to the size
  845. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  846. * of both the ecryptfs inode and the lower inode.
  847. *
  848. * All other metadata changes will be passed right to the lower filesystem,
  849. * and we will just update our inode to look like the lower.
  850. */
  851. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  852. {
  853. int rc = 0;
  854. struct dentry *lower_dentry;
  855. struct iattr lower_ia;
  856. struct inode *inode;
  857. struct inode *lower_inode;
  858. struct ecryptfs_crypt_stat *crypt_stat;
  859. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  860. if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
  861. ecryptfs_init_crypt_stat(crypt_stat);
  862. inode = dentry->d_inode;
  863. lower_inode = ecryptfs_inode_to_lower(inode);
  864. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  865. mutex_lock(&crypt_stat->cs_mutex);
  866. if (S_ISDIR(dentry->d_inode->i_mode))
  867. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  868. else if (S_ISREG(dentry->d_inode->i_mode)
  869. && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  870. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
  871. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  872. mount_crypt_stat = &ecryptfs_superblock_to_private(
  873. dentry->d_sb)->mount_crypt_stat;
  874. rc = ecryptfs_read_metadata(dentry);
  875. if (rc) {
  876. if (!(mount_crypt_stat->flags
  877. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  878. rc = -EIO;
  879. printk(KERN_WARNING "Either the lower file "
  880. "is not in a valid eCryptfs format, "
  881. "or the key could not be retrieved. "
  882. "Plaintext passthrough mode is not "
  883. "enabled; returning -EIO\n");
  884. mutex_unlock(&crypt_stat->cs_mutex);
  885. goto out;
  886. }
  887. rc = 0;
  888. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  889. }
  890. }
  891. mutex_unlock(&crypt_stat->cs_mutex);
  892. memcpy(&lower_ia, ia, sizeof(lower_ia));
  893. if (ia->ia_valid & ATTR_FILE)
  894. lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
  895. if (ia->ia_valid & ATTR_SIZE) {
  896. rc = truncate_upper(dentry, ia, &lower_ia);
  897. if (rc < 0)
  898. goto out;
  899. }
  900. /*
  901. * mode change is for clearing setuid/setgid bits. Allow lower fs
  902. * to interpret this in its own way.
  903. */
  904. if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  905. lower_ia.ia_valid &= ~ATTR_MODE;
  906. mutex_lock(&lower_dentry->d_inode->i_mutex);
  907. rc = notify_change(lower_dentry, &lower_ia);
  908. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  909. out:
  910. fsstack_copy_attr_all(inode, lower_inode);
  911. return rc;
  912. }
  913. int ecryptfs_getattr_link(struct vfsmount *mnt, struct dentry *dentry,
  914. struct kstat *stat)
  915. {
  916. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  917. int rc = 0;
  918. mount_crypt_stat = &ecryptfs_superblock_to_private(
  919. dentry->d_sb)->mount_crypt_stat;
  920. generic_fillattr(dentry->d_inode, stat);
  921. if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
  922. char *target;
  923. size_t targetsiz;
  924. rc = ecryptfs_readlink_lower(dentry, &target, &targetsiz);
  925. if (!rc) {
  926. kfree(target);
  927. stat->size = targetsiz;
  928. }
  929. }
  930. return rc;
  931. }
  932. int ecryptfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  933. struct kstat *stat)
  934. {
  935. struct kstat lower_stat;
  936. int rc;
  937. rc = vfs_getattr(ecryptfs_dentry_to_lower_mnt(dentry),
  938. ecryptfs_dentry_to_lower(dentry), &lower_stat);
  939. if (!rc) {
  940. generic_fillattr(dentry->d_inode, stat);
  941. stat->blocks = lower_stat.blocks;
  942. }
  943. return rc;
  944. }
  945. int
  946. ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  947. size_t size, int flags)
  948. {
  949. int rc = 0;
  950. struct dentry *lower_dentry;
  951. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  952. if (!lower_dentry->d_inode->i_op->setxattr) {
  953. rc = -EOPNOTSUPP;
  954. goto out;
  955. }
  956. mutex_lock(&lower_dentry->d_inode->i_mutex);
  957. rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
  958. size, flags);
  959. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  960. out:
  961. return rc;
  962. }
  963. ssize_t
  964. ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
  965. void *value, size_t size)
  966. {
  967. int rc = 0;
  968. if (!lower_dentry->d_inode->i_op->getxattr) {
  969. rc = -EOPNOTSUPP;
  970. goto out;
  971. }
  972. mutex_lock(&lower_dentry->d_inode->i_mutex);
  973. rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
  974. size);
  975. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  976. out:
  977. return rc;
  978. }
  979. static ssize_t
  980. ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
  981. size_t size)
  982. {
  983. return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
  984. value, size);
  985. }
  986. static ssize_t
  987. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  988. {
  989. int rc = 0;
  990. struct dentry *lower_dentry;
  991. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  992. if (!lower_dentry->d_inode->i_op->listxattr) {
  993. rc = -EOPNOTSUPP;
  994. goto out;
  995. }
  996. mutex_lock(&lower_dentry->d_inode->i_mutex);
  997. rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
  998. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  999. out:
  1000. return rc;
  1001. }
  1002. static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
  1003. {
  1004. int rc = 0;
  1005. struct dentry *lower_dentry;
  1006. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  1007. if (!lower_dentry->d_inode->i_op->removexattr) {
  1008. rc = -EOPNOTSUPP;
  1009. goto out;
  1010. }
  1011. mutex_lock(&lower_dentry->d_inode->i_mutex);
  1012. rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
  1013. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  1014. out:
  1015. return rc;
  1016. }
  1017. int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
  1018. {
  1019. if ((ecryptfs_inode_to_lower(inode)
  1020. == (struct inode *)candidate_lower_inode))
  1021. return 1;
  1022. else
  1023. return 0;
  1024. }
  1025. int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
  1026. {
  1027. ecryptfs_init_inode(inode, (struct inode *)lower_inode);
  1028. return 0;
  1029. }
  1030. const struct inode_operations ecryptfs_symlink_iops = {
  1031. .readlink = ecryptfs_readlink,
  1032. .follow_link = ecryptfs_follow_link,
  1033. .put_link = ecryptfs_put_link,
  1034. .permission = ecryptfs_permission,
  1035. .setattr = ecryptfs_setattr,
  1036. .getattr = ecryptfs_getattr_link,
  1037. .setxattr = ecryptfs_setxattr,
  1038. .getxattr = ecryptfs_getxattr,
  1039. .listxattr = ecryptfs_listxattr,
  1040. .removexattr = ecryptfs_removexattr
  1041. };
  1042. const struct inode_operations ecryptfs_dir_iops = {
  1043. .create = ecryptfs_create,
  1044. .lookup = ecryptfs_lookup,
  1045. .link = ecryptfs_link,
  1046. .unlink = ecryptfs_unlink,
  1047. .symlink = ecryptfs_symlink,
  1048. .mkdir = ecryptfs_mkdir,
  1049. .rmdir = ecryptfs_rmdir,
  1050. .mknod = ecryptfs_mknod,
  1051. .rename = ecryptfs_rename,
  1052. .permission = ecryptfs_permission,
  1053. .setattr = ecryptfs_setattr,
  1054. .setxattr = ecryptfs_setxattr,
  1055. .getxattr = ecryptfs_getxattr,
  1056. .listxattr = ecryptfs_listxattr,
  1057. .removexattr = ecryptfs_removexattr
  1058. };
  1059. const struct inode_operations ecryptfs_main_iops = {
  1060. .permission = ecryptfs_permission,
  1061. .setattr = ecryptfs_setattr,
  1062. .getattr = ecryptfs_getattr,
  1063. .setxattr = ecryptfs_setxattr,
  1064. .getxattr = ecryptfs_getxattr,
  1065. .listxattr = ecryptfs_listxattr,
  1066. .removexattr = ecryptfs_removexattr
  1067. };