nfs4proc.c 111 KB

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