smb2ops.c 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SMB2 version specific operations
  4. *
  5. * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
  6. */
  7. #include <linux/pagemap.h>
  8. #include <linux/vfs.h>
  9. #include <linux/falloc.h>
  10. #include <linux/scatterlist.h>
  11. #include <linux/uuid.h>
  12. #include <linux/sort.h>
  13. #include <crypto/aead.h>
  14. #include <linux/fiemap.h>
  15. #include <linux/folio_queue.h>
  16. #include <uapi/linux/magic.h>
  17. #include "cifsfs.h"
  18. #include "cifsglob.h"
  19. #include "smb2pdu.h"
  20. #include "smb2proto.h"
  21. #include "cifsproto.h"
  22. #include "cifs_debug.h"
  23. #include "cifs_unicode.h"
  24. #include "../common/smb2status.h"
  25. #include "smb2glob.h"
  26. #include "cifs_ioctl.h"
  27. #include "smbdirect.h"
  28. #include "fscache.h"
  29. #include "fs_context.h"
  30. #include "cached_dir.h"
  31. #include "reparse.h"
  32. /* Change credits for different ops and return the total number of credits */
  33. static int
  34. change_conf(struct TCP_Server_Info *server)
  35. {
  36. server->credits += server->echo_credits + server->oplock_credits;
  37. if (server->credits > server->max_credits)
  38. server->credits = server->max_credits;
  39. server->oplock_credits = server->echo_credits = 0;
  40. switch (server->credits) {
  41. case 0:
  42. return 0;
  43. case 1:
  44. server->echoes = false;
  45. server->oplocks = false;
  46. break;
  47. case 2:
  48. server->echoes = true;
  49. server->oplocks = false;
  50. server->echo_credits = 1;
  51. break;
  52. default:
  53. server->echoes = true;
  54. if (enable_oplocks) {
  55. server->oplocks = true;
  56. server->oplock_credits = 1;
  57. } else
  58. server->oplocks = false;
  59. server->echo_credits = 1;
  60. }
  61. server->credits -= server->echo_credits + server->oplock_credits;
  62. return server->credits + server->echo_credits + server->oplock_credits;
  63. }
  64. static void
  65. smb2_add_credits(struct TCP_Server_Info *server,
  66. struct cifs_credits *credits, const int optype)
  67. {
  68. int *val, rc = -1;
  69. int scredits, in_flight;
  70. unsigned int add = credits->value;
  71. unsigned int instance = credits->instance;
  72. bool reconnect_detected = false;
  73. bool reconnect_with_invalid_credits = false;
  74. spin_lock(&server->req_lock);
  75. val = server->ops->get_credits_field(server, optype);
  76. /* eg found case where write overlapping reconnect messed up credits */
  77. if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
  78. reconnect_with_invalid_credits = true;
  79. if ((instance == 0) || (instance == server->reconnect_instance))
  80. *val += add;
  81. else
  82. reconnect_detected = true;
  83. if (*val > 65000) {
  84. *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
  85. pr_warn_once("server overflowed SMB3 credits\n");
  86. trace_smb3_overflow_credits(server->CurrentMid,
  87. server->conn_id, server->hostname, *val,
  88. add, server->in_flight);
  89. }
  90. if (credits->in_flight_check > 1) {
  91. pr_warn_once("rreq R=%08x[%x] Credits not in flight\n",
  92. credits->rreq_debug_id, credits->rreq_debug_index);
  93. } else {
  94. credits->in_flight_check = 2;
  95. }
  96. if (WARN_ON_ONCE(server->in_flight == 0)) {
  97. pr_warn_once("rreq R=%08x[%x] Zero in_flight\n",
  98. credits->rreq_debug_id, credits->rreq_debug_index);
  99. trace_smb3_rw_credits(credits->rreq_debug_id,
  100. credits->rreq_debug_index,
  101. credits->value,
  102. server->credits, server->in_flight, 0,
  103. cifs_trace_rw_credits_zero_in_flight);
  104. }
  105. server->in_flight--;
  106. if (server->in_flight == 0 &&
  107. ((optype & CIFS_OP_MASK) != CIFS_NEG_OP) &&
  108. ((optype & CIFS_OP_MASK) != CIFS_SESS_OP))
  109. rc = change_conf(server);
  110. /*
  111. * Sometimes server returns 0 credits on oplock break ack - we need to
  112. * rebalance credits in this case.
  113. */
  114. else if (server->in_flight > 0 && server->oplock_credits == 0 &&
  115. server->oplocks) {
  116. if (server->credits > 1) {
  117. server->credits--;
  118. server->oplock_credits++;
  119. }
  120. } else if ((server->in_flight > 0) && (server->oplock_credits > 3) &&
  121. ((optype & CIFS_OP_MASK) == CIFS_OBREAK_OP))
  122. /* if now have too many oplock credits, rebalance so don't starve normal ops */
  123. change_conf(server);
  124. scredits = *val;
  125. in_flight = server->in_flight;
  126. spin_unlock(&server->req_lock);
  127. wake_up(&server->request_q);
  128. if (reconnect_detected) {
  129. trace_smb3_reconnect_detected(server->CurrentMid,
  130. server->conn_id, server->hostname, scredits, add, in_flight);
  131. cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
  132. add, instance);
  133. }
  134. if (reconnect_with_invalid_credits) {
  135. trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
  136. server->conn_id, server->hostname, scredits, add, in_flight);
  137. cifs_dbg(FYI, "Negotiate operation when server credits is non-zero. Optype: %d, server credits: %d, credits added: %d\n",
  138. optype, scredits, add);
  139. }
  140. spin_lock(&server->srv_lock);
  141. if (server->tcpStatus == CifsNeedReconnect
  142. || server->tcpStatus == CifsExiting) {
  143. spin_unlock(&server->srv_lock);
  144. return;
  145. }
  146. spin_unlock(&server->srv_lock);
  147. switch (rc) {
  148. case -1:
  149. /* change_conf hasn't been executed */
  150. break;
  151. case 0:
  152. cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
  153. break;
  154. case 1:
  155. cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
  156. break;
  157. case 2:
  158. cifs_dbg(FYI, "disabling oplocks\n");
  159. break;
  160. default:
  161. /* change_conf rebalanced credits for different types */
  162. break;
  163. }
  164. trace_smb3_add_credits(server->CurrentMid,
  165. server->conn_id, server->hostname, scredits, add, in_flight);
  166. cifs_dbg(FYI, "%s: added %u credits total=%d\n", __func__, add, scredits);
  167. }
  168. static void
  169. smb2_set_credits(struct TCP_Server_Info *server, const int val)
  170. {
  171. int scredits, in_flight;
  172. spin_lock(&server->req_lock);
  173. server->credits = val;
  174. if (val == 1) {
  175. server->reconnect_instance++;
  176. /*
  177. * ChannelSequence updated for all channels in primary channel so that consistent
  178. * across SMB3 requests sent on any channel. See MS-SMB2 3.2.4.1 and 3.2.7.1
  179. */
  180. if (SERVER_IS_CHAN(server))
  181. server->primary_server->channel_sequence_num++;
  182. else
  183. server->channel_sequence_num++;
  184. }
  185. scredits = server->credits;
  186. in_flight = server->in_flight;
  187. spin_unlock(&server->req_lock);
  188. trace_smb3_set_credits(server->CurrentMid,
  189. server->conn_id, server->hostname, scredits, val, in_flight);
  190. cifs_dbg(FYI, "%s: set %u credits\n", __func__, val);
  191. /* don't log while holding the lock */
  192. if (val == 1)
  193. cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
  194. }
  195. static int *
  196. smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
  197. {
  198. switch (optype) {
  199. case CIFS_ECHO_OP:
  200. return &server->echo_credits;
  201. case CIFS_OBREAK_OP:
  202. return &server->oplock_credits;
  203. default:
  204. return &server->credits;
  205. }
  206. }
  207. static unsigned int
  208. smb2_get_credits(struct mid_q_entry *mid)
  209. {
  210. return mid->credits_received;
  211. }
  212. static int
  213. smb2_wait_mtu_credits(struct TCP_Server_Info *server, size_t size,
  214. size_t *num, struct cifs_credits *credits)
  215. {
  216. int rc = 0;
  217. unsigned int scredits, in_flight;
  218. spin_lock(&server->req_lock);
  219. while (1) {
  220. spin_unlock(&server->req_lock);
  221. spin_lock(&server->srv_lock);
  222. if (server->tcpStatus == CifsExiting) {
  223. spin_unlock(&server->srv_lock);
  224. return -ENOENT;
  225. }
  226. spin_unlock(&server->srv_lock);
  227. spin_lock(&server->req_lock);
  228. if (server->credits <= 0) {
  229. spin_unlock(&server->req_lock);
  230. cifs_num_waiters_inc(server);
  231. rc = wait_event_killable(server->request_q,
  232. has_credits(server, &server->credits, 1));
  233. cifs_num_waiters_dec(server);
  234. if (rc)
  235. return rc;
  236. spin_lock(&server->req_lock);
  237. } else {
  238. scredits = server->credits;
  239. /* can deadlock with reopen */
  240. if (scredits <= 8) {
  241. *num = SMB2_MAX_BUFFER_SIZE;
  242. credits->value = 0;
  243. credits->instance = 0;
  244. break;
  245. }
  246. /* leave some credits for reopen and other ops */
  247. scredits -= 8;
  248. *num = min_t(unsigned int, size,
  249. scredits * SMB2_MAX_BUFFER_SIZE);
  250. credits->value =
  251. DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
  252. credits->instance = server->reconnect_instance;
  253. server->credits -= credits->value;
  254. server->in_flight++;
  255. if (server->in_flight > server->max_in_flight)
  256. server->max_in_flight = server->in_flight;
  257. break;
  258. }
  259. }
  260. scredits = server->credits;
  261. in_flight = server->in_flight;
  262. spin_unlock(&server->req_lock);
  263. trace_smb3_wait_credits(server->CurrentMid,
  264. server->conn_id, server->hostname, scredits, -(credits->value), in_flight);
  265. cifs_dbg(FYI, "%s: removed %u credits total=%d\n",
  266. __func__, credits->value, scredits);
  267. return rc;
  268. }
  269. static int
  270. smb2_adjust_credits(struct TCP_Server_Info *server,
  271. struct cifs_io_subrequest *subreq,
  272. unsigned int /*enum smb3_rw_credits_trace*/ trace)
  273. {
  274. struct cifs_credits *credits = &subreq->credits;
  275. int new_val = DIV_ROUND_UP(subreq->subreq.len - subreq->subreq.transferred,
  276. SMB2_MAX_BUFFER_SIZE);
  277. int scredits, in_flight;
  278. if (!credits->value || credits->value == new_val)
  279. return 0;
  280. if (credits->value < new_val) {
  281. trace_smb3_rw_credits(subreq->rreq->debug_id,
  282. subreq->subreq.debug_index,
  283. credits->value,
  284. server->credits, server->in_flight,
  285. new_val - credits->value,
  286. cifs_trace_rw_credits_no_adjust_up);
  287. trace_smb3_too_many_credits(server->CurrentMid,
  288. server->conn_id, server->hostname, 0, credits->value - new_val, 0);
  289. cifs_server_dbg(VFS, "R=%x[%x] request has less credits (%d) than required (%d)",
  290. subreq->rreq->debug_id, subreq->subreq.debug_index,
  291. credits->value, new_val);
  292. return -EOPNOTSUPP;
  293. }
  294. spin_lock(&server->req_lock);
  295. if (server->reconnect_instance != credits->instance) {
  296. scredits = server->credits;
  297. in_flight = server->in_flight;
  298. spin_unlock(&server->req_lock);
  299. trace_smb3_rw_credits(subreq->rreq->debug_id,
  300. subreq->subreq.debug_index,
  301. credits->value,
  302. server->credits, server->in_flight,
  303. new_val - credits->value,
  304. cifs_trace_rw_credits_old_session);
  305. trace_smb3_reconnect_detected(server->CurrentMid,
  306. server->conn_id, server->hostname, scredits,
  307. credits->value - new_val, in_flight);
  308. cifs_server_dbg(VFS, "R=%x[%x] trying to return %d credits to old session\n",
  309. subreq->rreq->debug_id, subreq->subreq.debug_index,
  310. credits->value - new_val);
  311. return -EAGAIN;
  312. }
  313. trace_smb3_rw_credits(subreq->rreq->debug_id,
  314. subreq->subreq.debug_index,
  315. credits->value,
  316. server->credits, server->in_flight,
  317. new_val - credits->value, trace);
  318. server->credits += credits->value - new_val;
  319. scredits = server->credits;
  320. in_flight = server->in_flight;
  321. spin_unlock(&server->req_lock);
  322. wake_up(&server->request_q);
  323. trace_smb3_adj_credits(server->CurrentMid,
  324. server->conn_id, server->hostname, scredits,
  325. credits->value - new_val, in_flight);
  326. cifs_dbg(FYI, "%s: adjust added %u credits total=%d\n",
  327. __func__, credits->value - new_val, scredits);
  328. credits->value = new_val;
  329. return 0;
  330. }
  331. static __u64
  332. smb2_get_next_mid(struct TCP_Server_Info *server)
  333. {
  334. __u64 mid;
  335. /* for SMB2 we need the current value */
  336. spin_lock(&server->mid_lock);
  337. mid = server->CurrentMid++;
  338. spin_unlock(&server->mid_lock);
  339. return mid;
  340. }
  341. static void
  342. smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
  343. {
  344. spin_lock(&server->mid_lock);
  345. if (server->CurrentMid >= val)
  346. server->CurrentMid -= val;
  347. spin_unlock(&server->mid_lock);
  348. }
  349. static struct mid_q_entry *
  350. __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
  351. {
  352. struct mid_q_entry *mid;
  353. struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
  354. __u64 wire_mid = le64_to_cpu(shdr->MessageId);
  355. if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
  356. cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
  357. return NULL;
  358. }
  359. spin_lock(&server->mid_lock);
  360. list_for_each_entry(mid, &server->pending_mid_q, qhead) {
  361. if ((mid->mid == wire_mid) &&
  362. (mid->mid_state == MID_REQUEST_SUBMITTED) &&
  363. (mid->command == shdr->Command)) {
  364. kref_get(&mid->refcount);
  365. if (dequeue) {
  366. list_del_init(&mid->qhead);
  367. mid->mid_flags |= MID_DELETED;
  368. }
  369. spin_unlock(&server->mid_lock);
  370. return mid;
  371. }
  372. }
  373. spin_unlock(&server->mid_lock);
  374. return NULL;
  375. }
  376. static struct mid_q_entry *
  377. smb2_find_mid(struct TCP_Server_Info *server, char *buf)
  378. {
  379. return __smb2_find_mid(server, buf, false);
  380. }
  381. static struct mid_q_entry *
  382. smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
  383. {
  384. return __smb2_find_mid(server, buf, true);
  385. }
  386. static void
  387. smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
  388. {
  389. #ifdef CONFIG_CIFS_DEBUG2
  390. struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
  391. cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
  392. shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
  393. shdr->Id.SyncId.ProcessId);
  394. if (!server->ops->check_message(buf, server->total_read, server)) {
  395. cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
  396. server->ops->calc_smb_size(buf));
  397. }
  398. #endif
  399. }
  400. static bool
  401. smb2_need_neg(struct TCP_Server_Info *server)
  402. {
  403. return server->max_read == 0;
  404. }
  405. static int
  406. smb2_negotiate(const unsigned int xid,
  407. struct cifs_ses *ses,
  408. struct TCP_Server_Info *server)
  409. {
  410. int rc;
  411. spin_lock(&server->mid_lock);
  412. server->CurrentMid = 0;
  413. spin_unlock(&server->mid_lock);
  414. rc = SMB2_negotiate(xid, ses, server);
  415. return rc;
  416. }
  417. static inline unsigned int
  418. prevent_zero_iosize(unsigned int size, const char *type)
  419. {
  420. if (size == 0) {
  421. cifs_dbg(VFS, "SMB: Zero %ssize calculated, using minimum value %u\n",
  422. type, CIFS_MIN_DEFAULT_IOSIZE);
  423. return CIFS_MIN_DEFAULT_IOSIZE;
  424. }
  425. return size;
  426. }
  427. static unsigned int
  428. smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
  429. {
  430. struct TCP_Server_Info *server = tcon->ses->server;
  431. unsigned int wsize;
  432. /* start with specified wsize, or default */
  433. wsize = ctx->got_wsize ? ctx->vol_wsize : CIFS_DEFAULT_IOSIZE;
  434. wsize = min_t(unsigned int, wsize, server->max_write);
  435. if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
  436. wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
  437. return prevent_zero_iosize(wsize, "w");
  438. }
  439. static unsigned int
  440. smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
  441. {
  442. struct TCP_Server_Info *server = tcon->ses->server;
  443. unsigned int wsize;
  444. /* start with specified wsize, or default */
  445. wsize = ctx->got_wsize ? ctx->vol_wsize : SMB3_DEFAULT_IOSIZE;
  446. wsize = min_t(unsigned int, wsize, server->max_write);
  447. #ifdef CONFIG_CIFS_SMB_DIRECT
  448. if (server->rdma) {
  449. struct smbdirect_socket_parameters *sp =
  450. &server->smbd_conn->socket.parameters;
  451. if (server->sign)
  452. /*
  453. * Account for SMB2 data transfer packet header and
  454. * possible encryption header
  455. */
  456. wsize = min_t(unsigned int,
  457. wsize,
  458. sp->max_fragmented_send_size -
  459. SMB2_READWRITE_PDU_HEADER_SIZE -
  460. sizeof(struct smb2_transform_hdr));
  461. else
  462. wsize = min_t(unsigned int,
  463. wsize, sp->max_read_write_size);
  464. }
  465. #endif
  466. if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
  467. wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
  468. return prevent_zero_iosize(wsize, "w");
  469. }
  470. static unsigned int
  471. smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
  472. {
  473. struct TCP_Server_Info *server = tcon->ses->server;
  474. unsigned int rsize;
  475. /* start with specified rsize, or default */
  476. rsize = ctx->got_rsize ? ctx->vol_rsize : CIFS_DEFAULT_IOSIZE;
  477. rsize = min_t(unsigned int, rsize, server->max_read);
  478. if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
  479. rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
  480. return prevent_zero_iosize(rsize, "r");
  481. }
  482. static unsigned int
  483. smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
  484. {
  485. struct TCP_Server_Info *server = tcon->ses->server;
  486. unsigned int rsize;
  487. /* start with specified rsize, or default */
  488. rsize = ctx->got_rsize ? ctx->vol_rsize : SMB3_DEFAULT_IOSIZE;
  489. rsize = min_t(unsigned int, rsize, server->max_read);
  490. #ifdef CONFIG_CIFS_SMB_DIRECT
  491. if (server->rdma) {
  492. struct smbdirect_socket_parameters *sp =
  493. &server->smbd_conn->socket.parameters;
  494. if (server->sign)
  495. /*
  496. * Account for SMB2 data transfer packet header and
  497. * possible encryption header
  498. */
  499. rsize = min_t(unsigned int,
  500. rsize,
  501. sp->max_fragmented_recv_size -
  502. SMB2_READWRITE_PDU_HEADER_SIZE -
  503. sizeof(struct smb2_transform_hdr));
  504. else
  505. rsize = min_t(unsigned int,
  506. rsize, sp->max_read_write_size);
  507. }
  508. #endif
  509. if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
  510. rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
  511. return prevent_zero_iosize(rsize, "r");
  512. }
  513. /*
  514. * compare two interfaces a and b
  515. * return 0 if everything matches.
  516. * return 1 if a is rdma capable, or rss capable, or has higher link speed
  517. * return -1 otherwise.
  518. */
  519. static int
  520. iface_cmp(struct cifs_server_iface *a, struct cifs_server_iface *b)
  521. {
  522. int cmp_ret = 0;
  523. WARN_ON(!a || !b);
  524. if (a->rdma_capable == b->rdma_capable) {
  525. if (a->rss_capable == b->rss_capable) {
  526. if (a->speed == b->speed) {
  527. cmp_ret = cifs_ipaddr_cmp((struct sockaddr *) &a->sockaddr,
  528. (struct sockaddr *) &b->sockaddr);
  529. if (!cmp_ret)
  530. return 0;
  531. else if (cmp_ret > 0)
  532. return 1;
  533. else
  534. return -1;
  535. } else if (a->speed > b->speed)
  536. return 1;
  537. else
  538. return -1;
  539. } else if (a->rss_capable > b->rss_capable)
  540. return 1;
  541. else
  542. return -1;
  543. } else if (a->rdma_capable > b->rdma_capable)
  544. return 1;
  545. else
  546. return -1;
  547. }
  548. static int
  549. parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
  550. size_t buf_len, struct cifs_ses *ses, bool in_mount)
  551. {
  552. struct network_interface_info_ioctl_rsp *p;
  553. struct sockaddr_in *addr4;
  554. struct sockaddr_in6 *addr6;
  555. struct iface_info_ipv4 *p4;
  556. struct iface_info_ipv6 *p6;
  557. struct cifs_server_iface *info = NULL, *iface = NULL, *niface = NULL;
  558. struct cifs_server_iface tmp_iface;
  559. ssize_t bytes_left;
  560. size_t next = 0;
  561. int nb_iface = 0;
  562. int rc = 0, ret = 0;
  563. bytes_left = buf_len;
  564. p = buf;
  565. spin_lock(&ses->iface_lock);
  566. /* do not query too frequently, this time with lock held */
  567. if (ses->iface_last_update &&
  568. time_before(jiffies, ses->iface_last_update +
  569. (SMB_INTERFACE_POLL_INTERVAL * HZ))) {
  570. spin_unlock(&ses->iface_lock);
  571. return 0;
  572. }
  573. /*
  574. * Go through iface_list and mark them as inactive
  575. */
  576. list_for_each_entry_safe(iface, niface, &ses->iface_list,
  577. iface_head)
  578. iface->is_active = 0;
  579. spin_unlock(&ses->iface_lock);
  580. /*
  581. * Samba server e.g. can return an empty interface list in some cases,
  582. * which would only be a problem if we were requesting multichannel
  583. */
  584. if (bytes_left == 0) {
  585. /* avoid spamming logs every 10 minutes, so log only in mount */
  586. if ((ses->chan_max > 1) && in_mount)
  587. cifs_dbg(VFS,
  588. "multichannel not available\n"
  589. "Empty network interface list returned by server %s\n",
  590. ses->server->hostname);
  591. rc = -EOPNOTSUPP;
  592. ses->iface_last_update = jiffies;
  593. goto out;
  594. }
  595. while (bytes_left >= (ssize_t)sizeof(*p)) {
  596. memset(&tmp_iface, 0, sizeof(tmp_iface));
  597. /* default to 1Gbps when link speed is unset */
  598. tmp_iface.speed = le64_to_cpu(p->LinkSpeed) ?: 1000000000;
  599. tmp_iface.rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
  600. tmp_iface.rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
  601. switch (p->Family) {
  602. /*
  603. * The kernel and wire socket structures have the same
  604. * layout and use network byte order but make the
  605. * conversion explicit in case either one changes.
  606. */
  607. case INTERNETWORK:
  608. addr4 = (struct sockaddr_in *)&tmp_iface.sockaddr;
  609. p4 = (struct iface_info_ipv4 *)p->Buffer;
  610. addr4->sin_family = AF_INET;
  611. memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
  612. /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
  613. addr4->sin_port = cpu_to_be16(CIFS_PORT);
  614. cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
  615. &addr4->sin_addr);
  616. break;
  617. case INTERNETWORKV6:
  618. addr6 = (struct sockaddr_in6 *)&tmp_iface.sockaddr;
  619. p6 = (struct iface_info_ipv6 *)p->Buffer;
  620. addr6->sin6_family = AF_INET6;
  621. memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
  622. /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
  623. addr6->sin6_flowinfo = 0;
  624. addr6->sin6_scope_id = 0;
  625. addr6->sin6_port = cpu_to_be16(CIFS_PORT);
  626. cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
  627. &addr6->sin6_addr);
  628. break;
  629. default:
  630. cifs_dbg(VFS,
  631. "%s: skipping unsupported socket family\n",
  632. __func__);
  633. goto next_iface;
  634. }
  635. /*
  636. * The iface_list is assumed to be sorted by speed.
  637. * Check if the new interface exists in that list.
  638. * NEVER change iface. it could be in use.
  639. * Add a new one instead
  640. */
  641. spin_lock(&ses->iface_lock);
  642. list_for_each_entry_safe(iface, niface, &ses->iface_list,
  643. iface_head) {
  644. ret = iface_cmp(iface, &tmp_iface);
  645. if (!ret) {
  646. iface->is_active = 1;
  647. spin_unlock(&ses->iface_lock);
  648. goto next_iface;
  649. } else if (ret < 0) {
  650. /* all remaining ifaces are slower */
  651. kref_get(&iface->refcount);
  652. break;
  653. }
  654. }
  655. spin_unlock(&ses->iface_lock);
  656. /* no match. insert the entry in the list */
  657. info = kmalloc(sizeof(struct cifs_server_iface),
  658. GFP_KERNEL);
  659. if (!info) {
  660. rc = -ENOMEM;
  661. goto out;
  662. }
  663. memcpy(info, &tmp_iface, sizeof(tmp_iface));
  664. /* add this new entry to the list */
  665. kref_init(&info->refcount);
  666. info->is_active = 1;
  667. cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, ses->iface_count);
  668. cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
  669. cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
  670. le32_to_cpu(p->Capability));
  671. spin_lock(&ses->iface_lock);
  672. if (!list_entry_is_head(iface, &ses->iface_list, iface_head)) {
  673. list_add_tail(&info->iface_head, &iface->iface_head);
  674. kref_put(&iface->refcount, release_iface);
  675. } else
  676. list_add_tail(&info->iface_head, &ses->iface_list);
  677. ses->iface_count++;
  678. spin_unlock(&ses->iface_lock);
  679. next_iface:
  680. nb_iface++;
  681. next = le32_to_cpu(p->Next);
  682. if (!next) {
  683. bytes_left -= sizeof(*p);
  684. break;
  685. }
  686. /* Validate that Next doesn't point beyond the buffer */
  687. if (next > bytes_left) {
  688. cifs_dbg(VFS, "%s: invalid Next pointer %zu > %zd\n",
  689. __func__, next, bytes_left);
  690. rc = -EINVAL;
  691. goto out;
  692. }
  693. p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
  694. bytes_left -= next;
  695. }
  696. if (!nb_iface) {
  697. cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
  698. rc = -EINVAL;
  699. goto out;
  700. }
  701. /* Azure rounds the buffer size up 8, to a 16 byte boundary */
  702. if ((bytes_left > 8) ||
  703. (bytes_left >= offsetof(struct network_interface_info_ioctl_rsp, Next)
  704. + sizeof(p->Next) && p->Next))
  705. cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
  706. ses->iface_last_update = jiffies;
  707. out:
  708. /*
  709. * Go through the list again and put the inactive entries
  710. */
  711. spin_lock(&ses->iface_lock);
  712. list_for_each_entry_safe(iface, niface, &ses->iface_list,
  713. iface_head) {
  714. if (!iface->is_active) {
  715. list_del(&iface->iface_head);
  716. kref_put(&iface->refcount, release_iface);
  717. ses->iface_count--;
  718. }
  719. }
  720. spin_unlock(&ses->iface_lock);
  721. return rc;
  722. }
  723. int
  724. SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon, bool in_mount)
  725. {
  726. int rc;
  727. unsigned int ret_data_len = 0;
  728. struct network_interface_info_ioctl_rsp *out_buf = NULL;
  729. struct cifs_ses *ses = tcon->ses;
  730. struct TCP_Server_Info *pserver;
  731. /* do not query too frequently */
  732. if (ses->iface_last_update &&
  733. time_before(jiffies, ses->iface_last_update +
  734. (SMB_INTERFACE_POLL_INTERVAL * HZ)))
  735. return 0;
  736. rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
  737. FSCTL_QUERY_NETWORK_INTERFACE_INFO,
  738. NULL /* no data input */, 0 /* no data input */,
  739. CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
  740. if (rc == -EOPNOTSUPP) {
  741. cifs_dbg(FYI,
  742. "server does not support query network interfaces\n");
  743. ret_data_len = 0;
  744. } else if (rc != 0) {
  745. cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
  746. goto out;
  747. }
  748. rc = parse_server_interfaces(out_buf, ret_data_len, ses, in_mount);
  749. if (rc)
  750. goto out;
  751. /* check if iface is still active */
  752. spin_lock(&ses->chan_lock);
  753. pserver = ses->chans[0].server;
  754. if (pserver && !cifs_chan_is_iface_active(ses, pserver)) {
  755. spin_unlock(&ses->chan_lock);
  756. cifs_chan_update_iface(ses, pserver);
  757. spin_lock(&ses->chan_lock);
  758. }
  759. spin_unlock(&ses->chan_lock);
  760. out:
  761. kfree(out_buf);
  762. return rc;
  763. }
  764. static void
  765. smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
  766. struct cifs_sb_info *cifs_sb)
  767. {
  768. int rc;
  769. __le16 srch_path = 0; /* Null - open root of share */
  770. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  771. struct cifs_open_parms oparms;
  772. struct cifs_fid fid;
  773. struct cached_fid *cfid = NULL;
  774. oparms = (struct cifs_open_parms) {
  775. .tcon = tcon,
  776. .path = "",
  777. .desired_access = FILE_READ_ATTRIBUTES,
  778. .disposition = FILE_OPEN,
  779. .create_options = cifs_create_options(cifs_sb, 0),
  780. .fid = &fid,
  781. };
  782. rc = open_cached_dir(xid, tcon, "", cifs_sb, false, &cfid);
  783. if (rc == 0)
  784. memcpy(&fid, &cfid->fid, sizeof(struct cifs_fid));
  785. else
  786. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
  787. NULL, NULL);
  788. if (rc)
  789. return;
  790. SMB3_request_interfaces(xid, tcon, true /* called during mount */);
  791. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  792. FS_ATTRIBUTE_INFORMATION);
  793. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  794. FS_DEVICE_INFORMATION);
  795. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  796. FS_VOLUME_INFORMATION);
  797. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  798. FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
  799. if (cfid == NULL)
  800. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  801. else
  802. close_cached_dir(cfid);
  803. }
  804. static void
  805. smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
  806. struct cifs_sb_info *cifs_sb)
  807. {
  808. int rc;
  809. __le16 srch_path = 0; /* Null - open root of share */
  810. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  811. struct cifs_open_parms oparms;
  812. struct cifs_fid fid;
  813. oparms = (struct cifs_open_parms) {
  814. .tcon = tcon,
  815. .path = "",
  816. .desired_access = FILE_READ_ATTRIBUTES,
  817. .disposition = FILE_OPEN,
  818. .create_options = cifs_create_options(cifs_sb, 0),
  819. .fid = &fid,
  820. };
  821. rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
  822. NULL, NULL);
  823. if (rc)
  824. return;
  825. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  826. FS_ATTRIBUTE_INFORMATION);
  827. SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  828. FS_DEVICE_INFORMATION);
  829. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  830. }
  831. static int
  832. smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
  833. struct cifs_sb_info *cifs_sb, const char *full_path)
  834. {
  835. __le16 *utf16_path;
  836. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  837. int err_buftype = CIFS_NO_BUFFER;
  838. struct cifs_open_parms oparms;
  839. struct kvec err_iov = {};
  840. struct cifs_fid fid;
  841. struct cached_fid *cfid;
  842. bool islink;
  843. int rc, rc2;
  844. rc = open_cached_dir(xid, tcon, full_path, cifs_sb, true, &cfid);
  845. if (!rc) {
  846. if (cfid->has_lease) {
  847. close_cached_dir(cfid);
  848. return 0;
  849. }
  850. close_cached_dir(cfid);
  851. }
  852. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  853. if (!utf16_path)
  854. return -ENOMEM;
  855. oparms = (struct cifs_open_parms) {
  856. .tcon = tcon,
  857. .path = full_path,
  858. .desired_access = FILE_READ_ATTRIBUTES,
  859. .disposition = FILE_OPEN,
  860. .create_options = cifs_create_options(cifs_sb, 0),
  861. .fid = &fid,
  862. };
  863. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
  864. &err_iov, &err_buftype);
  865. if (rc) {
  866. struct smb2_hdr *hdr = err_iov.iov_base;
  867. if (unlikely(!hdr || err_buftype == CIFS_NO_BUFFER))
  868. goto out;
  869. if (rc != -EREMOTE && hdr->Status == STATUS_OBJECT_NAME_INVALID) {
  870. rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb,
  871. full_path, &islink);
  872. if (rc2) {
  873. rc = rc2;
  874. goto out;
  875. }
  876. if (islink)
  877. rc = -EREMOTE;
  878. }
  879. if (rc == -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) && cifs_sb &&
  880. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS))
  881. rc = -EOPNOTSUPP;
  882. goto out;
  883. }
  884. rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  885. out:
  886. free_rsp_buf(err_buftype, err_iov.iov_base);
  887. kfree(utf16_path);
  888. return rc;
  889. }
  890. static int smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
  891. struct cifs_sb_info *cifs_sb, const char *full_path,
  892. u64 *uniqueid, struct cifs_open_info_data *data)
  893. {
  894. *uniqueid = le64_to_cpu(data->fi.IndexNumber);
  895. return 0;
  896. }
  897. static int smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
  898. struct cifsFileInfo *cfile, struct cifs_open_info_data *data)
  899. {
  900. struct cifs_fid *fid = &cfile->fid;
  901. if (cfile->symlink_target) {
  902. data->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
  903. if (!data->symlink_target)
  904. return -ENOMEM;
  905. }
  906. data->contains_posix_file_info = false;
  907. return SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid, &data->fi);
  908. }
  909. #ifdef CONFIG_CIFS_XATTR
  910. static ssize_t
  911. move_smb2_ea_to_cifs(char *dst, size_t dst_size,
  912. struct smb2_file_full_ea_info *src, size_t src_size,
  913. const unsigned char *ea_name)
  914. {
  915. int rc = 0;
  916. unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
  917. char *name, *value;
  918. size_t buf_size = dst_size;
  919. size_t name_len, value_len, user_name_len;
  920. while (src_size > 0) {
  921. name_len = (size_t)src->ea_name_length;
  922. value_len = (size_t)le16_to_cpu(src->ea_value_length);
  923. if (name_len == 0)
  924. break;
  925. if (src_size < 8 + name_len + 1 + value_len) {
  926. cifs_dbg(FYI, "EA entry goes beyond length of list\n");
  927. rc = -EIO;
  928. goto out;
  929. }
  930. name = &src->ea_data[0];
  931. value = &src->ea_data[src->ea_name_length + 1];
  932. if (ea_name) {
  933. if (ea_name_len == name_len &&
  934. memcmp(ea_name, name, name_len) == 0) {
  935. rc = value_len;
  936. if (dst_size == 0)
  937. goto out;
  938. if (dst_size < value_len) {
  939. rc = -ERANGE;
  940. goto out;
  941. }
  942. memcpy(dst, value, value_len);
  943. goto out;
  944. }
  945. } else {
  946. /* 'user.' plus a terminating null */
  947. user_name_len = 5 + 1 + name_len;
  948. if (buf_size == 0) {
  949. /* skip copy - calc size only */
  950. rc += user_name_len;
  951. } else if (dst_size >= user_name_len) {
  952. dst_size -= user_name_len;
  953. memcpy(dst, "user.", 5);
  954. dst += 5;
  955. memcpy(dst, src->ea_data, name_len);
  956. dst += name_len;
  957. *dst = 0;
  958. ++dst;
  959. rc += user_name_len;
  960. } else {
  961. /* stop before overrun buffer */
  962. rc = -ERANGE;
  963. break;
  964. }
  965. }
  966. if (!src->next_entry_offset)
  967. break;
  968. if (src_size < le32_to_cpu(src->next_entry_offset)) {
  969. /* stop before overrun buffer */
  970. rc = -ERANGE;
  971. break;
  972. }
  973. src_size -= le32_to_cpu(src->next_entry_offset);
  974. src = (void *)((char *)src +
  975. le32_to_cpu(src->next_entry_offset));
  976. }
  977. /* didn't find the named attribute */
  978. if (ea_name)
  979. rc = -ENODATA;
  980. out:
  981. return (ssize_t)rc;
  982. }
  983. static ssize_t
  984. smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
  985. const unsigned char *path, const unsigned char *ea_name,
  986. char *ea_data, size_t buf_size,
  987. struct cifs_sb_info *cifs_sb)
  988. {
  989. int rc;
  990. struct kvec rsp_iov = {NULL, 0};
  991. int buftype = CIFS_NO_BUFFER;
  992. struct smb2_query_info_rsp *rsp;
  993. struct smb2_file_full_ea_info *info = NULL;
  994. rc = smb2_query_info_compound(xid, tcon, path,
  995. FILE_READ_EA,
  996. FILE_FULL_EA_INFORMATION,
  997. SMB2_O_INFO_FILE,
  998. CIFSMaxBufSize -
  999. MAX_SMB2_CREATE_RESPONSE_SIZE -
  1000. MAX_SMB2_CLOSE_RESPONSE_SIZE,
  1001. &rsp_iov, &buftype, cifs_sb);
  1002. if (rc) {
  1003. /*
  1004. * If ea_name is NULL (listxattr) and there are no EAs,
  1005. * return 0 as it's not an error. Otherwise, the specified
  1006. * ea_name was not found.
  1007. */
  1008. if (!ea_name && rc == -ENODATA)
  1009. rc = 0;
  1010. goto qeas_exit;
  1011. }
  1012. rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
  1013. rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
  1014. le32_to_cpu(rsp->OutputBufferLength),
  1015. &rsp_iov,
  1016. sizeof(struct smb2_file_full_ea_info));
  1017. if (rc)
  1018. goto qeas_exit;
  1019. info = (struct smb2_file_full_ea_info *)(
  1020. le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
  1021. rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
  1022. le32_to_cpu(rsp->OutputBufferLength), ea_name);
  1023. qeas_exit:
  1024. free_rsp_buf(buftype, rsp_iov.iov_base);
  1025. return rc;
  1026. }
  1027. static int
  1028. smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
  1029. const char *path, const char *ea_name, const void *ea_value,
  1030. const __u16 ea_value_len, const struct nls_table *nls_codepage,
  1031. struct cifs_sb_info *cifs_sb)
  1032. {
  1033. struct smb2_compound_vars *vars;
  1034. struct cifs_ses *ses = tcon->ses;
  1035. struct TCP_Server_Info *server;
  1036. struct smb_rqst *rqst;
  1037. struct kvec *rsp_iov;
  1038. __le16 *utf16_path = NULL;
  1039. int ea_name_len = strlen(ea_name);
  1040. int flags = CIFS_CP_CREATE_CLOSE_OP;
  1041. int len;
  1042. int resp_buftype[3];
  1043. struct cifs_open_parms oparms;
  1044. __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  1045. struct cifs_fid fid;
  1046. unsigned int size[1];
  1047. void *data[1];
  1048. struct smb2_file_full_ea_info *ea;
  1049. struct smb2_query_info_rsp *rsp;
  1050. int rc, used_len = 0;
  1051. int retries = 0, cur_sleep = 1;
  1052. replay_again:
  1053. /* reinitialize for possible replay */
  1054. flags = CIFS_CP_CREATE_CLOSE_OP;
  1055. oplock = SMB2_OPLOCK_LEVEL_NONE;
  1056. server = cifs_pick_channel(ses);
  1057. if (smb3_encryption_required(tcon))
  1058. flags |= CIFS_TRANSFORM_REQ;
  1059. if (ea_name_len > 255)
  1060. return -EINVAL;
  1061. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  1062. if (!utf16_path)
  1063. return -ENOMEM;
  1064. ea = NULL;
  1065. resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
  1066. vars = kzalloc(sizeof(*vars), GFP_KERNEL);
  1067. if (!vars) {
  1068. rc = -ENOMEM;
  1069. goto out_free_path;
  1070. }
  1071. rqst = vars->rqst;
  1072. rsp_iov = vars->rsp_iov;
  1073. if (ses->server->ops->query_all_EAs) {
  1074. if (!ea_value) {
  1075. rc = ses->server->ops->query_all_EAs(xid, tcon, path,
  1076. ea_name, NULL, 0,
  1077. cifs_sb);
  1078. if (rc == -ENODATA)
  1079. goto sea_exit;
  1080. } else {
  1081. /* If we are adding a attribute we should first check
  1082. * if there will be enough space available to store
  1083. * the new EA. If not we should not add it since we
  1084. * would not be able to even read the EAs back.
  1085. */
  1086. rc = smb2_query_info_compound(xid, tcon, path,
  1087. FILE_READ_EA,
  1088. FILE_FULL_EA_INFORMATION,
  1089. SMB2_O_INFO_FILE,
  1090. CIFSMaxBufSize -
  1091. MAX_SMB2_CREATE_RESPONSE_SIZE -
  1092. MAX_SMB2_CLOSE_RESPONSE_SIZE,
  1093. &rsp_iov[1], &resp_buftype[1], cifs_sb);
  1094. if (rc == 0) {
  1095. rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
  1096. used_len = le32_to_cpu(rsp->OutputBufferLength);
  1097. }
  1098. free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
  1099. resp_buftype[1] = CIFS_NO_BUFFER;
  1100. memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
  1101. rc = 0;
  1102. /* Use a fudge factor of 256 bytes in case we collide
  1103. * with a different set_EAs command.
  1104. */
  1105. if (CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
  1106. MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
  1107. used_len + ea_name_len + ea_value_len + 1) {
  1108. rc = -ENOSPC;
  1109. goto sea_exit;
  1110. }
  1111. }
  1112. }
  1113. /* Open */
  1114. rqst[0].rq_iov = vars->open_iov;
  1115. rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
  1116. oparms = (struct cifs_open_parms) {
  1117. .tcon = tcon,
  1118. .path = path,
  1119. .desired_access = FILE_WRITE_EA,
  1120. .disposition = FILE_OPEN,
  1121. .create_options = cifs_create_options(cifs_sb, 0),
  1122. .fid = &fid,
  1123. .replay = !!(retries),
  1124. };
  1125. rc = SMB2_open_init(tcon, server,
  1126. &rqst[0], &oplock, &oparms, utf16_path);
  1127. if (rc)
  1128. goto sea_exit;
  1129. smb2_set_next_command(tcon, &rqst[0]);
  1130. /* Set Info */
  1131. rqst[1].rq_iov = vars->si_iov;
  1132. rqst[1].rq_nvec = 1;
  1133. len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
  1134. ea = kzalloc(len, GFP_KERNEL);
  1135. if (ea == NULL) {
  1136. rc = -ENOMEM;
  1137. goto sea_exit;
  1138. }
  1139. ea->ea_name_length = ea_name_len;
  1140. ea->ea_value_length = cpu_to_le16(ea_value_len);
  1141. memcpy(ea->ea_data, ea_name, ea_name_len + 1);
  1142. memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
  1143. size[0] = len;
  1144. data[0] = ea;
  1145. rc = SMB2_set_info_init(tcon, server,
  1146. &rqst[1], COMPOUND_FID,
  1147. COMPOUND_FID, current->tgid,
  1148. FILE_FULL_EA_INFORMATION,
  1149. SMB2_O_INFO_FILE, 0, data, size);
  1150. if (rc)
  1151. goto sea_exit;
  1152. smb2_set_next_command(tcon, &rqst[1]);
  1153. smb2_set_related(&rqst[1]);
  1154. /* Close */
  1155. rqst[2].rq_iov = &vars->close_iov;
  1156. rqst[2].rq_nvec = 1;
  1157. rc = SMB2_close_init(tcon, server,
  1158. &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
  1159. if (rc)
  1160. goto sea_exit;
  1161. smb2_set_related(&rqst[2]);
  1162. if (retries) {
  1163. smb2_set_replay(server, &rqst[0]);
  1164. smb2_set_replay(server, &rqst[1]);
  1165. smb2_set_replay(server, &rqst[2]);
  1166. }
  1167. rc = compound_send_recv(xid, ses, server,
  1168. flags, 3, rqst,
  1169. resp_buftype, rsp_iov);
  1170. /* no need to bump num_remote_opens because handle immediately closed */
  1171. sea_exit:
  1172. kfree(ea);
  1173. SMB2_open_free(&rqst[0]);
  1174. SMB2_set_info_free(&rqst[1]);
  1175. SMB2_close_free(&rqst[2]);
  1176. free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
  1177. free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
  1178. free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
  1179. kfree(vars);
  1180. out_free_path:
  1181. kfree(utf16_path);
  1182. if (is_replayable_error(rc) &&
  1183. smb2_should_replay(tcon, &retries, &cur_sleep))
  1184. goto replay_again;
  1185. return rc;
  1186. }
  1187. #endif
  1188. static bool
  1189. smb2_can_echo(struct TCP_Server_Info *server)
  1190. {
  1191. return server->echoes;
  1192. }
  1193. static void
  1194. smb2_clear_stats(struct cifs_tcon *tcon)
  1195. {
  1196. int i;
  1197. for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
  1198. atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
  1199. atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
  1200. }
  1201. }
  1202. static void
  1203. smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
  1204. {
  1205. seq_puts(m, "\n\tShare Capabilities:");
  1206. if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
  1207. seq_puts(m, " DFS,");
  1208. if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
  1209. seq_puts(m, " CONTINUOUS AVAILABILITY,");
  1210. if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
  1211. seq_puts(m, " SCALEOUT,");
  1212. if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
  1213. seq_puts(m, " CLUSTER,");
  1214. if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
  1215. seq_puts(m, " ASYMMETRIC,");
  1216. if (tcon->capabilities == 0)
  1217. seq_puts(m, " None");
  1218. if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
  1219. seq_puts(m, " Aligned,");
  1220. if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
  1221. seq_puts(m, " Partition Aligned,");
  1222. if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
  1223. seq_puts(m, " SSD,");
  1224. if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
  1225. seq_puts(m, " TRIM-support,");
  1226. seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
  1227. seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
  1228. if (tcon->perf_sector_size)
  1229. seq_printf(m, "\tOptimal sector size: 0x%x",
  1230. tcon->perf_sector_size);
  1231. seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
  1232. }
  1233. static void
  1234. smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
  1235. {
  1236. atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
  1237. atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
  1238. /*
  1239. * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
  1240. * totals (requests sent) since those SMBs are per-session not per tcon
  1241. */
  1242. seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
  1243. (long long)(tcon->bytes_read),
  1244. (long long)(tcon->bytes_written));
  1245. seq_printf(m, "\nOpen files: %d total (local), %d open on server",
  1246. atomic_read(&tcon->num_local_opens),
  1247. atomic_read(&tcon->num_remote_opens));
  1248. seq_printf(m, "\nTreeConnects: %d total %d failed",
  1249. atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
  1250. atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
  1251. seq_printf(m, "\nTreeDisconnects: %d total %d failed",
  1252. atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
  1253. atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
  1254. seq_printf(m, "\nCreates: %d total %d failed",
  1255. atomic_read(&sent[SMB2_CREATE_HE]),
  1256. atomic_read(&failed[SMB2_CREATE_HE]));
  1257. seq_printf(m, "\nCloses: %d total %d failed",
  1258. atomic_read(&sent[SMB2_CLOSE_HE]),
  1259. atomic_read(&failed[SMB2_CLOSE_HE]));
  1260. seq_printf(m, "\nFlushes: %d total %d failed",
  1261. atomic_read(&sent[SMB2_FLUSH_HE]),
  1262. atomic_read(&failed[SMB2_FLUSH_HE]));
  1263. seq_printf(m, "\nReads: %d total %d failed",
  1264. atomic_read(&sent[SMB2_READ_HE]),
  1265. atomic_read(&failed[SMB2_READ_HE]));
  1266. seq_printf(m, "\nWrites: %d total %d failed",
  1267. atomic_read(&sent[SMB2_WRITE_HE]),
  1268. atomic_read(&failed[SMB2_WRITE_HE]));
  1269. seq_printf(m, "\nLocks: %d total %d failed",
  1270. atomic_read(&sent[SMB2_LOCK_HE]),
  1271. atomic_read(&failed[SMB2_LOCK_HE]));
  1272. seq_printf(m, "\nIOCTLs: %d total %d failed",
  1273. atomic_read(&sent[SMB2_IOCTL_HE]),
  1274. atomic_read(&failed[SMB2_IOCTL_HE]));
  1275. seq_printf(m, "\nQueryDirectories: %d total %d failed",
  1276. atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
  1277. atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
  1278. seq_printf(m, "\nChangeNotifies: %d total %d failed",
  1279. atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
  1280. atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
  1281. seq_printf(m, "\nQueryInfos: %d total %d failed",
  1282. atomic_read(&sent[SMB2_QUERY_INFO_HE]),
  1283. atomic_read(&failed[SMB2_QUERY_INFO_HE]));
  1284. seq_printf(m, "\nSetInfos: %d total %d failed",
  1285. atomic_read(&sent[SMB2_SET_INFO_HE]),
  1286. atomic_read(&failed[SMB2_SET_INFO_HE]));
  1287. seq_printf(m, "\nOplockBreaks: %d sent %d failed",
  1288. atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
  1289. atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
  1290. }
  1291. static void
  1292. smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
  1293. {
  1294. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1295. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  1296. cfile->fid.persistent_fid = fid->persistent_fid;
  1297. cfile->fid.volatile_fid = fid->volatile_fid;
  1298. cfile->fid.access = fid->access;
  1299. #ifdef CONFIG_CIFS_DEBUG2
  1300. cfile->fid.mid = fid->mid;
  1301. #endif /* CIFS_DEBUG2 */
  1302. server->ops->set_oplock_level(cinode, oplock, fid->epoch,
  1303. &fid->purge_cache);
  1304. cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
  1305. memcpy(cfile->fid.create_guid, fid->create_guid, 16);
  1306. }
  1307. static int
  1308. smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
  1309. struct cifs_fid *fid)
  1310. {
  1311. return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  1312. }
  1313. static int
  1314. smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
  1315. struct cifsFileInfo *cfile)
  1316. {
  1317. struct smb2_file_network_open_info file_inf;
  1318. struct inode *inode;
  1319. int rc;
  1320. rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
  1321. cfile->fid.volatile_fid, &file_inf);
  1322. if (rc)
  1323. return rc;
  1324. inode = d_inode(cfile->dentry);
  1325. spin_lock(&inode->i_lock);
  1326. CIFS_I(inode)->time = jiffies;
  1327. /* Creation time should not need to be updated on close */
  1328. if (file_inf.LastWriteTime)
  1329. inode_set_mtime_to_ts(inode,
  1330. cifs_NTtimeToUnix(file_inf.LastWriteTime));
  1331. if (file_inf.ChangeTime)
  1332. inode_set_ctime_to_ts(inode,
  1333. cifs_NTtimeToUnix(file_inf.ChangeTime));
  1334. if (file_inf.LastAccessTime)
  1335. inode_set_atime_to_ts(inode,
  1336. cifs_NTtimeToUnix(file_inf.LastAccessTime));
  1337. /*
  1338. * i_blocks is not related to (i_size / i_blksize),
  1339. * but instead 512 byte (2**9) size is required for
  1340. * calculating num blocks.
  1341. */
  1342. if (le64_to_cpu(file_inf.AllocationSize) > 4096)
  1343. inode->i_blocks =
  1344. (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
  1345. /* End of file and Attributes should not have to be updated on close */
  1346. spin_unlock(&inode->i_lock);
  1347. return rc;
  1348. }
  1349. static int
  1350. SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
  1351. u64 persistent_fid, u64 volatile_fid,
  1352. struct copychunk_ioctl *pcchunk)
  1353. {
  1354. int rc;
  1355. unsigned int ret_data_len;
  1356. struct resume_key_req *res_key;
  1357. rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
  1358. FSCTL_SRV_REQUEST_RESUME_KEY, NULL, 0 /* no input */,
  1359. CIFSMaxBufSize, (char **)&res_key, &ret_data_len);
  1360. if (rc == -EOPNOTSUPP) {
  1361. pr_warn_once("Server share %s does not support copy range\n", tcon->tree_name);
  1362. goto req_res_key_exit;
  1363. } else if (rc) {
  1364. cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
  1365. goto req_res_key_exit;
  1366. }
  1367. if (ret_data_len < sizeof(struct resume_key_req)) {
  1368. cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
  1369. rc = -EINVAL;
  1370. goto req_res_key_exit;
  1371. }
  1372. memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
  1373. req_res_key_exit:
  1374. kfree(res_key);
  1375. return rc;
  1376. }
  1377. static int
  1378. smb2_ioctl_query_info(const unsigned int xid,
  1379. struct cifs_tcon *tcon,
  1380. struct cifs_sb_info *cifs_sb,
  1381. __le16 *path, int is_dir,
  1382. unsigned long p)
  1383. {
  1384. struct smb2_compound_vars *vars;
  1385. struct smb_rqst *rqst;
  1386. struct kvec *rsp_iov;
  1387. struct cifs_ses *ses = tcon->ses;
  1388. struct TCP_Server_Info *server;
  1389. char __user *arg = (char __user *)p;
  1390. struct smb_query_info qi;
  1391. struct smb_query_info __user *pqi;
  1392. int rc = 0;
  1393. int flags = CIFS_CP_CREATE_CLOSE_OP;
  1394. struct smb2_query_info_rsp *qi_rsp = NULL;
  1395. struct smb2_ioctl_rsp *io_rsp = NULL;
  1396. void *buffer = NULL;
  1397. int resp_buftype[3];
  1398. struct cifs_open_parms oparms;
  1399. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  1400. struct cifs_fid fid;
  1401. unsigned int size[2];
  1402. void *data[2];
  1403. int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
  1404. void (*free_req1_func)(struct smb_rqst *r);
  1405. int retries = 0, cur_sleep = 1;
  1406. replay_again:
  1407. /* reinitialize for possible replay */
  1408. flags = CIFS_CP_CREATE_CLOSE_OP;
  1409. oplock = SMB2_OPLOCK_LEVEL_NONE;
  1410. server = cifs_pick_channel(ses);
  1411. vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
  1412. if (vars == NULL)
  1413. return -ENOMEM;
  1414. rqst = &vars->rqst[0];
  1415. rsp_iov = &vars->rsp_iov[0];
  1416. resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
  1417. if (copy_from_user(&qi, arg, sizeof(struct smb_query_info))) {
  1418. rc = -EFAULT;
  1419. goto free_vars;
  1420. }
  1421. if (qi.output_buffer_length > 1024) {
  1422. rc = -EINVAL;
  1423. goto free_vars;
  1424. }
  1425. if (!ses || !server) {
  1426. rc = -EIO;
  1427. goto free_vars;
  1428. }
  1429. if (smb3_encryption_required(tcon))
  1430. flags |= CIFS_TRANSFORM_REQ;
  1431. if (qi.output_buffer_length) {
  1432. buffer = memdup_user(arg + sizeof(struct smb_query_info), qi.output_buffer_length);
  1433. if (IS_ERR(buffer)) {
  1434. rc = PTR_ERR(buffer);
  1435. goto free_vars;
  1436. }
  1437. }
  1438. /* Open */
  1439. rqst[0].rq_iov = &vars->open_iov[0];
  1440. rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
  1441. oparms = (struct cifs_open_parms) {
  1442. .tcon = tcon,
  1443. .disposition = FILE_OPEN,
  1444. .create_options = cifs_create_options(cifs_sb, create_options),
  1445. .fid = &fid,
  1446. .replay = !!(retries),
  1447. };
  1448. if (qi.flags & PASSTHRU_FSCTL) {
  1449. switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
  1450. case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
  1451. oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
  1452. break;
  1453. case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
  1454. oparms.desired_access = GENERIC_ALL;
  1455. break;
  1456. case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
  1457. oparms.desired_access = GENERIC_READ;
  1458. break;
  1459. case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
  1460. oparms.desired_access = GENERIC_WRITE;
  1461. break;
  1462. }
  1463. } else if (qi.flags & PASSTHRU_SET_INFO) {
  1464. oparms.desired_access = GENERIC_WRITE;
  1465. } else {
  1466. oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
  1467. }
  1468. rc = SMB2_open_init(tcon, server,
  1469. &rqst[0], &oplock, &oparms, path);
  1470. if (rc)
  1471. goto free_output_buffer;
  1472. smb2_set_next_command(tcon, &rqst[0]);
  1473. /* Query */
  1474. if (qi.flags & PASSTHRU_FSCTL) {
  1475. /* Can eventually relax perm check since server enforces too */
  1476. if (!capable(CAP_SYS_ADMIN)) {
  1477. rc = -EPERM;
  1478. goto free_open_req;
  1479. }
  1480. rqst[1].rq_iov = &vars->io_iov[0];
  1481. rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
  1482. rc = SMB2_ioctl_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
  1483. qi.info_type, buffer, qi.output_buffer_length,
  1484. CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
  1485. MAX_SMB2_CLOSE_RESPONSE_SIZE);
  1486. free_req1_func = SMB2_ioctl_free;
  1487. } else if (qi.flags == PASSTHRU_SET_INFO) {
  1488. /* Can eventually relax perm check since server enforces too */
  1489. if (!capable(CAP_SYS_ADMIN)) {
  1490. rc = -EPERM;
  1491. goto free_open_req;
  1492. }
  1493. if (qi.output_buffer_length < 8) {
  1494. rc = -EINVAL;
  1495. goto free_open_req;
  1496. }
  1497. rqst[1].rq_iov = vars->si_iov;
  1498. rqst[1].rq_nvec = 1;
  1499. /* MS-FSCC 2.4.13 FileEndOfFileInformation */
  1500. size[0] = 8;
  1501. data[0] = buffer;
  1502. rc = SMB2_set_info_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
  1503. current->tgid, FILE_END_OF_FILE_INFORMATION,
  1504. SMB2_O_INFO_FILE, 0, data, size);
  1505. free_req1_func = SMB2_set_info_free;
  1506. } else if (qi.flags == PASSTHRU_QUERY_INFO) {
  1507. rqst[1].rq_iov = &vars->qi_iov;
  1508. rqst[1].rq_nvec = 1;
  1509. rc = SMB2_query_info_init(tcon, server,
  1510. &rqst[1], COMPOUND_FID,
  1511. COMPOUND_FID, qi.file_info_class,
  1512. qi.info_type, qi.additional_information,
  1513. qi.input_buffer_length,
  1514. qi.output_buffer_length, buffer);
  1515. free_req1_func = SMB2_query_info_free;
  1516. } else { /* unknown flags */
  1517. cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
  1518. qi.flags);
  1519. rc = -EINVAL;
  1520. }
  1521. if (rc)
  1522. goto free_open_req;
  1523. smb2_set_next_command(tcon, &rqst[1]);
  1524. smb2_set_related(&rqst[1]);
  1525. /* Close */
  1526. rqst[2].rq_iov = &vars->close_iov;
  1527. rqst[2].rq_nvec = 1;
  1528. rc = SMB2_close_init(tcon, server,
  1529. &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
  1530. if (rc)
  1531. goto free_req_1;
  1532. smb2_set_related(&rqst[2]);
  1533. if (retries) {
  1534. smb2_set_replay(server, &rqst[0]);
  1535. smb2_set_replay(server, &rqst[1]);
  1536. smb2_set_replay(server, &rqst[2]);
  1537. }
  1538. rc = compound_send_recv(xid, ses, server,
  1539. flags, 3, rqst,
  1540. resp_buftype, rsp_iov);
  1541. if (rc)
  1542. goto out;
  1543. /* No need to bump num_remote_opens since handle immediately closed */
  1544. if (qi.flags & PASSTHRU_FSCTL) {
  1545. pqi = (struct smb_query_info __user *)arg;
  1546. io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
  1547. if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
  1548. qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
  1549. if (qi.input_buffer_length > 0 &&
  1550. le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
  1551. > rsp_iov[1].iov_len) {
  1552. rc = -EFAULT;
  1553. goto out;
  1554. }
  1555. if (copy_to_user(&pqi->input_buffer_length,
  1556. &qi.input_buffer_length,
  1557. sizeof(qi.input_buffer_length))) {
  1558. rc = -EFAULT;
  1559. goto out;
  1560. }
  1561. if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
  1562. (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
  1563. qi.input_buffer_length))
  1564. rc = -EFAULT;
  1565. } else {
  1566. pqi = (struct smb_query_info __user *)arg;
  1567. qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
  1568. if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
  1569. qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
  1570. if (copy_to_user(&pqi->input_buffer_length,
  1571. &qi.input_buffer_length,
  1572. sizeof(qi.input_buffer_length))) {
  1573. rc = -EFAULT;
  1574. goto out;
  1575. }
  1576. if (copy_to_user(pqi + 1, qi_rsp->Buffer,
  1577. qi.input_buffer_length))
  1578. rc = -EFAULT;
  1579. }
  1580. out:
  1581. free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
  1582. free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
  1583. free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
  1584. SMB2_close_free(&rqst[2]);
  1585. free_req_1:
  1586. free_req1_func(&rqst[1]);
  1587. free_open_req:
  1588. SMB2_open_free(&rqst[0]);
  1589. free_output_buffer:
  1590. kfree(buffer);
  1591. free_vars:
  1592. kfree(vars);
  1593. if (is_replayable_error(rc) &&
  1594. smb2_should_replay(tcon, &retries, &cur_sleep))
  1595. goto replay_again;
  1596. return rc;
  1597. }
  1598. static ssize_t
  1599. smb2_copychunk_range(const unsigned int xid,
  1600. struct cifsFileInfo *srcfile,
  1601. struct cifsFileInfo *trgtfile, u64 src_off,
  1602. u64 len, u64 dest_off)
  1603. {
  1604. int rc;
  1605. unsigned int ret_data_len;
  1606. struct copychunk_ioctl *pcchunk;
  1607. struct copychunk_ioctl_rsp *retbuf = NULL;
  1608. struct cifs_tcon *tcon;
  1609. int chunks_copied = 0;
  1610. bool chunk_sizes_updated = false;
  1611. ssize_t bytes_written, total_bytes_written = 0;
  1612. pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
  1613. if (pcchunk == NULL)
  1614. return -ENOMEM;
  1615. cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
  1616. /* Request a key from the server to identify the source of the copy */
  1617. rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
  1618. srcfile->fid.persistent_fid,
  1619. srcfile->fid.volatile_fid, pcchunk);
  1620. /* Note: request_res_key sets res_key null only if rc !=0 */
  1621. if (rc)
  1622. goto cchunk_out;
  1623. /* For now array only one chunk long, will make more flexible later */
  1624. pcchunk->ChunkCount = cpu_to_le32(1);
  1625. pcchunk->Reserved = 0;
  1626. pcchunk->Reserved2 = 0;
  1627. tcon = tlink_tcon(trgtfile->tlink);
  1628. trace_smb3_copychunk_enter(xid, srcfile->fid.volatile_fid,
  1629. trgtfile->fid.volatile_fid, tcon->tid,
  1630. tcon->ses->Suid, src_off, dest_off, len);
  1631. while (len > 0) {
  1632. pcchunk->SourceOffset = cpu_to_le64(src_off);
  1633. pcchunk->TargetOffset = cpu_to_le64(dest_off);
  1634. pcchunk->Length =
  1635. cpu_to_le32(min_t(u64, len, tcon->max_bytes_chunk));
  1636. /* Request server copy to target from src identified by key */
  1637. kfree(retbuf);
  1638. retbuf = NULL;
  1639. rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
  1640. trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
  1641. (char *)pcchunk, sizeof(struct copychunk_ioctl),
  1642. CIFSMaxBufSize, (char **)&retbuf, &ret_data_len);
  1643. if (rc == 0) {
  1644. if (ret_data_len !=
  1645. sizeof(struct copychunk_ioctl_rsp)) {
  1646. cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
  1647. rc = -EIO;
  1648. goto cchunk_out;
  1649. }
  1650. if (retbuf->TotalBytesWritten == 0) {
  1651. cifs_dbg(FYI, "no bytes copied\n");
  1652. rc = -EIO;
  1653. goto cchunk_out;
  1654. }
  1655. /*
  1656. * Check if server claimed to write more than we asked
  1657. */
  1658. if (le32_to_cpu(retbuf->TotalBytesWritten) >
  1659. le32_to_cpu(pcchunk->Length)) {
  1660. cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
  1661. rc = -EIO;
  1662. goto cchunk_out;
  1663. }
  1664. if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
  1665. cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
  1666. rc = -EIO;
  1667. goto cchunk_out;
  1668. }
  1669. chunks_copied++;
  1670. bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
  1671. src_off += bytes_written;
  1672. dest_off += bytes_written;
  1673. len -= bytes_written;
  1674. total_bytes_written += bytes_written;
  1675. cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
  1676. le32_to_cpu(retbuf->ChunksWritten),
  1677. le32_to_cpu(retbuf->ChunkBytesWritten),
  1678. bytes_written);
  1679. trace_smb3_copychunk_done(xid, srcfile->fid.volatile_fid,
  1680. trgtfile->fid.volatile_fid, tcon->tid,
  1681. tcon->ses->Suid, src_off, dest_off, len);
  1682. } else if (rc == -EINVAL) {
  1683. if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
  1684. goto cchunk_out;
  1685. cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
  1686. le32_to_cpu(retbuf->ChunksWritten),
  1687. le32_to_cpu(retbuf->ChunkBytesWritten),
  1688. le32_to_cpu(retbuf->TotalBytesWritten));
  1689. /*
  1690. * Check if this is the first request using these sizes,
  1691. * (ie check if copy succeed once with original sizes
  1692. * and check if the server gave us different sizes after
  1693. * we already updated max sizes on previous request).
  1694. * if not then why is the server returning an error now
  1695. */
  1696. if ((chunks_copied != 0) || chunk_sizes_updated)
  1697. goto cchunk_out;
  1698. /* Check that server is not asking us to grow size */
  1699. if (le32_to_cpu(retbuf->ChunkBytesWritten) <
  1700. tcon->max_bytes_chunk)
  1701. tcon->max_bytes_chunk =
  1702. le32_to_cpu(retbuf->ChunkBytesWritten);
  1703. else
  1704. goto cchunk_out; /* server gave us bogus size */
  1705. /* No need to change MaxChunks since already set to 1 */
  1706. chunk_sizes_updated = true;
  1707. } else
  1708. goto cchunk_out;
  1709. }
  1710. cchunk_out:
  1711. kfree(pcchunk);
  1712. kfree(retbuf);
  1713. if (rc)
  1714. return rc;
  1715. else
  1716. return total_bytes_written;
  1717. }
  1718. static int
  1719. smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
  1720. struct cifs_fid *fid)
  1721. {
  1722. return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  1723. }
  1724. static unsigned int
  1725. smb2_read_data_offset(char *buf)
  1726. {
  1727. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  1728. return rsp->DataOffset;
  1729. }
  1730. static unsigned int
  1731. smb2_read_data_length(char *buf, bool in_remaining)
  1732. {
  1733. struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
  1734. if (in_remaining)
  1735. return le32_to_cpu(rsp->DataRemaining);
  1736. return le32_to_cpu(rsp->DataLength);
  1737. }
  1738. static int
  1739. smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
  1740. struct cifs_io_parms *parms, unsigned int *bytes_read,
  1741. char **buf, int *buf_type)
  1742. {
  1743. parms->persistent_fid = pfid->persistent_fid;
  1744. parms->volatile_fid = pfid->volatile_fid;
  1745. return SMB2_read(xid, parms, bytes_read, buf, buf_type);
  1746. }
  1747. static int
  1748. smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
  1749. struct cifs_io_parms *parms, unsigned int *written,
  1750. struct kvec *iov, unsigned long nr_segs)
  1751. {
  1752. parms->persistent_fid = pfid->persistent_fid;
  1753. parms->volatile_fid = pfid->volatile_fid;
  1754. return SMB2_write(xid, parms, written, iov, nr_segs);
  1755. }
  1756. /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
  1757. static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
  1758. struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
  1759. {
  1760. struct cifsInodeInfo *cifsi;
  1761. int rc;
  1762. cifsi = CIFS_I(inode);
  1763. /* if file already sparse don't bother setting sparse again */
  1764. if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
  1765. return true; /* already sparse */
  1766. if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
  1767. return true; /* already not sparse */
  1768. /*
  1769. * Can't check for sparse support on share the usual way via the
  1770. * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
  1771. * since Samba server doesn't set the flag on the share, yet
  1772. * supports the set sparse FSCTL and returns sparse correctly
  1773. * in the file attributes. If we fail setting sparse though we
  1774. * mark that server does not support sparse files for this share
  1775. * to avoid repeatedly sending the unsupported fsctl to server
  1776. * if the file is repeatedly extended.
  1777. */
  1778. if (tcon->broken_sparse_sup)
  1779. return false;
  1780. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  1781. cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
  1782. &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
  1783. if (rc) {
  1784. tcon->broken_sparse_sup = true;
  1785. cifs_dbg(FYI, "set sparse rc = %d\n", rc);
  1786. return false;
  1787. }
  1788. if (setsparse)
  1789. cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
  1790. else
  1791. cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
  1792. return true;
  1793. }
  1794. static int
  1795. smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
  1796. struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
  1797. {
  1798. struct inode *inode;
  1799. /*
  1800. * If extending file more than one page make sparse. Many Linux fs
  1801. * make files sparse by default when extending via ftruncate
  1802. */
  1803. inode = d_inode(cfile->dentry);
  1804. if (!set_alloc && (size > inode->i_size + 8192)) {
  1805. __u8 set_sparse = 1;
  1806. /* whether set sparse succeeds or not, extend the file */
  1807. smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
  1808. }
  1809. return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  1810. cfile->fid.volatile_fid, cfile->pid, size);
  1811. }
  1812. static int
  1813. smb2_duplicate_extents(const unsigned int xid,
  1814. struct cifsFileInfo *srcfile,
  1815. struct cifsFileInfo *trgtfile, u64 src_off,
  1816. u64 len, u64 dest_off)
  1817. {
  1818. int rc;
  1819. unsigned int ret_data_len;
  1820. struct inode *inode;
  1821. struct duplicate_extents_to_file dup_ext_buf;
  1822. struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
  1823. /* server fileays advertise duplicate extent support with this flag */
  1824. if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
  1825. FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
  1826. return -EOPNOTSUPP;
  1827. dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
  1828. dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
  1829. dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
  1830. dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
  1831. dup_ext_buf.ByteCount = cpu_to_le64(len);
  1832. cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
  1833. src_off, dest_off, len);
  1834. trace_smb3_clone_enter(xid, srcfile->fid.volatile_fid,
  1835. trgtfile->fid.volatile_fid, tcon->tid,
  1836. tcon->ses->Suid, src_off, dest_off, len);
  1837. inode = d_inode(trgtfile->dentry);
  1838. if (inode->i_size < dest_off + len) {
  1839. rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
  1840. if (rc)
  1841. goto duplicate_extents_out;
  1842. /*
  1843. * Although also could set plausible allocation size (i_blocks)
  1844. * here in addition to setting the file size, in reflink
  1845. * it is likely that the target file is sparse. Its allocation
  1846. * size will be queried on next revalidate, but it is important
  1847. * to make sure that file's cached size is updated immediately
  1848. */
  1849. netfs_resize_file(netfs_inode(inode), dest_off + len, true);
  1850. cifs_setsize(inode, dest_off + len);
  1851. }
  1852. rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
  1853. trgtfile->fid.volatile_fid,
  1854. FSCTL_DUPLICATE_EXTENTS_TO_FILE,
  1855. (char *)&dup_ext_buf,
  1856. sizeof(struct duplicate_extents_to_file),
  1857. CIFSMaxBufSize, NULL,
  1858. &ret_data_len);
  1859. if (ret_data_len > 0)
  1860. cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
  1861. duplicate_extents_out:
  1862. if (rc)
  1863. trace_smb3_clone_err(xid, srcfile->fid.volatile_fid,
  1864. trgtfile->fid.volatile_fid,
  1865. tcon->tid, tcon->ses->Suid, src_off,
  1866. dest_off, len, rc);
  1867. else
  1868. trace_smb3_clone_done(xid, srcfile->fid.volatile_fid,
  1869. trgtfile->fid.volatile_fid, tcon->tid,
  1870. tcon->ses->Suid, src_off, dest_off, len);
  1871. return rc;
  1872. }
  1873. static int
  1874. smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
  1875. struct cifsFileInfo *cfile)
  1876. {
  1877. return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
  1878. cfile->fid.volatile_fid);
  1879. }
  1880. static int
  1881. smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
  1882. struct cifsFileInfo *cfile)
  1883. {
  1884. struct fsctl_set_integrity_information_req integr_info;
  1885. unsigned int ret_data_len;
  1886. integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
  1887. integr_info.Flags = 0;
  1888. integr_info.Reserved = 0;
  1889. return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  1890. cfile->fid.volatile_fid,
  1891. FSCTL_SET_INTEGRITY_INFORMATION,
  1892. (char *)&integr_info,
  1893. sizeof(struct fsctl_set_integrity_information_req),
  1894. CIFSMaxBufSize, NULL,
  1895. &ret_data_len);
  1896. }
  1897. /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
  1898. #define GMT_TOKEN_SIZE 50
  1899. #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
  1900. /*
  1901. * Input buffer contains (empty) struct smb_snapshot array with size filled in
  1902. * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
  1903. */
  1904. static int
  1905. smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
  1906. struct cifsFileInfo *cfile, void __user *ioc_buf)
  1907. {
  1908. char *retbuf = NULL;
  1909. unsigned int ret_data_len = 0;
  1910. int rc;
  1911. u32 max_response_size;
  1912. struct smb_snapshot_array snapshot_in;
  1913. /*
  1914. * On the first query to enumerate the list of snapshots available
  1915. * for this volume the buffer begins with 0 (number of snapshots
  1916. * which can be returned is zero since at that point we do not know
  1917. * how big the buffer needs to be). On the second query,
  1918. * it (ret_data_len) is set to number of snapshots so we can
  1919. * know to set the maximum response size larger (see below).
  1920. */
  1921. if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
  1922. return -EFAULT;
  1923. /*
  1924. * Note that for snapshot queries that servers like Azure expect that
  1925. * the first query be minimal size (and just used to get the number/size
  1926. * of previous versions) so response size must be specified as EXACTLY
  1927. * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
  1928. * of eight bytes.
  1929. */
  1930. if (ret_data_len == 0)
  1931. max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
  1932. else
  1933. max_response_size = CIFSMaxBufSize;
  1934. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  1935. cfile->fid.volatile_fid,
  1936. FSCTL_SRV_ENUMERATE_SNAPSHOTS,
  1937. NULL, 0 /* no input data */, max_response_size,
  1938. (char **)&retbuf,
  1939. &ret_data_len);
  1940. cifs_dbg(FYI, "enum snapshots ioctl returned %d and ret buflen is %d\n",
  1941. rc, ret_data_len);
  1942. if (rc)
  1943. return rc;
  1944. if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
  1945. /* Fixup buffer */
  1946. if (copy_from_user(&snapshot_in, ioc_buf,
  1947. sizeof(struct smb_snapshot_array))) {
  1948. rc = -EFAULT;
  1949. kfree(retbuf);
  1950. return rc;
  1951. }
  1952. /*
  1953. * Check for min size, ie not large enough to fit even one GMT
  1954. * token (snapshot). On the first ioctl some users may pass in
  1955. * smaller size (or zero) to simply get the size of the array
  1956. * so the user space caller can allocate sufficient memory
  1957. * and retry the ioctl again with larger array size sufficient
  1958. * to hold all of the snapshot GMT tokens on the second try.
  1959. */
  1960. if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
  1961. ret_data_len = sizeof(struct smb_snapshot_array);
  1962. /*
  1963. * We return struct SRV_SNAPSHOT_ARRAY, followed by
  1964. * the snapshot array (of 50 byte GMT tokens) each
  1965. * representing an available previous version of the data
  1966. */
  1967. if (ret_data_len > (snapshot_in.snapshot_array_size +
  1968. sizeof(struct smb_snapshot_array)))
  1969. ret_data_len = snapshot_in.snapshot_array_size +
  1970. sizeof(struct smb_snapshot_array);
  1971. if (copy_to_user(ioc_buf, retbuf, ret_data_len))
  1972. rc = -EFAULT;
  1973. }
  1974. kfree(retbuf);
  1975. return rc;
  1976. }
  1977. static int
  1978. smb3_notify(const unsigned int xid, struct file *pfile,
  1979. void __user *ioc_buf, bool return_changes)
  1980. {
  1981. struct smb3_notify_info notify;
  1982. struct smb3_notify_info __user *pnotify_buf;
  1983. struct dentry *dentry = pfile->f_path.dentry;
  1984. struct inode *inode = file_inode(pfile);
  1985. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1986. struct cifs_open_parms oparms;
  1987. struct cifs_fid fid;
  1988. struct cifs_tcon *tcon;
  1989. const unsigned char *path;
  1990. char *returned_ioctl_info = NULL;
  1991. void *page = alloc_dentry_path();
  1992. __le16 *utf16_path = NULL;
  1993. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  1994. int rc = 0;
  1995. __u32 ret_len = 0;
  1996. path = build_path_from_dentry(dentry, page);
  1997. if (IS_ERR(path)) {
  1998. rc = PTR_ERR(path);
  1999. goto notify_exit;
  2000. }
  2001. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  2002. if (utf16_path == NULL) {
  2003. rc = -ENOMEM;
  2004. goto notify_exit;
  2005. }
  2006. if (return_changes) {
  2007. if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify_info))) {
  2008. rc = -EFAULT;
  2009. goto notify_exit;
  2010. }
  2011. } else {
  2012. if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
  2013. rc = -EFAULT;
  2014. goto notify_exit;
  2015. }
  2016. notify.data_len = 0;
  2017. }
  2018. tcon = cifs_sb_master_tcon(cifs_sb);
  2019. oparms = (struct cifs_open_parms) {
  2020. .tcon = tcon,
  2021. .path = path,
  2022. .desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA,
  2023. .disposition = FILE_OPEN,
  2024. .create_options = cifs_create_options(cifs_sb, 0),
  2025. .fid = &fid,
  2026. };
  2027. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
  2028. NULL);
  2029. if (rc)
  2030. goto notify_exit;
  2031. rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
  2032. notify.watch_tree, notify.completion_filter,
  2033. notify.data_len, &returned_ioctl_info, &ret_len);
  2034. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  2035. cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
  2036. if (return_changes && (ret_len > 0) && (notify.data_len > 0)) {
  2037. if (ret_len > notify.data_len)
  2038. ret_len = notify.data_len;
  2039. pnotify_buf = (struct smb3_notify_info __user *)ioc_buf;
  2040. if (copy_to_user(pnotify_buf->notify_data, returned_ioctl_info, ret_len))
  2041. rc = -EFAULT;
  2042. else if (copy_to_user(&pnotify_buf->data_len, &ret_len, sizeof(ret_len)))
  2043. rc = -EFAULT;
  2044. }
  2045. kfree(returned_ioctl_info);
  2046. notify_exit:
  2047. free_dentry_path(page);
  2048. kfree(utf16_path);
  2049. return rc;
  2050. }
  2051. static int
  2052. smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
  2053. const char *path, struct cifs_sb_info *cifs_sb,
  2054. struct cifs_fid *fid, __u16 search_flags,
  2055. struct cifs_search_info *srch_inf)
  2056. {
  2057. __le16 *utf16_path;
  2058. struct smb_rqst rqst[2];
  2059. struct kvec rsp_iov[2];
  2060. int resp_buftype[2];
  2061. struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
  2062. struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
  2063. int rc, flags = 0;
  2064. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  2065. struct cifs_open_parms oparms;
  2066. struct smb2_query_directory_rsp *qd_rsp = NULL;
  2067. struct smb2_create_rsp *op_rsp = NULL;
  2068. struct TCP_Server_Info *server;
  2069. int retries = 0, cur_sleep = 1;
  2070. replay_again:
  2071. /* reinitialize for possible replay */
  2072. flags = 0;
  2073. oplock = SMB2_OPLOCK_LEVEL_NONE;
  2074. server = cifs_pick_channel(tcon->ses);
  2075. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  2076. if (!utf16_path)
  2077. return -ENOMEM;
  2078. if (smb3_encryption_required(tcon))
  2079. flags |= CIFS_TRANSFORM_REQ;
  2080. memset(rqst, 0, sizeof(rqst));
  2081. resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
  2082. memset(rsp_iov, 0, sizeof(rsp_iov));
  2083. /* Open */
  2084. memset(&open_iov, 0, sizeof(open_iov));
  2085. rqst[0].rq_iov = open_iov;
  2086. rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
  2087. oparms = (struct cifs_open_parms) {
  2088. .tcon = tcon,
  2089. .path = path,
  2090. .desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA,
  2091. .disposition = FILE_OPEN,
  2092. .create_options = cifs_create_options(cifs_sb, 0),
  2093. .fid = fid,
  2094. .replay = !!(retries),
  2095. };
  2096. rc = SMB2_open_init(tcon, server,
  2097. &rqst[0], &oplock, &oparms, utf16_path);
  2098. if (rc)
  2099. goto qdf_free;
  2100. smb2_set_next_command(tcon, &rqst[0]);
  2101. /* Query directory */
  2102. srch_inf->entries_in_buffer = 0;
  2103. srch_inf->index_of_last_entry = 2;
  2104. memset(&qd_iov, 0, sizeof(qd_iov));
  2105. rqst[1].rq_iov = qd_iov;
  2106. rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
  2107. rc = SMB2_query_directory_init(xid, tcon, server,
  2108. &rqst[1],
  2109. COMPOUND_FID, COMPOUND_FID,
  2110. 0, srch_inf->info_level);
  2111. if (rc)
  2112. goto qdf_free;
  2113. smb2_set_related(&rqst[1]);
  2114. if (retries) {
  2115. smb2_set_replay(server, &rqst[0]);
  2116. smb2_set_replay(server, &rqst[1]);
  2117. }
  2118. rc = compound_send_recv(xid, tcon->ses, server,
  2119. flags, 2, rqst,
  2120. resp_buftype, rsp_iov);
  2121. /* If the open failed there is nothing to do */
  2122. op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
  2123. if (op_rsp == NULL || op_rsp->hdr.Status != STATUS_SUCCESS) {
  2124. cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
  2125. goto qdf_free;
  2126. }
  2127. fid->persistent_fid = op_rsp->PersistentFileId;
  2128. fid->volatile_fid = op_rsp->VolatileFileId;
  2129. /* Anything else than ENODATA means a genuine error */
  2130. if (rc && rc != -ENODATA) {
  2131. SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  2132. cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
  2133. trace_smb3_query_dir_err(xid, fid->persistent_fid,
  2134. tcon->tid, tcon->ses->Suid, 0, 0, rc);
  2135. goto qdf_free;
  2136. }
  2137. atomic_inc(&tcon->num_remote_opens);
  2138. qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
  2139. if (qd_rsp->hdr.Status == STATUS_NO_MORE_FILES) {
  2140. trace_smb3_query_dir_done(xid, fid->persistent_fid,
  2141. tcon->tid, tcon->ses->Suid, 0, 0);
  2142. srch_inf->endOfSearch = true;
  2143. rc = 0;
  2144. goto qdf_free;
  2145. }
  2146. rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
  2147. srch_inf);
  2148. if (rc) {
  2149. trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
  2150. tcon->ses->Suid, 0, 0, rc);
  2151. goto qdf_free;
  2152. }
  2153. resp_buftype[1] = CIFS_NO_BUFFER;
  2154. trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
  2155. tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
  2156. qdf_free:
  2157. kfree(utf16_path);
  2158. SMB2_open_free(&rqst[0]);
  2159. SMB2_query_directory_free(&rqst[1]);
  2160. free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
  2161. free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
  2162. if (is_replayable_error(rc) &&
  2163. smb2_should_replay(tcon, &retries, &cur_sleep))
  2164. goto replay_again;
  2165. return rc;
  2166. }
  2167. static int
  2168. smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
  2169. struct cifs_fid *fid, __u16 search_flags,
  2170. struct cifs_search_info *srch_inf)
  2171. {
  2172. return SMB2_query_directory(xid, tcon, fid->persistent_fid,
  2173. fid->volatile_fid, 0, srch_inf);
  2174. }
  2175. static int
  2176. smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
  2177. struct cifs_fid *fid)
  2178. {
  2179. return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
  2180. }
  2181. /*
  2182. * If we negotiate SMB2 protocol and get STATUS_PENDING - update
  2183. * the number of credits and return true. Otherwise - return false.
  2184. */
  2185. static bool
  2186. smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
  2187. {
  2188. struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
  2189. int scredits, in_flight;
  2190. if (shdr->Status != STATUS_PENDING)
  2191. return false;
  2192. if (shdr->CreditRequest) {
  2193. spin_lock(&server->req_lock);
  2194. server->credits += le16_to_cpu(shdr->CreditRequest);
  2195. scredits = server->credits;
  2196. in_flight = server->in_flight;
  2197. spin_unlock(&server->req_lock);
  2198. wake_up(&server->request_q);
  2199. trace_smb3_pend_credits(server->CurrentMid,
  2200. server->conn_id, server->hostname, scredits,
  2201. le16_to_cpu(shdr->CreditRequest), in_flight);
  2202. cifs_dbg(FYI, "%s: status pending add %u credits total=%d\n",
  2203. __func__, le16_to_cpu(shdr->CreditRequest), scredits);
  2204. }
  2205. return true;
  2206. }
  2207. static bool
  2208. smb2_is_session_expired(char *buf)
  2209. {
  2210. struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
  2211. if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
  2212. shdr->Status != STATUS_USER_SESSION_DELETED)
  2213. return false;
  2214. trace_smb3_ses_expired(le32_to_cpu(shdr->Id.SyncId.TreeId),
  2215. le64_to_cpu(shdr->SessionId),
  2216. le16_to_cpu(shdr->Command),
  2217. le64_to_cpu(shdr->MessageId));
  2218. cifs_dbg(FYI, "Session expired or deleted\n");
  2219. return true;
  2220. }
  2221. static bool
  2222. smb2_is_status_io_timeout(char *buf)
  2223. {
  2224. struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
  2225. if (shdr->Status == STATUS_IO_TIMEOUT)
  2226. return true;
  2227. else
  2228. return false;
  2229. }
  2230. static bool
  2231. smb2_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
  2232. {
  2233. struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
  2234. struct TCP_Server_Info *pserver;
  2235. struct cifs_ses *ses;
  2236. struct cifs_tcon *tcon;
  2237. if (shdr->Status != STATUS_NETWORK_NAME_DELETED)
  2238. return false;
  2239. /* If server is a channel, select the primary channel */
  2240. pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
  2241. spin_lock(&cifs_tcp_ses_lock);
  2242. list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
  2243. if (cifs_ses_exiting(ses))
  2244. continue;
  2245. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  2246. if (tcon->tid == le32_to_cpu(shdr->Id.SyncId.TreeId)) {
  2247. spin_lock(&tcon->tc_lock);
  2248. tcon->need_reconnect = true;
  2249. spin_unlock(&tcon->tc_lock);
  2250. spin_unlock(&cifs_tcp_ses_lock);
  2251. pr_warn_once("Server share %s deleted.\n",
  2252. tcon->tree_name);
  2253. return true;
  2254. }
  2255. }
  2256. }
  2257. spin_unlock(&cifs_tcp_ses_lock);
  2258. return false;
  2259. }
  2260. static int
  2261. smb2_oplock_response(struct cifs_tcon *tcon, __u64 persistent_fid,
  2262. __u64 volatile_fid, __u16 net_fid, struct cifsInodeInfo *cinode)
  2263. {
  2264. if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
  2265. return SMB2_lease_break(0, tcon, cinode->lease_key,
  2266. smb2_get_lease_state(cinode));
  2267. return SMB2_oplock_break(0, tcon, persistent_fid, volatile_fid,
  2268. CIFS_CACHE_READ(cinode) ? 1 : 0);
  2269. }
  2270. void
  2271. smb2_set_replay(struct TCP_Server_Info *server, struct smb_rqst *rqst)
  2272. {
  2273. struct smb2_hdr *shdr;
  2274. if (server->dialect < SMB30_PROT_ID)
  2275. return;
  2276. shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
  2277. if (shdr == NULL) {
  2278. cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
  2279. return;
  2280. }
  2281. shdr->Flags |= SMB2_FLAGS_REPLAY_OPERATION;
  2282. }
  2283. void
  2284. smb2_set_related(struct smb_rqst *rqst)
  2285. {
  2286. struct smb2_hdr *shdr;
  2287. shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
  2288. if (shdr == NULL) {
  2289. cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
  2290. return;
  2291. }
  2292. shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  2293. }
  2294. char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
  2295. void
  2296. smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
  2297. {
  2298. struct smb2_hdr *shdr;
  2299. struct cifs_ses *ses = tcon->ses;
  2300. struct TCP_Server_Info *server = ses->server;
  2301. unsigned long len = smb_rqst_len(server, rqst);
  2302. int i, num_padding;
  2303. shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
  2304. if (shdr == NULL) {
  2305. cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
  2306. return;
  2307. }
  2308. /* SMB headers in a compound are 8 byte aligned. */
  2309. /* No padding needed */
  2310. if (!(len & 7))
  2311. goto finished;
  2312. num_padding = 8 - (len & 7);
  2313. if (!smb3_encryption_required(tcon)) {
  2314. /*
  2315. * If we do not have encryption then we can just add an extra
  2316. * iov for the padding.
  2317. */
  2318. rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
  2319. rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
  2320. rqst->rq_nvec++;
  2321. len += num_padding;
  2322. } else {
  2323. /*
  2324. * We can not add a small padding iov for the encryption case
  2325. * because the encryption framework can not handle the padding
  2326. * iovs.
  2327. * We have to flatten this into a single buffer and add
  2328. * the padding to it.
  2329. */
  2330. for (i = 1; i < rqst->rq_nvec; i++) {
  2331. memcpy(rqst->rq_iov[0].iov_base +
  2332. rqst->rq_iov[0].iov_len,
  2333. rqst->rq_iov[i].iov_base,
  2334. rqst->rq_iov[i].iov_len);
  2335. rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
  2336. }
  2337. memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
  2338. 0, num_padding);
  2339. rqst->rq_iov[0].iov_len += num_padding;
  2340. len += num_padding;
  2341. rqst->rq_nvec = 1;
  2342. }
  2343. finished:
  2344. shdr->NextCommand = cpu_to_le32(len);
  2345. }
  2346. /*
  2347. * helper function for exponential backoff and check if replayable
  2348. */
  2349. bool smb2_should_replay(struct cifs_tcon *tcon,
  2350. int *pretries,
  2351. int *pcur_sleep)
  2352. {
  2353. if (!pretries || !pcur_sleep)
  2354. return false;
  2355. if (tcon->retry || (*pretries)++ < tcon->ses->server->retrans) {
  2356. msleep(*pcur_sleep);
  2357. (*pcur_sleep) = ((*pcur_sleep) << 1);
  2358. if ((*pcur_sleep) > CIFS_MAX_SLEEP)
  2359. (*pcur_sleep) = CIFS_MAX_SLEEP;
  2360. return true;
  2361. }
  2362. return false;
  2363. }
  2364. /*
  2365. * Passes the query info response back to the caller on success.
  2366. * Caller need to free this with free_rsp_buf().
  2367. */
  2368. int
  2369. smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
  2370. const char *path, u32 desired_access,
  2371. u32 class, u32 type, u32 output_len,
  2372. struct kvec *rsp, int *buftype,
  2373. struct cifs_sb_info *cifs_sb)
  2374. {
  2375. struct smb2_compound_vars *vars;
  2376. struct cifs_ses *ses = tcon->ses;
  2377. struct TCP_Server_Info *server;
  2378. int flags = CIFS_CP_CREATE_CLOSE_OP;
  2379. struct smb_rqst *rqst;
  2380. int resp_buftype[3];
  2381. struct kvec *rsp_iov;
  2382. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  2383. struct cifs_open_parms oparms;
  2384. struct cifs_fid fid;
  2385. int rc;
  2386. __le16 *utf16_path;
  2387. struct cached_fid *cfid = NULL;
  2388. int retries = 0, cur_sleep = 1;
  2389. replay_again:
  2390. /* reinitialize for possible replay */
  2391. flags = CIFS_CP_CREATE_CLOSE_OP;
  2392. oplock = SMB2_OPLOCK_LEVEL_NONE;
  2393. server = cifs_pick_channel(ses);
  2394. if (!path)
  2395. path = "";
  2396. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  2397. if (!utf16_path)
  2398. return -ENOMEM;
  2399. if (smb3_encryption_required(tcon))
  2400. flags |= CIFS_TRANSFORM_REQ;
  2401. resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
  2402. vars = kzalloc(sizeof(*vars), GFP_KERNEL);
  2403. if (!vars) {
  2404. rc = -ENOMEM;
  2405. goto out_free_path;
  2406. }
  2407. rqst = vars->rqst;
  2408. rsp_iov = vars->rsp_iov;
  2409. /*
  2410. * We can only call this for things we know are directories.
  2411. */
  2412. if (!strcmp(path, ""))
  2413. open_cached_dir(xid, tcon, path, cifs_sb, false,
  2414. &cfid); /* cfid null if open dir failed */
  2415. rqst[0].rq_iov = vars->open_iov;
  2416. rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
  2417. oparms = (struct cifs_open_parms) {
  2418. .tcon = tcon,
  2419. .path = path,
  2420. .desired_access = desired_access,
  2421. .disposition = FILE_OPEN,
  2422. .create_options = cifs_create_options(cifs_sb, 0),
  2423. .fid = &fid,
  2424. .replay = !!(retries),
  2425. };
  2426. rc = SMB2_open_init(tcon, server,
  2427. &rqst[0], &oplock, &oparms, utf16_path);
  2428. if (rc)
  2429. goto qic_exit;
  2430. smb2_set_next_command(tcon, &rqst[0]);
  2431. rqst[1].rq_iov = &vars->qi_iov;
  2432. rqst[1].rq_nvec = 1;
  2433. if (cfid) {
  2434. rc = SMB2_query_info_init(tcon, server,
  2435. &rqst[1],
  2436. cfid->fid.persistent_fid,
  2437. cfid->fid.volatile_fid,
  2438. class, type, 0,
  2439. output_len, 0,
  2440. NULL);
  2441. } else {
  2442. rc = SMB2_query_info_init(tcon, server,
  2443. &rqst[1],
  2444. COMPOUND_FID,
  2445. COMPOUND_FID,
  2446. class, type, 0,
  2447. output_len, 0,
  2448. NULL);
  2449. }
  2450. if (rc)
  2451. goto qic_exit;
  2452. if (!cfid) {
  2453. smb2_set_next_command(tcon, &rqst[1]);
  2454. smb2_set_related(&rqst[1]);
  2455. }
  2456. rqst[2].rq_iov = &vars->close_iov;
  2457. rqst[2].rq_nvec = 1;
  2458. rc = SMB2_close_init(tcon, server,
  2459. &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
  2460. if (rc)
  2461. goto qic_exit;
  2462. smb2_set_related(&rqst[2]);
  2463. if (retries) {
  2464. if (!cfid) {
  2465. smb2_set_replay(server, &rqst[0]);
  2466. smb2_set_replay(server, &rqst[2]);
  2467. }
  2468. smb2_set_replay(server, &rqst[1]);
  2469. }
  2470. if (cfid) {
  2471. rc = compound_send_recv(xid, ses, server,
  2472. flags, 1, &rqst[1],
  2473. &resp_buftype[1], &rsp_iov[1]);
  2474. } else {
  2475. rc = compound_send_recv(xid, ses, server,
  2476. flags, 3, rqst,
  2477. resp_buftype, rsp_iov);
  2478. }
  2479. if (rc) {
  2480. free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
  2481. if (rc == -EREMCHG) {
  2482. tcon->need_reconnect = true;
  2483. pr_warn_once("server share %s deleted\n",
  2484. tcon->tree_name);
  2485. }
  2486. goto qic_exit;
  2487. }
  2488. *rsp = rsp_iov[1];
  2489. *buftype = resp_buftype[1];
  2490. qic_exit:
  2491. SMB2_open_free(&rqst[0]);
  2492. SMB2_query_info_free(&rqst[1]);
  2493. SMB2_close_free(&rqst[2]);
  2494. free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
  2495. free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
  2496. if (cfid)
  2497. close_cached_dir(cfid);
  2498. kfree(vars);
  2499. out_free_path:
  2500. kfree(utf16_path);
  2501. if (is_replayable_error(rc) &&
  2502. smb2_should_replay(tcon, &retries, &cur_sleep))
  2503. goto replay_again;
  2504. return rc;
  2505. }
  2506. static int
  2507. smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
  2508. const char *path, struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
  2509. {
  2510. struct smb2_query_info_rsp *rsp;
  2511. struct smb2_fs_full_size_info *info = NULL;
  2512. struct kvec rsp_iov = {NULL, 0};
  2513. int buftype = CIFS_NO_BUFFER;
  2514. int rc;
  2515. rc = smb2_query_info_compound(xid, tcon, path,
  2516. FILE_READ_ATTRIBUTES,
  2517. FS_FULL_SIZE_INFORMATION,
  2518. SMB2_O_INFO_FILESYSTEM,
  2519. sizeof(struct smb2_fs_full_size_info),
  2520. &rsp_iov, &buftype, cifs_sb);
  2521. if (rc)
  2522. goto qfs_exit;
  2523. rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
  2524. buf->f_type = SMB2_SUPER_MAGIC;
  2525. info = (struct smb2_fs_full_size_info *)(
  2526. le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
  2527. rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
  2528. le32_to_cpu(rsp->OutputBufferLength),
  2529. &rsp_iov,
  2530. sizeof(struct smb2_fs_full_size_info));
  2531. if (!rc)
  2532. smb2_copy_fs_info_to_kstatfs(info, buf);
  2533. qfs_exit:
  2534. trace_smb3_qfs_done(xid, tcon->tid, tcon->ses->Suid, tcon->tree_name, rc);
  2535. free_rsp_buf(buftype, rsp_iov.iov_base);
  2536. return rc;
  2537. }
  2538. static int
  2539. smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
  2540. const char *path, struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
  2541. {
  2542. int rc;
  2543. __le16 *utf16_path = NULL;
  2544. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  2545. struct cifs_open_parms oparms;
  2546. struct cifs_fid fid;
  2547. if (!tcon->posix_extensions)
  2548. return smb2_queryfs(xid, tcon, path, cifs_sb, buf);
  2549. oparms = (struct cifs_open_parms) {
  2550. .tcon = tcon,
  2551. .path = path,
  2552. .desired_access = FILE_READ_ATTRIBUTES,
  2553. .disposition = FILE_OPEN,
  2554. .create_options = cifs_create_options(cifs_sb, 0),
  2555. .fid = &fid,
  2556. };
  2557. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  2558. if (utf16_path == NULL)
  2559. return -ENOMEM;
  2560. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
  2561. NULL, NULL);
  2562. kfree(utf16_path);
  2563. if (rc)
  2564. return rc;
  2565. rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
  2566. fid.volatile_fid, buf);
  2567. buf->f_type = SMB2_SUPER_MAGIC;
  2568. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  2569. return rc;
  2570. }
  2571. static bool
  2572. smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
  2573. {
  2574. return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
  2575. ob1->fid.volatile_fid == ob2->fid.volatile_fid;
  2576. }
  2577. static int
  2578. smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
  2579. __u64 length, __u32 type, int lock, int unlock, bool wait)
  2580. {
  2581. if (unlock && !lock)
  2582. type = SMB2_LOCKFLAG_UNLOCK;
  2583. return SMB2_lock(xid, tlink_tcon(cfile->tlink),
  2584. cfile->fid.persistent_fid, cfile->fid.volatile_fid,
  2585. current->tgid, length, offset, type, wait);
  2586. }
  2587. static void
  2588. smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
  2589. {
  2590. memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
  2591. }
  2592. static void
  2593. smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
  2594. {
  2595. memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
  2596. }
  2597. static void
  2598. smb2_new_lease_key(struct cifs_fid *fid)
  2599. {
  2600. generate_random_uuid(fid->lease_key);
  2601. }
  2602. static int
  2603. smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
  2604. const char *search_name,
  2605. struct dfs_info3_param **target_nodes,
  2606. unsigned int *num_of_nodes,
  2607. const struct nls_table *nls_codepage, int remap)
  2608. {
  2609. int rc;
  2610. __le16 *utf16_path = NULL;
  2611. int utf16_path_len = 0;
  2612. struct cifs_tcon *tcon;
  2613. struct fsctl_get_dfs_referral_req *dfs_req = NULL;
  2614. struct get_dfs_referral_rsp *dfs_rsp = NULL;
  2615. u32 dfs_req_size = 0, dfs_rsp_size = 0;
  2616. int retry_count = 0;
  2617. cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
  2618. /*
  2619. * Try to use the IPC tcon, otherwise just use any
  2620. */
  2621. tcon = ses->tcon_ipc;
  2622. if (tcon == NULL) {
  2623. spin_lock(&cifs_tcp_ses_lock);
  2624. tcon = list_first_entry_or_null(&ses->tcon_list,
  2625. struct cifs_tcon,
  2626. tcon_list);
  2627. if (tcon) {
  2628. tcon->tc_count++;
  2629. trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
  2630. netfs_trace_tcon_ref_get_dfs_refer);
  2631. }
  2632. spin_unlock(&cifs_tcp_ses_lock);
  2633. }
  2634. if (tcon == NULL) {
  2635. cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
  2636. ses);
  2637. rc = -ENOTCONN;
  2638. goto out;
  2639. }
  2640. utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
  2641. &utf16_path_len,
  2642. nls_codepage, remap);
  2643. if (!utf16_path) {
  2644. rc = -ENOMEM;
  2645. goto out;
  2646. }
  2647. dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
  2648. dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
  2649. if (!dfs_req) {
  2650. rc = -ENOMEM;
  2651. goto out;
  2652. }
  2653. /* Highest DFS referral version understood */
  2654. dfs_req->MaxReferralLevel = DFS_VERSION;
  2655. /* Path to resolve in an UTF-16 null-terminated string */
  2656. memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
  2657. do {
  2658. rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
  2659. FSCTL_DFS_GET_REFERRALS,
  2660. (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
  2661. (char **)&dfs_rsp, &dfs_rsp_size);
  2662. if (!is_retryable_error(rc))
  2663. break;
  2664. usleep_range(512, 2048);
  2665. } while (++retry_count < 5);
  2666. if (!rc && !dfs_rsp)
  2667. rc = -EIO;
  2668. if (rc) {
  2669. if (!is_retryable_error(rc) && rc != -ENOENT && rc != -EOPNOTSUPP)
  2670. cifs_tcon_dbg(VFS, "%s: ioctl error: rc=%d\n", __func__, rc);
  2671. goto out;
  2672. }
  2673. rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
  2674. num_of_nodes, target_nodes,
  2675. nls_codepage, remap, search_name,
  2676. true /* is_unicode */);
  2677. if (rc) {
  2678. cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
  2679. goto out;
  2680. }
  2681. out:
  2682. if (tcon && !tcon->ipc) {
  2683. /* ipc tcons are not refcounted */
  2684. spin_lock(&cifs_tcp_ses_lock);
  2685. tcon->tc_count--;
  2686. trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
  2687. netfs_trace_tcon_ref_dec_dfs_refer);
  2688. /* tc_count can never go negative */
  2689. WARN_ON(tcon->tc_count < 0);
  2690. spin_unlock(&cifs_tcp_ses_lock);
  2691. }
  2692. kfree(utf16_path);
  2693. kfree(dfs_req);
  2694. kfree(dfs_rsp);
  2695. return rc;
  2696. }
  2697. static struct smb_ntsd *
  2698. get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
  2699. const struct cifs_fid *cifsfid, u32 *pacllen, u32 info)
  2700. {
  2701. struct smb_ntsd *pntsd = NULL;
  2702. unsigned int xid;
  2703. int rc = -EOPNOTSUPP;
  2704. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  2705. if (IS_ERR(tlink))
  2706. return ERR_CAST(tlink);
  2707. xid = get_xid();
  2708. cifs_dbg(FYI, "trying to get acl\n");
  2709. rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
  2710. cifsfid->volatile_fid, (void **)&pntsd, pacllen,
  2711. info);
  2712. free_xid(xid);
  2713. cifs_put_tlink(tlink);
  2714. cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
  2715. if (rc)
  2716. return ERR_PTR(rc);
  2717. return pntsd;
  2718. }
  2719. static struct smb_ntsd *
  2720. get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
  2721. const char *path, u32 *pacllen, u32 info)
  2722. {
  2723. struct smb_ntsd *pntsd = NULL;
  2724. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  2725. unsigned int xid;
  2726. int rc;
  2727. struct cifs_tcon *tcon;
  2728. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  2729. struct cifs_fid fid;
  2730. struct cifs_open_parms oparms;
  2731. __le16 *utf16_path;
  2732. cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
  2733. if (IS_ERR(tlink))
  2734. return ERR_CAST(tlink);
  2735. tcon = tlink_tcon(tlink);
  2736. xid = get_xid();
  2737. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  2738. if (!utf16_path) {
  2739. rc = -ENOMEM;
  2740. goto put_tlink;
  2741. }
  2742. oparms = (struct cifs_open_parms) {
  2743. .tcon = tcon,
  2744. .path = path,
  2745. .desired_access = READ_CONTROL,
  2746. .disposition = FILE_OPEN,
  2747. /*
  2748. * When querying an ACL, even if the file is a symlink
  2749. * we want to open the source not the target, and so
  2750. * the protocol requires that the client specify this
  2751. * flag when opening a reparse point
  2752. */
  2753. .create_options = cifs_create_options(cifs_sb, 0) |
  2754. OPEN_REPARSE_POINT,
  2755. .fid = &fid,
  2756. };
  2757. if (info & SACL_SECINFO)
  2758. oparms.desired_access |= SYSTEM_SECURITY;
  2759. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
  2760. NULL);
  2761. kfree(utf16_path);
  2762. if (!rc) {
  2763. rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
  2764. fid.volatile_fid, (void **)&pntsd, pacllen,
  2765. info);
  2766. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  2767. }
  2768. put_tlink:
  2769. cifs_put_tlink(tlink);
  2770. free_xid(xid);
  2771. cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
  2772. if (rc)
  2773. return ERR_PTR(rc);
  2774. return pntsd;
  2775. }
  2776. static int
  2777. set_smb2_acl(struct smb_ntsd *pnntsd, __u32 acllen,
  2778. struct inode *inode, const char *path, int aclflag)
  2779. {
  2780. u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
  2781. unsigned int xid;
  2782. int rc, access_flags = 0;
  2783. struct cifs_tcon *tcon;
  2784. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2785. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  2786. struct cifs_fid fid;
  2787. struct cifs_open_parms oparms;
  2788. __le16 *utf16_path;
  2789. cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
  2790. if (IS_ERR(tlink))
  2791. return PTR_ERR(tlink);
  2792. tcon = tlink_tcon(tlink);
  2793. xid = get_xid();
  2794. if (aclflag & CIFS_ACL_OWNER || aclflag & CIFS_ACL_GROUP)
  2795. access_flags |= WRITE_OWNER;
  2796. if (aclflag & CIFS_ACL_SACL)
  2797. access_flags |= SYSTEM_SECURITY;
  2798. if (aclflag & CIFS_ACL_DACL)
  2799. access_flags |= WRITE_DAC;
  2800. utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
  2801. if (!utf16_path) {
  2802. rc = -ENOMEM;
  2803. goto put_tlink;
  2804. }
  2805. oparms = (struct cifs_open_parms) {
  2806. .tcon = tcon,
  2807. .desired_access = access_flags,
  2808. .create_options = cifs_create_options(cifs_sb, 0),
  2809. .disposition = FILE_OPEN,
  2810. .path = path,
  2811. .fid = &fid,
  2812. };
  2813. rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
  2814. NULL, NULL);
  2815. kfree(utf16_path);
  2816. if (!rc) {
  2817. rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
  2818. fid.volatile_fid, pnntsd, acllen, aclflag);
  2819. SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
  2820. }
  2821. put_tlink:
  2822. cifs_put_tlink(tlink);
  2823. free_xid(xid);
  2824. return rc;
  2825. }
  2826. /* Retrieve an ACL from the server */
  2827. static struct smb_ntsd *
  2828. get_smb2_acl(struct cifs_sb_info *cifs_sb,
  2829. struct inode *inode, const char *path,
  2830. u32 *pacllen, u32 info)
  2831. {
  2832. struct smb_ntsd *pntsd = NULL;
  2833. struct cifsFileInfo *open_file = NULL;
  2834. if (inode && !(info & SACL_SECINFO))
  2835. open_file = find_readable_file(CIFS_I(inode), true);
  2836. if (!open_file || (info & SACL_SECINFO))
  2837. return get_smb2_acl_by_path(cifs_sb, path, pacllen, info);
  2838. pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen, info);
  2839. cifsFileInfo_put(open_file);
  2840. return pntsd;
  2841. }
  2842. static long smb3_zero_data(struct file *file, struct cifs_tcon *tcon,
  2843. loff_t offset, loff_t len, unsigned int xid)
  2844. {
  2845. struct cifsFileInfo *cfile = file->private_data;
  2846. struct file_zero_data_information fsctl_buf;
  2847. cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
  2848. fsctl_buf.FileOffset = cpu_to_le64(offset);
  2849. fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
  2850. return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  2851. cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
  2852. (char *)&fsctl_buf,
  2853. sizeof(struct file_zero_data_information),
  2854. 0, NULL, NULL);
  2855. }
  2856. static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
  2857. unsigned long long offset, unsigned long long len,
  2858. bool keep_size)
  2859. {
  2860. struct cifs_ses *ses = tcon->ses;
  2861. struct inode *inode = file_inode(file);
  2862. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  2863. struct cifsFileInfo *cfile = file->private_data;
  2864. struct netfs_inode *ictx = netfs_inode(inode);
  2865. unsigned long long i_size, new_size, remote_size;
  2866. long rc;
  2867. unsigned int xid;
  2868. xid = get_xid();
  2869. trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
  2870. ses->Suid, offset, len);
  2871. inode_lock(inode);
  2872. filemap_invalidate_lock(inode->i_mapping);
  2873. i_size = i_size_read(inode);
  2874. remote_size = ictx->remote_i_size;
  2875. if (offset + len >= remote_size && offset < i_size) {
  2876. unsigned long long top = umin(offset + len, i_size);
  2877. rc = filemap_write_and_wait_range(inode->i_mapping, offset, top - 1);
  2878. if (rc < 0)
  2879. goto zero_range_exit;
  2880. }
  2881. /*
  2882. * We zero the range through ioctl, so we need remove the page caches
  2883. * first, otherwise the data may be inconsistent with the server.
  2884. */
  2885. truncate_pagecache_range(inode, offset, offset + len - 1);
  2886. /* if file not oplocked can't be sure whether asking to extend size */
  2887. rc = -EOPNOTSUPP;
  2888. if (keep_size == false && !CIFS_CACHE_READ(cifsi))
  2889. goto zero_range_exit;
  2890. rc = smb3_zero_data(file, tcon, offset, len, xid);
  2891. if (rc < 0)
  2892. goto zero_range_exit;
  2893. /*
  2894. * do we also need to change the size of the file?
  2895. */
  2896. new_size = offset + len;
  2897. if (keep_size == false && (unsigned long long)i_size_read(inode) < new_size) {
  2898. rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  2899. cfile->fid.volatile_fid, cfile->pid, new_size);
  2900. if (rc >= 0) {
  2901. truncate_setsize(inode, new_size);
  2902. netfs_resize_file(&cifsi->netfs, new_size, true);
  2903. if (offset < cifsi->netfs.zero_point)
  2904. cifsi->netfs.zero_point = offset;
  2905. fscache_resize_cookie(cifs_inode_cookie(inode), new_size);
  2906. }
  2907. }
  2908. zero_range_exit:
  2909. filemap_invalidate_unlock(inode->i_mapping);
  2910. inode_unlock(inode);
  2911. free_xid(xid);
  2912. if (rc)
  2913. trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
  2914. ses->Suid, offset, len, rc);
  2915. else
  2916. trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
  2917. ses->Suid, offset, len);
  2918. return rc;
  2919. }
  2920. static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
  2921. loff_t offset, loff_t len)
  2922. {
  2923. struct inode *inode = file_inode(file);
  2924. struct cifsFileInfo *cfile = file->private_data;
  2925. struct file_zero_data_information fsctl_buf;
  2926. unsigned long long end = offset + len, i_size, remote_i_size;
  2927. long rc;
  2928. unsigned int xid;
  2929. __u8 set_sparse = 1;
  2930. xid = get_xid();
  2931. inode_lock(inode);
  2932. /* Need to make file sparse, if not already, before freeing range. */
  2933. /* Consider adding equivalent for compressed since it could also work */
  2934. if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
  2935. rc = -EOPNOTSUPP;
  2936. goto out;
  2937. }
  2938. filemap_invalidate_lock(inode->i_mapping);
  2939. /*
  2940. * We implement the punch hole through ioctl, so we need remove the page
  2941. * caches first, otherwise the data may be inconsistent with the server.
  2942. */
  2943. truncate_pagecache_range(inode, offset, offset + len - 1);
  2944. cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
  2945. fsctl_buf.FileOffset = cpu_to_le64(offset);
  2946. fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
  2947. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  2948. cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
  2949. (char *)&fsctl_buf,
  2950. sizeof(struct file_zero_data_information),
  2951. CIFSMaxBufSize, NULL, NULL);
  2952. if (rc)
  2953. goto unlock;
  2954. /* If there's dirty data in the buffer that would extend the EOF if it
  2955. * were written, then we need to move the EOF marker over to the lower
  2956. * of the high end of the hole and the proposed EOF. The problem is
  2957. * that we locally hole-punch the tail of the dirty data, the proposed
  2958. * EOF update will end up in the wrong place.
  2959. */
  2960. i_size = i_size_read(inode);
  2961. remote_i_size = netfs_inode(inode)->remote_i_size;
  2962. if (end > remote_i_size && i_size > remote_i_size) {
  2963. unsigned long long extend_to = umin(end, i_size);
  2964. rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  2965. cfile->fid.volatile_fid, cfile->pid, extend_to);
  2966. if (rc >= 0)
  2967. netfs_inode(inode)->remote_i_size = extend_to;
  2968. }
  2969. unlock:
  2970. filemap_invalidate_unlock(inode->i_mapping);
  2971. out:
  2972. inode_unlock(inode);
  2973. free_xid(xid);
  2974. return rc;
  2975. }
  2976. static int smb3_simple_fallocate_write_range(unsigned int xid,
  2977. struct cifs_tcon *tcon,
  2978. struct cifsFileInfo *cfile,
  2979. loff_t off, loff_t len,
  2980. char *buf)
  2981. {
  2982. struct cifs_io_parms io_parms = {0};
  2983. int nbytes;
  2984. int rc = 0;
  2985. struct kvec iov[2];
  2986. io_parms.netfid = cfile->fid.netfid;
  2987. io_parms.pid = current->tgid;
  2988. io_parms.tcon = tcon;
  2989. io_parms.persistent_fid = cfile->fid.persistent_fid;
  2990. io_parms.volatile_fid = cfile->fid.volatile_fid;
  2991. while (len) {
  2992. io_parms.offset = off;
  2993. io_parms.length = len;
  2994. if (io_parms.length > SMB2_MAX_BUFFER_SIZE)
  2995. io_parms.length = SMB2_MAX_BUFFER_SIZE;
  2996. /* iov[0] is reserved for smb header */
  2997. iov[1].iov_base = buf;
  2998. iov[1].iov_len = io_parms.length;
  2999. rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1);
  3000. if (rc)
  3001. break;
  3002. if (nbytes > len)
  3003. return -EINVAL;
  3004. buf += nbytes;
  3005. off += nbytes;
  3006. len -= nbytes;
  3007. }
  3008. return rc;
  3009. }
  3010. static int smb3_simple_fallocate_range(unsigned int xid,
  3011. struct cifs_tcon *tcon,
  3012. struct cifsFileInfo *cfile,
  3013. loff_t off, loff_t len)
  3014. {
  3015. struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data;
  3016. u32 out_data_len;
  3017. char *buf = NULL;
  3018. loff_t l;
  3019. int rc;
  3020. in_data.file_offset = cpu_to_le64(off);
  3021. in_data.length = cpu_to_le64(len);
  3022. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  3023. cfile->fid.volatile_fid,
  3024. FSCTL_QUERY_ALLOCATED_RANGES,
  3025. (char *)&in_data, sizeof(in_data),
  3026. 1024 * sizeof(struct file_allocated_range_buffer),
  3027. (char **)&out_data, &out_data_len);
  3028. if (rc)
  3029. goto out;
  3030. buf = kzalloc(1024 * 1024, GFP_KERNEL);
  3031. if (buf == NULL) {
  3032. rc = -ENOMEM;
  3033. goto out;
  3034. }
  3035. tmp_data = out_data;
  3036. while (len) {
  3037. /*
  3038. * The rest of the region is unmapped so write it all.
  3039. */
  3040. if (out_data_len == 0) {
  3041. rc = smb3_simple_fallocate_write_range(xid, tcon,
  3042. cfile, off, len, buf);
  3043. goto out;
  3044. }
  3045. if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
  3046. rc = -EINVAL;
  3047. goto out;
  3048. }
  3049. if (off < le64_to_cpu(tmp_data->file_offset)) {
  3050. /*
  3051. * We are at a hole. Write until the end of the region
  3052. * or until the next allocated data,
  3053. * whichever comes next.
  3054. */
  3055. l = le64_to_cpu(tmp_data->file_offset) - off;
  3056. if (len < l)
  3057. l = len;
  3058. rc = smb3_simple_fallocate_write_range(xid, tcon,
  3059. cfile, off, l, buf);
  3060. if (rc)
  3061. goto out;
  3062. off = off + l;
  3063. len = len - l;
  3064. if (len == 0)
  3065. goto out;
  3066. }
  3067. /*
  3068. * We are at a section of allocated data, just skip forward
  3069. * until the end of the data or the end of the region
  3070. * we are supposed to fallocate, whichever comes first.
  3071. */
  3072. l = le64_to_cpu(tmp_data->length);
  3073. if (len < l)
  3074. l = len;
  3075. off += l;
  3076. len -= l;
  3077. tmp_data = &tmp_data[1];
  3078. out_data_len -= sizeof(struct file_allocated_range_buffer);
  3079. }
  3080. out:
  3081. kfree(out_data);
  3082. kfree(buf);
  3083. return rc;
  3084. }
  3085. static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
  3086. loff_t off, loff_t len, bool keep_size)
  3087. {
  3088. struct inode *inode;
  3089. struct cifsInodeInfo *cifsi;
  3090. struct cifsFileInfo *cfile = file->private_data;
  3091. long rc = -EOPNOTSUPP;
  3092. unsigned int xid;
  3093. loff_t new_eof;
  3094. xid = get_xid();
  3095. inode = d_inode(cfile->dentry);
  3096. cifsi = CIFS_I(inode);
  3097. trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
  3098. tcon->ses->Suid, off, len);
  3099. /* if file not oplocked can't be sure whether asking to extend size */
  3100. if (!CIFS_CACHE_READ(cifsi))
  3101. if (keep_size == false) {
  3102. trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
  3103. tcon->tid, tcon->ses->Suid, off, len, rc);
  3104. free_xid(xid);
  3105. return rc;
  3106. }
  3107. /*
  3108. * Extending the file
  3109. */
  3110. if ((keep_size == false) && i_size_read(inode) < off + len) {
  3111. rc = inode_newsize_ok(inode, off + len);
  3112. if (rc)
  3113. goto out;
  3114. if (cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)
  3115. smb2_set_sparse(xid, tcon, cfile, inode, false);
  3116. new_eof = off + len;
  3117. rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  3118. cfile->fid.volatile_fid, cfile->pid, new_eof);
  3119. if (rc == 0) {
  3120. netfs_resize_file(&cifsi->netfs, new_eof, true);
  3121. cifs_setsize(inode, new_eof);
  3122. cifs_truncate_page(inode->i_mapping, inode->i_size);
  3123. truncate_setsize(inode, new_eof);
  3124. }
  3125. goto out;
  3126. }
  3127. /*
  3128. * Files are non-sparse by default so falloc may be a no-op
  3129. * Must check if file sparse. If not sparse, and since we are not
  3130. * extending then no need to do anything since file already allocated
  3131. */
  3132. if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
  3133. rc = 0;
  3134. goto out;
  3135. }
  3136. if (keep_size == true) {
  3137. /*
  3138. * We can not preallocate pages beyond the end of the file
  3139. * in SMB2
  3140. */
  3141. if (off >= i_size_read(inode)) {
  3142. rc = 0;
  3143. goto out;
  3144. }
  3145. /*
  3146. * For fallocates that are partially beyond the end of file,
  3147. * clamp len so we only fallocate up to the end of file.
  3148. */
  3149. if (off + len > i_size_read(inode)) {
  3150. len = i_size_read(inode) - off;
  3151. }
  3152. }
  3153. if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
  3154. /*
  3155. * At this point, we are trying to fallocate an internal
  3156. * regions of a sparse file. Since smb2 does not have a
  3157. * fallocate command we have two options on how to emulate this.
  3158. * We can either turn the entire file to become non-sparse
  3159. * which we only do if the fallocate is for virtually
  3160. * the whole file, or we can overwrite the region with zeroes
  3161. * using SMB2_write, which could be prohibitevly expensive
  3162. * if len is large.
  3163. */
  3164. /*
  3165. * We are only trying to fallocate a small region so
  3166. * just write it with zero.
  3167. */
  3168. if (len <= 1024 * 1024) {
  3169. rc = smb3_simple_fallocate_range(xid, tcon, cfile,
  3170. off, len);
  3171. goto out;
  3172. }
  3173. /*
  3174. * Check if falloc starts within first few pages of file
  3175. * and ends within a few pages of the end of file to
  3176. * ensure that most of file is being forced to be
  3177. * fallocated now. If so then setting whole file sparse
  3178. * ie potentially making a few extra pages at the beginning
  3179. * or end of the file non-sparse via set_sparse is harmless.
  3180. */
  3181. if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
  3182. rc = -EOPNOTSUPP;
  3183. goto out;
  3184. }
  3185. }
  3186. smb2_set_sparse(xid, tcon, cfile, inode, false);
  3187. rc = 0;
  3188. out:
  3189. if (rc)
  3190. trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
  3191. tcon->ses->Suid, off, len, rc);
  3192. else
  3193. trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
  3194. tcon->ses->Suid, off, len);
  3195. free_xid(xid);
  3196. return rc;
  3197. }
  3198. static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
  3199. loff_t off, loff_t len)
  3200. {
  3201. int rc;
  3202. unsigned int xid;
  3203. struct inode *inode = file_inode(file);
  3204. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  3205. struct cifsFileInfo *cfile = file->private_data;
  3206. struct netfs_inode *ictx = &cifsi->netfs;
  3207. loff_t old_eof, new_eof;
  3208. xid = get_xid();
  3209. inode_lock(inode);
  3210. old_eof = i_size_read(inode);
  3211. if ((off >= old_eof) ||
  3212. off + len >= old_eof) {
  3213. rc = -EINVAL;
  3214. goto out;
  3215. }
  3216. filemap_invalidate_lock(inode->i_mapping);
  3217. rc = filemap_write_and_wait_range(inode->i_mapping, off, old_eof - 1);
  3218. if (rc < 0)
  3219. goto out_2;
  3220. truncate_pagecache_range(inode, off, old_eof);
  3221. ictx->zero_point = old_eof;
  3222. rc = smb2_copychunk_range(xid, cfile, cfile, off + len,
  3223. old_eof - off - len, off);
  3224. if (rc < 0)
  3225. goto out_2;
  3226. new_eof = old_eof - len;
  3227. rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  3228. cfile->fid.volatile_fid, cfile->pid, new_eof);
  3229. if (rc < 0)
  3230. goto out_2;
  3231. rc = 0;
  3232. truncate_setsize(inode, new_eof);
  3233. netfs_resize_file(&cifsi->netfs, new_eof, true);
  3234. ictx->zero_point = new_eof;
  3235. fscache_resize_cookie(cifs_inode_cookie(inode), new_eof);
  3236. out_2:
  3237. filemap_invalidate_unlock(inode->i_mapping);
  3238. out:
  3239. inode_unlock(inode);
  3240. free_xid(xid);
  3241. return rc;
  3242. }
  3243. static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
  3244. loff_t off, loff_t len)
  3245. {
  3246. int rc;
  3247. unsigned int xid;
  3248. struct cifsFileInfo *cfile = file->private_data;
  3249. struct inode *inode = file_inode(file);
  3250. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  3251. __u64 count, old_eof, new_eof;
  3252. xid = get_xid();
  3253. inode_lock(inode);
  3254. old_eof = i_size_read(inode);
  3255. if (off >= old_eof) {
  3256. rc = -EINVAL;
  3257. goto out;
  3258. }
  3259. count = old_eof - off;
  3260. new_eof = old_eof + len;
  3261. filemap_invalidate_lock(inode->i_mapping);
  3262. rc = filemap_write_and_wait_range(inode->i_mapping, off, new_eof - 1);
  3263. if (rc < 0)
  3264. goto out_2;
  3265. truncate_pagecache_range(inode, off, old_eof);
  3266. rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
  3267. cfile->fid.volatile_fid, cfile->pid, new_eof);
  3268. if (rc < 0)
  3269. goto out_2;
  3270. truncate_setsize(inode, new_eof);
  3271. netfs_resize_file(&cifsi->netfs, i_size_read(inode), true);
  3272. fscache_resize_cookie(cifs_inode_cookie(inode), i_size_read(inode));
  3273. rc = smb2_copychunk_range(xid, cfile, cfile, off, count, off + len);
  3274. if (rc < 0)
  3275. goto out_2;
  3276. cifsi->netfs.zero_point = new_eof;
  3277. rc = smb3_zero_data(file, tcon, off, len, xid);
  3278. if (rc < 0)
  3279. goto out_2;
  3280. rc = 0;
  3281. out_2:
  3282. filemap_invalidate_unlock(inode->i_mapping);
  3283. out:
  3284. inode_unlock(inode);
  3285. free_xid(xid);
  3286. return rc;
  3287. }
  3288. static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
  3289. {
  3290. struct cifsFileInfo *wrcfile, *cfile = file->private_data;
  3291. struct cifsInodeInfo *cifsi;
  3292. struct inode *inode;
  3293. int rc = 0;
  3294. struct file_allocated_range_buffer in_data, *out_data = NULL;
  3295. u32 out_data_len;
  3296. unsigned int xid;
  3297. if (whence != SEEK_HOLE && whence != SEEK_DATA)
  3298. return generic_file_llseek(file, offset, whence);
  3299. inode = d_inode(cfile->dentry);
  3300. cifsi = CIFS_I(inode);
  3301. if (offset < 0 || offset >= i_size_read(inode))
  3302. return -ENXIO;
  3303. xid = get_xid();
  3304. /*
  3305. * We need to be sure that all dirty pages are written as they
  3306. * might fill holes on the server.
  3307. * Note that we also MUST flush any written pages since at least
  3308. * some servers (Windows2016) will not reflect recent writes in
  3309. * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
  3310. */
  3311. wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
  3312. if (wrcfile) {
  3313. filemap_write_and_wait(inode->i_mapping);
  3314. smb2_flush_file(xid, tcon, &wrcfile->fid);
  3315. cifsFileInfo_put(wrcfile);
  3316. }
  3317. if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
  3318. if (whence == SEEK_HOLE)
  3319. offset = i_size_read(inode);
  3320. goto lseek_exit;
  3321. }
  3322. in_data.file_offset = cpu_to_le64(offset);
  3323. in_data.length = cpu_to_le64(i_size_read(inode));
  3324. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  3325. cfile->fid.volatile_fid,
  3326. FSCTL_QUERY_ALLOCATED_RANGES,
  3327. (char *)&in_data, sizeof(in_data),
  3328. sizeof(struct file_allocated_range_buffer),
  3329. (char **)&out_data, &out_data_len);
  3330. if (rc == -E2BIG)
  3331. rc = 0;
  3332. if (rc)
  3333. goto lseek_exit;
  3334. if (whence == SEEK_HOLE && out_data_len == 0)
  3335. goto lseek_exit;
  3336. if (whence == SEEK_DATA && out_data_len == 0) {
  3337. rc = -ENXIO;
  3338. goto lseek_exit;
  3339. }
  3340. if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
  3341. rc = -EINVAL;
  3342. goto lseek_exit;
  3343. }
  3344. if (whence == SEEK_DATA) {
  3345. offset = le64_to_cpu(out_data->file_offset);
  3346. goto lseek_exit;
  3347. }
  3348. if (offset < le64_to_cpu(out_data->file_offset))
  3349. goto lseek_exit;
  3350. offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
  3351. lseek_exit:
  3352. free_xid(xid);
  3353. kfree(out_data);
  3354. if (!rc)
  3355. return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  3356. else
  3357. return rc;
  3358. }
  3359. static int smb3_fiemap(struct cifs_tcon *tcon,
  3360. struct cifsFileInfo *cfile,
  3361. struct fiemap_extent_info *fei, u64 start, u64 len)
  3362. {
  3363. unsigned int xid;
  3364. struct file_allocated_range_buffer in_data, *out_data;
  3365. u32 out_data_len;
  3366. int i, num, rc, flags, last_blob;
  3367. u64 next;
  3368. rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
  3369. if (rc)
  3370. return rc;
  3371. xid = get_xid();
  3372. again:
  3373. in_data.file_offset = cpu_to_le64(start);
  3374. in_data.length = cpu_to_le64(len);
  3375. rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
  3376. cfile->fid.volatile_fid,
  3377. FSCTL_QUERY_ALLOCATED_RANGES,
  3378. (char *)&in_data, sizeof(in_data),
  3379. 1024 * sizeof(struct file_allocated_range_buffer),
  3380. (char **)&out_data, &out_data_len);
  3381. if (rc == -E2BIG) {
  3382. last_blob = 0;
  3383. rc = 0;
  3384. } else
  3385. last_blob = 1;
  3386. if (rc)
  3387. goto out;
  3388. if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
  3389. rc = -EINVAL;
  3390. goto out;
  3391. }
  3392. if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
  3393. rc = -EINVAL;
  3394. goto out;
  3395. }
  3396. num = out_data_len / sizeof(struct file_allocated_range_buffer);
  3397. for (i = 0; i < num; i++) {
  3398. flags = 0;
  3399. if (i == num - 1 && last_blob)
  3400. flags |= FIEMAP_EXTENT_LAST;
  3401. rc = fiemap_fill_next_extent(fei,
  3402. le64_to_cpu(out_data[i].file_offset),
  3403. le64_to_cpu(out_data[i].file_offset),
  3404. le64_to_cpu(out_data[i].length),
  3405. flags);
  3406. if (rc < 0)
  3407. goto out;
  3408. if (rc == 1) {
  3409. rc = 0;
  3410. goto out;
  3411. }
  3412. }
  3413. if (!last_blob) {
  3414. next = le64_to_cpu(out_data[num - 1].file_offset) +
  3415. le64_to_cpu(out_data[num - 1].length);
  3416. len = len - (next - start);
  3417. start = next;
  3418. goto again;
  3419. }
  3420. out:
  3421. free_xid(xid);
  3422. kfree(out_data);
  3423. return rc;
  3424. }
  3425. static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
  3426. loff_t off, loff_t len)
  3427. {
  3428. /* KEEP_SIZE already checked for by do_fallocate */
  3429. if (mode & FALLOC_FL_PUNCH_HOLE)
  3430. return smb3_punch_hole(file, tcon, off, len);
  3431. else if (mode & FALLOC_FL_ZERO_RANGE) {
  3432. if (mode & FALLOC_FL_KEEP_SIZE)
  3433. return smb3_zero_range(file, tcon, off, len, true);
  3434. return smb3_zero_range(file, tcon, off, len, false);
  3435. } else if (mode == FALLOC_FL_KEEP_SIZE)
  3436. return smb3_simple_falloc(file, tcon, off, len, true);
  3437. else if (mode == FALLOC_FL_COLLAPSE_RANGE)
  3438. return smb3_collapse_range(file, tcon, off, len);
  3439. else if (mode == FALLOC_FL_INSERT_RANGE)
  3440. return smb3_insert_range(file, tcon, off, len);
  3441. else if (mode == 0)
  3442. return smb3_simple_falloc(file, tcon, off, len, false);
  3443. return -EOPNOTSUPP;
  3444. }
  3445. static void
  3446. smb2_downgrade_oplock(struct TCP_Server_Info *server,
  3447. struct cifsInodeInfo *cinode, __u32 oplock,
  3448. __u16 epoch, bool *purge_cache)
  3449. {
  3450. server->ops->set_oplock_level(cinode, oplock, 0, NULL);
  3451. }
  3452. static void
  3453. smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  3454. __u16 epoch, bool *purge_cache);
  3455. static void
  3456. smb3_downgrade_oplock(struct TCP_Server_Info *server,
  3457. struct cifsInodeInfo *cinode, __u32 oplock,
  3458. __u16 epoch, bool *purge_cache)
  3459. {
  3460. unsigned int old_state = cinode->oplock;
  3461. __u16 old_epoch = cinode->epoch;
  3462. unsigned int new_state;
  3463. if (epoch > old_epoch) {
  3464. smb21_set_oplock_level(cinode, oplock, 0, NULL);
  3465. cinode->epoch = epoch;
  3466. }
  3467. new_state = cinode->oplock;
  3468. *purge_cache = false;
  3469. if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
  3470. (new_state & CIFS_CACHE_READ_FLG) == 0)
  3471. *purge_cache = true;
  3472. else if (old_state == new_state && (epoch - old_epoch > 1))
  3473. *purge_cache = true;
  3474. }
  3475. static void
  3476. smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  3477. __u16 epoch, bool *purge_cache)
  3478. {
  3479. oplock &= 0xFF;
  3480. cinode->lease_granted = false;
  3481. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  3482. return;
  3483. if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  3484. cinode->oplock = CIFS_CACHE_RHW_FLG;
  3485. cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
  3486. &cinode->netfs.inode);
  3487. } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
  3488. cinode->oplock = CIFS_CACHE_RW_FLG;
  3489. cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
  3490. &cinode->netfs.inode);
  3491. } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
  3492. cinode->oplock = CIFS_CACHE_READ_FLG;
  3493. cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
  3494. &cinode->netfs.inode);
  3495. } else
  3496. cinode->oplock = 0;
  3497. }
  3498. static void
  3499. smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  3500. __u16 epoch, bool *purge_cache)
  3501. {
  3502. char message[5] = {0};
  3503. unsigned int new_oplock = 0;
  3504. oplock &= 0xFF;
  3505. cinode->lease_granted = true;
  3506. if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
  3507. return;
  3508. /* Check if the server granted an oplock rather than a lease */
  3509. if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
  3510. return smb2_set_oplock_level(cinode, oplock, epoch,
  3511. purge_cache);
  3512. if (oplock & SMB2_LEASE_READ_CACHING_HE) {
  3513. new_oplock |= CIFS_CACHE_READ_FLG;
  3514. strcat(message, "R");
  3515. }
  3516. if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
  3517. new_oplock |= CIFS_CACHE_HANDLE_FLG;
  3518. strcat(message, "H");
  3519. }
  3520. if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
  3521. new_oplock |= CIFS_CACHE_WRITE_FLG;
  3522. strcat(message, "W");
  3523. }
  3524. if (!new_oplock)
  3525. strscpy(message, "None");
  3526. cinode->oplock = new_oplock;
  3527. cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
  3528. &cinode->netfs.inode);
  3529. }
  3530. static void
  3531. smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
  3532. __u16 epoch, bool *purge_cache)
  3533. {
  3534. unsigned int old_oplock = cinode->oplock;
  3535. smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
  3536. if (purge_cache) {
  3537. *purge_cache = false;
  3538. if (old_oplock == CIFS_CACHE_READ_FLG) {
  3539. if (cinode->oplock == CIFS_CACHE_READ_FLG &&
  3540. (epoch - cinode->epoch > 0))
  3541. *purge_cache = true;
  3542. else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
  3543. (epoch - cinode->epoch > 1))
  3544. *purge_cache = true;
  3545. else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
  3546. (epoch - cinode->epoch > 1))
  3547. *purge_cache = true;
  3548. else if (cinode->oplock == 0 &&
  3549. (epoch - cinode->epoch > 0))
  3550. *purge_cache = true;
  3551. } else if (old_oplock == CIFS_CACHE_RH_FLG) {
  3552. if (cinode->oplock == CIFS_CACHE_RH_FLG &&
  3553. (epoch - cinode->epoch > 0))
  3554. *purge_cache = true;
  3555. else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
  3556. (epoch - cinode->epoch > 1))
  3557. *purge_cache = true;
  3558. }
  3559. cinode->epoch = epoch;
  3560. }
  3561. }
  3562. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  3563. static bool
  3564. smb2_is_read_op(__u32 oplock)
  3565. {
  3566. return oplock == SMB2_OPLOCK_LEVEL_II;
  3567. }
  3568. #endif /* CIFS_ALLOW_INSECURE_LEGACY */
  3569. static bool
  3570. smb21_is_read_op(__u32 oplock)
  3571. {
  3572. return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
  3573. !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
  3574. }
  3575. static __le32
  3576. map_oplock_to_lease(u8 oplock)
  3577. {
  3578. if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
  3579. return SMB2_LEASE_WRITE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE;
  3580. else if (oplock == SMB2_OPLOCK_LEVEL_II)
  3581. return SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE;
  3582. else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
  3583. return SMB2_LEASE_HANDLE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE |
  3584. SMB2_LEASE_WRITE_CACHING_LE;
  3585. return 0;
  3586. }
  3587. static char *
  3588. smb2_create_lease_buf(u8 *lease_key, u8 oplock)
  3589. {
  3590. struct create_lease *buf;
  3591. buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
  3592. if (!buf)
  3593. return NULL;
  3594. memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
  3595. buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
  3596. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  3597. (struct create_lease, lcontext));
  3598. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
  3599. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  3600. (struct create_lease, Name));
  3601. buf->ccontext.NameLength = cpu_to_le16(4);
  3602. /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
  3603. buf->Name[0] = 'R';
  3604. buf->Name[1] = 'q';
  3605. buf->Name[2] = 'L';
  3606. buf->Name[3] = 's';
  3607. return (char *)buf;
  3608. }
  3609. static char *
  3610. smb3_create_lease_buf(u8 *lease_key, u8 oplock)
  3611. {
  3612. struct create_lease_v2 *buf;
  3613. buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
  3614. if (!buf)
  3615. return NULL;
  3616. memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
  3617. buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
  3618. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  3619. (struct create_lease_v2, lcontext));
  3620. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
  3621. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  3622. (struct create_lease_v2, Name));
  3623. buf->ccontext.NameLength = cpu_to_le16(4);
  3624. /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
  3625. buf->Name[0] = 'R';
  3626. buf->Name[1] = 'q';
  3627. buf->Name[2] = 'L';
  3628. buf->Name[3] = 's';
  3629. return (char *)buf;
  3630. }
  3631. static __u8
  3632. smb2_parse_lease_buf(void *buf, __u16 *epoch, char *lease_key)
  3633. {
  3634. struct create_lease *lc = (struct create_lease *)buf;
  3635. *epoch = 0; /* not used */
  3636. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
  3637. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  3638. return le32_to_cpu(lc->lcontext.LeaseState);
  3639. }
  3640. static __u8
  3641. smb3_parse_lease_buf(void *buf, __u16 *epoch, char *lease_key)
  3642. {
  3643. struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
  3644. *epoch = le16_to_cpu(lc->lcontext.Epoch);
  3645. if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
  3646. return SMB2_OPLOCK_LEVEL_NOCHANGE;
  3647. if (lease_key)
  3648. memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
  3649. return le32_to_cpu(lc->lcontext.LeaseState);
  3650. }
  3651. static unsigned int
  3652. smb2_wp_retry_size(struct inode *inode)
  3653. {
  3654. return min_t(unsigned int, CIFS_SB(inode->i_sb)->ctx->wsize,
  3655. SMB2_MAX_BUFFER_SIZE);
  3656. }
  3657. static bool
  3658. smb2_dir_needs_close(struct cifsFileInfo *cfile)
  3659. {
  3660. return !cfile->invalidHandle;
  3661. }
  3662. static void
  3663. fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
  3664. struct smb_rqst *old_rq, __le16 cipher_type)
  3665. {
  3666. struct smb2_hdr *shdr =
  3667. (struct smb2_hdr *)old_rq->rq_iov[0].iov_base;
  3668. memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
  3669. tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
  3670. tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
  3671. tr_hdr->Flags = cpu_to_le16(0x01);
  3672. if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
  3673. (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
  3674. get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
  3675. else
  3676. get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
  3677. memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
  3678. }
  3679. static void *smb2_aead_req_alloc(struct crypto_aead *tfm, const struct smb_rqst *rqst,
  3680. int num_rqst, const u8 *sig, u8 **iv,
  3681. struct aead_request **req, struct sg_table *sgt,
  3682. unsigned int *num_sgs)
  3683. {
  3684. unsigned int req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
  3685. unsigned int iv_size = crypto_aead_ivsize(tfm);
  3686. unsigned int len;
  3687. u8 *p;
  3688. *num_sgs = cifs_get_num_sgs(rqst, num_rqst, sig);
  3689. if (IS_ERR_VALUE((long)(int)*num_sgs))
  3690. return ERR_PTR(*num_sgs);
  3691. len = iv_size;
  3692. len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
  3693. len = ALIGN(len, crypto_tfm_ctx_alignment());
  3694. len += req_size;
  3695. len = ALIGN(len, __alignof__(struct scatterlist));
  3696. len += array_size(*num_sgs, sizeof(struct scatterlist));
  3697. p = kzalloc(len, GFP_NOFS);
  3698. if (!p)
  3699. return ERR_PTR(-ENOMEM);
  3700. *iv = (u8 *)PTR_ALIGN(p, crypto_aead_alignmask(tfm) + 1);
  3701. *req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
  3702. crypto_tfm_ctx_alignment());
  3703. sgt->sgl = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
  3704. __alignof__(struct scatterlist));
  3705. return p;
  3706. }
  3707. static void *smb2_get_aead_req(struct crypto_aead *tfm, struct smb_rqst *rqst,
  3708. int num_rqst, const u8 *sig, u8 **iv,
  3709. struct aead_request **req, struct scatterlist **sgl)
  3710. {
  3711. struct sg_table sgtable = {};
  3712. unsigned int skip, num_sgs, i, j;
  3713. ssize_t rc;
  3714. void *p;
  3715. p = smb2_aead_req_alloc(tfm, rqst, num_rqst, sig, iv, req, &sgtable, &num_sgs);
  3716. if (IS_ERR(p))
  3717. return ERR_CAST(p);
  3718. sg_init_marker(sgtable.sgl, num_sgs);
  3719. /*
  3720. * The first rqst has a transform header where the
  3721. * first 20 bytes are not part of the encrypted blob.
  3722. */
  3723. skip = 20;
  3724. for (i = 0; i < num_rqst; i++) {
  3725. struct iov_iter *iter = &rqst[i].rq_iter;
  3726. size_t count = iov_iter_count(iter);
  3727. for (j = 0; j < rqst[i].rq_nvec; j++) {
  3728. cifs_sg_set_buf(&sgtable,
  3729. rqst[i].rq_iov[j].iov_base + skip,
  3730. rqst[i].rq_iov[j].iov_len - skip);
  3731. /* See the above comment on the 'skip' assignment */
  3732. skip = 0;
  3733. }
  3734. sgtable.orig_nents = sgtable.nents;
  3735. rc = extract_iter_to_sg(iter, count, &sgtable,
  3736. num_sgs - sgtable.nents, 0);
  3737. iov_iter_revert(iter, rc);
  3738. sgtable.orig_nents = sgtable.nents;
  3739. }
  3740. cifs_sg_set_buf(&sgtable, sig, SMB2_SIGNATURE_SIZE);
  3741. sg_mark_end(&sgtable.sgl[sgtable.nents - 1]);
  3742. *sgl = sgtable.sgl;
  3743. return p;
  3744. }
  3745. static int
  3746. smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
  3747. {
  3748. struct TCP_Server_Info *pserver;
  3749. struct cifs_ses *ses;
  3750. u8 *ses_enc_key;
  3751. /* If server is a channel, select the primary channel */
  3752. pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
  3753. spin_lock(&cifs_tcp_ses_lock);
  3754. list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
  3755. if (ses->Suid == ses_id) {
  3756. spin_lock(&ses->ses_lock);
  3757. ses_enc_key = enc ? ses->smb3encryptionkey :
  3758. ses->smb3decryptionkey;
  3759. memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
  3760. spin_unlock(&ses->ses_lock);
  3761. spin_unlock(&cifs_tcp_ses_lock);
  3762. return 0;
  3763. }
  3764. }
  3765. spin_unlock(&cifs_tcp_ses_lock);
  3766. trace_smb3_ses_not_found(ses_id);
  3767. return -EAGAIN;
  3768. }
  3769. /*
  3770. * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
  3771. * iov[0] - transform header (associate data),
  3772. * iov[1-N] - SMB2 header and pages - data to encrypt.
  3773. * On success return encrypted data in iov[1-N] and pages, leave iov[0]
  3774. * untouched.
  3775. */
  3776. static int
  3777. crypt_message(struct TCP_Server_Info *server, int num_rqst,
  3778. struct smb_rqst *rqst, int enc, struct crypto_aead *tfm)
  3779. {
  3780. struct smb2_transform_hdr *tr_hdr =
  3781. (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
  3782. unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
  3783. int rc = 0;
  3784. struct scatterlist *sg;
  3785. u8 sign[SMB2_SIGNATURE_SIZE] = {};
  3786. u8 key[SMB3_ENC_DEC_KEY_SIZE];
  3787. struct aead_request *req;
  3788. u8 *iv;
  3789. DECLARE_CRYPTO_WAIT(wait);
  3790. unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
  3791. void *creq;
  3792. rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), enc, key);
  3793. if (rc) {
  3794. cifs_server_dbg(FYI, "%s: Could not get %scryption key. sid: 0x%llx\n", __func__,
  3795. enc ? "en" : "de", le64_to_cpu(tr_hdr->SessionId));
  3796. return rc;
  3797. }
  3798. if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
  3799. (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
  3800. rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
  3801. else
  3802. rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
  3803. if (rc) {
  3804. cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
  3805. return rc;
  3806. }
  3807. rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
  3808. if (rc) {
  3809. cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
  3810. return rc;
  3811. }
  3812. creq = smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg);
  3813. if (IS_ERR(creq))
  3814. return PTR_ERR(creq);
  3815. if (!enc) {
  3816. memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
  3817. crypt_len += SMB2_SIGNATURE_SIZE;
  3818. }
  3819. if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
  3820. (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
  3821. memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
  3822. else {
  3823. iv[0] = 3;
  3824. memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
  3825. }
  3826. aead_request_set_tfm(req, tfm);
  3827. aead_request_set_crypt(req, sg, sg, crypt_len, iv);
  3828. aead_request_set_ad(req, assoc_data_len);
  3829. aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  3830. crypto_req_done, &wait);
  3831. rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
  3832. : crypto_aead_decrypt(req), &wait);
  3833. if (!rc && enc)
  3834. memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
  3835. kfree_sensitive(creq);
  3836. return rc;
  3837. }
  3838. /*
  3839. * Clear a read buffer, discarding the folios which have the 1st mark set.
  3840. */
  3841. static void cifs_clear_folioq_buffer(struct folio_queue *buffer)
  3842. {
  3843. struct folio_queue *folioq;
  3844. while ((folioq = buffer)) {
  3845. for (int s = 0; s < folioq_count(folioq); s++)
  3846. if (folioq_is_marked(folioq, s))
  3847. folio_put(folioq_folio(folioq, s));
  3848. buffer = folioq->next;
  3849. kfree(folioq);
  3850. }
  3851. }
  3852. /*
  3853. * Allocate buffer space into a folio queue.
  3854. */
  3855. static struct folio_queue *cifs_alloc_folioq_buffer(ssize_t size)
  3856. {
  3857. struct folio_queue *buffer = NULL, *tail = NULL, *p;
  3858. struct folio *folio;
  3859. unsigned int slot;
  3860. do {
  3861. if (!tail || folioq_full(tail)) {
  3862. p = kmalloc(sizeof(*p), GFP_NOFS);
  3863. if (!p)
  3864. goto nomem;
  3865. folioq_init(p);
  3866. if (tail) {
  3867. tail->next = p;
  3868. p->prev = tail;
  3869. } else {
  3870. buffer = p;
  3871. }
  3872. tail = p;
  3873. }
  3874. folio = folio_alloc(GFP_KERNEL|__GFP_HIGHMEM, 0);
  3875. if (!folio)
  3876. goto nomem;
  3877. slot = folioq_append_mark(tail, folio);
  3878. size -= folioq_folio_size(tail, slot);
  3879. } while (size > 0);
  3880. return buffer;
  3881. nomem:
  3882. cifs_clear_folioq_buffer(buffer);
  3883. return NULL;
  3884. }
  3885. /*
  3886. * Copy data from an iterator to the folios in a folio queue buffer.
  3887. */
  3888. static bool cifs_copy_iter_to_folioq(struct iov_iter *iter, size_t size,
  3889. struct folio_queue *buffer)
  3890. {
  3891. for (; buffer; buffer = buffer->next) {
  3892. for (int s = 0; s < folioq_count(buffer); s++) {
  3893. struct folio *folio = folioq_folio(buffer, s);
  3894. size_t part = folioq_folio_size(buffer, s);
  3895. part = umin(part, size);
  3896. if (copy_folio_from_iter(folio, 0, part, iter) != part)
  3897. return false;
  3898. size -= part;
  3899. }
  3900. }
  3901. return true;
  3902. }
  3903. void
  3904. smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
  3905. {
  3906. for (int i = 0; i < num_rqst; i++)
  3907. cifs_clear_folioq_buffer(rqst[i].rq_buffer);
  3908. }
  3909. /*
  3910. * This function will initialize new_rq and encrypt the content.
  3911. * The first entry, new_rq[0], only contains a single iov which contains
  3912. * a smb2_transform_hdr and is pre-allocated by the caller.
  3913. * This function then populates new_rq[1+] with the content from olq_rq[0+].
  3914. *
  3915. * The end result is an array of smb_rqst structures where the first structure
  3916. * only contains a single iov for the transform header which we then can pass
  3917. * to crypt_message().
  3918. *
  3919. * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
  3920. * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
  3921. */
  3922. static int
  3923. smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
  3924. struct smb_rqst *new_rq, struct smb_rqst *old_rq)
  3925. {
  3926. struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
  3927. unsigned int orig_len = 0;
  3928. int rc = -ENOMEM;
  3929. for (int i = 1; i < num_rqst; i++) {
  3930. struct smb_rqst *old = &old_rq[i - 1];
  3931. struct smb_rqst *new = &new_rq[i];
  3932. struct folio_queue *buffer = NULL;
  3933. size_t size = iov_iter_count(&old->rq_iter);
  3934. orig_len += smb_rqst_len(server, old);
  3935. new->rq_iov = old->rq_iov;
  3936. new->rq_nvec = old->rq_nvec;
  3937. if (size > 0) {
  3938. buffer = cifs_alloc_folioq_buffer(size);
  3939. if (!buffer)
  3940. goto err_free;
  3941. new->rq_buffer = buffer;
  3942. iov_iter_folio_queue(&new->rq_iter, ITER_SOURCE,
  3943. buffer, 0, 0, size);
  3944. if (!cifs_copy_iter_to_folioq(&old->rq_iter, size, buffer)) {
  3945. rc = -EIO;
  3946. goto err_free;
  3947. }
  3948. }
  3949. }
  3950. /* fill the 1st iov with a transform header */
  3951. fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
  3952. rc = crypt_message(server, num_rqst, new_rq, 1, server->secmech.enc);
  3953. cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
  3954. if (rc)
  3955. goto err_free;
  3956. return rc;
  3957. err_free:
  3958. smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
  3959. return rc;
  3960. }
  3961. static int
  3962. smb3_is_transform_hdr(void *buf)
  3963. {
  3964. struct smb2_transform_hdr *trhdr = buf;
  3965. return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
  3966. }
  3967. static int
  3968. decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
  3969. unsigned int buf_data_size, struct iov_iter *iter,
  3970. bool is_offloaded)
  3971. {
  3972. struct crypto_aead *tfm;
  3973. struct smb_rqst rqst = {NULL};
  3974. struct kvec iov[2];
  3975. size_t iter_size = 0;
  3976. int rc;
  3977. iov[0].iov_base = buf;
  3978. iov[0].iov_len = sizeof(struct smb2_transform_hdr);
  3979. iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
  3980. iov[1].iov_len = buf_data_size;
  3981. rqst.rq_iov = iov;
  3982. rqst.rq_nvec = 2;
  3983. if (iter) {
  3984. rqst.rq_iter = *iter;
  3985. iter_size = iov_iter_count(iter);
  3986. }
  3987. if (is_offloaded) {
  3988. if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
  3989. (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
  3990. tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
  3991. else
  3992. tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
  3993. if (IS_ERR(tfm)) {
  3994. rc = PTR_ERR(tfm);
  3995. cifs_server_dbg(VFS, "%s: Failed alloc decrypt TFM, rc=%d\n", __func__, rc);
  3996. return rc;
  3997. }
  3998. } else {
  3999. rc = smb3_crypto_aead_allocate(server);
  4000. if (unlikely(rc))
  4001. return rc;
  4002. tfm = server->secmech.dec;
  4003. }
  4004. rc = crypt_message(server, 1, &rqst, 0, tfm);
  4005. cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
  4006. if (is_offloaded)
  4007. crypto_free_aead(tfm);
  4008. if (rc)
  4009. return rc;
  4010. memmove(buf, iov[1].iov_base, buf_data_size);
  4011. if (!is_offloaded)
  4012. server->total_read = buf_data_size + iter_size;
  4013. return rc;
  4014. }
  4015. static int
  4016. cifs_copy_folioq_to_iter(struct folio_queue *folioq, size_t data_size,
  4017. size_t skip, struct iov_iter *iter)
  4018. {
  4019. for (; folioq; folioq = folioq->next) {
  4020. for (int s = 0; s < folioq_count(folioq); s++) {
  4021. struct folio *folio = folioq_folio(folioq, s);
  4022. size_t fsize = folio_size(folio);
  4023. size_t n, len = umin(fsize - skip, data_size);
  4024. n = copy_folio_to_iter(folio, skip, len, iter);
  4025. if (n != len) {
  4026. cifs_dbg(VFS, "%s: something went wrong\n", __func__);
  4027. return -EIO;
  4028. }
  4029. data_size -= n;
  4030. skip = 0;
  4031. }
  4032. }
  4033. return 0;
  4034. }
  4035. static int
  4036. handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
  4037. char *buf, unsigned int buf_len, struct folio_queue *buffer,
  4038. unsigned int buffer_len, bool is_offloaded)
  4039. {
  4040. unsigned int data_offset;
  4041. unsigned int data_len;
  4042. unsigned int cur_off;
  4043. unsigned int cur_page_idx;
  4044. unsigned int pad_len;
  4045. struct cifs_io_subrequest *rdata = mid->callback_data;
  4046. struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
  4047. size_t copied;
  4048. bool use_rdma_mr = false;
  4049. if (shdr->Command != SMB2_READ) {
  4050. cifs_server_dbg(VFS, "only big read responses are supported\n");
  4051. return -EOPNOTSUPP;
  4052. }
  4053. if (server->ops->is_session_expired &&
  4054. server->ops->is_session_expired(buf)) {
  4055. if (!is_offloaded)
  4056. cifs_reconnect(server, true);
  4057. return -1;
  4058. }
  4059. if (server->ops->is_status_pending &&
  4060. server->ops->is_status_pending(buf, server))
  4061. return -1;
  4062. /* set up first two iov to get credits */
  4063. rdata->iov[0].iov_base = buf;
  4064. rdata->iov[0].iov_len = 0;
  4065. rdata->iov[1].iov_base = buf;
  4066. rdata->iov[1].iov_len =
  4067. min_t(unsigned int, buf_len, server->vals->read_rsp_size);
  4068. cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
  4069. rdata->iov[0].iov_base, rdata->iov[0].iov_len);
  4070. cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
  4071. rdata->iov[1].iov_base, rdata->iov[1].iov_len);
  4072. rdata->result = server->ops->map_error(buf, true);
  4073. if (rdata->result != 0) {
  4074. cifs_dbg(FYI, "%s: server returned error %d\n",
  4075. __func__, rdata->result);
  4076. /* normal error on read response */
  4077. if (is_offloaded)
  4078. mid->mid_state = MID_RESPONSE_RECEIVED;
  4079. else
  4080. dequeue_mid(mid, false);
  4081. return 0;
  4082. }
  4083. data_offset = server->ops->read_data_offset(buf);
  4084. #ifdef CONFIG_CIFS_SMB_DIRECT
  4085. use_rdma_mr = rdata->mr;
  4086. #endif
  4087. data_len = server->ops->read_data_length(buf, use_rdma_mr);
  4088. if (data_offset < server->vals->read_rsp_size) {
  4089. /*
  4090. * win2k8 sometimes sends an offset of 0 when the read
  4091. * is beyond the EOF. Treat it as if the data starts just after
  4092. * the header.
  4093. */
  4094. cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
  4095. __func__, data_offset);
  4096. data_offset = server->vals->read_rsp_size;
  4097. } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
  4098. /* data_offset is beyond the end of smallbuf */
  4099. cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
  4100. __func__, data_offset);
  4101. rdata->result = -EIO;
  4102. if (is_offloaded)
  4103. mid->mid_state = MID_RESPONSE_MALFORMED;
  4104. else
  4105. dequeue_mid(mid, rdata->result);
  4106. return 0;
  4107. }
  4108. pad_len = data_offset - server->vals->read_rsp_size;
  4109. if (buf_len <= data_offset) {
  4110. /* read response payload is in pages */
  4111. cur_page_idx = pad_len / PAGE_SIZE;
  4112. cur_off = pad_len % PAGE_SIZE;
  4113. if (cur_page_idx != 0) {
  4114. /* data offset is beyond the 1st page of response */
  4115. cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
  4116. __func__, data_offset);
  4117. rdata->result = -EIO;
  4118. if (is_offloaded)
  4119. mid->mid_state = MID_RESPONSE_MALFORMED;
  4120. else
  4121. dequeue_mid(mid, rdata->result);
  4122. return 0;
  4123. }
  4124. if (data_len > buffer_len - pad_len) {
  4125. /* data_len is corrupt -- discard frame */
  4126. rdata->result = -EIO;
  4127. if (is_offloaded)
  4128. mid->mid_state = MID_RESPONSE_MALFORMED;
  4129. else
  4130. dequeue_mid(mid, rdata->result);
  4131. return 0;
  4132. }
  4133. /* Copy the data to the output I/O iterator. */
  4134. rdata->result = cifs_copy_folioq_to_iter(buffer, buffer_len,
  4135. cur_off, &rdata->subreq.io_iter);
  4136. if (rdata->result != 0) {
  4137. if (is_offloaded)
  4138. mid->mid_state = MID_RESPONSE_MALFORMED;
  4139. else
  4140. dequeue_mid(mid, rdata->result);
  4141. return 0;
  4142. }
  4143. rdata->got_bytes = buffer_len;
  4144. } else if (buf_len >= data_offset + data_len) {
  4145. /* read response payload is in buf */
  4146. WARN_ONCE(buffer, "read data can be either in buf or in buffer");
  4147. copied = copy_to_iter(buf + data_offset, data_len, &rdata->subreq.io_iter);
  4148. if (copied == 0)
  4149. return -EIO;
  4150. rdata->got_bytes = copied;
  4151. } else {
  4152. /* read response payload cannot be in both buf and pages */
  4153. WARN_ONCE(1, "buf can not contain only a part of read data");
  4154. rdata->result = -EIO;
  4155. if (is_offloaded)
  4156. mid->mid_state = MID_RESPONSE_MALFORMED;
  4157. else
  4158. dequeue_mid(mid, rdata->result);
  4159. return 0;
  4160. }
  4161. if (is_offloaded)
  4162. mid->mid_state = MID_RESPONSE_RECEIVED;
  4163. else
  4164. dequeue_mid(mid, false);
  4165. return 0;
  4166. }
  4167. struct smb2_decrypt_work {
  4168. struct work_struct decrypt;
  4169. struct TCP_Server_Info *server;
  4170. struct folio_queue *buffer;
  4171. char *buf;
  4172. unsigned int len;
  4173. };
  4174. static void smb2_decrypt_offload(struct work_struct *work)
  4175. {
  4176. struct smb2_decrypt_work *dw = container_of(work,
  4177. struct smb2_decrypt_work, decrypt);
  4178. int rc;
  4179. struct mid_q_entry *mid;
  4180. struct iov_iter iter;
  4181. iov_iter_folio_queue(&iter, ITER_DEST, dw->buffer, 0, 0, dw->len);
  4182. rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
  4183. &iter, true);
  4184. if (rc) {
  4185. cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
  4186. goto free_pages;
  4187. }
  4188. dw->server->lstrp = jiffies;
  4189. mid = smb2_find_dequeue_mid(dw->server, dw->buf);
  4190. if (mid == NULL)
  4191. cifs_dbg(FYI, "mid not found\n");
  4192. else {
  4193. mid->decrypted = true;
  4194. rc = handle_read_data(dw->server, mid, dw->buf,
  4195. dw->server->vals->read_rsp_size,
  4196. dw->buffer, dw->len,
  4197. true);
  4198. if (rc >= 0) {
  4199. #ifdef CONFIG_CIFS_STATS2
  4200. mid->when_received = jiffies;
  4201. #endif
  4202. if (dw->server->ops->is_network_name_deleted)
  4203. dw->server->ops->is_network_name_deleted(dw->buf,
  4204. dw->server);
  4205. mid->callback(mid);
  4206. } else {
  4207. spin_lock(&dw->server->srv_lock);
  4208. if (dw->server->tcpStatus == CifsNeedReconnect) {
  4209. spin_lock(&dw->server->mid_lock);
  4210. mid->mid_state = MID_RETRY_NEEDED;
  4211. spin_unlock(&dw->server->mid_lock);
  4212. spin_unlock(&dw->server->srv_lock);
  4213. mid->callback(mid);
  4214. } else {
  4215. spin_lock(&dw->server->mid_lock);
  4216. mid->mid_state = MID_REQUEST_SUBMITTED;
  4217. mid->mid_flags &= ~(MID_DELETED);
  4218. list_add_tail(&mid->qhead,
  4219. &dw->server->pending_mid_q);
  4220. spin_unlock(&dw->server->mid_lock);
  4221. spin_unlock(&dw->server->srv_lock);
  4222. }
  4223. }
  4224. release_mid(mid);
  4225. }
  4226. free_pages:
  4227. cifs_clear_folioq_buffer(dw->buffer);
  4228. cifs_small_buf_release(dw->buf);
  4229. kfree(dw);
  4230. }
  4231. static int
  4232. receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
  4233. int *num_mids)
  4234. {
  4235. char *buf = server->smallbuf;
  4236. struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
  4237. struct iov_iter iter;
  4238. unsigned int len;
  4239. unsigned int buflen = server->pdu_size;
  4240. int rc;
  4241. struct smb2_decrypt_work *dw;
  4242. dw = kzalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
  4243. if (!dw)
  4244. return -ENOMEM;
  4245. INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
  4246. dw->server = server;
  4247. *num_mids = 1;
  4248. len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
  4249. sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
  4250. rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
  4251. if (rc < 0)
  4252. goto free_dw;
  4253. server->total_read += rc;
  4254. len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
  4255. server->vals->read_rsp_size;
  4256. dw->len = len;
  4257. len = round_up(dw->len, PAGE_SIZE);
  4258. rc = -ENOMEM;
  4259. dw->buffer = cifs_alloc_folioq_buffer(len);
  4260. if (!dw->buffer)
  4261. goto discard_data;
  4262. iov_iter_folio_queue(&iter, ITER_DEST, dw->buffer, 0, 0, len);
  4263. /* Read the data into the buffer and clear excess bufferage. */
  4264. rc = cifs_read_iter_from_socket(server, &iter, dw->len);
  4265. if (rc < 0)
  4266. goto discard_data;
  4267. server->total_read += rc;
  4268. if (rc < len) {
  4269. struct iov_iter tmp = iter;
  4270. iov_iter_advance(&tmp, rc);
  4271. iov_iter_zero(len - rc, &tmp);
  4272. }
  4273. iov_iter_truncate(&iter, dw->len);
  4274. rc = cifs_discard_remaining_data(server);
  4275. if (rc)
  4276. goto free_pages;
  4277. /*
  4278. * For large reads, offload to different thread for better performance,
  4279. * use more cores decrypting which can be expensive
  4280. */
  4281. if ((server->min_offload) && (server->in_flight > 1) &&
  4282. (server->pdu_size >= server->min_offload)) {
  4283. dw->buf = server->smallbuf;
  4284. server->smallbuf = (char *)cifs_small_buf_get();
  4285. queue_work(decrypt_wq, &dw->decrypt);
  4286. *num_mids = 0; /* worker thread takes care of finding mid */
  4287. return -1;
  4288. }
  4289. rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
  4290. &iter, false);
  4291. if (rc)
  4292. goto free_pages;
  4293. *mid = smb2_find_mid(server, buf);
  4294. if (*mid == NULL) {
  4295. cifs_dbg(FYI, "mid not found\n");
  4296. } else {
  4297. cifs_dbg(FYI, "mid found\n");
  4298. (*mid)->decrypted = true;
  4299. rc = handle_read_data(server, *mid, buf,
  4300. server->vals->read_rsp_size,
  4301. dw->buffer, dw->len, false);
  4302. if (rc >= 0) {
  4303. if (server->ops->is_network_name_deleted) {
  4304. server->ops->is_network_name_deleted(buf,
  4305. server);
  4306. }
  4307. }
  4308. }
  4309. free_pages:
  4310. cifs_clear_folioq_buffer(dw->buffer);
  4311. free_dw:
  4312. kfree(dw);
  4313. return rc;
  4314. discard_data:
  4315. cifs_discard_remaining_data(server);
  4316. goto free_pages;
  4317. }
  4318. static int
  4319. receive_encrypted_standard(struct TCP_Server_Info *server,
  4320. struct mid_q_entry **mids, char **bufs,
  4321. int *num_mids)
  4322. {
  4323. int ret, length;
  4324. char *buf = server->smallbuf;
  4325. struct smb2_hdr *shdr;
  4326. unsigned int pdu_length = server->pdu_size;
  4327. unsigned int buf_size;
  4328. unsigned int next_cmd;
  4329. struct mid_q_entry *mid_entry;
  4330. int next_is_large;
  4331. char *next_buffer = NULL;
  4332. *num_mids = 0;
  4333. /* switch to large buffer if too big for a small one */
  4334. if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
  4335. server->large_buf = true;
  4336. memcpy(server->bigbuf, buf, server->total_read);
  4337. buf = server->bigbuf;
  4338. }
  4339. /* now read the rest */
  4340. length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
  4341. pdu_length - HEADER_SIZE(server) + 1);
  4342. if (length < 0)
  4343. return length;
  4344. server->total_read += length;
  4345. buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
  4346. length = decrypt_raw_data(server, buf, buf_size, NULL, false);
  4347. if (length)
  4348. return length;
  4349. next_is_large = server->large_buf;
  4350. one_more:
  4351. shdr = (struct smb2_hdr *)buf;
  4352. next_cmd = le32_to_cpu(shdr->NextCommand);
  4353. if (next_cmd) {
  4354. if (WARN_ON_ONCE(next_cmd > pdu_length))
  4355. return -1;
  4356. if (next_is_large)
  4357. next_buffer = (char *)cifs_buf_get();
  4358. else
  4359. next_buffer = (char *)cifs_small_buf_get();
  4360. if (!next_buffer) {
  4361. cifs_server_dbg(VFS, "No memory for (large) SMB response\n");
  4362. return -1;
  4363. }
  4364. memcpy(next_buffer, buf + next_cmd, pdu_length - next_cmd);
  4365. }
  4366. mid_entry = smb2_find_mid(server, buf);
  4367. if (mid_entry == NULL)
  4368. cifs_dbg(FYI, "mid not found\n");
  4369. else {
  4370. cifs_dbg(FYI, "mid found\n");
  4371. mid_entry->decrypted = true;
  4372. mid_entry->resp_buf_size = server->pdu_size;
  4373. }
  4374. if (*num_mids >= MAX_COMPOUND) {
  4375. cifs_server_dbg(VFS, "too many PDUs in compound\n");
  4376. return -1;
  4377. }
  4378. bufs[*num_mids] = buf;
  4379. mids[(*num_mids)++] = mid_entry;
  4380. if (mid_entry && mid_entry->handle)
  4381. ret = mid_entry->handle(server, mid_entry);
  4382. else
  4383. ret = cifs_handle_standard(server, mid_entry);
  4384. if (ret == 0 && next_cmd) {
  4385. pdu_length -= next_cmd;
  4386. server->large_buf = next_is_large;
  4387. if (next_is_large)
  4388. server->bigbuf = buf = next_buffer;
  4389. else
  4390. server->smallbuf = buf = next_buffer;
  4391. goto one_more;
  4392. } else if (ret != 0) {
  4393. /*
  4394. * ret != 0 here means that we didn't get to handle_mid() thus
  4395. * server->smallbuf and server->bigbuf are still valid. We need
  4396. * to free next_buffer because it is not going to be used
  4397. * anywhere.
  4398. */
  4399. if (next_is_large)
  4400. free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
  4401. else
  4402. free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
  4403. }
  4404. return ret;
  4405. }
  4406. static int
  4407. smb3_receive_transform(struct TCP_Server_Info *server,
  4408. struct mid_q_entry **mids, char **bufs, int *num_mids)
  4409. {
  4410. char *buf = server->smallbuf;
  4411. unsigned int pdu_length = server->pdu_size;
  4412. struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
  4413. unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
  4414. if (pdu_length < sizeof(struct smb2_transform_hdr) +
  4415. sizeof(struct smb2_hdr)) {
  4416. cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
  4417. pdu_length);
  4418. cifs_reconnect(server, true);
  4419. return -ECONNABORTED;
  4420. }
  4421. if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
  4422. cifs_server_dbg(VFS, "Transform message is broken\n");
  4423. cifs_reconnect(server, true);
  4424. return -ECONNABORTED;
  4425. }
  4426. /* TODO: add support for compounds containing READ. */
  4427. if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
  4428. return receive_encrypted_read(server, &mids[0], num_mids);
  4429. }
  4430. return receive_encrypted_standard(server, mids, bufs, num_mids);
  4431. }
  4432. int
  4433. smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  4434. {
  4435. char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
  4436. return handle_read_data(server, mid, buf, server->pdu_size,
  4437. NULL, 0, false);
  4438. }
  4439. static int smb2_next_header(struct TCP_Server_Info *server, char *buf,
  4440. unsigned int *noff)
  4441. {
  4442. struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
  4443. struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
  4444. if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
  4445. *noff = le32_to_cpu(t_hdr->OriginalMessageSize);
  4446. if (unlikely(check_add_overflow(*noff, sizeof(*t_hdr), noff)))
  4447. return -EINVAL;
  4448. } else {
  4449. *noff = le32_to_cpu(hdr->NextCommand);
  4450. }
  4451. if (unlikely(*noff && *noff < MID_HEADER_SIZE(server)))
  4452. return -EINVAL;
  4453. return 0;
  4454. }
  4455. int __cifs_sfu_make_node(unsigned int xid, struct inode *inode,
  4456. struct dentry *dentry, struct cifs_tcon *tcon,
  4457. const char *full_path, umode_t mode, dev_t dev,
  4458. const char *symname)
  4459. {
  4460. struct TCP_Server_Info *server = tcon->ses->server;
  4461. struct cifs_open_parms oparms;
  4462. struct cifs_open_info_data idata;
  4463. struct cifs_io_parms io_parms = {};
  4464. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  4465. struct cifs_fid fid;
  4466. unsigned int bytes_written;
  4467. u8 type[8];
  4468. int type_len = 0;
  4469. struct {
  4470. __le64 major;
  4471. __le64 minor;
  4472. } __packed pdev = {};
  4473. __le16 *symname_utf16 = NULL;
  4474. u8 *data = NULL;
  4475. int data_len = 0;
  4476. struct kvec iov[3];
  4477. __u32 oplock = server->oplocks ? REQ_OPLOCK : 0;
  4478. int rc;
  4479. switch (mode & S_IFMT) {
  4480. case S_IFCHR:
  4481. type_len = 8;
  4482. memcpy(type, "IntxCHR\0", type_len);
  4483. pdev.major = cpu_to_le64(MAJOR(dev));
  4484. pdev.minor = cpu_to_le64(MINOR(dev));
  4485. data = (u8 *)&pdev;
  4486. data_len = sizeof(pdev);
  4487. break;
  4488. case S_IFBLK:
  4489. type_len = 8;
  4490. memcpy(type, "IntxBLK\0", type_len);
  4491. pdev.major = cpu_to_le64(MAJOR(dev));
  4492. pdev.minor = cpu_to_le64(MINOR(dev));
  4493. data = (u8 *)&pdev;
  4494. data_len = sizeof(pdev);
  4495. break;
  4496. case S_IFLNK:
  4497. type_len = 8;
  4498. memcpy(type, "IntxLNK\1", type_len);
  4499. symname_utf16 = cifs_strndup_to_utf16(symname, strlen(symname),
  4500. &data_len, cifs_sb->local_nls,
  4501. NO_MAP_UNI_RSVD);
  4502. if (!symname_utf16) {
  4503. rc = -ENOMEM;
  4504. goto out;
  4505. }
  4506. data_len -= 2; /* symlink is without trailing wide-nul */
  4507. data = (u8 *)symname_utf16;
  4508. break;
  4509. case S_IFSOCK:
  4510. type_len = 8;
  4511. strscpy(type, "LnxSOCK");
  4512. data = (u8 *)&pdev;
  4513. data_len = sizeof(pdev);
  4514. break;
  4515. case S_IFIFO:
  4516. type_len = 8;
  4517. strscpy(type, "LnxFIFO");
  4518. data = (u8 *)&pdev;
  4519. data_len = sizeof(pdev);
  4520. break;
  4521. default:
  4522. rc = -EPERM;
  4523. goto out;
  4524. }
  4525. oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, GENERIC_WRITE,
  4526. FILE_CREATE, CREATE_NOT_DIR |
  4527. CREATE_OPTION_SPECIAL, ACL_NO_MODE);
  4528. oparms.fid = &fid;
  4529. idata.contains_posix_file_info = false;
  4530. rc = server->ops->open(xid, &oparms, &oplock, &idata);
  4531. if (rc)
  4532. goto out;
  4533. /*
  4534. * Check if the server honored ATTR_SYSTEM flag by CREATE_OPTION_SPECIAL
  4535. * option. If not then server does not support ATTR_SYSTEM and newly
  4536. * created file is not SFU compatible, which means that the call failed.
  4537. */
  4538. if (!(le32_to_cpu(idata.fi.Attributes) & ATTR_SYSTEM)) {
  4539. rc = -EOPNOTSUPP;
  4540. goto out_close;
  4541. }
  4542. if (type_len + data_len > 0) {
  4543. io_parms.pid = current->tgid;
  4544. io_parms.tcon = tcon;
  4545. io_parms.length = type_len + data_len;
  4546. iov[1].iov_base = type;
  4547. iov[1].iov_len = type_len;
  4548. iov[2].iov_base = data;
  4549. iov[2].iov_len = data_len;
  4550. rc = server->ops->sync_write(xid, &fid, &io_parms,
  4551. &bytes_written,
  4552. iov, ARRAY_SIZE(iov)-1);
  4553. }
  4554. out_close:
  4555. server->ops->close(xid, tcon, &fid);
  4556. /*
  4557. * If CREATE was successful but either setting ATTR_SYSTEM failed or
  4558. * writing type/data information failed then remove the intermediate
  4559. * object created by CREATE. Otherwise intermediate empty object stay
  4560. * on the server.
  4561. */
  4562. if (rc)
  4563. server->ops->unlink(xid, tcon, full_path, cifs_sb, NULL);
  4564. out:
  4565. kfree(symname_utf16);
  4566. return rc;
  4567. }
  4568. int cifs_sfu_make_node(unsigned int xid, struct inode *inode,
  4569. struct dentry *dentry, struct cifs_tcon *tcon,
  4570. const char *full_path, umode_t mode, dev_t dev)
  4571. {
  4572. struct inode *new = NULL;
  4573. int rc;
  4574. rc = __cifs_sfu_make_node(xid, inode, dentry, tcon,
  4575. full_path, mode, dev, NULL);
  4576. if (rc)
  4577. return rc;
  4578. if (tcon->posix_extensions) {
  4579. rc = smb311_posix_get_inode_info(&new, full_path, NULL,
  4580. inode->i_sb, xid);
  4581. } else if (tcon->unix_ext) {
  4582. rc = cifs_get_inode_info_unix(&new, full_path,
  4583. inode->i_sb, xid);
  4584. } else {
  4585. rc = cifs_get_inode_info(&new, full_path, NULL,
  4586. inode->i_sb, xid, NULL);
  4587. }
  4588. if (!rc)
  4589. d_instantiate(dentry, new);
  4590. return rc;
  4591. }
  4592. static int smb2_make_node(unsigned int xid, struct inode *inode,
  4593. struct dentry *dentry, struct cifs_tcon *tcon,
  4594. const char *full_path, umode_t mode, dev_t dev)
  4595. {
  4596. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  4597. int rc;
  4598. /*
  4599. * Check if mounted with mount parm 'sfu' mount parm.
  4600. * SFU emulation should work with all servers, but only
  4601. * supports block and char device, socket & fifo,
  4602. * and was used by default in earlier versions of Windows
  4603. */
  4604. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  4605. rc = cifs_sfu_make_node(xid, inode, dentry, tcon,
  4606. full_path, mode, dev);
  4607. } else {
  4608. rc = smb2_mknod_reparse(xid, inode, dentry, tcon,
  4609. full_path, mode, dev);
  4610. }
  4611. return rc;
  4612. }
  4613. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  4614. struct smb_version_operations smb20_operations = {
  4615. .compare_fids = smb2_compare_fids,
  4616. .setup_request = smb2_setup_request,
  4617. .setup_async_request = smb2_setup_async_request,
  4618. .check_receive = smb2_check_receive,
  4619. .add_credits = smb2_add_credits,
  4620. .set_credits = smb2_set_credits,
  4621. .get_credits_field = smb2_get_credits_field,
  4622. .get_credits = smb2_get_credits,
  4623. .wait_mtu_credits = cifs_wait_mtu_credits,
  4624. .get_next_mid = smb2_get_next_mid,
  4625. .revert_current_mid = smb2_revert_current_mid,
  4626. .read_data_offset = smb2_read_data_offset,
  4627. .read_data_length = smb2_read_data_length,
  4628. .map_error = map_smb2_to_linux_error,
  4629. .find_mid = smb2_find_mid,
  4630. .check_message = smb2_check_message,
  4631. .dump_detail = smb2_dump_detail,
  4632. .clear_stats = smb2_clear_stats,
  4633. .print_stats = smb2_print_stats,
  4634. .is_oplock_break = smb2_is_valid_oplock_break,
  4635. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  4636. .downgrade_oplock = smb2_downgrade_oplock,
  4637. .need_neg = smb2_need_neg,
  4638. .negotiate = smb2_negotiate,
  4639. .negotiate_wsize = smb2_negotiate_wsize,
  4640. .negotiate_rsize = smb2_negotiate_rsize,
  4641. .sess_setup = SMB2_sess_setup,
  4642. .logoff = SMB2_logoff,
  4643. .tree_connect = SMB2_tcon,
  4644. .tree_disconnect = SMB2_tdis,
  4645. .qfs_tcon = smb2_qfs_tcon,
  4646. .is_path_accessible = smb2_is_path_accessible,
  4647. .can_echo = smb2_can_echo,
  4648. .echo = SMB2_echo,
  4649. .query_path_info = smb2_query_path_info,
  4650. .query_reparse_point = smb2_query_reparse_point,
  4651. .get_srv_inum = smb2_get_srv_inum,
  4652. .query_file_info = smb2_query_file_info,
  4653. .set_path_size = smb2_set_path_size,
  4654. .set_file_size = smb2_set_file_size,
  4655. .set_file_info = smb2_set_file_info,
  4656. .set_compression = smb2_set_compression,
  4657. .mkdir = smb2_mkdir,
  4658. .mkdir_setinfo = smb2_mkdir_setinfo,
  4659. .rmdir = smb2_rmdir,
  4660. .unlink = smb2_unlink,
  4661. .rename = smb2_rename_path,
  4662. .create_hardlink = smb2_create_hardlink,
  4663. .parse_reparse_point = smb2_parse_reparse_point,
  4664. .query_mf_symlink = smb3_query_mf_symlink,
  4665. .create_mf_symlink = smb3_create_mf_symlink,
  4666. .create_reparse_symlink = smb2_create_reparse_symlink,
  4667. .open = smb2_open_file,
  4668. .set_fid = smb2_set_fid,
  4669. .close = smb2_close_file,
  4670. .flush = smb2_flush_file,
  4671. .async_readv = smb2_async_readv,
  4672. .async_writev = smb2_async_writev,
  4673. .sync_read = smb2_sync_read,
  4674. .sync_write = smb2_sync_write,
  4675. .query_dir_first = smb2_query_dir_first,
  4676. .query_dir_next = smb2_query_dir_next,
  4677. .close_dir = smb2_close_dir,
  4678. .calc_smb_size = smb2_calc_size,
  4679. .is_status_pending = smb2_is_status_pending,
  4680. .is_session_expired = smb2_is_session_expired,
  4681. .oplock_response = smb2_oplock_response,
  4682. .queryfs = smb2_queryfs,
  4683. .mand_lock = smb2_mand_lock,
  4684. .mand_unlock_range = smb2_unlock_range,
  4685. .push_mand_locks = smb2_push_mandatory_locks,
  4686. .get_lease_key = smb2_get_lease_key,
  4687. .set_lease_key = smb2_set_lease_key,
  4688. .new_lease_key = smb2_new_lease_key,
  4689. .calc_signature = smb2_calc_signature,
  4690. .is_read_op = smb2_is_read_op,
  4691. .set_oplock_level = smb2_set_oplock_level,
  4692. .create_lease_buf = smb2_create_lease_buf,
  4693. .parse_lease_buf = smb2_parse_lease_buf,
  4694. .copychunk_range = smb2_copychunk_range,
  4695. .wp_retry_size = smb2_wp_retry_size,
  4696. .dir_needs_close = smb2_dir_needs_close,
  4697. .get_dfs_refer = smb2_get_dfs_refer,
  4698. .select_sectype = smb2_select_sectype,
  4699. #ifdef CONFIG_CIFS_XATTR
  4700. .query_all_EAs = smb2_query_eas,
  4701. .set_EA = smb2_set_ea,
  4702. #endif /* CIFS_XATTR */
  4703. .get_acl = get_smb2_acl,
  4704. .get_acl_by_fid = get_smb2_acl_by_fid,
  4705. .set_acl = set_smb2_acl,
  4706. .next_header = smb2_next_header,
  4707. .ioctl_query_info = smb2_ioctl_query_info,
  4708. .make_node = smb2_make_node,
  4709. .fiemap = smb3_fiemap,
  4710. .llseek = smb3_llseek,
  4711. .is_status_io_timeout = smb2_is_status_io_timeout,
  4712. .is_network_name_deleted = smb2_is_network_name_deleted,
  4713. };
  4714. #endif /* CIFS_ALLOW_INSECURE_LEGACY */
  4715. struct smb_version_operations smb21_operations = {
  4716. .compare_fids = smb2_compare_fids,
  4717. .setup_request = smb2_setup_request,
  4718. .setup_async_request = smb2_setup_async_request,
  4719. .check_receive = smb2_check_receive,
  4720. .add_credits = smb2_add_credits,
  4721. .set_credits = smb2_set_credits,
  4722. .get_credits_field = smb2_get_credits_field,
  4723. .get_credits = smb2_get_credits,
  4724. .wait_mtu_credits = smb2_wait_mtu_credits,
  4725. .adjust_credits = smb2_adjust_credits,
  4726. .get_next_mid = smb2_get_next_mid,
  4727. .revert_current_mid = smb2_revert_current_mid,
  4728. .read_data_offset = smb2_read_data_offset,
  4729. .read_data_length = smb2_read_data_length,
  4730. .map_error = map_smb2_to_linux_error,
  4731. .find_mid = smb2_find_mid,
  4732. .check_message = smb2_check_message,
  4733. .dump_detail = smb2_dump_detail,
  4734. .clear_stats = smb2_clear_stats,
  4735. .print_stats = smb2_print_stats,
  4736. .is_oplock_break = smb2_is_valid_oplock_break,
  4737. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  4738. .downgrade_oplock = smb2_downgrade_oplock,
  4739. .need_neg = smb2_need_neg,
  4740. .negotiate = smb2_negotiate,
  4741. .negotiate_wsize = smb2_negotiate_wsize,
  4742. .negotiate_rsize = smb2_negotiate_rsize,
  4743. .sess_setup = SMB2_sess_setup,
  4744. .logoff = SMB2_logoff,
  4745. .tree_connect = SMB2_tcon,
  4746. .tree_disconnect = SMB2_tdis,
  4747. .qfs_tcon = smb2_qfs_tcon,
  4748. .is_path_accessible = smb2_is_path_accessible,
  4749. .can_echo = smb2_can_echo,
  4750. .echo = SMB2_echo,
  4751. .query_path_info = smb2_query_path_info,
  4752. .query_reparse_point = smb2_query_reparse_point,
  4753. .get_srv_inum = smb2_get_srv_inum,
  4754. .query_file_info = smb2_query_file_info,
  4755. .set_path_size = smb2_set_path_size,
  4756. .set_file_size = smb2_set_file_size,
  4757. .set_file_info = smb2_set_file_info,
  4758. .set_compression = smb2_set_compression,
  4759. .mkdir = smb2_mkdir,
  4760. .mkdir_setinfo = smb2_mkdir_setinfo,
  4761. .rmdir = smb2_rmdir,
  4762. .unlink = smb2_unlink,
  4763. .rename = smb2_rename_path,
  4764. .create_hardlink = smb2_create_hardlink,
  4765. .parse_reparse_point = smb2_parse_reparse_point,
  4766. .query_mf_symlink = smb3_query_mf_symlink,
  4767. .create_mf_symlink = smb3_create_mf_symlink,
  4768. .create_reparse_symlink = smb2_create_reparse_symlink,
  4769. .open = smb2_open_file,
  4770. .set_fid = smb2_set_fid,
  4771. .close = smb2_close_file,
  4772. .flush = smb2_flush_file,
  4773. .async_readv = smb2_async_readv,
  4774. .async_writev = smb2_async_writev,
  4775. .sync_read = smb2_sync_read,
  4776. .sync_write = smb2_sync_write,
  4777. .query_dir_first = smb2_query_dir_first,
  4778. .query_dir_next = smb2_query_dir_next,
  4779. .close_dir = smb2_close_dir,
  4780. .calc_smb_size = smb2_calc_size,
  4781. .is_status_pending = smb2_is_status_pending,
  4782. .is_session_expired = smb2_is_session_expired,
  4783. .oplock_response = smb2_oplock_response,
  4784. .queryfs = smb2_queryfs,
  4785. .mand_lock = smb2_mand_lock,
  4786. .mand_unlock_range = smb2_unlock_range,
  4787. .push_mand_locks = smb2_push_mandatory_locks,
  4788. .get_lease_key = smb2_get_lease_key,
  4789. .set_lease_key = smb2_set_lease_key,
  4790. .new_lease_key = smb2_new_lease_key,
  4791. .calc_signature = smb2_calc_signature,
  4792. .is_read_op = smb21_is_read_op,
  4793. .set_oplock_level = smb21_set_oplock_level,
  4794. .create_lease_buf = smb2_create_lease_buf,
  4795. .parse_lease_buf = smb2_parse_lease_buf,
  4796. .copychunk_range = smb2_copychunk_range,
  4797. .wp_retry_size = smb2_wp_retry_size,
  4798. .dir_needs_close = smb2_dir_needs_close,
  4799. .enum_snapshots = smb3_enum_snapshots,
  4800. .notify = smb3_notify,
  4801. .get_dfs_refer = smb2_get_dfs_refer,
  4802. .select_sectype = smb2_select_sectype,
  4803. #ifdef CONFIG_CIFS_XATTR
  4804. .query_all_EAs = smb2_query_eas,
  4805. .set_EA = smb2_set_ea,
  4806. #endif /* CIFS_XATTR */
  4807. .get_acl = get_smb2_acl,
  4808. .get_acl_by_fid = get_smb2_acl_by_fid,
  4809. .set_acl = set_smb2_acl,
  4810. .next_header = smb2_next_header,
  4811. .ioctl_query_info = smb2_ioctl_query_info,
  4812. .make_node = smb2_make_node,
  4813. .fiemap = smb3_fiemap,
  4814. .llseek = smb3_llseek,
  4815. .is_status_io_timeout = smb2_is_status_io_timeout,
  4816. .is_network_name_deleted = smb2_is_network_name_deleted,
  4817. };
  4818. struct smb_version_operations smb30_operations = {
  4819. .compare_fids = smb2_compare_fids,
  4820. .setup_request = smb2_setup_request,
  4821. .setup_async_request = smb2_setup_async_request,
  4822. .check_receive = smb2_check_receive,
  4823. .add_credits = smb2_add_credits,
  4824. .set_credits = smb2_set_credits,
  4825. .get_credits_field = smb2_get_credits_field,
  4826. .get_credits = smb2_get_credits,
  4827. .wait_mtu_credits = smb2_wait_mtu_credits,
  4828. .adjust_credits = smb2_adjust_credits,
  4829. .get_next_mid = smb2_get_next_mid,
  4830. .revert_current_mid = smb2_revert_current_mid,
  4831. .read_data_offset = smb2_read_data_offset,
  4832. .read_data_length = smb2_read_data_length,
  4833. .map_error = map_smb2_to_linux_error,
  4834. .find_mid = smb2_find_mid,
  4835. .check_message = smb2_check_message,
  4836. .dump_detail = smb2_dump_detail,
  4837. .clear_stats = smb2_clear_stats,
  4838. .print_stats = smb2_print_stats,
  4839. .dump_share_caps = smb2_dump_share_caps,
  4840. .is_oplock_break = smb2_is_valid_oplock_break,
  4841. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  4842. .downgrade_oplock = smb3_downgrade_oplock,
  4843. .need_neg = smb2_need_neg,
  4844. .negotiate = smb2_negotiate,
  4845. .negotiate_wsize = smb3_negotiate_wsize,
  4846. .negotiate_rsize = smb3_negotiate_rsize,
  4847. .sess_setup = SMB2_sess_setup,
  4848. .logoff = SMB2_logoff,
  4849. .tree_connect = SMB2_tcon,
  4850. .tree_disconnect = SMB2_tdis,
  4851. .qfs_tcon = smb3_qfs_tcon,
  4852. .query_server_interfaces = SMB3_request_interfaces,
  4853. .is_path_accessible = smb2_is_path_accessible,
  4854. .can_echo = smb2_can_echo,
  4855. .echo = SMB2_echo,
  4856. .query_path_info = smb2_query_path_info,
  4857. /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
  4858. .query_reparse_point = smb2_query_reparse_point,
  4859. .get_srv_inum = smb2_get_srv_inum,
  4860. .query_file_info = smb2_query_file_info,
  4861. .set_path_size = smb2_set_path_size,
  4862. .set_file_size = smb2_set_file_size,
  4863. .set_file_info = smb2_set_file_info,
  4864. .set_compression = smb2_set_compression,
  4865. .mkdir = smb2_mkdir,
  4866. .mkdir_setinfo = smb2_mkdir_setinfo,
  4867. .rmdir = smb2_rmdir,
  4868. .unlink = smb2_unlink,
  4869. .rename = smb2_rename_path,
  4870. .create_hardlink = smb2_create_hardlink,
  4871. .parse_reparse_point = smb2_parse_reparse_point,
  4872. .query_mf_symlink = smb3_query_mf_symlink,
  4873. .create_mf_symlink = smb3_create_mf_symlink,
  4874. .create_reparse_symlink = smb2_create_reparse_symlink,
  4875. .open = smb2_open_file,
  4876. .set_fid = smb2_set_fid,
  4877. .close = smb2_close_file,
  4878. .close_getattr = smb2_close_getattr,
  4879. .flush = smb2_flush_file,
  4880. .async_readv = smb2_async_readv,
  4881. .async_writev = smb2_async_writev,
  4882. .sync_read = smb2_sync_read,
  4883. .sync_write = smb2_sync_write,
  4884. .query_dir_first = smb2_query_dir_first,
  4885. .query_dir_next = smb2_query_dir_next,
  4886. .close_dir = smb2_close_dir,
  4887. .calc_smb_size = smb2_calc_size,
  4888. .is_status_pending = smb2_is_status_pending,
  4889. .is_session_expired = smb2_is_session_expired,
  4890. .oplock_response = smb2_oplock_response,
  4891. .queryfs = smb2_queryfs,
  4892. .mand_lock = smb2_mand_lock,
  4893. .mand_unlock_range = smb2_unlock_range,
  4894. .push_mand_locks = smb2_push_mandatory_locks,
  4895. .get_lease_key = smb2_get_lease_key,
  4896. .set_lease_key = smb2_set_lease_key,
  4897. .new_lease_key = smb2_new_lease_key,
  4898. .generate_signingkey = generate_smb30signingkey,
  4899. .calc_signature = smb3_calc_signature,
  4900. .set_integrity = smb3_set_integrity,
  4901. .is_read_op = smb21_is_read_op,
  4902. .set_oplock_level = smb3_set_oplock_level,
  4903. .create_lease_buf = smb3_create_lease_buf,
  4904. .parse_lease_buf = smb3_parse_lease_buf,
  4905. .copychunk_range = smb2_copychunk_range,
  4906. .duplicate_extents = smb2_duplicate_extents,
  4907. .validate_negotiate = smb3_validate_negotiate,
  4908. .wp_retry_size = smb2_wp_retry_size,
  4909. .dir_needs_close = smb2_dir_needs_close,
  4910. .fallocate = smb3_fallocate,
  4911. .enum_snapshots = smb3_enum_snapshots,
  4912. .notify = smb3_notify,
  4913. .init_transform_rq = smb3_init_transform_rq,
  4914. .is_transform_hdr = smb3_is_transform_hdr,
  4915. .receive_transform = smb3_receive_transform,
  4916. .get_dfs_refer = smb2_get_dfs_refer,
  4917. .select_sectype = smb2_select_sectype,
  4918. #ifdef CONFIG_CIFS_XATTR
  4919. .query_all_EAs = smb2_query_eas,
  4920. .set_EA = smb2_set_ea,
  4921. #endif /* CIFS_XATTR */
  4922. .get_acl = get_smb2_acl,
  4923. .get_acl_by_fid = get_smb2_acl_by_fid,
  4924. .set_acl = set_smb2_acl,
  4925. .next_header = smb2_next_header,
  4926. .ioctl_query_info = smb2_ioctl_query_info,
  4927. .make_node = smb2_make_node,
  4928. .fiemap = smb3_fiemap,
  4929. .llseek = smb3_llseek,
  4930. .is_status_io_timeout = smb2_is_status_io_timeout,
  4931. .is_network_name_deleted = smb2_is_network_name_deleted,
  4932. };
  4933. struct smb_version_operations smb311_operations = {
  4934. .compare_fids = smb2_compare_fids,
  4935. .setup_request = smb2_setup_request,
  4936. .setup_async_request = smb2_setup_async_request,
  4937. .check_receive = smb2_check_receive,
  4938. .add_credits = smb2_add_credits,
  4939. .set_credits = smb2_set_credits,
  4940. .get_credits_field = smb2_get_credits_field,
  4941. .get_credits = smb2_get_credits,
  4942. .wait_mtu_credits = smb2_wait_mtu_credits,
  4943. .adjust_credits = smb2_adjust_credits,
  4944. .get_next_mid = smb2_get_next_mid,
  4945. .revert_current_mid = smb2_revert_current_mid,
  4946. .read_data_offset = smb2_read_data_offset,
  4947. .read_data_length = smb2_read_data_length,
  4948. .map_error = map_smb2_to_linux_error,
  4949. .find_mid = smb2_find_mid,
  4950. .check_message = smb2_check_message,
  4951. .dump_detail = smb2_dump_detail,
  4952. .clear_stats = smb2_clear_stats,
  4953. .print_stats = smb2_print_stats,
  4954. .dump_share_caps = smb2_dump_share_caps,
  4955. .is_oplock_break = smb2_is_valid_oplock_break,
  4956. .handle_cancelled_mid = smb2_handle_cancelled_mid,
  4957. .downgrade_oplock = smb3_downgrade_oplock,
  4958. .need_neg = smb2_need_neg,
  4959. .negotiate = smb2_negotiate,
  4960. .negotiate_wsize = smb3_negotiate_wsize,
  4961. .negotiate_rsize = smb3_negotiate_rsize,
  4962. .sess_setup = SMB2_sess_setup,
  4963. .logoff = SMB2_logoff,
  4964. .tree_connect = SMB2_tcon,
  4965. .tree_disconnect = SMB2_tdis,
  4966. .qfs_tcon = smb3_qfs_tcon,
  4967. .query_server_interfaces = SMB3_request_interfaces,
  4968. .is_path_accessible = smb2_is_path_accessible,
  4969. .can_echo = smb2_can_echo,
  4970. .echo = SMB2_echo,
  4971. .query_path_info = smb2_query_path_info,
  4972. .query_reparse_point = smb2_query_reparse_point,
  4973. .get_srv_inum = smb2_get_srv_inum,
  4974. .query_file_info = smb2_query_file_info,
  4975. .set_path_size = smb2_set_path_size,
  4976. .set_file_size = smb2_set_file_size,
  4977. .set_file_info = smb2_set_file_info,
  4978. .set_compression = smb2_set_compression,
  4979. .mkdir = smb2_mkdir,
  4980. .mkdir_setinfo = smb2_mkdir_setinfo,
  4981. .posix_mkdir = smb311_posix_mkdir,
  4982. .rmdir = smb2_rmdir,
  4983. .unlink = smb2_unlink,
  4984. .rename = smb2_rename_path,
  4985. .create_hardlink = smb2_create_hardlink,
  4986. .parse_reparse_point = smb2_parse_reparse_point,
  4987. .query_mf_symlink = smb3_query_mf_symlink,
  4988. .create_mf_symlink = smb3_create_mf_symlink,
  4989. .create_reparse_symlink = smb2_create_reparse_symlink,
  4990. .open = smb2_open_file,
  4991. .set_fid = smb2_set_fid,
  4992. .close = smb2_close_file,
  4993. .close_getattr = smb2_close_getattr,
  4994. .flush = smb2_flush_file,
  4995. .async_readv = smb2_async_readv,
  4996. .async_writev = smb2_async_writev,
  4997. .sync_read = smb2_sync_read,
  4998. .sync_write = smb2_sync_write,
  4999. .query_dir_first = smb2_query_dir_first,
  5000. .query_dir_next = smb2_query_dir_next,
  5001. .close_dir = smb2_close_dir,
  5002. .calc_smb_size = smb2_calc_size,
  5003. .is_status_pending = smb2_is_status_pending,
  5004. .is_session_expired = smb2_is_session_expired,
  5005. .oplock_response = smb2_oplock_response,
  5006. .queryfs = smb311_queryfs,
  5007. .mand_lock = smb2_mand_lock,
  5008. .mand_unlock_range = smb2_unlock_range,
  5009. .push_mand_locks = smb2_push_mandatory_locks,
  5010. .get_lease_key = smb2_get_lease_key,
  5011. .set_lease_key = smb2_set_lease_key,
  5012. .new_lease_key = smb2_new_lease_key,
  5013. .generate_signingkey = generate_smb311signingkey,
  5014. .calc_signature = smb3_calc_signature,
  5015. .set_integrity = smb3_set_integrity,
  5016. .is_read_op = smb21_is_read_op,
  5017. .set_oplock_level = smb3_set_oplock_level,
  5018. .create_lease_buf = smb3_create_lease_buf,
  5019. .parse_lease_buf = smb3_parse_lease_buf,
  5020. .copychunk_range = smb2_copychunk_range,
  5021. .duplicate_extents = smb2_duplicate_extents,
  5022. /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
  5023. .wp_retry_size = smb2_wp_retry_size,
  5024. .dir_needs_close = smb2_dir_needs_close,
  5025. .fallocate = smb3_fallocate,
  5026. .enum_snapshots = smb3_enum_snapshots,
  5027. .notify = smb3_notify,
  5028. .init_transform_rq = smb3_init_transform_rq,
  5029. .is_transform_hdr = smb3_is_transform_hdr,
  5030. .receive_transform = smb3_receive_transform,
  5031. .get_dfs_refer = smb2_get_dfs_refer,
  5032. .select_sectype = smb2_select_sectype,
  5033. #ifdef CONFIG_CIFS_XATTR
  5034. .query_all_EAs = smb2_query_eas,
  5035. .set_EA = smb2_set_ea,
  5036. #endif /* CIFS_XATTR */
  5037. .get_acl = get_smb2_acl,
  5038. .get_acl_by_fid = get_smb2_acl_by_fid,
  5039. .set_acl = set_smb2_acl,
  5040. .next_header = smb2_next_header,
  5041. .ioctl_query_info = smb2_ioctl_query_info,
  5042. .make_node = smb2_make_node,
  5043. .fiemap = smb3_fiemap,
  5044. .llseek = smb3_llseek,
  5045. .is_status_io_timeout = smb2_is_status_io_timeout,
  5046. .is_network_name_deleted = smb2_is_network_name_deleted,
  5047. };
  5048. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  5049. struct smb_version_values smb20_values = {
  5050. .version_string = SMB20_VERSION_STRING,
  5051. .protocol_id = SMB20_PROT_ID,
  5052. .req_capabilities = 0, /* MBZ */
  5053. .large_lock_type = 0,
  5054. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
  5055. .shared_lock_type = SMB2_LOCKFLAG_SHARED,
  5056. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  5057. .header_size = sizeof(struct smb2_hdr),
  5058. .header_preamble_size = 0,
  5059. .max_header_size = MAX_SMB2_HDR_SIZE,
  5060. .read_rsp_size = sizeof(struct smb2_read_rsp),
  5061. .lock_cmd = SMB2_LOCK,
  5062. .cap_unix = 0,
  5063. .cap_nt_find = SMB2_NT_FIND,
  5064. .cap_large_files = SMB2_LARGE_FILES,
  5065. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5066. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5067. .create_lease_size = sizeof(struct create_lease),
  5068. };
  5069. #endif /* ALLOW_INSECURE_LEGACY */
  5070. struct smb_version_values smb21_values = {
  5071. .version_string = SMB21_VERSION_STRING,
  5072. .protocol_id = SMB21_PROT_ID,
  5073. .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
  5074. .large_lock_type = 0,
  5075. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
  5076. .shared_lock_type = SMB2_LOCKFLAG_SHARED,
  5077. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  5078. .header_size = sizeof(struct smb2_hdr),
  5079. .header_preamble_size = 0,
  5080. .max_header_size = MAX_SMB2_HDR_SIZE,
  5081. .read_rsp_size = sizeof(struct smb2_read_rsp),
  5082. .lock_cmd = SMB2_LOCK,
  5083. .cap_unix = 0,
  5084. .cap_nt_find = SMB2_NT_FIND,
  5085. .cap_large_files = SMB2_LARGE_FILES,
  5086. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5087. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5088. .create_lease_size = sizeof(struct create_lease),
  5089. };
  5090. struct smb_version_values smb3any_values = {
  5091. .version_string = SMB3ANY_VERSION_STRING,
  5092. .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
  5093. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
  5094. .large_lock_type = 0,
  5095. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
  5096. .shared_lock_type = SMB2_LOCKFLAG_SHARED,
  5097. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  5098. .header_size = sizeof(struct smb2_hdr),
  5099. .header_preamble_size = 0,
  5100. .max_header_size = MAX_SMB2_HDR_SIZE,
  5101. .read_rsp_size = sizeof(struct smb2_read_rsp),
  5102. .lock_cmd = SMB2_LOCK,
  5103. .cap_unix = 0,
  5104. .cap_nt_find = SMB2_NT_FIND,
  5105. .cap_large_files = SMB2_LARGE_FILES,
  5106. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5107. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5108. .create_lease_size = sizeof(struct create_lease_v2),
  5109. };
  5110. struct smb_version_values smbdefault_values = {
  5111. .version_string = SMBDEFAULT_VERSION_STRING,
  5112. .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
  5113. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
  5114. .large_lock_type = 0,
  5115. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
  5116. .shared_lock_type = SMB2_LOCKFLAG_SHARED,
  5117. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  5118. .header_size = sizeof(struct smb2_hdr),
  5119. .header_preamble_size = 0,
  5120. .max_header_size = MAX_SMB2_HDR_SIZE,
  5121. .read_rsp_size = sizeof(struct smb2_read_rsp),
  5122. .lock_cmd = SMB2_LOCK,
  5123. .cap_unix = 0,
  5124. .cap_nt_find = SMB2_NT_FIND,
  5125. .cap_large_files = SMB2_LARGE_FILES,
  5126. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5127. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5128. .create_lease_size = sizeof(struct create_lease_v2),
  5129. };
  5130. struct smb_version_values smb30_values = {
  5131. .version_string = SMB30_VERSION_STRING,
  5132. .protocol_id = SMB30_PROT_ID,
  5133. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
  5134. .large_lock_type = 0,
  5135. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
  5136. .shared_lock_type = SMB2_LOCKFLAG_SHARED,
  5137. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  5138. .header_size = sizeof(struct smb2_hdr),
  5139. .header_preamble_size = 0,
  5140. .max_header_size = MAX_SMB2_HDR_SIZE,
  5141. .read_rsp_size = sizeof(struct smb2_read_rsp),
  5142. .lock_cmd = SMB2_LOCK,
  5143. .cap_unix = 0,
  5144. .cap_nt_find = SMB2_NT_FIND,
  5145. .cap_large_files = SMB2_LARGE_FILES,
  5146. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5147. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5148. .create_lease_size = sizeof(struct create_lease_v2),
  5149. };
  5150. struct smb_version_values smb302_values = {
  5151. .version_string = SMB302_VERSION_STRING,
  5152. .protocol_id = SMB302_PROT_ID,
  5153. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
  5154. .large_lock_type = 0,
  5155. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
  5156. .shared_lock_type = SMB2_LOCKFLAG_SHARED,
  5157. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  5158. .header_size = sizeof(struct smb2_hdr),
  5159. .header_preamble_size = 0,
  5160. .max_header_size = MAX_SMB2_HDR_SIZE,
  5161. .read_rsp_size = sizeof(struct smb2_read_rsp),
  5162. .lock_cmd = SMB2_LOCK,
  5163. .cap_unix = 0,
  5164. .cap_nt_find = SMB2_NT_FIND,
  5165. .cap_large_files = SMB2_LARGE_FILES,
  5166. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5167. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5168. .create_lease_size = sizeof(struct create_lease_v2),
  5169. };
  5170. struct smb_version_values smb311_values = {
  5171. .version_string = SMB311_VERSION_STRING,
  5172. .protocol_id = SMB311_PROT_ID,
  5173. .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
  5174. .large_lock_type = 0,
  5175. .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
  5176. .shared_lock_type = SMB2_LOCKFLAG_SHARED,
  5177. .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
  5178. .header_size = sizeof(struct smb2_hdr),
  5179. .header_preamble_size = 0,
  5180. .max_header_size = MAX_SMB2_HDR_SIZE,
  5181. .read_rsp_size = sizeof(struct smb2_read_rsp),
  5182. .lock_cmd = SMB2_LOCK,
  5183. .cap_unix = 0,
  5184. .cap_nt_find = SMB2_NT_FIND,
  5185. .cap_large_files = SMB2_LARGE_FILES,
  5186. .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5187. .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
  5188. .create_lease_size = sizeof(struct create_lease_v2),
  5189. };