pandora: defconfig: update
[pandora-kernel.git] / fs / generic_acl.c
1 /*
2  * (C) 2005 Andreas Gruenbacher <agruen@suse.de>
3  *
4  * This file is released under the GPL.
5  *
6  * Generic ACL support for in-memory filesystems.
7  */
8
9 #include <linux/sched.h>
10 #include <linux/gfp.h>
11 #include <linux/fs.h>
12 #include <linux/generic_acl.h>
13 #include <linux/posix_acl.h>
14 #include <linux/posix_acl_xattr.h>
15
16
17 static size_t
18 generic_acl_list(struct dentry *dentry, char *list, size_t list_size,
19                 const char *name, size_t name_len, int type)
20 {
21         struct posix_acl *acl;
22         const char *xname;
23         size_t size;
24
25         acl = get_cached_acl(dentry->d_inode, type);
26         if (!acl)
27                 return 0;
28         posix_acl_release(acl);
29
30         switch (type) {
31         case ACL_TYPE_ACCESS:
32                 xname = POSIX_ACL_XATTR_ACCESS;
33                 break;
34         case ACL_TYPE_DEFAULT:
35                 xname = POSIX_ACL_XATTR_DEFAULT;
36                 break;
37         default:
38                 return 0;
39         }
40         size = strlen(xname) + 1;
41         if (list && size <= list_size)
42                 memcpy(list, xname, size);
43         return size;
44 }
45
46 static int
47 generic_acl_get(struct dentry *dentry, const char *name, void *buffer,
48                      size_t size, int type)
49 {
50         struct posix_acl *acl;
51         int error;
52
53         if (strcmp(name, "") != 0)
54                 return -EINVAL;
55
56         acl = get_cached_acl(dentry->d_inode, type);
57         if (!acl)
58                 return -ENODATA;
59         error = posix_acl_to_xattr(acl, buffer, size);
60         posix_acl_release(acl);
61
62         return error;
63 }
64
65 static int
66 generic_acl_set(struct dentry *dentry, const char *name, const void *value,
67                      size_t size, int flags, int type)
68 {
69         struct inode *inode = dentry->d_inode;
70         struct posix_acl *acl = NULL;
71         int error;
72
73         if (strcmp(name, "") != 0)
74                 return -EINVAL;
75         if (S_ISLNK(inode->i_mode))
76                 return -EOPNOTSUPP;
77         if (!inode_owner_or_capable(inode))
78                 return -EPERM;
79         if (value) {
80                 acl = posix_acl_from_xattr(value, size);
81                 if (IS_ERR(acl))
82                         return PTR_ERR(acl);
83         }
84         if (acl) {
85                 error = posix_acl_valid(acl);
86                 if (error)
87                         goto failed;
88                 switch (type) {
89                 case ACL_TYPE_ACCESS: {
90                         struct posix_acl *saved_acl = acl;
91
92                         error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
93                         if (acl == NULL)
94                                 posix_acl_release(saved_acl);
95                         if (error)
96                                 goto failed;
97                         inode->i_ctime = CURRENT_TIME;
98                         break;
99                 }
100                 case ACL_TYPE_DEFAULT:
101                         if (!S_ISDIR(inode->i_mode)) {
102                                 error = -EINVAL;
103                                 goto failed;
104                         }
105                         break;
106                 }
107         }
108         set_cached_acl(inode, type, acl);
109         error = 0;
110 failed:
111         posix_acl_release(acl);
112         return error;
113 }
114
115 /**
116  * generic_acl_init  -  Take care of acl inheritance at @inode create time
117  *
118  * Files created inside a directory with a default ACL inherit the
119  * directory's default ACL.
120  */
121 int
122 generic_acl_init(struct inode *inode, struct inode *dir)
123 {
124         struct posix_acl *acl = NULL;
125         int error;
126
127         if (!S_ISLNK(inode->i_mode))
128                 acl = get_cached_acl(dir, ACL_TYPE_DEFAULT);
129         if (acl) {
130                 if (S_ISDIR(inode->i_mode))
131                         set_cached_acl(inode, ACL_TYPE_DEFAULT, acl);
132                 error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
133                 if (error < 0)
134                         return error;
135                 if (error > 0)
136                         set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
137         } else {
138                 inode->i_mode &= ~current_umask();
139         }
140         error = 0;
141
142         posix_acl_release(acl);
143         return error;
144 }
145
146 /**
147  * generic_acl_chmod  -  change the access acl of @inode upon chmod()
148  *
149  * A chmod also changes the permissions of the owner, group/mask, and
150  * other ACL entries.
151  */
152 int
153 generic_acl_chmod(struct inode *inode)
154 {
155         struct posix_acl *acl;
156         int error = 0;
157
158         if (S_ISLNK(inode->i_mode))
159                 return -EOPNOTSUPP;
160         acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
161         if (acl) {
162                 error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
163                 if (error)
164                         return error;
165                 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
166                 posix_acl_release(acl);
167         }
168         return error;
169 }
170
171 const struct xattr_handler generic_acl_access_handler = {
172         .prefix = POSIX_ACL_XATTR_ACCESS,
173         .flags  = ACL_TYPE_ACCESS,
174         .list   = generic_acl_list,
175         .get    = generic_acl_get,
176         .set    = generic_acl_set,
177 };
178
179 const struct xattr_handler generic_acl_default_handler = {
180         .prefix = POSIX_ACL_XATTR_DEFAULT,
181         .flags  = ACL_TYPE_DEFAULT,
182         .list   = generic_acl_list,
183         .get    = generic_acl_get,
184         .set    = generic_acl_set,
185 };