devramfs.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 perm;
  26. int opencount;
  27. int deleteonclose;
  28. union {
  29. uint8_t *data; // List of children if directory
  30. struct RamFile* firstchild;
  31. };
  32. };
  33. static struct RamFile *ramroot;
  34. static QLock ramlock;
  35. static int devramdebug = 0;
  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. panic("Invalid ram file");
  181. qunlock(&ramlock);
  182. poperror();
  183. return ret;
  184. }
  185. static void
  186. delete(struct RamFile* file)
  187. {
  188. qlock(&ramlock);
  189. struct RamFile* prev = file->parent->firstchild;
  190. if(prev == file) {
  191. // This is the first file - make any sibling the first child
  192. file->parent->firstchild = file->sibling;
  193. } else {
  194. // Find previous file
  195. for(; prev != nil && prev->sibling != file; prev = prev->sibling)
  196. ;
  197. if(prev == nil){
  198. qunlock(&ramlock);
  199. error(Eperm);
  200. } else {
  201. prev->sibling = file->sibling;
  202. }
  203. }
  204. if(!file->perm & DMDIR){
  205. free(file->data);
  206. }
  207. file->magic = 0;
  208. free(file);
  209. if (devramdebug) debugwalk();
  210. qunlock(&ramlock);
  211. }
  212. static void
  213. ramclose(Chan* c)
  214. {
  215. struct RamFile* file = (struct RamFile *)c->qid.path;
  216. if (file->magic != RAM_MAGIC)
  217. error(INVALID_FILE);
  218. if(file->deleteonclose){
  219. delete(file);
  220. }
  221. }
  222. static int32_t
  223. ramread(Chan *c, void *buf, int32_t n, int64_t off)
  224. {
  225. qlock(&ramlock);
  226. if (c->qid.type == QTDIR){
  227. int32_t len = devdirread(c, buf, n, nil, 0, ramgen);
  228. qunlock(&ramlock);
  229. return len;
  230. }
  231. // Read file
  232. struct RamFile *file = (void*)c->qid.path;
  233. if (file->magic != RAM_MAGIC)
  234. error(INVALID_FILE);
  235. int filelen = file->length;
  236. if (off > filelen){
  237. qunlock(&ramlock);
  238. return 0;
  239. }
  240. if (off + n > filelen){
  241. n = filelen - off;
  242. }
  243. memmove(buf, file->data, n);
  244. qunlock(&ramlock);
  245. return n;
  246. }
  247. typedef double Align;
  248. typedef union Header Header;
  249. union Header {
  250. struct {
  251. Header* next;
  252. uint size;
  253. } s;
  254. Align al;
  255. };
  256. static int32_t
  257. ramwrite(Chan* c, void* v, int32_t n, int64_t off)
  258. {
  259. Header *p;
  260. qlock(&ramlock);
  261. struct RamFile *file = (void*)c->qid.path;
  262. if (file->magic != RAM_MAGIC)
  263. error(INVALID_FILE);
  264. if(n+off >= file->length){
  265. uint64_t alloclength = file->alloclength;
  266. while(alloclength < n + off)
  267. alloclength += RAM_BLOCK_LEN;
  268. if(alloclength > file->alloclength){
  269. void *newfile = realloc(file->data, alloclength);
  270. if(newfile == nil){
  271. qunlock(&ramlock);
  272. return 0;
  273. }
  274. file->data = newfile;
  275. file->alloclength = alloclength;
  276. }
  277. file->length = n+off;
  278. }
  279. if (devramdebug) {
  280. p = (Header*)file->data - 1;
  281. 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);
  282. }
  283. memmove(file->data + off, v, n);
  284. qunlock(&ramlock);
  285. return n;
  286. }
  287. void
  288. ramcreate(Chan* c, char *name, int omode, int perm)
  289. {
  290. Proc *up = externup();
  291. if(c->qid.type != QTDIR)
  292. error(Eperm);
  293. struct RamFile* parent = (struct RamFile *)c->qid.path;
  294. if (parent->magic != RAM_MAGIC)
  295. error(INVALID_FILE);
  296. omode = openmode(omode);
  297. struct RamFile* file = (struct RamFile*)malloc(sizeof(struct RamFile));
  298. if (file == nil) {
  299. error(Eperm);
  300. }
  301. file->length = 0;
  302. file->magic = RAM_MAGIC;
  303. strcpy(file->name, name);
  304. file->perm = perm;
  305. file->parent = parent;
  306. qlock(&ramlock);
  307. if(waserror()) {
  308. free(file->data);
  309. free(file);
  310. qunlock(&ramlock);
  311. nexterror();
  312. }
  313. file->sibling = parent->firstchild;
  314. parent->firstchild = file;
  315. mkqid(&c->qid, (uintptr_t)file, 0, file->perm & DMDIR ? QTDIR : 0);
  316. c->offset = 0;
  317. c->mode = omode;
  318. c->flag |= COPEN;
  319. qunlock(&ramlock);
  320. poperror();
  321. }
  322. void
  323. ramshutdown(void)
  324. {
  325. }
  326. void
  327. ramremove(Chan* c)
  328. {
  329. struct RamFile* doomed = (struct RamFile *)c->qid.path;
  330. if (doomed->magic != RAM_MAGIC)
  331. error(INVALID_FILE);
  332. if(doomed->opencount == 0){
  333. delete(doomed);
  334. }
  335. doomed->deleteonclose =1;
  336. }
  337. Dev ramdevtab = {
  338. .dc = '@',
  339. .name = "ram",
  340. .reset = ramreset,
  341. .init = raminit,
  342. .shutdown = ramshutdown,
  343. .attach = ramattach,
  344. .walk = ramwalk,
  345. .stat = ramstat,
  346. .open = ramopen,
  347. .create = ramcreate,
  348. .close = ramclose,
  349. .read = ramread,
  350. .bread = devbread,
  351. .write = ramwrite,
  352. .bwrite = devbwrite,
  353. .remove = ramremove,
  354. .wstat = devwstat,
  355. };