blob: a653f34c6e263ab755b71d6701bc83753f479780 [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
Miklos Szeredi0cad6242021-08-18 22:08:24 +020017struct posix_acl *jfs_get_acl(struct inode *inode, int type, bool rcu)
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
Miklos Szeredi0cad6242021-08-18 22:08:24 +020024 if (rcu)
25 return ERR_PTR(-ECHILD);
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 switch(type) {
28 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010029 ea_name = XATTR_NAME_POSIX_ACL_ACCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 break;
31 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010032 ea_name = XATTR_NAME_POSIX_ACL_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 break;
34 default:
35 return ERR_PTR(-EINVAL);
36 }
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 size = __jfs_getxattr(inode, ea_name, NULL, 0);
39
40 if (size > 0) {
41 value = kmalloc(size, GFP_KERNEL);
42 if (!value)
43 return ERR_PTR(-ENOMEM);
44 size = __jfs_getxattr(inode, ea_name, value, size);
45 }
46
47 if (size < 0) {
Al Viro073aaa12009-06-09 12:11:54 -040048 if (size == -ENODATA)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 acl = NULL;
Al Viro073aaa12009-06-09 12:11:54 -040050 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 acl = ERR_PTR(size);
52 } else {
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070053 acl = posix_acl_from_xattr(&init_user_ns, value, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 }
Jesper Juhl259692b2005-05-09 10:47:14 -050055 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return acl;
57}
58
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -080059static int __jfs_set_acl(tid_t tid, struct inode *inode, int type,
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050060 struct posix_acl *acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 char *ea_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int rc;
64 int size = 0;
65 char *value = NULL;
66
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -080067 switch (type) {
68 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010069 ea_name = XATTR_NAME_POSIX_ACL_ACCESS;
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -080070 break;
71 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010072 ea_name = XATTR_NAME_POSIX_ACL_DEFAULT;
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -080073 break;
74 default:
75 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -080077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (acl) {
Christoph Hellwig9a59f4522005-06-23 00:10:19 -070079 size = posix_acl_xattr_size(acl->a_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 value = kmalloc(size, GFP_KERNEL);
81 if (!value)
82 return -ENOMEM;
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070083 rc = posix_acl_to_xattr(&init_user_ns, acl, value, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (rc < 0)
85 goto out;
86 }
Dave Kleikamp4f4b4012005-09-01 09:02:43 -050087 rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088out:
Jesper Juhl259692b2005-05-09 10:47:14 -050089 kfree(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Al Viro073aaa12009-06-09 12:11:54 -040091 if (!rc)
92 set_cached_acl(inode, type, acl);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return rc;
95}
96
Christian Brauner549c7292021-01-21 14:19:43 +010097int jfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
98 struct posix_acl *acl, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 int rc;
Al Virobc26ab52011-07-23 00:18:02 -0400101 tid_t tid;
Ernesto A. Fernándezf070e5a2017-07-12 06:55:35 -0300102 int update_mode = 0;
103 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Al Virobc26ab52011-07-23 00:18:02 -0400105 tid = txBegin(inode->i_sb, 0);
106 mutex_lock(&JFS_IP(inode)->commit_mutex);
Jan Kara9bcf66c2017-06-22 15:31:10 +0200107 if (type == ACL_TYPE_ACCESS && acl) {
Christian Braunere65ce2a2021-01-21 14:19:27 +0100108 rc = posix_acl_update_mode(&init_user_ns, inode, &mode, &acl);
Jan Kara9bcf66c2017-06-22 15:31:10 +0200109 if (rc)
110 goto end_tx;
Chengguang Xu7ca5e8f2018-11-24 17:40:44 +0800111 if (mode != inode->i_mode)
112 update_mode = 1;
Jan Kara9bcf66c2017-06-22 15:31:10 +0200113 }
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800114 rc = __jfs_set_acl(tid, inode, type, acl);
Ernesto A. Fernándezf070e5a2017-07-12 06:55:35 -0300115 if (!rc) {
116 if (update_mode) {
117 inode->i_mode = mode;
118 inode->i_ctime = current_time(inode);
119 mark_inode_dirty(inode);
120 }
Al Virobc26ab52011-07-23 00:18:02 -0400121 rc = txCommit(tid, 1, &inode, 0);
Ernesto A. Fernándezf070e5a2017-07-12 06:55:35 -0300122 }
Jan Kara9bcf66c2017-06-22 15:31:10 +0200123end_tx:
Al Virobc26ab52011-07-23 00:18:02 -0400124 txEnd(tid);
125 mutex_unlock(&JFS_IP(inode)->commit_mutex);
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800126 return rc;
127}
Al Virobc26ab52011-07-23 00:18:02 -0400128
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800129int jfs_init_acl(tid_t tid, struct inode *inode, struct inode *dir)
130{
131 struct posix_acl *default_acl, *acl;
132 int rc = 0;
133
134 rc = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
135 if (rc)
136 return rc;
137
138 if (default_acl) {
139 rc = __jfs_set_acl(tid, inode, ACL_TYPE_DEFAULT, default_acl);
140 posix_acl_release(default_acl);
Chengguang Xue8d4cee2018-08-31 22:33:51 +0800141 } else {
142 inode->i_default_acl = NULL;
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800143 }
144
145 if (acl) {
146 if (!rc)
147 rc = __jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, acl);
148 posix_acl_release(acl);
Chengguang Xue8d4cee2018-08-31 22:33:51 +0800149 } else {
150 inode->i_acl = NULL;
Christoph Hellwig2cc6a5a2013-12-20 05:16:51 -0800151 }
152
153 JFS_IP(inode)->mode2 = (JFS_IP(inode)->mode2 & 0xffff0000) |
154 inode->i_mode;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return rc;
157}