Home | History | Annotate | Line # | Download | only in lfs
ulfs_vfsops.c revision 1.10
      1  1.10  dholland /*	$NetBSD: ulfs_vfsops.c,v 1.10 2015/09/01 06:16:59 dholland Exp $	*/
      2   1.1  dholland /*  from NetBSD: ufs_vfsops.c,v 1.52 2013/01/22 09:39:18 dholland Exp  */
      3   1.1  dholland 
      4   1.1  dholland /*
      5   1.1  dholland  * Copyright (c) 1991, 1993, 1994
      6   1.1  dholland  *	The Regents of the University of California.  All rights reserved.
      7   1.1  dholland  * (c) UNIX System Laboratories, Inc.
      8   1.1  dholland  * All or some portions of this file are derived from material licensed
      9   1.1  dholland  * to the University of California by American Telephone and Telegraph
     10   1.1  dholland  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     11   1.1  dholland  * the permission of UNIX System Laboratories, Inc.
     12   1.1  dholland  *
     13   1.1  dholland  * Redistribution and use in source and binary forms, with or without
     14   1.1  dholland  * modification, are permitted provided that the following conditions
     15   1.1  dholland  * are met:
     16   1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     17   1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     18   1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     19   1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     20   1.1  dholland  *    documentation and/or other materials provided with the distribution.
     21   1.1  dholland  * 3. Neither the name of the University nor the names of its contributors
     22   1.1  dholland  *    may be used to endorse or promote products derived from this software
     23   1.1  dholland  *    without specific prior written permission.
     24   1.1  dholland  *
     25   1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26   1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27   1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28   1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29   1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30   1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31   1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32   1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33   1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34   1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35   1.1  dholland  * SUCH DAMAGE.
     36   1.1  dholland  *
     37   1.1  dholland  *	@(#)ufs_vfsops.c	8.8 (Berkeley) 5/20/95
     38   1.1  dholland  */
     39   1.1  dholland 
     40   1.1  dholland #include <sys/cdefs.h>
     41  1.10  dholland __KERNEL_RCSID(0, "$NetBSD: ulfs_vfsops.c,v 1.10 2015/09/01 06:16:59 dholland Exp $");
     42   1.1  dholland 
     43   1.1  dholland #if defined(_KERNEL_OPT)
     44   1.3  dholland #include "opt_lfs.h"
     45   1.1  dholland #include "opt_quota.h"
     46   1.1  dholland #endif
     47   1.1  dholland 
     48   1.1  dholland #include <sys/param.h>
     49   1.1  dholland #include <sys/mbuf.h>
     50   1.1  dholland #include <sys/mount.h>
     51   1.1  dholland #include <sys/proc.h>
     52   1.1  dholland #include <sys/buf.h>
     53   1.1  dholland #include <sys/vnode.h>
     54   1.1  dholland #include <sys/kmem.h>
     55   1.1  dholland #include <sys/kauth.h>
     56   1.6  dholland #include <sys/quotactl.h>
     57   1.1  dholland 
     58   1.1  dholland #include <miscfs/specfs/specdev.h>
     59   1.1  dholland 
     60   1.6  dholland #include <ufs/lfs/lfs.h>
     61  1.10  dholland #include <ufs/lfs/lfs_accessors.h>
     62   1.2  dholland #include <ufs/lfs/ulfs_quotacommon.h>
     63   1.2  dholland #include <ufs/lfs/ulfs_inode.h>
     64   1.2  dholland #include <ufs/lfs/ulfsmount.h>
     65   1.2  dholland #include <ufs/lfs/ulfs_extern.h>
     66   1.3  dholland #ifdef LFS_DIRHASH
     67   1.2  dholland #include <ufs/lfs/ulfs_dirhash.h>
     68   1.1  dholland #endif
     69   1.1  dholland 
     70   1.4  dholland /* how many times ulfs_init() was called */
     71   1.4  dholland static int ulfs_initcount = 0;
     72   1.1  dholland 
     73   1.4  dholland pool_cache_t ulfs_direct_cache;
     74   1.1  dholland 
     75   1.1  dholland /*
     76   1.1  dholland  * Make a filesystem operational.
     77   1.1  dholland  * Nothing to do at the moment.
     78   1.1  dholland  */
     79   1.1  dholland /* ARGSUSED */
     80   1.1  dholland int
     81   1.4  dholland ulfs_start(struct mount *mp, int flags)
     82   1.1  dholland {
     83   1.1  dholland 
     84   1.1  dholland 	return (0);
     85   1.1  dholland }
     86   1.1  dholland 
     87   1.1  dholland /*
     88   1.1  dholland  * Return the root of a filesystem.
     89   1.1  dholland  */
     90   1.1  dholland int
     91   1.4  dholland ulfs_root(struct mount *mp, struct vnode **vpp)
     92   1.1  dholland {
     93   1.1  dholland 	struct vnode *nvp;
     94   1.1  dholland 	int error;
     95   1.1  dholland 
     96   1.4  dholland 	if ((error = VFS_VGET(mp, (ino_t)ULFS_ROOTINO, &nvp)) != 0)
     97   1.1  dholland 		return (error);
     98   1.1  dholland 	*vpp = nvp;
     99   1.1  dholland 	return (0);
    100   1.1  dholland }
    101   1.1  dholland 
    102   1.1  dholland /*
    103   1.1  dholland  * Do operations associated with quotas
    104   1.1  dholland  */
    105   1.1  dholland int
    106   1.4  dholland ulfs_quotactl(struct mount *mp, struct quotactl_args *args)
    107   1.1  dholland {
    108   1.1  dholland 
    109   1.3  dholland #if !defined(LFS_QUOTA) && !defined(LFS_QUOTA2)
    110   1.1  dholland 	(void) mp;
    111   1.1  dholland 	(void) args;
    112   1.1  dholland 	return (EOPNOTSUPP);
    113   1.1  dholland #else
    114   1.1  dholland 	struct lwp *l = curlwp;
    115   1.1  dholland 	int error;
    116   1.1  dholland 
    117   1.1  dholland 	/* Mark the mount busy, as we're passing it to kauth(9). */
    118   1.1  dholland 	error = vfs_busy(mp, NULL);
    119   1.1  dholland 	if (error) {
    120   1.1  dholland 		return (error);
    121   1.1  dholland 	}
    122   1.1  dholland 	mutex_enter(&mp->mnt_updating);
    123   1.1  dholland 
    124   1.5  dholland 	error = lfsquota_handle_cmd(mp, l, args);
    125   1.1  dholland 
    126   1.1  dholland 	mutex_exit(&mp->mnt_updating);
    127   1.1  dholland 	vfs_unbusy(mp, false, NULL);
    128   1.1  dholland 	return (error);
    129   1.1  dholland #endif
    130   1.1  dholland }
    131   1.1  dholland 
    132   1.1  dholland #if 0
    133   1.1  dholland 	switch (cmd) {
    134   1.1  dholland 	case Q_SYNC:
    135   1.1  dholland 		break;
    136   1.1  dholland 
    137   1.1  dholland 	case Q_GETQUOTA:
    138   1.1  dholland 		/* The user can always query about his own quota. */
    139   1.1  dholland 		if (uid == kauth_cred_getuid(l->l_cred))
    140   1.1  dholland 			break;
    141   1.1  dholland 
    142   1.1  dholland 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    143   1.1  dholland 		    KAUTH_REQ_SYSTEM_FS_QUOTA_GET, mp, KAUTH_ARG(uid), NULL);
    144   1.1  dholland 
    145   1.1  dholland 		break;
    146   1.1  dholland 
    147   1.1  dholland 	case Q_QUOTAON:
    148   1.1  dholland 	case Q_QUOTAOFF:
    149   1.1  dholland 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    150   1.1  dholland 		    KAUTH_REQ_SYSTEM_FS_QUOTA_ONOFF, mp, NULL, NULL);
    151   1.1  dholland 
    152   1.1  dholland 		break;
    153   1.1  dholland 
    154   1.1  dholland 	case Q_SETQUOTA:
    155   1.1  dholland 	case Q_SETUSE:
    156   1.1  dholland 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_QUOTA,
    157   1.1  dholland 		    KAUTH_REQ_SYSTEM_FS_QUOTA_MANAGE, mp, KAUTH_ARG(uid), NULL);
    158   1.1  dholland 
    159   1.1  dholland 		break;
    160   1.1  dholland 
    161   1.1  dholland 	default:
    162   1.1  dholland 		error = EINVAL;
    163   1.1  dholland 		break;
    164   1.1  dholland 	}
    165   1.1  dholland 
    166   1.1  dholland 	type = cmds & SUBCMDMASK;
    167   1.1  dholland 	if (!error) {
    168   1.1  dholland 		/* Only check if there was no error above. */
    169   1.1  dholland 		if ((u_int)type >= MAXQUOTAS)
    170   1.1  dholland 			error = EINVAL;
    171   1.1  dholland 	}
    172   1.1  dholland 
    173   1.1  dholland 	if (error) {
    174   1.1  dholland 		vfs_unbusy(mp, false, NULL);
    175   1.1  dholland 		return (error);
    176   1.1  dholland 	}
    177   1.1  dholland 
    178   1.1  dholland 	mutex_enter(&mp->mnt_updating);
    179   1.1  dholland 	switch (cmd) {
    180   1.1  dholland 
    181   1.1  dholland 	case Q_QUOTAON:
    182   1.1  dholland 		error = quotaon(l, mp, type, arg);
    183   1.1  dholland 		break;
    184   1.1  dholland 
    185   1.1  dholland 	case Q_QUOTAOFF:
    186   1.1  dholland 		error = quotaoff(l, mp, type);
    187   1.1  dholland 		break;
    188   1.1  dholland 
    189   1.1  dholland 	case Q_SETQUOTA:
    190   1.1  dholland 		error = setquota(mp, uid, type, arg);
    191   1.1  dholland 		break;
    192   1.1  dholland 
    193   1.1  dholland 	case Q_SETUSE:
    194   1.1  dholland 		error = setuse(mp, uid, type, arg);
    195   1.1  dholland 		break;
    196   1.1  dholland 
    197   1.1  dholland 	case Q_GETQUOTA:
    198   1.1  dholland 		error = getquota(mp, uid, type, arg);
    199   1.1  dholland 		break;
    200   1.1  dholland 
    201   1.1  dholland 	case Q_SYNC:
    202   1.5  dholland 		error = lfs_qsync(mp);
    203   1.1  dholland 		break;
    204   1.1  dholland 
    205   1.1  dholland 	default:
    206   1.1  dholland 		error = EINVAL;
    207   1.1  dholland 	}
    208   1.1  dholland 	mutex_exit(&mp->mnt_updating);
    209   1.1  dholland 	vfs_unbusy(mp, false, NULL);
    210   1.1  dholland 	return (error);
    211   1.1  dholland #endif
    212   1.1  dholland 
    213   1.1  dholland /*
    214   1.1  dholland  * This is the generic part of fhtovp called after the underlying
    215   1.1  dholland  * filesystem has validated the file handle.
    216   1.1  dholland  */
    217   1.1  dholland int
    218   1.7  dholland ulfs_fhtovp(struct mount *mp, struct ulfs_ufid *ufhp, struct vnode **vpp)
    219   1.1  dholland {
    220   1.1  dholland 	struct vnode *nvp;
    221   1.1  dholland 	struct inode *ip;
    222   1.1  dholland 	int error;
    223   1.1  dholland 
    224   1.1  dholland 	if ((error = VFS_VGET(mp, ufhp->ufid_ino, &nvp)) != 0) {
    225   1.9   hannken 		if (error == ENOENT)
    226   1.9   hannken 			error = ESTALE;
    227   1.1  dholland 		*vpp = NULLVP;
    228   1.1  dholland 		return (error);
    229   1.1  dholland 	}
    230   1.1  dholland 	ip = VTOI(nvp);
    231   1.1  dholland 	KASSERT(ip != NULL);
    232   1.1  dholland 	if (ip->i_mode == 0 || ip->i_gen != ufhp->ufid_gen) {
    233   1.1  dholland 		vput(nvp);
    234   1.1  dholland 		*vpp = NULLVP;
    235   1.1  dholland 		return (ESTALE);
    236   1.1  dholland 	}
    237   1.1  dholland 	*vpp = nvp;
    238   1.1  dholland 	return (0);
    239   1.1  dholland }
    240   1.1  dholland 
    241   1.1  dholland /*
    242   1.4  dholland  * Initialize ULFS filesystems, done only once.
    243   1.1  dholland  */
    244   1.1  dholland void
    245   1.4  dholland ulfs_init(void)
    246   1.1  dholland {
    247   1.4  dholland 	if (ulfs_initcount++ > 0)
    248   1.1  dholland 		return;
    249   1.1  dholland 
    250   1.8  dholland 	ulfs_direct_cache = pool_cache_init(sizeof(struct lfs_direct), 0, 0, 0,
    251   1.4  dholland 	    "ulfsdir", NULL, IPL_NONE, NULL, NULL, NULL);
    252   1.1  dholland 
    253   1.3  dholland #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    254   1.5  dholland 	lfs_dqinit();
    255   1.1  dholland #endif
    256   1.3  dholland #ifdef LFS_DIRHASH
    257   1.4  dholland 	ulfsdirhash_init();
    258   1.1  dholland #endif
    259   1.3  dholland #ifdef LFS_EXTATTR
    260   1.4  dholland 	ulfs_extattr_init();
    261   1.1  dholland #endif
    262   1.1  dholland }
    263   1.1  dholland 
    264   1.1  dholland void
    265   1.4  dholland ulfs_reinit(void)
    266   1.1  dholland {
    267   1.9   hannken 
    268   1.3  dholland #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    269   1.5  dholland 	lfs_dqreinit();
    270   1.1  dholland #endif
    271   1.1  dholland }
    272   1.1  dholland 
    273   1.1  dholland /*
    274   1.4  dholland  * Free ULFS filesystem resources, done only once.
    275   1.1  dholland  */
    276   1.1  dholland void
    277   1.4  dholland ulfs_done(void)
    278   1.1  dholland {
    279   1.4  dholland 	if (--ulfs_initcount > 0)
    280   1.1  dholland 		return;
    281   1.1  dholland 
    282   1.3  dholland #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
    283   1.5  dholland 	lfs_dqdone();
    284   1.1  dholland #endif
    285   1.4  dholland 	pool_cache_destroy(ulfs_direct_cache);
    286   1.3  dholland #ifdef LFS_DIRHASH
    287   1.4  dholland 	ulfsdirhash_done();
    288   1.1  dholland #endif
    289   1.3  dholland #ifdef LFS_EXTATTR
    290   1.4  dholland 	ulfs_extattr_done();
    291   1.1  dholland #endif
    292   1.1  dholland }
    293