+ -
当前位置:首页 → 问答吧 → 问:在<linux/fs。h>

问:在<linux/fs。h>

时间:2006-03-19

来源:互联网

extern int filp_close(struct file *, fl_owner_t id);
其中发了fl_owner_t 是什么意思,我找了半天找不到,高手请帮帮忙。。
extern struct file *filp_open(const char *, int, int);
后边两个参数什么意思??

作者: macping   发布时间: 2006-03-19

我再和你关注一个问题,你有答案了也告诉我一声把
这两个函数是不是可以用在内核源代码中加入读写文件的代码阿?
extern int filp_close(struct file *, fl_owner_t id);关闭

extern struct file *filp_open(const char *, int, int);打开

我只知道fl_owner_t在linux/file.h中定义,是指向下面文件描述符结构的指针
当前运行的进程描述符中有一项指向这个结构,里面应包括进程所有打开的文件
我觉得如果要关闭一个文件的话,得把current-〉files_struct作为参数传过来
/*
* Open file table structure
*/
struct files_struct {
atomic_t count;
struct fdtable *fdt;
struct fdtable fdtab;
fd_set close_on_exec_init;
fd_set open_fds_init;
struct file * fd_array[NR_OPEN_DEFAULT];
spinlock_t file_lock; /* Protects concurrent writers. Nests inside tsk->alloc_lock */
};

作者: sufei   发布时间: 2006-03-29

楼上的找到答案了。。找到的话说一声。。。。。。。

作者: macping   发布时间: 2006-04-04

看看下面这个模块例子,应该能明白,怎样来使用filp_open和filp_close
下面这个例子申请了一个页面作为缓冲,将一个文件的内容考到另一个文件,如果模块编译不通过,直接使用文件的绝对地址应该可通过:
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif

#include <linux/module.h>
#include <linux/malloc.h>
#include <asm/uaccess.h>
#include <asm/fs.h>

char *src = NULL;
char *dst = NULL;
MODULE_PARM(src,"s");
MODULE_PARM(dst,"s");

/* This is a kernel module, I must be very
* carefull to clean up everything myself,
* there is nobody who can do it for me.
*/

int init_module(void)
{
struct file *srcf,*dstf;
int retval,orgfsuid,orgfsgid;
mm_segment_t orgfs;
char *buffer;
unsigned long page;

/* Save uid and gid used for filesystem access.
* Set user and group to 0 (root)
*/
orgfsuid=current->fsuid;
orgfsgid=current->fsgid;
current->fsuid=current->fsgid=0;
/* save FS register and set FS register to kernel
* space, needed for read and write to accept
* buffer in kernel space.
*/
orgfs=get_fs();
set_fs(KERNEL_DS);

if (src&&dst&&*src&&*dst) {
printk("Copying %s to %s\n",src,dst);
/* Allocate one page for buffer */
page = __get_free_page(GFP_KERNEL);
if (page) {
buffer=(char*)page;
srcf = filp_open(src, O_RDONLY, 0);
if (IS_ERR(srcf)) {
printk("kcp: Error %ld opening %s\n",-PTR_ERR(srcf),src);
} else {
/* The object must have a read method */
if (srcf->f_op&&srcf->f_op->read) {
dstf = filp_open(dst, O_WRONLY|O_TRUNC|O_CREAT , 0644);
if (IS_ERR(dstf)) {
printk("kcp: Error %ld opening %s\n",-PTR_ERR(dstf),dst);
} else {
/* The object must have a write method */
if (dstf->f_op&&dstf->f_op->write) {
do {
/* Read as much as posible into the buffer,
* at most one page.
*/
retval=srcf->f_op->read(srcf,buffer,PAGE_SIZE,&srcf->f_pos);
if (retval<0) printk("kcp: Read error %d\n",-retval);
if (retval>0) {
int index=0,bufsize=retval;
/* Continue writing until error or everything
* written.
*/
while ((index<bufsize)&&
((retval=dstf->f_op->write
(dstf,buffer+index,bufsize-index,&dstf->f_pos))>0)
) index+=retval;
if (index<bufsize) printk("kcp: Write error %d\n",-retval);
}
/* Copy loop continues until EOF or error.
*/
} while (retval>0);
/* Now clean up everything that was actually allocated
*/
} else {
printk("kcp: %s does not have a write method\n",dst);
}
retval=filp_close(dstf,NULL);
if (retval) printk("kcp: Error %d closing %s\n",-retval,dst);
}
} else {
printk("kcp: %s does not have a read method\n",src);
}
retval=filp_close(srcf,NULL);
if (retval) printk("kcp: Error %d closing %s\n",-retval,src);
}
free_page(page);
} else {
printk("kcp: Out of memory\n");
}
} else printk("kcp: Both src= and dst= must be specified\n");

set_fs(orgfs);
current->fsuid=orgfsuid;
current->fsgid=orgfsgid;

/* End of demo, returning an error code
* makes insmod/modprobe remove the module
* from memory.
*/
return 1;
}

作者: sufei   发布时间: 2006-04-28