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/posix_acl.h> |
| 16 | #include <linux/posix_acl_xattr.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 17 | #include <linux/gfs2_ondisk.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 18 | |
| 19 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 20 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 21 | #include "acl.h" |
Steven Whitehouse | 307cf6e | 2009-08-26 18:51:04 +0100 | [diff] [blame] | 22 | #include "xattr.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 23 | #include "glock.h" |
| 24 | #include "inode.h" |
| 25 | #include "meta_io.h" |
| 26 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 27 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 28 | |
| 29 | #define ACL_ACCESS 1 |
| 30 | #define ACL_DEFAULT 0 |
| 31 | |
| 32 | int gfs2_acl_validate_set(struct gfs2_inode *ip, int access, |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 33 | struct gfs2_ea_request *er, int *remove, mode_t *mode) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 34 | { |
| 35 | struct posix_acl *acl; |
| 36 | int error; |
| 37 | |
| 38 | error = gfs2_acl_validate_remove(ip, access); |
| 39 | if (error) |
| 40 | return error; |
| 41 | |
| 42 | if (!er->er_data) |
| 43 | return -EINVAL; |
| 44 | |
| 45 | acl = posix_acl_from_xattr(er->er_data, er->er_data_len); |
| 46 | if (IS_ERR(acl)) |
| 47 | return PTR_ERR(acl); |
| 48 | if (!acl) { |
| 49 | *remove = 1; |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | error = posix_acl_valid(acl); |
| 54 | if (error) |
| 55 | goto out; |
| 56 | |
| 57 | if (access) { |
| 58 | error = posix_acl_equiv_mode(acl, mode); |
| 59 | if (!error) |
| 60 | *remove = 1; |
| 61 | else if (error > 0) |
| 62 | error = 0; |
| 63 | } |
| 64 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 65 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 66 | posix_acl_release(acl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 67 | return error; |
| 68 | } |
| 69 | |
| 70 | int gfs2_acl_validate_remove(struct gfs2_inode *ip, int access) |
| 71 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 72 | if (!GFS2_SB(&ip->i_inode)->sd_args.ar_posix_acl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 73 | return -EOPNOTSUPP; |
Satyam Sharma | 3bd858a | 2007-07-17 15:00:08 +0530 | [diff] [blame] | 74 | if (!is_owner_or_cap(&ip->i_inode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 75 | return -EPERM; |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 76 | if (S_ISLNK(ip->i_inode.i_mode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 77 | return -EOPNOTSUPP; |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 78 | if (!access && !S_ISDIR(ip->i_inode.i_mode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 79 | return -EACCES; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 84 | static int acl_get(struct gfs2_inode *ip, const char *name, |
| 85 | struct posix_acl **acl, struct gfs2_ea_location *el, |
| 86 | char **datap, unsigned int *lenp) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 87 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 88 | char *data; |
| 89 | unsigned int len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 90 | int error; |
| 91 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 92 | el->el_bh = NULL; |
| 93 | |
Steven Whitehouse | 3767ac2 | 2008-11-03 14:28:42 +0000 | [diff] [blame] | 94 | if (!ip->i_eattr) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 95 | return 0; |
| 96 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 97 | error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, el); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 98 | if (error) |
| 99 | return error; |
| 100 | if (!el->el_ea) |
| 101 | return 0; |
| 102 | if (!GFS2_EA_DATA_LEN(el->el_ea)) |
| 103 | goto out; |
| 104 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 105 | len = GFS2_EA_DATA_LEN(el->el_ea); |
| 106 | data = kmalloc(len, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 107 | error = -ENOMEM; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 108 | if (!data) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 109 | goto out; |
| 110 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 111 | error = gfs2_ea_get_copy(ip, el, data, len); |
| 112 | if (error < 0) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 113 | goto out_kfree; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 114 | error = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 115 | |
| 116 | if (acl) { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 117 | *acl = posix_acl_from_xattr(data, len); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 118 | if (IS_ERR(*acl)) |
| 119 | error = PTR_ERR(*acl); |
| 120 | } |
| 121 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 122 | out_kfree: |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 123 | if (error || !datap) { |
| 124 | kfree(data); |
| 125 | } else { |
| 126 | *datap = data; |
| 127 | *lenp = len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 128 | } |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 129 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 130 | return error; |
| 131 | } |
| 132 | |
| 133 | /** |
Steven Whitehouse | 77386e1 | 2006-11-29 10:41:49 -0500 | [diff] [blame] | 134 | * 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] | 135 | * @inode: the file we want to do something to |
| 136 | * @mask: what we want to do |
| 137 | * |
| 138 | * Returns: errno |
| 139 | */ |
| 140 | |
Steven Whitehouse | 77386e1 | 2006-11-29 10:41:49 -0500 | [diff] [blame] | 141 | int gfs2_check_acl(struct inode *inode, int mask) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 142 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 143 | struct gfs2_ea_location el; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 144 | struct posix_acl *acl = NULL; |
| 145 | int error; |
| 146 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 147 | error = acl_get(GFS2_I(inode), GFS2_POSIX_ACL_ACCESS, &acl, &el, NULL, NULL); |
| 148 | brelse(el.el_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 149 | if (error) |
| 150 | return error; |
| 151 | |
| 152 | if (acl) { |
| 153 | error = posix_acl_permission(inode, acl, mask); |
| 154 | posix_acl_release(acl); |
| 155 | return error; |
| 156 | } |
| 157 | |
| 158 | return -EAGAIN; |
| 159 | } |
| 160 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 161 | static int munge_mode(struct gfs2_inode *ip, mode_t mode) |
| 162 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 163 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 164 | struct buffer_head *dibh; |
| 165 | int error; |
| 166 | |
| 167 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); |
| 168 | if (error) |
| 169 | return error; |
| 170 | |
| 171 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 172 | if (!error) { |
| 173 | gfs2_assert_withdraw(sdp, |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 174 | (ip->i_inode.i_mode & S_IFMT) == (mode & S_IFMT)); |
| 175 | ip->i_inode.i_mode = mode; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 176 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 177 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 178 | brelse(dibh); |
| 179 | } |
| 180 | |
| 181 | gfs2_trans_end(sdp); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int gfs2_acl_create(struct gfs2_inode *dip, struct gfs2_inode *ip) |
| 187 | { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 188 | struct gfs2_ea_location el; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 189 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 190 | struct posix_acl *acl = NULL, *clone; |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 191 | mode_t mode = ip->i_inode.i_mode; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 192 | char *data = NULL; |
| 193 | unsigned int len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 194 | int error; |
| 195 | |
| 196 | if (!sdp->sd_args.ar_posix_acl) |
| 197 | return 0; |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 198 | if (S_ISLNK(ip->i_inode.i_mode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 199 | return 0; |
| 200 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 201 | error = acl_get(dip, GFS2_POSIX_ACL_DEFAULT, &acl, &el, &data, &len); |
| 202 | brelse(el.el_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 203 | if (error) |
| 204 | return error; |
| 205 | if (!acl) { |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 206 | mode &= ~current_umask(); |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 207 | if (mode != ip->i_inode.i_mode) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 208 | error = munge_mode(ip, mode); |
| 209 | return error; |
| 210 | } |
| 211 | |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 212 | clone = posix_acl_clone(acl, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 213 | error = -ENOMEM; |
| 214 | if (!clone) |
| 215 | goto out; |
| 216 | posix_acl_release(acl); |
| 217 | acl = clone; |
| 218 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 219 | if (S_ISDIR(ip->i_inode.i_mode)) { |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 220 | error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS, |
| 221 | GFS2_POSIX_ACL_DEFAULT, data, len, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 222 | if (error) |
| 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | error = posix_acl_create_masq(acl, &mode); |
| 227 | if (error < 0) |
| 228 | goto out; |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 229 | if (error == 0) |
| 230 | goto munge; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 231 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 232 | posix_acl_to_xattr(acl, data, len); |
| 233 | error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS, |
| 234 | GFS2_POSIX_ACL_ACCESS, data, len, 0); |
| 235 | if (error) |
| 236 | goto out; |
| 237 | munge: |
| 238 | error = munge_mode(ip, mode); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 239 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 240 | posix_acl_release(acl); |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 241 | kfree(data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 242 | return error; |
| 243 | } |
| 244 | |
| 245 | int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr) |
| 246 | { |
| 247 | struct posix_acl *acl = NULL, *clone; |
| 248 | struct gfs2_ea_location el; |
| 249 | char *data; |
| 250 | unsigned int len; |
| 251 | int error; |
| 252 | |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 253 | error = acl_get(ip, GFS2_POSIX_ACL_ACCESS, &acl, &el, &data, &len); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 254 | if (error) |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 255 | goto out_brelse; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 256 | if (!acl) |
| 257 | return gfs2_setattr_simple(ip, attr); |
| 258 | |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 259 | clone = posix_acl_clone(acl, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 260 | error = -ENOMEM; |
| 261 | if (!clone) |
| 262 | goto out; |
| 263 | posix_acl_release(acl); |
| 264 | acl = clone; |
| 265 | |
| 266 | error = posix_acl_chmod_masq(acl, attr->ia_mode); |
| 267 | if (!error) { |
| 268 | posix_acl_to_xattr(acl, data, len); |
| 269 | error = gfs2_ea_acl_chmod(ip, &el, attr, data); |
| 270 | } |
| 271 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 272 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 273 | posix_acl_release(acl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 274 | kfree(data); |
Steven Whitehouse | 40b78a3 | 2009-08-26 18:41:32 +0100 | [diff] [blame] | 275 | out_brelse: |
| 276 | brelse(el.el_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 277 | return error; |
| 278 | } |
| 279 | |