smb2ops.c 163 KB

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