inode.c 30 KB

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