0001
0002 #include <linux/capability.h>
0003 #include <linux/fs.h>
0004 #include <linux/posix_acl.h>
0005 #include "reiserfs.h"
0006 #include <linux/errno.h>
0007 #include <linux/pagemap.h>
0008 #include <linux/xattr.h>
0009 #include <linux/slab.h>
0010 #include <linux/posix_acl_xattr.h>
0011 #include "xattr.h"
0012 #include "acl.h"
0013 #include <linux/uaccess.h>
0014
0015 static int __reiserfs_set_acl(struct reiserfs_transaction_handle *th,
0016 struct inode *inode, int type,
0017 struct posix_acl *acl);
0018
0019
0020 int
0021 reiserfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
0022 struct posix_acl *acl, int type)
0023 {
0024 int error, error2;
0025 struct reiserfs_transaction_handle th;
0026 size_t jcreate_blocks;
0027 int size = acl ? posix_acl_xattr_size(acl->a_count) : 0;
0028 int update_mode = 0;
0029 umode_t mode = inode->i_mode;
0030
0031
0032
0033
0034
0035
0036 jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
0037 reiserfs_xattr_nblocks(inode, size) * 2;
0038
0039 reiserfs_write_lock(inode->i_sb);
0040 error = journal_begin(&th, inode->i_sb, jcreate_blocks);
0041 reiserfs_write_unlock(inode->i_sb);
0042 if (error == 0) {
0043 if (type == ACL_TYPE_ACCESS && acl) {
0044 error = posix_acl_update_mode(&init_user_ns, inode,
0045 &mode, &acl);
0046 if (error)
0047 goto unlock;
0048 update_mode = 1;
0049 }
0050 error = __reiserfs_set_acl(&th, inode, type, acl);
0051 if (!error && update_mode)
0052 inode->i_mode = mode;
0053 unlock:
0054 reiserfs_write_lock(inode->i_sb);
0055 error2 = journal_end(&th);
0056 reiserfs_write_unlock(inode->i_sb);
0057 if (error2)
0058 error = error2;
0059 }
0060
0061 return error;
0062 }
0063
0064
0065
0066
0067 static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size)
0068 {
0069 const char *end = (char *)value + size;
0070 int n, count;
0071 struct posix_acl *acl;
0072
0073 if (!value)
0074 return NULL;
0075 if (size < sizeof(reiserfs_acl_header))
0076 return ERR_PTR(-EINVAL);
0077 if (((reiserfs_acl_header *) value)->a_version !=
0078 cpu_to_le32(REISERFS_ACL_VERSION))
0079 return ERR_PTR(-EINVAL);
0080 value = (char *)value + sizeof(reiserfs_acl_header);
0081 count = reiserfs_acl_count(size);
0082 if (count < 0)
0083 return ERR_PTR(-EINVAL);
0084 if (count == 0)
0085 return NULL;
0086 acl = posix_acl_alloc(count, GFP_NOFS);
0087 if (!acl)
0088 return ERR_PTR(-ENOMEM);
0089 for (n = 0; n < count; n++) {
0090 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
0091 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
0092 goto fail;
0093 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
0094 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
0095 switch (acl->a_entries[n].e_tag) {
0096 case ACL_USER_OBJ:
0097 case ACL_GROUP_OBJ:
0098 case ACL_MASK:
0099 case ACL_OTHER:
0100 value = (char *)value +
0101 sizeof(reiserfs_acl_entry_short);
0102 break;
0103
0104 case ACL_USER:
0105 value = (char *)value + sizeof(reiserfs_acl_entry);
0106 if ((char *)value > end)
0107 goto fail;
0108 acl->a_entries[n].e_uid =
0109 make_kuid(&init_user_ns,
0110 le32_to_cpu(entry->e_id));
0111 break;
0112 case ACL_GROUP:
0113 value = (char *)value + sizeof(reiserfs_acl_entry);
0114 if ((char *)value > end)
0115 goto fail;
0116 acl->a_entries[n].e_gid =
0117 make_kgid(&init_user_ns,
0118 le32_to_cpu(entry->e_id));
0119 break;
0120
0121 default:
0122 goto fail;
0123 }
0124 }
0125 if (value != end)
0126 goto fail;
0127 return acl;
0128
0129 fail:
0130 posix_acl_release(acl);
0131 return ERR_PTR(-EINVAL);
0132 }
0133
0134
0135
0136
0137 static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
0138 {
0139 reiserfs_acl_header *ext_acl;
0140 char *e;
0141 int n;
0142
0143 *size = reiserfs_acl_size(acl->a_count);
0144 ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
0145 acl->a_count *
0146 sizeof(reiserfs_acl_entry),
0147 GFP_NOFS);
0148 if (!ext_acl)
0149 return ERR_PTR(-ENOMEM);
0150 ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
0151 e = (char *)ext_acl + sizeof(reiserfs_acl_header);
0152 for (n = 0; n < acl->a_count; n++) {
0153 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
0154 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
0155 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
0156 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
0157 switch (acl->a_entries[n].e_tag) {
0158 case ACL_USER:
0159 entry->e_id = cpu_to_le32(
0160 from_kuid(&init_user_ns, acl_e->e_uid));
0161 e += sizeof(reiserfs_acl_entry);
0162 break;
0163 case ACL_GROUP:
0164 entry->e_id = cpu_to_le32(
0165 from_kgid(&init_user_ns, acl_e->e_gid));
0166 e += sizeof(reiserfs_acl_entry);
0167 break;
0168
0169 case ACL_USER_OBJ:
0170 case ACL_GROUP_OBJ:
0171 case ACL_MASK:
0172 case ACL_OTHER:
0173 e += sizeof(reiserfs_acl_entry_short);
0174 break;
0175
0176 default:
0177 goto fail;
0178 }
0179 }
0180 return (char *)ext_acl;
0181
0182 fail:
0183 kfree(ext_acl);
0184 return ERR_PTR(-EINVAL);
0185 }
0186
0187
0188
0189
0190
0191
0192
0193 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type, bool rcu)
0194 {
0195 char *name, *value;
0196 struct posix_acl *acl;
0197 int size;
0198 int retval;
0199
0200 if (rcu)
0201 return ERR_PTR(-ECHILD);
0202
0203 switch (type) {
0204 case ACL_TYPE_ACCESS:
0205 name = XATTR_NAME_POSIX_ACL_ACCESS;
0206 break;
0207 case ACL_TYPE_DEFAULT:
0208 name = XATTR_NAME_POSIX_ACL_DEFAULT;
0209 break;
0210 default:
0211 BUG();
0212 }
0213
0214 size = reiserfs_xattr_get(inode, name, NULL, 0);
0215 if (size < 0) {
0216 if (size == -ENODATA || size == -ENOSYS)
0217 return NULL;
0218 return ERR_PTR(size);
0219 }
0220
0221 value = kmalloc(size, GFP_NOFS);
0222 if (!value)
0223 return ERR_PTR(-ENOMEM);
0224
0225 retval = reiserfs_xattr_get(inode, name, value, size);
0226 if (retval == -ENODATA || retval == -ENOSYS) {
0227
0228
0229
0230
0231 acl = NULL;
0232 } else if (retval < 0) {
0233 acl = ERR_PTR(retval);
0234 } else {
0235 acl = reiserfs_posix_acl_from_disk(value, retval);
0236 }
0237
0238 kfree(value);
0239 return acl;
0240 }
0241
0242
0243
0244
0245
0246
0247
0248 static int
0249 __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
0250 int type, struct posix_acl *acl)
0251 {
0252 char *name;
0253 void *value = NULL;
0254 size_t size = 0;
0255 int error;
0256
0257 switch (type) {
0258 case ACL_TYPE_ACCESS:
0259 name = XATTR_NAME_POSIX_ACL_ACCESS;
0260 break;
0261 case ACL_TYPE_DEFAULT:
0262 name = XATTR_NAME_POSIX_ACL_DEFAULT;
0263 if (!S_ISDIR(inode->i_mode))
0264 return acl ? -EACCES : 0;
0265 break;
0266 default:
0267 return -EINVAL;
0268 }
0269
0270 if (acl) {
0271 value = reiserfs_posix_acl_to_disk(acl, &size);
0272 if (IS_ERR(value))
0273 return (int)PTR_ERR(value);
0274 }
0275
0276 error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
0277
0278
0279
0280
0281
0282
0283
0284 if (error == -ENODATA) {
0285 error = 0;
0286 if (type == ACL_TYPE_ACCESS) {
0287 inode->i_ctime = current_time(inode);
0288 mark_inode_dirty(inode);
0289 }
0290 }
0291
0292 kfree(value);
0293
0294 if (!error)
0295 set_cached_acl(inode, type, acl);
0296
0297 return error;
0298 }
0299
0300
0301
0302
0303
0304 int
0305 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
0306 struct inode *dir, struct dentry *dentry,
0307 struct inode *inode)
0308 {
0309 struct posix_acl *default_acl, *acl;
0310 int err = 0;
0311
0312
0313 if (S_ISLNK(inode->i_mode))
0314 return 0;
0315
0316
0317
0318
0319
0320 if (get_inode_sd_version(dir) == STAT_DATA_V1)
0321 goto apply_umask;
0322
0323
0324
0325
0326
0327
0328 if (IS_PRIVATE(inode))
0329 goto apply_umask;
0330
0331 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
0332 if (err)
0333 return err;
0334
0335 if (default_acl) {
0336 err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
0337 default_acl);
0338 posix_acl_release(default_acl);
0339 }
0340 if (acl) {
0341 if (!err)
0342 err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
0343 acl);
0344 posix_acl_release(acl);
0345 }
0346
0347 return err;
0348
0349 apply_umask:
0350
0351 inode->i_mode &= ~current_umask();
0352 return err;
0353 }
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366 int reiserfs_cache_default_acl(struct inode *inode)
0367 {
0368 struct posix_acl *acl;
0369 int nblocks = 0;
0370
0371 if (IS_PRIVATE(inode))
0372 return 0;
0373
0374 acl = get_acl(inode, ACL_TYPE_DEFAULT);
0375
0376 if (acl && !IS_ERR(acl)) {
0377 int size = reiserfs_acl_size(acl->a_count);
0378
0379
0380
0381
0382
0383 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
0384 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
0385
0386 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
0387
0388
0389 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
0390 posix_acl_release(acl);
0391 }
0392
0393 return nblocks;
0394 }
0395
0396
0397
0398
0399 int reiserfs_acl_chmod(struct inode *inode)
0400 {
0401 if (IS_PRIVATE(inode))
0402 return 0;
0403 if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
0404 !reiserfs_posixacl(inode->i_sb))
0405 return 0;
0406
0407 return posix_acl_chmod(&init_user_ns, inode, inode->i_mode);
0408 }