3 #include <linux/nfs3.h>
4 #include <linux/nfs_fs.h>
5 #include <linux/posix_acl_xattr.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
;
16 # define output(s) do { \
17 if (pos + sizeof(s) <= size) { \
18 memcpy(buffer + pos, s, sizeof(s)); \
24 acl
= nfs3_proc_getacl(inode
, ACL_TYPE_ACCESS
);
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
);
37 output("system.posix_acl_default");
38 posix_acl_release(acl
);
44 if (!buffer
|| len
<= size
)
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
;
56 if (strcmp(name
, POSIX_ACL_XATTR_ACCESS
) == 0)
57 type
= ACL_TYPE_ACCESS
;
58 else if (strcmp(name
, POSIX_ACL_XATTR_DEFAULT
) == 0)
59 type
= ACL_TYPE_DEFAULT
;
63 acl
= nfs3_proc_getacl(inode
, type
);
67 if (type
== ACL_TYPE_ACCESS
&& acl
->a_count
== 0)
70 error
= posix_acl_to_xattr(acl
, buffer
, size
);
71 posix_acl_release(acl
);
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
;
85 if (strcmp(name
, POSIX_ACL_XATTR_ACCESS
) == 0)
86 type
= ACL_TYPE_ACCESS
;
87 else if (strcmp(name
, POSIX_ACL_XATTR_DEFAULT
) == 0)
88 type
= ACL_TYPE_DEFAULT
;
92 acl
= posix_acl_from_xattr(value
, size
);
95 error
= nfs3_proc_setacl(inode
, type
, acl
);
96 posix_acl_release(acl
);
101 int nfs3_removexattr(struct dentry
*dentry
, const char *name
)
103 struct inode
*inode
= dentry
->d_inode
;
106 if (strcmp(name
, POSIX_ACL_XATTR_ACCESS
) == 0)
107 type
= ACL_TYPE_ACCESS
;
108 else if (strcmp(name
, POSIX_ACL_XATTR_DEFAULT
) == 0)
109 type
= ACL_TYPE_DEFAULT
;
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
,
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
);
144 case ACL_TYPE_ACCESS
:
145 acl
= nfsi
->acl_access
;
148 case ACL_TYPE_DEFAULT
:
149 acl
= nfsi
->acl_default
;
156 acl
= ERR_PTR(-EAGAIN
);
158 acl
= posix_acl_dup(acl
);
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
);
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
= {
187 /* The xdr layer may allocate pages here. */
190 struct nfs3_getaclres res
= {
193 struct rpc_message msg
= {
197 struct posix_acl
*acl
;
200 if (!nfs_server_capable(inode
, NFS_CAP_ACLS
))
201 return ERR_PTR(-EOPNOTSUPP
);
203 status
= nfs_revalidate_inode(server
, inode
);
205 return ERR_PTR(status
);
206 acl
= nfs3_get_cached_acl(inode
, type
);
207 if (acl
!= ERR_PTR(-EAGAIN
))
212 * Only get the access acl when explicitly requested: We don't
213 * need it for access decisions, and only some applications use
214 * it. Applications which request the access acl first are not
215 * penalized from this optimization.
217 if (type
== ACL_TYPE_ACCESS
)
218 args
.mask
|= NFS_ACLCNT
|NFS_ACL
;
219 if (S_ISDIR(inode
->i_mode
))
220 args
.mask
|= NFS_DFACLCNT
|NFS_DFACL
;
224 dprintk("NFS call getacl\n");
225 msg
.rpc_proc
= &server
->client_acl
->cl_procinfo
[ACLPROC3_GETACL
];
226 status
= rpc_call_sync(server
->client_acl
, &msg
, 0);
227 dprintk("NFS reply getacl: %d\n", status
);
229 /* pages may have been allocated at the xdr layer. */
230 for (count
= 0; count
< NFSACL_MAXPAGES
&& args
.pages
[count
]; count
++)
231 __free_page(args
.pages
[count
]);
235 status
= nfs_refresh_inode(inode
, &fattr
);
238 case -EPROTONOSUPPORT
:
239 dprintk("NFS_V3_ACL extension not supported; disabling\n");
240 server
->caps
&= ~NFS_CAP_ACLS
;
242 status
= -EOPNOTSUPP
;
246 if ((args
.mask
& res
.mask
) != args
.mask
) {
251 if (res
.acl_access
!= NULL
) {
252 if (posix_acl_equiv_mode(res
.acl_access
, NULL
) == 0) {
253 posix_acl_release(res
.acl_access
);
254 res
.acl_access
= NULL
;
257 nfs3_cache_acls(inode
, res
.acl_access
, res
.acl_default
);
260 case ACL_TYPE_ACCESS
:
261 acl
= res
.acl_access
;
262 res
.acl_access
= NULL
;
265 case ACL_TYPE_DEFAULT
:
266 acl
= res
.acl_default
;
267 res
.acl_default
= NULL
;
271 posix_acl_release(res
.acl_access
);
272 posix_acl_release(res
.acl_default
);
275 posix_acl_release(acl
);
276 acl
= ERR_PTR(status
);
281 static int nfs3_proc_setacls(struct inode
*inode
, struct posix_acl
*acl
,
282 struct posix_acl
*dfacl
)
284 struct nfs_server
*server
= NFS_SERVER(inode
);
285 struct nfs_fattr fattr
;
286 struct page
*pages
[NFSACL_MAXPAGES
] = { };
287 struct nfs3_setaclargs args
= {
293 struct rpc_message msg
= {
299 status
= -EOPNOTSUPP
;
300 if (!nfs_server_capable(inode
, NFS_CAP_ACLS
))
303 /* We are doing this here, because XDR marshalling can only
306 if (acl
!= NULL
&& acl
->a_count
> NFS_ACL_MAX_ENTRIES
)
308 if (dfacl
!= NULL
&& dfacl
->a_count
> NFS_ACL_MAX_ENTRIES
)
310 if (S_ISDIR(inode
->i_mode
)) {
311 args
.mask
|= NFS_DFACL
;
312 args
.acl_default
= dfacl
;
315 dprintk("NFS call setacl\n");
316 nfs_begin_data_update(inode
);
317 msg
.rpc_proc
= &server
->client_acl
->cl_procinfo
[ACLPROC3_SETACL
];
318 status
= rpc_call_sync(server
->client_acl
, &msg
, 0);
319 spin_lock(&inode
->i_lock
);
320 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ACCESS
;
321 spin_unlock(&inode
->i_lock
);
322 nfs_end_data_update(inode
);
323 dprintk("NFS reply setacl: %d\n", status
);
325 /* pages may have been allocated at the xdr layer. */
326 for (count
= 0; count
< NFSACL_MAXPAGES
&& args
.pages
[count
]; count
++)
327 __free_page(args
.pages
[count
]);
331 status
= nfs_refresh_inode(inode
, &fattr
);
334 case -EPROTONOSUPPORT
:
335 dprintk("NFS_V3_ACL SETACL RPC not supported"
336 "(will not retry)\n");
337 server
->caps
&= ~NFS_CAP_ACLS
;
339 status
= -EOPNOTSUPP
;
345 int nfs3_proc_setacl(struct inode
*inode
, int type
, struct posix_acl
*acl
)
347 struct posix_acl
*alloc
= NULL
, *dfacl
= NULL
;
350 if (S_ISDIR(inode
->i_mode
)) {
352 case ACL_TYPE_ACCESS
:
353 alloc
= dfacl
= nfs3_proc_getacl(inode
,
359 case ACL_TYPE_DEFAULT
:
361 alloc
= acl
= nfs3_proc_getacl(inode
,
370 } else if (type
!= ACL_TYPE_ACCESS
)
374 alloc
= acl
= posix_acl_from_mode(inode
->i_mode
, GFP_KERNEL
);
378 status
= nfs3_proc_setacls(inode
, acl
, dfacl
);
379 posix_acl_release(alloc
);
383 return PTR_ERR(alloc
);
386 int nfs3_proc_set_default_acl(struct inode
*dir
, struct inode
*inode
,
389 struct posix_acl
*dfacl
, *acl
;
392 dfacl
= nfs3_proc_getacl(dir
, ACL_TYPE_DEFAULT
);
394 error
= PTR_ERR(dfacl
);
395 return (error
== -EOPNOTSUPP
) ? 0 : error
;
399 acl
= posix_acl_clone(dfacl
, GFP_KERNEL
);
402 goto out_release_dfacl
;
403 error
= posix_acl_create_masq(acl
, &mode
);
405 goto out_release_acl
;
406 error
= nfs3_proc_setacls(inode
, acl
, S_ISDIR(inode
->i_mode
) ?
409 posix_acl_release(acl
);
411 posix_acl_release(dfacl
);