github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/syscalls/linux/sys_sync.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 linux 16 17 import ( 18 "github.com/nicocha30/gvisor-ligolo/pkg/abi/linux" 19 "github.com/nicocha30/gvisor-ligolo/pkg/errors/linuxerr" 20 "github.com/nicocha30/gvisor-ligolo/pkg/sentry/arch" 21 "github.com/nicocha30/gvisor-ligolo/pkg/sentry/kernel" 22 ) 23 24 // Sync implements Linux syscall sync(2). 25 func Sync(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 26 return 0, nil, t.Kernel().VFS().SyncAllFilesystems(t) 27 } 28 29 // Syncfs implements Linux syscall syncfs(2). 30 func Syncfs(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 31 fd := args[0].Int() 32 33 file := t.GetFile(fd) 34 if file == nil { 35 return 0, nil, linuxerr.EBADF 36 } 37 defer file.DecRef(t) 38 39 if file.StatusFlags()&linux.O_PATH != 0 { 40 return 0, nil, linuxerr.EBADF 41 } 42 43 return 0, nil, file.SyncFS(t) 44 } 45 46 // Fsync implements Linux syscall fsync(2). 47 func Fsync(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 48 fd := args[0].Int() 49 50 file := t.GetFile(fd) 51 if file == nil { 52 return 0, nil, linuxerr.EBADF 53 } 54 defer file.DecRef(t) 55 56 return 0, nil, file.Sync(t) 57 } 58 59 // Fdatasync implements Linux syscall fdatasync(2). 60 func Fdatasync(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 61 // TODO(gvisor.dev/issue/1897): Avoid writeback of unnecessary metadata. 62 return Fsync(t, sysno, args) 63 } 64 65 // SyncFileRange implements Linux syscall sync_file_range(2). 66 func SyncFileRange(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { 67 fd := args[0].Int() 68 offset := args[1].Int64() 69 nbytes := args[2].Int64() 70 flags := args[3].Uint() 71 72 // Check for negative values and overflow. 73 if offset < 0 || offset+nbytes < 0 { 74 return 0, nil, linuxerr.EINVAL 75 } 76 if flags&^(linux.SYNC_FILE_RANGE_WAIT_BEFORE|linux.SYNC_FILE_RANGE_WRITE|linux.SYNC_FILE_RANGE_WAIT_AFTER) != 0 { 77 return 0, nil, linuxerr.EINVAL 78 } 79 80 file := t.GetFile(fd) 81 if file == nil { 82 return 0, nil, linuxerr.EBADF 83 } 84 defer file.DecRef(t) 85 86 // TODO(gvisor.dev/issue/1897): Currently, the only file syncing we support 87 // is a full-file sync, i.e. fsync(2). As a result, there are severe 88 // limitations on how much we support sync_file_range: 89 // - In Linux, sync_file_range(2) doesn't write out the file's metadata, even 90 // if the file size is changed. We do. 91 // - We always sync the entire file instead of [offset, offset+nbytes). 92 // - We do not support the use of WAIT_BEFORE without WAIT_AFTER. For 93 // correctness, we would have to perform a write-out every time WAIT_BEFORE 94 // was used, but this would be much more expensive than expected if there 95 // were no write-out operations in progress. 96 // - Whenever WAIT_AFTER is used, we sync the file. 97 // - Ignore WRITE. If this flag is used with WAIT_AFTER, then the file will 98 // be synced anyway. If this flag is used without WAIT_AFTER, then it is 99 // safe (and less expensive) to do nothing, because the syscall will not 100 // wait for the write-out to complete--we only need to make sure that the 101 // next time WAIT_BEFORE or WAIT_AFTER are used, the write-out completes. 102 // - According to fs/sync.c, WAIT_BEFORE|WAIT_AFTER "will detect any I/O 103 // errors or ENOSPC conditions and will return those to the caller, after 104 // clearing the EIO and ENOSPC flags in the address_space." We don't do 105 // this. 106 107 if flags&linux.SYNC_FILE_RANGE_WAIT_BEFORE != 0 && 108 flags&linux.SYNC_FILE_RANGE_WAIT_AFTER == 0 { 109 t.Kernel().EmitUnimplementedEvent(t, sysno) 110 return 0, nil, linuxerr.ENOSYS 111 } 112 113 if flags&linux.SYNC_FILE_RANGE_WAIT_AFTER != 0 { 114 if err := file.Sync(t); err != nil { 115 return 0, nil, linuxerr.ConvertIntr(err, linuxerr.ERESTARTSYS) 116 } 117 } 118 return 0, nil, nil 119 }