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