inode.c 30 KB

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