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