public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sam Ravnborg <sam@ravnborg.org>
To: Khalid Aziz <khalid.aziz@oracle.com>
Cc: davem@davemloft.net, sparclinux@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] sparc: Add support for seek and shorter read to /dev/mdesc
Date: Wed, 16 Jul 2014 21:04:14 +0200	[thread overview]
Message-ID: <20140716190414.GA6028@ravnborg.org> (raw)
In-Reply-To: <1405519323-3092-1-git-send-email-khalid.aziz@oracle.com>

Hi Kahlid.

On Wed, Jul 16, 2014 at 08:02:03AM -0600, Khalid Aziz wrote:
> /dev/mdesc on Linux does not support reading arbitrary number
> of bytes and seeking while /dev/mdesc on Solaris does. This
> causes tools that work on Solaris to break on Linux. This patch
> adds these two capabilities to /dev/mdesc.
> 
> Signed-off-by: Khalid Aziz <khalid.aziz@oracle.com>
> ---
>  arch/sparc/kernel/mdesc.c | 77 ++++++++++++++++++++++++++++++++++++++++-------
>  1 file changed, 66 insertions(+), 11 deletions(-)
> 
> +/* mdesc_open() - Grab a reference to mdesc_handle when /dev/mdesc is
> + *	opened. Hold this reference until /dev/mdesc is closed to ensure
> + *	mdesc data structure is not released underneath us. Store the
> + *	pointer to mdesc structure in private_data for read and seek to use
> + */
> +static int mdesc_open(struct inode *inode, struct file *file)
>  {
>  	struct mdesc_handle *hp = mdesc_grab();
>  
>  	if (!hp)
>  		return -ENODEV;
>  
> +	file->private_data = hp;
> +	return 0;
> +}

Do we know the open/close always come in pairs?
I assume so - but there is no check fo this (at least on this level).

> +
> +static ssize_t mdesc_read(struct file *file, char __user *buf,
> +			  size_t len, loff_t *offp)
> +{
> +	struct mdesc_handle *hp = file->private_data;
> +	unsigned char *mdesc;
> +	int err, bytes_left;
> +
> +	if (*offp >= hp->handle_size)
> +		return 0;
> +	err = len;
> +	bytes_left = hp->handle_size - *offp;
> +	if (len > bytes_left)
> +		err = bytes_left;
> +	mdesc = (unsigned char *)&hp->mdesc;
> +	mdesc += *offp;
> +	if (copy_to_user(buf, mdesc, err))
>  		err = -EFAULT;
> -	mdesc_release(hp);
> +	else
> +		*offp += err;
> +
> +	return err;
> +}

When reading your code it is confusing to read that err is set to len,
and then maybe later set to an error value or a new len.

See the following refactoring of mdesc_read() that avoids the err local
variable resulting in more readable code.

static ssize_t mdesc_read(struct file *file, char __user *buf,
                       size_t len, loff_t *offp)
{
     struct mdesc_handle *hp = file->private_data;
     unsigned char *mdesc;
     int bytes_left;

     if (*offp >= hp->handle_size)
             return 0;

     bytes_left = hp->handle_size - *offp;
     if (len > bytes_left)
             len = bytes_left;

     mdesc = (unsigned char *)&hp->mdesc;
     mdesc += *offp;
     if (!copy_to_user(buf, mdesc, len)) {
             *offp += len;
	     return len;
     } else {
             return -EFAULT;
     }
}

The above is IMO more readable.
>  
> +static loff_t mdesc_llseek(struct file *file, loff_t offset, int whence)
> +{
> +	struct mdesc_handle *hp;
> +	int err;
> +
> +	switch (whence) {
> +	case SEEK_CUR:
> +		offset += file->f_pos;
> +		break;
> +	case SEEK_SET:
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
> +
> +	err = offset;
> +	hp = file->private_data;
> +	if (offset > hp->handle_size)
> +		err = -EINVAL;
> +	else
> +		file->f_pos = offset;
>  	return err;
>  }
Same story here with err.


	Sam

  reply	other threads:[~2014-07-16 19:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-16 14:02 [PATCH] sparc: Add support for seek and shorter read to /dev/mdesc Khalid Aziz
2014-07-16 19:04 ` Sam Ravnborg [this message]
2014-07-16 20:35   ` Khalid Aziz
2014-07-16 21:00     ` Sam Ravnborg

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=20140716190414.GA6028@ravnborg.org \
    --to=sam@ravnborg.org \
    --cc=davem@davemloft.net \
    --cc=khalid.aziz@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sparclinux@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