• Hugh Dickins's avatar
    tmpfs: support fallocate preallocation · e2d12e22
    Hugh Dickins authored
    The systemd plumbers expressed a wish that tmpfs support preallocation.
    Cong Wang wrote a patch, but several kernel guys expressed scepticism:
    https://lkml.org/lkml/2011/11/18/137
    
    Christoph Hellwig: What for exactly? Please explain why preallocating on
    tmpfs would make any sense.
    
    Kay Sievers: To be able to safely use mmap(), regarding SIGBUS, on files
    on the /dev/shm filesystem.  The glibc fallback loop for -ENOSYS [or
    -EOPNOTSUPP] on fallocate is just ugly.
    
    Hugh Dickins: If tmpfs is going to support
    fallocate(FALLOC_FL_PUNCH_HOLE), it would seem perverse to permit the
    deallocation but fail the allocation.  Christoph Hellwig: Agreed.
    
    Now that we do have shmem_fallocate() for hole-punching, plumb in basic
    support for preallocation mode too.  It's fairly straightforward (though
    quite a few details needed attention), except for when it fails part way
    through.  What a pity that fallocate(2) was not specified to return the
    length allocated, permitting short fallocations!
    
    As it is, when it fails part way through, we ought to free what has just
    been allocated by this system call; but must be very sure not to free any
    allocated earlier, or any allocated by racing accesses (not all excluded
    by i_mutex).
    
    But we cannot distinguish them: so in this patch simply leak allocations
    on partial failure (they will be freed later if the file is removed).
    
    An attractive alternative approach would have been for fallocate() not to
    allocate pages at all, but note reservations by entries in the radix-tree.
     But that would give less assurance, and, critically, would be hard to fit
    with mem cgroups (who owns the reservations?): allocating pages lets
    fallocate() behave in just the same way as write().
    Based-on-patch-by: default avatarCong Wang <amwang@redhat.com>
    Signed-off-by: default avatarHugh Dickins <hughd@google.com>
    Cc: Christoph Hellwig <hch@infradead.org>
    Cc: Cong Wang <amwang@redhat.com>
    Cc: Kay Sievers <kay@vrfy.org>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    e2d12e22
shmem.c 72.5 KB