nfs4proc.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561
  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. #include "pnfs.h"
  46. #include "trace.h"
  47. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  48. #include <linux/security.h>
  49. static inline void
  50. nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
  51. {
  52. struct inode *inode = d_inode(resfh->fh_dentry);
  53. int status;
  54. inode_lock(inode);
  55. status = security_inode_setsecctx(resfh->fh_dentry,
  56. label->data, label->len);
  57. inode_unlock(inode);
  58. if (status)
  59. /*
  60. * XXX: We should really fail the whole open, but we may
  61. * already have created a new file, so it may be too
  62. * late. For now this seems the least of evils:
  63. */
  64. bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
  65. return;
  66. }
  67. #else
  68. static inline void
  69. nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
  70. { }
  71. #endif
  72. #define NFSDDBG_FACILITY NFSDDBG_PROC
  73. static u32 nfsd_attrmask[] = {
  74. NFSD_WRITEABLE_ATTRS_WORD0,
  75. NFSD_WRITEABLE_ATTRS_WORD1,
  76. NFSD_WRITEABLE_ATTRS_WORD2
  77. };
  78. static u32 nfsd41_ex_attrmask[] = {
  79. NFSD_SUPPATTR_EXCLCREAT_WORD0,
  80. NFSD_SUPPATTR_EXCLCREAT_WORD1,
  81. NFSD_SUPPATTR_EXCLCREAT_WORD2
  82. };
  83. static __be32
  84. check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  85. u32 *bmval, u32 *writable)
  86. {
  87. struct dentry *dentry = cstate->current_fh.fh_dentry;
  88. struct svc_export *exp = cstate->current_fh.fh_export;
  89. if (!nfsd_attrs_supported(cstate->minorversion, bmval))
  90. return nfserr_attrnotsupp;
  91. if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
  92. return nfserr_attrnotsupp;
  93. if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
  94. !(exp->ex_flags & NFSEXP_SECURITY_LABEL))
  95. return nfserr_attrnotsupp;
  96. if (writable && !bmval_is_subset(bmval, writable))
  97. return nfserr_inval;
  98. if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
  99. (bmval[1] & FATTR4_WORD1_MODE))
  100. return nfserr_inval;
  101. return nfs_ok;
  102. }
  103. static __be32
  104. nfsd4_check_open_attributes(struct svc_rqst *rqstp,
  105. struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  106. {
  107. __be32 status = nfs_ok;
  108. if (open->op_create == NFS4_OPEN_CREATE) {
  109. if (open->op_createmode == NFS4_CREATE_UNCHECKED
  110. || open->op_createmode == NFS4_CREATE_GUARDED)
  111. status = check_attr_support(rqstp, cstate,
  112. open->op_bmval, nfsd_attrmask);
  113. else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
  114. status = check_attr_support(rqstp, cstate,
  115. open->op_bmval, nfsd41_ex_attrmask);
  116. }
  117. return status;
  118. }
  119. static int
  120. is_create_with_attrs(struct nfsd4_open *open)
  121. {
  122. return open->op_create == NFS4_OPEN_CREATE
  123. && (open->op_createmode == NFS4_CREATE_UNCHECKED
  124. || open->op_createmode == NFS4_CREATE_GUARDED
  125. || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
  126. }
  127. /*
  128. * if error occurs when setting the acl, just clear the acl bit
  129. * in the returned attr bitmap.
  130. */
  131. static void
  132. do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  133. struct nfs4_acl *acl, u32 *bmval)
  134. {
  135. __be32 status;
  136. status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
  137. if (status)
  138. /*
  139. * We should probably fail the whole open at this point,
  140. * but we've already created the file, so it's too late;
  141. * So this seems the least of evils:
  142. */
  143. bmval[0] &= ~FATTR4_WORD0_ACL;
  144. }
  145. static inline void
  146. fh_dup2(struct svc_fh *dst, struct svc_fh *src)
  147. {
  148. fh_put(dst);
  149. dget(src->fh_dentry);
  150. if (src->fh_export)
  151. exp_get(src->fh_export);
  152. *dst = *src;
  153. }
  154. static __be32
  155. do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
  156. {
  157. __be32 status;
  158. if (open->op_truncate &&
  159. !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  160. return nfserr_inval;
  161. accmode |= NFSD_MAY_READ_IF_EXEC;
  162. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  163. accmode |= NFSD_MAY_READ;
  164. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  165. accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
  166. if (open->op_share_deny & NFS4_SHARE_DENY_READ)
  167. accmode |= NFSD_MAY_WRITE;
  168. status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
  169. return status;
  170. }
  171. static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
  172. {
  173. umode_t mode = d_inode(fh->fh_dentry)->i_mode;
  174. if (S_ISREG(mode))
  175. return nfs_ok;
  176. if (S_ISDIR(mode))
  177. return nfserr_isdir;
  178. /*
  179. * Using err_symlink as our catch-all case may look odd; but
  180. * there's no other obvious error for this case in 4.0, and we
  181. * happen to know that it will cause the linux v4 client to do
  182. * the right thing on attempts to open something other than a
  183. * regular file.
  184. */
  185. return nfserr_symlink;
  186. }
  187. static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
  188. {
  189. if (nfsd4_has_session(cstate))
  190. return;
  191. fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
  192. &resfh->fh_handle);
  193. }
  194. static __be32
  195. do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
  196. {
  197. struct svc_fh *current_fh = &cstate->current_fh;
  198. int accmode;
  199. __be32 status;
  200. *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  201. if (!*resfh)
  202. return nfserr_jukebox;
  203. fh_init(*resfh, NFS4_FHSIZE);
  204. open->op_truncate = 0;
  205. if (open->op_create) {
  206. /* FIXME: check session persistence and pnfs flags.
  207. * The nfsv4.1 spec requires the following semantics:
  208. *
  209. * Persistent | pNFS | Server REQUIRED | Client Allowed
  210. * Reply Cache | server | |
  211. * -------------+--------+-----------------+--------------------
  212. * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1
  213. * | | | (SHOULD)
  214. * | | and EXCLUSIVE4 | or EXCLUSIVE4
  215. * | | | (SHOULD NOT)
  216. * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1
  217. * yes | no | GUARDED4 | GUARDED4
  218. * yes | yes | GUARDED4 | GUARDED4
  219. */
  220. /*
  221. * Note: create modes (UNCHECKED,GUARDED...) are the same
  222. * in NFSv4 as in v3 except EXCLUSIVE4_1.
  223. */
  224. status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
  225. open->op_fname.len, &open->op_iattr,
  226. *resfh, open->op_createmode,
  227. (u32 *)open->op_verf.data,
  228. &open->op_truncate, &open->op_created);
  229. if (!status && open->op_label.len)
  230. nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
  231. /*
  232. * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
  233. * use the returned bitmask to indicate which attributes
  234. * we used to store the verifier:
  235. */
  236. if (nfsd_create_is_exclusive(open->op_createmode) && status == 0)
  237. open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
  238. FATTR4_WORD1_TIME_MODIFY);
  239. } else
  240. /*
  241. * Note this may exit with the parent still locked.
  242. * We will hold the lock until nfsd4_open's final
  243. * lookup, to prevent renames or unlinks until we've had
  244. * a chance to an acquire a delegation if appropriate.
  245. */
  246. status = nfsd_lookup(rqstp, current_fh,
  247. open->op_fname.data, open->op_fname.len, *resfh);
  248. if (status)
  249. goto out;
  250. status = nfsd_check_obj_isreg(*resfh);
  251. if (status)
  252. goto out;
  253. if (is_create_with_attrs(open) && open->op_acl != NULL)
  254. do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
  255. nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
  256. accmode = NFSD_MAY_NOP;
  257. if (open->op_created ||
  258. open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
  259. accmode |= NFSD_MAY_OWNER_OVERRIDE;
  260. status = do_open_permission(rqstp, *resfh, open, accmode);
  261. set_change_info(&open->op_cinfo, current_fh);
  262. out:
  263. return status;
  264. }
  265. static __be32
  266. do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  267. {
  268. struct svc_fh *current_fh = &cstate->current_fh;
  269. __be32 status;
  270. int accmode = 0;
  271. /* We don't know the target directory, and therefore can not
  272. * set the change info
  273. */
  274. memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
  275. nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
  276. open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
  277. (open->op_iattr.ia_size == 0);
  278. /*
  279. * In the delegation case, the client is telling us about an
  280. * open that it *already* performed locally, some time ago. We
  281. * should let it succeed now if possible.
  282. *
  283. * In the case of a CLAIM_FH open, on the other hand, the client
  284. * may be counting on us to enforce permissions (the Linux 4.1
  285. * client uses this for normal opens, for example).
  286. */
  287. if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
  288. accmode = NFSD_MAY_OWNER_OVERRIDE;
  289. status = do_open_permission(rqstp, current_fh, open, accmode);
  290. return status;
  291. }
  292. static void
  293. copy_clientid(clientid_t *clid, struct nfsd4_session *session)
  294. {
  295. struct nfsd4_sessionid *sid =
  296. (struct nfsd4_sessionid *)session->se_sessionid.data;
  297. clid->cl_boot = sid->clientid.cl_boot;
  298. clid->cl_id = sid->clientid.cl_id;
  299. }
  300. static __be32
  301. nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  302. union nfsd4_op_u *u)
  303. {
  304. struct nfsd4_open *open = &u->open;
  305. __be32 status;
  306. struct svc_fh *resfh = NULL;
  307. struct net *net = SVC_NET(rqstp);
  308. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  309. dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
  310. (int)open->op_fname.len, open->op_fname.data,
  311. open->op_openowner);
  312. /* This check required by spec. */
  313. if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
  314. return nfserr_inval;
  315. open->op_created = 0;
  316. /*
  317. * RFC5661 18.51.3
  318. * Before RECLAIM_COMPLETE done, server should deny new lock
  319. */
  320. if (nfsd4_has_session(cstate) &&
  321. !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  322. &cstate->session->se_client->cl_flags) &&
  323. open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  324. return nfserr_grace;
  325. if (nfsd4_has_session(cstate))
  326. copy_clientid(&open->op_clientid, cstate->session);
  327. /* check seqid for replay. set nfs4_owner */
  328. status = nfsd4_process_open1(cstate, open, nn);
  329. if (status == nfserr_replay_me) {
  330. struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
  331. fh_put(&cstate->current_fh);
  332. fh_copy_shallow(&cstate->current_fh.fh_handle,
  333. &rp->rp_openfh);
  334. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  335. if (status)
  336. dprintk("nfsd4_open: replay failed"
  337. " restoring previous filehandle\n");
  338. else
  339. status = nfserr_replay_me;
  340. }
  341. if (status)
  342. goto out;
  343. if (open->op_xdr_error) {
  344. status = open->op_xdr_error;
  345. goto out;
  346. }
  347. status = nfsd4_check_open_attributes(rqstp, cstate, open);
  348. if (status)
  349. goto out;
  350. /* Openowner is now set, so sequence id will get bumped. Now we need
  351. * these checks before we do any creates: */
  352. status = nfserr_grace;
  353. if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  354. goto out;
  355. status = nfserr_no_grace;
  356. if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  357. goto out;
  358. switch (open->op_claim_type) {
  359. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  360. case NFS4_OPEN_CLAIM_NULL:
  361. status = do_open_lookup(rqstp, cstate, open, &resfh);
  362. if (status)
  363. goto out;
  364. break;
  365. case NFS4_OPEN_CLAIM_PREVIOUS:
  366. status = nfs4_check_open_reclaim(&open->op_clientid,
  367. cstate, nn);
  368. if (status)
  369. goto out;
  370. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  371. case NFS4_OPEN_CLAIM_FH:
  372. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  373. status = do_open_fhandle(rqstp, cstate, open);
  374. if (status)
  375. goto out;
  376. resfh = &cstate->current_fh;
  377. break;
  378. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  379. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  380. dprintk("NFSD: unsupported OPEN claim type %d\n",
  381. open->op_claim_type);
  382. status = nfserr_notsupp;
  383. goto out;
  384. default:
  385. dprintk("NFSD: Invalid OPEN claim type %d\n",
  386. open->op_claim_type);
  387. status = nfserr_inval;
  388. goto out;
  389. }
  390. /*
  391. * nfsd4_process_open2() does the actual opening of the file. If
  392. * successful, it (1) truncates the file if open->op_truncate was
  393. * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
  394. */
  395. status = nfsd4_process_open2(rqstp, resfh, open);
  396. WARN(status && open->op_created,
  397. "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
  398. be32_to_cpu(status));
  399. out:
  400. if (resfh && resfh != &cstate->current_fh) {
  401. fh_dup2(&cstate->current_fh, resfh);
  402. fh_put(resfh);
  403. kfree(resfh);
  404. }
  405. nfsd4_cleanup_open_state(cstate, open);
  406. nfsd4_bump_seqid(cstate, status);
  407. return status;
  408. }
  409. /*
  410. * OPEN is the only seqid-mutating operation whose decoding can fail
  411. * with a seqid-mutating error (specifically, decoding of user names in
  412. * the attributes). Therefore we have to do some processing to look up
  413. * the stateowner so that we can bump the seqid.
  414. */
  415. static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
  416. {
  417. struct nfsd4_open *open = &op->u.open;
  418. if (!seqid_mutating_err(ntohl(op->status)))
  419. return op->status;
  420. if (nfsd4_has_session(cstate))
  421. return op->status;
  422. open->op_xdr_error = op->status;
  423. return nfsd4_open(rqstp, cstate, &op->u);
  424. }
  425. /*
  426. * filehandle-manipulating ops.
  427. */
  428. static __be32
  429. nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  430. union nfsd4_op_u *u)
  431. {
  432. u->getfh = &cstate->current_fh;
  433. return nfs_ok;
  434. }
  435. static __be32
  436. nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  437. union nfsd4_op_u *u)
  438. {
  439. struct nfsd4_putfh *putfh = &u->putfh;
  440. fh_put(&cstate->current_fh);
  441. cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
  442. memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
  443. putfh->pf_fhlen);
  444. return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
  445. }
  446. static __be32
  447. nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  448. union nfsd4_op_u *u)
  449. {
  450. __be32 status;
  451. fh_put(&cstate->current_fh);
  452. status = exp_pseudoroot(rqstp, &cstate->current_fh);
  453. return status;
  454. }
  455. static __be32
  456. nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  457. union nfsd4_op_u *u)
  458. {
  459. if (!cstate->save_fh.fh_dentry)
  460. return nfserr_restorefh;
  461. fh_dup2(&cstate->current_fh, &cstate->save_fh);
  462. if (HAS_STATE_ID(cstate, SAVED_STATE_ID_FLAG)) {
  463. memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
  464. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  465. }
  466. return nfs_ok;
  467. }
  468. static __be32
  469. nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  470. union nfsd4_op_u *u)
  471. {
  472. fh_dup2(&cstate->save_fh, &cstate->current_fh);
  473. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG)) {
  474. memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
  475. SET_STATE_ID(cstate, SAVED_STATE_ID_FLAG);
  476. }
  477. return nfs_ok;
  478. }
  479. /*
  480. * misc nfsv4 ops
  481. */
  482. static __be32
  483. nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  484. union nfsd4_op_u *u)
  485. {
  486. struct nfsd4_access *access = &u->access;
  487. if (access->ac_req_access & ~NFS3_ACCESS_FULL)
  488. return nfserr_inval;
  489. access->ac_resp_access = access->ac_req_access;
  490. return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
  491. &access->ac_supported);
  492. }
  493. static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
  494. {
  495. __be32 verf[2];
  496. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  497. /*
  498. * This is opaque to client, so no need to byte-swap. Use
  499. * __force to keep sparse happy. y2038 time_t overflow is
  500. * irrelevant in this usage.
  501. */
  502. verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
  503. verf[1] = (__force __be32)nn->nfssvc_boot.tv_nsec;
  504. memcpy(verifier->data, verf, sizeof(verifier->data));
  505. }
  506. static __be32
  507. nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  508. union nfsd4_op_u *u)
  509. {
  510. struct nfsd4_commit *commit = &u->commit;
  511. gen_boot_verifier(&commit->co_verf, SVC_NET(rqstp));
  512. return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
  513. commit->co_count);
  514. }
  515. static __be32
  516. nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  517. union nfsd4_op_u *u)
  518. {
  519. struct nfsd4_create *create = &u->create;
  520. struct svc_fh resfh;
  521. __be32 status;
  522. dev_t rdev;
  523. fh_init(&resfh, NFS4_FHSIZE);
  524. status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
  525. if (status)
  526. return status;
  527. status = check_attr_support(rqstp, cstate, create->cr_bmval,
  528. nfsd_attrmask);
  529. if (status)
  530. return status;
  531. switch (create->cr_type) {
  532. case NF4LNK:
  533. status = nfsd_symlink(rqstp, &cstate->current_fh,
  534. create->cr_name, create->cr_namelen,
  535. create->cr_data, &resfh);
  536. break;
  537. case NF4BLK:
  538. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  539. if (MAJOR(rdev) != create->cr_specdata1 ||
  540. MINOR(rdev) != create->cr_specdata2)
  541. return nfserr_inval;
  542. status = nfsd_create(rqstp, &cstate->current_fh,
  543. create->cr_name, create->cr_namelen,
  544. &create->cr_iattr, S_IFBLK, rdev, &resfh);
  545. break;
  546. case NF4CHR:
  547. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  548. if (MAJOR(rdev) != create->cr_specdata1 ||
  549. MINOR(rdev) != create->cr_specdata2)
  550. return nfserr_inval;
  551. status = nfsd_create(rqstp, &cstate->current_fh,
  552. create->cr_name, create->cr_namelen,
  553. &create->cr_iattr,S_IFCHR, rdev, &resfh);
  554. break;
  555. case NF4SOCK:
  556. status = nfsd_create(rqstp, &cstate->current_fh,
  557. create->cr_name, create->cr_namelen,
  558. &create->cr_iattr, S_IFSOCK, 0, &resfh);
  559. break;
  560. case NF4FIFO:
  561. status = nfsd_create(rqstp, &cstate->current_fh,
  562. create->cr_name, create->cr_namelen,
  563. &create->cr_iattr, S_IFIFO, 0, &resfh);
  564. break;
  565. case NF4DIR:
  566. create->cr_iattr.ia_valid &= ~ATTR_SIZE;
  567. status = nfsd_create(rqstp, &cstate->current_fh,
  568. create->cr_name, create->cr_namelen,
  569. &create->cr_iattr, S_IFDIR, 0, &resfh);
  570. break;
  571. default:
  572. status = nfserr_badtype;
  573. }
  574. if (status)
  575. goto out;
  576. if (create->cr_label.len)
  577. nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
  578. if (create->cr_acl != NULL)
  579. do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
  580. create->cr_bmval);
  581. fh_unlock(&cstate->current_fh);
  582. set_change_info(&create->cr_cinfo, &cstate->current_fh);
  583. fh_dup2(&cstate->current_fh, &resfh);
  584. out:
  585. fh_put(&resfh);
  586. return status;
  587. }
  588. static __be32
  589. nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  590. union nfsd4_op_u *u)
  591. {
  592. struct nfsd4_getattr *getattr = &u->getattr;
  593. __be32 status;
  594. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  595. if (status)
  596. return status;
  597. if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  598. return nfserr_inval;
  599. getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
  600. getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
  601. getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
  602. getattr->ga_fhp = &cstate->current_fh;
  603. return nfs_ok;
  604. }
  605. static __be32
  606. nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  607. union nfsd4_op_u *u)
  608. {
  609. struct nfsd4_link *link = &u->link;
  610. __be32 status;
  611. status = nfsd_link(rqstp, &cstate->current_fh,
  612. link->li_name, link->li_namelen, &cstate->save_fh);
  613. if (!status)
  614. set_change_info(&link->li_cinfo, &cstate->current_fh);
  615. return status;
  616. }
  617. static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
  618. {
  619. struct svc_fh tmp_fh;
  620. __be32 ret;
  621. fh_init(&tmp_fh, NFS4_FHSIZE);
  622. ret = exp_pseudoroot(rqstp, &tmp_fh);
  623. if (ret)
  624. return ret;
  625. if (tmp_fh.fh_dentry == fh->fh_dentry) {
  626. fh_put(&tmp_fh);
  627. return nfserr_noent;
  628. }
  629. fh_put(&tmp_fh);
  630. return nfsd_lookup(rqstp, fh, "..", 2, fh);
  631. }
  632. static __be32
  633. nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  634. union nfsd4_op_u *u)
  635. {
  636. return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  637. }
  638. static __be32
  639. nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  640. union nfsd4_op_u *u)
  641. {
  642. return nfsd_lookup(rqstp, &cstate->current_fh,
  643. u->lookup.lo_name, u->lookup.lo_len,
  644. &cstate->current_fh);
  645. }
  646. static __be32
  647. nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  648. union nfsd4_op_u *u)
  649. {
  650. struct nfsd4_read *read = &u->read;
  651. __be32 status;
  652. read->rd_filp = NULL;
  653. if (read->rd_offset >= OFFSET_MAX)
  654. return nfserr_inval;
  655. /*
  656. * If we do a zero copy read, then a client will see read data
  657. * that reflects the state of the file *after* performing the
  658. * following compound.
  659. *
  660. * To ensure proper ordering, we therefore turn off zero copy if
  661. * the client wants us to do more in this compound:
  662. */
  663. if (!nfsd4_last_compound_op(rqstp))
  664. clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
  665. /* check stateid */
  666. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  667. &read->rd_stateid, RD_STATE,
  668. &read->rd_filp, &read->rd_tmp_file);
  669. if (status) {
  670. dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
  671. goto out;
  672. }
  673. status = nfs_ok;
  674. out:
  675. read->rd_rqstp = rqstp;
  676. read->rd_fhp = &cstate->current_fh;
  677. return status;
  678. }
  679. static void
  680. nfsd4_read_release(union nfsd4_op_u *u)
  681. {
  682. if (u->read.rd_filp)
  683. fput(u->read.rd_filp);
  684. }
  685. static __be32
  686. nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  687. union nfsd4_op_u *u)
  688. {
  689. struct nfsd4_readdir *readdir = &u->readdir;
  690. u64 cookie = readdir->rd_cookie;
  691. static const nfs4_verifier zeroverf;
  692. /* no need to check permission - this will be done in nfsd_readdir() */
  693. if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  694. return nfserr_inval;
  695. readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
  696. readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
  697. readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
  698. if ((cookie == 1) || (cookie == 2) ||
  699. (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
  700. return nfserr_bad_cookie;
  701. readdir->rd_rqstp = rqstp;
  702. readdir->rd_fhp = &cstate->current_fh;
  703. return nfs_ok;
  704. }
  705. static __be32
  706. nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  707. union nfsd4_op_u *u)
  708. {
  709. u->readlink.rl_rqstp = rqstp;
  710. u->readlink.rl_fhp = &cstate->current_fh;
  711. return nfs_ok;
  712. }
  713. static __be32
  714. nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  715. union nfsd4_op_u *u)
  716. {
  717. struct nfsd4_remove *remove = &u->remove;
  718. __be32 status;
  719. if (opens_in_grace(SVC_NET(rqstp)))
  720. return nfserr_grace;
  721. status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
  722. remove->rm_name, remove->rm_namelen);
  723. if (!status) {
  724. fh_unlock(&cstate->current_fh);
  725. set_change_info(&remove->rm_cinfo, &cstate->current_fh);
  726. }
  727. return status;
  728. }
  729. static __be32
  730. nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  731. union nfsd4_op_u *u)
  732. {
  733. struct nfsd4_rename *rename = &u->rename;
  734. __be32 status;
  735. if (opens_in_grace(SVC_NET(rqstp)) &&
  736. !(cstate->save_fh.fh_export->ex_flags & NFSEXP_NOSUBTREECHECK))
  737. return nfserr_grace;
  738. status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
  739. rename->rn_snamelen, &cstate->current_fh,
  740. rename->rn_tname, rename->rn_tnamelen);
  741. if (status)
  742. return status;
  743. set_change_info(&rename->rn_sinfo, &cstate->current_fh);
  744. set_change_info(&rename->rn_tinfo, &cstate->save_fh);
  745. return nfs_ok;
  746. }
  747. static __be32
  748. nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  749. union nfsd4_op_u *u)
  750. {
  751. struct nfsd4_secinfo *secinfo = &u->secinfo;
  752. struct svc_export *exp;
  753. struct dentry *dentry;
  754. __be32 err;
  755. err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
  756. if (err)
  757. return err;
  758. err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
  759. secinfo->si_name, secinfo->si_namelen,
  760. &exp, &dentry);
  761. if (err)
  762. return err;
  763. fh_unlock(&cstate->current_fh);
  764. if (d_really_is_negative(dentry)) {
  765. exp_put(exp);
  766. err = nfserr_noent;
  767. } else
  768. secinfo->si_exp = exp;
  769. dput(dentry);
  770. if (cstate->minorversion)
  771. /* See rfc 5661 section 2.6.3.1.1.8 */
  772. fh_put(&cstate->current_fh);
  773. return err;
  774. }
  775. static __be32
  776. nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  777. union nfsd4_op_u *u)
  778. {
  779. __be32 err;
  780. switch (u->secinfo_no_name.sin_style) {
  781. case NFS4_SECINFO_STYLE4_CURRENT_FH:
  782. break;
  783. case NFS4_SECINFO_STYLE4_PARENT:
  784. err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  785. if (err)
  786. return err;
  787. break;
  788. default:
  789. return nfserr_inval;
  790. }
  791. u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
  792. fh_put(&cstate->current_fh);
  793. return nfs_ok;
  794. }
  795. static void
  796. nfsd4_secinfo_release(union nfsd4_op_u *u)
  797. {
  798. if (u->secinfo.si_exp)
  799. exp_put(u->secinfo.si_exp);
  800. }
  801. static void
  802. nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
  803. {
  804. if (u->secinfo_no_name.sin_exp)
  805. exp_put(u->secinfo_no_name.sin_exp);
  806. }
  807. static __be32
  808. nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  809. union nfsd4_op_u *u)
  810. {
  811. struct nfsd4_setattr *setattr = &u->setattr;
  812. __be32 status = nfs_ok;
  813. int err;
  814. if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
  815. status = nfs4_preprocess_stateid_op(rqstp, cstate,
  816. &cstate->current_fh, &setattr->sa_stateid,
  817. WR_STATE, NULL, NULL);
  818. if (status) {
  819. dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
  820. return status;
  821. }
  822. }
  823. err = fh_want_write(&cstate->current_fh);
  824. if (err)
  825. return nfserrno(err);
  826. status = nfs_ok;
  827. status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
  828. nfsd_attrmask);
  829. if (status)
  830. goto out;
  831. if (setattr->sa_acl != NULL)
  832. status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
  833. setattr->sa_acl);
  834. if (status)
  835. goto out;
  836. if (setattr->sa_label.len)
  837. status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
  838. &setattr->sa_label);
  839. if (status)
  840. goto out;
  841. status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
  842. 0, (time_t)0);
  843. out:
  844. fh_drop_write(&cstate->current_fh);
  845. return status;
  846. }
  847. static int fill_in_write_vector(struct kvec *vec, struct nfsd4_write *write)
  848. {
  849. int i = 1;
  850. int buflen = write->wr_buflen;
  851. vec[0].iov_base = write->wr_head.iov_base;
  852. vec[0].iov_len = min_t(int, buflen, write->wr_head.iov_len);
  853. buflen -= vec[0].iov_len;
  854. while (buflen) {
  855. vec[i].iov_base = page_address(write->wr_pagelist[i - 1]);
  856. vec[i].iov_len = min_t(int, PAGE_SIZE, buflen);
  857. buflen -= vec[i].iov_len;
  858. i++;
  859. }
  860. return i;
  861. }
  862. static __be32
  863. nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  864. union nfsd4_op_u *u)
  865. {
  866. struct nfsd4_write *write = &u->write;
  867. stateid_t *stateid = &write->wr_stateid;
  868. struct file *filp = NULL;
  869. __be32 status = nfs_ok;
  870. unsigned long cnt;
  871. int nvecs;
  872. if (write->wr_offset >= OFFSET_MAX)
  873. return nfserr_inval;
  874. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  875. stateid, WR_STATE, &filp, NULL);
  876. if (status) {
  877. dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
  878. return status;
  879. }
  880. cnt = write->wr_buflen;
  881. write->wr_how_written = write->wr_stable_how;
  882. gen_boot_verifier(&write->wr_verifier, SVC_NET(rqstp));
  883. nvecs = fill_in_write_vector(rqstp->rq_vec, write);
  884. WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
  885. status = nfsd_vfs_write(rqstp, &cstate->current_fh, filp,
  886. write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
  887. write->wr_how_written);
  888. fput(filp);
  889. write->wr_bytes_written = cnt;
  890. return status;
  891. }
  892. static __be32
  893. nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  894. stateid_t *src_stateid, struct file **src,
  895. stateid_t *dst_stateid, struct file **dst)
  896. {
  897. __be32 status;
  898. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
  899. src_stateid, RD_STATE, src, NULL);
  900. if (status) {
  901. dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
  902. goto out;
  903. }
  904. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  905. dst_stateid, WR_STATE, dst, NULL);
  906. if (status) {
  907. dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
  908. goto out_put_src;
  909. }
  910. /* fix up for NFS-specific error code */
  911. if (!S_ISREG(file_inode(*src)->i_mode) ||
  912. !S_ISREG(file_inode(*dst)->i_mode)) {
  913. status = nfserr_wrong_type;
  914. goto out_put_dst;
  915. }
  916. out:
  917. return status;
  918. out_put_dst:
  919. fput(*dst);
  920. out_put_src:
  921. fput(*src);
  922. goto out;
  923. }
  924. static __be32
  925. nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  926. union nfsd4_op_u *u)
  927. {
  928. struct nfsd4_clone *clone = &u->clone;
  929. struct file *src, *dst;
  930. __be32 status;
  931. status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
  932. &clone->cl_dst_stateid, &dst);
  933. if (status)
  934. goto out;
  935. status = nfsd4_clone_file_range(src, clone->cl_src_pos,
  936. dst, clone->cl_dst_pos, clone->cl_count);
  937. fput(dst);
  938. fput(src);
  939. out:
  940. return status;
  941. }
  942. static __be32
  943. nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  944. union nfsd4_op_u *u)
  945. {
  946. struct nfsd4_copy *copy = &u->copy;
  947. struct file *src, *dst;
  948. __be32 status;
  949. ssize_t bytes;
  950. status = nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid, &src,
  951. &copy->cp_dst_stateid, &dst);
  952. if (status)
  953. goto out;
  954. bytes = nfsd_copy_file_range(src, copy->cp_src_pos,
  955. dst, copy->cp_dst_pos, copy->cp_count);
  956. if (bytes < 0)
  957. status = nfserrno(bytes);
  958. else {
  959. copy->cp_res.wr_bytes_written = bytes;
  960. copy->cp_res.wr_stable_how = NFS_UNSTABLE;
  961. copy->cp_synchronous = 1;
  962. gen_boot_verifier(&copy->cp_res.wr_verifier, SVC_NET(rqstp));
  963. status = nfs_ok;
  964. }
  965. fput(src);
  966. fput(dst);
  967. out:
  968. return status;
  969. }
  970. static __be32
  971. nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  972. struct nfsd4_fallocate *fallocate, int flags)
  973. {
  974. __be32 status = nfserr_notsupp;
  975. struct file *file;
  976. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  977. &fallocate->falloc_stateid,
  978. WR_STATE, &file, NULL);
  979. if (status != nfs_ok) {
  980. dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
  981. return status;
  982. }
  983. status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, file,
  984. fallocate->falloc_offset,
  985. fallocate->falloc_length,
  986. flags);
  987. fput(file);
  988. return status;
  989. }
  990. static __be32
  991. nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  992. union nfsd4_op_u *u)
  993. {
  994. return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
  995. }
  996. static __be32
  997. nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  998. union nfsd4_op_u *u)
  999. {
  1000. return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
  1001. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
  1002. }
  1003. static __be32
  1004. nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1005. union nfsd4_op_u *u)
  1006. {
  1007. struct nfsd4_seek *seek = &u->seek;
  1008. int whence;
  1009. __be32 status;
  1010. struct file *file;
  1011. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  1012. &seek->seek_stateid,
  1013. RD_STATE, &file, NULL);
  1014. if (status) {
  1015. dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
  1016. return status;
  1017. }
  1018. switch (seek->seek_whence) {
  1019. case NFS4_CONTENT_DATA:
  1020. whence = SEEK_DATA;
  1021. break;
  1022. case NFS4_CONTENT_HOLE:
  1023. whence = SEEK_HOLE;
  1024. break;
  1025. default:
  1026. status = nfserr_union_notsupp;
  1027. goto out;
  1028. }
  1029. /*
  1030. * Note: This call does change file->f_pos, but nothing in NFSD
  1031. * should ever file->f_pos.
  1032. */
  1033. seek->seek_pos = vfs_llseek(file, seek->seek_offset, whence);
  1034. if (seek->seek_pos < 0)
  1035. status = nfserrno(seek->seek_pos);
  1036. else if (seek->seek_pos >= i_size_read(file_inode(file)))
  1037. seek->seek_eof = true;
  1038. out:
  1039. fput(file);
  1040. return status;
  1041. }
  1042. /* This routine never returns NFS_OK! If there are no other errors, it
  1043. * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
  1044. * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
  1045. * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
  1046. */
  1047. static __be32
  1048. _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1049. struct nfsd4_verify *verify)
  1050. {
  1051. __be32 *buf, *p;
  1052. int count;
  1053. __be32 status;
  1054. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  1055. if (status)
  1056. return status;
  1057. status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
  1058. if (status)
  1059. return status;
  1060. if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
  1061. || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
  1062. return nfserr_inval;
  1063. if (verify->ve_attrlen & 3)
  1064. return nfserr_inval;
  1065. /* count in words:
  1066. * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
  1067. */
  1068. count = 4 + (verify->ve_attrlen >> 2);
  1069. buf = kmalloc(count << 2, GFP_KERNEL);
  1070. if (!buf)
  1071. return nfserr_jukebox;
  1072. p = buf;
  1073. status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
  1074. cstate->current_fh.fh_export,
  1075. cstate->current_fh.fh_dentry,
  1076. verify->ve_bmval,
  1077. rqstp, 0);
  1078. /*
  1079. * If nfsd4_encode_fattr() ran out of space, assume that's because
  1080. * the attributes are longer (hence different) than those given:
  1081. */
  1082. if (status == nfserr_resource)
  1083. status = nfserr_not_same;
  1084. if (status)
  1085. goto out_kfree;
  1086. /* skip bitmap */
  1087. p = buf + 1 + ntohl(buf[0]);
  1088. status = nfserr_not_same;
  1089. if (ntohl(*p++) != verify->ve_attrlen)
  1090. goto out_kfree;
  1091. if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
  1092. status = nfserr_same;
  1093. out_kfree:
  1094. kfree(buf);
  1095. return status;
  1096. }
  1097. static __be32
  1098. nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1099. union nfsd4_op_u *u)
  1100. {
  1101. __be32 status;
  1102. status = _nfsd4_verify(rqstp, cstate, &u->verify);
  1103. return status == nfserr_not_same ? nfs_ok : status;
  1104. }
  1105. static __be32
  1106. nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1107. union nfsd4_op_u *u)
  1108. {
  1109. __be32 status;
  1110. status = _nfsd4_verify(rqstp, cstate, &u->nverify);
  1111. return status == nfserr_same ? nfs_ok : status;
  1112. }
  1113. #ifdef CONFIG_NFSD_PNFS
  1114. static const struct nfsd4_layout_ops *
  1115. nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
  1116. {
  1117. if (!exp->ex_layout_types) {
  1118. dprintk("%s: export does not support pNFS\n", __func__);
  1119. return NULL;
  1120. }
  1121. if (layout_type >= LAYOUT_TYPE_MAX ||
  1122. !(exp->ex_layout_types & (1 << layout_type))) {
  1123. dprintk("%s: layout type %d not supported\n",
  1124. __func__, layout_type);
  1125. return NULL;
  1126. }
  1127. return nfsd4_layout_ops[layout_type];
  1128. }
  1129. static __be32
  1130. nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
  1131. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1132. {
  1133. struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
  1134. const struct nfsd4_layout_ops *ops;
  1135. struct nfsd4_deviceid_map *map;
  1136. struct svc_export *exp;
  1137. __be32 nfserr;
  1138. dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
  1139. __func__,
  1140. gdp->gd_layout_type,
  1141. gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
  1142. gdp->gd_maxcount);
  1143. map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
  1144. if (!map) {
  1145. dprintk("%s: couldn't find device ID to export mapping!\n",
  1146. __func__);
  1147. return nfserr_noent;
  1148. }
  1149. exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
  1150. if (IS_ERR(exp)) {
  1151. dprintk("%s: could not find device id\n", __func__);
  1152. return nfserr_noent;
  1153. }
  1154. nfserr = nfserr_layoutunavailable;
  1155. ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
  1156. if (!ops)
  1157. goto out;
  1158. nfserr = nfs_ok;
  1159. if (gdp->gd_maxcount != 0) {
  1160. nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
  1161. rqstp, cstate->session->se_client, gdp);
  1162. }
  1163. gdp->gd_notify_types &= ops->notify_types;
  1164. out:
  1165. exp_put(exp);
  1166. return nfserr;
  1167. }
  1168. static void
  1169. nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
  1170. {
  1171. kfree(u->getdeviceinfo.gd_device);
  1172. }
  1173. static __be32
  1174. nfsd4_layoutget(struct svc_rqst *rqstp,
  1175. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1176. {
  1177. struct nfsd4_layoutget *lgp = &u->layoutget;
  1178. struct svc_fh *current_fh = &cstate->current_fh;
  1179. const struct nfsd4_layout_ops *ops;
  1180. struct nfs4_layout_stateid *ls;
  1181. __be32 nfserr;
  1182. int accmode = NFSD_MAY_READ_IF_EXEC;
  1183. switch (lgp->lg_seg.iomode) {
  1184. case IOMODE_READ:
  1185. accmode |= NFSD_MAY_READ;
  1186. break;
  1187. case IOMODE_RW:
  1188. accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
  1189. break;
  1190. default:
  1191. dprintk("%s: invalid iomode %d\n",
  1192. __func__, lgp->lg_seg.iomode);
  1193. nfserr = nfserr_badiomode;
  1194. goto out;
  1195. }
  1196. nfserr = fh_verify(rqstp, current_fh, 0, accmode);
  1197. if (nfserr)
  1198. goto out;
  1199. nfserr = nfserr_layoutunavailable;
  1200. ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
  1201. if (!ops)
  1202. goto out;
  1203. /*
  1204. * Verify minlength and range as per RFC5661:
  1205. * o If loga_length is less than loga_minlength,
  1206. * the metadata server MUST return NFS4ERR_INVAL.
  1207. * o If the sum of loga_offset and loga_minlength exceeds
  1208. * NFS4_UINT64_MAX, and loga_minlength is not
  1209. * NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
  1210. * o If the sum of loga_offset and loga_length exceeds
  1211. * NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
  1212. * the error NFS4ERR_INVAL MUST result.
  1213. */
  1214. nfserr = nfserr_inval;
  1215. if (lgp->lg_seg.length < lgp->lg_minlength ||
  1216. (lgp->lg_minlength != NFS4_MAX_UINT64 &&
  1217. lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
  1218. (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
  1219. lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
  1220. goto out;
  1221. if (lgp->lg_seg.length == 0)
  1222. goto out;
  1223. nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
  1224. true, lgp->lg_layout_type, &ls);
  1225. if (nfserr) {
  1226. trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
  1227. goto out;
  1228. }
  1229. nfserr = nfserr_recallconflict;
  1230. if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
  1231. goto out_put_stid;
  1232. nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
  1233. current_fh, lgp);
  1234. if (nfserr)
  1235. goto out_put_stid;
  1236. nfserr = nfsd4_insert_layout(lgp, ls);
  1237. out_put_stid:
  1238. mutex_unlock(&ls->ls_mutex);
  1239. nfs4_put_stid(&ls->ls_stid);
  1240. out:
  1241. return nfserr;
  1242. }
  1243. static void
  1244. nfsd4_layoutget_release(union nfsd4_op_u *u)
  1245. {
  1246. kfree(u->layoutget.lg_content);
  1247. }
  1248. static __be32
  1249. nfsd4_layoutcommit(struct svc_rqst *rqstp,
  1250. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1251. {
  1252. struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
  1253. const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
  1254. struct svc_fh *current_fh = &cstate->current_fh;
  1255. const struct nfsd4_layout_ops *ops;
  1256. loff_t new_size = lcp->lc_last_wr + 1;
  1257. struct inode *inode;
  1258. struct nfs4_layout_stateid *ls;
  1259. __be32 nfserr;
  1260. nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
  1261. if (nfserr)
  1262. goto out;
  1263. nfserr = nfserr_layoutunavailable;
  1264. ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
  1265. if (!ops)
  1266. goto out;
  1267. inode = d_inode(current_fh->fh_dentry);
  1268. nfserr = nfserr_inval;
  1269. if (new_size <= seg->offset) {
  1270. dprintk("pnfsd: last write before layout segment\n");
  1271. goto out;
  1272. }
  1273. if (new_size > seg->offset + seg->length) {
  1274. dprintk("pnfsd: last write beyond layout segment\n");
  1275. goto out;
  1276. }
  1277. if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
  1278. dprintk("pnfsd: layoutcommit beyond EOF\n");
  1279. goto out;
  1280. }
  1281. nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
  1282. false, lcp->lc_layout_type,
  1283. &ls);
  1284. if (nfserr) {
  1285. trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
  1286. /* fixup error code as per RFC5661 */
  1287. if (nfserr == nfserr_bad_stateid)
  1288. nfserr = nfserr_badlayout;
  1289. goto out;
  1290. }
  1291. /* LAYOUTCOMMIT does not require any serialization */
  1292. mutex_unlock(&ls->ls_mutex);
  1293. if (new_size > i_size_read(inode)) {
  1294. lcp->lc_size_chg = 1;
  1295. lcp->lc_newsize = new_size;
  1296. } else {
  1297. lcp->lc_size_chg = 0;
  1298. }
  1299. nfserr = ops->proc_layoutcommit(inode, lcp);
  1300. nfs4_put_stid(&ls->ls_stid);
  1301. out:
  1302. return nfserr;
  1303. }
  1304. static __be32
  1305. nfsd4_layoutreturn(struct svc_rqst *rqstp,
  1306. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1307. {
  1308. struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
  1309. struct svc_fh *current_fh = &cstate->current_fh;
  1310. __be32 nfserr;
  1311. nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
  1312. if (nfserr)
  1313. goto out;
  1314. nfserr = nfserr_layoutunavailable;
  1315. if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
  1316. goto out;
  1317. switch (lrp->lr_seg.iomode) {
  1318. case IOMODE_READ:
  1319. case IOMODE_RW:
  1320. case IOMODE_ANY:
  1321. break;
  1322. default:
  1323. dprintk("%s: invalid iomode %d\n", __func__,
  1324. lrp->lr_seg.iomode);
  1325. nfserr = nfserr_inval;
  1326. goto out;
  1327. }
  1328. switch (lrp->lr_return_type) {
  1329. case RETURN_FILE:
  1330. nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
  1331. break;
  1332. case RETURN_FSID:
  1333. case RETURN_ALL:
  1334. nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
  1335. break;
  1336. default:
  1337. dprintk("%s: invalid return_type %d\n", __func__,
  1338. lrp->lr_return_type);
  1339. nfserr = nfserr_inval;
  1340. break;
  1341. }
  1342. out:
  1343. return nfserr;
  1344. }
  1345. #endif /* CONFIG_NFSD_PNFS */
  1346. /*
  1347. * NULL call.
  1348. */
  1349. static __be32
  1350. nfsd4_proc_null(struct svc_rqst *rqstp)
  1351. {
  1352. return nfs_ok;
  1353. }
  1354. static inline void nfsd4_increment_op_stats(u32 opnum)
  1355. {
  1356. if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
  1357. nfsdstats.nfs4_opcount[opnum]++;
  1358. }
  1359. static const struct nfsd4_operation nfsd4_ops[];
  1360. static const char *nfsd4_op_name(unsigned opnum);
  1361. /*
  1362. * Enforce NFSv4.1 COMPOUND ordering rules:
  1363. *
  1364. * Also note, enforced elsewhere:
  1365. * - SEQUENCE other than as first op results in
  1366. * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
  1367. * - BIND_CONN_TO_SESSION must be the only op in its compound.
  1368. * (Enforced in nfsd4_bind_conn_to_session().)
  1369. * - DESTROY_SESSION must be the final operation in a compound, if
  1370. * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
  1371. * (Enforced in nfsd4_destroy_session().)
  1372. */
  1373. static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
  1374. {
  1375. struct nfsd4_op *op = &args->ops[0];
  1376. /* These ordering requirements don't apply to NFSv4.0: */
  1377. if (args->minorversion == 0)
  1378. return nfs_ok;
  1379. /* This is weird, but OK, not our problem: */
  1380. if (args->opcnt == 0)
  1381. return nfs_ok;
  1382. if (op->status == nfserr_op_illegal)
  1383. return nfs_ok;
  1384. if (!(nfsd4_ops[op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
  1385. return nfserr_op_not_in_session;
  1386. if (op->opnum == OP_SEQUENCE)
  1387. return nfs_ok;
  1388. if (args->opcnt != 1)
  1389. return nfserr_not_only_op;
  1390. return nfs_ok;
  1391. }
  1392. const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
  1393. {
  1394. return &nfsd4_ops[op->opnum];
  1395. }
  1396. bool nfsd4_cache_this_op(struct nfsd4_op *op)
  1397. {
  1398. if (op->opnum == OP_ILLEGAL)
  1399. return false;
  1400. return OPDESC(op)->op_flags & OP_CACHEME;
  1401. }
  1402. static bool need_wrongsec_check(struct svc_rqst *rqstp)
  1403. {
  1404. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1405. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  1406. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  1407. struct nfsd4_op *next = &argp->ops[resp->opcnt];
  1408. const struct nfsd4_operation *thisd = OPDESC(this);
  1409. const struct nfsd4_operation *nextd;
  1410. /*
  1411. * Most ops check wronsec on our own; only the putfh-like ops
  1412. * have special rules.
  1413. */
  1414. if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
  1415. return false;
  1416. /*
  1417. * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
  1418. * put-filehandle operation if we're not going to use the
  1419. * result:
  1420. */
  1421. if (argp->opcnt == resp->opcnt)
  1422. return false;
  1423. if (next->opnum == OP_ILLEGAL)
  1424. return false;
  1425. nextd = OPDESC(next);
  1426. /*
  1427. * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
  1428. * errors themselves as necessary; others should check for them
  1429. * now:
  1430. */
  1431. return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
  1432. }
  1433. static void svcxdr_init_encode(struct svc_rqst *rqstp,
  1434. struct nfsd4_compoundres *resp)
  1435. {
  1436. struct xdr_stream *xdr = &resp->xdr;
  1437. struct xdr_buf *buf = &rqstp->rq_res;
  1438. struct kvec *head = buf->head;
  1439. xdr->buf = buf;
  1440. xdr->iov = head;
  1441. xdr->p = head->iov_base + head->iov_len;
  1442. xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
  1443. /* Tail and page_len should be zero at this point: */
  1444. buf->len = buf->head[0].iov_len;
  1445. xdr->scratch.iov_len = 0;
  1446. xdr->page_ptr = buf->pages - 1;
  1447. buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
  1448. - rqstp->rq_auth_slack;
  1449. }
  1450. /*
  1451. * COMPOUND call.
  1452. */
  1453. static __be32
  1454. nfsd4_proc_compound(struct svc_rqst *rqstp)
  1455. {
  1456. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  1457. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1458. struct nfsd4_op *op;
  1459. struct nfsd4_compound_state *cstate = &resp->cstate;
  1460. struct svc_fh *current_fh = &cstate->current_fh;
  1461. struct svc_fh *save_fh = &cstate->save_fh;
  1462. __be32 status;
  1463. svcxdr_init_encode(rqstp, resp);
  1464. resp->tagp = resp->xdr.p;
  1465. /* reserve space for: taglen, tag, and opcnt */
  1466. xdr_reserve_space(&resp->xdr, 8 + args->taglen);
  1467. resp->taglen = args->taglen;
  1468. resp->tag = args->tag;
  1469. resp->rqstp = rqstp;
  1470. cstate->minorversion = args->minorversion;
  1471. fh_init(current_fh, NFS4_FHSIZE);
  1472. fh_init(save_fh, NFS4_FHSIZE);
  1473. /*
  1474. * Don't use the deferral mechanism for NFSv4; compounds make it
  1475. * too hard to avoid non-idempotency problems.
  1476. */
  1477. clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  1478. /*
  1479. * According to RFC3010, this takes precedence over all other errors.
  1480. */
  1481. status = nfserr_minor_vers_mismatch;
  1482. if (nfsd_minorversion(args->minorversion, NFSD_TEST) <= 0)
  1483. goto out;
  1484. status = nfserr_resource;
  1485. if (args->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
  1486. goto out;
  1487. status = nfs41_check_op_ordering(args);
  1488. if (status) {
  1489. op = &args->ops[0];
  1490. op->status = status;
  1491. goto encode_op;
  1492. }
  1493. while (!status && resp->opcnt < args->opcnt) {
  1494. op = &args->ops[resp->opcnt++];
  1495. dprintk("nfsv4 compound op #%d/%d: %d (%s)\n",
  1496. resp->opcnt, args->opcnt, op->opnum,
  1497. nfsd4_op_name(op->opnum));
  1498. /*
  1499. * The XDR decode routines may have pre-set op->status;
  1500. * for example, if there is a miscellaneous XDR error
  1501. * it will be set to nfserr_bad_xdr.
  1502. */
  1503. if (op->status) {
  1504. if (op->opnum == OP_OPEN)
  1505. op->status = nfsd4_open_omfg(rqstp, cstate, op);
  1506. goto encode_op;
  1507. }
  1508. if (!current_fh->fh_dentry) {
  1509. if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
  1510. op->status = nfserr_nofilehandle;
  1511. goto encode_op;
  1512. }
  1513. } else if (current_fh->fh_export->ex_fslocs.migrated &&
  1514. !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
  1515. op->status = nfserr_moved;
  1516. goto encode_op;
  1517. }
  1518. fh_clear_wcc(current_fh);
  1519. /* If op is non-idempotent */
  1520. if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
  1521. /*
  1522. * Don't execute this op if we couldn't encode a
  1523. * succesful reply:
  1524. */
  1525. u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
  1526. /*
  1527. * Plus if there's another operation, make sure
  1528. * we'll have space to at least encode an error:
  1529. */
  1530. if (resp->opcnt < args->opcnt)
  1531. plen += COMPOUND_ERR_SLACK_SPACE;
  1532. op->status = nfsd4_check_resp_size(resp, plen);
  1533. }
  1534. if (op->status)
  1535. goto encode_op;
  1536. if (op->opdesc->op_get_currentstateid)
  1537. op->opdesc->op_get_currentstateid(cstate, &op->u);
  1538. op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
  1539. /* Only from SEQUENCE */
  1540. if (cstate->status == nfserr_replay_cache) {
  1541. dprintk("%s NFS4.1 replay from cache\n", __func__);
  1542. status = op->status;
  1543. goto out;
  1544. }
  1545. if (!op->status) {
  1546. if (op->opdesc->op_set_currentstateid)
  1547. op->opdesc->op_set_currentstateid(cstate, &op->u);
  1548. if (op->opdesc->op_flags & OP_CLEAR_STATEID)
  1549. clear_current_stateid(cstate);
  1550. if (need_wrongsec_check(rqstp))
  1551. op->status = check_nfsd_access(current_fh->fh_export, rqstp);
  1552. }
  1553. encode_op:
  1554. if (op->status == nfserr_replay_me) {
  1555. op->replay = &cstate->replay_owner->so_replay;
  1556. nfsd4_encode_replay(&resp->xdr, op);
  1557. status = op->status = op->replay->rp_status;
  1558. } else {
  1559. nfsd4_encode_operation(resp, op);
  1560. status = op->status;
  1561. }
  1562. dprintk("nfsv4 compound op %p opcnt %d #%d: %d: status %d\n",
  1563. args->ops, args->opcnt, resp->opcnt, op->opnum,
  1564. be32_to_cpu(status));
  1565. nfsd4_cstate_clear_replay(cstate);
  1566. nfsd4_increment_op_stats(op->opnum);
  1567. }
  1568. cstate->status = status;
  1569. fh_put(current_fh);
  1570. fh_put(save_fh);
  1571. BUG_ON(cstate->replay_owner);
  1572. out:
  1573. /* Reset deferral mechanism for RPC deferrals */
  1574. set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  1575. dprintk("nfsv4 compound returned %d\n", ntohl(status));
  1576. return status;
  1577. }
  1578. #define op_encode_hdr_size (2)
  1579. #define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
  1580. #define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
  1581. #define op_encode_change_info_maxsz (5)
  1582. #define nfs4_fattr_bitmap_maxsz (4)
  1583. /* We'll fall back on returning no lockowner if run out of space: */
  1584. #define op_encode_lockowner_maxsz (0)
  1585. #define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
  1586. #define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
  1587. #define op_encode_ace_maxsz (3 + nfs4_owner_maxsz)
  1588. #define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \
  1589. op_encode_ace_maxsz)
  1590. #define op_encode_channel_attrs_maxsz (6 + 1 + 1)
  1591. static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1592. {
  1593. return (op_encode_hdr_size) * sizeof(__be32);
  1594. }
  1595. static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1596. {
  1597. return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
  1598. }
  1599. static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1600. {
  1601. /* ac_supported, ac_resp_access */
  1602. return (op_encode_hdr_size + 2)* sizeof(__be32);
  1603. }
  1604. static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1605. {
  1606. return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
  1607. }
  1608. static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1609. {
  1610. return (op_encode_hdr_size + op_encode_change_info_maxsz
  1611. + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  1612. }
  1613. /*
  1614. * Note since this is an idempotent operation we won't insist on failing
  1615. * the op prematurely if the estimate is too large. We may turn off splice
  1616. * reads unnecessarily.
  1617. */
  1618. static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
  1619. struct nfsd4_op *op)
  1620. {
  1621. u32 *bmap = op->u.getattr.ga_bmval;
  1622. u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
  1623. u32 ret = 0;
  1624. if (bmap0 & FATTR4_WORD0_ACL)
  1625. return svc_max_payload(rqstp);
  1626. if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
  1627. return svc_max_payload(rqstp);
  1628. if (bmap1 & FATTR4_WORD1_OWNER) {
  1629. ret += IDMAP_NAMESZ + 4;
  1630. bmap1 &= ~FATTR4_WORD1_OWNER;
  1631. }
  1632. if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
  1633. ret += IDMAP_NAMESZ + 4;
  1634. bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
  1635. }
  1636. if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
  1637. ret += NFS4_FHSIZE + 4;
  1638. bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
  1639. }
  1640. if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
  1641. ret += NFS4_MAXLABELLEN + 12;
  1642. bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  1643. }
  1644. /*
  1645. * Largest of remaining attributes are 16 bytes (e.g.,
  1646. * supported_attributes)
  1647. */
  1648. ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
  1649. /* bitmask, length */
  1650. ret += 20;
  1651. return ret;
  1652. }
  1653. static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1654. {
  1655. return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
  1656. }
  1657. static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1658. {
  1659. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  1660. * sizeof(__be32);
  1661. }
  1662. static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1663. {
  1664. return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
  1665. * sizeof(__be32);
  1666. }
  1667. static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1668. {
  1669. return (op_encode_hdr_size + op_encode_stateid_maxsz
  1670. + op_encode_change_info_maxsz + 1
  1671. + nfs4_fattr_bitmap_maxsz
  1672. + op_encode_delegation_maxsz) * sizeof(__be32);
  1673. }
  1674. static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1675. {
  1676. u32 maxcount = 0, rlen = 0;
  1677. maxcount = svc_max_payload(rqstp);
  1678. rlen = min(op->u.read.rd_length, maxcount);
  1679. return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
  1680. }
  1681. static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1682. {
  1683. u32 maxcount = 0, rlen = 0;
  1684. maxcount = svc_max_payload(rqstp);
  1685. rlen = min(op->u.readdir.rd_maxcount, maxcount);
  1686. return (op_encode_hdr_size + op_encode_verifier_maxsz +
  1687. XDR_QUADLEN(rlen)) * sizeof(__be32);
  1688. }
  1689. static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1690. {
  1691. return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
  1692. }
  1693. static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1694. {
  1695. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  1696. * sizeof(__be32);
  1697. }
  1698. static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1699. {
  1700. return (op_encode_hdr_size + op_encode_change_info_maxsz
  1701. + op_encode_change_info_maxsz) * sizeof(__be32);
  1702. }
  1703. static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
  1704. struct nfsd4_op *op)
  1705. {
  1706. return (op_encode_hdr_size
  1707. + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
  1708. }
  1709. static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1710. {
  1711. return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
  1712. * sizeof(__be32);
  1713. }
  1714. static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1715. {
  1716. return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  1717. }
  1718. static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1719. {
  1720. return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
  1721. (4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
  1722. }
  1723. static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1724. {
  1725. return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
  1726. sizeof(__be32);
  1727. }
  1728. static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1729. {
  1730. return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
  1731. }
  1732. static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1733. {
  1734. return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
  1735. 1 + 1 + /* eir_flags, spr_how */\
  1736. 4 + /* spo_must_enforce & _allow with bitmap */\
  1737. 2 + /*eir_server_owner.so_minor_id */\
  1738. /* eir_server_owner.so_major_id<> */\
  1739. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  1740. /* eir_server_scope<> */\
  1741. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  1742. 1 + /* eir_server_impl_id array length */\
  1743. 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
  1744. }
  1745. static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1746. {
  1747. return (op_encode_hdr_size + \
  1748. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
  1749. 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
  1750. }
  1751. static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1752. {
  1753. return (op_encode_hdr_size + \
  1754. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
  1755. 2 + /* csr_sequence, csr_flags */\
  1756. op_encode_channel_attrs_maxsz + \
  1757. op_encode_channel_attrs_maxsz) * sizeof(__be32);
  1758. }
  1759. static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1760. {
  1761. return (op_encode_hdr_size +
  1762. 1 /* wr_callback */ +
  1763. op_encode_stateid_maxsz /* wr_callback */ +
  1764. 2 /* wr_count */ +
  1765. 1 /* wr_committed */ +
  1766. op_encode_verifier_maxsz +
  1767. 1 /* cr_consecutive */ +
  1768. 1 /* cr_synchronous */) * sizeof(__be32);
  1769. }
  1770. #ifdef CONFIG_NFSD_PNFS
  1771. static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1772. {
  1773. u32 maxcount = 0, rlen = 0;
  1774. maxcount = svc_max_payload(rqstp);
  1775. rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
  1776. return (op_encode_hdr_size +
  1777. 1 /* gd_layout_type*/ +
  1778. XDR_QUADLEN(rlen) +
  1779. 2 /* gd_notify_types */) * sizeof(__be32);
  1780. }
  1781. /*
  1782. * At this stage we don't really know what layout driver will handle the request,
  1783. * so we need to define an arbitrary upper bound here.
  1784. */
  1785. #define MAX_LAYOUT_SIZE 128
  1786. static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1787. {
  1788. return (op_encode_hdr_size +
  1789. 1 /* logr_return_on_close */ +
  1790. op_encode_stateid_maxsz +
  1791. 1 /* nr of layouts */ +
  1792. MAX_LAYOUT_SIZE) * sizeof(__be32);
  1793. }
  1794. static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1795. {
  1796. return (op_encode_hdr_size +
  1797. 1 /* locr_newsize */ +
  1798. 2 /* ns_size */) * sizeof(__be32);
  1799. }
  1800. static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1801. {
  1802. return (op_encode_hdr_size +
  1803. 1 /* lrs_stateid */ +
  1804. op_encode_stateid_maxsz) * sizeof(__be32);
  1805. }
  1806. #endif /* CONFIG_NFSD_PNFS */
  1807. static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  1808. {
  1809. return (op_encode_hdr_size + 3) * sizeof(__be32);
  1810. }
  1811. static const struct nfsd4_operation nfsd4_ops[] = {
  1812. [OP_ACCESS] = {
  1813. .op_func = nfsd4_access,
  1814. .op_name = "OP_ACCESS",
  1815. .op_rsize_bop = nfsd4_access_rsize,
  1816. },
  1817. [OP_CLOSE] = {
  1818. .op_func = nfsd4_close,
  1819. .op_flags = OP_MODIFIES_SOMETHING,
  1820. .op_name = "OP_CLOSE",
  1821. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1822. .op_get_currentstateid = nfsd4_get_closestateid,
  1823. .op_set_currentstateid = nfsd4_set_closestateid,
  1824. },
  1825. [OP_COMMIT] = {
  1826. .op_func = nfsd4_commit,
  1827. .op_flags = OP_MODIFIES_SOMETHING,
  1828. .op_name = "OP_COMMIT",
  1829. .op_rsize_bop = nfsd4_commit_rsize,
  1830. },
  1831. [OP_CREATE] = {
  1832. .op_func = nfsd4_create,
  1833. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
  1834. .op_name = "OP_CREATE",
  1835. .op_rsize_bop = nfsd4_create_rsize,
  1836. },
  1837. [OP_DELEGRETURN] = {
  1838. .op_func = nfsd4_delegreturn,
  1839. .op_flags = OP_MODIFIES_SOMETHING,
  1840. .op_name = "OP_DELEGRETURN",
  1841. .op_rsize_bop = nfsd4_only_status_rsize,
  1842. .op_get_currentstateid = nfsd4_get_delegreturnstateid,
  1843. },
  1844. [OP_GETATTR] = {
  1845. .op_func = nfsd4_getattr,
  1846. .op_flags = ALLOWED_ON_ABSENT_FS,
  1847. .op_rsize_bop = nfsd4_getattr_rsize,
  1848. .op_name = "OP_GETATTR",
  1849. },
  1850. [OP_GETFH] = {
  1851. .op_func = nfsd4_getfh,
  1852. .op_name = "OP_GETFH",
  1853. .op_rsize_bop = nfsd4_getfh_rsize,
  1854. },
  1855. [OP_LINK] = {
  1856. .op_func = nfsd4_link,
  1857. .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
  1858. | OP_CACHEME,
  1859. .op_name = "OP_LINK",
  1860. .op_rsize_bop = nfsd4_link_rsize,
  1861. },
  1862. [OP_LOCK] = {
  1863. .op_func = nfsd4_lock,
  1864. .op_flags = OP_MODIFIES_SOMETHING |
  1865. OP_NONTRIVIAL_ERROR_ENCODE,
  1866. .op_name = "OP_LOCK",
  1867. .op_rsize_bop = nfsd4_lock_rsize,
  1868. .op_set_currentstateid = nfsd4_set_lockstateid,
  1869. },
  1870. [OP_LOCKT] = {
  1871. .op_func = nfsd4_lockt,
  1872. .op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
  1873. .op_name = "OP_LOCKT",
  1874. .op_rsize_bop = nfsd4_lock_rsize,
  1875. },
  1876. [OP_LOCKU] = {
  1877. .op_func = nfsd4_locku,
  1878. .op_flags = OP_MODIFIES_SOMETHING,
  1879. .op_name = "OP_LOCKU",
  1880. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1881. .op_get_currentstateid = nfsd4_get_lockustateid,
  1882. },
  1883. [OP_LOOKUP] = {
  1884. .op_func = nfsd4_lookup,
  1885. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  1886. .op_name = "OP_LOOKUP",
  1887. .op_rsize_bop = nfsd4_only_status_rsize,
  1888. },
  1889. [OP_LOOKUPP] = {
  1890. .op_func = nfsd4_lookupp,
  1891. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  1892. .op_name = "OP_LOOKUPP",
  1893. .op_rsize_bop = nfsd4_only_status_rsize,
  1894. },
  1895. [OP_NVERIFY] = {
  1896. .op_func = nfsd4_nverify,
  1897. .op_name = "OP_NVERIFY",
  1898. .op_rsize_bop = nfsd4_only_status_rsize,
  1899. },
  1900. [OP_OPEN] = {
  1901. .op_func = nfsd4_open,
  1902. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  1903. .op_name = "OP_OPEN",
  1904. .op_rsize_bop = nfsd4_open_rsize,
  1905. .op_set_currentstateid = nfsd4_set_openstateid,
  1906. },
  1907. [OP_OPEN_CONFIRM] = {
  1908. .op_func = nfsd4_open_confirm,
  1909. .op_flags = OP_MODIFIES_SOMETHING,
  1910. .op_name = "OP_OPEN_CONFIRM",
  1911. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1912. },
  1913. [OP_OPEN_DOWNGRADE] = {
  1914. .op_func = nfsd4_open_downgrade,
  1915. .op_flags = OP_MODIFIES_SOMETHING,
  1916. .op_name = "OP_OPEN_DOWNGRADE",
  1917. .op_rsize_bop = nfsd4_status_stateid_rsize,
  1918. .op_get_currentstateid = nfsd4_get_opendowngradestateid,
  1919. .op_set_currentstateid = nfsd4_set_opendowngradestateid,
  1920. },
  1921. [OP_PUTFH] = {
  1922. .op_func = nfsd4_putfh,
  1923. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1924. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1925. .op_name = "OP_PUTFH",
  1926. .op_rsize_bop = nfsd4_only_status_rsize,
  1927. },
  1928. [OP_PUTPUBFH] = {
  1929. .op_func = nfsd4_putrootfh,
  1930. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1931. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1932. .op_name = "OP_PUTPUBFH",
  1933. .op_rsize_bop = nfsd4_only_status_rsize,
  1934. },
  1935. [OP_PUTROOTFH] = {
  1936. .op_func = nfsd4_putrootfh,
  1937. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1938. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  1939. .op_name = "OP_PUTROOTFH",
  1940. .op_rsize_bop = nfsd4_only_status_rsize,
  1941. },
  1942. [OP_READ] = {
  1943. .op_func = nfsd4_read,
  1944. .op_release = nfsd4_read_release,
  1945. .op_name = "OP_READ",
  1946. .op_rsize_bop = nfsd4_read_rsize,
  1947. .op_get_currentstateid = nfsd4_get_readstateid,
  1948. },
  1949. [OP_READDIR] = {
  1950. .op_func = nfsd4_readdir,
  1951. .op_name = "OP_READDIR",
  1952. .op_rsize_bop = nfsd4_readdir_rsize,
  1953. },
  1954. [OP_READLINK] = {
  1955. .op_func = nfsd4_readlink,
  1956. .op_name = "OP_READLINK",
  1957. .op_rsize_bop = nfsd4_readlink_rsize,
  1958. },
  1959. [OP_REMOVE] = {
  1960. .op_func = nfsd4_remove,
  1961. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1962. .op_name = "OP_REMOVE",
  1963. .op_rsize_bop = nfsd4_remove_rsize,
  1964. },
  1965. [OP_RENAME] = {
  1966. .op_func = nfsd4_rename,
  1967. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  1968. .op_name = "OP_RENAME",
  1969. .op_rsize_bop = nfsd4_rename_rsize,
  1970. },
  1971. [OP_RENEW] = {
  1972. .op_func = nfsd4_renew,
  1973. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1974. | OP_MODIFIES_SOMETHING,
  1975. .op_name = "OP_RENEW",
  1976. .op_rsize_bop = nfsd4_only_status_rsize,
  1977. },
  1978. [OP_RESTOREFH] = {
  1979. .op_func = nfsd4_restorefh,
  1980. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  1981. | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
  1982. .op_name = "OP_RESTOREFH",
  1983. .op_rsize_bop = nfsd4_only_status_rsize,
  1984. },
  1985. [OP_SAVEFH] = {
  1986. .op_func = nfsd4_savefh,
  1987. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  1988. .op_name = "OP_SAVEFH",
  1989. .op_rsize_bop = nfsd4_only_status_rsize,
  1990. },
  1991. [OP_SECINFO] = {
  1992. .op_func = nfsd4_secinfo,
  1993. .op_release = nfsd4_secinfo_release,
  1994. .op_flags = OP_HANDLES_WRONGSEC,
  1995. .op_name = "OP_SECINFO",
  1996. .op_rsize_bop = nfsd4_secinfo_rsize,
  1997. },
  1998. [OP_SETATTR] = {
  1999. .op_func = nfsd4_setattr,
  2000. .op_name = "OP_SETATTR",
  2001. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
  2002. | OP_NONTRIVIAL_ERROR_ENCODE,
  2003. .op_rsize_bop = nfsd4_setattr_rsize,
  2004. .op_get_currentstateid = nfsd4_get_setattrstateid,
  2005. },
  2006. [OP_SETCLIENTID] = {
  2007. .op_func = nfsd4_setclientid,
  2008. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2009. | OP_MODIFIES_SOMETHING | OP_CACHEME
  2010. | OP_NONTRIVIAL_ERROR_ENCODE,
  2011. .op_name = "OP_SETCLIENTID",
  2012. .op_rsize_bop = nfsd4_setclientid_rsize,
  2013. },
  2014. [OP_SETCLIENTID_CONFIRM] = {
  2015. .op_func = nfsd4_setclientid_confirm,
  2016. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2017. | OP_MODIFIES_SOMETHING | OP_CACHEME,
  2018. .op_name = "OP_SETCLIENTID_CONFIRM",
  2019. .op_rsize_bop = nfsd4_only_status_rsize,
  2020. },
  2021. [OP_VERIFY] = {
  2022. .op_func = nfsd4_verify,
  2023. .op_name = "OP_VERIFY",
  2024. .op_rsize_bop = nfsd4_only_status_rsize,
  2025. },
  2026. [OP_WRITE] = {
  2027. .op_func = nfsd4_write,
  2028. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2029. .op_name = "OP_WRITE",
  2030. .op_rsize_bop = nfsd4_write_rsize,
  2031. .op_get_currentstateid = nfsd4_get_writestateid,
  2032. },
  2033. [OP_RELEASE_LOCKOWNER] = {
  2034. .op_func = nfsd4_release_lockowner,
  2035. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2036. | OP_MODIFIES_SOMETHING,
  2037. .op_name = "OP_RELEASE_LOCKOWNER",
  2038. .op_rsize_bop = nfsd4_only_status_rsize,
  2039. },
  2040. /* NFSv4.1 operations */
  2041. [OP_EXCHANGE_ID] = {
  2042. .op_func = nfsd4_exchange_id,
  2043. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2044. | OP_MODIFIES_SOMETHING,
  2045. .op_name = "OP_EXCHANGE_ID",
  2046. .op_rsize_bop = nfsd4_exchange_id_rsize,
  2047. },
  2048. [OP_BACKCHANNEL_CTL] = {
  2049. .op_func = nfsd4_backchannel_ctl,
  2050. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2051. .op_name = "OP_BACKCHANNEL_CTL",
  2052. .op_rsize_bop = nfsd4_only_status_rsize,
  2053. },
  2054. [OP_BIND_CONN_TO_SESSION] = {
  2055. .op_func = nfsd4_bind_conn_to_session,
  2056. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2057. | OP_MODIFIES_SOMETHING,
  2058. .op_name = "OP_BIND_CONN_TO_SESSION",
  2059. .op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
  2060. },
  2061. [OP_CREATE_SESSION] = {
  2062. .op_func = nfsd4_create_session,
  2063. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2064. | OP_MODIFIES_SOMETHING,
  2065. .op_name = "OP_CREATE_SESSION",
  2066. .op_rsize_bop = nfsd4_create_session_rsize,
  2067. },
  2068. [OP_DESTROY_SESSION] = {
  2069. .op_func = nfsd4_destroy_session,
  2070. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2071. | OP_MODIFIES_SOMETHING,
  2072. .op_name = "OP_DESTROY_SESSION",
  2073. .op_rsize_bop = nfsd4_only_status_rsize,
  2074. },
  2075. [OP_SEQUENCE] = {
  2076. .op_func = nfsd4_sequence,
  2077. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  2078. .op_name = "OP_SEQUENCE",
  2079. .op_rsize_bop = nfsd4_sequence_rsize,
  2080. },
  2081. [OP_DESTROY_CLIENTID] = {
  2082. .op_func = nfsd4_destroy_clientid,
  2083. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2084. | OP_MODIFIES_SOMETHING,
  2085. .op_name = "OP_DESTROY_CLIENTID",
  2086. .op_rsize_bop = nfsd4_only_status_rsize,
  2087. },
  2088. [OP_RECLAIM_COMPLETE] = {
  2089. .op_func = nfsd4_reclaim_complete,
  2090. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2091. .op_name = "OP_RECLAIM_COMPLETE",
  2092. .op_rsize_bop = nfsd4_only_status_rsize,
  2093. },
  2094. [OP_SECINFO_NO_NAME] = {
  2095. .op_func = nfsd4_secinfo_no_name,
  2096. .op_release = nfsd4_secinfo_no_name_release,
  2097. .op_flags = OP_HANDLES_WRONGSEC,
  2098. .op_name = "OP_SECINFO_NO_NAME",
  2099. .op_rsize_bop = nfsd4_secinfo_rsize,
  2100. },
  2101. [OP_TEST_STATEID] = {
  2102. .op_func = nfsd4_test_stateid,
  2103. .op_flags = ALLOWED_WITHOUT_FH,
  2104. .op_name = "OP_TEST_STATEID",
  2105. .op_rsize_bop = nfsd4_test_stateid_rsize,
  2106. },
  2107. [OP_FREE_STATEID] = {
  2108. .op_func = nfsd4_free_stateid,
  2109. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2110. .op_name = "OP_FREE_STATEID",
  2111. .op_get_currentstateid = nfsd4_get_freestateid,
  2112. .op_rsize_bop = nfsd4_only_status_rsize,
  2113. },
  2114. #ifdef CONFIG_NFSD_PNFS
  2115. [OP_GETDEVICEINFO] = {
  2116. .op_func = nfsd4_getdeviceinfo,
  2117. .op_release = nfsd4_getdeviceinfo_release,
  2118. .op_flags = ALLOWED_WITHOUT_FH,
  2119. .op_name = "OP_GETDEVICEINFO",
  2120. .op_rsize_bop = nfsd4_getdeviceinfo_rsize,
  2121. },
  2122. [OP_LAYOUTGET] = {
  2123. .op_func = nfsd4_layoutget,
  2124. .op_release = nfsd4_layoutget_release,
  2125. .op_flags = OP_MODIFIES_SOMETHING,
  2126. .op_name = "OP_LAYOUTGET",
  2127. .op_rsize_bop = nfsd4_layoutget_rsize,
  2128. },
  2129. [OP_LAYOUTCOMMIT] = {
  2130. .op_func = nfsd4_layoutcommit,
  2131. .op_flags = OP_MODIFIES_SOMETHING,
  2132. .op_name = "OP_LAYOUTCOMMIT",
  2133. .op_rsize_bop = nfsd4_layoutcommit_rsize,
  2134. },
  2135. [OP_LAYOUTRETURN] = {
  2136. .op_func = nfsd4_layoutreturn,
  2137. .op_flags = OP_MODIFIES_SOMETHING,
  2138. .op_name = "OP_LAYOUTRETURN",
  2139. .op_rsize_bop = nfsd4_layoutreturn_rsize,
  2140. },
  2141. #endif /* CONFIG_NFSD_PNFS */
  2142. /* NFSv4.2 operations */
  2143. [OP_ALLOCATE] = {
  2144. .op_func = nfsd4_allocate,
  2145. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2146. .op_name = "OP_ALLOCATE",
  2147. .op_rsize_bop = nfsd4_only_status_rsize,
  2148. },
  2149. [OP_DEALLOCATE] = {
  2150. .op_func = nfsd4_deallocate,
  2151. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2152. .op_name = "OP_DEALLOCATE",
  2153. .op_rsize_bop = nfsd4_only_status_rsize,
  2154. },
  2155. [OP_CLONE] = {
  2156. .op_func = nfsd4_clone,
  2157. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2158. .op_name = "OP_CLONE",
  2159. .op_rsize_bop = nfsd4_only_status_rsize,
  2160. },
  2161. [OP_COPY] = {
  2162. .op_func = nfsd4_copy,
  2163. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2164. .op_name = "OP_COPY",
  2165. .op_rsize_bop = nfsd4_copy_rsize,
  2166. },
  2167. [OP_SEEK] = {
  2168. .op_func = nfsd4_seek,
  2169. .op_name = "OP_SEEK",
  2170. .op_rsize_bop = nfsd4_seek_rsize,
  2171. },
  2172. };
  2173. /**
  2174. * nfsd4_spo_must_allow - Determine if the compound op contains an
  2175. * operation that is allowed to be sent with machine credentials
  2176. *
  2177. * @rqstp: a pointer to the struct svc_rqst
  2178. *
  2179. * Checks to see if the compound contains a spo_must_allow op
  2180. * and confirms that it was sent with the proper machine creds.
  2181. */
  2182. bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
  2183. {
  2184. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2185. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  2186. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  2187. struct nfsd4_compound_state *cstate = &resp->cstate;
  2188. struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
  2189. u32 opiter;
  2190. if (!cstate->minorversion)
  2191. return false;
  2192. if (cstate->spo_must_allowed == true)
  2193. return true;
  2194. opiter = resp->opcnt;
  2195. while (opiter < argp->opcnt) {
  2196. this = &argp->ops[opiter++];
  2197. if (test_bit(this->opnum, allow->u.longs) &&
  2198. cstate->clp->cl_mach_cred &&
  2199. nfsd4_mach_creds_match(cstate->clp, rqstp)) {
  2200. cstate->spo_must_allowed = true;
  2201. return true;
  2202. }
  2203. }
  2204. cstate->spo_must_allowed = false;
  2205. return false;
  2206. }
  2207. int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2208. {
  2209. if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
  2210. return op_encode_hdr_size * sizeof(__be32);
  2211. BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
  2212. return OPDESC(op)->op_rsize_bop(rqstp, op);
  2213. }
  2214. void warn_on_nonidempotent_op(struct nfsd4_op *op)
  2215. {
  2216. if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
  2217. pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
  2218. op->opnum, nfsd4_op_name(op->opnum));
  2219. WARN_ON_ONCE(1);
  2220. }
  2221. }
  2222. static const char *nfsd4_op_name(unsigned opnum)
  2223. {
  2224. if (opnum < ARRAY_SIZE(nfsd4_ops))
  2225. return nfsd4_ops[opnum].op_name;
  2226. return "unknown_operation";
  2227. }
  2228. #define nfsd4_voidres nfsd4_voidargs
  2229. struct nfsd4_voidargs { int dummy; };
  2230. static const struct svc_procedure nfsd_procedures4[2] = {
  2231. [NFSPROC4_NULL] = {
  2232. .pc_func = nfsd4_proc_null,
  2233. .pc_encode = nfs4svc_encode_voidres,
  2234. .pc_argsize = sizeof(struct nfsd4_voidargs),
  2235. .pc_ressize = sizeof(struct nfsd4_voidres),
  2236. .pc_cachetype = RC_NOCACHE,
  2237. .pc_xdrressize = 1,
  2238. },
  2239. [NFSPROC4_COMPOUND] = {
  2240. .pc_func = nfsd4_proc_compound,
  2241. .pc_decode = nfs4svc_decode_compoundargs,
  2242. .pc_encode = nfs4svc_encode_compoundres,
  2243. .pc_argsize = sizeof(struct nfsd4_compoundargs),
  2244. .pc_ressize = sizeof(struct nfsd4_compoundres),
  2245. .pc_release = nfsd4_release_compoundargs,
  2246. .pc_cachetype = RC_NOCACHE,
  2247. .pc_xdrressize = NFSD_BUFSIZE/4,
  2248. },
  2249. };
  2250. static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
  2251. const struct svc_version nfsd_version4 = {
  2252. .vs_vers = 4,
  2253. .vs_nproc = 2,
  2254. .vs_proc = nfsd_procedures4,
  2255. .vs_count = nfsd_count3,
  2256. .vs_dispatch = nfsd_dispatch,
  2257. .vs_xdrsize = NFS4_SVC_XDRSIZE,
  2258. .vs_rpcb_optnl = true,
  2259. .vs_need_cong_ctrl = true,
  2260. };
  2261. /*
  2262. * Local variables:
  2263. * c-basic-offset: 8
  2264. * End:
  2265. */