mksfs.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/types.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <assert.h>
  33. #include <limits.h>
  34. #include <err.h>
  35. #include "support.h"
  36. #include "kern/sfs.h"
  37. #ifdef HOST
  38. #include <netinet/in.h> // for arpa/inet.h
  39. #include <arpa/inet.h> // for ntohl
  40. #include "hostcompat.h"
  41. #define SWAPL(x) ntohl(x)
  42. #define SWAPS(x) ntohs(x)
  43. #else
  44. #define SWAPL(x) (x)
  45. #define SWAPS(x) (x)
  46. #endif
  47. #include "disk.h"
  48. #define MAXBITBLOCKS 32
  49. static
  50. void
  51. check(void)
  52. {
  53. assert(sizeof(struct sfs_super)==SFS_BLOCKSIZE);
  54. assert(sizeof(struct sfs_inode)==SFS_BLOCKSIZE);
  55. assert(SFS_BLOCKSIZE % sizeof(struct sfs_dir) == 0);
  56. }
  57. static
  58. void
  59. writesuper(const char *volname, uint32_t nblocks)
  60. {
  61. struct sfs_super sp;
  62. bzero((void *)&sp, sizeof(sp));
  63. if (strlen(volname) >= SFS_VOLNAME_SIZE) {
  64. errx(1, "Volume name %s too long", volname);
  65. }
  66. sp.sp_magic = SWAPL(SFS_MAGIC);
  67. sp.sp_nblocks = SWAPL(nblocks);
  68. strcpy(sp.sp_volname, volname);
  69. diskwrite(&sp, SFS_SB_LOCATION);
  70. }
  71. static
  72. void
  73. writerootdir(void)
  74. {
  75. struct sfs_inode sfi;
  76. bzero((void *)&sfi, sizeof(sfi));
  77. sfi.sfi_size = SWAPL(0);
  78. sfi.sfi_type = SWAPS(SFS_TYPE_DIR);
  79. sfi.sfi_linkcount = SWAPS(1);
  80. diskwrite(&sfi, SFS_ROOT_LOCATION);
  81. }
  82. static char bitbuf[MAXBITBLOCKS*SFS_BLOCKSIZE];
  83. static
  84. void
  85. doallocbit(uint32_t bit)
  86. {
  87. uint32_t byte = bit/CHAR_BIT;
  88. unsigned char mask = (1<<(bit % CHAR_BIT));
  89. assert((bitbuf[byte] & mask) == 0);
  90. bitbuf[byte] |= mask;
  91. }
  92. static
  93. void
  94. writebitmap(uint32_t fsblocks)
  95. {
  96. uint32_t nbits = SFS_BITMAPSIZE(fsblocks);
  97. uint32_t nblocks = SFS_BITBLOCKS(fsblocks);
  98. char *ptr;
  99. uint32_t i;
  100. if (nblocks > MAXBITBLOCKS) {
  101. errx(1, "Filesystem too large "
  102. "- increase MAXBITBLOCKS and recompile");
  103. }
  104. doallocbit(SFS_SB_LOCATION);
  105. doallocbit(SFS_ROOT_LOCATION);
  106. for (i=0; i<nblocks; i++) {
  107. doallocbit(SFS_MAP_LOCATION+i);
  108. }
  109. for (i=fsblocks; i<nbits; i++) {
  110. doallocbit(i);
  111. }
  112. for (i=0; i<nblocks; i++) {
  113. ptr = bitbuf + i*SFS_BLOCKSIZE;
  114. diskwrite(ptr, SFS_MAP_LOCATION+i);
  115. }
  116. }
  117. int
  118. main(int argc, char **argv)
  119. {
  120. uint32_t size, blocksize;
  121. char *volname, *s;
  122. #ifdef HOST
  123. hostcompat_init(argc, argv);
  124. #endif
  125. if (argc!=3) {
  126. errx(1, "Usage: mksfs device/diskfile volume-name");
  127. }
  128. check();
  129. volname = argv[2];
  130. /* Remove one trailing colon from volname, if present */
  131. s = strchr(volname, ':');
  132. if (s != NULL) {
  133. if (strlen(s)!=1) {
  134. errx(1, "Illegal volume name %s", volname);
  135. }
  136. *s = 0;
  137. }
  138. /* Don't allow slashes */
  139. s = strchr(volname, '/');
  140. if (s != NULL) {
  141. errx(1, "Illegal volume name %s", volname);
  142. }
  143. opendisk(argv[1]);
  144. blocksize = diskblocksize();
  145. if (blocksize!=SFS_BLOCKSIZE) {
  146. errx(1, "Device has wrong blocksize %u (should be %u)\n",
  147. blocksize, SFS_BLOCKSIZE);
  148. }
  149. size = diskblocks();
  150. writesuper(volname, size);
  151. writerootdir();
  152. writebitmap(size);
  153. closedisk();
  154. return 0;
  155. }