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