Re: Device driver query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Sep 11, 2004 at 08:01:38 -0700, linux lover wrote:
>  i know how to write a character device driver but the same will not work for
> simple file why? that mean if i create a file /root/myfile and want to write
> and read to it periodically from a kernel module it is not working cause i am doing

What you write sounds quite a bit confused.
Do you:
    a) want to create a disk file and write something to it (so it is
       persistently written on disk).
    b) or to create a file for which you forge the contents like for
      a device?

In the first case, you don't need any file_operations stuff, because the
file is handled by the filesystem driver. Your module acts as an
aplication for it. Ie. you just call ->ops->write on the filp.
Note, that doing this is *STRONGLY DISCOURAGED*. Prefered way is to feed
the data to a user-mode helper and have that helper write out the data
for you.

In the second case, you should look at the interface for creating procfs
or sysfs entries. It handles much of what you need to do to create
a "fake" file. Also you should not mess with real filesystem by creating
fake entries in it. You will confuse it and make it fail horribly.

> .....fragment of my code.........
> static struct file_operations myfile_file_operations = {
>     open:       myfile_open,
>     release:    myfile_release,
>     read:       myfile_read,
>     write:      myfile_write,
> };
> int init_module(void)
> {
>     printk("Loading myfile Module\n");
>  ent = filp_open("/root/myfile", O_RDWR,S_IRUSR | S_IWUSR);

So, what does this call return? I'd suspect it for returning NULL, since
the file does not exist and you didn't tell it to create it for you.

>   if ( ent == NULL)
>                       printk(KERN_DEBUG, "Error opening file...\n");
>     return 0;
> }
> void cleanup_module(void)
> {
>     printk("Unloading myfile Module\n");
>     filp_close(ent,NULL);
> }

Neither of the following two will work. There are things that must be
done in these methods (ie. as I vaguely recall, the file structure is
not completely intialized here).

By replacing these methods under filesystem's hands, you could cause it
to die a horrible death. The only place where you can safely install
your methods are either devices, or files in procfs and sysfs.

Also, the MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT is a bug. There is
a subtle race in using them. You must arrange it so the code that calls
you will call them for you. Procfs and sysfs will do it.

> int myfile_open(struct inode *inodep, struct file *filp)
> {
>     printk("Opening a file....\n");
>     MOD_INC_USE_COUNT;
>     return 0;
> }
> int myfile_release(struct inode *inodep, struct file *filp)
> {
>     MOD_DEC_USE_COUNT;
>     return 0;
> }
> I want whenever module is loaded it should create a file and when it unloaded should 
> remove file. but above code is fail to create a file and also causing problem in unloading
> a module with warning for arg 1 parameter of flip_close.
> help to write a module to read/write a normal file from kernel module.

-------------------------------------------------------------------------------
						 Jan 'Bulb' Hudec <bulb@xxxxxx>

Attachment: signature.asc
Description: Digital signature


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux