blob: 7b3aeb9327d30b8c904b93a762b812e2860b940f [file] [log] [blame]
Randy Dunlap16f7e0f2006-01-11 12:17:46 -08001#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#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>
Christoph Hellwig9a59f452005-06-23 00:10:19 -07008#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/reiserfs_xattr.h>
10#include <linux/reiserfs_acl.h>
11#include <asm/uaccess.h>
12
Jeff Mahoney0ab26212009-03-30 14:02:39 -040013static int reiserfs_set_acl(struct reiserfs_transaction_handle *th,
14 struct inode *inode, int type,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070015 struct posix_acl *acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17static int
18xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
19{
20 struct posix_acl *acl;
Jeff Mahoney0ab26212009-03-30 14:02:39 -040021 int error, error2;
22 struct reiserfs_transaction_handle th;
23 size_t jcreate_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 if (!reiserfs_posixacl(inode->i_sb))
25 return -EOPNOTSUPP;
Satyam Sharma3bd858a2007-07-17 15:00:08 +053026 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 return -EPERM;
28
29 if (value) {
30 acl = posix_acl_from_xattr(value, size);
31 if (IS_ERR(acl)) {
32 return PTR_ERR(acl);
33 } else if (acl) {
34 error = posix_acl_valid(acl);
35 if (error)
36 goto release_and_out;
37 }
38 } else
39 acl = NULL;
40
Jeff Mahoney0ab26212009-03-30 14:02:39 -040041 /* Pessimism: We can't assume that anything from the xattr root up
42 * has been created. */
43
44 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
45 reiserfs_xattr_nblocks(inode, size) * 2;
46
47 reiserfs_write_lock(inode->i_sb);
48 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
49 if (error == 0) {
50 error = reiserfs_set_acl(&th, inode, type, acl);
51 error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
52 if (error2)
53 error = error2;
54 }
55 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070057 release_and_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 posix_acl_release(acl);
59 return error;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static int
63xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
64{
65 struct posix_acl *acl;
66 int error;
67
68 if (!reiserfs_posixacl(inode->i_sb))
69 return -EOPNOTSUPP;
70
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070071 acl = reiserfs_get_acl(inode, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (IS_ERR(acl))
73 return PTR_ERR(acl);
74 if (acl == NULL)
75 return -ENODATA;
76 error = posix_acl_to_xattr(acl, buffer, size);
77 posix_acl_release(acl);
78
79 return error;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * Convert from filesystem to in-memory representation.
84 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 const char *end = (char *)value + size;
88 int n, count;
89 struct posix_acl *acl;
90
91 if (!value)
92 return NULL;
93 if (size < sizeof(reiserfs_acl_header))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070094 return ERR_PTR(-EINVAL);
95 if (((reiserfs_acl_header *) value)->a_version !=
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 cpu_to_le32(REISERFS_ACL_VERSION))
97 return ERR_PTR(-EINVAL);
98 value = (char *)value + sizeof(reiserfs_acl_header);
99 count = reiserfs_acl_count(size);
100 if (count < 0)
101 return ERR_PTR(-EINVAL);
102 if (count == 0)
103 return NULL;
104 acl = posix_acl_alloc(count, GFP_NOFS);
105 if (!acl)
106 return ERR_PTR(-ENOMEM);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700107 for (n = 0; n < count; n++) {
108 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
110 goto fail;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700111 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700113 switch (acl->a_entries[n].e_tag) {
114 case ACL_USER_OBJ:
115 case ACL_GROUP_OBJ:
116 case ACL_MASK:
117 case ACL_OTHER:
118 value = (char *)value +
119 sizeof(reiserfs_acl_entry_short);
120 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
121 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700123 case ACL_USER:
124 case ACL_GROUP:
125 value = (char *)value + sizeof(reiserfs_acl_entry);
126 if ((char *)value > end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 goto fail;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700128 acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
129 break;
130
131 default:
132 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134 }
135 if (value != end)
136 goto fail;
137 return acl;
138
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700139 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 posix_acl_release(acl);
141 return ERR_PTR(-EINVAL);
142}
143
144/*
145 * Convert from in-memory to filesystem representation.
146 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700147static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 reiserfs_acl_header *ext_acl;
150 char *e;
151 int n;
152
153 *size = reiserfs_acl_size(acl->a_count);
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800154 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700155 acl->a_count *
156 sizeof(reiserfs_acl_entry),
157 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (!ext_acl)
159 return ERR_PTR(-ENOMEM);
160 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
161 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700162 for (n = 0; n < acl->a_count; n++) {
163 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
164 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700166 switch (acl->a_entries[n].e_tag) {
167 case ACL_USER:
168 case ACL_GROUP:
169 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
170 e += sizeof(reiserfs_acl_entry);
171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700173 case ACL_USER_OBJ:
174 case ACL_GROUP_OBJ:
175 case ACL_MASK:
176 case ACL_OTHER:
177 e += sizeof(reiserfs_acl_entry_short);
178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700180 default:
181 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 }
184 return (char *)ext_acl;
185
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700186 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kfree(ext_acl);
188 return ERR_PTR(-EINVAL);
189}
190
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400191static inline void iset_acl(struct inode *inode, struct posix_acl **i_acl,
192 struct posix_acl *acl)
193{
194 spin_lock(&inode->i_lock);
Al Viro7a77b152009-06-08 21:01:13 -0400195 if (*i_acl != ACL_NOT_CACHED)
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400196 posix_acl_release(*i_acl);
Al Viro7a77b152009-06-08 21:01:13 -0400197 *i_acl = posix_acl_dup(acl);
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400198 spin_unlock(&inode->i_lock);
199}
200
201static inline struct posix_acl *iget_acl(struct inode *inode,
202 struct posix_acl **i_acl)
203{
Al Viro7a77b152009-06-08 21:01:13 -0400204 struct posix_acl *acl = ACL_NOT_CACHED;
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400205
206 spin_lock(&inode->i_lock);
Al Viro7a77b152009-06-08 21:01:13 -0400207 if (*i_acl != ACL_NOT_CACHED)
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400208 acl = posix_acl_dup(*i_acl);
209 spin_unlock(&inode->i_lock);
210
211 return acl;
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
215 * Inode operation get_posix_acl().
216 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800217 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * BKL held [before 2.5.x]
219 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700220struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 char *name, *value;
223 struct posix_acl *acl, **p_acl;
Adrian Bunk3cdc4092006-03-25 03:07:50 -0800224 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700226 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700228 switch (type) {
229 case ACL_TYPE_ACCESS:
230 name = POSIX_ACL_XATTR_ACCESS;
231 p_acl = &reiserfs_i->i_acl_access;
232 break;
233 case ACL_TYPE_DEFAULT:
234 name = POSIX_ACL_XATTR_DEFAULT;
235 p_acl = &reiserfs_i->i_acl_default;
236 break;
237 default:
238 return ERR_PTR(-EINVAL);
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400241 acl = iget_acl(inode, p_acl);
Al Viro7a77b152009-06-08 21:01:13 -0400242 if (acl != ACL_NOT_CACHED)
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400243 return acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700245 size = reiserfs_xattr_get(inode, name, NULL, 0);
Adrian Bunk3cdc4092006-03-25 03:07:50 -0800246 if (size < 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700247 if (size == -ENODATA || size == -ENOSYS) {
Al Viro7a77b152009-06-08 21:01:13 -0400248 *p_acl = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700249 return NULL;
250 }
251 return ERR_PTR(size);
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700254 value = kmalloc(size, GFP_NOFS);
255 if (!value)
256 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 retval = reiserfs_xattr_get(inode, name, value, size);
259 if (retval == -ENODATA || retval == -ENOSYS) {
260 /* This shouldn't actually happen as it should have
261 been caught above.. but just in case */
262 acl = NULL;
Al Viro7a77b152009-06-08 21:01:13 -0400263 *p_acl = acl;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700264 } else if (retval < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 acl = ERR_PTR(retval);
266 } else {
267 acl = posix_acl_from_disk(value, retval);
Jeff Mahoney90947ef2006-02-13 11:12:36 -0500268 if (!IS_ERR(acl))
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400269 iset_acl(inode, p_acl, acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 kfree(value);
273 return acl;
274}
275
276/*
277 * Inode operation set_posix_acl().
278 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800279 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * BKL held [before 2.5.x]
281 */
282static int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400283reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
284 int type, struct posix_acl *acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700286 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 void *value = NULL;
288 struct posix_acl **p_acl;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400289 size_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int error;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700291 struct reiserfs_inode_info *reiserfs_i = REISERFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 if (S_ISLNK(inode->i_mode))
294 return -EOPNOTSUPP;
295
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700296 switch (type) {
297 case ACL_TYPE_ACCESS:
298 name = POSIX_ACL_XATTR_ACCESS;
299 p_acl = &reiserfs_i->i_acl_access;
300 if (acl) {
301 mode_t mode = inode->i_mode;
302 error = posix_acl_equiv_mode(acl, &mode);
303 if (error < 0)
304 return error;
305 else {
306 inode->i_mode = mode;
307 if (error == 0)
308 acl = NULL;
309 }
310 }
311 break;
312 case ACL_TYPE_DEFAULT:
313 name = POSIX_ACL_XATTR_DEFAULT;
314 p_acl = &reiserfs_i->i_acl_default;
315 if (!S_ISDIR(inode->i_mode))
316 return acl ? -EACCES : 0;
317 break;
318 default:
319 return -EINVAL;
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700322 if (acl) {
323 value = posix_acl_to_disk(acl, &size);
324 if (IS_ERR(value))
325 return (int)PTR_ERR(value);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400326 }
327
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400328 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400329
330 /*
331 * Ensure that the inode gets dirtied if we're only using
332 * the mode bits and an old ACL didn't exist. We don't need
333 * to check if the inode is hashed here since we won't get
334 * called by reiserfs_inherit_default_acl().
335 */
336 if (error == -ENODATA) {
337 error = 0;
338 if (type == ACL_TYPE_ACCESS) {
339 inode->i_ctime = CURRENT_TIME_SEC;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700340 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700341 }
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
James Lamanna833d3042005-10-30 15:00:16 -0800344 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400346 if (!error)
347 iset_acl(inode, p_acl, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 return error;
350}
351
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800352/* dir->i_mutex: locked,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * inode is new and not released into the wild yet */
354int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400355reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
356 struct inode *dir, struct dentry *dentry,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700357 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700359 struct posix_acl *acl;
360 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700362 /* ACLs only get applied to files and directories */
363 if (S_ISLNK(inode->i_mode))
364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700366 /* ACLs can only be used on "new" objects, so if it's an old object
367 * there is nothing to inherit from */
368 if (get_inode_sd_version(dir) == STAT_DATA_V1)
369 goto apply_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700371 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
372 * would be useless since permissions are ignored, and a pain because
373 * it introduces locking cycles */
Jeff Mahoney6dfede692009-03-30 14:02:32 -0400374 if (IS_PRIVATE(dir)) {
375 inode->i_flags |= S_PRIVATE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700376 goto apply_umask;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700379 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
Al Viro7a77b152009-06-08 21:01:13 -0400380 if (IS_ERR(acl))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700381 return PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700383 if (acl) {
384 struct posix_acl *acl_copy;
385 mode_t mode = inode->i_mode;
386 int need_acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 /* Copy the default ACL to the default ACL of a new directory */
389 if (S_ISDIR(inode->i_mode)) {
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400390 err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
391 acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700392 if (err)
393 goto cleanup;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700396 /* Now we reconcile the new ACL and the mode,
397 potentially modifying both */
398 acl_copy = posix_acl_clone(acl, GFP_NOFS);
399 if (!acl_copy) {
400 err = -ENOMEM;
401 goto cleanup;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700404 need_acl = posix_acl_create_masq(acl_copy, &mode);
405 if (need_acl >= 0) {
406 if (mode != inode->i_mode) {
407 inode->i_mode = mode;
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700410 /* If we need an ACL.. */
411 if (need_acl > 0) {
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400412 err = reiserfs_set_acl(th, inode,
413 ACL_TYPE_ACCESS,
414 acl_copy);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700415 if (err)
416 goto cleanup_copy;
417 }
418 }
419 cleanup_copy:
420 posix_acl_release(acl_copy);
421 cleanup:
422 posix_acl_release(acl);
423 } else {
424 apply_umask:
425 /* no ACL, apply umask */
Al Viroce3b0f82009-03-29 19:08:22 -0400426 inode->i_mode &= ~current_umask();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400432/* This is used to cache the default acl before a new object is created.
433 * The biggest reason for this is to get an idea of how many blocks will
434 * actually be required for the create operation if we must inherit an ACL.
435 * An ACL write can add up to 3 object creations and an additional file write
436 * so we'd prefer not to reserve that many blocks in the journal if we can.
437 * It also has the advantage of not loading the ACL with a transaction open,
438 * this may seem silly, but if the owner of the directory is doing the
439 * creation, the ACL may not be loaded since the permissions wouldn't require
440 * it.
441 * We return the number of blocks required for the transaction.
442 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700443int reiserfs_cache_default_acl(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400445 struct posix_acl *acl;
446 int nblocks = 0;
447
448 if (IS_PRIVATE(inode))
449 return 0;
450
451 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
452
453 if (acl && !IS_ERR(acl)) {
454 int size = reiserfs_acl_size(acl->a_count);
455
456 /* Other xattrs can be created during inode creation. We don't
457 * want to claim too many blocks, so we check to see if we
458 * we need to create the tree to the xattrs, and then we
459 * just want two files. */
460 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
461 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
462
463 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
464
465 /* We need to account for writes + bitmaps for two files */
466 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
467 posix_acl_release(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400470 return nblocks;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700471}
472
473int reiserfs_acl_chmod(struct inode *inode)
474{
475 struct posix_acl *acl, *clone;
476 int error;
477
478 if (S_ISLNK(inode->i_mode))
479 return -EOPNOTSUPP;
480
481 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
482 !reiserfs_posixacl(inode->i_sb)) {
483 return 0;
484 }
485
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700486 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700487 if (!acl)
488 return 0;
489 if (IS_ERR(acl))
490 return PTR_ERR(acl);
491 clone = posix_acl_clone(acl, GFP_NOFS);
492 posix_acl_release(acl);
493 if (!clone)
494 return -ENOMEM;
495 error = posix_acl_chmod_masq(clone, inode->i_mode);
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400496 if (!error) {
497 struct reiserfs_transaction_handle th;
498 size_t size = reiserfs_xattr_nblocks(inode,
499 reiserfs_acl_size(clone->a_count));
500 reiserfs_write_lock(inode->i_sb);
501 error = journal_begin(&th, inode->i_sb, size * 2);
502 if (!error) {
503 int error2;
504 error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS,
505 clone);
506 error2 = journal_end(&th, inode->i_sb, size * 2);
507 if (error2)
508 error = error2;
509 }
510 reiserfs_write_unlock(inode->i_sb);
511 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700512 posix_acl_release(clone);
513 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516static int
517posix_acl_access_get(struct inode *inode, const char *name,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700518 void *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700520 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return -EINVAL;
522 return xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
523}
524
525static int
526posix_acl_access_set(struct inode *inode, const char *name,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700527 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700529 if (strlen(name) != sizeof(POSIX_ACL_XATTR_ACCESS) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return -EINVAL;
531 return xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
532}
533
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400534static size_t posix_acl_access_list(struct inode *inode, char *list,
535 size_t list_size, const char *name,
536 size_t name_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400538 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700539 if (!reiserfs_posixacl(inode->i_sb))
540 return 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400541 if (list && size <= list_size)
542 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
543 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400546struct xattr_handler reiserfs_posix_acl_access_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700547 .prefix = POSIX_ACL_XATTR_ACCESS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .get = posix_acl_access_get,
549 .set = posix_acl_access_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 .list = posix_acl_access_list,
551};
552
553static int
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700554posix_acl_default_get(struct inode *inode, const char *name,
555 void *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700557 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return -EINVAL;
559 return xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
560}
561
562static int
563posix_acl_default_set(struct inode *inode, const char *name,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700564 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700566 if (strlen(name) != sizeof(POSIX_ACL_XATTR_DEFAULT) - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return -EINVAL;
568 return xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
569}
570
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400571static size_t posix_acl_default_list(struct inode *inode, char *list,
572 size_t list_size, const char *name,
573 size_t name_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400575 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700576 if (!reiserfs_posixacl(inode->i_sb))
577 return 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400578 if (list && size <= list_size)
579 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
580 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400583struct xattr_handler reiserfs_posix_acl_default_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700584 .prefix = POSIX_ACL_XATTR_DEFAULT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 .get = posix_acl_default_get,
586 .set = posix_acl_default_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 .list = posix_acl_default_list,
588};