Page MenuHomeFreeBSD

posix_fallocate: push vnop implementation into the fileops layer

Authored by kevans on Jan 5 2020, 5:04 PM.
Referenced Files
Unknown Object (File)
Sun, Nov 27, 5:04 PM
Unknown Object (File)
Sun, Nov 27, 6:58 AM



As an example benefit, implement posix_fallocate(2) for shmfd. The shmfd implementation defers to shm_dotruncate as needed, which will reserve more swap space to accommodate -- this is something that Linux wants to be able to do, and it seems sensible enough.

Diff Detail

rS FreeBSD src repository - subversion
Lint Not Applicable
Tests Not Applicable

Event Timeline

bcr added a subscriber: bcr.

OK from manpages.

Move {sys,kern}_posix_fallocate into sys_generic.c -- there's no longer anything vfs specific to it, as those have been split off into vfs_vnops.

kib added inline comments.
857 ↗(On Diff #66380)
if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0)
         error = ESPIPE;
else if ((fp->f_flag & FWRITE) == 0)
         error = EBADF;
         error = fo_fallocate(fp, offset, len, td);

gets rid of the 'out' label.

1459 ↗(On Diff #66380)

Does it make sense to instantiate pages for the fallocated range ? Even if it does, I suggest to do it in the follow-up commit.

This revision is now accepted and ready to land.Jan 6 2020, 12:15 AM
1459 ↗(On Diff #66380)

This wasn't clear to me- the wording on the spec (which our manpage seems to basically copy) carefully avoids making any promises on the state of any newly allocated space

1459 ↗(On Diff #66380)

This is because the only way to guarantee that the pages are not get recycled is to wire them. But if the intent is to provide the backing storage so that later the app would not pay the allocation cost, e.g. by waiting for pages to become available, then pages should be allocated.

I do not know what is the use case for fallocate() over shmfd.

1459 ↗(On Diff #66380)

I do not know what is the use case for fallocate() over shmfd


The intent in the Weston code that ended up in many places was "to guarantee that disk space is available for the file at the given size"… even if the file is purely in-memory. It's code that was written assuming on-disk /tmp first, with memfd/SHM_ANON bolted on later. [[ | In Mesa, we don't use posix_fallocate anymore ]]. So it's relatively safe to assume that fallocate is used as just a "better" ftruncate here.

I have one more local change to this that allows shm_dotruncate to borrow a rangelock from the caller to use, then shm_fallocate takes a write lock on it before checking the size, but the changes are fairly non-invasive so I suspect I will not rev this review one more time unless requested.

Edit: though I'm considering just adding vm object locking to shm_fallocate at that point and calling shm_dotruncate_locked directly.

1459 ↗(On Diff #66380)

Thanks for the insight!