blob: d7c01ef64edab42ed59d022a2cf36f917e1935c0 [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>
Al Virof466c6f2012-03-17 01:16:43 -04004#include "reiserfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/errno.h>
6#include <linux/pagemap.h>
7#include <linux/xattr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Christoph Hellwig9a59f452005-06-23 00:10:19 -07009#include <linux/posix_acl_xattr.h>
Al Viroc45ac882012-03-17 00:59:06 -040010#include "xattr.h"
Al Viroa3063ab2012-03-17 01:03:10 -040011#include "acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/uaccess.h>
13
Jeff Mahoney0ab26212009-03-30 14:02:39 -040014static int reiserfs_set_acl(struct reiserfs_transaction_handle *th,
15 struct inode *inode, int type,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070016 struct posix_acl *acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18static int
Christoph Hellwig431547b2009-11-13 09:52:56 +000019posix_acl_set(struct dentry *dentry, const char *name, const void *value,
20 size_t size, int flags, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Christoph Hellwig431547b2009-11-13 09:52:56 +000022 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 struct posix_acl *acl;
Jeff Mahoney0ab26212009-03-30 14:02:39 -040024 int error, error2;
25 struct reiserfs_transaction_handle th;
26 size_t jcreate_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 if (!reiserfs_posixacl(inode->i_sb))
28 return -EOPNOTSUPP;
Serge E. Hallyn2e149672011-03-23 16:43:26 -070029 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return -EPERM;
31
32 if (value) {
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -070033 acl = posix_acl_from_xattr(&init_user_ns, value, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 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 Mahoney0ab26212009-03-30 14:02:39 -040044 /* 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 Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060 release_and_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 posix_acl_release(acl);
62 return error;
63}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int
Christoph Hellwig431547b2009-11-13 09:52:56 +000066posix_acl_get(struct dentry *dentry, const char *name, void *buffer,
67 size_t size, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct posix_acl *acl;
70 int error;
71
Christoph Hellwig431547b2009-11-13 09:52:56 +000072 if (!reiserfs_posixacl(dentry->d_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return -EOPNOTSUPP;
74
Christoph Hellwig431547b2009-11-13 09:52:56 +000075 acl = reiserfs_get_acl(dentry->d_inode, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (IS_ERR(acl))
77 return PTR_ERR(acl);
78 if (acl == NULL)
79 return -ENODATA;
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -070080 error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 posix_acl_release(acl);
82
83 return error;
84}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
87 * Convert from filesystem to in-memory representation.
88 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070089static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
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 Torvaldsbd4c6252005-07-12 20:21:28 -070098 return ERR_PTR(-EINVAL);
99 if (((reiserfs_acl_header *) value)->a_version !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 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 Torvaldsbd4c6252005-07-12 20:21:28 -0700111 for (n = 0; n < count; n++) {
112 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
114 goto fail;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700115 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700117 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);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700124 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700126 case ACL_USER:
Eric W. Biedermandf814652012-02-07 16:30:06 -0800127 value = (char *)value + sizeof(reiserfs_acl_entry);
128 if ((char *)value > end)
129 goto fail;
130 acl->a_entries[n].e_uid =
131 make_kuid(&init_user_ns,
132 le32_to_cpu(entry->e_id));
133 break;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700134 case ACL_GROUP:
135 value = (char *)value + sizeof(reiserfs_acl_entry);
136 if ((char *)value > end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 goto fail;
Eric W. Biedermandf814652012-02-07 16:30:06 -0800138 acl->a_entries[n].e_gid =
139 make_kgid(&init_user_ns,
140 le32_to_cpu(entry->e_id));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700141 break;
142
143 default:
144 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 }
147 if (value != end)
148 goto fail;
149 return acl;
150
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700151 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 posix_acl_release(acl);
153 return ERR_PTR(-EINVAL);
154}
155
156/*
157 * Convert from in-memory to filesystem representation.
158 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 reiserfs_acl_header *ext_acl;
162 char *e;
163 int n;
164
165 *size = reiserfs_acl_size(acl->a_count);
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800166 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700167 acl->a_count *
168 sizeof(reiserfs_acl_entry),
169 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (!ext_acl)
171 return ERR_PTR(-ENOMEM);
172 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
173 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700174 for (n = 0; n < acl->a_count; n++) {
Eric W. Biedermandf814652012-02-07 16:30:06 -0800175 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
177 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700179 switch (acl->a_entries[n].e_tag) {
180 case ACL_USER:
Eric W. Biedermandf814652012-02-07 16:30:06 -0800181 entry->e_id = cpu_to_le32(
182 from_kuid(&init_user_ns, acl_e->e_uid));
183 e += sizeof(reiserfs_acl_entry);
184 break;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700185 case ACL_GROUP:
Eric W. Biedermandf814652012-02-07 16:30:06 -0800186 entry->e_id = cpu_to_le32(
187 from_kgid(&init_user_ns, acl_e->e_gid));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700188 e += sizeof(reiserfs_acl_entry);
189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700191 case ACL_USER_OBJ:
192 case ACL_GROUP_OBJ:
193 case ACL_MASK:
194 case ACL_OTHER:
195 e += sizeof(reiserfs_acl_entry_short);
196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700198 default:
199 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 }
202 return (char *)ext_acl;
203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 kfree(ext_acl);
206 return ERR_PTR(-EINVAL);
207}
208
209/*
210 * Inode operation get_posix_acl().
211 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800212 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * BKL held [before 2.5.x]
214 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700215struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 char *name, *value;
Al Viro073aaa12009-06-09 12:11:54 -0400218 struct posix_acl *acl;
Adrian Bunk3cdc4092006-03-25 03:07:50 -0800219 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Al Viro073aaa12009-06-09 12:11:54 -0400222 acl = get_cached_acl(inode, type);
223 if (acl != ACL_NOT_CACHED)
224 return acl;
225
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700226 switch (type) {
227 case ACL_TYPE_ACCESS:
228 name = POSIX_ACL_XATTR_ACCESS;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700229 break;
230 case ACL_TYPE_DEFAULT:
231 name = POSIX_ACL_XATTR_DEFAULT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700232 break;
233 default:
Al Viro073aaa12009-06-09 12:11:54 -0400234 BUG();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700237 size = reiserfs_xattr_get(inode, name, NULL, 0);
Adrian Bunk3cdc4092006-03-25 03:07:50 -0800238 if (size < 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700239 if (size == -ENODATA || size == -ENOSYS) {
Al Viro073aaa12009-06-09 12:11:54 -0400240 set_cached_acl(inode, type, NULL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241 return NULL;
242 }
243 return ERR_PTR(size);
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700246 value = kmalloc(size, GFP_NOFS);
247 if (!value)
248 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 retval = reiserfs_xattr_get(inode, name, value, size);
251 if (retval == -ENODATA || retval == -ENOSYS) {
252 /* This shouldn't actually happen as it should have
253 been caught above.. but just in case */
254 acl = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700255 } else if (retval < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 acl = ERR_PTR(retval);
257 } else {
258 acl = posix_acl_from_disk(value, retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700259 }
Al Viro073aaa12009-06-09 12:11:54 -0400260 if (!IS_ERR(acl))
261 set_cached_acl(inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 kfree(value);
264 return acl;
265}
266
267/*
268 * Inode operation set_posix_acl().
269 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800270 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * BKL held [before 2.5.x]
272 */
273static int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400274reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
275 int type, struct posix_acl *acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700277 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 void *value = NULL;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400279 size_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (S_ISLNK(inode->i_mode))
283 return -EOPNOTSUPP;
284
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700285 switch (type) {
286 case ACL_TYPE_ACCESS:
287 name = POSIX_ACL_XATTR_ACCESS;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700288 if (acl) {
Al Virod6952122011-07-23 18:56:36 -0400289 error = posix_acl_equiv_mode(acl, &inode->i_mode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700290 if (error < 0)
291 return error;
292 else {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700293 if (error == 0)
294 acl = NULL;
295 }
296 }
297 break;
298 case ACL_TYPE_DEFAULT:
299 name = POSIX_ACL_XATTR_DEFAULT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700300 if (!S_ISDIR(inode->i_mode))
301 return acl ? -EACCES : 0;
302 break;
303 default:
304 return -EINVAL;
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700307 if (acl) {
308 value = posix_acl_to_disk(acl, &size);
309 if (IS_ERR(value))
310 return (int)PTR_ERR(value);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400311 }
312
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400313 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400314
315 /*
316 * Ensure that the inode gets dirtied if we're only using
317 * the mode bits and an old ACL didn't exist. We don't need
318 * to check if the inode is hashed here since we won't get
319 * called by reiserfs_inherit_default_acl().
320 */
321 if (error == -ENODATA) {
322 error = 0;
323 if (type == ACL_TYPE_ACCESS) {
324 inode->i_ctime = CURRENT_TIME_SEC;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700325 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700326 }
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
James Lamanna833d3042005-10-30 15:00:16 -0800329 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400331 if (!error)
Al Viro073aaa12009-06-09 12:11:54 -0400332 set_cached_acl(inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 return error;
335}
336
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800337/* dir->i_mutex: locked,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * inode is new and not released into the wild yet */
339int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400340reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
341 struct inode *dir, struct dentry *dentry,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700342 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700344 struct posix_acl *acl;
345 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700347 /* ACLs only get applied to files and directories */
348 if (S_ISLNK(inode->i_mode))
349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700351 /* ACLs can only be used on "new" objects, so if it's an old object
352 * there is nothing to inherit from */
353 if (get_inode_sd_version(dir) == STAT_DATA_V1)
354 goto apply_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700356 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
357 * would be useless since permissions are ignored, and a pain because
358 * it introduces locking cycles */
Jeff Mahoney6dfede692009-03-30 14:02:32 -0400359 if (IS_PRIVATE(dir)) {
360 inode->i_flags |= S_PRIVATE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700361 goto apply_umask;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700364 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
Al Viro7a77b152009-06-08 21:01:13 -0400365 if (IS_ERR(acl))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700366 return PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700368 if (acl) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700369 /* Copy the default ACL to the default ACL of a new directory */
370 if (S_ISDIR(inode->i_mode)) {
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400371 err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
372 acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700373 if (err)
374 goto cleanup;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700377 /* Now we reconcile the new ACL and the mode,
378 potentially modifying both */
Al Virod3fb6122011-07-23 18:37:50 -0400379 err = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
Al Viro826cae22011-07-23 03:10:32 -0400380 if (err < 0)
381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Al Viro826cae22011-07-23 03:10:32 -0400383 /* If we need an ACL.. */
384 if (err > 0)
385 err = reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700386 cleanup:
387 posix_acl_release(acl);
388 } else {
389 apply_umask:
390 /* no ACL, apply umask */
Al Viroce3b0f82009-03-29 19:08:22 -0400391 inode->i_mode &= ~current_umask();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400397/* This is used to cache the default acl before a new object is created.
398 * The biggest reason for this is to get an idea of how many blocks will
399 * actually be required for the create operation if we must inherit an ACL.
400 * An ACL write can add up to 3 object creations and an additional file write
401 * so we'd prefer not to reserve that many blocks in the journal if we can.
402 * It also has the advantage of not loading the ACL with a transaction open,
403 * this may seem silly, but if the owner of the directory is doing the
404 * creation, the ACL may not be loaded since the permissions wouldn't require
405 * it.
406 * We return the number of blocks required for the transaction.
407 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700408int reiserfs_cache_default_acl(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400410 struct posix_acl *acl;
411 int nblocks = 0;
412
413 if (IS_PRIVATE(inode))
414 return 0;
415
416 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
417
418 if (acl && !IS_ERR(acl)) {
419 int size = reiserfs_acl_size(acl->a_count);
420
421 /* Other xattrs can be created during inode creation. We don't
422 * want to claim too many blocks, so we check to see if we
423 * we need to create the tree to the xattrs, and then we
424 * just want two files. */
425 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
426 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
427
428 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
429
430 /* We need to account for writes + bitmaps for two files */
431 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
432 posix_acl_release(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400435 return nblocks;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700436}
437
438int reiserfs_acl_chmod(struct inode *inode)
439{
Al Virobc26ab52011-07-23 00:18:02 -0400440 struct reiserfs_transaction_handle th;
441 struct posix_acl *acl;
442 size_t size;
443 int depth;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700444 int error;
445
446 if (S_ISLNK(inode->i_mode))
447 return -EOPNOTSUPP;
448
449 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
450 !reiserfs_posixacl(inode->i_sb)) {
451 return 0;
452 }
453
Frederic Weisbecker6c287052010-01-07 12:57:47 +0100454 reiserfs_write_unlock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700455 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
Frederic Weisbecker6c287052010-01-07 12:57:47 +0100456 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700457 if (!acl)
458 return 0;
459 if (IS_ERR(acl))
460 return PTR_ERR(acl);
Al Virobc26ab52011-07-23 00:18:02 -0400461 error = posix_acl_chmod(&acl, GFP_NOFS, inode->i_mode);
462 if (error)
463 return error;
Frederic Weisbecker238af872010-12-02 14:31:16 -0800464
Al Virobc26ab52011-07-23 00:18:02 -0400465 size = reiserfs_xattr_nblocks(inode, reiserfs_acl_size(acl->a_count));
466 depth = reiserfs_write_lock_once(inode->i_sb);
467 error = journal_begin(&th, inode->i_sb, size * 2);
468 if (!error) {
469 int error2;
470 error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, acl);
471 error2 = journal_end(&th, inode->i_sb, size * 2);
472 if (error2)
473 error = error2;
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400474 }
Al Virobc26ab52011-07-23 00:18:02 -0400475 reiserfs_write_unlock_once(inode->i_sb, depth);
476 posix_acl_release(acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700477 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Christoph Hellwig431547b2009-11-13 09:52:56 +0000480static size_t posix_acl_access_list(struct dentry *dentry, char *list,
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400481 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000482 size_t name_len, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400484 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000485 if (!reiserfs_posixacl(dentry->d_sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700486 return 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400487 if (list && size <= list_size)
488 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
489 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Stephen Hemminger94d09a92010-05-13 17:53:19 -0700492const struct xattr_handler reiserfs_posix_acl_access_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700493 .prefix = POSIX_ACL_XATTR_ACCESS,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000494 .flags = ACL_TYPE_ACCESS,
495 .get = posix_acl_get,
496 .set = posix_acl_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 .list = posix_acl_access_list,
498};
499
Christoph Hellwig431547b2009-11-13 09:52:56 +0000500static size_t posix_acl_default_list(struct dentry *dentry, char *list,
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400501 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000502 size_t name_len, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400504 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000505 if (!reiserfs_posixacl(dentry->d_sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700506 return 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400507 if (list && size <= list_size)
508 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
509 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Stephen Hemminger94d09a92010-05-13 17:53:19 -0700512const struct xattr_handler reiserfs_posix_acl_default_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700513 .prefix = POSIX_ACL_XATTR_DEFAULT,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000514 .flags = ACL_TYPE_DEFAULT,
515 .get = posix_acl_get,
516 .set = posix_acl_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 .list = posix_acl_default_list,
518};