Module Name:    src
Committed By:   jdolecek
Date:           Fri Aug 12 19:04:03 UTC 2016

Modified Files:
        src/sys/ufs: files.ufs
        src/sys/ufs/ext2fs: ext2fs.h ext2fs_dinode.h ext2fs_vnops.c
Added Files:
        src/sys/ufs/ext2fs: ext2fs_xattr.c ext2fs_xattr.h

Log Message:
add support for extended attributes in ext2fs for ext3/ext4; read-only for now


To generate a diff of this commit:
cvs rdiff -u -r1.42 -r1.43 src/sys/ufs/files.ufs
cvs rdiff -u -r1.41 -r1.42 src/sys/ufs/ext2fs/ext2fs.h
cvs rdiff -u -r1.35 -r1.36 src/sys/ufs/ext2fs/ext2fs_dinode.h
cvs rdiff -u -r1.120 -r1.121 src/sys/ufs/ext2fs/ext2fs_vnops.c
cvs rdiff -u -r0 -r1.1 src/sys/ufs/ext2fs/ext2fs_xattr.c \
    src/sys/ufs/ext2fs/ext2fs_xattr.h

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

Modified files:

Index: src/sys/ufs/files.ufs
diff -u src/sys/ufs/files.ufs:1.42 src/sys/ufs/files.ufs:1.43
--- src/sys/ufs/files.ufs:1.42	Fri Jun 24 17:21:56 2016
+++ src/sys/ufs/files.ufs	Fri Aug 12 19:04:03 2016
@@ -1,4 +1,4 @@
-#	$NetBSD: files.ufs,v 1.42 2016/06/24 17:21:56 christos Exp $
+#	$NetBSD: files.ufs,v 1.43 2016/08/12 19:04:03 jdolecek Exp $
 
 deffs					FFS
 deffs					EXT2FS
@@ -28,6 +28,7 @@ file	ufs/ext2fs/ext2fs_rename.c	ext2fs
 file	ufs/ext2fs/ext2fs_subr.c	ext2fs
 file	ufs/ext2fs/ext2fs_vfsops.c	ext2fs
 file	ufs/ext2fs/ext2fs_vnops.c	ext2fs
+file	ufs/ext2fs/ext2fs_xattr.c	ext2fs
 
 define	chfs: vfs, ffs
 file	ufs/chfs/ebh.c			chfs

Index: src/sys/ufs/ext2fs/ext2fs.h
diff -u src/sys/ufs/ext2fs/ext2fs.h:1.41 src/sys/ufs/ext2fs/ext2fs.h:1.42
--- src/sys/ufs/ext2fs/ext2fs.h:1.41	Fri Aug  5 21:22:06 2016
+++ src/sys/ufs/ext2fs/ext2fs.h	Fri Aug 12 19:04:03 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ext2fs.h,v 1.41 2016/08/05 21:22:06 jdolecek Exp $	*/
+/*	$NetBSD: ext2fs.h,v 1.42 2016/08/12 19:04:03 jdolecek Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1993
@@ -341,6 +341,14 @@ struct m_ext2fs {
 	"\02INCOMPAT_FTYPE" \
 	"\01INCOMPAT_COMP"
 
+#define EXT2F_HAS_COMPAT_FEATURE(ip, feature) \
+	(ip)->i_e2fs->e2fs.e2fs_rev >= E2FS_REV1 && \
+	((ip)->i_e2fs->e2fs.e2fs_features_compat & (feature)) != 0
+
+#define EXT2F_HAS_INCOMPAT_FEATURE(ip, feature) \
+	(ip)->i_e2fs->e2fs.e2fs_rev >= E2FS_REV1 && \
+	((ip)->i_e2fs->e2fs.e2fs_features_incompat & (feature)) != 0
+
 /*
  * Features supported in this implementation
  *

Index: src/sys/ufs/ext2fs/ext2fs_dinode.h
diff -u src/sys/ufs/ext2fs/ext2fs_dinode.h:1.35 src/sys/ufs/ext2fs/ext2fs_dinode.h:1.36
--- src/sys/ufs/ext2fs/ext2fs_dinode.h:1.35	Sat Aug  6 09:29:28 2016
+++ src/sys/ufs/ext2fs/ext2fs_dinode.h	Fri Aug 12 19:04:03 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ext2fs_dinode.h,v 1.35 2016/08/06 09:29:28 jdolecek Exp $	*/
+/*	$NetBSD: ext2fs_dinode.h,v 1.36 2016/08/12 19:04:03 jdolecek Exp $	*/
 
 /*
  * Copyright (c) 1982, 1989, 1993
@@ -123,11 +123,11 @@ struct ext2fs_dinode {
 	uint32_t	e2di_blocks[EXT2FS_NDADDR+EXT2FS_NIADDR];
 					/* 40: disk blocks */
 	uint32_t	e2di_gen;	/* 100: generation number */
