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

📄 hppfs_kern.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) * Licensed under the GPL */#include <linux/fs.h>#include <linux/file.h>#include <linux/module.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/list.h>#include <linux/kernel.h>#include <linux/ctype.h>#include <linux/dcache.h>#include <linux/statfs.h>#include <asm/uaccess.h>#include <asm/fcntl.h>#include "os.h"static int init_inode(struct inode *inode, struct dentry *dentry);struct hppfs_data {	struct list_head list;	char contents[PAGE_SIZE - sizeof(struct list_head)];};struct hppfs_private {	struct file *proc_file;	int host_fd;	loff_t len;	struct hppfs_data *contents;};struct hppfs_inode_info {        struct dentry *proc_dentry;	struct inode vfs_inode;};static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode){	return container_of(inode, struct hppfs_inode_info, vfs_inode);}#define HPPFS_SUPER_MAGIC 0xb00000eestatic const struct super_operations hppfs_sbops;static int is_pid(struct dentry *dentry){	struct super_block *sb;	int i;	sb = dentry->d_sb;	if((sb->s_op != &hppfs_sbops) || (dentry->d_parent != sb->s_root))		return(0);	for(i = 0; i < dentry->d_name.len; i++){		if(!isdigit(dentry->d_name.name[i]))			return(0);	}	return(1);}static char *dentry_name(struct dentry *dentry, int extra){	struct dentry *parent;	char *root, *name;	const char *seg_name;	int len, seg_len;	len = 0;	parent = dentry;	while(parent->d_parent != parent){		if(is_pid(parent))			len += strlen("pid") + 1;		else len += parent->d_name.len + 1;		parent = parent->d_parent;	}	root = "proc";	len += strlen(root);	name = kmalloc(len + extra + 1, GFP_KERNEL);	if(name == NULL) return(NULL);	name[len] = '\0';	parent = dentry;	while(parent->d_parent != parent){		if(is_pid(parent)){			seg_name = "pid";			seg_len = strlen("pid");		}		else {			seg_name = parent->d_name.name;			seg_len = parent->d_name.len;		}		len -= seg_len + 1;		name[len] = '/';		strncpy(&name[len + 1], seg_name, seg_len);		parent = parent->d_parent;	}	strncpy(name, root, strlen(root));	return(name);}struct dentry_operations hppfs_dentry_ops = {};static int file_removed(struct dentry *dentry, const char *file){	char *host_file;	int extra, fd;	extra = 0;	if(file != NULL) extra += strlen(file) + 1;	host_file = dentry_name(dentry, extra + strlen("/remove"));	if(host_file == NULL){		printk("file_removed : allocation failed\n");		return(-ENOMEM);	}	if(file != NULL){		strcat(host_file, "/");		strcat(host_file, file);	}	strcat(host_file, "/remove");	fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);	kfree(host_file);	if(fd > 0){		os_close_file(fd);		return(1);	}	return(0);}static void hppfs_read_inode(struct inode *ino){	struct inode *proc_ino;	if(HPPFS_I(ino)->proc_dentry == NULL)		return;	proc_ino = HPPFS_I(ino)->proc_dentry->d_inode;	ino->i_uid = proc_ino->i_uid;	ino->i_gid = proc_ino->i_gid;	ino->i_atime = proc_ino->i_atime;	ino->i_mtime = proc_ino->i_mtime;	ino->i_ctime = proc_ino->i_ctime;	ino->i_ino = proc_ino->i_ino;	ino->i_mode = proc_ino->i_mode;	ino->i_nlink = proc_ino->i_nlink;	ino->i_size = proc_ino->i_size;	ino->i_blocks = proc_ino->i_blocks;}static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,                                  struct nameidata *nd){	struct dentry *proc_dentry, *new, *parent;	struct inode *inode;	int err, deleted;	deleted = file_removed(dentry, NULL);	if(deleted < 0)		return(ERR_PTR(deleted));	else if(deleted)		return(ERR_PTR(-ENOENT));	err = -ENOMEM;	parent = HPPFS_I(ino)->proc_dentry;	mutex_lock(&parent->d_inode->i_mutex);	proc_dentry = d_lookup(parent, &dentry->d_name);	if(proc_dentry == NULL){		proc_dentry = d_alloc(parent, &dentry->d_name);		if(proc_dentry == NULL){			mutex_unlock(&parent->d_inode->i_mutex);			goto out;		}		new = (*parent->d_inode->i_op->lookup)(parent->d_inode,						       proc_dentry, NULL);		if(new){			dput(proc_dentry);			proc_dentry = new;		}	}	mutex_unlock(&parent->d_inode->i_mutex);	if(IS_ERR(proc_dentry))		return(proc_dentry);	inode = iget(ino->i_sb, 0);	if(inode == NULL)		goto out_dput;	err = init_inode(inode, proc_dentry);	if(err)		goto out_put;	hppfs_read_inode(inode); 	d_add(dentry, inode);	dentry->d_op = &hppfs_dentry_ops;	return(NULL); out_put:	iput(inode); out_dput:	dput(proc_dentry); out:	return(ERR_PTR(err));}static const struct inode_operations hppfs_file_iops = {};static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,			 loff_t *ppos, int is_user){	ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);	ssize_t n;	read = file->f_path.dentry->d_inode->i_fop->read;	if(!is_user)		set_fs(KERNEL_DS);	n = (*read)(file, buf, count, &file->f_pos);	if(!is_user)		set_fs(USER_DS);	if(ppos) *ppos = file->f_pos;	return n;}static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count){	ssize_t n;	int cur, err;	char *new_buf;	n = -ENOMEM;	new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);	if(new_buf == NULL){		printk("hppfs_read_file : kmalloc failed\n");		goto out;	}	n = 0;	while(count > 0){		cur = min_t(ssize_t, count, PAGE_SIZE);		err = os_read_file(fd, new_buf, cur);		if(err < 0){			printk("hppfs_read : read failed, errno = %d\n",			       err);			n = err;			goto out_free;		}		else if(err == 0)			break;		if(copy_to_user(buf, new_buf, err)){			n = -EFAULT;			goto out_free;		}		n += err;		count -= err;	} out_free:	kfree(new_buf); out:	return n;}static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,			  loff_t *ppos){	struct hppfs_private *hppfs = file->private_data;	struct hppfs_data *data;	loff_t off;	int err;	if(hppfs->contents != NULL){		if(*ppos >= hppfs->len) return(0);		data = hppfs->contents;		off = *ppos;		while(off >= sizeof(data->contents)){			data = list_entry(data->list.next, struct hppfs_data,					  list);			off -= sizeof(data->contents);		}		if(off + count > hppfs->len)			count = hppfs->len - off;		copy_to_user(buf, &data->contents[off], count);		*ppos += count;	}	else if(hppfs->host_fd != -1){		err = os_seek_file(hppfs->host_fd, *ppos);		if(err){			printk("hppfs_read : seek failed, errno = %d\n", err);			return(err);		}		count = hppfs_read_file(hppfs->host_fd, buf, count);		if(count > 0)			*ppos += count;	}	else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);	return(count);}static ssize_t hppfs_write(struct file *file, const char __user *buf, size_t len,			   loff_t *ppos){	struct hppfs_private *data = file->private_data;	struct file *proc_file = data->proc_file;	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);	int err;	write = proc_file->f_path.dentry->d_inode->i_fop->write;	proc_file->f_pos = file->f_pos;	err = (*write)(proc_file, buf, len, &proc_file->f_pos);	file->f_pos = proc_file->f_pos;	return(err);}static int open_host_sock(char *host_file, int *filter_out){	char *end;	int fd;	end = &host_file[strlen(host_file)];	strcpy(end, "/rw");	*filter_out = 1;	fd = os_connect_socket(host_file);	if(fd > 0)		return(fd);	strcpy(end, "/r");	*filter_out = 0;	fd = os_connect_socket(host_file);	return(fd);}static void free_contents(struct hppfs_data *head){	struct hppfs_data *data;	struct list_head *ele, *next;	if(head == NULL) return;	list_for_each_safe(ele, next, &head->list){		data = list_entry(ele, struct hppfs_data, list);		kfree(data);	}	kfree(head);}static struct hppfs_data *hppfs_get_data(int fd, int filter,					 struct file *proc_file,					 struct file *hppfs_file,					 loff_t *size_out){	struct hppfs_data *data, *new, *head;	int n, err;	err = -ENOMEM;	data = kmalloc(sizeof(*data), GFP_KERNEL);	if(data == NULL){		printk("hppfs_get_data : head allocation failed\n");		goto failed;	}	INIT_LIST_HEAD(&data->list);	head = data;	*size_out = 0;	if(filter){		while((n = read_proc(proc_file, data->contents,				     sizeof(data->contents), NULL, 0)) > 0)			os_write_file(fd, data->contents, n);		err = os_shutdown_socket(fd, 0, 1);		if(err){			printk("hppfs_get_data : failed to shut down "			       "socket\n");			goto failed_free;		}	}	while(1){		n = os_read_file(fd, data->contents, sizeof(data->contents));		if(n < 0){			err = n;			printk("hppfs_get_data : read failed, errno = %d\n",			       err);			goto failed_free;		}		else if(n == 0)			break;

⌨️ 快捷键说明

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