-
Notifications
You must be signed in to change notification settings - Fork 4
/
nfs.go
136 lines (119 loc) · 3.37 KB
/
nfs.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
package nfs
import (
"github.com/tchajed/goose/machine/disk"
"github.com/mit-pdos/go-journal/buf"
"github.com/mit-pdos/go-journal/common"
"github.com/mit-pdos/go-journal/obj"
"github.com/mit-pdos/go-journal/util"
"github.com/mit-pdos/go-nfsd/dir"
"github.com/mit-pdos/go-nfsd/fstxn"
"github.com/mit-pdos/go-nfsd/inode"
"github.com/mit-pdos/go-nfsd/shrinker"
"github.com/mit-pdos/go-nfsd/super"
"github.com/mit-pdos/go-nfsd/util/stats"
)
type Nfs struct {
fsstate *fstxn.FsState
shrinkst *shrinker.ShrinkerSt
// support unstable writes
Unstable bool
// statistics
stats [NUM_NFS_OPS]stats.Op
}
func MakeNfs(d disk.Disk) *Nfs {
// run first so that disk is initialized before mkLog
super := super.MkFsSuper(d)
util.DPrintf(1, "Super: "+
"Size %d NBlockBitmap %d NInodeBitmap %d Maxaddr %d\n",
d.Size(),
super.NBlockBitmap, super.NInodeBitmap, super.Maxaddr)
log := obj.MkLog(d) // runs recovery
i := readRootInode(super)
if i.Kind == 0 { // make a new file system?
makeFs(super)
}
st := fstxn.MkFsState(super, log)
nfs := &Nfs{
fsstate: st,
shrinkst: shrinker.MkShrinkerSt(st),
Unstable: true,
}
if i.Kind == 0 {
nfs.makeRootDir()
}
return nfs
}
func (nfs *Nfs) ShutdownNfs() {
util.DPrintf(1, "Shutdown\n")
nfs.shrinkst.Shutdown()
nfs.fsstate.Txn.Shutdown()
util.DPrintf(1, "Shutdown done\n")
}
// Terminates shrinker thread immediately
func (nfs *Nfs) Crash() {
util.DPrintf(0, "Crash: terminate shrinker\n")
nfs.shrinkst.Crash()
nfs.ShutdownNfs()
}
func (nfs *Nfs) makeRootDir() {
op := fstxn.Begin(nfs.fsstate)
ip := op.GetInodeInumFree(common.ROOTINUM)
if ip == nil {
panic("makeRootDir")
}
dir.MkRootDir(ip, op)
ok := op.Commit()
if !ok {
panic("makeRootDir")
}
}
// Make an empty file system
func makeFs(super *super.FsSuper) {
util.DPrintf(1, "mkfs")
root := inode.MkRootInode()
util.DPrintf(1, "root %v\n", root)
raddr := super.Inum2Addr(common.ROOTINUM)
rootblk := root.Encode()
rootbuf := buf.MkBuf(raddr, common.INODESZ*8, rootblk)
rootbuf.WriteDirect(super.Disk)
markAlloc(super, super.DataStart(), super.MaxBnum())
}
func markAlloc(super *super.FsSuper, n common.Bnum, m common.Bnum) {
util.DPrintf(1, "markAlloc: [0, %d) and [%d,%d)\n", n, m,
super.NBlockBitmap*common.NBITBLOCK)
if n >= common.Bnum(common.NBITBLOCK) ||
m >= common.Bnum(common.NBITBLOCK*super.NBlockBitmap) ||
m < n {
panic("markAlloc: configuration makes no sense")
}
blk := make(disk.Block, disk.BlockSize)
for bn := uint64(0); bn < uint64(n); bn++ {
byte := bn / 8
bit := bn % 8
blk[byte] = blk[byte] | 1<<bit
}
super.Disk.Write(uint64(super.BitmapBlockStart()), blk)
var blk1 = blk
blkno := m/common.Bnum(common.NBITBLOCK) + super.BitmapBlockStart()
if blkno > super.BitmapBlockStart() {
blk1 = make(disk.Block, disk.BlockSize)
}
for bn := uint64(m) % common.NBITBLOCK; bn < common.NBITBLOCK; bn++ {
byte := bn / 8
bit := bn % 8
blk1[byte] = blk1[byte] | 1<<bit
}
super.Disk.Write(uint64(blkno), blk1)
// mark inode 0 and 1 as allocated
blk2 := make(disk.Block, disk.BlockSize)
blk2[0] = blk2[0] | 1<<0
blk2[0] = blk2[0] | 1<<1
super.Disk.Write(uint64(super.BitmapInodeStart()), blk2)
}
func readRootInode(super *super.FsSuper) *inode.Inode {
addr := super.Inum2Addr(common.ROOTINUM)
blk := super.Disk.Read(uint64(addr.Blkno))
buf := buf.MkBufLoad(addr, common.INODESZ*8, blk)
i := inode.Decode(buf, common.ROOTINUM)
return i
}