aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Evans <kevans@FreeBSD.org>2020-01-08 19:08:44 +0000
committerKyle Evans <kevans@FreeBSD.org>2020-01-08 19:08:44 +0000
commitf10405323ab1ac3b4bad100c6e746395d02f392a (patch)
treecf3e58b88d89031f4177cc5050bb458986fd1527
parent7ce40722673b6392e82abb7330665d3d235e7cac (diff)
downloadsrc-f10405323ab1.tar.gz
src-f10405323ab1.zip
posixshm: implement posix_fallocate(2)
Linux expects to be able to use posix_fallocate(2) on a memfd. Other places would use this with shm_open(2) to act as a smarter ftruncate(2). Test has been added to go along with this. Reviewed by: kib (earlier version) Differential Revision: https://reviews.freebsd.org/D23042
Notes
Notes: svn path=/head/; revision=356512
-rw-r--r--sys/kern/uipc_shm.c28
-rw-r--r--tests/sys/posixshm/posixshm_test.c39
2 files changed, 67 insertions, 0 deletions
diff --git a/sys/kern/uipc_shm.c b/sys/kern/uipc_shm.c
index 435da35a5b28..861e26c85834 100644
--- a/sys/kern/uipc_shm.c
+++ b/sys/kern/uipc_shm.c
@@ -138,6 +138,7 @@ static fo_fill_kinfo_t shm_fill_kinfo;
static fo_mmap_t shm_mmap;
static fo_get_seals_t shm_get_seals;
static fo_add_seals_t shm_add_seals;
+static fo_fallocate_t shm_fallocate;
/* File descriptor operations. */
struct fileops shm_ops = {
@@ -157,6 +158,7 @@ struct fileops shm_ops = {
.fo_mmap = shm_mmap,
.fo_get_seals = shm_get_seals,
.fo_add_seals = shm_add_seals,
+ .fo_fallocate = shm_fallocate,
.fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
};
@@ -1438,6 +1440,32 @@ shm_get_seals(struct file *fp, int *seals)
}
static int
+shm_fallocate(struct file *fp, off_t offset, off_t len, struct thread *td)
+{
+ void *rl_cookie;
+ struct shmfd *shmfd;
+ size_t size;
+ int error;
+
+ /* This assumes that the caller already checked for overflow. */
+ error = 0;
+ shmfd = fp->f_data;
+ size = offset + len;
+ rl_cookie = rangelock_wlock(&shmfd->shm_rl, 0, OFF_MAX,
+ &shmfd->shm_mtx);
+ if (size > shmfd->shm_size) {
+ VM_OBJECT_WLOCK(shmfd->shm_object);
+ error = shm_dotruncate_locked(shmfd, size, rl_cookie);
+ VM_OBJECT_WUNLOCK(shmfd->shm_object);
+ }
+ rangelock_unlock(&shmfd->shm_rl, rl_cookie, &shmfd->shm_mtx);
+ /* Translate to posix_fallocate(2) return value as needed. */
+ if (error == ENOMEM)
+ error = ENOSPC;
+ return (error);
+}
+
+static int
sysctl_posix_shm_list(SYSCTL_HANDLER_ARGS)
{
struct shm_mapping *shmm;
diff --git a/tests/sys/posixshm/posixshm_test.c b/tests/sys/posixshm/posixshm_test.c
index 2bc59807d83f..3f6622df99aa 100644
--- a/tests/sys/posixshm/posixshm_test.c
+++ b/tests/sys/posixshm/posixshm_test.c
@@ -918,6 +918,44 @@ ATF_TC_BODY(mode, tc)
umask(restore_mask);
}
+ATF_TC_WITHOUT_HEAD(fallocate);
+ATF_TC_BODY(fallocate, tc)
+{
+ struct stat st;
+ int error, fd, sz;
+
+ /*
+ * Primitive test case for posix_fallocate with shmd. Effectively
+ * expected to work like a smarter ftruncate that will grow the region
+ * as needed in a race-free way.
+ */
+ fd = shm_open(SHM_ANON, O_RDWR, 0666);
+ ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
+ /* Set the initial size. */
+ sz = 32;
+ ATF_REQUIRE(ftruncate(fd, sz) == 0);
+
+ /* Now grow it. */
+ error = 0;
+ sz *= 2;
+ ATF_REQUIRE_MSG((error = posix_fallocate(fd, 0, sz)) == 0,
+ "posix_fallocate failed; error=%d", error);
+ ATF_REQUIRE(fstat(fd, &st) == 0);
+ ATF_REQUIRE(st.st_size == sz);
+ /* Attempt to shrink it; should succeed, but not change the size. */
+ ATF_REQUIRE_MSG((error = posix_fallocate(fd, 0, sz / 2)) == 0,
+ "posix_fallocate failed; error=%d", error);
+ ATF_REQUIRE(fstat(fd, &st) == 0);
+ ATF_REQUIRE(st.st_size == sz);
+ /* Grow it using an offset of sz and len of sz. */
+ ATF_REQUIRE_MSG((error = posix_fallocate(fd, sz, sz)) == 0,
+ "posix_fallocate failed; error=%d", error);
+ ATF_REQUIRE(fstat(fd, &st) == 0);
+ ATF_REQUIRE(st.st_size == (sz * 2));
+
+ close(fd);
+}
+
ATF_TP_ADD_TCS(tp)
{
@@ -951,6 +989,7 @@ ATF_TP_ADD_TCS(tp)
ATF_TP_ADD_TC(tp, object_resize);
ATF_TP_ADD_TC(tp, cloexec);
ATF_TP_ADD_TC(tp, mode);
+ ATF_TP_ADD_TC(tp, fallocate);
return (atf_no_error());
}