inode.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 1997-2004 Erez Zadok
  5. * Copyright (C) 2001-2004 Stony Brook University
  6. * Copyright (C) 2004-2007 International Business Machines Corp.
  7. * Author(s): Michael A. Halcrow <[email protected]>
  8. * Michael C. Thompsion <[email protected]>
  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/fs_stack.h>
  32. #include <linux/slab.h>
  33. #include <linux/xattr.h>
  34. #include <asm/unaligned.h>
  35. #include "ecryptfs_kernel.h"
  36. static struct dentry *lock_parent(struct dentry *dentry)
  37. {
  38. struct dentry *dir;
  39. dir = dget_parent(dentry);
  40. inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
  41. return dir;
  42. }
  43. static void unlock_dir(struct dentry *dir)
  44. {
  45. inode_unlock(d_inode(dir));
  46. dput(dir);
  47. }
  48. static int ecryptfs_inode_test(struct inode *inode, void *lower_inode)
  49. {
  50. return ecryptfs_inode_to_lower(inode) == lower_inode;
  51. }
  52. static int ecryptfs_inode_set(struct inode *inode, void *opaque)
  53. {
  54. struct inode *lower_inode = opaque;
  55. ecryptfs_set_inode_lower(inode, lower_inode);
  56. fsstack_copy_attr_all(inode, lower_inode);
  57. /* i_size will be overwritten for encrypted regular files */
  58. fsstack_copy_inode_size(inode, lower_inode);
  59. inode->i_ino = lower_inode->i_ino;
  60. inode->i_version++;
  61. inode->i_mapping->a_ops = &ecryptfs_aops;
  62. if (S_ISLNK(inode->i_mode))
  63. inode->i_op = &ecryptfs_symlink_iops;
  64. else if (S_ISDIR(inode->i_mode))
  65. inode->i_op = &ecryptfs_dir_iops;
  66. else
  67. inode->i_op = &ecryptfs_main_iops;
  68. if (S_ISDIR(inode->i_mode))
  69. inode->i_fop = &ecryptfs_dir_fops;
  70. else if (special_file(inode->i_mode))
  71. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  72. else
  73. inode->i_fop = &ecryptfs_main_fops;
  74. return 0;
  75. }
  76. static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
  77. struct super_block *sb)
  78. {
  79. struct inode *inode;
  80. if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
  81. return ERR_PTR(-EXDEV);
  82. if (!igrab(lower_inode))
  83. return ERR_PTR(-ESTALE);
  84. inode = iget5_locked(sb, (unsigned long)lower_inode,
  85. ecryptfs_inode_test, ecryptfs_inode_set,
  86. lower_inode);
  87. if (!inode) {
  88. iput(lower_inode);
  89. return ERR_PTR(-EACCES);
  90. }
  91. if (!(inode->i_state & I_NEW))
  92. iput(lower_inode);
  93. return inode;
  94. }
  95. struct inode *ecryptfs_get_inode(struct inode *lower_inode,
  96. struct super_block *sb)
  97. {
  98. struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
  99. if (!IS_ERR(inode) && (inode->i_state & I_NEW))
  100. unlock_new_inode(inode);
  101. return inode;
  102. }
  103. /**
  104. * ecryptfs_interpose
  105. * @lower_dentry: Existing dentry in the lower filesystem
  106. * @dentry: ecryptfs' dentry
  107. * @sb: ecryptfs's super_block
  108. *
  109. * Interposes upper and lower dentries.
  110. *
  111. * Returns zero on success; non-zero otherwise
  112. */
  113. static int ecryptfs_interpose(struct dentry *lower_dentry,
  114. struct dentry *dentry, struct super_block *sb)
  115. {
  116. struct inode *inode = ecryptfs_get_inode(d_inode(lower_dentry), sb);
  117. if (IS_ERR(inode))
  118. return PTR_ERR(inode);
  119. d_instantiate(dentry, inode);
  120. return 0;
  121. }
  122. static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
  123. struct inode *inode)
  124. {
  125. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  126. struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
  127. struct dentry *lower_dir_dentry;
  128. int rc;
  129. dget(lower_dentry);
  130. lower_dir_dentry = lock_parent(lower_dentry);
  131. rc = vfs_unlink(lower_dir_inode, lower_dentry, NULL);
  132. if (rc) {
  133. printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
  134. goto out_unlock;
  135. }
  136. fsstack_copy_attr_times(dir, lower_dir_inode);
  137. set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
  138. inode->i_ctime = dir->i_ctime;
  139. d_drop(dentry);
  140. out_unlock:
  141. unlock_dir(lower_dir_dentry);
  142. dput(lower_dentry);
  143. return rc;
  144. }
  145. /**
  146. * ecryptfs_do_create
  147. * @directory_inode: inode of the new file's dentry's parent in ecryptfs
  148. * @ecryptfs_dentry: New file's dentry in ecryptfs
  149. * @mode: The mode of the new file
  150. *
  151. * Creates the underlying file and the eCryptfs inode which will link to
  152. * it. It will also update the eCryptfs directory inode to mimic the
  153. * stat of the lower directory inode.
  154. *
  155. * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
  156. */
  157. static struct inode *
  158. ecryptfs_do_create(struct inode *directory_inode,
  159. struct dentry *ecryptfs_dentry, umode_t mode)
  160. {
  161. int rc;
  162. struct dentry *lower_dentry;
  163. struct dentry *lower_dir_dentry;
  164. struct inode *inode;
  165. lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
  166. lower_dir_dentry = lock_parent(lower_dentry);
  167. rc = vfs_create(d_inode(lower_dir_dentry), lower_dentry, mode, true);
  168. if (rc) {
  169. printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
  170. "rc = [%d]\n", __func__, rc);
  171. inode = ERR_PTR(rc);
  172. goto out_lock;
  173. }
  174. inode = __ecryptfs_get_inode(d_inode(lower_dentry),
  175. directory_inode->i_sb);
  176. if (IS_ERR(inode)) {
  177. vfs_unlink(d_inode(lower_dir_dentry), lower_dentry, NULL);
  178. goto out_lock;
  179. }
  180. fsstack_copy_attr_times(directory_inode, d_inode(lower_dir_dentry));
  181. fsstack_copy_inode_size(directory_inode, d_inode(lower_dir_dentry));
  182. out_lock:
  183. unlock_dir(lower_dir_dentry);
  184. return inode;
  185. }
  186. /**
  187. * ecryptfs_initialize_file
  188. *
  189. * Cause the file to be changed from a basic empty file to an ecryptfs
  190. * file with a header and first data page.
  191. *
  192. * Returns zero on success
  193. */
  194. int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
  195. struct inode *ecryptfs_inode)
  196. {
  197. struct ecryptfs_crypt_stat *crypt_stat =
  198. &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  199. int rc = 0;
  200. if (S_ISDIR(ecryptfs_inode->i_mode)) {
  201. ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
  202. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  203. goto out;
  204. }
  205. ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
  206. rc = ecryptfs_new_file_context(ecryptfs_inode);
  207. if (rc) {
  208. ecryptfs_printk(KERN_ERR, "Error creating new file "
  209. "context; rc = [%d]\n", rc);
  210. goto out;
  211. }
  212. rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
  213. if (rc) {
  214. printk(KERN_ERR "%s: Error attempting to initialize "
  215. "the lower file for the dentry with name "
  216. "[%pd]; rc = [%d]\n", __func__,
  217. ecryptfs_dentry, rc);
  218. goto out;
  219. }
  220. rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
  221. if (rc)
  222. printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
  223. ecryptfs_put_lower_file(ecryptfs_inode);
  224. out:
  225. return rc;
  226. }
  227. /**
  228. * ecryptfs_create
  229. * @dir: The inode of the directory in which to create the file.
  230. * @dentry: The eCryptfs dentry
  231. * @mode: The mode of the new file.
  232. *
  233. * Creates a new file.
  234. *
  235. * Returns zero on success; non-zero on error condition
  236. */
  237. static int
  238. ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
  239. umode_t mode, bool excl)
  240. {
  241. struct inode *ecryptfs_inode;
  242. int rc;
  243. struct ecryptfs_crypt_stat *crypt_stat;
  244. ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
  245. mode);
  246. if (IS_ERR(ecryptfs_inode)) {
  247. ecryptfs_printk(KERN_WARNING, "Failed to create file in"
  248. "lower filesystem\n");
  249. rc = PTR_ERR(ecryptfs_inode);
  250. goto out;
  251. }
  252. /* At this point, a file exists on "disk"; we need to make sure
  253. * that this on disk file is prepared to be an ecryptfs file */
  254. rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
  255. if (rc) {
  256. ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
  257. ecryptfs_inode);
  258. iget_failed(ecryptfs_inode);
  259. goto out;
  260. }
  261. crypt_stat = &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
  262. if (get_events() && get_events()->open_cb)
  263. get_events()->open_cb(
  264. ecryptfs_inode_to_lower(ecryptfs_inode),
  265. crypt_stat);
  266. d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
  267. out:
  268. return rc;
  269. }
  270. static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
  271. {
  272. struct ecryptfs_crypt_stat *crypt_stat;
  273. int rc;
  274. rc = ecryptfs_get_lower_file(dentry, inode);
  275. if (rc) {
  276. printk(KERN_ERR "%s: Error attempting to initialize "
  277. "the lower file for the dentry with name "
  278. "[%pd]; rc = [%d]\n", __func__,
  279. dentry, rc);
  280. return rc;
  281. }
  282. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  283. /* TODO: lock for crypt_stat comparison */
  284. if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
  285. ecryptfs_set_default_sizes(crypt_stat);
  286. rc = ecryptfs_read_and_validate_header_region(inode);
  287. ecryptfs_put_lower_file(inode);
  288. if (rc) {
  289. rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
  290. if (!rc)
  291. crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
  292. }
  293. /* Must return 0 to allow non-eCryptfs files to be looked up, too */
  294. return 0;
  295. }
  296. /**
  297. * ecryptfs_lookup_interpose - Dentry interposition for a lookup
  298. */
  299. static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
  300. struct dentry *lower_dentry)
  301. {
  302. struct inode *inode, *lower_inode = d_inode(lower_dentry);
  303. struct ecryptfs_dentry_info *dentry_info;
  304. struct vfsmount *lower_mnt;
  305. int rc = 0;
  306. dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
  307. if (!dentry_info) {
  308. printk(KERN_ERR "%s: Out of memory whilst attempting "
  309. "to allocate ecryptfs_dentry_info struct\n",
  310. __func__);
  311. dput(lower_dentry);
  312. return ERR_PTR(-ENOMEM);
  313. }
  314. lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
  315. fsstack_copy_attr_atime(d_inode(dentry->d_parent),
  316. d_inode(lower_dentry->d_parent));
  317. BUG_ON(!d_count(lower_dentry));
  318. ecryptfs_set_dentry_private(dentry, dentry_info);
  319. dentry_info->lower_path.mnt = lower_mnt;
  320. dentry_info->lower_path.dentry = lower_dentry;
  321. if (d_really_is_negative(lower_dentry)) {
  322. /* We want to add because we couldn't find in lower */
  323. d_add(dentry, NULL);
  324. return NULL;
  325. }
  326. inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb);
  327. if (IS_ERR(inode)) {
  328. printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
  329. __func__, PTR_ERR(inode));
  330. return ERR_CAST(inode);
  331. }
  332. if (S_ISREG(inode->i_mode)) {
  333. rc = ecryptfs_i_size_read(dentry, inode);
  334. if (rc) {
  335. make_bad_inode(inode);
  336. return ERR_PTR(rc);
  337. }
  338. }
  339. if (inode->i_state & I_NEW)
  340. unlock_new_inode(inode);
  341. return d_splice_alias(inode, dentry);
  342. }
  343. /**
  344. * ecryptfs_lookup
  345. * @ecryptfs_dir_inode: The eCryptfs directory inode
  346. * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
  347. * @flags: lookup flags
  348. *
  349. * Find a file on disk. If the file does not exist, then we'll add it to the
  350. * dentry cache and continue on to read it from the disk.
  351. */
  352. static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
  353. struct dentry *ecryptfs_dentry,
  354. unsigned int flags)
  355. {
  356. char *encrypted_and_encoded_name = NULL;
  357. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  358. struct dentry *lower_dir_dentry, *lower_dentry;
  359. const char *name = ecryptfs_dentry->d_name.name;
  360. size_t len = ecryptfs_dentry->d_name.len;
  361. struct dentry *res;
  362. int rc = 0;
  363. lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
  364. mount_crypt_stat = &ecryptfs_superblock_to_private(
  365. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  366. if (mount_crypt_stat
  367. && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)) {
  368. rc = ecryptfs_encrypt_and_encode_filename(
  369. &encrypted_and_encoded_name, &len,
  370. mount_crypt_stat, name, len);
  371. if (rc) {
  372. printk(KERN_ERR "%s: Error attempting to encrypt and encode "
  373. "filename; rc = [%d]\n", __func__, rc);
  374. return ERR_PTR(rc);
  375. }
  376. name = encrypted_and_encoded_name;
  377. }
  378. lower_dentry = lookup_one_len_unlocked(name, lower_dir_dentry, len);
  379. if (IS_ERR(lower_dentry)) {
  380. ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
  381. "[%ld] on lower_dentry = [%s]\n", __func__,
  382. PTR_ERR(lower_dentry),
  383. name);
  384. res = ERR_CAST(lower_dentry);
  385. } else {
  386. res = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry);
  387. }
  388. kfree(encrypted_and_encoded_name);
  389. return res;
  390. }
  391. static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
  392. struct dentry *new_dentry)
  393. {
  394. struct dentry *lower_old_dentry;
  395. struct dentry *lower_new_dentry;
  396. struct dentry *lower_dir_dentry;
  397. u64 file_size_save;
  398. int rc;
  399. file_size_save = i_size_read(d_inode(old_dentry));
  400. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  401. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  402. dget(lower_old_dentry);
  403. dget(lower_new_dentry);
  404. lower_dir_dentry = lock_parent(lower_new_dentry);
  405. rc = vfs_link(lower_old_dentry, d_inode(lower_dir_dentry),
  406. lower_new_dentry, NULL);
  407. if (rc || d_really_is_negative(lower_new_dentry))
  408. goto out_lock;
  409. rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
  410. if (rc)
  411. goto out_lock;
  412. fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
  413. fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
  414. set_nlink(d_inode(old_dentry),
  415. ecryptfs_inode_to_lower(d_inode(old_dentry))->i_nlink);
  416. i_size_write(d_inode(new_dentry), file_size_save);
  417. out_lock:
  418. unlock_dir(lower_dir_dentry);
  419. dput(lower_new_dentry);
  420. dput(lower_old_dentry);
  421. return rc;
  422. }
  423. static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
  424. {
  425. return ecryptfs_do_unlink(dir, dentry, d_inode(dentry));
  426. }
  427. static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
  428. const char *symname)
  429. {
  430. int rc;
  431. struct dentry *lower_dentry;
  432. struct dentry *lower_dir_dentry;
  433. char *encoded_symname;
  434. size_t encoded_symlen;
  435. struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
  436. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  437. dget(lower_dentry);
  438. lower_dir_dentry = lock_parent(lower_dentry);
  439. mount_crypt_stat = &ecryptfs_superblock_to_private(
  440. dir->i_sb)->mount_crypt_stat;
  441. rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
  442. &encoded_symlen,
  443. mount_crypt_stat, symname,
  444. strlen(symname));
  445. if (rc)
  446. goto out_lock;
  447. rc = vfs_symlink(d_inode(lower_dir_dentry), lower_dentry,
  448. encoded_symname);
  449. kfree(encoded_symname);
  450. if (rc || d_really_is_negative(lower_dentry))
  451. goto out_lock;
  452. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  453. if (rc)
  454. goto out_lock;
  455. fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
  456. fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
  457. out_lock:
  458. unlock_dir(lower_dir_dentry);
  459. dput(lower_dentry);
  460. if (d_really_is_negative(dentry))
  461. d_drop(dentry);
  462. return rc;
  463. }
  464. static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  465. {
  466. int rc;
  467. struct dentry *lower_dentry;
  468. struct dentry *lower_dir_dentry;
  469. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  470. lower_dir_dentry = lock_parent(lower_dentry);
  471. rc = vfs_mkdir(d_inode(lower_dir_dentry), lower_dentry, mode);
  472. if (rc || d_really_is_negative(lower_dentry))
  473. goto out;
  474. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  475. if (rc)
  476. goto out;
  477. fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
  478. fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
  479. set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
  480. out:
  481. unlock_dir(lower_dir_dentry);
  482. if (d_really_is_negative(dentry))
  483. d_drop(dentry);
  484. return rc;
  485. }
  486. static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
  487. {
  488. struct dentry *lower_dentry;
  489. struct dentry *lower_dir_dentry;
  490. int rc;
  491. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  492. dget(dentry);
  493. lower_dir_dentry = lock_parent(lower_dentry);
  494. dget(lower_dentry);
  495. rc = vfs_rmdir(d_inode(lower_dir_dentry), lower_dentry);
  496. dput(lower_dentry);
  497. if (!rc && d_really_is_positive(dentry))
  498. clear_nlink(d_inode(dentry));
  499. fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
  500. set_nlink(dir, d_inode(lower_dir_dentry)->i_nlink);
  501. unlock_dir(lower_dir_dentry);
  502. if (!rc)
  503. d_drop(dentry);
  504. dput(dentry);
  505. return rc;
  506. }
  507. static int
  508. ecryptfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  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_mknod(d_inode(lower_dir_dentry), lower_dentry, mode, dev);
  516. if (rc || d_really_is_negative(lower_dentry))
  517. goto out;
  518. rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
  519. if (rc)
  520. goto out;
  521. fsstack_copy_attr_times(dir, d_inode(lower_dir_dentry));
  522. fsstack_copy_inode_size(dir, d_inode(lower_dir_dentry));
  523. out:
  524. unlock_dir(lower_dir_dentry);
  525. if (d_really_is_negative(dentry))
  526. d_drop(dentry);
  527. return rc;
  528. }
  529. static int
  530. ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  531. struct inode *new_dir, struct dentry *new_dentry,
  532. unsigned int flags)
  533. {
  534. int rc;
  535. struct dentry *lower_old_dentry;
  536. struct dentry *lower_new_dentry;
  537. struct dentry *lower_old_dir_dentry;
  538. struct dentry *lower_new_dir_dentry;
  539. struct dentry *trap = NULL;
  540. struct inode *target_inode;
  541. if (flags)
  542. return -EINVAL;
  543. lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
  544. lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
  545. dget(lower_old_dentry);
  546. dget(lower_new_dentry);
  547. lower_old_dir_dentry = dget_parent(lower_old_dentry);
  548. lower_new_dir_dentry = dget_parent(lower_new_dentry);
  549. target_inode = d_inode(new_dentry);
  550. trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  551. /* source should not be ancestor of target */
  552. if (trap == lower_old_dentry) {
  553. rc = -EINVAL;
  554. goto out_lock;
  555. }
  556. /* target should not be ancestor of source */
  557. if (trap == lower_new_dentry) {
  558. rc = -ENOTEMPTY;
  559. goto out_lock;
  560. }
  561. rc = vfs_rename(d_inode(lower_old_dir_dentry), lower_old_dentry,
  562. d_inode(lower_new_dir_dentry), lower_new_dentry,
  563. NULL, 0);
  564. if (rc)
  565. goto out_lock;
  566. if (target_inode)
  567. fsstack_copy_attr_all(target_inode,
  568. ecryptfs_inode_to_lower(target_inode));
  569. fsstack_copy_attr_all(new_dir, d_inode(lower_new_dir_dentry));
  570. if (new_dir != old_dir)
  571. fsstack_copy_attr_all(old_dir, d_inode(lower_old_dir_dentry));
  572. out_lock:
  573. unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
  574. dput(lower_new_dir_dentry);
  575. dput(lower_old_dir_dentry);
  576. dput(lower_new_dentry);
  577. dput(lower_old_dentry);
  578. return rc;
  579. }
  580. static char *ecryptfs_readlink_lower(struct dentry *dentry, size_t *bufsiz)
  581. {
  582. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  583. char *lower_buf;
  584. char *buf;
  585. mm_segment_t old_fs;
  586. int rc;
  587. lower_buf = kmalloc(PATH_MAX, GFP_KERNEL);
  588. if (!lower_buf)
  589. return ERR_PTR(-ENOMEM);
  590. old_fs = get_fs();
  591. set_fs(get_ds());
  592. rc = d_inode(lower_dentry)->i_op->readlink(lower_dentry,
  593. (char __user *)lower_buf,
  594. PATH_MAX);
  595. set_fs(old_fs);
  596. if (rc < 0)
  597. goto out;
  598. rc = ecryptfs_decode_and_decrypt_filename(&buf, bufsiz, dentry->d_sb,
  599. lower_buf, rc);
  600. out:
  601. kfree(lower_buf);
  602. return rc ? ERR_PTR(rc) : buf;
  603. }
  604. static const char *ecryptfs_get_link(struct dentry *dentry,
  605. struct inode *inode,
  606. struct delayed_call *done)
  607. {
  608. size_t len;
  609. char *buf;
  610. if (!dentry)
  611. return ERR_PTR(-ECHILD);
  612. buf = ecryptfs_readlink_lower(dentry, &len);
  613. if (IS_ERR(buf))
  614. return buf;
  615. fsstack_copy_attr_atime(d_inode(dentry),
  616. d_inode(ecryptfs_dentry_to_lower(dentry)));
  617. buf[len] = '\0';
  618. set_delayed_call(done, kfree_link, buf);
  619. return buf;
  620. }
  621. /**
  622. * upper_size_to_lower_size
  623. * @crypt_stat: Crypt_stat associated with file
  624. * @upper_size: Size of the upper file
  625. *
  626. * Calculate the required size of the lower file based on the
  627. * specified size of the upper file. This calculation is based on the
  628. * number of headers in the underlying file and the extent size.
  629. *
  630. * Returns Calculated size of the lower file.
  631. */
  632. static loff_t
  633. upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
  634. loff_t upper_size)
  635. {
  636. loff_t lower_size;
  637. lower_size = ecryptfs_lower_header_size(crypt_stat);
  638. if (upper_size != 0) {
  639. loff_t num_extents;
  640. num_extents = upper_size >> crypt_stat->extent_shift;
  641. if (upper_size & ~crypt_stat->extent_mask)
  642. num_extents++;
  643. lower_size += (num_extents * crypt_stat->extent_size);
  644. }
  645. return lower_size;
  646. }
  647. /**
  648. * truncate_upper
  649. * @dentry: The ecryptfs layer dentry
  650. * @ia: Address of the ecryptfs inode's attributes
  651. * @lower_ia: Address of the lower inode's attributes
  652. *
  653. * Function to handle truncations modifying the size of the file. Note
  654. * that the file sizes are interpolated. When expanding, we are simply
  655. * writing strings of 0's out. When truncating, we truncate the upper
  656. * inode and update the lower_ia according to the page index
  657. * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
  658. * the caller must use lower_ia in a call to notify_change() to perform
  659. * the truncation of the lower inode.
  660. *
  661. * Returns zero on success; non-zero otherwise
  662. */
  663. static int truncate_upper(struct dentry *dentry, struct iattr *ia,
  664. struct iattr *lower_ia)
  665. {
  666. int rc = 0;
  667. struct inode *inode = d_inode(dentry);
  668. struct ecryptfs_crypt_stat *crypt_stat;
  669. loff_t i_size = i_size_read(inode);
  670. loff_t lower_size_before_truncate;
  671. loff_t lower_size_after_truncate;
  672. if (unlikely((ia->ia_size == i_size))) {
  673. lower_ia->ia_valid &= ~ATTR_SIZE;
  674. return 0;
  675. }
  676. rc = ecryptfs_get_lower_file(dentry, inode);
  677. if (rc)
  678. return rc;
  679. crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
  680. /* Switch on growing or shrinking file */
  681. if (ia->ia_size > i_size) {
  682. char zero[] = { 0x00 };
  683. lower_ia->ia_valid &= ~ATTR_SIZE;
  684. /* Write a single 0 at the last position of the file;
  685. * this triggers code that will fill in 0's throughout
  686. * the intermediate portion of the previous end of the
  687. * file and the new and of the file */
  688. rc = ecryptfs_write(inode, zero,
  689. (ia->ia_size - 1), 1);
  690. } else { /* ia->ia_size < i_size_read(inode) */
  691. /* We're chopping off all the pages down to the page
  692. * in which ia->ia_size is located. Fill in the end of
  693. * that page from (ia->ia_size & ~PAGE_MASK) to
  694. * PAGE_SIZE with zeros. */
  695. size_t num_zeros = (PAGE_SIZE
  696. - (ia->ia_size & ~PAGE_MASK));
  697. if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
  698. truncate_setsize(inode, ia->ia_size);
  699. lower_ia->ia_size = ia->ia_size;
  700. lower_ia->ia_valid |= ATTR_SIZE;
  701. goto out;
  702. }
  703. if (num_zeros) {
  704. char *zeros_virt;
  705. zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
  706. if (!zeros_virt) {
  707. rc = -ENOMEM;
  708. goto out;
  709. }
  710. rc = ecryptfs_write(inode, zeros_virt,
  711. ia->ia_size, num_zeros);
  712. kfree(zeros_virt);
  713. if (rc) {
  714. printk(KERN_ERR "Error attempting to zero out "
  715. "the remainder of the end page on "
  716. "reducing truncate; rc = [%d]\n", rc);
  717. goto out;
  718. }
  719. }
  720. truncate_setsize(inode, ia->ia_size);
  721. rc = ecryptfs_write_inode_size_to_metadata(inode);
  722. if (rc) {
  723. printk(KERN_ERR "Problem with "
  724. "ecryptfs_write_inode_size_to_metadata; "
  725. "rc = [%d]\n", rc);
  726. goto out;
  727. }
  728. /* We are reducing the size of the ecryptfs file, and need to
  729. * know if we need to reduce the size of the lower file. */
  730. lower_size_before_truncate =
  731. upper_size_to_lower_size(crypt_stat, i_size);
  732. lower_size_after_truncate =
  733. upper_size_to_lower_size(crypt_stat, ia->ia_size);
  734. if (lower_size_after_truncate < lower_size_before_truncate) {
  735. lower_ia->ia_size = lower_size_after_truncate;
  736. lower_ia->ia_valid |= ATTR_SIZE;
  737. } else
  738. lower_ia->ia_valid &= ~ATTR_SIZE;
  739. }
  740. out:
  741. ecryptfs_put_lower_file(inode);
  742. return rc;
  743. }
  744. static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
  745. {
  746. struct ecryptfs_crypt_stat *crypt_stat;
  747. loff_t lower_oldsize, lower_newsize;
  748. crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
  749. lower_oldsize = upper_size_to_lower_size(crypt_stat,
  750. i_size_read(inode));
  751. lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
  752. if (lower_newsize > lower_oldsize) {
  753. /*
  754. * The eCryptfs inode and the new *lower* size are mixed here
  755. * because we may not have the lower i_mutex held and/or it may
  756. * not be appropriate to call inode_newsize_ok() with inodes
  757. * from other filesystems.
  758. */
  759. return inode_newsize_ok(inode, lower_newsize);
  760. }
  761. return 0;
  762. }
  763. /**
  764. * ecryptfs_truncate
  765. * @dentry: The ecryptfs layer dentry
  766. * @new_length: The length to expand the file to
  767. *
  768. * Simple function that handles the truncation of an eCryptfs inode and
  769. * its corresponding lower inode.
  770. *
  771. * Returns zero on success; non-zero otherwise
  772. */
  773. int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
  774. {
  775. struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
  776. struct iattr lower_ia = { .ia_valid = 0 };
  777. int rc;
  778. rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length);
  779. if (rc)
  780. return rc;
  781. rc = truncate_upper(dentry, &ia, &lower_ia);
  782. if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
  783. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  784. inode_lock(d_inode(lower_dentry));
  785. rc = notify_change(lower_dentry, &lower_ia, NULL);
  786. inode_unlock(d_inode(lower_dentry));
  787. }
  788. return rc;
  789. }
  790. static int
  791. ecryptfs_permission(struct inode *inode, int mask)
  792. {
  793. return inode_permission(ecryptfs_inode_to_lower(inode), mask);
  794. }
  795. /**
  796. * ecryptfs_setattr
  797. * @dentry: dentry handle to the inode to modify
  798. * @ia: Structure with flags of what to change and values
  799. *
  800. * Updates the metadata of an inode. If the update is to the size
  801. * i.e. truncation, then ecryptfs_truncate will handle the size modification
  802. * of both the ecryptfs inode and the lower inode.
  803. *
  804. * All other metadata changes will be passed right to the lower filesystem,
  805. * and we will just update our inode to look like the lower.
  806. */
  807. static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
  808. {
  809. int rc = 0;
  810. struct dentry *lower_dentry;
  811. struct iattr lower_ia;
  812. struct inode *inode;
  813. struct inode *lower_inode;
  814. struct ecryptfs_crypt_stat *crypt_stat;
  815. crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat;
  816. if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) {
  817. rc = ecryptfs_init_crypt_stat(crypt_stat);
  818. if (rc)
  819. return rc;
  820. }
  821. inode = d_inode(dentry);
  822. lower_inode = ecryptfs_inode_to_lower(inode);
  823. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  824. mutex_lock(&crypt_stat->cs_mutex);
  825. if (d_is_dir(dentry))
  826. crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
  827. else if (d_is_reg(dentry)
  828. && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
  829. || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
  830. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  831. mount_crypt_stat = &ecryptfs_superblock_to_private(
  832. dentry->d_sb)->mount_crypt_stat;
  833. rc = ecryptfs_get_lower_file(dentry, inode);
  834. if (rc) {
  835. mutex_unlock(&crypt_stat->cs_mutex);
  836. goto out;
  837. }
  838. rc = ecryptfs_read_metadata(dentry);
  839. ecryptfs_put_lower_file(inode);
  840. if (rc) {
  841. if (!(mount_crypt_stat->flags
  842. & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
  843. rc = -EIO;
  844. printk(KERN_WARNING "Either the lower file "
  845. "is not in a valid eCryptfs format, "
  846. "or the key could not be retrieved. "
  847. "Plaintext passthrough mode is not "
  848. "enabled; returning -EIO\n");
  849. mutex_unlock(&crypt_stat->cs_mutex);
  850. goto out;
  851. }
  852. rc = 0;
  853. crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
  854. | ECRYPTFS_ENCRYPTED);
  855. }
  856. }
  857. mutex_unlock(&crypt_stat->cs_mutex);
  858. rc = setattr_prepare(dentry, ia);
  859. if (rc)
  860. goto out;
  861. if (ia->ia_valid & ATTR_SIZE) {
  862. rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
  863. if (rc)
  864. goto out;
  865. }
  866. memcpy(&lower_ia, ia, sizeof(lower_ia));
  867. if (ia->ia_valid & ATTR_FILE)
  868. lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
  869. if (ia->ia_valid & ATTR_SIZE) {
  870. rc = truncate_upper(dentry, ia, &lower_ia);
  871. if (rc < 0)
  872. goto out;
  873. }
  874. /*
  875. * mode change is for clearing setuid/setgid bits. Allow lower fs
  876. * to interpret this in its own way.
  877. */
  878. if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  879. lower_ia.ia_valid &= ~ATTR_MODE;
  880. inode_lock(d_inode(lower_dentry));
  881. rc = notify_change(lower_dentry, &lower_ia, NULL);
  882. inode_unlock(d_inode(lower_dentry));
  883. out:
  884. fsstack_copy_attr_all(inode, lower_inode);
  885. return rc;
  886. }
  887. static int ecryptfs_getattr_link(struct vfsmount *mnt, struct dentry *dentry,
  888. struct kstat *stat)
  889. {
  890. struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
  891. int rc = 0;
  892. mount_crypt_stat = &ecryptfs_superblock_to_private(
  893. dentry->d_sb)->mount_crypt_stat;
  894. generic_fillattr(d_inode(dentry), stat);
  895. if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
  896. char *target;
  897. size_t targetsiz;
  898. target = ecryptfs_readlink_lower(dentry, &targetsiz);
  899. if (!IS_ERR(target)) {
  900. kfree(target);
  901. stat->size = targetsiz;
  902. } else {
  903. rc = PTR_ERR(target);
  904. }
  905. }
  906. return rc;
  907. }
  908. static int ecryptfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  909. struct kstat *stat)
  910. {
  911. struct kstat lower_stat;
  912. int rc;
  913. rc = vfs_getattr(ecryptfs_dentry_to_lower_path(dentry), &lower_stat);
  914. if (!rc) {
  915. fsstack_copy_attr_all(d_inode(dentry),
  916. ecryptfs_inode_to_lower(d_inode(dentry)));
  917. generic_fillattr(d_inode(dentry), stat);
  918. stat->blocks = lower_stat.blocks;
  919. }
  920. return rc;
  921. }
  922. int
  923. ecryptfs_setxattr(struct dentry *dentry, struct inode *inode,
  924. const char *name, const void *value,
  925. size_t size, int flags)
  926. {
  927. int rc;
  928. struct dentry *lower_dentry;
  929. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  930. if (!(d_inode(lower_dentry)->i_opflags & IOP_XATTR)) {
  931. rc = -EOPNOTSUPP;
  932. goto out;
  933. }
  934. rc = vfs_setxattr(lower_dentry, name, value, size, flags);
  935. if (!rc && inode)
  936. fsstack_copy_attr_all(inode, d_inode(lower_dentry));
  937. out:
  938. return rc;
  939. }
  940. ssize_t
  941. ecryptfs_getxattr_lower(struct dentry *lower_dentry, struct inode *lower_inode,
  942. const char *name, void *value, size_t size)
  943. {
  944. int rc;
  945. if (!(lower_inode->i_opflags & IOP_XATTR)) {
  946. rc = -EOPNOTSUPP;
  947. goto out;
  948. }
  949. inode_lock(lower_inode);
  950. rc = __vfs_getxattr(lower_dentry, lower_inode, name, value, size);
  951. inode_unlock(lower_inode);
  952. out:
  953. return rc;
  954. }
  955. static ssize_t
  956. ecryptfs_getxattr(struct dentry *dentry, struct inode *inode,
  957. const char *name, void *value, size_t size)
  958. {
  959. return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry),
  960. ecryptfs_inode_to_lower(inode),
  961. name, value, size);
  962. }
  963. static ssize_t
  964. ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
  965. {
  966. int rc = 0;
  967. struct dentry *lower_dentry;
  968. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  969. if (!d_inode(lower_dentry)->i_op->listxattr) {
  970. rc = -EOPNOTSUPP;
  971. goto out;
  972. }
  973. inode_lock(d_inode(lower_dentry));
  974. rc = d_inode(lower_dentry)->i_op->listxattr(lower_dentry, list, size);
  975. inode_unlock(d_inode(lower_dentry));
  976. out:
  977. return rc;
  978. }
  979. static int ecryptfs_removexattr(struct dentry *dentry, struct inode *inode,
  980. const char *name)
  981. {
  982. int rc;
  983. struct dentry *lower_dentry;
  984. struct inode *lower_inode;
  985. lower_dentry = ecryptfs_dentry_to_lower(dentry);
  986. lower_inode = ecryptfs_inode_to_lower(inode);
  987. if (!(lower_inode->i_opflags & IOP_XATTR)) {
  988. rc = -EOPNOTSUPP;
  989. goto out;
  990. }
  991. inode_lock(lower_inode);
  992. rc = __vfs_removexattr(lower_dentry, name);
  993. inode_unlock(lower_inode);
  994. out:
  995. return rc;
  996. }
  997. const struct inode_operations ecryptfs_symlink_iops = {
  998. .readlink = generic_readlink,
  999. .get_link = ecryptfs_get_link,
  1000. .permission = ecryptfs_permission,
  1001. .setattr = ecryptfs_setattr,
  1002. .getattr = ecryptfs_getattr_link,
  1003. .listxattr = ecryptfs_listxattr,
  1004. };
  1005. const struct inode_operations ecryptfs_dir_iops = {
  1006. .create = ecryptfs_create,
  1007. .lookup = ecryptfs_lookup,
  1008. .link = ecryptfs_link,
  1009. .unlink = ecryptfs_unlink,
  1010. .symlink = ecryptfs_symlink,
  1011. .mkdir = ecryptfs_mkdir,
  1012. .rmdir = ecryptfs_rmdir,
  1013. .mknod = ecryptfs_mknod,
  1014. .rename = ecryptfs_rename,
  1015. .permission = ecryptfs_permission,
  1016. .setattr = ecryptfs_setattr,
  1017. .listxattr = ecryptfs_listxattr,
  1018. };
  1019. const struct inode_operations ecryptfs_main_iops = {
  1020. .permission = ecryptfs_permission,
  1021. .setattr = ecryptfs_setattr,
  1022. .getattr = ecryptfs_getattr,
  1023. .listxattr = ecryptfs_listxattr,
  1024. };
  1025. static int ecryptfs_xattr_get(const struct xattr_handler *handler,
  1026. struct dentry *dentry, struct inode *inode,
  1027. const char *name, void *buffer, size_t size)
  1028. {
  1029. return ecryptfs_getxattr(dentry, inode, name, buffer, size);
  1030. }
  1031. static int ecryptfs_xattr_set(const struct xattr_handler *handler,
  1032. struct dentry *dentry, struct inode *inode,
  1033. const char *name, const void *value, size_t size,
  1034. int flags)
  1035. {
  1036. if (value)
  1037. return ecryptfs_setxattr(dentry, inode, name, value, size, flags);
  1038. else {
  1039. BUG_ON(flags != XATTR_REPLACE);
  1040. return ecryptfs_removexattr(dentry, inode, name);
  1041. }
  1042. }
  1043. const struct xattr_handler ecryptfs_xattr_handler = {
  1044. .prefix = "", /* match anything */
  1045. .get = ecryptfs_xattr_get,
  1046. .set = ecryptfs_xattr_set,
  1047. };
  1048. const struct xattr_handler *ecryptfs_xattr_handlers[] = {
  1049. &ecryptfs_xattr_handler,
  1050. NULL
  1051. };