Skip to contentSkip to conversation

add flags to stat()

PATCH In review Ameer Armaly 5 participants 6 messages Aug 6, 2006 – Aug 7, 2006

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 add flags to stat()

Hi all.
This is my first serious attempt at kernel hacking, so here goes. I was reading in LWN concerning the idea of adding flags to the stat system call, so that applications could request specific items they wanted as opposed to having to deal with the whole stat struct. Here is a patch that does just that. It ads a new parameter to the stat family including stat, stat64, and stat (and all their variants) to contain flags, and defines a flag for each entry in the stat structure. These flags are passed down the chain through the various vfs functions, and finally to the individual filesystem. This is where it hits a snag: each filesystem has to implement the actual processing of the flags on its own. However, it still seems to run fine even with these changes, so backwards compatibility doesn't appear to be a problem. If anyone knows some sources to read of a more efficient way to implement something like this, please let me know. All the kernel interfaces I have seen that use stat have been modified to pass 0 (give me everything) for now; we can optimize them later. Thoughts?

drivers/block/loop.c +1 -1
index 7b3b94d..73c60cf 100644
@@ -1008,7 +1008,7 @@ loop_get_status(struct loop_device *lo,
 
  	if (lo->lo_state != Lo_bound)
  		return -ENXIO;
-	error = vfs_getattr(file->f_vfsmnt, file->f_dentry, &stat);
+	error = vfs_getattr(file->f_vfsmnt, file->f_dentry, &stat, 0);
  	if (error)
  		return error;
  	memset(info, 0, sizeof(*info));
fs/stat.c +56 -37

241 lines not on this page · raw message

include/linux/fs.h +8 -7
index 2561020..252dc1a 100644
@@ -40,6 +40,7 @@ struct files_stat_struct {
  extern struct files_stat_struct files_stat;
  extern int get_max_files(void);
 
+
  struct inodes_stat_t {
  	int nr_inodes;
  	int nr_unused;
@@ -1099,7 +1100,7 @@ struct inode_operations {
  	void (*truncate) (struct inode *);
  	int (*permission) (struct inode *, int, struct nameidata *);
  	int (*setattr) (struct dentry *, struct iattr *);
-	int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);
+	int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *, unsigned int flags);
  	int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);
  	ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
  	ssize_t (*listxattr) (struct dentry *, char *, size_t);
@@ -1764,7 +1765,7 @@ extern int page_symlink(struct inode *in
  extern struct inode_operations page_symlink_inode_operations;
  extern int generic_readlink(struct dentry *, char __user *, int);
  extern void generic_fillattr(struct inode *, struct kstat *);
-extern int vfs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
+extern int vfs_getattr(struct vfsmount *, struct dentry *, struct kstat *, unsigned int);
  void inode_add_bytes(struct inode *inode, loff_t bytes);
  void inode_sub_bytes(struct inode *inode, loff_t bytes);
  loff_t inode_get_bytes(struct inode *inode);
@@ -1772,11 +1773,11 @@ void inode_set_bytes(struct inode *inode
 
  extern int vfs_readdir(struct file *, filldir_t, void *);
 
-extern int vfs_stat(char __user *, struct kstat *);
-extern int vfs_lstat(char __user *, struct kstat *);
-extern int vfs_stat_fd(int dfd, char __user *, struct kstat *);
-extern int vfs_lstat_fd(int dfd, char __user *, struct kstat *);
-extern int vfs_fstat(unsigned int, struct kstat *);
+extern int vfs_stat(char __user *, struct kstat *, unsigned int);
+extern int vfs_lstat(char __user *, struct kstat *, unsigned int);
+extern int vfs_stat_fd(int dfd, char __user *, struct kstat *, unsigned int);
+extern int vfs_lstat_fd(int dfd, char __user *, struct kstat *, unsigned int);
+extern int vfs_fstat(unsigned int, struct kstat *, unsigned int);
 
  extern int vfs_ioctl(struct file *, unsigned int, unsigned int, unsigned long);
 
include/linux/stat.h +15 -0
index 8669291..7f607e2 100644
@@ -72,6 +72,21 @@ struct kstat {
  	unsigned long long	blocks;
  };
 
+/* Flags for the construction of a stat structure as opposed to getting the whole thing. */
+#define S_GETDEV 0x001
+#define S_GETINODE 0x002
+#define S_GETMODE 0x004
+#define S_GETNLINK 0x008
+#define S_GETUID 0x010
+#define S_GETGID 0x020
+#define S_GETRDEV 0x040
+#define S_GETSIZE 0x080
+#define S_GETBLKSIZE 0x100
+#define S_GETBLOCKS 0x100
+#define S_GETATIME 0x200
+#define S_GETMTIME 0x400
+#define S_GETCTIME 0x800
+
  #endif
 
  #endif
include/linux/syscalls.h +7 -7
index 008f04c..f3a993c 100644
@@ -225,17 +225,17 @@ asmlinkage long sys_lstat(char __user *f
  asmlinkage long sys_fstat(unsigned int fd,
  			struct __old_kernel_stat __user *statbuf);
  asmlinkage long sys_newstat(char __user *filename,
-				struct stat __user *statbuf);
+				struct stat __user *statbuf, unsigned int flags);
  asmlinkage long sys_newlstat(char __user *filename,
-				struct stat __user *statbuf);
-asmlinkage long sys_newfstat(unsigned int fd, struct stat __user *statbuf);
+				struct stat __user *statbuf, unsigned int flags);
+asmlinkage long sys_newfstat(unsigned int fd, struct stat __user *statbuf, unsigned int flags);
  asmlinkage long sys_ustat(unsigned dev, struct ustat __user *ubuf);
  #if BITS_PER_LONG == 32
  asmlinkage long sys_stat64(char __user *filename,
-				struct stat64 __user *statbuf);
-asmlinkage long sys_fstat64(unsigned long fd, struct stat64 __user *statbuf);
+				struct stat64 __user *statbuf, unsigned int flags);
+asmlinkage long sys_fstat64(unsigned long fd, struct stat64 __user *statbuf, unsigned int flags);
  asmlinkage long sys_lstat64(char __user *filename,
-				struct stat64 __user *statbuf);
+				struct stat64 __user *statbuf, unsigned int flags);
  asmlinkage long sys_truncate64(const char __user *path, loff_t length);
  asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length);
  #endif
