cfs.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #ifndef __CFS_H
  20. #define __CFS_H
  21. #include <sys/types.h>
  22. typedef __uint32_t __inode_t;
  23. #define BLOCK_SIZE 4096
  24. #define BLOCK_SHIFT 12ul
  25. #define BLOCK_MASK ~(BLOCK_SIZE - 1)
  26. #define ALIGNUP_BLOCK(n) (((n) + BLOCK_SIZE - 1) & BLOCK_MASK)
  27. /* a map that describes block - inodes */
  28. /* should I call it ibm or bim? :-) */
  29. struct bim_entry {
  30. __inode_t inode;
  31. __uint32_t th_block;
  32. /* we don't need this, because this is already 4-bytes aligned */
  33. } __attribute__ ((packed));
  34. struct inode {
  35. #define ROOT_INO 2
  36. __inode_t inode;
  37. __inode_t pinode;
  38. #define A_ROOTRD (0x01)
  39. #define A_ROOTWR (0x02 | A_ROOTRD)
  40. #define A_ROOT_RW (A_ROOTWR | A_ROOTRD)
  41. #define A_USERRD (0x04 | A_ROOTRD)
  42. #define A_USERWR (0x08 | A_ROOT_RW)
  43. #define A_USER_RW (A_USERWR | A_ROOT_RW)
  44. #define A_REGFILE 0x10
  45. #define A_DIR 0x20
  46. /* A_DIR can exists with A_LINK */
  47. #define A_LINK 0x40
  48. /* directory must be executable */
  49. #define A_EXEC 0x80
  50. /* we support blkdev & chrdev etc in vfs, not here */
  51. __uint8_t access;
  52. /* these also in link files */
  53. /* aligned blocks nr */
  54. __uint32_t blocks_nr;
  55. /* blocks_nr * BLOCK_SIZE + upsize = fsize (maybe a __uint64_T) */
  56. __uint32_t upsize;
  57. /* create time */
  58. __uint32_t ctime;
  59. /* last write time */
  60. __uint32_t wtime;
  61. /* no access-time here */
  62. /* zero in link files */
  63. __uint8_t nlinks;
  64. /* creator's uid and gid */
  65. __uint16_t uid;
  66. __uint16_t gid;
  67. /* extend access (unuse now) */
  68. __uint16_t eaccess;
  69. } __attribute__ ((packed));
  70. #define FNAME_LEN 124
  71. struct dentry {
  72. __inode_t ino;
  73. char name[FNAME_LEN];
  74. } __attribute__ ((packed));
  75. typedef __uint32_t __bn_t;
  76. typedef __uint32_t __nr_t;
  77. struct superblock {
  78. #define MAGIC 0x01328e3b
  79. __uint32_t magic;
  80. __nr_t ninodes;
  81. __nr_t nblocks;
  82. /* blocks count of block-bitmap */
  83. __nr_t nb_bbm;
  84. __nr_t nb_ibm;
  85. /* block nr of bbm */
  86. __bn_t bbm_bn;
  87. __bn_t ibm_bn;
  88. __nr_t nb_bim;
  89. __nr_t nb_inodes;
  90. __bn_t bim_bn;
  91. __bn_t inodes_bn;
  92. /* 32 bytes */
  93. struct inode root_ino;
  94. __bn_t first_data_block;
  95. __inode_t next_inode;
  96. /* 12 bytes */
  97. __uint32_t zero[3];
  98. #ifdef __CFS
  99. } sb;
  100. #else
  101. };
  102. extern struct superblock sb;
  103. #endif
  104. struct bim_buffer {
  105. /* the block in device of this block (point to bim) */
  106. __bn_t this_block;
  107. __uint8_t dirty;
  108. /* real block the buffer[0] points to (a data block) */
  109. __nr_t bn_start;
  110. /* 4096 bytes, 512 entries */
  111. struct bim_entry *buffer;
  112. #ifdef __CFS
  113. } bimbuf;
  114. #else
  115. };
  116. extern struct bim_buffer bimbuf;
  117. #endif
  118. #ifndef __CFS
  119. /* for applications */
  120. int fs_fd;
  121. #else
  122. extern int fs_fd;
  123. #endif
  124. int cfs_init(int);
  125. void fill_super(int);
  126. void init_bim_buffer(int);
  127. __bn_t bmap(int, __inode_t, __bn_t);
  128. __bn_t create_block(int, __inode_t, __bn_t);
  129. void lsdir(int, struct inode *);
  130. struct inode *search_file(int, struct inode *, char *);
  131. struct inode *create_file(int, struct inode *, char *, __uint8_t);
  132. struct inode *mkdir(int, struct inode *, char *, __uint8_t);
  133. __uint32_t write_file(int, struct inode *, char *, __uint32_t);
  134. __uint32_t read_file(int, struct inode *, char *, __uint32_t);
  135. #ifndef __CFS
  136. #define sync_ino(buf) write_inode(fs_fd, (buf)->inode, buf);
  137. /* get real block of inode's bn-th block, returns real-block nr */
  138. #define cfs_bmap(ino, bn) bmap(fs_fd, inode, bn)
  139. /* list the directory 'dir' */
  140. #define cfs_lsdir(dir) lsdir(fs_fd, dir)
  141. /* create a new block for inode 'inode' (of the bn-th block),
  142. *and returns the block number
  143. */
  144. #define cfs_create_block(inode, bn) create_block(fs_fd, inode, bn)
  145. /* create a new directory (with a '.' and '..') with name 'name', attribute 'attr',
  146. * returns the new one's inode
  147. */
  148. #define cfs_mkdir(dir, name, attr) lsdir(fs_fd, dir, name, attr)
  149. /* search the file 'name' in directory 'dir' */
  150. #define cfs_search(dir, name) search_file(fs_fd, dir, name)
  151. /* create a new file in directory 'dir', with name 'name', attribute 'attr' */
  152. #define cfs_create(dir, name, attr) create_file(fs_fd, name, attr)
  153. /* write/read a file 'inode' for 'nbytes' bytes, returns bytes write/read */
  154. #define cfs_write(inode, buf, nbytes) write_file(fs_fd, inode, buf, nbytes)
  155. #define cfs_read(inode, buf, nbytes) read_file(fs_fd, inode, buf, nbytes)
  156. #endif
  157. #define fsize(inode) ((inode->blocks_nr << BLOCK_SHIFT) + inode->upsize)
  158. #define rootdir (&sb.root_ino)
  159. #endif