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