github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/sentry/vfs/lock.go (about) 1 // Copyright 2020 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package vfs 16 17 import ( 18 "github.com/MerlinKodo/gvisor/pkg/abi/linux" 19 "github.com/MerlinKodo/gvisor/pkg/context" 20 "github.com/MerlinKodo/gvisor/pkg/errors/linuxerr" 21 fslock "github.com/MerlinKodo/gvisor/pkg/sentry/fsimpl/lock" 22 ) 23 24 // FileLocks supports POSIX and BSD style locks, which correspond to fcntl(2) 25 // and flock(2) respectively in Linux. It can be embedded into various file 26 // implementations that support locking. 27 // 28 // Note that in Linux these two types of locks are _not_ cooperative, because 29 // race and deadlock conditions make merging them prohibitive. We do the same 30 // and keep them oblivious to each other. 31 // 32 // +stateify savable 33 type FileLocks struct { 34 // bsd is a set of BSD-style advisory file wide locks, see flock(2). 35 bsd fslock.Locks 36 37 // posix is a set of POSIX-style regional advisory locks, see fcntl(2). 38 posix fslock.Locks 39 } 40 41 // LockBSD tries to acquire a BSD-style lock on the entire file. 42 func (fl *FileLocks) LockBSD(ctx context.Context, uid fslock.UniqueID, ownerID int32, t fslock.LockType, block bool) error { 43 if err := fl.bsd.LockRegion(ctx, uid, ownerID, t, fslock.LockRange{0, fslock.LockEOF}, false, block); err == nil || err == linuxerr.ErrWouldBlock { 44 return err 45 } 46 return linuxerr.ERESTARTSYS 47 } 48 49 // UnlockBSD releases a BSD-style lock on the entire file. 50 // 51 // This operation is always successful, even if there did not exist a lock on 52 // the requested region held by uid in the first place. 53 func (fl *FileLocks) UnlockBSD(uid fslock.UniqueID) { 54 fl.bsd.UnlockRegion(uid, fslock.LockRange{0, fslock.LockEOF}) 55 } 56 57 // LockPOSIX tries to acquire a POSIX-style lock on a file region. 58 func (fl *FileLocks) LockPOSIX(ctx context.Context, uid fslock.UniqueID, ownerPID int32, t fslock.LockType, r fslock.LockRange, block bool) error { 59 _, ofd := uid.(*FileDescription) 60 if err := fl.posix.LockRegion(ctx, uid, ownerPID, t, r, ofd, block); err == nil || err == linuxerr.ErrWouldBlock { 61 return err 62 } 63 return linuxerr.ERESTARTSYS 64 } 65 66 // UnlockPOSIX releases a POSIX-style lock on a file region. 67 // 68 // This operation is always successful, even if there did not exist a lock on 69 // the requested region held by uid in the first place. 70 func (fl *FileLocks) UnlockPOSIX(ctx context.Context, uid fslock.UniqueID, r fslock.LockRange) error { 71 fl.posix.UnlockRegion(uid, r) 72 return nil 73 } 74 75 // TestPOSIX returns information about whether the specified lock can be held, in the style of the F_GETLK fcntl. 76 func (fl *FileLocks) TestPOSIX(ctx context.Context, uid fslock.UniqueID, t fslock.LockType, r fslock.LockRange) (linux.Flock, error) { 77 _, ofd := uid.(*FileDescription) 78 return fl.posix.TestRegion(ctx, uid, t, r, ofd), nil 79 }