cifssmb.c 192 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558
  1. /*
  2. * fs/cifs/cifssmb.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2010
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * Contains the routines for constructing the SMB PDUs themselves
  8. *
  9. * This library is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published
  11. * by the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  17. * the GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /* SMB/CIFS PDU handling routines here - except for leftovers in connect.c */
  24. /* These are mostly routines that operate on a pathname, or on a tree id */
  25. /* (mounted volume), but there are eight handle based routines which must be */
  26. /* treated slightly differently for reconnection purposes since we never */
  27. /* want to reuse a stale file handle and only the caller knows the file info */
  28. #include <linux/fs.h>
  29. #include <linux/kernel.h>
  30. #include <linux/vfs.h>
  31. #include <linux/slab.h>
  32. #include <linux/posix_acl_xattr.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/swap.h>
  35. #include <linux/task_io_accounting_ops.h>
  36. #include <asm/uaccess.h>
  37. #include "cifspdu.h"
  38. #include "cifsglob.h"
  39. #include "cifsacl.h"
  40. #include "cifsproto.h"
  41. #include "cifs_unicode.h"
  42. #include "cifs_debug.h"
  43. #include "fscache.h"
  44. #ifdef CONFIG_CIFS_POSIX
  45. static struct {
  46. int index;
  47. char *name;
  48. } protocols[] = {
  49. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  50. {LANMAN_PROT, "\2LM1.2X002"},
  51. {LANMAN2_PROT, "\2LANMAN2.1"},
  52. #endif /* weak password hashing for legacy clients */
  53. {CIFS_PROT, "\2NT LM 0.12"},
  54. {POSIX_PROT, "\2POSIX 2"},
  55. {BAD_PROT, "\2"}
  56. };
  57. #else
  58. static struct {
  59. int index;
  60. char *name;
  61. } protocols[] = {
  62. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  63. {LANMAN_PROT, "\2LM1.2X002"},
  64. {LANMAN2_PROT, "\2LANMAN2.1"},
  65. #endif /* weak password hashing for legacy clients */
  66. {CIFS_PROT, "\2NT LM 0.12"},
  67. {BAD_PROT, "\2"}
  68. };
  69. #endif
  70. /* define the number of elements in the cifs dialect array */
  71. #ifdef CONFIG_CIFS_POSIX
  72. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  73. #define CIFS_NUM_PROT 4
  74. #else
  75. #define CIFS_NUM_PROT 2
  76. #endif /* CIFS_WEAK_PW_HASH */
  77. #else /* not posix */
  78. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  79. #define CIFS_NUM_PROT 3
  80. #else
  81. #define CIFS_NUM_PROT 1
  82. #endif /* CONFIG_CIFS_WEAK_PW_HASH */
  83. #endif /* CIFS_POSIX */
  84. /*
  85. * Mark as invalid, all open files on tree connections since they
  86. * were closed when session to server was lost.
  87. */
  88. void
  89. cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
  90. {
  91. struct cifsFileInfo *open_file = NULL;
  92. struct list_head *tmp;
  93. struct list_head *tmp1;
  94. /* list all files open on tree connection and mark them invalid */
  95. spin_lock(&tcon->open_file_lock);
  96. list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
  97. open_file = list_entry(tmp, struct cifsFileInfo, tlist);
  98. open_file->invalidHandle = true;
  99. open_file->oplock_break_cancelled = true;
  100. }
  101. spin_unlock(&tcon->open_file_lock);
  102. /*
  103. * BB Add call to invalidate_inodes(sb) for all superblocks mounted
  104. * to this tcon.
  105. */
  106. }
  107. /* reconnect the socket, tcon, and smb session if needed */
  108. static int
  109. cifs_reconnect_tcon(struct cifs_tcon *tcon, int smb_command)
  110. {
  111. int rc;
  112. struct cifs_ses *ses;
  113. struct TCP_Server_Info *server;
  114. struct nls_table *nls_codepage;
  115. /*
  116. * SMBs NegProt, SessSetup, uLogoff do not have tcon yet so check for
  117. * tcp and smb session status done differently for those three - in the
  118. * calling routine
  119. */
  120. if (!tcon)
  121. return 0;
  122. ses = tcon->ses;
  123. server = ses->server;
  124. /*
  125. * only tree disconnect, open, and write, (and ulogoff which does not
  126. * have tcon) are allowed as we start force umount
  127. */
  128. if (tcon->tidStatus == CifsExiting) {
  129. if (smb_command != SMB_COM_WRITE_ANDX &&
  130. smb_command != SMB_COM_OPEN_ANDX &&
  131. smb_command != SMB_COM_TREE_DISCONNECT) {
  132. cifs_dbg(FYI, "can not send cmd %d while umounting\n",
  133. smb_command);
  134. return -ENODEV;
  135. }
  136. }
  137. /*
  138. * Give demultiplex thread up to 10 seconds to reconnect, should be
  139. * greater than cifs socket timeout which is 7 seconds
  140. */
  141. while (server->tcpStatus == CifsNeedReconnect) {
  142. rc = wait_event_interruptible_timeout(server->response_q,
  143. (server->tcpStatus != CifsNeedReconnect),
  144. 10 * HZ);
  145. if (rc < 0) {
  146. cifs_dbg(FYI, "%s: aborting reconnect due to a received"
  147. " signal by the process\n", __func__);
  148. return -ERESTARTSYS;
  149. }
  150. /* are we still trying to reconnect? */
  151. if (server->tcpStatus != CifsNeedReconnect)
  152. break;
  153. /*
  154. * on "soft" mounts we wait once. Hard mounts keep
  155. * retrying until process is killed or server comes
  156. * back on-line
  157. */
  158. if (!tcon->retry) {
  159. cifs_dbg(FYI, "gave up waiting on reconnect in smb_init\n");
  160. return -EHOSTDOWN;
  161. }
  162. }
  163. if (!ses->need_reconnect && !tcon->need_reconnect)
  164. return 0;
  165. nls_codepage = load_nls_default();
  166. /*
  167. * need to prevent multiple threads trying to simultaneously
  168. * reconnect the same SMB session
  169. */
  170. mutex_lock(&ses->session_mutex);
  171. rc = cifs_negotiate_protocol(0, ses);
  172. if (rc == 0 && ses->need_reconnect)
  173. rc = cifs_setup_session(0, ses, nls_codepage);
  174. /* do we need to reconnect tcon? */
  175. if (rc || !tcon->need_reconnect) {
  176. mutex_unlock(&ses->session_mutex);
  177. goto out;
  178. }
  179. cifs_mark_open_files_invalid(tcon);
  180. rc = CIFSTCon(0, ses, tcon->treeName, tcon, nls_codepage);
  181. mutex_unlock(&ses->session_mutex);
  182. cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
  183. if (rc)
  184. goto out;
  185. atomic_inc(&tconInfoReconnectCount);
  186. /* tell server Unix caps we support */
  187. if (ses->capabilities & CAP_UNIX)
  188. reset_cifs_unix_caps(0, tcon, NULL, NULL);
  189. /*
  190. * Removed call to reopen open files here. It is safer (and faster) to
  191. * reopen files one at a time as needed in read and write.
  192. *
  193. * FIXME: what about file locks? don't we need to reclaim them ASAP?
  194. */
  195. out:
  196. /*
  197. * Check if handle based operation so we know whether we can continue
  198. * or not without returning to caller to reset file handle
  199. */
  200. switch (smb_command) {
  201. case SMB_COM_READ_ANDX:
  202. case SMB_COM_WRITE_ANDX:
  203. case SMB_COM_CLOSE:
  204. case SMB_COM_FIND_CLOSE2:
  205. case SMB_COM_LOCKING_ANDX:
  206. rc = -EAGAIN;
  207. }
  208. unload_nls(nls_codepage);
  209. return rc;
  210. }
  211. /* Allocate and return pointer to an SMB request buffer, and set basic
  212. SMB information in the SMB header. If the return code is zero, this
  213. function must have filled in request_buf pointer */
  214. static int
  215. small_smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
  216. void **request_buf)
  217. {
  218. int rc;
  219. rc = cifs_reconnect_tcon(tcon, smb_command);
  220. if (rc)
  221. return rc;
  222. *request_buf = cifs_small_buf_get();
  223. if (*request_buf == NULL) {
  224. /* BB should we add a retry in here if not a writepage? */
  225. return -ENOMEM;
  226. }
  227. header_assemble((struct smb_hdr *) *request_buf, smb_command,
  228. tcon, wct);
  229. if (tcon != NULL)
  230. cifs_stats_inc(&tcon->num_smbs_sent);
  231. return 0;
  232. }
  233. int
  234. small_smb_init_no_tc(const int smb_command, const int wct,
  235. struct cifs_ses *ses, void **request_buf)
  236. {
  237. int rc;
  238. struct smb_hdr *buffer;
  239. rc = small_smb_init(smb_command, wct, NULL, request_buf);
  240. if (rc)
  241. return rc;
  242. buffer = (struct smb_hdr *)*request_buf;
  243. buffer->Mid = get_next_mid(ses->server);
  244. if (ses->capabilities & CAP_UNICODE)
  245. buffer->Flags2 |= SMBFLG2_UNICODE;
  246. if (ses->capabilities & CAP_STATUS32)
  247. buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  248. /* uid, tid can stay at zero as set in header assemble */
  249. /* BB add support for turning on the signing when
  250. this function is used after 1st of session setup requests */
  251. return rc;
  252. }
  253. /* If the return code is zero, this function must fill in request_buf pointer */
  254. static int
  255. __smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
  256. void **request_buf, void **response_buf)
  257. {
  258. *request_buf = cifs_buf_get();
  259. if (*request_buf == NULL) {
  260. /* BB should we add a retry in here if not a writepage? */
  261. return -ENOMEM;
  262. }
  263. /* Although the original thought was we needed the response buf for */
  264. /* potential retries of smb operations it turns out we can determine */
  265. /* from the mid flags when the request buffer can be resent without */
  266. /* having to use a second distinct buffer for the response */
  267. if (response_buf)
  268. *response_buf = *request_buf;
  269. header_assemble((struct smb_hdr *) *request_buf, smb_command, tcon,
  270. wct);
  271. if (tcon != NULL)
  272. cifs_stats_inc(&tcon->num_smbs_sent);
  273. return 0;
  274. }
  275. /* If the return code is zero, this function must fill in request_buf pointer */
  276. static int
  277. smb_init(int smb_command, int wct, struct cifs_tcon *tcon,
  278. void **request_buf, void **response_buf)
  279. {
  280. int rc;
  281. rc = cifs_reconnect_tcon(tcon, smb_command);
  282. if (rc)
  283. return rc;
  284. return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
  285. }
  286. static int
  287. smb_init_no_reconnect(int smb_command, int wct, struct cifs_tcon *tcon,
  288. void **request_buf, void **response_buf)
  289. {
  290. if (tcon->ses->need_reconnect || tcon->need_reconnect)
  291. return -EHOSTDOWN;
  292. return __smb_init(smb_command, wct, tcon, request_buf, response_buf);
  293. }
  294. static int validate_t2(struct smb_t2_rsp *pSMB)
  295. {
  296. unsigned int total_size;
  297. /* check for plausible wct */
  298. if (pSMB->hdr.WordCount < 10)
  299. goto vt2_err;
  300. /* check for parm and data offset going beyond end of smb */
  301. if (get_unaligned_le16(&pSMB->t2_rsp.ParameterOffset) > 1024 ||
  302. get_unaligned_le16(&pSMB->t2_rsp.DataOffset) > 1024)
  303. goto vt2_err;
  304. total_size = get_unaligned_le16(&pSMB->t2_rsp.ParameterCount);
  305. if (total_size >= 512)
  306. goto vt2_err;
  307. /* check that bcc is at least as big as parms + data, and that it is
  308. * less than negotiated smb buffer
  309. */
  310. total_size += get_unaligned_le16(&pSMB->t2_rsp.DataCount);
  311. if (total_size > get_bcc(&pSMB->hdr) ||
  312. total_size >= CIFSMaxBufSize + MAX_CIFS_HDR_SIZE)
  313. goto vt2_err;
  314. return 0;
  315. vt2_err:
  316. cifs_dump_mem("Invalid transact2 SMB: ", (char *)pSMB,
  317. sizeof(struct smb_t2_rsp) + 16);
  318. return -EINVAL;
  319. }
  320. static int
  321. decode_ext_sec_blob(struct cifs_ses *ses, NEGOTIATE_RSP *pSMBr)
  322. {
  323. int rc = 0;
  324. u16 count;
  325. char *guid = pSMBr->u.extended_response.GUID;
  326. struct TCP_Server_Info *server = ses->server;
  327. count = get_bcc(&pSMBr->hdr);
  328. if (count < SMB1_CLIENT_GUID_SIZE)
  329. return -EIO;
  330. spin_lock(&cifs_tcp_ses_lock);
  331. if (server->srv_count > 1) {
  332. spin_unlock(&cifs_tcp_ses_lock);
  333. if (memcmp(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE) != 0) {
  334. cifs_dbg(FYI, "server UID changed\n");
  335. memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
  336. }
  337. } else {
  338. spin_unlock(&cifs_tcp_ses_lock);
  339. memcpy(server->server_GUID, guid, SMB1_CLIENT_GUID_SIZE);
  340. }
  341. if (count == SMB1_CLIENT_GUID_SIZE) {
  342. server->sec_ntlmssp = true;
  343. } else {
  344. count -= SMB1_CLIENT_GUID_SIZE;
  345. rc = decode_negTokenInit(
  346. pSMBr->u.extended_response.SecurityBlob, count, server);
  347. if (rc != 1)
  348. return -EINVAL;
  349. }
  350. return 0;
  351. }
  352. int
  353. cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
  354. {
  355. bool srv_sign_required = server->sec_mode & server->vals->signing_required;
  356. bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
  357. bool mnt_sign_enabled = global_secflags & CIFSSEC_MAY_SIGN;
  358. /*
  359. * Is signing required by mnt options? If not then check
  360. * global_secflags to see if it is there.
  361. */
  362. if (!mnt_sign_required)
  363. mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
  364. CIFSSEC_MUST_SIGN);
  365. /*
  366. * If signing is required then it's automatically enabled too,
  367. * otherwise, check to see if the secflags allow it.
  368. */
  369. mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
  370. (global_secflags & CIFSSEC_MAY_SIGN);
  371. /* If server requires signing, does client allow it? */
  372. if (srv_sign_required) {
  373. if (!mnt_sign_enabled) {
  374. cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!");
  375. return -ENOTSUPP;
  376. }
  377. server->sign = true;
  378. }
  379. /* If client requires signing, does server allow it? */
  380. if (mnt_sign_required) {
  381. if (!srv_sign_enabled) {
  382. cifs_dbg(VFS, "Server does not support signing!");
  383. return -ENOTSUPP;
  384. }
  385. server->sign = true;
  386. }
  387. return 0;
  388. }
  389. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  390. static int
  391. decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
  392. {
  393. __s16 tmp;
  394. struct lanman_neg_rsp *rsp = (struct lanman_neg_rsp *)pSMBr;
  395. if (server->dialect != LANMAN_PROT && server->dialect != LANMAN2_PROT)
  396. return -EOPNOTSUPP;
  397. server->sec_mode = le16_to_cpu(rsp->SecurityMode);
  398. server->maxReq = min_t(unsigned int,
  399. le16_to_cpu(rsp->MaxMpxCount),
  400. cifs_max_pending);
  401. set_credits(server, server->maxReq);
  402. server->maxBuf = le16_to_cpu(rsp->MaxBufSize);
  403. /* even though we do not use raw we might as well set this
  404. accurately, in case we ever find a need for it */
  405. if ((le16_to_cpu(rsp->RawMode) & RAW_ENABLE) == RAW_ENABLE) {
  406. server->max_rw = 0xFF00;
  407. server->capabilities = CAP_MPX_MODE | CAP_RAW_MODE;
  408. } else {
  409. server->max_rw = 0;/* do not need to use raw anyway */
  410. server->capabilities = CAP_MPX_MODE;
  411. }
  412. tmp = (__s16)le16_to_cpu(rsp->ServerTimeZone);
  413. if (tmp == -1) {
  414. /* OS/2 often does not set timezone therefore
  415. * we must use server time to calc time zone.
  416. * Could deviate slightly from the right zone.
  417. * Smallest defined timezone difference is 15 minutes
  418. * (i.e. Nepal). Rounding up/down is done to match
  419. * this requirement.
  420. */
  421. int val, seconds, remain, result;
  422. struct timespec ts, utc;
  423. utc = CURRENT_TIME;
  424. ts = cnvrtDosUnixTm(rsp->SrvTime.Date,
  425. rsp->SrvTime.Time, 0);
  426. cifs_dbg(FYI, "SrvTime %d sec since 1970 (utc: %d) diff: %d\n",
  427. (int)ts.tv_sec, (int)utc.tv_sec,
  428. (int)(utc.tv_sec - ts.tv_sec));
  429. val = (int)(utc.tv_sec - ts.tv_sec);
  430. seconds = abs(val);
  431. result = (seconds / MIN_TZ_ADJ) * MIN_TZ_ADJ;
  432. remain = seconds % MIN_TZ_ADJ;
  433. if (remain >= (MIN_TZ_ADJ / 2))
  434. result += MIN_TZ_ADJ;
  435. if (val < 0)
  436. result = -result;
  437. server->timeAdj = result;
  438. } else {
  439. server->timeAdj = (int)tmp;
  440. server->timeAdj *= 60; /* also in seconds */
  441. }
  442. cifs_dbg(FYI, "server->timeAdj: %d seconds\n", server->timeAdj);
  443. /* BB get server time for time conversions and add
  444. code to use it and timezone since this is not UTC */
  445. if (rsp->EncryptionKeyLength ==
  446. cpu_to_le16(CIFS_CRYPTO_KEY_SIZE)) {
  447. memcpy(server->cryptkey, rsp->EncryptionKey,
  448. CIFS_CRYPTO_KEY_SIZE);
  449. } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
  450. return -EIO; /* need cryptkey unless plain text */
  451. }
  452. cifs_dbg(FYI, "LANMAN negotiated\n");
  453. return 0;
  454. }
  455. #else
  456. static inline int
  457. decode_lanman_negprot_rsp(struct TCP_Server_Info *server, NEGOTIATE_RSP *pSMBr)
  458. {
  459. cifs_dbg(VFS, "mount failed, cifs module not built with CIFS_WEAK_PW_HASH support\n");
  460. return -EOPNOTSUPP;
  461. }
  462. #endif
  463. static bool
  464. should_set_ext_sec_flag(enum securityEnum sectype)
  465. {
  466. switch (sectype) {
  467. case RawNTLMSSP:
  468. case Kerberos:
  469. return true;
  470. case Unspecified:
  471. if (global_secflags &
  472. (CIFSSEC_MAY_KRB5 | CIFSSEC_MAY_NTLMSSP))
  473. return true;
  474. /* Fallthrough */
  475. default:
  476. return false;
  477. }
  478. }
  479. int
  480. CIFSSMBNegotiate(const unsigned int xid, struct cifs_ses *ses)
  481. {
  482. NEGOTIATE_REQ *pSMB;
  483. NEGOTIATE_RSP *pSMBr;
  484. int rc = 0;
  485. int bytes_returned;
  486. int i;
  487. struct TCP_Server_Info *server = ses->server;
  488. u16 count;
  489. if (!server) {
  490. WARN(1, "%s: server is NULL!\n", __func__);
  491. return -EIO;
  492. }
  493. rc = smb_init(SMB_COM_NEGOTIATE, 0, NULL /* no tcon yet */ ,
  494. (void **) &pSMB, (void **) &pSMBr);
  495. if (rc)
  496. return rc;
  497. pSMB->hdr.Mid = get_next_mid(server);
  498. pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
  499. if (should_set_ext_sec_flag(ses->sectype)) {
  500. cifs_dbg(FYI, "Requesting extended security.");
  501. pSMB->hdr.Flags2 |= SMBFLG2_EXT_SEC;
  502. }
  503. count = 0;
  504. for (i = 0; i < CIFS_NUM_PROT; i++) {
  505. strncpy(pSMB->DialectsArray+count, protocols[i].name, 16);
  506. count += strlen(protocols[i].name) + 1;
  507. /* null at end of source and target buffers anyway */
  508. }
  509. inc_rfc1001_len(pSMB, count);
  510. pSMB->ByteCount = cpu_to_le16(count);
  511. rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
  512. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  513. if (rc != 0)
  514. goto neg_err_exit;
  515. server->dialect = le16_to_cpu(pSMBr->DialectIndex);
  516. cifs_dbg(FYI, "Dialect: %d\n", server->dialect);
  517. /* Check wct = 1 error case */
  518. if ((pSMBr->hdr.WordCount < 13) || (server->dialect == BAD_PROT)) {
  519. /* core returns wct = 1, but we do not ask for core - otherwise
  520. small wct just comes when dialect index is -1 indicating we
  521. could not negotiate a common dialect */
  522. rc = -EOPNOTSUPP;
  523. goto neg_err_exit;
  524. } else if (pSMBr->hdr.WordCount == 13) {
  525. server->negflavor = CIFS_NEGFLAVOR_LANMAN;
  526. rc = decode_lanman_negprot_rsp(server, pSMBr);
  527. goto signing_check;
  528. } else if (pSMBr->hdr.WordCount != 17) {
  529. /* unknown wct */
  530. rc = -EOPNOTSUPP;
  531. goto neg_err_exit;
  532. }
  533. /* else wct == 17, NTLM or better */
  534. server->sec_mode = pSMBr->SecurityMode;
  535. if ((server->sec_mode & SECMODE_USER) == 0)
  536. cifs_dbg(FYI, "share mode security\n");
  537. /* one byte, so no need to convert this or EncryptionKeyLen from
  538. little endian */
  539. server->maxReq = min_t(unsigned int, le16_to_cpu(pSMBr->MaxMpxCount),
  540. cifs_max_pending);
  541. set_credits(server, server->maxReq);
  542. /* probably no need to store and check maxvcs */
  543. server->maxBuf = le32_to_cpu(pSMBr->MaxBufferSize);
  544. server->max_rw = le32_to_cpu(pSMBr->MaxRawSize);
  545. cifs_dbg(NOISY, "Max buf = %d\n", ses->server->maxBuf);
  546. server->capabilities = le32_to_cpu(pSMBr->Capabilities);
  547. server->timeAdj = (int)(__s16)le16_to_cpu(pSMBr->ServerTimeZone);
  548. server->timeAdj *= 60;
  549. if (pSMBr->EncryptionKeyLength == CIFS_CRYPTO_KEY_SIZE) {
  550. server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
  551. memcpy(ses->server->cryptkey, pSMBr->u.EncryptionKey,
  552. CIFS_CRYPTO_KEY_SIZE);
  553. } else if (pSMBr->hdr.Flags2 & SMBFLG2_EXT_SEC ||
  554. server->capabilities & CAP_EXTENDED_SECURITY) {
  555. server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
  556. rc = decode_ext_sec_blob(ses, pSMBr);
  557. } else if (server->sec_mode & SECMODE_PW_ENCRYPT) {
  558. rc = -EIO; /* no crypt key only if plain text pwd */
  559. } else {
  560. server->negflavor = CIFS_NEGFLAVOR_UNENCAP;
  561. server->capabilities &= ~CAP_EXTENDED_SECURITY;
  562. }
  563. signing_check:
  564. if (!rc)
  565. rc = cifs_enable_signing(server, ses->sign);
  566. neg_err_exit:
  567. cifs_buf_release(pSMB);
  568. cifs_dbg(FYI, "negprot rc %d\n", rc);
  569. return rc;
  570. }
  571. int
  572. CIFSSMBTDis(const unsigned int xid, struct cifs_tcon *tcon)
  573. {
  574. struct smb_hdr *smb_buffer;
  575. int rc = 0;
  576. cifs_dbg(FYI, "In tree disconnect\n");
  577. /* BB: do we need to check this? These should never be NULL. */
  578. if ((tcon->ses == NULL) || (tcon->ses->server == NULL))
  579. return -EIO;
  580. /*
  581. * No need to return error on this operation if tid invalidated and
  582. * closed on server already e.g. due to tcp session crashing. Also,
  583. * the tcon is no longer on the list, so no need to take lock before
  584. * checking this.
  585. */
  586. if ((tcon->need_reconnect) || (tcon->ses->need_reconnect))
  587. return 0;
  588. rc = small_smb_init(SMB_COM_TREE_DISCONNECT, 0, tcon,
  589. (void **)&smb_buffer);
  590. if (rc)
  591. return rc;
  592. rc = SendReceiveNoRsp(xid, tcon->ses, (char *)smb_buffer, 0);
  593. if (rc)
  594. cifs_dbg(FYI, "Tree disconnect failed %d\n", rc);
  595. /* No need to return error on this operation if tid invalidated and
  596. closed on server already e.g. due to tcp session crashing */
  597. if (rc == -EAGAIN)
  598. rc = 0;
  599. return rc;
  600. }
  601. /*
  602. * This is a no-op for now. We're not really interested in the reply, but
  603. * rather in the fact that the server sent one and that server->lstrp
  604. * gets updated.
  605. *
  606. * FIXME: maybe we should consider checking that the reply matches request?
  607. */
  608. static void
  609. cifs_echo_callback(struct mid_q_entry *mid)
  610. {
  611. struct TCP_Server_Info *server = mid->callback_data;
  612. mutex_lock(&server->srv_mutex);
  613. DeleteMidQEntry(mid);
  614. mutex_unlock(&server->srv_mutex);
  615. add_credits(server, 1, CIFS_ECHO_OP);
  616. }
  617. int
  618. CIFSSMBEcho(struct TCP_Server_Info *server)
  619. {
  620. ECHO_REQ *smb;
  621. int rc = 0;
  622. struct kvec iov;
  623. struct smb_rqst rqst = { .rq_iov = &iov,
  624. .rq_nvec = 1 };
  625. cifs_dbg(FYI, "In echo request\n");
  626. rc = small_smb_init(SMB_COM_ECHO, 0, NULL, (void **)&smb);
  627. if (rc)
  628. return rc;
  629. if (server->capabilities & CAP_UNICODE)
  630. smb->hdr.Flags2 |= SMBFLG2_UNICODE;
  631. /* set up echo request */
  632. smb->hdr.Tid = 0xffff;
  633. smb->hdr.WordCount = 1;
  634. put_unaligned_le16(1, &smb->EchoCount);
  635. put_bcc(1, &smb->hdr);
  636. smb->Data[0] = 'a';
  637. inc_rfc1001_len(smb, 3);
  638. iov.iov_base = smb;
  639. iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
  640. rc = cifs_call_async(server, &rqst, NULL, cifs_echo_callback,
  641. server, CIFS_ASYNC_OP | CIFS_ECHO_OP);
  642. if (rc)
  643. cifs_dbg(FYI, "Echo request failed: %d\n", rc);
  644. cifs_small_buf_release(smb);
  645. return rc;
  646. }
  647. int
  648. CIFSSMBLogoff(const unsigned int xid, struct cifs_ses *ses)
  649. {
  650. LOGOFF_ANDX_REQ *pSMB;
  651. int rc = 0;
  652. cifs_dbg(FYI, "In SMBLogoff for session disconnect\n");
  653. /*
  654. * BB: do we need to check validity of ses and server? They should
  655. * always be valid since we have an active reference. If not, that
  656. * should probably be a BUG()
  657. */
  658. if (!ses || !ses->server)
  659. return -EIO;
  660. mutex_lock(&ses->session_mutex);
  661. if (ses->need_reconnect)
  662. goto session_already_dead; /* no need to send SMBlogoff if uid
  663. already closed due to reconnect */
  664. rc = small_smb_init(SMB_COM_LOGOFF_ANDX, 2, NULL, (void **)&pSMB);
  665. if (rc) {
  666. mutex_unlock(&ses->session_mutex);
  667. return rc;
  668. }
  669. pSMB->hdr.Mid = get_next_mid(ses->server);
  670. if (ses->server->sign)
  671. pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  672. pSMB->hdr.Uid = ses->Suid;
  673. pSMB->AndXCommand = 0xFF;
  674. rc = SendReceiveNoRsp(xid, ses, (char *) pSMB, 0);
  675. session_already_dead:
  676. mutex_unlock(&ses->session_mutex);
  677. /* if session dead then we do not need to do ulogoff,
  678. since server closed smb session, no sense reporting
  679. error */
  680. if (rc == -EAGAIN)
  681. rc = 0;
  682. return rc;
  683. }
  684. int
  685. CIFSPOSIXDelFile(const unsigned int xid, struct cifs_tcon *tcon,
  686. const char *fileName, __u16 type,
  687. const struct nls_table *nls_codepage, int remap)
  688. {
  689. TRANSACTION2_SPI_REQ *pSMB = NULL;
  690. TRANSACTION2_SPI_RSP *pSMBr = NULL;
  691. struct unlink_psx_rq *pRqD;
  692. int name_len;
  693. int rc = 0;
  694. int bytes_returned = 0;
  695. __u16 params, param_offset, offset, byte_count;
  696. cifs_dbg(FYI, "In POSIX delete\n");
  697. PsxDelete:
  698. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  699. (void **) &pSMBr);
  700. if (rc)
  701. return rc;
  702. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  703. name_len =
  704. cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
  705. PATH_MAX, nls_codepage, remap);
  706. name_len++; /* trailing null */
  707. name_len *= 2;
  708. } else { /* BB add path length overrun check */
  709. name_len = strnlen(fileName, PATH_MAX);
  710. name_len++; /* trailing null */
  711. strncpy(pSMB->FileName, fileName, name_len);
  712. }
  713. params = 6 + name_len;
  714. pSMB->MaxParameterCount = cpu_to_le16(2);
  715. pSMB->MaxDataCount = 0; /* BB double check this with jra */
  716. pSMB->MaxSetupCount = 0;
  717. pSMB->Reserved = 0;
  718. pSMB->Flags = 0;
  719. pSMB->Timeout = 0;
  720. pSMB->Reserved2 = 0;
  721. param_offset = offsetof(struct smb_com_transaction2_spi_req,
  722. InformationLevel) - 4;
  723. offset = param_offset + params;
  724. /* Setup pointer to Request Data (inode type) */
  725. pRqD = (struct unlink_psx_rq *)(((char *)&pSMB->hdr.Protocol) + offset);
  726. pRqD->type = cpu_to_le16(type);
  727. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  728. pSMB->DataOffset = cpu_to_le16(offset);
  729. pSMB->SetupCount = 1;
  730. pSMB->Reserved3 = 0;
  731. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
  732. byte_count = 3 /* pad */ + params + sizeof(struct unlink_psx_rq);
  733. pSMB->DataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
  734. pSMB->TotalDataCount = cpu_to_le16(sizeof(struct unlink_psx_rq));
  735. pSMB->ParameterCount = cpu_to_le16(params);
  736. pSMB->TotalParameterCount = pSMB->ParameterCount;
  737. pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_UNLINK);
  738. pSMB->Reserved4 = 0;
  739. inc_rfc1001_len(pSMB, byte_count);
  740. pSMB->ByteCount = cpu_to_le16(byte_count);
  741. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  742. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  743. if (rc)
  744. cifs_dbg(FYI, "Posix delete returned %d\n", rc);
  745. cifs_buf_release(pSMB);
  746. cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
  747. if (rc == -EAGAIN)
  748. goto PsxDelete;
  749. return rc;
  750. }
  751. int
  752. CIFSSMBDelFile(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
  753. struct cifs_sb_info *cifs_sb)
  754. {
  755. DELETE_FILE_REQ *pSMB = NULL;
  756. DELETE_FILE_RSP *pSMBr = NULL;
  757. int rc = 0;
  758. int bytes_returned;
  759. int name_len;
  760. int remap = cifs_remap(cifs_sb);
  761. DelFileRetry:
  762. rc = smb_init(SMB_COM_DELETE, 1, tcon, (void **) &pSMB,
  763. (void **) &pSMBr);
  764. if (rc)
  765. return rc;
  766. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  767. name_len = cifsConvertToUTF16((__le16 *) pSMB->fileName, name,
  768. PATH_MAX, cifs_sb->local_nls,
  769. remap);
  770. name_len++; /* trailing null */
  771. name_len *= 2;
  772. } else { /* BB improve check for buffer overruns BB */
  773. name_len = strnlen(name, PATH_MAX);
  774. name_len++; /* trailing null */
  775. strncpy(pSMB->fileName, name, name_len);
  776. }
  777. pSMB->SearchAttributes =
  778. cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
  779. pSMB->BufferFormat = 0x04;
  780. inc_rfc1001_len(pSMB, name_len + 1);
  781. pSMB->ByteCount = cpu_to_le16(name_len + 1);
  782. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  783. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  784. cifs_stats_inc(&tcon->stats.cifs_stats.num_deletes);
  785. if (rc)
  786. cifs_dbg(FYI, "Error in RMFile = %d\n", rc);
  787. cifs_buf_release(pSMB);
  788. if (rc == -EAGAIN)
  789. goto DelFileRetry;
  790. return rc;
  791. }
  792. int
  793. CIFSSMBRmDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
  794. struct cifs_sb_info *cifs_sb)
  795. {
  796. DELETE_DIRECTORY_REQ *pSMB = NULL;
  797. DELETE_DIRECTORY_RSP *pSMBr = NULL;
  798. int rc = 0;
  799. int bytes_returned;
  800. int name_len;
  801. int remap = cifs_remap(cifs_sb);
  802. cifs_dbg(FYI, "In CIFSSMBRmDir\n");
  803. RmDirRetry:
  804. rc = smb_init(SMB_COM_DELETE_DIRECTORY, 0, tcon, (void **) &pSMB,
  805. (void **) &pSMBr);
  806. if (rc)
  807. return rc;
  808. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  809. name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
  810. PATH_MAX, cifs_sb->local_nls,
  811. remap);
  812. name_len++; /* trailing null */
  813. name_len *= 2;
  814. } else { /* BB improve check for buffer overruns BB */
  815. name_len = strnlen(name, PATH_MAX);
  816. name_len++; /* trailing null */
  817. strncpy(pSMB->DirName, name, name_len);
  818. }
  819. pSMB->BufferFormat = 0x04;
  820. inc_rfc1001_len(pSMB, name_len + 1);
  821. pSMB->ByteCount = cpu_to_le16(name_len + 1);
  822. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  823. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  824. cifs_stats_inc(&tcon->stats.cifs_stats.num_rmdirs);
  825. if (rc)
  826. cifs_dbg(FYI, "Error in RMDir = %d\n", rc);
  827. cifs_buf_release(pSMB);
  828. if (rc == -EAGAIN)
  829. goto RmDirRetry;
  830. return rc;
  831. }
  832. int
  833. CIFSSMBMkDir(const unsigned int xid, struct cifs_tcon *tcon, const char *name,
  834. struct cifs_sb_info *cifs_sb)
  835. {
  836. int rc = 0;
  837. CREATE_DIRECTORY_REQ *pSMB = NULL;
  838. CREATE_DIRECTORY_RSP *pSMBr = NULL;
  839. int bytes_returned;
  840. int name_len;
  841. int remap = cifs_remap(cifs_sb);
  842. cifs_dbg(FYI, "In CIFSSMBMkDir\n");
  843. MkDirRetry:
  844. rc = smb_init(SMB_COM_CREATE_DIRECTORY, 0, tcon, (void **) &pSMB,
  845. (void **) &pSMBr);
  846. if (rc)
  847. return rc;
  848. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  849. name_len = cifsConvertToUTF16((__le16 *) pSMB->DirName, name,
  850. PATH_MAX, cifs_sb->local_nls,
  851. remap);
  852. name_len++; /* trailing null */
  853. name_len *= 2;
  854. } else { /* BB improve check for buffer overruns BB */
  855. name_len = strnlen(name, PATH_MAX);
  856. name_len++; /* trailing null */
  857. strncpy(pSMB->DirName, name, name_len);
  858. }
  859. pSMB->BufferFormat = 0x04;
  860. inc_rfc1001_len(pSMB, name_len + 1);
  861. pSMB->ByteCount = cpu_to_le16(name_len + 1);
  862. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  863. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  864. cifs_stats_inc(&tcon->stats.cifs_stats.num_mkdirs);
  865. if (rc)
  866. cifs_dbg(FYI, "Error in Mkdir = %d\n", rc);
  867. cifs_buf_release(pSMB);
  868. if (rc == -EAGAIN)
  869. goto MkDirRetry;
  870. return rc;
  871. }
  872. int
  873. CIFSPOSIXCreate(const unsigned int xid, struct cifs_tcon *tcon,
  874. __u32 posix_flags, __u64 mode, __u16 *netfid,
  875. FILE_UNIX_BASIC_INFO *pRetData, __u32 *pOplock,
  876. const char *name, const struct nls_table *nls_codepage,
  877. int remap)
  878. {
  879. TRANSACTION2_SPI_REQ *pSMB = NULL;
  880. TRANSACTION2_SPI_RSP *pSMBr = NULL;
  881. int name_len;
  882. int rc = 0;
  883. int bytes_returned = 0;
  884. __u16 params, param_offset, offset, byte_count, count;
  885. OPEN_PSX_REQ *pdata;
  886. OPEN_PSX_RSP *psx_rsp;
  887. cifs_dbg(FYI, "In POSIX Create\n");
  888. PsxCreat:
  889. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  890. (void **) &pSMBr);
  891. if (rc)
  892. return rc;
  893. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  894. name_len =
  895. cifsConvertToUTF16((__le16 *) pSMB->FileName, name,
  896. PATH_MAX, nls_codepage, remap);
  897. name_len++; /* trailing null */
  898. name_len *= 2;
  899. } else { /* BB improve the check for buffer overruns BB */
  900. name_len = strnlen(name, PATH_MAX);
  901. name_len++; /* trailing null */
  902. strncpy(pSMB->FileName, name, name_len);
  903. }
  904. params = 6 + name_len;
  905. count = sizeof(OPEN_PSX_REQ);
  906. pSMB->MaxParameterCount = cpu_to_le16(2);
  907. pSMB->MaxDataCount = cpu_to_le16(1000); /* large enough */
  908. pSMB->MaxSetupCount = 0;
  909. pSMB->Reserved = 0;
  910. pSMB->Flags = 0;
  911. pSMB->Timeout = 0;
  912. pSMB->Reserved2 = 0;
  913. param_offset = offsetof(struct smb_com_transaction2_spi_req,
  914. InformationLevel) - 4;
  915. offset = param_offset + params;
  916. pdata = (OPEN_PSX_REQ *)(((char *)&pSMB->hdr.Protocol) + offset);
  917. pdata->Level = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
  918. pdata->Permissions = cpu_to_le64(mode);
  919. pdata->PosixOpenFlags = cpu_to_le32(posix_flags);
  920. pdata->OpenFlags = cpu_to_le32(*pOplock);
  921. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  922. pSMB->DataOffset = cpu_to_le16(offset);
  923. pSMB->SetupCount = 1;
  924. pSMB->Reserved3 = 0;
  925. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
  926. byte_count = 3 /* pad */ + params + count;
  927. pSMB->DataCount = cpu_to_le16(count);
  928. pSMB->ParameterCount = cpu_to_le16(params);
  929. pSMB->TotalDataCount = pSMB->DataCount;
  930. pSMB->TotalParameterCount = pSMB->ParameterCount;
  931. pSMB->InformationLevel = cpu_to_le16(SMB_POSIX_OPEN);
  932. pSMB->Reserved4 = 0;
  933. inc_rfc1001_len(pSMB, byte_count);
  934. pSMB->ByteCount = cpu_to_le16(byte_count);
  935. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  936. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  937. if (rc) {
  938. cifs_dbg(FYI, "Posix create returned %d\n", rc);
  939. goto psx_create_err;
  940. }
  941. cifs_dbg(FYI, "copying inode info\n");
  942. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  943. if (rc || get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)) {
  944. rc = -EIO; /* bad smb */
  945. goto psx_create_err;
  946. }
  947. /* copy return information to pRetData */
  948. psx_rsp = (OPEN_PSX_RSP *)((char *) &pSMBr->hdr.Protocol
  949. + le16_to_cpu(pSMBr->t2.DataOffset));
  950. *pOplock = le16_to_cpu(psx_rsp->OplockFlags);
  951. if (netfid)
  952. *netfid = psx_rsp->Fid; /* cifs fid stays in le */
  953. /* Let caller know file was created so we can set the mode. */
  954. /* Do we care about the CreateAction in any other cases? */
  955. if (cpu_to_le32(FILE_CREATE) == psx_rsp->CreateAction)
  956. *pOplock |= CIFS_CREATE_ACTION;
  957. /* check to make sure response data is there */
  958. if (psx_rsp->ReturnedLevel != cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC)) {
  959. pRetData->Type = cpu_to_le32(-1); /* unknown */
  960. cifs_dbg(NOISY, "unknown type\n");
  961. } else {
  962. if (get_bcc(&pSMBr->hdr) < sizeof(OPEN_PSX_RSP)
  963. + sizeof(FILE_UNIX_BASIC_INFO)) {
  964. cifs_dbg(VFS, "Open response data too small\n");
  965. pRetData->Type = cpu_to_le32(-1);
  966. goto psx_create_err;
  967. }
  968. memcpy((char *) pRetData,
  969. (char *)psx_rsp + sizeof(OPEN_PSX_RSP),
  970. sizeof(FILE_UNIX_BASIC_INFO));
  971. }
  972. psx_create_err:
  973. cifs_buf_release(pSMB);
  974. if (posix_flags & SMB_O_DIRECTORY)
  975. cifs_stats_inc(&tcon->stats.cifs_stats.num_posixmkdirs);
  976. else
  977. cifs_stats_inc(&tcon->stats.cifs_stats.num_posixopens);
  978. if (rc == -EAGAIN)
  979. goto PsxCreat;
  980. return rc;
  981. }
  982. static __u16 convert_disposition(int disposition)
  983. {
  984. __u16 ofun = 0;
  985. switch (disposition) {
  986. case FILE_SUPERSEDE:
  987. ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
  988. break;
  989. case FILE_OPEN:
  990. ofun = SMBOPEN_OAPPEND;
  991. break;
  992. case FILE_CREATE:
  993. ofun = SMBOPEN_OCREATE;
  994. break;
  995. case FILE_OPEN_IF:
  996. ofun = SMBOPEN_OCREATE | SMBOPEN_OAPPEND;
  997. break;
  998. case FILE_OVERWRITE:
  999. ofun = SMBOPEN_OTRUNC;
  1000. break;
  1001. case FILE_OVERWRITE_IF:
  1002. ofun = SMBOPEN_OCREATE | SMBOPEN_OTRUNC;
  1003. break;
  1004. default:
  1005. cifs_dbg(FYI, "unknown disposition %d\n", disposition);
  1006. ofun = SMBOPEN_OAPPEND; /* regular open */
  1007. }
  1008. return ofun;
  1009. }
  1010. static int
  1011. access_flags_to_smbopen_mode(const int access_flags)
  1012. {
  1013. int masked_flags = access_flags & (GENERIC_READ | GENERIC_WRITE);
  1014. if (masked_flags == GENERIC_READ)
  1015. return SMBOPEN_READ;
  1016. else if (masked_flags == GENERIC_WRITE)
  1017. return SMBOPEN_WRITE;
  1018. /* just go for read/write */
  1019. return SMBOPEN_READWRITE;
  1020. }
  1021. int
  1022. SMBLegacyOpen(const unsigned int xid, struct cifs_tcon *tcon,
  1023. const char *fileName, const int openDisposition,
  1024. const int access_flags, const int create_options, __u16 *netfid,
  1025. int *pOplock, FILE_ALL_INFO *pfile_info,
  1026. const struct nls_table *nls_codepage, int remap)
  1027. {
  1028. int rc = -EACCES;
  1029. OPENX_REQ *pSMB = NULL;
  1030. OPENX_RSP *pSMBr = NULL;
  1031. int bytes_returned;
  1032. int name_len;
  1033. __u16 count;
  1034. OldOpenRetry:
  1035. rc = smb_init(SMB_COM_OPEN_ANDX, 15, tcon, (void **) &pSMB,
  1036. (void **) &pSMBr);
  1037. if (rc)
  1038. return rc;
  1039. pSMB->AndXCommand = 0xFF; /* none */
  1040. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  1041. count = 1; /* account for one byte pad to word boundary */
  1042. name_len =
  1043. cifsConvertToUTF16((__le16 *) (pSMB->fileName + 1),
  1044. fileName, PATH_MAX, nls_codepage, remap);
  1045. name_len++; /* trailing null */
  1046. name_len *= 2;
  1047. } else { /* BB improve check for buffer overruns BB */
  1048. count = 0; /* no pad */
  1049. name_len = strnlen(fileName, PATH_MAX);
  1050. name_len++; /* trailing null */
  1051. strncpy(pSMB->fileName, fileName, name_len);
  1052. }
  1053. if (*pOplock & REQ_OPLOCK)
  1054. pSMB->OpenFlags = cpu_to_le16(REQ_OPLOCK);
  1055. else if (*pOplock & REQ_BATCHOPLOCK)
  1056. pSMB->OpenFlags = cpu_to_le16(REQ_BATCHOPLOCK);
  1057. pSMB->OpenFlags |= cpu_to_le16(REQ_MORE_INFO);
  1058. pSMB->Mode = cpu_to_le16(access_flags_to_smbopen_mode(access_flags));
  1059. pSMB->Mode |= cpu_to_le16(0x40); /* deny none */
  1060. /* set file as system file if special file such
  1061. as fifo and server expecting SFU style and
  1062. no Unix extensions */
  1063. if (create_options & CREATE_OPTION_SPECIAL)
  1064. pSMB->FileAttributes = cpu_to_le16(ATTR_SYSTEM);
  1065. else /* BB FIXME BB */
  1066. pSMB->FileAttributes = cpu_to_le16(0/*ATTR_NORMAL*/);
  1067. if (create_options & CREATE_OPTION_READONLY)
  1068. pSMB->FileAttributes |= cpu_to_le16(ATTR_READONLY);
  1069. /* BB FIXME BB */
  1070. /* pSMB->CreateOptions = cpu_to_le32(create_options &
  1071. CREATE_OPTIONS_MASK); */
  1072. /* BB FIXME END BB */
  1073. pSMB->Sattr = cpu_to_le16(ATTR_HIDDEN | ATTR_SYSTEM | ATTR_DIRECTORY);
  1074. pSMB->OpenFunction = cpu_to_le16(convert_disposition(openDisposition));
  1075. count += name_len;
  1076. inc_rfc1001_len(pSMB, count);
  1077. pSMB->ByteCount = cpu_to_le16(count);
  1078. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  1079. (struct smb_hdr *)pSMBr, &bytes_returned, 0);
  1080. cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
  1081. if (rc) {
  1082. cifs_dbg(FYI, "Error in Open = %d\n", rc);
  1083. } else {
  1084. /* BB verify if wct == 15 */
  1085. /* *pOplock = pSMBr->OplockLevel; */ /* BB take from action field*/
  1086. *netfid = pSMBr->Fid; /* cifs fid stays in le */
  1087. /* Let caller know file was created so we can set the mode. */
  1088. /* Do we care about the CreateAction in any other cases? */
  1089. /* BB FIXME BB */
  1090. /* if (cpu_to_le32(FILE_CREATE) == pSMBr->CreateAction)
  1091. *pOplock |= CIFS_CREATE_ACTION; */
  1092. /* BB FIXME END */
  1093. if (pfile_info) {
  1094. pfile_info->CreationTime = 0; /* BB convert CreateTime*/
  1095. pfile_info->LastAccessTime = 0; /* BB fixme */
  1096. pfile_info->LastWriteTime = 0; /* BB fixme */
  1097. pfile_info->ChangeTime = 0; /* BB fixme */
  1098. pfile_info->Attributes =
  1099. cpu_to_le32(le16_to_cpu(pSMBr->FileAttributes));
  1100. /* the file_info buf is endian converted by caller */
  1101. pfile_info->AllocationSize =
  1102. cpu_to_le64(le32_to_cpu(pSMBr->EndOfFile));
  1103. pfile_info->EndOfFile = pfile_info->AllocationSize;
  1104. pfile_info->NumberOfLinks = cpu_to_le32(1);
  1105. pfile_info->DeletePending = 0;
  1106. }
  1107. }
  1108. cifs_buf_release(pSMB);
  1109. if (rc == -EAGAIN)
  1110. goto OldOpenRetry;
  1111. return rc;
  1112. }
  1113. int
  1114. CIFS_open(const unsigned int xid, struct cifs_open_parms *oparms, int *oplock,
  1115. FILE_ALL_INFO *buf)
  1116. {
  1117. int rc = -EACCES;
  1118. OPEN_REQ *req = NULL;
  1119. OPEN_RSP *rsp = NULL;
  1120. int bytes_returned;
  1121. int name_len;
  1122. __u16 count;
  1123. struct cifs_sb_info *cifs_sb = oparms->cifs_sb;
  1124. struct cifs_tcon *tcon = oparms->tcon;
  1125. int remap = cifs_remap(cifs_sb);
  1126. const struct nls_table *nls = cifs_sb->local_nls;
  1127. int create_options = oparms->create_options;
  1128. int desired_access = oparms->desired_access;
  1129. int disposition = oparms->disposition;
  1130. const char *path = oparms->path;
  1131. openRetry:
  1132. rc = smb_init(SMB_COM_NT_CREATE_ANDX, 24, tcon, (void **)&req,
  1133. (void **)&rsp);
  1134. if (rc)
  1135. return rc;
  1136. /* no commands go after this */
  1137. req->AndXCommand = 0xFF;
  1138. if (req->hdr.Flags2 & SMBFLG2_UNICODE) {
  1139. /* account for one byte pad to word boundary */
  1140. count = 1;
  1141. name_len = cifsConvertToUTF16((__le16 *)(req->fileName + 1),
  1142. path, PATH_MAX, nls, remap);
  1143. /* trailing null */
  1144. name_len++;
  1145. name_len *= 2;
  1146. req->NameLength = cpu_to_le16(name_len);
  1147. } else {
  1148. /* BB improve check for buffer overruns BB */
  1149. /* no pad */
  1150. count = 0;
  1151. name_len = strnlen(path, PATH_MAX);
  1152. /* trailing null */
  1153. name_len++;
  1154. req->NameLength = cpu_to_le16(name_len);
  1155. strncpy(req->fileName, path, name_len);
  1156. }
  1157. if (*oplock & REQ_OPLOCK)
  1158. req->OpenFlags = cpu_to_le32(REQ_OPLOCK);
  1159. else if (*oplock & REQ_BATCHOPLOCK)
  1160. req->OpenFlags = cpu_to_le32(REQ_BATCHOPLOCK);
  1161. req->DesiredAccess = cpu_to_le32(desired_access);
  1162. req->AllocationSize = 0;
  1163. /*
  1164. * Set file as system file if special file such as fifo and server
  1165. * expecting SFU style and no Unix extensions.
  1166. */
  1167. if (create_options & CREATE_OPTION_SPECIAL)
  1168. req->FileAttributes = cpu_to_le32(ATTR_SYSTEM);
  1169. else
  1170. req->FileAttributes = cpu_to_le32(ATTR_NORMAL);
  1171. /*
  1172. * XP does not handle ATTR_POSIX_SEMANTICS but it helps speed up case
  1173. * sensitive checks for other servers such as Samba.
  1174. */
  1175. if (tcon->ses->capabilities & CAP_UNIX)
  1176. req->FileAttributes |= cpu_to_le32(ATTR_POSIX_SEMANTICS);
  1177. if (create_options & CREATE_OPTION_READONLY)
  1178. req->FileAttributes |= cpu_to_le32(ATTR_READONLY);
  1179. req->ShareAccess = cpu_to_le32(FILE_SHARE_ALL);
  1180. req->CreateDisposition = cpu_to_le32(disposition);
  1181. req->CreateOptions = cpu_to_le32(create_options & CREATE_OPTIONS_MASK);
  1182. /* BB Expirement with various impersonation levels and verify */
  1183. req->ImpersonationLevel = cpu_to_le32(SECURITY_IMPERSONATION);
  1184. req->SecurityFlags = SECURITY_CONTEXT_TRACKING|SECURITY_EFFECTIVE_ONLY;
  1185. count += name_len;
  1186. inc_rfc1001_len(req, count);
  1187. req->ByteCount = cpu_to_le16(count);
  1188. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *)req,
  1189. (struct smb_hdr *)rsp, &bytes_returned, 0);
  1190. cifs_stats_inc(&tcon->stats.cifs_stats.num_opens);
  1191. if (rc) {
  1192. cifs_dbg(FYI, "Error in Open = %d\n", rc);
  1193. cifs_buf_release(req);
  1194. if (rc == -EAGAIN)
  1195. goto openRetry;
  1196. return rc;
  1197. }
  1198. /* 1 byte no need to le_to_cpu */
  1199. *oplock = rsp->OplockLevel;
  1200. /* cifs fid stays in le */
  1201. oparms->fid->netfid = rsp->Fid;
  1202. /* Let caller know file was created so we can set the mode. */
  1203. /* Do we care about the CreateAction in any other cases? */
  1204. if (cpu_to_le32(FILE_CREATE) == rsp->CreateAction)
  1205. *oplock |= CIFS_CREATE_ACTION;
  1206. if (buf) {
  1207. /* copy from CreationTime to Attributes */
  1208. memcpy((char *)buf, (char *)&rsp->CreationTime, 36);
  1209. /* the file_info buf is endian converted by caller */
  1210. buf->AllocationSize = rsp->AllocationSize;
  1211. buf->EndOfFile = rsp->EndOfFile;
  1212. buf->NumberOfLinks = cpu_to_le32(1);
  1213. buf->DeletePending = 0;
  1214. }
  1215. cifs_buf_release(req);
  1216. return rc;
  1217. }
  1218. /*
  1219. * Discard any remaining data in the current SMB. To do this, we borrow the
  1220. * current bigbuf.
  1221. */
  1222. static int
  1223. discard_remaining_data(struct TCP_Server_Info *server)
  1224. {
  1225. unsigned int rfclen = get_rfc1002_length(server->smallbuf);
  1226. int remaining = rfclen + 4 - server->total_read;
  1227. while (remaining > 0) {
  1228. int length;
  1229. length = cifs_read_from_socket(server, server->bigbuf,
  1230. min_t(unsigned int, remaining,
  1231. CIFSMaxBufSize + MAX_HEADER_SIZE(server)));
  1232. if (length < 0)
  1233. return length;
  1234. server->total_read += length;
  1235. remaining -= length;
  1236. }
  1237. return 0;
  1238. }
  1239. static int
  1240. cifs_readv_discard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  1241. {
  1242. int length;
  1243. struct cifs_readdata *rdata = mid->callback_data;
  1244. length = discard_remaining_data(server);
  1245. dequeue_mid(mid, rdata->result);
  1246. mid->resp_buf = server->smallbuf;
  1247. server->smallbuf = NULL;
  1248. return length;
  1249. }
  1250. int
  1251. cifs_readv_receive(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  1252. {
  1253. int length, len;
  1254. unsigned int data_offset, data_len;
  1255. struct cifs_readdata *rdata = mid->callback_data;
  1256. char *buf = server->smallbuf;
  1257. unsigned int buflen = get_rfc1002_length(buf) + 4;
  1258. cifs_dbg(FYI, "%s: mid=%llu offset=%llu bytes=%u\n",
  1259. __func__, mid->mid, rdata->offset, rdata->bytes);
  1260. /*
  1261. * read the rest of READ_RSP header (sans Data array), or whatever we
  1262. * can if there's not enough data. At this point, we've read down to
  1263. * the Mid.
  1264. */
  1265. len = min_t(unsigned int, buflen, server->vals->read_rsp_size) -
  1266. HEADER_SIZE(server) + 1;
  1267. length = cifs_read_from_socket(server,
  1268. buf + HEADER_SIZE(server) - 1, len);
  1269. if (length < 0)
  1270. return length;
  1271. server->total_read += length;
  1272. if (server->ops->is_session_expired &&
  1273. server->ops->is_session_expired(buf)) {
  1274. cifs_reconnect(server);
  1275. wake_up(&server->response_q);
  1276. return -1;
  1277. }
  1278. if (server->ops->is_status_pending &&
  1279. server->ops->is_status_pending(buf, server, 0)) {
  1280. discard_remaining_data(server);
  1281. return -1;
  1282. }
  1283. /* Was the SMB read successful? */
  1284. rdata->result = server->ops->map_error(buf, false);
  1285. if (rdata->result != 0) {
  1286. cifs_dbg(FYI, "%s: server returned error %d\n",
  1287. __func__, rdata->result);
  1288. return cifs_readv_discard(server, mid);
  1289. }
  1290. /* Is there enough to get to the rest of the READ_RSP header? */
  1291. if (server->total_read < server->vals->read_rsp_size) {
  1292. cifs_dbg(FYI, "%s: server returned short header. got=%u expected=%zu\n",
  1293. __func__, server->total_read,
  1294. server->vals->read_rsp_size);
  1295. rdata->result = -EIO;
  1296. return cifs_readv_discard(server, mid);
  1297. }
  1298. data_offset = server->ops->read_data_offset(buf) + 4;
  1299. if (data_offset < server->total_read) {
  1300. /*
  1301. * win2k8 sometimes sends an offset of 0 when the read
  1302. * is beyond the EOF. Treat it as if the data starts just after
  1303. * the header.
  1304. */
  1305. cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
  1306. __func__, data_offset);
  1307. data_offset = server->total_read;
  1308. } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
  1309. /* data_offset is beyond the end of smallbuf */
  1310. cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
  1311. __func__, data_offset);
  1312. rdata->result = -EIO;
  1313. return cifs_readv_discard(server, mid);
  1314. }
  1315. cifs_dbg(FYI, "%s: total_read=%u data_offset=%u\n",
  1316. __func__, server->total_read, data_offset);
  1317. len = data_offset - server->total_read;
  1318. if (len > 0) {
  1319. /* read any junk before data into the rest of smallbuf */
  1320. length = cifs_read_from_socket(server,
  1321. buf + server->total_read, len);
  1322. if (length < 0)
  1323. return length;
  1324. server->total_read += length;
  1325. }
  1326. /* set up first iov for signature check */
  1327. rdata->iov.iov_base = buf;
  1328. rdata->iov.iov_len = server->total_read;
  1329. cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
  1330. rdata->iov.iov_base, rdata->iov.iov_len);
  1331. /* how much data is in the response? */
  1332. data_len = server->ops->read_data_length(buf);
  1333. if (data_offset + data_len > buflen) {
  1334. /* data_len is corrupt -- discard frame */
  1335. rdata->result = -EIO;
  1336. return cifs_readv_discard(server, mid);
  1337. }
  1338. length = rdata->read_into_pages(server, rdata, data_len);
  1339. if (length < 0)
  1340. return length;
  1341. server->total_read += length;
  1342. cifs_dbg(FYI, "total_read=%u buflen=%u remaining=%u\n",
  1343. server->total_read, buflen, data_len);
  1344. /* discard anything left over */
  1345. if (server->total_read < buflen)
  1346. return cifs_readv_discard(server, mid);
  1347. dequeue_mid(mid, false);
  1348. mid->resp_buf = server->smallbuf;
  1349. server->smallbuf = NULL;
  1350. return length;
  1351. }
  1352. static void
  1353. cifs_readv_callback(struct mid_q_entry *mid)
  1354. {
  1355. struct cifs_readdata *rdata = mid->callback_data;
  1356. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  1357. struct TCP_Server_Info *server = tcon->ses->server;
  1358. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1359. .rq_nvec = 1,
  1360. .rq_pages = rdata->pages,
  1361. .rq_npages = rdata->nr_pages,
  1362. .rq_pagesz = rdata->pagesz,
  1363. .rq_tailsz = rdata->tailsz };
  1364. cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
  1365. __func__, mid->mid, mid->mid_state, rdata->result,
  1366. rdata->bytes);
  1367. switch (mid->mid_state) {
  1368. case MID_RESPONSE_RECEIVED:
  1369. /* result already set, check signature */
  1370. if (server->sign) {
  1371. int rc = 0;
  1372. rc = cifs_verify_signature(&rqst, server,
  1373. mid->sequence_number);
  1374. if (rc)
  1375. cifs_dbg(VFS, "SMB signature verification returned error = %d\n",
  1376. rc);
  1377. }
  1378. /* FIXME: should this be counted toward the initiating task? */
  1379. task_io_account_read(rdata->got_bytes);
  1380. cifs_stats_bytes_read(tcon, rdata->got_bytes);
  1381. break;
  1382. case MID_REQUEST_SUBMITTED:
  1383. case MID_RETRY_NEEDED:
  1384. rdata->result = -EAGAIN;
  1385. if (server->sign && rdata->got_bytes)
  1386. /* reset bytes number since we can not check a sign */
  1387. rdata->got_bytes = 0;
  1388. /* FIXME: should this be counted toward the initiating task? */
  1389. task_io_account_read(rdata->got_bytes);
  1390. cifs_stats_bytes_read(tcon, rdata->got_bytes);
  1391. break;
  1392. default:
  1393. rdata->result = -EIO;
  1394. }
  1395. queue_work(cifsiod_wq, &rdata->work);
  1396. mutex_lock(&server->srv_mutex);
  1397. DeleteMidQEntry(mid);
  1398. mutex_unlock(&server->srv_mutex);
  1399. add_credits(server, 1, 0);
  1400. }
  1401. /* cifs_async_readv - send an async write, and set up mid to handle result */
  1402. int
  1403. cifs_async_readv(struct cifs_readdata *rdata)
  1404. {
  1405. int rc;
  1406. READ_REQ *smb = NULL;
  1407. int wct;
  1408. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  1409. struct smb_rqst rqst = { .rq_iov = &rdata->iov,
  1410. .rq_nvec = 1 };
  1411. cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
  1412. __func__, rdata->offset, rdata->bytes);
  1413. if (tcon->ses->capabilities & CAP_LARGE_FILES)
  1414. wct = 12;
  1415. else {
  1416. wct = 10; /* old style read */
  1417. if ((rdata->offset >> 32) > 0) {
  1418. /* can not handle this big offset for old */
  1419. return -EIO;
  1420. }
  1421. }
  1422. rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **)&smb);
  1423. if (rc)
  1424. return rc;
  1425. smb->hdr.Pid = cpu_to_le16((__u16)rdata->pid);
  1426. smb->hdr.PidHigh = cpu_to_le16((__u16)(rdata->pid >> 16));
  1427. smb->AndXCommand = 0xFF; /* none */
  1428. smb->Fid = rdata->cfile->fid.netfid;
  1429. smb->OffsetLow = cpu_to_le32(rdata->offset & 0xFFFFFFFF);
  1430. if (wct == 12)
  1431. smb->OffsetHigh = cpu_to_le32(rdata->offset >> 32);
  1432. smb->Remaining = 0;
  1433. smb->MaxCount = cpu_to_le16(rdata->bytes & 0xFFFF);
  1434. smb->MaxCountHigh = cpu_to_le32(rdata->bytes >> 16);
  1435. if (wct == 12)
  1436. smb->ByteCount = 0;
  1437. else {
  1438. /* old style read */
  1439. struct smb_com_readx_req *smbr =
  1440. (struct smb_com_readx_req *)smb;
  1441. smbr->ByteCount = 0;
  1442. }
  1443. /* 4 for RFC1001 length + 1 for BCC */
  1444. rdata->iov.iov_base = smb;
  1445. rdata->iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4;
  1446. kref_get(&rdata->refcount);
  1447. rc = cifs_call_async(tcon->ses->server, &rqst, cifs_readv_receive,
  1448. cifs_readv_callback, rdata, 0);
  1449. if (rc == 0)
  1450. cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
  1451. else
  1452. kref_put(&rdata->refcount, cifs_readdata_release);
  1453. cifs_small_buf_release(smb);
  1454. return rc;
  1455. }
  1456. int
  1457. CIFSSMBRead(const unsigned int xid, struct cifs_io_parms *io_parms,
  1458. unsigned int *nbytes, char **buf, int *pbuf_type)
  1459. {
  1460. int rc = -EACCES;
  1461. READ_REQ *pSMB = NULL;
  1462. READ_RSP *pSMBr = NULL;
  1463. char *pReadData = NULL;
  1464. int wct;
  1465. int resp_buf_type = 0;
  1466. struct kvec iov[1];
  1467. __u32 pid = io_parms->pid;
  1468. __u16 netfid = io_parms->netfid;
  1469. __u64 offset = io_parms->offset;
  1470. struct cifs_tcon *tcon = io_parms->tcon;
  1471. unsigned int count = io_parms->length;
  1472. cifs_dbg(FYI, "Reading %d bytes on fid %d\n", count, netfid);
  1473. if (tcon->ses->capabilities & CAP_LARGE_FILES)
  1474. wct = 12;
  1475. else {
  1476. wct = 10; /* old style read */
  1477. if ((offset >> 32) > 0) {
  1478. /* can not handle this big offset for old */
  1479. return -EIO;
  1480. }
  1481. }
  1482. *nbytes = 0;
  1483. rc = small_smb_init(SMB_COM_READ_ANDX, wct, tcon, (void **) &pSMB);
  1484. if (rc)
  1485. return rc;
  1486. pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
  1487. pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
  1488. /* tcon and ses pointer are checked in smb_init */
  1489. if (tcon->ses->server == NULL)
  1490. return -ECONNABORTED;
  1491. pSMB->AndXCommand = 0xFF; /* none */
  1492. pSMB->Fid = netfid;
  1493. pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
  1494. if (wct == 12)
  1495. pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
  1496. pSMB->Remaining = 0;
  1497. pSMB->MaxCount = cpu_to_le16(count & 0xFFFF);
  1498. pSMB->MaxCountHigh = cpu_to_le32(count >> 16);
  1499. if (wct == 12)
  1500. pSMB->ByteCount = 0; /* no need to do le conversion since 0 */
  1501. else {
  1502. /* old style read */
  1503. struct smb_com_readx_req *pSMBW =
  1504. (struct smb_com_readx_req *)pSMB;
  1505. pSMBW->ByteCount = 0;
  1506. }
  1507. iov[0].iov_base = (char *)pSMB;
  1508. iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
  1509. rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
  1510. &resp_buf_type, CIFS_LOG_ERROR);
  1511. cifs_stats_inc(&tcon->stats.cifs_stats.num_reads);
  1512. pSMBr = (READ_RSP *)iov[0].iov_base;
  1513. if (rc) {
  1514. cifs_dbg(VFS, "Send error in read = %d\n", rc);
  1515. } else {
  1516. int data_length = le16_to_cpu(pSMBr->DataLengthHigh);
  1517. data_length = data_length << 16;
  1518. data_length += le16_to_cpu(pSMBr->DataLength);
  1519. *nbytes = data_length;
  1520. /*check that DataLength would not go beyond end of SMB */
  1521. if ((data_length > CIFSMaxBufSize)
  1522. || (data_length > count)) {
  1523. cifs_dbg(FYI, "bad length %d for count %d\n",
  1524. data_length, count);
  1525. rc = -EIO;
  1526. *nbytes = 0;
  1527. } else {
  1528. pReadData = (char *) (&pSMBr->hdr.Protocol) +
  1529. le16_to_cpu(pSMBr->DataOffset);
  1530. /* if (rc = copy_to_user(buf, pReadData, data_length)) {
  1531. cifs_dbg(VFS, "Faulting on read rc = %d\n",rc);
  1532. rc = -EFAULT;
  1533. }*/ /* can not use copy_to_user when using page cache*/
  1534. if (*buf)
  1535. memcpy(*buf, pReadData, data_length);
  1536. }
  1537. }
  1538. /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
  1539. if (*buf) {
  1540. free_rsp_buf(resp_buf_type, iov[0].iov_base);
  1541. } else if (resp_buf_type != CIFS_NO_BUFFER) {
  1542. /* return buffer to caller to free */
  1543. *buf = iov[0].iov_base;
  1544. if (resp_buf_type == CIFS_SMALL_BUFFER)
  1545. *pbuf_type = CIFS_SMALL_BUFFER;
  1546. else if (resp_buf_type == CIFS_LARGE_BUFFER)
  1547. *pbuf_type = CIFS_LARGE_BUFFER;
  1548. } /* else no valid buffer on return - leave as null */
  1549. /* Note: On -EAGAIN error only caller can retry on handle based calls
  1550. since file handle passed in no longer valid */
  1551. return rc;
  1552. }
  1553. int
  1554. CIFSSMBWrite(const unsigned int xid, struct cifs_io_parms *io_parms,
  1555. unsigned int *nbytes, const char *buf)
  1556. {
  1557. int rc = -EACCES;
  1558. WRITE_REQ *pSMB = NULL;
  1559. WRITE_RSP *pSMBr = NULL;
  1560. int bytes_returned, wct;
  1561. __u32 bytes_sent;
  1562. __u16 byte_count;
  1563. __u32 pid = io_parms->pid;
  1564. __u16 netfid = io_parms->netfid;
  1565. __u64 offset = io_parms->offset;
  1566. struct cifs_tcon *tcon = io_parms->tcon;
  1567. unsigned int count = io_parms->length;
  1568. *nbytes = 0;
  1569. /* cifs_dbg(FYI, "write at %lld %d bytes\n", offset, count);*/
  1570. if (tcon->ses == NULL)
  1571. return -ECONNABORTED;
  1572. if (tcon->ses->capabilities & CAP_LARGE_FILES)
  1573. wct = 14;
  1574. else {
  1575. wct = 12;
  1576. if ((offset >> 32) > 0) {
  1577. /* can not handle big offset for old srv */
  1578. return -EIO;
  1579. }
  1580. }
  1581. rc = smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB,
  1582. (void **) &pSMBr);
  1583. if (rc)
  1584. return rc;
  1585. pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
  1586. pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
  1587. /* tcon and ses pointer are checked in smb_init */
  1588. if (tcon->ses->server == NULL)
  1589. return -ECONNABORTED;
  1590. pSMB->AndXCommand = 0xFF; /* none */
  1591. pSMB->Fid = netfid;
  1592. pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
  1593. if (wct == 14)
  1594. pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
  1595. pSMB->Reserved = 0xFFFFFFFF;
  1596. pSMB->WriteMode = 0;
  1597. pSMB->Remaining = 0;
  1598. /* Can increase buffer size if buffer is big enough in some cases ie we
  1599. can send more if LARGE_WRITE_X capability returned by the server and if
  1600. our buffer is big enough or if we convert to iovecs on socket writes
  1601. and eliminate the copy to the CIFS buffer */
  1602. if (tcon->ses->capabilities & CAP_LARGE_WRITE_X) {
  1603. bytes_sent = min_t(const unsigned int, CIFSMaxBufSize, count);
  1604. } else {
  1605. bytes_sent = (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)
  1606. & ~0xFF;
  1607. }
  1608. if (bytes_sent > count)
  1609. bytes_sent = count;
  1610. pSMB->DataOffset =
  1611. cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
  1612. if (buf)
  1613. memcpy(pSMB->Data, buf, bytes_sent);
  1614. else if (count != 0) {
  1615. /* No buffer */
  1616. cifs_buf_release(pSMB);
  1617. return -EINVAL;
  1618. } /* else setting file size with write of zero bytes */
  1619. if (wct == 14)
  1620. byte_count = bytes_sent + 1; /* pad */
  1621. else /* wct == 12 */
  1622. byte_count = bytes_sent + 5; /* bigger pad, smaller smb hdr */
  1623. pSMB->DataLengthLow = cpu_to_le16(bytes_sent & 0xFFFF);
  1624. pSMB->DataLengthHigh = cpu_to_le16(bytes_sent >> 16);
  1625. inc_rfc1001_len(pSMB, byte_count);
  1626. if (wct == 14)
  1627. pSMB->ByteCount = cpu_to_le16(byte_count);
  1628. else { /* old style write has byte count 4 bytes earlier
  1629. so 4 bytes pad */
  1630. struct smb_com_writex_req *pSMBW =
  1631. (struct smb_com_writex_req *)pSMB;
  1632. pSMBW->ByteCount = cpu_to_le16(byte_count);
  1633. }
  1634. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  1635. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  1636. cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
  1637. if (rc) {
  1638. cifs_dbg(FYI, "Send error in write = %d\n", rc);
  1639. } else {
  1640. *nbytes = le16_to_cpu(pSMBr->CountHigh);
  1641. *nbytes = (*nbytes) << 16;
  1642. *nbytes += le16_to_cpu(pSMBr->Count);
  1643. /*
  1644. * Mask off high 16 bits when bytes written as returned by the
  1645. * server is greater than bytes requested by the client. Some
  1646. * OS/2 servers are known to set incorrect CountHigh values.
  1647. */
  1648. if (*nbytes > count)
  1649. *nbytes &= 0xFFFF;
  1650. }
  1651. cifs_buf_release(pSMB);
  1652. /* Note: On -EAGAIN error only caller can retry on handle based calls
  1653. since file handle passed in no longer valid */
  1654. return rc;
  1655. }
  1656. void
  1657. cifs_writedata_release(struct kref *refcount)
  1658. {
  1659. struct cifs_writedata *wdata = container_of(refcount,
  1660. struct cifs_writedata, refcount);
  1661. if (wdata->cfile)
  1662. cifsFileInfo_put(wdata->cfile);
  1663. kfree(wdata);
  1664. }
  1665. /*
  1666. * Write failed with a retryable error. Resend the write request. It's also
  1667. * possible that the page was redirtied so re-clean the page.
  1668. */
  1669. static void
  1670. cifs_writev_requeue(struct cifs_writedata *wdata)
  1671. {
  1672. int i, rc = 0;
  1673. struct inode *inode = d_inode(wdata->cfile->dentry);
  1674. struct TCP_Server_Info *server;
  1675. unsigned int rest_len;
  1676. server = tlink_tcon(wdata->cfile->tlink)->ses->server;
  1677. i = 0;
  1678. rest_len = wdata->bytes;
  1679. do {
  1680. struct cifs_writedata *wdata2;
  1681. unsigned int j, nr_pages, wsize, tailsz, cur_len;
  1682. wsize = server->ops->wp_retry_size(inode);
  1683. if (wsize < rest_len) {
  1684. nr_pages = wsize / PAGE_SIZE;
  1685. if (!nr_pages) {
  1686. rc = -ENOTSUPP;
  1687. break;
  1688. }
  1689. cur_len = nr_pages * PAGE_SIZE;
  1690. tailsz = PAGE_SIZE;
  1691. } else {
  1692. nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
  1693. cur_len = rest_len;
  1694. tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
  1695. }
  1696. wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
  1697. if (!wdata2) {
  1698. rc = -ENOMEM;
  1699. break;
  1700. }
  1701. for (j = 0; j < nr_pages; j++) {
  1702. wdata2->pages[j] = wdata->pages[i + j];
  1703. lock_page(wdata2->pages[j]);
  1704. clear_page_dirty_for_io(wdata2->pages[j]);
  1705. }
  1706. wdata2->sync_mode = wdata->sync_mode;
  1707. wdata2->nr_pages = nr_pages;
  1708. wdata2->offset = page_offset(wdata2->pages[0]);
  1709. wdata2->pagesz = PAGE_SIZE;
  1710. wdata2->tailsz = tailsz;
  1711. wdata2->bytes = cur_len;
  1712. wdata2->cfile = find_writable_file(CIFS_I(inode), false);
  1713. if (!wdata2->cfile) {
  1714. cifs_dbg(VFS, "No writable handles for inode\n");
  1715. rc = -EBADF;
  1716. break;
  1717. }
  1718. wdata2->pid = wdata2->cfile->pid;
  1719. rc = server->ops->async_writev(wdata2, cifs_writedata_release);
  1720. for (j = 0; j < nr_pages; j++) {
  1721. unlock_page(wdata2->pages[j]);
  1722. if (rc != 0 && rc != -EAGAIN) {
  1723. SetPageError(wdata2->pages[j]);
  1724. end_page_writeback(wdata2->pages[j]);
  1725. put_page(wdata2->pages[j]);
  1726. }
  1727. }
  1728. if (rc) {
  1729. kref_put(&wdata2->refcount, cifs_writedata_release);
  1730. if (rc == -EAGAIN)
  1731. continue;
  1732. break;
  1733. }
  1734. rest_len -= cur_len;
  1735. i += nr_pages;
  1736. } while (i < wdata->nr_pages);
  1737. mapping_set_error(inode->i_mapping, rc);
  1738. kref_put(&wdata->refcount, cifs_writedata_release);
  1739. }
  1740. void
  1741. cifs_writev_complete(struct work_struct *work)
  1742. {
  1743. struct cifs_writedata *wdata = container_of(work,
  1744. struct cifs_writedata, work);
  1745. struct inode *inode = d_inode(wdata->cfile->dentry);
  1746. int i = 0;
  1747. if (wdata->result == 0) {
  1748. spin_lock(&inode->i_lock);
  1749. cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
  1750. spin_unlock(&inode->i_lock);
  1751. cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
  1752. wdata->bytes);
  1753. } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
  1754. return cifs_writev_requeue(wdata);
  1755. for (i = 0; i < wdata->nr_pages; i++) {
  1756. struct page *page = wdata->pages[i];
  1757. if (wdata->result == -EAGAIN)
  1758. __set_page_dirty_nobuffers(page);
  1759. else if (wdata->result < 0)
  1760. SetPageError(page);
  1761. end_page_writeback(page);
  1762. put_page(page);
  1763. }
  1764. if (wdata->result != -EAGAIN)
  1765. mapping_set_error(inode->i_mapping, wdata->result);
  1766. kref_put(&wdata->refcount, cifs_writedata_release);
  1767. }
  1768. struct cifs_writedata *
  1769. cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
  1770. {
  1771. struct cifs_writedata *wdata;
  1772. /* writedata + number of page pointers */
  1773. wdata = kzalloc(sizeof(*wdata) +
  1774. sizeof(struct page *) * nr_pages, GFP_NOFS);
  1775. if (wdata != NULL) {
  1776. kref_init(&wdata->refcount);
  1777. INIT_LIST_HEAD(&wdata->list);
  1778. init_completion(&wdata->done);
  1779. INIT_WORK(&wdata->work, complete);
  1780. }
  1781. return wdata;
  1782. }
  1783. /*
  1784. * Check the mid_state and signature on received buffer (if any), and queue the
  1785. * workqueue completion task.
  1786. */
  1787. static void
  1788. cifs_writev_callback(struct mid_q_entry *mid)
  1789. {
  1790. struct cifs_writedata *wdata = mid->callback_data;
  1791. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1792. struct TCP_Server_Info *server = tcon->ses->server;
  1793. unsigned int written;
  1794. WRITE_RSP *smb = (WRITE_RSP *)mid->resp_buf;
  1795. switch (mid->mid_state) {
  1796. case MID_RESPONSE_RECEIVED:
  1797. wdata->result = cifs_check_receive(mid, tcon->ses->server, 0);
  1798. if (wdata->result != 0)
  1799. break;
  1800. written = le16_to_cpu(smb->CountHigh);
  1801. written <<= 16;
  1802. written += le16_to_cpu(smb->Count);
  1803. /*
  1804. * Mask off high 16 bits when bytes written as returned
  1805. * by the server is greater than bytes requested by the
  1806. * client. OS/2 servers are known to set incorrect
  1807. * CountHigh values.
  1808. */
  1809. if (written > wdata->bytes)
  1810. written &= 0xFFFF;
  1811. if (written < wdata->bytes)
  1812. wdata->result = -ENOSPC;
  1813. else
  1814. wdata->bytes = written;
  1815. break;
  1816. case MID_REQUEST_SUBMITTED:
  1817. case MID_RETRY_NEEDED:
  1818. wdata->result = -EAGAIN;
  1819. break;
  1820. default:
  1821. wdata->result = -EIO;
  1822. break;
  1823. }
  1824. queue_work(cifsiod_wq, &wdata->work);
  1825. mutex_lock(&server->srv_mutex);
  1826. DeleteMidQEntry(mid);
  1827. mutex_unlock(&server->srv_mutex);
  1828. add_credits(tcon->ses->server, 1, 0);
  1829. }
  1830. /* cifs_async_writev - send an async write, and set up mid to handle result */
  1831. int
  1832. cifs_async_writev(struct cifs_writedata *wdata,
  1833. void (*release)(struct kref *kref))
  1834. {
  1835. int rc = -EACCES;
  1836. WRITE_REQ *smb = NULL;
  1837. int wct;
  1838. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  1839. struct kvec iov;
  1840. struct smb_rqst rqst = { };
  1841. if (tcon->ses->capabilities & CAP_LARGE_FILES) {
  1842. wct = 14;
  1843. } else {
  1844. wct = 12;
  1845. if (wdata->offset >> 32 > 0) {
  1846. /* can not handle big offset for old srv */
  1847. return -EIO;
  1848. }
  1849. }
  1850. rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **)&smb);
  1851. if (rc)
  1852. goto async_writev_out;
  1853. smb->hdr.Pid = cpu_to_le16((__u16)wdata->pid);
  1854. smb->hdr.PidHigh = cpu_to_le16((__u16)(wdata->pid >> 16));
  1855. smb->AndXCommand = 0xFF; /* none */
  1856. smb->Fid = wdata->cfile->fid.netfid;
  1857. smb->OffsetLow = cpu_to_le32(wdata->offset & 0xFFFFFFFF);
  1858. if (wct == 14)
  1859. smb->OffsetHigh = cpu_to_le32(wdata->offset >> 32);
  1860. smb->Reserved = 0xFFFFFFFF;
  1861. smb->WriteMode = 0;
  1862. smb->Remaining = 0;
  1863. smb->DataOffset =
  1864. cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
  1865. /* 4 for RFC1001 length + 1 for BCC */
  1866. iov.iov_len = be32_to_cpu(smb->hdr.smb_buf_length) + 4 + 1;
  1867. iov.iov_base = smb;
  1868. rqst.rq_iov = &iov;
  1869. rqst.rq_nvec = 1;
  1870. rqst.rq_pages = wdata->pages;
  1871. rqst.rq_npages = wdata->nr_pages;
  1872. rqst.rq_pagesz = wdata->pagesz;
  1873. rqst.rq_tailsz = wdata->tailsz;
  1874. cifs_dbg(FYI, "async write at %llu %u bytes\n",
  1875. wdata->offset, wdata->bytes);
  1876. smb->DataLengthLow = cpu_to_le16(wdata->bytes & 0xFFFF);
  1877. smb->DataLengthHigh = cpu_to_le16(wdata->bytes >> 16);
  1878. if (wct == 14) {
  1879. inc_rfc1001_len(&smb->hdr, wdata->bytes + 1);
  1880. put_bcc(wdata->bytes + 1, &smb->hdr);
  1881. } else {
  1882. /* wct == 12 */
  1883. struct smb_com_writex_req *smbw =
  1884. (struct smb_com_writex_req *)smb;
  1885. inc_rfc1001_len(&smbw->hdr, wdata->bytes + 5);
  1886. put_bcc(wdata->bytes + 5, &smbw->hdr);
  1887. iov.iov_len += 4; /* pad bigger by four bytes */
  1888. }
  1889. kref_get(&wdata->refcount);
  1890. rc = cifs_call_async(tcon->ses->server, &rqst, NULL,
  1891. cifs_writev_callback, wdata, 0);
  1892. if (rc == 0)
  1893. cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
  1894. else
  1895. kref_put(&wdata->refcount, release);
  1896. async_writev_out:
  1897. cifs_small_buf_release(smb);
  1898. return rc;
  1899. }
  1900. int
  1901. CIFSSMBWrite2(const unsigned int xid, struct cifs_io_parms *io_parms,
  1902. unsigned int *nbytes, struct kvec *iov, int n_vec)
  1903. {
  1904. int rc = -EACCES;
  1905. WRITE_REQ *pSMB = NULL;
  1906. int wct;
  1907. int smb_hdr_len;
  1908. int resp_buf_type = 0;
  1909. __u32 pid = io_parms->pid;
  1910. __u16 netfid = io_parms->netfid;
  1911. __u64 offset = io_parms->offset;
  1912. struct cifs_tcon *tcon = io_parms->tcon;
  1913. unsigned int count = io_parms->length;
  1914. *nbytes = 0;
  1915. cifs_dbg(FYI, "write2 at %lld %d bytes\n", (long long)offset, count);
  1916. if (tcon->ses->capabilities & CAP_LARGE_FILES) {
  1917. wct = 14;
  1918. } else {
  1919. wct = 12;
  1920. if ((offset >> 32) > 0) {
  1921. /* can not handle big offset for old srv */
  1922. return -EIO;
  1923. }
  1924. }
  1925. rc = small_smb_init(SMB_COM_WRITE_ANDX, wct, tcon, (void **) &pSMB);
  1926. if (rc)
  1927. return rc;
  1928. pSMB->hdr.Pid = cpu_to_le16((__u16)pid);
  1929. pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid >> 16));
  1930. /* tcon and ses pointer are checked in smb_init */
  1931. if (tcon->ses->server == NULL)
  1932. return -ECONNABORTED;
  1933. pSMB->AndXCommand = 0xFF; /* none */
  1934. pSMB->Fid = netfid;
  1935. pSMB->OffsetLow = cpu_to_le32(offset & 0xFFFFFFFF);
  1936. if (wct == 14)
  1937. pSMB->OffsetHigh = cpu_to_le32(offset >> 32);
  1938. pSMB->Reserved = 0xFFFFFFFF;
  1939. pSMB->WriteMode = 0;
  1940. pSMB->Remaining = 0;
  1941. pSMB->DataOffset =
  1942. cpu_to_le16(offsetof(struct smb_com_write_req, Data) - 4);
  1943. pSMB->DataLengthLow = cpu_to_le16(count & 0xFFFF);
  1944. pSMB->DataLengthHigh = cpu_to_le16(count >> 16);
  1945. /* header + 1 byte pad */
  1946. smb_hdr_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 1;
  1947. if (wct == 14)
  1948. inc_rfc1001_len(pSMB, count + 1);
  1949. else /* wct == 12 */
  1950. inc_rfc1001_len(pSMB, count + 5); /* smb data starts later */
  1951. if (wct == 14)
  1952. pSMB->ByteCount = cpu_to_le16(count + 1);
  1953. else /* wct == 12 */ /* bigger pad, smaller smb hdr, keep offset ok */ {
  1954. struct smb_com_writex_req *pSMBW =
  1955. (struct smb_com_writex_req *)pSMB;
  1956. pSMBW->ByteCount = cpu_to_le16(count + 5);
  1957. }
  1958. iov[0].iov_base = pSMB;
  1959. if (wct == 14)
  1960. iov[0].iov_len = smb_hdr_len + 4;
  1961. else /* wct == 12 pad bigger by four bytes */
  1962. iov[0].iov_len = smb_hdr_len + 8;
  1963. rc = SendReceive2(xid, tcon->ses, iov, n_vec + 1, &resp_buf_type, 0);
  1964. cifs_stats_inc(&tcon->stats.cifs_stats.num_writes);
  1965. if (rc) {
  1966. cifs_dbg(FYI, "Send error Write2 = %d\n", rc);
  1967. } else if (resp_buf_type == 0) {
  1968. /* presumably this can not happen, but best to be safe */
  1969. rc = -EIO;
  1970. } else {
  1971. WRITE_RSP *pSMBr = (WRITE_RSP *)iov[0].iov_base;
  1972. *nbytes = le16_to_cpu(pSMBr->CountHigh);
  1973. *nbytes = (*nbytes) << 16;
  1974. *nbytes += le16_to_cpu(pSMBr->Count);
  1975. /*
  1976. * Mask off high 16 bits when bytes written as returned by the
  1977. * server is greater than bytes requested by the client. OS/2
  1978. * servers are known to set incorrect CountHigh values.
  1979. */
  1980. if (*nbytes > count)
  1981. *nbytes &= 0xFFFF;
  1982. }
  1983. /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
  1984. free_rsp_buf(resp_buf_type, iov[0].iov_base);
  1985. /* Note: On -EAGAIN error only caller can retry on handle based calls
  1986. since file handle passed in no longer valid */
  1987. return rc;
  1988. }
  1989. int cifs_lockv(const unsigned int xid, struct cifs_tcon *tcon,
  1990. const __u16 netfid, const __u8 lock_type, const __u32 num_unlock,
  1991. const __u32 num_lock, LOCKING_ANDX_RANGE *buf)
  1992. {
  1993. int rc = 0;
  1994. LOCK_REQ *pSMB = NULL;
  1995. struct kvec iov[2];
  1996. int resp_buf_type;
  1997. __u16 count;
  1998. cifs_dbg(FYI, "cifs_lockv num lock %d num unlock %d\n",
  1999. num_lock, num_unlock);
  2000. rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
  2001. if (rc)
  2002. return rc;
  2003. pSMB->Timeout = 0;
  2004. pSMB->NumberOfLocks = cpu_to_le16(num_lock);
  2005. pSMB->NumberOfUnlocks = cpu_to_le16(num_unlock);
  2006. pSMB->LockType = lock_type;
  2007. pSMB->AndXCommand = 0xFF; /* none */
  2008. pSMB->Fid = netfid; /* netfid stays le */
  2009. count = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
  2010. inc_rfc1001_len(pSMB, count);
  2011. pSMB->ByteCount = cpu_to_le16(count);
  2012. iov[0].iov_base = (char *)pSMB;
  2013. iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4 -
  2014. (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
  2015. iov[1].iov_base = (char *)buf;
  2016. iov[1].iov_len = (num_unlock + num_lock) * sizeof(LOCKING_ANDX_RANGE);
  2017. cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
  2018. rc = SendReceive2(xid, tcon->ses, iov, 2, &resp_buf_type, CIFS_NO_RESP);
  2019. if (rc)
  2020. cifs_dbg(FYI, "Send error in cifs_lockv = %d\n", rc);
  2021. return rc;
  2022. }
  2023. int
  2024. CIFSSMBLock(const unsigned int xid, struct cifs_tcon *tcon,
  2025. const __u16 smb_file_id, const __u32 netpid, const __u64 len,
  2026. const __u64 offset, const __u32 numUnlock,
  2027. const __u32 numLock, const __u8 lockType,
  2028. const bool waitFlag, const __u8 oplock_level)
  2029. {
  2030. int rc = 0;
  2031. LOCK_REQ *pSMB = NULL;
  2032. /* LOCK_RSP *pSMBr = NULL; */ /* No response data other than rc to parse */
  2033. int bytes_returned;
  2034. int flags = 0;
  2035. __u16 count;
  2036. cifs_dbg(FYI, "CIFSSMBLock timeout %d numLock %d\n",
  2037. (int)waitFlag, numLock);
  2038. rc = small_smb_init(SMB_COM_LOCKING_ANDX, 8, tcon, (void **) &pSMB);
  2039. if (rc)
  2040. return rc;
  2041. if (lockType == LOCKING_ANDX_OPLOCK_RELEASE) {
  2042. /* no response expected */
  2043. flags = CIFS_ASYNC_OP | CIFS_OBREAK_OP;
  2044. pSMB->Timeout = 0;
  2045. } else if (waitFlag) {
  2046. flags = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
  2047. pSMB->Timeout = cpu_to_le32(-1);/* blocking - do not time out */
  2048. } else {
  2049. pSMB->Timeout = 0;
  2050. }
  2051. pSMB->NumberOfLocks = cpu_to_le16(numLock);
  2052. pSMB->NumberOfUnlocks = cpu_to_le16(numUnlock);
  2053. pSMB->LockType = lockType;
  2054. pSMB->OplockLevel = oplock_level;
  2055. pSMB->AndXCommand = 0xFF; /* none */
  2056. pSMB->Fid = smb_file_id; /* netfid stays le */
  2057. if ((numLock != 0) || (numUnlock != 0)) {
  2058. pSMB->Locks[0].Pid = cpu_to_le16(netpid);
  2059. /* BB where to store pid high? */
  2060. pSMB->Locks[0].LengthLow = cpu_to_le32((u32)len);
  2061. pSMB->Locks[0].LengthHigh = cpu_to_le32((u32)(len>>32));
  2062. pSMB->Locks[0].OffsetLow = cpu_to_le32((u32)offset);
  2063. pSMB->Locks[0].OffsetHigh = cpu_to_le32((u32)(offset>>32));
  2064. count = sizeof(LOCKING_ANDX_RANGE);
  2065. } else {
  2066. /* oplock break */
  2067. count = 0;
  2068. }
  2069. inc_rfc1001_len(pSMB, count);
  2070. pSMB->ByteCount = cpu_to_le16(count);
  2071. if (waitFlag) {
  2072. rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
  2073. (struct smb_hdr *) pSMB, &bytes_returned);
  2074. cifs_small_buf_release(pSMB);
  2075. } else {
  2076. rc = SendReceiveNoRsp(xid, tcon->ses, (char *)pSMB, flags);
  2077. /* SMB buffer freed by function above */
  2078. }
  2079. cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
  2080. if (rc)
  2081. cifs_dbg(FYI, "Send error in Lock = %d\n", rc);
  2082. /* Note: On -EAGAIN error only caller can retry on handle based calls
  2083. since file handle passed in no longer valid */
  2084. return rc;
  2085. }
  2086. int
  2087. CIFSSMBPosixLock(const unsigned int xid, struct cifs_tcon *tcon,
  2088. const __u16 smb_file_id, const __u32 netpid,
  2089. const loff_t start_offset, const __u64 len,
  2090. struct file_lock *pLockData, const __u16 lock_type,
  2091. const bool waitFlag)
  2092. {
  2093. struct smb_com_transaction2_sfi_req *pSMB = NULL;
  2094. struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
  2095. struct cifs_posix_lock *parm_data;
  2096. int rc = 0;
  2097. int timeout = 0;
  2098. int bytes_returned = 0;
  2099. int resp_buf_type = 0;
  2100. __u16 params, param_offset, offset, byte_count, count;
  2101. struct kvec iov[1];
  2102. cifs_dbg(FYI, "Posix Lock\n");
  2103. rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
  2104. if (rc)
  2105. return rc;
  2106. pSMBr = (struct smb_com_transaction2_sfi_rsp *)pSMB;
  2107. params = 6;
  2108. pSMB->MaxSetupCount = 0;
  2109. pSMB->Reserved = 0;
  2110. pSMB->Flags = 0;
  2111. pSMB->Reserved2 = 0;
  2112. param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
  2113. offset = param_offset + params;
  2114. count = sizeof(struct cifs_posix_lock);
  2115. pSMB->MaxParameterCount = cpu_to_le16(2);
  2116. pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
  2117. pSMB->SetupCount = 1;
  2118. pSMB->Reserved3 = 0;
  2119. if (pLockData)
  2120. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
  2121. else
  2122. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
  2123. byte_count = 3 /* pad */ + params + count;
  2124. pSMB->DataCount = cpu_to_le16(count);
  2125. pSMB->ParameterCount = cpu_to_le16(params);
  2126. pSMB->TotalDataCount = pSMB->DataCount;
  2127. pSMB->TotalParameterCount = pSMB->ParameterCount;
  2128. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  2129. parm_data = (struct cifs_posix_lock *)
  2130. (((char *) &pSMB->hdr.Protocol) + offset);
  2131. parm_data->lock_type = cpu_to_le16(lock_type);
  2132. if (waitFlag) {
  2133. timeout = CIFS_BLOCKING_OP; /* blocking operation, no timeout */
  2134. parm_data->lock_flags = cpu_to_le16(1);
  2135. pSMB->Timeout = cpu_to_le32(-1);
  2136. } else
  2137. pSMB->Timeout = 0;
  2138. parm_data->pid = cpu_to_le32(netpid);
  2139. parm_data->start = cpu_to_le64(start_offset);
  2140. parm_data->length = cpu_to_le64(len); /* normalize negative numbers */
  2141. pSMB->DataOffset = cpu_to_le16(offset);
  2142. pSMB->Fid = smb_file_id;
  2143. pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_LOCK);
  2144. pSMB->Reserved4 = 0;
  2145. inc_rfc1001_len(pSMB, byte_count);
  2146. pSMB->ByteCount = cpu_to_le16(byte_count);
  2147. if (waitFlag) {
  2148. rc = SendReceiveBlockingLock(xid, tcon, (struct smb_hdr *) pSMB,
  2149. (struct smb_hdr *) pSMBr, &bytes_returned);
  2150. } else {
  2151. iov[0].iov_base = (char *)pSMB;
  2152. iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
  2153. rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovecs */,
  2154. &resp_buf_type, timeout);
  2155. pSMB = NULL; /* request buf already freed by SendReceive2. Do
  2156. not try to free it twice below on exit */
  2157. pSMBr = (struct smb_com_transaction2_sfi_rsp *)iov[0].iov_base;
  2158. }
  2159. if (rc) {
  2160. cifs_dbg(FYI, "Send error in Posix Lock = %d\n", rc);
  2161. } else if (pLockData) {
  2162. /* lock structure can be returned on get */
  2163. __u16 data_offset;
  2164. __u16 data_count;
  2165. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  2166. if (rc || get_bcc(&pSMBr->hdr) < sizeof(*parm_data)) {
  2167. rc = -EIO; /* bad smb */
  2168. goto plk_err_exit;
  2169. }
  2170. data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  2171. data_count = le16_to_cpu(pSMBr->t2.DataCount);
  2172. if (data_count < sizeof(struct cifs_posix_lock)) {
  2173. rc = -EIO;
  2174. goto plk_err_exit;
  2175. }
  2176. parm_data = (struct cifs_posix_lock *)
  2177. ((char *)&pSMBr->hdr.Protocol + data_offset);
  2178. if (parm_data->lock_type == cpu_to_le16(CIFS_UNLCK))
  2179. pLockData->fl_type = F_UNLCK;
  2180. else {
  2181. if (parm_data->lock_type ==
  2182. cpu_to_le16(CIFS_RDLCK))
  2183. pLockData->fl_type = F_RDLCK;
  2184. else if (parm_data->lock_type ==
  2185. cpu_to_le16(CIFS_WRLCK))
  2186. pLockData->fl_type = F_WRLCK;
  2187. pLockData->fl_start = le64_to_cpu(parm_data->start);
  2188. pLockData->fl_end = pLockData->fl_start +
  2189. le64_to_cpu(parm_data->length) - 1;
  2190. pLockData->fl_pid = le32_to_cpu(parm_data->pid);
  2191. }
  2192. }
  2193. plk_err_exit:
  2194. if (pSMB)
  2195. cifs_small_buf_release(pSMB);
  2196. free_rsp_buf(resp_buf_type, iov[0].iov_base);
  2197. /* Note: On -EAGAIN error only caller can retry on handle based calls
  2198. since file handle passed in no longer valid */
  2199. return rc;
  2200. }
  2201. int
  2202. CIFSSMBClose(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
  2203. {
  2204. int rc = 0;
  2205. CLOSE_REQ *pSMB = NULL;
  2206. cifs_dbg(FYI, "In CIFSSMBClose\n");
  2207. /* do not retry on dead session on close */
  2208. rc = small_smb_init(SMB_COM_CLOSE, 3, tcon, (void **) &pSMB);
  2209. if (rc == -EAGAIN)
  2210. return 0;
  2211. if (rc)
  2212. return rc;
  2213. pSMB->FileID = (__u16) smb_file_id;
  2214. pSMB->LastWriteTime = 0xFFFFFFFF;
  2215. pSMB->ByteCount = 0;
  2216. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
  2217. cifs_stats_inc(&tcon->stats.cifs_stats.num_closes);
  2218. if (rc) {
  2219. if (rc != -EINTR) {
  2220. /* EINTR is expected when user ctl-c to kill app */
  2221. cifs_dbg(VFS, "Send error in Close = %d\n", rc);
  2222. }
  2223. }
  2224. /* Since session is dead, file will be closed on server already */
  2225. if (rc == -EAGAIN)
  2226. rc = 0;
  2227. return rc;
  2228. }
  2229. int
  2230. CIFSSMBFlush(const unsigned int xid, struct cifs_tcon *tcon, int smb_file_id)
  2231. {
  2232. int rc = 0;
  2233. FLUSH_REQ *pSMB = NULL;
  2234. cifs_dbg(FYI, "In CIFSSMBFlush\n");
  2235. rc = small_smb_init(SMB_COM_FLUSH, 1, tcon, (void **) &pSMB);
  2236. if (rc)
  2237. return rc;
  2238. pSMB->FileID = (__u16) smb_file_id;
  2239. pSMB->ByteCount = 0;
  2240. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
  2241. cifs_stats_inc(&tcon->stats.cifs_stats.num_flushes);
  2242. if (rc)
  2243. cifs_dbg(VFS, "Send error in Flush = %d\n", rc);
  2244. return rc;
  2245. }
  2246. int
  2247. CIFSSMBRename(const unsigned int xid, struct cifs_tcon *tcon,
  2248. const char *from_name, const char *to_name,
  2249. struct cifs_sb_info *cifs_sb)
  2250. {
  2251. int rc = 0;
  2252. RENAME_REQ *pSMB = NULL;
  2253. RENAME_RSP *pSMBr = NULL;
  2254. int bytes_returned;
  2255. int name_len, name_len2;
  2256. __u16 count;
  2257. int remap = cifs_remap(cifs_sb);
  2258. cifs_dbg(FYI, "In CIFSSMBRename\n");
  2259. renameRetry:
  2260. rc = smb_init(SMB_COM_RENAME, 1, tcon, (void **) &pSMB,
  2261. (void **) &pSMBr);
  2262. if (rc)
  2263. return rc;
  2264. pSMB->BufferFormat = 0x04;
  2265. pSMB->SearchAttributes =
  2266. cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
  2267. ATTR_DIRECTORY);
  2268. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  2269. name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
  2270. from_name, PATH_MAX,
  2271. cifs_sb->local_nls, remap);
  2272. name_len++; /* trailing null */
  2273. name_len *= 2;
  2274. pSMB->OldFileName[name_len] = 0x04; /* pad */
  2275. /* protocol requires ASCII signature byte on Unicode string */
  2276. pSMB->OldFileName[name_len + 1] = 0x00;
  2277. name_len2 =
  2278. cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
  2279. to_name, PATH_MAX, cifs_sb->local_nls,
  2280. remap);
  2281. name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
  2282. name_len2 *= 2; /* convert to bytes */
  2283. } else { /* BB improve the check for buffer overruns BB */
  2284. name_len = strnlen(from_name, PATH_MAX);
  2285. name_len++; /* trailing null */
  2286. strncpy(pSMB->OldFileName, from_name, name_len);
  2287. name_len2 = strnlen(to_name, PATH_MAX);
  2288. name_len2++; /* trailing null */
  2289. pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
  2290. strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
  2291. name_len2++; /* trailing null */
  2292. name_len2++; /* signature byte */
  2293. }
  2294. count = 1 /* 1st signature byte */ + name_len + name_len2;
  2295. inc_rfc1001_len(pSMB, count);
  2296. pSMB->ByteCount = cpu_to_le16(count);
  2297. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  2298. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  2299. cifs_stats_inc(&tcon->stats.cifs_stats.num_renames);
  2300. if (rc)
  2301. cifs_dbg(FYI, "Send error in rename = %d\n", rc);
  2302. cifs_buf_release(pSMB);
  2303. if (rc == -EAGAIN)
  2304. goto renameRetry;
  2305. return rc;
  2306. }
  2307. int CIFSSMBRenameOpenFile(const unsigned int xid, struct cifs_tcon *pTcon,
  2308. int netfid, const char *target_name,
  2309. const struct nls_table *nls_codepage, int remap)
  2310. {
  2311. struct smb_com_transaction2_sfi_req *pSMB = NULL;
  2312. struct smb_com_transaction2_sfi_rsp *pSMBr = NULL;
  2313. struct set_file_rename *rename_info;
  2314. char *data_offset;
  2315. char dummy_string[30];
  2316. int rc = 0;
  2317. int bytes_returned = 0;
  2318. int len_of_str;
  2319. __u16 params, param_offset, offset, count, byte_count;
  2320. cifs_dbg(FYI, "Rename to File by handle\n");
  2321. rc = smb_init(SMB_COM_TRANSACTION2, 15, pTcon, (void **) &pSMB,
  2322. (void **) &pSMBr);
  2323. if (rc)
  2324. return rc;
  2325. params = 6;
  2326. pSMB->MaxSetupCount = 0;
  2327. pSMB->Reserved = 0;
  2328. pSMB->Flags = 0;
  2329. pSMB->Timeout = 0;
  2330. pSMB->Reserved2 = 0;
  2331. param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
  2332. offset = param_offset + params;
  2333. data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
  2334. rename_info = (struct set_file_rename *) data_offset;
  2335. pSMB->MaxParameterCount = cpu_to_le16(2);
  2336. pSMB->MaxDataCount = cpu_to_le16(1000); /* BB find max SMB from sess */
  2337. pSMB->SetupCount = 1;
  2338. pSMB->Reserved3 = 0;
  2339. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
  2340. byte_count = 3 /* pad */ + params;
  2341. pSMB->ParameterCount = cpu_to_le16(params);
  2342. pSMB->TotalParameterCount = pSMB->ParameterCount;
  2343. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  2344. pSMB->DataOffset = cpu_to_le16(offset);
  2345. /* construct random name ".cifs_tmp<inodenum><mid>" */
  2346. rename_info->overwrite = cpu_to_le32(1);
  2347. rename_info->root_fid = 0;
  2348. /* unicode only call */
  2349. if (target_name == NULL) {
  2350. sprintf(dummy_string, "cifs%x", pSMB->hdr.Mid);
  2351. len_of_str =
  2352. cifsConvertToUTF16((__le16 *)rename_info->target_name,
  2353. dummy_string, 24, nls_codepage, remap);
  2354. } else {
  2355. len_of_str =
  2356. cifsConvertToUTF16((__le16 *)rename_info->target_name,
  2357. target_name, PATH_MAX, nls_codepage,
  2358. remap);
  2359. }
  2360. rename_info->target_name_len = cpu_to_le32(2 * len_of_str);
  2361. count = 12 /* sizeof(struct set_file_rename) */ + (2 * len_of_str);
  2362. byte_count += count;
  2363. pSMB->DataCount = cpu_to_le16(count);
  2364. pSMB->TotalDataCount = pSMB->DataCount;
  2365. pSMB->Fid = netfid;
  2366. pSMB->InformationLevel =
  2367. cpu_to_le16(SMB_SET_FILE_RENAME_INFORMATION);
  2368. pSMB->Reserved4 = 0;
  2369. inc_rfc1001_len(pSMB, byte_count);
  2370. pSMB->ByteCount = cpu_to_le16(byte_count);
  2371. rc = SendReceive(xid, pTcon->ses, (struct smb_hdr *) pSMB,
  2372. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  2373. cifs_stats_inc(&pTcon->stats.cifs_stats.num_t2renames);
  2374. if (rc)
  2375. cifs_dbg(FYI, "Send error in Rename (by file handle) = %d\n",
  2376. rc);
  2377. cifs_buf_release(pSMB);
  2378. /* Note: On -EAGAIN error only caller can retry on handle based calls
  2379. since file handle passed in no longer valid */
  2380. return rc;
  2381. }
  2382. int
  2383. CIFSSMBCopy(const unsigned int xid, struct cifs_tcon *tcon,
  2384. const char *fromName, const __u16 target_tid, const char *toName,
  2385. const int flags, const struct nls_table *nls_codepage, int remap)
  2386. {
  2387. int rc = 0;
  2388. COPY_REQ *pSMB = NULL;
  2389. COPY_RSP *pSMBr = NULL;
  2390. int bytes_returned;
  2391. int name_len, name_len2;
  2392. __u16 count;
  2393. cifs_dbg(FYI, "In CIFSSMBCopy\n");
  2394. copyRetry:
  2395. rc = smb_init(SMB_COM_COPY, 1, tcon, (void **) &pSMB,
  2396. (void **) &pSMBr);
  2397. if (rc)
  2398. return rc;
  2399. pSMB->BufferFormat = 0x04;
  2400. pSMB->Tid2 = target_tid;
  2401. pSMB->Flags = cpu_to_le16(flags & COPY_TREE);
  2402. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  2403. name_len = cifsConvertToUTF16((__le16 *) pSMB->OldFileName,
  2404. fromName, PATH_MAX, nls_codepage,
  2405. remap);
  2406. name_len++; /* trailing null */
  2407. name_len *= 2;
  2408. pSMB->OldFileName[name_len] = 0x04; /* pad */
  2409. /* protocol requires ASCII signature byte on Unicode string */
  2410. pSMB->OldFileName[name_len + 1] = 0x00;
  2411. name_len2 =
  2412. cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
  2413. toName, PATH_MAX, nls_codepage, remap);
  2414. name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
  2415. name_len2 *= 2; /* convert to bytes */
  2416. } else { /* BB improve the check for buffer overruns BB */
  2417. name_len = strnlen(fromName, PATH_MAX);
  2418. name_len++; /* trailing null */
  2419. strncpy(pSMB->OldFileName, fromName, name_len);
  2420. name_len2 = strnlen(toName, PATH_MAX);
  2421. name_len2++; /* trailing null */
  2422. pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
  2423. strncpy(&pSMB->OldFileName[name_len + 1], toName, name_len2);
  2424. name_len2++; /* trailing null */
  2425. name_len2++; /* signature byte */
  2426. }
  2427. count = 1 /* 1st signature byte */ + name_len + name_len2;
  2428. inc_rfc1001_len(pSMB, count);
  2429. pSMB->ByteCount = cpu_to_le16(count);
  2430. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  2431. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  2432. if (rc) {
  2433. cifs_dbg(FYI, "Send error in copy = %d with %d files copied\n",
  2434. rc, le16_to_cpu(pSMBr->CopyCount));
  2435. }
  2436. cifs_buf_release(pSMB);
  2437. if (rc == -EAGAIN)
  2438. goto copyRetry;
  2439. return rc;
  2440. }
  2441. int
  2442. CIFSUnixCreateSymLink(const unsigned int xid, struct cifs_tcon *tcon,
  2443. const char *fromName, const char *toName,
  2444. const struct nls_table *nls_codepage, int remap)
  2445. {
  2446. TRANSACTION2_SPI_REQ *pSMB = NULL;
  2447. TRANSACTION2_SPI_RSP *pSMBr = NULL;
  2448. char *data_offset;
  2449. int name_len;
  2450. int name_len_target;
  2451. int rc = 0;
  2452. int bytes_returned = 0;
  2453. __u16 params, param_offset, offset, byte_count;
  2454. cifs_dbg(FYI, "In Symlink Unix style\n");
  2455. createSymLinkRetry:
  2456. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  2457. (void **) &pSMBr);
  2458. if (rc)
  2459. return rc;
  2460. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  2461. name_len =
  2462. cifsConvertToUTF16((__le16 *) pSMB->FileName, fromName,
  2463. /* find define for this maxpathcomponent */
  2464. PATH_MAX, nls_codepage, remap);
  2465. name_len++; /* trailing null */
  2466. name_len *= 2;
  2467. } else { /* BB improve the check for buffer overruns BB */
  2468. name_len = strnlen(fromName, PATH_MAX);
  2469. name_len++; /* trailing null */
  2470. strncpy(pSMB->FileName, fromName, name_len);
  2471. }
  2472. params = 6 + name_len;
  2473. pSMB->MaxSetupCount = 0;
  2474. pSMB->Reserved = 0;
  2475. pSMB->Flags = 0;
  2476. pSMB->Timeout = 0;
  2477. pSMB->Reserved2 = 0;
  2478. param_offset = offsetof(struct smb_com_transaction2_spi_req,
  2479. InformationLevel) - 4;
  2480. offset = param_offset + params;
  2481. data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
  2482. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  2483. name_len_target =
  2484. cifsConvertToUTF16((__le16 *) data_offset, toName,
  2485. /* find define for this maxpathcomponent */
  2486. PATH_MAX, nls_codepage, remap);
  2487. name_len_target++; /* trailing null */
  2488. name_len_target *= 2;
  2489. } else { /* BB improve the check for buffer overruns BB */
  2490. name_len_target = strnlen(toName, PATH_MAX);
  2491. name_len_target++; /* trailing null */
  2492. strncpy(data_offset, toName, name_len_target);
  2493. }
  2494. pSMB->MaxParameterCount = cpu_to_le16(2);
  2495. /* BB find exact max on data count below from sess */
  2496. pSMB->MaxDataCount = cpu_to_le16(1000);
  2497. pSMB->SetupCount = 1;
  2498. pSMB->Reserved3 = 0;
  2499. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
  2500. byte_count = 3 /* pad */ + params + name_len_target;
  2501. pSMB->DataCount = cpu_to_le16(name_len_target);
  2502. pSMB->ParameterCount = cpu_to_le16(params);
  2503. pSMB->TotalDataCount = pSMB->DataCount;
  2504. pSMB->TotalParameterCount = pSMB->ParameterCount;
  2505. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  2506. pSMB->DataOffset = cpu_to_le16(offset);
  2507. pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_LINK);
  2508. pSMB->Reserved4 = 0;
  2509. inc_rfc1001_len(pSMB, byte_count);
  2510. pSMB->ByteCount = cpu_to_le16(byte_count);
  2511. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  2512. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  2513. cifs_stats_inc(&tcon->stats.cifs_stats.num_symlinks);
  2514. if (rc)
  2515. cifs_dbg(FYI, "Send error in SetPathInfo create symlink = %d\n",
  2516. rc);
  2517. cifs_buf_release(pSMB);
  2518. if (rc == -EAGAIN)
  2519. goto createSymLinkRetry;
  2520. return rc;
  2521. }
  2522. int
  2523. CIFSUnixCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
  2524. const char *fromName, const char *toName,
  2525. const struct nls_table *nls_codepage, int remap)
  2526. {
  2527. TRANSACTION2_SPI_REQ *pSMB = NULL;
  2528. TRANSACTION2_SPI_RSP *pSMBr = NULL;
  2529. char *data_offset;
  2530. int name_len;
  2531. int name_len_target;
  2532. int rc = 0;
  2533. int bytes_returned = 0;
  2534. __u16 params, param_offset, offset, byte_count;
  2535. cifs_dbg(FYI, "In Create Hard link Unix style\n");
  2536. createHardLinkRetry:
  2537. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  2538. (void **) &pSMBr);
  2539. if (rc)
  2540. return rc;
  2541. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  2542. name_len = cifsConvertToUTF16((__le16 *) pSMB->FileName, toName,
  2543. PATH_MAX, nls_codepage, remap);
  2544. name_len++; /* trailing null */
  2545. name_len *= 2;
  2546. } else { /* BB improve the check for buffer overruns BB */
  2547. name_len = strnlen(toName, PATH_MAX);
  2548. name_len++; /* trailing null */
  2549. strncpy(pSMB->FileName, toName, name_len);
  2550. }
  2551. params = 6 + name_len;
  2552. pSMB->MaxSetupCount = 0;
  2553. pSMB->Reserved = 0;
  2554. pSMB->Flags = 0;
  2555. pSMB->Timeout = 0;
  2556. pSMB->Reserved2 = 0;
  2557. param_offset = offsetof(struct smb_com_transaction2_spi_req,
  2558. InformationLevel) - 4;
  2559. offset = param_offset + params;
  2560. data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
  2561. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  2562. name_len_target =
  2563. cifsConvertToUTF16((__le16 *) data_offset, fromName,
  2564. PATH_MAX, nls_codepage, remap);
  2565. name_len_target++; /* trailing null */
  2566. name_len_target *= 2;
  2567. } else { /* BB improve the check for buffer overruns BB */
  2568. name_len_target = strnlen(fromName, PATH_MAX);
  2569. name_len_target++; /* trailing null */
  2570. strncpy(data_offset, fromName, name_len_target);
  2571. }
  2572. pSMB->MaxParameterCount = cpu_to_le16(2);
  2573. /* BB find exact max on data count below from sess*/
  2574. pSMB->MaxDataCount = cpu_to_le16(1000);
  2575. pSMB->SetupCount = 1;
  2576. pSMB->Reserved3 = 0;
  2577. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
  2578. byte_count = 3 /* pad */ + params + name_len_target;
  2579. pSMB->ParameterCount = cpu_to_le16(params);
  2580. pSMB->TotalParameterCount = pSMB->ParameterCount;
  2581. pSMB->DataCount = cpu_to_le16(name_len_target);
  2582. pSMB->TotalDataCount = pSMB->DataCount;
  2583. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  2584. pSMB->DataOffset = cpu_to_le16(offset);
  2585. pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_HLINK);
  2586. pSMB->Reserved4 = 0;
  2587. inc_rfc1001_len(pSMB, byte_count);
  2588. pSMB->ByteCount = cpu_to_le16(byte_count);
  2589. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  2590. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  2591. cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
  2592. if (rc)
  2593. cifs_dbg(FYI, "Send error in SetPathInfo (hard link) = %d\n",
  2594. rc);
  2595. cifs_buf_release(pSMB);
  2596. if (rc == -EAGAIN)
  2597. goto createHardLinkRetry;
  2598. return rc;
  2599. }
  2600. int
  2601. CIFSCreateHardLink(const unsigned int xid, struct cifs_tcon *tcon,
  2602. const char *from_name, const char *to_name,
  2603. struct cifs_sb_info *cifs_sb)
  2604. {
  2605. int rc = 0;
  2606. NT_RENAME_REQ *pSMB = NULL;
  2607. RENAME_RSP *pSMBr = NULL;
  2608. int bytes_returned;
  2609. int name_len, name_len2;
  2610. __u16 count;
  2611. int remap = cifs_remap(cifs_sb);
  2612. cifs_dbg(FYI, "In CIFSCreateHardLink\n");
  2613. winCreateHardLinkRetry:
  2614. rc = smb_init(SMB_COM_NT_RENAME, 4, tcon, (void **) &pSMB,
  2615. (void **) &pSMBr);
  2616. if (rc)
  2617. return rc;
  2618. pSMB->SearchAttributes =
  2619. cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
  2620. ATTR_DIRECTORY);
  2621. pSMB->Flags = cpu_to_le16(CREATE_HARD_LINK);
  2622. pSMB->ClusterCount = 0;
  2623. pSMB->BufferFormat = 0x04;
  2624. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  2625. name_len =
  2626. cifsConvertToUTF16((__le16 *) pSMB->OldFileName, from_name,
  2627. PATH_MAX, cifs_sb->local_nls, remap);
  2628. name_len++; /* trailing null */
  2629. name_len *= 2;
  2630. /* protocol specifies ASCII buffer format (0x04) for unicode */
  2631. pSMB->OldFileName[name_len] = 0x04;
  2632. pSMB->OldFileName[name_len + 1] = 0x00; /* pad */
  2633. name_len2 =
  2634. cifsConvertToUTF16((__le16 *)&pSMB->OldFileName[name_len+2],
  2635. to_name, PATH_MAX, cifs_sb->local_nls,
  2636. remap);
  2637. name_len2 += 1 /* trailing null */ + 1 /* Signature word */ ;
  2638. name_len2 *= 2; /* convert to bytes */
  2639. } else { /* BB improve the check for buffer overruns BB */
  2640. name_len = strnlen(from_name, PATH_MAX);
  2641. name_len++; /* trailing null */
  2642. strncpy(pSMB->OldFileName, from_name, name_len);
  2643. name_len2 = strnlen(to_name, PATH_MAX);
  2644. name_len2++; /* trailing null */
  2645. pSMB->OldFileName[name_len] = 0x04; /* 2nd buffer format */
  2646. strncpy(&pSMB->OldFileName[name_len + 1], to_name, name_len2);
  2647. name_len2++; /* trailing null */
  2648. name_len2++; /* signature byte */
  2649. }
  2650. count = 1 /* string type byte */ + name_len + name_len2;
  2651. inc_rfc1001_len(pSMB, count);
  2652. pSMB->ByteCount = cpu_to_le16(count);
  2653. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  2654. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  2655. cifs_stats_inc(&tcon->stats.cifs_stats.num_hardlinks);
  2656. if (rc)
  2657. cifs_dbg(FYI, "Send error in hard link (NT rename) = %d\n", rc);
  2658. cifs_buf_release(pSMB);
  2659. if (rc == -EAGAIN)
  2660. goto winCreateHardLinkRetry;
  2661. return rc;
  2662. }
  2663. int
  2664. CIFSSMBUnixQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
  2665. const unsigned char *searchName, char **symlinkinfo,
  2666. const struct nls_table *nls_codepage, int remap)
  2667. {
  2668. /* SMB_QUERY_FILE_UNIX_LINK */
  2669. TRANSACTION2_QPI_REQ *pSMB = NULL;
  2670. TRANSACTION2_QPI_RSP *pSMBr = NULL;
  2671. int rc = 0;
  2672. int bytes_returned;
  2673. int name_len;
  2674. __u16 params, byte_count;
  2675. char *data_start;
  2676. cifs_dbg(FYI, "In QPathSymLinkInfo (Unix) for path %s\n", searchName);
  2677. querySymLinkRetry:
  2678. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  2679. (void **) &pSMBr);
  2680. if (rc)
  2681. return rc;
  2682. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  2683. name_len =
  2684. cifsConvertToUTF16((__le16 *) pSMB->FileName,
  2685. searchName, PATH_MAX, nls_codepage,
  2686. remap);
  2687. name_len++; /* trailing null */
  2688. name_len *= 2;
  2689. } else { /* BB improve the check for buffer overruns BB */
  2690. name_len = strnlen(searchName, PATH_MAX);
  2691. name_len++; /* trailing null */
  2692. strncpy(pSMB->FileName, searchName, name_len);
  2693. }
  2694. params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
  2695. pSMB->TotalDataCount = 0;
  2696. pSMB->MaxParameterCount = cpu_to_le16(2);
  2697. pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
  2698. pSMB->MaxSetupCount = 0;
  2699. pSMB->Reserved = 0;
  2700. pSMB->Flags = 0;
  2701. pSMB->Timeout = 0;
  2702. pSMB->Reserved2 = 0;
  2703. pSMB->ParameterOffset = cpu_to_le16(offsetof(
  2704. struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
  2705. pSMB->DataCount = 0;
  2706. pSMB->DataOffset = 0;
  2707. pSMB->SetupCount = 1;
  2708. pSMB->Reserved3 = 0;
  2709. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
  2710. byte_count = params + 1 /* pad */ ;
  2711. pSMB->TotalParameterCount = cpu_to_le16(params);
  2712. pSMB->ParameterCount = pSMB->TotalParameterCount;
  2713. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_LINK);
  2714. pSMB->Reserved4 = 0;
  2715. inc_rfc1001_len(pSMB, byte_count);
  2716. pSMB->ByteCount = cpu_to_le16(byte_count);
  2717. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  2718. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  2719. if (rc) {
  2720. cifs_dbg(FYI, "Send error in QuerySymLinkInfo = %d\n", rc);
  2721. } else {
  2722. /* decode response */
  2723. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  2724. /* BB also check enough total bytes returned */
  2725. if (rc || get_bcc(&pSMBr->hdr) < 2)
  2726. rc = -EIO;
  2727. else {
  2728. bool is_unicode;
  2729. u16 count = le16_to_cpu(pSMBr->t2.DataCount);
  2730. data_start = ((char *) &pSMBr->hdr.Protocol) +
  2731. le16_to_cpu(pSMBr->t2.DataOffset);
  2732. if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
  2733. is_unicode = true;
  2734. else
  2735. is_unicode = false;
  2736. /* BB FIXME investigate remapping reserved chars here */
  2737. *symlinkinfo = cifs_strndup_from_utf16(data_start,
  2738. count, is_unicode, nls_codepage);
  2739. if (!*symlinkinfo)
  2740. rc = -ENOMEM;
  2741. }
  2742. }
  2743. cifs_buf_release(pSMB);
  2744. if (rc == -EAGAIN)
  2745. goto querySymLinkRetry;
  2746. return rc;
  2747. }
  2748. /*
  2749. * Recent Windows versions now create symlinks more frequently
  2750. * and they use the "reparse point" mechanism below. We can of course
  2751. * do symlinks nicely to Samba and other servers which support the
  2752. * CIFS Unix Extensions and we can also do SFU symlinks and "client only"
  2753. * "MF" symlinks optionally, but for recent Windows we really need to
  2754. * reenable the code below and fix the cifs_symlink callers to handle this.
  2755. * In the interim this code has been moved to its own config option so
  2756. * it is not compiled in by default until callers fixed up and more tested.
  2757. */
  2758. int
  2759. CIFSSMBQuerySymLink(const unsigned int xid, struct cifs_tcon *tcon,
  2760. __u16 fid, char **symlinkinfo,
  2761. const struct nls_table *nls_codepage)
  2762. {
  2763. int rc = 0;
  2764. int bytes_returned;
  2765. struct smb_com_transaction_ioctl_req *pSMB;
  2766. struct smb_com_transaction_ioctl_rsp *pSMBr;
  2767. bool is_unicode;
  2768. unsigned int sub_len;
  2769. char *sub_start;
  2770. struct reparse_symlink_data *reparse_buf;
  2771. struct reparse_posix_data *posix_buf;
  2772. __u32 data_offset, data_count;
  2773. char *end_of_smb;
  2774. cifs_dbg(FYI, "In Windows reparse style QueryLink for fid %u\n", fid);
  2775. rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
  2776. (void **) &pSMBr);
  2777. if (rc)
  2778. return rc;
  2779. pSMB->TotalParameterCount = 0 ;
  2780. pSMB->TotalDataCount = 0;
  2781. pSMB->MaxParameterCount = cpu_to_le32(2);
  2782. /* BB find exact data count max from sess structure BB */
  2783. pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
  2784. pSMB->MaxSetupCount = 4;
  2785. pSMB->Reserved = 0;
  2786. pSMB->ParameterOffset = 0;
  2787. pSMB->DataCount = 0;
  2788. pSMB->DataOffset = 0;
  2789. pSMB->SetupCount = 4;
  2790. pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
  2791. pSMB->ParameterCount = pSMB->TotalParameterCount;
  2792. pSMB->FunctionCode = cpu_to_le32(FSCTL_GET_REPARSE_POINT);
  2793. pSMB->IsFsctl = 1; /* FSCTL */
  2794. pSMB->IsRootFlag = 0;
  2795. pSMB->Fid = fid; /* file handle always le */
  2796. pSMB->ByteCount = 0;
  2797. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  2798. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  2799. if (rc) {
  2800. cifs_dbg(FYI, "Send error in QueryReparseLinkInfo = %d\n", rc);
  2801. goto qreparse_out;
  2802. }
  2803. data_offset = le32_to_cpu(pSMBr->DataOffset);
  2804. data_count = le32_to_cpu(pSMBr->DataCount);
  2805. if (get_bcc(&pSMBr->hdr) < 2 || data_offset > 512) {
  2806. /* BB also check enough total bytes returned */
  2807. rc = -EIO; /* bad smb */
  2808. goto qreparse_out;
  2809. }
  2810. if (!data_count || (data_count > 2048)) {
  2811. rc = -EIO;
  2812. cifs_dbg(FYI, "Invalid return data count on get reparse info ioctl\n");
  2813. goto qreparse_out;
  2814. }
  2815. end_of_smb = 2 + get_bcc(&pSMBr->hdr) + (char *)&pSMBr->ByteCount;
  2816. reparse_buf = (struct reparse_symlink_data *)
  2817. ((char *)&pSMBr->hdr.Protocol + data_offset);
  2818. if ((char *)reparse_buf >= end_of_smb) {
  2819. rc = -EIO;
  2820. goto qreparse_out;
  2821. }
  2822. if (reparse_buf->ReparseTag == cpu_to_le32(IO_REPARSE_TAG_NFS)) {
  2823. cifs_dbg(FYI, "NFS style reparse tag\n");
  2824. posix_buf = (struct reparse_posix_data *)reparse_buf;
  2825. if (posix_buf->InodeType != cpu_to_le64(NFS_SPECFILE_LNK)) {
  2826. cifs_dbg(FYI, "unsupported file type 0x%llx\n",
  2827. le64_to_cpu(posix_buf->InodeType));
  2828. rc = -EOPNOTSUPP;
  2829. goto qreparse_out;
  2830. }
  2831. is_unicode = true;
  2832. sub_len = le16_to_cpu(reparse_buf->ReparseDataLength);
  2833. if (posix_buf->PathBuffer + sub_len > end_of_smb) {
  2834. cifs_dbg(FYI, "reparse buf beyond SMB\n");
  2835. rc = -EIO;
  2836. goto qreparse_out;
  2837. }
  2838. *symlinkinfo = cifs_strndup_from_utf16(posix_buf->PathBuffer,
  2839. sub_len, is_unicode, nls_codepage);
  2840. goto qreparse_out;
  2841. } else if (reparse_buf->ReparseTag !=
  2842. cpu_to_le32(IO_REPARSE_TAG_SYMLINK)) {
  2843. rc = -EOPNOTSUPP;
  2844. goto qreparse_out;
  2845. }
  2846. /* Reparse tag is NTFS symlink */
  2847. sub_start = le16_to_cpu(reparse_buf->SubstituteNameOffset) +
  2848. reparse_buf->PathBuffer;
  2849. sub_len = le16_to_cpu(reparse_buf->SubstituteNameLength);
  2850. if (sub_start + sub_len > end_of_smb) {
  2851. cifs_dbg(FYI, "reparse buf beyond SMB\n");
  2852. rc = -EIO;
  2853. goto qreparse_out;
  2854. }
  2855. if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
  2856. is_unicode = true;
  2857. else
  2858. is_unicode = false;
  2859. /* BB FIXME investigate remapping reserved chars here */
  2860. *symlinkinfo = cifs_strndup_from_utf16(sub_start, sub_len, is_unicode,
  2861. nls_codepage);
  2862. if (!*symlinkinfo)
  2863. rc = -ENOMEM;
  2864. qreparse_out:
  2865. cifs_buf_release(pSMB);
  2866. /*
  2867. * Note: On -EAGAIN error only caller can retry on handle based calls
  2868. * since file handle passed in no longer valid.
  2869. */
  2870. return rc;
  2871. }
  2872. int
  2873. CIFSSMB_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
  2874. __u16 fid)
  2875. {
  2876. int rc = 0;
  2877. int bytes_returned;
  2878. struct smb_com_transaction_compr_ioctl_req *pSMB;
  2879. struct smb_com_transaction_ioctl_rsp *pSMBr;
  2880. cifs_dbg(FYI, "Set compression for %u\n", fid);
  2881. rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
  2882. (void **) &pSMBr);
  2883. if (rc)
  2884. return rc;
  2885. pSMB->compression_state = cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
  2886. pSMB->TotalParameterCount = 0;
  2887. pSMB->TotalDataCount = cpu_to_le32(2);
  2888. pSMB->MaxParameterCount = 0;
  2889. pSMB->MaxDataCount = 0;
  2890. pSMB->MaxSetupCount = 4;
  2891. pSMB->Reserved = 0;
  2892. pSMB->ParameterOffset = 0;
  2893. pSMB->DataCount = cpu_to_le32(2);
  2894. pSMB->DataOffset =
  2895. cpu_to_le32(offsetof(struct smb_com_transaction_compr_ioctl_req,
  2896. compression_state) - 4); /* 84 */
  2897. pSMB->SetupCount = 4;
  2898. pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_IOCTL);
  2899. pSMB->ParameterCount = 0;
  2900. pSMB->FunctionCode = cpu_to_le32(FSCTL_SET_COMPRESSION);
  2901. pSMB->IsFsctl = 1; /* FSCTL */
  2902. pSMB->IsRootFlag = 0;
  2903. pSMB->Fid = fid; /* file handle always le */
  2904. /* 3 byte pad, followed by 2 byte compress state */
  2905. pSMB->ByteCount = cpu_to_le16(5);
  2906. inc_rfc1001_len(pSMB, 5);
  2907. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  2908. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  2909. if (rc)
  2910. cifs_dbg(FYI, "Send error in SetCompression = %d\n", rc);
  2911. cifs_buf_release(pSMB);
  2912. /*
  2913. * Note: On -EAGAIN error only caller can retry on handle based calls
  2914. * since file handle passed in no longer valid.
  2915. */
  2916. return rc;
  2917. }
  2918. #ifdef CONFIG_CIFS_POSIX
  2919. /*Convert an Access Control Entry from wire format to local POSIX xattr format*/
  2920. static void cifs_convert_ace(struct posix_acl_xattr_entry *ace,
  2921. struct cifs_posix_ace *cifs_ace)
  2922. {
  2923. /* u8 cifs fields do not need le conversion */
  2924. ace->e_perm = cpu_to_le16(cifs_ace->cifs_e_perm);
  2925. ace->e_tag = cpu_to_le16(cifs_ace->cifs_e_tag);
  2926. ace->e_id = cpu_to_le32(le64_to_cpu(cifs_ace->cifs_uid));
  2927. /*
  2928. cifs_dbg(FYI, "perm %d tag %d id %d\n",
  2929. ace->e_perm, ace->e_tag, ace->e_id);
  2930. */
  2931. return;
  2932. }
  2933. /* Convert ACL from CIFS POSIX wire format to local Linux POSIX ACL xattr */
  2934. static int cifs_copy_posix_acl(char *trgt, char *src, const int buflen,
  2935. const int acl_type, const int size_of_data_area)
  2936. {
  2937. int size = 0;
  2938. int i;
  2939. __u16 count;
  2940. struct cifs_posix_ace *pACE;
  2941. struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)src;
  2942. struct posix_acl_xattr_header *local_acl = (void *)trgt;
  2943. if (le16_to_cpu(cifs_acl->version) != CIFS_ACL_VERSION)
  2944. return -EOPNOTSUPP;
  2945. if (acl_type == ACL_TYPE_ACCESS) {
  2946. count = le16_to_cpu(cifs_acl->access_entry_count);
  2947. pACE = &cifs_acl->ace_array[0];
  2948. size = sizeof(struct cifs_posix_acl);
  2949. size += sizeof(struct cifs_posix_ace) * count;
  2950. /* check if we would go beyond end of SMB */
  2951. if (size_of_data_area < size) {
  2952. cifs_dbg(FYI, "bad CIFS POSIX ACL size %d vs. %d\n",
  2953. size_of_data_area, size);
  2954. return -EINVAL;
  2955. }
  2956. } else if (acl_type == ACL_TYPE_DEFAULT) {
  2957. count = le16_to_cpu(cifs_acl->access_entry_count);
  2958. size = sizeof(struct cifs_posix_acl);
  2959. size += sizeof(struct cifs_posix_ace) * count;
  2960. /* skip past access ACEs to get to default ACEs */
  2961. pACE = &cifs_acl->ace_array[count];
  2962. count = le16_to_cpu(cifs_acl->default_entry_count);
  2963. size += sizeof(struct cifs_posix_ace) * count;
  2964. /* check if we would go beyond end of SMB */
  2965. if (size_of_data_area < size)
  2966. return -EINVAL;
  2967. } else {
  2968. /* illegal type */
  2969. return -EINVAL;
  2970. }
  2971. size = posix_acl_xattr_size(count);
  2972. if ((buflen == 0) || (local_acl == NULL)) {
  2973. /* used to query ACL EA size */
  2974. } else if (size > buflen) {
  2975. return -ERANGE;
  2976. } else /* buffer big enough */ {
  2977. struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
  2978. local_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  2979. for (i = 0; i < count ; i++) {
  2980. cifs_convert_ace(&ace[i], pACE);
  2981. pACE++;
  2982. }
  2983. }
  2984. return size;
  2985. }
  2986. static __u16 convert_ace_to_cifs_ace(struct cifs_posix_ace *cifs_ace,
  2987. const struct posix_acl_xattr_entry *local_ace)
  2988. {
  2989. __u16 rc = 0; /* 0 = ACL converted ok */
  2990. cifs_ace->cifs_e_perm = le16_to_cpu(local_ace->e_perm);
  2991. cifs_ace->cifs_e_tag = le16_to_cpu(local_ace->e_tag);
  2992. /* BB is there a better way to handle the large uid? */
  2993. if (local_ace->e_id == cpu_to_le32(-1)) {
  2994. /* Probably no need to le convert -1 on any arch but can not hurt */
  2995. cifs_ace->cifs_uid = cpu_to_le64(-1);
  2996. } else
  2997. cifs_ace->cifs_uid = cpu_to_le64(le32_to_cpu(local_ace->e_id));
  2998. /*
  2999. cifs_dbg(FYI, "perm %d tag %d id %d\n",
  3000. ace->e_perm, ace->e_tag, ace->e_id);
  3001. */
  3002. return rc;
  3003. }
  3004. /* Convert ACL from local Linux POSIX xattr to CIFS POSIX ACL wire format */
  3005. static __u16 ACL_to_cifs_posix(char *parm_data, const char *pACL,
  3006. const int buflen, const int acl_type)
  3007. {
  3008. __u16 rc = 0;
  3009. struct cifs_posix_acl *cifs_acl = (struct cifs_posix_acl *)parm_data;
  3010. struct posix_acl_xattr_header *local_acl = (void *)pACL;
  3011. struct posix_acl_xattr_entry *ace = (void *)(local_acl + 1);
  3012. int count;
  3013. int i;
  3014. if ((buflen == 0) || (pACL == NULL) || (cifs_acl == NULL))
  3015. return 0;
  3016. count = posix_acl_xattr_count((size_t)buflen);
  3017. cifs_dbg(FYI, "setting acl with %d entries from buf of length %d and version of %d\n",
  3018. count, buflen, le32_to_cpu(local_acl->a_version));
  3019. if (le32_to_cpu(local_acl->a_version) != 2) {
  3020. cifs_dbg(FYI, "unknown POSIX ACL version %d\n",
  3021. le32_to_cpu(local_acl->a_version));
  3022. return 0;
  3023. }
  3024. cifs_acl->version = cpu_to_le16(1);
  3025. if (acl_type == ACL_TYPE_ACCESS) {
  3026. cifs_acl->access_entry_count = cpu_to_le16(count);
  3027. cifs_acl->default_entry_count = cpu_to_le16(0xFFFF);
  3028. } else if (acl_type == ACL_TYPE_DEFAULT) {
  3029. cifs_acl->default_entry_count = cpu_to_le16(count);
  3030. cifs_acl->access_entry_count = cpu_to_le16(0xFFFF);
  3031. } else {
  3032. cifs_dbg(FYI, "unknown ACL type %d\n", acl_type);
  3033. return 0;
  3034. }
  3035. for (i = 0; i < count; i++) {
  3036. rc = convert_ace_to_cifs_ace(&cifs_acl->ace_array[i], &ace[i]);
  3037. if (rc != 0) {
  3038. /* ACE not converted */
  3039. break;
  3040. }
  3041. }
  3042. if (rc == 0) {
  3043. rc = (__u16)(count * sizeof(struct cifs_posix_ace));
  3044. rc += sizeof(struct cifs_posix_acl);
  3045. /* BB add check to make sure ACL does not overflow SMB */
  3046. }
  3047. return rc;
  3048. }
  3049. int
  3050. CIFSSMBGetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
  3051. const unsigned char *searchName,
  3052. char *acl_inf, const int buflen, const int acl_type,
  3053. const struct nls_table *nls_codepage, int remap)
  3054. {
  3055. /* SMB_QUERY_POSIX_ACL */
  3056. TRANSACTION2_QPI_REQ *pSMB = NULL;
  3057. TRANSACTION2_QPI_RSP *pSMBr = NULL;
  3058. int rc = 0;
  3059. int bytes_returned;
  3060. int name_len;
  3061. __u16 params, byte_count;
  3062. cifs_dbg(FYI, "In GetPosixACL (Unix) for path %s\n", searchName);
  3063. queryAclRetry:
  3064. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  3065. (void **) &pSMBr);
  3066. if (rc)
  3067. return rc;
  3068. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  3069. name_len =
  3070. cifsConvertToUTF16((__le16 *) pSMB->FileName,
  3071. searchName, PATH_MAX, nls_codepage,
  3072. remap);
  3073. name_len++; /* trailing null */
  3074. name_len *= 2;
  3075. pSMB->FileName[name_len] = 0;
  3076. pSMB->FileName[name_len+1] = 0;
  3077. } else { /* BB improve the check for buffer overruns BB */
  3078. name_len = strnlen(searchName, PATH_MAX);
  3079. name_len++; /* trailing null */
  3080. strncpy(pSMB->FileName, searchName, name_len);
  3081. }
  3082. params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
  3083. pSMB->TotalDataCount = 0;
  3084. pSMB->MaxParameterCount = cpu_to_le16(2);
  3085. /* BB find exact max data count below from sess structure BB */
  3086. pSMB->MaxDataCount = cpu_to_le16(4000);
  3087. pSMB->MaxSetupCount = 0;
  3088. pSMB->Reserved = 0;
  3089. pSMB->Flags = 0;
  3090. pSMB->Timeout = 0;
  3091. pSMB->Reserved2 = 0;
  3092. pSMB->ParameterOffset = cpu_to_le16(
  3093. offsetof(struct smb_com_transaction2_qpi_req,
  3094. InformationLevel) - 4);
  3095. pSMB->DataCount = 0;
  3096. pSMB->DataOffset = 0;
  3097. pSMB->SetupCount = 1;
  3098. pSMB->Reserved3 = 0;
  3099. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
  3100. byte_count = params + 1 /* pad */ ;
  3101. pSMB->TotalParameterCount = cpu_to_le16(params);
  3102. pSMB->ParameterCount = pSMB->TotalParameterCount;
  3103. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_ACL);
  3104. pSMB->Reserved4 = 0;
  3105. inc_rfc1001_len(pSMB, byte_count);
  3106. pSMB->ByteCount = cpu_to_le16(byte_count);
  3107. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  3108. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  3109. cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
  3110. if (rc) {
  3111. cifs_dbg(FYI, "Send error in Query POSIX ACL = %d\n", rc);
  3112. } else {
  3113. /* decode response */
  3114. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  3115. /* BB also check enough total bytes returned */
  3116. if (rc || get_bcc(&pSMBr->hdr) < 2)
  3117. rc = -EIO; /* bad smb */
  3118. else {
  3119. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  3120. __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
  3121. rc = cifs_copy_posix_acl(acl_inf,
  3122. (char *)&pSMBr->hdr.Protocol+data_offset,
  3123. buflen, acl_type, count);
  3124. }
  3125. }
  3126. cifs_buf_release(pSMB);
  3127. if (rc == -EAGAIN)
  3128. goto queryAclRetry;
  3129. return rc;
  3130. }
  3131. int
  3132. CIFSSMBSetPosixACL(const unsigned int xid, struct cifs_tcon *tcon,
  3133. const unsigned char *fileName,
  3134. const char *local_acl, const int buflen,
  3135. const int acl_type,
  3136. const struct nls_table *nls_codepage, int remap)
  3137. {
  3138. struct smb_com_transaction2_spi_req *pSMB = NULL;
  3139. struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
  3140. char *parm_data;
  3141. int name_len;
  3142. int rc = 0;
  3143. int bytes_returned = 0;
  3144. __u16 params, byte_count, data_count, param_offset, offset;
  3145. cifs_dbg(FYI, "In SetPosixACL (Unix) for path %s\n", fileName);
  3146. setAclRetry:
  3147. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  3148. (void **) &pSMBr);
  3149. if (rc)
  3150. return rc;
  3151. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  3152. name_len =
  3153. cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
  3154. PATH_MAX, nls_codepage, remap);
  3155. name_len++; /* trailing null */
  3156. name_len *= 2;
  3157. } else { /* BB improve the check for buffer overruns BB */
  3158. name_len = strnlen(fileName, PATH_MAX);
  3159. name_len++; /* trailing null */
  3160. strncpy(pSMB->FileName, fileName, name_len);
  3161. }
  3162. params = 6 + name_len;
  3163. pSMB->MaxParameterCount = cpu_to_le16(2);
  3164. /* BB find max SMB size from sess */
  3165. pSMB->MaxDataCount = cpu_to_le16(1000);
  3166. pSMB->MaxSetupCount = 0;
  3167. pSMB->Reserved = 0;
  3168. pSMB->Flags = 0;
  3169. pSMB->Timeout = 0;
  3170. pSMB->Reserved2 = 0;
  3171. param_offset = offsetof(struct smb_com_transaction2_spi_req,
  3172. InformationLevel) - 4;
  3173. offset = param_offset + params;
  3174. parm_data = ((char *) &pSMB->hdr.Protocol) + offset;
  3175. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  3176. /* convert to on the wire format for POSIX ACL */
  3177. data_count = ACL_to_cifs_posix(parm_data, local_acl, buflen, acl_type);
  3178. if (data_count == 0) {
  3179. rc = -EOPNOTSUPP;
  3180. goto setACLerrorExit;
  3181. }
  3182. pSMB->DataOffset = cpu_to_le16(offset);
  3183. pSMB->SetupCount = 1;
  3184. pSMB->Reserved3 = 0;
  3185. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
  3186. pSMB->InformationLevel = cpu_to_le16(SMB_SET_POSIX_ACL);
  3187. byte_count = 3 /* pad */ + params + data_count;
  3188. pSMB->DataCount = cpu_to_le16(data_count);
  3189. pSMB->TotalDataCount = pSMB->DataCount;
  3190. pSMB->ParameterCount = cpu_to_le16(params);
  3191. pSMB->TotalParameterCount = pSMB->ParameterCount;
  3192. pSMB->Reserved4 = 0;
  3193. inc_rfc1001_len(pSMB, byte_count);
  3194. pSMB->ByteCount = cpu_to_le16(byte_count);
  3195. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  3196. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  3197. if (rc)
  3198. cifs_dbg(FYI, "Set POSIX ACL returned %d\n", rc);
  3199. setACLerrorExit:
  3200. cifs_buf_release(pSMB);
  3201. if (rc == -EAGAIN)
  3202. goto setAclRetry;
  3203. return rc;
  3204. }
  3205. /* BB fix tabs in this function FIXME BB */
  3206. int
  3207. CIFSGetExtAttr(const unsigned int xid, struct cifs_tcon *tcon,
  3208. const int netfid, __u64 *pExtAttrBits, __u64 *pMask)
  3209. {
  3210. int rc = 0;
  3211. struct smb_t2_qfi_req *pSMB = NULL;
  3212. struct smb_t2_qfi_rsp *pSMBr = NULL;
  3213. int bytes_returned;
  3214. __u16 params, byte_count;
  3215. cifs_dbg(FYI, "In GetExtAttr\n");
  3216. if (tcon == NULL)
  3217. return -ENODEV;
  3218. GetExtAttrRetry:
  3219. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  3220. (void **) &pSMBr);
  3221. if (rc)
  3222. return rc;
  3223. params = 2 /* level */ + 2 /* fid */;
  3224. pSMB->t2.TotalDataCount = 0;
  3225. pSMB->t2.MaxParameterCount = cpu_to_le16(4);
  3226. /* BB find exact max data count below from sess structure BB */
  3227. pSMB->t2.MaxDataCount = cpu_to_le16(4000);
  3228. pSMB->t2.MaxSetupCount = 0;
  3229. pSMB->t2.Reserved = 0;
  3230. pSMB->t2.Flags = 0;
  3231. pSMB->t2.Timeout = 0;
  3232. pSMB->t2.Reserved2 = 0;
  3233. pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
  3234. Fid) - 4);
  3235. pSMB->t2.DataCount = 0;
  3236. pSMB->t2.DataOffset = 0;
  3237. pSMB->t2.SetupCount = 1;
  3238. pSMB->t2.Reserved3 = 0;
  3239. pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
  3240. byte_count = params + 1 /* pad */ ;
  3241. pSMB->t2.TotalParameterCount = cpu_to_le16(params);
  3242. pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
  3243. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_ATTR_FLAGS);
  3244. pSMB->Pad = 0;
  3245. pSMB->Fid = netfid;
  3246. inc_rfc1001_len(pSMB, byte_count);
  3247. pSMB->t2.ByteCount = cpu_to_le16(byte_count);
  3248. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  3249. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  3250. if (rc) {
  3251. cifs_dbg(FYI, "error %d in GetExtAttr\n", rc);
  3252. } else {
  3253. /* decode response */
  3254. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  3255. /* BB also check enough total bytes returned */
  3256. if (rc || get_bcc(&pSMBr->hdr) < 2)
  3257. /* If rc should we check for EOPNOSUPP and
  3258. disable the srvino flag? or in caller? */
  3259. rc = -EIO; /* bad smb */
  3260. else {
  3261. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  3262. __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
  3263. struct file_chattr_info *pfinfo;
  3264. /* BB Do we need a cast or hash here ? */
  3265. if (count != 16) {
  3266. cifs_dbg(FYI, "Illegal size ret in GetExtAttr\n");
  3267. rc = -EIO;
  3268. goto GetExtAttrOut;
  3269. }
  3270. pfinfo = (struct file_chattr_info *)
  3271. (data_offset + (char *) &pSMBr->hdr.Protocol);
  3272. *pExtAttrBits = le64_to_cpu(pfinfo->mode);
  3273. *pMask = le64_to_cpu(pfinfo->mask);
  3274. }
  3275. }
  3276. GetExtAttrOut:
  3277. cifs_buf_release(pSMB);
  3278. if (rc == -EAGAIN)
  3279. goto GetExtAttrRetry;
  3280. return rc;
  3281. }
  3282. #endif /* CONFIG_POSIX */
  3283. #ifdef CONFIG_CIFS_ACL
  3284. /*
  3285. * Initialize NT TRANSACT SMB into small smb request buffer. This assumes that
  3286. * all NT TRANSACTS that we init here have total parm and data under about 400
  3287. * bytes (to fit in small cifs buffer size), which is the case so far, it
  3288. * easily fits. NB: Setup words themselves and ByteCount MaxSetupCount (size of
  3289. * returned setup area) and MaxParameterCount (returned parms size) must be set
  3290. * by caller
  3291. */
  3292. static int
  3293. smb_init_nttransact(const __u16 sub_command, const int setup_count,
  3294. const int parm_len, struct cifs_tcon *tcon,
  3295. void **ret_buf)
  3296. {
  3297. int rc;
  3298. __u32 temp_offset;
  3299. struct smb_com_ntransact_req *pSMB;
  3300. rc = small_smb_init(SMB_COM_NT_TRANSACT, 19 + setup_count, tcon,
  3301. (void **)&pSMB);
  3302. if (rc)
  3303. return rc;
  3304. *ret_buf = (void *)pSMB;
  3305. pSMB->Reserved = 0;
  3306. pSMB->TotalParameterCount = cpu_to_le32(parm_len);
  3307. pSMB->TotalDataCount = 0;
  3308. pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
  3309. pSMB->ParameterCount = pSMB->TotalParameterCount;
  3310. pSMB->DataCount = pSMB->TotalDataCount;
  3311. temp_offset = offsetof(struct smb_com_ntransact_req, Parms) +
  3312. (setup_count * 2) - 4 /* for rfc1001 length itself */;
  3313. pSMB->ParameterOffset = cpu_to_le32(temp_offset);
  3314. pSMB->DataOffset = cpu_to_le32(temp_offset + parm_len);
  3315. pSMB->SetupCount = setup_count; /* no need to le convert byte fields */
  3316. pSMB->SubCommand = cpu_to_le16(sub_command);
  3317. return 0;
  3318. }
  3319. static int
  3320. validate_ntransact(char *buf, char **ppparm, char **ppdata,
  3321. __u32 *pparmlen, __u32 *pdatalen)
  3322. {
  3323. char *end_of_smb;
  3324. __u32 data_count, data_offset, parm_count, parm_offset;
  3325. struct smb_com_ntransact_rsp *pSMBr;
  3326. u16 bcc;
  3327. *pdatalen = 0;
  3328. *pparmlen = 0;
  3329. if (buf == NULL)
  3330. return -EINVAL;
  3331. pSMBr = (struct smb_com_ntransact_rsp *)buf;
  3332. bcc = get_bcc(&pSMBr->hdr);
  3333. end_of_smb = 2 /* sizeof byte count */ + bcc +
  3334. (char *)&pSMBr->ByteCount;
  3335. data_offset = le32_to_cpu(pSMBr->DataOffset);
  3336. data_count = le32_to_cpu(pSMBr->DataCount);
  3337. parm_offset = le32_to_cpu(pSMBr->ParameterOffset);
  3338. parm_count = le32_to_cpu(pSMBr->ParameterCount);
  3339. *ppparm = (char *)&pSMBr->hdr.Protocol + parm_offset;
  3340. *ppdata = (char *)&pSMBr->hdr.Protocol + data_offset;
  3341. /* should we also check that parm and data areas do not overlap? */
  3342. if (*ppparm > end_of_smb) {
  3343. cifs_dbg(FYI, "parms start after end of smb\n");
  3344. return -EINVAL;
  3345. } else if (parm_count + *ppparm > end_of_smb) {
  3346. cifs_dbg(FYI, "parm end after end of smb\n");
  3347. return -EINVAL;
  3348. } else if (*ppdata > end_of_smb) {
  3349. cifs_dbg(FYI, "data starts after end of smb\n");
  3350. return -EINVAL;
  3351. } else if (data_count + *ppdata > end_of_smb) {
  3352. cifs_dbg(FYI, "data %p + count %d (%p) past smb end %p start %p\n",
  3353. *ppdata, data_count, (data_count + *ppdata),
  3354. end_of_smb, pSMBr);
  3355. return -EINVAL;
  3356. } else if (parm_count + data_count > bcc) {
  3357. cifs_dbg(FYI, "parm count and data count larger than SMB\n");
  3358. return -EINVAL;
  3359. }
  3360. *pdatalen = data_count;
  3361. *pparmlen = parm_count;
  3362. return 0;
  3363. }
  3364. /* Get Security Descriptor (by handle) from remote server for a file or dir */
  3365. int
  3366. CIFSSMBGetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
  3367. struct cifs_ntsd **acl_inf, __u32 *pbuflen)
  3368. {
  3369. int rc = 0;
  3370. int buf_type = 0;
  3371. QUERY_SEC_DESC_REQ *pSMB;
  3372. struct kvec iov[1];
  3373. cifs_dbg(FYI, "GetCifsACL\n");
  3374. *pbuflen = 0;
  3375. *acl_inf = NULL;
  3376. rc = smb_init_nttransact(NT_TRANSACT_QUERY_SECURITY_DESC, 0,
  3377. 8 /* parm len */, tcon, (void **) &pSMB);
  3378. if (rc)
  3379. return rc;
  3380. pSMB->MaxParameterCount = cpu_to_le32(4);
  3381. /* BB TEST with big acls that might need to be e.g. larger than 16K */
  3382. pSMB->MaxSetupCount = 0;
  3383. pSMB->Fid = fid; /* file handle always le */
  3384. pSMB->AclFlags = cpu_to_le32(CIFS_ACL_OWNER | CIFS_ACL_GROUP |
  3385. CIFS_ACL_DACL);
  3386. pSMB->ByteCount = cpu_to_le16(11); /* 3 bytes pad + 8 bytes parm */
  3387. inc_rfc1001_len(pSMB, 11);
  3388. iov[0].iov_base = (char *)pSMB;
  3389. iov[0].iov_len = be32_to_cpu(pSMB->hdr.smb_buf_length) + 4;
  3390. rc = SendReceive2(xid, tcon->ses, iov, 1 /* num iovec */, &buf_type,
  3391. 0);
  3392. cifs_stats_inc(&tcon->stats.cifs_stats.num_acl_get);
  3393. if (rc) {
  3394. cifs_dbg(FYI, "Send error in QuerySecDesc = %d\n", rc);
  3395. } else { /* decode response */
  3396. __le32 *parm;
  3397. __u32 parm_len;
  3398. __u32 acl_len;
  3399. struct smb_com_ntransact_rsp *pSMBr;
  3400. char *pdata;
  3401. /* validate_nttransact */
  3402. rc = validate_ntransact(iov[0].iov_base, (char **)&parm,
  3403. &pdata, &parm_len, pbuflen);
  3404. if (rc)
  3405. goto qsec_out;
  3406. pSMBr = (struct smb_com_ntransact_rsp *)iov[0].iov_base;
  3407. cifs_dbg(FYI, "smb %p parm %p data %p\n",
  3408. pSMBr, parm, *acl_inf);
  3409. if (le32_to_cpu(pSMBr->ParameterCount) != 4) {
  3410. rc = -EIO; /* bad smb */
  3411. *pbuflen = 0;
  3412. goto qsec_out;
  3413. }
  3414. /* BB check that data area is minimum length and as big as acl_len */
  3415. acl_len = le32_to_cpu(*parm);
  3416. if (acl_len != *pbuflen) {
  3417. cifs_dbg(VFS, "acl length %d does not match %d\n",
  3418. acl_len, *pbuflen);
  3419. if (*pbuflen > acl_len)
  3420. *pbuflen = acl_len;
  3421. }
  3422. /* check if buffer is big enough for the acl
  3423. header followed by the smallest SID */
  3424. if ((*pbuflen < sizeof(struct cifs_ntsd) + 8) ||
  3425. (*pbuflen >= 64 * 1024)) {
  3426. cifs_dbg(VFS, "bad acl length %d\n", *pbuflen);
  3427. rc = -EINVAL;
  3428. *pbuflen = 0;
  3429. } else {
  3430. *acl_inf = kmemdup(pdata, *pbuflen, GFP_KERNEL);
  3431. if (*acl_inf == NULL) {
  3432. *pbuflen = 0;
  3433. rc = -ENOMEM;
  3434. }
  3435. }
  3436. }
  3437. qsec_out:
  3438. free_rsp_buf(buf_type, iov[0].iov_base);
  3439. /* cifs_small_buf_release(pSMB); */ /* Freed earlier now in SendReceive2 */
  3440. return rc;
  3441. }
  3442. int
  3443. CIFSSMBSetCIFSACL(const unsigned int xid, struct cifs_tcon *tcon, __u16 fid,
  3444. struct cifs_ntsd *pntsd, __u32 acllen, int aclflag)
  3445. {
  3446. __u16 byte_count, param_count, data_count, param_offset, data_offset;
  3447. int rc = 0;
  3448. int bytes_returned = 0;
  3449. SET_SEC_DESC_REQ *pSMB = NULL;
  3450. void *pSMBr;
  3451. setCifsAclRetry:
  3452. rc = smb_init(SMB_COM_NT_TRANSACT, 19, tcon, (void **) &pSMB, &pSMBr);
  3453. if (rc)
  3454. return rc;
  3455. pSMB->MaxSetupCount = 0;
  3456. pSMB->Reserved = 0;
  3457. param_count = 8;
  3458. param_offset = offsetof(struct smb_com_transaction_ssec_req, Fid) - 4;
  3459. data_count = acllen;
  3460. data_offset = param_offset + param_count;
  3461. byte_count = 3 /* pad */ + param_count;
  3462. pSMB->DataCount = cpu_to_le32(data_count);
  3463. pSMB->TotalDataCount = pSMB->DataCount;
  3464. pSMB->MaxParameterCount = cpu_to_le32(4);
  3465. pSMB->MaxDataCount = cpu_to_le32(16384);
  3466. pSMB->ParameterCount = cpu_to_le32(param_count);
  3467. pSMB->ParameterOffset = cpu_to_le32(param_offset);
  3468. pSMB->TotalParameterCount = pSMB->ParameterCount;
  3469. pSMB->DataOffset = cpu_to_le32(data_offset);
  3470. pSMB->SetupCount = 0;
  3471. pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_SET_SECURITY_DESC);
  3472. pSMB->ByteCount = cpu_to_le16(byte_count+data_count);
  3473. pSMB->Fid = fid; /* file handle always le */
  3474. pSMB->Reserved2 = 0;
  3475. pSMB->AclFlags = cpu_to_le32(aclflag);
  3476. if (pntsd && acllen) {
  3477. memcpy((char *)pSMBr + offsetof(struct smb_hdr, Protocol) +
  3478. data_offset, pntsd, acllen);
  3479. inc_rfc1001_len(pSMB, byte_count + data_count);
  3480. } else
  3481. inc_rfc1001_len(pSMB, byte_count);
  3482. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  3483. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  3484. cifs_dbg(FYI, "SetCIFSACL bytes_returned: %d, rc: %d\n",
  3485. bytes_returned, rc);
  3486. if (rc)
  3487. cifs_dbg(FYI, "Set CIFS ACL returned %d\n", rc);
  3488. cifs_buf_release(pSMB);
  3489. if (rc == -EAGAIN)
  3490. goto setCifsAclRetry;
  3491. return (rc);
  3492. }
  3493. #endif /* CONFIG_CIFS_ACL */
  3494. /* Legacy Query Path Information call for lookup to old servers such
  3495. as Win9x/WinME */
  3496. int
  3497. SMBQueryInformation(const unsigned int xid, struct cifs_tcon *tcon,
  3498. const char *search_name, FILE_ALL_INFO *data,
  3499. const struct nls_table *nls_codepage, int remap)
  3500. {
  3501. QUERY_INFORMATION_REQ *pSMB;
  3502. QUERY_INFORMATION_RSP *pSMBr;
  3503. int rc = 0;
  3504. int bytes_returned;
  3505. int name_len;
  3506. cifs_dbg(FYI, "In SMBQPath path %s\n", search_name);
  3507. QInfRetry:
  3508. rc = smb_init(SMB_COM_QUERY_INFORMATION, 0, tcon, (void **) &pSMB,
  3509. (void **) &pSMBr);
  3510. if (rc)
  3511. return rc;
  3512. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  3513. name_len =
  3514. cifsConvertToUTF16((__le16 *) pSMB->FileName,
  3515. search_name, PATH_MAX, nls_codepage,
  3516. remap);
  3517. name_len++; /* trailing null */
  3518. name_len *= 2;
  3519. } else {
  3520. name_len = strnlen(search_name, PATH_MAX);
  3521. name_len++; /* trailing null */
  3522. strncpy(pSMB->FileName, search_name, name_len);
  3523. }
  3524. pSMB->BufferFormat = 0x04;
  3525. name_len++; /* account for buffer type byte */
  3526. inc_rfc1001_len(pSMB, (__u16)name_len);
  3527. pSMB->ByteCount = cpu_to_le16(name_len);
  3528. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  3529. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  3530. if (rc) {
  3531. cifs_dbg(FYI, "Send error in QueryInfo = %d\n", rc);
  3532. } else if (data) {
  3533. struct timespec ts;
  3534. __u32 time = le32_to_cpu(pSMBr->last_write_time);
  3535. /* decode response */
  3536. /* BB FIXME - add time zone adjustment BB */
  3537. memset(data, 0, sizeof(FILE_ALL_INFO));
  3538. ts.tv_nsec = 0;
  3539. ts.tv_sec = time;
  3540. /* decode time fields */
  3541. data->ChangeTime = cpu_to_le64(cifs_UnixTimeToNT(ts));
  3542. data->LastWriteTime = data->ChangeTime;
  3543. data->LastAccessTime = 0;
  3544. data->AllocationSize =
  3545. cpu_to_le64(le32_to_cpu(pSMBr->size));
  3546. data->EndOfFile = data->AllocationSize;
  3547. data->Attributes =
  3548. cpu_to_le32(le16_to_cpu(pSMBr->attr));
  3549. } else
  3550. rc = -EIO; /* bad buffer passed in */
  3551. cifs_buf_release(pSMB);
  3552. if (rc == -EAGAIN)
  3553. goto QInfRetry;
  3554. return rc;
  3555. }
  3556. int
  3557. CIFSSMBQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
  3558. u16 netfid, FILE_ALL_INFO *pFindData)
  3559. {
  3560. struct smb_t2_qfi_req *pSMB = NULL;
  3561. struct smb_t2_qfi_rsp *pSMBr = NULL;
  3562. int rc = 0;
  3563. int bytes_returned;
  3564. __u16 params, byte_count;
  3565. QFileInfoRetry:
  3566. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  3567. (void **) &pSMBr);
  3568. if (rc)
  3569. return rc;
  3570. params = 2 /* level */ + 2 /* fid */;
  3571. pSMB->t2.TotalDataCount = 0;
  3572. pSMB->t2.MaxParameterCount = cpu_to_le16(4);
  3573. /* BB find exact max data count below from sess structure BB */
  3574. pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
  3575. pSMB->t2.MaxSetupCount = 0;
  3576. pSMB->t2.Reserved = 0;
  3577. pSMB->t2.Flags = 0;
  3578. pSMB->t2.Timeout = 0;
  3579. pSMB->t2.Reserved2 = 0;
  3580. pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
  3581. Fid) - 4);
  3582. pSMB->t2.DataCount = 0;
  3583. pSMB->t2.DataOffset = 0;
  3584. pSMB->t2.SetupCount = 1;
  3585. pSMB->t2.Reserved3 = 0;
  3586. pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
  3587. byte_count = params + 1 /* pad */ ;
  3588. pSMB->t2.TotalParameterCount = cpu_to_le16(params);
  3589. pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
  3590. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
  3591. pSMB->Pad = 0;
  3592. pSMB->Fid = netfid;
  3593. inc_rfc1001_len(pSMB, byte_count);
  3594. pSMB->t2.ByteCount = cpu_to_le16(byte_count);
  3595. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  3596. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  3597. if (rc) {
  3598. cifs_dbg(FYI, "Send error in QFileInfo = %d", rc);
  3599. } else { /* decode response */
  3600. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  3601. if (rc) /* BB add auto retry on EOPNOTSUPP? */
  3602. rc = -EIO;
  3603. else if (get_bcc(&pSMBr->hdr) < 40)
  3604. rc = -EIO; /* bad smb */
  3605. else if (pFindData) {
  3606. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  3607. memcpy((char *) pFindData,
  3608. (char *) &pSMBr->hdr.Protocol +
  3609. data_offset, sizeof(FILE_ALL_INFO));
  3610. } else
  3611. rc = -ENOMEM;
  3612. }
  3613. cifs_buf_release(pSMB);
  3614. if (rc == -EAGAIN)
  3615. goto QFileInfoRetry;
  3616. return rc;
  3617. }
  3618. int
  3619. CIFSSMBQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
  3620. const char *search_name, FILE_ALL_INFO *data,
  3621. int legacy /* old style infolevel */,
  3622. const struct nls_table *nls_codepage, int remap)
  3623. {
  3624. /* level 263 SMB_QUERY_FILE_ALL_INFO */
  3625. TRANSACTION2_QPI_REQ *pSMB = NULL;
  3626. TRANSACTION2_QPI_RSP *pSMBr = NULL;
  3627. int rc = 0;
  3628. int bytes_returned;
  3629. int name_len;
  3630. __u16 params, byte_count;
  3631. /* cifs_dbg(FYI, "In QPathInfo path %s\n", search_name); */
  3632. QPathInfoRetry:
  3633. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  3634. (void **) &pSMBr);
  3635. if (rc)
  3636. return rc;
  3637. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  3638. name_len =
  3639. cifsConvertToUTF16((__le16 *) pSMB->FileName, search_name,
  3640. PATH_MAX, nls_codepage, remap);
  3641. name_len++; /* trailing null */
  3642. name_len *= 2;
  3643. } else { /* BB improve the check for buffer overruns BB */
  3644. name_len = strnlen(search_name, PATH_MAX);
  3645. name_len++; /* trailing null */
  3646. strncpy(pSMB->FileName, search_name, name_len);
  3647. }
  3648. params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
  3649. pSMB->TotalDataCount = 0;
  3650. pSMB->MaxParameterCount = cpu_to_le16(2);
  3651. /* BB find exact max SMB PDU from sess structure BB */
  3652. pSMB->MaxDataCount = cpu_to_le16(4000);
  3653. pSMB->MaxSetupCount = 0;
  3654. pSMB->Reserved = 0;
  3655. pSMB->Flags = 0;
  3656. pSMB->Timeout = 0;
  3657. pSMB->Reserved2 = 0;
  3658. pSMB->ParameterOffset = cpu_to_le16(offsetof(
  3659. struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
  3660. pSMB->DataCount = 0;
  3661. pSMB->DataOffset = 0;
  3662. pSMB->SetupCount = 1;
  3663. pSMB->Reserved3 = 0;
  3664. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
  3665. byte_count = params + 1 /* pad */ ;
  3666. pSMB->TotalParameterCount = cpu_to_le16(params);
  3667. pSMB->ParameterCount = pSMB->TotalParameterCount;
  3668. if (legacy)
  3669. pSMB->InformationLevel = cpu_to_le16(SMB_INFO_STANDARD);
  3670. else
  3671. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_ALL_INFO);
  3672. pSMB->Reserved4 = 0;
  3673. inc_rfc1001_len(pSMB, byte_count);
  3674. pSMB->ByteCount = cpu_to_le16(byte_count);
  3675. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  3676. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  3677. if (rc) {
  3678. cifs_dbg(FYI, "Send error in QPathInfo = %d\n", rc);
  3679. } else { /* decode response */
  3680. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  3681. if (rc) /* BB add auto retry on EOPNOTSUPP? */
  3682. rc = -EIO;
  3683. else if (!legacy && get_bcc(&pSMBr->hdr) < 40)
  3684. rc = -EIO; /* bad smb */
  3685. else if (legacy && get_bcc(&pSMBr->hdr) < 24)
  3686. rc = -EIO; /* 24 or 26 expected but we do not read
  3687. last field */
  3688. else if (data) {
  3689. int size;
  3690. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  3691. /*
  3692. * On legacy responses we do not read the last field,
  3693. * EAsize, fortunately since it varies by subdialect and
  3694. * also note it differs on Set vs Get, ie two bytes or 4
  3695. * bytes depending but we don't care here.
  3696. */
  3697. if (legacy)
  3698. size = sizeof(FILE_INFO_STANDARD);
  3699. else
  3700. size = sizeof(FILE_ALL_INFO);
  3701. memcpy((char *) data, (char *) &pSMBr->hdr.Protocol +
  3702. data_offset, size);
  3703. } else
  3704. rc = -ENOMEM;
  3705. }
  3706. cifs_buf_release(pSMB);
  3707. if (rc == -EAGAIN)
  3708. goto QPathInfoRetry;
  3709. return rc;
  3710. }
  3711. int
  3712. CIFSSMBUnixQFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
  3713. u16 netfid, FILE_UNIX_BASIC_INFO *pFindData)
  3714. {
  3715. struct smb_t2_qfi_req *pSMB = NULL;
  3716. struct smb_t2_qfi_rsp *pSMBr = NULL;
  3717. int rc = 0;
  3718. int bytes_returned;
  3719. __u16 params, byte_count;
  3720. UnixQFileInfoRetry:
  3721. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  3722. (void **) &pSMBr);
  3723. if (rc)
  3724. return rc;
  3725. params = 2 /* level */ + 2 /* fid */;
  3726. pSMB->t2.TotalDataCount = 0;
  3727. pSMB->t2.MaxParameterCount = cpu_to_le16(4);
  3728. /* BB find exact max data count below from sess structure BB */
  3729. pSMB->t2.MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
  3730. pSMB->t2.MaxSetupCount = 0;
  3731. pSMB->t2.Reserved = 0;
  3732. pSMB->t2.Flags = 0;
  3733. pSMB->t2.Timeout = 0;
  3734. pSMB->t2.Reserved2 = 0;
  3735. pSMB->t2.ParameterOffset = cpu_to_le16(offsetof(struct smb_t2_qfi_req,
  3736. Fid) - 4);
  3737. pSMB->t2.DataCount = 0;
  3738. pSMB->t2.DataOffset = 0;
  3739. pSMB->t2.SetupCount = 1;
  3740. pSMB->t2.Reserved3 = 0;
  3741. pSMB->t2.SubCommand = cpu_to_le16(TRANS2_QUERY_FILE_INFORMATION);
  3742. byte_count = params + 1 /* pad */ ;
  3743. pSMB->t2.TotalParameterCount = cpu_to_le16(params);
  3744. pSMB->t2.ParameterCount = pSMB->t2.TotalParameterCount;
  3745. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
  3746. pSMB->Pad = 0;
  3747. pSMB->Fid = netfid;
  3748. inc_rfc1001_len(pSMB, byte_count);
  3749. pSMB->t2.ByteCount = cpu_to_le16(byte_count);
  3750. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  3751. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  3752. if (rc) {
  3753. cifs_dbg(FYI, "Send error in UnixQFileInfo = %d", rc);
  3754. } else { /* decode response */
  3755. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  3756. if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
  3757. cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
  3758. rc = -EIO; /* bad smb */
  3759. } else {
  3760. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  3761. memcpy((char *) pFindData,
  3762. (char *) &pSMBr->hdr.Protocol +
  3763. data_offset,
  3764. sizeof(FILE_UNIX_BASIC_INFO));
  3765. }
  3766. }
  3767. cifs_buf_release(pSMB);
  3768. if (rc == -EAGAIN)
  3769. goto UnixQFileInfoRetry;
  3770. return rc;
  3771. }
  3772. int
  3773. CIFSSMBUnixQPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
  3774. const unsigned char *searchName,
  3775. FILE_UNIX_BASIC_INFO *pFindData,
  3776. const struct nls_table *nls_codepage, int remap)
  3777. {
  3778. /* SMB_QUERY_FILE_UNIX_BASIC */
  3779. TRANSACTION2_QPI_REQ *pSMB = NULL;
  3780. TRANSACTION2_QPI_RSP *pSMBr = NULL;
  3781. int rc = 0;
  3782. int bytes_returned = 0;
  3783. int name_len;
  3784. __u16 params, byte_count;
  3785. cifs_dbg(FYI, "In QPathInfo (Unix) the path %s\n", searchName);
  3786. UnixQPathInfoRetry:
  3787. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  3788. (void **) &pSMBr);
  3789. if (rc)
  3790. return rc;
  3791. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  3792. name_len =
  3793. cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
  3794. PATH_MAX, nls_codepage, remap);
  3795. name_len++; /* trailing null */
  3796. name_len *= 2;
  3797. } else { /* BB improve the check for buffer overruns BB */
  3798. name_len = strnlen(searchName, PATH_MAX);
  3799. name_len++; /* trailing null */
  3800. strncpy(pSMB->FileName, searchName, name_len);
  3801. }
  3802. params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
  3803. pSMB->TotalDataCount = 0;
  3804. pSMB->MaxParameterCount = cpu_to_le16(2);
  3805. /* BB find exact max SMB PDU from sess structure BB */
  3806. pSMB->MaxDataCount = cpu_to_le16(4000);
  3807. pSMB->MaxSetupCount = 0;
  3808. pSMB->Reserved = 0;
  3809. pSMB->Flags = 0;
  3810. pSMB->Timeout = 0;
  3811. pSMB->Reserved2 = 0;
  3812. pSMB->ParameterOffset = cpu_to_le16(offsetof(
  3813. struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
  3814. pSMB->DataCount = 0;
  3815. pSMB->DataOffset = 0;
  3816. pSMB->SetupCount = 1;
  3817. pSMB->Reserved3 = 0;
  3818. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
  3819. byte_count = params + 1 /* pad */ ;
  3820. pSMB->TotalParameterCount = cpu_to_le16(params);
  3821. pSMB->ParameterCount = pSMB->TotalParameterCount;
  3822. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_UNIX_BASIC);
  3823. pSMB->Reserved4 = 0;
  3824. inc_rfc1001_len(pSMB, byte_count);
  3825. pSMB->ByteCount = cpu_to_le16(byte_count);
  3826. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  3827. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  3828. if (rc) {
  3829. cifs_dbg(FYI, "Send error in UnixQPathInfo = %d", rc);
  3830. } else { /* decode response */
  3831. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  3832. if (rc || get_bcc(&pSMBr->hdr) < sizeof(FILE_UNIX_BASIC_INFO)) {
  3833. cifs_dbg(VFS, "Malformed FILE_UNIX_BASIC_INFO response. Unix Extensions can be disabled on mount by specifying the nosfu mount option.\n");
  3834. rc = -EIO; /* bad smb */
  3835. } else {
  3836. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  3837. memcpy((char *) pFindData,
  3838. (char *) &pSMBr->hdr.Protocol +
  3839. data_offset,
  3840. sizeof(FILE_UNIX_BASIC_INFO));
  3841. }
  3842. }
  3843. cifs_buf_release(pSMB);
  3844. if (rc == -EAGAIN)
  3845. goto UnixQPathInfoRetry;
  3846. return rc;
  3847. }
  3848. /* xid, tcon, searchName and codepage are input parms, rest are returned */
  3849. int
  3850. CIFSFindFirst(const unsigned int xid, struct cifs_tcon *tcon,
  3851. const char *searchName, struct cifs_sb_info *cifs_sb,
  3852. __u16 *pnetfid, __u16 search_flags,
  3853. struct cifs_search_info *psrch_inf, bool msearch)
  3854. {
  3855. /* level 257 SMB_ */
  3856. TRANSACTION2_FFIRST_REQ *pSMB = NULL;
  3857. TRANSACTION2_FFIRST_RSP *pSMBr = NULL;
  3858. T2_FFIRST_RSP_PARMS *parms;
  3859. int rc = 0;
  3860. int bytes_returned = 0;
  3861. int name_len, remap;
  3862. __u16 params, byte_count;
  3863. struct nls_table *nls_codepage;
  3864. cifs_dbg(FYI, "In FindFirst for %s\n", searchName);
  3865. findFirstRetry:
  3866. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  3867. (void **) &pSMBr);
  3868. if (rc)
  3869. return rc;
  3870. nls_codepage = cifs_sb->local_nls;
  3871. remap = cifs_remap(cifs_sb);
  3872. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  3873. name_len =
  3874. cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
  3875. PATH_MAX, nls_codepage, remap);
  3876. /* We can not add the asterik earlier in case
  3877. it got remapped to 0xF03A as if it were part of the
  3878. directory name instead of a wildcard */
  3879. name_len *= 2;
  3880. if (msearch) {
  3881. pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
  3882. pSMB->FileName[name_len+1] = 0;
  3883. pSMB->FileName[name_len+2] = '*';
  3884. pSMB->FileName[name_len+3] = 0;
  3885. name_len += 4; /* now the trailing null */
  3886. /* null terminate just in case */
  3887. pSMB->FileName[name_len] = 0;
  3888. pSMB->FileName[name_len+1] = 0;
  3889. name_len += 2;
  3890. }
  3891. } else { /* BB add check for overrun of SMB buf BB */
  3892. name_len = strnlen(searchName, PATH_MAX);
  3893. /* BB fix here and in unicode clause above ie
  3894. if (name_len > buffersize-header)
  3895. free buffer exit; BB */
  3896. strncpy(pSMB->FileName, searchName, name_len);
  3897. if (msearch) {
  3898. pSMB->FileName[name_len] = CIFS_DIR_SEP(cifs_sb);
  3899. pSMB->FileName[name_len+1] = '*';
  3900. pSMB->FileName[name_len+2] = 0;
  3901. name_len += 3;
  3902. }
  3903. }
  3904. params = 12 + name_len /* includes null */ ;
  3905. pSMB->TotalDataCount = 0; /* no EAs */
  3906. pSMB->MaxParameterCount = cpu_to_le16(10);
  3907. pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
  3908. pSMB->MaxSetupCount = 0;
  3909. pSMB->Reserved = 0;
  3910. pSMB->Flags = 0;
  3911. pSMB->Timeout = 0;
  3912. pSMB->Reserved2 = 0;
  3913. byte_count = params + 1 /* pad */ ;
  3914. pSMB->TotalParameterCount = cpu_to_le16(params);
  3915. pSMB->ParameterCount = pSMB->TotalParameterCount;
  3916. pSMB->ParameterOffset = cpu_to_le16(
  3917. offsetof(struct smb_com_transaction2_ffirst_req, SearchAttributes)
  3918. - 4);
  3919. pSMB->DataCount = 0;
  3920. pSMB->DataOffset = 0;
  3921. pSMB->SetupCount = 1; /* one byte, no need to make endian neutral */
  3922. pSMB->Reserved3 = 0;
  3923. pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_FIRST);
  3924. pSMB->SearchAttributes =
  3925. cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM |
  3926. ATTR_DIRECTORY);
  3927. pSMB->SearchCount = cpu_to_le16(CIFSMaxBufSize/sizeof(FILE_UNIX_INFO));
  3928. pSMB->SearchFlags = cpu_to_le16(search_flags);
  3929. pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
  3930. /* BB what should we set StorageType to? Does it matter? BB */
  3931. pSMB->SearchStorageType = 0;
  3932. inc_rfc1001_len(pSMB, byte_count);
  3933. pSMB->ByteCount = cpu_to_le16(byte_count);
  3934. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  3935. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  3936. cifs_stats_inc(&tcon->stats.cifs_stats.num_ffirst);
  3937. if (rc) {/* BB add logic to retry regular search if Unix search
  3938. rejected unexpectedly by server */
  3939. /* BB Add code to handle unsupported level rc */
  3940. cifs_dbg(FYI, "Error in FindFirst = %d\n", rc);
  3941. cifs_buf_release(pSMB);
  3942. /* BB eventually could optimize out free and realloc of buf */
  3943. /* for this case */
  3944. if (rc == -EAGAIN)
  3945. goto findFirstRetry;
  3946. } else { /* decode response */
  3947. /* BB remember to free buffer if error BB */
  3948. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  3949. if (rc == 0) {
  3950. unsigned int lnoff;
  3951. if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
  3952. psrch_inf->unicode = true;
  3953. else
  3954. psrch_inf->unicode = false;
  3955. psrch_inf->ntwrk_buf_start = (char *)pSMBr;
  3956. psrch_inf->smallBuf = 0;
  3957. psrch_inf->srch_entries_start =
  3958. (char *) &pSMBr->hdr.Protocol +
  3959. le16_to_cpu(pSMBr->t2.DataOffset);
  3960. parms = (T2_FFIRST_RSP_PARMS *)((char *) &pSMBr->hdr.Protocol +
  3961. le16_to_cpu(pSMBr->t2.ParameterOffset));
  3962. if (parms->EndofSearch)
  3963. psrch_inf->endOfSearch = true;
  3964. else
  3965. psrch_inf->endOfSearch = false;
  3966. psrch_inf->entries_in_buffer =
  3967. le16_to_cpu(parms->SearchCount);
  3968. psrch_inf->index_of_last_entry = 2 /* skip . and .. */ +
  3969. psrch_inf->entries_in_buffer;
  3970. lnoff = le16_to_cpu(parms->LastNameOffset);
  3971. if (CIFSMaxBufSize < lnoff) {
  3972. cifs_dbg(VFS, "ignoring corrupt resume name\n");
  3973. psrch_inf->last_entry = NULL;
  3974. return rc;
  3975. }
  3976. psrch_inf->last_entry = psrch_inf->srch_entries_start +
  3977. lnoff;
  3978. if (pnetfid)
  3979. *pnetfid = parms->SearchHandle;
  3980. } else {
  3981. cifs_buf_release(pSMB);
  3982. }
  3983. }
  3984. return rc;
  3985. }
  3986. int CIFSFindNext(const unsigned int xid, struct cifs_tcon *tcon,
  3987. __u16 searchHandle, __u16 search_flags,
  3988. struct cifs_search_info *psrch_inf)
  3989. {
  3990. TRANSACTION2_FNEXT_REQ *pSMB = NULL;
  3991. TRANSACTION2_FNEXT_RSP *pSMBr = NULL;
  3992. T2_FNEXT_RSP_PARMS *parms;
  3993. char *response_data;
  3994. int rc = 0;
  3995. int bytes_returned;
  3996. unsigned int name_len;
  3997. __u16 params, byte_count;
  3998. cifs_dbg(FYI, "In FindNext\n");
  3999. if (psrch_inf->endOfSearch)
  4000. return -ENOENT;
  4001. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  4002. (void **) &pSMBr);
  4003. if (rc)
  4004. return rc;
  4005. params = 14; /* includes 2 bytes of null string, converted to LE below*/
  4006. byte_count = 0;
  4007. pSMB->TotalDataCount = 0; /* no EAs */
  4008. pSMB->MaxParameterCount = cpu_to_le16(8);
  4009. pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize & 0xFFFFFF00);
  4010. pSMB->MaxSetupCount = 0;
  4011. pSMB->Reserved = 0;
  4012. pSMB->Flags = 0;
  4013. pSMB->Timeout = 0;
  4014. pSMB->Reserved2 = 0;
  4015. pSMB->ParameterOffset = cpu_to_le16(
  4016. offsetof(struct smb_com_transaction2_fnext_req,SearchHandle) - 4);
  4017. pSMB->DataCount = 0;
  4018. pSMB->DataOffset = 0;
  4019. pSMB->SetupCount = 1;
  4020. pSMB->Reserved3 = 0;
  4021. pSMB->SubCommand = cpu_to_le16(TRANS2_FIND_NEXT);
  4022. pSMB->SearchHandle = searchHandle; /* always kept as le */
  4023. pSMB->SearchCount =
  4024. cpu_to_le16(CIFSMaxBufSize / sizeof(FILE_UNIX_INFO));
  4025. pSMB->InformationLevel = cpu_to_le16(psrch_inf->info_level);
  4026. pSMB->ResumeKey = psrch_inf->resume_key;
  4027. pSMB->SearchFlags = cpu_to_le16(search_flags);
  4028. name_len = psrch_inf->resume_name_len;
  4029. params += name_len;
  4030. if (name_len < PATH_MAX) {
  4031. memcpy(pSMB->ResumeFileName, psrch_inf->presume_name, name_len);
  4032. byte_count += name_len;
  4033. /* 14 byte parm len above enough for 2 byte null terminator */
  4034. pSMB->ResumeFileName[name_len] = 0;
  4035. pSMB->ResumeFileName[name_len+1] = 0;
  4036. } else {
  4037. rc = -EINVAL;
  4038. goto FNext2_err_exit;
  4039. }
  4040. byte_count = params + 1 /* pad */ ;
  4041. pSMB->TotalParameterCount = cpu_to_le16(params);
  4042. pSMB->ParameterCount = pSMB->TotalParameterCount;
  4043. inc_rfc1001_len(pSMB, byte_count);
  4044. pSMB->ByteCount = cpu_to_le16(byte_count);
  4045. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  4046. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  4047. cifs_stats_inc(&tcon->stats.cifs_stats.num_fnext);
  4048. if (rc) {
  4049. if (rc == -EBADF) {
  4050. psrch_inf->endOfSearch = true;
  4051. cifs_buf_release(pSMB);
  4052. rc = 0; /* search probably was closed at end of search*/
  4053. } else
  4054. cifs_dbg(FYI, "FindNext returned = %d\n", rc);
  4055. } else { /* decode response */
  4056. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  4057. if (rc == 0) {
  4058. unsigned int lnoff;
  4059. /* BB fixme add lock for file (srch_info) struct here */
  4060. if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
  4061. psrch_inf->unicode = true;
  4062. else
  4063. psrch_inf->unicode = false;
  4064. response_data = (char *) &pSMBr->hdr.Protocol +
  4065. le16_to_cpu(pSMBr->t2.ParameterOffset);
  4066. parms = (T2_FNEXT_RSP_PARMS *)response_data;
  4067. response_data = (char *)&pSMBr->hdr.Protocol +
  4068. le16_to_cpu(pSMBr->t2.DataOffset);
  4069. if (psrch_inf->smallBuf)
  4070. cifs_small_buf_release(
  4071. psrch_inf->ntwrk_buf_start);
  4072. else
  4073. cifs_buf_release(psrch_inf->ntwrk_buf_start);
  4074. psrch_inf->srch_entries_start = response_data;
  4075. psrch_inf->ntwrk_buf_start = (char *)pSMB;
  4076. psrch_inf->smallBuf = 0;
  4077. if (parms->EndofSearch)
  4078. psrch_inf->endOfSearch = true;
  4079. else
  4080. psrch_inf->endOfSearch = false;
  4081. psrch_inf->entries_in_buffer =
  4082. le16_to_cpu(parms->SearchCount);
  4083. psrch_inf->index_of_last_entry +=
  4084. psrch_inf->entries_in_buffer;
  4085. lnoff = le16_to_cpu(parms->LastNameOffset);
  4086. if (CIFSMaxBufSize < lnoff) {
  4087. cifs_dbg(VFS, "ignoring corrupt resume name\n");
  4088. psrch_inf->last_entry = NULL;
  4089. return rc;
  4090. } else
  4091. psrch_inf->last_entry =
  4092. psrch_inf->srch_entries_start + lnoff;
  4093. /* cifs_dbg(FYI, "fnxt2 entries in buf %d index_of_last %d\n",
  4094. psrch_inf->entries_in_buffer, psrch_inf->index_of_last_entry); */
  4095. /* BB fixme add unlock here */
  4096. }
  4097. }
  4098. /* BB On error, should we leave previous search buf (and count and
  4099. last entry fields) intact or free the previous one? */
  4100. /* Note: On -EAGAIN error only caller can retry on handle based calls
  4101. since file handle passed in no longer valid */
  4102. FNext2_err_exit:
  4103. if (rc != 0)
  4104. cifs_buf_release(pSMB);
  4105. return rc;
  4106. }
  4107. int
  4108. CIFSFindClose(const unsigned int xid, struct cifs_tcon *tcon,
  4109. const __u16 searchHandle)
  4110. {
  4111. int rc = 0;
  4112. FINDCLOSE_REQ *pSMB = NULL;
  4113. cifs_dbg(FYI, "In CIFSSMBFindClose\n");
  4114. rc = small_smb_init(SMB_COM_FIND_CLOSE2, 1, tcon, (void **)&pSMB);
  4115. /* no sense returning error if session restarted
  4116. as file handle has been closed */
  4117. if (rc == -EAGAIN)
  4118. return 0;
  4119. if (rc)
  4120. return rc;
  4121. pSMB->FileID = searchHandle;
  4122. pSMB->ByteCount = 0;
  4123. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
  4124. if (rc)
  4125. cifs_dbg(VFS, "Send error in FindClose = %d\n", rc);
  4126. cifs_stats_inc(&tcon->stats.cifs_stats.num_fclose);
  4127. /* Since session is dead, search handle closed on server already */
  4128. if (rc == -EAGAIN)
  4129. rc = 0;
  4130. return rc;
  4131. }
  4132. int
  4133. CIFSGetSrvInodeNumber(const unsigned int xid, struct cifs_tcon *tcon,
  4134. const char *search_name, __u64 *inode_number,
  4135. const struct nls_table *nls_codepage, int remap)
  4136. {
  4137. int rc = 0;
  4138. TRANSACTION2_QPI_REQ *pSMB = NULL;
  4139. TRANSACTION2_QPI_RSP *pSMBr = NULL;
  4140. int name_len, bytes_returned;
  4141. __u16 params, byte_count;
  4142. cifs_dbg(FYI, "In GetSrvInodeNum for %s\n", search_name);
  4143. if (tcon == NULL)
  4144. return -ENODEV;
  4145. GetInodeNumberRetry:
  4146. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  4147. (void **) &pSMBr);
  4148. if (rc)
  4149. return rc;
  4150. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  4151. name_len =
  4152. cifsConvertToUTF16((__le16 *) pSMB->FileName,
  4153. search_name, PATH_MAX, nls_codepage,
  4154. remap);
  4155. name_len++; /* trailing null */
  4156. name_len *= 2;
  4157. } else { /* BB improve the check for buffer overruns BB */
  4158. name_len = strnlen(search_name, PATH_MAX);
  4159. name_len++; /* trailing null */
  4160. strncpy(pSMB->FileName, search_name, name_len);
  4161. }
  4162. params = 2 /* level */ + 4 /* rsrvd */ + name_len /* incl null */ ;
  4163. pSMB->TotalDataCount = 0;
  4164. pSMB->MaxParameterCount = cpu_to_le16(2);
  4165. /* BB find exact max data count below from sess structure BB */
  4166. pSMB->MaxDataCount = cpu_to_le16(4000);
  4167. pSMB->MaxSetupCount = 0;
  4168. pSMB->Reserved = 0;
  4169. pSMB->Flags = 0;
  4170. pSMB->Timeout = 0;
  4171. pSMB->Reserved2 = 0;
  4172. pSMB->ParameterOffset = cpu_to_le16(offsetof(
  4173. struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
  4174. pSMB->DataCount = 0;
  4175. pSMB->DataOffset = 0;
  4176. pSMB->SetupCount = 1;
  4177. pSMB->Reserved3 = 0;
  4178. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
  4179. byte_count = params + 1 /* pad */ ;
  4180. pSMB->TotalParameterCount = cpu_to_le16(params);
  4181. pSMB->ParameterCount = pSMB->TotalParameterCount;
  4182. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FILE_INTERNAL_INFO);
  4183. pSMB->Reserved4 = 0;
  4184. inc_rfc1001_len(pSMB, byte_count);
  4185. pSMB->ByteCount = cpu_to_le16(byte_count);
  4186. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  4187. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  4188. if (rc) {
  4189. cifs_dbg(FYI, "error %d in QueryInternalInfo\n", rc);
  4190. } else {
  4191. /* decode response */
  4192. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  4193. /* BB also check enough total bytes returned */
  4194. if (rc || get_bcc(&pSMBr->hdr) < 2)
  4195. /* If rc should we check for EOPNOSUPP and
  4196. disable the srvino flag? or in caller? */
  4197. rc = -EIO; /* bad smb */
  4198. else {
  4199. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  4200. __u16 count = le16_to_cpu(pSMBr->t2.DataCount);
  4201. struct file_internal_info *pfinfo;
  4202. /* BB Do we need a cast or hash here ? */
  4203. if (count < 8) {
  4204. cifs_dbg(FYI, "Illegal size ret in QryIntrnlInf\n");
  4205. rc = -EIO;
  4206. goto GetInodeNumOut;
  4207. }
  4208. pfinfo = (struct file_internal_info *)
  4209. (data_offset + (char *) &pSMBr->hdr.Protocol);
  4210. *inode_number = le64_to_cpu(pfinfo->UniqueId);
  4211. }
  4212. }
  4213. GetInodeNumOut:
  4214. cifs_buf_release(pSMB);
  4215. if (rc == -EAGAIN)
  4216. goto GetInodeNumberRetry;
  4217. return rc;
  4218. }
  4219. /* parses DFS refferal V3 structure
  4220. * caller is responsible for freeing target_nodes
  4221. * returns:
  4222. * on success - 0
  4223. * on failure - errno
  4224. */
  4225. static int
  4226. parse_DFS_referrals(TRANSACTION2_GET_DFS_REFER_RSP *pSMBr,
  4227. unsigned int *num_of_nodes,
  4228. struct dfs_info3_param **target_nodes,
  4229. const struct nls_table *nls_codepage, int remap,
  4230. const char *searchName)
  4231. {
  4232. int i, rc = 0;
  4233. char *data_end;
  4234. bool is_unicode;
  4235. struct dfs_referral_level_3 *ref;
  4236. if (pSMBr->hdr.Flags2 & SMBFLG2_UNICODE)
  4237. is_unicode = true;
  4238. else
  4239. is_unicode = false;
  4240. *num_of_nodes = le16_to_cpu(pSMBr->NumberOfReferrals);
  4241. if (*num_of_nodes < 1) {
  4242. cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
  4243. *num_of_nodes);
  4244. rc = -EINVAL;
  4245. goto parse_DFS_referrals_exit;
  4246. }
  4247. ref = (struct dfs_referral_level_3 *) &(pSMBr->referrals);
  4248. if (ref->VersionNumber != cpu_to_le16(3)) {
  4249. cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
  4250. le16_to_cpu(ref->VersionNumber));
  4251. rc = -EINVAL;
  4252. goto parse_DFS_referrals_exit;
  4253. }
  4254. /* get the upper boundary of the resp buffer */
  4255. data_end = (char *)(&(pSMBr->PathConsumed)) +
  4256. le16_to_cpu(pSMBr->t2.DataCount);
  4257. cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
  4258. *num_of_nodes, le32_to_cpu(pSMBr->DFSFlags));
  4259. *target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
  4260. GFP_KERNEL);
  4261. if (*target_nodes == NULL) {
  4262. rc = -ENOMEM;
  4263. goto parse_DFS_referrals_exit;
  4264. }
  4265. /* collect necessary data from referrals */
  4266. for (i = 0; i < *num_of_nodes; i++) {
  4267. char *temp;
  4268. int max_len;
  4269. struct dfs_info3_param *node = (*target_nodes)+i;
  4270. node->flags = le32_to_cpu(pSMBr->DFSFlags);
  4271. if (is_unicode) {
  4272. __le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
  4273. GFP_KERNEL);
  4274. if (tmp == NULL) {
  4275. rc = -ENOMEM;
  4276. goto parse_DFS_referrals_exit;
  4277. }
  4278. cifsConvertToUTF16((__le16 *) tmp, searchName,
  4279. PATH_MAX, nls_codepage, remap);
  4280. node->path_consumed = cifs_utf16_bytes(tmp,
  4281. le16_to_cpu(pSMBr->PathConsumed),
  4282. nls_codepage);
  4283. kfree(tmp);
  4284. } else
  4285. node->path_consumed = le16_to_cpu(pSMBr->PathConsumed);
  4286. node->server_type = le16_to_cpu(ref->ServerType);
  4287. node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
  4288. /* copy DfsPath */
  4289. temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
  4290. max_len = data_end - temp;
  4291. node->path_name = cifs_strndup_from_utf16(temp, max_len,
  4292. is_unicode, nls_codepage);
  4293. if (!node->path_name) {
  4294. rc = -ENOMEM;
  4295. goto parse_DFS_referrals_exit;
  4296. }
  4297. /* copy link target UNC */
  4298. temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
  4299. max_len = data_end - temp;
  4300. node->node_name = cifs_strndup_from_utf16(temp, max_len,
  4301. is_unicode, nls_codepage);
  4302. if (!node->node_name) {
  4303. rc = -ENOMEM;
  4304. goto parse_DFS_referrals_exit;
  4305. }
  4306. ref++;
  4307. }
  4308. parse_DFS_referrals_exit:
  4309. if (rc) {
  4310. free_dfs_info_array(*target_nodes, *num_of_nodes);
  4311. *target_nodes = NULL;
  4312. *num_of_nodes = 0;
  4313. }
  4314. return rc;
  4315. }
  4316. int
  4317. CIFSGetDFSRefer(const unsigned int xid, struct cifs_ses *ses,
  4318. const char *search_name, struct dfs_info3_param **target_nodes,
  4319. unsigned int *num_of_nodes,
  4320. const struct nls_table *nls_codepage, int remap)
  4321. {
  4322. /* TRANS2_GET_DFS_REFERRAL */
  4323. TRANSACTION2_GET_DFS_REFER_REQ *pSMB = NULL;
  4324. TRANSACTION2_GET_DFS_REFER_RSP *pSMBr = NULL;
  4325. int rc = 0;
  4326. int bytes_returned;
  4327. int name_len;
  4328. __u16 params, byte_count;
  4329. *num_of_nodes = 0;
  4330. *target_nodes = NULL;
  4331. cifs_dbg(FYI, "In GetDFSRefer the path %s\n", search_name);
  4332. if (ses == NULL)
  4333. return -ENODEV;
  4334. getDFSRetry:
  4335. rc = smb_init(SMB_COM_TRANSACTION2, 15, NULL, (void **) &pSMB,
  4336. (void **) &pSMBr);
  4337. if (rc)
  4338. return rc;
  4339. /* server pointer checked in called function,
  4340. but should never be null here anyway */
  4341. pSMB->hdr.Mid = get_next_mid(ses->server);
  4342. pSMB->hdr.Tid = ses->ipc_tid;
  4343. pSMB->hdr.Uid = ses->Suid;
  4344. if (ses->capabilities & CAP_STATUS32)
  4345. pSMB->hdr.Flags2 |= SMBFLG2_ERR_STATUS;
  4346. if (ses->capabilities & CAP_DFS)
  4347. pSMB->hdr.Flags2 |= SMBFLG2_DFS;
  4348. if (ses->capabilities & CAP_UNICODE) {
  4349. pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
  4350. name_len =
  4351. cifsConvertToUTF16((__le16 *) pSMB->RequestFileName,
  4352. search_name, PATH_MAX, nls_codepage,
  4353. remap);
  4354. name_len++; /* trailing null */
  4355. name_len *= 2;
  4356. } else { /* BB improve the check for buffer overruns BB */
  4357. name_len = strnlen(search_name, PATH_MAX);
  4358. name_len++; /* trailing null */
  4359. strncpy(pSMB->RequestFileName, search_name, name_len);
  4360. }
  4361. if (ses->server->sign)
  4362. pSMB->hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  4363. pSMB->hdr.Uid = ses->Suid;
  4364. params = 2 /* level */ + name_len /*includes null */ ;
  4365. pSMB->TotalDataCount = 0;
  4366. pSMB->DataCount = 0;
  4367. pSMB->DataOffset = 0;
  4368. pSMB->MaxParameterCount = 0;
  4369. /* BB find exact max SMB PDU from sess structure BB */
  4370. pSMB->MaxDataCount = cpu_to_le16(4000);
  4371. pSMB->MaxSetupCount = 0;
  4372. pSMB->Reserved = 0;
  4373. pSMB->Flags = 0;
  4374. pSMB->Timeout = 0;
  4375. pSMB->Reserved2 = 0;
  4376. pSMB->ParameterOffset = cpu_to_le16(offsetof(
  4377. struct smb_com_transaction2_get_dfs_refer_req, MaxReferralLevel) - 4);
  4378. pSMB->SetupCount = 1;
  4379. pSMB->Reserved3 = 0;
  4380. pSMB->SubCommand = cpu_to_le16(TRANS2_GET_DFS_REFERRAL);
  4381. byte_count = params + 3 /* pad */ ;
  4382. pSMB->ParameterCount = cpu_to_le16(params);
  4383. pSMB->TotalParameterCount = pSMB->ParameterCount;
  4384. pSMB->MaxReferralLevel = cpu_to_le16(3);
  4385. inc_rfc1001_len(pSMB, byte_count);
  4386. pSMB->ByteCount = cpu_to_le16(byte_count);
  4387. rc = SendReceive(xid, ses, (struct smb_hdr *) pSMB,
  4388. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  4389. if (rc) {
  4390. cifs_dbg(FYI, "Send error in GetDFSRefer = %d\n", rc);
  4391. goto GetDFSRefExit;
  4392. }
  4393. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  4394. /* BB Also check if enough total bytes returned? */
  4395. if (rc || get_bcc(&pSMBr->hdr) < 17) {
  4396. rc = -EIO; /* bad smb */
  4397. goto GetDFSRefExit;
  4398. }
  4399. cifs_dbg(FYI, "Decoding GetDFSRefer response BCC: %d Offset %d\n",
  4400. get_bcc(&pSMBr->hdr), le16_to_cpu(pSMBr->t2.DataOffset));
  4401. /* parse returned result into more usable form */
  4402. rc = parse_DFS_referrals(pSMBr, num_of_nodes,
  4403. target_nodes, nls_codepage, remap,
  4404. search_name);
  4405. GetDFSRefExit:
  4406. cifs_buf_release(pSMB);
  4407. if (rc == -EAGAIN)
  4408. goto getDFSRetry;
  4409. return rc;
  4410. }
  4411. /* Query File System Info such as free space to old servers such as Win 9x */
  4412. int
  4413. SMBOldQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
  4414. struct kstatfs *FSData)
  4415. {
  4416. /* level 0x01 SMB_QUERY_FILE_SYSTEM_INFO */
  4417. TRANSACTION2_QFSI_REQ *pSMB = NULL;
  4418. TRANSACTION2_QFSI_RSP *pSMBr = NULL;
  4419. FILE_SYSTEM_ALLOC_INFO *response_data;
  4420. int rc = 0;
  4421. int bytes_returned = 0;
  4422. __u16 params, byte_count;
  4423. cifs_dbg(FYI, "OldQFSInfo\n");
  4424. oldQFSInfoRetry:
  4425. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  4426. (void **) &pSMBr);
  4427. if (rc)
  4428. return rc;
  4429. params = 2; /* level */
  4430. pSMB->TotalDataCount = 0;
  4431. pSMB->MaxParameterCount = cpu_to_le16(2);
  4432. pSMB->MaxDataCount = cpu_to_le16(1000);
  4433. pSMB->MaxSetupCount = 0;
  4434. pSMB->Reserved = 0;
  4435. pSMB->Flags = 0;
  4436. pSMB->Timeout = 0;
  4437. pSMB->Reserved2 = 0;
  4438. byte_count = params + 1 /* pad */ ;
  4439. pSMB->TotalParameterCount = cpu_to_le16(params);
  4440. pSMB->ParameterCount = pSMB->TotalParameterCount;
  4441. pSMB->ParameterOffset = cpu_to_le16(offsetof(
  4442. struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
  4443. pSMB->DataCount = 0;
  4444. pSMB->DataOffset = 0;
  4445. pSMB->SetupCount = 1;
  4446. pSMB->Reserved3 = 0;
  4447. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
  4448. pSMB->InformationLevel = cpu_to_le16(SMB_INFO_ALLOCATION);
  4449. inc_rfc1001_len(pSMB, byte_count);
  4450. pSMB->ByteCount = cpu_to_le16(byte_count);
  4451. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  4452. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  4453. if (rc) {
  4454. cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
  4455. } else { /* decode response */
  4456. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  4457. if (rc || get_bcc(&pSMBr->hdr) < 18)
  4458. rc = -EIO; /* bad smb */
  4459. else {
  4460. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  4461. cifs_dbg(FYI, "qfsinf resp BCC: %d Offset %d\n",
  4462. get_bcc(&pSMBr->hdr), data_offset);
  4463. response_data = (FILE_SYSTEM_ALLOC_INFO *)
  4464. (((char *) &pSMBr->hdr.Protocol) + data_offset);
  4465. FSData->f_bsize =
  4466. le16_to_cpu(response_data->BytesPerSector) *
  4467. le32_to_cpu(response_data->
  4468. SectorsPerAllocationUnit);
  4469. FSData->f_blocks =
  4470. le32_to_cpu(response_data->TotalAllocationUnits);
  4471. FSData->f_bfree = FSData->f_bavail =
  4472. le32_to_cpu(response_data->FreeAllocationUnits);
  4473. cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
  4474. (unsigned long long)FSData->f_blocks,
  4475. (unsigned long long)FSData->f_bfree,
  4476. FSData->f_bsize);
  4477. }
  4478. }
  4479. cifs_buf_release(pSMB);
  4480. if (rc == -EAGAIN)
  4481. goto oldQFSInfoRetry;
  4482. return rc;
  4483. }
  4484. int
  4485. CIFSSMBQFSInfo(const unsigned int xid, struct cifs_tcon *tcon,
  4486. struct kstatfs *FSData)
  4487. {
  4488. /* level 0x103 SMB_QUERY_FILE_SYSTEM_INFO */
  4489. TRANSACTION2_QFSI_REQ *pSMB = NULL;
  4490. TRANSACTION2_QFSI_RSP *pSMBr = NULL;
  4491. FILE_SYSTEM_INFO *response_data;
  4492. int rc = 0;
  4493. int bytes_returned = 0;
  4494. __u16 params, byte_count;
  4495. cifs_dbg(FYI, "In QFSInfo\n");
  4496. QFSInfoRetry:
  4497. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  4498. (void **) &pSMBr);
  4499. if (rc)
  4500. return rc;
  4501. params = 2; /* level */
  4502. pSMB->TotalDataCount = 0;
  4503. pSMB->MaxParameterCount = cpu_to_le16(2);
  4504. pSMB->MaxDataCount = cpu_to_le16(1000);
  4505. pSMB->MaxSetupCount = 0;
  4506. pSMB->Reserved = 0;
  4507. pSMB->Flags = 0;
  4508. pSMB->Timeout = 0;
  4509. pSMB->Reserved2 = 0;
  4510. byte_count = params + 1 /* pad */ ;
  4511. pSMB->TotalParameterCount = cpu_to_le16(params);
  4512. pSMB->ParameterCount = pSMB->TotalParameterCount;
  4513. pSMB->ParameterOffset = cpu_to_le16(offsetof(
  4514. struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
  4515. pSMB->DataCount = 0;
  4516. pSMB->DataOffset = 0;
  4517. pSMB->SetupCount = 1;
  4518. pSMB->Reserved3 = 0;
  4519. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
  4520. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_SIZE_INFO);
  4521. inc_rfc1001_len(pSMB, byte_count);
  4522. pSMB->ByteCount = cpu_to_le16(byte_count);
  4523. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  4524. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  4525. if (rc) {
  4526. cifs_dbg(FYI, "Send error in QFSInfo = %d\n", rc);
  4527. } else { /* decode response */
  4528. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  4529. if (rc || get_bcc(&pSMBr->hdr) < 24)
  4530. rc = -EIO; /* bad smb */
  4531. else {
  4532. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  4533. response_data =
  4534. (FILE_SYSTEM_INFO
  4535. *) (((char *) &pSMBr->hdr.Protocol) +
  4536. data_offset);
  4537. FSData->f_bsize =
  4538. le32_to_cpu(response_data->BytesPerSector) *
  4539. le32_to_cpu(response_data->
  4540. SectorsPerAllocationUnit);
  4541. FSData->f_blocks =
  4542. le64_to_cpu(response_data->TotalAllocationUnits);
  4543. FSData->f_bfree = FSData->f_bavail =
  4544. le64_to_cpu(response_data->FreeAllocationUnits);
  4545. cifs_dbg(FYI, "Blocks: %lld Free: %lld Block size %ld\n",
  4546. (unsigned long long)FSData->f_blocks,
  4547. (unsigned long long)FSData->f_bfree,
  4548. FSData->f_bsize);
  4549. }
  4550. }
  4551. cifs_buf_release(pSMB);
  4552. if (rc == -EAGAIN)
  4553. goto QFSInfoRetry;
  4554. return rc;
  4555. }
  4556. int
  4557. CIFSSMBQFSAttributeInfo(const unsigned int xid, struct cifs_tcon *tcon)
  4558. {
  4559. /* level 0x105 SMB_QUERY_FILE_SYSTEM_INFO */
  4560. TRANSACTION2_QFSI_REQ *pSMB = NULL;
  4561. TRANSACTION2_QFSI_RSP *pSMBr = NULL;
  4562. FILE_SYSTEM_ATTRIBUTE_INFO *response_data;
  4563. int rc = 0;
  4564. int bytes_returned = 0;
  4565. __u16 params, byte_count;
  4566. cifs_dbg(FYI, "In QFSAttributeInfo\n");
  4567. QFSAttributeRetry:
  4568. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  4569. (void **) &pSMBr);
  4570. if (rc)
  4571. return rc;
  4572. params = 2; /* level */
  4573. pSMB->TotalDataCount = 0;
  4574. pSMB->MaxParameterCount = cpu_to_le16(2);
  4575. /* BB find exact max SMB PDU from sess structure BB */
  4576. pSMB->MaxDataCount = cpu_to_le16(1000);
  4577. pSMB->MaxSetupCount = 0;
  4578. pSMB->Reserved = 0;
  4579. pSMB->Flags = 0;
  4580. pSMB->Timeout = 0;
  4581. pSMB->Reserved2 = 0;
  4582. byte_count = params + 1 /* pad */ ;
  4583. pSMB->TotalParameterCount = cpu_to_le16(params);
  4584. pSMB->ParameterCount = pSMB->TotalParameterCount;
  4585. pSMB->ParameterOffset = cpu_to_le16(offsetof(
  4586. struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
  4587. pSMB->DataCount = 0;
  4588. pSMB->DataOffset = 0;
  4589. pSMB->SetupCount = 1;
  4590. pSMB->Reserved3 = 0;
  4591. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
  4592. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_ATTRIBUTE_INFO);
  4593. inc_rfc1001_len(pSMB, byte_count);
  4594. pSMB->ByteCount = cpu_to_le16(byte_count);
  4595. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  4596. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  4597. if (rc) {
  4598. cifs_dbg(VFS, "Send error in QFSAttributeInfo = %d\n", rc);
  4599. } else { /* decode response */
  4600. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  4601. if (rc || get_bcc(&pSMBr->hdr) < 13) {
  4602. /* BB also check if enough bytes returned */
  4603. rc = -EIO; /* bad smb */
  4604. } else {
  4605. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  4606. response_data =
  4607. (FILE_SYSTEM_ATTRIBUTE_INFO
  4608. *) (((char *) &pSMBr->hdr.Protocol) +
  4609. data_offset);
  4610. memcpy(&tcon->fsAttrInfo, response_data,
  4611. sizeof(FILE_SYSTEM_ATTRIBUTE_INFO));
  4612. }
  4613. }
  4614. cifs_buf_release(pSMB);
  4615. if (rc == -EAGAIN)
  4616. goto QFSAttributeRetry;
  4617. return rc;
  4618. }
  4619. int
  4620. CIFSSMBQFSDeviceInfo(const unsigned int xid, struct cifs_tcon *tcon)
  4621. {
  4622. /* level 0x104 SMB_QUERY_FILE_SYSTEM_INFO */
  4623. TRANSACTION2_QFSI_REQ *pSMB = NULL;
  4624. TRANSACTION2_QFSI_RSP *pSMBr = NULL;
  4625. FILE_SYSTEM_DEVICE_INFO *response_data;
  4626. int rc = 0;
  4627. int bytes_returned = 0;
  4628. __u16 params, byte_count;
  4629. cifs_dbg(FYI, "In QFSDeviceInfo\n");
  4630. QFSDeviceRetry:
  4631. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  4632. (void **) &pSMBr);
  4633. if (rc)
  4634. return rc;
  4635. params = 2; /* level */
  4636. pSMB->TotalDataCount = 0;
  4637. pSMB->MaxParameterCount = cpu_to_le16(2);
  4638. /* BB find exact max SMB PDU from sess structure BB */
  4639. pSMB->MaxDataCount = cpu_to_le16(1000);
  4640. pSMB->MaxSetupCount = 0;
  4641. pSMB->Reserved = 0;
  4642. pSMB->Flags = 0;
  4643. pSMB->Timeout = 0;
  4644. pSMB->Reserved2 = 0;
  4645. byte_count = params + 1 /* pad */ ;
  4646. pSMB->TotalParameterCount = cpu_to_le16(params);
  4647. pSMB->ParameterCount = pSMB->TotalParameterCount;
  4648. pSMB->ParameterOffset = cpu_to_le16(offsetof(
  4649. struct smb_com_transaction2_qfsi_req, InformationLevel) - 4);
  4650. pSMB->DataCount = 0;
  4651. pSMB->DataOffset = 0;
  4652. pSMB->SetupCount = 1;
  4653. pSMB->Reserved3 = 0;
  4654. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
  4655. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_FS_DEVICE_INFO);
  4656. inc_rfc1001_len(pSMB, byte_count);
  4657. pSMB->ByteCount = cpu_to_le16(byte_count);
  4658. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  4659. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  4660. if (rc) {
  4661. cifs_dbg(FYI, "Send error in QFSDeviceInfo = %d\n", rc);
  4662. } else { /* decode response */
  4663. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  4664. if (rc || get_bcc(&pSMBr->hdr) <
  4665. sizeof(FILE_SYSTEM_DEVICE_INFO))
  4666. rc = -EIO; /* bad smb */
  4667. else {
  4668. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  4669. response_data =
  4670. (FILE_SYSTEM_DEVICE_INFO *)
  4671. (((char *) &pSMBr->hdr.Protocol) +
  4672. data_offset);
  4673. memcpy(&tcon->fsDevInfo, response_data,
  4674. sizeof(FILE_SYSTEM_DEVICE_INFO));
  4675. }
  4676. }
  4677. cifs_buf_release(pSMB);
  4678. if (rc == -EAGAIN)
  4679. goto QFSDeviceRetry;
  4680. return rc;
  4681. }
  4682. int
  4683. CIFSSMBQFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon)
  4684. {
  4685. /* level 0x200 SMB_QUERY_CIFS_UNIX_INFO */
  4686. TRANSACTION2_QFSI_REQ *pSMB = NULL;
  4687. TRANSACTION2_QFSI_RSP *pSMBr = NULL;
  4688. FILE_SYSTEM_UNIX_INFO *response_data;
  4689. int rc = 0;
  4690. int bytes_returned = 0;
  4691. __u16 params, byte_count;
  4692. cifs_dbg(FYI, "In QFSUnixInfo\n");
  4693. QFSUnixRetry:
  4694. rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
  4695. (void **) &pSMB, (void **) &pSMBr);
  4696. if (rc)
  4697. return rc;
  4698. params = 2; /* level */
  4699. pSMB->TotalDataCount = 0;
  4700. pSMB->DataCount = 0;
  4701. pSMB->DataOffset = 0;
  4702. pSMB->MaxParameterCount = cpu_to_le16(2);
  4703. /* BB find exact max SMB PDU from sess structure BB */
  4704. pSMB->MaxDataCount = cpu_to_le16(100);
  4705. pSMB->MaxSetupCount = 0;
  4706. pSMB->Reserved = 0;
  4707. pSMB->Flags = 0;
  4708. pSMB->Timeout = 0;
  4709. pSMB->Reserved2 = 0;
  4710. byte_count = params + 1 /* pad */ ;
  4711. pSMB->ParameterCount = cpu_to_le16(params);
  4712. pSMB->TotalParameterCount = pSMB->ParameterCount;
  4713. pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
  4714. smb_com_transaction2_qfsi_req, InformationLevel) - 4);
  4715. pSMB->SetupCount = 1;
  4716. pSMB->Reserved3 = 0;
  4717. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
  4718. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_CIFS_UNIX_INFO);
  4719. inc_rfc1001_len(pSMB, byte_count);
  4720. pSMB->ByteCount = cpu_to_le16(byte_count);
  4721. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  4722. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  4723. if (rc) {
  4724. cifs_dbg(VFS, "Send error in QFSUnixInfo = %d\n", rc);
  4725. } else { /* decode response */
  4726. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  4727. if (rc || get_bcc(&pSMBr->hdr) < 13) {
  4728. rc = -EIO; /* bad smb */
  4729. } else {
  4730. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  4731. response_data =
  4732. (FILE_SYSTEM_UNIX_INFO
  4733. *) (((char *) &pSMBr->hdr.Protocol) +
  4734. data_offset);
  4735. memcpy(&tcon->fsUnixInfo, response_data,
  4736. sizeof(FILE_SYSTEM_UNIX_INFO));
  4737. }
  4738. }
  4739. cifs_buf_release(pSMB);
  4740. if (rc == -EAGAIN)
  4741. goto QFSUnixRetry;
  4742. return rc;
  4743. }
  4744. int
  4745. CIFSSMBSetFSUnixInfo(const unsigned int xid, struct cifs_tcon *tcon, __u64 cap)
  4746. {
  4747. /* level 0x200 SMB_SET_CIFS_UNIX_INFO */
  4748. TRANSACTION2_SETFSI_REQ *pSMB = NULL;
  4749. TRANSACTION2_SETFSI_RSP *pSMBr = NULL;
  4750. int rc = 0;
  4751. int bytes_returned = 0;
  4752. __u16 params, param_offset, offset, byte_count;
  4753. cifs_dbg(FYI, "In SETFSUnixInfo\n");
  4754. SETFSUnixRetry:
  4755. /* BB switch to small buf init to save memory */
  4756. rc = smb_init_no_reconnect(SMB_COM_TRANSACTION2, 15, tcon,
  4757. (void **) &pSMB, (void **) &pSMBr);
  4758. if (rc)
  4759. return rc;
  4760. params = 4; /* 2 bytes zero followed by info level. */
  4761. pSMB->MaxSetupCount = 0;
  4762. pSMB->Reserved = 0;
  4763. pSMB->Flags = 0;
  4764. pSMB->Timeout = 0;
  4765. pSMB->Reserved2 = 0;
  4766. param_offset = offsetof(struct smb_com_transaction2_setfsi_req, FileNum)
  4767. - 4;
  4768. offset = param_offset + params;
  4769. pSMB->MaxParameterCount = cpu_to_le16(4);
  4770. /* BB find exact max SMB PDU from sess structure BB */
  4771. pSMB->MaxDataCount = cpu_to_le16(100);
  4772. pSMB->SetupCount = 1;
  4773. pSMB->Reserved3 = 0;
  4774. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FS_INFORMATION);
  4775. byte_count = 1 /* pad */ + params + 12;
  4776. pSMB->DataCount = cpu_to_le16(12);
  4777. pSMB->ParameterCount = cpu_to_le16(params);
  4778. pSMB->TotalDataCount = pSMB->DataCount;
  4779. pSMB->TotalParameterCount = pSMB->ParameterCount;
  4780. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  4781. pSMB->DataOffset = cpu_to_le16(offset);
  4782. /* Params. */
  4783. pSMB->FileNum = 0;
  4784. pSMB->InformationLevel = cpu_to_le16(SMB_SET_CIFS_UNIX_INFO);
  4785. /* Data. */
  4786. pSMB->ClientUnixMajor = cpu_to_le16(CIFS_UNIX_MAJOR_VERSION);
  4787. pSMB->ClientUnixMinor = cpu_to_le16(CIFS_UNIX_MINOR_VERSION);
  4788. pSMB->ClientUnixCap = cpu_to_le64(cap);
  4789. inc_rfc1001_len(pSMB, byte_count);
  4790. pSMB->ByteCount = cpu_to_le16(byte_count);
  4791. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  4792. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  4793. if (rc) {
  4794. cifs_dbg(VFS, "Send error in SETFSUnixInfo = %d\n", rc);
  4795. } else { /* decode response */
  4796. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  4797. if (rc)
  4798. rc = -EIO; /* bad smb */
  4799. }
  4800. cifs_buf_release(pSMB);
  4801. if (rc == -EAGAIN)
  4802. goto SETFSUnixRetry;
  4803. return rc;
  4804. }
  4805. int
  4806. CIFSSMBQFSPosixInfo(const unsigned int xid, struct cifs_tcon *tcon,
  4807. struct kstatfs *FSData)
  4808. {
  4809. /* level 0x201 SMB_QUERY_CIFS_POSIX_INFO */
  4810. TRANSACTION2_QFSI_REQ *pSMB = NULL;
  4811. TRANSACTION2_QFSI_RSP *pSMBr = NULL;
  4812. FILE_SYSTEM_POSIX_INFO *response_data;
  4813. int rc = 0;
  4814. int bytes_returned = 0;
  4815. __u16 params, byte_count;
  4816. cifs_dbg(FYI, "In QFSPosixInfo\n");
  4817. QFSPosixRetry:
  4818. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  4819. (void **) &pSMBr);
  4820. if (rc)
  4821. return rc;
  4822. params = 2; /* level */
  4823. pSMB->TotalDataCount = 0;
  4824. pSMB->DataCount = 0;
  4825. pSMB->DataOffset = 0;
  4826. pSMB->MaxParameterCount = cpu_to_le16(2);
  4827. /* BB find exact max SMB PDU from sess structure BB */
  4828. pSMB->MaxDataCount = cpu_to_le16(100);
  4829. pSMB->MaxSetupCount = 0;
  4830. pSMB->Reserved = 0;
  4831. pSMB->Flags = 0;
  4832. pSMB->Timeout = 0;
  4833. pSMB->Reserved2 = 0;
  4834. byte_count = params + 1 /* pad */ ;
  4835. pSMB->ParameterCount = cpu_to_le16(params);
  4836. pSMB->TotalParameterCount = pSMB->ParameterCount;
  4837. pSMB->ParameterOffset = cpu_to_le16(offsetof(struct
  4838. smb_com_transaction2_qfsi_req, InformationLevel) - 4);
  4839. pSMB->SetupCount = 1;
  4840. pSMB->Reserved3 = 0;
  4841. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_FS_INFORMATION);
  4842. pSMB->InformationLevel = cpu_to_le16(SMB_QUERY_POSIX_FS_INFO);
  4843. inc_rfc1001_len(pSMB, byte_count);
  4844. pSMB->ByteCount = cpu_to_le16(byte_count);
  4845. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  4846. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  4847. if (rc) {
  4848. cifs_dbg(FYI, "Send error in QFSUnixInfo = %d\n", rc);
  4849. } else { /* decode response */
  4850. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  4851. if (rc || get_bcc(&pSMBr->hdr) < 13) {
  4852. rc = -EIO; /* bad smb */
  4853. } else {
  4854. __u16 data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  4855. response_data =
  4856. (FILE_SYSTEM_POSIX_INFO
  4857. *) (((char *) &pSMBr->hdr.Protocol) +
  4858. data_offset);
  4859. FSData->f_bsize =
  4860. le32_to_cpu(response_data->BlockSize);
  4861. FSData->f_blocks =
  4862. le64_to_cpu(response_data->TotalBlocks);
  4863. FSData->f_bfree =
  4864. le64_to_cpu(response_data->BlocksAvail);
  4865. if (response_data->UserBlocksAvail == cpu_to_le64(-1)) {
  4866. FSData->f_bavail = FSData->f_bfree;
  4867. } else {
  4868. FSData->f_bavail =
  4869. le64_to_cpu(response_data->UserBlocksAvail);
  4870. }
  4871. if (response_data->TotalFileNodes != cpu_to_le64(-1))
  4872. FSData->f_files =
  4873. le64_to_cpu(response_data->TotalFileNodes);
  4874. if (response_data->FreeFileNodes != cpu_to_le64(-1))
  4875. FSData->f_ffree =
  4876. le64_to_cpu(response_data->FreeFileNodes);
  4877. }
  4878. }
  4879. cifs_buf_release(pSMB);
  4880. if (rc == -EAGAIN)
  4881. goto QFSPosixRetry;
  4882. return rc;
  4883. }
  4884. /*
  4885. * We can not use write of zero bytes trick to set file size due to need for
  4886. * large file support. Also note that this SetPathInfo is preferred to
  4887. * SetFileInfo based method in next routine which is only needed to work around
  4888. * a sharing violation bugin Samba which this routine can run into.
  4889. */
  4890. int
  4891. CIFSSMBSetEOF(const unsigned int xid, struct cifs_tcon *tcon,
  4892. const char *file_name, __u64 size, struct cifs_sb_info *cifs_sb,
  4893. bool set_allocation)
  4894. {
  4895. struct smb_com_transaction2_spi_req *pSMB = NULL;
  4896. struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
  4897. struct file_end_of_file_info *parm_data;
  4898. int name_len;
  4899. int rc = 0;
  4900. int bytes_returned = 0;
  4901. int remap = cifs_remap(cifs_sb);
  4902. __u16 params, byte_count, data_count, param_offset, offset;
  4903. cifs_dbg(FYI, "In SetEOF\n");
  4904. SetEOFRetry:
  4905. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  4906. (void **) &pSMBr);
  4907. if (rc)
  4908. return rc;
  4909. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  4910. name_len =
  4911. cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
  4912. PATH_MAX, cifs_sb->local_nls, remap);
  4913. name_len++; /* trailing null */
  4914. name_len *= 2;
  4915. } else { /* BB improve the check for buffer overruns BB */
  4916. name_len = strnlen(file_name, PATH_MAX);
  4917. name_len++; /* trailing null */
  4918. strncpy(pSMB->FileName, file_name, name_len);
  4919. }
  4920. params = 6 + name_len;
  4921. data_count = sizeof(struct file_end_of_file_info);
  4922. pSMB->MaxParameterCount = cpu_to_le16(2);
  4923. pSMB->MaxDataCount = cpu_to_le16(4100);
  4924. pSMB->MaxSetupCount = 0;
  4925. pSMB->Reserved = 0;
  4926. pSMB->Flags = 0;
  4927. pSMB->Timeout = 0;
  4928. pSMB->Reserved2 = 0;
  4929. param_offset = offsetof(struct smb_com_transaction2_spi_req,
  4930. InformationLevel) - 4;
  4931. offset = param_offset + params;
  4932. if (set_allocation) {
  4933. if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
  4934. pSMB->InformationLevel =
  4935. cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
  4936. else
  4937. pSMB->InformationLevel =
  4938. cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
  4939. } else /* Set File Size */ {
  4940. if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
  4941. pSMB->InformationLevel =
  4942. cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
  4943. else
  4944. pSMB->InformationLevel =
  4945. cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
  4946. }
  4947. parm_data =
  4948. (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol) +
  4949. offset);
  4950. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  4951. pSMB->DataOffset = cpu_to_le16(offset);
  4952. pSMB->SetupCount = 1;
  4953. pSMB->Reserved3 = 0;
  4954. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
  4955. byte_count = 3 /* pad */ + params + data_count;
  4956. pSMB->DataCount = cpu_to_le16(data_count);
  4957. pSMB->TotalDataCount = pSMB->DataCount;
  4958. pSMB->ParameterCount = cpu_to_le16(params);
  4959. pSMB->TotalParameterCount = pSMB->ParameterCount;
  4960. pSMB->Reserved4 = 0;
  4961. inc_rfc1001_len(pSMB, byte_count);
  4962. parm_data->FileSize = cpu_to_le64(size);
  4963. pSMB->ByteCount = cpu_to_le16(byte_count);
  4964. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  4965. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  4966. if (rc)
  4967. cifs_dbg(FYI, "SetPathInfo (file size) returned %d\n", rc);
  4968. cifs_buf_release(pSMB);
  4969. if (rc == -EAGAIN)
  4970. goto SetEOFRetry;
  4971. return rc;
  4972. }
  4973. int
  4974. CIFSSMBSetFileSize(const unsigned int xid, struct cifs_tcon *tcon,
  4975. struct cifsFileInfo *cfile, __u64 size, bool set_allocation)
  4976. {
  4977. struct smb_com_transaction2_sfi_req *pSMB = NULL;
  4978. struct file_end_of_file_info *parm_data;
  4979. int rc = 0;
  4980. __u16 params, param_offset, offset, byte_count, count;
  4981. cifs_dbg(FYI, "SetFileSize (via SetFileInfo) %lld\n",
  4982. (long long)size);
  4983. rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
  4984. if (rc)
  4985. return rc;
  4986. pSMB->hdr.Pid = cpu_to_le16((__u16)cfile->pid);
  4987. pSMB->hdr.PidHigh = cpu_to_le16((__u16)(cfile->pid >> 16));
  4988. params = 6;
  4989. pSMB->MaxSetupCount = 0;
  4990. pSMB->Reserved = 0;
  4991. pSMB->Flags = 0;
  4992. pSMB->Timeout = 0;
  4993. pSMB->Reserved2 = 0;
  4994. param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
  4995. offset = param_offset + params;
  4996. count = sizeof(struct file_end_of_file_info);
  4997. pSMB->MaxParameterCount = cpu_to_le16(2);
  4998. /* BB find exact max SMB PDU from sess structure BB */
  4999. pSMB->MaxDataCount = cpu_to_le16(1000);
  5000. pSMB->SetupCount = 1;
  5001. pSMB->Reserved3 = 0;
  5002. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
  5003. byte_count = 3 /* pad */ + params + count;
  5004. pSMB->DataCount = cpu_to_le16(count);
  5005. pSMB->ParameterCount = cpu_to_le16(params);
  5006. pSMB->TotalDataCount = pSMB->DataCount;
  5007. pSMB->TotalParameterCount = pSMB->ParameterCount;
  5008. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  5009. parm_data =
  5010. (struct file_end_of_file_info *) (((char *) &pSMB->hdr.Protocol)
  5011. + offset);
  5012. pSMB->DataOffset = cpu_to_le16(offset);
  5013. parm_data->FileSize = cpu_to_le64(size);
  5014. pSMB->Fid = cfile->fid.netfid;
  5015. if (set_allocation) {
  5016. if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
  5017. pSMB->InformationLevel =
  5018. cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO2);
  5019. else
  5020. pSMB->InformationLevel =
  5021. cpu_to_le16(SMB_SET_FILE_ALLOCATION_INFO);
  5022. } else /* Set File Size */ {
  5023. if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
  5024. pSMB->InformationLevel =
  5025. cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO2);
  5026. else
  5027. pSMB->InformationLevel =
  5028. cpu_to_le16(SMB_SET_FILE_END_OF_FILE_INFO);
  5029. }
  5030. pSMB->Reserved4 = 0;
  5031. inc_rfc1001_len(pSMB, byte_count);
  5032. pSMB->ByteCount = cpu_to_le16(byte_count);
  5033. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
  5034. if (rc) {
  5035. cifs_dbg(FYI, "Send error in SetFileInfo (SetFileSize) = %d\n",
  5036. rc);
  5037. }
  5038. /* Note: On -EAGAIN error only caller can retry on handle based calls
  5039. since file handle passed in no longer valid */
  5040. return rc;
  5041. }
  5042. /* Some legacy servers such as NT4 require that the file times be set on
  5043. an open handle, rather than by pathname - this is awkward due to
  5044. potential access conflicts on the open, but it is unavoidable for these
  5045. old servers since the only other choice is to go from 100 nanosecond DCE
  5046. time and resort to the original setpathinfo level which takes the ancient
  5047. DOS time format with 2 second granularity */
  5048. int
  5049. CIFSSMBSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
  5050. const FILE_BASIC_INFO *data, __u16 fid, __u32 pid_of_opener)
  5051. {
  5052. struct smb_com_transaction2_sfi_req *pSMB = NULL;
  5053. char *data_offset;
  5054. int rc = 0;
  5055. __u16 params, param_offset, offset, byte_count, count;
  5056. cifs_dbg(FYI, "Set Times (via SetFileInfo)\n");
  5057. rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
  5058. if (rc)
  5059. return rc;
  5060. pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
  5061. pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
  5062. params = 6;
  5063. pSMB->MaxSetupCount = 0;
  5064. pSMB->Reserved = 0;
  5065. pSMB->Flags = 0;
  5066. pSMB->Timeout = 0;
  5067. pSMB->Reserved2 = 0;
  5068. param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
  5069. offset = param_offset + params;
  5070. data_offset = (char *)pSMB +
  5071. offsetof(struct smb_hdr, Protocol) + offset;
  5072. count = sizeof(FILE_BASIC_INFO);
  5073. pSMB->MaxParameterCount = cpu_to_le16(2);
  5074. /* BB find max SMB PDU from sess */
  5075. pSMB->MaxDataCount = cpu_to_le16(1000);
  5076. pSMB->SetupCount = 1;
  5077. pSMB->Reserved3 = 0;
  5078. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
  5079. byte_count = 3 /* pad */ + params + count;
  5080. pSMB->DataCount = cpu_to_le16(count);
  5081. pSMB->ParameterCount = cpu_to_le16(params);
  5082. pSMB->TotalDataCount = pSMB->DataCount;
  5083. pSMB->TotalParameterCount = pSMB->ParameterCount;
  5084. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  5085. pSMB->DataOffset = cpu_to_le16(offset);
  5086. pSMB->Fid = fid;
  5087. if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
  5088. pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
  5089. else
  5090. pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
  5091. pSMB->Reserved4 = 0;
  5092. inc_rfc1001_len(pSMB, byte_count);
  5093. pSMB->ByteCount = cpu_to_le16(byte_count);
  5094. memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
  5095. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
  5096. if (rc)
  5097. cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
  5098. rc);
  5099. /* Note: On -EAGAIN error only caller can retry on handle based calls
  5100. since file handle passed in no longer valid */
  5101. return rc;
  5102. }
  5103. int
  5104. CIFSSMBSetFileDisposition(const unsigned int xid, struct cifs_tcon *tcon,
  5105. bool delete_file, __u16 fid, __u32 pid_of_opener)
  5106. {
  5107. struct smb_com_transaction2_sfi_req *pSMB = NULL;
  5108. char *data_offset;
  5109. int rc = 0;
  5110. __u16 params, param_offset, offset, byte_count, count;
  5111. cifs_dbg(FYI, "Set File Disposition (via SetFileInfo)\n");
  5112. rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
  5113. if (rc)
  5114. return rc;
  5115. pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
  5116. pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
  5117. params = 6;
  5118. pSMB->MaxSetupCount = 0;
  5119. pSMB->Reserved = 0;
  5120. pSMB->Flags = 0;
  5121. pSMB->Timeout = 0;
  5122. pSMB->Reserved2 = 0;
  5123. param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
  5124. offset = param_offset + params;
  5125. data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
  5126. count = 1;
  5127. pSMB->MaxParameterCount = cpu_to_le16(2);
  5128. /* BB find max SMB PDU from sess */
  5129. pSMB->MaxDataCount = cpu_to_le16(1000);
  5130. pSMB->SetupCount = 1;
  5131. pSMB->Reserved3 = 0;
  5132. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
  5133. byte_count = 3 /* pad */ + params + count;
  5134. pSMB->DataCount = cpu_to_le16(count);
  5135. pSMB->ParameterCount = cpu_to_le16(params);
  5136. pSMB->TotalDataCount = pSMB->DataCount;
  5137. pSMB->TotalParameterCount = pSMB->ParameterCount;
  5138. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  5139. pSMB->DataOffset = cpu_to_le16(offset);
  5140. pSMB->Fid = fid;
  5141. pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_DISPOSITION_INFO);
  5142. pSMB->Reserved4 = 0;
  5143. inc_rfc1001_len(pSMB, byte_count);
  5144. pSMB->ByteCount = cpu_to_le16(byte_count);
  5145. *data_offset = delete_file ? 1 : 0;
  5146. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
  5147. if (rc)
  5148. cifs_dbg(FYI, "Send error in SetFileDisposition = %d\n", rc);
  5149. return rc;
  5150. }
  5151. int
  5152. CIFSSMBSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
  5153. const char *fileName, const FILE_BASIC_INFO *data,
  5154. const struct nls_table *nls_codepage, int remap)
  5155. {
  5156. TRANSACTION2_SPI_REQ *pSMB = NULL;
  5157. TRANSACTION2_SPI_RSP *pSMBr = NULL;
  5158. int name_len;
  5159. int rc = 0;
  5160. int bytes_returned = 0;
  5161. char *data_offset;
  5162. __u16 params, param_offset, offset, byte_count, count;
  5163. cifs_dbg(FYI, "In SetTimes\n");
  5164. SetTimesRetry:
  5165. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  5166. (void **) &pSMBr);
  5167. if (rc)
  5168. return rc;
  5169. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  5170. name_len =
  5171. cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
  5172. PATH_MAX, nls_codepage, remap);
  5173. name_len++; /* trailing null */
  5174. name_len *= 2;
  5175. } else { /* BB improve the check for buffer overruns BB */
  5176. name_len = strnlen(fileName, PATH_MAX);
  5177. name_len++; /* trailing null */
  5178. strncpy(pSMB->FileName, fileName, name_len);
  5179. }
  5180. params = 6 + name_len;
  5181. count = sizeof(FILE_BASIC_INFO);
  5182. pSMB->MaxParameterCount = cpu_to_le16(2);
  5183. /* BB find max SMB PDU from sess structure BB */
  5184. pSMB->MaxDataCount = cpu_to_le16(1000);
  5185. pSMB->MaxSetupCount = 0;
  5186. pSMB->Reserved = 0;
  5187. pSMB->Flags = 0;
  5188. pSMB->Timeout = 0;
  5189. pSMB->Reserved2 = 0;
  5190. param_offset = offsetof(struct smb_com_transaction2_spi_req,
  5191. InformationLevel) - 4;
  5192. offset = param_offset + params;
  5193. data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
  5194. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  5195. pSMB->DataOffset = cpu_to_le16(offset);
  5196. pSMB->SetupCount = 1;
  5197. pSMB->Reserved3 = 0;
  5198. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
  5199. byte_count = 3 /* pad */ + params + count;
  5200. pSMB->DataCount = cpu_to_le16(count);
  5201. pSMB->ParameterCount = cpu_to_le16(params);
  5202. pSMB->TotalDataCount = pSMB->DataCount;
  5203. pSMB->TotalParameterCount = pSMB->ParameterCount;
  5204. if (tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)
  5205. pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO2);
  5206. else
  5207. pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_BASIC_INFO);
  5208. pSMB->Reserved4 = 0;
  5209. inc_rfc1001_len(pSMB, byte_count);
  5210. memcpy(data_offset, data, sizeof(FILE_BASIC_INFO));
  5211. pSMB->ByteCount = cpu_to_le16(byte_count);
  5212. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  5213. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  5214. if (rc)
  5215. cifs_dbg(FYI, "SetPathInfo (times) returned %d\n", rc);
  5216. cifs_buf_release(pSMB);
  5217. if (rc == -EAGAIN)
  5218. goto SetTimesRetry;
  5219. return rc;
  5220. }
  5221. /* Can not be used to set time stamps yet (due to old DOS time format) */
  5222. /* Can be used to set attributes */
  5223. #if 0 /* Possibly not needed - since it turns out that strangely NT4 has a bug
  5224. handling it anyway and NT4 was what we thought it would be needed for
  5225. Do not delete it until we prove whether needed for Win9x though */
  5226. int
  5227. CIFSSMBSetAttrLegacy(unsigned int xid, struct cifs_tcon *tcon, char *fileName,
  5228. __u16 dos_attrs, const struct nls_table *nls_codepage)
  5229. {
  5230. SETATTR_REQ *pSMB = NULL;
  5231. SETATTR_RSP *pSMBr = NULL;
  5232. int rc = 0;
  5233. int bytes_returned;
  5234. int name_len;
  5235. cifs_dbg(FYI, "In SetAttrLegacy\n");
  5236. SetAttrLgcyRetry:
  5237. rc = smb_init(SMB_COM_SETATTR, 8, tcon, (void **) &pSMB,
  5238. (void **) &pSMBr);
  5239. if (rc)
  5240. return rc;
  5241. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  5242. name_len =
  5243. ConvertToUTF16((__le16 *) pSMB->fileName, fileName,
  5244. PATH_MAX, nls_codepage);
  5245. name_len++; /* trailing null */
  5246. name_len *= 2;
  5247. } else { /* BB improve the check for buffer overruns BB */
  5248. name_len = strnlen(fileName, PATH_MAX);
  5249. name_len++; /* trailing null */
  5250. strncpy(pSMB->fileName, fileName, name_len);
  5251. }
  5252. pSMB->attr = cpu_to_le16(dos_attrs);
  5253. pSMB->BufferFormat = 0x04;
  5254. inc_rfc1001_len(pSMB, name_len + 1);
  5255. pSMB->ByteCount = cpu_to_le16(name_len + 1);
  5256. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  5257. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  5258. if (rc)
  5259. cifs_dbg(FYI, "Error in LegacySetAttr = %d\n", rc);
  5260. cifs_buf_release(pSMB);
  5261. if (rc == -EAGAIN)
  5262. goto SetAttrLgcyRetry;
  5263. return rc;
  5264. }
  5265. #endif /* temporarily unneeded SetAttr legacy function */
  5266. static void
  5267. cifs_fill_unix_set_info(FILE_UNIX_BASIC_INFO *data_offset,
  5268. const struct cifs_unix_set_info_args *args)
  5269. {
  5270. u64 uid = NO_CHANGE_64, gid = NO_CHANGE_64;
  5271. u64 mode = args->mode;
  5272. if (uid_valid(args->uid))
  5273. uid = from_kuid(&init_user_ns, args->uid);
  5274. if (gid_valid(args->gid))
  5275. gid = from_kgid(&init_user_ns, args->gid);
  5276. /*
  5277. * Samba server ignores set of file size to zero due to bugs in some
  5278. * older clients, but we should be precise - we use SetFileSize to
  5279. * set file size and do not want to truncate file size to zero
  5280. * accidentally as happened on one Samba server beta by putting
  5281. * zero instead of -1 here
  5282. */
  5283. data_offset->EndOfFile = cpu_to_le64(NO_CHANGE_64);
  5284. data_offset->NumOfBytes = cpu_to_le64(NO_CHANGE_64);
  5285. data_offset->LastStatusChange = cpu_to_le64(args->ctime);
  5286. data_offset->LastAccessTime = cpu_to_le64(args->atime);
  5287. data_offset->LastModificationTime = cpu_to_le64(args->mtime);
  5288. data_offset->Uid = cpu_to_le64(uid);
  5289. data_offset->Gid = cpu_to_le64(gid);
  5290. /* better to leave device as zero when it is */
  5291. data_offset->DevMajor = cpu_to_le64(MAJOR(args->device));
  5292. data_offset->DevMinor = cpu_to_le64(MINOR(args->device));
  5293. data_offset->Permissions = cpu_to_le64(mode);
  5294. if (S_ISREG(mode))
  5295. data_offset->Type = cpu_to_le32(UNIX_FILE);
  5296. else if (S_ISDIR(mode))
  5297. data_offset->Type = cpu_to_le32(UNIX_DIR);
  5298. else if (S_ISLNK(mode))
  5299. data_offset->Type = cpu_to_le32(UNIX_SYMLINK);
  5300. else if (S_ISCHR(mode))
  5301. data_offset->Type = cpu_to_le32(UNIX_CHARDEV);
  5302. else if (S_ISBLK(mode))
  5303. data_offset->Type = cpu_to_le32(UNIX_BLOCKDEV);
  5304. else if (S_ISFIFO(mode))
  5305. data_offset->Type = cpu_to_le32(UNIX_FIFO);
  5306. else if (S_ISSOCK(mode))
  5307. data_offset->Type = cpu_to_le32(UNIX_SOCKET);
  5308. }
  5309. int
  5310. CIFSSMBUnixSetFileInfo(const unsigned int xid, struct cifs_tcon *tcon,
  5311. const struct cifs_unix_set_info_args *args,
  5312. u16 fid, u32 pid_of_opener)
  5313. {
  5314. struct smb_com_transaction2_sfi_req *pSMB = NULL;
  5315. char *data_offset;
  5316. int rc = 0;
  5317. u16 params, param_offset, offset, byte_count, count;
  5318. cifs_dbg(FYI, "Set Unix Info (via SetFileInfo)\n");
  5319. rc = small_smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB);
  5320. if (rc)
  5321. return rc;
  5322. pSMB->hdr.Pid = cpu_to_le16((__u16)pid_of_opener);
  5323. pSMB->hdr.PidHigh = cpu_to_le16((__u16)(pid_of_opener >> 16));
  5324. params = 6;
  5325. pSMB->MaxSetupCount = 0;
  5326. pSMB->Reserved = 0;
  5327. pSMB->Flags = 0;
  5328. pSMB->Timeout = 0;
  5329. pSMB->Reserved2 = 0;
  5330. param_offset = offsetof(struct smb_com_transaction2_sfi_req, Fid) - 4;
  5331. offset = param_offset + params;
  5332. data_offset = (char *)pSMB +
  5333. offsetof(struct smb_hdr, Protocol) + offset;
  5334. count = sizeof(FILE_UNIX_BASIC_INFO);
  5335. pSMB->MaxParameterCount = cpu_to_le16(2);
  5336. /* BB find max SMB PDU from sess */
  5337. pSMB->MaxDataCount = cpu_to_le16(1000);
  5338. pSMB->SetupCount = 1;
  5339. pSMB->Reserved3 = 0;
  5340. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_FILE_INFORMATION);
  5341. byte_count = 3 /* pad */ + params + count;
  5342. pSMB->DataCount = cpu_to_le16(count);
  5343. pSMB->ParameterCount = cpu_to_le16(params);
  5344. pSMB->TotalDataCount = pSMB->DataCount;
  5345. pSMB->TotalParameterCount = pSMB->ParameterCount;
  5346. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  5347. pSMB->DataOffset = cpu_to_le16(offset);
  5348. pSMB->Fid = fid;
  5349. pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
  5350. pSMB->Reserved4 = 0;
  5351. inc_rfc1001_len(pSMB, byte_count);
  5352. pSMB->ByteCount = cpu_to_le16(byte_count);
  5353. cifs_fill_unix_set_info((FILE_UNIX_BASIC_INFO *)data_offset, args);
  5354. rc = SendReceiveNoRsp(xid, tcon->ses, (char *) pSMB, 0);
  5355. if (rc)
  5356. cifs_dbg(FYI, "Send error in Set Time (SetFileInfo) = %d\n",
  5357. rc);
  5358. /* Note: On -EAGAIN error only caller can retry on handle based calls
  5359. since file handle passed in no longer valid */
  5360. return rc;
  5361. }
  5362. int
  5363. CIFSSMBUnixSetPathInfo(const unsigned int xid, struct cifs_tcon *tcon,
  5364. const char *file_name,
  5365. const struct cifs_unix_set_info_args *args,
  5366. const struct nls_table *nls_codepage, int remap)
  5367. {
  5368. TRANSACTION2_SPI_REQ *pSMB = NULL;
  5369. TRANSACTION2_SPI_RSP *pSMBr = NULL;
  5370. int name_len;
  5371. int rc = 0;
  5372. int bytes_returned = 0;
  5373. FILE_UNIX_BASIC_INFO *data_offset;
  5374. __u16 params, param_offset, offset, count, byte_count;
  5375. cifs_dbg(FYI, "In SetUID/GID/Mode\n");
  5376. setPermsRetry:
  5377. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  5378. (void **) &pSMBr);
  5379. if (rc)
  5380. return rc;
  5381. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  5382. name_len =
  5383. cifsConvertToUTF16((__le16 *) pSMB->FileName, file_name,
  5384. PATH_MAX, nls_codepage, remap);
  5385. name_len++; /* trailing null */
  5386. name_len *= 2;
  5387. } else { /* BB improve the check for buffer overruns BB */
  5388. name_len = strnlen(file_name, PATH_MAX);
  5389. name_len++; /* trailing null */
  5390. strncpy(pSMB->FileName, file_name, name_len);
  5391. }
  5392. params = 6 + name_len;
  5393. count = sizeof(FILE_UNIX_BASIC_INFO);
  5394. pSMB->MaxParameterCount = cpu_to_le16(2);
  5395. /* BB find max SMB PDU from sess structure BB */
  5396. pSMB->MaxDataCount = cpu_to_le16(1000);
  5397. pSMB->MaxSetupCount = 0;
  5398. pSMB->Reserved = 0;
  5399. pSMB->Flags = 0;
  5400. pSMB->Timeout = 0;
  5401. pSMB->Reserved2 = 0;
  5402. param_offset = offsetof(struct smb_com_transaction2_spi_req,
  5403. InformationLevel) - 4;
  5404. offset = param_offset + params;
  5405. data_offset =
  5406. (FILE_UNIX_BASIC_INFO *) ((char *) &pSMB->hdr.Protocol +
  5407. offset);
  5408. memset(data_offset, 0, count);
  5409. pSMB->DataOffset = cpu_to_le16(offset);
  5410. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  5411. pSMB->SetupCount = 1;
  5412. pSMB->Reserved3 = 0;
  5413. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
  5414. byte_count = 3 /* pad */ + params + count;
  5415. pSMB->ParameterCount = cpu_to_le16(params);
  5416. pSMB->DataCount = cpu_to_le16(count);
  5417. pSMB->TotalParameterCount = pSMB->ParameterCount;
  5418. pSMB->TotalDataCount = pSMB->DataCount;
  5419. pSMB->InformationLevel = cpu_to_le16(SMB_SET_FILE_UNIX_BASIC);
  5420. pSMB->Reserved4 = 0;
  5421. inc_rfc1001_len(pSMB, byte_count);
  5422. cifs_fill_unix_set_info(data_offset, args);
  5423. pSMB->ByteCount = cpu_to_le16(byte_count);
  5424. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  5425. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  5426. if (rc)
  5427. cifs_dbg(FYI, "SetPathInfo (perms) returned %d\n", rc);
  5428. cifs_buf_release(pSMB);
  5429. if (rc == -EAGAIN)
  5430. goto setPermsRetry;
  5431. return rc;
  5432. }
  5433. #ifdef CONFIG_CIFS_XATTR
  5434. /*
  5435. * Do a path-based QUERY_ALL_EAS call and parse the result. This is a common
  5436. * function used by listxattr and getxattr type calls. When ea_name is set,
  5437. * it looks for that attribute name and stuffs that value into the EAData
  5438. * buffer. When ea_name is NULL, it stuffs a list of attribute names into the
  5439. * buffer. In both cases, the return value is either the length of the
  5440. * resulting data or a negative error code. If EAData is a NULL pointer then
  5441. * the data isn't copied to it, but the length is returned.
  5442. */
  5443. ssize_t
  5444. CIFSSMBQAllEAs(const unsigned int xid, struct cifs_tcon *tcon,
  5445. const unsigned char *searchName, const unsigned char *ea_name,
  5446. char *EAData, size_t buf_size,
  5447. const struct nls_table *nls_codepage, int remap)
  5448. {
  5449. /* BB assumes one setup word */
  5450. TRANSACTION2_QPI_REQ *pSMB = NULL;
  5451. TRANSACTION2_QPI_RSP *pSMBr = NULL;
  5452. int rc = 0;
  5453. int bytes_returned;
  5454. int list_len;
  5455. struct fealist *ea_response_data;
  5456. struct fea *temp_fea;
  5457. char *temp_ptr;
  5458. char *end_of_smb;
  5459. __u16 params, byte_count, data_offset;
  5460. unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
  5461. cifs_dbg(FYI, "In Query All EAs path %s\n", searchName);
  5462. QAllEAsRetry:
  5463. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  5464. (void **) &pSMBr);
  5465. if (rc)
  5466. return rc;
  5467. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  5468. list_len =
  5469. cifsConvertToUTF16((__le16 *) pSMB->FileName, searchName,
  5470. PATH_MAX, nls_codepage, remap);
  5471. list_len++; /* trailing null */
  5472. list_len *= 2;
  5473. } else { /* BB improve the check for buffer overruns BB */
  5474. list_len = strnlen(searchName, PATH_MAX);
  5475. list_len++; /* trailing null */
  5476. strncpy(pSMB->FileName, searchName, list_len);
  5477. }
  5478. params = 2 /* level */ + 4 /* reserved */ + list_len /* includes NUL */;
  5479. pSMB->TotalDataCount = 0;
  5480. pSMB->MaxParameterCount = cpu_to_le16(2);
  5481. /* BB find exact max SMB PDU from sess structure BB */
  5482. pSMB->MaxDataCount = cpu_to_le16(CIFSMaxBufSize);
  5483. pSMB->MaxSetupCount = 0;
  5484. pSMB->Reserved = 0;
  5485. pSMB->Flags = 0;
  5486. pSMB->Timeout = 0;
  5487. pSMB->Reserved2 = 0;
  5488. pSMB->ParameterOffset = cpu_to_le16(offsetof(
  5489. struct smb_com_transaction2_qpi_req, InformationLevel) - 4);
  5490. pSMB->DataCount = 0;
  5491. pSMB->DataOffset = 0;
  5492. pSMB->SetupCount = 1;
  5493. pSMB->Reserved3 = 0;
  5494. pSMB->SubCommand = cpu_to_le16(TRANS2_QUERY_PATH_INFORMATION);
  5495. byte_count = params + 1 /* pad */ ;
  5496. pSMB->TotalParameterCount = cpu_to_le16(params);
  5497. pSMB->ParameterCount = pSMB->TotalParameterCount;
  5498. pSMB->InformationLevel = cpu_to_le16(SMB_INFO_QUERY_ALL_EAS);
  5499. pSMB->Reserved4 = 0;
  5500. inc_rfc1001_len(pSMB, byte_count);
  5501. pSMB->ByteCount = cpu_to_le16(byte_count);
  5502. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  5503. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  5504. if (rc) {
  5505. cifs_dbg(FYI, "Send error in QueryAllEAs = %d\n", rc);
  5506. goto QAllEAsOut;
  5507. }
  5508. /* BB also check enough total bytes returned */
  5509. /* BB we need to improve the validity checking
  5510. of these trans2 responses */
  5511. rc = validate_t2((struct smb_t2_rsp *)pSMBr);
  5512. if (rc || get_bcc(&pSMBr->hdr) < 4) {
  5513. rc = -EIO; /* bad smb */
  5514. goto QAllEAsOut;
  5515. }
  5516. /* check that length of list is not more than bcc */
  5517. /* check that each entry does not go beyond length
  5518. of list */
  5519. /* check that each element of each entry does not
  5520. go beyond end of list */
  5521. /* validate_trans2_offsets() */
  5522. /* BB check if start of smb + data_offset > &bcc+ bcc */
  5523. data_offset = le16_to_cpu(pSMBr->t2.DataOffset);
  5524. ea_response_data = (struct fealist *)
  5525. (((char *) &pSMBr->hdr.Protocol) + data_offset);
  5526. list_len = le32_to_cpu(ea_response_data->list_len);
  5527. cifs_dbg(FYI, "ea length %d\n", list_len);
  5528. if (list_len <= 8) {
  5529. cifs_dbg(FYI, "empty EA list returned from server\n");
  5530. /* didn't find the named attribute */
  5531. if (ea_name)
  5532. rc = -ENODATA;
  5533. goto QAllEAsOut;
  5534. }
  5535. /* make sure list_len doesn't go past end of SMB */
  5536. end_of_smb = (char *)pByteArea(&pSMBr->hdr) + get_bcc(&pSMBr->hdr);
  5537. if ((char *)ea_response_data + list_len > end_of_smb) {
  5538. cifs_dbg(FYI, "EA list appears to go beyond SMB\n");
  5539. rc = -EIO;
  5540. goto QAllEAsOut;
  5541. }
  5542. /* account for ea list len */
  5543. list_len -= 4;
  5544. temp_fea = ea_response_data->list;
  5545. temp_ptr = (char *)temp_fea;
  5546. while (list_len > 0) {
  5547. unsigned int name_len;
  5548. __u16 value_len;
  5549. list_len -= 4;
  5550. temp_ptr += 4;
  5551. /* make sure we can read name_len and value_len */
  5552. if (list_len < 0) {
  5553. cifs_dbg(FYI, "EA entry goes beyond length of list\n");
  5554. rc = -EIO;
  5555. goto QAllEAsOut;
  5556. }
  5557. name_len = temp_fea->name_len;
  5558. value_len = le16_to_cpu(temp_fea->value_len);
  5559. list_len -= name_len + 1 + value_len;
  5560. if (list_len < 0) {
  5561. cifs_dbg(FYI, "EA entry goes beyond length of list\n");
  5562. rc = -EIO;
  5563. goto QAllEAsOut;
  5564. }
  5565. if (ea_name) {
  5566. if (ea_name_len == name_len &&
  5567. memcmp(ea_name, temp_ptr, name_len) == 0) {
  5568. temp_ptr += name_len + 1;
  5569. rc = value_len;
  5570. if (buf_size == 0)
  5571. goto QAllEAsOut;
  5572. if ((size_t)value_len > buf_size) {
  5573. rc = -ERANGE;
  5574. goto QAllEAsOut;
  5575. }
  5576. memcpy(EAData, temp_ptr, value_len);
  5577. goto QAllEAsOut;
  5578. }
  5579. } else {
  5580. /* account for prefix user. and trailing null */
  5581. rc += (5 + 1 + name_len);
  5582. if (rc < (int) buf_size) {
  5583. memcpy(EAData, "user.", 5);
  5584. EAData += 5;
  5585. memcpy(EAData, temp_ptr, name_len);
  5586. EAData += name_len;
  5587. /* null terminate name */
  5588. *EAData = 0;
  5589. ++EAData;
  5590. } else if (buf_size == 0) {
  5591. /* skip copy - calc size only */
  5592. } else {
  5593. /* stop before overrun buffer */
  5594. rc = -ERANGE;
  5595. break;
  5596. }
  5597. }
  5598. temp_ptr += name_len + 1 + value_len;
  5599. temp_fea = (struct fea *)temp_ptr;
  5600. }
  5601. /* didn't find the named attribute */
  5602. if (ea_name)
  5603. rc = -ENODATA;
  5604. QAllEAsOut:
  5605. cifs_buf_release(pSMB);
  5606. if (rc == -EAGAIN)
  5607. goto QAllEAsRetry;
  5608. return (ssize_t)rc;
  5609. }
  5610. int
  5611. CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
  5612. const char *fileName, const char *ea_name, const void *ea_value,
  5613. const __u16 ea_value_len, const struct nls_table *nls_codepage,
  5614. int remap)
  5615. {
  5616. struct smb_com_transaction2_spi_req *pSMB = NULL;
  5617. struct smb_com_transaction2_spi_rsp *pSMBr = NULL;
  5618. struct fealist *parm_data;
  5619. int name_len;
  5620. int rc = 0;
  5621. int bytes_returned = 0;
  5622. __u16 params, param_offset, byte_count, offset, count;
  5623. cifs_dbg(FYI, "In SetEA\n");
  5624. SetEARetry:
  5625. rc = smb_init(SMB_COM_TRANSACTION2, 15, tcon, (void **) &pSMB,
  5626. (void **) &pSMBr);
  5627. if (rc)
  5628. return rc;
  5629. if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
  5630. name_len =
  5631. cifsConvertToUTF16((__le16 *) pSMB->FileName, fileName,
  5632. PATH_MAX, nls_codepage, remap);
  5633. name_len++; /* trailing null */
  5634. name_len *= 2;
  5635. } else { /* BB improve the check for buffer overruns BB */
  5636. name_len = strnlen(fileName, PATH_MAX);
  5637. name_len++; /* trailing null */
  5638. strncpy(pSMB->FileName, fileName, name_len);
  5639. }
  5640. params = 6 + name_len;
  5641. /* done calculating parms using name_len of file name,
  5642. now use name_len to calculate length of ea name
  5643. we are going to create in the inode xattrs */
  5644. if (ea_name == NULL)
  5645. name_len = 0;
  5646. else
  5647. name_len = strnlen(ea_name, 255);
  5648. count = sizeof(*parm_data) + ea_value_len + name_len;
  5649. pSMB->MaxParameterCount = cpu_to_le16(2);
  5650. /* BB find max SMB PDU from sess */
  5651. pSMB->MaxDataCount = cpu_to_le16(1000);
  5652. pSMB->MaxSetupCount = 0;
  5653. pSMB->Reserved = 0;
  5654. pSMB->Flags = 0;
  5655. pSMB->Timeout = 0;
  5656. pSMB->Reserved2 = 0;
  5657. param_offset = offsetof(struct smb_com_transaction2_spi_req,
  5658. InformationLevel) - 4;
  5659. offset = param_offset + params;
  5660. pSMB->InformationLevel =
  5661. cpu_to_le16(SMB_SET_FILE_EA);
  5662. parm_data = (void *)pSMB + offsetof(struct smb_hdr, Protocol) + offset;
  5663. pSMB->ParameterOffset = cpu_to_le16(param_offset);
  5664. pSMB->DataOffset = cpu_to_le16(offset);
  5665. pSMB->SetupCount = 1;
  5666. pSMB->Reserved3 = 0;
  5667. pSMB->SubCommand = cpu_to_le16(TRANS2_SET_PATH_INFORMATION);
  5668. byte_count = 3 /* pad */ + params + count;
  5669. pSMB->DataCount = cpu_to_le16(count);
  5670. parm_data->list_len = cpu_to_le32(count);
  5671. parm_data->list[0].EA_flags = 0;
  5672. /* we checked above that name len is less than 255 */
  5673. parm_data->list[0].name_len = (__u8)name_len;
  5674. /* EA names are always ASCII */
  5675. if (ea_name)
  5676. strncpy(parm_data->list[0].name, ea_name, name_len);
  5677. parm_data->list[0].name[name_len] = 0;
  5678. parm_data->list[0].value_len = cpu_to_le16(ea_value_len);
  5679. /* caller ensures that ea_value_len is less than 64K but
  5680. we need to ensure that it fits within the smb */
  5681. /*BB add length check to see if it would fit in
  5682. negotiated SMB buffer size BB */
  5683. /* if (ea_value_len > buffer_size - 512 (enough for header)) */
  5684. if (ea_value_len)
  5685. memcpy(parm_data->list[0].name+name_len+1,
  5686. ea_value, ea_value_len);
  5687. pSMB->TotalDataCount = pSMB->DataCount;
  5688. pSMB->ParameterCount = cpu_to_le16(params);
  5689. pSMB->TotalParameterCount = pSMB->ParameterCount;
  5690. pSMB->Reserved4 = 0;
  5691. inc_rfc1001_len(pSMB, byte_count);
  5692. pSMB->ByteCount = cpu_to_le16(byte_count);
  5693. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  5694. (struct smb_hdr *) pSMBr, &bytes_returned, 0);
  5695. if (rc)
  5696. cifs_dbg(FYI, "SetPathInfo (EA) returned %d\n", rc);
  5697. cifs_buf_release(pSMB);
  5698. if (rc == -EAGAIN)
  5699. goto SetEARetry;
  5700. return rc;
  5701. }
  5702. #endif
  5703. #ifdef CONFIG_CIFS_DNOTIFY_EXPERIMENTAL /* BB unused temporarily */
  5704. /*
  5705. * Years ago the kernel added a "dnotify" function for Samba server,
  5706. * to allow network clients (such as Windows) to display updated
  5707. * lists of files in directory listings automatically when
  5708. * files are added by one user when another user has the
  5709. * same directory open on their desktop. The Linux cifs kernel
  5710. * client hooked into the kernel side of this interface for
  5711. * the same reason, but ironically when the VFS moved from
  5712. * "dnotify" to "inotify" it became harder to plug in Linux
  5713. * network file system clients (the most obvious use case
  5714. * for notify interfaces is when multiple users can update
  5715. * the contents of the same directory - exactly what network
  5716. * file systems can do) although the server (Samba) could
  5717. * still use it. For the short term we leave the worker
  5718. * function ifdeffed out (below) until inotify is fixed
  5719. * in the VFS to make it easier to plug in network file
  5720. * system clients. If inotify turns out to be permanently
  5721. * incompatible for network fs clients, we could instead simply
  5722. * expose this config flag by adding a future cifs (and smb2) notify ioctl.
  5723. */
  5724. int CIFSSMBNotify(const unsigned int xid, struct cifs_tcon *tcon,
  5725. const int notify_subdirs, const __u16 netfid,
  5726. __u32 filter, struct file *pfile, int multishot,
  5727. const struct nls_table *nls_codepage)
  5728. {
  5729. int rc = 0;
  5730. struct smb_com_transaction_change_notify_req *pSMB = NULL;
  5731. struct smb_com_ntransaction_change_notify_rsp *pSMBr = NULL;
  5732. struct dir_notify_req *dnotify_req;
  5733. int bytes_returned;
  5734. cifs_dbg(FYI, "In CIFSSMBNotify for file handle %d\n", (int)netfid);
  5735. rc = smb_init(SMB_COM_NT_TRANSACT, 23, tcon, (void **) &pSMB,
  5736. (void **) &pSMBr);
  5737. if (rc)
  5738. return rc;
  5739. pSMB->TotalParameterCount = 0 ;
  5740. pSMB->TotalDataCount = 0;
  5741. pSMB->MaxParameterCount = cpu_to_le32(2);
  5742. pSMB->MaxDataCount = cpu_to_le32(CIFSMaxBufSize & 0xFFFFFF00);
  5743. pSMB->MaxSetupCount = 4;
  5744. pSMB->Reserved = 0;
  5745. pSMB->ParameterOffset = 0;
  5746. pSMB->DataCount = 0;
  5747. pSMB->DataOffset = 0;
  5748. pSMB->SetupCount = 4; /* single byte does not need le conversion */
  5749. pSMB->SubCommand = cpu_to_le16(NT_TRANSACT_NOTIFY_CHANGE);
  5750. pSMB->ParameterCount = pSMB->TotalParameterCount;
  5751. if (notify_subdirs)
  5752. pSMB->WatchTree = 1; /* one byte - no le conversion needed */
  5753. pSMB->Reserved2 = 0;
  5754. pSMB->CompletionFilter = cpu_to_le32(filter);
  5755. pSMB->Fid = netfid; /* file handle always le */
  5756. pSMB->ByteCount = 0;
  5757. rc = SendReceive(xid, tcon->ses, (struct smb_hdr *) pSMB,
  5758. (struct smb_hdr *)pSMBr, &bytes_returned,
  5759. CIFS_ASYNC_OP);
  5760. if (rc) {
  5761. cifs_dbg(FYI, "Error in Notify = %d\n", rc);
  5762. } else {
  5763. /* Add file to outstanding requests */
  5764. /* BB change to kmem cache alloc */
  5765. dnotify_req = kmalloc(
  5766. sizeof(struct dir_notify_req),
  5767. GFP_KERNEL);
  5768. if (dnotify_req) {
  5769. dnotify_req->Pid = pSMB->hdr.Pid;
  5770. dnotify_req->PidHigh = pSMB->hdr.PidHigh;
  5771. dnotify_req->Mid = pSMB->hdr.Mid;
  5772. dnotify_req->Tid = pSMB->hdr.Tid;
  5773. dnotify_req->Uid = pSMB->hdr.Uid;
  5774. dnotify_req->netfid = netfid;
  5775. dnotify_req->pfile = pfile;
  5776. dnotify_req->filter = filter;
  5777. dnotify_req->multishot = multishot;
  5778. spin_lock(&GlobalMid_Lock);
  5779. list_add_tail(&dnotify_req->lhead,
  5780. &GlobalDnotifyReqList);
  5781. spin_unlock(&GlobalMid_Lock);
  5782. } else
  5783. rc = -ENOMEM;
  5784. }
  5785. cifs_buf_release(pSMB);
  5786. return rc;
  5787. }
  5788. #endif /* was needed for dnotify, and will be needed for inotify when VFS fix */