dev.c 10 KB

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