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