Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / fs / ext4 / richacl.c
1 /*
2  * Copyright IBM Corporation, 2010
3  * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2.1 of the GNU Lesser General Public License
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/fs.h>
17 #include <linux/richacl_xattr.h>
18
19 #include "ext4.h"
20 #include "ext4_jbd2.h"
21 #include "xattr.h"
22 #include "acl.h"
23 #include "richacl.h"
24
25 static inline struct richacl *
26 ext4_iget_richacl(struct inode *inode)
27 {
28         struct richacl *acl = EXT4_RICHACL_NOT_CACHED;
29         struct ext4_inode_info *ei = EXT4_I(inode);
30
31         spin_lock(&inode->i_lock);
32         if (ei->i_richacl != EXT4_RICHACL_NOT_CACHED)
33                 acl = richacl_get(ei->i_richacl);
34         spin_unlock(&inode->i_lock);
35
36         return acl;
37 }
38
39 static inline void
40 ext4_iset_richacl(struct inode *inode, struct richacl *acl)
41 {
42         struct ext4_inode_info *ei = EXT4_I(inode);
43
44         spin_lock(&inode->i_lock);
45         if (ei->i_richacl != EXT4_RICHACL_NOT_CACHED)
46                 richacl_put(ei->i_richacl);
47         ei->i_richacl = richacl_get(acl);
48         spin_unlock(&inode->i_lock);
49 }
50
51 static struct richacl *
52 ext4_get_richacl(struct inode *inode)
53 {
54         const int name_index = EXT4_XATTR_INDEX_RICHACL;
55         void *value = NULL;
56         struct richacl *acl;
57         int retval;
58
59         if (!IS_RICHACL(inode))
60                 return ERR_PTR(-EOPNOTSUPP);
61         acl = ext4_iget_richacl(inode);
62         if (acl != EXT4_RICHACL_NOT_CACHED)
63                 return acl;
64         retval = ext4_xattr_get(inode, name_index, "", NULL, 0);
65         if (retval > 0) {
66                 value = kmalloc(retval, GFP_KERNEL);
67                 if (!value)
68                         return ERR_PTR(-ENOMEM);
69                 retval = ext4_xattr_get(inode, name_index, "", value, retval);
70         }
71         if (retval > 0) {
72                 acl = richacl_from_xattr(value, retval);
73                 if (acl == ERR_PTR(-EINVAL))
74                         acl = ERR_PTR(-EIO);
75         } else if (retval == -ENODATA || retval == -ENOSYS)
76                 acl = NULL;
77         else
78                 acl = ERR_PTR(retval);
79         kfree(value);
80
81         if (!IS_ERR_OR_NULL(acl))
82                 ext4_iset_richacl(inode, acl);
83
84         return acl;
85 }
86
87 static int
88 ext4_set_richacl(handle_t *handle, struct inode *inode, struct richacl *acl)
89 {
90         const int name_index = EXT4_XATTR_INDEX_RICHACL;
91         size_t size = 0;
92         void *value = NULL;
93         int retval;
94
95         if (acl) {
96                 mode_t mode = inode->i_mode;
97                 if (richacl_equiv_mode(acl, &mode) == 0) {
98                         inode->i_mode = mode;
99                         ext4_mark_inode_dirty(handle, inode);
100                         acl = NULL;
101                 }
102         }
103         if (acl) {
104                 size = richacl_xattr_size(acl);
105                 value = kmalloc(size, GFP_KERNEL);
106                 if (!value)
107                         return -ENOMEM;
108                 richacl_to_xattr(acl, value);
109         }
110         if (handle)
111                 retval = ext4_xattr_set_handle(handle, inode, name_index, "",
112                                                value, size, 0);
113         else
114                 retval = ext4_xattr_set(inode, name_index, "", value, size, 0);
115         kfree(value);
116         if (!retval)
117                 ext4_iset_richacl(inode, acl);
118
119         return retval;
120 }
121
122 int
123 ext4_richacl_permission(struct inode *inode, unsigned int mask)
124 {
125         struct richacl *acl;
126         int retval;
127
128         if (!IS_RICHACL(inode))
129                 BUG();
130
131         acl = ext4_get_richacl(inode);
132         if (acl && IS_ERR(acl))
133                 retval = PTR_ERR(acl);
134         else {
135                 retval = richacl_inode_permission(inode, acl, mask);
136                 richacl_put(acl);
137         }
138
139         return retval;
140 }
141
142 int ext4_permission(struct inode *inode, int mask)
143 {
144         if (IS_RICHACL(inode))
145                 return ext4_richacl_permission(inode,
146                                         richacl_want_to_mask(mask));
147         else
148                 return generic_permission(inode, mask);
149 }
150
151 int ext4_may_create(struct inode *dir, int isdir)
152 {
153         return richacl_may_create(dir, isdir, ext4_richacl_permission);
154 }
155
156 int ext4_may_delete(struct inode *dir, struct inode *inode, int replace)
157 {
158         return richacl_may_delete(dir, inode, replace, ext4_richacl_permission);
159 }
160
161 int
162 ext4_init_richacl(handle_t *handle, struct inode *inode, struct inode *dir)
163 {
164         struct richacl *dir_acl = NULL;
165
166         if (!S_ISLNK(inode->i_mode)) {
167                 dir_acl = ext4_get_richacl(dir);
168                 if (IS_ERR(dir_acl))
169                         return PTR_ERR(dir_acl);
170         }
171         if (dir_acl) {
172                 struct richacl *acl;
173                 int retval;
174
175                 acl = richacl_inherit(dir_acl, inode);
176                 richacl_put(dir_acl);
177
178                 retval = PTR_ERR(acl);
179                 if (acl && !IS_ERR(acl)) {
180                         retval = ext4_set_richacl(handle, inode, acl);
181                         richacl_put(acl);
182                 }
183                 return retval;
184         } else {
185                 inode->i_mode &= ~current_umask();
186                 return 0;
187         }
188 }
189
190 int
191 ext4_richacl_chmod(struct inode *inode)
192 {
193         struct richacl *acl;
194         int retval;
195
196         if (S_ISLNK(inode->i_mode))
197                 return -EOPNOTSUPP;
198         acl = ext4_get_richacl(inode);
199         if (IS_ERR_OR_NULL(acl))
200                 return PTR_ERR(acl);
201         acl = richacl_chmod(acl, inode->i_mode);
202         if (IS_ERR(acl))
203                 return PTR_ERR(acl);
204         retval = ext4_set_richacl(NULL, inode, acl);
205         richacl_put(acl);
206
207         return retval;
208 }
209
210 static size_t
211 ext4_xattr_list_richacl(struct dentry *dentry, char *list, size_t list_len,
212                         const char *name, size_t name_len, int type)
213 {
214         const size_t size = sizeof(RICHACL_XATTR);
215         if (!IS_RICHACL(dentry->d_inode))
216                 return 0;
217         if (list && size <= list_len)
218                 memcpy(list, RICHACL_XATTR, size);
219         return size;
220 }
221
222 static int
223 ext4_xattr_get_richacl(struct dentry *dentry, const char *name, void *buffer,
224                 size_t buffer_size, int type)
225 {
226         struct richacl *acl;
227         size_t size;
228
229         if (strcmp(name, "") != 0)
230                 return -EINVAL;
231         acl = ext4_get_richacl(dentry->d_inode);
232         if (IS_ERR(acl))
233                 return PTR_ERR(acl);
234         if (acl == NULL)
235                 return -ENODATA;
236         size = richacl_xattr_size(acl);
237         if (buffer) {
238                 if (size > buffer_size)
239                         return -ERANGE;
240                 richacl_to_xattr(acl, buffer);
241         }
242         richacl_put(acl);
243
244         return size;
245 }
246
247 static int
248 ext4_xattr_set_richacl(struct dentry *dentry, const char *name,
249                 const void *value, size_t size, int flags, int type)
250 {
251         handle_t *handle;
252         struct richacl *acl = NULL;
253         int retval, retries = 0;
254         struct inode *inode = dentry->d_inode;
255
256         if (!IS_RICHACL(dentry->d_inode))
257                 return -EOPNOTSUPP;
258         if (S_ISLNK(inode->i_mode))
259                 return -EOPNOTSUPP;
260         if (strcmp(name, "") != 0)
261                 return -EINVAL;
262         if (current_fsuid() != inode->i_uid &&
263             ext4_richacl_permission(inode, ACE4_WRITE_ACL) &&
264             !capable(CAP_FOWNER))
265                 return -EPERM;
266         if (value) {
267                 acl = richacl_from_xattr(value, size);
268                 if (IS_ERR(acl))
269                         return PTR_ERR(acl);
270
271                 inode->i_mode &= ~S_IRWXUGO;
272                 inode->i_mode |= richacl_masks_to_mode(acl);
273         }
274
275 retry:
276         handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
277         if (IS_ERR(handle))
278                 return PTR_ERR(handle);
279         ext4_mark_inode_dirty(handle, inode);
280         retval = ext4_set_richacl(handle, inode, acl);
281         ext4_journal_stop(handle);
282         if (retval == ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
283                 goto retry;
284         richacl_put(acl);
285         return retval;
286 }
287
288 const struct xattr_handler ext4_richacl_xattr_handler = {
289         .prefix = RICHACL_XATTR,
290         .list   = ext4_xattr_list_richacl,
291         .get    = ext4_xattr_get_richacl,
292         .set    = ext4_xattr_set_richacl,
293 };