blob: 7a3754488312c650b3003f5cb23a06bb17a53aeb [file] [log] [blame]
Mike Marshall5db11c22015-07-17 10:38:12 -04001/*
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 Marshall575e9462015-12-04 12:56:14 -05008#include "orangefs-kernel.h"
9#include "orangefs-bufmap.h"
Mike Marshall5db11c22015-07-17 10:38:12 -040010#include <linux/posix_acl_xattr.h>
11#include <linux/fs_struct.h>
12
Yi Liu8bb8aef2015-11-24 15:12:14 -050013struct posix_acl *orangefs_get_acl(struct inode *inode, int type)
Mike Marshall5db11c22015-07-17 10:38:12 -040014{
15 struct posix_acl *acl;
16 int ret;
17 char *key = NULL, *value = NULL;
18
19 switch (type) {
20 case ACL_TYPE_ACCESS:
Andreas Gruenbacher972a7342016-05-30 11:25:59 +020021 key = XATTR_NAME_POSIX_ACL_ACCESS;
Mike Marshall5db11c22015-07-17 10:38:12 -040022 break;
23 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher972a7342016-05-30 11:25:59 +020024 key = XATTR_NAME_POSIX_ACL_DEFAULT;
Mike Marshall5db11c22015-07-17 10:38:12 -040025 break;
26 default:
Yi Liu8bb8aef2015-11-24 15:12:14 -050027 gossip_err("orangefs_get_acl: bogus value of type %d\n", type);
Mike Marshall5db11c22015-07-17 10:38:12 -040028 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 Liu8bb8aef2015-11-24 15:12:14 -050034 * orangefs_inode_getxattr() to probe the length of the value, but
Mike Marshall5db11c22015-07-17 10:38:12 -040035 * I don't do that for now.
36 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050037 value = kmalloc(ORANGEFS_MAX_XATTR_VALUELEN, GFP_KERNEL);
Mike Marshall5db11c22015-07-17 10:38:12 -040038 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 Gruenbacherd373a712016-06-04 11:02:33 +020046 ret = orangefs_inode_getxattr(inode, key, value,
47 ORANGEFS_MAX_XATTR_VALUELEN);
Mike Marshall5db11c22015-07-17 10:38:12 -040048 /* 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 Liu8bb8aef2015-11-24 15:12:14 -050064int orangefs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
Mike Marshall5db11c22015-07-17 10:38:12 -040065{
Yi Liu8bb8aef2015-11-24 15:12:14 -050066 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -040067 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 Gruenbacher972a7342016-05-30 11:25:59 +020074 name = XATTR_NAME_POSIX_ACL_ACCESS;
Mike Marshall5db11c22015-07-17 10:38:12 -040075 if (acl) {
Jan Kara07393102016-09-19 17:39:09 +020076 umode_t mode;
77
78 error = posix_acl_update_mode(inode, &mode, &acl);
79 if (error) {
80 gossip_err("%s: posix_acl_update_mode err: %d\n",
Mike Marshall5db11c22015-07-17 10:38:12 -040081 __func__,
82 error);
83 return error;
84 }
85
86 if (inode->i_mode != mode)
Yi Liu8bb8aef2015-11-24 15:12:14 -050087 SetModeFlag(orangefs_inode);
Mike Marshall5db11c22015-07-17 10:38:12 -040088 inode->i_mode = mode;
89 mark_inode_dirty_sync(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -040090 }
91 break;
92 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher972a7342016-05-30 11:25:59 +020093 name = XATTR_NAME_POSIX_ACL_DEFAULT;
Mike Marshall5db11c22015-07-17 10:38:12 -040094 break;
95 default:
96 gossip_err("%s: invalid type %d!\n", __func__, type);
97 return -EINVAL;
98 }
99
100 gossip_debug(GOSSIP_ACL_DEBUG,
101 "%s: inode %pU, key %s type %d\n",
102 __func__, get_khandle_from_ino(inode),
103 name,
104 type);
105
106 if (acl) {
107 size = posix_acl_xattr_size(acl->a_count);
108 value = kmalloc(size, GFP_KERNEL);
109 if (!value)
110 return -ENOMEM;
111
112 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
113 if (error < 0)
114 goto out;
115 }
116
117 gossip_debug(GOSSIP_ACL_DEBUG,
118 "%s: name %s, value %p, size %zd, acl %p\n",
119 __func__, name, value, size, acl);
120 /*
121 * Go ahead and set the extended attribute now. NOTE: Suppose acl
122 * was NULL, then value will be NULL and size will be 0 and that
123 * will xlate to a removexattr. However, we don't want removexattr
124 * complain if attributes does not exist.
125 */
Andreas Gruenbacherd373a712016-06-04 11:02:33 +0200126 error = orangefs_inode_setxattr(inode, name, value, size, 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400127
128out:
129 kfree(value);
130 if (!error)
131 set_cached_acl(inode, type, acl);
132 return error;
133}
134
Yi Liu8bb8aef2015-11-24 15:12:14 -0500135int orangefs_init_acl(struct inode *inode, struct inode *dir)
Mike Marshall5db11c22015-07-17 10:38:12 -0400136{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500137 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400138 struct posix_acl *default_acl, *acl;
139 umode_t mode = inode->i_mode;
140 int error = 0;
141
Yi Liu8bb8aef2015-11-24 15:12:14 -0500142 ClearModeFlag(orangefs_inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400143
144 error = posix_acl_create(dir, &mode, &default_acl, &acl);
145 if (error)
146 return error;
147
148 if (default_acl) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500149 error = orangefs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
Mike Marshall5db11c22015-07-17 10:38:12 -0400150 posix_acl_release(default_acl);
151 }
152
153 if (acl) {
154 if (!error)
Yi Liu8bb8aef2015-11-24 15:12:14 -0500155 error = orangefs_set_acl(inode, acl, ACL_TYPE_ACCESS);
Mike Marshall5db11c22015-07-17 10:38:12 -0400156 posix_acl_release(acl);
157 }
158
159 /* If mode of the inode was changed, then do a forcible ->setattr */
160 if (mode != inode->i_mode) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500161 SetModeFlag(orangefs_inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400162 inode->i_mode = mode;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500163 orangefs_flush_inode(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400164 }
165
166 return error;
167}