aboutsummaryrefslogtreecommitdiff
path: root/pathfs_frontend/file.go
blob: a4ed30bb21e234b76b21abfc22a0b59ce1946de8 (plain)
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
package pathfs_frontend

import (
	"io"
	"bytes"
	"fmt"
	"os"
	"sync"
	"syscall"
	"time"

	"github.com/hanwen/go-fuse/fuse"
	"github.com/hanwen/go-fuse/fuse/nodefs"
	"github.com/rfjakob/gocryptfs/cryptfs"
)

// File - based on loopbackFile in go-fuse/fuse/nodefs/files.go
type file struct {
	fd *os.File

	// os.File is not threadsafe. Although fd themselves are
	// constant during the lifetime of an open file, the OS may
	// reuse the fd number after it is closed. When open races
	// with another close, they may lead to confusion as which
	// file gets written in the end.
	lock sync.Mutex

	// Was the file opened O_WRONLY?
	writeOnly bool

	// Parent CryptFS
	cfs *cryptfs.CryptFS
}

func NewFile(fd *os.File, writeOnly bool, cfs *cryptfs.CryptFS) nodefs.File {
	return &file{
		fd: fd,
		writeOnly: writeOnly,
		cfs: cfs,
	}
}

func (f *file) InnerFile() nodefs.File {
	return nil
}

func (f *file) SetInode(n *nodefs.Inode) {
}

func (f *file) String() string {
	return fmt.Sprintf("cryptFile(%s)", f.fd.Name())
}

// doRead - returns "length" plaintext bytes from plaintext offset "offset".
// Reads the corresponding ciphertext from disk, decryptfs it, returns the relevant
// part.
//
// Called by Read(), for RMW in Write() and Truncate()
func (f *file) doRead(off uint64, length uint64) ([]byte, fuse.Status) {

	// Read the backing ciphertext in one go
	alignedOffset, alignedLength, skip := f.cfs.CiphertextRange(off, length)
	cryptfs.Debug.Printf("CiphertextRange(%d, %d) -> %d, %d, %d\n", off, length, alignedOffset, alignedLength, skip)
	ciphertext := make([]byte, int(alignedLength))
	f.lock.Lock()
	n, err := f.fd.ReadAt(ciphertext, int64(alignedOffset))
	f.lock.Unlock()
	ciphertext = ciphertext[0:n]
	if err != nil && err != io.EOF {
		cryptfs.Warn.Printf("read: ReadAt: %s\n", err.Error())
		return nil, fuse.ToStatus(err)
	}
	cryptfs.Debug.Printf("ReadAt offset=%d length=%d -> n=%d len=%d\n", alignedLength, alignedOffset, n, len(ciphertext))

	// Decrypt it
	plaintext, err := f.cfs.DecryptBlocks(ciphertext)
	if err != nil {
		cryptfs.Warn.Printf("Corrupt block at offset=%d\n", off + uint64(len(plaintext)))
		cryptfs.Warn.Printf("doRead: returning IO error\n")
		return nil, fuse.EIO
	}

	// Crop down to the relevant part
	var out []byte
	lenHave := len(plaintext)
	lenWant := skip + int(length)
	if lenHave > lenWant {
		out = plaintext[skip:skip +  int(length)]
	} else if lenHave > skip {
		out = plaintext[skip:lenHave]
	} else {
		// Out stays empty, file was smaller than the requested offset
	}

	return out, fuse.OK
}

// Read - FUSE call
func (f *file) Read(buf []byte, off int64) (resultData fuse.ReadResult, code fuse.Status) {
	cryptfs.Debug.Printf("Read %s: offset=%d length=%d\n", f.fd.Name(), len(buf), off)

	if f.writeOnly {
		cryptfs.Warn.Printf("Tried to read from write-only file\n")
		return nil, fuse.EBADF
	}

	out, status := f.doRead(uint64(off), uint64(len(buf)))

	if status == fuse.EIO {
		cryptfs.Warn.Printf("Read failed with EIO: file %s, offset=%d, length=%d\n", f.fd.Name(), len(buf), off)
	}
	if status != fuse.OK {
		return nil, status
	}

	cryptfs.Debug.Printf("Read: status %v, returning %d bytes\n", status, len(out))
	return fuse.ReadResultData(out), status
}

