我發現這個鏈接(http://www.spinics.net/lists/newbies/msg41016.html),並一直在考慮這樣做。如何獲得Linux內核中文件的大小?
#include <linux/path.h>
#include <linux/namei.h>
#include <linux/fs.h>
struct path p;
struct kstat ks;
kern_path(filepath, 0, &p);
vfs_getattr(&p, &ks);
printk(KERN_INFO "size: %lld\n", ks.size);
這將不能編譯,因爲::所以我一個內核模塊中編寫代碼
/root/kernelmodule/hello.c:15: warning: passing argument 1 of ‘vfs_getattr’ from incompatible pointer type
include/linux/fs.h:2563: note: expected ‘struct vfsmount *’ but argument is of type ‘struct path *’
/root/kernelmodule/hello.c:15: warning: passing argument 2 of ‘vfs_getattr’ from incompatible pointer type
include/linux/fs.h:2563: note: expected ‘struct dentry *’ but argument is of type ‘struct kstat *’
/root/kernelmodule/hello.c:15: error: too few arguments to function ‘vfs_getattr’
所以我真的很困惑,因爲我一直在尋找這個文件:http://lxr.free-electrons.com/source/fs/stat.c#L40
現在我在/linux/fs.h裏面看到,vfs_getattr的原型是:
extern int vfs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
任何人都可以幫我實施我的嗎?我正在閱讀vfsmount和dentry,但仍然丟失。
「in the linux kernel」 - 意思是說你正在編寫kernel-land代碼?如果不是,那麼只需'stat()'或'ftell()'。 – 2013-09-23 18:18:28
是的,這是在一個內核模塊中。 – wright8191