Mercurial > hg > CbC > CbC_xv6
annotate src/impl/fs_impl.cbc @ 326:f0b337cb6024
use goto err->panic
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 08 Feb 2020 20:37:42 +0900 |
parents | 98902fad1e2e |
children | cb6045bce1d5 |
rev | line source |
---|---|
302 | 1 #include "types.h" |
301 | 2 #include "defs.h" |
302 | 3 #include "param.h" |
4 #include "stat.h" | |
5 #include "mmu.h" | |
6 #include "proc.h" | |
7 #include "spinlock.h" | |
8 #include "buf.h" | |
301 | 9 #include "fs.h" |
302 | 10 #include "file.h" |
326
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
11 #interface "Err.h" |
295 | 12 #interface "fs.dg" |
294 | 13 |
14 // ---- | |
15 // typedef struct fs_impl<Impl, Isa> impl fs{ | |
16 // union Data* fs_impl; | |
17 // | |
18 // | |
19 // | |
20 // | |
21 // } fs_impl; | |
22 // ---- | |
23 | |
24 fs* createfs_impl(struct Context* cbc_context) { | |
25 struct fs* fs = new fs(); | |
26 struct fs_impl* fs_impl = new fs_impl(); | |
27 fs->fs = (union Data*)fs_impl; | |
28 fs_impl->fs_impl = NULL; | |
321 | 29 fs_impl->sb = NULL; |
30 fs_impl->ret = 0; | |
31 fs_impl->dev = 0; | |
32 fs_impl->type = 0; | |
33 fs_impl->bp = NULL; | |
34 fs_impl->dip = NULL; | |
35 fs_impl->inum = 0; | |
36 fs_impl->dp = NULL; | |
37 fs_impl->name = NULL; | |
38 fs_impl->off = 0; | |
39 fs_impl->poff = NULL; | |
40 fs_impl->de = NULL; | |
41 fs_impl->tot = 0; | |
42 fs_impl->m = 0; | |
43 fs_impl->dst = NULL; | |
44 fs_impl->n = 0; | |
45 fs_impl->src = NULL; | |
302 | 46 fs_impl->allocinode = C_allocinode; |
47 fs_impl->allocinode_loop = C_allocinode_loop; | |
303 | 48 fs_impl->allocinode_loopcheck = C_allocinode_loopcheck; |
49 fs_impl->allocinode_noloop = C_allocinode_noloop; | |
321 | 50 fs_impl->lockinode1 = C_lockinode1; |
51 fs_impl->lockinode2 = C_lockinode2; | |
52 fs_impl->lockinode_sleepcheck = C_lockinode_sleepcheck; | |
53 fs_impl->iput_check = C_iput_check; | |
54 fs_impl->iput_inode_nolink = C_iput_inode_nolink; | |
55 fs_impl->readi_check_diskinode = C_readi_check_diskinode; | |
56 fs_impl->readi_loopcheck = C_readi_loopcheck; | |
57 fs_impl->readi_loop = C_readi_loop; | |
58 fs_impl->readi_noloop = C_readi_noloop; | |
59 fs_impl->writei_check_diskinode = C_writei_check_diskinode; | |
60 fs_impl->writei_loopcheck = C_writei_loopcheck; | |
61 fs_impl->writei_loop = C_writei_loop; | |
62 fs_impl->writei_noloop = C_writei_noloop; | |
63 fs_impl->dirlookup_loopcheck = C_dirlookup_loopcheck; | |
64 fs_impl->dirlookup_loop = C_dirlookup_loop; | |
65 fs_impl->dirlookup_noloop = C_dirlookup_noloop; | |
66 fs_impl->dirlink_namecheck = C_dirlink_namecheck; | |
67 fs_impl->dirlink_loopcheck = C_dirlink_loopcheck; | |
68 fs_impl->dirlink_loop = C_dirlink_loop; | |
69 fs_impl->dirlink_noloop = C_dirlink_noloop; | |
70 fs->readsb = C_readsbfs_impl; | |
71 fs->iinit = C_iinitfs_impl; | |
72 fs->ialloc = C_iallocfs_impl; | |
294 | 73 fs->iupdate = C_iupdatefs_impl; |
74 fs->idup = C_idupfs_impl; | |
75 fs->ilock = C_ilockfs_impl; | |
76 fs->iunlock = C_iunlockfs_impl; | |
77 fs->iput = C_iputfs_impl; | |
78 fs->iunlockput = C_iunlockputfs_impl; | |
79 fs->stati = C_statifs_impl; | |
80 fs->readi = C_readifs_impl; | |
81 fs->writei = C_writeifs_impl; | |
82 fs->namecmp = C_namecmpfs_impl; | |
83 fs->dirlookup = C_dirlookupfs_impl; | |
296 | 84 fs->dirlink = C_dirlinkfs_impl; |
294 | 85 fs->namei = C_nameifs_impl; |
86 fs->nameiparent = C_nameiparentfs_impl; | |
87 return fs; | |
88 } | |
298 | 89 |
299 | 90 typedef struct superblock superblock; |
296 | 91 __code readsbfs_impl(struct fs_impl* fs, uint dev, struct superblock* sb, __code next(...)) { //:skip |
294 | 92 |
301 | 93 struct buf* bp; |
94 | |
95 bp = bread(dev, 1); | |
96 memmove(sb, bp->data, sizeof(*sb)); | |
97 brelse(bp); | |
98 | |
294 | 99 goto next(...); |
100 } | |
101 | |
302 | 102 struct { |
103 struct spinlock lock; | |
104 struct inode inode[NINODE]; | |
105 } icache; | |
106 | |
294 | 107 __code iinitfs_impl(struct fs_impl* fs, __code next(...)) { |
108 | |
302 | 109 initlock(&icache.lock, "icache"); |
110 | |
294 | 111 goto next(...); |
112 } | |
113 | |
114 __code iallocfs_impl(struct fs_impl* fs, uint dev, short type, __code next(...)) { | |
305 | 115 goto allocinode(fs, dev, sb, next(...)); |
294 | 116 } |
117 | |
118 __code iupdatefs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { | |
119 | |
305 | 120 struct buf *bp; |
121 struct dinode *dip; | |
122 | |
123 bp = bread(ip->dev, IBLOCK(ip->inum)); | |
124 | |
125 dip = (struct dinode*) bp->data + ip->inum % IPB; | |
126 dip->type = ip->type; | |
127 dip->major = ip->major; | |
128 dip->minor = ip->minor; | |
129 dip->nlink = ip->nlink; | |
130 dip->size = ip->size; | |
131 | |
132 memmove(dip->addrs, ip->addrs, sizeof(ip->addrs)); | |
133 log_write(bp); | |
134 brelse(bp); | |
135 | |
136 | |
294 | 137 goto next(...); |
138 } | |
139 | |
140 __code idupfs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { | |
141 | |
305 | 142 acquire(&icache.lock); |
143 ip->ref++; | |
144 release(&icache.lock); | |
145 | |
146 goto next(ip, ...); | |
147 | |
294 | 148 } |
149 | |
150 __code ilockfs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { | |
151 | |
306 | 152 goto lockinode1(fs, ip, bp, dip, next(...)); |
294 | 153 } |
154 | |
155 __code iunlockfs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { | |
307 | 156 |
157 if (ip == 0 || !(ip->flags & I_BUSY) || ip->ref < 1) { | |
326
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
158 char* msg = "iunlock"; |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
159 struct Err* err = createKernelError(&proc->cbc_context); |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
160 Gearef(cbc_context, Err)->msg = msg; |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
161 goto meta(cbc_context, err->panic); |
307 | 162 } |
163 | |
164 acquire(&icache.lock); | |
165 ip->flags &= ~I_BUSY; | |
166 wakeup(ip); | |
167 release(&icache.lock); | |
294 | 168 |
169 goto next(...); | |
170 } | |
171 | |
172 __code iputfs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { | |
316 | 173 if (next == C_iputfs_impl) { |
174 next = fs->next2; | |
175 } | |
307 | 176 goto iput_check(fs, ip, next(...)); |
177 | |
294 | 178 } |
179 | |
316 | 180 __code iunlockputfs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { |
181 fs->next2 = next; | |
182 goto iunlockfs_impl(ip, fs->iput, ...); | |
294 | 183 } |
184 | |
299 | 185 typedef struct stat stat; |
296 | 186 __code statifs_impl(struct fs_impl* fs , struct inode* ip, struct stat* st, __code next(...)) { //:skip |
308 | 187 st->dev = ip->dev; |
188 st->ino = ip->inum; | |
189 st->type = ip->type; | |
190 st->nlink = ip->nlink; | |
191 st->size = ip->size; | |
294 | 192 goto next(...); |
193 } | |
194 | |
319 | 195 __code readifs_impl(struct fs_impl* fs, struct inode* ip, char* dst, uint off, uint tot, uint n, __code next(int ret, ...)) { |
317 | 196 if (ip->type == T_DEV) { |
318 | 197 goto readi_check_diskinode(fs, ip, dst, n, next(...)); |
317 | 198 } |
294 | 199 |
317 | 200 if (off > ip->size || off + n < off) { |
201 ret = -1; | |
202 goto next(ret, ...); | |
203 } | |
204 | |
205 if (off + n > ip->size) { | |
206 n = ip->size - off; | |
207 } | |
319 | 208 Gearef(cbc_context, fs)->tot = 0; |
317 | 209 goto readi_loopcheck(fs, tot, m, dst, off, n, next(...)); |
294 | 210 } |
211 | |
320 | 212 __code writeifs_impl(struct fs_impl* fs, struct inode* ip, char* src, uint off, uint tot, uint n, __code next(int ret, ...)) { |
213 if (ip->type == T_DEV) { | |
214 goto writei_check_diskinode(fs, ip, src, n, next(...)); | |
215 } | |
216 | |
217 if (off > ip->size || off + n < off) { | |
218 ret = -1; | |
219 goto next(ret, ...); | |
220 } | |
221 | |
222 if (off + n > MAXFILE * BSIZE) { | |
223 ret = -1; | |
224 goto next(ret, ...); | |
225 } | |
321 | 226 Gearef(cbc_context, fs)->tot = 0; |
320 | 227 goto writei_loopcheck(fs, tot, m, src, off, n, next(...)); |
228 } | |
294 | 229 |
230 | |
310 | 231 __code namecmpfs_impl(struct fs_impl* fs, const char* s, const char* t, __code next(int strncmp_val, ...)) { |
232 strncmp_val = strncmp(s, t, DIRSIZ); | |
233 goto next(strncmp_val, ...); | |
294 | 234 } |
235 | |
312 | 236 __code dirlookupfs_impl(struct fs_impl* fs, struct inode* dp, char* name, uint off, uint* poff, dirent* de, __code next(...)) { //:skip |
311 | 237 if (dp->type != T_DIR) { |
326
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
238 char* msg = "dirlookup not DIR"; |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
239 struct Err* err = createKernelError(&proc->cbc_context); |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
240 Gearef(cbc_context, Err)->msg = msg; |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
241 goto meta(cbc_context, err->panic); |
311 | 242 } |
312 | 243 Gearef(cbc_context, fs)->off = 0; |
244 goto dirlookup_loopcheck(fs, dp, name, off, poff, de, next(...)); | |
294 | 245 } |
246 | |
315 | 247 __code dirlinkfs_impl(struct fs_impl* fs, struct inode* ip, struct dirent* de, struct inode* dp, char* name, uint off, uint inum, __code next(...)) { //:skip |
248 // Check that name is not present. | |
249 if ((ip = dirlookup(dp, name, 0)) != 0) { | |
250 goto dirlink_namecheck(fs, ip, next(...)); | |
251 } | |
316 | 252 Gearef(cbc_context, fs)->off = 0; |
315 | 253 goto dirlink_loopcheck(fs, de, dp, off, next(...)); |
294 | 254 } |
255 | |
309 | 256 static struct inode* iget (uint dev, uint inum) |
257 { | |
258 struct inode *ip, *empty; | |
259 | |
260 acquire(&icache.lock); | |
261 | |
262 // Is the inode already cached? | |
263 empty = 0; | |
264 | |
265 for (ip = &icache.inode[0]; ip < &icache.inode[NINODE]; ip++) { | |
266 if (ip->ref > 0 && ip->dev == dev && ip->inum == inum) { | |
267 ip->ref++; | |
268 release(&icache.lock); | |
269 return ip; | |
270 } | |
271 | |
272 if (empty == 0 && ip->ref == 0) { // Remember empty slot. | |
273 empty = ip; | |
274 } | |
275 } | |
276 | |
277 // Recycle an inode cache entry. | |
278 if (empty == 0) { | |
279 panic("iget: no inodes"); | |
280 } | |
281 | |
282 ip = empty; | |
283 ip->dev = dev; | |
284 ip->inum = inum; | |
285 ip->ref = 1; | |
286 ip->flags = 0; | |
287 release(&icache.lock); | |
288 | |
289 return ip; | |
290 } | |
308 | 291 |
292 static char* skipelem (char *path, char *name) | |
293 { | |
294 char *s; | |
295 int len; | |
296 | |
297 while (*path == '/') { | |
298 path++; | |
299 } | |
300 | |
301 if (*path == 0) { | |
302 return 0; | |
303 } | |
304 | |
305 s = path; | |
294 | 306 |
308 | 307 while (*path != '/' && *path != 0) { |
308 path++; | |
309 } | |
310 | |
311 len = path - s; | |
312 | |
313 if (len >= DIRSIZ) { | |
314 memmove(name, s, DIRSIZ); | |
315 } else { | |
316 memmove(name, s, len); | |
317 name[len] = 0; | |
318 } | |
319 | |
320 while (*path == '/') { | |
321 path++; | |
322 } | |
323 | |
324 return path; | |
294 | 325 } |
326 | |
308 | 327 |
328 static struct inode* namex (char *path, int nameiparent, char *name) | |
329 { | |
330 struct inode *ip, *next; | |
331 | |
332 if (*path == '/') { | |
333 ip = iget(ROOTDEV, ROOTINO); | |
334 } else { | |
335 ip = idup(proc->cwd); | |
336 } | |
337 | |
338 while ((path = skipelem(path, name)) != 0) { | |
339 ilock(ip); | |
340 | |
341 if (ip->type != T_DIR) { | |
342 iunlockput(ip); | |
343 return 0; | |
344 } | |
294 | 345 |
308 | 346 if (nameiparent && *path == '\0') { |
347 // Stop one level early. | |
348 iunlock(ip); | |
349 return ip; | |
350 } | |
351 | |
352 if ((next = dirlookup(ip, name, 0)) == 0) { | |
353 iunlockput(ip); | |
354 return 0; | |
355 } | |
356 | |
357 iunlockput(ip); | |
358 ip = next; | |
359 } | |
360 | |
361 if (nameiparent) { | |
362 iput(ip); | |
363 return 0; | |
364 } | |
365 | |
366 return ip; | |
294 | 367 } |
368 | |
308 | 369 __code nameifs_impl(struct fs_impl* fs, char* path, __code next(int namex_val, ...)) { |
370 char name[DIRSIZ]; | |
371 namex_val = namex(path, 0, name); | |
372 goto next(namex_val, ...); | |
373 } | |
374 | |
375 __code nameiparentfs_impl(struct fs_impl* fs, char* path, char* name, __code next(int namex_val, ...)) { | |
376 | |
377 namex_val = namex(path, 1, name); | |
378 goto next(namex_val, ...); | |
379 | |
380 } | |
381 |