truncate implementation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux