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> |
| 4 | #include <linux/reiserfs_fs.h> |
| 5 | #include <linux/errno.h> |
| 6 | #include <linux/pagemap.h> |
| 7 | #include <linux/xattr.h> |
Christoph Hellwig | 9a59f45 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 8 | #include <linux/posix_acl_xattr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/reiserfs_xattr.h> |
| 10 | #include <linux/reiserfs_acl.h> |
| 11 | #include <asm/uaccess.h> |
| 12 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 13 | static int reiserfs_set_acl(struct reiserfs_transaction_handle *th, |
| 14 | struct inode *inode, int type, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 15 | struct posix_acl *acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 18 | posix_acl_set(struct dentry *dentry, const char *name, const void *value, |
| 19 | size_t size, int flags, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 21 | struct inode *inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | struct posix_acl *acl; |
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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | if (!reiserfs_posixacl(inode->i_sb)) |
| 27 | return -EOPNOTSUPP; |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 28 | if (!is_owner_or_cap(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | return -EPERM; |
| 30 | |
| 31 | if (value) { |
| 32 | acl = posix_acl_from_xattr(value, size); |
| 33 | if (IS_ERR(acl)) { |
| 34 | return PTR_ERR(acl); |
| 35 | } else if (acl) { |
| 36 | error = posix_acl_valid(acl); |
| 37 | if (error) |
| 38 | goto release_and_out; |
| 39 | } |
| 40 | } else |
| 41 | acl = NULL; |
| 42 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 43 | /* Pessimism: We can't assume that anything from the xattr root up |
| 44 | * has been created. */ |
| 45 | |
| 46 | jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) + |
| 47 | reiserfs_xattr_nblocks(inode, size) * 2; |
| 48 | |
| 49 | reiserfs_write_lock(inode->i_sb); |
| 50 | error = journal_begin(&th, inode->i_sb, jcreate_blocks); |
| 51 | if (error == 0) { |
| 52 | error = reiserfs_set_acl(&th, inode, type, acl); |
| 53 | error2 = journal_end(&th, inode->i_sb, jcreate_blocks); |
| 54 | if (error2) |
| 55 | error = error2; |
| 56 | } |
| 57 | reiserfs_write_unlock(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 59 | release_and_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | posix_acl_release(acl); |
| 61 | return error; |
| 62 | } |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 65 | posix_acl_get(struct dentry *dentry, const char *name, void *buffer, |
| 66 | size_t size, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
| 68 | struct posix_acl *acl; |
| 69 | int error; |
| 70 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 71 | if (!reiserfs_posixacl(dentry->d_sb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return -EOPNOTSUPP; |
| 73 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 74 | acl = reiserfs_get_acl(dentry->d_inode, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | if (IS_ERR(acl)) |
| 76 | return PTR_ERR(acl); |
| 77 | if (acl == NULL) |
| 78 | return -ENODATA; |
| 79 | error = posix_acl_to_xattr(acl, buffer, size); |
| 80 | posix_acl_release(acl); |
| 81 | |
| 82 | return error; |
| 83 | } |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | /* |
| 86 | * Convert from filesystem to in-memory representation. |
| 87 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 88 | static struct posix_acl *posix_acl_from_disk(const void *value, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
| 90 | const char *end = (char *)value + size; |
| 91 | int n, count; |
| 92 | struct posix_acl *acl; |
| 93 | |
| 94 | if (!value) |
| 95 | return NULL; |
| 96 | if (size < sizeof(reiserfs_acl_header)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 97 | return ERR_PTR(-EINVAL); |
| 98 | if (((reiserfs_acl_header *) value)->a_version != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | cpu_to_le32(REISERFS_ACL_VERSION)) |
| 100 | return ERR_PTR(-EINVAL); |
| 101 | value = (char *)value + sizeof(reiserfs_acl_header); |
| 102 | count = reiserfs_acl_count(size); |
| 103 | if (count < 0) |
| 104 | return ERR_PTR(-EINVAL); |
| 105 | if (count == 0) |
| 106 | return NULL; |
| 107 | acl = posix_acl_alloc(count, GFP_NOFS); |
| 108 | if (!acl) |
| 109 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 110 | for (n = 0; n < count; n++) { |
| 111 | reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | if ((char *)value + sizeof(reiserfs_acl_entry_short) > end) |
| 113 | goto fail; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 114 | acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 116 | switch (acl->a_entries[n].e_tag) { |
| 117 | case ACL_USER_OBJ: |
| 118 | case ACL_GROUP_OBJ: |
| 119 | case ACL_MASK: |
| 120 | case ACL_OTHER: |
| 121 | value = (char *)value + |
| 122 | sizeof(reiserfs_acl_entry_short); |
| 123 | acl->a_entries[n].e_id = ACL_UNDEFINED_ID; |
| 124 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 126 | case ACL_USER: |
| 127 | case ACL_GROUP: |
| 128 | value = (char *)value + sizeof(reiserfs_acl_entry); |
| 129 | if ((char *)value > end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | goto fail; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 131 | acl->a_entries[n].e_id = le32_to_cpu(entry->e_id); |
| 132 | break; |
| 133 | |
| 134 | default: |
| 135 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | if (value != end) |
| 139 | goto fail; |
| 140 | return acl; |
| 141 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 142 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | posix_acl_release(acl); |
| 144 | return ERR_PTR(-EINVAL); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Convert from in-memory to filesystem representation. |
| 149 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 150 | static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
| 152 | reiserfs_acl_header *ext_acl; |
| 153 | char *e; |
| 154 | int n; |
| 155 | |
| 156 | *size = reiserfs_acl_size(acl->a_count); |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 157 | ext_acl = kmalloc(sizeof(reiserfs_acl_header) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 158 | acl->a_count * |
| 159 | sizeof(reiserfs_acl_entry), |
| 160 | GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | if (!ext_acl) |
| 162 | return ERR_PTR(-ENOMEM); |
| 163 | ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION); |
| 164 | e = (char *)ext_acl + sizeof(reiserfs_acl_header); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 165 | for (n = 0; n < acl->a_count; n++) { |
| 166 | reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e; |
| 167 | entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 169 | switch (acl->a_entries[n].e_tag) { |
| 170 | case ACL_USER: |
| 171 | case ACL_GROUP: |
| 172 | entry->e_id = cpu_to_le32(acl->a_entries[n].e_id); |
| 173 | e += sizeof(reiserfs_acl_entry); |
| 174 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 176 | case ACL_USER_OBJ: |
| 177 | case ACL_GROUP_OBJ: |
| 178 | case ACL_MASK: |
| 179 | case ACL_OTHER: |
| 180 | e += sizeof(reiserfs_acl_entry_short); |
| 181 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 183 | default: |
| 184 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | return (char *)ext_acl; |
| 188 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 189 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | kfree(ext_acl); |
| 191 | return ERR_PTR(-EINVAL); |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Inode operation get_posix_acl(). |
| 196 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 197 | * inode->i_mutex: down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | * BKL held [before 2.5.x] |
| 199 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 200 | struct posix_acl *reiserfs_get_acl(struct inode *inode, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
| 202 | char *name, *value; |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 203 | struct posix_acl *acl; |
Adrian Bunk | 3cdc409 | 2006-03-25 03:07:50 -0800 | [diff] [blame] | 204 | int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 207 | acl = get_cached_acl(inode, type); |
| 208 | if (acl != ACL_NOT_CACHED) |
| 209 | return acl; |
| 210 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 211 | switch (type) { |
| 212 | case ACL_TYPE_ACCESS: |
| 213 | name = POSIX_ACL_XATTR_ACCESS; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 214 | break; |
| 215 | case ACL_TYPE_DEFAULT: |
| 216 | name = POSIX_ACL_XATTR_DEFAULT; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 217 | break; |
| 218 | default: |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 219 | BUG(); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 222 | size = reiserfs_xattr_get(inode, name, NULL, 0); |
Adrian Bunk | 3cdc409 | 2006-03-25 03:07:50 -0800 | [diff] [blame] | 223 | if (size < 0) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 224 | if (size == -ENODATA || size == -ENOSYS) { |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 225 | set_cached_acl(inode, type, NULL); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 226 | return NULL; |
| 227 | } |
| 228 | return ERR_PTR(size); |
| 229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 231 | value = kmalloc(size, GFP_NOFS); |
| 232 | if (!value) |
| 233 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
| 235 | retval = reiserfs_xattr_get(inode, name, value, size); |
| 236 | if (retval == -ENODATA || retval == -ENOSYS) { |
| 237 | /* This shouldn't actually happen as it should have |
| 238 | been caught above.. but just in case */ |
| 239 | acl = NULL; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 240 | } else if (retval < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | acl = ERR_PTR(retval); |
| 242 | } else { |
| 243 | acl = posix_acl_from_disk(value, retval); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 244 | } |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 245 | if (!IS_ERR(acl)) |
| 246 | set_cached_acl(inode, type, acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | kfree(value); |
| 249 | return acl; |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * Inode operation set_posix_acl(). |
| 254 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 255 | * inode->i_mutex: down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | * BKL held [before 2.5.x] |
| 257 | */ |
| 258 | static int |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 259 | reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode, |
| 260 | int type, struct posix_acl *acl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 262 | char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | void *value = NULL; |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 264 | size_t size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | if (S_ISLNK(inode->i_mode)) |
| 268 | return -EOPNOTSUPP; |
| 269 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 270 | switch (type) { |
| 271 | case ACL_TYPE_ACCESS: |
| 272 | name = POSIX_ACL_XATTR_ACCESS; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 273 | if (acl) { |
| 274 | mode_t mode = inode->i_mode; |
| 275 | error = posix_acl_equiv_mode(acl, &mode); |
| 276 | if (error < 0) |
| 277 | return error; |
| 278 | else { |
| 279 | inode->i_mode = mode; |
| 280 | if (error == 0) |
| 281 | acl = NULL; |
| 282 | } |
| 283 | } |
| 284 | break; |
| 285 | case ACL_TYPE_DEFAULT: |
| 286 | name = POSIX_ACL_XATTR_DEFAULT; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 287 | if (!S_ISDIR(inode->i_mode)) |
| 288 | return acl ? -EACCES : 0; |
| 289 | break; |
| 290 | default: |
| 291 | return -EINVAL; |
| 292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 294 | if (acl) { |
| 295 | value = posix_acl_to_disk(acl, &size); |
| 296 | if (IS_ERR(value)) |
| 297 | return (int)PTR_ERR(value); |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 300 | error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0); |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * Ensure that the inode gets dirtied if we're only using |
| 304 | * the mode bits and an old ACL didn't exist. We don't need |
| 305 | * to check if the inode is hashed here since we won't get |
| 306 | * called by reiserfs_inherit_default_acl(). |
| 307 | */ |
| 308 | if (error == -ENODATA) { |
| 309 | error = 0; |
| 310 | if (type == ACL_TYPE_ACCESS) { |
| 311 | inode->i_ctime = CURRENT_TIME_SEC; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 312 | mark_inode_dirty(inode); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
James Lamanna | 833d304 | 2005-10-30 15:00:16 -0800 | [diff] [blame] | 316 | kfree(value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Jeff Mahoney | d984561 | 2009-03-30 14:02:35 -0400 | [diff] [blame] | 318 | if (!error) |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 319 | set_cached_acl(inode, type, acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
| 321 | return error; |
| 322 | } |
| 323 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 324 | /* dir->i_mutex: locked, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | * inode is new and not released into the wild yet */ |
| 326 | int |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 327 | reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th, |
| 328 | struct inode *dir, struct dentry *dentry, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 329 | struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 331 | struct posix_acl *acl; |
| 332 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 334 | /* ACLs only get applied to files and directories */ |
| 335 | if (S_ISLNK(inode->i_mode)) |
| 336 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 338 | /* ACLs can only be used on "new" objects, so if it's an old object |
| 339 | * there is nothing to inherit from */ |
| 340 | if (get_inode_sd_version(dir) == STAT_DATA_V1) |
| 341 | goto apply_umask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 343 | /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This |
| 344 | * would be useless since permissions are ignored, and a pain because |
| 345 | * it introduces locking cycles */ |
Jeff Mahoney | 6dfede69 | 2009-03-30 14:02:32 -0400 | [diff] [blame] | 346 | if (IS_PRIVATE(dir)) { |
| 347 | inode->i_flags |= S_PRIVATE; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 348 | goto apply_umask; |
| 349 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 351 | acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT); |
Al Viro | 7a77b15 | 2009-06-08 21:01:13 -0400 | [diff] [blame] | 352 | if (IS_ERR(acl)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 353 | return PTR_ERR(acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 355 | if (acl) { |
| 356 | struct posix_acl *acl_copy; |
| 357 | mode_t mode = inode->i_mode; |
| 358 | int need_acl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 360 | /* Copy the default ACL to the default ACL of a new directory */ |
| 361 | if (S_ISDIR(inode->i_mode)) { |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 362 | err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT, |
| 363 | acl); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 364 | if (err) |
| 365 | goto cleanup; |
| 366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 368 | /* Now we reconcile the new ACL and the mode, |
| 369 | potentially modifying both */ |
| 370 | acl_copy = posix_acl_clone(acl, GFP_NOFS); |
| 371 | if (!acl_copy) { |
| 372 | err = -ENOMEM; |
| 373 | goto cleanup; |
| 374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 376 | need_acl = posix_acl_create_masq(acl_copy, &mode); |
| 377 | if (need_acl >= 0) { |
| 378 | if (mode != inode->i_mode) { |
| 379 | inode->i_mode = mode; |
| 380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 382 | /* If we need an ACL.. */ |
| 383 | if (need_acl > 0) { |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 384 | err = reiserfs_set_acl(th, inode, |
| 385 | ACL_TYPE_ACCESS, |
| 386 | acl_copy); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 387 | if (err) |
| 388 | goto cleanup_copy; |
| 389 | } |
| 390 | } |
| 391 | cleanup_copy: |
| 392 | posix_acl_release(acl_copy); |
| 393 | cleanup: |
| 394 | posix_acl_release(acl); |
| 395 | } else { |
| 396 | apply_umask: |
| 397 | /* no ACL, apply umask */ |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 398 | inode->i_mode &= ~current_umask(); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 401 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 404 | /* This is used to cache the default acl before a new object is created. |
| 405 | * The biggest reason for this is to get an idea of how many blocks will |
| 406 | * actually be required for the create operation if we must inherit an ACL. |
| 407 | * An ACL write can add up to 3 object creations and an additional file write |
| 408 | * so we'd prefer not to reserve that many blocks in the journal if we can. |
| 409 | * It also has the advantage of not loading the ACL with a transaction open, |
| 410 | * this may seem silly, but if the owner of the directory is doing the |
| 411 | * creation, the ACL may not be loaded since the permissions wouldn't require |
| 412 | * it. |
| 413 | * We return the number of blocks required for the transaction. |
| 414 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 415 | int reiserfs_cache_default_acl(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 417 | struct posix_acl *acl; |
| 418 | int nblocks = 0; |
| 419 | |
| 420 | if (IS_PRIVATE(inode)) |
| 421 | return 0; |
| 422 | |
| 423 | acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT); |
| 424 | |
| 425 | if (acl && !IS_ERR(acl)) { |
| 426 | int size = reiserfs_acl_size(acl->a_count); |
| 427 | |
| 428 | /* Other xattrs can be created during inode creation. We don't |
| 429 | * want to claim too many blocks, so we check to see if we |
| 430 | * we need to create the tree to the xattrs, and then we |
| 431 | * just want two files. */ |
| 432 | nblocks = reiserfs_xattr_jcreate_nblocks(inode); |
| 433 | nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb); |
| 434 | |
| 435 | REISERFS_I(inode)->i_flags |= i_has_xattr_dir; |
| 436 | |
| 437 | /* We need to account for writes + bitmaps for two files */ |
| 438 | nblocks += reiserfs_xattr_nblocks(inode, size) * 4; |
| 439 | posix_acl_release(acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 442 | return nblocks; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | int reiserfs_acl_chmod(struct inode *inode) |
| 446 | { |
| 447 | struct posix_acl *acl, *clone; |
| 448 | int error; |
| 449 | |
| 450 | if (S_ISLNK(inode->i_mode)) |
| 451 | return -EOPNOTSUPP; |
| 452 | |
| 453 | if (get_inode_sd_version(inode) == STAT_DATA_V1 || |
| 454 | !reiserfs_posixacl(inode->i_sb)) { |
| 455 | return 0; |
| 456 | } |
| 457 | |
Frederic Weisbecker | 6c28705 | 2010-01-07 12:57:47 +0100 | [diff] [blame] | 458 | reiserfs_write_unlock(inode->i_sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 459 | acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS); |
Frederic Weisbecker | 6c28705 | 2010-01-07 12:57:47 +0100 | [diff] [blame] | 460 | reiserfs_write_lock(inode->i_sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 461 | if (!acl) |
| 462 | return 0; |
| 463 | if (IS_ERR(acl)) |
| 464 | return PTR_ERR(acl); |
| 465 | clone = posix_acl_clone(acl, GFP_NOFS); |
| 466 | posix_acl_release(acl); |
| 467 | if (!clone) |
| 468 | return -ENOMEM; |
| 469 | error = posix_acl_chmod_masq(clone, inode->i_mode); |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 470 | if (!error) { |
| 471 | struct reiserfs_transaction_handle th; |
| 472 | size_t size = reiserfs_xattr_nblocks(inode, |
| 473 | reiserfs_acl_size(clone->a_count)); |
| 474 | reiserfs_write_lock(inode->i_sb); |
| 475 | error = journal_begin(&th, inode->i_sb, size * 2); |
| 476 | if (!error) { |
| 477 | int error2; |
| 478 | error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, |
| 479 | clone); |
| 480 | error2 = journal_end(&th, inode->i_sb, size * 2); |
| 481 | if (error2) |
| 482 | error = error2; |
| 483 | } |
| 484 | reiserfs_write_unlock(inode->i_sb); |
| 485 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 486 | posix_acl_release(clone); |
| 487 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 490 | static size_t posix_acl_access_list(struct dentry *dentry, char *list, |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 491 | size_t list_size, const char *name, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 492 | size_t name_len, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 494 | const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS); |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 495 | if (!reiserfs_posixacl(dentry->d_sb)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 496 | return 0; |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 497 | if (list && size <= list_size) |
| 498 | memcpy(list, POSIX_ACL_XATTR_ACCESS, size); |
| 499 | return size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 502 | struct xattr_handler reiserfs_posix_acl_access_handler = { |
Christoph Hellwig | 9a59f45 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 503 | .prefix = POSIX_ACL_XATTR_ACCESS, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 504 | .flags = ACL_TYPE_ACCESS, |
| 505 | .get = posix_acl_get, |
| 506 | .set = posix_acl_set, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | .list = posix_acl_access_list, |
| 508 | }; |
| 509 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 510 | static size_t posix_acl_default_list(struct dentry *dentry, char *list, |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 511 | size_t list_size, const char *name, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 512 | size_t name_len, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 514 | const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT); |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 515 | if (!reiserfs_posixacl(dentry->d_sb)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 516 | return 0; |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 517 | if (list && size <= list_size) |
| 518 | memcpy(list, POSIX_ACL_XATTR_DEFAULT, size); |
| 519 | return size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 522 | struct xattr_handler reiserfs_posix_acl_default_handler = { |
Christoph Hellwig | 9a59f45 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 523 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 524 | .flags = ACL_TYPE_DEFAULT, |
| 525 | .get = posix_acl_get, |
| 526 | .set = posix_acl_set, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | .list = posix_acl_default_list, |
| 528 | }; |