-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathpipe.c
346 lines (299 loc) · 9.62 KB
/
pipe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// Created by bruce on 25/04/20.
//
#include <fs/fs.h>
#include <winix/list.h>
#include <winix/ksignal.h>
#include <winix/mm.h>
#include <limits.h>
struct device pipe_dev;
static const char* name = "pipe";
static dev_t pipe_devid = MAKEDEV(2, 1);
#define PIPE_LIMIT (PAGE_LEN)
#define PIPE_INODE_INUM (INT_MAX)
struct pipe_waiting{
struct proc* who;
int sys_call_num;
struct filp *filp;
char *data;
size_t count;
off_t offset;
struct list_head list;
};
int set_filp(struct proc* who, struct filp** _file, struct inode* inode){
int ret, open_slot;
struct filp* file = get_free_filp();
if (!file)
return -ENFILE;
ret = get_fd(who, 0, &open_slot, file);
if(ret)
return ret;
*_file = file;
return open_slot;
}
int do_pipe(struct proc* who, struct message* msg){
int* fds;
vptr_t* vp = msg->m1_p1;
if(!is_vaddr_accessible(vp, who))
return -EFAULT;
fds = (int*)get_physical_addr(vp, who);
return sys_pipe(who, fds);
}
int sys_pipe(struct proc* who, int fd[2]){
int ret1, ret2, ret = 0;
struct filp *file1, *file2;
struct inode* inode;
struct filp_pipe* pipe;
char* ptr;
size_t pagelen = PAGE_LEN;
ptr = (char *)get_free_pages(pagelen, GFP_HIGH);
if(!ptr)
return -ENOMEM;
pipe = kmalloc(1, sizeof(struct filp_pipe));
if(!pipe){
ret = -ENOMEM;
goto failed_filp_pipe;
}
pipe->data = ptr;
pipe->pos = 0;
inode = get_free_inode_slot();
if(!inode){
ret = -ENFILE;
goto failed_filp_slot;
}
inode->i_flags |= INODE_FLAG_PIPE;
inode->i_count = 2;
inode->i_nlinks = 1;
init_inode_non_disk(inode, PIPE_INODE_INUM, &pipe_dev, NULL);
ret1 = set_filp(who, &file1, inode);
if(ret1 < 0){
ret = ret1;
goto failed_filp1;
}
file1->pipe_mode = FILP_PIPE_READ;
file1->pipe = pipe;
init_filp_by_inode(file1, inode);
who->fp_filp[ret1] = file1;
fd[0] = ret1;
ret2 = set_filp(who, &file2, inode);
if(ret2 < 0){
ret = ret2;
goto failed_filp2;
}
file2->pipe_mode = FILP_PIPE_WRITE;
file2->pipe = pipe;
init_filp_by_inode(file2, inode);
who->fp_filp[ret2] = file2;
fd[1] = ret2;
INIT_LIST_HEAD(&inode->pipe_writing_list);
INIT_LIST_HEAD(&inode->pipe_reading_list);
// kdebug("new pipe ret %d %d with inode %d for proc %d\n", ret1, ret2, inode->i_num, who->proc_nr);
return 0;
failed_filp2:
who->fp_filp[ret1] = NULL;
release_filp(file1);
failed_filp1:
inode->i_count = 0;
failed_filp_slot:
kfree(pipe);
failed_filp_pipe:
release_pages((ptr_t *)ptr, PAGE_LEN);
return ret;
}
struct pipe_waiting* get_next_waiting(struct list_head *waiting_list){
if(!list_empty(waiting_list)){
return list_entry(waiting_list->next, struct pipe_waiting, list);
}
return NULL;
}
void shift_pipe_data(struct filp* file, off_t i){
char *data = file->pipe->data;
off_t j = 0, k, end = file->pipe->pos;
while(i < end){
data[j] = data[i];
data[i] = '\0';
i++;
j++;
}
k = j;
while(k < end){
data[k++] = '\0';
}
file->pipe->pos = j;
}
static int _pipe_read(struct proc* who, struct filp *filp, char *data, size_t count, off_t offset){
int ret;
off_t i;
char *pipe_data = filp->pipe->data;
off_t end = count < filp->pipe->pos ? count : filp->pipe->pos;
for(i = 0; i < end; i++){
*data++ = pipe_data[i];
}
ret = (int)(i);
// kdebug("%s[%d] pipe read ret %d \n",
// who->name, curr_syscall_caller->proc_nr, ret);
shift_pipe_data(filp, i);
return ret;
}
static int _pipe_write(struct proc* who, struct filp *filp, char *data, size_t count, off_t offset){
int ret, i;
char *data_start = filp->pipe->data, *p = data;
off_t off = filp->pipe->pos, end;
end = (off + count) < PIPE_LIMIT ? (off + count) : PIPE_LIMIT;
for(i = off; i < end; i++){
data_start[i] = *p++;
}
ret = (int)(i - off);
filp->pipe->pos += ret;
// kdebug(" proc %d writing data 0x%x ret %d pipe->pos %d\n",
// who->proc_nr, data, ret, filp->pipe->pos);
return ret;
}
int pipe_read ( struct filp *filp, char *data, size_t count, off_t offset){
int ret, ret2;
struct pipe_waiting* next;
struct message msg;
struct inode* ino = filp->filp_ino;
if(filp->pipe_mode == FILP_PIPE_WRITE)
return 0;
offset = 0; //Pipe always read from start
if(filp->pipe->pos == 0){
if(filp->filp_flags & O_NONBLOCK)
return 0;
if(ino->i_count == 1) // write end is closed
return 0;
next = (struct pipe_waiting*)kmalloc(1, sizeof(struct pipe_waiting));
if(!next)
return -ENOMEM;
curr_syscall_caller->flags |= STATE_WAITING;
next->who = curr_syscall_caller;
next->filp = filp;
next->data = data;
next->count = count;
next->offset = offset;
next->sys_call_num = READ;
list_add(&next->list, &ino->pipe_reading_list);
// kdebug("pipe: proc %d reading inode num %d is blocked \n", curr_syscall_caller->pid, filp->filp_ino->i_num);
return SUSPEND;
}
ret = _pipe_read(curr_syscall_caller, filp, data, count, offset);
next = get_next_waiting(&ino->pipe_writing_list);
if(next!= NULL ){
// kdebug("pipe: proc %d is awaken for writing\n", next->who->proc_nr);
list_del(&next->list);
ret2 = _pipe_write(next->who, next->filp, next->data, next->count, next->offset);
next->who->flags &= ~STATE_WAITING;
syscall_reply2(next->sys_call_num, ret2, next->who->proc_nr, &msg);
kfree(next->data);
kfree(next);
}
return ret;
}
int pipe_write ( struct filp *filp, char *data, size_t count, off_t offset){
int ret, ret2;
struct pipe_waiting* next;
struct message msg;
struct inode* ino = filp->filp_ino;
if(filp->pipe_mode == FILP_PIPE_READ){
return 0;
}
if(filp->filp_ino->i_count == 1) {
int signum = SIGPIPE;
if(curr_syscall_caller->sig_table[signum].sa_handler == SIG_IGN){
return -EPIPE;
}
send_sig(curr_syscall_caller, signum);
return SUSPEND;
}
if(count > PIPE_LIMIT)
return -ENOSPC;
if(count > (PIPE_LIMIT - filp->pipe->pos)){
next = get_next_waiting(&ino->pipe_reading_list);
if(next!= NULL){
// kdebug("pipe: proc %d is awaken for reading\n", next->who->proc_nr);
list_del(&next->list);
ret2 = _pipe_read(next->who, next->filp, next->data, next->count, next->offset);
next->who->flags &= ~STATE_WAITING;
syscall_reply2(next->sys_call_num, ret2, next->who->proc_nr, &msg);
kfree(next);
}
}
if(count > (PIPE_LIMIT - filp->pipe->pos)){
char *p, *p2;
int len = count;
if(filp->filp_flags & O_NONBLOCK)
return 0;
next = (struct pipe_waiting*)kmalloc(1, sizeof(struct pipe_waiting));
if(!next)
return -ENOMEM;
p = (char *)kmalloc(count, sizeof(char));
if(!next){
kfree(next);
return -ENOMEM;
}
p2 = p;
while(len--){
*p2++ = *data++;
}
curr_syscall_caller->flags |= STATE_WAITING;
next->who = curr_syscall_caller;
next->filp = filp;
next->data = p;
next->count = count;
next->offset = offset;
next->sys_call_num = WRITE;
list_add(&next->list, &ino->pipe_writing_list);
// kdebug("pipe: proc %d writing from 0x%x %d bytes is blocked\n",
// curr_syscall_caller->pid, data, count);
// kdebug("\n");
return SUSPEND;
}
ret = _pipe_write(curr_syscall_caller, filp, data, count, offset);
return ret;
}
int pipe_open ( struct device* dev, struct filp *file){
return 0;
}
int pipe_close ( struct device* dev, struct filp *file){
int ret;
struct inode* ino = file->filp_ino;
struct pipe_waiting* next;
struct list_head *waiting;
struct message msg;
int (*fn)(struct proc*, struct filp*, char *, size_t, off_t);
file->filp_count -= 1;
if(file->filp_count == 0){
ino->i_count -= 1;
if(file->pipe_mode == FILP_PIPE_READ){
fn = _pipe_write;
waiting = &ino->pipe_writing_list;
}else{
fn = _pipe_read;
waiting = &ino->pipe_reading_list;
}
next = get_next_waiting(waiting);
while (next){
// kdebug("next waiting %s\n", next->who->name);
list_del(&next->list);
ret = fn(next->who, next->filp, next->data, next->count, next->offset);
next->who->flags &= ~STATE_WAITING;
syscall_reply2(next->sys_call_num, ret, next->who->proc_nr, &msg);
kfree(next);
next = get_next_waiting(waiting);
}
if(ino->i_count == 0){
// kdebug("Releasing pipe %d\n", file->filp_ino->i_num);
release_pages((ptr_t *)file->pipe->data, PAGE_LEN);
kfree(file->pipe);
// release inode
memset(ino, 0, sizeof(struct inode));
}
}
// kdebug("%s[%d] close file %d, mode %d, count %d ino count %d\n", curr_syscall_caller->name, curr_syscall_caller->proc_nr,
// file->filp_ino->i_num, file->pipe_mode, file->filp_count, ino->i_count);
return 0;
}
static struct filp_operations pipe_fops = {pipe_open, pipe_read, pipe_write, pipe_close};
void init_pipe(){
register_device(&pipe_dev, name, pipe_devid, S_IFIFO, NULL, &pipe_fops);
}