Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Need help to build a module: insert or delete from a file.

Hi All,
I am very much new in C with Linux.I am struggling to create a Linux kernel module (in vanilla 2.6.26.3 Linux) that, when loaded into Linux, will support a new system call called

sys_insert(const char __user *file, loff_t offset,
const char __user *buf, size_t count,
int flags)

- is the name of the file you want to insert or delete in
- is the offset where you start the insertion/deletion
- is the user buffer containing the data to be inserted/deleted
- is the length of the data to insert/delete from the offset point
- can be one of these two

#define OPTION_INSERT 1 /* insert data at offset */
#define OPTION_DELETE 2 /* delete data from offset */

The system call should return an error number on error (return -1 and set errno), or a positive integer denoting the number of bytes successfully inserted/deleted.

The most important thing system calls do first is ensure the validity of the input they are given. You must check for ALL possible bad conditions that could occur as the result of bad inputs to the system call. In that case, the system call should return the proper errno alue (EINVAL, EPERM, EACCES, etc.) Consult the system errno table and pick the right error numbers for different conditions.

The kinds of errors that could occur early during the system call's
execution are as follows (this is a non-exhaustive list):

- file doesn't exist, not a regular file, no permission
- offset is invalid or past EOF
- buffer is NULL or spans an invalid address space
- count is invalid
- flags are not one of the two allowed flags
- buf is shorter than count
- trying to delete more data than is available in the file (actually, that would NOT be an error, just return however many bytes of data you need).
- ANYTHING else you can think of (the more error checking you do, the better)

If the system call was invoked with OPTION_DELETE as the last argument, then the argument must be equal to NULL. Otherwise the input must be considered as invalid.

After checking for errors, you should open the input file. Then, if you have to delete part of the file, copy the file's data past the deletion point to override the section of the file being deleted. Then truncate the file to its new size. If you have to insert into the file, then copy the file's data at the insertion point outward, extending the file's size; then copy the new data into the new gap you've created. Hints: use filp_open, filp_close, vfs_read, vfs_write, and notify_change (and possibly others).

Please help me.

- Nancy


Sign In or Register to comment.