License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / fs / hfsplus / posix_acl.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
eef80d4a
VD
2/*
3 * linux/fs/hfsplus/posix_acl.c
4 *
5 * Vyacheslav Dubeyko <slava@dubeyko.com>
6 *
7 * Handler for Posix Access Control Lists (ACLs) support.
8 */
9
10#include "hfsplus_fs.h"
11#include "xattr.h"
12#include "acl.h"
13
14struct posix_acl *hfsplus_get_posix_acl(struct inode *inode, int type)
15{
16 struct posix_acl *acl;
17 char *xattr_name;
18 char *value = NULL;
19 ssize_t size;
20
b0a7ab57 21 hfs_dbg(ACL_MOD, "[%s]: ino %lu\n", __func__, inode->i_ino);
eef80d4a
VD
22
23 switch (type) {
24 case ACL_TYPE_ACCESS:
97d79299 25 xattr_name = XATTR_NAME_POSIX_ACL_ACCESS;
eef80d4a
VD
26 break;
27 case ACL_TYPE_DEFAULT:
97d79299 28 xattr_name = XATTR_NAME_POSIX_ACL_DEFAULT;
eef80d4a
VD
29 break;
30 default:
31 return ERR_PTR(-EINVAL);
32 }
33
34 size = __hfsplus_getxattr(inode, xattr_name, NULL, 0);
35
36 if (size > 0) {
37 value = (char *)hfsplus_alloc_attr_entry();
38 if (unlikely(!value))
39 return ERR_PTR(-ENOMEM);
40 size = __hfsplus_getxattr(inode, xattr_name, value, size);
41 }
42
43 if (size > 0)
44 acl = posix_acl_from_xattr(&init_user_ns, value, size);
45 else if (size == -ENODATA)
46 acl = NULL;
47 else
48 acl = ERR_PTR(size);
49
50 hfsplus_destroy_attr_entry((hfsplus_attr_entry *)value);
51
eef80d4a
VD
52 return acl;
53}
54
84969465
JK
55static int __hfsplus_set_posix_acl(struct inode *inode, struct posix_acl *acl,
56 int type)
eef80d4a
VD
57{
58 int err;
59 char *xattr_name;
60 size_t size = 0;
61 char *value = NULL;
62
b0a7ab57 63 hfs_dbg(ACL_MOD, "[%s]: ino %lu\n", __func__, inode->i_ino);
eef80d4a
VD
64
65 switch (type) {
66 case ACL_TYPE_ACCESS:
97d79299 67 xattr_name = XATTR_NAME_POSIX_ACL_ACCESS;
eef80d4a
VD
68 break;
69
70 case ACL_TYPE_DEFAULT:
97d79299 71 xattr_name = XATTR_NAME_POSIX_ACL_DEFAULT;
eef80d4a
VD
72 if (!S_ISDIR(inode->i_mode))
73 return acl ? -EACCES : 0;
74 break;
75
76 default:
77 return -EINVAL;
78 }
79
80 if (acl) {
81 size = posix_acl_xattr_size(acl->a_count);
82 if (unlikely(size > HFSPLUS_MAX_INLINE_DATA_SIZE))
83 return -ENOMEM;
84 value = (char *)hfsplus_alloc_attr_entry();
85 if (unlikely(!value))
86 return -ENOMEM;
87 err = posix_acl_to_xattr(&init_user_ns, acl, value, size);
88 if (unlikely(err < 0))
89 goto end_set_acl;
90 }
91
92 err = __hfsplus_setxattr(inode, xattr_name, value, size, 0);
93
94end_set_acl:
95 hfsplus_destroy_attr_entry((hfsplus_attr_entry *)value);
96
97 if (!err)
98 set_cached_acl(inode, type, acl);
99
100 return err;
101}
102
84969465
JK
103int hfsplus_set_posix_acl(struct inode *inode, struct posix_acl *acl, int type)
104{
105 int err;
106
107 if (type == ACL_TYPE_ACCESS && acl) {
108 err = posix_acl_update_mode(inode, &inode->i_mode, &acl);
109 if (err)
110 return err;
111 }
112 return __hfsplus_set_posix_acl(inode, acl, type);
113}
114
eef80d4a
VD
115int hfsplus_init_posix_acl(struct inode *inode, struct inode *dir)
116{
117 int err = 0;
b0a7ab57 118 struct posix_acl *default_acl, *acl;
eef80d4a
VD
119
120 hfs_dbg(ACL_MOD,
121 "[%s]: ino %lu, dir->ino %lu\n",
122 __func__, inode->i_ino, dir->i_ino);
123
124 if (S_ISLNK(inode->i_mode))
125 return 0;
126
b0a7ab57
CH
127 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
128 if (err)
eef80d4a
VD
129 return err;
130
b0a7ab57 131 if (default_acl) {
84969465
JK
132 err = __hfsplus_set_posix_acl(inode, default_acl,
133 ACL_TYPE_DEFAULT);
b0a7ab57 134 posix_acl_release(default_acl);
eef80d4a
VD
135 }
136
b0a7ab57
CH
137 if (acl) {
138 if (!err)
84969465
JK
139 err = __hfsplus_set_posix_acl(inode, acl,
140 ACL_TYPE_ACCESS);
b0a7ab57
CH
141 posix_acl_release(acl);
142 }
eef80d4a
VD
143 return err;
144}