inode.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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-2006 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 "ecryptfs_kernel.h"
  34. static struct dentry *lock_parent(struct dentry *dentry)
  35. {
  36. struct dentry *dir;
  37. dir = dget(dentry->d_parent);
  38. mutex_lock(&(dir->d_inode->i_mutex));
  39. return dir;
  40. }
  41. static void unlock_parent(struct dentry *dentry)
  42. {
  43. mutex_unlock(&(dentry->d_parent->d_inode->i_mutex));
  44. dput(dentry->d_parent);
  45. }
  46. static void unlock_dir(struct dentry *dir)
  47. {
  48. mutex_unlock(&dir->d_inode->i_mutex);
  49. dput(dir);
  50. }
  51. /**
  52. * ecryptfs_create_underlying_file
  53. * @lower_dir_inode: inode of the parent in the lower fs of the new file
  54. * @lower_dentry: New file's dentry in the lower fs
  55. * @ecryptfs_dentry: New file's dentry in ecryptfs
  56. * @mode: The mode of the new file
  57. * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
  58. *
  59. * Creates the file in the lower file system.
  60. *
  61. * Returns zero on success; non-zero on error condition
  62. */
  63. static int
  64. ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
  65. struct dentry *dentry, int mode,
  66. struct nameidata *nd)
  67. {
  68. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  69. struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
  70. struct dentry *dentry_save;
  71. struct vfsmount *vfsmount_save;
  72. int rc;
  73. dentry_save = nd->dentry;
  74. vfsmount_save = nd->mnt;
  75. nd->dentry = lower_dentry;
  76. nd->mnt = lower_mnt;
  77. rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
  78. nd->dentry = dentry_save;
  79. nd->mnt = vfsmount_save;
  80. return rc;
  81. }
  82. /**
  83. * ecryptfs_do_create
  84. * @directory_inode: inode of the new file's dentry's parent in ecryptfs
  85. * @ecryptfs_dentry: New file's dentry in ecryptfs
  86. * @mode: The mode of the new file
  87. * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
  88. *
  89. * Creates the underlying file and the eCryptfs inode which will link to
  90. * it. It will also update the eCryptfs directory inode to mimic the
  91. * stat of the lower directory inode.
  92. *
  93. * Returns zero on success; non-zero on error condition
  94. */
  95. static int
  96. ecryptfs_do_create(struct inode *directory_inode,
  97. struct dentry *ecryptfs_dentry, int mode,
  98. struct nameidata *nd)
  99. {
  100. int rc;
  101. struct dentry *lower_dentry;
  102. struct dentry *lower_dir_dentry;
  103. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  104. lower_dir_dentry = lock_parent(lower_dentry);
  105. if (unlikely(IS_ERR(lower_dir_dentry))) {
  106. ecryptfs_printk(KERN_ERR, "Error locking directory of "
  107. "dentry\n");
  108. rc = PTR_ERR(lower_dir_dentry);
  109. goto out;
  110. }
  111. rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
  112. ecryptfs_dentry, mode, nd);
  113. if (unlikely(rc)) {
  114. ecryptfs_printk(KERN_ERR,
  115. "Failure to create underlying file\n");
  116. goto out_lock;
  117. }
  118. rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
  119. directory_inode->i_sb, 0);
  120. if (rc) {
  121. ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
  122. goto out_lock;
  123. }
  124. fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
  125. fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
  126. out_lock:
  127. unlock_dir(lower_dir_dentry);
  128. out:
  129. return rc;
  130. }
  131. /**
  132. * grow_file
  133. * @ecryptfs_dentry: the ecryptfs dentry
  134. * @lower_file: The lower file
  135. * @inode: The ecryptfs inode
  136. * @lower_inode: The lower inode
  137. *
  138. * This is the code which will grow the file to its correct size.
  139. */
  140. static int grow_file(struct dentry *ecryptfs_dentry, struct file *lower_file,
  141. struct inode *inode, struct inode *lower_inode)
  142. {
  143. int rc = 0;
  144. struct file fake_file;
  145. struct ecryptfs_file_info tmp_file_info;
  146. memset(&fake_file, 0, sizeof(fake_file));
  147. fake_file.f_path.dentry = ecryptfs_dentry;
  148. memset(&tmp_file_info, 0, sizeof(tmp_file_info));
  149. ecryptfs_set_file_private(&fake_file, &tmp_file_info);
  150. ecryptfs_set_file_lower(&fake_file, lower_file);
  151. rc = ecryptfs_fill_zeros(&fake_file, 1);
  152. if (rc) {
  153. ECRYPTFS_SET_FLAG(
  154. ecryptfs_inode_to_private(inode)->crypt_stat.flags,
  155. ECRYPTFS_SECURITY_WARNING);
  156. ecryptfs_printk(KERN_WARNING, "Error attempting to fill zeros "
  157. "in file; rc = [%d]\n", rc);
  158. goto out;
  159. }
  160. i_size_write(inode, 0);
  161. ecryptfs_write_inode_size_to_header(lower_file, lower_inode, inode);
  162. ECRYPTFS_SET_FLAG(ecryptfs_inode_to_private(inode)->crypt_stat.flags,
  163. ECRYPTFS_NEW_FILE);
  164. out:
  165. return rc;
  166. }
  167. /**
  168. * ecryptfs_initialize_file
  169. *
  170. * Cause the file to be changed from a basic empty file to an ecryptfs
  171. * file with a header and first data page.
  172. *
  173. * Returns zero on success
  174. */
  175. static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
  176. {
  177. int rc = 0;
  178. int lower_flags;
  179. struct ecryptfs_crypt_stat *crypt_stat;
  180. struct dentry *lower_dentry;
  181. struct file *lower_file;
  182. struct inode *inode, *lower_inode;
  183. struct vfsmount *lower_mnt;
  184. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  185. ecryptfs_printk(KERN_DEBUG, "lower_dentry->d_name.name = [%s]\n",
  186. lower_dentry->d_name.name);
  187. inode = ecryptfs_dentry->d_inode;
  188. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  189. lower_flags = ((O_CREAT | O_WRONLY | O_TRUNC) & O_ACCMODE) | O_RDWR;
  190. #if BITS_PER_LONG != 32
  191. lower_flags |= O_LARGEFILE;
  192. #endif
  193. lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
  194. /* Corresponding fput() at end of this function */
  195. if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
  196. lower_flags))) {
  197. ecryptfs_printk(KERN_ERR,
  198. "Error opening dentry; rc = [%i]\n", rc);
  199. goto out;
  200. }
  201. lower_inode = lower_dentry->d_inode;
  202. if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
  203. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  204. ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
  205. goto out_fput;
  206. }
  207. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE);
  208. ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
  209. rc = ecryptfs_new_file_context(ecryptfs_dentry);
  210. if (rc) {
  211. ecryptfs_printk(KERN_DEBUG, "Error creating new file "
  212. "context\n");
  213. goto out_fput;
  214. }
  215. rc = ecryptfs_write_headers(ecryptfs_dentry, lower_file);
  216. if (rc) {
  217. ecryptfs_printk(KERN_DEBUG, "Error writing headers\n");
  218. goto out_fput;
  219. }
  220. rc = grow_file(ecryptfs_dentry, lower_file, inode, lower_inode);
  221. out_fput:
  222. if ((rc = ecryptfs_close_lower_file(lower_file)))
  223. printk(KERN_ERR "Error closing lower_file\n");
  224. out:
  225. return rc;
  226. }
  227. /**
  228. * ecryptfs_create
  229. * @dir: The inode of the directory in which to create the file.
  230. * @dentry: The eCryptfs dentry
  231. * @mode: The mode of the new file.
  232. * @nd: nameidata
  233. *
  234. * Creates a new file.
  235. *
  236. * Returns zero on success; non-zero on error condition
  237. */
  238. static int
  239. ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
  240. int mode, struct nameidata *nd)
  241. {
  242. int rc;
  243. rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
  244. if (unlikely(rc)) {
  245. ecryptfs_printk(KERN_WARNING, "Failed to create file in"
  246. "lower filesystem\n");
  247. goto out;
  248. }
  249. /* At this point, a file exists on "disk"; we need to make sure
  250. * that this on disk file is prepared to be an ecryptfs file */
  251. rc = ecryptfs_initialize_file(ecryptfs_dentry);
  252. out:
  253. return rc;
  254. }
  255. /**
  256. * ecryptfs_lookup
  257. * @dir: inode
  258. * @dentry: The dentry
  259. * @nd: nameidata, may be NULL
  260. *
  261. * Find a file on disk. If the file does not exist, then we'll add it to the
  262. * dentry cache and continue on to read it from the disk.
  263. */
  264. static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
  265. struct nameidata *nd)
  266. {
  267. int rc = 0;
  268. struct dentry *lower_dir_dentry;
  269. struct dentry *lower_dentry;
  270. struct vfsmount *lower_mnt;
  271. char *encoded_name;
  272. unsigned int encoded_namelen;
  273. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  274. char *page_virt = NULL;
  275. struct inode *lower_inode;
  276. u64 file_size;
  277. lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
  278. dentry->d_op = &ecryptfs_dops;
  279. if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, "."))
  280. || (dentry->d_name.len == 2
  281. && !strcmp(dentry->d_name.name, ".."))) {
  282. d_drop(dentry);
  283. goto out;
  284. }
  285. encoded_namelen = ecryptfs_encode_filename(crypt_stat,
  286. dentry->d_name.name,
  287. dentry->d_name.len,
  288. &encoded_name);
  289. if (encoded_namelen < 0) {
  290. rc = encoded_namelen;
  291. d_drop(dentry);
  292. goto out;
  293. }
  294. ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen "
  295. "= [%d]\n", encoded_name, encoded_namelen);
  296. lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry,
  297. encoded_namelen - 1);
  298. kfree(encoded_name);
  299. if (IS_ERR(lower_dentry)) {
  300. ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
  301. rc = PTR_ERR(lower_dentry);
  302. d_drop(dentry);
  303. goto out;
  304. }
  305. lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
  306. ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->"
  307. "d_name.name = [%s]\n", lower_dentry,
  308. lower_dentry->d_name.name);
  309. lower_inode = lower_dentry->d_inode;
  310. fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode);
  311. BUG_ON(!atomic_read(&lower_dentry->d_count));
  312. ecryptfs_set_dentry_private(dentry,
  313. kmem_cache_alloc(ecryptfs_dentry_info_cache,
  314. GFP_KERNEL));
  315. if (!ecryptfs_dentry_to_private(dentry)) {
  316. rc = -ENOMEM;
  317. ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting "
  318. "to allocate ecryptfs_dentry_info struct\n");
  319. goto out_dput;
  320. }
  321. ecryptfs_set_dentry_lower(dentry, lower_dentry);
  322. ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
  323. if (!lower_dentry->d_inode) {
  324. /* We want to add because we couldn't find in lower */
  325. d_add(dentry, NULL);
  326. goto out;
  327. }
  328. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 1);
  329. if (rc) {
  330. ecryptfs_printk(KERN_ERR, "Error interposing\n");
  331. goto out_dput;
  332. }
  333. if (S_ISDIR(lower_inode->i_mode)) {
  334. ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
  335. goto out;
  336. }
  337. if (S_ISLNK(lower_inode->i_mode)) {
  338. ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
  339. goto out;
  340. }
  341. if (!nd) {
  342. ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave"
  343. "as we *think* we are about to unlink\n");
  344. goto out;
  345. }
  346. /* Released in this function */
  347. page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2,
  348. GFP_USER);
  349. if (!page_virt) {
  350. rc = -ENOMEM;
  351. ecryptfs_printk(KERN_ERR,
  352. "Cannot ecryptfs_kmalloc a page\n");
  353. goto out_dput;
  354. }
  355. rc = ecryptfs_read_header_region(page_virt, lower_dentry, nd->mnt);
  356. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  357. if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_POLICY_APPLIED))
  358. ecryptfs_set_default_sizes(crypt_stat);
  359. if (rc) {
  360. rc = 0;
  361. ecryptfs_printk(KERN_WARNING, "Error reading header region;"
  362. " assuming unencrypted\n");
  363. } else {
  364. if (!contains_ecryptfs_marker(page_virt
  365. + ECRYPTFS_FILE_SIZE_BYTES)) {
  366. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  367. goto out;
  368. }
  369. memcpy(&file_size, page_virt, sizeof(file_size));
  370. file_size = be64_to_cpu(file_size);
  371. i_size_write(dentry->d_inode, (loff_t)file_size);
  372. }
  373. kmem_cache_free(ecryptfs_header_cache_2, page_virt);
  374. goto out;
  375. out_dput:
  376. dput(lower_dentry);
  377. d_drop(dentry);
  378. out:
  379. return ERR_PTR(rc);
  380. }
  381. static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
  382. struct dentry *new_dentry)
  383. {
  384. struct dentry *lower_old_dentry;
  385. struct dentry *lower_new_dentry;
  386. struct dentry *lower_dir_dentry;
  387. u64 file_size_save;
  388. int rc;
  389. file_size_save = i_size_read(old_dentry->d_inode);
  390. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  391. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  392. dget(lower_old_dentry);
  393. dget(lower_new_dentry);
  394. lower_dir_dentry = lock_parent(lower_new_dentry);
  395. rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
  396. lower_new_dentry);
  397. if (rc || !lower_new_dentry->d_inode)
  398. goto out_lock;
  399. rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
  400. if (rc)
  401. goto out_lock;
  402. fsstack_copy_attr_times(dir, lower_new_dentry->d_inode);
  403. fsstack_copy_inode_size(dir, lower_new_dentry->d_inode);
  404. old_dentry->d_inode->i_nlink =
  405. ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
  406. i_size_write(new_dentry->d_inode, file_size_save);
  407. out_lock:
  408. unlock_dir(lower_dir_dentry);
  409. dput(lower_new_dentry);
  410. dput(lower_old_dentry);
  411. d_drop(lower_old_dentry);
  412. d_drop(new_dentry);
  413. d_drop(old_dentry);
  414. return rc;
  415. }
  416. static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
  417. {
  418. int rc = 0;
  419. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  420. struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
  421. lock_parent(lower_dentry);
  422. rc = vfs_unlink(lower_dir_inode, lower_dentry);
  423. if (rc) {
  424. printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
  425. goto out_unlock;
  426. }
  427. fsstack_copy_attr_times(dir, lower_dir_inode);
  428. dentry->d_inode->i_nlink =
  429. ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
  430. dentry->d_inode->i_ctime = dir->i_ctime;
  431. out_unlock:
  432. unlock_parent(lower_dentry);
  433. return rc;
  434. }
  435. static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
  436. const char *symname)
  437. {
  438. int rc;
  439. struct dentry *lower_dentry;
  440. struct dentry *lower_dir_dentry;
  441. umode_t mode;
  442. char *encoded_symname;
  443. unsigned int encoded_symlen;
  444. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  445. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  446. dget(lower_dentry);
  447. lower_dir_dentry = lock_parent(lower_dentry);
  448. mode = S_IALLUGO;
  449. encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
  450. strlen(symname),
  451. &encoded_symname);
  452. if (encoded_symlen < 0) {
  453. rc = encoded_symlen;
  454. goto out_lock;
  455. }
  456. rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
  457. encoded_symname, mode);
  458. kfree(encoded_symname);
  459. if (rc || !lower_dentry->d_inode)
  460. goto out_lock;
  461. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  462. if (rc)
  463. goto out_lock;
  464. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  465. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  466. out_lock:
  467. unlock_dir(lower_dir_dentry);
  468. dput(lower_dentry);
  469. if (!dentry->d_inode)
  470. d_drop(dentry);
  471. return rc;
  472. }
  473. static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  474. {
  475. int rc;
  476. struct dentry *lower_dentry;
  477. struct dentry *lower_dir_dentry;
  478. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  479. lower_dir_dentry = lock_parent(lower_dentry);
  480. rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
  481. if (rc || !lower_dentry->d_inode)
  482. goto out;
  483. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  484. if (rc)
  485. goto out;
  486. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  487. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  488. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  489. out:
  490. unlock_dir(lower_dir_dentry);
  491. if (!dentry->d_inode)
  492. d_drop(dentry);
  493. return rc;
  494. }
  495. static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
  496. {
  497. struct dentry *lower_dentry;
  498. struct dentry *lower_dir_dentry;
  499. int rc;
  500. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  501. dget(dentry);
  502. lower_dir_dentry = lock_parent(lower_dentry);
  503. dget(lower_dentry);
  504. rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
  505. dput(lower_dentry);
  506. if (!rc)
  507. d_delete(lower_dentry);
  508. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  509. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  510. unlock_dir(lower_dir_dentry);
  511. if (!rc)
  512. d_drop(dentry);
  513. dput(dentry);
  514. return rc;
  515. }
  516. static int
  517. ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  518. {
  519. int rc;
  520. struct dentry *lower_dentry;
  521. struct dentry *lower_dir_dentry;
  522. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  523. lower_dir_dentry = lock_parent(lower_dentry);
  524. rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
  525. if (rc || !lower_dentry->d_inode)
  526. goto out;
  527. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  528. if (rc)
  529. goto out;
  530. fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
  531. fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
  532. out:
  533. unlock_dir(lower_dir_dentry);
  534. if (!dentry->d_inode)
  535. d_drop(dentry);
  536. return rc;
  537. }
  538. static int
  539. ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  540. struct inode *new_dir, struct dentry *new_dentry)
  541. {
  542. int rc;
  543. struct dentry *lower_old_dentry;
  544. struct dentry *lower_new_dentry;
  545. struct dentry *lower_old_dir_dentry;
  546. struct dentry *lower_new_dir_dentry;
  547. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  548. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  549. dget(lower_old_dentry);
  550. dget(lower_new_dentry);
  551. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  552. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  553. lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  554. rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
  555. lower_new_dir_dentry->d_inode, lower_new_dentry);
  556. if (rc)
  557. goto out_lock;
  558. fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
  559. if (new_dir != old_dir)
  560. fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
  561. out_lock:
  562. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  563. dput(lower_new_dentry->d_parent);
  564. dput(lower_old_dentry->d_parent);
  565. dput(lower_new_dentry);
  566. dput(lower_old_dentry);
  567. return rc;
  568. }
  569. static int
  570. ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
  571. {
  572. int rc;
  573. struct dentry *lower_dentry;
  574. char *decoded_name;
  575. char *lower_buf;
  576. mm_segment_t old_fs;
  577. struct ecryptfs_crypt_stat *crypt_stat;
  578. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  579. if (!lower_dentry->d_inode->i_op ||
  580. !lower_dentry->d_inode->i_op->readlink) {
  581. rc = -EINVAL;
  582. goto out;
  583. }
  584. /* Released in this function */
  585. lower_buf = kmalloc(bufsiz, GFP_KERNEL);
  586. if (lower_buf == NULL) {
  587. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  588. rc = -ENOMEM;
  589. goto out;
  590. }
  591. old_fs = get_fs();
  592. set_fs(get_ds());
  593. ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
  594. "lower_dentry->d_name.name = [%s]\n",
  595. lower_dentry->d_name.name);
  596. rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
  597. (char __user *)lower_buf,
  598. bufsiz);
  599. set_fs(old_fs);
  600. if (rc >= 0) {
  601. crypt_stat = NULL;
  602. rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
  603. &decoded_name);
  604. if (rc == -ENOMEM)
  605. goto out_free_lower_buf;
  606. if (rc > 0) {
  607. ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
  608. "to userspace: [%*s]\n", rc,
  609. decoded_name);
  610. if (copy_to_user(buf, decoded_name, rc))
  611. rc = -EFAULT;
  612. }
  613. kfree(decoded_name);
  614. fsstack_copy_attr_atime(dentry->d_inode,
  615. lower_dentry->d_inode);
  616. }
  617. out_free_lower_buf:
  618. kfree(lower_buf);
  619. out:
  620. return rc;
  621. }
  622. static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  623. {
  624. char *buf;
  625. int len = PAGE_SIZE, rc;
  626. mm_segment_t old_fs;
  627. /* Released in ecryptfs_put_link(); only release here on error */
  628. buf = kmalloc(len, GFP_KERNEL);
  629. if (!buf) {
  630. rc = -ENOMEM;
  631. goto out;
  632. }
  633. old_fs = get_fs();
  634. set_fs(get_ds());
  635. ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
  636. "dentry->d_name.name = [%s]\n", dentry->d_name.name);
  637. rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
  638. buf[rc] = '\0';
  639. set_fs(old_fs);
  640. if (rc < 0)
  641. goto out_free;
  642. rc = 0;
  643. nd_set_link(nd, buf);
  644. goto out;
  645. out_free:
  646. kfree(buf);
  647. out:
  648. return ERR_PTR(rc);
  649. }
  650. static void
  651. ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
  652. {
  653. /* Free the char* */
  654. kfree(nd_get_link(nd));
  655. }
  656. /**
  657. * upper_size_to_lower_size
  658. * @crypt_stat: Crypt_stat associated with file
  659. * @upper_size: Size of the upper file
  660. *
  661. * Calculate the requried size of the lower file based on the
  662. * specified size of the upper file. This calculation is based on the
  663. * number of headers in the underlying file and the extent size.
  664. *
  665. * Returns Calculated size of the lower file.
  666. */
  667. static loff_t
  668. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  669. loff_t upper_size)
  670. {
  671. loff_t lower_size;
  672. lower_size = ( crypt_stat->header_extent_size
  673. * crypt_stat->num_header_extents_at_front );
  674. if (upper_size != 0) {
  675. loff_t num_extents;
  676. num_extents = upper_size >> crypt_stat->extent_shift;
  677. if (upper_size & ~crypt_stat->extent_mask)
  678. num_extents++;
  679. lower_size += (num_extents * crypt_stat->extent_size);
  680. }
  681. return lower_size;
  682. }
  683. /**
  684. * ecryptfs_truncate
  685. * @dentry: The ecryptfs layer dentry
  686. * @new_length: The length to expand the file to
  687. *
  688. * Function to handle truncations modifying the size of the file. Note
  689. * that the file sizes are interpolated. When expanding, we are simply
  690. * writing strings of 0's out. When truncating, we need to modify the
  691. * underlying file size according to the page index interpolations.
  692. *
  693. * Returns zero on success; non-zero otherwise
  694. */
  695. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  696. {
  697. int rc = 0;
  698. struct inode *inode = dentry->d_inode;
  699. struct dentry *lower_dentry;
  700. struct vfsmount *lower_mnt;
  701. struct file fake_ecryptfs_file, *lower_file = NULL;
  702. struct ecryptfs_crypt_stat *crypt_stat;
  703. loff_t i_size = i_size_read(inode);
  704. loff_t lower_size_before_truncate;
  705. loff_t lower_size_after_truncate;
  706. if (unlikely((new_length == i_size)))
  707. goto out;
  708. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  709. /* Set up a fake ecryptfs file, this is used to interface with
  710. * the file in the underlying filesystem so that the
  711. * truncation has an effect there as well. */
  712. memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
  713. fake_ecryptfs_file.f_path.dentry = dentry;
  714. /* Released at out_free: label */
  715. ecryptfs_set_file_private(&fake_ecryptfs_file,
  716. kmem_cache_alloc(ecryptfs_file_info_cache,
  717. GFP_KERNEL));
  718. if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
  719. rc = -ENOMEM;
  720. goto out;
  721. }
  722. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  723. /* This dget & mntget is released through fput at out_fput: */
  724. lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
  725. if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
  726. O_RDWR))) {
  727. ecryptfs_printk(KERN_ERR,
  728. "Error opening dentry; rc = [%i]\n", rc);
  729. goto out_free;
  730. }
  731. ecryptfs_set_file_lower(&fake_ecryptfs_file, lower_file);
  732. /* Switch on growing or shrinking file */
  733. if (new_length > i_size) {
  734. rc = ecryptfs_fill_zeros(&fake_ecryptfs_file, new_length);
  735. if (rc) {
  736. ecryptfs_printk(KERN_ERR,
  737. "Problem with fill_zeros\n");
  738. goto out_fput;
  739. }
  740. i_size_write(inode, new_length);
  741. rc = ecryptfs_write_inode_size_to_header(lower_file,
  742. lower_dentry->d_inode,
  743. inode);
  744. if (rc) {
  745. ecryptfs_printk(KERN_ERR,
  746. "Problem with ecryptfs_write"
  747. "_inode_size\n");
  748. goto out_fput;
  749. }
  750. } else { /* new_length < i_size_read(inode) */
  751. vmtruncate(inode, new_length);
  752. ecryptfs_write_inode_size_to_header(lower_file,
  753. lower_dentry->d_inode,
  754. inode);
  755. /* We are reducing the size of the ecryptfs file, and need to
  756. * know if we need to reduce the size of the lower file. */
  757. lower_size_before_truncate =
  758. upper_size_to_lower_size(crypt_stat, i_size);
  759. lower_size_after_truncate =
  760. upper_size_to_lower_size(crypt_stat, new_length);
  761. if (lower_size_after_truncate < lower_size_before_truncate)
  762. vmtruncate(lower_dentry->d_inode,
  763. lower_size_after_truncate);
  764. }
  765. /* Update the access times */
  766. lower_dentry->d_inode->i_mtime = lower_dentry->d_inode->i_ctime
  767. = CURRENT_TIME;
  768. mark_inode_dirty_sync(inode);
  769. out_fput:
  770. if ((rc = ecryptfs_close_lower_file(lower_file)))
  771. printk(KERN_ERR "Error closing lower_file\n");
  772. out_free:
  773. if (ecryptfs_file_to_private(&fake_ecryptfs_file))
  774. kmem_cache_free(ecryptfs_file_info_cache,
  775. ecryptfs_file_to_private(&fake_ecryptfs_file));
  776. out:
  777. return rc;
  778. }
  779. static int
  780. ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  781. {
  782. int rc;
  783. if (nd) {
  784. struct vfsmount *vfsmnt_save = nd->mnt;
  785. struct dentry *dentry_save = nd->dentry;
  786. nd->mnt = ecryptfs_dentry_to_lower_mnt(nd->dentry);
  787. nd->dentry = ecryptfs_dentry_to_lower(nd->dentry);
  788. rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
  789. nd->mnt = vfsmnt_save;
  790. nd->dentry = dentry_save;
  791. } else
  792. rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
  793. return rc;
  794. }
  795. /**
  796. * ecryptfs_setattr
  797. * @dentry: dentry handle to the inode to modify
  798. * @ia: Structure with flags of what to change and values
  799. *
  800. * Updates the metadata of an inode. If the update is to the size
  801. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  802. * of both the ecryptfs inode and the lower inode.
  803. *
  804. * All other metadata changes will be passed right to the lower filesystem,
  805. * and we will just update our inode to look like the lower.
  806. */
  807. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  808. {
  809. int rc = 0;
  810. struct dentry *lower_dentry;
  811. struct inode *inode;
  812. struct inode *lower_inode;
  813. struct ecryptfs_crypt_stat *crypt_stat;
  814. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  815. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  816. inode = dentry->d_inode;
  817. lower_inode = ecryptfs_inode_to_lower(inode);
  818. if (ia->ia_valid & ATTR_SIZE) {
  819. ecryptfs_printk(KERN_DEBUG,
  820. "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
  821. ia->ia_valid, ATTR_SIZE);
  822. rc = ecryptfs_truncate(dentry, ia->ia_size);
  823. /* ecryptfs_truncate handles resizing of the lower file */
  824. ia->ia_valid &= ~ATTR_SIZE;
  825. ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
  826. ia->ia_valid);
  827. if (rc < 0)
  828. goto out;
  829. }
  830. rc = notify_change(lower_dentry, ia);
  831. out:
  832. fsstack_copy_attr_all(inode, lower_inode, NULL);
  833. return rc;
  834. }
  835. static int
  836. ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  837. size_t size, int flags)
  838. {
  839. int rc = 0;
  840. struct dentry *lower_dentry;
  841. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  842. if (!lower_dentry->d_inode->i_op->setxattr) {
  843. rc = -ENOSYS;
  844. goto out;
  845. }
  846. mutex_lock(&lower_dentry->d_inode->i_mutex);
  847. rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
  848. size, flags);
  849. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  850. out:
  851. return rc;
  852. }
  853. static ssize_t
  854. ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
  855. size_t size)
  856. {
  857. int rc = 0;
  858. struct dentry *lower_dentry;
  859. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  860. if (!lower_dentry->d_inode->i_op->getxattr) {
  861. rc = -ENOSYS;
  862. goto out;
  863. }
  864. mutex_lock(&lower_dentry->d_inode->i_mutex);
  865. rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
  866. size);
  867. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  868. out:
  869. return rc;
  870. }
  871. static ssize_t
  872. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  873. {
  874. int rc = 0;
  875. struct dentry *lower_dentry;
  876. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  877. if (!lower_dentry->d_inode->i_op->listxattr) {
  878. rc = -ENOSYS;
  879. goto out;
  880. }
  881. mutex_lock(&lower_dentry->d_inode->i_mutex);
  882. rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
  883. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  884. out:
  885. return rc;
  886. }
  887. static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
  888. {
  889. int rc = 0;
  890. struct dentry *lower_dentry;
  891. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  892. if (!lower_dentry->d_inode->i_op->removexattr) {
  893. rc = -ENOSYS;
  894. goto out;
  895. }
  896. mutex_lock(&lower_dentry->d_inode->i_mutex);
  897. rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
  898. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  899. out:
  900. return rc;
  901. }
  902. int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
  903. {
  904. if ((ecryptfs_inode_to_lower(inode)
  905. == (struct inode *)candidate_lower_inode))
  906. return 1;
  907. else
  908. return 0;
  909. }
  910. int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
  911. {
  912. ecryptfs_init_inode(inode, (struct inode *)lower_inode);
  913. return 0;
  914. }
  915. struct inode_operations ecryptfs_symlink_iops = {
  916. .readlink = ecryptfs_readlink,
  917. .follow_link = ecryptfs_follow_link,
  918. .put_link = ecryptfs_put_link,
  919. .permission = ecryptfs_permission,
  920. .setattr = ecryptfs_setattr,
  921. .setxattr = ecryptfs_setxattr,
  922. .getxattr = ecryptfs_getxattr,
  923. .listxattr = ecryptfs_listxattr,
  924. .removexattr = ecryptfs_removexattr
  925. };
  926. struct inode_operations ecryptfs_dir_iops = {
  927. .create = ecryptfs_create,
  928. .lookup = ecryptfs_lookup,
  929. .link = ecryptfs_link,
  930. .unlink = ecryptfs_unlink,
  931. .symlink = ecryptfs_symlink,
  932. .mkdir = ecryptfs_mkdir,
  933. .rmdir = ecryptfs_rmdir,
  934. .mknod = ecryptfs_mknod,
  935. .rename = ecryptfs_rename,
  936. .permission = ecryptfs_permission,
  937. .setattr = ecryptfs_setattr,
  938. .setxattr = ecryptfs_setxattr,
  939. .getxattr = ecryptfs_getxattr,
  940. .listxattr = ecryptfs_listxattr,
  941. .removexattr = ecryptfs_removexattr
  942. };
  943. struct inode_operations ecryptfs_main_iops = {
  944. .permission = ecryptfs_permission,
  945. .setattr = ecryptfs_setattr,
  946. .setxattr = ecryptfs_setxattr,
  947. .getxattr = ecryptfs_getxattr,
  948. .listxattr = ecryptfs_listxattr,
  949. .removexattr = ecryptfs_removexattr
  950. };