GFS2: eliminate log elements and simplify
[deliverable/linux.git] / fs / gfs2 / acl.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
b3b94faa
DT
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
2646a1f6 15#include <linux/xattr.h>
b3b94faa
DT
16#include <linux/posix_acl.h>
17#include <linux/posix_acl_xattr.h>
5c676f6d 18#include <linux/gfs2_ondisk.h>
b3b94faa
DT
19
20#include "gfs2.h"
5c676f6d 21#include "incore.h"
b3b94faa 22#include "acl.h"
307cf6e6 23#include "xattr.h"
b3b94faa
DT
24#include "glock.h"
25#include "inode.h"
26#include "meta_io.h"
27#include "trans.h"
5c676f6d 28#include "util.h"
b3b94faa 29
479c427d 30static const char *gfs2_acl_name(int type)
b3b94faa 31{
479c427d
SW
32 switch (type) {
33 case ACL_TYPE_ACCESS:
34 return GFS2_POSIX_ACL_ACCESS;
35 case ACL_TYPE_DEFAULT:
36 return GFS2_POSIX_ACL_DEFAULT;
37 }
38 return NULL;
39}
b3b94faa 40
018a01cd 41struct posix_acl *gfs2_get_acl(struct inode *inode, int type)
479c427d 42{
018a01cd 43 struct gfs2_inode *ip = GFS2_I(inode);
479c427d
SW
44 struct posix_acl *acl;
45 const char *name;
46 char *data;
47 int len;
40b78a32 48
3767ac21 49 if (!ip->i_eattr)
479c427d 50 return NULL;
b3b94faa 51
106381bf
SW
52 acl = get_cached_acl(&ip->i_inode, type);
53 if (acl != ACL_NOT_CACHED)
54 return acl;
55
479c427d
SW
56 name = gfs2_acl_name(type);
57 if (name == NULL)
58 return ERR_PTR(-EINVAL);
b3b94faa 59
479c427d
SW
60 len = gfs2_xattr_acl_get(ip, name, &data);
61 if (len < 0)
62 return ERR_PTR(len);
63 if (len == 0)
64 return NULL;
b3b94faa 65
479c427d
SW
66 acl = posix_acl_from_xattr(data, len);
67 kfree(data);
68 return acl;
b3b94faa
DT
69}
70
d3fb6120 71static int gfs2_set_mode(struct inode *inode, umode_t mode)
b3b94faa 72{
69dca424 73 int error = 0;
b3b94faa 74
69dca424
SW
75 if (mode != inode->i_mode) {
76 struct iattr iattr;
b3b94faa 77
69dca424
SW
78 iattr.ia_valid = ATTR_MODE;
79 iattr.ia_mode = mode;
b3b94faa 80
ab9bbda0 81 error = gfs2_setattr_simple(inode, &iattr);
69dca424 82 }
b3b94faa 83
69dca424 84 return error;
b3b94faa
DT
85}
86
479c427d 87static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl)
b3b94faa 88{
b3b94faa 89 int error;
479c427d
SW
90 int len;
91 char *data;
92 const char *name = gfs2_acl_name(type);
93
94 BUG_ON(name == NULL);
95 len = posix_acl_to_xattr(acl, NULL, 0);
96 if (len == 0)
97 return 0;
98 data = kmalloc(len, GFP_NOFS);
99 if (data == NULL)
100 return -ENOMEM;
101 error = posix_acl_to_xattr(acl, data, len);
102 if (error < 0)
103 goto out;
431547b3 104 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
106381bf
SW
105 if (!error)
106 set_cached_acl(inode, type, acl);
479c427d
SW
107out:
108 kfree(data);
109 return error;
110}
111
112int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode)
113{
114 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
826cae2f 115 struct posix_acl *acl;
d3fb6120 116 umode_t mode = inode->i_mode;
479c427d 117 int error = 0;
b3b94faa
DT
118
119 if (!sdp->sd_args.ar_posix_acl)
120 return 0;
479c427d 121 if (S_ISLNK(inode->i_mode))
b3b94faa
DT
122 return 0;
123
018a01cd 124 acl = gfs2_get_acl(&dip->i_inode, ACL_TYPE_DEFAULT);
479c427d
SW
125 if (IS_ERR(acl))
126 return PTR_ERR(acl);
b3b94faa 127 if (!acl) {
ce3b0f8d 128 mode &= ~current_umask();
479c427d
SW
129 if (mode != inode->i_mode)
130 error = gfs2_set_mode(inode, mode);
b3b94faa
DT
131 return error;
132 }
133
479c427d
SW
134 if (S_ISDIR(inode->i_mode)) {
135 error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl);
136 if (error)
137 goto out;
138 }
139
826cae2f 140 error = posix_acl_create(&acl, GFP_NOFS, &mode);
b3b94faa 141 if (error < 0)
826cae2f
AV
142 return error;
143
40b78a32
SW
144 if (error == 0)
145 goto munge;
b3b94faa 146
479c427d 147 error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl);
40b78a32
SW
148 if (error)
149 goto out;
150munge:
479c427d 151 error = gfs2_set_mode(inode, mode);
a91ea69f 152out:
b3b94faa 153 posix_acl_release(acl);
b3b94faa
DT
154 return error;
155}
156
157int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
158{
ab9bbda0 159 struct inode *inode = &ip->i_inode;
bc26ab5f 160 struct posix_acl *acl;
b3b94faa
DT
161 char *data;
162 unsigned int len;
163 int error;
164
018a01cd 165 acl = gfs2_get_acl(&ip->i_inode, ACL_TYPE_ACCESS);
479c427d
SW
166 if (IS_ERR(acl))
167 return PTR_ERR(acl);
b3b94faa 168 if (!acl)
ab9bbda0 169 return gfs2_setattr_simple(inode, attr);
b3b94faa 170
bc26ab5f
AV
171 error = posix_acl_chmod(&acl, GFP_NOFS, attr->ia_mode);
172 if (error)
173 return error;
174
175 len = posix_acl_to_xattr(acl, NULL, 0);
176 data = kmalloc(len, GFP_NOFS);
b3b94faa 177 error = -ENOMEM;
bc26ab5f 178 if (data == NULL)
b3b94faa 179 goto out;
bc26ab5f
AV
180 posix_acl_to_xattr(acl, data, len);
181 error = gfs2_xattr_acl_chmod(ip, attr, data);
182 kfree(data);
183 set_cached_acl(&ip->i_inode, ACL_TYPE_ACCESS, acl);
b3b94faa 184
a91ea69f 185out:
b3b94faa 186 posix_acl_release(acl);
b3b94faa
DT
187 return error;
188}
189
2646a1f6
SW
190static int gfs2_acl_type(const char *name)
191{
192 if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
193 return ACL_TYPE_ACCESS;
194 if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
195 return ACL_TYPE_DEFAULT;
196 return -EINVAL;
197}
198
431547b3
CH
199static int gfs2_xattr_system_get(struct dentry *dentry, const char *name,
200 void *buffer, size_t size, int xtype)
2646a1f6 201{
431547b3 202 struct inode *inode = dentry->d_inode;
f72f2d2e 203 struct gfs2_sbd *sdp = GFS2_SB(inode);
106381bf 204 struct posix_acl *acl;
2646a1f6 205 int type;
106381bf 206 int error;
2646a1f6 207
f72f2d2e
SW
208 if (!sdp->sd_args.ar_posix_acl)
209 return -EOPNOTSUPP;
210
2646a1f6
SW
211 type = gfs2_acl_type(name);
212 if (type < 0)
213 return type;
214
018a01cd 215 acl = gfs2_get_acl(inode, type);
106381bf
SW
216 if (IS_ERR(acl))
217 return PTR_ERR(acl);
218 if (acl == NULL)
219 return -ENODATA;
2646a1f6 220
106381bf
SW
221 error = posix_acl_to_xattr(acl, buffer, size);
222 posix_acl_release(acl);
223
224 return error;
225}
2646a1f6 226
431547b3
CH
227static int gfs2_xattr_system_set(struct dentry *dentry, const char *name,
228 const void *value, size_t size, int flags,
229 int xtype)
2646a1f6 230{
431547b3 231 struct inode *inode = dentry->d_inode;
2646a1f6
SW
232 struct gfs2_sbd *sdp = GFS2_SB(inode);
233 struct posix_acl *acl = NULL;
234 int error = 0, type;
235
236 if (!sdp->sd_args.ar_posix_acl)
237 return -EOPNOTSUPP;
238
239 type = gfs2_acl_type(name);
240 if (type < 0)
241 return type;
242 if (flags & XATTR_CREATE)
243 return -EINVAL;
244 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
245 return value ? -EACCES : 0;
246 if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
247 return -EPERM;
248 if (S_ISLNK(inode->i_mode))
249 return -EOPNOTSUPP;
250
251 if (!value)
252 goto set_acl;
253
254 acl = posix_acl_from_xattr(value, size);
255 if (!acl) {
256 /*
257 * acl_set_file(3) may request that we set default ACLs with
258 * zero length -- defend (gracefully) against that here.
259 */
260 goto out;
261 }
262 if (IS_ERR(acl)) {
263 error = PTR_ERR(acl);
264 goto out;
265 }
266
267 error = posix_acl_valid(acl);
268 if (error)
269 goto out_release;
270
271 error = -EINVAL;
272 if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
273 goto out_release;
274
275 if (type == ACL_TYPE_ACCESS) {
d6952123 276 umode_t mode = inode->i_mode;
2646a1f6
SW
277 error = posix_acl_equiv_mode(acl, &mode);
278
279 if (error <= 0) {
280 posix_acl_release(acl);
281 acl = NULL;
282
283 if (error < 0)
284 return error;
285 }
286
287 error = gfs2_set_mode(inode, mode);
288 if (error)
289 goto out_release;
290 }
291
292set_acl:
431547b3 293 error = __gfs2_xattr_set(inode, name, value, size, 0, GFS2_EATYPE_SYS);
106381bf
SW
294 if (!error) {
295 if (acl)
296 set_cached_acl(inode, type, acl);
297 else
298 forget_cached_acl(inode, type);
299 }
2646a1f6
SW
300out_release:
301 posix_acl_release(acl);
302out:
303 return error;
304}
305
b7bb0a12 306const struct xattr_handler gfs2_xattr_system_handler = {
2646a1f6 307 .prefix = XATTR_SYSTEM_PREFIX,
431547b3 308 .flags = GFS2_EATYPE_SYS,
2646a1f6
SW
309 .get = gfs2_xattr_system_get,
310 .set = gfs2_xattr_system_set,
311};
312
This page took 0.449728 seconds and 5 git commands to generate.