sfsck.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 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 <assert.h>
  31. #include <limits.h>
  32. #include <stdint.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <err.h>
  37. #include "support.h"
  38. #include "kern/sfs.h"
  39. #ifdef HOST
  40. #include <netinet/in.h> // for arpa/inet.h
  41. #include <arpa/inet.h> // for ntohl
  42. #include "hostcompat.h"
  43. #define SWAPL(x) ntohl(x)
  44. #define SWAPS(x) ntohs(x)
  45. #else
  46. #define SWAPL(x) (x)
  47. #define SWAPS(x) (x)
  48. #define NO_REALLOC
  49. #define NO_QSORT
  50. #endif
  51. #include "disk.h"
  52. #define EXIT_USAGE 4
  53. #define EXIT_FATAL 3
  54. #define EXIT_UNRECOV 2
  55. #define EXIT_RECOV 1
  56. #define EXIT_CLEAN 0
  57. static int badness=0;
  58. static
  59. void
  60. setbadness(int code)
  61. {
  62. if (badness < code) {
  63. badness = code;
  64. }
  65. }
  66. ////////////////////////////////////////////////////////////
  67. static
  68. void
  69. swapsb(struct sfs_super *sp)
  70. {
  71. sp->sp_magic = SWAPL(sp->sp_magic);
  72. sp->sp_nblocks = SWAPL(sp->sp_nblocks);
  73. }
  74. static
  75. void
  76. swapinode(struct sfs_inode *sfi)
  77. {
  78. int i;
  79. sfi->sfi_size = SWAPL(sfi->sfi_size);
  80. sfi->sfi_type = SWAPS(sfi->sfi_type);
  81. sfi->sfi_linkcount = SWAPS(sfi->sfi_linkcount);
  82. for (i=0; i<SFS_NDIRECT; i++) {
  83. sfi->sfi_direct[i] = SWAPL(sfi->sfi_direct[i]);
  84. }
  85. #ifdef SFS_NIDIRECT
  86. for (i=0; i<SFS_NIDIRECT; i++) {
  87. sfi->sfi_indirect[i] = SWAPL(sfi->sfi_indirect[i]);
  88. }
  89. #else
  90. sfi->sfi_indirect = SWAPL(sfi->sfi_indirect);
  91. #endif
  92. #ifdef SFS_NDIDIRECT
  93. for (i=0; i<SFS_NDIDIRECT; i++) {
  94. sfi->sfi_dindirect[i] = SWAPL(sfi->sfi_dindirect[i]);
  95. }
  96. #else
  97. #ifdef HAS_DIDIRECT
  98. sfi->sfi_dindirect = SWAPL(sfi->sfi_dindirect);
  99. #endif
  100. #endif
  101. #ifdef SFS_NTIDIRECT
  102. for (i=0; i<SFS_NTIDIRECT; i++) {
  103. sfi->sfi_tindirect[i] = SWAPL(sfi->sfi_tindirect[i]);
  104. }
  105. #else
  106. #ifdef HAS_TIDIRECT
  107. sfi->sfi_tindirect = SWAPL(sfi->sfi_tindirect);
  108. #endif
  109. #endif
  110. }
  111. static
  112. void
  113. swapdir(struct sfs_dir *sfd)
  114. {
  115. sfd->sfd_ino = SWAPL(sfd->sfd_ino);
  116. }
  117. static
  118. void
  119. swapindir(uint32_t *entries)
  120. {
  121. int i;
  122. for (i=0; i<SFS_DBPERIDB; i++) {
  123. entries[i] = SWAPL(entries[i]);
  124. }
  125. }
  126. static
  127. void
  128. swapbits(uint8_t *bits)
  129. {
  130. /* nothing to do */
  131. (void)bits;
  132. }
  133. ////////////////////////////////////////////////////////////
  134. static
  135. void *
  136. domalloc(size_t len)
  137. {
  138. void *x;
  139. x = malloc(len);
  140. if (x==NULL) {
  141. errx(EXIT_FATAL, "Out of memory");
  142. }
  143. return x;
  144. }
  145. ////////////////////////////////////////////////////////////
  146. typedef enum {
  147. B_SUPERBLOCK, /* Block that is the superblock */
  148. B_BITBLOCK, /* Block used by free-block bitmap */
  149. B_INODE, /* Block that is an inode */
  150. B_IBLOCK, /* Indirect (or doubly-indirect etc.) block */
  151. B_DIRDATA, /* Data block of a directory */
  152. B_DATA, /* Data block */
  153. B_TOFREE, /* Block that was used but we are releasing */
  154. B_PASTEND, /* Block off the end of the fs */
  155. } blockusage_t;
  156. static uint32_t nblocks, bitblocks;
  157. static uint32_t uniquecounter = 1;
  158. static unsigned long count_blocks=0, count_dirs=0, count_files=0;
  159. ////////////////////////////////////////////////////////////
  160. static uint8_t *bitmapdata;
  161. static uint8_t *tofreedata;
  162. static
  163. void
  164. bitmap_init(uint32_t bitblocks)
  165. {
  166. size_t i, mapsize = bitblocks * SFS_BLOCKSIZE;
  167. bitmapdata = domalloc(mapsize * sizeof(uint8_t));
  168. tofreedata = domalloc(mapsize * sizeof(uint8_t));
  169. for (i=0; i<mapsize; i++) {
  170. bitmapdata[i] = tofreedata[i] = 0;
  171. }
  172. }
  173. static
  174. const char *
  175. blockusagestr(blockusage_t how, uint32_t howdesc)
  176. {
  177. static char rv[256];
  178. switch (how) {
  179. case B_SUPERBLOCK: return "superblock";
  180. case B_BITBLOCK: return "bitmap block";
  181. case B_INODE: return "inode";
  182. case B_IBLOCK:
  183. snprintf(rv, sizeof(rv), "indirect block of inode %lu",
  184. (unsigned long) howdesc);
  185. break;
  186. case B_DIRDATA:
  187. snprintf(rv, sizeof(rv), "directory data from inode %lu",
  188. (unsigned long) howdesc);
  189. break;
  190. case B_DATA:
  191. snprintf(rv, sizeof(rv), "file data from inode %lu",
  192. (unsigned long) howdesc);
  193. break;
  194. case B_TOFREE:
  195. assert(0);
  196. break;
  197. case B_PASTEND:
  198. return "past the end of the fs";
  199. }
  200. return rv;
  201. }
  202. static
  203. void
  204. bitmap_mark(uint32_t block, blockusage_t how, uint32_t howdesc)
  205. {
  206. unsigned index = block/8;
  207. uint8_t mask = ((uint8_t)1)<<(block%8);
  208. if (how == B_TOFREE) {
  209. if (tofreedata[index] & mask) {
  210. /* already marked to free once, ignore */
  211. return;
  212. }
  213. if (bitmapdata[index] & mask) {
  214. /* block is used elsewhere, ignore */
  215. return;
  216. }
  217. tofreedata[index] |= mask;
  218. return;
  219. }
  220. if (tofreedata[index] & mask) {
  221. /* really using the block, don't free it */
  222. tofreedata[index] &= ~mask;
  223. }
  224. if (bitmapdata[index] & mask) {
  225. warnx("Block %lu (used as %s) already in use! (NOT FIXED)",
  226. (unsigned long) block, blockusagestr(how, howdesc));
  227. setbadness(EXIT_UNRECOV);
  228. }
  229. bitmapdata[index] |= mask;
  230. if (how != B_PASTEND) {
  231. count_blocks++;
  232. }
  233. }
  234. static
  235. int
  236. countbits(uint8_t val)
  237. {
  238. uint8_t x;
  239. int ct=0;
  240. for (x=1; x; x<<=1) {
  241. if (val & x) ct++;
  242. }
  243. return ct;
  244. }
  245. static
  246. void
  247. reportbits(uint32_t bitblock, uint32_t byte, uint8_t val, const char *what)
  248. {
  249. uint8_t x, y;
  250. uint32_t blocknum;
  251. for (x=1, y=0; x; x<<=1, y++) {
  252. if (val & x) {
  253. blocknum = bitblock*SFS_BLOCKBITS + byte*CHAR_BIT + y;
  254. warnx("Block %lu erroneously shown %s in bitmap",
  255. (unsigned long) blocknum, what);
  256. }
  257. }
  258. }
  259. static
  260. void
  261. check_bitmap(void)
  262. {
  263. uint8_t bits[SFS_BLOCKSIZE], *found, *tofree, tmp;
  264. uint32_t alloccount=0, freecount=0, i, j;
  265. int bchanged;
  266. for (i=0; i<bitblocks; i++) {
  267. diskread(bits, SFS_MAP_LOCATION+i);
  268. swapbits(bits);
  269. found = bitmapdata + i*SFS_BLOCKSIZE;
  270. tofree = tofreedata + i*SFS_BLOCKSIZE;
  271. bchanged = 0;
  272. for (j=0; j<SFS_BLOCKSIZE; j++) {
  273. /* we shouldn't have blocks marked both ways */
  274. assert((found[j] & tofree[j])==0);
  275. if (bits[j]==found[j]) {
  276. continue;
  277. }
  278. if (bits[j]==(found[j] | tofree[j])) {
  279. bits[j] = found[j];
  280. bchanged = 1;
  281. continue;
  282. }
  283. /* free the ones we're freeing */
  284. bits[j] &= ~tofree[j];
  285. /* are we short any? */
  286. if ((bits[j] & found[j]) != found[j]) {
  287. tmp = found[j] & ~bits[j];
  288. alloccount += countbits(tmp);
  289. if (tmp != 0) {
  290. reportbits(i, j, tmp, "free");
  291. }
  292. }
  293. /* do we have any extra? */
  294. if ((bits[j] & found[j]) != bits[j]) {
  295. tmp = bits[j] & ~found[j];
  296. freecount += countbits(tmp);
  297. if (tmp != 0) {
  298. reportbits(i, j, tmp, "allocated");
  299. }
  300. }
  301. bits[j] = found[j];
  302. bchanged = 1;
  303. }
  304. if (bchanged) {
  305. swapbits(bits);
  306. diskwrite(bits, SFS_MAP_LOCATION+i);
  307. }
  308. }
  309. if (alloccount > 0) {
  310. warnx("%lu blocks erroneously shown free in bitmap (fixed)",
  311. (unsigned long) alloccount);
  312. setbadness(EXIT_RECOV);
  313. }
  314. if (freecount > 0) {
  315. warnx("%lu blocks erroneously shown used in bitmap (fixed)",
  316. (unsigned long) freecount);
  317. setbadness(EXIT_RECOV);
  318. }
  319. }
  320. ////////////////////////////////////////////////////////////
  321. struct inodememory {
  322. uint32_t ino;
  323. uint32_t linkcount; /* files only; 0 for dirs */
  324. };
  325. static struct inodememory *inodes = NULL;
  326. static int ninodes=0, maxinodes=0;
  327. static
  328. void
  329. addmemory(uint32_t ino, uint32_t linkcount)
  330. {
  331. assert(ninodes <= maxinodes);
  332. if (ninodes == maxinodes) {
  333. #ifdef NO_REALLOC
  334. int newmax = (maxinodes+1)*2;
  335. void *p = domalloc(newmax * sizeof(struct inodememory));
  336. if (inodes) {
  337. memcpy(p, inodes, ninodes);
  338. free(inodes);
  339. }
  340. inodes = p;
  341. #else
  342. maxinodes = (maxinodes+1)*2;
  343. inodes = realloc(inodes, maxinodes * sizeof(uint32_t));
  344. if (inodes==NULL) {
  345. errx(EXIT_FATAL, "Out of memory");
  346. }
  347. #endif
  348. }
  349. inodes[ninodes].ino = ino;
  350. inodes[ninodes].linkcount = linkcount;
  351. }
  352. /* returns nonzero if directory already remembered */
  353. static
  354. int
  355. remember_dir(uint32_t ino, const char *pathsofar)
  356. {
  357. int i;
  358. /* don't use this for now */
  359. (void)pathsofar;
  360. for (i=0; i<ninodes; i++) {
  361. if (inodes[i].ino==ino) {
  362. assert(inodes[i].linkcount==0);
  363. return 1;
  364. }
  365. }
  366. addmemory(ino, 0);
  367. return 0;
  368. }
  369. static
  370. void
  371. observe_filelink(uint32_t ino)
  372. {
  373. int i;
  374. for (i=0; i<ninodes; i++) {
  375. if (inodes[i].ino==ino) {
  376. assert(inodes[i].linkcount>0);
  377. inodes[i].linkcount++;
  378. return;
  379. }
  380. }
  381. bitmap_mark(ino, B_INODE, ino);
  382. addmemory(ino, 1);
  383. }
  384. static
  385. void
  386. adjust_filelinks(void)
  387. {
  388. struct sfs_inode sfi;
  389. int i;
  390. for (i=0; i<ninodes; i++) {
  391. if (inodes[i].linkcount==0) {
  392. /* directory */
  393. continue;
  394. }
  395. diskread(&sfi, inodes[i].ino);
  396. swapinode(&sfi);
  397. assert(sfi.sfi_type == SFS_TYPE_FILE);
  398. if (sfi.sfi_linkcount != inodes[i].linkcount) {
  399. warnx("File %lu link count %lu should be %lu (fixed)",
  400. (unsigned long) inodes[i].ino,
  401. (unsigned long) sfi.sfi_linkcount,
  402. (unsigned long) inodes[i].linkcount);
  403. sfi.sfi_linkcount = inodes[i].linkcount;
  404. setbadness(EXIT_RECOV);
  405. swapinode(&sfi);
  406. diskwrite(&sfi, inodes[i].ino);
  407. }
  408. count_files++;
  409. }
  410. }
  411. ////////////////////////////////////////////////////////////
  412. static
  413. int
  414. checknullstring(char *buf, size_t maxlen)
  415. {
  416. size_t i;
  417. for (i=0; i<maxlen; i++) {
  418. if (buf[i]==0) {
  419. return 0;
  420. }
  421. }
  422. buf[maxlen-1] = 0;
  423. return 1;
  424. }
  425. static
  426. int
  427. checkbadstring(char *buf)
  428. {
  429. size_t i;
  430. int rv=0;
  431. for (i=0; buf[i]; i++) {
  432. if (buf[i]==':' || buf[i]=='/') {
  433. buf[i] = '_';
  434. rv = 1;
  435. }
  436. }
  437. return rv;
  438. }
  439. ////////////////////////////////////////////////////////////
  440. static
  441. void
  442. check_sb(void)
  443. {
  444. struct sfs_super sp;
  445. uint32_t i;
  446. int schanged=0;
  447. diskread(&sp, SFS_SB_LOCATION);
  448. swapsb(&sp);
  449. if (sp.sp_magic != SFS_MAGIC) {
  450. errx(EXIT_UNRECOV, "Not an sfs filesystem");
  451. }
  452. assert(nblocks==0);
  453. assert(bitblocks==0);
  454. nblocks = sp.sp_nblocks;
  455. bitblocks = SFS_BITBLOCKS(nblocks);
  456. assert(nblocks>0);
  457. assert(bitblocks>0);
  458. bitmap_init(bitblocks);
  459. for (i=nblocks; i<bitblocks*SFS_BLOCKBITS; i++) {
  460. bitmap_mark(i, B_PASTEND, 0);
  461. }
  462. if (checknullstring(sp.sp_volname, sizeof(sp.sp_volname))) {
  463. warnx("Volume name not null-terminated (fixed)");
  464. setbadness(EXIT_RECOV);
  465. schanged = 1;
  466. }
  467. if (checkbadstring(sp.sp_volname)) {
  468. warnx("Volume name contains illegal characters (fixed)");
  469. setbadness(EXIT_RECOV);
  470. schanged = 1;
  471. }
  472. if (schanged) {
  473. swapsb(&sp);
  474. diskwrite(&sp, SFS_SB_LOCATION);
  475. }
  476. bitmap_mark(SFS_SB_LOCATION, B_SUPERBLOCK, 0);
  477. for (i=0; i<bitblocks; i++) {
  478. bitmap_mark(SFS_MAP_LOCATION+i, B_BITBLOCK, i);
  479. }
  480. }
  481. ////////////////////////////////////////////////////////////
  482. static
  483. void
  484. check_indirect_block(uint32_t ino, uint32_t *ientry, uint32_t *blockp,
  485. uint32_t nblocks, uint32_t *badcountp,
  486. int isdir, int indirection)
  487. {
  488. uint32_t entries[SFS_DBPERIDB];
  489. uint32_t i, ct;
  490. if (*ientry !=0) {
  491. diskread(entries, *ientry);
  492. swapindir(entries);
  493. bitmap_mark(*ientry, B_IBLOCK, ino);
  494. }
  495. else {
  496. for (i=0; i<SFS_DBPERIDB; i++) {
  497. entries[i] = 0;
  498. }
  499. }
  500. if (indirection > 1) {
  501. for (i=0; i<SFS_DBPERIDB; i++) {
  502. check_indirect_block(ino, &entries[i],
  503. blockp, nblocks,
  504. badcountp,
  505. isdir,
  506. indirection-1);
  507. }
  508. }
  509. else {
  510. assert(indirection==1);
  511. for (i=0; i<SFS_DBPERIDB; i++) {
  512. if (*blockp < nblocks) {
  513. if (entries[i] != 0) {
  514. bitmap_mark(entries[i],
  515. isdir ? B_DIRDATA : B_DATA,
  516. ino);
  517. }
  518. }
  519. else {
  520. if (entries[i] != 0) {
  521. (*badcountp)++;
  522. bitmap_mark(entries[i],
  523. isdir ? B_DIRDATA : B_DATA,
  524. ino);
  525. entries[i] = 0;
  526. }
  527. }
  528. (*blockp)++;
  529. }
  530. }
  531. ct=0;
  532. for (i=ct=0; i<SFS_DBPERIDB; i++) {
  533. if (entries[i]!=0) ct++;
  534. }
  535. if (ct==0) {
  536. if (*ientry != 0) {
  537. (*badcountp)++;
  538. bitmap_mark(*ientry, B_TOFREE, 0);
  539. *ientry = 0;
  540. }
  541. }
  542. else {
  543. assert(*ientry != 0);
  544. if (*badcountp > 0) {
  545. swapindir(entries);
  546. diskwrite(entries, *ientry);
  547. }
  548. }
  549. }
  550. /* returns nonzero if inode modified */
  551. static
  552. int
  553. check_inode_blocks(uint32_t ino, struct sfs_inode *sfi, int isdir)
  554. {
  555. uint32_t size, block, nblocks, badcount;
  556. badcount = 0;
  557. size = SFS_ROUNDUP(sfi->sfi_size, SFS_BLOCKSIZE);
  558. nblocks = size/SFS_BLOCKSIZE;
  559. for (block=0; block<SFS_NDIRECT; block++) {
  560. if (block < nblocks) {
  561. if (sfi->sfi_direct[block] != 0) {
  562. bitmap_mark(sfi->sfi_direct[block],
  563. isdir ? B_DIRDATA : B_DATA, ino);
  564. }
  565. }
  566. else {
  567. if (sfi->sfi_direct[block] != 0) {
  568. badcount++;
  569. bitmap_mark(sfi->sfi_direct[block],
  570. B_TOFREE, 0);
  571. }
  572. }
  573. }
  574. #ifdef SFS_NIDIRECT
  575. for (i=0; i<SFS_NIDIRECT; i++) {
  576. check_indirect_block(ino, &sfi->sfi_indirect[i],
  577. &block, nblocks, &badcount, isdir, 1);
  578. }
  579. #else
  580. check_indirect_block(ino, &sfi->sfi_indirect,
  581. &block, nblocks, &badcount, isdir, 1);
  582. #endif
  583. #ifdef SFS_NDIDIRECT
  584. for (i=0; i<SFS_NDIDIRECT; i++) {
  585. check_indirect_block(ino, &sfi->sfi_dindirect[i],
  586. &block, nblocks, &badcount, isdir, 2);
  587. }
  588. #else
  589. #ifdef HAS_DIDIRECT
  590. check_indirect_block(ino, &sfi->sfi_dindirect,
  591. &block, nblocks, &badcount, isdir, 2);
  592. #endif
  593. #endif
  594. #ifdef SFS_NTIDIRECT
  595. for (i=0; i<SFS_NTIDIRECT; i++) {
  596. check_indirect_block(ino, &sfi->sfi_tindirect[i],
  597. &block, nblocks, &badcount, isdir, 3);
  598. }
  599. #else
  600. #ifdef HAS_TIDIRECT
  601. check_indirect_block(ino, &sfi->sfi_tindirect,
  602. &block, nblocks, &badcount, isdir, 3);
  603. #endif
  604. #endif
  605. if (badcount > 0) {
  606. warnx("Inode %lu: %lu blocks after EOF (freed)",
  607. (unsigned long) ino, (unsigned long) badcount);
  608. setbadness(EXIT_RECOV);
  609. return 1;
  610. }
  611. return 0;
  612. }
  613. ////////////////////////////////////////////////////////////
  614. static
  615. uint32_t
  616. ibmap(uint32_t iblock, uint32_t offset, uint32_t entrysize)
  617. {
  618. uint32_t entries[SFS_DBPERIDB];
  619. if (iblock == 0) {
  620. return 0;
  621. }
  622. diskread(entries, iblock);
  623. swapindir(entries);
  624. if (entrysize > 1) {
  625. uint32_t index = offset / entrysize;
  626. offset %= entrysize;
  627. return ibmap(entries[index], offset, entrysize/SFS_DBPERIDB);
  628. }
  629. else {
  630. assert(offset < SFS_DBPERIDB);
  631. return entries[offset];
  632. }
  633. }
  634. #define BMAP_ND SFS_NDIRECT
  635. #define BMAP_D(sfi, x) ((sfi)->sfi_direct[(x)])
  636. #ifdef SFS_NIDIRECT
  637. #define BMAP_NI SFS_NIDIRECT
  638. #define BMAP_I(sfi, x) ((sfi)->sfi_indirect[(x)])
  639. #else
  640. #define BMAP_NI 1
  641. #define BMAP_I(sfi, x) ((void)(x), (sfi)->sfi_indirect)
  642. #endif
  643. #ifdef SFS_NDIDIRECT
  644. #define BMAP_NII SFS_NDIDIRECT
  645. #define BMAP_II(sfi, x) ((sfi)->sfi_dindirect[(x)])
  646. #else
  647. #ifdef HAS_DIDIRECT
  648. #define BMAP_NII 1
  649. #define BMAP_II(sfi, x) ((void)(x), (sfi)->sfi_dindirect)
  650. #else
  651. #define BMAP_NII 0
  652. #define BMAP_II(sfi, x) ((void)(x), (void)(sfi), 0)
  653. #endif
  654. #endif
  655. #ifdef SFS_NTIDIRECT
  656. #define BMAP_NIII SFS_NTIDIRECT
  657. #define BMAP_III(sfi, x) ((sfi)->sfi_tindirect[(x)])
  658. #else
  659. #ifdef HAS_TIDIRECT
  660. #define BMAP_NIII 1
  661. #define BMAP_III(sfi, x) ((void)(x), (sfi)->sfi_tindirect)
  662. #else
  663. #define BMAP_NIII 0
  664. #define BMAP_III(sfi, x) ((void)(x), (void)(sfi), 0)
  665. #endif
  666. #endif
  667. #define BMAP_DMAX BMAP_ND
  668. #define BMAP_IMAX (BMAP_DMAX+SFS_DBPERIDB*BMAP_NI)
  669. #define BMAP_IIMAX (BMAP_IMAX+SFS_DBPERIDB*BMAP_NII)
  670. #define BMAP_IIIMAX (BMAP_IIMAX+SFS_DBPERIDB*BMAP_NIII)
  671. #define BMAP_DSIZE 1
  672. #define BMAP_ISIZE (BMAP_DSIZE*SFS_DBPERIDB)
  673. #define BMAP_IISIZE (BMAP_ISIZE*SFS_DBPERIDB)
  674. #define BMAP_IIISIZE (BMAP_IISIZE*SFS_DBPERIDB)
  675. static
  676. uint32_t
  677. dobmap(const struct sfs_inode *sfi, uint32_t fileblock)
  678. {
  679. uint32_t iblock, offset;
  680. if (fileblock < BMAP_DMAX) {
  681. return BMAP_D(sfi, fileblock);
  682. }
  683. else if (fileblock < BMAP_IMAX) {
  684. iblock = (fileblock - BMAP_DMAX)/BMAP_ISIZE;
  685. offset = (fileblock - BMAP_DMAX)%BMAP_ISIZE;
  686. return ibmap(BMAP_I(sfi, iblock), offset, BMAP_DSIZE);
  687. }
  688. else if (fileblock < BMAP_IIMAX) {
  689. iblock = (fileblock - BMAP_IMAX)/BMAP_IISIZE;
  690. offset = (fileblock - BMAP_IMAX)%BMAP_IISIZE;
  691. return ibmap(BMAP_II(sfi, iblock), offset, BMAP_ISIZE);
  692. }
  693. else if (fileblock < BMAP_IIIMAX) {
  694. iblock = (fileblock - BMAP_IIMAX)/BMAP_IIISIZE;
  695. offset = (fileblock - BMAP_IIMAX)%BMAP_IIISIZE;
  696. return ibmap(BMAP_III(sfi, iblock), offset, BMAP_IISIZE);
  697. }
  698. return 0;
  699. }
  700. static
  701. void
  702. dirread(struct sfs_inode *sfi, struct sfs_dir *d, unsigned nd)
  703. {
  704. const unsigned atonce = SFS_BLOCKSIZE/sizeof(struct sfs_dir);
  705. unsigned nblocks = SFS_ROUNDUP(nd, atonce) / atonce;
  706. unsigned i, j;
  707. for (i=0; i<nblocks; i++) {
  708. uint32_t block = dobmap(sfi, i);
  709. if (block!=0) {
  710. diskread(d + i*atonce, block);
  711. for (j=0; j<atonce; j++) {
  712. swapdir(&d[i*atonce+j]);
  713. }
  714. }
  715. else {
  716. warnx("Warning: sparse directory found");
  717. bzero(d + i*atonce, SFS_BLOCKSIZE);
  718. }
  719. }
  720. }
  721. static
  722. void
  723. dirwrite(const struct sfs_inode *sfi, struct sfs_dir *d, int nd)
  724. {
  725. const unsigned atonce = SFS_BLOCKSIZE/sizeof(struct sfs_dir);
  726. unsigned nblocks = SFS_ROUNDUP(nd, atonce) / atonce;
  727. unsigned i, j, bad;
  728. for (i=0; i<nblocks; i++) {
  729. uint32_t block = dobmap(sfi, i);
  730. if (block!=0) {
  731. for (j=0; j<atonce; j++) {
  732. swapdir(&d[i*atonce+j]);
  733. }
  734. diskwrite(d + i*atonce, block);
  735. }
  736. else {
  737. for (j=bad=0; j<atonce; j++) {
  738. if (d[i*atonce+j].sfd_ino != SFS_NOINO ||
  739. d[i*atonce+j].sfd_name[0] != 0) {
  740. bad = 1;
  741. }
  742. }
  743. if (bad) {
  744. warnx("Cannot write to missing block in "
  745. "sparse directory (ERROR)");
  746. setbadness(EXIT_UNRECOV);
  747. }
  748. }
  749. }
  750. }
  751. ////////////////////////////////////////////////////////////
  752. static struct sfs_dir *global_sortdirs;
  753. static
  754. int
  755. dirsortfunc(const void *aa, const void *bb)
  756. {
  757. const int *a = (const int *)aa;
  758. const int *b = (const int *)bb;
  759. const struct sfs_dir *ad = &global_sortdirs[*a];
  760. const struct sfs_dir *bd = &global_sortdirs[*b];
  761. return strcmp(ad->sfd_name, bd->sfd_name);
  762. }
  763. #ifdef NO_QSORT
  764. static
  765. void
  766. qsort(int *data, int num, size_t size, int (*f)(const void *, const void *))
  767. {
  768. int i, j;
  769. (void)size;
  770. /* because I'm lazy, bubble sort */
  771. for (i=0; i<num-1; i++) {
  772. for (j=i+1; j<num; j++) {
  773. if (f(&data[i], &data[j]) < 0) {
  774. int tmp = data[i];
  775. data[i] = data[j];
  776. data[j] = tmp;
  777. }
  778. }
  779. }
  780. }
  781. #endif
  782. static
  783. void
  784. sortdir(int *vector, struct sfs_dir *d, int nd)
  785. {
  786. global_sortdirs = d;
  787. qsort(vector, nd, sizeof(int), dirsortfunc);
  788. }
  789. /* tries to add a directory entry; returns 0 on success */
  790. static
  791. int
  792. dir_tryadd(struct sfs_dir *d, int nd, const char *name, uint32_t ino)
  793. {
  794. int i;
  795. for (i=0; i<nd; i++) {
  796. if (d[i].sfd_ino==SFS_NOINO) {
  797. d[i].sfd_ino = ino;
  798. assert(strlen(name) < sizeof(d[i].sfd_name));
  799. strcpy(d[i].sfd_name, name);
  800. return 0;
  801. }
  802. }
  803. return -1;
  804. }
  805. static
  806. int
  807. check_dir_entry(const char *pathsofar, uint32_t index, struct sfs_dir *sfd)
  808. {
  809. int dchanged = 0;
  810. if (sfd->sfd_ino == SFS_NOINO) {
  811. if (sfd->sfd_name[0] != 0) {
  812. setbadness(EXIT_RECOV);
  813. warnx("Directory /%s entry %lu has name but no file",
  814. pathsofar, (unsigned long) index);
  815. sfd->sfd_name[0] = 0;
  816. dchanged = 1;
  817. }
  818. }
  819. else {
  820. if (sfd->sfd_name[0] == 0) {
  821. snprintf(sfd->sfd_name, sizeof(sfd->sfd_name),
  822. "FSCK.%lu.%lu",
  823. (unsigned long) sfd->sfd_ino,
  824. (unsigned long) uniquecounter++);
  825. setbadness(EXIT_RECOV);
  826. warnx("Directory /%s entry %lu has file but "
  827. "no name (fixed: %s)",
  828. pathsofar, (unsigned long) index,
  829. sfd->sfd_name);
  830. dchanged = 1;
  831. }
  832. if (checknullstring(sfd->sfd_name, sizeof(sfd->sfd_name))) {
  833. setbadness(EXIT_RECOV);
  834. warnx("Directory /%s entry %lu not "
  835. "null-terminated (fixed)",
  836. pathsofar, (unsigned long) index);
  837. dchanged = 1;
  838. }
  839. if (checkbadstring(sfd->sfd_name)) {
  840. setbadness(EXIT_RECOV);
  841. warnx("Directory /%s entry %lu contains invalid "
  842. "characters (fixed)",
  843. pathsofar, (unsigned long) index);
  844. dchanged = 1;
  845. }
  846. }
  847. return dchanged;
  848. }
  849. ////////////////////////////////////////////////////////////
  850. static
  851. int
  852. check_dir(uint32_t ino, uint32_t parentino, const char *pathsofar)
  853. {
  854. struct sfs_inode sfi;
  855. struct sfs_dir *direntries;
  856. int *sortvector;
  857. uint32_t dirsize, ndirentries, maxdirentries, subdircount, i;
  858. int ichanged=0, dchanged=0, dotseen=0, dotdotseen=0;
  859. diskread(&sfi, ino);
  860. swapinode(&sfi);
  861. if (remember_dir(ino, pathsofar)) {
  862. /* crosslinked dir */
  863. return 1;
  864. }
  865. bitmap_mark(ino, B_INODE, ino);
  866. count_dirs++;
  867. if (sfi.sfi_size % sizeof(struct sfs_dir) != 0) {
  868. setbadness(EXIT_RECOV);
  869. warnx("Directory /%s has illegal size %lu (fixed)",
  870. pathsofar, (unsigned long) sfi.sfi_size);
  871. sfi.sfi_size = SFS_ROUNDUP(sfi.sfi_size,
  872. sizeof(struct sfs_dir));
  873. ichanged = 1;
  874. }
  875. if (check_inode_blocks(ino, &sfi, 1)) {
  876. ichanged = 1;
  877. }
  878. ndirentries = sfi.sfi_size/sizeof(struct sfs_dir);
  879. maxdirentries = SFS_ROUNDUP(ndirentries,
  880. SFS_BLOCKSIZE/sizeof(struct sfs_dir));
  881. dirsize = maxdirentries * sizeof(struct sfs_dir);
  882. direntries = domalloc(dirsize);
  883. sortvector = domalloc(ndirentries * sizeof(int));
  884. dirread(&sfi, direntries, ndirentries);
  885. for (i=ndirentries; i<maxdirentries; i++) {
  886. direntries[i].sfd_ino = SFS_NOINO;
  887. bzero(direntries[i].sfd_name, sizeof(direntries[i].sfd_name));
  888. }
  889. for (i=0; i<ndirentries; i++) {
  890. if (check_dir_entry(pathsofar, i, &direntries[i])) {
  891. dchanged = 1;
  892. }
  893. sortvector[i] = i;
  894. }
  895. sortdir(sortvector, direntries, ndirentries);
  896. /* don't use ndirentries-1 here in case ndirentries == 0 */
  897. for (i=0; i+1<ndirentries; i++) {
  898. struct sfs_dir *d1 = &direntries[sortvector[i]];
  899. struct sfs_dir *d2 = &direntries[sortvector[i+1]];
  900. assert(d1 != d2);
  901. if (d1->sfd_ino == SFS_NOINO) {
  902. continue;
  903. }
  904. if (!strcmp(d1->sfd_name, d2->sfd_name)) {
  905. if (d1->sfd_ino == d2->sfd_ino) {
  906. setbadness(EXIT_RECOV);
  907. warnx("Directory /%s: Duplicate entries for "
  908. "%s (merged)",
  909. pathsofar, d1->sfd_name);
  910. d1->sfd_ino = SFS_NOINO;
  911. d1->sfd_name[0] = 0;
  912. }
  913. else {
  914. snprintf(d1->sfd_name, sizeof(d1->sfd_name),
  915. "FSCK.%lu.%lu",
  916. (unsigned long) d1->sfd_ino,
  917. (unsigned long) uniquecounter++);
  918. setbadness(EXIT_RECOV);
  919. warnx("Directory /%s: Duplicate names %s "
  920. "(one renamed: %s)",
  921. pathsofar, d2->sfd_name, d1->sfd_name);
  922. }
  923. dchanged = 1;
  924. }
  925. }
  926. for (i=0; i<ndirentries; i++) {
  927. if (!strcmp(direntries[i].sfd_name, ".")) {
  928. if (direntries[i].sfd_ino != ino) {
  929. setbadness(EXIT_RECOV);
  930. warnx("Directory /%s: Incorrect `.' entry "
  931. "(fixed)", pathsofar);
  932. direntries[i].sfd_ino = ino;
  933. dchanged = 1;
  934. }
  935. assert(dotseen==0); /* due to duplicate checking */
  936. dotseen = 1;
  937. }
  938. else if (!strcmp(direntries[i].sfd_name, "..")) {
  939. if (direntries[i].sfd_ino != parentino) {
  940. setbadness(EXIT_RECOV);
  941. warnx("Directory /%s: Incorrect `..' entry "
  942. "(fixed)", pathsofar);
  943. direntries[i].sfd_ino = parentino;
  944. dchanged = 1;
  945. }
  946. assert(dotdotseen==0); /* due to duplicate checking */
  947. dotdotseen = 1;
  948. }
  949. }
  950. if (!dotseen) {
  951. if (dir_tryadd(direntries, ndirentries, ".", ino)==0) {
  952. setbadness(EXIT_RECOV);
  953. warnx("Directory /%s: No `.' entry (added)",
  954. pathsofar);
  955. dchanged = 1;
  956. }
  957. else if (dir_tryadd(direntries, maxdirentries, ".", ino)==0) {
  958. setbadness(EXIT_RECOV);
  959. warnx("Directory /%s: No `.' entry (added)",
  960. pathsofar);
  961. ndirentries++;
  962. dchanged = 1;
  963. sfi.sfi_size += sizeof(struct sfs_dir);
  964. ichanged = 1;
  965. }
  966. else {
  967. setbadness(EXIT_UNRECOV);
  968. warnx("Directory /%s: No `.' entry (NOT FIXED)",
  969. pathsofar);
  970. }
  971. }
  972. if (!dotdotseen) {
  973. if (dir_tryadd(direntries, ndirentries, "..", parentino)==0) {
  974. setbadness(EXIT_RECOV);
  975. warnx("Directory /%s: No `..' entry (added)",
  976. pathsofar);
  977. dchanged = 1;
  978. }
  979. else if (dir_tryadd(direntries, maxdirentries, "..",
  980. parentino)==0) {
  981. setbadness(EXIT_RECOV);
  982. warnx("Directory /%s: No `..' entry (added)",
  983. pathsofar);
  984. ndirentries++;
  985. dchanged = 1;
  986. sfi.sfi_size += sizeof(struct sfs_dir);
  987. ichanged = 1;
  988. }
  989. else {
  990. setbadness(EXIT_UNRECOV);
  991. warnx("Directory /%s: No `..' entry (NOT FIXED)",
  992. pathsofar);
  993. }
  994. }
  995. subdircount=0;
  996. for (i=0; i<ndirentries; i++) {
  997. if (!strcmp(direntries[i].sfd_name, ".")) {
  998. /* nothing */
  999. }
  1000. else if (!strcmp(direntries[i].sfd_name, "..")) {
  1001. /* nothing */
  1002. }
  1003. else if (direntries[i].sfd_ino == SFS_NOINO) {
  1004. /* nothing */
  1005. }
  1006. else {
  1007. char path[strlen(pathsofar)+SFS_NAMELEN+1];
  1008. struct sfs_inode subsfi;
  1009. diskread(&subsfi, direntries[i].sfd_ino);
  1010. swapinode(&subsfi);
  1011. snprintf(path, sizeof(path), "%s/%s",
  1012. pathsofar, direntries[i].sfd_name);
  1013. switch (subsfi.sfi_type) {
  1014. case SFS_TYPE_FILE:
  1015. if (check_inode_blocks(direntries[i].sfd_ino,
  1016. &subsfi, 0)) {
  1017. swapinode(&subsfi);
  1018. diskwrite(&subsfi,
  1019. direntries[i].sfd_ino);
  1020. }
  1021. observe_filelink(direntries[i].sfd_ino);
  1022. break;
  1023. case SFS_TYPE_DIR:
  1024. if (check_dir(direntries[i].sfd_ino,
  1025. ino,
  1026. path)) {
  1027. setbadness(EXIT_RECOV);
  1028. warnx("Directory /%s: Crosslink to "
  1029. "other directory (removed)",
  1030. path);
  1031. direntries[i].sfd_ino = SFS_NOINO;
  1032. direntries[i].sfd_name[0] = 0;
  1033. dchanged = 1;
  1034. }
  1035. else {
  1036. subdircount++;
  1037. }
  1038. break;
  1039. default:
  1040. setbadness(EXIT_RECOV);
  1041. warnx("Object /%s: Invalid inode type "
  1042. "(removed)", path);
  1043. direntries[i].sfd_ino = SFS_NOINO;
  1044. direntries[i].sfd_name[0] = 0;
  1045. dchanged = 1;
  1046. break;
  1047. }
  1048. }
  1049. }
  1050. if (sfi.sfi_linkcount != subdircount+2) {
  1051. setbadness(EXIT_RECOV);
  1052. warnx("Directory /%s: Link count %lu should be %lu (fixed)",
  1053. pathsofar, (unsigned long) sfi.sfi_linkcount,
  1054. (unsigned long) subdircount+2);
  1055. sfi.sfi_linkcount = subdircount+2;
  1056. ichanged = 1;
  1057. }
  1058. if (dchanged) {
  1059. dirwrite(&sfi, direntries, ndirentries);
  1060. }
  1061. if (ichanged) {
  1062. swapinode(&sfi);
  1063. diskwrite(&sfi, ino);
  1064. }
  1065. free(direntries);
  1066. free(sortvector);
  1067. return 0;
  1068. }
  1069. static
  1070. void
  1071. check_root_dir(void)
  1072. {
  1073. struct sfs_inode sfi;
  1074. diskread(&sfi, SFS_ROOT_LOCATION);
  1075. swapinode(&sfi);
  1076. switch (sfi.sfi_type) {
  1077. case SFS_TYPE_DIR:
  1078. break;
  1079. case SFS_TYPE_FILE:
  1080. warnx("Root directory inode is a regular file (fixed)");
  1081. goto fix;
  1082. default:
  1083. warnx("Root directory inode has invalid type %lu (fixed)",
  1084. (unsigned long) sfi.sfi_type);
  1085. fix:
  1086. setbadness(EXIT_RECOV);
  1087. sfi.sfi_type = SFS_TYPE_DIR;
  1088. swapinode(&sfi);
  1089. diskwrite(&sfi, SFS_ROOT_LOCATION);
  1090. break;
  1091. }
  1092. check_dir(SFS_ROOT_LOCATION, SFS_ROOT_LOCATION, "");
  1093. }
  1094. ////////////////////////////////////////////////////////////
  1095. int
  1096. main(int argc, char **argv)
  1097. {
  1098. #ifdef HOST
  1099. hostcompat_init(argc, argv);
  1100. #endif
  1101. if (argc!=2) {
  1102. errx(EXIT_USAGE, "Usage: sfsck device/diskfile");
  1103. }
  1104. assert(sizeof(struct sfs_super)==SFS_BLOCKSIZE);
  1105. assert(sizeof(struct sfs_inode)==SFS_BLOCKSIZE);
  1106. assert(SFS_BLOCKSIZE % sizeof(struct sfs_dir) == 0);
  1107. opendisk(argv[1]);
  1108. check_sb();
  1109. check_root_dir();
  1110. check_bitmap();
  1111. adjust_filelinks();
  1112. closedisk();
  1113. warnx("%lu blocks used (of %lu); %lu directories; %lu files",
  1114. count_blocks, (unsigned long) nblocks, count_dirs, count_files);
  1115. switch (badness) {
  1116. case EXIT_USAGE:
  1117. case EXIT_FATAL:
  1118. default:
  1119. /* not supposed to happen here */
  1120. assert(0);
  1121. break;
  1122. case EXIT_UNRECOV:
  1123. warnx("WARNING - unrecoverable errors. Maybe try again?");
  1124. break;
  1125. case EXIT_RECOV:
  1126. warnx("Caution - filesystem modified. Run again for luck.");
  1127. break;
  1128. case EXIT_CLEAN:
  1129. break;
  1130. }
  1131. return badness;
  1132. }