/*****************************************************************************************
 Copyright (c) 2002-2004 The UbixOS Project
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, this list of
 conditions, the following disclaimer and the list of authors.  Redistributions in binary
 form must reproduce the above copyright notice, this list of conditions, the following
 disclaimer and the list of authors in the documentation and/or other materials provided
 with the distribution. Neither the name of the UbixOS Project nor the names of its
 contributors may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id$

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

#include <vfs/mount.h>
#include <ubixos/vitals.h>
#include <ubixos/kpanic.h>
#include <lib/kmalloc.h>
#include <lib/kprintf.h>
#include <lib/string.h>
#include <sys/device.h>

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

Function: mount(int driveId,int partition,int fsType,char *mountPoint,char *perms)

Description: mount adds a mount point and returns 0 if successful 1 if it fails

Notes:

************************************************************************/
int vfs_mount(int major,int minor,int partition,int vfsType,char *mountPoint,char *perms) {
  vfs_mountPoint_t *mp     = 0x0;
  struct device_node  *device = 0x0;

  /* Allocate Memory For Mount Point */
  if ((mp = (vfs_mountPoint_t *)kmalloc(sizeof(vfs_mountPoint_t))) == NULL)
    kprintf("vfs_mount: failed to allocate mp\n");

  /* Copy Mount Point Into Buffer */
  sprintf(mp->mountPoint,"%s", mountPoint);
  
  /* Set Pointer To Physical Drive */
  device = device_find(major,minor);

  /* Set Up Mp Defaults */
  mp->device    = device;
  mp->fs        = vfsFindFS(vfsType);
  mp->partition = partition;
  mp->perms     = *perms;

  if (mp->fs == 0x0) {
    /* sysErr(systemErr,"File System Type: %i Not Found\n",fsType); */
    kprintf("File System Type: %i Not Found\n",vfsType);
    return(0x1);
    }

  if (device != 0x0) {
    mp->diskLabel = (struct ubixDiskLabel *)kmalloc(512);
    mp->device->devInfo->read(mp->device->devInfo->info,mp->diskLabel,1,1);
    kprintf("READING SECTOR");
    }

  /* Add Mountpoint If It Fails Free And Return */
  if (vfs_addMount(mp) != 0x0) {
    kfree(mp);
    return(0x1);
    }

  /* Initialize The File System If It Fails Return */
  if (mp->fs->vfsInitFS(mp) == 0x0) {
    kfree(mp);
    return(0x1);
    }
  /* Return */
  return(0x0);
  }

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

Function: vfs_addMount(vfs_mountPoint_t *mp)

Description: This function adds a mount point to the system

Notes:

************************************************************************/
int vfs_addMount(vfs_mountPoint_t  *mp) {

  /* If There Are No Existing Mounts Make It The First */
  if (systemVitals->mountPoints == 0x0) {
    mp->prev               = 0x0;
    mp->next               = 0x0;
    systemVitals->mountPoints = mp;
    }
  else {
    mp->next                        = systemVitals->mountPoints;
    systemVitals->mountPoints->prev = mp;
    mp->prev                        = 0x0;
    systemVitals->mountPoints       = mp;
    }
  /* Return */
  return(0x0);
  }

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

Function: vfs_findMount(char *mountPoint)

Description: This function finds a particular mount point

Notes:

************************************************************************/
vfs_mountPoint_t *vfs_findMount(char *mountPoint) {
  vfs_mountPoint_t *tmpMp = 0x0;

  for (tmpMp=systemVitals->mountPoints;tmpMp;tmpMp=tmpMp->next) {
    if (strcmp(tmpMp->mountPoint,mountPoint) == 0x0) {
      return(tmpMp);
      }
    }
  /* Return NULL If Mount Point Not Found */
  return NULL;
  }

/***
 $Log$
 Revision 1.13  2005/08/04 20:35:19  fsdfs

 various updates. mostly kprints, tabbing code to look cleaner

 Revision 1.12  2005/08/04 18:24:40  fsdfs

 fixed problem in kfree() freeing a b uffer that is not used

 Revision 1.11  2004/07/27 12:02:01  reddawg
 chg: fixed marks bug readFile did a lookup which is why it looked like it was loopping so much

 Revision 1.10  2004/07/27 08:42:29  reddawg
 chg: vfs_mount now ensures that malloc did not fail

 Revision 1.9  2004/07/23 09:10:06  reddawg
 ubixfs: cleaned up some functions played with the caching a bit
 vfs:    renamed a bunch of functions
 cleaned up a few misc bugs

 Revision 1.8  2004/07/21 10:02:09  reddawg
 devfs: renamed functions
 device system: renamed functions
 fdc: fixed a few potential bugs and cleaned up some unused variables
 strol: fixed definition
 endtask: made it print out freepage debug info
 kmalloc: fixed a huge memory leak we had some unhandled descriptor insertion so some descriptors were lost
 ld: fixed a pointer conversion
 file: cleaned up a few unused variables
 sched: broke task deletion
 kprintf: fixed ogPrintf definition

 Revision 1.7  2004/06/28 23:12:58  reddawg
 file format now container:/path/to/file

 Revision 1.6  2004/05/19 15:22:50  reddawg
 Fixed reference issues due to changes in driver subsystem

 Revision 1.5  2004/05/19 04:07:43  reddawg
 kmalloc(size,pid) no more it is no kmalloc(size); the way it should of been

 Revision 1.4  2004/04/29 15:45:19  reddawg
 Fixed some bugs so now the automade images will work correctly

 Revision 1.3  2004/04/28 21:10:40  reddawg
 Lots Of changes to make it work with existing os

 Revision 1.2  2004/04/28 02:22:55  reddawg
 This is a fiarly large commit but we are starting to use new driver model
 all around

 Revision 1.1.1.1  2004/04/15 12:06:53  reddawg
 UbixOS v1.0

 Revision 1.4  2004/04/13 16:36:34  reddawg
 Changed our copyright, it is all now under a BSD-Style license

 END
 ***/

