302
|
1 #include "types.h"
|
|
2 #include "defs.h"
|
|
3 #include "param.h"
|
|
4 #include "stat.h"
|
|
5 #include "mmu.h"
|
|
6 #include "proc.h"
|
|
7 #include "spinlock.h"
|
|
8 #include "buf.h"
|
|
9 #include "fs.h"
|
|
10 #include "file.h"
|
|
11 #interface "fs_impl.h"
|
|
12
|
|
13 /*
|
|
14 fs_impl* createfs_impl2();
|
|
15 */
|
|
16
|
304
|
17 __code allocinode(struct fs_impl* fs_impl, uint dev, struct superblock* sb, __code next(int iget_val, ...)){ //:skip
|
302
|
18
|
|
19 readsb(dev, sb);
|
303
|
20 Gearef(cbc_context, fs_impl)->inum = 1;
|
304
|
21 goto allocinode_loopcheck(fs_impl, inum, dev, sb, bp, dip, next(iget_val, ...));
|
302
|
22
|
|
23 }
|
|
24
|
|
25 typedef struct buf buf;
|
|
26 typedef struct dinode dinode;
|
303
|
27
|
304
|
28 __code allocinode_loopcheck(struct fs_impl* fs_impl, int inum, uint dev, struct superblock* sb, struct buf* bp, struct dinode* dip, __code next(int iget_val, ...)){ //:skip
|
303
|
29 if( inum < sb->ninodes){
|
304
|
30 goto allocinode_loop(fs_impl, inum, dev, type, sb, bp, dip, next(iget_val, ...));
|
303
|
31 }
|
|
32 /*
|
|
33 goto cbc_context->panic(...);
|
|
34 */
|
|
35 }
|
302
|
36
|
304
|
37 __code allocinode_loop(struct fs_impl* fs_impl, int inum, uint dev, short type, struct superblock* sb, struct buf* bp, struct dinode* dip, __code next(int iget_val, ...)){ //:skip
|
303
|
38 bp = bread(dev, IBLOCK(inum));
|
|
39 dip = (struct dinode*) bp->data + inum % IPB;
|
|
40 if(dip->type = 0){
|
304
|
41 goto allocinode_noloop(fs_impl, inum, dev, sb, bp, dip, next(iget_val, ...));
|
303
|
42 }
|
|
43
|
|
44 brelse(bp);
|
|
45 inum++;
|
304
|
46 goto allocinode_loopcheck(fs_impl, inum, dev, type, sb, bp, dip, next(iget_val, ...));
|
303
|
47 }
|
|
48
|
304
|
49 struct {
|
|
50 struct spinlock lock;
|
|
51 struct inode inode[NINODE];
|
|
52 } icache;
|
|
53
|
|
54 static struct inode* iget (uint dev, uint inum)
|
|
55 {
|
|
56 struct inode *ip, *empty;
|
|
57
|
|
58 acquire(&icache.lock);
|
|
59
|
|
60 // Is the inode already cached?
|
|
61 empty = 0;
|
|
62
|
|
63 for (ip = &icache.inode[0]; ip < &icache.inode[NINODE]; ip++) {
|
|
64 if (ip->ref > 0 && ip->dev == dev && ip->inum == inum) {
|
|
65 ip->ref++;
|
|
66 release(&icache.lock);
|
|
67 return ip;
|
|
68 }
|
|
69
|
|
70 if (empty == 0 && ip->ref == 0) { // Remember empty slot.
|
|
71 empty = ip;
|
|
72 }
|
|
73 }
|
|
74
|
|
75 // Recycle an inode cache entry.
|
|
76 if (empty == 0) {
|
|
77 panic("iget: no inodes");
|
|
78 }
|
|
79
|
|
80 ip = empty;
|
|
81 ip->dev = dev;
|
|
82 ip->inum = inum;
|
|
83 ip->ref = 1;
|
|
84 ip->flags = 0;
|
|
85 release(&icache.lock);
|
|
86
|
|
87 return ip;
|
|
88 }
|
|
89
|
|
90 __code allocinode_noloop(struct fs_impl* fs_impl, int inum, uint dev, short type, struct superblock* sb, struct buf* bp, struct dinode* dip, __code next(int iget_val, ...)){ //:skip
|
303
|
91
|
|
92 memset(dip, 0, sizeof(*dip));
|
|
93 dip->type = type;
|
|
94 log_write(bp);
|
|
95 brelse(bp);
|
|
96
|
304
|
97 iget_val = iget(dev, inum);
|
303
|
98 goto next(iget_val, ...);
|
302
|
99
|
|
100 }
|
303
|
101
|
306
|
102 __code lockinode1(struct fs_impl* fs_impl, struct inode* ip, struct buf* bp, struct dinode* dip, __code next(...)){ //:skip
|
|
103
|
|
104 if (ip == 0 || ip->ref < 1) {
|
|
105
|
|
106 /*
|
|
107 panic("ilock");
|
|
108 goto panic();
|
|
109 */
|
|
110 }
|
|
111 acquire(&icache.lock);
|
|
112
|
|
113 goto lockinode_sleepcheck(fs_impl, ip, next(...));
|
|
114
|
|
115 }
|
|
116
|
307
|
117
|
|
118 __code lockinode2(struct fs_impl* fs_impl, struct inode* ip, struct buf* bp, struct dinode* dip, __code next(...)){ //:skip
|
|
119
|
|
120 ip->flags |= I_BUSY;
|
|
121 release(&icache.lock);
|
|
122
|
|
123 if (!(ip->flags & I_VALID)) {
|
|
124 bp = bread(ip->dev, IBLOCK(ip->inum));
|
|
125
|
|
126 dip = (struct dinode*) bp->data + ip->inum % IPB;
|
|
127 ip->type = dip->type;
|
|
128 ip->major = dip->major;
|
|
129 ip->minor = dip->minor;
|
|
130 ip->nlink = dip->nlink;
|
|
131 ip->size = dip->size;
|
|
132
|
|
133 memmove(ip->addrs, dip->addrs, sizeof(ip->addrs));
|
|
134 brelse(bp);
|
|
135 ip->flags |= I_VALID;
|
|
136
|
|
137 if (ip->type == 0) {
|
|
138 /*
|
|
139 panic("ilock: no type");
|
|
140 goto panic();
|
|
141 */
|
|
142 }
|
|
143 }
|
|
144 goto next(...);
|
|
145 }
|
306
|
146 __code lockinode_sleepcheck(struct fs_impl* fs_impl, struct inode* ip, __code next(...)){
|
|
147 if(ip->flags & I_BUSY){
|
|
148 sleep(ip, &icache.lock);
|
|
149 goto lockinode_sleepcheck(fs_impl, ip, next(...));
|
|
150 }
|
307
|
151 goto lockinode2(fs_impl, ip, bp, dip, next(...));
|
|
152 }
|
|
153
|
|
154 __code iput_check(struct fs_impl* fs_impl, struct inode* ip, __code next(...)){
|
|
155 acquire(&icache.lock);
|
|
156 if (ip->ref == 1 && (ip->flags & I_VALID) && ip->nlink == 0) {
|
|
157 goto iput_inode_nolink(fs_impl, ip, next(...));
|
|
158 }
|
|
159 ip->ref--;
|
|
160 release(&icache.lock);
|
|
161 goto next(...);
|
|
162
|
|
163 }
|
|
164
|
|
165 static void bfree (int dev, uint b)
|
|
166 {
|
|
167 struct buf *bp;
|
|
168 struct superblock sb;
|
|
169 int bi, m;
|
|
170
|
|
171 readsb(dev, &sb);
|
|
172 bp = bread(dev, BBLOCK(b, sb.ninodes));
|
|
173 bi = b % BPB;
|
|
174 m = 1 << (bi % 8);
|
|
175
|
|
176 if ((bp->data[bi / 8] & m) == 0) {
|
|
177 panic("freeing free block");
|
|
178 }
|
|
179
|
|
180 bp->data[bi / 8] &= ~m;
|
|
181 log_write(bp);
|
|
182 brelse(bp);
|
|
183 }
|
|
184
|
|
185
|
|
186 static void itrunc (struct inode *ip)
|
|
187 {
|
|
188 int i, j;
|
|
189 struct buf *bp;
|
|
190 uint *a;
|
|
191
|
|
192 for (i = 0; i < NDIRECT; i++) {
|
|
193 if (ip->addrs[i]) {
|
|
194 bfree(ip->dev, ip->addrs[i]);
|
|
195 ip->addrs[i] = 0;
|
|
196 }
|
|
197 }
|
|
198
|
|
199 if (ip->addrs[NDIRECT]) {
|
|
200 bp = bread(ip->dev, ip->addrs[NDIRECT]);
|
|
201 a = (uint*) bp->data;
|
|
202
|
|
203 for (j = 0; j < NINDIRECT; j++) {
|
|
204 if (a[j]) {
|
|
205 bfree(ip->dev, a[j]);
|
|
206 }
|
|
207 }
|
|
208
|
|
209 brelse(bp);
|
|
210 bfree(ip->dev, ip->addrs[NDIRECT]);
|
|
211 ip->addrs[NDIRECT] = 0;
|
|
212 }
|
|
213
|
|
214 ip->size = 0;
|
|
215 iupdate(ip);
|
|
216 }
|
|
217
|
|
218 __code iput_inode_nolink(struct fs_impl* fs_impl, struct inode* ip, __code next(...)){
|
|
219
|
|
220 if (ip->flags & I_BUSY) {
|
|
221 /*
|
|
222 panic("iput busy");
|
|
223 */
|
|
224 }
|
|
225
|
|
226 ip->flags |= I_BUSY;
|
|
227 release(&icache.lock);
|
|
228 itrunc(ip);
|
|
229 ip->type = 0;
|
|
230 iupdate(ip);
|
|
231
|
|
232 acquire(&icache.lock);
|
|
233 ip->flags = 0;
|
|
234 wakeup(ip);
|
305
|
235 goto next(...);
|
|
236 }
|
306
|
237
|
307
|
238
|