8 struct devsw devsw
[NDEV
];
11 struct file file
[NFILE
];
17 initlock(&ftable
.lock
, "ftable");
20 // Allocate a file structure.
26 acquire(&ftable
.lock
);
27 for(f
= ftable
.file
; f
< ftable
.file
+ NFILE
; f
++){
30 release(&ftable
.lock
);
34 release(&ftable
.lock
);
38 // Increment ref count for file f.
40 filedup(struct file
*f
)
42 acquire(&ftable
.lock
);
46 release(&ftable
.lock
);
50 // Close file f. (Decrement ref count, close when reaches 0.)
52 fileclose(struct file
*f
)
56 acquire(&ftable
.lock
);
60 release(&ftable
.lock
);
66 release(&ftable
.lock
);
68 if(ff
.type
== FD_PIPE
)
69 pipeclose(ff
.pipe
, ff
.writable
);
70 else if(ff
.type
== FD_INODE
)
74 // Get metadata about file f.
76 filestat(struct file
*f
, struct stat
*st
)
78 if(f
->type
== FD_INODE
){
87 // Read from file f. Addr is kernel address.
89 fileread(struct file
*f
, char *addr
, int n
)
95 if(f
->type
== FD_PIPE
)
96 return piperead(f
->pipe
, addr
, n
);
97 if(f
->type
== FD_INODE
){
99 if((r
= readi(f
->ip
, addr
, f
->off
, n
)) > 0)
107 // Write to file f. Addr is kernel address.
109 filewrite(struct file
*f
, char *addr
, int n
)
115 if(f
->type
== FD_PIPE
)
116 return pipewrite(f
->pipe
, addr
, n
);
117 if(f
->type
== FD_INODE
){
119 if((r
= writei(f
->ip
, addr
, f
->off
, n
)) > 0)