Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * linux/fs/ceph/acl.c
0004  *
0005  * Copyright (C) 2013 Guangliang Zhao, <lucienchao@gmail.com>
0006  */
0007 
0008 #include <linux/ceph/ceph_debug.h>
0009 #include <linux/fs.h>
0010 #include <linux/string.h>
0011 #include <linux/xattr.h>
0012 #include <linux/posix_acl_xattr.h>
0013 #include <linux/posix_acl.h>
0014 #include <linux/sched.h>
0015 #include <linux/slab.h>
0016 
0017 #include "super.h"
0018 
0019 static inline void ceph_set_cached_acl(struct inode *inode,
0020                     int type, struct posix_acl *acl)
0021 {
0022     struct ceph_inode_info *ci = ceph_inode(inode);
0023 
0024     spin_lock(&ci->i_ceph_lock);
0025     if (__ceph_caps_issued_mask_metric(ci, CEPH_CAP_XATTR_SHARED, 0))
0026         set_cached_acl(inode, type, acl);
0027     else
0028         forget_cached_acl(inode, type);
0029     spin_unlock(&ci->i_ceph_lock);
0030 }
0031 
0032 struct posix_acl *ceph_get_acl(struct inode *inode, int type, bool rcu)
0033 {
0034     int size;
0035     unsigned int retry_cnt = 0;
0036     const char *name;
0037     char *value = NULL;
0038     struct posix_acl *acl;
0039 
0040     if (rcu)
0041         return ERR_PTR(-ECHILD);
0042 
0043     switch (type) {
0044     case ACL_TYPE_ACCESS:
0045         name = XATTR_NAME_POSIX_ACL_ACCESS;
0046         break;
0047     case ACL_TYPE_DEFAULT:
0048         name = XATTR_NAME_POSIX_ACL_DEFAULT;
0049         break;
0050     default:
0051         BUG();
0052     }
0053 
0054 retry:
0055     size = __ceph_getxattr(inode, name, "", 0);
0056     if (size > 0) {
0057         value = kzalloc(size, GFP_NOFS);
0058         if (!value)
0059             return ERR_PTR(-ENOMEM);
0060         size = __ceph_getxattr(inode, name, value, size);
0061     }
0062 
0063     if (size == -ERANGE && retry_cnt < 10) {
0064         retry_cnt++;
0065         kfree(value);
0066         value = NULL;
0067         goto retry;
0068     }
0069 
0070     if (size > 0) {
0071         acl = posix_acl_from_xattr(&init_user_ns, value, size);
0072     } else if (size == -ENODATA || size == 0) {
0073         acl = NULL;
0074     } else {
0075         pr_err_ratelimited("get acl %llx.%llx failed, err=%d\n",
0076                    ceph_vinop(inode), size);
0077         acl = ERR_PTR(-EIO);
0078     }
0079 
0080     kfree(value);
0081 
0082     if (!IS_ERR(acl))
0083         ceph_set_cached_acl(inode, type, acl);
0084 
0085     return acl;
0086 }
0087 
0088 int ceph_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
0089          struct posix_acl *acl, int type)
0090 {
0091     int ret = 0, size = 0;
0092     const char *name = NULL;
0093     char *value = NULL;
0094     struct iattr newattrs;
0095     struct timespec64 old_ctime = inode->i_ctime;
0096     umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
0097 
0098     if (ceph_snap(inode) != CEPH_NOSNAP) {
0099         ret = -EROFS;
0100         goto out;
0101     }
0102 
0103     switch (type) {
0104     case ACL_TYPE_ACCESS:
0105         name = XATTR_NAME_POSIX_ACL_ACCESS;
0106         if (acl) {
0107             ret = posix_acl_update_mode(&init_user_ns, inode,
0108                             &new_mode, &acl);
0109             if (ret)
0110                 goto out;
0111         }
0112         break;
0113     case ACL_TYPE_DEFAULT:
0114         if (!S_ISDIR(inode->i_mode)) {
0115             ret = acl ? -EINVAL : 0;
0116             goto out;
0117         }
0118         name = XATTR_NAME_POSIX_ACL_DEFAULT;
0119         break;
0120     default:
0121         ret = -EINVAL;
0122         goto out;
0123     }
0124 
0125     if (acl) {
0126         size = posix_acl_xattr_size(acl->a_count);
0127         value = kmalloc(size, GFP_NOFS);
0128         if (!value) {
0129             ret = -ENOMEM;
0130             goto out;
0131         }
0132 
0133         ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
0134         if (ret < 0)
0135             goto out_free;
0136     }
0137 
0138     if (new_mode != old_mode) {
0139         newattrs.ia_ctime = current_time(inode);
0140         newattrs.ia_mode = new_mode;
0141         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
0142         ret = __ceph_setattr(inode, &newattrs);
0143         if (ret)
0144             goto out_free;
0145     }
0146 
0147     ret = __ceph_setxattr(inode, name, value, size, 0);
0148     if (ret) {
0149         if (new_mode != old_mode) {
0150             newattrs.ia_ctime = old_ctime;
0151             newattrs.ia_mode = old_mode;
0152             newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
0153             __ceph_setattr(inode, &newattrs);
0154         }
0155         goto out_free;
0156     }
0157 
0158     ceph_set_cached_acl(inode, type, acl);
0159 
0160 out_free:
0161     kfree(value);
0162 out:
0163     return ret;
0164 }
0165 
0166 int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
0167                struct ceph_acl_sec_ctx *as_ctx)
0168 {
0169     struct posix_acl *acl, *default_acl;
0170     size_t val_size1 = 0, val_size2 = 0;
0171     struct ceph_pagelist *pagelist = NULL;
0172     void *tmp_buf = NULL;
0173     int err;
0174 
0175     err = posix_acl_create(dir, mode, &default_acl, &acl);
0176     if (err)
0177         return err;
0178 
0179     if (acl) {
0180         err = posix_acl_equiv_mode(acl, mode);
0181         if (err < 0)
0182             goto out_err;
0183         if (err == 0) {
0184             posix_acl_release(acl);
0185             acl = NULL;
0186         }
0187     }
0188 
0189     if (!default_acl && !acl)
0190         return 0;
0191 
0192     if (acl)
0193         val_size1 = posix_acl_xattr_size(acl->a_count);
0194     if (default_acl)
0195         val_size2 = posix_acl_xattr_size(default_acl->a_count);
0196 
0197     err = -ENOMEM;
0198     tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL);
0199     if (!tmp_buf)
0200         goto out_err;
0201     pagelist = ceph_pagelist_alloc(GFP_KERNEL);
0202     if (!pagelist)
0203         goto out_err;
0204 
0205     err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
0206     if (err)
0207         goto out_err;
0208 
0209     ceph_pagelist_encode_32(pagelist, acl && default_acl ? 2 : 1);
0210 
0211     if (acl) {
0212         size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS);
0213         err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8);
0214         if (err)
0215             goto out_err;
0216         ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS,
0217                         len);
0218         err = posix_acl_to_xattr(&init_user_ns, acl,
0219                      tmp_buf, val_size1);
0220         if (err < 0)
0221             goto out_err;
0222         ceph_pagelist_encode_32(pagelist, val_size1);
0223         ceph_pagelist_append(pagelist, tmp_buf, val_size1);
0224     }
0225     if (default_acl) {
0226         size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT);
0227         err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8);
0228         if (err)
0229             goto out_err;
0230         ceph_pagelist_encode_string(pagelist,
0231                       XATTR_NAME_POSIX_ACL_DEFAULT, len);
0232         err = posix_acl_to_xattr(&init_user_ns, default_acl,
0233                      tmp_buf, val_size2);
0234         if (err < 0)
0235             goto out_err;
0236         ceph_pagelist_encode_32(pagelist, val_size2);
0237         ceph_pagelist_append(pagelist, tmp_buf, val_size2);
0238     }
0239 
0240     kfree(tmp_buf);
0241 
0242     as_ctx->acl = acl;
0243     as_ctx->default_acl = default_acl;
0244     as_ctx->pagelist = pagelist;
0245     return 0;
0246 
0247 out_err:
0248     posix_acl_release(acl);
0249     posix_acl_release(default_acl);
0250     kfree(tmp_buf);
0251     if (pagelist)
0252         ceph_pagelist_release(pagelist);
0253     return err;
0254 }
0255 
0256 void ceph_init_inode_acls(struct inode *inode, struct ceph_acl_sec_ctx *as_ctx)
0257 {
0258     if (!inode)
0259         return;
0260     ceph_set_cached_acl(inode, ACL_TYPE_ACCESS, as_ctx->acl);
0261     ceph_set_cached_acl(inode, ACL_TYPE_DEFAULT, as_ctx->default_acl);
0262 }