Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 1 | /* |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 2 | * Process version 3 NFSACL requests. |
| 3 | * |
| 4 | * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de> |
| 5 | */ |
| 6 | |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 7 | #include "nfsd.h" |
| 8 | /* FIXME: nfsacl.h is a broken header */ |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 9 | #include <linux/nfsacl.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/gfp.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 11 | #include "cache.h" |
| 12 | #include "xdr3.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 13 | #include "vfs.h" |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 14 | |
| 15 | #define RETURN_STATUS(st) { resp->status = (st); return (st); } |
| 16 | |
| 17 | /* |
| 18 | * NULL call. |
| 19 | */ |
Al Viro | 7111c66 | 2006-10-19 23:28:45 -0700 | [diff] [blame] | 20 | static __be32 |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 21 | nfsd3_proc_null(struct svc_rqst *rqstp, void *argp, void *resp) |
| 22 | { |
| 23 | return nfs_ok; |
| 24 | } |
| 25 | |
| 26 | /* |
| 27 | * Get the Access and/or Default ACL of a file. |
| 28 | */ |
Al Viro | 7111c66 | 2006-10-19 23:28:45 -0700 | [diff] [blame] | 29 | static __be32 nfsd3_proc_getacl(struct svc_rqst * rqstp, |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 30 | struct nfsd3_getaclargs *argp, struct nfsd3_getaclres *resp) |
| 31 | { |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 32 | struct posix_acl *acl; |
Christoph Hellwig | 4ac7249 | 2013-12-20 05:16:55 -0800 | [diff] [blame] | 33 | struct inode *inode; |
| 34 | svc_fh *fh; |
Al Viro | c4d987b | 2006-10-19 23:29:00 -0700 | [diff] [blame] | 35 | __be32 nfserr = 0; |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 36 | |
| 37 | fh = fh_copy(&resp->fh, &argp->fh); |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 38 | nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP); |
| 39 | if (nfserr) |
J. Bruce Fields | ac8587d | 2007-11-12 16:05:02 -0500 | [diff] [blame] | 40 | RETURN_STATUS(nfserr); |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 41 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 42 | inode = d_inode(fh->fh_dentry); |
Christoph Hellwig | 4ac7249 | 2013-12-20 05:16:55 -0800 | [diff] [blame] | 43 | |
Kinglong Mee | 7b8f458 | 2015-07-03 19:39:02 +0800 | [diff] [blame] | 44 | if (argp->mask & ~NFS_ACL_MASK) |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 45 | RETURN_STATUS(nfserr_inval); |
| 46 | resp->mask = argp->mask; |
| 47 | |
| 48 | if (resp->mask & (NFS_ACL|NFS_ACLCNT)) { |
Christoph Hellwig | 4ac7249 | 2013-12-20 05:16:55 -0800 | [diff] [blame] | 49 | acl = get_acl(inode, ACL_TYPE_ACCESS); |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 50 | if (acl == NULL) { |
| 51 | /* Solaris returns the inode's minimum ACL. */ |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 52 | acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL); |
| 53 | } |
Kinglong Mee | 35e634b | 2014-07-09 21:54:16 +0800 | [diff] [blame] | 54 | if (IS_ERR(acl)) { |
| 55 | nfserr = nfserrno(PTR_ERR(acl)); |
| 56 | goto fail; |
| 57 | } |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 58 | resp->acl_access = acl; |
| 59 | } |
| 60 | if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) { |
| 61 | /* Check how Solaris handles requests for the Default ACL |
| 62 | of a non-directory! */ |
Christoph Hellwig | 4ac7249 | 2013-12-20 05:16:55 -0800 | [diff] [blame] | 63 | acl = get_acl(inode, ACL_TYPE_DEFAULT); |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 64 | if (IS_ERR(acl)) { |
Christoph Hellwig | 4ac7249 | 2013-12-20 05:16:55 -0800 | [diff] [blame] | 65 | nfserr = nfserrno(PTR_ERR(acl)); |
| 66 | goto fail; |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 67 | } |
| 68 | resp->acl_default = acl; |
| 69 | } |
| 70 | |
| 71 | /* resp->acl_{access,default} are released in nfs3svc_release_getacl. */ |
| 72 | RETURN_STATUS(0); |
| 73 | |
| 74 | fail: |
| 75 | posix_acl_release(resp->acl_access); |
| 76 | posix_acl_release(resp->acl_default); |
| 77 | RETURN_STATUS(nfserr); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Set the Access and/or Default ACL of a file. |
| 82 | */ |
Al Viro | 7111c66 | 2006-10-19 23:28:45 -0700 | [diff] [blame] | 83 | static __be32 nfsd3_proc_setacl(struct svc_rqst * rqstp, |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 84 | struct nfsd3_setaclargs *argp, |
| 85 | struct nfsd3_attrstat *resp) |
| 86 | { |
Christoph Hellwig | 4ac7249 | 2013-12-20 05:16:55 -0800 | [diff] [blame] | 87 | struct inode *inode; |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 88 | svc_fh *fh; |
Al Viro | c4d987b | 2006-10-19 23:29:00 -0700 | [diff] [blame] | 89 | __be32 nfserr = 0; |
Christoph Hellwig | 4ac7249 | 2013-12-20 05:16:55 -0800 | [diff] [blame] | 90 | int error; |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 91 | |
| 92 | fh = fh_copy(&resp->fh, &argp->fh); |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 93 | nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR); |
Christoph Hellwig | 4ac7249 | 2013-12-20 05:16:55 -0800 | [diff] [blame] | 94 | if (nfserr) |
| 95 | goto out; |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 96 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 97 | inode = d_inode(fh->fh_dentry); |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 98 | |
Christoph Hellwig | 4ac7249 | 2013-12-20 05:16:55 -0800 | [diff] [blame] | 99 | error = fh_want_write(fh); |
| 100 | if (error) |
| 101 | goto out_errno; |
| 102 | |
Ben Hutchings | 9996537 | 2016-06-22 19:43:35 +0100 | [diff] [blame] | 103 | fh_lock(fh); |
Christoph Hellwig | 4ac7249 | 2013-12-20 05:16:55 -0800 | [diff] [blame] | 104 | |
Ben Hutchings | 9996537 | 2016-06-22 19:43:35 +0100 | [diff] [blame] | 105 | error = set_posix_acl(inode, ACL_TYPE_ACCESS, argp->acl_access); |
| 106 | if (error) |
| 107 | goto out_drop_lock; |
| 108 | error = set_posix_acl(inode, ACL_TYPE_DEFAULT, argp->acl_default); |
| 109 | |
| 110 | out_drop_lock: |
| 111 | fh_unlock(fh); |
Christoph Hellwig | 4ac7249 | 2013-12-20 05:16:55 -0800 | [diff] [blame] | 112 | fh_drop_write(fh); |
| 113 | out_errno: |
| 114 | nfserr = nfserrno(error); |
| 115 | out: |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 116 | /* argp->acl_{access,default} may have been allocated in |
| 117 | nfs3svc_decode_setaclargs. */ |
| 118 | posix_acl_release(argp->acl_access); |
| 119 | posix_acl_release(argp->acl_default); |
| 120 | RETURN_STATUS(nfserr); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * XDR decode functions |
| 125 | */ |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 126 | static int nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p, |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 127 | struct nfsd3_getaclargs *args) |
| 128 | { |
Benoit Taine | d40aa33 | 2014-05-22 16:32:30 +0200 | [diff] [blame] | 129 | p = nfs3svc_decode_fh(p, &args->fh); |
| 130 | if (!p) |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 131 | return 0; |
| 132 | args->mask = ntohl(*p); p++; |
| 133 | |
| 134 | return xdr_argsize_check(rqstp, p); |
| 135 | } |
| 136 | |
| 137 | |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 138 | static int nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p, |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 139 | struct nfsd3_setaclargs *args) |
| 140 | { |
| 141 | struct kvec *head = rqstp->rq_arg.head; |
| 142 | unsigned int base; |
| 143 | int n; |
| 144 | |
Benoit Taine | d40aa33 | 2014-05-22 16:32:30 +0200 | [diff] [blame] | 145 | p = nfs3svc_decode_fh(p, &args->fh); |
| 146 | if (!p) |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 147 | return 0; |
| 148 | args->mask = ntohl(*p++); |
Kinglong Mee | 7b8f458 | 2015-07-03 19:39:02 +0800 | [diff] [blame] | 149 | if (args->mask & ~NFS_ACL_MASK || |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 150 | !xdr_argsize_check(rqstp, p)) |
| 151 | return 0; |
| 152 | |
| 153 | base = (char *)p - (char *)head->iov_base; |
| 154 | n = nfsacl_decode(&rqstp->rq_arg, base, NULL, |
| 155 | (args->mask & NFS_ACL) ? |
| 156 | &args->acl_access : NULL); |
| 157 | if (n > 0) |
| 158 | n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL, |
| 159 | (args->mask & NFS_DFACL) ? |
| 160 | &args->acl_default : NULL); |
| 161 | return (n > 0); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * XDR encode functions |
| 166 | */ |
| 167 | |
| 168 | /* GETACL */ |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 169 | static int nfs3svc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p, |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 170 | struct nfsd3_getaclres *resp) |
| 171 | { |
| 172 | struct dentry *dentry = resp->fh.fh_dentry; |
| 173 | |
| 174 | p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 175 | if (resp->status == 0 && dentry && d_really_is_positive(dentry)) { |
| 176 | struct inode *inode = d_inode(dentry); |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 177 | struct kvec *head = rqstp->rq_res.head; |
| 178 | unsigned int base; |
| 179 | int n; |
Jesper Juhl | 14d2b59 | 2006-12-08 02:39:40 -0800 | [diff] [blame] | 180 | int w; |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 181 | |
| 182 | *p++ = htonl(resp->mask); |
| 183 | if (!xdr_ressize_check(rqstp, p)) |
| 184 | return 0; |
| 185 | base = (char *)p - (char *)head->iov_base; |
| 186 | |
Jesper Juhl | 14d2b59 | 2006-12-08 02:39:40 -0800 | [diff] [blame] | 187 | rqstp->rq_res.page_len = w = nfsacl_size( |
| 188 | (resp->mask & NFS_ACL) ? resp->acl_access : NULL, |
| 189 | (resp->mask & NFS_DFACL) ? resp->acl_default : NULL); |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 190 | while (w > 0) { |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 191 | if (!*(rqstp->rq_next_page++)) |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 192 | return 0; |
| 193 | w -= PAGE_SIZE; |
| 194 | } |
| 195 | |
| 196 | n = nfsacl_encode(&rqstp->rq_res, base, inode, |
| 197 | resp->acl_access, |
| 198 | resp->mask & NFS_ACL, 0); |
| 199 | if (n > 0) |
| 200 | n = nfsacl_encode(&rqstp->rq_res, base + n, inode, |
| 201 | resp->acl_default, |
| 202 | resp->mask & NFS_DFACL, |
| 203 | NFS_ACL_DEFAULT); |
| 204 | if (n <= 0) |
| 205 | return 0; |
| 206 | } else |
| 207 | if (!xdr_ressize_check(rqstp, p)) |
| 208 | return 0; |
| 209 | |
| 210 | return 1; |
| 211 | } |
| 212 | |
| 213 | /* SETACL */ |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 214 | static int nfs3svc_encode_setaclres(struct svc_rqst *rqstp, __be32 *p, |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 215 | struct nfsd3_attrstat *resp) |
| 216 | { |
| 217 | p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh); |
| 218 | |
| 219 | return xdr_ressize_check(rqstp, p); |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * XDR release functions |
| 224 | */ |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 225 | static int nfs3svc_release_getacl(struct svc_rqst *rqstp, __be32 *p, |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 226 | struct nfsd3_getaclres *resp) |
| 227 | { |
| 228 | fh_put(&resp->fh); |
| 229 | posix_acl_release(resp->acl_access); |
| 230 | posix_acl_release(resp->acl_default); |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | #define nfs3svc_decode_voidargs NULL |
| 235 | #define nfs3svc_release_void NULL |
| 236 | #define nfsd3_setaclres nfsd3_attrstat |
| 237 | #define nfsd3_voidres nfsd3_voidargs |
| 238 | struct nfsd3_voidargs { int dummy; }; |
| 239 | |
| 240 | #define PROC(name, argt, rest, relt, cache, respsize) \ |
| 241 | { (svc_procfunc) nfsd3_proc_##name, \ |
| 242 | (kxdrproc_t) nfs3svc_decode_##argt##args, \ |
| 243 | (kxdrproc_t) nfs3svc_encode_##rest##res, \ |
| 244 | (kxdrproc_t) nfs3svc_release_##relt, \ |
| 245 | sizeof(struct nfsd3_##argt##args), \ |
| 246 | sizeof(struct nfsd3_##rest##res), \ |
| 247 | 0, \ |
| 248 | cache, \ |
| 249 | respsize, \ |
| 250 | } |
| 251 | |
| 252 | #define ST 1 /* status*/ |
| 253 | #define AT 21 /* attributes */ |
| 254 | #define pAT (1+AT) /* post attributes - conditional */ |
| 255 | #define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */ |
| 256 | |
| 257 | static struct svc_procedure nfsd_acl_procedures3[] = { |
| 258 | PROC(null, void, void, void, RC_NOCACHE, ST), |
| 259 | PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)), |
| 260 | PROC(setacl, setacl, setacl, fhandle, RC_NOCACHE, ST+pAT), |
| 261 | }; |
| 262 | |
| 263 | struct svc_version nfsd_acl_version3 = { |
| 264 | .vs_vers = 3, |
| 265 | .vs_nproc = 3, |
| 266 | .vs_proc = nfsd_acl_procedures3, |
| 267 | .vs_dispatch = nfsd_dispatch, |
| 268 | .vs_xdrsize = NFS3_SVC_XDRSIZE, |
Peter Staubach | 1b7e040 | 2009-11-02 16:59:07 -0500 | [diff] [blame] | 269 | .vs_hidden = 0, |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 270 | }; |
| 271 | |