nfs4proc.c 77 KB

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