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