Make hostfs_setattr() support operations on closed files.
PATCH 1/1 Make hostfs_setattr() support operations on closed files.
This patch allows hostfs_setattr() to work on closed files by calling set_attr() (the userspace part) with the inode's fd.
Without this, applications that depend on doing attribute changes to closed files will fail.
It works by using the fd versions instead of the path ones (for example fchmod() instead of chmod(), fchown() instead of chown()) when an fd is available.
---
I hit this using hostfs as my root device and trying to do dpkg-reconfigure locales, which called locale-gen which did something like:
fd = open("f1.tmp");
write(fd, ...);
link("f1.tmp", "f1");
unlink("f1.tmp");
fchmod(fd, 0644);And fchmod failed with ENOENT.
I compiled this and tested with locale-gen and a testcase I wrote for this.
I was also worried about the semantics, for instance, the outcome of:
fd = open("f1.tmp");
write(fd, ...);
link("f1.tmp", "f1");
unlink("f1.tmp");
fd2 = open("f1.tmp");
fchmod(fd, 0644);In the host fchmod() changes f1 and not f1.tmp, but maybe the open() in the middle got hostfs confused. Luckily this is not the case and it works fine; but as I'm not familiar with hostfs there may be other issues.
fs/hostfs/hostfs.h | 4 +- fs/hostfs/hostfs_kern.c | 10 ++++- fs/hostfs/hostfs_user.c | 89 ++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 86 insertions(+), 17 deletions(-)
fs/hostfs/hostfs.h +2 -2
@@ -55,7 +55,7 @@ extern int stat_file(const char *path, unsigned long long *inode_out, int *mode_out, int *nlink_out, int *uid_out, int *gid_out, unsigned long long *size_out, struct timespec *atime_out, struct timespec *mtime_out, struct timespec *ctime_out, - int *blksize_out, unsigned long long *blocks_out); + int *blksize_out, unsigned long long *blocks_out, int fd);extern int access_file (char *path,int r,int w,int x);extern int open_file (char *path,int r,int w,int append);extern int file_type (const char *path,int *maj,int *min);
@@ -71,7 +71,7 @@ extern int lseek_file(int fd, long long offset, int whence);extern int fsync_file (int fd,int datasync);extern int file_create (char *name,int ur,int uw,int ux,int gr,int gw,int gx,int or,int ow,int ox); -extern int set_attr (const char *file,struct hostfs_iattr *attrs); +extern int set_attr (const char *file,struct hostfs_iattr *attrs,int fd);extern int make_symlink (const char *from,const char *to);extern int unlink_file (const char *file);extern int do_mkdir (const char *file,int mode);
fs/hostfs/hostfs_kern.c +8 -2
@@ -147,7 +147,7 @@ static int read_name(struct inode *ino, char *name) err =stat_file (name, &i_ino, &i_mode, &i_nlink, &ino->i_uid, &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime, - &ino->i_ctime, &i_blksize, &i_blocks); + &ino->i_ctime, &i_blksize, &i_blocks,-1 );if (err)return (err);
@@ -817,8 +817,14 @@ int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)int hostfs_setattr (struct dentry *dentry,struct iattr *attr) {struct hostfs_iattr attrs; +struct hostfs_inode_info *iinfo;char *name;int err; +int fd =-1 ; + + iinfo =HOSTFS_I (dentry->d_inode); +if (iinfo !=NULL ) + fd = iinfo->fd; err =inode_change_ok (dentry->d_inode, attr);if (err)
@@ -864,7 +870,7 @@ int hostfs_setattr(struct dentry *dentry, struct iattr *attr) } name =dentry_name (dentry,0 );if (name ==NULL )return (-ENOMEM ); - err =set_attr (name, &attrs); + err =set_attr (name, &attrs, fd);kfree (name);if (err)return (err);
fs/hostfs/hostfs_user.c +76 -13
146 lines not on this page · raw message
Signature · 12 lines
1.5.1.1 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/ _______________________________________________ User-mode-linux-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel
Quoted 6 lines · Alberto Bertogli
This patch allows
hostfs_setattr()to work on closed files by callingset_attr()(the userspace part) with the inode's fd.Without this, applications that depend on doing attribute changes to closed files will fail.
Quoted 7 lines
It works by using the fd versions instead of the path ones (for example
fchmod()instead ofchmod(),fchown()instead ofchown()) when an fd is available.Signed-off-by Alberto Bertogli <[email protected]>---
This makes sense and is useful - but:
1) when I read the examples, I understood you meant 'unlinked open files', not 'closed files'. Right?
The patch is of good quality; I've not seen any single bug, which is rare in such submission, but before merging, some little things should be fixed up (either by you or by Jeff).
2) if (...) return ...; on a single line is horrible style, should be corrected (I see sometimes the original code uses it, well it shouldn't).
Quoted 9 lines
I was also worried about the semantics, for instance, the outcome of:
fd = open("f1.tmp"); write(fd, ...); link("f1.tmp", "f1"); unlink("f1.tmp"); fd2 = open("f1.tmp"); fchmod(fd, 0644);
Quoted 3 lines
In the host
fchmod()changes f1 and not f1.tmp, but maybe theopen()in the middle got hostfs confused. Luckily this is not the case and it works fine; but as I'm not familiar with hostfs there may be other issues.
I guess the patch would make this work, right?
Quoted 7 lines
fs/hostfs/hostfs_kern.c +0
-0@@ -817,8 +817,14 @@ int hostfs_permission(struct inode *ino, int desired,struct nameidata *nd) int
hostfs_setattr(struct dentry *dentry, struct iattr *attr)
{
fs/hostfs/hostfs_kern.c int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd) · line 827
Quoted 9 lines
fs/hostfs/hostfs_kern.c +6
-0struct hostfs_iattr attrs; +struct hostfs_inode_info *iinfo;char *name;int err; +int fd =-1 ; + + iinfo =HOSTFS_I (dentry->d_inode); +if (iinfo !=NULL ) + fd = iinfo->fd;
iinfo cannot be NULL, so please remove that if. HOSTFS_I does not access a field of d_inode; rather, d_inode points to a field of iinfo, i.e. the vfs_inode field of struct hostfs_inode_info.
fs/hostfs/hostfs_kern.c int hostfs_setattr(struct dentry *dentry, struct iattr · line 876
Quoted 11 lines
fs/hostfs/hostfs_kern.c +1
-1err =inode_change_ok (dentry->d_inode, attr);if (err)@@ -864,7 +870,7 @@ int hostfs_setattr(struct dentry *dentry, struct iattr *attr) } name =dentry_name (dentry,0 );if (name ==NULL )return (-ENOMEM ); - err =set_attr (name, &attrs); + err =set_attr (name, &attrs, fd);kfree (name);if (err)return (err);
I do not like the below code. The different code is for setting buf; what follows that is common to all cases, and the conversion probably should be factored out into a conversion function for better clarity: verifying what the code does is difficult here.
Quoted 87 lines
fs/hostfs/hostfs_kern.c +49
-6ma =HOSTFS_ATTR_ATIME_SET |HOSTFS_ATTR_MTIME_SET ;if ((attrs->ia_valid & ma) == ma){ +struct timeval times[2 ]; + buf.actime = attrs->ia_atime.tv_sec; buf.modtime = attrs->ia_mtime.tv_sec; -if (utime (file, &buf) !=0 )return (-errno); + +/* atime */ + times[0 ].tv_sec = attrs->ia_atime.tv_sec; + times[0 ].tv_usec = attrs->ia_atime.tv_nsec *1000 ; + +/* mtime */ + times[1 ].tv_sec = attrs->ia_mtime.tv_sec; + times[1 ].tv_usec = attrs->ia_mtime.tv_nsec *1000 ; + +if (fd >=0 ){ +if (futimes (fd, times) !=0 ) +return (-errno); + }else if (utime (file, &buf) !=0 ){ +return (-errno); + } }else {struct timespec ts; +struct timeval times[2 ];if (attrs->ia_valid &HOSTFS_ATTR_ATIME_SET ){ err =stat_file (file,NULL ,NULL ,NULL ,NULL ,NULL , -NULL ,NULL , &ts,NULL ,NULL ,NULL ); +NULL ,NULL , &ts,NULL ,NULL ,NULL , fd);if (err !=0 )return (err); buf.actime = attrs->ia_atime.tv_sec; buf.modtime = ts.tv_sec; -if (utime (file, &buf) !=0 ) + +/* atime */ + times[0 ].tv_sec = attrs->ia_atime.tv_sec; + times[0 ].tv_usec = attrs->ia_atime.tv_nsec *1000 ; + +/* mtime */ + times[1 ].tv_sec = ts.tv_sec; + times[1 ].tv_usec = ts.tv_nsec *1000 ; + +if (fd >=0 ){ +if (futimes (fd, times) !=0 ) +return (-errno); + }else if (utime (file, &buf) !=0 ){return (-errno); + } }if (attrs->ia_valid &HOSTFS_ATTR_MTIME_SET ){ err =stat_file (file,NULL ,NULL ,NULL ,NULL ,NULL , -NULL , &ts,NULL ,NULL ,NULL ,NULL ); +NULL , &ts,NULL ,NULL ,NULL ,NULL , fd);if (err !=0 )return (err); buf.actime = ts.tv_sec; buf.modtime = attrs->ia_mtime.tv_sec; -if (utime (file, &buf) !=0 ) + +/* atime */ + times[0 ].tv_sec = ts.tv_sec; + times[0 ].tv_usec = ts.tv_nsec *1000 ; + +/* mtime */ + times[1 ].tv_sec = attrs->ia_mtime.tv_sec; + times[1 ].tv_usec = attrs->ia_mtime.tv_nsec *1000 ; + +if (fd >=0 ){ +if (futimes (fd, times) !=0 ) +return (-errno); + }else if (utime (file, &buf) !=0 ){return (-errno); + } } }if (attrs->ia_valid &HOSTFS_ATTR_CTIME ) ;if (attrs->ia_valid & (HOSTFS_ATTR_ATIME |HOSTFS_ATTR_MTIME )){ err =stat_file (file,NULL ,NULL ,NULL ,NULL ,NULL ,NULL , &attrs->ia_atime, &attrs->ia_mtime,NULL , -NULL ,NULL ); +NULL ,NULL , fd);if (err !=0 )return (err); }return (0 );
Signature · 13 lines
Inform me of my mistakes, so I can add them to my list! Paolo Giarrusso, aka Blaisorblade http://www.user-mode-linux.org/~blaisorblade ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/ _______________________________________________ User-mode-linux-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel
Quoted 12 lines · Blaisorblade
Quoted 11 lines · Alberto Bertogli
This patch allows
hostfs_setattr()to work on closed files by callingset_attr()(the userspace part) with the inode's fd.Without this, applications that depend on doing attribute changes to closed files will fail.
It works by using the fd versions instead of the path ones (for example
fchmod()instead ofchmod(),fchown()instead ofchown()) when an fd is available.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Alberto Bertogli <[email protected]>---
This makes sense and is useful - but:
1) when I read the examples, I understood you meant 'unlinked open files', not 'closed files'. Right?
Right, sorry about that, the terminology I used is completely misleading. I'll change the description.
Quoted 6 lines
The patch is of good quality; I've not seen any single bug, which is rare in such submission, but before merging, some little things should be fixed up (either by you or by Jeff).
2) if (...) return ...; on a single line is horrible style, should be corrected (I see sometimes the original code uses it, well it shouldn't).
I couldn't agree more. I did it that way because I tried to follow the coding style of the original code.
Quoted 5 lines
Quoted 3 lines
In the host
fchmod()changes f1 and not f1.tmp, but maybe theopen()in the middle got hostfs confused. Luckily this is not the case and it works fine; but as I'm not familiar with hostfs there may be other issues.I guess the patch would make this work, right?
Yes, that case works with the patch.
Quoted 12 lines
Quoted 12 lines
fs/hostfs/hostfs_kern.c +0
-0@@ -817,8 +817,14 @@ int hostfs_permission(struct inode *ino, int desired,struct nameidata *nd) int
hostfs_setattr(struct dentry *dentry, struct iattr *attr)
{fs/hostfs/hostfs_kern.c +2
-0struct hostfs_iattr attrs; +struct hostfs_inode_info *iinfo;char *name;int err; +int fd =-1 ;
Quoted 8 lines
fs/hostfs/hostfs_kern.c int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd) · line 827
Quoted 4 lines
fs/hostfs/hostfs_kern.c +4
-0+ + iinfo =HOSTFS_I (dentry->d_inode); +if (iinfo !=NULL ) + fd = iinfo->fd;iinfo cannot be NULL, so please remove that if.
HOSTFS_Idoes not access a field ofd_inode; rather,d_inodepoints to a field of iinfo, i.e. thevfs_inodefield of structhostfs_inode_info.
That's good to know =) I'll fix it.
Quoted 4 lines
I do not like the below code. The different code is for setting buf; what follows that is common to all cases, and the conversion probably should be factored out into a conversion function for better clarity: verifying what the code does is difficult here.
I agree. I tried to make the changes to look as much as possible to the original, but if you want me to reorganize that code a bit, I'll be glad to give it a try.
I'll address all these issues and post and updated patch (probably tomorrow).
Thanks a lot,
Alberto
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
User-mode-linux-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel
Quoted 3 lines · Alberto Bertogli
This patch allows
hostfs_setattr()to work on closed files by callingset_attr()(the userspace part) with the inode's fd.
[...]
Quoted 5 lines
fs/hostfs/hostfs.h | 4 +- fs/hostfs/hostfs_kern.c | 10 ++++- fs/hostfs/hostfs_user.c | 89 ++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 86 insertions(+), 17 deletions(-)
Here (as an inline attachment) is the updated patch that (hopefully) address Blaisorblade's comments.
Thanks,
Alberto
From af922e915c05e32f0392b234e047f9aa9857fb80 Mon Sep 17 00:00:00 2001
From: Alberto Bertogli <[email protected]>
Date: Thu, 26 Apr 2007 01:57:07 -0300
Subject: [PATCH] Make hostfs_setattr() support operations on unlinked open files.
This patch allows hostfs_setattr() to work on unlinked open files by calling set_attr() (the userspace part) with the inode's fd.
Without this, applications that depend on doing attribute changes to unlinked open files will fail.
It works by using the fd versions instead of the path ones (for example fchmod() instead of chmod(), fchown() instead of chown()) when an fd is available.
--- fs/hostfs/hostfs.h | 4 +- fs/hostfs/hostfs_kern.c | 6 ++- fs/hostfs/hostfs_user.c | 108 +++++++++++++++++++++++++++++----------------- 3 files changed, 74 insertions(+), 44 deletions(-)
fs/hostfs/hostfs.h +2 -2
@@ -55,7 +55,7 @@ extern int stat_file(const char *path, unsigned long long *inode_out, int *mode_out, int *nlink_out, int *uid_out, int *gid_out, unsigned long long *size_out, struct timespec *atime_out, struct timespec *mtime_out, struct timespec *ctime_out, - int *blksize_out, unsigned long long *blocks_out); + int *blksize_out, unsigned long long *blocks_out, int fd);extern int access_file (char *path,int r,int w,int x);extern int open_file (char *path,int r,int w,int append);extern int file_type (const char *path,int *maj,int *min);
@@ -71,7 +71,7 @@ extern int lseek_file(int fd, long long offset, int whence);extern int fsync_file (int fd,int datasync);extern int file_create (char *name,int ur,int uw,int ux,int gr,int gw,int gx,int or,int ow,int ox); -extern int set_attr (const char *file,struct hostfs_iattr *attrs); +extern int set_attr (const char *file,struct hostfs_iattr *attrs,int fd);extern int make_symlink (const char *from,const char *to);extern int unlink_file (const char *file);extern int do_mkdir (const char *file,int mode);
fs/hostfs/hostfs_kern.c +4 -2
@@ -147,7 +147,7 @@ static int read_name(struct inode *ino, char *name) err =stat_file (name, &i_ino, &i_mode, &i_nlink, &ino->i_uid, &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime, - &ino->i_ctime, &i_blksize, &i_blocks); + &ino->i_ctime, &i_blksize, &i_blocks,-1 );if (err)return (err);
@@ -820,6 +820,8 @@ int hostfs_setattr(struct dentry *dentry, struct iattr *attr)char *name;int err; +int fd =HOSTFS_I (dentry->d_inode)->fd; + err =inode_change_ok (dentry->d_inode, attr);if (err)return err;
@@ -864,7 +866,7 @@ int hostfs_setattr(struct dentry *dentry, struct iattr *attr) } name =dentry_name (dentry,0 );if (name ==NULL )return (-ENOMEM ); - err =set_attr (name, &attrs); + err =set_attr (name, &attrs, fd);kfree (name);if (err)return (err);
fs/hostfs/hostfs_user.c +68 -40
138 lines not on this page · raw message
Signature · 1 line
1.5.1.1
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
User-mode-linux-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel