Home | History | Annotate | Line # | Download | only in kern
vfs_xattr.c revision 1.28.2.2
      1  1.28.2.2      yamt /*	$NetBSD: vfs_xattr.c,v 1.28.2.2 2012/04/17 00:08:33 yamt Exp $	*/
      2       1.2   thorpej 
      3       1.2   thorpej /*-
      4      1.17        ad  * Copyright (c) 2005, 2008 The NetBSD Foundation, Inc.
      5       1.2   thorpej  * All rights reserved.
      6       1.2   thorpej  *
      7       1.2   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.2   thorpej  * by Jason R. Thorpe.
      9       1.2   thorpej  *
     10       1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     11       1.2   thorpej  * modification, are permitted provided that the following conditions
     12       1.2   thorpej  * are met:
     13       1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     14       1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     15       1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17       1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     18       1.2   thorpej  *
     19       1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30       1.2   thorpej  */
     31       1.1   thorpej 
     32       1.1   thorpej /*
     33       1.1   thorpej  * Copyright (c) 1989, 1993
     34       1.1   thorpej  *	The Regents of the University of California.  All rights reserved.
     35       1.1   thorpej  * (c) UNIX System Laboratories, Inc.
     36       1.1   thorpej  * All or some portions of this file are derived from material licensed
     37       1.1   thorpej  * to the University of California by American Telephone and Telegraph
     38       1.1   thorpej  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39       1.1   thorpej  * the permission of UNIX System Laboratories, Inc.
     40       1.1   thorpej  *
     41       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     42       1.1   thorpej  * modification, are permitted provided that the following conditions
     43       1.1   thorpej  * are met:
     44       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     45       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     46       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     47       1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     48       1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     49       1.1   thorpej  * 3. Neither the name of the University nor the names of its contributors
     50       1.1   thorpej  *    may be used to endorse or promote products derived from this software
     51       1.1   thorpej  *    without specific prior written permission.
     52       1.1   thorpej  *
     53       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54       1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55       1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56       1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57       1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58       1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59       1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60       1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61       1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62       1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63       1.1   thorpej  * SUCH DAMAGE.
     64       1.1   thorpej  */
     65       1.1   thorpej 
     66       1.1   thorpej /*
     67       1.1   thorpej  * VFS extended attribute support.
     68       1.1   thorpej  */
     69       1.1   thorpej 
     70       1.1   thorpej #include <sys/cdefs.h>
     71  1.28.2.2      yamt __KERNEL_RCSID(0, "$NetBSD: vfs_xattr.c,v 1.28.2.2 2012/04/17 00:08:33 yamt Exp $");
     72       1.1   thorpej 
     73       1.1   thorpej #include <sys/param.h>
     74       1.1   thorpej #include <sys/systm.h>
     75       1.1   thorpej #include <sys/namei.h>
     76       1.1   thorpej #include <sys/filedesc.h>
     77       1.1   thorpej #include <sys/kernel.h>
     78       1.1   thorpej #include <sys/file.h>
     79       1.1   thorpej #include <sys/vnode.h>
     80       1.1   thorpej #include <sys/mount.h>
     81       1.1   thorpej #include <sys/proc.h>
     82       1.1   thorpej #include <sys/uio.h>
     83       1.1   thorpej #include <sys/extattr.h>
     84       1.2   thorpej #include <sys/xattr.h>
     85       1.1   thorpej #include <sys/sysctl.h>
     86       1.1   thorpej #include <sys/syscallargs.h>
     87       1.6      elad #include <sys/kauth.h>
     88      1.28      manu #include <sys/ktrace.h>
     89       1.1   thorpej 
     90  1.28.2.2      yamt #include <miscfs/genfs/genfs.h>
     91  1.28.2.2      yamt 
     92       1.1   thorpej /*
     93       1.3   thorpej  * Credential check based on process requesting service, and per-attribute
     94       1.3   thorpej  * permissions.
     95       1.3   thorpej  *
     96       1.3   thorpej  * NOTE: Vnode must be locked.
     97       1.3   thorpej  */
     98       1.3   thorpej int
     99  1.28.2.2      yamt extattr_check_cred(struct vnode *vp, const char *attr, kauth_cred_t cred,
    100  1.28.2.2      yamt     int access)
    101       1.3   thorpej {
    102       1.3   thorpej 
    103       1.3   thorpej 	if (cred == NOCRED)
    104       1.3   thorpej 		return (0);
    105       1.3   thorpej 
    106  1.28.2.2      yamt 	return kauth_authorize_vnode(cred, kauth_extattr_action(access), vp,
    107  1.28.2.2      yamt 	    NULL, genfs_can_extattr(cred, access, vp, attr));
    108       1.3   thorpej }
    109       1.3   thorpej 
    110       1.3   thorpej /*
    111       1.3   thorpej  * Default vfs_extattrctl routine for file systems that do not support
    112       1.3   thorpej  * it.
    113       1.3   thorpej  */
    114       1.3   thorpej /*ARGSUSED*/
    115       1.3   thorpej int
    116       1.9      yamt vfs_stdextattrctl(struct mount *mp, int cmt, struct vnode *vp,
    117      1.13     pooka     int attrnamespace, const char *attrname)
    118       1.3   thorpej {
    119       1.3   thorpej 
    120       1.3   thorpej 	if (vp != NULL)
    121      1.22   hannken 		VOP_UNLOCK(vp);
    122       1.3   thorpej 	return (EOPNOTSUPP);
    123       1.3   thorpej }
    124       1.3   thorpej 
    125       1.3   thorpej /*
    126       1.2   thorpej  * Push extended attribute configuration information into the file
    127       1.2   thorpej  * system.
    128       1.1   thorpej  *
    129       1.1   thorpej  * NOTE: Not all file systems that support extended attributes will
    130       1.1   thorpej  * require the use of this system call.
    131       1.1   thorpej  */
    132       1.1   thorpej int
    133      1.15       dsl sys_extattrctl(struct lwp *l, const struct sys_extattrctl_args *uap, register_t *retval)
    134       1.1   thorpej {
    135      1.15       dsl 	/* {
    136       1.1   thorpej 		syscallarg(const char *) path;
    137       1.1   thorpej 		syscallarg(int) cmd;
    138       1.1   thorpej 		syscallarg(const char *) filename;
    139       1.1   thorpej 		syscallarg(int) attrnamespace;
    140       1.1   thorpej 		syscallarg(const char *) attrname;
    141      1.15       dsl 	} */
    142      1.20  dholland 	struct vnode *path_vp, *file_vp;
    143      1.23  dholland 	struct pathbuf *file_pb;
    144      1.20  dholland 	struct nameidata file_nd;
    145       1.1   thorpej 	char attrname[EXTATTR_MAXNAMELEN];
    146       1.1   thorpej 	int error;
    147       1.1   thorpej 
    148       1.1   thorpej 	if (SCARG(uap, attrname) != NULL) {
    149       1.1   thorpej 		error = copyinstr(SCARG(uap, attrname), attrname,
    150       1.1   thorpej 		    sizeof(attrname), NULL);
    151       1.1   thorpej 		if (error)
    152       1.1   thorpej 			return (error);
    153       1.1   thorpej 	}
    154       1.1   thorpej 
    155      1.21     pooka 	error = namei_simple_user(SCARG(uap, path),
    156      1.21     pooka 				NSM_FOLLOW_NOEMULROOT, &path_vp);
    157      1.21     pooka 	if (error) {
    158      1.21     pooka 		return (error);
    159      1.21     pooka 	}
    160      1.21     pooka 
    161      1.20  dholland 	file_vp = NULL;
    162       1.1   thorpej 	if (SCARG(uap, filename) != NULL) {
    163      1.23  dholland 		error = pathbuf_copyin(SCARG(uap, filename), &file_pb);
    164      1.23  dholland 		if (error) {
    165      1.23  dholland 			vrele(path_vp);
    166      1.23  dholland 			return (error);
    167      1.23  dholland 		}
    168      1.23  dholland 		NDINIT(&file_nd, LOOKUP, FOLLOW | LOCKLEAF, file_pb);
    169      1.20  dholland 		error = namei(&file_nd);
    170      1.21     pooka 		if (error) {
    171      1.23  dholland 			pathbuf_destroy(file_pb);
    172      1.21     pooka 			vrele(path_vp);
    173       1.1   thorpej 			return (error);
    174      1.21     pooka 		}
    175      1.20  dholland 		file_vp = file_nd.ni_vp;
    176      1.23  dholland 		pathbuf_destroy(file_pb);
    177       1.1   thorpej 	}
    178       1.1   thorpej 
    179      1.20  dholland 	error = VFS_EXTATTRCTL(path_vp->v_mount, SCARG(uap, cmd), file_vp,
    180       1.1   thorpej 	    SCARG(uap, attrnamespace),
    181      1.13     pooka 	    SCARG(uap, attrname) != NULL ? attrname : NULL);
    182       1.1   thorpej 
    183      1.20  dholland 	if (file_vp != NULL)
    184      1.20  dholland 		vrele(file_vp);
    185      1.20  dholland 	vrele(path_vp);
    186       1.1   thorpej 
    187       1.1   thorpej 	return (error);
    188       1.1   thorpej }
    189       1.1   thorpej 
    190       1.1   thorpej /*****************************************************************************
    191       1.1   thorpej  * Internal routines to manipulate file system extended attributes:
    192       1.1   thorpej  *	- set
    193       1.1   thorpej  *	- get
    194       1.1   thorpej  *	- delete
    195       1.1   thorpej  *	- list
    196       1.1   thorpej  *****************************************************************************/
    197       1.1   thorpej 
    198       1.1   thorpej /*
    199       1.1   thorpej  * extattr_set_vp:
    200       1.1   thorpej  *
    201       1.1   thorpej  *	Set a named extended attribute on a file or directory.
    202       1.1   thorpej  */
    203       1.1   thorpej static int
    204       1.1   thorpej extattr_set_vp(struct vnode *vp, int attrnamespace, const char *attrname,
    205      1.24      manu     const void *data, size_t nbytes, struct lwp *l, register_t *retval,
    206      1.24      manu     int flag)
    207       1.1   thorpej {
    208       1.1   thorpej 	struct uio auio;
    209       1.1   thorpej 	struct iovec aiov;
    210       1.1   thorpej 	ssize_t cnt;
    211       1.1   thorpej 	int error;
    212       1.1   thorpej 
    213       1.1   thorpej 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    214       1.1   thorpej 
    215      1.24      manu 	if (flag) {
    216      1.24      manu 		size_t attrlen;
    217      1.24      manu 
    218      1.24      manu 		error = VOP_GETEXTATTR(vp, attrnamespace, attrname, NULL,
    219      1.24      manu 				       &attrlen, l->l_cred);
    220      1.24      manu 
    221      1.24      manu 		switch (error) {
    222      1.24      manu 		case ENOATTR:
    223      1.24      manu 			if (flag & XATTR_REPLACE)
    224      1.24      manu 				goto done;
    225      1.24      manu 			break;
    226      1.24      manu 		case 0:
    227      1.24      manu 			if (flag & XATTR_CREATE) {
    228      1.24      manu 				error = EEXIST;
    229      1.24      manu 				goto done;
    230      1.24      manu 			}
    231      1.24      manu 			break;
    232      1.24      manu 		default:
    233      1.24      manu 			goto done;
    234      1.24      manu 			break;
    235      1.24      manu 		}
    236      1.24      manu 	}
    237      1.24      manu 
    238       1.1   thorpej 	aiov.iov_base = __UNCONST(data);	/* XXXUNCONST kills const */
    239       1.1   thorpej 	aiov.iov_len = nbytes;
    240       1.1   thorpej 	auio.uio_iov = &aiov;
    241       1.1   thorpej 	auio.uio_iovcnt = 1;
    242       1.1   thorpej 	auio.uio_offset = 0;
    243       1.1   thorpej 	if (nbytes > INT_MAX) {
    244       1.1   thorpej 		error = EINVAL;
    245       1.1   thorpej 		goto done;
    246       1.1   thorpej 	}
    247       1.1   thorpej 	auio.uio_resid = nbytes;
    248       1.1   thorpej 	auio.uio_rw = UIO_WRITE;
    249       1.5      yamt 	KASSERT(l == curlwp);
    250       1.5      yamt 	auio.uio_vmspace = l->l_proc->p_vmspace;
    251       1.1   thorpej 	cnt = nbytes;
    252       1.1   thorpej 
    253      1.28      manu 	ktrkuser("xattr-name", (void *)__UNCONST(attrname), strlen(attrname));
    254      1.28      manu 	ktrkuser("xattr-val", __UNCONST(data), nbytes);
    255      1.28      manu 
    256      1.13     pooka 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, l->l_cred);
    257       1.1   thorpej 	cnt -= auio.uio_resid;
    258       1.1   thorpej 	retval[0] = cnt;
    259       1.1   thorpej 
    260       1.1   thorpej  done:
    261      1.22   hannken 	VOP_UNLOCK(vp);
    262       1.1   thorpej 	return (error);
    263       1.1   thorpej }
    264       1.1   thorpej 
    265       1.1   thorpej /*
    266       1.1   thorpej  * extattr_get_vp:
    267       1.1   thorpej  *
    268       1.1   thorpej  *	Get a named extended attribute on a file or directory.
    269       1.1   thorpej  */
    270       1.1   thorpej static int
    271       1.1   thorpej extattr_get_vp(struct vnode *vp, int attrnamespace, const char *attrname,
    272       1.4  christos     void *data, size_t nbytes, struct lwp *l, register_t *retval)
    273       1.1   thorpej {
    274       1.1   thorpej 	struct uio auio, *auiop;
    275       1.1   thorpej 	struct iovec aiov;
    276       1.1   thorpej 	ssize_t cnt;
    277       1.1   thorpej 	size_t size, *sizep;
    278       1.1   thorpej 	int error;
    279       1.1   thorpej 
    280       1.1   thorpej 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    281       1.1   thorpej 
    282       1.1   thorpej 	/*
    283       1.1   thorpej 	 * Slightly unusual semantics: if the user provides a NULL data
    284       1.1   thorpej 	 * pointer, they don't want to receive the data, just the maximum
    285       1.1   thorpej 	 * read length.
    286       1.1   thorpej 	 */
    287       1.1   thorpej 	auiop = NULL;
    288       1.1   thorpej 	sizep = NULL;
    289       1.1   thorpej 	cnt = 0;
    290       1.1   thorpej 	if (data != NULL) {
    291       1.1   thorpej 		aiov.iov_base = data;
    292       1.1   thorpej 		aiov.iov_len = nbytes;
    293       1.1   thorpej 		auio.uio_iov = &aiov;
    294       1.1   thorpej 		auio.uio_offset = 0;
    295       1.1   thorpej 		if (nbytes > INT_MAX) {
    296       1.1   thorpej 			error = EINVAL;
    297       1.1   thorpej 			goto done;
    298       1.1   thorpej 		}
    299       1.1   thorpej 		auio.uio_resid = nbytes;
    300       1.1   thorpej 		auio.uio_rw = UIO_READ;
    301       1.5      yamt 		KASSERT(l == curlwp);
    302       1.5      yamt 		auio.uio_vmspace = l->l_proc->p_vmspace;
    303       1.1   thorpej 		auiop = &auio;
    304       1.1   thorpej 		cnt = nbytes;
    305       1.1   thorpej 	} else
    306       1.1   thorpej 		sizep = &size;
    307       1.1   thorpej 
    308      1.28      manu 	ktrkuser("xattr-name", (void *)__UNCONST(attrname), strlen(attrname));
    309      1.28      manu 
    310       1.1   thorpej 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, auiop, sizep,
    311      1.13     pooka 	    l->l_cred);
    312       1.1   thorpej 
    313       1.1   thorpej 	if (auiop != NULL) {
    314       1.1   thorpej 		cnt -= auio.uio_resid;
    315       1.1   thorpej 		retval[0] = cnt;
    316      1.28      manu 
    317      1.28      manu 		ktrkuser("xattr-val", data, cnt);
    318       1.1   thorpej 	} else
    319       1.1   thorpej 		retval[0] = size;
    320       1.1   thorpej 
    321       1.1   thorpej  done:
    322      1.22   hannken 	VOP_UNLOCK(vp);
    323       1.1   thorpej 	return (error);
    324       1.1   thorpej }
    325       1.1   thorpej 
    326       1.1   thorpej /*
    327       1.1   thorpej  * extattr_delete_vp:
    328       1.1   thorpej  *
    329       1.1   thorpej  *	Delete a named extended attribute on a file or directory.
    330       1.1   thorpej  */
    331       1.1   thorpej static int
    332       1.1   thorpej extattr_delete_vp(struct vnode *vp, int attrnamespace, const char *attrname,
    333       1.4  christos     struct lwp *l)
    334       1.1   thorpej {
    335       1.1   thorpej 	int error;
    336       1.1   thorpej 
    337       1.1   thorpej 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    338       1.1   thorpej 
    339      1.28      manu 	ktrkuser("xattr-name", (void *)__UNCONST(attrname), strlen(attrname));
    340      1.28      manu 
    341      1.13     pooka 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, l->l_cred);
    342       1.1   thorpej 	if (error == EOPNOTSUPP)
    343       1.1   thorpej 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
    344      1.13     pooka 		    l->l_cred);
    345       1.1   thorpej 
    346      1.22   hannken 	VOP_UNLOCK(vp);
    347       1.1   thorpej 	return (error);
    348       1.1   thorpej }
    349       1.1   thorpej 
    350       1.1   thorpej /*
    351       1.1   thorpej  * extattr_list_vp:
    352       1.1   thorpej  *
    353       1.1   thorpej  *	Retrieve a list of extended attributes on a file or directory.
    354       1.1   thorpej  */
    355       1.1   thorpej static int
    356       1.1   thorpej extattr_list_vp(struct vnode *vp, int attrnamespace, void *data, size_t nbytes,
    357      1.27      manu     int flag, struct lwp *l, register_t *retval)
    358       1.1   thorpej {
    359       1.1   thorpej 	struct uio auio, *auiop;
    360       1.1   thorpej 	size_t size, *sizep;
    361       1.1   thorpej 	struct iovec aiov;
    362       1.1   thorpej 	ssize_t cnt;
    363       1.1   thorpej 	int error;
    364       1.1   thorpej 
    365       1.1   thorpej 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    366       1.1   thorpej 
    367       1.1   thorpej 	auiop = NULL;
    368       1.1   thorpej 	sizep = NULL;
    369       1.1   thorpej 	cnt = 0;
    370       1.1   thorpej 	if (data != NULL) {
    371       1.1   thorpej 		aiov.iov_base = data;
    372       1.1   thorpej 		aiov.iov_len = nbytes;
    373       1.1   thorpej 		auio.uio_iov = &aiov;
    374       1.1   thorpej 		auio.uio_offset = 0;
    375       1.1   thorpej 		if (nbytes > INT_MAX) {
    376       1.1   thorpej 			error = EINVAL;
    377       1.1   thorpej 			goto done;
    378       1.1   thorpej 		}
    379       1.1   thorpej 		auio.uio_resid = nbytes;
    380       1.1   thorpej 		auio.uio_rw = UIO_READ;
    381       1.5      yamt 		KASSERT(l == curlwp);
    382       1.5      yamt 		auio.uio_vmspace = l->l_proc->p_vmspace;
    383       1.1   thorpej 		auiop = &auio;
    384       1.1   thorpej 		cnt = nbytes;
    385       1.1   thorpej 	} else
    386       1.1   thorpej 		sizep = &size;
    387       1.1   thorpej 
    388      1.27      manu 	error = VOP_LISTEXTATTR(vp, attrnamespace, auiop, sizep,
    389      1.27      manu 				flag, l->l_cred);
    390       1.1   thorpej 
    391       1.1   thorpej 	if (auiop != NULL) {
    392       1.1   thorpej 		cnt -= auio.uio_resid;
    393       1.1   thorpej 		retval[0] = cnt;
    394      1.28      manu 
    395      1.28      manu 		ktrkuser("xattr-list", data, cnt);
    396       1.1   thorpej 	} else
    397       1.1   thorpej 		retval[0] = size;
    398       1.1   thorpej 
    399       1.1   thorpej  done:
    400      1.22   hannken 	VOP_UNLOCK(vp);
    401       1.1   thorpej 	return (error);
    402       1.1   thorpej }
    403       1.1   thorpej 
    404       1.1   thorpej /*****************************************************************************
    405       1.1   thorpej  * BSD <sys/extattr.h> API for file system extended attributes
    406       1.1   thorpej  *****************************************************************************/
    407       1.1   thorpej 
    408       1.1   thorpej int
    409      1.15       dsl sys_extattr_set_fd(struct lwp *l, const struct sys_extattr_set_fd_args *uap, register_t *retval)
    410       1.1   thorpej {
    411      1.15       dsl 	/* {
    412       1.1   thorpej 		syscallarg(int) fd;
    413       1.1   thorpej 		syscallarg(int) attrnamespace;
    414       1.1   thorpej 		syscallarg(const char *) attrname;
    415       1.1   thorpej 		syscallarg(const void *) data;
    416       1.1   thorpej 		syscallarg(size_t) nbytes;
    417      1.15       dsl 	} */
    418       1.1   thorpej 	struct file *fp;
    419       1.1   thorpej 	struct vnode *vp;
    420       1.1   thorpej 	char attrname[EXTATTR_MAXNAMELEN];
    421       1.1   thorpej 	int error;
    422       1.1   thorpej 
    423       1.1   thorpej 	error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
    424       1.1   thorpej 	    NULL);
    425       1.1   thorpej 	if (error)
    426       1.1   thorpej 		return (error);
    427       1.1   thorpej 
    428      1.19        ad 	error = fd_getvnode(SCARG(uap, fd), &fp);
    429       1.1   thorpej 	if (error)
    430       1.1   thorpej 		return (error);
    431       1.1   thorpej 	vp = (struct vnode *) fp->f_data;
    432       1.1   thorpej 
    433       1.1   thorpej 	error = extattr_set_vp(vp, SCARG(uap, attrnamespace), attrname,
    434      1.24      manu 	    SCARG(uap, data), SCARG(uap, nbytes), l, retval, 0);
    435       1.1   thorpej 
    436      1.17        ad 	fd_putfile(SCARG(uap, fd));
    437       1.1   thorpej 	return (error);
    438       1.1   thorpej }
    439       1.1   thorpej 
    440       1.1   thorpej int
    441      1.15       dsl sys_extattr_set_file(struct lwp *l, const struct sys_extattr_set_file_args *uap, register_t *retval)
    442       1.1   thorpej {
    443      1.15       dsl 	/* {
    444       1.1   thorpej 		syscallarg(const char *) path;
    445       1.1   thorpej 		syscallarg(int) attrnamespace;
    446       1.1   thorpej 		syscallarg(const char *) attrname;
    447       1.1   thorpej 		syscallarg(const void *) data;
    448       1.1   thorpej 		syscallarg(size_t) nbytes;
    449      1.15       dsl 	} */
    450      1.20  dholland 	struct vnode *vp;
    451       1.1   thorpej 	char attrname[EXTATTR_MAXNAMELEN];
    452       1.1   thorpej 	int error;
    453       1.1   thorpej 
    454       1.1   thorpej 	error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
    455       1.1   thorpej 	    NULL);
    456       1.1   thorpej 	if (error)
    457       1.1   thorpej 		return (error);
    458       1.1   thorpej 
    459      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    460      1.20  dholland 				NSM_FOLLOW_NOEMULROOT, &vp);
    461       1.1   thorpej 	if (error)
    462       1.1   thorpej 		return (error);
    463       1.1   thorpej 
    464      1.20  dholland 	error = extattr_set_vp(vp, SCARG(uap, attrnamespace), attrname,
    465      1.24      manu 	    SCARG(uap, data), SCARG(uap, nbytes), l, retval, 0);
    466       1.1   thorpej 
    467      1.20  dholland 	vrele(vp);
    468       1.1   thorpej 	return (error);
    469       1.1   thorpej }
    470       1.1   thorpej 
    471       1.1   thorpej int
    472      1.15       dsl sys_extattr_set_link(struct lwp *l, const struct sys_extattr_set_link_args *uap, register_t *retval)
    473       1.1   thorpej {
    474      1.15       dsl 	/* {
    475       1.1   thorpej 		syscallarg(const char *) path;
    476       1.1   thorpej 		syscallarg(int) attrnamespace;
    477       1.1   thorpej 		syscallarg(const char *) attrname;
    478       1.1   thorpej 		syscallarg(const void *) data;
    479       1.1   thorpej 		syscallarg(size_t) nbytes;
    480      1.15       dsl 	} */
    481      1.20  dholland 	struct vnode *vp;
    482       1.1   thorpej 	char attrname[EXTATTR_MAXNAMELEN];
    483       1.1   thorpej 	int error;
    484       1.1   thorpej 
    485       1.1   thorpej 	error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
    486       1.1   thorpej 	    NULL);
    487       1.1   thorpej 	if (error)
    488       1.1   thorpej 		return (error);
    489       1.1   thorpej 
    490      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    491      1.20  dholland 				NSM_NOFOLLOW_NOEMULROOT, &vp);
    492       1.1   thorpej 	if (error)
    493       1.1   thorpej 		return (error);
    494       1.1   thorpej 
    495      1.20  dholland 	error = extattr_set_vp(vp, SCARG(uap, attrnamespace), attrname,
    496      1.24      manu 	    SCARG(uap, data), SCARG(uap, nbytes), l, retval, 0);
    497       1.1   thorpej 
    498      1.20  dholland 	vrele(vp);
    499       1.1   thorpej 	return (error);
    500       1.1   thorpej }
    501       1.1   thorpej 
    502       1.1   thorpej int
    503      1.15       dsl sys_extattr_get_fd(struct lwp *l, const struct sys_extattr_get_fd_args *uap, register_t *retval)
    504       1.1   thorpej {
    505      1.15       dsl 	/* {
    506       1.1   thorpej 		syscallarg(int) fd;
    507       1.1   thorpej 		syscallarg(int) attrnamespace;
    508       1.1   thorpej 		syscallarg(const char *) attrname;
    509       1.1   thorpej 		syscallarg(void *) data;
    510       1.1   thorpej 		syscallarg(size_t) nbytes;
    511      1.15       dsl 	} */
    512       1.1   thorpej 	struct file *fp;
    513       1.1   thorpej 	struct vnode *vp;
    514       1.1   thorpej 	char attrname[EXTATTR_MAXNAMELEN];
    515       1.1   thorpej 	int error;
    516       1.1   thorpej 
    517       1.1   thorpej 	error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
    518       1.1   thorpej 	    NULL);
    519       1.1   thorpej 	if (error)
    520       1.1   thorpej 		return (error);
    521       1.1   thorpej 
    522      1.19        ad 	error = fd_getvnode(SCARG(uap, fd), &fp);
    523       1.1   thorpej 	if (error)
    524       1.1   thorpej 		return (error);
    525       1.1   thorpej 	vp = (struct vnode *) fp->f_data;
    526       1.1   thorpej 
    527       1.1   thorpej 	error = extattr_get_vp(vp, SCARG(uap, attrnamespace), attrname,
    528       1.4  christos 	    SCARG(uap, data), SCARG(uap, nbytes), l, retval);
    529       1.1   thorpej 
    530      1.17        ad 	fd_putfile(SCARG(uap, fd));
    531       1.1   thorpej 	return (error);
    532       1.1   thorpej }
    533       1.1   thorpej 
    534       1.1   thorpej int
    535      1.15       dsl sys_extattr_get_file(struct lwp *l, const struct sys_extattr_get_file_args *uap, register_t *retval)
    536       1.1   thorpej {
    537      1.15       dsl 	/* {
    538       1.1   thorpej 		syscallarg(const char *) path;
    539       1.1   thorpej 		syscallarg(int) attrnamespace;
    540       1.1   thorpej 		syscallarg(const char *) attrname;
    541       1.1   thorpej 		syscallarg(void *) data;
    542       1.1   thorpej 		syscallarg(size_t) nbytes;
    543      1.15       dsl 	} */
    544      1.20  dholland 	struct vnode *vp;
    545       1.1   thorpej 	char attrname[EXTATTR_MAXNAMELEN];
    546       1.1   thorpej 	int error;
    547       1.1   thorpej 
    548       1.1   thorpej 	error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
    549       1.1   thorpej 	    NULL);
    550       1.1   thorpej 	if (error)
    551       1.1   thorpej 		return (error);
    552       1.1   thorpej 
    553      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    554      1.20  dholland 				NSM_FOLLOW_NOEMULROOT, &vp);
    555       1.1   thorpej 	if (error)
    556       1.1   thorpej 		return (error);
    557       1.1   thorpej 
    558      1.20  dholland 	error = extattr_get_vp(vp, SCARG(uap, attrnamespace), attrname,
    559       1.4  christos 	    SCARG(uap, data), SCARG(uap, nbytes), l, retval);
    560       1.1   thorpej 
    561      1.20  dholland 	vrele(vp);
    562       1.1   thorpej 	return (error);
    563       1.1   thorpej }
    564       1.1   thorpej 
    565       1.1   thorpej int
    566      1.15       dsl sys_extattr_get_link(struct lwp *l, const struct sys_extattr_get_link_args *uap, register_t *retval)
    567       1.1   thorpej {
    568      1.15       dsl 	/* {
    569       1.1   thorpej 		syscallarg(const char *) path;
    570       1.1   thorpej 		syscallarg(int) attrnamespace;
    571       1.1   thorpej 		syscallarg(const char *) attrname;
    572       1.1   thorpej 		syscallarg(void *) data;
    573       1.1   thorpej 		syscallarg(size_t) nbytes;
    574      1.15       dsl 	} */
    575      1.20  dholland 	struct vnode *vp;
    576       1.1   thorpej 	char attrname[EXTATTR_MAXNAMELEN];
    577       1.1   thorpej 	int error;
    578       1.1   thorpej 
    579       1.1   thorpej 	error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
    580       1.1   thorpej 	    NULL);
    581       1.1   thorpej 	if (error)
    582       1.1   thorpej 		return (error);
    583       1.1   thorpej 
    584      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    585      1.20  dholland 				NSM_NOFOLLOW_NOEMULROOT, &vp);
    586       1.1   thorpej 	if (error)
    587       1.1   thorpej 		return (error);
    588       1.1   thorpej 
    589      1.20  dholland 	error = extattr_get_vp(vp, SCARG(uap, attrnamespace), attrname,
    590       1.4  christos 	    SCARG(uap, data), SCARG(uap, nbytes), l, retval);
    591       1.1   thorpej 
    592      1.20  dholland 	vrele(vp);
    593       1.1   thorpej 	return (error);
    594       1.1   thorpej }
    595       1.1   thorpej 
    596       1.1   thorpej int
    597      1.15       dsl sys_extattr_delete_fd(struct lwp *l, const struct sys_extattr_delete_fd_args *uap, register_t *retval)
    598       1.1   thorpej {
    599      1.15       dsl 	/* {
    600       1.1   thorpej 		syscallarg(int) fd;
    601       1.1   thorpej 		syscallarg(int) attrnamespace;
    602       1.1   thorpej 		syscallarg(const char *) attrname;
    603      1.15       dsl 	} */
    604       1.1   thorpej 	struct file *fp;
    605       1.1   thorpej 	struct vnode *vp;
    606       1.1   thorpej 	char attrname[EXTATTR_MAXNAMELEN];
    607       1.1   thorpej 	int error;
    608       1.1   thorpej 
    609       1.1   thorpej 	error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
    610       1.1   thorpej 	    NULL);
    611       1.1   thorpej 	if (error)
    612       1.1   thorpej 		return (error);
    613       1.1   thorpej 
    614      1.19        ad 	error = fd_getvnode(SCARG(uap, fd), &fp);
    615       1.1   thorpej 	if (error)
    616       1.1   thorpej 		return (error);
    617       1.1   thorpej 	vp = (struct vnode *) fp->f_data;
    618       1.1   thorpej 
    619       1.4  christos 	error = extattr_delete_vp(vp, SCARG(uap, attrnamespace), attrname, l);
    620       1.1   thorpej 
    621      1.17        ad 	fd_putfile(SCARG(uap, fd));
    622       1.1   thorpej 	return (error);
    623       1.1   thorpej }
    624       1.1   thorpej 
    625       1.1   thorpej int
    626      1.15       dsl sys_extattr_delete_file(struct lwp *l, const struct sys_extattr_delete_file_args *uap, register_t *retval)
    627       1.1   thorpej {
    628      1.15       dsl 	/* {
    629       1.1   thorpej 		syscallarg(const char *) path;
    630       1.1   thorpej 		syscallarg(int) attrnamespace;
    631       1.1   thorpej 		syscallarg(const char *) attrname;
    632      1.15       dsl 	} */
    633      1.20  dholland 	struct vnode *vp;
    634       1.1   thorpej 	char attrname[EXTATTR_MAXNAMELEN];
    635       1.1   thorpej 	int error;
    636       1.1   thorpej 
    637       1.1   thorpej 	error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
    638       1.1   thorpej 	    NULL);
    639       1.1   thorpej 	if (error)
    640       1.1   thorpej 		return (error);
    641       1.1   thorpej 
    642      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    643      1.20  dholland 				NSM_FOLLOW_NOEMULROOT, &vp);
    644       1.1   thorpej 	if (error)
    645       1.1   thorpej 		return (error);
    646       1.1   thorpej 
    647      1.20  dholland 	error = extattr_delete_vp(vp, SCARG(uap, attrnamespace), attrname, l);
    648       1.1   thorpej 
    649      1.20  dholland 	vrele(vp);
    650       1.1   thorpej 	return (error);
    651       1.1   thorpej }
    652       1.1   thorpej 
    653       1.1   thorpej int
    654      1.15       dsl sys_extattr_delete_link(struct lwp *l, const struct sys_extattr_delete_link_args *uap, register_t *retval)
    655       1.1   thorpej {
    656      1.15       dsl 	/* {
    657       1.1   thorpej 		syscallarg(const char *) path;
    658       1.1   thorpej 		syscallarg(int) attrnamespace;
    659       1.1   thorpej 		syscallarg(const char *) attrname;
    660      1.15       dsl 	} */
    661      1.20  dholland 	struct vnode *vp;
    662       1.1   thorpej 	char attrname[EXTATTR_MAXNAMELEN];
    663       1.1   thorpej 	int error;
    664       1.1   thorpej 
    665       1.1   thorpej 	error = copyinstr(SCARG(uap, attrname), attrname, sizeof(attrname),
    666       1.1   thorpej 	    NULL);
    667       1.1   thorpej 	if (error)
    668       1.1   thorpej 		return (error);
    669       1.1   thorpej 
    670      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    671      1.20  dholland 				NSM_NOFOLLOW_NOEMULROOT, &vp);
    672       1.1   thorpej 	if (error)
    673       1.1   thorpej 		return (error);
    674       1.1   thorpej 
    675      1.20  dholland 	error = extattr_delete_vp(vp, SCARG(uap, attrnamespace), attrname, l);
    676       1.1   thorpej 
    677      1.20  dholland 	vrele(vp);
    678       1.1   thorpej 	return (error);
    679       1.1   thorpej }
    680       1.1   thorpej 
    681       1.1   thorpej int
    682      1.15       dsl sys_extattr_list_fd(struct lwp *l, const struct sys_extattr_list_fd_args *uap, register_t *retval)
    683       1.1   thorpej {
    684      1.15       dsl 	/* {
    685       1.1   thorpej 		syscallarg(int) fd;
    686       1.1   thorpej 		syscallarg(int) attrnamespace;
    687       1.1   thorpej 		syscallarg(void *) data;
    688       1.1   thorpej 		syscallarg(size_t) nbytes;
    689      1.15       dsl 	} */
    690       1.1   thorpej 	struct file *fp;
    691       1.1   thorpej 	struct vnode *vp;
    692       1.1   thorpej 	int error;
    693       1.1   thorpej 
    694      1.19        ad 	error = fd_getvnode(SCARG(uap, fd), &fp);
    695       1.1   thorpej 	if (error)
    696       1.1   thorpej 		return (error);
    697       1.1   thorpej 	vp = (struct vnode *) fp->f_data;
    698       1.1   thorpej 
    699       1.1   thorpej 	error = extattr_list_vp(vp, SCARG(uap, attrnamespace),
    700      1.27      manu 	    SCARG(uap, data), SCARG(uap, nbytes),
    701      1.27      manu 	    EXTATTR_LIST_LENPREFIX, l, retval);
    702       1.1   thorpej 
    703      1.17        ad 	fd_putfile(SCARG(uap, fd));
    704       1.1   thorpej 	return (error);
    705       1.1   thorpej }
    706       1.1   thorpej 
    707       1.1   thorpej int
    708      1.15       dsl sys_extattr_list_file(struct lwp *l, const struct sys_extattr_list_file_args *uap, register_t *retval)
    709       1.1   thorpej {
    710      1.15       dsl 	/* {
    711       1.1   thorpej 		syscallarg(const char *) path;
    712       1.1   thorpej 		syscallarg(int) attrnamespace;
    713       1.1   thorpej 		syscallarg(void *) data;
    714       1.1   thorpej 		syscallarg(size_t) nbytes;
    715      1.15       dsl 	} */
    716      1.20  dholland 	struct vnode *vp;
    717       1.1   thorpej 	int error;
    718       1.1   thorpej 
    719      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    720      1.20  dholland 				NSM_FOLLOW_NOEMULROOT, &vp);
    721       1.1   thorpej 	if (error)
    722       1.1   thorpej 		return (error);
    723       1.1   thorpej 
    724      1.20  dholland 	error = extattr_list_vp(vp, SCARG(uap, attrnamespace),
    725      1.27      manu 	    SCARG(uap, data), SCARG(uap, nbytes),
    726      1.27      manu 	    EXTATTR_LIST_LENPREFIX, l, retval);
    727       1.1   thorpej 
    728      1.20  dholland 	vrele(vp);
    729       1.1   thorpej 	return (error);
    730       1.1   thorpej }
    731       1.1   thorpej 
    732       1.1   thorpej int
    733      1.15       dsl sys_extattr_list_link(struct lwp *l, const struct sys_extattr_list_link_args *uap, register_t *retval)
    734       1.1   thorpej {
    735      1.15       dsl 	/* {
    736       1.1   thorpej 		syscallarg(const char *) path;
    737       1.1   thorpej 		syscallarg(int) attrnamespace;
    738       1.1   thorpej 		syscallarg(void *) data;
    739       1.1   thorpej 		syscallarg(size_t) nbytes;
    740      1.15       dsl 	} */
    741      1.20  dholland 	struct vnode *vp;
    742       1.1   thorpej 	int error;
    743       1.1   thorpej 
    744      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    745      1.20  dholland 				NSM_NOFOLLOW_NOEMULROOT, &vp);
    746       1.1   thorpej 	if (error)
    747       1.1   thorpej 		return (error);
    748       1.1   thorpej 
    749      1.20  dholland 	error = extattr_list_vp(vp, SCARG(uap, attrnamespace),
    750      1.27      manu 	    SCARG(uap, data), SCARG(uap, nbytes),
    751      1.27      manu 	    EXTATTR_LIST_LENPREFIX, l, retval);
    752       1.1   thorpej 
    753      1.20  dholland 	vrele(vp);
    754       1.1   thorpej 	return (error);
    755       1.1   thorpej }
    756       1.2   thorpej 
    757       1.2   thorpej /*****************************************************************************
    758       1.2   thorpej  * Linux-compatible <sys/xattr.h> API for file system extended attributes
    759       1.2   thorpej  *****************************************************************************/
    760       1.2   thorpej 
    761      1.24      manu #define MATCH_NS(ns, key) (strncmp(ns, key, sizeof(ns) - 1) == 0)
    762      1.24      manu static int
    763      1.24      manu xattr_native(const char *key) {
    764      1.24      manu 	if (MATCH_NS("system.", key))
    765      1.24      manu 		return EXTATTR_NAMESPACE_SYSTEM;
    766      1.24      manu 	else if (MATCH_NS("user.", key))
    767      1.24      manu 		return EXTATTR_NAMESPACE_USER;
    768      1.24      manu 	else if (MATCH_NS("security.", key))
    769      1.24      manu 		return EXTATTR_NAMESPACE_SYSTEM;
    770      1.24      manu 	else if (MATCH_NS("trusted.", key))
    771      1.24      manu 		return EXTATTR_NAMESPACE_SYSTEM;
    772      1.24      manu 	else
    773      1.24      manu 		return EXTATTR_NAMESPACE_USER;
    774      1.24      manu 
    775      1.24      manu }
    776      1.24      manu #undef MATCH_NS
    777      1.24      manu 
    778  1.28.2.1      yamt #define XATTR_ERRNO(e) ((e) == EOPNOTSUPP ? ENOTSUP : (e))
    779  1.28.2.1      yamt 
    780       1.2   thorpej int
    781      1.15       dsl sys_setxattr(struct lwp *l, const struct sys_setxattr_args *uap, register_t *retval)
    782       1.2   thorpej {
    783      1.15       dsl 	/* {
    784       1.2   thorpej 		syscallarg(const char *) path;
    785       1.2   thorpej 		syscallarg(const char *) name;
    786       1.2   thorpej 		syscallarg(void *) value;
    787       1.2   thorpej 		syscallarg(size_t) size;
    788       1.2   thorpej 		syscallarg(int) flags;
    789      1.15       dsl 	} */
    790      1.20  dholland 	struct vnode *vp;
    791       1.2   thorpej 	char attrname[XATTR_NAME_MAX];
    792      1.24      manu 	int namespace;
    793      1.24      manu 	register_t attrlen;
    794       1.2   thorpej 	int error;
    795       1.2   thorpej 
    796       1.2   thorpej 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
    797       1.2   thorpej 	    NULL);
    798       1.2   thorpej 	if (error)
    799      1.24      manu 		goto out;
    800       1.2   thorpej 
    801      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    802      1.20  dholland 				NSM_FOLLOW_NOEMULROOT, &vp);
    803       1.2   thorpej 	if (error)
    804      1.26      manu 		goto out;
    805       1.2   thorpej 
    806      1.24      manu 	namespace = xattr_native(attrname);
    807       1.2   thorpej 
    808      1.24      manu 	error = extattr_set_vp(vp, namespace,
    809      1.24      manu 	    attrname, SCARG(uap, value), SCARG(uap, size), l,
    810      1.24      manu 	    &attrlen, SCARG(uap, flags));
    811       1.2   thorpej 
    812      1.20  dholland 	vrele(vp);
    813      1.24      manu out:
    814      1.24      manu 	*retval = (error == 0) ? 0 : -1;
    815  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
    816       1.2   thorpej }
    817       1.2   thorpej 
    818       1.2   thorpej int
    819      1.15       dsl sys_lsetxattr(struct lwp *l, const struct sys_lsetxattr_args *uap, register_t *retval)
    820       1.2   thorpej {
    821      1.15       dsl 	/* {
    822       1.2   thorpej 		syscallarg(const char *) path;
    823       1.2   thorpej 		syscallarg(const char *) name;
    824       1.2   thorpej 		syscallarg(void *) value;
    825       1.2   thorpej 		syscallarg(size_t) size;
    826       1.2   thorpej 		syscallarg(int) flags;
    827      1.15       dsl 	} */
    828      1.20  dholland 	struct vnode *vp;
    829       1.2   thorpej 	char attrname[XATTR_NAME_MAX];
    830      1.24      manu 	int namespace;
    831      1.24      manu 	register_t attrlen;
    832       1.2   thorpej 	int error;
    833       1.2   thorpej 
    834       1.2   thorpej 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
    835       1.2   thorpej 	    NULL);
    836       1.2   thorpej 	if (error)
    837      1.24      manu 		goto out;
    838       1.2   thorpej 
    839      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    840      1.20  dholland 				NSM_NOFOLLOW_NOEMULROOT, &vp);
    841       1.2   thorpej 	if (error)
    842      1.26      manu 		goto out;
    843       1.2   thorpej 
    844      1.24      manu 	namespace = xattr_native(attrname);
    845       1.2   thorpej 
    846      1.24      manu 	error = extattr_set_vp(vp, namespace,
    847      1.24      manu 	    attrname, SCARG(uap, value), SCARG(uap, size), l,
    848      1.24      manu 	    &attrlen, SCARG(uap, flags));
    849       1.2   thorpej 
    850      1.20  dholland 	vrele(vp);
    851      1.24      manu out:
    852      1.24      manu 	*retval = (error == 0) ? 0 : -1;
    853  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
    854       1.2   thorpej }
    855       1.2   thorpej 
    856       1.2   thorpej int
    857      1.15       dsl sys_fsetxattr(struct lwp *l, const struct sys_fsetxattr_args *uap, register_t *retval)
    858       1.2   thorpej {
    859      1.15       dsl 	/* {
    860       1.2   thorpej 		syscallarg(int) fd;
    861       1.2   thorpej 		syscallarg(const char *) name;
    862       1.2   thorpej 		syscallarg(void *) value;
    863       1.2   thorpej 		syscallarg(size_t) size;
    864       1.2   thorpej 		syscallarg(int) flags;
    865      1.15       dsl 	} */
    866       1.2   thorpej 	struct file *fp;
    867       1.2   thorpej 	struct vnode *vp;
    868       1.2   thorpej 	char attrname[XATTR_NAME_MAX];
    869      1.24      manu 	int namespace;
    870      1.24      manu 	register_t attrlen;
    871       1.2   thorpej 	int error;
    872       1.2   thorpej 
    873       1.2   thorpej 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
    874       1.2   thorpej 	    NULL);
    875       1.2   thorpej 	if (error)
    876      1.24      manu 		goto out;
    877       1.2   thorpej 
    878      1.19        ad 	error = fd_getvnode(SCARG(uap, fd), &fp);
    879       1.2   thorpej 	if (error)
    880      1.24      manu 		goto out;
    881       1.2   thorpej 	vp = (struct vnode *) fp->f_data;
    882       1.2   thorpej 
    883      1.24      manu 	namespace = xattr_native(attrname);
    884       1.2   thorpej 
    885      1.24      manu 	error = extattr_set_vp(vp, namespace,
    886      1.24      manu 	    attrname, SCARG(uap, value), SCARG(uap, size), l,
    887      1.24      manu 	    &attrlen, SCARG(uap, flags));
    888       1.2   thorpej 
    889      1.17        ad 	fd_putfile(SCARG(uap, fd));
    890      1.24      manu out:
    891      1.24      manu 	*retval = (error == 0) ? 0 : -1;
    892  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
    893       1.2   thorpej }
    894       1.2   thorpej 
    895       1.2   thorpej int
    896      1.15       dsl sys_getxattr(struct lwp *l, const struct sys_getxattr_args *uap, register_t *retval)
    897       1.2   thorpej {
    898      1.15       dsl 	/* {
    899       1.2   thorpej 		syscallarg(const char *) path;
    900       1.2   thorpej 		syscallarg(const char *) name;
    901       1.2   thorpej 		syscallarg(void *) value;
    902       1.2   thorpej 		syscallarg(size_t) size;
    903      1.15       dsl 	} */
    904      1.20  dholland 	struct vnode *vp;
    905       1.2   thorpej 	char attrname[XATTR_NAME_MAX];
    906      1.24      manu 	int namespace;
    907       1.2   thorpej 	int error;
    908       1.2   thorpej 
    909       1.2   thorpej 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
    910       1.2   thorpej 	    NULL);
    911       1.2   thorpej 	if (error)
    912       1.2   thorpej 		return (error);
    913       1.2   thorpej 
    914      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    915      1.20  dholland 				NSM_FOLLOW_NOEMULROOT, &vp);
    916       1.2   thorpej 	if (error)
    917       1.2   thorpej 		return (error);
    918       1.2   thorpej 
    919      1.24      manu 	namespace = xattr_native(attrname);
    920      1.24      manu 
    921      1.24      manu 	error = extattr_get_vp(vp, namespace,
    922       1.4  christos 	    attrname, SCARG(uap, value), SCARG(uap, size), l, retval);
    923       1.2   thorpej 
    924      1.20  dholland 	vrele(vp);
    925  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
    926       1.2   thorpej }
    927       1.2   thorpej 
    928       1.2   thorpej int
    929      1.15       dsl sys_lgetxattr(struct lwp *l, const struct sys_lgetxattr_args *uap, register_t *retval)
    930       1.2   thorpej {
    931      1.15       dsl 	/* {
    932       1.2   thorpej 		syscallarg(const char *) path;
    933       1.2   thorpej 		syscallarg(const char *) name;
    934       1.2   thorpej 		syscallarg(void *) value;
    935       1.2   thorpej 		syscallarg(size_t) size;
    936      1.15       dsl 	} */
    937      1.20  dholland 	struct vnode *vp;
    938       1.2   thorpej 	char attrname[XATTR_NAME_MAX];
    939      1.24      manu 	int namespace;
    940       1.2   thorpej 	int error;
    941       1.2   thorpej 
    942       1.2   thorpej 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
    943       1.2   thorpej 	    NULL);
    944       1.2   thorpej 	if (error)
    945       1.2   thorpej 		return (error);
    946       1.2   thorpej 
    947      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
    948      1.20  dholland 				NSM_NOFOLLOW_NOEMULROOT, &vp);
    949       1.2   thorpej 	if (error)
    950       1.2   thorpej 		return (error);
    951       1.2   thorpej 
    952      1.24      manu 	namespace = xattr_native(attrname);
    953      1.24      manu 
    954      1.24      manu 	error = extattr_get_vp(vp, namespace,
    955       1.4  christos 	    attrname, SCARG(uap, value), SCARG(uap, size), l, retval);
    956       1.2   thorpej 
    957      1.20  dholland 	vrele(vp);
    958  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
    959       1.2   thorpej }
    960       1.2   thorpej 
    961       1.2   thorpej int
    962      1.15       dsl sys_fgetxattr(struct lwp *l, const struct sys_fgetxattr_args *uap, register_t *retval)
    963       1.2   thorpej {
    964      1.15       dsl 	/* {
    965       1.2   thorpej 		syscallarg(int) fd;
    966       1.2   thorpej 		syscallarg(const char *) name;
    967       1.2   thorpej 		syscallarg(void *) value;
    968       1.2   thorpej 		syscallarg(size_t) size;
    969      1.15       dsl 	} */
    970       1.2   thorpej 	struct file *fp;
    971       1.2   thorpej 	struct vnode *vp;
    972       1.2   thorpej 	char attrname[XATTR_NAME_MAX];
    973      1.24      manu 	int namespace;
    974       1.2   thorpej 	int error;
    975       1.2   thorpej 
    976       1.2   thorpej 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
    977       1.2   thorpej 	    NULL);
    978       1.2   thorpej 	if (error)
    979       1.2   thorpej 		return (error);
    980       1.2   thorpej 
    981      1.19        ad 	error = fd_getvnode(SCARG(uap, fd), &fp);
    982       1.2   thorpej 	if (error)
    983       1.2   thorpej 		return (error);
    984       1.2   thorpej 	vp = (struct vnode *) fp->f_data;
    985       1.2   thorpej 
    986      1.24      manu 	namespace = xattr_native(attrname);
    987      1.24      manu 
    988      1.24      manu 	error = extattr_get_vp(vp, namespace,
    989       1.4  christos 	    attrname, SCARG(uap, value), SCARG(uap, size), l, retval);
    990       1.2   thorpej 
    991      1.17        ad 	fd_putfile(SCARG(uap, fd));
    992  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
    993       1.2   thorpej }
    994       1.2   thorpej 
    995       1.2   thorpej int
    996      1.15       dsl sys_listxattr(struct lwp *l, const struct sys_listxattr_args *uap, register_t *retval)
    997       1.2   thorpej {
    998      1.15       dsl 	/* {
    999       1.2   thorpej 		syscallarg(const char *) path;
   1000       1.2   thorpej 		syscallarg(char *) list;
   1001       1.2   thorpej 		syscallarg(size_t) size;
   1002      1.15       dsl 	} */
   1003      1.20  dholland 	struct vnode *vp;
   1004      1.24      manu 	char *list;
   1005      1.24      manu 	size_t size;
   1006      1.24      manu 	register_t listsize_usr, listsize_sys;
   1007       1.2   thorpej 	int error;
   1008       1.2   thorpej 
   1009      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
   1010      1.20  dholland 				NSM_FOLLOW_NOEMULROOT, &vp);
   1011       1.2   thorpej 	if (error)
   1012       1.2   thorpej 		return (error);
   1013       1.2   thorpej 
   1014      1.24      manu 	list = SCARG(uap, list);
   1015      1.24      manu 	size = SCARG(uap, size);
   1016      1.24      manu 
   1017      1.20  dholland 	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_USER,
   1018      1.27      manu 	    list, size, 0, l, &listsize_usr);
   1019      1.24      manu 	if (error)
   1020      1.24      manu 		goto out;
   1021      1.24      manu 
   1022      1.24      manu 	if (list)
   1023      1.24      manu 		list += listsize_usr;
   1024      1.24      manu 	if (size)
   1025      1.24      manu 		size -= listsize_usr;
   1026       1.2   thorpej 
   1027      1.24      manu 	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_SYSTEM,
   1028      1.27      manu 	    list, size, 0, l, &listsize_sys);
   1029      1.25      manu 	switch (error) {
   1030      1.25      manu 	case EPERM:
   1031      1.25      manu 		error = 0; /* Ignore and just skip system EA */
   1032      1.25      manu 		listsize_sys = 0;
   1033      1.25      manu 		break;
   1034      1.25      manu 	case 0:
   1035      1.25      manu 		break;
   1036      1.25      manu 	default:
   1037      1.24      manu 		goto out;
   1038      1.25      manu 		break;
   1039      1.25      manu 	}
   1040      1.24      manu 
   1041      1.24      manu 	*retval = listsize_usr + listsize_sys;
   1042      1.24      manu 
   1043      1.24      manu out:
   1044      1.20  dholland 	vrele(vp);
   1045  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
   1046       1.2   thorpej }
   1047       1.2   thorpej 
   1048       1.2   thorpej int
   1049      1.15       dsl sys_llistxattr(struct lwp *l, const struct sys_llistxattr_args *uap, register_t *retval)
   1050       1.2   thorpej {
   1051      1.15       dsl 	/* {
   1052       1.2   thorpej 		syscallarg(const char *) path;
   1053       1.2   thorpej 		syscallarg(char *) list;
   1054       1.2   thorpej 		syscallarg(size_t) size;
   1055      1.15       dsl 	} */
   1056      1.20  dholland 	struct vnode *vp;
   1057      1.24      manu 	char *list;
   1058      1.24      manu 	size_t size;
   1059      1.24      manu 	register_t listsize_usr, listsize_sys;
   1060       1.2   thorpej 	int error;
   1061       1.2   thorpej 
   1062      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
   1063      1.20  dholland 				NSM_NOFOLLOW_NOEMULROOT, &vp);
   1064       1.2   thorpej 	if (error)
   1065       1.2   thorpej 		return (error);
   1066       1.2   thorpej 
   1067      1.24      manu 	list = SCARG(uap, list);
   1068      1.24      manu 	size = SCARG(uap, size);
   1069      1.24      manu 
   1070      1.20  dholland 	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_USER,
   1071      1.27      manu 	    list, size, 0, l, &listsize_usr);
   1072      1.24      manu 	if (error)
   1073      1.24      manu 		goto out;
   1074      1.24      manu 	if (list)
   1075      1.24      manu 		list += listsize_usr;
   1076      1.24      manu 	if (size)
   1077      1.24      manu 		size -= listsize_usr;
   1078       1.2   thorpej 
   1079      1.24      manu 	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_SYSTEM,
   1080      1.27      manu 	    list, size, 0, l, &listsize_sys);
   1081      1.25      manu 	switch (error) {
   1082      1.25      manu 	case EPERM:
   1083      1.25      manu 		error = 0; /* Ignore and just skip system EA */
   1084      1.25      manu 		listsize_sys = 0;
   1085      1.25      manu 		break;
   1086      1.25      manu 	case 0:
   1087      1.25      manu 		break;
   1088      1.25      manu 	default:
   1089      1.24      manu 		goto out;
   1090      1.25      manu 		break;
   1091      1.25      manu 	}
   1092      1.24      manu 
   1093      1.24      manu 	*retval = listsize_usr + listsize_sys;
   1094      1.24      manu out:
   1095      1.20  dholland 	vrele(vp);
   1096  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
   1097       1.2   thorpej }
   1098       1.2   thorpej 
   1099       1.2   thorpej int
   1100      1.15       dsl sys_flistxattr(struct lwp *l, const struct sys_flistxattr_args *uap, register_t *retval)
   1101       1.2   thorpej {
   1102      1.15       dsl 	/* {
   1103       1.2   thorpej 		syscallarg(int) fd;
   1104       1.2   thorpej 		syscallarg(char *) list;
   1105       1.2   thorpej 		syscallarg(size_t) size;
   1106      1.15       dsl 	} */
   1107       1.2   thorpej 	struct file *fp;
   1108       1.2   thorpej 	struct vnode *vp;
   1109      1.24      manu 	char *list;
   1110      1.24      manu 	size_t size;
   1111      1.24      manu 	register_t listsize_usr, listsize_sys;
   1112       1.2   thorpej 	int error;
   1113       1.2   thorpej 
   1114      1.19        ad 	error = fd_getvnode(SCARG(uap, fd), &fp);
   1115       1.2   thorpej 	if (error)
   1116       1.2   thorpej 		return (error);
   1117       1.2   thorpej 	vp = (struct vnode *) fp->f_data;
   1118       1.2   thorpej 
   1119      1.24      manu 	list = SCARG(uap, list);
   1120      1.24      manu 	size = SCARG(uap, size);
   1121      1.24      manu 
   1122       1.2   thorpej 	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_USER,
   1123      1.27      manu 	    list, size, 0, l, &listsize_usr);
   1124      1.24      manu 	if (error)
   1125      1.24      manu 		goto out;
   1126      1.24      manu 
   1127      1.24      manu 	if (list)
   1128      1.24      manu 		list += listsize_usr;
   1129      1.24      manu 	if (size)
   1130      1.24      manu 		size -= listsize_usr;
   1131      1.24      manu 
   1132      1.24      manu 	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_SYSTEM,
   1133      1.27      manu 	    list, size, 0, l, &listsize_sys);
   1134      1.25      manu 	switch (error) {
   1135      1.25      manu 	case EPERM:
   1136      1.25      manu 		error = 0; /* Ignore and just skip system EA */
   1137      1.25      manu 		listsize_sys = 0;
   1138      1.25      manu 		break;
   1139      1.25      manu 	case 0:
   1140      1.25      manu 		break;
   1141      1.25      manu 	default:
   1142      1.24      manu 		goto out;
   1143      1.25      manu 		break;
   1144      1.25      manu 	}
   1145      1.24      manu 
   1146      1.24      manu 	*retval = listsize_usr + listsize_sys;
   1147      1.24      manu out:
   1148       1.2   thorpej 
   1149      1.17        ad 	fd_putfile(SCARG(uap, fd));
   1150  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
   1151       1.2   thorpej }
   1152       1.2   thorpej 
   1153       1.2   thorpej int
   1154      1.15       dsl sys_removexattr(struct lwp *l, const struct sys_removexattr_args *uap, register_t *retval)
   1155       1.2   thorpej {
   1156      1.15       dsl 	/* {
   1157       1.2   thorpej 		syscallarg(const char *) path;
   1158       1.2   thorpej 		syscallarg(const char *) name;
   1159      1.15       dsl 	} */
   1160      1.20  dholland 	struct vnode *vp;
   1161       1.2   thorpej 	char attrname[XATTR_NAME_MAX];
   1162      1.24      manu 	int namespace;
   1163       1.2   thorpej 	int error;
   1164       1.2   thorpej 
   1165       1.2   thorpej 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
   1166       1.2   thorpej 	    NULL);
   1167       1.2   thorpej 	if (error)
   1168       1.2   thorpej 		return (error);
   1169       1.2   thorpej 
   1170      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
   1171      1.20  dholland 				NSM_FOLLOW_NOEMULROOT, &vp);
   1172       1.2   thorpej 	if (error)
   1173       1.2   thorpej 		return (error);
   1174       1.2   thorpej 
   1175      1.24      manu 	namespace = xattr_native(attrname);
   1176      1.24      manu 
   1177      1.24      manu 	error = extattr_delete_vp(vp, namespace, attrname, l);
   1178       1.2   thorpej 
   1179      1.20  dholland 	vrele(vp);
   1180  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
   1181       1.2   thorpej }
   1182       1.2   thorpej 
   1183       1.2   thorpej int
   1184      1.15       dsl sys_lremovexattr(struct lwp *l, const struct sys_lremovexattr_args *uap, register_t *retval)
   1185       1.2   thorpej {
   1186      1.15       dsl 	/* {
   1187       1.2   thorpej 		syscallarg(const char *) path;
   1188       1.2   thorpej 		syscallarg(const char *) name;
   1189      1.15       dsl 	} */
   1190      1.20  dholland 	struct vnode *vp;
   1191       1.2   thorpej 	char attrname[XATTR_NAME_MAX];
   1192      1.24      manu 	int namespace;
   1193       1.2   thorpej 	int error;
   1194       1.2   thorpej 
   1195       1.2   thorpej 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
   1196       1.2   thorpej 	    NULL);
   1197       1.2   thorpej 	if (error)
   1198       1.2   thorpej 		return (error);
   1199       1.2   thorpej 
   1200      1.20  dholland 	error = namei_simple_user(SCARG(uap, path),
   1201      1.20  dholland 				NSM_NOFOLLOW_NOEMULROOT, &vp);
   1202       1.2   thorpej 	if (error)
   1203       1.2   thorpej 		return (error);
   1204       1.2   thorpej 
   1205      1.24      manu 	namespace = xattr_native(attrname);
   1206      1.24      manu 
   1207      1.24      manu 	error = extattr_delete_vp(vp, namespace, attrname, l);
   1208       1.2   thorpej 
   1209      1.20  dholland 	vrele(vp);
   1210  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
   1211       1.2   thorpej }
   1212       1.2   thorpej 
   1213       1.2   thorpej int
   1214      1.15       dsl sys_fremovexattr(struct lwp *l, const struct sys_fremovexattr_args *uap, register_t *retval)
   1215       1.2   thorpej {
   1216      1.15       dsl 	/* {
   1217       1.2   thorpej 		syscallarg(int) fd;
   1218       1.2   thorpej 		syscallarg(const char *) name;
   1219      1.15       dsl 	} */
   1220       1.2   thorpej 	struct file *fp;
   1221       1.2   thorpej 	struct vnode *vp;
   1222       1.2   thorpej 	char attrname[XATTR_NAME_MAX];
   1223      1.24      manu 	int namespace;
   1224       1.2   thorpej 	int error;
   1225       1.2   thorpej 
   1226       1.2   thorpej 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
   1227       1.2   thorpej 	    NULL);
   1228       1.2   thorpej 	if (error)
   1229       1.2   thorpej 		return (error);
   1230       1.2   thorpej 
   1231      1.19        ad 	error = fd_getvnode(SCARG(uap, fd), &fp);
   1232       1.2   thorpej 	if (error)
   1233       1.2   thorpej 		return (error);
   1234       1.2   thorpej 	vp = (struct vnode *) fp->f_data;
   1235       1.2   thorpej 
   1236      1.24      manu 	namespace = xattr_native(attrname);
   1237      1.24      manu 
   1238      1.24      manu 	error = extattr_delete_vp(vp, namespace, attrname, l);
   1239       1.2   thorpej 
   1240      1.17        ad 	fd_putfile(SCARG(uap, fd));
   1241  1.28.2.1      yamt 	return (XATTR_ERRNO(error));
   1242       1.2   thorpej }
   1243