Linux-libre 3.16.85-gnu
[librecmc/linux-libre.git] / fs / reiserfs / xattr_acl.c
1 #include <linux/capability.h>
2 #include <linux/fs.h>
3 #include <linux/posix_acl.h>
4 #include "reiserfs.h"
5 #include <linux/errno.h>
6 #include <linux/pagemap.h>
7 #include <linux/xattr.h>
8 #include <linux/slab.h>
9 #include <linux/posix_acl_xattr.h>
10 #include "xattr.h"
11 #include "acl.h"
12 #include <asm/uaccess.h>
13
14 static int __reiserfs_set_acl(struct reiserfs_transaction_handle *th,
15                             struct inode *inode, int type,
16                             struct posix_acl *acl);
17
18
19 int
20 reiserfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
21 {
22         int error, error2;
23         struct reiserfs_transaction_handle th;
24         size_t jcreate_blocks;
25         int size = acl ? posix_acl_xattr_size(acl->a_count) : 0;
26         int update_mode = 0;
27         umode_t mode = inode->i_mode;
28
29         /*
30          * Pessimism: We can't assume that anything from the xattr root up
31          * has been created.
32          */
33
34         jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
35                          reiserfs_xattr_nblocks(inode, size) * 2;
36
37         reiserfs_write_lock(inode->i_sb);
38         error = journal_begin(&th, inode->i_sb, jcreate_blocks);
39         reiserfs_write_unlock(inode->i_sb);
40         if (error == 0) {
41                 if (type == ACL_TYPE_ACCESS && acl) {
42                         error = posix_acl_update_mode(inode, &mode, &acl);
43                         if (error)
44                                 goto unlock;
45                         update_mode = 1;
46                 }
47                 error = __reiserfs_set_acl(&th, inode, type, acl);
48                 if (!error && update_mode)
49                         inode->i_mode = mode;
50 unlock:
51                 reiserfs_write_lock(inode->i_sb);
52                 error2 = journal_end(&th);
53                 reiserfs_write_unlock(inode->i_sb);
54                 if (error2)
55                         error = error2;
56         }
57
58         return error;
59 }
60
61 /*
62  * Convert from filesystem to in-memory representation.
63  */
64 static struct posix_acl *reiserfs_posix_acl_from_disk(const void *value, size_t size)
65 {
66         const char *end = (char *)value + size;
67         int n, count;
68         struct posix_acl *acl;
69
70         if (!value)
71                 return NULL;
72         if (size < sizeof(reiserfs_acl_header))
73                 return ERR_PTR(-EINVAL);
74         if (((reiserfs_acl_header *) value)->a_version !=
75             cpu_to_le32(REISERFS_ACL_VERSION))
76                 return ERR_PTR(-EINVAL);
77         value = (char *)value + sizeof(reiserfs_acl_header);
78         count = reiserfs_acl_count(size);
79         if (count < 0)
80                 return ERR_PTR(-EINVAL);
81         if (count == 0)
82                 return NULL;
83         acl = posix_acl_alloc(count, GFP_NOFS);
84         if (!acl)
85                 return ERR_PTR(-ENOMEM);
86         for (n = 0; n < count; n++) {
87                 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
88                 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
89                         goto fail;
90                 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
91                 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
92                 switch (acl->a_entries[n].e_tag) {
93                 case ACL_USER_OBJ:
94                 case ACL_GROUP_OBJ:
95                 case ACL_MASK:
96                 case ACL_OTHER:
97                         value = (char *)value +
98                             sizeof(reiserfs_acl_entry_short);
99                         break;
100
101                 case ACL_USER:
102                         value = (char *)value + sizeof(reiserfs_acl_entry);
103                         if ((char *)value > end)
104                                 goto fail;
105                         acl->a_entries[n].e_uid = 
106                                 make_kuid(&init_user_ns,
107                                           le32_to_cpu(entry->e_id));
108                         break;
109                 case ACL_GROUP:
110                         value = (char *)value + sizeof(reiserfs_acl_entry);
111                         if ((char *)value > end)
112                                 goto fail;
113                         acl->a_entries[n].e_gid =
114                                 make_kgid(&init_user_ns,
115                                           le32_to_cpu(entry->e_id));
116                         break;
117
118                 default:
119                         goto fail;
120                 }
121         }
122         if (value != end)
123                 goto fail;
124         return acl;
125
126 fail:
127         posix_acl_release(acl);
128         return ERR_PTR(-EINVAL);
129 }
130
131 /*
132  * Convert from in-memory to filesystem representation.
133  */
134 static void *reiserfs_posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
135 {
136         reiserfs_acl_header *ext_acl;
137         char *e;
138         int n;
139
140         *size = reiserfs_acl_size(acl->a_count);
141         ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
142                                                   acl->a_count *
143                                                   sizeof(reiserfs_acl_entry),
144                                                   GFP_NOFS);
145         if (!ext_acl)
146                 return ERR_PTR(-ENOMEM);
147         ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
148         e = (char *)ext_acl + sizeof(reiserfs_acl_header);
149         for (n = 0; n < acl->a_count; n++) {
150                 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
151                 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
152                 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
153                 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
154                 switch (acl->a_entries[n].e_tag) {
155                 case ACL_USER:
156                         entry->e_id = cpu_to_le32(
157                                 from_kuid(&init_user_ns, acl_e->e_uid));
158                         e += sizeof(reiserfs_acl_entry);
159                         break;
160                 case ACL_GROUP:
161                         entry->e_id = cpu_to_le32(
162                                 from_kgid(&init_user_ns, acl_e->e_gid));
163                         e += sizeof(reiserfs_acl_entry);
164                         break;
165
166                 case ACL_USER_OBJ:
167                 case ACL_GROUP_OBJ:
168                 case ACL_MASK:
169                 case ACL_OTHER:
170                         e += sizeof(reiserfs_acl_entry_short);
171                         break;
172
173                 default:
174                         goto fail;
175                 }
176         }
177         return (char *)ext_acl;
178
179 fail:
180         kfree(ext_acl);
181         return ERR_PTR(-EINVAL);
182 }
183
184 /*
185  * Inode operation get_posix_acl().
186  *
187  * inode->i_mutex: down
188  * BKL held [before 2.5.x]
189  */
190 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
191 {
192         char *name, *value;
193         struct posix_acl *acl;
194         int size;
195         int retval;
196
197         switch (type) {
198         case ACL_TYPE_ACCESS:
199                 name = POSIX_ACL_XATTR_ACCESS;
200                 break;
201         case ACL_TYPE_DEFAULT:
202                 name = POSIX_ACL_XATTR_DEFAULT;
203                 break;
204         default:
205                 BUG();
206         }
207
208         size = reiserfs_xattr_get(inode, name, NULL, 0);
209         if (size < 0) {
210                 if (size == -ENODATA || size == -ENOSYS) {
211                         set_cached_acl(inode, type, NULL);
212                         return NULL;
213                 }
214                 return ERR_PTR(size);
215         }
216
217         value = kmalloc(size, GFP_NOFS);
218         if (!value)
219                 return ERR_PTR(-ENOMEM);
220
221         retval = reiserfs_xattr_get(inode, name, value, size);
222         if (retval == -ENODATA || retval == -ENOSYS) {
223                 /*
224                  * This shouldn't actually happen as it should have
225                  * been caught above.. but just in case
226                  */
227                 acl = NULL;
228         } else if (retval < 0) {
229                 acl = ERR_PTR(retval);
230         } else {
231                 acl = reiserfs_posix_acl_from_disk(value, retval);
232         }
233         if (!IS_ERR(acl))
234                 set_cached_acl(inode, type, acl);
235
236         kfree(value);
237         return acl;
238 }
239
240 /*
241  * Inode operation set_posix_acl().
242  *
243  * inode->i_mutex: down
244  * BKL held [before 2.5.x]
245  */
246 static int
247 __reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
248                  int type, struct posix_acl *acl)
249 {
250         char *name;
251         void *value = NULL;
252         size_t size = 0;
253         int error;
254
255         switch (type) {
256         case ACL_TYPE_ACCESS:
257                 name = POSIX_ACL_XATTR_ACCESS;
258                 break;
259         case ACL_TYPE_DEFAULT:
260                 name = POSIX_ACL_XATTR_DEFAULT;
261                 if (!S_ISDIR(inode->i_mode))
262                         return acl ? -EACCES : 0;
263                 break;
264         default:
265                 return -EINVAL;
266         }
267
268         if (acl) {
269                 value = reiserfs_posix_acl_to_disk(acl, &size);
270                 if (IS_ERR(value))
271                         return (int)PTR_ERR(value);
272         }
273
274         error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
275
276         /*
277          * Ensure that the inode gets dirtied if we're only using
278          * the mode bits and an old ACL didn't exist. We don't need
279          * to check if the inode is hashed here since we won't get
280          * called by reiserfs_inherit_default_acl().
281          */
282         if (error == -ENODATA) {
283                 error = 0;
284                 if (type == ACL_TYPE_ACCESS) {
285                         inode->i_ctime = CURRENT_TIME_SEC;
286                         mark_inode_dirty(inode);
287                 }
288         }
289
290         kfree(value);
291
292         if (!error)
293                 set_cached_acl(inode, type, acl);
294
295         return error;
296 }
297
298 /*
299  * dir->i_mutex: locked,
300  * inode is new and not released into the wild yet
301  */
302 int
303 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
304                              struct inode *dir, struct dentry *dentry,
305                              struct inode *inode)
306 {
307         struct posix_acl *default_acl, *acl;
308         int err = 0;
309
310         /* ACLs only get applied to files and directories */
311         if (S_ISLNK(inode->i_mode))
312                 return 0;
313
314         /*
315          * ACLs can only be used on "new" objects, so if it's an old object
316          * there is nothing to inherit from
317          */
318         if (get_inode_sd_version(dir) == STAT_DATA_V1)
319                 goto apply_umask;
320
321         /*
322          * Don't apply ACLs to objects in the .reiserfs_priv tree.. This
323          * would be useless since permissions are ignored, and a pain because
324          * it introduces locking cycles
325          */
326         if (IS_PRIVATE(dir)) {
327                 inode->i_flags |= S_PRIVATE;
328                 goto apply_umask;
329         }
330
331         err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
332         if (err)
333                 return err;
334
335         if (default_acl) {
336                 err = __reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
337                                          default_acl);
338                 posix_acl_release(default_acl);
339         }
340         if (acl) {
341                 if (!err)
342                         err = __reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS,
343                                                  acl);
344                 posix_acl_release(acl);
345         }
346
347         return err;
348
349 apply_umask:
350         /* no ACL, apply umask */
351         inode->i_mode &= ~current_umask();
352         return err;
353 }
354
355 /* This is used to cache the default acl before a new object is created.
356  * The biggest reason for this is to get an idea of how many blocks will
357  * actually be required for the create operation if we must inherit an ACL.
358  * An ACL write can add up to 3 object creations and an additional file write
359  * so we'd prefer not to reserve that many blocks in the journal if we can.
360  * It also has the advantage of not loading the ACL with a transaction open,
361  * this may seem silly, but if the owner of the directory is doing the
362  * creation, the ACL may not be loaded since the permissions wouldn't require
363  * it.
364  * We return the number of blocks required for the transaction.
365  */
366 int reiserfs_cache_default_acl(struct inode *inode)
367 {
368         struct posix_acl *acl;
369         int nblocks = 0;
370
371         if (IS_PRIVATE(inode))
372                 return 0;
373
374         acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
375
376         if (acl && !IS_ERR(acl)) {
377                 int size = reiserfs_acl_size(acl->a_count);
378
379                 /* Other xattrs can be created during inode creation. We don't
380                  * want to claim too many blocks, so we check to see if we
381                  * we need to create the tree to the xattrs, and then we
382                  * just want two files. */
383                 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
384                 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
385
386                 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
387
388                 /* We need to account for writes + bitmaps for two files */
389                 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
390                 posix_acl_release(acl);
391         }
392
393         return nblocks;
394 }
395
396 /*
397  * Called under i_mutex
398  */
399 int reiserfs_acl_chmod(struct inode *inode)
400 {
401         if (IS_PRIVATE(inode))
402                 return 0;
403         if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
404             !reiserfs_posixacl(inode->i_sb))
405                 return 0;
406
407         return posix_acl_chmod(inode, inode->i_mode);
408 }