mkfs.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 <unistd.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <fcntl.h>
  24. #include <errno.h>
  25. #include <sys/types.h>
  26. #include <sys/time.h>
  27. #define __CFS
  28. #include "cfs.h"
  29. #include "bitmap.h"
  30. void fill_super(int);
  31. void setup_bbm(int);
  32. void setup_bim(int);
  33. void setup_ibm(int);
  34. void setup_root(int);
  35. void setup_rfile(int, char *, struct dentry *, __uint8_t);
  36. __uint32_t gettime(void);
  37. int main(int argc, const char **argv) {
  38. char *fname = (char *) argv[1];
  39. int fd;
  40. if (!fname) {
  41. printf("fatal: excepted one argument, but %d given. \n", argc - 1);
  42. printf("usage: %s device\n", argv[0]);
  43. return 1;
  44. }
  45. fd = open(fname, O_RDWR | O_CREAT);
  46. if (fd < 0) {
  47. printf("fatal: failed when try to open %s: %s\n", fname, strerror(errno));
  48. return 1;
  49. }
  50. printf("fill super block...");
  51. fill_super(fd);
  52. printf("OK\n");
  53. printf("setup block bitmap...");
  54. setup_bbm(fd);
  55. printf("OK\n");
  56. printf("setup bim");
  57. setup_bim(fd);
  58. printf("OK\n");
  59. printf("setup inodes bitmap");
  60. setup_ibm(fd);
  61. printf("OK\n");
  62. printf("setup root...");
  63. setup_root(fd);
  64. printf("OK\n");
  65. close(fd);
  66. printf("all done\n");
  67. return 0;
  68. }
  69. void fill_super(int fd) {
  70. /* fill sb, and write it back to the fd */
  71. memset(&sb, 0x00, sizeof(struct superblock));
  72. sb.magic = MAGIC;
  73. sb.nblocks = lseek(fd, 0, SEEK_END) >> BLOCK_SHIFT;
  74. /* inodes size : nblocks = 1 : 16 */
  75. sb.nb_inodes = sb.nblocks >> 4;
  76. sb.nb_ibm = ALIGNUP_BLOCK(sb.nb_inodes * sizeof(struct inode) >> 3) >> BLOCK_SHIFT;
  77. sb.ninodes = (sb.nb_inodes << BLOCK_SHIFT) / sizeof(struct inode);
  78. lseek(fd, 0, SEEK_SET);
  79. /* (sb.nblocks >> 3) + 1 is the size (bytes) of bbm, and align it to blocks */
  80. sb.nb_bbm = ALIGNUP_BLOCK((sb.nblocks >> 3) + 1) >> BLOCK_SHIFT;
  81. /* the second block */
  82. sb.bbm_bn = 1;
  83. /* << 3: * 8 (size of a bim_entry) */
  84. sb.nb_bim = ALIGNUP_BLOCK(sb.nblocks << 3) >> BLOCK_SHIFT;
  85. sb.bim_bn = 1 + sb.nb_bbm;
  86. sb.ibm_bn = sb.bim_bn + sb.nb_bim;
  87. sb.inodes_bn = sb.ibm_bn + sb.nb_ibm;
  88. /* root_ino will set in setup_root */
  89. /* and write back */
  90. #define update_sb() \
  91. lseek(fd, 512, SEEK_SET); \
  92. write(fd, (char *) &sb, sizeof(struct superblock)); \
  93. lseek(fd, 0, SEEK_SET);
  94. /* ignore that MBR (512 bytes) */
  95. update_sb();
  96. return;
  97. }
  98. /* clean bitmap, and then write (only first block) */
  99. void setup_bbm(int fd) {
  100. /* clear all bits in bitmap */
  101. int bn;
  102. char block[BLOCK_SIZE] = {0};
  103. lseek(fd, sb.bbm_bn << BLOCK_SHIFT, SEEK_SET);
  104. for (bn = sb.bbm_bn; bn < sb.bbm_bn + sb.nb_bbm; bn++) {
  105. /* write one block to fd */
  106. write(fd, block, BLOCK_SIZE);
  107. /* point to next point */
  108. lseek(fd, BLOCK_SIZE, SEEK_CUR);
  109. }
  110. /* and calc how many blocks are using */
  111. __nr_t blocks_used = sb.bbm_bn + sb.nb_bbm + sb.nb_bim + sb.nb_ibm + sb.nb_inodes;
  112. sb.first_data_block = blocks_used;
  113. update_sb();
  114. lseek(fd, sb.bbm_bn << BLOCK_SHIFT, SEEK_SET);
  115. /* set them */
  116. unsigned char *bm = malloc(ALIGNUP_BLOCK(blocks_used >> 3));
  117. if (!bm) {
  118. printf("fatal: failed to allocate space for bitmap. %d bytes needs. \n",
  119. blocks_used >> 3);
  120. exit(1);
  121. }
  122. memset(bm, 0x00, ALIGNUP_BLOCK(blocks_used >> 3));
  123. for (bn = 0; bn < blocks_used; bn++)
  124. bitmap_set(bm, bn, 1);
  125. /* write it back */
  126. for (bn = 0; bn < ALIGNUP_BLOCK(blocks_used >> 3) >> BLOCK_SHIFT; bn++)
  127. write(fd, bm + (bn << BLOCK_SHIFT), BLOCK_SIZE);
  128. free(bm);
  129. return;
  130. }
  131. void setup_ibm(int fd) {
  132. int i;
  133. char block[BLOCK_SIZE] = {0};
  134. /* put 16 '.'s on the screen */
  135. int points = sb.nb_ibm >> 4;
  136. lseek(fd, sb.ibm_bn << BLOCK_SHIFT, SEEK_SET);
  137. for (i = 0; i < sb.nb_ibm; i++) {
  138. /* write one block to fd */
  139. write(fd, block, BLOCK_SIZE);
  140. if (points && i % points == 0)
  141. putc('.', stdout);
  142. }
  143. if (!points)
  144. printf("................");
  145. return;
  146. }
  147. void setup_bim(int fd) {
  148. int i;
  149. char block[BLOCK_SIZE] = {0};
  150. /* put 16 '.'s on the screen */
  151. int points = sb.nb_bim >> 4;
  152. lseek(fd, sb.bim_bn << BLOCK_SHIFT, SEEK_SET);
  153. for (i = 0; i < sb.nb_bim; i++) {
  154. /* write one block to fd */
  155. write(fd, block, BLOCK_SIZE);
  156. if (i % points == 0)
  157. putc('.', stdout);
  158. }
  159. return;
  160. }
  161. /*
  162. * setup that root directory '/'.
  163. *
  164. * STEPS:
  165. * 1. setup root_ino in sb.
  166. * 2. setup bits in bim (initialize size 4k)
  167. * 3. setup dentry
  168. * */
  169. void setup_root(int fd) {
  170. sb.root_ino.inode = ROOT_INO;
  171. sb.root_ino.pinode = ROOT_INO;
  172. sb.root_ino.access = A_DIR | A_USERRD | A_ROOT_RW | A_EXEC;
  173. /* 4096 bytes = 1 block */
  174. sb.root_ino.blocks_nr = 1;
  175. sb.root_ino.upsize = 0;
  176. sb.root_ino.ctime = sb.root_ino.wtime = gettime();
  177. /* '.' + '..' */
  178. sb.root_ino.nlinks = 2;
  179. /* unused now in user space */
  180. sb.root_ino.uid = sb.root_ino.gid = 0;
  181. sb.root_ino.eaccess = 0;
  182. update_sb();
  183. /* okay, now setup bim */
  184. struct bim_entry entry;
  185. memset(&entry, 0x00, sizeof(struct bim_entry));
  186. entry.inode = ROOT_INO;
  187. entry.th_block = 0;
  188. /* write back */
  189. lseek(fd, sb.bim_bn << BLOCK_SHIFT, SEEK_SET);
  190. write(fd, &entry, sizeof(struct bim_entry));
  191. /* now root's 1st block is the first_data_block */
  192. /* setup '.' and '..' in it */
  193. struct dentry entries[BLOCK_SIZE / sizeof(struct dentry)];
  194. memset(entries, 0x00, BLOCK_SIZE);
  195. /* for clean these */
  196. lseek(fd, sb.first_data_block << BLOCK_SHIFT, SEEK_SET);
  197. write(fd, (void *) entries, BLOCK_SIZE);
  198. sb.next_inode = ROOT_INO + 1;
  199. setup_rfile(fd, ".", entries, A_DIR | A_LINK | A_USERRD | A_ROOT_RW);
  200. setup_rfile(fd, "..", entries, A_DIR | A_LINK | A_USERRD | A_ROOT_RW);
  201. update_sb();
  202. /* write back to device */
  203. lseek(fd, sb.first_data_block << BLOCK_SHIFT, SEEK_SET);
  204. write(fd, entries, BLOCK_SIZE);
  205. return;
  206. }
  207. static __inode_t find_free_inode(int fd) {
  208. int i = 0;
  209. __uint8_t block[BLOCK_SIZE];
  210. repeat:
  211. lseek(fd, (sb.ibm_bn + i) << BLOCK_SHIFT, SEEK_SET);
  212. read(fd, block, BLOCK_SIZE);
  213. int j;
  214. for (j = 0; bitmap_read(block, j) && j < 4096; j++);
  215. if (j == 4096) {
  216. i++;
  217. goto repeat;
  218. }
  219. bitmap_set(block, j, 1);
  220. lseek(fd, (sb.ibm_bn + i) << BLOCK_SHIFT, SEEK_SET);
  221. write(fd, block, BLOCK_SIZE);
  222. return (i << BLOCK_SHIFT << 3) + j;
  223. }
  224. void setup_rfile(int fd, char *fname, struct dentry *dentry, __uint8_t acc) {
  225. __inode_t ino = find_free_inode(fd);
  226. struct inode inode;
  227. inode.inode = sb.next_inode++;
  228. inode.pinode = ROOT_INO;
  229. inode.access = acc;
  230. inode.blocks_nr = 0;
  231. inode.upsize = 0;
  232. inode.ctime = inode.wtime = gettime();
  233. inode.nlinks = 1;
  234. inode.uid = inode.gid = 0;
  235. inode.eaccess = 0;
  236. lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + inode.inode * sizeof(struct inode), SEEK_SET);
  237. write(fd, &inode, sizeof(struct inode));
  238. dentry[ino].ino = inode.inode;
  239. strncpy(dentry[ino].name, fname, FNAME_LEN - 1);
  240. /* and sync dentry */
  241. /* we only sync the first block, because this is mkfs. :-) */
  242. lseek(fd, sb.first_data_block << BLOCK_SHIFT, SEEK_SET);
  243. write(fd, dentry, BLOCK_SIZE);
  244. return;
  245. #undef inode
  246. }
  247. __uint32_t gettime(void) {
  248. struct timeval t = {0};
  249. gettimeofday(&t, NULL);
  250. return t.tv_sec;
  251. }