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