nfs4proc.c 70 KB

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