-	uint32_t	e2di_facl;	/* 104: file ACL (not implemented) (ext3) */
+	uint32_t	e2di_facl;	/* 104: file ACL (ext3) */
 	uint32_t	e2di_size_high;	/* 108: Size (in bytes) high */
 	uint32_t	e2di_obso_faddr;/* 112: obsolete fragment address (ext2) */
 	uint16_t	e2di_nblock_high; /* 116: Blocks count bits 47:32 (ext4) */
-	uint16_t	e2di_facl_high; /* 118: file ACL bits 47:32 (ext4) */
+	uint16_t	e2di_facl_high; /* 118: file ACL bits 47:32 (ext4/64bit) */
 	uint16_t	e2di_uid_high;	/* 120: Owner UID top 16 bits (ext4) */
 	uint16_t	e2di_gid_high;	/* 122: Owner GID top 16 bits (ext4) */
 	uint16_t 	e2di_checksum_low;  /* 124: crc LE (not implemented) (ext4) */

Index: src/sys/ufs/ext2fs/ext2fs_vnops.c
diff -u src/sys/ufs/ext2fs/ext2fs_vnops.c:1.120 src/sys/ufs/ext2fs/ext2fs_vnops.c:1.121
--- src/sys/ufs/ext2fs/ext2fs_vnops.c:1.120	Fri Aug  5 20:15:41 2016
+++ src/sys/ufs/ext2fs/ext2fs_vnops.c	Fri Aug 12 19:04:03 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ext2fs_vnops.c,v 1.120 2016/08/05 20:15:41 jdolecek Exp $	*/
+/*	$NetBSD: ext2fs_vnops.c,v 1.121 2016/08/12 19:04:03 jdolecek Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -65,7 +65,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ext2fs_vnops.c,v 1.120 2016/08/05 20:15:41 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ext2fs_vnops.c,v 1.121 2016/08/12 19:04:03 jdolecek Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -94,6 +94,7 @@ __KERNEL_RCSID(0, "$NetBSD: ext2fs_vnops
 #include <ufs/ext2fs/ext2fs.h>
 #include <ufs/ext2fs/ext2fs_extern.h>
 #include <ufs/ext2fs/ext2fs_dir.h>
+#include <ufs/ext2fs/ext2fs_xattr.h>
 
 extern int prtactive;
 
@@ -1182,6 +1183,10 @@ const struct vnodeopv_entry_desc ext2fs_
 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
+	{ &vop_getextattr_desc, ext2fs_getextattr },	/* getextattr */
+	{ &vop_setextattr_desc, ext2fs_setextattr },	/* setextattr */
+	{ &vop_listextattr_desc, ext2fs_listextattr },	/* listextattr */
+	{ &vop_deleteextattr_desc, ext2fs_deleteextattr },/* deleteextattr */
 	{ NULL, NULL }
 };
 const struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
@@ -1232,6 +1237,10 @@ const struct vnodeopv_entry_desc ext2fs_
 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
+	{ &vop_getextattr_desc, ext2fs_getextattr },	/* getextattr */
+	{ &vop_setextattr_desc, ext2fs_setextattr },	/* setextattr */
+	{ &vop_listextattr_desc, ext2fs_listextattr },	/* listextattr */
+	{ &vop_deleteextattr_desc, ext2fs_deleteextattr },/* deleteextattr */
 	{ NULL, NULL }
 };
 const struct vnodeopv_desc ext2fs_specop_opv_desc =
@@ -1281,6 +1290,10 @@ const struct vnodeopv_entry_desc ext2fs_
 	{ &vop_advlock_desc, vn_fifo_bypass },		/* advlock */
 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
 	{ &vop_putpages_desc, vn_fifo_bypass },		/* putpages */
