inode.c 35 KB

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