From: Changli Gao <xiaosuo@gmail.com>
To: Amerigo Wang <amwang@redhat.com>
Cc: linux-kernel@vger.kernel.org,
Octavian Purdila <opurdila@ixiacom.com>,
Eric Dumazet <eric.dumazet@gmail.com>,
penguin-kernel@i-love.sakura.ne.jp, netdev@vger.kernel.org,
Neil Horman <nhorman@tuxdriver.com>,
ebiederm@xmission.com, adobriyan@gmail.com,
David Miller <davem@davemloft.net>
Subject: Re: [Patch 2/3] sysctl: add proc_do_large_bitmap
Date: Sat, 1 May 2010 06:41:18 +0800 [thread overview]
Message-ID: <v2y412e6f7f1004301541yb1ede589t8c446966743ca138@mail.gmail.com> (raw)
In-Reply-To: <20100430082936.5630.67682.sendpatchset@localhost.localdomain>
On Fri, Apr 30, 2010 at 4:25 PM, Amerigo Wang <amwang@redhat.com> wrote:
> From: Octavian Purdila <opurdila@ixiacom.com>
>
> The new function can be used to read/write large bitmaps via /proc. A
> comma separated range format is used for compact output and input
> (e.g. 1,3-4,10-10).
>
> Writing into the file will first reset the bitmap then update it
> based on the given input.
>
> Signed-off-by: Octavian Purdila <opurdila@ixiacom.com>
> Signed-off-by: WANG Cong <amwang@redhat.com>
> Cc: Eric W. Biederman <ebiederm@xmission.com>
> ---
>
> Index: linux-2.6/include/linux/sysctl.h
> ===================================================================
> --- linux-2.6.orig/include/linux/sysctl.h
> +++ linux-2.6/include/linux/sysctl.h
> @@ -980,6 +980,8 @@ extern int proc_doulongvec_minmax(struct
> void __user *, size_t *, loff_t *);
> extern int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int,
> void __user *, size_t *, loff_t *);
> +extern int proc_do_large_bitmap(struct ctl_table *, int,
> + void __user *, size_t *, loff_t *);
>
> /*
> * Register a set of sysctl names by calling register_sysctl_table
> Index: linux-2.6/kernel/sysctl.c
> ===================================================================
> --- linux-2.6.orig/kernel/sysctl.c
> +++ linux-2.6/kernel/sysctl.c
> @@ -2049,6 +2049,16 @@ static size_t proc_skip_spaces(char **bu
> return ret;
> }
>
> +static void proc_skip_char(char **buf, size_t *size, const char v)
> +{
> + while (*size) {
> + if (**buf != v)
> + break;
> + (*size)--;
> + (*buf)++;
> + }
> +}
> +
> #define TMPBUFLEN 22
> /**
> * proc_get_long - reads an ASCII formated integer from a user buffer
> @@ -2675,6 +2685,153 @@ static int proc_do_cad_pid(struct ctl_ta
> return 0;
> }
>
> +/**
> + * proc_do_large_bitmap - read/write from/to a large bitmap
> + * @table: the sysctl table
> + * @write: %TRUE if this is a write to the sysctl file
> + * @buffer: the user buffer
> + * @lenp: the size of the user buffer
> + * @ppos: file position
> + *
> + * The bitmap is stored at table->data and the bitmap length (in bits)
> + * in table->maxlen.
> + *
> + * We use a range comma separated format (e.g. 1,3-4,10-10) so that
> + * large bitmaps may be represented in a compact manner. Writing into
> + * the file will clear the bitmap then update it with the given input.
> + *
> + * Returns 0 on success.
> + */
> +int proc_do_large_bitmap(struct ctl_table *table, int write,
> + void __user *buffer, size_t *lenp, loff_t *ppos)
> +{
> + int err = 0;
> + bool first = 1;
> + size_t left = *lenp;
> + unsigned long bitmap_len = table->maxlen;
> + unsigned long *bitmap = (unsigned long *) table->data;
> + unsigned long *tmp_bitmap = NULL;
> + char tr_a[] = { '-', ',', '\n' }, tr_b[] = { ',', '\n', 0 }, c;
> +
> + if (!bitmap_len || !left || (*ppos && !write)) {
> + *lenp = 0;
> + return 0;
> + }
> +
> + if (write) {
> + unsigned long page = 0;
> + char *kbuf;
> +
> + if (left > PAGE_SIZE - 1)
> + left = PAGE_SIZE - 1;
> +
> + page = __get_free_page(GFP_TEMPORARY);
> + kbuf = (char *) page;
> + if (!kbuf)
> + return -ENOMEM;
> + if (copy_from_user(kbuf, buffer, left)) {
> + free_page(page);
> + return -EFAULT;
> + }
> + kbuf[left] = 0;
> +
> + tmp_bitmap = kzalloc(BITS_TO_LONGS(bitmap_len) * sizeof(unsigned long),
> + GFP_KERNEL);
> + if (!tmp_bitmap) {
> + free_page(page);
> + return -ENOMEM;
> + }
> + proc_skip_char(&kbuf, &left, '\n');
> + while (!err && left) {
> + unsigned long val_a, val_b;
> + bool neg;
> +
> + err = proc_get_long(&kbuf, &left, &val_a, &neg, tr_a,
> + sizeof(tr_a), &c);
> + if (err)
> + break;
> + if (val_a >= bitmap_len || neg) {
> + err = -EINVAL;
> + break;
> + }
> +
> + val_b = val_a;
> + if (left) {
> + kbuf++;
> + left--;
> + }
> +
> + if (c == '-') {
> + err = proc_get_long(&kbuf, &left, &val_b,
> + &neg, tr_b, sizeof(tr_b),
> + &c);
> + if (err)
> + break;
> + if (val_b >= bitmap_len || neg ||
> + val_a > val_b) {
> + err = -EINVAL;
> + break;
> + }
> + if (left) {
> + kbuf++;
> + left--;
> + }
> + }
> +
> + while (val_a <= val_b)
> + set_bit(val_a++, tmp_bitmap);
> +
> + first = 0;
> + proc_skip_char(&kbuf, &left, '\n');
> + }
> + free_page(page);
> + } else {
> + unsigned long bit_a, bit_b = 0;
> +
> + while (left) {
> + bit_a = find_next_bit(bitmap, bitmap_len, bit_b);
> + if (bit_a >= bitmap_len)
> + break;
> + bit_b = find_next_zero_bit(bitmap, bitmap_len,
> + bit_a + 1) - 1;
> +
> + if (!first) {
> + err = proc_put_char(&buffer, &left, ',');
> + if (err)
> + break;
> + }
> + err = proc_put_long(&buffer, &left, bit_a, false);
> + if (err)
> + break;
> + if (bit_a != bit_b) {
> + err = proc_put_char(&buffer, &left, '-');
> + if (err)
> + break;
> + err = proc_put_long(&buffer, &left, bit_b, false);
> + if (err)
> + break;
> + }
> +
> + first = 0; bit_b++;
> + }
> + if (!err)
> + err = proc_put_char(&buffer, &left, '\n');
> + }
> +
> + if (!err) {
add the following lines to let "echo 1-10 >>
/proc/..." work as normal.
if (write && *ppos)
bimap_or(bitmap, bitmap, tmp_bitmap,....)
else
> + if (write)
> + memcpy(bitmap, tmp_bitmap,
> + BITS_TO_LONGS(bitmap_len) * sizeof(unsigned long));
> + kfree(tmp_bitmap);
> + *lenp -= left;
> + *ppos += *lenp;
> + return 0;
--
Regards,
Changli Gao(xiaosuo@gmail.com)
next prev parent reply other threads:[~2010-04-30 22:41 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-04-30 8:25 [Patch v9 0/3] net: reserve ports for applications using fixed port numbers Amerigo Wang
2010-04-30 8:25 ` [Patch 1/3] sysctl: refactor integer handling proc code Amerigo Wang
2010-04-30 22:49 ` Changli Gao
2010-05-05 3:02 ` Cong Wang
2010-04-30 8:25 ` [Patch 2/3] sysctl: add proc_do_large_bitmap Amerigo Wang
2010-04-30 22:41 ` Changli Gao [this message]
2010-05-05 3:14 ` Cong Wang
2010-05-05 9:20 ` Cong Wang
2010-04-30 8:25 ` [Patch 3/3] net: reserve ports for applications using fixed port numbers Amerigo Wang
-- strict thread matches above, loose matches on Subject: below --
2010-05-05 10:26 [Patch v10 0/3] " Amerigo Wang
2010-05-05 10:26 ` [Patch 2/3] sysctl: add proc_do_large_bitmap Amerigo Wang
2010-04-12 10:03 [Patch v8 0/3] net: reserve ports for applications using fixed port numbers Amerigo Wang
2010-04-12 10:04 ` [Patch 2/3] sysctl: add proc_do_large_bitmap Amerigo Wang
2010-04-09 10:10 [Patch v7 0/3] net: reserve ports for applications using fixed port numbers Amerigo Wang
2010-04-09 10:11 ` [Patch 2/3] sysctl: add proc_do_large_bitmap Amerigo Wang
2010-04-09 10:33 ` Changli Gao
2010-04-09 12:35 ` Octavian Purdila
2010-04-12 6:32 ` Cong Wang
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=v2y412e6f7f1004301541yb1ede589t8c446966743ca138@mail.gmail.com \
--to=xiaosuo@gmail.com \
--cc=adobriyan@gmail.com \
--cc=amwang@redhat.com \
--cc=davem@davemloft.net \
--cc=ebiederm@xmission.com \
--cc=eric.dumazet@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=nhorman@tuxdriver.com \
--cc=opurdila@ixiacom.com \
--cc=penguin-kernel@i-love.sakura.ne.jp \
/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;
as well as URLs for NNTP newsgroup(s).