EmbLogic's Blog

Module code showing the difference between *loff and filep->f_pos when multiple write() system calls are issued by the application.

 

Difference between loff_t *loff and filep->f_pos:

When an application issues write() system call , chardev_write() function is called because it is mapped to (*write) function pointer.chardev_write() function contains 4 arguments namely:

1. A pointer to the struct file structure.

2.A pointer to the buffer in the user space.

3.Size of the data

4.Used for internal book-keeping.

Initially both filep->f_pos and *loff are 0.Only  *loff is updated with the number of characters successfully written whereas filep->f_pos remains at position 0.Now whenever an application issues another write() call without closing the file , the value of *loff is copied to filep->f_pos.After the file is closed both the filep->f_pos and the *loff are set to 0.
RCS file: writef.c,v
Working file: writef.c
head: 1.40
branch:
locks: strict
root: 1.40
access list:
symbolic names:
keyword substitution: kv
total revisions: 40;    selected revisions: 40
description:
chardev_write() function reside here.
—————————-
revision 1.40    locked by: root;
date: 2014/03/05 14:15:21;  author: root;  state: Exp;  lines: +6 -1
Updated loff while changing the nocsw.
Status : loff is getting updated successfully and is pointing to the current position.
—————————-
revision 1.39
date: 2014/03/05 14:10:54;  author: root;  state: Exp;  lines: +1 -1
Fixed the warning by typecasting loff_t to int.
—————————-
revision 1.38
date: 2014/03/05 14:08:44;  author: root;  state: Exp;  lines: +7 -2
Gave two printk() statements to check the starting values of filep->f_pos and loff.
Checking………….
—————————-
revision 1.37
date: 2014/03/04 18:55:04;  author: root;  state: Exp;  lines: +0 -21
Status : Writing in multiple qsets is successfully implemented.

Issue:The data pointer to the qset array was updated to point to the last qset array.Because of this reason the crash is taking place.
Bug fixed.
—————————-
revision 1.36
date: 2014/03/04 18:43:52;  author: root;  state: Exp;  lines: +18 -4
Input/output crash is taking place.
Debugging…..
—————————-
revision 1.35
date: 2014/03/04 07:36:31;  author: root;  state: Exp;  lines: +3 -1
Logical error fixed.
Issue : flag variable is not set to 1 due to which it is not able to link the scullqsets.
—————————-
revision 1.34
date: 2014/03/04 07:21:30;  author: root;  state: Exp;  lines: +7 -2
Declared a count variable inside create_scullqset() function too to make sure that exactly how many scullqsets are created.
The number of scullqsets are properly created inside the create_scullqset() function,but inside the chardev_write() it is showing 1 only same is the case with qset_arrays.
—————————-
revision 1.33
date: 2014/03/04 07:16:19;  author: root;  state: Exp;  lines: +10 -6
The messages inside two error conditions are changed to make them more meaningful.Also declared a count variable to make sure exactly how many qset arrays are exactly created by the create_qset() function.
—————————-
revision 1.32
date: 2014/03/04 07:10:31;  author: root;  state: Exp;  lines: +5 -1
Kernel crash taking place.The second qset array is not getting created or linked properly.
Debugging….
—————————-
revision 1.31
date: 2014/03/04 07:05:36;  author: root;  state: Exp;  lines: +30 -33
Properly placing the debugging statements to help finding out the error quickly.
—————————-
revision 1.30
date: 2014/03/03 23:19:07;  author: root;  state: Exp;  lines: +33 -2
Logical error fixed.
Issue:Kernel is crashing while running the module code.The problem lies in the check statement where the dereferencing of NULL pointer is taking placing.
—————————-
revision 1.29
date: 2014/03/03 22:58:55;  author: root;  state: Exp;  lines: +5 -2
Declared a pointer variable of type struct Scullqset to verify whether the number of scullqsets , qset arrays and quantums are actually created or not.
—————————-
revision 1.28
date: 2014/03/03 22:22:42;  author: root;  state: Exp;  lines: +11 -1
A check is made to move to the next lscullqset when the loop variable i equals to 7.Also a loop variable j is used to break the loop when the j becomes equal to noq variable.
Checking………….
—————————-
revision 1.27
date: 2014/03/03 22:17:47;  author: root;  state: Exp;  lines: +9 -6
Optimized the code by allocating the exact number of bytes required for each quantum.
—————————-
revision 1.26
date: 2014/03/03 21:58:52;  author: root;  state: Exp;  lines: +34 -10
Modified the create_qset() function to create the required number of qset arrays.
—————————-
revision 1.25
date: 2014/03/03 21:05:42;  author: root;  state: Exp;  lines: +3 -0
Declared a variable lscullqset of type struct Scullqset and it is initialized with fsculldev->scullqset.
This is done inside create_qset().
—————————-
revision 1.24
date: 2014/03/03 20:59:42;  author: root;  state: Exp;  lines: +2 -1
Logical error fixed.
Issue:kernel crash is taking place because the starting address of the lscullqset is not saved inside fsculldev->scullqset due to which while creating qset arrays it is not getting updated lscullqset address from fsculldev->scullqset.
—————————-
revision 1.23
date: 2014/03/03 20:53:32;  author: root;  state: Exp;  lines: +1 -1
While compilation a warning is coming indicating mixed declerations.
fixed the warning by placing all the declerations above the initializations.
—————————-
revision 1.22
date: 2014/03/03 20:52:17;  author: root;  state: Exp;  lines: +15 -0
Linking between lscullqsets is done.
Checking………….
—————————-
revision 1.21
date: 2014/03/03 20:44:14;  author: root;  state: Exp;  lines: +19 -11
Modifying the code so that writing in multiple qset arrays can be performed.
A flag variable is used to keep track on the number of scullqsets and with the help of this variable linking in lscullqset is also done.
—————————-
revision 1.20
date: 2014/03/03 20:38:05;  author: root;  state: Exp;  lines: +3 -0
Declared a structure variable lscullqset of type struct Scullqset.
—————————-
revision 1.19
date: 2014/03/03 19:00:50;  author: root;  state: Exp;  lines: +2 -2
Status : Writing in multiple quantums is successfully done.

