blob: 43c285c3d2a78398797a7a3e1f39fcba83ec7a65 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) International Business Machines Corp., 2002-2004
4 * Copyright (C) Andreas Gruenbacher, 2001
5 * Copyright (C) Linus Torvalds, 1991, 1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/fs.h>
Christoph Hellwig9a59f4522005-06-23 00:10:19 -070011#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "jfs_incore.h"
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050013#include "jfs_txnmgr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "jfs_xattr.h"
15#include "jfs_acl.h"
16
Christoph Hellwig4e34e712011-07-23 17:37:31 +020017struct posix_acl *jfs_get_acl(struct inode *inode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
19 struct posix_acl *acl;
20 char *ea_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 int size;
22 char *value = NULL;
23
24 switch(type) {
25 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010026 ea_name = XATTR_NAME_POSIX_ACL_ACCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 break;
28 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010029 ea_name = XATTR_NAME_POSIX_ACL_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 break;
31 default:
32 return ERR_PTR(-EINVAL);
33 }
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 size = __jfs_getxattr(inode, ea_name, NULL, 0);
36
37 if (size > 0) {
38 value = kmalloc(size, GFP_KERNEL);
39 if (!value)
40 return ERR_PTR(-ENOMEM);
41 size = __jfs_getxattr(inode, ea_name, value, size);
42 }
43
44 if (size < 0) {
Al Viro073aaa12009-06-09 12:11:54 -040045 if (size == -ENODATA)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 acl = NULL;
Al Viro073aaa12009-06-09 12:11:54 -040047 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 acl = ERR_PTR(size);
49 } else {
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070050 acl = posix_acl_from_xattr(&init_user_ns, value, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
Jesper Juhl259692b2005-05-09 10:47:14 -050052 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return acl;
54}
55
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -080056static int __jfs_set_acl(tid_t tid, struct inode *inode, int type,
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050057 struct posix_acl *acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 char *ea_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 int rc;
61 int size = 0;
62 char *value = NULL;
63
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -080064 switch (type) {
65 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010066 ea_name = XATTR_NAME_POSIX_ACL_ACCESS;
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -080067 break;
68 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010069 ea_name = XATTR_NAME_POSIX_ACL_DEFAULT;
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -080070 break;
71 default:
72 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -080074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (acl) {
Christoph Hellwig9a59f4522005-06-23 00:10:19 -070076 size = posix_acl_xattr_size(acl->a_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 value = kmalloc(size, GFP_KERNEL);
78 if (!value)
79 return -ENOMEM;
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070080 rc = posix_acl_to_xattr(&init_user_ns, acl, value, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (rc < 0)
82 goto out;
83 }
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050084 rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085out:
Jesper Juhl259692b2005-05-09 10:47:14 -050086 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Al Viro073aaa12009-06-09 12:11:54 -040088 if (!rc)
89 set_cached_acl(inode, type, acl);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return rc;
92}
93
Christian Brauner549c7292021-01-21 14:19:43 +010094int jfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
95 struct posix_acl *acl, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int rc;
Al Virobc26ab52011-07-23 00:18:02 -040098 tid_t tid;
Ernesto A. Fernándezf070e5a2017-07-12 06:55:35 -030099 int update_mode = 0;
100 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Al Virobc26ab52011-07-23 00:18:02 -0400102 tid = txBegin(inode->i_sb, 0);
103 mutex_lock(&JFS_IP(inode)->commit_mutex);
Jan Kara9bcf66c2017-06-22 15:31:10 +0200104 if (type == ACL_TYPE_ACCESS && acl) {
Christian Braunere65ce2a2021-01-21 14:19:27 +0100105 rc = posix_acl_update_mode(&init_user_ns, inode, &mode, &acl);
Jan Kara9bcf66c2017-06-22 15:31:10 +0200106 if (rc)
107 goto end_tx;
Chengguang Xu7ca5e8f2018-11-24 17:40:44 +0800108 if (mode != inode->i_mode)
109 update_mode = 1;
Jan Kara9bcf66c2017-06-22 15:31:10 +0200110 }
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800111 rc = __jfs_set_acl(tid, inode, type, acl);
Ernesto A. Fernándezf070e5a2017-07-12 06:55:35 -0300112 if (!rc) {
113 if (update_mode) {
114 inode->i_mode = mode;
115 inode->i_ctime = current_time(inode);
116 mark_inode_dirty(inode);
117 }
Al Virobc26ab52011-07-23 00:18:02 -0400118 rc = txCommit(tid, 1, &inode, 0);
Ernesto A. Fernándezf070e5a2017-07-12 06:55:35 -0300119 }
Jan Kara9bcf66c2017-06-22 15:31:10 +0200120end_tx:
Al Virobc26ab52011-07-23 00:18:02 -0400121 txEnd(tid);
122 mutex_unlock(&JFS_IP(inode)->commit_mutex);
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800123 return rc;
124}
Al Virobc26ab52011-07-23 00:18:02 -0400125
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800126int jfs_init_acl(tid_t tid, struct inode *inode, struct inode *dir)
127{
128 struct posix_acl *default_acl, *acl;
129 int rc = 0;
130
131 rc = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
132 if (rc)
133 return rc;
134
135 if (default_acl) {
136 rc = __jfs_set_acl(tid, inode, ACL_TYPE_DEFAULT, default_acl);
137 posix_acl_release(default_acl);
Chengguang Xue8d4cee2018-08-31 22:33:51 +0800138 } else {
139 inode->i_default_acl = NULL;
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800140 }
141
142 if (acl) {
143 if (!rc)
144 rc = __jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, acl);
145 posix_acl_release(acl);
Chengguang Xue8d4cee2018-08-31 22:33:51 +0800146 } else {
147 inode->i_acl = NULL;
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800148 }
149
150 JFS_IP(inode)->mode2 = (JFS_IP(inode)->mode2 & 0xffff0000) |
151 inode->i_mode;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return rc;
154}