i915: return -EFAULT if copy_to_user fails
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / acl.c
blobc8288df5712010c8485d352b8881de68813b0c4f
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * acl.c
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
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>
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.
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.
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/string.h>
26 #define MLOG_MASK_PREFIX ML_INODE
27 #include <cluster/masklog.h>
29 #include "ocfs2.h"
30 #include "alloc.h"
31 #include "dlmglue.h"
32 #include "file.h"
33 #include "inode.h"
34 #include "journal.h"
35 #include "ocfs2_fs.h"
37 #include "xattr.h"
38 #include "acl.h"
41 * Convert from xattr value to acl struct.
43 static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
45 int n, count;
46 struct posix_acl *acl;
48 if (!value)
49 return NULL;
50 if (size < sizeof(struct posix_acl_entry))
51 return ERR_PTR(-EINVAL);
53 count = size / sizeof(struct posix_acl_entry);
54 if (count < 0)
55 return ERR_PTR(-EINVAL);
56 if (count == 0)
57 return NULL;
59 acl = posix_acl_alloc(count, GFP_NOFS);
60 if (!acl)
61 return ERR_PTR(-ENOMEM);
62 for (n = 0; n < count; n++) {
63 struct ocfs2_acl_entry *entry =
64 (struct ocfs2_acl_entry *)value;
66 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
67 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
68 acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
69 value += sizeof(struct posix_acl_entry);
72 return acl;
76 * Convert acl struct to xattr value.
78 static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
80 struct ocfs2_acl_entry *entry = NULL;
81 char *ocfs2_acl;
82 size_t n;
84 *size = acl->a_count * sizeof(struct posix_acl_entry);
86 ocfs2_acl = kmalloc(*size, GFP_NOFS);
87 if (!ocfs2_acl)
88 return ERR_PTR(-ENOMEM);
90 entry = (struct ocfs2_acl_entry *)ocfs2_acl;
91 for (n = 0; n < acl->a_count; n++, entry++) {
92 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
93 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
94 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
96 return ocfs2_acl;
99 static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
100 int type,
101 struct buffer_head *di_bh)
103 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
104 int name_index;
105 char *value = NULL;
106 struct posix_acl *acl;
107 int retval;
109 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
110 return NULL;
112 switch (type) {
113 case ACL_TYPE_ACCESS:
114 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
115 break;
116 case ACL_TYPE_DEFAULT:
117 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
118 break;
119 default:
120 return ERR_PTR(-EINVAL);
123 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
124 if (retval > 0) {
125 value = kmalloc(retval, GFP_NOFS);
126 if (!value)
127 return ERR_PTR(-ENOMEM);
128 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
129 "", value, retval);
132 if (retval > 0)
133 acl = ocfs2_acl_from_xattr(value, retval);
134 else if (retval == -ENODATA || retval == 0)
135 acl = NULL;
136 else
137 acl = ERR_PTR(retval);
139 kfree(value);
141 return acl;
146 * Get posix acl.
148 static struct posix_acl *ocfs2_get_acl(struct inode *inode, int type)
150 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
151 struct buffer_head *di_bh = NULL;
152 struct posix_acl *acl;
153 int ret;
155 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
156 return NULL;
158 ret = ocfs2_inode_lock(inode, &di_bh, 0);
159 if (ret < 0) {
160 mlog_errno(ret);
161 acl = ERR_PTR(ret);
162 return acl;
165 acl = ocfs2_get_acl_nolock(inode, type, di_bh);
167 ocfs2_inode_unlock(inode, 0);
169 brelse(di_bh);
171 return acl;
175 * Helper function to set i_mode in memory and disk. Some call paths
176 * will not have di_bh or a journal handle to pass, in which case it
177 * will create it's own.
179 static int ocfs2_acl_set_mode(struct inode *inode, struct buffer_head *di_bh,
180 handle_t *handle, umode_t new_mode)
182 int ret, commit_handle = 0;
183 struct ocfs2_dinode *di;
185 if (di_bh == NULL) {
186 ret = ocfs2_read_inode_block(inode, &di_bh);
187 if (ret) {
188 mlog_errno(ret);
189 goto out;
191 } else
192 get_bh(di_bh);
194 if (handle == NULL) {
195 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
196 OCFS2_INODE_UPDATE_CREDITS);
197 if (IS_ERR(handle)) {
198 ret = PTR_ERR(handle);
199 mlog_errno(ret);
200 goto out_brelse;
203 commit_handle = 1;
206 di = (struct ocfs2_dinode *)di_bh->b_data;
207 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
208 OCFS2_JOURNAL_ACCESS_WRITE);
209 if (ret) {
210 mlog_errno(ret);
211 goto out_commit;
214 inode->i_mode = new_mode;
215 di->i_mode = cpu_to_le16(inode->i_mode);
217 ocfs2_journal_dirty(handle, di_bh);
219 out_commit:
220 if (commit_handle)
221 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
222 out_brelse:
223 brelse(di_bh);
224 out:
225 return ret;
229 * Set the access or default ACL of an inode.
231 static int ocfs2_set_acl(handle_t *handle,
232 struct inode *inode,
233 struct buffer_head *di_bh,
234 int type,
235 struct posix_acl *acl,
236 struct ocfs2_alloc_context *meta_ac,
237 struct ocfs2_alloc_context *data_ac)
239 int name_index;
240 void *value = NULL;
241 size_t size = 0;
242 int ret;
244 if (S_ISLNK(inode->i_mode))
245 return -EOPNOTSUPP;
247 switch (type) {
248 case ACL_TYPE_ACCESS:
249 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
250 if (acl) {
251 mode_t mode = inode->i_mode;
252 ret = posix_acl_equiv_mode(acl, &mode);
253 if (ret < 0)
254 return ret;
255 else {
256 if (ret == 0)
257 acl = NULL;
259 ret = ocfs2_acl_set_mode(inode, di_bh,
260 handle, mode);
261 if (ret)
262 return ret;
266 break;
267 case ACL_TYPE_DEFAULT:
268 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
269 if (!S_ISDIR(inode->i_mode))
270 return acl ? -EACCES : 0;
271 break;
272 default:
273 return -EINVAL;
276 if (acl) {
277 value = ocfs2_acl_to_xattr(acl, &size);
278 if (IS_ERR(value))
279 return (int)PTR_ERR(value);
282 if (handle)
283 ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
284 "", value, size, 0,
285 meta_ac, data_ac);
286 else
287 ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
289 kfree(value);
291 return ret;
294 int ocfs2_check_acl(struct inode *inode, int mask)
296 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
297 struct buffer_head *di_bh = NULL;
298 struct posix_acl *acl;
299 int ret = -EAGAIN;
301 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
302 return ret;
304 ret = ocfs2_read_inode_block(inode, &di_bh);
305 if (ret < 0) {
306 mlog_errno(ret);
307 return ret;
310 acl = ocfs2_get_acl_nolock(inode, ACL_TYPE_ACCESS, di_bh);
312 brelse(di_bh);
314 if (IS_ERR(acl)) {
315 mlog_errno(PTR_ERR(acl));
316 return PTR_ERR(acl);
318 if (acl) {
319 ret = posix_acl_permission(inode, acl, mask);
320 posix_acl_release(acl);
321 return ret;
324 return -EAGAIN;
327 int ocfs2_acl_chmod(struct inode *inode)
329 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
330 struct posix_acl *acl, *clone;
331 int ret;
333 if (S_ISLNK(inode->i_mode))
334 return -EOPNOTSUPP;
336 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
337 return 0;
339 acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
340 if (IS_ERR(acl) || !acl)
341 return PTR_ERR(acl);
342 clone = posix_acl_clone(acl, GFP_KERNEL);
343 posix_acl_release(acl);
344 if (!clone)
345 return -ENOMEM;
346 ret = posix_acl_chmod_masq(clone, inode->i_mode);
347 if (!ret)
348 ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
349 clone, NULL, NULL);
350 posix_acl_release(clone);
351 return ret;
355 * Initialize the ACLs of a new inode. If parent directory has default ACL,
356 * then clone to new inode. Called from ocfs2_mknod.
358 int ocfs2_init_acl(handle_t *handle,
359 struct inode *inode,
360 struct inode *dir,
361 struct buffer_head *di_bh,
362 struct buffer_head *dir_bh,
363 struct ocfs2_alloc_context *meta_ac,
364 struct ocfs2_alloc_context *data_ac)
366 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
367 struct posix_acl *acl = NULL;
368 int ret = 0, ret2;
369 mode_t mode;
371 if (!S_ISLNK(inode->i_mode)) {
372 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
373 acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
374 dir_bh);
375 if (IS_ERR(acl))
376 return PTR_ERR(acl);
378 if (!acl) {
379 mode = inode->i_mode & ~current_umask();
380 ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
381 if (ret) {
382 mlog_errno(ret);
383 goto cleanup;
387 if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
388 struct posix_acl *clone;
390 if (S_ISDIR(inode->i_mode)) {
391 ret = ocfs2_set_acl(handle, inode, di_bh,
392 ACL_TYPE_DEFAULT, acl,
393 meta_ac, data_ac);
394 if (ret)
395 goto cleanup;
397 clone = posix_acl_clone(acl, GFP_NOFS);
398 ret = -ENOMEM;
399 if (!clone)
400 goto cleanup;
402 mode = inode->i_mode;
403 ret = posix_acl_create_masq(clone, &mode);
404 if (ret >= 0) {
405 ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
406 if (ret2) {
407 mlog_errno(ret2);
408 ret = ret2;
409 goto cleanup;
411 if (ret > 0) {
412 ret = ocfs2_set_acl(handle, inode,
413 di_bh, ACL_TYPE_ACCESS,
414 clone, meta_ac, data_ac);
417 posix_acl_release(clone);
419 cleanup:
420 posix_acl_release(acl);
421 return ret;
424 static size_t ocfs2_xattr_list_acl_access(struct inode *inode,
425 char *list,
426 size_t list_len,
427 const char *name,
428 size_t name_len)
430 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
431 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
433 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
434 return 0;
436 if (list && size <= list_len)
437 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
438 return size;
441 static size_t ocfs2_xattr_list_acl_default(struct inode *inode,
442 char *list,
443 size_t list_len,
444 const char *name,
445 size_t name_len)
447 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
448 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
450 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
451 return 0;
453 if (list && size <= list_len)
454 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
455 return size;
458 static int ocfs2_xattr_get_acl(struct inode *inode,
459 int type,
460 void *buffer,
461 size_t size)
463 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
464 struct posix_acl *acl;
465 int ret;
467 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
468 return -EOPNOTSUPP;
470 acl = ocfs2_get_acl(inode, type);
471 if (IS_ERR(acl))
472 return PTR_ERR(acl);
473 if (acl == NULL)
474 return -ENODATA;
475 ret = posix_acl_to_xattr(acl, buffer, size);
476 posix_acl_release(acl);
478 return ret;
481 static int ocfs2_xattr_get_acl_access(struct inode *inode,
482 const char *name,
483 void *buffer,
484 size_t size)
486 if (strcmp(name, "") != 0)
487 return -EINVAL;
488 return ocfs2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
491 static int ocfs2_xattr_get_acl_default(struct inode *inode,
492 const char *name,
493 void *buffer,
494 size_t size)
496 if (strcmp(name, "") != 0)
497 return -EINVAL;
498 return ocfs2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
501 static int ocfs2_xattr_set_acl(struct inode *inode,
502 int type,
503 const void *value,
504 size_t size)
506 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
507 struct posix_acl *acl;
508 int ret = 0;
510 if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
511 return -EOPNOTSUPP;
513 if (!is_owner_or_cap(inode))
514 return -EPERM;
516 if (value) {
517 acl = posix_acl_from_xattr(value, size);
518 if (IS_ERR(acl))
519 return PTR_ERR(acl);
520 else if (acl) {
521 ret = posix_acl_valid(acl);
522 if (ret)
523 goto cleanup;
525 } else
526 acl = NULL;
528 ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
530 cleanup:
531 posix_acl_release(acl);
532 return ret;
535 static int ocfs2_xattr_set_acl_access(struct inode *inode,
536 const char *name,
537 const void *value,
538 size_t size,
539 int flags)
541 if (strcmp(name, "") != 0)
542 return -EINVAL;
543 return ocfs2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
546 static int ocfs2_xattr_set_acl_default(struct inode *inode,
547 const char *name,
548 const void *value,
549 size_t size,
550 int flags)
552 if (strcmp(name, "") != 0)
553 return -EINVAL;
554 return ocfs2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
557 struct xattr_handler ocfs2_xattr_acl_access_handler = {
558 .prefix = POSIX_ACL_XATTR_ACCESS,
559 .list = ocfs2_xattr_list_acl_access,
560 .get = ocfs2_xattr_get_acl_access,
561 .set = ocfs2_xattr_set_acl_access,
564 struct xattr_handler ocfs2_xattr_acl_default_handler = {
565 .prefix = POSIX_ACL_XATTR_DEFAULT,
566 .list = ocfs2_xattr_list_acl_default,
567 .get = ocfs2_xattr_get_acl_default,
568 .set = ocfs2_xattr_set_acl_default,