Merge branch 'for-3.10/core' of git://git.kernel.dk/linux-block
[linux-3.10.git] / fs / gfs2 / acl.c
index 60c98c0..f69ac0a 100644 (file)
 #include <linux/spinlock.h>
 #include <linux/completion.h>
 #include <linux/buffer_head.h>
+#include <linux/xattr.h>
 #include <linux/posix_acl.h>
 #include <linux/posix_acl_xattr.h>
 #include <linux/gfs2_ondisk.h>
 
 #include "gfs2.h"
-#include "lm_interface.h"
 #include "incore.h"
 #include "acl.h"
-#include "eaops.h"
-#include "eattr.h"
+#include "xattr.h"
 #include "glock.h"
 #include "inode.h"
 #include "meta_io.h"
 #include "trans.h"
 #include "util.h"
 
-#define ACL_ACCESS 1
-#define ACL_DEFAULT 0
+static const char *gfs2_acl_name(int type)
+{
+       switch (type) {
+       case ACL_TYPE_ACCESS:
+               return GFS2_POSIX_ACL_ACCESS;
+       case ACL_TYPE_DEFAULT:
+               return GFS2_POSIX_ACL_DEFAULT;
+       }
+       return NULL;
+}
 
-int gfs2_acl_validate_set(struct gfs2_inode *ip, int access,
-                     struct gfs2_ea_request *er,
-                     int *remove, mode_t *mode)
+struct posix_acl *gfs2_get_acl(struct inode *inode, int type)
 {
+       struct gfs2_inode *ip = GFS2_I(inode);
        struct posix_acl *acl;
-       int error;
+       const char *name;
+       char *data;
+       int len;
 
-       error = gfs2_acl_validate_remove(ip, access);
-       if (error)
-               return error;
+       if (!ip->i_eattr)
+               return NULL;
 
-       if (!er->er_data)
-               return -EINVAL;
+       acl = get_cached_acl(&ip->i_inode, type);
+       if (acl != ACL_NOT_CACHED)
+               return acl;
 
-       acl = posix_acl_from_xattr(er->er_data, er->er_data_len);
-       if (IS_ERR(acl))
-               return PTR_ERR(acl);
-       if (!acl) {
-               *remove = 1;
-               return 0;
-       }
+       name = gfs2_acl_name(type);
+       if (name == NULL)
+               return ERR_PTR(-EINVAL);
 
-       error = posix_acl_valid(acl);
-       if (error)
-               goto out;
+       len = gfs2_xattr_acl_get(ip, name, &data);
+       if (len < 0)
+               return ERR_PTR(len);
+       if (len == 0)
+               return NULL;
 
-       if (access) {
-               error = posix_acl_equiv_mode(acl, mode);
-               if (!error)
-                       *remove = 1;
-               else if (error > 0)
-                       error = 0;
-       }
+       acl = posix_acl_from_xattr(&init_user_ns, data, len);
+       kfree(data);
+       return acl;
+}
 
- out:
-       posix_acl_release(acl);
+static int gfs2_set_mode(struct inode *inode, umode_t mode)
+{
+       int error = 0;
+
+       if (mode != inode->i_mode) {
+               inode->i_mode = mode;
+               mark_inode_dirty(inode);
+       }
 
        return error;
 }
 
-int gfs2_acl_validate_remove(struct gfs2_inode *ip, int access)
+static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl)
 {
-       if (!GFS2_SB(&ip->i_inode)->sd_args.ar_posix_acl)
-               return -EOPNOTSUPP;
-       if (current->fsuid != ip->i_di.di_uid && !capable(CAP_FOWNER))
-               return -EPERM;
-       if (S_ISLNK(ip->i_di.di_mode))
-               return -EOPNOTSUPP;
-       if (!access && !S_ISDIR(ip->i_di.di_mode))
-               return -EACCES;
+       int error;
+       int len;
+       char *data;
+       const char *name = gfs2_acl_name(type);
 
-       return 0;
+       BUG_ON(name == NULL);
+       len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
+       if (len == 0)
+               return 0;
+       data = kmalloc(len, GFP_NOFS);
+       if (data == NULL)
+               return -ENOMEM;
+       error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
+       if (error < 0)
+               goto out;
+       error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
+       if (!error)
+               set_cached_acl(inode, type, acl);
+out:
+       kfree(data);
+       return error;
 }
 
