blob: a76e0aa5cd3fc5188a392639618858a7e7e34092 [file] [log] [blame]
Tiger Yang929fb012008-11-14 11:17:04 +08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * acl.c
5 *
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
7 *
8 * CREDITS:
9 * Lots of code in this file is copy from linux/fs/ext3/acl.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 */
21
22#include <linux/init.h>
23#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Tiger Yang929fb012008-11-14 11:17:04 +080025#include <linux/string.h>
26
27#define MLOG_MASK_PREFIX ML_INODE
28#include <cluster/masklog.h>
29
30#include "ocfs2.h"
31#include "alloc.h"
32#include "dlmglue.h"
33#include "file.h"
Mark Fashehfcefd252010-03-15 15:39:00 -070034#include "inode.h"
35#include "journal.h"
Tiger Yang929fb012008-11-14 11:17:04 +080036#include "ocfs2_fs.h"
37
38#include "xattr.h"
39#include "acl.h"
40
41/*
42 * Convert from xattr value to acl struct.
43 */
44static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
45{
46 int n, count;
47 struct posix_acl *acl;
48
49 if (!value)
50 return NULL;
51 if (size < sizeof(struct posix_acl_entry))
52 return ERR_PTR(-EINVAL);
53
54 count = size / sizeof(struct posix_acl_entry);
55 if (count < 0)
56 return ERR_PTR(-EINVAL);
57 if (count == 0)
58 return NULL;
59
60 acl = posix_acl_alloc(count, GFP_NOFS);
61 if (!acl)
62 return ERR_PTR(-ENOMEM);
63 for (n = 0; n < count; n++) {
64 struct ocfs2_acl_entry *entry =
65 (struct ocfs2_acl_entry *)value;
66
67 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
68 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
69 acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
70 value += sizeof(struct posix_acl_entry);
71
72 }
73 return acl;
74}
75
76/*
77 * Convert acl struct to xattr value.
78 */
79static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
80{
81 struct ocfs2_acl_entry *entry = NULL;
82 char *ocfs2_acl;
83 size_t n;
84
85 *size = acl->a_count * sizeof(struct posix_acl_entry);
86
87 ocfs2_acl = kmalloc(*size, GFP_NOFS);
88 if (!ocfs2_acl)
89 return ERR_PTR(-ENOMEM);
90
91 entry = (struct ocfs2_acl_entry *)ocfs2_acl;
92 for (n = 0; n < acl->a_count; n++, entry++) {
93 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
94 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
95 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
96 }
97 return ocfs2_acl;
98}
99
100static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
101 int type,
102 struct buffer_head *di_bh)
103{
Tiger Yang929fb012008-11-14 11:17:04 +0800104 int name_index;
105 char *value = NULL;
106 struct posix_acl *acl;
107 int retval;
108
Tiger Yang929fb012008-11-14 11:17:04 +0800109 switch (type) {
110 case ACL_TYPE_ACCESS:
111 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
112 break;
113 case ACL_TYPE_DEFAULT:
114 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
115 break;
116 default:
117 return ERR_PTR(-EINVAL);
118 }
119
120 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
121 if (retval > 0) {
122 value = kmalloc(retval, GFP_NOFS);
123 if (!value)
124 return ERR_PTR(-ENOMEM);
125 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
126 "", value, retval);
127 }
128
129 if (retval > 0)
130 acl = ocfs2_acl_from_xattr(value, retval);
131 else if (retval == -ENODATA || retval == 0)
132 acl = NULL;
133 else
134 acl = ERR_PTR(retval);
135
136 kfree(value);
137
138 return acl;
139}
140
141
142/*
143 * Get posix acl.
144 */
145static struct posix_acl *ocfs2_get_acl(struct inode *inode, int type)
146{
147 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
148 struct buffer_head *di_bh = NULL;
149 struct posix_acl *acl;
150 int ret;
151
152 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
153 return NULL;
154
155 ret = ocfs2_inode_lock(inode, &di_bh, 0);
156 if (ret < 0) {
157 mlog_errno(ret);
158 acl = ERR_PTR(ret);
159 return acl;
160 }
161
162 acl = ocfs2_get_acl_nolock(inode, type, di_bh);
163
164 ocfs2_inode_unlock(inode, 0);
165
166 brelse(di_bh);
167
168 return acl;
169}
170
171/*
Mark Fashehfcefd252010-03-15 15:39:00 -0700172 * Helper function to set i_mode in memory and disk. Some call paths
173 * will not have di_bh or a journal handle to pass, in which case it
174 * will create it's own.
175 */
176static int ocfs2_acl_set_mode(struct inode *inode, struct buffer_head *di_bh,
177 handle_t *handle, umode_t new_mode)
178{
179 int ret, commit_handle = 0;
180 struct ocfs2_dinode *di;
181
182 if (di_bh == NULL) {
183 ret = ocfs2_read_inode_block(inode, &di_bh);
184 if (ret) {
185 mlog_errno(ret);
186 goto out;
187 }
188 } else
189 get_bh(di_bh);
190
191 if (handle == NULL) {
192 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
193 OCFS2_INODE_UPDATE_CREDITS);
194 if (IS_ERR(handle)) {
195 ret = PTR_ERR(handle);
196 mlog_errno(ret);
197 goto out_brelse;
198 }
199
200 commit_handle = 1;
201 }
202
203 di = (struct ocfs2_dinode *)di_bh->b_data;
204 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
205 OCFS2_JOURNAL_ACCESS_WRITE);
206 if (ret) {
207 mlog_errno(ret);
208 goto out_commit;
209 }
210
211 inode->i_mode = new_mode;
212 di->i_mode = cpu_to_le16(inode->i_mode);
213
214 ocfs2_journal_dirty(handle, di_bh);
215
216out_commit:
217 if (commit_handle)
218 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
219out_brelse:
220 brelse(di_bh);
221out:
222 return ret;
223}
224
225/*
Tiger Yang929fb012008-11-14 11:17:04 +0800226 * Set the access or default ACL of an inode.
227 */
228static int ocfs2_set_acl(handle_t *handle,
229 struct inode *inode,
230 struct buffer_head *di_bh,
231 int type,
232 struct posix_acl *acl,
233 struct ocfs2_alloc_context *meta_ac,
234 struct ocfs2_alloc_context *data_ac)
235{
236 int name_index;
237 void *value = NULL;
238 size_t size = 0;
239 int ret;
240
241 if (S_ISLNK(inode->i_mode))
242 return -EOPNOTSUPP;
243
244 switch (type) {
245 case ACL_TYPE_ACCESS:
246 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
247 if (acl) {
248 mode_t mode = inode->i_mode;
249 ret = posix_acl_equiv_mode(acl, &mode);
250 if (ret < 0)
251 return ret;
252 else {
Tiger Yang929fb012008-11-14 11:17:04 +0800253 if (ret == 0)
254 acl = NULL;
Mark Fashehfcefd252010-03-15 15:39:00 -0700255
256 ret = ocfs2_acl_set_mode(inode, di_bh,
257 handle, mode);
258 if (ret)
259 return ret;
260
Tiger Yang929fb012008-11-14 11:17:04 +0800261 }
262 }
263 break;
264 case ACL_TYPE_DEFAULT:
265 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
266 if (!S_ISDIR(inode->i_mode))
267 return acl ? -EACCES : 0;
268 break;
269 default:
270 return -EINVAL;
271 }
272
273 if (acl) {
274 value = ocfs2_acl_to_xattr(acl, &size);
275 if (IS_ERR(value))
276 return (int)PTR_ERR(value);
277 }
278
279 if (handle)
280 ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
281 "", value, size, 0,
282 meta_ac, data_ac);
283 else
284 ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
285
286 kfree(value);
287
288 return ret;
289}
290
Tiger Yang23fc2702008-11-14 11:17:18 +0800291int ocfs2_check_acl(struct inode *inode, int mask)
292{
Jiaju Zhang845b6cf2010-07-28 13:21:06 +0800293 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
294 struct buffer_head *di_bh = NULL;
295 struct posix_acl *acl;
296 int ret = -EAGAIN;
Tiger Yang23fc2702008-11-14 11:17:18 +0800297
Jiaju Zhang845b6cf2010-07-28 13:21:06 +0800298 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
299 return ret;
300
301 ret = ocfs2_read_inode_block(inode, &di_bh);
302 if (ret < 0) {
303 mlog_errno(ret);
304 return ret;
305 }
306
307 acl = ocfs2_get_acl_nolock(inode, ACL_TYPE_ACCESS, di_bh);
308
309 brelse(di_bh);
310
311 if (IS_ERR(acl)) {
312 mlog_errno(PTR_ERR(acl));
Tiger Yang23fc2702008-11-14 11:17:18 +0800313 return PTR_ERR(acl);
Jiaju Zhang845b6cf2010-07-28 13:21:06 +0800314 }
Tiger Yang23fc2702008-11-14 11:17:18 +0800315 if (acl) {
Jiaju Zhang845b6cf2010-07-28 13:21:06 +0800316 ret = posix_acl_permission(inode, acl, mask);
Tiger Yang23fc2702008-11-14 11:17:18 +0800317 posix_acl_release(acl);
318 return ret;
319 }
320
321 return -EAGAIN;
322}
323
Tiger Yang060bc662008-11-14 11:17:29 +0800324int ocfs2_acl_chmod(struct inode *inode)
325{
326 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
327 struct posix_acl *acl, *clone;
328 int ret;
329
330 if (S_ISLNK(inode->i_mode))
331 return -EOPNOTSUPP;
332
333 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
334 return 0;
335
336 acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
337 if (IS_ERR(acl) || !acl)
338 return PTR_ERR(acl);
339 clone = posix_acl_clone(acl, GFP_KERNEL);
340 posix_acl_release(acl);
341 if (!clone)
342 return -ENOMEM;
343 ret = posix_acl_chmod_masq(clone, inode->i_mode);
344 if (!ret)
345 ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
346 clone, NULL, NULL);
347 posix_acl_release(clone);
348 return ret;
349}
350
Tiger Yang89c38bd2008-11-14 11:17:41 +0800351/*
352 * Initialize the ACLs of a new inode. If parent directory has default ACL,
353 * then clone to new inode. Called from ocfs2_mknod.
354 */
355int ocfs2_init_acl(handle_t *handle,
356 struct inode *inode,
357 struct inode *dir,
358 struct buffer_head *di_bh,
359 struct buffer_head *dir_bh,
360 struct ocfs2_alloc_context *meta_ac,
361 struct ocfs2_alloc_context *data_ac)
362{
363 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
364 struct posix_acl *acl = NULL;
Tiger Yang6eda3dd2010-07-16 11:21:23 +0800365 int ret = 0, ret2;
Mark Fashehfcefd252010-03-15 15:39:00 -0700366 mode_t mode;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800367
368 if (!S_ISLNK(inode->i_mode)) {
369 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
370 acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
371 dir_bh);
372 if (IS_ERR(acl))
373 return PTR_ERR(acl);
374 }
Mark Fashehfcefd252010-03-15 15:39:00 -0700375 if (!acl) {
376 mode = inode->i_mode & ~current_umask();
377 ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
378 if (ret) {
379 mlog_errno(ret);
380 goto cleanup;
381 }
382 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800383 }
384 if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
385 struct posix_acl *clone;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800386
387 if (S_ISDIR(inode->i_mode)) {
388 ret = ocfs2_set_acl(handle, inode, di_bh,
389 ACL_TYPE_DEFAULT, acl,
390 meta_ac, data_ac);
391 if (ret)
392 goto cleanup;
393 }
394 clone = posix_acl_clone(acl, GFP_NOFS);
395 ret = -ENOMEM;
396 if (!clone)
397 goto cleanup;
398
399 mode = inode->i_mode;
400 ret = posix_acl_create_masq(clone, &mode);
401 if (ret >= 0) {
Tiger Yang6eda3dd2010-07-16 11:21:23 +0800402 ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
403 if (ret2) {
404 mlog_errno(ret2);
405 ret = ret2;
406 goto cleanup;
407 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800408 if (ret > 0) {
409 ret = ocfs2_set_acl(handle, inode,
410 di_bh, ACL_TYPE_ACCESS,
411 clone, meta_ac, data_ac);
412 }
413 }
414 posix_acl_release(clone);
415 }
416cleanup:
417 posix_acl_release(acl);
418 return ret;
419}
420
Christoph Hellwig431547b2009-11-13 09:52:56 +0000421static size_t ocfs2_xattr_list_acl_access(struct dentry *dentry,
Tiger Yang929fb012008-11-14 11:17:04 +0800422 char *list,
423 size_t list_len,
424 const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000425 size_t name_len,
426 int type)
Tiger Yang929fb012008-11-14 11:17:04 +0800427{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000428 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Tiger Yang929fb012008-11-14 11:17:04 +0800429 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
430
431 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
432 return 0;
433
434 if (list && size <= list_len)
435 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
436 return size;
437}
438
Christoph Hellwig431547b2009-11-13 09:52:56 +0000439static size_t ocfs2_xattr_list_acl_default(struct dentry *dentry,
Tiger Yang929fb012008-11-14 11:17:04 +0800440 char *list,
441 size_t list_len,
442 const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000443 size_t name_len,
444 int type)
Tiger Yang929fb012008-11-14 11:17:04 +0800445{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000446 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Tiger Yang929fb012008-11-14 11:17:04 +0800447 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
448
449 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
450 return 0;
451
452 if (list && size <= list_len)
453 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
454 return size;
455}
456
Christoph Hellwig431547b2009-11-13 09:52:56 +0000457static int ocfs2_xattr_get_acl(struct dentry *dentry, const char *name,
458 void *buffer, size_t size, int type)
Tiger Yang929fb012008-11-14 11:17:04 +0800459{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000460 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Tiger Yang929fb012008-11-14 11:17:04 +0800461 struct posix_acl *acl;
462 int ret;
463
Christoph Hellwig431547b2009-11-13 09:52:56 +0000464 if (strcmp(name, "") != 0)
465 return -EINVAL;
Tiger Yang929fb012008-11-14 11:17:04 +0800466 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
467 return -EOPNOTSUPP;
468
Christoph Hellwig431547b2009-11-13 09:52:56 +0000469 acl = ocfs2_get_acl(dentry->d_inode, type);
Tiger Yang929fb012008-11-14 11:17:04 +0800470 if (IS_ERR(acl))
471 return PTR_ERR(acl);
472 if (acl == NULL)
473 return -ENODATA;
474 ret = posix_acl_to_xattr(acl, buffer, size);
475 posix_acl_release(acl);
476
477 return ret;
478}
479
Christoph Hellwig431547b2009-11-13 09:52:56 +0000480static int ocfs2_xattr_set_acl(struct dentry *dentry, const char *name,
481 const void *value, size_t size, int flags, int type)
Tiger Yang929fb012008-11-14 11:17:04 +0800482{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000483 struct inode *inode = dentry->d_inode;
Tiger Yang929fb012008-11-14 11:17:04 +0800484 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
485 struct posix_acl *acl;
486 int ret = 0;
487
Christoph Hellwig431547b2009-11-13 09:52:56 +0000488 if (strcmp(name, "") != 0)
489 return -EINVAL;
Tiger Yang929fb012008-11-14 11:17:04 +0800490 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
491 return -EOPNOTSUPP;
492
493 if (!is_owner_or_cap(inode))
494 return -EPERM;
495
496 if (value) {
497 acl = posix_acl_from_xattr(value, size);
498 if (IS_ERR(acl))
499 return PTR_ERR(acl);
500 else if (acl) {
501 ret = posix_acl_valid(acl);
502 if (ret)
503 goto cleanup;
504 }
505 } else
506 acl = NULL;
507
508 ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
509
510cleanup:
511 posix_acl_release(acl);
512 return ret;
513}
514
Stephen Hemminger537d81c2010-05-13 17:53:22 -0700515const struct xattr_handler ocfs2_xattr_acl_access_handler = {
Tiger Yang929fb012008-11-14 11:17:04 +0800516 .prefix = POSIX_ACL_XATTR_ACCESS,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000517 .flags = ACL_TYPE_ACCESS,
Tiger Yang929fb012008-11-14 11:17:04 +0800518 .list = ocfs2_xattr_list_acl_access,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000519 .get = ocfs2_xattr_get_acl,
520 .set = ocfs2_xattr_set_acl,
Tiger Yang929fb012008-11-14 11:17:04 +0800521};
522
Stephen Hemminger537d81c2010-05-13 17:53:22 -0700523const struct xattr_handler ocfs2_xattr_acl_default_handler = {
Tiger Yang929fb012008-11-14 11:17:04 +0800524 .prefix = POSIX_ACL_XATTR_DEFAULT,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000525 .flags = ACL_TYPE_DEFAULT,
Tiger Yang929fb012008-11-14 11:17:04 +0800526 .list = ocfs2_xattr_list_acl_default,
Christoph Hellwig431547b2009-11-13 09:52:56 +0000527 .get = ocfs2_xattr_get_acl,
528 .set = ocfs2_xattr_set_acl,
Tiger Yang929fb012008-11-14 11:17:04 +0800529};