inode.c 33 KB

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