super.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 = kmem_cache_alloc(ecryptfs_inode_info_cache, GFP_KERNEL);
  38. if (unlikely(!inode_info))
  39. goto out;
  40. if (ecryptfs_init_crypt_stat(&inode_info->crypt_stat)) {
  41. kmem_cache_free(ecryptfs_inode_info_cache, inode_info);
  42. goto out;
  43. }
  44. mutex_init(&inode_info->lower_file_mutex);
  45. atomic_set(&inode_info->lower_file_count, 0);
  46. inode_info->lower_file = NULL;
  47. inode = &inode_info->vfs_inode;
  48. out:
  49. return inode;
  50. }
  51. static void ecryptfs_free_inode(struct inode *inode)
  52. {
  53. struct ecryptfs_inode_info *inode_info;
  54. inode_info = ecryptfs_inode_to_private(inode);
  55. kmem_cache_free(ecryptfs_inode_info_cache, inode_info);
  56. }
  57. /**
  58. * ecryptfs_destroy_inode
  59. * @inode: The ecryptfs inode
  60. *
  61. * This is used during the final destruction of the inode. All
  62. * allocation of memory related to the inode, including allocated
  63. * memory in the crypt_stat struct, will be released here.
  64. * There should be no chance that this deallocation will be missed.
  65. */
  66. static void ecryptfs_destroy_inode(struct inode *inode)
  67. {
  68. struct ecryptfs_inode_info *inode_info;
  69. inode_info = ecryptfs_inode_to_private(inode);
  70. BUG_ON(inode_info->lower_file);
  71. ecryptfs_destroy_crypt_stat(&inode_info->crypt_stat);
  72. }
  73. /**
  74. * ecryptfs_statfs
  75. * @sb: The ecryptfs super block
  76. * @buf: The struct kstatfs to fill in with stats
  77. *
  78. * Get the filesystem statistics. Currently, we let this pass right through
  79. * to the lower filesystem and take no action ourselves.
  80. */
  81. static int ecryptfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  82. {
  83. struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
  84. int rc;
  85. if (!lower_dentry->d_sb->s_op->statfs)
  86. return -ENOSYS;
  87. rc = lower_dentry->d_sb->s_op->statfs(lower_dentry, buf);
  88. if (rc)
  89. return rc;
  90. buf->f_type = ECRYPTFS_SUPER_MAGIC;
  91. rc = ecryptfs_set_f_namelen(&buf->f_namelen, buf->f_namelen,
  92. &ecryptfs_superblock_to_private(dentry->d_sb)->mount_crypt_stat);
  93. return rc;
  94. }
  95. /**
  96. * ecryptfs_evict_inode
  97. * @inode - The ecryptfs inode
  98. *
  99. * Called by iput() when the inode reference count reached zero
  100. * and the inode is not hashed anywhere. Used to clear anything
  101. * that needs to be, before the inode is completely destroyed and put
  102. * on the inode free list. We use this to drop out reference to the
  103. * lower inode.
  104. */
  105. static void ecryptfs_evict_inode(struct inode *inode)
  106. {
  107. truncate_inode_pages_final(&inode->i_data);
  108. clear_inode(inode);
  109. iput(ecryptfs_inode_to_lower(inode));
  110. }
  111. /**
  112. * ecryptfs_show_options
  113. *
  114. * Prints the mount options for a given superblock.
  115. * Returns zero; does not fail.
  116. */
  117. static int ecryptfs_show_options(struct seq_file *m, struct dentry *root)
  118. {
  119. struct super_block *sb = root->d_sb;
  120. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  121. &ecryptfs_superblock_to_private(sb)->mount_crypt_stat;
  122. struct ecryptfs_global_auth_tok *walker;
  123. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  124. list_for_each_entry(walker,
  125. &mount_crypt_stat->global_auth_tok_list,
  126. mount_crypt_stat_list) {
  127. if (walker->flags & ECRYPTFS_AUTH_TOK_FNEK)
  128. seq_printf(m, ",ecryptfs_fnek_sig=%s", walker->sig);
  129. else
  130. seq_printf(m, ",ecryptfs_sig=%s", walker->sig);
  131. }
  132. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  133. seq_printf(m, ",ecryptfs_cipher=%s",
  134. mount_crypt_stat->global_default_cipher_name);
  135. if (mount_crypt_stat->global_default_cipher_key_size)
  136. seq_printf(m, ",ecryptfs_key_bytes=%zd",
  137. mount_crypt_stat->global_default_cipher_key_size);
  138. if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)
  139. seq_printf(m, ",ecryptfs_passthrough");
  140. if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED)
  141. seq_printf(m, ",ecryptfs_xattr_metadata");
  142. if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED)
  143. seq_printf(m, ",ecryptfs_encrypted_view");
  144. if (mount_crypt_stat->flags & ECRYPTFS_UNLINK_SIGS)
  145. seq_printf(m, ",ecryptfs_unlink_sigs");
  146. if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY)
  147. seq_printf(m, ",ecryptfs_mount_auth_tok_only");
  148. return 0;
  149. }
  150. const struct super_operations ecryptfs_sops = {
  151. .alloc_inode = ecryptfs_alloc_inode,
  152. .destroy_inode = ecryptfs_destroy_inode,
  153. .free_inode = ecryptfs_free_inode,
  154. .statfs = ecryptfs_statfs,
  155. .remount_fs = NULL,
  156. .evict_inode = ecryptfs_evict_inode,
  157. .show_options = ecryptfs_show_options
  158. };