Mercurial > hg > Members > menikon > CbC_xv6
annotate src/file.cbc @ 92:bc5bcfd2f6d6
rename CbCFile to CbCSysFile
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 19 Oct 2019 16:44:24 +0900 |
parents | b5ddf6fb0a6d |
children | 0ddcd561d975 |
rev | line source |
---|---|
0 | 1 // |
2 // File descriptors | |
3 // | |
4 | |
5 #include "types.h" | |
6 #include "defs.h" | |
7 #include "param.h" | |
8 #include "fs.h" | |
9 #include "file.h" | |
10 #include "spinlock.h" | |
32 | 11 #include "proc.h" |
0 | 12 |
52 | 13 #define __ncode __code |
14 # | |
0 | 15 struct devsw devsw[NDEV]; |
27 | 16 struct cbc_devsw cbc_devsw[NDEV]; |
17 | |
0 | 18 struct { |
19 struct spinlock lock; | |
20 struct file file[NFILE]; | |
21 } ftable; | |
22 | |
23 void fileinit (void) | |
24 { | |
25 initlock(&ftable.lock, "ftable"); | |
26 } | |
27 | |
28 // Allocate a file structure. | |
29 struct file* filealloc (void) | |
30 { | |
31 struct file *f; | |
32 | |
33 acquire(&ftable.lock); | |
34 | |
35 for (f = ftable.file; f < ftable.file + NFILE; f++) { | |
36 if (f->ref == 0) { | |
37 f->ref = 1; | |
38 release(&ftable.lock); | |
39 return f; | |
40 } | |
41 } | |
42 | |
43 release(&ftable.lock); | |
44 return 0; | |
45 } | |
46 | |
47 // Increment ref count for file f. | |
48 struct file* filedup (struct file *f) | |
49 { | |
50 acquire(&ftable.lock); | |
51 | |
52 if (f->ref < 1) { | |
53 panic("filedup"); | |
54 } | |
55 | |
56 f->ref++; | |
57 release(&ftable.lock); | |
58 return f; | |
59 } | |
60 | |
61 // Close file f. (Decrement ref count, close when reaches 0.) | |
62 void fileclose (struct file *f) | |
63 { | |
64 struct file ff; | |
65 | |
66 acquire(&ftable.lock); | |
67 | |
68 if (f->ref < 1) { | |
69 panic("fileclose"); | |
70 } | |
71 | |
72 if (--f->ref > 0) { | |
73 release(&ftable.lock); | |
74 return; | |
75 } | |
76 | |
77 ff = *f; | |
78 f->ref = 0; | |
79 f->type = FD_NONE; | |
80 release(&ftable.lock); | |
81 | |
82 if (ff.type == FD_PIPE) { | |
83 pipeclose(ff.pipe, ff.writable); | |
84 | |
85 } else if (ff.type == FD_INODE) { | |
86 begin_trans(); | |
87 iput(ff.ip); | |
88 commit_trans(); | |
89 } | |
90 } | |
91 | |
92 // Get metadata about file f. | |
93 int filestat (struct file *f, struct stat *st) | |
94 { | |
95 if (f->type == FD_INODE) { | |
96 ilock(f->ip); | |
97 stati(f->ip, st); | |
98 iunlock(f->ip); | |
99 | |
100 return 0; | |
101 } | |
102 | |
103 return -1; | |
104 } | |
105 | |
52 | 106 __ncode cbc_fileread1 (int r) |
24 | 107 { |
33 | 108 struct file *f = proc->cbc_arg.cbc_console_arg.f; |
109 __code (*next)(int ret) = cbc_ret; | |
24 | 110 if (r > 0) |
111 f->off += r; | |
112 iunlock(f->ip); | |
113 goto next(r); | |
114 } | |
115 | |
91
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
116 |
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
117 __code cbc_fileread (struct file *f, char *addr, int n, __code (*next)(int ret)) |
24 | 118 { |
119 if (f->readable == 0) { | |
120 goto next(-1); | |
121 } | |
122 | |
123 if (f->type == FD_PIPE) { | |
35 | 124 goto cbc_piperead(f->pipe, addr, n, next); |
30 | 125 goto next(-1); |
24 | 126 } |
127 | |
128 if (f->type == FD_INODE) { | |
129 ilock(f->ip); | |
33 | 130 proc->cbc_arg.cbc_console_arg.f = f; |
131 goto cbc_readi(f->ip, addr, f->off, n, cbc_fileread1); | |
24 | 132 } |
133 | |
134 goto cbc_panic("fileread"); | |
135 } | |
136 | |
91
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
137 __code cbc_fileread_stub(struct Context* cbc_context) { |
92
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
138 struct CbCSysFile* cbc_file = &cbc_context->data[D_CbCSysFile]->CbCSysFile; |
91
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
139 enum Code next = cbc_context->next; |
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
140 goto cbc_fileread(cbc_file->f, cbc_file->p, cbc_file->n, cbc_context->code[next]); |
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
141 } |
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
52
diff
changeset
|
142 |
0 | 143 // Read from file f. |
144 int fileread (struct file *f, char *addr, int n) | |
145 { | |
146 int r; | |
147 | |
148 if (f->readable == 0) { | |
149 return -1; | |
150 } | |
151 | |
152 if (f->type == FD_PIPE) { | |
153 return piperead(f->pipe, addr, n); | |
154 } | |
155 | |
156 if (f->type == FD_INODE) { | |
157 ilock(f->ip); | |
158 | |
159 if ((r = readi(f->ip, addr, f->off, n)) > 0) { | |
160 f->off += r; | |
161 } | |
162 | |
163 iunlock(f->ip); | |
164 | |
165 return r; | |
166 } | |
167 | |
168 panic("fileread"); | |
169 } | |
170 | |
171 //PAGEBREAK! | |
172 // Write to file f. | |
173 int filewrite (struct file *f, char *addr, int n) | |
174 { | |
175 int r; | |
176 int i; | |
177 int max; | |
178 int n1; | |
179 | |
180 if (f->writable == 0) { | |
181 return -1; | |
182 } | |
183 | |
184 if (f->type == FD_PIPE) { | |
185 return pipewrite(f->pipe, addr, n); | |
186 } | |
187 | |
188 if (f->type == FD_INODE) { | |
189 // write a few blocks at a time to avoid exceeding | |
190 // the maximum log transaction size, including | |
191 // i-node, indirect block, allocation blocks, | |
192 // and 2 blocks of slop for non-aligned writes. | |
193 // this really belongs lower down, since writei() | |
194 // might be writing a device like the console. | |
195 max = ((LOGSIZE - 1 - 1 - 2) / 2) * 512; | |
196 i = 0; | |
197 | |
198 while (i < n) { | |
199 n1 = n - i; | |
200 | |
201 if (n1 > max) { | |
202 n1 = max; | |
203 } | |
204 | |
205 begin_trans(); | |
206 ilock(f->ip); | |
207 | |
208 if ((r = writei(f->ip, addr + i, f->off, n1)) > 0) { | |
209 f->off += r; | |
210 } | |
211 | |
212 iunlock(f->ip); | |
213 commit_trans(); | |
214 | |
215 if (r < 0) { | |
216 break; | |
217 } | |
218 | |
219 if (r != n1) { | |
220 panic("short filewrite"); | |
221 } | |
222 | |
223 i += r; | |
224 } | |
225 | |
226 return i == n ? n : -1; | |
227 } | |
228 | |
229 panic("filewrite"); | |
230 } | |
231 |