From: Greg Kroah-Hartman <gregkh@suse.de>
To: linux-kernel@vger.kernel.org
Cc: Bart Van Assche <bvanassche@acm.org>,
Bart Van Assche <bart.vanassche@gmail.com>,
Randy Dunlap <randy.dunlap@oracle.com>,
Andrew Morton <akpm@linux-foundation.org>,
Greg Kroah-Hartman <gregkh@suse.de>
Subject: [PATCH 16/28] sysfs: fix discrepancies between implementation and documentation
Date: Thu, 5 Aug 2010 15:24:06 -0700 [thread overview]
Message-ID: <1281047058-23716-16-git-send-email-gregkh@suse.de> (raw)
In-Reply-To: <20100805213314.GB13744@kroah.com>
From: Bart Van Assche <bvanassche@acm.org>
Fix all discrepancies I know of between the sysfs implementation and its
documentation.
Signed-off-by: Bart Van Assche <bart.vanassche@gmail.com>
Cc: Randy Dunlap <randy.dunlap@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
---
Documentation/filesystems/sysfs.txt | 44 +++++++++++++++++++---------------
1 files changed, 25 insertions(+), 19 deletions(-)
diff --git a/Documentation/filesystems/sysfs.txt b/Documentation/filesystems/sysfs.txt
index 931c806..d78ed0b 100644
--- a/Documentation/filesystems/sysfs.txt
+++ b/Documentation/filesystems/sysfs.txt
@@ -4,7 +4,7 @@ sysfs - _The_ filesystem for exporting kernel objects.
Patrick Mochel <mochel@osdl.org>
Mike Murphy <mamurph@cs.clemson.edu>
-Revised: 22 February 2009
+Revised: 10 July 2010
Original: 10 January 2003
@@ -124,7 +124,7 @@ show and store methods of the attribute owners.
struct sysfs_ops {
ssize_t (*show)(struct kobject *, struct attribute *, char *);
- ssize_t (*store)(struct kobject *, struct attribute *, const char *);
+ ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
};
[ Subsystems should have already defined a struct kobj_type as a
@@ -139,18 +139,22 @@ calls the associated methods.
To illustrate:
+#define to_dev(obj) container_of(obj, struct device, kobj)
#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
-#define to_dev(d) container_of(d, struct device, kobj)
-static ssize_t
-dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
+static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
+ char *buf)
{
- struct device_attribute * dev_attr = to_dev_attr(attr);
- struct device * dev = to_dev(kobj);
- ssize_t ret = 0;
+ struct device_attribute *dev_attr = to_dev_attr(attr);
+ struct device *dev = to_dev(kobj);
+ ssize_t ret = -EIO;
if (dev_attr->show)
- ret = dev_attr->show(dev, buf);
+ ret = dev_attr->show(dev, dev_attr, buf);
+ if (ret >= (ssize_t)PAGE_SIZE) {
+ print_symbol("dev_attr_show: %s returned bad count\n",
+ (unsigned long)dev_attr->show);
+ }
return ret;
}
@@ -163,10 +167,9 @@ To read or write attributes, show() or store() methods must be
specified when declaring the attribute. The method types should be as
simple as those defined for device attributes:
-ssize_t (*show)(struct device * dev, struct device_attribute * attr,
- char * buf);
-ssize_t (*store)(struct device * dev, struct device_attribute * attr,
- const char * buf);
+ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf);
+ssize_t (*store)(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count);
IOW, they should take only an object, an attribute, and a buffer as parameters.
@@ -209,8 +212,8 @@ Other notes:
- show() should always use snprintf().
-- store() should return the number of bytes used from the buffer. This
- can be done using strlen().
+- store() should return the number of bytes used from the buffer. If the
+ entire buffer has been used, just return the count argument.
- show() or store() can always return errors. If a bad value comes
through, be sure to return an error.
@@ -223,15 +226,18 @@ Other notes:
A very simple (and naive) implementation of a device attribute is:
-static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
+static ssize_t show_name(struct device *dev, struct device_attribute *attr,
+ char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", dev->name);
}
-static ssize_t store_name(struct device * dev, const char * buf)
+static ssize_t store_name(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
{
- sscanf(buf, "%20s", dev->name);
- return strnlen(buf, PAGE_SIZE);
+ snprintf(dev->name, sizeof(dev->name), "%.*s",
+ (int)min(count, sizeof(dev->name) - 1), buf);
+ return count;
}
static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
--
1.7.2
next prev parent reply other threads:[~2010-08-05 22:32 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-08-05 21:33 [GIT PATCH] driver core patches for .36 Greg KH
2010-08-05 22:23 ` [PATCH 01/28] uio: Remove IRQF_DISABLED flag from uio_pdrv_genirq.c Greg Kroah-Hartman
2010-08-05 22:23 ` [PATCH 02/28] uio: Remove IRQF_DISABLED from uio_sercos3.c Greg Kroah-Hartman
2010-08-05 22:23 ` [PATCH 03/28] uio: Remove IRQF_DISABLED flag from uio_cif.c Greg Kroah-Hartman
2010-08-05 22:23 ` [PATCH 04/28] hotplug: Support kernel/hotplug sysctl variable when !CONFIG_NET Greg Kroah-Hartman
2010-08-05 22:23 ` [PATCH 05/28] Driver core: internal struct dma_coherent_mem, change type of a member Greg Kroah-Hartman
2010-08-05 22:23 ` [PATCH 06/28] Driver core: Drop __must_check from bus_for_each_drv() Greg Kroah-Hartman
2010-08-05 22:23 ` [PATCH 07/28] firmware loader: use statically initialized data attribute Greg Kroah-Hartman
2010-08-05 22:23 ` [PATCH 08/28] firmware loader: embed device into firmware_priv structure Greg Kroah-Hartman
2010-08-05 22:23 ` [PATCH 09/28] Driver core: use kmemdup in platform_device_add_resources Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 10/28] Driver core: reduce duplicated code for platform_device creation Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 11/28] Driver core: move platform device creation helpers to .init.text (if MODULE=n) Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 12/28] firmware: Update hotplug script Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 13/28] sysfs: sysfs_chmod_file's attr can be const Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 14/28] dmi-id: fix a memory leak in dmi_id_init error path Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 15/28] dcdbas: remove a redundant smi_data_buf_free in dcdbas_exit Greg Kroah-Hartman
2010-08-05 22:24 ` Greg Kroah-Hartman [this message]
2010-08-05 22:24 ` [PATCH 17/28] sysfs: Fix one more signature discrepancy between sysfs implementation and docs Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 18/28] debugfs: no longer needs to depend on SYSFS Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 19/28] driver core: fix memory leak on one error path in bus_register() Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 20/28] Driver core: Add BUS_NOTIFY_BIND_DRIVER Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 21/28] cgroupfs: create /sys/fs/cgroup to mount cgroupfs on Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 22/28] scsi: Remove owner field from attribute initialization in LPFC driver Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 23/28] scsi: Remove owner field from attribute initialization in ARCMSR driver Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 24/28] leds: Remove owner field from attribute initialization in bd2802 driver Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 25/28] regulator: Remove owner field from attribute initialization in regulator core driver Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 26/28] powerpc/pci: Remove owner field from attribute initialization in PCI bridge init Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 27/28] sysfs: Remove owner field from sysfs struct attribute Greg Kroah-Hartman
2010-08-05 22:24 ` [PATCH 28/28] driver core: device_rename's new_name can be const Greg Kroah-Hartman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1281047058-23716-16-git-send-email-gregkh@suse.de \
--to=gregkh@suse.de \
--cc=akpm@linux-foundation.org \
--cc=bart.vanassche@gmail.com \
--cc=bvanassche@acm.org \
--cc=linux-kernel@vger.kernel.org \
--cc=randy.dunlap@oracle.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox