cifssmb.c 155 KB

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