📄 inode.c
字号:
{ atomic_inc(&fc->count); return fc;}static struct inode *get_root_inode(struct super_block *sb, unsigned mode){ struct fuse_attr attr; memset(&attr, 0, sizeof(attr)); attr.mode = mode; attr.ino = FUSE_ROOT_ID; return fuse_iget(sb, 1, 0, &attr);}#ifndef FUSE_MAINLINEstatic struct dentry *fuse_get_dentry(struct super_block *sb, void *vobjp){ __u32 *objp = vobjp; unsigned long nodeid = objp[0]; __u32 generation = objp[1]; struct inode *inode; struct dentry *entry; if (nodeid == 0) return ERR_PTR(-ESTALE); inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid); if (!inode) return ERR_PTR(-ESTALE); if (inode->i_generation != generation) { iput(inode); return ERR_PTR(-ESTALE); } entry = d_alloc_anon(inode); if (!entry) { iput(inode); return ERR_PTR(-ENOMEM); } return entry;}static int fuse_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len, int connectable){ struct inode *inode = dentry->d_inode; int len = *max_len; int type = 1; if (len < 2 || (connectable && len < 4)) return 255; len = 2; fh[0] = get_fuse_inode(inode)->nodeid; fh[1] = inode->i_generation; if (connectable && !S_ISDIR(inode->i_mode)) { struct inode *parent; spin_lock(&dentry->d_lock); parent = dentry->d_parent->d_inode; fh[2] = get_fuse_inode(parent)->nodeid; fh[3] = parent->i_generation; spin_unlock(&dentry->d_lock); len = 4; type = 2; } *max_len = len; return type;}static struct export_operations fuse_export_operations = { .get_dentry = fuse_get_dentry, .encode_fh = fuse_encode_fh,};#endifstatic struct super_operations fuse_super_operations = { .alloc_inode = fuse_alloc_inode, .destroy_inode = fuse_destroy_inode, .read_inode = fuse_read_inode, .clear_inode = fuse_clear_inode, .remount_fs = fuse_remount_fs, .put_super = fuse_put_super, .umount_begin = fuse_umount_begin, .statfs = fuse_statfs, .show_options = fuse_show_options,};static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req){ struct fuse_init_out *arg = &req->misc.init_out; if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION) fc->conn_error = 1; else { unsigned long ra_pages; if (arg->minor >= 6) { ra_pages = arg->max_readahead / PAGE_CACHE_SIZE; if (arg->flags & FUSE_ASYNC_READ) fc->async_read = 1; if (!(arg->flags & FUSE_POSIX_LOCKS)) fc->no_lock = 1; } else { ra_pages = fc->max_read / PAGE_CACHE_SIZE; fc->no_lock = 1; } fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages); fc->minor = arg->minor; fc->max_write = arg->minor < 5 ? 4096 : arg->max_write; fc->conn_init = 1; } fuse_put_request(fc, req); fc->blocked = 0; wake_up_all(&fc->blocked_waitq);}static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req){ struct fuse_init_in *arg = &req->misc.init_in; arg->major = FUSE_KERNEL_VERSION; arg->minor = FUSE_KERNEL_MINOR_VERSION; arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE; arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS; req->in.h.opcode = FUSE_INIT; req->in.numargs = 1; req->in.args[0].size = sizeof(*arg); req->in.args[0].value = arg; req->out.numargs = 1; /* Variable length arguement used for backward compatibility with interface version < 7.5. Rest of init_out is zeroed by do_get_request(), so a short reply is not a problem */ req->out.argvar = 1; req->out.args[0].size = sizeof(struct fuse_init_out); req->out.args[0].value = &req->misc.init_out; req->end = process_init_reply; request_send_background(fc, req);}static u64 conn_id(void){ static u64 ctr = 1; return ctr++;}static int fuse_fill_super(struct super_block *sb, void *data, int silent){ struct fuse_conn *fc; struct inode *root; struct fuse_mount_data d; struct file *file; struct dentry *root_dentry; struct fuse_req *init_req; int err; int is_bdev = sb->s_bdev != NULL; if (sb->s_flags & MS_MANDLOCK) return -EINVAL; if (!parse_fuse_opt((char *) data, &d, is_bdev)) return -EINVAL; if (is_bdev) {#ifdef CONFIG_BLOCK if (!sb_set_blocksize(sb, d.blksize)) return -EINVAL;#endif } else { sb->s_blocksize = PAGE_CACHE_SIZE; sb->s_blocksize_bits = PAGE_CACHE_SHIFT; } sb->s_magic = FUSE_SUPER_MAGIC; sb->s_op = &fuse_super_operations; sb->s_maxbytes = MAX_LFS_FILESIZE;#ifndef FUSE_MAINLINE sb->s_export_op = &fuse_export_operations;#endif file = fget(d.fd); if (!file) return -EINVAL; if (file->f_op != &fuse_dev_operations) return -EINVAL; fc = new_conn(); if (!fc) return -ENOMEM; fc->flags = d.flags; fc->user_id = d.user_id; fc->group_id = d.group_id; fc->max_read = d.max_read; /* Used by get_root_inode() */ sb->s_fs_info = fc; err = -ENOMEM; root = get_root_inode(sb, d.rootmode); if (!root) goto err; root_dentry = d_alloc_root(root); if (!root_dentry) { iput(root); goto err; } init_req = fuse_request_alloc(); if (!init_req) goto err_put_root; if (is_bdev) { fc->destroy_req = fuse_request_alloc(); if (!fc->destroy_req) goto err_put_root; } mutex_lock(&fuse_mutex); err = -EINVAL; if (file->private_data) goto err_unlock; fc->id = conn_id(); err = fuse_ctl_add_conn(fc); if (err) goto err_unlock; list_add_tail(&fc->entry, &fuse_conn_list); sb->s_root = root_dentry; fc->connected = 1; file->private_data = fuse_conn_get(fc); mutex_unlock(&fuse_mutex); /* * atomic_dec_and_test() in fput() provides the necessary * memory barrier for file->private_data to be visible on all * CPUs after this */ fput(file); fuse_send_init(fc, init_req); return 0; err_unlock: mutex_unlock(&fuse_mutex); fuse_request_free(init_req); err_put_root: dput(root_dentry); err: fput(file); fuse_conn_put(fc); return err;}#ifdef KERNEL_2_6_18_PLUSstatic int fuse_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt){ return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);}#elsestatic struct super_block *fuse_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *raw_data){ return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);}#endifstatic struct file_system_type fuse_fs_type = { .owner = THIS_MODULE, .name = "fuse", .get_sb = fuse_get_sb, .kill_sb = kill_anon_super,};#ifdef CONFIG_BLOCK#ifdef KERNEL_2_6_18_PLUSstatic int fuse_get_sb_blk(struct file_system_type *fs_type, int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt){ return get_sb_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super, mnt);}#elsestatic struct super_block *fuse_get_sb_blk(struct file_system_type *fs_type, int flags, const char *dev_name, void *raw_data){ return get_sb_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super);}#endifstatic struct file_system_type fuseblk_fs_type = { .owner = THIS_MODULE, .name = "fuseblk", .get_sb = fuse_get_sb_blk, .kill_sb = kill_block_super, .fs_flags = FS_REQUIRES_DEV,};static inline int register_fuseblk(void){ return register_filesystem(&fuseblk_fs_type);}static inline void unregister_fuseblk(void){ unregister_filesystem(&fuseblk_fs_type);}#elsestatic inline int register_fuseblk(void){ return 0;}static inline void unregister_fuseblk(void){}#endif#ifndef HAVE_FS_SUBSYSstatic decl_subsys(fs, NULL, NULL);#endifstatic decl_subsys(fuse, NULL, NULL);static decl_subsys(connections, NULL, NULL);static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep, unsigned long flags){ struct inode * inode = foo; if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR) inode_init_once(inode);}static int __init fuse_fs_init(void){ int err; err = register_filesystem(&fuse_fs_type); if (err) goto out; err = register_fuseblk(); if (err) goto out_unreg; fuse_inode_cachep = kmem_cache_create("fuse_inode", sizeof(struct fuse_inode), 0, SLAB_HWCACHE_ALIGN, fuse_inode_init_once, NULL); err = -ENOMEM; if (!fuse_inode_cachep) goto out_unreg2; return 0; out_unreg2: unregister_fuseblk(); out_unreg: unregister_filesystem(&fuse_fs_type); out: return err;}static void fuse_fs_cleanup(void){ unregister_filesystem(&fuse_fs_type); unregister_fuseblk(); kmem_cache_destroy(fuse_inode_cachep);}static int fuse_sysfs_init(void){ int err;#ifndef HAVE_FS_SUBSYS err = subsystem_register(&fs_subsys); if (err) return err;#endif kset_set_kset_s(&fuse_subsys, fs_subsys); err = subsystem_register(&fuse_subsys); if (err) goto out_err; kset_set_kset_s(&connections_subsys, fuse_subsys); err = subsystem_register(&connections_subsys); if (err) goto out_fuse_unregister; return 0; out_fuse_unregister: subsystem_unregister(&fuse_subsys); out_err:#ifndef HAVE_FS_SUBSYS subsystem_unregister(&fs_subsys);#endif return err;}static void fuse_sysfs_cleanup(void){ subsystem_unregister(&connections_subsys); subsystem_unregister(&fuse_subsys);#ifndef HAVE_FS_SUBSYS subsystem_unregister(&fs_subsys);#endif}static int __init fuse_init(void){ int res; printk("fuse init (API version %i.%i)\n", FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);#ifndef FUSE_MAINLINE printk("fuse distribution version: %s\n", FUSE_VERSION);#endif INIT_LIST_HEAD(&fuse_conn_list); res = fuse_fs_init(); if (res) goto err; res = fuse_dev_init(); if (res) goto err_fs_cleanup; res = fuse_sysfs_init(); if (res) goto err_dev_cleanup; res = fuse_ctl_init(); if (res) goto err_sysfs_cleanup; return 0; err_sysfs_cleanup: fuse_sysfs_cleanup(); err_dev_cleanup: fuse_dev_cleanup(); err_fs_cleanup: fuse_fs_cleanup(); err: return res;}static void __exit fuse_exit(void){ printk(KERN_DEBUG "fuse exit\n"); fuse_ctl_cleanup(); fuse_sysfs_cleanup(); fuse_fs_cleanup(); fuse_dev_cleanup();}module_init(fuse_init);module_exit(fuse_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -