blob: a824441b95a2721cb1738630381799c867553b5d [file] [log] [blame]
Dominique Martinet024b7d62021-11-02 22:12:01 +09001// SPDX-License-Identifier: LGPL-2.1
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +05302/*
3 * Copyright IBM Corporation, 2010
4 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +05305 */
6
7#include <linux/module.h>
8#include <linux/fs.h>
9#include <linux/sched.h>
Al Viro070b3652015-04-01 20:17:51 -040010#include <linux/uio.h>
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053011#include <net/9p/9p.h>
12#include <net/9p/client.h>
13
14#include "fid.h"
15#include "xattr.h"
16
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053017ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name,
18 void *buffer, size_t buffer_size)
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053019{
20 ssize_t retval;
Al Viroe1200fe62015-04-01 23:42:28 -040021 u64 attr_size;
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053022 struct p9_fid *attr_fid;
Al Viroe1200fe62015-04-01 23:42:28 -040023 struct kvec kvec = {.iov_base = buffer, .iov_len = buffer_size};
24 struct iov_iter to;
25 int err;
26
David Howellsaa563d72018-10-20 00:57:56 +010027 iov_iter_kvec(&to, READ, &kvec, 1, buffer_size);
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053028
29 attr_fid = p9_client_xattrwalk(fid, name, &attr_size);
30 if (IS_ERR(attr_fid)) {
31 retval = PTR_ERR(attr_fid);
Joe Perches5d385152011-11-28 10:40:46 -080032 p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n",
33 retval);
Al Viroe1200fe62015-04-01 23:42:28 -040034 return retval;
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053035 }
36 if (attr_size > buffer_size) {
Al Viroe1200fe62015-04-01 23:42:28 -040037 if (!buffer_size) /* request to get the attr_size */
38 retval = attr_size;
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053039 else
Al Viroe1200fe62015-04-01 23:42:28 -040040 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;
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053046 }
Al Viroe1200fe62015-04-01 23:42:28 -040047 p9_client_clunk(attr_fid);
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053048 return retval;
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053049}
50
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053051
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 */
62ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name,
63 void *buffer, size_t buffer_size)
64{
65 struct p9_fid *fid;
Jianyong Wu6636b6d2020-09-23 22:11:46 +080066 int ret;
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053067
Joe Perches5d385152011-11-28 10:40:46 -080068 p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu\n",
69 name, buffer_size);
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053070 fid = v9fs_fid_lookup(dentry);
71 if (IS_ERR(fid))
72 return PTR_ERR(fid);
Jianyong Wu6636b6d2020-09-23 22:11:46 +080073 ret = v9fs_fid_xattr_get(fid, name, buffer, buffer_size);
74 p9_client_clunk(fid);
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053075
Jianyong Wu6636b6d2020-09-23 22:11:46 +080076 return ret;
Aneesh Kumar K.V85ff8722010-09-28 00:27:39 +053077}
78
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +053079/*
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 */
91int v9fs_xattr_set(struct dentry *dentry, const char *name,
92 const void *value, size_t value_len, int flags)
93{
Jianyong Wu6636b6d2020-09-23 22:11:46 +080094 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_client_clunk(fid);
102 return ret;
Al Viro38baba92013-01-31 12:34:58 -0500103}
104
105int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
106 const void *value, size_t value_len, int flags)
107{
Al Viro070b3652015-04-01 20:17:51 -0400108 struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len};
109 struct iov_iter from;
piaojun31117842018-07-25 11:13:16 +0800110 int retval, err;
Al Viro070b3652015-04-01 20:17:51 -0400111
David Howellsaa563d72018-10-20 00:57:56 +0100112 iov_iter_kvec(&from, WRITE, &kvec, 1, value_len);
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530113
Joe Perches5d385152011-11-28 10:40:46 -0800114 p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
115 name, value_len, flags);
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530116
Al Viro38baba92013-01-31 12:34:58 -0500117 /* Clone it */
Al Viro7d50a292016-08-03 11:12:12 -0400118 fid = clone_fid(fid);
Al Viro38baba92013-01-31 12:34:58 -0500119 if (IS_ERR(fid))
120 return PTR_ERR(fid);
121
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530122 /*
123 * On success fid points to xattr
124 */
125 retval = p9_client_xattrcreate(fid, name, value_len, flags);
Al Viro070b3652015-04-01 20:17:51 -0400126 if (retval < 0)
Joe Perches5d385152011-11-28 10:40:46 -0800127 p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n",
128 retval);
Al Viro070b3652015-04-01 20:17:51 -0400129 else
130 p9_client_write(fid, 0, &from, &retval);
piaojun31117842018-07-25 11:13:16 +0800131 err = p9_client_clunk(fid);
132 if (!retval && err)
133 retval = err;
Geyslan G. Bembdd5c282013-10-21 16:47:58 -0300134 return retval;
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530135}
136
137ssize_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
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200142static int v9fs_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -0400143 struct dentry *dentry, struct inode *inode,
144 const char *name, void *buffer, size_t size)
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200145{
146 const char *full_name = xattr_full_name(handler, name);
147
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200148 return v9fs_xattr_get(dentry, full_name, buffer, size);
149}
150
151static int v9fs_xattr_handler_set(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +0100152 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -0400153 struct dentry *dentry, struct inode *inode,
154 const char *name, const void *value,
155 size_t size, int flags)
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200156{
157 const char *full_name = xattr_full_name(handler, name);
158
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200159 return v9fs_xattr_set(dentry, full_name, value, size, flags);
160}
161
162static 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
168static 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
175static 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
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530182const struct xattr_handler *v9fs_xattr_handlers[] = {
183 &v9fs_xattr_user_handler,
Jim Garlickd9a73852013-05-29 12:09:39 -0700184 &v9fs_xattr_trusted_handler,
Aneesh Kumar K.V7a4566b2010-09-28 00:27:39 +0530185#ifdef CONFIG_9P_FS_POSIX_ACL
186 &v9fs_xattr_acl_access_handler,
187 &v9fs_xattr_acl_default_handler,
188#endif
Jim Garlickd9a73852013-05-29 12:09:39 -0700189#ifdef CONFIG_9P_FS_SECURITY
190 &v9fs_xattr_security_handler,
191#endif
Aneesh Kumar K.Vebf46262010-05-31 13:22:56 +0530192 NULL
193};