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