Module Name:    src
Committed By:   manu
Date:           Mon Jun 27 16:39:44 UTC 2011

Modified Files:
        src/distrib/sets/lists/comp: mi
        src/lib/libc/sys: Makefile.inc
        src/sys/kern: vfs_xattr.c
        src/sys/sys: Makefile

Log Message:
Fix multiple non compliances in our Linux-like extattr API, and make it
public so that it can be used.


To generate a diff of this commit:
cvs rdiff -u -r1.1633 -r1.1634 src/distrib/sets/lists/comp/mi
cvs rdiff -u -r1.206 -r1.207 src/lib/libc/sys/Makefile.inc
cvs rdiff -u -r1.23 -r1.24 src/sys/kern/vfs_xattr.c
cvs rdiff -u -r1.132 -r1.133 src/sys/sys/Makefile

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/comp/mi
diff -u src/distrib/sets/lists/comp/mi:1.1633 src/distrib/sets/lists/comp/mi:1.1634
--- src/distrib/sets/lists/comp/mi:1.1633	Mon Jun 27 11:52:22 2011
+++ src/distrib/sets/lists/comp/mi	Mon Jun 27 16:39:43 2011
@@ -1,4 +1,4 @@
-#	$NetBSD: mi,v 1.1633 2011/06/27 11:52:22 uch Exp $
+#	$NetBSD: mi,v 1.1634 2011/06/27 16:39:43 manu Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -2228,6 +2228,7 @@
 ./usr/include/sys/wapbl.h			comp-c-include
 ./usr/include/sys/wapbl_replay.h		comp-c-include
 ./usr/include/sys/wdog.h			comp-c-include
+./usr/include/sys/xattr.h			comp-c-include
 ./usr/include/sysexits.h			comp-c-include
 ./usr/include/syslog.h				comp-c-include
 ./usr/include/tar.h				comp-c-include

Index: src/lib/libc/sys/Makefile.inc
diff -u src/lib/libc/sys/Makefile.inc:1.206 src/lib/libc/sys/Makefile.inc:1.207
--- src/lib/libc/sys/Makefile.inc:1.206	Sun Jun 26 16:42:41 2011
+++ src/lib/libc/sys/Makefile.inc	Mon Jun 27 16:39:44 2011
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.inc,v 1.206 2011/06/26 16:42:41 christos Exp $
+#	$NetBSD: Makefile.inc,v 1.207 2011/06/27 16:39:44 manu Exp $
 #	@(#)Makefile.inc	8.3 (Berkeley) 10/24/94
 
 # sys sources
@@ -84,6 +84,9 @@
 		extattr_get_link.S extattr_list_fd.S extattr_list_file.S \
 		extattr_list_link.S extattr_set_fd.S extattr_set_file.S \
 		extattr_set_link.S \
+	setxattr.S getxattr.S listxattr.S removexattr.S \
+		lsetxattr.S lgetxattr.S llistxattr.S lremovexattr.S \
+		fsetxattr.S fgetxattr.S flistxattr.S fremovexattr.S \
 	fchdir.S fchflags.S fchmod.S fchown.S fchroot.S \
 		__fhopen40.S __fhstat50.S __fhstatvfs140.S fktrace.S \
 		flock.S fpathconf.S __fstat50.S fstatvfs1.S __futimes50.S \

Index: src/sys/kern/vfs_xattr.c
diff -u src/sys/kern/vfs_xattr.c:1.23 src/sys/kern/vfs_xattr.c:1.24
--- src/sys/kern/vfs_xattr.c:1.23	Fri Nov 19 06:44:45 2010
+++ src/sys/kern/vfs_xattr.c	Mon Jun 27 16:39:43 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_xattr.c,v 1.23 2010/11/19 06:44:45 dholland Exp $	*/
+/*	$NetBSD: vfs_xattr.c,v 1.24 2011/06/27 16:39:43 manu Exp $	*/
 
 /*-
  * Copyright (c) 2005, 2008 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_xattr.c,v 1.23 2010/11/19 06:44:45 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_xattr.c,v 1.24 2011/06/27 16:39:43 manu Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -212,7 +212,8 @@
  */
 static int
 extattr_set_vp(struct vnode *vp, int attrnamespace, const char *attrname,
-    const void *data, size_t nbytes, struct lwp *l, register_t *retval)
+    const void *data, size_t nbytes, struct lwp *l, register_t *retval,
+    int flag)
 {
 	struct uio auio;
 	struct iovec aiov;
@@ -221,6 +222,29 @@
 
 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 
+	if (flag) {
+		size_t attrlen;
+
+		error = VOP_GETEXTATTR(vp, attrnamespace, attrname, NULL,
+				       &attrlen, l->l_cred);
+
+		switch (error) {
+		case ENOATTR:
+			if (flag & XATTR_REPLACE)
+				goto done;
+			break;
+		case 0:
+			if (flag & XATTR_CREATE) {
+				error = EEXIST;
+				goto done;
+			}
+			break;
+		default:
+			goto done;
+			break;
+		}
+	}
+
 	aiov.iov_base = __UNCONST(data);	/* XXXUNCONST kills const */
 	aiov.iov_len = nbytes;
 	auio.uio_iov = &aiov;
@@ -405,7 +429,7 @@
 	vp = (struct vnode *) fp->f_data;
 
 	error = extattr_set_vp(vp, SCARG(uap, attrnamespace), attrname,
-	    SCARG(uap, data), SCARG(uap, nbytes), l, retval);
+	    SCARG(uap, data), SCARG(uap, nbytes), l, retval, 0);
 
 	fd_putfile(SCARG(uap, fd));
 	return (error);
