内核获取文件大小

函数:

 int vfs_stat(const char __user *, struct kstat *);

struct kstat 结构体如下:

struct kstat {
u64 ino;
dev_t dev;
umode_t mode;
unsigned intnlink;
kuid_t uid;
kgid_t gid;
dev_trdev;
loff_t size;
struct timespec  atime;
struct timespecmtime;
struct timespecctime;
unsigned longblksize;
unsigned long longblocks;
};




用法如下:

#include 
#include
#include 
#include 
#include 
#include 

static int __init hello_init (void){
        mm_segment_t fs;
        struct kstat *stat;

        fs= get_fs();
        set_fs(KERNEL_DS);

        stat =(struct kstat *) kmalloc(sizeof(struct kstat),GFP_KERNEL);
        vfs_stat("/home/zyj/work/fileops2/123",stat);
        printk(KERN_ALERT "%d\n",stat->size);
        set_fs(fs);


        return 0;
}

static void __exit  hello_exit (void) {
        printk(KERN_ALERT "exit\n");
}

MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);

你可能感兴趣的:(内核获取文件大小)