nfs4proc.c 98 KB

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