cifssmb.c 187 KB

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