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