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