Blame view
fs/ext4/acl.c
5.95 KB
ac27a0ec1 [PATCH] ext4: ini... |
1 |
/* |
617ba13b3 [PATCH] ext4: ren... |
2 |
* linux/fs/ext4/acl.c |
ac27a0ec1 [PATCH] ext4: ini... |
3 4 5 |
* * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> */ |
3dcf54515 ext4: move header... |
6 7 |
#include "ext4_jbd2.h" #include "ext4.h" |
ac27a0ec1 [PATCH] ext4: ini... |
8 9 10 11 12 13 14 |
#include "xattr.h" #include "acl.h" /* * Convert from filesystem to in-memory representation. */ static struct posix_acl * |
617ba13b3 [PATCH] ext4: ren... |
15 |
ext4_acl_from_disk(const void *value, size_t size) |
ac27a0ec1 [PATCH] ext4: ini... |
16 17 18 19 20 21 22 |
{ const char *end = (char *)value + size; int n, count; struct posix_acl *acl; if (!value) return NULL; |
617ba13b3 [PATCH] ext4: ren... |
23 |
if (size < sizeof(ext4_acl_header)) |
ac27a0ec1 [PATCH] ext4: ini... |
24 |
return ERR_PTR(-EINVAL); |
617ba13b3 [PATCH] ext4: ren... |
25 26 |
if (((ext4_acl_header *)value)->a_version != cpu_to_le32(EXT4_ACL_VERSION)) |
ac27a0ec1 [PATCH] ext4: ini... |
27 |
return ERR_PTR(-EINVAL); |
617ba13b3 [PATCH] ext4: ren... |
28 29 |
value = (char *)value + sizeof(ext4_acl_header); count = ext4_acl_count(size); |
ac27a0ec1 [PATCH] ext4: ini... |
30 31 32 33 |
if (count < 0) return ERR_PTR(-EINVAL); if (count == 0) return NULL; |
216553c4b ext4: fix wrong g... |
34 |
acl = posix_acl_alloc(count, GFP_NOFS); |
ac27a0ec1 [PATCH] ext4: ini... |
35 36 |
if (!acl) return ERR_PTR(-ENOMEM); |
2b2d6d019 ext4: Cleanup whi... |
37 |
for (n = 0; n < count; n++) { |
617ba13b3 [PATCH] ext4: ren... |
38 39 40 |
ext4_acl_entry *entry = (ext4_acl_entry *)value; if ((char *)value + sizeof(ext4_acl_entry_short) > end) |
ac27a0ec1 [PATCH] ext4: ini... |
41 42 43 |
goto fail; acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); |
2b2d6d019 ext4: Cleanup whi... |
44 45 46 47 48 49 50 51 |
switch (acl->a_entries[n].e_tag) { case ACL_USER_OBJ: case ACL_GROUP_OBJ: case ACL_MASK: case ACL_OTHER: value = (char *)value + sizeof(ext4_acl_entry_short); |
2b2d6d019 ext4: Cleanup whi... |
52 53 54 |
break; case ACL_USER: |
af84df93f userns: Convert e... |
55 56 57 58 59 60 61 |
value = (char *)value + sizeof(ext4_acl_entry); if ((char *)value > end) goto fail; acl->a_entries[n].e_uid = make_kuid(&init_user_ns, le32_to_cpu(entry->e_id)); break; |
2b2d6d019 ext4: Cleanup whi... |
62 63 64 |
case ACL_GROUP: value = (char *)value + sizeof(ext4_acl_entry); if ((char *)value > end) |
ac27a0ec1 [PATCH] ext4: ini... |
65 |
goto fail; |
af84df93f userns: Convert e... |
66 67 68 |
acl->a_entries[n].e_gid = make_kgid(&init_user_ns, le32_to_cpu(entry->e_id)); |
2b2d6d019 ext4: Cleanup whi... |
69 70 71 72 |
break; default: goto fail; |
ac27a0ec1 [PATCH] ext4: ini... |
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
} } if (value != end) goto fail; return acl; fail: posix_acl_release(acl); return ERR_PTR(-EINVAL); } /* * Convert from in-memory to filesystem representation. */ static void * |
617ba13b3 [PATCH] ext4: ren... |
88 |
ext4_acl_to_disk(const struct posix_acl *acl, size_t *size) |
ac27a0ec1 [PATCH] ext4: ini... |
89 |
{ |
617ba13b3 [PATCH] ext4: ren... |
90 |
ext4_acl_header *ext_acl; |
ac27a0ec1 [PATCH] ext4: ini... |
91 92 |
char *e; size_t n; |
617ba13b3 [PATCH] ext4: ren... |
93 94 |
*size = ext4_acl_size(acl->a_count); ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count * |
216553c4b ext4: fix wrong g... |
95 |
sizeof(ext4_acl_entry), GFP_NOFS); |
ac27a0ec1 [PATCH] ext4: ini... |
96 97 |
if (!ext_acl) return ERR_PTR(-ENOMEM); |
617ba13b3 [PATCH] ext4: ren... |
98 99 |
ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION); e = (char *)ext_acl + sizeof(ext4_acl_header); |
2b2d6d019 ext4: Cleanup whi... |
100 |
for (n = 0; n < acl->a_count; n++) { |
af84df93f userns: Convert e... |
101 |
const struct posix_acl_entry *acl_e = &acl->a_entries[n]; |
617ba13b3 [PATCH] ext4: ren... |
102 |
ext4_acl_entry *entry = (ext4_acl_entry *)e; |
af84df93f userns: Convert e... |
103 104 105 |
entry->e_tag = cpu_to_le16(acl_e->e_tag); entry->e_perm = cpu_to_le16(acl_e->e_perm); switch (acl_e->e_tag) { |
2b2d6d019 ext4: Cleanup whi... |
106 |
case ACL_USER: |
af84df93f userns: Convert e... |
107 108 109 110 |
entry->e_id = cpu_to_le32( from_kuid(&init_user_ns, acl_e->e_uid)); e += sizeof(ext4_acl_entry); break; |
2b2d6d019 ext4: Cleanup whi... |
111 |
case ACL_GROUP: |
af84df93f userns: Convert e... |
112 113 |
entry->e_id = cpu_to_le32( from_kgid(&init_user_ns, acl_e->e_gid)); |
2b2d6d019 ext4: Cleanup whi... |
114 115 116 117 118 119 120 121 122 123 124 125 |
e += sizeof(ext4_acl_entry); break; case ACL_USER_OBJ: case ACL_GROUP_OBJ: case ACL_MASK: case ACL_OTHER: e += sizeof(ext4_acl_entry_short); break; default: goto fail; |
ac27a0ec1 [PATCH] ext4: ini... |
126 127 128 129 130 131 132 133 |
} } return (char *)ext_acl; fail: kfree(ext_acl); return ERR_PTR(-EINVAL); } |
ac27a0ec1 [PATCH] ext4: ini... |
134 135 136 137 138 |
/* * Inode operation get_posix_acl(). * * inode->i_mutex: don't care */ |
4e34e719e fs: take the ACL ... |
139 |
struct posix_acl * |
617ba13b3 [PATCH] ext4: ren... |
140 |
ext4_get_acl(struct inode *inode, int type) |
ac27a0ec1 [PATCH] ext4: ini... |
141 |
{ |
ac27a0ec1 [PATCH] ext4: ini... |
142 143 144 145 |
int name_index; char *value = NULL; struct posix_acl *acl; int retval; |
2b2d6d019 ext4: Cleanup whi... |
146 147 |
switch (type) { case ACL_TYPE_ACCESS: |
2b2d6d019 ext4: Cleanup whi... |
148 149 |
name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; break; |
2b2d6d019 ext4: Cleanup whi... |
150 |
case ACL_TYPE_DEFAULT: |
2b2d6d019 ext4: Cleanup whi... |
151 152 |
name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; break; |
2b2d6d019 ext4: Cleanup whi... |
153 |
default: |
073aaa1b1 helpers for acl c... |
154 |
BUG(); |
ac27a0ec1 [PATCH] ext4: ini... |
155 |
} |
617ba13b3 [PATCH] ext4: ren... |
156 |
retval = ext4_xattr_get(inode, name_index, "", NULL, 0); |
ac27a0ec1 [PATCH] ext4: ini... |
157 |
if (retval > 0) { |
216553c4b ext4: fix wrong g... |
158 |
value = kmalloc(retval, GFP_NOFS); |
ac27a0ec1 [PATCH] ext4: ini... |
159 160 |
if (!value) return ERR_PTR(-ENOMEM); |
617ba13b3 [PATCH] ext4: ren... |
161 |
retval = ext4_xattr_get(inode, name_index, "", value, retval); |
ac27a0ec1 [PATCH] ext4: ini... |
162 163 |
} if (retval > 0) |
617ba13b3 [PATCH] ext4: ren... |
164 |
acl = ext4_acl_from_disk(value, retval); |
ac27a0ec1 [PATCH] ext4: ini... |
165 166 167 168 169 |
else if (retval == -ENODATA || retval == -ENOSYS) acl = NULL; else acl = ERR_PTR(retval); kfree(value); |
ac27a0ec1 [PATCH] ext4: ini... |
170 171 172 173 174 175 |
return acl; } /* * Set the access or default ACL of an inode. * |
617ba13b3 [PATCH] ext4: ren... |
176 |
* inode->i_mutex: down unless called from ext4_new_inode |
ac27a0ec1 [PATCH] ext4: ini... |
177 178 |
*/ static int |
64e178a71 ext2/3/4: use gen... |
179 |
__ext4_set_acl(handle_t *handle, struct inode *inode, int type, |
ac27a0ec1 [PATCH] ext4: ini... |
180 181 |
struct posix_acl *acl) { |
ac27a0ec1 [PATCH] ext4: ini... |
182 183 184 185 |
int name_index; void *value = NULL; size_t size = 0; int error; |
2b2d6d019 ext4: Cleanup whi... |
186 187 188 189 |
switch (type) { case ACL_TYPE_ACCESS: name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; if (acl) { |
073931017 posix_acl: Clear ... |
190 191 |
error = posix_acl_update_mode(inode, &inode->i_mode, &acl); if (error) |
2b2d6d019 ext4: Cleanup whi... |
192 |
return error; |
073931017 posix_acl: Clear ... |
193 194 |
inode->i_ctime = ext4_current_time(inode); ext4_mark_inode_dirty(handle, inode); |
2b2d6d019 ext4: Cleanup whi... |
195 196 |
} break; |
ac27a0ec1 [PATCH] ext4: ini... |
197 |
|
2b2d6d019 ext4: Cleanup whi... |
198 199 200 201 202 |
case ACL_TYPE_DEFAULT: name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; if (!S_ISDIR(inode->i_mode)) return acl ? -EACCES : 0; break; |
ac27a0ec1 [PATCH] ext4: ini... |
203 |
|
2b2d6d019 ext4: Cleanup whi... |
204 205 |
default: return -EINVAL; |
ac27a0ec1 [PATCH] ext4: ini... |
206 207 |
} if (acl) { |
617ba13b3 [PATCH] ext4: ren... |
208 |
value = ext4_acl_to_disk(acl, &size); |
ac27a0ec1 [PATCH] ext4: ini... |
209 210 211 |
if (IS_ERR(value)) return (int)PTR_ERR(value); } |
617ba13b3 [PATCH] ext4: ren... |
212 |
error = ext4_xattr_set_handle(handle, inode, name_index, "", |
ac27a0ec1 [PATCH] ext4: ini... |
213 214 215 |
value, size, 0); kfree(value); |
073aaa1b1 helpers for acl c... |
216 217 |
if (!error) set_cached_acl(inode, type, acl); |
2b2d6d019 ext4: Cleanup whi... |
218 |
|
ac27a0ec1 [PATCH] ext4: ini... |
219 220 |
return error; } |
ac27a0ec1 [PATCH] ext4: ini... |
221 |
int |
64e178a71 ext2/3/4: use gen... |
222 |
ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
ac27a0ec1 [PATCH] ext4: ini... |
223 |
{ |
bc26ab5f6 kill boilerplate ... |
224 |
handle_t *handle; |
64e178a71 ext2/3/4: use gen... |
225 |
int error, retries = 0; |
bc26ab5f6 kill boilerplate ... |
226 |
|
bc26ab5f6 kill boilerplate ... |
227 |
retry: |
9924a92a8 ext4: pass contex... |
228 |
handle = ext4_journal_start(inode, EXT4_HT_XATTR, |
95eaefbde ext4: fix the num... |
229 |
ext4_jbd2_credits_xattr(inode)); |
64e178a71 ext2/3/4: use gen... |
230 231 232 233 |
if (IS_ERR(handle)) return PTR_ERR(handle); error = __ext4_set_acl(handle, inode, type, acl); |
bc26ab5f6 kill boilerplate ... |
234 |
ext4_journal_stop(handle); |
64e178a71 ext2/3/4: use gen... |
235 |
if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
bc26ab5f6 kill boilerplate ... |
236 |
goto retry; |
ac27a0ec1 [PATCH] ext4: ini... |
237 238 239 240 |
return error; } /* |
64e178a71 ext2/3/4: use gen... |
241 242 243 244 |
* Initialize the ACLs of a new inode. Called from ext4_new_inode. * * dir->i_mutex: down * inode->i_mutex: up (access to inode is still exclusive) |
ac27a0ec1 [PATCH] ext4: ini... |
245 |
*/ |
64e178a71 ext2/3/4: use gen... |
246 247 |
int ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir) |
ac27a0ec1 [PATCH] ext4: ini... |
248 |
{ |
64e178a71 ext2/3/4: use gen... |
249 |
struct posix_acl *default_acl, *acl; |
ac27a0ec1 [PATCH] ext4: ini... |
250 |
int error; |
64e178a71 ext2/3/4: use gen... |
251 252 253 |
error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); if (error) return error; |
ac27a0ec1 [PATCH] ext4: ini... |
254 |
|
64e178a71 ext2/3/4: use gen... |
255 256 257 258 259 260 261 262 263 264 |
if (default_acl) { error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT, default_acl); posix_acl_release(default_acl); } if (acl) { if (!error) error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS, acl); posix_acl_release(acl); |
24ec19b0a ext4: fix memory ... |
265 |
} |
ac27a0ec1 [PATCH] ext4: ini... |
266 267 |
return error; } |