Blame view

fs/btrfs/acl.c 3.56 KB
5103e947b   Josef Bacik   xattr support for...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  /*
   * Copyright (C) 2007 Red Hat.  All rights reserved.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public
   * License v2 as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public
   * License along with this program; if not, write to the
   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   * Boston, MA 021110-1307, USA.
   */
  
  #include <linux/fs.h>
  #include <linux/string.h>
  #include <linux/xattr.h>
  #include <linux/posix_acl_xattr.h>
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
23
  #include <linux/posix_acl.h>
c1e32da61   Chris Mason   Btrfs: Include sc...
24
  #include <linux/sched.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
25
  #include <linux/slab.h>
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
26

5103e947b   Josef Bacik   xattr support for...
27
  #include "ctree.h"
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
28
  #include "btrfs_inode.h"
5103e947b   Josef Bacik   xattr support for...
29
  #include "xattr.h"
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
30

4e34e719e   Christoph Hellwig   fs: take the ACL ...
31
  struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
32
  {
95819c057   Christoph Hellwig   Btrfs: optimize b...
33
34
  	int size;
  	const char *name;
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
35
  	char *value = NULL;
073aaa1b1   Al Viro   helpers for acl c...
36
  	struct posix_acl *acl;
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
37
38
  	switch (type) {
  	case ACL_TYPE_ACCESS:
97d792992   Andreas Gruenbacher   posix acls: Remov...
39
  		name = XATTR_NAME_POSIX_ACL_ACCESS;
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
40
41
  		break;
  	case ACL_TYPE_DEFAULT:
97d792992   Andreas Gruenbacher   posix acls: Remov...
42
  		name = XATTR_NAME_POSIX_ACL_DEFAULT;
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
43
44
  		break;
  	default:
073aaa1b1   Al Viro   helpers for acl c...
45
  		BUG();
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
46
  	}
95819c057   Christoph Hellwig   Btrfs: optimize b...
47
  	size = __btrfs_getxattr(inode, name, "", 0);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
48
  	if (size > 0) {
39a27ec10   David Sterba   btrfs: use GFP_KE...
49
  		value = kzalloc(size, GFP_KERNEL);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
50
51
  		if (!value)
  			return ERR_PTR(-ENOMEM);
95819c057   Christoph Hellwig   Btrfs: optimize b...
52
  		size = __btrfs_getxattr(inode, name, value, size);
cfbffc39a   Tsutomu Itoh   Btrfs: fix return...
53
54
  	}
  	if (size > 0) {
5f3a4a28e   Eric W. Biederman   userns: Pass a us...
55
  		acl = posix_acl_from_xattr(&init_user_ns, value, size);
a60617d0a   Salah Triki   btrfs: Replace -E...
56
  	} else if (size == -ERANGE || size == -ENODATA || size == 0) {
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
57
  		acl = NULL;
7b1a14bbb   Chris Mason   Btrfs: fix acl ca...
58
59
  	} else {
  		acl = ERR_PTR(-EIO);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
60
  	}
cfbffc39a   Tsutomu Itoh   Btrfs: fix return...
61
  	kfree(value);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
62
63
  	return acl;
  }
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
64
65
66
  /*
   * Needs to be called with fs_mutex held
   */
996a710d4   Christoph Hellwig   btrfs: use generi...
67
  static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
