devramfs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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_BLOCK_LEN 32768
  16. #define RAM_MAGIC 0xbedabb1e
  17. #define INVALID_FILE "Invalid ram file"
  18. struct RamFile {
  19. unsigned int magic;
  20. char name[KNAMELEN];
  21. struct RamFile *parent;
  22. struct RamFile *sibling;
  23. uint64_t length;
  24. uint64_t alloclength;
  25. int busy;
  26. int perm;
  27. int opencount;
  28. int deleteonclose;
  29. union {
  30. uint8_t *data; // List of children if directory
  31. struct RamFile* firstchild;
  32. };
  33. };
  34. static struct RamFile *ramroot;
  35. static QLock ramlock;
  36. static void
  37. printramfile(int offset, struct RamFile* file)
  38. {
  39. int i;
  40. for(i = 0; i < offset; i++) {
  41. print(" ");
  42. }
  43. print("ramfile: %x, magic:%x, name: %s, parent: %x, sibling:%x, length:%d, alloclength: %d, perm: %o\n",
  44. file, file->magic, file->name, file->parent, file->sibling, file->length, file->alloclength, file->perm);
  45. }
  46. static void
  47. debugwalkinternal(int offset, struct RamFile* current)
  48. {
  49. printramfile(offset, current);
  50. for (current = current->firstchild; current != nil; current = current->sibling) {
  51. if(current->perm & DMDIR){
  52. debugwalkinternal(offset+1, current);
  53. }else{
  54. printramfile(offset,current);
  55. }
  56. }
  57. }
  58. static void
  59. debugwalk()
  60. {
  61. print("***********************\n");
  62. debugwalkinternal(0, ramroot);
  63. }
  64. static void
  65. raminit(void)
  66. {
  67. ramroot = (struct RamFile *)malloc(sizeof(struct RamFile));
  68. if (ramroot == nil) {
  69. error(Eperm);
  70. }
  71. strcpy(ramroot->name, ".");
  72. ramroot->magic = RAM_MAGIC;
  73. ramroot->length = 0;
  74. ramroot->alloclength = 0;
  75. ramroot->perm = DMDIR|0777;
  76. ramroot->firstchild = nil;
  77. }
  78. static void
  79. ramreset(void)
  80. {
  81. }
  82. static Chan*
  83. ramattach(char *spec)
  84. {
  85. Chan *c;
  86. char *buf;
  87. c = newchan();
  88. mkqid(&c->qid, (int64_t)ramroot, 0, QTDIR);
  89. c->dev = devtabget('@', 0);
  90. if(spec == nil)
  91. spec = "";
  92. buf = malloc(1+UTFmax+strlen(spec)+1);
  93. if (buf == nil) {
  94. error(Eperm);
  95. }
  96. sprint(buf, "#@%s", spec);
  97. c->path = newpath(buf);
  98. c->mode = 0777;
  99. free(buf);
  100. return c;
  101. }
  102. static int
  103. ramgen(Chan *c, char *name, Dirtab *tab, int ntab, int pos, Dir *dp)
  104. {
  105. Qid qid;
  106. int i;
  107. struct RamFile *current = (struct RamFile *)c->qid.path;
  108. if(pos == DEVDOTDOT){
  109. if(current->parent == nil){
  110. mkqid(&qid, (uintptr_t)current, 0, QTDIR);
  111. devdir(c, qid, "#@", 0, "harvey", 0555, dp);
  112. return 1;
  113. } else {
  114. mkqid(&qid, (uintptr_t)current->parent, 0, QTDIR);
  115. devdir(c, qid, current->name, 0, "harvey", 0555, dp);
  116. return 1;
  117. }
  118. }
  119. if(current->perm & QTDIR){
  120. current = current->firstchild;
  121. if(current == nil){
  122. return -1;
  123. }
  124. }
  125. for(i = 0; i < pos; i++){
  126. current = current->sibling;
  127. if (current == nil){
  128. return -1;
  129. }
  130. }
  131. mkqid(&qid, (uintptr_t)current, 0, current->perm & DMDIR ? QTDIR : 0);
  132. devdir(c, qid, current->name, current->length, "harvey", current->perm, dp);
  133. if(name == nil || strcmp(current->name, name) == 0){
  134. return 1;
  135. } else {
  136. return 0;
  137. }
  138. }
  139. static Walkqid*
  140. ramwalk(Chan *c, Chan *nc, char **name, int nname)
  141. {
  142. Proc *up = externup();
  143. qlock(&ramlock);
  144. if(waserror()){
  145. qunlock(&ramlock);
  146. nexterror();
  147. }
  148. Walkqid* wqid = devwalk(c, nc, name, nname, 0, 0, ramgen);
  149. qunlock(&ramlock);
  150. poperror();
  151. return wqid;
  152. }
  153. static int32_t
  154. ramstat(Chan *c, uint8_t *dp, int32_t n)
  155. {
  156. Dir dir;
  157. Qid qid;
  158. struct RamFile* current = (struct RamFile*)c->qid.path;
  159. if (current->magic != RAM_MAGIC)
  160. error(INVALID_FILE);
  161. qlock(&ramlock);
  162. mkqid(&qid, c->qid.path, 0, current->perm & DMDIR ? QTDIR : 0);
  163. devdir(c, qid, current->name, current->length, "harvey", 0555, &dir);
  164. int32_t ret = convD2M(&dir, dp, n);
  165. qunlock(&ramlock);
  166. return ret;
  167. }
  168. static Chan*
  169. ramopen(Chan *c, int omode)
  170. {
  171. Proc *up = externup();
  172. qlock(&ramlock);
  173. if(waserror()){
  174. qunlock(&ramlock);
  175. nexterror();
  176. }
  177. Chan* ret = devopen(c, omode, nil, 0, ramgen);
  178. struct RamFile* file = (struct RamFile*)c->qid.path;
  179. if (file->magic != RAM_MAGIC)
  180. error(INVALID_FILE);
  181. file->busy++;
  182. qunlock(&ramlock);
  183. poperror();
  184. return ret;
  185. }
  186. static void
  187. delete(struct RamFile* file)
  188. {
  189. qlock(&ramlock);
  190. //printramfile(0, file);
  191. //debugwalk();
  192. struct RamFile* prev = file->parent->firstchild;
  193. if(prev == file) {
  194. // This is the first file - make any sibling the first child
  195. file->parent->firstchild = file->sibling;
  196. } else {
  197. // Find previous file
  198. for(; prev != nil && prev->sibling != file; prev = prev->sibling)
  199. ;
  200. if(prev == nil){
  201. qunlock(&ramlock);
  202. error(Eperm);
  203. } else {
  204. prev->sibling = file->sibling;
  205. }
  206. }
  207. if(!file->perm & DMDIR){
  208. free(file->data);
  209. }
  210. file->magic = 0;
  211. free(file);
  212. if (0) debugwalk();
  213. qunlock(&ramlock);
  214. }
  215. static void
  216. ramclose(Chan* c)
  217. {
  218. struct RamFile* file = (struct RamFile *)c->qid.path;
  219. if (file->magic != RAM_MAGIC)
  220. error(INVALID_FILE);
  221. qlock(&ramlock);
  222. file->busy--;
  223. qunlock(&ramlock);
  224. if(file->deleteonclose){
  225. delete(file);
  226. }
  227. }
  228. static int32_t
  229. ramread(Chan *c, void *buf, int32_t n, int64_t off)
  230. {
  231. qlock(&ramlock);
  232. if (c->qid.type == QTDIR){
  233. int32_t len = devdirread(c, buf, n, nil, 0, ramgen);
  234. qunlock(&ramlock);
  235. return len;
  236. }
  237. // Read file
  238. struct RamFile *file = (void*)c->qid.path;
  239. if (file->magic != RAM_MAGIC)
  240. error(INVALID_FILE);
  241. int filelen = file->length;
  242. if (off > filelen){
  243. qunlock(&ramlock);
  244. return 0;
  245. }
  246. if (off + n > filelen){
  247. n = filelen - off;
  248. }
  249. memmove(buf, file->data, n);
  250. qunlock(&ramlock);
  251. return n;
  252. }
  253. typedef double Align;
  254. typedef union Header Header;
  255. union Header {
  256. struct {
  257. Header* next;
  258. uint size;
  259. } s;
  260. Align al;
  261. };
  262. static int32_t
  263. ramwrite(Chan* c, void* v, int32_t n, int64_t off)
  264. {
  265. // Header *p;
  266. qlock(&ramlock);
  267. struct RamFile *file = (void*)c->qid.path;
  268. if (file->magic != RAM_MAGIC)
  269. error(INVALID_FILE);
  270. if(n+off >= file->length){
  271. uint64_t alloclength = file->alloclength;
  272. while(alloclength < n + off)
  273. alloclength += RAM_BLOCK_LEN;
  274. if(alloclength > file->alloclength){
  275. void *newfile = realloc(file->data, alloclength);
  276. if(newfile == nil){
  277. qunlock(&ramlock);
  278. return 0;
  279. }
  280. file->data = newfile;
  281. file->alloclength = alloclength;
  282. }
  283. file->length = n+off;
  284. }
  285. // p = (Header*)file->data - 1;
  286. // print("length of buffer=%d, header size=%d, header next=%x, start of write=%d, end of write=%d\n", file->alloclength, p->s.size, p->s.next, off, off + n);
  287. memmove(file->data + off, v, n);
  288. qunlock(&ramlock);
  289. return n;
  290. }
  291. void
  292. ramcreate(Chan* c, char *name, int omode, int perm)
  293. {
  294. Proc *up = externup();
  295. if(c->qid.type != QTDIR)
  296. error(Eperm);
  297. struct RamFile* parent = (struct RamFile *)c->qid.path;
  298. if (parent->magic != RAM_MAGIC)
  299. error(INVALID_FILE);
  300. omode = openmode(omode);
  301. struct RamFile* file = (struct RamFile*)malloc(sizeof(struct RamFile));
  302. if (file == nil) {
  303. error(Eperm);
  304. }
  305. file->length = 0;
  306. file->magic = RAM_MAGIC;
  307. strcpy(file->name, name);
  308. file->perm = perm;
  309. file->parent = parent;
  310. file->busy = 1;
  311. qlock(&ramlock);
  312. if(waserror()) {
  313. free(file->data);
  314. free(file);
  315. qunlock(&ramlock);
  316. nexterror();
  317. }
  318. file->sibling = parent->firstchild;
  319. parent->firstchild = file;
  320. mkqid(&c->qid, (uintptr_t)file, 0, file->perm & DMDIR ? QTDIR : 0);
  321. c->offset = 0;
  322. c->mode = omode;
  323. c->flag |= COPEN;
  324. qunlock(&ramlock);
  325. poperror();
  326. }
  327. void
  328. ramshutdown(void)
  329. {
  330. }
  331. void
  332. ramremove(Chan* c)
  333. {
  334. struct RamFile* doomed = (struct RamFile *)c->qid.path;
  335. if (doomed->magic != RAM_MAGIC)
  336. error(INVALID_FILE);
  337. if(doomed->opencount == 0){
  338. delete(doomed);
  339. }
  340. doomed->deleteonclose =1;
  341. }
  342. Dev ramdevtab = {
  343. .dc = '@',
  344. .name = "ram",
  345. .reset = ramreset,
  346. .init = raminit,
  347. .shutdown = ramshutdown,
  348. .attach = ramattach,
  349. .walk = ramwalk,
  350. .stat = ramstat,
  351. .open = ramopen,
  352. .create = ramcreate,
  353. .close = ramclose,
  354. .read = ramread,
  355. .bread = devbread,
  356. .write = ramwrite,
  357. .bwrite = devbwrite,
  358. .remove = ramremove,
  359. .wstat = devwstat,
  360. };