1/* 2 * Copyright (c) 2008, Christoph Hellwig 3 * All Rights Reserved. 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it would be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 */ 18#include "xfs.h" 19#include "xfs_format.h" 20#include "xfs_log_format.h" 21#include "xfs_trans_resv.h" 22#include "xfs_mount.h" 23#include "xfs_inode.h" 24#include "xfs_acl.h" 25#include "xfs_attr.h" 26#include "xfs_trace.h" 27#include <linux/slab.h> 28#include <linux/xattr.h> 29#include <linux/posix_acl_xattr.h> 30 31 32/* 33 * Locking scheme: 34 * - all ACL updates are protected by inode->i_mutex, which is taken before 35 * calling into this file. 36 */ 37 38STATIC struct posix_acl * 39xfs_acl_from_disk( 40 struct xfs_acl *aclp, 41 int max_entries) 42{ 43 struct posix_acl_entry *acl_e; 44 struct posix_acl *acl; 45 struct xfs_acl_entry *ace; 46 unsigned int count, i; 47 48 count = be32_to_cpu(aclp->acl_cnt); 49 if (count > max_entries) 50 return ERR_PTR(-EFSCORRUPTED); 51 52 acl = posix_acl_alloc(count, GFP_KERNEL); 53 if (!acl) 54 return ERR_PTR(-ENOMEM); 55 56 for (i = 0; i < count; i++) { 57 acl_e = &acl->a_entries[i]; 58 ace = &aclp->acl_entry[i]; 59 60 /* 61 * The tag is 32 bits on disk and 16 bits in core. 62 * 63 * Because every access to it goes through the core 64 * format first this is not a problem. 65 */ 66 acl_e->e_tag = be32_to_cpu(ace->ae_tag); 67 acl_e->e_perm = be16_to_cpu(ace->ae_perm); 68 69 switch (acl_e->e_tag) { 70 case ACL_USER: 71 acl_e->e_uid = xfs_uid_to_kuid(be32_to_cpu(ace->ae_id)); 72 break; 73 case ACL_GROUP: 74 acl_e->e_gid = xfs_gid_to_kgid(be32_to_cpu(ace->ae_id)); 75 break; 76 case ACL_USER_OBJ: 77 case ACL_GROUP_OBJ: 78 case ACL_MASK: 79 case ACL_OTHER: 80 break; 81 default: 82 goto fail; 83 } 84 } 85 return acl; 86 87fail: 88 posix_acl_release(acl); 89 return ERR_PTR(-EINVAL); 90} 91 92STATIC void 93xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl) 94{ 95 const struct posix_acl_entry *acl_e; 96 struct xfs_acl_entry *ace; 97 int i; 98 99 aclp->acl_cnt = cpu_to_be32(acl->a_count); 100 for (i = 0; i < acl->a_count; i++) { 101 ace = &aclp->acl_entry[i]; 102 acl_e = &acl->a_entries[i]; 103 104 ace->ae_tag = cpu_to_be32(acl_e->e_tag); 105 switch (acl_e->e_tag) { 106 case ACL_USER: 107 ace->ae_id = cpu_to_be32(xfs_kuid_to_uid(acl_e->e_uid)); 108 break; 109 case ACL_GROUP: 110 ace->ae_id = cpu_to_be32(xfs_kgid_to_gid(acl_e->e_gid)); 111 break; 112 default: 113 ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID); 114 break; 115 } 116 117 ace->ae_perm = cpu_to_be16(acl_e->e_perm); 118 } 119} 120 121struct posix_acl * 122xfs_get_acl(struct inode *inode, int type) 123{ 124 struct xfs_inode *ip = XFS_I(inode); 125 struct posix_acl *acl = NULL; 126 struct xfs_acl *xfs_acl; 127 unsigned char *ea_name; 128 int error; 129 int len; 130 131 trace_xfs_get_acl(ip); 132 133 switch (type) { 134 case ACL_TYPE_ACCESS: 135 ea_name = SGI_ACL_FILE; 136 break; 137 case ACL_TYPE_DEFAULT: 138 ea_name = SGI_ACL_DEFAULT; 139 break; 140 default: 141 BUG(); 142 } 143 144 /* 145 * If we have a cached ACLs value just return it, not need to 146 * go out to the disk. 147 */ 148 len = XFS_ACL_MAX_SIZE(ip->i_mount); 149 xfs_acl = kmem_zalloc_large(len, KM_SLEEP); 150 if (!xfs_acl) 151 return ERR_PTR(-ENOMEM); 152 153 error = xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl, 154 &len, ATTR_ROOT); 155 if (error) { 156 /* 157 * If the attribute doesn't exist make sure we have a negative 158 * cache entry, for any other error assume it is transient and 159 * leave the cache entry as ACL_NOT_CACHED. 160 */ 161 if (error == -ENOATTR) 162 goto out_update_cache; 163 goto out; 164 } 165 166 acl = xfs_acl_from_disk(xfs_acl, XFS_ACL_MAX_ENTRIES(ip->i_mount)); 167 if (IS_ERR(acl)) 168 goto out; 169 170out_update_cache: 171 set_cached_acl(inode, type, acl); 172out: 173 kmem_free(xfs_acl); 174 return acl; 175} 176 177STATIC int 178__xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl) 179{ 180 struct xfs_inode *ip = XFS_I(inode); 181 unsigned char *ea_name; 182 int error; 183 184 switch (type) { 185 case ACL_TYPE_ACCESS: 186 ea_name = SGI_ACL_FILE; 187 break; 188 case ACL_TYPE_DEFAULT: 189 if (!S_ISDIR(inode->i_mode)) 190 return acl ? -EACCES : 0; 191 ea_name = SGI_ACL_DEFAULT; 192 break; 193 default: 194 return -EINVAL; 195 } 196 197 if (acl) { 198 struct xfs_acl *xfs_acl; 199 int len = XFS_ACL_MAX_SIZE(ip->i_mount); 200 201 xfs_acl = kmem_zalloc_large(len, KM_SLEEP); 202 if (!xfs_acl) 203 return -ENOMEM; 204 205 xfs_acl_to_disk(xfs_acl, acl); 206 207 /* subtract away the unused acl entries */ 208 len -= sizeof(struct xfs_acl_entry) * 209 (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count); 210 211 error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl, 212 len, ATTR_ROOT); 213 214 kmem_free(xfs_acl); 215 } else { 216 /* 217 * A NULL ACL argument means we want to remove the ACL. 218 */ 219 error = xfs_attr_remove(ip, ea_name, ATTR_ROOT); 220 221 /* 222 * If the attribute didn't exist to start with that's fine. 223 */ 224 if (error == -ENOATTR) 225 error = 0; 226 } 227 228 if (!error) 229 set_cached_acl(inode, type, acl); 230 return error; 231} 232 233static int 234xfs_set_mode(struct inode *inode, umode_t mode) 235{ 236 int error = 0; 237 238 if (mode != inode->i_mode) { 239 struct iattr iattr; 240 241 iattr.ia_valid = ATTR_MODE | ATTR_CTIME; 242 iattr.ia_mode = mode; 243 iattr.ia_ctime = current_fs_time(inode->i_sb); 244 245 error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL); 246 } 247 248 return error; 249} 250 251static int 252xfs_acl_exists(struct inode *inode, unsigned char *name) 253{ 254 int len = XFS_ACL_MAX_SIZE(XFS_M(inode->i_sb)); 255 256 return (xfs_attr_get(XFS_I(inode), name, NULL, &len, 257 ATTR_ROOT|ATTR_KERNOVAL) == 0); 258} 259 260int 261posix_acl_access_exists(struct inode *inode) 262{ 263 return xfs_acl_exists(inode, SGI_ACL_FILE); 264} 265 266int 267posix_acl_default_exists(struct inode *inode) 268{ 269 if (!S_ISDIR(inode->i_mode)) 270 return 0; 271 return xfs_acl_exists(inode, SGI_ACL_DEFAULT); 272} 273 274int 275xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) 276{ 277 int error = 0; 278 279 if (!acl) 280 goto set_acl; 281 282 error = -E2BIG; 283 if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb))) 284 return error; 285 286 if (type == ACL_TYPE_ACCESS) { 287 umode_t mode = inode->i_mode; 288 error = posix_acl_equiv_mode(acl, &mode); 289 290 if (error <= 0) { 291 acl = NULL; 292 293 if (error < 0) 294 return error; 295 } 296 297 error = xfs_set_mode(inode, mode); 298 if (error) 299 return error; 300 } 301 302 set_acl: 303 return __xfs_set_acl(inode, type, acl); 304} 305