本文使用内核源码版本为:2.6.32
vfs_stat函数原型:int vfs_stat(char __user *name, struct kstat *stat)
struct kstat定义位于:include/fs.h
struct kstat { u64 ino; dev_t dev; umode_t mode; unsigned int nlink; uid_t uid; gid_t gid; dev_t rdev; loff_t size; struct timespec atime; struct timespec mtime; struct timespec ctime; unsigned long blksize; unsigned long long blocks; };该结构体封装了文件的基本属性
函数 vfs_stat 定义位于:fs/stat.c
int vfs_stat(char __user *name, struct kstat *stat) { return vfs_fstatat(AT_FDCWD, name, stat, 0); //调用了vfs_fstatat } EXPORT_SYMBOL(vfs_stat);
int vfs_fstatat(int dfd, char __user *filename, struct kstat *stat, int flag) { struct path path; int error = -EINVAL; int lookup_flags = 0; if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0) goto out; if (!(flag & AT_SYMLINK_NOFOLLOW)) lookup_flags |= LOOKUP_FOLLOW; error = user_path_at(dfd, filename, lookup_flags, &path); if (error) goto out; error = vfs_getattr(path.mnt, path.dentry, stat); //这里调用 vfs_getattr 获取文件属性 path_put(&path); out: return error; } EXPORT_SYMBOL(vfs_fstatat);
int vfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) { struct inode *inode = dentry->d_inode; int retval; retval = security_inode_getattr(mnt, dentry); if (retval) return retval; if (inode->i_op->getattr) return inode->i_op->getattr(mnt, dentry, stat); //通过 inode 取得文件属性 generic_fillattr(inode, stat); return 0; } EXPORT_SYMBOL(vfs_getattr);