blob: cc32e6ada67b3330aa0a9e3fc3bacfd0ca7c7c61 [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
Christoph Hellwig431547b2009-11-13 09:52:56 +000018posix_acl_set(struct dentry *dentry, const char *name, const void *value,
19 size_t size, int flags, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Christoph Hellwig431547b2009-11-13 09:52:56 +000021 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 struct posix_acl *acl;
Jeff Mahoney0ab26212009-03-30 14:02:39 -040023 int error, error2;
24 struct reiserfs_transaction_handle th;
25 size_t jcreate_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 if (!reiserfs_posixacl(inode->i_sb))
27 return -EOPNOTSUPP;
Satyam Sharma3bd858a2007-07-17 15:00:08 +053028 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 return -EPERM;
30
31 if (value) {
32 acl = posix_acl_from_xattr(value, size);
33 if (IS_ERR(acl)) {
34 return PTR_ERR(acl);
35 } else if (acl) {
36 error = posix_acl_valid(acl);
37 if (error)
38 goto release_and_out;
39 }
40 } else
41 acl = NULL;
42
Jeff Mahoney0ab26212009-03-30 14:02:39 -040043 /* Pessimism: We can't assume that anything from the xattr root up
44 * has been created. */
45
46 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
47 reiserfs_xattr_nblocks(inode, size) * 2;
48
49 reiserfs_write_lock(inode->i_sb);
50 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
51 if (error == 0) {
52 error = reiserfs_set_acl(&th, inode, type, acl);
53 error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
54 if (error2)
55 error = error2;
56 }
57 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070059 release_and_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 posix_acl_release(acl);
61 return error;
62}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int
Christoph Hellwig431547b2009-11-13 09:52:56 +000065posix_acl_get(struct dentry *dentry, const char *name, void *buffer,
66 size_t size, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 struct posix_acl *acl;
69 int error;
70
Christoph Hellwig431547b2009-11-13 09:52:56 +000071 if (!reiserfs_posixacl(dentry->d_sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return -EOPNOTSUPP;
73
Christoph Hellwig431547b2009-11-13 09:52:56 +000074 acl = reiserfs_get_acl(dentry->d_inode, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (IS_ERR(acl))
76 return PTR_ERR(acl);
77 if (acl == NULL)
78 return -ENODATA;
79 error = posix_acl_to_xattr(acl, buffer, size);
80 posix_acl_release(acl);
81
82 return error;
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/*
86 * Convert from filesystem to in-memory representation.
87 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070088static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 const char *end = (char *)value + size;
91 int n, count;
92 struct posix_acl *acl;
93
94 if (!value)
95 return NULL;
96 if (size < sizeof(reiserfs_acl_header))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070097 return ERR_PTR(-EINVAL);
98 if (((reiserfs_acl_header *) value)->a_version !=
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 cpu_to_le32(REISERFS_ACL_VERSION))
100 return ERR_PTR(-EINVAL);
101 value = (char *)value + sizeof(reiserfs_acl_header);
102 count = reiserfs_acl_count(size);
103 if (count < 0)
104 return ERR_PTR(-EINVAL);
105 if (count == 0)
106 return NULL;
107 acl = posix_acl_alloc(count, GFP_NOFS);
108 if (!acl)
109 return ERR_PTR(-ENOMEM);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700110 for (n = 0; n < count; n++) {
111 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
113 goto fail;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700114 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700116 switch (acl->a_entries[n].e_tag) {
117 case ACL_USER_OBJ:
118 case ACL_GROUP_OBJ:
119 case ACL_MASK:
120 case ACL_OTHER:
121 value = (char *)value +
122 sizeof(reiserfs_acl_entry_short);
123 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
124 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700126 case ACL_USER:
127 case ACL_GROUP:
128 value = (char *)value + sizeof(reiserfs_acl_entry);
129 if ((char *)value > end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 goto fail;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700131 acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
132 break;
133
134 default:
135 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 }
138 if (value != end)
139 goto fail;
140 return acl;
141
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700142 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 posix_acl_release(acl);
144 return ERR_PTR(-EINVAL);
145}
146
147/*
148 * Convert from in-memory to filesystem representation.
149 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700150static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 reiserfs_acl_header *ext_acl;
153 char *e;
154 int n;
155
156 *size = reiserfs_acl_size(acl->a_count);
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800157 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700158 acl->a_count *
159 sizeof(reiserfs_acl_entry),
160 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!ext_acl)
162 return ERR_PTR(-ENOMEM);
163 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
164 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700165 for (n = 0; n < acl->a_count; n++) {
166 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
167 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700169 switch (acl->a_entries[n].e_tag) {
170 case ACL_USER:
171 case ACL_GROUP:
172 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
173 e += sizeof(reiserfs_acl_entry);
174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176 case ACL_USER_OBJ:
177 case ACL_GROUP_OBJ:
178 case ACL_MASK:
179 case ACL_OTHER:
180 e += sizeof(reiserfs_acl_entry_short);
181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700183 default:
184 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186 }
187 return (char *)ext_acl;
188
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700189 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 kfree(ext_acl);
191 return ERR_PTR(-EINVAL);
192}
193
194/*
195 * Inode operation get_posix_acl().
196 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800197 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * BKL held [before 2.5.x]
199 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700200struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 char *name, *value;
Al Viro073aaa12009-06-09 12:11:54 -0400203 struct posix_acl *acl;
Adrian Bunk3cdc4092006-03-25 03:07:50 -0800204 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Al Viro073aaa12009-06-09 12:11:54 -0400207 acl = get_cached_acl(inode, type);
208 if (acl != ACL_NOT_CACHED)
209 return acl;
210
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700211 switch (type) {
212 case ACL_TYPE_ACCESS:
213 name = POSIX_ACL_XATTR_ACCESS;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700214 break;
215 case ACL_TYPE_DEFAULT:
216 name = POSIX_ACL_XATTR_DEFAULT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700217 break;
218 default:
Al Viro073aaa12009-06-09 12:11:54 -0400219 BUG();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222 size = reiserfs_xattr_get(inode, name, NULL, 0);
Adrian Bunk3cdc4092006-03-25 03:07:50 -0800223 if (size < 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700224 if (size == -ENODATA || size == -ENOSYS) {
Al Viro073aaa12009-06-09 12:11:54 -0400225 set_cached_acl(inode, type, NULL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700226 return NULL;
227 }
228 return ERR_PTR(size);
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700231 value = kmalloc(size, GFP_NOFS);
232 if (!value)
233 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 retval = reiserfs_xattr_get(inode, name, value, size);
236 if (retval == -ENODATA || retval == -ENOSYS) {
237 /* This shouldn't actually happen as it should have
238 been caught above.. but just in case */
239 acl = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700240 } else if (retval < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 acl = ERR_PTR(retval);
242 } else {
243 acl = posix_acl_from_disk(value, retval);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700244 }
Al Viro073aaa12009-06-09 12:11:54 -0400245 if (!IS_ERR(acl))
246 set_cached_acl(inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 kfree(value);
249 return acl;
250}
251
252/*
253 * Inode operation set_posix_acl().
254 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800255 * inode->i_mutex: down
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * BKL held [before 2.5.x]
257 */
258static int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400259reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
260 int type, struct posix_acl *acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700262 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 void *value = NULL;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400264 size_t size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if (S_ISLNK(inode->i_mode))
268 return -EOPNOTSUPP;
269
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700270 switch (type) {
271 case ACL_TYPE_ACCESS:
272 name = POSIX_ACL_XATTR_ACCESS;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700273 if (acl) {
274 mode_t mode = inode->i_mode;
275 error = posix_acl_equiv_mode(acl, &mode);
276 if (error < 0)
277 return error;
278 else {
279 inode->i_mode = mode;
280 if (error == 0)
281 acl = NULL;
282 }
283 }
284 break;
285 case ACL_TYPE_DEFAULT:
286 name = POSIX_ACL_XATTR_DEFAULT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700287 if (!S_ISDIR(inode->i_mode))
288 return acl ? -EACCES : 0;
289 break;
290 default:
291 return -EINVAL;
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700294 if (acl) {
295 value = posix_acl_to_disk(acl, &size);
296 if (IS_ERR(value))
297 return (int)PTR_ERR(value);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400298 }
299
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400300 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400301
302 /*
303 * Ensure that the inode gets dirtied if we're only using
304 * the mode bits and an old ACL didn't exist. We don't need
305 * to check if the inode is hashed here since we won't get
306 * called by reiserfs_inherit_default_acl().
307 */
308 if (error == -ENODATA) {
309 error = 0;
310 if (type == ACL_TYPE_ACCESS) {
311 inode->i_ctime = CURRENT_TIME_SEC;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700312 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700313 }
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
James Lamanna833d3042005-10-30 15:00:16 -0800316 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jeff Mahoneyd9845612009-03-30 14:02:35 -0400318 if (!error)
Al Viro073aaa12009-06-09 12:11:54 -0400319 set_cached_acl(inode, type, acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 return error;
322}
323
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800324/* dir->i_mutex: locked,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * inode is new and not released into the wild yet */
326int
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400327reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
328 struct inode *dir, struct dentry *dentry,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700329 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700331 struct posix_acl *acl;
332 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700334 /* ACLs only get applied to files and directories */
335 if (S_ISLNK(inode->i_mode))
336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700338 /* ACLs can only be used on "new" objects, so if it's an old object
339 * there is nothing to inherit from */
340 if (get_inode_sd_version(dir) == STAT_DATA_V1)
341 goto apply_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700343 /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
344 * would be useless since permissions are ignored, and a pain because
345 * it introduces locking cycles */
Jeff Mahoney6dfede62009-03-30 14:02:32 -0400346 if (IS_PRIVATE(dir)) {
347 inode->i_flags |= S_PRIVATE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700348 goto apply_umask;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700351 acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
Al Viro7a77b152009-06-08 21:01:13 -0400352 if (IS_ERR(acl))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700353 return PTR_ERR(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700355 if (acl) {
356 struct posix_acl *acl_copy;
357 mode_t mode = inode->i_mode;
358 int need_acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700360 /* Copy the default ACL to the default ACL of a new directory */
361 if (S_ISDIR(inode->i_mode)) {
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400362 err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
363 acl);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700364 if (err)
365 goto cleanup;
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700368 /* Now we reconcile the new ACL and the mode,
369 potentially modifying both */
370 acl_copy = posix_acl_clone(acl, GFP_NOFS);
371 if (!acl_copy) {
372 err = -ENOMEM;
373 goto cleanup;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700376 need_acl = posix_acl_create_masq(acl_copy, &mode);
377 if (need_acl >= 0) {
378 if (mode != inode->i_mode) {
379 inode->i_mode = mode;
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700382 /* If we need an ACL.. */
383 if (need_acl > 0) {
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400384 err = reiserfs_set_acl(th, inode,
385 ACL_TYPE_ACCESS,
386 acl_copy);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700387 if (err)
388 goto cleanup_copy;
389 }
390 }
391 cleanup_copy:
392 posix_acl_release(acl_copy);
393 cleanup:
394 posix_acl_release(acl);
395 } else {
396 apply_umask:
397 /* no ACL, apply umask */
Al Viroce3b0f82009-03-29 19:08:22 -0400398 inode->i_mode &= ~current_umask();
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400404/* This is used to cache the default acl before a new object is created.
405 * The biggest reason for this is to get an idea of how many blocks will
406 * actually be required for the create operation if we must inherit an ACL.
407 * An ACL write can add up to 3 object creations and an additional file write
408 * so we'd prefer not to reserve that many blocks in the journal if we can.
409 * It also has the advantage of not loading the ACL with a transaction open,
410 * this may seem silly, but if the owner of the directory is doing the
411 * creation, the ACL may not be loaded since the permissions wouldn't require
412 * it.
413 * We return the number of blocks required for the transaction.
414 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700415int reiserfs_cache_default_acl(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400417 struct posix_acl *acl;
418 int nblocks = 0;
419
420 if (IS_PRIVATE(inode))
421 return 0;
422
423 acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
424
425 if (acl && !IS_ERR(acl)) {
426 int size = reiserfs_acl_size(acl->a_count);
427
428 /* Other xattrs can be created during inode creation. We don't
429 * want to claim too many blocks, so we check to see if we
430 * we need to create the tree to the xattrs, and then we
431 * just want two files. */
432 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
433 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
434
435 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
436
437 /* We need to account for writes + bitmaps for two files */
438 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
439 posix_acl_release(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400442 return nblocks;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700443}
444
445int reiserfs_acl_chmod(struct inode *inode)
446{
447 struct posix_acl *acl, *clone;
448 int error;
449
450 if (S_ISLNK(inode->i_mode))
451 return -EOPNOTSUPP;
452
453 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
454 !reiserfs_posixacl(inode->i_sb)) {
455 return 0;
456 }
457
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700458 acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700459 if (!acl)
460 return 0;
461 if (IS_ERR(acl))
462 return PTR_ERR(acl);
463 clone = posix_acl_clone(acl, GFP_NOFS);
464 posix_acl_release(acl);
465 if (!clone)
466 return -ENOMEM;
467 error = posix_acl_chmod_masq(clone, inode->i_mode);
Jeff Mahoney0ab26212009-03-30 14:02:39 -0400468 if (!error) {
469 struct reiserfs_transaction_handle th;
470 size_t size = reiserfs_xattr_nblocks(inode,
471 reiserfs_acl_size(clone->a_count));
472 reiserfs_write_lock(inode->i_sb);
473 error = journal_begin(&th, inode->i_sb, size * 2);
474 if (!error) {
475 int error2;
476 error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS,
477 clone);
478 error2 = journal_end(&th, inode->i_sb, size * 2);
479 if (error2)
480 error = error2;
481 }
482 reiserfs_write_unlock(inode->i_sb);
483 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700484 posix_acl_release(clone);
485 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Christoph Hellwig431547b2009-11-13 09:52:56 +0000488static size_t posix_acl_access_list(struct dentry *dentry, char *list,
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400489 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000490 size_t name_len, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400492 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000493 if (!reiserfs_posixacl(dentry->d_sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700494 return 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400495 if (list && size <= list_size)
496 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
497 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400500struct xattr_handler reiserfs_posix_acl_access_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700501 .prefix = POSIX_ACL_XATTR_ACCESS,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000502 .flags = ACL_TYPE_ACCESS,
503 .get = posix_acl_get,
504 .set = posix_acl_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .list = posix_acl_access_list,
506};
507
Christoph Hellwig431547b2009-11-13 09:52:56 +0000508static size_t posix_acl_default_list(struct dentry *dentry, char *list,
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400509 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000510 size_t name_len, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400512 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
Christoph Hellwig431547b2009-11-13 09:52:56 +0000513 if (!reiserfs_posixacl(dentry->d_sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700514 return 0;
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400515 if (list && size <= list_size)
516 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
517 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Jeff Mahoney48b32a32009-03-30 14:02:38 -0400520struct xattr_handler reiserfs_posix_acl_default_handler = {
Christoph Hellwig9a59f452005-06-23 00:10:19 -0700521 .prefix = POSIX_ACL_XATTR_DEFAULT,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000522 .flags = ACL_TYPE_DEFAULT,
523 .get = posix_acl_get,
524 .set = posix_acl_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 .list = posix_acl_default_list,
526};