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