nfs4proc.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336
  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 <linux/kthread.h>
  40. #include <linux/sunrpc/addr.h>
  41. #include <linux/nfs_ssc.h>
  42. #include "idmap.h"
  43. #include "cache.h"
  44. #include "xdr4.h"
  45. #include "vfs.h"
  46. #include "current_stateid.h"
  47. #include "netns.h"
  48. #include "acl.h"
  49. #include "pnfs.h"
  50. #include "trace.h"
  51. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  52. #include <linux/security.h>
  53. static inline void
  54. nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
  55. {
  56. struct inode *inode = d_inode(resfh->fh_dentry);
  57. int status;
  58. inode_lock(inode);
  59. status = security_inode_setsecctx(resfh->fh_dentry,
  60. label->data, label->len);
  61. inode_unlock(inode);
  62. if (status)
  63. /*
  64. * XXX: We should really fail the whole open, but we may
  65. * already have created a new file, so it may be too
  66. * late. For now this seems the least of evils:
  67. */
  68. bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
  69. return;
  70. }
  71. #else
  72. static inline void
  73. nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
  74. { }
  75. #endif
  76. #define NFSDDBG_FACILITY NFSDDBG_PROC
  77. static u32 nfsd_attrmask[] = {
  78. NFSD_WRITEABLE_ATTRS_WORD0,
  79. NFSD_WRITEABLE_ATTRS_WORD1,
  80. NFSD_WRITEABLE_ATTRS_WORD2
  81. };
  82. static u32 nfsd41_ex_attrmask[] = {
  83. NFSD_SUPPATTR_EXCLCREAT_WORD0,
  84. NFSD_SUPPATTR_EXCLCREAT_WORD1,
  85. NFSD_SUPPATTR_EXCLCREAT_WORD2
  86. };
  87. static __be32
  88. check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  89. u32 *bmval, u32 *writable)
  90. {
  91. struct dentry *dentry = cstate->current_fh.fh_dentry;
  92. struct svc_export *exp = cstate->current_fh.fh_export;
  93. if (!nfsd_attrs_supported(cstate->minorversion, bmval))
  94. return nfserr_attrnotsupp;
  95. if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
  96. return nfserr_attrnotsupp;
  97. if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
  98. !(exp->ex_flags & NFSEXP_SECURITY_LABEL))
  99. return nfserr_attrnotsupp;
  100. if (writable && !bmval_is_subset(bmval, writable))
  101. return nfserr_inval;
  102. if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
  103. (bmval[1] & FATTR4_WORD1_MODE))
  104. return nfserr_inval;
  105. return nfs_ok;
  106. }
  107. static __be32
  108. nfsd4_check_open_attributes(struct svc_rqst *rqstp,
  109. struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  110. {
  111. __be32 status = nfs_ok;
  112. if (open->op_create == NFS4_OPEN_CREATE) {
  113. if (open->op_createmode == NFS4_CREATE_UNCHECKED
  114. || open->op_createmode == NFS4_CREATE_GUARDED)
  115. status = check_attr_support(rqstp, cstate,
  116. open->op_bmval, nfsd_attrmask);
  117. else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
  118. status = check_attr_support(rqstp, cstate,
  119. open->op_bmval, nfsd41_ex_attrmask);
  120. }
  121. return status;
  122. }
  123. static int
  124. is_create_with_attrs(struct nfsd4_open *open)
  125. {
  126. return open->op_create == NFS4_OPEN_CREATE
  127. && (open->op_createmode == NFS4_CREATE_UNCHECKED
  128. || open->op_createmode == NFS4_CREATE_GUARDED
  129. || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
  130. }
  131. /*
  132. * if error occurs when setting the acl, just clear the acl bit
  133. * in the returned attr bitmap.
  134. */
  135. static void
  136. do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  137. struct nfs4_acl *acl, u32 *bmval)
  138. {
  139. __be32 status;
  140. status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
  141. if (status)
  142. /*
  143. * We should probably fail the whole open at this point,
  144. * but we've already created the file, so it's too late;
  145. * So this seems the least of evils:
  146. */
  147. bmval[0] &= ~FATTR4_WORD0_ACL;
  148. }
  149. static inline void
  150. fh_dup2(struct svc_fh *dst, struct svc_fh *src)
  151. {
  152. fh_put(dst);
  153. dget(src->fh_dentry);
  154. if (src->fh_export)
  155. exp_get(src->fh_export);
  156. *dst = *src;
  157. }
  158. static __be32
  159. do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
  160. {
  161. __be32 status;
  162. if (open->op_truncate &&
  163. !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  164. return nfserr_inval;
  165. accmode |= NFSD_MAY_READ_IF_EXEC;
  166. if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
  167. accmode |= NFSD_MAY_READ;
  168. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  169. accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
  170. if (open->op_share_deny & NFS4_SHARE_DENY_READ)
  171. accmode |= NFSD_MAY_WRITE;
  172. status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
  173. return status;
  174. }
  175. static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
  176. {
  177. umode_t mode = d_inode(fh->fh_dentry)->i_mode;
  178. if (S_ISREG(mode))
  179. return nfs_ok;
  180. if (S_ISDIR(mode))
  181. return nfserr_isdir;
  182. /*
  183. * Using err_symlink as our catch-all case may look odd; but
  184. * there's no other obvious error for this case in 4.0, and we
  185. * happen to know that it will cause the linux v4 client to do
  186. * the right thing on attempts to open something other than a
  187. * regular file.
  188. */
  189. return nfserr_symlink;
  190. }
  191. static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
  192. {
  193. if (nfsd4_has_session(cstate))
  194. return;
  195. fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
  196. &resfh->fh_handle);
  197. }
  198. static __be32
  199. do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
  200. {
  201. struct svc_fh *current_fh = &cstate->current_fh;
  202. int accmode;
  203. __be32 status;
  204. *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  205. if (!*resfh)
  206. return nfserr_jukebox;
  207. fh_init(*resfh, NFS4_FHSIZE);
  208. open->op_truncate = false;
  209. if (open->op_create) {
  210. /* FIXME: check session persistence and pnfs flags.
  211. * The nfsv4.1 spec requires the following semantics:
  212. *
  213. * Persistent | pNFS | Server REQUIRED | Client Allowed
  214. * Reply Cache | server | |
  215. * -------------+--------+-----------------+--------------------
  216. * no | no | EXCLUSIVE4_1 | EXCLUSIVE4_1
  217. * | | | (SHOULD)
  218. * | | and EXCLUSIVE4 | or EXCLUSIVE4
  219. * | | | (SHOULD NOT)
  220. * no | yes | EXCLUSIVE4_1 | EXCLUSIVE4_1
  221. * yes | no | GUARDED4 | GUARDED4
  222. * yes | yes | GUARDED4 | GUARDED4
  223. */
  224. /*
  225. * Note: create modes (UNCHECKED,GUARDED...) are the same
  226. * in NFSv4 as in v3 except EXCLUSIVE4_1.
  227. */
  228. current->fs->umask = open->op_umask;
  229. status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
  230. open->op_fname.len, &open->op_iattr,
  231. *resfh, open->op_createmode,
  232. (u32 *)open->op_verf.data,
  233. &open->op_truncate, &open->op_created);
  234. current->fs->umask = 0;
  235. if (!status && open->op_label.len)
  236. nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
  237. /*
  238. * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
  239. * use the returned bitmask to indicate which attributes
  240. * we used to store the verifier:
  241. */
  242. if (nfsd_create_is_exclusive(open->op_createmode) && status == 0)
  243. open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
  244. FATTR4_WORD1_TIME_MODIFY);
  245. } else
  246. /*
  247. * Note this may exit with the parent still locked.
  248. * We will hold the lock until nfsd4_open's final
  249. * lookup, to prevent renames or unlinks until we've had
  250. * a chance to an acquire a delegation if appropriate.
  251. */
  252. status = nfsd_lookup(rqstp, current_fh,
  253. open->op_fname.data, open->op_fname.len, *resfh);
  254. if (status)
  255. goto out;
  256. status = nfsd_check_obj_isreg(*resfh);
  257. if (status)
  258. goto out;
  259. if (is_create_with_attrs(open) && open->op_acl != NULL)
  260. do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
  261. nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
  262. accmode = NFSD_MAY_NOP;
  263. if (open->op_created ||
  264. open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
  265. accmode |= NFSD_MAY_OWNER_OVERRIDE;
  266. status = do_open_permission(rqstp, *resfh, open, accmode);
  267. set_change_info(&open->op_cinfo, current_fh);
  268. out:
  269. return status;
  270. }
  271. static __be32
  272. do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  273. {
  274. struct svc_fh *current_fh = &cstate->current_fh;
  275. __be32 status;
  276. int accmode = 0;
  277. /* We don't know the target directory, and therefore can not
  278. * set the change info
  279. */
  280. memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
  281. nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
  282. open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
  283. (open->op_iattr.ia_size == 0);
  284. /*
  285. * In the delegation case, the client is telling us about an
  286. * open that it *already* performed locally, some time ago. We
  287. * should let it succeed now if possible.
  288. *
  289. * In the case of a CLAIM_FH open, on the other hand, the client
  290. * may be counting on us to enforce permissions (the Linux 4.1
  291. * client uses this for normal opens, for example).
  292. */
  293. if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
  294. accmode = NFSD_MAY_OWNER_OVERRIDE;
  295. status = do_open_permission(rqstp, current_fh, open, accmode);
  296. return status;
  297. }
  298. static void
  299. copy_clientid(clientid_t *clid, struct nfsd4_session *session)
  300. {
  301. struct nfsd4_sessionid *sid =
  302. (struct nfsd4_sessionid *)session->se_sessionid.data;
  303. clid->cl_boot = sid->clientid.cl_boot;
  304. clid->cl_id = sid->clientid.cl_id;
  305. }
  306. static __be32
  307. nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  308. union nfsd4_op_u *u)
  309. {
  310. struct nfsd4_open *open = &u->open;
  311. __be32 status;
  312. struct svc_fh *resfh = NULL;
  313. struct net *net = SVC_NET(rqstp);
  314. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  315. bool reclaim = false;
  316. dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
  317. (int)open->op_fname.len, open->op_fname.data,
  318. open->op_openowner);
  319. /* This check required by spec. */
  320. if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
  321. return nfserr_inval;
  322. open->op_created = false;
  323. /*
  324. * RFC5661 18.51.3
  325. * Before RECLAIM_COMPLETE done, server should deny new lock
  326. */
  327. if (nfsd4_has_session(cstate) &&
  328. !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  329. &cstate->session->se_client->cl_flags) &&
  330. open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  331. return nfserr_grace;
  332. if (nfsd4_has_session(cstate))
  333. copy_clientid(&open->op_clientid, cstate->session);
  334. /* check seqid for replay. set nfs4_owner */
  335. status = nfsd4_process_open1(cstate, open, nn);
  336. if (status == nfserr_replay_me) {
  337. struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
  338. fh_put(&cstate->current_fh);
  339. fh_copy_shallow(&cstate->current_fh.fh_handle,
  340. &rp->rp_openfh);
  341. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  342. if (status)
  343. dprintk("nfsd4_open: replay failed"
  344. " restoring previous filehandle\n");
  345. else
  346. status = nfserr_replay_me;
  347. }
  348. if (status)
  349. goto out;
  350. if (open->op_xdr_error) {
  351. status = open->op_xdr_error;
  352. goto out;
  353. }
  354. status = nfsd4_check_open_attributes(rqstp, cstate, open);
  355. if (status)
  356. goto out;
  357. /* Openowner is now set, so sequence id will get bumped. Now we need
  358. * these checks before we do any creates: */
  359. status = nfserr_grace;
  360. if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
  361. goto out;
  362. status = nfserr_no_grace;
  363. if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  364. goto out;
  365. switch (open->op_claim_type) {
  366. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  367. case NFS4_OPEN_CLAIM_NULL:
  368. status = do_open_lookup(rqstp, cstate, open, &resfh);
  369. if (status)
  370. goto out;
  371. break;
  372. case NFS4_OPEN_CLAIM_PREVIOUS:
  373. status = nfs4_check_open_reclaim(&open->op_clientid,
  374. cstate, nn);
  375. if (status)
  376. goto out;
  377. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  378. reclaim = true;
  379. fallthrough;
  380. case NFS4_OPEN_CLAIM_FH:
  381. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  382. status = do_open_fhandle(rqstp, cstate, open);
  383. if (status)
  384. goto out;
  385. resfh = &cstate->current_fh;
  386. break;
  387. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  388. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  389. dprintk("NFSD: unsupported OPEN claim type %d\n",
  390. open->op_claim_type);
  391. status = nfserr_notsupp;
  392. goto out;
  393. default:
  394. dprintk("NFSD: Invalid OPEN claim type %d\n",
  395. open->op_claim_type);
  396. status = nfserr_inval;
  397. goto out;
  398. }
  399. /*
  400. * nfsd4_process_open2() does the actual opening of the file. If
  401. * successful, it (1) truncates the file if open->op_truncate was
  402. * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
  403. */
  404. status = nfsd4_process_open2(rqstp, resfh, open);
  405. WARN(status && open->op_created,
  406. "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
  407. be32_to_cpu(status));
  408. if (reclaim && !status)
  409. nn->somebody_reclaimed = true;
  410. out:
  411. if (resfh && resfh != &cstate->current_fh) {
  412. fh_dup2(&cstate->current_fh, resfh);
  413. fh_put(resfh);
  414. kfree(resfh);
  415. }
  416. nfsd4_cleanup_open_state(cstate, open);
  417. nfsd4_bump_seqid(cstate, status);
  418. return status;
  419. }
  420. /*
  421. * OPEN is the only seqid-mutating operation whose decoding can fail
  422. * with a seqid-mutating error (specifically, decoding of user names in
  423. * the attributes). Therefore we have to do some processing to look up
  424. * the stateowner so that we can bump the seqid.
  425. */
  426. static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
  427. {
  428. struct nfsd4_open *open = &op->u.open;
  429. if (!seqid_mutating_err(ntohl(op->status)))
  430. return op->status;
  431. if (nfsd4_has_session(cstate))
  432. return op->status;
  433. open->op_xdr_error = op->status;
  434. return nfsd4_open(rqstp, cstate, &op->u);
  435. }
  436. /*
  437. * filehandle-manipulating ops.
  438. */
  439. static __be32
  440. nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  441. union nfsd4_op_u *u)
  442. {
  443. u->getfh = &cstate->current_fh;
  444. return nfs_ok;
  445. }
  446. static __be32
  447. nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  448. union nfsd4_op_u *u)
  449. {
  450. struct nfsd4_putfh *putfh = &u->putfh;
  451. __be32 ret;
  452. fh_put(&cstate->current_fh);
  453. cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
  454. memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
  455. putfh->pf_fhlen);
  456. ret = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
  457. #ifdef CONFIG_NFSD_V4_2_INTER_SSC
  458. if (ret == nfserr_stale && putfh->no_verify) {
  459. SET_FH_FLAG(&cstate->current_fh, NFSD4_FH_FOREIGN);
  460. ret = 0;
  461. }
  462. #endif
  463. return ret;
  464. }
  465. static __be32
  466. nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  467. union nfsd4_op_u *u)
  468. {
  469. __be32 status;
  470. fh_put(&cstate->current_fh);
  471. status = exp_pseudoroot(rqstp, &cstate->current_fh);
  472. return status;
  473. }
  474. static __be32
  475. nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  476. union nfsd4_op_u *u)
  477. {
  478. if (!cstate->save_fh.fh_dentry)
  479. return nfserr_restorefh;
  480. fh_dup2(&cstate->current_fh, &cstate->save_fh);
  481. if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) {
  482. memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
  483. SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
  484. }
  485. return nfs_ok;
  486. }
  487. static __be32
  488. nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  489. union nfsd4_op_u *u)
  490. {
  491. fh_dup2(&cstate->save_fh, &cstate->current_fh);
  492. if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG)) {
  493. memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
  494. SET_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG);
  495. }
  496. return nfs_ok;
  497. }
  498. /*
  499. * misc nfsv4 ops
  500. */
  501. static __be32
  502. nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  503. union nfsd4_op_u *u)
  504. {
  505. struct nfsd4_access *access = &u->access;
  506. u32 access_full;
  507. access_full = NFS3_ACCESS_FULL;
  508. if (cstate->minorversion >= 2)
  509. access_full |= NFS4_ACCESS_XALIST | NFS4_ACCESS_XAREAD |
  510. NFS4_ACCESS_XAWRITE;
  511. if (access->ac_req_access & ~access_full)
  512. return nfserr_inval;
  513. access->ac_resp_access = access->ac_req_access;
  514. return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
  515. &access->ac_supported);
  516. }
  517. static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
  518. {
  519. __be32 *verf = (__be32 *)verifier->data;
  520. BUILD_BUG_ON(2*sizeof(*verf) != sizeof(verifier->data));
  521. nfsd_copy_boot_verifier(verf, net_generic(net, nfsd_net_id));
  522. }
  523. static __be32
  524. nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  525. union nfsd4_op_u *u)
  526. {
  527. struct nfsd4_commit *commit = &u->commit;
  528. return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
  529. commit->co_count,
  530. (__be32 *)commit->co_verf.data);
  531. }
  532. static __be32
  533. nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  534. union nfsd4_op_u *u)
  535. {
  536. struct nfsd4_create *create = &u->create;
  537. struct svc_fh resfh;
  538. __be32 status;
  539. dev_t rdev;
  540. fh_init(&resfh, NFS4_FHSIZE);
  541. status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
  542. if (status)
  543. return status;
  544. status = check_attr_support(rqstp, cstate, create->cr_bmval,
  545. nfsd_attrmask);
  546. if (status)
  547. return status;
  548. current->fs->umask = create->cr_umask;
  549. switch (create->cr_type) {
  550. case NF4LNK:
  551. status = nfsd_symlink(rqstp, &cstate->current_fh,
  552. create->cr_name, create->cr_namelen,
  553. create->cr_data, &resfh);
  554. break;
  555. case NF4BLK:
  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_IFBLK, rdev, &resfh);
  564. break;
  565. case NF4CHR:
  566. status = nfserr_inval;
  567. rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
  568. if (MAJOR(rdev) != create->cr_specdata1 ||
  569. MINOR(rdev) != create->cr_specdata2)
  570. goto out_umask;
  571. status = nfsd_create(rqstp, &cstate->current_fh,
  572. create->cr_name, create->cr_namelen,
  573. &create->cr_iattr,S_IFCHR, rdev, &resfh);
  574. break;
  575. case NF4SOCK:
  576. status = nfsd_create(rqstp, &cstate->current_fh,
  577. create->cr_name, create->cr_namelen,
  578. &create->cr_iattr, S_IFSOCK, 0, &resfh);
  579. break;
  580. case NF4FIFO:
  581. status = nfsd_create(rqstp, &cstate->current_fh,
  582. create->cr_name, create->cr_namelen,
  583. &create->cr_iattr, S_IFIFO, 0, &resfh);
  584. break;
  585. case NF4DIR:
  586. create->cr_iattr.ia_valid &= ~ATTR_SIZE;
  587. status = nfsd_create(rqstp, &cstate->current_fh,
  588. create->cr_name, create->cr_namelen,
  589. &create->cr_iattr, S_IFDIR, 0, &resfh);
  590. break;
  591. default:
  592. status = nfserr_badtype;
  593. }
  594. if (status)
  595. goto out;
  596. if (create->cr_label.len)
  597. nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
  598. if (create->cr_acl != NULL)
  599. do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
  600. create->cr_bmval);
  601. fh_unlock(&cstate->current_fh);
  602. set_change_info(&create->cr_cinfo, &cstate->current_fh);
  603. fh_dup2(&cstate->current_fh, &resfh);
  604. out:
  605. fh_put(&resfh);
  606. out_umask:
  607. current->fs->umask = 0;
  608. return status;
  609. }
  610. static __be32
  611. nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  612. union nfsd4_op_u *u)
  613. {
  614. struct nfsd4_getattr *getattr = &u->getattr;
  615. __be32 status;
  616. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  617. if (status)
  618. return status;
  619. if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  620. return nfserr_inval;
  621. getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
  622. getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
  623. getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
  624. getattr->ga_fhp = &cstate->current_fh;
  625. return nfs_ok;
  626. }
  627. static __be32
  628. nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  629. union nfsd4_op_u *u)
  630. {
  631. struct nfsd4_link *link = &u->link;
  632. __be32 status;
  633. status = nfsd_link(rqstp, &cstate->current_fh,
  634. link->li_name, link->li_namelen, &cstate->save_fh);
  635. if (!status)
  636. set_change_info(&link->li_cinfo, &cstate->current_fh);
  637. return status;
  638. }
  639. static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
  640. {
  641. struct svc_fh tmp_fh;
  642. __be32 ret;
  643. fh_init(&tmp_fh, NFS4_FHSIZE);
  644. ret = exp_pseudoroot(rqstp, &tmp_fh);
  645. if (ret)
  646. return ret;
  647. if (tmp_fh.fh_dentry == fh->fh_dentry) {
  648. fh_put(&tmp_fh);
  649. return nfserr_noent;
  650. }
  651. fh_put(&tmp_fh);
  652. return nfsd_lookup(rqstp, fh, "..", 2, fh);
  653. }
  654. static __be32
  655. nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  656. union nfsd4_op_u *u)
  657. {
  658. return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  659. }
  660. static __be32
  661. nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  662. union nfsd4_op_u *u)
  663. {
  664. return nfsd_lookup(rqstp, &cstate->current_fh,
  665. u->lookup.lo_name, u->lookup.lo_len,
  666. &cstate->current_fh);
  667. }
  668. static __be32
  669. nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  670. union nfsd4_op_u *u)
  671. {
  672. struct nfsd4_read *read = &u->read;
  673. __be32 status;
  674. read->rd_nf = NULL;
  675. if (read->rd_offset >= OFFSET_MAX)
  676. return nfserr_inval;
  677. trace_nfsd_read_start(rqstp, &cstate->current_fh,
  678. read->rd_offset, read->rd_length);
  679. /*
  680. * If we do a zero copy read, then a client will see read data
  681. * that reflects the state of the file *after* performing the
  682. * following compound.
  683. *
  684. * To ensure proper ordering, we therefore turn off zero copy if
  685. * the client wants us to do more in this compound:
  686. */
  687. if (!nfsd4_last_compound_op(rqstp))
  688. clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
  689. /* check stateid */
  690. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  691. &read->rd_stateid, RD_STATE,
  692. &read->rd_nf, NULL);
  693. if (status) {
  694. dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
  695. goto out;
  696. }
  697. status = nfs_ok;
  698. out:
  699. read->rd_rqstp = rqstp;
  700. read->rd_fhp = &cstate->current_fh;
  701. return status;
  702. }
  703. static void
  704. nfsd4_read_release(union nfsd4_op_u *u)
  705. {
  706. if (u->read.rd_nf)
  707. nfsd_file_put(u->read.rd_nf);
  708. trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
  709. u->read.rd_offset, u->read.rd_length);
  710. }
  711. static __be32
  712. nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  713. union nfsd4_op_u *u)
  714. {
  715. struct nfsd4_readdir *readdir = &u->readdir;
  716. u64 cookie = readdir->rd_cookie;
  717. static const nfs4_verifier zeroverf;
  718. /* no need to check permission - this will be done in nfsd_readdir() */
  719. if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
  720. return nfserr_inval;
  721. readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
  722. readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
  723. readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
  724. if ((cookie == 1) || (cookie == 2) ||
  725. (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
  726. return nfserr_bad_cookie;
  727. readdir->rd_rqstp = rqstp;
  728. readdir->rd_fhp = &cstate->current_fh;
  729. return nfs_ok;
  730. }
  731. static __be32
  732. nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  733. union nfsd4_op_u *u)
  734. {
  735. u->readlink.rl_rqstp = rqstp;
  736. u->readlink.rl_fhp = &cstate->current_fh;
  737. return nfs_ok;
  738. }
  739. static __be32
  740. nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  741. union nfsd4_op_u *u)
  742. {
  743. struct nfsd4_remove *remove = &u->remove;
  744. __be32 status;
  745. if (opens_in_grace(SVC_NET(rqstp)))
  746. return nfserr_grace;
  747. status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
  748. remove->rm_name, remove->rm_namelen);
  749. if (!status) {
  750. fh_unlock(&cstate->current_fh);
  751. set_change_info(&remove->rm_cinfo, &cstate->current_fh);
  752. }
  753. return status;
  754. }
  755. static __be32
  756. nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  757. union nfsd4_op_u *u)
  758. {
  759. struct nfsd4_rename *rename = &u->rename;
  760. __be32 status;
  761. if (opens_in_grace(SVC_NET(rqstp)))
  762. return nfserr_grace;
  763. status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
  764. rename->rn_snamelen, &cstate->current_fh,
  765. rename->rn_tname, rename->rn_tnamelen);
  766. if (status)
  767. return status;
  768. set_change_info(&rename->rn_sinfo, &cstate->current_fh);
  769. set_change_info(&rename->rn_tinfo, &cstate->save_fh);
  770. return nfs_ok;
  771. }
  772. static __be32
  773. nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  774. union nfsd4_op_u *u)
  775. {
  776. struct nfsd4_secinfo *secinfo = &u->secinfo;
  777. struct svc_export *exp;
  778. struct dentry *dentry;
  779. __be32 err;
  780. err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
  781. if (err)
  782. return err;
  783. err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
  784. secinfo->si_name, secinfo->si_namelen,
  785. &exp, &dentry);
  786. if (err)
  787. return err;
  788. fh_unlock(&cstate->current_fh);
  789. if (d_really_is_negative(dentry)) {
  790. exp_put(exp);
  791. err = nfserr_noent;
  792. } else
  793. secinfo->si_exp = exp;
  794. dput(dentry);
  795. if (cstate->minorversion)
  796. /* See rfc 5661 section 2.6.3.1.1.8 */
  797. fh_put(&cstate->current_fh);
  798. return err;
  799. }
  800. static __be32
  801. nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  802. union nfsd4_op_u *u)
  803. {
  804. __be32 err;
  805. switch (u->secinfo_no_name.sin_style) {
  806. case NFS4_SECINFO_STYLE4_CURRENT_FH:
  807. break;
  808. case NFS4_SECINFO_STYLE4_PARENT:
  809. err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
  810. if (err)
  811. return err;
  812. break;
  813. default:
  814. return nfserr_inval;
  815. }
  816. u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
  817. fh_put(&cstate->current_fh);
  818. return nfs_ok;
  819. }
  820. static void
  821. nfsd4_secinfo_release(union nfsd4_op_u *u)
  822. {
  823. if (u->secinfo.si_exp)
  824. exp_put(u->secinfo.si_exp);
  825. }
  826. static void
  827. nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
  828. {
  829. if (u->secinfo_no_name.sin_exp)
  830. exp_put(u->secinfo_no_name.sin_exp);
  831. }
  832. static __be32
  833. nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  834. union nfsd4_op_u *u)
  835. {
  836. struct nfsd4_setattr *setattr = &u->setattr;
  837. __be32 status = nfs_ok;
  838. int err;
  839. if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
  840. status = nfs4_preprocess_stateid_op(rqstp, cstate,
  841. &cstate->current_fh, &setattr->sa_stateid,
  842. WR_STATE, NULL, NULL);
  843. if (status) {
  844. dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
  845. return status;
  846. }
  847. }
  848. err = fh_want_write(&cstate->current_fh);
  849. if (err)
  850. return nfserrno(err);
  851. status = nfs_ok;
  852. status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
  853. nfsd_attrmask);
  854. if (status)
  855. goto out;
  856. if (setattr->sa_acl != NULL)
  857. status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
  858. setattr->sa_acl);
  859. if (status)
  860. goto out;
  861. if (setattr->sa_label.len)
  862. status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
  863. &setattr->sa_label);
  864. if (status)
  865. goto out;
  866. status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
  867. 0, (time64_t)0);
  868. out:
  869. fh_drop_write(&cstate->current_fh);
  870. return status;
  871. }
  872. static __be32
  873. nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  874. union nfsd4_op_u *u)
  875. {
  876. struct nfsd4_write *write = &u->write;
  877. stateid_t *stateid = &write->wr_stateid;
  878. struct nfsd_file *nf = NULL;
  879. __be32 status = nfs_ok;
  880. unsigned long cnt;
  881. int nvecs;
  882. if (write->wr_offset > (u64)OFFSET_MAX ||
  883. write->wr_offset + write->wr_buflen > (u64)OFFSET_MAX)
  884. return nfserr_fbig;
  885. cnt = write->wr_buflen;
  886. trace_nfsd_write_start(rqstp, &cstate->current_fh,
  887. write->wr_offset, cnt);
  888. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  889. stateid, WR_STATE, &nf, NULL);
  890. if (status) {
  891. dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
  892. return status;
  893. }
  894. write->wr_how_written = write->wr_stable_how;
  895. nvecs = svc_fill_write_vector(rqstp, write->wr_pagelist,
  896. &write->wr_head, write->wr_buflen);
  897. WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
  898. status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf,
  899. write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
  900. write->wr_how_written,
  901. (__be32 *)write->wr_verifier.data);
  902. nfsd_file_put(nf);
  903. write->wr_bytes_written = cnt;
  904. trace_nfsd_write_done(rqstp, &cstate->current_fh,
  905. write->wr_offset, cnt);
  906. return status;
  907. }
  908. static __be32
  909. nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  910. stateid_t *src_stateid, struct nfsd_file **src,
  911. stateid_t *dst_stateid, struct nfsd_file **dst)
  912. {
  913. __be32 status;
  914. if (!cstate->save_fh.fh_dentry)
  915. return nfserr_nofilehandle;
  916. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
  917. src_stateid, RD_STATE, src, NULL);
  918. if (status) {
  919. dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
  920. goto out;
  921. }
  922. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  923. dst_stateid, WR_STATE, dst, NULL);
  924. if (status) {
  925. dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
  926. goto out_put_src;
  927. }
  928. /* fix up for NFS-specific error code */
  929. if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
  930. !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
  931. status = nfserr_wrong_type;
  932. goto out_put_dst;
  933. }
  934. out:
  935. return status;
  936. out_put_dst:
  937. nfsd_file_put(*dst);
  938. out_put_src:
  939. nfsd_file_put(*src);
  940. goto out;
  941. }
  942. static __be32
  943. nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  944. union nfsd4_op_u *u)
  945. {
  946. struct nfsd4_clone *clone = &u->clone;
  947. struct nfsd_file *src, *dst;
  948. __be32 status;
  949. status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
  950. &clone->cl_dst_stateid, &dst);
  951. if (status)
  952. goto out;
  953. status = nfsd4_clone_file_range(src, clone->cl_src_pos,
  954. dst, clone->cl_dst_pos, clone->cl_count,
  955. EX_ISSYNC(cstate->current_fh.fh_export));
  956. nfsd_file_put(dst);
  957. nfsd_file_put(src);
  958. out:
  959. return status;
  960. }
  961. void nfs4_put_copy(struct nfsd4_copy *copy)
  962. {
  963. if (!refcount_dec_and_test(&copy->refcount))
  964. return;
  965. kfree(copy);
  966. }
  967. static bool
  968. check_and_set_stop_copy(struct nfsd4_copy *copy)
  969. {
  970. bool value;
  971. spin_lock(&copy->cp_clp->async_lock);
  972. value = copy->stopped;
  973. if (!copy->stopped)
  974. copy->stopped = true;
  975. spin_unlock(&copy->cp_clp->async_lock);
  976. return value;
  977. }
  978. static void nfsd4_stop_copy(struct nfsd4_copy *copy)
  979. {
  980. /* only 1 thread should stop the copy */
  981. if (!check_and_set_stop_copy(copy))
  982. kthread_stop(copy->copy_task);
  983. nfs4_put_copy(copy);
  984. }
  985. static struct nfsd4_copy *nfsd4_get_copy(struct nfs4_client *clp)
  986. {
  987. struct nfsd4_copy *copy = NULL;
  988. spin_lock(&clp->async_lock);
  989. if (!list_empty(&clp->async_copies)) {
  990. copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
  991. copies);
  992. refcount_inc(&copy->refcount);
  993. }
  994. spin_unlock(&clp->async_lock);
  995. return copy;
  996. }
  997. void nfsd4_shutdown_copy(struct nfs4_client *clp)
  998. {
  999. struct nfsd4_copy *copy;
  1000. while ((copy = nfsd4_get_copy(clp)) != NULL)
  1001. nfsd4_stop_copy(copy);
  1002. }
  1003. #ifdef CONFIG_NFSD_V4_2_INTER_SSC
  1004. extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
  1005. struct nfs_fh *src_fh,
  1006. nfs4_stateid *stateid);
  1007. extern void nfs42_ssc_close(struct file *filep);
  1008. extern void nfs_sb_deactive(struct super_block *sb);
  1009. #define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys"
  1010. /*
  1011. * Support one copy source server for now.
  1012. */
  1013. static __be32
  1014. nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp,
  1015. struct vfsmount **mount)
  1016. {
  1017. struct file_system_type *type;
  1018. struct vfsmount *ss_mnt;
  1019. struct nfs42_netaddr *naddr;
  1020. struct sockaddr_storage tmp_addr;
  1021. size_t tmp_addrlen, match_netid_len = 3;
  1022. char *startsep = "", *endsep = "", *match_netid = "tcp";
  1023. char *ipaddr, *dev_name, *raw_data;
  1024. int len, raw_len;
  1025. __be32 status = nfserr_inval;
  1026. naddr = &nss->u.nl4_addr;
  1027. tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr,
  1028. naddr->addr_len,
  1029. (struct sockaddr *)&tmp_addr,
  1030. sizeof(tmp_addr));
  1031. if (tmp_addrlen == 0)
  1032. goto out_err;
  1033. if (tmp_addr.ss_family == AF_INET6) {
  1034. startsep = "[";
  1035. endsep = "]";
  1036. match_netid = "tcp6";
  1037. match_netid_len = 4;
  1038. }
  1039. if (naddr->netid_len != match_netid_len ||
  1040. strncmp(naddr->netid, match_netid, naddr->netid_len))
  1041. goto out_err;
  1042. /* Construct the raw data for the vfs_kern_mount call */
  1043. len = RPC_MAX_ADDRBUFLEN + 1;
  1044. ipaddr = kzalloc(len, GFP_KERNEL);
  1045. if (!ipaddr)
  1046. goto out_err;
  1047. rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len);
  1048. /* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/
  1049. raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr);
  1050. raw_data = kzalloc(raw_len, GFP_KERNEL);
  1051. if (!raw_data)
  1052. goto out_free_ipaddr;
  1053. snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr);
  1054. status = nfserr_nodev;
  1055. type = get_fs_type("nfs");
  1056. if (!type)
  1057. goto out_free_rawdata;
  1058. /* Set the server:<export> for the vfs_kern_mount call */
  1059. dev_name = kzalloc(len + 5, GFP_KERNEL);
  1060. if (!dev_name)
  1061. goto out_free_rawdata;
  1062. snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep);
  1063. /* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */
  1064. ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data);
  1065. module_put(type->owner);
  1066. if (IS_ERR(ss_mnt))
  1067. goto out_free_devname;
  1068. status = 0;
  1069. *mount = ss_mnt;
  1070. out_free_devname:
  1071. kfree(dev_name);
  1072. out_free_rawdata:
  1073. kfree(raw_data);
  1074. out_free_ipaddr:
  1075. kfree(ipaddr);
  1076. out_err:
  1077. return status;
  1078. }
  1079. static void
  1080. nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
  1081. {
  1082. nfs_do_sb_deactive(ss_mnt->mnt_sb);
  1083. mntput(ss_mnt);
  1084. }
  1085. /*
  1086. * Verify COPY destination stateid.
  1087. *
  1088. * Connect to the source server with NFSv4.1.
  1089. * Create the source struct file for nfsd_copy_range.
  1090. * Called with COPY cstate:
  1091. * SAVED_FH: source filehandle
  1092. * CURRENT_FH: destination filehandle
  1093. */
  1094. static __be32
  1095. nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
  1096. struct nfsd4_compound_state *cstate,
  1097. struct nfsd4_copy *copy, struct vfsmount **mount)
  1098. {
  1099. struct svc_fh *s_fh = NULL;
  1100. stateid_t *s_stid = &copy->cp_src_stateid;
  1101. __be32 status = nfserr_inval;
  1102. /* Verify the destination stateid and set dst struct file*/
  1103. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  1104. &copy->cp_dst_stateid,
  1105. WR_STATE, &copy->nf_dst, NULL);
  1106. if (status)
  1107. goto out;
  1108. status = nfsd4_interssc_connect(&copy->cp_src, rqstp, mount);
  1109. if (status)
  1110. goto out;
  1111. s_fh = &cstate->save_fh;
  1112. copy->c_fh.size = s_fh->fh_handle.fh_size;
  1113. memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_base, copy->c_fh.size);
  1114. copy->stateid.seqid = cpu_to_be32(s_stid->si_generation);
  1115. memcpy(copy->stateid.other, (void *)&s_stid->si_opaque,
  1116. sizeof(stateid_opaque_t));
  1117. status = 0;
  1118. out:
  1119. return status;
  1120. }
  1121. static void
  1122. nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct nfsd_file *src,
  1123. struct nfsd_file *dst)
  1124. {
  1125. nfs42_ssc_close(src->nf_file);
  1126. fput(src->nf_file);
  1127. nfsd_file_put(dst);
  1128. mntput(ss_mnt);
  1129. }
  1130. #else /* CONFIG_NFSD_V4_2_INTER_SSC */
  1131. static __be32
  1132. nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
  1133. struct nfsd4_compound_state *cstate,
  1134. struct nfsd4_copy *copy,
  1135. struct vfsmount **mount)
  1136. {
  1137. *mount = NULL;
  1138. return nfserr_inval;
  1139. }
  1140. static void
  1141. nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct nfsd_file *src,
  1142. struct nfsd_file *dst)
  1143. {
  1144. }
  1145. static void
  1146. nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
  1147. {
  1148. }
  1149. static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
  1150. struct nfs_fh *src_fh,
  1151. nfs4_stateid *stateid)
  1152. {
  1153. return NULL;
  1154. }
  1155. #endif /* CONFIG_NFSD_V4_2_INTER_SSC */
  1156. static __be32
  1157. nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
  1158. struct nfsd4_compound_state *cstate,
  1159. struct nfsd4_copy *copy)
  1160. {
  1161. return nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
  1162. &copy->nf_src, &copy->cp_dst_stateid,
  1163. &copy->nf_dst);
  1164. }
  1165. static void
  1166. nfsd4_cleanup_intra_ssc(struct nfsd_file *src, struct nfsd_file *dst)
  1167. {
  1168. nfsd_file_put(src);
  1169. nfsd_file_put(dst);
  1170. }
  1171. static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
  1172. {
  1173. struct nfsd4_copy *copy = container_of(cb, struct nfsd4_copy, cp_cb);
  1174. nfs4_put_copy(copy);
  1175. }
  1176. static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
  1177. struct rpc_task *task)
  1178. {
  1179. return 1;
  1180. }
  1181. static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
  1182. .release = nfsd4_cb_offload_release,
  1183. .done = nfsd4_cb_offload_done
  1184. };
  1185. static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
  1186. {
  1187. copy->cp_res.wr_stable_how = NFS_UNSTABLE;
  1188. copy->cp_synchronous = sync;
  1189. gen_boot_verifier(&copy->cp_res.wr_verifier, copy->cp_clp->net);
  1190. }
  1191. static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy)
  1192. {
  1193. ssize_t bytes_copied = 0;
  1194. size_t bytes_total = copy->cp_count;
  1195. u64 src_pos = copy->cp_src_pos;
  1196. u64 dst_pos = copy->cp_dst_pos;
  1197. do {
  1198. if (kthread_should_stop())
  1199. break;
  1200. bytes_copied = nfsd_copy_file_range(copy->nf_src->nf_file,
  1201. src_pos, copy->nf_dst->nf_file, dst_pos,
  1202. bytes_total);
  1203. if (bytes_copied <= 0)
  1204. break;
  1205. bytes_total -= bytes_copied;
  1206. copy->cp_res.wr_bytes_written += bytes_copied;
  1207. src_pos += bytes_copied;
  1208. dst_pos += bytes_copied;
  1209. } while (bytes_total > 0 && !copy->cp_synchronous);
  1210. return bytes_copied;
  1211. }
  1212. static __be32 nfsd4_do_copy(struct nfsd4_copy *copy, bool sync)
  1213. {
  1214. __be32 status;
  1215. ssize_t bytes;
  1216. bytes = _nfsd_copy_file_range(copy);
  1217. /* for async copy, we ignore the error, client can always retry
  1218. * to get the error
  1219. */
  1220. if (bytes < 0 && !copy->cp_res.wr_bytes_written)
  1221. status = nfserrno(bytes);
  1222. else {
  1223. nfsd4_init_copy_res(copy, sync);
  1224. status = nfs_ok;
  1225. }
  1226. if (!copy->cp_intra) /* Inter server SSC */
  1227. nfsd4_cleanup_inter_ssc(copy->ss_mnt, copy->nf_src,
  1228. copy->nf_dst);
  1229. else
  1230. nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst);
  1231. return status;
  1232. }
  1233. static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
  1234. {
  1235. dst->cp_src_pos = src->cp_src_pos;
  1236. dst->cp_dst_pos = src->cp_dst_pos;
  1237. dst->cp_count = src->cp_count;
  1238. dst->cp_synchronous = src->cp_synchronous;
  1239. memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
  1240. memcpy(&dst->fh, &src->fh, sizeof(src->fh));
  1241. dst->cp_clp = src->cp_clp;
  1242. dst->nf_dst = nfsd_file_get(src->nf_dst);
  1243. dst->cp_intra = src->cp_intra;
  1244. if (src->cp_intra) /* for inter, file_src doesn't exist yet */
  1245. dst->nf_src = nfsd_file_get(src->nf_src);
  1246. memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
  1247. memcpy(&dst->cp_src, &src->cp_src, sizeof(struct nl4_server));
  1248. memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid));
  1249. memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh));
  1250. dst->ss_mnt = src->ss_mnt;
  1251. }
  1252. static void cleanup_async_copy(struct nfsd4_copy *copy)
  1253. {
  1254. nfs4_free_copy_state(copy);
  1255. nfsd_file_put(copy->nf_dst);
  1256. if (copy->cp_intra)
  1257. nfsd_file_put(copy->nf_src);
  1258. spin_lock(&copy->cp_clp->async_lock);
  1259. list_del(&copy->copies);
  1260. spin_unlock(&copy->cp_clp->async_lock);
  1261. nfs4_put_copy(copy);
  1262. }
  1263. static int nfsd4_do_async_copy(void *data)
  1264. {
  1265. struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
  1266. struct nfsd4_copy *cb_copy;
  1267. if (!copy->cp_intra) { /* Inter server SSC */
  1268. copy->nf_src = kzalloc(sizeof(struct nfsd_file), GFP_KERNEL);
  1269. if (!copy->nf_src) {
  1270. copy->nfserr = nfserr_serverfault;
  1271. nfsd4_interssc_disconnect(copy->ss_mnt);
  1272. goto do_callback;
  1273. }
  1274. copy->nf_src->nf_file = nfs42_ssc_open(copy->ss_mnt, &copy->c_fh,
  1275. &copy->stateid);
  1276. if (IS_ERR(copy->nf_src->nf_file)) {
  1277. copy->nfserr = nfserr_offload_denied;
  1278. nfsd4_interssc_disconnect(copy->ss_mnt);
  1279. goto do_callback;
  1280. }
  1281. }
  1282. copy->nfserr = nfsd4_do_copy(copy, 0);
  1283. do_callback:
  1284. cb_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
  1285. if (!cb_copy)
  1286. goto out;
  1287. refcount_set(&cb_copy->refcount, 1);
  1288. memcpy(&cb_copy->cp_res, &copy->cp_res, sizeof(copy->cp_res));
  1289. cb_copy->cp_clp = copy->cp_clp;
  1290. cb_copy->nfserr = copy->nfserr;
  1291. memcpy(&cb_copy->fh, &copy->fh, sizeof(copy->fh));
  1292. nfsd4_init_cb(&cb_copy->cp_cb, cb_copy->cp_clp,
  1293. &nfsd4_cb_offload_ops, NFSPROC4_CLNT_CB_OFFLOAD);
  1294. nfsd4_run_cb(&cb_copy->cp_cb);
  1295. out:
  1296. if (!copy->cp_intra)
  1297. kfree(copy->nf_src);
  1298. cleanup_async_copy(copy);
  1299. return 0;
  1300. }
  1301. static __be32
  1302. nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1303. union nfsd4_op_u *u)
  1304. {
  1305. struct nfsd4_copy *copy = &u->copy;
  1306. __be32 status;
  1307. struct nfsd4_copy *async_copy = NULL;
  1308. if (!copy->cp_intra) { /* Inter server SSC */
  1309. if (!inter_copy_offload_enable || copy->cp_synchronous) {
  1310. status = nfserr_notsupp;
  1311. goto out;
  1312. }
  1313. status = nfsd4_setup_inter_ssc(rqstp, cstate, copy,
  1314. &copy->ss_mnt);
  1315. if (status)
  1316. return nfserr_offload_denied;
  1317. } else {
  1318. status = nfsd4_setup_intra_ssc(rqstp, cstate, copy);
  1319. if (status)
  1320. return status;
  1321. }
  1322. copy->cp_clp = cstate->clp;
  1323. memcpy(&copy->fh, &cstate->current_fh.fh_handle,
  1324. sizeof(struct knfsd_fh));
  1325. if (!copy->cp_synchronous) {
  1326. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1327. status = nfserrno(-ENOMEM);
  1328. async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
  1329. if (!async_copy)
  1330. goto out_err;
  1331. if (!nfs4_init_copy_state(nn, copy))
  1332. goto out_err;
  1333. refcount_set(&async_copy->refcount, 1);
  1334. memcpy(&copy->cp_res.cb_stateid, &copy->cp_stateid.stid,
  1335. sizeof(copy->cp_res.cb_stateid));
  1336. dup_copy_fields(copy, async_copy);
  1337. async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
  1338. async_copy, "%s", "copy thread");
  1339. if (IS_ERR(async_copy->copy_task))
  1340. goto out_err;
  1341. spin_lock(&async_copy->cp_clp->async_lock);
  1342. list_add(&async_copy->copies,
  1343. &async_copy->cp_clp->async_copies);
  1344. spin_unlock(&async_copy->cp_clp->async_lock);
  1345. wake_up_process(async_copy->copy_task);
  1346. status = nfs_ok;
  1347. } else {
  1348. status = nfsd4_do_copy(copy, 1);
  1349. }
  1350. out:
  1351. return status;
  1352. out_err:
  1353. if (async_copy)
  1354. cleanup_async_copy(async_copy);
  1355. status = nfserrno(-ENOMEM);
  1356. if (!copy->cp_intra)
  1357. nfsd4_interssc_disconnect(copy->ss_mnt);
  1358. goto out;
  1359. }
  1360. struct nfsd4_copy *
  1361. find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
  1362. {
  1363. struct nfsd4_copy *copy;
  1364. spin_lock(&clp->async_lock);
  1365. list_for_each_entry(copy, &clp->async_copies, copies) {
  1366. if (memcmp(&copy->cp_stateid.stid, stateid, NFS4_STATEID_SIZE))
  1367. continue;
  1368. refcount_inc(&copy->refcount);
  1369. spin_unlock(&clp->async_lock);
  1370. return copy;
  1371. }
  1372. spin_unlock(&clp->async_lock);
  1373. return NULL;
  1374. }
  1375. static __be32
  1376. nfsd4_offload_cancel(struct svc_rqst *rqstp,
  1377. struct nfsd4_compound_state *cstate,
  1378. union nfsd4_op_u *u)
  1379. {
  1380. struct nfsd4_offload_status *os = &u->offload_status;
  1381. struct nfsd4_copy *copy;
  1382. struct nfs4_client *clp = cstate->clp;
  1383. copy = find_async_copy(clp, &os->stateid);
  1384. if (!copy) {
  1385. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1386. return manage_cpntf_state(nn, &os->stateid, clp, NULL);
  1387. } else
  1388. nfsd4_stop_copy(copy);
  1389. return nfs_ok;
  1390. }
  1391. static __be32
  1392. nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1393. union nfsd4_op_u *u)
  1394. {
  1395. struct nfsd4_copy_notify *cn = &u->copy_notify;
  1396. __be32 status;
  1397. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1398. struct nfs4_stid *stid;
  1399. struct nfs4_cpntf_state *cps;
  1400. struct nfs4_client *clp = cstate->clp;
  1401. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  1402. &cn->cpn_src_stateid, RD_STATE, NULL,
  1403. &stid);
  1404. if (status)
  1405. return status;
  1406. cn->cpn_sec = nn->nfsd4_lease;
  1407. cn->cpn_nsec = 0;
  1408. status = nfserrno(-ENOMEM);
  1409. cps = nfs4_alloc_init_cpntf_state(nn, stid);
  1410. if (!cps)
  1411. goto out;
  1412. memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.stid, sizeof(stateid_t));
  1413. memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t));
  1414. memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t));
  1415. /* For now, only return one server address in cpn_src, the
  1416. * address used by the client to connect to this server.
  1417. */
  1418. cn->cpn_src.nl4_type = NL4_NETADDR;
  1419. status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr,
  1420. &cn->cpn_src.u.nl4_addr);
  1421. WARN_ON_ONCE(status);
  1422. if (status) {
  1423. nfs4_put_cpntf_state(nn, cps);
  1424. goto out;
  1425. }
  1426. out:
  1427. nfs4_put_stid(stid);
  1428. return status;
  1429. }
  1430. static __be32
  1431. nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1432. struct nfsd4_fallocate *fallocate, int flags)
  1433. {
  1434. __be32 status;
  1435. struct nfsd_file *nf;
  1436. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  1437. &fallocate->falloc_stateid,
  1438. WR_STATE, &nf, NULL);
  1439. if (status != nfs_ok) {
  1440. dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
  1441. return status;
  1442. }
  1443. status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
  1444. fallocate->falloc_offset,
  1445. fallocate->falloc_length,
  1446. flags);
  1447. nfsd_file_put(nf);
  1448. return status;
  1449. }
  1450. static __be32
  1451. nfsd4_offload_status(struct svc_rqst *rqstp,
  1452. struct nfsd4_compound_state *cstate,
  1453. union nfsd4_op_u *u)
  1454. {
  1455. struct nfsd4_offload_status *os = &u->offload_status;
  1456. __be32 status = 0;
  1457. struct nfsd4_copy *copy;
  1458. struct nfs4_client *clp = cstate->clp;
  1459. copy = find_async_copy(clp, &os->stateid);
  1460. if (copy) {
  1461. os->count = copy->cp_res.wr_bytes_written;
  1462. nfs4_put_copy(copy);
  1463. } else
  1464. status = nfserr_bad_stateid;
  1465. return status;
  1466. }
  1467. static __be32
  1468. nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1469. union nfsd4_op_u *u)
  1470. {
  1471. return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
  1472. }
  1473. static __be32
  1474. nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1475. union nfsd4_op_u *u)
  1476. {
  1477. return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
  1478. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
  1479. }
  1480. static __be32
  1481. nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1482. union nfsd4_op_u *u)
  1483. {
  1484. struct nfsd4_seek *seek = &u->seek;
  1485. int whence;
  1486. __be32 status;
  1487. struct nfsd_file *nf;
  1488. status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
  1489. &seek->seek_stateid,
  1490. RD_STATE, &nf, NULL);
  1491. if (status) {
  1492. dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
  1493. return status;
  1494. }
  1495. switch (seek->seek_whence) {
  1496. case NFS4_CONTENT_DATA:
  1497. whence = SEEK_DATA;
  1498. break;
  1499. case NFS4_CONTENT_HOLE:
  1500. whence = SEEK_HOLE;
  1501. break;
  1502. default:
  1503. status = nfserr_union_notsupp;
  1504. goto out;
  1505. }
  1506. /*
  1507. * Note: This call does change file->f_pos, but nothing in NFSD
  1508. * should ever file->f_pos.
  1509. */
  1510. seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
  1511. if (seek->seek_pos < 0)
  1512. status = nfserrno(seek->seek_pos);
  1513. else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
  1514. seek->seek_eof = true;
  1515. out:
  1516. nfsd_file_put(nf);
  1517. return status;
  1518. }
  1519. /* This routine never returns NFS_OK! If there are no other errors, it
  1520. * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
  1521. * attributes matched. VERIFY is implemented by mapping NFSERR_SAME
  1522. * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
  1523. */
  1524. static __be32
  1525. _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1526. struct nfsd4_verify *verify)
  1527. {
  1528. __be32 *buf, *p;
  1529. int count;
  1530. __be32 status;
  1531. status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
  1532. if (status)
  1533. return status;
  1534. status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
  1535. if (status)
  1536. return status;
  1537. if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
  1538. || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
  1539. return nfserr_inval;
  1540. if (verify->ve_attrlen & 3)
  1541. return nfserr_inval;
  1542. /* count in words:
  1543. * bitmap_len(1) + bitmap(2) + attr_len(1) = 4
  1544. */
  1545. count = 4 + (verify->ve_attrlen >> 2);
  1546. buf = kmalloc(count << 2, GFP_KERNEL);
  1547. if (!buf)
  1548. return nfserr_jukebox;
  1549. p = buf;
  1550. status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
  1551. cstate->current_fh.fh_export,
  1552. cstate->current_fh.fh_dentry,
  1553. verify->ve_bmval,
  1554. rqstp, 0);
  1555. /*
  1556. * If nfsd4_encode_fattr() ran out of space, assume that's because
  1557. * the attributes are longer (hence different) than those given:
  1558. */
  1559. if (status == nfserr_resource)
  1560. status = nfserr_not_same;
  1561. if (status)
  1562. goto out_kfree;
  1563. /* skip bitmap */
  1564. p = buf + 1 + ntohl(buf[0]);
  1565. status = nfserr_not_same;
  1566. if (ntohl(*p++) != verify->ve_attrlen)
  1567. goto out_kfree;
  1568. if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
  1569. status = nfserr_same;
  1570. out_kfree:
  1571. kfree(buf);
  1572. return status;
  1573. }
  1574. static __be32
  1575. nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1576. union nfsd4_op_u *u)
  1577. {
  1578. __be32 status;
  1579. status = _nfsd4_verify(rqstp, cstate, &u->verify);
  1580. return status == nfserr_not_same ? nfs_ok : status;
  1581. }
  1582. static __be32
  1583. nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1584. union nfsd4_op_u *u)
  1585. {
  1586. __be32 status;
  1587. status = _nfsd4_verify(rqstp, cstate, &u->nverify);
  1588. return status == nfserr_same ? nfs_ok : status;
  1589. }
  1590. #ifdef CONFIG_NFSD_PNFS
  1591. static const struct nfsd4_layout_ops *
  1592. nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
  1593. {
  1594. if (!exp->ex_layout_types) {
  1595. dprintk("%s: export does not support pNFS\n", __func__);
  1596. return NULL;
  1597. }
  1598. if (layout_type >= LAYOUT_TYPE_MAX ||
  1599. !(exp->ex_layout_types & (1 << layout_type))) {
  1600. dprintk("%s: layout type %d not supported\n",
  1601. __func__, layout_type);
  1602. return NULL;
  1603. }
  1604. return nfsd4_layout_ops[layout_type];
  1605. }
  1606. static __be32
  1607. nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
  1608. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1609. {
  1610. struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
  1611. const struct nfsd4_layout_ops *ops;
  1612. struct nfsd4_deviceid_map *map;
  1613. struct svc_export *exp;
  1614. __be32 nfserr;
  1615. dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
  1616. __func__,
  1617. gdp->gd_layout_type,
  1618. gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
  1619. gdp->gd_maxcount);
  1620. map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
  1621. if (!map) {
  1622. dprintk("%s: couldn't find device ID to export mapping!\n",
  1623. __func__);
  1624. return nfserr_noent;
  1625. }
  1626. exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
  1627. if (IS_ERR(exp)) {
  1628. dprintk("%s: could not find device id\n", __func__);
  1629. return nfserr_noent;
  1630. }
  1631. nfserr = nfserr_layoutunavailable;
  1632. ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
  1633. if (!ops)
  1634. goto out;
  1635. nfserr = nfs_ok;
  1636. if (gdp->gd_maxcount != 0) {
  1637. nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
  1638. rqstp, cstate->session->se_client, gdp);
  1639. }
  1640. gdp->gd_notify_types &= ops->notify_types;
  1641. out:
  1642. exp_put(exp);
  1643. return nfserr;
  1644. }
  1645. static void
  1646. nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
  1647. {
  1648. kfree(u->getdeviceinfo.gd_device);
  1649. }
  1650. static __be32
  1651. nfsd4_layoutget(struct svc_rqst *rqstp,
  1652. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1653. {
  1654. struct nfsd4_layoutget *lgp = &u->layoutget;
  1655. struct svc_fh *current_fh = &cstate->current_fh;
  1656. const struct nfsd4_layout_ops *ops;
  1657. struct nfs4_layout_stateid *ls;
  1658. __be32 nfserr;
  1659. int accmode = NFSD_MAY_READ_IF_EXEC;
  1660. switch (lgp->lg_seg.iomode) {
  1661. case IOMODE_READ:
  1662. accmode |= NFSD_MAY_READ;
  1663. break;
  1664. case IOMODE_RW:
  1665. accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
  1666. break;
  1667. default:
  1668. dprintk("%s: invalid iomode %d\n",
  1669. __func__, lgp->lg_seg.iomode);
  1670. nfserr = nfserr_badiomode;
  1671. goto out;
  1672. }
  1673. nfserr = fh_verify(rqstp, current_fh, 0, accmode);
  1674. if (nfserr)
  1675. goto out;
  1676. nfserr = nfserr_layoutunavailable;
  1677. ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
  1678. if (!ops)
  1679. goto out;
  1680. /*
  1681. * Verify minlength and range as per RFC5661:
  1682. * o If loga_length is less than loga_minlength,
  1683. * the metadata server MUST return NFS4ERR_INVAL.
  1684. * o If the sum of loga_offset and loga_minlength exceeds
  1685. * NFS4_UINT64_MAX, and loga_minlength is not
  1686. * NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
  1687. * o If the sum of loga_offset and loga_length exceeds
  1688. * NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
  1689. * the error NFS4ERR_INVAL MUST result.
  1690. */
  1691. nfserr = nfserr_inval;
  1692. if (lgp->lg_seg.length < lgp->lg_minlength ||
  1693. (lgp->lg_minlength != NFS4_MAX_UINT64 &&
  1694. lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
  1695. (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
  1696. lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
  1697. goto out;
  1698. if (lgp->lg_seg.length == 0)
  1699. goto out;
  1700. nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
  1701. true, lgp->lg_layout_type, &ls);
  1702. if (nfserr) {
  1703. trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
  1704. goto out;
  1705. }
  1706. nfserr = nfserr_recallconflict;
  1707. if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
  1708. goto out_put_stid;
  1709. nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
  1710. current_fh, lgp);
  1711. if (nfserr)
  1712. goto out_put_stid;
  1713. nfserr = nfsd4_insert_layout(lgp, ls);
  1714. out_put_stid:
  1715. mutex_unlock(&ls->ls_mutex);
  1716. nfs4_put_stid(&ls->ls_stid);
  1717. out:
  1718. return nfserr;
  1719. }
  1720. static void
  1721. nfsd4_layoutget_release(union nfsd4_op_u *u)
  1722. {
  1723. kfree(u->layoutget.lg_content);
  1724. }
  1725. static __be32
  1726. nfsd4_layoutcommit(struct svc_rqst *rqstp,
  1727. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1728. {
  1729. struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
  1730. const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
  1731. struct svc_fh *current_fh = &cstate->current_fh;
  1732. const struct nfsd4_layout_ops *ops;
  1733. loff_t new_size = lcp->lc_last_wr + 1;
  1734. struct inode *inode;
  1735. struct nfs4_layout_stateid *ls;
  1736. __be32 nfserr;
  1737. nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
  1738. if (nfserr)
  1739. goto out;
  1740. nfserr = nfserr_layoutunavailable;
  1741. ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
  1742. if (!ops)
  1743. goto out;
  1744. inode = d_inode(current_fh->fh_dentry);
  1745. nfserr = nfserr_inval;
  1746. if (new_size <= seg->offset) {
  1747. dprintk("pnfsd: last write before layout segment\n");
  1748. goto out;
  1749. }
  1750. if (new_size > seg->offset + seg->length) {
  1751. dprintk("pnfsd: last write beyond layout segment\n");
  1752. goto out;
  1753. }
  1754. if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
  1755. dprintk("pnfsd: layoutcommit beyond EOF\n");
  1756. goto out;
  1757. }
  1758. nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
  1759. false, lcp->lc_layout_type,
  1760. &ls);
  1761. if (nfserr) {
  1762. trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
  1763. /* fixup error code as per RFC5661 */
  1764. if (nfserr == nfserr_bad_stateid)
  1765. nfserr = nfserr_badlayout;
  1766. goto out;
  1767. }
  1768. /* LAYOUTCOMMIT does not require any serialization */
  1769. mutex_unlock(&ls->ls_mutex);
  1770. if (new_size > i_size_read(inode)) {
  1771. lcp->lc_size_chg = 1;
  1772. lcp->lc_newsize = new_size;
  1773. } else {
  1774. lcp->lc_size_chg = 0;
  1775. }
  1776. nfserr = ops->proc_layoutcommit(inode, lcp);
  1777. nfs4_put_stid(&ls->ls_stid);
  1778. out:
  1779. return nfserr;
  1780. }
  1781. static __be32
  1782. nfsd4_layoutreturn(struct svc_rqst *rqstp,
  1783. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  1784. {
  1785. struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
  1786. struct svc_fh *current_fh = &cstate->current_fh;
  1787. __be32 nfserr;
  1788. nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
  1789. if (nfserr)
  1790. goto out;
  1791. nfserr = nfserr_layoutunavailable;
  1792. if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
  1793. goto out;
  1794. switch (lrp->lr_seg.iomode) {
  1795. case IOMODE_READ:
  1796. case IOMODE_RW:
  1797. case IOMODE_ANY:
  1798. break;
  1799. default:
  1800. dprintk("%s: invalid iomode %d\n", __func__,
  1801. lrp->lr_seg.iomode);
  1802. nfserr = nfserr_inval;
  1803. goto out;
  1804. }
  1805. switch (lrp->lr_return_type) {
  1806. case RETURN_FILE:
  1807. nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
  1808. break;
  1809. case RETURN_FSID:
  1810. case RETURN_ALL:
  1811. nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
  1812. break;
  1813. default:
  1814. dprintk("%s: invalid return_type %d\n", __func__,
  1815. lrp->lr_return_type);
  1816. nfserr = nfserr_inval;
  1817. break;
  1818. }
  1819. out:
  1820. return nfserr;
  1821. }
  1822. #endif /* CONFIG_NFSD_PNFS */
  1823. static __be32
  1824. nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1825. union nfsd4_op_u *u)
  1826. {
  1827. struct nfsd4_getxattr *getxattr = &u->getxattr;
  1828. return nfsd_getxattr(rqstp, &cstate->current_fh,
  1829. getxattr->getxa_name, &getxattr->getxa_buf,
  1830. &getxattr->getxa_len);
  1831. }
  1832. static __be32
  1833. nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1834. union nfsd4_op_u *u)
  1835. {
  1836. struct nfsd4_setxattr *setxattr = &u->setxattr;
  1837. __be32 ret;
  1838. if (opens_in_grace(SVC_NET(rqstp)))
  1839. return nfserr_grace;
  1840. ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name,
  1841. setxattr->setxa_buf, setxattr->setxa_len,
  1842. setxattr->setxa_flags);
  1843. if (!ret)
  1844. set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh);
  1845. return ret;
  1846. }
  1847. static __be32
  1848. nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1849. union nfsd4_op_u *u)
  1850. {
  1851. /*
  1852. * Get the entire list, then copy out only the user attributes
  1853. * in the encode function.
  1854. */
  1855. return nfsd_listxattr(rqstp, &cstate->current_fh,
  1856. &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len);
  1857. }
  1858. static __be32
  1859. nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1860. union nfsd4_op_u *u)
  1861. {
  1862. struct nfsd4_removexattr *removexattr = &u->removexattr;
  1863. __be32 ret;
  1864. if (opens_in_grace(SVC_NET(rqstp)))
  1865. return nfserr_grace;
  1866. ret = nfsd_removexattr(rqstp, &cstate->current_fh,
  1867. removexattr->rmxa_name);
  1868. if (!ret)
  1869. set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh);
  1870. return ret;
  1871. }
  1872. /*
  1873. * NULL call.
  1874. */
  1875. static __be32
  1876. nfsd4_proc_null(struct svc_rqst *rqstp)
  1877. {
  1878. return rpc_success;
  1879. }
  1880. static inline void nfsd4_increment_op_stats(u32 opnum)
  1881. {
  1882. if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
  1883. nfsdstats.nfs4_opcount[opnum]++;
  1884. }
  1885. static const struct nfsd4_operation nfsd4_ops[];
  1886. static const char *nfsd4_op_name(unsigned opnum);
  1887. /*
  1888. * Enforce NFSv4.1 COMPOUND ordering rules:
  1889. *
  1890. * Also note, enforced elsewhere:
  1891. * - SEQUENCE other than as first op results in
  1892. * NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
  1893. * - BIND_CONN_TO_SESSION must be the only op in its compound.
  1894. * (Enforced in nfsd4_bind_conn_to_session().)
  1895. * - DESTROY_SESSION must be the final operation in a compound, if
  1896. * sessionid's in SEQUENCE and DESTROY_SESSION are the same.
  1897. * (Enforced in nfsd4_destroy_session().)
  1898. */
  1899. static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
  1900. {
  1901. struct nfsd4_op *first_op = &args->ops[0];
  1902. /* These ordering requirements don't apply to NFSv4.0: */
  1903. if (args->minorversion == 0)
  1904. return nfs_ok;
  1905. /* This is weird, but OK, not our problem: */
  1906. if (args->opcnt == 0)
  1907. return nfs_ok;
  1908. if (first_op->status == nfserr_op_illegal)
  1909. return nfs_ok;
  1910. if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
  1911. return nfserr_op_not_in_session;
  1912. if (first_op->opnum == OP_SEQUENCE)
  1913. return nfs_ok;
  1914. /*
  1915. * So first_op is something allowed outside a session, like
  1916. * EXCHANGE_ID; but then it has to be the only op in the
  1917. * compound:
  1918. */
  1919. if (args->opcnt != 1)
  1920. return nfserr_not_only_op;
  1921. return nfs_ok;
  1922. }
  1923. const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
  1924. {
  1925. return &nfsd4_ops[op->opnum];
  1926. }
  1927. bool nfsd4_cache_this_op(struct nfsd4_op *op)
  1928. {
  1929. if (op->opnum == OP_ILLEGAL)
  1930. return false;
  1931. return OPDESC(op)->op_flags & OP_CACHEME;
  1932. }
  1933. static bool need_wrongsec_check(struct svc_rqst *rqstp)
  1934. {
  1935. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1936. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  1937. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  1938. struct nfsd4_op *next = &argp->ops[resp->opcnt];
  1939. const struct nfsd4_operation *thisd = OPDESC(this);
  1940. const struct nfsd4_operation *nextd;
  1941. /*
  1942. * Most ops check wronsec on our own; only the putfh-like ops
  1943. * have special rules.
  1944. */
  1945. if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
  1946. return false;
  1947. /*
  1948. * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
  1949. * put-filehandle operation if we're not going to use the
  1950. * result:
  1951. */
  1952. if (argp->opcnt == resp->opcnt)
  1953. return false;
  1954. if (next->opnum == OP_ILLEGAL)
  1955. return false;
  1956. nextd = OPDESC(next);
  1957. /*
  1958. * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
  1959. * errors themselves as necessary; others should check for them
  1960. * now:
  1961. */
  1962. return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
  1963. }
  1964. static void svcxdr_init_encode(struct svc_rqst *rqstp,
  1965. struct nfsd4_compoundres *resp)
  1966. {
  1967. struct xdr_stream *xdr = &resp->xdr;
  1968. struct xdr_buf *buf = &rqstp->rq_res;
  1969. struct kvec *head = buf->head;
  1970. xdr->buf = buf;
  1971. xdr->iov = head;
  1972. xdr->p = head->iov_base + head->iov_len;
  1973. xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
  1974. /* Tail and page_len should be zero at this point: */
  1975. buf->len = buf->head[0].iov_len;
  1976. xdr->scratch.iov_len = 0;
  1977. xdr->page_ptr = buf->pages - 1;
  1978. buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
  1979. - rqstp->rq_auth_slack;
  1980. }
  1981. #ifdef CONFIG_NFSD_V4_2_INTER_SSC
  1982. static void
  1983. check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
  1984. {
  1985. struct nfsd4_op *op, *current_op = NULL, *saved_op = NULL;
  1986. struct nfsd4_copy *copy;
  1987. struct nfsd4_putfh *putfh;
  1988. int i;
  1989. /* traverse all operation and if it's a COPY compound, mark the
  1990. * source filehandle to skip verification
  1991. */
  1992. for (i = 0; i < args->opcnt; i++) {
  1993. op = &args->ops[i];
  1994. if (op->opnum == OP_PUTFH)
  1995. current_op = op;
  1996. else if (op->opnum == OP_SAVEFH)
  1997. saved_op = current_op;
  1998. else if (op->opnum == OP_RESTOREFH)
  1999. current_op = saved_op;
  2000. else if (op->opnum == OP_COPY) {
  2001. copy = (struct nfsd4_copy *)&op->u;
  2002. if (!saved_op) {
  2003. op->status = nfserr_nofilehandle;
  2004. return;
  2005. }
  2006. putfh = (struct nfsd4_putfh *)&saved_op->u;
  2007. if (!copy->cp_intra)
  2008. putfh->no_verify = true;
  2009. }
  2010. }
  2011. }
  2012. #else
  2013. static void
  2014. check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
  2015. {
  2016. }
  2017. #endif
  2018. /*
  2019. * COMPOUND call.
  2020. */
  2021. static __be32
  2022. nfsd4_proc_compound(struct svc_rqst *rqstp)
  2023. {
  2024. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  2025. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2026. struct nfsd4_op *op;
  2027. struct nfsd4_compound_state *cstate = &resp->cstate;
  2028. struct svc_fh *current_fh = &cstate->current_fh;
  2029. struct svc_fh *save_fh = &cstate->save_fh;
  2030. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2031. __be32 status;
  2032. svcxdr_init_encode(rqstp, resp);
  2033. resp->tagp = resp->xdr.p;
  2034. /* reserve space for: taglen, tag, and opcnt */
  2035. xdr_reserve_space(&resp->xdr, 8 + args->taglen);
  2036. resp->taglen = args->taglen;
  2037. resp->tag = args->tag;
  2038. resp->rqstp = rqstp;
  2039. cstate->minorversion = args->minorversion;
  2040. fh_init(current_fh, NFS4_FHSIZE);
  2041. fh_init(save_fh, NFS4_FHSIZE);
  2042. /*
  2043. * Don't use the deferral mechanism for NFSv4; compounds make it
  2044. * too hard to avoid non-idempotency problems.
  2045. */
  2046. clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  2047. /*
  2048. * According to RFC3010, this takes precedence over all other errors.
  2049. */
  2050. status = nfserr_minor_vers_mismatch;
  2051. if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
  2052. goto out;
  2053. status = nfserr_resource;
  2054. if (args->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
  2055. goto out;
  2056. status = nfs41_check_op_ordering(args);
  2057. if (status) {
  2058. op = &args->ops[0];
  2059. op->status = status;
  2060. resp->opcnt = 1;
  2061. goto encode_op;
  2062. }
  2063. check_if_stalefh_allowed(args);
  2064. rqstp->rq_lease_breaker = (void **)&cstate->clp;
  2065. trace_nfsd_compound(rqstp, args->opcnt);
  2066. while (!status && resp->opcnt < args->opcnt) {
  2067. op = &args->ops[resp->opcnt++];
  2068. /*
  2069. * The XDR decode routines may have pre-set op->status;
  2070. * for example, if there is a miscellaneous XDR error
  2071. * it will be set to nfserr_bad_xdr.
  2072. */
  2073. if (op->status) {
  2074. if (op->opnum == OP_OPEN)
  2075. op->status = nfsd4_open_omfg(rqstp, cstate, op);
  2076. goto encode_op;
  2077. }
  2078. if (!current_fh->fh_dentry &&
  2079. !HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) {
  2080. if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
  2081. op->status = nfserr_nofilehandle;
  2082. goto encode_op;
  2083. }
  2084. } else if (current_fh->fh_export &&
  2085. current_fh->fh_export->ex_fslocs.migrated &&
  2086. !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
  2087. op->status = nfserr_moved;
  2088. goto encode_op;
  2089. }
  2090. fh_clear_wcc(current_fh);
  2091. /* If op is non-idempotent */
  2092. if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
  2093. /*
  2094. * Don't execute this op if we couldn't encode a
  2095. * succesful reply:
  2096. */
  2097. u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
  2098. /*
  2099. * Plus if there's another operation, make sure
  2100. * we'll have space to at least encode an error:
  2101. */
  2102. if (resp->opcnt < args->opcnt)
  2103. plen += COMPOUND_ERR_SLACK_SPACE;
  2104. op->status = nfsd4_check_resp_size(resp, plen);
  2105. }
  2106. if (op->status)
  2107. goto encode_op;
  2108. if (op->opdesc->op_get_currentstateid)
  2109. op->opdesc->op_get_currentstateid(cstate, &op->u);
  2110. op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
  2111. /* Only from SEQUENCE */
  2112. if (cstate->status == nfserr_replay_cache) {
  2113. dprintk("%s NFS4.1 replay from cache\n", __func__);
  2114. status = op->status;
  2115. goto out;
  2116. }
  2117. if (!op->status) {
  2118. if (op->opdesc->op_set_currentstateid)
  2119. op->opdesc->op_set_currentstateid(cstate, &op->u);
  2120. if (op->opdesc->op_flags & OP_CLEAR_STATEID)
  2121. clear_current_stateid(cstate);
  2122. if (current_fh->fh_export &&
  2123. need_wrongsec_check(rqstp))
  2124. op->status = check_nfsd_access(current_fh->fh_export, rqstp);
  2125. }
  2126. encode_op:
  2127. if (op->status == nfserr_replay_me) {
  2128. op->replay = &cstate->replay_owner->so_replay;
  2129. nfsd4_encode_replay(&resp->xdr, op);
  2130. status = op->status = op->replay->rp_status;
  2131. } else {
  2132. nfsd4_encode_operation(resp, op);
  2133. status = op->status;
  2134. }
  2135. trace_nfsd_compound_status(args->opcnt, resp->opcnt, status,
  2136. nfsd4_op_name(op->opnum));
  2137. nfsd4_cstate_clear_replay(cstate);
  2138. nfsd4_increment_op_stats(op->opnum);
  2139. }
  2140. fh_put(current_fh);
  2141. fh_put(save_fh);
  2142. BUG_ON(cstate->replay_owner);
  2143. out:
  2144. cstate->status = status;
  2145. /* Reset deferral mechanism for RPC deferrals */
  2146. set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
  2147. return rpc_success;
  2148. }
  2149. #define op_encode_hdr_size (2)
  2150. #define op_encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
  2151. #define op_encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
  2152. #define op_encode_change_info_maxsz (5)
  2153. #define nfs4_fattr_bitmap_maxsz (4)
  2154. /* We'll fall back on returning no lockowner if run out of space: */
  2155. #define op_encode_lockowner_maxsz (0)
  2156. #define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
  2157. #define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
  2158. #define op_encode_ace_maxsz (3 + nfs4_owner_maxsz)
  2159. #define op_encode_delegation_maxsz (1 + op_encode_stateid_maxsz + 1 + \
  2160. op_encode_ace_maxsz)
  2161. #define op_encode_channel_attrs_maxsz (6 + 1 + 1)
  2162. static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2163. {
  2164. return (op_encode_hdr_size) * sizeof(__be32);
  2165. }
  2166. static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2167. {
  2168. return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
  2169. }
  2170. static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2171. {
  2172. /* ac_supported, ac_resp_access */
  2173. return (op_encode_hdr_size + 2)* sizeof(__be32);
  2174. }
  2175. static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2176. {
  2177. return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
  2178. }
  2179. static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2180. {
  2181. return (op_encode_hdr_size + op_encode_change_info_maxsz
  2182. + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  2183. }
  2184. /*
  2185. * Note since this is an idempotent operation we won't insist on failing
  2186. * the op prematurely if the estimate is too large. We may turn off splice
  2187. * reads unnecessarily.
  2188. */
  2189. static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
  2190. struct nfsd4_op *op)
  2191. {
  2192. u32 *bmap = op->u.getattr.ga_bmval;
  2193. u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
  2194. u32 ret = 0;
  2195. if (bmap0 & FATTR4_WORD0_ACL)
  2196. return svc_max_payload(rqstp);
  2197. if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
  2198. return svc_max_payload(rqstp);
  2199. if (bmap1 & FATTR4_WORD1_OWNER) {
  2200. ret += IDMAP_NAMESZ + 4;
  2201. bmap1 &= ~FATTR4_WORD1_OWNER;
  2202. }
  2203. if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
  2204. ret += IDMAP_NAMESZ + 4;
  2205. bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
  2206. }
  2207. if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
  2208. ret += NFS4_FHSIZE + 4;
  2209. bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
  2210. }
  2211. if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
  2212. ret += NFS4_MAXLABELLEN + 12;
  2213. bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  2214. }
  2215. /*
  2216. * Largest of remaining attributes are 16 bytes (e.g.,
  2217. * supported_attributes)
  2218. */
  2219. ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
  2220. /* bitmask, length */
  2221. ret += 20;
  2222. return ret;
  2223. }
  2224. static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2225. {
  2226. return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
  2227. }
  2228. static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2229. {
  2230. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  2231. * sizeof(__be32);
  2232. }
  2233. static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2234. {
  2235. return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
  2236. * sizeof(__be32);
  2237. }
  2238. static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2239. {
  2240. return (op_encode_hdr_size + op_encode_stateid_maxsz
  2241. + op_encode_change_info_maxsz + 1
  2242. + nfs4_fattr_bitmap_maxsz
  2243. + op_encode_delegation_maxsz) * sizeof(__be32);
  2244. }
  2245. static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2246. {
  2247. u32 maxcount = 0, rlen = 0;
  2248. maxcount = svc_max_payload(rqstp);
  2249. rlen = min(op->u.read.rd_length, maxcount);
  2250. return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
  2251. }
  2252. static inline u32 nfsd4_read_plus_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2253. {
  2254. u32 maxcount = svc_max_payload(rqstp);
  2255. u32 rlen = min(op->u.read.rd_length, maxcount);
  2256. /*
  2257. * If we detect that the file changed during hole encoding, then we
  2258. * recover by encoding the remaining reply as data. This means we need
  2259. * to set aside enough room to encode two data segments.
  2260. */
  2261. u32 seg_len = 2 * (1 + 2 + 1);
  2262. return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32);
  2263. }
  2264. static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2265. {
  2266. u32 maxcount = 0, rlen = 0;
  2267. maxcount = svc_max_payload(rqstp);
  2268. rlen = min(op->u.readdir.rd_maxcount, maxcount);
  2269. return (op_encode_hdr_size + op_encode_verifier_maxsz +
  2270. XDR_QUADLEN(rlen)) * sizeof(__be32);
  2271. }
  2272. static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2273. {
  2274. return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
  2275. }
  2276. static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2277. {
  2278. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  2279. * sizeof(__be32);
  2280. }
  2281. static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2282. {
  2283. return (op_encode_hdr_size + op_encode_change_info_maxsz
  2284. + op_encode_change_info_maxsz) * sizeof(__be32);
  2285. }
  2286. static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
  2287. struct nfsd4_op *op)
  2288. {
  2289. return (op_encode_hdr_size
  2290. + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
  2291. }
  2292. static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2293. {
  2294. return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
  2295. * sizeof(__be32);
  2296. }
  2297. static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2298. {
  2299. return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
  2300. }
  2301. static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2302. {
  2303. return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
  2304. (4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
  2305. }
  2306. static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2307. {
  2308. return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
  2309. sizeof(__be32);
  2310. }
  2311. static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2312. {
  2313. return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
  2314. }
  2315. static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2316. {
  2317. return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
  2318. 1 + 1 + /* eir_flags, spr_how */\
  2319. 4 + /* spo_must_enforce & _allow with bitmap */\
  2320. 2 + /*eir_server_owner.so_minor_id */\
  2321. /* eir_server_owner.so_major_id<> */\
  2322. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  2323. /* eir_server_scope<> */\
  2324. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
  2325. 1 + /* eir_server_impl_id array length */\
  2326. 0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
  2327. }
  2328. static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2329. {
  2330. return (op_encode_hdr_size + \
  2331. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
  2332. 2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
  2333. }
  2334. static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2335. {
  2336. return (op_encode_hdr_size + \
  2337. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
  2338. 2 + /* csr_sequence, csr_flags */\
  2339. op_encode_channel_attrs_maxsz + \
  2340. op_encode_channel_attrs_maxsz) * sizeof(__be32);
  2341. }
  2342. static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2343. {
  2344. return (op_encode_hdr_size +
  2345. 1 /* wr_callback */ +
  2346. op_encode_stateid_maxsz /* wr_callback */ +
  2347. 2 /* wr_count */ +
  2348. 1 /* wr_committed */ +
  2349. op_encode_verifier_maxsz +
  2350. 1 /* cr_consecutive */ +
  2351. 1 /* cr_synchronous */) * sizeof(__be32);
  2352. }
  2353. static inline u32 nfsd4_offload_status_rsize(struct svc_rqst *rqstp,
  2354. struct nfsd4_op *op)
  2355. {
  2356. return (op_encode_hdr_size +
  2357. 2 /* osr_count */ +
  2358. 1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
  2359. }
  2360. static inline u32 nfsd4_copy_notify_rsize(struct svc_rqst *rqstp,
  2361. struct nfsd4_op *op)
  2362. {
  2363. return (op_encode_hdr_size +
  2364. 3 /* cnr_lease_time */ +
  2365. 1 /* We support one cnr_source_server */ +
  2366. 1 /* cnr_stateid seq */ +
  2367. op_encode_stateid_maxsz /* cnr_stateid */ +
  2368. 1 /* num cnr_source_server*/ +
  2369. 1 /* nl4_type */ +
  2370. 1 /* nl4 size */ +
  2371. XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
  2372. * sizeof(__be32);
  2373. }
  2374. #ifdef CONFIG_NFSD_PNFS
  2375. static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2376. {
  2377. u32 maxcount = 0, rlen = 0;
  2378. maxcount = svc_max_payload(rqstp);
  2379. rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
  2380. return (op_encode_hdr_size +
  2381. 1 /* gd_layout_type*/ +
  2382. XDR_QUADLEN(rlen) +
  2383. 2 /* gd_notify_types */) * sizeof(__be32);
  2384. }
  2385. /*
  2386. * At this stage we don't really know what layout driver will handle the request,
  2387. * so we need to define an arbitrary upper bound here.
  2388. */
  2389. #define MAX_LAYOUT_SIZE 128
  2390. static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2391. {
  2392. return (op_encode_hdr_size +
  2393. 1 /* logr_return_on_close */ +
  2394. op_encode_stateid_maxsz +
  2395. 1 /* nr of layouts */ +
  2396. MAX_LAYOUT_SIZE) * sizeof(__be32);
  2397. }
  2398. static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2399. {
  2400. return (op_encode_hdr_size +
  2401. 1 /* locr_newsize */ +
  2402. 2 /* ns_size */) * sizeof(__be32);
  2403. }
  2404. static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2405. {
  2406. return (op_encode_hdr_size +
  2407. 1 /* lrs_stateid */ +
  2408. op_encode_stateid_maxsz) * sizeof(__be32);
  2409. }
  2410. #endif /* CONFIG_NFSD_PNFS */
  2411. static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2412. {
  2413. return (op_encode_hdr_size + 3) * sizeof(__be32);
  2414. }
  2415. static inline u32 nfsd4_getxattr_rsize(struct svc_rqst *rqstp,
  2416. struct nfsd4_op *op)
  2417. {
  2418. u32 maxcount, rlen;
  2419. maxcount = svc_max_payload(rqstp);
  2420. rlen = min_t(u32, XATTR_SIZE_MAX, maxcount);
  2421. return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32);
  2422. }
  2423. static inline u32 nfsd4_setxattr_rsize(struct svc_rqst *rqstp,
  2424. struct nfsd4_op *op)
  2425. {
  2426. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  2427. * sizeof(__be32);
  2428. }
  2429. static inline u32 nfsd4_listxattrs_rsize(struct svc_rqst *rqstp,
  2430. struct nfsd4_op *op)
  2431. {
  2432. u32 maxcount, rlen;
  2433. maxcount = svc_max_payload(rqstp);
  2434. rlen = min(op->u.listxattrs.lsxa_maxcount, maxcount);
  2435. return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32);
  2436. }
  2437. static inline u32 nfsd4_removexattr_rsize(struct svc_rqst *rqstp,
  2438. struct nfsd4_op *op)
  2439. {
  2440. return (op_encode_hdr_size + op_encode_change_info_maxsz)
  2441. * sizeof(__be32);
  2442. }
  2443. static const struct nfsd4_operation nfsd4_ops[] = {
  2444. [OP_ACCESS] = {
  2445. .op_func = nfsd4_access,
  2446. .op_name = "OP_ACCESS",
  2447. .op_rsize_bop = nfsd4_access_rsize,
  2448. },
  2449. [OP_CLOSE] = {
  2450. .op_func = nfsd4_close,
  2451. .op_flags = OP_MODIFIES_SOMETHING,
  2452. .op_name = "OP_CLOSE",
  2453. .op_rsize_bop = nfsd4_status_stateid_rsize,
  2454. .op_get_currentstateid = nfsd4_get_closestateid,
  2455. .op_set_currentstateid = nfsd4_set_closestateid,
  2456. },
  2457. [OP_COMMIT] = {
  2458. .op_func = nfsd4_commit,
  2459. .op_flags = OP_MODIFIES_SOMETHING,
  2460. .op_name = "OP_COMMIT",
  2461. .op_rsize_bop = nfsd4_commit_rsize,
  2462. },
  2463. [OP_CREATE] = {
  2464. .op_func = nfsd4_create,
  2465. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
  2466. .op_name = "OP_CREATE",
  2467. .op_rsize_bop = nfsd4_create_rsize,
  2468. },
  2469. [OP_DELEGRETURN] = {
  2470. .op_func = nfsd4_delegreturn,
  2471. .op_flags = OP_MODIFIES_SOMETHING,
  2472. .op_name = "OP_DELEGRETURN",
  2473. .op_rsize_bop = nfsd4_only_status_rsize,
  2474. .op_get_currentstateid = nfsd4_get_delegreturnstateid,
  2475. },
  2476. [OP_GETATTR] = {
  2477. .op_func = nfsd4_getattr,
  2478. .op_flags = ALLOWED_ON_ABSENT_FS,
  2479. .op_rsize_bop = nfsd4_getattr_rsize,
  2480. .op_name = "OP_GETATTR",
  2481. },
  2482. [OP_GETFH] = {
  2483. .op_func = nfsd4_getfh,
  2484. .op_name = "OP_GETFH",
  2485. .op_rsize_bop = nfsd4_getfh_rsize,
  2486. },
  2487. [OP_LINK] = {
  2488. .op_func = nfsd4_link,
  2489. .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
  2490. | OP_CACHEME,
  2491. .op_name = "OP_LINK",
  2492. .op_rsize_bop = nfsd4_link_rsize,
  2493. },
  2494. [OP_LOCK] = {
  2495. .op_func = nfsd4_lock,
  2496. .op_flags = OP_MODIFIES_SOMETHING |
  2497. OP_NONTRIVIAL_ERROR_ENCODE,
  2498. .op_name = "OP_LOCK",
  2499. .op_rsize_bop = nfsd4_lock_rsize,
  2500. .op_set_currentstateid = nfsd4_set_lockstateid,
  2501. },
  2502. [OP_LOCKT] = {
  2503. .op_func = nfsd4_lockt,
  2504. .op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
  2505. .op_name = "OP_LOCKT",
  2506. .op_rsize_bop = nfsd4_lock_rsize,
  2507. },
  2508. [OP_LOCKU] = {
  2509. .op_func = nfsd4_locku,
  2510. .op_flags = OP_MODIFIES_SOMETHING,
  2511. .op_name = "OP_LOCKU",
  2512. .op_rsize_bop = nfsd4_status_stateid_rsize,
  2513. .op_get_currentstateid = nfsd4_get_lockustateid,
  2514. },
  2515. [OP_LOOKUP] = {
  2516. .op_func = nfsd4_lookup,
  2517. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  2518. .op_name = "OP_LOOKUP",
  2519. .op_rsize_bop = nfsd4_only_status_rsize,
  2520. },
  2521. [OP_LOOKUPP] = {
  2522. .op_func = nfsd4_lookupp,
  2523. .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
  2524. .op_name = "OP_LOOKUPP",
  2525. .op_rsize_bop = nfsd4_only_status_rsize,
  2526. },
  2527. [OP_NVERIFY] = {
  2528. .op_func = nfsd4_nverify,
  2529. .op_name = "OP_NVERIFY",
  2530. .op_rsize_bop = nfsd4_only_status_rsize,
  2531. },
  2532. [OP_OPEN] = {
  2533. .op_func = nfsd4_open,
  2534. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  2535. .op_name = "OP_OPEN",
  2536. .op_rsize_bop = nfsd4_open_rsize,
  2537. .op_set_currentstateid = nfsd4_set_openstateid,
  2538. },
  2539. [OP_OPEN_CONFIRM] = {
  2540. .op_func = nfsd4_open_confirm,
  2541. .op_flags = OP_MODIFIES_SOMETHING,
  2542. .op_name = "OP_OPEN_CONFIRM",
  2543. .op_rsize_bop = nfsd4_status_stateid_rsize,
  2544. },
  2545. [OP_OPEN_DOWNGRADE] = {
  2546. .op_func = nfsd4_open_downgrade,
  2547. .op_flags = OP_MODIFIES_SOMETHING,
  2548. .op_name = "OP_OPEN_DOWNGRADE",
  2549. .op_rsize_bop = nfsd4_status_stateid_rsize,
  2550. .op_get_currentstateid = nfsd4_get_opendowngradestateid,
  2551. .op_set_currentstateid = nfsd4_set_opendowngradestateid,
  2552. },
  2553. [OP_PUTFH] = {
  2554. .op_func = nfsd4_putfh,
  2555. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2556. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  2557. .op_name = "OP_PUTFH",
  2558. .op_rsize_bop = nfsd4_only_status_rsize,
  2559. },
  2560. [OP_PUTPUBFH] = {
  2561. .op_func = nfsd4_putrootfh,
  2562. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2563. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  2564. .op_name = "OP_PUTPUBFH",
  2565. .op_rsize_bop = nfsd4_only_status_rsize,
  2566. },
  2567. [OP_PUTROOTFH] = {
  2568. .op_func = nfsd4_putrootfh,
  2569. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2570. | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
  2571. .op_name = "OP_PUTROOTFH",
  2572. .op_rsize_bop = nfsd4_only_status_rsize,
  2573. },
  2574. [OP_READ] = {
  2575. .op_func = nfsd4_read,
  2576. .op_release = nfsd4_read_release,
  2577. .op_name = "OP_READ",
  2578. .op_rsize_bop = nfsd4_read_rsize,
  2579. .op_get_currentstateid = nfsd4_get_readstateid,
  2580. },
  2581. [OP_READDIR] = {
  2582. .op_func = nfsd4_readdir,
  2583. .op_name = "OP_READDIR",
  2584. .op_rsize_bop = nfsd4_readdir_rsize,
  2585. },
  2586. [OP_READLINK] = {
  2587. .op_func = nfsd4_readlink,
  2588. .op_name = "OP_READLINK",
  2589. .op_rsize_bop = nfsd4_readlink_rsize,
  2590. },
  2591. [OP_REMOVE] = {
  2592. .op_func = nfsd4_remove,
  2593. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2594. .op_name = "OP_REMOVE",
  2595. .op_rsize_bop = nfsd4_remove_rsize,
  2596. },
  2597. [OP_RENAME] = {
  2598. .op_func = nfsd4_rename,
  2599. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2600. .op_name = "OP_RENAME",
  2601. .op_rsize_bop = nfsd4_rename_rsize,
  2602. },
  2603. [OP_RENEW] = {
  2604. .op_func = nfsd4_renew,
  2605. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2606. | OP_MODIFIES_SOMETHING,
  2607. .op_name = "OP_RENEW",
  2608. .op_rsize_bop = nfsd4_only_status_rsize,
  2609. },
  2610. [OP_RESTOREFH] = {
  2611. .op_func = nfsd4_restorefh,
  2612. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2613. | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
  2614. .op_name = "OP_RESTOREFH",
  2615. .op_rsize_bop = nfsd4_only_status_rsize,
  2616. },
  2617. [OP_SAVEFH] = {
  2618. .op_func = nfsd4_savefh,
  2619. .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
  2620. .op_name = "OP_SAVEFH",
  2621. .op_rsize_bop = nfsd4_only_status_rsize,
  2622. },
  2623. [OP_SECINFO] = {
  2624. .op_func = nfsd4_secinfo,
  2625. .op_release = nfsd4_secinfo_release,
  2626. .op_flags = OP_HANDLES_WRONGSEC,
  2627. .op_name = "OP_SECINFO",
  2628. .op_rsize_bop = nfsd4_secinfo_rsize,
  2629. },
  2630. [OP_SETATTR] = {
  2631. .op_func = nfsd4_setattr,
  2632. .op_name = "OP_SETATTR",
  2633. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
  2634. | OP_NONTRIVIAL_ERROR_ENCODE,
  2635. .op_rsize_bop = nfsd4_setattr_rsize,
  2636. .op_get_currentstateid = nfsd4_get_setattrstateid,
  2637. },
  2638. [OP_SETCLIENTID] = {
  2639. .op_func = nfsd4_setclientid,
  2640. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2641. | OP_MODIFIES_SOMETHING | OP_CACHEME
  2642. | OP_NONTRIVIAL_ERROR_ENCODE,
  2643. .op_name = "OP_SETCLIENTID",
  2644. .op_rsize_bop = nfsd4_setclientid_rsize,
  2645. },
  2646. [OP_SETCLIENTID_CONFIRM] = {
  2647. .op_func = nfsd4_setclientid_confirm,
  2648. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2649. | OP_MODIFIES_SOMETHING | OP_CACHEME,
  2650. .op_name = "OP_SETCLIENTID_CONFIRM",
  2651. .op_rsize_bop = nfsd4_only_status_rsize,
  2652. },
  2653. [OP_VERIFY] = {
  2654. .op_func = nfsd4_verify,
  2655. .op_name = "OP_VERIFY",
  2656. .op_rsize_bop = nfsd4_only_status_rsize,
  2657. },
  2658. [OP_WRITE] = {
  2659. .op_func = nfsd4_write,
  2660. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2661. .op_name = "OP_WRITE",
  2662. .op_rsize_bop = nfsd4_write_rsize,
  2663. .op_get_currentstateid = nfsd4_get_writestateid,
  2664. },
  2665. [OP_RELEASE_LOCKOWNER] = {
  2666. .op_func = nfsd4_release_lockowner,
  2667. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
  2668. | OP_MODIFIES_SOMETHING,
  2669. .op_name = "OP_RELEASE_LOCKOWNER",
  2670. .op_rsize_bop = nfsd4_only_status_rsize,
  2671. },
  2672. /* NFSv4.1 operations */
  2673. [OP_EXCHANGE_ID] = {
  2674. .op_func = nfsd4_exchange_id,
  2675. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2676. | OP_MODIFIES_SOMETHING,
  2677. .op_name = "OP_EXCHANGE_ID",
  2678. .op_rsize_bop = nfsd4_exchange_id_rsize,
  2679. },
  2680. [OP_BACKCHANNEL_CTL] = {
  2681. .op_func = nfsd4_backchannel_ctl,
  2682. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2683. .op_name = "OP_BACKCHANNEL_CTL",
  2684. .op_rsize_bop = nfsd4_only_status_rsize,
  2685. },
  2686. [OP_BIND_CONN_TO_SESSION] = {
  2687. .op_func = nfsd4_bind_conn_to_session,
  2688. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2689. | OP_MODIFIES_SOMETHING,
  2690. .op_name = "OP_BIND_CONN_TO_SESSION",
  2691. .op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
  2692. },
  2693. [OP_CREATE_SESSION] = {
  2694. .op_func = nfsd4_create_session,
  2695. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2696. | OP_MODIFIES_SOMETHING,
  2697. .op_name = "OP_CREATE_SESSION",
  2698. .op_rsize_bop = nfsd4_create_session_rsize,
  2699. },
  2700. [OP_DESTROY_SESSION] = {
  2701. .op_func = nfsd4_destroy_session,
  2702. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2703. | OP_MODIFIES_SOMETHING,
  2704. .op_name = "OP_DESTROY_SESSION",
  2705. .op_rsize_bop = nfsd4_only_status_rsize,
  2706. },
  2707. [OP_SEQUENCE] = {
  2708. .op_func = nfsd4_sequence,
  2709. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
  2710. .op_name = "OP_SEQUENCE",
  2711. .op_rsize_bop = nfsd4_sequence_rsize,
  2712. },
  2713. [OP_DESTROY_CLIENTID] = {
  2714. .op_func = nfsd4_destroy_clientid,
  2715. .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
  2716. | OP_MODIFIES_SOMETHING,
  2717. .op_name = "OP_DESTROY_CLIENTID",
  2718. .op_rsize_bop = nfsd4_only_status_rsize,
  2719. },
  2720. [OP_RECLAIM_COMPLETE] = {
  2721. .op_func = nfsd4_reclaim_complete,
  2722. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2723. .op_name = "OP_RECLAIM_COMPLETE",
  2724. .op_rsize_bop = nfsd4_only_status_rsize,
  2725. },
  2726. [OP_SECINFO_NO_NAME] = {
  2727. .op_func = nfsd4_secinfo_no_name,
  2728. .op_release = nfsd4_secinfo_no_name_release,
  2729. .op_flags = OP_HANDLES_WRONGSEC,
  2730. .op_name = "OP_SECINFO_NO_NAME",
  2731. .op_rsize_bop = nfsd4_secinfo_rsize,
  2732. },
  2733. [OP_TEST_STATEID] = {
  2734. .op_func = nfsd4_test_stateid,
  2735. .op_flags = ALLOWED_WITHOUT_FH,
  2736. .op_name = "OP_TEST_STATEID",
  2737. .op_rsize_bop = nfsd4_test_stateid_rsize,
  2738. },
  2739. [OP_FREE_STATEID] = {
  2740. .op_func = nfsd4_free_stateid,
  2741. .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
  2742. .op_name = "OP_FREE_STATEID",
  2743. .op_get_currentstateid = nfsd4_get_freestateid,
  2744. .op_rsize_bop = nfsd4_only_status_rsize,
  2745. },
  2746. #ifdef CONFIG_NFSD_PNFS
  2747. [OP_GETDEVICEINFO] = {
  2748. .op_func = nfsd4_getdeviceinfo,
  2749. .op_release = nfsd4_getdeviceinfo_release,
  2750. .op_flags = ALLOWED_WITHOUT_FH,
  2751. .op_name = "OP_GETDEVICEINFO",
  2752. .op_rsize_bop = nfsd4_getdeviceinfo_rsize,
  2753. },
  2754. [OP_LAYOUTGET] = {
  2755. .op_func = nfsd4_layoutget,
  2756. .op_release = nfsd4_layoutget_release,
  2757. .op_flags = OP_MODIFIES_SOMETHING,
  2758. .op_name = "OP_LAYOUTGET",
  2759. .op_rsize_bop = nfsd4_layoutget_rsize,
  2760. },
  2761. [OP_LAYOUTCOMMIT] = {
  2762. .op_func = nfsd4_layoutcommit,
  2763. .op_flags = OP_MODIFIES_SOMETHING,
  2764. .op_name = "OP_LAYOUTCOMMIT",
  2765. .op_rsize_bop = nfsd4_layoutcommit_rsize,
  2766. },
  2767. [OP_LAYOUTRETURN] = {
  2768. .op_func = nfsd4_layoutreturn,
  2769. .op_flags = OP_MODIFIES_SOMETHING,
  2770. .op_name = "OP_LAYOUTRETURN",
  2771. .op_rsize_bop = nfsd4_layoutreturn_rsize,
  2772. },
  2773. #endif /* CONFIG_NFSD_PNFS */
  2774. /* NFSv4.2 operations */
  2775. [OP_ALLOCATE] = {
  2776. .op_func = nfsd4_allocate,
  2777. .op_flags = OP_MODIFIES_SOMETHING,
  2778. .op_name = "OP_ALLOCATE",
  2779. .op_rsize_bop = nfsd4_only_status_rsize,
  2780. },
  2781. [OP_DEALLOCATE] = {
  2782. .op_func = nfsd4_deallocate,
  2783. .op_flags = OP_MODIFIES_SOMETHING,
  2784. .op_name = "OP_DEALLOCATE",
  2785. .op_rsize_bop = nfsd4_only_status_rsize,
  2786. },
  2787. [OP_CLONE] = {
  2788. .op_func = nfsd4_clone,
  2789. .op_flags = OP_MODIFIES_SOMETHING,
  2790. .op_name = "OP_CLONE",
  2791. .op_rsize_bop = nfsd4_only_status_rsize,
  2792. },
  2793. [OP_COPY] = {
  2794. .op_func = nfsd4_copy,
  2795. .op_flags = OP_MODIFIES_SOMETHING,
  2796. .op_name = "OP_COPY",
  2797. .op_rsize_bop = nfsd4_copy_rsize,
  2798. },
  2799. [OP_READ_PLUS] = {
  2800. .op_func = nfsd4_read,
  2801. .op_release = nfsd4_read_release,
  2802. .op_name = "OP_READ_PLUS",
  2803. .op_rsize_bop = nfsd4_read_plus_rsize,
  2804. .op_get_currentstateid = nfsd4_get_readstateid,
  2805. },
  2806. [OP_SEEK] = {
  2807. .op_func = nfsd4_seek,
  2808. .op_name = "OP_SEEK",
  2809. .op_rsize_bop = nfsd4_seek_rsize,
  2810. },
  2811. [OP_OFFLOAD_STATUS] = {
  2812. .op_func = nfsd4_offload_status,
  2813. .op_name = "OP_OFFLOAD_STATUS",
  2814. .op_rsize_bop = nfsd4_offload_status_rsize,
  2815. },
  2816. [OP_OFFLOAD_CANCEL] = {
  2817. .op_func = nfsd4_offload_cancel,
  2818. .op_flags = OP_MODIFIES_SOMETHING,
  2819. .op_name = "OP_OFFLOAD_CANCEL",
  2820. .op_rsize_bop = nfsd4_only_status_rsize,
  2821. },
  2822. [OP_COPY_NOTIFY] = {
  2823. .op_func = nfsd4_copy_notify,
  2824. .op_flags = OP_MODIFIES_SOMETHING,
  2825. .op_name = "OP_COPY_NOTIFY",
  2826. .op_rsize_bop = nfsd4_copy_notify_rsize,
  2827. },
  2828. [OP_GETXATTR] = {
  2829. .op_func = nfsd4_getxattr,
  2830. .op_name = "OP_GETXATTR",
  2831. .op_rsize_bop = nfsd4_getxattr_rsize,
  2832. },
  2833. [OP_SETXATTR] = {
  2834. .op_func = nfsd4_setxattr,
  2835. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2836. .op_name = "OP_SETXATTR",
  2837. .op_rsize_bop = nfsd4_setxattr_rsize,
  2838. },
  2839. [OP_LISTXATTRS] = {
  2840. .op_func = nfsd4_listxattrs,
  2841. .op_name = "OP_LISTXATTRS",
  2842. .op_rsize_bop = nfsd4_listxattrs_rsize,
  2843. },
  2844. [OP_REMOVEXATTR] = {
  2845. .op_func = nfsd4_removexattr,
  2846. .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
  2847. .op_name = "OP_REMOVEXATTR",
  2848. .op_rsize_bop = nfsd4_removexattr_rsize,
  2849. },
  2850. };
  2851. /**
  2852. * nfsd4_spo_must_allow - Determine if the compound op contains an
  2853. * operation that is allowed to be sent with machine credentials
  2854. *
  2855. * @rqstp: a pointer to the struct svc_rqst
  2856. *
  2857. * Checks to see if the compound contains a spo_must_allow op
  2858. * and confirms that it was sent with the proper machine creds.
  2859. */
  2860. bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
  2861. {
  2862. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2863. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  2864. struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
  2865. struct nfsd4_compound_state *cstate = &resp->cstate;
  2866. struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
  2867. u32 opiter;
  2868. if (!cstate->minorversion)
  2869. return false;
  2870. if (cstate->spo_must_allowed)
  2871. return true;
  2872. opiter = resp->opcnt;
  2873. while (opiter < argp->opcnt) {
  2874. this = &argp->ops[opiter++];
  2875. if (test_bit(this->opnum, allow->u.longs) &&
  2876. cstate->clp->cl_mach_cred &&
  2877. nfsd4_mach_creds_match(cstate->clp, rqstp)) {
  2878. cstate->spo_must_allowed = true;
  2879. return true;
  2880. }
  2881. }
  2882. cstate->spo_must_allowed = false;
  2883. return false;
  2884. }
  2885. int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
  2886. {
  2887. if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
  2888. return op_encode_hdr_size * sizeof(__be32);
  2889. BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
  2890. return OPDESC(op)->op_rsize_bop(rqstp, op);
  2891. }
  2892. void warn_on_nonidempotent_op(struct nfsd4_op *op)
  2893. {
  2894. if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
  2895. pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
  2896. op->opnum, nfsd4_op_name(op->opnum));
  2897. WARN_ON_ONCE(1);
  2898. }
  2899. }
  2900. static const char *nfsd4_op_name(unsigned opnum)
  2901. {
  2902. if (opnum < ARRAY_SIZE(nfsd4_ops))
  2903. return nfsd4_ops[opnum].op_name;
  2904. return "unknown_operation";
  2905. }
  2906. #define nfsd4_voidres nfsd4_voidargs
  2907. struct nfsd4_voidargs { int dummy; };
  2908. static const struct svc_procedure nfsd_procedures4[2] = {
  2909. [NFSPROC4_NULL] = {
  2910. .pc_func = nfsd4_proc_null,
  2911. .pc_decode = nfs4svc_decode_voidarg,
  2912. .pc_encode = nfs4svc_encode_voidres,
  2913. .pc_argsize = sizeof(struct nfsd4_voidargs),
  2914. .pc_ressize = sizeof(struct nfsd4_voidres),
  2915. .pc_cachetype = RC_NOCACHE,
  2916. .pc_xdrressize = 1,
  2917. },
  2918. [NFSPROC4_COMPOUND] = {
  2919. .pc_func = nfsd4_proc_compound,
  2920. .pc_decode = nfs4svc_decode_compoundargs,
  2921. .pc_encode = nfs4svc_encode_compoundres,
  2922. .pc_argsize = sizeof(struct nfsd4_compoundargs),
  2923. .pc_ressize = sizeof(struct nfsd4_compoundres),
  2924. .pc_release = nfsd4_release_compoundargs,
  2925. .pc_cachetype = RC_NOCACHE,
  2926. .pc_xdrressize = NFSD_BUFSIZE/4,
  2927. },
  2928. };
  2929. static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
  2930. const struct svc_version nfsd_version4 = {
  2931. .vs_vers = 4,
  2932. .vs_nproc = 2,
  2933. .vs_proc = nfsd_procedures4,
  2934. .vs_count = nfsd_count3,
  2935. .vs_dispatch = nfsd_dispatch,
  2936. .vs_xdrsize = NFS4_SVC_XDRSIZE,
  2937. .vs_rpcb_optnl = true,
  2938. .vs_need_cong_ctrl = true,
  2939. };
  2940. /*
  2941. * Local variables:
  2942. * c-basic-offset: 8
  2943. * End:
  2944. */