From: SunKe <sunke32@huawei.com>
To: <jlbec@evilplan.org>, <hch@lst.de>,
<linux-kernel@vger.kernel.org>, <sunke32@huawei.com>
Subject: [PATCH 1/2] sample_configfs: bin_file read and write
Date: Tue, 18 Jun 2019 20:28:28 +0800 [thread overview]
Message-ID: <1560860909-51059-2-git-send-email-sunke32@huawei.com> (raw)
In-Reply-To: <1560860909-51059-1-git-send-email-sunke32@huawei.com>
Add bin_file read and write function
Signed-off-by: SunKe <sunke32@huawei.com>
---
samples/configfs/configfs_sample.c | 43 ++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/samples/configfs/configfs_sample.c b/samples/configfs/configfs_sample.c
index 004a4e2..c76b784 100644
--- a/samples/configfs/configfs_sample.c
+++ b/samples/configfs/configfs_sample.c
@@ -146,6 +146,8 @@ static struct childless childless_subsys = {
struct simple_child {
struct config_item item;
int storeme;
+ void *data;
+ size_t len;
};
static inline struct simple_child *to_simple_child(struct config_item *item)
@@ -153,6 +155,46 @@ static inline struct simple_child *to_simple_child(struct config_item *item)
return item ? container_of(item, struct simple_child, item) : NULL;
}
+static ssize_t simple_child_bin_storeme_bin_write(struct config_item *item,
+ const void *data, size_t size)
+{
+ struct simple_child *simple_child = to_simple_child(item);
+
+ kfree(simple_child->data);
+ simple_child->data = NULL;
+
+ simple_child->data = kmemdup(data, size, GFP_KERNEL);
+ if (!simple_child->data)
+ return -ENOMEM;
+ simple_child->len = size;
+
+ return 0;
+}
+
+static ssize_t simple_child_bin_storeme_bin_read(struct config_item *item,
+ void *data, size_t size)
+{
+ struct simple_child *simple_child = to_simple_child(item);
+
+ if (!data) {
+ size = simple_child->len;
+ } else {
+ memcpy(data, simple_child->data, simple_child->len);
+ size = simple_child->len;
+ }
+
+ return size;
+}
+
+#define MAX_SIZE (128 * 1024)
+
+CONFIGFS_BIN_ATTR(simple_child_bin_, storeme_bin, NULL, MAX_SIZE);
+
+static struct configfs_bin_attribute *simple_child_bin_attrs[] = {
+ &simple_child_bin_attr_storeme_bin,
+ NULL,
+};
+
static ssize_t simple_child_storeme_show(struct config_item *item, char *page)
{
return sprintf(page, "%d\n", to_simple_child(item)->storeme);
@@ -196,6 +238,7 @@ static struct configfs_item_operations simple_child_item_ops = {
static const struct config_item_type simple_child_type = {
.ct_item_ops = &simple_child_item_ops,
.ct_attrs = simple_child_attrs,
+ .ct_bin_attrs = simple_child_bin_attrs,
.ct_owner = THIS_MODULE,
};
--
2.7.4
next prev parent reply other threads:[~2019-06-18 12:23 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-18 12:28 [PATCH 0/2] two test samples for configfs SunKe
2019-06-18 12:28 ` SunKe [this message]
2019-06-18 12:28 ` [PATCH 2/2] sample_configfs: soft link creat and delete SunKe
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=1560860909-51059-2-git-send-email-sunke32@huawei.com \
--to=sunke32@huawei.com \
--cc=hch@lst.de \
--cc=jlbec@evilplan.org \
--cc=linux-kernel@vger.kernel.org \
/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
all inboxes | Powered by JetHome®