Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 2 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | #include <linux/fs.h> |
| 4 | #include <linux/posix_acl.h> |
Al Viro | f466c6f | 2012-03-17 01:16:43 -0400 | [diff] [blame] | 5 | #include "reiserfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/errno.h> |
| 7 | #include <linux/pagemap.h> |
| 8 | #include <linux/xattr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Christoph Hellwig | 9a59f452 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 10 | #include <linux/posix_acl_xattr.h> |
Al Viro | c45ac88 | 2012-03-17 00:59:06 -0400 | [diff] [blame] | 11 | #include "xattr.h" |
Al Viro | a3063ab | 2012-03-17 01:03:10 -0400 | [diff] [blame] | 12 | #include "acl.h" |
Fabian Frederick | 17093991 | 2014-08-08 14:21:12 -0700 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 15 | static int __reiserfs_set_acl(struct reiserfs_transaction_handle *th, |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 16 | struct inode *inode, int type, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 17 | struct posix_acl *acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 19 | |
| 20 | int |
| 21 | reiserfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 23 | int error, error2; |
| 24 | struct reiserfs_transaction_handle th; |
| 25 | size_t jcreate_blocks; |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 26 | int size = acl ? posix_acl_xattr_size(acl->a_count) : 0; |
Ernesto A. Fernández | fcea8ae | 2017-07-17 18:42:41 +0200 | [diff] [blame] | 27 | int update_mode = 0; |
| 28 | umode_t mode = inode->i_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 30 | /* |
| 31 | * Pessimism: We can't assume that anything from the xattr root up |
| 32 | * has been created. |
| 33 | */ |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 34 | |
| 35 | jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) + |
| 36 | reiserfs_xattr_nblocks(inode, size) * 2; |
| 37 | |
| 38 | reiserfs_write_lock(inode->i_sb); |
| 39 | error = journal_begin(&th, inode->i_sb, jcreate_blocks); |
Jeff Mahoney | 4c05141 | 2013-08-08 17:27:19 -0400 | [diff] [blame] | 40 | reiserfs_write_unlock(inode->i_sb); |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 41 | if (error == 0) { |
Jan Kara | 6883cd7 | 2017-06-22 09:32:49 +0200 | [diff] [blame] | 42 | if (type == ACL_TYPE_ACCESS && acl) { |
Ernesto A. Fernández | fcea8ae | 2017-07-17 18:42:41 +0200 | [diff] [blame] | 43 | error = posix_acl_update_mode(inode, &mode, &acl); |
Jan Kara | 6883cd7 | 2017-06-22 09:32:49 +0200 | [diff] [blame] | 44 | if (error) |
| 45 | goto unlock; |
Ernesto A. Fernández | fcea8ae | 2017-07-17 18:42:41 +0200 | [diff] [blame] | 46 | update_mode = 1; |
Jan Kara | 6883cd7 | 2017-06-22 09:32:49 +0200 | [diff] [blame] | 47 | } |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 48 | error = __reiserfs_set_acl(&th, inode, type, acl); |
Ernesto A. Fernández | fcea8ae | 2017-07-17 18:42:41 +0200 | [diff] [blame] | 49 | if (!error && update_mode) |
| 50 | inode->i_mode = mode; |
Jan Kara | 6883cd7 | 2017-06-22 09:32:49 +0200 | [diff] [blame] | 51 | unlock: |
Jeff Mahoney | 4c05141 | 2013-08-08 17:27:19 -0400 | [diff] [blame] | 52 | reiserfs_write_lock(inode->i_sb); |
Jeff Mahoney | 58d8542 | 2014-04-23 10:00:38 -0400 | [diff] [blame] | 53 | error2 = journal_end(&th); |
Jeff Mahoney | 4c05141 | 2013-08-08 17:27:19 -0400 | [diff] [blame] | 54 | reiserfs_write_unlock(inode->i_sb); |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 55 | if (error2) |
| 56 | error = error2; |
| 57 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return error; |
| 60 | } |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
| 63 | * Convert from filesystem to in-memory representation. |
| 64 | */ |
Christoph Hellwig | 9dad943 | 2013-12-20 05:16:36 -0800 | [diff] [blame] | 65 | static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | const char *end = (char *)value + size; |
| 68 | int n, count; |
| 69 | struct posix_acl *acl; |
| 70 | |
| 71 | if (!value) |
| 72 | return NULL; |
| 73 | if (size < sizeof(reiserfs_acl_header)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 74 | return ERR_PTR(-EINVAL); |
| 75 | if (((reiserfs_acl_header *) value)->a_version != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | cpu_to_le32(REISERFS_ACL_VERSION)) |
| 77 | return ERR_PTR(-EINVAL); |
| 78 | value = (char *)value + sizeof(reiserfs_acl_header); |
| 79 | count = reiserfs_acl_count(size); |
| 80 | if (count < 0) |
| 81 | return ERR_PTR(-EINVAL); |
| 82 | if (count == 0) |
| 83 | return NULL; |
| 84 | acl = posix_acl_alloc(count, GFP_NOFS); |
| 85 | if (!acl) |
| 86 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 87 | for (n = 0; n < count; n++) { |
| 88 | reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | if ((char *)value + sizeof(reiserfs_acl_entry_short) > end) |
| 90 | goto fail; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 91 | acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 93 | switch (acl->a_entries[n].e_tag) { |
| 94 | case ACL_USER_OBJ: |
| 95 | case ACL_GROUP_OBJ: |
| 96 | case ACL_MASK: |
| 97 | case ACL_OTHER: |
| 98 | value = (char *)value + |
| 99 | sizeof(reiserfs_acl_entry_short); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 100 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 102 | case ACL_USER: |
Eric W. Biederman | df81465 | 2012-02-07 16:30:06 -0800 | [diff] [blame] | 103 | value = (char *)value + sizeof(reiserfs_acl_entry); |
| 104 | if ((char *)value > end) |
| 105 | goto fail; |
| 106 | acl->a_entries[n].e_uid = |
| 107 | make_kuid(&init_user_ns, |
| 108 | le32_to_cpu(entry->e_id)); |
| 109 | break; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 110 | case ACL_GROUP: |
| 111 | value = (char *)value + sizeof(reiserfs_acl_entry); |
| 112 | if ((char *)value > end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | goto fail; |
Eric W. Biederman | df81465 | 2012-02-07 16:30:06 -0800 | [diff] [blame] | 114 | acl->a_entries[n].e_gid = |
| 115 | make_kgid(&init_user_ns, |
| 116 | le32_to_cpu(entry->e_id)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 117 | break; |
| 118 | |
| 119 | default: |
| 120 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | if (value != end) |
| 124 | goto fail; |
| 125 | return acl; |
| 126 | |
Jeff Mahoney | cf776a7 | 2014-04-23 10:00:41 -0400 | [diff] [blame] | 127 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | posix_acl_release(acl); |
| 129 | return ERR_PTR(-EINVAL); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Convert from in-memory to filesystem representation. |
| 134 | */ |
Christoph Hellwig | 9dad943 | 2013-12-20 05:16:36 -0800 | [diff] [blame] | 135 | static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
| 137 | reiserfs_acl_header *ext_acl; |
| 138 | char *e; |
| 139 | int n; |
| 140 | |
| 141 | *size = reiserfs_acl_size(acl->a_count); |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 142 | ext_acl = kmalloc(sizeof(reiserfs_acl_header) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 143 | acl->a_count * |
| 144 | sizeof(reiserfs_acl_entry), |
| 145 | GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if (!ext_acl) |
| 147 | return ERR_PTR(-ENOMEM); |
| 148 | ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION); |
| 149 | e = (char *)ext_acl + sizeof(reiserfs_acl_header); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 150 | for (n = 0; n < acl->a_count; n++) { |
Eric W. Biederman | df81465 | 2012-02-07 16:30:06 -0800 | [diff] [blame] | 151 | const struct posix_acl_entry *acl_e = &acl->a_entries[n]; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 152 | reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e; |
| 153 | entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 155 | switch (acl->a_entries[n].e_tag) { |
| 156 | case ACL_USER: |
Eric W. Biederman | df81465 | 2012-02-07 16:30:06 -0800 | [diff] [blame] | 157 | entry->e_id = cpu_to_le32( |
| 158 | from_kuid(&init_user_ns, acl_e->e_uid)); |
| 159 | e += sizeof(reiserfs_acl_entry); |
| 160 | break; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 161 | case ACL_GROUP: |
Eric W. Biederman | df81465 | 2012-02-07 16:30:06 -0800 | [diff] [blame] | 162 | entry->e_id = cpu_to_le32( |
| 163 | from_kgid(&init_user_ns, acl_e->e_gid)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 164 | e += sizeof(reiserfs_acl_entry); |
| 165 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 167 | case ACL_USER_OBJ: |
| 168 | case ACL_GROUP_OBJ: |
| 169 | case ACL_MASK: |
| 170 | case ACL_OTHER: |
| 171 | e += sizeof(reiserfs_acl_entry_short); |
| 172 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 174 | default: |
| 175 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | return (char *)ext_acl; |
| 179 | |
Jeff Mahoney | cf776a7 | 2014-04-23 10:00:41 -0400 | [diff] [blame] | 180 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | kfree(ext_acl); |
| 182 | return ERR_PTR(-EINVAL); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Inode operation get_posix_acl(). |
| 187 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 188 | * inode->i_mutex: down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | * BKL held [before 2.5.x] |
| 190 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 191 | struct posix_acl *reiserfs_get_acl(struct inode *inode, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | char *name, *value; |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 194 | struct posix_acl *acl; |
Adrian Bunk | 3cdc409 | 2006-03-25 03:07:50 -0800 | [diff] [blame] | 195 | int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 198 | switch (type) { |
| 199 | case ACL_TYPE_ACCESS: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 200 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 201 | break; |
| 202 | case ACL_TYPE_DEFAULT: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 203 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 204 | break; |
| 205 | default: |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 206 | BUG(); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 209 | size = reiserfs_xattr_get(inode, name, NULL, 0); |
Adrian Bunk | 3cdc409 | 2006-03-25 03:07:50 -0800 | [diff] [blame] | 210 | if (size < 0) { |
Andreas Gruenbacher | b8a7a3a | 2016-03-24 14:38:37 +0100 | [diff] [blame] | 211 | if (size == -ENODATA || size == -ENOSYS) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 212 | return NULL; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 213 | return ERR_PTR(size); |
| 214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 216 | value = kmalloc(size, GFP_NOFS); |
| 217 | if (!value) |
| 218 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | retval = reiserfs_xattr_get(inode, name, value, size); |
| 221 | if (retval == -ENODATA || retval == -ENOSYS) { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 222 | /* |
| 223 | * This shouldn't actually happen as it should have |
| 224 | * been caught above.. but just in case |
| 225 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | acl = NULL; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 227 | } else if (retval < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | acl = ERR_PTR(retval); |
| 229 | } else { |
Christoph Hellwig | 9dad943 | 2013-12-20 05:16:36 -0800 | [diff] [blame] | 230 | acl = reiserfs_posix_acl_from_disk(value, retval); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | kfree(value); |
| 234 | return acl; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Inode operation set_posix_acl(). |
| 239 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 240 | * inode->i_mutex: down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | * BKL held [before 2.5.x] |
| 242 | */ |
| 243 | static int |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 244 | __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode, |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 245 | int type, struct posix_acl *acl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 247 | char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | void *value = NULL; |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 249 | size_t size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 252 | switch (type) { |
| 253 | case ACL_TYPE_ACCESS: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 254 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 255 | break; |
| 256 | case ACL_TYPE_DEFAULT: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 257 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 258 | if (!S_ISDIR(inode->i_mode)) |
| 259 | return acl ? -EACCES : 0; |
| 260 | break; |
| 261 | default: |
| 262 | return -EINVAL; |
| 263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 265 | if (acl) { |
Christoph Hellwig | 9dad943 | 2013-12-20 05:16:36 -0800 | [diff] [blame] | 266 | value = reiserfs_posix_acl_to_disk(acl, &size); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 267 | if (IS_ERR(value)) |
| 268 | return (int)PTR_ERR(value); |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 269 | } |
| 270 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 271 | error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0); |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 272 | |
| 273 | /* |
| 274 | * Ensure that the inode gets dirtied if we're only using |
| 275 | * the mode bits and an old ACL didn't exist. We don't need |
| 276 | * to check if the inode is hashed here since we won't get |
| 277 | * called by reiserfs_inherit_default_acl(). |
| 278 | */ |
| 279 | if (error == -ENODATA) { |
| 280 | error = 0; |
| 281 | if (type == ACL_TYPE_ACCESS) { |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 282 | inode->i_ctime = current_time(inode); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 283 | mark_inode_dirty(inode); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
James Lamanna | 833d304 | 2005-10-30 15:00:16 -0800 | [diff] [blame] | 287 | kfree(value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Jeff Mahoney | d984561 | 2009-03-30 14:02:35 -0400 | [diff] [blame] | 289 | if (!error) |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 290 | set_cached_acl(inode, type, acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | return error; |
| 293 | } |
| 294 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 295 | /* |
| 296 | * dir->i_mutex: locked, |
| 297 | * inode is new and not released into the wild yet |
| 298 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | int |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 300 | reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th, |
| 301 | struct inode *dir, struct dentry *dentry, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 302 | struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 304 | struct posix_acl *default_acl, *acl; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 305 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 307 | /* ACLs only get applied to files and directories */ |
| 308 | if (S_ISLNK(inode->i_mode)) |
| 309 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 311 | /* |
| 312 | * ACLs can only be used on "new" objects, so if it's an old object |
| 313 | * there is nothing to inherit from |
| 314 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 315 | if (get_inode_sd_version(dir) == STAT_DATA_V1) |
| 316 | goto apply_umask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 318 | /* |
| 319 | * Don't apply ACLs to objects in the .reiserfs_priv tree.. This |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 320 | * would be useless since permissions are ignored, and a pain because |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 321 | * it introduces locking cycles |
| 322 | */ |
Jeff Mahoney | 60e4cf6 | 2019-10-24 10:31:27 -0400 | [diff] [blame] | 323 | if (IS_PRIVATE(inode)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 324 | goto apply_umask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 326 | err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); |
| 327 | if (err) |
| 328 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 330 | if (default_acl) { |
| 331 | err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT, |
| 332 | default_acl); |
| 333 | posix_acl_release(default_acl); |
| 334 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 335 | if (acl) { |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 336 | if (!err) |
| 337 | err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, |
| 338 | acl); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 339 | posix_acl_release(acl); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 340 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 342 | return err; |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 343 | |
Jeff Mahoney | cf776a7 | 2014-04-23 10:00:41 -0400 | [diff] [blame] | 344 | apply_umask: |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 345 | /* no ACL, apply umask */ |
| 346 | inode->i_mode &= ~current_umask(); |
| 347 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 350 | /* This is used to cache the default acl before a new object is created. |
| 351 | * The biggest reason for this is to get an idea of how many blocks will |
| 352 | * actually be required for the create operation if we must inherit an ACL. |
| 353 | * An ACL write can add up to 3 object creations and an additional file write |
| 354 | * so we'd prefer not to reserve that many blocks in the journal if we can. |
| 355 | * It also has the advantage of not loading the ACL with a transaction open, |
| 356 | * this may seem silly, but if the owner of the directory is doing the |
| 357 | * creation, the ACL may not be loaded since the permissions wouldn't require |
| 358 | * it. |
| 359 | * We return the number of blocks required for the transaction. |
| 360 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 361 | int reiserfs_cache_default_acl(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 363 | struct posix_acl *acl; |
| 364 | int nblocks = 0; |
| 365 | |
| 366 | if (IS_PRIVATE(inode)) |
| 367 | return 0; |
| 368 | |
Al Viro | 7dffc87f | 2016-03-18 13:25:43 -0400 | [diff] [blame] | 369 | acl = get_acl(inode, ACL_TYPE_DEFAULT); |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 370 | |
| 371 | if (acl && !IS_ERR(acl)) { |
| 372 | int size = reiserfs_acl_size(acl->a_count); |
| 373 | |
| 374 | /* Other xattrs can be created during inode creation. We don't |
| 375 | * want to claim too many blocks, so we check to see if we |
Randy Dunlap | 9436fb4 | 2020-08-04 19:49:25 -0700 | [diff] [blame^] | 376 | * need to create the tree to the xattrs, and then we |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 377 | * just want two files. */ |
| 378 | nblocks = reiserfs_xattr_jcreate_nblocks(inode); |
| 379 | nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb); |
| 380 | |
| 381 | REISERFS_I(inode)->i_flags |= i_has_xattr_dir; |
| 382 | |
| 383 | /* We need to account for writes + bitmaps for two files */ |
| 384 | nblocks += reiserfs_xattr_nblocks(inode, size) * 4; |
| 385 | posix_acl_release(acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 388 | return nblocks; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Jeff Mahoney | 4c05141 | 2013-08-08 17:27:19 -0400 | [diff] [blame] | 391 | /* |
| 392 | * Called under i_mutex |
| 393 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 394 | int reiserfs_acl_chmod(struct inode *inode) |
| 395 | { |
Jeff Mahoney | 4a85701 | 2013-05-31 15:54:17 -0400 | [diff] [blame] | 396 | if (IS_PRIVATE(inode)) |
| 397 | return 0; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 398 | if (get_inode_sd_version(inode) == STAT_DATA_V1 || |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 399 | !reiserfs_posixacl(inode->i_sb)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 400 | return 0; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 401 | |
Christoph Hellwig | 47f70d0 | 2013-12-20 05:16:49 -0800 | [diff] [blame] | 402 | return posix_acl_chmod(inode, inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |