| 1 |
// SPDX-License-Identifier: LGPL-2.1 |
| 2 |
/* |
| 3 |
* Copyright IBM Corporation, 2010 |
| 4 |
* Author Aneesh Kumar K.V <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/module.h> |
| 8 |
#include <linux/fs.h> |
| 9 |
#include <linux/fs_struct.h> |
| 10 |
#include <net/9p/9p.h> |
| 11 |
#include <net/9p/client.h> |
| 12 |
#include <linux/slab.h> |
| 13 |
#include <linux/sched.h> |
| 14 |
#include <linux/posix_acl_xattr.h> |
| 15 |
#include "xattr.h" |
| 16 |
#include "acl.h" |
| 17 |
#include "v9fs.h" |
| 18 |
#include "v9fs_vfs.h" |
| 19 |
#include "fid.h" |
| 20 |
|
| 21 |
static struct posix_acl *v9fs_fid_get_acl(struct p9_fid *fid, const char *name) |
| 22 |
{ |
| 23 |
ssize_t size; |
| 24 |
void *value = NULL; |
| 25 |
struct posix_acl *acl = NULL; |
| 26 |
|
| 27 |
size = v9fs_fid_xattr_get(fid, name, NULL, 0); |
| 28 |
if (size < 0) |
| 29 |
return ERR_PTR(size); |
| 30 |
if (size == 0) |
| 31 |
return ERR_PTR(-ENODATA); |
| 32 |
|
| 33 |
value = kzalloc(size, GFP_NOFS); |
| 34 |
if (!value) |
| 35 |
return ERR_PTR(-ENOMEM); |
| 36 |
|
| 37 |
size = v9fs_fid_xattr_get(fid, name, value, size); |
| 38 |
if (size < 0) |
| 39 |
acl = ERR_PTR(size); |
| 40 |
else if (size == 0) |
| 41 |
acl = ERR_PTR(-ENODATA); |
| 42 |
else |
| 43 |
acl = posix_acl_from_xattr(&init_user_ns, value, size); |
| 44 |
kfree(value); |
| 45 |
return acl; |
| 46 |
} |
| 47 |
|
| 48 |
static struct posix_acl *v9fs_acl_get(struct dentry *dentry, const char *name) |
| 49 |
{ |
| 50 |
struct p9_fid *fid; |
| 51 |
struct posix_acl *acl = NULL; |
| 52 |
|
| 53 |
fid = v9fs_fid_lookup(dentry); |
| 54 |
if (IS_ERR(fid)) |
| 55 |
return ERR_CAST(fid); |
| 56 |
|
| 57 |
acl = v9fs_fid_get_acl(fid, name); |
| 58 |
p9_fid_put(fid); |
| 59 |
return acl; |
| 60 |
} |
| 61 |
|
| 62 |
static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, const char *name) |
| 63 |
{ |
| 64 |
int retval; |
| 65 |
struct posix_acl *acl = NULL; |
| 66 |
|
| 67 |
acl = v9fs_fid_get_acl(fid, name); |
| 68 |
if (!IS_ERR(acl)) |
| 69 |
return acl; |
| 70 |
|
| 71 |
retval = PTR_ERR(acl); |
| 72 |
if (retval == -ENODATA || retval == -ENOSYS || retval == -EOPNOTSUPP) |
| 73 |
return NULL; |
| 74 |
|
| 75 |
/* map everything else to -EIO */ |
| 76 |
return ERR_PTR(-EIO); |
| 77 |
} |
| 78 |
|
| 79 |
int v9fs_get_acl(struct inode *inode, struct p9_fid *fid) |
| 80 |
{ |
| 81 |
int retval = 0; |
| 82 |
struct posix_acl *pacl, *dacl; |
| 83 |
struct v9fs_session_info *v9ses; |
| 84 |
|
| 85 |
v9ses = v9fs_inode2v9ses(inode); |
| 86 |
if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) || |
| 87 |
((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) { |
| 88 |
set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL); |
| 89 |
set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); |
| 90 |
return 0; |
| 91 |
} |
| 92 |
/* get the default/access acl values and cache them */ |
| 93 |
dacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_DEFAULT); |
| 94 |
pacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_ACCESS); |
| 95 |
|
| 96 |
if (!IS_ERR(dacl) && !IS_ERR(pacl)) { |
| 97 |
set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl); |
| 98 |
set_cached_acl(inode, ACL_TYPE_ACCESS, pacl); |
| 99 |
} else |
| 100 |
retval = -EIO; |
| 101 |
|
| 102 |
if (!IS_ERR(dacl)) |
| 103 |
posix_acl_release(dacl); |
| 104 |
|
| 105 |
if (!IS_ERR(pacl)) |
| 106 |
posix_acl_release(pacl); |
| 107 |
|
| 108 |
return retval; |
| 109 |
} |
| 110 |
|
| 111 |
static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type) |
| 112 |
{ |
| 113 |
struct posix_acl *acl; |
| 114 |
/* |
| 115 |
* 9p Always cache the acl value when |
| 116 |
* instantiating the inode (v9fs_inode_from_fid) |
| 117 |
*/ |
| 118 |
acl = get_cached_acl(inode, type); |
| 119 |
BUG_ON(is_uncached_acl(acl)); |
| 120 |
return acl; |
| 121 |
} |
| 122 |
|
| 123 |
struct posix_acl *v9fs_iop_get_inode_acl(struct inode *inode, int type, bool rcu) |
| 124 |
{ |
| 125 |
struct v9fs_session_info *v9ses; |
| 126 |
|
| 127 |
if (rcu) |
| 128 |
return ERR_PTR(-ECHILD); |
| 129 |
|
| 130 |
v9ses = v9fs_inode2v9ses(inode); |
| 131 |
if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) || |
| 132 |
((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) { |
| 133 |
/* |
| 134 |
* On access = client and acl = on mode get the acl |
| 135 |
* values from the server |
| 136 |
*/ |
| 137 |
return NULL; |
| 138 |
} |
| 139 |
return v9fs_get_cached_acl(inode, type); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
struct posix_acl *v9fs_iop_get_acl(struct mnt_idmap *idmap, |
| 144 |
struct dentry *dentry, int type) |
| 145 |
{ |
| 146 |
struct v9fs_session_info *v9ses; |
| 147 |
|
| 148 |
v9ses = v9fs_dentry2v9ses(dentry); |
| 149 |
/* We allow set/get/list of acl when access=client is not specified. */ |
| 150 |
if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) |
| 151 |
return v9fs_acl_get(dentry, posix_acl_xattr_name(type)); |
| 152 |
return v9fs_get_cached_acl(d_inode(dentry), type); |
| 153 |
} |
| 154 |
|
| 155 |
int v9fs_iop_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, |
| 156 |
struct posix_acl *acl, int type) |
| 157 |
{ |
| 158 |
int retval; |
| 159 |
size_t size = 0; |
| 160 |
void *value = NULL; |
| 161 |
const char *acl_name; |
| 162 |
struct v9fs_session_info *v9ses; |
| 163 |
struct inode *inode = d_inode(dentry); |
| 164 |
|
| 165 |
if (acl) { |
| 166 |
retval = posix_acl_valid(inode->i_sb->s_user_ns, acl); |
| 167 |
if (retval) |
| 168 |
goto err_out; |
| 169 |
|
| 170 |
value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS); |
| 171 |
if (!value) { |
| 172 |
retval = -ENOMEM; |
| 173 |
goto err_out; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/* |
| 178 |
* set the attribute on the remote. Without even looking at the |
| 179 |
* xattr value. We leave it to the server to validate |
| 180 |
*/ |
| 181 |
acl_name = posix_acl_xattr_name(type); |
| 182 |
v9ses = v9fs_dentry2v9ses(dentry); |
| 183 |
if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) { |
| 184 |
retval = v9fs_xattr_set(dentry, acl_name, value, size, 0); |
| 185 |
goto err_out; |
| 186 |
} |
| 187 |
|
| 188 |
if (S_ISLNK(inode->i_mode)) { |
| 189 |
retval = -EOPNOTSUPP; |
| 190 |
goto err_out; |
| 191 |
} |
| 192 |
|
| 193 |
if (!inode_owner_or_capable(&nop_mnt_idmap, inode)) { |
| 194 |
retval = -EPERM; |
| 195 |
goto err_out; |
| 196 |
} |
| 197 |
|
| 198 |
switch (type) { |
| 199 |
case ACL_TYPE_ACCESS: |
| 200 |
if (acl) { |
| 201 |
struct iattr iattr = {}; |
| 202 |
struct posix_acl *acl_mode = acl; |
| 203 |
|
| 204 |
retval = posix_acl_update_mode(&nop_mnt_idmap, inode, |
| 205 |
&iattr.ia_mode, |
| 206 |
&acl_mode); |
| 207 |
if (retval) |
| 208 |
goto err_out; |
| 209 |
if (!acl_mode) { |
| 210 |
/* |
| 211 |
* ACL can be represented by the mode bits. |
| 212 |
* So don't update ACL below. |
| 213 |
*/ |
| 214 |
kfree(value); |
| 215 |
value = NULL; |
| 216 |
size = 0; |
| 217 |
} |
| 218 |
iattr.ia_valid = ATTR_MODE; |
| 219 |
/* |
| 220 |
* FIXME should we update ctime ? |
| 221 |
* What is the following setxattr update the mode ? |
| 222 |
*/ |
| 223 |
v9fs_vfs_setattr_dotl(&nop_mnt_idmap, dentry, &iattr); |
| 224 |
} |
| 225 |
break; |
| 226 |
case ACL_TYPE_DEFAULT: |
| 227 |
if (!S_ISDIR(inode->i_mode)) { |
| 228 |
retval = acl ? -EINVAL : 0; |
| 229 |
goto err_out; |
| 230 |
} |
| 231 |
break; |
| 232 |
} |
| 233 |
|
| 234 |
retval = v9fs_xattr_set(dentry, acl_name, value, size, 0); |
| 235 |
if (!retval) |
| 236 |
set_cached_acl(inode, type, acl); |
| 237 |
|
| 238 |
err_out: |
| 239 |
kfree(value); |
| 240 |
return retval; |
| 241 |
} |
| 242 |
|
| 243 |
static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl) |
| 244 |
{ |
| 245 |
int retval; |
| 246 |
char *name; |
| 247 |
size_t size; |
| 248 |
void *buffer; |
| 249 |
|
| 250 |
if (!acl) |
| 251 |
return 0; |
| 252 |
|
| 253 |
/* Set a setxattr request to server */ |
| 254 |
buffer = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_KERNEL); |
| 255 |
if (!buffer) |
| 256 |
return -ENOMEM; |
| 257 |
|
| 258 |
switch (type) { |
| 259 |
case ACL_TYPE_ACCESS: |
| 260 |
name = XATTR_NAME_POSIX_ACL_ACCESS; |
| 261 |
break; |
| 262 |
case ACL_TYPE_DEFAULT: |
| 263 |
name = XATTR_NAME_POSIX_ACL_DEFAULT; |
| 264 |
break; |
| 265 |
default: |
| 266 |
BUG(); |
| 267 |
} |
| 268 |
retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0); |
| 269 |
kfree(buffer); |
| 270 |
return retval; |
| 271 |
} |
| 272 |
|
| 273 |
int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid) |
| 274 |
{ |
| 275 |
int retval = 0; |
| 276 |
struct posix_acl *acl; |
| 277 |
|
| 278 |
if (S_ISLNK(inode->i_mode)) |
| 279 |
return -EOPNOTSUPP; |
| 280 |
acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS); |
| 281 |
if (acl) { |
| 282 |
retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode); |
| 283 |
if (retval) |
| 284 |
return retval; |
| 285 |
set_cached_acl(inode, ACL_TYPE_ACCESS, acl); |
| 286 |
retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl); |
| 287 |
posix_acl_release(acl); |
| 288 |
} |
| 289 |
return retval; |
| 290 |
} |
| 291 |
|
| 292 |
int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid, |
| 293 |
struct posix_acl *dacl, struct posix_acl *acl) |
| 294 |
{ |
| 295 |
set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl); |
| 296 |
set_cached_acl(inode, ACL_TYPE_ACCESS, acl); |
| 297 |
v9fs_set_acl(fid, ACL_TYPE_DEFAULT, dacl); |
| 298 |
v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl); |
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
void v9fs_put_acl(struct posix_acl *dacl, |
| 303 |
struct posix_acl *acl) |
| 304 |
{ |
| 305 |
posix_acl_release(dacl); |
| 306 |
posix_acl_release(acl); |
| 307 |
} |
| 308 |
|
| 309 |
int v9fs_acl_mode(struct inode *dir, umode_t *modep, |
| 310 |
struct posix_acl **dpacl, struct posix_acl **pacl) |
| 311 |
{ |
| 312 |
int retval = 0; |
| 313 |
umode_t mode = *modep; |
| 314 |
struct posix_acl *acl = NULL; |
| 315 |
|
| 316 |
if (!S_ISLNK(mode)) { |
| 317 |
acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT); |
| 318 |
if (IS_ERR(acl)) |
| 319 |
return PTR_ERR(acl); |
| 320 |
if (!acl) |
| 321 |
mode &= ~current_umask(); |
| 322 |
} |
| 323 |
if (acl) { |
| 324 |
if (S_ISDIR(mode)) |
| 325 |
*dpacl = posix_acl_dup(acl); |
| 326 |
retval = __posix_acl_create(&acl, GFP_NOFS, &mode); |
| 327 |
if (retval < 0) |
| 328 |
return retval; |
| 329 |
if (retval > 0) |
| 330 |
*pacl = acl; |
| 331 |
else |
| 332 |
posix_acl_release(acl); |
| 333 |
} |
| 334 |
*modep = mode; |
| 335 |
return 0; |
| 336 |
} |