nfs4proc.c 70 KB

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