0001
0002
0003
0004
0005
0006
0007 #include <linux/sched.h>
0008 #include <linux/slab.h>
0009 #include <linux/spinlock.h>
0010 #include <linux/completion.h>
0011 #include <linux/buffer_head.h>
0012 #include <linux/xattr.h>
0013 #include <linux/posix_acl.h>
0014 #include <linux/posix_acl_xattr.h>
0015 #include <linux/gfs2_ondisk.h>
0016
0017 #include "gfs2.h"
0018 #include "incore.h"
0019 #include "acl.h"
0020 #include "xattr.h"
0021 #include "glock.h"
0022 #include "inode.h"
0023 #include "meta_io.h"
0024 #include "quota.h"
0025 #include "rgrp.h"
0026 #include "trans.h"
0027 #include "util.h"
0028
0029 static const char *gfs2_acl_name(int type)
0030 {
0031 switch (type) {
0032 case ACL_TYPE_ACCESS:
0033 return XATTR_POSIX_ACL_ACCESS;
0034 case ACL_TYPE_DEFAULT:
0035 return XATTR_POSIX_ACL_DEFAULT;
0036 }
0037 return NULL;
0038 }
0039
0040 static struct posix_acl *__gfs2_get_acl(struct inode *inode, int type)
0041 {
0042 struct gfs2_inode *ip = GFS2_I(inode);
0043 struct posix_acl *acl;
0044 const char *name;
0045 char *data;
0046 int len;
0047
0048 if (!ip->i_eattr)
0049 return NULL;
0050
0051 name = gfs2_acl_name(type);
0052 len = gfs2_xattr_acl_get(ip, name, &data);
0053 if (len <= 0)
0054 return ERR_PTR(len);
0055 acl = posix_acl_from_xattr(&init_user_ns, data, len);
0056 kfree(data);
0057 return acl;
0058 }
0059
0060 struct posix_acl *gfs2_get_acl(struct inode *inode, int type, bool rcu)
0061 {
0062 struct gfs2_inode *ip = GFS2_I(inode);
0063 struct gfs2_holder gh;
0064 bool need_unlock = false;
0065 struct posix_acl *acl;
0066
0067 if (rcu)
0068 return ERR_PTR(-ECHILD);
0069
0070 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
0071 int ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
0072 LM_FLAG_ANY, &gh);
0073 if (ret)
0074 return ERR_PTR(ret);
0075 need_unlock = true;
0076 }
0077 acl = __gfs2_get_acl(inode, type);
0078 if (need_unlock)
0079 gfs2_glock_dq_uninit(&gh);
0080 return acl;
0081 }
0082
0083 int __gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
0084 {
0085 int error;
0086 size_t len;
0087 char *data;
0088 const char *name = gfs2_acl_name(type);
0089
0090 if (acl) {
0091 len = posix_acl_xattr_size(acl->a_count);
0092 data = kmalloc(len, GFP_NOFS);
0093 if (data == NULL)
0094 return -ENOMEM;
0095 error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
0096 if (error < 0)
0097 goto out;
0098 } else {
0099 data = NULL;
0100 len = 0;
0101 }
0102
0103 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
0104 if (error)
0105 goto out;
0106 set_cached_acl(inode, type, acl);
0107 out:
0108 kfree(data);
0109 return error;
0110 }
0111
0112 int gfs2_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
0113 struct posix_acl *acl, int type)
0114 {
0115 struct gfs2_inode *ip = GFS2_I(inode);
0116 struct gfs2_holder gh;
0117 bool need_unlock = false;
0118 int ret;
0119 umode_t mode;
0120
0121 if (acl && acl->a_count > GFS2_ACL_MAX_ENTRIES(GFS2_SB(inode)))
0122 return -E2BIG;
0123
0124 ret = gfs2_qa_get(ip);
0125 if (ret)
0126 return ret;
0127
0128 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
0129 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
0130 if (ret)
0131 goto out;
0132 need_unlock = true;
0133 }
0134
0135 mode = inode->i_mode;
0136 if (type == ACL_TYPE_ACCESS && acl) {
0137 ret = posix_acl_update_mode(&init_user_ns, inode, &mode, &acl);
0138 if (ret)
0139 goto unlock;
0140 }
0141
0142 ret = __gfs2_set_acl(inode, acl, type);
0143 if (!ret && mode != inode->i_mode) {
0144 inode->i_ctime = current_time(inode);
0145 inode->i_mode = mode;
0146 mark_inode_dirty(inode);
0147 }
0148 unlock:
0149 if (need_unlock)
0150 gfs2_glock_dq_uninit(&gh);
0151 out:
0152 gfs2_qa_put(ip);
0153 return ret;
0154 }