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 320cad6246SMiklos 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 400cad6246SMiklos Szeredi if (rcu) 410cad6246SMiklos Szeredi return ERR_PTR(-ECHILD); 420cad6246SMiklos 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 8813e83a49SChristian Brauner int ceph_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 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; 95138060baSChristian Brauner struct inode *inode = d_inode(dentry); 9693d35c75SChengguang Xu struct timespec64 old_ctime = inode->i_ctime; 977221fe4cSGuangliang Zhao umode_t new_mode = inode->i_mode, old_mode = inode->i_mode; 987221fe4cSGuangliang Zhao 995da20799SChengguang Xu if (ceph_snap(inode) != CEPH_NOSNAP) { 1005da20799SChengguang Xu ret = -EROFS; 1015da20799SChengguang Xu goto out; 1025da20799SChengguang Xu } 1035da20799SChengguang Xu 1047221fe4cSGuangliang Zhao switch (type) { 1057221fe4cSGuangliang Zhao case ACL_TYPE_ACCESS: 10697d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_ACCESS; 1077221fe4cSGuangliang Zhao if (acl) { 108*700b7940SChristian Brauner ret = posix_acl_update_mode(&nop_mnt_idmap, inode, 109e65ce2a5SChristian Brauner &new_mode, &acl); 11007393101SJan Kara if (ret) 1117221fe4cSGuangliang Zhao goto out; 1127221fe4cSGuangliang Zhao } 1137221fe4cSGuangliang Zhao break; 1147221fe4cSGuangliang Zhao case ACL_TYPE_DEFAULT: 1157221fe4cSGuangliang Zhao if (!S_ISDIR(inode->i_mode)) { 1167221fe4cSGuangliang Zhao ret = acl ? -EINVAL : 0; 1177221fe4cSGuangliang Zhao goto out; 1187221fe4cSGuangliang Zhao } 11997d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_DEFAULT; 1207221fe4cSGuangliang Zhao break; 1217221fe4cSGuangliang Zhao default: 1227221fe4cSGuangliang Zhao ret = -EINVAL; 1237221fe4cSGuangliang Zhao goto out; 1247221fe4cSGuangliang Zhao } 1257221fe4cSGuangliang Zhao 1267221fe4cSGuangliang Zhao if (acl) { 1277221fe4cSGuangliang Zhao size = posix_acl_xattr_size(acl->a_count); 1287221fe4cSGuangliang Zhao value = kmalloc(size, GFP_NOFS); 1297221fe4cSGuangliang Zhao if (!value) { 1307221fe4cSGuangliang Zhao ret = -ENOMEM; 1317221fe4cSGuangliang Zhao goto out; 1327221fe4cSGuangliang Zhao } 1337221fe4cSGuangliang Zhao 1347221fe4cSGuangliang Zhao ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); 1357221fe4cSGuangliang Zhao if (ret < 0) 1367221fe4cSGuangliang Zhao goto out_free; 1377221fe4cSGuangliang Zhao } 1387221fe4cSGuangliang Zhao 1397221fe4cSGuangliang Zhao if (new_mode != old_mode) { 1404ca2fea6SYan, Zheng newattrs.ia_ctime = current_time(inode); 1417221fe4cSGuangliang Zhao newattrs.ia_mode = new_mode; 14293d35c75SChengguang Xu newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; 143a26feccaSAndreas Gruenbacher ret = __ceph_setattr(inode, &newattrs); 1447221fe4cSGuangliang Zhao if (ret) 145a26feccaSAndreas Gruenbacher goto out_free; 1467221fe4cSGuangliang Zhao } 1477221fe4cSGuangliang Zhao 148a26feccaSAndreas Gruenbacher ret = __ceph_setxattr(inode, name, value, size, 0); 1497221fe4cSGuangliang Zhao if (ret) { 1507221fe4cSGuangliang Zhao if (new_mode != old_mode) { 15193d35c75SChengguang Xu newattrs.ia_ctime = old_ctime; 1527221fe4cSGuangliang Zhao newattrs.ia_mode = old_mode; 15393d35c75SChengguang Xu newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; 154a26feccaSAndreas Gruenbacher __ceph_setattr(inode, &newattrs); 1557221fe4cSGuangliang Zhao } 156a26feccaSAndreas Gruenbacher goto out_free; 1577221fe4cSGuangliang Zhao } 1587221fe4cSGuangliang Zhao 1597221fe4cSGuangliang Zhao ceph_set_cached_acl(inode, type, acl); 1607221fe4cSGuangliang Zhao 1617221fe4cSGuangliang Zhao out_free: 1627221fe4cSGuangliang Zhao kfree(value); 1637221fe4cSGuangliang Zhao out: 1647221fe4cSGuangliang Zhao return ret; 1657221fe4cSGuangliang Zhao } 1667221fe4cSGuangliang Zhao 167b1ee94aaSYan, Zheng int ceph_pre_init_acls(struct inode *dir, umode_t *mode, 1685c31e92dSYan, Zheng struct ceph_acl_sec_ctx *as_ctx) 1697221fe4cSGuangliang Zhao { 170b1ee94aaSYan, Zheng struct posix_acl *acl, *default_acl; 171b1ee94aaSYan, Zheng size_t val_size1 = 0, val_size2 = 0; 172b1ee94aaSYan, Zheng struct ceph_pagelist *pagelist = NULL; 173b1ee94aaSYan, Zheng void *tmp_buf = NULL; 174b1ee94aaSYan, Zheng int err; 1757221fe4cSGuangliang Zhao 176b1ee94aaSYan, Zheng err = posix_acl_create(dir, mode, &default_acl, &acl); 177b1ee94aaSYan, Zheng if (err) 178b1ee94aaSYan, Zheng return err; 1797221fe4cSGuangliang Zhao 18075858236SChristoph Hellwig if (acl) { 18161ad36d4SChengguang Xu err = posix_acl_equiv_mode(acl, mode); 18261ad36d4SChengguang Xu if (err < 0) 183b1ee94aaSYan, Zheng goto out_err; 18461ad36d4SChengguang Xu if (err == 0) { 1857221fe4cSGuangliang Zhao posix_acl_release(acl); 186b1ee94aaSYan, Zheng acl = NULL; 18775858236SChristoph Hellwig } 188b1ee94aaSYan, Zheng } 189b1ee94aaSYan, Zheng 190b1ee94aaSYan, Zheng if (!default_acl && !acl) 191b1ee94aaSYan, Zheng return 0; 192b1ee94aaSYan, Zheng 193b1ee94aaSYan, Zheng if (acl) 194b1ee94aaSYan, Zheng val_size1 = posix_acl_xattr_size(acl->a_count); 195b1ee94aaSYan, Zheng if (default_acl) 196b1ee94aaSYan, Zheng val_size2 = posix_acl_xattr_size(default_acl->a_count); 197b1ee94aaSYan, Zheng 198b1ee94aaSYan, Zheng err = -ENOMEM; 199687265e5SYan, Zheng tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL); 200b1ee94aaSYan, Zheng if (!tmp_buf) 201b1ee94aaSYan, Zheng goto out_err; 20233165d47SIlya Dryomov pagelist = ceph_pagelist_alloc(GFP_KERNEL); 203b1ee94aaSYan, Zheng if (!pagelist) 204b1ee94aaSYan, Zheng goto out_err; 205b1ee94aaSYan, Zheng 206b1ee94aaSYan, Zheng err = ceph_pagelist_reserve(pagelist, PAGE_SIZE); 207b1ee94aaSYan, Zheng if (err) 208b1ee94aaSYan, Zheng goto out_err; 209b1ee94aaSYan, Zheng 210b1ee94aaSYan, Zheng ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1); 211b1ee94aaSYan, Zheng 212b1ee94aaSYan, Zheng if (acl) { 21397d79299SAndreas Gruenbacher size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS); 214b1ee94aaSYan, Zheng err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8); 215b1ee94aaSYan, Zheng if (err) 216b1ee94aaSYan, Zheng goto out_err; 21797d79299SAndreas Gruenbacher ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS, 218b1ee94aaSYan, Zheng len); 219b1ee94aaSYan, Zheng err = posix_acl_to_xattr(&init_user_ns, acl, 220b1ee94aaSYan, Zheng tmp_buf, val_size1); 221b1ee94aaSYan, Zheng if (err < 0) 222b1ee94aaSYan, Zheng goto out_err; 223b1ee94aaSYan, Zheng ceph_pagelist_encode_32(pagelist, val_size1); 224b1ee94aaSYan, Zheng ceph_pagelist_append(pagelist, tmp_buf, val_size1); 225b1ee94aaSYan, Zheng } 226b1ee94aaSYan, Zheng if (default_acl) { 22797d79299SAndreas Gruenbacher size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT); 228b1ee94aaSYan, Zheng err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8); 229b1ee94aaSYan, Zheng if (err) 230b1ee94aaSYan, Zheng goto out_err; 231d80865bfSChengguang Xu ceph_pagelist_encode_string(pagelist, 23297d79299SAndreas Gruenbacher XATTR_NAME_POSIX_ACL_DEFAULT, len); 233b1ee94aaSYan, Zheng err = posix_acl_to_xattr(&init_user_ns, default_acl, 234b1ee94aaSYan, Zheng tmp_buf, val_size2); 235b1ee94aaSYan, Zheng if (err < 0) 236b1ee94aaSYan, Zheng goto out_err; 237b1ee94aaSYan, Zheng ceph_pagelist_encode_32(pagelist, val_size2); 238b1ee94aaSYan, Zheng ceph_pagelist_append(pagelist, tmp_buf, val_size2); 239b1ee94aaSYan, Zheng } 240b1ee94aaSYan, Zheng 241b1ee94aaSYan, Zheng kfree(tmp_buf); 242b1ee94aaSYan, Zheng 2435c31e92dSYan, Zheng as_ctx->acl = acl; 2445c31e92dSYan, Zheng as_ctx->default_acl = default_acl; 2455c31e92dSYan, Zheng as_ctx->pagelist = pagelist; 246b1ee94aaSYan, Zheng return 0; 247b1ee94aaSYan, Zheng 248b1ee94aaSYan, Zheng out_err: 249b1ee94aaSYan, Zheng posix_acl_release(acl); 250b1ee94aaSYan, Zheng posix_acl_release(default_acl); 251b1ee94aaSYan, Zheng kfree(tmp_buf); 252b1ee94aaSYan, Zheng if (pagelist) 253b1ee94aaSYan, Zheng ceph_pagelist_release(pagelist); 254b1ee94aaSYan, Zheng return err; 255b1ee94aaSYan, Zheng } 256b1ee94aaSYan, Zheng 2575c31e92dSYan, Zheng void ceph_init_inode_acls(struct inode *inode, struct ceph_acl_sec_ctx *as_ctx) 258b1ee94aaSYan, Zheng { 259b1ee94aaSYan, Zheng if (!inode) 260b1ee94aaSYan, Zheng return; 2615c31e92dSYan, Zheng ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, as_ctx->acl); 2625c31e92dSYan, Zheng ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, as_ctx->default_acl); 2637221fe4cSGuangliang Zhao } 264