]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/reiserfs/xattr_acl.c
reiserfs: cleanup, rename key and item accessors to more friendly names
[mirror_ubuntu-bionic-kernel.git] / fs / reiserfs / xattr_acl.c
CommitLineData
16f7e0fe 1#include <linux/capability.h>
1da177e4
LT
2#include <linux/fs.h>
3#include <linux/posix_acl.h>
f466c6fd 4#include "reiserfs.h"
1da177e4
LT
5#include <linux/errno.h>
6#include <linux/pagemap.h>
7#include <linux/xattr.h>
5a0e3ad6 8#include <linux/slab.h>
9a59f452 9#include <linux/posix_acl_xattr.h>
c45ac888 10#include "xattr.h"
a3063ab8 11#include "acl.h"
1da177e4
LT
12#include <asm/uaccess.h>
13
47f70d08 14static int __reiserfs_set_acl(struct reiserfs_transaction_handle *th,
0ab2621e 15 struct inode *inode, int type,
bd4c625c 16 struct posix_acl *acl);
1da177e4 17
47f70d08
CH
18
19int
20reiserfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
1da177e4 21{
0ab2621e
JM
22 int error, error2;
23 struct reiserfs_transaction_handle th;
24 size_t jcreate_blocks;
47f70d08
CH
25 int size = acl ? posix_acl_xattr_size(acl->a_count) : 0;
26
1da177e4 27
0ab2621e
JM
28 /* Pessimism: We can't assume that anything from the xattr root up
29 * has been created. */
30
31 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
32 reiserfs_xattr_nblocks(inode, size) * 2;
33
34 reiserfs_write_lock(inode->i_sb);
35 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
4c05141d 36 reiserfs_write_unlock(inode->i_sb);
0ab2621e 37 if (error == 0) {
47f70d08 38 error = __reiserfs_set_acl(&th, inode, type, acl);
4c05141d 39 reiserfs_write_lock(inode->i_sb);
0ab2621e 40 error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
4c05141d 41 reiserfs_write_unlock(inode->i_sb);
0ab2621e
JM
42 if (error2)
43 error = error2;
44 }
1da177e4 45
1da177e4
LT
46 return error;
47}
48
1da177e4
LT
49/*
50 * Convert from filesystem to in-memory representation.
51 */
9dad943a 52static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size)
1da177e4
LT
53{
54 const char *end = (char *)value + size;
55 int n, count;
56 struct posix_acl *acl;
57
58 if (!value)
59 return NULL;
60 if (size < sizeof(reiserfs_acl_header))
bd4c625c
LT
61 return ERR_PTR(-EINVAL);
62 if (((reiserfs_acl_header *) value)->a_version !=
1da177e4
LT
63 cpu_to_le32(REISERFS_ACL_VERSION))
64 return ERR_PTR(-EINVAL);
65 value = (char *)value + sizeof(reiserfs_acl_header);
66 count = reiserfs_acl_count(size);
67 if (count < 0)
68 return ERR_PTR(-EINVAL);
69 if (count == 0)
70 return NULL;
71 acl = posix_acl_alloc(count, GFP_NOFS);
72 if (!acl)
73 return ERR_PTR(-ENOMEM);
bd4c625c
LT
74 for (n = 0; n < count; n++) {
75 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
1da177e4
LT
76 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
77 goto fail;
bd4c625c 78 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
1da177e4 79 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
bd4c625c
LT
80 switch (acl->a_entries[n].e_tag) {
81 case ACL_USER_OBJ:
82 case ACL_GROUP_OBJ:
83 case ACL_MASK:
84 case ACL_OTHER:
85 value = (char *)value +
86 sizeof(reiserfs_acl_entry_short);
bd4c625c
LT
87 break;
88
89 case ACL_USER:
df814654
EB
90 value = (char *)value + sizeof(reiserfs_acl_entry);
91 if ((char *)value > end)
92 goto fail;
93 acl->a_entries[n].e_uid =
94 make_kuid(&init_user_ns,
95 le32_to_cpu(entry->e_id));
96 break;
bd4c625c
LT
97 case ACL_GROUP:
98 value = (char *)value + sizeof(reiserfs_acl_entry);
99 if ((char *)value > end)
1da177e4 100 goto fail;
df814654
EB
101 acl->a_entries[n].e_gid =
102 make_kgid(&init_user_ns,
103 le32_to_cpu(entry->e_id));
bd4c625c
LT
104 break;
105
106 default:
107 goto fail;
1da177e4
LT
108 }
109 }
110 if (value != end)
111 goto fail;
112 return acl;
113
bd4c625c 114 fail:
1da177e4
LT
115 posix_acl_release(acl);
116 return ERR_PTR(-EINVAL);
117}
118
119/*
120 * Convert from in-memory to filesystem representation.
121 */
9dad943a 122static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
1da177e4
LT
123{
124 reiserfs_acl_header *ext_acl;
125 char *e;
126 int n;
127
128 *size = reiserfs_acl_size(acl->a_count);
5cbded58 129 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
bd4c625c
LT
130 acl->a_count *
131 sizeof(reiserfs_acl_entry),
132 GFP_NOFS);
1da177e4
LT
133 if (!ext_acl)
134 return ERR_PTR(-ENOMEM);
135 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
136 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
bd4c625c 137 for (n = 0; n < acl->a_count; n++) {
df814654 138 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
bd4c625c
LT
139 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
140 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
1da177e4 141 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
bd4c625c
LT
142 switch (acl->a_entries[n].e_tag) {
143 case ACL_USER:
df814654
EB
144 entry->e_id = cpu_to_le32(
145 from_kuid(&init_user_ns, acl_e->e_uid));
146 e += sizeof(reiserfs_acl_entry);
147 break;
bd4c625c 148 case ACL_GROUP:
df814654
EB
149 entry->e_id = cpu_to_le32(
150 from_kgid(&init_user_ns, acl_e->e_gid));
bd4c625c
LT
151 e += sizeof(reiserfs_acl_entry);
152 break;
153
154 case ACL_USER_OBJ:
155 case ACL_GROUP_OBJ:
156 case ACL_MASK:
157 case ACL_OTHER:
158 e += sizeof(reiserfs_acl_entry_short);
159 break;
160
161 default:
162 goto fail;
1da177e4
LT
163 }
164 }
165 return (char *)ext_acl;
166
bd4c625c 167 fail:
1da177e4
LT
168 kfree(ext_acl);
169 return ERR_PTR(-EINVAL);
170}
171
172/*
173 * Inode operation get_posix_acl().
174 *
1b1dcc1b 175 * inode->i_mutex: down
1da177e4
LT
176 * BKL held [before 2.5.x]
177 */
bd4c625c 178struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
1da177e4
LT
179{
180 char *name, *value;
073aaa1b 181 struct posix_acl *acl;
3cdc409c 182 int size;
1da177e4 183 int retval;
bd4c625c
LT
184
185 switch (type) {
186 case ACL_TYPE_ACCESS:
187 name = POSIX_ACL_XATTR_ACCESS;
bd4c625c
LT
188 break;
189 case ACL_TYPE_DEFAULT:
190 name = POSIX_ACL_XATTR_DEFAULT;
bd4c625c
LT
191 break;
192 default:
073aaa1b 193 BUG();
bd4c625c
LT
194 }
195
bd4c625c 196 size = reiserfs_xattr_get(inode, name, NULL, 0);
3cdc409c 197 if (size < 0) {
bd4c625c 198 if (size == -ENODATA || size == -ENOSYS) {
073aaa1b 199 set_cached_acl(inode, type, NULL);
bd4c625c
LT
200 return NULL;
201 }
202 return ERR_PTR(size);
203 }
1da177e4 204
bd4c625c
LT
205 value = kmalloc(size, GFP_NOFS);
206 if (!value)
207 return ERR_PTR(-ENOMEM);
1da177e4
LT
208
209 retval = reiserfs_xattr_get(inode, name, value, size);
210 if (retval == -ENODATA || retval == -ENOSYS) {
211 /* This shouldn't actually happen as it should have
212 been caught above.. but just in case */
213 acl = NULL;
bd4c625c 214 } else if (retval < 0) {
1da177e4
LT
215 acl = ERR_PTR(retval);
216 } else {
9dad943a 217 acl = reiserfs_posix_acl_from_disk(value, retval);
bd4c625c 218 }
073aaa1b
AV
219 if (!IS_ERR(acl))
220 set_cached_acl(inode, type, acl);
1da177e4
LT
221
222 kfree(value);
223 return acl;
224}
225
226/*
227 * Inode operation set_posix_acl().
228 *
1b1dcc1b 229 * inode->i_mutex: down
1da177e4
LT
230 * BKL held [before 2.5.x]
231 */
232static int
47f70d08 233__reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
0ab2621e 234 int type, struct posix_acl *acl)
1da177e4 235{
bd4c625c 236 char *name;
1da177e4 237 void *value = NULL;
48b32a35 238 size_t size = 0;
1da177e4 239 int error;
1da177e4 240
bd4c625c
LT
241 switch (type) {
242 case ACL_TYPE_ACCESS:
243 name = POSIX_ACL_XATTR_ACCESS;
bd4c625c 244 if (acl) {
d6952123 245 error = posix_acl_equiv_mode(acl, &inode->i_mode);
bd4c625c
LT
246 if (error < 0)
247 return error;
248 else {
bd4c625c
LT
249 if (error == 0)
250 acl = NULL;
251 }
252 }
253 break;
254 case ACL_TYPE_DEFAULT:
255 name = POSIX_ACL_XATTR_DEFAULT;
bd4c625c
LT
256 if (!S_ISDIR(inode->i_mode))
257 return acl ? -EACCES : 0;
258 break;
259 default:
260 return -EINVAL;
261 }
262
263 if (acl) {
9dad943a 264 value = reiserfs_posix_acl_to_disk(acl, &size);
bd4c625c
LT
265 if (IS_ERR(value))
266 return (int)PTR_ERR(value);
48b32a35
JM
267 }
268
0ab2621e 269 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
48b32a35
JM
270
271 /*
272 * Ensure that the inode gets dirtied if we're only using
273 * the mode bits and an old ACL didn't exist. We don't need
274 * to check if the inode is hashed here since we won't get
275 * called by reiserfs_inherit_default_acl().
276 */
277 if (error == -ENODATA) {
278 error = 0;
279 if (type == ACL_TYPE_ACCESS) {
280 inode->i_ctime = CURRENT_TIME_SEC;
bd4c625c 281 mark_inode_dirty(inode);
bd4c625c
LT
282 }
283 }
1da177e4 284
833d304b 285 kfree(value);
1da177e4 286
d984561b 287 if (!error)
073aaa1b 288 set_cached_acl(inode, type, acl);
1da177e4
LT
289
290 return error;
291}
292
1b1dcc1b 293/* dir->i_mutex: locked,
1da177e4
LT
294 * inode is new and not released into the wild yet */
295int
0ab2621e
JM
296reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
297 struct inode *dir, struct dentry *dentry,
bd4c625c 298 struct inode *inode)
1da177e4 299{
47f70d08 300 struct posix_acl *default_acl, *acl;
bd4c625c
LT
301 int err = 0;
302
303 /* ACLs only get applied to files and directories */
304 if (S_ISLNK(inode->i_mode))
305 return 0;
306
307 /* ACLs can only be used on "new" objects, so if it's an old object
308 * there is nothing to inherit from */
309 if (get_inode_sd_version(dir) == STAT_DATA_V1)
310 goto apply_umask;
311
312 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
313 * would be useless since permissions are ignored, and a pain because
314 * it introduces locking cycles */
6dfede69
JM
315 if (IS_PRIVATE(dir)) {
316 inode->i_flags |= S_PRIVATE;
bd4c625c
LT
317 goto apply_umask;
318 }
319
47f70d08
CH
320 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
321 if (err)
322 return err;
bd4c625c 323
47f70d08
CH
324 if (default_acl) {
325 err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
326 default_acl);
327 posix_acl_release(default_acl);
328 }
bd4c625c 329 if (acl) {
47f70d08
CH
330 if (!err)
331 err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
332 acl);
bd4c625c 333 posix_acl_release(acl);
bd4c625c
LT
334 }
335
336 return err;
47f70d08
CH
337
338 apply_umask:
339 /* no ACL, apply umask */
340 inode->i_mode &= ~current_umask();
341 return err;
1da177e4
LT
342}
343
0ab2621e
JM
344/* This is used to cache the default acl before a new object is created.
345 * The biggest reason for this is to get an idea of how many blocks will
346 * actually be required for the create operation if we must inherit an ACL.
347 * An ACL write can add up to 3 object creations and an additional file write
348 * so we'd prefer not to reserve that many blocks in the journal if we can.
349 * It also has the advantage of not loading the ACL with a transaction open,
350 * this may seem silly, but if the owner of the directory is doing the
351 * creation, the ACL may not be loaded since the permissions wouldn't require
352 * it.
353 * We return the number of blocks required for the transaction.
354 */
bd4c625c 355int reiserfs_cache_default_acl(struct inode *inode)
1da177e4 356{
0ab2621e
JM
357 struct posix_acl *acl;
358 int nblocks = 0;
359
360 if (IS_PRIVATE(inode))
361 return 0;
362
363 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
364
365 if (acl && !IS_ERR(acl)) {
366 int size = reiserfs_acl_size(acl->a_count);
367
368 /* Other xattrs can be created during inode creation. We don't
369 * want to claim too many blocks, so we check to see if we
370 * we need to create the tree to the xattrs, and then we
371 * just want two files. */
372 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
373 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
374
375 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
376
377 /* We need to account for writes + bitmaps for two files */
378 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
379 posix_acl_release(acl);
bd4c625c
LT
380 }
381
0ab2621e 382 return nblocks;
1da177e4
LT
383}
384
4c05141d
JM
385/*
386 * Called under i_mutex
387 */
bd4c625c 388int reiserfs_acl_chmod(struct inode *inode)
1da177e4 389{
4a857011
JM
390 if (IS_PRIVATE(inode))
391 return 0;
bd4c625c 392 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
47f70d08 393 !reiserfs_posixacl(inode->i_sb))
bd4c625c 394 return 0;
1da177e4 395
47f70d08 396 return posix_acl_chmod(inode, inode->i_mode);
1da177e4 397}