Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright IBM Corporation, 2010 |
| 3 | * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2.1 of the GNU Lesser General Public License |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/sched.h> |
Al Viro | 070b365 | 2015-04-01 20:17:51 -0400 | [diff] [blame] | 18 | #include <linux/uio.h> |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 19 | #include <net/9p/9p.h> |
| 20 | #include <net/9p/client.h> |
| 21 | |
| 22 | #include "fid.h" |
| 23 | #include "xattr.h" |
| 24 | |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 25 | ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name, |
| 26 | void *buffer, size_t buffer_size) |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 27 | { |
| 28 | ssize_t retval; |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 29 | u64 attr_size; |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 30 | struct p9_fid *attr_fid; |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 31 | struct kvec kvec = {.iov_base = buffer, .iov_len = buffer_size}; |
| 32 | struct iov_iter to; |
| 33 | int err; |
| 34 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 35 | iov_iter_kvec(&to, READ, &kvec, 1, buffer_size); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 36 | |
| 37 | attr_fid = p9_client_xattrwalk(fid, name, &attr_size); |
| 38 | if (IS_ERR(attr_fid)) { |
| 39 | retval = PTR_ERR(attr_fid); |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 40 | p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n", |
| 41 | retval); |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 42 | return retval; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 43 | } |
| 44 | if (attr_size > buffer_size) { |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 45 | if (!buffer_size) /* request to get the attr_size */ |
| 46 | retval = attr_size; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 47 | else |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 48 | retval = -ERANGE; |
| 49 | } else { |
| 50 | iov_iter_truncate(&to, attr_size); |
| 51 | retval = p9_client_read(attr_fid, 0, &to, &err); |
| 52 | if (err) |
| 53 | retval = err; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 54 | } |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 55 | p9_client_clunk(attr_fid); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 56 | return retval; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 57 | } |
| 58 | |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * v9fs_xattr_get() |
| 62 | * |
| 63 | * Copy an extended attribute into the buffer |
| 64 | * provided, or compute the buffer size required. |
| 65 | * Buffer is NULL to compute the size of the buffer required. |
| 66 | * |
| 67 | * Returns a negative error number on failure, or the number of bytes |
| 68 | * used / required on success. |
| 69 | */ |
| 70 | ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name, |
| 71 | void *buffer, size_t buffer_size) |
| 72 | { |
| 73 | struct p9_fid *fid; |
Jianyong Wu | 6636b6d | 2020-09-23 22:11:46 +0800 | [diff] [blame^] | 74 | int ret; |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 75 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 76 | p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu\n", |
| 77 | name, buffer_size); |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 78 | fid = v9fs_fid_lookup(dentry); |
| 79 | if (IS_ERR(fid)) |
| 80 | return PTR_ERR(fid); |
Jianyong Wu | 6636b6d | 2020-09-23 22:11:46 +0800 | [diff] [blame^] | 81 | ret = v9fs_fid_xattr_get(fid, name, buffer, buffer_size); |
| 82 | p9_client_clunk(fid); |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 83 | |
Jianyong Wu | 6636b6d | 2020-09-23 22:11:46 +0800 | [diff] [blame^] | 84 | return ret; |
Aneesh Kumar K.V | 85ff872 | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 85 | } |
| 86 | |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 87 | /* |
| 88 | * v9fs_xattr_set() |
| 89 | * |
| 90 | * Create, replace or remove an extended attribute for this inode. Buffer |
| 91 | * is NULL to remove an existing extended attribute, and non-NULL to |
| 92 | * either replace an existing extended attribute, or create a new extended |
| 93 | * attribute. The flags XATTR_REPLACE and XATTR_CREATE |
| 94 | * specify that an extended attribute must exist and must not exist |
| 95 | * previous to the call, respectively. |
| 96 | * |
| 97 | * Returns 0, or a negative error number on failure. |
| 98 | */ |
| 99 | int v9fs_xattr_set(struct dentry *dentry, const char *name, |
| 100 | const void *value, size_t value_len, int flags) |
| 101 | { |
Jianyong Wu | 6636b6d | 2020-09-23 22:11:46 +0800 | [diff] [blame^] | 102 | int ret; |
| 103 | struct p9_fid *fid; |
| 104 | |
| 105 | fid = v9fs_fid_lookup(dentry); |
| 106 | if (IS_ERR(fid)) |
| 107 | return PTR_ERR(fid); |
| 108 | ret = v9fs_fid_xattr_set(fid, name, value, value_len, flags); |
| 109 | p9_client_clunk(fid); |
| 110 | return ret; |
Al Viro | 38baba9 | 2013-01-31 12:34:58 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name, |
| 114 | const void *value, size_t value_len, int flags) |
| 115 | { |
Al Viro | 070b365 | 2015-04-01 20:17:51 -0400 | [diff] [blame] | 116 | struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len}; |
| 117 | struct iov_iter from; |
piaojun | 3111784 | 2018-07-25 11:13:16 +0800 | [diff] [blame] | 118 | int retval, err; |
Al Viro | 070b365 | 2015-04-01 20:17:51 -0400 | [diff] [blame] | 119 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 120 | iov_iter_kvec(&from, WRITE, &kvec, 1, value_len); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 121 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 122 | p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n", |
| 123 | name, value_len, flags); |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 124 | |
Al Viro | 38baba9 | 2013-01-31 12:34:58 -0500 | [diff] [blame] | 125 | /* Clone it */ |
Al Viro | 7d50a29 | 2016-08-03 11:12:12 -0400 | [diff] [blame] | 126 | fid = clone_fid(fid); |
Al Viro | 38baba9 | 2013-01-31 12:34:58 -0500 | [diff] [blame] | 127 | if (IS_ERR(fid)) |
| 128 | return PTR_ERR(fid); |
| 129 | |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 130 | /* |
| 131 | * On success fid points to xattr |
| 132 | */ |
| 133 | retval = p9_client_xattrcreate(fid, name, value_len, flags); |
Al Viro | 070b365 | 2015-04-01 20:17:51 -0400 | [diff] [blame] | 134 | if (retval < 0) |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 135 | p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n", |
| 136 | retval); |
Al Viro | 070b365 | 2015-04-01 20:17:51 -0400 | [diff] [blame] | 137 | else |
| 138 | p9_client_write(fid, 0, &from, &retval); |
piaojun | 3111784 | 2018-07-25 11:13:16 +0800 | [diff] [blame] | 139 | err = p9_client_clunk(fid); |
| 140 | if (!retval && err) |
| 141 | retval = err; |
Geyslan G. Bem | bdd5c28 | 2013-10-21 16:47:58 -0300 | [diff] [blame] | 142 | return retval; |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) |
| 146 | { |
| 147 | return v9fs_xattr_get(dentry, NULL, buffer, buffer_size); |
| 148 | } |
| 149 | |
Andreas Gruenbacher | e409de9 | 2015-10-04 19:18:52 +0200 | [diff] [blame] | 150 | static int v9fs_xattr_handler_get(const struct xattr_handler *handler, |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 151 | struct dentry *dentry, struct inode *inode, |
| 152 | const char *name, void *buffer, size_t size) |
Andreas Gruenbacher | e409de9 | 2015-10-04 19:18:52 +0200 | [diff] [blame] | 153 | { |
| 154 | const char *full_name = xattr_full_name(handler, name); |
| 155 | |
Andreas Gruenbacher | e409de9 | 2015-10-04 19:18:52 +0200 | [diff] [blame] | 156 | return v9fs_xattr_get(dentry, full_name, buffer, size); |
| 157 | } |
| 158 | |
| 159 | static int v9fs_xattr_handler_set(const struct xattr_handler *handler, |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 160 | struct dentry *dentry, struct inode *inode, |
| 161 | const char *name, const void *value, |
| 162 | size_t size, int flags) |
Andreas Gruenbacher | e409de9 | 2015-10-04 19:18:52 +0200 | [diff] [blame] | 163 | { |
| 164 | const char *full_name = xattr_full_name(handler, name); |
| 165 | |
Andreas Gruenbacher | e409de9 | 2015-10-04 19:18:52 +0200 | [diff] [blame] | 166 | return v9fs_xattr_set(dentry, full_name, value, size, flags); |
| 167 | } |
| 168 | |
| 169 | static struct xattr_handler v9fs_xattr_user_handler = { |
| 170 | .prefix = XATTR_USER_PREFIX, |
| 171 | .get = v9fs_xattr_handler_get, |
| 172 | .set = v9fs_xattr_handler_set, |
| 173 | }; |
| 174 | |
| 175 | static struct xattr_handler v9fs_xattr_trusted_handler = { |
| 176 | .prefix = XATTR_TRUSTED_PREFIX, |
| 177 | .get = v9fs_xattr_handler_get, |
| 178 | .set = v9fs_xattr_handler_set, |
| 179 | }; |
| 180 | |
| 181 | #ifdef CONFIG_9P_FS_SECURITY |
| 182 | static struct xattr_handler v9fs_xattr_security_handler = { |
| 183 | .prefix = XATTR_SECURITY_PREFIX, |
| 184 | .get = v9fs_xattr_handler_get, |
| 185 | .set = v9fs_xattr_handler_set, |
| 186 | }; |
| 187 | #endif |
| 188 | |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 189 | const struct xattr_handler *v9fs_xattr_handlers[] = { |
| 190 | &v9fs_xattr_user_handler, |
Jim Garlick | d9a7385 | 2013-05-29 12:09:39 -0700 | [diff] [blame] | 191 | &v9fs_xattr_trusted_handler, |
Aneesh Kumar K.V | 7a4566b | 2010-09-28 00:27:39 +0530 | [diff] [blame] | 192 | #ifdef CONFIG_9P_FS_POSIX_ACL |
| 193 | &v9fs_xattr_acl_access_handler, |
| 194 | &v9fs_xattr_acl_default_handler, |
| 195 | #endif |
Jim Garlick | d9a7385 | 2013-05-29 12:09:39 -0700 | [diff] [blame] | 196 | #ifdef CONFIG_9P_FS_SECURITY |
| 197 | &v9fs_xattr_security_handler, |
| 198 | #endif |
Aneesh Kumar K.V | ebf4626 | 2010-05-31 13:22:56 +0530 | [diff] [blame] | 199 | NULL |
| 200 | }; |