From owner-svn-src-all@freebsd.org Mon Nov 11 15:26:06 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id E30501B7CA9; Mon, 11 Nov 2019 15:26:06 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47BZTV5ckQz4Hs4; Mon, 11 Nov 2019 15:26:06 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id A411019559; Mon, 11 Nov 2019 15:26:06 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id xABFQ6JG022360; Mon, 11 Nov 2019 15:26:06 GMT (envelope-from hselasky@FreeBSD.org) Received: (from hselasky@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id xABFQ6dC022358; Mon, 11 Nov 2019 15:26:06 GMT (envelope-from hselasky@FreeBSD.org) Message-Id: <201911111526.xABFQ6dC022358@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: hselasky set sender to hselasky@FreeBSD.org using -f From: Hans Petter Selasky Date: Mon, 11 Nov 2019 15:26:06 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r354615 - in stable/11/sys: compat/linuxkpi/common/include/linux sys X-SVN-Group: stable-11 X-SVN-Commit-Author: hselasky X-SVN-Commit-Paths: in stable/11/sys: compat/linuxkpi/common/include/linux sys X-SVN-Commit-Revision: 354615 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Nov 2019 15:26:07 -0000 Author: hselasky Date: Mon Nov 11 15:26:05 2019 New Revision: 354615 URL: https://svnweb.freebsd.org/changeset/base/354615 Log: MFC r351937: LinuxKPI: Improve sysfs support. - Add functions for creating and merging sysfs groups. - Add sysfs_streq function to compare strings ignoring newline from the sysctl userland call. - Add a call to sysfs_create_groups in device_add. - Remove duplicate header include. - Bump __FreeBSD_version. Differential Revision: D21542 Modified: stable/11/sys/compat/linuxkpi/common/include/linux/device.h stable/11/sys/compat/linuxkpi/common/include/linux/sysfs.h stable/11/sys/sys/param.h Directory Properties: stable/11/ (props changed) Modified: stable/11/sys/compat/linuxkpi/common/include/linux/device.h ============================================================================== --- stable/11/sys/compat/linuxkpi/common/include/linux/device.h Mon Nov 11 15:23:58 2019 (r354614) +++ stable/11/sys/compat/linuxkpi/common/include/linux/device.h Mon Nov 11 15:26:05 2019 (r354615) @@ -40,7 +40,6 @@ #include #include #include -#include #include #include @@ -315,6 +314,10 @@ device_add(struct device *dev) dev->devt = makedev(0, device_get_unit(dev->bsddev)); } kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); + + if (dev->groups) + return (sysfs_create_groups(&dev->kobj, dev->groups)); + return (0); } Modified: stable/11/sys/compat/linuxkpi/common/include/linux/sysfs.h ============================================================================== --- stable/11/sys/compat/linuxkpi/common/include/linux/sysfs.h Mon Nov 11 15:23:58 2019 (r354614) +++ stable/11/sys/compat/linuxkpi/common/include/linux/sysfs.h Mon Nov 11 15:26:05 2019 (r354615) @@ -175,6 +175,27 @@ sysfs_remove_files(struct kobject *kobj, const struct sysfs_remove_file(kobj, attrs[i]); } +static inline int +sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp) +{ + struct attribute **attr; + struct sysctl_oid *oidp; + + /* Don't create the group node if grp->name is undefined. */ + if (grp->name) + oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->oidp), + OID_AUTO, grp->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, grp->name); + else + oidp = kobj->oidp; + for (attr = grp->attrs; *attr != NULL; attr++) { + SYSCTL_ADD_OID(NULL, SYSCTL_CHILDREN(oidp), OID_AUTO, + (*attr)->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE, + kobj, (uintptr_t)*attr, sysctl_handle_attr, "A", ""); + } + + return (0); +} + static inline void sysfs_remove_group(struct kobject *kobj, const struct attribute_group *grp) { @@ -184,20 +205,40 @@ sysfs_remove_group(struct kobject *kobj, const struct } static inline int -sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp) +sysfs_create_groups(struct kobject *kobj, const struct attribute_group **grps) { + int error = 0; + int i; + + for (i = 0; grps[i] && !error; i++) + error = sysfs_create_group(kobj, grps[i]); + while (error && --i >= 0) + sysfs_remove_group(kobj, grps[i]); + + return (error); +} + +static inline int +sysfs_merge_group(struct kobject *kobj, const struct attribute_group *grp) +{ + + /* Really expected behavior is to return failure if group exists. */ + return (sysfs_create_group(kobj, grp)); +} + +static inline void +sysfs_unmerge_group(struct kobject *kobj, const struct attribute_group *grp) +{ struct attribute **attr; struct sysctl_oid *oidp; - oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(kobj->oidp), - OID_AUTO, grp->name, CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, grp->name); - for (attr = grp->attrs; *attr != NULL; attr++) { - SYSCTL_ADD_OID(NULL, SYSCTL_CHILDREN(oidp), OID_AUTO, - (*attr)->name, CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_MPSAFE, - kobj, (uintptr_t)*attr, sysctl_handle_attr, "A", ""); + SLIST_FOREACH(oidp, SYSCTL_CHILDREN(kobj->oidp), oid_link) { + if (strcmp(oidp->oid_name, grp->name) != 0) + continue; + for (attr = grp->attrs; *attr != NULL; attr++) { + sysctl_remove_name(oidp, (*attr)->name, 1, 1); + } } - - return (0); } static inline int @@ -222,6 +263,22 @@ sysfs_remove_dir(struct kobject *kobj) if (kobj->oidp == NULL) return; sysctl_remove_oid(kobj->oidp, 1, 1); +} + +static inline bool +sysfs_streq(const char *s1, const char *s2) +{ + int l1, l2; + + l1 = strlen(s1); + l2 = strlen(s2); + + if (l1 != 0 && s1[l1-1] == '\n') + l1--; + if (l2 != 0 && s2[l2-1] == '\n') + l2--; + + return (l1 == l2 && strncmp(s1, s2, l1) == 0); } #define sysfs_attr_init(attr) do {} while(0) Modified: stable/11/sys/sys/param.h ============================================================================== --- stable/11/sys/sys/param.h Mon Nov 11 15:23:58 2019 (r354614) +++ stable/11/sys/sys/param.h Mon Nov 11 15:26:05 2019 (r354615) @@ -58,7 +58,7 @@ * in the range 5 to 9. */ #undef __FreeBSD_version -#define __FreeBSD_version 1103502 /* Master, propagated to newvers */ +#define __FreeBSD_version 1103503 /* Master, propagated to newvers */ /* * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD,