super.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * eCryptfs: Linux filesystem encryption layer
  4. *
  5. * Copyright (C) 1997-2003 Erez Zadok
  6. * Copyright (C) 2001-2003 Stony Brook University
  7. * Copyright (C) 2004-2006 International Business Machines Corp.
  8. * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
  9. * Michael C. Thompson <mcthomps@us.ibm.com>
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/mount.h>
  13. #include <linux/key.h>
  14. #include <linux/slab.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/file.h>
  17. #include <linux/statfs.h>
  18. #include <linux/magic.h>
  19. #include "ecryptfs_kernel.h"
  20. struct kmem_cache *ecryptfs_inode_info_cache;
  21. /**
  22. * ecryptfs_alloc_inode - allocate an ecryptfs inode
  23. * @sb: Pointer to the ecryptfs super block
  24. *
  25. * Called to bring an inode into existence.
  26. *
  27. * Only handle allocation, setting up structures should be done in
  28. * ecryptfs_read_inode. This is because the kernel, between now and
  29. * then, will 0 out the private data pointer.
  30. *
  31. * Returns a pointer to a newly allocated inode, NULL otherwise
  32. */
  33. static struct inode *ecryptfs_alloc_inode(struct super_block *sb)
  34. {
  35. struct ecryptfs_inode_info *inode_info;
  36. struct inode *inode = NULL;
  37. inode_info = alloc_inode_sb(sb, ecryptfs_inode_info_cache, GFP_KERNEL);
  38. if (unlikely(!inode_info))
  39. goto out;
  40. ecryptfs_init_crypt_stat(&inode_info->crypt_stat);
  41. mutex_init(&inode_info->lower_file_mutex);
  42. atomic_set(&inode_info->lower_file_count, 0);
  43. inode_info->lower_file = NULL;
  44. inode = &inode_info->vfs_inode;
  45. out:
  46. return inode;
  47. }
  48. static void ecryptfs_free_inode(struct inode *inode)
  49. {
  50. struct ecryptfs_inode_info *inode_info;
  51. inode_info = ecryptfs_inode_to_private(inode);
  52. kmem_cache_free(ecryptfs_inode_info_cache, inode_info);
  53. }
  54. /**
  55. * ecryptfs_destroy_inode
  56. * @inode: The ecryptfs inode
  57. *
  58. * This is used during the final destruction of the inode. All
  59. * allocation of memory related to the inode, including allocated
  60. * memory in the crypt_stat struct, will be released here.
  61. * There should be no chance that this deallocation will be missed.
  62. */
  63. static void ecryptfs_destroy_inode(struct inode *inode)
  64. {
  65. struct ecryptfs_inode_info *inode_info;
  66. inode_info = ecryptfs_inode_to_private(inode);
  67. BUG_ON(inode_info->lower_file);
  68. ecryptfs_destroy_crypt_stat(&inode_info->crypt_stat);
  69. }
  70. /**
  71. * ecryptfs_statfs
  72. * @dentry: The ecryptfs dentry
  73. * @buf: The struct kstatfs to fill in with stats
  74. *
  75. * Get the filesystem statistics. Currently, we let this pass right through
  76. * to the lower filesystem and take no action ourselves.
  77. */
  78. static int ecryptfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  79. {
  80. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  81. int rc;
  82. if (!lower_dentry->d_sb->s_op->statfs)
  83. return -ENOSYS;
  84. rc = lower_dentry->d_sb->s_op->statfs(lower_dentry, buf);
  85. if (rc)
  86. return rc;
  87. buf->f_type = ECRYPTFS_SUPER_MAGIC;
  88. rc = ecryptfs_set_f_namelen(&buf->f_namelen, buf->f_namelen,
  89. &ecryptfs_superblock_to_private(dentry->d_sb)->mount_crypt_stat);
  90. return rc;
  91. }
  92. /**
  93. * ecryptfs_evict_inode
  94. * @inode: The ecryptfs inode
  95. *
  96. * Called by iput() when the inode reference count reached zero
  97. * and the inode is not hashed anywhere. Used to clear anything
  98. * that needs to be, before the inode is completely destroyed and put
  99. * on the inode free list. We use this to drop out reference to the
  100. * lower inode.
  101. */
  102. static void ecryptfs_evict_inode(struct inode *inode)
  103. {
  104. truncate_inode_pages_final(&inode->i_data);
  105. clear_inode(inode);
  106. iput(ecryptfs_inode_to_lower(inode));
  107. }
  108. /*
  109. * ecryptfs_show_options
  110. *
  111. * Prints the mount options for a given superblock.
  112. * Returns zero; does not fail.
  113. */
  114. static int ecryptfs_show_options(struct seq_file *m, struct dentry *root)
  115. {
  116. struct super_block *sb = root->d_sb;
  117. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  118. &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
  119. struct ecryptfs_global_auth_tok *walker;
  120. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  121. list_for_each_entry(walker,
  122. &mount_crypt_stat->global_auth_tok_list,
  123. mount_crypt_stat_list) {
  124. if (walker->flags & ECRYPTFS_AUTH_TOK_FNEK)
  125. seq_printf(m, ",ecryptfs_fnek_sig=%s", walker->sig);
  126. else
  127. seq_printf(m, ",ecryptfs_sig=%s", walker->sig);
  128. }
  129. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  130. seq_printf(m, ",ecryptfs_cipher=%s",
  131. mount_crypt_stat->global_default_cipher_name);
  132. if (mount_crypt_stat->global_default_cipher_key_size)
  133. seq_printf(m, ",ecryptfs_key_bytes=%zd",
  134. mount_crypt_stat->global_default_cipher_key_size);
  135. if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)
  136. seq_printf(m, ",ecryptfs_passthrough");
  137. if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
  138. seq_printf(m, ",ecryptfs_xattr_metadata");
  139. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  140. seq_printf(m, ",ecryptfs_encrypted_view");
  141. if (mount_crypt_stat->flags & ECRYPTFS_UNLINK_SIGS)
  142. seq_printf(m, ",ecryptfs_unlink_sigs");
  143. if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY)
  144. seq_printf(m, ",ecryptfs_mount_auth_tok_only");
  145. return 0;
  146. }
  147. const struct super_operations ecryptfs_sops = {
  148. .alloc_inode = ecryptfs_alloc_inode,
  149. .destroy_inode = ecryptfs_destroy_inode,
  150. .free_inode = ecryptfs_free_inode,
  151. .statfs = ecryptfs_statfs,
  152. .evict_inode = ecryptfs_evict_inode,
  153. .show_options = ecryptfs_show_options
  154. };