smb2ops.c 157 KB

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