blob: 55458031e501405b06371b04f958323d44e12549 [file] [log] [blame]
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -07001/*
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -07002 * (C) 2005 Andreas Gruenbacher <agruen@suse.de>
3 *
4 * This file is released under the GPL.
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01005 *
6 * Generic ACL support for in-memory filesystems.
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -07007 */
8
9#include <linux/sched.h>
10#include <linux/fs.h>
11#include <linux/generic_acl.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010012#include <linux/posix_acl.h>
13#include <linux/posix_acl_xattr.h>
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070014
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010015
16static size_t
17generic_acl_list(struct dentry *dentry, char *list, size_t list_size,
18 const char *name, size_t name_len, int type)
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070019{
20 struct posix_acl *acl;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010021 const char *xname;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070022 size_t size;
23
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010024 acl = get_cached_acl(dentry->d_inode, type);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070025 if (!acl)
26 return 0;
27 posix_acl_release(acl);
28
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010029 switch (type) {
30 case ACL_TYPE_ACCESS:
31 xname = POSIX_ACL_XATTR_ACCESS;
32 break;
33 case ACL_TYPE_DEFAULT:
34 xname = POSIX_ACL_XATTR_DEFAULT;
35 break;
36 default:
37 return 0;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070038 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010039 size = strlen(xname) + 1;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070040 if (list && size <= list_size)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010041 memcpy(list, xname, size);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070042 return size;
43}
44
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010045static int
46generic_acl_get(struct dentry *dentry, const char *name, void *buffer,
47 size_t size, int type)
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070048{
49 struct posix_acl *acl;
50 int error;
51
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010052 if (strcmp(name, "") != 0)
53 return -EINVAL;
54
55 acl = get_cached_acl(dentry->d_inode, type);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070056 if (!acl)
57 return -ENODATA;
58 error = posix_acl_to_xattr(acl, buffer, size);
59 posix_acl_release(acl);
60
61 return error;
62}
63
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010064static int
65generic_acl_set(struct dentry *dentry, const char *name, const void *value,
66 size_t size, int flags, int type)
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070067{
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010068 struct inode *inode = dentry->d_inode;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070069 struct posix_acl *acl = NULL;
70 int error;
71
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010072 if (strcmp(name, "") != 0)
73 return -EINVAL;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070074 if (S_ISLNK(inode->i_mode))
75 return -EOPNOTSUPP;
Satyam Sharma3bd858a2007-07-17 15:00:08 +053076 if (!is_owner_or_cap(inode))
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070077 return -EPERM;
78 if (value) {
79 acl = posix_acl_from_xattr(value, size);
80 if (IS_ERR(acl))
81 return PTR_ERR(acl);
82 }
83 if (acl) {
84 mode_t mode;
85
86 error = posix_acl_valid(acl);
87 if (error)
88 goto failed;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010089 switch (type) {
90 case ACL_TYPE_ACCESS:
91 mode = inode->i_mode;
92 error = posix_acl_equiv_mode(acl, &mode);
93 if (error < 0)
94 goto failed;
95 inode->i_mode = mode;
96 if (error == 0) {
97 posix_acl_release(acl);
98 acl = NULL;
99 }
100 break;
101 case ACL_TYPE_DEFAULT:
102 if (!S_ISDIR(inode->i_mode)) {
103 error = -EINVAL;
104 goto failed;
105 }
106 break;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700107 }
108 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100109 set_cached_acl(inode, type, acl);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700110 error = 0;
111failed:
112 posix_acl_release(acl);
113 return error;
114}
115
116/**
117 * generic_acl_init - Take care of acl inheritance at @inode create time
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700118 *
119 * Files created inside a directory with a default ACL inherit the
120 * directory's default ACL.
121 */
122int
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100123generic_acl_init(struct inode *inode, struct inode *dir)
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700124{
125 struct posix_acl *acl = NULL;
126 mode_t mode = inode->i_mode;
127 int error;
128
Al Viroce3b0f82009-03-29 19:08:22 -0400129 inode->i_mode = mode & ~current_umask();
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700130 if (!S_ISLNK(inode->i_mode))
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100131 acl = get_cached_acl(dir, ACL_TYPE_DEFAULT);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700132 if (acl) {
133 struct posix_acl *clone;
134
135 if (S_ISDIR(inode->i_mode)) {
136 clone = posix_acl_clone(acl, GFP_KERNEL);
137 error = -ENOMEM;
138 if (!clone)
139 goto cleanup;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100140 set_cached_acl(inode, ACL_TYPE_DEFAULT, clone);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700141 posix_acl_release(clone);
142 }
143 clone = posix_acl_clone(acl, GFP_KERNEL);
144 error = -ENOMEM;
145 if (!clone)
146 goto cleanup;
147 error = posix_acl_create_masq(clone, &mode);
148 if (error >= 0) {
149 inode->i_mode = mode;
150 if (error > 0)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100151 set_cached_acl(inode, ACL_TYPE_ACCESS, clone);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700152 }
153 posix_acl_release(clone);
154 }
155 error = 0;
156
157cleanup:
158 posix_acl_release(acl);
159 return error;
160}
161
162/**
163 * generic_acl_chmod - change the access acl of @inode upon chmod()
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700164 *
165 * A chmod also changes the permissions of the owner, group/mask, and
166 * other ACL entries.
167 */
168int
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100169generic_acl_chmod(struct inode *inode)
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700170{
171 struct posix_acl *acl, *clone;
172 int error = 0;
173
174 if (S_ISLNK(inode->i_mode))
175 return -EOPNOTSUPP;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100176 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700177 if (acl) {
178 clone = posix_acl_clone(acl, GFP_KERNEL);
179 posix_acl_release(acl);
180 if (!clone)
181 return -ENOMEM;
182 error = posix_acl_chmod_masq(clone, inode->i_mode);
183 if (!error)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100184 set_cached_acl(inode, ACL_TYPE_ACCESS, clone);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700185 posix_acl_release(clone);
186 }
187 return error;
188}
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100189
190int
191generic_check_acl(struct inode *inode, int mask)
192{
193 struct posix_acl *acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
194
195 if (acl) {
196 int error = posix_acl_permission(inode, acl, mask);
197 posix_acl_release(acl);
198 return error;
199 }
200 return -EAGAIN;
201}
202
203struct xattr_handler generic_acl_access_handler = {
204 .prefix = POSIX_ACL_XATTR_ACCESS,
205 .flags = ACL_TYPE_ACCESS,
206 .list = generic_acl_list,
207 .get = generic_acl_get,
208 .set = generic_acl_set,
209};
210
211struct xattr_handler generic_acl_default_handler = {
212 .prefix = POSIX_ACL_XATTR_DEFAULT,
213 .flags = ACL_TYPE_DEFAULT,
214 .list = generic_acl_list,
215 .get = generic_acl_get,
216 .set = generic_acl_set,
217};