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