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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
#include "u.h"
#include "lib.h"
#include "dat.h"
#include "fns.h"
#include "error.h"
extern ulong kerndate;
void
mkqid(Qid *q, vlong path, ulong vers, int type)
{
q->type = type;
q->vers = vers;
q->path = path;
}
int
devno(int c, int user)
{
int i;
for(i = 0; devtab[i] != nil; i++) {
if(devtab[i]->dc == c)
return i;
}
if(user == 0)
panic("devno %C 0x%ux", c, c);
return -1;
}
void
devdir(Chan *c, Qid qid, char *n, vlong length, char *user, long perm, Dir *db)
{
db->name = n;
if(c->flag&CMSG)
qid.type |= QTMOUNT;
db->qid = qid;
db->type = devtab[c->type]->dc;
db->dev = c->dev;
db->mode = perm;
db->mode |= qid.type << 24;
db->atime = seconds();
db->mtime = kerndate;
db->length = length;
db->uid = user;
db->gid = eve;
db->muid = user;
}
/*
* (here, Devgen is the prototype; devgen is the function in dev.c.)
*
* a Devgen is expected to return the directory entry for ".."
* if you pass it s==DEVDOTDOT (-1). otherwise...
*
* there are two contradictory rules.
*
* (i) if c is a directory, a Devgen is expected to list its children
* as you iterate s.
*
* (ii) whether or not c is a directory, a Devgen is expected to list
* its siblings as you iterate s.
*
* devgen always returns the list of children in the root
* directory. thus it follows (i) when c is the root and (ii) otherwise.
* many other Devgens follow (i) when c is a directory and (ii) otherwise.
*
* devwalk assumes (i). it knows that devgen breaks (i)
* for children that are themselves directories, and explicitly catches them.
*
* devstat assumes (ii). if the Devgen in question follows (i)
* for this particular c, devstat will not find the necessary info.
* with our particular Devgen functions, this happens only for
* directories, so devstat makes something up, assuming
* c->name, c->qid, eve, DMDIR|0555.
*
* devdirread assumes (i). the callers have to make sure
* that the Devgen satisfies (i) for the chan being read.
*/
/*
* the zeroth element of the table MUST be the directory itself for ..
*/
int
devgen(Chan *c, char *name, Dirtab *tab, int ntab, int i, Dir *dp)
{
if(tab == 0)
return -1;
if(i == DEVDOTDOT){
/* nothing */
}else if(name){
for(i=1; i<ntab; i++)
if(strcmp(tab[i].name, name) == 0)
break;
if(i==ntab)
return -1;
tab += i;
}else{
/* skip over the first element, that for . itself */
i++;
if(i >= ntab)
return -1;
tab += i;
}
devdir(c, tab->qid, tab->name, tab->length, eve, tab->perm, dp);
return 1;
}
void
devreset(void)
{
}
void
devinit(void)
{
}
void
devshutdown(void)
{
}
Chan*
devattach(int tc, char *spec)
{
Chan *c;
char *buf;
c = newchan();
mkqid(&c->qid, 0, 0, QTDIR);
c->type = devno(tc, 0);
if(spec == nil)
spec = "";
buf = smalloc(4+strlen(spec)+1);
sprint(buf, "#%C%s", tc, spec);
c->name = newcname(buf);
free(buf);
return c;
}
Chan*
devclone(Chan *c)
{
Chan *nc;
if(c->flag & COPEN)
panic("clone of open file type %C\n", devtab[c->type]->dc);
nc = newchan();
nc->type = c->type;
nc->dev = c->dev;
nc->mode = c->mode;
nc->qid = c->qid;
nc->offset = c->offset;
nc->umh = nil;
nc->mountid = c->mountid;
nc->aux = c->aux;
nc->pgrpid = c->pgrpid;
nc->mid = c->mid;
nc->mqid = c->mqid;
nc->mcp = c->mcp;
return nc;
}
Walkqid*
devwalk(Chan *c, Chan *nc, char **name, int nname, Dirtab *tab, int ntab, Devgen *gen)
{
int i, j, alloc;
Walkqid *wq;
char *n;
Dir dir;
if(nname > 0)
isdir(c);
alloc = 0;
wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid));
if(waserror()){
if(alloc && wq->clone!=nil)
cclose(wq->clone);
free(wq);
return nil;
}
if(nc == nil){
nc = devclone(c);
nc->type = 0; /* device doesn't know about this channel yet */
alloc = 1;
}
wq->clone = nc;
for(j=0; j<nname; j++){
if(!(nc->qid.type&QTDIR)){
if(j==0)
error(Enotdir);
goto Done;
}
n = name[j];
if(strcmp(n, ".") == 0){
Accept:
wq->qid[wq->nqid++] = nc->qid;
continue;
}
if(strcmp(n, "..") == 0){
if((*gen)(nc, nil, tab, ntab, DEVDOTDOT, &dir) != 1){
print("devgen walk .. in dev%s %llux broken\n",
devtab[nc->type]->name, nc->qid.path);
error("broken devgen");
}
nc->qid = dir.qid;
goto Accept;
}
/*
* Ugly problem: If we're using devgen, make sure we're
* walking the directory itself, represented by the first
* entry in the table, and not trying to step into a sub-
* directory of the table, e.g. /net/net. Devgen itself
* should take care of the problem, but it doesn't have
* the necessary information (that we're doing a walk).
*/
if(gen==devgen && nc->qid.path!=tab[0].qid.path)
goto Notfound;
for(i=0;; i++) {
switch((*gen)(nc, n, tab, ntab, i, &dir)){
case -1:
Notfound:
if(j == 0)
error(Enonexist);
kstrcpy(up->errstr, Enonexist, ERRMAX);
goto Done;
case 0:
continue;
case 1:
if(strcmp(n, dir.name) == 0){
nc->qid = dir.qid;
goto Accept;
}
continue;
}
}
}
/*
* We processed at least one name, so will return some data.
* If we didn't process all nname entries succesfully, we drop
* the cloned channel and return just the Qids of the walks.
*/
Done:
poperror();
if(wq->nqid < nname){
if(alloc)
cclose(wq->clone);
wq->clone = nil;
}else if(wq->clone){
/* attach cloned channel to same device */
wq->clone->type = c->type;
}
return wq;
}
int
devstat(Chan *c, uchar *db, int n, Dirtab *tab, int ntab, Devgen *gen)
{
int i;
Dir dir;
char *p, *elem;
for(i=0;; i++)
switch((*gen)(c, nil, tab, ntab, i, &dir)){
case -1:
if(c->qid.type & QTDIR){
if(c->name == nil)
elem = "???";
else if(strcmp(c->name->s, "/") == 0)
elem = "/";
else
for(elem=p=c->name->s; *p; p++)
if(*p == '/')
elem = p+1;
devdir(c, c->qid, elem, 0, eve, DMDIR|0555, &dir);
n = convD2M(&dir, db, n);
if(n == 0)
error(Ebadarg);
return n;
}
print("devstat %C %llux\n", devtab[c->type]->dc, c->qid.path);
error(Enonexist);
case 0:
break;
case 1:
if(c->qid.path == dir.qid.path) {
if(c->flag&CMSG)
dir.mode |= DMMOUNT;
n = convD2M(&dir, db, n);
if(n == 0)
error(Ebadarg);
return n;
}
break;
}
error(Egreg); /* not reached? */
return -1;
}
long
devdirread(Chan *c, char *d, long n, Dirtab *tab, int ntab, Devgen *gen)
{
long m, dsz;
struct{
Dir d;
char slop[100];
}dir;
for(m=0; m<n; c->dri++) {
switch((*gen)(c, nil, tab, ntab, c->dri, &dir.d)){
case -1:
return m;
case 0:
break;
case 1:
dsz = convD2M(&dir.d, (uchar*)d, n-m);
if(dsz <= BIT16SZ){ /* <= not < because this isn't stat; read is stuck */
if(m == 0)
error(Eshort);
return m;
}
m += dsz;
d += dsz;
break;
}
}
return m;
}
/*
* error(Eperm) if open permission not granted for up->user.
*/
void
devpermcheck(char *fileuid, ulong perm, int omode)
{
ulong t;
static int access[] = { 0400, 0200, 0600, 0100 };
if(strcmp(up->user, fileuid) == 0)
perm <<= 0;
else
if(strcmp(up->user, eve) == 0)
perm <<= 3;
else
perm <<= 6;
t = access[omode&3];
if((t&perm) != t)
error(Eperm);
}
Chan*
devopen(Chan *c, int omode, Dirtab *tab, int ntab, Devgen *gen)
{
int i;
Dir dir;
for(i=0;; i++) {
switch((*gen)(c, nil, tab, ntab, i, &dir)){
case -1:
goto Return;
case 0:
break;
case 1:
if(c->qid.path == dir.qid.path) {
devpermcheck(dir.uid, dir.mode, omode);
goto Return;
}
break;
}
}
Return:
c->offset = 0;
if((c->qid.type&QTDIR) && omode!=OREAD)
error(Eperm);
c->mode = openmode(omode);
c->flag |= COPEN;
return c;
}
void
devcreate(Chan *c, char *name, int mode, ulong perm)
{
USED(c);
USED(name);
USED(mode);
USED(perm);
error(Eperm);
}
Block*
devbread(Chan *c, long n, ulong offset)
{
Block *bp;
bp = allocb(n);
if(bp == 0)
error(Enomem);
if(waserror()) {
freeb(bp);
nexterror();
}
bp->wp += devtab[c->type]->read(c, bp->wp, n, offset);
poperror();
return bp;
}
long
devbwrite(Chan *c, Block *bp, ulong offset)
{
long n;
if(waserror()) {
freeb(bp);
nexterror();
}
n = devtab[c->type]->write(c, bp->rp, BLEN(bp), offset);
poperror();
freeb(bp);
return n;
}
void
devremove(Chan *c)
{
USED(c);
error(Eperm);
}
int
devwstat(Chan *c, uchar *a, int n)
{
USED(c);
USED(a);
USED(n);
error(Eperm);
return 0;
}
void
devpower(int a)
{
USED(a);
error(Eperm);
}
int
devconfig(int a, char *b, DevConf *c)
{
USED(a);
USED(b);
USED(c);
error(Eperm);
return 0;
}
|