xref: /minix3/sys/ufs/lfs/ulfs_extattr.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ulfs_extattr.c,v 1.7 2014/02/07 15:29:23 hannken Exp $	*/
284d9c625SLionel Sambuc /*  from NetBSD: ufs_extattr.c,v 1.41 2012/12/08 13:42:36 manu Exp  */
384d9c625SLionel Sambuc 
484d9c625SLionel Sambuc /*-
584d9c625SLionel Sambuc  * Copyright (c) 1999-2002 Robert N. M. Watson
684d9c625SLionel Sambuc  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
784d9c625SLionel Sambuc  * All rights reserved.
884d9c625SLionel Sambuc  *
984d9c625SLionel Sambuc  * This software was developed by Robert Watson for the TrustedBSD Project.
1084d9c625SLionel Sambuc  *
1184d9c625SLionel Sambuc  * This software was developed for the FreeBSD Project in part by Network
1284d9c625SLionel Sambuc  * Associates Laboratories, the Security Research Division of Network
1384d9c625SLionel Sambuc  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"),
1484d9c625SLionel Sambuc  * as part of the DARPA CHATS research program.
1584d9c625SLionel Sambuc  *
1684d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1784d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
1884d9c625SLionel Sambuc  * are met:
1984d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
2084d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
2184d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
2284d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
2384d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
2484d9c625SLionel Sambuc  *
2584d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2684d9c625SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2784d9c625SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2884d9c625SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2984d9c625SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3084d9c625SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3184d9c625SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3284d9c625SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3384d9c625SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3484d9c625SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3584d9c625SLionel Sambuc  * SUCH DAMAGE.
3684d9c625SLionel Sambuc  *
3784d9c625SLionel Sambuc  */
3884d9c625SLionel Sambuc 
3984d9c625SLionel Sambuc /*
4084d9c625SLionel Sambuc  * Support for file system extended attributes on the ULFS1 file system.
4184d9c625SLionel Sambuc  *
4284d9c625SLionel Sambuc  * Extended attributes are defined in the form name=value, where name is
4384d9c625SLionel Sambuc  * a nul-terminated string in the style of a file name, and value is a
4484d9c625SLionel Sambuc  * binary blob of zero or more bytes.  The ULFS1 extended attribute service
4584d9c625SLionel Sambuc  * layers support for extended attributes onto a backing file, in the style
4684d9c625SLionel Sambuc  * of the quota implementation, meaning that it requires no underlying format
4784d9c625SLionel Sambuc  * changes to the file system.  This design choice exchanges simplicity,
4884d9c625SLionel Sambuc  * usability, and easy deployment for performance.
4984d9c625SLionel Sambuc  */
5084d9c625SLionel Sambuc 
5184d9c625SLionel Sambuc #include <sys/cdefs.h>
52*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: ulfs_extattr.c,v 1.7 2014/02/07 15:29:23 hannken Exp $");
5384d9c625SLionel Sambuc 
5484d9c625SLionel Sambuc #ifdef _KERNEL_OPT
5584d9c625SLionel Sambuc #include "opt_lfs.h"
5684d9c625SLionel Sambuc #endif
5784d9c625SLionel Sambuc 
5884d9c625SLionel Sambuc #include <sys/param.h>
5984d9c625SLionel Sambuc #include <sys/systm.h>
6084d9c625SLionel Sambuc #include <sys/reboot.h>
6184d9c625SLionel Sambuc #include <sys/kauth.h>
6284d9c625SLionel Sambuc #include <sys/kernel.h>
6384d9c625SLionel Sambuc #include <sys/namei.h>
6484d9c625SLionel Sambuc #include <sys/kmem.h>
6584d9c625SLionel Sambuc #include <sys/fcntl.h>
6684d9c625SLionel Sambuc #include <sys/lwp.h>
6784d9c625SLionel Sambuc #include <sys/vnode.h>
6884d9c625SLionel Sambuc #include <sys/mount.h>
6984d9c625SLionel Sambuc #include <sys/lock.h>
7084d9c625SLionel Sambuc #include <sys/dirent.h>
7184d9c625SLionel Sambuc #include <sys/extattr.h>
7284d9c625SLionel Sambuc #include <sys/sysctl.h>
7384d9c625SLionel Sambuc 
7484d9c625SLionel Sambuc #include <ufs/lfs/ulfs_extattr.h>
7584d9c625SLionel Sambuc #include <ufs/lfs/ulfsmount.h>
7684d9c625SLionel Sambuc #include <ufs/lfs/ulfs_inode.h>
7784d9c625SLionel Sambuc #include <ufs/lfs/ulfs_bswap.h>
7884d9c625SLionel Sambuc #include <ufs/lfs/ulfs_extern.h>
7984d9c625SLionel Sambuc 
8084d9c625SLionel Sambuc int ulfs_extattr_sync = 1;
8184d9c625SLionel Sambuc int ulfs_extattr_autocreate = 1024;
8284d9c625SLionel Sambuc 
8384d9c625SLionel Sambuc static int	ulfs_extattr_valid_attrname(int attrnamespace,
8484d9c625SLionel Sambuc 		    const char *attrname);
8584d9c625SLionel Sambuc static int	ulfs_extattr_enable_with_open(struct ulfsmount *ump,
8684d9c625SLionel Sambuc 		    struct vnode *vp, int attrnamespace, const char *attrname,
8784d9c625SLionel Sambuc 		    struct lwp *l);
8884d9c625SLionel Sambuc static int	ulfs_extattr_enable(struct ulfsmount *ump, int attrnamespace,
8984d9c625SLionel Sambuc 		    const char *attrname, struct vnode *backing_vnode,
9084d9c625SLionel Sambuc 		    struct lwp *l);
9184d9c625SLionel Sambuc static int	ulfs_extattr_disable(struct ulfsmount *ump, int attrnamespace,
9284d9c625SLionel Sambuc 		    const char *attrname, struct lwp *l);
9384d9c625SLionel Sambuc static int	ulfs_extattr_get(struct vnode *vp, int attrnamespace,
9484d9c625SLionel Sambuc 		    const char *name, struct uio *uio, size_t *size,
9584d9c625SLionel Sambuc 		    kauth_cred_t cred, struct lwp *l);
9684d9c625SLionel Sambuc static int	ulfs_extattr_list(struct vnode *vp, int attrnamespace,
9784d9c625SLionel Sambuc 		    struct uio *uio, size_t *size, int flag,
9884d9c625SLionel Sambuc 		    kauth_cred_t cred, struct lwp *l);
9984d9c625SLionel Sambuc static int	ulfs_extattr_set(struct vnode *vp, int attrnamespace,
10084d9c625SLionel Sambuc 		    const char *name, struct uio *uio, kauth_cred_t cred,
10184d9c625SLionel Sambuc 		    struct lwp *l);
10284d9c625SLionel Sambuc static int	ulfs_extattr_rm(struct vnode *vp, int attrnamespace,
10384d9c625SLionel Sambuc 		    const char *name, kauth_cred_t cred, struct lwp *l);
10484d9c625SLionel Sambuc static struct ulfs_extattr_list_entry *ulfs_extattr_find_attr(struct ulfsmount *,
10584d9c625SLionel Sambuc 		    int, const char *);
10684d9c625SLionel Sambuc static int	ulfs_extattr_get_header(struct vnode *,
10784d9c625SLionel Sambuc 		    struct ulfs_extattr_list_entry *,
10884d9c625SLionel Sambuc 		    struct ulfs_extattr_header *, off_t *);
10984d9c625SLionel Sambuc 
11084d9c625SLionel Sambuc /*
11184d9c625SLionel Sambuc  * Convert a FreeBSD extended attribute and namespace to a consistent string
11284d9c625SLionel Sambuc  * representation.
11384d9c625SLionel Sambuc  *
11484d9c625SLionel Sambuc  * The returned value, if not NULL, is guaranteed to be an allocated object
11584d9c625SLionel Sambuc  * of its size as returned by strlen() + 1 and must be freed by the caller.
11684d9c625SLionel Sambuc  */
11784d9c625SLionel Sambuc static char *
from_freebsd_extattr(int attrnamespace,const char * attrname)11884d9c625SLionel Sambuc from_freebsd_extattr(int attrnamespace, const char *attrname)
11984d9c625SLionel Sambuc {
12084d9c625SLionel Sambuc 	const char *namespace;
12184d9c625SLionel Sambuc 	char *attr;
12284d9c625SLionel Sambuc 	size_t len;
12384d9c625SLionel Sambuc 
12484d9c625SLionel Sambuc 	if (attrnamespace == EXTATTR_NAMESPACE_SYSTEM)
12584d9c625SLionel Sambuc 		namespace = "system";
12684d9c625SLionel Sambuc 	else if (attrnamespace == EXTATTR_NAMESPACE_USER)
12784d9c625SLionel Sambuc 		namespace = "user";
12884d9c625SLionel Sambuc 	else
12984d9c625SLionel Sambuc 		return NULL;
13084d9c625SLionel Sambuc 
13184d9c625SLionel Sambuc 	/* <namespace>.<attrname>\0 */
13284d9c625SLionel Sambuc 	len = strlen(namespace) + 1 + strlen(attrname) + 1;
13384d9c625SLionel Sambuc 
13484d9c625SLionel Sambuc 	attr = kmem_alloc(len, KM_SLEEP);
13584d9c625SLionel Sambuc 
13684d9c625SLionel Sambuc 	snprintf(attr, len, "%s.%s", namespace, attrname);
13784d9c625SLionel Sambuc 
13884d9c625SLionel Sambuc 	return attr;
13984d9c625SLionel Sambuc }
14084d9c625SLionel Sambuc 
14184d9c625SLionel Sambuc /*
14284d9c625SLionel Sambuc  * Internal wrapper around a conversion-check-free sequence.
14384d9c625SLionel Sambuc  */
14484d9c625SLionel Sambuc static int
internal_extattr_check_cred(vnode_t * vp,int attrnamespace,const char * name,kauth_cred_t cred,int access_mode)14584d9c625SLionel Sambuc internal_extattr_check_cred(vnode_t *vp, int attrnamespace, const char *name,
14684d9c625SLionel Sambuc     kauth_cred_t cred, int access_mode)
14784d9c625SLionel Sambuc {
14884d9c625SLionel Sambuc 	char *attr;
14984d9c625SLionel Sambuc 	int error;
15084d9c625SLionel Sambuc 
15184d9c625SLionel Sambuc 	attr = from_freebsd_extattr(attrnamespace, name);
15284d9c625SLionel Sambuc 	if (attr == NULL)
15384d9c625SLionel Sambuc 		return EINVAL;
15484d9c625SLionel Sambuc 
15584d9c625SLionel Sambuc 	error = extattr_check_cred(vp, attr, cred, access_mode);
15684d9c625SLionel Sambuc 
15784d9c625SLionel Sambuc 	kmem_free(attr, strlen(attr) + 1);
15884d9c625SLionel Sambuc 
15984d9c625SLionel Sambuc 	return error;
16084d9c625SLionel Sambuc }
16184d9c625SLionel Sambuc 
16284d9c625SLionel Sambuc /*
16384d9c625SLionel Sambuc  * Per-FS attribute lock protecting attribute operations.
16484d9c625SLionel Sambuc  * XXX Right now there is a lot of lock contention due to having a single
16584d9c625SLionel Sambuc  * lock per-FS; really, this should be far more fine-grained.
16684d9c625SLionel Sambuc  */
16784d9c625SLionel Sambuc static void
ulfs_extattr_uepm_lock(struct ulfsmount * ump)16884d9c625SLionel Sambuc ulfs_extattr_uepm_lock(struct ulfsmount *ump)
16984d9c625SLionel Sambuc {
17084d9c625SLionel Sambuc 
17184d9c625SLionel Sambuc 	/* XXX Why does this need to be recursive? */
17284d9c625SLionel Sambuc 	if (mutex_owned(&ump->um_extattr.uepm_lock)) {
17384d9c625SLionel Sambuc 		ump->um_extattr.uepm_lockcnt++;
17484d9c625SLionel Sambuc 		return;
17584d9c625SLionel Sambuc 	}
17684d9c625SLionel Sambuc 	mutex_enter(&ump->um_extattr.uepm_lock);
17784d9c625SLionel Sambuc }
17884d9c625SLionel Sambuc 
17984d9c625SLionel Sambuc static void
ulfs_extattr_uepm_unlock(struct ulfsmount * ump)18084d9c625SLionel Sambuc ulfs_extattr_uepm_unlock(struct ulfsmount *ump)
18184d9c625SLionel Sambuc {
18284d9c625SLionel Sambuc 
18384d9c625SLionel Sambuc 	if (ump->um_extattr.uepm_lockcnt != 0) {
18484d9c625SLionel Sambuc 		KASSERT(mutex_owned(&ump->um_extattr.uepm_lock));
18584d9c625SLionel Sambuc 		ump->um_extattr.uepm_lockcnt--;
18684d9c625SLionel Sambuc 		return;
18784d9c625SLionel Sambuc 	}
18884d9c625SLionel Sambuc 	mutex_exit(&ump->um_extattr.uepm_lock);
18984d9c625SLionel Sambuc }
19084d9c625SLionel Sambuc 
19184d9c625SLionel Sambuc /*-
19284d9c625SLionel Sambuc  * Determine whether the name passed is a valid name for an actual
19384d9c625SLionel Sambuc  * attribute.
19484d9c625SLionel Sambuc  *
19584d9c625SLionel Sambuc  * Invalid currently consists of:
19684d9c625SLionel Sambuc  *	 NULL pointer for attrname
19784d9c625SLionel Sambuc  *	 zero-length attrname (used to retrieve application attribute list)
19884d9c625SLionel Sambuc  */
19984d9c625SLionel Sambuc static int
ulfs_extattr_valid_attrname(int attrnamespace,const char * attrname)20084d9c625SLionel Sambuc ulfs_extattr_valid_attrname(int attrnamespace, const char *attrname)
20184d9c625SLionel Sambuc {
20284d9c625SLionel Sambuc 
20384d9c625SLionel Sambuc 	if (attrname == NULL)
20484d9c625SLionel Sambuc 		return (0);
20584d9c625SLionel Sambuc 	if (strlen(attrname) == 0)
20684d9c625SLionel Sambuc 		return (0);
20784d9c625SLionel Sambuc 	return (1);
20884d9c625SLionel Sambuc }
20984d9c625SLionel Sambuc 
21084d9c625SLionel Sambuc /*
21184d9c625SLionel Sambuc  * Autocreate an attribute storage
21284d9c625SLionel Sambuc  */
21384d9c625SLionel Sambuc static struct ulfs_extattr_list_entry *
ulfs_extattr_autocreate_attr(struct vnode * vp,int attrnamespace,const char * attrname,struct lwp * l)21484d9c625SLionel Sambuc ulfs_extattr_autocreate_attr(struct vnode *vp, int attrnamespace,
21584d9c625SLionel Sambuc     const char *attrname, struct lwp *l)
21684d9c625SLionel Sambuc {
21784d9c625SLionel Sambuc 	struct mount *mp = vp->v_mount;
21884d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
21984d9c625SLionel Sambuc 	struct vnode *backing_vp;
22084d9c625SLionel Sambuc 	struct nameidata nd;
22184d9c625SLionel Sambuc 	struct pathbuf *pb;
22284d9c625SLionel Sambuc 	char *path;
22384d9c625SLionel Sambuc 	struct ulfs_extattr_fileheader uef;
22484d9c625SLionel Sambuc 	struct ulfs_extattr_list_entry *uele;
22584d9c625SLionel Sambuc 	int error;
22684d9c625SLionel Sambuc 
22784d9c625SLionel Sambuc 	path = PNBUF_GET();
22884d9c625SLionel Sambuc 
22984d9c625SLionel Sambuc 	/*
23084d9c625SLionel Sambuc 	 * We only support system and user namespace autocreation
23184d9c625SLionel Sambuc 	 */
23284d9c625SLionel Sambuc 	switch (attrnamespace) {
23384d9c625SLionel Sambuc 	case EXTATTR_NAMESPACE_SYSTEM:
23484d9c625SLionel Sambuc 		(void)snprintf(path, PATH_MAX, "%s/%s/%s/%s",
23584d9c625SLionel Sambuc 			       mp->mnt_stat.f_mntonname,
23684d9c625SLionel Sambuc 			       ULFS_EXTATTR_FSROOTSUBDIR,
23784d9c625SLionel Sambuc 			       ULFS_EXTATTR_SUBDIR_SYSTEM,
23884d9c625SLionel Sambuc 			       attrname);
23984d9c625SLionel Sambuc 		break;
24084d9c625SLionel Sambuc 	case EXTATTR_NAMESPACE_USER:
24184d9c625SLionel Sambuc 		(void)snprintf(path, PATH_MAX, "%s/%s/%s/%s",
24284d9c625SLionel Sambuc 			       mp->mnt_stat.f_mntonname,
24384d9c625SLionel Sambuc 			       ULFS_EXTATTR_FSROOTSUBDIR,
24484d9c625SLionel Sambuc 			       ULFS_EXTATTR_SUBDIR_USER,
24584d9c625SLionel Sambuc 			       attrname);
24684d9c625SLionel Sambuc 		break;
24784d9c625SLionel Sambuc 	default:
24884d9c625SLionel Sambuc 		PNBUF_PUT(path);
24984d9c625SLionel Sambuc 		return NULL;
25084d9c625SLionel Sambuc 		break;
25184d9c625SLionel Sambuc 	}
25284d9c625SLionel Sambuc 
25384d9c625SLionel Sambuc 	/*
25484d9c625SLionel Sambuc 	 * XXX unlock/lock should only be done when setting extattr
25584d9c625SLionel Sambuc 	 * on backing store or one of its parent directory
25684d9c625SLionel Sambuc 	 * including root, but we always do it for now.
25784d9c625SLionel Sambuc 	 */
25884d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
25984d9c625SLionel Sambuc 	VOP_UNLOCK(vp);
26084d9c625SLionel Sambuc 
26184d9c625SLionel Sambuc 	pb = pathbuf_create(path);
26284d9c625SLionel Sambuc 	NDINIT(&nd, CREATE, LOCKPARENT, pb);
26384d9c625SLionel Sambuc 
26484d9c625SLionel Sambuc 	error = vn_open(&nd, O_CREAT|O_RDWR, 0600);
26584d9c625SLionel Sambuc 
26684d9c625SLionel Sambuc 	/*
26784d9c625SLionel Sambuc 	 * Reacquire the lock on the vnode
26884d9c625SLionel Sambuc 	 */
26984d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(vp) == 0);
27084d9c625SLionel Sambuc 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
27184d9c625SLionel Sambuc 
27284d9c625SLionel Sambuc 	if (error != 0) {
27384d9c625SLionel Sambuc 		pathbuf_destroy(pb);
27484d9c625SLionel Sambuc 		PNBUF_PUT(path);
27584d9c625SLionel Sambuc 		return NULL;
27684d9c625SLionel Sambuc 	}
27784d9c625SLionel Sambuc 
27884d9c625SLionel Sambuc 	KASSERT(nd.ni_vp != NULL);
27984d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(nd.ni_vp) == LK_EXCLUSIVE);
28084d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(nd.ni_dvp) == 0);
28184d9c625SLionel Sambuc 
28284d9c625SLionel Sambuc 	/*
28384d9c625SLionel Sambuc  	 * backing_vp is the backing store.
28484d9c625SLionel Sambuc 	 */
28584d9c625SLionel Sambuc 	backing_vp = nd.ni_vp;
28684d9c625SLionel Sambuc 	pathbuf_destroy(pb);
28784d9c625SLionel Sambuc 	PNBUF_PUT(path);
28884d9c625SLionel Sambuc 
28984d9c625SLionel Sambuc 	uef.uef_magic = ULFS_EXTATTR_MAGIC;
29084d9c625SLionel Sambuc 	uef.uef_version = ULFS_EXTATTR_VERSION;
29184d9c625SLionel Sambuc 	uef.uef_size = ulfs_extattr_autocreate;
29284d9c625SLionel Sambuc 
29384d9c625SLionel Sambuc 	error = vn_rdwr(UIO_WRITE, backing_vp, &uef, sizeof(uef), 0,
29484d9c625SLionel Sambuc 		        UIO_SYSSPACE, IO_NODELOCKED|IO_APPEND,
29584d9c625SLionel Sambuc 			l->l_cred, NULL, l);
29684d9c625SLionel Sambuc 
29784d9c625SLionel Sambuc 	VOP_UNLOCK(backing_vp);
29884d9c625SLionel Sambuc 
29984d9c625SLionel Sambuc 	if (error != 0) {
30084d9c625SLionel Sambuc 		printf("%s: write uef header failed for %s, error = %d\n",
30184d9c625SLionel Sambuc 		       __func__, attrname, error);
30284d9c625SLionel Sambuc 		vn_close(backing_vp, FREAD|FWRITE, l->l_cred);
30384d9c625SLionel Sambuc 		return NULL;
30484d9c625SLionel Sambuc 	}
30584d9c625SLionel Sambuc 
30684d9c625SLionel Sambuc 	/*
30784d9c625SLionel Sambuc 	 * Now enable attribute.
30884d9c625SLionel Sambuc 	 */
30984d9c625SLionel Sambuc 	error = ulfs_extattr_enable(ump,attrnamespace, attrname, backing_vp, l);
31084d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(backing_vp) == 0);
31184d9c625SLionel Sambuc 
31284d9c625SLionel Sambuc 	if (error != 0) {
31384d9c625SLionel Sambuc 		printf("%s: enable %s failed, error %d\n",
31484d9c625SLionel Sambuc 		       __func__, attrname, error);
31584d9c625SLionel Sambuc 		vn_close(backing_vp, FREAD|FWRITE, l->l_cred);
31684d9c625SLionel Sambuc 		return NULL;
31784d9c625SLionel Sambuc 	}
31884d9c625SLionel Sambuc 
31984d9c625SLionel Sambuc 	uele = ulfs_extattr_find_attr(ump, attrnamespace, attrname);
32084d9c625SLionel Sambuc 	if (uele == NULL) {
32184d9c625SLionel Sambuc 		printf("%s: atttribute %s created but not found!\n",
32284d9c625SLionel Sambuc 		       __func__, attrname);
32384d9c625SLionel Sambuc 		vn_close(backing_vp, FREAD|FWRITE, l->l_cred);
32484d9c625SLionel Sambuc 		return NULL;
32584d9c625SLionel Sambuc 	}
32684d9c625SLionel Sambuc 
32784d9c625SLionel Sambuc 	printf("%s: EA backing store autocreated for %s\n",
32884d9c625SLionel Sambuc 	       mp->mnt_stat.f_mntonname, attrname);
32984d9c625SLionel Sambuc 
33084d9c625SLionel Sambuc 	return uele;
33184d9c625SLionel Sambuc }
33284d9c625SLionel Sambuc 
33384d9c625SLionel Sambuc /*
33484d9c625SLionel Sambuc  * Locate an attribute given a name and mountpoint.
33584d9c625SLionel Sambuc  * Must be holding uepm lock for the mount point.
33684d9c625SLionel Sambuc  */
33784d9c625SLionel Sambuc static struct ulfs_extattr_list_entry *
ulfs_extattr_find_attr(struct ulfsmount * ump,int attrnamespace,const char * attrname)33884d9c625SLionel Sambuc ulfs_extattr_find_attr(struct ulfsmount *ump, int attrnamespace,
33984d9c625SLionel Sambuc     const char *attrname)
34084d9c625SLionel Sambuc {
34184d9c625SLionel Sambuc 	struct ulfs_extattr_list_entry *search_attribute;
34284d9c625SLionel Sambuc 
34384d9c625SLionel Sambuc 	for (search_attribute = LIST_FIRST(&ump->um_extattr.uepm_list);
34484d9c625SLionel Sambuc 	    search_attribute != NULL;
34584d9c625SLionel Sambuc 	    search_attribute = LIST_NEXT(search_attribute, uele_entries)) {
34684d9c625SLionel Sambuc 		if (!(strncmp(attrname, search_attribute->uele_attrname,
34784d9c625SLionel Sambuc 		    ULFS_EXTATTR_MAXEXTATTRNAME)) &&
34884d9c625SLionel Sambuc 		    (attrnamespace == search_attribute->uele_attrnamespace)) {
34984d9c625SLionel Sambuc 			return (search_attribute);
35084d9c625SLionel Sambuc 		}
35184d9c625SLionel Sambuc 	}
35284d9c625SLionel Sambuc 
35384d9c625SLionel Sambuc 	return (0);
35484d9c625SLionel Sambuc }
35584d9c625SLionel Sambuc 
35684d9c625SLionel Sambuc /*
35784d9c625SLionel Sambuc  * Initialize per-FS structures supporting extended attributes.  Do not
35884d9c625SLionel Sambuc  * start extended attributes yet.
35984d9c625SLionel Sambuc  */
36084d9c625SLionel Sambuc void
ulfs_extattr_uepm_init(struct ulfs_extattr_per_mount * uepm)36184d9c625SLionel Sambuc ulfs_extattr_uepm_init(struct ulfs_extattr_per_mount *uepm)
36284d9c625SLionel Sambuc {
36384d9c625SLionel Sambuc 
36484d9c625SLionel Sambuc 	uepm->uepm_flags = 0;
36584d9c625SLionel Sambuc 	uepm->uepm_lockcnt = 0;
36684d9c625SLionel Sambuc 
36784d9c625SLionel Sambuc 	LIST_INIT(&uepm->uepm_list);
36884d9c625SLionel Sambuc 	mutex_init(&uepm->uepm_lock, MUTEX_DEFAULT, IPL_NONE);
36984d9c625SLionel Sambuc 	uepm->uepm_flags |= ULFS_EXTATTR_UEPM_INITIALIZED;
37084d9c625SLionel Sambuc }
37184d9c625SLionel Sambuc 
37284d9c625SLionel Sambuc /*
37384d9c625SLionel Sambuc  * Destroy per-FS structures supporting extended attributes.  Assumes
37484d9c625SLionel Sambuc  * that EAs have already been stopped, and will panic if not.
37584d9c625SLionel Sambuc  */
37684d9c625SLionel Sambuc void
ulfs_extattr_uepm_destroy(struct ulfs_extattr_per_mount * uepm)37784d9c625SLionel Sambuc ulfs_extattr_uepm_destroy(struct ulfs_extattr_per_mount *uepm)
37884d9c625SLionel Sambuc {
37984d9c625SLionel Sambuc 
38084d9c625SLionel Sambuc 	if (!(uepm->uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED))
38184d9c625SLionel Sambuc 		panic("ulfs_extattr_uepm_destroy: not initialized");
38284d9c625SLionel Sambuc 
38384d9c625SLionel Sambuc 	if ((uepm->uepm_flags & ULFS_EXTATTR_UEPM_STARTED))
38484d9c625SLionel Sambuc 		panic("ulfs_extattr_uepm_destroy: called while still started");
38584d9c625SLionel Sambuc 
38684d9c625SLionel Sambuc 	/*
38784d9c625SLionel Sambuc 	 * It's not clear that either order for the next two lines is
38884d9c625SLionel Sambuc 	 * ideal, and it should never be a problem if this is only called
38984d9c625SLionel Sambuc 	 * during unmount, and with vfs_busy().
39084d9c625SLionel Sambuc 	 */
39184d9c625SLionel Sambuc 	uepm->uepm_flags &= ~ULFS_EXTATTR_UEPM_INITIALIZED;
39284d9c625SLionel Sambuc 	mutex_destroy(&uepm->uepm_lock);
39384d9c625SLionel Sambuc }
39484d9c625SLionel Sambuc 
39584d9c625SLionel Sambuc /*
39684d9c625SLionel Sambuc  * Start extended attribute support on an FS.
39784d9c625SLionel Sambuc  */
39884d9c625SLionel Sambuc int
ulfs_extattr_start(struct mount * mp,struct lwp * l)39984d9c625SLionel Sambuc ulfs_extattr_start(struct mount *mp, struct lwp *l)
40084d9c625SLionel Sambuc {
40184d9c625SLionel Sambuc 	struct ulfsmount *ump;
40284d9c625SLionel Sambuc 	int error = 0;
40384d9c625SLionel Sambuc 
40484d9c625SLionel Sambuc 	ump = VFSTOULFS(mp);
40584d9c625SLionel Sambuc 
40684d9c625SLionel Sambuc 	ulfs_extattr_uepm_lock(ump);
40784d9c625SLionel Sambuc 
40884d9c625SLionel Sambuc 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED)) {
40984d9c625SLionel Sambuc 		error = EOPNOTSUPP;
41084d9c625SLionel Sambuc 		goto unlock;
41184d9c625SLionel Sambuc 	}
41284d9c625SLionel Sambuc 	if (ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED) {
41384d9c625SLionel Sambuc 		error = EBUSY;
41484d9c625SLionel Sambuc 		goto unlock;
41584d9c625SLionel Sambuc 	}
41684d9c625SLionel Sambuc 
41784d9c625SLionel Sambuc 	ump->um_extattr.uepm_flags |= ULFS_EXTATTR_UEPM_STARTED;
41884d9c625SLionel Sambuc 
41984d9c625SLionel Sambuc 	ump->um_extattr.uepm_ucred = l->l_cred;
42084d9c625SLionel Sambuc 	kauth_cred_hold(ump->um_extattr.uepm_ucred);
42184d9c625SLionel Sambuc 
42284d9c625SLionel Sambuc  unlock:
42384d9c625SLionel Sambuc 	ulfs_extattr_uepm_unlock(ump);
42484d9c625SLionel Sambuc 
42584d9c625SLionel Sambuc 	return (error);
42684d9c625SLionel Sambuc }
42784d9c625SLionel Sambuc 
42884d9c625SLionel Sambuc /*
42984d9c625SLionel Sambuc  * Helper routine: given a locked parent directory and filename, return
43084d9c625SLionel Sambuc  * the locked vnode of the inode associated with the name.  Will not
43184d9c625SLionel Sambuc  * follow symlinks, may return any type of vnode.  Lock on parent will
43284d9c625SLionel Sambuc  * be released even in the event of a failure.  In the event that the
43384d9c625SLionel Sambuc  * target is the parent (i.e., "."), there will be two references and
43484d9c625SLionel Sambuc  * one lock, requiring the caller to possibly special-case.
43584d9c625SLionel Sambuc  */
43684d9c625SLionel Sambuc static int
ulfs_extattr_lookup(struct vnode * start_dvp,int lockparent,const char * dirname,struct vnode ** vp,struct lwp * l)43784d9c625SLionel Sambuc ulfs_extattr_lookup(struct vnode *start_dvp, int lockparent, const char *dirname,
43884d9c625SLionel Sambuc     struct vnode **vp, struct lwp *l)
43984d9c625SLionel Sambuc {
440*0a6a1f1dSLionel Sambuc 	struct vop_lookup_v2_args vargs;
44184d9c625SLionel Sambuc 	struct componentname cnp;
44284d9c625SLionel Sambuc 	struct vnode *target_vp;
44384d9c625SLionel Sambuc 	char *pnbuf;
44484d9c625SLionel Sambuc 	int error;
44584d9c625SLionel Sambuc 
44684d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(start_dvp) == LK_EXCLUSIVE);
44784d9c625SLionel Sambuc 
44884d9c625SLionel Sambuc 	pnbuf = PNBUF_GET();
44984d9c625SLionel Sambuc 
45084d9c625SLionel Sambuc 	memset(&cnp, 0, sizeof(cnp));
45184d9c625SLionel Sambuc 	cnp.cn_nameiop = LOOKUP;
45284d9c625SLionel Sambuc 	cnp.cn_flags = ISLASTCN | lockparent;
45384d9c625SLionel Sambuc 	cnp.cn_cred = l->l_cred;
45484d9c625SLionel Sambuc 	cnp.cn_nameptr = pnbuf;
45584d9c625SLionel Sambuc 	error = copystr(dirname, pnbuf, MAXPATHLEN, &cnp.cn_namelen);
45684d9c625SLionel Sambuc 	if (error) {
45784d9c625SLionel Sambuc 		if (lockparent == 0) {
45884d9c625SLionel Sambuc 			VOP_UNLOCK(start_dvp);
45984d9c625SLionel Sambuc 		}
46084d9c625SLionel Sambuc 		PNBUF_PUT(pnbuf);
46184d9c625SLionel Sambuc 		printf("ulfs_extattr_lookup: copystr failed\n");
46284d9c625SLionel Sambuc 		return (error);
46384d9c625SLionel Sambuc 	}
46484d9c625SLionel Sambuc 	cnp.cn_namelen--;	/* trim nul termination */
46584d9c625SLionel Sambuc 	vargs.a_desc = NULL;
46684d9c625SLionel Sambuc 	vargs.a_dvp = start_dvp;
46784d9c625SLionel Sambuc 	vargs.a_vpp = &target_vp;
46884d9c625SLionel Sambuc 	vargs.a_cnp = &cnp;
46984d9c625SLionel Sambuc 	error = ulfs_lookup(&vargs);
47084d9c625SLionel Sambuc 	PNBUF_PUT(pnbuf);
47184d9c625SLionel Sambuc 	if (error) {
47284d9c625SLionel Sambuc 		if (lockparent == 0) {
47384d9c625SLionel Sambuc 			VOP_UNLOCK(start_dvp);
47484d9c625SLionel Sambuc 		}
47584d9c625SLionel Sambuc 		return (error);
47684d9c625SLionel Sambuc 	}
47784d9c625SLionel Sambuc #if 0
47884d9c625SLionel Sambuc 	if (target_vp == start_dvp)
47984d9c625SLionel Sambuc 		panic("ulfs_extattr_lookup: target_vp == start_dvp");
48084d9c625SLionel Sambuc #endif
48184d9c625SLionel Sambuc 
482*0a6a1f1dSLionel Sambuc 	if (target_vp != start_dvp) {
483*0a6a1f1dSLionel Sambuc 		error = vn_lock(target_vp, LK_EXCLUSIVE);
484*0a6a1f1dSLionel Sambuc 		if (lockparent == 0)
48584d9c625SLionel Sambuc 			VOP_UNLOCK(start_dvp);
486*0a6a1f1dSLionel Sambuc 		if (error) {
487*0a6a1f1dSLionel Sambuc 			vrele(target_vp);
488*0a6a1f1dSLionel Sambuc 			return error;
489*0a6a1f1dSLionel Sambuc 		}
490*0a6a1f1dSLionel Sambuc 	}
49184d9c625SLionel Sambuc 
49284d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(target_vp) == LK_EXCLUSIVE);
49384d9c625SLionel Sambuc 	*vp = target_vp;
49484d9c625SLionel Sambuc 	return (0);
49584d9c625SLionel Sambuc }
49684d9c625SLionel Sambuc 
49784d9c625SLionel Sambuc /*
49884d9c625SLionel Sambuc  * Enable an EA using the passed filesystem, backing vnode, attribute name,
49984d9c625SLionel Sambuc  * namespace, and proc.  Will perform a VOP_OPEN() on the vp, so expects vp
50084d9c625SLionel Sambuc  * to be locked when passed in.  The vnode will be returned unlocked,
50184d9c625SLionel Sambuc  * regardless of success/failure of the function.  As a result, the caller
50284d9c625SLionel Sambuc  * will always need to vrele(), but not vput().
50384d9c625SLionel Sambuc  */
50484d9c625SLionel Sambuc static int
ulfs_extattr_enable_with_open(struct ulfsmount * ump,struct vnode * vp,int attrnamespace,const char * attrname,struct lwp * l)50584d9c625SLionel Sambuc ulfs_extattr_enable_with_open(struct ulfsmount *ump, struct vnode *vp,
50684d9c625SLionel Sambuc     int attrnamespace, const char *attrname, struct lwp *l)
50784d9c625SLionel Sambuc {
50884d9c625SLionel Sambuc 	int error;
50984d9c625SLionel Sambuc 
51084d9c625SLionel Sambuc 	error = VOP_OPEN(vp, FREAD|FWRITE, l->l_cred);
51184d9c625SLionel Sambuc 	if (error) {
51284d9c625SLionel Sambuc 		printf("ulfs_extattr_enable_with_open.VOP_OPEN(): failed "
51384d9c625SLionel Sambuc 		    "with %d\n", error);
51484d9c625SLionel Sambuc 		VOP_UNLOCK(vp);
51584d9c625SLionel Sambuc 		return (error);
51684d9c625SLionel Sambuc 	}
51784d9c625SLionel Sambuc 
51884d9c625SLionel Sambuc 	mutex_enter(vp->v_interlock);
51984d9c625SLionel Sambuc 	vp->v_writecount++;
52084d9c625SLionel Sambuc 	mutex_exit(vp->v_interlock);
52184d9c625SLionel Sambuc 
52284d9c625SLionel Sambuc 	vref(vp);
52384d9c625SLionel Sambuc 
52484d9c625SLionel Sambuc 	VOP_UNLOCK(vp);
52584d9c625SLionel Sambuc 
52684d9c625SLionel Sambuc 	error = ulfs_extattr_enable(ump, attrnamespace, attrname, vp, l);
52784d9c625SLionel Sambuc 	if (error != 0)
52884d9c625SLionel Sambuc 		vn_close(vp, FREAD|FWRITE, l->l_cred);
52984d9c625SLionel Sambuc 	return (error);
53084d9c625SLionel Sambuc }
53184d9c625SLionel Sambuc 
53284d9c625SLionel Sambuc /*
53384d9c625SLionel Sambuc  * Given a locked directory vnode, iterate over the names in the directory
53484d9c625SLionel Sambuc  * and use ulfs_extattr_lookup() to retrieve locked vnodes of potential
53584d9c625SLionel Sambuc  * attribute files.  Then invoke ulfs_extattr_enable_with_open() on each
53684d9c625SLionel Sambuc  * to attempt to start the attribute.  Leaves the directory locked on
53784d9c625SLionel Sambuc  * exit.
53884d9c625SLionel Sambuc  */
53984d9c625SLionel Sambuc static int
ulfs_extattr_iterate_directory(struct ulfsmount * ump,struct vnode * dvp,int attrnamespace,struct lwp * l)54084d9c625SLionel Sambuc ulfs_extattr_iterate_directory(struct ulfsmount *ump, struct vnode *dvp,
54184d9c625SLionel Sambuc     int attrnamespace, struct lwp *l)
54284d9c625SLionel Sambuc {
54384d9c625SLionel Sambuc 	struct vop_readdir_args vargs;
54484d9c625SLionel Sambuc 	struct statvfs *sbp = &ump->um_mountp->mnt_stat;
54584d9c625SLionel Sambuc 	struct dirent *dp, *edp;
54684d9c625SLionel Sambuc 	struct vnode *attr_vp;
54784d9c625SLionel Sambuc 	struct uio auio;
54884d9c625SLionel Sambuc 	struct iovec aiov;
54984d9c625SLionel Sambuc 	char *dirbuf;
55084d9c625SLionel Sambuc 	int error, eofflag = 0;
55184d9c625SLionel Sambuc 
55284d9c625SLionel Sambuc 	if (dvp->v_type != VDIR)
55384d9c625SLionel Sambuc 		return (ENOTDIR);
55484d9c625SLionel Sambuc 
55584d9c625SLionel Sambuc 	dirbuf = kmem_alloc(LFS_DIRBLKSIZ, KM_SLEEP);
55684d9c625SLionel Sambuc 
55784d9c625SLionel Sambuc 	auio.uio_iov = &aiov;
55884d9c625SLionel Sambuc 	auio.uio_iovcnt = 1;
55984d9c625SLionel Sambuc 	auio.uio_rw = UIO_READ;
56084d9c625SLionel Sambuc 	auio.uio_offset = 0;
56184d9c625SLionel Sambuc 	UIO_SETUP_SYSSPACE(&auio);
56284d9c625SLionel Sambuc 
56384d9c625SLionel Sambuc 	vargs.a_desc = NULL;
56484d9c625SLionel Sambuc 	vargs.a_vp = dvp;
56584d9c625SLionel Sambuc 	vargs.a_uio = &auio;
56684d9c625SLionel Sambuc 	vargs.a_cred = l->l_cred;
56784d9c625SLionel Sambuc 	vargs.a_eofflag = &eofflag;
56884d9c625SLionel Sambuc 	vargs.a_ncookies = NULL;
56984d9c625SLionel Sambuc 	vargs.a_cookies = NULL;
57084d9c625SLionel Sambuc 
57184d9c625SLionel Sambuc 	while (!eofflag) {
57284d9c625SLionel Sambuc 		auio.uio_resid = LFS_DIRBLKSIZ;
57384d9c625SLionel Sambuc 		aiov.iov_base = dirbuf;
57484d9c625SLionel Sambuc 		aiov.iov_len = LFS_DIRBLKSIZ;
57584d9c625SLionel Sambuc 		error = ulfs_readdir(&vargs);
57684d9c625SLionel Sambuc 		if (error) {
57784d9c625SLionel Sambuc 			printf("ulfs_extattr_iterate_directory: ulfs_readdir "
57884d9c625SLionel Sambuc 			    "%d\n", error);
57984d9c625SLionel Sambuc 			return (error);
58084d9c625SLionel Sambuc 		}
58184d9c625SLionel Sambuc 
58284d9c625SLionel Sambuc 		/*
58384d9c625SLionel Sambuc 		 * XXXRW: While in LFS, we always get LFS_DIRBLKSIZ returns from
58484d9c625SLionel Sambuc 		 * the directory code on success, on other file systems this
58584d9c625SLionel Sambuc 		 * may not be the case.  For portability, we should check the
58684d9c625SLionel Sambuc 		 * read length on return from ulfs_readdir().
58784d9c625SLionel Sambuc 		 */
58884d9c625SLionel Sambuc 		edp = (struct dirent *)&dirbuf[LFS_DIRBLKSIZ];
58984d9c625SLionel Sambuc 		for (dp = (struct dirent *)dirbuf; dp < edp; ) {
59084d9c625SLionel Sambuc 			if (dp->d_reclen == 0)
59184d9c625SLionel Sambuc 				break;
59284d9c625SLionel Sambuc 			/* Skip "." and ".." */
59384d9c625SLionel Sambuc 			if (dp->d_name[0] == '.' &&
59484d9c625SLionel Sambuc 			    (dp->d_name[1] == '\0' ||
59584d9c625SLionel Sambuc 			     (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
59684d9c625SLionel Sambuc 				goto next;
59784d9c625SLionel Sambuc 			error = ulfs_extattr_lookup(dvp, LOCKPARENT,
59884d9c625SLionel Sambuc 			    dp->d_name, &attr_vp, l);
59984d9c625SLionel Sambuc 			if (error == ENOENT) {
60084d9c625SLionel Sambuc 				goto next; /* keep silent */
60184d9c625SLionel Sambuc 			} else if (error) {
60284d9c625SLionel Sambuc 				printf("ulfs_extattr_iterate_directory: lookup "
60384d9c625SLionel Sambuc 				    "%s %d\n", dp->d_name, error);
60484d9c625SLionel Sambuc 			} else if (attr_vp == dvp) {
60584d9c625SLionel Sambuc 				vrele(attr_vp);
60684d9c625SLionel Sambuc 			} else if (attr_vp->v_type != VREG) {
60784d9c625SLionel Sambuc 				vput(attr_vp);
60884d9c625SLionel Sambuc 			} else {
60984d9c625SLionel Sambuc 				error = ulfs_extattr_enable_with_open(ump,
61084d9c625SLionel Sambuc 				    attr_vp, attrnamespace, dp->d_name, l);
61184d9c625SLionel Sambuc 				vrele(attr_vp);
61284d9c625SLionel Sambuc 				if (error) {
61384d9c625SLionel Sambuc 					printf("ulfs_extattr_iterate_directory: "
61484d9c625SLionel Sambuc 					    "enable %s %d\n", dp->d_name,
61584d9c625SLionel Sambuc 					    error);
61684d9c625SLionel Sambuc 				} else if (bootverbose) {
61784d9c625SLionel Sambuc 					printf("%s: EA %s loaded\n",
61884d9c625SLionel Sambuc 					       sbp->f_mntonname, dp->d_name);
61984d9c625SLionel Sambuc 				}
62084d9c625SLionel Sambuc 			}
62184d9c625SLionel Sambuc  next:
62284d9c625SLionel Sambuc 			dp = (struct dirent *) ((char *)dp + dp->d_reclen);
62384d9c625SLionel Sambuc 			if (dp >= edp)
62484d9c625SLionel Sambuc 				break;
62584d9c625SLionel Sambuc 		}
62684d9c625SLionel Sambuc 	}
62784d9c625SLionel Sambuc 	kmem_free(dirbuf, LFS_DIRBLKSIZ);
62884d9c625SLionel Sambuc 
62984d9c625SLionel Sambuc 	return (0);
63084d9c625SLionel Sambuc }
63184d9c625SLionel Sambuc 
63284d9c625SLionel Sambuc /*
63384d9c625SLionel Sambuc  * Auto-start of extended attributes, to be executed (optionally) at
63484d9c625SLionel Sambuc  * mount-time.
63584d9c625SLionel Sambuc  */
63684d9c625SLionel Sambuc int
ulfs_extattr_autostart(struct mount * mp,struct lwp * l)63784d9c625SLionel Sambuc ulfs_extattr_autostart(struct mount *mp, struct lwp *l)
63884d9c625SLionel Sambuc {
63984d9c625SLionel Sambuc 	struct vnode *rvp, *attr_dvp, *attr_system_dvp, *attr_user_dvp;
64084d9c625SLionel Sambuc 	int error;
64184d9c625SLionel Sambuc 
64284d9c625SLionel Sambuc 	/*
64384d9c625SLionel Sambuc 	 * Does ULFS_EXTATTR_FSROOTSUBDIR exist off the filesystem root?
64484d9c625SLionel Sambuc 	 * If so, automatically start EA's.
64584d9c625SLionel Sambuc 	 */
64684d9c625SLionel Sambuc 	error = VFS_ROOT(mp, &rvp);
64784d9c625SLionel Sambuc 	if (error) {
64884d9c625SLionel Sambuc 		printf("ulfs_extattr_autostart.VFS_ROOT() returned %d\n",
64984d9c625SLionel Sambuc 		    error);
65084d9c625SLionel Sambuc 		return (error);
65184d9c625SLionel Sambuc 	}
65284d9c625SLionel Sambuc 
65384d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(rvp) == LK_EXCLUSIVE);
65484d9c625SLionel Sambuc 
65584d9c625SLionel Sambuc 	error = ulfs_extattr_lookup(rvp, 0,
65684d9c625SLionel Sambuc 	    ULFS_EXTATTR_FSROOTSUBDIR, &attr_dvp, l);
65784d9c625SLionel Sambuc 	if (error) {
65884d9c625SLionel Sambuc 		/* rvp ref'd but now unlocked */
65984d9c625SLionel Sambuc 		KASSERT(VOP_ISLOCKED(rvp) == 0);
66084d9c625SLionel Sambuc 		vrele(rvp);
66184d9c625SLionel Sambuc 		return (error);
66284d9c625SLionel Sambuc 	}
66384d9c625SLionel Sambuc 	if (rvp == attr_dvp) {
66484d9c625SLionel Sambuc 		/* Should never happen. */
66584d9c625SLionel Sambuc 		KASSERT(VOP_ISLOCKED(rvp) == LK_EXCLUSIVE);
66684d9c625SLionel Sambuc 		vrele(attr_dvp);
66784d9c625SLionel Sambuc 		vput(rvp);
66884d9c625SLionel Sambuc 		return (EINVAL);
66984d9c625SLionel Sambuc 	}
67084d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(rvp) == 0);
67184d9c625SLionel Sambuc 	vrele(rvp);
67284d9c625SLionel Sambuc 
67384d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE);
67484d9c625SLionel Sambuc 
67584d9c625SLionel Sambuc 	if (attr_dvp->v_type != VDIR) {
67684d9c625SLionel Sambuc 		printf("ulfs_extattr_autostart: %s != VDIR\n",
67784d9c625SLionel Sambuc 		    ULFS_EXTATTR_FSROOTSUBDIR);
67884d9c625SLionel Sambuc 		goto return_vput_attr_dvp;
67984d9c625SLionel Sambuc 	}
68084d9c625SLionel Sambuc 
68184d9c625SLionel Sambuc 	error = ulfs_extattr_start(mp, l);
68284d9c625SLionel Sambuc 	if (error) {
68384d9c625SLionel Sambuc 		printf("ulfs_extattr_autostart: ulfs_extattr_start failed (%d)\n",
68484d9c625SLionel Sambuc 		    error);
68584d9c625SLionel Sambuc 		goto return_vput_attr_dvp;
68684d9c625SLionel Sambuc 	}
68784d9c625SLionel Sambuc 
68884d9c625SLionel Sambuc 	/*
68984d9c625SLionel Sambuc 	 * Look for two subdirectories: ULFS_EXTATTR_SUBDIR_SYSTEM,
69084d9c625SLionel Sambuc 	 * ULFS_EXTATTR_SUBDIR_USER.  For each, iterate over the sub-directory,
69184d9c625SLionel Sambuc 	 * and start with appropriate type.  Failures in either don't
69284d9c625SLionel Sambuc 	 * result in an over-all failure.  attr_dvp is left locked to
69384d9c625SLionel Sambuc 	 * be cleaned up on exit.
69484d9c625SLionel Sambuc 	 */
69584d9c625SLionel Sambuc 	error = ulfs_extattr_lookup(attr_dvp, LOCKPARENT,
69684d9c625SLionel Sambuc 	    ULFS_EXTATTR_SUBDIR_SYSTEM, &attr_system_dvp, l);
69784d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE);
69884d9c625SLionel Sambuc 	if (error == 0) {
69984d9c625SLionel Sambuc 		KASSERT(VOP_ISLOCKED(attr_system_dvp) == LK_EXCLUSIVE);
70084d9c625SLionel Sambuc 		error = ulfs_extattr_iterate_directory(VFSTOULFS(mp),
70184d9c625SLionel Sambuc 		    attr_system_dvp, EXTATTR_NAMESPACE_SYSTEM, l);
70284d9c625SLionel Sambuc 		if (error)
70384d9c625SLionel Sambuc 			printf("ulfs_extattr_iterate_directory returned %d\n",
70484d9c625SLionel Sambuc 			    error);
70584d9c625SLionel Sambuc 		KASSERT(VOP_ISLOCKED(attr_system_dvp) == LK_EXCLUSIVE);
70684d9c625SLionel Sambuc 		vput(attr_system_dvp);
70784d9c625SLionel Sambuc 	}
70884d9c625SLionel Sambuc 
70984d9c625SLionel Sambuc 	error = ulfs_extattr_lookup(attr_dvp, LOCKPARENT,
71084d9c625SLionel Sambuc 	    ULFS_EXTATTR_SUBDIR_USER, &attr_user_dvp, l);
71184d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE);
71284d9c625SLionel Sambuc 	if (error == 0) {
71384d9c625SLionel Sambuc 		KASSERT(VOP_ISLOCKED(attr_user_dvp) == LK_EXCLUSIVE);
71484d9c625SLionel Sambuc 		error = ulfs_extattr_iterate_directory(VFSTOULFS(mp),
71584d9c625SLionel Sambuc 		    attr_user_dvp, EXTATTR_NAMESPACE_USER, l);
71684d9c625SLionel Sambuc 		if (error)
71784d9c625SLionel Sambuc 			printf("ulfs_extattr_iterate_directory returned %d\n",
71884d9c625SLionel Sambuc 			    error);
71984d9c625SLionel Sambuc 		KASSERT(VOP_ISLOCKED(attr_user_dvp) == LK_EXCLUSIVE);
72084d9c625SLionel Sambuc 		vput(attr_user_dvp);
72184d9c625SLionel Sambuc 	}
72284d9c625SLionel Sambuc 
72384d9c625SLionel Sambuc 	/* Mask startup failures in sub-directories. */
72484d9c625SLionel Sambuc 	error = 0;
72584d9c625SLionel Sambuc 
72684d9c625SLionel Sambuc  return_vput_attr_dvp:
72784d9c625SLionel Sambuc 	KASSERT(VOP_ISLOCKED(attr_dvp) == LK_EXCLUSIVE);
72884d9c625SLionel Sambuc 	vput(attr_dvp);
72984d9c625SLionel Sambuc 
73084d9c625SLionel Sambuc 	return (error);
73184d9c625SLionel Sambuc }
73284d9c625SLionel Sambuc 
73384d9c625SLionel Sambuc /*
73484d9c625SLionel Sambuc  * Stop extended attribute support on an FS.
73584d9c625SLionel Sambuc  */
73684d9c625SLionel Sambuc void
ulfs_extattr_stop(struct mount * mp,struct lwp * l)73784d9c625SLionel Sambuc ulfs_extattr_stop(struct mount *mp, struct lwp *l)
73884d9c625SLionel Sambuc {
73984d9c625SLionel Sambuc 	struct ulfs_extattr_list_entry *uele;
74084d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
74184d9c625SLionel Sambuc 
74284d9c625SLionel Sambuc 	ulfs_extattr_uepm_lock(ump);
74384d9c625SLionel Sambuc 
74484d9c625SLionel Sambuc 	/*
74584d9c625SLionel Sambuc 	 * If we haven't been started, no big deal.  Just short-circuit
74684d9c625SLionel Sambuc 	 * the processing work.
74784d9c625SLionel Sambuc 	 */
74884d9c625SLionel Sambuc 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) {
74984d9c625SLionel Sambuc 		goto unlock;
75084d9c625SLionel Sambuc 	}
75184d9c625SLionel Sambuc 
75284d9c625SLionel Sambuc 	while (LIST_FIRST(&ump->um_extattr.uepm_list) != NULL) {
75384d9c625SLionel Sambuc 		uele = LIST_FIRST(&ump->um_extattr.uepm_list);
75484d9c625SLionel Sambuc 		ulfs_extattr_disable(ump, uele->uele_attrnamespace,
75584d9c625SLionel Sambuc 		    uele->uele_attrname, l);
75684d9c625SLionel Sambuc 	}
75784d9c625SLionel Sambuc 
75884d9c625SLionel Sambuc 	ump->um_extattr.uepm_flags &= ~ULFS_EXTATTR_UEPM_STARTED;
75984d9c625SLionel Sambuc 
76084d9c625SLionel Sambuc 	kauth_cred_free(ump->um_extattr.uepm_ucred);
76184d9c625SLionel Sambuc 	ump->um_extattr.uepm_ucred = NULL;
76284d9c625SLionel Sambuc 
76384d9c625SLionel Sambuc  unlock:
76484d9c625SLionel Sambuc 	ulfs_extattr_uepm_unlock(ump);
76584d9c625SLionel Sambuc }
76684d9c625SLionel Sambuc 
76784d9c625SLionel Sambuc /*
76884d9c625SLionel Sambuc  * Enable a named attribute on the specified filesystem; provide an
76984d9c625SLionel Sambuc  * unlocked backing vnode to hold the attribute data.
77084d9c625SLionel Sambuc  */
77184d9c625SLionel Sambuc static int
ulfs_extattr_enable(struct ulfsmount * ump,int attrnamespace,const char * attrname,struct vnode * backing_vnode,struct lwp * l)77284d9c625SLionel Sambuc ulfs_extattr_enable(struct ulfsmount *ump, int attrnamespace,
77384d9c625SLionel Sambuc     const char *attrname, struct vnode *backing_vnode, struct lwp *l)
77484d9c625SLionel Sambuc {
77584d9c625SLionel Sambuc 	struct ulfs_extattr_list_entry *attribute;
77684d9c625SLionel Sambuc 	struct iovec aiov;
77784d9c625SLionel Sambuc 	struct uio auio;
77884d9c625SLionel Sambuc 	int error = 0;
77984d9c625SLionel Sambuc 
78084d9c625SLionel Sambuc 	if (!ulfs_extattr_valid_attrname(attrnamespace, attrname))
78184d9c625SLionel Sambuc 		return (EINVAL);
78284d9c625SLionel Sambuc 	if (backing_vnode->v_type != VREG)
78384d9c625SLionel Sambuc 		return (EINVAL);
78484d9c625SLionel Sambuc 
78584d9c625SLionel Sambuc 	attribute = kmem_zalloc(sizeof(*attribute), KM_SLEEP);
78684d9c625SLionel Sambuc 
78784d9c625SLionel Sambuc 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) {
78884d9c625SLionel Sambuc 		error = EOPNOTSUPP;
78984d9c625SLionel Sambuc 		goto free_exit;
79084d9c625SLionel Sambuc 	}
79184d9c625SLionel Sambuc 
79284d9c625SLionel Sambuc 	if (ulfs_extattr_find_attr(ump, attrnamespace, attrname)) {
79384d9c625SLionel Sambuc 		error = EEXIST;
79484d9c625SLionel Sambuc 		goto free_exit;
79584d9c625SLionel Sambuc 	}
79684d9c625SLionel Sambuc 
79784d9c625SLionel Sambuc 	strncpy(attribute->uele_attrname, attrname,
79884d9c625SLionel Sambuc 	    ULFS_EXTATTR_MAXEXTATTRNAME);
79984d9c625SLionel Sambuc 	attribute->uele_attrnamespace = attrnamespace;
80084d9c625SLionel Sambuc 	memset(&attribute->uele_fileheader, 0,
80184d9c625SLionel Sambuc 	    sizeof(struct ulfs_extattr_fileheader));
80284d9c625SLionel Sambuc 
80384d9c625SLionel Sambuc 	attribute->uele_backing_vnode = backing_vnode;
80484d9c625SLionel Sambuc 
80584d9c625SLionel Sambuc 	auio.uio_iov = &aiov;
80684d9c625SLionel Sambuc 	auio.uio_iovcnt = 1;
80784d9c625SLionel Sambuc 	aiov.iov_base = (void *) &attribute->uele_fileheader;
80884d9c625SLionel Sambuc 	aiov.iov_len = sizeof(struct ulfs_extattr_fileheader);
80984d9c625SLionel Sambuc 	auio.uio_resid = sizeof(struct ulfs_extattr_fileheader);
81084d9c625SLionel Sambuc 	auio.uio_offset = (off_t) 0;
81184d9c625SLionel Sambuc 	auio.uio_rw = UIO_READ;
81284d9c625SLionel Sambuc 	UIO_SETUP_SYSSPACE(&auio);
81384d9c625SLionel Sambuc 
81484d9c625SLionel Sambuc 	vn_lock(backing_vnode, LK_SHARED | LK_RETRY);
81584d9c625SLionel Sambuc 	error = VOP_READ(backing_vnode, &auio, IO_NODELOCKED,
81684d9c625SLionel Sambuc 	    ump->um_extattr.uepm_ucred);
81784d9c625SLionel Sambuc 
81884d9c625SLionel Sambuc 	if (error)
81984d9c625SLionel Sambuc 		goto unlock_free_exit;
82084d9c625SLionel Sambuc 
82184d9c625SLionel Sambuc 	if (auio.uio_resid != 0) {
82284d9c625SLionel Sambuc 		printf("ulfs_extattr_enable: malformed attribute header\n");
82384d9c625SLionel Sambuc 		error = EINVAL;
82484d9c625SLionel Sambuc 		goto unlock_free_exit;
82584d9c625SLionel Sambuc 	}
82684d9c625SLionel Sambuc 
82784d9c625SLionel Sambuc 	/*
82884d9c625SLionel Sambuc 	 * Try to determine the byte order of the attribute file.
82984d9c625SLionel Sambuc 	 */
83084d9c625SLionel Sambuc 	if (attribute->uele_fileheader.uef_magic != ULFS_EXTATTR_MAGIC) {
83184d9c625SLionel Sambuc 		attribute->uele_flags |= UELE_F_NEEDSWAP;
83284d9c625SLionel Sambuc 		attribute->uele_fileheader.uef_magic =
83384d9c625SLionel Sambuc 		    ulfs_rw32(attribute->uele_fileheader.uef_magic,
83484d9c625SLionel Sambuc 			     UELE_NEEDSWAP(attribute));
83584d9c625SLionel Sambuc 		if (attribute->uele_fileheader.uef_magic != ULFS_EXTATTR_MAGIC) {
83684d9c625SLionel Sambuc 			printf("ulfs_extattr_enable: invalid attribute header "
83784d9c625SLionel Sambuc 			       "magic\n");
83884d9c625SLionel Sambuc 			error = EINVAL;
83984d9c625SLionel Sambuc 			goto unlock_free_exit;
84084d9c625SLionel Sambuc 		}
84184d9c625SLionel Sambuc 	}
84284d9c625SLionel Sambuc 	attribute->uele_fileheader.uef_version =
84384d9c625SLionel Sambuc 	    ulfs_rw32(attribute->uele_fileheader.uef_version,
84484d9c625SLionel Sambuc 		     UELE_NEEDSWAP(attribute));
84584d9c625SLionel Sambuc 	attribute->uele_fileheader.uef_size =
84684d9c625SLionel Sambuc 	    ulfs_rw32(attribute->uele_fileheader.uef_size,
84784d9c625SLionel Sambuc 		     UELE_NEEDSWAP(attribute));
84884d9c625SLionel Sambuc 
84984d9c625SLionel Sambuc 	if (attribute->uele_fileheader.uef_version != ULFS_EXTATTR_VERSION) {
85084d9c625SLionel Sambuc 		printf("ulfs_extattr_enable: incorrect attribute header "
85184d9c625SLionel Sambuc 		    "version\n");
85284d9c625SLionel Sambuc 		error = EINVAL;
85384d9c625SLionel Sambuc 		goto unlock_free_exit;
85484d9c625SLionel Sambuc 	}
85584d9c625SLionel Sambuc 
85684d9c625SLionel Sambuc 	LIST_INSERT_HEAD(&ump->um_extattr.uepm_list, attribute,
85784d9c625SLionel Sambuc 	    uele_entries);
85884d9c625SLionel Sambuc 
85984d9c625SLionel Sambuc 	VOP_UNLOCK(backing_vnode);
86084d9c625SLionel Sambuc 	return (0);
86184d9c625SLionel Sambuc 
86284d9c625SLionel Sambuc  unlock_free_exit:
86384d9c625SLionel Sambuc 	VOP_UNLOCK(backing_vnode);
86484d9c625SLionel Sambuc 
86584d9c625SLionel Sambuc  free_exit:
86684d9c625SLionel Sambuc 	kmem_free(attribute, sizeof(*attribute));
86784d9c625SLionel Sambuc 	return (error);
86884d9c625SLionel Sambuc }
86984d9c625SLionel Sambuc 
87084d9c625SLionel Sambuc /*
87184d9c625SLionel Sambuc  * Disable extended attribute support on an FS.
87284d9c625SLionel Sambuc  */
87384d9c625SLionel Sambuc static int
ulfs_extattr_disable(struct ulfsmount * ump,int attrnamespace,const char * attrname,struct lwp * l)87484d9c625SLionel Sambuc ulfs_extattr_disable(struct ulfsmount *ump, int attrnamespace,
87584d9c625SLionel Sambuc     const char *attrname, struct lwp *l)
87684d9c625SLionel Sambuc {
87784d9c625SLionel Sambuc 	struct ulfs_extattr_list_entry *uele;
87884d9c625SLionel Sambuc 	int error = 0;
87984d9c625SLionel Sambuc 
88084d9c625SLionel Sambuc 	if (!ulfs_extattr_valid_attrname(attrnamespace, attrname))
88184d9c625SLionel Sambuc 		return (EINVAL);
88284d9c625SLionel Sambuc 
88384d9c625SLionel Sambuc 	uele = ulfs_extattr_find_attr(ump, attrnamespace, attrname);
88484d9c625SLionel Sambuc 	if (!uele)
88584d9c625SLionel Sambuc 		return (ENODATA);
88684d9c625SLionel Sambuc 
88784d9c625SLionel Sambuc 	LIST_REMOVE(uele, uele_entries);
88884d9c625SLionel Sambuc 
88984d9c625SLionel Sambuc 	error = vn_close(uele->uele_backing_vnode, FREAD|FWRITE,
89084d9c625SLionel Sambuc 	    l->l_cred);
89184d9c625SLionel Sambuc 
89284d9c625SLionel Sambuc 	kmem_free(uele, sizeof(*uele));
89384d9c625SLionel Sambuc 
89484d9c625SLionel Sambuc 	return (error);
89584d9c625SLionel Sambuc }
89684d9c625SLionel Sambuc 
89784d9c625SLionel Sambuc /*
89884d9c625SLionel Sambuc  * VFS call to manage extended attributes in ULFS.  If filename_vp is
89984d9c625SLionel Sambuc  * non-NULL, it must be passed in locked, and regardless of errors in
90084d9c625SLionel Sambuc  * processing, will be unlocked.
90184d9c625SLionel Sambuc  */
90284d9c625SLionel Sambuc int
ulfs_extattrctl(struct mount * mp,int cmd,struct vnode * filename_vp,int attrnamespace,const char * attrname)90384d9c625SLionel Sambuc ulfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
90484d9c625SLionel Sambuc     int attrnamespace, const char *attrname)
90584d9c625SLionel Sambuc {
90684d9c625SLionel Sambuc 	struct lwp *l = curlwp;
90784d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
90884d9c625SLionel Sambuc 	int error;
90984d9c625SLionel Sambuc 
91084d9c625SLionel Sambuc 	/*
91184d9c625SLionel Sambuc 	 * Only privileged processes can configure extended attributes.
91284d9c625SLionel Sambuc 	 */
91384d9c625SLionel Sambuc 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FS_EXTATTR,
91484d9c625SLionel Sambuc 	    0, mp, NULL, NULL);
91584d9c625SLionel Sambuc 	if (error) {
91684d9c625SLionel Sambuc 		if (filename_vp != NULL)
91784d9c625SLionel Sambuc 			VOP_UNLOCK(filename_vp);
91884d9c625SLionel Sambuc 		return (error);
91984d9c625SLionel Sambuc 	}
92084d9c625SLionel Sambuc 
92184d9c625SLionel Sambuc 	switch(cmd) {
92284d9c625SLionel Sambuc 	case ULFS_EXTATTR_CMD_START:
92384d9c625SLionel Sambuc 		if (filename_vp != NULL) {
92484d9c625SLionel Sambuc 			VOP_UNLOCK(filename_vp);
92584d9c625SLionel Sambuc 			return (EINVAL);
92684d9c625SLionel Sambuc 		}
92784d9c625SLionel Sambuc 		if (attrname != NULL)
92884d9c625SLionel Sambuc 			return (EINVAL);
92984d9c625SLionel Sambuc 
93084d9c625SLionel Sambuc 		error = ulfs_extattr_autostart(mp, l);
93184d9c625SLionel Sambuc 		return (error);
93284d9c625SLionel Sambuc 
93384d9c625SLionel Sambuc 	case ULFS_EXTATTR_CMD_STOP:
93484d9c625SLionel Sambuc 		if (filename_vp != NULL) {
93584d9c625SLionel Sambuc 			VOP_UNLOCK(filename_vp);
93684d9c625SLionel Sambuc 			return (EINVAL);
93784d9c625SLionel Sambuc 		}
93884d9c625SLionel Sambuc 		if (attrname != NULL)
93984d9c625SLionel Sambuc 			return (EINVAL);
94084d9c625SLionel Sambuc 
94184d9c625SLionel Sambuc 		ulfs_extattr_stop(mp, l);
94284d9c625SLionel Sambuc 		return (0);
94384d9c625SLionel Sambuc 
94484d9c625SLionel Sambuc 	case ULFS_EXTATTR_CMD_ENABLE:
94584d9c625SLionel Sambuc 		if (filename_vp == NULL)
94684d9c625SLionel Sambuc 			return (EINVAL);
94784d9c625SLionel Sambuc 		if (attrname == NULL) {
94884d9c625SLionel Sambuc 			VOP_UNLOCK(filename_vp);
94984d9c625SLionel Sambuc 			return (EINVAL);
95084d9c625SLionel Sambuc 		}
95184d9c625SLionel Sambuc 
95284d9c625SLionel Sambuc 		/*
95384d9c625SLionel Sambuc 		 * ulfs_extattr_enable_with_open() will always unlock the
95484d9c625SLionel Sambuc 		 * vnode, regardless of failure.
95584d9c625SLionel Sambuc 		 */
95684d9c625SLionel Sambuc 		ulfs_extattr_uepm_lock(ump);
95784d9c625SLionel Sambuc 		error = ulfs_extattr_enable_with_open(ump, filename_vp,
95884d9c625SLionel Sambuc 		    attrnamespace, attrname, l);
95984d9c625SLionel Sambuc 		ulfs_extattr_uepm_unlock(ump);
96084d9c625SLionel Sambuc 		return (error);
96184d9c625SLionel Sambuc 
96284d9c625SLionel Sambuc 	case ULFS_EXTATTR_CMD_DISABLE:
96384d9c625SLionel Sambuc 		if (filename_vp != NULL) {
96484d9c625SLionel Sambuc 			VOP_UNLOCK(filename_vp);
96584d9c625SLionel Sambuc 			return (EINVAL);
96684d9c625SLionel Sambuc 		}
96784d9c625SLionel Sambuc 		if (attrname == NULL)
96884d9c625SLionel Sambuc 			return (EINVAL);
96984d9c625SLionel Sambuc 
97084d9c625SLionel Sambuc 		ulfs_extattr_uepm_lock(ump);
97184d9c625SLionel Sambuc 		error = ulfs_extattr_disable(ump, attrnamespace, attrname, l);
97284d9c625SLionel Sambuc 		ulfs_extattr_uepm_unlock(ump);
97384d9c625SLionel Sambuc 		return (error);
97484d9c625SLionel Sambuc 
97584d9c625SLionel Sambuc 	default:
97684d9c625SLionel Sambuc 		return (EINVAL);
97784d9c625SLionel Sambuc 	}
97884d9c625SLionel Sambuc }
97984d9c625SLionel Sambuc 
98084d9c625SLionel Sambuc /*
98184d9c625SLionel Sambuc  * Read extended attribute header for a given vnode and attribute.
98284d9c625SLionel Sambuc  * Backing vnode should be locked and unlocked by caller.
98384d9c625SLionel Sambuc  */
98484d9c625SLionel Sambuc static int
ulfs_extattr_get_header(struct vnode * vp,struct ulfs_extattr_list_entry * uele,struct ulfs_extattr_header * ueh,off_t * bap)98584d9c625SLionel Sambuc ulfs_extattr_get_header(struct vnode *vp, struct ulfs_extattr_list_entry *uele,
98684d9c625SLionel Sambuc     struct ulfs_extattr_header *ueh, off_t *bap)
98784d9c625SLionel Sambuc {
98884d9c625SLionel Sambuc 	struct mount *mp = vp->v_mount;
98984d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
99084d9c625SLionel Sambuc 	struct inode *ip = VTOI(vp);
99184d9c625SLionel Sambuc 	off_t base_offset;
99284d9c625SLionel Sambuc 	struct iovec aiov;
99384d9c625SLionel Sambuc 	struct uio aio;
99484d9c625SLionel Sambuc 	int error;
99584d9c625SLionel Sambuc 
99684d9c625SLionel Sambuc 	/*
99784d9c625SLionel Sambuc 	 * Find base offset of header in file based on file header size, and
99884d9c625SLionel Sambuc 	 * data header size + maximum data size, indexed by inode number.
99984d9c625SLionel Sambuc 	 */
100084d9c625SLionel Sambuc 	base_offset = sizeof(struct ulfs_extattr_fileheader) +
100184d9c625SLionel Sambuc 	    ip->i_number * (sizeof(struct ulfs_extattr_header) +
100284d9c625SLionel Sambuc 	    uele->uele_fileheader.uef_size);
100384d9c625SLionel Sambuc 
100484d9c625SLionel Sambuc 	/*
100584d9c625SLionel Sambuc 	 * Read in the data header to see if the data is defined, and if so
100684d9c625SLionel Sambuc 	 * how much.
100784d9c625SLionel Sambuc 	 */
100884d9c625SLionel Sambuc 	memset(ueh, 0, sizeof(struct ulfs_extattr_header));
100984d9c625SLionel Sambuc 	aiov.iov_base = ueh;
101084d9c625SLionel Sambuc 	aiov.iov_len = sizeof(struct ulfs_extattr_header);
101184d9c625SLionel Sambuc 	aio.uio_iov = &aiov;
101284d9c625SLionel Sambuc 	aio.uio_iovcnt = 1;
101384d9c625SLionel Sambuc 	aio.uio_rw = UIO_READ;
101484d9c625SLionel Sambuc 	aio.uio_offset = base_offset;
101584d9c625SLionel Sambuc 	aio.uio_resid = sizeof(struct ulfs_extattr_header);
101684d9c625SLionel Sambuc 	UIO_SETUP_SYSSPACE(&aio);
101784d9c625SLionel Sambuc 
101884d9c625SLionel Sambuc 	error = VOP_READ(uele->uele_backing_vnode, &aio,
101984d9c625SLionel Sambuc 	    IO_NODELOCKED, ump->um_extattr.uepm_ucred);
102084d9c625SLionel Sambuc 	if (error)
102184d9c625SLionel Sambuc 		return error;
102284d9c625SLionel Sambuc 
102384d9c625SLionel Sambuc 	/*
102484d9c625SLionel Sambuc 	 * Attribute headers are kept in file system byte order.
102584d9c625SLionel Sambuc 	 * XXX What about the blob of data?
102684d9c625SLionel Sambuc 	 */
102784d9c625SLionel Sambuc 	ueh->ueh_flags = ulfs_rw32(ueh->ueh_flags, UELE_NEEDSWAP(uele));
102884d9c625SLionel Sambuc 	ueh->ueh_len   = ulfs_rw32(ueh->ueh_len, UELE_NEEDSWAP(uele));
102984d9c625SLionel Sambuc 	ueh->ueh_i_gen = ulfs_rw32(ueh->ueh_i_gen, UELE_NEEDSWAP(uele));
103084d9c625SLionel Sambuc 
103184d9c625SLionel Sambuc 	/* Defined? */
103284d9c625SLionel Sambuc 	if ((ueh->ueh_flags & ULFS_EXTATTR_ATTR_FLAG_INUSE) == 0)
103384d9c625SLionel Sambuc 		return ENODATA;
103484d9c625SLionel Sambuc 
103584d9c625SLionel Sambuc 	/* Valid for the current inode generation? */
103684d9c625SLionel Sambuc 	if (ueh->ueh_i_gen != ip->i_gen) {
103784d9c625SLionel Sambuc 		/*
103884d9c625SLionel Sambuc 		 * The inode itself has a different generation number
103984d9c625SLionel Sambuc 		 * than the uele data.  For now, the best solution
104084d9c625SLionel Sambuc 		 * is to coerce this to undefined, and let it get cleaned
104184d9c625SLionel Sambuc 		 * up by the next write or extattrctl clean.
104284d9c625SLionel Sambuc 		 */
104384d9c625SLionel Sambuc 		printf("%s (%s): inode gen inconsistency (%u, %jd)\n",
104484d9c625SLionel Sambuc 		       __func__,  mp->mnt_stat.f_mntonname, ueh->ueh_i_gen,
104584d9c625SLionel Sambuc 		       (intmax_t)ip->i_gen);
104684d9c625SLionel Sambuc 		return ENODATA;
104784d9c625SLionel Sambuc 	}
104884d9c625SLionel Sambuc 
104984d9c625SLionel Sambuc 	/* Local size consistency check. */
105084d9c625SLionel Sambuc 	if (ueh->ueh_len > uele->uele_fileheader.uef_size)
105184d9c625SLionel Sambuc 		return ENXIO;
105284d9c625SLionel Sambuc 
105384d9c625SLionel Sambuc 	/* Return base offset */
105484d9c625SLionel Sambuc 	if (bap != NULL)
105584d9c625SLionel Sambuc 		*bap = base_offset;
105684d9c625SLionel Sambuc 
105784d9c625SLionel Sambuc 	return 0;
105884d9c625SLionel Sambuc }
105984d9c625SLionel Sambuc 
106084d9c625SLionel Sambuc /*
106184d9c625SLionel Sambuc  * Vnode operation to retrieve a named extended attribute.
106284d9c625SLionel Sambuc  */
106384d9c625SLionel Sambuc int
ulfs_getextattr(struct vop_getextattr_args * ap)106484d9c625SLionel Sambuc ulfs_getextattr(struct vop_getextattr_args *ap)
106584d9c625SLionel Sambuc /*
106684d9c625SLionel Sambuc vop_getextattr {
106784d9c625SLionel Sambuc 	IN struct vnode *a_vp;
106884d9c625SLionel Sambuc 	IN int a_attrnamespace;
106984d9c625SLionel Sambuc 	IN const char *a_name;
107084d9c625SLionel Sambuc 	INOUT struct uio *a_uio;
107184d9c625SLionel Sambuc 	OUT size_t *a_size;
107284d9c625SLionel Sambuc 	IN kauth_cred_t a_cred;
107384d9c625SLionel Sambuc };
107484d9c625SLionel Sambuc */
107584d9c625SLionel Sambuc {
107684d9c625SLionel Sambuc 	struct mount *mp = ap->a_vp->v_mount;
107784d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
107884d9c625SLionel Sambuc 	int error;
107984d9c625SLionel Sambuc 
108084d9c625SLionel Sambuc 	ulfs_extattr_uepm_lock(ump);
108184d9c625SLionel Sambuc 
108284d9c625SLionel Sambuc 	error = ulfs_extattr_get(ap->a_vp, ap->a_attrnamespace, ap->a_name,
108384d9c625SLionel Sambuc 	    ap->a_uio, ap->a_size, ap->a_cred, curlwp);
108484d9c625SLionel Sambuc 
108584d9c625SLionel Sambuc 	ulfs_extattr_uepm_unlock(ump);
108684d9c625SLionel Sambuc 
108784d9c625SLionel Sambuc 	return (error);
108884d9c625SLionel Sambuc }
108984d9c625SLionel Sambuc 
109084d9c625SLionel Sambuc /*
109184d9c625SLionel Sambuc  * Real work associated with retrieving a named attribute--assumes that
109284d9c625SLionel Sambuc  * the attribute lock has already been grabbed.
109384d9c625SLionel Sambuc  */
109484d9c625SLionel Sambuc static int
ulfs_extattr_get(struct vnode * vp,int attrnamespace,const char * name,struct uio * uio,size_t * size,kauth_cred_t cred,struct lwp * l)109584d9c625SLionel Sambuc ulfs_extattr_get(struct vnode *vp, int attrnamespace, const char *name,
109684d9c625SLionel Sambuc     struct uio *uio, size_t *size, kauth_cred_t cred, struct lwp *l)
109784d9c625SLionel Sambuc {
109884d9c625SLionel Sambuc 	struct ulfs_extattr_list_entry *attribute;
109984d9c625SLionel Sambuc 	struct ulfs_extattr_header ueh;
110084d9c625SLionel Sambuc 	struct mount *mp = vp->v_mount;
110184d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
110284d9c625SLionel Sambuc 	off_t base_offset;
110384d9c625SLionel Sambuc 	size_t len, old_len;
110484d9c625SLionel Sambuc 	int error = 0;
110584d9c625SLionel Sambuc 
110684d9c625SLionel Sambuc 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED))
110784d9c625SLionel Sambuc 		return (EOPNOTSUPP);
110884d9c625SLionel Sambuc 
110984d9c625SLionel Sambuc 	if (strlen(name) == 0)
111084d9c625SLionel Sambuc 		return (EINVAL);
111184d9c625SLionel Sambuc 
111284d9c625SLionel Sambuc 	error = internal_extattr_check_cred(vp, attrnamespace, name, cred,
111384d9c625SLionel Sambuc 	    VREAD);
111484d9c625SLionel Sambuc 	if (error)
111584d9c625SLionel Sambuc 		return (error);
111684d9c625SLionel Sambuc 
111784d9c625SLionel Sambuc 	attribute = ulfs_extattr_find_attr(ump, attrnamespace, name);
111884d9c625SLionel Sambuc 	if (!attribute)
111984d9c625SLionel Sambuc 		return (ENODATA);
112084d9c625SLionel Sambuc 
112184d9c625SLionel Sambuc 	/*
112284d9c625SLionel Sambuc 	 * Allow only offsets of zero to encourage the read/replace
112384d9c625SLionel Sambuc 	 * extended attribute semantic.  Otherwise we can't guarantee
112484d9c625SLionel Sambuc 	 * atomicity, as we don't provide locks for extended attributes.
112584d9c625SLionel Sambuc 	 */
112684d9c625SLionel Sambuc 	if (uio != NULL && uio->uio_offset != 0)
112784d9c625SLionel Sambuc 		return (ENXIO);
112884d9c625SLionel Sambuc 
112984d9c625SLionel Sambuc 	/*
113084d9c625SLionel Sambuc 	 * Don't need to get a lock on the backing file if the getattr is
113184d9c625SLionel Sambuc 	 * being applied to the backing file, as the lock is already held.
113284d9c625SLionel Sambuc 	 */
113384d9c625SLionel Sambuc 	if (attribute->uele_backing_vnode != vp)
113484d9c625SLionel Sambuc 		vn_lock(attribute->uele_backing_vnode, LK_SHARED | LK_RETRY);
113584d9c625SLionel Sambuc 
113684d9c625SLionel Sambuc 	error = ulfs_extattr_get_header(vp, attribute, &ueh, &base_offset);
113784d9c625SLionel Sambuc 	if (error)
113884d9c625SLionel Sambuc 		goto vopunlock_exit;
113984d9c625SLionel Sambuc 
114084d9c625SLionel Sambuc 	/* Return full data size if caller requested it. */
114184d9c625SLionel Sambuc 	if (size != NULL)
114284d9c625SLionel Sambuc 		*size = ueh.ueh_len;
114384d9c625SLionel Sambuc 
114484d9c625SLionel Sambuc 	/* Return data if the caller requested it. */
114584d9c625SLionel Sambuc 	if (uio != NULL) {
114684d9c625SLionel Sambuc 		/* Allow for offset into the attribute data. */
114784d9c625SLionel Sambuc 		uio->uio_offset = base_offset + sizeof(struct
114884d9c625SLionel Sambuc 		    ulfs_extattr_header);
114984d9c625SLionel Sambuc 
115084d9c625SLionel Sambuc 		/*
115184d9c625SLionel Sambuc 		 * Figure out maximum to transfer -- use buffer size and
115284d9c625SLionel Sambuc 		 * local data limit.
115384d9c625SLionel Sambuc 		 */
115484d9c625SLionel Sambuc 		len = MIN(uio->uio_resid, ueh.ueh_len);
115584d9c625SLionel Sambuc 		old_len = uio->uio_resid;
115684d9c625SLionel Sambuc 		uio->uio_resid = len;
115784d9c625SLionel Sambuc 
115884d9c625SLionel Sambuc 		error = VOP_READ(attribute->uele_backing_vnode, uio,
115984d9c625SLionel Sambuc 		    IO_NODELOCKED, ump->um_extattr.uepm_ucred);
116084d9c625SLionel Sambuc 		if (error)
116184d9c625SLionel Sambuc 			goto vopunlock_exit;
116284d9c625SLionel Sambuc 
116384d9c625SLionel Sambuc 		uio->uio_resid = old_len - (len - uio->uio_resid);
116484d9c625SLionel Sambuc 	}
116584d9c625SLionel Sambuc 
116684d9c625SLionel Sambuc  vopunlock_exit:
116784d9c625SLionel Sambuc 
116884d9c625SLionel Sambuc 	if (uio != NULL)
116984d9c625SLionel Sambuc 		uio->uio_offset = 0;
117084d9c625SLionel Sambuc 
117184d9c625SLionel Sambuc 	if (attribute->uele_backing_vnode != vp)
117284d9c625SLionel Sambuc 		VOP_UNLOCK(attribute->uele_backing_vnode);
117384d9c625SLionel Sambuc 
117484d9c625SLionel Sambuc 	return (error);
117584d9c625SLionel Sambuc }
117684d9c625SLionel Sambuc 
117784d9c625SLionel Sambuc /*
117884d9c625SLionel Sambuc  * Vnode operation to list extended attribute for a vnode
117984d9c625SLionel Sambuc  */
118084d9c625SLionel Sambuc int
ulfs_listextattr(struct vop_listextattr_args * ap)118184d9c625SLionel Sambuc ulfs_listextattr(struct vop_listextattr_args *ap)
118284d9c625SLionel Sambuc /*
118384d9c625SLionel Sambuc vop_listextattr {
118484d9c625SLionel Sambuc 	IN struct vnode *a_vp;
118584d9c625SLionel Sambuc 	IN int a_attrnamespace;
118684d9c625SLionel Sambuc 	INOUT struct uio *a_uio;
118784d9c625SLionel Sambuc 	OUT size_t *a_size;
118884d9c625SLionel Sambuc 	IN int flag;
118984d9c625SLionel Sambuc 	IN kauth_cred_t a_cred;
119084d9c625SLionel Sambuc 	struct proc *a_p;
119184d9c625SLionel Sambuc };
119284d9c625SLionel Sambuc */
119384d9c625SLionel Sambuc {
119484d9c625SLionel Sambuc 	struct mount *mp = ap->a_vp->v_mount;
119584d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
119684d9c625SLionel Sambuc 	int error;
119784d9c625SLionel Sambuc 
119884d9c625SLionel Sambuc 	ulfs_extattr_uepm_lock(ump);
119984d9c625SLionel Sambuc 
120084d9c625SLionel Sambuc 	error = ulfs_extattr_list(ap->a_vp, ap->a_attrnamespace,
120184d9c625SLionel Sambuc 	    ap->a_uio, ap->a_size, ap->a_flag, ap->a_cred, curlwp);
120284d9c625SLionel Sambuc 
120384d9c625SLionel Sambuc 	ulfs_extattr_uepm_unlock(ump);
120484d9c625SLionel Sambuc 
120584d9c625SLionel Sambuc 	return (error);
120684d9c625SLionel Sambuc }
120784d9c625SLionel Sambuc 
120884d9c625SLionel Sambuc /*
120984d9c625SLionel Sambuc  * Real work associated with retrieving list of attributes--assumes that
121084d9c625SLionel Sambuc  * the attribute lock has already been grabbed.
121184d9c625SLionel Sambuc  */
121284d9c625SLionel Sambuc static int
ulfs_extattr_list(struct vnode * vp,int attrnamespace,struct uio * uio,size_t * size,int flag,kauth_cred_t cred,struct lwp * l)121384d9c625SLionel Sambuc ulfs_extattr_list(struct vnode *vp, int attrnamespace,
121484d9c625SLionel Sambuc     struct uio *uio, size_t *size, int flag,
121584d9c625SLionel Sambuc     kauth_cred_t cred, struct lwp *l)
121684d9c625SLionel Sambuc {
121784d9c625SLionel Sambuc 	struct ulfs_extattr_list_entry *uele;
121884d9c625SLionel Sambuc 	struct ulfs_extattr_header ueh;
121984d9c625SLionel Sambuc 	struct mount *mp = vp->v_mount;
122084d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
122184d9c625SLionel Sambuc 	size_t listsize = 0;
122284d9c625SLionel Sambuc 	int error = 0;
122384d9c625SLionel Sambuc 
122484d9c625SLionel Sambuc 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED))
122584d9c625SLionel Sambuc 		return (EOPNOTSUPP);
122684d9c625SLionel Sambuc 
122784d9c625SLionel Sambuc 	/*
122884d9c625SLionel Sambuc 	 * XXX: We can move this inside the loop and iterate on individual
122984d9c625SLionel Sambuc 	 *	attributes.
123084d9c625SLionel Sambuc 	 */
123184d9c625SLionel Sambuc 	error = internal_extattr_check_cred(vp, attrnamespace, "", cred,
123284d9c625SLionel Sambuc 	    VREAD);
123384d9c625SLionel Sambuc 	if (error)
123484d9c625SLionel Sambuc 		return (error);
123584d9c625SLionel Sambuc 
123684d9c625SLionel Sambuc 	LIST_FOREACH(uele, &ump->um_extattr.uepm_list, uele_entries) {
123784d9c625SLionel Sambuc 		unsigned char attrnamelen;
123884d9c625SLionel Sambuc 
123984d9c625SLionel Sambuc 		if (uele->uele_attrnamespace != attrnamespace)
124084d9c625SLionel Sambuc 			continue;
124184d9c625SLionel Sambuc 
124284d9c625SLionel Sambuc 		error = ulfs_extattr_get_header(vp, uele, &ueh, NULL);
124384d9c625SLionel Sambuc 		if (error == ENODATA)
124484d9c625SLionel Sambuc 			continue;
124584d9c625SLionel Sambuc 		if (error != 0)
124684d9c625SLionel Sambuc 			return error;
124784d9c625SLionel Sambuc 
124884d9c625SLionel Sambuc 		/*
124984d9c625SLionel Sambuc 		 * Don't need to get a lock on the backing file if
125084d9c625SLionel Sambuc 		 * the listattr is being applied to the backing file,
125184d9c625SLionel Sambuc 		 * as the lock is already held.
125284d9c625SLionel Sambuc 		 */
125384d9c625SLionel Sambuc 		if (uele->uele_backing_vnode != vp)
125484d9c625SLionel Sambuc 			vn_lock(uele->uele_backing_vnode, LK_SHARED | LK_RETRY);
125584d9c625SLionel Sambuc 
125684d9c625SLionel Sambuc 		/*
125784d9c625SLionel Sambuc 		 * +1 for trailing NUL (listxattr flavor)
125884d9c625SLionel Sambuc 		 *  or leading name length (extattr_list_file flavor)
125984d9c625SLionel Sambuc 	 	 */
126084d9c625SLionel Sambuc 		attrnamelen = strlen(uele->uele_attrname);
126184d9c625SLionel Sambuc 		listsize += attrnamelen + 1;
126284d9c625SLionel Sambuc 
126384d9c625SLionel Sambuc 		/* Return data if the caller requested it. */
126484d9c625SLionel Sambuc 		if (uio != NULL) {
126584d9c625SLionel Sambuc 			/*
126684d9c625SLionel Sambuc 			 * We support two flavors. Either NUL-terminated
126784d9c625SLionel Sambuc 			 * strings (a la listxattr), or non NUL-terminated,
126884d9c625SLionel Sambuc 			 * one byte length prefixed strings (for
126984d9c625SLionel Sambuc 			 * extattr_list_file). EXTATTR_LIST_LENPREFIX switches
127084d9c625SLionel Sambuc 		 	 * that second behavior.
127184d9c625SLionel Sambuc 			 */
127284d9c625SLionel Sambuc 			if (flag & EXTATTR_LIST_LENPREFIX) {
127384d9c625SLionel Sambuc 				uint8_t len = (uint8_t)attrnamelen;
127484d9c625SLionel Sambuc 
127584d9c625SLionel Sambuc 				/* Copy leading name length */
127684d9c625SLionel Sambuc 				error = uiomove(&len, sizeof(len), uio);
127784d9c625SLionel Sambuc 				if (error != 0)
127884d9c625SLionel Sambuc 					break;
127984d9c625SLionel Sambuc 			} else {
128084d9c625SLionel Sambuc 				/* Include trailing NULL */
128184d9c625SLionel Sambuc 				attrnamelen++;
128284d9c625SLionel Sambuc 			}
128384d9c625SLionel Sambuc 
128484d9c625SLionel Sambuc 			error = uiomove(uele->uele_attrname,
128584d9c625SLionel Sambuc 					(size_t)attrnamelen, uio);
128684d9c625SLionel Sambuc 			if (error != 0)
128784d9c625SLionel Sambuc 				break;
128884d9c625SLionel Sambuc 		}
128984d9c625SLionel Sambuc 
129084d9c625SLionel Sambuc 		if (uele->uele_backing_vnode != vp)
129184d9c625SLionel Sambuc 			VOP_UNLOCK(uele->uele_backing_vnode);
129284d9c625SLionel Sambuc 
129384d9c625SLionel Sambuc 		if (error != 0)
129484d9c625SLionel Sambuc 			return error;
129584d9c625SLionel Sambuc 	}
129684d9c625SLionel Sambuc 
129784d9c625SLionel Sambuc 	if (uio != NULL)
129884d9c625SLionel Sambuc 		uio->uio_offset = 0;
129984d9c625SLionel Sambuc 
130084d9c625SLionel Sambuc 	/* Return full data size if caller requested it. */
130184d9c625SLionel Sambuc 	if (size != NULL)
130284d9c625SLionel Sambuc 		*size = listsize;
130384d9c625SLionel Sambuc 
130484d9c625SLionel Sambuc 	return 0;
130584d9c625SLionel Sambuc }
130684d9c625SLionel Sambuc 
130784d9c625SLionel Sambuc /*
130884d9c625SLionel Sambuc  * Vnode operation to remove a named attribute.
130984d9c625SLionel Sambuc  */
131084d9c625SLionel Sambuc int
ulfs_deleteextattr(struct vop_deleteextattr_args * ap)131184d9c625SLionel Sambuc ulfs_deleteextattr(struct vop_deleteextattr_args *ap)
131284d9c625SLionel Sambuc /*
131384d9c625SLionel Sambuc vop_deleteextattr {
131484d9c625SLionel Sambuc 	IN struct vnode *a_vp;
131584d9c625SLionel Sambuc 	IN int a_attrnamespace;
131684d9c625SLionel Sambuc 	IN const char *a_name;
131784d9c625SLionel Sambuc 	IN kauth_cred_t a_cred;
131884d9c625SLionel Sambuc };
131984d9c625SLionel Sambuc */
132084d9c625SLionel Sambuc {
132184d9c625SLionel Sambuc 	struct mount *mp = ap->a_vp->v_mount;
132284d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
132384d9c625SLionel Sambuc 	int error;
132484d9c625SLionel Sambuc 
132584d9c625SLionel Sambuc 	ulfs_extattr_uepm_lock(ump);
132684d9c625SLionel Sambuc 
132784d9c625SLionel Sambuc 	error = ulfs_extattr_rm(ap->a_vp, ap->a_attrnamespace, ap->a_name,
132884d9c625SLionel Sambuc 	    ap->a_cred, curlwp);
132984d9c625SLionel Sambuc 
133084d9c625SLionel Sambuc 	ulfs_extattr_uepm_unlock(ump);
133184d9c625SLionel Sambuc 
133284d9c625SLionel Sambuc 	return (error);
133384d9c625SLionel Sambuc }
133484d9c625SLionel Sambuc 
133584d9c625SLionel Sambuc /*
133684d9c625SLionel Sambuc  * Vnode operation to set a named attribute.
133784d9c625SLionel Sambuc  */
133884d9c625SLionel Sambuc int
ulfs_setextattr(struct vop_setextattr_args * ap)133984d9c625SLionel Sambuc ulfs_setextattr(struct vop_setextattr_args *ap)
134084d9c625SLionel Sambuc /*
134184d9c625SLionel Sambuc vop_setextattr {
134284d9c625SLionel Sambuc 	IN struct vnode *a_vp;
134384d9c625SLionel Sambuc 	IN int a_attrnamespace;
134484d9c625SLionel Sambuc 	IN const char *a_name;
134584d9c625SLionel Sambuc 	INOUT struct uio *a_uio;
134684d9c625SLionel Sambuc 	IN kauth_cred_t a_cred;
134784d9c625SLionel Sambuc };
134884d9c625SLionel Sambuc */
134984d9c625SLionel Sambuc {
135084d9c625SLionel Sambuc 	struct mount *mp = ap->a_vp->v_mount;
135184d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
135284d9c625SLionel Sambuc 	int error;
135384d9c625SLionel Sambuc 
135484d9c625SLionel Sambuc 	ulfs_extattr_uepm_lock(ump);
135584d9c625SLionel Sambuc 
135684d9c625SLionel Sambuc 	/*
135784d9c625SLionel Sambuc 	 * XXX: No longer a supported way to delete extended attributes.
135884d9c625SLionel Sambuc 	 */
135984d9c625SLionel Sambuc 	if (ap->a_uio == NULL) {
136084d9c625SLionel Sambuc 		ulfs_extattr_uepm_unlock(ump);
136184d9c625SLionel Sambuc 		return (EINVAL);
136284d9c625SLionel Sambuc 	}
136384d9c625SLionel Sambuc 
136484d9c625SLionel Sambuc 	error = ulfs_extattr_set(ap->a_vp, ap->a_attrnamespace, ap->a_name,
136584d9c625SLionel Sambuc 	    ap->a_uio, ap->a_cred, curlwp);
136684d9c625SLionel Sambuc 
136784d9c625SLionel Sambuc 	ulfs_extattr_uepm_unlock(ump);
136884d9c625SLionel Sambuc 
136984d9c625SLionel Sambuc 	return (error);
137084d9c625SLionel Sambuc }
137184d9c625SLionel Sambuc 
137284d9c625SLionel Sambuc /*
137384d9c625SLionel Sambuc  * Real work associated with setting a vnode's extended attributes;
137484d9c625SLionel Sambuc  * assumes that the attribute lock has already been grabbed.
137584d9c625SLionel Sambuc  */
137684d9c625SLionel Sambuc static int
ulfs_extattr_set(struct vnode * vp,int attrnamespace,const char * name,struct uio * uio,kauth_cred_t cred,struct lwp * l)137784d9c625SLionel Sambuc ulfs_extattr_set(struct vnode *vp, int attrnamespace, const char *name,
137884d9c625SLionel Sambuc     struct uio *uio, kauth_cred_t cred, struct lwp *l)
137984d9c625SLionel Sambuc {
138084d9c625SLionel Sambuc 	struct ulfs_extattr_list_entry *attribute;
138184d9c625SLionel Sambuc 	struct ulfs_extattr_header ueh;
138284d9c625SLionel Sambuc 	struct iovec local_aiov;
138384d9c625SLionel Sambuc 	struct uio local_aio;
138484d9c625SLionel Sambuc 	struct mount *mp = vp->v_mount;
138584d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
138684d9c625SLionel Sambuc 	struct inode *ip = VTOI(vp);
138784d9c625SLionel Sambuc 	off_t base_offset;
138884d9c625SLionel Sambuc 	int error = 0, ioflag;
138984d9c625SLionel Sambuc 
139084d9c625SLionel Sambuc 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
139184d9c625SLionel Sambuc 		return (EROFS);
139284d9c625SLionel Sambuc 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED))
139384d9c625SLionel Sambuc 		return (EOPNOTSUPP);
139484d9c625SLionel Sambuc 	if (!ulfs_extattr_valid_attrname(attrnamespace, name))
139584d9c625SLionel Sambuc 		return (EINVAL);
139684d9c625SLionel Sambuc 
139784d9c625SLionel Sambuc 	error = internal_extattr_check_cred(vp, attrnamespace, name, cred,
139884d9c625SLionel Sambuc 	    VWRITE);
139984d9c625SLionel Sambuc 	if (error)
140084d9c625SLionel Sambuc 		return (error);
140184d9c625SLionel Sambuc 
140284d9c625SLionel Sambuc 	attribute = ulfs_extattr_find_attr(ump, attrnamespace, name);
140384d9c625SLionel Sambuc 	if (!attribute) {
140484d9c625SLionel Sambuc 		attribute =  ulfs_extattr_autocreate_attr(vp, attrnamespace,
140584d9c625SLionel Sambuc 							 name, l);
140684d9c625SLionel Sambuc 		if  (!attribute)
140784d9c625SLionel Sambuc 			return (ENODATA);
140884d9c625SLionel Sambuc 	}
140984d9c625SLionel Sambuc 
141084d9c625SLionel Sambuc 	/*
141184d9c625SLionel Sambuc 	 * Early rejection of invalid offsets/length.
141284d9c625SLionel Sambuc 	 * Reject: any offset but 0 (replace)
141384d9c625SLionel Sambuc 	 *	 Any size greater than attribute size limit
141484d9c625SLionel Sambuc  	 */
141584d9c625SLionel Sambuc 	if (uio->uio_offset != 0 ||
141684d9c625SLionel Sambuc 	    uio->uio_resid > attribute->uele_fileheader.uef_size)
141784d9c625SLionel Sambuc 		return (ENXIO);
141884d9c625SLionel Sambuc 
141984d9c625SLionel Sambuc 	/*
142084d9c625SLionel Sambuc 	 * Find base offset of header in file based on file header size, and
142184d9c625SLionel Sambuc 	 * data header size + maximum data size, indexed by inode number.
142284d9c625SLionel Sambuc 	 */
142384d9c625SLionel Sambuc 	base_offset = sizeof(struct ulfs_extattr_fileheader) +
142484d9c625SLionel Sambuc 	    ip->i_number * (sizeof(struct ulfs_extattr_header) +
142584d9c625SLionel Sambuc 	    attribute->uele_fileheader.uef_size);
142684d9c625SLionel Sambuc 
142784d9c625SLionel Sambuc 	/*
142884d9c625SLionel Sambuc 	 * Write out a data header for the data.
142984d9c625SLionel Sambuc 	 */
143084d9c625SLionel Sambuc 	ueh.ueh_len = ulfs_rw32((uint32_t) uio->uio_resid,
143184d9c625SLionel Sambuc 	    UELE_NEEDSWAP(attribute));
143284d9c625SLionel Sambuc 	ueh.ueh_flags = ulfs_rw32(ULFS_EXTATTR_ATTR_FLAG_INUSE,
143384d9c625SLionel Sambuc 				 UELE_NEEDSWAP(attribute));
143484d9c625SLionel Sambuc 	ueh.ueh_i_gen = ulfs_rw32(ip->i_gen, UELE_NEEDSWAP(attribute));
143584d9c625SLionel Sambuc 	local_aiov.iov_base = &ueh;
143684d9c625SLionel Sambuc 	local_aiov.iov_len = sizeof(struct ulfs_extattr_header);
143784d9c625SLionel Sambuc 	local_aio.uio_iov = &local_aiov;
143884d9c625SLionel Sambuc 	local_aio.uio_iovcnt = 1;
143984d9c625SLionel Sambuc 	local_aio.uio_rw = UIO_WRITE;
144084d9c625SLionel Sambuc 	local_aio.uio_offset = base_offset;
144184d9c625SLionel Sambuc 	local_aio.uio_resid = sizeof(struct ulfs_extattr_header);
144284d9c625SLionel Sambuc 	UIO_SETUP_SYSSPACE(&local_aio);
144384d9c625SLionel Sambuc 
144484d9c625SLionel Sambuc 	/*
144584d9c625SLionel Sambuc 	 * Don't need to get a lock on the backing file if the setattr is
144684d9c625SLionel Sambuc 	 * being applied to the backing file, as the lock is already held.
144784d9c625SLionel Sambuc 	 */
144884d9c625SLionel Sambuc 	if (attribute->uele_backing_vnode != vp)
144984d9c625SLionel Sambuc 		vn_lock(attribute->uele_backing_vnode,
145084d9c625SLionel Sambuc 		    LK_EXCLUSIVE | LK_RETRY);
145184d9c625SLionel Sambuc 
145284d9c625SLionel Sambuc 	ioflag = IO_NODELOCKED;
145384d9c625SLionel Sambuc 	if (ulfs_extattr_sync)
145484d9c625SLionel Sambuc 		ioflag |= IO_SYNC;
145584d9c625SLionel Sambuc 	error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
145684d9c625SLionel Sambuc 	    ump->um_extattr.uepm_ucred);
145784d9c625SLionel Sambuc 	if (error)
145884d9c625SLionel Sambuc 		goto vopunlock_exit;
145984d9c625SLionel Sambuc 
146084d9c625SLionel Sambuc 	if (local_aio.uio_resid != 0) {
146184d9c625SLionel Sambuc 		error = ENXIO;
146284d9c625SLionel Sambuc 		goto vopunlock_exit;
146384d9c625SLionel Sambuc 	}
146484d9c625SLionel Sambuc 
146584d9c625SLionel Sambuc 	/*
146684d9c625SLionel Sambuc 	 * Write out user data.
146784d9c625SLionel Sambuc 	 * XXX NOT ATOMIC WITH RESPECT TO THE HEADER.
146884d9c625SLionel Sambuc 	 */
146984d9c625SLionel Sambuc 	uio->uio_offset = base_offset + sizeof(struct ulfs_extattr_header);
147084d9c625SLionel Sambuc 
147184d9c625SLionel Sambuc 	ioflag = IO_NODELOCKED;
147284d9c625SLionel Sambuc 	if (ulfs_extattr_sync)
147384d9c625SLionel Sambuc 		ioflag |= IO_SYNC;
147484d9c625SLionel Sambuc 	error = VOP_WRITE(attribute->uele_backing_vnode, uio, ioflag,
147584d9c625SLionel Sambuc 	    ump->um_extattr.uepm_ucred);
147684d9c625SLionel Sambuc 
147784d9c625SLionel Sambuc  vopunlock_exit:
147884d9c625SLionel Sambuc 	uio->uio_offset = 0;
147984d9c625SLionel Sambuc 
148084d9c625SLionel Sambuc 	if (attribute->uele_backing_vnode != vp)
148184d9c625SLionel Sambuc 		VOP_UNLOCK(attribute->uele_backing_vnode);
148284d9c625SLionel Sambuc 
148384d9c625SLionel Sambuc 	return (error);
148484d9c625SLionel Sambuc }
148584d9c625SLionel Sambuc 
148684d9c625SLionel Sambuc /*
148784d9c625SLionel Sambuc  * Real work associated with removing an extended attribute from a vnode.
148884d9c625SLionel Sambuc  * Assumes the attribute lock has already been grabbed.
148984d9c625SLionel Sambuc  */
149084d9c625SLionel Sambuc static int
ulfs_extattr_rm(struct vnode * vp,int attrnamespace,const char * name,kauth_cred_t cred,struct lwp * l)149184d9c625SLionel Sambuc ulfs_extattr_rm(struct vnode *vp, int attrnamespace, const char *name,
149284d9c625SLionel Sambuc     kauth_cred_t cred, struct lwp *l)
149384d9c625SLionel Sambuc {
149484d9c625SLionel Sambuc 	struct ulfs_extattr_list_entry *attribute;
149584d9c625SLionel Sambuc 	struct ulfs_extattr_header ueh;
149684d9c625SLionel Sambuc 	struct mount *mp = vp->v_mount;
149784d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
149884d9c625SLionel Sambuc 	struct iovec local_aiov;
149984d9c625SLionel Sambuc 	struct uio local_aio;
150084d9c625SLionel Sambuc 	off_t base_offset;
150184d9c625SLionel Sambuc 	int error = 0, ioflag;
150284d9c625SLionel Sambuc 
150384d9c625SLionel Sambuc 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
150484d9c625SLionel Sambuc 		return (EROFS);
150584d9c625SLionel Sambuc 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED))
150684d9c625SLionel Sambuc 		return (EOPNOTSUPP);
150784d9c625SLionel Sambuc 	if (!ulfs_extattr_valid_attrname(attrnamespace, name))
150884d9c625SLionel Sambuc 		return (EINVAL);
150984d9c625SLionel Sambuc 
151084d9c625SLionel Sambuc 	error = internal_extattr_check_cred(vp, attrnamespace, name, cred,
151184d9c625SLionel Sambuc 	    VWRITE);
151284d9c625SLionel Sambuc 	if (error)
151384d9c625SLionel Sambuc 		return (error);
151484d9c625SLionel Sambuc 
151584d9c625SLionel Sambuc 	attribute = ulfs_extattr_find_attr(ump, attrnamespace, name);
151684d9c625SLionel Sambuc 	if (!attribute)
151784d9c625SLionel Sambuc 		return (ENODATA);
151884d9c625SLionel Sambuc 
151984d9c625SLionel Sambuc 	/*
152084d9c625SLionel Sambuc 	 * Don't need to get a lock on the backing file if the getattr is
152184d9c625SLionel Sambuc 	 * being applied to the backing file, as the lock is already held.
152284d9c625SLionel Sambuc 	 */
152384d9c625SLionel Sambuc 	if (attribute->uele_backing_vnode != vp)
152484d9c625SLionel Sambuc 		vn_lock(attribute->uele_backing_vnode, LK_EXCLUSIVE | LK_RETRY);
152584d9c625SLionel Sambuc 
152684d9c625SLionel Sambuc 	error = ulfs_extattr_get_header(vp, attribute, &ueh, &base_offset);
152784d9c625SLionel Sambuc 	if (error)
152884d9c625SLionel Sambuc 		goto vopunlock_exit;
152984d9c625SLionel Sambuc 
153084d9c625SLionel Sambuc 	/* Flag it as not in use. */
153184d9c625SLionel Sambuc 	ueh.ueh_flags = 0;		/* No need to byte swap 0 */
153284d9c625SLionel Sambuc 	ueh.ueh_len = 0;		/* ...ditto... */
153384d9c625SLionel Sambuc 
153484d9c625SLionel Sambuc 	local_aiov.iov_base = &ueh;
153584d9c625SLionel Sambuc 	local_aiov.iov_len = sizeof(struct ulfs_extattr_header);
153684d9c625SLionel Sambuc 	local_aio.uio_iov = &local_aiov;
153784d9c625SLionel Sambuc 	local_aio.uio_iovcnt = 1;
153884d9c625SLionel Sambuc 	local_aio.uio_rw = UIO_WRITE;
153984d9c625SLionel Sambuc 	local_aio.uio_offset = base_offset;
154084d9c625SLionel Sambuc 	local_aio.uio_resid = sizeof(struct ulfs_extattr_header);
154184d9c625SLionel Sambuc 	UIO_SETUP_SYSSPACE(&local_aio);
154284d9c625SLionel Sambuc 
154384d9c625SLionel Sambuc 	ioflag = IO_NODELOCKED;
154484d9c625SLionel Sambuc 	if (ulfs_extattr_sync)
154584d9c625SLionel Sambuc 		ioflag |= IO_SYNC;
154684d9c625SLionel Sambuc 	error = VOP_WRITE(attribute->uele_backing_vnode, &local_aio, ioflag,
154784d9c625SLionel Sambuc 	    ump->um_extattr.uepm_ucred);
154884d9c625SLionel Sambuc 	if (error)
154984d9c625SLionel Sambuc 		goto vopunlock_exit;
155084d9c625SLionel Sambuc 
155184d9c625SLionel Sambuc 	if (local_aio.uio_resid != 0)
155284d9c625SLionel Sambuc 		error = ENXIO;
155384d9c625SLionel Sambuc 
155484d9c625SLionel Sambuc  vopunlock_exit:
155584d9c625SLionel Sambuc 	VOP_UNLOCK(attribute->uele_backing_vnode);
155684d9c625SLionel Sambuc 
155784d9c625SLionel Sambuc 	return (error);
155884d9c625SLionel Sambuc }
155984d9c625SLionel Sambuc 
156084d9c625SLionel Sambuc /*
156184d9c625SLionel Sambuc  * Called by ULFS when an inode is no longer active and should have its
156284d9c625SLionel Sambuc  * attributes stripped.
156384d9c625SLionel Sambuc  */
156484d9c625SLionel Sambuc void
ulfs_extattr_vnode_inactive(struct vnode * vp,struct lwp * l)156584d9c625SLionel Sambuc ulfs_extattr_vnode_inactive(struct vnode *vp, struct lwp *l)
156684d9c625SLionel Sambuc {
156784d9c625SLionel Sambuc 	struct ulfs_extattr_list_entry *uele;
156884d9c625SLionel Sambuc 	struct mount *mp = vp->v_mount;
156984d9c625SLionel Sambuc 	struct ulfsmount *ump = VFSTOULFS(mp);
157084d9c625SLionel Sambuc 
157184d9c625SLionel Sambuc 	/*
157284d9c625SLionel Sambuc 	 * In that case, we cannot lock. We should not have any active vnodes
157384d9c625SLionel Sambuc 	 * on the fs if this is not yet initialized but is going to be, so
157484d9c625SLionel Sambuc 	 * this can go unlocked.
157584d9c625SLionel Sambuc 	 */
157684d9c625SLionel Sambuc 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_INITIALIZED))
157784d9c625SLionel Sambuc 		return;
157884d9c625SLionel Sambuc 
157984d9c625SLionel Sambuc 	ulfs_extattr_uepm_lock(ump);
158084d9c625SLionel Sambuc 
158184d9c625SLionel Sambuc 	if (!(ump->um_extattr.uepm_flags & ULFS_EXTATTR_UEPM_STARTED)) {
158284d9c625SLionel Sambuc 		ulfs_extattr_uepm_unlock(ump);
158384d9c625SLionel Sambuc 		return;
158484d9c625SLionel Sambuc 	}
158584d9c625SLionel Sambuc 
158684d9c625SLionel Sambuc 	LIST_FOREACH(uele, &ump->um_extattr.uepm_list, uele_entries)
158784d9c625SLionel Sambuc 		ulfs_extattr_rm(vp, uele->uele_attrnamespace,
158884d9c625SLionel Sambuc 		    uele->uele_attrname, lwp0.l_cred, l);
158984d9c625SLionel Sambuc 
159084d9c625SLionel Sambuc 	ulfs_extattr_uepm_unlock(ump);
159184d9c625SLionel Sambuc }
159284d9c625SLionel Sambuc 
159384d9c625SLionel Sambuc void
ulfs_extattr_init(void)159484d9c625SLionel Sambuc ulfs_extattr_init(void)
159584d9c625SLionel Sambuc {
159684d9c625SLionel Sambuc 
159784d9c625SLionel Sambuc }
159884d9c625SLionel Sambuc 
159984d9c625SLionel Sambuc void
ulfs_extattr_done(void)160084d9c625SLionel Sambuc ulfs_extattr_done(void)
160184d9c625SLionel Sambuc {
160284d9c625SLionel Sambuc 
160384d9c625SLionel Sambuc }
1604