]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
NFS: invalidate file size when taking a lock.
authorNeilBrown <neilb@suse.com>
Mon, 24 Jul 2017 03:18:50 +0000 (13:18 +1000)
committerAnna Schumaker <Anna.Schumaker@Netapp.com>
Thu, 27 Jul 2017 15:22:42 +0000 (11:22 -0400)
Prior to commit ca0daa277aca ("NFS: Cache aggressively when file is open
for writing"), NFS would revalidate, or invalidate, the file size when
taking a lock.  Since that commit it only invalidates the file content.

If the file size is changed on the server while wait for the lock, the
client will have an incorrect understanding of the file size and could
corrupt data.  This particularly happens when writing beyond the
(supposed) end of file and can be easily be demonstrated with
posix_fallocate().

If an application opens an empty file, waits for a write lock, and then
calls posix_fallocate(), glibc will determine that the underlying
filesystem doesn't support fallocate (assuming version 4.1 or earlier)
and will write out a '0' byte at the end of each 4K page in the region
being fallocated that is after the end of the file.
NFS will (usually) detect that these writes are beyond EOF and will
expand them to cover the whole page, and then will merge the pages.
Consequently, NFS will write out large blocks of zeroes beyond where it
thought EOF was.  If EOF had moved, the pre-existing part of the file
will be over-written.  Locking should have protected against this,
but it doesn't.

This patch restores the use of nfs_zap_caches() which invalidated the
cached attributes.  When posix_fallocate() asks for the file size, the
request will go to the server and get a correct answer.

cc: stable@vger.kernel.org (v4.8+)
Fixes: ca0daa277aca ("NFS: Cache aggressively when file is open for writing")
Signed-off-by: NeilBrown <neilb@suse.com>
Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
fs/nfs/file.c

index 5713eb32a45ea20c1de50f2ee28f4ddcae468f67..d264363559db6e7fafd789e26e372ee5766ac05e 100644 (file)
@@ -750,7 +750,7 @@ do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
         */
        nfs_sync_mapping(filp->f_mapping);
        if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
-               nfs_zap_mapping(inode, filp->f_mapping);
+               nfs_zap_caches(inode);
 out:
        return status;
 }