[GFS2] remove unnecessary permission checks
[safe/jmp/linux-2.6] / fs / gfs2 / eaops.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
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
7  * of the GNU General Public License version 2.
8  */
9
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/capability.h>
15 #include <linux/xattr.h>
16 #include <linux/gfs2_ondisk.h>
17 #include <linux/lm_interface.h>
18 #include <asm/uaccess.h>
19
20 #include "gfs2.h"
21 #include "incore.h"
22 #include "acl.h"
23 #include "eaops.h"
24 #include "eattr.h"
25 #include "util.h"
26
27 /**
28  * gfs2_ea_name2type - get the type of the ea, and truncate type from the name
29  * @namep: ea name, possibly with type appended
30  *
31  * Returns: GFS2_EATYPE_XXX
32  */
33
34 unsigned int gfs2_ea_name2type(const char *name, const char **truncated_name)
35 {
36         unsigned int type;
37
38         if (strncmp(name, "system.", 7) == 0) {
39                 type = GFS2_EATYPE_SYS;
40                 if (truncated_name)
41                         *truncated_name = name + sizeof("system.") - 1;
42         } else if (strncmp(name, "user.", 5) == 0) {
43                 type = GFS2_EATYPE_USR;
44                 if (truncated_name)
45                         *truncated_name = name + sizeof("user.") - 1;
46         } else if (strncmp(name, "security.", 9) == 0) {
47                 type = GFS2_EATYPE_SECURITY;
48                 if (truncated_name)
49                         *truncated_name = name + sizeof("security.") - 1;
50         } else {
51                 type = GFS2_EATYPE_UNUSED;
52                 if (truncated_name)
53                         *truncated_name = NULL;
54         }
55
56         return type;
57 }
58
59 static int user_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
60 {
61         struct inode *inode = &ip->i_inode;
62
63         return gfs2_ea_get_i(ip, er);
64 }
65
66 static int user_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
67 {
68         struct inode *inode = &ip->i_inode;
69
70         return gfs2_ea_set_i(ip, er);
71 }
72
73 static int user_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
74 {
75         struct inode *inode = &ip->i_inode;
76
77         return gfs2_ea_remove_i(ip, er);
78 }
79
80 static int system_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
81 {
82         if (!GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) &&
83             !GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len) &&
84             !capable(CAP_SYS_ADMIN))
85                 return -EPERM;
86
87         if (GFS2_SB(&ip->i_inode)->sd_args.ar_posix_acl == 0 &&
88             (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) ||
89              GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)))
90                 return -EOPNOTSUPP;
91
92         return gfs2_ea_get_i(ip, er);
93 }
94
95 static int system_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
96 {
97         int remove = 0;
98         int error;
99
100         if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) {
101                 if (!(er->er_flags & GFS2_ERF_MODE)) {
102                         er->er_mode = ip->i_inode.i_mode;
103                         er->er_flags |= GFS2_ERF_MODE;
104                 }
105                 error = gfs2_acl_validate_set(ip, 1, er,
106                                               &remove, &er->er_mode);
107                 if (error)
108                         return error;
109                 error = gfs2_ea_set_i(ip, er);
110                 if (error)
111                         return error;
112                 if (remove)
113                         gfs2_ea_remove_i(ip, er);
114                 return 0;
115
116         } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) {
117                 error = gfs2_acl_validate_set(ip, 0, er,
118                                               &remove, NULL);
119                 if (error)
120                         return error;
121                 if (!remove)
122                         error = gfs2_ea_set_i(ip, er);
123                 else {
124                         error = gfs2_ea_remove_i(ip, er);
125                         if (error == -ENODATA)
126                                 error = 0;
127                 }
128                 return error;
129         }
130
131         return -EPERM;
132 }
133
134 static int system_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
135 {
136         if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) {
137                 int error = gfs2_acl_validate_remove(ip, 1);
138                 if (error)
139                         return error;
140
141         } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) {
142                 int error = gfs2_acl_validate_remove(ip, 0);
143                 if (error)
144                         return error;
145
146         } else
147                 return -EPERM;
148
149         return gfs2_ea_remove_i(ip, er);
150 }
151
152 static int security_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
153 {
154         struct inode *inode = &ip->i_inode;
155
156         return gfs2_ea_get_i(ip, er);
157 }
158
159 static int security_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
160 {
161         struct inode *inode = &ip->i_inode;
162
163         return gfs2_ea_set_i(ip, er);
164 }
165
166 static int security_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
167 {
168         struct inode *inode = &ip->i_inode;
169
170         return gfs2_ea_remove_i(ip, er);
171 }
172
173 static const struct gfs2_eattr_operations gfs2_user_eaops = {
174         .eo_get = user_eo_get,
175         .eo_set = user_eo_set,
176         .eo_remove = user_eo_remove,
177         .eo_name = "user",
178 };
179
180 const struct gfs2_eattr_operations gfs2_system_eaops = {
181         .eo_get = system_eo_get,
182         .eo_set = system_eo_set,
183         .eo_remove = system_eo_remove,
184         .eo_name = "system",
185 };
186
187 static const struct gfs2_eattr_operations gfs2_security_eaops = {
188         .eo_get = security_eo_get,
189         .eo_set = security_eo_set,
190         .eo_remove = security_eo_remove,
191         .eo_name = "security",
192 };
193
194 const struct gfs2_eattr_operations *gfs2_ea_ops[] = {
195         NULL,
196         &gfs2_user_eaops,
197         &gfs2_system_eaops,
198         &gfs2_security_eaops,
199 };
200