1 /* 2 * Copyright IBM Corporation, 2010 3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of version 2.1 of the GNU Lesser General Public License 7 * as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it would be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 * 13 */ 14 15 #include <linux/module.h> 16 #include <linux/fs.h> 17 #include <net/9p/9p.h> 18 #include <net/9p/client.h> 19 #include <linux/slab.h> 20 #include <linux/sched.h> 21 #include <linux/posix_acl_xattr.h> 22 #include "xattr.h" 23 #include "acl.h" 24 #include "v9fs_vfs.h" 25 26 static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name) 27 { 28 ssize_t size; 29 void *value = NULL; 30 struct posix_acl *acl = NULL;; 31 32 size = v9fs_fid_xattr_get(fid, name, NULL, 0); 33 if (size > 0) { 34 value = kzalloc(size, GFP_NOFS); 35 if (!value) 36 return ERR_PTR(-ENOMEM); 37 size = v9fs_fid_xattr_get(fid, name, value, size); 38 if (size > 0) { 39 acl = posix_acl_from_xattr(value, size); 40 if (IS_ERR(acl)) 41 goto err_out; 42 } 43 } else if (size == -ENODATA || size == 0 || 44 size == -ENOSYS || size == -EOPNOTSUPP) { 45 acl = NULL; 46 } else 47 acl = ERR_PTR(-EIO); 48 49 err_out: 50 kfree(value); 51 return acl; 52 } 53 54 int v9fs_get_acl(struct inode *inode, struct p9_fid *fid) 55 { 56 int retval = 0; 57 struct posix_acl *pacl, *dacl; 58 59 /* get the default/access acl values and cache them */ 60 dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT); 61 pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS); 62 63 if (!IS_ERR(dacl) && !IS_ERR(pacl)) { 64 set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl); 65 set_cached_acl(inode, ACL_TYPE_ACCESS, pacl); 66 posix_acl_release(dacl); 67 posix_acl_release(pacl); 68 } else 69 retval = -EIO; 70 71 return retval; 72 } 73 74 static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type) 75 { 76 struct posix_acl *acl; 77 /* 78 * 9p Always cache the acl value when 79 * instantiating the inode (v9fs_inode_from_fid) 80 */ 81 acl = get_cached_acl(inode, type); 82 BUG_ON(acl == ACL_NOT_CACHED); 83 return acl; 84 } 85 86 int v9fs_check_acl(struct inode *inode, int mask) 87 { 88 struct posix_acl *acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS); 89 90 if (IS_ERR(acl)) 91 return PTR_ERR(acl); 92 if (acl) { 93 int error = posix_acl_permission(inode, acl, mask); 94 posix_acl_release(acl); 95 return error; 96 } 97 return -EAGAIN; 98 } 99 100 static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl) 101 { 102 int retval; 103 char *name; 104 size_t size; 105 void *buffer; 106 struct inode *inode = dentry->d_inode; 107 108 set_cached_acl(inode, type, acl); 109 /* Set a setxattr request to server */ 110 size = posix_acl_xattr_size(acl->a_count); 111 buffer = kmalloc(size, GFP_KERNEL); 112 if (!buffer) 113 return -ENOMEM; 114 retval = posix_acl_to_xattr(acl, buffer, size); 115 if (retval < 0) 116 goto err_free_out; 117 switch (type) { 118 case ACL_TYPE_ACCESS: 119 name = POSIX_ACL_XATTR_ACCESS; 120 break; 121 case ACL_TYPE_DEFAULT: 122 name = POSIX_ACL_XATTR_DEFAULT; 123 break; 124 default: 125 BUG(); 126 } 127 retval = v9fs_xattr_set(dentry, name, buffer, size, 0); 128 err_free_out: 129 kfree(buffer); 130 return retval; 131 } 132 133 int v9fs_acl_chmod(struct dentry *dentry) 134 { 135 int retval = 0; 136 struct posix_acl *acl, *clone; 137 struct inode *inode = dentry->d_inode; 138 139 if (S_ISLNK(inode->i_mode)) 140 return -EOPNOTSUPP; 141 acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS); 142 if (acl) { 143 clone = posix_acl_clone(acl, GFP_KERNEL); 144 posix_acl_release(acl); 145 if (!clone) 146 return -ENOMEM; 147 retval = posix_acl_chmod_masq(clone, inode->i_mode); 148 if (!retval) 149 retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone); 150 posix_acl_release(clone); 151 } 152 return retval; 153 } 154 155 int v9fs_set_create_acl(struct dentry *dentry, 156 struct posix_acl *dpacl, struct posix_acl *pacl) 157 { 158 if (dpacl) 159 v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, dpacl); 160 if (pacl) 161 v9fs_set_acl(dentry, ACL_TYPE_ACCESS, pacl); 162 posix_acl_release(dpacl); 163 posix_acl_release(pacl); 164 return 0; 165 } 166 167 int v9fs_acl_mode(struct inode *dir, mode_t *modep, 168 struct posix_acl **dpacl, struct posix_acl **pacl) 169 { 170 int retval = 0; 171 mode_t mode = *modep; 172 struct posix_acl *acl = NULL; 173 174 if (!S_ISLNK(mode)) { 175 acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT); 176 if (IS_ERR(acl)) 177 return PTR_ERR(acl); 178 if (!acl) 179 mode &= ~current_umask(); 180 } 181 if (acl) { 182 struct posix_acl *clone; 183 184 if (S_ISDIR(mode)) 185 *dpacl = acl; 186 clone = posix_acl_clone(acl, GFP_NOFS); 187 retval = -ENOMEM; 188 if (!clone) 189 goto cleanup; 190 191 retval = posix_acl_create_masq(clone, &mode); 192 if (retval < 0) { 193 posix_acl_release(clone); 194 goto cleanup; 195 } 196 if (retval > 0) 197 *pacl = clone; 198 } 199 *modep = mode; 200 return 0; 201 cleanup: 202 posix_acl_release(acl); 203 return retval; 204 205 } 206 207 static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name, 208 void *buffer, size_t size, int type) 209 { 210 struct posix_acl *acl; 211 int error; 212 213 if (strcmp(name, "") != 0) 214 return -EINVAL; 215 216 acl = v9fs_get_cached_acl(dentry->d_inode, type); 217 if (IS_ERR(acl)) 218 return PTR_ERR(acl); 219 if (acl == NULL) 220 return -ENODATA; 221 error = posix_acl_to_xattr(acl, buffer, size); 222 posix_acl_release(acl); 223 224 return error; 225 } 226 227 static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name, 228 const void *value, size_t size, 229 int flags, int type) 230 { 231 int retval; 232 struct posix_acl *acl; 233 struct inode *inode = dentry->d_inode; 234 235 if (strcmp(name, "") != 0) 236 return -EINVAL; 237 if (S_ISLNK(inode->i_mode)) 238 return -EOPNOTSUPP; 239 if (!is_owner_or_cap(inode)) 240 return -EPERM; 241 if (value) { 242 /* update the cached acl value */ 243 acl = posix_acl_from_xattr(value, size); 244 if (IS_ERR(acl)) 245 return PTR_ERR(acl); 246 else if (acl) { 247 retval = posix_acl_valid(acl); 248 if (retval) 249 goto err_out; 250 } 251 } else 252 acl = NULL; 253 254 switch (type) { 255 case ACL_TYPE_ACCESS: 256 name = POSIX_ACL_XATTR_ACCESS; 257 if (acl) { 258 mode_t mode = inode->i_mode; 259 retval = posix_acl_equiv_mode(acl, &mode); 260 if (retval < 0) 261 goto err_out; 262 else { 263 struct iattr iattr; 264 if (retval == 0) { 265 /* 266 * ACL can be represented 267 * by the mode bits. So don't 268 * update ACL. 269 */ 270 acl = NULL; 271 value = NULL; 272 size = 0; 273 } 274 /* Updte the mode bits */ 275 iattr.ia_mode = ((mode & S_IALLUGO) | 276 (inode->i_mode & ~S_IALLUGO)); 277 iattr.ia_valid = ATTR_MODE; 278 /* FIXME should we update ctime ? 279 * What is the following setxattr update the 280 * mode ? 281 */ 282 v9fs_vfs_setattr_dotl(dentry, &iattr); 283 } 284 } 285 break; 286 case ACL_TYPE_DEFAULT: 287 name = POSIX_ACL_XATTR_DEFAULT; 288 if (!S_ISDIR(inode->i_mode)) { 289 retval = -EINVAL; 290 goto err_out; 291 } 292 break; 293 default: 294 BUG(); 295 } 296 retval = v9fs_xattr_set(dentry, name, value, size, flags); 297 if (!retval) 298 set_cached_acl(inode, type, acl); 299 err_out: 300 posix_acl_release(acl); 301 return retval; 302 } 303 304 const struct xattr_handler v9fs_xattr_acl_access_handler = { 305 .prefix = POSIX_ACL_XATTR_ACCESS, 306 .flags = ACL_TYPE_ACCESS, 307 .get = v9fs_xattr_get_acl, 308 .set = v9fs_xattr_set_acl, 309 }; 310 311 const struct xattr_handler v9fs_xattr_acl_default_handler = { 312 .prefix = POSIX_ACL_XATTR_DEFAULT, 313 .flags = ACL_TYPE_DEFAULT, 314 .get = v9fs_xattr_get_acl, 315 .set = v9fs_xattr_set_acl, 316 }; 317