blob: e9c0746e82050a9668127c039cc42867f380bb01 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/gfp.h>
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070011#include <linux/fs.h>
12#include <linux/generic_acl.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010013#include <linux/posix_acl.h>
14#include <linux/posix_acl_xattr.h>
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070015
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010016
17static size_t
18generic_acl_list(struct dentry *dentry, char *list, size_t list_size,
19 const char *name, size_t name_len, int type)
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070020{
21 struct posix_acl *acl;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010022 const char *xname;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070023 size_t size;
24
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010025 acl = get_cached_acl(dentry->d_inode, type);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070026 if (!acl)
27 return 0;
28 posix_acl_release(acl);
29
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010030 switch (type) {
31 case ACL_TYPE_ACCESS:
32 xname = POSIX_ACL_XATTR_ACCESS;
33 break;
34 case ACL_TYPE_DEFAULT:
35 xname = POSIX_ACL_XATTR_DEFAULT;
36 break;
37 default:
38 return 0;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070039 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010040 size = strlen(xname) + 1;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070041 if (list && size <= list_size)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010042 memcpy(list, xname, size);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070043 return size;
44}
45
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010046static int
47generic_acl_get(struct dentry *dentry, const char *name, void *buffer,
48 size_t size, int type)
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070049{
50 struct posix_acl *acl;
51 int error;
52
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010053 if (strcmp(name, "") != 0)
54 return -EINVAL;
55
56 acl = get_cached_acl(dentry->d_inode, type);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070057 if (!acl)
58 return -ENODATA;
59 error = posix_acl_to_xattr(acl, buffer, size);
60 posix_acl_release(acl);
61
62 return error;
63}
64
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010065static int
66generic_acl_set(struct dentry *dentry, const char *name, const void *value,
67 size_t size, int flags, int type)
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070068{
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010069 struct inode *inode = dentry->d_inode;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070070 struct posix_acl *acl = NULL;
71 int error;
72
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010073 if (strcmp(name, "") != 0)
74 return -EINVAL;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070075 if (S_ISLNK(inode->i_mode))
76 return -EOPNOTSUPP;
Serge E. Hallyn2e149672011-03-23 16:43:26 -070077 if (!inode_owner_or_capable(inode))
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070078 return -EPERM;
79 if (value) {
80 acl = posix_acl_from_xattr(value, size);
81 if (IS_ERR(acl))
82 return PTR_ERR(acl);
83 }
84 if (acl) {
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -070085 error = posix_acl_valid(acl);
86 if (error)
87 goto failed;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010088 switch (type) {
89 case ACL_TYPE_ACCESS:
Jan Kara9451fcb2016-09-19 17:39:09 +020090 error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
91 if (error)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010092 goto failed;
Jan Karadad5eb62010-08-17 12:42:13 +020093 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010094 break;
95 case ACL_TYPE_DEFAULT:
96 if (!S_ISDIR(inode->i_mode)) {
97 error = -EINVAL;
98 goto failed;
99 }
100 break;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700101 }
102 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100103 set_cached_acl(inode, type, acl);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700104 error = 0;
105failed:
106 posix_acl_release(acl);
107 return error;
108}
109
110/**
111 * generic_acl_init - Take care of acl inheritance at @inode create time
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700112 *
113 * Files created inside a directory with a default ACL inherit the
114 * directory's default ACL.
115 */
116int
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100117generic_acl_init(struct inode *inode, struct inode *dir)
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700118{
119 struct posix_acl *acl = NULL;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700120 int error;
121
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700122 if (!S_ISLNK(inode->i_mode))
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100123 acl = get_cached_acl(dir, ACL_TYPE_DEFAULT);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700124 if (acl) {
Al Viro95203be2011-07-23 02:41:54 -0400125 if (S_ISDIR(inode->i_mode))
126 set_cached_acl(inode, ACL_TYPE_DEFAULT, acl);
Al Virod3fb6122011-07-23 18:37:50 -0400127 error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
Al Viro826cae22011-07-23 03:10:32 -0400128 if (error < 0)
129 return error;
Al Viro826cae22011-07-23 03:10:32 -0400130 if (error > 0)
131 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
Al Virod3fb6122011-07-23 18:37:50 -0400132 } else {
133 inode->i_mode &= ~current_umask();
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700134 }
135 error = 0;
136
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700137 posix_acl_release(acl);
138 return error;
139}
140
141/**
142 * generic_acl_chmod - change the access acl of @inode upon chmod()
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700143 *
144 * A chmod also changes the permissions of the owner, group/mask, and
145 * other ACL entries.
146 */
147int
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100148generic_acl_chmod(struct inode *inode)
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700149{
Al Virobc26ab52011-07-23 00:18:02 -0400150 struct posix_acl *acl;
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700151 int error = 0;
152
153 if (S_ISLNK(inode->i_mode))
154 return -EOPNOTSUPP;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100155 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700156 if (acl) {
Al Virobc26ab52011-07-23 00:18:02 -0400157 error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
158 if (error)
159 return error;
160 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700161 posix_acl_release(acl);
Andreas Gruenbacherf0c8bd12006-09-29 02:01:34 -0700162 }
163 return error;
164}
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100165
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700166const struct xattr_handler generic_acl_access_handler = {
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100167 .prefix = POSIX_ACL_XATTR_ACCESS,
168 .flags = ACL_TYPE_ACCESS,
169 .list = generic_acl_list,
170 .get = generic_acl_get,
171 .set = generic_acl_set,
172};
173
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700174const struct xattr_handler generic_acl_default_handler = {
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100175 .prefix = POSIX_ACL_XATTR_DEFAULT,
176 .flags = ACL_TYPE_DEFAULT,
177 .list = generic_acl_list,
178 .get = generic_acl_get,
179 .set = generic_acl_set,
180};