2 * Copyright (c) 2008, Christoph Hellwig
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.
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.
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
21 #include "xfs_bmap_btree.h"
22 #include "xfs_inode.h"
23 #include "xfs_vnodeops.h"
25 #include "xfs_mount.h"
26 #include "xfs_trace.h"
27 #include <linux/slab.h>
28 #include <linux/xattr.h>
29 #include <linux/posix_acl_xattr.h>
34 * - all ACL updates are protected by inode->i_mutex, which is taken before
35 * calling into this file.
38 STATIC
struct posix_acl
*
43 struct posix_acl_entry
*acl_e
;
44 struct posix_acl
*acl
;
45 struct xfs_acl_entry
*ace
;
46 unsigned int count
, i
;
48 count
= be32_to_cpu(aclp
->acl_cnt
);
49 if (count
> max_entries
)
50 return ERR_PTR(-EFSCORRUPTED
);
52 acl
= posix_acl_alloc(count
, GFP_KERNEL
);
54 return ERR_PTR(-ENOMEM
);
56 for (i
= 0; i
< count
; i
++) {
57 acl_e
= &acl
->a_entries
[i
];
58 ace
= &aclp
->acl_entry
[i
];
61 * The tag is 32 bits on disk and 16 bits in core.
63 * Because every access to it goes through the core
64 * format first this is not a problem.
66 acl_e
->e_tag
= be32_to_cpu(ace
->ae_tag
);
67 acl_e
->e_perm
= be16_to_cpu(ace
->ae_perm
);
69 switch (acl_e
->e_tag
) {
72 acl_e
->e_id
= be32_to_cpu(ace
->ae_id
);
78 acl_e
->e_id
= ACL_UNDEFINED_ID
;
87 posix_acl_release(acl
);
88 return ERR_PTR(-EINVAL
);
92 xfs_acl_to_disk(struct xfs_acl
*aclp
, const struct posix_acl
*acl
)
94 const struct posix_acl_entry
*acl_e
;
95 struct xfs_acl_entry
*ace
;
98 aclp
->acl_cnt
= cpu_to_be32(acl
->a_count
);
99 for (i
= 0; i
< acl
->a_count
; i
++) {
100 ace
= &aclp
->acl_entry
[i
];
101 acl_e
= &acl
->a_entries
[i
];
103 ace
->ae_tag
= cpu_to_be32(acl_e
->e_tag
);
104 ace
->ae_id
= cpu_to_be32(acl_e
->e_id
);
105 ace
->ae_perm
= cpu_to_be16(acl_e
->e_perm
);
110 xfs_get_acl(struct inode
*inode
, int type
)
112 struct xfs_inode
*ip
= XFS_I(inode
);
113 struct posix_acl
*acl
;
114 struct xfs_acl
*xfs_acl
;
115 unsigned char *ea_name
;
119 acl
= get_cached_acl(inode
, type
);
120 if (acl
!= ACL_NOT_CACHED
)
123 trace_xfs_get_acl(ip
);
126 case ACL_TYPE_ACCESS
:
127 ea_name
= SGI_ACL_FILE
;
129 case ACL_TYPE_DEFAULT
:
130 ea_name
= SGI_ACL_DEFAULT
;
137 * If we have a cached ACLs value just return it, not need to
138 * go out to the disk.
140 len
= XFS_ACL_MAX_SIZE(ip
->i_mount
);
141 xfs_acl
= kzalloc(len
, GFP_KERNEL
);
143 return ERR_PTR(-ENOMEM
);
145 error
= -xfs_attr_get(ip
, ea_name
, (unsigned char *)xfs_acl
,
149 * If the attribute doesn't exist make sure we have a negative
150 * cache entry, for any other error assume it is transient and
151 * leave the cache entry as ACL_NOT_CACHED.
153 if (error
== -ENOATTR
) {
155 goto out_update_cache
;
160 acl
= xfs_acl_from_disk(xfs_acl
, XFS_ACL_MAX_ENTRIES(ip
->i_mount
));
165 set_cached_acl(inode
, type
, acl
);
172 xfs_set_acl(struct inode
*inode
, int type
, struct posix_acl
*acl
)
174 struct xfs_inode
*ip
= XFS_I(inode
);
175 unsigned char *ea_name
;
178 if (S_ISLNK(inode
->i_mode
))
182 case ACL_TYPE_ACCESS
:
183 ea_name
= SGI_ACL_FILE
;
185 case ACL_TYPE_DEFAULT
:
186 if (!S_ISDIR(inode
->i_mode
))
187 return acl
? -EACCES
: 0;
188 ea_name
= SGI_ACL_DEFAULT
;
195 struct xfs_acl
*xfs_acl
;
196 int len
= XFS_ACL_MAX_SIZE(ip
->i_mount
);
198 xfs_acl
= kzalloc(len
, GFP_KERNEL
);
202 xfs_acl_to_disk(xfs_acl
, acl
);
204 /* subtract away the unused acl entries */
205 len
-= sizeof(struct xfs_acl_entry
) *
206 (XFS_ACL_MAX_ENTRIES(ip
->i_mount
) - acl
->a_count
);
208 error
= -xfs_attr_set(ip
, ea_name
, (unsigned char *)xfs_acl
,
214 * A NULL ACL argument means we want to remove the ACL.
216 error
= -xfs_attr_remove(ip
, ea_name
, ATTR_ROOT
);
219 * If the attribute didn't exist to start with that's fine.
221 if (error
== -ENOATTR
)
226 set_cached_acl(inode
, type
, acl
);
231 xfs_set_mode(struct inode
*inode
, umode_t mode
)
235 if (mode
!= inode
->i_mode
) {
238 iattr
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
239 iattr
.ia_mode
= mode
;
240 iattr
.ia_ctime
= current_fs_time(inode
->i_sb
);
242 error
= -xfs_setattr_nonsize(XFS_I(inode
), &iattr
, XFS_ATTR_NOACL
);
249 xfs_acl_exists(struct inode
*inode
, unsigned char *name
)
251 int len
= XFS_ACL_MAX_SIZE(XFS_M(inode
->i_sb
));
253 return (xfs_attr_get(XFS_I(inode
), name
, NULL
, &len
,
254 ATTR_ROOT
|ATTR_KERNOVAL
) == 0);
258 posix_acl_access_exists(struct inode
*inode
)
260 return xfs_acl_exists(inode
, SGI_ACL_FILE
);
264 posix_acl_default_exists(struct inode
*inode
)
266 if (!S_ISDIR(inode
->i_mode
))
268 return xfs_acl_exists(inode
, SGI_ACL_DEFAULT
);
272 * No need for i_mutex because the inode is not yet exposed to the VFS.
275 xfs_inherit_acl(struct inode
*inode
, struct posix_acl
*acl
)
277 umode_t mode
= inode
->i_mode
;
278 int error
= 0, inherit
= 0;
280 if (S_ISDIR(inode
->i_mode
)) {
281 error
= xfs_set_acl(inode
, ACL_TYPE_DEFAULT
, acl
);
286 error
= posix_acl_create(&acl
, GFP_KERNEL
, &mode
);
291 * If posix_acl_create returns a positive value we need to
292 * inherit a permission that can't be represented using the Unix
293 * mode bits and we actually need to set an ACL.
298 error
= xfs_set_mode(inode
, mode
);
303 error
= xfs_set_acl(inode
, ACL_TYPE_ACCESS
, acl
);
306 posix_acl_release(acl
);
311 xfs_acl_chmod(struct inode
*inode
)
313 struct posix_acl
*acl
;
316 if (S_ISLNK(inode
->i_mode
))
319 acl
= xfs_get_acl(inode
, ACL_TYPE_ACCESS
);
320 if (IS_ERR(acl
) || !acl
)
323 error
= posix_acl_chmod(&acl
, GFP_KERNEL
, inode
->i_mode
);
327 error
= xfs_set_acl(inode
, ACL_TYPE_ACCESS
, acl
);
328 posix_acl_release(acl
);
333 xfs_xattr_acl_get(struct dentry
*dentry
, const char *name
,
334 void *value
, size_t size
, int type
)
336 struct posix_acl
*acl
;
339 acl
= xfs_get_acl(dentry
->d_inode
, type
);
345 error
= posix_acl_to_xattr(&init_user_ns
, acl
, value
, size
);
346 posix_acl_release(acl
);
352 xfs_xattr_acl_set(struct dentry
*dentry
, const char *name
,
353 const void *value
, size_t size
, int flags
, int type
)
355 struct inode
*inode
= dentry
->d_inode
;
356 struct posix_acl
*acl
= NULL
;
359 if (flags
& XATTR_CREATE
)
361 if (type
== ACL_TYPE_DEFAULT
&& !S_ISDIR(inode
->i_mode
))
362 return value
? -EACCES
: 0;
363 if ((current_fsuid() != inode
->i_uid
) && !capable(CAP_FOWNER
))
369 acl
= posix_acl_from_xattr(&init_user_ns
, value
, size
);
372 * acl_set_file(3) may request that we set default ACLs with
373 * zero length -- defend (gracefully) against that here.
378 error
= PTR_ERR(acl
);
382 error
= posix_acl_valid(acl
);
387 if (acl
->a_count
> XFS_ACL_MAX_ENTRIES(XFS_M(inode
->i_sb
)))
390 if (type
== ACL_TYPE_ACCESS
) {
391 umode_t mode
= inode
->i_mode
;
392 error
= posix_acl_equiv_mode(acl
, &mode
);
395 posix_acl_release(acl
);
402 error
= xfs_set_mode(inode
, mode
);
408 error
= xfs_set_acl(inode
, type
, acl
);
410 posix_acl_release(acl
);
415 const struct xattr_handler xfs_xattr_acl_access_handler
= {
416 .prefix
= POSIX_ACL_XATTR_ACCESS
,
417 .flags
= ACL_TYPE_ACCESS
,
418 .get
= xfs_xattr_acl_get
,
419 .set
= xfs_xattr_acl_set
,
422 const struct xattr_handler xfs_xattr_acl_default_handler
= {
423 .prefix
= POSIX_ACL_XATTR_DEFAULT
,
424 .flags
= ACL_TYPE_DEFAULT
,
425 .get
= xfs_xattr_acl_get
,
426 .set
= xfs_xattr_acl_set
,