// Write - FUSE call
func (f *file) Write(data []byte, off int64) (uint32, fuse.Status) {
	cryptfs.Debug.Printf("Write %s: offset=%d length=%d\n", f.fd.Name(), off, len(data))

	var written uint32
	status := fuse.OK
	dataBuf := bytes.NewBuffer(data)
	blocks := f.cfs.SplitRange(uint64(off), uint64(len(data)))
	for _, b := range(blocks) {

		blockData := dataBuf.Next(int(b.Length))

		// Incomplete block -> Read-Modify-Write
		if b.IsPartial() {
			// Read
			o, _ := b.PlaintextRange()
			oldData, status := f.doRead(o, f.cfs.PlainBS())
			if status != fuse.OK {
				cryptfs.Warn.Printf("RMW read failed: %s\n", status.String())
				return written, status
			}
			// Modify
			blockData = f.cfs.MergeBlocks(oldData, blockData, int(b.Offset))
			cryptfs.Debug.Printf("len(oldData)=%d len(blockData)=%d\n", len(oldData), len(blockData))
		}

		// Write
		blockOffset, _ := b.CiphertextRange()
		blockData = f.cfs.EncryptBlock(blockData)
		cryptfs.Debug.Printf("WriteAt offset=%d length=%d\n", blockOffset, len(blockData))
		f.lock.Lock()
		_, err := f.fd.WriteAt(blockData, int64(blockOffset))
		f.lock.Unlock()

		if err != nil {
			cryptfs.Warn.Printf("Write failed: %s\n", err.Error())
			status = fuse.ToStatus(err)
			break
		}
		written += uint32(b.Length)
	}

	return written, status
}

// Release - FUSE call, forget file
func (f *file) Release() {
	f.lock.Lock()
	f.fd.Close()
	f.lock.Unlock()
}

// Flush - FUSE call
func (f *file) Flush() fuse.Status {
	f.lock.Lock()

	// Since Flush() may be called for each dup'd fd, we don't
	// want to really close the file, we just want to flush. This
	// is achieved by closing a dup'd fd.
	newFd, err := syscall.Dup(int(f.fd.Fd()))
	f.lock.Unlock()

	if err != nil {
		return fuse.ToStatus(err)
	}
	err = syscall.Close(newFd)
	return fuse.ToStatus(err)
}

func (f *file) Fsync(flags int) (code fuse.Status) {
	f.lock.Lock()
	r := fuse.ToStatus(syscall.Fsync(int(f.fd.Fd())))
	f.lock.Unlock()

	return r
}

