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