posix_acl: Clear SGID bit when setting file permissions
[pandora-kernel.git] / fs / ocfs2 / acl.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * acl.c
5  *
6  * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
7  *
8  * CREDITS:
9  * Lots of code in this file is copy from linux/fs/ext3/acl.c.
10  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public
14  * License version 2 as published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  */
21
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26
27 #include <cluster/masklog.h>
28
29 #include "ocfs2.h"
30 #include "alloc.h"
31 #include "dlmglue.h"
32 #include "file.h"
33 #include "inode.h"
34 #include "journal.h"
35 #include "ocfs2_fs.h"
36
37 #include "xattr.h"
38 #include "acl.h"
39
40 /*
41  * Convert from xattr value to acl struct.
42  */
43 static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
44 {
45         int n, count;
46         struct posix_acl *acl;
47
48         if (!value)
49                 return NULL;
50         if (size < sizeof(struct posix_acl_entry))
51                 return ERR_PTR(-EINVAL);
52
53         count = size / sizeof(struct posix_acl_entry);
54         if (count < 0)
55                 return ERR_PTR(-EINVAL);
56         if (count == 0)
57                 return NULL;
58
59         acl = posix_acl_alloc(count, GFP_NOFS);
60         if (!acl)
61                 return ERR_PTR(-ENOMEM);
62         for (n = 0; n < count; n++) {
63                 struct ocfs2_acl_entry *entry =
64                         (struct ocfs2_acl_entry *)value;
65
66                 acl->a_entries[n].e_tag  = le16_to_cpu(entry->e_tag);
67                 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
68                 acl->a_entries[n].e_id   = le32_to_cpu(entry->e_id);
69                 value += sizeof(struct posix_acl_entry);
70
71         }
72         return acl;
73 }
74
75 /*
76  * Convert acl struct to xattr value.
77  */
78 static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
79 {
80         struct ocfs2_acl_entry *entry = NULL;
81         char *ocfs2_acl;
82         size_t n;
83
84         *size = acl->a_count * sizeof(struct posix_acl_entry);
85
86         ocfs2_acl = kmalloc(*size, GFP_NOFS);
87         if (!ocfs2_acl)
88                 return ERR_PTR(-ENOMEM);
89
90         entry = (struct ocfs2_acl_entry *)ocfs2_acl;
91         for (n = 0; n < acl->a_count; n++, entry++) {
92                 entry->e_tag  = cpu_to_le16(acl->a_entries[n].e_tag);
93                 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
94                 entry->e_id   = cpu_to_le32(acl->a_entries[n].e_id);
95         }
96         return ocfs2_acl;
97 }
98
99 static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
100                                               int type,
101                                               struct buffer_head *di_bh)
102 {
103         int name_index;
104         char *value = NULL;
105         struct posix_acl *acl;
106         int retval;
107
108         switch (type) {
109         case ACL_TYPE_ACCESS:
110                 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
111                 break;
112         case ACL_TYPE_DEFAULT:
113                 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
114                 break;
115         default:
116                 return ERR_PTR(-EINVAL);
117         }
118
119         retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
120         if (retval > 0) {
121                 value = kmalloc(retval, GFP_NOFS);
122                 if (!value)
123                         return ERR_PTR(-ENOMEM);
124                 retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
125                                                 "", value, retval);
126         }
127
128         if (retval > 0)
129                 acl = ocfs2_acl_from_xattr(value, retval);
130         else if (retval == -ENODATA || retval == 0)
131                 acl = NULL;
132         else
133                 acl = ERR_PTR(retval);
134
135         kfree(value);
136
137         return acl;
138 }
139
140
141 /*
142  * Get posix acl.
143  */
144 static struct posix_acl *ocfs2_get_acl(struct inode *inode, int type)
145 {
146         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
147         struct buffer_head *di_bh = NULL;
148         struct posix_acl *acl;
149         int ret;
150
151         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
152                 return NULL;
153
154         ret = ocfs2_inode_lock(inode, &di_bh, 0);
155         if (ret < 0) {
156                 mlog_errno(ret);
157                 acl = ERR_PTR(ret);
158                 return acl;
159         }
160
161         acl = ocfs2_get_acl_nolock(inode, type, di_bh);
162
163         ocfs2_inode_unlock(inode, 0);
164
165         brelse(di_bh);
166
167         return acl;
168 }
169
170 /*
171  * Helper function to set i_mode in memory and disk. Some call paths
172  * will not have di_bh or a journal handle to pass, in which case it
173  * will create it's own.
174  */
175 static int ocfs2_acl_set_mode(struct inode *inode, struct buffer_head *di_bh,
176                               handle_t *handle, umode_t new_mode)
177 {
178         int ret, commit_handle = 0;
179         struct ocfs2_dinode *di;
180
181         if (di_bh == NULL) {
182                 ret = ocfs2_read_inode_block(inode, &di_bh);
183                 if (ret) {
184                         mlog_errno(ret);
185                         goto out;
186                 }
187         } else
188                 get_bh(di_bh);
189
190         if (handle == NULL) {
191                 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
192                                            OCFS2_INODE_UPDATE_CREDITS);
193                 if (IS_ERR(handle)) {
194                         ret = PTR_ERR(handle);
195                         mlog_errno(ret);
196                         goto out_brelse;
197                 }
198
199                 commit_handle = 1;
200         }
201
202         di = (struct ocfs2_dinode *)di_bh->b_data;
203         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
204                                       OCFS2_JOURNAL_ACCESS_WRITE);
205         if (ret) {
206                 mlog_errno(ret);
207                 goto out_commit;
208         }
209
210         inode->i_mode = new_mode;
211         inode->i_ctime = CURRENT_TIME;
212         di->i_mode = cpu_to_le16(inode->i_mode);
213         di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
214         di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
215
216         ocfs2_journal_dirty(handle, di_bh);
217
218 out_commit:
219         if (commit_handle)
220                 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
221 out_brelse:
222         brelse(di_bh);
223 out:
224         return ret;
225 }
226
227 /*
228  * Set the access or default ACL of an inode.
229  */
230 static int ocfs2_set_acl(handle_t *handle,
231                          struct inode *inode,
232                          struct buffer_head *di_bh,
233                          int type,
234                          struct posix_acl *acl,
235                          struct ocfs2_alloc_context *meta_ac,
236                          struct ocfs2_alloc_context *data_ac)
237 {
238         int name_index;
239         void *value = NULL;
240         size_t size = 0;
241         int ret;
242
243         if (S_ISLNK(inode->i_mode))
244                 return -EOPNOTSUPP;
245
246         switch (type) {
247         case ACL_TYPE_ACCESS:
248                 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
249                 if (acl) {
250                         umode_t mode;
251                         ret = posix_acl_update_mode(inode, &mode, &acl);
252                         if (ret)
253                                 return ret;
254                         else {
255                                 ret = ocfs2_acl_set_mode(inode, di_bh,
256                                                          handle, mode);
257                                 if (ret)
258                                         return ret;
259
260                         }
261                 }
262                 break;
263         case ACL_TYPE_DEFAULT:
264                 name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
265                 if (!S_ISDIR(inode->i_mode))
266                         return acl ? -EACCES : 0;
267                 break;
268         default:
269                 return -EINVAL;
270         }
271
272         if (acl) {
273                 value = ocfs2_acl_to_xattr(acl, &size);
274                 if (IS_ERR(value))
275                         return (int)PTR_ERR(value);
276         }
277
278         if (handle)
279                 ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
280                                              "", value, size, 0,
281                                              meta_ac, data_ac);
282         else
283                 ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
284
285         kfree(value);
286
287         return ret;
288 }
289
290 struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
291 {
292         struct ocfs2_super *osb;
293         struct buffer_head *di_bh = NULL;
294         struct posix_acl *acl;
295         int ret = -EAGAIN;
296
297         osb = OCFS2_SB(inode->i_sb);
298         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
299                 return NULL;
300
301         ret = ocfs2_read_inode_block(inode, &di_bh);
302         if (ret < 0)
303                 return ERR_PTR(ret);
304
305         acl = ocfs2_get_acl_nolock(inode, type, di_bh);
306
307         brelse(di_bh);
308
309         return acl;
310 }
311
312 int ocfs2_acl_chmod(struct inode *inode)
313 {
314         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
315         struct posix_acl *acl;
316         int ret;
317
318         if (S_ISLNK(inode->i_mode))
319                 return -EOPNOTSUPP;
320
321         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
322                 return 0;
323
324         acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
325         if (IS_ERR(acl) || !acl)
326                 return PTR_ERR(acl);
327         ret = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
328         if (ret)
329                 return ret;
330         ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
331                             acl, NULL, NULL);
332         posix_acl_release(acl);
333         return ret;
334 }
335
336 /*
337  * Initialize the ACLs of a new inode. If parent directory has default ACL,
338  * then clone to new inode. Called from ocfs2_mknod.
339  */
340 int ocfs2_init_acl(handle_t *handle,
341                    struct inode *inode,
342                    struct inode *dir,
343                    struct buffer_head *di_bh,
344                    struct buffer_head *dir_bh,
345                    struct ocfs2_alloc_context *meta_ac,
346                    struct ocfs2_alloc_context *data_ac)
347 {
348         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
349         struct posix_acl *acl = NULL;
350         int ret = 0, ret2;
351         umode_t mode;
352
353         if (!S_ISLNK(inode->i_mode)) {
354                 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
355                         acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
356                                                    dir_bh);
357                         if (IS_ERR(acl))
358                                 return PTR_ERR(acl);
359                 }
360                 if (!acl) {
361                         mode = inode->i_mode & ~current_umask();
362                         ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
363                         if (ret) {
364                                 mlog_errno(ret);
365                                 goto cleanup;
366                         }
367                 }
368         }
369         if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
370                 if (S_ISDIR(inode->i_mode)) {
371                         ret = ocfs2_set_acl(handle, inode, di_bh,
372                                             ACL_TYPE_DEFAULT, acl,
373                                             meta_ac, data_ac);
374                         if (ret)
375                                 goto cleanup;
376                 }
377                 mode = inode->i_mode;
378                 ret = posix_acl_create(&acl, GFP_NOFS, &mode);
379                 if (ret < 0)
380                         return ret;
381
382                 ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
383                 if (ret2) {
384                         mlog_errno(ret2);
385                         ret = ret2;
386                         goto cleanup;
387                 }
388                 if (ret > 0) {
389                         ret = ocfs2_set_acl(handle, inode,
390                                             di_bh, ACL_TYPE_ACCESS,
391                                             acl, meta_ac, data_ac);
392                 }
393         }
394 cleanup:
395         posix_acl_release(acl);
396         return ret;
397 }
398
399 static size_t ocfs2_xattr_list_acl_access(struct dentry *dentry,
400                                           char *list,
401                                           size_t list_len,
402                                           const char *name,
403                                           size_t name_len,
404                                           int type)
405 {
406         struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
407         const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
408
409         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
410                 return 0;
411
412         if (list && size <= list_len)
413                 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
414         return size;
415 }
416
417 static size_t ocfs2_xattr_list_acl_default(struct dentry *dentry,
418                                            char *list,
419                                            size_t list_len,
420                                            const char *name,
421                                            size_t name_len,
422                                            int type)
423 {
424         struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
425         const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
426
427         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
428                 return 0;
429
430         if (list && size <= list_len)
431                 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
432         return size;
433 }
434
435 static int ocfs2_xattr_get_acl(struct dentry *dentry, const char *name,
436                 void *buffer, size_t size, int type)
437 {
438         struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
439         struct posix_acl *acl;
440         int ret;
441
442         if (strcmp(name, "") != 0)
443                 return -EINVAL;
444         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
445                 return -EOPNOTSUPP;
446
447         acl = ocfs2_get_acl(dentry->d_inode, type);
448         if (IS_ERR(acl))
449                 return PTR_ERR(acl);
450         if (acl == NULL)
451                 return -ENODATA;
452         ret = posix_acl_to_xattr(acl, buffer, size);
453         posix_acl_release(acl);
454
455         return ret;
456 }
457
458 static int ocfs2_xattr_set_acl(struct dentry *dentry, const char *name,
459                 const void *value, size_t size, int flags, int type)
460 {
461         struct inode *inode = dentry->d_inode;
462         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
463         struct posix_acl *acl;
464         int ret = 0;
465
466         if (strcmp(name, "") != 0)
467                 return -EINVAL;
468         if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
469                 return -EOPNOTSUPP;
470
471         if (!inode_owner_or_capable(inode))
472                 return -EPERM;
473
474         if (value) {
475                 acl = posix_acl_from_xattr(value, size);
476                 if (IS_ERR(acl))
477                         return PTR_ERR(acl);
478                 else if (acl) {
479                         ret = posix_acl_valid(acl);
480                         if (ret)
481                                 goto cleanup;
482                 }
483         } else
484                 acl = NULL;
485
486         ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
487
488 cleanup:
489         posix_acl_release(acl);
490         return ret;
491 }
492
493 const struct xattr_handler ocfs2_xattr_acl_access_handler = {
494         .prefix = POSIX_ACL_XATTR_ACCESS,
495         .flags  = ACL_TYPE_ACCESS,
496         .list   = ocfs2_xattr_list_acl_access,
497         .get    = ocfs2_xattr_get_acl,
498         .set    = ocfs2_xattr_set_acl,
499 };
500
501 const struct xattr_handler ocfs2_xattr_acl_default_handler = {
502         .prefix = POSIX_ACL_XATTR_DEFAULT,
503         .flags  = ACL_TYPE_DEFAULT,
504         .list   = ocfs2_xattr_list_acl_default,
505         .get    = ocfs2_xattr_get_acl,
506         .set    = ocfs2_xattr_set_acl,
507 };