Browse code

Windows: Use sequential file access

Signed-off-by: John Howard <jhoward@microsoft.com>

John Howard authored on 2016/11/02 07:44:06
Showing 6 changed files
... ...
@@ -3,13 +3,13 @@ package image
3 3
 import (
4 4
 	"fmt"
5 5
 	"io"
6
-	"os"
7 6
 
8 7
 	"golang.org/x/net/context"
9 8
 
10 9
 	"github.com/docker/docker/cli"
11 10
 	"github.com/docker/docker/cli/command"
12 11
 	"github.com/docker/docker/pkg/jsonmessage"
12
+	"github.com/docker/docker/pkg/system"
13 13
 	"github.com/spf13/cobra"
14 14
 )
15 15
 
... ...
@@ -43,7 +43,9 @@ func runLoad(dockerCli *command.DockerCli, opts loadOptions) error {
43 43
 
44 44
 	var input io.Reader = dockerCli.In()
45 45
 	if opts.input != "" {
46
-		file, err := os.Open(opts.input)
46
+		// We use system.OpenSequential to use sequential file access on Windows, avoiding
47
+		// depleting the standby list un-necessarily. On Linux, this equates to a regular os.Open.
48
+		file, err := system.OpenSequential(opts.input)
47 49
 		if err != nil {
48 50
 			return err
49 51
 		}
... ...
@@ -21,6 +21,7 @@ import (
21 21
 	"github.com/docker/docker/pkg/streamformatter"
22 22
 	"github.com/docker/docker/pkg/stringid"
23 23
 	"github.com/docker/docker/pkg/symlink"
24
+	"github.com/docker/docker/pkg/system"
24 25
 	"github.com/docker/docker/reference"
25 26
 )
26 27
 
... ...
@@ -164,7 +165,9 @@ func (l *tarexporter) setParentID(id, parentID image.ID) error {
164 164
 }
165 165
 
166 166
 func (l *tarexporter) loadLayer(filename string, rootFS image.RootFS, id string, foreignSrc distribution.Descriptor, progressOutput progress.Output) (layer.Layer, error) {
167
-	rawTar, err := os.Open(filename)
167
+	// We use system.OpenSequential to use sequential file access on Windows, avoiding
168
+	// depleting the standby list. On Linux, this equates to a regular os.Open.
169
+	rawTar, err := system.OpenSequential(filename)
168 170
 	if err != nil {
169 171
 		logrus.Debugf("Error reading embedded tar: %v", err)
170 172
 		return nil, err
... ...
@@ -315,8 +315,10 @@ func (s *saveSession) saveLayer(id layer.ChainID, legacyImg image.V1Image, creat
315 315
 		}
316 316
 		os.Symlink(relPath, layerPath)
317 317
 	} else {
318
-
319
-		tarFile, err := os.Create(layerPath)
318
+		// Use system.CreateSequential rather than os.Create. This ensures sequential
319
+		// file access on Windows to avoid eating into MM standby list.
320
+		// On Linux, this equates to a regular os.Create.
321
+		tarFile, err := system.CreateSequential(layerPath)
320 322
 		if err != nil {
321 323
 			return distribution.Descriptor{}, err
322 324
 		}
... ...
@@ -374,7 +374,10 @@ func (ta *tarAppender) addTarFile(path, name string) error {
374 374
 	}
375 375
 
376 376
 	if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 {
377
-		file, err := os.Open(path)
377
+		// We use system.OpenSequential to ensure we use sequential file
378
+		// access on Windows to avoid depleting the standby list.
379
+		// On Linux, this equates to a regular os.Open.
380
+		file, err := system.OpenSequential(path)
378 381
 		if err != nil {
379 382
 			return err
380 383
 		}
... ...
@@ -412,8 +415,10 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, L
412 412
 		}
413 413
 
414 414
 	case tar.TypeReg, tar.TypeRegA:
415
-		// Source is regular file
416
-		file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode())
415
+		// Source is regular file. We use system.OpenFileSequential to use sequential
416
+		// file access to avoid depleting the standby list on Windows.
417
+		// On Linux, this equates to a regular os.OpenFile
418
+		file, err := system.OpenFileSequential(path, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode())
417 419
 		if err != nil {
418 420
 			return err
419 421
 		}
... ...
@@ -23,3 +23,32 @@ func MkdirAll(path string, perm os.FileMode) error {
23 23
 func IsAbs(path string) bool {
24 24
 	return filepath.IsAbs(path)
25 25
 }
26
+
27
+// The functions below here are wrappers for the equivalents in the os package.
28
+// They are passthrough on Unix platforms, and only relevant on Windows.
29
+
30
+// CreateSequential creates the named file with mode 0666 (before umask), truncating
31
+// it if it already exists. If successful, methods on the returned
32
+// File can be used for I/O; the associated file descriptor has mode
33
+// O_RDWR.
34
+// If there is an error, it will be of type *PathError.
35
+func CreateSequential(name string) (*os.File, error) {
36
+	return os.Create(name)
37
+}
38
+
39
+// OpenSequential opens the named file for reading. If successful, methods on
40
+// the returned file can be used for reading; the associated file
41
+// descriptor has mode O_RDONLY.
42
+// If there is an error, it will be of type *PathError.
43
+func OpenSequential(name string) (*os.File, error) {
44
+	return os.Open(name)
45
+}
46
+
47
+// OpenFileSequential is the generalized open call; most users will use Open
48
+// or Create instead. It opens the named file with specified flag
49
+// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
50
+// methods on the returned File can be used for I/O.
51
+// If there is an error, it will be of type *PathError.
52
+func OpenFileSequential(name string, flag int, perm os.FileMode) (*os.File, error) {
53
+	return os.OpenFile(name, flag, perm)
54
+}
... ...
@@ -131,3 +131,106 @@ func IsAbs(path string) bool {
131 131
 	}
132 132
 	return true
133 133
 }
134
+
135
+// The origin of the functions below here are the golang OS and syscall packages,
136
+// slightly modified to only cope with files, not directories due to the
137
+// specific use case.
138
+//
139
+// The alteration is to allow a file on Windows to be opened with
140
+// FILE_FLAG_SEQUENTIAL_SCAN (particular for docker load), to avoid eating
141
+// the standby list, particularly when accessing large files such as layer.tar.
142
+
143
+// CreateSequential creates the named file with mode 0666 (before umask), truncating
144
+// it if it already exists. If successful, methods on the returned
145
+// File can be used for I/O; the associated file descriptor has mode
146
+// O_RDWR.
147
+// If there is an error, it will be of type *PathError.
148
+func CreateSequential(name string) (*os.File, error) {
149
+	return OpenFileSequential(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0)
150
+}
151
+
152
+// OpenSequential opens the named file for reading. If successful, methods on
153
+// the returned file can be used for reading; the associated file
154
+// descriptor has mode O_RDONLY.
155
+// If there is an error, it will be of type *PathError.
156
+func OpenSequential(name string) (*os.File, error) {
157
+	return OpenFileSequential(name, os.O_RDONLY, 0)
158
+}
159
+
160
+// OpenFileSequential is the generalized open call; most users will use Open
161
+// or Create instead.
162
+// If there is an error, it will be of type *PathError.
163
+func OpenFileSequential(name string, flag int, _ os.FileMode) (*os.File, error) {
164
+	if name == "" {
165
+		return nil, &os.PathError{"open", name, syscall.ENOENT}
166
+	}
167
+	r, errf := syscallOpenFileSequential(name, flag, 0)
168
+	if errf == nil {
169
+		return r, nil
170
+	}
171
+	return nil, &os.PathError{"open", name, errf}
172
+}
173
+
174
+func syscallOpenFileSequential(name string, flag int, _ os.FileMode) (file *os.File, err error) {
175
+	r, e := syscallOpenSequential(name, flag|syscall.O_CLOEXEC, 0)
176
+	if e != nil {
177
+		return nil, e
178
+	}
179
+	return os.NewFile(uintptr(r), name), nil
180
+}
181
+
182
+func makeInheritSa() *syscall.SecurityAttributes {
183
+	var sa syscall.SecurityAttributes
184
+	sa.Length = uint32(unsafe.Sizeof(sa))
185
+	sa.InheritHandle = 1
186
+	return &sa
187
+}
188
+
189
+func syscallOpenSequential(path string, mode int, _ uint32) (fd syscall.Handle, err error) {
190
+	if len(path) == 0 {
191
+		return syscall.InvalidHandle, syscall.ERROR_FILE_NOT_FOUND
192
+	}
193
+	pathp, err := syscall.UTF16PtrFromString(path)
194
+	if err != nil {
195
+		return syscall.InvalidHandle, err
196
+	}
197
+	var access uint32
198
+	switch mode & (syscall.O_RDONLY | syscall.O_WRONLY | syscall.O_RDWR) {
199
+	case syscall.O_RDONLY:
200
+		access = syscall.GENERIC_READ
201
+	case syscall.O_WRONLY:
202
+		access = syscall.GENERIC_WRITE
203
+	case syscall.O_RDWR:
204
+		access = syscall.GENERIC_READ | syscall.GENERIC_WRITE
205
+	}
206
+	if mode&syscall.O_CREAT != 0 {
207
+		access |= syscall.GENERIC_WRITE
208
+	}
209
+	if mode&syscall.O_APPEND != 0 {
210
+		access &^= syscall.GENERIC_WRITE
211
+		access |= syscall.FILE_APPEND_DATA
212
+	}
213
+	sharemode := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE)
214
+	var sa *syscall.SecurityAttributes
215
+	if mode&syscall.O_CLOEXEC == 0 {
216
+		sa = makeInheritSa()
217
+	}
218
+	var createmode uint32
219
+	switch {
220
+	case mode&(syscall.O_CREAT|syscall.O_EXCL) == (syscall.O_CREAT | syscall.O_EXCL):
221
+		createmode = syscall.CREATE_NEW
222
+	case mode&(syscall.O_CREAT|syscall.O_TRUNC) == (syscall.O_CREAT | syscall.O_TRUNC):
223
+		createmode = syscall.CREATE_ALWAYS
224
+	case mode&syscall.O_CREAT == syscall.O_CREAT:
225
+		createmode = syscall.OPEN_ALWAYS
226
+	case mode&syscall.O_TRUNC == syscall.O_TRUNC:
227
+		createmode = syscall.TRUNCATE_EXISTING
228
+	default:
229
+		createmode = syscall.OPEN_EXISTING
230
+	}
231
+	// Use FILE_FLAG_SEQUENTIAL_SCAN rather than FILE_ATTRIBUTE_NORMAL as implemented in golang.
232
+	//https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
233
+	const fileFlagSequentialScan = 0x08000000 // FILE_FLAG_SEQUENTIAL_SCAN
234
+	h, e := syscall.CreateFile(pathp, access, sharemode, sa, createmode, fileFlagSequentialScan, 0)
235
+	return h, e
236
+}