3 #include <linux/nfs3.h>
4 #include <linux/nfs_fs.h>
5 #include <linux/posix_acl_xattr.h>
6 #include <linux/nfsacl.h>
10 #define NFSDBG_FACILITY NFSDBG_PROC
12 ssize_t nfs3_listxattr(struct dentry *dentry, char *buffer, size_t size)
14 struct inode *inode = dentry->d_inode;
15 struct posix_acl *acl;
18 # define output(s) do { \
19 if (pos + sizeof(s) <= size) { \
20 memcpy(buffer + pos, s, sizeof(s)); \
26 acl = nfs3_proc_getacl(inode, ACL_TYPE_ACCESS);
30 output("system.posix_acl_access");
31 posix_acl_release(acl);
34 if (S_ISDIR(inode->i_mode)) {
35 acl = nfs3_proc_getacl(inode, ACL_TYPE_DEFAULT);
39 output("system.posix_acl_default");
40 posix_acl_release(acl);
46 if (!buffer || len <= size)
51 ssize_t nfs3_getxattr(struct dentry *dentry, const char *name,
52 void *buffer, size_t size)
54 struct inode *inode = dentry->d_inode;
55 struct posix_acl *acl;
58 if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0)
59 type = ACL_TYPE_ACCESS;
60 else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0)
61 type = ACL_TYPE_DEFAULT;
65 acl = nfs3_proc_getacl(inode, type);
69 if (type == ACL_TYPE_ACCESS && acl->a_count == 0)
72 error = posix_acl_to_xattr(acl, buffer, size);
73 posix_acl_release(acl);
80 int nfs3_setxattr(struct dentry *dentry, const char *name,
81 const void *value, size_t size, int flags)
83 struct inode *inode = dentry->d_inode;
84 struct posix_acl *acl;
87 if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0)
88 type = ACL_TYPE_ACCESS;
89 else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0)
90 type = ACL_TYPE_DEFAULT;
94 acl = posix_acl_from_xattr(value, size);
97 error = nfs3_proc_setacl(inode, type, acl);
98 posix_acl_release(acl);
103 int nfs3_removexattr(struct dentry *dentry, const char *name)
105 struct inode *inode = dentry->d_inode;
108 if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0)
109 type = ACL_TYPE_ACCESS;
110 else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0)
111 type = ACL_TYPE_DEFAULT;
115 return nfs3_proc_setacl(inode, type, NULL);
118 static void __nfs3_forget_cached_acls(struct nfs_inode *nfsi)
120 if (!IS_ERR(nfsi->acl_access)) {
121 posix_acl_release(nfsi->acl_access);
122 nfsi->acl_access = ERR_PTR(-EAGAIN);
124 if (!IS_ERR(nfsi->acl_default)) {
125 posix_acl_release(nfsi->acl_default);
126 nfsi->acl_default = ERR_PTR(-EAGAIN);
130 void nfs3_forget_cached_acls(struct inode *inode)
132 dprintk("NFS: nfs3_forget_cached_acls(%s/%ld)\n", inode->i_sb->s_id,
134 spin_lock(&inode->i_lock);
135 __nfs3_forget_cached_acls(NFS_I(inode));
136 spin_unlock(&inode->i_lock);
139 static struct posix_acl *nfs3_get_cached_acl(struct inode *inode, int type)
141 struct nfs_inode *nfsi = NFS_I(inode);
142 struct posix_acl *acl = ERR_PTR(-EINVAL);
144 spin_lock(&inode->i_lock);
146 case ACL_TYPE_ACCESS:
147 acl = nfsi->acl_access;
150 case ACL_TYPE_DEFAULT:
151 acl = nfsi->acl_default;
158 acl = ERR_PTR(-EAGAIN);
160 acl = posix_acl_dup(acl);
162 spin_unlock(&inode->i_lock);
163 dprintk("NFS: nfs3_get_cached_acl(%s/%ld, %d) = %p\n", inode->i_sb->s_id,
164 inode->i_ino, type, acl);
168 static void nfs3_cache_acls(struct inode *inode, struct posix_acl *acl,
169 struct posix_acl *dfacl)
171 struct nfs_inode *nfsi = NFS_I(inode);
173 dprintk("nfs3_cache_acls(%s/%ld, %p, %p)\n", inode->i_sb->s_id,
174 inode->i_ino, acl, dfacl);
175 spin_lock(&inode->i_lock);
176 __nfs3_forget_cached_acls(NFS_I(inode));
178 nfsi->acl_access = posix_acl_dup(acl);
180 nfsi->acl_default = posix_acl_dup(dfacl);
181 spin_unlock(&inode->i_lock);
184 struct posix_acl *nfs3_proc_getacl(struct inode *inode, int type)
186 struct nfs_server *server = NFS_SERVER(inode);
187 struct nfs_fattr fattr;
188 struct page *pages[NFSACL_MAXPAGES] = { };
189 struct nfs3_getaclargs args = {
191 /* The xdr layer may allocate pages here. */
194 struct nfs3_getaclres res = {
197 struct rpc_message msg = {
201 struct posix_acl *acl;
204 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
205 return ERR_PTR(-EOPNOTSUPP);
207 status = nfs_revalidate_inode(server, inode);
209 return ERR_PTR(status);
210 acl = nfs3_get_cached_acl(inode, type);
211 if (acl != ERR_PTR(-EAGAIN))
216 * Only get the access acl when explicitly requested: We don't
217 * need it for access decisions, and only some applications use
218 * it. Applications which request the access acl first are not
219 * penalized from this optimization.
221 if (type == ACL_TYPE_ACCESS)
222 args.mask |= NFS_ACLCNT|NFS_ACL;
223 if (S_ISDIR(inode->i_mode))
224 args.mask |= NFS_DFACLCNT|NFS_DFACL;
228 dprintk("NFS call getacl\n");
229 msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_GETACL];
230 nfs_fattr_init(&fattr);
231 status = rpc_call_sync(server->client_acl, &msg, 0);
232 dprintk("NFS reply getacl: %d\n", status);
234 /* pages may have been allocated at the xdr layer. */
235 for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
236 __free_page(args.pages[count]);
240 status = nfs_refresh_inode(inode, &fattr);
243 case -EPROTONOSUPPORT:
244 dprintk("NFS_V3_ACL extension not supported; disabling\n");
245 server->caps &= ~NFS_CAP_ACLS;
247 status = -EOPNOTSUPP;
251 if ((args.mask & res.mask) != args.mask) {
256 if (res.acl_access != NULL) {
257 if (posix_acl_equiv_mode(res.acl_access, NULL) == 0) {
258 posix_acl_release(res.acl_access);
259 res.acl_access = NULL;
262 nfs3_cache_acls(inode,
263 (res.mask & NFS_ACL) ? res.acl_access : ERR_PTR(-EINVAL),
264 (res.mask & NFS_DFACL) ? res.acl_default : ERR_PTR(-EINVAL));
267 case ACL_TYPE_ACCESS:
268 acl = res.acl_access;
269 res.acl_access = NULL;
272 case ACL_TYPE_DEFAULT:
273 acl = res.acl_default;
274 res.acl_default = NULL;
278 posix_acl_release(res.acl_access);
279 posix_acl_release(res.acl_default);
282 posix_acl_release(acl);
283 acl = ERR_PTR(status);
288 static int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
289 struct posix_acl *dfacl)
291 struct nfs_server *server = NFS_SERVER(inode);
292 struct nfs_fattr fattr;
293 struct page *pages[NFSACL_MAXPAGES];
294 struct nfs3_setaclargs args = {
300 struct rpc_message msg = {
306 status = -EOPNOTSUPP;
307 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
310 /* We are doing this here, because XDR marshalling can only
313 if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
315 if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
317 if (S_ISDIR(inode->i_mode)) {
318 args.mask |= NFS_DFACL;
319 args.acl_default = dfacl;
320 args.len = nfsacl_size(acl, dfacl);
322 args.len = nfsacl_size(acl, NULL);
324 if (args.len > NFS_ACL_INLINE_BUFSIZE) {
325 unsigned int npages = 1 + ((args.len - 1) >> PAGE_SHIFT);
329 args.pages[args.npages] = alloc_page(GFP_KERNEL);
330 if (args.pages[args.npages] == NULL)
333 } while (args.npages < npages);
336 dprintk("NFS call setacl\n");
337 msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
338 nfs_fattr_init(&fattr);
339 status = rpc_call_sync(server->client_acl, &msg, 0);
340 nfs_access_zap_cache(inode);
341 nfs_zap_acl_cache(inode);
342 dprintk("NFS reply setacl: %d\n", status);
346 status = nfs_refresh_inode(inode, &fattr);
347 nfs3_cache_acls(inode, acl, dfacl);
350 case -EPROTONOSUPPORT:
351 dprintk("NFS_V3_ACL SETACL RPC not supported"
352 "(will not retry)\n");
353 server->caps &= ~NFS_CAP_ACLS;
355 status = -EOPNOTSUPP;
358 while (args.npages != 0) {
360 __free_page(args.pages[args.npages]);
366 int nfs3_proc_setacl(struct inode *inode, int type, struct posix_acl *acl)
368 struct posix_acl *alloc = NULL, *dfacl = NULL;
371 if (S_ISDIR(inode->i_mode)) {
373 case ACL_TYPE_ACCESS:
374 alloc = dfacl = nfs3_proc_getacl(inode,
380 case ACL_TYPE_DEFAULT:
382 alloc = acl = nfs3_proc_getacl(inode,
391 } else if (type != ACL_TYPE_ACCESS)
395 alloc = acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
399 status = nfs3_proc_setacls(inode, acl, dfacl);
400 posix_acl_release(alloc);
404 return PTR_ERR(alloc);
407 int nfs3_proc_set_default_acl(struct inode *dir, struct inode *inode,
410 struct posix_acl *dfacl, *acl;
413 dfacl = nfs3_proc_getacl(dir, ACL_TYPE_DEFAULT);
415 error = PTR_ERR(dfacl);
416 return (error == -EOPNOTSUPP) ? 0 : error;
420 acl = posix_acl_clone(dfacl, GFP_KERNEL);
423 goto out_release_dfacl;
424 error = posix_acl_create_masq(acl, &mode);
426 goto out_release_acl;
427 error = nfs3_proc_setacls(inode, acl, S_ISDIR(inode->i_mode) ?
430 posix_acl_release(acl);
432 posix_acl_release(dfacl);