diff --git a/cmd/common-main.go b/cmd/common-main.go index d813e7b37..d744905fb 100644 --- a/cmd/common-main.go +++ b/cmd/common-main.go @@ -186,6 +186,11 @@ func handleCommonEnvVars() { logger.Fatal(config.ErrInvalidBrowserValue(err), "Invalid MINIO_BROWSER value in environment variable") } + globalFSOSync, err = config.ParseBool(env.Get(config.EnvFSOSync, config.EnableOff)) + if err != nil { + logger.Fatal(config.ErrInvalidFSOSyncValue(err), "Invalid MINIO_FS_OSYNC value in environment variable") + } + domains := env.Get(config.EnvDomain, "") if len(domains) != 0 { for _, domainName := range strings.Split(domains, config.ValueSeparator) { diff --git a/cmd/config/constants.go b/cmd/config/constants.go index b526b7987..1e47e7132 100644 --- a/cmd/config/constants.go +++ b/cmd/config/constants.go @@ -32,6 +32,7 @@ const ( EnvRegionName = "MINIO_REGION_NAME" EnvPublicIPs = "MINIO_PUBLIC_IPS" EnvEndpoints = "MINIO_ENDPOINTS" + EnvFSOSync = "MINIO_FS_OSYNC" // API sub-system EnvAPIRequestsMax = "MINIO_API_REQUESTS_MAX" diff --git a/cmd/config/errors.go b/cmd/config/errors.go index 1122e5224..6ae509114 100644 --- a/cmd/config/errors.go +++ b/cmd/config/errors.go @@ -24,6 +24,12 @@ var ( "Browser can only accept `on` and `off` values. To disable web browser access, set this value to `off`", ) + ErrInvalidFSOSyncValue = newErrFn( + "Invalid O_SYNC value", + "Please check the passed value", + "Can only accept `on` and `off` values. To enable O_SYNC for fs backend, set this value to `on`", + ) + ErrInvalidDomainValue = newErrFn( "Invalid domain value", "Please check the passed value", diff --git a/cmd/fs-v1-helpers.go b/cmd/fs-v1-helpers.go index a275c08b6..37756a16f 100644 --- a/cmd/fs-v1-helpers.go +++ b/cmd/fs-v1-helpers.go @@ -321,7 +321,11 @@ func fsCreateFile(ctx context.Context, filePath string, reader io.Reader, buf [] return 0, err } - writer, err := lock.Open(filePath, os.O_CREATE|os.O_WRONLY, 0666) + flags := os.O_CREATE | os.O_WRONLY + if globalFSOSync { + flags = flags | os.O_SYNC + } + writer, err := lock.Open(filePath, flags, 0666) if err != nil { return 0, osErrToFSFileErr(err) } diff --git a/cmd/fs-v1-multipart.go b/cmd/fs-v1-multipart.go index 0bea09432..f04b2364a 100644 --- a/cmd/fs-v1-multipart.go +++ b/cmd/fs-v1-multipart.go @@ -112,7 +112,7 @@ func (fs *FSObjects) backgroundAppend(ctx context.Context, bucket, object, uploa } partPath := pathJoin(uploadIDDir, entry) - err = mioutil.AppendFile(file.filePath, partPath) + err = mioutil.AppendFile(file.filePath, partPath, globalFSOSync) if err != nil { reqInfo := logger.GetReqInfo(ctx).AppendTags("partPath", partPath) reqInfo.AppendTags("filepath", file.filePath) @@ -638,7 +638,7 @@ func (fs *FSObjects) CompleteMultipartUpload(ctx context.Context, bucket string, } for _, part := range parts { partPath := getPartFile(entries, part.PartNumber, part.ETag) - if err = mioutil.AppendFile(appendFilePath, pathJoin(uploadIDDir, partPath)); err != nil { + if err = mioutil.AppendFile(appendFilePath, pathJoin(uploadIDDir, partPath), globalFSOSync); err != nil { logger.LogIf(ctx, err) return oi, toObjectErr(err) } diff --git a/cmd/globals.go b/cmd/globals.go index bd5163284..2f785eb9c 100644 --- a/cmd/globals.go +++ b/cmd/globals.go @@ -280,6 +280,8 @@ var ( // fix the system. globalSafeMode bool + // If writes to FS backend should be O_SYNC. + globalFSOSync bool // Add new variable global values here. ) diff --git a/pkg/ioutil/append-file_nix.go b/pkg/ioutil/append-file_nix.go index f82367a9f..468c185d0 100644 --- a/pkg/ioutil/append-file_nix.go +++ b/pkg/ioutil/append-file_nix.go @@ -24,8 +24,12 @@ import ( ) // AppendFile - appends the file "src" to the file "dst" -func AppendFile(dst string, src string) error { - appendFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) +func AppendFile(dst string, src string, osync bool) error { + flags := os.O_WRONLY | os.O_APPEND | os.O_CREATE + if osync { + flags = flags | os.O_SYNC + } + appendFile, err := os.OpenFile(dst, flags, 0666) if err != nil { return err } diff --git a/pkg/ioutil/append-file_windows.go b/pkg/ioutil/append-file_windows.go index afb9ff408..ee23bb03b 100644 --- a/pkg/ioutil/append-file_windows.go +++ b/pkg/ioutil/append-file_windows.go @@ -24,7 +24,7 @@ import ( ) // AppendFile - appends the file "src" to the file "dst" -func AppendFile(dst string, src string) error { +func AppendFile(dst string, src string, osync bool) error { appendFile, err := lock.Open(dst, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) if err != nil { return err diff --git a/pkg/ioutil/ioutil_test.go b/pkg/ioutil/ioutil_test.go index bbfe067fa..c2aaee7cb 100644 --- a/pkg/ioutil/ioutil_test.go +++ b/pkg/ioutil/ioutil_test.go @@ -61,7 +61,7 @@ func TestAppendFile(t *testing.T) { f.WriteString("bbbbbbbbbb") f.Close() - if err = AppendFile(name1, name2); err != nil { + if err = AppendFile(name1, name2, false); err != nil { t.Error(err) }