1 // SPDX-License-Identifier: GPL-2.0
3 * (C) 2001 Clemson University and The University of Chicago
5 * See COPYING in top-level directory.
9 #include "orangefs-kernel.h"
10 #include "orangefs-bufmap.h"
11 #include <linux/posix_acl_xattr.h>
12 #include <linux/fs_struct.h>
14 struct posix_acl
*orangefs_get_acl(struct inode
*inode
, int type
)
16 struct posix_acl
*acl
;
18 char *key
= NULL
, *value
= NULL
;
22 key
= XATTR_NAME_POSIX_ACL_ACCESS
;
24 case ACL_TYPE_DEFAULT
:
25 key
= XATTR_NAME_POSIX_ACL_DEFAULT
;
28 gossip_err("orangefs_get_acl: bogus value of type %d\n", type
);
29 return ERR_PTR(-EINVAL
);
32 * Rather than incurring a network call just to determine the exact
33 * length of the attribute, I just allocate a max length to save on
34 * the network call. Conceivably, we could pass NULL to
35 * orangefs_inode_getxattr() to probe the length of the value, but
36 * I don't do that for now.
38 value
= kmalloc(ORANGEFS_MAX_XATTR_VALUELEN
, GFP_KERNEL
);
40 return ERR_PTR(-ENOMEM
);
42 gossip_debug(GOSSIP_ACL_DEBUG
,
43 "inode %pU, key %s, type %d\n",
44 get_khandle_from_ino(inode
),
47 ret
= orangefs_inode_getxattr(inode
, key
, value
,
48 ORANGEFS_MAX_XATTR_VALUELEN
);
49 /* if the key exists, convert it to an in-memory rep */
51 acl
= posix_acl_from_xattr(&init_user_ns
, value
, ret
);
52 } else if (ret
== -ENODATA
|| ret
== -ENOSYS
) {
55 gossip_err("inode %pU retrieving acl's failed with error %d\n",
56 get_khandle_from_ino(inode
),
60 /* kfree(NULL) is safe, so don't worry if value ever got used */
65 static int __orangefs_set_acl(struct inode
*inode
, struct posix_acl
*acl
,
71 const char *name
= NULL
;
75 name
= XATTR_NAME_POSIX_ACL_ACCESS
;
77 case ACL_TYPE_DEFAULT
:
78 name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
81 gossip_err("%s: invalid type %d!\n", __func__
, type
);
85 gossip_debug(GOSSIP_ACL_DEBUG
,
86 "%s: inode %pU, key %s type %d\n",
87 __func__
, get_khandle_from_ino(inode
),
92 size
= posix_acl_xattr_size(acl
->a_count
);
93 value
= kmalloc(size
, GFP_KERNEL
);
97 error
= posix_acl_to_xattr(&init_user_ns
, acl
, value
, size
);
102 gossip_debug(GOSSIP_ACL_DEBUG
,
103 "%s: name %s, value %p, size %zd, acl %p\n",
104 __func__
, name
, value
, size
, acl
);
106 * Go ahead and set the extended attribute now. NOTE: Suppose acl
107 * was NULL, then value will be NULL and size will be 0 and that
108 * will xlate to a removexattr. However, we don't want removexattr
109 * complain if attributes does not exist.
111 error
= orangefs_inode_setxattr(inode
, name
, value
, size
, 0);
116 set_cached_acl(inode
, type
, acl
);
120 int orangefs_set_acl(struct inode
*inode
, struct posix_acl
*acl
, int type
)
126 if (type
== ACL_TYPE_ACCESS
&& acl
) {
128 * posix_acl_update_mode checks to see if the permissions
129 * described by the ACL can be encoded into the
130 * object's mode. If so, it sets "acl" to NULL
131 * and "mode" to the new desired value. It is up to
132 * us to propagate the new mode back to the server...
134 error
= posix_acl_update_mode(inode
, &iattr
.ia_mode
, &acl
);
136 gossip_err("%s: posix_acl_update_mode err: %d\n",
143 rc
= __orangefs_set_acl(inode
, acl
, type
);
145 iattr
.ia_valid
= ATTR_MODE
;
146 rc
= orangefs_inode_setattr(inode
, &iattr
);
156 int orangefs_init_acl(struct inode
*inode
, struct inode
*dir
)
158 struct orangefs_inode_s
*orangefs_inode
= ORANGEFS_I(inode
);
159 struct posix_acl
*default_acl
, *acl
;
160 umode_t mode
= inode
->i_mode
;
163 ClearModeFlag(orangefs_inode
);
165 error
= posix_acl_create(dir
, &mode
, &default_acl
, &acl
);
170 error
= __orangefs_set_acl(inode
, default_acl
,
172 posix_acl_release(default_acl
);
177 error
= __orangefs_set_acl(inode
, acl
, ACL_TYPE_ACCESS
);
178 posix_acl_release(acl
);
181 /* If mode of the inode was changed, then do a forcible ->setattr */
182 if (mode
!= inode
->i_mode
) {
183 SetModeFlag(orangefs_inode
);
184 inode
->i_mode
= mode
;
185 orangefs_flush_inode(inode
);