nfs4proc.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  1. /*
  2. * Server-side procedures for NFSv4.
  3. *
  4. * Copyright (c) 2002 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * Kendrick Smith <kmsmith@umich.edu>
  8. * Andy Adamson <andros@umich.edu>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <linux/file.h>
  36. #include <linux/falloc.h>
  37. #include <linux/slab.h>
  38. #include "idmap.h"
  39. #include "cache.h"
  40. #include "xdr4.h"
  41. #include "vfs.h"
  42. #include "current_stateid.h"
  43. #include "netns.h"
  44. #include "acl.h"
  45. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  46. #include <linux/security.h>
  47. static inline void
  48. nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
  49. {
  50. struct inode *inode = resfh->fh_dentry->d_inode;
  51. int status;
  52. mutex_lock(&inode->i_mutex);
  53. status = security_inode_setsecctx(resfh->fh_dentry,
  54. label->data, label->len);
  55. mutex_unlock(&inode->i_mutex);
  56. if (status)
  57. /*
  58. * XXX: We should really fail the whole open, but we may
  59. * already have created a new file, so it may be too
  60. * late. For now this seems the least of evils:
  61. */
  62. bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
  63. return;
  64. }
  65. #else
  66. static inline void
  67. nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
  68. { }
  69. #endif
  70. #define NFSDDBG_FACILITY NFSDDBG_PROC
  71. static u32 nfsd_attrmask[] = {
  72. NFSD_WRITEABLE_ATTRS_WORD0,
  73. NFSD_WRITEABLE_ATTRS_WORD1,
  74. NFSD_WRITEABLE_ATTRS_WORD2
  75. };
  76. static u32 nfsd41_ex_attrmask[] = {
  77. NFSD_SUPPATTR_EXCLCREAT_WORD0,
  78. NFSD_SUPPATTR_EXCLCREAT_WORD1,
  79. NFSD_SUPPATTR_EXCLCREAT_WORD2
  80. };
  81. static __be32
  82. check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  83. u32 *bmval, u32 *writable)
  84. {
  85. struct dentry *dentry = cstate->current_fh.fh_dentry;
  86. /*
  87. * Check about attributes are supported by the NFSv4 server or not.
  88. * According to spec, unsupported attributes return ERR_ATTRNOTSUPP.
  89. */
  90. if ((bmval[0] & ~nfsd_suppattrs0(cstate->minorversion)) ||
  91. (bmval[1] & ~nfsd_suppattrs1(cstate->minorversion)) ||
  92. (bmval[2] & ~nfsd_suppattrs2(cstate->minorversion)))
  93. return nfserr_attrnotsupp;
  94. /*
  95. * Check FATTR4_WORD0_ACL can be supported
  96. * in current environment or not.
  97. */
  98. if (bmval[0] & FATTR4_WORD0_ACL) {
  99. if (!IS_POSIXACL(dentry->d_inode))
  100. return nfserr_attrnotsupp;
  101. }
  102. /*
  103. * According to spec, read-only attributes return ERR_INVAL.
  104. */
  105. if (writable) {
  106. if ((bmval[0] & ~writable[0]) || (bmval[1] & ~writable[1]) ||
  107. (bmval[2] & ~writable[2]))
  108. return nfserr_inval;
  109. }
  110. return nfs_ok;
  111. }
  112. static __be32
  113. nfsd4_check_open_attributes(struct svc_rqst *rqstp,
  114. struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  115. {
  116. __be32 status = nfs_ok;
  117. if (open->op_create == NFS4_OPEN_CREATE) {
  118. if (open->op_createmode == NFS4_CREATE_UNCHECKED
  119. || open->op_createmode == NFS4_CREATE_GUARDED)
  120. status = check_attr_support(rqstp, cstate,
  121. open->op_bmval, nfsd_attrmask);
  122. else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
  123. status = check_attr_support(rqstp, cstate,
  124. open->op_bmval, nfsd41_ex_attrmask);
  125. }
  126. return status;
  127. }
  128. static int
  129. is_create_with_attrs(struct nfsd4_open *open)
  130. {
  131. return open->op_create == NFS4_OPEN_CREATE
  132. && (open->op_createmode == NFS4_CREATE_UNCHECKED
  133. || open->op_createmode == NFS4_CREATE_GUARDED
  134. || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
  135. }
  136. /*
  137. * if error occurs when setting the acl, just clear the acl bit
  138. * in the returned attr bitmap.
  139. */
  140. static void
  141. do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  142. struct nfs4_acl *acl, u32 *bmval)
  143. {
  144. __be32 status;
  145. status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
  146. if (status)
  147. /*
  148. * We should probably fail the whole open at this point,
  149. * but we've already created the file, so it's too late;
  150. * So this seems the least of evils:
  151. */
  152. bmval[0] &= ~FATTR4_WORD0_ACL;
  153. }
  154. static inline void
  155. fh_dup2(struct svc_fh *dst, struct svc_fh *src)
  156. {
  157. fh_put(dst);
  158. dget(src->fh_dentry);
  159. if (src->fh_export)
  160. exp_get(src->fh_export);
  161. *dst = *src;
  162. }
  163. static __be32
  164. do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
  165. {
  166. __be32 status;
  167. if (open->op_truncate &&
  168. !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  169. return nfserr_inval;
  170. accmode |= NFSD_MAY_READ_IF_EXEC;
  171. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  172. accmode |= NFSD_MAY_READ;
  173. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  174. accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
  175. if (open->op_share_deny & NFS4_SHARE_DENY_READ)
  176. accmode |= NFSD_MAY_WRITE;
  177. status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
  178. return status;
  179. }
  180. static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
  181. {
  182. umode_t mode = fh->fh_dentry->d_inode->i_mode;
  183. if (S_ISREG(mode))
  184. return nfs_ok;
  185. if (S_ISDIR(mode))
  186. return nfserr_isdir;
  187. /*
  188. * Using err_symlink as our catch-all case may look odd; but
  189. * there's no other obvious error for this case in 4.0, and we
  190. * happen to know that it will cause the linux v4 client to do
  191. * the right thing on attempts to open something other than a
  192. * regular file.
  193. */
  194. return nfserr_symlink;
  195. }
  196. static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
  197. {
  198. if (nfsd4_has_session(cstate))
  199. return;
  200. fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
  201. &resfh->fh_handle);
  202. }
  203. static __be32
  204. do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
  205. {
  206. struct svc_fh *current_fh = &cstate->current_fh;
  207. int accmode;
  208. __be32 status;
  209. *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  210. if (!*resfh)
  211. return nfserr_jukebox;
  212. fh_init(*resfh, NFS4_FHSIZE);
  213. open->op_truncate = 0;
  214. if (open->op_create) {
  215. /* FIXME: check session persistence and pnfs flags.
  216. * The nfsv4.1 spec requires the following semantics:
  217. *
  218. * Persistent | pNFS | Server REQUIRED | Client Allowed
  219. * Reply Cache | server | |
  220. * -------------+--------+-----------------+--------------------
  221. * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1
  222. * | | | (SHOULD)
  223. * | | and EXCLUSIVE4 | or EXCLUSIVE4
  224. * | | | (SHOULD NOT)
  225. * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1
  226. * yes | no | GUARDED4 | GUARDED4
  227. * yes | yes | GUARDED4 | GUARDED4
  228. */
  229. /*
  230. * Note: create modes (UNCHECKED,GUARDED...) are the same
  231. * in NFSv4 as in v3 except EXCLUSIVE4_1.
  232. */
  233. status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
  234. open->op_fname.len, &open->op_iattr,
  235. *resfh, open->op_createmode,
  236. (u32 *)open->op_verf.data,
  237. &open->op_truncate, &open->op_created);
  238. if (!status && open->op_label.len)
  239. nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
  240. /*
  241. * Following rfc 3530 14.2.16, use the returned bitmask
  242. * to indicate which attributes we used to store the
  243. * verifier:
  244. */
  245. if (open->op_createmode == NFS4_CREATE_EXCLUSIVE && status == 0)
  246. open->op_bmval[1] = (FATTR4_WORD1_TIME_ACCESS |
  247. FATTR4_WORD1_TIME_MODIFY);
  248. } else
  249. /*
  250. * Note this may exit with the parent still locked.
  251. * We will hold the lock until nfsd4_open's final
  252. * lookup, to prevent renames or unlinks until we've had
  253. * a chance to an acquire a delegation if appropriate.
  254. */
  255. status = nfsd_lookup(rqstp, current_fh,
  256. open->op_fname.data, open->op_fname.len, *resfh);
  257. if (status)
  258. goto out;
  259. status = nfsd_check_obj_isreg(*resfh);
  260. if (status)
  261. goto out;
  262. if (is_create_with_attrs(open) && open->op_acl != NULL)
  263. do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
  264. nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
  265. accmode = NFSD_MAY_NOP;
  266. if (open->op_created ||
  267. open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
  268. accmode |= NFSD_MAY_OWNER_OVERRIDE;
  269. status = do_open_permission(rqstp, *resfh, open, accmode);
  270. set_change_info(&open->op_cinfo, current_fh);
  271. out:
  272. return status;
  273. }
  274. static __be32
  275. do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  276. {
  277. struct svc_fh *current_fh = &cstate->current_fh;
  278. __be32 status;
  279. int accmode = 0;
  280. /* We don't know the target directory, and therefore can not
  281. * set the change info
  282. */
  283. memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
  284. nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
  285. open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
  286. (open->op_iattr.ia_size == 0);
  287. /*
  288. * In the delegation case, the client is telling us about an
  289. * open that it *already* performed locally, some time ago. We
  290. * should let it succeed now if possible.
  291. *
  292. * In the case of a CLAIM_FH open, on the other hand, the client
  293. * may be counting on us to enforce permissions (the Linux 4.1
  294. * client uses this for normal opens, for example).
  295. */
  296. if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
  297. accmode = NFSD_MAY_OWNER_OVERRIDE;
  298. status = do_open_permission(rqstp, current_fh, open, accmode);
  299. return status;
  300. }
  301. static void
  302. copy_clientid(clientid_t *clid, struct nfsd4_session *session)
  303. {
  304. struct nfsd4_sessionid *sid =
  305. (struct nfsd4_sessionid *)session->se_sessionid.data;
  306. clid->cl_boot = sid->clientid.cl_boot;
  307. clid->cl_id = sid->clientid.cl_id;
  308. }
  309. static __be32
  310. nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  311. struct nfsd4_open *open)
  312. {
  313. __be32 status;
  314. struct svc_fh *resfh = NULL;
  315. struct nfsd4_compoundres *resp;
  316. struct net *net = SVC_NET(rqstp);
  317. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  318. dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
  319. (int)open->op_fname.len, open->op_fname.data,
  320. open->op_openowner);
  321. /* This check required by spec. */
  322. if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
  323. return nfserr_inval;
  324. open->op_created = 0;
  325. /*
  326. * RFC5661 18.51.3
  327. * Before RECLAIM_COMPLETE done, server should deny new lock
  328. */
  329. if (nfsd4_has_session(cstate) &&
  330. !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  331. &cstate->session->se_client->cl_flags) &&
  332. open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  333. return nfserr_grace;
  334. if (nfsd4_has_session(cstate))
  335. copy_clientid(&open->op_clientid, cstate->session);
  336. /* check seqid for replay. set nfs4_owner */
  337. resp = rqstp->rq_resp;
  338. status = nfsd4_process_open1(&resp->cstate, open, nn);
  339. if (status == nfserr_replay_me) {
  340. struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
  341. fh_put(&cstate->current_fh);
  342. fh_copy_shallow(&cstate->current_fh.fh_handle,
  343. &rp->rp_openfh);
  344. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  345. if (status)
  346. dprintk("nfsd4_open: replay failed"
  347. " restoring previous filehandle\n");
  348. else
  349. status = nfserr_replay_me;
  350. }
  351. if (status)
  352. goto out;
  353. if (open->op_xdr_error) {
  354. status = open->op_xdr_error;
  355. goto out;
  356. }
  357. status = nfsd4_check_open_attributes(rqstp, cstate, open);
  358. if (status)
  359. goto out;
  360. /* Openowner is now set, so sequence id will get bumped. Now we need
  361. * these checks before we do any creates: */
  362. status = nfserr_grace;
  363. if (locks_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  364. goto out;
  365. status = nfserr_no_grace;
  366. if (!locks_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  367. goto out;
  368. switch (open->op_claim_type) {
  369. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  370. case NFS4_OPEN_CLAIM_NULL:
  371. status = do_open_lookup(rqstp, cstate, open, &resfh);
  372. if (status)
  373. goto out;
  374. break;
  375. case NFS4_OPEN_CLAIM_PREVIOUS:
  376. status = nfs4_check_open_reclaim(&open->op_clientid,
  377. cstate, nn);
  378. if (status)
  379. goto out;
  380. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  381. case NFS4_OPEN_CLAIM_FH:
  382. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  383. status = do_open_fhandle(rqstp, cstate, open);
  384. if (status)
  385. goto out;
  386. resfh = &cstate->current_fh;
  387. break;
  388. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  389. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  390. dprintk("NFSD: unsupported OPEN claim type %d\n",
  391. open->op_claim_type);
  392. status = nfserr_notsupp;
  393. goto out;
  394. default:
  395. dprintk("NFSD: Invalid OPEN claim type %d\n",
  396. open->op_claim_type);
  397. status = nfserr_inval;
  398. goto out;
  399. }
  400. /*
  401. * nfsd4_process_open2() does the actual opening of the file. If
  402. * successful, it (1) truncates the file if open->op_truncate was
  403. * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
  404. */
  405. status = nfsd4_process_open2(rqstp, resfh, open);
  406. WARN(status && open->op_created,
  407. "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
  408. be32_to_cpu(status));
  409. out:
  410. if (resfh && resfh != &cstate->current_fh) {
  411. fh_dup2(&cstate->current_fh, resfh);
  412. fh_put(resfh);
  413. kfree(resfh);
  414. }
  415. nfsd4_cleanup_open_state(cstate, open, status);
  416. nfsd4_bump_seqid(cstate, status);
  417. return status;
  418. }
  419. /*
  420. * OPEN is the only seqid-mutating operation whose decoding can fail
  421. * with a seqid-mutating error (specifically, decoding of user names in
  422. * the attributes). Therefore we have to do some processing to look up
  423. * the stateowner so that we can bump the seqid.
  424. */
  425. static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
  426. {
  427. struct nfsd4_open *open = (struct nfsd4_open *)&op->u;
  428. if (!seqid_mutating_err(ntohl(op->status)))
  429. return op->status;
  430. if (nfsd4_has_session(cstate))
  431. return op->status;
  432. open->op_xdr_error = op->status;
  433. return nfsd4_open(rqstp, cstate, open);
  434. }
  435. /*
  436. * filehandle-manipulating ops.
  437. */
  438. static __be32
  439. nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  440. struct svc_fh **getfh)
  441. {
  442. if (!cstate->current_fh.fh_dentry)
  443. return nfserr_nofilehandle;
  444. *getfh = &cstate->current_fh;
  445. return nfs_ok;
  446. }
  447. static __be32
  448. nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  449. struct nfsd4_putfh *putfh)
  450. {
  451. fh_put(&cstate->current_fh);
  452. cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
  453. memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
  454. putfh->pf_fhlen);
  455. return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
  456. }
  457. static __be32
  458. nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  459. void *arg)
  460. {
  461. __be32 status;
  462. fh_put(&cstate->current_fh);
  463. status = exp_pseudoroot(rqstp, &cstate->current_fh);
  464. return status;
  465. }
  466. static __be32
  467. nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  468. void *arg)
  469. {
  470. if (!cstate->save_fh.fh_dentry)
  471. return nfserr_restorefh;
  472. fh_dup2(&cstate->current_fh, &cstate->save_fh);
  473. if (HAS_STATE_ID(cstate, SAVED_STATE_ID_FLAG)) {
  474. memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
  475. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  476. }
  477. return nfs_ok;
  478. }
  479. static __be32
  480. nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  481. void *arg)
  482. {
  483. if (!cstate->current_fh.fh_dentry)
  484. return nfserr_nofilehandle;
  485. fh_dup2(&cstate->save_fh, &cstate->current_fh);
  486. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG)) {
  487. memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
  488. SET_STATE_ID(cstate, SAVED_STATE_ID_FLAG);
  489. }
  490. return nfs_ok;
  491. }
  492. /*
  493. * misc nfsv4 ops
  494. */
  495. static __be32
  496. nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  497. struct nfsd4_access *access)
  498. {
  499. if (access->ac_req_access & ~NFS3_ACCESS_FULL)
  500. return nfserr_inval;
  501. access->ac_resp_access = access->ac_req_access;
  502. return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
  503. &access->ac_supported);
  504. }
  505. static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
  506. {
  507. __be32 verf[2];
  508. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  509. /*
  510. * This is opaque to client, so no need to byte-swap. Use
  511. * __force to keep sparse happy
  512. */
  513. verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
  514. verf[1] = (__force __be32)nn->nfssvc_boot.tv_usec;
  515. memcpy(verifier->data, verf, sizeof(verifier->data));
  516. }
  517. static __be32
  518. nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  519. struct nfsd4_commit *commit)
  520. {
  521. gen_boot_verifier(&commit->co_verf, SVC_NET(rqstp));
  522. return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
  523. commit->co_count);
  524. }
  525. static __be32
  526. nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  527. struct nfsd4_create *create)
  528. {
  529. struct svc_fh resfh;
  530. __be32 status;
  531. dev_t rdev;
  532. fh_init(&resfh, NFS4_FHSIZE);
  533. status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR,
  534. NFSD_MAY_CREATE);
  535. if (status)
  536. return status;
  537. status = check_attr_support(rqstp, cstate, create->cr_bmval,
  538. nfsd_attrmask);
  539. if (status)
  540. return status;
  541. switch (create->cr_type) {
  542. case NF4LNK:
  543. status = nfsd_symlink(rqstp, &cstate->current_fh,
  544. create->cr_name, create->cr_namelen,
  545. create->cr_data, &resfh);
  546. break;
  547. case NF4BLK:
  548. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  549. if (MAJOR(rdev) != create->cr_specdata1 ||
  550. MINOR(rdev) != create->cr_specdata2)
  551. return nfserr_inval;
  552. status = nfsd_create(rqstp, &cstate->current_fh,
  553. create->cr_name, create->cr_namelen,
  554. &create->cr_iattr, S_IFBLK, rdev, &resfh);
  555. break;
  556. case NF4CHR:
  557. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  558. if (MAJOR(rdev) != create->cr_specdata1 ||
  559. MINOR(rdev) != create->cr_specdata2)
  560. return nfserr_inval;
  561. status = nfsd_create(rqstp, &cstate->current_fh,
  562. create->cr_name, create->cr_namelen,
  563. &create->cr_iattr,S_IFCHR, rdev, &resfh);
  564. break;
  565. case NF4SOCK:
  566. status = nfsd_create(rqstp, &cstate->current_fh,
  567. create->cr_name, create->cr_namelen,
  568. &create->cr_iattr, S_IFSOCK, 0, &resfh);
  569. break;
  570. case NF4FIFO:
  571. status = nfsd_create(rqstp, &cstate->current_fh,
  572. create->cr_name, create->cr_namelen,
  573. &create->cr_iattr, S_IFIFO, 0, &resfh);
  574. break;
  575. case NF4DIR:
  576. create->cr_iattr.ia_valid &= ~ATTR_SIZE;
  577. status = nfsd_create(rqstp, &cstate->current_fh,
  578. create->cr_name, create->cr_namelen,
  579. &create->cr_iattr, S_IFDIR, 0, &resfh);
  580. break;
  581. default:
  582. status = nfserr_badtype;
  583. }
  584. if (status)
  585. goto out;
  586. if (create->cr_label.len)
  587. nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
  588. if (create->cr_acl != NULL)
  589. do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
  590. create->cr_bmval);
  591. fh_unlock(&cstate->current_fh);
  592. set_change_info(&create->cr_cinfo, &cstate->current_fh);
  593. fh_dup2(&cstate->current_fh, &resfh);
  594. out:
  595. fh_put(&resfh);
  596. return status;
  597. }
  598. static __be32
  599. nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  600. struct nfsd4_getattr *getattr)
  601. {
  602. __be32 status;
  603. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  604. if (status)
  605. return status;
  606. if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  607. return nfserr_inval;
  608. getattr->ga_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
  609. getattr->ga_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
  610. getattr->ga_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
  611. getattr->ga_fhp = &cstate->current_fh;
  612. return nfs_ok;
  613. }
  614. static __be32
  615. nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  616. struct nfsd4_link *link)
  617. {
  618. __be32 status = nfserr_nofilehandle;
  619. if (!cstate->save_fh.fh_dentry)
  620. return status;
  621. status = nfsd_link(rqstp, &cstate->current_fh,
  622. link->li_name, link->li_namelen, &cstate->save_fh);
  623. if (!status)
  624. set_change_info(&link->li_cinfo, &cstate->current_fh);
  625. return status;
  626. }
  627. static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
  628. {
  629. struct svc_fh tmp_fh;
  630. __be32 ret;
  631. fh_init(&tmp_fh, NFS4_FHSIZE);
  632. ret = exp_pseudoroot(rqstp, &tmp_fh);
  633. if (ret)
  634. return ret;
  635. if (tmp_fh.fh_dentry == fh->fh_dentry) {
  636. fh_put(&tmp_fh);
  637. return nfserr_noent;
  638. }
  639. fh_put(&tmp_fh);
  640. return nfsd_lookup(rqstp, fh, "..", 2, fh);
  641. }
  642. static __be32
  643. nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  644. void *arg)
  645. {
  646. return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  647. }
  648. static __be32
  649. nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  650. struct nfsd4_lookup *lookup)
  651. {
  652. return nfsd_lookup(rqstp, &cstate->current_fh,
  653. lookup->lo_name, lookup->lo_len,
  654. &cstate->current_fh);
  655. }
  656. static __be32
  657. nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  658. struct nfsd4_read *read)
  659. {
  660. __be32 status;
  661. /* no need to check permission - this will be done in nfsd_read() */
  662. read->rd_filp = NULL;
  663. if (read->rd_offset >= OFFSET_MAX)
  664. return nfserr_inval;
  665. /*
  666. * If we do a zero copy read, then a client will see read data
  667. * that reflects the state of the file *after* performing the
  668. * following compound.
  669. *
  670. * To ensure proper ordering, we therefore turn off zero copy if
  671. * the client wants us to do more in this compound:
  672. */
  673. if (!nfsd4_last_compound_op(rqstp))
  674. clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
  675. /* check stateid */
  676. if ((status = nfs4_preprocess_stateid_op(SVC_NET(rqstp),
  677. cstate, &read->rd_stateid,
  678. RD_STATE, &read->rd_filp))) {
  679. dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
  680. goto out;
  681. }
  682. status = nfs_ok;
  683. out:
  684. read->rd_rqstp = rqstp;
  685. read->rd_fhp = &cstate->current_fh;
  686. return status;
  687. }
  688. static __be32
  689. nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  690. struct nfsd4_readdir *readdir)
  691. {
  692. u64 cookie = readdir->rd_cookie;
  693. static const nfs4_verifier zeroverf;
  694. /* no need to check permission - this will be done in nfsd_readdir() */
  695. if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  696. return nfserr_inval;
  697. readdir->rd_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
  698. readdir->rd_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
  699. readdir->rd_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
  700. if ((cookie == 1) || (cookie == 2) ||
  701. (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
  702. return nfserr_bad_cookie;
  703. readdir->rd_rqstp = rqstp;
  704. readdir->rd_fhp = &cstate->current_fh;
  705. return nfs_ok;
  706. }
  707. static __be32
  708. nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  709. struct nfsd4_readlink *readlink)
  710. {
  711. readlink->rl_rqstp = rqstp;
  712. readlink->rl_fhp = &cstate->current_fh;
  713. return nfs_ok;
  714. }
  715. static __be32
  716. nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  717. struct nfsd4_remove *remove)
  718. {
  719. __be32 status;
  720. if (locks_in_grace(SVC_NET(rqstp)))
  721. return nfserr_grace;
  722. status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
  723. remove->rm_name, remove->rm_namelen);
  724. if (!status) {
  725. fh_unlock(&cstate->current_fh);
  726. set_change_info(&remove->rm_cinfo, &cstate->current_fh);
  727. }
  728. return status;
  729. }
  730. static __be32
  731. nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  732. struct nfsd4_rename *rename)
  733. {
  734. __be32 status = nfserr_nofilehandle;
  735. if (!cstate->save_fh.fh_dentry)
  736. return status;
  737. if (locks_in_grace(SVC_NET(rqstp)) &&
  738. !(cstate->save_fh.fh_export->ex_flags & NFSEXP_NOSUBTREECHECK))
  739. return nfserr_grace;
  740. status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
  741. rename->rn_snamelen, &cstate->current_fh,
  742. rename->rn_tname, rename->rn_tnamelen);
  743. if (status)
  744. return status;
  745. set_change_info(&rename->rn_sinfo, &cstate->current_fh);
  746. set_change_info(&rename->rn_tinfo, &cstate->save_fh);
  747. return nfs_ok;
  748. }
  749. static __be32
  750. nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  751. struct nfsd4_secinfo *secinfo)
  752. {
  753. struct svc_fh resfh;
  754. struct svc_export *exp;
  755. struct dentry *dentry;
  756. __be32 err;
  757. fh_init(&resfh, NFS4_FHSIZE);
  758. err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
  759. if (err)
  760. return err;
  761. err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
  762. secinfo->si_name, secinfo->si_namelen,
  763. &exp, &dentry);
  764. if (err)
  765. return err;
  766. if (dentry->d_inode == NULL) {
  767. exp_put(exp);
  768. err = nfserr_noent;
  769. } else
  770. secinfo->si_exp = exp;
  771. dput(dentry);
  772. if (cstate->minorversion)
  773. /* See rfc 5661 section 2.6.3.1.1.8 */
  774. fh_put(&cstate->current_fh);
  775. return err;
  776. }
  777. static __be32
  778. nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  779. struct nfsd4_secinfo_no_name *sin)
  780. {
  781. __be32 err;
  782. switch (sin->sin_style) {
  783. case NFS4_SECINFO_STYLE4_CURRENT_FH:
  784. break;
  785. case NFS4_SECINFO_STYLE4_PARENT:
  786. err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  787. if (err)
  788. return err;
  789. break;
  790. default:
  791. return nfserr_inval;
  792. }
  793. sin->sin_exp = exp_get(cstate->current_fh.fh_export);
  794. fh_put(&cstate->current_fh);
  795. return nfs_ok;
  796. }
  797. static __be32
  798. nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  799. struct nfsd4_setattr *setattr)
  800. {
  801. __be32 status = nfs_ok;
  802. int err;
  803. if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
  804. status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate,
  805. &setattr->sa_stateid, WR_STATE, NULL);
  806. if (status) {
  807. dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
  808. return status;
  809. }
  810. }
  811. err = fh_want_write(&cstate->current_fh);
  812. if (err)
  813. return nfserrno(err);
  814. status = nfs_ok;
  815. status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
  816. nfsd_attrmask);
  817. if (status)
  818. goto out;
  819. if (setattr->sa_acl != NULL)
  820. status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
  821. setattr->sa_acl);
  822. if (status)
  823. goto out;
  824. if (setattr->sa_label.len)
  825. status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
  826. &setattr->sa_label);
  827. if (status)
  828. goto out;
  829. status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
  830. 0, (time_t)0);
  831. out:
  832. fh_drop_write(&cstate->current_fh);
  833. return status;
  834. }
  835. static int fill_in_write_vector(struct kvec *vec, struct nfsd4_write *write)
  836. {
  837. int i = 1;
  838. int buflen = write->wr_buflen;
  839. vec[0].iov_base = write->wr_head.iov_base;
  840. vec[0].iov_len = min_t(int, buflen, write->wr_head.iov_len);
  841. buflen -= vec[0].iov_len;
  842. while (buflen) {
  843. vec[i].iov_base = page_address(write->wr_pagelist[i - 1]);
  844. vec[i].iov_len = min_t(int, PAGE_SIZE, buflen);
  845. buflen -= vec[i].iov_len;
  846. i++;
  847. }
  848. return i;
  849. }
  850. static __be32
  851. nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  852. struct nfsd4_write *write)
  853. {
  854. stateid_t *stateid = &write->wr_stateid;
  855. struct file *filp = NULL;
  856. __be32 status = nfs_ok;
  857. unsigned long cnt;
  858. int nvecs;
  859. /* no need to check permission - this will be done in nfsd_write() */
  860. if (write->wr_offset >= OFFSET_MAX)
  861. return nfserr_inval;
  862. status = nfs4_preprocess_stateid_op(SVC_NET(rqstp),
  863. cstate, stateid, WR_STATE, &filp);
  864. if (status) {
  865. dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
  866. return status;
  867. }
  868. cnt = write->wr_buflen;
  869. write->wr_how_written = write->wr_stable_how;
  870. gen_boot_verifier(&write->wr_verifier, SVC_NET(rqstp));
  871. nvecs = fill_in_write_vector(rqstp->rq_vec, write);
  872. WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
  873. status = nfsd_write(rqstp, &cstate->current_fh, filp,
  874. write->wr_offset, rqstp->rq_vec, nvecs,
  875. &cnt, &write->wr_how_written);
  876. if (filp)
  877. fput(filp);
  878. write->wr_bytes_written = cnt;
  879. return status;
  880. }
  881. static __be32
  882. nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  883. struct nfsd4_fallocate *fallocate, int flags)
  884. {
  885. __be32 status = nfserr_notsupp;
  886. struct file *file;
  887. status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate,
  888. &fallocate->falloc_stateid,
  889. WR_STATE, &file);
  890. if (status != nfs_ok) {
  891. dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
  892. return status;
  893. }
  894. status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, file,
  895. fallocate->falloc_offset,
  896. fallocate->falloc_length,
  897. flags);
  898. fput(file);
  899. return status;
  900. }
  901. static __be32
  902. nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  903. struct nfsd4_fallocate *fallocate)
  904. {
  905. return nfsd4_fallocate(rqstp, cstate, fallocate, 0);
  906. }
  907. static __be32
  908. nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  909. struct nfsd4_fallocate *fallocate)
  910. {
  911. return nfsd4_fallocate(rqstp, cstate, fallocate,
  912. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
  913. }
  914. static __be32
  915. nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  916. struct nfsd4_seek *seek)
  917. {
  918. int whence;
  919. __be32 status;
  920. struct file *file;
  921. status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate,
  922. &seek->seek_stateid,
  923. RD_STATE, &file);
  924. if (status) {
  925. dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
  926. return status;
  927. }
  928. switch (seek->seek_whence) {
  929. case NFS4_CONTENT_DATA:
  930. whence = SEEK_DATA;
  931. break;
  932. case NFS4_CONTENT_HOLE:
  933. whence = SEEK_HOLE;
  934. break;
  935. default:
  936. status = nfserr_union_notsupp;
  937. goto out;
  938. }
  939. /*
  940. * Note: This call does change file->f_pos, but nothing in NFSD
  941. * should ever file->f_pos.
  942. */
  943. seek->seek_pos = vfs_llseek(file, seek->seek_offset, whence);
  944. if (seek->seek_pos < 0)
  945. status = nfserrno(seek->seek_pos);
  946. else if (seek->seek_pos >= i_size_read(file_inode(file)))
  947. seek->seek_eof = true;
  948. out:
  949. fput(file);
  950. return status;
  951. }
  952. /* This routine never returns NFS_OK! If there are no other errors, it
  953. * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
  954. * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
  955. * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
  956. */
  957. static __be32
  958. _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  959. struct nfsd4_verify *verify)
  960. {
  961. __be32 *buf, *p;
  962. int count;
  963. __be32 status;
  964. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  965. if (status)
  966. return status;
  967. status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
  968. if (status)
  969. return status;
  970. if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
  971. || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
  972. return nfserr_inval;
  973. if (verify->ve_attrlen & 3)
  974. return nfserr_inval;
  975. /* count in words:
  976. * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
  977. */
  978. count = 4 + (verify->ve_attrlen >> 2);
  979. buf = kmalloc(count << 2, GFP_KERNEL);
  980. if (!buf)
  981. return nfserr_jukebox;
  982. p = buf;
  983. status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
  984. cstate->current_fh.fh_export,
  985. cstate->current_fh.fh_dentry,
  986. verify->ve_bmval,
  987. rqstp, 0);
  988. /*
  989. * If nfsd4_encode_fattr() ran out of space, assume that's because
  990. * the attributes are longer (hence different) than those given:
  991. */
  992. if (status == nfserr_resource)
  993. status = nfserr_not_same;
  994. if (status)
  995. goto out_kfree;
  996. /* skip bitmap */
  997. p = buf + 1 + ntohl(buf[0]);
  998. status = nfserr_not_same;
  999. if (ntohl(*p++) != verify->ve_attrlen)
  1000. goto out_kfree;
  1001. if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
  1002. status = nfserr_same;
  1003. out_kfree:
  1004. kfree(buf);
  1005. return status;
  1006. }
  1007. static __be32
  1008. nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1009. struct nfsd4_verify *verify)
  1010. {
  1011. __be32 status;
  1012. status = _nfsd4_verify(rqstp, cstate, verify);
  1013. return status == nfserr_not_same ? nfs_ok : status;
  1014. }
  1015. static __be32
  1016. nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1017. struct nfsd4_verify *verify)
  1018. {
  1019. __be32 status;
  1020. status = _nfsd4_verify(rqstp, cstate, verify);
  1021. return status == nfserr_same ? nfs_ok : status;
  1022. }
  1023. /*
  1024. * NULL call.
  1025. */
  1026. static __be32
  1027. nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  1028. {
  1029. return nfs_ok;
  1030. }
  1031. static inline void nfsd4_increment_op_stats(u32 opnum)
  1032. {
  1033. if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
  1034. nfsdstats.nfs4_opcount[opnum]++;
  1035. }
  1036. typedef __be32(*nfsd4op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
  1037. void *);
  1038. typedef u32(*nfsd4op_rsize)(struct svc_rqst *, struct nfsd4_op *op);
  1039. typedef void(*stateid_setter)(struct nfsd4_compound_state *, void *);
  1040. typedef void(*stateid_getter)(struct nfsd4_compound_state *, void *);
  1041. enum nfsd4_op_flags {
  1042. ALLOWED_WITHOUT_FH = 1 << 0, /* No current filehandle required */
  1043. ALLOWED_ON_ABSENT_FS = 1 << 1, /* ops processed on absent fs */
  1044. ALLOWED_AS_FIRST_OP = 1 << 2, /* ops reqired first in compound */
  1045. /* For rfc 5661 section 2.6.3.1.1: */
  1046. OP_HANDLES_WRONGSEC = 1 << 3,
  1047. OP_IS_PUTFH_LIKE = 1 << 4,
  1048. /*
  1049. * These are the ops whose result size we estimate before
  1050. * encoding, to avoid performing an op then not being able to
  1051. * respond or cache a response. This includes writes and setattrs
  1052. * as well as the operations usually called "nonidempotent":
  1053. */
  1054. OP_MODIFIES_SOMETHING = 1 << 5,
  1055. /*
  1056. * Cache compounds containing these ops in the xid-based drc:
  1057. * We use the DRC for compounds containing non-idempotent
  1058. * operations, *except* those that are 4.1-specific (since
  1059. * sessions provide their own EOS), and except for stateful
  1060. * operations other than setclientid and setclientid_confirm
  1061. * (since sequence numbers provide EOS for open, lock, etc in
  1062. * the v4.0 case).
  1063. */
  1064. OP_CACHEME = 1 << 6,
  1065. /*
  1066. * These are ops which clear current state id.
  1067. */
  1068. OP_CLEAR_STATEID = 1 << 7,
  1069. };
  1070. struct nfsd4_operation {
  1071. nfsd4op_func op_func;
  1072. u32 op_flags;
  1073. char *op_name;
  1074. /* Try to get response size before operation */
  1075. nfsd4op_rsize op_rsize_bop;
  1076. stateid_getter op_get_currentstateid;
  1077. stateid_setter op_set_currentstateid;
  1078. };
  1079. static struct nfsd4_operation nfsd4_ops[];
  1080. static const char *nfsd4_op_name(unsigned opnum);
  1081. /*
  1082. * Enforce NFSv4.1 COMPOUND ordering rules:
  1083. *
  1084. * Also note, enforced elsewhere:
  1085. * - SEQUENCE other than as first op results in
  1086. * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
  1087. * - BIND_CONN_TO_SESSION must be the only op in its compound.
  1088. * (Enforced in nfsd4_bind_conn_to_session().)
  1089. * - DESTROY_SESSION must be the final operation in a compound, if
  1090. * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
  1091. * (Enforced in nfsd4_destroy_session().)
  1092. */
  1093. static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
  1094. {
  1095. struct nfsd4_op *op = &args->ops[0];
  1096. /* These ordering requirements don't apply to NFSv4.0: */
  1097. if (args->minorversion == 0)
  1098. return nfs_ok;
  1099. /* This is weird, but OK, not our problem: */
  1100. if (args->opcnt == 0)
  1101. return nfs_ok;
  1102. if (op->status == nfserr_op_illegal)
  1103. return nfs_ok;
  1104. if (!(nfsd4_ops[op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
  1105. return nfserr_op_not_in_session;
  1106. if (op->opnum == OP_SEQUENCE)
  1107. return nfs_ok;
  1108. if (args->opcnt != 1)
  1109. return nfserr_not_only_op;
  1110. return nfs_ok;
  1111. }
  1112. static inline struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
  1113. {
  1114. return &nfsd4_ops[op->opnum];
  1115. }
  1116. bool nfsd4_cache_this_op(struct nfsd4_op *op)
  1117. {
  1118. if (op->opnum == OP_ILLEGAL)
  1119. return false;
  1120. return OPDESC(op)->op_flags & OP_CACHEME;
  1121. }
  1122. static bool need_wrongsec_check(struct svc_rqst *rqstp)
  1123. {
  1124. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1125. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  1126. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  1127. struct nfsd4_op *next = &argp->ops[resp->opcnt];
  1128. struct nfsd4_operation *thisd;
  1129. struct nfsd4_operation *nextd;
  1130. thisd = OPDESC(this);
  1131. /*
  1132. * Most ops check wronsec on our own; only the putfh-like ops
  1133. * have special rules.
  1134. */
  1135. if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
  1136. return false;
  1137. /*
  1138. * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
  1139. * put-filehandle operation if we're not going to use the
  1140. * result:
  1141. */
  1142. if (argp->opcnt == resp->opcnt)
  1143. return false;
  1144. if (next->opnum == OP_ILLEGAL)
  1145. return false;
  1146. nextd = OPDESC(next);
  1147. /*
  1148. * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
  1149. * errors themselves as necessary; others should check for them
  1150. * now:
  1151. */
  1152. return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
  1153. }
  1154. static void svcxdr_init_encode(struct svc_rqst *rqstp,
  1155. struct nfsd4_compoundres *resp)
  1156. {
  1157. struct xdr_stream *xdr = &resp->xdr;
  1158. struct xdr_buf *buf = &rqstp->rq_res;
  1159. struct kvec *head = buf->head;
  1160. xdr->buf = buf;
  1161. xdr->iov = head;
  1162. xdr->p = head->iov_base + head->iov_len;
  1163. xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
  1164. /* Tail and page_len should be zero at this point: */
  1165. buf->len = buf->head[0].iov_len;
  1166. xdr->scratch.iov_len = 0;
  1167. xdr->page_ptr = buf->pages - 1;
  1168. buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
  1169. - rqstp->rq_auth_slack;
  1170. }
  1171. /*
  1172. * COMPOUND call.
  1173. */
  1174. static __be32
  1175. nfsd4_proc_compound(struct svc_rqst *rqstp,
  1176. struct nfsd4_compoundargs *args,
  1177. struct nfsd4_compoundres *resp)
  1178. {
  1179. struct nfsd4_op *op;
  1180. struct nfsd4_operation *opdesc;
  1181. struct nfsd4_compound_state *cstate = &resp->cstate;
  1182. struct svc_fh *current_fh = &cstate->current_fh;
  1183. struct svc_fh *save_fh = &cstate->save_fh;
  1184. __be32 status;
  1185. svcxdr_init_encode(rqstp, resp);
  1186. resp->tagp = resp->xdr.p;
  1187. /* reserve space for: taglen, tag, and opcnt */
  1188. xdr_reserve_space(&resp->xdr, 8 + args->taglen);
  1189. resp->taglen = args->taglen;
  1190. resp->tag = args->tag;
  1191. resp->rqstp = rqstp;
  1192. cstate->minorversion = args->minorversion;
  1193. fh_init(current_fh, NFS4_FHSIZE);
  1194. fh_init(save_fh, NFS4_FHSIZE);
  1195. /*
  1196. * Don't use the deferral mechanism for NFSv4; compounds make it
  1197. * too hard to avoid non-idempotency problems.
  1198. */
  1199. clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  1200. /*
  1201. * According to RFC3010, this takes precedence over all other errors.
  1202. */
  1203. status = nfserr_minor_vers_mismatch;
  1204. if (nfsd_minorversion(args->minorversion, NFSD_TEST) <= 0)
  1205. goto out;
  1206. status = nfs41_check_op_ordering(args);
  1207. if (status) {
  1208. op = &args->ops[0];
  1209. op->status = status;
  1210. goto encode_op;
  1211. }
  1212. while (!status && resp->opcnt < args->opcnt) {
  1213. op = &args->ops[resp->opcnt++];
  1214. dprintk("nfsv4 compound op #%d/%d: %d (%s)\n",
  1215. resp->opcnt, args->opcnt, op->opnum,
  1216. nfsd4_op_name(op->opnum));
  1217. /*
  1218. * The XDR decode routines may have pre-set op->status;
  1219. * for example, if there is a miscellaneous XDR error
  1220. * it will be set to nfserr_bad_xdr.
  1221. */
  1222. if (op->status) {
  1223. if (op->opnum == OP_OPEN)
  1224. op->status = nfsd4_open_omfg(rqstp, cstate, op);
  1225. goto encode_op;
  1226. }
  1227. opdesc = OPDESC(op);
  1228. if (!current_fh->fh_dentry) {
  1229. if (!(opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
  1230. op->status = nfserr_nofilehandle;
  1231. goto encode_op;
  1232. }
  1233. } else if (current_fh->fh_export->ex_fslocs.migrated &&
  1234. !(opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
  1235. op->status = nfserr_moved;
  1236. goto encode_op;
  1237. }
  1238. fh_clear_wcc(current_fh);
  1239. /* If op is non-idempotent */
  1240. if (opdesc->op_flags & OP_MODIFIES_SOMETHING) {
  1241. /*
  1242. * Don't execute this op if we couldn't encode a
  1243. * succesful reply:
  1244. */
  1245. u32 plen = opdesc->op_rsize_bop(rqstp, op);
  1246. /*
  1247. * Plus if there's another operation, make sure
  1248. * we'll have space to at least encode an error:
  1249. */
  1250. if (resp->opcnt < args->opcnt)
  1251. plen += COMPOUND_ERR_SLACK_SPACE;
  1252. op->status = nfsd4_check_resp_size(resp, plen);
  1253. }
  1254. if (op->status)
  1255. goto encode_op;
  1256. if (opdesc->op_get_currentstateid)
  1257. opdesc->op_get_currentstateid(cstate, &op->u);
  1258. op->status = opdesc->op_func(rqstp, cstate, &op->u);
  1259. if (!op->status) {
  1260. if (opdesc->op_set_currentstateid)
  1261. opdesc->op_set_currentstateid(cstate, &op->u);
  1262. if (opdesc->op_flags & OP_CLEAR_STATEID)
  1263. clear_current_stateid(cstate);
  1264. if (need_wrongsec_check(rqstp))
  1265. op->status = check_nfsd_access(current_fh->fh_export, rqstp);
  1266. }
  1267. encode_op:
  1268. /* Only from SEQUENCE */
  1269. if (cstate->status == nfserr_replay_cache) {
  1270. dprintk("%s NFS4.1 replay from cache\n", __func__);
  1271. status = op->status;
  1272. goto out;
  1273. }
  1274. if (op->status == nfserr_replay_me) {
  1275. op->replay = &cstate->replay_owner->so_replay;
  1276. nfsd4_encode_replay(&resp->xdr, op);
  1277. status = op->status = op->replay->rp_status;
  1278. } else {
  1279. nfsd4_encode_operation(resp, op);
  1280. status = op->status;
  1281. }
  1282. dprintk("nfsv4 compound op %p opcnt %d #%d: %d: status %d\n",
  1283. args->ops, args->opcnt, resp->opcnt, op->opnum,
  1284. be32_to_cpu(status));
  1285. nfsd4_cstate_clear_replay(cstate);
  1286. /* XXX Ugh, we need to get rid of this kind of special case: */
  1287. if (op->opnum == OP_READ && op->u.read.rd_filp)
  1288. fput(op->u.read.rd_filp);
  1289. nfsd4_increment_op_stats(op->opnum);
  1290. }
  1291. cstate->status = status;
  1292. fh_put(current_fh);
  1293. fh_put(save_fh);
  1294. BUG_ON(cstate->replay_owner);
  1295. out:
  1296. /* Reset deferral mechanism for RPC deferrals */
  1297. set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  1298. dprintk("nfsv4 compound returned %d\n", ntohl(status));
  1299. return status;
  1300. }
  1301. #define op_encode_hdr_size (2)
  1302. #define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
  1303. #define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
  1304. #define op_encode_change_info_maxsz (5)
  1305. #define nfs4_fattr_bitmap_maxsz (4)
  1306. /* We'll fall back on returning no lockowner if run out of space: */
  1307. #define op_encode_lockowner_maxsz (0)
  1308. #define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
  1309. #define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
  1310. #define op_encode_ace_maxsz (3 + nfs4_owner_maxsz)
  1311. #define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \
  1312. op_encode_ace_maxsz)
  1313. #define op_encode_channel_attrs_maxsz (6 + 1 + 1)
  1314. static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1315. {
  1316. return (op_encode_hdr_size) * sizeof(__be32);
  1317. }
  1318. static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1319. {
  1320. return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
  1321. }
  1322. static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1323. {
  1324. return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
  1325. }
  1326. static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1327. {
  1328. return (op_encode_hdr_size + op_encode_change_info_maxsz
  1329. + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  1330. }
  1331. /*
  1332. * Note since this is an idempotent operation we won't insist on failing
  1333. * the op prematurely if the estimate is too large. We may turn off splice
  1334. * reads unnecessarily.
  1335. */
  1336. static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
  1337. struct nfsd4_op *op)
  1338. {
  1339. u32 *bmap = op->u.getattr.ga_bmval;
  1340. u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
  1341. u32 ret = 0;
  1342. if (bmap0 & FATTR4_WORD0_ACL)
  1343. return svc_max_payload(rqstp);
  1344. if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
  1345. return svc_max_payload(rqstp);
  1346. if (bmap1 & FATTR4_WORD1_OWNER) {
  1347. ret += IDMAP_NAMESZ + 4;
  1348. bmap1 &= ~FATTR4_WORD1_OWNER;
  1349. }
  1350. if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
  1351. ret += IDMAP_NAMESZ + 4;
  1352. bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
  1353. }
  1354. if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
  1355. ret += NFS4_FHSIZE + 4;
  1356. bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
  1357. }
  1358. if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
  1359. ret += NFSD4_MAX_SEC_LABEL_LEN + 12;
  1360. bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  1361. }
  1362. /*
  1363. * Largest of remaining attributes are 16 bytes (e.g.,
  1364. * supported_attributes)
  1365. */
  1366. ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
  1367. /* bitmask, length */
  1368. ret += 20;
  1369. return ret;
  1370. }
  1371. static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1372. {
  1373. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  1374. * sizeof(__be32);
  1375. }
  1376. static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1377. {
  1378. return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
  1379. * sizeof(__be32);
  1380. }
  1381. static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1382. {
  1383. return (op_encode_hdr_size + op_encode_stateid_maxsz
  1384. + op_encode_change_info_maxsz + 1
  1385. + nfs4_fattr_bitmap_maxsz
  1386. + op_encode_delegation_maxsz) * sizeof(__be32);
  1387. }
  1388. static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1389. {
  1390. u32 maxcount = 0, rlen = 0;
  1391. maxcount = svc_max_payload(rqstp);
  1392. rlen = min(op->u.read.rd_length, maxcount);
  1393. return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
  1394. }
  1395. static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1396. {
  1397. u32 maxcount = 0, rlen = 0;
  1398. maxcount = svc_max_payload(rqstp);
  1399. rlen = min(op->u.readdir.rd_maxcount, maxcount);
  1400. return (op_encode_hdr_size + op_encode_verifier_maxsz +
  1401. XDR_QUADLEN(rlen)) * sizeof(__be32);
  1402. }
  1403. static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1404. {
  1405. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  1406. * sizeof(__be32);
  1407. }
  1408. static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1409. {
  1410. return (op_encode_hdr_size + op_encode_change_info_maxsz
  1411. + op_encode_change_info_maxsz) * sizeof(__be32);
  1412. }
  1413. static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
  1414. struct nfsd4_op *op)
  1415. {
  1416. return (op_encode_hdr_size
  1417. + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
  1418. }
  1419. static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1420. {
  1421. return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  1422. }
  1423. static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1424. {
  1425. return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
  1426. sizeof(__be32);
  1427. }
  1428. static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1429. {
  1430. return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
  1431. }
  1432. static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1433. {
  1434. return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
  1435. 1 + 1 + /* eir_flags, spr_how */\
  1436. 4 + /* spo_must_enforce & _allow with bitmap */\
  1437. 2 + /*eir_server_owner.so_minor_id */\
  1438. /* eir_server_owner.so_major_id<> */\
  1439. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  1440. /* eir_server_scope<> */\
  1441. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  1442. 1 + /* eir_server_impl_id array length */\
  1443. 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
  1444. }
  1445. static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1446. {
  1447. return (op_encode_hdr_size + \
  1448. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
  1449. 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
  1450. }
  1451. static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1452. {
  1453. return (op_encode_hdr_size + \
  1454. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
  1455. 2 + /* csr_sequence, csr_flags */\
  1456. op_encode_channel_attrs_maxsz + \
  1457. op_encode_channel_attrs_maxsz) * sizeof(__be32);
  1458. }
  1459. static struct nfsd4_operation nfsd4_ops[] = {
  1460. [OP_ACCESS] = {
  1461. .op_func = (nfsd4op_func)nfsd4_access,
  1462. .op_name = "OP_ACCESS",
  1463. },
  1464. [OP_CLOSE] = {
  1465. .op_func = (nfsd4op_func)nfsd4_close,
  1466. .op_flags = OP_MODIFIES_SOMETHING,
  1467. .op_name = "OP_CLOSE",
  1468. .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
  1469. .op_get_currentstateid = (stateid_getter)nfsd4_get_closestateid,
  1470. .op_set_currentstateid = (stateid_setter)nfsd4_set_closestateid,
  1471. },
  1472. [OP_COMMIT] = {
  1473. .op_func = (nfsd4op_func)nfsd4_commit,
  1474. .op_flags = OP_MODIFIES_SOMETHING,
  1475. .op_name = "OP_COMMIT",
  1476. .op_rsize_bop = (nfsd4op_rsize)nfsd4_commit_rsize,
  1477. },
  1478. [OP_CREATE] = {
  1479. .op_func = (nfsd4op_func)nfsd4_create,
  1480. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
  1481. .op_name = "OP_CREATE",
  1482. .op_rsize_bop = (nfsd4op_rsize)nfsd4_create_rsize,
  1483. },
  1484. [OP_DELEGRETURN] = {
  1485. .op_func = (nfsd4op_func)nfsd4_delegreturn,
  1486. .op_flags = OP_MODIFIES_SOMETHING,
  1487. .op_name = "OP_DELEGRETURN",
  1488. .op_rsize_bop = nfsd4_only_status_rsize,
  1489. .op_get_currentstateid = (stateid_getter)nfsd4_get_delegreturnstateid,
  1490. },
  1491. [OP_GETATTR] = {
  1492. .op_func = (nfsd4op_func)nfsd4_getattr,
  1493. .op_flags = ALLOWED_ON_ABSENT_FS,
  1494. .op_rsize_bop = nfsd4_getattr_rsize,
  1495. .op_name = "OP_GETATTR",
  1496. },
  1497. [OP_GETFH] = {
  1498. .op_func = (nfsd4op_func)nfsd4_getfh,
  1499. .op_name = "OP_GETFH",
  1500. },
  1501. [OP_LINK] = {
  1502. .op_func = (nfsd4op_func)nfsd4_link,
  1503. .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
  1504. | OP_CACHEME,
  1505. .op_name = "OP_LINK",
  1506. .op_rsize_bop = (nfsd4op_rsize)nfsd4_link_rsize,
  1507. },
  1508. [OP_LOCK] = {
  1509. .op_func = (nfsd4op_func)nfsd4_lock,
  1510. .op_flags = OP_MODIFIES_SOMETHING,
  1511. .op_name = "OP_LOCK",
  1512. .op_rsize_bop = (nfsd4op_rsize)nfsd4_lock_rsize,
  1513. .op_set_currentstateid = (stateid_setter)nfsd4_set_lockstateid,
  1514. },
  1515. [OP_LOCKT] = {
  1516. .op_func = (nfsd4op_func)nfsd4_lockt,
  1517. .op_name = "OP_LOCKT",
  1518. },
  1519. [OP_LOCKU] = {
  1520. .op_func = (nfsd4op_func)nfsd4_locku,
  1521. .op_flags = OP_MODIFIES_SOMETHING,
  1522. .op_name = "OP_LOCKU",
  1523. .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
  1524. .op_get_currentstateid = (stateid_getter)nfsd4_get_lockustateid,
  1525. },
  1526. [OP_LOOKUP] = {
  1527. .op_func = (nfsd4op_func)nfsd4_lookup,
  1528. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  1529. .op_name = "OP_LOOKUP",
  1530. },
  1531. [OP_LOOKUPP] = {
  1532. .op_func = (nfsd4op_func)nfsd4_lookupp,
  1533. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  1534. .op_name = "OP_LOOKUPP",
  1535. },
  1536. [OP_NVERIFY] = {
  1537. .op_func = (nfsd4op_func)nfsd4_nverify,
  1538. .op_name = "OP_NVERIFY",
  1539. },
  1540. [OP_OPEN] = {
  1541. .op_func = (nfsd4op_func)nfsd4_open,
  1542. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  1543. .op_name = "OP_OPEN",
  1544. .op_rsize_bop = (nfsd4op_rsize)nfsd4_open_rsize,
  1545. .op_set_currentstateid = (stateid_setter)nfsd4_set_openstateid,
  1546. },
  1547. [OP_OPEN_CONFIRM] = {
  1548. .op_func = (nfsd4op_func)nfsd4_open_confirm,
  1549. .op_flags = OP_MODIFIES_SOMETHING,
  1550. .op_name = "OP_OPEN_CONFIRM",
  1551. .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
  1552. },
  1553. [OP_OPEN_DOWNGRADE] = {
  1554. .op_func = (nfsd4op_func)nfsd4_open_downgrade,
  1555. .op_flags = OP_MODIFIES_SOMETHING,
  1556. .op_name = "OP_OPEN_DOWNGRADE",
  1557. .op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
  1558. .op_get_currentstateid = (stateid_getter)nfsd4_get_opendowngradestateid,
  1559. .op_set_currentstateid = (stateid_setter)nfsd4_set_opendowngradestateid,
  1560. },
  1561. [OP_PUTFH] = {
  1562. .op_func = (nfsd4op_func)nfsd4_putfh,
  1563. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1564. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1565. .op_name = "OP_PUTFH",
  1566. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1567. },
  1568. [OP_PUTPUBFH] = {
  1569. .op_func = (nfsd4op_func)nfsd4_putrootfh,
  1570. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1571. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1572. .op_name = "OP_PUTPUBFH",
  1573. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1574. },
  1575. [OP_PUTROOTFH] = {
  1576. .op_func = (nfsd4op_func)nfsd4_putrootfh,
  1577. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1578. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1579. .op_name = "OP_PUTROOTFH",
  1580. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1581. },
  1582. [OP_READ] = {
  1583. .op_func = (nfsd4op_func)nfsd4_read,
  1584. .op_name = "OP_READ",
  1585. .op_rsize_bop = (nfsd4op_rsize)nfsd4_read_rsize,
  1586. .op_get_currentstateid = (stateid_getter)nfsd4_get_readstateid,
  1587. },
  1588. [OP_READDIR] = {
  1589. .op_func = (nfsd4op_func)nfsd4_readdir,
  1590. .op_name = "OP_READDIR",
  1591. .op_rsize_bop = (nfsd4op_rsize)nfsd4_readdir_rsize,
  1592. },
  1593. [OP_READLINK] = {
  1594. .op_func = (nfsd4op_func)nfsd4_readlink,
  1595. .op_name = "OP_READLINK",
  1596. },
  1597. [OP_REMOVE] = {
  1598. .op_func = (nfsd4op_func)nfsd4_remove,
  1599. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1600. .op_name = "OP_REMOVE",
  1601. .op_rsize_bop = (nfsd4op_rsize)nfsd4_remove_rsize,
  1602. },
  1603. [OP_RENAME] = {
  1604. .op_func = (nfsd4op_func)nfsd4_rename,
  1605. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1606. .op_name = "OP_RENAME",
  1607. .op_rsize_bop = (nfsd4op_rsize)nfsd4_rename_rsize,
  1608. },
  1609. [OP_RENEW] = {
  1610. .op_func = (nfsd4op_func)nfsd4_renew,
  1611. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1612. | OP_MODIFIES_SOMETHING,
  1613. .op_name = "OP_RENEW",
  1614. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1615. },
  1616. [OP_RESTOREFH] = {
  1617. .op_func = (nfsd4op_func)nfsd4_restorefh,
  1618. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1619. | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
  1620. .op_name = "OP_RESTOREFH",
  1621. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1622. },
  1623. [OP_SAVEFH] = {
  1624. .op_func = (nfsd4op_func)nfsd4_savefh,
  1625. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  1626. .op_name = "OP_SAVEFH",
  1627. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1628. },
  1629. [OP_SECINFO] = {
  1630. .op_func = (nfsd4op_func)nfsd4_secinfo,
  1631. .op_flags = OP_HANDLES_WRONGSEC,
  1632. .op_name = "OP_SECINFO",
  1633. },
  1634. [OP_SETATTR] = {
  1635. .op_func = (nfsd4op_func)nfsd4_setattr,
  1636. .op_name = "OP_SETATTR",
  1637. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1638. .op_rsize_bop = (nfsd4op_rsize)nfsd4_setattr_rsize,
  1639. .op_get_currentstateid = (stateid_getter)nfsd4_get_setattrstateid,
  1640. },
  1641. [OP_SETCLIENTID] = {
  1642. .op_func = (nfsd4op_func)nfsd4_setclientid,
  1643. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1644. | OP_MODIFIES_SOMETHING | OP_CACHEME,
  1645. .op_name = "OP_SETCLIENTID",
  1646. .op_rsize_bop = (nfsd4op_rsize)nfsd4_setclientid_rsize,
  1647. },
  1648. [OP_SETCLIENTID_CONFIRM] = {
  1649. .op_func = (nfsd4op_func)nfsd4_setclientid_confirm,
  1650. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1651. | OP_MODIFIES_SOMETHING | OP_CACHEME,
  1652. .op_name = "OP_SETCLIENTID_CONFIRM",
  1653. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1654. },
  1655. [OP_VERIFY] = {
  1656. .op_func = (nfsd4op_func)nfsd4_verify,
  1657. .op_name = "OP_VERIFY",
  1658. },
  1659. [OP_WRITE] = {
  1660. .op_func = (nfsd4op_func)nfsd4_write,
  1661. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1662. .op_name = "OP_WRITE",
  1663. .op_rsize_bop = (nfsd4op_rsize)nfsd4_write_rsize,
  1664. .op_get_currentstateid = (stateid_getter)nfsd4_get_writestateid,
  1665. },
  1666. [OP_RELEASE_LOCKOWNER] = {
  1667. .op_func = (nfsd4op_func)nfsd4_release_lockowner,
  1668. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1669. | OP_MODIFIES_SOMETHING,
  1670. .op_name = "OP_RELEASE_LOCKOWNER",
  1671. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1672. },
  1673. /* NFSv4.1 operations */
  1674. [OP_EXCHANGE_ID] = {
  1675. .op_func = (nfsd4op_func)nfsd4_exchange_id,
  1676. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  1677. | OP_MODIFIES_SOMETHING,
  1678. .op_name = "OP_EXCHANGE_ID",
  1679. .op_rsize_bop = (nfsd4op_rsize)nfsd4_exchange_id_rsize,
  1680. },
  1681. [OP_BACKCHANNEL_CTL] = {
  1682. .op_func = (nfsd4op_func)nfsd4_backchannel_ctl,
  1683. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  1684. .op_name = "OP_BACKCHANNEL_CTL",
  1685. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1686. },
  1687. [OP_BIND_CONN_TO_SESSION] = {
  1688. .op_func = (nfsd4op_func)nfsd4_bind_conn_to_session,
  1689. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  1690. | OP_MODIFIES_SOMETHING,
  1691. .op_name = "OP_BIND_CONN_TO_SESSION",
  1692. .op_rsize_bop = (nfsd4op_rsize)nfsd4_bind_conn_to_session_rsize,
  1693. },
  1694. [OP_CREATE_SESSION] = {
  1695. .op_func = (nfsd4op_func)nfsd4_create_session,
  1696. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  1697. | OP_MODIFIES_SOMETHING,
  1698. .op_name = "OP_CREATE_SESSION",
  1699. .op_rsize_bop = (nfsd4op_rsize)nfsd4_create_session_rsize,
  1700. },
  1701. [OP_DESTROY_SESSION] = {
  1702. .op_func = (nfsd4op_func)nfsd4_destroy_session,
  1703. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  1704. | OP_MODIFIES_SOMETHING,
  1705. .op_name = "OP_DESTROY_SESSION",
  1706. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1707. },
  1708. [OP_SEQUENCE] = {
  1709. .op_func = (nfsd4op_func)nfsd4_sequence,
  1710. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  1711. .op_name = "OP_SEQUENCE",
  1712. .op_rsize_bop = (nfsd4op_rsize)nfsd4_sequence_rsize,
  1713. },
  1714. [OP_DESTROY_CLIENTID] = {
  1715. .op_func = (nfsd4op_func)nfsd4_destroy_clientid,
  1716. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  1717. | OP_MODIFIES_SOMETHING,
  1718. .op_name = "OP_DESTROY_CLIENTID",
  1719. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1720. },
  1721. [OP_RECLAIM_COMPLETE] = {
  1722. .op_func = (nfsd4op_func)nfsd4_reclaim_complete,
  1723. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  1724. .op_name = "OP_RECLAIM_COMPLETE",
  1725. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1726. },
  1727. [OP_SECINFO_NO_NAME] = {
  1728. .op_func = (nfsd4op_func)nfsd4_secinfo_no_name,
  1729. .op_flags = OP_HANDLES_WRONGSEC,
  1730. .op_name = "OP_SECINFO_NO_NAME",
  1731. },
  1732. [OP_TEST_STATEID] = {
  1733. .op_func = (nfsd4op_func)nfsd4_test_stateid,
  1734. .op_flags = ALLOWED_WITHOUT_FH,
  1735. .op_name = "OP_TEST_STATEID",
  1736. },
  1737. [OP_FREE_STATEID] = {
  1738. .op_func = (nfsd4op_func)nfsd4_free_stateid,
  1739. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  1740. .op_name = "OP_FREE_STATEID",
  1741. .op_get_currentstateid = (stateid_getter)nfsd4_get_freestateid,
  1742. .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
  1743. },
  1744. /* NFSv4.2 operations */
  1745. [OP_ALLOCATE] = {
  1746. .op_func = (nfsd4op_func)nfsd4_allocate,
  1747. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1748. .op_name = "OP_ALLOCATE",
  1749. .op_rsize_bop = (nfsd4op_rsize)nfsd4_write_rsize,
  1750. },
  1751. [OP_DEALLOCATE] = {
  1752. .op_func = (nfsd4op_func)nfsd4_deallocate,
  1753. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1754. .op_name = "OP_DEALLOCATE",
  1755. .op_rsize_bop = (nfsd4op_rsize)nfsd4_write_rsize,
  1756. },
  1757. [OP_SEEK] = {
  1758. .op_func = (nfsd4op_func)nfsd4_seek,
  1759. .op_name = "OP_SEEK",
  1760. },
  1761. };
  1762. int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1763. {
  1764. struct nfsd4_operation *opdesc;
  1765. nfsd4op_rsize estimator;
  1766. if (op->opnum == OP_ILLEGAL)
  1767. return op_encode_hdr_size * sizeof(__be32);
  1768. opdesc = OPDESC(op);
  1769. estimator = opdesc->op_rsize_bop;
  1770. return estimator ? estimator(rqstp, op) : PAGE_SIZE;
  1771. }
  1772. void warn_on_nonidempotent_op(struct nfsd4_op *op)
  1773. {
  1774. if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
  1775. pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
  1776. op->opnum, nfsd4_op_name(op->opnum));
  1777. WARN_ON_ONCE(1);
  1778. }
  1779. }
  1780. static const char *nfsd4_op_name(unsigned opnum)
  1781. {
  1782. if (opnum < ARRAY_SIZE(nfsd4_ops))
  1783. return nfsd4_ops[opnum].op_name;
  1784. return "unknown_operation";
  1785. }
  1786. #define nfsd4_voidres nfsd4_voidargs
  1787. struct nfsd4_voidargs { int dummy; };
  1788. static struct svc_procedure nfsd_procedures4[2] = {
  1789. [NFSPROC4_NULL] = {
  1790. .pc_func = (svc_procfunc) nfsd4_proc_null,
  1791. .pc_encode = (kxdrproc_t) nfs4svc_encode_voidres,
  1792. .pc_argsize = sizeof(struct nfsd4_voidargs),
  1793. .pc_ressize = sizeof(struct nfsd4_voidres),
  1794. .pc_cachetype = RC_NOCACHE,
  1795. .pc_xdrressize = 1,
  1796. },
  1797. [NFSPROC4_COMPOUND] = {
  1798. .pc_func = (svc_procfunc) nfsd4_proc_compound,
  1799. .pc_decode = (kxdrproc_t) nfs4svc_decode_compoundargs,
  1800. .pc_encode = (kxdrproc_t) nfs4svc_encode_compoundres,
  1801. .pc_argsize = sizeof(struct nfsd4_compoundargs),
  1802. .pc_ressize = sizeof(struct nfsd4_compoundres),
  1803. .pc_release = nfsd4_release_compoundargs,
  1804. .pc_cachetype = RC_NOCACHE,
  1805. .pc_xdrressize = NFSD_BUFSIZE/4,
  1806. },
  1807. };
  1808. struct svc_version nfsd_version4 = {
  1809. .vs_vers = 4,
  1810. .vs_nproc = 2,
  1811. .vs_proc = nfsd_procedures4,
  1812. .vs_dispatch = nfsd_dispatch,
  1813. .vs_xdrsize = NFS4_SVC_XDRSIZE,
  1814. .vs_rpcb_optnl = 1,
  1815. };
  1816. /*
  1817. * Local variables:
  1818. * c-basic-offset: 8
  1819. * End:
  1820. */