[PATCH] Remove f_error field from struct file
[wrt350n-kernel.git] / fs / nfs / nfs3acl.c
blobee3536fc84a3bb49e7d99ab74b2ceffa162c6b89
1 #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>
8 #define NFSDBG_FACILITY NFSDBG_PROC
10 ssize_t nfs3_listxattr(struct dentry *dentry, char *buffer, size_t size)
12 struct inode *inode = dentry->d_inode;
13 struct posix_acl *acl;
14 int pos=0, len=0;
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)
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);
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);
42 # undef output
44 if (!buffer || len <= size)
45 return len;
46 return -ERANGE;
49 ssize_t nfs3_getxattr(struct dentry *dentry, const char *name,
50 void *buffer, size_t size)
52 struct inode *inode = dentry->d_inode;
53 struct posix_acl *acl;
54 int type, error = 0;
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;
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;
75 return error;
78 int nfs3_setxattr(struct dentry *dentry, const char *name,
79 const void *value, size_t size, int flags)
81 struct inode *inode = dentry->d_inode;
82 struct posix_acl *acl;
83 int type, error;
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;
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);
98 return error;
101 int nfs3_removexattr(struct dentry *dentry, const char *name)
103 struct inode *inode = dentry->d_inode;
104 int type;
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;
113 return nfs3_proc_setacl(inode, type, NULL);
116 static void __nfs3_forget_cached_acls(struct nfs_inode *nfsi)
118 if (!IS_ERR(nfsi->acl_access)) {
119 posix_acl_release(nfsi->acl_access);
120 nfsi->acl_access = ERR_PTR(-EAGAIN);
122 if (!IS_ERR(nfsi->acl_default)) {
123 posix_acl_release(nfsi->acl_default);
124 nfsi->acl_default = ERR_PTR(-EAGAIN);
128 void nfs3_forget_cached_acls(struct inode *inode)
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);
137 static struct posix_acl *nfs3_get_cached_acl(struct inode *inode, int type)
139 struct nfs_inode *nfsi = NFS_I(inode);
140 struct posix_acl *acl = ERR_PTR(-EINVAL);
142 spin_lock(&inode->i_lock);
143 switch(type) {
144 case ACL_TYPE_ACCESS:
145 acl = nfsi->acl_access;
146 break;
148 case ACL_TYPE_DEFAULT:
149 acl = nfsi->acl_default;
150 break;
152 default:
153 goto out;
155 if (IS_ERR(acl))
156 acl = ERR_PTR(-EAGAIN);
157 else
158 acl = posix_acl_dup(acl);
159 out:
160 spin_unlock(&inode->i_lock);
161 dprintk("NFS: nfs3_get_cached_acl(%s/%ld, %d) = %p\n", inode->i_sb->s_id,
162 inode->i_ino, type, acl);
163 return acl;
166 static void nfs3_cache_acls(struct inode *inode, struct posix_acl *acl,
167 struct posix_acl *dfacl)
169 struct nfs_inode *nfsi = NFS_I(inode);
171 dprintk("nfs3_cache_acls(%s/%ld, %p, %p)\n", inode->i_sb->s_id,
172 inode->i_ino, acl, dfacl);
173 spin_lock(&inode->i_lock);
174 __nfs3_forget_cached_acls(NFS_I(inode));
175 nfsi->acl_access = posix_acl_dup(acl);
176 nfsi->acl_default = posix_acl_dup(dfacl);
177 spin_unlock(&inode->i_lock);
180 struct posix_acl *nfs3_proc_getacl(struct inode *inode, int type)
182 struct nfs_server *server = NFS_SERVER(inode);
183 struct nfs_fattr fattr;
184 struct page *pages[NFSACL_MAXPAGES] = { };
185 struct nfs3_getaclargs args = {
186 .fh = NFS_FH(inode),
187 /* The xdr layer may allocate pages here. */
188 .pages = pages,
190 struct nfs3_getaclres res = {
191 .fattr = &fattr,
193 struct posix_acl *acl;
194 int status, count;
196 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
197 return ERR_PTR(-EOPNOTSUPP);
199 status = nfs_revalidate_inode(server, inode);
200 if (status < 0)
201 return ERR_PTR(status);
202 acl = nfs3_get_cached_acl(inode, type);
203 if (acl != ERR_PTR(-EAGAIN))
204 return acl;
205 acl = NULL;
208 * Only get the access acl when explicitly requested: We don't
209 * need it for access decisions, and only some applications use
210 * it. Applications which request the access acl first are not
211 * penalized from this optimization.
213 if (type == ACL_TYPE_ACCESS)
214 args.mask |= NFS_ACLCNT|NFS_ACL;
215 if (S_ISDIR(inode->i_mode))
216 args.mask |= NFS_DFACLCNT|NFS_DFACL;
217 if (args.mask == 0)
218 return NULL;
220 dprintk("NFS call getacl\n");
221 status = rpc_call(server->client_acl, ACLPROC3_GETACL,
222 &args, &res, 0);
223 dprintk("NFS reply getacl: %d\n", status);
225 /* pages may have been allocated at the xdr layer. */
226 for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
227 __free_page(args.pages[count]);
229 switch (status) {
230 case 0:
231 status = nfs_refresh_inode(inode, &fattr);
232 break;
233 case -EPFNOSUPPORT:
234 case -EPROTONOSUPPORT:
235 dprintk("NFS_V3_ACL extension not supported; disabling\n");
236 server->caps &= ~NFS_CAP_ACLS;
237 case -ENOTSUPP:
238 status = -EOPNOTSUPP;
239 default:
240 goto getout;
242 if ((args.mask & res.mask) != args.mask) {
243 status = -EIO;
244 goto getout;
247 if (res.acl_access != NULL) {
248 if (posix_acl_equiv_mode(res.acl_access, NULL) == 0) {
249 posix_acl_release(res.acl_access);
250 res.acl_access = NULL;
253 nfs3_cache_acls(inode, res.acl_access, res.acl_default);
255 switch(type) {
256 case ACL_TYPE_ACCESS:
257 acl = res.acl_access;
258 res.acl_access = NULL;
259 break;
261 case ACL_TYPE_DEFAULT:
262 acl = res.acl_default;
263 res.acl_default = NULL;
266 getout:
267 posix_acl_release(res.acl_access);
268 posix_acl_release(res.acl_default);
270 if (status != 0) {
271 posix_acl_release(acl);
272 acl = ERR_PTR(status);
274 return acl;
277 static int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
278 struct posix_acl *dfacl)
280 struct nfs_server *server = NFS_SERVER(inode);
281 struct nfs_fattr fattr;
282 struct page *pages[NFSACL_MAXPAGES] = { };
283 struct nfs3_setaclargs args = {
284 .inode = inode,
285 .mask = NFS_ACL,
286 .acl_access = acl,
287 .pages = pages,
289 int status, count;
291 status = -EOPNOTSUPP;
292 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
293 goto out;
295 /* We are doing this here, because XDR marshalling can only
296 return -ENOMEM. */
297 status = -ENOSPC;
298 if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
299 goto out;
300 if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
301 goto out;
302 if (S_ISDIR(inode->i_mode)) {
303 args.mask |= NFS_DFACL;
304 args.acl_default = dfacl;
307 dprintk("NFS call setacl\n");
308 nfs_begin_data_update(inode);
309 status = rpc_call(server->client_acl, ACLPROC3_SETACL,
310 &args, &fattr, 0);
311 NFS_FLAGS(inode) |= NFS_INO_INVALID_ACCESS;
312 nfs_end_data_update(inode);
313 dprintk("NFS reply setacl: %d\n", status);
315 /* pages may have been allocated at the xdr layer. */
316 for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
317 __free_page(args.pages[count]);
319 switch (status) {
320 case 0:
321 status = nfs_refresh_inode(inode, &fattr);
322 break;
323 case -EPFNOSUPPORT:
324 case -EPROTONOSUPPORT:
325 dprintk("NFS_V3_ACL SETACL RPC not supported"
326 "(will not retry)\n");
327 server->caps &= ~NFS_CAP_ACLS;
328 case -ENOTSUPP:
329 status = -EOPNOTSUPP;
331 out:
332 return status;
335 int nfs3_proc_setacl(struct inode *inode, int type, struct posix_acl *acl)
337 struct posix_acl *alloc = NULL, *dfacl = NULL;
338 int status;
340 if (S_ISDIR(inode->i_mode)) {
341 switch(type) {
342 case ACL_TYPE_ACCESS:
343 alloc = dfacl = nfs3_proc_getacl(inode,
344 ACL_TYPE_DEFAULT);
345 if (IS_ERR(alloc))
346 goto fail;
347 break;
349 case ACL_TYPE_DEFAULT:
350 dfacl = acl;
351 alloc = acl = nfs3_proc_getacl(inode,
352 ACL_TYPE_ACCESS);
353 if (IS_ERR(alloc))
354 goto fail;
355 break;
357 default:
358 return -EINVAL;
360 } else if (type != ACL_TYPE_ACCESS)
361 return -EINVAL;
363 if (acl == NULL) {
364 alloc = acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
365 if (IS_ERR(alloc))
366 goto fail;
368 status = nfs3_proc_setacls(inode, acl, dfacl);
369 posix_acl_release(alloc);
370 return status;
372 fail:
373 return PTR_ERR(alloc);
376 int nfs3_proc_set_default_acl(struct inode *dir, struct inode *inode,
377 mode_t mode)
379 struct posix_acl *dfacl, *acl;
380 int error = 0;
382 dfacl = nfs3_proc_getacl(dir, ACL_TYPE_DEFAULT);
383 if (IS_ERR(dfacl)) {
384 error = PTR_ERR(dfacl);
385 return (error == -EOPNOTSUPP) ? 0 : error;
387 if (!dfacl)
388 return 0;
389 acl = posix_acl_clone(dfacl, GFP_KERNEL);
390 error = -ENOMEM;
391 if (!acl)
392 goto out_release_dfacl;
393 error = posix_acl_create_masq(acl, &mode);
394 if (error < 0)
395 goto out_release_acl;
396 error = nfs3_proc_setacls(inode, acl, S_ISDIR(inode->i_mode) ?
397 dfacl : NULL);
398 out_release_acl:
399 posix_acl_release(acl);
400 out_release_dfacl:
401 posix_acl_release(dfacl);
402 return error;