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