1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) International Business Machines Corp., 2002-2004 4 * Copyright (C) Andreas Gruenbacher, 2001 5 * Copyright (C) Linus Torvalds, 1991, 1992 6 */ 7 8 #include <linux/sched.h> 9 #include <linux/slab.h> 10 #include <linux/fs.h> 11 #include <linux/posix_acl_xattr.h> 12 #include "jfs_incore.h" 13 #include "jfs_txnmgr.h" 14 #include "jfs_xattr.h" 15 #include "jfs_acl.h" 16 17 struct posix_acl *jfs_get_acl(struct inode *inode, int type, bool rcu) 18 { 19 struct posix_acl *acl; 20 char *ea_name; 21 int size; 22 char *value = NULL; 23 24 if (rcu) 25 return ERR_PTR(-ECHILD); 26 27 switch(type) { 28 case ACL_TYPE_ACCESS: 29 ea_name = XATTR_NAME_POSIX_ACL_ACCESS; 30 break; 31 case ACL_TYPE_DEFAULT: 32 ea_name = XATTR_NAME_POSIX_ACL_DEFAULT; 33 break; 34 default: 35 return ERR_PTR(-EINVAL); 36 } 37 38 size = __jfs_getxattr(inode, ea_name, NULL, 0); 39 40 if (size > 0) { 41 value = kmalloc(size, GFP_KERNEL); 42 if (!value) 43 return ERR_PTR(-ENOMEM); 44 size = __jfs_getxattr(inode, ea_name, value, size); 45 } 46 47 if (size < 0) { 48 if (size == -ENODATA) 49 acl = NULL; 50 else 51 acl = ERR_PTR(size); 52 } else { 53 acl = posix_acl_from_xattr(&init_user_ns, value, size); 54 } 55 kfree(value); 56 return acl; 57 } 58 59 static int __jfs_set_acl(tid_t tid, struct inode *inode, int type, 60 struct posix_acl *acl) 61 { 62 char *ea_name; 63 int rc; 64 size_t size = 0; 65 char *value = NULL; 66 67 switch (type) { 68 case ACL_TYPE_ACCESS: 69 ea_name = XATTR_NAME_POSIX_ACL_ACCESS; 70 break; 71 case ACL_TYPE_DEFAULT: 72 ea_name = XATTR_NAME_POSIX_ACL_DEFAULT; 73 break; 74 default: 75 return -EINVAL; 76 } 77 78 if (acl) { 79 value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_KERNEL); 80 if (!value) 81 return -ENOMEM; 82 } 83 rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0); 84 kfree(value); 85 86 if (!rc) 87 set_cached_acl(inode, type, acl); 88 89 return rc; 90 } 91 92 int jfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 93 struct posix_acl *acl, int type) 94 { 95 int rc; 96 tid_t tid; 97 int update_mode = 0; 98 struct inode *inode = d_inode(dentry); 99 umode_t mode = inode->i_mode; 100 101 tid = txBegin(inode->i_sb, 0); 102 mutex_lock(&JFS_IP(inode)->commit_mutex); 103 if (type == ACL_TYPE_ACCESS && acl) { 104 rc = posix_acl_update_mode(&nop_mnt_idmap, inode, &mode, &acl); 105 if (rc) 106 goto end_tx; 107 if (mode != inode->i_mode) 108 update_mode = 1; 109 } 110 rc = __jfs_set_acl(tid, inode, type, acl); 111 if (!rc) { 112 if (update_mode) { 113 inode->i_mode = mode; 114 inode_set_ctime_current(inode); 115 mark_inode_dirty(inode); 116 } 117 rc = txCommit(tid, 1, &inode, 0); 118 } 119 end_tx: 120 txEnd(tid); 121 mutex_unlock(&JFS_IP(inode)->commit_mutex); 122 return rc; 123 } 124 125 int jfs_init_acl(tid_t tid, struct inode *inode, struct inode *dir) 126 { 127 struct posix_acl *default_acl, *acl; 128 int rc = 0; 129 130 rc = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); 131 if (rc) 132 return rc; 133 134 if (default_acl) { 135 rc = __jfs_set_acl(tid, inode, ACL_TYPE_DEFAULT, default_acl); 136 posix_acl_release(default_acl); 137 } else { 138 inode->i_default_acl = NULL; 139 } 140 141 if (acl) { 142 if (!rc) 143 rc = __jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, acl); 144 posix_acl_release(acl); 145 } else { 146 inode->i_acl = NULL; 147 } 148 149 JFS_IP(inode)->mode2 = (JFS_IP(inode)->mode2 & 0xffff0000) | 150 inode->i_mode; 151 152 return rc; 153 } 154