Previous Issues:
1.Allocating memory for qset array incorrectly.
2.Wrongly calculating the number of qset arrays.
Such logical errors are identified and corrected successfully.
—————————-
revision 1.18
date: 2014/03/03 18:55:17;  author: root;  state: Exp;  lines: +13 -13
Corrected the argument inside kmalloc().The size of the qset array is calculated by multiplying the qset size with the size of the pointer.
Also modified a message to display how many qset arrays are created.
—————————-
revision 1.17
date: 2014/03/01 21:01:35;  author: root;  state: Exp;  lines: +2 -2
Status : Writing in multiple quantums successfully implemented.
—————————-
revision 1.16
date: 2014/03/01 20:53:57;  author: root;  state: Exp;  lines: +20 -10
Modifying the code to work for writing in more than one quantums.
—————————-
revision 1.15
date: 2014/03/01 11:19:07;  author: root;  state: Exp;  lines: +5 -0
One printk statement is added to test whether the data is actually copied into the kernel buffer or not.
Testing……….
—————————-
revision 1.14
date: 2014/03/01 11:13:18;  author: root;  state: Exp;  lines: +15 -7
The copy_from_user() function is used to copy a block of data from user space into the kernel buffer.copy_from_user() function contains 3 arguments:
1.A destination buffer,
2.A source buffer,and
3.length in bytes.
—————————-
revision 1.13
date: 2014/03/01 11:06:21;  author: root;  state: Exp;  lines: +2 -1
Done memset().
Testing the code…..
—————————-
revision 1.12
date: 2014/03/01 11:01:16;  author: root;  state: Exp;  lines: +54 -5
Implemented the create_quantums() function that will create the required number of quantums.
Kmalloc() is used to allocate memory for each quantum.
Filling the memory allocated using kmalloc() with memset() so that it does not contain any garbage.
—————————-
revision 1.11
date: 2014/03/01 10:39:39;  author: root;  state: Exp;  lines: +1 -0
memset() function is used to fill the memory allocated for the qset array to ”.
—————————-
revision 1.10
date: 2014/03/01 10:36:59;  author: root;  state: Exp;  lines: +14 -4
Using kmalloc() memory is allocated to the required number of qsets.
Checking whether it is working fine or not.
—————————-
revision 1.9
date: 2014/03/01 10:26:27;  author: root;  state: Exp;  lines: +36 -2
Implemented create_scullqset() function.Now checking the number of qsets that needs to created by using the size argument of the chardev_write().After determining the number of qsets that needs to be created create_qset() function is called.
—————————-
revision 1.8
date: 2014/03/01 10:07:44;  author: root;  state: Exp;  lines: +2 -1
memset() function is used to fill the memory allocated using kmalloc() with ” .
—————————-
revision 1.7
date: 2014/03/01 10:04:21;  author: root;  state: Exp;  lines: +10 -0
Allocating memory for 1 scullqset to check whether the create_scullqset() function is working properly or not.
—————————-
revision 1.6
date: 2014/03/01 09:56:22;  author: root;  state: Exp;  lines: +22 -2
After finding out the number of scullqsets,create_scullqset() function is called to create the specified number of scullqsets.
Checking whether the function calling is taking place or not……
—————————-
revision 1.5
date: 2014/03/01 09:47:56;  author: root;  state: Exp;  lines: +19 -0
The size argument that specifies the size of the buffer pointed to by ubuff is used to find the number of scullqsets that needs to be created.
After finding out the exact number of scullqsets that needs to be created,the number of scullqsets are printed.
Creating scullqsets…….
—————————-
revision 1.4
date: 2014/03/01 09:32:34;  author: root;  state: Exp;  lines: +1 -1
Fixed the warning by type-casting size_t to int.
—————————-
revision 1.3
date: 2014/03/01 09:28:31;  author: root;  state: Exp;  lines: +6 -1
Checking whether the ubuff pointer is actually pointing to the buffer in the application.Also checking the size field inside the 3rd argument of chardev_write().
—————————-
revision 1.2
date: 2014/03/01 09:20:00;  author: root;  state: Exp;  lines: +9 -1
Printing the address inside filep->private_data to check whether it matches with the address returned by container_of() macro.
A local variable of type Sculldev is declared which will store the address that is present inside filep->private_data.
—————————-
revision 1.1
date: 2014/02/28 21:33:05;  author: root;  state: Exp;
Initial revision
=============================================================================

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>