PageRenderTime 45ms CodeModel.GetById 15ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

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