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> |
Al Viro | f466c6f | 2012-03-17 01:16:43 -0400 | [diff] [blame] | 4 | #include "reiserfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/errno.h> |
| 6 | #include <linux/pagemap.h> |
| 7 | #include <linux/xattr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Christoph Hellwig | 9a59f45 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 9 | #include <linux/posix_acl_xattr.h> |
Al Viro | c45ac88 | 2012-03-17 00:59:06 -0400 | [diff] [blame] | 10 | #include "xattr.h" |
Al Viro | a3063ab | 2012-03-17 01:03:10 -0400 | [diff] [blame] | 11 | #include "acl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <asm/uaccess.h> |
| 13 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 14 | static int reiserfs_set_acl(struct reiserfs_transaction_handle *th, |
| 15 | struct inode *inode, int type, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 16 | struct posix_acl *acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 19 | posix_acl_set(struct dentry *dentry, const char *name, const void *value, |
| 20 | size_t size, int flags, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 22 | struct inode *inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | struct posix_acl *acl; |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 24 | int error, error2; |
| 25 | struct reiserfs_transaction_handle th; |
| 26 | size_t jcreate_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | if (!reiserfs_posixacl(inode->i_sb)) |
| 28 | return -EOPNOTSUPP; |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 29 | if (!inode_owner_or_capable(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | return -EPERM; |
| 31 | |
| 32 | if (value) { |
| 33 | acl = posix_acl_from_xattr(value, size); |
| 34 | if (IS_ERR(acl)) { |
| 35 | return PTR_ERR(acl); |
| 36 | } else if (acl) { |
| 37 | error = posix_acl_valid(acl); |
| 38 | if (error) |
| 39 | goto release_and_out; |
| 40 | } |
| 41 | } else |
| 42 | acl = NULL; |
| 43 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 44 | /* Pessimism: We can't assume that anything from the xattr root up |
| 45 | * has been created. */ |
| 46 | |
| 47 | jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) + |
| 48 | reiserfs_xattr_nblocks(inode, size) * 2; |
| 49 | |
| 50 | reiserfs_write_lock(inode->i_sb); |
| 51 | error = journal_begin(&th, inode->i_sb, jcreate_blocks); |
| 52 | if (error == 0) { |
| 53 | error = reiserfs_set_acl(&th, inode, type, acl); |
| 54 | error2 = journal_end(&th, inode->i_sb, jcreate_blocks); |
| 55 | if (error2) |
| 56 | error = error2; |
| 57 | } |
| 58 | reiserfs_write_unlock(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 60 | release_and_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | posix_acl_release(acl); |
| 62 | return error; |
| 63 | } |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static int |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 66 | posix_acl_get(struct dentry *dentry, const char *name, void *buffer, |
| 67 | size_t size, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | struct posix_acl *acl; |
| 70 | int error; |
| 71 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 72 | if (!reiserfs_posixacl(dentry->d_sb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | return -EOPNOTSUPP; |
| 74 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 75 | acl = reiserfs_get_acl(dentry->d_inode, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | if (IS_ERR(acl)) |
| 77 | return PTR_ERR(acl); |
| 78 | if (acl == NULL) |
| 79 | return -ENODATA; |
| 80 | error = posix_acl_to_xattr(acl, buffer, size); |
| 81 | posix_acl_release(acl); |
| 82 | |
| 83 | return error; |
| 84 | } |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | /* |
| 87 | * Convert from filesystem to in-memory representation. |
| 88 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 89 | 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] | 90 | { |
| 91 | const char *end = (char *)value + size; |
| 92 | int n, count; |
| 93 | struct posix_acl *acl; |
| 94 | |
| 95 | if (!value) |
| 96 | return NULL; |
| 97 | if (size < sizeof(reiserfs_acl_header)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 98 | return ERR_PTR(-EINVAL); |
| 99 | if (((reiserfs_acl_header *) value)->a_version != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | cpu_to_le32(REISERFS_ACL_VERSION)) |
| 101 | return ERR_PTR(-EINVAL); |
| 102 | value = (char *)value + sizeof(reiserfs_acl_header); |
| 103 | count = reiserfs_acl_count(size); |
| 104 | if (count < 0) |
| 105 | return ERR_PTR(-EINVAL); |
| 106 | if (count == 0) |
| 107 | return NULL; |
| 108 | acl = posix_acl_alloc(count, GFP_NOFS); |
| 109 | if (!acl) |
| 110 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 111 | for (n = 0; n < count; n++) { |
| 112 | reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | if ((char *)value + sizeof(reiserfs_acl_entry_short) > end) |
| 114 | goto fail; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 115 | acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 117 | switch (acl->a_entries[n].e_tag) { |
| 118 | case ACL_USER_OBJ: |
| 119 | case ACL_GROUP_OBJ: |
| 120 | case ACL_MASK: |
| 121 | case ACL_OTHER: |
| 122 | value = (char *)value + |
| 123 | sizeof(reiserfs_acl_entry_short); |
| 124 | acl->a_entries[n].e_id = ACL_UNDEFINED_ID; |
| 125 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 127 | case ACL_USER: |
| 128 | case ACL_GROUP: |
| 129 | value = (char *)value + sizeof(reiserfs_acl_entry); |
| 130 | if ((char *)value > end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | goto fail; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 132 | acl->a_entries[n].e_id = le32_to_cpu(entry->e_id); |
| 133 | break; |
| 134 | |
| 135 | default: |
| 136 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | if (value != end) |
| 140 | goto fail; |
| 141 | return acl; |
| 142 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 143 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | posix_acl_release(acl); |
| 145 | return ERR_PTR(-EINVAL); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Convert from in-memory to filesystem representation. |
| 150 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 151 | 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] | 152 | { |
| 153 | reiserfs_acl_header *ext_acl; |
| 154 | char *e; |
| 155 | int n; |
| 156 | |
| 157 | *size = reiserfs_acl_size(acl->a_count); |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 158 | ext_acl = kmalloc(sizeof(reiserfs_acl_header) + |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 159 | acl->a_count * |
| 160 | sizeof(reiserfs_acl_entry), |
| 161 | GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (!ext_acl) |
| 163 | return ERR_PTR(-ENOMEM); |
| 164 | ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION); |
| 165 | e = (char *)ext_acl + sizeof(reiserfs_acl_header); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 166 | for (n = 0; n < acl->a_count; n++) { |
| 167 | reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e; |
| 168 | entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 170 | switch (acl->a_entries[n].e_tag) { |
| 171 | case ACL_USER: |
| 172 | case ACL_GROUP: |
| 173 | entry->e_id = cpu_to_le32(acl->a_entries[n].e_id); |
| 174 | e += sizeof(reiserfs_acl_entry); |
| 175 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 177 | case ACL_USER_OBJ: |
| 178 | case ACL_GROUP_OBJ: |
| 179 | case ACL_MASK: |
| 180 | case ACL_OTHER: |
| 181 | e += sizeof(reiserfs_acl_entry_short); |
| 182 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 184 | default: |
| 185 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | return (char *)ext_acl; |
| 189 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 190 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | kfree(ext_acl); |
| 192 | return ERR_PTR(-EINVAL); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Inode operation get_posix_acl(). |
| 197 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 198 | * inode->i_mutex: down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | * BKL held [before 2.5.x] |
| 200 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 201 | struct posix_acl *reiserfs_get_acl(struct inode *inode, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | char *name, *value; |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 204 | struct posix_acl *acl; |
Adrian Bunk | 3cdc409 | 2006-03-25 03:07:50 -0800 | [diff] [blame] | 205 | int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 208 | acl = get_cached_acl(inode, type); |
| 209 | if (acl != ACL_NOT_CACHED) |
| 210 | return acl; |
| 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; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 215 | break; |
| 216 | case ACL_TYPE_DEFAULT: |
| 217 | name = POSIX_ACL_XATTR_DEFAULT; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 218 | break; |
| 219 | default: |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 220 | BUG(); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 223 | size = reiserfs_xattr_get(inode, name, NULL, 0); |
Adrian Bunk | 3cdc409 | 2006-03-25 03:07:50 -0800 | [diff] [blame] | 224 | if (size < 0) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 225 | if (size == -ENODATA || size == -ENOSYS) { |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 226 | set_cached_acl(inode, type, NULL); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 227 | return NULL; |
| 228 | } |
| 229 | return ERR_PTR(size); |
| 230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 232 | value = kmalloc(size, GFP_NOFS); |
| 233 | if (!value) |
| 234 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | retval = reiserfs_xattr_get(inode, name, value, size); |
| 237 | if (retval == -ENODATA || retval == -ENOSYS) { |
| 238 | /* This shouldn't actually happen as it should have |
| 239 | been caught above.. but just in case */ |
| 240 | acl = NULL; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 241 | } else if (retval < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | acl = ERR_PTR(retval); |
| 243 | } else { |
| 244 | acl = posix_acl_from_disk(value, retval); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 245 | } |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 246 | if (!IS_ERR(acl)) |
| 247 | set_cached_acl(inode, type, acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | kfree(value); |
| 250 | return acl; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Inode operation set_posix_acl(). |
| 255 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 256 | * inode->i_mutex: down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | * BKL held [before 2.5.x] |
| 258 | */ |
| 259 | static int |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 260 | reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode, |
| 261 | int type, struct posix_acl *acl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 263 | char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | void *value = NULL; |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 265 | size_t size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | if (S_ISLNK(inode->i_mode)) |
| 269 | return -EOPNOTSUPP; |
| 270 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 271 | switch (type) { |
| 272 | case ACL_TYPE_ACCESS: |
| 273 | name = POSIX_ACL_XATTR_ACCESS; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 274 | if (acl) { |
Al Viro | d695212 | 2011-07-23 18:56:36 -0400 | [diff] [blame] | 275 | error = posix_acl_equiv_mode(acl, &inode->i_mode); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 276 | if (error < 0) |
| 277 | return error; |
| 278 | else { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 279 | if (error == 0) |
| 280 | acl = NULL; |
| 281 | } |
| 282 | } |
| 283 | break; |
| 284 | case ACL_TYPE_DEFAULT: |
| 285 | name = POSIX_ACL_XATTR_DEFAULT; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 286 | if (!S_ISDIR(inode->i_mode)) |
| 287 | return acl ? -EACCES : 0; |
| 288 | break; |
| 289 | default: |
| 290 | return -EINVAL; |
| 291 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 293 | if (acl) { |
| 294 | value = posix_acl_to_disk(acl, &size); |
| 295 | if (IS_ERR(value)) |
| 296 | return (int)PTR_ERR(value); |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 299 | error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0); |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * Ensure that the inode gets dirtied if we're only using |
| 303 | * the mode bits and an old ACL didn't exist. We don't need |
| 304 | * to check if the inode is hashed here since we won't get |
| 305 | * called by reiserfs_inherit_default_acl(). |
| 306 | */ |
| 307 | if (error == -ENODATA) { |
| 308 | error = 0; |
| 309 | if (type == ACL_TYPE_ACCESS) { |
| 310 | inode->i_ctime = CURRENT_TIME_SEC; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 311 | mark_inode_dirty(inode); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
James Lamanna | 833d304 | 2005-10-30 15:00:16 -0800 | [diff] [blame] | 315 | kfree(value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Jeff Mahoney | d984561 | 2009-03-30 14:02:35 -0400 | [diff] [blame] | 317 | if (!error) |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 318 | set_cached_acl(inode, type, acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | return error; |
| 321 | } |
| 322 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 323 | /* dir->i_mutex: locked, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | * inode is new and not released into the wild yet */ |
| 325 | int |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 326 | reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th, |
| 327 | struct inode *dir, struct dentry *dentry, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 328 | struct inode *inode) |
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 | struct posix_acl *acl; |
| 331 | int err = 0; |
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 | /* ACLs only get applied to files and directories */ |
| 334 | if (S_ISLNK(inode->i_mode)) |
| 335 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 337 | /* ACLs can only be used on "new" objects, so if it's an old object |
| 338 | * there is nothing to inherit from */ |
| 339 | if (get_inode_sd_version(dir) == STAT_DATA_V1) |
| 340 | goto apply_umask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 342 | /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This |
| 343 | * would be useless since permissions are ignored, and a pain because |
| 344 | * it introduces locking cycles */ |
Jeff Mahoney | 6dfede69 | 2009-03-30 14:02:32 -0400 | [diff] [blame] | 345 | if (IS_PRIVATE(dir)) { |
| 346 | inode->i_flags |= S_PRIVATE; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 347 | goto apply_umask; |
| 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 | acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT); |
Al Viro | 7a77b15 | 2009-06-08 21:01:13 -0400 | [diff] [blame] | 351 | if (IS_ERR(acl)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 352 | return PTR_ERR(acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 354 | if (acl) { |
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)) { |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 357 | err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT, |
| 358 | acl); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 359 | if (err) |
| 360 | goto cleanup; |
| 361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 363 | /* Now we reconcile the new ACL and the mode, |
| 364 | potentially modifying both */ |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 365 | err = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode); |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 366 | if (err < 0) |
| 367 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 369 | /* If we need an ACL.. */ |
| 370 | if (err > 0) |
| 371 | err = reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, acl); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 372 | cleanup: |
| 373 | posix_acl_release(acl); |
| 374 | } else { |
| 375 | apply_umask: |
| 376 | /* no ACL, apply umask */ |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 377 | inode->i_mode &= ~current_umask(); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 380 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 383 | /* This is used to cache the default acl before a new object is created. |
| 384 | * The biggest reason for this is to get an idea of how many blocks will |
| 385 | * actually be required for the create operation if we must inherit an ACL. |
| 386 | * An ACL write can add up to 3 object creations and an additional file write |
| 387 | * so we'd prefer not to reserve that many blocks in the journal if we can. |
| 388 | * It also has the advantage of not loading the ACL with a transaction open, |
| 389 | * this may seem silly, but if the owner of the directory is doing the |
| 390 | * creation, the ACL may not be loaded since the permissions wouldn't require |
| 391 | * it. |
| 392 | * We return the number of blocks required for the transaction. |
| 393 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 394 | int reiserfs_cache_default_acl(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 396 | struct posix_acl *acl; |
| 397 | int nblocks = 0; |
| 398 | |
| 399 | if (IS_PRIVATE(inode)) |
| 400 | return 0; |
| 401 | |
| 402 | acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT); |
| 403 | |
| 404 | if (acl && !IS_ERR(acl)) { |
| 405 | int size = reiserfs_acl_size(acl->a_count); |
| 406 | |
| 407 | /* Other xattrs can be created during inode creation. We don't |
| 408 | * want to claim too many blocks, so we check to see if we |
| 409 | * we need to create the tree to the xattrs, and then we |
| 410 | * just want two files. */ |
| 411 | nblocks = reiserfs_xattr_jcreate_nblocks(inode); |
| 412 | nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb); |
| 413 | |
| 414 | REISERFS_I(inode)->i_flags |= i_has_xattr_dir; |
| 415 | |
| 416 | /* We need to account for writes + bitmaps for two files */ |
| 417 | nblocks += reiserfs_xattr_nblocks(inode, size) * 4; |
| 418 | posix_acl_release(acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 421 | return nblocks; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | int reiserfs_acl_chmod(struct inode *inode) |
| 425 | { |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 426 | struct reiserfs_transaction_handle th; |
| 427 | struct posix_acl *acl; |
| 428 | size_t size; |
| 429 | int depth; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 430 | int error; |
| 431 | |
| 432 | if (S_ISLNK(inode->i_mode)) |
| 433 | return -EOPNOTSUPP; |
| 434 | |
| 435 | if (get_inode_sd_version(inode) == STAT_DATA_V1 || |
| 436 | !reiserfs_posixacl(inode->i_sb)) { |
| 437 | return 0; |
| 438 | } |
| 439 | |
Frederic Weisbecker | 6c28705 | 2010-01-07 12:57:47 +0100 | [diff] [blame] | 440 | reiserfs_write_unlock(inode->i_sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 441 | acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS); |
Frederic Weisbecker | 6c28705 | 2010-01-07 12:57:47 +0100 | [diff] [blame] | 442 | reiserfs_write_lock(inode->i_sb); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 443 | if (!acl) |
| 444 | return 0; |
| 445 | if (IS_ERR(acl)) |
| 446 | return PTR_ERR(acl); |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 447 | error = posix_acl_chmod(&acl, GFP_NOFS, inode->i_mode); |
| 448 | if (error) |
| 449 | return error; |
Frederic Weisbecker | 238af87 | 2010-12-02 14:31:16 -0800 | [diff] [blame] | 450 | |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 451 | size = reiserfs_xattr_nblocks(inode, reiserfs_acl_size(acl->a_count)); |
| 452 | depth = reiserfs_write_lock_once(inode->i_sb); |
| 453 | error = journal_begin(&th, inode->i_sb, size * 2); |
| 454 | if (!error) { |
| 455 | int error2; |
| 456 | error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, acl); |
| 457 | error2 = journal_end(&th, inode->i_sb, size * 2); |
| 458 | if (error2) |
| 459 | error = error2; |
Jeff Mahoney | 0ab2621 | 2009-03-30 14:02:39 -0400 | [diff] [blame] | 460 | } |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 461 | reiserfs_write_unlock_once(inode->i_sb, depth); |
| 462 | posix_acl_release(acl); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 463 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 466 | static size_t posix_acl_access_list(struct dentry *dentry, char *list, |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 467 | size_t list_size, const char *name, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 468 | size_t name_len, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 470 | const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS); |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 471 | if (!reiserfs_posixacl(dentry->d_sb)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 472 | return 0; |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 473 | if (list && size <= list_size) |
| 474 | memcpy(list, POSIX_ACL_XATTR_ACCESS, size); |
| 475 | return size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Stephen Hemminger | 94d09a9 | 2010-05-13 17:53:19 -0700 | [diff] [blame] | 478 | const struct xattr_handler reiserfs_posix_acl_access_handler = { |
Christoph Hellwig | 9a59f45 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 479 | .prefix = POSIX_ACL_XATTR_ACCESS, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 480 | .flags = ACL_TYPE_ACCESS, |
| 481 | .get = posix_acl_get, |
| 482 | .set = posix_acl_set, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | .list = posix_acl_access_list, |
| 484 | }; |
| 485 | |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 486 | static size_t posix_acl_default_list(struct dentry *dentry, char *list, |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 487 | size_t list_size, const char *name, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 488 | size_t name_len, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 490 | const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT); |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 491 | if (!reiserfs_posixacl(dentry->d_sb)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 492 | return 0; |
Jeff Mahoney | 48b32a3 | 2009-03-30 14:02:38 -0400 | [diff] [blame] | 493 | if (list && size <= list_size) |
| 494 | memcpy(list, POSIX_ACL_XATTR_DEFAULT, size); |
| 495 | return size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Stephen Hemminger | 94d09a9 | 2010-05-13 17:53:19 -0700 | [diff] [blame] | 498 | const struct xattr_handler reiserfs_posix_acl_default_handler = { |
Christoph Hellwig | 9a59f45 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 499 | .prefix = POSIX_ACL_XATTR_DEFAULT, |
Christoph Hellwig | 431547b | 2009-11-13 09:52:56 +0000 | [diff] [blame] | 500 | .flags = ACL_TYPE_DEFAULT, |
| 501 | .get = posix_acl_get, |
| 502 | .set = posix_acl_set, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | .list = posix_acl_default_list, |
| 504 | }; |