From: Jan Koss <kossjan@gmail.com>
To: kernelnewbies@nl.linux.org
Cc: linux-fsdevel@vger.kernel.org
Subject: truncate implementation
Date: Tue, 17 Jan 2006 11:18:15 +0300 [thread overview]
Message-ID: <b97d23040601170018o6255d9edifd7f42f1cbee0710@mail.gmail.com> (raw)
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/
next reply other threads:[~2006-01-17 8:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-01-17 8:18 Jan Koss [this message]
2006-01-17 8:31 ` truncate implementation 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b97d23040601170018o6255d9edifd7f42f1cbee0710@mail.gmail.com \
--to=kossjan@gmail.com \
--cc=kernelnewbies@nl.linux.org \
--cc=linux-fsdevel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).