PageRenderTime 48ms CodeModel.GetById 9ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/filesystems/unixfs/ancientfs/ancientfs_2.9bsd.c

http://macfuse.googlecode.com/
C | 599 lines | 457 code | 119 blank | 23 comment | 115 complexity | 02439c2f6d2a3d40dbcf37b11d99590b MD5 | raw file
  1/*
  2 * Ancient UNIX File Systems for MacFUSE
  3 * Amit Singh
  4 * http://osxbook.com
  5 */
  6
  7#include "ancientfs_2.9bsd.h"
  8#include "unixfs_common.h"
  9
 10#include <errno.h>
 11#include <fcntl.h>
 12#include <stdio.h>
 13#include <stdlib.h>
 14#include <string.h>
 15#include <unistd.h>
 16#include <sys/ioctl.h>
 17#include <sys/stat.h>
 18
 19DECL_UNIXFS("2.9BSD", 29bsd);
 20
 21static void*
 22unixfs_internal_init(const char* dmg, uint32_t flags, fs_endian_t fse,
 23                     char** fsname, char** volname)
 24{
 25    int fd = -1;
 26    if ((fd = open(dmg, O_RDONLY)) < 0) {
 27        perror("open");
 28        return NULL;
 29    }
 30
 31    int err, i;
 32    struct stat stbuf;
 33    struct super_block* sb = (struct super_block*)0;
 34    struct filsys* fs = (struct filsys*)0;
 35
 36    if ((err = fstat(fd, &stbuf)) != 0) {
 37        perror("fstat");
 38        goto out;
 39    }
 40
 41    if (!S_ISREG(stbuf.st_mode) && !(flags & UNIXFS_FORCE)) {
 42        err = EINVAL;
 43        fprintf(stderr, "%s is not a disk image file\n", dmg);
 44        goto out;
 45    }
 46
 47    sb = malloc(sizeof(struct super_block));
 48    if (!sb) {
 49        err = ENOMEM;
 50        goto out;
 51    }
 52
 53    assert(sizeof(struct filsys) <= BSIZE);
 54
 55    fs = malloc(BSIZE);
 56    if (!fs) {
 57        free(sb);
 58        err = ENOMEM;
 59        goto out;
 60    }
 61
 62    if (pread(fd, fs, BSIZE, (off_t)BSIZE) != BSIZE) {
 63        perror("pread");
 64        err = EIO;
 65        goto out;
 66    }
 67
 68    unixfs = sb;
 69
 70    unixfs->s_flags = flags;
 71    unixfs->s_endian = (fse == UNIXFS_FS_INVALID) ? UNIXFS_FS_PDP : fse;
 72    unixfs->s_fs_info = (void*)fs;
 73    unixfs->s_bdev = fd;
 74
 75    fs->s_isize = fs16_to_host(unixfs->s_endian, fs->s_isize);
 76    fs->s_fsize = fs32_to_host(unixfs->s_endian, fs->s_fsize);
 77    fs->s_nfree = fs16_to_host(unixfs->s_endian, fs->s_nfree);
 78
 79    for (i = 0; i < NICFREE; i++)
 80        fs->s_free[i] = fs32_to_host(unixfs->s_endian, fs->s_free[i]);
 81    fs->s_ninode = fs16_to_host(unixfs->s_endian, fs->s_ninode);
 82    for (i = 0; i < NICINOD; i++)
 83        fs->s_inode[i] = fs16_to_host(unixfs->s_endian, fs->s_inode[i]);
 84    fs->s_time = fs32_to_host(unixfs->s_endian, fs->s_time);
 85
 86    unixfs->s_statvfs.f_bsize = BSIZE;
 87    unixfs->s_statvfs.f_frsize = BSIZE;
 88
 89    /* must initialize the inode layer before sanity checking */
 90    if ((err = unixfs_inodelayer_init(0)) != 0)
 91        goto out;
 92
 93    if (unixfs_internal_sanitycheck(fs, stbuf.st_size) != 0) {
 94        if (!(flags & UNIXFS_FORCE)) {
 95            fprintf(stderr,
 96              "retry with the --force option to see if it will work anyway\n");
 97            err = EINVAL;
 98            goto out;
 99        }
100    }
101
102    int iblock;
103    unixfs->s_statvfs.f_files = 0;
104    unixfs->s_statvfs.f_ffree = 0;
105
106    char* ubuf = malloc(UNIXFS_IOSIZE(unixfs));
107    if (!ubuf) {
108        err = ENOMEM;
109        goto out;
110    }
111
112    for (iblock = 2; iblock < fs->s_isize; iblock++) {
113        if (unixfs_internal_bread((off_t)iblock, ubuf) != 0)
114            continue;
115        struct dinode* dip = (struct dinode*)ubuf;
116        for (i = 0; i < INOPB; i++, dip++) {
117            if (fs16_to_host(unixfs->s_endian, dip->di_nlink) == 0)
118                unixfs->s_statvfs.f_ffree++;
119            else
120                unixfs->s_statvfs.f_files++;
121        }
122    }
123
124    free(ubuf);
125
126    unixfs->s_statvfs.f_blocks = fs->s_fsize;
127    unixfs->s_statvfs.f_bfree = 0;
128
129    while (unixfs_internal_alloc())
130        unixfs->s_statvfs.f_bfree++;
131
132    unixfs->s_statvfs.f_bavail = unixfs->s_statvfs.f_bfree;
133    unixfs->s_dentsize = DIRSIZ + 2;
134    unixfs->s_statvfs.f_namemax = DIRSIZ;
135
136    snprintf(unixfs->s_fsname, UNIXFS_MNAMELEN, "%s", unixfs_fstype);
137
138    char* dmg_basename = basename((char*)dmg);
139    snprintf(unixfs->s_volname, UNIXFS_MAXNAMLEN, "%s (disk=%s)",
140             unixfs_fstype, (dmg_basename) ? dmg_basename : "Disk Image");
141
142    *fsname = unixfs->s_fsname;
143    *volname = unixfs->s_volname;
144
145out:
146    if (err) {
147        if (fd >= 0)
148            close(fd);
149        if (fs)
150            free(fs);
151        if (sb)
152            free(sb);
153        return NULL;
154    }
155
156    return sb;
157}
158
159static void
160unixfs_internal_fini(void* filsys)
161{
162    unixfs_inodelayer_fini();
163    struct super_block* sb = (struct super_block*)filsys;
164    if (sb) {
165        if (sb->s_bdev >= 0)
166            close(sb->s_bdev);
167        sb->s_bdev = -1;
168        if (sb->s_fs_info)
169            free(sb->s_fs_info);
170    }
171}
172
173static off_t
174unixfs_internal_alloc(void)
175{
176    struct filsys* fs = (struct filsys*)unixfs->s_fs_info;
177
178    a_int i = --fs->s_nfree;
179    if (i < 0)
180        goto nospace;
181
182    if (i > NICFREE) /* bad free count */
183        return (off_t)0;
184
185    a_daddr_t bno = fs->s_free[i];
186    if (bno == 0)
187        return (off_t)0;
188
189    if (bno < fs->s_isize || bno >= fs->s_fsize)
190        return (off_t)0; /* bad free block <bno> */
191
192    if (fs->s_nfree <= 0) {
193        char ubuf[UNIXFS_IOSIZE(unixfs)];
194        int ret = unixfs_internal_bread((off_t)bno, ubuf);
195        if (ret == 0) {
196            struct fblk* fblk = (struct fblk*)ubuf;
197            fs->s_nfree = fs16_to_host(unixfs->s_endian, fblk->df_nfree);
198            for (i = 0; i < NICFREE; i++)
199                fs->s_free[i] = fs32_to_host(unixfs->s_endian, fblk->df_free[i]);
200        } else
201            return (off_t)0;
202    }
203
204    return (off_t)bno;
205
206nospace:
207    fs->s_nfree = 0;
208    return (off_t)0; /* ENOSPC */
209}
210
211static off_t
212unixfs_internal_bmap(struct inode* ip, off_t lblkno, int* error)
213{
214    a_daddr_t bn = (a_daddr_t)lblkno;
215
216    if (bn < 0) {
217        *error = EFBIG;
218        return (off_t)0;
219    }
220
221    *error = EROFS;
222
223    /*
224     * blocks 0..NADDR-4 are direct blocks
225     */
226
227    int i;
228    a_daddr_t nb;
229
230    if (bn < (NADDR - 3)) {
231        i = bn;
232        nb = ip->I_daddr[i];
233        if (nb == 0)
234            return (off_t)0; /* !writable; should be -1 rather */
235        *error = 0;
236        return (off_t)nb;
237    }
238
239    /*
240     * addresses NADDR - 3, NADDR - 2, and NADDR - 1 have single, double,
241     * and triple indirect blocks. the first step is to determine how many
242     * levels of indirection.
243     */
244
245    int j, sh = 0;
246    nb = 1;
247    bn -= NADDR - 3;
248    for (j = 3; j > 0; j--) { 
249        sh += NSHIFT;
250        nb <<= NSHIFT;
251        if (bn < nb)
252            break;
253        bn -= nb;
254    }
255    if (j == 0) {
256        *error = EFBIG;
257        return (off_t)0;
258    }
259
260    /*
261     * fetch the address from the inode
262     */
263
264    nb = ip->I_daddr[NADDR - j];
265    if (nb == 0)
266        return (off_t)0; /* !writable; should be -1 rather */
267
268    /*
269     * fetch through the indirect blocks
270     */
271
272    for (; j <= 3; j++) {
273        char ubuf[UNIXFS_IOSIZE(unixfs)];
274        int ret = unixfs_internal_bread((off_t)nb, ubuf);
275        if (ret) {
276            *error = ret;
277            return (off_t)0;
278        }
279        a_daddr_t* bap = (a_daddr_t*)ubuf;
280        sh -= NSHIFT;
281        i = (bn >> sh) & NMASK;
282        nb = fs32_to_host(unixfs->s_endian, bap[i]);
283        if (nb == 0)
284            return (off_t)0; /* !writable; should be -1 rather */
285    }
286
287    /* calculate read-ahead here. */
288
289    *error = 0;
290
291    return (off_t)nb;
292}
293
294static int
295unixfs_internal_bread(off_t blkno, char* blkbuf)
296{
297    if (blkno >= ((struct filsys*)unixfs->s_fs_info)->s_fsize) {
298        fprintf(stderr,
299                "***fatal error: bread failed for block %llu\n", blkno);
300        abort();
301        /* NOTREACHED */
302    }
303
304    if (blkno == 0) { /* zero fill */
305        memset(blkbuf, 0, UNIXFS_IOSIZE(unixfs));
306        return 0;
307    }
308
309    if (pread(unixfs->s_bdev, blkbuf, UNIXFS_IOSIZE(unixfs),
310              blkno * (off_t)BSIZE) != UNIXFS_IOSIZE(unixfs))
311        return EIO;
312
313    return 0;
314}
315
316static struct inode*
317unixfs_internal_iget(ino_t ino)
318{
319    if (ino == MACFUSE_ROOTINO)
320        ino = ROOTINO;
321
322    struct inode* ip = unixfs_inodelayer_iget(ino);
323    if (!ip) {
324        fprintf(stderr, "*** fatal error: no inode for %llu\n", (ino64_t)ino);
325        abort();
326    }
327
328    if (ip->I_initialized)
329        return ip;
330
331    char ubuf[UNIXFS_IOSIZE(unixfs)];
332
333    if (unixfs_internal_bread((off_t)itod((a_ino_t)ino), ubuf) != 0) {
334        unixfs_inodelayer_ifailed(ip);
335        return NULL;
336    }
337
338    struct dinode* dip = (struct dinode*)ubuf;
339    dip += itoo((a_ino_t)ino);
340
341    ip->I_number = ino;
342
343    ip->I_mode  = fs16_to_host(unixfs->s_endian, dip->di_mode);
344    ip->I_nlink = fs16_to_host(unixfs->s_endian, dip->di_nlink);
345    ip->I_uid   = fs16_to_host(unixfs->s_endian, dip->di_uid);
346    ip->I_gid   = fs16_to_host(unixfs->s_endian, dip->di_gid);
347    ip->I_size  = fs32_to_host(unixfs->s_endian, dip->di_size);
348
349    ip->I_atime_sec = fs32_to_host(unixfs->s_endian, dip->di_atime);
350    ip->I_mtime_sec = fs32_to_host(unixfs->s_endian, dip->di_mtime);
351    ip->I_ctime_sec = fs32_to_host(unixfs->s_endian, dip->di_ctime);
352
353    int i;
354
355    char* p1 = (char*)(ip->I_daddr);
356    char* p2 = (char*)(dip->di_addr);
357
358    for (i = 0; i < NADDR; i++) {
359        *p1++ = *p2++;
360        *p1++ = 0;
361        *p1++ = *p2++;
362        *p1++ = *p2++;
363    }
364
365    for (i = 0; i < NADDR; i++)
366        ip->I_daddr[i] = fs32_to_host(unixfs->s_endian, ip->I_daddr[i]);
367
368    if (S_ISCHR(ip->I_mode) || S_ISBLK(ip->I_mode)) {
369        uint32_t rdev = ip->I_daddr[0];
370        ip->I_rdev = makedev((rdev >> 8) & 255, rdev & 255);
371    }
372
373    unixfs_inodelayer_isucceeded(ip);
374
375    return ip;
376}
377
378static void
379unixfs_internal_iput(struct inode* ip)
380{
381    unixfs_inodelayer_iput(ip);
382}
383
384static int
385unixfs_internal_igetattr(ino_t ino, struct stat* stbuf)
386{
387    if (ino == MACFUSE_ROOTINO)
388        ino = ROOTINO;
389
390    struct inode* ip = unixfs_internal_iget(ino);
391    if (!ip)
392        return ENOENT;
393
394    unixfs_internal_istat(ip, stbuf);
395
396    unixfs_internal_iput(ip);
397
398    return 0;
399}
400
401static void
402unixfs_internal_istat(struct inode* ip, struct stat* stbuf)
403{
404    memcpy(stbuf, &ip->I_stat, sizeof(struct stat));
405}
406
407static int
408unixfs_internal_namei(ino_t parentino, const char* name, struct stat* stbuf)
409{
410    if (parentino == MACFUSE_ROOTINO)
411        parentino = ROOTINO;
412
413    stbuf->st_ino = 0;
414
415    struct inode* dp = unixfs_internal_iget(parentino);
416    if (!dp)
417        return ENOENT;
418
419    if (!S_ISDIR(dp->I_mode)) {
420        unixfs_internal_iput(dp);
421        return ENOTDIR;
422    }
423
424    int ret = ENOENT, eo = 0, count = dp->I_size / unixfs->s_dentsize;
425    a_int offset = 0;
426    char ubuf[UNIXFS_IOSIZE(unixfs)];
427    struct dent udent;
428
429eloop:
430    if (count == 0) {
431        ret = ENOENT;
432        goto out;
433    }
434
435    if ((offset & BMASK) == 0) {
436        off_t blkno = unixfs_internal_bmap(dp, (off_t)(offset/BSIZE), &ret);
437        if (UNIXFS_BADBLOCK(blkno, ret))
438            goto out;
439        if (unixfs_internal_bread(blkno, ubuf) != 0)
440            goto out;
441    }
442
443    memset(&udent, 0, sizeof(udent));
444    memcpy(&udent, ubuf + (offset & BMASK), unixfs->s_dentsize);
445
446    udent.u_ino = fs16_to_host(unixfs->s_endian, udent.u_ino);
447
448    offset += unixfs->s_dentsize;
449    count--;
450
451    if (udent.u_ino == 0) {
452        if (eo == 0)
453            eo = offset;
454        goto eloop;
455    }
456
457    if (strncmp(name, udent.u_name, DIRSIZ) != 0)
458        goto eloop;
459
460    /* matched */
461
462    ret = unixfs_internal_igetattr((ino_t)(udent.u_ino), stbuf);
463
464out:
465    unixfs_internal_iput(dp);
466
467    return ret;
468}
469
470static int
471unixfs_internal_nextdirentry(struct inode* dp, struct unixfs_dirbuf* dirbuf,
472                             off_t* offset, struct unixfs_direntry* dent)
473{
474    if ((*offset + unixfs->s_dentsize) > dp->I_size)
475        return -1;
476
477    if (!dirbuf->flags.initialized || ((*offset & BMASK) == 0)) {
478        int ret;
479        off_t blkno = unixfs_internal_bmap(dp, (off_t)(*offset / BSIZE), &ret);
480        if (UNIXFS_BADBLOCK(blkno, ret))
481            return ret;
482        ret = unixfs_internal_bread(blkno, dirbuf->data);
483        if (ret != 0)
484            return ret;
485        dirbuf->flags.initialized = 1;
486    }
487
488    struct dent udent;
489    size_t dirnamelen = min(DIRSIZ, UNIXFS_MAXNAMLEN);
490
491    memset(&udent, 0, sizeof(udent));
492    memcpy(&udent, dirbuf->data + (*offset & BMASK), unixfs->s_dentsize);
493    udent.u_ino = fs16_to_host(unixfs->s_endian, udent.u_ino);
494    dent->ino = udent.u_ino;
495    memcpy(dent->name, udent.u_name, dirnamelen);
496    dent->name[dirnamelen] = '\0';
497
498    *offset += unixfs->s_dentsize;
499
500    return 0;
501}
502
503static ssize_t
504unixfs_internal_pbread(struct inode* ip, char* buf, size_t nbyte, off_t offset,
505                       int* error)
506{
507    ssize_t done = 0;
508    size_t tomove = 0;
509    ssize_t remaining = nbyte;
510    ssize_t iosize = UNIXFS_IOSIZE(unixfs);
511    char blkbuf[iosize];
512    char* p = buf;
513
514    while (remaining > 0) {
515        off_t lbn = offset / BSIZE;
516        off_t bn = unixfs_internal_bmap(ip, lbn, error);
517        if (UNIXFS_BADBLOCK(bn, *error))
518            break;
519        *error = unixfs_internal_bread(bn, blkbuf);
520        if (*error != 0)
521            break;
522        tomove = (remaining > iosize) ? iosize : remaining;
523        memcpy(p, blkbuf, tomove);
524        remaining -= tomove;
525        done += tomove;
526        offset += tomove;
527        p += tomove;
528    }
529
530    if ((done == 0) && *error)
531        return -1;
532
533    return done;
534}
535
536static int
537unixfs_internal_readlink(ino_t ino, char path[UNIXFS_MAXPATHLEN])
538{
539    return ENOSYS;
540}
541
542static int
543unixfs_internal_sanitycheck(void* filsys, off_t disksize)
544{
545    struct filsys* fs = (struct filsys*)filsys;
546
547    if ((off_t)(fs->s_fsize * BSIZE) > disksize) {
548        fprintf(stderr, "*** disk image seems smaller than the volume\n");
549        return -1;
550    }
551
552    if (fs->s_nfree > NICFREE) {
553        fprintf(stderr, "*** warning: implausible s_nfree %hu\n", fs->s_nfree);
554        return -1;
555    }
556
557    if (fs->s_ninode > NICINOD) {
558        fprintf(stderr,
559                "*** warning: implausible s_ninode %hu\n", fs->s_ninode);
560        return -1;
561    }
562
563    if (fs->s_time == 0) {
564        fprintf(stderr, "*** warning: implausible timestamp of 0\n");
565        return -1;
566    }
567
568    struct stat stbuf;
569    int ret = unixfs_internal_igetattr((ino_t)ROOTINO, &stbuf);
570    if (ret) {
571        fprintf(stderr, "*** warning: failed to get root inode\n");
572        return -1;
573    }
574
575    if (!S_ISDIR(stbuf.st_mode)) {
576        fprintf(stderr, "*** warning: root inode is not a directory\n");
577        return -1;
578    }
579
580    if (stbuf.st_size == 0) {
581        fprintf(stderr, "*** warning: root inode has zero size\n");
582        return -1;
583    }
584
585    if (stbuf.st_size % (off_t)sizeof(struct dent)) {
586        fprintf(stderr, "*** warning: root inode's size (%llu) is suspicious\n",
587                stbuf.st_size);
588        return -1;
589    }
590
591    return 0;
592}
593
594static int
595unixfs_internal_statvfs(struct statvfs* svb)
596{
597    memcpy(svb, &unixfs->s_statvfs, sizeof(struct statvfs));
598    return 0;
599}