@@ -436,7 +460,7 @@
 		return (error);
 
 	error = extattr_set_vp(vp, SCARG(uap, attrnamespace), attrname,
-	    SCARG(uap, data), SCARG(uap, nbytes), l, retval);
+	    SCARG(uap, data), SCARG(uap, nbytes), l, retval, 0);
 
 	vrele(vp);
 	return (error);
@@ -467,7 +491,7 @@
 		return (error);
 
 	error = extattr_set_vp(vp, SCARG(uap, attrnamespace), attrname,
-	    SCARG(uap, data), SCARG(uap, nbytes), l, retval);
+	    SCARG(uap, data), SCARG(uap, nbytes), l, retval, 0);
 
 	vrele(vp);
 	return (error);
@@ -729,6 +753,23 @@
  * Linux-compatible <sys/xattr.h> API for file system extended attributes
  *****************************************************************************/
 
+#define MATCH_NS(ns, key) (strncmp(ns, key, sizeof(ns) - 1) == 0)
+static int
+xattr_native(const char *key) {
+	if (MATCH_NS("system.", key))
+		return EXTATTR_NAMESPACE_SYSTEM;
+	else if (MATCH_NS("user.", key))
+		return EXTATTR_NAMESPACE_USER;
+	else if (MATCH_NS("security.", key))
+		return EXTATTR_NAMESPACE_SYSTEM;
+	else if (MATCH_NS("trusted.", key))
+		return EXTATTR_NAMESPACE_SYSTEM;
+	else 
+		return EXTATTR_NAMESPACE_USER;
+	
+}
+#undef MATCH_NS
+
 int
 sys_setxattr(struct lwp *l, const struct sys_setxattr_args *uap, register_t *retval)
 {
@@ -741,24 +782,30 @@
 	} */
 	struct vnode *vp;
 	char attrname[XATTR_NAME_MAX];
+	int namespace;
+	register_t attrlen;
 	int error;
 
 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
 	    NULL);
 	if (error)
-		return (error);
+		goto out;
 
 	error = namei_simple_user(SCARG(uap, path),
 				NSM_FOLLOW_NOEMULROOT, &vp);
 	if (error)
-		return (error);
+		goto out_rele;
 
-	/* XXX flags */
+	namespace = xattr_native(attrname);
 
-	error = extattr_set_vp(vp, EXTATTR_NAMESPACE_USER,
-	    attrname, SCARG(uap, value), SCARG(uap, size), l, retval);
+	error = extattr_set_vp(vp, namespace,
+	    attrname, SCARG(uap, value), SCARG(uap, size), l, 
+	    &attrlen, SCARG(uap, flags));
 
+out_rele:
 	vrele(vp);
+out:
+	*retval = (error == 0) ? 0 : -1;
 	return (error);
 }
 
@@ -774,24 +821,30 @@
 	} */
 	struct vnode *vp;
 	char attrname[XATTR_NAME_MAX];
+	int namespace;
+	register_t attrlen;
 	int error;
 
 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
 	    NULL);
 	if (error)
-		return (error);
+		goto out;
 
 	error = namei_simple_user(SCARG(uap, path),
 				NSM_NOFOLLOW_NOEMULROOT, &vp);
 	if (error)
-		return (error);
+		goto out_rele;
 
-	/* XXX flags */
+	namespace = xattr_native(attrname);
 
