~kris/9p

9hist

ref: cd47f2433b78e08525e6c1bbbd15b702dba35a87 9hist/ip/kio.c -rw-r--r-- 2.8 KiB
cd47f243 — David du Colombier Plan 9 from Bell Labs 2000-01-29 26 years ago
                                                                                
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
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"
#include	"kernel.h"

#define validaddr(a, b, c)

long
kclose(int fd)
{
	if(waserror())
		return -1;
	/*
	 * Take no reference on the chan because we don't really need the
	 * data structure, and are calling fdtochan only for error checks.
	 * fdclose takes care of processes racing through here.
	 */
	fdtochan(fd, -1, 0, 0);
	fdclose(fd, 0);

	poperror();
	return 0;
}

long
kopen(char *path, int mode)
{
	int fd;
	Chan *c = 0;

	if(waserror()) {
		if(c)
			cclose(c);
		return -1;
	}

	openmode(mode);
	validaddr((ulong)path, 1, 0);
	c = namec(path, Aopen, mode, 0);
	fd = newfd(c);

	poperror();
	return fd;
}

long
kread(int fd, void *va, long n)
{
	int dir;
	Chan *c;

	if(waserror())
		return -1;

	validaddr((ulong)va, n, 1);
	c = fdtochan(fd, OREAD, 1, 1);
	if(waserror()) {
		cclose(c);
		nexterror();
	}

	dir = c->qid.path&CHDIR;
	if(dir) {
		n -= n%DIRLEN;
		if(c->offset%DIRLEN || n==0)
			error(Etoosmall);
	}

	if(dir && c->mh)
		n = unionread(c, va, n);
	else if(devtab[c->type]->dc != L'M')
		n = devtab[c->type]->read(c, va, n, c->offset);
	else
		n = mntread9p(c, va, n, c->offset);

	lock(c);
	c->offset += n;
	unlock(c);

	poperror();
	cclose(c);

	poperror();
	return n;
}

long
kseek(int fd, vlong offset, int whence)
{
	Chan *c;
	char buf[DIRLEN];
	Dir dir;
	long off;

	if(waserror())
		return -1;

	c = fdtochan(fd, -1, 1, 1);
	if(waserror()) {
		cclose(c);
		nexterror();
	}
	if(c->qid.path & CHDIR)
		error(Eisdir);

	if(devtab[c->type]->dc == '|')
		error(Eisstream);

	off = 0;
	switch(whence) {
	case 0:
		off = offset;
		c->offset = offset;
		break;
	case 1:
		lock(c);	/* lock for read/write update */
		c->offset += offset;
		off = c->offset;
		unlock(c);
		break;
	case 2:
		devtab[c->type]->stat(c, buf);
		convM2D(buf, &dir);
		c->offset = dir.length + offset;
		off = c->offset;
		break;
	}
	poperror();
	cclose(c);
	poperror();
	return off;
}

long
kwrite(int fd, void *va, long n)
{
	Chan *c;

	if(waserror())
		return -1;

	validaddr((ulong)va, n, 0);
	c = fdtochan(fd, OWRITE, 1, 1);
	if(waserror()) {
		cclose(c);
		nexterror();
	}

	if(c->qid.path & CHDIR)
		error(Eisdir);

	n = devtab[c->type]->write(c, va, n, c->offset);

	lock(c);
	c->offset += n;
	unlock(c);

	poperror();
	cclose(c);

	poperror();
	return n;
}

/* Set kernel error string */
void
kerrstr(char *err)
{
	if(up != nil)
		strncpy(up->error, err, ERRLEN);
}

/* Get kernel error string */
void
kgerrstr(char *err)
{
	char *s;

	s = "<no-up>";
	if(up != nil)
		s = up->error;
	strncpy(err, s, ERRLEN);
}

/* Set kernel error string, using formatted print */
void
kwerrstr(char *fmt, ...)
{
	va_list arg;
	char buf[256];

	va_start(arg, fmt);
	doprint(buf, buf+sizeof(buf), fmt, arg);
	va_end(arg);
	strncpy(up->error, buf, ERRLEN);
}