views:

303

answers:

3

I was assigned to write a system call for Linux kernel, which oddly determines (and reduces) users´ maximum transfer amount per minute (for file operations). This system call will be called lim_fs_usage and will take a parameter for maximum number of bytes all users can access in a minute. For short, I am going to determine bandwidth of all filesystem operations in Linux. The project also asks for choosing appropriate method for distribution of this restricted resource (file access) among the users but I think this won´t be a big problem.

I did a long long search and scan but could not find a method for managing file system access programmatically. I thought of mapping (mmap())hard drive to memory and manage memory operations but this turned to be useless. I also tried to find an API for virtual file system in order to monitor and limit it but I could not find one. Any ideas, please... Any help is greatly appreciated. Thank you in advance...

A: 

Since it's urgent I'll give you an idea out of the top of my head without doing any research on the feasibility -- what about inserting a hook to monitor system calls that deal with file system access? You might end up writing specialised kernel modules to handle the various filesystems (ext3, ext4, etc) but as a proof-of-concept you can start with one. Do not forget that root has reserved blocks in memory, process space and disk for his own operations.

Managing memory operations does not sound related to what you're trying to do (but perhaps I am mistaken here).

lorenzog
Good idea, and thank you but I have questions about applicability of this idea. This is because there should be a huge number of system calls that do an operation on filesystem. If I try to modify all of them... This might mean reshaping the whole kernel... But maybe I am wrong, I´ll search about it, thanks...
Ersan Tasan
There are very few system calls - full stop. The two main ones you would have to worry about would be read() and write(). Don't confuse system calls with the thousands of APIs that Linux supports.
anon
neil: you're right. I meant doing like a some kernel rootkit do, ie intercept library calls. Thanks.
lorenzog
+1  A: 

I wonder if you could do this as an IO scheduler implementation.

The main difficulty of doing IO bandwidth limitation under Linux is, by the time it reaches anywhere near the device, the kernel has probably long since forgotten who caused it.

Likewise, you can get on some very tricky ground in determining who is responsible for a given piece of IO:

  • If a binary is demand-loaded, who owns the IO doing that?
  • A mapped section of memory (demand-loaded executable or otherwise) might be kicked out of memory because someone else used too much ram, thus causing the kernel to choose to evict those pages, which places an unfair burden on the quota of the other user to then page it back in
  • IO operations can be combined, and might come from different users
  • A write operation might cause an IO sooner or later depending on how the kernel schedules it; a later schedule may mean that fewer IOs need to be done in the long run, as another write gets done to the same block in the interim; writing to an already dirty block in cache does not make it any dirtier.

If you understand all these and more caveats, and still want to, I imagine doing it as an IO scheduler is the way to go.

IO schedulers are pluggable under Linux (2.6) and can be changed dynamically - the kernel waits for all IO on the device (IO scheduler is switchable per block device) to end and then switches to the new one.

MarkR
Thanks for the answer but I suppose this is not what I need. I do not need to manage ALL i/o operations - just reading and writing to hard drive. A program may need an input and a user gives it via keyboard- I don`t limit this. The solution is probably modifying Read() and Write() system calls, which take care of hdd operations.
Ersan Tasan
read / write are not the only ways to cause IO to happen into the hard drive, nor do they necessarily (both can use the cache). I had assumed you wanted to throttle the rate at which IO requests actually reach the device; this needs to be done at a lower level than read/write calls.
MarkR
An application could trivially get around that (limiting `read`/`write` bandwidth) by using `mmap` to read and write files instead.
caf
A: 

After a long period of thinking and searching, I decided to use the ¨hooking¨ method proposed. I am thinking of creating a new system call which initializes and manages a global variable like hdd_ bandwith _limit. This variable will be used in Read() and Write() system calls´ modified implementation (instead of ¨count¨ variable). Then I will decide distribution of this resource which is the real issue. Probably I will find out how many users are using the system for a certain moment and divide this resource equally. Will be a Round-Robin-like distribution. But still, I am open to suggestions on this distribution issue. Will it be a SJF or FCFS or Round-Robin? Synchronization is another issue. How can I know a user´s job is short or long? Or whether he is done with the operation or not?

Ersan Tasan
You don't need to implement a system call to manage a single variable - just implement a `sysctl` tuneable (gettable and settable from userspace through `/proc/sys/kernel/bandwidth_limit`). The interface for adding these is pretty simple.
caf