diff mbox

[v3,07/39] fs: char_dev: introduce cd_acquire function to acquire cdev

Message ID 1442307754-13233-8-git-send-email-yangds.fnst@cn.fujitsu.com
State New
Headers show

Commit Message

Yang Dongsheng Sept. 15, 2015, 9:02 a.m. UTC
cd_acquire() works like bd_acquire() to get cdev by an inode,
this commit is a preparation for lookup_cdev().

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 fs/char_dev.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

Comments

Jan Kara Sept. 16, 2015, 8:16 a.m. UTC | #1
On Tue 15-09-15 17:02:02, Dongsheng Yang wrote:
> cd_acquire() works like bd_acquire() to get cdev by an inode,
> this commit is a preparation for lookup_cdev().
> 
> Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>

Two comments below.

> ---
>  fs/char_dev.c | 35 +++++++++++++++++++++++++++++++++++
>  1 file changed, 35 insertions(+)
> 
> diff --git a/fs/char_dev.c b/fs/char_dev.c
> index ea06a3d..e818faa 100644
> --- a/fs/char_dev.c
> +++ b/fs/char_dev.c
> @@ -439,6 +439,41 @@ static int exact_lock(dev_t dev, void *data)
>  	return cdev_get(p) ? 0 : -1;
>  }
>  
> +struct cdev *cd_acquire(struct inode *inode)
> +{
> +	struct cdev *cdev;
> +	struct cdev *new = NULL;
> +
> +	spin_lock(&cdev_lock);
> +	cdev = inode->i_cdev;
> +	if (!cdev) {
> +		struct kobject *kobj;
> +		int idx;

Empty line here please to separate variable declarations from the code.

> +		spin_unlock(&cdev_lock);
> +		kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
> +		if (!kobj) {
> +			cdev = NULL;
> +			goto out;
> +		}
> +		new = container_of(kobj, struct cdev, kobj);
> +		spin_lock(&cdev_lock);
> +		/* Check i_cdev again in case somebody beat us to it while
> +		   we dropped the lock. */
> +		cdev = inode->i_cdev;
> +		if (!cdev) {
> +			inode->i_cdev = cdev = new;
> +			list_add(&inode->i_devices, &cdev->list);
> +		}

I think you need to put 'new' in case you didn't use it, don't you?

> +	}
> +
> +	if (!cdev_get(cdev))
> +		cdev = NULL;
> +	spin_unlock(&cdev_lock);
> +
> +out:
> +	return cdev;
> +}
> +
>  /**
>   * cdev_add() - add a char device to the system
>   * @p: the cdev structure for the device

								Honza
Yang Dongsheng Sept. 17, 2015, 3:30 a.m. UTC | #2
On 09/16/2015 04:16 PM, Jan Kara wrote:
> On Tue 15-09-15 17:02:02, Dongsheng Yang wrote:
>> cd_acquire() works like bd_acquire() to get cdev by an inode,
>> this commit is a preparation for lookup_cdev().
>>
>> Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
>
> Two comments below.
>
>> ---
>>   fs/char_dev.c | 35 +++++++++++++++++++++++++++++++++++
>>   1 file changed, 35 insertions(+)
>>
>> diff --git a/fs/char_dev.c b/fs/char_dev.c
>> index ea06a3d..e818faa 100644
>> --- a/fs/char_dev.c
>> +++ b/fs/char_dev.c
>> @@ -439,6 +439,41 @@ static int exact_lock(dev_t dev, void *data)
>>   	return cdev_get(p) ? 0 : -1;
>>   }
>>
>> +struct cdev *cd_acquire(struct inode *inode)
>> +{
>> +	struct cdev *cdev;
>> +	struct cdev *new = NULL;
>> +
>> +	spin_lock(&cdev_lock);
>> +	cdev = inode->i_cdev;
>> +	if (!cdev) {
>> +		struct kobject *kobj;
>> +		int idx;
>
> Empty line here please to separate variable declarations from the code.

Yes,
>
>> +		spin_unlock(&cdev_lock);
>> +		kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
>> +		if (!kobj) {
>> +			cdev = NULL;
>> +			goto out;
>> +		}
>> +		new = container_of(kobj, struct cdev, kobj);
>> +		spin_lock(&cdev_lock);
>> +		/* Check i_cdev again in case somebody beat us to it while
>> +		   we dropped the lock. */
>> +		cdev = inode->i_cdev;
>> +		if (!cdev) {
>> +			inode->i_cdev = cdev = new;
>> +			list_add(&inode->i_devices, &cdev->list);
>> +		}
>
> I think you need to put 'new' in case you didn't use it, don't you?

Oh, yes, I forgot it. will update it.

Thanx
Yang
>
>> +	}
>> +
>> +	if (!cdev_get(cdev))
>> +		cdev = NULL;
>> +	spin_unlock(&cdev_lock);
>> +
>> +out:
>> +	return cdev;
>> +}
>> +
>>   /**
>>    * cdev_add() - add a char device to the system
>>    * @p: the cdev structure for the device
>
> 								Honza
>

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/fs/char_dev.c b/fs/char_dev.c
index ea06a3d..e818faa 100644
--- a/fs/char_dev.c
+++ b/fs/char_dev.c
@@ -439,6 +439,41 @@  static int exact_lock(dev_t dev, void *data)
 	return cdev_get(p) ? 0 : -1;
 }
 
+struct cdev *cd_acquire(struct inode *inode)
+{
+	struct cdev *cdev;
+	struct cdev *new = NULL;
+
+	spin_lock(&cdev_lock);
+	cdev = inode->i_cdev;
+	if (!cdev) {
+		struct kobject *kobj;
+		int idx;
+		spin_unlock(&cdev_lock);
+		kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
+		if (!kobj) {
+			cdev = NULL;
+			goto out;
+		}
+		new = container_of(kobj, struct cdev, kobj);
+		spin_lock(&cdev_lock);
+		/* Check i_cdev again in case somebody beat us to it while
+		   we dropped the lock. */
+		cdev = inode->i_cdev;
+		if (!cdev) {
+			inode->i_cdev = cdev = new;
+			list_add(&inode->i_devices, &cdev->list);
+		}
+	}
+
+	if (!cdev_get(cdev))
+		cdev = NULL;
+	spin_unlock(&cdev_lock);
+
+out:
+	return cdev;
+}
+
 /**
  * cdev_add() - add a char device to the system
  * @p: the cdev structure for the device