@@ -568,7 +568,7 @@ asmlinkage long sys_openat(int dfd, cons
  asmlinkage long sys_newfstatat(int dfd, char __user *filename,
  			       struct stat __user *statbuf, int flag);
  asmlinkage long sys_fstatat64(int dfd, char __user *filename,
-			       struct stat64 __user *statbuf, int flag);
+			       struct stat64 __user *statbuf, int flag, unsigned int statflags);
  asmlinkage long sys_readlinkat(int dfd, const char __user *path, char __user *buf,
  			       int bufsiz);
  asmlinkage long compat_sys_futimesat(unsigned int dfd, char __user *filename,
init/do_mounts.h +2 -2
index e7f2e7f..e65b6fa 100644
@@ -24,7 +24,7 @@ #if BITS_PER_LONG == 32
  static inline u32 bstat(char *name)
  {
  	struct stat64 stat;
-	if (sys_stat64(name, &stat) != 0)
+	if (sys_stat64(name, &stat, 0) != 0)
  		return 0;
  	if (!S_ISBLK(stat.st_mode))
  		return 0;
@@ -36,7 +36,7 @@ #else
  static inline u32 bstat(char *name)
  {
  	struct stat stat;
-	if (sys_newstat(name, &stat) != 0)
+	if (sys_newstat(name, &stat, 0) != 0)
  		return 0;
  	if (!S_ISBLK(stat.st_mode))
  		return 0;
init/initramfs.c +1 -1
index d28c109..4b12040 100644
@@ -245,7 +245,7 @@ static void __init clean_path(char *path
  {
  	struct stat st;
 
-	if (!sys_newlstat(path, &st) && (st.st_mode^mode) & S_IFMT) {
+	if (!sys_newlstat(path, &st, 0) && (st.st_mode^mode) & S_IFMT) {
  		if (S_ISDIR(st.st_mode))
  			sys_rmdir(path);
  		else
#1 lore raw

On Sun, Aug 06, 2006 at 11:58:26AM -0400, Ameer Armaly wrote:
...

Quoted 2 lines

+asmlinkage long sys_newstat(char __user *filename, struct stat __user *statbuf, unsigned int flags)

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+        if(!flags) /*The want everything, so let's flip on all the flags.*/
+                --flags;

if (!flags)

	flags = ~0;

or even..

flags = flags ? flags : ~0;

Alternatively, you could define S_ALL, have it include all the flags and use that instead of ~0. This is probably the best option.

Josef "Jeff" Sipek.

Signature · 3 lines
You measure democracy by the freedom it gives its dissidents, not the
freedom it gives its assimilated conformists.
		- Abbie Hoffman
#2 lore raw
Quoted 1 line

flags = flags ? flags : ~0;

Here's a little known GNU C feature that makes it even easier to read:

flags = flags ?: ~0

#3 lore raw
Quoted 5 lines · Bryan Henderson
Quoted 1 line

flags = flags ? flags : ~0;

Here's a little known GNU C feature that makes it even easier to read:

flags = flags ?: ~0

I think the first one is easier to read. Probably since the second is "little-known" and requires a bit more thought to understand.

Signature · 2 lines
David Kleikamp
IBM Linux Technology Center
#4 lore raw
Quoted 6 lines · Dave Kleikamp
Quoted 5 lines · Bryan Henderson
Quoted 1 line

flags = flags ? flags : ~0;

Here's a little known GNU C feature that makes it even easier to read:

flags = flags ?: ~0

Bryan: Interesting, I didn't know that.

Quoted 2 lines

I think the first one is easier to read. Probably since the second is "little-known" and requires a bit more thought to understand.

Agreed.

Josef "Jeff" Sipek.

Signature · 1 line
#endif /* NO LIFE */
#5 lore raw
In reply to Ameer Armaly

fs/stat.c #endif · line 247

Quoted 11 lines · Ameer Armaly

-asmlinkage long sys_newstat(char __user *filename, struct stat __user *statbuf)
+asmlinkage long sys_newstat(char __user *filename, struct stat __user *statbuf, unsigned int flags)
{
struct kstat stat;

fs/stat.c +4 -1
-	int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
+	int error;
+        if(!flags) /*The want everything, so let's flip on all the flags.*/
+                --flags;
+	error = vfs_stat_fd(AT_FDCWD, filename, &stat, flags);

You can't assume that old programs calling sys_newstat will happen to have a 0 in the flags argument. Look at how we handled this for sys_mount (MS_MGC_VAL).

Or introduce new syscalls with flags, of course.