func (f *file) Truncate(newSize uint64) fuse.Status {

	// Common case: Truncate to zero
	if newSize == 0 {
		f.lock.Lock()
		err := syscall.Ftruncate(int(f.fd.Fd()), 0)
		f.lock.Unlock()
		return fuse.ToStatus(err)
	}

	// We need the old file size to determine if we are growing or shrinking
	// the file
	fi, err := f.fd.Stat()
	if err != nil {
		cryptfs.Warn.Printf("Truncate: fstat failed: %v\n", err)
		return fuse.ToStatus(err)
	}
	oldSize := uint64(fi.Size())

	// Grow file by appending zeros
	if newSize > oldSize {
		remaining := newSize - oldSize
		offset := oldSize
		var zeros []byte
		// Append a maximum of 1MB in each iteration
		if remaining > 1048576 {
			zeros = make([]byte, 1048576)
		} else {
			zeros = make([]byte, remaining)
		}
		for remaining >= uint64(len(zeros)) {
			written, status := f.Write(zeros, int64(offset))
			if status != fuse.OK {
				return status
			}
			remaining -= uint64(written)
			offset += uint64(written)
			cryptfs.Debug.Printf("Truncate: written=%d remaining=%d offset=%d\n",
				written, remaining, offset)
		}
		if remaining > 0 {
			_, status := f.Write(zeros[0:remaining], int64(offset))
			return status
		}
		return fuse.OK
	}

	// Shrink file by truncating
	newBlockLen := int(newSize % f.cfs.PlainBS())
	// New file size is aligned to block size - just truncate
	if newBlockLen == 0 {
		cSize := int64(f.cfs.CipherSize(newSize))
		f.lock.Lock()
		err := syscall.Ftruncate(int(f.fd.Fd()), cSize)
		f.lock.Unlock()
		return fuse.ToStatus(err)
	}
	// New file size is not aligned - need to do RMW on the last block
	var blockOffset, blockLen uint64
	{
		// Get the block the last byte belongs to.
		// This is, by definition, the last block.
		blockList := f.cfs.SplitRange(newSize - 1, 1)
		lastBlock := blockList[0]
		blockOffset, blockLen = lastBlock.PlaintextRange()
	}
	blockData, status := f.doRead(blockOffset, blockLen)
	if status != fuse.OK {
		cryptfs.Warn.Printf("Truncate: doRead failed: %v\n", err)
		return status
	}
	if len(blockData) < newBlockLen {
		cryptfs.Warn.Printf("Truncate: file has shrunk under our feet\n")
		return fuse.OK
	}
	// Truncate the file down to the next block
	{
		nextBlockSz := int64(f.cfs.CipherSize(newSize - uint64(newBlockLen)))
		f.lock.Lock()
		err = syscall.Ftruncate(int(f.fd.Fd()), nextBlockSz)
		f.lock.Unlock()
		if err != nil {
			cryptfs.Warn.Printf("Truncate: Intermediate Ftruncate failed: %v\n", err)
			return fuse.ToStatus(err)
		}
	}
	// Append truncated last block
	_, status = f.Write(blockData[0:newBlockLen], int64(blockOffset))
	return status
}

func (f *file) Chmod(mode uint32) fuse.Status {
	f.lock.Lock()
	r := fuse.ToStatus(f.fd.Chmod(os.FileMode(mode)))
	f.lock.Unlock()

	return r
}

func (f *file) Chown(uid uint32, gid uint32) fuse.Status {
	f.lock.Lock()
	r := fuse.ToStatus(f.fd.Chown(int(uid), int(gid)))
	f.lock.Unlock()

	return r
}

func (f *file) GetAttr(a *fuse.Attr) fuse.Status {
	cryptfs.Debug.Printf("file.GetAttr()\n")
	st := syscall.Stat_t{}
	f.lock.Lock()
	err := syscall.Fstat(int(f.fd.Fd()), &st)
	f.lock.Unlock()
	if err != nil {
		return fuse.ToStatus(err)
	}
	a.FromStat(&st)
	a.Size = f.cfs.PlainSize(a.Size)

	return fuse.OK
}

func (f *file) Allocate(off uint64, sz uint64, mode uint32) fuse.Status {
	f.lock.Lock()
	err := syscall.Fallocate(int(f.fd.Fd()), mode, int64(off), int64(sz))
	f.lock.Unlock()
	if err != nil {
		return fuse.ToStatus(err)
	}
	return fuse.OK
}

const _UTIME_NOW = ((1 << 30) - 1)
const _UTIME_OMIT = ((1 << 30) - 2)

func (f *file) Utimens(a *time.Time, m *time.Time) fuse.Status {
	tv := make([]syscall.Timeval, 2)
	if a == nil {
		tv[0].Usec = _UTIME_OMIT
	} else {
		n := a.UnixNano()
		tv[0] = syscall.NsecToTimeval(n)
	}

	if m == nil {
		tv[1].Usec = _UTIME_OMIT
	} else {
		n := a.UnixNano()
		tv[1] = syscall.NsecToTimeval(n)
	}

	f.lock.Lock()
	err := syscall.Futimes(int(f.fd.Fd()), tv)
	f.lock.Unlock()
	return fuse.ToStatus(err)
}