blob: 92cc0ac2d1fc12d11596ebe5b2778a100be6fa3b [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
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -080094int jfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 int rc;
Al Virobc26ab52011-07-23 00:18:02 -040097 tid_t tid;
Ernesto A. Fernándezf070e5a2017-07-12 06:55:35 -030098 int update_mode = 0;
99 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Al Virobc26ab52011-07-23 00:18:02 -0400101 tid = txBegin(inode->i_sb, 0);
102 mutex_lock(&JFS_IP(inode)->commit_mutex);
Jan Kara9bcf66c2017-06-22 15:31:10 +0200103 if (type == ACL_TYPE_ACCESS && acl) {
Ernesto A. Fernándezf070e5a2017-07-12 06:55:35 -0300104 rc = posix_acl_update_mode(inode, &mode, &acl);
Jan Kara9bcf66c2017-06-22 15:31:10 +0200105 if (rc)
106 goto end_tx;
Chengguang Xu7ca5e8f2018-11-24 17:40:44 +0800107 if (mode != inode->i_mode)
108 update_mode = 1;
Jan Kara9bcf66c2017-06-22 15:31:10 +0200109 }
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800110 rc = __jfs_set_acl(tid, inode, type, acl);
Ernesto A. Fernándezf070e5a2017-07-12 06:55:35 -0300111 if (!rc) {
112 if (update_mode) {
113 inode->i_mode = mode;
114 inode->i_ctime = current_time(inode);
115 mark_inode_dirty(inode);
116 }
Al Virobc26ab52011-07-23 00:18:02 -0400117 rc = txCommit(tid, 1, &inode, 0);
Ernesto A. Fernándezf070e5a2017-07-12 06:55:35 -0300118 }
Jan Kara9bcf66c2017-06-22 15:31:10 +0200119end_tx:
Al Virobc26ab52011-07-23 00:18:02 -0400120 txEnd(tid);
121 mutex_unlock(&JFS_IP(inode)->commit_mutex);
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800122 return rc;
123}
Al Virobc26ab52011-07-23 00:18:02 -0400124
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800125int jfs_init_acl(tid_t tid, struct inode *inode, struct inode *dir)
126{
127 struct posix_acl *default_acl, *acl;
128 int rc = 0;
129
130 rc = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
131 if (rc)
132 return rc;
133
134 if (default_acl) {
135 rc = __jfs_set_acl(tid, inode, ACL_TYPE_DEFAULT, default_acl);
136 posix_acl_release(default_acl);
Chengguang Xue8d4cee2018-08-31 22:33:51 +0800137 } else {
138 inode->i_default_acl = NULL;
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800139 }
140
141 if (acl) {
142 if (!rc)
143 rc = __jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, acl);
144 posix_acl_release(acl);
Chengguang Xue8d4cee2018-08-31 22:33:51 +0800145 } else {
146 inode->i_acl = NULL;
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800147 }
148
149 JFS_IP(inode)->mode2 = (JFS_IP(inode)->mode2 & 0xffff0000) |
150 inode->i_mode;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return rc;
153}