blob: 451112ff9aa47cda581abb6f4c028a5ca4379f77 [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
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000116static void __nfs3_forget_cached_acls(struct nfs_inode *nfsi)
117{
118 if (nfsi->acl_access != ERR_PTR(-EAGAIN)) {
119 posix_acl_release(nfsi->acl_access);
120 nfsi->acl_access = ERR_PTR(-EAGAIN);
121 }
122 if (nfsi->acl_default != ERR_PTR(-EAGAIN)) {
123 posix_acl_release(nfsi->acl_default);
124 nfsi->acl_default = ERR_PTR(-EAGAIN);
125 }
126}
127
128void nfs3_forget_cached_acls(struct inode *inode)
129{
130 dprintk("NFS: nfs3_forget_cached_acls(%s/%ld)\n", inode->i_sb->s_id,
131 inode->i_ino);
132 spin_lock(&inode->i_lock);
133 __nfs3_forget_cached_acls(NFS_I(inode));
134 spin_unlock(&inode->i_lock);
135}
136
137static struct posix_acl *nfs3_get_cached_acl(struct inode *inode, int type)
138{
139 struct nfs_inode *nfsi = NFS_I(inode);
140 struct posix_acl *acl = ERR_PTR(-EAGAIN);
141
142 spin_lock(&inode->i_lock);
143 switch(type) {
144 case ACL_TYPE_ACCESS:
145 acl = nfsi->acl_access;
146 break;
147
148 case ACL_TYPE_DEFAULT:
149 acl = nfsi->acl_default;
150 break;
151
152 default:
153 return ERR_PTR(-EINVAL);
154 }
155 if (acl == ERR_PTR(-EAGAIN))
156 acl = ERR_PTR(-EAGAIN);
157 else
158 acl = posix_acl_dup(acl);
159 spin_unlock(&inode->i_lock);
160 dprintk("NFS: nfs3_get_cached_acl(%s/%ld, %d) = %p\n", inode->i_sb->s_id,
161 inode->i_ino, type, acl);
162 return acl;
163}
164
165static void nfs3_cache_acls(struct inode *inode, struct posix_acl *acl,
166 struct posix_acl *dfacl)
167{
168 struct nfs_inode *nfsi = NFS_I(inode);
169
170 dprintk("nfs3_cache_acls(%s/%ld, %p, %p)\n", inode->i_sb->s_id,
171 inode->i_ino, acl, dfacl);
172 spin_lock(&inode->i_lock);
173 __nfs3_forget_cached_acls(NFS_I(inode));
174 nfsi->acl_access = posix_acl_dup(acl);
175 nfsi->acl_default = posix_acl_dup(dfacl);
176 spin_unlock(&inode->i_lock);
177}
178
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000179struct posix_acl *nfs3_proc_getacl(struct inode *inode, int type)
180{
181 struct nfs_server *server = NFS_SERVER(inode);
182 struct nfs_fattr fattr;
183 struct page *pages[NFSACL_MAXPAGES] = { };
184 struct nfs3_getaclargs args = {
185 .fh = NFS_FH(inode),
186 /* The xdr layer may allocate pages here. */
187 .pages = pages,
188 };
189 struct nfs3_getaclres res = {
190 .fattr = &fattr,
191 };
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000192 struct posix_acl *acl;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000193 int status, count;
194
195 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
196 return ERR_PTR(-EOPNOTSUPP);
197
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000198 status = nfs_revalidate_inode(server, inode);
199 if (status < 0)
200 return ERR_PTR(status);
201 acl = nfs3_get_cached_acl(inode, type);
202 if (acl != ERR_PTR(-EAGAIN))
203 return acl;
204 acl = NULL;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000205
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000206 /*
207 * Only get the access acl when explicitly requested: We don't
208 * need it for access decisions, and only some applications use
209 * it. Applications which request the access acl first are not
210 * penalized from this optimization.
211 */
212 if (type == ACL_TYPE_ACCESS)
213 args.mask |= NFS_ACLCNT|NFS_ACL;
214 if (S_ISDIR(inode->i_mode))
215 args.mask |= NFS_DFACLCNT|NFS_DFACL;
216 if (args.mask == 0)
217 return NULL;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000218
219 dprintk("NFS call getacl\n");
220 status = rpc_call(server->client_acl, ACLPROC3_GETACL,
221 &args, &res, 0);
222 dprintk("NFS reply getacl: %d\n", status);
223
224 /* pages may have been allocated at the xdr layer. */
225 for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
226 __free_page(args.pages[count]);
227
228 switch (status) {
229 case 0:
230 status = nfs_refresh_inode(inode, &fattr);
231 break;
232 case -EPFNOSUPPORT:
233 case -EPROTONOSUPPORT:
234 dprintk("NFS_V3_ACL extension not supported; disabling\n");
235 server->caps &= ~NFS_CAP_ACLS;
236 case -ENOTSUPP:
237 status = -EOPNOTSUPP;
238 default:
239 goto getout;
240 }
241 if ((args.mask & res.mask) != args.mask) {
242 status = -EIO;
243 goto getout;
244 }
245
246 if (res.acl_access != NULL) {
247 if (posix_acl_equiv_mode(res.acl_access, NULL) == 0) {
248 posix_acl_release(res.acl_access);
249 res.acl_access = NULL;
250 }
251 }
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +0000252 nfs3_cache_acls(inode, res.acl_access, res.acl_default);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000253
254 switch(type) {
255 case ACL_TYPE_ACCESS:
256 acl = res.acl_access;
257 res.acl_access = NULL;
258 break;
259
260 case ACL_TYPE_DEFAULT:
261 acl = res.acl_default;
262 res.acl_default = NULL;
263 }
264
265getout:
266 posix_acl_release(res.acl_access);
267 posix_acl_release(res.acl_default);
268
269 if (status != 0) {
270 posix_acl_release(acl);
271 acl = ERR_PTR(status);
272 }
273 return acl;
274}
275
276static int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
277 struct posix_acl *dfacl)
278{
279 struct nfs_server *server = NFS_SERVER(inode);
280 struct nfs_fattr fattr;
281 struct page *pages[NFSACL_MAXPAGES] = { };
282 struct nfs3_setaclargs args = {
283 .inode = inode,
284 .mask = NFS_ACL,
285 .acl_access = acl,
286 .pages = pages,
287 };
288 int status, count;
289
290 status = -EOPNOTSUPP;
291 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
292 goto out;
293
294 /* We are doing this here, because XDR marshalling can only
295 return -ENOMEM. */
296 status = -ENOSPC;
297 if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
298 goto out;
299 if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
300 goto out;
301 if (S_ISDIR(inode->i_mode)) {
302 args.mask |= NFS_DFACL;
303 args.acl_default = dfacl;
304 }
305
306 dprintk("NFS call setacl\n");
307 nfs_begin_data_update(inode);
308 status = rpc_call(server->client_acl, ACLPROC3_SETACL,
309 &args, &fattr, 0);
310 NFS_FLAGS(inode) |= NFS_INO_INVALID_ACCESS;
311 nfs_end_data_update(inode);
312 dprintk("NFS reply setacl: %d\n", status);
313
314 /* pages may have been allocated at the xdr layer. */
315 for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
316 __free_page(args.pages[count]);
317
318 switch (status) {
319 case 0:
320 status = nfs_refresh_inode(inode, &fattr);
321 break;
322 case -EPFNOSUPPORT:
323 case -EPROTONOSUPPORT:
324 dprintk("NFS_V3_ACL SETACL RPC not supported"
325 "(will not retry)\n");
326 server->caps &= ~NFS_CAP_ACLS;
327 case -ENOTSUPP:
328 status = -EOPNOTSUPP;
329 }
330out:
331 return status;
332}
333
334int nfs3_proc_setacl(struct inode *inode, int type, struct posix_acl *acl)
335{
336 struct posix_acl *alloc = NULL, *dfacl = NULL;
337 int status;
338
339 if (S_ISDIR(inode->i_mode)) {
340 switch(type) {
341 case ACL_TYPE_ACCESS:
342 alloc = dfacl = nfs3_proc_getacl(inode,
343 ACL_TYPE_DEFAULT);
344 if (IS_ERR(alloc))
345 goto fail;
346 break;
347
348 case ACL_TYPE_DEFAULT:
349 dfacl = acl;
350 alloc = acl = nfs3_proc_getacl(inode,
351 ACL_TYPE_ACCESS);
352 if (IS_ERR(alloc))
353 goto fail;
354 break;
355
356 default:
357 return -EINVAL;
358 }
359 } else if (type != ACL_TYPE_ACCESS)
360 return -EINVAL;
361
362 if (acl == NULL) {
363 alloc = acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
364 if (IS_ERR(alloc))
365 goto fail;
366 }
367 status = nfs3_proc_setacls(inode, acl, dfacl);
368 posix_acl_release(alloc);
369 return status;
370
371fail:
372 return PTR_ERR(alloc);
373}
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000374
375int nfs3_proc_set_default_acl(struct inode *dir, struct inode *inode,
376 mode_t mode)
377{
378 struct posix_acl *dfacl, *acl;
379 int error = 0;
380
381 dfacl = nfs3_proc_getacl(dir, ACL_TYPE_DEFAULT);
382 if (IS_ERR(dfacl)) {
383 error = PTR_ERR(dfacl);
384 return (error == -EOPNOTSUPP) ? 0 : error;
385 }
386 if (!dfacl)
387 return 0;
388 acl = posix_acl_clone(dfacl, GFP_KERNEL);
389 error = -ENOMEM;
390 if (!acl)
391 goto out_release_dfacl;
392 error = posix_acl_create_masq(acl, &mode);
393 if (error < 0)
394 goto out_release_acl;
395 error = nfs3_proc_setacls(inode, acl, S_ISDIR(inode->i_mode) ?
396 dfacl : NULL);
397out_release_acl:
398 posix_acl_release(acl);
399out_release_dfacl:
400 posix_acl_release(dfacl);
401 return error;
402}