The Central Mystery

Colin Plumb (
Thu, 24 Jul 97 01:59:21 MDT

I've been trying to understand how mmap() works, and it seems to
be one of the central mysteries of the Linux kernel, because it's
spread across the mm and fs directories, deeply intertwingled
with the buffer cache, and somehow works without a lot of explicit
support from specific file systems making implict use of the
apparently undocumented detailed semantics of the vfs layer.

I guess in order of preference, either
- Someone has already written a good explanation, or
- There are a few sources which, when put together, explain most of it, or
- I can get a few fragmentary clues and figure the rest out by reading
a lot of source, or
- I never figure it out.

In the second and third case, I'll try to write something so the next
person after me who tries to understand this has an easier time.
(Note to participants in a certain thread: if you feel there's a lack,
please *do* something about it. Even if you don't think you can do
anything "good enough", there's a story about "stone soup" that
you might have heard...)

What I want to figure out is how a file system brings data into memory
in such a way that it's properly aligned and packed into MMU-sized
(e.g. 4K) pages despite the huge variability in the number of ways that
it can get there. How do permissions get checked and used? How
do writes get propagated back to the file system?

In particular, how does it work if the file system block size doesn't
match the MMU page size? How do I mmap() a 1K file?

It all seems to be buried in generic_file_mmap, mm/filemap.c, and
the vm_operations_struct.

I realize that this is getting hacked on pretty actively, and the dcache
makes things even more interesting, but can anyone enlighten me?

(This all came about from trying to understand reiserfs and the performance
ramifications of not storing file data page-aligned on disk. I think
this means that the kernel has to do some copying to do mmap(), but I
don't understand where, or how it knows when it doesn't have to do
the copying.)

Thanks to anyone who has any pointers!