github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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/SagerNet/gvisor/pkg/abi/linux" 19 "github.com/SagerNet/gvisor/pkg/context" 20 fslock "github.com/SagerNet/gvisor/pkg/sentry/fs/lock" 21 "github.com/SagerNet/gvisor/pkg/syserror" 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 for VFS2 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 fslock.Blocker) error { 43 if fl.bsd.LockRegion(uid, ownerID, t, fslock.LockRange{0, fslock.LockEOF}, block) { 44 return nil 45 } 46 47 // Return an appropriate error for the unsuccessful lock attempt, depending on 48 // whether this is a blocking or non-blocking operation. 49 if block == nil { 50 return syserror.ErrWouldBlock 51 } 52 return syserror.ERESTARTSYS 53 } 54 55 // UnlockBSD releases a BSD-style lock on the entire file. 56 // 57 // This operation is always successful, even if there did not exist a lock on 58 // the requested region held by uid in the first place. 59 func (fl *FileLocks) UnlockBSD(uid fslock.UniqueID) { 60 fl.bsd.UnlockRegion(uid, fslock.LockRange{0, fslock.LockEOF}) 61 } 62 63 // LockPOSIX tries to acquire a POSIX-style lock on a file region. 64 func (fl *FileLocks) LockPOSIX(ctx context.Context, uid fslock.UniqueID, ownerPID int32, t fslock.LockType, r fslock.LockRange, block fslock.Blocker) error { 65 if fl.posix.LockRegion(uid, ownerPID, t, r, block) { 66 return nil 67 } 68 69 // Return an appropriate error for the unsuccessful lock attempt, depending on 70 // whether this is a blocking or non-blocking operation. 71 if block == nil { 72 return syserror.ErrWouldBlock 73 } 74 return syserror.ERESTARTSYS 75 } 76 77 // UnlockPOSIX releases a POSIX-style lock on a file region. 78 // 79 // This operation is always successful, even if there did not exist a lock on 80 // the requested region held by uid in the first place. 81 func (fl *FileLocks) UnlockPOSIX(ctx context.Context, uid fslock.UniqueID, r fslock.LockRange) error { 82 fl.posix.UnlockRegion(uid, r) 83 return nil 84 } 85 86 // TestPOSIX returns information about whether the specified lock can be held, in the style of the F_GETLK fcntl. 87 func (fl *FileLocks) TestPOSIX(ctx context.Context, uid fslock.UniqueID, t fslock.LockType, r fslock.LockRange) (linux.Flock, error) { 88 return fl.posix.TestRegion(ctx, uid, t, r), nil 89 }