nfs2acl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Process version 2 NFSACL requests.
  3. *
  4. * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
  5. */
  6. #include "nfsd.h"
  7. /* FIXME: nfsacl.h is a broken header */
  8. #include <linux/nfsacl.h>
  9. #include <linux/gfp.h>
  10. #include "cache.h"
  11. #include "xdr3.h"
  12. #include "vfs.h"
  13. #define NFSDDBG_FACILITY NFSDDBG_PROC
  14. #define RETURN_STATUS(st) { resp->status = (st); return (st); }
  15. /*
  16. * NULL call.
  17. */
  18. static __be32
  19. nfsacld_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  20. {
  21. return nfs_ok;
  22. }
  23. /*
  24. * Get the Access and/or Default ACL of a file.
  25. */
  26. static __be32 nfsacld_proc_getacl(struct svc_rqst * rqstp,
  27. struct nfsd3_getaclargs *argp, struct nfsd3_getaclres *resp)
  28. {
  29. struct posix_acl *acl;
  30. struct inode *inode;
  31. svc_fh *fh;
  32. __be32 nfserr = 0;
  33. dprintk("nfsd: GETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
  34. fh = fh_copy(&resp->fh, &argp->fh);
  35. nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
  36. if (nfserr)
  37. RETURN_STATUS(nfserr);
  38. inode = d_inode(fh->fh_dentry);
  39. if (argp->mask & ~NFS_ACL_MASK)
  40. RETURN_STATUS(nfserr_inval);
  41. resp->mask = argp->mask;
  42. nfserr = fh_getattr(fh, &resp->stat);
  43. if (nfserr)
  44. RETURN_STATUS(nfserr);
  45. if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
  46. acl = get_acl(inode, ACL_TYPE_ACCESS);
  47. if (acl == NULL) {
  48. /* Solaris returns the inode's minimum ACL. */
  49. acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  50. }
  51. if (IS_ERR(acl)) {
  52. nfserr = nfserrno(PTR_ERR(acl));
  53. goto fail;
  54. }
  55. resp->acl_access = acl;
  56. }
  57. if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
  58. /* Check how Solaris handles requests for the Default ACL
  59. of a non-directory! */
  60. acl = get_acl(inode, ACL_TYPE_DEFAULT);
  61. if (IS_ERR(acl)) {
  62. nfserr = nfserrno(PTR_ERR(acl));
  63. goto fail;
  64. }
  65. resp->acl_default = acl;
  66. }
  67. /* resp->acl_{access,default} are released in nfssvc_release_getacl. */
  68. RETURN_STATUS(0);
  69. fail:
  70. posix_acl_release(resp->acl_access);
  71. posix_acl_release(resp->acl_default);
  72. RETURN_STATUS(nfserr);
  73. }
  74. /*
  75. * Set the Access and/or Default ACL of a file.
  76. */
  77. static __be32 nfsacld_proc_setacl(struct svc_rqst * rqstp,
  78. struct nfsd3_setaclargs *argp,
  79. struct nfsd_attrstat *resp)
  80. {
  81. struct inode *inode;
  82. svc_fh *fh;
  83. __be32 nfserr = 0;
  84. int error;
  85. dprintk("nfsd: SETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
  86. fh = fh_copy(&resp->fh, &argp->fh);
  87. nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
  88. if (nfserr)
  89. goto out;
  90. inode = d_inode(fh->fh_dentry);
  91. error = fh_want_write(fh);
  92. if (error)
  93. goto out_errno;
  94. fh_lock(fh);
  95. error = set_posix_acl(inode, ACL_TYPE_ACCESS, argp->acl_access);
  96. if (error)
  97. goto out_drop_lock;
  98. error = set_posix_acl(inode, ACL_TYPE_DEFAULT, argp->acl_default);
  99. if (error)
  100. goto out_drop_lock;
  101. fh_unlock(fh);
  102. fh_drop_write(fh);
  103. nfserr = fh_getattr(fh, &resp->stat);
  104. out:
  105. /* argp->acl_{access,default} may have been allocated in
  106. nfssvc_decode_setaclargs. */
  107. posix_acl_release(argp->acl_access);
  108. posix_acl_release(argp->acl_default);
  109. return nfserr;
  110. out_drop_lock:
  111. fh_unlock(fh);
  112. fh_drop_write(fh);
  113. out_errno:
  114. nfserr = nfserrno(error);
  115. goto out;
  116. }
  117. /*
  118. * Check file attributes
  119. */
  120. static __be32 nfsacld_proc_getattr(struct svc_rqst * rqstp,
  121. struct nfsd_fhandle *argp, struct nfsd_attrstat *resp)
  122. {
  123. __be32 nfserr;
  124. dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
  125. fh_copy(&resp->fh, &argp->fh);
  126. nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
  127. if (nfserr)
  128. return nfserr;
  129. nfserr = fh_getattr(&resp->fh, &resp->stat);
  130. return nfserr;
  131. }
  132. /*
  133. * Check file access
  134. */
  135. static __be32 nfsacld_proc_access(struct svc_rqst *rqstp, struct nfsd3_accessargs *argp,
  136. struct nfsd3_accessres *resp)
  137. {
  138. __be32 nfserr;
  139. dprintk("nfsd: ACCESS(2acl) %s 0x%x\n",
  140. SVCFH_fmt(&argp->fh),
  141. argp->access);
  142. fh_copy(&resp->fh, &argp->fh);
  143. resp->access = argp->access;
  144. nfserr = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
  145. if (nfserr)
  146. return nfserr;
  147. nfserr = fh_getattr(&resp->fh, &resp->stat);
  148. return nfserr;
  149. }
  150. /*
  151. * XDR decode functions
  152. */
  153. static int nfsaclsvc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p,
  154. struct nfsd3_getaclargs *argp)
  155. {
  156. p = nfs2svc_decode_fh(p, &argp->fh);
  157. if (!p)
  158. return 0;
  159. argp->mask = ntohl(*p); p++;
  160. return xdr_argsize_check(rqstp, p);
  161. }
  162. static int nfsaclsvc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p,
  163. struct nfsd3_setaclargs *argp)
  164. {
  165. struct kvec *head = rqstp->rq_arg.head;
  166. unsigned int base;
  167. int n;
  168. p = nfs2svc_decode_fh(p, &argp->fh);
  169. if (!p)
  170. return 0;
  171. argp->mask = ntohl(*p++);
  172. if (argp->mask & ~NFS_ACL_MASK ||
  173. !xdr_argsize_check(rqstp, p))
  174. return 0;
  175. base = (char *)p - (char *)head->iov_base;
  176. n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
  177. (argp->mask & NFS_ACL) ?
  178. &argp->acl_access : NULL);
  179. if (n > 0)
  180. n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
  181. (argp->mask & NFS_DFACL) ?
  182. &argp->acl_default : NULL);
  183. return (n > 0);
  184. }
  185. static int nfsaclsvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p,
  186. struct nfsd_fhandle *argp)
  187. {
  188. p = nfs2svc_decode_fh(p, &argp->fh);
  189. if (!p)
  190. return 0;
  191. return xdr_argsize_check(rqstp, p);
  192. }
  193. static int nfsaclsvc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
  194. struct nfsd3_accessargs *argp)
  195. {
  196. p = nfs2svc_decode_fh(p, &argp->fh);
  197. if (!p)
  198. return 0;
  199. argp->access = ntohl(*p++);
  200. return xdr_argsize_check(rqstp, p);
  201. }
  202. /*
  203. * XDR encode functions
  204. */
  205. /*
  206. * There must be an encoding function for void results so svc_process
  207. * will work properly.
  208. */
  209. static int nfsaclsvc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  210. {
  211. return xdr_ressize_check(rqstp, p);
  212. }
  213. /* GETACL */
  214. static int nfsaclsvc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p,
  215. struct nfsd3_getaclres *resp)
  216. {
  217. struct dentry *dentry = resp->fh.fh_dentry;
  218. struct inode *inode;
  219. struct kvec *head = rqstp->rq_res.head;
  220. unsigned int base;
  221. int n;
  222. int w;
  223. /*
  224. * Since this is version 2, the check for nfserr in
  225. * nfsd_dispatch actually ensures the following cannot happen.
  226. * However, it seems fragile to depend on that.
  227. */
  228. if (dentry == NULL || d_really_is_negative(dentry))
  229. return 0;
  230. inode = d_inode(dentry);
  231. p = nfs2svc_encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  232. *p++ = htonl(resp->mask);
  233. if (!xdr_ressize_check(rqstp, p))
  234. return 0;
  235. base = (char *)p - (char *)head->iov_base;
  236. rqstp->rq_res.page_len = w = nfsacl_size(
  237. (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
  238. (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
  239. while (w > 0) {
  240. if (!*(rqstp->rq_next_page++))
  241. return 0;
  242. w -= PAGE_SIZE;
  243. }
  244. n = nfsacl_encode(&rqstp->rq_res, base, inode,
  245. resp->acl_access,
  246. resp->mask & NFS_ACL, 0);
  247. if (n > 0)
  248. n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
  249. resp->acl_default,
  250. resp->mask & NFS_DFACL,
  251. NFS_ACL_DEFAULT);
  252. return (n > 0);
  253. }
  254. static int nfsaclsvc_encode_attrstatres(struct svc_rqst *rqstp, __be32 *p,
  255. struct nfsd_attrstat *resp)
  256. {
  257. p = nfs2svc_encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  258. return xdr_ressize_check(rqstp, p);
  259. }
  260. /* ACCESS */
  261. static int nfsaclsvc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
  262. struct nfsd3_accessres *resp)
  263. {
  264. p = nfs2svc_encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  265. *p++ = htonl(resp->access);
  266. return xdr_ressize_check(rqstp, p);
  267. }
  268. /*
  269. * XDR release functions
  270. */
  271. static int nfsaclsvc_release_getacl(struct svc_rqst *rqstp, __be32 *p,
  272. struct nfsd3_getaclres *resp)
  273. {
  274. fh_put(&resp->fh);
  275. posix_acl_release(resp->acl_access);
  276. posix_acl_release(resp->acl_default);
  277. return 1;
  278. }
  279. static int nfsaclsvc_release_attrstat(struct svc_rqst *rqstp, __be32 *p,
  280. struct nfsd_attrstat *resp)
  281. {
  282. fh_put(&resp->fh);
  283. return 1;
  284. }
  285. static int nfsaclsvc_release_access(struct svc_rqst *rqstp, __be32 *p,
  286. struct nfsd3_accessres *resp)
  287. {
  288. fh_put(&resp->fh);
  289. return 1;
  290. }
  291. #define nfsaclsvc_decode_voidargs NULL
  292. #define nfsaclsvc_release_void NULL
  293. #define nfsd3_fhandleargs nfsd_fhandle
  294. #define nfsd3_attrstatres nfsd_attrstat
  295. #define nfsd3_voidres nfsd3_voidargs
  296. struct nfsd3_voidargs { int dummy; };
  297. #define PROC(name, argt, rest, relt, cache, respsize) \
  298. { (svc_procfunc) nfsacld_proc_##name, \
  299. (kxdrproc_t) nfsaclsvc_decode_##argt##args, \
  300. (kxdrproc_t) nfsaclsvc_encode_##rest##res, \
  301. (kxdrproc_t) nfsaclsvc_release_##relt, \
  302. sizeof(struct nfsd3_##argt##args), \
  303. sizeof(struct nfsd3_##rest##res), \
  304. 0, \
  305. cache, \
  306. respsize, \
  307. }
  308. #define ST 1 /* status*/
  309. #define AT 21 /* attributes */
  310. #define pAT (1+AT) /* post attributes - conditional */
  311. #define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
  312. static struct svc_procedure nfsd_acl_procedures2[] = {
  313. PROC(null, void, void, void, RC_NOCACHE, ST),
  314. PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)),
  315. PROC(setacl, setacl, attrstat, attrstat, RC_NOCACHE, ST+AT),
  316. PROC(getattr, fhandle, attrstat, attrstat, RC_NOCACHE, ST+AT),
  317. PROC(access, access, access, access, RC_NOCACHE, ST+AT+1),
  318. };
  319. struct svc_version nfsd_acl_version2 = {
  320. .vs_vers = 2,
  321. .vs_nproc = 5,
  322. .vs_proc = nfsd_acl_procedures2,
  323. .vs_dispatch = nfsd_dispatch,
  324. .vs_xdrsize = NFS3_SVC_XDRSIZE,
  325. .vs_hidden = 0,
  326. };