ufs.c
vfs/vfs.h
ufs/ufs.h
ufs/ffs.h
lib/kprintf.h
lib/kmalloc.h
ubixos/kpanic.h
lib/string.h
ufs/ufs.h
sys/thread.h
ubixos/kpanic.h
ufs/ffs.h
src/sys/ufs/ufs.c
ubixos/types.h
sys/_types.h
lib/string.h
vfs/file.h
lib/kmalloc.h
vfs/vfs.h
vfs/mount.h
lib/kprintf.h
ubixfs/dirCache.h
sys/sysproto.h
sys/signal.h
sys/device.h
DBPERVBLK
(VBLKSIZE / DEV_BSIZE)
fsread
DIP
field
fs
->fs_magic == FS_UFS1_MAGIC ? dp1.field : dp2.field
fsread
INDIRPERVBLK
fs
(NINDIR(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
fsread
INO_TO_VBA
fs
ipervblk
x
(fsbtodb(fs, cgimin(fs, ino_to_cg(fs, x))) + \ (((x) % (fs)->fs_ipg) / (ipervblk) * DBPERVBLK))
fsread
INO_TO_VBO
ipervblk
x
((x) % ipervblk)
fsread
INOPB
fs
((fs)->fs_inopb)
IPERVBLK
fs
(INOPB(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
fsread
VBLKMASK
(VBLKSIZE - 1)
fsread
VBLKSHIFT
12
VBLKSIZE
(1 << VBLKSHIFT)
int static int dskread (void *buf, u_int64_t block, size_t count, fileDescriptor *fd) dskread void * buf u_int64_t block size_t count fileDescriptor * fd vfs_mountPoint::device device_node::devInfo device_interface::info fileDescriptorStruct::mp device_interface::read fsread
__inline int static __inline int fsfind (const char *name, ino_t *ino, fileDescriptor *fd) fsfind const char * name ino_t * ino fileDescriptor * fd dirent::d_fileno dirent::d_name dirent::d_reclen dirent::d_type DEV_BSIZE fsread fileDescriptorStruct::offset strcmp lookup
ssize_t static ssize_t fsread (ino_t inode, void *buf, size_t nbyte, fileDescriptor *fd) fsread ino_t inode void * buf size_t nbyte fileDescriptor * fd dmadat::blkbuf blkoff DBPERVBLK DEV_BSHIFT DEV_BSIZE ufs2_dinode::di_db ufs2_dinode::di_ib ufs2_dinode::di_size DIP fileDescriptorStruct::dmadat fileDescriptorStruct::dsk_meta dskread fs::fs_magic FS_UFS1_MAGIC FS_UFS2_MAGIC fsbtodb dmadat::indbuf INDIRPERVBLK INO_TO_VBA INO_TO_VBO IPERVBLK kprintf lblkno MAXBSIZE memcpy NDADDR NINDIR fileDescriptorStruct::offset dmadat::sbbuf sblksize sblock_try fileDescriptorStruct::size VBLKMASK VBLKSHIFT VBLKSIZE fsfind ufs_openFile ufs_readFile
ino_t static ino_t lookup (const char *path, fileDescriptor *fd) lookup const char * path fileDescriptor * fd DT_DIR DT_REG fsfind kprintf MAXNAMLEN memcpy name ROOTINO ufs_openFile
int int ufs_init () ufs_init kpanic NULL ufs_initialize ufs_openFile ufs_readFile ufs_writeFile vfsRegisterFS x1
int int ufs_initialize (struct vfs_mountPoint *mp) ufs_initialize struct vfs_mountPoint * mp x1
int static int ufs_openFile (const char *file, fileDescriptor *fd) ufs_openFile const char * file fileDescriptor * fd fileDescriptorStruct::dmadat fsread fileDescriptorStruct::ino kmalloc lookup fileDescriptorStruct::offset fileDescriptorStruct::perms x1 ufs_init
int int ufs_readFile (fileDescriptor *fd, char *data, uInt32 offset, long size) ufs_readFile fileDescriptor * fd char * data uInt32 offset long size fsread fileDescriptorStruct::ino ufs_init
int int ufs_writeFile (fileDescriptor *fd, char *data, uInt32 offset, long size) ufs_writeFile fileDescriptor * fd char * data uInt32 offset long size kprintf ufs_init
int int sblock_try[] [] sblock_try SBLOCKSEARCH fsread

    /*****************************************************************************************
    Copyright(c)2002-2004TheUbixOSProject
    Allrightsreserved.

    Redistributionanduseinsourceandbinaryforms,withorwithoutmodification,are
    permittedprovidedthatthefollowingconditionsaremet:

    Redistributionsofsourcecodemustretaintheabovecopyrightnotice,thislistof
    conditions,thefollowingdisclaimerandthelistofauthors.Redistributionsinbinary
    formmustreproducetheabovecopyrightnotice,thislistofconditions,thefollowing
    disclaimerandthelistofauthorsinthedocumentationand/orothermaterialsprovided
    withthedistribution.NeitherthenameoftheUbixOSProjectnorthenamesofits
    contributorsmaybeusedtoendorseorpromoteproductsderivedfromthissoftware
    withoutspecificpriorwrittenpermission.

    THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS"ASIS"ANDANY
    EXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THEIMPLIEDWARRANTIESOF
    MERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALL
    THECOPYRIGHTOWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,
    SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,PROCUREMENT
    OFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,DATA,ORPROFITS;ORBUSINESSINTERRUPTION)
    HOWEVERCAUSEDANDONANYTHEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,OR
    TORT(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSEOFTHIS
    SOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.

    $Id: ufs_8c.xml 88 2016-01-12 00:11:29Z reddawg $

    *****************************************************************************************/

    #include<vfs/vfs.h>
    #include<ufs/ufs.h>
    #include<ufs/ffs.h>
    #include<lib/kprintf.h>
    #include<lib/kmalloc.h>
    #include<ubixos/kpanic.h>
    #include<lib/string.h>

    #defineVBLKSHIFT12
    #defineVBLKSIZE(1<<VBLKSHIFT)
    #defineVBLKMASK(VBLKSIZE-1)
    #defineDBPERVBLK(VBLKSIZE/DEV_BSIZE)
    #defineINDIRPERVBLK(fs)(NINDIR(fs)/((fs)->fs_bsize>>VBLKSHIFT))
    #defineIPERVBLK(fs)(INOPB(fs)/((fs)->fs_bsize>>VBLKSHIFT))
    #defineINOPB(fs)((fs)->fs_inopb)
    #defineINO_TO_VBA(fs,ipervblk,x)\
    (fsbtodb(fs,cgimin(fs,ino_to_cg(fs,x)))+\
    (((x)%(fs)->fs_ipg)/(ipervblk)*DBPERVBLK))
    #defineINO_TO_VBO(ipervblk,x)((x)%ipervblk)

    staticintdskread(void*buf,u_int64_tblock,size_tcount,fileDescriptor*fd){
    fd->mp->device->devInfo->read(fd->mp->device->devInfo->info,buf,block,count);
    return(0x0);
    }

    //structdmadat{
    //charblkbuf[VBLKSIZE];/*filesystemblocks*/
    //charindbuf[VBLKSIZE];/*indirblocks*/
    //charsbbuf[SBLOCKSIZE];/*superblock*/
    //charsecbuf[DEV_BSIZE];/*forMBR/disklabel*/
    //};
    //staticstructdmadat*dmadat;

    //staticintls,dsk_meta;

    staticintsblock_try[]=SBLOCKSEARCH;

    #ifdefined(UFS2_ONLY)
    #defineDIP(field)dp2.field
    #elifdefined(UFS1_ONLY)
    #defineDIP(field)dp1.field
    #else
    #defineDIP(field)fs->fs_magic==FS_UFS1_MAGIC?dp1.field:dp2.field
    #endif

    staticssize_tfsread(ino_tinode,void*buf,size_tnbyte,fileDescriptor*fd){
    #ifndefUFS2_ONLY
    staticstructufs1_dinodedp1;
    #endif
    #ifndefUFS1_ONLY
    staticstructufs2_dinodedp2;
    #endif
    staticino_tinomap;
    char*blkbuf;
    void*indbuf;
    structfs*fs;
    char*s;
    size_tn,nb,size,off,vboff;
    ufs_lbn_tlbn;
    ufs2_daddr_taddr,vbaddr;
    staticufs2_daddr_tblkmap,indmap;
    u_intu;

    blkbuf=fd->dmadat->blkbuf;
    indbuf=fd->dmadat->indbuf;
    fs=(structfs*)fd->dmadat->sbbuf;

    if(!fd->dsk_meta){
    inomap=0;
    for(n=0;sblock_try[n]!=-1;n++){
    if(dskread(fs,sblock_try[n]/DEV_BSIZE,16,fd))
    return-1;
    if((
    #ifdefined(UFS1_ONLY)
    fs->fs_magic==FS_UFS1_MAGIC
    #elifdefined(UFS2_ONLY)
    (fs->fs_magic==FS_UFS2_MAGIC&&
    fs->fs_sblockloc==sblock_try[n])
    #else
    fs->fs_magic==FS_UFS1_MAGIC||
    (fs->fs_magic==FS_UFS2_MAGIC&&
    fs->fs_sblockloc==sblock_try[n])
    #endif
    )&&
    fs->fs_bsize<=MAXBSIZE&&
    fs->fs_bsize>=sizeof(structfs))
    break;
    }
    if(sblock_try[n]==-1){
    kprintf("Notufs\n");
    return-1;
    }
    fd->dsk_meta++;
    }

    if(!inode)
    return(0x0);

    if(inomap!=inode){
    n=IPERVBLK(fs);
    if(dskread(blkbuf,INO_TO_VBA(fs,n,inode),DBPERVBLK,fd))
    return-1;
    n=INO_TO_VBO(n,inode);
    #ifdefined(UFS1_ONLY)
    dp1=((structufs1_dinode*)blkbuf)[n];
    #elifdefined(UFS2_ONLY)
    dp2=((structufs2_dinode*)blkbuf)[n];
    #else
    if(fs->fs_magic==FS_UFS1_MAGIC)
    dp1=((structufs1_dinode*)blkbuf)[n];
    else
    dp2=((structufs2_dinode*)blkbuf)[n];
    #endif
    inomap=inode;
    fd->offset=0;
    blkmap=indmap=0;
    }

    s=buf;
    size=DIP(di_size);
    fd->size=size;
    n=size-fd->offset;
    //Why?
    if(n<0)
    return(0x0);
    if(nbyte>n)
    nbyte=n;
    nb=nbyte;
    while(nb){
    lbn=lblkno(fs,fd->offset);
    off=blkoff(fs,fd->offset);
    if(lbn<NDADDR){
    addr=DIP(di_db[lbn]);
    }elseif(lbn<NDADDR+NINDIR(fs)){
    n=INDIRPERVBLK(fs);
    addr=DIP(di_ib[0]);
    u=(u_int)(lbn-NDADDR)/(n*DBPERVBLK);
    vbaddr=fsbtodb(fs,addr)+u;
    if(indmap!=vbaddr){
    if(dskread(indbuf,vbaddr,DBPERVBLK,fd))
    return-1;
    indmap=vbaddr;
    }
    n=(lbn-NDADDR)&(n-1);
    #ifdefined(UFS1_ONLY)
    addr=((ufs1_daddr_t*)indbuf)[n];
    #elifdefined(UFS2_ONLY)
    addr=((ufs2_daddr_t*)indbuf)[n];
    #else
    if(fs->fs_magic==FS_UFS1_MAGIC)
    addr=((ufs1_daddr_t*)indbuf)[n];
    else
    addr=((ufs2_daddr_t*)indbuf)[n];
    #endif
    }else{
    return-1;
    }
    vbaddr=fsbtodb(fs,addr)+(off>>VBLKSHIFT)*DBPERVBLK;
    vboff=off&VBLKMASK;
    n=sblksize(fs,size,lbn)-(off&~VBLKMASK);
    if(n>VBLKSIZE)
    n=VBLKSIZE;
    if(blkmap!=vbaddr){
    if(dskread(blkbuf,vbaddr,n>>DEV_BSHIFT,fd))
    return-1;
    blkmap=vbaddr;
    }
    n-=vboff;
    if(n>nb)
    n=nb;
    memcpy(s,blkbuf+vboff,n);
    s+=n;
    fd->offset+=n;
    nb-=n;
    }
    returnnbyte;
    }

    static__inlineintfsfind(constchar*name,ino_t*ino,fileDescriptor*fd){
    charbuf[DEV_BSIZE];
    structdirent*d;
    char*s;
    ssize_tn;

    fd->offset=0;
    while((n=fsread(*ino,buf,DEV_BSIZE,fd))>0)
    for(s=buf;s<buf+DEV_BSIZE;){
    d=(void*)s;
    if(!strcmp(name,d->d_name)){
    *ino=d->d_fileno;
    returnd->d_type;
    }
    s+=d->d_reclen;
    }
    //if(n!=-1&&ls)
    //kprintf("\n");
    return0;
    }

    staticino_tlookup(constchar*path,fileDescriptor*fd){
    charname[MAXNAMLEN+1];
    constchar*s;
    ino_tino;
    ssize_tn;
    intdt;

    ino=ROOTINO;
    dt=DT_DIR;
    name[0]='/';
    name[1]='\0';
    for(;;){
    if(*path=='/')
    path++;
    if(!*path)
    break;
    for(s=path;*s&&*s!='/';s++);
    if((n=s-path)>MAXNAMLEN)
    return0;
    //ls=*path=='?'&&n==1&&!*s;
    memcpy(name,path,n);
    name[n]=0;
    if(dt!=DT_DIR){
    kprintf("%s:notadirectory.\n",name);
    return(0);
    }
    if((dt=fsfind(name,&ino,fd))<=0)
    break;
    path=s;
    }

    returndt==DT_REG?ino:0;
    }

    staticintufs_openFile(constchar*file,fileDescriptor*fd){
    chartmp[2];
    intino=0;
    fd->dmadat=(structdmadat*)kmalloc(sizeof(structdmadat));
    ino=lookup(file,fd);
    fd->offset=0x0;
    fd->ino=ino;
    if(ino==0x0){
    return(-1);
    }

    /*QuickHackforfilesize*/
    fsread(fd->ino,&tmp,1,fd);
    fd->offset=0;
    /*Return*/
    fd->perms=0x1;
    return(0x1);
    }

    intufs_readFile(fileDescriptor*fd,char*data,uInt32offset,longsize){
    return(fsread(fd->ino,data,size,fd));
    }

    intufs_writeFile(fileDescriptor*fd,char*data,uInt32offset,longsize){
    kprintf("Writing:)\n");
    return(0x0);
    }

    /*****************************************************************************************

    Function:intufs_initialize()

    Description:ThiswillinitializeamountpointitloadstheBATandCachestherootDir

    Notes:

    *****************************************************************************************/
    intufs_initialize(structvfs_mountPoint*mp){
    /*Return*/
    return(0x1);
    }

    intufs_init(){
    /*Buildourufsstruct*/
    structfileSystemufs=
    {NULL,/*prev*/
    NULL,/*next*/
    (void*)ufs_initialize,/*vfsInitFS*/
    (void*)ufs_readFile,/*vfsRead*/
    (void*)ufs_writeFile,/*vfsWrite*/
    (void*)ufs_openFile,/*vfsOpenFile*/
    NULL,/*vfsUnlink*/
    NULL,/*vfsMakeDir*/
    NULL,/*vfsRemDir*/
    NULL,/*vfsSync*/
    0xAA,/*vfsType*/
    };/*UFS*/

    if(vfsRegisterFS(ufs)!=0x0){
    kpanic("UnableToEnableUFS");
    return(0x1);
    }
    //dmadat=(structdmadat*)kmalloc(sizeof(structdmadat));
    /*Return*/
    return(0x0);
    }

    /***
    END
    ***/