inode.c 34 KB

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