r/linux • • Jan 25 '20

Kernel LDD3 Linux drivee question

/r/linuxdev/comments/etbp45/ldd3_linux_drivee_question/

[removed] — view removed post

2 Upvotes

4 comments sorted by

2

u/Nnarol Jan 25 '20

I do not know what the heck f_ops means, or what any of this is supposed to do, but the code snippet in itself is actually understandable despite the short variable names:

dev->size seems to be most likely to be the size of some device.

f_ops is a pointer to some value which is comparable to dev->size. You even mentioned it is a pointer to type "loff_t", which souds like something something "offset type", probably "long offset type", due to the "l".

But it doesn't really matter. Being comparable to a "device size" alone and the fact that the first "if" has a "goto out" - which seems to be an exit point from something - suggests that it measures size in some way, be it in bytes, blocks, kilobytes, whatever. So if *f_ops already exceeds the device size, we abort this calculation with "count".So the device could be a disk, although it could probably be some other device with a countable, limited resource. I'll assume a disk.

If *f_ops is still below the device size, the next "if" basically rounds down "count" to the space left between *f_ops and the device's size if it would exceed that space.

It is hard for me to guess why any of this is needed though, since I am neither reading the book, nor have the slightest idea of drivers, and have no more context than what you provided. Maybe wherever "count" is used, the space within the device that *f_ops (apparently an offset) covers is unusable for that purpose.

Very naively, I would suspect that this algorithm is used to determine the available space on a device when something is to be stored on it, and *f_ops marks the part of the device that is full, whilst count is the size of the next chunk of data to be added. I'd have to assume that in that case, "*f_ops += count" takes place somewhere next.

It also might be the case that "*f_ops" is the space that is reserved by the filesystem for purposes such as defining the GPT, journaling, etc. and is not increased during regular write operations, but it still needs to be calculated whether the "count"-sized chunk would fit on disk.

1

u/0xdeadbeef0000 Jan 25 '20

Thanks! Well to let you know i am indeed trying to write a char driver that can write and read stuff and yes count is related to storage available to write. And finally it clicks me now.... Count is used to get the storage available after writing once to disk.

2

u/Nnarol Jan 25 '20 edited Jan 25 '20

OK. I'm glad I could accidentally help :D

EDIT:

Well, it was probably a wrong assessment on my part that "count" is the size of the chunk to be stored. That's because I do not know how the driver is supposed to work:

If my assumption had been correct, a partial chunk would have been written to the device if the chunk didn't fit.

The way it actually functions, you can abort a write operation if the next thing to be written does not fit.

The problem with variable names like "count" is that they are too generic, if not constrained within a very small scope so as to make its purpose obvious. From what I see, it could be "available_bytes", "available_sectors", etc.

The problem is usually exacerbated with "goto": you could divide your algorithm into functions that make sense, and in this case just return early. Instead, control flow is done using "goto", and it's just a temporal sequence of statements, where every variable can potentially interact with everything else.

•

u/[deleted] Jan 25 '20

Your post was removed for being a support request or support related question such as which distro to use or application suggestions.

We get a lot of question posts on r/linux but the subreddit is considered a news/discussion sub. Luckily there are multiple communities you can post to for help on GNU/Linux issues 24/7: /r/linuxquestions, /r/linux4noobs, or /r/findmeadistro just to name a few.

You may also post on the "Weekly Questions and Hardware Thread" which is stickied on r/linux on Wednesdays.

Please make your post in /r/linuxquestions or /r/linux4noobs. Looking for a distro? Try r/findmeadistro.

Rule:

This is not a support forum! Head to /r/linuxquestions or /r/linux4noobs for support or help. Looking for a distro? Try r/findmeadistro.