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" |
| 19 | #include "xfs_acl.h" |
| 20 | #include "xfs_attr.h" |
| 21 | #include "xfs_bmap_btree.h" |
| 22 | #include "xfs_inode.h" |
| 23 | #include "xfs_vnodeops.h" |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 24 | #include "xfs_sb.h" |
| 25 | #include "xfs_mount.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: |
| 71 | case ACL_GROUP: |
| 72 | acl_e->e_id = be32_to_cpu(ace->ae_id); |
| 73 | break; |
| 74 | case ACL_USER_OBJ: |
| 75 | case ACL_GROUP_OBJ: |
| 76 | case ACL_MASK: |
| 77 | case ACL_OTHER: |
| 78 | acl_e->e_id = ACL_UNDEFINED_ID; |
| 79 | break; |
| 80 | default: |
| 81 | goto fail; |
| 82 | } |
| 83 | } |
| 84 | return acl; |
| 85 | |
| 86 | fail: |
| 87 | posix_acl_release(acl); |
| 88 | return ERR_PTR(-EINVAL); |
| 89 | } |
| 90 | |
| 91 | STATIC void |
| 92 | xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl) |
| 93 | { |
| 94 | const struct posix_acl_entry *acl_e; |
| 95 | struct xfs_acl_entry *ace; |
| 96 | int i; |
| 97 | |
| 98 | aclp->acl_cnt = cpu_to_be32(acl->a_count); |
| 99 | for (i = 0; i < acl->a_count; i++) { |
| 100 | ace = &aclp->acl_entry[i]; |
| 101 | acl_e = &acl->a_entries[i]; |
| 102 | |
| 103 | ace->ae_tag = cpu_to_be32(acl_e->e_tag); |
| 104 | ace->ae_id = cpu_to_be32(acl_e->e_id); |
| 105 | ace->ae_perm = cpu_to_be16(acl_e->e_perm); |
| 106 | } |
| 107 | } |
| 108 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 109 | struct posix_acl * |
| 110 | xfs_get_acl(struct inode *inode, int type) |
| 111 | { |
| 112 | struct xfs_inode *ip = XFS_I(inode); |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 113 | struct posix_acl *acl; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 114 | struct xfs_acl *xfs_acl; |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 115 | unsigned char *ea_name; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 116 | int error; |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 117 | int len; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 118 | |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 119 | acl = get_cached_acl(inode, type); |
| 120 | if (acl != ACL_NOT_CACHED) |
| 121 | return acl; |
| 122 | |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 123 | trace_xfs_get_acl(ip); |
| 124 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 125 | switch (type) { |
| 126 | case ACL_TYPE_ACCESS: |
| 127 | ea_name = SGI_ACL_FILE; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 128 | break; |
| 129 | case ACL_TYPE_DEFAULT: |
| 130 | ea_name = SGI_ACL_DEFAULT; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 131 | break; |
| 132 | default: |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 133 | BUG(); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 136 | /* |
| 137 | * If we have a cached ACLs value just return it, not need to |
| 138 | * go out to the disk. |
| 139 | */ |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 140 | len = XFS_ACL_MAX_SIZE(ip->i_mount); |
| 141 | xfs_acl = kzalloc(len, GFP_KERNEL); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 142 | if (!xfs_acl) |
| 143 | return ERR_PTR(-ENOMEM); |
| 144 | |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 145 | error = -xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl, |
| 146 | &len, ATTR_ROOT); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 147 | if (error) { |
| 148 | /* |
| 149 | * If the attribute doesn't exist make sure we have a negative |
| 150 | * cache entry, for any other error assume it is transient and |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 151 | * leave the cache entry as ACL_NOT_CACHED. |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 152 | */ |
| 153 | if (error == -ENOATTR) { |
| 154 | acl = NULL; |
| 155 | goto out_update_cache; |
| 156 | } |
| 157 | goto out; |
| 158 | } |
| 159 | |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 160 | 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] | 161 | if (IS_ERR(acl)) |
| 162 | goto out; |
| 163 | |
| 164 | out_update_cache: |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 165 | set_cached_acl(inode, type, acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 166 | out: |
| 167 | kfree(xfs_acl); |
| 168 | return acl; |
| 169 | } |
| 170 | |
| 171 | STATIC int |
| 172 | xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl) |
| 173 | { |
| 174 | struct xfs_inode *ip = XFS_I(inode); |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 175 | unsigned char *ea_name; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 176 | int error; |
| 177 | |
| 178 | if (S_ISLNK(inode->i_mode)) |
| 179 | return -EOPNOTSUPP; |
| 180 | |
| 181 | switch (type) { |
| 182 | case ACL_TYPE_ACCESS: |
| 183 | ea_name = SGI_ACL_FILE; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 184 | break; |
| 185 | case ACL_TYPE_DEFAULT: |
| 186 | if (!S_ISDIR(inode->i_mode)) |
| 187 | return acl ? -EACCES : 0; |
| 188 | ea_name = SGI_ACL_DEFAULT; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 189 | break; |
| 190 | default: |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | if (acl) { |
| 195 | struct xfs_acl *xfs_acl; |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 196 | int len = XFS_ACL_MAX_SIZE(ip->i_mount); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 197 | |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 198 | xfs_acl = kzalloc(len, GFP_KERNEL); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 199 | if (!xfs_acl) |
| 200 | return -ENOMEM; |
| 201 | |
| 202 | xfs_acl_to_disk(xfs_acl, acl); |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 203 | |
| 204 | /* subtract away the unused acl entries */ |
| 205 | len -= sizeof(struct xfs_acl_entry) * |
| 206 | (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 207 | |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 208 | error = -xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl, |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 209 | len, ATTR_ROOT); |
| 210 | |
| 211 | kfree(xfs_acl); |
| 212 | } else { |
| 213 | /* |
| 214 | * A NULL ACL argument means we want to remove the ACL. |
| 215 | */ |
| 216 | error = -xfs_attr_remove(ip, ea_name, ATTR_ROOT); |
| 217 | |
| 218 | /* |
| 219 | * If the attribute didn't exist to start with that's fine. |
| 220 | */ |
| 221 | if (error == -ENOATTR) |
| 222 | error = 0; |
| 223 | } |
| 224 | |
| 225 | if (!error) |
Al Viro | 1cbd20d | 2009-06-09 13:29:39 -0400 | [diff] [blame] | 226 | set_cached_acl(inode, type, acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 227 | return error; |
| 228 | } |
| 229 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 230 | static int |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 231 | xfs_set_mode(struct inode *inode, umode_t mode) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 232 | { |
| 233 | int error = 0; |
| 234 | |
| 235 | if (mode != inode->i_mode) { |
| 236 | struct iattr iattr; |
| 237 | |
Christoph Hellwig | d6d59ba | 2009-12-23 16:09:13 +0000 | [diff] [blame] | 238 | iattr.ia_valid = ATTR_MODE | ATTR_CTIME; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 239 | iattr.ia_mode = mode; |
Christoph Hellwig | d6d59ba | 2009-12-23 16:09:13 +0000 | [diff] [blame] | 240 | iattr.ia_ctime = current_fs_time(inode->i_sb); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 241 | |
Christoph Hellwig | c4ed424 | 2011-07-08 14:34:23 +0200 | [diff] [blame] | 242 | error = -xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return error; |
| 246 | } |
| 247 | |
| 248 | static int |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 249 | xfs_acl_exists(struct inode *inode, unsigned char *name) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 250 | { |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 251 | int len = XFS_ACL_MAX_SIZE(XFS_M(inode->i_sb)); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 252 | |
| 253 | return (xfs_attr_get(XFS_I(inode), name, NULL, &len, |
| 254 | ATTR_ROOT|ATTR_KERNOVAL) == 0); |
| 255 | } |
| 256 | |
| 257 | int |
| 258 | posix_acl_access_exists(struct inode *inode) |
| 259 | { |
| 260 | return xfs_acl_exists(inode, SGI_ACL_FILE); |
| 261 | } |
| 262 | |
| 263 | int |
| 264 | posix_acl_default_exists(struct inode *inode) |
| 265 | { |
| 266 | if (!S_ISDIR(inode->i_mode)) |
| 267 | return 0; |
| 268 | return xfs_acl_exists(inode, SGI_ACL_DEFAULT); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * No need for i_mutex because the inode is not yet exposed to the VFS. |
| 273 | */ |
| 274 | int |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 275 | xfs_inherit_acl(struct inode *inode, struct posix_acl *acl) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 276 | { |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 277 | umode_t mode = inode->i_mode; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 278 | int error = 0, inherit = 0; |
| 279 | |
| 280 | if (S_ISDIR(inode->i_mode)) { |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 281 | error = xfs_set_acl(inode, ACL_TYPE_DEFAULT, acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 282 | if (error) |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 283 | goto out; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 286 | error = posix_acl_create(&acl, GFP_KERNEL, &mode); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 287 | if (error < 0) |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 288 | return error; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 289 | |
| 290 | /* |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 291 | * If posix_acl_create returns a positive value we need to |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 292 | * inherit a permission that can't be represented using the Unix |
| 293 | * mode bits and we actually need to set an ACL. |
| 294 | */ |
| 295 | if (error > 0) |
| 296 | inherit = 1; |
| 297 | |
| 298 | error = xfs_set_mode(inode, mode); |
| 299 | if (error) |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 300 | goto out; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 301 | |
| 302 | if (inherit) |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 303 | error = xfs_set_acl(inode, ACL_TYPE_ACCESS, acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 304 | |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 305 | out: |
| 306 | posix_acl_release(acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 307 | return error; |
| 308 | } |
| 309 | |
| 310 | int |
| 311 | xfs_acl_chmod(struct inode *inode) |
| 312 | { |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 313 | struct posix_acl *acl; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 314 | int error; |
| 315 | |
| 316 | if (S_ISLNK(inode->i_mode)) |
| 317 | return -EOPNOTSUPP; |
| 318 | |
| 319 | acl = xfs_get_acl(inode, ACL_TYPE_ACCESS); |
| 320 | if (IS_ERR(acl) || !acl) |
| 321 | return PTR_ERR(acl); |
| 322 | |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 323 | error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode); |
| 324 | if (error) |
| 325 | return error; |
| 326 | |
| 327 | error = xfs_set_acl(inode, ACL_TYPE_ACCESS, acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 328 | posix_acl_release(acl); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 329 | return error; |
| 330 | } |
| 331 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 332 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 333 | xfs_xattr_acl_get(struct dentry *dentry, const char *name, |
| 334 | void *value, size_t size, int type) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 335 | { |
| 336 | struct posix_acl *acl; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 337 | int error; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 338 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 339 | acl = xfs_get_acl(dentry->d_inode, type); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 340 | if (IS_ERR(acl)) |
| 341 | return PTR_ERR(acl); |
| 342 | if (acl == NULL) |
| 343 | return -ENODATA; |
| 344 | |
Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 345 | error = posix_acl_to_xattr(&init_user_ns, acl, value, size); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 346 | posix_acl_release(acl); |
| 347 | |
| 348 | return error; |
| 349 | } |
| 350 | |
| 351 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 352 | xfs_xattr_acl_set(struct dentry *dentry, const char *name, |
| 353 | const void *value, size_t size, int flags, int type) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 354 | { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 355 | struct inode *inode = dentry->d_inode; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 356 | struct posix_acl *acl = NULL; |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 357 | int error = 0; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 358 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 359 | if (flags & XATTR_CREATE) |
| 360 | return -EINVAL; |
| 361 | if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) |
| 362 | return value ? -EACCES : 0; |
| 363 | if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER)) |
| 364 | return -EPERM; |
| 365 | |
| 366 | if (!value) |
| 367 | goto set_acl; |
| 368 | |
Eric W. Biederman | 5f3a4a2 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 369 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 370 | if (!acl) { |
| 371 | /* |
| 372 | * acl_set_file(3) may request that we set default ACLs with |
| 373 | * zero length -- defend (gracefully) against that here. |
| 374 | */ |
| 375 | goto out; |
| 376 | } |
| 377 | if (IS_ERR(acl)) { |
| 378 | error = PTR_ERR(acl); |
| 379 | goto out; |
| 380 | } |
| 381 | |
| 382 | error = posix_acl_valid(acl); |
| 383 | if (error) |
| 384 | goto out_release; |
| 385 | |
| 386 | error = -EINVAL; |
Dave Chinner | 0a8aa19 | 2013-06-05 12:09:10 +1000 | [diff] [blame] | 387 | if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb))) |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 388 | goto out_release; |
| 389 | |
| 390 | if (type == ACL_TYPE_ACCESS) { |
Al Viro | d695212 | 2011-07-23 18:56:36 -0400 | [diff] [blame] | 391 | umode_t mode = inode->i_mode; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 392 | error = posix_acl_equiv_mode(acl, &mode); |
| 393 | |
| 394 | if (error <= 0) { |
| 395 | posix_acl_release(acl); |
| 396 | acl = NULL; |
| 397 | |
| 398 | if (error < 0) |
| 399 | return error; |
| 400 | } |
| 401 | |
| 402 | error = xfs_set_mode(inode, mode); |
| 403 | if (error) |
| 404 | goto out_release; |
| 405 | } |
| 406 | |
| 407 | set_acl: |
| 408 | error = xfs_set_acl(inode, type, acl); |
| 409 | out_release: |
| 410 | posix_acl_release(acl); |
| 411 | out: |
| 412 | return error; |
| 413 | } |
| 414 | |
Stephen Hemminger | 46e5876 | 2010-05-13 17:53:20 -0700 | [diff] [blame] | 415 | const struct xattr_handler xfs_xattr_acl_access_handler = { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 416 | .prefix = POSIX_ACL_XATTR_ACCESS, |
| 417 | .flags = ACL_TYPE_ACCESS, |
| 418 | .get = xfs_xattr_acl_get, |
| 419 | .set = xfs_xattr_acl_set, |
| 420 | }; |
| 421 | |
Stephen Hemminger | 46e5876 | 2010-05-13 17:53:20 -0700 | [diff] [blame] | 422 | const struct xattr_handler xfs_xattr_acl_default_handler = { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 423 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
| 424 | .flags = ACL_TYPE_DEFAULT, |
| 425 | .get = xfs_xattr_acl_get, |
| 426 | .set = xfs_xattr_acl_set, |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 427 | }; |