inode.c 34 KB

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