David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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> |
| 15 | #include <linux/xattr.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 16 | #include <linux/gfs2_ondisk.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 17 | #include <linux/lm_interface.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 18 | #include <asm/uaccess.h> |
| 19 | |
| 20 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 21 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 22 | #include "acl.h" |
| 23 | #include "eaops.h" |
| 24 | #include "eattr.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 25 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 34 | unsigned int gfs2_ea_name2type(const char *name, const char **truncated_name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 35 | { |
| 36 | unsigned int type; |
| 37 | |
| 38 | if (strncmp(name, "system.", 7) == 0) { |
| 39 | type = GFS2_EATYPE_SYS; |
| 40 | if (truncated_name) |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 41 | *truncated_name = name + sizeof("system.") - 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 42 | } else if (strncmp(name, "user.", 5) == 0) { |
| 43 | type = GFS2_EATYPE_USR; |
| 44 | if (truncated_name) |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 45 | *truncated_name = name + sizeof("user.") - 1; |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 46 | } else if (strncmp(name, "security.", 9) == 0) { |
| 47 | type = GFS2_EATYPE_SECURITY; |
| 48 | if (truncated_name) |
Steven Whitehouse | cca195c | 2006-09-05 13:15:18 -0400 | [diff] [blame] | 49 | *truncated_name = name + sizeof("security.") - 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 61 | struct inode *inode = &ip->i_inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 62 | int error = permission(inode, MAY_READ, NULL); |
| 63 | if (error) |
| 64 | return error; |
| 65 | |
| 66 | return gfs2_ea_get_i(ip, er); |
| 67 | } |
| 68 | |
| 69 | static int user_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 70 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 71 | struct inode *inode = &ip->i_inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 72 | |
| 73 | if (S_ISREG(inode->i_mode) || |
| 74 | (S_ISDIR(inode->i_mode) && !(inode->i_mode & S_ISVTX))) { |
| 75 | int error = permission(inode, MAY_WRITE, NULL); |
| 76 | if (error) |
| 77 | return error; |
| 78 | } else |
| 79 | return -EPERM; |
| 80 | |
| 81 | return gfs2_ea_set_i(ip, er); |
| 82 | } |
| 83 | |
| 84 | static int user_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 85 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 86 | struct inode *inode = &ip->i_inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 87 | |
| 88 | if (S_ISREG(inode->i_mode) || |
| 89 | (S_ISDIR(inode->i_mode) && !(inode->i_mode & S_ISVTX))) { |
| 90 | int error = permission(inode, MAY_WRITE, NULL); |
| 91 | if (error) |
| 92 | return error; |
| 93 | } else |
| 94 | return -EPERM; |
| 95 | |
| 96 | return gfs2_ea_remove_i(ip, er); |
| 97 | } |
| 98 | |
| 99 | static int system_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 100 | { |
| 101 | if (!GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) && |
| 102 | !GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len) && |
| 103 | !capable(CAP_SYS_ADMIN)) |
| 104 | return -EPERM; |
| 105 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 106 | if (GFS2_SB(&ip->i_inode)->sd_args.ar_posix_acl == 0 && |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 107 | (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) || |
| 108 | GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len))) |
| 109 | return -EOPNOTSUPP; |
| 110 | |
| 111 | |
| 112 | |
| 113 | return gfs2_ea_get_i(ip, er); |
| 114 | } |
| 115 | |
| 116 | static int system_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 117 | { |
| 118 | int remove = 0; |
| 119 | int error; |
| 120 | |
| 121 | if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) { |
| 122 | if (!(er->er_flags & GFS2_ERF_MODE)) { |
| 123 | er->er_mode = ip->i_di.di_mode; |
| 124 | er->er_flags |= GFS2_ERF_MODE; |
| 125 | } |
| 126 | error = gfs2_acl_validate_set(ip, 1, er, |
| 127 | &remove, &er->er_mode); |
| 128 | if (error) |
| 129 | return error; |
| 130 | error = gfs2_ea_set_i(ip, er); |
| 131 | if (error) |
| 132 | return error; |
| 133 | if (remove) |
| 134 | gfs2_ea_remove_i(ip, er); |
| 135 | return 0; |
| 136 | |
| 137 | } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) { |
| 138 | error = gfs2_acl_validate_set(ip, 0, er, |
| 139 | &remove, NULL); |
| 140 | if (error) |
| 141 | return error; |
| 142 | if (!remove) |
| 143 | error = gfs2_ea_set_i(ip, er); |
| 144 | else { |
| 145 | error = gfs2_ea_remove_i(ip, er); |
| 146 | if (error == -ENODATA) |
| 147 | error = 0; |
| 148 | } |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 149 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return -EPERM; |
| 153 | } |
| 154 | |
| 155 | static int system_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 156 | { |
| 157 | if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) { |
| 158 | int error = gfs2_acl_validate_remove(ip, 1); |
| 159 | if (error) |
| 160 | return error; |
| 161 | |
| 162 | } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) { |
| 163 | int error = gfs2_acl_validate_remove(ip, 0); |
| 164 | if (error) |
| 165 | return error; |
| 166 | |
| 167 | } else |
| 168 | return -EPERM; |
| 169 | |
| 170 | return gfs2_ea_remove_i(ip, er); |
| 171 | } |
| 172 | |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 173 | static int security_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 174 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 175 | struct inode *inode = &ip->i_inode; |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 176 | int error = permission(inode, MAY_READ, NULL); |
| 177 | if (error) |
| 178 | return error; |
| 179 | |
| 180 | return gfs2_ea_get_i(ip, er); |
| 181 | } |
| 182 | |
| 183 | static int security_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 184 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 185 | struct inode *inode = &ip->i_inode; |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 186 | int error = permission(inode, MAY_WRITE, NULL); |
| 187 | if (error) |
| 188 | return error; |
| 189 | |
| 190 | return gfs2_ea_set_i(ip, er); |
| 191 | } |
| 192 | |
| 193 | static int security_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er) |
| 194 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 195 | struct inode *inode = &ip->i_inode; |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 196 | int error = permission(inode, MAY_WRITE, NULL); |
| 197 | if (error) |
| 198 | return error; |
| 199 | |
| 200 | return gfs2_ea_remove_i(ip, er); |
| 201 | } |
| 202 | |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 203 | static struct gfs2_eattr_operations gfs2_user_eaops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 204 | .eo_get = user_eo_get, |
| 205 | .eo_set = user_eo_set, |
| 206 | .eo_remove = user_eo_remove, |
| 207 | .eo_name = "user", |
| 208 | }; |
| 209 | |
| 210 | struct gfs2_eattr_operations gfs2_system_eaops = { |
| 211 | .eo_get = system_eo_get, |
| 212 | .eo_set = system_eo_set, |
| 213 | .eo_remove = system_eo_remove, |
| 214 | .eo_name = "system", |
| 215 | }; |
| 216 | |
Adrian Bunk | 43f5d21 | 2006-06-22 11:16:40 -0400 | [diff] [blame] | 217 | static struct gfs2_eattr_operations gfs2_security_eaops = { |
Ryan O'Hara | 639b6d7 | 2006-05-22 10:08:35 -0400 | [diff] [blame] | 218 | .eo_get = security_eo_get, |
| 219 | .eo_set = security_eo_set, |
| 220 | .eo_remove = security_eo_remove, |
| 221 | .eo_name = "security", |
| 222 | }; |
| 223 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 224 | struct gfs2_eattr_operations *gfs2_ea_ops[] = { |
| 225 | NULL, |
| 226 | &gfs2_user_eaops, |
| 227 | &gfs2_system_eaops, |
Ryan O'Hara | e70409f | 2006-05-25 17:36:15 -0400 | [diff] [blame] | 228 | &gfs2_security_eaops, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 229 | }; |
| 230 | |