/**************************************************************************************
 Copyright (c) 2002 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: mount.c,v 1.12 2003/05/04 13:20:15 reddawg Exp $

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

#include <ubixos/mount.h>
#include <ubixos/fs.h>
#include <ubixos/vitals.h>
#include <ubixos/syserr.h>
#include <lib/kmalloc.h>
#include <lib/string.h>
#include <sys/drives.h>

int mount(int driveId,int partition,int fsType,char *mountPoint,char *perms) {
  struct mountPoints *mp    = 0x0;
  struct driveDriver *drive = 0x0;

  //Allocate Memory For Mount Point
  mp = (struct mountPoints *)kmalloc(sizeof(struct mountPoints),-2);

  //Copy Mount Point Into Buffer
  sprintf(mp->mountPoint,mountPoint);
  
  //Set Pointer To Physical Drive
  drive = findDrive(driveId);

  //Set Up Mp Defaults
  mp->drive     = drive;
  mp->fs        = findFileSystem(fsType);
  mp->partition = partition;
  mp->perms     = *perms;
  if (mp->fs == 0x0) {
    sysErr(systemErr,"File System Type: %i Not Found\n",fsType);
    return(0x0);
    }
  //Add Mountpoint If It Fails Free And Return
  if (addMount(mp) == 0x0) {
    kfree(mp);
    return(0x0);
    }
  //Initialize The File System If It Fails Return
  if (mp->fs->initFileSystem(mp) == 0x0) {
    kfree(mp);
    return(0x0);
    }
  //Return
  return(0x1);
  }

int addMount(struct mountPoints *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(0x1);
  }

struct mountPoints *findMount(char *mountPoint) {
  struct mountPoints *tmpMp = 0x0;

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

