4 #include <linux/nfs3.h>
5 #include <linux/nfs_fs.h>
6 #include <linux/posix_acl_xattr.h>
7 #include <linux/nfsacl.h>
12 #define NFSDBG_FACILITY NFSDBG_PROC
15 * nfs3_prepare_get_acl, nfs3_complete_get_acl, nfs3_abort_get_acl: Helpers for
16 * caching get_acl results in a race-free way. See fs/posix_acl.c:get_acl()
19 static void nfs3_prepare_get_acl(struct posix_acl **p)
21 struct posix_acl *sentinel = uncached_acl_sentinel(current);
23 if (cmpxchg(p, ACL_NOT_CACHED, sentinel) != ACL_NOT_CACHED) {
24 /* Not the first reader or sentinel already in place. */
28 static void nfs3_complete_get_acl(struct posix_acl **p, struct posix_acl *acl)
30 struct posix_acl *sentinel = uncached_acl_sentinel(current);
32 /* Only cache the ACL if our sentinel is still in place. */
34 if (cmpxchg(p, sentinel, acl) != sentinel)
35 posix_acl_release(acl);
38 static void nfs3_abort_get_acl(struct posix_acl **p)
40 struct posix_acl *sentinel = uncached_acl_sentinel(current);
42 /* Remove our sentinel upon failure. */
43 cmpxchg(p, sentinel, ACL_NOT_CACHED);
46 struct posix_acl *nfs3_get_acl(struct inode *inode, int type)
48 struct nfs_server *server = NFS_SERVER(inode);
49 struct page *pages[NFSACL_MAXPAGES] = { };
50 struct nfs3_getaclargs args = {
52 /* The xdr layer may allocate pages here. */
55 struct nfs3_getaclres res = {
58 struct rpc_message msg = {
64 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
65 return ERR_PTR(-EOPNOTSUPP);
67 status = nfs_revalidate_inode(server, inode);
69 return ERR_PTR(status);
72 * Only get the access acl when explicitly requested: We don't
73 * need it for access decisions, and only some applications use
74 * it. Applications which request the access acl first are not
75 * penalized from this optimization.
77 if (type == ACL_TYPE_ACCESS)
78 args.mask |= NFS_ACLCNT|NFS_ACL;
79 if (S_ISDIR(inode->i_mode))
80 args.mask |= NFS_DFACLCNT|NFS_DFACL;
84 dprintk("NFS call getacl\n");
85 msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_GETACL];
86 res.fattr = nfs_alloc_fattr();
87 if (res.fattr == NULL)
88 return ERR_PTR(-ENOMEM);
90 if (args.mask & NFS_ACL)
91 nfs3_prepare_get_acl(&inode->i_acl);
92 if (args.mask & NFS_DFACL)
93 nfs3_prepare_get_acl(&inode->i_default_acl);
95 status = rpc_call_sync(server->client_acl, &msg, 0);
96 dprintk("NFS reply getacl: %d\n", status);
98 /* pages may have been allocated at the xdr layer. */
99 for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
100 __free_page(args.pages[count]);
104 status = nfs_refresh_inode(inode, res.fattr);
107 case -EPROTONOSUPPORT:
108 dprintk("NFS_V3_ACL extension not supported; disabling\n");
109 server->caps &= ~NFS_CAP_ACLS;
111 status = -EOPNOTSUPP;
115 if ((args.mask & res.mask) != args.mask) {
120 if (res.acl_access != NULL) {
121 if ((posix_acl_equiv_mode(res.acl_access, NULL) == 0) ||
122 res.acl_access->a_count == 0) {
123 posix_acl_release(res.acl_access);
124 res.acl_access = NULL;
128 if (res.mask & NFS_ACL)
129 nfs3_complete_get_acl(&inode->i_acl, res.acl_access);
131 forget_cached_acl(inode, ACL_TYPE_ACCESS);
133 if (res.mask & NFS_DFACL)
134 nfs3_complete_get_acl(&inode->i_default_acl, res.acl_default);
136 forget_cached_acl(inode, ACL_TYPE_DEFAULT);
138 nfs_free_fattr(res.fattr);
139 if (type == ACL_TYPE_ACCESS) {
140 posix_acl_release(res.acl_default);
141 return res.acl_access;
143 posix_acl_release(res.acl_access);
144 return res.acl_default;
148 nfs3_abort_get_acl(&inode->i_acl);
149 nfs3_abort_get_acl(&inode->i_default_acl);
150 posix_acl_release(res.acl_access);
151 posix_acl_release(res.acl_default);
152 nfs_free_fattr(res.fattr);
153 return ERR_PTR(status);
156 static int __nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
157 struct posix_acl *dfacl)
159 struct nfs_server *server = NFS_SERVER(inode);
160 struct nfs_fattr *fattr;
161 struct page *pages[NFSACL_MAXPAGES];
162 struct nfs3_setaclargs args = {
168 struct rpc_message msg = {
174 if (acl == NULL && (!S_ISDIR(inode->i_mode) || dfacl == NULL))
177 status = -EOPNOTSUPP;
178 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
181 /* We are doing this here because XDR marshalling does not
182 * return any results, it BUGs. */
184 if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
186 if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
188 if (S_ISDIR(inode->i_mode)) {
189 args.mask |= NFS_DFACL;
190 args.acl_default = dfacl;
191 args.len = nfsacl_size(acl, dfacl);
193 args.len = nfsacl_size(acl, NULL);
195 if (args.len > NFS_ACL_INLINE_BUFSIZE) {
196 unsigned int npages = 1 + ((args.len - 1) >> PAGE_SHIFT);
200 args.pages[args.npages] = alloc_page(GFP_KERNEL);
201 if (args.pages[args.npages] == NULL)
204 } while (args.npages < npages);
207 dprintk("NFS call setacl\n");
209 fattr = nfs_alloc_fattr();
213 msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
214 msg.rpc_resp = fattr;
215 status = rpc_call_sync(server->client_acl, &msg, 0);
216 nfs_access_zap_cache(inode);
217 nfs_zap_acl_cache(inode);
218 dprintk("NFS reply setacl: %d\n", status);
222 status = nfs_refresh_inode(inode, fattr);
223 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
224 set_cached_acl(inode, ACL_TYPE_DEFAULT, dfacl);
227 case -EPROTONOSUPPORT:
228 dprintk("NFS_V3_ACL SETACL RPC not supported"
229 "(will not retry)\n");
230 server->caps &= ~NFS_CAP_ACLS;
232 status = -EOPNOTSUPP;
234 nfs_free_fattr(fattr);
236 while (args.npages != 0) {
238 __free_page(args.pages[args.npages]);
244 int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
245 struct posix_acl *dfacl)
248 ret = __nfs3_proc_setacls(inode, acl, dfacl);
249 return (ret == -EOPNOTSUPP) ? 0 : ret;
253 int nfs3_set_acl(struct inode *inode, struct posix_acl *acl, int type)
255 struct posix_acl *orig = acl, *dfacl = NULL, *alloc;
258 if (S_ISDIR(inode->i_mode)) {
260 case ACL_TYPE_ACCESS:
261 alloc = get_acl(inode, ACL_TYPE_DEFAULT);
267 case ACL_TYPE_DEFAULT:
268 alloc = get_acl(inode, ACL_TYPE_ACCESS);
278 alloc = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
283 status = __nfs3_proc_setacls(inode, acl, dfacl);
286 posix_acl_release(acl);
288 posix_acl_release(dfacl);
292 status = PTR_ERR(alloc);
296 const struct xattr_handler *nfs3_xattr_handlers[] = {
297 &posix_acl_access_xattr_handler,
298 &posix_acl_default_xattr_handler,
303 nfs3_list_one_acl(struct inode *inode, int type, const char *name, void *data,
304 size_t size, ssize_t *result)
306 struct posix_acl *acl;
307 char *p = data + *result;
309 acl = get_acl(inode, type);
310 if (IS_ERR_OR_NULL(acl))
313 posix_acl_release(acl);
315 *result += strlen(name);
327 nfs3_listxattr(struct dentry *dentry, char *data, size_t size)
329 struct inode *inode = d_inode(dentry);
333 error = nfs3_list_one_acl(inode, ACL_TYPE_ACCESS,
334 XATTR_NAME_POSIX_ACL_ACCESS, data, size, &result);
338 error = nfs3_list_one_acl(inode, ACL_TYPE_DEFAULT,
339 XATTR_NAME_POSIX_ACL_DEFAULT, data, size, &result);