annotate final_main/src/fs_impl.cbc @ 16:b67e4c9f0374

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