inode.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. SLAB_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. SLAB_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(new_dentry);
  449. d_drop(old_dentry);
  450. return rc;
  451. }
  452. static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
  453. {
  454. int rc = 0;
  455. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  456. struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
  457. lock_parent(lower_dentry);
  458. rc = vfs_unlink(lower_dir_inode, lower_dentry);
  459. if (rc) {
  460. ecryptfs_printk(KERN_ERR, "Error in vfs_unlink\n");
  461. goto out_unlock;
  462. }
  463. ecryptfs_copy_attr_times(dir, lower_dir_inode);
  464. dentry->d_inode->i_nlink =
  465. ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
  466. dentry->d_inode->i_ctime = dir->i_ctime;
  467. out_unlock:
  468. unlock_parent(lower_dentry);
  469. return rc;
  470. }
  471. static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
  472. const char *symname)
  473. {
  474. int rc;
  475. struct dentry *lower_dentry;
  476. struct dentry *lower_dir_dentry;
  477. umode_t mode;
  478. char *encoded_symname;
  479. unsigned int encoded_symlen;
  480. struct ecryptfs_crypt_stat *crypt_stat = NULL;
  481. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  482. dget(lower_dentry);
  483. lower_dir_dentry = lock_parent(lower_dentry);
  484. mode = S_IALLUGO;
  485. encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
  486. strlen(symname),
  487. &encoded_symname);
  488. if (encoded_symlen < 0) {
  489. rc = encoded_symlen;
  490. goto out_lock;
  491. }
  492. rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
  493. encoded_symname, mode);
  494. kfree(encoded_symname);
  495. if (rc || !lower_dentry->d_inode)
  496. goto out_lock;
  497. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  498. if (rc)
  499. goto out_lock;
  500. ecryptfs_copy_attr_timesizes(dir, lower_dir_dentry->d_inode);
  501. out_lock:
  502. unlock_dir(lower_dir_dentry);
  503. dput(lower_dentry);
  504. if (!dentry->d_inode)
  505. d_drop(dentry);
  506. return rc;
  507. }
  508. static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  509. {
  510. int rc;
  511. struct dentry *lower_dentry;
  512. struct dentry *lower_dir_dentry;
  513. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  514. lower_dir_dentry = lock_parent(lower_dentry);
  515. rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
  516. if (rc || !lower_dentry->d_inode)
  517. goto out;
  518. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  519. if (rc)
  520. goto out;
  521. ecryptfs_copy_attr_timesizes(dir, lower_dir_dentry->d_inode);
  522. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  523. out:
  524. unlock_dir(lower_dir_dentry);
  525. if (!dentry->d_inode)
  526. d_drop(dentry);
  527. return rc;
  528. }
  529. static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
  530. {
  531. struct dentry *lower_dentry;
  532. struct dentry *lower_dir_dentry;
  533. int rc;
  534. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  535. dget(dentry);
  536. lower_dir_dentry = lock_parent(lower_dentry);
  537. dget(lower_dentry);
  538. rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
  539. dput(lower_dentry);
  540. if (!rc)
  541. d_delete(lower_dentry);
  542. ecryptfs_copy_attr_times(dir, lower_dir_dentry->d_inode);
  543. dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
  544. unlock_dir(lower_dir_dentry);
  545. if (!rc)
  546. d_drop(dentry);
  547. dput(dentry);
  548. return rc;
  549. }
  550. static int
  551. ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  552. {
  553. int rc;
  554. struct dentry *lower_dentry;
  555. struct dentry *lower_dir_dentry;
  556. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  557. lower_dir_dentry = lock_parent(lower_dentry);
  558. rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
  559. if (rc || !lower_dentry->d_inode)
  560. goto out;
  561. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
  562. if (rc)
  563. goto out;
  564. ecryptfs_copy_attr_timesizes(dir, lower_dir_dentry->d_inode);
  565. out:
  566. unlock_dir(lower_dir_dentry);
  567. if (!dentry->d_inode)
  568. d_drop(dentry);
  569. return rc;
  570. }
  571. static int
  572. ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  573. struct inode *new_dir, struct dentry *new_dentry)
  574. {
  575. int rc;
  576. struct dentry *lower_old_dentry;
  577. struct dentry *lower_new_dentry;
  578. struct dentry *lower_old_dir_dentry;
  579. struct dentry *lower_new_dir_dentry;
  580. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  581. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  582. dget(lower_old_dentry);
  583. dget(lower_new_dentry);
  584. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  585. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  586. lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  587. rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
  588. lower_new_dir_dentry->d_inode, lower_new_dentry);
  589. if (rc)
  590. goto out_lock;
  591. ecryptfs_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
  592. if (new_dir != old_dir)
  593. ecryptfs_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
  594. out_lock:
  595. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  596. dput(lower_new_dentry);
  597. dput(lower_old_dentry);
  598. return rc;
  599. }
  600. static int
  601. ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
  602. {
  603. int rc;
  604. struct dentry *lower_dentry;
  605. char *decoded_name;
  606. char *lower_buf;
  607. mm_segment_t old_fs;
  608. struct ecryptfs_crypt_stat *crypt_stat;
  609. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  610. if (!lower_dentry->d_inode->i_op ||
  611. !lower_dentry->d_inode->i_op->readlink) {
  612. rc = -EINVAL;
  613. goto out;
  614. }
  615. /* Released in this function */
  616. lower_buf = kmalloc(bufsiz, GFP_KERNEL);
  617. if (lower_buf == NULL) {
  618. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  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. "lower_dentry->d_name.name = [%s]\n",
  626. lower_dentry->d_name.name);
  627. rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
  628. (char __user *)lower_buf,
  629. bufsiz);
  630. set_fs(old_fs);
  631. if (rc >= 0) {
  632. crypt_stat = NULL;
  633. rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
  634. &decoded_name);
  635. if (rc == -ENOMEM)
  636. goto out_free_lower_buf;
  637. if (rc > 0) {
  638. ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
  639. "to userspace: [%*s]\n", rc,
  640. decoded_name);
  641. if (copy_to_user(buf, decoded_name, rc))
  642. rc = -EFAULT;
  643. }
  644. kfree(decoded_name);
  645. ecryptfs_copy_attr_atime(dentry->d_inode,
  646. lower_dentry->d_inode);
  647. }
  648. out_free_lower_buf:
  649. kfree(lower_buf);
  650. out:
  651. return rc;
  652. }
  653. static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  654. {
  655. char *buf;
  656. int len = PAGE_SIZE, rc;
  657. mm_segment_t old_fs;
  658. /* Released in ecryptfs_put_link(); only release here on error */
  659. buf = kmalloc(len, GFP_KERNEL);
  660. if (!buf) {
  661. rc = -ENOMEM;
  662. goto out;
  663. }
  664. old_fs = get_fs();
  665. set_fs(get_ds());
  666. ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
  667. "dentry->d_name.name = [%s]\n", dentry->d_name.name);
  668. rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
  669. buf[rc] = '\0';
  670. set_fs(old_fs);
  671. if (rc < 0)
  672. goto out_free;
  673. rc = 0;
  674. nd_set_link(nd, buf);
  675. goto out;
  676. out_free:
  677. kfree(buf);
  678. out:
  679. return ERR_PTR(rc);
  680. }
  681. static void
  682. ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
  683. {
  684. /* Free the char* */
  685. kfree(nd_get_link(nd));
  686. }
  687. /**
  688. * upper_size_to_lower_size
  689. * @crypt_stat: Crypt_stat associated with file
  690. * @upper_size: Size of the upper file
  691. *
  692. * Calculate the requried size of the lower file based on the
  693. * specified size of the upper file. This calculation is based on the
  694. * number of headers in the underlying file and the extent size.
  695. *
  696. * Returns Calculated size of the lower file.
  697. */
  698. static loff_t
  699. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  700. loff_t upper_size)
  701. {
  702. loff_t lower_size;
  703. lower_size = ( crypt_stat->header_extent_size
  704. * crypt_stat->num_header_extents_at_front );
  705. if (upper_size != 0) {
  706. loff_t num_extents;
  707. num_extents = upper_size >> crypt_stat->extent_shift;
  708. if (upper_size & ~crypt_stat->extent_mask)
  709. num_extents++;
  710. lower_size += (num_extents * crypt_stat->extent_size);
  711. }
  712. return lower_size;
  713. }
  714. /**
  715. * ecryptfs_truncate
  716. * @dentry: The ecryptfs layer dentry
  717. * @new_length: The length to expand the file to
  718. *
  719. * Function to handle truncations modifying the size of the file. Note
  720. * that the file sizes are interpolated. When expanding, we are simply
  721. * writing strings of 0's out. When truncating, we need to modify the
  722. * underlying file size according to the page index interpolations.
  723. *
  724. * Returns zero on success; non-zero otherwise
  725. */
  726. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  727. {
  728. int rc = 0;
  729. struct inode *inode = dentry->d_inode;
  730. struct dentry *lower_dentry;
  731. struct vfsmount *lower_mnt;
  732. struct file fake_ecryptfs_file, *lower_file = NULL;
  733. struct ecryptfs_crypt_stat *crypt_stat;
  734. loff_t i_size = i_size_read(inode);
  735. loff_t lower_size_before_truncate;
  736. loff_t lower_size_after_truncate;
  737. if (unlikely((new_length == i_size)))
  738. goto out;
  739. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  740. /* Set up a fake ecryptfs file, this is used to interface with
  741. * the file in the underlying filesystem so that the
  742. * truncation has an effect there as well. */
  743. memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
  744. fake_ecryptfs_file.f_dentry = dentry;
  745. /* Released at out_free: label */
  746. ecryptfs_set_file_private(&fake_ecryptfs_file,
  747. kmem_cache_alloc(ecryptfs_file_info_cache,
  748. SLAB_KERNEL));
  749. if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
  750. rc = -ENOMEM;
  751. goto out;
  752. }
  753. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  754. /* This dget & mntget is released through fput at out_fput: */
  755. lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
  756. if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
  757. O_RDWR))) {
  758. ecryptfs_printk(KERN_ERR,
  759. "Error opening dentry; rc = [%i]\n", rc);
  760. goto out_free;
  761. }
  762. ecryptfs_set_file_lower(&fake_ecryptfs_file, lower_file);
  763. /* Switch on growing or shrinking file */
  764. if (new_length > i_size) {
  765. rc = ecryptfs_fill_zeros(&fake_ecryptfs_file, new_length);
  766. if (rc) {
  767. ecryptfs_printk(KERN_ERR,
  768. "Problem with fill_zeros\n");
  769. goto out_fput;
  770. }
  771. i_size_write(inode, new_length);
  772. rc = ecryptfs_write_inode_size_to_header(lower_file,
  773. lower_dentry->d_inode,
  774. inode);
  775. if (rc) {
  776. ecryptfs_printk(KERN_ERR,
  777. "Problem with ecryptfs_write"
  778. "_inode_size\n");
  779. goto out_fput;
  780. }
  781. } else { /* new_length < i_size_read(inode) */
  782. vmtruncate(inode, new_length);
  783. ecryptfs_write_inode_size_to_header(lower_file,
  784. lower_dentry->d_inode,
  785. inode);
  786. /* We are reducing the size of the ecryptfs file, and need to
  787. * know if we need to reduce the size of the lower file. */
  788. lower_size_before_truncate =
  789. upper_size_to_lower_size(crypt_stat, i_size);
  790. lower_size_after_truncate =
  791. upper_size_to_lower_size(crypt_stat, new_length);
  792. if (lower_size_after_truncate < lower_size_before_truncate)
  793. vmtruncate(lower_dentry->d_inode,
  794. lower_size_after_truncate);
  795. }
  796. /* Update the access times */
  797. lower_dentry->d_inode->i_mtime = lower_dentry->d_inode->i_ctime
  798. = CURRENT_TIME;
  799. mark_inode_dirty_sync(inode);
  800. out_fput:
  801. if ((rc = ecryptfs_close_lower_file(lower_file)))
  802. printk(KERN_ERR "Error closing lower_file\n");
  803. out_free:
  804. if (ecryptfs_file_to_private(&fake_ecryptfs_file))
  805. kmem_cache_free(ecryptfs_file_info_cache,
  806. ecryptfs_file_to_private(&fake_ecryptfs_file));
  807. out:
  808. return rc;
  809. }
  810. static int
  811. ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  812. {
  813. int rc;
  814. if (nd) {
  815. struct vfsmount *vfsmnt_save = nd->mnt;
  816. struct dentry *dentry_save = nd->dentry;
  817. nd->mnt = ecryptfs_dentry_to_lower_mnt(nd->dentry);
  818. nd->dentry = ecryptfs_dentry_to_lower(nd->dentry);
  819. rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
  820. nd->mnt = vfsmnt_save;
  821. nd->dentry = dentry_save;
  822. } else
  823. rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
  824. return rc;
  825. }
  826. /**
  827. * ecryptfs_setattr
  828. * @dentry: dentry handle to the inode to modify
  829. * @ia: Structure with flags of what to change and values
  830. *
  831. * Updates the metadata of an inode. If the update is to the size
  832. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  833. * of both the ecryptfs inode and the lower inode.
  834. *
  835. * All other metadata changes will be passed right to the lower filesystem,
  836. * and we will just update our inode to look like the lower.
  837. */
  838. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  839. {
  840. int rc = 0;
  841. struct dentry *lower_dentry;
  842. struct inode *inode;
  843. struct inode *lower_inode;
  844. struct ecryptfs_crypt_stat *crypt_stat;
  845. crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
  846. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  847. inode = dentry->d_inode;
  848. lower_inode = ecryptfs_inode_to_lower(inode);
  849. if (ia->ia_valid & ATTR_SIZE) {
  850. ecryptfs_printk(KERN_DEBUG,
  851. "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
  852. ia->ia_valid, ATTR_SIZE);
  853. rc = ecryptfs_truncate(dentry, ia->ia_size);
  854. /* ecryptfs_truncate handles resizing of the lower file */
  855. ia->ia_valid &= ~ATTR_SIZE;
  856. ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
  857. ia->ia_valid);
  858. if (rc < 0)
  859. goto out;
  860. }
  861. rc = notify_change(lower_dentry, ia);
  862. out:
  863. ecryptfs_copy_attr_all(inode, lower_inode);
  864. return rc;
  865. }
  866. static int
  867. ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  868. size_t size, int flags)
  869. {
  870. int rc = 0;
  871. struct dentry *lower_dentry;
  872. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  873. if (!lower_dentry->d_inode->i_op->setxattr) {
  874. rc = -ENOSYS;
  875. goto out;
  876. }
  877. mutex_lock(&lower_dentry->d_inode->i_mutex);
  878. rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
  879. size, flags);
  880. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  881. out:
  882. return rc;
  883. }
  884. static ssize_t
  885. ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
  886. size_t size)
  887. {
  888. int rc = 0;
  889. struct dentry *lower_dentry;
  890. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  891. if (!lower_dentry->d_inode->i_op->getxattr) {
  892. rc = -ENOSYS;
  893. goto out;
  894. }
  895. mutex_lock(&lower_dentry->d_inode->i_mutex);
  896. rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
  897. size);
  898. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  899. out:
  900. return rc;
  901. }
  902. static ssize_t
  903. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  904. {
  905. int rc = 0;
  906. struct dentry *lower_dentry;
  907. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  908. if (!lower_dentry->d_inode->i_op->listxattr) {
  909. rc = -ENOSYS;
  910. goto out;
  911. }
  912. mutex_lock(&lower_dentry->d_inode->i_mutex);
  913. rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
  914. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  915. out:
  916. return rc;
  917. }
  918. static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
  919. {
  920. int rc = 0;
  921. struct dentry *lower_dentry;
  922. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  923. if (!lower_dentry->d_inode->i_op->removexattr) {
  924. rc = -ENOSYS;
  925. goto out;
  926. }
  927. mutex_lock(&lower_dentry->d_inode->i_mutex);
  928. rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
  929. mutex_unlock(&lower_dentry->d_inode->i_mutex);
  930. out:
  931. return rc;
  932. }
  933. int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
  934. {
  935. if ((ecryptfs_inode_to_lower(inode)
  936. == (struct inode *)candidate_lower_inode))
  937. return 1;
  938. else
  939. return 0;
  940. }
  941. int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
  942. {
  943. ecryptfs_init_inode(inode, (struct inode *)lower_inode);
  944. return 0;
  945. }
  946. struct inode_operations ecryptfs_symlink_iops = {
  947. .readlink = ecryptfs_readlink,
  948. .follow_link = ecryptfs_follow_link,
  949. .put_link = ecryptfs_put_link,
  950. .permission = ecryptfs_permission,
  951. .setattr = ecryptfs_setattr,
  952. .setxattr = ecryptfs_setxattr,
  953. .getxattr = ecryptfs_getxattr,
  954. .listxattr = ecryptfs_listxattr,
  955. .removexattr = ecryptfs_removexattr
  956. };
  957. struct inode_operations ecryptfs_dir_iops = {
  958. .create = ecryptfs_create,
  959. .lookup = ecryptfs_lookup,
  960. .link = ecryptfs_link,
  961. .unlink = ecryptfs_unlink,
  962. .symlink = ecryptfs_symlink,
  963. .mkdir = ecryptfs_mkdir,
  964. .rmdir = ecryptfs_rmdir,
  965. .mknod = ecryptfs_mknod,
  966. .rename = ecryptfs_rename,
  967. .permission = ecryptfs_permission,
  968. .setattr = ecryptfs_setattr,
  969. .setxattr = ecryptfs_setxattr,
  970. .getxattr = ecryptfs_getxattr,
  971. .listxattr = ecryptfs_listxattr,
  972. .removexattr = ecryptfs_removexattr
  973. };
  974. struct inode_operations ecryptfs_main_iops = {
  975. .permission = ecryptfs_permission,
  976. .setattr = ecryptfs_setattr,
  977. .setxattr = ecryptfs_setxattr,
  978. .getxattr = ecryptfs_getxattr,
  979. .listxattr = ecryptfs_listxattr,
  980. .removexattr = ecryptfs_removexattr
  981. };