Skip to contentSkip to conversation

prepare_write problem for small write when page not uptodate

DISCUSSION Steve French 2 participants 2 messages Apr 29, 2004 – Apr 30, 2004 VFS

lore
Share & tools
lore
#0 lore raw

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?

#1 lore raw
Quoted 15 lines · Steve French

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);
Quoted 5 lines

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.

Don't use simple_prepare_write! The simple_* routines are for file systems like ramfs that don't have backing store. The premise with these functions is that if it's not cached, it's zero.

Shaggy

Signature · 2 lines
David Kleikamp
IBM Linux Technology Center