-static int acl_get(struct gfs2_inode *ip, int access, struct posix_acl **acl,
-                  struct gfs2_ea_location *el, char **data, unsigned int *len)
+int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode)
 {
-       struct gfs2_ea_request er;
-       struct gfs2_ea_location el_this;
-       int error;
+       struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
+       struct posix_acl *acl;
+       umode_t mode = inode->i_mode;
+       int error = 0;
 
-       if (!ip->i_di.di_eattr)
+       if (!sdp->sd_args.ar_posix_acl)
+               return 0;
+       if (S_ISLNK(inode->i_mode))
                return 0;
 
-       memset(&er, 0, sizeof(struct gfs2_ea_request));
-       if (access) {
-               er.er_name = GFS2_POSIX_ACL_ACCESS;
-               er.er_name_len = GFS2_POSIX_ACL_ACCESS_LEN;
-       } else {
-               er.er_name = GFS2_POSIX_ACL_DEFAULT;
-               er.er_name_len = GFS2_POSIX_ACL_DEFAULT_LEN;
+       acl = gfs2_get_acl(&dip->i_inode, ACL_TYPE_DEFAULT);
+       if (IS_ERR(acl))
+               return PTR_ERR(acl);
+       if (!acl) {
+               mode &= ~current_umask();
+               return gfs2_set_mode(inode, mode);
        }
-       er.er_type = GFS2_EATYPE_SYS;
 
-       if (!el)
-               el = &el_this;
+       if (S_ISDIR(inode->i_mode)) {
+               error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl);
+               if (error)
+                       goto out;
+       }
 
-       error = gfs2_ea_find(ip, &er, el);
-       if (error)
+       error = posix_acl_create(&acl, GFP_NOFS, &mode);
+       if (error < 0)
                return error;
-       if (!el->el_ea)
-               return 0;
-       if (!GFS2_EA_DATA_LEN(el->el_ea))
-               goto out;
 
-       er.er_data_len = GFS2_EA_DATA_LEN(el->el_ea);
-       er.er_data = kmalloc(er.er_data_len, GFP_KERNEL);
-       error = -ENOMEM;
-       if (!er.er_data)
-               goto out;
+       if (error == 0)
+               goto munge;
 
-       error = gfs2_ea_get_copy(ip, el, er.er_data);
+       error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl);
        if (error)
-               goto out_kfree;
-
-       if (acl) {
-               *acl = posix_acl_from_xattr(er.er_data, er.er_data_len);
-               if (IS_ERR(*acl))
-                       error = PTR_ERR(*acl);
-       }
-
- out_kfree:
-       if (error || !data)
-               kfree(er.er_data);
-       else {
-               *data = er.er_data;
-               *len = er.er_data_len;
-       }
-
- out:
-       if (error || el == &el_this)
-               brelse(el->el_bh);
-
+               goto out;
+munge:
+       error = gfs2_set_mode(inode, mode);
+out:
+       posix_acl_release(acl);
        return error;
 }
 
-/**
- * gfs2_check_acl_locked - Check an ACL to see if we're allowed to do something
- * @inode: the file we want to do something to
- * @mask: what we want to do
- *
- * Returns: errno
- */
-
-int gfs2_check_acl_locked(struct inode *inode, int mask)
+int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
 {
-       struct posix_acl *acl = NULL;
+       struct inode *inode = &ip->i_inode;
+       struct posix_acl *acl;
+       char *data;
+       unsigned int len;
        int error;
 
-       error = acl_get(GFS2_I(inode), ACL_ACCESS, &acl, NULL, NULL, NULL);
+       acl = gfs2_get_acl(&ip->i_inode, ACL_TYPE_ACCESS);
+       if (IS_ERR(acl))
+               return PTR_ERR(acl);
+       if (!acl)
+               return gfs2_setattr_simple(inode, attr);
+
+       error = posix_acl_chmod(&acl, GFP_NOFS, attr->ia_mode);
        if (error)
                return error;
 
-       if (acl) {
-               error = posix_acl_permission(inode, acl, mask);
-               posix_acl_release(acl);
-               return error;
-       }
+       len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
+       data = kmalloc(len, GFP_NOFS);
+       error = -ENOMEM;
+       if (data == NULL)
+               goto out;
+       posix_acl_to_xattr(&init_user_ns, acl, data, len);
+       error = gfs2_xattr_acl_chmod(ip, attr, data);
+       kfree(data);
+       set_cached_acl(&ip->i_inode, ACL_TYPE_ACCESS, acl);
 
-       return -EAGAIN;
+out:
+       posix_acl_release(acl);
+       return error;
 }
 
