dev.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. extern ulong kerndate;
  8. void
  9. mkqid(Qid *q, vlong path, ulong vers, int type)
  10. {
  11. q->type = type;
  12. q->vers = vers;
  13. q->path = path;
  14. }
  15. int
  16. devno(int c, int user)
  17. {
  18. int i;
  19. for(i = 0; devtab[i] != nil; i++) {
  20. if(devtab[i]->dc == c)
  21. return i;
  22. }
  23. if(user == 0)
  24. panic("devno %C 0x%ux", c, c);
  25. return -1;
  26. }
  27. void
  28. devdir(Chan *c, Qid qid, char *n, vlong length, char *user, long perm, Dir *db)
  29. {
  30. db->name = n;
  31. if(c->flag&CMSG)
  32. qid.type |= QTMOUNT;
  33. db->qid = qid;
  34. db->type = devtab[c->type]->dc;
  35. db->dev = c->dev;
  36. db->mode = perm;
  37. db->mode |= qid.type << 24;
  38. db->atime = seconds();
  39. db->mtime = kerndate;
  40. db->length = length;
  41. db->uid = user;
  42. db->gid = eve;
  43. db->muid = user;
  44. }
  45. /*
  46. * (here, Devgen is the prototype; devgen is the function in dev.c.)
  47. *
  48. * a Devgen is expected to return the directory entry for ".."
  49. * if you pass it s==DEVDOTDOT (-1). otherwise...
  50. *
  51. * there are two contradictory rules.
  52. *
  53. * (i) if c is a directory, a Devgen is expected to list its children
  54. * as you iterate s.
  55. *
  56. * (ii) whether or not c is a directory, a Devgen is expected to list
  57. * its siblings as you iterate s.
  58. *
  59. * devgen always returns the list of children in the root
  60. * directory. thus it follows (i) when c is the root and (ii) otherwise.
  61. * many other Devgens follow (i) when c is a directory and (ii) otherwise.
  62. *
  63. * devwalk assumes (i). it knows that devgen breaks (i)
  64. * for children that are themselves directories, and explicitly catches them.
  65. *
  66. * devstat assumes (ii). if the Devgen in question follows (i)
  67. * for this particular c, devstat will not find the necessary info.
  68. * with our particular Devgen functions, this happens only for
  69. * directories, so devstat makes something up, assuming
  70. * c->name, c->qid, eve, DMDIR|0555.
  71. *
  72. * devdirread assumes (i). the callers have to make sure
  73. * that the Devgen satisfies (i) for the chan being read.
  74. */
  75. /*
  76. * the zeroth element of the table MUST be the directory itself for ..
  77. */
  78. int
  79. devgen(Chan *c, char *name, Dirtab *tab, int ntab, int i, Dir *dp)
  80. {
  81. if(tab == 0)
  82. return -1;
  83. if(i == DEVDOTDOT){
  84. /* nothing */
  85. }else if(name){
  86. for(i=1; i<ntab; i++)
  87. if(strcmp(tab[i].name, name) == 0)
  88. break;
  89. if(i==ntab)
  90. return -1;
  91. tab += i;
  92. }else{
  93. /* skip over the first element, that for . itself */
  94. i++;
  95. if(i >= ntab)
  96. return -1;
  97. tab += i;
  98. }
  99. devdir(c, tab->qid, tab->name, tab->length, eve, tab->perm, dp);
  100. return 1;
  101. }
  102. void
  103. devreset(void)
  104. {
  105. }
  106. void
  107. devinit(void)
  108. {
  109. }
  110. void
  111. devshutdown(void)
  112. {
  113. }
  114. Chan*
  115. devattach(int tc, char *spec)
  116. {
  117. Chan *c;
  118. char *buf;
  119. c = newchan();
  120. mkqid(&c->qid, 0, 0, QTDIR);
  121. c->type = devno(tc, 0);
  122. if(spec == nil)
  123. spec = "";
  124. buf = smalloc(4+strlen(spec)+1);
  125. sprint(buf, "#%C%s", tc, spec);
  126. c->path = newpath(buf);
  127. free(buf);
  128. return c;
  129. }
  130. Chan*
  131. devclone(Chan *c)
  132. {
  133. Chan *nc;
  134. if(c->flag & COPEN)
  135. panic("clone of open file type %C\n", devtab[c->type]->dc);
  136. nc = newchan();
  137. nc->type = c->type;
  138. nc->dev = c->dev;
  139. nc->mode = c->mode;
  140. nc->qid = c->qid;
  141. nc->offset = c->offset;
  142. nc->umh = nil;
  143. nc->mountid = c->mountid;
  144. nc->aux = c->aux;
  145. nc->mqid = c->mqid;
  146. nc->mcp = c->mcp;
  147. return nc;
  148. }
  149. Walkqid*
  150. devwalk(Chan *c, Chan *nc, char **name, int nname, Dirtab *tab, int ntab, Devgen *gen)
  151. {
  152. int i, j, alloc;
  153. Walkqid *wq;
  154. char *n;
  155. Dir dir;
  156. if(nname > 0)
  157. isdir(c);
  158. alloc = 0;
  159. wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid));
  160. if(waserror()){
  161. if(alloc && wq->clone!=nil)
  162. cclose(wq->clone);
  163. free(wq);
  164. return nil;
  165. }
  166. if(nc == nil){
  167. nc = devclone(c);
  168. nc->type = 0; /* device doesn't know about this channel yet */
  169. alloc = 1;
  170. }
  171. wq->clone = nc;
  172. for(j=0; j<nname; j++){
  173. if(!(nc->qid.type&QTDIR)){
  174. if(j==0)
  175. error(Enotdir);
  176. goto Done;
  177. }
  178. n = name[j];
  179. if(strcmp(n, ".") == 0){
  180. Accept:
  181. wq->qid[wq->nqid++] = nc->qid;
  182. continue;
  183. }
  184. if(strcmp(n, "..") == 0){
  185. if((*gen)(nc, nil, tab, ntab, DEVDOTDOT, &dir) != 1){
  186. print("devgen walk .. in dev%s %llux broken\n",
  187. devtab[nc->type]->name, nc->qid.path);
  188. error("broken devgen");
  189. }
  190. nc->qid = dir.qid;
  191. goto Accept;
  192. }
  193. /*
  194. * Ugly problem: If we're using devgen, make sure we're
  195. * walking the directory itself, represented by the first
  196. * entry in the table, and not trying to step into a sub-
  197. * directory of the table, e.g. /net/net. Devgen itself
  198. * should take care of the problem, but it doesn't have
  199. * the necessary information (that we're doing a walk).
  200. */
  201. if(gen==devgen && nc->qid.path!=tab[0].qid.path)
  202. goto Notfound;
  203. for(i=0;; i++) {
  204. switch((*gen)(nc, n, tab, ntab, i, &dir)){
  205. case -1:
  206. Notfound:
  207. if(j == 0)
  208. error(Enonexist);
  209. kstrcpy(up->errstr, Enonexist, ERRMAX);
  210. goto Done;
  211. case 0:
  212. continue;
  213. case 1:
  214. if(strcmp(n, dir.name) == 0){
  215. nc->qid = dir.qid;
  216. goto Accept;
  217. }
  218. continue;
  219. }
  220. }
  221. }
  222. /*
  223. * We processed at least one name, so will return some data.
  224. * If we didn't process all nname entries succesfully, we drop
  225. * the cloned channel and return just the Qids of the walks.
  226. */
  227. Done:
  228. poperror();
  229. if(wq->nqid < nname){
  230. if(alloc)
  231. cclose(wq->clone);
  232. wq->clone = nil;
  233. }else if(wq->clone){
  234. /* attach cloned channel to same device */
  235. wq->clone->type = c->type;
  236. }
  237. return wq;
  238. }
  239. int
  240. devstat(Chan *c, uchar *db, int n, Dirtab *tab, int ntab, Devgen *gen)
  241. {
  242. int i;
  243. Dir dir;
  244. char *p, *elem;
  245. for(i=0;; i++)
  246. switch((*gen)(c, nil, tab, ntab, i, &dir)){
  247. case -1:
  248. if(c->qid.type & QTDIR){
  249. if(c->path == nil)
  250. elem = "???";
  251. else if(strcmp(c->path->s, "/") == 0)
  252. elem = "/";
  253. else
  254. for(elem=p=c->path->s; *p; p++)
  255. if(*p == '/')
  256. elem = p+1;
  257. devdir(c, c->qid, elem, 0, eve, DMDIR|0555, &dir);
  258. n = convD2M(&dir, db, n);
  259. if(n == 0)
  260. error(Ebadarg);
  261. return n;
  262. }
  263. error(Enonexist);
  264. case 0:
  265. break;
  266. case 1:
  267. if(c->qid.path == dir.qid.path) {
  268. if(c->flag&CMSG)
  269. dir.mode |= DMMOUNT;
  270. n = convD2M(&dir, db, n);
  271. if(n == 0)
  272. error(Ebadarg);
  273. return n;
  274. }
  275. break;
  276. }
  277. error(Egreg); /* not reached? */
  278. return -1;
  279. }
  280. long
  281. devdirread(Chan *c, char *d, long n, Dirtab *tab, int ntab, Devgen *gen)
  282. {
  283. long m, dsz;
  284. Dir dir;
  285. for(m=0; m<n; c->dri++) {
  286. switch((*gen)(c, nil, tab, ntab, c->dri, &dir)){
  287. case -1:
  288. return m;
  289. case 0:
  290. break;
  291. case 1:
  292. dsz = convD2M(&dir, (uchar*)d, n-m);
  293. if(dsz <= BIT16SZ){ /* <= not < because this isn't stat; read is stuck */
  294. if(m == 0)
  295. error(Eshort);
  296. return m;
  297. }
  298. m += dsz;
  299. d += dsz;
  300. break;
  301. }
  302. }
  303. return m;
  304. }
  305. /*
  306. * error(Eperm) if open permission not granted for up->user.
  307. */
  308. void
  309. devpermcheck(char *fileuid, ulong perm, int omode)
  310. {
  311. ulong t;
  312. static int access[] = { 0400, 0200, 0600, 0100 };
  313. if(strcmp(up->user, fileuid) == 0)
  314. perm <<= 0;
  315. else
  316. if(strcmp(up->user, eve) == 0)
  317. perm <<= 3;
  318. else
  319. perm <<= 6;
  320. t = access[omode&3];
  321. if((t&perm) != t)
  322. error(Eperm);
  323. }
  324. Chan*
  325. devopen(Chan *c, int omode, Dirtab *tab, int ntab, Devgen *gen)
  326. {
  327. int i;
  328. Dir dir;
  329. for(i=0;; i++) {
  330. switch((*gen)(c, nil, tab, ntab, i, &dir)){
  331. case -1:
  332. goto Return;
  333. case 0:
  334. break;
  335. case 1:
  336. if(c->qid.path == dir.qid.path) {
  337. devpermcheck(dir.uid, dir.mode, omode);
  338. goto Return;
  339. }
  340. break;
  341. }
  342. }
  343. Return:
  344. c->offset = 0;
  345. if((c->qid.type&QTDIR) && omode!=OREAD)
  346. error(Eperm);
  347. c->mode = openmode(omode);
  348. c->flag |= COPEN;
  349. return c;
  350. }
  351. void
  352. devcreate(Chan*, char*, int, ulong)
  353. {
  354. error(Eperm);
  355. }
  356. Block*
  357. devbread(Chan *c, long n, ulong offset)
  358. {
  359. Block *bp;
  360. bp = allocb(n);
  361. if(bp == 0)
  362. error(Enomem);
  363. if(waserror()) {
  364. freeb(bp);
  365. nexterror();
  366. }
  367. bp->wp += devtab[c->type]->read(c, bp->wp, n, offset);
  368. poperror();
  369. return bp;
  370. }
  371. long
  372. devbwrite(Chan *c, Block *bp, ulong offset)
  373. {
  374. long n;
  375. if(waserror()) {
  376. freeb(bp);
  377. nexterror();
  378. }
  379. n = devtab[c->type]->write(c, bp->rp, BLEN(bp), offset);
  380. poperror();
  381. freeb(bp);
  382. return n;
  383. }
  384. void
  385. devremove(Chan*)
  386. {
  387. error(Eperm);
  388. }
  389. int
  390. devwstat(Chan*, uchar*, int)
  391. {
  392. error(Eperm);
  393. return 0;
  394. }
  395. void
  396. devpower(int)
  397. {
  398. error(Eperm);
  399. }
  400. int
  401. devconfig(int, char *, DevConf *)
  402. {
  403. error(Eperm);
  404. return 0;
  405. }