Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3 | * linux/fs/ext4/acl.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> |
| 6 | */ |
| 7 | |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 8 | #include <linux/quotaops.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 9 | #include "ext4_jbd2.h" |
| 10 | #include "ext4.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 11 | #include "xattr.h" |
| 12 | #include "acl.h" |
| 13 | |
| 14 | /* |
| 15 | * Convert from filesystem to in-memory representation. |
| 16 | */ |
| 17 | static struct posix_acl * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 18 | ext4_acl_from_disk(const void *value, size_t size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 19 | { |
| 20 | const char *end = (char *)value + size; |
| 21 | int n, count; |
| 22 | struct posix_acl *acl; |
| 23 | |
| 24 | if (!value) |
| 25 | return NULL; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 26 | if (size < sizeof(ext4_acl_header)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 27 | return ERR_PTR(-EINVAL); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 28 | if (((ext4_acl_header *)value)->a_version != |
| 29 | cpu_to_le32(EXT4_ACL_VERSION)) |
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 | value = (char *)value + sizeof(ext4_acl_header); |
| 32 | count = ext4_acl_count(size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 33 | if (count < 0) |
| 34 | return ERR_PTR(-EINVAL); |
| 35 | if (count == 0) |
| 36 | return NULL; |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 37 | acl = posix_acl_alloc(count, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 38 | if (!acl) |
| 39 | return ERR_PTR(-ENOMEM); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 40 | for (n = 0; n < count; n++) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 41 | ext4_acl_entry *entry = |
| 42 | (ext4_acl_entry *)value; |
| 43 | if ((char *)value + sizeof(ext4_acl_entry_short) > end) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 44 | goto fail; |
| 45 | acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); |
| 46 | acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 47 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 48 | switch (acl->a_entries[n].e_tag) { |
| 49 | case ACL_USER_OBJ: |
| 50 | case ACL_GROUP_OBJ: |
| 51 | case ACL_MASK: |
| 52 | case ACL_OTHER: |
| 53 | value = (char *)value + |
| 54 | sizeof(ext4_acl_entry_short); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 55 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 56 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 57 | case ACL_USER: |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 58 | value = (char *)value + sizeof(ext4_acl_entry); |
| 59 | if ((char *)value > end) |
| 60 | goto fail; |
| 61 | acl->a_entries[n].e_uid = |
| 62 | make_kuid(&init_user_ns, |
| 63 | le32_to_cpu(entry->e_id)); |
| 64 | break; |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 65 | case ACL_GROUP: |
| 66 | value = (char *)value + sizeof(ext4_acl_entry); |
| 67 | if ((char *)value > end) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 68 | goto fail; |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 69 | acl->a_entries[n].e_gid = |
| 70 | make_kgid(&init_user_ns, |
| 71 | le32_to_cpu(entry->e_id)); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 72 | break; |
| 73 | |
| 74 | default: |
| 75 | goto fail; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | if (value != end) |
| 79 | goto fail; |
| 80 | return acl; |
| 81 | |
| 82 | fail: |
| 83 | posix_acl_release(acl); |
| 84 | return ERR_PTR(-EINVAL); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Convert from in-memory to filesystem representation. |
| 89 | */ |
| 90 | static void * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 91 | ext4_acl_to_disk(const struct posix_acl *acl, size_t *size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 92 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 93 | ext4_acl_header *ext_acl; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 94 | char *e; |
| 95 | size_t n; |
| 96 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 97 | *size = ext4_acl_size(acl->a_count); |
| 98 | ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count * |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 99 | sizeof(ext4_acl_entry), GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 100 | if (!ext_acl) |
| 101 | return ERR_PTR(-ENOMEM); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 102 | ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION); |
| 103 | e = (char *)ext_acl + sizeof(ext4_acl_header); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 104 | for (n = 0; n < acl->a_count; n++) { |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 105 | const struct posix_acl_entry *acl_e = &acl->a_entries[n]; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 106 | ext4_acl_entry *entry = (ext4_acl_entry *)e; |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 107 | entry->e_tag = cpu_to_le16(acl_e->e_tag); |
| 108 | entry->e_perm = cpu_to_le16(acl_e->e_perm); |
| 109 | switch (acl_e->e_tag) { |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 110 | case ACL_USER: |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 111 | entry->e_id = cpu_to_le32( |
| 112 | from_kuid(&init_user_ns, acl_e->e_uid)); |
| 113 | e += sizeof(ext4_acl_entry); |
| 114 | break; |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 115 | case ACL_GROUP: |
Eric W. Biederman | af84df9 | 2012-09-10 20:44:54 -0700 | [diff] [blame] | 116 | entry->e_id = cpu_to_le32( |
| 117 | from_kgid(&init_user_ns, acl_e->e_gid)); |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 118 | e += sizeof(ext4_acl_entry); |
| 119 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 120 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 121 | case ACL_USER_OBJ: |
| 122 | case ACL_GROUP_OBJ: |
| 123 | case ACL_MASK: |
| 124 | case ACL_OTHER: |
| 125 | e += sizeof(ext4_acl_entry_short); |
| 126 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 127 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 128 | default: |
| 129 | goto fail; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | return (char *)ext_acl; |
| 133 | |
| 134 | fail: |
| 135 | kfree(ext_acl); |
| 136 | return ERR_PTR(-EINVAL); |
| 137 | } |
| 138 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 139 | /* |
| 140 | * Inode operation get_posix_acl(). |
| 141 | * |
| 142 | * inode->i_mutex: don't care |
| 143 | */ |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 144 | struct posix_acl * |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 145 | ext4_get_acl(struct inode *inode, int type, bool rcu) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 146 | { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 147 | int name_index; |
| 148 | char *value = NULL; |
| 149 | struct posix_acl *acl; |
| 150 | int retval; |
| 151 | |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 152 | if (rcu) |
| 153 | return ERR_PTR(-ECHILD); |
| 154 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 155 | switch (type) { |
| 156 | case ACL_TYPE_ACCESS: |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 157 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; |
| 158 | break; |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 159 | case ACL_TYPE_DEFAULT: |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 160 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 161 | break; |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 162 | default: |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 163 | BUG(); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 164 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 165 | retval = ext4_xattr_get(inode, name_index, "", NULL, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 166 | if (retval > 0) { |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 167 | value = kmalloc(retval, GFP_NOFS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 168 | if (!value) |
| 169 | return ERR_PTR(-ENOMEM); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 170 | retval = ext4_xattr_get(inode, name_index, "", value, retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 171 | } |
| 172 | if (retval > 0) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 173 | acl = ext4_acl_from_disk(value, retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 174 | else if (retval == -ENODATA || retval == -ENOSYS) |
| 175 | acl = NULL; |
| 176 | else |
| 177 | acl = ERR_PTR(retval); |
| 178 | kfree(value); |
| 179 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 180 | return acl; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Set the access or default ACL of an inode. |
| 185 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 186 | * inode->i_mutex: down unless called from ext4_new_inode |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 187 | */ |
| 188 | static int |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 189 | __ext4_set_acl(handle_t *handle, struct inode *inode, int type, |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 190 | struct posix_acl *acl, int xattr_flags) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 191 | { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 192 | int name_index; |
| 193 | void *value = NULL; |
| 194 | size_t size = 0; |
| 195 | int error; |
| 196 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 197 | switch (type) { |
| 198 | case ACL_TYPE_ACCESS: |
| 199 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 200 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 201 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 202 | case ACL_TYPE_DEFAULT: |
| 203 | name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 204 | if (!S_ISDIR(inode->i_mode)) |
| 205 | return acl ? -EACCES : 0; |
| 206 | break; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 207 | |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 208 | default: |
| 209 | return -EINVAL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 210 | } |
| 211 | if (acl) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 212 | value = ext4_acl_to_disk(acl, &size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 213 | if (IS_ERR(value)) |
| 214 | return (int)PTR_ERR(value); |
| 215 | } |
| 216 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 217 | error = ext4_xattr_set_handle(handle, inode, name_index, "", |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 218 | value, size, xattr_flags); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 219 | |
| 220 | kfree(value); |
Carlos Guerrero Álvarez | 6b6aeff | 2020-04-16 16:14:56 +0200 | [diff] [blame] | 221 | if (!error) |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 222 | set_cached_acl(inode, type, acl); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 223 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 224 | return error; |
| 225 | } |
| 226 | |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 227 | int |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 228 | ext4_set_acl(struct user_namespace *mnt_userns, struct inode *inode, |
| 229 | struct posix_acl *acl, int type) |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 230 | { |
| 231 | handle_t *handle; |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 232 | int error, credits, retries = 0; |
| 233 | size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0; |
Jan Kara | a3bb2d5 | 2017-07-30 23:33:01 -0400 | [diff] [blame] | 234 | umode_t mode = inode->i_mode; |
| 235 | int update_mode = 0; |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 236 | |
Tahsin Erdogan | b8cb5a5 | 2017-05-24 18:24:07 -0400 | [diff] [blame] | 237 | error = dquot_initialize(inode); |
| 238 | if (error) |
| 239 | return error; |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 240 | retry: |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 241 | error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */, |
| 242 | &credits); |
Tahsin Erdogan | dec214d | 2017-06-22 11:44:55 -0400 | [diff] [blame] | 243 | if (error) |
| 244 | return error; |
| 245 | |
Tahsin Erdogan | c1a5d5f | 2017-06-21 22:28:40 -0400 | [diff] [blame] | 246 | handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 247 | if (IS_ERR(handle)) |
| 248 | return PTR_ERR(handle); |
| 249 | |
Jan Kara | a3bb2d5 | 2017-07-30 23:33:01 -0400 | [diff] [blame] | 250 | if ((type == ACL_TYPE_ACCESS) && acl) { |
Christian Brauner | 14f3db5 | 2021-01-21 14:19:57 +0100 | [diff] [blame] | 251 | error = posix_acl_update_mode(mnt_userns, inode, &mode, &acl); |
Jan Kara | a3bb2d5 | 2017-07-30 23:33:01 -0400 | [diff] [blame] | 252 | if (error) |
| 253 | goto out_stop; |
Chengguang Xu | 0a1e825 | 2018-12-10 00:22:38 -0500 | [diff] [blame] | 254 | if (mode != inode->i_mode) |
| 255 | update_mode = 1; |
Jan Kara | a3bb2d5 | 2017-07-30 23:33:01 -0400 | [diff] [blame] | 256 | } |
| 257 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 258 | error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */); |
Jan Kara | a3bb2d5 | 2017-07-30 23:33:01 -0400 | [diff] [blame] | 259 | if (!error && update_mode) { |
| 260 | inode->i_mode = mode; |
| 261 | inode->i_ctime = current_time(inode); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 262 | error = ext4_mark_inode_dirty(handle, inode); |
Jan Kara | a3bb2d5 | 2017-07-30 23:33:01 -0400 | [diff] [blame] | 263 | } |
| 264 | out_stop: |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 265 | ext4_journal_stop(handle); |
| 266 | if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
| 267 | goto retry; |
| 268 | return error; |
| 269 | } |
| 270 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 271 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 272 | * Initialize the ACLs of a new inode. Called from ext4_new_inode. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 273 | * |
| 274 | * dir->i_mutex: down |
| 275 | * inode->i_mutex: up (access to inode is still exclusive) |
| 276 | */ |
| 277 | int |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 278 | ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 279 | { |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 280 | struct posix_acl *default_acl, *acl; |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 281 | int error; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 282 | |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 283 | error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 284 | if (error) |
| 285 | return error; |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 286 | |
| 287 | if (default_acl) { |
| 288 | error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT, |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 289 | default_acl, XATTR_CREATE); |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 290 | posix_acl_release(default_acl); |
Chengguang Xu | 6fd9417 | 2018-10-06 22:40:34 -0400 | [diff] [blame] | 291 | } else { |
| 292 | inode->i_default_acl = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 293 | } |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 294 | if (acl) { |
| 295 | if (!error) |
| 296 | error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS, |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 297 | acl, XATTR_CREATE); |
Christoph Hellwig | 64e178a | 2013-12-20 05:16:44 -0800 | [diff] [blame] | 298 | posix_acl_release(acl); |
Chengguang Xu | 6fd9417 | 2018-10-06 22:40:34 -0400 | [diff] [blame] | 299 | } else { |
| 300 | inode->i_acl = NULL; |
Eugene Shatokhin | 24ec19b | 2012-11-08 15:11:11 -0500 | [diff] [blame] | 301 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 302 | return error; |
| 303 | } |