⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inode.c

📁 FUSE文件系统开发工具,将内核层面的文件系统开发过程平移到应用层面上来。
💻 C
📖 第 1 页 / 共 2 页
字号:
	int type = 1;	u64 nodeid;	u32 generation;	if (len < 3 || (connectable && len < 6))		return  255;	nodeid = get_fuse_inode(inode)->nodeid;	generation = inode->i_generation;	len = 3;	fh[0] = (u32)(nodeid >> 32);	fh[1] = (u32)(nodeid & 0xffffffff);	fh[2] = generation;	if (connectable && !S_ISDIR(inode->i_mode)) {		struct inode *parent;		spin_lock(&dentry->d_lock);		parent = dentry->d_parent->d_inode;		nodeid = get_fuse_inode(parent)->nodeid;		generation = parent->i_generation;		fh[3] = (u32)(nodeid >> 32);		fh[4] = (u32)(nodeid & 0xffffffff);		fh[5] = generation;		spin_unlock(&dentry->d_lock);		len = 6;		type = 2;	}	*max_len = len;	return type;}#ifdef KERNEL_2_6_24_PLUSstatic struct dentry *fuse_fh_to_dentry(struct super_block *sb,		struct fid *fid, int fh_len, int fh_type){	struct fuse_inode_handle handle;	if (fh_len < 3 || fh_type > 2)		return NULL;	handle.nodeid = (u64) fid->raw[0] << 32;	handle.nodeid |= (u64) fid->raw[1];	handle.generation = fid->raw[2];	return fuse_get_dentry(sb, &handle);}static struct dentry *fuse_fh_to_parent(struct super_block *sb,		struct fid *fid, int fh_len, int fh_type){	struct fuse_inode_handle parent;	if (fh_type != 2 || fh_len < 6)		return NULL;	parent.nodeid = (u64) fid->raw[3] << 32;	parent.nodeid |= (u64) fid->raw[4];	parent.generation = fid->raw[5];	return fuse_get_dentry(sb, &parent);}static const struct export_operations fuse_export_operations = {	.fh_to_dentry	= fuse_fh_to_dentry,	.fh_to_parent	= fuse_fh_to_parent,	.encode_fh	= fuse_encode_fh,};#elsestatic struct dentry *fuse_get_dentry_old(struct super_block *sb, void *objp){	return fuse_get_dentry(sb, objp);}static struct export_operations fuse_export_operations;static struct dentry *fuse_decode_fh(struct super_block *sb, u32 *fh,			int fh_len, int fileid_type,			int (*acceptable)(void *context, struct dentry *de),			void *context){	struct fuse_inode_handle handle;	struct fuse_inode_handle parent;	if (fh_len < 3 || fileid_type > 2)		return NULL;	if (fileid_type == 2) {		if (fh_len < 6)			return NULL;		parent.nodeid = (u64) fh[3] << 32;		parent.nodeid |= (u64) fh[4];		parent.generation = fh[5];	} else {		parent.nodeid = 0;		parent.generation = 0;	}	handle.nodeid = (u64) fh[0] << 32;	handle.nodeid |= (u64) fh[1];	handle.generation = fh[2];	return fuse_export_operations.		find_exported_dentry(sb, &handle, &parent, acceptable, context);}static struct export_operations fuse_export_operations = {	.get_dentry	= fuse_get_dentry_old,	.encode_fh      = fuse_encode_fh,	.decode_fh	= fuse_decode_fh,};#endif#endifstatic struct super_operations fuse_super_operations = {	.alloc_inode    = fuse_alloc_inode,	.destroy_inode  = fuse_destroy_inode,#ifndef KERNEL_2_6_25_PLUS	.read_inode	= fuse_read_inode,#endif	.clear_inode	= fuse_clear_inode,	.drop_inode	= generic_delete_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;	/* This is a privileged option */	if ((d.flags & FUSE_ALLOW_OTHER) && !capable(CAP_SYS_ADMIN))		return -EPERM;	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,	.fs_flags	= FS_HAS_SUBTYPE | FS_SAFE,};#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 | FS_HAS_SUBTYPE,};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#ifdef KERNEL_2_6_25_PLUSstatic struct kobject *fuse_kobj;static struct kobject *connections_kobj;#else#ifndef HAVE_FS_SUBSYSstatic decl_subsys(fs, NULL, NULL);#endifstatic decl_subsys(fuse, NULL, NULL);static decl_subsys(connections, NULL, NULL);#endif#ifdef KERNEL_2_6_24_PLUSstatic void fuse_inode_init_once(struct kmem_cache *cachep, void *foo)#elsestatic void fuse_inode_init_once(void *foo, struct kmem_cache *cachep,				 unsigned long flags)#endif{	struct inode * inode = foo;#ifndef KERNEL_2_6_22_PLUS	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==	    SLAB_CTOR_CONSTRUCTOR)#endif	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;#ifdef KERNEL_2_6_23_PLUS	fuse_inode_cachep = kmem_cache_create("fuse_inode",					      sizeof(struct fuse_inode),					      0, SLAB_HWCACHE_ALIGN,					      fuse_inode_init_once);#else	fuse_inode_cachep = kmem_cache_create("fuse_inode",					      sizeof(struct fuse_inode),					      0, SLAB_HWCACHE_ALIGN,					      fuse_inode_init_once, NULL);#endif	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;#ifdef KERNEL_2_6_25_PLUS	fuse_kobj = kobject_create_and_add("fuse", fs_kobj);	if (!fuse_kobj) {		err = -ENOMEM;		goto out_err;	}	connections_kobj = kobject_create_and_add("connections", fuse_kobj);	if (!connections_kobj) {		err = -ENOMEM;		goto out_fuse_unregister;	}	return 0; out_fuse_unregister:	kobject_put(fuse_kobj); out_err:#else#ifndef HAVE_FS_SUBSYS	err = subsystem_register(&fs_subsys);	if (err)		return err;#endif#ifdef KERNEL_2_6_22_PLUS	kobj_set_kset_s(&fuse_subsys, fs_subsys);#else	kset_set_kset_s(&fuse_subsys, fs_subsys);#endif	err = subsystem_register(&fuse_subsys);	if (err)		goto out_err;#ifdef KERNEL_2_6_22_PLUS	kobj_set_kset_s(&connections_subsys, fuse_subsys);#else	kset_set_kset_s(&connections_subsys, fuse_subsys);#endif	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#endif	return err;}static void fuse_sysfs_cleanup(void){#ifdef KERNEL_2_6_25_PLUS	kobject_put(connections_kobj);	kobject_put(fuse_kobj);#else	subsystem_unregister(&connections_subsys);	subsystem_unregister(&fuse_subsys);#ifndef HAVE_FS_SUBSYS	subsystem_unregister(&fs_subsys);#endif#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 + -