initcfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of CFS. */
  3. /*
  4. CFS is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. CFS is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with CFS. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <sys/fcntl.h>
  25. #include <sys/types.h>
  26. #include <sys/time.h>
  27. #define __CFS
  28. #include "cfs.h"
  29. void fill_super(int);
  30. void init_bim_buffer(int);
  31. __bn_t bmap(int, __inode_t, __bn_t);
  32. __bn_t create_block(int, __inode_t, __bn_t);
  33. void lsdir(int, struct inode *);
  34. struct inode *search_file(int, struct inode *, char *);
  35. struct inode *create_file(int, struct inode *, char *, __uint8_t);
  36. struct inode *mkdir(int, struct inode *, char *, __uint8_t);
  37. __uint32_t gettime(void);
  38. __uint32_t write_file(int, struct inode *, char *, __uint32_t);
  39. __uint32_t read_file(int, struct inode *, char *, __uint32_t);
  40. void dsync(int, struct inode *, char *);
  41. #define sync_sb() \
  42. lseek(fd, 512, SEEK_SET); \
  43. write(fd, &sb, sizeof(struct superblock)); \
  44. lseek(fd, 0, SEEK_SET);
  45. int main(int argc, const char **argv) {
  46. char *fname = (char *) argv[1];
  47. int fd, bootfd, loaderfd, sysfd;
  48. if (argc < 5) {
  49. printf("fatal: excepted four arguments, but %d given. \n", argc - 1);
  50. printf("usage: %s device boot loader ksetup\n", argv[0]);
  51. return 1;
  52. }
  53. fd = open(fname, O_RDWR);
  54. if (fd < 0) {
  55. printf("fatal: failed when try to open %s: %s\n", fname, strerror(errno));
  56. return 1;
  57. }
  58. fill_super(fd);
  59. init_bim_buffer(fd);
  60. /* write boot to mbr, loader to that space that after superblock */
  61. bootfd = open(argv[2], O_RDONLY);
  62. loaderfd = open(argv[3], O_RDONLY);
  63. /* sys (vmxnix) to a file that called vmxnix */
  64. sysfd = open(argv[4], O_RDONLY);
  65. __uint32_t fsize = lseek(sysfd, 0, SEEK_END);
  66. lseek(sysfd, 0, SEEK_SET);
  67. char *buf = malloc(fsize);
  68. if (!buf) {
  69. printf("fatal: failed when allocate space: %u bytes need. ", fsize);
  70. close(fd);
  71. close(bootfd);
  72. close(sysfd);
  73. }
  74. int bytesread;
  75. bytesread = read(bootfd, buf, 512);
  76. lseek(fd, 0, SEEK_SET);
  77. write(fd, buf, 512);
  78. close(bootfd);
  79. printf("boot: %d bytes. \n", bytesread);
  80. /* max bytes for loader */
  81. bytesread = read(loaderfd, buf, BLOCK_SIZE - 512 - sizeof(struct superblock));
  82. lseek(fd, 512 + sizeof(struct superblock), SEEK_SET);
  83. write(fd, buf, bytesread);
  84. close(loaderfd);
  85. printf("loader: %d bytes\n", bytesread);
  86. bytesread = read(sysfd, buf, fsize);
  87. struct inode *ino = create_file(fd, &sb.root_ino, "ksetup",
  88. A_ROOTRD | A_USERRD | A_REGFILE | A_EXEC);
  89. if (!ino)
  90. printf("file vmxnix exists, not continuing. \n");
  91. else
  92. write_file(fd, ino, buf, fsize);
  93. close(fd);
  94. printf("vmxnix: %d bytes. \n", bytesread);
  95. return 0;
  96. }
  97. void fill_super(int fd) {
  98. /* read super block from fd */
  99. lseek(fd, 512, SEEK_SET);
  100. read(fd, &sb, sizeof(struct superblock));
  101. if (sb.magic != MAGIC) {
  102. printf("fatal: bad magic number. \n");
  103. close(fd);
  104. exit(1);
  105. }
  106. return;
  107. }
  108. /*
  109. * initial the bim buffer. (struct bim_buffer).
  110. * : read the first block of bim to buffer, and
  111. * clear 'dirty'.
  112. * */
  113. void init_bim_buffer(int fd) {
  114. bimbuf.this_block = sb.bim_bn;
  115. bimbuf.dirty = 0;
  116. bimbuf.bn_start = sb.first_data_block;
  117. bimbuf.buffer = (struct bim_entry *) malloc(BLOCK_SIZE);
  118. if (!bimbuf.buffer) {
  119. printf("fatal: unable to allocate space for bim buffer. (%d bytes requested) \n", BLOCK_SIZE);
  120. close(fd);
  121. exit(1);
  122. }
  123. lseek(fd, sb.bim_bn << BLOCK_SHIFT, SEEK_SET);
  124. read(fd, bimbuf.buffer, BLOCK_SIZE);
  125. return;
  126. }
  127. void read_inode(int fd, __inode_t ino, struct inode *inode) {
  128. lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + ino * sizeof(struct inode), SEEK_SET);
  129. read(fd, inode, sizeof(struct inode));
  130. return;
  131. }
  132. void write_inode(int fd, __inode_t ino, struct inode *inode) {
  133. lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + ino * sizeof(struct inode), SEEK_SET);
  134. write(fd, inode, sizeof(struct inode));
  135. return;
  136. }
  137. /*
  138. * a function that used to rw inodes. (get the `bn`th block of inode `ino`)
  139. *
  140. * STEPS:
  141. * 1. check in bim buffer.
  142. * 1a. (found): return real block.
  143. * 1b, (not-found): read buffer again (next bim block) and check again.
  144. * */
  145. __bn_t bmap(int fd, __inode_t ino, __bn_t bn) {
  146. __bn_t i;
  147. struct bim_entry *buf;
  148. repeat:
  149. buf = bimbuf.buffer;
  150. /* search in buf */
  151. for (i = 0; i < BLOCK_SIZE / sizeof(struct bim_entry); i++) {
  152. if (buf[i].inode == ino && buf[i].th_block == bn)
  153. return i + bimbuf.bn_start;
  154. }
  155. /* not found, so load buffer again and repeat */
  156. bimbuf.this_block++;
  157. bimbuf.dirty = 0;
  158. /* += how many blocks it can describe */
  159. bimbuf.bn_start += BLOCK_SIZE / sizeof(struct bim_entry);
  160. lseek(fd, bimbuf.this_block << BLOCK_SHIFT, SEEK_SET);
  161. read(fd, bimbuf.buffer, BLOCK_SIZE);
  162. goto repeat;
  163. return 0;
  164. }
  165. void sync_bimbuf(int fd) {
  166. lseek(fd, bimbuf.this_block << BLOCK_SHIFT, SEEK_SET);
  167. write(fd, bimbuf.buffer, BLOCK_SIZE);
  168. bimbuf.dirty = 0;
  169. return;
  170. }
  171. /*
  172. * find a new free block, and register it in bim buffer and device.
  173. * */
  174. __bn_t create_block(int fd, __inode_t ino, __bn_t bn) {
  175. __bn_t i;
  176. struct bim_entry *buf;
  177. repeat:
  178. buf = bimbuf.buffer;
  179. for (i = 0; i < BLOCK_SIZE / sizeof(struct bim_entry); i++) {
  180. if (!buf[i].inode) {
  181. /* register it in buffer first */
  182. buf[i].inode = ino;
  183. buf[i].th_block = bn;
  184. /* and write it back to device (sync) */
  185. sync_bimbuf(fd);
  186. return i + bimbuf.bn_start;
  187. }
  188. }
  189. bimbuf.this_block++;
  190. bimbuf.dirty = 0;
  191. bimbuf.bn_start += BLOCK_SIZE / sizeof(struct bim_entry);
  192. read(fd, bimbuf.buffer, BLOCK_SIZE);
  193. goto repeat;
  194. return 0;
  195. }
  196. /* list the files in root directory, until it's empty */
  197. void lsdir(int fd, struct inode *dir) {
  198. int i;
  199. struct dentry *entries = (struct dentry *) malloc(BLOCK_SIZE);
  200. for (i = 0; i < dir->blocks_nr; i++) {
  201. lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
  202. read(fd, entries, BLOCK_SIZE);
  203. int j;
  204. for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry) && entries[j].ino; j++) {
  205. if (!entries[j].ino) continue;
  206. printf("%d: %s\n", j, entries[j].name);
  207. }
  208. }
  209. return;
  210. }
  211. /*
  212. * search file 'name' in directory 'dir'.
  213. *
  214. * 'name' is only a file name, unlike '/foo.bar' or '/foo/bar.foo'
  215. * */
  216. struct inode *search_file(int fd, struct inode *dir, char *name) {
  217. /* read root directory until find file 'name' */
  218. int i;
  219. struct dentry *entries = (struct dentry *) malloc(BLOCK_SIZE);
  220. struct inode *ino = malloc(sizeof(struct inode));
  221. for (i = 0; i < dir->blocks_nr; i++) {
  222. lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
  223. read(fd, entries, BLOCK_SIZE);
  224. int j;
  225. for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
  226. if (strncmp(entries[j].name, name, FNAME_LEN) == 0) {
  227. read_inode(fd, entries[j].ino, ino);
  228. return ino;
  229. }
  230. }
  231. }
  232. /* root directory is dirty so it runs here. */
  233. return NULL;
  234. }
  235. /*
  236. * sync the directory 'inode' (write all data blocks back)
  237. * (calls from create file or other functions that changed inode of the
  238. * child of this directory)
  239. * */
  240. void dsync(int fd, struct inode *inode, char *entries) {
  241. int i;
  242. for (i = 0; i < inode->blocks_nr; i++) {
  243. lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
  244. write(fd, entries + (i << BLOCK_SHIFT), BLOCK_SIZE);
  245. }
  246. return;
  247. }
  248. /*
  249. * create a file 'name' in directory 'dir' with access mode 'acc'.
  250. * */
  251. struct inode *create_file(int fd, struct inode *dir, char *name, __uint8_t acc) {
  252. /* find a free entry in root directory */
  253. int i;
  254. struct dentry entries[BLOCK_SIZE / sizeof(struct dentry)];
  255. struct inode *ino = malloc(sizeof(struct inode));
  256. memset(ino, 0x00, sizeof(struct inode));
  257. for (i = 0; i < sb.root_ino.blocks_nr; i++) {
  258. lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
  259. read(fd, entries, BLOCK_SIZE);
  260. int j;
  261. /* first: check this file is non exists */
  262. for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
  263. if (strncmp(entries[j].name, name, FNAME_LEN) == 0) {
  264. printf("create_file: failed: file '%s' exists (inode %d)\n",
  265. name, entries[j].ino);
  266. return 0;
  267. }
  268. }
  269. for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
  270. if (!entries[j].ino) {
  271. ino->inode = entries[j].ino = sb.next_inode++;
  272. ino->pinode = ROOT_INO;
  273. ino->access = acc;
  274. ino->blocks_nr = ino->upsize = 0;
  275. ino->ctime = ino->wtime = gettime();
  276. ino->nlinks = 1;
  277. ino->uid = ino->gid = 0;
  278. ino->eaccess = 0;
  279. /* copy file name */
  280. strncpy(entries[j].name, name, FNAME_LEN);
  281. sync_sb();
  282. dsync(fd, dir, (char *) entries);
  283. return ino;
  284. }
  285. }
  286. }
  287. printf("fatal: create_file: no enough space in this directory\n");
  288. return 0;
  289. }
  290. __uint32_t write_file(int fd, struct inode *inode, char *__buf, __uint32_t nbytes) {
  291. /* bad bad, we shouldn't do this */
  292. char *buffer = malloc(ALIGNUP_BLOCK(nbytes));
  293. int i;
  294. __uint32_t nblocks = ALIGNUP_BLOCK(nbytes) >> BLOCK_SHIFT;
  295. memcpy(buffer, __buf, nbytes);
  296. for (i = 0; i < nblocks; i++) {
  297. lseek(fd, create_block(fd, inode->inode, inode->blocks_nr + i) << BLOCK_SHIFT, SEEK_SET);
  298. write(fd, buffer + (i << BLOCK_SHIFT), BLOCK_SIZE);
  299. }
  300. inode->blocks_nr = (nbytes & BLOCK_MASK) >> BLOCK_SHIFT;
  301. inode->upsize = nbytes - (inode->blocks_nr << BLOCK_SHIFT);
  302. inode->wtime = gettime();
  303. free(buffer);
  304. /* sync, then we can return */
  305. write_inode(fd, inode->inode, inode);
  306. return nbytes;
  307. }
  308. __uint32_t read_file(int fd, struct inode *inode, char *__buf, __uint32_t nbytes) {
  309. /* first read blocks that aligned */
  310. __uint32_t nblocks = (nbytes & BLOCK_MASK) >> BLOCK_SHIFT;
  311. int i;
  312. for (i = 0; i < nblocks; i++) {
  313. lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
  314. read(fd, __buf + (i << BLOCK_SHIFT), BLOCK_SIZE);
  315. }
  316. char buf[BLOCK_SIZE];
  317. lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
  318. read(fd, buf, BLOCK_SIZE);
  319. memcpy(__buf + (i << BLOCK_SHIFT), buf, nbytes - (nbytes & BLOCK_MASK));
  320. return nbytes;
  321. }
  322. __uint32_t gettime(void) {
  323. struct timeval t = {0};
  324. gettimeofday(&t, NULL);
  325. return t.tv_sec;
  326. }
  327. struct inode *mkdir(int fd, struct inode *dir, char *name, __uint8_t acc) {
  328. struct inode *new_dir = malloc(sizeof(struct inode));
  329. /* first create a file in directory 'dir' */
  330. if (!(new_dir = create_file(fd, dir, name, acc & A_DIR & A_EXEC))) {
  331. return NULL;
  332. }
  333. new_dir->blocks_nr = 1;
  334. create_block(fd, new_dir->inode, 0);
  335. /* then create '.' and '..' in new directory */
  336. create_file(fd, new_dir, ".", A_ROOT_RW | A_USER_RW | A_DIR | A_LINK);
  337. create_file(fd, new_dir, "..", A_ROOT_RW | A_USER_RW | A_DIR | A_LINK);
  338. write_inode(fd, new_dir->inode, new_dir);
  339. return new_dir;
  340. }