Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mm/shmem_acl.c |
| 3 | * |
| 4 | * (C) 2005 Andreas Gruenbacher <agruen@suse.de> |
| 5 | * |
| 6 | * This file is released under the GPL. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/shmem_fs.h> |
| 11 | #include <linux/xattr.h> |
| 12 | #include <linux/generic_acl.h> |
| 13 | |
| 14 | /** |
| 15 | * shmem_get_acl - generic_acl_operations->getacl() operation |
| 16 | */ |
| 17 | static struct posix_acl * |
| 18 | shmem_get_acl(struct inode *inode, int type) |
| 19 | { |
| 20 | struct posix_acl *acl = NULL; |
| 21 | |
| 22 | spin_lock(&inode->i_lock); |
| 23 | switch(type) { |
| 24 | case ACL_TYPE_ACCESS: |
Al Viro | 06b16e9 | 2009-06-08 19:56:00 -0400 | [diff] [blame] | 25 | acl = posix_acl_dup(inode->i_acl); |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 26 | break; |
| 27 | |
| 28 | case ACL_TYPE_DEFAULT: |
Al Viro | 06b16e9 | 2009-06-08 19:56:00 -0400 | [diff] [blame] | 29 | acl = posix_acl_dup(inode->i_default_acl); |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 30 | break; |
| 31 | } |
| 32 | spin_unlock(&inode->i_lock); |
| 33 | |
| 34 | return acl; |
| 35 | } |
| 36 | |
| 37 | /** |
Aneesh Kumar | 53bc5b2 | 2006-10-11 01:22:03 -0700 | [diff] [blame] | 38 | * shmem_set_acl - generic_acl_operations->setacl() operation |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 39 | */ |
| 40 | static void |
| 41 | shmem_set_acl(struct inode *inode, int type, struct posix_acl *acl) |
| 42 | { |
| 43 | struct posix_acl *free = NULL; |
| 44 | |
| 45 | spin_lock(&inode->i_lock); |
| 46 | switch(type) { |
| 47 | case ACL_TYPE_ACCESS: |
Al Viro | 06b16e9 | 2009-06-08 19:56:00 -0400 | [diff] [blame] | 48 | free = inode->i_acl; |
| 49 | inode->i_acl = posix_acl_dup(acl); |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 50 | break; |
| 51 | |
| 52 | case ACL_TYPE_DEFAULT: |
Al Viro | 06b16e9 | 2009-06-08 19:56:00 -0400 | [diff] [blame] | 53 | free = inode->i_default_acl; |
| 54 | inode->i_default_acl = posix_acl_dup(acl); |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 55 | break; |
| 56 | } |
| 57 | spin_unlock(&inode->i_lock); |
| 58 | posix_acl_release(free); |
| 59 | } |
| 60 | |
| 61 | struct generic_acl_operations shmem_acl_ops = { |
| 62 | .getacl = shmem_get_acl, |
| 63 | .setacl = shmem_set_acl, |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * shmem_list_acl_access, shmem_get_acl_access, shmem_set_acl_access, |
| 68 | * shmem_xattr_acl_access_handler - plumbing code to implement the |
| 69 | * system.posix_acl_access xattr using the generic acl functions. |
| 70 | */ |
| 71 | |
| 72 | static size_t |
| 73 | shmem_list_acl_access(struct inode *inode, char *list, size_t list_size, |
| 74 | const char *name, size_t name_len) |
| 75 | { |
| 76 | return generic_acl_list(inode, &shmem_acl_ops, ACL_TYPE_ACCESS, |
| 77 | list, list_size); |
| 78 | } |
| 79 | |
| 80 | static int |
| 81 | shmem_get_acl_access(struct inode *inode, const char *name, void *buffer, |
| 82 | size_t size) |
| 83 | { |
| 84 | if (strcmp(name, "") != 0) |
| 85 | return -EINVAL; |
| 86 | return generic_acl_get(inode, &shmem_acl_ops, ACL_TYPE_ACCESS, buffer, |
| 87 | size); |
| 88 | } |
| 89 | |
| 90 | static int |
| 91 | shmem_set_acl_access(struct inode *inode, const char *name, const void *value, |
| 92 | size_t size, int flags) |
| 93 | { |
| 94 | if (strcmp(name, "") != 0) |
| 95 | return -EINVAL; |
| 96 | return generic_acl_set(inode, &shmem_acl_ops, ACL_TYPE_ACCESS, value, |
| 97 | size); |
| 98 | } |
| 99 | |
| 100 | struct xattr_handler shmem_xattr_acl_access_handler = { |
| 101 | .prefix = POSIX_ACL_XATTR_ACCESS, |
| 102 | .list = shmem_list_acl_access, |
| 103 | .get = shmem_get_acl_access, |
| 104 | .set = shmem_set_acl_access, |
| 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * shmem_list_acl_default, shmem_get_acl_default, shmem_set_acl_default, |
| 109 | * shmem_xattr_acl_default_handler - plumbing code to implement the |
| 110 | * system.posix_acl_default xattr using the generic acl functions. |
| 111 | */ |
| 112 | |
| 113 | static size_t |
| 114 | shmem_list_acl_default(struct inode *inode, char *list, size_t list_size, |
| 115 | const char *name, size_t name_len) |
| 116 | { |
| 117 | return generic_acl_list(inode, &shmem_acl_ops, ACL_TYPE_DEFAULT, |
| 118 | list, list_size); |
| 119 | } |
| 120 | |
| 121 | static int |
| 122 | shmem_get_acl_default(struct inode *inode, const char *name, void *buffer, |
| 123 | size_t size) |
| 124 | { |
| 125 | if (strcmp(name, "") != 0) |
| 126 | return -EINVAL; |
| 127 | return generic_acl_get(inode, &shmem_acl_ops, ACL_TYPE_DEFAULT, buffer, |
| 128 | size); |
| 129 | } |
| 130 | |
| 131 | static int |
| 132 | shmem_set_acl_default(struct inode *inode, const char *name, const void *value, |
| 133 | size_t size, int flags) |
| 134 | { |
| 135 | if (strcmp(name, "") != 0) |
| 136 | return -EINVAL; |
| 137 | return generic_acl_set(inode, &shmem_acl_ops, ACL_TYPE_DEFAULT, value, |
| 138 | size); |
| 139 | } |
| 140 | |
| 141 | struct xattr_handler shmem_xattr_acl_default_handler = { |
| 142 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
| 143 | .list = shmem_list_acl_default, |
| 144 | .get = shmem_get_acl_default, |
| 145 | .set = shmem_set_acl_default, |
| 146 | }; |
| 147 | |
| 148 | /** |
| 149 | * shmem_acl_init - Inizialize the acl(s) of a new inode |
| 150 | */ |
| 151 | int |
| 152 | shmem_acl_init(struct inode *inode, struct inode *dir) |
| 153 | { |
| 154 | return generic_acl_init(inode, dir, &shmem_acl_ops); |
| 155 | } |
| 156 | |
| 157 | /** |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 158 | * shmem_check_acl - check_acl() callback for generic_permission() |
| 159 | */ |
Linus Torvalds | 6d848a4 | 2009-08-28 12:04:28 -0700 | [diff] [blame^] | 160 | int |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 161 | shmem_check_acl(struct inode *inode, int mask) |
| 162 | { |
| 163 | struct posix_acl *acl = shmem_get_acl(inode, ACL_TYPE_ACCESS); |
| 164 | |
| 165 | if (acl) { |
| 166 | int error = posix_acl_permission(inode, acl, mask); |
| 167 | posix_acl_release(acl); |
| 168 | return error; |
| 169 | } |
| 170 | return -EAGAIN; |
| 171 | } |