inode.c 35 KB

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