1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/nfs3.h>
6 #include <linux/nfs_fs.h>
7 #include <linux/posix_acl_xattr.h>
8 #include <linux/nfsacl.h>
13 #define NFSDBG_FACILITY NFSDBG_PROC
16 * nfs3_prepare_get_acl, nfs3_complete_get_acl, nfs3_abort_get_acl: Helpers for
17 * caching get_acl results in a race-free way. See fs/posix_acl.c:get_acl()
20 static void nfs3_prepare_get_acl(struct posix_acl **p)
22 struct posix_acl *sentinel = uncached_acl_sentinel(current);
24 /* If the ACL isn't being read yet, set our sentinel. */
25 cmpxchg(p, ACL_NOT_CACHED, sentinel);
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, bool rcu)
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 = {
65 return ERR_PTR(-ECHILD);
67 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
68 return ERR_PTR(-EOPNOTSUPP);
70 status = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
72 return ERR_PTR(status);
75 * Only get the access acl when explicitly requested: We don't
76 * need it for access decisions, and only some applications use
77 * it. Applications which request the access acl first are not
78 * penalized from this optimization.
80 if (type == ACL_TYPE_ACCESS)
81 args.mask |= NFS_ACLCNT|NFS_ACL;
82 if (S_ISDIR(inode->i_mode))
83 args.mask |= NFS_DFACLCNT|NFS_DFACL;
87 dprintk("NFS call getacl\n");
88 msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_GETACL];
89 res.fattr = nfs_alloc_fattr();
90 if (res.fattr == NULL)
91 return ERR_PTR(-ENOMEM);
93 if (args.mask & NFS_ACL)
94 nfs3_prepare_get_acl(&inode->i_acl);
95 if (args.mask & NFS_DFACL)
96 nfs3_prepare_get_acl(&inode->i_default_acl);
98 status = rpc_call_sync(server->client_acl, &msg, 0);
99 dprintk("NFS reply getacl: %d\n", status);
101 /* pages may have been allocated at the xdr layer. */
102 for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
103 __free_page(args.pages[count]);
107 status = nfs_refresh_inode(inode, res.fattr);
110 case -EPROTONOSUPPORT:
111 dprintk("NFS_V3_ACL extension not supported; disabling\n");
112 server->caps &= ~NFS_CAP_ACLS;
115 status = -EOPNOTSUPP;
120 if ((args.mask & res.mask) != args.mask) {
125 if (res.acl_access != NULL) {
126 if ((posix_acl_equiv_mode(res.acl_access, NULL) == 0) ||
127 res.acl_access->a_count == 0) {
128 posix_acl_release(res.acl_access);
129 res.acl_access = NULL;
133 if (res.mask & NFS_ACL)
134 nfs3_complete_get_acl(&inode->i_acl, res.acl_access);
136 forget_cached_acl(inode, ACL_TYPE_ACCESS);
138 if (res.mask & NFS_DFACL)
139 nfs3_complete_get_acl(&inode->i_default_acl, res.acl_default);
141 forget_cached_acl(inode, ACL_TYPE_DEFAULT);
143 nfs_free_fattr(res.fattr);
144 if (type == ACL_TYPE_ACCESS) {
145 posix_acl_release(res.acl_default);
146 return res.acl_access;
148 posix_acl_release(res.acl_access);
149 return res.acl_default;
153 nfs3_abort_get_acl(&inode->i_acl);
154 nfs3_abort_get_acl(&inode->i_default_acl);
155 posix_acl_release(res.acl_access);
156 posix_acl_release(res.acl_default);
157 nfs_free_fattr(res.fattr);
158 return ERR_PTR(status);
161 static int __nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
162 struct posix_acl *dfacl)
164 struct nfs_server *server = NFS_SERVER(inode);
165 struct nfs_fattr *fattr;
166 struct page *pages[NFSACL_MAXPAGES];
167 struct nfs3_setaclargs args = {
173 struct rpc_message msg = {
179 if (acl == NULL && (!S_ISDIR(inode->i_mode) || dfacl == NULL))
182 status = -EOPNOTSUPP;
183 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
186 /* We are doing this here because XDR marshalling does not
187 * return any results, it BUGs. */
189 if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
191 if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
193 if (S_ISDIR(inode->i_mode)) {
194 args.mask |= NFS_DFACL;
195 args.acl_default = dfacl;
196 args.len = nfsacl_size(acl, dfacl);
198 args.len = nfsacl_size(acl, NULL);
200 if (args.len > NFS_ACL_INLINE_BUFSIZE) {
201 unsigned int npages = 1 + ((args.len - 1) >> PAGE_SHIFT);
205 args.pages[args.npages] = alloc_page(GFP_KERNEL);
206 if (args.pages[args.npages] == NULL)
209 } while (args.npages < npages);
212 dprintk("NFS call setacl\n");
214 fattr = nfs_alloc_fattr();
218 msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
219 msg.rpc_resp = fattr;
220 status = rpc_call_sync(server->client_acl, &msg, 0);
221 nfs_access_zap_cache(inode);
222 nfs_zap_acl_cache(inode);
223 dprintk("NFS reply setacl: %d\n", status);
227 status = nfs_refresh_inode(inode, fattr);
230 case -EPROTONOSUPPORT:
231 dprintk("NFS_V3_ACL SETACL RPC not supported"
232 "(will not retry)\n");
233 server->caps &= ~NFS_CAP_ACLS;
236 status = -EOPNOTSUPP;
238 nfs_free_fattr(fattr);
240 while (args.npages != 0) {
242 __free_page(args.pages[args.npages]);
248 int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
249 struct posix_acl *dfacl)
252 ret = __nfs3_proc_setacls(inode, acl, dfacl);
253 return (ret == -EOPNOTSUPP) ? 0 : ret;
257 int nfs3_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
258 struct posix_acl *acl, int type)
260 struct posix_acl *orig = acl, *dfacl = NULL, *alloc;
261 struct inode *inode = d_inode(dentry);
264 if (S_ISDIR(inode->i_mode)) {
266 case ACL_TYPE_ACCESS:
267 alloc = get_inode_acl(inode, ACL_TYPE_DEFAULT);
273 case ACL_TYPE_DEFAULT:
274 alloc = get_inode_acl(inode, ACL_TYPE_ACCESS);
284 alloc = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
289 status = __nfs3_proc_setacls(inode, acl, dfacl);
292 posix_acl_release(acl);
294 posix_acl_release(dfacl);
298 status = PTR_ERR(alloc);
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_inode_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);