forked from mendersoftware/mender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_device.go
437 lines (381 loc) · 12.5 KB
/
block_device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright 2020 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package installer
import (
"bytes"
"io"
"os"
"path/filepath"
"syscall"
"github.com/mendersoftware/log"
"github.com/mendersoftware/mender/system"
"github.com/mendersoftware/mender/utils"
"github.com/pkg/errors"
)
var (
BlockDeviceGetSizeOf BlockDeviceGetSizeFunc = system.GetBlockDeviceSize
BlockDeviceGetSectorSizeOf BlockDeviceGetSectorSizeFunc = system.GetBlockDeviceSectorSize
)
// BlockDevicer is a file-like interface for the block-device.
type BlockDevicer interface {
io.Reader
io.Writer
io.Closer
io.Seeker
Sync() error // Commits previously-written data to stable storage.
}
// BlockDeviceGetSizeFunc is a helper for obtaining the size of a block device.
type BlockDeviceGetSizeFunc func(file *os.File) (uint64, error)
// BlockDeviceGetSectorSizeFunc is a helper for obtaining the sector size of a block device.
type BlockDeviceGetSectorSizeFunc func(file *os.File) (int, error)
// BlockDevice is a low-level wrapper for a block device. The wrapper implements
// the io.Writer and io.Closer interfaces, which is all that is needed by the
// mender-client.
type BlockDevice struct {
Path string // Device path, ex. /dev/mmcblk0p1
w io.WriteCloser
}
type bdevice int
// Give the block-device a package-like interface,
// i.e., blockdevice.Open(partition, size)
var blockdevice bdevice
// Open tries to open the 'device' (/dev/<device> usually), and returns a
// BlockDevice.
func (bd bdevice) Open(device string, size int64) (*BlockDevice, error) {
log.Infof("opening device %s for writing", device)
var out *os.File
var err error
log.Debugf("Open block-device for installing update of size: %d", size)
if size < 0 {
return nil, errors.New("Have invalid update. Aborting.")
}
// Make sure the file system is not mounted (MEN-2084)
if mntPt := checkMounted(device); mntPt != "" {
log.Warnf("Inactive partition %q is mounted at %q. "+
"This might be caused by some \"auto mount\" service "+
"(e.g udisks2) that mounts all block devices. It is "+
"recommended to blacklist the partitions used by "+
"Mender to avoid any issues.", device, mntPt)
log.Warnf("Performing umount on %q.", mntPt)
err = syscall.Unmount(device, 0)
if err != nil {
log.Errorf("Error unmounting partition %s",
device)
return nil, err
}
}
typeUBI := system.IsUbiBlockDevice(device)
log.Debugf("Device: %s is a ubi device: %t", device, typeUBI)
var flag int
if typeUBI {
// UBI block devices are not prefixed with /dev due to the fact
// that the kernel root= argument does not handle UBI block
// devices which are prefixed with /dev
//
// Kernel root= only accepts:
// - ubi0_0
// - ubi:rootfsa
device = filepath.Join("/dev", device)
flag = os.O_WRONLY
} else {
flag = os.O_RDWR
}
b := &BlockDevice{
Path: device,
}
if bsz, err := b.Size(); err != nil {
log.Errorf("failed to read size of block device %s: %v",
device, err)
return nil, err
} else if bsz < uint64(size) {
log.Errorf("update (%v bytes) is larger than the size of device %s (%v bytes)",
size, device, bsz)
return nil, syscall.ENOSPC
}
nativeSsz, err := b.SectorSize()
if err != nil {
log.Errorf("failed to read sector size of block device %s: %v",
device, err)
return nil, err
}
// The size of an individual sector tends to be quite small. Rather than
// doing a zillion small writes, do medium-size-ish writes that are
// still sector aligned. (Doing too many small writes can put pressure
// on the DMA subsystem (unless writes are able to be coalesced) by
// requiring large numbers of scatter-gather descriptors to be
// allocated.)
chunkSize := nativeSsz
// Pick a multiple of the sector size that's around 1 MiB.
for chunkSize < 1*1024*1024 {
chunkSize = chunkSize * 2
}
log.Infof("native sector size of block device %s is %v, we will write in chunks of %v",
device,
nativeSsz,
chunkSize,
)
log.Debugf("Opening device: %s for writing with flag: %d", device, flag)
out, err = os.OpenFile(device, flag, 0)
if err != nil {
return nil, errors.Wrapf(err, "Failed to open the device: %q", device)
}
// From <mtd/ubi-user.h>
//
// UBI volume update
// ~~~~~~~~~~~~~~~~~
//
// Volume update should be done via the UBI_IOCVOLUP ioctl command of the
// corresponding UBI volume character device. A pointer to a 64-bit update
// size should be passed to the ioctl. After this, UBI expects user to write
// this number of bytes to the volume character device. The update is finished
// when the claimed number of bytes is passed. So, the volume update sequence
// is something like:
//
// fd = open("/dev/my_volume");
// ioctl(fd, UBI_IOCVOLUP, &image_size);
// write(fd, buf, image_size);
// close(fd);
if typeUBI {
err := system.SetUbiUpdateVolume(out, uint64(size))
if err != nil {
log.Errorf("Failed to write images size to UBI_IOCVOLUP: %v", err)
return nil, err
}
}
var bdw io.WriteCloser
if !typeUBI {
//
// FlushingWriter is needed due to a driver bug in the linux emmc driver
// OOM errors.
//
// Implements 'BlockDevicer' interface, and is hence the owner of the file
//
fw := NewFlushingWriter(out, uint64(nativeSsz))
//
// bdw owns the block-device.
// No-one else is allowed to touch it!
//
odw := &OptimizedBlockDeviceWriter{
blockDevice: fw,
}
//
// Buffers writes, and writes to the underlying writer
// once a buffer of size 'frameSize' is full.
//
bdw = &BlockFrameWriter{
frameSize: chunkSize,
buf: bytes.NewBuffer(nil),
w: odw,
}
} else {
// No optimized writes possible on UBI (Mirza)
// All the bytes have to be written
bdw = out
}
//
// The outermost writer. Makes sure that we never(!) write
// more than the size of the image.
//
b.w = &utils.LimitedWriteCloser{
W: bdw,
N: uint64(size),
}
return b, nil
}
// Write writes data 'b' to the underlying writer. Although this is just one
// line, the underlying implementation is currently slightly more involved. The
// BlockDevice writer will write to a chain of writers as follows:
//
// LimitWriter
// Make sure that no more than image-size
// bytes are written to the block-device.
// |
// |
// v
// BlockFrameWriter
// Buffers the writes into 'chunkSize' frames
// for writing to the underlying writer.
// |
// |
// v
// OptimizedBlockDeviceWriter
// Only writes dirty frames to the underlying block-device.
// Note: This is not done for UBI volumes
// |
// |
// v
// BlockDevicer
// This is an interface with all the main functionality
// of a file, and is in this case a FlushingWriter,
// which writes a chunk to the underlying file-descriptor,
// and then calls Sync() on every 'FlushIntervalBytes' written.
//
// Due to the underlying writer caching writes, the block-device needs to be
// closed, in order to make sure that all data has been flushed to the device.
func (bd *BlockDevice) Write(b []byte) (n int, err error) {
if bd.w == nil {
return 0, errors.New("No device")
}
n, err = bd.w.Write(b)
return n, err
}
// Close closes the underlying block device, thus automatically syncing any
// unwritten data. Othewise, behaves like io.Closer.
func (bd *BlockDevice) Close() error {
if bd.w == nil {
return nil
}
return bd.w.Close()
}
// Size queries the size of the underlying block device. Automatically opens a
// new fd in O_RDONLY mode, thus can be used in parallel to other operations.
func (bd *BlockDevice) Size() (uint64, error) {
out, err := os.OpenFile(bd.Path, os.O_RDONLY, 0)
if err != nil {
return 0, err
}
defer out.Close()
return BlockDeviceGetSizeOf(out)
}
// SectorSize queries the logical sector size of the underlying block device. Automatically opens a
// new fd in O_RDONLY mode, thus can be used in parallel to other operations.
func (bd *BlockDevice) SectorSize() (int, error) {
out, err := os.OpenFile(bd.Path, os.O_RDONLY, 0)
if err != nil {
return 0, err
}
defer out.Close()
return BlockDeviceGetSectorSizeOf(out)
}
type BlockFrameWriter struct {
buf *bytes.Buffer
frameSize int
w io.WriteCloser
}
// Write buffers the writes into a buffer of size 'frameSize'. Then, when this
// buffer is full, it writes 'frameSize' bytes to the underlying writer.
func (bw *BlockFrameWriter) Write(b []byte) (n int, err error) {
// Fill the frame buffer first
n, err = bw.buf.Write(b)
if err != nil {
return n, err
}
if bw.buf.Len() < bw.frameSize {
return n, nil // Chunk buffer not full
}
totWritten := 0
nFrames := bw.buf.Len() / bw.frameSize
for i := 0; i < nFrames; i++ {
n, err = bw.w.Write(bw.buf.Next(bw.frameSize))
totWritten += n
if err != nil {
return 0, err
}
}
// Report the cached bytes as written
totWritten += bw.buf.Len() % bw.frameSize
// Write a full frame, but report only the last byte chunk as written
return len(b), nil
}
// Close flushes the remaining cached bytes -- if any.
func (bw *BlockFrameWriter) Close() error {
_, err := bw.w.Write(bw.buf.Bytes())
if cerr := bw.w.Close(); cerr != nil {
return cerr
}
if err == io.EOF {
return nil
}
return err
}
// OptimizedBlockDeviceWriter wraps an underlying blockDevice write, however,
// with the optimization that it compares the bytes passed in to the Write
// method, with the next len([]byte) bytes (considered a frame) on the block
// device, and if they match, the write is discarded. The lingo is that only
// dirty frames are written. Clean ones are discarded.
type OptimizedBlockDeviceWriter struct {
blockDevice BlockDevicer
totalFrames int
dirtyFrames int
}
// Write only write 'dirty' frames.
// Note: a frame-size is always the size 'len(b)'
func (bd *OptimizedBlockDeviceWriter) Write(b []byte) (n int, err error) {
frameSize := int64(len(b))
payloadBuf := make([]byte, frameSize)
//
// Read len(b) bytes from the block-device
//
n, err = io.ReadFull(bd.blockDevice, payloadBuf)
if err != nil {
log.Errorf("Failed to read a full frame of size: %d from the block-device", err)
return 0, err
}
//
// Write the frame if it is dirty.
//
if !bytes.Equal(payloadBuf, b) {
// In order to write, we need to seek back to
// the start of the chunk.
if _, err = bd.blockDevice.Seek(-int64(frameSize), io.SeekCurrent); err != nil {
log.Errorf("Failed to seek back to the start of the frame. Err: %v", err)
return 0, err
}
bd.totalFrames += 1
bd.dirtyFrames += 1
return bd.blockDevice.Write(b)
}
// No need to write a clean frame
bd.totalFrames += 1
return n, err
}
func (obw *OptimizedBlockDeviceWriter) Close() error {
s := "The optimized block-device writer wrote a total of %d frames, " +
"where %d frames did need to be rewritten"
log.Infof(s, obw.totalFrames, obw.dirtyFrames)
return obw.blockDevice.Close()
}
// FlushingWriter is a wrapper around a BlockDevice which forces a Sync() to occur
// every FlushIntervalBytes.
type FlushingWriter struct {
BlockDevicer
FlushIntervalBytes uint64
unflushedBytesWritten uint64
}
// NewFlushingWriter returns a FlushingWriter which wraps the provided
// block-device (BlockDevicer) and automatically flushes (calls Sync()) each
// time the specified number of bytes is written. Setting flushIntervalBytes == 0
// causes Sync() to be called after every Write().
func NewFlushingWriter(wf *os.File, flushIntervalBytes uint64) *FlushingWriter {
return &FlushingWriter{
BlockDevicer: wf,
FlushIntervalBytes: flushIntervalBytes,
unflushedBytesWritten: 0,
}
}
func (fw *FlushingWriter) Write(p []byte) (int, error) {
rv, err := fw.BlockDevicer.Write(p)
fw.unflushedBytesWritten += uint64(rv)
if err != nil {
return rv, err
} else if fw.unflushedBytesWritten >= fw.FlushIntervalBytes {
err = fw.Sync()
}
return rv, err
}
func (fw *FlushingWriter) Sync() error {
err := fw.BlockDevicer.Sync()
fw.unflushedBytesWritten = 0
return err
}