-	error = extattr_set_vp(vp, EXTATTR_NAMESPACE_USER,
-	    attrname, SCARG(uap, value), SCARG(uap, size), l, retval);
+	error = extattr_set_vp(vp, namespace,
+	    attrname, SCARG(uap, value), SCARG(uap, size), l,
+	    &attrlen, SCARG(uap, flags));
 
+out_rele:
 	vrele(vp);
+out:
+	*retval = (error == 0) ? 0 : -1;
 	return (error);
 }
 
@@ -808,24 +861,29 @@
 	struct file *fp;
 	struct vnode *vp;
 	char attrname[XATTR_NAME_MAX];
+	int namespace;
+	register_t attrlen;
 	int error;
 
 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
 	    NULL);
 	if (error)
-		return (error);
+		goto out;
 
 	error = fd_getvnode(SCARG(uap, fd), &fp);
 	if (error)
-		return (error);
+		goto out;
 	vp = (struct vnode *) fp->f_data;
 
-	/* XXX flags */
+	namespace = xattr_native(attrname);
 
-	error = extattr_set_vp(vp, EXTATTR_NAMESPACE_USER,
-	    attrname, SCARG(uap, value), SCARG(uap, size), l, retval);
+	error = extattr_set_vp(vp, namespace,
+	    attrname, SCARG(uap, value), SCARG(uap, size), l,
+	    &attrlen, SCARG(uap, flags));
 
 	fd_putfile(SCARG(uap, fd));
+out:
+	*retval = (error == 0) ? 0 : -1;
 	return (error);
 }
 
@@ -840,6 +898,7 @@
 	} */
 	struct vnode *vp;
 	char attrname[XATTR_NAME_MAX];
+	int namespace;
 	int error;
 
 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
@@ -852,7 +911,9 @@
 	if (error)
 		return (error);
 
-	error = extattr_get_vp(vp, EXTATTR_NAMESPACE_USER,
+	namespace = xattr_native(attrname);
+
+	error = extattr_get_vp(vp, namespace,
 	    attrname, SCARG(uap, value), SCARG(uap, size), l, retval);
 
 	vrele(vp);
@@ -870,6 +931,7 @@
 	} */
 	struct vnode *vp;
 	char attrname[XATTR_NAME_MAX];
+	int namespace;
 	int error;
 
 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
@@ -882,7 +944,9 @@
 	if (error)
 		return (error);
 
-	error = extattr_get_vp(vp, EXTATTR_NAMESPACE_USER,
+	namespace = xattr_native(attrname);
+
+	error = extattr_get_vp(vp, namespace,
 	    attrname, SCARG(uap, value), SCARG(uap, size), l, retval);
 
 	vrele(vp);
@@ -901,6 +965,7 @@
 	struct file *fp;
 	struct vnode *vp;
 	char attrname[XATTR_NAME_MAX];
+	int namespace;
 	int error;
 
 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
@@ -913,7 +978,9 @@
 		return (error);
 	vp = (struct vnode *) fp->f_data;
 
-	error = extattr_get_vp(vp, EXTATTR_NAMESPACE_USER,
+	namespace = xattr_native(attrname);
+
+	error = extattr_get_vp(vp, namespace,
 	    attrname, SCARG(uap, value), SCARG(uap, size), l, retval);
 
 	fd_putfile(SCARG(uap, fd));
@@ -929,6 +996,9 @@
 		syscallarg(size_t) size;
 	} */
 	struct vnode *vp;
+	char *list;
+	size_t size;
+	register_t listsize_usr, listsize_sys;
 	int error;
 
 	error = namei_simple_user(SCARG(uap, path),
@@ -936,9 +1006,27 @@
 	if (error)
 		return (error);
 
+	list = SCARG(uap, list);
+	size = SCARG(uap, size);
+
 	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_USER,
-	    SCARG(uap, list), SCARG(uap, size), l, retval);
+	    list, size, l, &listsize_usr);
+	if (error)
+		goto out;
+
+	if (list)
+		list += listsize_usr;
+	if (size)
+		size -= listsize_usr;
 
+	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_SYSTEM,
+	    list, size, l, &listsize_sys);
+	if (error)
+		goto out;
+
+	*retval = listsize_usr + listsize_sys; 	
+
+out:
 	vrele(vp);
 	return (error);
 }
@@ -952,6 +1040,9 @@
 		syscallarg(size_t) size;
 	} */
 	struct vnode *vp;
