Mercurial > hg > CbC > CbC_xv6
annotate src/impl/fs_impl.cbc @ 338:2cbaa4c74d15
remove_skip
author | anatofuz |
---|---|
date | Sun, 16 Feb 2020 18:30:06 +0900 |
parents | cb6045bce1d5 |
children |
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; |
338 | 91 __code readsbfs_impl(struct fs_impl* fs, uint dev, struct superblock* sb, __code next(...)) { |
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 | |
337 | 118 typedef struct inode inode; |
294 | 119 __code iupdatefs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { |
120 | |
305 | 121 struct buf *bp; |
122 struct dinode *dip; | |
123 | |
124 bp = bread(ip->dev, IBLOCK(ip->inum)); | |
125 | |
126 dip = (struct dinode*) bp->data + ip->inum % IPB; | |
127 dip->type = ip->type; | |
128 dip->major = ip->major; | |
129 dip->minor = ip->minor; | |
130 dip->nlink = ip->nlink; | |
131 dip->size = ip->size; | |
132 | |
133 memmove(dip->addrs, ip->addrs, sizeof(ip->addrs)); | |
134 log_write(bp); | |
135 brelse(bp); | |
136 | |
137 | |
294 | 138 goto next(...); |
139 } | |
140 | |
141 __code idupfs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { | |
142 | |
305 | 143 acquire(&icache.lock); |
144 ip->ref++; | |
145 release(&icache.lock); | |
146 | |
147 goto next(ip, ...); | |
148 | |
294 | 149 } |
150 | |
151 __code ilockfs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { | |
152 | |
306 | 153 goto lockinode1(fs, ip, bp, dip, next(...)); |
294 | 154 } |
155 | |
156 __code iunlockfs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { | |
307 | 157 |
158 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
|
159 char* msg = "iunlock"; |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
160 struct Err* err = createKernelError(&proc->cbc_context); |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
161 Gearef(cbc_context, Err)->msg = msg; |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
162 goto meta(cbc_context, err->panic); |
307 | 163 } |
164 | |
165 acquire(&icache.lock); | |
166 ip->flags &= ~I_BUSY; | |
167 wakeup(ip); | |
168 release(&icache.lock); | |
294 | 169 |
170 goto next(...); | |
171 } | |
172 | |
173 __code iputfs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { | |
316 | 174 if (next == C_iputfs_impl) { |
175 next = fs->next2; | |
176 } | |
307 | 177 goto iput_check(fs, ip, next(...)); |
178 | |
294 | 179 } |
180 | |
316 | 181 __code iunlockputfs_impl(struct fs_impl* fs, struct inode* ip, __code next(...)) { |
182 fs->next2 = next; | |
183 goto iunlockfs_impl(ip, fs->iput, ...); | |
294 | 184 } |
185 | |
299 | 186 typedef struct stat stat; |
338 | 187 __code statifs_impl(struct fs_impl* fs , struct inode* ip, struct stat* st, __code next(...)) { |
308 | 188 st->dev = ip->dev; |
189 st->ino = ip->inum; | |
190 st->type = ip->type; | |
191 st->nlink = ip->nlink; | |
192 st->size = ip->size; | |
294 | 193 goto next(...); |
194 } | |
195 | |
319 | 196 __code readifs_impl(struct fs_impl* fs, struct inode* ip, char* dst, uint off, uint tot, uint n, __code next(int ret, ...)) { |
317 | 197 if (ip->type == T_DEV) { |
318 | 198 goto readi_check_diskinode(fs, ip, dst, n, next(...)); |
317 | 199 } |
294 | 200 |
317 | 201 if (off > ip->size || off + n < off) { |
202 ret = -1; | |
203 goto next(ret, ...); | |
204 } | |
205 | |
206 if (off + n > ip->size) { | |
207 n = ip->size - off; | |
208 } | |
319 | 209 Gearef(cbc_context, fs)->tot = 0; |
317 | 210 goto readi_loopcheck(fs, tot, m, dst, off, n, next(...)); |
294 | 211 } |
212 | |
320 | 213 __code writeifs_impl(struct fs_impl* fs, struct inode* ip, char* src, uint off, uint tot, uint n, __code next(int ret, ...)) { |
214 if (ip->type == T_DEV) { | |
215 goto writei_check_diskinode(fs, ip, src, n, next(...)); | |
216 } | |
217 | |
218 if (off > ip->size || off + n < off) { | |
219 ret = -1; | |
220 goto next(ret, ...); | |
221 } | |
222 | |
223 if (off + n > MAXFILE * BSIZE) { | |
224 ret = -1; | |
225 goto next(ret, ...); | |
226 } | |
321 | 227 Gearef(cbc_context, fs)->tot = 0; |
320 | 228 goto writei_loopcheck(fs, tot, m, src, off, n, next(...)); |
229 } | |
294 | 230 |
231 | |
310 | 232 __code namecmpfs_impl(struct fs_impl* fs, const char* s, const char* t, __code next(int strncmp_val, ...)) { |
233 strncmp_val = strncmp(s, t, DIRSIZ); | |
234 goto next(strncmp_val, ...); | |
294 | 235 } |
236 | |
338 | 237 __code dirlookupfs_impl(struct fs_impl* fs, struct inode* dp, char* name, uint off, uint* poff, dirent* de, __code next(...)) { |
311 | 238 if (dp->type != T_DIR) { |
326
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
239 char* msg = "dirlookup not DIR"; |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
240 struct Err* err = createKernelError(&proc->cbc_context); |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
241 Gearef(cbc_context, Err)->msg = msg; |
f0b337cb6024
use goto err->panic
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
321
diff
changeset
|
242 goto meta(cbc_context, err->panic); |
311 | 243 } |
312 | 244 Gearef(cbc_context, fs)->off = 0; |
245 goto dirlookup_loopcheck(fs, dp, name, off, poff, de, next(...)); | |
294 | 246 } |
247 | |
338 | 248 __code dirlinkfs_impl(struct fs_impl* fs, struct inode* ip, struct dirent* de, struct inode* dp, char* name, uint off, uint inum, __code next(...)) { |
315 | 249 // Check that name is not present. |
250 if ((ip = dirlookup(dp, name, 0)) != 0) { | |
251 goto dirlink_namecheck(fs, ip, next(...)); | |
252 } | |
316 | 253 Gearef(cbc_context, fs)->off = 0; |
315 | 254 goto dirlink_loopcheck(fs, de, dp, off, next(...)); |
294 | 255 } |
256 | |
309 | 257 static struct inode* iget (uint dev, uint inum) |
258 { | |
259 struct inode *ip, *empty; | |
260 | |
261 acquire(&icache.lock); | |
262 | |
263 // Is the inode already cached? | |
264 empty = 0; | |
265 | |
266 for (ip = &icache.inode[0]; ip < &icache.inode[NINODE]; ip++) { | |
267 if (ip->ref > 0 && ip->dev == dev && ip->inum == inum) { | |
268 ip->ref++; | |
269 release(&icache.lock); | |
270 return ip; | |
271 } | |
272 | |
273 if (empty == 0 && ip->ref == 0) { // Remember empty slot. | |
274 empty = ip; | |
275 } | |
276 } | |
277 | |
278 // Recycle an inode cache entry. | |
279 if (empty == 0) { | |
280 panic("iget: no inodes"); | |
281 } | |
282 | |
283 ip = empty; | |
284 ip->dev = dev; | |
285 ip->inum = inum; | |
286 ip->ref = 1; | |
287 ip->flags = 0; | |
288 release(&icache.lock); | |
289 | |
290 return ip; | |
291 } | |
308 | 292 |
293 static char* skipelem (char *path, char *name) | |
294 { | |
295 char *s; | |
296 int len; | |
297 | |
298 while (*path == '/') { | |
299 path++; | |
300 } | |
301 | |
302 if (*path == 0) { | |
303 return 0; | |
304 } | |
305 | |
306 s = path; | |
294 | 307 |
308 | 308 while (*path != '/' && *path != 0) { |
309 path++; | |
310 } | |
311 | |
312 len = path - s; | |
313 | |
314 if (len >= DIRSIZ) { | |
315 memmove(name, s, DIRSIZ); | |
316 } else { | |
317 memmove(name, s, len); | |
318 name[len] = 0; | |
319 } | |
320 | |
321 while (*path == '/') { | |
322 path++; | |
323 } | |
324 | |
325 return path; | |
294 | 326 } |
327 | |
308 | 328 |
329 static struct inode* namex (char *path, int nameiparent, char *name) | |
330 { | |
331 struct inode *ip, *next; | |
332 | |
333 if (*path == '/') { | |
334 ip = iget(ROOTDEV, ROOTINO); | |
335 } else { | |
336 ip = idup(proc->cwd); | |
337 } | |
338 | |
339 while ((path = skipelem(path, name)) != 0) { | |
340 ilock(ip); | |
341 | |
342 if (ip->type != T_DIR) { | |
343 iunlockput(ip); | |
344 return 0; | |
345 } | |
294 | 346 |
308 | 347 if (nameiparent && *path == '\0') { |
348 // Stop one level early. | |
349 iunlock(ip); | |
350 return ip; | |
351 } | |
352 | |
353 if ((next = dirlookup(ip, name, 0)) == 0) { | |
354 iunlockput(ip); | |
355 return 0; | |
356 } | |
357 | |
358 iunlockput(ip); | |
359 ip = next; | |
360 } | |
361 | |
362 if (nameiparent) { | |
363 iput(ip); | |
364 return 0; | |
365 } | |
366 | |
367 return ip; | |
294 | 368 } |
369 | |
308 | 370 __code nameifs_impl(struct fs_impl* fs, char* path, __code next(int namex_val, ...)) { |
371 char name[DIRSIZ]; | |
372 namex_val = namex(path, 0, name); | |
373 goto next(namex_val, ...); | |
374 } | |
375 | |
376 __code nameiparentfs_impl(struct fs_impl* fs, char* path, char* name, __code next(int namex_val, ...)) { | |
377 | |
378 namex_val = namex(path, 1, name); | |
379 goto next(namex_val, ...); | |
380 | |
381 } | |
382 |