/*-
* Copyright (c) 1998, 1999 Semen Ustimenko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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: ntfs_vfsops.c,v 1.7 1999/05/31 11:28:30 phk Exp
*/
if (args == NULL)
return EINVAL;
if (*data_len < sizeof *args)
return EINVAL;
if (mp->mnt_flag & MNT_GETARGS) {
struct ntfsmount *ntmp = VFSTONTFS(mp);
if (ntmp == NULL)
return EIO;
args->fspec = NULL;
args->uid = ntmp->ntm_uid;
args->gid = ntmp->ntm_gid;
args->mode = ntmp->ntm_mode;
args->flag = ntmp->ntm_flag;
*data_len = sizeof *args;
return 0;
}
/*
***
* Mounting non-root file system or updating a file system
***
*/
/*
* If updating, check whether changing from read-only to
* read/write; if there is no device name, that's all we do.
*/
if (mp->mnt_flag & MNT_UPDATE) {
printf("ntfs_mount(): MNT_UPDATE not supported\n");
return (EINVAL);
}
/*
* Not an update, or updating the name: look up the name
* and verify that it refers to a sensible block device.
*/
err = namei_simple_user(args->fspec,
NSM_FOLLOW_NOEMULROOT, &devvp);
if (err)
return (err);
/*
* Update device name only on success
*/
err = set_statvfs_info(NULL, UIO_USERSPACE, args->fspec,
UIO_USERSPACE, mp->mnt_op->vfs_name, mp, p);
if (err)
goto fail;
vrele(devvp);
#endif
} else {
/*
********************
* NEW MOUNT
********************
*/
/*
* Since this is a new mount, we want the names for
* the device and the mount point copied in. If an
* error occurs, the mountpoint is discarded by the
* upper level code.
*/
/* Save "last mounted on" info for mount point (NULL pad)*/
err = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
if (err)
goto fail;
/*
* Initialize FS stat information in mount struct; uses both
* mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
*
* This code is common to root and non-root mounts
*/
(void)VFS_STATVFS(mp, &mp->mnt_stat);
return (err);
fail:
vrele(devvp);
return (err);
}
static int
ntfs_superblock_validate(struct ntfsmount *ntmp)
{
/* Sanity checks. XXX: More checks are probably needed. */
if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
dprintf(("ntfs_superblock_validate: invalid boot block\n"));
return EINVAL;
}
if (ntmp->ntm_bps == 0) {
dprintf(("ntfs_superblock_validate: invalid bytes per sector\n"));
return EINVAL;
}
if (ntmp->ntm_spc == 0) {
dprintf(("ntfs_superblock_validate: invalid sectors per cluster\n"));
return EINVAL;
}
return 0;
}
/*
* Common code for mount and mountroot
*/
int
ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp, struct lwp *l)
{
struct buf *bp;
struct ntfsmount *ntmp;
dev_t dev = devvp->v_rdev;
int error, i;
struct vnode *vp;
struct vnode_iterator *marker;
ntmp = NULL;
/*
* Flush out any old buffers remaining from a previous use.
*/
vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0);
VOP_UNLOCK(devvp);
if (error)
return (error);
/* set file name encode/decode hooks XXX utf-8 only for now */
ntmp->ntm_wget = ntfs_utf8_wget;
ntmp->ntm_wput = ntfs_utf8_wput;
ntmp->ntm_wcmp = ntfs_utf8_wcmp;
/*
* We read in some system nodes to do not allow
* reclaim them and to have everytime access to them.
*/
{
int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
for (i = 0; i < 3; i++) {
error = VFS_VGET(mp, pi[i], LK_EXCLUSIVE,
&(ntmp->ntm_sysvn[pi[i]]));
if (error)
goto out1;
ntmp->ntm_sysvn[pi[i]]->v_vflag |= VV_SYSTEM;
vref(ntmp->ntm_sysvn[pi[i]]);
vput(ntmp->ntm_sysvn[pi[i]]);
}
}
/* read the Unicode lowercase --> uppercase translation table,
* if necessary */
if ((error = ntfs_toupper_use(mp, ntmp)))
goto out1;
/*
* Scan $BitMap and count free clusters
*/
error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
if (error)
goto out1;
/*
* Read and translate to internal format attribute
* definition file.
*/
{
int num,j;
struct attrdef ad;
/* Open $AttrDef */
error = VFS_VGET(mp, NTFS_ATTRDEFINO, LK_EXCLUSIVE, &vp);
if (error)
goto out1;
/* Count valid entries */
for (num = 0; ; num++) {
error = ntfs_readattr(ntmp, VTONT(vp),
NTFS_A_DATA, NULL,
num * sizeof(ad), sizeof(ad),
&ad, NULL);
if (error)
goto out1;
if (ad.ad_name[0] == 0)
break;
}
/* Alloc memory for attribute definitions */
ntmp->ntm_ad = (struct ntvattrdef *) malloc(
num * sizeof(struct ntvattrdef),
M_NTFSMNT, M_WAITOK);
ntmp->ntm_adnum = num;
/* Read them and translate */
for (i = 0; i < num; i++) {
error = ntfs_readattr(ntmp, VTONT(vp),
NTFS_A_DATA, NULL,
i * sizeof(ad), sizeof(ad),
&ad, NULL);
if (error)
goto out1;
j = 0;
do {
ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
} while(ad.ad_name[j++]);
ntmp->ntm_ad[i].ad_namelen = j - 1;
ntmp->ntm_ad[i].ad_type = ad.ad_type;
}
/* Check if only system vnodes are rest */
for (i = 0; i < NTFS_SYSNODESNUM; i++)
if ((ntmp->ntm_sysvn[i]) &&
(vrefcnt(ntmp->ntm_sysvn[i]) > 1))
return (EBUSY);
/* Dereference all system vnodes */
for (i = 0; i < NTFS_SYSNODESNUM; i++)
if (ntmp->ntm_sysvn[i])
vrele(ntmp->ntm_sysvn[i]);
/* vflush system vnodes */
error = vflush(mp, NULLVP, flags);
if (error) {
panic("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
}
/* Check if the type of device node isn't VBAD before
* touching v_specinfo. If the device vnode is revoked, the
* field is NULL and touching it causes null pointer derefercence.
*/
if (ntmp->ntm_devvp->v_type != VBAD)
spec_node_setmountedfs(ntmp->ntm_devvp, NULL);
dprintf(("ntfs_loadvnode: ino: %llu, attr: 0x%x:%s",
(unsigned long long)ntkey->k_ino,
ntkey->k_attrtype, ntkey->k_attrname));
ntmp = VFSTONTFS(mp);
/* Get ntnode */
error = ntfs_ntlookup(ntmp, ntkey->k_ino, &ip);
if (error) {
printf("ntfs_loadvnode: ntfs_ntget failed\n");
goto out;
}
/* It may be not initialized fully, so force load it */
if (!(ip->i_flag & IN_LOADED)) {
error = ntfs_loadntnode(ntmp, ip);
if (error) {
printf("ntfs_loadvnode: CAN'T LOAD ATTRIBUTES FOR INO:"
" %llu\n", (unsigned long long)ip->i_number);
ntfs_ntput(ip);
goto out;
}
}
static int
ntfs_modcmd(modcmd_t cmd, void *arg)
{
int error;
switch (cmd) {
case MODULE_CMD_INIT:
error = vfs_attach(&ntfs_vfsops);
if (error != 0)
break;
sysctl_createv(&ntfs_sysctl_log, 0, NULL, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "ntfs",
SYSCTL_DESCR("NTFS file system"),
NULL, 0, NULL, 0,
CTL_VFS, 20, CTL_EOL);
/*
* XXX the "20" above could be dynamic, thereby eliminating
* one more instance of the "number to vfs" mapping problem,
* but "20" is the order as taken from sys/mount.h
*/
break;
case MODULE_CMD_FINI:
error = vfs_detach(&ntfs_vfsops);
if (error != 0)
break;
sysctl_teardown(&ntfs_sysctl_log);
break;
default:
error = ENOTTY;
break;
}