All of lore.kernel.org
 help / color / mirror / Atom feed
* Accessing allocated space in a debugfs file
@ 2020-09-30 17:02 ymdatta
  2020-09-30 17:49 ` Greg KH
  0 siblings, 1 reply; 4+ messages in thread
From: ymdatta @ 2020-09-30 17:02 UTC (permalink / raw)
  To: kernelnewbies

Hello everyone,

I am trying to create a debugfs file for exposing some information to the
userspace. (I am exploring on how to use debugfs)

From the documentation (Documentation/filesystems/debugfs.txt), i came
across a function called

       struct dentry *debugfs_create_file_size(...,loff_t file_size);

This essentially creates a file with an initial size.

I want to write in this file, how should i be accessing the space created
from previous function call.

I have looked through the source code, but i have found very few places
where this function is used. In one of the use cases [0], the memory
equal to the size is allocated in the open file operation, and the
private_data member is pointed to this. (Looking at [0] helps in
understanding this). But from #kernelnewbies channel, i found that
private_data is a pointer for tty/char drivers.

Thanks for the help.

[0]: https://elixir.bootlin.com/linux/latest/source/drivers/usb/gadget/udc/atmel_usba_udc.c#L140

ymdatta.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Accessing allocated space in a debugfs file
  2020-09-30 17:02 Accessing allocated space in a debugfs file ymdatta
@ 2020-09-30 17:49 ` Greg KH
  2020-10-02 11:29   ` ymdatta
  0 siblings, 1 reply; 4+ messages in thread
From: Greg KH @ 2020-09-30 17:49 UTC (permalink / raw)
  To: ymdatta; +Cc: kernelnewbies

On Wed, Sep 30, 2020 at 10:32:05PM +0530, ymdatta wrote:
> Hello everyone,
> 
> I am trying to create a debugfs file for exposing some information to the
> userspace. (I am exploring on how to use debugfs)
> 
> >From the documentation (Documentation/filesystems/debugfs.txt), i came
> across a function called
> 
>        struct dentry *debugfs_create_file_size(...,loff_t file_size);
> 
> This essentially creates a file with an initial size.
> 
> I want to write in this file, how should i be accessing the space created
> from previous function call.

That's not what "size" means here.  "size" just sets the value that you
see if you look at the directory for that debugfs file (or stat() it).

If you don't set a specific "size", it will just show up as 4k
(PAGE_SIZE).

> I have looked through the source code, but i have found very few places
> where this function is used. In one of the use cases [0], the memory
> equal to the size is allocated in the open file operation, and the
> private_data member is pointed to this. (Looking at [0] helps in
> understanding this). But from #kernelnewbies channel, i found that
> private_data is a pointer for tty/char drivers.

debugfs is a virtual filesystem, there is no "backing store" or place to
put your data in it.  It is there so that you can write code that can
handle open/read/write/close to happen on a file, and your code will
provide the data to userspace directly.

The simplest way to create a debugfs file is to just point it at a
variable, and then you can change the variable value in the kernel, and
userspace reading from the file will see whatever the value is at that
point in time.

Does that help?

thanks,

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Accessing allocated space in a debugfs file
  2020-09-30 17:49 ` Greg KH
@ 2020-10-02 11:29   ` ymdatta
  2020-10-02 11:35     ` Greg KH
  0 siblings, 1 reply; 4+ messages in thread
From: ymdatta @ 2020-10-02 11:29 UTC (permalink / raw)
  To: Greg KH; +Cc: kernelnewbies

On 30/09/20 11:19 pm, Greg KH wrote:
> On Wed, Sep 30, 2020 at 10:32:05PM +0530, ymdatta wrote:

>>
>> I want to write in this file, how should i be accessing the space created
>> from previous function call.
> 
> That's not what "size" means here.  "size" just sets the value that you
> see if you look at the directory for that debugfs file (or stat() it).
> 

Didn't realize this. Why do we need this then? What does this 'size' help
in achieving (or) where is this used?

> debugfs is a virtual filesystem, there is no "backing store" or place to
> put your data in it.  It is there so that you can write code that can
> handle open/read/write/close to happen on a file, and your code will
> provide the data to userspace directly.
> 
> The simplest way to create a debugfs file is to just point it at a
> variable, and then you can change the variable value in the kernel, and
> userspace reading from the file will see whatever the value is at that
> point in time.

This clears up a lot of my doubts. Thanks!

ymdatta.


_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Accessing allocated space in a debugfs file
  2020-10-02 11:29   ` ymdatta
@ 2020-10-02 11:35     ` Greg KH
  0 siblings, 0 replies; 4+ messages in thread
From: Greg KH @ 2020-10-02 11:35 UTC (permalink / raw)
  To: ymdatta; +Cc: kernelnewbies

On Fri, Oct 02, 2020 at 04:59:24PM +0530, ymdatta wrote:
> On 30/09/20 11:19 pm, Greg KH wrote:
> > On Wed, Sep 30, 2020 at 10:32:05PM +0530, ymdatta wrote:
> 
> >>
> >> I want to write in this file, how should i be accessing the space created
> >> from previous function call.
> > 
> > That's not what "size" means here.  "size" just sets the value that you
> > see if you look at the directory for that debugfs file (or stat() it).
> > 
> 
> Didn't realize this. Why do we need this then? What does this 'size' help
> in achieving (or) where is this used?

People like to have pretty values visable in userspace for looking at
files before opening them :)

Right now there are only a handful of users of that "option", compared
with the thousands of "normal" debugfs files.  Look at the callers of
that function for why it might make sense for that option being used
here.

good luck!

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-10-02 11:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-30 17:02 Accessing allocated space in a debugfs file ymdatta
2020-09-30 17:49 ` Greg KH
2020-10-02 11:29   ` ymdatta
2020-10-02 11:35     ` Greg KH

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.