inode.c 35 KB

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