Multiple file->f_pos/nonseekable_open/llseek fixes (reposy)
Subsystems (12)
cover Multiple file->f_pos/nonseekable_open/llseek fixes (reposy)
This is a repost of a patch series I already posted in November 2009. This time I left out the patch that removes the BKL from default_llseek() since this should go through Frederic's tree. This series mostly consists of fixing direct modifications of file->f_pos and some additional cases where nonseekable_open() can be used. The rest of this series is unrelated to but a preparation for the BKL removal from default_llseek().
Thanks,
Jan
Jan Blunck (12):
osst: Update ppos instead of using file->f_pos flash_read should update ppos instead of file->f_pos eeprom_read()/eeprom_write() should update ppos instead of
file->f_pos
frv: remove "struct file *" argument from sysctl ->proc_handler
mISDN: Remove unnecessary test on f_pos
zcrypt: Use nonseekable_open()
rtc-m41t80: Use nonseekable_open()
Introduce noop_llseek()
osst: Use noop_llseek() instead of default_llseek()
st: Use noop_llseek() instead of default_llseek()
Do not fallback to default_llseek() when readdir() uses BKL Documentation/filesystems/Locking: Update documentation on llseek()
wrt BKL
Documentation/filesystems/Locking | 5 +++-- arch/cris/arch-v10/drivers/eeprom.c | 34 +++++++++++++--------------------- arch/frv/kernel/sysctl.c | 18 ++++++++++-------- drivers/isdn/mISDN/timerdev.c | 2 -- drivers/rtc/rtc-m41t80.c | 6 +----- drivers/s390/crypto/zcrypt_api.c | 2 +- drivers/sbus/char/flash.c | 6 +++--- drivers/scsi/osst.c | 9 +++++---- drivers/scsi/st.c | 1 + fs/autofs/root.c | 1 + fs/freevxfs/vxfs_lookup.c | 2 ++ fs/isofs/dir.c | 1 + fs/ncpfs/dir.c | 1 + fs/qnx4/dir.c | 1 + fs/read_write.c | 17 +++++++++++++++++ fs/reiserfs/dir.c | 1 + fs/smbfs/dir.c | 1 + fs/udf/dir.c | 1 + include/linux/fs.h | 1 + 19 files changed, 64 insertions(+), 46 deletions(-)
PATCH 1/12 osst: Update ppos instead of using file->f_pos
osst_read()/osst_write() modify file->f_pos directly instead of the ppos given to them. The VFS later updates the file->f_pos and overwrites it with the value of ppos.
--- drivers/scsi/osst.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
drivers/scsi/osst.c +4 -4
@@ -3586,7 +3586,7 @@ if (SRpnt) printk(KERN_ERR "%s:A: Not supposed to have SRpnt at line %d\n", nameif (i == (-ENOSPC )) { transfer = STp->buffer->writing;/* FIXME -- check this logic */ if (transfer <= do_count) { - filp->f_pos += do_count - transfer; + *ppos += do_count - transfer; count -= do_count - transfer;if (STps->drv_block >=0 ) { STps->drv_block += (do_count - transfer) / STp->block_size;
@@ -3624,7 +3624,7 @@ if (SRpnt) printk(KERN_ERR "%s:A: Not supposed to have SRpnt at line %d\n", name goto out; } - filp->f_pos += do_count; + *ppos += do_count; b_point += do_count; count -= do_count;if (STps->drv_block >=0 ) {
@@ -3646,7 +3646,7 @@ if (SRpnt) printk(KERN_ERR "%s:A: Not supposed to have SRpnt at line %d\n", nameif (STps->drv_block >=0 ) { STps->drv_block += blks; } - filp->f_pos += count; + *ppos += count; count =0 ; }
@@ -3822,7 +3822,7 @@ static ssize_t osst_read(struct file * filp, char __user * buf, size_t count, lo } STp->logical_blk_num += transfer / STp->block_size; STps->drv_block += transfer / STp->block_size; - filp->f_pos += transfer; + *ppos += transfer; buf += transfer; total += transfer; }
Signature · 1 line
1.6.4.2
PATCH 2/12 flash_read should update ppos instead of file->f_pos
In reply to Jan Blunckflash_read() updates file->f_pos directly instead of the ppos given. The VFS later updates the file->f_pos and overwrites it with the unchanged value of ppos.
--- drivers/sbus/char/flash.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/sbus/char/flash.c +3 -3
@@ -106,9 +106,9 @@ static ssize_t flash_read(struct file * file, char __user * buf, size_t count, loff_t *ppos) { - unsigned long p = file->f_pos; + loff_t p = *ppos;int i; - +if (count > flash.read_size - p) count = flash.read_size - p;
@@ -119,7 +119,7 @@ flash_read(struct file * file, char __user * buf, buf++; } - file->f_pos += count; + *ppos += count;return count; }
Signature · 1 line
1.6.4.2
PATCH 3/12 eeprom_read()/eeprom_write() should update ppos instead of file->f_pos
In reply to Jan Blunckeeprom_read()/eeprom_write() modify file->f_pos directly instead of the ppos given. The VFS later updates the file->f_pos and overwrites it with the unchanged value of ppos. Since nothing touches the struct file in eeprom_read()/eeprom_write() now, the on stack allocated struct file in eeprom_read_buf()/eeprom_write_buf() can be removed as well.
--- arch/cris/arch-v10/drivers/eeprom.c | 34 +++++++++++++--------------------- 1 files changed, 13 insertions(+), 21 deletions(-)
arch/cris/arch-v10/drivers/eeprom.c +13 -21
@@ -439,20 +439,18 @@ static loff_t eeprom_lseek(struct file * file, loff_t offset, int orig)static int eeprom_read_buf (loff_t addr,char * buf,int count) { -struct file f; - - f.f_pos = addr; -return eeprom_read (&f, buf, count, &addr); +return eeprom_read (NULL , buf, count, &addr); }/* Reads data from eeprom. */ -static ssize_t eeprom_read (struct file * file,char * buf,size_t count,loff_t *off) +static ssize_t eeprom_read (struct file * file,char * buf,size_t count, +loff_t *ppos) {int read=0 ; -unsigned long p = file->f_pos; +loff_t p = *ppos;unsigned char page;
@@ -498,11 +496,9 @@ static ssize_t eeprom_read(struct file * file, char * buf, size_t count, loff_t/* go on with the actual read */ read =read_from_eeprom ( buf, count); - +if (read >0 ) - { - file->f_pos += read; - } + *ppos += read; eeprom.busy--;wake_up_interruptible (&eeprom.wait_q);
@@ -513,18 +509,14 @@ static ssize_t eeprom_read(struct file * file, char * buf, size_t count, loff_tstatic int eeprom_write_buf (loff_t addr,const char * buf,int count) { -struct file f; - - f.f_pos = addr; - -return eeprom_write (&f, buf, count, &addr); +return eeprom_write (NULL , buf, count, &addr); }/* Writes data to eeprom. */ static ssize_t eeprom_write (struct file * file,const char * buf,size_t count, -loff_t *off) +loff_t *ppos) {int i, written, restart=1 ;unsigned long p;
@@ -543,9 +535,9 @@ static ssize_t eeprom_write(struct file * file, const char * buf, size_t count, { restart =0 ; written =0 ; - p = file->f_pos; - - + p = *ppos; + +while ( (written < count) && (p < eeprom.size)) {/* address the eeprom */
@@ -671,10 +663,10 @@ static ssize_t eeprom_write(struct file * file, const char * buf, size_t count, eeprom.busy--;wake_up_interruptible (&eeprom.wait_q); -if (written ==0 && file->f_pos >= eeprom.size){ +if (written ==0 && *ppos >= eeprom.size){return -ENOSPC ; } - file->f_pos += written; + *ppos += written;return written; }
Signature · 1 line
1.6.4.2
PATCH 4/12 frv: remove "struct file *" argument from sysctl ->proc_handler
In reply to Jan BlunckSeems that Alexey Dobriyan missed this usage of the file argument when removing it from ->proc_handler in commit 8d65af789f3e2cf4cfbdbf71a0f7a61ebcd41d38.
--- arch/frv/kernel/sysctl.c | 18 ++++++++++-------- 1 files changed, 10 insertions(+), 8 deletions(-)
arch/frv/kernel/sysctl.c +10 -8
@@ -47,8 +47,9 @@ static void frv_change_dcache_mode(unsigned long newmode)/* * handle requests to dynamically switch the write caching mode delivered by /proc */ -static int procctl_frv_cachemode (ctl_table *table,int write,struct file *filp, -void __user *buffer,size_t *lenp,loff_t *ppos) +static int procctl_frv_cachemode (ctl_table *table,int write, +void __user *buffer,size_t *lenp, +loff_t *ppos) {unsigned long hsr0;char buff[8 ];
@@ -85,7 +86,7 @@ static int procctl_frv_cachemode(ctl_table *table, int write, struct file *filp, }/* read the state */ -if (filp->f_pos >0 ) { +if (*ppos >0 ) { *lenp =0 ;return 0 ; }
@@ -111,7 +112,7 @@ static int procctl_frv_cachemode(ctl_table *table, int write, struct file *filp,return -EFAULT ; *lenp = len; - filp->f_pos = len; + *ppos = len;return 0 ; }/* end procctl_frv_cachemode() */
@@ -121,8 +122,9 @@ static int procctl_frv_cachemode(ctl_table *table, int write, struct file *filp,* permit the mm_struct the nominated process is using have its MMU context ID pinned */ #ifdef CONFIG_MMU -static int procctl_frv_pin_cxnr (ctl_table *table,int write,struct file *filp, -void __user *buffer,size_t *lenp,loff_t *ppos) +static int procctl_frv_pin_cxnr (ctl_table *table,int write, +void __user *buffer,size_t *lenp, +loff_t *ppos) {pid_t pid;char buff[16 ], *p;
@@ -151,7 +153,7 @@ static int procctl_frv_pin_cxnr(ctl_table *table, int write, struct file *filp, }/* read the currently pinned CXN */ -if (filp->f_pos >0 ) { +if (*ppos >0 ) { *lenp =0 ;return 0 ; }
@@ -164,7 +166,7 @@ static int procctl_frv_pin_cxnr(ctl_table *table, int write, struct file *filp,return -EFAULT ; *lenp = len; - filp->f_pos = len; + *ppos = len;return 0 ; }/* end procctl_frv_pin_cxnr() */
Signature · 1 line
1.6.4.2
Re: [PATCH 04/12] frv: remove "struct file *" argument from sysctl ->proc_handler
Quoted 2 lines · Jan Blunck
Seems that Alexey Dobriyan missed this usage of the file argument when removing it from ->proc_handler in commit
8d65af789f3e2cf4cfbdbf71a0f7a61ebcd41d38.
It wasn't missed, it was deliberately left out because I didn't understand what's going on there.
I mean, sysctl handler touching ->f_pos, come on!
What's going on?
Quoted 11 lines
arch/frv/kernel/sysctl.c +1
-1@@ -85,7 +86,7 @@ static int procctl_frv_cachemode(ctl_table *table, int write, struct file *filp, } /* read the state */ - if (filp->f_pos > 0) { + if (*ppos > 0) { *lenp = 0; return 0; }
Re: [PATCH 04/12] frv: remove "struct file *" argument from sysctl ->proc_handler
Quoted 11 lines · Alexey Dobriyan
Quoted 2 lines · Jan Blunck
Seems that Alexey Dobriyan missed this usage of the file argument when removing it from ->proc_handler in commit
8d65af789f3e2cf4cfbdbf71a0f7a61ebcd41d38.It wasn't missed, it was deliberately left out because I didn't understand what's going on there.
I mean, sysctl handler touching ->f_pos, come on!
What's going on?
It is just a normal /proc/sys file. You can write a string to it to change the some caching mode somehow.
Jan
Quoted 11 lines
arch/frv/kernel/sysctl.c static int procctl_frv_cachemode(ctl_table *table, int write, struct file *filp, · line 92
Quoted 11 lines
arch/frv/kernel/sysctl.c +1
-1@@ -85,7 +86,7 @@ static int procctl_frv_cachemode(ctl_table *table, int write, struct file *filp, } /* read the state */ - if (filp->f_pos > 0) { + if (*ppos > 0) { *lenp = 0; return 0; }
Regards,
Jan
Signature · 1 line
Jan Blunck <[email protected]>
PATCH 5/12 mISDN: Remove unnecessary test on f_pos
In reply to Jan BlunckThis test is not doing anything since it is always false if the mISDN_read() is called from vfs_read(). Besides that the driver uses nonseekable_open() and is not using off or file->f_pos anywhere.
--- drivers/isdn/mISDN/timerdev.c | 2 -- 1 files changed, 0 insertions(+), 2 deletions(-)
drivers/isdn/mISDN/timerdev.c +0 -2
@@ -96,8 +96,6 @@ mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)if (*debug &DEBUG_TIMER )printk (KERN_DEBUG "%s(%p, %p, %d, %p)\n" , __func__, filep, buf, (int )count, off); -if (*off != filep->f_pos) -return -ESPIPE ;if (list_empty (&dev->expired) && (dev->work ==0 )) {if (filep->f_flags &O_NONBLOCK )
Signature · 1 line
1.6.4.2
PATCH 6/12 zcrypt: Use nonseekable_open()
In reply to Jan Blunckzcrypt doesn't even allow to read/write the files it opens. So seeking on it shouldn't fall back to defaults either but should be disallowed as well.
--- drivers/s390/crypto/zcrypt_api.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/s390/crypto/zcrypt_api.c +1 -1
@@ -301,7 +301,7 @@ static ssize_t zcrypt_write(struct file *filp, const char __user *buf,static int zcrypt_open (struct inode *inode,struct file *filp) {atomic_inc (&zcrypt_open_count); -return 0 ; +return nonseekable_open (inode, filp); }/**
Signature · 1 line
1.6.4.2
PATCH 7/12 rtc-m41t80: Use nonseekable_open()
In reply to Jan BlunckUse nonseekable_open() for this since seeking is not supported anyway.
--- drivers/rtc/rtc-m41t80.c | 6 +----- 1 files changed, 1 insertions(+), 5 deletions(-)
drivers/rtc/rtc-m41t80.c +1 -5
@@ -595,10 +595,6 @@ static void wdt_disable(void)static ssize_t wdt_write (struct file *file,const char __user *buf,size_t count,loff_t *ppos) { -/* Can't seek (pwrite) on this device -if (ppos != &file->f_pos) -return -ESPIPE; -*/ if (count) {wdt_ping ();return 1 ;
@@ -695,7 +691,7 @@ static int wdt_open(struct inode *inode, struct file *file)*/ wdt_is_open =1 ;unlock_kernel (); -return 0 ; +return nonseekable_open (inode, file); }return -ENODEV ; }
Signature · 1 line
1.6.4.2
PATCH 8/12 Introduce noop_llseek()
In reply to Jan BlunckThis is an implementation of ->llseek useable for the rare special case when userspace expects the seek to succeed but the (device) file is actually not able to perform the seek. In this case you use noop_llseek() instead of falling back to the default implementation of ->llseek.
--- fs/read_write.c | 17 +++++++++++++++++ include/linux/fs.h | 1 + 2 files changed, 18 insertions(+), 0 deletions(-)
fs/read_write.c +17 -0
@@ -97,6 +97,23 @@ loff_t generic_file_llseek(struct file *file, loff_t offset, int origin) }EXPORT_SYMBOL (generic_file_llseek); +/** +* noop_llseek - No Operation Performed llseek implementation +* @file: file structure to seek on +* @offset: file offset to seek to +* @origin: type of seek +* +* This is an implementation of ->llseek useable for the rare special case when +* userspace expects the seek to succeed but the (device) file is actually not +* able to perform the seek. In this case you use noop_llseek() instead of +* falling back to the default implementation of ->llseek. +*/ +loff_t noop_llseek (struct file *file,loff_t offset,int origin) +{ +return file->f_pos; +} +EXPORT_SYMBOL (noop_llseek); +loff_t no_llseek (struct file *file,loff_t offset,int origin) {return -ESPIPE ;
include/linux/fs.h +1 -0
@@ -2227,6 +2227,7 @@ extern long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,extern void file_ra_state_init (struct file_ra_state *ra,struct address_space *mapping); +extern loff_t noop_llseek (struct file *file,loff_t offset,int origin);extern loff_t no_llseek (struct file *file,loff_t offset,int origin);extern loff_t generic_file_llseek (struct file *file,loff_t offset,int origin); extern loff_t generic_file_llseek_unlocked(struct file *file, loff_t offset,
Signature · 1 line
1.6.4.2
PATCH 9/12 osst: Use noop_llseek() instead of default_llseek()
In reply to Jan Blunck__os_scsi_tape_open() suggests that llseek() doesn't work:
"We really want to do nonseekable_open(inode, filp); here, but some versions of tar incorrectly call lseek on tapes and bail out if that fails. So we disallow pread() and pwrite(), but permit lseeks."
Instead of using the fallback default_llseek() the driver should use noop_llseek() which leaves the file->f_pos untouched but succeeds.
--- drivers/scsi/osst.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
drivers/scsi/osst.c +1 -0
@@ -5619,6 +5619,7 @@ static const struct file_operations osst_fops = { .open = os_scsi_tape_open, .flush = os_scsi_tape_flush, .release = os_scsi_tape_close, + .llseek = noop_llseek, };static int osst_supports (struct scsi_device * SDp)
Signature · 1 line
1.6.4.2
PATCH 10/12 st: Use noop_llseek() instead of default_llseek()
In reply to Jan Blunckst_open() suggests that llseek() doesn't work:
"We really want to do nonseekable_open(inode, filp); here, but some versions of tar incorrectly call lseek on tapes and bail out if that fails. So we disallow pread() and pwrite(), but permit lseeks."
Instead of using the fallback default_llseek() the driver should use noop_llseek() which leaves the file->f_pos untouched but succeeds.
--- drivers/scsi/st.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
drivers/scsi/st.c +1 -0
@@ -3961,6 +3961,7 @@ static const struct file_operations st_fops = .open = st_open, .flush = st_flush, .release = st_release, + .llseek = noop_llseek, };static int st_probe (struct device *dev)
Signature · 1 line
1.6.4.2
PATCH 11/12 Do not fallback to default_llseek() when readdir() uses BKL
In reply to Jan BlunckDo not use the fallback default_llseek() if the readdir operation of the filesystem still uses the big kernel lock. Since llseek() modifies file->f_pos of the directory directly it may need locking to not confuse readdir which usually uses file->f_pos directly as well. Since the special characteristics of the BKL (unlocked on schedule) are not necessary in this case, the inode mutex can be used for locking as provided by generic_file_llseek(). This is only possible since all filesystems, except reiserfs, either use a directory as a flat file or with disk address offsets. Reiserfs on the other hand uses a 32bit hash off the filename as the offset so generic_file_llseek() can get used as well since the hash is always smaller than sb->s_maxbytes (= (512 << 32) - blocksize).
--- fs/autofs/root.c | 1 + fs/freevxfs/vxfs_lookup.c | 2 ++ fs/isofs/dir.c | 1 + fs/ncpfs/dir.c | 1 + fs/qnx4/dir.c | 1 + fs/reiserfs/dir.c | 1 + fs/smbfs/dir.c | 1 + fs/udf/dir.c | 1 + 8 files changed, 9 insertions(+), 0 deletions(-)
fs/autofs/root.c +1 -0
@@ -30,6 +30,7 @@ const struct file_operations autofs_root_operations = { .read = generic_read_dir, .readdir = autofs_root_readdir, .ioctl = autofs_root_ioctl, + .llseek = generic_file_llseek, };const struct inode_operations autofs_root_inode_operations = {
fs/freevxfs/vxfs_lookup.c +2 -0
@@ -57,6 +57,8 @@ const struct inode_operations vxfs_dir_inode_ops = { };const struct file_operations vxfs_dir_operations = { + .llseek = generic_file_llseek, + .read = generic_read_dir, .readdir = vxfs_readdir, };
fs/isofs/dir.c +1 -0
@@ -271,6 +271,7 @@ static int isofs_readdir(struct file *filp,const struct file_operations isofs_dir_operations = { + .llseek = generic_file_llseek, .read = generic_read_dir, .readdir = isofs_readdir, };
fs/ncpfs/dir.c +1 -0
@@ -50,6 +50,7 @@ extern int ncp_symlink(struct inode *, struct dentry *, const char *);const struct file_operations ncp_dir_operations = { + .llseek = generic_file_llseek, .read = generic_read_dir, .readdir = ncp_readdir, .ioctl = ncp_ioctl,
fs/qnx4/dir.c +1 -0
@@ -77,6 +77,7 @@ out:const struct file_operations qnx4_dir_operations = { + .llseek = generic_file_llseek, .read = generic_read_dir, .readdir = qnx4_readdir, .fsync = simple_fsync,
fs/reiserfs/dir.c +1 -0
@@ -17,6 +17,7 @@ static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry, int datasync);const struct file_operations reiserfs_dir_operations = { + .llseek = generic_file_llseek, .read = generic_read_dir, .readdir = reiserfs_readdir, .fsync = reiserfs_dir_fsync,
fs/smbfs/dir.c +1 -0
@@ -37,6 +37,7 @@ static int smb_link(struct dentry *, struct inode *, struct dentry *);const struct file_operations smb_dir_operations = { + .llseek = generic_file_llseek, .read = generic_read_dir, .readdir = smb_readdir, .ioctl = smb_ioctl,
fs/udf/dir.c +1 -0
@@ -207,6 +207,7 @@ static int udf_readdir(struct file *filp, void *dirent, filldir_t filldir)/* readdir and lookup functions */ const struct file_operations udf_dir_operations = { + .llseek = generic_file_llseek, .read = generic_read_dir, .readdir = udf_readdir, .ioctl = udf_ioctl,
Signature · 1 line
1.6.4.2
PATCH 12/12 Documentation/filesystems/Locking: Update documentation on llseek() wrt BKL
In reply to Jan BlunckThe inode's i_size is not protected by the big kernel lock. Therefore it does not make sense to recommend taking the BKL in filesystems llseek operations. Instead it should use the inode's mutex or use just use i_size_read() instead. Add a note that this is not protecting file->f_pos.
--- Documentation/filesystems/Locking | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)
Documentation/filesystems/Locking +3 -2
@@ -429,8 +429,9 @@ check_flags: no implementations. If your fs is not using generic_file_llseek, you need to acquire and release the appropriate locks in your ->llseek(). For many filesystems, it is probably safe to acquire the inode -semaphore. Note some filesystems (i.e. remote ones) provide no -protection for i_size so you will need to use the BKL. +mutex or just to use i_size_read() instead. +Note: this does not protect the file->f_pos against concurrent modifications +since this is something the userspace has to take care about. Note: ext2_release() was *the* source of contention on fs-intensive loads and dropping BKL on ->release() helps to get rid of that (we still
Signature · 1 line
1.6.4.2