Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

posix_acl: make posix_acl_to_xattr() alloc the buffer

Without exception all caller do that. So move the allocation into the
helper.

This reduces boilerplate and removes unnecessary error checking.

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
Link: https://patch.msgid.link/20260115122341.556026-1-mszeredi@redhat.com
Signed-off-by: Christian Brauner <brauner@kernel.org>

authored by

Miklos Szeredi and committed by
Christian Brauner
6cbfdf89 88ec797c

+53 -97
+3 -13
fs/9p/acl.c
··· 167 167 if (retval) 168 168 goto err_out; 169 169 170 - size = posix_acl_xattr_size(acl->a_count); 171 - 172 - value = kzalloc(size, GFP_NOFS); 170 + value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS); 173 171 if (!value) { 174 172 retval = -ENOMEM; 175 173 goto err_out; 176 174 } 177 - 178 - retval = posix_acl_to_xattr(&init_user_ns, acl, value, size); 179 - if (retval < 0) 180 - goto err_out; 181 175 } 182 176 183 177 /* ··· 251 257 return 0; 252 258 253 259 /* Set a setxattr request to server */ 254 - size = posix_acl_xattr_size(acl->a_count); 255 - buffer = kmalloc(size, GFP_KERNEL); 260 + buffer = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_KERNEL); 256 261 if (!buffer) 257 262 return -ENOMEM; 258 - retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size); 259 - if (retval < 0) 260 - goto err_free_out; 263 + 261 264 switch (type) { 262 265 case ACL_TYPE_ACCESS: 263 266 name = XATTR_NAME_POSIX_ACL_ACCESS; ··· 266 275 BUG(); 267 276 } 268 277 retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0); 269 - err_free_out: 270 278 kfree(buffer); 271 279 return retval; 272 280 }
+3 -7
fs/btrfs/acl.c
··· 57 57 int __btrfs_set_acl(struct btrfs_trans_handle *trans, struct inode *inode, 58 58 struct posix_acl *acl, int type) 59 59 { 60 - int ret, size = 0; 60 + int ret; 61 + size_t size = 0; 61 62 const char *name; 62 63 char AUTO_KFREE(value); 63 64 ··· 78 77 if (acl) { 79 78 unsigned int nofs_flag; 80 79 81 - size = posix_acl_xattr_size(acl->a_count); 82 80 /* 83 81 * We're holding a transaction handle, so use a NOFS memory 84 82 * allocation context to avoid deadlock if reclaim happens. 85 83 */ 86 84 nofs_flag = memalloc_nofs_save(); 87 - value = kmalloc(size, GFP_KERNEL); 85 + value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_KERNEL); 88 86 memalloc_nofs_restore(nofs_flag); 89 87 if (!value) 90 88 return -ENOMEM; 91 - 92 - ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); 93 - if (ret < 0) 94 - return ret; 95 89 } 96 90 97 91 if (trans)
+22 -28
fs/ceph/acl.c
··· 90 90 int ceph_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 91 91 struct posix_acl *acl, int type) 92 92 { 93 - int ret = 0, size = 0; 93 + int ret = 0; 94 + size_t size = 0; 94 95 const char *name = NULL; 95 96 char *value = NULL; 96 97 struct iattr newattrs; ··· 127 126 } 128 127 129 128 if (acl) { 130 - size = posix_acl_xattr_size(acl->a_count); 131 - value = kmalloc(size, GFP_NOFS); 129 + value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS); 132 130 if (!value) { 133 131 ret = -ENOMEM; 134 132 goto out; 135 133 } 136 - 137 - ret = posix_acl_to_xattr(&init_user_ns, acl, value, size); 138 - if (ret < 0) 139 - goto out_free; 140 134 } 141 135 142 136 if (new_mode != old_mode) { ··· 168 172 struct posix_acl *acl, *default_acl; 169 173 size_t val_size1 = 0, val_size2 = 0; 170 174 struct ceph_pagelist *pagelist = NULL; 171 - void *tmp_buf = NULL; 175 + void *tmp_buf1 = NULL, *tmp_buf2 = NULL; 172 176 int err; 173 177 174 178 err = posix_acl_create(dir, mode, &default_acl, &acl); ··· 188 192 if (!default_acl && !acl) 189 193 return 0; 190 194 191 - if (acl) 192 - val_size1 = posix_acl_xattr_size(acl->a_count); 193 - if (default_acl) 194 - val_size2 = posix_acl_xattr_size(default_acl->a_count); 195 - 196 195 err = -ENOMEM; 197 - tmp_buf = kmalloc(max(val_size1, val_size2), GFP_KERNEL); 198 - if (!tmp_buf) 199 - goto out_err; 200 196 pagelist = ceph_pagelist_alloc(GFP_KERNEL); 201 197 if (!pagelist) 202 198 goto out_err; ··· 201 213 202 214 if (acl) { 203 215 size_t len = strlen(XATTR_NAME_POSIX_ACL_ACCESS); 216 + 217 + err = -ENOMEM; 218 + tmp_buf1 = posix_acl_to_xattr(&init_user_ns, acl, 219 + &val_size1, GFP_KERNEL); 220 + if (!tmp_buf1) 221 + goto out_err; 204 222 err = ceph_pagelist_reserve(pagelist, len + val_size1 + 8); 205 223 if (err) 206 224 goto out_err; 207 225 ceph_pagelist_encode_string(pagelist, XATTR_NAME_POSIX_ACL_ACCESS, 208 226 len); 209 - err = posix_acl_to_xattr(&init_user_ns, acl, 210 - tmp_buf, val_size1); 211 - if (err < 0) 212 - goto out_err; 213 227 ceph_pagelist_encode_32(pagelist, val_size1); 214 - ceph_pagelist_append(pagelist, tmp_buf, val_size1); 228 + ceph_pagelist_append(pagelist, tmp_buf1, val_size1); 215 229 } 216 230 if (default_acl) { 217 231 size_t len = strlen(XATTR_NAME_POSIX_ACL_DEFAULT); 232 + 233 + err = -ENOMEM; 234 + tmp_buf2 = posix_acl_to_xattr(&init_user_ns, default_acl, 235 + &val_size2, GFP_KERNEL); 236 + if (!tmp_buf2) 237 + goto out_err; 218 238 err = ceph_pagelist_reserve(pagelist, len + val_size2 + 8); 219 239 if (err) 220 240 goto out_err; 221 241 ceph_pagelist_encode_string(pagelist, 222 242 XATTR_NAME_POSIX_ACL_DEFAULT, len); 223 - err = posix_acl_to_xattr(&init_user_ns, default_acl, 224 - tmp_buf, val_size2); 225 - if (err < 0) 226 - goto out_err; 227 243 ceph_pagelist_encode_32(pagelist, val_size2); 228 - ceph_pagelist_append(pagelist, tmp_buf, val_size2); 244 + ceph_pagelist_append(pagelist, tmp_buf2, val_size2); 229 245 } 230 246 231 - kfree(tmp_buf); 247 + kfree(tmp_buf1); 248 + kfree(tmp_buf2); 232 249 233 250 as_ctx->acl = acl; 234 251 as_ctx->default_acl = default_acl; ··· 243 250 out_err: 244 251 posix_acl_release(acl); 245 252 posix_acl_release(default_acl); 246 - kfree(tmp_buf); 253 + kfree(tmp_buf1); 254 + kfree(tmp_buf2); 247 255 if (pagelist) 248 256 ceph_pagelist_release(pagelist); 249 257 return err;
+4 -8
fs/fuse/acl.c
··· 122 122 * them to be refreshed the next time they are used, 123 123 * and it also updates i_ctime. 124 124 */ 125 - size_t size = posix_acl_xattr_size(acl->a_count); 125 + size_t size; 126 126 void *value; 127 127 128 - if (size > PAGE_SIZE) 129 - return -E2BIG; 130 - 131 - value = kmalloc(size, GFP_KERNEL); 128 + value = posix_acl_to_xattr(fc->user_ns, acl, &size, GFP_KERNEL); 132 129 if (!value) 133 130 return -ENOMEM; 134 131 135 - ret = posix_acl_to_xattr(fc->user_ns, acl, value, size); 136 - if (ret < 0) { 132 + if (size > PAGE_SIZE) { 137 133 kfree(value); 138 - return ret; 134 + return -E2BIG; 139 135 } 140 136 141 137 /*
+3 -10
fs/gfs2/acl.c
··· 83 83 int __gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type) 84 84 { 85 85 int error; 86 - size_t len; 87 - char *data; 86 + size_t len = 0; 87 + char *data = NULL; 88 88 const char *name = gfs2_acl_name(type); 89 89 90 90 if (acl) { 91 - len = posix_acl_xattr_size(acl->a_count); 92 - data = kmalloc(len, GFP_NOFS); 91 + data = posix_acl_to_xattr(&init_user_ns, acl, &len, GFP_NOFS); 93 92 if (data == NULL) 94 93 return -ENOMEM; 95 - error = posix_acl_to_xattr(&init_user_ns, acl, data, len); 96 - if (error < 0) 97 - goto out; 98 - } else { 99 - data = NULL; 100 - len = 0; 101 94 } 102 95 103 96 error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
+2 -7
fs/jfs/acl.c
··· 61 61 { 62 62 char *ea_name; 63 63 int rc; 64 - int size = 0; 64 + size_t size = 0; 65 65 char *value = NULL; 66 66 67 67 switch (type) { ··· 76 76 } 77 77 78 78 if (acl) { 79 - size = posix_acl_xattr_size(acl->a_count); 80 - value = kmalloc(size, GFP_KERNEL); 79 + value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_KERNEL); 81 80 if (!value) 82 81 return -ENOMEM; 83 - rc = posix_acl_to_xattr(&init_user_ns, acl, value, size); 84 - if (rc < 0) 85 - goto out; 86 82 } 87 83 rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0); 88 - out: 89 84 kfree(value); 90 85 91 86 if (!rc)
+1 -5
fs/ntfs3/xattr.c
··· 641 641 value = NULL; 642 642 flags = XATTR_REPLACE; 643 643 } else { 644 - size = posix_acl_xattr_size(acl->a_count); 645 - value = kmalloc(size, GFP_NOFS); 644 + value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS); 646 645 if (!value) 647 646 return -ENOMEM; 648 - err = posix_acl_to_xattr(&init_user_ns, acl, value, size); 649 - if (err < 0) 650 - goto out; 651 647 flags = 0; 652 648 } 653 649
+1 -7
fs/orangefs/acl.c
··· 90 90 type); 91 91 92 92 if (acl) { 93 - size = posix_acl_xattr_size(acl->a_count); 94 - value = kmalloc(size, GFP_KERNEL); 93 + value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_KERNEL); 95 94 if (!value) 96 95 return -ENOMEM; 97 - 98 - error = posix_acl_to_xattr(&init_user_ns, acl, value, size); 99 - if (error < 0) 100 - goto out; 101 96 } 102 97 103 98 gossip_debug(GOSSIP_ACL_DEBUG, ··· 106 111 */ 107 112 error = orangefs_inode_setxattr(inode, name, value, size, 0); 108 113 109 - out: 110 114 kfree(value); 111 115 if (!error) 112 116 set_cached_acl(inode, type, acl);
+11 -10
fs/posix_acl.c
··· 829 829 /* 830 830 * Convert from in-memory to extended attribute representation. 831 831 */ 832 - int 832 + void * 833 833 posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl, 834 - void *buffer, size_t size) 834 + size_t *sizep, gfp_t gfp) 835 835 { 836 - struct posix_acl_xattr_header *ext_acl = buffer; 836 + struct posix_acl_xattr_header *ext_acl; 837 837 struct posix_acl_xattr_entry *ext_entry; 838 - int real_size, n; 838 + size_t size; 839 + int n; 839 840 840 - real_size = posix_acl_xattr_size(acl->a_count); 841 - if (!buffer) 842 - return real_size; 843 - if (real_size > size) 844 - return -ERANGE; 841 + size = posix_acl_xattr_size(acl->a_count); 842 + ext_acl = kmalloc(size, gfp); 843 + if (!ext_acl) 844 + return NULL; 845 845 846 846 ext_entry = (void *)(ext_acl + 1); 847 847 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION); ··· 864 864 break; 865 865 } 866 866 } 867 - return real_size; 867 + *sizep = size; 868 + return ext_acl; 868 869 } 869 870 EXPORT_SYMBOL (posix_acl_to_xattr); 870 871
+3 -2
include/linux/posix_acl_xattr.h
··· 44 44 } 45 45 #endif 46 46 47 - int posix_acl_to_xattr(struct user_namespace *user_ns, 48 - const struct posix_acl *acl, void *buffer, size_t size); 47 + extern void *posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl, 48 + size_t *sizep, gfp_t gfp); 49 + 49 50 static inline const char *posix_acl_xattr_name(int type) 50 51 { 51 52 switch (type) {