blob: 7fe6551bc59b575e5ca3ec84f42ffc188196b45b [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Josef Bacik5103e942007-11-16 11:45:54 -05002/*
3 * Copyright (C) 2007 Red Hat. All rights reserved.
Josef Bacik5103e942007-11-16 11:45:54 -05004 */
5
6#include <linux/fs.h>
7#include <linux/string.h>
8#include <linux/xattr.h>
9#include <linux/posix_acl_xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040010#include <linux/posix_acl.h>
Chris Masonc1e32da2008-01-22 12:46:56 -050011#include <linux/sched.h>
Filipe Mananaa0873492018-12-13 21:16:56 +000012#include <linux/sched/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040014
Josef Bacik5103e942007-11-16 11:45:54 -050015#include "ctree.h"
Josef Bacik33268ea2008-07-24 12:16:36 -040016#include "btrfs_inode.h"
Josef Bacik5103e942007-11-16 11:45:54 -050017#include "xattr.h"
Josef Bacik33268ea2008-07-24 12:16:36 -040018
Christoph Hellwig4e34e712011-07-23 17:37:31 +020019struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -040020{
Christoph Hellwig95819c02008-08-28 06:21:17 -040021 int size;
22 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -040023 char *value = NULL;
Al Viro073aaa12009-06-09 12:11:54 -040024 struct posix_acl *acl;
25
Josef Bacik33268ea2008-07-24 12:16:36 -040026 switch (type) {
27 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010028 name = XATTR_NAME_POSIX_ACL_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -040029 break;
30 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010031 name = XATTR_NAME_POSIX_ACL_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -040032 break;
33 default:
Chengguang Xuab3629e2018-06-27 12:16:34 +080034 return ERR_PTR(-EINVAL);
Josef Bacik33268ea2008-07-24 12:16:36 -040035 }
36
Chengguang Xu7e35eab2018-06-27 12:16:35 +080037 size = btrfs_getxattr(inode, name, NULL, 0);
Josef Bacik33268ea2008-07-24 12:16:36 -040038 if (size > 0) {
David Sterba39a27ec2015-12-03 12:49:48 +010039 value = kzalloc(size, GFP_KERNEL);
Josef Bacik33268ea2008-07-24 12:16:36 -040040 if (!value)
41 return ERR_PTR(-ENOMEM);
David Sterba78527812018-02-27 15:48:52 +010042 size = btrfs_getxattr(inode, name, value, size);
Tsutomu Itohcfbffc32011-10-06 13:37:08 +090043 }
Chengguang Xu4de426c2018-06-27 12:16:38 +080044 if (size > 0)
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070045 acl = posix_acl_from_xattr(&init_user_ns, value, size);
Chengguang Xu4de426c2018-06-27 12:16:38 +080046 else if (size == -ENODATA || size == 0)
Josef Bacik33268ea2008-07-24 12:16:36 -040047 acl = NULL;
Chengguang Xu4de426c2018-06-27 12:16:38 +080048 else
Chengguang Xudc7789e2018-06-27 12:16:37 +080049 acl = ERR_PTR(size);
Tsutomu Itohcfbffc32011-10-06 13:37:08 +090050 kfree(value);
51
Josef Bacik33268ea2008-07-24 12:16:36 -040052 return acl;
53}
54
Christoph Hellwig996a7102013-12-20 05:16:43 -080055static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
Yan, Zhengf34f57a2009-11-12 09:35:27 +000056 struct inode *inode, struct posix_acl *acl, int type)
Josef Bacik33268ea2008-07-24 12:16:36 -040057{
Christoph Hellwig95819c02008-08-28 06:21:17 -040058 int ret, size = 0;
59 const char *name;
Josef Bacik33268ea2008-07-24 12:16:36 -040060 char *value = NULL;
Josef Bacik33268ea2008-07-24 12:16:36 -040061
Josef Bacik33268ea2008-07-24 12:16:36 -040062 switch (type) {
63 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010064 name = XATTR_NAME_POSIX_ACL_ACCESS;
Josef Bacik33268ea2008-07-24 12:16:36 -040065 break;
66 case ACL_TYPE_DEFAULT:
67 if (!S_ISDIR(inode->i_mode))
68 return acl ? -EINVAL : 0;
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010069 name = XATTR_NAME_POSIX_ACL_DEFAULT;
Josef Bacik33268ea2008-07-24 12:16:36 -040070 break;
71 default:
72 return -EINVAL;
73 }
74
75 if (acl) {
Filipe Mananaa0873492018-12-13 21:16:56 +000076 unsigned int nofs_flag;
77
Josef Bacik33268ea2008-07-24 12:16:36 -040078 size = posix_acl_xattr_size(acl->a_count);
Filipe Mananaa0873492018-12-13 21:16:56 +000079 /*
80 * We're holding a transaction handle, so use a NOFS memory
81 * allocation context to avoid deadlock if reclaim happens.
82 */
83 nofs_flag = memalloc_nofs_save();
David Sterba39a27ec2015-12-03 12:49:48 +010084 value = kmalloc(size, GFP_KERNEL);
Filipe Mananaa0873492018-12-13 21:16:56 +000085 memalloc_nofs_restore(nofs_flag);
Josef Bacik33268ea2008-07-24 12:16:36 -040086 if (!value) {
87 ret = -ENOMEM;
88 goto out;
89 }
90
Eric W. Biederman5f3a4a282012-09-10 20:17:44 -070091 ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
Josef Bacik33268ea2008-07-24 12:16:36 -040092 if (ret < 0)
93 goto out;
94 }
95
Anand Jain04e68632019-04-12 16:02:58 +080096 if (trans)
97 ret = btrfs_setxattr(trans, inode, name, value, size, 0);
98 else
99 ret = btrfs_setxattr_trans(NULL, inode, name, value, size, 0);
100
Josef Bacik33268ea2008-07-24 12:16:36 -0400101out:
Chris Masond3977122009-01-05 21:25:51 -0500102 kfree(value);
Josef Bacik33268ea2008-07-24 12:16:36 -0400103
104 if (!ret)
Al Viro073aaa12009-06-09 12:11:54 -0400105 set_cached_acl(inode, type, acl);
Josef Bacik33268ea2008-07-24 12:16:36 -0400106
107 return ret;
108}
Yanfb4bc1e2008-01-17 11:59:51 -0500109
Christoph Hellwig996a7102013-12-20 05:16:43 -0800110int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
Yan744f52f2008-01-14 13:26:08 -0500111{
Jan Karab7f8a092017-06-22 15:31:07 +0200112 int ret;
Ernesto A. Fernándezd7d82492017-08-02 03:18:27 -0300113 umode_t old_mode = inode->i_mode;
Jan Karab7f8a092017-06-22 15:31:07 +0200114
115 if (type == ACL_TYPE_ACCESS && acl) {
116 ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
117 if (ret)
118 return ret;
119 }
Ernesto A. Fernándezd7d82492017-08-02 03:18:27 -0300120 ret = __btrfs_set_acl(NULL, inode, acl, type);
121 if (ret)
122 inode->i_mode = old_mode;
123 return ret;
Yan744f52f2008-01-14 13:26:08 -0500124}
Josef Bacik1caf9342007-11-19 10:18:17 -0500125
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000126int btrfs_init_acl(struct btrfs_trans_handle *trans,
127 struct inode *inode, struct inode *dir)
Josef Bacik33268ea2008-07-24 12:16:36 -0400128{
Christoph Hellwig996a7102013-12-20 05:16:43 -0800129 struct posix_acl *default_acl, *acl;
Josef Bacik33268ea2008-07-24 12:16:36 -0400130 int ret = 0;
131
132 /* this happens with subvols */
133 if (!dir)
134 return 0;
135
Christoph Hellwig996a7102013-12-20 05:16:43 -0800136 ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
Al Virobc26ab52011-07-23 00:18:02 -0400137 if (ret)
138 return ret;
Christoph Hellwig996a7102013-12-20 05:16:43 -0800139
140 if (default_acl) {
141 ret = __btrfs_set_acl(trans, inode, default_acl,
142 ACL_TYPE_DEFAULT);
143 posix_acl_release(default_acl);
144 }
145
146 if (acl) {
147 if (!ret)
148 ret = __btrfs_set_acl(trans, inode, acl,
149 ACL_TYPE_ACCESS);
150 posix_acl_release(acl);
151 }
152
153 if (!default_acl && !acl)
154 cache_no_acl(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -0400155 return ret;
156}