📄 main.c
字号:
.hash = 0,.name = "/",.len = 1}); if (!sb->s_root) { ecryptfs_printk(KERN_ERR, "d_alloc failed\n"); rc = -ENOMEM; goto out; } sb->s_root->d_op = &ecryptfs_dops; sb->s_root->d_sb = sb; sb->s_root->d_parent = sb->s_root; /* Released in d_release when dput(sb->s_root) is called */ /* through deactivate_super(sb) from get_sb_nodev() */ ecryptfs_set_dentry_private(sb->s_root, kmem_cache_zalloc(ecryptfs_dentry_info_cache, GFP_KERNEL)); if (!ecryptfs_dentry_to_private(sb->s_root)) { ecryptfs_printk(KERN_ERR, "dentry_info_cache alloc failed\n"); rc = -ENOMEM; goto out; } rc = 0;out: /* Should be able to rely on deactivate_super called from * get_sb_nodev */ return rc;}/** * ecryptfs_read_super * @sb: The ecryptfs super block * @dev_name: The path to mount over * * Read the super block of the lower filesystem, and use * ecryptfs_interpose to create our initial inode and super block * struct. */static int ecryptfs_read_super(struct super_block *sb, const char *dev_name){ int rc; struct nameidata nd; struct dentry *lower_root; struct vfsmount *lower_mnt; memset(&nd, 0, sizeof(struct nameidata)); rc = path_lookup(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd); if (rc) { ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n"); goto out; } lower_root = nd.dentry; lower_mnt = nd.mnt; ecryptfs_set_superblock_lower(sb, lower_root->d_sb); sb->s_maxbytes = lower_root->d_sb->s_maxbytes; sb->s_blocksize = lower_root->d_sb->s_blocksize; ecryptfs_set_dentry_lower(sb->s_root, lower_root); ecryptfs_set_dentry_lower_mnt(sb->s_root, lower_mnt); rc = ecryptfs_interpose(lower_root, sb->s_root, sb, 0); if (rc) goto out_free; rc = 0; goto out;out_free: path_release(&nd);out: return rc;}/** * ecryptfs_get_sb * @fs_type * @flags * @dev_name: The path to mount over * @raw_data: The options passed into the kernel * * The whole ecryptfs_get_sb process is broken into 4 functions: * ecryptfs_parse_options(): handle options passed to ecryptfs, if any * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block * with as much information as it can before needing * the lower filesystem. * ecryptfs_read_super(): this accesses the lower filesystem and uses * ecryptfs_interpolate to perform most of the linking * ecryptfs_interpolate(): links the lower filesystem into ecryptfs */static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt){ int rc; struct super_block *sb; rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt); if (rc < 0) { printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc); goto out; } sb = mnt->mnt_sb; rc = ecryptfs_parse_options(sb, raw_data); if (rc) { printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc); goto out_abort; } rc = ecryptfs_read_super(sb, dev_name); if (rc) { printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc); goto out_abort; } goto out;out_abort: dput(sb->s_root); up_write(&sb->s_umount); deactivate_super(sb);out: return rc;}/** * ecryptfs_kill_block_super * @sb: The ecryptfs super block * * Used to bring the superblock down and free the private data. * Private data is free'd in ecryptfs_put_super() */static void ecryptfs_kill_block_super(struct super_block *sb){ generic_shutdown_super(sb);}static struct file_system_type ecryptfs_fs_type = { .owner = THIS_MODULE, .name = "ecryptfs", .get_sb = ecryptfs_get_sb, .kill_sb = ecryptfs_kill_block_super, .fs_flags = 0};/** * inode_info_init_once * * Initializes the ecryptfs_inode_info_cache when it is created */static voidinode_info_init_once(struct kmem_cache *cachep, void *vptr){ struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr; inode_init_once(&ei->vfs_inode);}static struct ecryptfs_cache_info { struct kmem_cache **cache; const char *name; size_t size; void (*ctor)(struct kmem_cache *cache, void *obj);} ecryptfs_cache_infos[] = { { .cache = &ecryptfs_auth_tok_list_item_cache, .name = "ecryptfs_auth_tok_list_item", .size = sizeof(struct ecryptfs_auth_tok_list_item), }, { .cache = &ecryptfs_file_info_cache, .name = "ecryptfs_file_cache", .size = sizeof(struct ecryptfs_file_info), }, { .cache = &ecryptfs_dentry_info_cache, .name = "ecryptfs_dentry_info_cache", .size = sizeof(struct ecryptfs_dentry_info), }, { .cache = &ecryptfs_inode_info_cache, .name = "ecryptfs_inode_cache", .size = sizeof(struct ecryptfs_inode_info), .ctor = inode_info_init_once, }, { .cache = &ecryptfs_sb_info_cache, .name = "ecryptfs_sb_cache", .size = sizeof(struct ecryptfs_sb_info), }, { .cache = &ecryptfs_header_cache_0, .name = "ecryptfs_headers_0", .size = PAGE_CACHE_SIZE, }, { .cache = &ecryptfs_header_cache_1, .name = "ecryptfs_headers_1", .size = PAGE_CACHE_SIZE, }, { .cache = &ecryptfs_header_cache_2, .name = "ecryptfs_headers_2", .size = PAGE_CACHE_SIZE, }, { .cache = &ecryptfs_xattr_cache, .name = "ecryptfs_xattr_cache", .size = PAGE_CACHE_SIZE, }, { .cache = &ecryptfs_key_record_cache, .name = "ecryptfs_key_record_cache", .size = sizeof(struct ecryptfs_key_record), }, { .cache = &ecryptfs_key_sig_cache, .name = "ecryptfs_key_sig_cache", .size = sizeof(struct ecryptfs_key_sig), }, { .cache = &ecryptfs_global_auth_tok_cache, .name = "ecryptfs_global_auth_tok_cache", .size = sizeof(struct ecryptfs_global_auth_tok), }, { .cache = &ecryptfs_key_tfm_cache, .name = "ecryptfs_key_tfm_cache", .size = sizeof(struct ecryptfs_key_tfm), },};static void ecryptfs_free_kmem_caches(void){ int i; for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { struct ecryptfs_cache_info *info; info = &ecryptfs_cache_infos[i]; if (*(info->cache)) kmem_cache_destroy(*(info->cache)); }}/** * ecryptfs_init_kmem_caches * * Returns zero on success; non-zero otherwise */static int ecryptfs_init_kmem_caches(void){ int i; for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { struct ecryptfs_cache_info *info; info = &ecryptfs_cache_infos[i]; *(info->cache) = kmem_cache_create(info->name, info->size, 0, SLAB_HWCACHE_ALIGN, info->ctor); if (!*(info->cache)) { ecryptfs_free_kmem_caches(); ecryptfs_printk(KERN_WARNING, "%s: " "kmem_cache_create failed\n", info->name); return -ENOMEM; } } return 0;}struct ecryptfs_obj { char *name; struct list_head slot_list; struct kobject kobj;};struct ecryptfs_attribute { struct attribute attr; ssize_t(*show) (struct ecryptfs_obj *, char *); ssize_t(*store) (struct ecryptfs_obj *, const char *, size_t);};static ssize_tecryptfs_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t len){ struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj, kobj); struct ecryptfs_attribute *attribute = container_of(attr, struct ecryptfs_attribute, attr); return (attribute->store ? attribute->store(obj, buf, len) : 0);}static ssize_tecryptfs_attr_show(struct kobject *kobj, struct attribute *attr, char *buf){ struct ecryptfs_obj *obj = container_of(kobj, struct ecryptfs_obj, kobj); struct ecryptfs_attribute *attribute = container_of(attr, struct ecryptfs_attribute, attr); return (attribute->show ? attribute->show(obj, buf) : 0);}static struct sysfs_ops ecryptfs_sysfs_ops = { .show = ecryptfs_attr_show, .store = ecryptfs_attr_store};static struct kobj_type ecryptfs_ktype = { .sysfs_ops = &ecryptfs_sysfs_ops};static decl_subsys(ecryptfs, &ecryptfs_ktype, NULL);static ssize_t version_show(struct ecryptfs_obj *obj, char *buff){ return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK);}static struct ecryptfs_attribute sysfs_attr_version = __ATTR_RO(version);static struct ecryptfs_version_str_map_elem { u32 flag; char *str;} ecryptfs_version_str_map[] = { {ECRYPTFS_VERSIONING_PASSPHRASE, "passphrase"}, {ECRYPTFS_VERSIONING_PUBKEY, "pubkey"}, {ECRYPTFS_VERSIONING_PLAINTEXT_PASSTHROUGH, "plaintext passthrough"}, {ECRYPTFS_VERSIONING_POLICY, "policy"}, {ECRYPTFS_VERSIONING_XATTR, "metadata in extended attribute"}, {ECRYPTFS_VERSIONING_MULTKEY, "multiple keys per file"}};static ssize_t version_str_show(struct ecryptfs_obj *obj, char *buff){ int i; int remaining = PAGE_SIZE; int total_written = 0; buff[0] = '\0'; for (i = 0; i < ARRAY_SIZE(ecryptfs_version_str_map); i++) { int entry_size; if (!(ECRYPTFS_VERSIONING_MASK & ecryptfs_version_str_map[i].flag)) continue; entry_size = strlen(ecryptfs_version_str_map[i].str); if ((entry_size + 2) > remaining) goto out; memcpy(buff, ecryptfs_version_str_map[i].str, entry_size); buff[entry_size++] = '\n'; buff[entry_size] = '\0'; buff += entry_size; total_written += entry_size; remaining -= entry_size; }out: return total_written;}static struct ecryptfs_attribute sysfs_attr_version_str = __ATTR_RO(version_str);static int do_sysfs_registration(void){ int rc; rc = subsystem_register(&ecryptfs_subsys); if (rc) { printk(KERN_ERR "Unable to register ecryptfs sysfs subsystem\n"); goto out; } rc = sysfs_create_file(&ecryptfs_subsys.kobj, &sysfs_attr_version.attr); if (rc) { printk(KERN_ERR "Unable to create ecryptfs version attribute\n"); subsystem_unregister(&ecryptfs_subsys); goto out; } rc = sysfs_create_file(&ecryptfs_subsys.kobj, &sysfs_attr_version_str.attr); if (rc) { printk(KERN_ERR "Unable to create ecryptfs version_str attribute\n"); sysfs_remove_file(&ecryptfs_subsys.kobj, &sysfs_attr_version.attr); subsystem_unregister(&ecryptfs_subsys); goto out; }out: return rc;}static void do_sysfs_unregistration(void){ sysfs_remove_file(&ecryptfs_subsys.kobj, &sysfs_attr_version.attr); sysfs_remove_file(&ecryptfs_subsys.kobj, &sysfs_attr_version_str.attr); subsystem_unregister(&ecryptfs_subsys);}static int __init ecryptfs_init(void){ int rc; if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) { rc = -EINVAL; ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is " "larger than the host's page size, and so " "eCryptfs cannot run on this system. The " "default eCryptfs extent size is [%d] bytes; " "the page size is [%d] bytes.\n", ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE); goto out; } rc = ecryptfs_init_kmem_caches(); if (rc) { printk(KERN_ERR "Failed to allocate one or more kmem_cache objects\n"); goto out; } rc = register_filesystem(&ecryptfs_fs_type); if (rc) { printk(KERN_ERR "Failed to register filesystem\n"); goto out_free_kmem_caches; } kobj_set_kset_s(&ecryptfs_subsys, fs_subsys); rc = do_sysfs_registration(); if (rc) { printk(KERN_ERR "sysfs registration failed\n"); goto out_unregister_filesystem; } rc = ecryptfs_init_messaging(ecryptfs_transport); if (rc) { ecryptfs_printk(KERN_ERR, "Failure occured while attempting to " "initialize the eCryptfs netlink socket\n"); goto out_do_sysfs_unregistration; } rc = ecryptfs_init_crypto(); if (rc) { printk(KERN_ERR "Failure whilst attempting to init crypto; " "rc = [%d]\n", rc); goto out_release_messaging; } goto out;out_release_messaging: ecryptfs_release_messaging(ecryptfs_transport);out_do_sysfs_unregistration: do_sysfs_unregistration();out_unregister_filesystem: unregister_filesystem(&ecryptfs_fs_type);out_free_kmem_caches: ecryptfs_free_kmem_caches();out: return rc;}static void __exit ecryptfs_exit(void){ int rc; rc = ecryptfs_destroy_crypto(); if (rc) printk(KERN_ERR "Failure whilst attempting to destroy crypto; " "rc = [%d]\n", rc); ecryptfs_release_messaging(ecryptfs_transport); do_sysfs_unregistration(); unregister_filesystem(&ecryptfs_fs_type); ecryptfs_free_kmem_caches();}MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>");MODULE_DESCRIPTION("eCryptfs");MODULE_LICENSE("GPL");module_init(ecryptfs_init)module_exit(ecryptfs_exit)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -