inode.c 34 KB

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