devramfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. #define RAM_MAGIC 0xbedabb1e
  16. #define Einvalid "Invalid ram file"
  17. #define NBLOCK 4096
  18. struct RamFile {
  19. unsigned int magic;
  20. char *n;
  21. struct RamFile *parent;
  22. struct RamFile *sibling;
  23. u64 length;
  24. u64 alloclength;
  25. int perm;
  26. int opencount;
  27. int deleteonclose;
  28. // A file is up to 4K 8192-byte blocks. That's 32M. Enough.
  29. void *blocks[NBLOCK];
  30. union {
  31. struct RamFile *firstchild;
  32. };
  33. };
  34. static struct RamFile *ramroot;
  35. static QLock ramlock;
  36. static int devramdebug;
  37. static void
  38. printramfile(int offset, struct RamFile *file)
  39. {
  40. int i;
  41. for(i = 0; i < offset; i++){
  42. print(" ");
  43. }
  44. print("ramfile: %x, magic:%x, name: %s, parent: %x, sibling:%x, length:%d, alloclength: %d, perm: %o\n",
  45. file, file->magic, file->n, file->parent, file->sibling, file->length, file->alloclength, file->perm);
  46. }
  47. static void
  48. debugwalkinternal(int offset, struct RamFile *current)
  49. {
  50. printramfile(offset, current);
  51. for(current = current->firstchild; current != nil; current = current->sibling){
  52. if(current->perm & DMDIR){
  53. debugwalkinternal(offset + 1, current);
  54. } else {
  55. printramfile(offset, current);
  56. }
  57. }
  58. }
  59. static void
  60. debugwalk()
  61. {
  62. print("***********************\n");
  63. debugwalkinternal(0, ramroot);
  64. }
  65. static void
  66. raminit(void)
  67. {
  68. ramroot = (struct RamFile *)malloc(sizeof(struct RamFile));
  69. if(ramroot == nil){
  70. error("raminit: out of memory");
  71. }
  72. ramroot->n = smalloc(2);
  73. kstrdup(&ramroot->n, ".");
  74. ramroot->magic = RAM_MAGIC;
  75. ramroot->length = 0;
  76. ramroot->alloclength = 0;
  77. ramroot->perm = DMDIR | 0777;
  78. ramroot->firstchild = nil;
  79. }
  80. static void
  81. ramreset(void)
  82. {
  83. }
  84. static Chan *
  85. ramattach(char *spec)
  86. {
  87. Chan *c;
  88. char *buf;
  89. c = newchan();
  90. mkqid(&c->qid, (i64)ramroot, 0, QTDIR);
  91. c->dev = devtabget('@', 0);
  92. if(spec == nil)
  93. spec = "";
  94. buf = malloc(1 + UTFmax + strlen(spec) + 1);
  95. if(buf == nil){
  96. error("ramattach:out of memory");
  97. }
  98. sprint(buf, "#@%s", spec);
  99. c->path = newpath(buf);
  100. c->mode = 0777;
  101. free(buf);
  102. return c;
  103. }
  104. static int
  105. ramgen(Chan *c, char *name, Dirtab *tab, int ntab, int pos, Dir *dp)
  106. {
  107. Qid qid;
  108. int i;
  109. struct RamFile *current = (struct RamFile *)c->qid.path;
  110. if(pos == DEVDOTDOT){
  111. if(current->parent == nil){
  112. mkqid(&qid, (usize)current, 0, QTDIR);
  113. devdir(c, qid, "#@", 0, eve, current->perm, dp);
  114. return 1;
  115. } else {
  116. mkqid(&qid, (usize)current->parent, 0, QTDIR);
  117. devdir(c, qid, current->n, 0, eve, current->perm, dp);
  118. return 1;
  119. }
  120. }
  121. if(current->perm & DMDIR){
  122. current = current->firstchild;
  123. if(current == nil){
  124. return -1;
  125. }
  126. }
  127. for(i = 0; i < pos; i++){
  128. current = current->sibling;
  129. if(current == nil){
  130. return -1;
  131. }
  132. }
  133. mkqid(&qid, (usize)current, 0, current->perm & DMDIR ? QTDIR : 0);
  134. devdir(c, qid, current->n, current->length, eve, current->perm, dp);
  135. if(name == nil || strcmp(current->n, name) == 0){
  136. return 1;
  137. } else {
  138. return 0;
  139. }
  140. }
  141. static Walkqid *
  142. ramwalk(Chan *c, Chan *nc, char **name, int nname)
  143. {
  144. Proc *up = externup();
  145. qlock(&ramlock);
  146. if(waserror()){
  147. qunlock(&ramlock);
  148. nexterror();
  149. }
  150. Walkqid *wqid = devwalk(c, nc, name, nname, 0, 0, ramgen);
  151. poperror();
  152. qunlock(&ramlock);
  153. return wqid;
  154. }
  155. static i32
  156. ramstat(Chan *c, u8 *dp, i32 n)
  157. {
  158. Proc *up = externup();
  159. Dir dir;
  160. Qid qid;
  161. struct RamFile *current = (struct RamFile *)c->qid.path;
  162. if(current->magic != RAM_MAGIC)
  163. error("ramstat: current->magic != RAM_MAGIC");
  164. qlock(&ramlock);
  165. if(waserror()){
  166. qunlock(&ramlock);
  167. nexterror();
  168. }
  169. mkqid(&qid, c->qid.path, 0, current->perm & DMDIR ? QTDIR : 0);
  170. devdir(c, qid, current->n, current->length, eve, current->perm, &dir);
  171. i32 ret = convD2M(&dir, dp, n);
  172. poperror();
  173. qunlock(&ramlock);
  174. return ret;
  175. }
  176. static i32
  177. ramwstat(Chan *c, u8 *dp, i32 n)
  178. {
  179. Proc *up = externup();
  180. Dir d;
  181. struct RamFile *current = (struct RamFile *)c->qid.path;
  182. char *strs;
  183. if(current->magic != RAM_MAGIC)
  184. error("ramwstat:current->magic != RAM_MAGIC");
  185. qlock(&ramlock);
  186. if(waserror()){
  187. qunlock(&ramlock);
  188. nexterror();
  189. }
  190. strs = smalloc(n);
  191. n = convM2D(dp, n, &d, strs);
  192. if(n == 0)
  193. error(Eshortstat);
  194. if(d.mode != (u32)~0UL)
  195. current->perm = d.mode;
  196. if(d.uid && *d.uid)
  197. error(Eperm);
  198. if(d.name && *d.name && strcmp(current->n, d.name) != 0){
  199. if(strchr(d.name, '/') != nil)
  200. error(Ebadchar);
  201. // Invalid names should have been caught in convM2D()
  202. kstrdup(&current->n, d.name);
  203. }
  204. qunlock(&ramlock);
  205. free(strs);
  206. poperror();
  207. return n;
  208. }
  209. static Chan *
  210. ramopen(Chan *c, int omode)
  211. {
  212. Proc *up = externup();
  213. qlock(&ramlock);
  214. if(waserror()){
  215. qunlock(&ramlock);
  216. nexterror();
  217. }
  218. Chan *ret = devopen(c, omode, nil, 0, ramgen);
  219. struct RamFile *file = (struct RamFile *)c->qid.path;
  220. if(file->magic != RAM_MAGIC)
  221. error("ramopen:file->magic != RAM_MAGIC");
  222. qunlock(&ramlock);
  223. poperror();
  224. return ret;
  225. }
  226. static void delete(struct RamFile *file)
  227. {
  228. Proc *up = externup();
  229. struct RamFile *prev = file->parent->firstchild;
  230. qlock(&ramlock);
  231. if(waserror()){
  232. qunlock(&ramlock);
  233. nexterror();
  234. }
  235. if(prev == file){
  236. // This is the first file - make any sibling the first child
  237. file->parent->firstchild = file->sibling;
  238. } else {
  239. // Find previous file
  240. for(; prev != nil && prev->sibling != file; prev = prev->sibling)
  241. ;
  242. if(prev == nil){
  243. error("delete:previous of this file is nil?");
  244. } else {
  245. prev->sibling = file->sibling;
  246. }
  247. }
  248. if(!(file->perm & DMDIR)){
  249. for(int i = 0; i < nelem(file->blocks); i++)
  250. free(file->blocks[i]);
  251. }
  252. file->magic = 0;
  253. free(file->n);
  254. free(file);
  255. if(devramdebug)
  256. debugwalk();
  257. poperror();
  258. qunlock(&ramlock);
  259. }
  260. static void
  261. ramclose(Chan *c)
  262. {
  263. struct RamFile *file = (struct RamFile *)c->qid.path;
  264. if(file->magic != RAM_MAGIC)
  265. error("close:file->magic != RAM_MAGIC");
  266. if(file->deleteonclose){
  267. delete(file);
  268. }
  269. }
  270. static i32
  271. ramreadblock(Chan *c, void *buf, i32 n, i64 off)
  272. {
  273. Proc *up = externup();
  274. // first block, last block
  275. int fb = off >> 13, fl = (off + n) >> 13, offinblock = off & 0x1fff;
  276. // adjust read size and offset.
  277. // we only read one block for now.
  278. if(fl != fb){
  279. n = 8192 - (off & 0x1fff);
  280. }
  281. if(fb > NBLOCK)
  282. return 0;
  283. qlock(&ramlock);
  284. if(waserror()){
  285. qunlock(&ramlock);
  286. nexterror();
  287. }
  288. if(c->qid.type == QTDIR){
  289. i32 len = devdirread(c, buf, n, nil, 0, ramgen);
  290. poperror();
  291. qunlock(&ramlock);
  292. return len;
  293. }
  294. // Read file
  295. struct RamFile *file = (void *)c->qid.path;
  296. if(off + n > file->length)
  297. n = file->length - off;
  298. if(file->magic != RAM_MAGIC)
  299. error("ramreadblock:file->magic != RAM_MAGIC");
  300. if(n > 0 && off < file->length && file->blocks[fb])
  301. memmove(buf, file->blocks[fb] + offinblock, n);
  302. else
  303. n = 0;
  304. poperror();
  305. qunlock(&ramlock);
  306. return n;
  307. }
  308. static i32
  309. ramread(Chan *c, void *v, i32 n, i64 off)
  310. {
  311. i32 total = 0, amt;
  312. while(total < n){
  313. amt = ramreadblock(c, v + total, n - total, off + total);
  314. if(amt <= 0)
  315. break;
  316. total += amt;
  317. }
  318. return total;
  319. }
  320. typedef double Align;
  321. typedef union Header Header;
  322. union Header {
  323. struct {
  324. Header *next;
  325. u32 size;
  326. } s;
  327. Align al;
  328. };
  329. static i32
  330. ramwriteblock(Chan *c, void *v, i32 n, i64 off)
  331. {
  332. Proc *up = externup();
  333. struct RamFile *file = (void *)c->qid.path;
  334. // first block, last block
  335. int fb = off >> 13, fl = (off + n) >> 13, offinblock = off & 0x1fff;
  336. // we only write one block.
  337. if(fl != fb){
  338. n = 8192 - (off & 0x1fff);
  339. }
  340. if(fb > NBLOCK)
  341. return 0;
  342. qlock(&ramlock);
  343. if(waserror()){
  344. qunlock(&ramlock);
  345. nexterror();
  346. }
  347. if(file->magic != RAM_MAGIC)
  348. error("ramwriteblock:file->magic != RAM_MAGIC");
  349. if(devramdebug)
  350. print("ramwrite: v %p n %#x off %#llx\n", v, n, off);
  351. if(!file->blocks[fb]){
  352. file->blocks[fb] = mallocz(8192, 1);
  353. if(!file->blocks[fb])
  354. error("ramwriteblock:enomem");
  355. }
  356. if(devramdebug){
  357. print("length of start of write=%do\n", offinblock);
  358. }
  359. memmove(file->blocks[fb] + offinblock, v, n);
  360. if((off + n) > file->length)
  361. file->length = off + n;
  362. poperror();
  363. qunlock(&ramlock);
  364. return n;
  365. }
  366. static i32
  367. ramwrite(Chan *c, void *v, i32 n, i64 off)
  368. {
  369. i32 total = 0, amt;
  370. if(devramdebug)
  371. print("Write %#x %#x %#x \n", v + total, n - total, off + total);
  372. while(total < n){
  373. if(devramdebug)
  374. print("Write block %#x %#x %#x total is %#x\n", v + total, n - total, off + total, total);
  375. amt = ramwriteblock(c, v + total, n - total, off + total);
  376. if(devramdebug)
  377. print("amt %#x\n", amt);
  378. if(amt <= 0)
  379. break;
  380. total += amt;
  381. }
  382. if(devramdebug)
  383. print("ramwrite %#x\n", total);
  384. return total;
  385. }
  386. void
  387. ramcreate(Chan *c, char *name, int omode, int perm)
  388. {
  389. Proc *up = externup();
  390. if(c->qid.type != QTDIR)
  391. error("ramcreate:not a directory");
  392. struct RamFile *parent = (struct RamFile *)c->qid.path;
  393. if(parent->magic != RAM_MAGIC)
  394. error("ramcreate:parent->magic != RAM_MAGIC");
  395. omode = openmode(omode);
  396. struct RamFile *file = (struct RamFile *)malloc(sizeof(struct RamFile));
  397. if(file == nil){
  398. error("ramcreate:no memory to create file struct");
  399. }
  400. file->length = 0;
  401. file->magic = RAM_MAGIC;
  402. file->n = smalloc(strlen(name) + 1);
  403. kstrdup(&file->n, name);
  404. file->perm = perm;
  405. file->parent = parent;
  406. qlock(&ramlock);
  407. if(waserror()){
  408. free(file->n);
  409. free(file);
  410. qunlock(&ramlock);
  411. nexterror();
  412. }
  413. file->sibling = parent->firstchild;
  414. parent->firstchild = file;
  415. mkqid(&c->qid, (usize)file, 0, file->perm & DMDIR ? QTDIR : 0);
  416. c->offset = 0;
  417. c->mode = omode;
  418. c->flag |= COPEN;
  419. qunlock(&ramlock);
  420. poperror();
  421. }
  422. void
  423. ramshutdown(void)
  424. {
  425. }
  426. void
  427. ramremove(Chan *c)
  428. {
  429. struct RamFile *doomed = (struct RamFile *)c->qid.path;
  430. if(doomed->magic != RAM_MAGIC)
  431. error("ramremove:file->magic != RAM_MAGIC");
  432. if(doomed->opencount == 0){
  433. delete(doomed);
  434. }
  435. doomed->deleteonclose = 1;
  436. }
  437. Dev ramdevtab = {
  438. .dc = '@',
  439. .name = "ram",
  440. .reset = ramreset,
  441. .init = raminit,
  442. .shutdown = ramshutdown,
  443. .attach = ramattach,
  444. .walk = ramwalk,
  445. .stat = ramstat,
  446. .open = ramopen,
  447. .create = ramcreate,
  448. .close = ramclose,
  449. .read = ramread,
  450. .bread = devbread,
  451. .write = ramwrite,
  452. .bwrite = devbwrite,
  453. .remove = ramremove,
  454. .wstat = ramwstat,
  455. };