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

📄 dir.c

📁 UNIX/LINUX下面的用户文件系统
💻 C
📖 第 1 页 / 共 3 页
字号:
	struct fuse_conn *fc = get_fuse_conn(dir);	unsigned len = strlen(link) + 1;	struct fuse_req *req = fuse_get_request(fc);	if (!req)		return -EINTR;	req->in.h.opcode = FUSE_SYMLINK;	req->in.numargs = 2;	req->in.args[0].size = entry->d_name.len + 1;	req->in.args[0].value = entry->d_name.name;	req->in.args[1].size = len;	req->in.args[1].value = link;	return create_new_entry(fc, req, dir, entry, S_IFLNK);}static int fuse_unlink(struct inode *dir, struct dentry *entry){	int err;	struct fuse_conn *fc = get_fuse_conn(dir);	struct fuse_req *req = fuse_get_request(fc);	if (!req)		return -EINTR;	req->in.h.opcode = FUSE_UNLINK;	req->in.h.nodeid = get_node_id(dir);	req->inode = dir;	req->in.numargs = 1;	req->in.args[0].size = entry->d_name.len + 1;	req->in.args[0].value = entry->d_name.name;	request_send(fc, req);	err = req->out.h.error;	fuse_put_request(fc, req);	if (!err) {		struct inode *inode = entry->d_inode;		/* Set nlink to zero so the inode can be cleared, if                   the inode does have more links this will be                   discovered at the next lookup/getattr */		inode->i_nlink = 0;		fuse_invalidate_attr(inode);		fuse_invalidate_attr(dir);		fuse_invalidate_entry_cache(entry);	} else if (err == -EINTR)		fuse_invalidate_entry(entry);	return err;}static int fuse_rmdir(struct inode *dir, struct dentry *entry){	int err;	struct fuse_conn *fc = get_fuse_conn(dir);	struct fuse_req *req = fuse_get_request(fc);	if (!req)		return -EINTR;	req->in.h.opcode = FUSE_RMDIR;	req->in.h.nodeid = get_node_id(dir);	req->inode = dir;	req->in.numargs = 1;	req->in.args[0].size = entry->d_name.len + 1;	req->in.args[0].value = entry->d_name.name;	request_send(fc, req);	err = req->out.h.error;	fuse_put_request(fc, req);	if (!err) {		entry->d_inode->i_nlink = 0;		fuse_invalidate_attr(dir);		fuse_invalidate_entry_cache(entry);	} else if (err == -EINTR)		fuse_invalidate_entry(entry);	return err;}static int fuse_rename(struct inode *olddir, struct dentry *oldent,		       struct inode *newdir, struct dentry *newent){	int err;	struct fuse_rename_in inarg;	struct fuse_conn *fc = get_fuse_conn(olddir);	struct fuse_req *req = fuse_get_request(fc);	if (!req)		return -EINTR;	memset(&inarg, 0, sizeof(inarg));	inarg.newdir = get_node_id(newdir);	req->in.h.opcode = FUSE_RENAME;	req->in.h.nodeid = get_node_id(olddir);	req->inode = olddir;	req->inode2 = newdir;	req->in.numargs = 3;	req->in.args[0].size = sizeof(inarg);	req->in.args[0].value = &inarg;	req->in.args[1].size = oldent->d_name.len + 1;	req->in.args[1].value = oldent->d_name.name;	req->in.args[2].size = newent->d_name.len + 1;	req->in.args[2].value = newent->d_name.name;	request_send(fc, req);	err = req->out.h.error;	fuse_put_request(fc, req);	if (!err) {		fuse_invalidate_attr(olddir);		if (olddir != newdir)			fuse_invalidate_attr(newdir);		/* newent will end up negative */		if (newent->d_inode)			fuse_invalidate_entry_cache(newent);	} else if (err == -EINTR) {		/* If request was interrupted, DEITY only knows if the		   rename actually took place.  If the invalidation		   fails (e.g. some process has CWD under the renamed		   directory), then there can be inconsistency between		   the dcache and the real filesystem.  Tough luck. */		fuse_invalidate_entry(oldent);		if (newent->d_inode)			fuse_invalidate_entry(newent);	}	return err;}static int fuse_link(struct dentry *entry, struct inode *newdir,		     struct dentry *newent){	int err;	struct fuse_link_in inarg;	struct inode *inode = entry->d_inode;	struct fuse_conn *fc = get_fuse_conn(inode);	struct fuse_req *req = fuse_get_request(fc);	if (!req)		return -EINTR;	memset(&inarg, 0, sizeof(inarg));	inarg.oldnodeid = get_node_id(inode);	req->in.h.opcode = FUSE_LINK;	req->inode2 = inode;	req->in.numargs = 2;	req->in.args[0].size = sizeof(inarg);	req->in.args[0].value = &inarg;	req->in.args[1].size = newent->d_name.len + 1;	req->in.args[1].value = newent->d_name.name;	err = create_new_entry(fc, req, newdir, newent, inode->i_mode);	/* Contrary to "normal" filesystems it can happen that link	   makes two "logical" inodes point to the same "physical"	   inode.  We invalidate the attributes of the old one, so it	   will reflect changes in the backing inode (link count,	   etc.)	*/	if (!err || err == -EINTR)		fuse_invalidate_attr(inode);	return err;}int fuse_do_getattr(struct inode *inode){	int err;	struct fuse_attr_out arg;	struct fuse_conn *fc = get_fuse_conn(inode);	struct fuse_req *req = fuse_get_request(fc);	if (!req)		return -EINTR;	req->in.h.opcode = FUSE_GETATTR;	req->in.h.nodeid = get_node_id(inode);	req->inode = inode;	req->out.numargs = 1;	req->out.args[0].size = sizeof(arg);	req->out.args[0].value = &arg;	request_send(fc, req);	err = req->out.h.error;	fuse_put_request(fc, req);	if (!err) {		if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {#ifndef KERNEL_2_6_12_PLUS			if (get_node_id(inode) != FUSE_ROOT_ID)				make_bad_inode(inode);#else			make_bad_inode(inode);#endif			err = -EIO;		} else {			struct fuse_inode *fi = get_fuse_inode(inode);			fuse_change_attributes(inode, &arg.attr);			fi->i_time = time_to_jiffies(arg.attr_valid,						     arg.attr_valid_nsec);		}	}	return err;}/* * Calling into a user-controlled filesystem gives the filesystem * daemon ptrace-like capabilities over the requester process.  This * means, that the filesystem daemon is able to record the exact * filesystem operations performed, and can also control the behavior * of the requester process in otherwise impossible ways.  For example * it can delay the operation for arbitrary length of time allowing * DoS against the requester. * * For this reason only those processes can call into the filesystem, * for which the owner of the mount has ptrace privilege.  This * excludes processes started by other users, suid or sgid processes. */static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task){	if (fc->flags & FUSE_ALLOW_OTHER)		return 1;	if (task->euid == fc->user_id &&	    task->suid == fc->user_id &&	    task->uid == fc->user_id &&	    task->egid == fc->group_id &&	    task->sgid == fc->group_id &&	    task->gid == fc->group_id)		return 1;	return 0;}/* * Check whether the inode attributes are still valid * * If the attribute validity timeout has expired, then fetch the fresh * attributes with a 'getattr' request * * I'm not sure why cached attributes are never returned for the root * inode, this is probably being too cautious. */static int fuse_revalidate(struct dentry *entry){	struct inode *inode = entry->d_inode;	struct fuse_inode *fi = get_fuse_inode(inode);	struct fuse_conn *fc = get_fuse_conn(inode);	if (!fuse_allow_task(fc, current))		return -EACCES;	if (get_node_id(inode) != FUSE_ROOT_ID &&	    time_before_eq(jiffies, fi->i_time))		return 0;	return fuse_do_getattr(inode);}#ifdef KERNEL_2_6static int fuse_access(struct inode *inode, int mask){	struct fuse_conn *fc = get_fuse_conn(inode);	struct fuse_req *req;	struct fuse_access_in inarg;	int err;	if (fc->no_access)		return 0;	req = fuse_get_request(fc);	if (!req)		return -EINTR;	memset(&inarg, 0, sizeof(inarg));	inarg.mask = mask;	req->in.h.opcode = FUSE_ACCESS;	req->in.h.nodeid = get_node_id(inode);	req->inode = inode;	req->in.numargs = 1;	req->in.args[0].size = sizeof(inarg);	req->in.args[0].value = &inarg;	request_send(fc, req);	err = req->out.h.error;	fuse_put_request(fc, req);	if (err == -ENOSYS) {		fc->no_access = 1;		err = 0;	}	return err;}#endif/* * Check permission.  The two basic access models of FUSE are: * * 1) Local access checking ('default_permissions' mount option) based * on file mode.  This is the plain old disk filesystem permission * modell. * * 2) "Remote" access checking, where server is responsible for * checking permission in each inode operation.  An exception to this * is if ->permission() was invoked from sys_access() in which case an * access request is sent.  Execute permission is still checked * locally based on file mode. */static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd){	struct fuse_conn *fc = get_fuse_conn(inode);	if (!fuse_allow_task(fc, current))		return -EACCES;	else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {#ifdef KERNEL_2_6_10_PLUS		int err = generic_permission(inode, mask, NULL);#else		int err = vfs_permission(inode, mask);#endif		/* If permission is denied, try to refresh file		   attributes.  This is also needed, because the root		   node will at first have no permissions */		if (err == -EACCES) {		 	err = fuse_do_getattr(inode);			if (!err)#ifdef KERNEL_2_6_10_PLUS				err = generic_permission(inode, mask, NULL);#else				err = vfs_permission(inode, mask);#endif		}		/* Note: the opposite of the above test does not		   exist.  So if permissions are revoked this won't be		   noticed immediately, only after the attribute		   timeout has expired */		return err;	} else {		int mode = inode->i_mode;#ifndef KERNEL_2_6_11_PLUS		if ((mask & MAY_WRITE) && IS_RDONLY(inode) &&                    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))                        return -EROFS;#endif		if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))			return -EACCES;#ifdef KERNEL_2_6		if (nd && (nd->flags & LOOKUP_ACCESS))			return fuse_access(inode, mask);#endif		return 0;	}}static int parse_dirfile(char *buf, size_t nbytes, struct file *file,			 void *dstbuf, filldir_t filldir){	while (nbytes >= FUSE_NAME_OFFSET) {		struct fuse_dirent *dirent = (struct fuse_dirent *) buf;		size_t reclen = FUSE_DIRENT_SIZE(dirent);		int over;		if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)			return -EIO;		if (reclen > nbytes)			break;		over = filldir(dstbuf, dirent->name, dirent->namelen,			       file->f_pos, dirent->ino, dirent->type);		if (over)			break;		buf += reclen;		nbytes -= reclen;		file->f_pos = dirent->off;	}	return 0;}static inline size_t fuse_send_readdir(struct fuse_req *req, struct file *file,				       struct inode *inode, loff_t pos,				       size_t count){	return fuse_send_read_common(req, file, inode, pos, count, 1);}static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir){	int err;	size_t nbytes;	struct page *page;	struct inode *inode = file->f_dentry->d_inode;	struct fuse_conn *fc = get_fuse_conn(inode);	struct fuse_req *req;	if (is_bad_inode(inode))		return -EIO;	req = fuse_get_request(fc);	if (!req)		return -EINTR;	page = alloc_page(GFP_KERNEL);	if (!page) {		fuse_put_request(fc, req);		return -ENOMEM;	}	req->num_pages = 1;	req->pages[0] = page;	nbytes = fuse_send_readdir(req, file, inode, file->f_pos, PAGE_SIZE);	err = req->out.h.error;	fuse_put_request(fc, req);	if (!err)		err = parse_dirfile(page_address(page), nbytes, file, dstbuf,				    filldir);	__free_page(page);	fuse_invalidate_attr(inode); /* atime changed */	return err;}static char *read_link(struct dentry *dentry){	struct inode *inode = dentry->d_inode;	struct fuse_conn *fc = get_fuse_conn(inode);	struct fuse_req *req = fuse_get_request(fc);	char *link;	if (!req)		return ERR_PTR(-EINTR);	link = (char *) __get_free_page(GFP_KERNEL);	if (!link) {		link = ERR_PTR(-ENOMEM);		goto out;	}	req->in.h.opcode = FUSE_READLINK;	req->in.h.nodeid = get_node_id(inode);	req->inode = inode;	req->out.argvar = 1;	req->out.numargs = 1;	req->out.args[0].size = PAGE_SIZE - 1;	req->out.args[0].value = link;	request_send(fc, req);	if (req->out.h.error) {		free_page((unsigned long) link);		link = ERR_PTR(req->out.h.error);	} else		link[req->out.args[0].size] = '\0'; out:	fuse_put_request(fc, req);	fuse_invalidate_attr(inode); /* atime changed */	return link;}static void free_link(char *link){	if (!IS_ERR(link))		free_page((unsigned long) link);}#ifdef KERNEL_2_6_13_PLUSstatic void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd){	nd_set_link(nd, read_link(dentry));	return NULL;}static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c){	free_link(nd_get_link(nd));}#elif defined(KERNEL_2_6_8_PLUS)static int fuse_follow_link(struct dentry *dentry, struct nameidata *nd){	nd_set_link(nd, read_link(dentry));	return 0;}static void fuse_put_link(struct dentry *dentry, struct nameidata *nd)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -