cifssmb.c 192 KB

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