12 struct devsw devsw
[NDEV
];
15 struct file file
[NFILE
];
21 initlock(&ftable
.lock
, "ftable");
24 // Allocate a file structure.
30 acquire(&ftable
.lock
);
31 for(f
= ftable
.file
; f
< ftable
.file
+ NFILE
; f
++){
34 release(&ftable
.lock
);
38 release(&ftable
.lock
);
42 // Increment ref count for file f.
44 filedup(struct file
*f
)
46 acquire(&ftable
.lock
);
50 release(&ftable
.lock
);
54 // Close file f. (Decrement ref count, close when reaches 0.)
56 fileclose(struct file
*f
)
60 acquire(&ftable
.lock
);
64 release(&ftable
.lock
);
70 release(&ftable
.lock
);
72 if(ff
.type
== FD_PIPE
)
73 pipeclose(ff
.pipe
, ff
.writable
);
74 else if(ff
.type
== FD_INODE
){
81 // Get metadata about file f.
83 filestat(struct file
*f
, struct stat
*st
)
85 if(f
->type
== FD_INODE
){
96 fileread(struct file
*f
, char *addr
, int n
)
102 if(f
->type
== FD_PIPE
)
103 return piperead(f
->pipe
, addr
, n
);
104 if(f
->type
== FD_INODE
){
106 if((r
= readi(f
->ip
, addr
, f
->off
, n
)) > 0)
117 filewrite(struct file
*f
, char *addr
, int n
)
123 if(f
->type
== FD_PIPE
)
124 return pipewrite(f
->pipe
, addr
, n
);
125 if(f
->type
== FD_INODE
){
126 // write a few blocks at a time to avoid exceeding
127 // the maximum log transaction size, including
128 // i-node, indirect block, allocation blocks,
129 // and 2 blocks of slop for non-aligned writes.
130 // this really belongs lower down, since writei()
131 // might be writing a device like the console.
132 int max
= ((LOGSIZE
-1-1-2) / 2) * 512;
141 if ((r
= writei(f
->ip
, addr
+ i
, f
->off
, n1
)) > 0)
149 panic("short filewrite");
152 return i
== n
? n
: -1;