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