xref: /openbmc/linux/fs/9p/xattr.c (revision 12cecbf9)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  * Copyright IBM Corporation, 2010
4  * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
5  */
6 
7 #include <linux/module.h>
8 #include <linux/fs.h>
9 #include <linux/sched.h>
10 #include <linux/uio.h>
11 #include <net/9p/9p.h>
12 #include <net/9p/client.h>
13 
14 #include "fid.h"
15 #include "xattr.h"
16 
17 ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name,
18 			   void *buffer, size_t buffer_size)
19 {
20 	ssize_t retval;
21 	u64 attr_size;
22 	struct p9_fid *attr_fid;
23 	struct kvec kvec = {.iov_base = buffer, .iov_len = buffer_size};
24 	struct iov_iter to;
25 	int err;
26 
27 	iov_iter_kvec(&to, READ, &kvec, 1, buffer_size);
28 
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",
33 			 retval);
34 		return retval;
35 	}
36 	if (attr_size > buffer_size) {
37 		if (!buffer_size) /* request to get the attr_size */
38 			retval = attr_size;
39 		else
40 			retval = -ERANGE;
41 	} else {
42 		iov_iter_truncate(&to, attr_size);
43 		retval = p9_client_read(attr_fid, 0, &to, &err);
44 		if (err)
45 			retval = err;
46 	}
47 	p9_fid_put(attr_fid);
48 	return retval;
49 }
50 
51 
52 /*
53  * v9fs_xattr_get()
54  *
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.
58  *
59  * Returns a negative error number on failure, or the number of bytes
60  * used / required on success.
61  */
62 ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name,
63 		       void *buffer, size_t buffer_size)
64 {
65 	struct p9_fid *fid;
66 	int ret;
67 
68 	p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu\n",
69 		 name, buffer_size);
70 	fid = v9fs_fid_lookup(dentry);
71 	if (IS_ERR(fid))
72 		return PTR_ERR(fid);
73 	ret = v9fs_fid_xattr_get(fid, name, buffer, buffer_size);
74 	p9_fid_put(fid);
75 
76 	return ret;
77 }
78 
79 /*
80  * v9fs_xattr_set()
81  *
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.
88  *
89  * Returns 0, or a negative error number on failure.
90  */
91 int v9fs_xattr_set(struct dentry *dentry, const char *name,
92 		   const void *value, size_t value_len, int flags)
93 {
94 	int ret;
95 	struct p9_fid *fid;
96 
97 	fid  = v9fs_fid_lookup(dentry);
98 	if (IS_ERR(fid))
99 		return PTR_ERR(fid);
100 	ret = v9fs_fid_xattr_set(fid, name, value, value_len, flags);
101 	p9_fid_put(fid);
102 	return ret;
103 }
104 
105 int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
106 		   const void *value, size_t value_len, int flags)
107 {
108 	struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len};
109 	struct iov_iter from;
110 	int retval, err;
111 
112 	iov_iter_kvec(&from, WRITE, &kvec, 1, value_len);
113 
114 	p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
115 		 name, value_len, flags);
116 
117 	/* Clone it */
118 	fid = clone_fid(fid);
119 	if (IS_ERR(fid))
120 		return PTR_ERR(fid);
121 
122 	/*
123 	 * On success fid points to xattr
124 	 */
125 	retval = p9_client_xattrcreate(fid, name, value_len, flags);
126 	if (retval < 0)
127 		p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n",
128 			 retval);
129 	else
130 		p9_client_write(fid, 0, &from, &retval);
131 	err = p9_fid_put(fid);
132 	if (!retval && err)
133 		retval = err;
134 	return retval;
135 }
136 
137 ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
138 {
139 	return v9fs_xattr_get(dentry, NULL, buffer, buffer_size);
140 }
141 
142 static int v9fs_xattr_handler_get(const struct xattr_handler *handler,
143 				  struct dentry *dentry, struct inode *inode,
144 				  const char *name, void *buffer, size_t size)
145 {
146 	const char *full_name = xattr_full_name(handler, name);
147 
148 	return v9fs_xattr_get(dentry, full_name, buffer, size);
149 }
150 
151 static int v9fs_xattr_handler_set(const struct xattr_handler *handler,
152 				  struct user_namespace *mnt_userns,
153 				  struct dentry *dentry, struct inode *inode,
154 				  const char *name, const void *value,
155 				  size_t size, int flags)
156 {
157 	const char *full_name = xattr_full_name(handler, name);
158 
159 	return v9fs_xattr_set(dentry, full_name, value, size, flags);
160 }
161 
162 static struct xattr_handler v9fs_xattr_user_handler = {
163 	.prefix	= XATTR_USER_PREFIX,
164 	.get	= v9fs_xattr_handler_get,
165 	.set	= v9fs_xattr_handler_set,
166 };
167 
168 static struct xattr_handler v9fs_xattr_trusted_handler = {
169 	.prefix	= XATTR_TRUSTED_PREFIX,
170 	.get	= v9fs_xattr_handler_get,
171 	.set	= v9fs_xattr_handler_set,
172 };
173 
174 #ifdef CONFIG_9P_FS_SECURITY
175 static struct xattr_handler v9fs_xattr_security_handler = {
176 	.prefix	= XATTR_SECURITY_PREFIX,
177 	.get	= v9fs_xattr_handler_get,
178 	.set	= v9fs_xattr_handler_set,
179 };
180 #endif
181 
182 const struct xattr_handler *v9fs_xattr_handlers[] = {
183 	&v9fs_xattr_user_handler,
184 	&v9fs_xattr_trusted_handler,
185 #ifdef CONFIG_9P_FS_POSIX_ACL
186 	&v9fs_xattr_acl_access_handler,
187 	&v9fs_xattr_acl_default_handler,
188 #endif
189 #ifdef CONFIG_9P_FS_SECURITY
190 	&v9fs_xattr_security_handler,
191 #endif
192 	NULL
193 };
194