HID: convert to dev_* prints
[linux-2.6/next.git] / fs / generic_acl.c
blob995d63b2e747556c879c52be194375196995b58a
1 /*
2 * fs/generic_acl.c
4 * (C) 2005 Andreas Gruenbacher <agruen@suse.de>
6 * This file is released under the GPL.
7 */
9 #include <linux/sched.h>
10 #include <linux/fs.h>
11 #include <linux/generic_acl.h>
13 /**
14 * generic_acl_list - Generic xattr_handler->list() operation
15 * @ops: Filesystem specific getacl and setacl callbacks
17 size_t
18 generic_acl_list(struct inode *inode, struct generic_acl_operations *ops,
19 int type, char *list, size_t list_size)
21 struct posix_acl *acl;
22 const char *name;
23 size_t size;
25 acl = ops->getacl(inode, type);
26 if (!acl)
27 return 0;
28 posix_acl_release(acl);
30 switch(type) {
31 case ACL_TYPE_ACCESS:
32 name = POSIX_ACL_XATTR_ACCESS;
33 break;
35 case ACL_TYPE_DEFAULT:
36 name = POSIX_ACL_XATTR_DEFAULT;
37 break;
39 default:
40 return 0;
42 size = strlen(name) + 1;
43 if (list && size <= list_size)
44 memcpy(list, name, size);
45 return size;
48 /**
49 * generic_acl_get - Generic xattr_handler->get() operation
50 * @ops: Filesystem specific getacl and setacl callbacks
52 int
53 generic_acl_get(struct inode *inode, struct generic_acl_operations *ops,
54 int type, void *buffer, size_t size)
56 struct posix_acl *acl;
57 int error;
59 acl = ops->getacl(inode, type);
60 if (!acl)
61 return -ENODATA;
62 error = posix_acl_to_xattr(acl, buffer, size);
63 posix_acl_release(acl);
65 return error;
68 /**
69 * generic_acl_set - Generic xattr_handler->set() operation
70 * @ops: Filesystem specific getacl and setacl callbacks
72 int
73 generic_acl_set(struct inode *inode, struct generic_acl_operations *ops,
74 int type, const void *value, size_t size)
76 struct posix_acl *acl = NULL;
77 int error;
79 if (S_ISLNK(inode->i_mode))
80 return -EOPNOTSUPP;
81 if (!is_owner_or_cap(inode))
82 return -EPERM;
83 if (value) {
84 acl = posix_acl_from_xattr(value, size);
85 if (IS_ERR(acl))
86 return PTR_ERR(acl);
88 if (acl) {
89 mode_t mode;
91 error = posix_acl_valid(acl);
92 if (error)
93 goto failed;
94 switch(type) {
95 case ACL_TYPE_ACCESS:
96 mode = inode->i_mode;
97 error = posix_acl_equiv_mode(acl, &mode);
98 if (error < 0)
99 goto failed;
100 inode->i_mode = mode;
101 if (error == 0) {
102 posix_acl_release(acl);
103 acl = NULL;
105 break;
107 case ACL_TYPE_DEFAULT:
108 if (!S_ISDIR(inode->i_mode)) {
109 error = -EINVAL;
110 goto failed;
112 break;
115 ops->setacl(inode, type, acl);
116 error = 0;
117 failed:
118 posix_acl_release(acl);
119 return error;
123 * generic_acl_init - Take care of acl inheritance at @inode create time
124 * @ops: Filesystem specific getacl and setacl callbacks
126 * Files created inside a directory with a default ACL inherit the
127 * directory's default ACL.
130 generic_acl_init(struct inode *inode, struct inode *dir,
131 struct generic_acl_operations *ops)
133 struct posix_acl *acl = NULL;
134 mode_t mode = inode->i_mode;
135 int error;
137 inode->i_mode = mode & ~current->fs->umask;
138 if (!S_ISLNK(inode->i_mode))
139 acl = ops->getacl(dir, ACL_TYPE_DEFAULT);
140 if (acl) {
141 struct posix_acl *clone;
143 if (S_ISDIR(inode->i_mode)) {
144 clone = posix_acl_clone(acl, GFP_KERNEL);
145 error = -ENOMEM;
146 if (!clone)
147 goto cleanup;
148 ops->setacl(inode, ACL_TYPE_DEFAULT, clone);
149 posix_acl_release(clone);
151 clone = posix_acl_clone(acl, GFP_KERNEL);
152 error = -ENOMEM;
153 if (!clone)
154 goto cleanup;
155 error = posix_acl_create_masq(clone, &mode);
156 if (error >= 0) {
157 inode->i_mode = mode;
158 if (error > 0)
159 ops->setacl(inode, ACL_TYPE_ACCESS, clone);
161 posix_acl_release(clone);
163 error = 0;
165 cleanup:
166 posix_acl_release(acl);
167 return error;
171 * generic_acl_chmod - change the access acl of @inode upon chmod()
172 * @ops: FIlesystem specific getacl and setacl callbacks
174 * A chmod also changes the permissions of the owner, group/mask, and
175 * other ACL entries.
178 generic_acl_chmod(struct inode *inode, struct generic_acl_operations *ops)
180 struct posix_acl *acl, *clone;
181 int error = 0;
183 if (S_ISLNK(inode->i_mode))
184 return -EOPNOTSUPP;
185 acl = ops->getacl(inode, ACL_TYPE_ACCESS);
186 if (acl) {
187 clone = posix_acl_clone(acl, GFP_KERNEL);
188 posix_acl_release(acl);
189 if (!clone)
190 return -ENOMEM;
191 error = posix_acl_chmod_masq(clone, inode->i_mode);
192 if (!error)
193 ops->setacl(inode, ACL_TYPE_ACCESS, clone);
194 posix_acl_release(clone);
196 return error;