Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) International Business Machines Corp., 2002-2004 |
| 4 | * Copyright (C) Andreas Gruenbacher, 2001 |
| 5 | * Copyright (C) Linus Torvalds, 1991, 1992 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/fs.h> |
Christoph Hellwig | 9a59f452 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 11 | #include <linux/posix_acl_xattr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "jfs_incore.h" |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 13 | #include "jfs_txnmgr.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "jfs_xattr.h" |
| 15 | #include "jfs_acl.h" |
| 16 | |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 17 | struct posix_acl *jfs_get_acl(struct inode *inode, int type, bool rcu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | { |
| 19 | struct posix_acl *acl; |
| 20 | char *ea_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | int size; |
| 22 | char *value = NULL; |
| 23 | |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 24 | if (rcu) |
| 25 | return ERR_PTR(-ECHILD); |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | switch(type) { |
| 28 | case ACL_TYPE_ACCESS: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 29 | ea_name = XATTR_NAME_POSIX_ACL_ACCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | break; |
| 31 | case ACL_TYPE_DEFAULT: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 32 | ea_name = XATTR_NAME_POSIX_ACL_DEFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | break; |
| 34 | default: |
| 35 | return ERR_PTR(-EINVAL); |
| 36 | } |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | 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 Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 48 | if (size == -ENODATA) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | acl = NULL; |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 50 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | acl = ERR_PTR(size); |
| 52 | } else { |
Eric W. Biederman | 5f3a4a28 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 53 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
Jesper Juhl | 259692b | 2005-05-09 10:47:14 -0500 | [diff] [blame] | 55 | kfree(value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return acl; |
| 57 | } |
| 58 | |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 59 | static int __jfs_set_acl(tid_t tid, struct inode *inode, int type, |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 60 | struct posix_acl *acl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | char *ea_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | int rc; |
| 64 | int size = 0; |
| 65 | char *value = NULL; |
| 66 | |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 67 | switch (type) { |
| 68 | case ACL_TYPE_ACCESS: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 69 | ea_name = XATTR_NAME_POSIX_ACL_ACCESS; |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 70 | break; |
| 71 | case ACL_TYPE_DEFAULT: |
Andreas Gruenbacher | 97d7929 | 2015-12-02 14:44:35 +0100 | [diff] [blame] | 72 | ea_name = XATTR_NAME_POSIX_ACL_DEFAULT; |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 73 | break; |
| 74 | default: |
| 75 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | if (acl) { |
Christoph Hellwig | 9a59f452 | 2005-06-23 00:10:19 -0700 | [diff] [blame] | 79 | size = posix_acl_xattr_size(acl->a_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | value = kmalloc(size, GFP_KERNEL); |
| 81 | if (!value) |
| 82 | return -ENOMEM; |
Eric W. Biederman | 5f3a4a28 | 2012-09-10 20:17:44 -0700 | [diff] [blame] | 83 | rc = posix_acl_to_xattr(&init_user_ns, acl, value, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | if (rc < 0) |
| 85 | goto out; |
| 86 | } |
Dave Kleikamp | 4f4b401 | 2005-09-01 09:02:43 -0500 | [diff] [blame] | 87 | rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | out: |
Jesper Juhl | 259692b | 2005-05-09 10:47:14 -0500 | [diff] [blame] | 89 | kfree(value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Al Viro | 073aaa1 | 2009-06-09 12:11:54 -0400 | [diff] [blame] | 91 | if (!rc) |
| 92 | set_cached_acl(inode, type, acl); |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return rc; |
| 95 | } |
| 96 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 97 | int jfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode, |
| 98 | struct posix_acl *acl, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | int rc; |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 101 | tid_t tid; |
Ernesto A. Fernández | f070e5a | 2017-07-12 06:55:35 -0300 | [diff] [blame] | 102 | int update_mode = 0; |
| 103 | umode_t mode = inode->i_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 105 | tid = txBegin(inode->i_sb, 0); |
| 106 | mutex_lock(&JFS_IP(inode)->commit_mutex); |
Jan Kara | 9bcf66c | 2017-06-22 15:31:10 +0200 | [diff] [blame] | 107 | if (type == ACL_TYPE_ACCESS && acl) { |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 108 | rc = posix_acl_update_mode(&init_user_ns, inode, &mode, &acl); |
Jan Kara | 9bcf66c | 2017-06-22 15:31:10 +0200 | [diff] [blame] | 109 | if (rc) |
| 110 | goto end_tx; |
Chengguang Xu | 7ca5e8f | 2018-11-24 17:40:44 +0800 | [diff] [blame] | 111 | if (mode != inode->i_mode) |
| 112 | update_mode = 1; |
Jan Kara | 9bcf66c | 2017-06-22 15:31:10 +0200 | [diff] [blame] | 113 | } |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 114 | rc = __jfs_set_acl(tid, inode, type, acl); |
Ernesto A. Fernández | f070e5a | 2017-07-12 06:55:35 -0300 | [diff] [blame] | 115 | 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 Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 121 | rc = txCommit(tid, 1, &inode, 0); |
Ernesto A. Fernández | f070e5a | 2017-07-12 06:55:35 -0300 | [diff] [blame] | 122 | } |
Jan Kara | 9bcf66c | 2017-06-22 15:31:10 +0200 | [diff] [blame] | 123 | end_tx: |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 124 | txEnd(tid); |
| 125 | mutex_unlock(&JFS_IP(inode)->commit_mutex); |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 126 | return rc; |
| 127 | } |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 128 | |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 129 | int 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 Xu | e8d4cee | 2018-08-31 22:33:51 +0800 | [diff] [blame] | 141 | } else { |
| 142 | inode->i_default_acl = NULL; |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if (acl) { |
| 146 | if (!rc) |
| 147 | rc = __jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, acl); |
| 148 | posix_acl_release(acl); |
Chengguang Xu | e8d4cee | 2018-08-31 22:33:51 +0800 | [diff] [blame] | 149 | } else { |
| 150 | inode->i_acl = NULL; |
Christoph Hellwig | 2cc6a5a | 2013-12-20 05:16:51 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | JFS_IP(inode)->mode2 = (JFS_IP(inode)->mode2 & 0xffff0000) | |
| 154 | inode->i_mode; |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return rc; |
| 157 | } |