How would one go about adding an extended attribute like the command line function setfattr -n user.custom_attrib -v 99 ex1.txt
, but doing it from within the kernel in a custom system call. I've looked at linux/xattrib.h
and I've had no luck trying to set anything from kernel space. Anytime I've used vfs_setxattr(struct dentry *, const char *, const void *, size_t, int);
it reboots the whole VM. In the end I'm trying to add a new integer type as an extended attribute to files and I also will need to retrieve that extended attribute. I need to use the functions allowed within kernel space.
I was able to get the extended attributes working for: vfs_setxattr(struct dentry *, const char *, const void *, size_t, int);
The main problem was the const void *
wanted a char *
to be passed. That code looked something like this:
char * buf = "test\0";
int size = 5; //number of bytes needed for attribute
int flag = 0; //0 allows for replacement or creation of attribute
int err; //gets error code negative error and positive success
err = vfs_setxattr(path_struct.dentry, "user.custom_attrib", buf, size, flag);
I also was able to get vfs_getxattr(struct dentry *, const char *, const void *, size_t);
working as well. The buffer and void *
was once again where I got stuck. I had to allocate a buffer to hold the extended attribute that was being passed. So my code looked something like this:
char buf[1024];
int size_buf = 1024;
int err;
err = vfs_getxattr(path_struct.dentry, "user.custom_attrib",buf, size_buf);
So now buf will hold the value from the specified file from dentry. The error codes are extremely helpful in figuring out what is going on. So is using the command line tools.
To install the command line tool:
sudo apt-get install attr
To set an attribute manually from the command line:
setfattr -n user.custom_attrib -v "test_if working" test.txt
To get an attribute manually from the command line:
getfattr -n user.custom_attrib test.txt
I wasn't able to figure out if you could pass different types like int's into the extended atrributes and my trials caused me to brick the kernel builds numerous times. Hope this helps some people out, or if anyone has any corrections let me know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With