xfs: merge xfs_ag.h into xfs_format.h
[firefly-linux-kernel-4.4.55.git] / fs / xfs / xfs_acl.c
1 /*
2  * Copyright (c) 2008, Christoph Hellwig
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_format.h"
20 #include "xfs_log_format.h"
21 #include "xfs_trans_resv.h"
22 #include "xfs_sb.h"
23 #include "xfs_mount.h"
24 #include "xfs_inode.h"
25 #include "xfs_acl.h"
26 #include "xfs_attr.h"
27 #include "xfs_trace.h"
28 #include <linux/slab.h>
29 #include <linux/xattr.h>
30 #include <linux/posix_acl_xattr.h>
31
32
33 /*
34  * Locking scheme:
35  *  - all ACL updates are protected by inode->i_mutex, which is taken before
36  *    calling into this file.
37  */
38
39 STATIC struct posix_acl *
40 xfs_acl_from_disk(
41         struct xfs_acl  *aclp,
42         int             max_entries)
43 {
44         struct posix_acl_entry *acl_e;
45         struct posix_acl *acl;
46         struct xfs_acl_entry *ace;
47         unsigned int count, i;
48
49         count = be32_to_cpu(aclp->acl_cnt);
50         if (count > max_entries)
51                 return ERR_PTR(-EFSCORRUPTED);
52
53         acl = posix_acl_alloc(count, GFP_KERNEL);
54         if (!acl)
55                 return ERR_PTR(-ENOMEM);
56
57         for (i = 0; i < count; i++) {
58                 acl_e = &acl->a_entries[i];
59                 ace = &aclp->acl_entry[i];
60
61                 /*
62                  * The tag is 32 bits on disk and 16 bits in core.
63                  *
64                  * Because every access to it goes through the core
65                  * format first this is not a problem.
66                  */
67                 acl_e->e_tag = be32_to_cpu(ace->ae_tag);
68                 acl_e->e_perm = be16_to_cpu(ace->ae_perm);
69
70                 switch (acl_e->e_tag) {
71                 case ACL_USER:
72                         acl_e->e_uid = xfs_uid_to_kuid(be32_to_cpu(ace->ae_id));
73                         break;
74                 case ACL_GROUP:
75                         acl_e->e_gid = xfs_gid_to_kgid(be32_to_cpu(ace->ae_id));
76                         break;
77                 case ACL_USER_OBJ:
78                 case ACL_GROUP_OBJ:
79                 case ACL_MASK:
80                 case ACL_OTHER:
81                         break;
82                 default:
83                         goto fail;
84                 }
85         }
86         return acl;
87
88 fail:
89         posix_acl_release(acl);
90         return ERR_PTR(-EINVAL);
91 }
92
93 STATIC void
94 xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
95 {
96         const struct posix_acl_entry *acl_e;
97         struct xfs_acl_entry *ace;
98         int i;
99
100         aclp->acl_cnt = cpu_to_be32(acl->a_count);
101         for (i = 0; i < acl->a_count; i++) {
102                 ace = &aclp->acl_entry[i];
103                 acl_e = &acl->a_entries[i];
104
105                 ace->ae_tag = cpu_to_be32(acl_e->e_tag);
106                 switch (acl_e->e_tag) {
107                 case ACL_USER:
108                         ace->ae_id = cpu_to_be32(xfs_kuid_to_uid(acl_e->e_uid));
109                         break;
110                 case ACL_GROUP:
111                         ace->ae_id = cpu_to_be32(xfs_kgid_to_gid(acl_e->e_gid));
112                         break;
113                 default:
114                         ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
115                         break;
116                 }
117
118                 ace->ae_perm = cpu_to_be16(acl_e->e_perm);
119         }
120 }
121
122 struct posix_acl *
123 xfs_get_acl(struct inode *inode, int type)
124 {
125         struct xfs_inode *ip = XFS_I(inode);
126         struct posix_acl *acl = NULL;
127         struct xfs_acl *xfs_acl;
128         unsigned char *ea_name;
129         int error;
130         int len;
131
132         trace_xfs_get_acl(ip);
133
134         switch (type) {
135         case ACL_TYPE_ACCESS:
136                 ea_name = SGI_ACL_FILE;
137                 break;
138         case ACL_TYPE_DEFAULT:
139                 ea_name = SGI_ACL_DEFAULT;
140                 break;
141         default:
142                 BUG();
143         }
144
145         /*
146          * If we have a cached ACLs value just return it, not need to
147          * go out to the disk.
148          */
149         len = XFS_ACL_MAX_SIZE(ip->i_mount);
150         xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
151         if (!xfs_acl)
152                 return ERR_PTR(-ENOMEM);
153
154         error = xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
155                                                         &len, ATTR_ROOT);
156         if (error) {
157                 /*
158                  * If the attribute doesn't exist make sure we have a negative
159                  * cache entry, for any other error assume it is transient and
160                  * leave the cache entry as ACL_NOT_CACHED.
161                  */
162                 if (error == -ENOATTR)
163                         goto out_update_cache;
164                 goto out;
165         }
166
167         acl = xfs_acl_from_disk(xfs_acl, XFS_ACL_MAX_ENTRIES(ip->i_mount));
168         if (IS_ERR(acl))
169                 goto out;
170
171 out_update_cache:
172         set_cached_acl(inode, type, acl);
173 out:
174         kmem_free(xfs_acl);
175         return acl;
176 }
177
178 STATIC int
179 __xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
180 {
181         struct xfs_inode *ip = XFS_I(inode);
182         unsigned char *ea_name;
183         int error;
184
185         switch (type) {
186         case ACL_TYPE_ACCESS:
187                 ea_name = SGI_ACL_FILE;
188                 break;
189         case ACL_TYPE_DEFAULT:
190                 if (!S_ISDIR(inode->i_mode))
191                         return acl ? -EACCES : 0;
192                 ea_name = SGI_ACL_DEFAULT;
193                 break;
194         default:
195                 return -EINVAL;
196         }
197
198         if (acl) {
199                 struct xfs_acl *xfs_acl;
200                 int len = XFS_ACL_MAX_SIZE(ip->i_mount);
201
202                 xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
203                 if (!xfs_acl)
204                         return -ENOMEM;
205
206                 xfs_acl_to_disk(xfs_acl, acl);
207
208                 /* subtract away the unused acl entries */
209                 len -= sizeof(struct xfs_acl_entry) *
210                          (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
211
212                 error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
213                                 len, ATTR_ROOT);
214
215                 kmem_free(xfs_acl);
216         } else {
217                 /*
218                  * A NULL ACL argument means we want to remove the ACL.
219                  */
220                 error = xfs_attr_remove(ip, ea_name, ATTR_ROOT);
221
222                 /*
223                  * If the attribute didn't exist to start with that's fine.
224                  */
225                 if (error == -ENOATTR)
226                         error = 0;
227         }
228
229         if (!error)
230                 set_cached_acl(inode, type, acl);
231         return error;
232 }
233
234 static int
235 xfs_set_mode(struct inode *inode, umode_t mode)
236 {
237         int error = 0;
238
239         if (mode != inode->i_mode) {
240                 struct iattr iattr;
241
242                 iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
243                 iattr.ia_mode = mode;
244                 iattr.ia_ctime = current_fs_time(inode->i_sb);
245
246                 error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
247         }
248
249         return error;
250 }
251
252 static int
253 xfs_acl_exists(struct inode *inode, unsigned char *name)
254 {
255         int len = XFS_ACL_MAX_SIZE(XFS_M(inode->i_sb));
256
257         return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
258                             ATTR_ROOT|ATTR_KERNOVAL) == 0);
259 }
260
261 int
262 posix_acl_access_exists(struct inode *inode)
263 {
264         return xfs_acl_exists(inode, SGI_ACL_FILE);
265 }
266
267 int
268 posix_acl_default_exists(struct inode *inode)
269 {
270         if (!S_ISDIR(inode->i_mode))
271                 return 0;
272         return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
273 }
274
275 int
276 xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
277 {
278         int error = 0;
279
280         if (!acl)
281                 goto set_acl;
282
283         error = -E2BIG;
284         if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
285                 return error;
286
287         if (type == ACL_TYPE_ACCESS) {
288                 umode_t mode = inode->i_mode;
289                 error = posix_acl_equiv_mode(acl, &mode);
290
291                 if (error <= 0) {
292                         acl = NULL;
293
294                         if (error < 0)
295                                 return error;
296                 }
297
298                 error = xfs_set_mode(inode, mode);
299                 if (error)
300                         return error;
301         }
302
303  set_acl:
304         return __xfs_set_acl(inode, type, acl);
305 }