annotate src/file.cbc @ 96:d5c4016c65b8

implement read interfacce
author anatofuz
date Sat, 09 Nov 2019 19:56:07 +0900
parents 0ddcd561d975
children 4f9d95dc4efd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 // File descriptors
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 //
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 #include "types.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 #include "defs.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 #include "param.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 #include "fs.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "file.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "spinlock.h"
32
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
11 #include "proc.h"
0
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
52
214d21c891c7 rename to cbc
kono
parents: 35
diff changeset
13 #define __ncode __code
214d21c891c7 rename to cbc
kono
parents: 35
diff changeset
14 #
96
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
15
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
16 #interface "SysRead.h"
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
17
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
18
0
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 struct devsw devsw[NDEV];
27
mir3636
parents: 24
diff changeset
20 struct cbc_devsw cbc_devsw[NDEV];
mir3636
parents: 24
diff changeset
21
0
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 struct {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 struct spinlock lock;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 struct file file[NFILE];
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 } ftable;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 void fileinit (void)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 initlock(&ftable.lock, "ftable");
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
96
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
32 struct SysFileRead* createSysFileRead(struct Context* context) {
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
33 struct SysRead* sys_read = new SysRead();
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
34 struct SysFileRead* sys_file_read = new SysFileRead();
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
35 sys_read->sys_read = (union Data*)sys_file_read;
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
36 sys_read->read = C_cbc_fileread;
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
37 sys_read->ret = C_cbc_fileret;
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
38 sys_read->next = NULL;
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
39 return sys_read;
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
40 }
d5c4016c65b8 implement read interfacce
anatofuz
parents: 95
diff changeset
41
0
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 // Allocate a file structure.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 struct file* filealloc (void)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 struct file *f;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 acquire(&ftable.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 for (f = ftable.file; f < ftable.file + NFILE; f++) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 if (f->ref == 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 f->ref = 1;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 release(&ftable.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 return f;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 release(&ftable.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 return 0;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 // Increment ref count for file f.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 struct file* filedup (struct file *f)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 acquire(&ftable.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 if (f->ref < 1) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 panic("filedup");
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 f->ref++;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 release(&ftable.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 return f;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 // Close file f. (Decrement ref count, close when reaches 0.)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 void fileclose (struct file *f)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 struct file ff;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 acquire(&ftable.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 if (f->ref < 1) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 panic("fileclose");
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 if (--f->ref > 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 release(&ftable.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 return;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 ff = *f;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 f->ref = 0;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 f->type = FD_NONE;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 release(&ftable.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 if (ff.type == FD_PIPE) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 pipeclose(ff.pipe, ff.writable);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 } else if (ff.type == FD_INODE) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 begin_trans();
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 iput(ff.ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 commit_trans();
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 // Get metadata about file f.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 int filestat (struct file *f, struct stat *st)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 if (f->type == FD_INODE) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 ilock(f->ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 stati(f->ip, st);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 iunlock(f->ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 return 0;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 return -1;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119
52
214d21c891c7 rename to cbc
kono
parents: 35
diff changeset
120 __ncode cbc_fileread1 (int r)
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
121 {
33
7a63dacab7f8 fix cbc_read
mir3636
parents: 32
diff changeset
122 struct file *f = proc->cbc_arg.cbc_console_arg.f;
7a63dacab7f8 fix cbc_read
mir3636
parents: 32
diff changeset
123 __code (*next)(int ret) = cbc_ret;
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
124 if (r > 0)
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
125 f->off += r;
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
126 iunlock(f->ip);
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
127 goto next(r);
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
128 }
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
129
91
b5ddf6fb0a6d use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
130
95
0ddcd561d975 fix make error
tobaru
parents: 92
diff changeset
131 __code cbc_fileread(struct file *f, char *addr, int n, __code next(int ret, ...))
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
132 {
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
133 if (f->readable == 0) {
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
134 goto next(-1);
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
135 }
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
136
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
137 if (f->type == FD_PIPE) {
95
0ddcd561d975 fix make error
tobaru
parents: 92
diff changeset
138 piperead(f->pipe, addr, n);
30
mir3636
parents: 27
diff changeset
139 goto next(-1);
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
140 }
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
141
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
142 if (f->type == FD_INODE) {
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
143 ilock(f->ip);
33
7a63dacab7f8 fix cbc_read
mir3636
parents: 32
diff changeset
144 proc->cbc_arg.cbc_console_arg.f = f;
7a63dacab7f8 fix cbc_read
mir3636
parents: 32
diff changeset
145 goto cbc_readi(f->ip, addr, f->off, n, cbc_fileread1);
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
146 }
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
147
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
148 goto cbc_panic("fileread");
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
149 }
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
150
0
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 // Read from file f.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 int fileread (struct file *f, char *addr, int n)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 int r;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 if (f->readable == 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 return -1;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 if (f->type == FD_PIPE) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 return piperead(f->pipe, addr, n);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
163
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 if (f->type == FD_INODE) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 ilock(f->ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 if ((r = readi(f->ip, addr, f->off, n)) > 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 f->off += r;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
170
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 iunlock(f->ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 return r;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 panic("fileread");
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 //PAGEBREAK!
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 // Write to file f.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 int filewrite (struct file *f, char *addr, int n)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 int r;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 int i;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 int max;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 int n1;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 if (f->writable == 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 return -1;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 if (f->type == FD_PIPE) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 return pipewrite(f->pipe, addr, n);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 if (f->type == FD_INODE) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 // write a few blocks at a time to avoid exceeding
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 // the maximum log transaction size, including
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 // i-node, indirect block, allocation blocks,
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 // and 2 blocks of slop for non-aligned writes.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 // this really belongs lower down, since writei()
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 // might be writing a device like the console.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 max = ((LOGSIZE - 1 - 1 - 2) / 2) * 512;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 i = 0;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 while (i < n) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 n1 = n - i;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 if (n1 > max) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 n1 = max;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 begin_trans();
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 ilock(f->ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 if ((r = writei(f->ip, addr + i, f->off, n1)) > 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 f->off += r;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 iunlock(f->ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 commit_trans();
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 if (r < 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
226
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 if (r != n1) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 panic("short filewrite");
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 i += r;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 return i == n ? n : -1;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 panic("filewrite");