17221fe4cSGuangliang Zhao /* 27221fe4cSGuangliang Zhao * linux/fs/ceph/acl.c 37221fe4cSGuangliang Zhao * 47221fe4cSGuangliang Zhao * Copyright (C) 2013 Guangliang Zhao, <lucienchao@gmail.com> 57221fe4cSGuangliang Zhao * 67221fe4cSGuangliang Zhao * This program is free software; you can redistribute it and/or 77221fe4cSGuangliang Zhao * modify it under the terms of the GNU General Public 87221fe4cSGuangliang Zhao * License v2 as published by the Free Software Foundation. 97221fe4cSGuangliang Zhao * 107221fe4cSGuangliang Zhao * This program is distributed in the hope that it will be useful, 117221fe4cSGuangliang Zhao * but WITHOUT ANY WARRANTY; without even the implied warranty of 127221fe4cSGuangliang Zhao * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 137221fe4cSGuangliang Zhao * General Public License for more details. 147221fe4cSGuangliang Zhao * 157221fe4cSGuangliang Zhao * You should have received a copy of the GNU General Public 167221fe4cSGuangliang Zhao * License along with this program; if not, write to the 177221fe4cSGuangliang Zhao * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 187221fe4cSGuangliang Zhao * Boston, MA 021110-1307, USA. 197221fe4cSGuangliang Zhao */ 207221fe4cSGuangliang Zhao 217221fe4cSGuangliang Zhao #include <linux/ceph/ceph_debug.h> 227221fe4cSGuangliang Zhao #include <linux/fs.h> 237221fe4cSGuangliang Zhao #include <linux/string.h> 247221fe4cSGuangliang Zhao #include <linux/xattr.h> 257221fe4cSGuangliang Zhao #include <linux/posix_acl_xattr.h> 267221fe4cSGuangliang Zhao #include <linux/posix_acl.h> 277221fe4cSGuangliang Zhao #include <linux/sched.h> 287221fe4cSGuangliang Zhao #include <linux/slab.h> 297221fe4cSGuangliang Zhao 307221fe4cSGuangliang Zhao #include "super.h" 317221fe4cSGuangliang Zhao 327221fe4cSGuangliang Zhao static inline void ceph_set_cached_acl(struct inode *inode, 337221fe4cSGuangliang Zhao int type, struct posix_acl *acl) 347221fe4cSGuangliang Zhao { 357221fe4cSGuangliang Zhao struct ceph_inode_info *ci = ceph_inode(inode); 367221fe4cSGuangliang Zhao 377221fe4cSGuangliang Zhao spin_lock(&ci->i_ceph_lock); 387221fe4cSGuangliang Zhao if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0)) 397221fe4cSGuangliang Zhao set_cached_acl(inode, type, acl); 40b8a7a3a6SAndreas Gruenbacher else 41b8a7a3a6SAndreas Gruenbacher forget_cached_acl(inode, type); 427221fe4cSGuangliang Zhao spin_unlock(&ci->i_ceph_lock); 437221fe4cSGuangliang Zhao } 447221fe4cSGuangliang Zhao 457221fe4cSGuangliang Zhao struct posix_acl *ceph_get_acl(struct inode *inode, int type) 467221fe4cSGuangliang Zhao { 477221fe4cSGuangliang Zhao int size; 48f017754dSChengguang Xu unsigned int retry_cnt = 0; 497221fe4cSGuangliang Zhao const char *name; 507221fe4cSGuangliang Zhao char *value = NULL; 517221fe4cSGuangliang Zhao struct posix_acl *acl; 527221fe4cSGuangliang Zhao 537221fe4cSGuangliang Zhao switch (type) { 547221fe4cSGuangliang Zhao case ACL_TYPE_ACCESS: 5597d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_ACCESS; 567221fe4cSGuangliang Zhao break; 577221fe4cSGuangliang Zhao case ACL_TYPE_DEFAULT: 5897d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_DEFAULT; 597221fe4cSGuangliang Zhao break; 607221fe4cSGuangliang Zhao default: 617221fe4cSGuangliang Zhao BUG(); 627221fe4cSGuangliang Zhao } 637221fe4cSGuangliang Zhao 64f017754dSChengguang Xu retry: 657221fe4cSGuangliang Zhao size = __ceph_getxattr(inode, name, "", 0); 667221fe4cSGuangliang Zhao if (size > 0) { 677221fe4cSGuangliang Zhao value = kzalloc(size, GFP_NOFS); 687221fe4cSGuangliang Zhao if (!value) 697221fe4cSGuangliang Zhao return ERR_PTR(-ENOMEM); 707221fe4cSGuangliang Zhao size = __ceph_getxattr(inode, name, value, size); 717221fe4cSGuangliang Zhao } 727221fe4cSGuangliang Zhao 73f017754dSChengguang Xu if (size == -ERANGE && retry_cnt < 10) { 74f017754dSChengguang Xu retry_cnt++; 75f017754dSChengguang Xu kfree(value); 76f017754dSChengguang Xu value = NULL; 77f017754dSChengguang Xu goto retry; 78f017754dSChengguang Xu } 79f017754dSChengguang Xu 80f017754dSChengguang Xu if (size > 0) { 817221fe4cSGuangliang Zhao acl = posix_acl_from_xattr(&init_user_ns, value, size); 82f017754dSChengguang Xu } else if (size == -ENODATA || size == 0) { 837221fe4cSGuangliang Zhao acl = NULL; 84f017754dSChengguang Xu } else { 85f017754dSChengguang Xu pr_err_ratelimited("get acl %llx.%llx failed, err=%d\n", 86f017754dSChengguang Xu ceph_vinop(inode), size); 877221fe4cSGuangliang Zhao acl = ERR_PTR(-EIO); 88f017754dSChengguang Xu } 897221fe4cSGuangliang Zhao 907221fe4cSGuangliang Zhao kfree(value); 917221fe4cSGuangliang Zhao 927221fe4cSGuangliang Zhao if (!IS_ERR(acl)) 937221fe4cSGuangliang Zhao ceph_set_cached_acl(inode, type, acl); 947221fe4cSGuangliang Zhao 957221fe4cSGuangliang Zhao return acl; 967221fe4cSGuangliang Zhao } 977221fe4cSGuangliang Zhao 9872466d0bSSage Weil int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type) 997221fe4cSGuangliang Zhao { 1007221fe4cSGuangliang Zhao int ret = 0, size = 0; 1017221fe4cSGuangliang Zhao const char *name = NULL; 1027221fe4cSGuangliang Zhao char *value = NULL; 1037221fe4cSGuangliang Zhao struct iattr newattrs; 10493d35c75SChengguang Xu struct timespec64 old_ctime = inode->i_ctime; 1057221fe4cSGuangliang Zhao umode_t new_mode = inode->i_mode, old_mode = inode->i_mode; 1067221fe4cSGuangliang Zhao 1077221fe4cSGuangliang Zhao switch (type) { 1087221fe4cSGuangliang Zhao case ACL_TYPE_ACCESS: 10997d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_ACCESS; 1107221fe4cSGuangliang Zhao if (acl) { 11107393101SJan Kara ret = posix_acl_update_mode(inode, &new_mode, &acl); 11207393101SJan Kara if (ret) 1137221fe4cSGuangliang Zhao goto out; 1147221fe4cSGuangliang Zhao } 1157221fe4cSGuangliang Zhao break; 1167221fe4cSGuangliang Zhao case ACL_TYPE_DEFAULT: 1177221fe4cSGuangliang Zhao if (!S_ISDIR(inode->i_mode)) { 1187221fe4cSGuangliang Zhao ret = acl ? -EINVAL : 0; 1197221fe4cSGuangliang Zhao goto out; 1207221fe4cSGuangliang Zhao } 12197d79299SAndreas Gruenbacher name = XATTR_NAME_POSIX_ACL_DEFAULT; 1227221fe4cSGuangliang Zhao break; 1237221fe4cSGuangliang Zhao default: 1247221fe4cSGuangliang Zhao ret = -EINVAL; 1257221fe4cSGuangliang Zhao goto out; 1267221fe4cSGuangliang Zhao } 1277221fe4cSGuangliang Zhao 1287221fe4cSGuangliang Zhao if (acl) { 1297221fe4cSGuangliang Zhao size = posix_acl_xattr_size(acl->a_count); 1307221fe4cSGuangliang Zhao value = kmalloc(size, GFP_NOFS); 1317221fe4cSGuangliang Zhao if (!value) { 1327221fe4cSGuangliang Zhao ret = -ENOMEM; 1337221fe4cSGuangliang Zhao goto out; 1347221fe4cSGuangliang Zhao } 1357221fe4cSGuangliang Zhao 1367221fe4cSGuangliang Zhao ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); 1377221fe4cSGuangliang Zhao if (ret < 0) 1387221fe4cSGuangliang Zhao goto out_free; 1397221fe4cSGuangliang Zhao } 1407221fe4cSGuangliang Zhao 141fd5472edSJan Kara if (ceph_snap(inode) != CEPH_NOSNAP) { 142fd5472edSJan Kara ret = -EROFS; 143fd5472edSJan Kara goto out_free; 144fd5472edSJan Kara } 145fd5472edSJan Kara 1467221fe4cSGuangliang Zhao if (new_mode != old_mode) { 1474ca2fea6SYan, Zheng newattrs.ia_ctime = current_time(inode); 1487221fe4cSGuangliang Zhao newattrs.ia_mode = new_mode; 14993d35c75SChengguang Xu newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; 150a26feccaSAndreas Gruenbacher ret = __ceph_setattr(inode, &newattrs); 1517221fe4cSGuangliang Zhao if (ret) 152a26feccaSAndreas Gruenbacher goto out_free; 1537221fe4cSGuangliang Zhao } 1547221fe4cSGuangliang Zhao 155a26feccaSAndreas Gruenbacher ret = __ceph_setxattr(inode, name, value, size, 0); 1567221fe4cSGuangliang Zhao if (ret) { 1577221fe4cSGuangliang Zhao if (new_mode != old_mode) { 15893d35c75SChengguang Xu newattrs.ia_ctime = old_ctime; 1597221fe4cSGuangliang Zhao newattrs.ia_mode = old_mode; 16093d35c75SChengguang Xu newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; 161a26feccaSAndreas Gruenbacher __ceph_setattr(inode, &newattrs); 1627221fe4cSGuangliang Zhao } 163a26feccaSAndreas Gruenbacher goto out_free; 1647221fe4cSGuangliang Zhao } 1657221fe4cSGuangliang Zhao 1667221fe4cSGuangliang Zhao ceph_set_cached_acl(inode, type, acl); 1677221fe4cSGuangliang Zhao 1687221fe4cSGuangliang Zhao out_free: 1697221fe4cSGuangliang Zhao kfree(value); 1707221fe4cSGuangliang Zhao out: 1717221fe4cSGuangliang Zhao return ret; 1727221fe4cSGuangliang Zhao } 1737221fe4cSGuangliang Zhao 174b1ee94aaSYan, Zheng int ceph_pre_init_acls(struct inode *dir, umode_t *mode, 175b1ee94aaSYan, Zheng struct ceph_acls_info *info) 1767221fe4cSGuangliang Zhao { 177b1ee94aaSYan, Zheng struct posix_acl *acl, *default_acl; 178b1ee94aaSYan, Zheng size_t val_size1 = 0, val_size2 = 0; 179b1ee94aaSYan, Zheng struct ceph_pagelist *pagelist = NULL; 180b1ee94aaSYan, Zheng void *tmp_buf = NULL; 181b1ee94aaSYan, Zheng int err; 1827221fe4cSGuangliang Zhao 183b1ee94aaSYan, Zheng err = posix_acl_create(dir, mode, &default_acl, &acl); 184b1ee94aaSYan, Zheng if (err) 185b1ee94aaSYan, Zheng return err; 1867221fe4cSGuangliang Zhao 18775858236SChristoph Hellwig if (acl) { 188*61ad36d4SChengguang Xu err = posix_acl_equiv_mode(acl, mode); 189*61ad36d4SChengguang Xu if (err < 0) 190b1ee94aaSYan, Zheng goto out_err; 191*61ad36d4SChengguang Xu if (err == 0) { 1927221fe4cSGuangliang Zhao posix_acl_release(acl); 193b1ee94aaSYan, Zheng acl = NULL; 19475858236SChristoph Hellwig } 195b1ee94aaSYan, Zheng } 196b1ee94aaSYan, Zheng 197b1ee94aaSYan, Zheng if (!default_acl && !acl) 198b1ee94aaSYan, Zheng return 0; 199b1ee94aaSYan, Zheng 200b1ee94aaSYan, Zheng if (acl) 201b1ee94aaSYan, Zheng val_size1 = posix_acl_xattr_size(acl->a_count); 202b1ee94aaSYan, Zheng if (default_acl) 203b1ee94aaSYan, Zheng val_size2 = posix_acl_xattr_size(default_acl->a_count); 204b1ee94aaSYan, Zheng 205b1ee94aaSYan, Zheng err = -ENOMEM; 206687265e5SYan, Zheng tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL); 207b1ee94aaSYan, Zheng if (!tmp_buf) 208b1ee94aaSYan, Zheng goto out_err; 209687265e5SYan, Zheng pagelist = kmalloc(sizeof(struct ceph_pagelist), GFP_KERNEL); 210b1ee94aaSYan, Zheng if (!pagelist) 211b1ee94aaSYan, Zheng goto out_err; 212b1ee94aaSYan, Zheng ceph_pagelist_init(pagelist); 213b1ee94aaSYan, Zheng 214b1ee94aaSYan, Zheng err = ceph_pagelist_reserve(pagelist, PAGE_SIZE); 215b1ee94aaSYan, Zheng if (err) 216b1ee94aaSYan, Zheng goto out_err; 217b1ee94aaSYan, Zheng 218b1ee94aaSYan, Zheng ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1); 219b1ee94aaSYan, Zheng 220b1ee94aaSYan, Zheng if (acl) { 22197d79299SAndreas Gruenbacher size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS); 222b1ee94aaSYan, Zheng err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8); 223b1ee94aaSYan, Zheng if (err) 224b1ee94aaSYan, Zheng goto out_err; 22597d79299SAndreas Gruenbacher ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS, 226b1ee94aaSYan, Zheng len); 227b1ee94aaSYan, Zheng err = posix_acl_to_xattr(&init_user_ns, acl, 228b1ee94aaSYan, Zheng tmp_buf, val_size1); 229b1ee94aaSYan, Zheng if (err < 0) 230b1ee94aaSYan, Zheng goto out_err; 231b1ee94aaSYan, Zheng ceph_pagelist_encode_32(pagelist, val_size1); 232b1ee94aaSYan, Zheng ceph_pagelist_append(pagelist, tmp_buf, val_size1); 233b1ee94aaSYan, Zheng } 234b1ee94aaSYan, Zheng if (default_acl) { 23597d79299SAndreas Gruenbacher size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT); 236b1ee94aaSYan, Zheng err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8); 237b1ee94aaSYan, Zheng if (err) 238b1ee94aaSYan, Zheng goto out_err; 239b1ee94aaSYan, Zheng err = ceph_pagelist_encode_string(pagelist, 24097d79299SAndreas Gruenbacher XATTR_NAME_POSIX_ACL_DEFAULT, len); 241b1ee94aaSYan, Zheng err = posix_acl_to_xattr(&init_user_ns, default_acl, 242b1ee94aaSYan, Zheng tmp_buf, val_size2); 243b1ee94aaSYan, Zheng if (err < 0) 244b1ee94aaSYan, Zheng goto out_err; 245b1ee94aaSYan, Zheng ceph_pagelist_encode_32(pagelist, val_size2); 246b1ee94aaSYan, Zheng ceph_pagelist_append(pagelist, tmp_buf, val_size2); 247b1ee94aaSYan, Zheng } 248b1ee94aaSYan, Zheng 249b1ee94aaSYan, Zheng kfree(tmp_buf); 250b1ee94aaSYan, Zheng 251b1ee94aaSYan, Zheng info->acl = acl; 252b1ee94aaSYan, Zheng info->default_acl = default_acl; 253b1ee94aaSYan, Zheng info->pagelist = pagelist; 254b1ee94aaSYan, Zheng return 0; 255b1ee94aaSYan, Zheng 256b1ee94aaSYan, Zheng out_err: 257b1ee94aaSYan, Zheng posix_acl_release(acl); 258b1ee94aaSYan, Zheng posix_acl_release(default_acl); 259b1ee94aaSYan, Zheng kfree(tmp_buf); 260b1ee94aaSYan, Zheng if (pagelist) 261b1ee94aaSYan, Zheng ceph_pagelist_release(pagelist); 262b1ee94aaSYan, Zheng return err; 263b1ee94aaSYan, Zheng } 264b1ee94aaSYan, Zheng 265b1ee94aaSYan, Zheng void ceph_init_inode_acls(struct inode* inode, struct ceph_acls_info *info) 266b1ee94aaSYan, Zheng { 267b1ee94aaSYan, Zheng if (!inode) 268b1ee94aaSYan, Zheng return; 269b1ee94aaSYan, Zheng ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, info->acl); 270b1ee94aaSYan, Zheng ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, info->default_acl); 271b1ee94aaSYan, Zheng } 272b1ee94aaSYan, Zheng 273b1ee94aaSYan, Zheng void ceph_release_acls_info(struct ceph_acls_info *info) 274b1ee94aaSYan, Zheng { 275b1ee94aaSYan, Zheng posix_acl_release(info->acl); 276b1ee94aaSYan, Zheng posix_acl_release(info->default_acl); 277b1ee94aaSYan, Zheng if (info->pagelist) 278b1ee94aaSYan, Zheng ceph_pagelist_release(info->pagelist); 2797221fe4cSGuangliang Zhao } 280