cifssmb.c 180 KB

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