Merge branch 'sh-latest' of git://git.kernel.org/pub/scm/linux/kernel/git/lethal...
[firefly-linux-kernel-4.4.55.git] / fs / reiserfs / xattr_acl.c
1 #include <linux/capability.h>
2 #include <linux/fs.h>
3 #include <linux/posix_acl.h>
4 #include <linux/reiserfs_fs.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 <linux/reiserfs_xattr.h>
11 #include <linux/reiserfs_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 static int
19 posix_acl_set(struct dentry *dentry, const char *name, const void *value,
20                 size_t size, int flags, int type)
21 {
22         struct inode *inode = dentry->d_inode;
23         struct posix_acl *acl;
24         int error, error2;
25         struct reiserfs_transaction_handle th;
26         size_t jcreate_blocks;
27         if (!reiserfs_posixacl(inode->i_sb))
28                 return -EOPNOTSUPP;
29         if (!inode_owner_or_capable(inode))
30                 return -EPERM;
31
32         if (value) {
33                 acl = posix_acl_from_xattr(value, size);
34                 if (IS_ERR(acl)) {
35                         return PTR_ERR(acl);
36                 } else if (acl) {
37                         error = posix_acl_valid(acl);
38                         if (error)
39                                 goto release_and_out;
40                 }
41         } else
42                 acl = NULL;
43
44         /* Pessimism: We can't assume that anything from the xattr root up
45          * has been created. */
46
47         jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
48                          reiserfs_xattr_nblocks(inode, size) * 2;
49
50         reiserfs_write_lock(inode->i_sb);
51         error = journal_begin(&th, inode->i_sb, jcreate_blocks);
52         if (error == 0) {
53                 error = reiserfs_set_acl(&th, inode, type, acl);
54                 error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
55                 if (error2)
56                         error = error2;
57         }
58         reiserfs_write_unlock(inode->i_sb);
59
60       release_and_out:
61         posix_acl_release(acl);
62         return error;
63 }
64
65 static int
66 posix_acl_get(struct dentry *dentry, const char *name, void *buffer,
67                 size_t size, int type)
68 {
69         struct posix_acl *acl;
70         int error;
71
72         if (!reiserfs_posixacl(dentry->d_sb))
73                 return -EOPNOTSUPP;
74
75         acl = reiserfs_get_acl(dentry->d_inode, type);
76         if (IS_ERR(acl))
77                 return PTR_ERR(acl);
78         if (acl == NULL)
79                 return -ENODATA;
80         error = posix_acl_to_xattr(acl, buffer, size);
81         posix_acl_release(acl);
82
83         return error;
84 }
85
86 /*
87  * Convert from filesystem to in-memory representation.
88  */
89 static struct posix_acl *posix_acl_from_disk(const void *value, size_t size)
90 {
91         const char *end = (char *)value + size;
92         int n, count;
93         struct posix_acl *acl;
94
95         if (!value)
96                 return NULL;
97         if (size < sizeof(reiserfs_acl_header))
98                 return ERR_PTR(-EINVAL);
99         if (((reiserfs_acl_header *) value)->a_version !=
100             cpu_to_le32(REISERFS_ACL_VERSION))
101                 return ERR_PTR(-EINVAL);
102         value = (char *)value + sizeof(reiserfs_acl_header);
103         count = reiserfs_acl_count(size);
104         if (count < 0)
105                 return ERR_PTR(-EINVAL);
106         if (count == 0)
107                 return NULL;
108         acl = posix_acl_alloc(count, GFP_NOFS);
109         if (!acl)
110                 return ERR_PTR(-ENOMEM);
111         for (n = 0; n < count; n++) {
112                 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) value;
113                 if ((char *)value + sizeof(reiserfs_acl_entry_short) > end)
114                         goto fail;
115                 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
116                 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
117                 switch (acl->a_entries[n].e_tag) {
118                 case ACL_USER_OBJ:
119                 case ACL_GROUP_OBJ:
120                 case ACL_MASK:
121                 case ACL_OTHER:
122                         value = (char *)value +
123                             sizeof(reiserfs_acl_entry_short);
124                         acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
125                         break;
126
127                 case ACL_USER:
128                 case ACL_GROUP:
129                         value = (char *)value + sizeof(reiserfs_acl_entry);
130                         if ((char *)value > end)
131                                 goto fail;
132                         acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
133                         break;
134
135                 default:
136                         goto fail;
137                 }
138         }
139         if (value != end)
140                 goto fail;
141         return acl;
142
143       fail:
144         posix_acl_release(acl);
145         return ERR_PTR(-EINVAL);
146 }
147
148 /*
149  * Convert from in-memory to filesystem representation.
150  */
151 static void *posix_acl_to_disk(const struct posix_acl *acl, size_t * size)
152 {
153         reiserfs_acl_header *ext_acl;
154         char *e;
155         int n;
156
157         *size = reiserfs_acl_size(acl->a_count);
158         ext_acl = kmalloc(sizeof(reiserfs_acl_header) +
159                                                   acl->a_count *
160                                                   sizeof(reiserfs_acl_entry),
161                                                   GFP_NOFS);
162         if (!ext_acl)
163                 return ERR_PTR(-ENOMEM);
164         ext_acl->a_version = cpu_to_le32(REISERFS_ACL_VERSION);
165         e = (char *)ext_acl + sizeof(reiserfs_acl_header);
166         for (n = 0; n < acl->a_count; n++) {
167                 reiserfs_acl_entry *entry = (reiserfs_acl_entry *) e;
168                 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
169                 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
170                 switch (acl->a_entries[n].e_tag) {
171                 case ACL_USER:
172                 case ACL_GROUP:
173                         entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
174                         e += sizeof(reiserfs_acl_entry);
175                         break;
176
177                 case ACL_USER_OBJ:
178                 case ACL_GROUP_OBJ:
179                 case ACL_MASK:
180                 case ACL_OTHER:
181                         e += sizeof(reiserfs_acl_entry_short);
182                         break;
183
184                 default:
185                         goto fail;
186                 }
187         }
188         return (char *)ext_acl;
189
190       fail:
191         kfree(ext_acl);
192         return ERR_PTR(-EINVAL);
193 }
194
195 /*
196  * Inode operation get_posix_acl().
197  *
198  * inode->i_mutex: down
199  * BKL held [before 2.5.x]
200  */
201 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type)
202 {
203         char *name, *value;
204         struct posix_acl *acl;
205         int size;
206         int retval;
207
208         acl = get_cached_acl(inode, type);
209         if (acl != ACL_NOT_CACHED)
210                 return acl;
211
212         switch (type) {
213         case ACL_TYPE_ACCESS:
214                 name = POSIX_ACL_XATTR_ACCESS;
215                 break;
216         case ACL_TYPE_DEFAULT:
217                 name = POSIX_ACL_XATTR_DEFAULT;
218                 break;
219         default:
220                 BUG();
221         }
222
223         size = reiserfs_xattr_get(inode, name, NULL, 0);
224         if (size < 0) {
225                 if (size == -ENODATA || size == -ENOSYS) {
226                         set_cached_acl(inode, type, NULL);
227                         return NULL;
228                 }
229                 return ERR_PTR(size);
230         }
231
232         value = kmalloc(size, GFP_NOFS);
233         if (!value)
234                 return ERR_PTR(-ENOMEM);
235
236         retval = reiserfs_xattr_get(inode, name, value, size);
237         if (retval == -ENODATA || retval == -ENOSYS) {
238                 /* This shouldn't actually happen as it should have
239                    been caught above.. but just in case */
240                 acl = NULL;
241         } else if (retval < 0) {
242                 acl = ERR_PTR(retval);
243         } else {
244                 acl = posix_acl_from_disk(value, retval);
245         }
246         if (!IS_ERR(acl))
247                 set_cached_acl(inode, type, acl);
248
249         kfree(value);
250         return acl;
251 }
252
253 /*
254  * Inode operation set_posix_acl().
255  *
256  * inode->i_mutex: down
257  * BKL held [before 2.5.x]
258  */
259 static int
260 reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
261                  int type, struct posix_acl *acl)
262 {
263         char *name;
264         void *value = NULL;
265         size_t size = 0;
266         int error;
267
268         if (S_ISLNK(inode->i_mode))
269                 return -EOPNOTSUPP;
270
271         switch (type) {
272         case ACL_TYPE_ACCESS:
273                 name = POSIX_ACL_XATTR_ACCESS;
274                 if (acl) {
275                         mode_t mode = inode->i_mode;
276                         error = posix_acl_equiv_mode(acl, &mode);
277                         if (error < 0)
278                                 return error;
279                         else {
280                                 inode->i_mode = mode;
281                                 if (error == 0)
282                                         acl = NULL;
283                         }
284                 }
285                 break;
286         case ACL_TYPE_DEFAULT:
287                 name = POSIX_ACL_XATTR_DEFAULT;
288                 if (!S_ISDIR(inode->i_mode))
289                         return acl ? -EACCES : 0;
290                 break;
291         default:
292                 return -EINVAL;
293         }
294
295         if (acl) {
296                 value = posix_acl_to_disk(acl, &size);
297                 if (IS_ERR(value))
298                         return (int)PTR_ERR(value);
299         }
300
301         error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
302
303         /*
304          * Ensure that the inode gets dirtied if we're only using
305          * the mode bits and an old ACL didn't exist. We don't need
306          * to check if the inode is hashed here since we won't get
307          * called by reiserfs_inherit_default_acl().
308          */
309         if (error == -ENODATA) {
310                 error = 0;
311                 if (type == ACL_TYPE_ACCESS) {
312                         inode->i_ctime = CURRENT_TIME_SEC;
313                         mark_inode_dirty(inode);
314                 }
315         }
316
317         kfree(value);
318
319         if (!error)
320                 set_cached_acl(inode, type, acl);
321
322         return error;
323 }
324
325 /* dir->i_mutex: locked,
326  * inode is new and not released into the wild yet */
327 int
328 reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
329                              struct inode *dir, struct dentry *dentry,
330                              struct inode *inode)
331 {
332         struct posix_acl *acl;
333         int err = 0;
334
335         /* ACLs only get applied to files and directories */
336         if (S_ISLNK(inode->i_mode))
337                 return 0;
338
339         /* ACLs can only be used on "new" objects, so if it's an old object
340          * there is nothing to inherit from */
341         if (get_inode_sd_version(dir) == STAT_DATA_V1)
342                 goto apply_umask;
343
344         /* Don't apply ACLs to objects in the .reiserfs_priv tree.. This
345          * would be useless since permissions are ignored, and a pain because
346          * it introduces locking cycles */
347         if (IS_PRIVATE(dir)) {
348                 inode->i_flags |= S_PRIVATE;
349                 goto apply_umask;
350         }
351
352         acl = reiserfs_get_acl(dir, ACL_TYPE_DEFAULT);
353         if (IS_ERR(acl))
354                 return PTR_ERR(acl);
355
356         if (acl) {
357                 mode_t mode = inode->i_mode;
358
359                 /* Copy the default ACL to the default ACL of a new directory */
360                 if (S_ISDIR(inode->i_mode)) {
361                         err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
362                                                acl);
363                         if (err)
364                                 goto cleanup;
365                 }
366
367                 /* Now we reconcile the new ACL and the mode,
368                    potentially modifying both */
369                 err = posix_acl_create(&acl, GFP_NOFS, &mode);
370                 if (err < 0)
371                         return err;
372
373                 inode->i_mode = mode;
374
375                 /* If we need an ACL.. */
376                 if (err > 0)
377                         err = reiserfs_set_acl(th, inode, ACL_TYPE_ACCESS, acl);
378               cleanup:
379                 posix_acl_release(acl);
380         } else {
381               apply_umask:
382                 /* no ACL, apply umask */
383                 inode->i_mode &= ~current_umask();
384         }
385
386         return err;
387 }
388
389 /* This is used to cache the default acl before a new object is created.
390  * The biggest reason for this is to get an idea of how many blocks will
391  * actually be required for the create operation if we must inherit an ACL.
392  * An ACL write can add up to 3 object creations and an additional file write
393  * so we'd prefer not to reserve that many blocks in the journal if we can.
394  * It also has the advantage of not loading the ACL with a transaction open,
395  * this may seem silly, but if the owner of the directory is doing the
396  * creation, the ACL may not be loaded since the permissions wouldn't require
397  * it.
398  * We return the number of blocks required for the transaction.
399  */
400 int reiserfs_cache_default_acl(struct inode *inode)
401 {
402         struct posix_acl *acl;
403         int nblocks = 0;
404
405         if (IS_PRIVATE(inode))
406                 return 0;
407
408         acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
409
410         if (acl && !IS_ERR(acl)) {
411                 int size = reiserfs_acl_size(acl->a_count);
412
413                 /* Other xattrs can be created during inode creation. We don't
414                  * want to claim too many blocks, so we check to see if we
415                  * we need to create the tree to the xattrs, and then we
416                  * just want two files. */
417                 nblocks = reiserfs_xattr_jcreate_nblocks(inode);
418                 nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
419
420                 REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
421
422                 /* We need to account for writes + bitmaps for two files */
423                 nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
424                 posix_acl_release(acl);
425         }
426
427         return nblocks;
428 }
429
430 int reiserfs_acl_chmod(struct inode *inode)
431 {
432         struct reiserfs_transaction_handle th;
433         struct posix_acl *acl;
434         size_t size;
435         int depth;
436         int error;
437
438         if (S_ISLNK(inode->i_mode))
439                 return -EOPNOTSUPP;
440
441         if (get_inode_sd_version(inode) == STAT_DATA_V1 ||
442             !reiserfs_posixacl(inode->i_sb)) {
443                 return 0;
444         }
445
446         reiserfs_write_unlock(inode->i_sb);
447         acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
448         reiserfs_write_lock(inode->i_sb);
449         if (!acl)
450                 return 0;
451         if (IS_ERR(acl))
452                 return PTR_ERR(acl);
453         error = posix_acl_chmod(&acl, GFP_NOFS, inode->i_mode);
454         if (error)
455                 return error;
456
457         size = reiserfs_xattr_nblocks(inode, reiserfs_acl_size(acl->a_count));
458         depth = reiserfs_write_lock_once(inode->i_sb);
459         error = journal_begin(&th, inode->i_sb, size * 2);
460         if (!error) {
461                 int error2;
462                 error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, acl);
463                 error2 = journal_end(&th, inode->i_sb, size * 2);
464                 if (error2)
465                         error = error2;
466         }
467         reiserfs_write_unlock_once(inode->i_sb, depth);
468         posix_acl_release(acl);
469         return error;
470 }
471
472 static size_t posix_acl_access_list(struct dentry *dentry, char *list,
473                                     size_t list_size, const char *name,
474                                     size_t name_len, int type)
475 {
476         const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
477         if (!reiserfs_posixacl(dentry->d_sb))
478                 return 0;
479         if (list && size <= list_size)
480                 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
481         return size;
482 }
483
484 const struct xattr_handler reiserfs_posix_acl_access_handler = {
485         .prefix = POSIX_ACL_XATTR_ACCESS,
486         .flags = ACL_TYPE_ACCESS,
487         .get = posix_acl_get,
488         .set = posix_acl_set,
489         .list = posix_acl_access_list,
490 };
491
492 static size_t posix_acl_default_list(struct dentry *dentry, char *list,
493                                      size_t list_size, const char *name,
494                                      size_t name_len, int type)
495 {
496         const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
497         if (!reiserfs_posixacl(dentry->d_sb))
498                 return 0;
499         if (list && size <= list_size)
500                 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
501         return size;
502 }
503
504 const struct xattr_handler reiserfs_posix_acl_default_handler = {
505         .prefix = POSIX_ACL_XATTR_DEFAULT,
506         .flags = ACL_TYPE_DEFAULT,
507         .get = posix_acl_get,
508         .set = posix_acl_set,
509         .list = posix_acl_default_list,
510 };