1 #include <klibc/string.h>
3 #include <lunaix/mm/valloc.h>
4 #include <lunaix/process.h>
5 #include <lunaix/syscall.h>
6 #include <lunaix/syscall_utils.h>
9 xattr_new(struct hstr* name)
11 struct v_xattr_entry* entry = valloc(sizeof(*entry));
16 (struct v_xattr_entry){ .name = HHSTR(valloc(VFS_NAME_MAXLEN), 0, 0),
19 hstrcpy(&entry->name, name);
24 xattr_free(struct v_xattr_entry* entry)
26 vfree((void*)entry->name.value);
31 xattr_getcache(struct v_inode* inode, struct hstr* name)
33 struct v_xattr_entry *pos, *n;
34 llist_for_each(pos, n, &inode->xattrs, entries)
36 if (HSTR_EQ(&pos->name, name)) {
45 xattr_addcache(struct v_inode* inode, struct v_xattr_entry* xattr)
47 llist_append(&inode->xattrs, &xattr->entries);
51 xattr_delcache(struct v_inode* inode, struct v_xattr_entry* xattr)
53 llist_delete(&xattr->entries);
57 __vfs_getxattr(struct v_inode* inode,
58 struct v_xattr_entry** xentry,
61 if (!inode->ops->getxattr) {
66 size_t len = strlen(name);
68 if (len > VFS_NAME_MAXLEN) {
72 struct hstr hname = HSTR(name, len);
74 hstr_rehash(&hname, HSTR_FULL_HASH);
76 struct v_xattr_entry* entry = xattr_getcache(inode, &hname);
78 if (!(entry = xattr_new(&hname))) {
83 if (!(errno = inode->ops->getxattr(inode, entry))) {
85 xattr_addcache(inode, entry);
93 __vfs_setxattr(struct v_inode* inode,
98 if (!inode->ops->setxattr || !inode->ops->delxattr) {
103 size_t slen = strlen(name);
105 if (slen > VFS_NAME_MAXLEN) {
109 struct hstr hname = HSTR(name, slen);
111 hstr_rehash(&hname, HSTR_FULL_HASH);
113 struct v_xattr_entry* entry = xattr_getcache(inode, &hname);
115 if (!(entry = xattr_new(&hname))) {
119 xattr_delcache(inode, entry);
122 if ((errno = inode->ops->delxattr(inode, entry))) {
130 if ((errno = inode->ops->setxattr(inode, entry))) {
135 xattr_addcache(inode, entry);
140 __DEFINE_LXSYSCALL4(int,
151 struct v_dnode* dnode;
152 struct v_xattr_entry* xattr;
155 if ((errno = vfs_walk_proc(path, &dnode, NULL, 0))) {
159 if ((errno = __vfs_getxattr(dnode->inode, &xattr, name))) {
163 if (len < xattr->len) {
168 memcpy(value, xattr->value, len);
171 return DO_STATUS(errno);
174 __DEFINE_LXSYSCALL4(int,
185 struct v_dnode* dnode;
186 struct v_xattr_entry* xattr;
189 if ((errno = vfs_walk_proc(path, &dnode, NULL, 0))) {
193 if ((errno = __vfs_setxattr(dnode->inode, name, value, len))) {
198 return DO_STATUS(errno);
201 __DEFINE_LXSYSCALL4(int,
213 struct v_xattr_entry* xattr;
216 if ((errno = vfs_getfd(fd, &fd_s))) {
220 if ((errno = __vfs_getxattr(fd_s->file->inode, &xattr, name))) {
224 if (len < xattr->len) {
229 memcpy(value, xattr->value, len);
232 return DO_STATUS(errno);
235 __DEFINE_LXSYSCALL4(int,
247 struct v_xattr_entry* xattr;
250 if ((errno = vfs_getfd(fd, &fd_s))) {
254 if ((errno = __vfs_setxattr(fd_s->file->inode, name, value, len))) {
259 return DO_STATUS(errno);