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