inode.c 31 KB

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