linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* truncate implementation
@ 2006-01-17  8:18 Jan Koss
  2006-01-17  8:31 ` Arjan van de Ven
  0 siblings, 1 reply; 5+ messages in thread
From: Jan Koss @ 2006-01-17  8:18 UTC (permalink / raw)
  To: kernelnewbies; +Cc: linux-fsdevel

Hello.

I have question about "truncate" function implementation.

Let's suppose that we have file with 8K size.

One process (let's call it 'A') open file 'X' for read and write,
and mmap'ed it to memory with "MAP_SHARED" flag. After
that it read content of file, so all data of file go to "page cache".
And after that 'A' wait something.
At this moment another process (let's call it 'B')
call "truncate" function, and truncate 'X' file to 4K length.
After that process 'C' create file 'Y' and because of
second block of file 'X' now is free, kernel give it to file 'Y'.
And at last, process 'A' write to second page of file 'X' and call
'munmap' and 'close'. And 'C' also close file 'Y'.

Hence instead of writing to file 'X' 'A' will write to 'Y'.

In short
process 'A':

fd=open('X', O_RDWR)
p=mmap(MAP_SHARED, fd)
for (i=0; i<length(fd); ++i)
  putchar(p[i])
sleep(1)
process 'B': truncate('X')
process 'C': open('Y'), write('Y')

Process 'A':
	p[length(fd)-1]='Z'
	munmap(p)
process 'C' close('Y')<=here we MAY get wrong data in 'Y'.



I look at 'ext2_truncate' and 'sys_msync',
but still haven't see whole picture.

in ext2_truncate we call
sync_mapping_buffers(inode->i_mapping);

so as I understand all pages belong to file will be moved
from 'cache' to disk?
but what happend in:
>process 'A':
>	p[length(fd)-1]='Z'
>	munmap(p)

will be data wroten to disk or not, and if not where this
sitation is handled?

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-01-17  9:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-01-17  8:18 truncate implementation Jan Koss
2006-01-17  8:31 ` Arjan van de Ven
2006-01-17  9:21   ` Jan Koss
2006-01-17  9:38     ` Arjan van de Ven
2006-01-17  9:39     ` Anton Altaparmakov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).