inode.c 30 KB

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