Dominique Martinet | 024b7d6 | 2021-11-02 22:12:01 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1 |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright IBM Corporation, 2010 |
| 4 | * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/sched.h> |
Al Viro | 070b365 | 2015-04-01 20:17:51 -0400 | [diff] [blame] | 10 | #include <linux/uio.h> |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 11 | #include <net/9p/9p.h> |
| 12 | #include <net/9p/client.h> |
| 13 | |
| 14 | #include "fid.h" |
| 15 | #include "xattr.h" |
| 16 | |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 17 | ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name, |
| 18 | void *buffer, size_t buffer_size) |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 19 | { |
| 20 | ssize_t retval; |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 21 | u64 attr_size; |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 22 | struct p9_fid *attr_fid; |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 23 | struct kvec kvec = {.iov_base = buffer, .iov_len = buffer_size}; |
| 24 | struct iov_iter to; |
| 25 | int err; |
| 26 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 27 | iov_iter_kvec(&to, READ, &kvec, 1, buffer_size); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 28 | |
| 29 | attr_fid = p9_client_xattrwalk(fid, name, &attr_size); |
| 30 | if (IS_ERR(attr_fid)) { |
| 31 | retval = PTR_ERR(attr_fid); |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 32 | p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n", |
| 33 | retval); |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 34 | return retval; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 35 | } |
| 36 | if (attr_size > buffer_size) { |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 37 | if (!buffer_size) /* request to get the attr_size */ |
| 38 | retval = attr_size; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 39 | else |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 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; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 46 | } |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 47 | p9_client_clunk(attr_fid); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 48 | return retval; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 49 | } |
| 50 | |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 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; |
Jianyong Wu | 6636b6d | 2020-09-23 22:11:46 +0800 | [diff] [blame] | 66 | int ret; |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 67 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 68 | p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu\n", |
| 69 | name, buffer_size); |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 70 | fid = v9fs_fid_lookup(dentry); |
| 71 | if (IS_ERR(fid)) |
| 72 | return PTR_ERR(fid); |
Jianyong Wu | 6636b6d | 2020-09-23 22:11:46 +0800 | [diff] [blame] | 73 | ret = v9fs_fid_xattr_get(fid, name, buffer, buffer_size); |
| 74 | p9_client_clunk(fid); |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 75 | |
Jianyong Wu | 6636b6d | 2020-09-23 22:11:46 +0800 | [diff] [blame] | 76 | return ret; |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 77 | } |
| 78 | |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 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 | { |
Jianyong Wu | 6636b6d | 2020-09-23 22:11:46 +0800 | [diff] [blame] | 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_client_clunk(fid); |
| 102 | return ret; |
Al Viro | 38baba9 | 2013-01-31 12:34:58 -0500 | [diff] [blame] | 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 | { |
Al Viro | 070b365 | 2015-04-01 20:17:51 -0400 | [diff] [blame] | 108 | struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len}; |
| 109 | struct iov_iter from; |
piaojun | 3111784 | 2018-07-25 11:13:16 +0800 | [diff] [blame] | 110 | int retval, err; |
Al Viro | 070b365 | 2015-04-01 20:17:51 -0400 | [diff] [blame] | 111 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 112 | iov_iter_kvec(&from, WRITE, &kvec, 1, value_len); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 113 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 114 | p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n", |
| 115 | name, value_len, flags); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 116 | |
Al Viro | 38baba9 | 2013-01-31 12:34:58 -0500 | [diff] [blame] | 117 | /* Clone it */ |
Al Viro | 7d50a29 | 2016-08-03 11:12:12 -0400 | [diff] [blame] | 118 | fid = clone_fid(fid); |
Al Viro | 38baba9 | 2013-01-31 12:34:58 -0500 | [diff] [blame] | 119 | if (IS_ERR(fid)) |
| 120 | return PTR_ERR(fid); |
| 121 | |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 122 | /* |
| 123 | * On success fid points to xattr |
| 124 | */ |
| 125 | retval = p9_client_xattrcreate(fid, name, value_len, flags); |
Al Viro | 070b365 | 2015-04-01 20:17:51 -0400 | [diff] [blame] | 126 | if (retval < 0) |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 127 | p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n", |
| 128 | retval); |
Al Viro | 070b365 | 2015-04-01 20:17:51 -0400 | [diff] [blame] | 129 | else |
| 130 | p9_client_write(fid, 0, &from, &retval); |
piaojun | 3111784 | 2018-07-25 11:13:16 +0800 | [diff] [blame] | 131 | err = p9_client_clunk(fid); |
| 132 | if (!retval && err) |
| 133 | retval = err; |
Geyslan G. Bem | bdd5c28 | 2013-10-21 16:47:58 -0300 | [diff] [blame] | 134 | return retval; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 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 | |
Andreas Gruenbacher | e409de9 | 2015-10-04 19:18:52 +0200 | [diff] [blame] | 142 | static int v9fs_xattr_handler_get(const struct xattr_handler *handler, |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 143 | struct dentry *dentry, struct inode *inode, |
| 144 | const char *name, void *buffer, size_t size) |
Andreas Gruenbacher | e409de9 | 2015-10-04 19:18:52 +0200 | [diff] [blame] | 145 | { |
| 146 | const char *full_name = xattr_full_name(handler, name); |
| 147 | |
Andreas Gruenbacher | e409de9 | 2015-10-04 19:18:52 +0200 | [diff] [blame] | 148 | return v9fs_xattr_get(dentry, full_name, buffer, size); |
| 149 | } |
| 150 | |
| 151 | static int v9fs_xattr_handler_set(const struct xattr_handler *handler, |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 152 | struct user_namespace *mnt_userns, |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 153 | struct dentry *dentry, struct inode *inode, |
| 154 | const char *name, const void *value, |
| 155 | size_t size, int flags) |
Andreas Gruenbacher | e409de9 | 2015-10-04 19:18:52 +0200 | [diff] [blame] | 156 | { |
| 157 | const char *full_name = xattr_full_name(handler, name); |
| 158 | |
Andreas Gruenbacher | e409de9 | 2015-10-04 19:18:52 +0200 | [diff] [blame] | 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 | |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 182 | const struct xattr_handler *v9fs_xattr_handlers[] = { |
| 183 | &v9fs_xattr_user_handler, |
Jim Garlick | d9a7385 | 2013-05-29 12:09:39 -0700 | [diff] [blame] | 184 | &v9fs_xattr_trusted_handler, |
Aneesh Kumar K.V | 7a4566b | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 185 | #ifdef CONFIG_9P_FS_POSIX_ACL |
| 186 | &v9fs_xattr_acl_access_handler, |
| 187 | &v9fs_xattr_acl_default_handler, |
| 188 | #endif |
Jim Garlick | d9a7385 | 2013-05-29 12:09:39 -0700 | [diff] [blame] | 189 | #ifdef CONFIG_9P_FS_SECURITY |
| 190 | &v9fs_xattr_security_handler, |
| 191 | #endif |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 192 | NULL |
| 193 | }; |