Re: [Patch 2/3] sysctl: add proc_do_large_bitmap

From: Changli Gao
Date: Fri Apr 30 2010 - 18:41:45 EST


On Fri, Apr 30, 2010 at 4:25 PM, Amerigo Wang <amwang@xxxxxxxxxx> wrote:
> From: Octavian Purdila <opurdila@xxxxxxxxxxx>
>
> 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@xxxxxxxxxxx>
> Signed-off-by: WANG Cong <amwang@xxxxxxxxxx>
> Cc: Eric W. Biederman <ebiederm@xxxxxxxxxxxx>
> ---
>
> 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@xxxxxxxxx)
èº{.nÇ+‰·Ÿ®‰­†+%ŠËlzwm…ébëæìr¸›zX§»®w¥Š{ayºÊÚë,j­¢f£¢·hš‹àz¹®w¥¢¸ ¢·¦j:+v‰¨ŠwèjØm¶Ÿÿ¾«‘êçzZ+ƒùšŽŠÝj"ú!¶iO•æ¬z·švØ^¶m§ÿðà nÆàþY&—