From ea45573148e0656e5e3412f5a5db9cd823f5a2a0 Mon Sep 17 00:00:00 2001 From: Ayush Ranjan Date: Mon, 2 May 2022 17:36:40 -0700 Subject: [PATCH] Update comments about tmpfs size. - Removed old TODO from VFS1. We will not support this option on VFS1. - Enhanced comments about how tmpfs pages are accounted for by write(2)s. PiperOrigin-RevId: 446072121 --- pkg/sentry/fs/tmpfs/tmpfs.go | 2 +- pkg/sentry/fsimpl/tmpfs/regular_file.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/sentry/fs/tmpfs/tmpfs.go b/pkg/sentry/fs/tmpfs/tmpfs.go index 9a835b556..e06f39730 100644 --- a/pkg/sentry/fs/tmpfs/tmpfs.go +++ b/pkg/sentry/fs/tmpfs/tmpfs.go @@ -40,7 +40,7 @@ var fsInfo = fs.Info{ // of 0. To work around this, claim to have a very large but non-zero size, // chosen to ensure that BlockSize * Blocks does not overflow int64 (which // applications may also handle incorrectly). - // TODO(b/29637826): allow configuring a tmpfs size and enforce it. + // NOTE(b/29637826): Support for configurable tmpfs size was added to VFS2. TotalBlocks: math.MaxInt64 / hostarch.PageSize, FreeBlocks: math.MaxInt64 / hostarch.PageSize, } diff --git a/pkg/sentry/fsimpl/tmpfs/regular_file.go b/pkg/sentry/fsimpl/tmpfs/regular_file.go index b59a4c806..0472f28c0 100644 --- a/pkg/sentry/fsimpl/tmpfs/regular_file.go +++ b/pkg/sentry/fsimpl/tmpfs/regular_file.go @@ -453,6 +453,10 @@ func (fd *regularFileFD) pwrite(ctx context.Context, src usermem.IOSequence, off if err != nil { return 0, offset, err } + + // Reserve enough space assuming the entire write was successful. The + // corresponding update to f.size is done in WriteFromBlocks() which is + // called via src.CopyInTo() below. maybeSizeInc := false src = src.TakeFirst64(srclen) if uint64(end) > f.size.Load() { @@ -461,9 +465,13 @@ func (fd *regularFileFD) pwrite(ctx context.Context, src usermem.IOSequence, off return 0, 0, err } } + + // Perform the write. rw := getRegularFileReadWriter(f, offset) n, err := src.CopyInTo(ctx, rw) - if unwritten := srclen - n; maybeSizeInc && unwritten != 0 { + + // Correct page accounting if this was a partial write. + if maybeSizeInc && srclen-n != 0 { if err := f.inode.fs.updatePagesUsed(uint64(end), f.size.Load()); err != nil { return 0, 0, err }