Home | History | Annotate | Line # | Download | only in lfs
ulfs_extattr.c revision 1.7
      1  1.7   hannken /*	$NetBSD: ulfs_extattr.c,v 1.7 2014/02/07 15:29:23 hannken Exp $	*/
      2  1.1  dholland /*  from NetBSD: ufs_extattr.c,v 1.41 2012/12/08 13:42:36 manu Exp  */
      3  1.1  dholland 
      4  1.1  dholland /*-
      5  1.1  dholland  * Copyright (c) 1999-2002 Robert N. M. Watson
      6  1.1  dholland  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
      7  1.1  dholland  * All rights reserved.
      8  1.1  dholland  *
      9  1.1  dholland  * This software was developed by Robert Watson for the TrustedBSD Project.
     10  1.1  dholland  *
     11  1.1  dholland  * This software was developed for the FreeBSD Project in part by Network
     12  1.1  dholland  * Associates Laboratories, the Security Research Division of Network
     13  1.1  dholland  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
     14  1.1  dholland  * as part of the DARPA CHATS research program.
     15  1.1  dholland  *
     16  1.1  dholland  * Redistribution and use in source and binary forms, with or without
     17  1.1  dholland  * modification, are permitted provided that the following conditions
     18  1.1  dholland  * are met:
     19  1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     20  1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     21  1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     23  1.1  dholland  *    documentation and/or other materials provided with the distribution.
     24  1.1  dholland  *
     25  1.1  dholland  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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  */
     38  1.1  dholland 
     39  1.1  dholland /*
     40  1.4  dholland  * Support for file system extended attributes on the ULFS1 file system.
     41  1.1  dholland  *
     42  1.1  dholland  * Extended attributes are defined in the form name=value, where name is
     43  1.1  dholland  * a nul-terminated string in the style of a file name, and value is a
     44  1.4  dholland  * binary blob of zero or more bytes.  The ULFS1 extended attribute service
     45  1.1  dholland  * layers support for extended attributes onto a backing file, in the style
     46  1.1  dholland  * of the quota implementation, meaning that it requires no underlying format
     47  1.1  dholland  * changes to the file system.  This design choice exchanges simplicity,
     48  1.1  dholland  * usability, and easy deployment for performance.
     49  1.1  dholland  */
     50  1.1  dholland 
     51  1.1  dholland #include <sys/cdefs.h>
     52  1.7   hannken __KERNEL_RCSID(0, "$NetBSD: ulfs_extattr.c,v 1.7 2014/02/07 15:29:23 hannken Exp $");
     53  1.1  dholland 
     54  1.1  dholland #ifdef _KERNEL_OPT
     55  1.3  dholland #include "opt_lfs.h"
     56  1.1  dholland #endif
     57  1.1  dholland 
     58  1.1  dholland #include <sys/param.h>
     59  1.1  dholland #include <sys/systm.h>
     60  1.1  dholland #include <sys/reboot.h>
     61  1.1  dholland #include <sys/kauth.h>
     62  1.1  dholland #include <sys/kernel.h>
     63  1.1  dholland #include <sys/namei.h>
     64  1.1  dholland #include <sys/kmem.h>
     65  1.1  dholland #include <sys/fcntl.h>
     66  1.1  dholland #include <sys/lwp.h>
     67  1.1  dholland #include <sys/vnode.h>
     68  1.1  dholland #include <sys/mount.h>
     69  1.1  dholland #include <sys/lock.h>
     70  1.1  dholland #include <sys/dirent.h>
     71  1.1  dholland #include <sys/extattr.h>
     72  1.1  dholland #include <sys/sysctl.h>
     73  1.1  dholland 
     74  1.2  dholland #include <ufs/lfs/ulfs_extattr.h>
     75  1.2  dholland #include <ufs/lfs/ulfsmount.h>
     76  1.2  dholland #include <ufs/lfs/ulfs_inode.h>
     77  1.2  dholland #include <ufs/lfs/ulfs_bswap.h>
     78  1.2  dholland #include <ufs/lfs/ulfs_extern.h>
     79  1.1  dholland 
     80  1.4  dholland int ulfs_extattr_sync = 1;
     81  1.4  dholland int ulfs_extattr_autocreate = 1024;
     82  1.1  dholland 
     83  1.4  dholland static int	ulfs_extattr_valid_attrname(int attrnamespace,
     84  1.1  dholland 		    const char *attrname);
     85  1.4  dholland static int	ulfs_extattr_enable_with_open(struct ulfsmount *ump,
     86  1.1  dholland 		    struct vnode *vp, int attrnamespace, const char *attrname,
     87  1.1  dholland 		    struct lwp *l);
     88  1.4  dholland static int	ulfs_extattr_enable(struct ulfsmount *ump, int attrnamespace,
     89  1.1  dholland 		    const char *attrname, struct vnode *backing_vnode,
     90  1.1  dholland 		    struct lwp *l);
     91  1.4  dholland static int	ulfs_extattr_disable(struct ulfsmount *ump, int attrnamespace,
     92  1.1  dholland 		    const char *attrname, struct lwp *l);
     93  1.4  dholland static int	ulfs_extattr_get(struct vnode *vp, int attrnamespace,
     94  1.1  dholland 		    const char *name, struct uio *uio, size_t *size,
     95  1.1  dholland 		    kauth_cred_t cred, struct lwp *l);
     96  1.4  dholland static int	ulfs_extattr_list(struct vnode *vp, int attrnamespace,
     97  1.1  dholland 		    struct uio *uio, size_t *size, int flag,
     98  1.1  dholland 		    kauth_cred_t cred, struct lwp *l);
     99  1.4  dholland static int	ulfs_extattr_set(struct vnode *vp, int attrnamespace,
    100  1.1  dholland 		    const char *name, struct uio *uio, kauth_cred_t cred,
    101  1.1  dholland 		    struct lwp *l);
    102  1.4  dholland static int	ulfs_extattr_rm(struct vnode *vp, int attrnamespace,
    103  1.1  dholland 		    const char *name, kauth_cred_t cred, struct lwp *l);
    104  1.4  dholland static struct ulfs_extattr_list_entry *ulfs_extattr_find_attr(struct ulfsmount *,
    105  1.1  dholland 		    int, const char *);
    106  1.4  dholland static int	ulfs_extattr_get_header(struct vnode *,
    107  1.4  dholland 		    struct ulfs_extattr_list_entry *,
    108  1.4  dholland 		    struct ulfs_extattr_header *, off_t *);
    109  1.1  dholland 
    110  1.1  dholland /*
    111  1.1  dholland  * Convert a FreeBSD extended attribute and namespace to a consistent string
    112  1.1  dholland  * representation.
    113  1.1  dholland  *
    114  1.1  dholland  * The returned value, if not NULL, is guaranteed to be an allocated object
    115  1.1  dholland  * of its size as returned by strlen() + 1 and must be freed by the caller.
    116  1.1  dholland  */
    117  1.1  dholland static char *
    118  1.1  dholland from_freebsd_extattr(int attrnamespace, const char *attrname)
    119  1.1  dholland {
    120  1.1  dholland 	const char *namespace;
    121  1.1  dholland 	char *attr;
    122  1.1  dholland 	size_t len;
    123  1.1  dholland 
    124  1.1  dholland 	if (attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
    125  1.1  dholland 		namespace = "system";
    126  1.1  dholland 	else if (attrnamespace == EXTATTR_NAMESPACE_USER)
    127  1.1  dholland 		namespace = "user";
    128  1.1  dholland 	else
    129  1.1  dholland 		return NULL;
    130  1.1  dholland 
    131  1.1  dholland 	/* <namespace>.<attrname>\0 */
    132  1.1  dholland 	len = strlen(namespace) + 1 + strlen(attrname) + 1;
    133  1.1  dholland 
    134  1.1  dholland 	attr = kmem_alloc(len, KM_SLEEP);
    135  1.1  dholland 
    136  1.1  dholland 	snprintf(attr, len, "%s.%s", namespace, attrname);
    137  1.1  dholland 
    138  1.1  dholland 	return attr;
    139  1.1  dholland }
    140  1.1  dholland 
    141  1.1  dholland /*
    142  1.1  dholland  * Internal wrapper around a conversion-check-free sequence.
    143  1.1  dholland  */
    144  1.1  dholland static int
    145  1.1  dholland internal_extattr_check_cred(vnode_t *vp, int attrnamespace, const char *name,
    146  1.1  dholland     kauth_cred_t cred, int access_mode)
    147  1.1  dholland {
    148  1.1  dholland 	char *attr;
    149  1.1  dholland 	int error;
    150  1.1  dholland 
    151  1.1  dholland 	attr = from_freebsd_extattr(attrnamespace, name);
    152  1.1  dholland 	if (attr == NULL)
    153  1.1  dholland 		return EINVAL;
    154  1.1  dholland 
    155  1.1  dholland 	error = extattr_check_cred(vp, attr, cred, access_mode);
    156  1.1  dholland 
    157  1.1  dholland 	kmem_free(attr, strlen(attr) + 1);
    158  1.1  dholland 
    159  1.1  dholland 	return error;
    160  1.1  dholland }
    161  1.1  dholland 
    162  1.1  dholland /*
    163  1.1  dholland  * Per-FS attribute lock protecting attribute operations.
    164  1.1  dholland  * XXX Right now there is a lot of lock contention due to having a single
    165  1.1  dholland  * lock per-FS; really, this should be far more fine-grained.
    166  1.1  dholland  */
    167  1.1  dholland static void
    168  1.4  dholland ulfs_extattr_uepm_lock(struct ulfsmount *ump)
    169  1.1  dholland {
    170  1.1  dholland 
    171  1.1  dholland 	/* XXX Why does this need to be recursive? */
    172  1.1  dholland 	if (mutex_owned(&ump->um_extattr.uepm_lock)) {
    173  1.1  dholland 		ump->um_extattr.uepm_lockcnt++;
    174  1.1  dholland 		return;
    175  1.1  dholland 	}
    176  1.1  dholland 	mutex_enter(&ump->um_extattr.uepm_lock);
    177  1.1  dholland }
    178  1.1  dholland 
    179  1.1  dholland static void
    180  1.4  dholland ulfs_extattr_uepm_unlock(struct ulfsmount *ump)
    181  1.1  dholland {
    182  1.1  dholland 
    183  1.1  dholland 	if (ump->um_extattr.uepm_lockcnt != 0) {
    184  1.1  dholland 		KASSERT(mutex_owned(&ump->um_extattr.uepm_lock));
    185  1.1  dholland 		ump->um_extattr.uepm_lockcnt--;
    186  1.1  dholland 		return;
    187  1.1  dholland 	}
    188  1.1  dholland 	mutex_exit(&ump->um_extattr.uepm_lock);
    189  1.1  dholland }
    190  1.1  dholland 
    191  1.1  dholland /*-
    192  1.1  dholland  * Determine whether the name passed is a valid name for an actual
    193  1.1  dholland  * attribute.
    194  1.1  dholland  *
    195  1.1  dholland  * Invalid currently consists of:
    196  1.1  dholland  *	 NULL pointer for attrname
    197  1.1  dholland  *	 zero-length attrname (used to retrieve application attribute list)
    198  1.1  dholland  */
    199  1.1  dholland static int
    200  1.4  dholland ulfs_extattr_valid_attrname(int attrnamespace, const char *attrname)
    201  1.1  dholland {
    202  1.1  dholland 
    203  1.1  dholland 	if (attrname == NULL)
    204  1.1  dholland 		return (0);
    205  1.1  dholland 	if (strlen(attrname) == 0)
    206  1.1  dholland 		return (0);
    207  1.1  dholland 	return (1);
    208  1.1  dholland }
    209  1.1  dholland 
    210  1.1  dholland /*
    211  1.1  dholland  * Autocreate an attribute storage
    212  1.1  dholland  */
    213  1.4  dholland static struct ulfs_extattr_list_entry *
    214  1.4  dholland ulfs_extattr_autocreate_attr(struct vnode *vp, int attrnamespace,
    215  1.1  dholland     const char *attrname, struct lwp *l)
    216  1.1  dholland {
    217  1.1  dholland 	struct mount *mp = vp->v_mount;
    218  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    219  1.1  dholland 	struct vnode *backing_vp;
    220  1.1  dholland 	struct nameidata nd;
    221  1.1  dholland 	struct pathbuf *pb;
    222  1.1  dholland 	char *path;
    223  1.4  dholland 	struct ulfs_extattr_fileheader uef;
    224  1.4  dholland 	struct ulfs_extattr_list_entry *uele;
    225  1.1  dholland 	int error;
    226  1.1  dholland 
    227  1.1  dholland 	path = PNBUF_GET();
    228  1.1  dholland 
    229  1.1  dholland 	/*
    230  1.1  dholland 	 * We only support system and user namespace autocreation
    231  1.1  dholland 	 */
    232  1.1  dholland 	switch (attrnamespace) {
    233  1.1  dholland 	case EXTATTR_NAMESPACE_SYSTEM:
    234  1.1  dholland 		(void)snprintf(path, PATH_MAX, "%s/%s/%s/%s",
    235  1.1  dholland 			       mp->mnt_stat.f_mntonname,
    236  1.4  dholland 			       ULFS_EXTATTR_FSROOTSUBDIR,
    237  1.4  dholland 			       ULFS_EXTATTR_SUBDIR_SYSTEM,
    238  1.1  dholland 			       attrname);
    239  1.1  dholland 		break;
    240  1.1  dholland 	case EXTATTR_NAMESPACE_USER:
    241  1.1  dholland 		(void)snprintf(path, PATH_MAX, "%s/%s/%s/%s",
    242  1.1  dholland 			       mp->mnt_stat.f_mntonname,
    243  1.4  dholland 			       ULFS_EXTATTR_FSROOTSUBDIR,
    244  1.4  dholland 			       ULFS_EXTATTR_SUBDIR_USER,
    245  1.1  dholland 			       attrname);
    246  1.1  dholland 		break;
    247  1.1  dholland 	default:
    248  1.1  dholland 		PNBUF_PUT(path);
    249  1.1  dholland 		return NULL;
    250  1.1  dholland 		break;
    251  1.1  dholland 	}
    252  1.1  dholland 
    253  1.1  dholland 	/*
    254  1.1  dholland 	 * XXX unlock/lock should only be done when setting extattr
    255  1.1  dholland 	 * on backing store or one of its parent directory
    256  1.1  dholland 	 * including root, but we always do it for now.
    257  1.1  dholland 	 */
    258  1.1  dholland 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    259  1.1  dholland 	VOP_UNLOCK(vp);
    260  1.1  dholland 
    261  1.1  dholland 	pb = pathbuf_create(path);
    262  1.1  dholland 	NDINIT(&nd, CREATE, LOCKPARENT, pb);
    263  1.1  dholland 
    264  1.1  dholland 	error = vn_open(&nd, O_CREAT|O_RDWR, 0600);
    265  1.1  dholland 
    266  1.1  dholland 	/*
    267  1.1  dholland 	 * Reacquire the lock on the vnode
    268  1.1  dholland 	 */
    269  1.1  dholland 	KASSERT(VOP_ISLOCKED(vp) == 0);
    270  1.1  dholland 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    271  1.1  dholland 
    272  1.1  dholland 	if (error != 0) {
    273  1.1  dholland 		pathbuf_destroy(pb);
    274  1.1  dholland 		PNBUF_PUT(path);
    275  1.1  dholland 		return NULL;
    276  1.1  dholland 	}
    277  1.1  dholland 
    278  1.1  dholland 	KASSERT(nd.ni_vp != NULL);
    279  1.1  dholland 	KASSERT(VOP_ISLOCKED(nd.ni_vp) == LK_EXCLUSIVE);
    280  1.1  dholland 	KASSERT(VOP_ISLOCKED(nd.ni_dvp) == 0);
    281  1.1  dholland 
    282  1.1  dholland 	/*
    283  1.1  dholland  	 * backing_vp is the backing store.
    284  1.1  dholland 	 */
    285  1.1  dholland 	backing_vp = nd.ni_vp;
    286  1.1  dholland 	pathbuf_destroy(pb);
    287  1.1  dholland 	PNBUF_PUT(path);
    288  1.1  dholland 
    289  1.4  dholland 	uef.uef_magic = ULFS_EXTATTR_MAGIC;
    290  1.4  dholland 	uef.uef_version = ULFS_EXTATTR_VERSION;
    291  1.4  dholland 	uef.uef_size = ulfs_extattr_autocreate;
    292  1.1  dholland 
    293  1.1  dholland 	error = vn_rdwr(UIO_WRITE, backing_vp, &uef, sizeof(uef), 0,
    294  1.1  dholland 		        UIO_SYSSPACE, IO_NODELOCKED|IO_APPEND,
    295  1.1  dholland 			l->l_cred, NULL, l);
    296  1.1  dholland 
    297  1.1  dholland 	VOP_UNLOCK(backing_vp);
    298  1.1  dholland 
    299  1.1  dholland 	if (error != 0) {
    300  1.1  dholland 		printf("%s: write uef header failed for %s, error = %d\n",
    301  1.1  dholland 		       __func__, attrname, error);
    302  1.1  dholland 		vn_close(backing_vp, FREAD|FWRITE, l->l_cred);
    303  1.1  dholland 		return NULL;
    304  1.1  dholland 	}
    305  1.1  dholland 
    306  1.1  dholland 	/*
    307  1.1  dholland 	 * Now enable attribute.
    308  1.1  dholland 	 */
    309  1.4  dholland 	error = ulfs_extattr_enable(ump,attrnamespace, attrname, backing_vp, l);
    310  1.1  dholland 	KASSERT(VOP_ISLOCKED(backing_vp) == 0);
    311  1.1  dholland 
    312  1.1  dholland 	if (error != 0) {
    313  1.1  dholland 		printf("%s: enable %s failed, error %d\n",
    314  1.1  dholland 		       __func__, attrname, error);
    315  1.1  dholland 		vn_close(backing_vp, FREAD|FWRITE, l->l_cred);
    316  1.1  dholland 		return NULL;
    317  1.1  dholland 	}
    318  1.1  dholland 
    319  1.4  dholland 	uele = ulfs_extattr_find_attr(ump, attrnamespace, attrname);
    320  1.1  dholland 	if (uele == NULL) {
    321  1.1  dholland 		printf("%s: atttribute %s created but not found!\n",
    322  1.1  dholland 		       __func__, attrname);
    323  1.1  dholland 		vn_close(backing_vp, FREAD|FWRITE, l->l_cred);
    324  1.1  dholland 		return NULL;
    325  1.1  dholland 	}
    326  1.1  dholland 
    327  1.1  dholland 	printf("%s: EA backing store autocreated for %s\n",
    328  1.1  dholland 	       mp->mnt_stat.f_mntonname, attrname);
    329  1.1  dholland 
    330  1.1  dholland 	return uele;
    331  1.1  dholland }
    332  1.1  dholland 
    333  1.1  dholland /*
    334  1.1  dholland  * Locate an attribute given a name and mountpoint.
    335  1.1  dholland  * Must be holding uepm lock for the mount point.
    336  1.1  dholland  */
    337  1.4  dholland static struct ulfs_extattr_list_entry *
    338  1.4  dholland ulfs_extattr_find_attr(struct ulfsmount *ump, int attrnamespace,
    339  1.1  dholland     const char *attrname)
    340  1.1  dholland {
    341  1.4  dholland 	struct ulfs_extattr_list_entry *search_attribute;
    342  1.1  dholland 
    343  1.1  dholland 	for (search_attribute = LIST_FIRST(&ump->um_extattr.uepm_list);
    344  1.1  dholland 	    search_attribute != NULL;
    345  1.1  dholland 	    search_attribute = LIST_NEXT(search_attribute, uele_entries)) {
    346  1.1  dholland 		if (!(strncmp(attrname, search_attribute->uele_attrname,
    347  1.4  dholland 		    ULFS_EXTATTR_MAXEXTATTRNAME)) &&
    348  1.1  dholland 		    (attrnamespace == search_attribute->uele_attrnamespace)) {
    349  1.1  dholland 			return (search_attribute);
    350  1.1  dholland 		}
    351  1.1  dholland 	}
    352  1.1  dholland 
    353  1.1  dholland 	return (0);
    354  1.1  dholland }
    355  1.1  dholland 
    356  1.1  dholland /*
    357  1.1  dholland  * Initialize per-FS structures supporting extended attributes.  Do not
    358  1.1  dholland  * start extended attributes yet.
    359  1.1  dholland  */
    360  1.1  dholland void
    361  1.4  dholland ulfs_extattr_uepm_init(struct ulfs_extattr_per_mount *uepm)
    362  1.1  dholland {
    363  1.1  dholland 
    364  1.1  dholland 	uepm->uepm_flags = 0;
    365  1.1  dholland 	uepm->uepm_lockcnt = 0;
    366  1.1  dholland 
    367  1.1  dholland 	LIST_INIT(&uepm->uepm_list);
    368  1.1  dholland 	mutex_init(&uepm->uepm_lock, MUTEX_DEFAULT, IPL_NONE);
    369  1.4  dholland 	uepm->uepm_flags |= ULFS_EXTATTR_UEPM_INITIALIZED;
    370  1.1  dholland }
    371  1.1  dholland 
    372  1.1  dholland /*
    373  1.1  dholland  * Destroy per-FS structures supporting extended attributes.  Assumes
    374  1.1  dholland  * that EAs have already been stopped, and will panic if not.
    375  1.1  dholland  */
    376  1.1  dholland void
    377  1.4  dholland ulfs_extattr_uepm_destroy(struct ulfs_extattr_per_mount *uepm)
    378  1.1  dholland {
    379  1.1  dholland 
    380  1.4  dholland 	if (!(uepm->uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED))
    381  1.4  dholland 		panic("ulfs_extattr_uepm_destroy: not initialized");
    382  1.1  dholland 
    383  1.4  dholland 	if ((uepm->uepm_flags & ULFS_EXTATTR_UEPM_STARTED))
    384  1.4  dholland 		panic("ulfs_extattr_uepm_destroy: called while still started");
    385  1.1  dholland 
    386  1.1  dholland 	/*
    387  1.1  dholland 	 * It's not clear that either order for the next two lines is
    388  1.1  dholland 	 * ideal, and it should never be a problem if this is only called
    389  1.1  dholland 	 * during unmount, and with vfs_busy().
    390  1.1  dholland 	 */
    391  1.4  dholland 	uepm->uepm_flags &= ~ULFS_EXTATTR_UEPM_INITIALIZED;
    392  1.1  dholland 	mutex_destroy(&uepm->uepm_lock);
    393  1.1  dholland }
    394  1.1  dholland 
    395  1.1  dholland /*
    396  1.1  dholland  * Start extended attribute support on an FS.
    397  1.1  dholland  */
    398  1.1  dholland int
    399  1.4  dholland ulfs_extattr_start(struct mount *mp, struct lwp *l)
    400  1.1  dholland {
    401  1.4  dholland 	struct ulfsmount *ump;
    402  1.1  dholland 	int error = 0;
    403  1.1  dholland 
    404  1.4  dholland 	ump = VFSTOULFS(mp);
    405  1.1  dholland 
    406  1.4  dholland 	ulfs_extattr_uepm_lock(ump);
    407  1.1  dholland 
    408  1.4  dholland 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED)) {
    409  1.1  dholland 		error = EOPNOTSUPP;
    410  1.1  dholland 		goto unlock;
    411  1.1  dholland 	}
    412  1.4  dholland 	if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED) {
    413  1.1  dholland 		error = EBUSY;
    414  1.1  dholland 		goto unlock;
    415  1.1  dholland 	}
    416  1.1  dholland 
    417  1.4  dholland 	ump->um_extattr.uepm_flags |= ULFS_EXTATTR_UEPM_STARTED;
    418  1.1  dholland 
    419  1.1  dholland 	ump->um_extattr.uepm_ucred = l->l_cred;
    420  1.1  dholland 	kauth_cred_hold(ump->um_extattr.uepm_ucred);
    421  1.1  dholland 
    422  1.1  dholland  unlock:
    423  1.4  dholland 	ulfs_extattr_uepm_unlock(ump);
    424  1.1  dholland 
    425  1.1  dholland 	return (error);
    426  1.1  dholland }
    427  1.1  dholland 
    428  1.1  dholland /*
    429  1.1  dholland  * Helper routine: given a locked parent directory and filename, return
    430  1.1  dholland  * the locked vnode of the inode associated with the name.  Will not
    431  1.1  dholland  * follow symlinks, may return any type of vnode.  Lock on parent will
    432  1.1  dholland  * be released even in the event of a failure.  In the event that the
    433  1.1  dholland  * target is the parent (i.e., "."), there will be two references and
    434  1.1  dholland  * one lock, requiring the caller to possibly special-case.
    435  1.1  dholland  */
    436  1.1  dholland static int
    437  1.4  dholland ulfs_extattr_lookup(struct vnode *start_dvp, int lockparent, const char *dirname,
    438  1.1  dholland     struct vnode **vp, struct lwp *l)
    439  1.1  dholland {
    440  1.7   hannken 	struct vop_lookup_v2_args vargs;
    441  1.1  dholland 	struct componentname cnp;
    442  1.1  dholland 	struct vnode *target_vp;
    443  1.1  dholland 	char *pnbuf;
    444  1.1  dholland 	int error;
    445  1.1  dholland 
    446  1.1  dholland 	KASSERT(VOP_ISLOCKED(start_dvp) == LK_EXCLUSIVE);
    447  1.1  dholland 
    448  1.1  dholland 	pnbuf = PNBUF_GET();
    449  1.1  dholland 
    450  1.1  dholland 	memset(&cnp, 0, sizeof(cnp));
    451  1.1  dholland 	cnp.cn_nameiop = LOOKUP;
    452  1.1  dholland 	cnp.cn_flags = ISLASTCN | lockparent;
    453  1.1  dholland 	cnp.cn_cred = l->l_cred;
    454  1.1  dholland 	cnp.cn_nameptr = pnbuf;
    455  1.1  dholland 	error = copystr(dirname, pnbuf, MAXPATHLEN, &cnp.cn_namelen);
    456  1.1  dholland 	if (error) {
    457  1.1  dholland 		if (lockparent == 0) {
    458  1.1  dholland 			VOP_UNLOCK(start_dvp);
    459  1.1  dholland 		}
    460  1.1  dholland 		PNBUF_PUT(pnbuf);
    461  1.4  dholland 		printf("ulfs_extattr_lookup: copystr failed\n");
    462  1.1  dholland 		return (error);
    463  1.1  dholland 	}
    464  1.1  dholland 	cnp.cn_namelen--;	/* trim nul termination */
    465  1.1  dholland 	vargs.a_desc = NULL;
    466  1.1  dholland 	vargs.a_dvp = start_dvp;
    467  1.1  dholland 	vargs.a_vpp = &target_vp;
    468  1.1  dholland 	vargs.a_cnp = &cnp;
    469  1.4  dholland 	error = ulfs_lookup(&vargs);
    470  1.1  dholland 	PNBUF_PUT(pnbuf);
    471  1.1  dholland 	if (error) {
    472  1.1  dholland 		if (lockparent == 0) {
    473  1.1  dholland 			VOP_UNLOCK(start_dvp);
    474  1.1  dholland 		}
    475  1.1  dholland 		return (error);
    476  1.1  dholland 	}
    477  1.1  dholland #if 0
    478  1.1  dholland 	if (target_vp == start_dvp)
    479  1.4  dholland 		panic("ulfs_extattr_lookup: target_vp == start_dvp");
    480  1.1  dholland #endif
    481  1.1  dholland 
    482  1.7   hannken 	if (target_vp != start_dvp) {
    483  1.7   hannken 		error = vn_lock(target_vp, LK_EXCLUSIVE);
    484  1.7   hannken 		if (lockparent == 0)
    485  1.7   hannken 			VOP_UNLOCK(start_dvp);
    486  1.7   hannken 		if (error) {
    487  1.7   hannken 			vrele(target_vp);
    488  1.7   hannken 			return error;
    489  1.7   hannken 		}
    490  1.7   hannken 	}
    491  1.1  dholland 
    492  1.1  dholland 	KASSERT(VOP_ISLOCKED(target_vp) == LK_EXCLUSIVE);
    493  1.1  dholland 	*vp = target_vp;
    494  1.1  dholland 	return (0);
    495  1.1  dholland }
    496  1.1  dholland 
    497  1.1  dholland /*
    498  1.1  dholland  * Enable an EA using the passed filesystem, backing vnode, attribute name,
    499  1.1  dholland  * namespace, and proc.  Will perform a VOP_OPEN() on the vp, so expects vp
    500  1.1  dholland  * to be locked when passed in.  The vnode will be returned unlocked,
    501  1.1  dholland  * regardless of success/failure of the function.  As a result, the caller
    502  1.1  dholland  * will always need to vrele(), but not vput().
    503  1.1  dholland  */
    504  1.1  dholland static int
    505  1.4  dholland ulfs_extattr_enable_with_open(struct ulfsmount *ump, struct vnode *vp,
    506  1.1  dholland     int attrnamespace, const char *attrname, struct lwp *l)
    507  1.1  dholland {
    508  1.1  dholland 	int error;
    509  1.1  dholland 
    510  1.1  dholland 	error = VOP_OPEN(vp, FREAD|FWRITE, l->l_cred);
    511  1.1  dholland 	if (error) {
    512  1.4  dholland 		printf("ulfs_extattr_enable_with_open.VOP_OPEN(): failed "
    513  1.1  dholland 		    "with %d\n", error);
    514  1.1  dholland 		VOP_UNLOCK(vp);
    515  1.1  dholland 		return (error);
    516  1.1  dholland 	}
    517  1.1  dholland 
    518  1.1  dholland 	mutex_enter(vp->v_interlock);
    519  1.1  dholland 	vp->v_writecount++;
    520  1.1  dholland 	mutex_exit(vp->v_interlock);
    521  1.1  dholland 
    522  1.1  dholland 	vref(vp);
    523  1.1  dholland 
    524  1.1  dholland 	VOP_UNLOCK(vp);
    525  1.1  dholland 
    526  1.4  dholland 	error = ulfs_extattr_enable(ump, attrnamespace, attrname, vp, l);
    527  1.1  dholland 	if (error != 0)
    528  1.1  dholland 		vn_close(vp, FREAD|FWRITE, l->l_cred);
    529  1.1  dholland 	return (error);
    530  1.1  dholland }
    531  1.1  dholland 
    532  1.1  dholland /*
    533  1.1  dholland  * Given a locked directory vnode, iterate over the names in the directory
    534  1.4  dholland  * and use ulfs_extattr_lookup() to retrieve locked vnodes of potential
    535  1.4  dholland  * attribute files.  Then invoke ulfs_extattr_enable_with_open() on each
    536  1.1  dholland  * to attempt to start the attribute.  Leaves the directory locked on
    537  1.1  dholland  * exit.
    538  1.1  dholland  */
    539  1.1  dholland static int
    540  1.4  dholland ulfs_extattr_iterate_directory(struct ulfsmount *ump, struct vnode *dvp,
    541  1.1  dholland     int attrnamespace, struct lwp *l)
    542  1.1  dholland {
    543  1.1  dholland 	struct vop_readdir_args vargs;
    544  1.1  dholland 	struct statvfs *sbp = &ump->um_mountp->mnt_stat;
    545  1.1  dholland 	struct dirent *dp, *edp;
    546  1.1  dholland 	struct vnode *attr_vp;
    547  1.1  dholland 	struct uio auio;
    548  1.1  dholland 	struct iovec aiov;
    549  1.1  dholland 	char *dirbuf;
    550  1.1  dholland 	int error, eofflag = 0;
    551  1.1  dholland 
    552  1.1  dholland 	if (dvp->v_type != VDIR)
    553  1.1  dholland 		return (ENOTDIR);
    554  1.1  dholland 
    555  1.5  dholland 	dirbuf = kmem_alloc(LFS_DIRBLKSIZ, KM_SLEEP);
    556  1.1  dholland 
    557  1.1  dholland 	auio.uio_iov = &aiov;
    558  1.1  dholland 	auio.uio_iovcnt = 1;
    559  1.1  dholland 	auio.uio_rw = UIO_READ;
    560  1.1  dholland 	auio.uio_offset = 0;
    561  1.1  dholland 	UIO_SETUP_SYSSPACE(&auio);
    562  1.1  dholland 
    563  1.1  dholland 	vargs.a_desc = NULL;
    564  1.1  dholland 	vargs.a_vp = dvp;
    565  1.1  dholland 	vargs.a_uio = &auio;
    566  1.1  dholland 	vargs.a_cred = l->l_cred;
    567  1.1  dholland 	vargs.a_eofflag = &eofflag;
    568  1.1  dholland 	vargs.a_ncookies = NULL;
    569  1.1  dholland 	vargs.a_cookies = NULL;
    570  1.1  dholland 
    571  1.1  dholland 	while (!eofflag) {
    572  1.5  dholland 		auio.uio_resid = LFS_DIRBLKSIZ;
    573  1.1  dholland 		aiov.iov_base = dirbuf;
    574  1.5  dholland 		aiov.iov_len = LFS_DIRBLKSIZ;
    575  1.4  dholland 		error = ulfs_readdir(&vargs);
    576  1.1  dholland 		if (error) {
    577  1.4  dholland 			printf("ulfs_extattr_iterate_directory: ulfs_readdir "
    578  1.1  dholland 			    "%d\n", error);
    579  1.1  dholland 			return (error);
    580  1.1  dholland 		}
    581  1.1  dholland 
    582  1.1  dholland 		/*
    583  1.5  dholland 		 * XXXRW: While in LFS, we always get LFS_DIRBLKSIZ returns from
    584  1.1  dholland 		 * the directory code on success, on other file systems this
    585  1.1  dholland 		 * may not be the case.  For portability, we should check the
    586  1.4  dholland 		 * read length on return from ulfs_readdir().
    587  1.1  dholland 		 */
    588  1.5  dholland 		edp = (struct dirent *)&dirbuf[LFS_DIRBLKSIZ];
    589  1.1  dholland 		for (dp = (struct dirent *)dirbuf; dp < edp; ) {
    590  1.1  dholland 			if (dp->d_reclen == 0)
    591  1.1  dholland 				break;
    592  1.1  dholland 			/* Skip "." and ".." */
    593  1.1  dholland 			if (dp->d_name[0] == '.' &&
    594  1.1  dholland 			    (dp->d_name[1] == '\0' ||
    595  1.1  dholland 			     (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
    596  1.1  dholland 				goto next;
    597  1.4  dholland 			error = ulfs_extattr_lookup(dvp, LOCKPARENT,
    598  1.1  dholland 			    dp->d_name, &attr_vp, l);
    599  1.1  dholland 			if (error == ENOENT) {
    600  1.1  dholland 				goto next; /* keep silent */
    601  1.1  dholland 			} else if (error) {
    602  1.4  dholland 				printf("ulfs_extattr_iterate_directory: lookup "
    603  1.1  dholland 				    "%s %d\n", dp->d_name, error);
    604  1.1  dholland 			} else if (attr_vp == dvp) {
    605  1.1  dholland 				vrele(attr_vp);
    606  1.1  dholland 			} else if (attr_vp->v_type != VREG) {
    607  1.1  dholland 				vput(attr_vp);
    608  1.1  dholland 			} else {
    609  1.4  dholland 				error = ulfs_extattr_enable_with_open(ump,
    610  1.1  dholland 				    attr_vp, attrnamespace, dp->d_name, l);
    611  1.1  dholland 				vrele(attr_vp);
    612  1.1  dholland 				if (error) {
    613  1.4  dholland 					printf("ulfs_extattr_iterate_directory: "
    614  1.1  dholland 					    "enable %s %d\n", dp->d_name,
    615  1.1  dholland 					    error);
    616  1.1  dholland 				} else if (bootverbose) {
    617  1.1  dholland 					printf("%s: EA %s loaded\n",
    618  1.1  dholland 					       sbp->f_mntonname, dp->d_name);
    619  1.1  dholland 				}
    620  1.1  dholland 			}
    621  1.1  dholland  next:
    622  1.1  dholland 			dp = (struct dirent *) ((char *)dp + dp->d_reclen);
    623  1.1  dholland 			if (dp >= edp)
    624  1.1  dholland 				break;
    625  1.1  dholland 		}
    626  1.1  dholland 	}
    627  1.5  dholland 	kmem_free(dirbuf, LFS_DIRBLKSIZ);
    628  1.1  dholland 
    629  1.1  dholland 	return (0);
    630  1.1  dholland }
    631  1.1  dholland 
    632  1.1  dholland /*
    633  1.1  dholland  * Auto-start of extended attributes, to be executed (optionally) at
    634  1.1  dholland  * mount-time.
    635  1.1  dholland  */
    636  1.1  dholland int
    637  1.4  dholland ulfs_extattr_autostart(struct mount *mp, struct lwp *l)
    638  1.1  dholland {
    639  1.1  dholland 	struct vnode *rvp, *attr_dvp, *attr_system_dvp, *attr_user_dvp;
    640  1.1  dholland 	int error;
    641  1.1  dholland 
    642  1.1  dholland 	/*
    643  1.4  dholland 	 * Does ULFS_EXTATTR_FSROOTSUBDIR exist off the filesystem root?
    644  1.1  dholland 	 * If so, automatically start EA's.
    645  1.1  dholland 	 */
    646  1.1  dholland 	error = VFS_ROOT(mp, &rvp);
    647  1.1  dholland 	if (error) {
    648  1.4  dholland 		printf("ulfs_extattr_autostart.VFS_ROOT() returned %d\n",
    649  1.1  dholland 		    error);
    650  1.1  dholland 		return (error);
    651  1.1  dholland 	}
    652  1.1  dholland 
    653  1.1  dholland 	KASSERT(VOP_ISLOCKED(rvp) == LK_EXCLUSIVE);
    654  1.1  dholland 
    655  1.4  dholland 	error = ulfs_extattr_lookup(rvp, 0,
    656  1.4  dholland 	    ULFS_EXTATTR_FSROOTSUBDIR, &attr_dvp, l);
    657  1.1  dholland 	if (error) {
    658  1.1  dholland 		/* rvp ref'd but now unlocked */
    659  1.1  dholland 		KASSERT(VOP_ISLOCKED(rvp) == 0);
    660  1.1  dholland 		vrele(rvp);
    661  1.1  dholland 		return (error);
    662  1.1  dholland 	}
    663  1.1  dholland 	if (rvp == attr_dvp) {
    664  1.1  dholland 		/* Should never happen. */
    665  1.1  dholland 		KASSERT(VOP_ISLOCKED(rvp) == LK_EXCLUSIVE);
    666  1.1  dholland 		vrele(attr_dvp);
    667  1.1  dholland 		vput(rvp);
    668  1.1  dholland 		return (EINVAL);
    669  1.1  dholland 	}
    670  1.1  dholland 	KASSERT(VOP_ISLOCKED(rvp) == 0);
    671  1.1  dholland 	vrele(rvp);
    672  1.1  dholland 
    673  1.1  dholland 	KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE);
    674  1.1  dholland 
    675  1.1  dholland 	if (attr_dvp->v_type != VDIR) {
    676  1.4  dholland 		printf("ulfs_extattr_autostart: %s != VDIR\n",
    677  1.4  dholland 		    ULFS_EXTATTR_FSROOTSUBDIR);
    678  1.1  dholland 		goto return_vput_attr_dvp;
    679  1.1  dholland 	}
    680  1.1  dholland 
    681  1.4  dholland 	error = ulfs_extattr_start(mp, l);
    682  1.1  dholland 	if (error) {
    683  1.4  dholland 		printf("ulfs_extattr_autostart: ulfs_extattr_start failed (%d)\n",
    684  1.1  dholland 		    error);
    685  1.1  dholland 		goto return_vput_attr_dvp;
    686  1.1  dholland 	}
    687  1.1  dholland 
    688  1.1  dholland 	/*
    689  1.4  dholland 	 * Look for two subdirectories: ULFS_EXTATTR_SUBDIR_SYSTEM,
    690  1.4  dholland 	 * ULFS_EXTATTR_SUBDIR_USER.  For each, iterate over the sub-directory,
    691  1.1  dholland 	 * and start with appropriate type.  Failures in either don't
    692  1.1  dholland 	 * result in an over-all failure.  attr_dvp is left locked to
    693  1.1  dholland 	 * be cleaned up on exit.
    694  1.1  dholland 	 */
    695  1.4  dholland 	error = ulfs_extattr_lookup(attr_dvp, LOCKPARENT,
    696  1.4  dholland 	    ULFS_EXTATTR_SUBDIR_SYSTEM, &attr_system_dvp, l);
    697  1.1  dholland 	KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE);
    698  1.1  dholland 	if (error == 0) {
    699  1.1  dholland 		KASSERT(VOP_ISLOCKED(attr_system_dvp) == LK_EXCLUSIVE);
    700  1.4  dholland 		error = ulfs_extattr_iterate_directory(VFSTOULFS(mp),
    701  1.1  dholland 		    attr_system_dvp, EXTATTR_NAMESPACE_SYSTEM, l);
    702  1.1  dholland 		if (error)
    703  1.4  dholland 			printf("ulfs_extattr_iterate_directory returned %d\n",
    704  1.1  dholland 			    error);
    705  1.1  dholland 		KASSERT(VOP_ISLOCKED(attr_system_dvp) == LK_EXCLUSIVE);
    706  1.1  dholland 		vput(attr_system_dvp);
    707  1.1  dholland 	}
    708  1.1  dholland 
    709  1.4  dholland 	error = ulfs_extattr_lookup(attr_dvp, LOCKPARENT,
    710  1.4  dholland 	    ULFS_EXTATTR_SUBDIR_USER, &attr_user_dvp, l);
    711  1.1  dholland 	KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE);
    712  1.1  dholland 	if (error == 0) {
    713  1.1  dholland 		KASSERT(VOP_ISLOCKED(attr_user_dvp) == LK_EXCLUSIVE);
    714  1.4  dholland 		error = ulfs_extattr_iterate_directory(VFSTOULFS(mp),
    715  1.1  dholland 		    attr_user_dvp, EXTATTR_NAMESPACE_USER, l);
    716  1.1  dholland 		if (error)
    717  1.4  dholland 			printf("ulfs_extattr_iterate_directory returned %d\n",
    718  1.1  dholland 			    error);
    719  1.1  dholland 		KASSERT(VOP_ISLOCKED(attr_user_dvp) == LK_EXCLUSIVE);
    720  1.1  dholland 		vput(attr_user_dvp);
    721  1.1  dholland 	}
    722  1.1  dholland 
    723  1.1  dholland 	/* Mask startup failures in sub-directories. */
    724  1.1  dholland 	error = 0;
    725  1.1  dholland 
    726  1.1  dholland  return_vput_attr_dvp:
    727  1.1  dholland 	KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE);
    728  1.1  dholland 	vput(attr_dvp);
    729  1.1  dholland 
    730  1.1  dholland 	return (error);
    731  1.1  dholland }
    732  1.1  dholland 
    733  1.1  dholland /*
    734  1.1  dholland  * Stop extended attribute support on an FS.
    735  1.1  dholland  */
    736  1.1  dholland void
    737  1.4  dholland ulfs_extattr_stop(struct mount *mp, struct lwp *l)
    738  1.1  dholland {
    739  1.4  dholland 	struct ulfs_extattr_list_entry *uele;
    740  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    741  1.1  dholland 
    742  1.4  dholland 	ulfs_extattr_uepm_lock(ump);
    743  1.1  dholland 
    744  1.1  dholland 	/*
    745  1.1  dholland 	 * If we haven't been started, no big deal.  Just short-circuit
    746  1.1  dholland 	 * the processing work.
    747  1.1  dholland 	 */
    748  1.4  dholland 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) {
    749  1.1  dholland 		goto unlock;
    750  1.1  dholland 	}
    751  1.1  dholland 
    752  1.1  dholland 	while (LIST_FIRST(&ump->um_extattr.uepm_list) != NULL) {
    753  1.1  dholland 		uele = LIST_FIRST(&ump->um_extattr.uepm_list);
    754  1.4  dholland 		ulfs_extattr_disable(ump, uele->uele_attrnamespace,
    755  1.1  dholland 		    uele->uele_attrname, l);
    756  1.1  dholland 	}
    757  1.1  dholland 
    758  1.4  dholland 	ump->um_extattr.uepm_flags &= ~ULFS_EXTATTR_UEPM_STARTED;
    759  1.1  dholland 
    760  1.1  dholland 	kauth_cred_free(ump->um_extattr.uepm_ucred);
    761  1.1  dholland 	ump->um_extattr.uepm_ucred = NULL;
    762  1.1  dholland 
    763  1.1  dholland  unlock:
    764  1.4  dholland 	ulfs_extattr_uepm_unlock(ump);
    765  1.1  dholland }
    766  1.1  dholland 
    767  1.1  dholland /*
    768  1.1  dholland  * Enable a named attribute on the specified filesystem; provide an
    769  1.1  dholland  * unlocked backing vnode to hold the attribute data.
    770  1.1  dholland  */
    771  1.1  dholland static int
    772  1.4  dholland ulfs_extattr_enable(struct ulfsmount *ump, int attrnamespace,
    773  1.1  dholland     const char *attrname, struct vnode *backing_vnode, struct lwp *l)
    774  1.1  dholland {
    775  1.4  dholland 	struct ulfs_extattr_list_entry *attribute;
    776  1.1  dholland 	struct iovec aiov;
    777  1.1  dholland 	struct uio auio;
    778  1.1  dholland 	int error = 0;
    779  1.1  dholland 
    780  1.4  dholland 	if (!ulfs_extattr_valid_attrname(attrnamespace, attrname))
    781  1.1  dholland 		return (EINVAL);
    782  1.1  dholland 	if (backing_vnode->v_type != VREG)
    783  1.1  dholland 		return (EINVAL);
    784  1.1  dholland 
    785  1.1  dholland 	attribute = kmem_zalloc(sizeof(*attribute), KM_SLEEP);
    786  1.1  dholland 
    787  1.4  dholland 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) {
    788  1.1  dholland 		error = EOPNOTSUPP;
    789  1.1  dholland 		goto free_exit;
    790  1.1  dholland 	}
    791  1.1  dholland 
    792  1.4  dholland 	if (ulfs_extattr_find_attr(ump, attrnamespace, attrname)) {
    793  1.1  dholland 		error = EEXIST;
    794  1.1  dholland 		goto free_exit;
    795  1.1  dholland 	}
    796  1.1  dholland 
    797  1.1  dholland 	strncpy(attribute->uele_attrname, attrname,
    798  1.4  dholland 	    ULFS_EXTATTR_MAXEXTATTRNAME);
    799  1.1  dholland 	attribute->uele_attrnamespace = attrnamespace;
    800  1.1  dholland 	memset(&attribute->uele_fileheader, 0,
    801  1.4  dholland 	    sizeof(struct ulfs_extattr_fileheader));
    802  1.1  dholland 
    803  1.1  dholland 	attribute->uele_backing_vnode = backing_vnode;
    804  1.1  dholland 
    805  1.1  dholland 	auio.uio_iov = &aiov;
    806  1.1  dholland 	auio.uio_iovcnt = 1;
    807  1.1  dholland 	aiov.iov_base = (void *) &attribute->uele_fileheader;
    808  1.4  dholland 	aiov.iov_len = sizeof(struct ulfs_extattr_fileheader);
    809  1.4  dholland 	auio.uio_resid = sizeof(struct ulfs_extattr_fileheader);
    810  1.1  dholland 	auio.uio_offset = (off_t) 0;
    811  1.1  dholland 	auio.uio_rw = UIO_READ;
    812  1.1  dholland 	UIO_SETUP_SYSSPACE(&auio);
    813  1.1  dholland 
    814  1.1  dholland 	vn_lock(backing_vnode, LK_SHARED | LK_RETRY);
    815  1.1  dholland 	error = VOP_READ(backing_vnode, &auio, IO_NODELOCKED,
    816  1.1  dholland 	    ump->um_extattr.uepm_ucred);
    817  1.1  dholland 
    818  1.1  dholland 	if (error)
    819  1.1  dholland 		goto unlock_free_exit;
    820  1.1  dholland 
    821  1.1  dholland 	if (auio.uio_resid != 0) {
    822  1.4  dholland 		printf("ulfs_extattr_enable: malformed attribute header\n");
    823  1.1  dholland 		error = EINVAL;
    824  1.1  dholland 		goto unlock_free_exit;
    825  1.1  dholland 	}
    826  1.1  dholland 
    827  1.1  dholland 	/*
    828  1.1  dholland 	 * Try to determine the byte order of the attribute file.
    829  1.1  dholland 	 */
    830  1.4  dholland 	if (attribute->uele_fileheader.uef_magic != ULFS_EXTATTR_MAGIC) {
    831  1.1  dholland 		attribute->uele_flags |= UELE_F_NEEDSWAP;
    832  1.1  dholland 		attribute->uele_fileheader.uef_magic =
    833  1.4  dholland 		    ulfs_rw32(attribute->uele_fileheader.uef_magic,
    834  1.1  dholland 			     UELE_NEEDSWAP(attribute));
    835  1.4  dholland 		if (attribute->uele_fileheader.uef_magic != ULFS_EXTATTR_MAGIC) {
    836  1.4  dholland 			printf("ulfs_extattr_enable: invalid attribute header "
    837  1.1  dholland 			       "magic\n");
    838  1.1  dholland 			error = EINVAL;
    839  1.1  dholland 			goto unlock_free_exit;
    840  1.1  dholland 		}
    841  1.1  dholland 	}
    842  1.1  dholland 	attribute->uele_fileheader.uef_version =
    843  1.4  dholland 	    ulfs_rw32(attribute->uele_fileheader.uef_version,
    844  1.1  dholland 		     UELE_NEEDSWAP(attribute));
    845  1.1  dholland 	attribute->uele_fileheader.uef_size =
    846  1.4  dholland 	    ulfs_rw32(attribute->uele_fileheader.uef_size,
    847  1.1  dholland 		     UELE_NEEDSWAP(attribute));
    848  1.1  dholland 
    849  1.4  dholland 	if (attribute->uele_fileheader.uef_version != ULFS_EXTATTR_VERSION) {
    850  1.4  dholland 		printf("ulfs_extattr_enable: incorrect attribute header "
    851  1.1  dholland 		    "version\n");
    852  1.1  dholland 		error = EINVAL;
    853  1.1  dholland 		goto unlock_free_exit;
    854  1.1  dholland 	}
    855  1.1  dholland 
    856  1.1  dholland 	LIST_INSERT_HEAD(&ump->um_extattr.uepm_list, attribute,
    857  1.1  dholland 	    uele_entries);
    858  1.1  dholland 
    859  1.1  dholland 	VOP_UNLOCK(backing_vnode);
    860  1.1  dholland 	return (0);
    861  1.1  dholland 
    862  1.1  dholland  unlock_free_exit:
    863  1.1  dholland 	VOP_UNLOCK(backing_vnode);
    864  1.1  dholland 
    865  1.1  dholland  free_exit:
    866  1.1  dholland 	kmem_free(attribute, sizeof(*attribute));
    867  1.1  dholland 	return (error);
    868  1.1  dholland }
    869  1.1  dholland 
    870  1.1  dholland /*
    871  1.1  dholland  * Disable extended attribute support on an FS.
    872  1.1  dholland  */
    873  1.1  dholland static int
    874  1.4  dholland ulfs_extattr_disable(struct ulfsmount *ump, int attrnamespace,
    875  1.1  dholland     const char *attrname, struct lwp *l)
    876  1.1  dholland {
    877  1.4  dholland 	struct ulfs_extattr_list_entry *uele;
    878  1.1  dholland 	int error = 0;
    879  1.1  dholland 
    880  1.4  dholland 	if (!ulfs_extattr_valid_attrname(attrnamespace, attrname))
    881  1.1  dholland 		return (EINVAL);
    882  1.1  dholland 
    883  1.4  dholland 	uele = ulfs_extattr_find_attr(ump, attrnamespace, attrname);
    884  1.1  dholland 	if (!uele)
    885  1.1  dholland 		return (ENODATA);
    886  1.1  dholland 
    887  1.1  dholland 	LIST_REMOVE(uele, uele_entries);
    888  1.1  dholland 
    889  1.1  dholland 	error = vn_close(uele->uele_backing_vnode, FREAD|FWRITE,
    890  1.1  dholland 	    l->l_cred);
    891  1.1  dholland 
    892  1.1  dholland 	kmem_free(uele, sizeof(*uele));
    893  1.1  dholland 
    894  1.1  dholland 	return (error);
    895  1.1  dholland }
    896  1.1  dholland 
    897  1.1  dholland /*
    898  1.4  dholland  * VFS call to manage extended attributes in ULFS.  If filename_vp is
    899  1.1  dholland  * non-NULL, it must be passed in locked, and regardless of errors in
    900  1.1  dholland  * processing, will be unlocked.
    901  1.1  dholland  */
    902  1.1  dholland int
    903  1.4  dholland ulfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
    904  1.1  dholland     int attrnamespace, const char *attrname)
    905  1.1  dholland {
    906  1.1  dholland 	struct lwp *l = curlwp;
    907  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    908  1.1  dholland 	int error;
    909  1.1  dholland 
    910  1.1  dholland 	/*
    911  1.1  dholland 	 * Only privileged processes can configure extended attributes.
    912  1.1  dholland 	 */
    913  1.1  dholland 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_EXTATTR,
    914  1.1  dholland 	    0, mp, NULL, NULL);
    915  1.1  dholland 	if (error) {
    916  1.1  dholland 		if (filename_vp != NULL)
    917  1.1  dholland 			VOP_UNLOCK(filename_vp);
    918  1.1  dholland 		return (error);
    919  1.1  dholland 	}
    920  1.1  dholland 
    921  1.1  dholland 	switch(cmd) {
    922  1.4  dholland 	case ULFS_EXTATTR_CMD_START:
    923  1.1  dholland 		if (filename_vp != NULL) {
    924  1.1  dholland 			VOP_UNLOCK(filename_vp);
    925  1.1  dholland 			return (EINVAL);
    926  1.1  dholland 		}
    927  1.1  dholland 		if (attrname != NULL)
    928  1.1  dholland 			return (EINVAL);
    929  1.1  dholland 
    930  1.4  dholland 		error = ulfs_extattr_autostart(mp, l);
    931  1.1  dholland 		return (error);
    932  1.1  dholland 
    933  1.4  dholland 	case ULFS_EXTATTR_CMD_STOP:
    934  1.1  dholland 		if (filename_vp != NULL) {
    935  1.1  dholland 			VOP_UNLOCK(filename_vp);
    936  1.1  dholland 			return (EINVAL);
    937  1.1  dholland 		}
    938  1.1  dholland 		if (attrname != NULL)
    939  1.1  dholland 			return (EINVAL);
    940  1.1  dholland 
    941  1.4  dholland 		ulfs_extattr_stop(mp, l);
    942  1.1  dholland 		return (0);
    943  1.1  dholland 
    944  1.4  dholland 	case ULFS_EXTATTR_CMD_ENABLE:
    945  1.1  dholland 		if (filename_vp == NULL)
    946  1.1  dholland 			return (EINVAL);
    947  1.1  dholland 		if (attrname == NULL) {
    948  1.1  dholland 			VOP_UNLOCK(filename_vp);
    949  1.1  dholland 			return (EINVAL);
    950  1.1  dholland 		}
    951  1.1  dholland 
    952  1.1  dholland 		/*
    953  1.4  dholland 		 * ulfs_extattr_enable_with_open() will always unlock the
    954  1.1  dholland 		 * vnode, regardless of failure.
    955  1.1  dholland 		 */
    956  1.4  dholland 		ulfs_extattr_uepm_lock(ump);
    957  1.4  dholland 		error = ulfs_extattr_enable_with_open(ump, filename_vp,
    958  1.1  dholland 		    attrnamespace, attrname, l);
    959  1.4  dholland 		ulfs_extattr_uepm_unlock(ump);
    960  1.1  dholland 		return (error);
    961  1.1  dholland 
    962  1.4  dholland 	case ULFS_EXTATTR_CMD_DISABLE:
    963  1.1  dholland 		if (filename_vp != NULL) {
    964  1.1  dholland 			VOP_UNLOCK(filename_vp);
    965  1.1  dholland 			return (EINVAL);
    966  1.1  dholland 		}
    967  1.1  dholland 		if (attrname == NULL)
    968  1.1  dholland 			return (EINVAL);
    969  1.1  dholland 
    970  1.4  dholland 		ulfs_extattr_uepm_lock(ump);
    971  1.4  dholland 		error = ulfs_extattr_disable(ump, attrnamespace, attrname, l);
    972  1.4  dholland 		ulfs_extattr_uepm_unlock(ump);
    973  1.1  dholland 		return (error);
    974  1.1  dholland 
    975  1.1  dholland 	default:
    976  1.1  dholland 		return (EINVAL);
    977  1.1  dholland 	}
    978  1.1  dholland }
    979  1.1  dholland 
    980  1.1  dholland /*
    981  1.1  dholland  * Read extended attribute header for a given vnode and attribute.
    982  1.1  dholland  * Backing vnode should be locked and unlocked by caller.
    983  1.1  dholland  */
    984  1.1  dholland static int
    985  1.4  dholland ulfs_extattr_get_header(struct vnode *vp, struct ulfs_extattr_list_entry *uele,
    986  1.4  dholland     struct ulfs_extattr_header *ueh, off_t *bap)
    987  1.1  dholland {
    988  1.1  dholland 	struct mount *mp = vp->v_mount;
    989  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
    990  1.1  dholland 	struct inode *ip = VTOI(vp);
    991  1.1  dholland 	off_t base_offset;
    992  1.1  dholland 	struct iovec aiov;
    993  1.1  dholland 	struct uio aio;
    994  1.1  dholland 	int error;
    995  1.1  dholland 
    996  1.1  dholland 	/*
    997  1.1  dholland 	 * Find base offset of header in file based on file header size, and
    998  1.1  dholland 	 * data header size + maximum data size, indexed by inode number.
    999  1.1  dholland 	 */
   1000  1.4  dholland 	base_offset = sizeof(struct ulfs_extattr_fileheader) +
   1001  1.4  dholland 	    ip->i_number * (sizeof(struct ulfs_extattr_header) +
   1002  1.1  dholland 	    uele->uele_fileheader.uef_size);
   1003  1.1  dholland 
   1004  1.1  dholland 	/*
   1005  1.1  dholland 	 * Read in the data header to see if the data is defined, and if so
   1006  1.1  dholland 	 * how much.
   1007  1.1  dholland 	 */
   1008  1.4  dholland 	memset(ueh, 0, sizeof(struct ulfs_extattr_header));
   1009  1.1  dholland 	aiov.iov_base = ueh;
   1010  1.4  dholland 	aiov.iov_len = sizeof(struct ulfs_extattr_header);
   1011  1.1  dholland 	aio.uio_iov = &aiov;
   1012  1.1  dholland 	aio.uio_iovcnt = 1;
   1013  1.1  dholland 	aio.uio_rw = UIO_READ;
   1014  1.1  dholland 	aio.uio_offset = base_offset;
   1015  1.4  dholland 	aio.uio_resid = sizeof(struct ulfs_extattr_header);
   1016  1.1  dholland 	UIO_SETUP_SYSSPACE(&aio);
   1017  1.1  dholland 
   1018  1.1  dholland 	error = VOP_READ(uele->uele_backing_vnode, &aio,
   1019  1.1  dholland 	    IO_NODELOCKED, ump->um_extattr.uepm_ucred);
   1020  1.1  dholland 	if (error)
   1021  1.1  dholland 		return error;
   1022  1.1  dholland 
   1023  1.1  dholland 	/*
   1024  1.1  dholland 	 * Attribute headers are kept in file system byte order.
   1025  1.1  dholland 	 * XXX What about the blob of data?
   1026  1.1  dholland 	 */
   1027  1.4  dholland 	ueh->ueh_flags = ulfs_rw32(ueh->ueh_flags, UELE_NEEDSWAP(uele));
   1028  1.4  dholland 	ueh->ueh_len   = ulfs_rw32(ueh->ueh_len, UELE_NEEDSWAP(uele));
   1029  1.4  dholland 	ueh->ueh_i_gen = ulfs_rw32(ueh->ueh_i_gen, UELE_NEEDSWAP(uele));
   1030  1.1  dholland 
   1031  1.1  dholland 	/* Defined? */
   1032  1.4  dholland 	if ((ueh->ueh_flags & ULFS_EXTATTR_ATTR_FLAG_INUSE) == 0)
   1033  1.1  dholland 		return ENODATA;
   1034  1.1  dholland 
   1035  1.1  dholland 	/* Valid for the current inode generation? */
   1036  1.1  dholland 	if (ueh->ueh_i_gen != ip->i_gen) {
   1037  1.1  dholland 		/*
   1038  1.1  dholland 		 * The inode itself has a different generation number
   1039  1.1  dholland 		 * than the uele data.  For now, the best solution
   1040  1.1  dholland 		 * is to coerce this to undefined, and let it get cleaned
   1041  1.1  dholland 		 * up by the next write or extattrctl clean.
   1042  1.1  dholland 		 */
   1043  1.1  dholland 		printf("%s (%s): inode gen inconsistency (%u, %jd)\n",
   1044  1.1  dholland 		       __func__,  mp->mnt_stat.f_mntonname, ueh->ueh_i_gen,
   1045  1.1  dholland 		       (intmax_t)ip->i_gen);
   1046  1.1  dholland 		return ENODATA;
   1047  1.1  dholland 	}
   1048  1.1  dholland 
   1049  1.1  dholland 	/* Local size consistency check. */
   1050  1.1  dholland 	if (ueh->ueh_len > uele->uele_fileheader.uef_size)
   1051  1.1  dholland 		return ENXIO;
   1052  1.1  dholland 
   1053  1.1  dholland 	/* Return base offset */
   1054  1.1  dholland 	if (bap != NULL)
   1055  1.1  dholland 		*bap = base_offset;
   1056  1.1  dholland 
   1057  1.1  dholland 	return 0;
   1058  1.1  dholland }
   1059  1.1  dholland 
   1060  1.1  dholland /*
   1061  1.1  dholland  * Vnode operation to retrieve a named extended attribute.
   1062  1.1  dholland  */
   1063  1.1  dholland int
   1064  1.4  dholland ulfs_getextattr(struct vop_getextattr_args *ap)
   1065  1.1  dholland /*
   1066  1.1  dholland vop_getextattr {
   1067  1.1  dholland 	IN struct vnode *a_vp;
   1068  1.1  dholland 	IN int a_attrnamespace;
   1069  1.1  dholland 	IN const char *a_name;
   1070  1.1  dholland 	INOUT struct uio *a_uio;
   1071  1.1  dholland 	OUT size_t *a_size;
   1072  1.1  dholland 	IN kauth_cred_t a_cred;
   1073  1.1  dholland };
   1074  1.1  dholland */
   1075  1.1  dholland {
   1076  1.1  dholland 	struct mount *mp = ap->a_vp->v_mount;
   1077  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
   1078  1.1  dholland 	int error;
   1079  1.1  dholland 
   1080  1.4  dholland 	ulfs_extattr_uepm_lock(ump);
   1081  1.1  dholland 
   1082  1.4  dholland 	error = ulfs_extattr_get(ap->a_vp, ap->a_attrnamespace, ap->a_name,
   1083  1.1  dholland 	    ap->a_uio, ap->a_size, ap->a_cred, curlwp);
   1084  1.1  dholland 
   1085  1.4  dholland 	ulfs_extattr_uepm_unlock(ump);
   1086  1.1  dholland 
   1087  1.1  dholland 	return (error);
   1088  1.1  dholland }
   1089  1.1  dholland 
   1090  1.1  dholland /*
   1091  1.1  dholland  * Real work associated with retrieving a named attribute--assumes that
   1092  1.1  dholland  * the attribute lock has already been grabbed.
   1093  1.1  dholland  */
   1094  1.1  dholland static int
   1095  1.4  dholland ulfs_extattr_get(struct vnode *vp, int attrnamespace, const char *name,
   1096  1.1  dholland     struct uio *uio, size_t *size, kauth_cred_t cred, struct lwp *l)
   1097  1.1  dholland {
   1098  1.4  dholland 	struct ulfs_extattr_list_entry *attribute;
   1099  1.4  dholland 	struct ulfs_extattr_header ueh;
   1100  1.1  dholland 	struct mount *mp = vp->v_mount;
   1101  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
   1102  1.1  dholland 	off_t base_offset;
   1103  1.1  dholland 	size_t len, old_len;
   1104  1.1  dholland 	int error = 0;
   1105  1.1  dholland 
   1106  1.4  dholland 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED))
   1107  1.1  dholland 		return (EOPNOTSUPP);
   1108  1.1  dholland 
   1109  1.1  dholland 	if (strlen(name) == 0)
   1110  1.1  dholland 		return (EINVAL);
   1111  1.1  dholland 
   1112  1.1  dholland 	error = internal_extattr_check_cred(vp, attrnamespace, name, cred,
   1113  1.1  dholland 	    VREAD);
   1114  1.1  dholland 	if (error)
   1115  1.1  dholland 		return (error);
   1116  1.1  dholland 
   1117  1.4  dholland 	attribute = ulfs_extattr_find_attr(ump, attrnamespace, name);
   1118  1.1  dholland 	if (!attribute)
   1119  1.1  dholland 		return (ENODATA);
   1120  1.1  dholland 
   1121  1.1  dholland 	/*
   1122  1.1  dholland 	 * Allow only offsets of zero to encourage the read/replace
   1123  1.1  dholland 	 * extended attribute semantic.  Otherwise we can't guarantee
   1124  1.1  dholland 	 * atomicity, as we don't provide locks for extended attributes.
   1125  1.1  dholland 	 */
   1126  1.1  dholland 	if (uio != NULL && uio->uio_offset != 0)
   1127  1.1  dholland 		return (ENXIO);
   1128  1.1  dholland 
   1129  1.1  dholland 	/*
   1130  1.1  dholland 	 * Don't need to get a lock on the backing file if the getattr is
   1131  1.1  dholland 	 * being applied to the backing file, as the lock is already held.
   1132  1.1  dholland 	 */
   1133  1.1  dholland 	if (attribute->uele_backing_vnode != vp)
   1134  1.1  dholland 		vn_lock(attribute->uele_backing_vnode, LK_SHARED | LK_RETRY);
   1135  1.1  dholland 
   1136  1.4  dholland 	error = ulfs_extattr_get_header(vp, attribute, &ueh, &base_offset);
   1137  1.1  dholland 	if (error)
   1138  1.1  dholland 		goto vopunlock_exit;
   1139  1.1  dholland 
   1140  1.1  dholland 	/* Return full data size if caller requested it. */
   1141  1.1  dholland 	if (size != NULL)
   1142  1.1  dholland 		*size = ueh.ueh_len;
   1143  1.1  dholland 
   1144  1.1  dholland 	/* Return data if the caller requested it. */
   1145  1.1  dholland 	if (uio != NULL) {
   1146  1.1  dholland 		/* Allow for offset into the attribute data. */
   1147  1.1  dholland 		uio->uio_offset = base_offset + sizeof(struct
   1148  1.4  dholland 		    ulfs_extattr_header);
   1149  1.1  dholland 
   1150  1.1  dholland 		/*
   1151  1.1  dholland 		 * Figure out maximum to transfer -- use buffer size and
   1152  1.1  dholland 		 * local data limit.
   1153  1.1  dholland 		 */
   1154  1.1  dholland 		len = MIN(uio->uio_resid, ueh.ueh_len);
   1155  1.1  dholland 		old_len = uio->uio_resid;
   1156  1.1  dholland 		uio->uio_resid = len;
   1157  1.1  dholland 
   1158  1.1  dholland 		error = VOP_READ(attribute->uele_backing_vnode, uio,
   1159  1.1  dholland 		    IO_NODELOCKED, ump->um_extattr.uepm_ucred);
   1160  1.1  dholland 		if (error)
   1161  1.1  dholland 			goto vopunlock_exit;
   1162  1.1  dholland 
   1163  1.1  dholland 		uio->uio_resid = old_len - (len - uio->uio_resid);
   1164  1.1  dholland 	}
   1165  1.1  dholland 
   1166  1.1  dholland  vopunlock_exit:
   1167  1.1  dholland 
   1168  1.1  dholland 	if (uio != NULL)
   1169  1.1  dholland 		uio->uio_offset = 0;
   1170  1.1  dholland 
   1171  1.1  dholland 	if (attribute->uele_backing_vnode != vp)
   1172  1.1  dholland 		VOP_UNLOCK(attribute->uele_backing_vnode);
   1173  1.1  dholland 
   1174  1.1  dholland 	return (error);
   1175  1.1  dholland }
   1176  1.1  dholland 
   1177  1.1  dholland /*
   1178  1.1  dholland  * Vnode operation to list extended attribute for a vnode
   1179  1.1  dholland  */
   1180  1.1  dholland int
   1181  1.4  dholland ulfs_listextattr(struct vop_listextattr_args *ap)
   1182  1.1  dholland /*
   1183  1.1  dholland vop_listextattr {
   1184  1.1  dholland 	IN struct vnode *a_vp;
   1185  1.1  dholland 	IN int a_attrnamespace;
   1186  1.1  dholland 	INOUT struct uio *a_uio;
   1187  1.1  dholland 	OUT size_t *a_size;
   1188  1.1  dholland 	IN int flag;
   1189  1.1  dholland 	IN kauth_cred_t a_cred;
   1190  1.1  dholland 	struct proc *a_p;
   1191  1.1  dholland };
   1192  1.1  dholland */
   1193  1.1  dholland {
   1194  1.1  dholland 	struct mount *mp = ap->a_vp->v_mount;
   1195  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
   1196  1.1  dholland 	int error;
   1197  1.1  dholland 
   1198  1.4  dholland 	ulfs_extattr_uepm_lock(ump);
   1199  1.1  dholland 
   1200  1.4  dholland 	error = ulfs_extattr_list(ap->a_vp, ap->a_attrnamespace,
   1201  1.1  dholland 	    ap->a_uio, ap->a_size, ap->a_flag, ap->a_cred, curlwp);
   1202  1.1  dholland 
   1203  1.4  dholland 	ulfs_extattr_uepm_unlock(ump);
   1204  1.1  dholland 
   1205  1.1  dholland 	return (error);
   1206  1.1  dholland }
   1207  1.1  dholland 
   1208  1.1  dholland /*
   1209  1.1  dholland  * Real work associated with retrieving list of attributes--assumes that
   1210  1.1  dholland  * the attribute lock has already been grabbed.
   1211  1.1  dholland  */
   1212  1.1  dholland static int
   1213  1.4  dholland ulfs_extattr_list(struct vnode *vp, int attrnamespace,
   1214  1.1  dholland     struct uio *uio, size_t *size, int flag,
   1215  1.1  dholland     kauth_cred_t cred, struct lwp *l)
   1216  1.1  dholland {
   1217  1.4  dholland 	struct ulfs_extattr_list_entry *uele;
   1218  1.4  dholland 	struct ulfs_extattr_header ueh;
   1219  1.1  dholland 	struct mount *mp = vp->v_mount;
   1220  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
   1221  1.1  dholland 	size_t listsize = 0;
   1222  1.1  dholland 	int error = 0;
   1223  1.1  dholland 
   1224  1.4  dholland 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED))
   1225  1.1  dholland 		return (EOPNOTSUPP);
   1226  1.1  dholland 
   1227  1.1  dholland 	/*
   1228  1.1  dholland 	 * XXX: We can move this inside the loop and iterate on individual
   1229  1.1  dholland 	 *	attributes.
   1230  1.1  dholland 	 */
   1231  1.1  dholland 	error = internal_extattr_check_cred(vp, attrnamespace, "", cred,
   1232  1.1  dholland 	    VREAD);
   1233  1.1  dholland 	if (error)
   1234  1.1  dholland 		return (error);
   1235  1.1  dholland 
   1236  1.1  dholland 	LIST_FOREACH(uele, &ump->um_extattr.uepm_list, uele_entries) {
   1237  1.1  dholland 		unsigned char attrnamelen;
   1238  1.1  dholland 
   1239  1.1  dholland 		if (uele->uele_attrnamespace != attrnamespace)
   1240  1.1  dholland 			continue;
   1241  1.1  dholland 
   1242  1.4  dholland 		error = ulfs_extattr_get_header(vp, uele, &ueh, NULL);
   1243  1.1  dholland 		if (error == ENODATA)
   1244  1.1  dholland 			continue;
   1245  1.1  dholland 		if (error != 0)
   1246  1.1  dholland 			return error;
   1247  1.1  dholland 
   1248  1.1  dholland 		/*
   1249  1.1  dholland 		 * Don't need to get a lock on the backing file if
   1250  1.1  dholland 		 * the listattr is being applied to the backing file,
   1251  1.1  dholland 		 * as the lock is already held.
   1252  1.1  dholland 		 */
   1253  1.1  dholland 		if (uele->uele_backing_vnode != vp)
   1254  1.1  dholland 			vn_lock(uele->uele_backing_vnode, LK_SHARED | LK_RETRY);
   1255  1.1  dholland 
   1256  1.1  dholland 		/*
   1257  1.1  dholland 		 * +1 for trailing NUL (listxattr flavor)
   1258  1.1  dholland 		 *  or leading name length (extattr_list_file flavor)
   1259  1.1  dholland 	 	 */
   1260  1.1  dholland 		attrnamelen = strlen(uele->uele_attrname);
   1261  1.1  dholland 		listsize += attrnamelen + 1;
   1262  1.1  dholland 
   1263  1.1  dholland 		/* Return data if the caller requested it. */
   1264  1.1  dholland 		if (uio != NULL) {
   1265  1.1  dholland 			/*
   1266  1.1  dholland 			 * We support two flavors. Either NUL-terminated
   1267  1.1  dholland 			 * strings (a la listxattr), or non NUL-terminated,
   1268  1.1  dholland 			 * one byte length prefixed strings (for
   1269  1.1  dholland 			 * extattr_list_file). EXTATTR_LIST_LENPREFIX switches
   1270  1.1  dholland 		 	 * that second behavior.
   1271  1.1  dholland 			 */
   1272  1.1  dholland 			if (flag & EXTATTR_LIST_LENPREFIX) {
   1273  1.1  dholland 				uint8_t len = (uint8_t)attrnamelen;
   1274  1.1  dholland 
   1275  1.1  dholland 				/* Copy leading name length */
   1276  1.1  dholland 				error = uiomove(&len, sizeof(len), uio);
   1277  1.1  dholland 				if (error != 0)
   1278  1.1  dholland 					break;
   1279  1.1  dholland 			} else {
   1280  1.1  dholland 				/* Include trailing NULL */
   1281  1.1  dholland 				attrnamelen++;
   1282  1.1  dholland 			}
   1283  1.1  dholland 
   1284  1.1  dholland 			error = uiomove(uele->uele_attrname,
   1285  1.1  dholland 					(size_t)attrnamelen, uio);
   1286  1.1  dholland 			if (error != 0)
   1287  1.1  dholland 				break;
   1288  1.1  dholland 		}
   1289  1.1  dholland 
   1290  1.1  dholland 		if (uele->uele_backing_vnode != vp)
   1291  1.1  dholland 			VOP_UNLOCK(uele->uele_backing_vnode);
   1292  1.1  dholland 
   1293  1.1  dholland 		if (error != 0)
   1294  1.1  dholland 			return error;
   1295  1.1  dholland 	}
   1296  1.1  dholland 
   1297  1.1  dholland 	if (uio != NULL)
   1298  1.1  dholland 		uio->uio_offset = 0;
   1299  1.1  dholland 
   1300  1.1  dholland 	/* Return full data size if caller requested it. */
   1301  1.1  dholland 	if (size != NULL)
   1302  1.1  dholland 		*size = listsize;
   1303  1.1  dholland 
   1304  1.1  dholland 	return 0;
   1305  1.1  dholland }
   1306  1.1  dholland 
   1307  1.1  dholland /*
   1308  1.1  dholland  * Vnode operation to remove a named attribute.
   1309  1.1  dholland  */
   1310  1.1  dholland int
   1311  1.4  dholland ulfs_deleteextattr(struct vop_deleteextattr_args *ap)
   1312  1.1  dholland /*
   1313  1.1  dholland vop_deleteextattr {
   1314  1.1  dholland 	IN struct vnode *a_vp;
   1315  1.1  dholland 	IN int a_attrnamespace;
   1316  1.1  dholland 	IN const char *a_name;
   1317  1.1  dholland 	IN kauth_cred_t a_cred;
   1318  1.1  dholland };
   1319  1.1  dholland */
   1320  1.1  dholland {
   1321  1.1  dholland 	struct mount *mp = ap->a_vp->v_mount;
   1322  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
   1323  1.1  dholland 	int error;
   1324  1.1  dholland 
   1325  1.4  dholland 	ulfs_extattr_uepm_lock(ump);
   1326  1.1  dholland 
   1327  1.4  dholland 	error = ulfs_extattr_rm(ap->a_vp, ap->a_attrnamespace, ap->a_name,
   1328  1.1  dholland 	    ap->a_cred, curlwp);
   1329  1.1  dholland 
   1330  1.4  dholland 	ulfs_extattr_uepm_unlock(ump);
   1331  1.1  dholland 
   1332  1.1  dholland 	return (error);
   1333  1.1  dholland }
   1334  1.1  dholland 
   1335  1.1  dholland /*
   1336  1.1  dholland  * Vnode operation to set a named attribute.
   1337  1.1  dholland  */
   1338  1.1  dholland int
   1339  1.4  dholland ulfs_setextattr(struct vop_setextattr_args *ap)
   1340  1.1  dholland /*
   1341  1.1  dholland vop_setextattr {
   1342  1.1  dholland 	IN struct vnode *a_vp;
   1343  1.1  dholland 	IN int a_attrnamespace;
   1344  1.1  dholland 	IN const char *a_name;
   1345  1.1  dholland 	INOUT struct uio *a_uio;
   1346  1.1  dholland 	IN kauth_cred_t a_cred;
   1347  1.1  dholland };
   1348  1.1  dholland */
   1349  1.1  dholland {
   1350  1.1  dholland 	struct mount *mp = ap->a_vp->v_mount;
   1351  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
   1352  1.1  dholland 	int error;
   1353  1.1  dholland 
   1354  1.4  dholland 	ulfs_extattr_uepm_lock(ump);
   1355  1.1  dholland 
   1356  1.1  dholland 	/*
   1357  1.1  dholland 	 * XXX: No longer a supported way to delete extended attributes.
   1358  1.1  dholland 	 */
   1359  1.1  dholland 	if (ap->a_uio == NULL) {
   1360  1.4  dholland 		ulfs_extattr_uepm_unlock(ump);
   1361  1.1  dholland 		return (EINVAL);
   1362  1.1  dholland 	}
   1363  1.1  dholland 
   1364  1.4  dholland 	error = ulfs_extattr_set(ap->a_vp, ap->a_attrnamespace, ap->a_name,
   1365  1.1  dholland 	    ap->a_uio, ap->a_cred, curlwp);
   1366  1.1  dholland 
   1367  1.4  dholland 	ulfs_extattr_uepm_unlock(ump);
   1368  1.1  dholland 
   1369  1.1  dholland 	return (error);
   1370  1.1  dholland }
   1371  1.1  dholland 
   1372  1.1  dholland /*
   1373  1.1  dholland  * Real work associated with setting a vnode's extended attributes;
   1374  1.1  dholland  * assumes that the attribute lock has already been grabbed.
   1375  1.1  dholland  */
   1376  1.1  dholland static int
   1377  1.4  dholland ulfs_extattr_set(struct vnode *vp, int attrnamespace, const char *name,
   1378  1.1  dholland     struct uio *uio, kauth_cred_t cred, struct lwp *l)
   1379  1.1  dholland {
   1380  1.4  dholland 	struct ulfs_extattr_list_entry *attribute;
   1381  1.4  dholland 	struct ulfs_extattr_header ueh;
   1382  1.1  dholland 	struct iovec local_aiov;
   1383  1.1  dholland 	struct uio local_aio;
   1384  1.1  dholland 	struct mount *mp = vp->v_mount;
   1385  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
   1386  1.1  dholland 	struct inode *ip = VTOI(vp);
   1387  1.1  dholland 	off_t base_offset;
   1388  1.1  dholland 	int error = 0, ioflag;
   1389  1.1  dholland 
   1390  1.1  dholland 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1391  1.1  dholland 		return (EROFS);
   1392  1.4  dholland 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED))
   1393  1.1  dholland 		return (EOPNOTSUPP);
   1394  1.4  dholland 	if (!ulfs_extattr_valid_attrname(attrnamespace, name))
   1395  1.1  dholland 		return (EINVAL);
   1396  1.1  dholland 
   1397  1.1  dholland 	error = internal_extattr_check_cred(vp, attrnamespace, name, cred,
   1398  1.1  dholland 	    VWRITE);
   1399  1.1  dholland 	if (error)
   1400  1.1  dholland 		return (error);
   1401  1.1  dholland 
   1402  1.4  dholland 	attribute = ulfs_extattr_find_attr(ump, attrnamespace, name);
   1403  1.1  dholland 	if (!attribute) {
   1404  1.4  dholland 		attribute =  ulfs_extattr_autocreate_attr(vp, attrnamespace,
   1405  1.1  dholland 							 name, l);
   1406  1.1  dholland 		if  (!attribute)
   1407  1.1  dholland 			return (ENODATA);
   1408  1.1  dholland 	}
   1409  1.1  dholland 
   1410  1.1  dholland 	/*
   1411  1.1  dholland 	 * Early rejection of invalid offsets/length.
   1412  1.1  dholland 	 * Reject: any offset but 0 (replace)
   1413  1.1  dholland 	 *	 Any size greater than attribute size limit
   1414  1.1  dholland  	 */
   1415  1.1  dholland 	if (uio->uio_offset != 0 ||
   1416  1.1  dholland 	    uio->uio_resid > attribute->uele_fileheader.uef_size)
   1417  1.1  dholland 		return (ENXIO);
   1418  1.1  dholland 
   1419  1.1  dholland 	/*
   1420  1.1  dholland 	 * Find base offset of header in file based on file header size, and
   1421  1.1  dholland 	 * data header size + maximum data size, indexed by inode number.
   1422  1.1  dholland 	 */
   1423  1.4  dholland 	base_offset = sizeof(struct ulfs_extattr_fileheader) +
   1424  1.4  dholland 	    ip->i_number * (sizeof(struct ulfs_extattr_header) +
   1425  1.1  dholland 	    attribute->uele_fileheader.uef_size);
   1426  1.1  dholland 
   1427  1.1  dholland 	/*
   1428  1.1  dholland 	 * Write out a data header for the data.
   1429  1.1  dholland 	 */
   1430  1.4  dholland 	ueh.ueh_len = ulfs_rw32((uint32_t) uio->uio_resid,
   1431  1.1  dholland 	    UELE_NEEDSWAP(attribute));
   1432  1.4  dholland 	ueh.ueh_flags = ulfs_rw32(ULFS_EXTATTR_ATTR_FLAG_INUSE,
   1433  1.1  dholland 				 UELE_NEEDSWAP(attribute));
   1434  1.4  dholland 	ueh.ueh_i_gen = ulfs_rw32(ip->i_gen, UELE_NEEDSWAP(attribute));
   1435  1.1  dholland 	local_aiov.iov_base = &ueh;
   1436  1.4  dholland 	local_aiov.iov_len = sizeof(struct ulfs_extattr_header);
   1437  1.1  dholland 	local_aio.uio_iov = &local_aiov;
   1438  1.1  dholland 	local_aio.uio_iovcnt = 1;
   1439  1.1  dholland 	local_aio.uio_rw = UIO_WRITE;
   1440  1.1  dholland 	local_aio.uio_offset = base_offset;
   1441  1.4  dholland 	local_aio.uio_resid = sizeof(struct ulfs_extattr_header);
   1442  1.1  dholland 	UIO_SETUP_SYSSPACE(&local_aio);
   1443  1.1  dholland 
   1444  1.1  dholland 	/*
   1445  1.1  dholland 	 * Don't need to get a lock on the backing file if the setattr is
   1446  1.1  dholland 	 * being applied to the backing file, as the lock is already held.
   1447  1.1  dholland 	 */
   1448  1.1  dholland 	if (attribute->uele_backing_vnode != vp)
   1449  1.1  dholland 		vn_lock(attribute->uele_backing_vnode,
   1450  1.1  dholland 		    LK_EXCLUSIVE | LK_RETRY);
   1451  1.1  dholland 
   1452  1.1  dholland 	ioflag = IO_NODELOCKED;
   1453  1.4  dholland 	if (ulfs_extattr_sync)
   1454  1.1  dholland 		ioflag |= IO_SYNC;
   1455  1.1  dholland 	error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
   1456  1.1  dholland 	    ump->um_extattr.uepm_ucred);
   1457  1.1  dholland 	if (error)
   1458  1.1  dholland 		goto vopunlock_exit;
   1459  1.1  dholland 
   1460  1.1  dholland 	if (local_aio.uio_resid != 0) {
   1461  1.1  dholland 		error = ENXIO;
   1462  1.1  dholland 		goto vopunlock_exit;
   1463  1.1  dholland 	}
   1464  1.1  dholland 
   1465  1.1  dholland 	/*
   1466  1.1  dholland 	 * Write out user data.
   1467  1.1  dholland 	 * XXX NOT ATOMIC WITH RESPECT TO THE HEADER.
   1468  1.1  dholland 	 */
   1469  1.4  dholland 	uio->uio_offset = base_offset + sizeof(struct ulfs_extattr_header);
   1470  1.1  dholland 
   1471  1.1  dholland 	ioflag = IO_NODELOCKED;
   1472  1.4  dholland 	if (ulfs_extattr_sync)
   1473  1.1  dholland 		ioflag |= IO_SYNC;
   1474  1.1  dholland 	error = VOP_WRITE(attribute->uele_backing_vnode, uio, ioflag,
   1475  1.1  dholland 	    ump->um_extattr.uepm_ucred);
   1476  1.1  dholland 
   1477  1.1  dholland  vopunlock_exit:
   1478  1.1  dholland 	uio->uio_offset = 0;
   1479  1.1  dholland 
   1480  1.1  dholland 	if (attribute->uele_backing_vnode != vp)
   1481  1.1  dholland 		VOP_UNLOCK(attribute->uele_backing_vnode);
   1482  1.1  dholland 
   1483  1.1  dholland 	return (error);
   1484  1.1  dholland }
   1485  1.1  dholland 
   1486  1.1  dholland /*
   1487  1.1  dholland  * Real work associated with removing an extended attribute from a vnode.
   1488  1.1  dholland  * Assumes the attribute lock has already been grabbed.
   1489  1.1  dholland  */
   1490  1.1  dholland static int
   1491  1.4  dholland ulfs_extattr_rm(struct vnode *vp, int attrnamespace, const char *name,
   1492  1.1  dholland     kauth_cred_t cred, struct lwp *l)
   1493  1.1  dholland {
   1494  1.4  dholland 	struct ulfs_extattr_list_entry *attribute;
   1495  1.4  dholland 	struct ulfs_extattr_header ueh;
   1496  1.1  dholland 	struct mount *mp = vp->v_mount;
   1497  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
   1498  1.1  dholland 	struct iovec local_aiov;
   1499  1.1  dholland 	struct uio local_aio;
   1500  1.1  dholland 	off_t base_offset;
   1501  1.1  dholland 	int error = 0, ioflag;
   1502  1.1  dholland 
   1503  1.1  dholland 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1504  1.1  dholland 		return (EROFS);
   1505  1.4  dholland 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED))
   1506  1.1  dholland 		return (EOPNOTSUPP);
   1507  1.4  dholland 	if (!ulfs_extattr_valid_attrname(attrnamespace, name))
   1508  1.1  dholland 		return (EINVAL);
   1509  1.1  dholland 
   1510  1.1  dholland 	error = internal_extattr_check_cred(vp, attrnamespace, name, cred,
   1511  1.1  dholland 	    VWRITE);
   1512  1.1  dholland 	if (error)
   1513  1.1  dholland 		return (error);
   1514  1.1  dholland 
   1515  1.4  dholland 	attribute = ulfs_extattr_find_attr(ump, attrnamespace, name);
   1516  1.1  dholland 	if (!attribute)
   1517  1.1  dholland 		return (ENODATA);
   1518  1.1  dholland 
   1519  1.1  dholland 	/*
   1520  1.1  dholland 	 * Don't need to get a lock on the backing file if the getattr is
   1521  1.1  dholland 	 * being applied to the backing file, as the lock is already held.
   1522  1.1  dholland 	 */
   1523  1.1  dholland 	if (attribute->uele_backing_vnode != vp)
   1524  1.1  dholland 		vn_lock(attribute->uele_backing_vnode, LK_EXCLUSIVE | LK_RETRY);
   1525  1.1  dholland 
   1526  1.4  dholland 	error = ulfs_extattr_get_header(vp, attribute, &ueh, &base_offset);
   1527  1.1  dholland 	if (error)
   1528  1.1  dholland 		goto vopunlock_exit;
   1529  1.1  dholland 
   1530  1.1  dholland 	/* Flag it as not in use. */
   1531  1.1  dholland 	ueh.ueh_flags = 0;		/* No need to byte swap 0 */
   1532  1.1  dholland 	ueh.ueh_len = 0;		/* ...ditto... */
   1533  1.1  dholland 
   1534  1.1  dholland 	local_aiov.iov_base = &ueh;
   1535  1.4  dholland 	local_aiov.iov_len = sizeof(struct ulfs_extattr_header);
   1536  1.1  dholland 	local_aio.uio_iov = &local_aiov;
   1537  1.1  dholland 	local_aio.uio_iovcnt = 1;
   1538  1.1  dholland 	local_aio.uio_rw = UIO_WRITE;
   1539  1.1  dholland 	local_aio.uio_offset = base_offset;
   1540  1.4  dholland 	local_aio.uio_resid = sizeof(struct ulfs_extattr_header);
   1541  1.1  dholland 	UIO_SETUP_SYSSPACE(&local_aio);
   1542  1.1  dholland 
   1543  1.1  dholland 	ioflag = IO_NODELOCKED;
   1544  1.4  dholland 	if (ulfs_extattr_sync)
   1545  1.1  dholland 		ioflag |= IO_SYNC;
   1546  1.1  dholland 	error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
   1547  1.1  dholland 	    ump->um_extattr.uepm_ucred);
   1548  1.1  dholland 	if (error)
   1549  1.1  dholland 		goto vopunlock_exit;
   1550  1.1  dholland 
   1551  1.1  dholland 	if (local_aio.uio_resid != 0)
   1552  1.1  dholland 		error = ENXIO;
   1553  1.1  dholland 
   1554  1.1  dholland  vopunlock_exit:
   1555  1.1  dholland 	VOP_UNLOCK(attribute->uele_backing_vnode);
   1556  1.1  dholland 
   1557  1.1  dholland 	return (error);
   1558  1.1  dholland }
   1559  1.1  dholland 
   1560  1.1  dholland /*
   1561  1.4  dholland  * Called by ULFS when an inode is no longer active and should have its
   1562  1.1  dholland  * attributes stripped.
   1563  1.1  dholland  */
   1564  1.1  dholland void
   1565  1.4  dholland ulfs_extattr_vnode_inactive(struct vnode *vp, struct lwp *l)
   1566  1.1  dholland {
   1567  1.4  dholland 	struct ulfs_extattr_list_entry *uele;
   1568  1.1  dholland 	struct mount *mp = vp->v_mount;
   1569  1.4  dholland 	struct ulfsmount *ump = VFSTOULFS(mp);
   1570  1.1  dholland 
   1571  1.1  dholland 	/*
   1572  1.1  dholland 	 * In that case, we cannot lock. We should not have any active vnodes
   1573  1.1  dholland 	 * on the fs if this is not yet initialized but is going to be, so
   1574  1.1  dholland 	 * this can go unlocked.
   1575  1.1  dholland 	 */
   1576  1.4  dholland 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED))
   1577  1.1  dholland 		return;
   1578  1.1  dholland 
   1579  1.4  dholland 	ulfs_extattr_uepm_lock(ump);
   1580  1.1  dholland 
   1581  1.4  dholland 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) {
   1582  1.4  dholland 		ulfs_extattr_uepm_unlock(ump);
   1583  1.1  dholland 		return;
   1584  1.1  dholland 	}
   1585  1.1  dholland 
   1586  1.1  dholland 	LIST_FOREACH(uele, &ump->um_extattr.uepm_list, uele_entries)
   1587  1.4  dholland 		ulfs_extattr_rm(vp, uele->uele_attrnamespace,
   1588  1.1  dholland 		    uele->uele_attrname, lwp0.l_cred, l);
   1589  1.1  dholland 
   1590  1.4  dholland 	ulfs_extattr_uepm_unlock(ump);
   1591  1.1  dholland }
   1592  1.1  dholland 
   1593  1.1  dholland void
   1594  1.4  dholland ulfs_extattr_init(void)
   1595  1.1  dholland {
   1596  1.1  dholland 
   1597  1.1  dholland }
   1598  1.1  dholland 
   1599  1.1  dholland void
   1600  1.4  dholland ulfs_extattr_done(void)
   1601  1.1  dholland {
   1602  1.1  dholland 
   1603  1.1  dholland }
   1604