+	{ &vop_getextattr_desc, ext2fs_getextattr },	/* getextattr */
+	{ &vop_setextattr_desc, ext2fs_setextattr },	/* setextattr */
+	{ &vop_listextattr_desc, ext2fs_listextattr },	/* listextattr */
+	{ &vop_deleteextattr_desc, ext2fs_deleteextattr },/* deleteextattr */
 	{ NULL, NULL }
 };
 const struct vnodeopv_desc ext2fs_fifoop_opv_desc =

Added files:

Index: src/sys/ufs/ext2fs/ext2fs_xattr.c
diff -u /dev/null src/sys/ufs/ext2fs/ext2fs_xattr.c:1.1
--- /dev/null	Fri Aug 12 19:04:03 2016
+++ src/sys/ufs/ext2fs/ext2fs_xattr.c	Fri Aug 12 19:04:03 2016
@@ -0,0 +1,450 @@
+/*	$NetBSD: ext2fs_xattr.c,v 1.1 2016/08/12 19:04:03 jdolecek Exp $	*/
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jaromir Dolecek.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: ext2fs_xattr.c,v 1.1 2016/08/12 19:04:03 jdolecek Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/mount.h>
+#include <sys/proc.h>
+#include <sys/file.h>
+#include <sys/buf.h>
+#include <sys/vnode.h>
+#include <sys/kernel.h>
+#include <sys/kmem.h>
+#include <sys/trace.h>
+#include <sys/resourcevar.h>
+#include <sys/kauth.h>
+#include <sys/extattr.h>
+
+#include <ufs/ufs/inode.h>
+#include <ufs/ufs/ufsmount.h>
+#include <ufs/ufs/ufs_extern.h>
+
+#include <ufs/ext2fs/ext2fs.h>
+#include <ufs/ext2fs/ext2fs_extern.h>
+#include <ufs/ext2fs/ext2fs_xattr.h>
+
+static const char * const xattr_prefix_index[] = {
+	"",
+	"user.",
+	"system.posix_acl_access",
+	"system.posix_acl_default",
+	"trusted.",
+	"", 	/* unused */
+	"security",
+	"system.",
+	"system.richacl",
+	"c",
+};
+
+static int
+ext2fs_find_xattr(struct ext2fs_xattr_entry *e, uint8_t *start, uint8_t *end, int attrnamespace, struct uio *uio, size_t *size, uint8_t name_index, const char *name)
+{
+	uint8_t *value;
+	int error;
+	size_t value_offs, value_len, len, old_len;
+
+	/*
+	 * Individual entries follow the header. Each is aligned on 4-byte
+	 * boundary.
+	 */
+	for(; !EXT2FS_XATTR_IS_LAST_ENTRY(e, end); e = EXT2FS_XATTR_NEXT(e)) {
+		/*
+		 * Only EXT2FS_XATTR_PREFIX_USER is USER, anything else
+		 * is considered SYSTEM.
+		 */
+		if ((attrnamespace == EXTATTR_NAMESPACE_USER && e->e_name_index != EXT2FS_XATTR_PREFIX_USER) ||
+		    (attrnamespace == EXTATTR_NAMESPACE_SYSTEM && e->e_name_index == EXT2FS_XATTR_PREFIX_USER)) {
+			continue;
+		}
+
+		if (e->e_name_index != name_index ||
+		    e->e_name_len != strlen(name) ||
+		    strncmp(e->e_name, name, e->e_name_len) != 0)
+			continue;
+
+		value_offs = fs2h32(e->e_value_offs);
+		value_len  = fs2h32(e->e_value_size);
+		value      = &start[value_offs];
+
+		/* make sure the value offset are sane */
+		if (&value[value_len] > end)
+			return (EINVAL);
+
+		if (uio != NULL) {
+			/*
+			 * Figure out maximum to transfer -- use buffer size and
+			 * local data limit.
+			 */
+			len = MIN(uio->uio_resid, value_len);
+			old_len = uio->uio_resid;
+			uio->uio_resid = len;
+
+			uio->uio_resid = old_len - (len - uio->uio_resid);
+
+			error = uiomove(value, value_len, uio);
+			if (error)
+				return error;
+		}
+
+		/* full data size */
+		*size += value_len;
+
+		goto found;
+	}
+
+	/* requested attribute not found */
+	return (ENODATA);
+
+    found:
+	return (0);
+}
+
+static int
+ext2fs_get_inode_xattr(struct inode *ip, int attrnamespace, struct uio *uio, size_t *size, uint8_t name_index, const char *name)
+{
+	struct ext2fs_dinode *di = ip->i_din.e2fs_din;
+	struct ext2fs_xattr_ibody_header *h;
+	uint8_t *start, *end;
+
+	start = &((uint8_t *)di)[EXT2_REV0_DINODE_SIZE + di->e2di_extra_isize];
+	h     = (struct ext2fs_xattr_ibody_header *)start;
+	end   = &((uint8_t *)di)[EXT2_DINODE_SIZE(ip->i_e2fs)];
+
+	if (end <= start || fs2h32(h->h_magic) != EXT2FS_XATTR_MAGIC)
+		return (ENODATA);
+
+	return ext2fs_find_xattr(EXT2FS_XATTR_IFIRST(h), start, end, attrnamespace, uio, size, name_index, name);
+}
+
+static int
+ext2fs_get_block_xattr(struct inode *ip, int attrnamespace, struct uio *uio, size_t *size, uint8_t name_index, const char *name)
+{
+	struct ext2fs_dinode *di = ip->i_din.e2fs_din;
+	uint8_t *start, *end;
+	struct ext2fs_xattr_header *h;
+	int error = 0;
+	struct buf *bp = NULL;
+	daddr_t xblk;
+
+	xblk = di->e2di_facl;
+	if (EXT2F_HAS_INCOMPAT_FEATURE(ip, EXT2F_INCOMPAT_64BIT))
+		xblk |= (((daddr_t)di->e2di_facl_high) << 32);
+
+	/* don't do anything if no attr block was allocated */
+	if (xblk == 0)
+		return (0);
+
+	error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, xblk), (int)ip->i_e2fs->e2fs_bsize, 0, &bp);
+	if (error)
+		goto out;
+
+	start = (uint8_t *)bp->b_data;
+	h     = (struct ext2fs_xattr_header *)start;
+	end   = &((uint8_t *)bp->b_data)[bp->b_bcount];
+
+	if (end <= start || fs2h32(h->h_magic) != EXT2FS_XATTR_MAGIC)
+		goto out;
+
+	error = ext2fs_find_xattr(EXT2FS_XATTR_BFIRST(h), start, end, attrnamespace, uio, size, name_index, name);
+
+    out:
+	if (bp)
+		brelse(bp, 0);
+	return (error);
+}
+int
+ext2fs_getextattr(void *v)
+{
+	struct vop_getextattr_args /* {
+	        const struct vnodeop_desc *a_desc;
+	        struct vnode *a_vp;
+	        int a_attrnamespace;
+	        const char *a_name;
+	        struct uio *a_uio;
+	        size_t *a_size;
+	        kauth_cred_t a_cred;
+	} */ *ap = v;
+	struct inode *ip = VTOI(ap->a_vp);
+	char namebuf[EXT2FS_XATTR_NAME_LEN_MAX + 1];
+	int error, i;
+	const char *prefix, *name;
+	uint8_t name_index;
+	size_t name_match, valuesize = 0;
+
+	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_USER)
+		prefix = xattr_prefix_index[EXT2FS_XATTR_PREFIX_USER];
+	else
+		prefix = xattr_prefix_index[EXT2FS_XATTR_PREFIX_SYSTEM];
+	snprintf(namebuf, sizeof(namebuf), "%s%s", prefix, ap->a_name);
+
+        error = extattr_check_cred(ap->a_vp, namebuf, ap->a_cred, VREAD);
+        if (error)
+                return (error);
+
+        /*
+         * Allow only offsets of zero to encourage the read/replace
+         * extended attribute semantic.  Otherwise we can't guarantee
+         * atomicity, as we don't provide locks for extended attributes.
+         */
+        if (ap->a_uio != NULL && ap->a_uio->uio_offset != 0)
+                return (ENXIO);
+
+	/* figure out the name index */
+	name = ap->a_name;
+	name_index = 0;
+	name_match = 0;
+	for(i=0; i < sizeof(xattr_prefix_index)/sizeof(void *); i++) {
+		prefix = xattr_prefix_index[i];
+		if (strlen(prefix) > 0 &&
+		    strncmp(ap->a_name, prefix, strlen(prefix)) == 0 &&
+		    name_match < strlen(prefix)) {
+			name = &ap->a_name[strlen(prefix)];
+			name_index = i;
+			name_match = strlen(prefix);
+			continue;
+		}
+	}
+
+	/* fetch the xattr */
+	error = ext2fs_get_inode_xattr(ip, ap->a_attrnamespace, ap->a_uio, &valuesize, name_index, name);
+	if (error == ENODATA) {
+		/* not found in inode, try facl */
+		error = ext2fs_get_block_xattr(ip, ap->a_attrnamespace, ap->a_uio, &valuesize, name_index, name);
+	}
+
+	if (ap->a_size != NULL)
+		*ap->a_size = valuesize;
+
+	return (error);
+}
+
+int
+ext2fs_setextattr(void *v)
+{
+#if 0
+	struct vop_setextattr_args /* {
+	        const struct vnodeop_desc *a_desc;
+	        struct vnode *a_vp;
+	        int a_attrnamespace;
+	        const char *a_name;
+	        struct uio *a_uio;
+	        kauth_cred_t a_cred;
+	} */ *ap = v;
+
+	/* XXX set EXT2F_COMPAT_EXTATTR in superblock after successful set */
+#endif
+
+	/* XXX Not implemented */
+	return (EOPNOTSUPP);
+}
+
+static int
+ext2fs_list_xattr(struct ext2fs_xattr_entry *e, uint8_t *end, int attrnamespace, int flags, struct uio *uio, size_t *size)
+{
+	char name[EXT2FS_XATTR_NAME_LEN_MAX + 1];
+	uint8_t len;
+	int error;
+	const char *prefix;
+
+	/*
+	 * Individual entries follow the header. Each is aligned on 4-byte
+	 * boundary.
+	 */
+	for(; !EXT2FS_XATTR_IS_LAST_ENTRY(e, end); e = EXT2FS_XATTR_NEXT(e)) {
+		/*
+		 * Only EXT2FS_XATTR_PREFIX_USER is USER, anything else
+		 * is considered SYSTEM.
+		 */
+		if ((attrnamespace == EXTATTR_NAMESPACE_USER && e->e_name_index != EXT2FS_XATTR_PREFIX_USER) ||
+		    (attrnamespace == EXTATTR_NAMESPACE_SYSTEM && e->e_name_index == EXT2FS_XATTR_PREFIX_USER)) {
+			continue;
+		}
+
+		if (e->e_name_index <= sizeof(xattr_prefix_index)/sizeof(void *))
+			prefix = xattr_prefix_index[e->e_name_index];
+		else
+			prefix = "";
+
+		len = snprintf(name, sizeof(name), "%s%.*s",
+			prefix,
+			e->e_name_len, e->e_name);
+
+		if (uio != NULL) {
+			if (flags & EXTATTR_LIST_LENPREFIX) {
+				/* write name length */
+				uiomove(&len, sizeof(uint8_t), uio);
+			} else {
+				/* include trailing NUL */
+				len++;
+			}
+
+			error = uiomove(name, len, uio);
+			if (error)
+				return error;
+
+			*size += len;
+		}
+	}
+
+	return (0);
+}
+
+static int
+ext2fs_list_inode_xattr(struct inode *ip, int attrnamespace, int flags, struct uio *uio, size_t *size)
+{
+	struct ext2fs_dinode *di = ip->i_din.e2fs_din;
+	void *start, *end;
+	struct ext2fs_xattr_ibody_header *h;
+
+	start = &((uint8_t *)di)[EXT2_REV0_DINODE_SIZE + di->e2di_extra_isize];
+	h     = start;
+	end   = &((uint8_t *)di)[EXT2_DINODE_SIZE(ip->i_e2fs)];
+
+	if (end <= start || fs2h32(h->h_magic) != EXT2FS_XATTR_MAGIC)
+		return (0);
+
+	return ext2fs_list_xattr(EXT2FS_XATTR_IFIRST(h), end, attrnamespace, flags, uio, size);
+}
+
+static int
+ext2fs_list_block_xattr(struct inode *ip, int attrnamespace, int flags, struct uio *uio, size_t *size)
+{
+	struct ext2fs_dinode *di = ip->i_din.e2fs_din;
+	void *end;
+	struct ext2fs_xattr_header *h;
+	int error = 0;
+	struct buf *bp = NULL;
+	daddr_t xblk;
+
+	xblk = di->e2di_facl;
+	if (EXT2F_HAS_INCOMPAT_FEATURE(ip, EXT2F_INCOMPAT_64BIT))
+		xblk |= (((daddr_t)di->e2di_facl_high) << 32);
+
+	/* don't do anything if no attr block was allocated */
+	if (xblk == 0)
+		return (0);
+
+	error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, xblk), (int)ip->i_e2fs->e2fs_bsize, 0, &bp);
+	if (error)
+		goto out;
+
+	h     = (struct ext2fs_xattr_header *)bp->b_data;
+	end   = &((uint8_t *)bp->b_data)[bp->b_bcount];
+
+	if (end <= (void *)h || fs2h32(h->h_magic) != EXT2FS_XATTR_MAGIC)
+		goto out;
+
+	error = ext2fs_list_xattr(EXT2FS_XATTR_BFIRST(h), end, attrnamespace, flags, uio, size);
+
+    out:
+	if (bp)
+		brelse(bp, 0);
+	return (error);
+}
+
+int
+ext2fs_listextattr(void *v)
+{
+	struct vop_listextattr_args /* {
+	        const struct vnodeop_desc *a_desc;
+	        struct vnode *a_vp;
+	        int a_attrnamespace;
+	        struct uio *a_uio;
+	        size_t *a_size;
+	        int a_flag;
+	        kauth_cred_t a_cred;
+	} */ *ap = v;
+	struct inode *ip = VTOI(ap->a_vp);
+	int error;
+	const char *prefix;
+	size_t listsize = 0;
+
+	if (!EXT2F_HAS_COMPAT_FEATURE(ip, EXT2F_COMPAT_EXTATTR)) {
+		/* no EA on the filesystem */
+		goto out;
+	}
+
+        /*
+         * XXX: We can move this inside the loop and iterate on individual
+         *      attributes.
+         */
+	if (ap->a_attrnamespace == EXTATTR_NAMESPACE_USER)
+		prefix = xattr_prefix_index[EXT2FS_XATTR_PREFIX_USER];
+	else
+		prefix = xattr_prefix_index[EXT2FS_XATTR_PREFIX_SYSTEM];
+        error = extattr_check_cred(ap->a_vp, prefix, ap->a_cred, VREAD);
+        if (error)
+                return (error);
+
+        /*
+         * Allow only offsets of zero to encourage the read/replace
+         * extended attribute semantic.  Otherwise we can't guarantee
+         * atomicity, as we don't provide locks for extended attributes.
+	 * XXX revisit - vnode lock enough?
+         */
+        if (ap->a_uio != NULL && ap->a_uio->uio_offset != 0)
+                return (ENXIO);
+
+	/* fetch inode xattrs */
+	error = ext2fs_list_inode_xattr(ip, ap->a_attrnamespace, ap->a_flag, ap->a_uio, &listsize);
+	if (error)
+		return (error);
+
+	error = ext2fs_list_block_xattr(ip, ap->a_attrnamespace, ap->a_flag, ap->a_uio, &listsize);
+	if (error)
+		return (error);
+
+    out:
+	if (ap->a_size != NULL)
+		*ap->a_size = listsize;
+
+	return (0);
+}
+
+int
+ext2fs_deleteextattr(void *v)
+{
+#if 0
+	struct vop_deleteextattr_args /* {
+	        const struct vnodeop_desc *a_desc;
+	        struct vnode *a_vp;
+	        int a_attrnamespace;
+	        const char *a_name;
+	        kauth_cred_t a_cred;
+	} */ *ap = v;
+#endif
+
+	/* XXX Not implemented */
+	return (EOPNOTSUPP);
+}
Index: src/sys/ufs/ext2fs/ext2fs_xattr.h
diff -u /dev/null src/sys/ufs/ext2fs/ext2fs_xattr.h:1.1
--- /dev/null	Fri Aug 12 19:04:03 2016
+++ src/sys/ufs/ext2fs/ext2fs_xattr.h	Fri Aug 12 19:04:03 2016
@@ -0,0 +1,117 @@
+/*	$NetBSD: ext2fs_xattr.h,v 1.1 2016/08/12 19:04:03 jdolecek Exp $	*/
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jaromir Dolecek.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _UFS_EXT2FS_EXT2FS_XATTR_H_
+#define _UFS_EXT2FS_EXT2FS_XATTR_H_
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: ext2fs_xattr.h,v 1.1 2016/08/12 19:04:03 jdolecek Exp $");
+
+#ifdef _KERNEL
+
+#define EXT2FS_XATTR_MAGIC 	0xEA020000
+
+#define EXT2FS_XATTR_NAME_LEN_MAX	255
+#define EXT2FS_XATTR_REFCOUNT_MAX	1024
+
+/*
+ * This is used as header for extended attribute block within inode
+ */
+struct ext2fs_xattr_ibody_header {
+	uint32_t h_magic;	/* Magic number - 0xEA020000 */
+};
+
+/*
+ * This is used as header for extended attribute on separate disk block
+ */
+struct ext2fs_xattr_header {
+	uint32_t h_magic;	/* Magic number - 0xEA020000 */
+	uint32_t h_refcount;	/* Reference count */
+	uint32_t h_blocks;	/* Number of blocks - only 1 supported */
+	uint32_t h_hash;	/* Hash of all attributes */
+ 	uint32_t h_checksum;	/* Checksum of the extended attribute block */
+	uint32_t h_reserved[3];
+};
+
+/*
+ * Extended attribute on-disk header structure
+ */
+struct ext2fs_xattr_entry {
+        uint8_t  e_name_len;		/* Name length */
+        uint8_t  e_name_index;		/* Name prefix index (see below) */
+        uint16_t e_value_offs;		/* Offset of value within block */
+        uint32_t e_value_block;		/* Value block - not supported (always zero) */
+        uint32_t e_value_size;		/* Length of value */
+        uint32_t e_hash;		/* Hash (not supported) */
+        char e_name[0];			/* Name string (e_name_len bytes) */
+};
+
+/*
+ * Linux kernel checks only the 0, we also check that the current entry
+ * doesn't overflow past end.
+ */
+#define EXT2FS_XATTR_IS_LAST_ENTRY(entry, end) \
+	*((uint32_t *)(entry)) == 0 || EXT2FS_XATTR_NEXT(entry) > end
+
+/*
+ * Each ext2fs_xattr_entry starts on next 4-byte boundary, pad if necessary.
+ */
+#define EXT2FS_XATTR_PAD	4
+#define EXT2FS_XATTR_ROUND	(EXT2FS_XATTR_PAD - 1)
+#define EXT2FS_XATTR_LEN(name_len) \
+	(((name_len) + EXT2FS_XATTR_ROUND + \
+	sizeof(struct ext2fs_xattr_entry)) & ~EXT2FS_XATTR_ROUND)
+#define EXT2FS_XATTR_NEXT(entry) \
+	(struct ext2fs_xattr_entry *)(((uint8_t *)(entry)) + EXT2FS_XATTR_LEN((entry)->e_name_len))
+
+#define EXT2FS_XATTR_IFIRST(h) (void *)&(h)[1]
+#define EXT2FS_XATTR_BFIRST(h) EXT2FS_XATTR_IFIRST(h)
+
+/*
+ * Name prefixes
+ */
+#define EXT2FS_XATTR_PREFIX_NONE		0 /* no prefix */
+#define EXT2FS_XATTR_PREFIX_USER		1 /* "user." */
+#define EXT2FS_XATTR_PREFIX_POSIX_ACCESS	2 /* "system.posix_acl_access" */
+#define EXT2FS_XATTR_PREFIX_POSIX_DEFAULT1	3 /* "system.posix_acl_default" */
+#define EXT2FS_XATTR_PREFIX_TRUSTED		4 /* "trusted." */
+#define EXT2FS_XATTR_PREFIX_SECURITY		6 /* "security." */
+#define EXT2FS_XATTR_PREFIX_SYSTEM		7 /* "system." */
+#define EXT2FS_XATTR_PREFIX_SYSTEM_RICHACL	8 /* "system.richacl" */
+#define EXT2FS_XATTR_PREFIX_ENCRYPTION		9 /* "c" */
+
+int ext2fs_getextattr(void *);
+int ext2fs_setextattr(void *);
+int ext2fs_listextattr(void *);
+int ext2fs_deleteextattr(void *);
+#endif /* _KERNEL */
+
+#endif /* _UFS_EXT2FS_EXT2FS_XATTR_H_ */

Reply via email to