Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2001 Clemson University and The University of Chicago |
| 3 | * |
| 4 | * See COPYING in top-level directory. |
| 5 | */ |
| 6 | |
| 7 | #include "protocol.h" |
Mike Marshall | 575e946 | 2015-12-04 12:56:14 -0500 | [diff] [blame] | 8 | #include "orangefs-kernel.h" |
| 9 | #include "orangefs-bufmap.h" |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 10 | #include <linux/posix_acl_xattr.h> |
| 11 | #include <linux/fs_struct.h> |
| 12 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 13 | struct posix_acl *orangefs_get_acl(struct inode *inode, int type) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 14 | { |
| 15 | struct posix_acl *acl; |
| 16 | int ret; |
| 17 | char *key = NULL, *value = NULL; |
| 18 | |
| 19 | switch (type) { |
| 20 | case ACL_TYPE_ACCESS: |
Andreas Gruenbacher | 972a734 | 2016-05-30 11:25:59 +0200 | [diff] [blame] | 21 | key = XATTR_NAME_POSIX_ACL_ACCESS; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 22 | break; |
| 23 | case ACL_TYPE_DEFAULT: |
Andreas Gruenbacher | 972a734 | 2016-05-30 11:25:59 +0200 | [diff] [blame] | 24 | key = XATTR_NAME_POSIX_ACL_DEFAULT; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 25 | break; |
| 26 | default: |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 27 | gossip_err("orangefs_get_acl: bogus value of type %d\n", type); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 28 | return ERR_PTR(-EINVAL); |
| 29 | } |
| 30 | /* |
| 31 | * Rather than incurring a network call just to determine the exact |
| 32 | * length of the attribute, I just allocate a max length to save on |
| 33 | * the network call. Conceivably, we could pass NULL to |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 34 | * orangefs_inode_getxattr() to probe the length of the value, but |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 35 | * I don't do that for now. |
| 36 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 37 | value = kmalloc(ORANGEFS_MAX_XATTR_VALUELEN, GFP_KERNEL); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 38 | if (value == NULL) |
| 39 | return ERR_PTR(-ENOMEM); |
| 40 | |
| 41 | gossip_debug(GOSSIP_ACL_DEBUG, |
| 42 | "inode %pU, key %s, type %d\n", |
| 43 | get_khandle_from_ino(inode), |
| 44 | key, |
| 45 | type); |
Andreas Gruenbacher | d373a71 | 2016-06-04 11:02:33 +0200 | [diff] [blame] | 46 | ret = orangefs_inode_getxattr(inode, key, value, |
| 47 | ORANGEFS_MAX_XATTR_VALUELEN); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 48 | /* if the key exists, convert it to an in-memory rep */ |
| 49 | if (ret > 0) { |
| 50 | acl = posix_acl_from_xattr(&init_user_ns, value, ret); |
| 51 | } else if (ret == -ENODATA || ret == -ENOSYS) { |
| 52 | acl = NULL; |
| 53 | } else { |
| 54 | gossip_err("inode %pU retrieving acl's failed with error %d\n", |
| 55 | get_khandle_from_ino(inode), |
| 56 | ret); |
| 57 | acl = ERR_PTR(ret); |
| 58 | } |
| 59 | /* kfree(NULL) is safe, so don't worry if value ever got used */ |
| 60 | kfree(value); |
| 61 | return acl; |
| 62 | } |
| 63 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 64 | int orangefs_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 65 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 66 | struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 67 | int error = 0; |
| 68 | void *value = NULL; |
| 69 | size_t size = 0; |
| 70 | const char *name = NULL; |
| 71 | |
| 72 | switch (type) { |
| 73 | case ACL_TYPE_ACCESS: |
Andreas Gruenbacher | 972a734 | 2016-05-30 11:25:59 +0200 | [diff] [blame] | 74 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 75 | if (acl) { |
| 76 | umode_t mode = inode->i_mode; |
| 77 | /* |
| 78 | * can we represent this with the traditional file |
| 79 | * mode permission bits? |
| 80 | */ |
| 81 | error = posix_acl_equiv_mode(acl, &mode); |
| 82 | if (error < 0) { |
| 83 | gossip_err("%s: posix_acl_equiv_mode err: %d\n", |
| 84 | __func__, |
| 85 | error); |
| 86 | return error; |
| 87 | } |
| 88 | |
| 89 | if (inode->i_mode != mode) |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 90 | SetModeFlag(orangefs_inode); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 91 | inode->i_mode = mode; |
| 92 | mark_inode_dirty_sync(inode); |
| 93 | if (error == 0) |
| 94 | acl = NULL; |
| 95 | } |
| 96 | break; |
| 97 | case ACL_TYPE_DEFAULT: |
Andreas Gruenbacher | 972a734 | 2016-05-30 11:25:59 +0200 | [diff] [blame] | 98 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 99 | break; |
| 100 | default: |
| 101 | gossip_err("%s: invalid type %d!\n", __func__, type); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | |
| 105 | gossip_debug(GOSSIP_ACL_DEBUG, |
| 106 | "%s: inode %pU, key %s type %d\n", |
| 107 | __func__, get_khandle_from_ino(inode), |
| 108 | name, |
| 109 | type); |
| 110 | |
| 111 | if (acl) { |
| 112 | size = posix_acl_xattr_size(acl->a_count); |
| 113 | value = kmalloc(size, GFP_KERNEL); |
| 114 | if (!value) |
| 115 | return -ENOMEM; |
| 116 | |
| 117 | error = posix_acl_to_xattr(&init_user_ns, acl, value, size); |
| 118 | if (error < 0) |
| 119 | goto out; |
| 120 | } |
| 121 | |
| 122 | gossip_debug(GOSSIP_ACL_DEBUG, |
| 123 | "%s: name %s, value %p, size %zd, acl %p\n", |
| 124 | __func__, name, value, size, acl); |
| 125 | /* |
| 126 | * Go ahead and set the extended attribute now. NOTE: Suppose acl |
| 127 | * was NULL, then value will be NULL and size will be 0 and that |
| 128 | * will xlate to a removexattr. However, we don't want removexattr |
| 129 | * complain if attributes does not exist. |
| 130 | */ |
Andreas Gruenbacher | d373a71 | 2016-06-04 11:02:33 +0200 | [diff] [blame] | 131 | error = orangefs_inode_setxattr(inode, name, value, size, 0); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 132 | |
| 133 | out: |
| 134 | kfree(value); |
| 135 | if (!error) |
| 136 | set_cached_acl(inode, type, acl); |
| 137 | return error; |
| 138 | } |
| 139 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 140 | int orangefs_init_acl(struct inode *inode, struct inode *dir) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 141 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 142 | struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 143 | struct posix_acl *default_acl, *acl; |
| 144 | umode_t mode = inode->i_mode; |
| 145 | int error = 0; |
| 146 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 147 | ClearModeFlag(orangefs_inode); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 148 | |
| 149 | error = posix_acl_create(dir, &mode, &default_acl, &acl); |
| 150 | if (error) |
| 151 | return error; |
| 152 | |
| 153 | if (default_acl) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 154 | error = orangefs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 155 | posix_acl_release(default_acl); |
| 156 | } |
| 157 | |
| 158 | if (acl) { |
| 159 | if (!error) |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 160 | error = orangefs_set_acl(inode, acl, ACL_TYPE_ACCESS); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 161 | posix_acl_release(acl); |
| 162 | } |
| 163 | |
| 164 | /* If mode of the inode was changed, then do a forcible ->setattr */ |
| 165 | if (mode != inode->i_mode) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 166 | SetModeFlag(orangefs_inode); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 167 | inode->i_mode = mode; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 168 | orangefs_flush_inode(inode); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return error; |
| 172 | } |