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