+	char *list;
+	size_t size;
+	register_t listsize_usr, listsize_sys;
 	int error;
 
 	error = namei_simple_user(SCARG(uap, path),
@@ -959,9 +1050,26 @@
 	if (error)
 		return (error);
 
+	list = SCARG(uap, list);
+	size = SCARG(uap, size);
+
 	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_USER,
-	    SCARG(uap, list), SCARG(uap, size), l, retval);
+	    list, size, l, &listsize_usr);
+	if (error)
+		goto out;
+
+	if (list)
+		list += listsize_usr;
+	if (size)
+		size -= listsize_usr;
 
+	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_SYSTEM,
+	    list, size, l, &listsize_sys);
+	if (error)
+		goto out;
+
+	*retval = listsize_usr + listsize_sys; 	
+out:
 	vrele(vp);
 	return (error);
 }
@@ -976,6 +1084,9 @@
 	} */
 	struct file *fp;
 	struct vnode *vp;
+	char *list;
+	size_t size;
+	register_t listsize_usr, listsize_sys;
 	int error;
 
 	error = fd_getvnode(SCARG(uap, fd), &fp);
@@ -983,8 +1094,26 @@
 		return (error);
 	vp = (struct vnode *) fp->f_data;
 
+	list = SCARG(uap, list);
+	size = SCARG(uap, size);
+
 	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_USER,
-	    SCARG(uap, list), SCARG(uap, size), l, retval);
+	    list, size, l, &listsize_usr);
+	if (error)
+		goto out;
+
+	if (list)
+		list += listsize_usr;
+	if (size)
+		size -= listsize_usr;
+
+	error = extattr_list_vp(vp, EXTATTR_NAMESPACE_SYSTEM,
+	    list, size, l, &listsize_sys);
+	if (error)
+		goto out;
+
+	*retval = listsize_usr + listsize_sys; 	
+out:
 
 	fd_putfile(SCARG(uap, fd));
 	return (error);
@@ -999,6 +1128,7 @@
 	} */
 	struct vnode *vp;
 	char attrname[XATTR_NAME_MAX];
+	int namespace;
 	int error;
 
 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
@@ -1011,8 +1141,9 @@
 	if (error)
 		return (error);
 
-	error = extattr_delete_vp(vp, EXTATTR_NAMESPACE_USER,
-	    attrname, l);
+	namespace = xattr_native(attrname);
+
+	error = extattr_delete_vp(vp, namespace, attrname, l);
 
 	vrele(vp);
 	return (error);
@@ -1027,6 +1158,7 @@
 	} */
 	struct vnode *vp;
 	char attrname[XATTR_NAME_MAX];
+	int namespace;
 	int error;
 
 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
@@ -1039,8 +1171,9 @@
 	if (error)
 		return (error);
 
-	error = extattr_delete_vp(vp, EXTATTR_NAMESPACE_USER,
-	    attrname, l);
+	namespace = xattr_native(attrname);
+
+	error = extattr_delete_vp(vp, namespace, attrname, l);
 
 	vrele(vp);
 	return (error);
@@ -1056,6 +1189,7 @@
 	struct file *fp;
 	struct vnode *vp;
 	char attrname[XATTR_NAME_MAX];
+	int namespace;
 	int error;
 
 	error = copyinstr(SCARG(uap, name), attrname, sizeof(attrname),
@@ -1068,8 +1202,9 @@
 		return (error);
 	vp = (struct vnode *) fp->f_data;
 
-	error = extattr_delete_vp(vp, EXTATTR_NAMESPACE_USER,
-	    attrname, l);
+	namespace = xattr_native(attrname);
+
+	error = extattr_delete_vp(vp, namespace, attrname, l);
 
 	fd_putfile(SCARG(uap, fd));
 	return (error);

Index: src/sys/sys/Makefile
diff -u src/sys/sys/Makefile:1.132 src/sys/sys/Makefile:1.133
--- src/sys/sys/Makefile:1.132	Wed Mar  9 23:10:07 2011
+++ src/sys/sys/Makefile	Mon Jun 27 16:39:43 2011
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.132 2011/03/09 23:10:07 joerg Exp $
+#	$NetBSD: Makefile,v 1.133 2011/06/27 16:39:43 manu Exp $
 
 .include <bsd.sys.mk>
 
@@ -40,7 +40,7 @@
 	ttydefaults.h ttydev.h types.h \
 	ucontext.h ucred.h uio.h un.h unistd.h unpcb.h user.h utsname.h uuid.h \
 	vadvise.h verified_exec.h videoio.h vmmeter.h vnode.h vnode_if.h \
-	wait.h wapbl.h wapbl_replay.h wdog.h
+	wait.h wapbl.h wapbl_replay.h wdog.h xattr.h
 
 INCSYMLINKS=\
 	sys/exec_elf.h /usr/include/elf.h \

Reply via email to