dev.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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->aux = c->aux;
  144. nc->mqid = c->mqid;
  145. nc->mcp = c->mcp;
  146. return nc;
  147. }
  148. Walkqid*
  149. devwalk(Chan *c, Chan *nc, char **name, int nname, Dirtab *tab, int ntab, Devgen *gen)
  150. {
  151. int i, j, alloc;
  152. Walkqid *wq;
  153. char *n;
  154. Dir dir;
  155. if(nname > 0)
  156. isdir(c);
  157. alloc = 0;
  158. wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid));
  159. if(waserror()){
  160. if(alloc && wq->clone!=nil)
  161. cclose(wq->clone);
  162. free(wq);
  163. return nil;
  164. }
  165. if(nc == nil){
  166. nc = devclone(c);
  167. nc->type = 0; /* device doesn't know about this channel yet */
  168. alloc = 1;
  169. }
  170. wq->clone = nc;
  171. for(j=0; j<nname; j++){
  172. if(!(nc->qid.type&QTDIR)){
  173. if(j==0)
  174. error(Enotdir);
  175. goto Done;
  176. }
  177. n = name[j];
  178. if(strcmp(n, ".") == 0){
  179. Accept:
  180. wq->qid[wq->nqid++] = nc->qid;
  181. continue;
  182. }
  183. if(strcmp(n, "..") == 0){
  184. if((*gen)(nc, nil, tab, ntab, DEVDOTDOT, &dir) != 1){
  185. print("devgen walk .. in dev%s %llux broken\n",
  186. devtab[nc->type]->name, nc->qid.path);
  187. error("broken devgen");
  188. }
  189. nc->qid = dir.qid;
  190. goto Accept;
  191. }
  192. /*
  193. * Ugly problem: If we're using devgen, make sure we're
  194. * walking the directory itself, represented by the first
  195. * entry in the table, and not trying to step into a sub-
  196. * directory of the table, e.g. /net/net. Devgen itself
  197. * should take care of the problem, but it doesn't have
  198. * the necessary information (that we're doing a walk).
  199. */
  200. if(gen==devgen && nc->qid.path!=tab[0].qid.path)
  201. goto Notfound;
  202. for(i=0;; i++) {
  203. switch((*gen)(nc, n, tab, ntab, i, &dir)){
  204. case -1:
  205. Notfound:
  206. if(j == 0)
  207. error(Enonexist);
  208. kstrcpy(up->errstr, Enonexist, ERRMAX);
  209. goto Done;
  210. case 0:
  211. continue;
  212. case 1:
  213. if(strcmp(n, dir.name) == 0){
  214. nc->qid = dir.qid;
  215. goto Accept;
  216. }
  217. continue;
  218. }
  219. }
  220. }
  221. /*
  222. * We processed at least one name, so will return some data.
  223. * If we didn't process all nname entries succesfully, we drop
  224. * the cloned channel and return just the Qids of the walks.
  225. */
  226. Done:
  227. poperror();
  228. if(wq->nqid < nname){
  229. if(alloc)
  230. cclose(wq->clone);
  231. wq->clone = nil;
  232. }else if(wq->clone){
  233. /* attach cloned channel to same device */
  234. wq->clone->type = c->type;
  235. }
  236. return wq;
  237. }
  238. int
  239. devstat(Chan *c, uchar *db, int n, Dirtab *tab, int ntab, Devgen *gen)
  240. {
  241. int i;
  242. Dir dir;
  243. char *p, *elem;
  244. for(i=0;; i++){
  245. switch((*gen)(c, nil, tab, ntab, i, &dir)){
  246. case -1:
  247. if(c->qid.type & QTDIR){
  248. if(c->path == nil)
  249. elem = "???";
  250. else if(strcmp(c->path->s, "/") == 0)
  251. elem = "/";
  252. else
  253. for(elem=p=c->path->s; *p; p++)
  254. if(*p == '/')
  255. elem = p+1;
  256. devdir(c, c->qid, elem, 0, eve, DMDIR|0555, &dir);
  257. n = convD2M(&dir, db, n);
  258. if(n == 0)
  259. error(Ebadarg);
  260. return n;
  261. }
  262. error(Enonexist);
  263. case 0:
  264. break;
  265. case 1:
  266. if(c->qid.path == dir.qid.path) {
  267. if(c->flag&CMSG)
  268. dir.mode |= DMMOUNT;
  269. n = convD2M(&dir, db, n);
  270. if(n == 0)
  271. error(Ebadarg);
  272. return n;
  273. }
  274. break;
  275. }
  276. }
  277. }
  278. long
  279. devdirread(Chan *c, char *d, long n, Dirtab *tab, int ntab, Devgen *gen)
  280. {
  281. long m, dsz;
  282. Dir dir;
  283. for(m=0; m<n; c->dri++) {
  284. switch((*gen)(c, nil, tab, ntab, c->dri, &dir)){
  285. case -1:
  286. return m;
  287. case 0:
  288. break;
  289. case 1:
  290. dsz = convD2M(&dir, (uchar*)d, n-m);
  291. if(dsz <= BIT16SZ){ /* <= not < because this isn't stat; read is stuck */
  292. if(m == 0)
  293. error(Eshort);
  294. return m;
  295. }
  296. m += dsz;
  297. d += dsz;
  298. break;
  299. }
  300. }
  301. return m;
  302. }
  303. /*
  304. * error(Eperm) if open permission not granted for up->user.
  305. */
  306. void
  307. devpermcheck(char *fileuid, ulong perm, int omode)
  308. {
  309. ulong t;
  310. static int access[] = { 0400, 0200, 0600, 0100 };
  311. if(strcmp(up->user, fileuid) == 0)
  312. perm <<= 0;
  313. else
  314. if(strcmp(up->user, eve) == 0)
  315. perm <<= 3;
  316. else
  317. perm <<= 6;
  318. t = access[omode&3];
  319. if((t&perm) != t)
  320. error(Eperm);
  321. }
  322. Chan*
  323. devopen(Chan *c, int omode, Dirtab *tab, int ntab, Devgen *gen)
  324. {
  325. int i;
  326. Dir dir;
  327. for(i=0;; i++) {
  328. switch((*gen)(c, nil, tab, ntab, i, &dir)){
  329. case -1:
  330. goto Return;
  331. case 0:
  332. break;
  333. case 1:
  334. if(c->qid.path == dir.qid.path) {
  335. devpermcheck(dir.uid, dir.mode, omode);
  336. goto Return;
  337. }
  338. break;
  339. }
  340. }
  341. Return:
  342. c->offset = 0;
  343. if((c->qid.type&QTDIR) && omode!=OREAD)
  344. error(Eperm);
  345. c->mode = openmode(omode);
  346. c->flag |= COPEN;
  347. return c;
  348. }
  349. void
  350. devcreate(Chan*, char*, int, ulong)
  351. {
  352. error(Eperm);
  353. }
  354. Block*
  355. devbread(Chan *c, long n, ulong offset)
  356. {
  357. Block *bp;
  358. bp = allocb(n);
  359. if(bp == 0)
  360. error(Enomem);
  361. if(waserror()) {
  362. freeb(bp);
  363. nexterror();
  364. }
  365. bp->wp += devtab[c->type]->read(c, bp->wp, n, offset);
  366. poperror();
  367. return bp;
  368. }
  369. long
  370. devbwrite(Chan *c, Block *bp, ulong offset)
  371. {
  372. long n;
  373. if(waserror()) {
  374. freeb(bp);
  375. nexterror();
  376. }
  377. n = devtab[c->type]->write(c, bp->rp, BLEN(bp), offset);
  378. poperror();
  379. freeb(bp);
  380. return n;
  381. }
  382. void
  383. devremove(Chan*)
  384. {
  385. error(Eperm);
  386. }
  387. int
  388. devwstat(Chan*, uchar*, int)
  389. {
  390. error(Eperm);
  391. return 0;
  392. }
  393. void
  394. devpower(int)
  395. {
  396. error(Eperm);
  397. }
  398. int
  399. devconfig(int, char *, DevConf *)
  400. {
  401. error(Eperm);
  402. return 0;
  403. }