PageRenderTime 59ms CodeModel.GetById 15ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 0ms

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