blob: 3dc38f1206fc06a6757521a399263edca7d1f844 [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>
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/reiserfs_xattr.h>
11#include <linux/reiserfs_acl.h>
12#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) {
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
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;
80 error = posix_acl_to_xattr(acl, buffer, size);
81 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);
124 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700127 case ACL_USER:
128 case ACL_GROUP:
129 value = (char *)value + sizeof(reiserfs_acl_entry);
130 if ((char *)value > end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto fail;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700132 acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
133 break;
134
135 default:
136 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138 }
139 if (value != end)
140 goto fail;
141 return acl;
142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 posix_acl_release(acl);
145 return ERR_PTR(-EINVAL);
146}
147
148/*
149 * Convert from in-memory to filesystem representation.
150 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700151static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 reiserfs_acl_header *ext_acl;
154 char *e;
155 int n;
156
157 *size = reiserfs_acl_size(acl->a_count);
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800158 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159 acl->a_count *
160 sizeof(reiserfs_acl_entry),
161 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 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);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700166 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700170 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700177 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700184 default:
185 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187 }
188 return (char *)ext_acl;
189
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700190 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 kfree(ext_acl);
192 return ERR_PTR(-EINVAL);
193}
194
195/*
196 * Inode operation get_posix_acl().
197 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800198 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * BKL held [before 2.5.x]
200 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700201struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 char *name, *value;
Al Viro073aaa12009-06-09 12:11:54 -0400204 struct posix_acl *acl;
Adrian Bunk3cdc4092006-03-25 03:07:50 -0800205 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Al Viro073aaa12009-06-09 12:11:54 -0400208 acl = get_cached_acl(inode, type);
209 if (acl != ACL_NOT_CACHED)
210 return acl;
211
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700212 switch (type) {
213 case ACL_TYPE_ACCESS:
214 name = POSIX_ACL_XATTR_ACCESS;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700215 break;
216 case ACL_TYPE_DEFAULT:
217 name = POSIX_ACL_XATTR_DEFAULT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700218 break;
219 default:
Al Viro073aaa12009-06-09 12:11:54 -0400220 BUG();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700223 size = reiserfs_xattr_get(inode, name, NULL, 0);
Adrian Bunk3cdc4092006-03-25 03:07:50 -0800224 if (size < 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700225 if (size == -ENODATA || size == -ENOSYS) {
Al Viro073aaa12009-06-09 12:11:54 -0400226 set_cached_acl(inode, type, NULL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700227 return NULL;
228 }
229 return ERR_PTR(size);
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700232 value = kmalloc(size, GFP_NOFS);
233 if (!value)
234 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
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;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241 } else if (retval < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 acl = ERR_PTR(retval);
243 } else {
244 acl = posix_acl_from_disk(value, retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700245 }
Al Viro073aaa12009-06-09 12:11:54 -0400246 if (!IS_ERR(acl))
247 set_cached_acl(inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 kfree(value);
250 return acl;
251}
252
253/*
254 * Inode operation set_posix_acl().
255 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800256 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * BKL held [before 2.5.x]
258 */
259static int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400260reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
261 int type, struct posix_acl *acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700263 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 void *value = NULL;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400265 size_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (S_ISLNK(inode->i_mode))
269 return -EOPNOTSUPP;
270
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700271 switch (type) {
272 case ACL_TYPE_ACCESS:
273 name = POSIX_ACL_XATTR_ACCESS;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700274 if (acl) {
275 mode_t mode = inode->i_mode;
276 error = posix_acl_equiv_mode(acl, &mode);
277 if (error < 0)
278 return error;
279 else {
280 inode->i_mode = mode;
281 if (error == 0)
282 acl = NULL;
283 }
284 }
285 break;
286 case ACL_TYPE_DEFAULT:
287 name = POSIX_ACL_XATTR_DEFAULT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700288 if (!S_ISDIR(inode->i_mode))
289 return acl ? -EACCES : 0;
290 break;
291 default:
292 return -EINVAL;
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700295 if (acl) {
296 value = posix_acl_to_disk(acl, &size);
297 if (IS_ERR(value))
298 return (int)PTR_ERR(value);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400299 }
300
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400301 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400302
303 /*
304 * Ensure that the inode gets dirtied if we're only using
305 * the mode bits and an old ACL didn't exist. We don't need
306 * to check if the inode is hashed here since we won't get
307 * called by reiserfs_inherit_default_acl().
308 */
309 if (error == -ENODATA) {
310 error = 0;
311 if (type == ACL_TYPE_ACCESS) {
312 inode->i_ctime = CURRENT_TIME_SEC;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700313 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700314 }
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
James Lamanna833d3042005-10-30 15:00:16 -0800317 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400319 if (!error)
Al Viro073aaa12009-06-09 12:11:54 -0400320 set_cached_acl(inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 return error;
323}
324
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800325/* dir->i_mutex: locked,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * inode is new and not released into the wild yet */
327int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400328reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
329 struct inode *dir, struct dentry *dentry,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700330 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700332 struct posix_acl *acl;
333 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700335 /* ACLs only get applied to files and directories */
336 if (S_ISLNK(inode->i_mode))
337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700339 /* ACLs can only be used on "new" objects, so if it's an old object
340 * there is nothing to inherit from */
341 if (get_inode_sd_version(dir) == STAT_DATA_V1)
342 goto apply_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700344 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
345 * would be useless since permissions are ignored, and a pain because
346 * it introduces locking cycles */
Jeff Mahoney6dfede62009-03-30 14:02:32 -0400347 if (IS_PRIVATE(dir)) {
348 inode->i_flags |= S_PRIVATE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700349 goto apply_umask;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700352 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
Al Viro7a77b152009-06-08 21:01:13 -0400353 if (IS_ERR(acl))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700354 return PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700356 if (acl) {
357 struct posix_acl *acl_copy;
358 mode_t mode = inode->i_mode;
359 int need_acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700361 /* Copy the default ACL to the default ACL of a new directory */
362 if (S_ISDIR(inode->i_mode)) {
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400363 err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
364 acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700365 if (err)
366 goto cleanup;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700369 /* Now we reconcile the new ACL and the mode,
370 potentially modifying both */
371 acl_copy = posix_acl_clone(acl, GFP_NOFS);
372 if (!acl_copy) {
373 err = -ENOMEM;
374 goto cleanup;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700377 need_acl = posix_acl_create_masq(acl_copy, &mode);
378 if (need_acl >= 0) {
379 if (mode != inode->i_mode) {
380 inode->i_mode = mode;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700383 /* If we need an ACL.. */
384 if (need_acl > 0) {
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400385 err = reiserfs_set_acl(th, inode,
386 ACL_TYPE_ACCESS,
387 acl_copy);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 if (err)
389 goto cleanup_copy;
390 }
391 }
392 cleanup_copy:
393 posix_acl_release(acl_copy);
394 cleanup:
395 posix_acl_release(acl);
396 } else {
397 apply_umask:
398 /* no ACL, apply umask */
Al Viroce3b0f82009-03-29 19:08:22 -0400399 inode->i_mode &= ~current_umask();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700402 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400405/* This is used to cache the default acl before a new object is created.
406 * The biggest reason for this is to get an idea of how many blocks will
407 * actually be required for the create operation if we must inherit an ACL.
408 * An ACL write can add up to 3 object creations and an additional file write
409 * so we'd prefer not to reserve that many blocks in the journal if we can.
410 * It also has the advantage of not loading the ACL with a transaction open,
411 * this may seem silly, but if the owner of the directory is doing the
412 * creation, the ACL may not be loaded since the permissions wouldn't require
413 * it.
414 * We return the number of blocks required for the transaction.
415 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700416int reiserfs_cache_default_acl(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400418 struct posix_acl *acl;
419 int nblocks = 0;
420
421 if (IS_PRIVATE(inode))
422 return 0;
423
424 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
425
426 if (acl && !IS_ERR(acl)) {
427 int size = reiserfs_acl_size(acl->a_count);
428
429 /* Other xattrs can be created during inode creation. We don't
430 * want to claim too many blocks, so we check to see if we
431 * we need to create the tree to the xattrs, and then we
432 * just want two files. */
433 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
434 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
435
436 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
437
438 /* We need to account for writes + bitmaps for two files */
439 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
440 posix_acl_release(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400443 return nblocks;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700444}
445
446int reiserfs_acl_chmod(struct inode *inode)
447{
448 struct posix_acl *acl, *clone;
449 int error;
450
451 if (S_ISLNK(inode->i_mode))
452 return -EOPNOTSUPP;
453
454 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
455 !reiserfs_posixacl(inode->i_sb)) {
456 return 0;
457 }
458
Frederic Weisbecker6c287052010-01-07 12:57:47 +0100459 reiserfs_write_unlock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700460 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
Frederic Weisbecker6c287052010-01-07 12:57:47 +0100461 reiserfs_write_lock(inode->i_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700462 if (!acl)
463 return 0;
464 if (IS_ERR(acl))
465 return PTR_ERR(acl);
466 clone = posix_acl_clone(acl, GFP_NOFS);
467 posix_acl_release(acl);
468 if (!clone)
469 return -ENOMEM;
470 error = posix_acl_chmod_masq(clone, inode->i_mode);
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400471 if (!error) {
472 struct reiserfs_transaction_handle th;
473 size_t size = reiserfs_xattr_nblocks(inode,
474 reiserfs_acl_size(clone->a_count));
Frederic Weisbecker238af872010-12-02 14:31:16 -0800475 int depth;
476
477 depth = reiserfs_write_lock_once(inode->i_sb);
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400478 error = journal_begin(&th, inode->i_sb, size * 2);
479 if (!error) {
480 int error2;
481 error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS,
482 clone);
483 error2 = journal_end(&th, inode->i_sb, size * 2);
484 if (error2)
485 error = error2;
486 }
Frederic Weisbecker238af872010-12-02 14:31:16 -0800487 reiserfs_write_unlock_once(inode->i_sb, depth);
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400488 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700489 posix_acl_release(clone);
490 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Christoph Hellwig431547b2009-11-13 09:52:56 +0000493static size_t posix_acl_access_list(struct dentry *dentry, char *list,
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400494 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000495 size_t name_len, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400497 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000498 if (!reiserfs_posixacl(dentry->d_sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700499 return 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400500 if (list && size <= list_size)
501 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
502 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Stephen Hemminger94d09a92010-05-13 17:53:19 -0700505const struct xattr_handler reiserfs_posix_acl_access_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700506 .prefix = POSIX_ACL_XATTR_ACCESS,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000507 .flags = ACL_TYPE_ACCESS,
508 .get = posix_acl_get,
509 .set = posix_acl_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .list = posix_acl_access_list,
511};
512
Christoph Hellwig431547b2009-11-13 09:52:56 +0000513static size_t posix_acl_default_list(struct dentry *dentry, char *list,
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400514 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000515 size_t name_len, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400517 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000518 if (!reiserfs_posixacl(dentry->d_sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700519 return 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400520 if (list && size <= list_size)
521 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
522 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Stephen Hemminger94d09a92010-05-13 17:53:19 -0700525const struct xattr_handler reiserfs_posix_acl_default_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700526 .prefix = POSIX_ACL_XATTR_DEFAULT,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000527 .flags = ACL_TYPE_DEFAULT,
528 .get = posix_acl_get,
529 .set = posix_acl_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 .list = posix_acl_default_list,
531};