Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * fs/f2fs/acl.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
| 7 | * Portions of this code from linux/fs/ext2/acl.c |
| 8 | * |
| 9 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | #include <linux/f2fs_fs.h> |
| 16 | #include "f2fs.h" |
| 17 | #include "xattr.h" |
| 18 | #include "acl.h" |
| 19 | |
Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 20 | static inline size_t f2fs_acl_size(int count) |
| 21 | { |
| 22 | if (count <= 4) { |
| 23 | return sizeof(struct f2fs_acl_header) + |
| 24 | count * sizeof(struct f2fs_acl_entry_short); |
| 25 | } else { |
| 26 | return sizeof(struct f2fs_acl_header) + |
| 27 | 4 * sizeof(struct f2fs_acl_entry_short) + |
| 28 | (count - 4) * sizeof(struct f2fs_acl_entry); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | static inline int f2fs_acl_count(size_t size) |
| 33 | { |
| 34 | ssize_t s; |
| 35 | size -= sizeof(struct f2fs_acl_header); |
| 36 | s = size - 4 * sizeof(struct f2fs_acl_entry_short); |
| 37 | if (s < 0) { |
| 38 | if (size % sizeof(struct f2fs_acl_entry_short)) |
| 39 | return -1; |
| 40 | return size / sizeof(struct f2fs_acl_entry_short); |
| 41 | } else { |
| 42 | if (s % sizeof(struct f2fs_acl_entry)) |
| 43 | return -1; |
| 44 | return s / sizeof(struct f2fs_acl_entry) + 4; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size) |
| 49 | { |
| 50 | int i, count; |
| 51 | struct posix_acl *acl; |
| 52 | struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value; |
| 53 | struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1); |
| 54 | const char *end = value + size; |
| 55 | |
| 56 | if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION)) |
| 57 | return ERR_PTR(-EINVAL); |
| 58 | |
| 59 | count = f2fs_acl_count(size); |
| 60 | if (count < 0) |
| 61 | return ERR_PTR(-EINVAL); |
| 62 | if (count == 0) |
| 63 | return NULL; |
| 64 | |
| 65 | acl = posix_acl_alloc(count, GFP_KERNEL); |
| 66 | if (!acl) |
| 67 | return ERR_PTR(-ENOMEM); |
| 68 | |
| 69 | for (i = 0; i < count; i++) { |
| 70 | |
| 71 | if ((char *)entry > end) |
| 72 | goto fail; |
| 73 | |
| 74 | acl->a_entries[i].e_tag = le16_to_cpu(entry->e_tag); |
| 75 | acl->a_entries[i].e_perm = le16_to_cpu(entry->e_perm); |
| 76 | |
| 77 | switch (acl->a_entries[i].e_tag) { |
| 78 | case ACL_USER_OBJ: |
| 79 | case ACL_GROUP_OBJ: |
| 80 | case ACL_MASK: |
| 81 | case ACL_OTHER: |
Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 82 | entry = (struct f2fs_acl_entry *)((char *)entry + |
| 83 | sizeof(struct f2fs_acl_entry_short)); |
| 84 | break; |
| 85 | |
| 86 | case ACL_USER: |
Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 87 | case ACL_GROUP: |
Evan McClain | 1a808fb | 2014-03-20 09:12:16 -0400 | [diff] [blame] | 88 | acl->a_entries[i].e_id = le32_to_cpu(entry->e_id); |
Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 89 | entry = (struct f2fs_acl_entry *)((char *)entry + |
| 90 | sizeof(struct f2fs_acl_entry)); |
| 91 | break; |
| 92 | default: |
| 93 | goto fail; |
| 94 | } |
| 95 | } |
| 96 | if ((char *)entry != end) |
| 97 | goto fail; |
| 98 | return acl; |
| 99 | fail: |
| 100 | posix_acl_release(acl); |
| 101 | return ERR_PTR(-EINVAL); |
| 102 | } |
| 103 | |
| 104 | static void *f2fs_acl_to_disk(const struct posix_acl *acl, size_t *size) |
| 105 | { |
| 106 | struct f2fs_acl_header *f2fs_acl; |
| 107 | struct f2fs_acl_entry *entry; |
| 108 | int i; |
| 109 | |
| 110 | f2fs_acl = kmalloc(sizeof(struct f2fs_acl_header) + acl->a_count * |
| 111 | sizeof(struct f2fs_acl_entry), GFP_KERNEL); |
| 112 | if (!f2fs_acl) |
| 113 | return ERR_PTR(-ENOMEM); |
| 114 | |
| 115 | f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION); |
| 116 | entry = (struct f2fs_acl_entry *)(f2fs_acl + 1); |
| 117 | |
| 118 | for (i = 0; i < acl->a_count; i++) { |
| 119 | |
| 120 | entry->e_tag = cpu_to_le16(acl->a_entries[i].e_tag); |
| 121 | entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm); |
| 122 | |
| 123 | switch (acl->a_entries[i].e_tag) { |
| 124 | case ACL_USER: |
Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 125 | case ACL_GROUP: |
Evan McClain | 1a808fb | 2014-03-20 09:12:16 -0400 | [diff] [blame] | 126 | entry->e_id = cpu_to_le32(acl->a_entries[i].e_id); |
Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 127 | entry = (struct f2fs_acl_entry *)((char *)entry + |
| 128 | sizeof(struct f2fs_acl_entry)); |
| 129 | break; |
| 130 | case ACL_USER_OBJ: |
| 131 | case ACL_GROUP_OBJ: |
| 132 | case ACL_MASK: |
| 133 | case ACL_OTHER: |
| 134 | entry = (struct f2fs_acl_entry *)((char *)entry + |
| 135 | sizeof(struct f2fs_acl_entry_short)); |
| 136 | break; |
| 137 | default: |
| 138 | goto fail; |
| 139 | } |
| 140 | } |
| 141 | *size = f2fs_acl_size(acl->a_count); |
| 142 | return (void *)f2fs_acl; |
| 143 | |
| 144 | fail: |
| 145 | kfree(f2fs_acl); |
| 146 | return ERR_PTR(-EINVAL); |
| 147 | } |
| 148 | |
| 149 | struct posix_acl *f2fs_get_acl(struct inode *inode, int type) |
| 150 | { |
| 151 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 152 | int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 153 | void *value = NULL; |
| 154 | struct posix_acl *acl; |
| 155 | int retval; |
| 156 | |
| 157 | if (!test_opt(sbi, POSIX_ACL)) |
| 158 | return NULL; |
| 159 | |
| 160 | acl = get_cached_acl(inode, type); |
| 161 | if (acl != ACL_NOT_CACHED) |
| 162 | return acl; |
| 163 | |
| 164 | if (type == ACL_TYPE_ACCESS) |
| 165 | name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS; |
| 166 | |
| 167 | retval = f2fs_getxattr(inode, name_index, "", NULL, 0); |
| 168 | if (retval > 0) { |
Jaegeuk Kim | 395204b | 2014-03-20 22:21:08 +0900 | [diff] [blame] | 169 | value = kmalloc(retval, GFP_F2FS_ZERO); |
Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 170 | if (!value) |
| 171 | return ERR_PTR(-ENOMEM); |
| 172 | retval = f2fs_getxattr(inode, name_index, "", value, retval); |
| 173 | } |
| 174 | |
| 175 | if (retval > 0) |
| 176 | acl = f2fs_acl_from_disk(value, retval); |
| 177 | else if (retval == -ENODATA) |
| 178 | acl = NULL; |
| 179 | else |
| 180 | acl = ERR_PTR(retval); |
| 181 | kfree(value); |
| 182 | |
| 183 | if (!IS_ERR(acl)) |
| 184 | set_cached_acl(inode, type, acl); |
| 185 | |
| 186 | return acl; |
| 187 | } |
| 188 | |
| 189 | static int f2fs_set_acl(struct inode *inode, int type, |
| 190 | struct posix_acl *acl, struct page *ipage) |
| 191 | { |
| 192 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 193 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 194 | int name_index; |
| 195 | void *value = NULL; |
| 196 | size_t size = 0; |
| 197 | int error; |
| 198 | |
| 199 | if (!test_opt(sbi, POSIX_ACL)) |
| 200 | return 0; |
| 201 | if (S_ISLNK(inode->i_mode)) |
| 202 | return -EOPNOTSUPP; |
| 203 | |
| 204 | switch (type) { |
| 205 | case ACL_TYPE_ACCESS: |
| 206 | name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS; |
| 207 | if (acl) { |
| 208 | error = posix_acl_equiv_mode(acl, &inode->i_mode); |
| 209 | if (error < 0) |
| 210 | return error; |
| 211 | set_acl_inode(fi, inode->i_mode); |
| 212 | if (error == 0) |
| 213 | acl = NULL; |
| 214 | } |
| 215 | break; |
| 216 | |
| 217 | case ACL_TYPE_DEFAULT: |
| 218 | name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT; |
| 219 | if (!S_ISDIR(inode->i_mode)) |
| 220 | return acl ? -EACCES : 0; |
| 221 | break; |
| 222 | |
| 223 | default: |
| 224 | return -EINVAL; |
| 225 | } |
| 226 | |
| 227 | if (acl) { |
| 228 | value = f2fs_acl_to_disk(acl, &size); |
| 229 | if (IS_ERR(value)) { |
| 230 | cond_clear_inode_flag(fi, FI_ACL_MODE); |
| 231 | return (int)PTR_ERR(value); |
| 232 | } |
| 233 | } |
| 234 | |
Jaegeuk Kim | 2eb8e7c | 2014-04-23 12:23:14 +0900 | [diff] [blame] | 235 | error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0); |
Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 236 | |
| 237 | kfree(value); |
| 238 | if (!error) |
| 239 | set_cached_acl(inode, type, acl); |
| 240 | |
| 241 | cond_clear_inode_flag(fi, FI_ACL_MODE); |
| 242 | return error; |
| 243 | } |
| 244 | |
| 245 | int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage) |
| 246 | { |
| 247 | struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); |
| 248 | struct posix_acl *acl = NULL; |
| 249 | int error = 0; |
| 250 | |
| 251 | if (!S_ISLNK(inode->i_mode)) { |
| 252 | if (test_opt(sbi, POSIX_ACL)) { |
| 253 | acl = f2fs_get_acl(dir, ACL_TYPE_DEFAULT); |
| 254 | if (IS_ERR(acl)) |
| 255 | return PTR_ERR(acl); |
| 256 | } |
| 257 | if (!acl) |
| 258 | inode->i_mode &= ~current_umask(); |
| 259 | } |
| 260 | |
| 261 | if (!test_opt(sbi, POSIX_ACL) || !acl) |
| 262 | goto cleanup; |
| 263 | |
| 264 | if (S_ISDIR(inode->i_mode)) { |
| 265 | error = f2fs_set_acl(inode, ACL_TYPE_DEFAULT, acl, ipage); |
| 266 | if (error) |
| 267 | goto cleanup; |
| 268 | } |
| 269 | error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode); |
| 270 | if (error < 0) |
| 271 | return error; |
| 272 | if (error > 0) |
| 273 | error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl, ipage); |
| 274 | cleanup: |
| 275 | posix_acl_release(acl); |
| 276 | return error; |
| 277 | } |
| 278 | |
| 279 | int f2fs_acl_chmod(struct inode *inode) |
| 280 | { |
| 281 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 282 | struct posix_acl *acl; |
| 283 | int error; |
Evan McClain | f3f030d | 2014-07-17 21:16:35 -0400 | [diff] [blame^] | 284 | umode_t mode = get_inode_mode(inode); |
Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 285 | |
| 286 | if (!test_opt(sbi, POSIX_ACL)) |
| 287 | return 0; |
| 288 | if (S_ISLNK(mode)) |
| 289 | return -EOPNOTSUPP; |
| 290 | |
| 291 | acl = f2fs_get_acl(inode, ACL_TYPE_ACCESS); |
| 292 | if (IS_ERR(acl) || !acl) |
| 293 | return PTR_ERR(acl); |
| 294 | |
| 295 | error = posix_acl_chmod(&acl, GFP_KERNEL, mode); |
| 296 | if (error) |
| 297 | return error; |
| 298 | |
| 299 | error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl, NULL); |
| 300 | posix_acl_release(acl); |
| 301 | return error; |
| 302 | } |
| 303 | |
| 304 | static size_t f2fs_xattr_list_acl(struct dentry *dentry, char *list, |
| 305 | size_t list_size, const char *name, size_t name_len, int type) |
| 306 | { |
| 307 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); |
| 308 | const char *xname = POSIX_ACL_XATTR_DEFAULT; |
| 309 | size_t size; |
| 310 | |
| 311 | if (!test_opt(sbi, POSIX_ACL)) |
| 312 | return 0; |
| 313 | |
| 314 | if (type == ACL_TYPE_ACCESS) |
| 315 | xname = POSIX_ACL_XATTR_ACCESS; |
| 316 | |
| 317 | size = strlen(xname) + 1; |
| 318 | if (list && size <= list_size) |
| 319 | memcpy(list, xname, size); |
| 320 | return size; |
| 321 | } |
| 322 | |
| 323 | static int f2fs_xattr_get_acl(struct dentry *dentry, const char *name, |
| 324 | void *buffer, size_t size, int type) |
| 325 | { |
| 326 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); |
| 327 | struct posix_acl *acl; |
| 328 | int error; |
| 329 | |
| 330 | if (strcmp(name, "") != 0) |
| 331 | return -EINVAL; |
| 332 | if (!test_opt(sbi, POSIX_ACL)) |
| 333 | return -EOPNOTSUPP; |
| 334 | |
| 335 | acl = f2fs_get_acl(dentry->d_inode, type); |
| 336 | if (IS_ERR(acl)) |
| 337 | return PTR_ERR(acl); |
| 338 | if (!acl) |
| 339 | return -ENODATA; |
Evan McClain | 1a808fb | 2014-03-20 09:12:16 -0400 | [diff] [blame] | 340 | error = posix_acl_to_xattr(acl, buffer, size); |
Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 341 | posix_acl_release(acl); |
| 342 | |
| 343 | return error; |
| 344 | } |
| 345 | |
| 346 | static int f2fs_xattr_set_acl(struct dentry *dentry, const char *name, |
| 347 | const void *value, size_t size, int flags, int type) |
| 348 | { |
| 349 | struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb); |
| 350 | struct inode *inode = dentry->d_inode; |
| 351 | struct posix_acl *acl = NULL; |
| 352 | int error; |
| 353 | |
| 354 | if (strcmp(name, "") != 0) |
| 355 | return -EINVAL; |
| 356 | if (!test_opt(sbi, POSIX_ACL)) |
| 357 | return -EOPNOTSUPP; |
| 358 | if (!inode_owner_or_capable(inode)) |
| 359 | return -EPERM; |
| 360 | |
| 361 | if (value) { |
Evan McClain | 1a808fb | 2014-03-20 09:12:16 -0400 | [diff] [blame] | 362 | acl = posix_acl_from_xattr(value, size); |
Linus Torvalds | 8005ecc | 2012-12-20 13:54:51 -0800 | [diff] [blame] | 363 | if (IS_ERR(acl)) |
| 364 | return PTR_ERR(acl); |
| 365 | if (acl) { |
| 366 | error = posix_acl_valid(acl); |
| 367 | if (error) |
| 368 | goto release_and_out; |
| 369 | } |
| 370 | } else { |
| 371 | acl = NULL; |
| 372 | } |
| 373 | |
| 374 | error = f2fs_set_acl(inode, type, acl, NULL); |
| 375 | |
| 376 | release_and_out: |
| 377 | posix_acl_release(acl); |
| 378 | return error; |
| 379 | } |
| 380 | |
| 381 | const struct xattr_handler f2fs_xattr_acl_default_handler = { |
| 382 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
| 383 | .flags = ACL_TYPE_DEFAULT, |
| 384 | .list = f2fs_xattr_list_acl, |
| 385 | .get = f2fs_xattr_get_acl, |
| 386 | .set = f2fs_xattr_set_acl, |
| 387 | }; |
| 388 | |
| 389 | const struct xattr_handler f2fs_xattr_acl_access_handler = { |
| 390 | .prefix = POSIX_ACL_XATTR_ACCESS, |
| 391 | .flags = ACL_TYPE_ACCESS, |
| 392 | .list = f2fs_xattr_list_acl, |
| 393 | .get = f2fs_xattr_get_acl, |
| 394 | .set = f2fs_xattr_set_acl, |
| 395 | }; |