f6af069d42705c5de42a87def6e24832cb210069
[linux-2.6-microblaze.git] / fs / xfs / xfs_acl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2008, Christoph Hellwig
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_shared.h"
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_inode.h"
13 #include "xfs_acl.h"
14 #include "xfs_attr.h"
15 #include "xfs_trace.h"
16 #include <linux/slab.h>
17 #include <linux/xattr.h>
18 #include <linux/posix_acl_xattr.h>
19
20
21 /*
22  * Locking scheme:
23  *  - all ACL updates are protected by inode->i_mutex, which is taken before
24  *    calling into this file.
25  */
26
27 STATIC struct posix_acl *
28 xfs_acl_from_disk(
29         const struct xfs_acl    *aclp,
30         int                     len,
31         int                     max_entries)
32 {
33         struct posix_acl_entry *acl_e;
34         struct posix_acl *acl;
35         const struct xfs_acl_entry *ace;
36         unsigned int count, i;
37
38         if (len < sizeof(*aclp))
39                 return ERR_PTR(-EFSCORRUPTED);
40         count = be32_to_cpu(aclp->acl_cnt);
41         if (count > max_entries || XFS_ACL_SIZE(count) != len)
42                 return ERR_PTR(-EFSCORRUPTED);
43
44         acl = posix_acl_alloc(count, GFP_KERNEL);
45         if (!acl)
46                 return ERR_PTR(-ENOMEM);
47
48         for (i = 0; i < count; i++) {
49                 acl_e = &acl->a_entries[i];
50                 ace = &aclp->acl_entry[i];
51
52                 /*
53                  * The tag is 32 bits on disk and 16 bits in core.
54                  *
55                  * Because every access to it goes through the core
56                  * format first this is not a problem.
57                  */
58                 acl_e->e_tag = be32_to_cpu(ace->ae_tag);
59                 acl_e->e_perm = be16_to_cpu(ace->ae_perm);
60
61                 switch (acl_e->e_tag) {
62                 case ACL_USER:
63                         acl_e->e_uid = xfs_uid_to_kuid(be32_to_cpu(ace->ae_id));
64                         break;
65                 case ACL_GROUP:
66                         acl_e->e_gid = xfs_gid_to_kgid(be32_to_cpu(ace->ae_id));
67                         break;
68                 case ACL_USER_OBJ:
69                 case ACL_GROUP_OBJ:
70                 case ACL_MASK:
71                 case ACL_OTHER:
72                         break;
73                 default:
74                         goto fail;
75                 }
76         }
77         return acl;
78
79 fail:
80         posix_acl_release(acl);
81         return ERR_PTR(-EINVAL);
82 }
83
84 STATIC void
85 xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
86 {
87         const struct posix_acl_entry *acl_e;
88         struct xfs_acl_entry *ace;
89         int i;
90
91         aclp->acl_cnt = cpu_to_be32(acl->a_count);
92         for (i = 0; i < acl->a_count; i++) {
93                 ace = &aclp->acl_entry[i];
94                 acl_e = &acl->a_entries[i];
95
96                 ace->ae_tag = cpu_to_be32(acl_e->e_tag);
97                 switch (acl_e->e_tag) {
98                 case ACL_USER:
99                         ace->ae_id = cpu_to_be32(xfs_kuid_to_uid(acl_e->e_uid));
100                         break;
101                 case ACL_GROUP:
102                         ace->ae_id = cpu_to_be32(xfs_kgid_to_gid(acl_e->e_gid));
103                         break;
104                 default:
105                         ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
106                         break;
107                 }
108
109                 ace->ae_perm = cpu_to_be16(acl_e->e_perm);
110         }
111 }
112
113 struct posix_acl *
114 xfs_get_acl(struct inode *inode, int type)
115 {
116         struct xfs_inode *ip = XFS_I(inode);
117         struct posix_acl *acl = NULL;
118         struct xfs_acl *xfs_acl;
119         unsigned char *ea_name;
120         int error;
121         int len;
122
123         trace_xfs_get_acl(ip);
124
125         switch (type) {
126         case ACL_TYPE_ACCESS:
127                 ea_name = SGI_ACL_FILE;
128                 break;
129         case ACL_TYPE_DEFAULT:
130                 ea_name = SGI_ACL_DEFAULT;
131                 break;
132         default:
133                 BUG();
134         }
135
136         /*
137          * If we have a cached ACLs value just return it, not need to
138          * go out to the disk.
139          */
140         len = XFS_ACL_MAX_SIZE(ip->i_mount);
141         xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
142         if (!xfs_acl)
143                 return ERR_PTR(-ENOMEM);
144
145         error = xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
146                                                         &len, ATTR_ROOT);
147         if (error) {
148                 /*
149                  * If the attribute doesn't exist make sure we have a negative
150                  * cache entry, for any other error assume it is transient.
151                  */
152                 if (error != -ENOATTR)
153                         acl = ERR_PTR(error);
154         } else  {
155                 acl = xfs_acl_from_disk(xfs_acl, len,
156                                         XFS_ACL_MAX_ENTRIES(ip->i_mount));
157         }
158         kmem_free(xfs_acl);
159         return acl;
160 }
161
162 int
163 __xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
164 {
165         struct xfs_inode *ip = XFS_I(inode);
166         unsigned char *ea_name;
167         int error;
168
169         switch (type) {
170         case ACL_TYPE_ACCESS:
171                 ea_name = SGI_ACL_FILE;
172                 break;
173         case ACL_TYPE_DEFAULT:
174                 if (!S_ISDIR(inode->i_mode))
175                         return acl ? -EACCES : 0;
176                 ea_name = SGI_ACL_DEFAULT;
177                 break;
178         default:
179                 return -EINVAL;
180         }
181
182         if (acl) {
183                 struct xfs_acl *xfs_acl;
184                 int len = XFS_ACL_MAX_SIZE(ip->i_mount);
185
186                 xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
187                 if (!xfs_acl)
188                         return -ENOMEM;
189
190                 xfs_acl_to_disk(xfs_acl, acl);
191
192                 /* subtract away the unused acl entries */
193                 len -= sizeof(struct xfs_acl_entry) *
194                          (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
195
196                 error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
197                                 len, ATTR_ROOT);
198
199                 kmem_free(xfs_acl);
200         } else {
201                 /*
202                  * A NULL ACL argument means we want to remove the ACL.
203                  */
204                 error = xfs_attr_remove(ip, ea_name, ATTR_ROOT);
205
206                 /*
207                  * If the attribute didn't exist to start with that's fine.
208                  */
209                 if (error == -ENOATTR)
210                         error = 0;
211         }
212
213         if (!error)
214                 set_cached_acl(inode, type, acl);
215         return error;
216 }
217
218 static int
219 xfs_set_mode(struct inode *inode, umode_t mode)
220 {
221         int error = 0;
222
223         if (mode != inode->i_mode) {
224                 struct iattr iattr;
225
226                 iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
227                 iattr.ia_mode = mode;
228                 iattr.ia_ctime = current_time(inode);
229
230                 error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
231         }
232
233         return error;
234 }
235
236 int
237 xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
238 {
239         umode_t mode;
240         bool set_mode = false;
241         int error = 0;
242
243         if (!acl)
244                 goto set_acl;
245
246         error = -E2BIG;
247         if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
248                 return error;
249
250         if (type == ACL_TYPE_ACCESS) {
251                 error = posix_acl_update_mode(inode, &mode, &acl);
252                 if (error)
253                         return error;
254                 set_mode = true;
255         }
256
257  set_acl:
258         error =  __xfs_set_acl(inode, acl, type);
259         if (error)
260                 return error;
261
262         /*
263          * We set the mode after successfully updating the ACL xattr because the
264          * xattr update can fail at ENOSPC and we don't want to change the mode
265          * if the ACL update hasn't been applied.
266          */
267         if (set_mode)
268                 error = xfs_set_mode(inode, mode);
269
270         return error;
271 }