dev.c 8.0 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 %#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. int n;
  118. Chan *c;
  119. char *buf;
  120. c = newchan();
  121. mkqid(&c->qid, 0, 0, QTDIR);
  122. c->type = devno(tc, 0);
  123. if(spec == nil)
  124. spec = "";
  125. n = 1+UTFmax+strlen(spec)+1;
  126. buf = smalloc(n);
  127. snprint(buf, n, "#%C%s", tc, spec);
  128. c->path = newpath(buf);
  129. free(buf);
  130. return c;
  131. }
  132. Chan*
  133. devclone(Chan *c)
  134. {
  135. Chan *nc;
  136. if(c->flag & COPEN)
  137. panic("clone of open file type %C\n", devtab[c->type]->dc);
  138. nc = newchan();
  139. nc->type = c->type;
  140. nc->dev = c->dev;
  141. nc->mode = c->mode;
  142. nc->qid = c->qid;
  143. nc->offset = c->offset;
  144. nc->umh = nil;
  145. nc->aux = c->aux;
  146. nc->mqid = c->mqid;
  147. nc->mcp = c->mcp;
  148. return nc;
  149. }
  150. Walkqid*
  151. devwalk(Chan *c, Chan *nc, char **name, int nname, Dirtab *tab, int ntab, Devgen *gen)
  152. {
  153. int i, j, alloc;
  154. Walkqid *wq;
  155. char *n;
  156. Dir dir;
  157. if(nname > 0)
  158. isdir(c);
  159. alloc = 0;
  160. wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid));
  161. if(waserror()){
  162. if(alloc && wq->clone!=nil)
  163. cclose(wq->clone);
  164. free(wq);
  165. return nil;
  166. }
  167. if(nc == nil){
  168. nc = devclone(c);
  169. nc->type = 0; /* device doesn't know about this channel yet */
  170. alloc = 1;
  171. }
  172. wq->clone = nc;
  173. for(j=0; j<nname; j++){
  174. if(!(nc->qid.type&QTDIR)){
  175. if(j==0)
  176. error(Enotdir);
  177. goto Done;
  178. }
  179. n = name[j];
  180. if(strcmp(n, ".") == 0){
  181. Accept:
  182. wq->qid[wq->nqid++] = nc->qid;
  183. continue;
  184. }
  185. if(strcmp(n, "..") == 0){
  186. if((*gen)(nc, nil, tab, ntab, DEVDOTDOT, &dir) != 1){
  187. print("devgen walk .. in dev%s %llux broken\n",
  188. devtab[nc->type]->name, nc->qid.path);
  189. error("broken devgen");
  190. }
  191. nc->qid = dir.qid;
  192. goto Accept;
  193. }
  194. /*
  195. * Ugly problem: If we're using devgen, make sure we're
  196. * walking the directory itself, represented by the first
  197. * entry in the table, and not trying to step into a sub-
  198. * directory of the table, e.g. /net/net. Devgen itself
  199. * should take care of the problem, but it doesn't have
  200. * the necessary information (that we're doing a walk).
  201. */
  202. if(gen==devgen && nc->qid.path!=tab[0].qid.path)
  203. goto Notfound;
  204. for(i=0;; i++) {
  205. switch((*gen)(nc, n, tab, ntab, i, &dir)){
  206. case -1:
  207. Notfound:
  208. if(j == 0)
  209. error(Enonexist);
  210. kstrcpy(up->errstr, Enonexist, ERRMAX);
  211. goto Done;
  212. case 0:
  213. continue;
  214. case 1:
  215. if(strcmp(n, dir.name) == 0){
  216. nc->qid = dir.qid;
  217. goto Accept;
  218. }
  219. continue;
  220. }
  221. }
  222. }
  223. /*
  224. * We processed at least one name, so will return some data.
  225. * If we didn't process all nname entries succesfully, we drop
  226. * the cloned channel and return just the Qids of the walks.
  227. */
  228. Done:
  229. poperror();
  230. if(wq->nqid < nname){
  231. if(alloc)
  232. cclose(wq->clone);
  233. wq->clone = nil;
  234. }else if(wq->clone){
  235. /* attach cloned channel to same device */
  236. wq->clone->type = c->type;
  237. }
  238. return wq;
  239. }
  240. int
  241. devstat(Chan *c, uchar *db, int n, Dirtab *tab, int ntab, Devgen *gen)
  242. {
  243. int i;
  244. Dir dir;
  245. char *p, *elem;
  246. for(i=0;; i++){
  247. switch((*gen)(c, nil, tab, ntab, i, &dir)){
  248. case -1:
  249. if(c->qid.type & QTDIR){
  250. if(c->path == nil)
  251. elem = "???";
  252. else if(strcmp(c->path->s, "/") == 0)
  253. elem = "/";
  254. else
  255. for(elem=p=c->path->s; *p; p++)
  256. if(*p == '/')
  257. elem = p+1;
  258. devdir(c, c->qid, elem, 0, eve, DMDIR|0555, &dir);
  259. n = convD2M(&dir, db, n);
  260. if(n == 0)
  261. error(Ebadarg);
  262. return n;
  263. }
  264. error(Enonexist);
  265. case 0:
  266. break;
  267. case 1:
  268. if(c->qid.path == dir.qid.path) {
  269. if(c->flag&CMSG)
  270. dir.mode |= DMMOUNT;
  271. n = convD2M(&dir, db, n);
  272. if(n == 0)
  273. error(Ebadarg);
  274. return n;
  275. }
  276. break;
  277. }
  278. }
  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. }