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
|
32
|
104 __code cbc_fileread1 (int r, struct file *f, __code (*next)(int ret))
|
24
|
105 {
|
|
106 if (r > 0)
|
|
107 f->off += r;
|
|
108 iunlock(f->ip);
|
|
109 goto next(r);
|
|
110 }
|
|
111
|
|
112 __code cbc_fileread (struct file *f, char *addr, int n, __code (*next)(int ret))
|
|
113 {
|
|
114 if (f->readable == 0) {
|
|
115 goto next(-1);
|
|
116 }
|
|
117
|
|
118 if (f->type == FD_PIPE) {
|
30
|
119 //goto cbc_piperead(f->pipe, addr, n, next);
|
|
120 goto next(-1);
|
24
|
121 }
|
|
122
|
|
123 if (f->type == FD_INODE) {
|
|
124 ilock(f->ip);
|
31
|
125 goto cbc_readi(f->ip, addr, f->off, n, f, next);
|
24
|
126 }
|
|
127
|
|
128 goto cbc_panic("fileread");
|
|
129 }
|
|
130
|
0
|
131 // Read from file f.
|
|
132 int fileread (struct file *f, char *addr, int n)
|
|
133 {
|
|
134 int r;
|
|
135
|
|
136 if (f->readable == 0) {
|
|
137 return -1;
|
|
138 }
|
|
139
|
|
140 if (f->type == FD_PIPE) {
|
|
141 return piperead(f->pipe, addr, n);
|
|
142 }
|
|
143
|
|
144 if (f->type == FD_INODE) {
|
|
145 ilock(f->ip);
|
|
146
|
|
147 if ((r = readi(f->ip, addr, f->off, n)) > 0) {
|
|
148 f->off += r;
|
|
149 }
|
|
150
|
|
151 iunlock(f->ip);
|
|
152
|
|
153 return r;
|
|
154 }
|
|
155
|
|
156 panic("fileread");
|
|
157 }
|
|
158
|
|
159 //PAGEBREAK!
|
|
160 // Write to file f.
|
|
161 int filewrite (struct file *f, char *addr, int n)
|
|
162 {
|
|
163 int r;
|
|
164 int i;
|
|
165 int max;
|
|
166 int n1;
|
|
167
|
|
168 if (f->writable == 0) {
|
|
169 return -1;
|
|
170 }
|
|
171
|
|
172 if (f->type == FD_PIPE) {
|
|
173 return pipewrite(f->pipe, addr, n);
|
|
174 }
|
|
175
|
|
176 if (f->type == FD_INODE) {
|
|
177 // write a few blocks at a time to avoid exceeding
|
|
178 // the maximum log transaction size, including
|
|
179 // i-node, indirect block, allocation blocks,
|
|
180 // and 2 blocks of slop for non-aligned writes.
|
|
181 // this really belongs lower down, since writei()
|
|
182 // might be writing a device like the console.
|
|
183 max = ((LOGSIZE - 1 - 1 - 2) / 2) * 512;
|
|
184 i = 0;
|
|
185
|
|
186 while (i < n) {
|
|
187 n1 = n - i;
|
|
188
|
|
189 if (n1 > max) {
|
|
190 n1 = max;
|
|
191 }
|
|
192
|
|
193 begin_trans();
|
|
194 ilock(f->ip);
|
|
195
|
|
196 if ((r = writei(f->ip, addr + i, f->off, n1)) > 0) {
|
|
197 f->off += r;
|
|
198 }
|
|
199
|
|
200 iunlock(f->ip);
|
|
201 commit_trans();
|
|
202
|
|
203 if (r < 0) {
|
|
204 break;
|
|
205 }
|
|
206
|
|
207 if (r != n1) {
|
|
208 panic("short filewrite");
|
|
209 }
|
|
210
|
|
211 i += r;
|
|
212 }
|
|
213
|
|
214 return i == n ? n : -1;
|
|
215 }
|
|
216
|
|
217 panic("filewrite");
|
|
218 }
|
|
219
|