cifssmb.c 164 KB

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