-int gfs2_check_acl(struct inode *inode, int mask)
+static int gfs2_acl_type(const char *name)
 {
-       struct gfs2_inode *ip = GFS2_I(inode);
-       struct gfs2_holder i_gh;
-       int error;
-
-       error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
-       if (!error) {
-               error = gfs2_check_acl_locked(inode, mask);
-               gfs2_glock_dq_uninit(&i_gh);
-       }
-       
-       return error;
+       if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
+               return ACL_TYPE_ACCESS;
+       if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
+               return ACL_TYPE_DEFAULT;
+       return -EINVAL;
 }
 
-static int munge_mode(struct gfs2_inode *ip, mode_t mode)
+static int gfs2_xattr_system_get(struct dentry *dentry, const char *name,
+                                void *buffer, size_t size, int xtype)
 {
-       struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
-       struct buffer_head *dibh;
+       struct inode *inode = dentry->d_inode;
+       struct gfs2_sbd *sdp = GFS2_SB(inode);
+       struct posix_acl *acl;
+       int type;
        int error;
 
-       error = gfs2_trans_begin(sdp, RES_DINODE, 0);
-       if (error)
-               return error;
+       if (!sdp->sd_args.ar_posix_acl)
+               return -EOPNOTSUPP;
 
-       error = gfs2_meta_inode_buffer(ip, &dibh);
-       if (!error) {
-               gfs2_assert_withdraw(sdp,
-                               (ip->i_di.di_mode & S_IFMT) == (mode & S_IFMT));
-               ip->i_di.di_mode = mode;
-               gfs2_trans_add_bh(ip->i_gl, dibh, 1);
-               gfs2_dinode_out(&ip->i_di, dibh->b_data);
-               brelse(dibh);
-       }
+       type = gfs2_acl_type(name);
+       if (type < 0)
+               return type;
 
-       gfs2_trans_end(sdp);
+       acl = gfs2_get_acl(inode, type);
+       if (IS_ERR(acl))
+               return PTR_ERR(acl);
+       if (acl == NULL)
+               return -ENODATA;
+
+       error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
+       posix_acl_release(acl);
 
-       return 0;
+       return error;
 }
 
