inode.c 35 KB

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