cfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. static __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. #ifdef DEBUG
  46. int main(int argc, const char **argv) {
  47. char *fname = (char *) argv[1];
  48. int fd;
  49. if (!fname) {
  50. printf("fatal: excepted one argument 'file name', but %d given. \n", argc - 1);
  51. printf("usage: %s device\n", argv[0]);
  52. return 1;
  53. }
  54. fd = open(fname, O_RDWR);
  55. if (fd < 0) {
  56. printf("fatal: failed when try to open %s: %s\n", fname, strerror(errno));
  57. return 1;
  58. }
  59. fill_super(fd);
  60. init_bim_buffer(fd);
  61. lsdir(fd, &sb.root_ino);
  62. struct inode *inode;
  63. struct inode *cyyds_c = create_file(fd, &sb.root_ino, "cyyds.c", A_ROOT_RW | A_USER_RW | A_REGFILE);
  64. if (cyyds_c) {
  65. printf("writing to cyyds.c\n");
  66. char buffer[] = "C is the best language in the wowrld! I will love it forever. because it's very fast and very clear. I love it. ";
  67. write_file(fd, cyyds_c, buffer, strlen(buffer));
  68. /* write back that inode (not data) */
  69. sync_sb();
  70. }
  71. char buf[250];
  72. inode = search_file(fd, &sb.root_ino, "cyyds.c");
  73. read_file(fd, inode, buf, 250);
  74. printf("\n%s\n", buf);
  75. inode = mkdir(fd, &sb.root_ino, "usr", A_ROOT_RW | A_USER_RW);
  76. if (inode)
  77. printf("new directory 'usr'\n");
  78. lsdir(fd, &sb.root_ino);
  79. printf("listing /usr/: \n");
  80. if (!inode)
  81. inode = search_file(fd, &sb.root_ino, "usr");
  82. lsdir(fd, inode);
  83. close(fd);
  84. return 0;
  85. }
  86. #endif
  87. int cfs_init(int fd) {
  88. fs_fd = fd;
  89. fill_super(fd);
  90. init_bim_buffer(fd);
  91. return 0;
  92. }
  93. void fill_super(int fd) {
  94. /* read super block from fd */
  95. lseek(fd, 512, SEEK_SET);
  96. read(fd, &sb, sizeof(struct superblock));
  97. if (sb.magic != MAGIC) {
  98. printf("fatal: bad magic number. \n");
  99. close(fd);
  100. exit(1);
  101. }
  102. return;
  103. }
  104. /*
  105. * initial the bim buffer. (struct bim_buffer).
  106. * : read the first block of bim to buffer, and
  107. * clear 'dirty'.
  108. * */
  109. void init_bim_buffer(int fd) {
  110. bimbuf.this_block = sb.bim_bn;
  111. bimbuf.dirty = 0;
  112. bimbuf.bn_start = sb.first_data_block;
  113. bimbuf.buffer = (struct bim_entry *) malloc(BLOCK_SIZE);
  114. if (!bimbuf.buffer) {
  115. printf("fatal: unable to allocate space for bim buffer. (%d bytes requested) \n", BLOCK_SIZE);
  116. close(fd);
  117. exit(1);
  118. }
  119. lseek(fd, sb.bim_bn << BLOCK_SHIFT, SEEK_SET);
  120. read(fd, bimbuf.buffer, BLOCK_SIZE);
  121. return;
  122. }
  123. void read_inode(int fd, __inode_t ino, struct inode *inode) {
  124. lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + ino * sizeof(struct inode), SEEK_SET);
  125. read(fd, inode, sizeof(struct inode));
  126. return;
  127. }
  128. void write_inode(int fd, __inode_t ino, struct inode *inode) {
  129. lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + ino * sizeof(struct inode), SEEK_SET);
  130. write(fd, inode, sizeof(struct inode));
  131. return;
  132. }
  133. /*
  134. * a function that used to rw inodes. (get the `bn`th block of inode `ino`)
  135. *
  136. * STEPS:
  137. * 1. check in bim buffer.
  138. * 1a. (found): return real block.
  139. * 1b, (not-found): read buffer again (next bim block) and check again.
  140. * */
  141. __bn_t bmap(int fd, __inode_t ino, __bn_t bn) {
  142. __bn_t i;
  143. struct bim_entry *buf;
  144. repeat:
  145. buf = bimbuf.buffer;
  146. /* search in buf */
  147. for (i = 0; i < BLOCK_SIZE / sizeof(struct bim_entry); i++) {
  148. if (buf[i].inode == ino && buf[i].th_block == bn)
  149. return i + bimbuf.bn_start;
  150. }
  151. /* not found, so load buffer again and repeat */
  152. bimbuf.this_block++;
  153. bimbuf.dirty = 0;
  154. /* += how many blocks it can describe */
  155. bimbuf.bn_start += BLOCK_SIZE / sizeof(struct bim_entry);
  156. lseek(fd, bimbuf.this_block << BLOCK_SHIFT, SEEK_SET);
  157. read(fd, bimbuf.buffer, BLOCK_SIZE);
  158. goto repeat;
  159. return 0;
  160. }
  161. void sync_bimbuf(int fd) {
  162. lseek(fd, bimbuf.this_block << BLOCK_SHIFT, SEEK_SET);
  163. write(fd, bimbuf.buffer, BLOCK_SIZE);
  164. bimbuf.dirty = 0;
  165. return;
  166. }
  167. /*
  168. * find a new free block, and register it in bim buffer and device.
  169. * */
  170. __bn_t create_block(int fd, __inode_t ino, __bn_t bn) {
  171. __bn_t i;
  172. struct bim_entry *buf;
  173. repeat:
  174. buf = bimbuf.buffer;
  175. for (i = 0; i < BLOCK_SIZE / sizeof(struct bim_entry); i++) {
  176. if (!buf[i].inode) {
  177. /* register it in buffer first */
  178. buf[i].inode = ino;
  179. buf[i].th_block = bn;
  180. /* and write it back to device (sync) */
  181. sync_bimbuf(fd);
  182. return i + bimbuf.bn_start;
  183. }
  184. }
  185. bimbuf.this_block++;
  186. bimbuf.dirty = 0;
  187. bimbuf.bn_start += BLOCK_SIZE / sizeof(struct bim_entry);
  188. read(fd, bimbuf.buffer, BLOCK_SIZE);
  189. goto repeat;
  190. return 0;
  191. }
  192. /* list the files in root directory, until it's empty */
  193. void lsdir(int fd, struct inode *dir) {
  194. int i;
  195. struct dentry *entries = (struct dentry *) malloc(BLOCK_SIZE);
  196. for (i = 0; i < dir->blocks_nr; i++) {
  197. lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
  198. read(fd, entries, BLOCK_SIZE);
  199. int j;
  200. for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry) && entries[j].ino; j++) {
  201. if (!entries[j].ino) continue;
  202. printf("%d: %s\n", j, entries[j].name);
  203. }
  204. }
  205. return;
  206. }
  207. /*
  208. * search file 'name' in directory 'dir'.
  209. *
  210. * 'name' is only a file name, unlike '/foo.bar' or '/foo/bar.foo'
  211. * */
  212. struct inode *search_file(int fd, struct inode *dir, char *name) {
  213. /* read root directory until find file 'name' */
  214. int i;
  215. struct dentry *entries = (struct dentry *) malloc(BLOCK_SIZE);
  216. struct inode *ino = malloc(sizeof(struct inode));
  217. for (i = 0; i < dir->blocks_nr; i++) {
  218. lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
  219. read(fd, entries, BLOCK_SIZE);
  220. int j;
  221. for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
  222. if (strncmp(entries[j].name, name, FNAME_LEN) == 0) {
  223. read_inode(fd, entries[j].ino, ino);
  224. return ino;
  225. }
  226. }
  227. }
  228. /* root directory is dirty so it runs here. */
  229. return NULL;
  230. }
  231. /*
  232. * sync the directory 'inode' (write all data blocks back)
  233. * (calls from create file or other functions that changed inode of the
  234. * child of this directory)
  235. * */
  236. void dsync(int fd, struct inode *inode, char *entries) {
  237. int i;
  238. for (i = 0; i < inode->blocks_nr; i++) {
  239. lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
  240. write(fd, entries + (i << BLOCK_SHIFT), BLOCK_SIZE);
  241. }
  242. return;
  243. }
  244. /*
  245. * create a file 'name' in directory 'dir' with access mode 'acc'.
  246. * */
  247. struct inode *create_file(int fd, struct inode *dir, char *name, __uint8_t acc) {
  248. /* find a free entry in root directory */
  249. int i;
  250. struct dentry entries[BLOCK_SIZE / sizeof(struct dentry)];
  251. struct inode *ino = malloc(sizeof(struct inode));
  252. memset(ino, 0x00, sizeof(struct inode));
  253. for (i = 0; i < sb.root_ino.blocks_nr; i++) {
  254. lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
  255. read(fd, entries, BLOCK_SIZE);
  256. int j;
  257. /* first: check this file is non exists */
  258. for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
  259. if (strncmp(entries[j].name, name, FNAME_LEN) == 0) {
  260. printf("create_file: failed: file '%s' exists (inode %d)\n",
  261. name, entries[j].ino);
  262. return 0;
  263. }
  264. }
  265. for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry); j++) {
  266. if (!entries[j].ino) {
  267. ino->inode = entries[j].ino = sb.next_inode++;
  268. ino->pinode = ROOT_INO;
  269. ino->access = acc;
  270. ino->blocks_nr = ino->upsize = 0;
  271. ino->ctime = ino->wtime = gettime();
  272. ino->nlinks = 1;
  273. ino->uid = ino->gid = 0;
  274. ino->eaccess = 0;
  275. /* copy file name */
  276. strncpy(entries[j].name, name, FNAME_LEN);
  277. sync_sb();
  278. dsync(fd, dir, (char *) entries);
  279. return ino;
  280. }
  281. }
  282. }
  283. printf("fatal: create_file: no enough space in this directory\n");
  284. return 0;
  285. }
  286. __uint32_t write_file(int fd, struct inode *inode, char *__buf, __uint32_t nbytes) {
  287. /* bad bad, we shouldn't do this */
  288. char *buffer = malloc(ALIGNUP_BLOCK(nbytes));
  289. int i;
  290. __uint32_t nblocks = ALIGNUP_BLOCK(nbytes) >> BLOCK_SHIFT;
  291. memcpy(buffer, __buf, nbytes);
  292. for (i = 0; i < nblocks; i++) {
  293. lseek(fd, create_block(fd, inode->inode, inode->blocks_nr + i) << BLOCK_SHIFT, SEEK_SET);
  294. write(fd, buffer + (i << BLOCK_SHIFT), BLOCK_SIZE);
  295. }
  296. inode->blocks_nr = (nbytes & BLOCK_MASK) >> BLOCK_SHIFT;
  297. inode->upsize = nbytes - (inode->blocks_nr << BLOCK_SHIFT);
  298. inode->wtime = gettime();
  299. free(buffer);
  300. /* sync, then we can return */
  301. write_inode(fd, inode->inode, inode);
  302. return nbytes;
  303. }
  304. __uint32_t read_file(int fd, struct inode *inode, char *__buf, __uint32_t nbytes) {
  305. /* first read blocks that aligned */
  306. __uint32_t nblocks = (nbytes & BLOCK_MASK) >> BLOCK_SHIFT;
  307. int i;
  308. for (i = 0; i < nblocks; i++) {
  309. lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
  310. read(fd, __buf + (i << BLOCK_SHIFT), BLOCK_SIZE);
  311. }
  312. if (nblocks << BLOCK_SHIFT != nbytes) {
  313. char buf[BLOCK_SIZE];
  314. lseek(fd, bmap(fd, inode->inode, i) << BLOCK_SHIFT, SEEK_SET);
  315. read(fd, buf, BLOCK_SIZE);
  316. memcpy(__buf + (i << BLOCK_SHIFT), buf, nbytes - (nbytes & BLOCK_MASK));
  317. }
  318. return nbytes;
  319. }
  320. __uint32_t gettime(void) {
  321. struct timeval t = {0};
  322. gettimeofday(&t, NULL);
  323. return t.tv_sec;
  324. }
  325. struct inode *mkdir(int fd, struct inode *dir, char *name, __uint8_t acc) {
  326. struct inode *new_dir = malloc(sizeof(struct inode));
  327. /* first create a file in directory 'dir' */
  328. if (!(new_dir = create_file(fd, dir, name, acc & A_DIR & A_EXEC))) {
  329. return NULL;
  330. }
  331. new_dir->blocks_nr = 1;
  332. create_block(fd, new_dir->inode, 0);
  333. /* then create '.' and '..' in new directory */
  334. create_file(fd, new_dir, ".", A_ROOT_RW | A_USER_RW | A_DIR | A_LINK);
  335. create_file(fd, new_dir, "..", A_ROOT_RW | A_USER_RW | A_DIR | A_LINK);
  336. write_inode(fd, new_dir->inode, new_dir);
  337. return new_dir;
  338. }