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 | |
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 | |
| 175 | /* |
| 176 | * Inode operation get_posix_acl(). |
| 177 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 178 | * inode->i_mutex: down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | * BKL held [before 2.5.x] |
| 180 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 181 | struct posix_acl *reiserfs_get_acl(struct inode *inode, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | char *name, *value; |
| 184 | struct posix_acl *acl, **p_acl; |
Adrian Bunk | 3cdc409 | 2006-03-25 03:07:50 -0800 | [diff] [blame] | 185 | int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | int retval; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 187 | struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 189 | switch (type) { |
| 190 | case ACL_TYPE_ACCESS: |
| 191 | name = POSIX_ACL_XATTR_ACCESS; |
| 192 | p_acl = &reiserfs_i->i_acl_access; |
| 193 | break; |
| 194 | case ACL_TYPE_DEFAULT: |
| 195 | name = POSIX_ACL_XATTR_DEFAULT; |
| 196 | p_acl = &reiserfs_i->i_acl_default; |
| 197 | break; |
| 198 | default: |
| 199 | return ERR_PTR(-EINVAL); |
| 200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 202 | if (IS_ERR(*p_acl)) { |
| 203 | if (PTR_ERR(*p_acl) == -ENODATA) |
| 204 | return NULL; |
| 205 | } else if (*p_acl != NULL) |
| 206 | return posix_acl_dup(*p_acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 208 | size = reiserfs_xattr_get(inode, name, NULL, 0); |
Adrian Bunk | 3cdc409 | 2006-03-25 03:07:50 -0800 | [diff] [blame] | 209 | if (size < 0) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 210 | if (size == -ENODATA || size == -ENOSYS) { |
| 211 | *p_acl = ERR_PTR(-ENODATA); |
| 212 | return NULL; |
| 213 | } |
| 214 | return ERR_PTR(size); |
| 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 217 | value = kmalloc(size, GFP_NOFS); |
| 218 | if (!value) |
| 219 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | retval = reiserfs_xattr_get(inode, name, value, size); |
| 222 | if (retval == -ENODATA || retval == -ENOSYS) { |
| 223 | /* This shouldn't actually happen as it should have |
| 224 | been caught above.. but just in case */ |
| 225 | acl = NULL; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 226 | *p_acl = ERR_PTR(-ENODATA); |
| 227 | } else if (retval < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | acl = ERR_PTR(retval); |
| 229 | } else { |
| 230 | acl = posix_acl_from_disk(value, retval); |
Jeff Mahoney | 90947ef | 2006-02-13 11:12:36 -0500 | [diff] [blame] | 231 | if (!IS_ERR(acl)) |
| 232 | *p_acl = posix_acl_dup(acl); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 233 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
| 235 | kfree(value); |
| 236 | return acl; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Inode operation set_posix_acl(). |
| 241 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 242 | * inode->i_mutex: down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | * BKL held [before 2.5.x] |
| 244 | */ |
| 245 | static int |
| 246 | reiserfs_set_acl(struct inode *inode, int type, struct posix_acl *acl) |
| 247 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 248 | char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | void *value = NULL; |
| 250 | struct posix_acl **p_acl; |
| 251 | size_t size; |
| 252 | int error; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 253 | struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | if (S_ISLNK(inode->i_mode)) |
| 256 | return -EOPNOTSUPP; |
| 257 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 258 | switch (type) { |
| 259 | case ACL_TYPE_ACCESS: |
| 260 | name = POSIX_ACL_XATTR_ACCESS; |
| 261 | p_acl = &reiserfs_i->i_acl_access; |
| 262 | if (acl) { |
| 263 | mode_t mode = inode->i_mode; |
| 264 | error = posix_acl_equiv_mode(acl, &mode); |
| 265 | if (error < 0) |
| 266 | return error; |
| 267 | else { |
| 268 | inode->i_mode = mode; |
| 269 | if (error == 0) |
| 270 | acl = NULL; |
| 271 | } |
| 272 | } |
| 273 | break; |
| 274 | case ACL_TYPE_DEFAULT: |
| 275 | name = POSIX_ACL_XATTR_DEFAULT; |
| 276 | p_acl = &reiserfs_i->i_acl_default; |
| 277 | if (!S_ISDIR(inode->i_mode)) |
| 278 | return acl ? -EACCES : 0; |
| 279 | break; |
| 280 | default: |
| 281 | return -EINVAL; |
| 282 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 284 | if (acl) { |
| 285 | value = posix_acl_to_disk(acl, &size); |
| 286 | if (IS_ERR(value)) |
| 287 | return (int)PTR_ERR(value); |
| 288 | error = reiserfs_xattr_set(inode, name, value, size, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } else { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 290 | error = reiserfs_xattr_del(inode, name); |
| 291 | if (error == -ENODATA) { |
| 292 | /* This may seem odd here, but it means that the ACL was set |
| 293 | * with a value representable with mode bits. If there was |
| 294 | * an ACL before, reiserfs_xattr_del already dirtied the inode. |
| 295 | */ |
| 296 | mark_inode_dirty(inode); |
| 297 | error = 0; |
| 298 | } |
| 299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
James Lamanna | 833d304 | 2005-10-30 15:00:16 -0800 | [diff] [blame] | 301 | kfree(value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 303 | if (!error) { |
| 304 | /* Release the old one */ |
| 305 | if (!IS_ERR(*p_acl) && *p_acl) |
| 306 | posix_acl_release(*p_acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 308 | if (acl == NULL) |
| 309 | *p_acl = ERR_PTR(-ENODATA); |
| 310 | else |
| 311 | *p_acl = posix_acl_dup(acl); |
| 312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | return error; |
| 315 | } |
| 316 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 317 | /* dir->i_mutex: locked, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | * inode is new and not released into the wild yet */ |
| 319 | int |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 320 | reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry, |
| 321 | struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 323 | struct posix_acl *acl; |
| 324 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 326 | /* ACLs only get applied to files and directories */ |
| 327 | if (S_ISLNK(inode->i_mode)) |
| 328 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 330 | /* ACLs can only be used on "new" objects, so if it's an old object |
| 331 | * there is nothing to inherit from */ |
| 332 | if (get_inode_sd_version(dir) == STAT_DATA_V1) |
| 333 | goto apply_umask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 335 | /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This |
| 336 | * would be useless since permissions are ignored, and a pain because |
| 337 | * it introduces locking cycles */ |
| 338 | if (is_reiserfs_priv_object(dir)) { |
| 339 | reiserfs_mark_inode_private(inode); |
| 340 | goto apply_umask; |
| 341 | } |
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 | acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT); |
| 344 | if (IS_ERR(acl)) { |
| 345 | if (PTR_ERR(acl) == -ENODATA) |
| 346 | goto apply_umask; |
| 347 | return PTR_ERR(acl); |
| 348 | } |
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 | if (acl) { |
| 351 | struct posix_acl *acl_copy; |
| 352 | mode_t mode = inode->i_mode; |
| 353 | int need_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 | /* Copy the default ACL to the default ACL of a new directory */ |
| 356 | if (S_ISDIR(inode->i_mode)) { |
| 357 | err = reiserfs_set_acl(inode, ACL_TYPE_DEFAULT, acl); |
| 358 | if (err) |
| 359 | goto cleanup; |
| 360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 362 | /* Now we reconcile the new ACL and the mode, |
| 363 | potentially modifying both */ |
| 364 | acl_copy = posix_acl_clone(acl, GFP_NOFS); |
| 365 | if (!acl_copy) { |
| 366 | err = -ENOMEM; |
| 367 | goto cleanup; |
| 368 | } |
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 | need_acl = posix_acl_create_masq(acl_copy, &mode); |
| 371 | if (need_acl >= 0) { |
| 372 | if (mode != inode->i_mode) { |
| 373 | inode->i_mode = mode; |
| 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 | /* If we need an ACL.. */ |
| 377 | if (need_acl > 0) { |
| 378 | err = |
| 379 | reiserfs_set_acl(inode, ACL_TYPE_ACCESS, |
| 380 | acl_copy); |
| 381 | if (err) |
| 382 | goto cleanup_copy; |
| 383 | } |
| 384 | } |
| 385 | cleanup_copy: |
| 386 | posix_acl_release(acl_copy); |
| 387 | cleanup: |
| 388 | posix_acl_release(acl); |
| 389 | } else { |
| 390 | apply_umask: |
| 391 | /* no ACL, apply umask */ |
| 392 | inode->i_mode &= ~current->fs->umask; |
| 393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 395 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* Looks up and caches the result of the default ACL. |
| 399 | * We do this so that we don't need to carry the xattr_sem into |
| 400 | * reiserfs_new_inode if we don't need to */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 401 | int reiserfs_cache_default_acl(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 403 | int ret = 0; |
| 404 | if (reiserfs_posixacl(inode->i_sb) && !is_reiserfs_priv_object(inode)) { |
| 405 | struct posix_acl *acl; |
| 406 | reiserfs_read_lock_xattr_i(inode); |
| 407 | reiserfs_read_lock_xattrs(inode->i_sb); |
| 408 | acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT); |
| 409 | reiserfs_read_unlock_xattrs(inode->i_sb); |
| 410 | reiserfs_read_unlock_xattr_i(inode); |
Jan Kara | b9251b8 | 2006-04-22 02:36:24 -0700 | [diff] [blame] | 411 | ret = (acl && !IS_ERR(acl)); |
| 412 | if (ret) |
| 413 | posix_acl_release(acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | int reiserfs_acl_chmod(struct inode *inode) |
| 420 | { |
| 421 | struct posix_acl *acl, *clone; |
| 422 | int error; |
| 423 | |
| 424 | if (S_ISLNK(inode->i_mode)) |
| 425 | return -EOPNOTSUPP; |
| 426 | |
| 427 | if (get_inode_sd_version(inode) == STAT_DATA_V1 || |
| 428 | !reiserfs_posixacl(inode->i_sb)) { |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | reiserfs_read_lock_xattrs(inode->i_sb); |
| 433 | acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS); |
| 434 | reiserfs_read_unlock_xattrs(inode->i_sb); |
| 435 | if (!acl) |
| 436 | return 0; |
| 437 | if (IS_ERR(acl)) |
| 438 | return PTR_ERR(acl); |
| 439 | clone = posix_acl_clone(acl, GFP_NOFS); |
| 440 | posix_acl_release(acl); |
| 441 | if (!clone) |
| 442 | return -ENOMEM; |
| 443 | error = posix_acl_chmod_masq(clone, inode->i_mode); |
| 444 | if (!error) { |
| 445 | int lock = !has_xattr_dir(inode); |
| 446 | reiserfs_write_lock_xattr_i(inode); |
| 447 | if (lock) |
| 448 | reiserfs_write_lock_xattrs(inode->i_sb); |
| 449 | else |
| 450 | reiserfs_read_lock_xattrs(inode->i_sb); |
| 451 | error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone); |
| 452 | if (lock) |
| 453 | reiserfs_write_unlock_xattrs(inode->i_sb); |
| 454 | else |
| 455 | reiserfs_read_unlock_xattrs(inode->i_sb); |
| 456 | reiserfs_write_unlock_xattr_i(inode); |
| 457 | } |
| 458 | posix_acl_release(clone); |
| 459 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static int |
| 463 | posix_acl_access_get(struct inode *inode, const char *name, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 464 | void *buffer, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 466 | if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | return -EINVAL; |
| 468 | return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size); |
| 469 | } |
| 470 | |
| 471 | static int |
| 472 | posix_acl_access_set(struct inode *inode, const char *name, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 473 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 475 | if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | return -EINVAL; |
| 477 | return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size); |
| 478 | } |
| 479 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 480 | static int posix_acl_access_del(struct inode *inode, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 482 | struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode); |
| 483 | struct posix_acl **acl = &reiserfs_i->i_acl_access; |
| 484 | if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1) |
| 485 | return -EINVAL; |
| 486 | if (!IS_ERR(*acl) && *acl) { |
| 487 | posix_acl_release(*acl); |
| 488 | *acl = ERR_PTR(-ENODATA); |
| 489 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 491 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static int |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 495 | posix_acl_access_list(struct inode *inode, const char *name, int namelen, |
| 496 | char *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 498 | int len = namelen; |
| 499 | if (!reiserfs_posixacl(inode->i_sb)) |
| 500 | return 0; |
| 501 | if (out) |
| 502 | memcpy(out, name, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 504 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | struct reiserfs_xattr_handler posix_acl_access_handler = { |
Christoph Hellwig | 9a59f45 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 508 | .prefix = POSIX_ACL_XATTR_ACCESS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | .get = posix_acl_access_get, |
| 510 | .set = posix_acl_access_set, |
| 511 | .del = posix_acl_access_del, |
| 512 | .list = posix_acl_access_list, |
| 513 | }; |
| 514 | |
| 515 | static int |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 516 | posix_acl_default_get(struct inode *inode, const char *name, |
| 517 | void *buffer, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 519 | if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | return -EINVAL; |
| 521 | return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size); |
| 522 | } |
| 523 | |
| 524 | static int |
| 525 | posix_acl_default_set(struct inode *inode, const char *name, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 526 | const void *value, size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 528 | if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | return -EINVAL; |
| 530 | return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size); |
| 531 | } |
| 532 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 533 | static int posix_acl_default_del(struct inode *inode, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 535 | struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode); |
| 536 | struct posix_acl **acl = &reiserfs_i->i_acl_default; |
| 537 | if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) |
| 538 | return -EINVAL; |
| 539 | if (!IS_ERR(*acl) && *acl) { |
| 540 | posix_acl_release(*acl); |
| 541 | *acl = ERR_PTR(-ENODATA); |
| 542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 544 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static int |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 548 | posix_acl_default_list(struct inode *inode, const char *name, int namelen, |
| 549 | char *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 551 | int len = namelen; |
| 552 | if (!reiserfs_posixacl(inode->i_sb)) |
| 553 | return 0; |
| 554 | if (out) |
| 555 | memcpy(out, name, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 557 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | struct reiserfs_xattr_handler posix_acl_default_handler = { |
Christoph Hellwig | 9a59f45 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 561 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | .get = posix_acl_default_get, |
| 563 | .set = posix_acl_default_set, |
| 564 | .del = posix_acl_default_del, |
| 565 | .list = posix_acl_default_list, |
| 566 | }; |