/filesystems/unixfs/ancientfs/ancientfs_itp.c

http://macfuse.googlecode.com/ · C · 500 lines · 404 code · 84 blank · 12 comment · 84 complexity · 548d6687497247cc5e2b48ff548bc04f MD5 · raw file

  1. /*
  2. * Ancient UNIX File Systems for MacFUSE
  3. * Amit Singh
  4. * http://osxbook.com
  5. */
  6. #include "ancientfs_itp.h"
  7. #include "unixfs_common.h"
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/stat.h>
  16. DECL_UNIXFS("UNIX itp", itp);
  17. static void*
  18. unixfs_internal_init(const char* dmg, uint32_t flags, fs_endian_t fse,
  19. char** fsname, char** volname)
  20. {
  21. int fd = -1;
  22. if ((fd = open(dmg, O_RDONLY)) < 0) {
  23. perror("open");
  24. return NULL;
  25. }
  26. int err, i, j;
  27. struct stat stbuf;
  28. struct super_block* sb = (struct super_block*)0;
  29. struct filsys* fs = (struct filsys*)0;
  30. uint32_t tapedir_begin_block = 0, tapedir_end_block = 0, last_block = 0;
  31. if ((err = fstat(fd, &stbuf)) != 0) {
  32. perror("fstat");
  33. goto out;
  34. }
  35. if (!S_ISREG(stbuf.st_mode) && !(flags & UNIXFS_FORCE)) {
  36. err = EINVAL;
  37. fprintf(stderr, "%s is not a tape image file\n", dmg);
  38. goto out;
  39. }
  40. if (flags & ANCIENTFS_DECTAPE) {
  41. tapedir_begin_block = 0;
  42. tapedir_end_block = TAPEDIR_END_BLOCK_DEC;
  43. } else if (flags & ANCIENTFS_MAGTAPE) {
  44. tapedir_begin_block = 0;
  45. tapedir_end_block = TAPEDIR_END_BLOCK_MAG;
  46. } else if (flags & ANCIENTFS_GENTAPE) {
  47. tapedir_begin_block = 0;
  48. tapedir_end_block = TAPEDIR_END_BLOCK_GENERIC;
  49. } else {
  50. err = EINVAL;
  51. fprintf(stderr, "unrecognized tape type\n");
  52. goto out;
  53. }
  54. if (S_ISREG(stbuf.st_mode))
  55. last_block = stbuf.st_size / BSIZE;
  56. else
  57. last_block = tapedir_end_block;
  58. sb = malloc(sizeof(struct super_block));
  59. if (!sb) {
  60. err = ENOMEM;
  61. goto out;
  62. }
  63. assert(sizeof(struct filsys) <= BSIZE);
  64. fs = calloc(1, BSIZE);
  65. if (!fs) {
  66. free(sb);
  67. err = ENOMEM;
  68. goto out;
  69. }
  70. unixfs = sb;
  71. unixfs->s_flags = flags;
  72. unixfs->s_endian = (fse == UNIXFS_FS_INVALID) ? UNIXFS_FS_PDP : fse;
  73. unixfs->s_fs_info = (void*)fs;
  74. unixfs->s_bdev = fd;
  75. /* must initialize the inode layer before sanity checking */
  76. if ((err = unixfs_inodelayer_init(sizeof(struct tap_node_info))) != 0)
  77. goto out;
  78. struct inode* rootip = unixfs_inodelayer_iget((ino_t)ROOTINO);
  79. if (!rootip) {
  80. fprintf(stderr, "*** fatal error: no root inode\n");
  81. abort();
  82. }
  83. rootip->I_mode = S_IFDIR | 0755;
  84. rootip->I_uid = getuid();
  85. rootip->I_gid = getgid();
  86. rootip->I_size = 2;
  87. rootip->I_atime_sec = rootip->I_mtime_sec = rootip->I_ctime_sec = time(0);
  88. struct tap_node_info* rootti = (struct tap_node_info*)rootip->I_private;
  89. rootti->ti_self = rootip;
  90. rootti->ti_name[0] = '\0';
  91. rootti->ti_parent = NULL;
  92. rootti->ti_children = NULL;
  93. rootti->ti_next_sibling = NULL;
  94. unixfs_inodelayer_isucceeded(rootip);
  95. fs->s_fsize = stbuf.st_size / BSIZE;
  96. fs->s_files = 0;
  97. fs->s_directories = 1 + 1 + 1;
  98. fs->s_rootip = rootip;
  99. fs->s_lastino = ROOTINO;
  100. char tapeblock[BSIZE];
  101. for (i = tapedir_begin_block; i < tapedir_end_block; i++) {
  102. if (pread(fd, tapeblock, BSIZE, (off_t)(i * BSIZE)) != BSIZE) {
  103. fprintf(stderr, "*** fatal error: cannot read tape block %llu\n",
  104. (off_t)i);
  105. err = EIO;
  106. goto out;
  107. }
  108. struct dinode_itp* di = (struct dinode_itp*)tapeblock;
  109. for (j = 0; j < INOPB; j++, di++) {
  110. if (ancientfs_itp_cksum((uint8_t*)di,
  111. unixfs->s_flags, unixfs->s_endian) != 0)
  112. continue;
  113. if (!di->di_path[0]) {
  114. if (flags & ANCIENTFS_GENTAPE)
  115. i = tapedir_end_block;
  116. continue;
  117. }
  118. ino_t parent_ino = ROOTINO;
  119. char* path = (char*)di->di_path;
  120. size_t pathlen = strlen(path);
  121. if ((*path == '.') && ((pathlen == 1) ||
  122. ((pathlen == 2) && (*(path + 1) == '/')))) {
  123. /* root */
  124. rootip->I_mode = fs16_to_host(unixfs->s_endian, di->di_mode);
  125. rootip->I_atime_sec = \
  126. rootip->I_mtime_sec = \
  127. rootip->I_ctime_sec = \
  128. fs32_to_host(unixfs->s_endian, di->di_mtime);
  129. continue;
  130. }
  131. /* we don't deal with many fancy paths here: just '/' and './' */
  132. if (*path == '/')
  133. path++;
  134. else if (*path == '.' && *(path + 1) == '/')
  135. path += 2;
  136. char *cnp, *term;
  137. for (cnp = strtok_r(path, "/", &term); cnp;
  138. cnp = strtok_r(NULL, "/", &term)) {
  139. /* we have { parent_ino, cnp } */
  140. struct stat stbuf;
  141. int missing = unixfs_internal_namei(parent_ino, cnp, &stbuf);
  142. if (!missing) {
  143. parent_ino = stbuf.st_ino;
  144. continue;
  145. }
  146. struct inode* ip =
  147. unixfs_inodelayer_iget((ino_t)(fs->s_lastino + 1));
  148. if (!ip) {
  149. fprintf(stderr, "*** fatal error: no inode for %llu\n",
  150. (ino64_t)(fs->s_lastino + 1));
  151. abort();
  152. }
  153. ip->I_mode = fs16_to_host(unixfs->s_endian, di->di_mode);
  154. ip->I_uid = di->di_uid;
  155. ip->I_gid = di->di_gid;
  156. ip->I_size = di->di_size0 << 16 |
  157. fs16_to_host(unixfs->s_endian, di->di_size1);
  158. ip->I_daddr[0] = (uint32_t)fs16_to_host(unixfs->s_endian,
  159. di->di_addr);
  160. ip->I_nlink = 1;
  161. ip->I_atime_sec = ip->I_mtime_sec = ip->I_ctime_sec =
  162. fs32_to_host(unixfs->s_endian, di->di_mtime);
  163. struct tap_node_info* ti = (struct tap_node_info*)ip->I_private;
  164. memcpy(ti->ti_name, cnp, strlen(cnp));
  165. ti->ti_self = ip;
  166. ti->ti_children = NULL;
  167. /* this should work out as long as we have no corruption */
  168. struct inode* parent_ip = unixfs_internal_iget(parent_ino);
  169. parent_ip->I_size += 1;
  170. ti->ti_parent = (struct tap_node_info*)(parent_ip->I_private);
  171. ti->ti_next_sibling = ti->ti_parent->ti_children;
  172. ti->ti_parent->ti_children = ti;
  173. if (S_ISDIR(ancientfs_itp_mode(ip->I_mode, flags))) {
  174. fs->s_directories++;
  175. parent_ino = fs->s_lastino + 1;
  176. ip->I_size = 2;
  177. ip->I_daddr[0] = 0;
  178. } else
  179. fs->s_files++;
  180. fs->s_lastino++;
  181. unixfs_internal_iput(parent_ip);
  182. unixfs_inodelayer_isucceeded(ip);
  183. /* no put */
  184. }
  185. }
  186. }
  187. unixfs->s_statvfs.f_bsize = BSIZE;
  188. unixfs->s_statvfs.f_frsize = BSIZE;
  189. unixfs->s_statvfs.f_ffree = 0;
  190. unixfs->s_statvfs.f_files = fs->s_files + fs->s_directories;
  191. unixfs->s_statvfs.f_blocks = fs->s_fsize;
  192. unixfs->s_statvfs.f_bfree = 0;
  193. unixfs->s_statvfs.f_bavail = 0;
  194. unixfs->s_dentsize = 1;
  195. unixfs->s_statvfs.f_namemax = DIRSIZ;
  196. snprintf(unixfs->s_fsname, UNIXFS_MNAMELEN, "UNIX itp");
  197. char* dmg_basename = basename((char*)dmg);
  198. snprintf(unixfs->s_volname, UNIXFS_MAXNAMLEN, "%s (tape=%s)",
  199. unixfs_fstype, (dmg_basename) ? dmg_basename : "Tape Image");
  200. *fsname = unixfs->s_fsname;
  201. *volname = unixfs->s_volname;
  202. out:
  203. if (err) {
  204. if (fd >= 0)
  205. close(fd);
  206. if (fs)
  207. free(fs);
  208. if (sb)
  209. free(sb);
  210. return NULL;
  211. }
  212. return sb;
  213. }
  214. static void
  215. unixfs_internal_fini(void* filsys)
  216. {
  217. struct super_block* sb = (struct super_block*)filsys;
  218. struct filsys* fs = (struct filsys*)sb->s_fs_info;
  219. ino_t i = fs->s_lastino;
  220. for (; i >= ROOTINO; i--) {
  221. struct inode* tmp = unixfs_internal_iget(i);
  222. if (tmp) {
  223. unixfs_internal_iput(tmp);
  224. unixfs_internal_iput(tmp);
  225. }
  226. }
  227. unixfs_inodelayer_fini();
  228. if (sb) {
  229. if (sb->s_bdev >= 0)
  230. close(sb->s_bdev);
  231. sb->s_bdev = -1;
  232. if (sb->s_fs_info)
  233. free(sb->s_fs_info);
  234. }
  235. }
  236. static off_t
  237. unixfs_internal_alloc(void)
  238. {
  239. return (off_t)0;
  240. }
  241. static off_t
  242. unixfs_internal_bmap(struct inode* ip, off_t lblkno, int* error)
  243. {
  244. off_t nblocks = (off_t)((ip->I_size + (BSIZE - 1)) / BSIZE);
  245. if (lblkno >= nblocks) {
  246. *error = EFBIG;
  247. return (off_t)0;
  248. }
  249. return (off_t)(ip->I_daddr[0] + lblkno);
  250. }
  251. static int
  252. unixfs_internal_bread(off_t blkno, char* blkbuf)
  253. {
  254. if (blkno >= ((struct filsys*)unixfs->s_fs_info)->s_fsize) {
  255. fprintf(stderr,
  256. "***fatal error: bread failed for block %llu\n", blkno);
  257. abort();
  258. /* NOTREACHED */
  259. }
  260. if (pread(unixfs->s_bdev, blkbuf, UNIXFS_IOSIZE(unixfs),
  261. blkno * (off_t)BSIZE) != UNIXFS_IOSIZE(unixfs))
  262. return EIO;
  263. return 0;
  264. }
  265. static struct inode*
  266. unixfs_internal_iget(ino_t ino)
  267. {
  268. struct inode* ip = unixfs_inodelayer_iget(ino);
  269. if (!ip) {
  270. fprintf(stderr, "*** fatal error: no inode for %llu\n", (ino64_t)ino);
  271. abort();
  272. }
  273. if (ip->I_initialized)
  274. return ip;
  275. unixfs_inodelayer_ifailed(ip);
  276. return NULL;
  277. }
  278. static void
  279. unixfs_internal_iput(struct inode* ip)
  280. {
  281. unixfs_inodelayer_iput(ip);
  282. }
  283. static int
  284. unixfs_internal_igetattr(ino_t ino, struct stat* stbuf)
  285. {
  286. struct inode* ip = unixfs_internal_iget(ino);
  287. if (!ip)
  288. return ENOENT;
  289. unixfs_internal_istat(ip, stbuf);
  290. unixfs_internal_iput(ip);
  291. return 0;
  292. }
  293. static void
  294. unixfs_internal_istat(struct inode* ip, struct stat* stbuf)
  295. {
  296. memcpy(stbuf, &ip->I_stat, sizeof(struct stat));
  297. stbuf->st_mode = ancientfs_itp_mode(ip->I_mode, unixfs->s_flags);
  298. }
  299. static int
  300. unixfs_internal_namei(ino_t parentino, const char* name, struct stat* stbuf)
  301. {
  302. int ret = ENOENT;
  303. stbuf->st_ino = 0;
  304. size_t namelen = strlen(name);
  305. if (namelen > DIRSIZ)
  306. return ENAMETOOLONG;
  307. struct inode* dp = unixfs_internal_iget(parentino);
  308. if (!dp)
  309. return ENOENT;
  310. if (!S_ISDIR(ancientfs_itp_mode(dp->I_mode, unixfs->s_flags))) {
  311. ret = ENOTDIR;
  312. goto out;
  313. }
  314. struct tap_node_info* child =
  315. ((struct tap_node_info*)dp->I_private)->ti_children;;
  316. if (!child) {
  317. ret = ENOENT;
  318. goto out;
  319. }
  320. int found = 0;
  321. do {
  322. size_t target_namelen = strlen((const char*)child->ti_name);
  323. if ((namelen == target_namelen) &&
  324. (memcmp(name, child->ti_name, target_namelen) == 0)) {
  325. found = 1;
  326. break;
  327. }
  328. child = child->ti_next_sibling;
  329. } while (child);
  330. if (found)
  331. ret = unixfs_internal_igetattr((ino_t)child->ti_self->I_ino, stbuf);
  332. out:
  333. unixfs_internal_iput(dp);
  334. return ret;
  335. }
  336. static int
  337. unixfs_internal_nextdirentry(struct inode* dp, struct unixfs_dirbuf* dirbuf,
  338. off_t* offset, struct unixfs_direntry* dent)
  339. {
  340. if (*offset >= dp->I_size)
  341. return -1;
  342. if (*offset < 2) {
  343. int idx = 0;
  344. dent->name[idx++] = '.';
  345. dent->ino = ROOTINO;
  346. if (*offset == 1) {
  347. if (dp->I_ino != ROOTINO) {
  348. struct inode* pdp = unixfs_internal_iget(dp->I_ino);
  349. if (pdp) {
  350. dent->ino = pdp->I_ino;
  351. unixfs_internal_iput(pdp);
  352. }
  353. }
  354. dent->name[idx++] = '.';
  355. }
  356. dent->name[idx++] = '\0';
  357. goto out;
  358. }
  359. struct tap_node_info* child =
  360. ((struct tap_node_info*)dp->I_private)->ti_children;;
  361. off_t i;
  362. for (i = 0; i < (*offset - 2); i++)
  363. child = child->ti_next_sibling;
  364. dent->ino = (ino_t)child->ti_self->I_ino;
  365. size_t dirnamelen = min(DIRSIZ, UNIXFS_MAXNAMLEN);
  366. memcpy(dent->name, child->ti_name, dirnamelen);
  367. dent->name[dirnamelen] = '\0';
  368. out:
  369. *offset += 1;
  370. return 0;
  371. }
  372. static ssize_t
  373. unixfs_internal_pbread(struct inode* ip, char* buf, size_t nbyte, off_t offset,
  374. int* error)
  375. {
  376. ssize_t done = 0;
  377. size_t tomove = 0;
  378. ssize_t remaining = nbyte;
  379. ssize_t iosize = UNIXFS_IOSIZE(unixfs);
  380. char blkbuf[iosize];
  381. char* p = buf;
  382. while (remaining > 0) {
  383. off_t lbn = offset / BSIZE;
  384. off_t bn = unixfs_internal_bmap(ip, lbn, error);
  385. if (UNIXFS_BADBLOCK(bn, *error))
  386. break;
  387. *error = unixfs_internal_bread(bn, blkbuf);
  388. if (*error != 0)
  389. break;
  390. tomove = (remaining > iosize) ? iosize : remaining;
  391. memcpy(p, blkbuf, tomove);
  392. remaining -= tomove;
  393. done += tomove;
  394. offset += tomove;
  395. p += tomove;
  396. }
  397. if ((done == 0) && *error)
  398. return -1;
  399. return done;
  400. }
  401. static int
  402. unixfs_internal_readlink(ino_t ino, char path[UNIXFS_MAXPATHLEN])
  403. {
  404. return ENOSYS;
  405. }
  406. static int
  407. unixfs_internal_sanitycheck(void* filsys, off_t disksize)
  408. {
  409. return 0;
  410. }
  411. static int
  412. unixfs_internal_statvfs(struct statvfs* svb)
  413. {
  414. memcpy(svb, &unixfs->s_statvfs, sizeof(struct statvfs));
  415. return 0;
  416. }