blob: 9b92058a12409d6aa4040ed228ab1d2e5a6d8537 [file] [log] [blame]
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -07001/*
2 * linux/fs/hfsplus/posix_acl.c
3 *
4 * Vyacheslav Dubeyko <slava@dubeyko.com>
5 *
6 * Handler for Posix Access Control Lists (ACLs) support.
7 */
8
9#include "hfsplus_fs.h"
10#include "xattr.h"
11#include "acl.h"
12
13struct posix_acl *hfsplus_get_posix_acl(struct inode *inode, int type)
14{
15 struct posix_acl *acl;
16 char *xattr_name;
17 char *value = NULL;
18 ssize_t size;
19
Christoph Hellwigb0a7ab52013-12-20 05:16:46 -080020 hfs_dbg(ACL_MOD, "[%s]: ino %lu\n", __func__, inode->i_ino);
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -070021
22 switch (type) {
23 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010024 xattr_name = XATTR_NAME_POSIX_ACL_ACCESS;
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -070025 break;
26 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010027 xattr_name = XATTR_NAME_POSIX_ACL_DEFAULT;
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -070028 break;
29 default:
30 return ERR_PTR(-EINVAL);
31 }
32
33 size = __hfsplus_getxattr(inode, xattr_name, NULL, 0);
34
35 if (size > 0) {
36 value = (char *)hfsplus_alloc_attr_entry();
37 if (unlikely(!value))
38 return ERR_PTR(-ENOMEM);
39 size = __hfsplus_getxattr(inode, xattr_name, value, size);
40 }
41
42 if (size > 0)
43 acl = posix_acl_from_xattr(&init_user_ns, value, size);
44 else if (size == -ENODATA)
45 acl = NULL;
46 else
47 acl = ERR_PTR(size);
48
49 hfsplus_destroy_attr_entry((hfsplus_attr_entry *)value);
50
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -070051 return acl;
52}
53
Christoph Hellwigb0a7ab52013-12-20 05:16:46 -080054int hfsplus_set_posix_acl(struct inode *inode, struct posix_acl *acl,
55 int type)
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -070056{
57 int err;
58 char *xattr_name;
59 size_t size = 0;
60 char *value = NULL;
61
Christoph Hellwigb0a7ab52013-12-20 05:16:46 -080062 hfs_dbg(ACL_MOD, "[%s]: ino %lu\n", __func__, inode->i_ino);
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -070063
64 switch (type) {
65 case ACL_TYPE_ACCESS:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010066 xattr_name = XATTR_NAME_POSIX_ACL_ACCESS;
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -070067 if (acl) {
Jan Kara07393102016-09-19 17:39:09 +020068 err = posix_acl_update_mode(inode, &inode->i_mode, &acl);
69 if (err)
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -070070 return err;
71 }
72 err = 0;
73 break;
74
75 case ACL_TYPE_DEFAULT:
Andreas Gruenbacher97d79292015-12-02 14:44:35 +010076 xattr_name = XATTR_NAME_POSIX_ACL_DEFAULT;
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -070077 if (!S_ISDIR(inode->i_mode))
78 return acl ? -EACCES : 0;
79 break;
80
81 default:
82 return -EINVAL;
83 }
84
85 if (acl) {
86 size = posix_acl_xattr_size(acl->a_count);
87 if (unlikely(size > HFSPLUS_MAX_INLINE_DATA_SIZE))
88 return -ENOMEM;
89 value = (char *)hfsplus_alloc_attr_entry();
90 if (unlikely(!value))
91 return -ENOMEM;
92 err = posix_acl_to_xattr(&init_user_ns, acl, value, size);
93 if (unlikely(err < 0))
94 goto end_set_acl;
95 }
96
97 err = __hfsplus_setxattr(inode, xattr_name, value, size, 0);
98
99end_set_acl:
100 hfsplus_destroy_attr_entry((hfsplus_attr_entry *)value);
101
102 if (!err)
103 set_cached_acl(inode, type, acl);
104
105 return err;
106}
107
108int hfsplus_init_posix_acl(struct inode *inode, struct inode *dir)
109{
110 int err = 0;
Christoph Hellwigb0a7ab52013-12-20 05:16:46 -0800111 struct posix_acl *default_acl, *acl;
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -0700112
113 hfs_dbg(ACL_MOD,
114 "[%s]: ino %lu, dir->ino %lu\n",
115 __func__, inode->i_ino, dir->i_ino);
116
117 if (S_ISLNK(inode->i_mode))
118 return 0;
119
Christoph Hellwigb0a7ab52013-12-20 05:16:46 -0800120 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
121 if (err)
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -0700122 return err;
123
Christoph Hellwigb0a7ab52013-12-20 05:16:46 -0800124 if (default_acl) {
125 err = hfsplus_set_posix_acl(inode, default_acl,
126 ACL_TYPE_DEFAULT);
127 posix_acl_release(default_acl);
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -0700128 }
129
Christoph Hellwigb0a7ab52013-12-20 05:16:46 -0800130 if (acl) {
131 if (!err)
132 err = hfsplus_set_posix_acl(inode, acl,
133 ACL_TYPE_ACCESS);
134 posix_acl_release(acl);
135 }
Vyacheslav Dubeykoeef80d42013-09-11 14:24:29 -0700136 return err;
137}