-int gfs2_acl_create(struct gfs2_inode *dip, struct gfs2_inode *ip)
+static int gfs2_xattr_system_set(struct dentry *dentry, const char *name,
+                                const void *value, size_t size, int flags,
+                                int xtype)
 {
-       struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
-       struct posix_acl *acl = NULL, *clone;
-       struct gfs2_ea_request er;
-       mode_t mode = ip->i_di.di_mode;
-       int error;
+       struct inode *inode = dentry->d_inode;
+       struct gfs2_sbd *sdp = GFS2_SB(inode);
+       struct posix_acl *acl = NULL;
+       int error = 0, type;
 
        if (!sdp->sd_args.ar_posix_acl)
-               return 0;
-       if (S_ISLNK(ip->i_di.di_mode))
-               return 0;
+               return -EOPNOTSUPP;
 
-       memset(&er, 0, sizeof(struct gfs2_ea_request));
-       er.er_type = GFS2_EATYPE_SYS;
+       type = gfs2_acl_type(name);
+       if (type < 0)
+               return type;
+       if (flags & XATTR_CREATE)
+               return -EINVAL;
+       if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
+               return value ? -EACCES : 0;
+       if (!uid_eq(current_fsuid(), inode->i_uid) && !capable(CAP_FOWNER))
+               return -EPERM;
+       if (S_ISLNK(inode->i_mode))
+               return -EOPNOTSUPP;
 
-       error = acl_get(dip, ACL_DEFAULT, &acl, NULL,
-                       &er.er_data, &er.er_data_len);
-       if (error)
-               return error;
+       if (!value)
+               goto set_acl;
+
+       acl = posix_acl_from_xattr(&init_user_ns, value, size);
        if (!acl) {
-               mode &= ~current->fs->umask;
-               if (mode != ip->i_di.di_mode)
-                       error = munge_mode(ip, mode);
-               return error;
+               /*
+                * acl_set_file(3) may request that we set default ACLs with
+                * zero length -- defend (gracefully) against that here.
+                */
+               goto out;
        }
-
-       clone = posix_acl_clone(acl, GFP_KERNEL);
-       error = -ENOMEM;
-       if (!clone)
+       if (IS_ERR(acl)) {
+               error = PTR_ERR(acl);
                goto out;
-       posix_acl_release(acl);
-       acl = clone;
-
-       if (S_ISDIR(ip->i_di.di_mode)) {
-               er.er_name = GFS2_POSIX_ACL_DEFAULT;
-               er.er_name_len = GFS2_POSIX_ACL_DEFAULT_LEN;
-               error = gfs2_system_eaops.eo_set(ip, &er);
-               if (error)
-                       goto out;
        }
 
-       error = posix_acl_create_masq(acl, &mode);
-       if (error < 0)
-               goto out;
-       if (error > 0) {
-               er.er_name = GFS2_POSIX_ACL_ACCESS;
-               er.er_name_len = GFS2_POSIX_ACL_ACCESS_LEN;
-               posix_acl_to_xattr(acl, er.er_data, er.er_data_len);
-               er.er_mode = mode;
-               er.er_flags = GFS2_ERF_MODE;
-               error = gfs2_system_eaops.eo_set(ip, &er);
-               if (error)
-                       goto out;
-       } else
-               munge_mode(ip, mode);
+       error = posix_acl_valid(acl);
+       if (error)
+               goto out_release;
 
- out:
-       posix_acl_release(acl);
-       kfree(er.er_data);
-       return error;
-}
+       error = -EINVAL;
+       if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
+               goto out_release;
 
-int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
-{
-       struct posix_acl *acl = NULL, *clone;
-       struct gfs2_ea_location el;
-       char *data;
-       unsigned int len;
-       int error;
+       if (type == ACL_TYPE_ACCESS) {
+               umode_t mode = inode->i_mode;
+               error = posix_acl_equiv_mode(acl, &mode);
 
-       error = acl_get(ip, ACL_ACCESS, &acl, &el, &data, &len);
-       if (error)
-               return error;
-       if (!acl)
-               return gfs2_setattr_simple(ip, attr);
+               if (error <= 0) {
+                       posix_acl_release(acl);
+                       acl = NULL;
 
-       clone = posix_acl_clone(acl, GFP_KERNEL);
-       error = -ENOMEM;
-       if (!clone)
-               goto out;
-       posix_acl_release(acl);
-       acl = clone;
+                       if (error < 0)
+                               return error;
+               }
 
-       error = posix_acl_chmod_masq(acl, attr->ia_mode);
-       if (!error) {
-               posix_acl_to_xattr(acl, data, len);
-               error = gfs2_ea_acl_chmod(ip, &el, attr, data);
+               error = gfs2_set_mode(inode, mode);
+               if (error)
+                       goto out_release;
        }
 
- out:
+set_acl:
+       error = __gfs2_xattr_set(inode, name, value, size, 0, GFS2_EATYPE_SYS);
+       if (!error) {
+               if (acl)
+                       set_cached_acl(inode, type, acl);
+               else
+                       forget_cached_acl(inode, type);
+       }
+out_release:
        posix_acl_release(acl);
-       brelse(el.el_bh);
-       kfree(data);
-
+out:
        return error;
 }
 
+const struct xattr_handler gfs2_xattr_system_handler = {
+       .prefix = XATTR_SYSTEM_PREFIX,
+       .flags  = GFS2_EATYPE_SYS,
+       .get    = gfs2_xattr_system_get,
+       .set    = gfs2_xattr_system_set,
+};
+