blob: 6f721ea9403919ea6ed22b628ff210d1b0d77238 [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 Hellwig9dad9432013-12-20 05:16:36 -080019reiserfs_posix_acl_set(struct dentry *dentry, const char *name, const void *value,
Christoph Hellwig431547b2009-11-13 09:52:56 +000020 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);
Jeff Mahoney4c051412013-08-08 17:27:19 -040052 reiserfs_write_unlock(inode->i_sb);
Jeff Mahoney0ab26212009-03-30 14:02:39 -040053 if (error == 0) {
54 error = reiserfs_set_acl(&th, inode, type, acl);
Jeff Mahoney4c051412013-08-08 17:27:19 -040055 reiserfs_write_lock(inode->i_sb);
Jeff Mahoney0ab26212009-03-30 14:02:39 -040056 error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
Jeff Mahoney4c051412013-08-08 17:27:19 -040057 reiserfs_write_unlock(inode->i_sb);
Jeff Mahoney0ab26212009-03-30 14:02:39 -040058 if (error2)
59 error = error2;
60 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070062 release_and_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 posix_acl_release(acl);
64 return error;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int
Christoph Hellwig9dad9432013-12-20 05:16:36 -080068reiserfs_posix_acl_get(struct dentry *dentry, const char *name, void *buffer,
Christoph Hellwig431547b2009-11-13 09:52:56 +000069 size_t size, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 struct posix_acl *acl;
72 int error;
73
Christoph Hellwig431547b2009-11-13 09:52:56 +000074 if (!reiserfs_posixacl(dentry->d_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EOPNOTSUPP;
76
Christoph Hellwig431547b2009-11-13 09:52:56 +000077 acl = reiserfs_get_acl(dentry->d_inode, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (IS_ERR(acl))
79 return PTR_ERR(acl);
80 if (acl == NULL)
81 return -ENODATA;
Eric W. Biederman5f3a4a22012-09-10 20:17:44 -070082 error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 posix_acl_release(acl);
84
85 return error;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * Convert from filesystem to in-memory representation.
90 */
Christoph Hellwig9dad9432013-12-20 05:16:36 -080091static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 const char *end = (char *)value + size;
94 int n, count;
95 struct posix_acl *acl;
96
97 if (!value)
98 return NULL;
99 if (size < sizeof(reiserfs_acl_header))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700100 return ERR_PTR(-EINVAL);
101 if (((reiserfs_acl_header *) value)->a_version !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 cpu_to_le32(REISERFS_ACL_VERSION))
103 return ERR_PTR(-EINVAL);
104 value = (char *)value + sizeof(reiserfs_acl_header);
105 count = reiserfs_acl_count(size);
106 if (count < 0)
107 return ERR_PTR(-EINVAL);
108 if (count == 0)
109 return NULL;
110 acl = posix_acl_alloc(count, GFP_NOFS);
111 if (!acl)
112 return ERR_PTR(-ENOMEM);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700113 for (n = 0; n < count; n++) {
114 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
116 goto fail;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700117 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700119 switch (acl->a_entries[n].e_tag) {
120 case ACL_USER_OBJ:
121 case ACL_GROUP_OBJ:
122 case ACL_MASK:
123 case ACL_OTHER:
124 value = (char *)value +
125 sizeof(reiserfs_acl_entry_short);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700126 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700128 case ACL_USER:
Eric W. Biedermandf814652012-02-07 16:30:06 -0800129 value = (char *)value + sizeof(reiserfs_acl_entry);
130 if ((char *)value > end)
131 goto fail;
132 acl->a_entries[n].e_uid =
133 make_kuid(&init_user_ns,
134 le32_to_cpu(entry->e_id));
135 break;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700136 case ACL_GROUP:
137 value = (char *)value + sizeof(reiserfs_acl_entry);
138 if ((char *)value > end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 goto fail;
Eric W. Biedermandf814652012-02-07 16:30:06 -0800140 acl->a_entries[n].e_gid =
141 make_kgid(&init_user_ns,
142 le32_to_cpu(entry->e_id));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143 break;
144
145 default:
146 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148 }
149 if (value != end)
150 goto fail;
151 return acl;
152
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700153 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 posix_acl_release(acl);
155 return ERR_PTR(-EINVAL);
156}
157
158/*
159 * Convert from in-memory to filesystem representation.
160 */
Christoph Hellwig9dad9432013-12-20 05:16:36 -0800161static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 reiserfs_acl_header *ext_acl;
164 char *e;
165 int n;
166
167 *size = reiserfs_acl_size(acl->a_count);
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800168 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700169 acl->a_count *
170 sizeof(reiserfs_acl_entry),
171 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (!ext_acl)
173 return ERR_PTR(-ENOMEM);
174 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
175 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176 for (n = 0; n < acl->a_count; n++) {
Eric W. Biedermandf814652012-02-07 16:30:06 -0800177 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700178 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
179 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700181 switch (acl->a_entries[n].e_tag) {
182 case ACL_USER:
Eric W. Biedermandf814652012-02-07 16:30:06 -0800183 entry->e_id = cpu_to_le32(
184 from_kuid(&init_user_ns, acl_e->e_uid));
185 e += sizeof(reiserfs_acl_entry);
186 break;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700187 case ACL_GROUP:
Eric W. Biedermandf814652012-02-07 16:30:06 -0800188 entry->e_id = cpu_to_le32(
189 from_kgid(&init_user_ns, acl_e->e_gid));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700190 e += sizeof(reiserfs_acl_entry);
191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700193 case ACL_USER_OBJ:
194 case ACL_GROUP_OBJ:
195 case ACL_MASK:
196 case ACL_OTHER:
197 e += sizeof(reiserfs_acl_entry_short);
198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700200 default:
201 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 }
204 return (char *)ext_acl;
205
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700206 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 kfree(ext_acl);
208 return ERR_PTR(-EINVAL);
209}
210
211/*
212 * Inode operation get_posix_acl().
213 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800214 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * BKL held [before 2.5.x]
216 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700217struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 char *name, *value;
Al Viro073aaa12009-06-09 12:11:54 -0400220 struct posix_acl *acl;
Adrian Bunk3cdc4092006-03-25 03:07:50 -0800221 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Al Viro073aaa12009-06-09 12:11:54 -0400224 acl = get_cached_acl(inode, type);
225 if (acl != ACL_NOT_CACHED)
226 return acl;
227
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700228 switch (type) {
229 case ACL_TYPE_ACCESS:
230 name = POSIX_ACL_XATTR_ACCESS;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700231 break;
232 case ACL_TYPE_DEFAULT:
233 name = POSIX_ACL_XATTR_DEFAULT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700234 break;
235 default:
Al Viro073aaa12009-06-09 12:11:54 -0400236 BUG();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700239 size = reiserfs_xattr_get(inode, name, NULL, 0);
Adrian Bunk3cdc4092006-03-25 03:07:50 -0800240 if (size < 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241 if (size == -ENODATA || size == -ENOSYS) {
Al Viro073aaa12009-06-09 12:11:54 -0400242 set_cached_acl(inode, type, NULL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700243 return NULL;
244 }
245 return ERR_PTR(size);
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700248 value = kmalloc(size, GFP_NOFS);
249 if (!value)
250 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 retval = reiserfs_xattr_get(inode, name, value, size);
253 if (retval == -ENODATA || retval == -ENOSYS) {
254 /* This shouldn't actually happen as it should have
255 been caught above.. but just in case */
256 acl = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700257 } else if (retval < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 acl = ERR_PTR(retval);
259 } else {
Christoph Hellwig9dad9432013-12-20 05:16:36 -0800260 acl = reiserfs_posix_acl_from_disk(value, retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700261 }
Al Viro073aaa12009-06-09 12:11:54 -0400262 if (!IS_ERR(acl))
263 set_cached_acl(inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 kfree(value);
266 return acl;
267}
268
269/*
270 * Inode operation set_posix_acl().
271 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800272 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * BKL held [before 2.5.x]
274 */
275static int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400276reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
277 int type, struct posix_acl *acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700279 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 void *value = NULL;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400281 size_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (S_ISLNK(inode->i_mode))
285 return -EOPNOTSUPP;
286
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700287 switch (type) {
288 case ACL_TYPE_ACCESS:
289 name = POSIX_ACL_XATTR_ACCESS;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700290 if (acl) {
Al Virod6952122011-07-23 18:56:36 -0400291 error = posix_acl_equiv_mode(acl, &inode->i_mode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700292 if (error < 0)
293 return error;
294 else {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700295 if (error == 0)
296 acl = NULL;
297 }
298 }
299 break;
300 case ACL_TYPE_DEFAULT:
301 name = POSIX_ACL_XATTR_DEFAULT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700302 if (!S_ISDIR(inode->i_mode))
303 return acl ? -EACCES : 0;
304 break;
305 default:
306 return -EINVAL;
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700309 if (acl) {
Christoph Hellwig9dad9432013-12-20 05:16:36 -0800310 value = reiserfs_posix_acl_to_disk(acl, &size);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700311 if (IS_ERR(value))
312 return (int)PTR_ERR(value);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400313 }
314
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400315 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400316
317 /*
318 * Ensure that the inode gets dirtied if we're only using
319 * the mode bits and an old ACL didn't exist. We don't need
320 * to check if the inode is hashed here since we won't get
321 * called by reiserfs_inherit_default_acl().
322 */
323 if (error == -ENODATA) {
324 error = 0;
325 if (type == ACL_TYPE_ACCESS) {
326 inode->i_ctime = CURRENT_TIME_SEC;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700327 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700328 }
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
James Lamanna833d3042005-10-30 15:00:16 -0800331 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400333 if (!error)
Al Viro073aaa12009-06-09 12:11:54 -0400334 set_cached_acl(inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 return error;
337}
338
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800339/* dir->i_mutex: locked,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * inode is new and not released into the wild yet */
341int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400342reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
343 struct inode *dir, struct dentry *dentry,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700344 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700346 struct posix_acl *acl;
347 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700349 /* ACLs only get applied to files and directories */
350 if (S_ISLNK(inode->i_mode))
351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700353 /* ACLs can only be used on "new" objects, so if it's an old object
354 * there is nothing to inherit from */
355 if (get_inode_sd_version(dir) == STAT_DATA_V1)
356 goto apply_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700358 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
359 * would be useless since permissions are ignored, and a pain because
360 * it introduces locking cycles */
Jeff Mahoney6dfede692009-03-30 14:02:32 -0400361 if (IS_PRIVATE(dir)) {
362 inode->i_flags |= S_PRIVATE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700363 goto apply_umask;
364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700366 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
Al Viro7a77b152009-06-08 21:01:13 -0400367 if (IS_ERR(acl))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700368 return PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700370 if (acl) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700371 /* Copy the default ACL to the default ACL of a new directory */
372 if (S_ISDIR(inode->i_mode)) {
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400373 err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
374 acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700375 if (err)
376 goto cleanup;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700379 /* Now we reconcile the new ACL and the mode,
380 potentially modifying both */
Al Virod3fb6122011-07-23 18:37:50 -0400381 err = posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
Al Viro826cae22011-07-23 03:10:32 -0400382 if (err < 0)
383 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Al Viro826cae22011-07-23 03:10:32 -0400385 /* If we need an ACL.. */
386 if (err > 0)
387 err = reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 cleanup:
389 posix_acl_release(acl);
390 } else {
391 apply_umask:
392 /* no ACL, apply umask */
Al Viroce3b0f82009-03-29 19:08:22 -0400393 inode->i_mode &= ~current_umask();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400399/* This is used to cache the default acl before a new object is created.
400 * The biggest reason for this is to get an idea of how many blocks will
401 * actually be required for the create operation if we must inherit an ACL.
402 * An ACL write can add up to 3 object creations and an additional file write
403 * so we'd prefer not to reserve that many blocks in the journal if we can.
404 * It also has the advantage of not loading the ACL with a transaction open,
405 * this may seem silly, but if the owner of the directory is doing the
406 * creation, the ACL may not be loaded since the permissions wouldn't require
407 * it.
408 * We return the number of blocks required for the transaction.
409 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700410int reiserfs_cache_default_acl(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400412 struct posix_acl *acl;
413 int nblocks = 0;
414
415 if (IS_PRIVATE(inode))
416 return 0;
417
418 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
419
420 if (acl && !IS_ERR(acl)) {
421 int size = reiserfs_acl_size(acl->a_count);
422
423 /* Other xattrs can be created during inode creation. We don't
424 * want to claim too many blocks, so we check to see if we
425 * we need to create the tree to the xattrs, and then we
426 * just want two files. */
427 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
428 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
429
430 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
431
432 /* We need to account for writes + bitmaps for two files */
433 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
434 posix_acl_release(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400437 return nblocks;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700438}
439
Jeff Mahoney4c051412013-08-08 17:27:19 -0400440/*
441 * Called under i_mutex
442 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700443int reiserfs_acl_chmod(struct inode *inode)
444{
Al Virobc26ab52011-07-23 00:18:02 -0400445 struct reiserfs_transaction_handle th;
446 struct posix_acl *acl;
447 size_t size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700448 int error;
449
Jeff Mahoney4a857012013-05-31 15:54:17 -0400450 if (IS_PRIVATE(inode))
451 return 0;
452
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700453 if (S_ISLNK(inode->i_mode))
454 return -EOPNOTSUPP;
455
456 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
457 !reiserfs_posixacl(inode->i_sb)) {
458 return 0;
459 }
460
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700461 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700462 if (!acl)
463 return 0;
464 if (IS_ERR(acl))
465 return PTR_ERR(acl);
Al Virobc26ab52011-07-23 00:18:02 -0400466 error = posix_acl_chmod(&acl, GFP_NOFS, inode->i_mode);
467 if (error)
468 return error;
Frederic Weisbecker238af872010-12-02 14:31:16 -0800469
Al Virobc26ab52011-07-23 00:18:02 -0400470 size = reiserfs_xattr_nblocks(inode, reiserfs_acl_size(acl->a_count));
Jeff Mahoney4c051412013-08-08 17:27:19 -0400471 reiserfs_write_lock(inode->i_sb);
Al Virobc26ab52011-07-23 00:18:02 -0400472 error = journal_begin(&th, inode->i_sb, size * 2);
Jeff Mahoney4c051412013-08-08 17:27:19 -0400473 reiserfs_write_unlock(inode->i_sb);
Al Virobc26ab52011-07-23 00:18:02 -0400474 if (!error) {
475 int error2;
476 error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, acl);
Jeff Mahoney4c051412013-08-08 17:27:19 -0400477 reiserfs_write_lock(inode->i_sb);
Al Virobc26ab52011-07-23 00:18:02 -0400478 error2 = journal_end(&th, inode->i_sb, size * 2);
Jeff Mahoney4c051412013-08-08 17:27:19 -0400479 reiserfs_write_unlock(inode->i_sb);
Al Virobc26ab52011-07-23 00:18:02 -0400480 if (error2)
481 error = error2;
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400482 }
Al Virobc26ab52011-07-23 00:18:02 -0400483 posix_acl_release(acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700484 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Christoph Hellwig431547b2009-11-13 09:52:56 +0000487static size_t posix_acl_access_list(struct dentry *dentry, char *list,
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400488 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000489 size_t name_len, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400491 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000492 if (!reiserfs_posixacl(dentry->d_sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700493 return 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400494 if (list && size <= list_size)
495 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
496 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Stephen Hemminger94d09a92010-05-13 17:53:19 -0700499const struct xattr_handler reiserfs_posix_acl_access_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700500 .prefix = POSIX_ACL_XATTR_ACCESS,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000501 .flags = ACL_TYPE_ACCESS,
Christoph Hellwig9dad9432013-12-20 05:16:36 -0800502 .get = reiserfs_posix_acl_get,
503 .set = reiserfs_posix_acl_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 .list = posix_acl_access_list,
505};
506
Christoph Hellwig431547b2009-11-13 09:52:56 +0000507static size_t posix_acl_default_list(struct dentry *dentry, char *list,
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400508 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000509 size_t name_len, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400511 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000512 if (!reiserfs_posixacl(dentry->d_sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700513 return 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400514 if (list && size <= list_size)
515 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
516 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Stephen Hemminger94d09a92010-05-13 17:53:19 -0700519const struct xattr_handler reiserfs_posix_acl_default_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700520 .prefix = POSIX_ACL_XATTR_DEFAULT,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000521 .flags = ACL_TYPE_DEFAULT,
Christoph Hellwig9dad9432013-12-20 05:16:36 -0800522 .get = reiserfs_posix_acl_get,
523 .set = reiserfs_posix_acl_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 .list = posix_acl_default_list,
525};