diff --git a/fs/Kconfig b/fs/Kconfig index b95f212be39e31501f342b42bdada3d3b1f4b1df..05c389c7a6001d5ce3fb30fe7d17958d875d1cf6 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -23,6 +23,7 @@ config FS_IOMAP source "fs/ext2/Kconfig" source "fs/ext4/Kconfig" source "fs/hmdfs/Kconfig" +source "fs/sharefs/Kconfig" source "fs/jbd2/Kconfig" config FS_MBCACHE diff --git a/fs/Makefile b/fs/Makefile index d71954aaba20e3adf2e640c5f91549605d71af69..d00c7a2e854ed3c599602339e698c0b1c8781f0b 100644 --- a/fs/Makefile +++ b/fs/Makefile @@ -72,6 +72,7 @@ obj-$(CONFIG_FSCACHE) += fscache/ obj-$(CONFIG_REISERFS_FS) += reiserfs/ obj-$(CONFIG_EXT4_FS) += ext4/ obj-$(CONFIG_HMDFS_FS) += hmdfs/ +obj-$(CONFIG_SHARE_FS) += sharefs/ # We place ext4 before ext2 so that clean ext3 root fs's do NOT mount using the # ext2 driver, which doesn't know about journalling! Explicitly request ext2 # by giving the rootfstype= parameter. diff --git a/fs/sharefs/Kconfig b/fs/sharefs/Kconfig new file mode 100644 index 0000000000000000000000000000000000000000..bce24bfdc21e5d2b86db3c3c1d8f65b3eab88ea9 --- /dev/null +++ b/fs/sharefs/Kconfig @@ -0,0 +1,6 @@ +config SHARE_FS + tristate "SHAREFS filesystem support" + help + SHAREFS is an overlay file system.SHAREFS is used for file sharing + between applications. Sharefs manages permissions through different + permissions for reading and writing directories. diff --git a/fs/sharefs/Makefile b/fs/sharefs/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..9b84e26d1cf6cce309297568ecaf32a4cb38bd58 --- /dev/null +++ b/fs/sharefs/Makefile @@ -0,0 +1,12 @@ +obj-$(CONFIG_SHARE_FS) += sharefs.o +ccflags-y += -I$(src) + +sharefs-y := dentry.o file.o inode.o main.o super.o lookup.o authentication.o config.o +ccflags-y += -I$(src) -Werror -Wall +export CONFIG_SHARE_FS := m +KDIR ::= /lib/modules/$(shell uname -r)/build +PWD := $(shell pwd) +all: + $(MAKE) -C $(KDIR) M=$(PWD) modules +clean: + $(MAKE) -C $(KDIR) M=$(PWD) clean \ No newline at end of file diff --git a/fs/sharefs/authentication.c b/fs/sharefs/authentication.c new file mode 100644 index 0000000000000000000000000000000000000000..57756e479feaeb00c3385f50e1d45dbd1d326560 --- /dev/null +++ b/fs/sharefs/authentication.c @@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * fs/sharefs/authentication.c + * + * Copyright (c) 2023 Huawei Device Co., Ltd. + */ +#include "authentication.h" + +static inline __u16 perm_get_next_level(__u16 perm) +{ + __u16 level = (perm & SHAREFS_PERM_MASK) + 1; + + if (level <= SHAREFS_PERM_OTHER) + return level; + else + return SHAREFS_PERM_OTHER; +} + +void fixup_perm_from_level(struct inode *dir, struct dentry *dentry) +{ + struct sharefs_inode_info *hii = SHAREFS_I(dir); + struct inode *dinode = d_inode(dentry); + struct sharefs_inode_info *dinfo = SHAREFS_I(dinode); + const unsigned char* cur_name = dentry->d_name.name; + __u16 level = perm_get_next_level(hii->perm); + __u16 perm = 0; + + if (IS_ERR_OR_NULL(dinode)) + return; + dinode->i_uid = dir->i_uid; + dinode->i_gid = dir->i_gid; + switch (level) + { + case SHAREFS_PERM_MNT: + int bid = get_bundle_uid(SHAREFS_SB(dentry->d_sb), + dentry->d_name.name); + perm = level; + if (bid != 0) { + dinode->i_uid = KUIDT_INIT(bid); + dinode->i_gid = KGIDT_INIT(bid); + } else { + dinode->i_uid = ROOT_UID; + dinode->i_gid = ROOT_GID; + } + dinode->i_mode = (dinode->i_mode & S_IFMT) | SHAREFS_PERM_READONLY_DIR; + break; + case SHAREFS_PERM_DFS: + if (!strcmp(cur_name, SHAREFS_READ_DIR)) { + perm = SHAREFS_DIR_TYPE_READONLY | level; + sharefs_set_read_perm(dinode); + } else if (!strcmp(cur_name, SHAREFS_READWRITE_DIR)) { + perm = SHAREFS_DIR_TYPE_READWRITE | level; + sharefs_set_read_write_perm(dinode); + } + break; + case SHAREFS_PERM_OTHER: + if (is_read_only_auth(hii->perm)) { + perm = SHAREFS_DIR_TYPE_READONLY | SHAREFS_PERM_DFS; + sharefs_set_read_perm(dinode); + } else if (is_read_write_auth(hii->perm)) { + perm = SHAREFS_DIR_TYPE_READWRITE | SHAREFS_PERM_DFS; + sharefs_set_read_write_perm(dinode); + } + break; + default: + /* ! it should not get to here */ + sharefs_err("sharedfs perm incorrect got default case, level:%u", level); + break; + } + dinfo->perm = perm; +} + +void sharefs_root_inode_perm_init(struct inode *root_inode) +{ + struct sharefs_inode_info *hii = SHAREFS_I(root_inode); + hii->perm = SHAREFS_PERM_FIX; +} \ No newline at end of file diff --git a/fs/sharefs/authentication.h b/fs/sharefs/authentication.h new file mode 100644 index 0000000000000000000000000000000000000000..84a1a8dcd660b3b26938af94e2e6ca2d2e1e1483 --- /dev/null +++ b/fs/sharefs/authentication.h @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * fs/sharefs/authentication.h + * + * Copyright (c) 2023 Huawei Device Co., Ltd. + */ + +#include "sharefs.h" + +#define OID_ROOT 0 + +#define SHAREFS_PERM_MASK 0x000F + +#define SHAREFS_PERM_FIX 0 +#define SHAREFS_PERM_MNT 1 +#define SHAREFS_PERM_DFS 2 +#define SHAREFS_PERM_OTHER 3 + +#define SHAREFS_READ_DIR "r" +#define SHAREFS_READWRITE_DIR "rw" + +#define BASE_USER_RANGE 200000 /* offset for uid ranges for each user */ + + +#define SHAREFS_DIR_TYPE_MASK 0x00F0 +#define SHAREFS_DIR_TYPE_READONLY 0x0010 +#define SHAREFS_DIR_TYPE_READWRITE 0x0020 + +#define SHAREFS_PERM_READONLY_DIR 00550 +#define SHAREFS_PERM_READONLY_FILE 00440 +#define SHAREFS_PERM_READWRITE_DIR 00550 +#define SHAREFS_PERM_READWRITE_FILE 00660 + +extern int get_bid_config(const char *bname); +extern int __init sharefs_init_configfs(void); +extern void sharefs_exit_configfs(void); + +void sharefs_root_inode_perm_init(struct inode *root_inode); +void fixup_perm_from_level(struct inode *dir, struct dentry *dentry); + +static inline bool is_read_only_auth(__u16 perm) +{ + return (perm & SHAREFS_DIR_TYPE_MASK) == SHAREFS_DIR_TYPE_READONLY; +} + +static inline bool is_read_write_auth(__u16 perm) +{ + return (perm & SHAREFS_DIR_TYPE_MASK) == SHAREFS_DIR_TYPE_READWRITE; +} + +static inline void sharefs_set_read_perm(struct inode *inode) { + if (S_ISDIR(inode->i_mode)) + inode->i_mode = (inode->i_mode & S_IFMT) | SHAREFS_PERM_READONLY_DIR; + else + inode->i_mode = (inode->i_mode & S_IFMT) | SHAREFS_PERM_READONLY_FILE; +} + +static inline void sharefs_set_read_write_perm(struct inode *inode) { + if (S_ISDIR(inode->i_mode)) + inode->i_mode = (inode->i_mode & S_IFMT) | SHAREFS_PERM_READWRITE_DIR; + else + inode->i_mode = (inode->i_mode & S_IFMT) | SHAREFS_PERM_READWRITE_FILE; +} + +static inline int get_bundle_uid(struct sharefs_sb_info *sbi, const char *bname) +{ + return sbi->user_id * BASE_USER_RANGE + get_bid_config(bname); +} diff --git a/fs/sharefs/config.c b/fs/sharefs/config.c new file mode 100644 index 0000000000000000000000000000000000000000..35f682a1ed6fc7a73ed005d4076a5dfa690fb865 --- /dev/null +++ b/fs/sharefs/config.c @@ -0,0 +1,374 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * fs/sharefs/config.c + * + * Copyright (c) 2023 Huawei Device Co., Ltd. + */ + +#include +#include +#include +#include +#include +#include "sharefs.h" + +static struct kmem_cache *sharefs_bid_entry_cachep; + +struct sharefs_bid_entry { + struct hlist_node node; + struct qstr str; + int id; +}; + +struct sharefs_config_bitem { + struct config_item item; + struct qstr str; +}; + +static unsigned int make_hash(const char *name, unsigned int len) +{ + unsigned long hash; + + hash = init_name_hash(0); + while (len--) + hash = partial_name_hash(tolower(*name++), hash); + + return end_name_hash(hash); +} + +static struct qstr make_qstr(const char *name) +{ + struct qstr str; + str.name = name; + str.len = strlen(name); + str.hash = make_hash(str.name, str.len); + + return str; +} + +static struct sharefs_bid_entry *alloc_bid_entry(const char *name, int id) +{ + struct sharefs_bid_entry *bid_entry; + char *bid_entry_name; + + bid_entry = kmem_cache_alloc(sharefs_bid_entry_cachep, GFP_KERNEL); + if (!bid_entry) { + bid_entry = ERR_PTR(-ENOMEM); + goto out; + } + + bid_entry_name = kstrdup(name, GFP_KERNEL); + if (!bid_entry_name) { + kmem_cache_free(sharefs_bid_entry_cachep, bid_entry); + bid_entry = ERR_PTR(-ENOMEM); + goto out; + } + + INIT_HLIST_NODE(&bid_entry->node); + bid_entry->str = make_qstr(bid_entry_name); + bid_entry->id = id; +out: + return bid_entry; +} + +static void free_bid_entry(struct sharefs_bid_entry *bid_entry) +{ + if (bid_entry == NULL) + return; + + kfree(bid_entry->str.name); + kmem_cache_free(sharefs_bid_entry_cachep, bid_entry); +} + +static struct sharefs_config_bitem *alloc_bitem(const char *name) +{ + struct sharefs_config_bitem *bitem; + char *bitem_name; + + bitem = kzalloc(sizeof(*bitem), GFP_KERNEL); + if (!bitem) { + bitem = ERR_PTR(-ENOMEM); + goto out; + } + + bitem_name = kstrdup(name, GFP_KERNEL); + if (!bitem_name) { + kfree(bitem); + bitem = ERR_PTR(-ENOMEM); + goto out; + } + + bitem->str = make_qstr(bitem_name); +out: + return bitem; +} + +static void free_bitem(struct sharefs_config_bitem *bitem) +{ + if (bitem == NULL) + return; + + kfree(bitem->str.name); + kfree(bitem); +} + +#define SHAREFS_BUNDLE_ATTRIBUTE(_attr_) \ + \ +static DEFINE_HASHTABLE(sharefs_##_attr_##_hash_table, 4); \ + \ +static DEFINE_MUTEX(sharefs_##_attr_##_hash_mutex); \ + \ +static int query_##_attr_##_hash_entry(struct qstr *str) \ +{ \ + int id = 0; \ + struct sharefs_bid_entry *bid_entry; \ + struct hlist_node *hash_node; \ + \ + mutex_lock(&sharefs_##_attr_##_hash_mutex); \ + hash_for_each_possible_safe(sharefs_##_attr_##_hash_table, \ + bid_entry, hash_node, node, str->hash) { \ + if (qstr_case_eq(str, &bid_entry->str)) { \ + id = bid_entry->id; \ + break; \ + } \ + } \ + mutex_unlock(&sharefs_##_attr_##_hash_mutex); \ + \ + return id; \ +} \ + \ +static int insert_##_attr_##_hash_entry(struct qstr *str, int id) \ +{ \ + int err = 0; \ + struct sharefs_bid_entry *bid_entry; \ + struct hlist_node *hash_node; \ + \ + sharefs_info("insert name = %s", str->name); \ + \ + mutex_lock(&sharefs_##_attr_##_hash_mutex); \ + hash_for_each_possible_safe(sharefs_##_attr_##_hash_table, \ + bid_entry, hash_node, node, str->hash) { \ + if (qstr_case_eq(str, &bid_entry->str)) { \ + bid_entry->id = id; \ + mutex_unlock(&sharefs_##_attr_##_hash_mutex); \ + goto out; \ + } \ + } \ + mutex_unlock(&sharefs_##_attr_##_hash_mutex); \ + \ + bid_entry = alloc_bid_entry(str->name, id); \ + if (IS_ERR(bid_entry)) { \ + err = PTR_ERR(bid_entry); \ + goto out; \ + } \ + \ + hash_add_rcu(sharefs_##_attr_##_hash_table, &bid_entry->node, \ + bid_entry->str.hash); \ +out: \ + return err; \ +} \ + \ +static void remove_##_attr_##_hash_entry(struct qstr *str) \ +{ \ + struct sharefs_bid_entry *bid_entry; \ + struct hlist_node *hash_node; \ + \ + sharefs_info("remove name = %s", str->name); \ + \ + mutex_lock(&sharefs_##_attr_##_hash_mutex); \ + hash_for_each_possible_safe(sharefs_##_attr_##_hash_table, \ + bid_entry, hash_node, node, str->hash) { \ + if (qstr_case_eq(str, &bid_entry->str)) { \ + hash_del_rcu(&bid_entry->node); \ + free_bid_entry(bid_entry); \ + break; \ + } \ + } \ + mutex_unlock(&sharefs_##_attr_##_hash_mutex); \ +} \ + \ +static void clear_##_attr_##_hash_entry(void) \ +{ \ + int index; \ + struct sharefs_bid_entry *bid_entry; \ + struct hlist_node *hash_node; \ + \ + sharefs_info("clear bid entry"); \ + \ + mutex_lock(&sharefs_##_attr_##_hash_mutex); \ + hash_for_each_safe(sharefs_##_attr_##_hash_table, index, \ + hash_node, bid_entry, node) { \ + hash_del_rcu(&bid_entry->node); \ + kfree(bid_entry->str.name); \ + kmem_cache_free(sharefs_bid_entry_cachep, bid_entry); \ + } \ + mutex_unlock(&sharefs_##_attr_##_hash_mutex); \ +} \ + \ +static int sharefs_##_attr_##_get(const char *bname) \ +{ \ + struct qstr str; \ + \ + str = make_qstr(bname); \ + return query_##_attr_##_hash_entry(&str); \ +} \ + \ +static ssize_t sharefs_##_attr_##_show(struct config_item *item, \ + char *page) \ +{ \ + int id; \ + struct sharefs_config_bitem *bitem; \ + \ + sharefs_info("show bundle id"); \ + \ + bitem = container_of(item, struct sharefs_config_bitem, item); \ + id = query_##_attr_##_hash_entry(&bitem->str); \ + \ + return scnprintf(page, PAGE_SIZE, "%u\n", id); \ +} \ + \ +static ssize_t sharefs_##_attr_##_store(struct config_item *item, \ + const char *page, size_t count) \ +{ \ + int id; \ + int err; \ + size_t size; \ + struct sharefs_config_bitem *bitem; \ + \ + sharefs_info("store bundle id"); \ + \ + bitem = container_of(item, struct sharefs_config_bitem, item); \ + \ + if (kstrtouint(page, 10, &id)) { \ + size = -EINVAL; \ + goto out; \ + } \ + \ + err = insert_##_attr_##_hash_entry(&bitem->str, id); \ + if (err) { \ + size = err; \ + goto out; \ + } \ + \ + size = count; \ +out: \ + return size; \ +} \ + \ +static struct configfs_attribute sharefs_##_attr_##_attr = { \ + .ca_name = __stringify(_attr_), \ + .ca_mode = S_IRUGO | S_IWUGO, \ + .ca_owner = THIS_MODULE, \ + .show = sharefs_##_attr_##_show, \ + .store = sharefs_##_attr_##_store, \ +}; + +SHAREFS_BUNDLE_ATTRIBUTE(bid) + +static struct configfs_attribute *sharefs_battrs[] = { + &sharefs_bid_attr, + NULL, +}; + +static void sharefs_config_bitem_release(struct config_item *item) +{ + struct sharefs_config_bitem *bitem; + + sharefs_info("release bundle item"); + + bitem = container_of(item, struct sharefs_config_bitem, item); + remove_bid_hash_entry(&bitem->str); + remove_bid_hash_entry(&bitem->str); + free_bitem(bitem); +} + +static struct configfs_item_operations sharefs_config_bitem_ops = { + .release = sharefs_config_bitem_release, +}; + +static struct config_item_type sharefs_config_bitem_type = { + .ct_item_ops = &sharefs_config_bitem_ops, + .ct_attrs = sharefs_battrs, + .ct_owner = THIS_MODULE, +}; + +static struct config_item *sharefs_make_bitem(struct config_group *group, + const char *name) +{ + struct config_item *item; + struct sharefs_config_bitem *bitem; + + sharefs_info("make bundle item = %s", name); + + bitem = alloc_bitem(name); + if (IS_ERR(bitem)) { + item = ERR_PTR(-ENOMEM); + goto out; + } + + config_item_init_type_name(&bitem->item, name, + &sharefs_config_bitem_type); + item = &bitem->item; +out: + return item; +} + +static struct configfs_group_operations sharefs_group_ops = { + .make_item = sharefs_make_bitem, +}; + +static struct config_item_type sharefs_group_type = { + .ct_group_ops = &sharefs_group_ops, + .ct_owner = THIS_MODULE, +}; + +static struct configfs_subsystem sharefs_subsystem = { + .su_group = { + .cg_item = { + .ci_namebuf = "sharefs", + .ci_type = &sharefs_group_type, + }, + }, +}; + +int get_bid_config(const char *bname) +{ + return sharefs_bid_get(bname); +} + +int __init sharefs_init_configfs(void) +{ + int err; + struct configfs_subsystem *subsys; + + sharefs_info("init configfs"); + + sharefs_bid_entry_cachep = kmem_cache_create("sharefs_bid_entry_cachep", + sizeof(struct sharefs_bid_entry), 0, 0, NULL); + if (!sharefs_bid_entry_cachep) { + sharefs_err("failed to create bid entry cachep"); + err = -ENOMEM; + goto out; + } + + subsys = &sharefs_subsystem; + config_group_init(&subsys->su_group); + mutex_init(&subsys->su_mutex); + + err = configfs_register_subsystem(subsys); + if (err) + sharefs_err("failed to register subsystem"); + +out: + return err; +} + +void sharefs_exit_configfs(void) +{ + sharefs_info("sharefs exit configfs"); + + configfs_unregister_subsystem(&sharefs_subsystem); + clear_bid_hash_entry(); + + kmem_cache_destroy(sharefs_bid_entry_cachep); +} \ No newline at end of file diff --git a/fs/sharefs/dentry.c b/fs/sharefs/dentry.c new file mode 100644 index 0000000000000000000000000000000000000000..a880b27514ba743c4d4cef006b66ae8d7191a0b2 --- /dev/null +++ b/fs/sharefs/dentry.c @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * fs/sharefs/dentry.c + * + * Copyright (c) 1998-2022 Erez Zadok + * Copyright (c) 2009 Shrikar Archak + * Copyright (c) 2003-2022 Stony Brook University + * Copyright (c) 2003-2022 The Research Foundation of SUNY + */ + +#include "sharefs.h" + +/* + * returns: 0: tell VFS to invalidate dentry in share directory + */ +static int sharefs_d_revalidate(struct dentry *dentry, unsigned int flags) +{ + return 0; +} + +static void sharefs_d_release(struct dentry *dentry) +{ + /* + * It is possible that the dentry private data is NULL in case we + * ran out of memory while initializing it in + * new_dentry_private_data. So check for NULL before attempting to + * release resources. + */ + if (SHAREFS_D(dentry)) { + /* release and reset the lower paths */ + sharefs_put_reset_lower_path(dentry); + free_dentry_private_data(dentry); + } + return; +} + +const struct dentry_operations sharefs_dops = { + .d_revalidate = sharefs_d_revalidate, + .d_release = sharefs_d_release, +}; diff --git a/fs/sharefs/file.c b/fs/sharefs/file.c new file mode 100644 index 0000000000000000000000000000000000000000..d3ba82c04f9f700fc018d595d20fa1daea4706c9 --- /dev/null +++ b/fs/sharefs/file.c @@ -0,0 +1,240 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * fs/sharefs/file.c + * + * Copyright (c) 1998-2022 Erez Zadok + * Copyright (c) 2009 Shrikar Archak + * Copyright (c) 2003-2022 Stony Brook University + * Copyright (c) 2003-2022 The Research Foundation of SUNY + */ + +#include "sharefs.h" + +static int sharefs_readdir(struct file *file, struct dir_context *ctx) +{ + int err; + struct file *lower_file = NULL; + struct dentry *dentry = file->f_path.dentry; + + lower_file = sharefs_lower_file(file); + err = iterate_dir(lower_file, ctx); + file->f_pos = lower_file->f_pos; + if (err >= 0) + fsstack_copy_attr_atime(d_inode(dentry), + file_inode(lower_file)); + return err; +} + +static int sharefs_open(struct inode *inode, struct file *file) +{ + int err = 0; + struct file *lower_file = NULL; + struct path lower_path; + + /* don't open unhashed/deleted files */ + if (d_unhashed(file->f_path.dentry)) { + err = -ENOENT; + goto out_err; + } + + file->private_data = + kzalloc(sizeof(struct sharefs_file_info), GFP_KERNEL); + if (!SHAREFS_F(file)) { + err = -ENOMEM; + goto out_err; + } + + /* open lower object and link sharefs's file struct to lower's */ + sharefs_get_lower_path(file->f_path.dentry, &lower_path); + lower_file = dentry_open(&lower_path, file->f_flags, current_cred()); + path_put(&lower_path); + if (IS_ERR(lower_file)) { + err = PTR_ERR(lower_file); + lower_file = sharefs_lower_file(file); + if (lower_file) { + sharefs_set_lower_file(file, NULL); + fput(lower_file); /* fput calls dput for lower_dentry */ + } + } else { + sharefs_set_lower_file(file, lower_file); + } + + if (err) + kfree(SHAREFS_F(file)); + else { + kuid_t uid = inode->i_uid; + kgid_t gid = inode->i_gid; + mode_t mode = inode->i_mode; + fsstack_copy_attr_all(inode, sharefs_lower_inode(inode)); + inode->i_uid = uid; + inode->i_gid = gid; + inode->i_mode = mode; + } +out_err: + return err; +} + +static int sharefs_flush(struct file *file, fl_owner_t id) +{ + int err = 0; + struct file *lower_file = NULL; + + lower_file = sharefs_lower_file(file); + if (lower_file && lower_file->f_op && lower_file->f_op->flush) { + filemap_write_and_wait(file->f_mapping); + err = lower_file->f_op->flush(lower_file, id); + } + + return err; +} + +/* release all lower object references & free the file info structure */ +static int sharefs_file_release(struct inode *inode, struct file *file) +{ + struct file *lower_file; + + lower_file = sharefs_lower_file(file); + if (lower_file) { + sharefs_set_lower_file(file, NULL); + fput(lower_file); + } + + kfree(SHAREFS_F(file)); + return 0; +} + +static int sharefs_fsync(struct file *file, loff_t start, loff_t end, + int datasync) +{ + int err; + struct file *lower_file; + struct path lower_path; + struct dentry *dentry = file->f_path.dentry; + + err = __generic_file_fsync(file, start, end, datasync); + if (err) + goto out; + lower_file = sharefs_lower_file(file); + sharefs_get_lower_path(dentry, &lower_path); + err = vfs_fsync_range(lower_file, start, end, datasync); + sharefs_put_lower_path(dentry, &lower_path); +out: + return err; +} + +static int sharefs_fasync(int fd, struct file *file, int flag) +{ + int err = 0; + struct file *lower_file = NULL; + + lower_file = sharefs_lower_file(file); + if (lower_file->f_op && lower_file->f_op->fasync) + err = lower_file->f_op->fasync(fd, lower_file, flag); + + return err; +} + +/* + * Sharefs cannot use generic_file_llseek as ->llseek, because it would + * only set the offset of the upper file. So we have to implement our + * own method to set both the upper and lower file offsets + * consistently. + */ +static loff_t sharefs_file_llseek(struct file *file, loff_t offset, int whence) +{ + int err; + struct file *lower_file; + + err = generic_file_llseek(file, offset, whence); + if (err < 0) + goto out; + + lower_file = sharefs_lower_file(file); + err = generic_file_llseek(lower_file, offset, whence); + +out: + return err; +} + +/* + * Sharefs read_iter, redirect modified iocb to lower read_iter + */ +ssize_t +sharefs_read_iter(struct kiocb *iocb, struct iov_iter *iter) +{ + int err; + struct file *file = iocb->ki_filp, *lower_file; + + lower_file = sharefs_lower_file(file); + if (!lower_file->f_op->read_iter) { + err = -EINVAL; + goto out; + } + + /* prevent lower_file from being released */ + get_file(lower_file); + iocb->ki_filp = lower_file; + err = lower_file->f_op->read_iter(iocb, iter); + iocb->ki_filp = file; + fput(lower_file); + + /* update upper inode atime as needed */ + if (err >= 0 || err == -EIOCBQUEUED) + fsstack_copy_attr_atime(d_inode(file->f_path.dentry), + file_inode(lower_file)); +out: + return err; +} + +/* + * Sharefs write_iter, redirect modified iocb to lower write_iter + */ +ssize_t +sharefs_write_iter(struct kiocb *iocb, struct iov_iter *iter) +{ + int err; + struct file *file = iocb->ki_filp, *lower_file; + + lower_file = sharefs_lower_file(file); + if (!lower_file->f_op->write_iter) { + err = -EINVAL; + goto out; + } + + get_file(lower_file); /* prevent lower_file from being released */ + iocb->ki_filp = lower_file; + err = lower_file->f_op->write_iter(iocb, iter); + iocb->ki_filp = file; + fput(lower_file); + /* update upper inode times/sizes as needed */ + if (err >= 0 || err == -EIOCBQUEUED) { + fsstack_copy_inode_size(d_inode(file->f_path.dentry), + file_inode(lower_file)); + fsstack_copy_attr_times(d_inode(file->f_path.dentry), + file_inode(lower_file)); + } +out: + return err; +} + +const struct file_operations sharefs_main_fops = { + .llseek = sharefs_file_llseek, + .open = sharefs_open, + .flush = sharefs_flush, + .release = sharefs_file_release, + .fsync = sharefs_fsync, + .fasync = sharefs_fasync, + .read_iter = sharefs_read_iter, + .write_iter = sharefs_write_iter, +}; + +/* trimmed directory options */ +const struct file_operations sharefs_dir_fops = { + .llseek = sharefs_file_llseek, + .iterate = sharefs_readdir, + .open = sharefs_open, + .release = sharefs_file_release, + .flush = sharefs_flush, + .fsync = sharefs_fsync, + .fasync = sharefs_fasync, +}; diff --git a/fs/sharefs/inode.c b/fs/sharefs/inode.c new file mode 100644 index 0000000000000000000000000000000000000000..43e31b45caab24ea20632267e500480c1a6526e4 --- /dev/null +++ b/fs/sharefs/inode.c @@ -0,0 +1,133 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * fs/sharefs/inode.c + * + * Copyright (c) 1998-2022 Erez Zadok + * Copyright (c) 2009 Shrikar Archak + * Copyright (c) 2003-2022 Stony Brook University + * Copyright (c) 2003-2022 The Research Foundation of SUNY + */ + +#include "sharefs.h" + +static const char *sharefs_get_link(struct dentry *dentry, struct inode *inode, + struct delayed_call *done) +{ + DEFINE_DELAYED_CALL(lower_done); + struct dentry *lower_dentry; + struct path lower_path; + char *buf; + const char *lower_link; + + if (!dentry) + return ERR_PTR(-ECHILD); + + sharefs_get_lower_path(dentry, &lower_path); + lower_dentry = lower_path.dentry; + + /* + * get link from lower file system, but use a separate + * delayed_call callback. + */ + lower_link = vfs_get_link(lower_dentry, &lower_done); + if (IS_ERR(lower_link)) { + buf = ERR_CAST(lower_link); + goto out; + } + + /* + * we can't pass lower link up: have to make private copy and + * pass that. + */ + buf = kstrdup(lower_link, GFP_KERNEL); + do_delayed_call(&lower_done); + if (!buf) { + buf = ERR_PTR(-ENOMEM); + goto out; + } + + fsstack_copy_attr_atime(d_inode(dentry), d_inode(lower_dentry)); + + set_delayed_call(done, kfree_link, buf); +out: + sharefs_put_lower_path(dentry, &lower_path); + return buf; +} + +static int sharefs_getattr(const struct path *path, struct kstat *stat, + u32 request_mask, unsigned int flags) +{ + struct path lower_path; + int ret; + + sharefs_get_lower_path(path->dentry, &lower_path); + ret = vfs_getattr(&lower_path, stat, request_mask, flags); + stat->ino = d_inode(path->dentry)->i_ino; + stat->uid = d_inode(path->dentry)->i_uid; + stat->gid = d_inode(path->dentry)->i_gid; + stat->mode = d_inode(path->dentry)->i_mode; + sharefs_put_lower_path(path->dentry, &lower_path); + + return ret; +} + +static ssize_t +sharefs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) +{ + int err; + struct dentry *lower_dentry; + struct path lower_path; + + sharefs_get_lower_path(dentry, &lower_path); + lower_dentry = lower_path.dentry; + if (!(d_inode(lower_dentry)->i_opflags & IOP_XATTR)) { + err = -EOPNOTSUPP; + goto out; + } + err = vfs_listxattr(lower_dentry, buffer, buffer_size); + if (err) + goto out; + fsstack_copy_attr_atime(d_inode(dentry), + d_inode(lower_path.dentry)); +out: + sharefs_put_lower_path(dentry, &lower_path); + return err; +} + +static int sharefs_permission(struct inode *inode, int mask) +{ + unsigned short mode = inode->i_mode; + kuid_t cur_uid = current_fsuid(); + if (uid_eq(cur_uid, ROOT_UID)) + return 0; + if (uid_eq(cur_uid, inode->i_uid)) { + mode >>= 6; + } else if (in_group_p(inode->i_gid)) { + mode >>= 3; + } + + if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) + return 0; + + return -EACCES; +} + +const struct inode_operations sharefs_symlink_iops = { + .permission = sharefs_permission, + .getattr = sharefs_getattr, + .get_link = sharefs_get_link, + .listxattr = sharefs_listxattr, +}; + +const struct inode_operations sharefs_dir_iops = { + .lookup = sharefs_lookup, + .permission = sharefs_permission, + .getattr = sharefs_getattr, + .listxattr = sharefs_listxattr, +}; + +const struct inode_operations sharefs_main_iops = { + .permission = sharefs_permission, + .getattr = sharefs_getattr, + .listxattr = sharefs_listxattr, +}; diff --git a/fs/sharefs/lookup.c b/fs/sharefs/lookup.c new file mode 100644 index 0000000000000000000000000000000000000000..bd9881c88f90bad9476c434d2e04def2521360f1 --- /dev/null +++ b/fs/sharefs/lookup.c @@ -0,0 +1,328 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * fs/sharefs/lookup.c + * + * Copyright (c) 1998-2022 Erez Zadok + * Copyright (c) 2009 Shrikar Archak + * Copyright (c) 2003-2022 Stony Brook University + * Copyright (c) 2003-2022 The Research Foundation of SUNY + */ + +#include "sharefs.h" +#include "authentication.h" + +/* The dentry cache is just so we have properly sized dentries */ +static struct kmem_cache *sharefs_dentry_cachep; + +int sharefs_init_dentry_cache(void) +{ + sharefs_dentry_cachep = + kmem_cache_create("sharefs_dentry", + sizeof(struct sharefs_dentry_info), + 0, SLAB_RECLAIM_ACCOUNT, NULL); + + return sharefs_dentry_cachep ? 0 : -ENOMEM; +} + +void sharefs_destroy_dentry_cache(void) +{ + if (sharefs_dentry_cachep) + kmem_cache_destroy(sharefs_dentry_cachep); +} + +void free_dentry_private_data(struct dentry *dentry) +{ + if (!dentry || !dentry->d_fsdata) + return; + kmem_cache_free(sharefs_dentry_cachep, dentry->d_fsdata); + dentry->d_fsdata = NULL; +} + +/* allocate new dentry private data */ +int new_dentry_private_data(struct dentry *dentry) +{ + struct sharefs_dentry_info *info = SHAREFS_D(dentry); + + /* use zalloc to init dentry_info.lower_path */ + info = kmem_cache_zalloc(sharefs_dentry_cachep, GFP_ATOMIC); + if (!info) + return -ENOMEM; + + spin_lock_init(&info->lock); + dentry->d_fsdata = info; + + return 0; +} + +static int sharefs_inode_test(struct inode *inode, void *candidate_lower_inode) +{ + struct inode *current_lower_inode = sharefs_lower_inode(inode); + if (current_lower_inode == (struct inode *)candidate_lower_inode) + return 1; /* found a match */ + else + return 0; /* no match */ +} + +static int sharefs_inode_set(struct inode *inode, void *lower_inode) +{ + /* we do actual inode initialization in sharefs_iget */ + return 0; +} + +struct inode *sharefs_iget(struct super_block *sb, struct inode *lower_inode) +{ + struct inode *inode; /* the new inode to return */ + + if (!igrab(lower_inode)) + return ERR_PTR(-ESTALE); + inode = iget5_locked(sb, /* our superblock */ + /* + * hashval: we use inode number, but we can + * also use "(unsigned long)lower_inode" + * instead. + */ + lower_inode->i_ino, /* hashval */ + sharefs_inode_test, /* inode comparison function */ + sharefs_inode_set, /* inode init function */ + lower_inode); /* data passed to test+set fxns */ + if (!inode) { + iput(lower_inode); + return ERR_PTR(-ENOMEM); + } + /* if found a cached inode, then just return it (after iput) */ + if (!(inode->i_state & I_NEW)) { + iput(lower_inode); + return inode; + } + + /* initialize new inode */ + inode->i_ino = lower_inode->i_ino; + sharefs_set_lower_inode(inode, lower_inode); + + atomic64_inc(&inode->i_version); + + /* use different set of inode ops for symlinks & directories */ + if (S_ISDIR(lower_inode->i_mode)) + inode->i_op = &sharefs_dir_iops; + else if (S_ISLNK(lower_inode->i_mode)) + inode->i_op = &sharefs_symlink_iops; + else + inode->i_op = &sharefs_main_iops; + + /* use different set of file ops for directories */ + if (S_ISDIR(lower_inode->i_mode)) + inode->i_fop = &sharefs_dir_fops; + else + inode->i_fop = &sharefs_main_fops; + + inode->i_atime.tv_sec = 0; + inode->i_atime.tv_nsec = 0; + inode->i_mtime.tv_sec = 0; + inode->i_mtime.tv_nsec = 0; + inode->i_ctime.tv_sec = 0; + inode->i_ctime.tv_nsec = 0; + + /* properly initialize special inodes */ + if (S_ISBLK(lower_inode->i_mode) || S_ISCHR(lower_inode->i_mode) || + S_ISFIFO(lower_inode->i_mode) || S_ISSOCK(lower_inode->i_mode)) + init_special_inode(inode, lower_inode->i_mode, + lower_inode->i_rdev); + + /* all well, copy inode attributes */ + fsstack_copy_attr_all(inode, lower_inode); + fsstack_copy_inode_size(inode, lower_inode); + + unlock_new_inode(inode); + return inode; +} + +/* + * Helper interpose routine, called directly by ->lookup to handle + * spliced dentries. + */ +static struct dentry *__sharefs_interpose(struct dentry *dentry, + struct super_block *sb, + struct path *lower_path) +{ + struct inode *inode; + struct inode *lower_inode; + struct super_block *lower_sb; + struct dentry *ret_dentry; + + lower_inode = d_inode(lower_path->dentry); + lower_sb = sharefs_lower_super(sb); + + /* check that the lower file system didn't cross a mount point */ + if (lower_inode->i_sb != lower_sb) { + ret_dentry = ERR_PTR(-EXDEV); + goto out; + } + + /* + * We allocate our new inode below by calling sharefs_iget, + * which will initialize some of the new inode's fields + */ + + /* inherit lower inode number for sharefs's inode */ + inode = sharefs_iget(sb, lower_inode); + if (IS_ERR(inode)) { + ret_dentry = ERR_PTR(PTR_ERR(inode)); + goto out; + } + + ret_dentry = d_splice_alias(inode, dentry); + +out: + return ret_dentry; +} + +/* + * Connect a sharefs inode dentry/inode with several lower ones. This is + * the classic stackable file system "vnode interposition" action. + * + * @dentry: sharefs's dentry which interposes on lower one + * @sb: sharefs's super_block + * @lower_path: the lower path (caller does path_get/put) + */ +int sharefs_interpose(struct dentry *dentry, struct super_block *sb, + struct path *lower_path) +{ + struct dentry *ret_dentry; + + ret_dentry = __sharefs_interpose(dentry, sb, lower_path); + return PTR_ERR(ret_dentry); +} + +/* + * Main driver function for sharefs's lookup. + * + * Returns: NULL (ok), ERR_PTR if an error occurred. + * Fills in lower_parent_path with on success. + */ +static struct dentry *__sharefs_lookup(struct dentry *dentry, + unsigned int flags, + struct path *lower_parent_path) +{ + int err = 0; + struct vfsmount *lower_dir_mnt; + struct dentry *lower_dir_dentry = NULL; + struct dentry *lower_dentry; + const char *name; + struct path lower_path; + struct qstr this; + struct dentry *ret_dentry = NULL; + + /* must initialize dentry operations */ + d_set_d_op(dentry, &sharefs_dops); + + if (IS_ROOT(dentry)) + goto out; + + name = dentry->d_name.name; + + /* now start the actual lookup procedure */ + lower_dir_dentry = lower_parent_path->dentry; + lower_dir_mnt = lower_parent_path->mnt; + + /* Use vfs_path_lookup to check if the dentry exists or not */ + err = vfs_path_lookup(lower_dir_dentry, lower_dir_mnt, name, 0, + &lower_path); + + /* no error: handle positive dentries */ + if (!err) { + sharefs_set_lower_path(dentry, &lower_path); + ret_dentry = + __sharefs_interpose(dentry, dentry->d_sb, &lower_path); + if (IS_ERR(ret_dentry)) { + err = PTR_ERR(ret_dentry); + /* path_put underlying path on error */ + sharefs_put_reset_lower_path(dentry); + } + goto out; + } + + /* + * We don't consider ENOENT an error, and we want to return a + * negative dentry. + */ + if (err && err != -ENOENT) + goto out; + + /* instantiate a new negative dentry */ + this.name = name; + this.len = strlen(name); + this.hash = full_name_hash(lower_dir_dentry, this.name, this.len); + lower_dentry = d_lookup(lower_dir_dentry, &this); + if (lower_dentry) + goto setup_lower; + + lower_dentry = d_alloc(lower_dir_dentry, &this); + if (!lower_dentry) { + err = -ENOMEM; + goto out; + } + + /* + * Calling ->lookup instead of d_add will give the lower fs a chance + * to allocate the d_fsdata field but will still instantiate and hash the + * lower_dentry. Without this, sharefs could not stack on top of itself. + */ + d_inode(lower_dir_dentry)->i_op->lookup(d_inode(lower_dir_dentry), + lower_dentry, flags); + +setup_lower: + lower_path.dentry = lower_dentry; + lower_path.mnt = mntget(lower_dir_mnt); + sharefs_set_lower_path(dentry, &lower_path); + + /* + * If the intent is to create a file, then don't return an error, so + * the VFS will continue the process of making this negative dentry + * into a positive one. + */ + if (err == -ENOENT || (flags & (LOOKUP_CREATE|LOOKUP_RENAME_TARGET))) + err = 0; + +out: + if (err) + return ERR_PTR(err); + return ret_dentry; +} + +struct dentry *sharefs_lookup(struct inode *dir, struct dentry *dentry, + unsigned int flags) +{ + int err; + struct dentry *ret, *parent; + struct path lower_parent_path; + + parent = dget_parent(dentry); + + sharefs_get_lower_path(parent, &lower_parent_path); + + /* allocate dentry private data. We free it in ->d_release */ + err = new_dentry_private_data(dentry); + if (err) { + ret = ERR_PTR(err); + goto out; + } + ret = __sharefs_lookup(dentry, flags, &lower_parent_path); + if (IS_ERR(ret)) { + sharefs_err("sharefs_lookup error!"); + goto out; + } + + if (ret) + dentry = ret; + if (d_inode(dentry)) + fsstack_copy_attr_times(d_inode(dentry), + sharefs_lower_inode(d_inode(dentry))); + /* update parent directory's atime */ + fsstack_copy_attr_atime(d_inode(parent), + sharefs_lower_inode(d_inode(parent))); + fixup_perm_from_level(d_inode(parent), dentry); +out: + sharefs_put_lower_path(parent, &lower_parent_path); + dput(parent); + return ret; +} diff --git a/fs/sharefs/main.c b/fs/sharefs/main.c new file mode 100644 index 0000000000000000000000000000000000000000..b1f656240023ea93974c15b4a1635a32f5a84065 --- /dev/null +++ b/fs/sharefs/main.c @@ -0,0 +1,192 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * fs/sharefs/main.c + * + * Copyright (c) 1998-2022 Erez Zadok + * Copyright (c) 2009 Shrikar Archak + * Copyright (c) 2003-2022 Stony Brook University + * Copyright (c) 2003-2022 The Research Foundation of SUNY + */ + +#include +#include "sharefs.h" +#include "authentication.h" + + +struct sharefs_mount_priv { + const char *dev_name; + const char *raw_data; +}; + +/* + * There is no need to lock the sharefs_super_info's rwsem as there is no + * way anyone can have a reference to the superblock at this point in time. + */ +static int sharefs_fill_super(struct super_block *sb, void *data, int silent) +{ + + struct sharefs_mount_priv *priv = (struct sharefs_mount_priv *)data; + const char *dev_name = priv->dev_name; + const char *raw_data = priv->raw_data; + + int err = 0; + struct super_block *lower_sb; + struct path lower_path; + struct inode *inode; + + /* parse lower path */ + err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, + &lower_path); + if (err) { + printk(KERN_ERR "sharefs: error accessing " + "lower directory '%s'\n", dev_name); + goto out; + } + + /* allocate superblock private data */ + sb->s_fs_info = kzalloc(sizeof(struct sharefs_sb_info), GFP_KERNEL); + if (!SHAREFS_SB(sb)) { + printk(KERN_CRIT "sharefs: fill_super: out of memory\n"); + err = -ENOMEM; + goto out_pput; + } + + /* set the lower superblock field of upper superblock */ + lower_sb = lower_path.dentry->d_sb; + atomic_inc(&lower_sb->s_active); + sharefs_set_lower_super(sb, lower_sb); + + /* inherit maxbytes from lower file system */ + sb->s_maxbytes = lower_sb->s_maxbytes; + + /* + * Our c/m/atime granularity is 1 ns because we may stack on file + * systems whose granularity is as good. + */ + sb->s_time_gran = 1; + + sb->s_op = &sharefs_sops; + + /* get a new inode and allocate our root dentry */ + inode = sharefs_iget(sb, d_inode(lower_path.dentry)); + if (IS_ERR(inode)) { + err = PTR_ERR(inode); + goto out_pput; + } + sharefs_root_inode_perm_init(inode); + sb->s_root = d_make_root(inode); + if (!sb->s_root) { + err = -ENOMEM; + goto out_pput; + } + d_set_d_op(sb->s_root, &sharefs_dops); + + err = sharefs_parse_options(sb->s_fs_info, raw_data); + if (err) + goto out_pput; + + /* link the upper and lower dentries */ + sb->s_root->d_fsdata = NULL; + err = new_dentry_private_data(sb->s_root); + if (err) + goto out_pput; + + /* if get here: cannot have error */ + + /* set the lower dentries for s_root */ + sharefs_set_lower_path(sb->s_root, &lower_path); + + /* + * No need to call interpose because we already have a positive + * dentry, which was instantiated by d_make_root. Just need to + * d_rehash it. + */ + d_rehash(sb->s_root); + if (!silent) + printk(KERN_INFO + "sharefs: mounted on top of %s type %s\n", + dev_name, lower_sb->s_type->name); + goto out; /* all is well */ + + /* + * path_put is the only resource we need to free if an error occurred + * because returning an error from this function will cause + * generic_shutdown_super to be called, which will call + * sharefs_put_super, and that function will release any other + * resources we took. + */ +out_pput: + path_put(&lower_path); +out: + return err; +} + +struct dentry *sharefs_mount(struct file_system_type *fs_type, int flags, + const char *dev_name, void *raw_data) +{ + struct sharefs_mount_priv priv = { + .dev_name = dev_name, + .raw_data = raw_data, + }; + + /* sharefs needs a valid dev_name to get the lower_sb's metadata */ + if (!dev_name || !*dev_name) + return ERR_PTR(-EINVAL); + + return mount_nodev(fs_type, flags, &priv, + sharefs_fill_super); +} + +static struct file_system_type sharefs_fs_type = { + .owner = THIS_MODULE, + .name = SHAREFS_NAME, + .mount = sharefs_mount, + .kill_sb = generic_shutdown_super, + .fs_flags = 0, +}; +MODULE_ALIAS_FS(SHAREFS_NAME); + +static int __init init_sharefs_fs(void) +{ + int err; + + pr_info("Registering sharefs"); + + err = sharefs_init_inode_cache(); + if (err) + goto out_err; + err = sharefs_init_dentry_cache(); + if (err) + goto out_err; + err = register_filesystem(&sharefs_fs_type); + if (err) { + sharefs_err("share register failed!"); + goto out_err; + } + + err = sharefs_init_configfs(); + if (err) + goto out_err; + return 0; +out_err: + sharefs_exit_configfs(); + sharefs_destroy_inode_cache(); + sharefs_destroy_dentry_cache(); + sharefs_err("sharefs init failed!"); + return err; +} + +static void __exit exit_sharefs_fs(void) +{ + sharefs_destroy_inode_cache(); + sharefs_destroy_dentry_cache(); + unregister_filesystem(&sharefs_fs_type); + pr_info("Completed sharefs module unload\n"); +} + +MODULE_AUTHOR("Jingjing Mao"); +MODULE_DESCRIPTION("Sharefs"); +MODULE_LICENSE("GPL"); + +module_init(init_sharefs_fs); +module_exit(exit_sharefs_fs); diff --git a/fs/sharefs/sharefs.h b/fs/sharefs/sharefs.h new file mode 100644 index 0000000000000000000000000000000000000000..5429018f2c120a25ed7dd03f2a14d06269c51ef5 --- /dev/null +++ b/fs/sharefs/sharefs.h @@ -0,0 +1,241 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 1998-2022 Erez Zadok + * Copyright (c) 2009 Shrikar Archak + * Copyright (c) 2003-2022 Stony Brook University + * Copyright (c) 2003-2022 The Research Foundation of SUNY + */ + +#ifndef _SHAREFS_H_ +#define _SHAREFS_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* the file system name */ +#define SHAREFS_NAME "sharefs" + +/* sharefs root inode number */ +#define SHAREFS_ROOT_INO 1 +#define OID_ROOT 0 +#define ROOT_UID KUIDT_INIT(OID_ROOT) +#define ROOT_GID KGIDT_INIT(OID_ROOT) +#define SHAREFS_SUPER_MAGIC 0x20230212 + +/* useful for tracking code reachability */ +#define UDBG printk(KERN_DEFAULT "DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__) + +/* file private data */ +struct sharefs_file_info { + struct file *lower_file; + const struct vm_operations_struct *lower_vm_ops; +}; + +/* sharefs inode data in memory */ +struct sharefs_inode_info { + struct inode *lower_inode; + struct inode vfs_inode; + __u16 perm; +}; + +/* sharefs dentry data in memory */ +struct sharefs_dentry_info { + spinlock_t lock; /* protects lower_path */ + struct path lower_path; +}; + +/* sharefs super-block data in memory */ +struct sharefs_sb_info { + struct super_block *lower_sb; + /* multi user */ + unsigned int user_id; +}; + +/* operations vectors defined in specific files */ +extern const struct file_operations sharefs_main_fops; +extern const struct file_operations sharefs_dir_fops; +extern const struct inode_operations sharefs_main_iops; +extern const struct inode_operations sharefs_dir_iops; +extern const struct inode_operations sharefs_symlink_iops; +extern const struct super_operations sharefs_sops; +extern const struct dentry_operations sharefs_dops; + +extern int sharefs_init_inode_cache(void); +extern void sharefs_destroy_inode_cache(void); +extern int sharefs_init_dentry_cache(void); +extern void sharefs_destroy_dentry_cache(void); +extern int new_dentry_private_data(struct dentry *dentry); +extern void free_dentry_private_data(struct dentry *dentry); +extern struct dentry *sharefs_lookup(struct inode *dir, struct dentry *dentry, + unsigned int flags); +extern struct inode *sharefs_iget(struct super_block *sb, + struct inode *lower_inode); +extern int sharefs_interpose(struct dentry *dentry, struct super_block *sb, + struct path *lower_path); +extern int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, + const char *name, unsigned int flags, + struct path *path); +extern int sharefs_parse_options(struct sharefs_sb_info *sbi, + const char *data); + +/* + * inode to private data + * + * Since we use containers and the struct inode is _inside_ the + * sharefs_inode_info structure, SHAREFS_I will always (given a non-NULL + * inode pointer), return a valid non-NULL pointer. + */ +static inline struct sharefs_inode_info *SHAREFS_I(const struct inode *inode) +{ + return container_of(inode, struct sharefs_inode_info, vfs_inode); +} + +/* dentry to private data */ +#define SHAREFS_D(dent) ((struct sharefs_dentry_info *)(dent)->d_fsdata) + +/* superblock to private data */ +#define SHAREFS_SB(super) ((struct sharefs_sb_info *)(super)->s_fs_info) + +/* file to private Data */ +#define SHAREFS_F(file) ((struct sharefs_file_info *)((file)->private_data)) + +/* file to lower file */ +static inline struct file *sharefs_lower_file(const struct file *f) +{ + return SHAREFS_F(f)->lower_file; +} + +static inline void sharefs_set_lower_file(struct file *f, struct file *val) +{ + SHAREFS_F(f)->lower_file = val; +} + +/* inode to lower inode. */ +static inline struct inode *sharefs_lower_inode(const struct inode *i) +{ + return SHAREFS_I(i)->lower_inode; +} + +static inline void sharefs_set_lower_inode(struct inode *i, struct inode *val) +{ + SHAREFS_I(i)->lower_inode = val; +} + +/* superblock to lower superblock */ +static inline struct super_block *sharefs_lower_super( + const struct super_block *sb) +{ + return SHAREFS_SB(sb)->lower_sb; +} + +static inline void sharefs_set_lower_super(struct super_block *sb, + struct super_block *val) +{ + SHAREFS_SB(sb)->lower_sb = val; +} + +/* path based (dentry/mnt) macros */ +static inline void pathcpy(struct path *dst, const struct path *src) +{ + dst->dentry = src->dentry; + dst->mnt = src->mnt; +} +/* Returns struct path. Caller must path_put it. */ +static inline void sharefs_get_lower_path(const struct dentry *dent, + struct path *lower_path) +{ + spin_lock(&SHAREFS_D(dent)->lock); + pathcpy(lower_path, &SHAREFS_D(dent)->lower_path); + path_get(lower_path); + spin_unlock(&SHAREFS_D(dent)->lock); + return; +} +static inline void sharefs_put_lower_path(const struct dentry *dent, + struct path *lower_path) +{ + path_put(lower_path); + return; +} +static inline void sharefs_set_lower_path(const struct dentry *dent, + struct path *lower_path) +{ + spin_lock(&SHAREFS_D(dent)->lock); + pathcpy(&SHAREFS_D(dent)->lower_path, lower_path); + spin_unlock(&SHAREFS_D(dent)->lock); + return; +} +static inline void sharefs_reset_lower_path(const struct dentry *dent) +{ + spin_lock(&SHAREFS_D(dent)->lock); + SHAREFS_D(dent)->lower_path.dentry = NULL; + SHAREFS_D(dent)->lower_path.mnt = NULL; + spin_unlock(&SHAREFS_D(dent)->lock); + return; +} +static inline void sharefs_put_reset_lower_path(const struct dentry *dent) +{ + struct path lower_path; + spin_lock(&SHAREFS_D(dent)->lock); + pathcpy(&lower_path, &SHAREFS_D(dent)->lower_path); + SHAREFS_D(dent)->lower_path.dentry = NULL; + SHAREFS_D(dent)->lower_path.mnt = NULL; + spin_unlock(&SHAREFS_D(dent)->lock); + path_put(&lower_path); + return; +} + +/* locking helpers */ +static inline struct dentry *lock_parent(struct dentry *dentry) +{ + struct dentry *dir = dget_parent(dentry); + inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); + return dir; +} + +static inline void unlock_dir(struct dentry *dir) +{ + inode_unlock(d_inode(dir)); + dput(dir); +} + +static inline bool str_n_case_eq(const char *s1, const char *s2, size_t len) +{ + return !strncasecmp(s1, s2, len); +} + +static inline bool qstr_case_eq(const struct qstr *q1, const struct qstr *q2) +{ + return q1->len == q2->len && str_n_case_eq(q1->name, q2->name, q2->len); +} +/***************************************************************************** + * log print helpers + *****************************************************************************/ +__printf(4, 5) void __sharefs_log(const char *level, const bool ratelimited, + const char *function, const char *fmt, ...); +#define sharefs_err(fmt, ...) \ + __sharefs_log(KERN_ERR, false, __func__, fmt, ##__VA_ARGS__) +#define sharefs_warning(fmt, ...) \ + __sharefs_log(KERN_WARNING, false, __func__, fmt, ##__VA_ARGS__) +#define sharefs_info(fmt, ...) \ + __sharefs_log(KERN_INFO, false, __func__, fmt, ##__VA_ARGS__) +#define sharefs_err_ratelimited(fmt, ...) \ + __sharefs_log(KERN_ERR, true, __func__, fmt, ##__VA_ARGS__) +#define sharefs_warning_ratelimited(fmt, ...) \ + __sharefs_log(KERN_WARNING, true, __func__, fmt, ##__VA_ARGS__) +#define sharefs_info_ratelimited(fmt, ...) \ + __sharefs_log(KERN_INFO, true, __func__, fmt, ##__VA_ARGS__) + +#endif /* not _SHAREFS_H_ */ diff --git a/fs/sharefs/super.c b/fs/sharefs/super.c new file mode 100644 index 0000000000000000000000000000000000000000..e130126496a68dfca56f9af7dc8e3064fcc4dca0 --- /dev/null +++ b/fs/sharefs/super.c @@ -0,0 +1,202 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 1998-2022 Erez Zadok + * Copyright (c) 2009 Shrikar Archak + * Copyright (c) 2003-2022 Stony Brook University + * Copyright (c) 2003-2022 The Research Foundation of SUNY + */ +#include +#include +#include +#include +#include "sharefs.h" + +enum { + OPT_USER_ID, +}; + +static match_table_t sharefs_tokens = { + { OPT_USER_ID, "user_id=%s"}, +}; + +int sharefs_parse_options(struct sharefs_sb_info *sbi, const char *data) +{ + char *p = NULL; + char *name = NULL; + char *options = NULL; + char *options_src = NULL; + substring_t args[MAX_OPT_ARGS]; + unsigned int user_id = 0; + int err = 0; + + options = kstrdup(data, GFP_KERNEL); + if (data && !options) { + err = -ENOMEM; + goto out; + } + options_src = options; + + while ((p = strsep(&options_src, ",")) != NULL) { + int token; + + if (!*p) + continue; + args[0].to = args[0].from = NULL; + token = match_token(p, sharefs_tokens, args); + + switch (token) { + case OPT_USER_ID: + name = match_strdup(&args[0]); + if (name) { + err = kstrtouint(name, 10, &user_id); + kfree(name); + name = NULL; + if (err) + goto out; + sbi->user_id = user_id; + } + break; + default: + err = -EINVAL; + goto out; + } + } +out: + kfree(options); + + return err; +} + +/* + * The inode cache is used with alloc_inode for both our inode info and the + * vfs inode. + */ +static struct kmem_cache *sharefs_inode_cachep; + +/* final actions when unmounting a file system */ +static void sharefs_put_super(struct super_block *sb) +{ + struct sharefs_sb_info *spd; + struct super_block *s; + + spd = SHAREFS_SB(sb); + if (!spd) + return; + + /* decrement lower super references */ + s = sharefs_lower_super(sb); + sharefs_set_lower_super(sb, NULL); + atomic_dec(&s->s_active); + + kfree(spd); + sb->s_fs_info = NULL; +} + +static int sharefs_statfs(struct dentry *dentry, struct kstatfs *buf) +{ + int err; + struct path lower_path; + + sharefs_get_lower_path(dentry, &lower_path); + err = vfs_statfs(&lower_path, buf); + sharefs_put_lower_path(dentry, &lower_path); + + /* set return buf to our f/s to avoid confusing user-level utils */ + buf->f_type = SHAREFS_SUPER_MAGIC; + + return err; +} + +/* + * Called by iput() when the inode reference count reached zero + * and the inode is not hashed anywhere. Used to clear anything + * that needs to be, before the inode is completely destroyed and put + * on the inode free list. + */ +static void sharefs_evict_inode(struct inode *inode) +{ + struct inode *lower_inode; + + truncate_inode_pages(&inode->i_data, 0); + clear_inode(inode); + /* + * Decrement a reference to a lower_inode, which was incremented + * by our read_inode when it was created initially. + */ + lower_inode = sharefs_lower_inode(inode); + sharefs_set_lower_inode(inode, NULL); + iput(lower_inode); +} + +void __sharefs_log(const char *level, const bool ratelimited, + const char *function, const char *fmt, ...) +{ + struct va_format vaf; + va_list args; + + va_start(args, fmt); + vaf.fmt = fmt; + vaf.va = &args; + if (ratelimited) + printk_ratelimited("%s sharefs: %s() %pV\n", level, + function, &vaf); + else + printk("%s sharefs: %s() %pV\n", level, function, &vaf); + va_end(args); +} + +static struct inode *sharefs_alloc_inode(struct super_block *sb) +{ + struct sharefs_inode_info *i; + + i = kmem_cache_alloc(sharefs_inode_cachep, GFP_KERNEL); + if (!i) + return NULL; + + /* memset everything up to the inode to 0 */ + memset(i, 0, offsetof(struct sharefs_inode_info, vfs_inode)); + + atomic64_set(&i->vfs_inode.i_version, 1); + return &i->vfs_inode; +} + +static void sharefs_destroy_inode(struct inode *inode) +{ + kmem_cache_free(sharefs_inode_cachep, SHAREFS_I(inode)); +} + +/* sharefs inode cache constructor */ +static void init_once(void *obj) +{ + struct sharefs_inode_info *i = obj; + + inode_init_once(&i->vfs_inode); +} + +int sharefs_init_inode_cache(void) +{ + int err = 0; + + sharefs_inode_cachep = + kmem_cache_create("sharefs_inode_cache", + sizeof(struct sharefs_inode_info), 0, + SLAB_RECLAIM_ACCOUNT, init_once); + if (!sharefs_inode_cachep) + err = -ENOMEM; + return err; +} + +/* sharefs inode cache destructor */ +void sharefs_destroy_inode_cache(void) +{ + if (sharefs_inode_cachep) + kmem_cache_destroy(sharefs_inode_cachep); +} + +const struct super_operations sharefs_sops = { + .put_super = sharefs_put_super, + .statfs = sharefs_statfs, + .evict_inode = sharefs_evict_inode, + .alloc_inode = sharefs_alloc_inode, + .destroy_inode = sharefs_destroy_inode, +};