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