dumpsfs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <stdio.h>
  33. #include <assert.h>
  34. #include <limits.h>
  35. #include <err.h>
  36. #include "support.h"
  37. #include "kern/sfs.h"
  38. #ifdef HOST
  39. #include <netinet/in.h> // for arpa/inet.h
  40. #include <arpa/inet.h> // for ntohl
  41. #include "hostcompat.h"
  42. #define SWAPL(x) ntohl(x)
  43. #define SWAPS(x) ntohs(x)
  44. #else
  45. #define SWAPL(x) (x)
  46. #define SWAPS(x) (x)
  47. #endif
  48. #include "disk.h"
  49. static
  50. uint32_t
  51. dumpsb(void)
  52. {
  53. struct sfs_super sp;
  54. diskread(&sp, SFS_SB_LOCATION);
  55. if (SWAPL(sp.sp_magic) != SFS_MAGIC) {
  56. errx(1, "Not an sfs filesystem");
  57. }
  58. sp.sp_volname[sizeof(sp.sp_volname)-1] = 0;
  59. printf("Volume name: %-40s %u blocks\n", sp.sp_volname,
  60. SWAPL(sp.sp_nblocks));
  61. return SWAPL(sp.sp_nblocks);
  62. }
  63. static
  64. void
  65. dodirblock(uint32_t block)
  66. {
  67. struct sfs_dir sds[SFS_BLOCKSIZE/sizeof(struct sfs_dir)];
  68. int nsds = SFS_BLOCKSIZE/sizeof(struct sfs_dir);
  69. int i;
  70. diskread(&sds, block);
  71. printf(" [block %u]\n", block);
  72. for (i=0; i<nsds; i++) {
  73. uint32_t ino = SWAPL(sds[i].sfd_ino);
  74. if (ino==SFS_NOINO) {
  75. printf(" [free entry]\n");
  76. }
  77. else {
  78. sds[i].sfd_name[SFS_NAMELEN-1] = 0; /* just in case */
  79. printf(" %u %s\n", ino, sds[i].sfd_name);
  80. }
  81. }
  82. }
  83. static
  84. void
  85. dumpdir(uint32_t ino)
  86. {
  87. struct sfs_inode sfi;
  88. uint32_t ib[SFS_DBPERIDB];
  89. int nentries, i;
  90. uint32_t block, nblocks=0;
  91. diskread(&sfi, ino);
  92. nentries = SWAPL(sfi.sfi_size) / sizeof(struct sfs_dir);
  93. if (SWAPL(sfi.sfi_size) % sizeof(struct sfs_dir) != 0) {
  94. warnx("Warning: dir size is not a multiple of dir entry size");
  95. }
  96. printf("Directory %u: %d entries\n", ino, nentries);
  97. for (i=0; i<SFS_NDIRECT; i++) {
  98. block = SWAPL(sfi.sfi_direct[i]);
  99. if (block) {
  100. dodirblock(block);
  101. nblocks++;
  102. }
  103. }
  104. if (SWAPL(sfi.sfi_indirect)) {
  105. diskread(&ib, SWAPL(sfi.sfi_indirect));
  106. for (i=0; i<SFS_DBPERIDB; i++) {
  107. block = SWAPL(ib[i]);
  108. if (block) {
  109. dodirblock(block);
  110. nblocks++;
  111. }
  112. }
  113. }
  114. printf(" %u blocks in directory\n", nblocks);
  115. }
  116. static
  117. void
  118. dumpbits(uint32_t fsblocks)
  119. {
  120. uint32_t nblocks = SFS_BITBLOCKS(fsblocks);
  121. uint32_t i, j;
  122. char data[SFS_BLOCKSIZE];
  123. printf("Freemap: %u blocks (%u %u %u)\n", nblocks, SFS_BITMAPSIZE(fsblocks), fsblocks, SFS_BLOCKBITS);
  124. for (i=0; i<nblocks; i++) {
  125. diskread(data, SFS_MAP_LOCATION+i);
  126. for (j=0; j<SFS_BLOCKSIZE; j++) {
  127. printf("%02x", (unsigned char)data[j]);
  128. if (j%32==31) {
  129. printf("\n");
  130. }
  131. }
  132. }
  133. printf("\n");
  134. }
  135. int
  136. main(int argc, char **argv)
  137. {
  138. uint32_t nblocks;
  139. #ifdef HOST
  140. hostcompat_init(argc, argv);
  141. #endif
  142. if (argc!=2) {
  143. errx(1, "Usage: dumpsfs device/diskfile");
  144. }
  145. opendisk(argv[1]);
  146. nblocks = dumpsb();
  147. dumpbits(nblocks);
  148. dumpdir(SFS_ROOT_LOCATION);
  149. closedisk();
  150. return 0;
  151. }