blob: 09441ae07a5be9de1f18cdc01d896808ba1e02be [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/acl.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 */
6
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04007#include <linux/quotaops.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -04008#include "ext4_jbd2.h"
9#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070010#include "xattr.h"
11#include "acl.h"
12
13/*
14 * Convert from filesystem to in-memory representation.
15 */
16static struct posix_acl *
Mingming Cao617ba132006-10-11 01:20:53 -070017ext4_acl_from_disk(const void *value, size_t size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070018{
19 const char *end = (char *)value + size;
20 int n, count;
21 struct posix_acl *acl;
22
23 if (!value)
24 return NULL;
Mingming Cao617ba132006-10-11 01:20:53 -070025 if (size < sizeof(ext4_acl_header))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070026 return ERR_PTR(-EINVAL);
Mingming Cao617ba132006-10-11 01:20:53 -070027 if (((ext4_acl_header *)value)->a_version !=
28 cpu_to_le32(EXT4_ACL_VERSION))
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029 return ERR_PTR(-EINVAL);
Mingming Cao617ba132006-10-11 01:20:53 -070030 value = (char *)value + sizeof(ext4_acl_header);
31 count = ext4_acl_count(size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032 if (count < 0)
33 return ERR_PTR(-EINVAL);
34 if (count == 0)
35 return NULL;
Josef Bacik216553c2008-04-29 22:02:02 -040036 acl = posix_acl_alloc(count, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070037 if (!acl)
38 return ERR_PTR(-ENOMEM);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040039 for (n = 0; n < count; n++) {
Mingming Cao617ba132006-10-11 01:20:53 -070040 ext4_acl_entry *entry =
41 (ext4_acl_entry *)value;
42 if ((char *)value + sizeof(ext4_acl_entry_short) > end)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070043 goto fail;
44 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
45 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070046
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040047 switch (acl->a_entries[n].e_tag) {
48 case ACL_USER_OBJ:
49 case ACL_GROUP_OBJ:
50 case ACL_MASK:
51 case ACL_OTHER:
52 value = (char *)value +
53 sizeof(ext4_acl_entry_short);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040054 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070055
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040056 case ACL_USER:
Eric W. Biedermanaf84df92012-09-10 20:44:54 -070057 value = (char *)value + sizeof(ext4_acl_entry);
58 if ((char *)value > end)
59 goto fail;
60 acl->a_entries[n].e_uid =
61 make_kuid(&init_user_ns,
62 le32_to_cpu(entry->e_id));
63 break;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040064 case ACL_GROUP:
65 value = (char *)value + sizeof(ext4_acl_entry);
66 if ((char *)value > end)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070067 goto fail;
Eric W. Biedermanaf84df92012-09-10 20:44:54 -070068 acl->a_entries[n].e_gid =
69 make_kgid(&init_user_ns,
70 le32_to_cpu(entry->e_id));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040071 break;
72
73 default:
74 goto fail;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070075 }
76 }
77 if (value != end)
78 goto fail;
79 return acl;
80
81fail:
82 posix_acl_release(acl);
83 return ERR_PTR(-EINVAL);
84}
85
86/*
87 * Convert from in-memory to filesystem representation.
88 */
89static void *
Mingming Cao617ba132006-10-11 01:20:53 -070090ext4_acl_to_disk(const struct posix_acl *acl, size_t *size)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070091{
Mingming Cao617ba132006-10-11 01:20:53 -070092 ext4_acl_header *ext_acl;
Dave Kleikampac27a0e2006-10-11 01:20:50 -070093 char *e;
94 size_t n;
95
Mingming Cao617ba132006-10-11 01:20:53 -070096 *size = ext4_acl_size(acl->a_count);
97 ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count *
Josef Bacik216553c2008-04-29 22:02:02 -040098 sizeof(ext4_acl_entry), GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070099 if (!ext_acl)
100 return ERR_PTR(-ENOMEM);
Mingming Cao617ba132006-10-11 01:20:53 -0700101 ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION);
102 e = (char *)ext_acl + sizeof(ext4_acl_header);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400103 for (n = 0; n < acl->a_count; n++) {
Eric W. Biedermanaf84df92012-09-10 20:44:54 -0700104 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
Mingming Cao617ba132006-10-11 01:20:53 -0700105 ext4_acl_entry *entry = (ext4_acl_entry *)e;
Eric W. Biedermanaf84df92012-09-10 20:44:54 -0700106 entry->e_tag = cpu_to_le16(acl_e->e_tag);
107 entry->e_perm = cpu_to_le16(acl_e->e_perm);
108 switch (acl_e->e_tag) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400109 case ACL_USER:
Eric W. Biedermanaf84df92012-09-10 20:44:54 -0700110 entry->e_id = cpu_to_le32(
111 from_kuid(&init_user_ns, acl_e->e_uid));
112 e += sizeof(ext4_acl_entry);
113 break;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400114 case ACL_GROUP:
Eric W. Biedermanaf84df92012-09-10 20:44:54 -0700115 entry->e_id = cpu_to_le32(
116 from_kgid(&init_user_ns, acl_e->e_gid));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400117 e += sizeof(ext4_acl_entry);
118 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700119
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400120 case ACL_USER_OBJ:
121 case ACL_GROUP_OBJ:
122 case ACL_MASK:
123 case ACL_OTHER:
124 e += sizeof(ext4_acl_entry_short);
125 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700126
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400127 default:
128 goto fail;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700129 }
130 }
131 return (char *)ext_acl;
132
133fail:
134 kfree(ext_acl);
135 return ERR_PTR(-EINVAL);
136}
137
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700138/*
139 * Inode operation get_posix_acl().
140 *
141 * inode->i_mutex: don't care
142 */
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200143struct posix_acl *
Mingming Cao617ba132006-10-11 01:20:53 -0700144ext4_get_acl(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700145{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146 int name_index;
147 char *value = NULL;
148 struct posix_acl *acl;
149 int retval;
150
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400151 switch (type) {
152 case ACL_TYPE_ACCESS:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400153 name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
154 break;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400155 case ACL_TYPE_DEFAULT:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400156 name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
157 break;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400158 default:
Al Viro073aaa12009-06-09 12:11:54 -0400159 BUG();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700160 }
Mingming Cao617ba132006-10-11 01:20:53 -0700161 retval = ext4_xattr_get(inode, name_index, "", NULL, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700162 if (retval > 0) {
Josef Bacik216553c2008-04-29 22:02:02 -0400163 value = kmalloc(retval, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700164 if (!value)
165 return ERR_PTR(-ENOMEM);
Mingming Cao617ba132006-10-11 01:20:53 -0700166 retval = ext4_xattr_get(inode, name_index, "", value, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700167 }
168 if (retval > 0)
Mingming Cao617ba132006-10-11 01:20:53 -0700169 acl = ext4_acl_from_disk(value, retval);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170 else if (retval == -ENODATA || retval == -ENOSYS)
171 acl = NULL;
172 else
173 acl = ERR_PTR(retval);
174 kfree(value);
175
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176 return acl;
177}
178
179/*
180 * Set the access or default ACL of an inode.
181 *
Mingming Cao617ba132006-10-11 01:20:53 -0700182 * inode->i_mutex: down unless called from ext4_new_inode
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183 */
184static int
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800185__ext4_set_acl(handle_t *handle, struct inode *inode, int type,
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400186 struct posix_acl *acl, int xattr_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700188 int name_index;
189 void *value = NULL;
190 size_t size = 0;
191 int error;
192
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400193 switch (type) {
194 case ACL_TYPE_ACCESS:
195 name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
196 if (acl) {
Jan Kara07393102016-09-19 17:39:09 +0200197 error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
198 if (error)
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400199 return error;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500200 inode->i_ctime = current_time(inode);
Jan Kara07393102016-09-19 17:39:09 +0200201 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400202 }
203 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700204
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400205 case ACL_TYPE_DEFAULT:
206 name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
207 if (!S_ISDIR(inode->i_mode))
208 return acl ? -EACCES : 0;
209 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400211 default:
212 return -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213 }
214 if (acl) {
Mingming Cao617ba132006-10-11 01:20:53 -0700215 value = ext4_acl_to_disk(acl, &size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700216 if (IS_ERR(value))
217 return (int)PTR_ERR(value);
218 }
219
Mingming Cao617ba132006-10-11 01:20:53 -0700220 error = ext4_xattr_set_handle(handle, inode, name_index, "",
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400221 value, size, xattr_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700222
223 kfree(value);
Al Viro073aaa12009-06-09 12:11:54 -0400224 if (!error)
225 set_cached_acl(inode, type, acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700227 return error;
228}
229
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800230int
231ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type)
232{
233 handle_t *handle;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -0400234 int error, credits, retries = 0;
235 size_t acl_size = acl ? ext4_acl_size(acl->a_count) : 0;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800236
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -0400237 error = dquot_initialize(inode);
238 if (error)
239 return error;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800240retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400241 error = ext4_xattr_set_credits(inode, acl_size, false /* is_create */,
242 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400243 if (error)
244 return error;
245
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -0400246 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800247 if (IS_ERR(handle))
248 return PTR_ERR(handle);
249
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400250 error = __ext4_set_acl(handle, inode, type, acl, 0 /* xattr_flags */);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800251 ext4_journal_stop(handle);
252 if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
253 goto retry;
254 return error;
255}
256
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257/*
Mingming Cao617ba132006-10-11 01:20:53 -0700258 * Initialize the ACLs of a new inode. Called from ext4_new_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700259 *
260 * dir->i_mutex: down
261 * inode->i_mutex: up (access to inode is still exclusive)
262 */
263int
Mingming Cao617ba132006-10-11 01:20:53 -0700264ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265{
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800266 struct posix_acl *default_acl, *acl;
Andrew Morton63f57932006-10-11 01:21:24 -0700267 int error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800269 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
Al Virobc26ab52011-07-23 00:18:02 -0400270 if (error)
271 return error;
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800272
273 if (default_acl) {
274 error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT,
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400275 default_acl, XATTR_CREATE);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800276 posix_acl_release(default_acl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700277 }
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800278 if (acl) {
279 if (!error)
280 error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS,
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400281 acl, XATTR_CREATE);
Christoph Hellwig64e178a2013-12-20 05:16:44 -0800282 posix_acl_release(acl);
Eugene Shatokhin24ec19b2012-11-08 15:11:11 -0500283 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700284 return error;
285}