Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 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" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 19 | #include "xfs_format.h" |
Dave Chinner | 6943283 | 2013-08-12 20:49:23 +1000 | [diff] [blame] | 20 | #include "xfs_log_format.h" |
Dave Chinner | 7fd36c4 | 2013-08-12 20:49:32 +1000 | [diff] [blame] | 21 | #include "xfs_trans_resv.h" |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 22 | #include "xfs_mount.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 23 | #include "xfs_inode.h" |
| 24 | #include "xfs_acl.h" |
| 25 | #include "xfs_attr.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 26 | #include "xfs_trace.h" |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 28 | #include <linux/xattr.h> |
| 29 | #include <linux/posix_acl_xattr.h> |
| 30 | |
| 31 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 32 | /* |
| 33 | * Locking scheme: |
| 34 | * - all ACL updates are protected by inode->i_mutex, which is taken before |
| 35 | * calling into this file. |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | STATIC struct posix_acl * |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 39 | xfs_acl_from_disk( |
| 40 | struct xfs_acl *aclp, |
| 41 | int max_entries) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 42 | { |
| 43 | struct posix_acl_entry *acl_e; |
| 44 | struct posix_acl *acl; |
| 45 | struct xfs_acl_entry *ace; |
Xi Wang | 093019c | 2011-12-12 21:55:52 +0000 | [diff] [blame] | 46 | unsigned int count, i; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 47 | |
| 48 | count = be32_to_cpu(aclp->acl_cnt); |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 49 | if (count > max_entries) |
Christoph Hellwig | fa8b18e | 2011-11-20 15:35:32 +0000 | [diff] [blame] | 50 | return ERR_PTR(-EFSCORRUPTED); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 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: |
Dwight Engen | 288bbe0 | 2013-08-15 14:07:59 -0400 | [diff] [blame] | 71 | acl_e->e_uid = xfs_uid_to_kuid(be32_to_cpu(ace->ae_id)); |
| 72 | break; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 73 | case ACL_GROUP: |
Dwight Engen | 288bbe0 | 2013-08-15 14:07:59 -0400 | [diff] [blame] | 74 | acl_e->e_gid = xfs_gid_to_kgid(be32_to_cpu(ace->ae_id)); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 75 | break; |
| 76 | case ACL_USER_OBJ: |
| 77 | case ACL_GROUP_OBJ: |
| 78 | case ACL_MASK: |
| 79 | case ACL_OTHER: |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 80 | break; |
| 81 | default: |
| 82 | goto fail; |
| 83 | } |
| 84 | } |
| 85 | return acl; |
| 86 | |
| 87 | fail: |
| 88 | posix_acl_release(acl); |
| 89 | return ERR_PTR(-EINVAL); |
| 90 | } |
| 91 | |
| 92 | STATIC void |
| 93 | xfs_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); |
Dwight Engen | 288bbe0 | 2013-08-15 14:07:59 -0400 | [diff] [blame] | 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 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 117 | ace->ae_perm = cpu_to_be16(acl_e->e_perm); |
| 118 | } |
| 119 | } |
| 120 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 121 | struct posix_acl * |
| 122 | xfs_get_acl(struct inode *inode, int type) |
| 123 | { |
| 124 | struct xfs_inode *ip = XFS_I(inode); |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 125 | struct posix_acl *acl = NULL; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 126 | struct xfs_acl *xfs_acl; |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 127 | unsigned char *ea_name; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 128 | int error; |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 129 | int len; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 130 | |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 131 | trace_xfs_get_acl(ip); |
| 132 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 133 | switch (type) { |
| 134 | case ACL_TYPE_ACCESS: |
| 135 | ea_name = SGI_ACL_FILE; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 136 | break; |
| 137 | case ACL_TYPE_DEFAULT: |
| 138 | ea_name = SGI_ACL_DEFAULT; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 139 | break; |
| 140 | default: |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 141 | BUG(); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 144 | /* |
| 145 | * If we have a cached ACLs value just return it, not need to |
| 146 | * go out to the disk. |
| 147 | */ |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 148 | len = XFS_ACL_MAX_SIZE(ip->i_mount); |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 149 | xfs_acl = kmem_zalloc_large(len, KM_SLEEP); |
| 150 | if (!xfs_acl) |
| 151 | return ERR_PTR(-ENOMEM); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 152 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 153 | error = xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 154 | &len, ATTR_ROOT); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 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 |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 159 | * leave the cache entry as ACL_NOT_CACHED. |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 160 | */ |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 161 | if (error == -ENOATTR) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 162 | goto out_update_cache; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 163 | goto out; |
| 164 | } |
| 165 | |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 166 | acl = xfs_acl_from_disk(xfs_acl, XFS_ACL_MAX_ENTRIES(ip->i_mount)); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 167 | if (IS_ERR(acl)) |
| 168 | goto out; |
| 169 | |
Dave Chinner | 2dc164f | 2013-09-02 20:52:59 +1000 | [diff] [blame] | 170 | out_update_cache: |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 171 | set_cached_acl(inode, type, acl); |
Dave Chinner | 2dc164f | 2013-09-02 20:52:59 +1000 | [diff] [blame] | 172 | out: |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 173 | kmem_free(xfs_acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 174 | return acl; |
| 175 | } |
| 176 | |
| 177 | STATIC int |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 178 | __xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 179 | { |
| 180 | struct xfs_inode *ip = XFS_I(inode); |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 181 | unsigned char *ea_name; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 182 | int error; |
| 183 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 184 | switch (type) { |
| 185 | case ACL_TYPE_ACCESS: |
| 186 | ea_name = SGI_ACL_FILE; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 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; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 192 | break; |
| 193 | default: |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | |
| 197 | if (acl) { |
| 198 | struct xfs_acl *xfs_acl; |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 199 | int len = XFS_ACL_MAX_SIZE(ip->i_mount); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 200 | |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 201 | xfs_acl = kmem_zalloc_large(len, KM_SLEEP); |
| 202 | if (!xfs_acl) |
| 203 | return -ENOMEM; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 204 | |
| 205 | xfs_acl_to_disk(xfs_acl, acl); |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 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); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 210 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 211 | error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl, |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 212 | len, ATTR_ROOT); |
| 213 | |
Dave Chinner | fdd3cce | 2013-09-02 20:53:00 +1000 | [diff] [blame] | 214 | kmem_free(xfs_acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 215 | } else { |
| 216 | /* |
| 217 | * A NULL ACL argument means we want to remove the ACL. |
| 218 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 219 | error = xfs_attr_remove(ip, ea_name, ATTR_ROOT); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 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) |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 229 | set_cached_acl(inode, type, acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 230 | return error; |
| 231 | } |
| 232 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 233 | static int |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 234 | xfs_set_mode(struct inode *inode, umode_t mode) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 235 | { |
| 236 | int error = 0; |
| 237 | |
| 238 | if (mode != inode->i_mode) { |
| 239 | struct iattr iattr; |
| 240 | |
Christoph Hellwig | d6d59ba | 2009-12-23 16:09:13 +0000 | [diff] [blame] | 241 | iattr.ia_valid = ATTR_MODE | ATTR_CTIME; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 242 | iattr.ia_mode = mode; |
Christoph Hellwig | d6d59ba | 2009-12-23 16:09:13 +0000 | [diff] [blame] | 243 | iattr.ia_ctime = current_fs_time(inode->i_sb); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 244 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 245 | error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | return error; |
| 249 | } |
| 250 | |
| 251 | static int |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 252 | xfs_acl_exists(struct inode *inode, unsigned char *name) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 253 | { |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 254 | int len = XFS_ACL_MAX_SIZE(XFS_M(inode->i_sb)); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 255 | |
| 256 | return (xfs_attr_get(XFS_I(inode), name, NULL, &len, |
| 257 | ATTR_ROOT|ATTR_KERNOVAL) == 0); |
| 258 | } |
| 259 | |
| 260 | int |
| 261 | posix_acl_access_exists(struct inode *inode) |
| 262 | { |
| 263 | return xfs_acl_exists(inode, SGI_ACL_FILE); |
| 264 | } |
| 265 | |
| 266 | int |
| 267 | posix_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 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 274 | int |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 275 | xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 276 | { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 277 | int error = 0; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 278 | |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 279 | if (!acl) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 280 | goto set_acl; |
| 281 | |
Jie Liu | 4ae69fe | 2014-02-07 15:26:11 +1100 | [diff] [blame] | 282 | error = -E2BIG; |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 283 | if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb))) |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 284 | return error; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 285 | |
| 286 | if (type == ACL_TYPE_ACCESS) { |
Al Viro | d695212 | 2011-07-23 18:56:36 -0400 | [diff] [blame] | 287 | umode_t mode = inode->i_mode; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 288 | error = posix_acl_equiv_mode(acl, &mode); |
| 289 | |
| 290 | if (error <= 0) { |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 291 | acl = NULL; |
| 292 | |
| 293 | if (error < 0) |
| 294 | return error; |
| 295 | } |
| 296 | |
| 297 | error = xfs_set_mode(inode, mode); |
| 298 | if (error) |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 299 | return error; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | set_acl: |
Christoph Hellwig | 2401dc2 | 2013-12-20 05:16:50 -0800 | [diff] [blame] | 303 | return __xfs_set_acl(inode, type, acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 304 | } |