Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/acl.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/capability.h> |
| 11 | #include <linux/fs.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 12 | #include "ext4_jbd2.h" |
| 13 | #include "ext4.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 14 | #include "xattr.h" |
| 15 | #include "acl.h" |
| 16 | |
| 17 | /* |
| 18 | * Convert from filesystem to in-memory representation. |
| 19 | */ |
| 20 | static struct posix_acl * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 21 | ext4_acl_from_disk(const void *value, size_t size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 22 | { |
| 23 | const char *end = (char *)value + size; |
| 24 | int n, count; |
| 25 | struct posix_acl *acl; |
| 26 | |
| 27 | if (!value) |
| 28 | return NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 29 | if (size < sizeof(ext4_acl_header)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 30 | return ERR_PTR(-EINVAL); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 31 | if (((ext4_acl_header *)value)->a_version != |
| 32 | cpu_to_le32(EXT4_ACL_VERSION)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 33 | return ERR_PTR(-EINVAL); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 34 | value = (char *)value + sizeof(ext4_acl_header); |
| 35 | count = ext4_acl_count(size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 36 | if (count < 0) |
| 37 | return ERR_PTR(-EINVAL); |
| 38 | if (count == 0) |
| 39 | return NULL; |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 40 | acl = posix_acl_alloc(count, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 41 | if (!acl) |
| 42 | return ERR_PTR(-ENOMEM); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 43 | for (n = 0; n < count; n++) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 44 | ext4_acl_entry *entry = |
| 45 | (ext4_acl_entry *)value; |
| 46 | if ((char *)value + sizeof(ext4_acl_entry_short) > end) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 47 | goto fail; |
| 48 | acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); |
| 49 | acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 50 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 51 | switch (acl->a_entries[n].e_tag) { |
| 52 | case ACL_USER_OBJ: |
| 53 | case ACL_GROUP_OBJ: |
| 54 | case ACL_MASK: |
| 55 | case ACL_OTHER: |
| 56 | value = (char *)value + |
| 57 | sizeof(ext4_acl_entry_short); |
| 58 | acl->a_entries[n].e_id = ACL_UNDEFINED_ID; |
| 59 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 60 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 61 | case ACL_USER: |
| 62 | case ACL_GROUP: |
| 63 | value = (char *)value + sizeof(ext4_acl_entry); |
| 64 | if ((char *)value > end) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 65 | goto fail; |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 66 | acl->a_entries[n].e_id = |
| 67 | le32_to_cpu(entry->e_id); |
| 68 | break; |
| 69 | |
| 70 | default: |
| 71 | goto fail; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | if (value != end) |
| 75 | goto fail; |
| 76 | return acl; |
| 77 | |
| 78 | fail: |
| 79 | posix_acl_release(acl); |
| 80 | return ERR_PTR(-EINVAL); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Convert from in-memory to filesystem representation. |
| 85 | */ |
| 86 | static void * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 87 | ext4_acl_to_disk(const struct posix_acl *acl, size_t *size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 88 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 89 | ext4_acl_header *ext_acl; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 90 | char *e; |
| 91 | size_t n; |
| 92 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 93 | *size = ext4_acl_size(acl->a_count); |
| 94 | ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count * |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 95 | sizeof(ext4_acl_entry), GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 96 | if (!ext_acl) |
| 97 | return ERR_PTR(-ENOMEM); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 98 | ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION); |
| 99 | e = (char *)ext_acl + sizeof(ext4_acl_header); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 100 | for (n = 0; n < acl->a_count; n++) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 101 | ext4_acl_entry *entry = (ext4_acl_entry *)e; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 102 | entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag); |
| 103 | entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 104 | switch (acl->a_entries[n].e_tag) { |
| 105 | case ACL_USER: |
| 106 | case ACL_GROUP: |
| 107 | entry->e_id = cpu_to_le32(acl->a_entries[n].e_id); |
| 108 | e += sizeof(ext4_acl_entry); |
| 109 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 110 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 111 | case ACL_USER_OBJ: |
| 112 | case ACL_GROUP_OBJ: |
| 113 | case ACL_MASK: |
| 114 | case ACL_OTHER: |
| 115 | e += sizeof(ext4_acl_entry_short); |
| 116 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 117 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 118 | default: |
| 119 | goto fail; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | return (char *)ext_acl; |
| 123 | |
| 124 | fail: |
| 125 | kfree(ext_acl); |
| 126 | return ERR_PTR(-EINVAL); |
| 127 | } |
| 128 | |
| 129 | static inline struct posix_acl * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 130 | ext4_iget_acl(struct inode *inode, struct posix_acl **i_acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 131 | { |
Theodore Ts'o | 210ad6a | 2009-06-08 15:22:25 -0400 | [diff] [blame] | 132 | struct posix_acl *acl = ACCESS_ONCE(*i_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 133 | |
Theodore Ts'o | 210ad6a | 2009-06-08 15:22:25 -0400 | [diff] [blame] | 134 | if (acl) { |
| 135 | spin_lock(&inode->i_lock); |
| 136 | acl = *i_acl; |
| 137 | if (acl != EXT4_ACL_NOT_CACHED) |
| 138 | acl = posix_acl_dup(acl); |
| 139 | spin_unlock(&inode->i_lock); |
| 140 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 141 | |
| 142 | return acl; |
| 143 | } |
| 144 | |
| 145 | static inline void |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 146 | ext4_iset_acl(struct inode *inode, struct posix_acl **i_acl, |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 147 | struct posix_acl *acl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 148 | { |
| 149 | spin_lock(&inode->i_lock); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 150 | if (*i_acl != EXT4_ACL_NOT_CACHED) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 151 | posix_acl_release(*i_acl); |
| 152 | *i_acl = posix_acl_dup(acl); |
| 153 | spin_unlock(&inode->i_lock); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Inode operation get_posix_acl(). |
| 158 | * |
| 159 | * inode->i_mutex: don't care |
| 160 | */ |
| 161 | static struct posix_acl * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 162 | ext4_get_acl(struct inode *inode, int type) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 163 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 164 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 165 | int name_index; |
| 166 | char *value = NULL; |
| 167 | struct posix_acl *acl; |
| 168 | int retval; |
| 169 | |
| 170 | if (!test_opt(inode->i_sb, POSIX_ACL)) |
| 171 | return NULL; |
| 172 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 173 | switch (type) { |
| 174 | case ACL_TYPE_ACCESS: |
| 175 | acl = ext4_iget_acl(inode, &ei->i_acl); |
| 176 | if (acl != EXT4_ACL_NOT_CACHED) |
| 177 | return acl; |
| 178 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; |
| 179 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 180 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 181 | case ACL_TYPE_DEFAULT: |
| 182 | acl = ext4_iget_acl(inode, &ei->i_default_acl); |
| 183 | if (acl != EXT4_ACL_NOT_CACHED) |
| 184 | return acl; |
| 185 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 186 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 187 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 188 | default: |
| 189 | return ERR_PTR(-EINVAL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 190 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 191 | retval = ext4_xattr_get(inode, name_index, "", NULL, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 192 | if (retval > 0) { |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 193 | value = kmalloc(retval, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 194 | if (!value) |
| 195 | return ERR_PTR(-ENOMEM); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 196 | retval = ext4_xattr_get(inode, name_index, "", value, retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 197 | } |
| 198 | if (retval > 0) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 199 | acl = ext4_acl_from_disk(value, retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 200 | else if (retval == -ENODATA || retval == -ENOSYS) |
| 201 | acl = NULL; |
| 202 | else |
| 203 | acl = ERR_PTR(retval); |
| 204 | kfree(value); |
| 205 | |
| 206 | if (!IS_ERR(acl)) { |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 207 | switch (type) { |
| 208 | case ACL_TYPE_ACCESS: |
| 209 | ext4_iset_acl(inode, &ei->i_acl, acl); |
| 210 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 211 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 212 | case ACL_TYPE_DEFAULT: |
| 213 | ext4_iset_acl(inode, &ei->i_default_acl, acl); |
| 214 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | return acl; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Set the access or default ACL of an inode. |
| 222 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 223 | * inode->i_mutex: down unless called from ext4_new_inode |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 224 | */ |
| 225 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 226 | ext4_set_acl(handle_t *handle, struct inode *inode, int type, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 227 | struct posix_acl *acl) |
| 228 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 229 | struct ext4_inode_info *ei = EXT4_I(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 230 | int name_index; |
| 231 | void *value = NULL; |
| 232 | size_t size = 0; |
| 233 | int error; |
| 234 | |
| 235 | if (S_ISLNK(inode->i_mode)) |
| 236 | return -EOPNOTSUPP; |
| 237 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 238 | switch (type) { |
| 239 | case ACL_TYPE_ACCESS: |
| 240 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; |
| 241 | if (acl) { |
| 242 | mode_t mode = inode->i_mode; |
| 243 | error = posix_acl_equiv_mode(acl, &mode); |
| 244 | if (error < 0) |
| 245 | return error; |
| 246 | else { |
| 247 | inode->i_mode = mode; |
| 248 | ext4_mark_inode_dirty(handle, inode); |
| 249 | if (error == 0) |
| 250 | acl = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 251 | } |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 252 | } |
| 253 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 254 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 255 | case ACL_TYPE_DEFAULT: |
| 256 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 257 | if (!S_ISDIR(inode->i_mode)) |
| 258 | return acl ? -EACCES : 0; |
| 259 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 260 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 261 | default: |
| 262 | return -EINVAL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 263 | } |
| 264 | if (acl) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 265 | value = ext4_acl_to_disk(acl, &size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 266 | if (IS_ERR(value)) |
| 267 | return (int)PTR_ERR(value); |
| 268 | } |
| 269 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 270 | error = ext4_xattr_set_handle(handle, inode, name_index, "", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 271 | value, size, 0); |
| 272 | |
| 273 | kfree(value); |
| 274 | if (!error) { |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 275 | switch (type) { |
| 276 | case ACL_TYPE_ACCESS: |
| 277 | ext4_iset_acl(inode, &ei->i_acl, acl); |
| 278 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 279 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 280 | case ACL_TYPE_DEFAULT: |
| 281 | ext4_iset_acl(inode, &ei->i_default_acl, acl); |
| 282 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | return error; |
| 286 | } |
| 287 | |
| 288 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 289 | ext4_check_acl(struct inode *inode, int mask) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 290 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 291 | struct posix_acl *acl = ext4_get_acl(inode, ACL_TYPE_ACCESS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 292 | |
| 293 | if (IS_ERR(acl)) |
| 294 | return PTR_ERR(acl); |
| 295 | if (acl) { |
| 296 | int error = posix_acl_permission(inode, acl, mask); |
| 297 | posix_acl_release(acl); |
| 298 | return error; |
| 299 | } |
| 300 | |
| 301 | return -EAGAIN; |
| 302 | } |
| 303 | |
| 304 | int |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 305 | ext4_permission(struct inode *inode, int mask) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 306 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 307 | return generic_permission(inode, mask, ext4_check_acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 311 | * Initialize the ACLs of a new inode. Called from ext4_new_inode. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 312 | * |
| 313 | * dir->i_mutex: down |
| 314 | * inode->i_mutex: up (access to inode is still exclusive) |
| 315 | */ |
| 316 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 317 | ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 318 | { |
| 319 | struct posix_acl *acl = NULL; |
| 320 | int error = 0; |
| 321 | |
| 322 | if (!S_ISLNK(inode->i_mode)) { |
| 323 | if (test_opt(dir->i_sb, POSIX_ACL)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 324 | acl = ext4_get_acl(dir, ACL_TYPE_DEFAULT); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 325 | if (IS_ERR(acl)) |
| 326 | return PTR_ERR(acl); |
| 327 | } |
| 328 | if (!acl) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 329 | inode->i_mode &= ~current_umask(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 330 | } |
| 331 | if (test_opt(inode->i_sb, POSIX_ACL) && acl) { |
| 332 | struct posix_acl *clone; |
| 333 | mode_t mode; |
| 334 | |
| 335 | if (S_ISDIR(inode->i_mode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 336 | error = ext4_set_acl(handle, inode, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 337 | ACL_TYPE_DEFAULT, acl); |
| 338 | if (error) |
| 339 | goto cleanup; |
| 340 | } |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 341 | clone = posix_acl_clone(acl, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 342 | error = -ENOMEM; |
| 343 | if (!clone) |
| 344 | goto cleanup; |
| 345 | |
| 346 | mode = inode->i_mode; |
| 347 | error = posix_acl_create_masq(clone, &mode); |
| 348 | if (error >= 0) { |
| 349 | inode->i_mode = mode; |
| 350 | if (error > 0) { |
| 351 | /* This is an extended ACL */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 352 | error = ext4_set_acl(handle, inode, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 353 | ACL_TYPE_ACCESS, clone); |
| 354 | } |
| 355 | } |
| 356 | posix_acl_release(clone); |
| 357 | } |
| 358 | cleanup: |
| 359 | posix_acl_release(acl); |
| 360 | return error; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Does chmod for an inode that may have an Access Control List. The |
| 365 | * inode->i_mode field must be updated to the desired value by the caller |
| 366 | * before calling this function. |
| 367 | * Returns 0 on success, or a negative error number. |
| 368 | * |
| 369 | * We change the ACL rather than storing some ACL entries in the file |
| 370 | * mode permission bits (which would be more efficient), because that |
| 371 | * would break once additional permissions (like ACL_APPEND, ACL_DELETE |
| 372 | * for directories) are added. There are no more bits available in the |
| 373 | * file mode. |
| 374 | * |
| 375 | * inode->i_mutex: down |
| 376 | */ |
| 377 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 378 | ext4_acl_chmod(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 379 | { |
| 380 | struct posix_acl *acl, *clone; |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 381 | int error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 382 | |
| 383 | if (S_ISLNK(inode->i_mode)) |
| 384 | return -EOPNOTSUPP; |
| 385 | if (!test_opt(inode->i_sb, POSIX_ACL)) |
| 386 | return 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 387 | acl = ext4_get_acl(inode, ACL_TYPE_ACCESS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 388 | if (IS_ERR(acl) || !acl) |
| 389 | return PTR_ERR(acl); |
| 390 | clone = posix_acl_clone(acl, GFP_KERNEL); |
| 391 | posix_acl_release(acl); |
| 392 | if (!clone) |
| 393 | return -ENOMEM; |
| 394 | error = posix_acl_chmod_masq(clone, inode->i_mode); |
| 395 | if (!error) { |
| 396 | handle_t *handle; |
| 397 | int retries = 0; |
| 398 | |
| 399 | retry: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 400 | handle = ext4_journal_start(inode, |
| 401 | EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 402 | if (IS_ERR(handle)) { |
| 403 | error = PTR_ERR(handle); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 404 | ext4_std_error(inode->i_sb, error); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 405 | goto out; |
| 406 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 407 | error = ext4_set_acl(handle, inode, ACL_TYPE_ACCESS, clone); |
| 408 | ext4_journal_stop(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 409 | if (error == -ENOSPC && |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 410 | ext4_should_retry_alloc(inode->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 411 | goto retry; |
| 412 | } |
| 413 | out: |
| 414 | posix_acl_release(clone); |
| 415 | return error; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Extended attribute handlers |
| 420 | */ |
| 421 | static size_t |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 422 | ext4_xattr_list_acl_access(struct inode *inode, char *list, size_t list_len, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 423 | const char *name, size_t name_len) |
| 424 | { |
| 425 | const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS); |
| 426 | |
| 427 | if (!test_opt(inode->i_sb, POSIX_ACL)) |
| 428 | return 0; |
| 429 | if (list && size <= list_len) |
| 430 | memcpy(list, POSIX_ACL_XATTR_ACCESS, size); |
| 431 | return size; |
| 432 | } |
| 433 | |
| 434 | static size_t |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 435 | ext4_xattr_list_acl_default(struct inode *inode, char *list, size_t list_len, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 436 | const char *name, size_t name_len) |
| 437 | { |
| 438 | const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT); |
| 439 | |
| 440 | if (!test_opt(inode->i_sb, POSIX_ACL)) |
| 441 | return 0; |
| 442 | if (list && size <= list_len) |
| 443 | memcpy(list, POSIX_ACL_XATTR_DEFAULT, size); |
| 444 | return size; |
| 445 | } |
| 446 | |
| 447 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 448 | ext4_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 449 | { |
| 450 | struct posix_acl *acl; |
| 451 | int error; |
| 452 | |
| 453 | if (!test_opt(inode->i_sb, POSIX_ACL)) |
| 454 | return -EOPNOTSUPP; |
| 455 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 456 | acl = ext4_get_acl(inode, type); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 457 | if (IS_ERR(acl)) |
| 458 | return PTR_ERR(acl); |
| 459 | if (acl == NULL) |
| 460 | return -ENODATA; |
| 461 | error = posix_acl_to_xattr(acl, buffer, size); |
| 462 | posix_acl_release(acl); |
| 463 | |
| 464 | return error; |
| 465 | } |
| 466 | |
| 467 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 468 | ext4_xattr_get_acl_access(struct inode *inode, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 469 | void *buffer, size_t size) |
| 470 | { |
| 471 | if (strcmp(name, "") != 0) |
| 472 | return -EINVAL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 473 | return ext4_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 477 | ext4_xattr_get_acl_default(struct inode *inode, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 478 | void *buffer, size_t size) |
| 479 | { |
| 480 | if (strcmp(name, "") != 0) |
| 481 | return -EINVAL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 482 | return ext4_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 486 | ext4_xattr_set_acl(struct inode *inode, int type, const void *value, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 487 | size_t size) |
| 488 | { |
| 489 | handle_t *handle; |
| 490 | struct posix_acl *acl; |
| 491 | int error, retries = 0; |
| 492 | |
| 493 | if (!test_opt(inode->i_sb, POSIX_ACL)) |
| 494 | return -EOPNOTSUPP; |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 495 | if (!is_owner_or_cap(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 496 | return -EPERM; |
| 497 | |
| 498 | if (value) { |
| 499 | acl = posix_acl_from_xattr(value, size); |
| 500 | if (IS_ERR(acl)) |
| 501 | return PTR_ERR(acl); |
| 502 | else if (acl) { |
| 503 | error = posix_acl_valid(acl); |
| 504 | if (error) |
| 505 | goto release_and_out; |
| 506 | } |
| 507 | } else |
| 508 | acl = NULL; |
| 509 | |
| 510 | retry: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 511 | handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 512 | if (IS_ERR(handle)) |
| 513 | return PTR_ERR(handle); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 514 | error = ext4_set_acl(handle, inode, type, acl); |
| 515 | ext4_journal_stop(handle); |
| 516 | if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 517 | goto retry; |
| 518 | |
| 519 | release_and_out: |
| 520 | posix_acl_release(acl); |
| 521 | return error; |
| 522 | } |
| 523 | |
| 524 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 525 | ext4_xattr_set_acl_access(struct inode *inode, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 526 | const void *value, size_t size, int flags) |
| 527 | { |
| 528 | if (strcmp(name, "") != 0) |
| 529 | return -EINVAL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 530 | return ext4_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 534 | ext4_xattr_set_acl_default(struct inode *inode, const char *name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 535 | const void *value, size_t size, int flags) |
| 536 | { |
| 537 | if (strcmp(name, "") != 0) |
| 538 | return -EINVAL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 539 | return ext4_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 542 | struct xattr_handler ext4_xattr_acl_access_handler = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 543 | .prefix = POSIX_ACL_XATTR_ACCESS, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 544 | .list = ext4_xattr_list_acl_access, |
| 545 | .get = ext4_xattr_get_acl_access, |
| 546 | .set = ext4_xattr_set_acl_access, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 547 | }; |
| 548 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 549 | struct xattr_handler ext4_xattr_acl_default_handler = { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 550 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 551 | .list = ext4_xattr_list_acl_default, |
| 552 | .get = ext4_xattr_get_acl_default, |
| 553 | .set = ext4_xattr_set_acl_default, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 554 | }; |