Home | History | Annotate | Line # | Download | only in udf
udf_vfsops.c revision 1.29.4.3
      1  1.29.4.3      matt /* udf_vfsops.c,v 1.29.4.2 2008/01/09 01:55:54 matt Exp */
      2       1.1   reinoud 
      3       1.1   reinoud /*
      4       1.1   reinoud  * Copyright (c) 2006 Reinoud Zandijk
      5       1.1   reinoud  * All rights reserved.
      6       1.1   reinoud  *
      7       1.1   reinoud  * Redistribution and use in source and binary forms, with or without
      8       1.1   reinoud  * modification, are permitted provided that the following conditions
      9       1.1   reinoud  * are met:
     10       1.1   reinoud  * 1. Redistributions of source code must retain the above copyright
     11       1.1   reinoud  *    notice, this list of conditions and the following disclaimer.
     12       1.1   reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1   reinoud  *    notice, this list of conditions and the following disclaimer in the
     14       1.1   reinoud  *    documentation and/or other materials provided with the distribution.
     15       1.1   reinoud  * 3. All advertising materials mentioning features or use of this software
     16       1.1   reinoud  *    must display the following acknowledgement:
     17       1.1   reinoud  *          This product includes software developed for the
     18       1.1   reinoud  *          NetBSD Project.  See http://www.NetBSD.org/ for
     19       1.1   reinoud  *          information about NetBSD.
     20       1.1   reinoud  * 4. The name of the author may not be used to endorse or promote products
     21       1.1   reinoud  *    derived from this software without specific prior written permission.
     22       1.1   reinoud  *
     23       1.1   reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24       1.1   reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25       1.1   reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26       1.1   reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27       1.1   reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28       1.1   reinoud  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29       1.1   reinoud  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30       1.1   reinoud  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31       1.1   reinoud  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32       1.1   reinoud  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33       1.1   reinoud  *
     34       1.1   reinoud  */
     35       1.1   reinoud 
     36       1.1   reinoud 
     37       1.1   reinoud #include <sys/cdefs.h>
     38       1.1   reinoud #ifndef lint
     39  1.29.4.3      matt __KERNEL_RCSID(0, "udf_vfsops.c,v 1.29.4.2 2008/01/09 01:55:54 matt Exp");
     40       1.1   reinoud #endif /* not lint */
     41       1.1   reinoud 
     42       1.1   reinoud 
     43       1.1   reinoud #if defined(_KERNEL_OPT)
     44       1.1   reinoud #include "opt_quota.h"
     45       1.1   reinoud #include "opt_compat_netbsd.h"
     46       1.1   reinoud #endif
     47       1.1   reinoud 
     48       1.1   reinoud #include <sys/param.h>
     49       1.1   reinoud #include <sys/systm.h>
     50       1.1   reinoud #include <sys/sysctl.h>
     51       1.1   reinoud #include <sys/namei.h>
     52       1.1   reinoud #include <sys/proc.h>
     53       1.1   reinoud #include <sys/kernel.h>
     54       1.1   reinoud #include <sys/vnode.h>
     55  1.29.4.3      matt #include <miscfs/genfs/genfs.h>
     56       1.1   reinoud #include <miscfs/specfs/specdev.h>
     57       1.1   reinoud #include <sys/mount.h>
     58       1.1   reinoud #include <sys/buf.h>
     59       1.1   reinoud #include <sys/file.h>
     60       1.1   reinoud #include <sys/device.h>
     61       1.1   reinoud #include <sys/disklabel.h>
     62       1.1   reinoud #include <sys/ioctl.h>
     63       1.1   reinoud #include <sys/malloc.h>
     64       1.1   reinoud #include <sys/dirent.h>
     65       1.1   reinoud #include <sys/stat.h>
     66       1.1   reinoud #include <sys/conf.h>
     67       1.5  christos #include <sys/kauth.h>
     68       1.1   reinoud 
     69       1.1   reinoud #include <fs/udf/ecma167-udf.h>
     70       1.1   reinoud #include <fs/udf/udf_mount.h>
     71       1.1   reinoud 
     72       1.1   reinoud #include "udf.h"
     73       1.1   reinoud #include "udf_subr.h"
     74       1.1   reinoud #include "udf_bswap.h"
     75       1.1   reinoud 
     76       1.1   reinoud 
     77       1.1   reinoud /* verbose levels of the udf filingsystem */
     78       1.1   reinoud int udf_verbose = UDF_DEBUGGING;
     79       1.1   reinoud 
     80       1.1   reinoud /* malloc regions */
     81      1.25     pooka MALLOC_JUSTDEFINE(M_UDFMNT,   "UDF mount",	"UDF mount structures");
     82      1.25     pooka MALLOC_JUSTDEFINE(M_UDFVOLD,  "UDF volspace",	"UDF volume space descriptors");
     83      1.25     pooka MALLOC_JUSTDEFINE(M_UDFTEMP,  "UDF temp",	"UDF scrap space");
     84       1.1   reinoud struct pool udf_node_pool;
     85       1.1   reinoud 
     86       1.1   reinoud /* supported functions predefined */
     87      1.29     pooka VFS_PROTOS(udf);
     88       1.1   reinoud 
     89       1.1   reinoud 
     90       1.1   reinoud /* internal functions */
     91       1.1   reinoud static int udf_mountfs(struct vnode *, struct mount *, struct lwp *, struct udf_args *);
     92       1.1   reinoud #if 0
     93       1.1   reinoud static int update_mp(struct mount *, struct udf_args *);
     94       1.1   reinoud #endif
     95       1.1   reinoud 
     96       1.1   reinoud 
     97       1.1   reinoud /* handies */
     98       1.1   reinoud #define	VFSTOUDF(mp)	((struct udf_mount *)mp->mnt_data)
     99       1.1   reinoud 
    100       1.1   reinoud 
    101       1.1   reinoud /* --------------------------------------------------------------------- */
    102       1.1   reinoud 
    103       1.1   reinoud /* predefine vnode-op list descriptor */
    104       1.1   reinoud extern const struct vnodeopv_desc udf_vnodeop_opv_desc;
    105       1.1   reinoud 
    106       1.1   reinoud const struct vnodeopv_desc * const udf_vnodeopv_descs[] = {
    107       1.1   reinoud 	&udf_vnodeop_opv_desc,
    108       1.1   reinoud 	NULL,
    109       1.1   reinoud };
    110       1.1   reinoud 
    111       1.1   reinoud 
    112       1.1   reinoud /* vfsops descriptor linked in as anchor point for the filingsystem */
    113       1.1   reinoud struct vfsops udf_vfsops = {
    114       1.1   reinoud 	MOUNT_UDF,			/* vfs_name */
    115      1.26       dsl 	sizeof (struct udf_args),
    116       1.1   reinoud 	udf_mount,
    117       1.1   reinoud 	udf_start,
    118       1.1   reinoud 	udf_unmount,
    119       1.1   reinoud 	udf_root,
    120  1.29.4.2      matt 	(void *)eopnotsupp,		/* vfs_quotactl */
    121       1.1   reinoud 	udf_statvfs,
    122       1.1   reinoud 	udf_sync,
    123       1.1   reinoud 	udf_vget,
    124       1.1   reinoud 	udf_fhtovp,
    125       1.1   reinoud 	udf_vptofh,
    126       1.1   reinoud 	udf_init,
    127       1.1   reinoud 	udf_reinit,
    128       1.1   reinoud 	udf_done,
    129       1.1   reinoud 	udf_mountroot,
    130       1.1   reinoud 	udf_snapshot,
    131       1.1   reinoud 	vfs_stdextattrctl,
    132      1.28     pooka 	(void *)eopnotsupp,		/* vfs_suspendctl */
    133  1.29.4.3      matt 	genfs_renamelock_enter,
    134  1.29.4.3      matt 	genfs_renamelock_exit,
    135       1.1   reinoud 	udf_vnodeopv_descs,
    136      1.12  christos 	0, /* int vfs_refcount   */
    137      1.12  christos 	{ NULL, NULL, }, /* LIST_ENTRY(vfsops) */
    138       1.1   reinoud };
    139       1.1   reinoud VFS_ATTACH(udf_vfsops);
    140       1.1   reinoud 
    141       1.1   reinoud 
    142       1.1   reinoud /* --------------------------------------------------------------------- */
    143       1.1   reinoud 
    144       1.1   reinoud /* file system starts here */
    145       1.1   reinoud void
    146       1.1   reinoud udf_init(void)
    147       1.1   reinoud {
    148       1.1   reinoud 	size_t size;
    149       1.1   reinoud 
    150       1.1   reinoud 	malloc_type_attach(M_UDFMNT);
    151       1.1   reinoud 	malloc_type_attach(M_UDFVOLD);
    152       1.1   reinoud 	malloc_type_attach(M_UDFTEMP);
    153       1.1   reinoud 
    154       1.1   reinoud 	/* init hashtables and pools */
    155       1.1   reinoud 	size = sizeof(struct udf_node);
    156      1.23        ad 	pool_init(&udf_node_pool, size, 0, 0, 0, "udf_node_pool", NULL,
    157      1.23        ad 	    IPL_NONE);
    158       1.1   reinoud }
    159       1.1   reinoud 
    160       1.1   reinoud /* --------------------------------------------------------------------- */
    161       1.1   reinoud 
    162       1.1   reinoud void
    163       1.1   reinoud udf_reinit(void)
    164       1.1   reinoud {
    165       1.1   reinoud 	/* recreate hashtables */
    166       1.1   reinoud 	/* reinit pool? */
    167       1.1   reinoud }
    168       1.1   reinoud 
    169       1.1   reinoud /* --------------------------------------------------------------------- */
    170       1.1   reinoud 
    171       1.1   reinoud void
    172       1.1   reinoud udf_done(void)
    173       1.1   reinoud {
    174       1.1   reinoud 	/* remove hashtables and pools */
    175       1.1   reinoud 	pool_destroy(&udf_node_pool);
    176       1.1   reinoud 
    177       1.1   reinoud 	malloc_type_detach(M_UDFMNT);
    178       1.1   reinoud 	malloc_type_detach(M_UDFVOLD);
    179       1.1   reinoud 	malloc_type_detach(M_UDFTEMP);
    180       1.1   reinoud }
    181       1.1   reinoud 
    182       1.1   reinoud /* --------------------------------------------------------------------- */
    183       1.1   reinoud 
    184       1.1   reinoud int
    185       1.1   reinoud udf_mountroot(void)
    186       1.1   reinoud {
    187       1.1   reinoud 	return EOPNOTSUPP;
    188       1.1   reinoud }
    189       1.1   reinoud 
    190       1.1   reinoud /* --------------------------------------------------------------------- */
    191       1.1   reinoud 
    192       1.1   reinoud #define MPFREE(a, lst) \
    193       1.1   reinoud 	if ((a)) free((a), lst);
    194       1.1   reinoud static void
    195       1.1   reinoud free_udf_mountinfo(struct mount *mp)
    196       1.1   reinoud {
    197       1.1   reinoud 	struct udf_mount *ump;
    198       1.1   reinoud 	int i;
    199       1.1   reinoud 
    200      1.15   reinoud 	if (!mp)
    201      1.15   reinoud 		return;
    202      1.15   reinoud 
    203       1.1   reinoud 	ump = VFSTOUDF(mp);
    204       1.1   reinoud 	if (ump) {
    205      1.19   reinoud 		/* dereference all system nodes */
    206      1.19   reinoud 		if (ump->metadata_file)
    207      1.19   reinoud 			vrele(ump->metadata_file->vnode);
    208      1.19   reinoud 		if (ump->metadatamirror_file)
    209      1.19   reinoud 			vrele(ump->metadatamirror_file->vnode);
    210      1.19   reinoud 		if (ump->metadatabitmap_file)
    211      1.19   reinoud 			vrele(ump->metadatabitmap_file->vnode);
    212      1.19   reinoud 
    213      1.19   reinoud 		/* vflush all (system) nodes if any */
    214      1.19   reinoud 		(void) vflush(mp, NULLVP, FORCECLOSE);
    215      1.19   reinoud 
    216      1.10   reinoud 		/* dispose of our descriptor pool */
    217      1.15   reinoud 		if (ump->desc_pool) {
    218      1.11   reinoud 			pool_destroy(ump->desc_pool);
    219      1.15   reinoud 			free(ump->desc_pool, M_UDFMNT);
    220      1.15   reinoud 		}
    221      1.10   reinoud 
    222      1.10   reinoud 		/* clear our data */
    223       1.1   reinoud 		for (i = 0; i < UDF_ANCHORS; i++)
    224       1.1   reinoud 			MPFREE(ump->anchors[i], M_UDFVOLD);
    225       1.1   reinoud 		MPFREE(ump->primary_vol,      M_UDFVOLD);
    226       1.1   reinoud 		MPFREE(ump->logical_vol,      M_UDFVOLD);
    227       1.1   reinoud 		MPFREE(ump->unallocated,      M_UDFVOLD);
    228       1.1   reinoud 		MPFREE(ump->implementation,   M_UDFVOLD);
    229       1.1   reinoud 		MPFREE(ump->logvol_integrity, M_UDFVOLD);
    230       1.1   reinoud 		for (i = 0; i < UDF_PARTITIONS; i++)
    231       1.1   reinoud 			MPFREE(ump->partitions[i], M_UDFVOLD);
    232       1.1   reinoud 		MPFREE(ump->fileset_desc,     M_UDFVOLD);
    233       1.1   reinoud 		MPFREE(ump->vat_table,        M_UDFVOLD);
    234       1.1   reinoud 		MPFREE(ump->sparing_table,    M_UDFVOLD);
    235       1.1   reinoud 
    236  1.29.4.1      matt 		mutex_destroy(&ump->ihash_lock);
    237  1.29.4.1      matt 		mutex_destroy(&ump->get_node_lock);
    238       1.1   reinoud 		free(ump, M_UDFMNT);
    239       1.6  christos 	}
    240       1.1   reinoud }
    241       1.1   reinoud #undef MPFREE
    242       1.1   reinoud 
    243       1.1   reinoud /* --------------------------------------------------------------------- */
    244       1.1   reinoud 
    245       1.1   reinoud int
    246       1.1   reinoud udf_mount(struct mount *mp, const char *path,
    247  1.29.4.2      matt 	  void *data, size_t *data_len)
    248       1.1   reinoud {
    249  1.29.4.2      matt 	struct lwp *l = curlwp;
    250      1.29     pooka 	struct nameidata nd;
    251      1.26       dsl 	struct udf_args *args = data;
    252       1.1   reinoud 	struct udf_mount *ump;
    253       1.1   reinoud 	struct vnode *devvp;
    254       1.1   reinoud 	int openflags, accessmode, error;
    255       1.1   reinoud 
    256       1.1   reinoud 	DPRINTF(CALL, ("udf_mount called\n"));
    257       1.1   reinoud 
    258      1.26       dsl 	if (*data_len < sizeof *args)
    259      1.26       dsl 		return EINVAL;
    260      1.26       dsl 
    261       1.1   reinoud 	if (mp->mnt_flag & MNT_GETARGS) {
    262       1.1   reinoud 		/* request for the mount arguments */
    263       1.1   reinoud 		ump = VFSTOUDF(mp);
    264       1.1   reinoud 		if (ump == NULL)
    265       1.1   reinoud 			return EINVAL;
    266      1.26       dsl 		*args = ump->mount_args;
    267      1.26       dsl 		*data_len = sizeof *args;
    268      1.26       dsl 		return 0;
    269       1.6  christos 	}
    270       1.1   reinoud 
    271       1.1   reinoud 	/* handle request for updating mount parameters */
    272       1.1   reinoud 	/* TODO can't update my mountpoint yet */
    273       1.1   reinoud 	if (mp->mnt_flag & MNT_UPDATE) {
    274       1.1   reinoud 		return EOPNOTSUPP;
    275       1.6  christos 	}
    276       1.1   reinoud 
    277      1.15   reinoud 	/* OK, so we are asked to mount the device */
    278       1.1   reinoud 
    279       1.1   reinoud 	/* check/translate struct version */
    280       1.1   reinoud 	/* TODO sanity checking other mount arguments */
    281      1.26       dsl 	if (args->version != 1) {
    282       1.1   reinoud 		printf("mount_udf: unrecognized argument structure version\n");
    283       1.1   reinoud 		return EINVAL;
    284       1.6  christos 	}
    285       1.1   reinoud 
    286       1.1   reinoud 	/* lookup name to get its vnode */
    287  1.29.4.2      matt 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
    288      1.29     pooka 	error = namei(&nd);
    289       1.1   reinoud 	if (error)
    290       1.1   reinoud 		return error;
    291      1.29     pooka 	devvp = nd.ni_vp;
    292       1.1   reinoud 
    293       1.1   reinoud #ifdef DEBUG
    294       1.1   reinoud 	if (udf_verbose & UDF_DEBUG_VOLUMES)
    295       1.1   reinoud 		vprint("UDF mount, trying to mount \n", devvp);
    296       1.1   reinoud #endif
    297       1.1   reinoud 
    298       1.1   reinoud 	/* check if its a block device specified */
    299       1.1   reinoud 	if (devvp->v_type != VBLK) {
    300       1.1   reinoud 		vrele(devvp);
    301       1.1   reinoud 		return ENOTBLK;
    302       1.1   reinoud 	}
    303       1.1   reinoud 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    304       1.1   reinoud 		vrele(devvp);
    305       1.1   reinoud 		return ENXIO;
    306       1.1   reinoud 	}
    307       1.1   reinoud 
    308       1.1   reinoud 	/* force read-only for now */
    309       1.1   reinoud 	mp->mnt_flag |= MNT_RDONLY;
    310       1.1   reinoud 
    311       1.1   reinoud 	/*
    312       1.1   reinoud 	 * If mount by non-root, then verify that user has necessary
    313       1.1   reinoud 	 * permissions on the device.
    314       1.1   reinoud 	 */
    315      1.20      elad 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
    316       1.1   reinoud 		accessmode = VREAD;
    317       1.1   reinoud 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    318       1.1   reinoud 			accessmode |= VWRITE;
    319      1.13   reinoud 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    320  1.29.4.2      matt 		error = VOP_ACCESS(devvp, accessmode, l->l_cred);
    321      1.13   reinoud 		VOP_UNLOCK(devvp, 0);
    322       1.1   reinoud 		if (error) {
    323      1.13   reinoud 			vrele(devvp);
    324      1.13   reinoud 			return error;
    325       1.1   reinoud 		}
    326       1.1   reinoud 	}
    327       1.1   reinoud 
    328       1.1   reinoud 	/*
    329       1.1   reinoud 	 * Open device and try to mount it!
    330       1.1   reinoud 	 */
    331       1.1   reinoud 	if (mp->mnt_flag & MNT_RDONLY) {
    332       1.1   reinoud 		openflags = FREAD;
    333       1.1   reinoud 	} else {
    334       1.1   reinoud 		openflags = FREAD | FWRITE;
    335       1.6  christos 	}
    336  1.29.4.2      matt 	error = VOP_OPEN(devvp, openflags, FSCRED);
    337       1.1   reinoud 	if (error == 0) {
    338       1.1   reinoud 		/* opened ok, try mounting */
    339      1.26       dsl 		error = udf_mountfs(devvp, mp, l, args);
    340       1.1   reinoud 		if (error) {
    341       1.1   reinoud 			free_udf_mountinfo(mp);
    342      1.13   reinoud 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    343  1.29.4.2      matt 			(void) VOP_CLOSE(devvp, openflags, NOCRED);
    344      1.13   reinoud 			VOP_UNLOCK(devvp, 0);
    345       1.6  christos 		}
    346       1.6  christos 	}
    347       1.1   reinoud 	if (error) {
    348       1.1   reinoud 		/* devvp is still locked */
    349      1.13   reinoud 		vrele(devvp);
    350       1.1   reinoud 		return error;
    351       1.6  christos 	}
    352       1.1   reinoud 
    353       1.1   reinoud 	/* register our mountpoint being on this device */
    354       1.1   reinoud 	devvp->v_specmountpoint = mp;
    355       1.1   reinoud 
    356       1.1   reinoud 	/* successfully mounted */
    357       1.1   reinoud 	DPRINTF(VOLUMES, ("udf_mount() successfull\n"));
    358       1.1   reinoud 
    359      1.26       dsl 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
    360      1.27     pooka 			mp->mnt_op->vfs_name, mp, l);
    361       1.1   reinoud }
    362       1.1   reinoud 
    363       1.1   reinoud /* --------------------------------------------------------------------- */
    364       1.1   reinoud 
    365       1.1   reinoud #ifdef DEBUG
    366       1.1   reinoud static void
    367       1.1   reinoud udf_unmount_sanity_check(struct mount *mp)
    368       1.1   reinoud {
    369       1.1   reinoud 	struct vnode *vp;
    370       1.1   reinoud 
    371       1.1   reinoud 	printf("On unmount, i found the following nodes:\n");
    372      1.17   reinoud 	TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
    373       1.1   reinoud 		vprint("", vp);
    374       1.1   reinoud 		if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
    375       1.1   reinoud 			printf("  is locked\n");
    376       1.6  christos 		}
    377       1.1   reinoud 		if (vp->v_usecount > 1)
    378       1.1   reinoud 			printf("  more than one usecount %d\n", vp->v_usecount);
    379       1.6  christos 	}
    380       1.1   reinoud }
    381       1.1   reinoud #endif
    382       1.1   reinoud 
    383       1.1   reinoud 
    384       1.1   reinoud int
    385  1.29.4.2      matt udf_unmount(struct mount *mp, int mntflags)
    386       1.1   reinoud {
    387       1.1   reinoud 	struct udf_mount *ump;
    388       1.1   reinoud 	int error, flags, closeflags;
    389       1.1   reinoud 
    390       1.1   reinoud 	DPRINTF(CALL, ("udf_umount called\n"));
    391       1.1   reinoud 
    392       1.1   reinoud 	ump = VFSTOUDF(mp);
    393       1.1   reinoud 	if (!ump)
    394       1.1   reinoud 		panic("UDF unmount: empty ump\n");
    395       1.1   reinoud 
    396      1.19   reinoud 	flags = (mntflags & MNT_FORCE) ? FORCECLOSE : 0;
    397       1.1   reinoud 	/* TODO remove these paranoid functions */
    398       1.1   reinoud #ifdef DEBUG
    399       1.1   reinoud 	if (udf_verbose & UDF_DEBUG_LOCKING)
    400       1.1   reinoud 		udf_unmount_sanity_check(mp);
    401       1.1   reinoud #endif
    402       1.1   reinoud 
    403      1.19   reinoud 	/*
    404  1.29.4.1      matt 	 * By specifying SKIPSYSTEM we can skip vnodes marked with VV_SYSTEM.
    405      1.19   reinoud 	 * This hardly documented feature allows us to exempt certain files
    406      1.19   reinoud 	 * from being flushed.
    407      1.19   reinoud 	 */
    408  1.29.4.1      matt 	if ((error = vflush(mp, NULLVP, flags | SKIPSYSTEM)) != 0)
    409       1.1   reinoud 		return error;
    410       1.1   reinoud 
    411       1.1   reinoud #ifdef DEBUG
    412       1.1   reinoud 	if (udf_verbose & UDF_DEBUG_LOCKING)
    413       1.1   reinoud 		udf_unmount_sanity_check(mp);
    414       1.1   reinoud #endif
    415       1.1   reinoud 
    416       1.1   reinoud 	DPRINTF(VOLUMES, ("flush OK\n"));
    417       1.1   reinoud 
    418       1.1   reinoud 	/*
    419       1.1   reinoud 	 * TODO close logical volume and close session if requested.
    420       1.1   reinoud 	 */
    421       1.1   reinoud 
    422       1.1   reinoud 	/* close device */
    423       1.1   reinoud 	DPRINTF(VOLUMES, ("closing device\n"));
    424       1.1   reinoud 	if (mp->mnt_flag & MNT_RDONLY) {
    425       1.1   reinoud 		closeflags = FREAD;
    426       1.1   reinoud 	} else {
    427       1.1   reinoud 		closeflags = FREAD | FWRITE;
    428       1.6  christos 	}
    429       1.1   reinoud 
    430       1.1   reinoud 	/* devvp is still locked by us */
    431       1.1   reinoud 	vn_lock(ump->devvp, LK_EXCLUSIVE | LK_RETRY);
    432  1.29.4.2      matt 	error = VOP_CLOSE(ump->devvp, closeflags, NOCRED);
    433       1.1   reinoud 	if (error)
    434       1.1   reinoud 		printf("Error during closure of device! error %d, "
    435       1.1   reinoud 		       "device might stay locked\n", error);
    436       1.1   reinoud 	DPRINTF(VOLUMES, ("device close ok\n"));
    437       1.1   reinoud 
    438       1.1   reinoud 	/* clear our mount reference and release device node */
    439       1.1   reinoud 	ump->devvp->v_specmountpoint = NULL;
    440       1.1   reinoud 	vput(ump->devvp);
    441       1.1   reinoud 
    442      1.15   reinoud 	/* free up umt structure */
    443      1.15   reinoud 	free_udf_mountinfo(mp);
    444      1.15   reinoud 
    445      1.15   reinoud 	/* free ump struct reference */
    446       1.1   reinoud 	mp->mnt_data = NULL;
    447       1.1   reinoud 	mp->mnt_flag &= ~MNT_LOCAL;
    448       1.1   reinoud 
    449       1.1   reinoud 	DPRINTF(VOLUMES, ("Fin unmount\n"));
    450       1.1   reinoud 	return error;
    451       1.1   reinoud }
    452       1.1   reinoud 
    453       1.1   reinoud /* --------------------------------------------------------------------- */
    454       1.1   reinoud 
    455       1.1   reinoud /*
    456       1.1   reinoud  * Helper function of udf_mount() that actually mounts the disc.
    457       1.1   reinoud  */
    458       1.1   reinoud 
    459       1.1   reinoud static int
    460       1.1   reinoud udf_mountfs(struct vnode *devvp, struct mount *mp,
    461       1.1   reinoud 	    struct lwp *l, struct udf_args *args)
    462       1.1   reinoud {
    463       1.1   reinoud 	struct udf_mount     *ump;
    464       1.1   reinoud 	uint32_t sector_size, lb_size, bshift;
    465       1.1   reinoud 	int    num_anchors, error, lst;
    466       1.1   reinoud 
    467       1.1   reinoud 	/* flush out any old buffers remaining from a previous use. */
    468       1.9   reinoud 	if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)))
    469       1.1   reinoud 		return error;
    470       1.1   reinoud 
    471      1.24   msaitoh 	/* allocate udf part of mount structure; malloc always succeeds */
    472  1.29.4.1      matt 	ump = malloc(sizeof(struct udf_mount), M_UDFMNT, M_WAITOK | M_ZERO);
    473       1.1   reinoud 
    474       1.1   reinoud 	/* init locks */
    475  1.29.4.1      matt 	mutex_init(&ump->ihash_lock, MUTEX_DEFAULT, IPL_NONE);
    476  1.29.4.1      matt 	mutex_init(&ump->get_node_lock, MUTEX_DEFAULT, IPL_NONE);
    477       1.1   reinoud 
    478       1.1   reinoud 	/* init `ino_t' to udf_node hash table */
    479       1.1   reinoud 	for (lst = 0; lst < UDF_INODE_HASHSIZE; lst++) {
    480       1.1   reinoud 		LIST_INIT(&ump->udf_nodes[lst]);
    481       1.6  christos 	}
    482       1.1   reinoud 
    483       1.1   reinoud 	/* set up linkage */
    484       1.1   reinoud 	mp->mnt_data    = ump;
    485       1.1   reinoud 	ump->vfs_mountp = mp;
    486       1.1   reinoud 
    487       1.1   reinoud 	/* set up arguments and device */
    488       1.1   reinoud 	ump->mount_args = *args;
    489       1.1   reinoud 	ump->devvp      = devvp;
    490       1.9   reinoud 	if ((error = udf_update_discinfo(ump))) {
    491       1.1   reinoud 		printf("UDF mount: error inspecting fs node\n");
    492       1.1   reinoud 		return error;
    493       1.6  christos 	}
    494       1.1   reinoud 
    495       1.1   reinoud 	/* inspect sector size */
    496       1.1   reinoud 	sector_size = ump->discinfo.sector_size;
    497       1.1   reinoud 	bshift = 1;
    498       1.1   reinoud 	while ((1 << bshift) < sector_size)
    499       1.1   reinoud 		bshift++;
    500       1.1   reinoud 	if ((1 << bshift) != sector_size) {
    501       1.1   reinoud 		printf("UDF mount: "
    502       1.1   reinoud 		       "hit NetBSD implementation fence on sector size\n");
    503       1.1   reinoud 		return EIO;
    504       1.6  christos 	}
    505       1.1   reinoud 
    506       1.1   reinoud 	/* read all anchors to get volume descriptor sequence */
    507       1.1   reinoud 	num_anchors = udf_read_anchors(ump, args);
    508       1.1   reinoud 	if (num_anchors == 0)
    509       1.1   reinoud 		return ENOENT;
    510       1.1   reinoud 
    511       1.1   reinoud 	DPRINTF(VOLUMES, ("Read %d anchors on this disc, session %d\n",
    512       1.1   reinoud 	    num_anchors, args->sessionnr));
    513       1.1   reinoud 
    514       1.1   reinoud 	/* read in volume descriptor sequence */
    515       1.9   reinoud 	if ((error = udf_read_vds_space(ump))) {
    516       1.1   reinoud 		printf("UDF mount: error reading volume space\n");
    517       1.9   reinoud 		return error;
    518       1.9   reinoud 	}
    519       1.1   reinoud 
    520       1.1   reinoud 	/* check consistency and completeness */
    521       1.9   reinoud 	if ((error = udf_process_vds(ump, args))) {
    522      1.19   reinoud 		printf( "UDF mount: disc not properly formatted"
    523      1.19   reinoud 			"(bad VDS)\n");
    524       1.9   reinoud 		return error;
    525       1.6  christos 	}
    526       1.1   reinoud 
    527       1.1   reinoud 	/*
    528       1.1   reinoud 	 * Initialise pool for descriptors associated with nodes. This is done
    529       1.1   reinoud 	 * in lb_size units though currently lb_size is dictated to be
    530       1.1   reinoud 	 * sector_size.
    531       1.1   reinoud 	 */
    532       1.1   reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    533      1.11   reinoud 	ump->desc_pool = malloc(sizeof(struct pool), M_UDFMNT, M_WAITOK);
    534      1.11   reinoud 	memset(ump->desc_pool, 0, sizeof(struct pool));
    535      1.23        ad 	pool_init(ump->desc_pool, lb_size, 0, 0, 0, "udf_desc_pool", NULL,
    536      1.23        ad 	    IPL_NONE);
    537       1.1   reinoud 
    538       1.1   reinoud 	/* read vds support tables like VAT, sparable etc. */
    539       1.9   reinoud 	if ((error = udf_read_vds_tables(ump, args))) {
    540      1.19   reinoud 		printf( "UDF mount: error in format or damaged disc "
    541      1.19   reinoud 			"(VDS tables failing)\n");
    542       1.9   reinoud 		return error;
    543       1.6  christos 	}
    544       1.9   reinoud 
    545       1.9   reinoud 	if ((error = udf_read_rootdirs(ump, args))) {
    546      1.19   reinoud 		printf( "UDF mount: "
    547      1.19   reinoud 			"disc not properly formatted or damaged disc "
    548      1.19   reinoud 			"(rootdirs failing)\n");
    549       1.1   reinoud 		return error;
    550       1.9   reinoud 	}
    551       1.1   reinoud 
    552       1.1   reinoud 	/* setup rest of mount information */
    553       1.1   reinoud 	mp->mnt_data = ump;
    554       1.1   reinoud 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (uint32_t) devvp->v_rdev;
    555       1.1   reinoud 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_UDF);
    556       1.1   reinoud 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    557       1.1   reinoud 	mp->mnt_stat.f_namemax = UDF_MAX_NAMELEN;
    558       1.1   reinoud 	mp->mnt_flag |= MNT_LOCAL;
    559       1.1   reinoud 
    560      1.24   msaitoh 	/* bshift is always equal to disc sector size */
    561       1.1   reinoud 	mp->mnt_dev_bshift = bshift;
    562       1.1   reinoud 	mp->mnt_fs_bshift  = bshift;
    563       1.1   reinoud 
    564       1.1   reinoud 	/* do we have to set this? */
    565       1.1   reinoud 	devvp->v_specmountpoint = mp;
    566       1.1   reinoud 
    567       1.1   reinoud 	/* success! */
    568       1.1   reinoud 	return 0;
    569       1.1   reinoud }
    570       1.1   reinoud 
    571       1.1   reinoud /* --------------------------------------------------------------------- */
    572       1.1   reinoud 
    573       1.1   reinoud int
    574  1.29.4.2      matt udf_start(struct mount *mp, int flags)
    575       1.1   reinoud {
    576       1.1   reinoud 	/* do we have to do something here? */
    577       1.1   reinoud 	return 0;
    578       1.1   reinoud }
    579       1.1   reinoud 
    580       1.1   reinoud /* --------------------------------------------------------------------- */
    581       1.1   reinoud 
    582       1.1   reinoud int
    583       1.1   reinoud udf_root(struct mount *mp, struct vnode **vpp)
    584       1.1   reinoud {
    585       1.1   reinoud 	struct vnode *vp;
    586       1.1   reinoud 	struct long_ad *dir_loc;
    587       1.1   reinoud 	struct udf_mount *ump = VFSTOUDF(mp);
    588       1.1   reinoud 	struct udf_node *root_dir;
    589       1.1   reinoud 	int error;
    590       1.1   reinoud 
    591       1.1   reinoud 	DPRINTF(CALL, ("udf_root called\n"));
    592       1.1   reinoud 
    593       1.1   reinoud 	dir_loc = &ump->fileset_desc->rootdir_icb;
    594       1.1   reinoud 	error = udf_get_node(ump, dir_loc, &root_dir);
    595       1.1   reinoud 
    596       1.1   reinoud 	if (!root_dir)
    597       1.1   reinoud 		error = ENOENT;
    598       1.1   reinoud 	if (error)
    599       1.1   reinoud 		return error;
    600       1.1   reinoud 
    601       1.1   reinoud 	vp = root_dir->vnode;
    602  1.29.4.1      matt 	root_dir->vnode->v_vflag |= VV_ROOT;
    603       1.1   reinoud 
    604       1.1   reinoud 	*vpp = vp;
    605       1.1   reinoud 	return 0;
    606       1.1   reinoud }
    607       1.1   reinoud 
    608       1.1   reinoud /* --------------------------------------------------------------------- */
    609       1.1   reinoud 
    610       1.1   reinoud int
    611  1.29.4.2      matt udf_statvfs(struct mount *mp, struct statvfs *sbp)
    612       1.1   reinoud {
    613       1.1   reinoud 	struct udf_mount *ump = VFSTOUDF(mp);
    614       1.1   reinoud 	struct logvol_int_desc *lvid;
    615       1.1   reinoud 	struct udf_logvol_info *impl;
    616       1.1   reinoud 	uint64_t freeblks, sizeblks;
    617       1.1   reinoud 	uint32_t *pos1, *pos2;
    618       1.1   reinoud 	int part, num_part;
    619       1.1   reinoud 
    620       1.1   reinoud 	DPRINTF(CALL, ("udf_statvfs called\n"));
    621       1.1   reinoud 	sbp->f_flag   = mp->mnt_flag;
    622       1.1   reinoud 	sbp->f_bsize  = ump->discinfo.sector_size;
    623       1.1   reinoud 	sbp->f_frsize = ump->discinfo.sector_size;
    624       1.1   reinoud 	sbp->f_iosize = ump->discinfo.sector_size;
    625       1.1   reinoud 
    626       1.1   reinoud 	/* TODO integrity locking */
    627       1.1   reinoud 	/* free and used space for mountpoint based on logvol integrity */
    628       1.1   reinoud 	lvid = ump->logvol_integrity;
    629       1.1   reinoud 	if (lvid) {
    630       1.1   reinoud 		num_part = udf_rw32(lvid->num_part);
    631       1.1   reinoud 		impl = (struct udf_logvol_info *) (lvid->tables + 2*num_part);
    632       1.1   reinoud 
    633       1.1   reinoud 		freeblks = sizeblks = 0;
    634       1.1   reinoud 		for (part=0; part < num_part; part++) {
    635       1.1   reinoud 			pos1 = &lvid->tables[0] + part;
    636       1.1   reinoud 			pos2 = &lvid->tables[0] + num_part + part;
    637       1.1   reinoud 			if (udf_rw32(*pos1) != (uint32_t) -1) {
    638       1.1   reinoud 				freeblks += udf_rw32(*pos1);
    639       1.1   reinoud 				sizeblks += udf_rw32(*pos2);
    640       1.6  christos 			}
    641       1.6  christos 		}
    642       1.1   reinoud 		sbp->f_blocks = sizeblks;
    643       1.1   reinoud 		sbp->f_bfree  = freeblks;
    644       1.1   reinoud 		sbp->f_files  = udf_rw32(impl->num_files);
    645       1.1   reinoud 		sbp->f_files += udf_rw32(impl->num_directories);
    646       1.1   reinoud 
    647       1.1   reinoud 		/* XXX read only for now XXX */
    648       1.1   reinoud 		sbp->f_bavail = 0;
    649       1.1   reinoud 		sbp->f_bresvd = 0;
    650       1.1   reinoud 
    651       1.1   reinoud 		/* tricky, next only aplies to ffs i think, so set to zero */
    652       1.1   reinoud 		sbp->f_ffree  = 0;
    653       1.1   reinoud 		sbp->f_favail = 0;
    654       1.1   reinoud 		sbp->f_fresvd = 0;
    655       1.6  christos 	}
    656       1.1   reinoud 
    657       1.1   reinoud 	copy_statvfs_info(sbp, mp);
    658       1.1   reinoud 	return 0;
    659       1.1   reinoud }
    660       1.1   reinoud 
    661       1.1   reinoud /* --------------------------------------------------------------------- */
    662       1.1   reinoud 
    663       1.1   reinoud int
    664      1.18  christos udf_sync(struct mount *mp, int waitfor,
    665  1.29.4.2      matt     kauth_cred_t cred)
    666       1.1   reinoud {
    667       1.1   reinoud 	DPRINTF(CALL, ("udf_sync called\n"));
    668       1.1   reinoud 	/* nothing to be done as upto now read-only */
    669       1.1   reinoud 	return 0;
    670       1.1   reinoud }
    671       1.1   reinoud 
    672       1.1   reinoud /* --------------------------------------------------------------------- */
    673       1.1   reinoud 
    674       1.1   reinoud /*
    675       1.1   reinoud  * Get vnode for the file system type specific file id ino for the fs. Its
    676       1.1   reinoud  * used for reference to files by unique ID and for NFSv3.
    677       1.1   reinoud  * (optional) TODO lookup why some sources state NFSv3
    678       1.1   reinoud  */
    679       1.1   reinoud int
    680      1.18  christos udf_vget(struct mount *mp, ino_t ino,
    681      1.18  christos     struct vnode **vpp)
    682       1.1   reinoud {
    683       1.1   reinoud 	DPRINTF(NOTIMPL, ("udf_vget called\n"));
    684       1.1   reinoud 	return EOPNOTSUPP;
    685       1.1   reinoud }
    686       1.1   reinoud 
    687       1.1   reinoud /* --------------------------------------------------------------------- */
    688       1.1   reinoud 
    689       1.1   reinoud /*
    690       1.1   reinoud  * Lookup vnode for file handle specified
    691       1.1   reinoud  */
    692       1.1   reinoud int
    693      1.18  christos udf_fhtovp(struct mount *mp, struct fid *fhp,
    694      1.18  christos     struct vnode **vpp)
    695       1.1   reinoud {
    696       1.1   reinoud 	DPRINTF(NOTIMPL, ("udf_fhtovp called\n"));
    697       1.1   reinoud 	return EOPNOTSUPP;
    698       1.1   reinoud }
    699       1.1   reinoud 
    700       1.1   reinoud /* --------------------------------------------------------------------- */
    701       1.1   reinoud 
    702       1.1   reinoud /*
    703       1.1   reinoud  * Create an unique file handle. Its structure is opaque and won't be used by
    704       1.1   reinoud  * other subsystems. It should uniquely identify the file in the filingsystem
    705       1.1   reinoud  * and enough information to know if a file has been removed and/or resources
    706       1.1   reinoud  * have been recycled.
    707       1.1   reinoud  */
    708       1.1   reinoud int
    709      1.18  christos udf_vptofh(struct vnode *vp, struct fid *fid,
    710      1.18  christos     size_t *fh_size)
    711       1.1   reinoud {
    712       1.1   reinoud 	DPRINTF(NOTIMPL, ("udf_vptofh called\n"));
    713       1.1   reinoud 	return EOPNOTSUPP;
    714       1.1   reinoud }
    715       1.1   reinoud 
    716       1.1   reinoud /* --------------------------------------------------------------------- */
    717       1.1   reinoud 
    718       1.1   reinoud /*
    719       1.1   reinoud  * Create a filingsystem snapshot at the specified timestamp. Could be
    720       1.1   reinoud  * implemented by explicitly creating a new session or with spare room in the
    721       1.1   reinoud  * integrity descriptor space
    722       1.1   reinoud  */
    723       1.1   reinoud int
    724      1.18  christos udf_snapshot(struct mount *mp, struct vnode *vp,
    725      1.18  christos     struct timespec *tm)
    726       1.1   reinoud {
    727       1.1   reinoud 	DPRINTF(NOTIMPL, ("udf_snapshot called\n"));
    728       1.1   reinoud 	return EOPNOTSUPP;
    729       1.1   reinoud }
    730      1.14   reinoud 
    731      1.14   reinoud /* --------------------------------------------------------------------- */
    732      1.14   reinoud 
    733      1.14   reinoud /*
    734      1.14   reinoud  * If running a DEBUG kernel, provide an easy way to set the debug flags when
    735      1.14   reinoud  * running into a problem.
    736      1.14   reinoud  */
    737      1.14   reinoud 
    738      1.14   reinoud #ifdef DEBUG
    739      1.14   reinoud #define UDF_VERBOSE_SYSCTLOPT 1
    740      1.14   reinoud 
    741      1.14   reinoud SYSCTL_SETUP(sysctl_vfs_udf_setup, "sysctl vfs.udf subtree setup")
    742      1.14   reinoud {
    743      1.14   reinoud 	/*
    744      1.14   reinoud 	 * XXX the "24" below could be dynamic, thereby eliminating one
    745      1.14   reinoud 	 * more instance of the "number to vfs" mapping problem, but
    746      1.14   reinoud 	 * "24" is the order as taken from sys/mount.h
    747      1.14   reinoud 	 */
    748      1.14   reinoud 
    749      1.14   reinoud 	sysctl_createv(clog, 0, NULL, NULL,
    750      1.14   reinoud 		       CTLFLAG_PERMANENT,
    751      1.14   reinoud 		       CTLTYPE_NODE, "vfs", NULL,
    752      1.14   reinoud 		       NULL, 0, NULL, 0,
    753      1.14   reinoud 		       CTL_VFS, CTL_EOL);
    754      1.14   reinoud 	sysctl_createv(clog, 0, NULL, NULL,
    755      1.14   reinoud 		       CTLFLAG_PERMANENT,
    756      1.14   reinoud 		       CTLTYPE_NODE, "udf",
    757      1.14   reinoud 		       SYSCTL_DESCR("OSTA Universal File System"),
    758      1.14   reinoud 		       NULL, 0, NULL, 0,
    759      1.14   reinoud 		       CTL_VFS, 24, CTL_EOL);
    760      1.14   reinoud 
    761      1.14   reinoud 	sysctl_createv(clog, 0, NULL, NULL,
    762      1.14   reinoud 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    763      1.14   reinoud 		       CTLTYPE_INT, "verbose",
    764      1.14   reinoud 		       SYSCTL_DESCR("Bitmask for filesystem debugging"),
    765      1.14   reinoud 		       NULL, 0, &udf_verbose, 0,
    766      1.14   reinoud 		       CTL_VFS, 24, UDF_VERBOSE_SYSCTLOPT, CTL_EOL);
    767      1.14   reinoud }
    768      1.14   reinoud 
    769      1.14   reinoud #endif /* DEBUG */
    770      1.14   reinoud 
    771