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> |
Steven Whitehouse | 2646a1f | 2009-10-02 11:50:54 +0100 | [diff] [blame^] | 15 | #include <linux/xattr.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 16 | #include <linux/posix_acl.h> |
| 17 | #include <linux/posix_acl_xattr.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 18 | #include <linux/gfs2_ondisk.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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" |
Steven Whitehouse | 307cf6e | 2009-08-26 18:51:04 +0100 | [diff] [blame] | 23 | #include "xattr.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 24 | #include "glock.h" |
| 25 | #include "inode.h" |
| 26 | #include "meta_io.h" |
| 27 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 28 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 29 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 30 | static int acl_get(struct gfs2_inode *ip, const char *name, |
| 31 | struct posix_acl **acl, struct gfs2_ea_location *el, |
| 32 | char **datap, unsigned int *lenp) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 33 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 34 | char *data; |
| 35 | unsigned int len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 36 | int error; |
| 37 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 38 | el->el_bh = NULL; |
| 39 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 40 | if (!ip->i_eattr) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 41 | return 0; |
| 42 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 43 | error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, el); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 44 | if (error) |
| 45 | return error; |
| 46 | if (!el->el_ea) |
| 47 | return 0; |
| 48 | if (!GFS2_EA_DATA_LEN(el->el_ea)) |
| 49 | goto out; |
| 50 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 51 | len = GFS2_EA_DATA_LEN(el->el_ea); |
| 52 | data = kmalloc(len, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 53 | error = -ENOMEM; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 54 | if (!data) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 55 | goto out; |
| 56 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 57 | error = gfs2_ea_get_copy(ip, el, data, len); |
| 58 | if (error < 0) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 59 | goto out_kfree; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 60 | error = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 61 | |
| 62 | if (acl) { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 63 | *acl = posix_acl_from_xattr(data, len); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 64 | if (IS_ERR(*acl)) |
| 65 | error = PTR_ERR(*acl); |
| 66 | } |
| 67 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 68 | out_kfree: |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 69 | if (error || !datap) { |
| 70 | kfree(data); |
| 71 | } else { |
| 72 | *datap = data; |
| 73 | *lenp = len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 74 | } |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 75 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 76 | return error; |
| 77 | } |
| 78 | |
| 79 | /** |
Steven Whitehouse | 77386e1 | 2006-11-29 10:41:49 -0500 | [diff] [blame] | 80 | * gfs2_check_acl - Check an ACL to see if we're allowed to do something |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 81 | * @inode: the file we want to do something to |
| 82 | * @mask: what we want to do |
| 83 | * |
| 84 | * Returns: errno |
| 85 | */ |
| 86 | |
Steven Whitehouse | 77386e1 | 2006-11-29 10:41:49 -0500 | [diff] [blame] | 87 | int gfs2_check_acl(struct inode *inode, int mask) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 88 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 89 | struct gfs2_ea_location el; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 90 | struct posix_acl *acl = NULL; |
| 91 | int error; |
| 92 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 93 | error = acl_get(GFS2_I(inode), GFS2_POSIX_ACL_ACCESS, &acl, &el, NULL, NULL); |
| 94 | brelse(el.el_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 95 | if (error) |
| 96 | return error; |
| 97 | |
| 98 | if (acl) { |
| 99 | error = posix_acl_permission(inode, acl, mask); |
| 100 | posix_acl_release(acl); |
| 101 | return error; |
| 102 | } |
| 103 | |
| 104 | return -EAGAIN; |
| 105 | } |
| 106 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 107 | static int munge_mode(struct gfs2_inode *ip, mode_t mode) |
| 108 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 109 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 110 | struct buffer_head *dibh; |
| 111 | int error; |
| 112 | |
| 113 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); |
| 114 | if (error) |
| 115 | return error; |
| 116 | |
| 117 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 118 | if (!error) { |
| 119 | gfs2_assert_withdraw(sdp, |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 120 | (ip->i_inode.i_mode & S_IFMT) == (mode & S_IFMT)); |
| 121 | ip->i_inode.i_mode = mode; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 122 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 123 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 124 | brelse(dibh); |
| 125 | } |
| 126 | |
| 127 | gfs2_trans_end(sdp); |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | int gfs2_acl_create(struct gfs2_inode *dip, struct gfs2_inode *ip) |
| 133 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 134 | struct gfs2_ea_location el; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 135 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 136 | struct posix_acl *acl = NULL, *clone; |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 137 | mode_t mode = ip->i_inode.i_mode; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 138 | char *data = NULL; |
| 139 | unsigned int len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 140 | int error; |
| 141 | |
| 142 | if (!sdp->sd_args.ar_posix_acl) |
| 143 | return 0; |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 144 | if (S_ISLNK(ip->i_inode.i_mode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 145 | return 0; |
| 146 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 147 | error = acl_get(dip, GFS2_POSIX_ACL_DEFAULT, &acl, &el, &data, &len); |
| 148 | brelse(el.el_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 149 | if (error) |
| 150 | return error; |
| 151 | if (!acl) { |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 152 | mode &= ~current_umask(); |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 153 | if (mode != ip->i_inode.i_mode) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 154 | error = munge_mode(ip, mode); |
| 155 | return error; |
| 156 | } |
| 157 | |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 158 | clone = posix_acl_clone(acl, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 159 | error = -ENOMEM; |
| 160 | if (!clone) |
| 161 | goto out; |
| 162 | posix_acl_release(acl); |
| 163 | acl = clone; |
| 164 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 165 | if (S_ISDIR(ip->i_inode.i_mode)) { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 166 | error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS, |
| 167 | GFS2_POSIX_ACL_DEFAULT, data, len, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 168 | if (error) |
| 169 | goto out; |
| 170 | } |
| 171 | |
| 172 | error = posix_acl_create_masq(acl, &mode); |
| 173 | if (error < 0) |
| 174 | goto out; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 175 | if (error == 0) |
| 176 | goto munge; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 177 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 178 | posix_acl_to_xattr(acl, data, len); |
| 179 | error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS, |
| 180 | GFS2_POSIX_ACL_ACCESS, data, len, 0); |
| 181 | if (error) |
| 182 | goto out; |
| 183 | munge: |
| 184 | error = munge_mode(ip, mode); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 185 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 186 | posix_acl_release(acl); |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 187 | kfree(data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 188 | return error; |
| 189 | } |
| 190 | |
| 191 | int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr) |
| 192 | { |
| 193 | struct posix_acl *acl = NULL, *clone; |
| 194 | struct gfs2_ea_location el; |
| 195 | char *data; |
| 196 | unsigned int len; |
| 197 | int error; |
| 198 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 199 | error = acl_get(ip, GFS2_POSIX_ACL_ACCESS, &acl, &el, &data, &len); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 200 | if (error) |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 201 | goto out_brelse; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 202 | if (!acl) |
| 203 | return gfs2_setattr_simple(ip, attr); |
| 204 | |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 205 | clone = posix_acl_clone(acl, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 206 | error = -ENOMEM; |
| 207 | if (!clone) |
| 208 | goto out; |
| 209 | posix_acl_release(acl); |
| 210 | acl = clone; |
| 211 | |
| 212 | error = posix_acl_chmod_masq(acl, attr->ia_mode); |
| 213 | if (!error) { |
| 214 | posix_acl_to_xattr(acl, data, len); |
| 215 | error = gfs2_ea_acl_chmod(ip, &el, attr, data); |
| 216 | } |
| 217 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 218 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 219 | posix_acl_release(acl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 220 | kfree(data); |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 221 | out_brelse: |
| 222 | brelse(el.el_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 223 | return error; |
| 224 | } |
| 225 | |
Steven Whitehouse | 2646a1f | 2009-10-02 11:50:54 +0100 | [diff] [blame^] | 226 | static int gfs2_acl_type(const char *name) |
| 227 | { |
| 228 | if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0) |
| 229 | return ACL_TYPE_ACCESS; |
| 230 | if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0) |
| 231 | return ACL_TYPE_DEFAULT; |
| 232 | return -EINVAL; |
| 233 | } |
| 234 | |
| 235 | static int gfs2_xattr_system_get(struct inode *inode, const char *name, |
| 236 | void *buffer, size_t size) |
| 237 | { |
| 238 | int type; |
| 239 | |
| 240 | type = gfs2_acl_type(name); |
| 241 | if (type < 0) |
| 242 | return type; |
| 243 | |
| 244 | return gfs2_xattr_get(inode, GFS2_EATYPE_SYS, name, buffer, size); |
| 245 | } |
| 246 | |
| 247 | static int gfs2_set_mode(struct inode *inode, mode_t mode) |
| 248 | { |
| 249 | int error = 0; |
| 250 | |
| 251 | if (mode != inode->i_mode) { |
| 252 | struct iattr iattr; |
| 253 | |
| 254 | iattr.ia_valid = ATTR_MODE; |
| 255 | iattr.ia_mode = mode; |
| 256 | |
| 257 | error = gfs2_setattr_simple(GFS2_I(inode), &iattr); |
| 258 | } |
| 259 | |
| 260 | return error; |
| 261 | } |
| 262 | |
| 263 | static int gfs2_xattr_system_set(struct inode *inode, const char *name, |
| 264 | const void *value, size_t size, int flags) |
| 265 | { |
| 266 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 267 | struct posix_acl *acl = NULL; |
| 268 | int error = 0, type; |
| 269 | |
| 270 | if (!sdp->sd_args.ar_posix_acl) |
| 271 | return -EOPNOTSUPP; |
| 272 | |
| 273 | type = gfs2_acl_type(name); |
| 274 | if (type < 0) |
| 275 | return type; |
| 276 | if (flags & XATTR_CREATE) |
| 277 | return -EINVAL; |
| 278 | if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) |
| 279 | return value ? -EACCES : 0; |
| 280 | if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER)) |
| 281 | return -EPERM; |
| 282 | if (S_ISLNK(inode->i_mode)) |
| 283 | return -EOPNOTSUPP; |
| 284 | |
| 285 | if (!value) |
| 286 | goto set_acl; |
| 287 | |
| 288 | acl = posix_acl_from_xattr(value, size); |
| 289 | if (!acl) { |
| 290 | /* |
| 291 | * acl_set_file(3) may request that we set default ACLs with |
| 292 | * zero length -- defend (gracefully) against that here. |
| 293 | */ |
| 294 | goto out; |
| 295 | } |
| 296 | if (IS_ERR(acl)) { |
| 297 | error = PTR_ERR(acl); |
| 298 | goto out; |
| 299 | } |
| 300 | |
| 301 | error = posix_acl_valid(acl); |
| 302 | if (error) |
| 303 | goto out_release; |
| 304 | |
| 305 | error = -EINVAL; |
| 306 | if (acl->a_count > GFS2_ACL_MAX_ENTRIES) |
| 307 | goto out_release; |
| 308 | |
| 309 | if (type == ACL_TYPE_ACCESS) { |
| 310 | mode_t mode = inode->i_mode; |
| 311 | error = posix_acl_equiv_mode(acl, &mode); |
| 312 | |
| 313 | if (error <= 0) { |
| 314 | posix_acl_release(acl); |
| 315 | acl = NULL; |
| 316 | |
| 317 | if (error < 0) |
| 318 | return error; |
| 319 | } |
| 320 | |
| 321 | error = gfs2_set_mode(inode, mode); |
| 322 | if (error) |
| 323 | goto out_release; |
| 324 | } |
| 325 | |
| 326 | set_acl: |
| 327 | error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, value, size, 0); |
| 328 | out_release: |
| 329 | posix_acl_release(acl); |
| 330 | out: |
| 331 | return error; |
| 332 | } |
| 333 | |
| 334 | struct xattr_handler gfs2_xattr_system_handler = { |
| 335 | .prefix = XATTR_SYSTEM_PREFIX, |
| 336 | .get = gfs2_xattr_system_get, |
| 337 | .set = gfs2_xattr_system_set, |
| 338 | }; |
| 339 | |