1f2cde895SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 27221fe4cSGuangliang Zhao /* 37221fe4cSGuangliang Zhao * linux/fs/ceph/acl.c 47221fe4cSGuangliang Zhao * 57221fe4cSGuangliang Zhao * Copyright (C) 2013 Guangliang Zhao, <lucienchao@gmail.com> 67221fe4cSGuangliang Zhao */ 77221fe4cSGuangliang Zhao 87221fe4cSGuangliang Zhao #include <linux/ceph/ceph_debug.h> 97221fe4cSGuangliang Zhao #include <linux/fs.h> 107221fe4cSGuangliang Zhao #include <linux/string.h> 117221fe4cSGuangliang Zhao #include <linux/xattr.h> 127221fe4cSGuangliang Zhao #include <linux/posix_acl_xattr.h> 137221fe4cSGuangliang Zhao #include <linux/posix_acl.h> 147221fe4cSGuangliang Zhao #include <linux/sched.h> 157221fe4cSGuangliang Zhao #include <linux/slab.h> 167221fe4cSGuangliang Zhao 177221fe4cSGuangliang Zhao #include "super.h" 187221fe4cSGuangliang Zhao 197221fe4cSGuangliang Zhao static inline void ceph_set_cached_acl(struct inode *inode, 207221fe4cSGuangliang Zhao int type, struct posix_acl *acl) 217221fe4cSGuangliang Zhao { 227221fe4cSGuangliang Zhao struct ceph_inode_info *ci = ceph_inode(inode); 237221fe4cSGuangliang Zhao 247221fe4cSGuangliang Zhao spin_lock(&ci->i_ceph_lock); 251af16d54SXiubo Li if (__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 0)) 267221fe4cSGuangliang Zhao set_cached_acl(inode, type, acl); 27b8a7a3a6SAndreas Gruenbacher else 28b8a7a3a6SAndreas Gruenbacher forget_cached_acl(inode, type); 297221fe4cSGuangliang Zhao spin_unlock(&ci->i_ceph_lock); 307221fe4cSGuangliang Zhao } 317221fe4cSGuangliang Zhao 32*0cad6246SMiklos Szeredi struct posix_acl *ceph_get_acl(struct inode *inode, int type, bool rcu) 337221fe4cSGuangliang Zhao { 347221fe4cSGuangliang Zhao int size; 35f017754dSChengguang Xu unsigned int retry_cnt = 0; 367221fe4cSGuangliang Zhao const char *name; 377221fe4cSGuangliang Zhao char *value = NULL; 387221fe4cSGuangliang Zhao struct posix_acl *acl; 397221fe4cSGuangliang Zhao 40*0cad6246SMiklos Szeredi if (rcu) 41*0cad6246SMiklos Szeredi return ERR_PTR(-ECHILD); 42*0cad6246SMiklos Szeredi 437221fe4cSGuangliang Zhao switch (type) { 447221fe4cSGuangliang Zhao case ACL_TYPE_ACCESS: 4597d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_ACCESS; 467221fe4cSGuangliang Zhao break; 477221fe4cSGuangliang Zhao case ACL_TYPE_DEFAULT: 4897d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_DEFAULT; 497221fe4cSGuangliang Zhao break; 507221fe4cSGuangliang Zhao default: 517221fe4cSGuangliang Zhao BUG(); 527221fe4cSGuangliang Zhao } 537221fe4cSGuangliang Zhao 54f017754dSChengguang Xu retry: 557221fe4cSGuangliang Zhao size = __ceph_getxattr(inode, name, "", 0); 567221fe4cSGuangliang Zhao if (size > 0) { 577221fe4cSGuangliang Zhao value = kzalloc(size, GFP_NOFS); 587221fe4cSGuangliang Zhao if (!value) 597221fe4cSGuangliang Zhao return ERR_PTR(-ENOMEM); 607221fe4cSGuangliang Zhao size = __ceph_getxattr(inode, name, value, size); 617221fe4cSGuangliang Zhao } 627221fe4cSGuangliang Zhao 63f017754dSChengguang Xu if (size == -ERANGE && retry_cnt < 10) { 64f017754dSChengguang Xu retry_cnt++; 65f017754dSChengguang Xu kfree(value); 66f017754dSChengguang Xu value = NULL; 67f017754dSChengguang Xu goto retry; 68f017754dSChengguang Xu } 69f017754dSChengguang Xu 70f017754dSChengguang Xu if (size > 0) { 717221fe4cSGuangliang Zhao acl = posix_acl_from_xattr(&init_user_ns, value, size); 72f017754dSChengguang Xu } else if (size == -ENODATA || size == 0) { 737221fe4cSGuangliang Zhao acl = NULL; 74f017754dSChengguang Xu } else { 75f017754dSChengguang Xu pr_err_ratelimited("get acl %llx.%llx failed, err=%d\n", 76f017754dSChengguang Xu ceph_vinop(inode), size); 777221fe4cSGuangliang Zhao acl = ERR_PTR(-EIO); 78f017754dSChengguang Xu } 797221fe4cSGuangliang Zhao 807221fe4cSGuangliang Zhao kfree(value); 817221fe4cSGuangliang Zhao 827221fe4cSGuangliang Zhao if (!IS_ERR(acl)) 837221fe4cSGuangliang Zhao ceph_set_cached_acl(inode, type, acl); 847221fe4cSGuangliang Zhao 857221fe4cSGuangliang Zhao return acl; 867221fe4cSGuangliang Zhao } 877221fe4cSGuangliang Zhao 88549c7297SChristian Brauner int ceph_set_acl(struct user_namespace *mnt_userns, struct inode *inode, 89549c7297SChristian Brauner struct posix_acl *acl, int type) 907221fe4cSGuangliang Zhao { 917221fe4cSGuangliang Zhao int ret = 0, size = 0; 927221fe4cSGuangliang Zhao const char *name = NULL; 937221fe4cSGuangliang Zhao char *value = NULL; 947221fe4cSGuangliang Zhao struct iattr newattrs; 9593d35c75SChengguang Xu struct timespec64 old_ctime = inode->i_ctime; 967221fe4cSGuangliang Zhao umode_t new_mode = inode->i_mode, old_mode = inode->i_mode; 977221fe4cSGuangliang Zhao 985da20799SChengguang Xu if (ceph_snap(inode) != CEPH_NOSNAP) { 995da20799SChengguang Xu ret = -EROFS; 1005da20799SChengguang Xu goto out; 1015da20799SChengguang Xu } 1025da20799SChengguang Xu 1037221fe4cSGuangliang Zhao switch (type) { 1047221fe4cSGuangliang Zhao case ACL_TYPE_ACCESS: 10597d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_ACCESS; 1067221fe4cSGuangliang Zhao if (acl) { 107e65ce2a5SChristian Brauner ret = posix_acl_update_mode(&init_user_ns, inode, 108e65ce2a5SChristian Brauner &new_mode, &acl); 10907393101SJan Kara if (ret) 1107221fe4cSGuangliang Zhao goto out; 1117221fe4cSGuangliang Zhao } 1127221fe4cSGuangliang Zhao break; 1137221fe4cSGuangliang Zhao case ACL_TYPE_DEFAULT: 1147221fe4cSGuangliang Zhao if (!S_ISDIR(inode->i_mode)) { 1157221fe4cSGuangliang Zhao ret = acl ? -EINVAL : 0; 1167221fe4cSGuangliang Zhao goto out; 1177221fe4cSGuangliang Zhao } 11897d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_DEFAULT; 1197221fe4cSGuangliang Zhao break; 1207221fe4cSGuangliang Zhao default: 1217221fe4cSGuangliang Zhao ret = -EINVAL; 1227221fe4cSGuangliang Zhao goto out; 1237221fe4cSGuangliang Zhao } 1247221fe4cSGuangliang Zhao 1257221fe4cSGuangliang Zhao if (acl) { 1267221fe4cSGuangliang Zhao size = posix_acl_xattr_size(acl->a_count); 1277221fe4cSGuangliang Zhao value = kmalloc(size, GFP_NOFS); 1287221fe4cSGuangliang Zhao if (!value) { 1297221fe4cSGuangliang Zhao ret = -ENOMEM; 1307221fe4cSGuangliang Zhao goto out; 1317221fe4cSGuangliang Zhao } 1327221fe4cSGuangliang Zhao 1337221fe4cSGuangliang Zhao ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); 1347221fe4cSGuangliang Zhao if (ret < 0) 1357221fe4cSGuangliang Zhao goto out_free; 1367221fe4cSGuangliang Zhao } 1377221fe4cSGuangliang Zhao 1387221fe4cSGuangliang Zhao if (new_mode != old_mode) { 1394ca2fea6SYan, Zheng newattrs.ia_ctime = current_time(inode); 1407221fe4cSGuangliang Zhao newattrs.ia_mode = new_mode; 14193d35c75SChengguang Xu newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; 142a26feccaSAndreas Gruenbacher ret = __ceph_setattr(inode, &newattrs); 1437221fe4cSGuangliang Zhao if (ret) 144a26feccaSAndreas Gruenbacher goto out_free; 1457221fe4cSGuangliang Zhao } 1467221fe4cSGuangliang Zhao 147a26feccaSAndreas Gruenbacher ret = __ceph_setxattr(inode, name, value, size, 0); 1487221fe4cSGuangliang Zhao if (ret) { 1497221fe4cSGuangliang Zhao if (new_mode != old_mode) { 15093d35c75SChengguang Xu newattrs.ia_ctime = old_ctime; 1517221fe4cSGuangliang Zhao newattrs.ia_mode = old_mode; 15293d35c75SChengguang Xu newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; 153a26feccaSAndreas Gruenbacher __ceph_setattr(inode, &newattrs); 1547221fe4cSGuangliang Zhao } 155a26feccaSAndreas Gruenbacher goto out_free; 1567221fe4cSGuangliang Zhao } 1577221fe4cSGuangliang Zhao 1587221fe4cSGuangliang Zhao ceph_set_cached_acl(inode, type, acl); 1597221fe4cSGuangliang Zhao 1607221fe4cSGuangliang Zhao out_free: 1617221fe4cSGuangliang Zhao kfree(value); 1627221fe4cSGuangliang Zhao out: 1637221fe4cSGuangliang Zhao return ret; 1647221fe4cSGuangliang Zhao } 1657221fe4cSGuangliang Zhao 166b1ee94aaSYan, Zheng int ceph_pre_init_acls(struct inode *dir, umode_t *mode, 1675c31e92dSYan, Zheng struct ceph_acl_sec_ctx *as_ctx) 1687221fe4cSGuangliang Zhao { 169b1ee94aaSYan, Zheng struct posix_acl *acl, *default_acl; 170b1ee94aaSYan, Zheng size_t val_size1 = 0, val_size2 = 0; 171b1ee94aaSYan, Zheng struct ceph_pagelist *pagelist = NULL; 172b1ee94aaSYan, Zheng void *tmp_buf = NULL; 173b1ee94aaSYan, Zheng int err; 1747221fe4cSGuangliang Zhao 175b1ee94aaSYan, Zheng err = posix_acl_create(dir, mode, &default_acl, &acl); 176b1ee94aaSYan, Zheng if (err) 177b1ee94aaSYan, Zheng return err; 1787221fe4cSGuangliang Zhao 17975858236SChristoph Hellwig if (acl) { 18061ad36d4SChengguang Xu err = posix_acl_equiv_mode(acl, mode); 18161ad36d4SChengguang Xu if (err < 0) 182b1ee94aaSYan, Zheng goto out_err; 18361ad36d4SChengguang Xu if (err == 0) { 1847221fe4cSGuangliang Zhao posix_acl_release(acl); 185b1ee94aaSYan, Zheng acl = NULL; 18675858236SChristoph Hellwig } 187b1ee94aaSYan, Zheng } 188b1ee94aaSYan, Zheng 189b1ee94aaSYan, Zheng if (!default_acl && !acl) 190b1ee94aaSYan, Zheng return 0; 191b1ee94aaSYan, Zheng 192b1ee94aaSYan, Zheng if (acl) 193b1ee94aaSYan, Zheng val_size1 = posix_acl_xattr_size(acl->a_count); 194b1ee94aaSYan, Zheng if (default_acl) 195b1ee94aaSYan, Zheng val_size2 = posix_acl_xattr_size(default_acl->a_count); 196b1ee94aaSYan, Zheng 197b1ee94aaSYan, Zheng err = -ENOMEM; 198687265e5SYan, Zheng tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL); 199b1ee94aaSYan, Zheng if (!tmp_buf) 200b1ee94aaSYan, Zheng goto out_err; 20133165d47SIlya Dryomov pagelist = ceph_pagelist_alloc(GFP_KERNEL); 202b1ee94aaSYan, Zheng if (!pagelist) 203b1ee94aaSYan, Zheng goto out_err; 204b1ee94aaSYan, Zheng 205b1ee94aaSYan, Zheng err = ceph_pagelist_reserve(pagelist, PAGE_SIZE); 206b1ee94aaSYan, Zheng if (err) 207b1ee94aaSYan, Zheng goto out_err; 208b1ee94aaSYan, Zheng 209b1ee94aaSYan, Zheng ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1); 210b1ee94aaSYan, Zheng 211b1ee94aaSYan, Zheng if (acl) { 21297d79299SAndreas Gruenbacher size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS); 213b1ee94aaSYan, Zheng err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8); 214b1ee94aaSYan, Zheng if (err) 215b1ee94aaSYan, Zheng goto out_err; 21697d79299SAndreas Gruenbacher ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS, 217b1ee94aaSYan, Zheng len); 218b1ee94aaSYan, Zheng err = posix_acl_to_xattr(&init_user_ns, acl, 219b1ee94aaSYan, Zheng tmp_buf, val_size1); 220b1ee94aaSYan, Zheng if (err < 0) 221b1ee94aaSYan, Zheng goto out_err; 222b1ee94aaSYan, Zheng ceph_pagelist_encode_32(pagelist, val_size1); 223b1ee94aaSYan, Zheng ceph_pagelist_append(pagelist, tmp_buf, val_size1); 224b1ee94aaSYan, Zheng } 225b1ee94aaSYan, Zheng if (default_acl) { 22697d79299SAndreas Gruenbacher size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT); 227b1ee94aaSYan, Zheng err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8); 228b1ee94aaSYan, Zheng if (err) 229b1ee94aaSYan, Zheng goto out_err; 230d80865bfSChengguang Xu ceph_pagelist_encode_string(pagelist, 23197d79299SAndreas Gruenbacher XATTR_NAME_POSIX_ACL_DEFAULT, len); 232b1ee94aaSYan, Zheng err = posix_acl_to_xattr(&init_user_ns, default_acl, 233b1ee94aaSYan, Zheng tmp_buf, val_size2); 234b1ee94aaSYan, Zheng if (err < 0) 235b1ee94aaSYan, Zheng goto out_err; 236b1ee94aaSYan, Zheng ceph_pagelist_encode_32(pagelist, val_size2); 237b1ee94aaSYan, Zheng ceph_pagelist_append(pagelist, tmp_buf, val_size2); 238b1ee94aaSYan, Zheng } 239b1ee94aaSYan, Zheng 240b1ee94aaSYan, Zheng kfree(tmp_buf); 241b1ee94aaSYan, Zheng 2425c31e92dSYan, Zheng as_ctx->acl = acl; 2435c31e92dSYan, Zheng as_ctx->default_acl = default_acl; 2445c31e92dSYan, Zheng as_ctx->pagelist = pagelist; 245b1ee94aaSYan, Zheng return 0; 246b1ee94aaSYan, Zheng 247b1ee94aaSYan, Zheng out_err: 248b1ee94aaSYan, Zheng posix_acl_release(acl); 249b1ee94aaSYan, Zheng posix_acl_release(default_acl); 250b1ee94aaSYan, Zheng kfree(tmp_buf); 251b1ee94aaSYan, Zheng if (pagelist) 252b1ee94aaSYan, Zheng ceph_pagelist_release(pagelist); 253b1ee94aaSYan, Zheng return err; 254b1ee94aaSYan, Zheng } 255b1ee94aaSYan, Zheng 2565c31e92dSYan, Zheng void ceph_init_inode_acls(struct inode *inode, struct ceph_acl_sec_ctx *as_ctx) 257b1ee94aaSYan, Zheng { 258b1ee94aaSYan, Zheng if (!inode) 259b1ee94aaSYan, Zheng return; 2605c31e92dSYan, Zheng ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, as_ctx->acl); 2615c31e92dSYan, Zheng ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, as_ctx->default_acl); 2627221fe4cSGuangliang Zhao } 263