From: Steve French Subject: prepare_write problem for small write when page not uptodate Date: Wed, 28 Apr 2004 23:02:05 -0500 Sender: linux-fsdevel-owner@vger.kernel.org Message-ID: <40907E3D.3040604@austin.rr.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Return-path: Received: from ms-smtp-04.texas.rr.com ([24.93.47.43]:40674 "EHLO ms-smtp-04.texas.rr.com") by vger.kernel.org with ESMTP id S263315AbUD2ECJ (ORCPT ); Thu, 29 Apr 2004 00:02:09 -0400 To: linux-fsdevel@vger.kernel.org List-Id: linux-fsdevel.vger.kernel.org I was able to isolate a case in which data at the beginning of the 1st page of a file is overwritten with zero when a series of writes begins midpage. The problem is that simple_prepare_write zeroes the beginning of a page that has been invalidated even if there is good data that could be read into that. See below (from fs/libfs.c): 319 int simple_prepare_write(struct file *file, struct page *page, 320 unsigned from, unsigned to) 321 { 322 if (!PageUptodate(page)) { 323 if (to - from != PAGE_CACHE_SIZE) { 324 void *kaddr = kmap_atomic(page, KM_USER0); 325 memset(kaddr, 0, from); 326 memset(kaddr + to, 0, PAGE_CACHE_SIZE - to); At one point I had stopped using simple_prepare_write in cifs and like NFS, checked for pending writes that would overlap, but it looks like that could not be the case with current code since writepage was synchronous in the cifs case and in any case had the same problem with the partial page zeroing logic as simple_prepare_write.. The sequence that showed this problem was the following: create file write 128 bytes at offset zero flush close file open file - which causes cifs to invalidate cache (since we could not be certain if file had changed in this particular case) filemap_fdatawrite filemap_fdatawait invalidate_remote_inode (which basically just calls invalidate_mapping_pages to do the work - which presumably caused the page to be marked not uptodate) write 128 bytes at offset 128 prepare_write (which zeroed 0 to 127 and 256 to PAGE_CACHE_SIZE) and then eventually writepage (which will write a 1/2 corrupt 256 bytes which incorrectly includes zeroes in 0 to 127, to the server) Seems like invalidate_remote_inode (eventually calling invalidate_complete_page) does not always actually free the page (perhaps if there was an mmap on it?), in this case just marks it not up to date so the first write to the page in this case is not preceeded by a readpage as one would expect. Is it the responsibility of the filesystem's prepare_write code to reread in a page on a partial page write if page not uptodate? Or is there a way to call something after invalidate_remote_inode to force a reread of the page before the next prepare_write?