blob: 89b6468700e770215cb063843b6a983bbfece208 [file] [log] [blame]
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00001#include <linux/fs.h>
2#include <linux/nfs.h>
3#include <linux/nfs3.h>
4#include <linux/nfs_fs.h>
5#include <linux/xattr_acl.h>
6#include <linux/nfsacl.h>
7
8#define NFSDBG_FACILITY NFSDBG_PROC
9
10ssize_t nfs3_listxattr(struct dentry *dentry, char *buffer, size_t size)
11{
12 struct inode *inode = dentry->d_inode;
13 struct posix_acl *acl;
14 int pos=0, len=0;
15
16# define output(s) do { \
17 if (pos + sizeof(s) <= size) { \
18 memcpy(buffer + pos, s, sizeof(s)); \
19 pos += sizeof(s); \
20 } \
21 len += sizeof(s); \
22 } while(0)
23
24 acl = nfs3_proc_getacl(inode, ACL_TYPE_ACCESS);
25 if (IS_ERR(acl))
26 return PTR_ERR(acl);
27 if (acl) {
28 output("system.posix_acl_access");
29 posix_acl_release(acl);
30 }
31
32 if (S_ISDIR(inode->i_mode)) {
33 acl = nfs3_proc_getacl(inode, ACL_TYPE_DEFAULT);
34 if (IS_ERR(acl))
35 return PTR_ERR(acl);
36 if (acl) {
37 output("system.posix_acl_default");
38 posix_acl_release(acl);
39 }
40 }
41
42# undef output
43
44 if (!buffer || len <= size)
45 return len;
46 return -ERANGE;
47}
48
49ssize_t nfs3_getxattr(struct dentry *dentry, const char *name,
50 void *buffer, size_t size)
51{
52 struct inode *inode = dentry->d_inode;
53 struct posix_acl *acl;
54 int type, error = 0;
55
56 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0)
57 type = ACL_TYPE_ACCESS;
58 else if (strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0)
59 type = ACL_TYPE_DEFAULT;
60 else
61 return -EOPNOTSUPP;
62
63 acl = nfs3_proc_getacl(inode, type);
64 if (IS_ERR(acl))
65 return PTR_ERR(acl);
66 else if (acl) {
67 if (type == ACL_TYPE_ACCESS && acl->a_count == 0)
68 error = -ENODATA;
69 else
70 error = posix_acl_to_xattr(acl, buffer, size);
71 posix_acl_release(acl);
72 } else
73 error = -ENODATA;
74
75 return error;
76}
77
78int nfs3_setxattr(struct dentry *dentry, const char *name,
79 const void *value, size_t size, int flags)
80{
81 struct inode *inode = dentry->d_inode;
82 struct posix_acl *acl;
83 int type, error;
84
85 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0)
86 type = ACL_TYPE_ACCESS;
87 else if (strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0)
88 type = ACL_TYPE_DEFAULT;
89 else
90 return -EOPNOTSUPP;
91
92 acl = posix_acl_from_xattr(value, size);
93 if (IS_ERR(acl))
94 return PTR_ERR(acl);
95 error = nfs3_proc_setacl(inode, type, acl);
96 posix_acl_release(acl);
97
98 return error;
99}
100
101int nfs3_removexattr(struct dentry *dentry, const char *name)
102{
103 struct inode *inode = dentry->d_inode;
104 int type;
105
106 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0)
107 type = ACL_TYPE_ACCESS;
108 else if (strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0)
109 type = ACL_TYPE_DEFAULT;
110 else
111 return -EOPNOTSUPP;
112
113 return nfs3_proc_setacl(inode, type, NULL);
114}
115
116struct posix_acl *nfs3_proc_getacl(struct inode *inode, int type)
117{
118 struct nfs_server *server = NFS_SERVER(inode);
119 struct nfs_fattr fattr;
120 struct page *pages[NFSACL_MAXPAGES] = { };
121 struct nfs3_getaclargs args = {
122 .fh = NFS_FH(inode),
123 /* The xdr layer may allocate pages here. */
124 .pages = pages,
125 };
126 struct nfs3_getaclres res = {
127 .fattr = &fattr,
128 };
129 struct posix_acl *acl = NULL;
130 int status, count;
131
132 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
133 return ERR_PTR(-EOPNOTSUPP);
134
135 switch (type) {
136 case ACL_TYPE_ACCESS:
137 args.mask = NFS_ACLCNT|NFS_ACL;
138 break;
139
140 case ACL_TYPE_DEFAULT:
141 if (!S_ISDIR(inode->i_mode))
142 return NULL;
143 args.mask = NFS_DFACLCNT|NFS_DFACL;
144 break;
145
146 default:
147 return ERR_PTR(-EINVAL);
148 }
149
150 dprintk("NFS call getacl\n");
151 status = rpc_call(server->client_acl, ACLPROC3_GETACL,
152 &args, &res, 0);
153 dprintk("NFS reply getacl: %d\n", status);
154
155 /* pages may have been allocated at the xdr layer. */
156 for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
157 __free_page(args.pages[count]);
158
159 switch (status) {
160 case 0:
161 status = nfs_refresh_inode(inode, &fattr);
162 break;
163 case -EPFNOSUPPORT:
164 case -EPROTONOSUPPORT:
165 dprintk("NFS_V3_ACL extension not supported; disabling\n");
166 server->caps &= ~NFS_CAP_ACLS;
167 case -ENOTSUPP:
168 status = -EOPNOTSUPP;
169 default:
170 goto getout;
171 }
172 if ((args.mask & res.mask) != args.mask) {
173 status = -EIO;
174 goto getout;
175 }
176
177 if (res.acl_access != NULL) {
178 if (posix_acl_equiv_mode(res.acl_access, NULL) == 0) {
179 posix_acl_release(res.acl_access);
180 res.acl_access = NULL;
181 }
182 }
183
184 switch(type) {
185 case ACL_TYPE_ACCESS:
186 acl = res.acl_access;
187 res.acl_access = NULL;
188 break;
189
190 case ACL_TYPE_DEFAULT:
191 acl = res.acl_default;
192 res.acl_default = NULL;
193 }
194
195getout:
196 posix_acl_release(res.acl_access);
197 posix_acl_release(res.acl_default);
198
199 if (status != 0) {
200 posix_acl_release(acl);
201 acl = ERR_PTR(status);
202 }
203 return acl;
204}
205
206static int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
207 struct posix_acl *dfacl)
208{
209 struct nfs_server *server = NFS_SERVER(inode);
210 struct nfs_fattr fattr;
211 struct page *pages[NFSACL_MAXPAGES] = { };
212 struct nfs3_setaclargs args = {
213 .inode = inode,
214 .mask = NFS_ACL,
215 .acl_access = acl,
216 .pages = pages,
217 };
218 int status, count;
219
220 status = -EOPNOTSUPP;
221 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
222 goto out;
223
224 /* We are doing this here, because XDR marshalling can only
225 return -ENOMEM. */
226 status = -ENOSPC;
227 if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
228 goto out;
229 if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
230 goto out;
231 if (S_ISDIR(inode->i_mode)) {
232 args.mask |= NFS_DFACL;
233 args.acl_default = dfacl;
234 }
235
236 dprintk("NFS call setacl\n");
237 nfs_begin_data_update(inode);
238 status = rpc_call(server->client_acl, ACLPROC3_SETACL,
239 &args, &fattr, 0);
240 NFS_FLAGS(inode) |= NFS_INO_INVALID_ACCESS;
241 nfs_end_data_update(inode);
242 dprintk("NFS reply setacl: %d\n", status);
243
244 /* pages may have been allocated at the xdr layer. */
245 for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
246 __free_page(args.pages[count]);
247
248 switch (status) {
249 case 0:
250 status = nfs_refresh_inode(inode, &fattr);
251 break;
252 case -EPFNOSUPPORT:
253 case -EPROTONOSUPPORT:
254 dprintk("NFS_V3_ACL SETACL RPC not supported"
255 "(will not retry)\n");
256 server->caps &= ~NFS_CAP_ACLS;
257 case -ENOTSUPP:
258 status = -EOPNOTSUPP;
259 }
260out:
261 return status;
262}
263
264int nfs3_proc_setacl(struct inode *inode, int type, struct posix_acl *acl)
265{
266 struct posix_acl *alloc = NULL, *dfacl = NULL;
267 int status;
268
269 if (S_ISDIR(inode->i_mode)) {
270 switch(type) {
271 case ACL_TYPE_ACCESS:
272 alloc = dfacl = nfs3_proc_getacl(inode,
273 ACL_TYPE_DEFAULT);
274 if (IS_ERR(alloc))
275 goto fail;
276 break;
277
278 case ACL_TYPE_DEFAULT:
279 dfacl = acl;
280 alloc = acl = nfs3_proc_getacl(inode,
281 ACL_TYPE_ACCESS);
282 if (IS_ERR(alloc))
283 goto fail;
284 break;
285
286 default:
287 return -EINVAL;
288 }
289 } else if (type != ACL_TYPE_ACCESS)
290 return -EINVAL;
291
292 if (acl == NULL) {
293 alloc = acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
294 if (IS_ERR(alloc))
295 goto fail;
296 }
297 status = nfs3_proc_setacls(inode, acl, dfacl);
298 posix_acl_release(alloc);
299 return status;
300
301fail:
302 return PTR_ERR(alloc);
303}
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000304
305int nfs3_proc_set_default_acl(struct inode *dir, struct inode *inode,
306 mode_t mode)
307{
308 struct posix_acl *dfacl, *acl;
309 int error = 0;
310
311 dfacl = nfs3_proc_getacl(dir, ACL_TYPE_DEFAULT);
312 if (IS_ERR(dfacl)) {
313 error = PTR_ERR(dfacl);
314 return (error == -EOPNOTSUPP) ? 0 : error;
315 }
316 if (!dfacl)
317 return 0;
318 acl = posix_acl_clone(dfacl, GFP_KERNEL);
319 error = -ENOMEM;
320 if (!acl)
321 goto out_release_dfacl;
322 error = posix_acl_create_masq(acl, &mode);
323 if (error < 0)
324 goto out_release_acl;
325 error = nfs3_proc_setacls(inode, acl, S_ISDIR(inode->i_mode) ?
326 dfacl : NULL);
327out_release_acl:
328 posix_acl_release(acl);
329out_release_dfacl:
330 posix_acl_release(dfacl);
331 return error;
332}