f34f57a3a   Yan, Zheng   Btrfs: Pass trans...
68
  			 struct inode *inode, struct posix_acl *acl, int type)
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
69
  {
95819c057   Christoph Hellwig   Btrfs: optimize b...
70
71
  	int ret, size = 0;
  	const char *name;
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
72
  	char *value = NULL;
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
73

33268eaf0   Josef Bacik   Btrfs: Add ACL su...
74
75
  	switch (type) {
  	case ACL_TYPE_ACCESS:
97d792992   Andreas Gruenbacher   posix acls: Remov...
76
  		name = XATTR_NAME_POSIX_ACL_ACCESS;
a9cc71a60   Chris Mason   Btrfs: deal with ...
77
  		if (acl) {
073931017   Jan Kara   posix_acl: Clear ...
78
79
  			ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
  			if (ret)
a9cc71a60   Chris Mason   Btrfs: deal with ...
80
  				return ret;
a9cc71a60   Chris Mason   Btrfs: deal with ...
81
82
  		}
  		ret = 0;
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
83
84
85
86
  		break;
  	case ACL_TYPE_DEFAULT:
  		if (!S_ISDIR(inode->i_mode))
  			return acl ? -EINVAL : 0;
97d792992   Andreas Gruenbacher   posix acls: Remov...
87
  		name = XATTR_NAME_POSIX_ACL_DEFAULT;
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
88
89
90
91
92
93
94
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	if (acl) {
  		size = posix_acl_xattr_size(acl->a_count);
39a27ec10   David Sterba   btrfs: use GFP_KE...
95
  		value = kmalloc(size, GFP_KERNEL);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
96
97
98
99
  		if (!value) {
  			ret = -ENOMEM;
  			goto out;
  		}
5f3a4a28e   Eric W. Biederman   userns: Pass a us...
100
  		ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
101
102
103
  		if (ret < 0)
  			goto out;
  	}
f34f57a3a   Yan, Zheng   Btrfs: Pass trans...
104
  	ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
105
  out:
d397712bc   Chris Mason   Btrfs: Fix checkp...
106
  	kfree(value);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
107
108
  
  	if (!ret)
073aaa1b1   Al Viro   helpers for acl c...
109
  		set_cached_acl(inode, type, acl);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
110
111
112
  
  	return ret;
  }
fb4bc1e05   Yan   Btrfs: Fix compil...
113

996a710d4   Christoph Hellwig   btrfs: use generi...
114
  int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
744f52f99   Yan   Btrfs: Implement ...
115
  {
996a710d4   Christoph Hellwig   btrfs: use generi...
116
  	return __btrfs_set_acl(NULL, inode, acl, type);
744f52f99   Yan   Btrfs: Implement ...
117
  }
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
118

33268eaf0   Josef Bacik   Btrfs: Add ACL su...
119
120
121
122
123
  /*
   * btrfs_init_acl is already generally called under fs_mutex, so the locking
   * stuff has been fixed to work with that.  If the locking stuff changes, we
   * need to re-evaluate the acl locking stuff.
   */
f34f57a3a   Yan, Zheng   Btrfs: Pass trans...
124
125
  int btrfs_init_acl(struct btrfs_trans_handle *trans,
  		   struct inode *inode, struct inode *dir)
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
126
  {
996a710d4   Christoph Hellwig   btrfs: use generi...
127
  	struct posix_acl *default_acl, *acl;
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
128
129
130
131
132
  	int ret = 0;
  
  	/* this happens with subvols */
  	if (!dir)
  		return 0;
996a710d4   Christoph Hellwig   btrfs: use generi...
133
134
135
  	ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  	if (ret)
  		return ret;
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
136

996a710d4   Christoph Hellwig   btrfs: use generi...
137
138
139
140
  	if (default_acl) {
  		ret = __btrfs_set_acl(trans, inode, default_acl,
  				      ACL_TYPE_DEFAULT);
  		posix_acl_release(default_acl);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
141
  	}
996a710d4   Christoph Hellwig   btrfs: use generi...
142
143
144
145
146
  	if (acl) {
  		if (!ret)
  			ret = __btrfs_set_acl(trans, inode, acl,
  					      ACL_TYPE_ACCESS);
  		posix_acl_release(acl);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
147
  	}
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
148

996a710d4   Christoph Hellwig   btrfs: use generi...
149
150
  	if (!default_acl && !acl)
  		cache_no_acl(inode);
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
151
  	return ret;
5103e947b   Josef Bacik   xattr support for...
152
  }