1 // SPDX-License-Identifier: LGPL-2.1
3 * Copyright IBM Corporation, 2010
4 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
7 #include <linux/module.h>
9 #include <linux/sched.h>
10 #include <linux/uio.h>
11 #include <net/9p/9p.h>
12 #include <net/9p/client.h>
17 ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name,
18 void *buffer, size_t buffer_size)
22 struct p9_fid *attr_fid;
23 struct kvec kvec = {.iov_base = buffer, .iov_len = buffer_size};
27 iov_iter_kvec(&to, ITER_DEST, &kvec, 1, buffer_size);
29 attr_fid = p9_client_xattrwalk(fid, name, &attr_size);
30 if (IS_ERR(attr_fid)) {
31 retval = PTR_ERR(attr_fid);
32 p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n",
36 if (attr_size > buffer_size) {
37 if (!buffer_size) /* request to get the attr_size */
42 iov_iter_truncate(&to, attr_size);
43 retval = p9_client_read(attr_fid, 0, &to, &err);
55 * Copy an extended attribute into the buffer
56 * provided, or compute the buffer size required.
57 * Buffer is NULL to compute the size of the buffer required.
59 * Returns a negative error number on failure, or the number of bytes
60 * used / required on success.
62 ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name,
63 void *buffer, size_t buffer_size)
68 p9_debug(P9_DEBUG_VFS, "name = '%s' value_len = %zu\n",
70 fid = v9fs_fid_lookup(dentry);
73 ret = v9fs_fid_xattr_get(fid, name, buffer, buffer_size);
82 * Create, replace or remove an extended attribute for this inode. Buffer
83 * is NULL to remove an existing extended attribute, and non-NULL to
84 * either replace an existing extended attribute, or create a new extended
85 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
86 * specify that an extended attribute must exist and must not exist
87 * previous to the call, respectively.
89 * Returns 0, or a negative error number on failure.
91 int v9fs_xattr_set(struct dentry *dentry, const char *name,
92 const void *value, size_t value_len, int flags)
97 fid = v9fs_fid_lookup(dentry);
100 ret = v9fs_fid_xattr_set(fid, name, value, value_len, flags);
105 int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
106 const void *value, size_t value_len, int flags)
108 struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len};
109 struct iov_iter from;
112 iov_iter_kvec(&from, ITER_SOURCE, &kvec, 1, value_len);
114 p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
115 name, value_len, flags);
118 fid = clone_fid(fid);
123 * On success fid points to xattr
125 retval = p9_client_xattrcreate(fid, name, value_len, flags);
127 p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n",
130 p9_client_write(fid, 0, &from, &retval);
131 err = p9_fid_put(fid);
137 ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
139 /* Txattrwalk with an empty string lists xattrs instead */
140 return v9fs_xattr_get(dentry, "", buffer, buffer_size);
143 static int v9fs_xattr_handler_get(const struct xattr_handler *handler,
144 struct dentry *dentry, struct inode *inode,
145 const char *name, void *buffer, size_t size)
147 const char *full_name = xattr_full_name(handler, name);
149 return v9fs_xattr_get(dentry, full_name, buffer, size);
152 static int v9fs_xattr_handler_set(const struct xattr_handler *handler,
153 struct user_namespace *mnt_userns,
154 struct dentry *dentry, struct inode *inode,
155 const char *name, const void *value,
156 size_t size, int flags)
158 const char *full_name = xattr_full_name(handler, name);
160 return v9fs_xattr_set(dentry, full_name, value, size, flags);
163 static struct xattr_handler v9fs_xattr_user_handler = {
164 .prefix = XATTR_USER_PREFIX,
165 .get = v9fs_xattr_handler_get,
166 .set = v9fs_xattr_handler_set,
169 static struct xattr_handler v9fs_xattr_trusted_handler = {
170 .prefix = XATTR_TRUSTED_PREFIX,
171 .get = v9fs_xattr_handler_get,
172 .set = v9fs_xattr_handler_set,
175 #ifdef CONFIG_9P_FS_SECURITY
176 static struct xattr_handler v9fs_xattr_security_handler = {
177 .prefix = XATTR_SECURITY_PREFIX,
178 .get = v9fs_xattr_handler_get,
179 .set = v9fs_xattr_handler_set,
183 const struct xattr_handler *v9fs_xattr_handlers[] = {
184 &v9fs_xattr_user_handler,
185 &v9fs_xattr_trusted_handler,
186 #ifdef CONFIG_9P_FS_POSIX_ACL
187 &v9fs_xattr_acl_access_handler,
188 &v9fs_xattr_acl_default_handler,
190 #ifdef CONFIG_9P_FS_SECURITY
191 &v9fs_xattr_security_handler,