From: Andreas Dilger Subject: Re: fs/locks.c BKL removal Date: Mon, 13 May 2002 17:18:21 -0600 Sender: linux-fsdevel-owner@vger.kernel.org Message-ID: <20020513231821.GP12975@turbolinux.com> References: <3CDC4037.8040104@us.ibm.com> <20020511204551.M32414@parcelfarce.linux.theplanet.co.uk> <20020513171729.4165.qmail@thales.mathematik.uni-ulm.de> <20020513204347.C10287@parcelfarce.linux.theplanet.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Christian Ehrhardt , linux-fsdevel@vger.kernel.org Return-path: To: Matthew Wilcox Content-Disposition: inline In-Reply-To: <20020513204347.C10287@parcelfarce.linux.theplanet.co.uk> List-Id: linux-fsdevel.vger.kernel.org On May 13, 2002 20:43 +0100, Matthew Wilcox wrote: > Basically, this is a race. Here's the code (in sys_write()): > > struct inode *inode = file->f_dentry->d_inode; > ret = locks_verify_area(FLOCK_VERIFY_WRITE, inode, file, > file->f_pos, count); > if (!ret) { > ssize_t (*write)(struct file *, const char *, size_t, loff_t *); > ret = -EINVAL; > if (file->f_op && (write = file->f_op->write) != NULL) > ret = write(file, buf, count, &file->f_pos); > } > > Any time between calling locks_verify_area and the filesystem's write() > method, another task which shares the struct file may modify f_pos. This > race could be fixed by doing something like: > > struct inode *inode = file->f_dentry->d_inode; > loff_t start = file->f_pos; > ret = locks_verify_area(FLOCK_VERIFY_WRITE, inode, file, > start, count); > if (!ret) { > ssize_t (*write)(struct file *, const char *, size_t, loff_t *); ret = -EINVAL; > if (file->f_op && (write = file->f_op->write) != NULL) > ret = write(file, buf, count, &start); > } > if (!ret) { > file->f_pos = start; > } > > Would this cause problems with programs that share file descriptors and > expect that the second seek in your example to not be overridden by the > result of the first write? Two things here: 1) You probably need to set file->f_pos unconditionally after calling write() because it is possible for a write to partially complete and you want the file descriptor left at the end of the completed part. 2) If you have two threads writing to the same descriptor without any locking on their part, they can't expect _anything_ w.r.t. the position of the file descriptor, or you wouldn't have this problem in the first place. Hence, no need to worry about the result. Cheers, Andreas -- Andreas Dilger http://www-mddsp.enel.ucalgary.ca/People/adilger/ http://sourceforge.net/projects/ext2resize/