/filesystems/unixfs/ancientfs/ancientfs_tp.c

http://macfuse.googlecode.com/ · C · 506 lines · 409 code · 84 blank · 13 comment · 86 complexity · a467c38cbec8efddb48ace1872272349 MD5 · raw file

  1. /*
  2. * Ancient UNIX File Systems for MacFUSE
  3. * Amit Singh
  4. * http://osxbook.com
  5. */
  6. #include "ancientfs_tp.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 tp", tp);
  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 = 1;
  42. tapedir_end_block = TAPEDIR_END_BLOCK_DEC;
  43. } else if (flags & ANCIENTFS_MAGTAPE) {
  44. tapedir_begin_block = 1;
  45. tapedir_end_block = TAPEDIR_END_BLOCK_MAG;
  46. } else if (flags & ANCIENTFS_GENTAPE) {
  47. tapedir_begin_block = 1;
  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_tp* di = (struct dinode_tp*)tapeblock;
  109. for (j = 0; j < INOPB; j++, di++) {
  110. if (ancientfs_tp_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 = S_IFDIR | fs16_to_host(unixfs->s_endian,
  125. di->di_mode);
  126. rootip->I_atime_sec = \
  127. rootip->I_mtime_sec = \
  128. rootip->I_ctime_sec = \
  129. fs32_to_host(unixfs->s_endian, di->di_mtime);
  130. continue;
  131. }
  132. /* we don't deal with many fancy paths here: just '/' and './' */
  133. if (*path == '/')
  134. path++;
  135. else if (*path == '.' && *(path + 1) == '/')
  136. path += 2;
  137. char *cnp, *term;
  138. for (cnp = strtok_r(path, "/", &term); cnp;
  139. cnp = strtok_r(NULL, "/", &term)) {
  140. /* we have { parent_ino, cnp } */
  141. struct stat stbuf;
  142. int missing = unixfs_internal_namei(parent_ino, cnp, &stbuf);
  143. if (!missing) {
  144. parent_ino = stbuf.st_ino;
  145. continue;
  146. }
  147. /* create directory */
  148. struct inode* ip =
  149. unixfs_inodelayer_iget((ino_t)(fs->s_lastino + 1));
  150. if (!ip) {
  151. fprintf(stderr, "*** fatal error: no inode for %llu\n",
  152. (ino64_t)(fs->s_lastino + 1));
  153. abort();
  154. }
  155. if (term) { /* directory */
  156. fs->s_directories++;
  157. ip->I_mode = S_IFDIR | 0755;
  158. ip->I_uid = getuid();
  159. ip->I_gid = getgid();
  160. ip->I_size = 2;
  161. } else {
  162. fs->s_files++;
  163. ip->I_mode = fs16_to_host(unixfs->s_endian, di->di_mode);
  164. ip->I_uid = di->di_uid;
  165. ip->I_gid = di->di_gid;
  166. ip->I_size = di->di_size0 << 16 |
  167. fs16_to_host(unixfs->s_endian, di->di_size1);
  168. ip->I_daddr[0] = (uint32_t)fs16_to_host(unixfs->s_endian,
  169. di->di_addr);
  170. }
  171. ip->I_nlink = 1;
  172. ip->I_atime_sec = ip->I_mtime_sec = ip->I_ctime_sec =
  173. fs32_to_host(unixfs->s_endian, di->di_mtime);
  174. struct tap_node_info* ti = (struct tap_node_info*)ip->I_private;
  175. memcpy(ti->ti_name, cnp, strlen(cnp));
  176. ti->ti_self = ip;
  177. ti->ti_children = NULL;
  178. /* this should work out as long as we have no corruption */
  179. struct inode* parent_ip = unixfs_internal_iget(parent_ino);
  180. parent_ip->I_size += 1;
  181. ti->ti_parent = (struct tap_node_info*)(parent_ip->I_private);
  182. ti->ti_next_sibling = ti->ti_parent->ti_children;
  183. ti->ti_parent->ti_children = ti;
  184. if (term)
  185. parent_ino = fs->s_lastino + 1;
  186. fs->s_lastino++;
  187. unixfs_internal_iput(parent_ip);
  188. unixfs_inodelayer_isucceeded(ip);
  189. /* no put */
  190. }
  191. }
  192. }
  193. unixfs->s_statvfs.f_bsize = BSIZE;
  194. unixfs->s_statvfs.f_frsize = BSIZE;
  195. unixfs->s_statvfs.f_ffree = 0;
  196. unixfs->s_statvfs.f_files = fs->s_files + fs->s_directories;
  197. unixfs->s_statvfs.f_blocks = fs->s_fsize;
  198. unixfs->s_statvfs.f_bfree = 0;
  199. unixfs->s_statvfs.f_bavail = 0;
  200. unixfs->s_dentsize = 1;
  201. unixfs->s_statvfs.f_namemax = DIRSIZ;
  202. snprintf(unixfs->s_fsname, UNIXFS_MNAMELEN, "UNIX tp");
  203. char* dmg_basename = basename((char*)dmg);
  204. snprintf(unixfs->s_volname, UNIXFS_MAXNAMLEN, "%s (tape=%s)",
  205. unixfs_fstype, (dmg_basename) ? dmg_basename : "Tape Image");
  206. *fsname = unixfs->s_fsname;
  207. *volname = unixfs->s_volname;
  208. out:
  209. if (err) {
  210. if (fd >= 0)
  211. close(fd);
  212. if (fs)
  213. free(fs);
  214. if (sb)
  215. free(sb);
  216. return NULL;
  217. }
  218. return sb;
  219. }
  220. static void
  221. unixfs_internal_fini(void* filsys)
  222. {
  223. struct super_block* sb = (struct super_block*)filsys;
  224. struct filsys* fs = (struct filsys*)sb->s_fs_info;
  225. ino_t i = fs->s_lastino;
  226. for (; i >= ROOTINO; i--) {
  227. struct inode* tmp = unixfs_internal_iget(i);
  228. if (tmp) {
  229. unixfs_internal_iput(tmp);
  230. unixfs_internal_iput(tmp);
  231. }
  232. }
  233. unixfs_inodelayer_fini();
  234. if (sb) {
  235. if (sb->s_bdev >= 0)
  236. close(sb->s_bdev);
  237. sb->s_bdev = -1;
  238. if (sb->s_fs_info)
  239. free(sb->s_fs_info);
  240. }
  241. }
  242. static off_t
  243. unixfs_internal_alloc(void)
  244. {
  245. return (off_t)0;
  246. }
  247. static off_t
  248. unixfs_internal_bmap(struct inode* ip, off_t lblkno, int* error)
  249. {
  250. off_t nblocks = (off_t)((ip->I_size + (BSIZE - 1)) / BSIZE);
  251. if (lblkno >= nblocks) {
  252. *error = EFBIG;
  253. return (off_t)0;
  254. }
  255. return (off_t)(ip->I_daddr[0] + lblkno);
  256. }
  257. static int
  258. unixfs_internal_bread(off_t blkno, char* blkbuf)
  259. {
  260. if (blkno >= ((struct filsys*)unixfs->s_fs_info)->s_fsize) {
  261. fprintf(stderr,
  262. "***fatal error: bread failed for block %llu\n", blkno);
  263. abort();
  264. /* NOTREACHED */
  265. }
  266. if (pread(unixfs->s_bdev, blkbuf, UNIXFS_IOSIZE(unixfs),
  267. blkno * (off_t)BSIZE) != UNIXFS_IOSIZE(unixfs))
  268. return EIO;
  269. return 0;
  270. }
  271. static struct inode*
  272. unixfs_internal_iget(ino_t ino)
  273. {
  274. struct inode* ip = unixfs_inodelayer_iget(ino);
  275. if (!ip) {
  276. fprintf(stderr, "*** fatal error: no inode for %llu\n", (ino64_t)ino);
  277. abort();
  278. }
  279. if (ip->I_initialized)
  280. return ip;
  281. unixfs_inodelayer_ifailed(ip);
  282. return NULL;
  283. }
  284. static void
  285. unixfs_internal_iput(struct inode* ip)
  286. {
  287. unixfs_inodelayer_iput(ip);
  288. }
  289. static int
  290. unixfs_internal_igetattr(ino_t ino, struct stat* stbuf)
  291. {
  292. struct inode* ip = unixfs_internal_iget(ino);
  293. if (!ip)
  294. return ENOENT;
  295. unixfs_internal_istat(ip, stbuf);
  296. unixfs_internal_iput(ip);
  297. return 0;
  298. }
  299. static void
  300. unixfs_internal_istat(struct inode* ip, struct stat* stbuf)
  301. {
  302. memcpy(stbuf, &ip->I_stat, sizeof(struct stat));
  303. stbuf->st_mode = ancientfs_tp_mode(ip->I_mode, unixfs->s_flags);
  304. }
  305. static int
  306. unixfs_internal_namei(ino_t parentino, const char* name, struct stat* stbuf)
  307. {
  308. int ret = ENOENT;
  309. stbuf->st_ino = 0;
  310. size_t namelen = strlen(name);
  311. if (namelen > DIRSIZ)
  312. return ENAMETOOLONG;
  313. struct inode* dp = unixfs_internal_iget(parentino);
  314. if (!dp)
  315. return ENOENT;
  316. if (!S_ISDIR(ancientfs_tp_mode(dp->I_mode, unixfs->s_flags))) {
  317. ret = ENOTDIR;
  318. goto out;
  319. }
  320. struct tap_node_info* child =
  321. ((struct tap_node_info*)dp->I_private)->ti_children;;
  322. if (!child) {
  323. ret = ENOENT;
  324. goto out;
  325. }
  326. int found = 0;
  327. do {
  328. size_t target_namelen = strlen((const char*)child->ti_name);
  329. if ((namelen == target_namelen) &&
  330. (memcmp(name, child->ti_name, target_namelen) == 0)) {
  331. found = 1;
  332. break;
  333. }
  334. child = child->ti_next_sibling;
  335. } while (child);
  336. if (found)
  337. ret = unixfs_internal_igetattr((ino_t)child->ti_self->I_ino, stbuf);
  338. out:
  339. unixfs_internal_iput(dp);
  340. return ret;
  341. }
  342. static int
  343. unixfs_internal_nextdirentry(struct inode* dp, struct unixfs_dirbuf* dirbuf,
  344. off_t* offset, struct unixfs_direntry* dent)
  345. {
  346. if (*offset >= dp->I_size)
  347. return -1;
  348. if (*offset < 2) {
  349. int idx = 0;
  350. dent->name[idx++] = '.';
  351. dent->ino = ROOTINO;
  352. if (*offset == 1) {
  353. if (dp->I_ino != ROOTINO) {
  354. struct inode* pdp = unixfs_internal_iget(dp->I_ino);
  355. if (pdp) {
  356. dent->ino = pdp->I_ino;
  357. unixfs_internal_iput(pdp);
  358. }
  359. }
  360. dent->name[idx++] = '.';
  361. }
  362. dent->name[idx++] = '\0';
  363. goto out;
  364. }
  365. struct tap_node_info* child =
  366. ((struct tap_node_info*)dp->I_private)->ti_children;;
  367. off_t i;
  368. for (i = 0; i < (*offset - 2); i++)
  369. child = child->ti_next_sibling;
  370. dent->ino = (ino_t)child->ti_self->I_ino;
  371. size_t dirnamelen = min(DIRSIZ, UNIXFS_MAXNAMLEN);
  372. memcpy(dent->name, child->ti_name, dirnamelen);
  373. dent->name[dirnamelen] = '\0';
  374. out:
  375. *offset += 1;
  376. return 0;
  377. }
  378. static ssize_t
  379. unixfs_internal_pbread(struct inode* ip, char* buf, size_t nbyte, off_t offset,
  380. int* error)
  381. {
  382. ssize_t done = 0;
  383. size_t tomove = 0;
  384. ssize_t remaining = nbyte;
  385. ssize_t iosize = UNIXFS_IOSIZE(unixfs);
  386. char blkbuf[iosize];
  387. char* p = buf;
  388. while (remaining > 0) {
  389. off_t lbn = offset / BSIZE;
  390. off_t bn = unixfs_internal_bmap(ip, lbn, error);
  391. if (UNIXFS_BADBLOCK(bn, *error))
  392. break;
  393. *error = unixfs_internal_bread(bn, blkbuf);
  394. if (*error != 0)
  395. break;
  396. tomove = (remaining > iosize) ? iosize : remaining;
  397. memcpy(p, blkbuf, tomove);
  398. remaining -= tomove;
  399. done += tomove;
  400. offset += tomove;
  401. p += tomove;
  402. }
  403. if ((done == 0) && *error)
  404. return -1;
  405. return done;
  406. }
  407. static int
  408. unixfs_internal_readlink(ino_t ino, char path[UNIXFS_MAXPATHLEN])
  409. {
  410. return ENOSYS;
  411. }
  412. static int
  413. unixfs_internal_sanitycheck(void* filsys, off_t disksize)
  414. {
  415. return 0;
  416. }
  417. static int
  418. unixfs_internal_statvfs(struct statvfs* svb)
  419. {
  420. memcpy(svb, &unixfs->s_statvfs, sizeof(struct statvfs));
  421. return 0;
  422. }